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