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