branch: optionally setup branch.*.merge from upstream local branches
[git] / t / t7201-co.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git-checkout tests.
7
8 Creates master, forks renamer and side branches from it.
9 Test 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
23 test_tick
24
25 fill () {
26         for i
27         do
28                 echo "$i"
29         done
30 }
31
32
33 test_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
58 test_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
67 test_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
80 test_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         diff -u same kept
87         (cat > messages.expect <<EOF
88 M       same
89 EOF
90 ) &&
91         touch messages.expect &&
92         diff -u messages.expect messages
93 '
94
95 test_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
106 M       one
107 EOF
108 ) &&
109         diff -u expect.messages messages &&
110
111         fill "M one" "A three" "D       two" >expect.master &&
112         git diff --name-status master >current.master &&
113         diff -u expect.master current.master &&
114
115         fill "M one" >expect.side &&
116         git diff --name-status side >current.side &&
117         diff -u expect.side current.side &&
118
119         : >expect.index &&
120         git diff --cached >current.index &&
121         diff -u expect.index current.index
122 '
123
124 test_expect_success "checkout -m with dirty tree, renamed" '
125
126         git checkout -f master && git clean -f &&
127
128         fill 1 2 3 4 5 7 8 >one &&
129         if git checkout renamer
130         then
131                 echo Not happy
132                 false
133         else
134                 echo "happy - failed correctly"
135         fi &&
136
137         git checkout -m renamer &&
138         fill 1 3 4 5 7 8 >expect &&
139         diff -u expect uno &&
140         ! test -f one &&
141         git diff --cached >current &&
142         ! test -s current
143
144 '
145
146 test_expect_success 'checkout -m with merge conflict' '
147
148         git checkout -f master && git clean -f &&
149
150         fill 1 T 3 4 5 6 S 8 >one &&
151         if git checkout renamer
152         then
153                 echo Not happy
154                 false
155         else
156                 echo "happy - failed correctly"
157         fi &&
158
159         git checkout -m renamer &&
160
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         diff -u current expect &&
165         git diff --cached two >current &&
166         ! test -s current
167 '
168
169 test_expect_success 'checkout to detach HEAD' '
170
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
179 EOF
180 ) &&
181         diff -u 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
186         then
187                 echo "OOPS, HEAD is still symbolic???"
188                 false
189         else
190                 : happy
191         fi
192 '
193
194 test_expect_success 'checkout to detach HEAD with branchname^' '
195
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
202         then
203                 echo "OOPS, HEAD is still symbolic???"
204                 false
205         else
206                 : happy
207         fi
208 '
209
210 test_expect_success 'checkout to detach HEAD with HEAD^0' '
211
212         git checkout -f master && git clean -f &&
213         git checkout HEAD^0 &&
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
218         then
219                 echo "OOPS, HEAD is still symbolic???"
220                 false
221         else
222                 : happy
223         fi
224 '
225
226 test_expect_success 'checkout with ambiguous tag/branch names' '
227
228         git tag both side &&
229         git branch both master &&
230         git reset --hard &&
231         git checkout master &&
232
233         git checkout both &&
234         H=$(git rev-parse --verify HEAD) &&
235         M=$(git show-ref -s --verify refs/heads/master) &&
236         test "z$H" = "z$M" &&
237         name=$(git symbolic-ref HEAD 2>/dev/null) &&
238         test "z$name" = zrefs/heads/both
239
240 '
241
242 test_expect_success 'checkout with ambiguous tag/branch names' '
243
244         git reset --hard &&
245         git checkout master &&
246
247         git tag frotz side &&
248         git branch frotz master &&
249         git reset --hard &&
250         git checkout master &&
251
252         git checkout tags/frotz &&
253         H=$(git rev-parse --verify HEAD) &&
254         S=$(git show-ref -s --verify refs/heads/side) &&
255         test "z$H" = "z$S" &&
256         if name=$(git symbolic-ref HEAD 2>/dev/null)
257         then
258                 echo "Bad -- should have detached"
259                 false
260         else
261                 : happy
262         fi
263
264 '
265
266 test_expect_success \
267     'checkout w/--track sets up tracking' '
268     git config branch.autosetupmerge false &&
269     git checkout master &&
270     git checkout --track -b track1 &&
271     test "$(git config branch.track1.remote)" &&
272     test "$(git config branch.track1.merge)"'
273
274 test_expect_success \
275     'checkout w/autosetupmerge=always sets up tracking' '
276     git config branch.autosetupmerge always &&
277     git checkout master &&
278     git checkout -b track2 &&
279     test "$(git config branch.track2.remote)" &&
280     test "$(git config branch.track2.merge)"
281     git config branch.autosetupmerge false'
282
283 test_expect_success \
284     'checkout w/--track from non-branch HEAD fails' '
285     git checkout -b delete-me master &&
286     rm .git/refs/heads/delete-me &&
287     test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
288     !(git checkout --track -b track)'
289
290 test_done