3 test_description
='test cherry-pick and revert with conflicts
6 + picked: rewrites foo to c
7 + base: rewrites foo to b
8 + initial: writes foo as a, unrelated as unrelated
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
18 git checkout
-f "$1^0" &&
19 git read-tree
-u --reset HEAD
&&
20 git clean
-d -f -f -q -x
23 test_expect_success setup
'
25 echo unrelated >unrelated &&
27 test_commit initial foo a &&
28 test_commit base foo b &&
29 test_commit picked foo c &&
30 test_commit --signoff picked-signed foo d &&
31 git checkout -b topic initial &&
32 test_commit redundant-pick foo c redundant &&
33 git commit --allow-empty --allow-empty-message &&
36 git config advice.detachedhead false
40 test_expect_success
'failed cherry-pick does not advance HEAD' '
41 pristine_detach initial &&
43 head=$(git rev-parse HEAD) &&
44 test_must_fail git cherry-pick picked &&
45 newhead=$(git rev-parse HEAD) &&
47 test "$head" = "$newhead"
50 test_expect_success
'advice from failed cherry-pick' '
51 pristine_detach initial &&
53 picked=$(git rev-parse --short picked) &&
54 cat <<-EOF >expected &&
55 error: could not apply $picked... picked
56 hint: After resolving the conflicts, mark them with
57 hint: "git add/rm <pathspec>", then run
58 hint: "git cherry-pick --continue".
59 hint: You can instead skip this commit with "git cherry-pick --skip".
60 hint: To abort and get back to the state before "git cherry-pick",
61 hint: run "git cherry-pick --abort".
63 test_must_fail git cherry-pick picked 2>actual &&
65 test_cmp expected actual
68 test_expect_success
'advice from failed cherry-pick --no-commit' "
69 pristine_detach initial &&
71 picked=\$(git rev-parse --short picked) &&
72 cat <<-EOF >expected &&
73 error: could not apply \$picked... picked
74 hint: after resolving the conflicts, mark the corrected paths
75 hint: with 'git add <paths>' or 'git rm <paths>'
77 test_must_fail git cherry-pick --no-commit picked 2>actual &&
79 test_cmp expected actual
82 test_expect_success
'failed cherry-pick sets CHERRY_PICK_HEAD' '
83 pristine_detach initial &&
84 test_must_fail git cherry-pick picked &&
85 test_cmp_rev picked CHERRY_PICK_HEAD
88 test_expect_success
'successful cherry-pick does not set CHERRY_PICK_HEAD' '
89 pristine_detach initial &&
90 git cherry-pick base &&
91 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
94 test_expect_success
'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
95 pristine_detach initial &&
96 git cherry-pick --no-commit base &&
97 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
100 test_expect_success
'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
101 pristine_detach initial &&
103 test_must_fail git cherry-pick base &&
104 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
107 test_expect_success \
108 'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
109 pristine_detach initial &&
111 test_must_fail git cherry-pick --strategy=resolve base &&
112 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
115 test_expect_success
'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
116 pristine_detach initial &&
118 GIT_CHERRY_PICK_HELP="and then do something else" &&
119 export GIT_CHERRY_PICK_HELP &&
120 test_must_fail git cherry-pick picked
122 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
125 test_expect_success
'git reset clears CHERRY_PICK_HEAD' '
126 pristine_detach initial &&
128 test_must_fail git cherry-pick picked &&
131 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
134 test_expect_success
'failed commit does not clear CHERRY_PICK_HEAD' '
135 pristine_detach initial &&
137 test_must_fail git cherry-pick picked &&
138 test_must_fail git commit &&
140 test_cmp_rev picked CHERRY_PICK_HEAD
143 test_expect_success
'cancelled commit does not clear CHERRY_PICK_HEAD' '
144 pristine_detach initial &&
146 test_must_fail git cherry-pick picked &&
147 echo resolved >foo &&
149 git update-index --refresh -q &&
150 test_must_fail git diff-index --exit-code HEAD &&
154 test_must_fail git commit
157 test_cmp_rev picked CHERRY_PICK_HEAD
160 test_expect_success
'successful commit clears CHERRY_PICK_HEAD' '
161 pristine_detach initial &&
163 test_must_fail git cherry-pick picked &&
164 echo resolved >foo &&
168 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
171 test_expect_success
'partial commit of cherry-pick fails' '
172 pristine_detach initial &&
174 test_must_fail git cherry-pick picked &&
175 echo resolved >foo &&
177 test_must_fail git commit foo 2>err &&
179 test_i18ngrep "cannot do a partial commit during a cherry-pick." err
182 test_expect_success
'commit --amend of cherry-pick fails' '
183 pristine_detach initial &&
185 test_must_fail git cherry-pick picked &&
186 echo resolved >foo &&
188 test_must_fail git commit --amend 2>err &&
190 test_i18ngrep "in the middle of a cherry-pick -- cannot amend." err
193 test_expect_success
'successful final commit clears cherry-pick state' '
194 pristine_detach initial &&
196 test_must_fail git cherry-pick base picked-signed &&
197 echo resolved >foo &&
198 test_path_is_file .git/sequencer/todo &&
200 test_path_is_missing .git/sequencer
203 test_expect_success
'reset after final pick clears cherry-pick state' '
204 pristine_detach initial &&
206 test_must_fail git cherry-pick base picked-signed &&
207 echo resolved >foo &&
208 test_path_is_file .git/sequencer/todo &&
210 test_path_is_missing .git/sequencer
213 test_expect_success
'failed cherry-pick produces dirty index' '
214 pristine_detach initial &&
216 test_must_fail git cherry-pick picked &&
218 test_must_fail git update-index --refresh -q &&
219 test_must_fail git diff-index --exit-code HEAD
222 test_expect_success
'failed cherry-pick registers participants in index' '
223 pristine_detach initial &&
225 git checkout base -- foo &&
226 git ls-files --stage foo &&
227 git checkout initial -- foo &&
228 git ls-files --stage foo &&
229 git checkout picked -- foo &&
230 git ls-files --stage foo
236 " stages >expected &&
237 git read-tree -u --reset HEAD &&
239 test_must_fail git cherry-pick picked &&
240 git ls-files --stage --unmerged >actual &&
242 test_cmp expected actual
245 test_expect_success \
246 'cherry-pick conflict, ensure commit.cleanup = scissors places scissors line properly' '
247 pristine_detach initial &&
248 git config commit.cleanup scissors &&
249 cat <<-EOF >expected &&
252 # ------------------------ >8 ------------------------
253 # Do not modify or remove the line above.
254 # Everything below it will be ignored.
260 test_must_fail git cherry-pick picked &&
262 test_cmp expected .git/MERGE_MSG
265 test_expect_success \
266 'cherry-pick conflict, ensure cleanup=scissors places scissors line properly' '
267 pristine_detach initial &&
268 git config --unset commit.cleanup &&
269 cat <<-EOF >expected &&
272 # ------------------------ >8 ------------------------
273 # Do not modify or remove the line above.
274 # Everything below it will be ignored.
280 test_must_fail git cherry-pick --cleanup=scissors picked &&
282 test_cmp expected .git/MERGE_MSG
285 test_expect_success
'failed cherry-pick describes conflict in work tree' '
286 pristine_detach initial &&
287 cat <<-EOF >expected &&
292 >>>>>>> objid (picked)
295 test_must_fail git cherry-pick picked &&
297 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
298 test_cmp expected actual
301 test_expect_success
'diff3 -m style' '
302 pristine_detach initial &&
303 git config merge.conflictstyle diff3 &&
304 cat <<-EOF >expected &&
307 ||||||| parent of objid (picked)
311 >>>>>>> objid (picked)
314 test_must_fail git cherry-pick picked &&
316 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
317 test_cmp expected actual
320 test_expect_success
'revert also handles conflicts sanely' '
321 git config --unset merge.conflictstyle &&
322 pristine_detach initial &&
323 cat <<-EOF >expected &&
328 >>>>>>> parent of objid (picked)
331 git checkout picked -- foo &&
332 git ls-files --stage foo &&
333 git checkout initial -- foo &&
334 git ls-files --stage foo &&
335 git checkout base -- foo &&
336 git ls-files --stage foo
342 " stages >expected-stages &&
343 git read-tree -u --reset HEAD &&
345 head=$(git rev-parse HEAD) &&
346 test_must_fail git revert picked &&
347 newhead=$(git rev-parse HEAD) &&
348 git ls-files --stage --unmerged >actual-stages &&
350 test "$head" = "$newhead" &&
351 test_must_fail git update-index --refresh -q &&
352 test_must_fail git diff-index --exit-code HEAD &&
353 test_cmp expected-stages actual-stages &&
354 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
355 test_cmp expected actual
358 test_expect_success
'failed revert sets REVERT_HEAD' '
359 pristine_detach initial &&
360 test_must_fail git revert picked &&
361 test_cmp_rev picked REVERT_HEAD
364 test_expect_success
'successful revert does not set REVERT_HEAD' '
365 pristine_detach base &&
367 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
368 test_must_fail git rev-parse --verify REVERT_HEAD
371 test_expect_success
'revert --no-commit sets REVERT_HEAD' '
372 pristine_detach base &&
373 git revert --no-commit base &&
374 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
375 test_cmp_rev base REVERT_HEAD
378 test_expect_success
'revert w/dirty tree does not set REVERT_HEAD' '
379 pristine_detach base &&
381 test_must_fail git revert base &&
382 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
383 test_must_fail git rev-parse --verify REVERT_HEAD
386 test_expect_success
'GIT_CHERRY_PICK_HELP does not suppress REVERT_HEAD' '
387 pristine_detach initial &&
389 GIT_CHERRY_PICK_HELP="and then do something else" &&
390 GIT_REVERT_HELP="and then do something else, again" &&
391 export GIT_CHERRY_PICK_HELP GIT_REVERT_HELP &&
392 test_must_fail git revert picked
394 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
395 test_cmp_rev picked REVERT_HEAD
398 test_expect_success
'git reset clears REVERT_HEAD' '
399 pristine_detach initial &&
400 test_must_fail git revert picked &&
402 test_must_fail git rev-parse --verify REVERT_HEAD
405 test_expect_success
'failed commit does not clear REVERT_HEAD' '
406 pristine_detach initial &&
407 test_must_fail git revert picked &&
408 test_must_fail git commit &&
409 test_cmp_rev picked REVERT_HEAD
412 test_expect_success
'successful final commit clears revert state' '
413 pristine_detach picked-signed &&
415 test_must_fail git revert picked-signed base &&
416 echo resolved >foo &&
417 test_path_is_file .git/sequencer/todo &&
419 test_path_is_missing .git/sequencer
422 test_expect_success
'reset after final pick clears revert state' '
423 pristine_detach picked-signed &&
425 test_must_fail git revert picked-signed base &&
426 echo resolved >foo &&
427 test_path_is_file .git/sequencer/todo &&
429 test_path_is_missing .git/sequencer
432 test_expect_success
'revert conflict, diff3 -m style' '
433 pristine_detach initial &&
434 git config merge.conflictstyle diff3 &&
435 cat <<-EOF >expected &&
438 ||||||| objid (picked)
442 >>>>>>> parent of objid (picked)
445 test_must_fail git revert picked &&
447 sed "s/[a-f0-9]* (/objid (/" foo >actual &&
448 test_cmp expected actual
451 test_expect_success \
452 'revert conflict, ensure commit.cleanup = scissors places scissors line properly' '
453 pristine_detach initial &&
454 git config commit.cleanup scissors &&
455 cat >expected <<-EOF &&
458 This reverts commit OBJID.
460 # ------------------------ >8 ------------------------
461 # Do not modify or remove the line above.
462 # Everything below it will be ignored.
468 test_must_fail git revert picked &&
470 sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
471 test_cmp expected actual
474 test_expect_success \
475 'revert conflict, ensure cleanup=scissors places scissors line properly' '
476 pristine_detach initial &&
477 git config --unset commit.cleanup &&
478 cat >expected <<-EOF &&
481 This reverts commit OBJID.
483 # ------------------------ >8 ------------------------
484 # Do not modify or remove the line above.
485 # Everything below it will be ignored.
491 test_must_fail git revert --cleanup=scissors picked &&
493 sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
494 test_cmp expected actual
497 test_expect_success
'failed cherry-pick does not forget -s' '
498 pristine_detach initial &&
499 test_must_fail git cherry-pick -s picked &&
500 test_i18ngrep -e "Signed-off-by" .git/MERGE_MSG
503 test_expect_success
'commit after failed cherry-pick does not add duplicated -s' '
504 pristine_detach initial &&
505 test_must_fail git cherry-pick -s picked-signed &&
507 test $(git show -s >tmp && grep -c "Signed-off-by" tmp && rm tmp) = 1
510 test_expect_success
'commit after failed cherry-pick adds -s at the right place' '
511 pristine_detach initial &&
512 test_must_fail git cherry-pick picked &&
516 # Do S-o-b and Conflicts appear in the right order?
517 cat <<-\EOF >expect &&
518 Signed-off-by: C O Mitter <committer@example.com>
521 grep -e "^# Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
522 test_cmp expect actual &&
524 cat <<-\EOF >expected &&
527 Signed-off-by: C O Mitter <committer@example.com>
530 git show -s --pretty=format:%B >actual &&
531 test_cmp expected actual
534 test_expect_success
'commit --amend -s places the sign-off at the right place' '
535 pristine_detach initial &&
536 test_must_fail git cherry-pick picked &&
538 # emulate old-style conflicts block
539 mv .git/MERGE_MSG .git/MERGE_MSG+ &&
540 sed -e "/^# Conflicts:/,\$s/^# *//" .git/MERGE_MSG+ >.git/MERGE_MSG &&
543 git commit --amend -s &&
545 # Do S-o-b and Conflicts appear in the right order?
546 cat <<-\EOF >expect &&
547 Signed-off-by: C O Mitter <committer@example.com>
550 grep -e "^Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
551 test_cmp expect actual
554 test_expect_success
'cherry-pick preserves sparse-checkout' '
555 pristine_detach initial &&
556 test_config core.sparseCheckout true &&
558 echo \"/*\" >.git/info/sparse-checkout
559 git read-tree --reset -u HEAD
560 rm .git/info/sparse-checkout" &&
561 echo /unrelated >.git/info/sparse-checkout &&
562 git read-tree --reset -u HEAD &&
563 test_must_fail git cherry-pick -Xours picked>actual &&
564 test_i18ngrep ! "Changes not staged for commit:" actual
567 test_expect_success
'cherry-pick --continue remembers --keep-redundant-commits' '
568 test_when_finished "git cherry-pick --abort || :" &&
569 pristine_detach initial &&
570 test_must_fail git cherry-pick --keep-redundant-commits picked redundant &&
573 git cherry-pick --continue
576 test_expect_success
'cherry-pick --continue remembers --allow-empty and --allow-empty-message' '
577 test_when_finished "git cherry-pick --abort || :" &&
578 pristine_detach initial &&
579 test_must_fail git cherry-pick --allow-empty --allow-empty-message \
583 git cherry-pick --continue