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 (main)
21 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
22 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
24 TEST_PASSES_SANITIZE_LEAK
=true
26 .
"$TEST_DIRECTORY"/lib-rebase.sh
27 .
"$TEST_DIRECTORY"/lib-log-graph.sh
31 lib_test_cmp_graph
--boundary --format=%s
"$@"
34 test_expect_success
'setup' '
35 write_script replace-editor.sh <<-\EOF &&
36 mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
37 cp script-from-scratch "$1"
41 git checkout -b first &&
43 b=$(git rev-parse --short HEAD) &&
46 c=$(git rev-parse --short HEAD) &&
48 d=$(git rev-parse --short HEAD) &&
49 git merge --no-commit B &&
53 e=$(git rev-parse --short HEAD) &&
54 git checkout -b second C &&
56 f=$(git rev-parse --short HEAD) &&
58 g=$(git rev-parse --short HEAD) &&
60 git merge --no-commit G &&
63 h=$(git rev-parse --short HEAD) &&
66 test_commit conflicting-G G.t
69 test_expect_success
'create completely different structure' '
70 cat >script-from-scratch <<-\EOF &&
85 merge onebranch # Merge the topic branch '\''onebranch'\''
87 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
89 git rebase -i -r A main &&
90 test_cmp_graph <<-\EOF
91 * Merge the topic branch '\''onebranch'\''
105 test_expect_success
'generate correct todo list' '
106 cat >expect <<-EOF &&
120 reset branch-point # C
122 merge -C $e first # E
123 merge -C $h second # H
127 grep -v "^#" <.git/ORIGINAL-TODO >output &&
128 test_cmp expect output
131 test_expect_success
'`reset` refuses to overwrite untracked files' '
133 test_commit dont-overwrite-untracked &&
134 cat >script-from-scratch <<-EOF &&
135 exec >dont-overwrite-untracked.t
136 pick $(git rev-parse B) B
137 reset refs/tags/dont-overwrite-untracked
138 pick $(git rev-parse C) C
139 exec cat .git/rebase-merge/done >actual
141 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
142 test_must_fail git rebase -ir A &&
143 test_cmp_rev HEAD B &&
144 head -n3 script-from-scratch >expect &&
145 test_cmp expect .git/rebase-merge/done &&
146 rm dont-overwrite-untracked.t &&
147 git rebase --continue &&
148 tail -n3 script-from-scratch >>expect &&
149 test_cmp expect actual
152 test_expect_success
'`reset` rejects trees' '
153 test_when_finished "test_might_fail git rebase --abort" &&
154 test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
155 git rebase -i B C >out 2>err &&
156 grep "object .* is a tree" err &&
157 test_must_be_empty out
160 test_expect_success
'`reset` only looks for labels under refs/rewritten/' '
161 test_when_finished "test_might_fail git rebase --abort" &&
162 git branch refs/rewritten/my-label A &&
163 test_must_fail env GIT_SEQUENCE_EDITOR="echo reset my-label >" \
164 git rebase -i B C >out 2>err &&
165 grep "could not resolve ${SQ}my-label${SQ}" err &&
166 test_must_be_empty out
169 test_expect_success
'failed `merge -C` writes patch (may be rescheduled, too)' '
170 test_when_finished "test_might_fail git rebase --abort" &&
171 git checkout -b conflicting-merge A &&
173 : fail because of conflicting untracked file &&
175 echo "merge -C H G" >script-from-scratch &&
176 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
178 test_must_fail git rebase -ir HEAD &&
179 test_cmp_rev REBASE_HEAD H^0 &&
180 grep "^merge -C .* G$" .git/rebase-merge/done &&
181 grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
182 test_path_is_missing .git/rebase-merge/patch &&
183 echo changed >file1 &&
185 test_must_fail git rebase --continue 2>err &&
186 grep "error: you have staged changes in your working tree" err &&
188 : fail because of merge conflict &&
189 git reset --hard conflicting-G &&
190 test_must_fail git rebase --continue &&
191 ! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
192 test_path_is_file .git/rebase-merge/patch
195 test_expect_success
'failed `merge <branch>` does not crash' '
196 test_when_finished "test_might_fail git rebase --abort" &&
197 git checkout conflicting-G &&
199 echo "merge G" >script-from-scratch &&
200 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
202 test_must_fail git rebase -ir HEAD &&
203 ! grep "^merge G$" .git/rebase-merge/git-rebase-todo &&
204 grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
207 test_expect_success
'merge -c commits before rewording and reloads todo-list' '
208 cat >script-from-scratch <<-\EOF &&
213 git checkout -b merge-c H &&
216 GIT_SEQUENCE_EDITOR="\"$PWD/replace-editor.sh\"" \
219 check_reworded_commits E H
222 test_expect_success
'merge -c rewords when a strategy is given' '
223 git checkout -b merge-c-with-strategy H &&
224 write_script git-merge-override <<-\EOF &&
225 echo overridden$1 >G.t
230 GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
231 GIT_EDITOR="echo edited >>" \
232 git rebase --no-ff -ir -s override -Xxopt E &&
233 test_write_lines overridden--xopt >expect &&
234 test_cmp expect G.t &&
235 test_write_lines H "" edited "" >expect &&
236 git log --format=%B -1 >actual &&
237 test_cmp expect actual
240 test_expect_success
'with a branch tip that was cherry-picked already' '
241 git checkout -b already-upstream main &&
242 base="$(git rev-parse --verify HEAD)" &&
246 git reset --hard $base &&
249 git merge -m "Merge branch A" A2 &&
251 git checkout -b upstream-with-a2 $base &&
253 git cherry-pick A2 &&
255 git checkout already-upstream &&
257 git rebase -i -r upstream-with-a2 &&
258 test_cmp_graph upstream-with-a2.. <<-\EOF
268 test_expect_success
'--no-rebase-merges countermands --rebase-merges' '
269 git checkout -b no-rebase-merges E &&
270 git rebase --rebase-merges --no-rebase-merges C &&
271 test_cmp_graph C.. <<-\EOF
278 test_expect_success
'do not rebase cousins unless asked for' '
279 git checkout -b cousins main &&
280 before="$(git rev-parse --verify HEAD)" &&
282 git rebase -r HEAD^ &&
283 test_cmp_rev HEAD $before &&
285 git rebase --rebase-merges=rebase-cousins HEAD^ &&
286 test_cmp_graph HEAD^.. <<-\EOF
287 * Merge the topic branch '\''onebranch'\''
296 test_expect_success
'rebase.rebaseMerges=rebase-cousins is equivalent to --rebase-merges=rebase-cousins' '
297 test_config rebase.rebaseMerges rebase-cousins &&
298 git checkout -b config-rebase-cousins main &&
300 test_cmp_graph HEAD^.. <<-\EOF
301 * Merge the topic branch '\''onebranch'\''
310 test_expect_success
'--no-rebase-merges overrides rebase.rebaseMerges=no-rebase-cousins' '
311 test_config rebase.rebaseMerges no-rebase-cousins &&
312 git checkout -b override-config-no-rebase-cousins E &&
313 git rebase --no-rebase-merges C &&
314 test_cmp_graph C.. <<-\EOF
321 test_expect_success
'--rebase-merges overrides rebase.rebaseMerges=rebase-cousins' '
322 test_config rebase.rebaseMerges rebase-cousins &&
323 git checkout -b override-config-rebase-cousins E &&
324 before="$(git rev-parse --verify HEAD)" &&
326 git rebase --rebase-merges C &&
327 test_cmp_rev HEAD $before
330 test_expect_success
'refs/rewritten/* is worktree-local' '
331 git worktree add wt &&
332 cat >wt/script-from-scratch <<-\EOF &&
334 exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
335 exec git rev-parse --verify refs/rewritten/xyz >b
338 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
339 git -C wt rebase -i HEAD &&
340 test_must_be_empty wt/a &&
341 test_cmp_rev HEAD "$(cat wt/b)"
344 test_expect_success
'--abort cleans up refs/rewritten' '
345 git checkout -b abort-cleans-refs-rewritten H &&
346 GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
347 git rev-parse --verify refs/rewritten/onto &&
348 git rebase --abort &&
349 test_must_fail git rev-parse --verify refs/rewritten/onto
352 test_expect_success
'--quit cleans up refs/rewritten' '
353 git checkout -b quit-cleans-refs-rewritten H &&
354 GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
355 git rev-parse --verify refs/rewritten/onto &&
357 test_must_fail git rev-parse --verify refs/rewritten/onto
360 test_expect_success
'post-rewrite hook and fixups work for merges' '
361 git checkout -b post-rewrite H &&
363 git reset --hard HEAD^ &&
365 git merge -m "to fix up" same1 &&
366 echo same old same old >same2.t &&
368 git commit --fixup HEAD same2.t &&
369 fixup="$(git rev-parse HEAD)" &&
371 test_hook post-rewrite <<-\EOF &&
376 git rebase -i --autosquash -r HEAD^^^ &&
377 printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
382 test_cmp expect actual
385 test_expect_success
'refuse to merge ancestors of HEAD' '
386 echo "merge HEAD^" >script-from-scratch &&
387 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
388 before="$(git rev-parse HEAD)" &&
389 git rebase -i HEAD &&
390 test_cmp_rev HEAD $before
393 test_expect_success
'root commits' '
394 git checkout --orphan unrelated &&
395 test_commit --author "Parsnip <root@example.com>" second-root &&
396 test_commit third-root &&
397 cat >script-from-scratch <<-\EOF &&
402 merge first-branch # Merge the 3rd root
404 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
406 git rebase -i --force-rebase --root -r &&
407 test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
408 test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
409 test $(git rev-parse second-root:second-root.t) = \
410 $(git rev-parse HEAD^:second-root.t) &&
411 test_cmp_graph HEAD <<-\EOF &&
418 : fast forward if possible &&
419 before="$(git rev-parse --verify HEAD)" &&
420 test_might_fail git config --unset sequence.editor &&
422 git rebase -i --root -r &&
423 test_cmp_rev HEAD $before
426 test_expect_success
'a "merge" into a root commit is a fast-forward' '
427 head=$(git rev-parse HEAD) &&
428 cat >script-from-scratch <<-EOF &&
432 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
434 git rebase -i -r HEAD^ &&
435 test_cmp_rev HEAD $head
438 test_expect_success
'A root commit can be a cousin, treat it that way' '
439 git checkout --orphan khnum &&
441 git checkout -b asherah main &&
442 test_commit shamkat &&
443 git merge --allow-unrelated-histories khnum &&
445 git rebase -f -r HEAD^ &&
446 test_cmp_rev ! HEAD^2 khnum &&
447 test_cmp_graph HEAD^.. <<-\EOF &&
448 * Merge branch '\''khnum'\'' into asherah
454 git rebase --rebase-merges=rebase-cousins HEAD^ &&
455 test_cmp_graph HEAD^.. <<-\EOF
456 * Merge branch '\''khnum'\'' into asherah
464 test_expect_success
'labels that are object IDs are rewritten' '
465 git checkout --detach B &&
467 third=$(git rev-parse HEAD) &&
468 git checkout -b labels main &&
469 git merge --no-commit $third &&
471 git commit -m "Merge commit '\''$third'\'' into labels" &&
472 echo noop >script-from-scratch &&
473 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
475 git rebase -i -r A &&
476 grep "^label $third-" .git/ORIGINAL-TODO &&
477 ! grep "^label $third$" .git/ORIGINAL-TODO
480 test_expect_success
'octopus merges' '
481 git checkout -b three &&
482 test_commit before-octopus &&
484 git checkout -b two HEAD^ &&
486 git checkout -b one HEAD^ &&
489 (GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="hank@sea.world" \
490 git merge -m "Tüntenfüsch" two three) &&
492 : fast forward if possible &&
493 before="$(git rev-parse --verify HEAD)" &&
495 git rebase -i -r HEAD^^ &&
496 test_cmp_rev HEAD $before &&
499 git rebase -i --force-rebase -r HEAD^^ &&
500 test "Hank" = "$(git show -s --format=%an HEAD)" &&
501 test "$before" != $(git rev-parse HEAD) &&
502 test_cmp_graph HEAD^^.. <<-\EOF
514 test_expect_success
'with --autosquash and --exec' '
515 git checkout -b with-exec H &&
518 git commit --fixup B B.t &&
519 write_script show.sh <<-\EOF &&
520 subject="$(git show -s --format=%s HEAD)"
521 content="$(git diff HEAD^ HEAD | tail -n 1)"
522 echo "$subject: $content"
525 git rebase -ir --autosquash --exec ./show.sh A >actual &&
526 grep "B: +Booh" actual &&
527 grep "E: +Booh" actual &&
531 test_expect_success
'--continue after resolving conflicts after a merge' '
532 git checkout -b already-has-g E &&
533 git cherry-pick E..G &&
536 git checkout -b conflicts-in-merge H &&
537 test_commit H2 H2.t conflicts H2-conflict &&
538 test_must_fail git rebase -r already-has-g &&
539 grep conflicts H2.t &&
540 echo resolved >H2.t &&
542 git rebase --continue &&
543 test_must_fail git rev-parse --verify HEAD^2 &&
544 test_path_is_missing .git/MERGE_HEAD
547 test_expect_success
'--rebase-merges with strategies' '
548 git checkout -b with-a-strategy F &&
550 git merge -m "Merge conflicting-G" conflicting-G &&
552 : first, test with a merge strategy option &&
553 git rebase -ir -Xtheirs G &&
554 echo conflicting-G >expect &&
555 test_cmp expect G.t &&
557 : now, try with a merge strategy other than recursive &&
558 git reset --hard @{1} &&
559 write_script git-merge-override <<-\EOF &&
560 echo overridden$1 >>G.t
563 PATH="$PWD:$PATH" git rebase -ir -s override -Xxopt G &&
564 test_write_lines G overridden--xopt >expect &&
568 test_expect_success
'--rebase-merges with commit that can generate bad characters for filename' '
569 git checkout -b colon-in-label E &&
570 git merge -m "colon: this should work" G &&
571 git rebase --rebase-merges --force-rebase E
574 test_expect_success
'--rebase-merges with message matched with onto label' '
575 git checkout -b onto-label E &&
576 git merge -m onto G &&
577 git rebase --rebase-merges --force-rebase E &&
578 test_cmp_graph <<-\EOF
595 test_expect_success
'progress shows the correct total' '
596 git checkout -b progress H &&
597 git rebase --rebase-merges --force-rebase --verbose A 2> err &&
598 # Expecting "Rebasing (N/14)" here, no bogus total number
599 grep "^Rebasing.*/14.$" err >progress &&
600 test_line_count = 14 progress
603 test_expect_success
'truncate label names' '
604 commit=$(git commit-tree -p HEAD^ -p HEAD -m "0123456789 我 123" HEAD^{tree}) &&
605 git merge --ff-only $commit &&
607 done="$(git rev-parse --git-path rebase-merge/done)" &&
608 git -c rebase.maxLabelLength=14 rebase --rebase-merges -x "cp \"$done\" out" --root &&
609 grep "label 0123456789-我$" out &&
610 git -c rebase.maxLabelLength=13 rebase --rebase-merges -x "cp \"$done\" out" --root &&
611 grep "label 0123456789-$" out