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 git diff 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
106 Merging side with local
108 ab76817 Side M one, D two, A three
110 found 1 common ancestor(s):
111 7329388 Initial A one, A two
116 git diff expect.messages messages &&
118 fill "M one" "A three" "D two" >expect.master &&
119 git diff --name-status master >current.master &&
120 diff expect.master current.master &&
122 fill "M one" >expect.side &&
123 git diff --name-status side >current.side &&
124 diff expect.side current.side &&
127 git diff --cached >current.index &&
128 diff expect.index current.index
131 test_expect_success
"checkout -m with dirty tree, renamed" '
133 git checkout -f master && git clean -f &&
135 fill 1 2 3 4 5 7 8 >one &&
136 if git checkout renamer
141 echo "happy - failed correctly"
144 git checkout -m renamer &&
145 fill 1 3 4 5 7 8 >expect &&
148 git diff --cached >current &&
153 test_expect_success
'checkout -m with merge conflict' '
155 git checkout -f master && git clean -f &&
157 fill 1 T 3 4 5 6 S 8 >one &&
158 if git checkout renamer
163 echo "happy - failed correctly"
166 git checkout -m renamer &&
168 git diff master:one :3:uno |
169 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
170 fill d2 aT d7 aS >expect &&
171 diff current expect &&
172 git diff --cached two >current &&
176 test_expect_success
'checkout to detach HEAD' '
178 git checkout -f renamer && git clean -f &&
179 git checkout renamer^ 2>messages &&
180 (cat >messages.expect <<EOF
181 Note: moving to "renamer^" which isn'"'"'t a local branch
182 If you want to create a new branch from this checkout, you may do so
183 (now or later) by using -b with the checkout command again. Example:
184 git checkout -b <new_branch_name>
185 HEAD is now at 7329388... Initial A one, A two
188 git diff messages.expect messages &&
189 H=$(git rev-parse --verify HEAD) &&
190 M=$(git show-ref -s --verify refs/heads/master) &&
191 test "z$H" = "z$M" &&
192 if git symbolic-ref HEAD >/dev/null 2>&1
194 echo "OOPS, HEAD is still symbolic???"
201 test_expect_success
'checkout to detach HEAD with branchname^' '
203 git checkout -f master && git clean -f &&
204 git checkout renamer^ &&
205 H=$(git rev-parse --verify HEAD) &&
206 M=$(git show-ref -s --verify refs/heads/master) &&
207 test "z$H" = "z$M" &&
208 if git symbolic-ref HEAD >/dev/null 2>&1
210 echo "OOPS, HEAD is still symbolic???"
217 test_expect_success
'checkout to detach HEAD with HEAD^0' '
219 git checkout -f master && git clean -f &&
220 git checkout HEAD^0 &&
221 H=$(git rev-parse --verify HEAD) &&
222 M=$(git show-ref -s --verify refs/heads/master) &&
223 test "z$H" = "z$M" &&
224 if git symbolic-ref HEAD >/dev/null 2>&1
226 echo "OOPS, HEAD is still symbolic???"
233 test_expect_success
'checkout with ambiguous tag/branch names' '
236 git branch both master &&
238 git checkout master &&
241 H=$(git rev-parse --verify HEAD) &&
242 M=$(git show-ref -s --verify refs/heads/master) &&
243 test "z$H" = "z$M" &&
244 name=$(git symbolic-ref HEAD 2>/dev/null) &&
245 test "z$name" = zrefs/heads/both
249 test_expect_success
'checkout with ambiguous tag/branch names' '
252 git checkout master &&
254 git tag frotz side &&
255 git branch frotz master &&
257 git checkout master &&
259 git checkout tags/frotz &&
260 H=$(git rev-parse --verify HEAD) &&
261 S=$(git show-ref -s --verify refs/heads/side) &&
262 test "z$H" = "z$S" &&
263 if name=$(git symbolic-ref HEAD 2>/dev/null)
265 echo "Bad -- should have detached"