Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add --create to branch set to upsert branches #3585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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