From 3fb0a3b9267aab9c26a4f2876dddd6a1f5ef78dc Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 6 Oct 2023 18:33:13 +0900 Subject: [PATCH] view: add has_branch(name), replace some of get_branch(name) callers get_branch(name) will be removed soon. --- cli/src/commands/branch.rs | 6 ++---- lib/src/repo.rs | 6 ++++++ lib/src/view.rs | 6 ++++++ lib/tests/test_git.rs | 18 +++++++++--------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index eb8118866..7153cf66f 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -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 = args.names.iter().cloned().chain(globbed_names).collect(); diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 0e64212fa..62a65a644 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -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 { self.view.with_ref(|v| v.get_branch(name).cloned()) } diff --git a/lib/src/view.rs b/lib/src/view.rs index 1fbaf3e70..580b1cb92 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -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) } diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index 162e1ed22..7d344bc17 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -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]