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