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

If we migrate RefTarget to new Conflict-based type, it won't store
Conflict<CommitId>, but Conflict<Option<CommitId>>. As the Option will
be internalized, new RefTarget type will also represent an absent target.
The 'target: Option<RefTarget>' argument will be replaced with new RefTarget
type.

I've also renamed the function for consistency with the following changes.
It would be surprising if set_local_branch(name, target) could remove the
branch. I feel the name set_local_branch_target() is less confusing.
This commit is contained in:
Yuya Nishihara 2023-07-11 22:25:27 +09:00
parent 6d7998f8c5
commit 5218591a82
6 changed files with 17 additions and 26 deletions

View file

@ -203,10 +203,10 @@ pub fn import_some_refs(
let head_commit = store.get_commit(&head_commit_id).unwrap();
prevent_gc(git_repo, &head_commit_id)?;
mut_repo.add_head(&head_commit);
mut_repo.set_git_head(RefTarget::Normal(head_commit_id));
mut_repo.set_git_head_target(Some(RefTarget::Normal(head_commit_id)));
}
} else {
mut_repo.clear_git_head();
mut_repo.set_git_head_target(None);
}
let mut changed_git_refs = BTreeMap::new();

View file

@ -1023,12 +1023,8 @@ impl MutableRepo {
self.view.with_ref(|v| v.git_head().cloned())
}
pub fn set_git_head(&mut self, target: RefTarget) {
self.view_mut().set_git_head(target);
}
pub fn clear_git_head(&mut self) {
self.view_mut().clear_git_head();
pub fn set_git_head_target(&mut self, target: Option<RefTarget>) {
self.view_mut().set_git_head_target(target);
}
pub fn set_view(&mut self, data: op_store::View) {
@ -1152,16 +1148,13 @@ impl MutableRepo {
);
}
if let Some(new_git_head) = merge_ref_targets(
let new_git_head_target = merge_ref_targets(
self.index(),
self.view().git_head(),
base.git_head(),
other.git_head(),
) {
self.set_git_head(new_git_head);
} else {
self.clear_git_head();
}
);
self.set_git_head_target(new_git_head_target);
}
/// Finds and records commits that were rewritten or abandoned between

View file

@ -258,12 +258,10 @@ impl View {
self.data.git_refs.remove(name);
}
pub fn set_git_head(&mut self, target: RefTarget) {
self.data.git_head = Some(target);
}
pub fn clear_git_head(&mut self) {
self.data.git_head = None;
/// Sets `HEAD@git` to point to the given target. If the target is absent,
/// the reference will be cleared.
pub fn set_git_head_target(&mut self, target: Option<RefTarget>) {
self.data.git_head = target;
}
pub fn set_view(&mut self, data: op_store::View) {

View file

@ -649,7 +649,7 @@ fn test_resolve_symbol_git_head() {
"###);
// With HEAD@git
mut_repo.set_git_head(RefTarget::Normal(commit1.id().clone()));
mut_repo.set_git_head_target(Some(RefTarget::Normal(commit1.id().clone())));
insta::assert_debug_snapshot!(
resolve_symbol(mut_repo, "HEAD", None).unwrap_err(), @r###"
NoSuchRevision {
@ -1677,7 +1677,7 @@ fn test_evaluate_expression_git_head(use_git: bool) {
// Can get git head when it's not set
assert_eq!(resolve_commit_ids(mut_repo, "git_head()"), vec![]);
mut_repo.set_git_head(RefTarget::Normal(commit1.id().clone()));
mut_repo.set_git_head_target(Some(RefTarget::Normal(commit1.id().clone())));
assert_eq!(
resolve_commit_ids(mut_repo, "git_head()"),
vec![commit1.id().clone()]

View file

@ -442,19 +442,19 @@ fn test_merge_views_git_heads() {
let mut tx0 = repo.start_transaction(&settings, "test");
let tx0_head = write_random_commit(tx0.mut_repo(), &settings);
tx0.mut_repo()
.set_git_head(RefTarget::Normal(tx0_head.id().clone()));
.set_git_head_target(Some(RefTarget::Normal(tx0_head.id().clone())));
let repo = tx0.commit();
let mut tx1 = repo.start_transaction(&settings, "test");
let tx1_head = write_random_commit(tx1.mut_repo(), &settings);
tx1.mut_repo()
.set_git_head(RefTarget::Normal(tx1_head.id().clone()));
.set_git_head_target(Some(RefTarget::Normal(tx1_head.id().clone())));
tx1.commit();
let mut tx2 = repo.start_transaction(&settings, "test");
let tx2_head = write_random_commit(tx2.mut_repo(), &settings);
tx2.mut_repo()
.set_git_head(RefTarget::Normal(tx2_head.id().clone()));
.set_git_head_target(Some(RefTarget::Normal(tx2_head.id().clone())));
tx2.commit();
let repo = repo.reload_at_head(&settings).unwrap();

View file

@ -796,7 +796,7 @@ impl WorkspaceCommandHelper {
let new_git_commit_id = Oid::from_bytes(first_parent_id.as_bytes()).unwrap();
let new_git_commit = git_repo.find_commit(new_git_commit_id)?;
git_repo.reset(new_git_commit.as_object(), git2::ResetType::Mixed, None)?;
mut_repo.set_git_head(RefTarget::Normal(first_parent_id));
mut_repo.set_git_head_target(Some(RefTarget::Normal(first_parent_id)));
}
} else {
// The workspace was removed (maybe the user undid the