]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3404-rebase-interactive.sh
branch.c: Relax unnecessary requirement on upstream's remote ref name
[thirdparty/git.git] / t / t3404-rebase-interactive.sh
CommitLineData
1b1dce4b
JS
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.
99b028e5
JN
10
11Initial setup:
12
13 one - two - three - four (conflict-branch)
14 /
15 A - B - C - D - E (master)
16 | \
17 | F - G - H (branch1)
18 | \
19 |\ I (branch2)
20 | \
21 | J - K - L - M (no-conflict-branch)
22 \
23 N - O - P (no-ff-branch)
24
25 where A, B, D and G all touch file1, and one, two, three, four all
26 touch file "conflict".
1b1dce4b
JS
27'
28. ./test-lib.sh
29
eaf0551d 30. "$TEST_DIRECTORY"/lib-rebase.sh
29a03348
JS
31
32set_fake_editor
33
b4995494
MB
34# WARNING: Modifications to the initial repository can change the SHA ID used
35# in the expect2 file for the 'stop on conflicting pick' test.
36
1b1dce4b 37test_expect_success 'setup' '
163f3925
MH
38 test_commit A file1 &&
39 test_commit B file1 &&
40 test_commit C file2 &&
41 test_commit D file1 &&
42 test_commit E file3 &&
1b1dce4b 43 git checkout -b branch1 A &&
163f3925
MH
44 test_commit F file4 &&
45 test_commit G file1 &&
46 test_commit H file5 &&
1b1dce4b 47 git checkout -b branch2 F &&
2dec68cf 48 test_commit I file6 &&
6c4c44c4 49 git checkout -b conflict-branch A &&
391a825f
JN
50 test_commit one conflict &&
51 test_commit two conflict &&
52 test_commit three conflict &&
53 test_commit four conflict &&
6c4c44c4 54 git checkout -b no-conflict-branch A &&
391a825f
JN
55 test_commit J fileJ &&
56 test_commit K fileK &&
57 test_commit L fileL &&
58 test_commit M fileM &&
b4995494 59 git checkout -b no-ff-branch A &&
391a825f
JN
60 test_commit N fileN &&
61 test_commit O fileO &&
62 test_commit P fileP
1b1dce4b
JS
63'
64
cd035b1c
MM
65# "exec" commands are ran with the user shell by default, but this may
66# be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
67# to create a file. Unseting SHELL avoids such non-portable behavior
5cd3e107 68# in tests. It must be exported for it to take effect where needed.
cd035b1c 69SHELL=
5cd3e107 70export SHELL
cd035b1c
MM
71
72test_expect_success 'rebase -i with the exec command' '
73 git checkout master &&
74 (
75 FAKE_LINES="1 exec_>touch-one
76 2 exec_>touch-two exec_false exec_>touch-three
77 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
78 export FAKE_LINES &&
79 test_must_fail git rebase -i A
80 ) &&
2caf20c5
MM
81 test_path_is_file touch-one &&
82 test_path_is_file touch-two &&
83 test_path_is_missing touch-three " (should have stopped before)" &&
5c947e2c 84 test_cmp_rev C HEAD &&
cd035b1c 85 git rebase --continue &&
2caf20c5
MM
86 test_path_is_file touch-three &&
87 test_path_is_file "touch-file name with spaces" &&
88 test_path_is_file touch-after-semicolon &&
5c947e2c 89 test_cmp_rev master HEAD &&
cd035b1c
MM
90 rm -f touch-*
91'
92
93test_expect_success 'rebase -i with the exec command runs from tree root' '
94 git checkout master &&
c2e0940b 95 mkdir subdir && (cd subdir &&
cd035b1c 96 FAKE_LINES="1 exec_>touch-subdir" \
c2e0940b
JL
97 git rebase -i HEAD^
98 ) &&
2caf20c5 99 test_path_is_file touch-subdir &&
cd035b1c
MM
100 rm -fr subdir
101'
102
103test_expect_success 'rebase -i with the exec command checks tree cleanness' '
104 git checkout master &&
105 (
106 FAKE_LINES="exec_echo_foo_>file1 1" &&
107 export FAKE_LINES &&
108 test_must_fail git rebase -i HEAD^
109 ) &&
5c947e2c 110 test_cmp_rev master^ HEAD &&
cd035b1c
MM
111 git reset --hard &&
112 git rebase --continue
113'
114
ecfe1ea9
JS
115test_expect_success 'rebase -i with exec of inexistent command' '
116 git checkout master &&
117 test_when_finished "git rebase --abort" &&
118 (
119 FAKE_LINES="exec_this-command-does-not-exist 1" &&
120 export FAKE_LINES &&
121 test_must_fail git rebase -i HEAD^ >actual 2>&1
122 ) &&
123 ! grep "Maybe git-rebase is broken" actual
124'
125
1b1dce4b 126test_expect_success 'no changes are a nop' '
6c4c44c4 127 git checkout branch2 &&
1b1dce4b 128 git rebase -i F &&
ab736792 129 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
1b1dce4b
JS
130 test $(git rev-parse I) = $(git rev-parse HEAD)
131'
132
c9e65892
JS
133test_expect_success 'test the [branch] option' '
134 git checkout -b dead-end &&
135 git rm file6 &&
136 git commit -m "stop here" &&
137 git rebase -i F branch2 &&
ab736792
SB
138 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
139 test $(git rev-parse I) = $(git rev-parse branch2) &&
c9e65892
JS
140 test $(git rev-parse I) = $(git rev-parse HEAD)
141'
142
ab736792
SB
143test_expect_success 'test --onto <branch>' '
144 git checkout -b test-onto branch2 &&
145 git rebase -i --onto branch1 F &&
146 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
147 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
148 test $(git rev-parse I) = $(git rev-parse branch2)
149'
150
1b1dce4b
JS
151test_expect_success 'rebase on top of a non-conflicting commit' '
152 git checkout branch1 &&
153 git tag original-branch1 &&
154 git rebase -i branch2 &&
155 test file6 = $(git diff --name-only original-branch1) &&
ab736792
SB
156 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
157 test $(git rev-parse I) = $(git rev-parse branch2) &&
1b1dce4b
JS
158 test $(git rev-parse I) = $(git rev-parse HEAD~2)
159'
160
68a163c9
JS
161test_expect_success 'reflog for the branch shows state before rebase' '
162 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
163'
164
1b1dce4b
JS
165test_expect_success 'exchange two commits' '
166 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
b4ce54fc
JK
167 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
168 test G = $(git cat-file commit HEAD | sed -ne \$p)
1b1dce4b
JS
169'
170
171cat > expect << EOF
172diff --git a/file1 b/file1
163f3925 173index f70f10e..fd79235 100644
1b1dce4b
JS
174--- a/file1
175+++ b/file1
163f3925
MH
176@@ -1 +1 @@
177-A
178+G
1b1dce4b
JS
179EOF
180
181cat > expect2 << EOF
606475f3 182<<<<<<< HEAD
163f3925 183D
1b1dce4b 184=======
163f3925 185G
b4995494 186>>>>>>> 5d18e54... G
1b1dce4b
JS
187EOF
188
189test_expect_success 'stop on conflicting pick' '
190 git tag new-branch1 &&
ab736792
SB
191 test_must_fail git rebase -i master &&
192 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
28ed6e7b 193 test_cmp expect .git/rebase-merge/patch &&
82ebb0b6 194 test_cmp expect2 file1 &&
0cb0e143 195 test "$(git diff --name-status |
ab736792 196 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
28ed6e7b
JS
197 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
198 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
1b1dce4b
JS
199'
200
201test_expect_success 'abort' '
202 git rebase --abort &&
203 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
ab736792 204 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
2caf20c5 205 test_path_is_missing .git/rebase-merge
1b1dce4b
JS
206'
207
b096374f
IWC
208test_expect_success 'abort with error when new base cannot be checked out' '
209 git rm --cached file1 &&
210 git commit -m "remove file in base" &&
211 test_must_fail git rebase -i master > output 2>&1 &&
e6c111b4 212 grep "The following untracked working tree files would be overwritten by checkout:" \
b096374f 213 output &&
e6c111b4 214 grep "file1" output &&
2caf20c5 215 test_path_is_missing .git/rebase-merge &&
b096374f
IWC
216 git reset --hard HEAD^
217'
218
1b1dce4b
JS
219test_expect_success 'retain authorship' '
220 echo A > file7 &&
221 git add file7 &&
c54b7817 222 test_tick &&
1b1dce4b
JS
223 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
224 git tag twerp &&
225 git rebase -i --onto master HEAD^ &&
226 git show HEAD | grep "^Author: Twerp Snog"
227'
228
229test_expect_success 'squash' '
230 git reset --hard twerp &&
231 echo B > file7 &&
c54b7817 232 test_tick &&
1b1dce4b
JS
233 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
234 echo "******************************" &&
5065ed29 235 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
959c0d06 236 git rebase -i --onto master HEAD~2 &&
1b1dce4b
JS
237 test B = $(cat file7) &&
238 test $(git rev-parse HEAD^) = $(git rev-parse master)
239'
240
241test_expect_success 'retain authorship when squashing' '
81ab1cb4 242 git show HEAD | grep "^Author: Twerp Snog"
1b1dce4b
JS
243'
244
34454e85
JS
245test_expect_success '-p handles "no changes" gracefully' '
246 HEAD=$(git rev-parse HEAD) &&
247 git rebase -i -p HEAD^ &&
71d9451e
TR
248 git update-index --refresh &&
249 git diff-files --quiet &&
250 git diff-index --quiet --cached HEAD -- &&
34454e85
JS
251 test $HEAD = $(git rev-parse HEAD)
252'
253
cddb42d2 254test_expect_failure 'exchange two commits with -p' '
27ccd8d1 255 git checkout H &&
cddb42d2
JN
256 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
257 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
258 test G = $(git cat-file commit HEAD | sed -ne \$p)
259'
260
f09c9b8c
JS
261test_expect_success 'preserve merges with -p' '
262 git checkout -b to-be-preserved master^ &&
263 : > unrelated-file &&
264 git add unrelated-file &&
265 test_tick &&
266 git commit -m "unrelated" &&
5352a82b 267 git checkout -b another-branch master &&
f09c9b8c
JS
268 echo B > file1 &&
269 test_tick &&
270 git commit -m J file1 &&
271 test_tick &&
272 git merge to-be-preserved &&
273 echo C > file1 &&
274 test_tick &&
275 git commit -m K file1 &&
5352a82b
SB
276 echo D > file1 &&
277 test_tick &&
278 git commit -m L1 file1 &&
279 git checkout HEAD^ &&
280 echo 1 > unrelated-file &&
281 test_tick &&
282 git commit -m L2 unrelated-file &&
283 test_tick &&
284 git merge another-branch &&
285 echo E > file1 &&
286 test_tick &&
287 git commit -m M file1 &&
288 git checkout -b to-be-rebased &&
18640d99 289 test_tick &&
f09c9b8c 290 git rebase -i -p --onto branch1 master &&
71d9451e
TR
291 git update-index --refresh &&
292 git diff-files --quiet &&
293 git diff-index --quiet --cached HEAD -- &&
5352a82b
SB
294 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
295 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
296 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
297 test $(git show HEAD~5:file1) = B &&
298 test $(git show HEAD~3:file1) = C &&
299 test $(git show HEAD:file1) = E &&
300 test $(git show HEAD:unrelated-file) = 1
f09c9b8c
JS
301'
302
a96dc01e 303test_expect_success 'edit ancestor with -p' '
12bf8283 304 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
a96dc01e
TR
305 echo 2 > unrelated-file &&
306 test_tick &&
307 git commit -m L2-modified --amend unrelated-file &&
308 git rebase --continue &&
309 git update-index --refresh &&
310 git diff-files --quiet &&
311 git diff-index --quiet --cached HEAD -- &&
312 test $(git show HEAD:unrelated-file) = 2
313'
314
18640d99
JS
315test_expect_success '--continue tries to commit' '
316 test_tick &&
ab736792 317 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
18640d99
JS
318 echo resolved > file1 &&
319 git add file1 &&
320 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
321 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
322 git show HEAD | grep chouette
323'
324
8e4a91bd 325test_expect_success 'verbose flag is heeded, even after --continue' '
53f2ffa8 326 git reset --hard master@{1} &&
8e4a91bd 327 test_tick &&
ab736792 328 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
8e4a91bd
JS
329 echo resolved > file1 &&
330 git add file1 &&
331 git rebase --continue > output &&
dc801e71 332 grep "^ file1 | 2 +-$" output
8e4a91bd
JS
333'
334
6368f3f8
JS
335test_expect_success 'multi-squash only fires up editor once' '
336 base=$(git rev-parse HEAD~4) &&
337 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
959c0d06 338 EXPECT_HEADER_COUNT=4 \
6368f3f8
JS
339 git rebase -i $base &&
340 test $base = $(git rev-parse HEAD^) &&
341 test 1 = $(git show | grep ONCE | wc -l)
342'
343
a25eb139 344test_expect_success 'multi-fixup does not fire up editor' '
0205e72f
MH
345 git checkout -b multi-fixup E &&
346 base=$(git rev-parse HEAD~4) &&
a25eb139 347 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
0205e72f
MH
348 git rebase -i $base &&
349 test $base = $(git rev-parse HEAD^) &&
a25eb139 350 test 0 = $(git show | grep NEVER | wc -l) &&
0205e72f
MH
351 git checkout to-be-rebased &&
352 git branch -D multi-fixup
353'
354
6bdcd0d2
MH
355test_expect_success 'commit message used after conflict' '
356 git checkout -b conflict-fixup conflict-branch &&
357 base=$(git rev-parse HEAD~4) &&
358 (
359 FAKE_LINES="1 fixup 3 fixup 4" &&
360 export FAKE_LINES &&
361 test_must_fail git rebase -i $base
362 ) &&
363 echo three > conflict &&
364 git add conflict &&
365 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
366 git rebase --continue &&
367 test $base = $(git rev-parse HEAD^) &&
368 test 1 = $(git show | grep ONCE | wc -l) &&
369 git checkout to-be-rebased &&
370 git branch -D conflict-fixup
371'
372
373test_expect_success 'commit message retained after conflict' '
374 git checkout -b conflict-squash conflict-branch &&
375 base=$(git rev-parse HEAD~4) &&
376 (
377 FAKE_LINES="1 fixup 3 squash 4" &&
378 export FAKE_LINES &&
379 test_must_fail git rebase -i $base
380 ) &&
381 echo three > conflict &&
382 git add conflict &&
383 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
384 git rebase --continue &&
385 test $base = $(git rev-parse HEAD^) &&
386 test 2 = $(git show | grep TWICE | wc -l) &&
387 git checkout to-be-rebased &&
388 git branch -D conflict-squash
389'
390
0205e72f
MH
391cat > expect-squash-fixup << EOF
392B
393
394D
395
396ONCE
397EOF
398
399test_expect_success 'squash and fixup generate correct log messages' '
400 git checkout -b squash-fixup E &&
401 base=$(git rev-parse HEAD~4) &&
402 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
959c0d06 403 EXPECT_HEADER_COUNT=4 \
0205e72f
MH
404 git rebase -i $base &&
405 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
406 test_cmp expect-squash-fixup actual-squash-fixup &&
407 git checkout to-be-rebased &&
408 git branch -D squash-fixup
409'
410
234b3dae
MH
411test_expect_success 'squash ignores comments' '
412 git checkout -b skip-comments E &&
413 base=$(git rev-parse HEAD~4) &&
414 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
415 EXPECT_HEADER_COUNT=4 \
416 git rebase -i $base &&
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l) &&
419 git checkout to-be-rebased &&
420 git branch -D skip-comments
421'
422
423test_expect_success 'squash ignores blank lines' '
424 git checkout -b skip-blank-lines E &&
425 base=$(git rev-parse HEAD~4) &&
426 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
427 EXPECT_HEADER_COUNT=4 \
428 git rebase -i $base &&
429 test $base = $(git rev-parse HEAD^) &&
430 test 1 = $(git show | grep ONCE | wc -l) &&
431 git checkout to-be-rebased &&
432 git branch -D skip-blank-lines
433'
434
fb47cfbd 435test_expect_success 'squash works as expected' '
6c4c44c4 436 git checkout -b squash-works no-conflict-branch &&
fb47cfbd 437 one=$(git rev-parse HEAD~3) &&
5065ed29 438 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
959c0d06 439 git rebase -i HEAD~3 &&
fb47cfbd
JS
440 test $one = $(git rev-parse HEAD~2)
441'
442
443test_expect_success 'interrupted squash works as expected' '
6c4c44c4 444 git checkout -b interrupted-squash conflict-branch &&
fb47cfbd 445 one=$(git rev-parse HEAD~3) &&
ab736792
SB
446 (
447 FAKE_LINES="1 squash 3 2" &&
448 export FAKE_LINES &&
449 test_must_fail git rebase -i HEAD~3
450 ) &&
fb47cfbd
JS
451 (echo one; echo two; echo four) > conflict &&
452 git add conflict &&
ab736792 453 test_must_fail git rebase --continue &&
fb47cfbd
JS
454 echo resolved > conflict &&
455 git add conflict &&
456 git rebase --continue &&
457 test $one = $(git rev-parse HEAD~2)
458'
459
1d25c8cf 460test_expect_success 'interrupted squash works as expected (case 2)' '
6c4c44c4 461 git checkout -b interrupted-squash2 conflict-branch &&
1d25c8cf 462 one=$(git rev-parse HEAD~3) &&
ab736792
SB
463 (
464 FAKE_LINES="3 squash 1 2" &&
465 export FAKE_LINES &&
466 test_must_fail git rebase -i HEAD~3
467 ) &&
1d25c8cf
JS
468 (echo one; echo four) > conflict &&
469 git add conflict &&
ab736792 470 test_must_fail git rebase --continue &&
1d25c8cf
JS
471 (echo one; echo two; echo four) > conflict &&
472 git add conflict &&
ab736792 473 test_must_fail git rebase --continue &&
1d25c8cf
JS
474 echo resolved > conflict &&
475 git add conflict &&
476 git rebase --continue &&
477 test $one = $(git rev-parse HEAD~2)
478'
479
96ffe892
JS
480test_expect_success 'ignore patch if in upstream' '
481 HEAD=$(git rev-parse HEAD) &&
482 git checkout -b has-cherry-picked HEAD^ &&
483 echo unrelated > file7 &&
484 git add file7 &&
485 test_tick &&
486 git commit -m "unrelated change" &&
487 git cherry-pick $HEAD &&
488 EXPECT_COUNT=1 git rebase -i $HEAD &&
489 test $HEAD = $(git rev-parse HEAD^)
490'
491
be6ff208
JS
492test_expect_success '--continue tries to commit, even for "edit"' '
493 parent=$(git rev-parse HEAD^) &&
494 test_tick &&
495 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
496 echo edited > file7 &&
497 git add file7 &&
498 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
499 test edited = $(git show HEAD:file7) &&
500 git show HEAD | grep chouette &&
501 test $parent = $(git rev-parse HEAD^)
502'
503
dc7f55cb
SB
504test_expect_success 'aborted --continue does not squash commits after "edit"' '
505 old=$(git rev-parse HEAD) &&
506 test_tick &&
507 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
508 echo "edited again" > file7 &&
509 git add file7 &&
510 (
511 FAKE_COMMIT_MESSAGE=" " &&
512 export FAKE_COMMIT_MESSAGE &&
513 test_must_fail git rebase --continue
514 ) &&
515 test $old = $(git rev-parse HEAD) &&
516 git rebase --abort
517'
518
f8aa1b69
SB
519test_expect_success 'auto-amend only edited commits after "edit"' '
520 test_tick &&
521 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
522 echo "edited again" > file7 &&
523 git add file7 &&
524 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
525 echo "and again" > file7 &&
526 git add file7 &&
527 test_tick &&
528 (
529 FAKE_COMMIT_MESSAGE="and again" &&
530 export FAKE_COMMIT_MESSAGE &&
531 test_must_fail git rebase --continue
532 ) &&
533 git rebase --abort
534'
535
ffaaed88
MM
536test_expect_success 'clean error after failed "exec"' '
537 test_tick &&
538 test_when_finished "git rebase --abort || :" &&
539 (
540 FAKE_LINES="1 exec_false" &&
541 export FAKE_LINES &&
542 test_must_fail git rebase -i HEAD^
543 ) &&
544 echo "edited again" > file7 &&
545 git add file7 &&
546 test_must_fail git rebase --continue 2>error &&
547 grep "You have staged changes in your working tree." error
548'
549
73697a0b
JS
550test_expect_success 'rebase a detached HEAD' '
551 grandparent=$(git rev-parse HEAD~2) &&
552 git checkout $(git rev-parse HEAD) &&
553 test_tick &&
554 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
555 test $grandparent = $(git rev-parse HEAD~2)
556'
557
752527f5
JS
558test_expect_success 'rebase a commit violating pre-commit' '
559
560 mkdir -p .git/hooks &&
561 PRE_COMMIT=.git/hooks/pre-commit &&
562 echo "#!/bin/sh" > $PRE_COMMIT &&
563 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
564 chmod a+x $PRE_COMMIT &&
565 echo "monde! " >> file1 &&
566 test_tick &&
ab736792 567 test_must_fail git commit -m doesnt-verify file1 &&
752527f5
JS
568 git commit -m doesnt-verify --no-verify file1 &&
569 test_tick &&
570 FAKE_LINES=2 git rebase -i HEAD~2
571
572'
573
077b725f
JH
574test_expect_success 'rebase with a file named HEAD in worktree' '
575
576 rm -fr .git/hooks &&
577 git reset --hard &&
578 git checkout -b branch3 A &&
579
580 (
581 GIT_AUTHOR_NAME="Squashed Away" &&
582 export GIT_AUTHOR_NAME &&
583 >HEAD &&
584 git add HEAD &&
585 git commit -m "Add head" &&
586 >BODY &&
587 git add BODY &&
588 git commit -m "Add body"
589 ) &&
590
591 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
592 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
593
594'
595
ff74126c
JS
596test_expect_success 'do "noop" when there is nothing to cherry-pick' '
597
598 git checkout -b branch4 HEAD &&
599 GIT_EDITOR=: git commit --amend \
2dec68cf 600 --author="Somebody else <somebody@else.com>" &&
ff74126c
JS
601 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
602 git rebase -i branch3 &&
603 test $(git rev-parse branch3) = $(git rev-parse branch4)
604
605'
606
96747696
JH
607test_expect_success 'submodule rebase setup' '
608 git checkout A &&
609 mkdir sub &&
610 (
611 cd sub && git init && >elif &&
612 git add elif && git commit -m "submodule initial"
613 ) &&
614 echo 1 >file1 &&
2dec68cf 615 git add file1 sub &&
96747696
JH
616 test_tick &&
617 git commit -m "One" &&
618 echo 2 >file1 &&
619 test_tick &&
620 git commit -a -m "Two" &&
621 (
622 cd sub && echo 3 >elif &&
623 git commit -a -m "submodule second"
624 ) &&
625 test_tick &&
626 git commit -a -m "Three changes submodule"
627'
628
94c88ede 629test_expect_success 'submodule rebase -i' '
96747696
JH
630 FAKE_LINES="1 squash 2 3" git rebase -i A
631'
632
a6754cda
JK
633test_expect_success 'submodule conflict setup' '
634 git tag submodule-base &&
635 git checkout HEAD^ &&
636 (
637 cd sub && git checkout HEAD^ && echo 4 >elif &&
638 git add elif && git commit -m "submodule conflict"
639 ) &&
640 git add sub &&
641 test_tick &&
642 git commit -m "Conflict in submodule" &&
643 git tag submodule-topic
644'
645
646test_expect_success 'rebase -i continue with only submodule staged' '
647 test_must_fail git rebase -i submodule-base &&
648 git add sub &&
649 git rebase --continue &&
650 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
651'
652
653test_expect_success 'rebase -i continue with unstaged submodule' '
654 git checkout submodule-topic &&
655 git reset --hard &&
656 test_must_fail git rebase -i submodule-base &&
657 git reset &&
658 git rebase --continue &&
659 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
660'
661
0e757e30
JS
662test_expect_success 'avoid unnecessary reset' '
663 git checkout master &&
a6754cda 664 git reset --hard &&
0e757e30
JS
665 test-chmtime =123456789 file3 &&
666 git update-index --refresh &&
667 HEAD=$(git rev-parse HEAD) &&
668 git rebase -i HEAD~4 &&
669 test $HEAD = $(git rev-parse HEAD) &&
670 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
671 test 123456789 = $MTIME
672'
673
6741aa6c
BG
674test_expect_success 'reword' '
675 git checkout -b reword-branch master &&
676 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
677 git show HEAD | grep "E changed" &&
678 test $(git rev-parse master) != $(git rev-parse HEAD) &&
679 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
680 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
681 git show HEAD^ | grep "D changed" &&
682 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
683 git show HEAD~3 | grep "B changed" &&
684 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
685 git show HEAD~2 | grep "C changed"
686'
687
eb2151bb
TR
688test_expect_success 'rebase -i can copy notes' '
689 git config notes.rewrite.rebase true &&
690 git config notes.rewriteRef "refs/notes/*" &&
691 test_commit n1 &&
692 test_commit n2 &&
693 test_commit n3 &&
694 git notes add -m"a note" n3 &&
695 git rebase --onto n1 n2 &&
696 test "a note" = "$(git notes show HEAD)"
697'
698
699cat >expect <<EOF
700an earlier note
d4990c4b 701
eb2151bb
TR
702a note
703EOF
704
705test_expect_success 'rebase -i can copy notes over a fixup' '
706 git reset --hard n3 &&
707 git notes add -m"an earlier note" n2 &&
708 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
709 git notes show > output &&
710 test_cmp expect output
711'
712
2ec33cdd
DO
713test_expect_success 'rebase while detaching HEAD' '
714 git symbolic-ref HEAD &&
715 grandparent=$(git rev-parse HEAD~2) &&
716 test_tick &&
717 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
718 test $grandparent = $(git rev-parse HEAD~2) &&
719 test_must_fail git symbolic-ref HEAD
720'
721
b4995494
MB
722test_tick # Ensure that the rebased commits get a different timestamp.
723test_expect_success 'always cherry-pick with --no-ff' '
724 git checkout no-ff-branch &&
725 git tag original-no-ff-branch &&
726 git rebase -i --no-ff A &&
727 touch empty &&
728 for p in 0 1 2
729 do
730 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
731 git diff HEAD~$p original-no-ff-branch~$p > out &&
732 test_cmp empty out
733 done &&
734 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
735 git diff HEAD~3 original-no-ff-branch~3 > out &&
736 test_cmp empty out
737'
738
57f2b6b2
JH
739test_expect_success 'set up commits with funny messages' '
740 git checkout -b funny A &&
741 echo >>file1 &&
742 test_tick &&
743 git commit -a -m "end with slash\\" &&
744 echo >>file1 &&
745 test_tick &&
938791cd
BC
746 git commit -a -m "something (\000) that looks like octal" &&
747 echo >>file1 &&
748 test_tick &&
749 git commit -a -m "something (\n) that looks like a newline" &&
750 echo >>file1 &&
751 test_tick &&
57f2b6b2
JH
752 git commit -a -m "another commit"
753'
754
755test_expect_success 'rebase-i history with funny messages' '
756 git rev-list A..funny >expect &&
757 test_tick &&
938791cd 758 FAKE_LINES="1 2 3 4" git rebase -i A &&
57f2b6b2
JH
759 git rev-list A.. >actual &&
760 test_cmp expect actual
761'
762
c2145384
LK
763
764test_expect_success 'prepare for rebase -i --exec' '
765 git checkout master &&
766 git checkout -b execute &&
767 test_commit one_exec main.txt one_exec &&
768 test_commit two_exec main.txt two_exec &&
769 test_commit three_exec main.txt three_exec
770'
771
772
773test_expect_success 'running "git rebase -i --exec git show HEAD"' '
774 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
775 (
776 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
777 export FAKE_LINES &&
778 git rebase -i HEAD~2 >expect
779 ) &&
780 sed -e "1,9d" expect >expected &&
781 test_cmp expected actual
782'
783
784
785test_expect_success 'running "git rebase --exec git show HEAD -i"' '
786 git reset --hard execute &&
787 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
788 (
789 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
790 export FAKE_LINES &&
791 git rebase -i HEAD~2 >expect
792 ) &&
793 sed -e "1,9d" expect >expected &&
794 test_cmp expected actual
795'
796
797
798test_expect_success 'running "git rebase -ix git show HEAD"' '
799 git reset --hard execute &&
800 git rebase -ix "git show HEAD" HEAD~2 >actual &&
801 (
802 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
803 export FAKE_LINES &&
804 git rebase -i HEAD~2 >expect
805 ) &&
806 sed -e "1,9d" expect >expected &&
807 test_cmp expected actual
808'
809
810
811test_expect_success 'rebase -ix with several <CMD>' '
812 git reset --hard execute &&
813 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
814 (
815 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
816 export FAKE_LINES &&
817 git rebase -i HEAD~2 >expect
818 ) &&
819 sed -e "1,9d" expect >expected &&
820 test_cmp expected actual
821'
822
823
824test_expect_success 'rebase -ix with several instances of --exec' '
825 git reset --hard execute &&
826 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
827 (
828 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
829 exec_git_show_HEAD exec_pwd" &&
830 export FAKE_LINES &&
831 git rebase -i HEAD~2 >expect
832 ) &&
833 sed -e "1,11d" expect >expected &&
834 test_cmp expected actual
835'
836
837
838test_expect_success 'rebase -ix with --autosquash' '
839 git reset --hard execute &&
840 git checkout -b autosquash &&
841 echo second >second.txt &&
842 git add second.txt &&
843 git commit -m "fixup! two_exec" &&
844 echo bis >bis.txt &&
845 git add bis.txt &&
846 git commit -m "fixup! two_exec" &&
847 (
848 git checkout -b autosquash_actual &&
849 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
850 ) &&
851 git checkout autosquash &&
852 (
853 git checkout -b autosquash_expected &&
854 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
855 export FAKE_LINES &&
856 git rebase -i HEAD~4 >expect
857 ) &&
858 sed -e "1,13d" expect >expected &&
859 test_cmp expected actual
860'
861
862
863test_expect_success 'rebase --exec without -i shows error message' '
864 git reset --hard execute &&
865 test_must_fail git rebase --exec "git show HEAD" HEAD~2 2>actual &&
465d6a00 866 echo "The --exec option must be used with the --interactive option" >expected &&
c2145384
LK
867 test_i18ncmp expected actual
868'
869
870
871test_expect_success 'rebase -i --exec without <CMD>' '
872 git reset --hard execute &&
873 test_must_fail git rebase -i --exec 2>tmp &&
874 sed -e "1d" tmp >actual &&
875 test_must_fail git rebase -h >expected &&
876 test_cmp expected actual &&
877 git checkout master
878'
879
b64b7feb
CW
880test_expect_success 'rebase -i --root re-order and drop commits' '
881 git checkout E &&
882 FAKE_LINES="3 1 2 5" git rebase -i --root &&
883 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
884 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
885 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
886 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
887 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
888'
889
890test_expect_success 'rebase -i --root retain root commit author and message' '
891 git checkout A &&
892 echo B >file7 &&
893 git add file7 &&
894 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
895 FAKE_LINES="2" git rebase -i --root &&
896 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
897 git cat-file commit HEAD | grep -q "^different author$"
898'
899
900test_expect_success 'rebase -i --root temporary sentinel commit' '
901 git checkout B &&
994fd91d
MG
902 (
903 FAKE_LINES="2" &&
904 export FAKE_LINES &&
905 test_must_fail git rebase -i --root
906 ) &&
b64b7feb
CW
907 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
908 git rebase --abort
909'
910
2147f844
CW
911test_expect_success 'rebase -i --root fixup root commit' '
912 git checkout B &&
913 FAKE_LINES="1 fixup 2" git rebase -i --root &&
914 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
915 test B = $(git show HEAD:file1) &&
916 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
917'
918
9f4981ba
AW
919test_expect_success 'rebase --edit-todo does not works on non-interactive rebase' '
920 git reset --hard &&
921 git checkout conflict-branch &&
922 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
923 test_must_fail git rebase --edit-todo &&
924 git rebase --abort
925'
926
927test_expect_success 'rebase --edit-todo can be used to modify todo' '
928 git reset --hard &&
929 git checkout no-conflict-branch^0 &&
930 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
931 FAKE_LINES="2 1" git rebase --edit-todo &&
932 git rebase --continue
933 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
934 test L = $(git cat-file commit HEAD | sed -ne \$p)
935'
936
180bad3d
JK
937test_expect_success 'rebase -i respects core.commentchar' '
938 git reset --hard &&
939 git checkout E^0 &&
aac6c2f4 940 test_config core.commentchar "\\" &&
180bad3d
JK
941 write_script remove-all-but-first.sh <<-\EOF &&
942 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
943 mv "$1.tmp" "$1"
944 EOF
945 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
946 git rebase -i B &&
947 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
948'
949
1b1dce4b 950test_done