3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git-checkout tests.
8 Creates master, forks renamer and side branches from it.
9 Test switching across them.
11 ! [master] Initial A one, A two
12 * [renamer] Renamer R one->uno, M two
13 ! [side] Side M one, D two, A three
15 + [side] Side M one, D two, A three
16 * [renamer] Renamer R one->uno, M two
17 +*+ [master] Initial A one, A two
33 test_expect_success setup
'
36 fill 1 2 3 4 5 6 7 8 >one &&
37 fill a b c d e >two &&
38 git add same one two &&
39 git commit -m "Initial A one, A two" &&
41 git checkout -b renamer &&
43 fill 1 3 4 5 6 7 8 >uno &&
45 fill a b c d e f >two &&
46 git commit -a -m "Renamer R one->uno, M two" &&
48 git checkout -b side master &&
49 fill 1 2 3 4 5 6 7 >one &&
50 fill A B C D E >three &&
52 git update-index --add --remove one two three &&
53 git commit -m "Side M one, D two, A three" &&
58 test_expect_success
"checkout from non-existing branch" '
60 git checkout -b delete-me master &&
61 rm .git/refs/heads/delete-me &&
62 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
63 git checkout master &&
64 test refs/heads/master = "$(git symbolic-ref HEAD)"
67 test_expect_success
"checkout with dirty tree without -m" '
69 fill 0 1 2 3 4 5 6 7 8 >one &&
75 echo "happy - failed correctly"
80 test_expect_success
"checkout with unrelated dirty tree without -m" '
82 git checkout -f master &&
83 fill 0 1 2 3 4 5 6 7 8 >same &&
85 git checkout side >messages &&
87 (cat > messages.expect <<EOF
91 touch messages.expect &&
92 test_cmp messages.expect messages
95 test_expect_success
"checkout -m with dirty tree" '
97 git checkout -f master &&
100 fill 0 1 2 3 4 5 6 7 8 >one &&
101 git checkout -m side > messages &&
103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
105 (cat >expect.messages <<EOF
109 test_cmp expect.messages messages &&
111 fill "M one" "A three" "D two" >expect.master &&
112 git diff --name-status master >current.master &&
113 test_cmp expect.master current.master &&
115 fill "M one" >expect.side &&
116 git diff --name-status side >current.side &&
117 test_cmp expect.side current.side &&
120 git diff --cached >current.index &&
121 test_cmp expect.index current.index
124 test_expect_success
"checkout -m with dirty tree, renamed" '
126 git checkout -f master && git clean -f &&
128 fill 1 2 3 4 5 7 8 >one &&
129 if git checkout renamer
134 echo "happy - failed correctly"
137 git checkout -m renamer &&
138 fill 1 3 4 5 7 8 >expect &&
139 test_cmp expect uno &&
141 git diff --cached >current &&
146 test_expect_success
'checkout -m with merge conflict' '
148 git checkout -f master && git clean -f &&
150 fill 1 T 3 4 5 6 S 8 >one &&
151 if git checkout renamer
156 echo "happy - failed correctly"
159 git checkout -m renamer &&
161 git diff master:one :3:uno |
162 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
163 fill d2 aT d7 aS >expect &&
164 test_cmp current expect &&
165 git diff --cached two >current &&
169 test_expect_success
'checkout to detach HEAD' '
171 git checkout -f renamer && git clean -f &&
172 git checkout renamer^ 2>messages &&
173 (cat >messages.expect <<EOF
174 Note: moving to "renamer^" which isn'"'"'t a local branch
175 If you want to create a new branch from this checkout, you may do so
176 (now or later) by using -b with the checkout command again. Example:
177 git checkout -b <new_branch_name>
178 HEAD is now at 7329388... Initial A one, A two
181 test_cmp messages.expect messages &&
182 H=$(git rev-parse --verify HEAD) &&
183 M=$(git show-ref -s --verify refs/heads/master) &&
184 test "z$H" = "z$M" &&
185 if git symbolic-ref HEAD >/dev/null 2>&1
187 echo "OOPS, HEAD is still symbolic???"
194 test_expect_success
'checkout to detach HEAD with branchname^' '
196 git checkout -f master && git clean -f &&
197 git checkout renamer^ &&
198 H=$(git rev-parse --verify HEAD) &&
199 M=$(git show-ref -s --verify refs/heads/master) &&
200 test "z$H" = "z$M" &&
201 if git symbolic-ref HEAD >/dev/null 2>&1
203 echo "OOPS, HEAD is still symbolic???"
210 test_expect_success
'checkout to detach HEAD with :/message' '
212 git checkout -f master && git clean -f &&
213 git checkout ":/Initial" &&
214 H=$(git rev-parse --verify HEAD) &&
215 M=$(git show-ref -s --verify refs/heads/master) &&
216 test "z$H" = "z$M" &&
217 if git symbolic-ref HEAD >/dev/null 2>&1
219 echo "OOPS, HEAD is still symbolic???"
226 test_expect_success
'checkout to detach HEAD with HEAD^0' '
228 git checkout -f master && git clean -f &&
229 git checkout HEAD^0 &&
230 H=$(git rev-parse --verify HEAD) &&
231 M=$(git show-ref -s --verify refs/heads/master) &&
232 test "z$H" = "z$M" &&
233 if git symbolic-ref HEAD >/dev/null 2>&1
235 echo "OOPS, HEAD is still symbolic???"
242 test_expect_success
'checkout with ambiguous tag/branch names' '
245 git branch both master &&
247 git checkout master &&
250 H=$(git rev-parse --verify HEAD) &&
251 M=$(git show-ref -s --verify refs/heads/master) &&
252 test "z$H" = "z$M" &&
253 name=$(git symbolic-ref HEAD 2>/dev/null) &&
254 test "z$name" = zrefs/heads/both
258 test_expect_success
'checkout with ambiguous tag/branch names' '
261 git checkout master &&
263 git tag frotz side &&
264 git branch frotz master &&
266 git checkout master &&
268 git checkout tags/frotz &&
269 H=$(git rev-parse --verify HEAD) &&
270 S=$(git show-ref -s --verify refs/heads/side) &&
271 test "z$H" = "z$S" &&
272 if name=$(git symbolic-ref HEAD 2>/dev/null)
274 echo "Bad -- should have detached"
282 test_expect_success
'switch branches while in subdirectory' '
285 git checkout master &&
292 ! test -f subs/one &&
297 test_expect_success
'checkout specific path while in subdirectory' '
304 git commit -m "add subs/bero" &&
306 git checkout master &&
310 git checkout side -- bero
316 test_expect_success \
317 'checkout w/--track sets up tracking' '
318 git config branch.autosetupmerge false &&
319 git checkout master &&
320 git checkout --track -b track1 &&
321 test "$(git config branch.track1.remote)" &&
322 test "$(git config branch.track1.merge)"'
324 test_expect_success \
325 'checkout w/autosetupmerge=always sets up tracking' '
326 git config branch.autosetupmerge always &&
327 git checkout master &&
328 git checkout -b track2 &&
329 test "$(git config branch.track2.remote)" &&
330 test "$(git config branch.track2.merge)"
331 git config branch.autosetupmerge false'
333 test_expect_success \
334 'checkout w/--track from non-branch HEAD fails' '
335 git checkout -b delete-me master &&
336 rm .git/refs/heads/delete-me &&
337 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
338 test_must_fail git checkout --track -b track'
340 setup_conflicting_index
() {
342 O
=$
(echo original | git hash-object
-w --stdin) &&
343 A
=$
(echo ourside | git hash-object
-w --stdin) &&
344 B
=$
(echo theirside | git hash-object
-w --stdin) &&
346 echo "100644 $A 0 fild" &&
347 echo "100644 $O 1 file" &&
348 echo "100644 $A 2 file" &&
349 echo "100644 $B 3 file" &&
350 echo "100644 $A 0 filf"
351 ) | git update-index
--index-info
354 test_expect_success
'checkout an unmerged path should fail' '
355 setup_conflicting_index &&
356 echo "none of the above" >sample &&
360 test_must_fail git checkout fild file filf &&
361 test_cmp sample fild &&
362 test_cmp sample filf &&
366 test_expect_success
'checkout with an unmerged path can be ignored' '
367 setup_conflicting_index &&
368 echo "none of the above" >sample &&
369 echo ourside >expect &&
373 git checkout -f fild file filf &&
374 test_cmp expect fild &&
375 test_cmp expect filf &&
379 test_expect_success
'checkout unmerged stage' '
380 setup_conflicting_index &&
381 echo "none of the above" >sample &&
382 echo ourside >expect &&
386 git checkout --ours . &&
387 test_cmp expect fild &&
388 test_cmp expect filf &&
389 test_cmp expect file &&
390 git checkout --theirs file &&
391 test ztheirside = "z$(cat file)"
394 test_expect_success
'checkout with --merge' '
395 setup_conflicting_index &&
396 echo "none of the above" >sample &&
397 echo ourside >expect &&
401 git checkout -m -- fild file filf &&
407 echo ">>>>>>> theirs"
409 test_cmp expect fild &&
410 test_cmp expect filf &&
414 test_expect_success
'checkout with --merge, in diff3 -m style' '
415 git config merge.conflictstyle diff3 &&
416 setup_conflicting_index &&
417 echo "none of the above" >sample &&
418 echo ourside >expect &&
422 git checkout -m -- fild file filf &&
430 echo ">>>>>>> theirs"
432 test_cmp expect fild &&
433 test_cmp expect filf &&
437 test_expect_success
'checkout --conflict=merge, overriding config' '
438 git config merge.conflictstyle diff3 &&
439 setup_conflicting_index &&
440 echo "none of the above" >sample &&
441 echo ourside >expect &&
445 git checkout --conflict=merge -- fild file filf &&
451 echo ">>>>>>> theirs"
453 test_cmp expect fild &&
454 test_cmp expect filf &&
458 test_expect_success
'checkout --conflict=diff3' '
459 git config --unset merge.conflictstyle
460 setup_conflicting_index &&
461 echo "none of the above" >sample &&
462 echo ourside >expect &&
466 git checkout --conflict=diff3 -- fild file filf &&
474 echo ">>>>>>> theirs"
476 test_cmp expect fild &&
477 test_cmp expect filf &&