Skip to content

Commit

Permalink
rebase: allow both --insert-after and --insert-before options to …
Browse files Browse the repository at this point in the history
…be used simultaneously
  • Loading branch information
bnjmnt4n committed Apr 25, 2024
1 parent fde5a91 commit e4c52a5
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
54 changes: 52 additions & 2 deletions cli/src/commands/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)]
#[command(verbatim_doc_comment)]
#[command(group(ArgGroup::new("to_rebase").args(&["branch", "source", "revisions"])))]
#[command(group(ArgGroup::new("target").args(&["destination", "insert_after", "insert_before"]).required(true)))]
#[command(group(ArgGroup::new("target").args(&["destination", "insert_after", "insert_before"]).multiple(true).required(true)))]
pub(crate) struct RebaseArgs {
/// Rebase the whole branch relative to destination's ancestors (can be
/// repeated)
Expand Down Expand Up @@ -169,6 +169,7 @@ pub(crate) struct RebaseArgs {
long,
short = 'A',
visible_alias = "after",
conflicts_with = "destination",
conflicts_with = "source",
conflicts_with = "branch"
)]
Expand All @@ -181,6 +182,7 @@ pub(crate) struct RebaseArgs {
long,
short = 'B',
visible_alias = "before",
conflicts_with = "destination",
conflicts_with = "source",
conflicts_with = "branch"
)]
Expand Down Expand Up @@ -239,7 +241,20 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets
.parse_union_revsets(&args.revisions)?
.evaluate_to_commits()?
.try_collect()?; // in reverse topological order
if !args.insert_after.is_empty() {
if !args.insert_after.is_empty() && !args.insert_before.is_empty() {
let after_commits =
workspace_command.resolve_some_revsets_default_single(&args.insert_after)?;
let before_commits =
workspace_command.resolve_some_revsets_default_single(&args.insert_before)?;
rebase_revision_after_before(
ui,
command.settings(),
&mut workspace_command,
&after_commits,
&before_commits,
revision_commits,
)?;
} else if !args.insert_after.is_empty() {
let after_commits =
workspace_command.resolve_some_revsets_default_single(&args.insert_after)?;
rebase_revisions_after(
Expand Down Expand Up @@ -599,6 +614,41 @@ fn rebase_revisions_before(
)
}

fn rebase_revision_after_before(
ui: &mut Ui,
settings: &UserSettings,
workspace_command: &mut WorkspaceCommandHelper,
after_commits: &IndexSet<Commit>,
before_commits: &IndexSet<Commit>,
target_commits: Vec<Commit>,
) -> Result<(), CommandError> {
workspace_command.check_rewritable(target_commits.iter().ids())?;
let before_commit_ids = before_commits.iter().ids().cloned().collect_vec();
workspace_command.check_rewritable(&before_commit_ids)?;

let after_commit_ids = after_commits.iter().ids().cloned().collect_vec();
let new_children_expression = RevsetExpression::commits(before_commit_ids);
let new_parents_expression = RevsetExpression::commits(after_commit_ids.clone());

ensure_no_commit_loop(
workspace_command.repo().as_ref(),
&new_children_expression,
&new_parents_expression,
)?;

let new_parent_ids = after_commit_ids;
let new_children = before_commits.iter().cloned().collect_vec();

move_commits(
ui,
settings,
workspace_command,
&new_parent_ids,
&new_children,
target_commits,
)
}

/// Extracts `target_commits` from the graph by rebasing its descendants onto
/// its parents. This assumes that `target_commits` can be rewritten.
/// `target_commits` should be in reverse topological order.
Expand Down
121 changes: 121 additions & 0 deletions cli/tests/test_rebase_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,127 @@ fn test_rebase_revision_before() {
"###);
}

#[test]
fn test_rebase_revision_after_before() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");

create_commit(&test_env, &repo_path, "a", &[]);
create_commit(&test_env, &repo_path, "b1", &["a"]);
create_commit(&test_env, &repo_path, "b2", &["a"]);
create_commit(&test_env, &repo_path, "c", &["b1", "b2"]);
create_commit(&test_env, &repo_path, "d", &["c"]);
create_commit(&test_env, &repo_path, "e", &["c"]);
create_commit(&test_env, &repo_path, "f", &["e"]);
// Test the setup
insta::assert_snapshot!(get_long_log_output(&test_env, &repo_path), @r###"
@ f lylxulpl 88f778c5
◉ e kmkuslsw 48dd9e3f
│ ◉ d znkkpsqq 92438fc9
├─╯
◉ c vruxwmqv c41e416e
├─╮
│ ◉ b2 royxmykx 903ab0d6
◉ │ b1 zsuskuln 072d5ae1
├─╯
◉ a rlvkpnrz 2443ea76
◉ zzzzzzzz 00000000
"###);

let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["rebase", "-r", "c", "--before", "e", "--after", "d"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Rebased 1 commits onto destination
Rebased 1 descendant commits onto parent of rebased commits
Rebased 2 commits onto commits at destination
Working copy now at: lylxulpl 4aa9c561 f | f
Parent commit : kmkuslsw 288ca227 e | e
Added 1 files, modified 0 files, removed 0 files
"###);
insta::assert_snapshot!(get_long_log_output(&test_env, &repo_path), @r###"
@ f lylxulpl 4aa9c561
◉ e kmkuslsw 288ca227
◉ c vruxwmqv 583a5164
◉ d znkkpsqq 1ca86c56
├─╮
│ ◉ b2 royxmykx 903ab0d6
◉ │ b1 zsuskuln 072d5ae1
├─╯
◉ a rlvkpnrz 2443ea76
◉ zzzzzzzz 00000000
"###);

let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["rebase", "-r", "f", "--before", "e", "--after", "d"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Rebased 1 commits onto destination
Rebased 1 commits onto commits at destination
Working copy now at: lylxulpl 044de9f6 f | f
Parent commit : znkkpsqq 1ca86c56 d | d
Added 0 files, modified 0 files, removed 2 files
"###);
insta::assert_snapshot!(get_long_log_output(&test_env, &repo_path), @r###"
◉ e kmkuslsw c3183134
├─╮
│ @ f lylxulpl 044de9f6
◉ │ c vruxwmqv 583a5164
├─╯
◉ d znkkpsqq 1ca86c56
├─╮
│ ◉ b2 royxmykx 903ab0d6
◉ │ b1 zsuskuln 072d5ae1
├─╯
◉ a rlvkpnrz 2443ea76
◉ zzzzzzzz 00000000
"###);

// Test multiple revisions.
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&[
"rebase", "-r", "c", "-r", "f", "-r", "e", "--before", "d", "--after", "a",
],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Rebased 3 commits onto destination
Rebased 1 commits onto commits at destination
Working copy now at: lylxulpl 02505fa5 f | f
Parent commit : rlvkpnrz 2443ea76 a | a
Added 0 files, modified 0 files, removed 3 files
"###);
insta::assert_snapshot!(get_long_log_output(&test_env, &repo_path), @r###"
◉ d znkkpsqq 879e9d83
├─┬─╮
│ │ ◉ e kmkuslsw b3ff12c1
│ │ ├─╮
│ │ │ @ f lylxulpl 02505fa5
│ │ ◉ │ c vruxwmqv 3dfde861
│ │ ├─╯
│ ◉ │ b2 royxmykx 903ab0d6
│ ├─╯
◉ │ b1 zsuskuln 072d5ae1
├─╯
◉ a rlvkpnrz 2443ea76
◉ zzzzzzzz 00000000
"###);

let stderr = test_env.jj_cmd_failure(
&repo_path,
&["rebase", "-r", "e", "--before", "a", "--before", "d"],
);
insta::assert_snapshot!(stderr, @r###"
Error: Refusing to create a loop: commit b3ff12c1523d would be both an ancestor and a descendant of the rebased commit
"###);
}

#[test]
fn test_rebase_skip_empty() {
let test_env = TestEnvironment::default();
Expand Down

0 comments on commit e4c52a5

Please sign in to comment.