git: leverage parse_git_ref() to test reference kind

This commit is contained in:
Yuya Nishihara 2023-06-28 18:48:16 +09:00
parent e6d5df1010
commit 1d9552406b

View file

@ -42,6 +42,8 @@ fn parse_git_ref(ref_name: &str) -> Option<RefName> {
} else if let Some(remote_and_branch) = ref_name.strip_prefix("refs/remotes/") { } else if let Some(remote_and_branch) = ref_name.strip_prefix("refs/remotes/") {
remote_and_branch remote_and_branch
.split_once('/') .split_once('/')
// "refs/remotes/origin/HEAD" isn't a real remote-tracking branch
.filter(|&(_, branch)| branch != "HEAD")
.map(|(remote, branch)| RefName::RemoteBranch { .map(|(remote, branch)| RefName::RemoteBranch {
remote: remote.to_string(), remote: remote.to_string(),
branch: branch.to_string(), branch: branch.to_string(),
@ -176,21 +178,20 @@ pub fn import_some_refs(
let git_repo_refs = git_repo.references()?; let git_repo_refs = git_repo.references()?;
for git_repo_ref in git_repo_refs { for git_repo_ref in git_repo_refs {
let git_repo_ref = git_repo_ref?; let git_repo_ref = git_repo_ref?;
if !(git_repo_ref.is_tag() || git_repo_ref.is_branch() || git_repo_ref.is_remote()) let full_name = if let Some(full_name) = git_repo_ref.name() {
|| git_repo_ref.name().is_none() full_name
{ } else {
// Skip other refs (such as notes) and symbolic refs, as well as non-utf8 refs. // Skip non-utf8 refs.
continue; continue;
} };
let full_name = git_repo_ref.name().unwrap().to_string(); let _ref_name = if let Some(ref_name) = parse_git_ref(full_name) {
if let Some(RefName::RemoteBranch { branch, remote: _ }) = parse_git_ref(&full_name) { ref_name
// "refs/remotes/origin/HEAD" isn't a real remote-tracking branch } else {
if &branch == "HEAD" { // Skip other refs (such as notes) and symbolic refs.
continue; continue;
} };
}
let id = if let Some(id) = let id = if let Some(id) =
resolve_git_ref_to_commit_id(&git_repo_ref, jj_view_git_refs.get(&full_name)) resolve_git_ref_to_commit_id(&git_repo_ref, jj_view_git_refs.get(full_name))
{ {
id id
} else { } else {
@ -198,19 +199,19 @@ pub fn import_some_refs(
continue; continue;
}; };
pinned_git_heads.insert(full_name.to_string(), vec![id.clone()]); pinned_git_heads.insert(full_name.to_string(), vec![id.clone()]);
if !git_ref_filter(&full_name) { if !git_ref_filter(full_name) {
continue; continue;
} }
// TODO: Make it configurable which remotes are publishing and update public // TODO: Make it configurable which remotes are publishing and update public
// heads here. // heads here.
let old_target = jj_view_git_refs.remove(&full_name); let old_target = jj_view_git_refs.remove(full_name);
let new_target = Some(RefTarget::Normal(id.clone())); let new_target = Some(RefTarget::Normal(id.clone()));
if new_target != old_target { if new_target != old_target {
prevent_gc(git_repo, &id)?; prevent_gc(git_repo, &id)?;
mut_repo.set_git_ref(full_name.clone(), RefTarget::Normal(id.clone())); mut_repo.set_git_ref(full_name.to_owned(), RefTarget::Normal(id.clone()));
let commit = store.get_commit(&id).unwrap(); let commit = store.get_commit(&id).unwrap();
mut_repo.add_head(&commit); mut_repo.add_head(&commit);
changed_git_refs.insert(full_name, (old_target, new_target)); changed_git_refs.insert(full_name.to_owned(), (old_target, new_target));
} }
} }
for (full_name, target) in jj_view_git_refs { for (full_name, target) in jj_view_git_refs {