Skip to content

Commit

Permalink
cli: duplicate: parse -rREV option properly
Browse files Browse the repository at this point in the history
Since "jj duplicate" interface is now quite similar to "jj rebase", it's
annoying that "-r" isn't parsed properly.
  • Loading branch information
yuja committed Dec 3, 2024
1 parent 21a6a24 commit ae9898b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
20 changes: 12 additions & 8 deletions cli/src/commands/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ use crate::ui::Ui;
/// specified commits.
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct DuplicateArgs {
/// The revision(s) to duplicate
#[arg(default_value = "@", add = ArgValueCandidates::new(complete::all_revisions))]
revisions: Vec<RevisionArg>,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true, action = clap::ArgAction::Count)]
unused_revision: u8,
/// The revision(s) to duplicate (default: @)
#[arg(value_name = "REVISIONS", add = ArgValueCandidates::new(complete::all_revisions))]
revisions_pos: Vec<RevisionArg>,
#[arg(short = 'r', hide = true)]
revisions_opt: Vec<RevisionArg>,
/// The revision(s) to duplicate onto (can be repeated to create a merge
/// commit)
#[arg(long, short, add = ArgValueCandidates::new(complete::all_revisions))]
Expand Down Expand Up @@ -90,8 +89,13 @@ pub(crate) fn cmd_duplicate(
args: &DuplicateArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let to_duplicate: Vec<CommitId> = workspace_command
.parse_union_revsets(ui, &args.revisions)?
let to_duplicate: Vec<CommitId> =
if !args.revisions_pos.is_empty() || !args.revisions_opt.is_empty() {
workspace_command
.parse_union_revsets(ui, &[&*args.revisions_pos, &*args.revisions_opt].concat())?
} else {
workspace_command.parse_revset(ui, &RevisionArg::AT)?
}
.evaluate_to_commit_ids()?
.try_collect()?; // in reverse topological order
if to_duplicate.is_empty() {
Expand Down
4 changes: 1 addition & 3 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,7 @@ When any of the `--destination`, `--insert-after`, or `--insert-before` argument
###### **Arguments:**
* `<REVISIONS>` — The revision(s) to duplicate
Default value: `@`
* `<REVISIONS>` — The revision(s) to duplicate (default: @)
###### **Options:**
Expand Down
9 changes: 6 additions & 3 deletions cli/tests/test_duplicate_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ fn test_duplicate_destination() {
test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
// Duplicate multiple commits without a direct ancestry relationship onto a
// single destination.
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["duplicate", "a1", "b", "-d", "c"]);
let (stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["duplicate", "-r=a1", "-r=b", "-d", "c"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r"
Duplicated 9e85a474f005 as xlzxqlsl da0996fd a1
Expand All @@ -369,8 +370,10 @@ fn test_duplicate_destination() {

// Duplicate multiple commits without a direct ancestry relationship onto
// multiple destinations.
let (stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["duplicate", "a1", "b", "-d", "c", "-d", "d"]);
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["duplicate", "-r=a1", "b", "-d", "c", "-d", "d"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r"
Duplicated 9e85a474f005 as oupztwtk 2f519daa a1
Expand Down

0 comments on commit ae9898b

Please sign in to comment.