]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3404-rebase-interactive.sh
sequencer: factor out part of pick_commits()
[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 /
9c8509a4 15 A - B - C - D - E (primary)
99b028e5
JN
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 27'
334afbc7 28
1b1dce4b
JS
29. ./test-lib.sh
30
eaf0551d 31. "$TEST_DIRECTORY"/lib-rebase.sh
29a03348 32
1b1dce4b 33test_expect_success 'setup' '
9c8509a4 34 git switch -C primary &&
163f3925
MH
35 test_commit A file1 &&
36 test_commit B file1 &&
37 test_commit C file2 &&
38 test_commit D file1 &&
39 test_commit E file3 &&
1b1dce4b 40 git checkout -b branch1 A &&
163f3925
MH
41 test_commit F file4 &&
42 test_commit G file1 &&
43 test_commit H file5 &&
1b1dce4b 44 git checkout -b branch2 F &&
2dec68cf 45 test_commit I file6 &&
6c4c44c4 46 git checkout -b conflict-branch A &&
391a825f
JN
47 test_commit one conflict &&
48 test_commit two conflict &&
49 test_commit three conflict &&
50 test_commit four conflict &&
6c4c44c4 51 git checkout -b no-conflict-branch A &&
391a825f
JN
52 test_commit J fileJ &&
53 test_commit K fileK &&
54 test_commit L fileL &&
55 test_commit M fileM &&
b4995494 56 git checkout -b no-ff-branch A &&
391a825f
JN
57 test_commit N fileN &&
58 test_commit O fileO &&
59 test_commit P fileP
1b1dce4b
JS
60'
61
fa90ab4a 62# "exec" commands are run with the user shell by default, but this may
cd035b1c 63# be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
386aad5a 64# to create a file. Unsetting SHELL avoids such non-portable behavior
5cd3e107 65# in tests. It must be exported for it to take effect where needed.
cd035b1c 66SHELL=
5cd3e107 67export SHELL
cd035b1c 68
bd46cfae 69test_expect_success 'rebase --keep-empty' '
9c8509a4 70 git checkout -b emptybranch primary &&
bd46cfae
MT
71 git commit --allow-empty -m "empty" &&
72 git rebase --keep-empty -i HEAD~2 &&
73 git log --oneline >actual &&
74 test_line_count = 6 actual
75'
76
a9ae8fde 77test_expect_success 'rebase -i with empty todo list' '
c9749b36
SG
78 cat >expect <<-\EOF &&
79 error: nothing to do
80 EOF
b2dbacbd
PW
81 (
82 set_fake_editor &&
a9ae8fde
EN
83 test_must_fail env FAKE_LINES="#" \
84 git rebase -i HEAD^ >output 2>&1
b2dbacbd 85 ) &&
a9ae8fde 86 tail -n 1 output >actual && # Ignore output about changing todo list
1108cea7 87 test_cmp expect actual
d078c391
AG
88'
89
cd035b1c 90test_expect_success 'rebase -i with the exec command' '
9c8509a4 91 git checkout primary &&
cd035b1c 92 (
31cd8275 93 set_fake_editor &&
cd035b1c
MM
94 FAKE_LINES="1 exec_>touch-one
95 2 exec_>touch-two exec_false exec_>touch-three
96 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
97 export FAKE_LINES &&
98 test_must_fail git rebase -i A
99 ) &&
2caf20c5
MM
100 test_path_is_file touch-one &&
101 test_path_is_file touch-two &&
45a26864
ÆAB
102 # Missing because we should have stopped by now.
103 test_path_is_missing touch-three &&
5c947e2c 104 test_cmp_rev C HEAD &&
cd035b1c 105 git rebase --continue &&
2caf20c5
MM
106 test_path_is_file touch-three &&
107 test_path_is_file "touch-file name with spaces" &&
108 test_path_is_file touch-after-semicolon &&
9c8509a4 109 test_cmp_rev primary HEAD &&
cd035b1c
MM
110 rm -f touch-*
111'
112
113test_expect_success 'rebase -i with the exec command runs from tree root' '
9c8509a4 114 git checkout primary &&
c2e0940b 115 mkdir subdir && (cd subdir &&
31cd8275 116 set_fake_editor &&
cd035b1c 117 FAKE_LINES="1 exec_>touch-subdir" \
c2e0940b
JL
118 git rebase -i HEAD^
119 ) &&
2caf20c5 120 test_path_is_file touch-subdir &&
cd035b1c
MM
121 rm -fr subdir
122'
123
09d7b6c6
JK
124test_expect_success 'rebase -i with exec allows git commands in subdirs' '
125 test_when_finished "rm -rf subdir" &&
126 test_when_finished "git rebase --abort ||:" &&
9c8509a4 127 git checkout primary &&
09d7b6c6
JK
128 mkdir subdir && (cd subdir &&
129 set_fake_editor &&
8c64bc94 130 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
09d7b6c6
JK
131 git rebase -i HEAD^
132 )
133'
134
ab5e67d7 135test_expect_success 'rebase -i sets work tree properly' '
136 test_when_finished "rm -rf subdir" &&
137 test_when_finished "test_might_fail git rebase --abort" &&
138 mkdir subdir &&
139 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
140 >actual &&
141 ! grep "/subdir$" actual
142'
143
cd035b1c 144test_expect_success 'rebase -i with the exec command checks tree cleanness' '
9c8509a4 145 git checkout primary &&
b2dbacbd
PW
146 (
147 set_fake_editor &&
148 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
149 git rebase -i HEAD^
150 ) &&
9c8509a4 151 test_cmp_rev primary^ HEAD &&
cd035b1c
MM
152 git reset --hard &&
153 git rebase --continue
154'
155
c762aada
PW
156test_expect_success 'rebase -x with empty command fails' '
157 test_when_finished "git rebase --abort ||:" &&
d03ebd41 158 test_must_fail env git rebase -x "" @ 2>actual &&
c762aada 159 test_write_lines "error: empty exec command" >expected &&
1108cea7 160 test_cmp expected actual &&
d03ebd41 161 test_must_fail env git rebase -x " " @ 2>actual &&
1108cea7 162 test_cmp expected actual
c762aada
PW
163'
164
c762aada
PW
165test_expect_success 'rebase -x with newline in command fails' '
166 test_when_finished "git rebase --abort ||:" &&
d03ebd41 167 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
c762aada
PW
168 test_write_lines "error: exec commands cannot contain newlines" \
169 >expected &&
1108cea7 170 test_cmp expected actual
c762aada
PW
171'
172
ecfe1ea9 173test_expect_success 'rebase -i with exec of inexistent command' '
9c8509a4 174 git checkout primary &&
ecfe1ea9 175 test_when_finished "git rebase --abort" &&
b2dbacbd
PW
176 (
177 set_fake_editor &&
178 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
179 git rebase -i HEAD^ >actual 2>&1
180 ) &&
ecfe1ea9
JS
181 ! grep "Maybe git-rebase is broken" actual
182'
183
891d4a03
PW
184test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
185 test_when_finished "git rebase --abort ||:" &&
186 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
187'
188
1b1dce4b 189test_expect_success 'no changes are a nop' '
6c4c44c4 190 git checkout branch2 &&
1b1dce4b 191 git rebase -i F &&
ab736792 192 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
780308d0 193 test_cmp_rev I HEAD
1b1dce4b
JS
194'
195
c9e65892
JS
196test_expect_success 'test the [branch] option' '
197 git checkout -b dead-end &&
198 git rm file6 &&
199 git commit -m "stop here" &&
200 git rebase -i F branch2 &&
ab736792 201 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
780308d0
PW
202 test_cmp_rev I branch2 &&
203 test_cmp_rev I HEAD
c9e65892
JS
204'
205
ab736792
SB
206test_expect_success 'test --onto <branch>' '
207 git checkout -b test-onto branch2 &&
208 git rebase -i --onto branch1 F &&
209 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
780308d0
PW
210 test_cmp_rev HEAD^ branch1 &&
211 test_cmp_rev I branch2
ab736792
SB
212'
213
1b1dce4b
JS
214test_expect_success 'rebase on top of a non-conflicting commit' '
215 git checkout branch1 &&
216 git tag original-branch1 &&
217 git rebase -i branch2 &&
218 test file6 = $(git diff --name-only original-branch1) &&
ab736792 219 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
780308d0
PW
220 test_cmp_rev I branch2 &&
221 test_cmp_rev I HEAD~2
1b1dce4b
JS
222'
223
68a163c9 224test_expect_success 'reflog for the branch shows state before rebase' '
780308d0 225 test_cmp_rev branch1@{1} original-branch1
68a163c9
JS
226'
227
1ceb9dfa 228test_expect_success 'reflog for the branch shows correct finish message' '
c2417d3a 229 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
1ceb9dfa
PW
230 "$(git rev-parse branch2)" >expected &&
231 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
232 test_cmp expected actual
233'
234
1b1dce4b 235test_expect_success 'exchange two commits' '
b2dbacbd
PW
236 (
237 set_fake_editor &&
238 FAKE_LINES="2 1" git rebase -i HEAD~2
239 ) &&
b4ce54fc 240 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
4dd1b5f9 241 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
242 blob1=$(git rev-parse --short HEAD^:file1) &&
243 blob2=$(git rev-parse --short HEAD:file1) &&
244 commit=$(git rev-parse --short HEAD)
1b1dce4b
JS
245'
246
1b1dce4b 247test_expect_success 'stop on conflicting pick' '
4dd1b5f9 248 cat >expect <<-EOF &&
c9749b36 249 diff --git a/file1 b/file1
4dd1b5f9 250 index $blob1..$blob2 100644
c9749b36
SG
251 --- a/file1
252 +++ b/file1
253 @@ -1 +1 @@
254 -A
255 +G
256 EOF
4dd1b5f9 257 cat >expect2 <<-EOF &&
c9749b36
SG
258 <<<<<<< HEAD
259 D
260 =======
261 G
7d056dea 262 >>>>>>> $commit (G)
c9749b36 263 EOF
1b1dce4b 264 git tag new-branch1 &&
9c8509a4
JS
265 test_must_fail git rebase -i primary &&
266 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
28ed6e7b 267 test_cmp expect .git/rebase-merge/patch &&
82ebb0b6 268 test_cmp expect2 file1 &&
0cb0e143 269 test "$(git diff --name-status |
ab736792 270 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
28ed6e7b
JS
271 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
272 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
1b1dce4b
JS
273'
274
66335298
NTND
275test_expect_success 'show conflicted patch' '
276 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
fbd7a232
NTND
277 grep "show.*REBASE_HEAD" stderr &&
278 # the original stopped-sha1 is abbreviated
279 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
280 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
66335298
NTND
281'
282
1b1dce4b
JS
283test_expect_success 'abort' '
284 git rebase --abort &&
780308d0 285 test_cmp_rev new-branch1 HEAD &&
ab736792 286 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
2caf20c5 287 test_path_is_missing .git/rebase-merge
1b1dce4b
JS
288'
289
b096374f
IWC
290test_expect_success 'abort with error when new base cannot be checked out' '
291 git rm --cached file1 &&
292 git commit -m "remove file in base" &&
9c8509a4 293 test_must_fail git rebase -i primary > output 2>&1 &&
e5c1272c 294 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
b096374f 295 output &&
e5c1272c 296 test_i18ngrep "file1" output &&
2caf20c5 297 test_path_is_missing .git/rebase-merge &&
f222bd34 298 rm file1 &&
b096374f
IWC
299 git reset --hard HEAD^
300'
301
1b1dce4b
JS
302test_expect_success 'retain authorship' '
303 echo A > file7 &&
304 git add file7 &&
c54b7817 305 test_tick &&
1b1dce4b
JS
306 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
307 git tag twerp &&
9c8509a4 308 git rebase -i --onto primary HEAD^ &&
1b1dce4b
JS
309 git show HEAD | grep "^Author: Twerp Snog"
310'
311
a2a20b0d 312test_expect_success 'retain authorship w/ conflicts' '
650161a2
JH
313 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
314 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
315
a2a20b0d
JS
316 git reset --hard twerp &&
317 test_commit a conflict a conflict-a &&
318 git reset --hard twerp &&
650161a2
JH
319
320 GIT_AUTHOR_NAME=AttributeMe &&
321 export GIT_AUTHOR_NAME &&
a2a20b0d 322 test_commit b conflict b conflict-b &&
650161a2
JH
323 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
324
a2a20b0d
JS
325 test_must_fail git rebase -i conflict-a &&
326 echo resolved >conflict &&
327 git add conflict &&
328 git rebase --continue &&
780308d0 329 test_cmp_rev conflict-a^0 HEAD^ &&
a2a20b0d
JS
330 git show >out &&
331 grep AttributeMe out
332'
333
1b1dce4b
JS
334test_expect_success 'squash' '
335 git reset --hard twerp &&
336 echo B > file7 &&
c54b7817 337 test_tick &&
1b1dce4b
JS
338 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
339 echo "******************************" &&
b2dbacbd
PW
340 (
341 set_fake_editor &&
342 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
9c8509a4 343 git rebase -i --onto primary HEAD~2
b2dbacbd 344 ) &&
1b1dce4b 345 test B = $(cat file7) &&
9c8509a4 346 test_cmp_rev HEAD^ primary
1b1dce4b
JS
347'
348
349test_expect_success 'retain authorship when squashing' '
81ab1cb4 350 git show HEAD | grep "^Author: Twerp Snog"
1b1dce4b
JS
351'
352
18640d99 353test_expect_success '--continue tries to commit' '
6c8fbae6 354 git reset --hard D &&
18640d99 355 test_tick &&
b2dbacbd
PW
356 (
357 set_fake_editor &&
358 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
359 echo resolved > file1 &&
360 git add file1 &&
361 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
362 ) &&
780308d0 363 test_cmp_rev HEAD^ new-branch1 &&
18640d99
JS
364 git show HEAD | grep chouette
365'
366
8e4a91bd 367test_expect_success 'verbose flag is heeded, even after --continue' '
9c8509a4 368 git reset --hard primary@{1} &&
8e4a91bd 369 test_tick &&
ab736792 370 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
8e4a91bd
JS
371 echo resolved > file1 &&
372 git add file1 &&
373 git rebase --continue > output &&
dc801e71 374 grep "^ file1 | 2 +-$" output
8e4a91bd
JS
375'
376
a926c4b9 377test_expect_success 'multi-squash only fires up editor once' '
6368f3f8 378 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
379 (
380 set_fake_editor &&
381 FAKE_COMMIT_AMEND="ONCE" \
382 FAKE_LINES="1 squash 2 squash 3 squash 4" \
383 EXPECT_HEADER_COUNT=4 \
384 git rebase -i $base
385 ) &&
6368f3f8
JS
386 test $base = $(git rev-parse HEAD^) &&
387 test 1 = $(git show | grep ONCE | wc -l)
388'
389
a926c4b9 390test_expect_success 'multi-fixup does not fire up editor' '
0205e72f
MH
391 git checkout -b multi-fixup E &&
392 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
393 (
394 set_fake_editor &&
395 FAKE_COMMIT_AMEND="NEVER" \
396 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
397 git rebase -i $base
398 ) &&
0205e72f 399 test $base = $(git rev-parse HEAD^) &&
a25eb139 400 test 0 = $(git show | grep NEVER | wc -l) &&
6c8fbae6 401 git checkout @{-1} &&
0205e72f
MH
402 git branch -D multi-fixup
403'
404
6bdcd0d2
MH
405test_expect_success 'commit message used after conflict' '
406 git checkout -b conflict-fixup conflict-branch &&
407 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
408 (
409 set_fake_editor &&
410 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
411 git rebase -i $base &&
412 echo three > conflict &&
413 git add conflict &&
414 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
415 git rebase --continue
416 ) &&
6bdcd0d2
MH
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 419 git checkout @{-1} &&
6bdcd0d2
MH
420 git branch -D conflict-fixup
421'
422
423test_expect_success 'commit message retained after conflict' '
424 git checkout -b conflict-squash conflict-branch &&
425 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
426 (
427 set_fake_editor &&
428 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
429 git rebase -i $base &&
430 echo three > conflict &&
431 git add conflict &&
432 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
433 git rebase --continue
434 ) &&
6bdcd0d2
MH
435 test $base = $(git rev-parse HEAD^) &&
436 test 2 = $(git show | grep TWICE | wc -l) &&
6c8fbae6 437 git checkout @{-1} &&
6bdcd0d2
MH
438 git branch -D conflict-squash
439'
440
a926c4b9 441test_expect_success 'squash and fixup generate correct log messages' '
c9749b36
SG
442 cat >expect-squash-fixup <<-\EOF &&
443 B
0205e72f 444
c9749b36 445 D
0205e72f 446
c9749b36
SG
447 ONCE
448 EOF
0205e72f
MH
449 git checkout -b squash-fixup E &&
450 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
451 (
452 set_fake_editor &&
453 FAKE_COMMIT_AMEND="ONCE" \
454 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
455 EXPECT_HEADER_COUNT=4 \
456 git rebase -i $base
457 ) &&
0205e72f
MH
458 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
459 test_cmp expect-squash-fixup actual-squash-fixup &&
60b61588
PW
460 git cat-file commit HEAD@{2} |
461 grep "^# This is a combination of 3 commits\." &&
462 git cat-file commit HEAD@{3} |
463 grep "^# This is a combination of 2 commits\." &&
6c8fbae6 464 git checkout @{-1} &&
0205e72f
MH
465 git branch -D squash-fixup
466'
467
a926c4b9 468test_expect_success 'squash ignores comments' '
234b3dae
MH
469 git checkout -b skip-comments E &&
470 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
471 (
472 set_fake_editor &&
473 FAKE_COMMIT_AMEND="ONCE" \
474 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
475 EXPECT_HEADER_COUNT=4 \
476 git rebase -i $base
477 ) &&
234b3dae
MH
478 test $base = $(git rev-parse HEAD^) &&
479 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 480 git checkout @{-1} &&
234b3dae
MH
481 git branch -D skip-comments
482'
483
a926c4b9 484test_expect_success 'squash ignores blank lines' '
234b3dae
MH
485 git checkout -b skip-blank-lines E &&
486 base=$(git rev-parse HEAD~4) &&
b2dbacbd
PW
487 (
488 set_fake_editor &&
489 FAKE_COMMIT_AMEND="ONCE" \
490 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
491 EXPECT_HEADER_COUNT=4 \
492 git rebase -i $base
493 ) &&
234b3dae
MH
494 test $base = $(git rev-parse HEAD^) &&
495 test 1 = $(git show | grep ONCE | wc -l) &&
6c8fbae6 496 git checkout @{-1} &&
234b3dae
MH
497 git branch -D skip-blank-lines
498'
499
fb47cfbd 500test_expect_success 'squash works as expected' '
6c4c44c4 501 git checkout -b squash-works no-conflict-branch &&
fb47cfbd 502 one=$(git rev-parse HEAD~3) &&
b2dbacbd
PW
503 (
504 set_fake_editor &&
505 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
506 ) &&
fb47cfbd
JS
507 test $one = $(git rev-parse HEAD~2)
508'
509
510test_expect_success 'interrupted squash works as expected' '
6c4c44c4 511 git checkout -b interrupted-squash conflict-branch &&
fb47cfbd 512 one=$(git rev-parse HEAD~3) &&
b2dbacbd
PW
513 (
514 set_fake_editor &&
515 test_must_fail env FAKE_LINES="1 squash 3 2" \
516 git rebase -i HEAD~3
517 ) &&
0590ff26 518 test_write_lines one two four > conflict &&
fb47cfbd 519 git add conflict &&
ab736792 520 test_must_fail git rebase --continue &&
fb47cfbd
JS
521 echo resolved > conflict &&
522 git add conflict &&
523 git rebase --continue &&
524 test $one = $(git rev-parse HEAD~2)
525'
526
1d25c8cf 527test_expect_success 'interrupted squash works as expected (case 2)' '
6c4c44c4 528 git checkout -b interrupted-squash2 conflict-branch &&
1d25c8cf 529 one=$(git rev-parse HEAD~3) &&
b2dbacbd
PW
530 (
531 set_fake_editor &&
532 test_must_fail env FAKE_LINES="3 squash 1 2" \
533 git rebase -i HEAD~3
534 ) &&
0590ff26 535 test_write_lines one four > conflict &&
1d25c8cf 536 git add conflict &&
ab736792 537 test_must_fail git rebase --continue &&
0590ff26 538 test_write_lines one two four > conflict &&
1d25c8cf 539 git add conflict &&
ab736792 540 test_must_fail git rebase --continue &&
1d25c8cf
JS
541 echo resolved > conflict &&
542 git add conflict &&
543 git rebase --continue &&
544 test $one = $(git rev-parse HEAD~2)
545'
546
3f213981 547test_expect_success '--continue tries to commit, even for "edit"' '
96ffe892
JS
548 echo unrelated > file7 &&
549 git add file7 &&
550 test_tick &&
551 git commit -m "unrelated change" &&
be6ff208
JS
552 parent=$(git rev-parse HEAD^) &&
553 test_tick &&
b2dbacbd
PW
554 (
555 set_fake_editor &&
556 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
557 echo edited > file7 &&
558 git add file7 &&
559 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
560 ) &&
be6ff208
JS
561 test edited = $(git show HEAD:file7) &&
562 git show HEAD | grep chouette &&
563 test $parent = $(git rev-parse HEAD^)
564'
565
dc7f55cb
SB
566test_expect_success 'aborted --continue does not squash commits after "edit"' '
567 old=$(git rev-parse HEAD) &&
568 test_tick &&
b2dbacbd
PW
569 (
570 set_fake_editor &&
571 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
572 echo "edited again" > file7 &&
573 git add file7 &&
574 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
575 ) &&
a3ec9eaf
EN
576 test $old = $(git rev-parse HEAD) &&
577 git rebase --abort
dc7f55cb
SB
578'
579
f8aa1b69
SB
580test_expect_success 'auto-amend only edited commits after "edit"' '
581 test_tick &&
b2dbacbd
PW
582 (
583 set_fake_editor &&
584 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
585 echo "edited again" > file7 &&
586 git add file7 &&
587 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
588 echo "and again" > file7 &&
589 git add file7 &&
590 test_tick &&
591 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
592 git rebase --continue
593 ) &&
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 || :" &&
b2dbacbd
PW
600 (
601 set_fake_editor &&
602 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
603 ) &&
ffaaed88
MM
604 echo "edited again" > file7 &&
605 git add file7 &&
606 test_must_fail git rebase --continue 2>error &&
18633e1a 607 test_i18ngrep "you have staged changes in your working tree" error
ffaaed88
MM
608'
609
73697a0b
JS
610test_expect_success 'rebase a detached HEAD' '
611 grandparent=$(git rev-parse HEAD~2) &&
612 git checkout $(git rev-parse HEAD) &&
613 test_tick &&
b2dbacbd
PW
614 (
615 set_fake_editor &&
616 FAKE_LINES="2 1" git rebase -i HEAD~2
617 ) &&
73697a0b
JS
618 test $grandparent = $(git rev-parse HEAD~2)
619'
620
752527f5 621test_expect_success 'rebase a commit violating pre-commit' '
bef805b7 622 test_hook pre-commit <<-\EOF &&
7bec7f50
JH
623 test -z "$(git diff --cached --check)"
624 EOF
752527f5
JS
625 echo "monde! " >> file1 &&
626 test_tick &&
ab736792 627 test_must_fail git commit -m doesnt-verify file1 &&
752527f5
JS
628 git commit -m doesnt-verify --no-verify file1 &&
629 test_tick &&
b2dbacbd
PW
630 (
631 set_fake_editor &&
632 FAKE_LINES=2 git rebase -i HEAD~2
633 )
752527f5
JS
634'
635
077b725f 636test_expect_success 'rebase with a file named HEAD in worktree' '
077b725f
JH
637 git reset --hard &&
638 git checkout -b branch3 A &&
639
640 (
641 GIT_AUTHOR_NAME="Squashed Away" &&
642 export GIT_AUTHOR_NAME &&
643 >HEAD &&
644 git add HEAD &&
645 git commit -m "Add head" &&
646 >BODY &&
647 git add BODY &&
648 git commit -m "Add body"
649 ) &&
650
b2dbacbd
PW
651 (
652 set_fake_editor &&
653 FAKE_LINES="1 squash 2" git rebase -i @{-1}
654 ) &&
077b725f
JH
655 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
656
657'
658
ff74126c
JS
659test_expect_success 'do "noop" when there is nothing to cherry-pick' '
660
661 git checkout -b branch4 HEAD &&
662 GIT_EDITOR=: git commit --amend \
2dec68cf 663 --author="Somebody else <somebody@else.com>" &&
ff74126c
JS
664 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
665 git rebase -i branch3 &&
780308d0 666 test_cmp_rev branch3 branch4
ff74126c
JS
667
668'
669
96747696
JH
670test_expect_success 'submodule rebase setup' '
671 git checkout A &&
672 mkdir sub &&
673 (
674 cd sub && git init && >elif &&
675 git add elif && git commit -m "submodule initial"
676 ) &&
677 echo 1 >file1 &&
2dec68cf 678 git add file1 sub &&
96747696
JH
679 test_tick &&
680 git commit -m "One" &&
681 echo 2 >file1 &&
682 test_tick &&
683 git commit -a -m "Two" &&
684 (
685 cd sub && echo 3 >elif &&
686 git commit -a -m "submodule second"
687 ) &&
688 test_tick &&
689 git commit -a -m "Three changes submodule"
690'
691
94c88ede 692test_expect_success 'submodule rebase -i' '
b2dbacbd
PW
693 (
694 set_fake_editor &&
695 FAKE_LINES="1 squash 2 3" git rebase -i A
696 )
96747696
JH
697'
698
a6754cda
JK
699test_expect_success 'submodule conflict setup' '
700 git tag submodule-base &&
701 git checkout HEAD^ &&
702 (
703 cd sub && git checkout HEAD^ && echo 4 >elif &&
704 git add elif && git commit -m "submodule conflict"
705 ) &&
706 git add sub &&
707 test_tick &&
708 git commit -m "Conflict in submodule" &&
709 git tag submodule-topic
710'
711
712test_expect_success 'rebase -i continue with only submodule staged' '
713 test_must_fail git rebase -i submodule-base &&
714 git add sub &&
715 git rebase --continue &&
716 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
717'
718
719test_expect_success 'rebase -i continue with unstaged submodule' '
720 git checkout submodule-topic &&
721 git reset --hard &&
722 test_must_fail git rebase -i submodule-base &&
723 git reset &&
724 git rebase --continue &&
780308d0 725 test_cmp_rev submodule-base HEAD
a6754cda
JK
726'
727
0e757e30 728test_expect_success 'avoid unnecessary reset' '
9c8509a4 729 git checkout primary &&
a6754cda 730 git reset --hard &&
0e496492 731 test-tool chmtime =123456789 file3 &&
0e757e30
JS
732 git update-index --refresh &&
733 HEAD=$(git rev-parse HEAD) &&
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 740test_expect_success 'reword' '
9c8509a4 741 git checkout -b reword-branch primary &&
b2dbacbd
PW
742 (
743 set_fake_editor &&
744 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
745 git rebase -i A &&
746 git show HEAD | grep "E changed" &&
9c8509a4
JS
747 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
748 test_cmp_rev primary^ HEAD^ &&
b2dbacbd
PW
749 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
750 git rebase -i A &&
751 git show HEAD^ | grep "D changed" &&
752 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
753 git rebase -i A &&
754 git show HEAD~3 | grep "B changed" &&
755 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
756 git rebase -i A
757 ) &&
6741aa6c
BG
758 git show HEAD~2 | grep "C changed"
759'
760
0c164ae7
PW
761test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
762 git checkout E &&
763 test_when_finished "git checkout @{-1}" &&
764 (
765 set_fake_editor &&
766 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
767 export GIT_SEQUENCE_EDITOR &&
768 set_reword_editor &&
769 FAKE_LINES="reword 1 reword 2" git rebase -i C
770 ) &&
771 check_reworded_commits D E
772'
773
eb2151bb
TR
774test_expect_success 'rebase -i can copy notes' '
775 git config notes.rewrite.rebase true &&
776 git config notes.rewriteRef "refs/notes/*" &&
777 test_commit n1 &&
778 test_commit n2 &&
779 test_commit n3 &&
780 git notes add -m"a note" n3 &&
6f87f038 781 git rebase -i --onto n1 n2 &&
eb2151bb
TR
782 test "a note" = "$(git notes show HEAD)"
783'
784
eb2151bb 785test_expect_success 'rebase -i can copy notes over a fixup' '
c9749b36
SG
786 cat >expect <<-\EOF &&
787 an earlier note
788
789 a note
790 EOF
eb2151bb
TR
791 git reset --hard n3 &&
792 git notes add -m"an earlier note" n2 &&
b2dbacbd
PW
793 (
794 set_fake_editor &&
795 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
796 git rebase -i n1
797 ) &&
eb2151bb
TR
798 git notes show > output &&
799 test_cmp expect output
800'
801
2ec33cdd
DO
802test_expect_success 'rebase while detaching HEAD' '
803 git symbolic-ref HEAD &&
804 grandparent=$(git rev-parse HEAD~2) &&
805 test_tick &&
b2dbacbd
PW
806 (
807 set_fake_editor &&
808 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
809 ) &&
2ec33cdd
DO
810 test $grandparent = $(git rev-parse HEAD~2) &&
811 test_must_fail git symbolic-ref HEAD
812'
813
b4995494
MB
814test_tick # Ensure that the rebased commits get a different timestamp.
815test_expect_success 'always cherry-pick with --no-ff' '
816 git checkout no-ff-branch &&
817 git tag original-no-ff-branch &&
818 git rebase -i --no-ff A &&
b4995494
MB
819 for p in 0 1 2
820 do
821 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
822 git diff HEAD~$p original-no-ff-branch~$p > out &&
db5875aa 823 test_must_be_empty out || return 1
b4995494 824 done &&
780308d0 825 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
b4995494 826 git diff HEAD~3 original-no-ff-branch~3 > out &&
1c5e94f4 827 test_must_be_empty out
b4995494
MB
828'
829
57f2b6b2
JH
830test_expect_success 'set up commits with funny messages' '
831 git checkout -b funny A &&
832 echo >>file1 &&
833 test_tick &&
834 git commit -a -m "end with slash\\" &&
835 echo >>file1 &&
836 test_tick &&
938791cd
BC
837 git commit -a -m "something (\000) that looks like octal" &&
838 echo >>file1 &&
839 test_tick &&
840 git commit -a -m "something (\n) that looks like a newline" &&
841 echo >>file1 &&
842 test_tick &&
57f2b6b2
JH
843 git commit -a -m "another commit"
844'
845
846test_expect_success 'rebase-i history with funny messages' '
847 git rev-list A..funny >expect &&
848 test_tick &&
b2dbacbd
PW
849 (
850 set_fake_editor &&
851 FAKE_LINES="1 2 3 4" git rebase -i A
852 ) &&
57f2b6b2
JH
853 git rev-list A.. >actual &&
854 test_cmp expect actual
855'
856
c2145384 857test_expect_success 'prepare for rebase -i --exec' '
9c8509a4 858 git checkout primary &&
c2145384
LK
859 git checkout -b execute &&
860 test_commit one_exec main.txt one_exec &&
861 test_commit two_exec main.txt two_exec &&
862 test_commit three_exec main.txt three_exec
863'
864
c2145384 865test_expect_success 'running "git rebase -i --exec git show HEAD"' '
c2145384 866 (
b2dbacbd
PW
867 set_fake_editor &&
868 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
c2145384
LK
869 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
870 export FAKE_LINES &&
871 git rebase -i HEAD~2 >expect
872 ) &&
873 sed -e "1,9d" expect >expected &&
874 test_cmp expected actual
875'
876
c2145384
LK
877test_expect_success 'running "git rebase --exec git show HEAD -i"' '
878 git reset --hard execute &&
c2145384 879 (
b2dbacbd
PW
880 set_fake_editor &&
881 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
c2145384
LK
882 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
883 export FAKE_LINES &&
884 git rebase -i HEAD~2 >expect
885 ) &&
886 sed -e "1,9d" expect >expected &&
887 test_cmp expected actual
888'
889
c2145384
LK
890test_expect_success 'running "git rebase -ix git show HEAD"' '
891 git reset --hard execute &&
c2145384 892 (
b2dbacbd
PW
893 set_fake_editor &&
894 git rebase -ix "git show HEAD" HEAD~2 >actual &&
c2145384
LK
895 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
896 export FAKE_LINES &&
897 git rebase -i HEAD~2 >expect
898 ) &&
899 sed -e "1,9d" expect >expected &&
900 test_cmp expected actual
901'
902
903
904test_expect_success 'rebase -ix with several <CMD>' '
905 git reset --hard execute &&
c2145384 906 (
b2dbacbd
PW
907 set_fake_editor &&
908 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
c2145384
LK
909 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
910 export FAKE_LINES &&
911 git rebase -i HEAD~2 >expect
912 ) &&
913 sed -e "1,9d" expect >expected &&
914 test_cmp expected actual
915'
916
c2145384
LK
917test_expect_success 'rebase -ix with several instances of --exec' '
918 git reset --hard execute &&
c2145384 919 (
b2dbacbd
PW
920 set_fake_editor &&
921 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
c2145384
LK
922 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
923 exec_git_show_HEAD exec_pwd" &&
924 export FAKE_LINES &&
925 git rebase -i HEAD~2 >expect
926 ) &&
927 sed -e "1,11d" expect >expected &&
928 test_cmp expected actual
929'
930
a926c4b9 931test_expect_success 'rebase -ix with --autosquash' '
c2145384
LK
932 git reset --hard execute &&
933 git checkout -b autosquash &&
934 echo second >second.txt &&
935 git add second.txt &&
936 git commit -m "fixup! two_exec" &&
937 echo bis >bis.txt &&
938 git add bis.txt &&
939 git commit -m "fixup! two_exec" &&
88a92b6c
PW
940 git checkout -b autosquash_actual &&
941 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
c2145384
LK
942 git checkout autosquash &&
943 (
b2dbacbd 944 set_fake_editor &&
c2145384
LK
945 git checkout -b autosquash_expected &&
946 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
947 export FAKE_LINES &&
948 git rebase -i HEAD~4 >expect
949 ) &&
950 sed -e "1,13d" expect >expected &&
951 test_cmp expected actual
952'
953
78ec2400 954test_expect_success 'rebase --exec works without -i ' '
c2145384 955 git reset --hard execute &&
78ec2400
SB
956 rm -rf exec_output &&
957 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
958 test_i18ngrep "Successfully rebased and updated" actual &&
959 test_line_count = 2 exec_output &&
960 test_path_is_missing invoked_editor
c2145384
LK
961'
962
c2145384
LK
963test_expect_success 'rebase -i --exec without <CMD>' '
964 git reset --hard execute &&
3bb0923f
PSU
965 test_must_fail git rebase -i --exec 2>actual &&
966 test_i18ngrep "requires a value" actual &&
9c8509a4 967 git checkout primary
c2145384
LK
968'
969
b64b7feb
CW
970test_expect_success 'rebase -i --root re-order and drop commits' '
971 git checkout E &&
b2dbacbd
PW
972 (
973 set_fake_editor &&
974 FAKE_LINES="3 1 2 5" git rebase -i --root
975 ) &&
b64b7feb
CW
976 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
977 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
978 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
979 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
980 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
981'
982
983test_expect_success 'rebase -i --root retain root commit author and message' '
984 git checkout A &&
985 echo B >file7 &&
986 git add file7 &&
987 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
b2dbacbd
PW
988 (
989 set_fake_editor &&
990 FAKE_LINES="2" git rebase -i --root
991 ) &&
b64b7feb
CW
992 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
993 git cat-file commit HEAD | grep -q "^different author$"
994'
995
996test_expect_success 'rebase -i --root temporary sentinel commit' '
997 git checkout B &&
b2dbacbd
PW
998 (
999 set_fake_editor &&
1000 test_must_fail env FAKE_LINES="2" git rebase -i --root
1001 ) &&
4dd1b5f9 1002 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
b64b7feb
CW
1003 git rebase --abort
1004'
1005
2147f844
CW
1006test_expect_success 'rebase -i --root fixup root commit' '
1007 git checkout B &&
b2dbacbd
PW
1008 (
1009 set_fake_editor &&
1010 FAKE_LINES="1 fixup 2" git rebase -i --root
1011 ) &&
2147f844
CW
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" &&
9c8509a4 1019 git checkout -b reword-original-root-branch primary &&
b2dbacbd
PW
1020 (
1021 set_fake_editor &&
1022 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1023 git rebase -i --root
1024 ) &&
87881055
TZ
1025 git show HEAD^ | grep "A changed" &&
1026 test -z "$(git show -s --format=%p HEAD^)"
3a36ca08
TZ
1027'
1028
450efe2d
PW
1029test_expect_success 'rebase -i --root reword new root commit' '
1030 test_when_finished "test_might_fail git rebase --abort" &&
9c8509a4 1031 git checkout -b reword-now-root-branch primary &&
b2dbacbd
PW
1032 (
1033 set_fake_editor &&
1034 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1035 git rebase -i --root
1036 ) &&
450efe2d
PW
1037 git show HEAD^ | grep "C changed" &&
1038 test -z "$(git show -s --format=%p HEAD^)"
1039'
1040
cc8d872e 1041test_expect_success 'rebase -i --root when root has untracked file conflict' '
a9279c67
PW
1042 test_when_finished "reset_rebase" &&
1043 git checkout -b failing-root-pick A &&
1044 echo x >file2 &&
1045 git rm file1 &&
1046 git commit -m "remove file 1 add file 2" &&
1047 echo z >file1 &&
b2dbacbd
PW
1048 (
1049 set_fake_editor &&
1050 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1051 ) &&
a9279c67
PW
1052 rm file1 &&
1053 git rebase --continue &&
1054 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1055 test "$(git rev-list --count HEAD)" = 2
1056'
1057
1058test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1059 test_when_finished "reset_rebase" &&
1060 echo z>file1 &&
b2dbacbd
PW
1061 (
1062 set_fake_editor &&
1063 test_must_fail env FAKE_LINES="reword 1 2" \
1064 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1065 rm file1 &&
1066 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1067 ) &&
a9279c67
PW
1068 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1069 test "$(git rev-list --count HEAD)" = 2
1070'
1071
a926c4b9 1072test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
450efe2d 1073 git checkout reword-original-root-branch &&
9f4981ba
AW
1074 git reset --hard &&
1075 git checkout conflict-branch &&
b2dbacbd
PW
1076 (
1077 set_fake_editor &&
10cdb9f3 1078 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
b2dbacbd
PW
1079 test_must_fail git rebase --edit-todo
1080 ) &&
9f4981ba
AW
1081 git rebase --abort
1082'
1083
1084test_expect_success 'rebase --edit-todo can be used to modify todo' '
1085 git reset --hard &&
1086 git checkout no-conflict-branch^0 &&
b2dbacbd
PW
1087 (
1088 set_fake_editor &&
1089 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1090 FAKE_LINES="2 1" git rebase --edit-todo &&
1091 git rebase --continue
1092 ) &&
9f4981ba
AW
1093 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1094 test L = $(git cat-file commit HEAD | sed -ne \$p)
1095'
1096
26cd160c
RR
1097test_expect_success 'rebase -i produces readable reflog' '
1098 git reset --hard &&
1099 git branch -f branch-reflog-test H &&
1100 git rebase -i --onto I F branch-reflog-test &&
1101 cat >expect <<-\EOF &&
c2417d3a
EN
1102 rebase (finish): returning to refs/heads/branch-reflog-test
1103 rebase (pick): H
1104 rebase (pick): G
1105 rebase (start): checkout I
26cd160c 1106 EOF
d0ab0584
DT
1107 git reflog -n4 HEAD |
1108 sed "s/[^:]*: //" >actual &&
26cd160c
RR
1109 test_cmp expect actual
1110'
1111
180bad3d
JK
1112test_expect_success 'rebase -i respects core.commentchar' '
1113 git reset --hard &&
1114 git checkout E^0 &&
aac6c2f4 1115 test_config core.commentchar "\\" &&
180bad3d
JK
1116 write_script remove-all-but-first.sh <<-\EOF &&
1117 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1118 mv "$1.tmp" "$1"
1119 EOF
b2dbacbd
PW
1120 (
1121 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1122 git rebase -i B
1123 ) &&
180bad3d
JK
1124 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1125'
1126
882cd237 1127test_expect_success 'rebase -i respects core.commentchar=auto' '
66458388
JS
1128 test_config core.commentchar auto &&
1129 write_script copy-edit-script.sh <<-\EOF &&
1130 cp "$1" edit-script
1131 EOF
66458388 1132 test_when_finished "git rebase --abort || :" &&
b2dbacbd
PW
1133 (
1134 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1135 git rebase -i HEAD^
1136 ) &&
66458388
JS
1137 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1138'
1139
2e6e276d 1140test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
6567dc05
RR
1141 test_when_finished "git branch -D torebase" &&
1142 git checkout -b torebase branch1 &&
1143 upstream=$(git rev-parse ":/J") &&
1144 onto=$(git rev-parse ":/A") &&
1145 git rebase --onto $onto $upstream &&
1146 git reset --hard branch1 &&
1147 git rebase --onto ":/A" ":/J" &&
1148 git checkout branch1
1149'
1150
db2b3b82
AF
1151test_expect_success 'rebase -i with --strategy and -X' '
1152 git checkout -b conflict-merge-use-theirs conflict-branch &&
1153 git reset --hard HEAD^ &&
1154 echo five >conflict &&
1155 echo Z >file1 &&
1156 git commit -a -m "one file conflict" &&
1157 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1158 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1159 test $(cat file1) = Z
1160'
1161
040fd39e
FR
1162test_expect_success 'interrupted rebase -i with --strategy and -X' '
1163 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1164 git reset --hard HEAD^ &&
1165 >breakpoint &&
1166 git add breakpoint &&
1167 git commit -m "breakpoint for interactive mode" &&
1168 echo five >conflict &&
1169 echo Z >file1 &&
1170 git commit -a -m "one file conflict" &&
b2dbacbd
PW
1171 (
1172 set_fake_editor &&
1173 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1174 -Xours conflict-branch
1175 ) &&
040fd39e
FR
1176 git rebase --continue &&
1177 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1178 test $(cat file1) = Z
1179'
1180
89b0230a 1181test_expect_success 'rebase -i error on commits with \ in message' '
99094a7a 1182 current_head=$(git rev-parse HEAD) &&
89b0230a
MM
1183 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1184 test_commit TO-REMOVE will-conflict old-content &&
1185 test_commit "\temp" will-conflict new-content dummy &&
512477b1 1186 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
89b0230a
MM
1187 test_expect_code 1 grep " emp" error
1188'
1189
800e6a70 1190test_expect_success 'short commit ID setup' '
9c8509a4 1191 test_when_finished "git checkout primary" &&
66ae9a57
ES
1192 git checkout --orphan collide &&
1193 git rm -rf . &&
1194 (
1195 unset test_tick &&
1196 test_commit collide1 collide &&
1197 test_commit --notick collide2 collide &&
1198 test_commit --notick collide3 collide
1199 )
1200'
1201
800e6a70
JS
1202if test -n "$GIT_TEST_FIND_COLLIDER"
1203then
1204 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1205 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1206 blob="$(git rev-parse collide2:collide)"
1207 from="$(git rev-parse collide1^0)"
1208 repl="commit refs/heads/collider-&\\n"
1209 repl="${repl}author $author\\ncommitter $committer\\n"
1210 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1211 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1212 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1213 git fast-import <script &&
1214 git pack-refs &&
1215 git for-each-ref >refs &&
1216 grep "^$(test_oid t3404_collision)" <refs >matches &&
1217 cat matches &&
1218 test_line_count -gt 2 matches || {
1219 echo "Could not find a collider" >&2
1220 exit 1
1221 }
1222fi
1223
1224test_expect_success 'short commit ID collide' '
1225 test_oid_cache <<-EOF &&
1226 # collision-related constants
1227 t3404_collision sha1:6bcd
1228 t3404_collision sha256:0161
1229 t3404_collider sha1:ac4f2ee
1230 t3404_collider sha256:16697
1231 EOF
9c8509a4 1232 test_when_finished "reset_rebase && git checkout primary" &&
66ae9a57 1233 git checkout collide &&
800e6a70
JS
1234 colliding_id=$(test_oid t3404_collision) &&
1235 hexsz=$(test_oid hexsz) &&
1236 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1237 test_config core.abbrev 4 &&
66ae9a57 1238 (
8cf8f9b4
JS
1239 unset test_tick &&
1240 test_tick &&
1241 set_fake_editor &&
800e6a70 1242 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
b6992261 1243 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
800e6a70
JS
1244 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1245 grep "^pick $colliding_id " \
b6992261 1246 .git/rebase-merge/git-rebase-todo.tmp &&
a764c37b 1247 grep -E "^pick [0-9a-f]{$hexsz}" \
b6992261 1248 .git/rebase-merge/git-rebase-todo &&
a764c37b 1249 grep -E "^pick [0-9a-f]{$hexsz}" \
26027625 1250 .git/rebase-merge/git-rebase-todo.backup &&
b6992261
JS
1251 git rebase --continue
1252 ) &&
1253 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1254 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1255 test "$collide2" = "$collide3"
66ae9a57
ES
1256'
1257
edb72d55
KS
1258test_expect_success 'respect core.abbrev' '
1259 git config core.abbrev 12 &&
b2dbacbd
PW
1260 (
1261 set_cat_todo_editor &&
1262 test_must_fail git rebase -i HEAD~4 >todo-list
1263 ) &&
a764c37b 1264 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
edb72d55
KS
1265'
1266
28c8cfc3
ES
1267test_expect_success 'todo count' '
1268 write_script dump-raw.sh <<-\EOF &&
1269 cat "$1"
1270 EOF
b2dbacbd
PW
1271 (
1272 test_set_editor "$(pwd)/dump-raw.sh" &&
1273 git rebase -i HEAD~4 >actual
1274 ) &&
b8fc9e43 1275 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
28c8cfc3
ES
1276'
1277
8cbc57ca
PH
1278test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1279 git checkout --force branch2 &&
1280 git clean -f &&
b2dbacbd
PW
1281 (
1282 set_fake_editor &&
1283 FAKE_LINES="edit 1 2" git rebase -i A
1284 ) &&
8cbc57ca
PH
1285 test_cmp_rev HEAD F &&
1286 test_path_is_missing file6 &&
1287 >file6 &&
1288 test_must_fail git rebase --continue &&
1289 test_cmp_rev HEAD F &&
1290 rm file6 &&
1291 git rebase --continue &&
1292 test_cmp_rev HEAD I
1293'
1294
1295test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1296 git checkout --force branch2 &&
1297 git clean -f &&
1298 git tag original-branch2 &&
b2dbacbd
PW
1299 (
1300 set_fake_editor &&
1301 FAKE_LINES="edit 1 squash 2" git rebase -i A
1302 ) &&
8cbc57ca
PH
1303 test_cmp_rev HEAD F &&
1304 test_path_is_missing file6 &&
1305 >file6 &&
1306 test_must_fail git rebase --continue &&
1307 test_cmp_rev HEAD F &&
1308 rm file6 &&
1309 git rebase --continue &&
1310 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1311 git reset --hard original-branch2
1312'
1313
1314test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1315 git checkout --force branch2 &&
1316 git clean -f &&
b2dbacbd
PW
1317 (
1318 set_fake_editor &&
1319 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1320 ) &&
8cbc57ca
PH
1321 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1322 test_path_is_missing file6 &&
1323 >file6 &&
1324 test_must_fail git rebase --continue &&
1325 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1326 rm file6 &&
1327 git rebase --continue &&
1328 test $(git cat-file commit HEAD | sed -ne \$p) = I
1329'
1330
0e0aff4b 1331test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
d17ec3a9
JS
1332 git checkout -b commit-to-skip &&
1333 for double in X 3 1
1334 do
1335 test_seq 5 | sed "s/$double/&&/" >seq &&
1336 git add seq &&
1337 test_tick &&
db5875aa 1338 git commit -m seq-$double || return 1
d17ec3a9
JS
1339 done &&
1340 git tag seq-onto &&
1341 git reset --hard HEAD~2 &&
1342 git cherry-pick seq-onto &&
b2dbacbd
PW
1343 (
1344 set_fake_editor &&
1345 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1346 ) &&
d17ec3a9
JS
1347 test -d .git/rebase-merge &&
1348 git rebase --continue &&
1349 git diff --exit-code seq-onto &&
1350 test ! -d .git/rebase-merge &&
1351 test ! -f .git/CHERRY_PICK_HEAD
1352'
1353
c9266d58
GR
1354rebase_setup_and_clean () {
1355 test_when_finished "
9c8509a4 1356 git checkout primary &&
c9266d58
GR
1357 test_might_fail git branch -D $1 &&
1358 test_might_fail git rebase --abort
1359 " &&
9c8509a4 1360 git checkout -b $1 ${2:-primary}
c9266d58
GR
1361}
1362
1363test_expect_success 'drop' '
1364 rebase_setup_and_clean drop-test &&
b2dbacbd
PW
1365 (
1366 set_fake_editor &&
1367 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1368 ) &&
c9266d58
GR
1369 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1370 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1371 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1372'
1373
37079959
GR
1374test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1375 test_config rebase.missingCommitsCheck ignore &&
1376 rebase_setup_and_clean missing-commit &&
b2dbacbd
PW
1377 (
1378 set_fake_editor &&
1379 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1380 ) &&
37079959 1381 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
21d0764c
JS
1382 test_i18ngrep \
1383 "Successfully rebased and updated refs/heads/missing-commit" \
1384 actual
37079959
GR
1385'
1386
37079959 1387test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
c9749b36
SG
1388 cat >expect <<-EOF &&
1389 Warning: some commits may have been dropped accidentally.
1390 Dropped commits (newer to older):
9c8509a4 1391 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
c9749b36 1392 To avoid this message, use "drop" to explicitly remove a commit.
c9749b36 1393 EOF
37079959
GR
1394 test_config rebase.missingCommitsCheck warn &&
1395 rebase_setup_and_clean missing-commit &&
b2dbacbd
PW
1396 (
1397 set_fake_editor &&
1398 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1399 ) &&
077b9798 1400 head -n4 actual.2 >actual &&
1108cea7 1401 test_cmp expect actual &&
37079959
GR
1402 test D = $(git cat-file commit HEAD | sed -ne \$p)
1403'
1404
37079959 1405test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
c9749b36
SG
1406 cat >expect <<-EOF &&
1407 Warning: some commits may have been dropped accidentally.
1408 Dropped commits (newer to older):
9c8509a4
JS
1409 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1410 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
c9749b36
SG
1411 To avoid this message, use "drop" to explicitly remove a commit.
1412
1413 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1414 The possible behaviours are: ignore, warn, error.
1415
1416 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1417 Or you can abort the rebase with '\''git rebase --abort'\''.
1418 EOF
37079959
GR
1419 test_config rebase.missingCommitsCheck error &&
1420 rebase_setup_and_clean missing-commit &&
b2dbacbd
PW
1421 (
1422 set_fake_editor &&
1423 test_must_fail env FAKE_LINES="1 2 4" \
1424 git rebase -i --root 2>actual &&
1108cea7 1425 test_cmp expect actual &&
b2dbacbd
PW
1426 cp .git/rebase-merge/git-rebase-todo.backup \
1427 .git/rebase-merge/git-rebase-todo &&
1428 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1429 ) &&
37079959
GR
1430 git rebase --continue &&
1431 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1432 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1433'
1434
5a5445d8
AG
1435test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1436 test_config rebase.missingCommitsCheck ignore &&
1437 rebase_setup_and_clean missing-commit &&
1438 (
1439 set_fake_editor &&
1440 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1441 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1442 git rebase --continue 2>actual
1443 ) &&
1444 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1445 test_i18ngrep \
1446 "Successfully rebased and updated refs/heads/missing-commit" \
1447 actual
1448'
1449
1450test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1451 cat >expect <<-EOF &&
7aed2c05
PW
1452 error: invalid command '\''pickled'\''
1453 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
5a5445d8
AG
1454 Warning: some commits may have been dropped accidentally.
1455 Dropped commits (newer to older):
9c8509a4
JS
1456 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1457 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
5a5445d8
AG
1458 To avoid this message, use "drop" to explicitly remove a commit.
1459 EOF
7aed2c05 1460 head -n5 expect >expect.2 &&
5a5445d8
AG
1461 tail -n1 expect >>expect.2 &&
1462 tail -n4 expect.2 >expect.3 &&
1463 test_config rebase.missingCommitsCheck warn &&
1464 rebase_setup_and_clean missing-commit &&
1465 (
1466 set_fake_editor &&
1467 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1468 git rebase -i --root &&
1469 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1470 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
7aed2c05 1471 head -n7 actual.2 >actual &&
1108cea7 1472 test_cmp expect actual &&
5a5445d8
AG
1473 cp orig .git/rebase-merge/git-rebase-todo &&
1474 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1475 head -n4 actual.2 >actual &&
1108cea7 1476 test_cmp expect.3 actual &&
5a5445d8
AG
1477 git rebase --continue 2>actual
1478 ) &&
1479 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1480 test_i18ngrep \
1481 "Successfully rebased and updated refs/heads/missing-commit" \
1482 actual
1483'
1484
1485test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1486 cat >expect <<-EOF &&
7aed2c05
PW
1487 error: invalid command '\''pickled'\''
1488 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
5a5445d8
AG
1489 Warning: some commits may have been dropped accidentally.
1490 Dropped commits (newer to older):
9c8509a4
JS
1491 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1492 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
5a5445d8
AG
1493 To avoid this message, use "drop" to explicitly remove a commit.
1494
1495 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1496 The possible behaviours are: ignore, warn, error.
1497
1498 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1499 Or you can abort the rebase with '\''git rebase --abort'\''.
1500 EOF
1501 tail -n11 expect >expect.2 &&
1502 head -n3 expect.2 >expect.3 &&
1503 tail -n7 expect.2 >>expect.3 &&
1504 test_config rebase.missingCommitsCheck error &&
1505 rebase_setup_and_clean missing-commit &&
1506 (
1507 set_fake_editor &&
1508 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1509 git rebase -i --root &&
1510 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1511 test_must_fail env FAKE_LINES="2 3 4" \
1512 git rebase --edit-todo 2>actual &&
1108cea7 1513 test_cmp expect actual &&
5a5445d8 1514 test_must_fail git rebase --continue 2>actual &&
1108cea7 1515 test_cmp expect.2 actual &&
5a5445d8
AG
1516 test_must_fail git rebase --edit-todo &&
1517 cp orig .git/rebase-merge/git-rebase-todo &&
1518 test_must_fail env FAKE_LINES="1 2 3 4" \
1519 git rebase --edit-todo 2>actual &&
1108cea7 1520 test_cmp expect.3 actual &&
5a5445d8 1521 test_must_fail git rebase --continue 2>actual &&
1108cea7 1522 test_cmp expect.3 actual &&
5a5445d8
AG
1523 cp orig .git/rebase-merge/git-rebase-todo &&
1524 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1525 git rebase --continue 2>actual
1526 ) &&
1527 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1528 test_i18ngrep \
1529 "Successfully rebased and updated refs/heads/missing-commit" \
1530 actual
1531'
1532
1533test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1534 test_config rebase.missingCommitsCheck error &&
1535 (
1536 set_fake_editor &&
1537 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1538 ) &&
1539 git rebase --edit-todo &&
1540 test_must_fail git rebase --continue &&
1541 echo x >file1 &&
1542 git add file1 &&
1543 git rebase --continue
1544'
1545
1546test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1547 test_config rebase.missingCommitsCheck error &&
1548 (
1549 set_fake_editor &&
1550 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1551 cp .git/rebase-merge/git-rebase-todo todo &&
1552 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1553 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1554 git rebase --continue
1555 )
1556'
1557
17959934
LB
1558test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1559 rebase_setup_and_clean abbrevcmd &&
1560 test_commit "first" file1.txt "first line" first &&
1561 test_commit "second" file1.txt "another line" second &&
1562 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1563 test_commit "squash! second" file1.txt "another line here" second_squash &&
1564 cat >expected <<-EOF &&
1565 p $(git rev-list --abbrev-commit -1 first) first
1566 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1567 x git show HEAD
1568 p $(git rev-list --abbrev-commit -1 second) second
1569 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1570 x git show HEAD
1571 EOF
1572 git checkout abbrevcmd &&
17959934 1573 test_config rebase.abbreviateCommands true &&
b2dbacbd
PW
1574 (
1575 set_cat_todo_editor &&
1576 test_must_fail git rebase -i --exec "git show HEAD" \
9c8509a4 1577 --autosquash primary >actual
b2dbacbd 1578 ) &&
17959934
LB
1579 test_cmp expected actual
1580'
1581
804098bb
GR
1582test_expect_success 'static check of bad command' '
1583 rebase_setup_and_clean bad-cmd &&
b2dbacbd
PW
1584 (
1585 set_fake_editor &&
1586 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
804098bb 1587 git rebase -i --root 2>actual &&
7aed2c05 1588 test_i18ngrep "pickled $(git rev-list --oneline -1 primary~1)" \
b2dbacbd
PW
1589 actual &&
1590 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1591 actual &&
1592 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1593 ) &&
804098bb
GR
1594 git rebase --continue &&
1595 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1596 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1597'
1598
1db168ee
MM
1599test_expect_success 'tabs and spaces are accepted in the todolist' '
1600 rebase_setup_and_clean indented-comment &&
1601 write_script add-indent.sh <<-\EOF &&
1602 (
1603 # Turn single spaces into space/tab mix
1604 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1605 printf "\n\t# comment\n #more\n\t # comment\n"
7966230b 1606 ) >"$1.new"
1db168ee
MM
1607 mv "$1.new" "$1"
1608 EOF
b2dbacbd
PW
1609 (
1610 test_set_editor "$(pwd)/add-indent.sh" &&
1611 git rebase -i HEAD^^^
1612 ) &&
1db168ee
MM
1613 test E = $(git cat-file commit HEAD | sed -ne \$p)
1614'
1615
804098bb
GR
1616test_expect_success 'static check of bad SHA-1' '
1617 rebase_setup_and_clean bad-sha &&
b2dbacbd
PW
1618 (
1619 set_fake_editor &&
1620 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1621 git rebase -i --root 2>actual &&
1622 test_i18ngrep "edit XXXXXXX False commit" actual &&
1623 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1624 actual &&
1625 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1626 ) &&
804098bb
GR
1627 git rebase --continue &&
1628 test E = $(git cat-file commit HEAD | sed -ne \$p)
1629'
1630
39743cf5 1631test_expect_success 'editor saves as CR/LF' '
1db25aae
JS
1632 git checkout -b with-crlf &&
1633 write_script add-crs.sh <<-\EOF &&
1634 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1635 mv -f "$1".new "$1"
1636 EOF
1637 (
1638 test_set_editor "$(pwd)/add-crs.sh" &&
1639 git rebase -i HEAD^
1640 )
1641'
1642
6672b9f9 1643test_expect_success 'rebase -i --gpg-sign=<key-id>' '
ed1e5282 1644 test_when_finished "test_might_fail git rebase --abort" &&
b2dbacbd
PW
1645 (
1646 set_fake_editor &&
1647 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1648 HEAD^ >out 2>err
1649 ) &&
ed1e5282
JS
1650 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1651'
1652
1653test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1654 test_when_finished "test_might_fail git rebase --abort" &&
1655 test_config commit.gpgsign true &&
b2dbacbd
PW
1656 (
1657 set_fake_editor &&
1658 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1659 HEAD^ >out 2>err
1660 ) &&
7ca79dca 1661 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
6672b9f9
JS
1662'
1663
ca3e1826
ES
1664test_expect_success 'valid author header after --root swap' '
1665 rebase_setup_and_clean author-header no-conflict-branch &&
4aa5ff94
PW
1666 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1667 git cat-file commit HEAD | grep ^author >expected &&
b2dbacbd
PW
1668 (
1669 set_fake_editor &&
1670 FAKE_LINES="5 1" git rebase -i --root
1671 ) &&
4aa5ff94
PW
1672 git cat-file commit HEAD^ | grep ^author >actual &&
1673 test_cmp expected actual
1674'
1675
1676test_expect_success 'valid author header when author contains single quote' '
1677 rebase_setup_and_clean author-header no-conflict-branch &&
4aa5ff94
PW
1678 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1679 git cat-file commit HEAD | grep ^author >expected &&
b2dbacbd
PW
1680 (
1681 set_fake_editor &&
1682 FAKE_LINES="2" git rebase -i HEAD~2
1683 ) &&
4aa5ff94
PW
1684 git cat-file commit HEAD | grep ^author >actual &&
1685 test_cmp expected actual
ca3e1826
ES
1686'
1687
4627bc77 1688test_expect_success 'post-commit hook is called' '
4627bc77 1689 >actual &&
bef805b7 1690 test_hook post-commit <<-\EOS &&
4627bc77
PW
1691 git rev-parse HEAD >>actual
1692 EOS
1693 (
1694 set_fake_editor &&
1695 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1696 echo x>file3 &&
1697 git add file3 &&
1698 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1699 ) &&
1700 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1701 >expect &&
1702 test_cmp expect actual
1703'
1704
430b75f7
PW
1705test_expect_success 'correct error message for partial commit after empty pick' '
1706 test_when_finished "git rebase --abort" &&
1707 (
1708 set_fake_editor &&
1709 FAKE_LINES="2 1 1" &&
1710 export FAKE_LINES &&
1711 test_must_fail git rebase -i A D
1712 ) &&
1713 echo x >file1 &&
1714 test_must_fail git commit file1 2>err &&
1715 test_i18ngrep "cannot do a partial commit during a rebase." err
1716'
1717
1718test_expect_success 'correct error message for commit --amend after empty pick' '
1719 test_when_finished "git rebase --abort" &&
1720 (
1721 set_fake_editor &&
1722 FAKE_LINES="1 1" &&
1723 export FAKE_LINES &&
1724 test_must_fail git rebase -i A D
1725 ) &&
1726 echo x>file1 &&
1727 test_must_fail git commit -a --amend 2>err &&
1728 test_i18ngrep "middle of a rebase -- cannot amend." err
1729'
1730
5da69c0d
AK
1731test_expect_success 'todo has correct onto hash' '
1732 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1733 onto=$(git rev-parse --short HEAD~4) &&
1734 test_i18ngrep "^# Rebase ..* onto $onto" actual
1735'
1736
e100bea4 1737test_expect_success 'ORIG_HEAD is updated correctly' '
9c8509a4 1738 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
e100bea4
PW
1739 git checkout -b test-orig-head A &&
1740 git commit --allow-empty -m A1 &&
1741 git commit --allow-empty -m A2 &&
1742 git commit --allow-empty -m A3 &&
1743 git commit --allow-empty -m A4 &&
9c8509a4 1744 git rebase primary &&
e100bea4
PW
1745 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1746'
1747
900b50c2
DS
1748test_expect_success '--update-refs adds label and update-ref commands' '
1749 git checkout -b update-refs no-conflict-branch &&
1750 git branch -f base HEAD~4 &&
1751 git branch -f first HEAD~3 &&
1752 git branch -f second HEAD~3 &&
1753 git branch -f third HEAD~1 &&
1754 git commit --allow-empty --fixup=third &&
1755 git branch -f is-not-reordered &&
1756 git commit --allow-empty --fixup=HEAD~4 &&
1757 git branch -f shared-tip &&
1758 (
1759 set_cat_todo_editor &&
1760
1761 cat >expect <<-EOF &&
1762 pick $(git log -1 --format=%h J) J
1763 fixup $(git log -1 --format=%h update-refs) fixup! J # empty
1764 update-ref refs/heads/second
1765 update-ref refs/heads/first
1766 pick $(git log -1 --format=%h K) K
1767 pick $(git log -1 --format=%h L) L
1768 fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
1769 update-ref refs/heads/third
1770 pick $(git log -1 --format=%h M) M
1771 update-ref refs/heads/no-conflict-branch
1772 update-ref refs/heads/is-not-reordered
1773 update-ref refs/heads/shared-tip
1774 EOF
1775
1776 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
3113feda
DS
1777 test_cmp expect todo &&
1778
1779 test_must_fail git -c rebase.autosquash=true \
1780 -c rebase.updaterefs=true \
1781 rebase -i primary >todo &&
1782
900b50c2
DS
1783 test_cmp expect todo
1784 )
1785'
1786
1787test_expect_success '--update-refs adds commands with --rebase-merges' '
1788 git checkout -b update-refs-with-merge no-conflict-branch &&
1789 git branch -f base HEAD~4 &&
1790 git branch -f first HEAD~3 &&
1791 git branch -f second HEAD~3 &&
1792 git branch -f third HEAD~1 &&
1793 git merge -m merge branch2 &&
1794 git branch -f merge-branch &&
1795 git commit --fixup=third --allow-empty &&
1796 (
1797 set_cat_todo_editor &&
1798
1799 cat >expect <<-EOF &&
1800 label onto
1801 reset onto
1802 pick $(git log -1 --format=%h branch2~1) F
1803 pick $(git log -1 --format=%h branch2) I
1804 update-ref refs/heads/branch2
1805 label merge
1806 reset onto
1807 pick $(git log -1 --format=%h refs/heads/second) J
1808 update-ref refs/heads/second
1809 update-ref refs/heads/first
1810 pick $(git log -1 --format=%h refs/heads/third~1) K
1811 pick $(git log -1 --format=%h refs/heads/third) L
1812 fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
1813 update-ref refs/heads/third
1814 pick $(git log -1 --format=%h HEAD~2) M
1815 update-ref refs/heads/no-conflict-branch
1816 merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1817 update-ref refs/heads/merge-branch
1818 EOF
1819
1820 test_must_fail git rebase -i --autosquash \
1821 --rebase-merges=rebase-cousins \
1822 --update-refs primary >todo &&
1823
3113feda
DS
1824 test_cmp expect todo &&
1825
1826 test_must_fail git -c rebase.autosquash=true \
1827 -c rebase.updaterefs=true \
1828 rebase -i \
1829 --rebase-merges=rebase-cousins \
1830 primary >todo &&
1831
900b50c2
DS
1832 test_cmp expect todo
1833 )
1834'
1835
89fc0b53
DS
1836test_expect_success '--update-refs updates refs correctly' '
1837 git checkout -B update-refs no-conflict-branch &&
1838 git branch -f base HEAD~4 &&
1839 git branch -f first HEAD~3 &&
1840 git branch -f second HEAD~3 &&
1841 git branch -f third HEAD~1 &&
1842 test_commit extra2 fileX &&
1843 git commit --amend --fixup=L &&
1844
4611884e 1845 git rebase -i --autosquash --update-refs primary 2>err &&
89fc0b53
DS
1846
1847 test_cmp_rev HEAD~3 refs/heads/first &&
1848 test_cmp_rev HEAD~3 refs/heads/second &&
1849 test_cmp_rev HEAD~1 refs/heads/third &&
4611884e
DS
1850 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1851
1852 cat >expect <<-\EOF &&
1853 Successfully rebased and updated refs/heads/update-refs.
1854 Updated the following refs with --update-refs:
1855 refs/heads/first
1856 refs/heads/no-conflict-branch
1857 refs/heads/second
1858 refs/heads/third
1859 EOF
1860
1861 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
1862 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
1863 <err >err.trimmed &&
1864 test_cmp expect err.trimmed
89fc0b53
DS
1865'
1866
b3b1a21d
DS
1867test_expect_success 'respect user edits to update-ref steps' '
1868 git checkout -B update-refs-break no-conflict-branch &&
1869 git branch -f base HEAD~4 &&
1870 git branch -f first HEAD~3 &&
1871 git branch -f second HEAD~3 &&
1872 git branch -f third HEAD~1 &&
1873 git branch -f unseen base &&
1874
1875 # First, we will add breaks to the expected todo file
1876 cat >fake-todo-1 <<-EOF &&
1877 pick $(git rev-parse HEAD~3)
1878 break
1879 update-ref refs/heads/second
1880 update-ref refs/heads/first
1881
1882 pick $(git rev-parse HEAD~2)
1883 pick $(git rev-parse HEAD~1)
1884 update-ref refs/heads/third
1885
1886 pick $(git rev-parse HEAD)
1887 update-ref refs/heads/no-conflict-branch
1888 EOF
1889
1890 # Second, we will drop some update-refs commands (and move one)
1891 cat >fake-todo-2 <<-EOF &&
1892 update-ref refs/heads/second
1893
1894 pick $(git rev-parse HEAD~2)
1895 update-ref refs/heads/third
1896 pick $(git rev-parse HEAD~1)
1897 break
1898
1899 pick $(git rev-parse HEAD)
1900 EOF
1901
1902 # Third, we will:
1903 # * insert a new one (new-branch),
1904 # * re-add an old one (first), and
1905 # * add a second instance of a previously-stored one (second)
1906 cat >fake-todo-3 <<-EOF &&
1907 update-ref refs/heads/unseen
1908 update-ref refs/heads/new-branch
1909 pick $(git rev-parse HEAD)
1910 update-ref refs/heads/first
1911 update-ref refs/heads/second
1912 EOF
1913
1914 (
1915 set_replace_editor fake-todo-1 &&
1916 git rebase -i --update-refs primary &&
1917
1918 # These branches are currently locked.
1919 for b in first second third no-conflict-branch
1920 do
1921 test_must_fail git branch -f $b base || return 1
1922 done &&
1923
1924 set_replace_editor fake-todo-2 &&
1925 git rebase --edit-todo &&
1926
1927 # These branches are currently locked.
1928 for b in second third
1929 do
1930 test_must_fail git branch -f $b base || return 1
1931 done &&
1932
1933 # These branches are currently unlocked for checkout.
1934 for b in first no-conflict-branch
1935 do
1936 git worktree add wt-$b $b &&
1937 git worktree remove wt-$b || return 1
1938 done &&
1939
1940 git rebase --continue &&
1941
1942 set_replace_editor fake-todo-3 &&
1943 git rebase --edit-todo &&
1944
1945 # These branches are currently locked.
1946 for b in second third first unseen
1947 do
1948 test_must_fail git branch -f $b base || return 1
1949 done &&
1950
1951 # These branches are currently unlocked for checkout.
1952 for b in no-conflict-branch
1953 do
1954 git worktree add wt-$b $b &&
1955 git worktree remove wt-$b || return 1
1956 done &&
1957
1958 git rebase --continue
1959 ) &&
1960
1961 test_cmp_rev HEAD~2 refs/heads/third &&
1962 test_cmp_rev HEAD~1 refs/heads/unseen &&
1963 test_cmp_rev HEAD~1 refs/heads/new-branch &&
1964 test_cmp_rev HEAD refs/heads/first &&
1965 test_cmp_rev HEAD refs/heads/second &&
1966 test_cmp_rev HEAD refs/heads/no-conflict-branch
1967'
1968
44da9e08
VD
1969test_expect_success '--update-refs: all update-ref lines removed' '
1970 git checkout -b test-refs-not-removed no-conflict-branch &&
1971 git branch -f base HEAD~4 &&
1972 git branch -f first HEAD~3 &&
1973 git branch -f second HEAD~3 &&
1974 git branch -f third HEAD~1 &&
1975 git branch -f tip &&
1976
1977 test_commit test-refs-not-removed &&
1978 git commit --amend --fixup first &&
1979
1980 git rev-parse first second third tip no-conflict-branch >expect-oids &&
1981
1982 (
1983 set_cat_todo_editor &&
1984 test_must_fail git rebase -i --update-refs base >todo.raw &&
1985 sed -e "/^update-ref/d" <todo.raw >todo
1986 ) &&
1987 (
1988 set_replace_editor todo &&
1989 git rebase -i --update-refs base
1990 ) &&
1991
1992 # Ensure refs are not deleted and their OIDs have not changed
1993 git rev-parse first second third tip no-conflict-branch >actual-oids &&
1994 test_cmp expect-oids actual-oids
1995'
1996
1997test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
1998 git checkout -b test-refs-not-removed2 no-conflict-branch &&
1999 git branch -f base HEAD~4 &&
2000 git branch -f first HEAD~3 &&
2001 git branch -f second HEAD~3 &&
2002 git branch -f third HEAD~1 &&
2003 git branch -f tip &&
2004
2005 test_commit test-refs-not-removed2 &&
2006 git commit --amend --fixup first &&
2007
2008 git rev-parse first second third >expect-oids &&
2009
2010 (
2011 set_cat_todo_editor &&
2012 test_must_fail git rebase -i \
2013 --autosquash --update-refs \
2014 base >todo.raw &&
2015 sed -e "/^update-ref/d" <todo.raw >todo
2016 ) &&
2017
2018 # Add a break to the end of the todo so we can edit later
2019 echo "break" >>todo &&
2020
2021 (
2022 set_replace_editor todo &&
2023 git rebase -i --autosquash --update-refs base &&
2024 echo "update-ref refs/heads/tip" >todo &&
2025 git rebase --edit-todo &&
2026 git rebase --continue
2027 ) &&
2028
2029 # Ensure first/second/third are unchanged, but tip is updated
2030 git rev-parse first second third >actual-oids &&
2031 test_cmp expect-oids actual-oids &&
2032 test_cmp_rev HEAD tip
2033'
2034
2035test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
2036 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2037 git branch -f base HEAD~4 &&
2038 git branch -f first HEAD~3 &&
2039 git branch -f second HEAD~3 &&
2040 git branch -f third HEAD~1 &&
2041 git branch -f tip &&
2042
2043 test_commit test-refs-not-removed3 &&
2044 git commit --amend --fixup first &&
2045
2046 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2047
2048 (
2049 set_cat_todo_editor &&
2050 test_must_fail git rebase -i \
2051 --autosquash --update-refs \
2052 base >todo.raw &&
2053 sed -e "/^update-ref/d" <todo.raw >todo
2054 ) &&
2055
2056 # Add a break to the beginning of the todo so we can resume with no
2057 # update-ref lines
2058 echo "break" >todo.new &&
2059 cat todo >>todo.new &&
2060
2061 (
2062 set_replace_editor todo.new &&
2063 git rebase -i --autosquash --update-refs base &&
2064
2065 # Make no changes when editing so update-refs is still empty
2066 cat todo >todo.new &&
2067 git rebase --edit-todo &&
2068 git rebase --continue
2069 ) &&
2070
2071 # Ensure refs are not deleted and their OIDs have not changed
2072 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2073 test_cmp expect-oids actual-oids
2074'
2075
b3b1a21d 2076test_expect_success '--update-refs: check failed ref update' '
16b3880d 2077 test_when_finished "test_might_fail git rebase --abort" &&
b3b1a21d
DS
2078 git checkout -B update-refs-error no-conflict-branch &&
2079 git branch -f base HEAD~4 &&
2080 git branch -f first HEAD~3 &&
2081 git branch -f second HEAD~2 &&
2082 git branch -f third HEAD~1 &&
2083
2084 cat >fake-todo <<-EOF &&
2085 pick $(git rev-parse HEAD~3)
2086 break
2087 update-ref refs/heads/first
2088
2089 pick $(git rev-parse HEAD~2)
2090 update-ref refs/heads/second
2091
2092 pick $(git rev-parse HEAD~1)
2093 update-ref refs/heads/third
2094
2095 pick $(git rev-parse HEAD)
2096 update-ref refs/heads/no-conflict-branch
2097 EOF
2098
2099 (
2100 set_replace_editor fake-todo &&
2101 git rebase -i --update-refs base
2102 ) &&
2103
2104 # At this point, the values of first, second, and third are
2105 # recorded in the update-refs file. We will force-update the
2106 # "second" ref, but "git branch -f" will not work because of
2107 # the lock in the update-refs file.
2108 git rev-parse third >.git/refs/heads/second &&
2109
4611884e
DS
2110 test_must_fail git rebase --continue 2>err &&
2111 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2112
2113 cat >expect <<-\EOF &&
2114 Updated the following refs with --update-refs:
2115 refs/heads/first
2116 refs/heads/no-conflict-branch
2117 refs/heads/third
2118 Failed to update the following refs with --update-refs:
2119 refs/heads/second
2120 EOF
2121
2122 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2123 tail -n 6 err >err.last &&
2124 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
2125 <err.last >err.trimmed &&
2126 test_cmp expect err.trimmed
b3b1a21d
DS
2127'
2128
16b3880d
PW
2129test_expect_success 'bad labels and refs rejected when parsing todo list' '
2130 test_when_finished "test_might_fail git rebase --abort" &&
2131 cat >todo <<-\EOF &&
2132 exec >execed
2133 label #
2134 label :invalid
2135 update-ref :bad
2136 update-ref topic
2137 EOF
2138 rm -f execed &&
2139 (
2140 set_replace_editor todo &&
2141 test_must_fail git rebase -i HEAD 2>err
2142 ) &&
2143 grep "'\''#'\'' is not a valid label" err &&
2144 grep "'\'':invalid'\'' is not a valid label" err &&
2145 grep "'\'':bad'\'' is not a valid refname" err &&
2146 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2147 err &&
2148 test_path_is_missing execed
2149'
2150
6a619ca0
PW
2151# This must be the last test in this file
2152test_expect_success '$EDITOR and friends are unchanged' '
2153 test_editor_unchanged
2154'
2155
1b1dce4b 2156test_done