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

This commit is contained in:
Yuya Nishihara 2023-07-11 23:29:45 +09:00
parent 78eea06c36
commit d80a719042
9 changed files with 105 additions and 123 deletions

View file

@ -970,12 +970,8 @@ impl MutableRepo {
self.view.with_ref(|v| v.get_local_branch(name))
}
pub fn set_local_branch(&mut self, name: String, target: RefTarget) {
self.view_mut().set_local_branch(name, target);
}
pub fn remove_local_branch(&mut self, name: &str) {
self.view_mut().remove_local_branch(name);
pub fn set_local_branch_target(&mut self, name: &str, target: Option<RefTarget>) {
self.view_mut().set_local_branch_target(name, target);
}
pub fn get_remote_branch(&self, name: &str, remote_name: &str) -> Option<RefTarget> {

View file

@ -139,7 +139,7 @@ impl View {
if let Some(target) = target {
match name {
RefName::LocalBranch(name) => {
self.set_local_branch(name, target);
self.set_local_branch_target(&name, Some(target));
}
RefName::RemoteBranch { branch, remote } => {
self.set_remote_branch_target(&branch, &remote, Some(target));
@ -154,7 +154,7 @@ impl View {
} else {
match name {
RefName::LocalBranch(name) => {
self.remove_local_branch(&name);
self.set_local_branch_target(&name, None);
}
RefName::RemoteBranch { branch, remote } => {
self.set_remote_branch_target(&branch, &remote, None);
@ -188,11 +188,21 @@ impl View {
.and_then(|branch_target| branch_target.local_target.clone())
}
pub fn set_local_branch(&mut self, name: String, target: RefTarget) {
/// Sets local branch to point to the given target. If the target is absent,
/// and if no associated remote branches exist, the branch will be removed.
pub fn set_local_branch_target(&mut self, name: &str, target: Option<RefTarget>) {
if let Some(target) = target {
self.insert_local_branch(name.to_owned(), target);
} else {
self.remove_local_branch(name);
}
}
fn insert_local_branch(&mut self, name: String, target: RefTarget) {
self.data.branches.entry(name).or_default().local_target = Some(target);
}
pub fn remove_local_branch(&mut self, name: &str) {
fn remove_local_branch(&mut self, name: &str) {
if let Some(branch) = self.data.branches.get_mut(name) {
branch.local_target = None;
if branch.remote_targets.is_empty() {

View file

@ -219,10 +219,8 @@ fn test_import_refs_reimport() {
.set_parents(vec![jj_id(&commit2)])
.write()
.unwrap();
tx.mut_repo().set_local_branch(
"feature2".to_string(),
RefTarget::Normal(commit6.id().clone()),
);
tx.mut_repo()
.set_local_branch_target("feature2", Some(RefTarget::Normal(commit6.id().clone())));
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
@ -988,10 +986,7 @@ fn test_export_refs_branch_changed() {
.set_parents(vec![jj_id(&commit)])
.write()
.unwrap();
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(new_commit.id().clone())));
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
@ -1030,10 +1025,7 @@ fn test_export_refs_current_branch_changed() {
.set_parents(vec![jj_id(&commit1)])
.write()
.unwrap();
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(new_commit.id().clone())));
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
@ -1067,10 +1059,7 @@ fn test_export_refs_unborn_git_branch() {
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
let new_commit = write_random_commit(mut_repo, &test_data.settings);
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(new_commit.id().clone())));
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
@ -1118,7 +1107,7 @@ fn test_export_import_sequence() {
);
// Modify the branch in jj to point to B
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
// Export the branch to git
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
@ -1194,22 +1183,19 @@ fn test_export_conflicts() {
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let commit_b = write_random_commit(mut_repo, &test_data.settings);
let commit_c = write_random_commit(mut_repo, &test_data.settings);
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_a.id().clone()));
mut_repo.set_local_branch(
"feature".to_string(),
RefTarget::Normal(commit_a.id().clone()),
);
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit_a.id().clone())));
mut_repo.set_local_branch_target("feature", Some(RefTarget::Normal(commit_a.id().clone())));
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
// Create a conflict and export. It should not be exported, but other changes
// should be.
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
mut_repo.set_local_branch(
"feature".to_string(),
RefTarget::Conflict {
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
mut_repo.set_local_branch_target(
"feature",
Some(RefTarget::Conflict {
removes: vec![commit_a.id().clone()],
adds: vec![commit_b.id().clone(), commit_c.id().clone()],
},
}),
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
@ -1240,15 +1226,15 @@ fn test_export_partial_failure() {
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let target = RefTarget::Normal(commit_a.id().clone());
let target = Some(RefTarget::Normal(commit_a.id().clone()));
// Empty string is disallowed by Git
mut_repo.set_local_branch("".to_string(), target.clone());
mut_repo.set_local_branch_target("", target.clone());
// Branch named HEAD is disallowed by Git CLI
mut_repo.set_local_branch("HEAD".to_string(), target.clone());
mut_repo.set_local_branch("main".to_string(), target.clone());
mut_repo.set_local_branch_target("HEAD", target.clone());
mut_repo.set_local_branch_target("main", target.clone());
// `main/sub` will conflict with `main` in Git, at least when using loose ref
// storage
mut_repo.set_local_branch("main/sub".to_string(), target);
mut_repo.set_local_branch_target("main/sub", target);
assert_eq!(
git::export_refs(mut_repo, &git_repo),
Ok(vec![
@ -1273,7 +1259,7 @@ fn test_export_partial_failure() {
// Now remove the `main` branch and make sure that the `main/sub` gets exported
// even though it didn't change
mut_repo.remove_local_branch("main");
mut_repo.set_local_branch_target("main", None);
assert_eq!(
git::export_refs(mut_repo, &git_repo),
Ok(vec![
@ -1330,19 +1316,19 @@ fn test_export_reexport_transitions() {
for branch in [
"AAB", "AAX", "ABA", "ABB", "ABC", "ABX", "AXA", "AXB", "AXX",
] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_a.id().clone()));
mut_repo.set_local_branch_target(branch, Some(RefTarget::Normal(commit_a.id().clone())));
}
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
// Make changes on the jj side
for branch in ["AXA", "AXB", "AXX"] {
mut_repo.remove_local_branch(branch);
mut_repo.set_local_branch_target(branch, None);
}
for branch in ["XAA", "XAB", "XAX"] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_a.id().clone()));
mut_repo.set_local_branch_target(branch, Some(RefTarget::Normal(commit_a.id().clone())));
}
for branch in ["ABA", "ABB", "ABC", "ABX"] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_b.id().clone()));
mut_repo.set_local_branch_target(branch, Some(RefTarget::Normal(commit_b.id().clone())));
}
// Make changes on the git side

View file

@ -170,10 +170,7 @@ fn test_checkout_previous_empty_with_local_branch(use_git: bool) {
)
.write()
.unwrap();
mut_repo.set_local_branch(
"b".to_owned(),
RefTarget::Normal(old_wc_commit.id().clone()),
);
mut_repo.set_local_branch_target("b", Some(RefTarget::Normal(old_wc_commit.id().clone())));
let ws_id = WorkspaceId::default();
mut_repo.edit(ws_id.clone(), &old_wc_commit).unwrap();
let repo = tx.commit();
@ -472,7 +469,7 @@ fn test_has_changed(use_git: bool) {
mut_repo
.set_wc_commit(ws_id.clone(), commit1.id().clone())
.unwrap();
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit1.id().clone()));
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit1.id().clone())));
mut_repo.set_remote_branch_target(
"main",
"origin",
@ -491,7 +488,7 @@ fn test_has_changed(use_git: bool) {
mut_repo
.set_wc_commit(ws_id.clone(), commit1.id().clone())
.unwrap();
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit1.id().clone()));
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit1.id().clone())));
mut_repo.set_remote_branch_target(
"main",
"origin",
@ -501,7 +498,7 @@ fn test_has_changed(use_git: bool) {
mut_repo.remove_public_head(commit2.id());
mut_repo.remove_head(commit2.id());
mut_repo.remove_local_branch("stable");
mut_repo.set_local_branch_target("stable", None);
mut_repo.set_remote_branch_target("stable", "origin", None);
assert!(!mut_repo.has_changes());
@ -527,9 +524,9 @@ fn test_has_changed(use_git: bool) {
mut_repo.set_wc_commit(ws_id, commit1.id().clone()).unwrap();
assert!(!mut_repo.has_changes());
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit2.id().clone()));
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit2.id().clone())));
assert!(mut_repo.has_changes());
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit1.id().clone()));
mut_repo.set_local_branch_target("main", Some(RefTarget::Normal(commit1.id().clone())));
assert!(!mut_repo.has_changes());
mut_repo.set_remote_branch_target(

View file

@ -425,15 +425,15 @@ fn test_resolve_symbol_branches() {
let commit4 = write_random_commit(mut_repo, &settings);
let commit5 = write_random_commit(mut_repo, &settings);
mut_repo.set_local_branch("local".to_owned(), RefTarget::Normal(commit1.id().clone()));
mut_repo.set_local_branch_target("local", Some(RefTarget::Normal(commit1.id().clone())));
mut_repo.set_remote_branch_target(
"remote",
"origin",
Some(RefTarget::Normal(commit2.id().clone())),
);
mut_repo.set_local_branch(
"local-remote".to_owned(),
RefTarget::Normal(commit3.id().clone()),
mut_repo.set_local_branch_target(
"local-remote",
Some(RefTarget::Normal(commit3.id().clone())),
);
mut_repo.set_remote_branch_target(
"local-remote",
@ -450,12 +450,12 @@ fn test_resolve_symbol_branches() {
mut_repo.get_local_branch("local-remote"),
);
mut_repo.set_local_branch(
"local-conflicted".to_owned(),
RefTarget::Conflict {
mut_repo.set_local_branch_target(
"local-conflicted",
Some(RefTarget::Conflict {
removes: vec![commit1.id().clone()],
adds: vec![commit3.id().clone(), commit2.id().clone()],
},
}),
);
mut_repo.set_remote_branch_target(
"remote-conflicted",
@ -1702,14 +1702,8 @@ fn test_evaluate_expression_branches(use_git: bool) {
// Can get branches when there are none
assert_eq!(resolve_commit_ids(mut_repo, "branches()"), vec![]);
// Can get a few branches
mut_repo.set_local_branch(
"branch1".to_string(),
RefTarget::Normal(commit1.id().clone()),
);
mut_repo.set_local_branch(
"branch2".to_string(),
RefTarget::Normal(commit2.id().clone()),
);
mut_repo.set_local_branch_target("branch1", Some(RefTarget::Normal(commit1.id().clone())));
mut_repo.set_local_branch_target("branch2", Some(RefTarget::Normal(commit2.id().clone())));
assert_eq!(
resolve_commit_ids(mut_repo, "branches()"),
vec![commit2.id().clone(), commit1.id().clone()]
@ -1727,30 +1721,27 @@ fn test_evaluate_expression_branches(use_git: bool) {
assert_eq!(resolve_commit_ids(mut_repo, "branches(branch3)"), vec![]);
// Two branches pointing to the same commit does not result in a duplicate in
// the revset
mut_repo.set_local_branch(
"branch3".to_string(),
RefTarget::Normal(commit2.id().clone()),
);
mut_repo.set_local_branch_target("branch3", Some(RefTarget::Normal(commit2.id().clone())));
assert_eq!(
resolve_commit_ids(mut_repo, "branches()"),
vec![commit2.id().clone(), commit1.id().clone()]
);
// Can get branches when there are conflicted refs
mut_repo.set_local_branch(
"branch1".to_string(),
RefTarget::Conflict {
mut_repo.set_local_branch_target(
"branch1",
Some(RefTarget::Conflict {
removes: vec![commit1.id().clone()],
adds: vec![commit2.id().clone(), commit3.id().clone()],
},
}),
);
mut_repo.set_local_branch(
"branch2".to_string(),
RefTarget::Conflict {
mut_repo.set_local_branch_target(
"branch2",
Some(RefTarget::Conflict {
removes: vec![commit2.id().clone()],
adds: vec![commit3.id().clone(), commit4.id().clone()],
},
}),
);
mut_repo.remove_local_branch("branch3");
mut_repo.set_local_branch_target("branch3", None);
assert_eq!(
resolve_commit_ids(mut_repo, "branches()"),
vec![

View file

@ -935,7 +935,7 @@ fn test_rebase_descendants_basic_branch_update() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo()
.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
@ -978,7 +978,7 @@ fn test_rebase_descendants_branch_move_two_steps() {
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
let commit_c = graph_builder.commit_with_parents(&[&commit_b]);
tx.mut_repo()
.set_local_branch("main".to_string(), RefTarget::Normal(commit_c.id().clone()));
.set_local_branch_target("main", Some(RefTarget::Normal(commit_c.id().clone())));
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
@ -1024,7 +1024,7 @@ fn test_rebase_descendants_basic_branch_update_with_non_local_branch() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo()
.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
tx.mut_repo().set_remote_branch_target(
"main",
"origin",
@ -1080,7 +1080,7 @@ fn test_rebase_descendants_update_branch_after_abandon() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo()
.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
@ -1116,7 +1116,7 @@ fn test_rebase_descendants_update_branches_after_divergent_rewrite() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo()
.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
.set_local_branch_target("main", Some(RefTarget::Normal(commit_b.id().clone())));
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
@ -1180,12 +1180,12 @@ fn test_rebase_descendants_rewrite_updates_branch_conflict() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.initial_commit();
let commit_c = graph_builder.initial_commit();
tx.mut_repo().set_local_branch(
"main".to_string(),
RefTarget::Conflict {
tx.mut_repo().set_local_branch_target(
"main",
Some(RefTarget::Conflict {
removes: vec![commit_a.id().clone()],
adds: vec![commit_b.id().clone(), commit_c.id().clone()],
},
}),
);
let repo = tx.commit();
@ -1261,12 +1261,12 @@ fn test_rebase_descendants_rewrite_resolves_branch_conflict() {
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
let commit_c = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo().set_local_branch(
"main".to_string(),
RefTarget::Conflict {
tx.mut_repo().set_local_branch_target(
"main",
Some(RefTarget::Conflict {
removes: vec![commit_a.id().clone()],
adds: vec![commit_b.id().clone(), commit_c.id().clone()],
},
}),
);
let repo = tx.commit();
@ -1304,12 +1304,12 @@ fn test_rebase_descendants_branch_delete_modify_abandon() {
let mut graph_builder = CommitGraphBuilder::new(&settings, tx.mut_repo());
let commit_a = graph_builder.initial_commit();
let commit_b = graph_builder.commit_with_parents(&[&commit_a]);
tx.mut_repo().set_local_branch(
"main".to_string(),
RefTarget::Conflict {
tx.mut_repo().set_local_branch_target(
"main",
Some(RefTarget::Conflict {
removes: vec![commit_a.id().clone()],
adds: vec![commit_b.id().clone()],
},
}),
);
let repo = tx.commit();

View file

@ -248,9 +248,9 @@ fn test_merge_views_branches() {
let main_branch_origin_tx0 = write_random_commit(mut_repo, &settings);
let main_branch_origin_tx1 = write_random_commit(mut_repo, &settings);
let main_branch_alternate_tx0 = write_random_commit(mut_repo, &settings);
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(main_branch_local_tx0.id().clone()),
mut_repo.set_local_branch_target(
"main",
Some(RefTarget::Normal(main_branch_local_tx0.id().clone())),
);
mut_repo.set_remote_branch_target(
"main",
@ -271,9 +271,9 @@ fn test_merge_views_branches() {
let mut tx1 = repo.start_transaction(&settings, "test");
let main_branch_local_tx1 = write_random_commit(tx1.mut_repo(), &settings);
tx1.mut_repo().set_local_branch(
"main".to_string(),
RefTarget::Normal(main_branch_local_tx1.id().clone()),
tx1.mut_repo().set_local_branch_target(
"main",
Some(RefTarget::Normal(main_branch_local_tx1.id().clone())),
);
tx1.mut_repo().set_remote_branch_target(
"main",
@ -281,17 +281,17 @@ fn test_merge_views_branches() {
Some(RefTarget::Normal(main_branch_origin_tx1.id().clone())),
);
let feature_branch_tx1 = write_random_commit(tx1.mut_repo(), &settings);
tx1.mut_repo().set_local_branch(
"feature".to_string(),
RefTarget::Normal(feature_branch_tx1.id().clone()),
tx1.mut_repo().set_local_branch_target(
"feature",
Some(RefTarget::Normal(feature_branch_tx1.id().clone())),
);
tx1.commit();
let mut tx2 = repo.start_transaction(&settings, "test");
let main_branch_local_tx2 = write_random_commit(tx2.mut_repo(), &settings);
tx2.mut_repo().set_local_branch(
"main".to_string(),
RefTarget::Normal(main_branch_local_tx2.id().clone()),
tx2.mut_repo().set_local_branch_target(
"main",
Some(RefTarget::Normal(main_branch_local_tx2.id().clone())),
);
tx2.mut_repo().set_remote_branch_target(
"main",

View file

@ -156,9 +156,9 @@ fn cmd_branch_create(
target_commit.id().hex()
));
for branch_name in branch_names {
tx.mut_repo().set_local_branch(
branch_name.to_string(),
RefTarget::Normal(target_commit.id().clone()),
tx.mut_repo().set_local_branch_target(
branch_name,
Some(RefTarget::Normal(target_commit.id().clone())),
);
}
tx.finish(ui)?;
@ -203,9 +203,9 @@ fn cmd_branch_set(
target_commit.id().hex()
));
for branch_name in branch_names {
tx.mut_repo().set_local_branch(
branch_name.to_string(),
RefTarget::Normal(target_commit.id().clone()),
tx.mut_repo().set_local_branch_target(
branch_name,
Some(RefTarget::Normal(target_commit.id().clone())),
);
}
tx.finish(ui)?;
@ -280,7 +280,7 @@ fn cmd_branch_delete(
let branch_term = make_branch_term(names.iter().collect_vec().as_slice());
let mut tx = workspace_command.start_transaction(&format!("delete {branch_term}"));
for branch_name in names.iter() {
tx.mut_repo().remove_local_branch(branch_name);
tx.mut_repo().set_local_branch_target(branch_name, None);
}
tx.finish(ui)?;
if names.len() > 1 {

View file

@ -717,8 +717,10 @@ fn cmd_git_push(
change_str.deref()
)?;
}
tx.mut_repo()
.set_local_branch(branch_name.clone(), RefTarget::Normal(commit.id().clone()));
tx.mut_repo().set_local_branch_target(
&branch_name,
Some(RefTarget::Normal(commit.id().clone())),
);
let branch_target = tx.repo().view().get_branch(&branch_name).unwrap();
match classify_branch_update(&branch_name, branch_target, &remote) {
Ok(Some(update)) => branch_updates.push((branch_name.clone(), update)),