]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3404-rebase-interactive.sh
Merge branch 'tt/bisect-in-c'
[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 31
b4995494
MB
32# WARNING: Modifications to the initial repository can change the SHA ID used
33# in the expect2 file for the 'stop on conflicting pick' test.
34
1b1dce4b 35test_expect_success 'setup' '
163f3925
MH
36 test_commit A file1 &&
37 test_commit B file1 &&
38 test_commit C file2 &&
39 test_commit D file1 &&
40 test_commit E file3 &&
1b1dce4b 41 git checkout -b branch1 A &&
163f3925
MH
42 test_commit F file4 &&
43 test_commit G file1 &&
44 test_commit H file5 &&
1b1dce4b 45 git checkout -b branch2 F &&
2dec68cf 46 test_commit I file6 &&
6c4c44c4 47 git checkout -b conflict-branch A &&
391a825f
JN
48 test_commit one conflict &&
49 test_commit two conflict &&
50 test_commit three conflict &&
51 test_commit four conflict &&
6c4c44c4 52 git checkout -b no-conflict-branch A &&
391a825f
JN
53 test_commit J fileJ &&
54 test_commit K fileK &&
55 test_commit L fileL &&
56 test_commit M fileM &&
b4995494 57 git checkout -b no-ff-branch A &&
391a825f
JN
58 test_commit N fileN &&
59 test_commit O fileO &&
60 test_commit P fileP
1b1dce4b
JS
61'
62
fa90ab4a 63# "exec" commands are run with the user shell by default, but this may
cd035b1c 64# be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
386aad5a 65# to create a file. Unsetting SHELL avoids such non-portable behavior
5cd3e107 66# in tests. It must be exported for it to take effect where needed.
cd035b1c 67SHELL=
5cd3e107 68export SHELL
cd035b1c 69
bd46cfae
MT
70test_expect_success 'rebase --keep-empty' '
71 git checkout -b emptybranch master &&
72 git commit --allow-empty -m "empty" &&
73 git rebase --keep-empty -i HEAD~2 &&
74 git log --oneline >actual &&
75 test_line_count = 6 actual
76'
77
d078c391 78cat > expect <<EOF
b97e1873 79error: nothing to do
d078c391
AG
80EOF
81
82test_expect_success 'rebase -i with empty HEAD' '
83 set_fake_editor &&
84 test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 &&
85 test_i18ncmp expect actual
86'
87
cd035b1c
MM
88test_expect_success 'rebase -i with the exec command' '
89 git checkout master &&
90 (
31cd8275 91 set_fake_editor &&
cd035b1c
MM
92 FAKE_LINES="1 exec_>touch-one
93 2 exec_>touch-two exec_false exec_>touch-three
94 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
95 export FAKE_LINES &&
96 test_must_fail git rebase -i A
97 ) &&
2caf20c5
MM
98 test_path_is_file touch-one &&
99 test_path_is_file touch-two &&
100 test_path_is_missing touch-three " (should have stopped before)" &&
5c947e2c 101 test_cmp_rev C HEAD &&
cd035b1c 102 git rebase --continue &&
2caf20c5
MM
103 test_path_is_file touch-three &&
104 test_path_is_file "touch-file name with spaces" &&
105 test_path_is_file touch-after-semicolon &&
5c947e2c 106 test_cmp_rev master HEAD &&
cd035b1c
MM
107 rm -f touch-*
108'
109
110test_expect_success 'rebase -i with the exec command runs from tree root' '
111 git checkout master &&
c2e0940b 112 mkdir subdir && (cd subdir &&
31cd8275 113 set_fake_editor &&
cd035b1c 114 FAKE_LINES="1 exec_>touch-subdir" \
c2e0940b
JL
115 git rebase -i HEAD^
116 ) &&
2caf20c5 117 test_path_is_file touch-subdir &&
cd035b1c
MM
118 rm -fr subdir
119'
120
09d7b6c6
JK
121test_expect_success 'rebase -i with exec allows git commands in subdirs' '
122 test_when_finished "rm -rf subdir" &&
123 test_when_finished "git rebase --abort ||:" &&
124 git checkout master &&
125 mkdir subdir && (cd subdir &&
126 set_fake_editor &&
8c64bc94 127 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
09d7b6c6
JK
128 git rebase -i HEAD^
129 )
130'
131
ab5e67d7 132test_expect_success 'rebase -i sets work tree properly' '
133 test_when_finished "rm -rf subdir" &&
134 test_when_finished "test_might_fail git rebase --abort" &&
135 mkdir subdir &&
136 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
137 >actual &&
138 ! grep "/subdir$" actual
139'
140
cd035b1c
MM
141test_expect_success 'rebase -i with the exec command checks tree cleanness' '
142 git checkout master &&
31cd8275 143 set_fake_editor &&
512477b1 144 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
5c947e2c 145 test_cmp_rev master^ HEAD &&
cd035b1c
MM
146 git reset --hard &&
147 git rebase --continue
148'
149
ecfe1ea9
JS
150test_expect_success 'rebase -i with exec of inexistent command' '
151 git checkout master &&
152 test_when_finished "git rebase --abort" &&
31cd8275 153 set_fake_editor &&
512477b1
DT
154 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
155 git rebase -i HEAD^ >actual 2>&1 &&
ecfe1ea9
JS
156 ! grep "Maybe git-rebase is broken" actual
157'
158
891d4a03
PW
159test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
160 test_when_finished "git rebase --abort ||:" &&
161 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
162'
163
1b1dce4b 164test_expect_success 'no changes are a nop' '
6c4c44c4 165 git checkout branch2 &&
31cd8275 166 set_fake_editor &&
1b1dce4b 167 git rebase -i F &&
ab736792 168 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
1b1dce4b
JS
169 test $(git rev-parse I) = $(git rev-parse HEAD)
170'
171
c9e65892
JS
172test_expect_success 'test the [branch] option' '
173 git checkout -b dead-end &&
174 git rm file6 &&
175 git commit -m "stop here" &&
31cd8275 176 set_fake_editor &&
c9e65892 177 git rebase -i F branch2 &&
ab736792
SB
178 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
179 test $(git rev-parse I) = $(git rev-parse branch2) &&
c9e65892
JS
180 test $(git rev-parse I) = $(git rev-parse HEAD)
181'
182
ab736792
SB
183test_expect_success 'test --onto <branch>' '
184 git checkout -b test-onto branch2 &&
31cd8275 185 set_fake_editor &&
ab736792
SB
186 git rebase -i --onto branch1 F &&
187 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
188 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
189 test $(git rev-parse I) = $(git rev-parse branch2)
190'
191
1b1dce4b
JS
192test_expect_success 'rebase on top of a non-conflicting commit' '
193 git checkout branch1 &&
194 git tag original-branch1 &&
31cd8275 195 set_fake_editor &&
1b1dce4b
JS
196 git rebase -i branch2 &&
197 test file6 = $(git diff --name-only original-branch1) &&
ab736792
SB
198 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
199 test $(git rev-parse I) = $(git rev-parse branch2) &&
1b1dce4b
JS
200 test $(git rev-parse I) = $(git rev-parse HEAD~2)
201'
202
68a163c9
JS
203test_expect_success 'reflog for the branch shows state before rebase' '
204 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
205'
206
1ceb9dfa
PW
207test_expect_success 'reflog for the branch shows correct finish message' '
208 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
209 "$(git rev-parse branch2)" >expected &&
210 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
211 test_cmp expected actual
212'
213
1b1dce4b 214test_expect_success 'exchange two commits' '
31cd8275 215 set_fake_editor &&
1b1dce4b 216 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
b4ce54fc
JK
217 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
218 test G = $(git cat-file commit HEAD | sed -ne \$p)
1b1dce4b
JS
219'
220
221cat > expect << EOF
222diff --git a/file1 b/file1
163f3925 223index f70f10e..fd79235 100644
1b1dce4b
JS
224--- a/file1
225+++ b/file1
163f3925
MH
226@@ -1 +1 @@
227-A
228+G
1b1dce4b
JS
229EOF
230
231cat > expect2 << EOF
606475f3 232<<<<<<< HEAD
163f3925 233D
1b1dce4b 234=======
163f3925 235G
b4995494 236>>>>>>> 5d18e54... G
1b1dce4b
JS
237EOF
238
239test_expect_success 'stop on conflicting pick' '
240 git tag new-branch1 &&
31cd8275 241 set_fake_editor &&
ab736792
SB
242 test_must_fail git rebase -i master &&
243 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
28ed6e7b 244 test_cmp expect .git/rebase-merge/patch &&
82ebb0b6 245 test_cmp expect2 file1 &&
0cb0e143 246 test "$(git diff --name-status |
ab736792 247 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
28ed6e7b
JS
248 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
249 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
1b1dce4b
JS
250'
251
66335298
NTND
252test_expect_success 'show conflicted patch' '
253 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
fbd7a232
NTND
254 grep "show.*REBASE_HEAD" stderr &&
255 # the original stopped-sha1 is abbreviated
256 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
257 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
66335298
NTND
258'
259
1b1dce4b
JS
260test_expect_success 'abort' '
261 git rebase --abort &&
262 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
ab736792 263 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
2caf20c5 264 test_path_is_missing .git/rebase-merge
1b1dce4b
JS
265'
266
b096374f
IWC
267test_expect_success 'abort with error when new base cannot be checked out' '
268 git rm --cached file1 &&
269 git commit -m "remove file in base" &&
31cd8275 270 set_fake_editor &&
b096374f 271 test_must_fail git rebase -i master > output 2>&1 &&
e5c1272c 272 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
b096374f 273 output &&
e5c1272c 274 test_i18ngrep "file1" output &&
2caf20c5 275 test_path_is_missing .git/rebase-merge &&
b096374f
IWC
276 git reset --hard HEAD^
277'
278
1b1dce4b
JS
279test_expect_success 'retain authorship' '
280 echo A > file7 &&
281 git add file7 &&
c54b7817 282 test_tick &&
1b1dce4b
JS
283 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
284 git tag twerp &&
31cd8275 285 set_fake_editor &&
1b1dce4b
JS
286 git rebase -i --onto master HEAD^ &&
287 git show HEAD | grep "^Author: Twerp Snog"
288'
289
a2a20b0d 290test_expect_success 'retain authorship w/ conflicts' '
650161a2
JH
291 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
292 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
293
a2a20b0d
JS
294 git reset --hard twerp &&
295 test_commit a conflict a conflict-a &&
296 git reset --hard twerp &&
650161a2
JH
297
298 GIT_AUTHOR_NAME=AttributeMe &&
299 export GIT_AUTHOR_NAME &&
a2a20b0d 300 test_commit b conflict b conflict-b &&
650161a2
JH
301 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
302
a2a20b0d
JS
303 set_fake_editor &&
304 test_must_fail git rebase -i conflict-a &&
305 echo resolved >conflict &&
306 git add conflict &&
307 git rebase --continue &&
308 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
309 git show >out &&
310 grep AttributeMe out
311'
312
1b1dce4b
JS
313test_expect_success 'squash' '
314 git reset --hard twerp &&
315 echo B > file7 &&
c54b7817 316 test_tick &&
1b1dce4b
JS
317 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
318 echo "******************************" &&
31cd8275 319 set_fake_editor &&
5065ed29 320 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
959c0d06 321 git rebase -i --onto master HEAD~2 &&
1b1dce4b
JS
322 test B = $(cat file7) &&
323 test $(git rev-parse HEAD^) = $(git rev-parse master)
324'
325
326test_expect_success 'retain authorship when squashing' '
81ab1cb4 327 git show HEAD | grep "^Author: Twerp Snog"
1b1dce4b
JS
328'
329
11aad464 330test_expect_success REBASE_P '-p handles "no changes" gracefully' '
34454e85 331 HEAD=$(git rev-parse HEAD) &&
31cd8275 332 set_fake_editor &&
34454e85 333 git rebase -i -p HEAD^ &&
71d9451e
TR
334 git update-index --refresh &&
335 git diff-files --quiet &&
336 git diff-index --quiet --cached HEAD -- &&
34454e85
JS
337 test $HEAD = $(git rev-parse HEAD)
338'
339
11aad464 340test_expect_failure REBASE_P 'exchange two commits with -p' '
27ccd8d1 341 git checkout H &&
31cd8275 342 set_fake_editor &&
cddb42d2
JN
343 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
344 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
345 test G = $(git cat-file commit HEAD | sed -ne \$p)
346'
347
11aad464 348test_expect_success REBASE_P 'preserve merges with -p' '
f09c9b8c
JS
349 git checkout -b to-be-preserved master^ &&
350 : > unrelated-file &&
351 git add unrelated-file &&
352 test_tick &&
353 git commit -m "unrelated" &&
5352a82b 354 git checkout -b another-branch master &&
f09c9b8c
JS
355 echo B > file1 &&
356 test_tick &&
357 git commit -m J file1 &&
358 test_tick &&
359 git merge to-be-preserved &&
360 echo C > file1 &&
361 test_tick &&
362 git commit -m K file1 &&
5352a82b
SB
363 echo D > file1 &&
364 test_tick &&
365 git commit -m L1 file1 &&
366 git checkout HEAD^ &&
367 echo 1 > unrelated-file &&
368 test_tick &&
369 git commit -m L2 unrelated-file &&
370 test_tick &&
371 git merge another-branch &&
372 echo E > file1 &&
373 test_tick &&
374 git commit -m M file1 &&
375 git checkout -b to-be-rebased &&
18640d99 376 test_tick &&
31cd8275 377 set_fake_editor &&
f09c9b8c 378 git rebase -i -p --onto branch1 master &&
71d9451e
TR
379 git update-index --refresh &&
380 git diff-files --quiet &&
381 git diff-index --quiet --cached HEAD -- &&
5352a82b
SB
382 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
383 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
384 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
385 test $(git show HEAD~5:file1) = B &&
386 test $(git show HEAD~3:file1) = C &&
387 test $(git show HEAD:file1) = E &&
388 test $(git show HEAD:unrelated-file) = 1
f09c9b8c
JS
389'
390
11aad464 391test_expect_success REBASE_P 'edit ancestor with -p' '
31cd8275 392 set_fake_editor &&
12bf8283 393 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
a96dc01e
TR
394 echo 2 > unrelated-file &&
395 test_tick &&
396 git commit -m L2-modified --amend unrelated-file &&
397 git rebase --continue &&
398 git update-index --refresh &&
399 git diff-files --quiet &&
400 git diff-index --quiet --cached HEAD -- &&
401 test $(git show HEAD:unrelated-file) = 2
402'
403
18640d99 404test_expect_success '--continue tries to commit' '
6c8fbae6 405 git reset --hard D &&
18640d99 406 test_tick &&
31cd8275 407 set_fake_editor &&
ab736792 408 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
18640d99
JS
409 echo resolved > file1 &&
410 git add file1 &&
411 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
412 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
413 git show HEAD | grep chouette
414'
415
8e4a91bd 416test_expect_success 'verbose flag is heeded, even after --continue' '
53f2ffa8 417 git reset --hard master@{1} &&
8e4a91bd 418 test_tick &&
31cd8275 419 set_fake_editor &&
ab736792 420 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
8e4a91bd
JS
421 echo resolved > file1 &&
422 git add file1 &&
423 git rebase --continue > output &&
dc801e71 424 grep "^ file1 | 2 +-$" output
8e4a91bd
JS
425'
426
0d75bfe6 427test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
6368f3f8 428 base=$(git rev-parse HEAD~4) &&
31cd8275 429 set_fake_editor &&
6368f3f8 430 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
959c0d06 431 EXPECT_HEADER_COUNT=4 \
6368f3f8
JS
432 git rebase -i $base &&
433 test $base = $(git rev-parse HEAD^) &&
434 test 1 = $(git show | grep ONCE | wc -l)
435'
436
0d75bfe6 437test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
0205e72f
MH
438 git checkout -b multi-fixup E &&
439 base=$(git rev-parse HEAD~4) &&
31cd8275 440 set_fake_editor &&
a25eb139 441 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
0205e72f
MH
442 git rebase -i $base &&
443 test $base = $(git rev-parse HEAD^) &&
a25eb139 444 test 0 = $(git show | grep NEVER | wc -l) &&
6c8fbae6 445 git checkout @{-1} &&
0205e72f
MH
446 git branch -D multi-fixup
447'
448
6bdcd0d2
MH
449test_expect_success 'commit message used after conflict' '
450 git checkout -b conflict-fixup conflict-branch &&
451 base=$(git rev-parse HEAD~4) &&
31cd8275 452 set_fake_editor &&
512477b1 453 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
6bdcd0d2
MH
454 echo three > conflict &&
455 git add conflict &&
456 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
457 git rebase --continue &&
458 test $base = $(git rev-parse HEAD^) &&
459 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 460 git checkout @{-1} &&
6bdcd0d2
MH
461 git branch -D conflict-fixup
462'
463
464test_expect_success 'commit message retained after conflict' '
465 git checkout -b conflict-squash conflict-branch &&
466 base=$(git rev-parse HEAD~4) &&
31cd8275 467 set_fake_editor &&
512477b1 468 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
6bdcd0d2
MH
469 echo three > conflict &&
470 git add conflict &&
471 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
472 git rebase --continue &&
473 test $base = $(git rev-parse HEAD^) &&
474 test 2 = $(git show | grep TWICE | wc -l) &&
6c8fbae6 475 git checkout @{-1} &&
6bdcd0d2
MH
476 git branch -D conflict-squash
477'
478
0205e72f
MH
479cat > expect-squash-fixup << EOF
480B
481
482D
483
484ONCE
485EOF
486
0d75bfe6 487test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
0205e72f
MH
488 git checkout -b squash-fixup E &&
489 base=$(git rev-parse HEAD~4) &&
31cd8275 490 set_fake_editor &&
0205e72f 491 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
959c0d06 492 EXPECT_HEADER_COUNT=4 \
0205e72f
MH
493 git rebase -i $base &&
494 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
495 test_cmp expect-squash-fixup actual-squash-fixup &&
60b61588
PW
496 git cat-file commit HEAD@{2} |
497 grep "^# This is a combination of 3 commits\." &&
498 git cat-file commit HEAD@{3} |
499 grep "^# This is a combination of 2 commits\." &&
6c8fbae6 500 git checkout @{-1} &&
0205e72f
MH
501 git branch -D squash-fixup
502'
503
0d75bfe6 504test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
234b3dae
MH
505 git checkout -b skip-comments E &&
506 base=$(git rev-parse HEAD~4) &&
31cd8275 507 set_fake_editor &&
234b3dae
MH
508 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
509 EXPECT_HEADER_COUNT=4 \
510 git rebase -i $base &&
511 test $base = $(git rev-parse HEAD^) &&
512 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 513 git checkout @{-1} &&
234b3dae
MH
514 git branch -D skip-comments
515'
516
0d75bfe6 517test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
234b3dae
MH
518 git checkout -b skip-blank-lines E &&
519 base=$(git rev-parse HEAD~4) &&
31cd8275 520 set_fake_editor &&
234b3dae
MH
521 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
522 EXPECT_HEADER_COUNT=4 \
523 git rebase -i $base &&
524 test $base = $(git rev-parse HEAD^) &&
525 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 526 git checkout @{-1} &&
234b3dae
MH
527 git branch -D skip-blank-lines
528'
529
fb47cfbd 530test_expect_success 'squash works as expected' '
6c4c44c4 531 git checkout -b squash-works no-conflict-branch &&
fb47cfbd 532 one=$(git rev-parse HEAD~3) &&
31cd8275 533 set_fake_editor &&
8c64bc94 534 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 \
959c0d06 535 git rebase -i HEAD~3 &&
fb47cfbd
JS
536 test $one = $(git rev-parse HEAD~2)
537'
538
539test_expect_success 'interrupted squash works as expected' '
6c4c44c4 540 git checkout -b interrupted-squash conflict-branch &&
fb47cfbd 541 one=$(git rev-parse HEAD~3) &&
31cd8275 542 set_fake_editor &&
512477b1 543 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
0590ff26 544 test_write_lines one two four > conflict &&
fb47cfbd 545 git add conflict &&
ab736792 546 test_must_fail git rebase --continue &&
fb47cfbd
JS
547 echo resolved > conflict &&
548 git add conflict &&
549 git rebase --continue &&
550 test $one = $(git rev-parse HEAD~2)
551'
552
1d25c8cf 553test_expect_success 'interrupted squash works as expected (case 2)' '
6c4c44c4 554 git checkout -b interrupted-squash2 conflict-branch &&
1d25c8cf 555 one=$(git rev-parse HEAD~3) &&
31cd8275 556 set_fake_editor &&
512477b1 557 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
0590ff26 558 test_write_lines one four > conflict &&
1d25c8cf 559 git add conflict &&
ab736792 560 test_must_fail git rebase --continue &&
0590ff26 561 test_write_lines one two four > conflict &&
1d25c8cf 562 git add conflict &&
ab736792 563 test_must_fail git rebase --continue &&
1d25c8cf
JS
564 echo resolved > conflict &&
565 git add conflict &&
566 git rebase --continue &&
567 test $one = $(git rev-parse HEAD~2)
568'
569
3f213981 570test_expect_success '--continue tries to commit, even for "edit"' '
96ffe892
JS
571 echo unrelated > file7 &&
572 git add file7 &&
573 test_tick &&
574 git commit -m "unrelated change" &&
be6ff208
JS
575 parent=$(git rev-parse HEAD^) &&
576 test_tick &&
31cd8275 577 set_fake_editor &&
be6ff208
JS
578 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
579 echo edited > file7 &&
580 git add file7 &&
581 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
582 test edited = $(git show HEAD:file7) &&
583 git show HEAD | grep chouette &&
584 test $parent = $(git rev-parse HEAD^)
585'
586
dc7f55cb
SB
587test_expect_success 'aborted --continue does not squash commits after "edit"' '
588 old=$(git rev-parse HEAD) &&
589 test_tick &&
31cd8275 590 set_fake_editor &&
dc7f55cb
SB
591 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
592 echo "edited again" > file7 &&
593 git add file7 &&
a3ec9eaf
EN
594 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
595 test $old = $(git rev-parse HEAD) &&
596 git rebase --abort
dc7f55cb
SB
597'
598
f8aa1b69
SB
599test_expect_success 'auto-amend only edited commits after "edit"' '
600 test_tick &&
31cd8275 601 set_fake_editor &&
f8aa1b69
SB
602 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
603 echo "edited again" > file7 &&
604 git add file7 &&
605 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
606 echo "and again" > file7 &&
607 git add file7 &&
608 test_tick &&
512477b1 609 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
f8aa1b69
SB
610 git rebase --abort
611'
612
ffaaed88
MM
613test_expect_success 'clean error after failed "exec"' '
614 test_tick &&
615 test_when_finished "git rebase --abort || :" &&
31cd8275 616 set_fake_editor &&
512477b1 617 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
ffaaed88
MM
618 echo "edited again" > file7 &&
619 git add file7 &&
620 test_must_fail git rebase --continue 2>error &&
18633e1a 621 test_i18ngrep "you have staged changes in your working tree" error
ffaaed88
MM
622'
623
73697a0b
JS
624test_expect_success 'rebase a detached HEAD' '
625 grandparent=$(git rev-parse HEAD~2) &&
626 git checkout $(git rev-parse HEAD) &&
627 test_tick &&
31cd8275 628 set_fake_editor &&
73697a0b
JS
629 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
630 test $grandparent = $(git rev-parse HEAD~2)
631'
632
752527f5
JS
633test_expect_success 'rebase a commit violating pre-commit' '
634
635 mkdir -p .git/hooks &&
7bec7f50
JH
636 write_script .git/hooks/pre-commit <<-\EOF &&
637 test -z "$(git diff --cached --check)"
638 EOF
752527f5
JS
639 echo "monde! " >> file1 &&
640 test_tick &&
ab736792 641 test_must_fail git commit -m doesnt-verify file1 &&
752527f5
JS
642 git commit -m doesnt-verify --no-verify file1 &&
643 test_tick &&
31cd8275 644 set_fake_editor &&
752527f5
JS
645 FAKE_LINES=2 git rebase -i HEAD~2
646
647'
648
077b725f
JH
649test_expect_success 'rebase with a file named HEAD in worktree' '
650
651 rm -fr .git/hooks &&
652 git reset --hard &&
653 git checkout -b branch3 A &&
654
655 (
656 GIT_AUTHOR_NAME="Squashed Away" &&
657 export GIT_AUTHOR_NAME &&
658 >HEAD &&
659 git add HEAD &&
660 git commit -m "Add head" &&
661 >BODY &&
662 git add BODY &&
663 git commit -m "Add body"
664 ) &&
665
31cd8275 666 set_fake_editor &&
6c8fbae6 667 FAKE_LINES="1 squash 2" git rebase -i @{-1} &&
077b725f
JH
668 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
669
670'
671
ff74126c
JS
672test_expect_success 'do "noop" when there is nothing to cherry-pick' '
673
674 git checkout -b branch4 HEAD &&
675 GIT_EDITOR=: git commit --amend \
2dec68cf 676 --author="Somebody else <somebody@else.com>" &&
ff74126c 677 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
31cd8275 678 set_fake_editor &&
ff74126c
JS
679 git rebase -i branch3 &&
680 test $(git rev-parse branch3) = $(git rev-parse branch4)
681
682'
683
96747696
JH
684test_expect_success 'submodule rebase setup' '
685 git checkout A &&
686 mkdir sub &&
687 (
688 cd sub && git init && >elif &&
689 git add elif && git commit -m "submodule initial"
690 ) &&
691 echo 1 >file1 &&
2dec68cf 692 git add file1 sub &&
96747696
JH
693 test_tick &&
694 git commit -m "One" &&
695 echo 2 >file1 &&
696 test_tick &&
697 git commit -a -m "Two" &&
698 (
699 cd sub && echo 3 >elif &&
700 git commit -a -m "submodule second"
701 ) &&
702 test_tick &&
31cd8275 703 set_fake_editor &&
96747696
JH
704 git commit -a -m "Three changes submodule"
705'
706
94c88ede 707test_expect_success 'submodule rebase -i' '
31cd8275 708 set_fake_editor &&
96747696
JH
709 FAKE_LINES="1 squash 2 3" git rebase -i A
710'
711
a6754cda
JK
712test_expect_success 'submodule conflict setup' '
713 git tag submodule-base &&
714 git checkout HEAD^ &&
715 (
716 cd sub && git checkout HEAD^ && echo 4 >elif &&
717 git add elif && git commit -m "submodule conflict"
718 ) &&
719 git add sub &&
720 test_tick &&
721 git commit -m "Conflict in submodule" &&
722 git tag submodule-topic
723'
724
725test_expect_success 'rebase -i continue with only submodule staged' '
31cd8275 726 set_fake_editor &&
a6754cda
JK
727 test_must_fail git rebase -i submodule-base &&
728 git add sub &&
729 git rebase --continue &&
730 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
731'
732
733test_expect_success 'rebase -i continue with unstaged submodule' '
734 git checkout submodule-topic &&
735 git reset --hard &&
31cd8275 736 set_fake_editor &&
a6754cda
JK
737 test_must_fail git rebase -i submodule-base &&
738 git reset &&
739 git rebase --continue &&
740 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
741'
742
0e757e30
JS
743test_expect_success 'avoid unnecessary reset' '
744 git checkout master &&
a6754cda 745 git reset --hard &&
0e496492 746 test-tool chmtime =123456789 file3 &&
0e757e30
JS
747 git update-index --refresh &&
748 HEAD=$(git rev-parse HEAD) &&
31cd8275 749 set_fake_editor &&
0e757e30
JS
750 git rebase -i HEAD~4 &&
751 test $HEAD = $(git rev-parse HEAD) &&
deb9845a 752 MTIME=$(test-tool chmtime --get file3) &&
0e757e30
JS
753 test 123456789 = $MTIME
754'
755
6741aa6c
BG
756test_expect_success 'reword' '
757 git checkout -b reword-branch master &&
31cd8275 758 set_fake_editor &&
6741aa6c
BG
759 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
760 git show HEAD | grep "E changed" &&
761 test $(git rev-parse master) != $(git rev-parse HEAD) &&
762 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
763 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
764 git show HEAD^ | grep "D changed" &&
765 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
766 git show HEAD~3 | grep "B changed" &&
8c64bc94 767 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
6741aa6c
BG
768 git show HEAD~2 | grep "C changed"
769'
770
eb2151bb
TR
771test_expect_success 'rebase -i can copy notes' '
772 git config notes.rewrite.rebase true &&
773 git config notes.rewriteRef "refs/notes/*" &&
774 test_commit n1 &&
775 test_commit n2 &&
776 test_commit n3 &&
777 git notes add -m"a note" n3 &&
31cd8275 778 set_fake_editor &&
6f87f038 779 git rebase -i --onto n1 n2 &&
eb2151bb
TR
780 test "a note" = "$(git notes show HEAD)"
781'
782
783cat >expect <<EOF
784an earlier note
d4990c4b 785
eb2151bb
TR
786a note
787EOF
788
789test_expect_success 'rebase -i can copy notes over a fixup' '
790 git reset --hard n3 &&
791 git notes add -m"an earlier note" n2 &&
31cd8275 792 set_fake_editor &&
8c64bc94 793 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" git rebase -i n1 &&
eb2151bb
TR
794 git notes show > output &&
795 test_cmp expect output
796'
797
2ec33cdd
DO
798test_expect_success 'rebase while detaching HEAD' '
799 git symbolic-ref HEAD &&
800 grandparent=$(git rev-parse HEAD~2) &&
801 test_tick &&
31cd8275 802 set_fake_editor &&
2ec33cdd
DO
803 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
804 test $grandparent = $(git rev-parse HEAD~2) &&
805 test_must_fail git symbolic-ref HEAD
806'
807
b4995494
MB
808test_tick # Ensure that the rebased commits get a different timestamp.
809test_expect_success 'always cherry-pick with --no-ff' '
810 git checkout no-ff-branch &&
811 git tag original-no-ff-branch &&
31cd8275 812 set_fake_editor &&
b4995494 813 git rebase -i --no-ff A &&
b4995494
MB
814 for p in 0 1 2
815 do
816 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
817 git diff HEAD~$p original-no-ff-branch~$p > out &&
1c5e94f4 818 test_must_be_empty out
b4995494
MB
819 done &&
820 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
821 git diff HEAD~3 original-no-ff-branch~3 > out &&
1c5e94f4 822 test_must_be_empty out
b4995494
MB
823'
824
57f2b6b2
JH
825test_expect_success 'set up commits with funny messages' '
826 git checkout -b funny A &&
827 echo >>file1 &&
828 test_tick &&
829 git commit -a -m "end with slash\\" &&
830 echo >>file1 &&
831 test_tick &&
938791cd
BC
832 git commit -a -m "something (\000) that looks like octal" &&
833 echo >>file1 &&
834 test_tick &&
835 git commit -a -m "something (\n) that looks like a newline" &&
836 echo >>file1 &&
837 test_tick &&
57f2b6b2
JH
838 git commit -a -m "another commit"
839'
840
841test_expect_success 'rebase-i history with funny messages' '
842 git rev-list A..funny >expect &&
843 test_tick &&
31cd8275 844 set_fake_editor &&
938791cd 845 FAKE_LINES="1 2 3 4" git rebase -i A &&
57f2b6b2
JH
846 git rev-list A.. >actual &&
847 test_cmp expect actual
848'
849
c2145384
LK
850test_expect_success 'prepare for rebase -i --exec' '
851 git checkout master &&
852 git checkout -b execute &&
853 test_commit one_exec main.txt one_exec &&
854 test_commit two_exec main.txt two_exec &&
855 test_commit three_exec main.txt three_exec
856'
857
c2145384 858test_expect_success 'running "git rebase -i --exec git show HEAD"' '
31cd8275 859 set_fake_editor &&
c2145384
LK
860 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
861 (
862 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
863 export FAKE_LINES &&
864 git rebase -i HEAD~2 >expect
865 ) &&
866 sed -e "1,9d" expect >expected &&
867 test_cmp expected actual
868'
869
c2145384
LK
870test_expect_success 'running "git rebase --exec git show HEAD -i"' '
871 git reset --hard execute &&
31cd8275 872 set_fake_editor &&
c2145384
LK
873 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
874 (
875 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
876 export FAKE_LINES &&
877 git rebase -i HEAD~2 >expect
878 ) &&
879 sed -e "1,9d" expect >expected &&
880 test_cmp expected actual
881'
882
c2145384
LK
883test_expect_success 'running "git rebase -ix git show HEAD"' '
884 git reset --hard execute &&
31cd8275 885 set_fake_editor &&
c2145384
LK
886 git rebase -ix "git show HEAD" HEAD~2 >actual &&
887 (
888 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
889 export FAKE_LINES &&
890 git rebase -i HEAD~2 >expect
891 ) &&
892 sed -e "1,9d" expect >expected &&
893 test_cmp expected actual
894'
895
896
897test_expect_success 'rebase -ix with several <CMD>' '
898 git reset --hard execute &&
31cd8275 899 set_fake_editor &&
c2145384
LK
900 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
901 (
902 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
903 export FAKE_LINES &&
904 git rebase -i HEAD~2 >expect
905 ) &&
906 sed -e "1,9d" expect >expected &&
907 test_cmp expected actual
908'
909
c2145384
LK
910test_expect_success 'rebase -ix with several instances of --exec' '
911 git reset --hard execute &&
31cd8275 912 set_fake_editor &&
c2145384
LK
913 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
914 (
915 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
916 exec_git_show_HEAD exec_pwd" &&
917 export FAKE_LINES &&
918 git rebase -i HEAD~2 >expect
919 ) &&
920 sed -e "1,11d" expect >expected &&
921 test_cmp expected actual
922'
923
0d75bfe6 924test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
c2145384
LK
925 git reset --hard execute &&
926 git checkout -b autosquash &&
927 echo second >second.txt &&
928 git add second.txt &&
929 git commit -m "fixup! two_exec" &&
930 echo bis >bis.txt &&
931 git add bis.txt &&
932 git commit -m "fixup! two_exec" &&
31cd8275 933 set_fake_editor &&
c2145384
LK
934 (
935 git checkout -b autosquash_actual &&
936 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
937 ) &&
938 git checkout autosquash &&
939 (
940 git checkout -b autosquash_expected &&
941 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
942 export FAKE_LINES &&
943 git rebase -i HEAD~4 >expect
944 ) &&
945 sed -e "1,13d" expect >expected &&
946 test_cmp expected actual
947'
948
78ec2400 949test_expect_success 'rebase --exec works without -i ' '
c2145384 950 git reset --hard execute &&
78ec2400
SB
951 rm -rf exec_output &&
952 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
953 test_i18ngrep "Successfully rebased and updated" actual &&
954 test_line_count = 2 exec_output &&
955 test_path_is_missing invoked_editor
c2145384
LK
956'
957
c2145384
LK
958test_expect_success 'rebase -i --exec without <CMD>' '
959 git reset --hard execute &&
31cd8275 960 set_fake_editor &&
3bb0923f
PSU
961 test_must_fail git rebase -i --exec 2>actual &&
962 test_i18ngrep "requires a value" actual &&
c2145384
LK
963 git checkout master
964'
965
b64b7feb
CW
966test_expect_success 'rebase -i --root re-order and drop commits' '
967 git checkout E &&
31cd8275 968 set_fake_editor &&
b64b7feb
CW
969 FAKE_LINES="3 1 2 5" git rebase -i --root &&
970 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
971 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
972 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
973 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
974 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
975'
976
977test_expect_success 'rebase -i --root retain root commit author and message' '
978 git checkout A &&
979 echo B >file7 &&
980 git add file7 &&
981 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
31cd8275 982 set_fake_editor &&
b64b7feb
CW
983 FAKE_LINES="2" git rebase -i --root &&
984 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
985 git cat-file commit HEAD | grep -q "^different author$"
986'
987
988test_expect_success 'rebase -i --root temporary sentinel commit' '
989 git checkout B &&
512477b1
DT
990 set_fake_editor &&
991 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
b64b7feb
CW
992 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
993 git rebase --abort
994'
995
2147f844
CW
996test_expect_success 'rebase -i --root fixup root commit' '
997 git checkout B &&
31cd8275 998 set_fake_editor &&
2147f844
CW
999 FAKE_LINES="1 fixup 2" git rebase -i --root &&
1000 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1001 test B = $(git show HEAD:file1) &&
1002 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1003'
1004
76fda6eb 1005test_expect_success 'rebase -i --root reword root commit' '
3a36ca08
TZ
1006 test_when_finished "test_might_fail git rebase --abort" &&
1007 git checkout -b reword-root-branch master &&
1008 set_fake_editor &&
1009 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1010 git rebase -i --root &&
87881055
TZ
1011 git show HEAD^ | grep "A changed" &&
1012 test -z "$(git show -s --format=%p HEAD^)"
3a36ca08
TZ
1013'
1014
a9279c67
PW
1015test_expect_success 'rebase -i --root when root has untracked file confilct' '
1016 test_when_finished "reset_rebase" &&
1017 git checkout -b failing-root-pick A &&
1018 echo x >file2 &&
1019 git rm file1 &&
1020 git commit -m "remove file 1 add file 2" &&
1021 echo z >file1 &&
1022 set_fake_editor &&
1023 test_must_fail env FAKE_LINES="1 2" git rebase -i --root &&
1024 rm file1 &&
1025 git rebase --continue &&
1026 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1027 test "$(git rev-list --count HEAD)" = 2
1028'
1029
1030test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1031 test_when_finished "reset_rebase" &&
1032 echo z>file1 &&
1033 set_fake_editor &&
1034 test_must_fail env FAKE_LINES="reword 1 2" \
1035 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1036 rm file1 &&
1037 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue &&
1038 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1039 test "$(git rev-list --count HEAD)" = 2
1040'
1041
0d75bfe6 1042test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
a9279c67 1043 git checkout reword-root-branch &&
9f4981ba
AW
1044 git reset --hard &&
1045 git checkout conflict-branch &&
31cd8275 1046 set_fake_editor &&
9f4981ba
AW
1047 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
1048 test_must_fail git rebase --edit-todo &&
1049 git rebase --abort
1050'
1051
1052test_expect_success 'rebase --edit-todo can be used to modify todo' '
1053 git reset --hard &&
1054 git checkout no-conflict-branch^0 &&
31cd8275 1055 set_fake_editor &&
9f4981ba
AW
1056 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1057 FAKE_LINES="2 1" git rebase --edit-todo &&
60687de5 1058 git rebase --continue &&
9f4981ba
AW
1059 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1060 test L = $(git cat-file commit HEAD | sed -ne \$p)
1061'
1062
26cd160c
RR
1063test_expect_success 'rebase -i produces readable reflog' '
1064 git reset --hard &&
1065 git branch -f branch-reflog-test H &&
31cd8275 1066 set_fake_editor &&
26cd160c
RR
1067 git rebase -i --onto I F branch-reflog-test &&
1068 cat >expect <<-\EOF &&
26cd160c 1069 rebase -i (finish): returning to refs/heads/branch-reflog-test
d0ab0584
DT
1070 rebase -i (pick): H
1071 rebase -i (pick): G
1072 rebase -i (start): checkout I
26cd160c 1073 EOF
d0ab0584
DT
1074 git reflog -n4 HEAD |
1075 sed "s/[^:]*: //" >actual &&
26cd160c
RR
1076 test_cmp expect actual
1077'
1078
180bad3d
JK
1079test_expect_success 'rebase -i respects core.commentchar' '
1080 git reset --hard &&
1081 git checkout E^0 &&
aac6c2f4 1082 test_config core.commentchar "\\" &&
180bad3d
JK
1083 write_script remove-all-but-first.sh <<-\EOF &&
1084 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1085 mv "$1.tmp" "$1"
1086 EOF
1087 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1088 git rebase -i B &&
1089 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1090'
1091
882cd237 1092test_expect_success 'rebase -i respects core.commentchar=auto' '
66458388
JS
1093 test_config core.commentchar auto &&
1094 write_script copy-edit-script.sh <<-\EOF &&
1095 cp "$1" edit-script
1096 EOF
1097 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1098 test_when_finished "git rebase --abort || :" &&
1099 git rebase -i HEAD^ &&
1100 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1101'
1102
2e6e276d 1103test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
6567dc05
RR
1104 test_when_finished "git branch -D torebase" &&
1105 git checkout -b torebase branch1 &&
1106 upstream=$(git rev-parse ":/J") &&
1107 onto=$(git rev-parse ":/A") &&
1108 git rebase --onto $onto $upstream &&
1109 git reset --hard branch1 &&
1110 git rebase --onto ":/A" ":/J" &&
1111 git checkout branch1
1112'
1113
db2b3b82
AF
1114test_expect_success 'rebase -i with --strategy and -X' '
1115 git checkout -b conflict-merge-use-theirs conflict-branch &&
1116 git reset --hard HEAD^ &&
1117 echo five >conflict &&
1118 echo Z >file1 &&
1119 git commit -a -m "one file conflict" &&
1120 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1121 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1122 test $(cat file1) = Z
1123'
1124
040fd39e
FR
1125test_expect_success 'interrupted rebase -i with --strategy and -X' '
1126 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1127 git reset --hard HEAD^ &&
1128 >breakpoint &&
1129 git add breakpoint &&
1130 git commit -m "breakpoint for interactive mode" &&
1131 echo five >conflict &&
1132 echo Z >file1 &&
1133 git commit -a -m "one file conflict" &&
1134 set_fake_editor &&
1135 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1136 git rebase --continue &&
1137 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1138 test $(cat file1) = Z
1139'
1140
89b0230a 1141test_expect_success 'rebase -i error on commits with \ in message' '
99094a7a 1142 current_head=$(git rev-parse HEAD) &&
89b0230a
MM
1143 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1144 test_commit TO-REMOVE will-conflict old-content &&
1145 test_commit "\temp" will-conflict new-content dummy &&
512477b1 1146 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
89b0230a
MM
1147 test_expect_code 1 grep " emp" error
1148'
1149
66ae9a57
ES
1150test_expect_success 'short SHA-1 setup' '
1151 test_when_finished "git checkout master" &&
1152 git checkout --orphan collide &&
1153 git rm -rf . &&
1154 (
1155 unset test_tick &&
1156 test_commit collide1 collide &&
1157 test_commit --notick collide2 collide &&
1158 test_commit --notick collide3 collide
1159 )
1160'
1161
75c69766 1162test_expect_success 'short SHA-1 collide' '
66ae9a57
ES
1163 test_when_finished "reset_rebase && git checkout master" &&
1164 git checkout collide &&
1165 (
1166 unset test_tick &&
1167 test_tick &&
1168 set_fake_editor &&
1169 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1170 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1171 )
1172'
1173
edb72d55
KS
1174test_expect_success 'respect core.abbrev' '
1175 git config core.abbrev 12 &&
1176 set_cat_todo_editor &&
1177 test_must_fail git rebase -i HEAD~4 >todo-list &&
1178 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1179'
1180
28c8cfc3
ES
1181test_expect_success 'todo count' '
1182 write_script dump-raw.sh <<-\EOF &&
1183 cat "$1"
1184 EOF
1185 test_set_editor "$(pwd)/dump-raw.sh" &&
1186 git rebase -i HEAD~4 >actual &&
b8fc9e43 1187 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
28c8cfc3
ES
1188'
1189
8cbc57ca
PH
1190test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1191 git checkout --force branch2 &&
1192 git clean -f &&
1193 set_fake_editor &&
1194 FAKE_LINES="edit 1 2" git rebase -i A &&
1195 test_cmp_rev HEAD F &&
1196 test_path_is_missing file6 &&
1197 >file6 &&
1198 test_must_fail git rebase --continue &&
1199 test_cmp_rev HEAD F &&
1200 rm file6 &&
1201 git rebase --continue &&
1202 test_cmp_rev HEAD I
1203'
1204
1205test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1206 git checkout --force branch2 &&
1207 git clean -f &&
1208 git tag original-branch2 &&
1209 set_fake_editor &&
1210 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1211 test_cmp_rev HEAD F &&
1212 test_path_is_missing file6 &&
1213 >file6 &&
1214 test_must_fail git rebase --continue &&
1215 test_cmp_rev HEAD F &&
1216 rm file6 &&
1217 git rebase --continue &&
1218 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1219 git reset --hard original-branch2
1220'
1221
1222test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1223 git checkout --force branch2 &&
1224 git clean -f &&
1225 set_fake_editor &&
1226 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1227 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1228 test_path_is_missing file6 &&
1229 >file6 &&
1230 test_must_fail git rebase --continue &&
1231 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1232 rm file6 &&
1233 git rebase --continue &&
1234 test $(git cat-file commit HEAD | sed -ne \$p) = I
1235'
1236
0e0aff4b 1237test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
d17ec3a9
JS
1238 git checkout -b commit-to-skip &&
1239 for double in X 3 1
1240 do
1241 test_seq 5 | sed "s/$double/&&/" >seq &&
1242 git add seq &&
1243 test_tick &&
1244 git commit -m seq-$double
1245 done &&
1246 git tag seq-onto &&
1247 git reset --hard HEAD~2 &&
1248 git cherry-pick seq-onto &&
1249 set_fake_editor &&
1250 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1251 test -d .git/rebase-merge &&
1252 git rebase --continue &&
1253 git diff --exit-code seq-onto &&
1254 test ! -d .git/rebase-merge &&
1255 test ! -f .git/CHERRY_PICK_HEAD
1256'
1257
c9266d58
GR
1258rebase_setup_and_clean () {
1259 test_when_finished "
1260 git checkout master &&
1261 test_might_fail git branch -D $1 &&
1262 test_might_fail git rebase --abort
1263 " &&
ca3e1826 1264 git checkout -b $1 ${2:-master}
c9266d58
GR
1265}
1266
1267test_expect_success 'drop' '
1268 rebase_setup_and_clean drop-test &&
1269 set_fake_editor &&
8c64bc94 1270 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root &&
c9266d58
GR
1271 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1272 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1273 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1274'
1275
37079959
GR
1276test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1277 test_config rebase.missingCommitsCheck ignore &&
1278 rebase_setup_and_clean missing-commit &&
1279 set_fake_editor &&
1280 FAKE_LINES="1 2 3 4" \
1281 git rebase -i --root 2>actual &&
1282 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
21d0764c
JS
1283 test_i18ngrep \
1284 "Successfully rebased and updated refs/heads/missing-commit" \
1285 actual
37079959
GR
1286'
1287
1288cat >expect <<EOF
1289Warning: some commits may have been dropped accidentally.
1290Dropped commits (newer to older):
1291 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1292To avoid this message, use "drop" to explicitly remove a commit.
1293
1294Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1295The possible behaviours are: ignore, warn, error.
1296
21d0764c
JS
1297Rebasing (1/4)
1298Rebasing (2/4)
1299Rebasing (3/4)
1300Rebasing (4/4)
37079959
GR
1301Successfully rebased and updated refs/heads/missing-commit.
1302EOF
1303
21d0764c
JS
1304cr_to_nl () {
1305 tr '\015' '\012'
1306}
1307
37079959
GR
1308test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1309 test_config rebase.missingCommitsCheck warn &&
1310 rebase_setup_and_clean missing-commit &&
1311 set_fake_editor &&
1312 FAKE_LINES="1 2 3 4" \
21d0764c
JS
1313 git rebase -i --root 2>actual.2 &&
1314 cr_to_nl <actual.2 >actual &&
b8fc9e43 1315 test_i18ncmp expect actual &&
37079959
GR
1316 test D = $(git cat-file commit HEAD | sed -ne \$p)
1317'
1318
1319cat >expect <<EOF
1320Warning: some commits may have been dropped accidentally.
1321Dropped commits (newer to older):
1322 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1323 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1324To avoid this message, use "drop" to explicitly remove a commit.
1325
1326Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1327The possible behaviours are: ignore, warn, error.
1328
37875b47 1329You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
37079959
GR
1330Or you can abort the rebase with 'git rebase --abort'.
1331EOF
1332
1333test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1334 test_config rebase.missingCommitsCheck error &&
1335 rebase_setup_and_clean missing-commit &&
1336 set_fake_editor &&
1337 test_must_fail env FAKE_LINES="1 2 4" \
1338 git rebase -i --root 2>actual &&
b8fc9e43 1339 test_i18ncmp expect actual &&
37079959
GR
1340 cp .git/rebase-merge/git-rebase-todo.backup \
1341 .git/rebase-merge/git-rebase-todo &&
1342 FAKE_LINES="1 2 drop 3 4 drop 5" \
1343 git rebase --edit-todo &&
1344 git rebase --continue &&
1345 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1346 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1347'
1348
17959934
LB
1349test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1350 rebase_setup_and_clean abbrevcmd &&
1351 test_commit "first" file1.txt "first line" first &&
1352 test_commit "second" file1.txt "another line" second &&
1353 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1354 test_commit "squash! second" file1.txt "another line here" second_squash &&
1355 cat >expected <<-EOF &&
1356 p $(git rev-list --abbrev-commit -1 first) first
1357 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1358 x git show HEAD
1359 p $(git rev-list --abbrev-commit -1 second) second
1360 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1361 x git show HEAD
1362 EOF
1363 git checkout abbrevcmd &&
1364 set_cat_todo_editor &&
1365 test_config rebase.abbreviateCommands true &&
1366 test_must_fail git rebase -i --exec "git show HEAD" \
1367 --autosquash master >actual &&
1368 test_cmp expected actual
1369'
1370
804098bb
GR
1371test_expect_success 'static check of bad command' '
1372 rebase_setup_and_clean bad-cmd &&
1373 set_fake_editor &&
1374 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1375 git rebase -i --root 2>actual &&
47d4ac01
JS
1376 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1377 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
804098bb
GR
1378 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1379 git rebase --continue &&
1380 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1381 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1382'
1383
1db168ee
MM
1384test_expect_success 'tabs and spaces are accepted in the todolist' '
1385 rebase_setup_and_clean indented-comment &&
1386 write_script add-indent.sh <<-\EOF &&
1387 (
1388 # Turn single spaces into space/tab mix
1389 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1390 printf "\n\t# comment\n #more\n\t # comment\n"
7966230b 1391 ) >"$1.new"
1db168ee
MM
1392 mv "$1.new" "$1"
1393 EOF
1394 test_set_editor "$(pwd)/add-indent.sh" &&
1395 git rebase -i HEAD^^^ &&
1396 test E = $(git cat-file commit HEAD | sed -ne \$p)
1397'
1398
804098bb
GR
1399test_expect_success 'static check of bad SHA-1' '
1400 rebase_setup_and_clean bad-sha &&
1401 set_fake_editor &&
1402 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1403 git rebase -i --root 2>actual &&
47d4ac01
JS
1404 test_i18ngrep "edit XXXXXXX False commit" actual &&
1405 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
804098bb
GR
1406 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1407 git rebase --continue &&
1408 test E = $(git cat-file commit HEAD | sed -ne \$p)
1409'
1410
39743cf5 1411test_expect_success 'editor saves as CR/LF' '
1db25aae
JS
1412 git checkout -b with-crlf &&
1413 write_script add-crs.sh <<-\EOF &&
1414 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1415 mv -f "$1".new "$1"
1416 EOF
1417 (
1418 test_set_editor "$(pwd)/add-crs.sh" &&
1419 git rebase -i HEAD^
1420 )
1421'
1422
6672b9f9
JS
1423SQ="'"
1424test_expect_success 'rebase -i --gpg-sign=<key-id>' '
ed1e5282
JS
1425 test_when_finished "test_might_fail git rebase --abort" &&
1426 set_fake_editor &&
1427 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1428 >out 2>err &&
1429 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1430'
1431
1432test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1433 test_when_finished "test_might_fail git rebase --abort" &&
1434 test_config commit.gpgsign true &&
6672b9f9
JS
1435 set_fake_editor &&
1436 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1437 >out 2>err &&
7ca79dca 1438 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
6672b9f9
JS
1439'
1440
ca3e1826
ES
1441test_expect_success 'valid author header after --root swap' '
1442 rebase_setup_and_clean author-header no-conflict-branch &&
1443 set_fake_editor &&
4aa5ff94
PW
1444 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1445 git cat-file commit HEAD | grep ^author >expected &&
1446 FAKE_LINES="5 1" git rebase -i --root &&
1447 git cat-file commit HEAD^ | grep ^author >actual &&
1448 test_cmp expected actual
1449'
1450
1451test_expect_success 'valid author header when author contains single quote' '
1452 rebase_setup_and_clean author-header no-conflict-branch &&
1453 set_fake_editor &&
1454 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1455 git cat-file commit HEAD | grep ^author >expected &&
1456 FAKE_LINES="2" git rebase -i HEAD~2 &&
1457 git cat-file commit HEAD | grep ^author >actual &&
1458 test_cmp expected actual
ca3e1826
ES
1459'
1460
1b1dce4b 1461test_done