view: unify set/remove_git_ref() functions to take Option<RefTarget>

New function takes name by reference since it doesn't always need an owned
String.
This commit is contained in:
Yuya Nishihara 2023-07-11 22:42:35 +09:00
parent 5218591a82
commit 3be462266c
5 changed files with 93 additions and 95 deletions

View file

@ -243,7 +243,7 @@ pub fn import_some_refs(
let new_target = Some(RefTarget::Normal(id.clone()));
if new_target != old_target {
prevent_gc(git_repo, &id)?;
mut_repo.set_git_ref(full_name.to_owned(), RefTarget::Normal(id.clone()));
mut_repo.set_git_ref_target(full_name, Some(RefTarget::Normal(id.clone())));
let commit = store.get_commit(&id).unwrap();
mut_repo.add_head(&commit);
changed_git_refs.insert(ref_name, (old_target, new_target));
@ -253,7 +253,7 @@ pub fn import_some_refs(
// TODO: or clean up invalid ref in case it was stored due to historical bug?
let ref_name = parse_git_ref(&full_name).expect("stored git ref should be parsable");
if git_ref_filter(&ref_name) {
mut_repo.remove_git_ref(&full_name);
mut_repo.set_git_ref_target(&full_name, None);
changed_git_refs.insert(ref_name, (Some(target), None));
} else {
pinned_git_heads.insert(ref_name, target.adds().to_vec());
@ -467,7 +467,7 @@ pub fn export_some_refs(
true
};
if success {
mut_repo.remove_git_ref(&git_ref_name);
mut_repo.set_git_ref_target(&git_ref_name, None);
} else {
failed_branches.push(parsed_ref_name);
}
@ -509,9 +509,9 @@ pub fn export_some_refs(
}
};
if success {
mut_repo.set_git_ref(
git_ref_name,
RefTarget::Normal(CommitId::from_bytes(new_oid.as_bytes())),
mut_repo.set_git_ref_target(
&git_ref_name,
Some(RefTarget::Normal(CommitId::from_bytes(new_oid.as_bytes()))),
);
} else {
failed_branches.push(parsed_ref_name);
@ -543,7 +543,7 @@ pub fn remove_remote(
mut_repo.remove_remote_branch(&branch, remote_name);
}
for git_ref in git_refs_to_delete {
mut_repo.remove_git_ref(&git_ref);
mut_repo.set_git_ref_target(&git_ref, None);
}
Ok(())
}
@ -572,8 +572,8 @@ pub fn rename_remote(
})
.collect_vec();
for (old, new, target) in git_refs {
mut_repo.remove_git_ref(&old);
mut_repo.set_git_ref(new, target);
mut_repo.set_git_ref_target(&old, None);
mut_repo.set_git_ref_target(&new, Some(target));
}
Ok(())
}

View file

@ -1011,12 +1011,8 @@ impl MutableRepo {
self.view.with_ref(|v| v.get_git_ref(name))
}
pub fn set_git_ref(&mut self, name: String, target: RefTarget) {
self.view_mut().set_git_ref(name, target);
}
pub fn remove_git_ref(&mut self, name: &str) {
self.view_mut().remove_git_ref(name);
pub fn set_git_ref_target(&mut self, name: &str, target: Option<RefTarget>) {
self.view_mut().set_git_ref_target(name, target);
}
pub fn git_head(&self) -> Option<RefTarget> {

View file

@ -148,7 +148,7 @@ impl View {
self.set_tag(name, target);
}
RefName::GitRef(name) => {
self.set_git_ref(name, target);
self.set_git_ref_target(&name, Some(target));
}
}
} else {
@ -163,7 +163,7 @@ impl View {
self.remove_tag(&name);
}
RefName::GitRef(name) => {
self.remove_git_ref(&name);
self.set_git_ref_target(&name, None);
}
}
}
@ -250,12 +250,14 @@ impl View {
self.data.git_refs.get(name).cloned()
}
pub fn set_git_ref(&mut self, name: String, target: RefTarget) {
self.data.git_refs.insert(name, target);
}
pub fn remove_git_ref(&mut self, name: &str) {
self.data.git_refs.remove(name);
/// Sets the last imported Git ref to point to the given target. If the
/// target is absent, the reference will be removed.
pub fn set_git_ref_target(&mut self, name: &str, target: Option<RefTarget>) {
if let Some(target) = target {
self.data.git_refs.insert(name.to_owned(), target);
} else {
self.data.git_refs.remove(name);
}
}
/// Sets `HEAD@git` to point to the given target. If the target is absent,

View file

@ -445,9 +445,9 @@ fn test_resolve_symbol_branches() {
"mirror".to_owned(),
mut_repo.get_local_branch("local-remote").unwrap(),
);
mut_repo.set_git_ref(
"refs/heads/local-remote".to_owned(),
mut_repo.get_local_branch("local-remote").unwrap(),
mut_repo.set_git_ref_target(
"refs/heads/local-remote",
mut_repo.get_local_branch("local-remote"),
);
mut_repo.set_local_branch(
@ -680,28 +680,28 @@ fn test_resolve_symbol_git_refs() {
let commit3 = write_random_commit(mut_repo, &settings);
let commit4 = write_random_commit(mut_repo, &settings);
let commit5 = write_random_commit(mut_repo, &settings);
mut_repo.set_git_ref(
"refs/heads/branch1".to_string(),
RefTarget::Normal(commit1.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/branch1",
Some(RefTarget::Normal(commit1.id().clone())),
);
mut_repo.set_git_ref(
"refs/heads/branch2".to_string(),
RefTarget::Normal(commit2.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/branch2",
Some(RefTarget::Normal(commit2.id().clone())),
);
mut_repo.set_git_ref(
"refs/heads/conflicted".to_string(),
RefTarget::Conflict {
mut_repo.set_git_ref_target(
"refs/heads/conflicted",
Some(RefTarget::Conflict {
removes: vec![commit2.id().clone()],
adds: vec![commit1.id().clone(), commit3.id().clone()],
},
}),
);
mut_repo.set_git_ref(
"refs/tags/tag1".to_string(),
RefTarget::Normal(commit2.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/tag1",
Some(RefTarget::Normal(commit2.id().clone())),
);
mut_repo.set_git_ref(
"refs/tags/remotes/origin/branch1".to_string(),
RefTarget::Normal(commit3.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/remotes/origin/branch1",
Some(RefTarget::Normal(commit3.id().clone())),
);
// Nonexistent ref
@ -712,9 +712,9 @@ fn test_resolve_symbol_git_refs() {
);
// Full ref
mut_repo.set_git_ref(
"refs/heads/branch".to_string(),
RefTarget::Normal(commit4.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/branch",
Some(RefTarget::Normal(commit4.id().clone())),
);
assert_eq!(
resolve_symbol(mut_repo, "refs/heads/branch", None).unwrap(),
@ -722,9 +722,9 @@ fn test_resolve_symbol_git_refs() {
);
// Qualified with only heads/
mut_repo.set_git_ref(
"refs/heads/branch".to_string(),
RefTarget::Normal(commit5.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/branch",
Some(RefTarget::Normal(commit5.id().clone())),
);
// branch alone is not recognized
insta::assert_debug_snapshot!(
@ -738,9 +738,9 @@ fn test_resolve_symbol_git_refs() {
],
}
"###);
mut_repo.set_git_ref(
"refs/tags/branch".to_string(),
RefTarget::Normal(commit4.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/branch",
Some(RefTarget::Normal(commit4.id().clone())),
);
// The *tag* branch is recognized
assert_eq!(
@ -754,9 +754,9 @@ fn test_resolve_symbol_git_refs() {
);
// Unqualified tag name
mut_repo.set_git_ref(
"refs/tags/tag".to_string(),
RefTarget::Normal(commit4.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/tag",
Some(RefTarget::Normal(commit4.id().clone())),
);
assert_eq!(
resolve_symbol(mut_repo, "tag", None).unwrap(),
@ -764,9 +764,9 @@ fn test_resolve_symbol_git_refs() {
);
// Unqualified remote-tracking branch name
mut_repo.set_git_ref(
"refs/remotes/origin/remote-branch".to_string(),
RefTarget::Normal(commit2.id().clone()),
mut_repo.set_git_ref_target(
"refs/remotes/origin/remote-branch",
Some(RefTarget::Normal(commit2.id().clone())),
);
assert_eq!(
resolve_symbol(mut_repo, "origin/remote-branch", None).unwrap(),
@ -778,8 +778,8 @@ fn test_resolve_symbol_git_refs() {
mut_repo
.set_wc_commit(ws_id.clone(), commit1.id().clone())
.unwrap();
mut_repo.set_git_ref("@".to_string(), RefTarget::Normal(commit2.id().clone()));
mut_repo.set_git_ref("root".to_string(), RefTarget::Normal(commit3.id().clone()));
mut_repo.set_git_ref_target("@", Some(RefTarget::Normal(commit2.id().clone())));
mut_repo.set_git_ref_target("root", Some(RefTarget::Normal(commit3.id().clone())));
assert_eq!(
resolve_symbol(mut_repo, "@", Some(&ws_id)).unwrap(),
vec![mut_repo.view().get_wc_commit_id(&ws_id).unwrap().clone()]
@ -1615,13 +1615,13 @@ fn test_evaluate_expression_git_refs(use_git: bool) {
// Can get git refs when there are none
assert_eq!(resolve_commit_ids(mut_repo, "git_refs()"), vec![]);
// Can get a mix of git refs
mut_repo.set_git_ref(
"refs/heads/branch1".to_string(),
RefTarget::Normal(commit1.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/branch1",
Some(RefTarget::Normal(commit1.id().clone())),
);
mut_repo.set_git_ref(
"refs/tags/tag1".to_string(),
RefTarget::Normal(commit2.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/tag1",
Some(RefTarget::Normal(commit2.id().clone())),
);
assert_eq!(
resolve_commit_ids(mut_repo, "git_refs()"),
@ -1629,30 +1629,30 @@ fn test_evaluate_expression_git_refs(use_git: bool) {
);
// Two refs pointing to the same commit does not result in a duplicate in the
// revset
mut_repo.set_git_ref(
"refs/tags/tag2".to_string(),
RefTarget::Normal(commit2.id().clone()),
mut_repo.set_git_ref_target(
"refs/tags/tag2",
Some(RefTarget::Normal(commit2.id().clone())),
);
assert_eq!(
resolve_commit_ids(mut_repo, "git_refs()"),
vec![commit2.id().clone(), commit1.id().clone()]
);
// Can get git refs when there are conflicted refs
mut_repo.set_git_ref(
"refs/heads/branch1".to_string(),
RefTarget::Conflict {
mut_repo.set_git_ref_target(
"refs/heads/branch1",
Some(RefTarget::Conflict {
removes: vec![commit1.id().clone()],
adds: vec![commit2.id().clone(), commit3.id().clone()],
},
}),
);
mut_repo.set_git_ref(
"refs/tags/tag1".to_string(),
RefTarget::Conflict {
mut_repo.set_git_ref_target(
"refs/tags/tag1",
Some(RefTarget::Conflict {
removes: vec![commit2.id().clone()],
adds: vec![commit3.id().clone(), commit4.id().clone()],
},
}),
);
mut_repo.remove_git_ref("refs/tags/tag2");
mut_repo.set_git_ref_target("refs/tags/tag2", None);
assert_eq!(
resolve_commit_ids(mut_repo, "git_refs()"),
vec![

View file

@ -263,9 +263,9 @@ fn test_merge_views_branches() {
RefTarget::Normal(main_branch_alternate_tx0.id().clone()),
);
let feature_branch_local_tx0 = write_random_commit(mut_repo, &settings);
mut_repo.set_git_ref(
"feature".to_string(),
RefTarget::Normal(feature_branch_local_tx0.id().clone()),
mut_repo.set_git_ref_target(
"feature",
Some(RefTarget::Normal(feature_branch_local_tx0.id().clone())),
);
let repo = tx.commit();
@ -384,35 +384,35 @@ fn test_merge_views_git_refs() {
let mut tx = repo.start_transaction(&settings, "test");
let mut_repo = tx.mut_repo();
let main_branch_tx0 = write_random_commit(mut_repo, &settings);
mut_repo.set_git_ref(
"refs/heads/main".to_string(),
RefTarget::Normal(main_branch_tx0.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/main",
Some(RefTarget::Normal(main_branch_tx0.id().clone())),
);
let feature_branch_tx0 = write_random_commit(mut_repo, &settings);
mut_repo.set_git_ref(
"refs/heads/feature".to_string(),
RefTarget::Normal(feature_branch_tx0.id().clone()),
mut_repo.set_git_ref_target(
"refs/heads/feature",
Some(RefTarget::Normal(feature_branch_tx0.id().clone())),
);
let repo = tx.commit();
let mut tx1 = repo.start_transaction(&settings, "test");
let main_branch_tx1 = write_random_commit(tx1.mut_repo(), &settings);
tx1.mut_repo().set_git_ref(
"refs/heads/main".to_string(),
RefTarget::Normal(main_branch_tx1.id().clone()),
tx1.mut_repo().set_git_ref_target(
"refs/heads/main",
Some(RefTarget::Normal(main_branch_tx1.id().clone())),
);
let feature_branch_tx1 = write_random_commit(tx1.mut_repo(), &settings);
tx1.mut_repo().set_git_ref(
"refs/heads/feature".to_string(),
RefTarget::Normal(feature_branch_tx1.id().clone()),
tx1.mut_repo().set_git_ref_target(
"refs/heads/feature",
Some(RefTarget::Normal(feature_branch_tx1.id().clone())),
);
tx1.commit();
let mut tx2 = repo.start_transaction(&settings, "test");
let main_branch_tx2 = write_random_commit(tx2.mut_repo(), &settings);
tx2.mut_repo().set_git_ref(
"refs/heads/main".to_string(),
RefTarget::Normal(main_branch_tx2.id().clone()),
tx2.mut_repo().set_git_ref_target(
"refs/heads/main",
Some(RefTarget::Normal(main_branch_tx2.id().clone())),
);
tx2.commit();