]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3507-cherry-pick-conflict.sh
Sync with Git 2.45.1
[thirdparty/git.git] / t / t3507-cherry-pick-conflict.sh
CommitLineData
6a843348
JN
1#!/bin/sh
2
3test_description='test cherry-pick and revert with conflicts
4
5 -
6 + picked: rewrites foo to c
7 + base: rewrites foo to b
8 + initial: writes foo as a, unrelated as unrelated
9
10'
11
cbc75a12 12GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
13export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
ead74601 15TEST_CREATE_REPO_NO_TEMPLATE=1
6a843348
JN
16. ./test-lib.sh
17
2161da10
JN
18pristine_detach () {
19 git checkout -f "$1^0" &&
20 git read-tree -u --reset HEAD &&
21 git clean -d -f -f -q -x
22}
23
6a843348
JN
24test_expect_success setup '
25
26 echo unrelated >unrelated &&
27 git add unrelated &&
28 test_commit initial foo a &&
29 test_commit base foo b &&
30 test_commit picked foo c &&
5ed75e2a 31 test_commit --signoff picked-signed foo d &&
36b0503a
PW
32 git checkout -b topic initial &&
33 test_commit redundant-pick foo c redundant &&
34 git commit --allow-empty --allow-empty-message &&
35 git tag empty &&
cbc75a12 36 git checkout main &&
6a843348
JN
37 git config advice.detachedhead false
38
39'
40
41test_expect_success 'failed cherry-pick does not advance HEAD' '
2161da10 42 pristine_detach initial &&
6a843348
JN
43
44 head=$(git rev-parse HEAD) &&
45 test_must_fail git cherry-pick picked &&
46 newhead=$(git rev-parse HEAD) &&
47
48 test "$head" = "$newhead"
49'
50
f172556b 51test_expect_success 'advice from failed cherry-pick' '
2161da10 52 pristine_detach initial &&
314eeb6e 53
f172556b 54 picked=$(git rev-parse --short picked) &&
314eeb6e 55 cat <<-EOF >expected &&
f172556b
ZH
56 error: could not apply $picked... picked
57 hint: After resolving the conflicts, mark them with
58 hint: "git add/rm <pathspec>", then run
59 hint: "git cherry-pick --continue".
60 hint: You can instead skip this commit with "git cherry-pick --skip".
61 hint: To abort and get back to the state before "git cherry-pick",
62 hint: run "git cherry-pick --abort".
ec030091 63 hint: Disable this message with "git config advice.mergeConflict false"
314eeb6e
JN
64 EOF
65 test_must_fail git cherry-pick picked 2>actual &&
66
1108cea7 67 test_cmp expected actual
f172556b 68'
314eeb6e 69
ed727b19
PH
70test_expect_success 'advice from failed cherry-pick --no-commit' "
71 pristine_detach initial &&
72
73 picked=\$(git rev-parse --short picked) &&
74 cat <<-EOF >expected &&
75 error: could not apply \$picked... picked
76 hint: after resolving the conflicts, mark the corrected paths
77 hint: with 'git add <paths>' or 'git rm <paths>'
ec030091 78 hint: Disable this message with \"git config advice.mergeConflict false\"
ed727b19
PH
79 EOF
80 test_must_fail git cherry-pick --no-commit picked 2>actual &&
81
1108cea7 82 test_cmp expected actual
ed727b19
PH
83"
84
d7e5c0cb
JS
85test_expect_success 'failed cherry-pick sets CHERRY_PICK_HEAD' '
86 pristine_detach initial &&
87 test_must_fail git cherry-pick picked &&
88 test_cmp_rev picked CHERRY_PICK_HEAD
89'
90
91test_expect_success 'successful cherry-pick does not set CHERRY_PICK_HEAD' '
92 pristine_detach initial &&
93 git cherry-pick base &&
94 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
95'
96
97test_expect_success 'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
98 pristine_detach initial &&
99 git cherry-pick --no-commit base &&
100 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
101'
9fa8aecd
JS
102
103test_expect_success 'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
104 pristine_detach initial &&
5caab8de 105 echo foo >foo &&
9fa8aecd
JS
106 test_must_fail git cherry-pick base &&
107 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
108'
109
110test_expect_success \
111 'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
112 pristine_detach initial &&
5caab8de 113 echo foo >foo &&
9fa8aecd
JS
114 test_must_fail git cherry-pick --strategy=resolve base &&
115 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
116'
d7e5c0cb
JS
117
118test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
119 pristine_detach initial &&
120 (
121 GIT_CHERRY_PICK_HELP="and then do something else" &&
122 export GIT_CHERRY_PICK_HELP &&
123 test_must_fail git cherry-pick picked
124 ) &&
125 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
126'
127
128test_expect_success 'git reset clears CHERRY_PICK_HEAD' '
129 pristine_detach initial &&
130
131 test_must_fail git cherry-pick picked &&
132 git reset &&
133
134 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
135'
136
137test_expect_success 'failed commit does not clear CHERRY_PICK_HEAD' '
138 pristine_detach initial &&
139
140 test_must_fail git cherry-pick picked &&
141 test_must_fail git commit &&
142
143 test_cmp_rev picked CHERRY_PICK_HEAD
144'
145
146test_expect_success 'cancelled commit does not clear CHERRY_PICK_HEAD' '
147 pristine_detach initial &&
148
149 test_must_fail git cherry-pick picked &&
150 echo resolved >foo &&
151 git add foo &&
152 git update-index --refresh -q &&
153 test_must_fail git diff-index --exit-code HEAD &&
154 (
155 GIT_EDITOR=false &&
156 export GIT_EDITOR &&
157 test_must_fail git commit
158 ) &&
159
160 test_cmp_rev picked CHERRY_PICK_HEAD
161'
162
163test_expect_success 'successful commit clears CHERRY_PICK_HEAD' '
164 pristine_detach initial &&
165
166 test_must_fail git cherry-pick picked &&
167 echo resolved >foo &&
168 git add foo &&
169 git commit &&
170
171 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
172'
f028d661
PW
173
174test_expect_success 'partial commit of cherry-pick fails' '
175 pristine_detach initial &&
176
177 test_must_fail git cherry-pick picked &&
178 echo resolved >foo &&
179 git add foo &&
180 test_must_fail git commit foo 2>err &&
181
6789275d 182 test_grep "cannot do a partial commit during a cherry-pick." err
f028d661
PW
183'
184
185test_expect_success 'commit --amend of cherry-pick fails' '
186 pristine_detach initial &&
187
188 test_must_fail git cherry-pick picked &&
189 echo resolved >foo &&
190 git add foo &&
191 test_must_fail git commit --amend 2>err &&
192
6789275d 193 test_grep "in the middle of a cherry-pick -- cannot amend." err
f028d661
PW
194'
195
b07d9bfd
PW
196test_expect_success 'successful final commit clears cherry-pick state' '
197 pristine_detach initial &&
198
199 test_must_fail git cherry-pick base picked-signed &&
200 echo resolved >foo &&
201 test_path_is_file .git/sequencer/todo &&
202 git commit -a &&
a8c663cf 203 test_path_is_missing .git/sequencer
b07d9bfd
PW
204'
205
206test_expect_success 'reset after final pick clears cherry-pick state' '
207 pristine_detach initial &&
208
209 test_must_fail git cherry-pick base picked-signed &&
210 echo resolved >foo &&
211 test_path_is_file .git/sequencer/todo &&
212 git reset &&
a8c663cf 213 test_path_is_missing .git/sequencer
b07d9bfd 214'
d7e5c0cb 215
6a843348 216test_expect_success 'failed cherry-pick produces dirty index' '
2161da10 217 pristine_detach initial &&
6a843348
JN
218
219 test_must_fail git cherry-pick picked &&
220
221 test_must_fail git update-index --refresh -q &&
222 test_must_fail git diff-index --exit-code HEAD
223'
224
225test_expect_success 'failed cherry-pick registers participants in index' '
2161da10 226 pristine_detach initial &&
6a843348
JN
227 {
228 git checkout base -- foo &&
229 git ls-files --stage foo &&
230 git checkout initial -- foo &&
231 git ls-files --stage foo &&
232 git checkout picked -- foo &&
233 git ls-files --stage foo
5caab8de 234 } >stages &&
6a843348
JN
235 sed "
236 1 s/ 0 / 1 /
237 2 s/ 0 / 2 /
238 3 s/ 0 / 3 /
5caab8de 239 " stages >expected &&
2161da10 240 git read-tree -u --reset HEAD &&
6a843348
JN
241
242 test_must_fail git cherry-pick picked &&
5caab8de 243 git ls-files --stage --unmerged >actual &&
6a843348
JN
244
245 test_cmp expected actual
246'
247
1a2b985f
DL
248test_expect_success \
249 'cherry-pick conflict, ensure commit.cleanup = scissors places scissors line properly' '
250 pristine_detach initial &&
251 git config commit.cleanup scissors &&
252 cat <<-EOF >expected &&
253 picked
254
255 # ------------------------ >8 ------------------------
256 # Do not modify or remove the line above.
257 # Everything below it will be ignored.
258 #
259 # Conflicts:
260 # foo
261 EOF
262
263 test_must_fail git cherry-pick picked &&
264
1108cea7 265 test_cmp expected .git/MERGE_MSG
1a2b985f
DL
266'
267
268test_expect_success \
269 'cherry-pick conflict, ensure cleanup=scissors places scissors line properly' '
270 pristine_detach initial &&
271 git config --unset commit.cleanup &&
272 cat <<-EOF >expected &&
273 picked
274
275 # ------------------------ >8 ------------------------
276 # Do not modify or remove the line above.
277 # Everything below it will be ignored.
278 #
279 # Conflicts:
280 # foo
281 EOF
282
283 test_must_fail git cherry-pick --cleanup=scissors picked &&
284
1108cea7 285 test_cmp expected .git/MERGE_MSG
1a2b985f
DL
286'
287
6a843348 288test_expect_success 'failed cherry-pick describes conflict in work tree' '
2161da10 289 pristine_detach initial &&
5caab8de 290 cat <<-EOF >expected &&
6a843348
JN
291 <<<<<<< HEAD
292 a
293 =======
294 c
7d056dea 295 >>>>>>> objid (picked)
6a843348
JN
296 EOF
297
6a843348
JN
298 test_must_fail git cherry-pick picked &&
299
7d056dea 300 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
6a843348
JN
301 test_cmp expected actual
302'
303
304test_expect_success 'diff3 -m style' '
2161da10 305 pristine_detach initial &&
6a843348 306 git config merge.conflictstyle diff3 &&
5caab8de 307 cat <<-EOF >expected &&
6a843348
JN
308 <<<<<<< HEAD
309 a
7d056dea 310 ||||||| parent of objid (picked)
6a843348
JN
311 b
312 =======
313 c
7d056dea 314 >>>>>>> objid (picked)
6a843348
JN
315 EOF
316
6a843348
JN
317 test_must_fail git cherry-pick picked &&
318
7d056dea 319 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
6a843348
JN
320 test_cmp expected actual
321'
322
323test_expect_success 'revert also handles conflicts sanely' '
6a843348 324 git config --unset merge.conflictstyle &&
2161da10 325 pristine_detach initial &&
5caab8de 326 cat <<-EOF >expected &&
6a843348
JN
327 <<<<<<< HEAD
328 a
329 =======
330 b
7d056dea 331 >>>>>>> parent of objid (picked)
6a843348
JN
332 EOF
333 {
334 git checkout picked -- foo &&
335 git ls-files --stage foo &&
336 git checkout initial -- foo &&
337 git ls-files --stage foo &&
338 git checkout base -- foo &&
339 git ls-files --stage foo
5caab8de 340 } >stages &&
6a843348
JN
341 sed "
342 1 s/ 0 / 1 /
343 2 s/ 0 / 2 /
344 3 s/ 0 / 3 /
5caab8de 345 " stages >expected-stages &&
2161da10 346 git read-tree -u --reset HEAD &&
6a843348
JN
347
348 head=$(git rev-parse HEAD) &&
349 test_must_fail git revert picked &&
350 newhead=$(git rev-parse HEAD) &&
5caab8de 351 git ls-files --stage --unmerged >actual-stages &&
6a843348
JN
352
353 test "$head" = "$newhead" &&
354 test_must_fail git update-index --refresh -q &&
355 test_must_fail git diff-index --exit-code HEAD &&
356 test_cmp expected-stages actual-stages &&
7d056dea 357 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
6a843348
JN
358 test_cmp expected actual
359'
360
82433cdf
JN
361test_expect_success 'failed revert sets REVERT_HEAD' '
362 pristine_detach initial &&
363 test_must_fail git revert picked &&
364 test_cmp_rev picked REVERT_HEAD
365'
366
367test_expect_success 'successful revert does not set REVERT_HEAD' '
368 pristine_detach base &&
369 git revert base &&
370 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
371 test_must_fail git rev-parse --verify REVERT_HEAD
372'
373
374test_expect_success 'revert --no-commit sets REVERT_HEAD' '
375 pristine_detach base &&
376 git revert --no-commit base &&
377 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
378 test_cmp_rev base REVERT_HEAD
379'
380
381test_expect_success 'revert w/dirty tree does not set REVERT_HEAD' '
382 pristine_detach base &&
5caab8de 383 echo foo >foo &&
82433cdf
JN
384 test_must_fail git revert base &&
385 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
386 test_must_fail git rev-parse --verify REVERT_HEAD
387'
388
389test_expect_success 'GIT_CHERRY_PICK_HELP does not suppress REVERT_HEAD' '
390 pristine_detach initial &&
391 (
392 GIT_CHERRY_PICK_HELP="and then do something else" &&
393 GIT_REVERT_HELP="and then do something else, again" &&
394 export GIT_CHERRY_PICK_HELP GIT_REVERT_HELP &&
395 test_must_fail git revert picked
396 ) &&
397 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
398 test_cmp_rev picked REVERT_HEAD
399'
400
401test_expect_success 'git reset clears REVERT_HEAD' '
402 pristine_detach initial &&
403 test_must_fail git revert picked &&
404 git reset &&
405 test_must_fail git rev-parse --verify REVERT_HEAD
406'
407
408test_expect_success 'failed commit does not clear REVERT_HEAD' '
409 pristine_detach initial &&
410 test_must_fail git revert picked &&
411 test_must_fail git commit &&
412 test_cmp_rev picked REVERT_HEAD
413'
414
b07d9bfd 415test_expect_success 'successful final commit clears revert state' '
2def7f01 416 pristine_detach picked-signed &&
b07d9bfd 417
2def7f01
DL
418 test_must_fail git revert picked-signed base &&
419 echo resolved >foo &&
420 test_path_is_file .git/sequencer/todo &&
421 git commit -a &&
a8c663cf 422 test_path_is_missing .git/sequencer
b07d9bfd
PW
423'
424
425test_expect_success 'reset after final pick clears revert state' '
2def7f01 426 pristine_detach picked-signed &&
b07d9bfd 427
2def7f01
DL
428 test_must_fail git revert picked-signed base &&
429 echo resolved >foo &&
430 test_path_is_file .git/sequencer/todo &&
431 git reset &&
a8c663cf 432 test_path_is_missing .git/sequencer
b07d9bfd
PW
433'
434
6a843348 435test_expect_success 'revert conflict, diff3 -m style' '
2161da10 436 pristine_detach initial &&
6a843348 437 git config merge.conflictstyle diff3 &&
5caab8de 438 cat <<-EOF >expected &&
6a843348
JN
439 <<<<<<< HEAD
440 a
7d056dea 441 ||||||| objid (picked)
6a843348
JN
442 c
443 =======
444 b
7d056dea 445 >>>>>>> parent of objid (picked)
6a843348
JN
446 EOF
447
6a843348
JN
448 test_must_fail git revert picked &&
449
7d056dea 450 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
6a843348
JN
451 test_cmp expected actual
452'
453
1a2b985f
DL
454test_expect_success \
455 'revert conflict, ensure commit.cleanup = scissors places scissors line properly' '
456 pristine_detach initial &&
457 git config commit.cleanup scissors &&
458 cat >expected <<-EOF &&
459 Revert "picked"
460
461 This reverts commit OBJID.
462
463 # ------------------------ >8 ------------------------
464 # Do not modify or remove the line above.
465 # Everything below it will be ignored.
466 #
467 # Conflicts:
468 # foo
469 EOF
470
471 test_must_fail git revert picked &&
472
473 sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
1108cea7 474 test_cmp expected actual
1a2b985f
DL
475'
476
477test_expect_success \
478 'revert conflict, ensure cleanup=scissors places scissors line properly' '
479 pristine_detach initial &&
480 git config --unset commit.cleanup &&
481 cat >expected <<-EOF &&
482 Revert "picked"
483
484 This reverts commit OBJID.
485
486 # ------------------------ >8 ------------------------
487 # Do not modify or remove the line above.
488 # Everything below it will be ignored.
489 #
490 # Conflicts:
491 # foo
492 EOF
493
494 test_must_fail git revert --cleanup=scissors picked &&
495
496 sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
1108cea7 497 test_cmp expected actual
1a2b985f
DL
498'
499
5ed75e2a
MV
500test_expect_success 'failed cherry-pick does not forget -s' '
501 pristine_detach initial &&
502 test_must_fail git cherry-pick -s picked &&
6789275d 503 test_grep -e "Signed-off-by" .git/MERGE_MSG
5ed75e2a
MV
504'
505
506test_expect_success 'commit after failed cherry-pick does not add duplicated -s' '
507 pristine_detach initial &&
508 test_must_fail git cherry-pick -s picked-signed &&
509 git commit -a -s &&
5caab8de 510 test $(git show -s >tmp && grep -c "Signed-off-by" tmp && rm tmp) = 1
5ed75e2a
MV
511'
512
513test_expect_success 'commit after failed cherry-pick adds -s at the right place' '
514 pristine_detach initial &&
515 test_must_fail git cherry-pick picked &&
261f315b 516
5ed75e2a 517 git commit -a -s &&
5ed75e2a 518
261f315b
JH
519 # Do S-o-b and Conflicts appear in the right order?
520 cat <<-\EOF >expect &&
521 Signed-off-by: C O Mitter <committer@example.com>
522 # Conflicts:
523 EOF
c76b84a1 524 grep -e "^# Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
261f315b
JH
525 test_cmp expect actual &&
526
527 cat <<-\EOF >expected &&
528 picked
5ed75e2a 529
261f315b
JH
530 Signed-off-by: C O Mitter <committer@example.com>
531 EOF
5ed75e2a 532
261f315b 533 git show -s --pretty=format:%B >actual &&
5ed75e2a
MV
534 test_cmp expected actual
535'
536
261f315b
JH
537test_expect_success 'commit --amend -s places the sign-off at the right place' '
538 pristine_detach initial &&
539 test_must_fail git cherry-pick picked &&
540
541 # emulate old-style conflicts block
542 mv .git/MERGE_MSG .git/MERGE_MSG+ &&
5caab8de 543 sed -e "/^# Conflicts:/,\$s/^# *//" .git/MERGE_MSG+ >.git/MERGE_MSG &&
261f315b
JH
544
545 git commit -a &&
546 git commit --amend -s &&
547
548 # Do S-o-b and Conflicts appear in the right order?
549 cat <<-\EOF >expect &&
550 Signed-off-by: C O Mitter <committer@example.com>
551 Conflicts:
552 EOF
c76b84a1 553 grep -e "^Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
261f315b
JH
554 test_cmp expect actual
555'
556
2b75fb60 557test_expect_success 'cherry-pick preserves sparse-checkout' '
92203e64
BP
558 pristine_detach initial &&
559 test_config core.sparseCheckout true &&
560 test_when_finished "
561 echo \"/*\" >.git/info/sparse-checkout
562 git read-tree --reset -u HEAD
563 rm .git/info/sparse-checkout" &&
ead74601 564 mkdir .git/info &&
92203e64
BP
565 echo /unrelated >.git/info/sparse-checkout &&
566 git read-tree --reset -u HEAD &&
567 test_must_fail git cherry-pick -Xours picked>actual &&
6789275d 568 test_grep ! "Changes not staged for commit:" actual
92203e64
BP
569'
570
6860ce5d 571test_expect_success 'cherry-pick --continue remembers --keep-redundant-commits' '
36b0503a
PW
572 test_when_finished "git cherry-pick --abort || :" &&
573 pristine_detach initial &&
574 test_must_fail git cherry-pick --keep-redundant-commits picked redundant &&
575 echo c >foo &&
576 git add foo &&
577 git cherry-pick --continue
578'
579
6860ce5d 580test_expect_success 'cherry-pick --continue remembers --allow-empty and --allow-empty-message' '
36b0503a
PW
581 test_when_finished "git cherry-pick --abort || :" &&
582 pristine_detach initial &&
583 test_must_fail git cherry-pick --allow-empty --allow-empty-message \
584 picked empty &&
585 echo c >foo &&
586 git add foo &&
587 git cherry-pick --continue
588'
589
6a843348 590test_done