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