sequencer: handle post-rewrite for merge commands
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 23 Feb 2018 12:39:07 +0000 (13:39 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 23 Feb 2018 19:30:52 +0000 (11:30 -0800)
In the previous patches, we implemented the basic functionality of the
`git rebase -i --recreate-merges` command, in particular the `merge`
command to create merge commits in the sequencer.

The interactive rebase is a lot more these days, though, than a simple
cherry-pick in a loop. For example, it calls the post-rewrite hook (if
any) after rebasing with a mapping of the old->new commits.

This patch implements the post-rewrite handling for the `merge` command
we just introduced. The other commands that were added recently (`label`
and `reset`) do not create new commits, therefore post-rewrite do not
need to handle them.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sequencer.c
t/t3430-rebase-recreate-merges.sh
index fdf45797af77a0658c4179351b3711ef8ffb5d03..0bb59e4411528a5cf3dee9b7d3db2c3514cb32c8 100644 (file)
@@ -2458,11 +2458,14 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                else if (item->command == TODO_RESET)
                        res = do_reset(item->arg, item->arg_len, opts);
                else if (item->command == TODO_MERGE ||
-                        item->command == TODO_MERGE_AND_EDIT)
+                        item->command == TODO_MERGE_AND_EDIT) {
                        res = do_merge(item->commit, item->arg, item->arg_len,
                                       item->command == TODO_MERGE_AND_EDIT ?
                                       EDIT_MSG | VERIFY_MSG : 0, opts);
-               else if (!is_noop(item->command))
+                       if (item->commit)
+                               record_in_rewritten(&item->commit->object.oid,
+                                                   peek_command(todo_list, 1));
+               } else if (!is_noop(item->command))
                        return error(_("unknown command %d"), item->command);
 
                todo_list->current++;
index e10c7970e885b58d14cddaa482f86e9fa4482b0f..2fb74cbbd32aa4ccf408c588c33ab7f39432e0fb 100755 (executable)
@@ -157,4 +157,29 @@ test_expect_success 'refs/rewritten/* is worktree-local' '
        test_cmp_rev HEAD "$(cat wt/b)"
 '
 
+test_expect_success 'post-rewrite hook and fixups work for merges' '
+       git checkout -b post-rewrite &&
+       test_commit same1 &&
+       git reset --hard HEAD^ &&
+       test_commit same2 &&
+       git merge -m "to fix up" same1 &&
+       echo same old same old >same2.t &&
+       test_tick &&
+       git commit --fixup HEAD same2.t &&
+       fixup="$(git rev-parse HEAD)" &&
+
+       mkdir -p .git/hooks &&
+       test_when_finished "rm .git/hooks/post-rewrite" &&
+       echo "cat >actual" | write_script .git/hooks/post-rewrite &&
+
+       test_tick &&
+       git rebase -i --autosquash --recreate-merges HEAD^^^ &&
+       printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
+               $fixup^^2 HEAD^2 \
+               $fixup^^ HEAD^ \
+               $fixup^ HEAD \
+               $fixup HEAD) &&
+       test_cmp expect actual
+'
+
 test_done