Skip to content

Commit

Permalink
cli new: Make -r, --before, and --after repeatable
Browse files Browse the repository at this point in the history
Repeating these is a no-op. This allows:

```shell
jj new -r a -r b # Equivalent to jj new a b
jj new --before a --before b  # Equivalent to jj new a b --before
```

I keep typing the latter and getting an annoying error.
  • Loading branch information
ilyagr committed Nov 21, 2023
1 parent 22ad4d7 commit 8496198
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
20 changes: 17 additions & 3 deletions cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) struct NewArgs {
#[arg(default_value = "@")]
pub(crate) revisions: Vec<RevisionArg>,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
#[arg(short = 'r', hide = true, overrides_with = "unused_revision")]
unused_revision: bool,
/// The change description to use
#[arg(long = "message", short, value_name = "MESSAGE")]
Expand All @@ -51,10 +51,24 @@ pub(crate) struct NewArgs {
#[arg(long, short = 'L', hide = true)]
allow_large_revsets: bool,
/// Insert the new change between the target commit(s) and their children
#[arg(long, short = 'A', visible_alias = "after")]
//
// Repeating this flag is allowed, but has no effect.
#[arg(
long,
short = 'A',
visible_alias = "after",
overrides_with = "insert_after"
)]
insert_after: bool,
/// Insert the new change between the target commit(s) and their parents
#[arg(long, short = 'B', visible_alias = "before")]
//
// Repeating this flag is allowed, but has no effect.
#[arg(
long,
short = 'B',
visible_alias = "before",
overrides_with = "insert_before"
)]
insert_before: bool,
}

Expand Down
30 changes: 27 additions & 3 deletions cli/tests/test_new_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,21 @@ fn test_new_insert_after() {
◉ root
"###);

let (stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["new", "--insert-after", "-m", "G", "B", "D"]);
// --insert-after can be repeated (this does not affect the outcome); --after is
// an alias
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&[
"new",
"--insert-after",
"-m",
"G",
"--after",
"B",
"--after",
"D",
],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Rebased 2 descendant commits
Expand Down Expand Up @@ -172,6 +185,16 @@ fn test_new_insert_after() {
├─╯
◉ root
"###);

// --after cannot be used with --before
let stderr = test_env.jj_cmd_cli_error(&repo_path, &["new", "--after", "B", "--before", "D"]);
insta::assert_snapshot!(stderr, @r###"
error: the argument '--insert-after' cannot be used with '--insert-before'
Usage: jj new --insert-after <REVISIONS>...
For more information, try '--help'.
"###);
}

#[test]
Expand Down Expand Up @@ -415,7 +438,8 @@ fn setup_before_insertion(test_env: &TestEnvironment, repo_path: &Path) {
test_env.jj_cmd_ok(repo_path, &["branch", "create", "D"]);
test_env.jj_cmd_ok(repo_path, &["new", "-m", "E", "root()"]);
test_env.jj_cmd_ok(repo_path, &["branch", "create", "E"]);
test_env.jj_cmd_ok(repo_path, &["new", "-m", "F", "D", "E"]);
// Any number of -r's is ignored
test_env.jj_cmd_ok(repo_path, &["new", "-m", "F", "-r", "D", "-r", "E"]);
test_env.jj_cmd_ok(repo_path, &["branch", "create", "F"]);
}

Expand Down

0 comments on commit 8496198

Please sign in to comment.