Skip to content

Commit

Permalink
cli: add --second to split command
Browse files Browse the repository at this point in the history
  • Loading branch information
fowles committed Dec 13, 2024
1 parent fca92f1 commit 97bc277
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* Templates now support the `>=`, `>`, `<=`, and `<` relational operators for
`Integer` types.

* `jj split` now accepts `--second` to put specified paths into the second
commit instead of the first.

### Fixed bugs

* The `$NO_COLOR` environment variable must now be non-empty to be respected.
Expand Down
14 changes: 12 additions & 2 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::io::Write;

use clap_complete::ArgValueCandidates;
use clap_complete::ArgValueCompleter;
use jj_lib::matchers::DifferenceMatcher;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use tracing::instrument;
Expand Down Expand Up @@ -66,12 +68,16 @@ pub(crate) struct SplitArgs {
// TODO: Delete `--siblings` alias in jj 0.25+
#[arg(long, short, alias = "siblings")]
parallel: bool,
/// Put these paths in the first commit
/// Put these paths into one commit (the first by default).
#[arg(
value_hint = clap::ValueHint::AnyPath,
add = ArgValueCompleter::new(complete::modified_revision_files),
)]
paths: Vec<String>,

/// Put the specified paths into the second commit instead of the first.
#[arg(long, short, conflicts_with_all = ["parallel", "interactive", "tool"])]
second: bool,
}

#[instrument(skip_all)]
Expand All @@ -90,9 +96,13 @@ pub(crate) fn cmd_split(
}

workspace_command.check_rewritable([commit.id()])?;
let matcher = workspace_command
let mut matcher = workspace_command
.parse_file_patterns(ui, &args.paths)?
.to_matcher();
if args.second {
matcher = Box::new(DifferenceMatcher::new(EverythingMatcher, matcher));
}

let diff_selector = workspace_command.diff_selector(
ui,
args.tool.as_deref(),
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ Splitting an empty commit is not supported because the same effect can be achiev
###### **Arguments:**
* `<PATHS>` — Put these paths in the first commit
* `<PATHS>` — Put these paths into one commit (the first by default)
###### **Options:**
Expand All @@ -2068,6 +2068,7 @@ Splitting an empty commit is not supported because the same effect can be achiev
Default value: `@`
* `-p`, `--parallel` — Split the revision into two parallel revisions instead of a parent and child
* `-s`, `--second` — Put the specified paths into the second commit instead of the first
Expand Down
19 changes: 19 additions & 0 deletions cli/tests/test_split_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ fn test_split_by_paths() {
insta::assert_snapshot!(stdout, @r###"
A file2
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s", "-r", "@"]);
insta::assert_snapshot!(stdout, @r###"
A file1
A file3
"###);

let (stdout, _) = test_env.jj_cmd_ok(&repo_path, &["split", "--second", "file3"]);
insta::assert_snapshot!(stdout, @"");

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s", "-r", "@-"]);
insta::assert_snapshot!(stdout, @r###"
A file1
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s", "-r", "@"]);
insta::assert_snapshot!(stdout, @r###"
A file3
"###);
}

#[test]
Expand Down

0 comments on commit 97bc277

Please sign in to comment.