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
15 git checkout -f "$1^0" &&
16 git read-tree -u --reset HEAD &&
17 git clean -d -f -f -q -x
20 test_expect_success setup '
22 echo unrelated >unrelated &&
24 test_commit initial foo a &&
25 test_commit base foo b &&
26 test_commit picked foo c &&
27 test_commit --signoff picked-signed foo d &&
28 git config advice.detachedhead false
32 test_expect_success 'failed cherry-pick does not advance HEAD' '
33 pristine_detach initial &&
35 head=$(git rev-parse HEAD) &&
36 test_must_fail git cherry-pick picked &&
37 newhead=$(git rev-parse HEAD) &&
39 test "$head" = "$newhead"
42 test_expect_success 'advice from failed cherry-pick' "
43 pristine_detach initial &&
45 picked=\$(git rev-parse --short picked) &&
46 cat <<-EOF >expected &&
47 error: could not apply \$picked... picked
48 hint: after resolving the conflicts, mark the corrected paths
49 hint: with 'git add <paths>' or 'git rm <paths>'
50 hint: and commit the result with 'git commit'
52 test_must_fail git cherry-pick picked 2>actual &&
54 test_i18ncmp expected actual
57 test_expect_success 'advice from failed cherry-pick --no-commit' "
58 pristine_detach initial &&
60 picked=\$(git rev-parse --short picked) &&
61 cat <<-EOF >expected &&
62 error: could not apply \$picked... picked
63 hint: after resolving the conflicts, mark the corrected paths
64 hint: with 'git add <paths>' or 'git rm <paths>'
66 test_must_fail git cherry-pick --no-commit picked 2>actual &&
68 test_i18ncmp expected actual
71 test_expect_success 'failed cherry-pick sets CHERRY_PICK_HEAD' '
72 pristine_detach initial &&
73 test_must_fail git cherry-pick picked &&
74 test_cmp_rev picked CHERRY_PICK_HEAD
77 test_expect_success 'successful cherry-pick does not set CHERRY_PICK_HEAD' '
78 pristine_detach initial &&
79 git cherry-pick base &&
80 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
83 test_expect_success 'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
84 pristine_detach initial &&
85 git cherry-pick --no-commit base &&
86 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
89 test_expect_success 'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
90 pristine_detach initial &&
92 test_must_fail git cherry-pick base &&
93 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
97 'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
98 pristine_detach initial &&
100 test_must_fail git cherry-pick --strategy=resolve base &&
101 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
104 test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
105 pristine_detach initial &&
107 GIT_CHERRY_PICK_HELP="and then do something else" &&
108 export GIT_CHERRY_PICK_HELP &&
109 test_must_fail git cherry-pick picked
111 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
114 test_expect_success 'git reset clears CHERRY_PICK_HEAD' '
115 pristine_detach initial &&
117 test_must_fail git cherry-pick picked &&
120 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
123 test_expect_success 'failed commit does not clear CHERRY_PICK_HEAD' '
124 pristine_detach initial &&
126 test_must_fail git cherry-pick picked &&
127 test_must_fail git commit &&
129 test_cmp_rev picked CHERRY_PICK_HEAD
132 test_expect_success 'cancelled commit does not clear CHERRY_PICK_HEAD' '
133 pristine_detach initial &&
135 test_must_fail git cherry-pick picked &&
136 echo resolved >foo &&
138 git update-index --refresh -q &&
139 test_must_fail git diff-index --exit-code HEAD &&
143 test_must_fail git commit
146 test_cmp_rev picked CHERRY_PICK_HEAD
149 test_expect_success 'successful commit clears CHERRY_PICK_HEAD' '
150 pristine_detach initial &&
152 test_must_fail git cherry-pick picked &&
153 echo resolved >foo &&
157 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
160 test_expect_success 'failed cherry-pick produces dirty index' '
161 pristine_detach initial &&
163 test_must_fail git cherry-pick picked &&
165 test_must_fail git update-index --refresh -q &&
166 test_must_fail git diff-index --exit-code HEAD
169 test_expect_success 'failed cherry-pick registers participants in index' '
170 pristine_detach initial &&
172 git checkout base -- foo &&
173 git ls-files --stage foo &&
174 git checkout initial -- foo &&
175 git ls-files --stage foo &&
176 git checkout picked -- foo &&
177 git ls-files --stage foo
183 " < stages > expected &&
184 git read-tree -u --reset HEAD &&
186 test_must_fail git cherry-pick picked &&
187 git ls-files --stage --unmerged > actual &&
189 test_cmp expected actual
192 test_expect_success 'failed cherry-pick describes conflict in work tree' '
193 pristine_detach initial &&
194 cat <<-EOF > expected &&
202 test_must_fail git cherry-pick picked &&
204 sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
205 test_cmp expected actual
208 test_expect_success 'diff3 -m style' '
209 pristine_detach initial &&
210 git config merge.conflictstyle diff3 &&
211 cat <<-EOF > expected &&
214 ||||||| parent of objid picked
221 test_must_fail git cherry-pick picked &&
223 sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
224 test_cmp expected actual
227 test_expect_success 'revert also handles conflicts sanely' '
228 git config --unset merge.conflictstyle &&
229 pristine_detach initial &&
230 cat <<-EOF > expected &&
235 >>>>>>> parent of objid picked
238 git checkout picked -- foo &&
239 git ls-files --stage foo &&
240 git checkout initial -- foo &&
241 git ls-files --stage foo &&
242 git checkout base -- foo &&
243 git ls-files --stage foo
249 " < stages > expected-stages &&
250 git read-tree -u --reset HEAD &&
252 head=$(git rev-parse HEAD) &&
253 test_must_fail git revert picked &&
254 newhead=$(git rev-parse HEAD) &&
255 git ls-files --stage --unmerged > actual-stages &&
257 test "$head" = "$newhead" &&
258 test_must_fail git update-index --refresh -q &&
259 test_must_fail git diff-index --exit-code HEAD &&
260 test_cmp expected-stages actual-stages &&
261 sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
262 test_cmp expected actual
265 test_expect_success 'failed revert sets REVERT_HEAD' '
266 pristine_detach initial &&
267 test_must_fail git revert picked &&
268 test_cmp_rev picked REVERT_HEAD
271 test_expect_success 'successful revert does not set REVERT_HEAD' '
272 pristine_detach base &&
274 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
275 test_must_fail git rev-parse --verify REVERT_HEAD
278 test_expect_success 'revert --no-commit sets REVERT_HEAD' '
279 pristine_detach base &&
280 git revert --no-commit base &&
281 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
282 test_cmp_rev base REVERT_HEAD
285 test_expect_success 'revert w/dirty tree does not set REVERT_HEAD' '
286 pristine_detach base &&
288 test_must_fail git revert base &&
289 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
290 test_must_fail git rev-parse --verify REVERT_HEAD
293 test_expect_success 'GIT_CHERRY_PICK_HELP does not suppress REVERT_HEAD' '
294 pristine_detach initial &&
296 GIT_CHERRY_PICK_HELP="and then do something else" &&
297 GIT_REVERT_HELP="and then do something else, again" &&
298 export GIT_CHERRY_PICK_HELP GIT_REVERT_HELP &&
299 test_must_fail git revert picked
301 test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
302 test_cmp_rev picked REVERT_HEAD
305 test_expect_success 'git reset clears REVERT_HEAD' '
306 pristine_detach initial &&
307 test_must_fail git revert picked &&
309 test_must_fail git rev-parse --verify REVERT_HEAD
312 test_expect_success 'failed commit does not clear REVERT_HEAD' '
313 pristine_detach initial &&
314 test_must_fail git revert picked &&
315 test_must_fail git commit &&
316 test_cmp_rev picked REVERT_HEAD
319 test_expect_success 'revert conflict, diff3 -m style' '
320 pristine_detach initial &&
321 git config merge.conflictstyle diff3 &&
322 cat <<-EOF > expected &&
329 >>>>>>> parent of objid picked
332 test_must_fail git revert picked &&
334 sed "s/[a-f0-9]*\.\.\./objid/" foo > actual &&
335 test_cmp expected actual
338 test_expect_success 'failed cherry-pick does not forget -s' '
339 pristine_detach initial &&
340 test_must_fail git cherry-pick -s picked &&
341 test_i18ngrep -e "Signed-off-by" .git/MERGE_MSG
344 test_expect_success 'commit after failed cherry-pick does not add duplicated -s' '
345 pristine_detach initial &&
346 test_must_fail git cherry-pick -s picked-signed &&
348 test $(git show -s |grep -c "Signed-off-by") = 1
351 test_expect_success 'commit after failed cherry-pick adds -s at the right place' '
352 pristine_detach initial &&
353 test_must_fail git cherry-pick picked &&
357 # Do S-o-b and Conflicts appear in the right order?
358 cat <<-\EOF >expect &&
359 Signed-off-by: C O Mitter <committer@example.com>
362 grep -e "^# Conflicts:" -e '^Signed-off-by' <.git/COMMIT_EDITMSG >actual &&
363 test_cmp expect actual &&
365 cat <<-\EOF >expected &&
368 Signed-off-by: C O Mitter <committer@example.com>
371 git show -s --pretty=format:%B >actual &&
372 test_cmp expected actual
375 test_expect_success 'commit --amend -s places the sign-off at the right place' '
376 pristine_detach initial &&
377 test_must_fail git cherry-pick picked &&
379 # emulate old-style conflicts block
380 mv .git/MERGE_MSG .git/MERGE_MSG+ &&
381 sed -e "/^# Conflicts:/,\$s/^# *//" <.git/MERGE_MSG+ >.git/MERGE_MSG &&
384 git commit --amend -s &&
386 # Do S-o-b and Conflicts appear in the right order?
387 cat <<-\EOF >expect &&
388 Signed-off-by: C O Mitter <committer@example.com>
391 grep -e "^Conflicts:" -e '^Signed-off-by' <.git/COMMIT_EDITMSG >actual &&
392 test_cmp expect actual
395 test_expect_success 'cherry-pick preserves sparse-checkout' '
396 pristine_detach initial &&
397 test_config core.sparseCheckout true &&
399 echo \"/*\" >.git/info/sparse-checkout
400 git read-tree --reset -u HEAD
401 rm .git/info/sparse-checkout" &&
402 echo /unrelated >.git/info/sparse-checkout &&
403 git read-tree --reset -u HEAD &&
404 test_must_fail git cherry-pick -Xours picked>actual &&
405 test_i18ngrep ! "Changes not staged for commit:" actual