Skip to content

Commit

Permalink
Fix rebase -r of a parent of a merge commit
Browse files Browse the repository at this point in the history
Previously, using `rebase -r` on the parent of a merge commit
turned it into a non-merge commit. In other words, starting
with

```
    o   d
    |\
    o | c
    o | b
    | o a
    |/  
    o 
```

and doing `rebase -r c -d a` resulted in

```
    o d
    o b
    | o c
    | o a
    |/  
    o 
```

where `d` is no longer a merge commit.

For reference, here's a complete test that passed before this commit (but should NOT pass; see the diff for a test that should pass):

```
#[test]
fn test_rebase_single_revision_merge_parent() {
    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");

    create_commit(&test_env, &repo_path, "a", &[]);
    create_commit(&test_env, &repo_path, "b", &[]);
    create_commit(&test_env, &repo_path, "c", &["b"]);
    create_commit(&test_env, &repo_path, "d", &["a", "c"]);
    // Test the setup
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @
    o   d
    |\
    o | c
    o | b
    | o a
    |/
    o
    "###);

    let stdout = test_env.jj_cmd_success(&repo_path, &["rebase", "-r", "c", "-d", "a"]);
    insta::assert_snapshot!(stdout, @r###"
    Also rebased 2 descendant commits onto parent of rebased commit
    Working copy now at: 3e176b54d680 (no description set)
    Added 0 files, modified 0 files, removed 2 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @
    o d
    | o c
    o | b
    | o a
    |/
    o
    "###);
}
```
  • Loading branch information
ilyagr committed Sep 12, 2022
1 parent 459f2f7 commit 3831033
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
39 changes: 37 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4130,6 +4130,7 @@ fn rebase_revision(
let children_expression = RevsetExpression::commit(old_commit.id().clone()).children();
let mut num_rebased_descendants = 0;
let store = workspace_command.repo.store();

for child_commit in children_expression
.evaluate(
workspace_command.repo().as_repo_ref(),
Expand All @@ -4139,11 +4140,45 @@ fn rebase_revision(
.iter()
.commits(store)
{
let child_commit = child_commit?;
let new_parent_ids: Vec<CommitId> = child_commit
.parents()
.iter()
.flat_map(|c| {
if c == &old_commit {
old_commit
.parents()
.iter()
.map(|c| c.id().clone())
.collect()
} else {
[c.id().clone()].to_vec()
}
})
.collect();

// Some of `new_parents` may be ancestors of other elements of `new_parents`
// as in `test_rebase_single_revision`.
let new_child_parents: Result<Vec<Commit>, BackendError> = RevsetExpression::Difference(
RevsetExpression::commits(new_parent_ids.clone()),
RevsetExpression::commits(new_parent_ids.clone())
.parents()
.ancestors(),
)
.evaluate(
workspace_command.repo().as_repo_ref(),
Some(&workspace_command.workspace_id()),
)
.unwrap()
.iter()
.commits(store)
.collect();

rebase_commit(
ui.settings(),
tx.mut_repo(),
&child_commit?,
&old_commit.parents(),
&child_commit,
&new_child_parents?,
);
num_rebased_descendants += 1;
}
Expand Down
51 changes: 47 additions & 4 deletions tests/test_rebase_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,23 @@ fn test_rebase_single_revision() {
let stdout = test_env.jj_cmd_success(&repo_path, &["rebase", "-r", "b", "-d", "a"]);
insta::assert_snapshot!(stdout, @r###"
Also rebased 3 descendant commits onto parent of rebased commit
Working copy now at: b5ce312dacd3 (no description set)
Added 0 files, modified 0 files, removed 2 files
Working copy now at: e7299ad0c9a7 (no description set)
Added 0 files, modified 0 files, removed 1 files
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@
o d
o c
| o b
| o a
|/
o a
o
"###);
test_env.jj_cmd_success(&repo_path, &["undo"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["rebase", "-r", "c", "-d", "root"]);
insta::assert_snapshot!(stdout, @r###"
Also rebased 2 descendant commits onto parent of rebased commit
Working copy now at: 5e21ebf9fd2b (no description set)
Working copy now at: 2d90465bd244 (no description set)
Added 0 files, modified 0 files, removed 1 files
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
Expand All @@ -226,6 +226,49 @@ fn test_rebase_single_revision() {
"###);
}

#[test]
fn test_rebase_single_revision_merge_parent() {
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");

create_commit(&test_env, &repo_path, "a", &[]);
create_commit(&test_env, &repo_path, "b", &[]);
create_commit(&test_env, &repo_path, "c", &["b"]);
create_commit(&test_env, &repo_path, "d", &["a", "c"]);
// Test the setup
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@
o d
|\
o | c
o | b
| o a
|/
o
"###);

// Descendants of the rebased commit should be rebased onto parents, and if
// the descendant is a merge commit, it shouldn't forget its other parents.
let stdout = test_env.jj_cmd_success(&repo_path, &["rebase", "-r", "c", "-d", "a"]);
insta::assert_snapshot!(stdout, @r###"
Also rebased 2 descendant commits onto parent of rebased commit
Working copy now at: 9b0a69a895b4 (no description set)
Added 0 files, modified 0 files, removed 1 files
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@
o d
|\
| | o c
| |/
o | b
| o a
|/
o
"###);
}

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

0 comments on commit 3831033

Please sign in to comment.