git: don't import refs/remotes/origin/HEAD as a branch called "HEAD"

`refs/remotes/origin/*` are usually (remote-tracking) branches, but
`refs/remotes/origin/HEAD` is not.
This commit is contained in:
Martin von Zweigbergk 2021-11-28 15:00:46 -08:00
parent 1143b1ab6c
commit a27e77205a
2 changed files with 9 additions and 7 deletions

View File

@ -65,6 +65,13 @@ pub fn import_refs(
// TODO: Is it useful to import HEAD (especially if it's detached)?
continue;
}
let full_name = git_ref.name().unwrap().to_string();
if let Some(RefName::RemoteBranch { branch, remote: _ }) = parse_git_ref(&full_name) {
// "refs/remotes/origin/HEAD" isn't a real remote-tracking branch
if &branch == "HEAD" {
continue;
}
}
let git_commit = match git_ref.peel_to_commit() {
Ok(git_commit) => git_commit,
Err(_) => {
@ -77,7 +84,6 @@ pub fn import_refs(
mut_repo.add_head(&commit);
// TODO: Make it configurable which remotes are publishing and update public
// heads here.
let full_name = git_ref.name().unwrap().to_string();
mut_repo.set_git_ref(full_name.clone(), RefTarget::Normal(id.clone()));
let old_target = existing_git_refs.remove(&full_name);
let new_target = Some(RefTarget::Normal(id));

View File

@ -67,6 +67,7 @@ fn test_import_refs() {
let commit5 = empty_git_commit(&git_repo, "refs/tags/v1.0", &[&commit1]);
// Should not be imported
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]);
let git_repo = repo.store().git_repo().unwrap();
let mut tx = repo.start_transaction("test");
@ -308,9 +309,8 @@ fn test_fetch_success() {
assert_eq!(
*view.git_refs(),
btreemap! {
// The two first refs were created by by git2::Repository::clone()
// The second ref was created by by git2::Repository::clone()
"refs/heads/main".to_string() => initial_commit_target,
"refs/remotes/origin/HEAD".to_string() => new_commit_target.clone(),
"refs/remotes/origin/main".to_string() => new_commit_target.clone(),
}
);
@ -318,10 +318,6 @@ fn test_fetch_success() {
*view.branches(),
btreemap! {
"main".to_string() => BranchTarget {
local_target: Some(new_commit_target.clone()),
remote_targets: btreemap! {"origin".to_string() => new_commit_target.clone()}
},
"HEAD".to_string() => BranchTarget {
local_target: Some(new_commit_target.clone()),
remote_targets: btreemap! {"origin".to_string() => new_commit_target}
},