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