1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
5
6test_description='git-checkout tests.
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
14 ---
15 + [side] Side M one, D two, A three
16 * [renamer] Renamer R one->uno, M two
17 +*+ [master] Initial A one, A two
18
19'
20
21. ./test-lib.sh
22
23test_tick
24
25fill () {
26 for i
27 do
28 echo "$i"
29 done
30}
31
32
33test_expect_success setup '
34
35 fill x y z > same &&
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" &&
40
41 git checkout -b renamer &&
42 rm -f one &&
43 fill 1 3 4 5 6 7 8 >uno &&
44 git add uno &&
45 fill a b c d e f >two &&
46 git commit -a -m "Renamer R one->uno, M two" &&
47
48 git checkout -b side master &&
49 fill 1 2 3 4 5 6 7 >one &&
50 fill A B C D E >three &&
51 rm -f two &&
52 git update-index --add --remove one two three &&
53 git commit -m "Side M one, D two, A three" &&
54
55 git checkout master
56'
57
58test_expect_success "checkout from non-existing branch" '
59
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)"
65'
66
67test_expect_success "checkout with dirty tree without -m" '
68
69 fill 0 1 2 3 4 5 6 7 8 >one &&
70 if git checkout side
71 then
72 echo Not happy
73 false
74 else
75 echo "happy - failed correctly"
76 fi
77
78'
79
80test_expect_success "checkout with unrelated dirty tree without -m" '
81
82 git checkout -f master &&
83 fill 0 1 2 3 4 5 6 7 8 >same &&
84 cp same kept
85 git checkout side >messages &&
86 git diff same kept
87 (cat > messages.expect <<EOF
88M same
89EOF
90) &&
91 touch messages.expect &&
92 git diff messages.expect messages
93'
94
95test_expect_success "checkout -m with dirty tree" '
96
97 git checkout -f master &&
98 git clean -f &&
99
100 fill 0 1 2 3 4 5 6 7 8 >one &&
101 git checkout -m side > messages &&
102
103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
104
105 (cat >expect.messages <<EOF
106Merging side with local
107Merging:
108ab76817 Side M one, D two, A three
109virtual local
110found 1 common ancestor(s):
1117329388 Initial A one, A two
112Auto-merged one
113M one
114EOF
115) &&
116 git diff expect.messages messages &&
117
118 fill "M one" "A three" "D two" >expect.master &&
119 git diff --name-status master >current.master &&
120 diff expect.master current.master &&
121
122 fill "M one" >expect.side &&
123 git diff --name-status side >current.side &&
124 diff expect.side current.side &&
125
126 : >expect.index &&
127 git diff --cached >current.index &&
128 diff expect.index current.index
129'
130
131test_expect_success "checkout -m with dirty tree, renamed" '
132
133 git checkout -f master && git clean -f &&
134
135 fill 1 2 3 4 5 7 8 >one &&
136 if git checkout renamer
137 then
138 echo Not happy
139 false
140 else
141 echo "happy - failed correctly"
142 fi &&
143
144 git checkout -m renamer &&
145 fill 1 3 4 5 7 8 >expect &&
146 diff expect uno &&
147 ! test -f one &&
148 git diff --cached >current &&
149 ! test -s current
150
151'
152
153test_expect_success 'checkout -m with merge conflict' '
154
155 git checkout -f master && git clean -f &&
156
157 fill 1 T 3 4 5 6 S 8 >one &&
158 if git checkout renamer
159 then
160 echo Not happy
161 false
162 else
163 echo "happy - failed correctly"
164 fi &&
165
166 git checkout -m renamer &&
167
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 &&
173 ! test -s current
174'
175
176test_expect_success 'checkout to detach HEAD' '
177
178 git checkout -f renamer && git clean -f &&
179 git checkout renamer^ 2>messages &&
180 (cat >messages.expect <<EOF
181Note: moving to "renamer^" which isn'"'"'t a local branch
182If 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>
185HEAD is now at 7329388... Initial A one, A two
186EOF
187) &&
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
193 then
194 echo "OOPS, HEAD is still symbolic???"
195 false
196 else
197 : happy
198 fi
199'
200
201test_expect_success 'checkout to detach HEAD with branchname^' '
202
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
209 then
210 echo "OOPS, HEAD is still symbolic???"
211 false
212 else
213 : happy
214 fi
215'
216
217test_expect_success 'checkout to detach HEAD with :/message' '
218
219 git checkout -f master && git clean -f &&
220 git checkout ":/Initial" &&
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
225 then
226 echo "OOPS, HEAD is still symbolic???"
227 false
228 else
229 : happy
230 fi
231'
232
233test_expect_success 'checkout to detach HEAD with HEAD^0' '
234
235 git checkout -f master && git clean -f &&
236 git checkout HEAD^0 &&
237 H=$(git rev-parse --verify HEAD) &&
238 M=$(git show-ref -s --verify refs/heads/master) &&
239 test "z$H" = "z$M" &&
240 if git symbolic-ref HEAD >/dev/null 2>&1
241 then
242 echo "OOPS, HEAD is still symbolic???"
243 false
244 else
245 : happy
246 fi
247'
248
249test_expect_success 'checkout with ambiguous tag/branch names' '
250
251 git tag both side &&
252 git branch both master &&
253 git reset --hard &&
254 git checkout master &&
255
256 git checkout both &&
257 H=$(git rev-parse --verify HEAD) &&
258 M=$(git show-ref -s --verify refs/heads/master) &&
259 test "z$H" = "z$M" &&
260 name=$(git symbolic-ref HEAD 2>/dev/null) &&
261 test "z$name" = zrefs/heads/both
262
263'
264
265test_expect_success 'checkout with ambiguous tag/branch names' '
266
267 git reset --hard &&
268 git checkout master &&
269
270 git tag frotz side &&
271 git branch frotz master &&
272 git reset --hard &&
273 git checkout master &&
274
275 git checkout tags/frotz &&
276 H=$(git rev-parse --verify HEAD) &&
277 S=$(git show-ref -s --verify refs/heads/side) &&
278 test "z$H" = "z$S" &&
279 if name=$(git symbolic-ref HEAD 2>/dev/null)
280 then
281 echo "Bad -- should have detached"
282 false
283 else
284 : happy
285 fi
286
287'
288
289test_done