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