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)
20 .
"$TEST_DIRECTORY"/lib-rebase.sh
24 git log
--graph --boundary --format=%s
"$@" >output
&&
25 sed "s/ *$//" <output
>output.trimmed
&&
26 test_cmp expect output.trimmed
29 test_expect_success
'setup' '
30 write_script replace-editor.sh <<-\EOF &&
31 mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
32 cp script-from-scratch "$1"
36 git checkout -b first &&
38 git checkout master &&
41 git merge --no-commit B &&
45 git checkout -b second C &&
48 git checkout master &&
49 git merge --no-commit G &&
55 test_expect_success
'create completely different structure' '
56 cat >script-from-scratch <<-\EOF &&
71 merge onebranch # Merge the topic branch '\''onebranch'\''
73 cp script-from-scratch script-from-scratch-orig &&
74 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
77 test_cmp_graph <<-\EOF
78 * Merge the topic branch '\''onebranch'\''
92 test_expect_success
'generate correct todo list' '
93 cat >expect <<-\EOF &&
107 reset branch-point # C
109 merge -C 2051b56 E # E
110 merge -C 233d48a H # H
114 grep -v "^#" <.git/ORIGINAL-TODO >output &&
115 test_cmp expect output
118 test_expect_success
'`reset` refuses to overwrite untracked files' '
119 git checkout -b refuse-to-reset &&
120 test_commit dont-overwrite-untracked &&
121 git checkout @{-1} &&
122 : >dont-overwrite-untracked.t &&
123 echo "reset refs/tags/dont-overwrite-untracked" >script-from-scratch &&
124 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
125 test_must_fail git rebase -r HEAD &&
129 test_expect_success
'failed `merge` writes patch (may be rescheduled, too)' '
130 test_when_finished "test_might_fail git rebase --abort" &&
131 git checkout -b conflicting-merge A &&
133 : fail because of conflicting untracked file &&
135 echo "merge -C H G" >script-from-scratch &&
136 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
138 test_must_fail git rebase -ir HEAD &&
139 grep "^merge -C .* G$" .git/rebase-merge/done &&
140 grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
141 test_path_is_file .git/rebase-merge/patch &&
143 : fail because of merge conflict &&
144 rm G.t .git/rebase-merge/patch &&
146 test_commit conflicting-G G.t not-G conflicting-G &&
147 test_must_fail git rebase --continue &&
148 ! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
149 test_path_is_file .git/rebase-merge/patch
152 test_expect_success
'with a branch tip that was cherry-picked already' '
153 git checkout -b already-upstream master &&
154 base="$(git rev-parse --verify HEAD)" &&
158 git reset --hard $base &&
161 git merge -m "Merge branch A" A2 &&
163 git checkout -b upstream-with-a2 $base &&
165 git cherry-pick A2 &&
167 git checkout already-upstream &&
169 git rebase -i -r upstream-with-a2 &&
170 test_cmp_graph upstream-with-a2.. <<-\EOF
180 test_expect_success
'do not rebase cousins unless asked for' '
181 git checkout -b cousins master &&
182 before="$(git rev-parse --verify HEAD)" &&
184 git rebase -r HEAD^ &&
185 test_cmp_rev HEAD $before &&
187 git rebase --rebase-merges=rebase-cousins HEAD^ &&
188 test_cmp_graph HEAD^.. <<-\EOF
189 * Merge the topic branch '\''onebranch'\''
198 test_expect_success
'refs/rewritten/* is worktree-local' '
199 git worktree add wt &&
200 cat >wt/script-from-scratch <<-\EOF &&
202 exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
203 exec git rev-parse --verify refs/rewritten/xyz >b
206 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
207 git -C wt rebase -i HEAD &&
208 test_must_be_empty wt/a &&
209 test_cmp_rev HEAD "$(cat wt/b)"
212 test_expect_success
'post-rewrite hook and fixups work for merges' '
213 git checkout -b post-rewrite &&
215 git reset --hard HEAD^ &&
217 git merge -m "to fix up" same1 &&
218 echo same old same old >same2.t &&
220 git commit --fixup HEAD same2.t &&
221 fixup="$(git rev-parse HEAD)" &&
223 mkdir -p .git/hooks &&
224 test_when_finished "rm .git/hooks/post-rewrite" &&
225 echo "cat >actual" | write_script .git/hooks/post-rewrite &&
228 git rebase -i --autosquash -r HEAD^^^ &&
229 printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
234 test_cmp expect actual
237 test_expect_success
'refuse to merge ancestors of HEAD' '
238 echo "merge HEAD^" >script-from-scratch &&
239 test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
240 before="$(git rev-parse HEAD)" &&
241 git rebase -i HEAD &&
242 test_cmp_rev HEAD $before
245 test_expect_success
'root commits' '
246 git checkout --orphan unrelated &&
247 (GIT_AUTHOR_NAME="Parsnip" GIT_AUTHOR_EMAIL="root@example.com" \
248 test_commit second-root) &&
249 test_commit third-root &&
250 cat >script-from-scratch <<-\EOF &&
255 merge first-branch # Merge the 3rd root
257 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
259 git rebase -i --force --root -r &&
260 test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
261 test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
262 test $(git rev-parse second-root:second-root.t) = \
263 $(git rev-parse HEAD^:second-root.t) &&
264 test_cmp_graph HEAD <<-\EOF &&
271 : fast forward if possible &&
272 before="$(git rev-parse --verify HEAD)" &&
273 test_might_fail git config --unset sequence.editor &&
275 git rebase -i --root -r &&
276 test_cmp_rev HEAD $before
279 test_expect_success
'a "merge" into a root commit is a fast-forward' '
280 head=$(git rev-parse HEAD) &&
281 cat >script-from-scratch <<-EOF &&
285 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
287 git rebase -i -r HEAD^ &&
288 test_cmp_rev HEAD $head
291 test_expect_success
'A root commit can be a cousin, treat it that way' '
292 git checkout --orphan khnum &&
294 git checkout -b asherah master &&
295 test_commit shamkat &&
296 git merge --allow-unrelated-histories khnum &&
298 git rebase -f -r HEAD^ &&
299 ! test_cmp_rev HEAD^2 khnum &&
300 test_cmp_graph HEAD^.. <<-\EOF &&
301 * Merge branch '\''khnum'\'' into asherah
307 git rebase --rebase-merges=rebase-cousins HEAD^ &&
308 test_cmp_graph HEAD^.. <<-\EOF
309 * Merge branch '\''khnum'\'' into asherah
317 test_expect_success
'labels that are object IDs are rewritten' '
318 git checkout -b third B &&
321 third=$(git rev-parse HEAD) &&
322 git checkout -b labels master &&
323 git merge --no-commit third &&
325 git commit -m "Merge commit '\''$third'\'' into labels" &&
326 cp script-from-scratch-orig script-from-scratch &&
327 test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
329 git rebase -i -r A &&
330 ! grep "^label $third$" .git/ORIGINAL-TODO