Skip to content

Commit

Permalink
cli: add --create to branch set to upsert branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Cretezy committed Apr 29, 2024
1 parent e54e83b commit 0c0fddd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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.",
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
18 changes: 17 additions & 1 deletion cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0c0fddd

Please sign in to comment.