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