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
31 test_expect_success setup '
33 fill 1 2 3 4 5 6 7 8 >one &&
34 fill a b c d e >two &&
36 git commit -m "Initial A one, A two" &&
38 git checkout -b renamer &&
40 fill 1 3 4 5 6 7 8 >uno &&
42 fill a b c d e f >two &&
43 git commit -a -m "Renamer R one->uno, M two" &&
45 git checkout -b side master &&
46 fill 1 2 3 4 5 6 7 >one &&
47 fill A B C D E >three &&
49 git update-index --add --remove one two three &&
50 git commit -m "Side M one, D two, A three" &&
55 test_expect_success "checkout from non-existing branch" '
57 git checkout -b delete-me master &&
58 rm .git/refs/heads/delete-me &&
59 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
60 git checkout master &&
61 test refs/heads/master = "$(git symbolic-ref HEAD)"
64 test_expect_success "checkout with dirty tree without -m" '
66 fill 0 1 2 3 4 5 6 7 8 >one &&
72 echo "happy - failed correctly"
77 test_expect_success "checkout -m with dirty tree" '
79 git checkout -f master &&
82 fill 0 1 2 3 4 5 6 7 8 >one &&
83 git checkout -m side &&
85 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
87 fill "M one" "A three" "D two" >expect.master &&
88 git diff --name-status master >current.master &&
89 diff expect.master current.master &&
91 fill "M one" >expect.side &&
92 git diff --name-status side >current.side &&
93 diff expect.side current.side &&
96 git diff --cached >current.index &&
97 diff expect.index current.index
100 test_expect_success "checkout -m with dirty tree, renamed" '
102 git checkout -f master && git clean &&
104 fill 1 2 3 4 5 7 8 >one &&
105 if git checkout renamer
110 echo "happy - failed correctly"
113 git checkout -m renamer &&
114 fill 1 3 4 5 7 8 >expect &&
117 git diff --cached >current &&
122 test_expect_success 'checkout -m with merge conflict' '
124 git checkout -f master && git clean &&
126 fill 1 T 3 4 5 6 S 8 >one &&
127 if git checkout renamer
132 echo "happy - failed correctly"
135 git checkout -m renamer &&
137 git diff master:one :3:uno |
138 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
139 fill d2 aT d7 aS >expect &&
140 diff current expect &&
141 git diff --cached two >current &&
145 test_expect_success 'checkout to detach HEAD' '
147 git checkout -f renamer && git clean &&
148 git checkout renamer^ &&
149 H=$(git rev-parse --verify HEAD) &&
150 M=$(git show-ref -s --verify refs/heads/master) &&
151 test "z$H" = "z$M" &&
152 if git symbolic-ref HEAD >/dev/null 2>&1
154 echo "OOPS, HEAD is still symbolic???"
161 test_expect_success 'checkout to detach HEAD with branchname^' '
163 git checkout -f master && git clean &&
164 git checkout renamer^ &&
165 H=$(git rev-parse --verify HEAD) &&
166 M=$(git show-ref -s --verify refs/heads/master) &&
167 test "z$H" = "z$M" &&
168 if git symbolic-ref HEAD >/dev/null 2>&1
170 echo "OOPS, HEAD is still symbolic???"
177 test_expect_success 'checkout to detach HEAD with HEAD^0' '
179 git checkout -f master && git clean &&
180 git checkout HEAD^0 &&
181 H=$(git rev-parse --verify HEAD) &&
182 M=$(git show-ref -s --verify refs/heads/master) &&
183 test "z$H" = "z$M" &&
184 if git symbolic-ref HEAD >/dev/null 2>&1
186 echo "OOPS, HEAD is still symbolic???"
193 test_expect_success 'checkout with ambiguous tag/branch names' '
196 git branch both master &&
198 git checkout master &&
201 H=$(git rev-parse --verify HEAD) &&
202 M=$(git show-ref -s --verify refs/heads/master) &&
203 test "z$H" = "z$M" &&
204 name=$(git symbolic-ref HEAD 2>/dev/null) &&
205 test "z$name" = zrefs/heads/both
209 test_expect_success 'checkout with ambiguous tag/branch names' '
212 git checkout master &&
214 git tag frotz side &&
215 git branch frotz master &&
217 git checkout master &&
219 git checkout tags/frotz &&
220 H=$(git rev-parse --verify HEAD) &&
221 S=$(git show-ref -s --verify refs/heads/side) &&
222 test "z$H" = "z$S" &&
223 if name=$(git symbolic-ref HEAD 2>/dev/null)
225 echo "Bad -- should have detached"