3 # Copyright (c) 2007 Johannes E. Schindelin
6 test_description
='git rebase interactive
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
13 # set up two branches like this:
21 # where B, D and G touch the same file.
23 test_expect_success
'setup' '
31 git commit -m B file1 &&
38 git commit -m D file1 &&
43 git checkout -b branch1 A &&
51 git commit -m G file1 &&
56 git checkout -b branch2 F &&
64 echo "#!$SHELL_PATH" >fake-editor.sh
65 cat >> fake-editor.sh
<<\EOF
68 test -z "$FAKE_COMMIT_MESSAGE" ||
echo "$FAKE_COMMIT_MESSAGE" > "$1"
69 test -z "$FAKE_COMMIT_AMEND" ||
echo "$FAKE_COMMIT_AMEND" >> "$1"
73 test -z "$EXPECT_COUNT" ||
74 test "$EXPECT_COUNT" = $
(sed -e '/^#/d' -e '/^$/d' < "$1" |
wc -l) ||
76 test -z "$FAKE_LINES" && { grep -v '^#' "$1"; exit; }
77 grep -v '^#' < "$1" > "$1".tmp
81 for line
in $FAKE_LINES; do
86 echo "mark ${line#mark}"
87 echo "mark ${line#mark}" >> "$1";;
89 echo "reset ${line#reset}"
90 echo "reset ${line#reset}" >> "$1";;
92 echo "merge ${line#merge}" |
tr / ' '
93 echo "merge ${line#merge}" |
tr / ' ' >> "$1";;
95 echo "tag ${line#tag}"
96 echo "tag ${line#tag}" >> "$1";;
98 sed -n "${line}{s/^pick/$action/; p;}" < "$1".tmp
99 sed -n "${line}{s/^pick/$action/; p;}" < "$1".tmp
>> "$1"
105 test_set_editor
"$(pwd)/fake-editor.sh"
106 chmod a
+x fake-editor.sh
108 test_expect_success
'no changes are a nop' '
110 test $(git rev-parse I) = $(git rev-parse HEAD)
113 test_expect_success
'test the [branch] option' '
114 git checkout -b dead-end &&
116 git commit -m "stop here" &&
117 git rebase -i F branch2 &&
118 test $(git rev-parse I) = $(git rev-parse HEAD)
121 test_expect_success
'rebase on top of a non-conflicting commit' '
122 git checkout branch1 &&
123 git tag original-branch1 &&
124 git rebase -i branch2 &&
125 test file6 = $(git diff --name-only original-branch1) &&
126 test $(git rev-parse I) = $(git rev-parse HEAD~2)
129 test_expect_success
'reflog for the branch shows state before rebase' '
130 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
133 test_expect_success
'exchange two commits' '
134 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
135 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
136 test G = $(git cat-file commit HEAD | sed -ne \$p)
140 diff --git a/file1 b/file1
141 index e69de29..00750ed 100644
153 >>>>>>> b7ca976... G:file1
156 test_expect_success
'stop on conflicting pick' '
157 git tag new-branch1 &&
158 ! git rebase -i master &&
159 test_cmp expect .git/.dotest-merge/patch &&
160 test_cmp expect2 file1 &&
161 test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
162 test 0 = $(grep -c "^[^#]" < .git/.dotest-merge/git-rebase-todo)
165 test_expect_success
'abort' '
166 git rebase --abort &&
167 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
168 ! test -d .git/.dotest-merge
171 test_expect_success
'retain authorship' '
175 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
177 git rebase -i --onto master HEAD^ &&
178 git show HEAD | grep "^Author: Twerp Snog"
181 test_expect_success
'squash' '
182 git reset --hard twerp &&
185 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
186 echo "******************************" &&
187 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
188 test B = $(cat file7) &&
189 test $(git rev-parse HEAD^) = $(git rev-parse master)
192 test_expect_success
'retain authorship when squashing' '
193 git show HEAD | grep "^Author: Twerp Snog"
196 test_expect_success
'-p handles "no changes" gracefully' '
197 HEAD=$(git rev-parse HEAD) &&
198 git rebase -i -p HEAD^ &&
199 test $HEAD = $(git rev-parse HEAD)
202 test_expect_success
'setting marks works' '
203 git checkout master &&
204 FAKE_LINES="mark:0 2 1 mark:42 3 edit 4" git rebase -i HEAD~4 &&
205 marks_dir=.git/refs/rebase-marks &&
206 test -d $marks_dir &&
207 test $(ls $marks_dir | wc -l) -eq 2 &&
208 test "$(git rev-parse HEAD~4)" = \
209 "$(git rev-parse refs/rebase-marks/0)" &&
210 test "$(git rev-parse HEAD~2)" = \
211 "$(git rev-parse refs/rebase-marks/42)" &&
212 git rebase --abort &&
213 test 0 = $(ls $marks_dir | wc -l)
216 test_expect_success
'reset with nonexistent mark fails' '
217 export FAKE_LINES="reset:0 1" &&
218 test_must_fail git rebase -i HEAD~1 &&
223 test_expect_success
'reset to HEAD is a nop' '
225 head=$(git rev-parse --short HEAD) &&
226 FAKE_LINES="reset$head" git rebase -i HEAD~4 &&
227 test "$(git rev-parse --short HEAD)" = "$head"
230 test_expect_success
'merge redoes merges' '
232 git merge dead-end &&
233 merge=$(git rev-parse HEAD) &&
234 git reset --hard HEAD~1 &&
235 FAKE_LINES="1 merge$merge/dead-end" git rebase -i HEAD~1 &&
236 test $merge = "$(git rev-parse HEAD)" &&
237 git reset --hard HEAD~1
240 test_expect_success
'preserve merges with -p' '
241 git checkout -b to-be-preserved master^ &&
242 : > unrelated-file &&
243 git add unrelated-file &&
245 git commit -m "unrelated" &&
246 git checkout -b to-be-rebased master &&
249 git commit -m J file1 &&
251 git merge to-be-preserved &&
254 git commit -m K file1 &&
256 git rebase -i -p --onto branch1 master &&
257 test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
258 test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
259 test $(git show HEAD:file1) = C &&
260 test $(git show HEAD~2:file1) = B
263 test_expect_success
'rebase with preserve merge forth and back is a noop' '
264 git checkout -b big-branch-1 master &&
268 git commit -m "big branch commit 1" &&
271 git commit -m "big branch commit 2" &&
274 git commit -m "big branch commit 3" &&
275 git checkout -b big-branch-2 master &&
278 git commit -m "big branch commit 4" &&
281 git commit -m "big branch commit 5" &&
282 git merge big-branch-1~1 &&
283 git merge to-be-preserved &&
284 tbp_merge=$(git rev-parse HEAD) &&
287 git commit -m "big branch commit 6" &&
288 git merge big-branch-1 &&
289 head=$(git rev-parse HEAD) &&
290 FAKE_LINES="16 6 19 20 4 1 2 5 22" \
291 git rebase -i -p --onto dead-end master &&
292 test "$head" != "$(git rev-parse HEAD)" &&
293 FAKE_LINES="3 7 mark:10 8 9 5 1 2 merge$tbp_merge~1/:10 \
294 merge$tbp_merge/to-be-preserved 6 11" \
295 git rebase -i -p --onto master dead-end &&
296 test "$head" = "$(git rev-parse HEAD)"
299 test_expect_success
'interactive --first-parent gives a linear list' '
300 head=$(git rev-parse HEAD) &&
301 EXPECT_COUNT=6 FAKE_LINES="2 1 4 3 6 5" \
302 git rebase -i -f --onto dead-end master &&
303 test "$head" != "$(git rev-parse HEAD)" &&
304 git rev-parse HEAD^^2 &&
305 test "$(git rev-parse HEAD~6)" = "$(git rev-parse dead-end)" &&
306 EXPECT_COUNT=6 FAKE_LINES="2 1 4 3 6 5" \
307 git rebase -i -f --onto master dead-end &&
308 test "$head" = "$(git rev-parse HEAD)"
311 test_expect_success
'tag sets tags' '
312 head=$(git rev-parse HEAD) &&
313 FAKE_LINES="1 2 3 4 5 tagbb-tag1 6 7 8 9 10 11 12 13 14 15 \
314 tagbb-tag2 16 tagbb-tag3a tagbb-tag3b 17 18 19 20 21 22" \
315 EXPECT_COUNT=22 git rebase -i -p master &&
316 test "$head" = "$(git rev-parse HEAD)" &&
317 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
318 "$(git rev-parse HEAD^2~2 HEAD~2 HEAD~1 HEAD~1)"
321 test_expect_success
'interactive -t preserves tags' '
322 git rebase -i -p -t --onto dead-end master &&
323 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
324 "$(git rev-parse HEAD^2~2 HEAD~2 HEAD~1 HEAD~1)" &&
325 head=$(git rev-parse HEAD) &&
326 git rebase -i -t dead-end &&
327 test "$(git rev-parse bb-tag1 bb-tag2 bb-tag3a bb-tag3b)" = \
328 "$(git rev-parse HEAD~7 $head~2 HEAD~1 HEAD~1)"
331 test_expect_success
'--continue tries to commit' '
332 git checkout to-be-rebased &&
334 ! git rebase -i --onto new-branch1 HEAD^ &&
335 echo resolved > file1 &&
337 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
338 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
339 git show HEAD | grep chouette
342 test_expect_success
'verbose flag is heeded, even after --continue' '
343 git reset --hard HEAD@{1} &&
345 ! git rebase -v -i --onto new-branch1 HEAD^ &&
346 echo resolved > file1 &&
348 git rebase --continue > output &&
349 grep "^ file1 | 2 +-$" output
352 test_expect_success
'multi-squash only fires up editor once' '
353 base=$(git rev-parse HEAD~4) &&
354 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
355 git rebase -i $base &&
356 test $base = $(git rev-parse HEAD^) &&
357 test 1 = $(git show | grep ONCE | wc -l)
360 test_expect_success
'squash works as expected' '
361 for n in one two three four
367 one=$(git rev-parse HEAD~3) &&
368 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
369 test $one = $(git rev-parse HEAD~2)
372 test_expect_success
'interrupted squash works as expected' '
373 for n in one two three four
375 echo $n >> conflict &&
379 one=$(git rev-parse HEAD~3) &&
380 ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
381 (echo one; echo two; echo four) > conflict &&
383 ! git rebase --continue &&
384 echo resolved > conflict &&
386 git rebase --continue &&
387 test $one = $(git rev-parse HEAD~2)
390 test_expect_success
'interrupted squash works as expected (case 2)' '
391 for n in one two three four
393 echo $n >> conflict &&
397 one=$(git rev-parse HEAD~3) &&
398 ! FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
399 (echo one; echo four) > conflict &&
401 ! git rebase --continue &&
402 (echo one; echo two; echo four) > conflict &&
404 ! git rebase --continue &&
405 echo resolved > conflict &&
407 git rebase --continue &&
408 test $one = $(git rev-parse HEAD~2)
411 test_expect_success
'ignore patch if in upstream' '
412 HEAD=$(git rev-parse HEAD) &&
413 git checkout -b has-cherry-picked HEAD^ &&
414 echo unrelated > file7 &&
417 git commit -m "unrelated change" &&
418 git cherry-pick $HEAD &&
419 EXPECT_COUNT=1 git rebase -i $HEAD &&
420 test $HEAD = $(git rev-parse HEAD^)
423 test_expect_success
'--continue tries to commit, even for "edit"' '
424 parent=$(git rev-parse HEAD^) &&
426 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
427 echo edited > file7 &&
429 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
430 test edited = $(git show HEAD:file7) &&
431 git show HEAD | grep chouette &&
432 test $parent = $(git rev-parse HEAD^)
435 test_expect_success
'rebase a detached HEAD' '
436 grandparent=$(git rev-parse HEAD~2) &&
437 git checkout $(git rev-parse HEAD) &&
439 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
440 test $grandparent = $(git rev-parse HEAD~2)
443 test_expect_success
'rebase a commit violating pre-commit' '
445 mkdir -p .git/hooks &&
446 PRE_COMMIT=.git/hooks/pre-commit &&
447 echo "#!/bin/sh" > $PRE_COMMIT &&
448 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
449 chmod a+x $PRE_COMMIT &&
450 echo "monde! " >> file1 &&
452 ! git commit -m doesnt-verify file1 &&
453 git commit -m doesnt-verify --no-verify file1 &&
455 FAKE_LINES=2 git rebase -i HEAD~2
459 test_expect_success
'rebase with a file named HEAD in worktree' '
463 git checkout -b branch3 A &&
466 GIT_AUTHOR_NAME="Squashed Away" &&
467 export GIT_AUTHOR_NAME &&
470 git commit -m "Add head" &&
473 git commit -m "Add body"
476 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
477 test "$(git show -s --pretty=format:%an)" = "Squashed Away"