Skip to content

Commit

Permalink
jj new --insert-after
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Feb 4, 2023
1 parent 8d0a69e commit b9899d7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
and `jj split any-non-existent-path` inserts an empty commit between the
target commit and its parents.

* `jj new --insert-after` inserts the new commit between the target commit and
its children.

* `jj new --insert-before` inserts the new commit between the target commit and
its parents.

Expand Down
29 changes: 29 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ struct EditArgs {
/// For more information, see
/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md.
#[derive(clap::Args, Clone, Debug)]
#[command(group(ArgGroup::new("order").args(&["insert_after", "insert_before"])))]
struct NewArgs {
/// Parent(s) of the new change
#[arg(default_value = "@")]
Expand All @@ -456,6 +457,9 @@ struct NewArgs {
/// The change description to use
#[arg(long, short, default_value = "")]
message: DescriptionArg,
/// Insert the new change between the target commit(s) and their children
#[arg(long, short = 'A', visible_alias = "after")]
insert_after: bool,
/// Insert the new change between the target commit(s) and their parents
#[arg(long, short = 'B', visible_alias = "before")]
insert_before: bool,
Expand Down Expand Up @@ -2068,6 +2072,31 @@ fn cmd_new(ui: &mut Ui, command: &CommandHelper, args: &NewArgs) -> Result<(), C
)
.set_description(&args.message)
.write()?;
if args.insert_after {
// Replace parents in children by the new commit
let old_parents = RevsetExpression::commits(parent_ids);
// Exclude children that are ancestors of the new commit
let to_rebase = old_parents.children().minus(&old_parents.ancestors());
let commits_to_rebase = tx
.base_workspace_helper()
.evaluate_revset(&to_rebase)?
.iter()
.commits(tx.base_repo().store())
.collect::<Result<Vec<_>, _>>()?;
num_rebased = commits_to_rebase.len();
for child in commits_to_rebase {
let commit_parents = RevsetExpression::commits(child.parent_ids().to_owned());
let new_parents = commit_parents.minus(&old_parents);
let mut new_parents = tx
.base_workspace_helper()
.evaluate_revset(&new_parents)?
.iter()
.commits(tx.base_repo().store())
.collect::<Result<Vec<_>, _>>()?;
new_parents.push(new_commit.clone());
rebase_commit(command.settings(), tx.mut_repo(), &child, &new_parents)?;
}
}
}
num_rebased += tx.mut_repo().rebase_descendants(command.settings())?;
if num_rebased > 0 {
Expand Down
95 changes: 91 additions & 4 deletions tests/test_new_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ fn test_new_merge() {
"###);
}

// All tests test_new_insert_* start with the same initial graph, which is
// checked once in `test_new_insert_setup()` below.
#[test]
fn test_new_insert_setup() {
// Check that the initial setup starts in the expected state.
fn test_new_insert_after() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
Expand All @@ -127,6 +124,96 @@ fn test_new_insert_setup() {
|/
o root
"###);

let stdout =
test_env.jj_cmd_success(&repo_path, &["new", "--insert-after", "-m", "G", "B", "D"]);
insta::assert_snapshot!(stdout, @r###"
Rebased 2 descendant commits
Working copy now at: ca7c6481a8dd G
"###);
insta::assert_snapshot!(get_short_log_output(&test_env, &repo_path), @r###"
o C
| o F
| |\
|/ /
@ | G
|\ \
| | o E
o | | D
| |/
|/|
| o B
| o A
|/
o root
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["new", "--insert-after", "-m", "H", "D"]);
insta::assert_snapshot!(stdout, @r###"
Rebased 3 descendant commits
Working copy now at: fcf8281b4135 H
"###);
insta::assert_snapshot!(get_short_log_output(&test_env, &repo_path), @r###"
o C
| o F
| |\
|/ /
o | G
|\ \
@ | | H
| | o E
o | | D
| |/
|/|
| o B
| o A
|/
o root
"###);
}

#[test]
fn test_new_insert_after_children() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
setup_before_insertion(&test_env, &repo_path);
insta::assert_snapshot!(get_short_log_output(&test_env, &repo_path), @r###"
@ F
|\
o | E
| o D
|/
| o C
| o B
| o A
|/
o root
"###);

// Check that inserting G after A and C doesn't try to rebase B (which is
// initially a child of A) onto G as that would create a cycle since B is
// a parent of C which is a parent G.
let stdout =
test_env.jj_cmd_success(&repo_path, &["new", "--insert-after", "-m", "G", "A", "C"]);
insta::assert_snapshot!(stdout, @r###"
Working copy now at: b48d4d73a39c G
"###);
insta::assert_snapshot!(get_short_log_output(&test_env, &repo_path), @r###"
@ G
|\
| | o F
| | |\
| | o | E
| | | o D
| | |/
o | | C
o | | B
|/ /
o | A
|/
o root
"###);
}

#[test]
Expand Down

0 comments on commit b9899d7

Please sign in to comment.