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 git checkout master &&
43 git merge --no-commit B &&
47 git checkout -b second C &&
50 git checkout master &&
51 git merge --no-commit G &&
56 test_commit conflicting-G G.t
59 test_expect_success
'create completely different structure' '
60 cat >script-from-scratch <<-\EOF &&
75 merge onebranch # Merge the topic branch '\''onebranch'\''
77 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
79 git rebase -i -r A master &&
80 test_cmp_graph <<-\EOF
81 * Merge the topic branch '\''onebranch'\''
95 test_expect_success
'generate correct todo list' '
96 cat >expect <<-\EOF &&
110 reset branch-point # C
112 merge -C 2051b56 E # E
113 merge -C 233d48a H # H
117 grep -v "^#" <.git/ORIGINAL-TODO >output &&
118 test_cmp expect output
121 test_expect_success
'`reset` refuses to overwrite untracked files' '
122 git checkout -b refuse-to-reset &&
123 test_commit dont-overwrite-untracked &&
124 git checkout @{-1} &&
125 : >dont-overwrite-untracked.t &&
126 echo "reset refs/tags/dont-overwrite-untracked" >script-from-scratch &&
127 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
128 test_must_fail git rebase -ir HEAD &&
132 test_expect_success
'failed `merge -C` writes patch (may be rescheduled, too)' '
133 test_when_finished "test_might_fail git rebase --abort" &&
134 git checkout -b conflicting-merge A &&
136 : fail because of conflicting untracked file &&
138 echo "merge -C H G" >script-from-scratch &&
139 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
141 test_must_fail git rebase -ir HEAD &&
142 grep "^merge -C .* G$" .git/rebase-merge/done &&
143 grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
144 test_path_is_file .git/rebase-merge/patch &&
146 : fail because of merge conflict &&
147 rm G.t .git/rebase-merge/patch &&
148 git reset --hard conflicting-G &&
149 test_must_fail git rebase --continue &&
150 ! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
151 test_path_is_file .git/rebase-merge/patch
155 test_expect_success
'failed `merge <branch>` does not crash' '
156 test_when_finished "test_might_fail git rebase --abort" &&
157 git checkout conflicting-G &&
159 echo "merge G" >script-from-scratch &&
160 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
162 test_must_fail git rebase -ir HEAD &&
163 ! grep "^merge G$" .git/rebase-merge/git-rebase-todo &&
164 grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
167 test_expect_success
'fast-forward merge -c still rewords' '
168 git checkout -b fast-forward-merge-c H &&
171 FAKE_COMMIT_MESSAGE=edited \
172 GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
175 echo edited >expected &&
176 git log --pretty=format:%B -1 >actual &&
177 test_cmp expected actual
180 test_expect_success
'with a branch tip that was cherry-picked already' '
181 git checkout -b already-upstream master &&
182 base="$(git rev-parse --verify HEAD)" &&
186 git reset --hard $base &&
189 git merge -m "Merge branch A" A2 &&
191 git checkout -b upstream-with-a2 $base &&
193 git cherry-pick A2 &&
195 git checkout already-upstream &&
197 git rebase -i -r upstream-with-a2 &&
198 test_cmp_graph upstream-with-a2.. <<-\EOF
208 test_expect_success
'do not rebase cousins unless asked for' '
209 git checkout -b cousins master &&
210 before="$(git rev-parse --verify HEAD)" &&
212 git rebase -r HEAD^ &&
213 test_cmp_rev HEAD $before &&
215 git rebase --rebase-merges=rebase-cousins HEAD^ &&
216 test_cmp_graph HEAD^.. <<-\EOF
217 * Merge the topic branch '\''onebranch'\''
226 test_expect_success
'refs/rewritten/* is worktree-local' '
227 git worktree add wt &&
228 cat >wt/script-from-scratch <<-\EOF &&
230 exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
231 exec git rev-parse --verify refs/rewritten/xyz >b
234 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
235 git -C wt rebase -i HEAD &&
236 test_must_be_empty wt/a &&
237 test_cmp_rev HEAD "$(cat wt/b)"
240 test_expect_success
'post-rewrite hook and fixups work for merges' '
241 git checkout -b post-rewrite &&
243 git reset --hard HEAD^ &&
245 git merge -m "to fix up" same1 &&
246 echo same old same old >same2.t &&
248 git commit --fixup HEAD same2.t &&
249 fixup="$(git rev-parse HEAD)" &&
251 mkdir -p .git/hooks &&
252 test_when_finished "rm .git/hooks/post-rewrite" &&
253 echo "cat >actual" | write_script .git/hooks/post-rewrite &&
256 git rebase -i --autosquash -r HEAD^^^ &&
257 printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
262 test_cmp expect actual
265 test_expect_success
'refuse to merge ancestors of HEAD' '
266 echo "merge HEAD^" >script-from-scratch &&
267 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
268 before="$(git rev-parse HEAD)" &&
269 git rebase -i HEAD &&
270 test_cmp_rev HEAD $before
273 test_expect_success
'root commits' '
274 git checkout --orphan unrelated &&
275 (GIT_AUTHOR_NAME="Parsnip" GIT_AUTHOR_EMAIL="root@example.com" \
276 test_commit second-root) &&
277 test_commit third-root &&
278 cat >script-from-scratch <<-\EOF &&
283 merge first-branch # Merge the 3rd root
285 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
287 git rebase -i --force-rebase --root -r &&
288 test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
289 test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
290 test $(git rev-parse second-root:second-root.t) = \
291 $(git rev-parse HEAD^:second-root.t) &&
292 test_cmp_graph HEAD <<-\EOF &&
299 : fast forward if possible &&
300 before="$(git rev-parse --verify HEAD)" &&
301 test_might_fail git config --unset sequence.editor &&
303 git rebase -i --root -r &&
304 test_cmp_rev HEAD $before
307 test_expect_success
'a "merge" into a root commit is a fast-forward' '
308 head=$(git rev-parse HEAD) &&
309 cat >script-from-scratch <<-EOF &&
313 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
315 git rebase -i -r HEAD^ &&
316 test_cmp_rev HEAD $head
319 test_expect_success
'A root commit can be a cousin, treat it that way' '
320 git checkout --orphan khnum &&
322 git checkout -b asherah master &&
323 test_commit shamkat &&
324 git merge --allow-unrelated-histories khnum &&
326 git rebase -f -r HEAD^ &&
327 ! test_cmp_rev HEAD^2 khnum &&
328 test_cmp_graph HEAD^.. <<-\EOF &&
329 * Merge branch '\''khnum'\'' into asherah
335 git rebase --rebase-merges=rebase-cousins HEAD^ &&
336 test_cmp_graph HEAD^.. <<-\EOF
337 * Merge branch '\''khnum'\'' into asherah
345 test_expect_success
'labels that are object IDs are rewritten' '
346 git checkout -b third B &&
348 third=$(git rev-parse HEAD) &&
349 git checkout -b labels master &&
350 git merge --no-commit third &&
352 git commit -m "Merge commit '\''$third'\'' into labels" &&
353 echo noop >script-from-scratch &&
354 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
356 git rebase -i -r A &&
357 grep "^label $third-" .git/ORIGINAL-TODO &&
358 ! grep "^label $third$" .git/ORIGINAL-TODO
361 test_expect_success
'octopus merges' '
362 git checkout -b three &&
363 test_commit before-octopus &&
365 git checkout -b two HEAD^ &&
367 git checkout -b one HEAD^ &&
370 (GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="hank@sea.world" \
371 git merge -m "Tüntenfüsch" two three) &&
373 : fast forward if possible &&
374 before="$(git rev-parse --verify HEAD)" &&
376 git rebase -i -r HEAD^^ &&
377 test_cmp_rev HEAD $before &&
380 git rebase -i --force-rebase -r HEAD^^ &&
381 test "Hank" = "$(git show -s --format=%an HEAD)" &&
382 test "$before" != $(git rev-parse HEAD) &&
383 test_cmp_graph HEAD^^.. <<-\EOF
395 test_expect_success
'with --autosquash and --exec' '
396 git checkout -b with-exec H &&
399 git commit --fixup B B.t &&
400 write_script show.sh <<-\EOF &&
401 subject="$(git show -s --format=%s HEAD)"
402 content="$(git diff HEAD^! | tail -n 1)"
403 echo "$subject: $content"
406 git rebase -ir --autosquash --exec ./show.sh A >actual &&
407 grep "B: +Booh" actual &&
408 grep "E: +Booh" actual &&
412 test_expect_success
'--continue after resolving conflicts after a merge' '
413 git checkout -b already-has-g E &&
414 git cherry-pick E..G &&
417 git checkout -b conflicts-in-merge H &&
418 test_commit H2 H2.t conflicts H2-conflict &&
419 test_must_fail git rebase -r already-has-g &&
420 grep conflicts H2.t &&
421 echo resolved >H2.t &&
423 git rebase --continue &&
424 test_must_fail git rev-parse --verify HEAD^2 &&
425 test_path_is_missing .git/MERGE_HEAD