1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E. Schindelin
4#
5
6test_description='git rebase interactive
7
8This test runs git rebase "interactively", by faking an edit, and verifies
9that the result still makes sense.
10'
11. ./test-lib.sh
12
13. ../lib-rebase.sh
14
15set_fake_editor
16
17# set up two branches like this:
18#
19# A - B - C - D - E
20# \
21# F - G - H
22# \
23# I
24#
25# where B, D and G touch the same file.
26
27test_expect_success 'setup' '
28 : > file1 &&
29 git add file1 &&
30 test_tick &&
31 git commit -m A &&
32 git tag A &&
33 echo 1 > file1 &&
34 test_tick &&
35 git commit -m B file1 &&
36 : > file2 &&
37 git add file2 &&
38 test_tick &&
39 git commit -m C &&
40 echo 2 > file1 &&
41 test_tick &&
42 git commit -m D file1 &&
43 : > file3 &&
44 git add file3 &&
45 test_tick &&
46 git commit -m E &&
47 git checkout -b branch1 A &&
48 : > file4 &&
49 git add file4 &&
50 test_tick &&
51 git commit -m F &&
52 git tag F &&
53 echo 3 > file1 &&
54 test_tick &&
55 git commit -m G file1 &&
56 : > file5 &&
57 git add file5 &&
58 test_tick &&
59 git commit -m H &&
60 git checkout -b branch2 F &&
61 : > file6 &&
62 git add file6 &&
63 test_tick &&
64 git commit -m I &&
65 git tag I
66'
67
68test_expect_success 'no changes are a nop' '
69 git rebase -i F &&
70 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71 test $(git rev-parse I) = $(git rev-parse HEAD)
72'
73
74test_expect_success 'test the [branch] option' '
75 git checkout -b dead-end &&
76 git rm file6 &&
77 git commit -m "stop here" &&
78 git rebase -i F branch2 &&
79 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
80 test $(git rev-parse I) = $(git rev-parse branch2) &&
81 test $(git rev-parse I) = $(git rev-parse HEAD)
82'
83
84test_expect_success 'test --onto <branch>' '
85 git checkout -b test-onto branch2 &&
86 git rebase -i --onto branch1 F &&
87 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
88 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
89 test $(git rev-parse I) = $(git rev-parse branch2)
90'
91
92test_expect_success 'rebase on top of a non-conflicting commit' '
93 git checkout branch1 &&
94 git tag original-branch1 &&
95 git rebase -i branch2 &&
96 test file6 = $(git diff --name-only original-branch1) &&
97 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
98 test $(git rev-parse I) = $(git rev-parse branch2) &&
99 test $(git rev-parse I) = $(git rev-parse HEAD~2)
100'
101
102test_expect_success 'reflog for the branch shows state before rebase' '
103 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
104'
105
106test_expect_success 'exchange two commits' '
107 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
108 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
109 test G = $(git cat-file commit HEAD | sed -ne \$p)
110'
111
112cat > expect << EOF
113diff --git a/file1 b/file1
114index e69de29..00750ed 100644
115--- a/file1
116+++ b/file1
117@@ -0,0 +1 @@
118+3
119EOF
120
121cat > expect2 << EOF
122<<<<<<< HEAD
1232
124=======
1253
126>>>>>>> b7ca976... G
127EOF
128
129test_expect_success 'stop on conflicting pick' '
130 git tag new-branch1 &&
131 test_must_fail git rebase -i master &&
132 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
133 test_cmp expect .git/rebase-merge/patch &&
134 test_cmp expect2 file1 &&
135 test "$(git diff --name-status |
136 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
137 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
138 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
139'
140
141test_expect_success 'abort' '
142 git rebase --abort &&
143 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
144 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
145 ! test -d .git/rebase-merge
146'
147
148test_expect_success 'retain authorship' '
149 echo A > file7 &&
150 git add file7 &&
151 test_tick &&
152 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
153 git tag twerp &&
154 git rebase -i --onto master HEAD^ &&
155 git show HEAD | grep "^Author: Twerp Snog"
156'
157
158test_expect_success 'squash' '
159 git reset --hard twerp &&
160 echo B > file7 &&
161 test_tick &&
162 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
163 echo "******************************" &&
164 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
165 test B = $(cat file7) &&
166 test $(git rev-parse HEAD^) = $(git rev-parse master)
167'
168
169test_expect_success 'retain authorship when squashing' '
170 git show HEAD | grep "^Author: Twerp Snog"
171'
172
173test_expect_success '-p handles "no changes" gracefully' '
174 HEAD=$(git rev-parse HEAD) &&
175 git rebase -i -p HEAD^ &&
176 git update-index --refresh &&
177 git diff-files --quiet &&
178 git diff-index --quiet --cached HEAD -- &&
179 test $HEAD = $(git rev-parse HEAD)
180'
181
182test_expect_success 'preserve merges with -p' '
183 git checkout -b to-be-preserved master^ &&
184 : > unrelated-file &&
185 git add unrelated-file &&
186 test_tick &&
187 git commit -m "unrelated" &&
188 git checkout -b another-branch master &&
189 echo B > file1 &&
190 test_tick &&
191 git commit -m J file1 &&
192 test_tick &&
193 git merge to-be-preserved &&
194 echo C > file1 &&
195 test_tick &&
196 git commit -m K file1 &&
197 echo D > file1 &&
198 test_tick &&
199 git commit -m L1 file1 &&
200 git checkout HEAD^ &&
201 echo 1 > unrelated-file &&
202 test_tick &&
203 git commit -m L2 unrelated-file &&
204 test_tick &&
205 git merge another-branch &&
206 echo E > file1 &&
207 test_tick &&
208 git commit -m M file1 &&
209 git checkout -b to-be-rebased &&
210 test_tick &&
211 git rebase -i -p --onto branch1 master &&
212 git update-index --refresh &&
213 git diff-files --quiet &&
214 git diff-index --quiet --cached HEAD -- &&
215 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
216 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
217 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
218 test $(git show HEAD~5:file1) = B &&
219 test $(git show HEAD~3:file1) = C &&
220 test $(git show HEAD:file1) = E &&
221 test $(git show HEAD:unrelated-file) = 1
222'
223
224test_expect_success 'edit ancestor with -p' '
225 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
226 echo 2 > unrelated-file &&
227 test_tick &&
228 git commit -m L2-modified --amend unrelated-file &&
229 git rebase --continue &&
230 git update-index --refresh &&
231 git diff-files --quiet &&
232 git diff-index --quiet --cached HEAD -- &&
233 test $(git show HEAD:unrelated-file) = 2
234'
235
236test_expect_success '--continue tries to commit' '
237 test_tick &&
238 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
239 echo resolved > file1 &&
240 git add file1 &&
241 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
242 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
243 git show HEAD | grep chouette
244'
245
246test_expect_success 'verbose flag is heeded, even after --continue' '
247 git reset --hard HEAD@{1} &&
248 test_tick &&
249 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
250 echo resolved > file1 &&
251 git add file1 &&
252 git rebase --continue > output &&
253 grep "^ file1 | 2 +-$" output
254'
255
256test_expect_success 'multi-squash only fires up editor once' '
257 base=$(git rev-parse HEAD~4) &&
258 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
259 git rebase -i $base &&
260 test $base = $(git rev-parse HEAD^) &&
261 test 1 = $(git show | grep ONCE | wc -l)
262'
263
264test_expect_success 'squash works as expected' '
265 for n in one two three four
266 do
267 echo $n >> file$n &&
268 git add file$n &&
269 git commit -m $n
270 done &&
271 one=$(git rev-parse HEAD~3) &&
272 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
273 test $one = $(git rev-parse HEAD~2)
274'
275
276test_expect_success 'interrupted squash works as expected' '
277 for n in one two three four
278 do
279 echo $n >> conflict &&
280 git add conflict &&
281 git commit -m $n
282 done &&
283 one=$(git rev-parse HEAD~3) &&
284 (
285 FAKE_LINES="1 squash 3 2" &&
286 export FAKE_LINES &&
287 test_must_fail git rebase -i HEAD~3
288 ) &&
289 (echo one; echo two; echo four) > conflict &&
290 git add conflict &&
291 test_must_fail git rebase --continue &&
292 echo resolved > conflict &&
293 git add conflict &&
294 git rebase --continue &&
295 test $one = $(git rev-parse HEAD~2)
296'
297
298test_expect_success 'interrupted squash works as expected (case 2)' '
299 for n in one two three four
300 do
301 echo $n >> conflict &&
302 git add conflict &&
303 git commit -m $n
304 done &&
305 one=$(git rev-parse HEAD~3) &&
306 (
307 FAKE_LINES="3 squash 1 2" &&
308 export FAKE_LINES &&
309 test_must_fail git rebase -i HEAD~3
310 ) &&
311 (echo one; echo four) > conflict &&
312 git add conflict &&
313 test_must_fail git rebase --continue &&
314 (echo one; echo two; echo four) > conflict &&
315 git add conflict &&
316 test_must_fail git rebase --continue &&
317 echo resolved > conflict &&
318 git add conflict &&
319 git rebase --continue &&
320 test $one = $(git rev-parse HEAD~2)
321'
322
323test_expect_success 'ignore patch if in upstream' '
324 HEAD=$(git rev-parse HEAD) &&
325 git checkout -b has-cherry-picked HEAD^ &&
326 echo unrelated > file7 &&
327 git add file7 &&
328 test_tick &&
329 git commit -m "unrelated change" &&
330 git cherry-pick $HEAD &&
331 EXPECT_COUNT=1 git rebase -i $HEAD &&
332 test $HEAD = $(git rev-parse HEAD^)
333'
334
335test_expect_success '--continue tries to commit, even for "edit"' '
336 parent=$(git rev-parse HEAD^) &&
337 test_tick &&
338 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
339 echo edited > file7 &&
340 git add file7 &&
341 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
342 test edited = $(git show HEAD:file7) &&
343 git show HEAD | grep chouette &&
344 test $parent = $(git rev-parse HEAD^)
345'
346
347test_expect_success 'aborted --continue does not squash commits after "edit"' '
348 old=$(git rev-parse HEAD) &&
349 test_tick &&
350 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
351 echo "edited again" > file7 &&
352 git add file7 &&
353 (
354 FAKE_COMMIT_MESSAGE=" " &&
355 export FAKE_COMMIT_MESSAGE &&
356 test_must_fail git rebase --continue
357 ) &&
358 test $old = $(git rev-parse HEAD) &&
359 git rebase --abort
360'
361
362test_expect_success 'auto-amend only edited commits after "edit"' '
363 test_tick &&
364 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
365 echo "edited again" > file7 &&
366 git add file7 &&
367 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
368 echo "and again" > file7 &&
369 git add file7 &&
370 test_tick &&
371 (
372 FAKE_COMMIT_MESSAGE="and again" &&
373 export FAKE_COMMIT_MESSAGE &&
374 test_must_fail git rebase --continue
375 ) &&
376 git rebase --abort
377'
378
379test_expect_success 'rebase a detached HEAD' '
380 grandparent=$(git rev-parse HEAD~2) &&
381 git checkout $(git rev-parse HEAD) &&
382 test_tick &&
383 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
384 test $grandparent = $(git rev-parse HEAD~2)
385'
386
387test_expect_success 'rebase a commit violating pre-commit' '
388
389 mkdir -p .git/hooks &&
390 PRE_COMMIT=.git/hooks/pre-commit &&
391 echo "#!/bin/sh" > $PRE_COMMIT &&
392 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
393 chmod a+x $PRE_COMMIT &&
394 echo "monde! " >> file1 &&
395 test_tick &&
396 test_must_fail git commit -m doesnt-verify file1 &&
397 git commit -m doesnt-verify --no-verify file1 &&
398 test_tick &&
399 FAKE_LINES=2 git rebase -i HEAD~2
400
401'
402
403test_expect_success 'rebase with a file named HEAD in worktree' '
404
405 rm -fr .git/hooks &&
406 git reset --hard &&
407 git checkout -b branch3 A &&
408
409 (
410 GIT_AUTHOR_NAME="Squashed Away" &&
411 export GIT_AUTHOR_NAME &&
412 >HEAD &&
413 git add HEAD &&
414 git commit -m "Add head" &&
415 >BODY &&
416 git add BODY &&
417 git commit -m "Add body"
418 ) &&
419
420 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
421 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
422
423'
424
425test_expect_success 'do "noop" when there is nothing to cherry-pick' '
426
427 git checkout -b branch4 HEAD &&
428 GIT_EDITOR=: git commit --amend \
429 --author="Somebody else <somebody@else.com>"
430 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
431 git rebase -i branch3 &&
432 test $(git rev-parse branch3) = $(git rev-parse branch4)
433
434'
435
436test_expect_success 'submodule rebase setup' '
437 git checkout A &&
438 mkdir sub &&
439 (
440 cd sub && git init && >elif &&
441 git add elif && git commit -m "submodule initial"
442 ) &&
443 echo 1 >file1 &&
444 git add file1 sub
445 test_tick &&
446 git commit -m "One" &&
447 echo 2 >file1 &&
448 test_tick &&
449 git commit -a -m "Two" &&
450 (
451 cd sub && echo 3 >elif &&
452 git commit -a -m "submodule second"
453 ) &&
454 test_tick &&
455 git commit -a -m "Three changes submodule"
456'
457
458test_expect_success 'submodule rebase -i' '
459 FAKE_LINES="1 squash 2 3" git rebase -i A
460'
461
462test_expect_success 'avoid unnecessary reset' '
463 git checkout master &&
464 test-chmtime =123456789 file3 &&
465 git update-index --refresh &&
466 HEAD=$(git rev-parse HEAD) &&
467 git rebase -i HEAD~4 &&
468 test $HEAD = $(git rev-parse HEAD) &&
469 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
470 test 123456789 = $MTIME
471'
472
473test_done