diff --git a/CHANGELOG.md b/CHANGELOG.md index aad38dc042..efde48cfcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Commit objects in templates now have a `contained_in(revset: String) -> Boolean` method. +* `jj branch set` now accepts `-c`/`--create` option to create the branch if it does not exist. + ### Fixed bugs * Revsets now support `\`-escapes in string literal. diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index 78f7fd2374..24a5d5d118 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -169,6 +169,10 @@ pub struct BranchSetArgs { #[arg(long, short = 'B')] pub allow_backwards: bool, + /// Allow creating the branch if it does not exist. + #[arg(long, short = 'c')] + pub create: bool, + /// The branches to update. #[arg(required = true)] pub names: Vec, @@ -353,13 +357,13 @@ fn cmd_branch_set( let branch_names = &args.names; for name in branch_names { let old_target = repo.view().get_local_branch(name); - if old_target.is_absent() { + if !args.create && old_target.is_absent() { return Err(user_error_with_hint( format!("No such branch: {name}"), - "Use `jj branch create` to create it.", + "Use `jj branch create` or `jj branch set --create` to create it.", )); } - if !args.allow_backwards && !is_fast_forward(old_target) { + if !args.allow_backwards && !old_target.is_absent() && !is_fast_forward(old_target) { return Err(user_error_with_hint( format!("Refusing to move branch backwards or sideways: {name}"), "Use --allow-backwards to allow it.", diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index a542c8d236..9a6d704188 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -355,6 +355,10 @@ Update an existing branch to point to a certain commit Possible values: `true`, `false` +* `-c`, `--create` — Allow creating the branch if it does not exist + + Possible values: `true`, `false` + diff --git a/cli/tests/test_branch_command.rs b/cli/tests/test_branch_command.rs index eae3312b5d..3615bacb25 100644 --- a/cli/tests/test_branch_command.rs +++ b/cli/tests/test_branch_command.rs @@ -99,7 +99,7 @@ fn test_branch_move() { let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "set", "foo"]); insta::assert_snapshot!(stderr, @r###" Error: No such branch: foo - Hint: Use `jj branch create` to create it. + Hint: Use `jj branch create` or `jj branch set --create` to create it. "###); let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "create", "foo"]); @@ -128,6 +128,22 @@ fn test_branch_move() { insta::assert_snapshot!(stderr, @""); } +#[test] +fn test_branch_set_create() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "set", "foo"]); + insta::assert_snapshot!(stderr, @r###" + Error: No such branch: foo + Hint: Use `jj branch create` or `jj branch set --create` to create it. + "###); + + let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo", "--create"]); + insta::assert_snapshot!(stderr, @""); +} + #[test] fn test_branch_move_conflicting() { let test_env = TestEnvironment::default();