ok/jj
1
0
Fork 0
forked from mirrors/jj

view: add has_branch(name), replace some of get_branch(name) callers

get_branch(name) will be removed soon.
This commit is contained in:
Yuya Nishihara 2023-10-06 18:33:13 +09:00
parent d840610bad
commit 3fb0a3b926
4 changed files with 23 additions and 13 deletions

View file

@ -295,10 +295,8 @@ fn cmd_branch_forget(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let view = workspace_command.repo().view();
for branch_name in args.names.iter() {
if view.get_branch(branch_name).is_none() {
return Err(user_error(format!("No such branch: {branch_name}")));
}
if let Some(branch_name) = args.names.iter().find(|name| !view.has_branch(name)) {
return Err(user_error(format!("No such branch: {branch_name}")));
}
let globbed_names = find_globs(view, &args.glob, true)?;
let names: BTreeSet<String> = args.names.iter().cloned().chain(globbed_names).collect();

View file

@ -979,6 +979,12 @@ impl MutableRepo {
self.view.mark_dirty();
}
/// Returns true if any local or remote branch of the given `name` exists.
#[must_use]
pub fn has_branch(&self, name: &str) -> bool {
self.view.with_ref(|v| v.has_branch(name))
}
pub fn get_branch(&self, name: &str) -> Option<BranchTarget> {
self.view.with_ref(|v| v.get_branch(name).cloned())
}

View file

@ -148,6 +148,12 @@ impl View {
}
}
/// Returns true if any local or remote branch of the given `name` exists.
#[must_use]
pub fn has_branch(&self, name: &str) -> bool {
self.data.branches.contains_key(name)
}
pub fn get_branch(&self, name: &str) -> Option<&BranchTarget> {
self.data.branches.get(name)
}

View file

@ -476,7 +476,7 @@ fn test_import_refs_reimport_with_deleted_remote_ref() {
},
}),
);
view.get_branch("main").unwrap(); // branch #3 of 3
assert!(view.has_branch("main")); // branch #3 of 3
// Simulate fetching from a remote where feature-remote-only and
// feature-remote-and-local branches were deleted. This leads to the
@ -492,8 +492,8 @@ fn test_import_refs_reimport_with_deleted_remote_ref() {
let view = repo.view();
// The local branches were indeed deleted
assert_eq!(view.branches().len(), 2);
assert!(view.get_branch("main").is_some());
assert!(view.get_branch("feature-remote-only").is_none());
assert!(view.has_branch("main"));
assert!(!view.has_branch("feature-remote-only"));
assert_eq!(
view.get_branch("feature-remote-and-local"),
Some(&BranchTarget {
@ -574,7 +574,7 @@ fn test_import_refs_reimport_with_moved_remote_ref() {
},
}),
);
view.get_branch("main").unwrap(); // branch #3 of 3
assert!(view.has_branch("main")); // branch #3 of 3
// Simulate fetching from a remote where feature-remote-only and
// feature-remote-and-local branches were moved. This leads to the
@ -619,7 +619,7 @@ fn test_import_refs_reimport_with_moved_remote_ref() {
},
}),
);
view.get_branch("main").unwrap(); // branch #3 of 3
assert!(view.has_branch("main")); // branch #3 of 3
let expected_heads = hashset! {
jj_id(&commit_main),
jj_id(&new_commit_remote_and_local),
@ -844,9 +844,9 @@ fn test_import_some_refs() {
view.get_branch("feature4"),
Some(expected_feature4_branch).as_ref()
);
assert_eq!(view.get_branch("main"), None,);
assert!(!view.has_branch("main"));
assert!(!view.heads().contains(&jj_id(&commit_main)));
assert_eq!(view.get_branch("ignored"), None,);
assert!(!view.has_branch("ignored"));
assert!(!view.heads().contains(&jj_id(&commit_ign)));
// Delete branch feature1, feature3 and feature4 in git repository and import
@ -1900,7 +1900,7 @@ fn test_fetch_prune_deleted_ref() {
)
.unwrap();
// Test the setup
assert!(tx.mut_repo().get_branch("main").is_some());
assert!(tx.mut_repo().has_branch("main"));
test_data
.origin_repo
@ -1919,7 +1919,7 @@ fn test_fetch_prune_deleted_ref() {
)
.unwrap();
assert_eq!(stats.import_stats.abandoned_commits, vec![jj_id(&commit)]);
assert!(tx.mut_repo().get_branch("main").is_none());
assert!(!tx.mut_repo().has_branch("main"));
}
#[test]