Skip to content

Commit

Permalink
cli: branch: add "set --allow-new" flag
Browse files Browse the repository at this point in the history
Since "set <thing>" often adds a <thing> as needed, it make some sense that
"branch set" does upsert. The added flag helps re-execute a failed "set"
command to create new branch.

I'm hoping to make it the default by migrating existing "branch set" users to
"branch move", but changing the default behavior right now can cause problems.
So, let's add an opt-in flag first.

Closes #3584
  • Loading branch information
yuja committed Jun 20, 2024
1 parent 5988a00 commit bf20766
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* New command `jj branch move` let you update branches by name pattern or source
revision.

* `jj branch set` now accepts `--allow-new` option to create new branch if it
doesn't exist.
[#3584](https://github.com/martinvonz/jj/issues/3584)

* New diff option `jj diff --name-only` allows for easier shell scripting.

### Fixed bugs
Expand Down
12 changes: 8 additions & 4 deletions cli/src/commands/branch/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::{user_error_with_hint, CommandError};
use crate::ui::Ui;

/// Update an existing branch to point to a certain commit
/// Update a branch to point to a certain commit
#[derive(clap::Args, Clone, Debug)]
pub struct BranchSetArgs {
/// The branch's target revision
Expand All @@ -32,6 +32,10 @@ pub struct BranchSetArgs {
#[arg(long, short = 'B')]
allow_backwards: bool,

/// Allow creating new branch
#[arg(long, short = 'N')]
allow_new: bool,

/// The branches to update
#[arg(required = true, value_parser = NonEmptyStringValueParser::new())]
names: Vec<String>,
Expand All @@ -49,10 +53,10 @@ pub 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.allow_new && old_target.is_absent() {
return Err(user_error_with_hint(
format!("No such branch: {name}"),
"Use `jj branch create` to create it.",
format!("Refusing to create new branch: {name}"),
"Use --allow-new to create it.",
));
}
if !args.allow_backwards && !is_fast_forward(repo, old_target, target_commit.id()) {
Expand Down
5 changes: 3 additions & 2 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ For information about branches, see https://github.com/martinvonz/jj/blob/main/d
* `list` — List branches and their targets
* `move` — Move existing branches to target revision
* `rename` — Rename `old` branch name to `new` branch name
* `set` — Update an existing branch to point to a certain commit
* `set` — Update a branch to point to a certain commit
* `track` — Start tracking given remote branches
* `untrack` — Stop tracking given remote branches
Expand Down Expand Up @@ -370,7 +370,7 @@ The new branch name points at the same commit as the old branch name.
## `jj branch set`
Update an existing branch to point to a certain commit
Update a branch to point to a certain commit
**Usage:** `jj branch set [OPTIONS] <NAMES>...`
Expand All @@ -382,6 +382,7 @@ Update an existing branch to point to a certain commit
* `-r`, `--revision <REVISION>` — The branch's target revision
* `-B`, `--allow-backwards` — Allow moving the branch backwards or sideways
* `-N`, `--allow-new` — Allow creating new branch
Expand Down
13 changes: 10 additions & 3 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ 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.
Error: Refusing to create new branch: foo
Hint: Use --allow-new to create it.
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "move", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: No such branch: foo
"###);

let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "create", "foo"]);
// "set --allow-new" should work no matter if branch exists
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "set", "--allow-new", "foo"]);
insta::assert_snapshot!(stderr, @"");
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "set", "--allow-new", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Nothing changed.
"###);

test_env.jj_cmd_ok(&repo_path, &["new"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "create", "foo"]);
Expand Down

0 comments on commit bf20766

Please sign in to comment.