diff --git a/src/commands/branch.rs b/src/commands/branch.rs index 48ba21da8..ae073fa7e 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -232,17 +232,36 @@ fn cmd_branch_forget( args: &BranchForgetArgs, ) -> Result<(), CommandError> { fn find_globs(view: &View, globs: &[String]) -> Result, CommandError> { - let globs: Vec = globs - .iter() - .map(|glob| glob::Pattern::new(glob)) - .try_collect()?; - let matching_branches = view - .branches() - .iter() - .map(|(branch_name, _branch_target)| branch_name) - .filter(|branch_name| globs.iter().any(|glob| glob.matches(branch_name))) - .cloned() - .collect(); + let mut matching_branches: Vec = vec![]; + let mut failed_globs = vec![]; + for glob_str in globs { + let glob = glob::Pattern::new(glob_str)?; + let names = view + .branches() + .iter() + .map(|(branch_name, _branch_target)| branch_name) + .filter(|branch_name| glob.matches(branch_name)) + .cloned() + .collect_vec(); + if names.is_empty() { + failed_globs.push(glob); + } + matching_branches.extend(names.into_iter()); + } + match &failed_globs[..] { + [] => { /* No problem */ } + [glob] => { + return Err(user_error(format!( + "The provided glob '{glob}' did not match any branches" + ))) + } + globs => { + return Err(user_error(format!( + "The provided globs '{}' did not match any branches", + globs.iter().join("', '") + ))) + } + }; Ok(matching_branches) } diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 28a2ed5ca..f27a33bcb 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -112,6 +112,21 @@ fn test_branch_forget_glob() { insta::assert_snapshot!(stderr, @r###" Error: Failed to compile glob: Pattern syntax error near position 4: invalid range pattern "###); + + // We get an error if none of the globs match anything + let stderr = test_env.jj_cmd_failure( + &repo_path, + &[ + "branch", + "forget", + "--glob=bar*", + "--glob=baz*", + "--glob=boom*", + ], + ); + insta::assert_snapshot!(stderr, @r###" + Error: The provided globs 'baz*', 'boom*' did not match any branches + "###); } #[test]