3 # Copyright (c) 2018 Johannes E. Schindelin
6 test_description='git rebase -i --rebase-merges
8 This test runs git rebase "interactively", retaining the branch structure by
9 recreating merge commits.
15 A - C - D - E - H (master)
22 . "$TEST_DIRECTORY"/lib-rebase.sh
26 git log --graph --boundary --format=%s "$@" >output &&
27 sed "s/ *$//" <output >output.trimmed &&
28 test_cmp expect output.trimmed
31 test_expect_success 'setup' '
32 write_script replace-editor.sh <<-\EOF &&
33 mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
34 cp script-from-scratch "$1"
38 git checkout -b first &&
40 b=$(git rev-parse --short HEAD) &&
41 git checkout master &&
43 c=$(git rev-parse --short HEAD) &&
45 d=$(git rev-parse --short HEAD) &&
46 git merge --no-commit B &&
50 e=$(git rev-parse --short HEAD) &&
51 git checkout -b second C &&
53 f=$(git rev-parse --short HEAD) &&
55 g=$(git rev-parse --short HEAD) &&
56 git checkout master &&
57 git merge --no-commit G &&
60 h=$(git rev-parse --short HEAD) &&
63 test_commit conflicting-G G.t
66 test_expect_success 'create completely different structure' '
67 cat >script-from-scratch <<-\EOF &&
82 merge onebranch # Merge the topic branch '\''onebranch'\''
84 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
86 git rebase -i -r A master &&
87 test_cmp_graph <<-\EOF
88 * Merge the topic branch '\''onebranch'\''
102 test_expect_success 'generate correct todo list' '
103 cat >expect <<-EOF &&
117 reset branch-point # C
124 grep -v "^#" <.git/ORIGINAL-TODO >output &&
125 test_cmp expect output
128 test_expect_success '`reset` refuses to overwrite untracked files' '
129 git checkout -b refuse-to-reset &&
130 test_commit dont-overwrite-untracked &&
131 git checkout @{-1} &&
132 : >dont-overwrite-untracked.t &&
133 echo "reset refs/tags/dont-overwrite-untracked" >script-from-scratch &&
134 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
135 test_must_fail git rebase -ir HEAD &&
139 test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
140 test_when_finished "test_might_fail git rebase --abort" &&
141 git checkout -b conflicting-merge A &&
143 : fail because of conflicting untracked file &&
145 echo "merge -C H G" >script-from-scratch &&
146 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
148 test_must_fail git rebase -ir HEAD &&
149 grep "^merge -C .* G$" .git/rebase-merge/done &&
150 grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
151 test_path_is_file .git/rebase-merge/patch &&
153 : fail because of merge conflict &&
154 rm G.t .git/rebase-merge/patch &&
155 git reset --hard conflicting-G &&
156 test_must_fail git rebase --continue &&
157 ! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
158 test_path_is_file .git/rebase-merge/patch
162 test_expect_success 'failed `merge <branch>` does not crash' '
163 test_when_finished "test_might_fail git rebase --abort" &&
164 git checkout conflicting-G &&
166 echo "merge G" >script-from-scratch &&
167 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
169 test_must_fail git rebase -ir HEAD &&
170 ! grep "^merge G$" .git/rebase-merge/git-rebase-todo &&
171 grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
174 test_expect_success 'fast-forward merge -c still rewords' '
175 git checkout -b fast-forward-merge-c H &&
178 FAKE_COMMIT_MESSAGE=edited \
179 GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
182 echo edited >expected &&
183 git log --pretty=format:%B -1 >actual &&
184 test_cmp expected actual
187 test_expect_success 'with a branch tip that was cherry-picked already' '
188 git checkout -b already-upstream master &&
189 base="$(git rev-parse --verify HEAD)" &&
193 git reset --hard $base &&
196 git merge -m "Merge branch A" A2 &&
198 git checkout -b upstream-with-a2 $base &&
200 git cherry-pick A2 &&
202 git checkout already-upstream &&
204 git rebase -i -r upstream-with-a2 &&
205 test_cmp_graph upstream-with-a2.. <<-\EOF
215 test_expect_success 'do not rebase cousins unless asked for' '
216 git checkout -b cousins master &&
217 before="$(git rev-parse --verify HEAD)" &&
219 git rebase -r HEAD^ &&
220 test_cmp_rev HEAD $before &&
222 git rebase --rebase-merges=rebase-cousins HEAD^ &&
223 test_cmp_graph HEAD^.. <<-\EOF
224 * Merge the topic branch '\''onebranch'\''
233 test_expect_success 'refs/rewritten/* is worktree-local' '
234 git worktree add wt &&
235 cat >wt/script-from-scratch <<-\EOF &&
237 exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
238 exec git rev-parse --verify refs/rewritten/xyz >b
241 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
242 git -C wt rebase -i HEAD &&
243 test_must_be_empty wt/a &&
244 test_cmp_rev HEAD "$(cat wt/b)"
247 test_expect_success '--abort cleans up refs/rewritten' '
248 git checkout -b abort-cleans-refs-rewritten H &&
249 GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
250 git rev-parse --verify refs/rewritten/onto &&
251 git rebase --abort &&
252 test_must_fail git rev-parse --verify refs/rewritten/onto
255 test_expect_success '--quit cleans up refs/rewritten' '
256 git checkout -b quit-cleans-refs-rewritten H &&
257 GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
258 git rev-parse --verify refs/rewritten/onto &&
260 test_must_fail git rev-parse --verify refs/rewritten/onto
263 test_expect_success 'post-rewrite hook and fixups work for merges' '
264 git checkout -b post-rewrite H &&
266 git reset --hard HEAD^ &&
268 git merge -m "to fix up" same1 &&
269 echo same old same old >same2.t &&
271 git commit --fixup HEAD same2.t &&
272 fixup="$(git rev-parse HEAD)" &&
274 mkdir -p .git/hooks &&
275 test_when_finished "rm .git/hooks/post-rewrite" &&
276 echo "cat >actual" | write_script .git/hooks/post-rewrite &&
279 git rebase -i --autosquash -r HEAD^^^ &&
280 printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
285 test_cmp expect actual
288 test_expect_success 'refuse to merge ancestors of HEAD' '
289 echo "merge HEAD^" >script-from-scratch &&
290 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
291 before="$(git rev-parse HEAD)" &&
292 git rebase -i HEAD &&
293 test_cmp_rev HEAD $before
296 test_expect_success 'root commits' '
297 git checkout --orphan unrelated &&
298 (GIT_AUTHOR_NAME="Parsnip" GIT_AUTHOR_EMAIL="root@example.com" \
299 test_commit second-root) &&
300 test_commit third-root &&
301 cat >script-from-scratch <<-\EOF &&
306 merge first-branch # Merge the 3rd root
308 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
310 git rebase -i --force-rebase --root -r &&
311 test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
312 test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
313 test $(git rev-parse second-root:second-root.t) = \
314 $(git rev-parse HEAD^:second-root.t) &&
315 test_cmp_graph HEAD <<-\EOF &&
322 : fast forward if possible &&
323 before="$(git rev-parse --verify HEAD)" &&
324 test_might_fail git config --unset sequence.editor &&
326 git rebase -i --root -r &&
327 test_cmp_rev HEAD $before
330 test_expect_success 'a "merge" into a root commit is a fast-forward' '
331 head=$(git rev-parse HEAD) &&
332 cat >script-from-scratch <<-EOF &&
336 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
338 git rebase -i -r HEAD^ &&
339 test_cmp_rev HEAD $head
342 test_expect_success 'A root commit can be a cousin, treat it that way' '
343 git checkout --orphan khnum &&
345 git checkout -b asherah master &&
346 test_commit shamkat &&
347 git merge --allow-unrelated-histories khnum &&
349 git rebase -f -r HEAD^ &&
350 ! test_cmp_rev HEAD^2 khnum &&
351 test_cmp_graph HEAD^.. <<-\EOF &&
352 * Merge branch '\''khnum'\'' into asherah
358 git rebase --rebase-merges=rebase-cousins HEAD^ &&
359 test_cmp_graph HEAD^.. <<-\EOF
360 * Merge branch '\''khnum'\'' into asherah
368 test_expect_success 'labels that are object IDs are rewritten' '
369 git checkout -b third B &&
371 third=$(git rev-parse HEAD) &&
372 git checkout -b labels master &&
373 git merge --no-commit third &&
375 git commit -m "Merge commit '\''$third'\'' into labels" &&
376 echo noop >script-from-scratch &&
377 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
379 git rebase -i -r A &&
380 grep "^label $third-" .git/ORIGINAL-TODO &&
381 ! grep "^label $third$" .git/ORIGINAL-TODO
384 test_expect_success 'octopus merges' '
385 git checkout -b three &&
386 test_commit before-octopus &&
388 git checkout -b two HEAD^ &&
390 git checkout -b one HEAD^ &&
393 (GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="hank@sea.world" \
394 git merge -m "Tüntenfüsch" two three) &&
396 : fast forward if possible &&
397 before="$(git rev-parse --verify HEAD)" &&
399 git rebase -i -r HEAD^^ &&
400 test_cmp_rev HEAD $before &&
403 git rebase -i --force-rebase -r HEAD^^ &&
404 test "Hank" = "$(git show -s --format=%an HEAD)" &&
405 test "$before" != $(git rev-parse HEAD) &&
406 test_cmp_graph HEAD^^.. <<-\EOF
418 test_expect_success 'with --autosquash and --exec' '
419 git checkout -b with-exec H &&
422 git commit --fixup B B.t &&
423 write_script show.sh <<-\EOF &&
424 subject="$(git show -s --format=%s HEAD)"
425 content="$(git diff HEAD^! | tail -n 1)"
426 echo "$subject: $content"
429 git rebase -ir --autosquash --exec ./show.sh A >actual &&
430 grep "B: +Booh" actual &&
431 grep "E: +Booh" actual &&
435 test_expect_success '--continue after resolving conflicts after a merge' '
436 git checkout -b already-has-g E &&
437 git cherry-pick E..G &&
440 git checkout -b conflicts-in-merge H &&
441 test_commit H2 H2.t conflicts H2-conflict &&
442 test_must_fail git rebase -r already-has-g &&
443 grep conflicts H2.t &&
444 echo resolved >H2.t &&
446 git rebase --continue &&
447 test_must_fail git rev-parse --verify HEAD^2 &&
448 test_path_is_missing .git/MERGE_HEAD
451 test_expect_success '--rebase-merges with strategies' '
452 git checkout -b with-a-strategy F &&
454 git merge -m "Merge conflicting-G" conflicting-G &&
456 : first, test with a merge strategy option &&
457 git rebase -ir -Xtheirs G &&
458 echo conflicting-G >expect &&
459 test_cmp expect G.t &&
461 : now, try with a merge strategy other than recursive &&
462 git reset --hard @{1} &&
463 write_script git-merge-override <<-\EOF &&
464 echo overridden$1 >>G.t
467 PATH="$PWD:$PATH" git rebase -ir -s override -Xxopt G &&
468 test_write_lines G overridden--xopt >expect &&