Skip to content

Commit

Permalink
import_refs/export_refs: refactor conversion from branch names to ref…
Browse files Browse the repository at this point in the history
…s (no-op)

This is supposed to make `import_refs` and `export_refs` a little less prone to typos
  • Loading branch information
ilyagr committed May 17, 2023
1 parent 39dd4a0 commit a9cb997
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ fn parse_git_ref(ref_name: &str) -> Option<RefName> {
}
}

fn ref_name_to_local_branch_name(ref_name: &str) -> Option<&str> {
ref_name.strip_prefix("refs/heads/")
}

fn local_branch_name_to_ref_name(branch: &str) -> String {
format!("refs/heads/{branch}")
}

fn prevent_gc(git_repo: &git2::Repository, id: &CommitId) {
git_repo
.reference(
Expand Down Expand Up @@ -174,9 +182,10 @@ pub fn import_some_refs(
new_git_target.as_ref(),
);
match mut_repo.get_local_branch(&branch) {
None => new_git_heads.remove(&format!("refs/heads/{branch}")),
None => new_git_heads.remove(&local_branch_name_to_ref_name(&branch)),
Some(target) => {
new_git_heads.insert(format!("refs/heads/{branch}"), target.adds())
// Note that we are mostly *replacing*, not inserting
new_git_heads.insert(local_branch_name_to_ref_name(&branch), target.adds())
}
};
}
Expand Down Expand Up @@ -238,11 +247,11 @@ pub fn export_refs(
let all_local_branch_names: HashSet<&str> = view
.git_refs()
.keys()
.filter_map(|git_ref| git_ref.strip_prefix("refs/heads/"))
.filter_map(|r| ref_name_to_local_branch_name(r))
.chain(view.branches().keys().map(AsRef::as_ref))
.collect();
for branch_name in all_local_branch_names {
let old_branch = view.get_git_ref(&format!("refs/heads/{branch_name}"));
let old_branch = view.get_git_ref(&local_branch_name_to_ref_name(branch_name));
let new_branch = view.get_local_branch(branch_name);
if new_branch == old_branch {
continue;
Expand Down Expand Up @@ -277,7 +286,7 @@ pub fn export_refs(
if let (Some(head_git_ref), Ok(current_git_commit)) =
(head_ref.symbolic_target(), head_ref.peel_to_commit())
{
if let Some(branch_name) = head_git_ref.strip_prefix("refs/heads/") {
if let Some(branch_name) = ref_name_to_local_branch_name(head_git_ref) {
let detach_head =
if let Some((_old_oid, new_oid)) = branches_to_update.get(branch_name) {
*new_oid != current_git_commit.id()
Expand All @@ -291,7 +300,7 @@ pub fn export_refs(
}
}
for (branch_name, old_oid) in branches_to_delete {
let git_ref_name = format!("refs/heads/{branch_name}");
let git_ref_name = local_branch_name_to_ref_name(&branch_name);
let success = if let Ok(mut git_ref) = git_repo.find_reference(&git_ref_name) {
if git_ref.target() == Some(old_oid) {
// The branch has not been updated by git, so go ahead and delete it
Expand All @@ -311,7 +320,7 @@ pub fn export_refs(
}
}
for (branch_name, (old_oid, new_oid)) in branches_to_update {
let git_ref_name = format!("refs/heads/{branch_name}");
let git_ref_name = local_branch_name_to_ref_name(&branch_name);
let success = match old_oid {
None => {
if let Ok(git_ref) = git_repo.find_reference(&git_ref_name) {
Expand Down

0 comments on commit a9cb997

Please sign in to comment.