]>
Commit | Line | Data |
---|---|---|
abc02670 JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Junio C Hamano | |
4 | # | |
5 | ||
47a528ad | 6 | test_description='git checkout tests. |
3e0318a3 | 7 | |
01dc8133 | 8 | Creates main, forks renamer and side branches from it. |
3e0318a3 JH |
9 | Test switching across them. |
10 | ||
01dc8133 | 11 | ! [main] Initial A one, A two |
3e0318a3 JH |
12 | * [renamer] Renamer R one->uno, M two |
13 | ! [side] Side M one, D two, A three | |
47349a8c JN |
14 | ! [simple] Simple D one, M two |
15 | ---- | |
16 | + [simple] Simple D one, M two | |
17 | + [side] Side M one, D two, A three | |
18 | * [renamer] Renamer R one->uno, M two | |
01dc8133 | 19 | +*++ [main] Initial A one, A two |
3e0318a3 JH |
20 | |
21 | ' | |
abc02670 | 22 | |
01dc8133 | 23 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
24 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
25 | ||
abc02670 JH |
26 | . ./test-lib.sh |
27 | ||
c07c7bf6 DB |
28 | test_tick |
29 | ||
abc02670 JH |
30 | fill () { |
31 | for i | |
32 | do | |
33 | echo "$i" | |
34 | done | |
35 | } | |
36 | ||
c1a4278e | 37 | |
abc02670 | 38 | test_expect_success setup ' |
c327762f | 39 | fill x y z >same && |
c1a4278e | 40 | fill 1 2 3 4 5 6 7 8 >one && |
abc02670 | 41 | fill a b c d e >two && |
c07c7bf6 | 42 | git add same one two && |
abc02670 JH |
43 | git commit -m "Initial A one, A two" && |
44 | ||
c1a4278e JH |
45 | git checkout -b renamer && |
46 | rm -f one && | |
47 | fill 1 3 4 5 6 7 8 >uno && | |
48 | git add uno && | |
49 | fill a b c d e f >two && | |
50 | git commit -a -m "Renamer R one->uno, M two" && | |
51 | ||
01dc8133 | 52 | git checkout -b side main && |
c1a4278e | 53 | fill 1 2 3 4 5 6 7 >one && |
abc02670 JH |
54 | fill A B C D E >three && |
55 | rm -f two && | |
56 | git update-index --add --remove one two three && | |
57 | git commit -m "Side M one, D two, A three" && | |
58 | ||
01dc8133 | 59 | git checkout -b simple main && |
47349a8c | 60 | rm -f one && |
c327762f | 61 | fill a c e >two && |
47349a8c JN |
62 | git commit -a -m "Simple D one, M two" && |
63 | ||
01dc8133 | 64 | git checkout main |
abc02670 JH |
65 | ' |
66 | ||
e166fe36 | 67 | test_expect_success 'checkout from non-existing branch' ' |
01dc8133 | 68 | git checkout -b delete-me main && |
cbc5cf7c | 69 | git update-ref -d --no-deref refs/heads/delete-me && |
5a03e7f2 | 70 | test refs/heads/delete-me = "$(git symbolic-ref HEAD)" && |
01dc8133 JS |
71 | git checkout main && |
72 | test refs/heads/main = "$(git symbolic-ref HEAD)" | |
5a03e7f2 SP |
73 | ' |
74 | ||
e166fe36 | 75 | test_expect_success 'checkout with dirty tree without -m' ' |
c1a4278e | 76 | fill 0 1 2 3 4 5 6 7 8 >one && |
abc02670 JH |
77 | if git checkout side |
78 | then | |
79 | echo Not happy | |
80 | false | |
81 | else | |
82 | echo "happy - failed correctly" | |
83 | fi | |
abc02670 JH |
84 | ' |
85 | ||
e166fe36 | 86 | test_expect_success 'checkout with unrelated dirty tree without -m' ' |
01dc8133 | 87 | git checkout -f main && |
c07c7bf6 | 88 | fill 0 1 2 3 4 5 6 7 8 >same && |
aef591a0 | 89 | cp same kept && |
c07c7bf6 | 90 | git checkout side >messages && |
aef591a0 JK |
91 | test_cmp same kept && |
92 | printf "M\t%s\n" same >messages.expect && | |
82ebb0b6 | 93 | test_cmp messages.expect messages |
c07c7bf6 DB |
94 | ' |
95 | ||
e166fe36 | 96 | test_expect_success 'checkout -m with dirty tree' ' |
01dc8133 | 97 | git checkout -f main && |
562ca192 | 98 | git clean -f && |
abc02670 | 99 | |
c1a4278e | 100 | fill 0 1 2 3 4 5 6 7 8 >one && |
c327762f | 101 | git checkout -m side >messages && |
abc02670 | 102 | |
c1a4278e | 103 | test "$(git symbolic-ref HEAD)" = "refs/heads/side" && |
abc02670 | 104 | |
aef591a0 | 105 | printf "M\t%s\n" one >expect.messages && |
82ebb0b6 | 106 | test_cmp expect.messages messages && |
c07c7bf6 | 107 | |
01dc8133 JS |
108 | fill "M one" "A three" "D two" >expect.main && |
109 | git diff --name-status main >current.main && | |
110 | test_cmp expect.main current.main && | |
abc02670 JH |
111 | |
112 | fill "M one" >expect.side && | |
113 | git diff --name-status side >current.side && | |
82ebb0b6 | 114 | test_cmp expect.side current.side && |
abc02670 | 115 | |
abc02670 | 116 | git diff --cached >current.index && |
1c5e94f4 | 117 | test_must_be_empty current.index |
abc02670 JH |
118 | ' |
119 | ||
e166fe36 | 120 | test_expect_success 'checkout -m with dirty tree, renamed' ' |
01dc8133 | 121 | git checkout -f main && git clean -f && |
c1a4278e JH |
122 | |
123 | fill 1 2 3 4 5 7 8 >one && | |
124 | if git checkout renamer | |
125 | then | |
126 | echo Not happy | |
127 | false | |
128 | else | |
129 | echo "happy - failed correctly" | |
130 | fi && | |
131 | ||
132 | git checkout -m renamer && | |
133 | fill 1 3 4 5 7 8 >expect && | |
82ebb0b6 | 134 | test_cmp expect uno && |
d7ebd53d JH |
135 | ! test -f one && |
136 | git diff --cached >current && | |
ec10b018 | 137 | test_must_be_empty current |
d7ebd53d JH |
138 | ' |
139 | ||
140 | test_expect_success 'checkout -m with merge conflict' ' | |
01dc8133 | 141 | git checkout -f main && git clean -f && |
d7ebd53d JH |
142 | |
143 | fill 1 T 3 4 5 6 S 8 >one && | |
144 | if git checkout renamer | |
145 | then | |
146 | echo Not happy | |
147 | false | |
148 | else | |
149 | echo "happy - failed correctly" | |
150 | fi && | |
151 | ||
152 | git checkout -m renamer && | |
153 | ||
01dc8133 | 154 | git diff main:one :3:uno | |
d7ebd53d JH |
155 | sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current && |
156 | fill d2 aT d7 aS >expect && | |
dcbaa0b3 | 157 | test_cmp expect current && |
d7ebd53d | 158 | git diff --cached two >current && |
ec10b018 | 159 | test_must_be_empty current |
c1a4278e JH |
160 | ' |
161 | ||
47349a8c | 162 | test_expect_success 'format of merge conflict from checkout -m' ' |
01dc8133 | 163 | git checkout -f main && |
1c0ab5c7 | 164 | git clean -f && |
47349a8c | 165 | |
c327762f | 166 | fill b d >two && |
47349a8c JN |
167 | git checkout -m simple && |
168 | ||
169 | git ls-files >current && | |
170 | fill same two two two >expect && | |
dcbaa0b3 | 171 | test_cmp expect current && |
47349a8c JN |
172 | |
173 | cat <<-EOF >expect && | |
174 | <<<<<<< simple | |
175 | a | |
176 | c | |
177 | e | |
178 | ======= | |
179 | b | |
180 | d | |
181 | >>>>>>> local | |
182 | EOF | |
9c5b2fab | 183 | test_cmp expect two |
47349a8c JN |
184 | ' |
185 | ||
186 | test_expect_success 'checkout --merge --conflict=diff3 <branch>' ' | |
01dc8133 | 187 | git checkout -f main && |
1c0ab5c7 CM |
188 | git reset --hard && |
189 | git clean -f && | |
47349a8c | 190 | |
c327762f | 191 | fill b d >two && |
47349a8c JN |
192 | git checkout --merge --conflict=diff3 simple && |
193 | ||
194 | cat <<-EOF >expect && | |
195 | <<<<<<< simple | |
196 | a | |
197 | c | |
198 | e | |
01dc8133 | 199 | ||||||| main |
47349a8c JN |
200 | a |
201 | b | |
202 | c | |
203 | d | |
204 | e | |
205 | ======= | |
206 | b | |
207 | d | |
208 | >>>>>>> local | |
209 | EOF | |
9c5b2fab | 210 | test_cmp expect two |
47349a8c JN |
211 | ' |
212 | ||
6a143aa2 | 213 | test_expect_success 'switch to another branch while carrying a deletion' ' |
01dc8133 | 214 | git checkout -f main && |
1c0ab5c7 CM |
215 | git reset --hard && |
216 | git clean -f && | |
6a143aa2 JN |
217 | git rm two && |
218 | ||
219 | test_must_fail git checkout simple 2>errs && | |
6789275d | 220 | test_grep overwritten errs && |
6a143aa2 | 221 | |
3e41485d | 222 | test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs && |
6eff409e | 223 | test_must_be_empty errs |
6a143aa2 JN |
224 | ' |
225 | ||
69382976 | 226 | test_expect_success 'checkout to detach HEAD (with advice declined)' ' |
13be3e31 | 227 | git config advice.detachedHead false && |
d482c234 | 228 | rev=$(git rev-parse --short renamer^) && |
1c0ab5c7 CM |
229 | git checkout -f renamer && |
230 | git clean -f && | |
c07c7bf6 | 231 | git checkout renamer^ 2>messages && |
6789275d | 232 | test_grep "HEAD is now at $rev" messages && |
3fb0459b | 233 | test_line_count = 1 messages && |
13be3e31 | 234 | H=$(git rev-parse --verify HEAD) && |
01dc8133 | 235 | M=$(git show-ref -s --verify refs/heads/main) && |
13be3e31 JH |
236 | test "z$H" = "z$M" && |
237 | if git symbolic-ref HEAD >/dev/null 2>&1 | |
238 | then | |
239 | echo "OOPS, HEAD is still symbolic???" | |
240 | false | |
241 | else | |
242 | : happy | |
243 | fi | |
244 | ' | |
245 | ||
69382976 | 246 | test_expect_success 'checkout to detach HEAD' ' |
13be3e31 | 247 | git config advice.detachedHead true && |
d482c234 | 248 | rev=$(git rev-parse --short renamer^) && |
1c0ab5c7 CM |
249 | git checkout -f renamer && |
250 | git clean -f && | |
73c01d25 | 251 | git checkout renamer^ 2>messages && |
d482c234 | 252 | grep "HEAD is now at $rev" messages && |
6cdccfce | 253 | test_line_count -gt 1 messages && |
3e0318a3 | 254 | H=$(git rev-parse --verify HEAD) && |
01dc8133 | 255 | M=$(git show-ref -s --verify refs/heads/main) && |
3e0318a3 JH |
256 | test "z$H" = "z$M" && |
257 | if git symbolic-ref HEAD >/dev/null 2>&1 | |
258 | then | |
259 | echo "OOPS, HEAD is still symbolic???" | |
260 | false | |
261 | else | |
262 | : happy | |
263 | fi | |
264 | ' | |
265 | ||
69382976 | 266 | test_expect_success 'checkout to detach HEAD with branchname^' ' |
01dc8133 | 267 | git checkout -f main && |
1c0ab5c7 | 268 | git clean -f && |
3e0318a3 JH |
269 | git checkout renamer^ && |
270 | H=$(git rev-parse --verify HEAD) && | |
01dc8133 | 271 | M=$(git show-ref -s --verify refs/heads/main) && |
3e0318a3 JH |
272 | test "z$H" = "z$M" && |
273 | if git symbolic-ref HEAD >/dev/null 2>&1 | |
274 | then | |
275 | echo "OOPS, HEAD is still symbolic???" | |
276 | false | |
277 | else | |
278 | : happy | |
279 | fi | |
280 | ' | |
281 | ||
69382976 | 282 | test_expect_success 'checkout to detach HEAD with :/message' ' |
01dc8133 | 283 | git checkout -f main && |
1c0ab5c7 | 284 | git clean -f && |
b1e9efa7 DB |
285 | git checkout ":/Initial" && |
286 | H=$(git rev-parse --verify HEAD) && | |
01dc8133 | 287 | M=$(git show-ref -s --verify refs/heads/main) && |
b1e9efa7 DB |
288 | test "z$H" = "z$M" && |
289 | if git symbolic-ref HEAD >/dev/null 2>&1 | |
290 | then | |
291 | echo "OOPS, HEAD is still symbolic???" | |
292 | false | |
293 | else | |
294 | : happy | |
295 | fi | |
296 | ' | |
297 | ||
69382976 | 298 | test_expect_success 'checkout to detach HEAD with HEAD^0' ' |
01dc8133 | 299 | git checkout -f main && |
1c0ab5c7 | 300 | git clean -f && |
3e0318a3 JH |
301 | git checkout HEAD^0 && |
302 | H=$(git rev-parse --verify HEAD) && | |
01dc8133 | 303 | M=$(git show-ref -s --verify refs/heads/main) && |
3e0318a3 JH |
304 | test "z$H" = "z$M" && |
305 | if git symbolic-ref HEAD >/dev/null 2>&1 | |
306 | then | |
307 | echo "OOPS, HEAD is still symbolic???" | |
308 | false | |
309 | else | |
310 | : happy | |
311 | fi | |
312 | ' | |
313 | ||
5035242c | 314 | test_expect_success 'checkout with ambiguous tag/branch names' ' |
5035242c | 315 | git tag both side && |
01dc8133 | 316 | git branch both main && |
5035242c | 317 | git reset --hard && |
01dc8133 | 318 | git checkout main && |
5035242c JH |
319 | |
320 | git checkout both && | |
321 | H=$(git rev-parse --verify HEAD) && | |
01dc8133 | 322 | M=$(git show-ref -s --verify refs/heads/main) && |
5035242c JH |
323 | test "z$H" = "z$M" && |
324 | name=$(git symbolic-ref HEAD 2>/dev/null) && | |
325 | test "z$name" = zrefs/heads/both | |
5035242c JH |
326 | ' |
327 | ||
328 | test_expect_success 'checkout with ambiguous tag/branch names' ' | |
5035242c | 329 | git reset --hard && |
01dc8133 | 330 | git checkout main && |
5035242c JH |
331 | |
332 | git tag frotz side && | |
01dc8133 | 333 | git branch frotz main && |
5035242c | 334 | git reset --hard && |
01dc8133 | 335 | git checkout main && |
5035242c JH |
336 | |
337 | git checkout tags/frotz && | |
338 | H=$(git rev-parse --verify HEAD) && | |
339 | S=$(git show-ref -s --verify refs/heads/side) && | |
340 | test "z$H" = "z$S" && | |
341 | if name=$(git symbolic-ref HEAD 2>/dev/null) | |
342 | then | |
343 | echo "Bad -- should have detached" | |
344 | false | |
345 | else | |
346 | : happy | |
347 | fi | |
5035242c JH |
348 | ' |
349 | ||
6010d2d9 | 350 | test_expect_success 'switch branches while in subdirectory' ' |
6010d2d9 | 351 | git reset --hard && |
01dc8133 | 352 | git checkout main && |
6010d2d9 JH |
353 | |
354 | mkdir subs && | |
627f2d79 | 355 | git -C subs checkout side && |
6010d2d9 JH |
356 | ! test -f subs/one && |
357 | rm -fr subs | |
6010d2d9 JH |
358 | ' |
359 | ||
360 | test_expect_success 'checkout specific path while in subdirectory' ' | |
6010d2d9 JH |
361 | git reset --hard && |
362 | git checkout side && | |
363 | mkdir subs && | |
364 | >subs/bero && | |
365 | git add subs/bero && | |
366 | git commit -m "add subs/bero" && | |
367 | ||
01dc8133 | 368 | git checkout main && |
6010d2d9 | 369 | mkdir -p subs && |
627f2d79 | 370 | git -C subs checkout side -- bero && |
6010d2d9 | 371 | test -f subs/bero |
6010d2d9 JH |
372 | ' |
373 | ||
e166fe36 | 374 | test_expect_success 'checkout w/--track sets up tracking' ' |
10dae785 JC |
375 | git config branch.autosetupmerge false && |
376 | git checkout main && | |
377 | git checkout --track -b track1 && | |
378 | test "$(git config branch.track1.remote)" && | |
379 | test "$(git config branch.track1.merge)" | |
e166fe36 | 380 | ' |
9ed36cfa | 381 | |
e166fe36 | 382 | test_expect_success 'checkout w/autosetupmerge=always sets up tracking' ' |
10dae785 JC |
383 | test_when_finished git config branch.autosetupmerge false && |
384 | git config branch.autosetupmerge always && | |
385 | git checkout main && | |
386 | git checkout -b track2 && | |
387 | test "$(git config branch.track2.remote)" && | |
388 | test "$(git config branch.track2.merge)" | |
e166fe36 | 389 | ' |
9ed36cfa | 390 | |
51a94af8 | 391 | test_expect_success 'checkout w/--track from non-branch HEAD fails' ' |
10dae785 JC |
392 | git checkout main^0 && |
393 | test_must_fail git symbolic-ref HEAD && | |
394 | test_must_fail git checkout --track -b track && | |
395 | test_must_fail git rev-parse --verify track && | |
396 | test_must_fail git symbolic-ref HEAD && | |
397 | test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)" | |
51a94af8 | 398 | ' |
9ed36cfa | 399 | |
21b5b1e8 | 400 | test_expect_success 'checkout w/--track from tag fails' ' |
10dae785 JC |
401 | git checkout main^0 && |
402 | test_must_fail git symbolic-ref HEAD && | |
403 | test_must_fail git checkout --track -b track frotz && | |
404 | test_must_fail git rev-parse --verify track && | |
405 | test_must_fail git symbolic-ref HEAD && | |
406 | test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)" | |
21b5b1e8 JH |
407 | ' |
408 | ||
acd3b9ec | 409 | test_expect_success 'detach a symbolic link HEAD' ' |
10dae785 JC |
410 | git checkout main && |
411 | git config --bool core.prefersymlinkrefs yes && | |
412 | git checkout side && | |
413 | git checkout main && | |
414 | it=$(git symbolic-ref HEAD) && | |
415 | test "z$it" = zrefs/heads/main && | |
416 | here=$(git rev-parse --verify refs/heads/main) && | |
417 | git checkout side^ && | |
418 | test "z$(git rev-parse --verify refs/heads/main)" = "z$here" | |
f5637549 | 419 | ' |
9ed36cfa | 420 | |
e166fe36 | 421 | test_expect_success 'checkout with --track fakes a sensible -b <name>' ' |
10dae785 JC |
422 | git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && |
423 | git update-ref refs/remotes/origin/koala/bear renamer && | |
9188ed89 | 424 | |
10dae785 JC |
425 | git checkout --track origin/koala/bear && |
426 | test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && | |
427 | test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" && | |
9188ed89 | 428 | |
10dae785 | 429 | git checkout main && git branch -D koala/bear && |
9188ed89 | 430 | |
10dae785 JC |
431 | git checkout --track refs/remotes/origin/koala/bear && |
432 | test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && | |
433 | test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" && | |
9188ed89 | 434 | |
10dae785 | 435 | git checkout main && git branch -D koala/bear && |
9188ed89 | 436 | |
10dae785 JC |
437 | git checkout --track remotes/origin/koala/bear && |
438 | test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && | |
439 | test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" | |
9188ed89 | 440 | ' |
bb0ceb62 | 441 | |
e166fe36 | 442 | test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' ' |
10dae785 | 443 | test_must_fail git checkout --track renamer |
e166fe36 | 444 | ' |
bb0ceb62 | 445 | |
eac5a401 | 446 | setup_conflicting_index () { |
8fdcf312 JH |
447 | rm -f .git/index && |
448 | O=$(echo original | git hash-object -w --stdin) && | |
449 | A=$(echo ourside | git hash-object -w --stdin) && | |
450 | B=$(echo theirside | git hash-object -w --stdin) && | |
451 | ( | |
452 | echo "100644 $A 0 fild" && | |
453 | echo "100644 $O 1 file" && | |
454 | echo "100644 $A 2 file" && | |
455 | echo "100644 $B 3 file" && | |
456 | echo "100644 $A 0 filf" | |
eac5a401 JH |
457 | ) | git update-index --index-info |
458 | } | |
459 | ||
460 | test_expect_success 'checkout an unmerged path should fail' ' | |
461 | setup_conflicting_index && | |
8fdcf312 JH |
462 | echo "none of the above" >sample && |
463 | cat sample >fild && | |
464 | cat sample >file && | |
465 | cat sample >filf && | |
466 | test_must_fail git checkout fild file filf && | |
467 | test_cmp sample fild && | |
468 | test_cmp sample filf && | |
469 | test_cmp sample file | |
470 | ' | |
471 | ||
db941099 | 472 | test_expect_success 'checkout with an unmerged path can be ignored' ' |
eac5a401 | 473 | setup_conflicting_index && |
db941099 JH |
474 | echo "none of the above" >sample && |
475 | echo ourside >expect && | |
476 | cat sample >fild && | |
477 | cat sample >file && | |
478 | cat sample >filf && | |
479 | git checkout -f fild file filf && | |
480 | test_cmp expect fild && | |
481 | test_cmp expect filf && | |
482 | test_cmp sample file | |
483 | ' | |
484 | ||
38901a48 | 485 | test_expect_success 'checkout unmerged stage' ' |
eac5a401 | 486 | setup_conflicting_index && |
38901a48 JH |
487 | echo "none of the above" >sample && |
488 | echo ourside >expect && | |
489 | cat sample >fild && | |
490 | cat sample >file && | |
491 | cat sample >filf && | |
492 | git checkout --ours . && | |
493 | test_cmp expect fild && | |
494 | test_cmp expect filf && | |
495 | test_cmp expect file && | |
496 | git checkout --theirs file && | |
497 | test ztheirside = "z$(cat file)" | |
498 | ' | |
499 | ||
54f98fee JH |
500 | test_expect_success 'checkout path with --merge from tree-ish is a no-no' ' |
501 | setup_conflicting_index && | |
502 | test_must_fail git checkout -m HEAD -- file | |
503 | ' | |
504 | ||
0cf8581e | 505 | test_expect_success 'checkout with --merge' ' |
eac5a401 | 506 | setup_conflicting_index && |
0cf8581e JH |
507 | echo "none of the above" >sample && |
508 | echo ourside >expect && | |
509 | cat sample >fild && | |
510 | cat sample >file && | |
511 | cat sample >filf && | |
512 | git checkout -m -- fild file filf && | |
513 | ( | |
e974e06d ES |
514 | echo "<<<<<<< ours" && |
515 | echo ourside && | |
516 | echo "=======" && | |
517 | echo theirside && | |
0cf8581e JH |
518 | echo ">>>>>>> theirs" |
519 | ) >merged && | |
520 | test_cmp expect fild && | |
521 | test_cmp expect filf && | |
522 | test_cmp merged file | |
523 | ' | |
524 | ||
ed3789f2 JH |
525 | test_expect_success 'checkout -m works after (mistaken) resolution' ' |
526 | setup_conflicting_index && | |
527 | echo "none of the above" >sample && | |
528 | cat sample >fild && | |
529 | cat sample >file && | |
530 | cat sample >filf && | |
531 | # resolve to something | |
532 | git add file && | |
533 | git checkout --merge -- fild file filf && | |
534 | { | |
535 | echo "<<<<<<< ours" && | |
536 | echo ourside && | |
537 | echo "=======" && | |
538 | echo theirside && | |
539 | echo ">>>>>>> theirs" | |
540 | } >merged && | |
541 | test_cmp expect fild && | |
542 | test_cmp expect filf && | |
543 | test_cmp merged file | |
544 | ' | |
545 | ||
5bdedac3 | 546 | test_expect_success 'checkout -m works after (mistaken) resolution to remove' ' |
ed3789f2 JH |
547 | setup_conflicting_index && |
548 | echo "none of the above" >sample && | |
549 | cat sample >fild && | |
550 | cat sample >file && | |
551 | cat sample >filf && | |
552 | # resolve to remove | |
553 | git rm file && | |
554 | git checkout --merge -- fild file filf && | |
555 | { | |
556 | echo "<<<<<<< ours" && | |
557 | echo ourside && | |
558 | echo "=======" && | |
559 | echo theirside && | |
560 | echo ">>>>>>> theirs" | |
561 | } >merged && | |
562 | test_cmp expect fild && | |
563 | test_cmp expect filf && | |
564 | test_cmp merged file | |
565 | ' | |
566 | ||
0cf8581e JH |
567 | test_expect_success 'checkout with --merge, in diff3 -m style' ' |
568 | git config merge.conflictstyle diff3 && | |
eac5a401 | 569 | setup_conflicting_index && |
0cf8581e JH |
570 | echo "none of the above" >sample && |
571 | echo ourside >expect && | |
572 | cat sample >fild && | |
573 | cat sample >file && | |
574 | cat sample >filf && | |
575 | git checkout -m -- fild file filf && | |
576 | ( | |
e974e06d ES |
577 | echo "<<<<<<< ours" && |
578 | echo ourside && | |
579 | echo "||||||| base" && | |
580 | echo original && | |
581 | echo "=======" && | |
582 | echo theirside && | |
0cf8581e JH |
583 | echo ">>>>>>> theirs" |
584 | ) >merged && | |
585 | test_cmp expect fild && | |
586 | test_cmp expect filf && | |
587 | test_cmp merged file | |
588 | ' | |
589 | ||
eac5a401 JH |
590 | test_expect_success 'checkout --conflict=merge, overriding config' ' |
591 | git config merge.conflictstyle diff3 && | |
592 | setup_conflicting_index && | |
593 | echo "none of the above" >sample && | |
594 | echo ourside >expect && | |
595 | cat sample >fild && | |
596 | cat sample >file && | |
597 | cat sample >filf && | |
598 | git checkout --conflict=merge -- fild file filf && | |
599 | ( | |
e974e06d ES |
600 | echo "<<<<<<< ours" && |
601 | echo ourside && | |
602 | echo "=======" && | |
603 | echo theirside && | |
eac5a401 JH |
604 | echo ">>>>>>> theirs" |
605 | ) >merged && | |
606 | test_cmp expect fild && | |
607 | test_cmp expect filf && | |
608 | test_cmp merged file | |
609 | ' | |
610 | ||
611 | test_expect_success 'checkout --conflict=diff3' ' | |
a6a4a88a | 612 | test_unconfig merge.conflictstyle && |
eac5a401 JH |
613 | setup_conflicting_index && |
614 | echo "none of the above" >sample && | |
615 | echo ourside >expect && | |
616 | cat sample >fild && | |
617 | cat sample >file && | |
618 | cat sample >filf && | |
619 | git checkout --conflict=diff3 -- fild file filf && | |
620 | ( | |
e974e06d ES |
621 | echo "<<<<<<< ours" && |
622 | echo ourside && | |
623 | echo "||||||| base" && | |
624 | echo original && | |
625 | echo "=======" && | |
626 | echo theirside && | |
eac5a401 JH |
627 | echo ">>>>>>> theirs" |
628 | ) >merged && | |
629 | test_cmp expect fild && | |
630 | test_cmp expect filf && | |
631 | test_cmp merged file | |
632 | ' | |
633 | ||
5a99c1ac PW |
634 | test_expect_success 'checkout --conflict=diff3 --no-conflict does not merge' ' |
635 | setup_conflicting_index && | |
636 | echo "none of the above" >expect && | |
637 | cat expect >fild && | |
638 | cat expect >file && | |
639 | test_must_fail git checkout --conflict=diff3 --no-conflict -- fild file 2>err && | |
640 | test_cmp expect file && | |
641 | test_cmp expect fild && | |
642 | echo "error: path ${SQ}file${SQ} is unmerged" >expect && | |
643 | test_cmp expect err | |
644 | ' | |
645 | ||
646 | test_expect_success 'checkout --conflict=diff3 --no-merge does not merge' ' | |
647 | setup_conflicting_index && | |
648 | echo "none of the above" >expect && | |
649 | cat expect >fild && | |
650 | cat expect >file && | |
651 | test_must_fail git checkout --conflict=diff3 --no-merge -- fild file 2>err && | |
652 | test_cmp expect file && | |
653 | test_cmp expect fild && | |
654 | echo "error: path ${SQ}file${SQ} is unmerged" >expect && | |
655 | test_cmp expect err | |
656 | ' | |
657 | ||
658 | test_expect_success 'checkout --no-merge --conflict=diff3 does merge' ' | |
659 | setup_conflicting_index && | |
660 | echo "none of the above" >fild && | |
661 | echo "none of the above" >file && | |
662 | git checkout --no-merge --conflict=diff3 -- fild file && | |
663 | echo "ourside" >expect && | |
664 | test_cmp expect fild && | |
665 | cat >expect <<-\EOF && | |
666 | <<<<<<< ours | |
667 | ourside | |
668 | ||||||| base | |
669 | original | |
670 | ======= | |
671 | theirside | |
672 | >>>>>>> theirs | |
673 | EOF | |
674 | test_cmp expect file | |
675 | ' | |
676 | ||
677 | test_expect_success 'checkout --merge --conflict=diff3 --no-conflict does merge' ' | |
678 | setup_conflicting_index && | |
679 | echo "none of the above" >fild && | |
680 | echo "none of the above" >file && | |
681 | git checkout --merge --conflict=diff3 --no-conflict -- fild file && | |
682 | echo "ourside" >expect && | |
683 | test_cmp expect fild && | |
684 | cat >expect <<-\EOF && | |
685 | <<<<<<< ours | |
686 | ourside | |
687 | ======= | |
688 | theirside | |
689 | >>>>>>> theirs | |
690 | EOF | |
691 | test_cmp expect file | |
692 | ' | |
693 | ||
dbeaf8e8 PW |
694 | test_expect_success 'checkout with invalid conflict style' ' |
695 | test_must_fail git checkout --conflict=bad 2>actual -- file && | |
696 | echo "error: unknown conflict style ${SQ}bad${SQ}" >expect && | |
697 | test_cmp expect actual | |
698 | ' | |
699 | ||
352eadc4 | 700 | test_expect_success 'failing checkout -b should not break working tree' ' |
f222bd34 | 701 | git clean -fd && # Remove untracked files in the way |
01dc8133 JS |
702 | git reset --hard main && |
703 | git symbolic-ref HEAD refs/heads/main && | |
352eadc4 | 704 | test_must_fail git checkout -b renamer side^ && |
01dc8133 | 705 | test $(git symbolic-ref HEAD) = refs/heads/main && |
352eadc4 DB |
706 | git diff --exit-code && |
707 | git diff --cached --exit-code | |
352eadc4 DB |
708 | ' |
709 | ||
e4b09dad | 710 | test_expect_success 'switch out of non-branch' ' |
01dc8133 JS |
711 | git reset --hard main && |
712 | git checkout main^0 && | |
e4b09dad NS |
713 | echo modified >one && |
714 | test_must_fail git checkout renamer 2>error.log && | |
715 | ! grep "^Previous HEAD" error.log | |
716 | ' | |
717 | ||
6d6f9acc JH |
718 | ( |
719 | echo "#!$SHELL_PATH" | |
720 | cat <<\EOF | |
721 | O=$1 A=$2 B=$3 | |
722 | cat "$A" >.tmp | |
723 | exec >"$A" | |
724 | echo '<<<<<<< filfre-theirs' | |
725 | cat "$B" | |
726 | echo '||||||| filfre-common' | |
727 | cat "$O" | |
728 | echo '=======' | |
729 | cat ".tmp" | |
730 | echo '>>>>>>> filfre-ours' | |
731 | rm -f .tmp | |
732 | exit 1 | |
733 | EOF | |
734 | ) >filfre.sh | |
735 | chmod +x filfre.sh | |
736 | ||
737 | test_expect_success 'custom merge driver with checkout -m' ' | |
738 | git reset --hard && | |
739 | ||
740 | git config merge.filfre.driver "./filfre.sh %O %A %B" && | |
741 | git config merge.filfre.name "Feel-free merge driver" && | |
742 | git config merge.filfre.recursive binary && | |
743 | echo "arm merge=filfre" >.gitattributes && | |
744 | ||
745 | git checkout -b left && | |
746 | echo neutral >arm && | |
747 | git add arm .gitattributes && | |
748 | test_tick && | |
749 | git commit -m neutral && | |
750 | git branch right && | |
751 | ||
752 | echo left >arm && | |
753 | test_tick && | |
754 | git commit -a -m left && | |
755 | git checkout right && | |
756 | ||
757 | echo right >arm && | |
758 | test_tick && | |
759 | git commit -a -m right && | |
760 | ||
761 | test_must_fail git merge left && | |
762 | ( | |
763 | for t in filfre-common left right | |
764 | do | |
765 | grep $t arm || exit 1 | |
766 | done | |
6d6f9acc JH |
767 | ) && |
768 | ||
769 | mv arm expect && | |
770 | git checkout -m arm && | |
771 | test_cmp expect arm | |
772 | ' | |
773 | ||
d3115660 JS |
774 | test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' |
775 | git reset --hard main && | |
776 | # default config does not copy tracking info | |
777 | git checkout -b foo-no-inherit koala/bear && | |
778 | test_cmp_config "" --default "" branch.foo-no-inherit.remote && | |
779 | test_cmp_config "" --default "" branch.foo-no-inherit.merge && | |
780 | # with autoSetupMerge=inherit, we copy tracking info from koala/bear | |
781 | test_config branch.autoSetupMerge inherit && | |
782 | git checkout -b foo koala/bear && | |
783 | test_cmp_config origin branch.foo.remote && | |
784 | test_cmp_config refs/heads/koala/bear branch.foo.merge && | |
785 | # no tracking info to inherit from main | |
786 | git checkout -b main2 main && | |
787 | test_cmp_config "" --default "" branch.main2.remote && | |
788 | test_cmp_config "" --default "" branch.main2.merge | |
789 | ' | |
790 | ||
abc02670 | 791 | test_done |