diff --git a/src/commands.rs b/src/commands.rs index 89e450e492..fc6e6af505 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1630,13 +1630,16 @@ enum BranchSubcommand { revision: Option<String>, /// The branches to create. + #[clap(required = true)] names: Vec<String>, }, - /// Delete an existing branch and propagate the deletion to remotes on the next push. + /// Delete an existing branch and propagate the deletion to remotes on the + /// next push. #[clap(visible_alias("d"))] Delete { /// The branches to delete. + #[clap(required = true)] names: Vec<String>, }, @@ -1645,6 +1648,7 @@ enum BranchSubcommand { #[clap(visible_alias("f"))] Forget { /// The branches to delete. + #[clap(required = true)] names: Vec<String>, }, @@ -1670,6 +1674,7 @@ enum BranchSubcommand { allow_backwards: bool, /// The branches to update. + #[clap(required = true)] names: Vec<String>, }, } @@ -4037,39 +4042,31 @@ fn cmd_branch( ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; let view = workspace_command.repo().view(); - fn assert_branch_names_exist<'a>( + fn validate_branch_names_exist<'a>( view: &'a View, names: &'a [String], - ) -> Result<Vec<&'a str>, CommandError> { - let result: Vec<_> = names - .iter() - .map(|branch_name| match view.get_local_branch(branch_name) { - Some(_) => Ok(branch_name.as_str()), - None => Err(CommandError::UserError(format!( + ) -> Result<(), CommandError> { + for branch_name in names { + if view.get_local_branch(branch_name).is_none() { + return Err(CommandError::UserError(format!( "No such branch: {}", branch_name - ))), - }) - .try_collect()?; - Ok(result) + ))); + } + } + Ok(()) } - fn make_branch_term(ui: &mut Ui, branch_names: &[impl AsRef<str>]) -> io::Result<String> { - if branch_names.is_empty() { - ui.write_warn("warning: No branches provided.\n")?; + fn make_branch_term(branch_names: &[impl AsRef<str>]) -> String { + match branch_names { + [branch_name] => format!("branch {}", branch_name.as_ref()), + branch_names => { + format!( + "branches {}", + branch_names.iter().map(AsRef::as_ref).join(", ") + ) + } } - let branch_term = if branch_names.len() == 1 { - "branch" - } else { - "branches" - }; - Ok(format!( - "{branch_term} {}", - branch_names - .iter() - .map(|branch_name| branch_name.as_ref()) - .join(", ") - )) } match subcommand { @@ -4096,7 +4093,7 @@ fn cmd_branch( workspace_command.resolve_single_rev(ui, revision.as_deref().unwrap_or("@"))?; let mut tx = workspace_command.start_transaction(&format!( "create {} pointing to commit {}", - make_branch_term(ui, &branch_names)?, + make_branch_term(&branch_names), target_commit.id().hex() )); for branch_name in branch_names { @@ -4138,7 +4135,7 @@ fn cmd_branch( } let mut tx = workspace_command.start_transaction(&format!( "point {} to commit {}", - make_branch_term(ui, branch_names)?, + make_branch_term(branch_names), target_commit.id().hex() )); for branch_name in branch_names { @@ -4151,20 +4148,20 @@ fn cmd_branch( } BranchSubcommand::Delete { names } => { - let branch_names = assert_branch_names_exist(view, names)?; - let mut tx = workspace_command - .start_transaction(&format!("delete {}", make_branch_term(ui, &branch_names)?,)); - for branch_name in branch_names { + validate_branch_names_exist(view, names)?; + let mut tx = + workspace_command.start_transaction(&format!("delete {}", make_branch_term(names))); + for branch_name in names { tx.mut_repo().remove_local_branch(branch_name); } workspace_command.finish_transaction(ui, tx)?; } BranchSubcommand::Forget { names } => { - let branch_names = assert_branch_names_exist(view, names)?; - let mut tx = workspace_command - .start_transaction(&format!("forget {}", make_branch_term(ui, &branch_names)?)); - for branch_name in branch_names { + validate_branch_names_exist(view, names)?; + let mut tx = + workspace_command.start_transaction(&format!("forget {}", make_branch_term(names))); + for branch_name in names { tx.mut_repo().remove_branch(branch_name); } workspace_command.finish_transaction(ui, tx)?; diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 97adde32d6..8da98ccbab 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -55,21 +55,6 @@ fn test_branch_multiple_names() { "###); } -#[test] -fn test_branch_hint_no_branches() { - let test_env = TestEnvironment::default(); - test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); - let repo_path = test_env.env_root().join("repo"); - - let assert = test_env - .jj_cmd(&repo_path, &["branch", "delete"]) - .assert() - .success(); - let stderr = get_stderr_string(&assert); - insta::assert_snapshot!(stderr, @"warning: No branches provided. -"); -} - fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String { test_env.jj_cmd_success(cwd, &["log", "-T", r#"branches " " commit_id.short()"#]) }