From 26380e14418ad231003ce7ee466d3823a491dfcf Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Tue, 30 Apr 2024 20:56:53 +0800 Subject: [PATCH] rebase -s: add support for `--insert-after` and `--insert-before` options --- CHANGELOG.md | 3 + cli/src/commands/rebase.rs | 84 ++++++++----- cli/tests/cli-reference@.md.snap | 4 +- cli/tests/test_rebase_command.rs | 196 +++++++++++++++++++++++++++---- 4 files changed, 234 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 347216fa85..4ec520b0de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). * A tilde (`~`) at the start of the path will now be expanded to the user's home directory when configuring a `signing.key` for SSH commit signing. +* `jj rebase -s` can now be used with the `--insert-after` and `--insert-before` + options, like `jj rebase -r`. + ### Fixed bugs * Release binaries for Intel Macs have been restored. They were previously diff --git a/cli/src/commands/rebase.rs b/cli/src/commands/rebase.rs index 451f50ab46..72a9ea6466 100644 --- a/cli/src/commands/rebase.rs +++ b/cli/src/commands/rebase.rs @@ -158,26 +158,24 @@ pub(crate) struct RebaseArgs { /// The revision(s) to insert after (can be repeated to create a merge /// commit) /// - /// Only works with `-r`. + /// Only works with `-r` and `-s`. #[arg( long, short = 'A', visible_alias = "after", conflicts_with = "destination", - conflicts_with = "source", conflicts_with = "branch" )] insert_after: Vec, /// The revision(s) to insert before (can be repeated to create a merge /// commit) /// - /// Only works with `-r`. + /// Only works with `-r` and `-s`. #[arg( long, short = 'B', visible_alias = "before", conflicts_with = "destination", - conflicts_with = "source", conflicts_with = "branch" )] insert_before: Vec, @@ -242,17 +240,14 @@ pub(crate) fn cmd_rebase( &rebase_options, )?; } else if !args.source.is_empty() { - let new_parents = workspace_command - .resolve_some_revsets_default_single(&args.destination)? - .into_iter() - .collect_vec(); - let source_commits = workspace_command.resolve_some_revsets_default_single(&args.source)?; - rebase_descendants_transaction( + rebase_source( ui, command.settings(), &mut workspace_command, - new_parents, - &source_commits, + &args.source, + &args.destination, + &args.insert_after, + &args.insert_before, &rebase_options, )?; } else { @@ -318,6 +313,42 @@ fn rebase_revisions( ) } +#[allow(clippy::too_many_arguments)] +fn rebase_source( + ui: &mut Ui, + settings: &UserSettings, + workspace_command: &mut WorkspaceCommandHelper, + source: &[RevisionArg], + destination: &[RevisionArg], + insert_after: &[RevisionArg], + insert_before: &[RevisionArg], + rebase_options: &RebaseOptions, +) -> Result<(), CommandError> { + let source_commits = workspace_command + .resolve_some_revsets_default_single(source)? + .into_iter() + .collect_vec(); + workspace_command.check_rewritable(source_commits.iter().ids())?; + + let (new_parents, new_children) = + compute_rebase_destination(workspace_command, destination, insert_after, insert_before)?; + if !destination.is_empty() && new_children.is_empty() { + for commit in source_commits.iter() { + check_rebase_destinations(workspace_command.repo(), &new_parents, commit)?; + } + } + + rebase_descendants_transaction( + ui, + settings, + workspace_command, + &new_parents.iter().ids().cloned().collect_vec(), + &new_children, + &source_commits, + rebase_options, + ) +} + fn rebase_branch( ui: &mut Ui, settings: &UserSettings, @@ -337,17 +368,23 @@ fn rebase_branch( let roots_expression = RevsetExpression::commits(parent_ids) .range(&RevsetExpression::commits(branch_commit_ids)) .roots(); - let root_commits: IndexSet<_> = roots_expression + let root_commits: Vec<_> = roots_expression .evaluate_programmatic(workspace_command.repo().as_ref()) .unwrap() .iter() .commits(workspace_command.repo().store()) .try_collect()?; + workspace_command.check_rewritable(root_commits.iter().ids())?; + for commit in root_commits.iter() { + check_rebase_destinations(workspace_command.repo(), &new_parents, commit)?; + } + rebase_descendants_transaction( ui, settings, workspace_command, - new_parents, + &new_parents.iter().ids().cloned().collect_vec(), + &[], &root_commits, &rebase_options, ) @@ -357,19 +394,15 @@ fn rebase_descendants_transaction( ui: &mut Ui, settings: &UserSettings, workspace_command: &mut WorkspaceCommandHelper, - new_parents: Vec, - target_roots: &IndexSet, + new_parent_ids: &[CommitId], + new_children: &[Commit], + target_roots: &[Commit], rebase_options: &RebaseOptions, ) -> Result<(), CommandError> { if target_roots.is_empty() { return Ok(()); } - workspace_command.check_rewritable(target_roots.iter().ids())?; - for commit in target_roots.iter() { - check_rebase_destinations(workspace_command.repo(), &new_parents, commit)?; - } - let mut tx = workspace_command.start_transaction(); let tx_description = if target_roots.len() == 1 { format!( @@ -390,8 +423,6 @@ fn rebase_descendants_transaction( .iter() .commits(tx.repo().store()) .try_collect()?; - let new_parent_ids = new_parents.iter().ids().cloned().collect_vec(); - let new_children: [Commit; 0] = []; let target_roots = target_roots.iter().ids().cloned().collect_vec(); let MoveCommitsStats { @@ -402,15 +433,12 @@ fn rebase_descendants_transaction( } = move_commits( settings, tx.mut_repo(), - &new_parent_ids, - &new_children, + new_parent_ids, + new_children, &target_commits, &target_roots, rebase_options, )?; - // All the descendants of the roots should be part of `target_commits`, so - // `num_rebased_descendants` should be 0. - debug_assert_eq!(num_rebased_descendants, 0); if num_skipped_rebases > 0 { writeln!( diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index fd81686e87..d46c24071a 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -1644,10 +1644,10 @@ commit. This is true in general; it is not specific to this command. * `-d`, `--destination ` — The revision(s) to rebase onto (can be repeated to create a merge commit) * `-A`, `--insert-after ` — The revision(s) to insert after (can be repeated to create a merge commit) - Only works with `-r`. + Only works with `-r` and `-s`. * `-B`, `--insert-before ` — The revision(s) to insert before (can be repeated to create a merge commit) - Only works with `-r`. + Only works with `-r` and `-s`. * `--skip-emptied` — If true, when rebasing would produce an empty commit, the commit is abandoned. It will not be abandoned if it was already empty before the rebase. Will never skip merge commits with multiple non-empty parents diff --git a/cli/tests/test_rebase_command.rs b/cli/tests/test_rebase_command.rs index b2fd90b9cb..5a8ba3e2aa 100644 --- a/cli/tests/test_rebase_command.rs +++ b/cli/tests/test_rebase_command.rs @@ -96,16 +96,6 @@ fn test_rebase_invalid() { For more information, try '--help'. "###); - // -s with --after - let stderr = test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-s", "a", "--after", "b"]); - insta::assert_snapshot!(stderr, @r###" - error: the argument '--source ' cannot be used with '--insert-after ' - - Usage: jj rebase --source <--destination |--insert-after |--insert-before > - - For more information, try '--help'. - "###); - // -b with --after let stderr = test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-b", "a", "--after", "b"]); insta::assert_snapshot!(stderr, @r###" @@ -129,16 +119,6 @@ fn test_rebase_invalid() { For more information, try '--help'. "###); - // -s with --before - let stderr = test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-s", "a", "--before", "b"]); - insta::assert_snapshot!(stderr, @r###" - error: the argument '--source ' cannot be used with '--insert-before ' - - Usage: jj rebase --source <--destination |--insert-after |--insert-before > - - For more information, try '--help'. - "###); - // -b with --before let stderr = test_env.jj_cmd_cli_error(&repo_path, &["rebase", "-b", "a", "--before", "b"]); insta::assert_snapshot!(stderr, @r###" @@ -1299,7 +1279,7 @@ fn test_rebase_with_child_and_descendant_bug_2600() { } #[test] -fn test_rebase_revisions_after() { +fn test_rebase_after() { let test_env = TestEnvironment::default(); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); let repo_path = test_env.env_root().join("repo"); @@ -1700,6 +1680,62 @@ fn test_rebase_revisions_after() { "###); test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // `rebase -s` of commit "c" and its descendants after itself should be a no-op. + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["rebase", "-s", "c", "--after", "c"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Skipped rebase of 4 commits that were already in place + Nothing changed. + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + @ f: e + ○ e: c + │ ○ d: c + ├─╯ + ○ c: b2 b4 + ├─╮ + │ ○ b4: b3 + │ ○ b3: a + ○ │ b2: b1 + ○ │ b1: a + ├─╯ + ○ a + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + + // `rebase -s` of a commit and its descendants after multiple commits. + let (stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &["rebase", "-s", "c", "--after", "b1", "--after", "b3"], + ); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Rebased 4 commits + Rebased 2 descendant commits + Working copy now at: xznxytkn a4ace41c f | f + Parent commit : nkmrtpmo c7744d08 e | e + Added 0 files, modified 0 files, removed 2 files + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + ○ b4: d f + ├─╮ + │ │ ○ b2: d f + ╭─┬─╯ + │ @ f: e + │ ○ e: c + ○ │ d: c + ├─╯ + ○ c: b1 b3 + ├─╮ + │ ○ b3: a + ○ │ b1: a + ├─╯ + ○ a + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // Should error if a loop will be created. let stderr = test_env.jj_cmd_failure( &repo_path, @@ -1711,7 +1747,7 @@ fn test_rebase_revisions_after() { } #[test] -fn test_rebase_revisions_before() { +fn test_rebase_before() { let test_env = TestEnvironment::default(); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); let repo_path = test_env.env_root().join("repo"); @@ -2121,6 +2157,92 @@ fn test_rebase_revisions_before() { "###); test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // Rebase a subgraph before the parents of one of the commits in the subgraph. + // "c" had parents "b2" and "b4", but no longer has "b4" as a parent since + // "b4" would be a descendant of "c" after the rebase. + let (stdout, stderr) = + test_env.jj_cmd_ok(&repo_path, &["rebase", "-r", "b2::d", "--before", "a"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Rebased 3 commits onto destination + Rebased 6 descendant commits + Working copy now at: xznxytkn 308a31e9 f | f + Parent commit : nkmrtpmo 538444a5 e | e + Added 1 files, modified 0 files, removed 0 files + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + @ f: e + ○ e: b1 b4 + ├─╮ + │ ○ b4: b3 + │ ○ b3: a + ○ │ b1: a + ├─╯ + ○ a: d + ○ d: c + ○ c: b2 + ○ b2 + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + + // `rebase -s` of commit "c" and its descendants before itself should be a + // no-op. + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["rebase", "-s", "c", "--before", "c"]); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Skipped rebase of 4 commits that were already in place + Nothing changed. + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + @ f: e + ○ e: c + │ ○ d: c + ├─╯ + ○ c: b2 b4 + ├─╮ + │ ○ b4: b3 + │ ○ b3: a + ○ │ b2: b1 + ○ │ b1: a + ├─╯ + ○ a + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + + // `rebase -s` of a commit and its descendants before multiple commits. + let (stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &["rebase", "-s", "c", "--before", "b2", "--before", "b4"], + ); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Rebased 4 commits + Rebased 2 descendant commits + Working copy now at: xznxytkn 84704387 f | f + Parent commit : nkmrtpmo cff61821 e | e + Added 0 files, modified 0 files, removed 2 files + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + ○ b4: d f + ├─╮ + │ │ ○ b2: d f + ╭─┬─╯ + │ @ f: e + │ ○ e: c + ○ │ d: c + ├─╯ + ○ c: b1 b3 + ├─╮ + │ ○ b3: a + ○ │ b1: a + ├─╯ + ○ a + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // Should error if a loop will be created. let stderr = test_env.jj_cmd_failure( &repo_path, @@ -2132,7 +2254,7 @@ fn test_rebase_revisions_before() { } #[test] -fn test_rebase_revisions_after_before() { +fn test_rebase_after_before() { let test_env = TestEnvironment::default(); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); let repo_path = test_env.env_root().join("repo"); @@ -2287,6 +2409,34 @@ fn test_rebase_revisions_after_before() { "###); test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // `rebase -s` of a commit and its descendants. + let (stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &["rebase", "-s", "c", "--before", "b1", "--after", "b2"], + ); + insta::assert_snapshot!(stdout, @""); + insta::assert_snapshot!(stderr, @r###" + Rebased 4 commits + Rebased 1 descendant commits + Working copy now at: lylxulpl 108f0202 f | f + Parent commit : kmkuslsw 52245d71 e | e + Added 0 files, modified 0 files, removed 1 files + "###); + insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###" + ○ b1: a d f + ├─┬─╮ + │ │ @ f: e + │ │ ○ e: c + │ ○ │ d: c + │ ├─╯ + │ ○ c: b2 + │ ○ b2: a + ├─╯ + ○ a + ◆ + "###); + test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]); + // Should error if a loop will be created. let stderr = test_env.jj_cmd_failure( &repo_path,