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