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
23fill () {
24 for i
25 do
26 echo "$i"
27 done
28}
29
30
31test_expect_success setup '
32
33 fill 1 2 3 4 5 6 7 8 >one &&
34 fill a b c d e >two &&
35 git add one two &&
36 git commit -m "Initial A one, A two" &&
37
38 git checkout -b renamer &&
39 rm -f one &&
40 fill 1 3 4 5 6 7 8 >uno &&
41 git add uno &&
42 fill a b c d e f >two &&
43 git commit -a -m "Renamer R one->uno, M two" &&
44
45 git checkout -b side master &&
46 fill 1 2 3 4 5 6 7 >one &&
47 fill A B C D E >three &&
48 rm -f two &&
49 git update-index --add --remove one two three &&
50 git commit -m "Side M one, D two, A three" &&
51
52 git checkout master
53'
54
55test_expect_success "checkout from non-existing branch" '
56
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)"
62'
63
64test_expect_success "checkout with dirty tree without -m" '
65
66 fill 0 1 2 3 4 5 6 7 8 >one &&
67 if git checkout side
68 then
69 echo Not happy
70 false
71 else
72 echo "happy - failed correctly"
73 fi
74
75'
76
77test_expect_success "checkout -m with dirty tree" '
78
79 git checkout -f master &&
80 git clean &&
81
82 fill 0 1 2 3 4 5 6 7 8 >one &&
83 git checkout -m side &&
84
85 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
86
87 fill "M one" "A three" "D two" >expect.master &&
88 git diff --name-status master >current.master &&
89 diff expect.master current.master &&
90
91 fill "M one" >expect.side &&
92 git diff --name-status side >current.side &&
93 diff expect.side current.side &&
94
95 : >expect.index &&
96 git diff --cached >current.index &&
97 diff expect.index current.index
98'
99
100test_expect_success "checkout -m with dirty tree, renamed" '
101
102 git checkout -f master && git clean &&
103
104 fill 1 2 3 4 5 7 8 >one &&
105 if git checkout renamer
106 then
107 echo Not happy
108 false
109 else
110 echo "happy - failed correctly"
111 fi &&
112
113 git checkout -m renamer &&
114 fill 1 3 4 5 7 8 >expect &&
115 diff expect uno &&
116 ! test -f one &&
117 git diff --cached >current &&
118 ! test -s current
119
120'
121
122test_expect_success 'checkout -m with merge conflict' '
123
124 git checkout -f master && git clean &&
125
126 fill 1 T 3 4 5 6 S 8 >one &&
127 if git checkout renamer
128 then
129 echo Not happy
130 false
131 else
132 echo "happy - failed correctly"
133 fi &&
134
135 git checkout -m renamer &&
136
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 &&
142 ! test -s current
143'
144
145test_expect_success 'checkout to detach HEAD' '
146
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
153 then
154 echo "OOPS, HEAD is still symbolic???"
155 false
156 else
157 : happy
158 fi
159'
160
161test_expect_success 'checkout to detach HEAD with branchname^' '
162
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
169 then
170 echo "OOPS, HEAD is still symbolic???"
171 false
172 else
173 : happy
174 fi
175'
176
177test_expect_success 'checkout to detach HEAD with HEAD^0' '
178
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
185 then
186 echo "OOPS, HEAD is still symbolic???"
187 false
188 else
189 : happy
190 fi
191'
192
193test_done