checkout -m: recreate merge when checking out of unmerged index
[git/git.git] / t / t7201-co.sh
CommitLineData
abc02670
JH
1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
5
3e0318a3
JH
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'
abc02670
JH
20
21. ./test-lib.sh
22
c07c7bf6
DB
23test_tick
24
abc02670
JH
25fill () {
26 for i
27 do
28 echo "$i"
29 done
30}
31
c1a4278e 32
abc02670
JH
33test_expect_success setup '
34
c07c7bf6 35 fill x y z > same &&
c1a4278e 36 fill 1 2 3 4 5 6 7 8 >one &&
abc02670 37 fill a b c d e >two &&
c07c7bf6 38 git add same one two &&
abc02670
JH
39 git commit -m "Initial A one, A two" &&
40
c1a4278e
JH
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 &&
abc02670
JH
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
5a03e7f2
SP
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
abc02670
JH
67test_expect_success "checkout with dirty tree without -m" '
68
c1a4278e 69 fill 0 1 2 3 4 5 6 7 8 >one &&
abc02670
JH
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
c07c7bf6
DB
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 &&
82ebb0b6 86 test_cmp same kept
c07c7bf6
DB
87 (cat > messages.expect <<EOF
88M same
89EOF
90) &&
91 touch messages.expect &&
82ebb0b6 92 test_cmp messages.expect messages
c07c7bf6
DB
93'
94
abc02670
JH
95test_expect_success "checkout -m with dirty tree" '
96
97 git checkout -f master &&
562ca192 98 git clean -f &&
abc02670 99
c1a4278e 100 fill 0 1 2 3 4 5 6 7 8 >one &&
c07c7bf6 101 git checkout -m side > messages &&
abc02670 102
c1a4278e 103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
abc02670 104
c07c7bf6 105 (cat >expect.messages <<EOF
c07c7bf6
DB
106M one
107EOF
108) &&
82ebb0b6 109 test_cmp expect.messages messages &&
c07c7bf6 110
abc02670
JH
111 fill "M one" "A three" "D two" >expect.master &&
112 git diff --name-status master >current.master &&
82ebb0b6 113 test_cmp expect.master current.master &&
abc02670
JH
114
115 fill "M one" >expect.side &&
116 git diff --name-status side >current.side &&
82ebb0b6 117 test_cmp expect.side current.side &&
abc02670
JH
118
119 : >expect.index &&
120 git diff --cached >current.index &&
82ebb0b6 121 test_cmp expect.index current.index
abc02670
JH
122'
123
c1a4278e
JH
124test_expect_success "checkout -m with dirty tree, renamed" '
125
562ca192 126 git checkout -f master && git clean -f &&
c1a4278e
JH
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 &&
82ebb0b6 139 test_cmp expect uno &&
d7ebd53d
JH
140 ! test -f one &&
141 git diff --cached >current &&
142 ! test -s current
143
144'
145
146test_expect_success 'checkout -m with merge conflict' '
147
562ca192 148 git checkout -f master && git clean -f &&
d7ebd53d
JH
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 &&
82ebb0b6 164 test_cmp current expect &&
d7ebd53d
JH
165 git diff --cached two >current &&
166 ! test -s current
c1a4278e
JH
167'
168
3e0318a3
JH
169test_expect_success 'checkout to detach HEAD' '
170
562ca192 171 git checkout -f renamer && git clean -f &&
c07c7bf6
DB
172 git checkout renamer^ 2>messages &&
173 (cat >messages.expect <<EOF
174Note: moving to "renamer^" which isn'"'"'t a local branch
175If 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>
178HEAD is now at 7329388... Initial A one, A two
179EOF
180) &&
82ebb0b6 181 test_cmp messages.expect messages &&
3e0318a3
JH
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
194test_expect_success 'checkout to detach HEAD with branchname^' '
195
562ca192 196 git checkout -f master && git clean -f &&
3e0318a3
JH
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
b1e9efa7
DB
210test_expect_success 'checkout to detach HEAD with :/message' '
211
212 git checkout -f master && git clean -f &&
213 git checkout ":/Initial" &&
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
3e0318a3
JH
226test_expect_success 'checkout to detach HEAD with HEAD^0' '
227
562ca192 228 git checkout -f master && git clean -f &&
3e0318a3
JH
229 git checkout HEAD^0 &&
230 H=$(git rev-parse --verify HEAD) &&
231 M=$(git show-ref -s --verify refs/heads/master) &&
232 test "z$H" = "z$M" &&
233 if git symbolic-ref HEAD >/dev/null 2>&1
234 then
235 echo "OOPS, HEAD is still symbolic???"
236 false
237 else
238 : happy
239 fi
240'
241
5035242c
JH
242test_expect_success 'checkout with ambiguous tag/branch names' '
243
244 git tag both side &&
245 git branch both master &&
246 git reset --hard &&
247 git checkout master &&
248
249 git checkout both &&
250 H=$(git rev-parse --verify HEAD) &&
251 M=$(git show-ref -s --verify refs/heads/master) &&
252 test "z$H" = "z$M" &&
253 name=$(git symbolic-ref HEAD 2>/dev/null) &&
254 test "z$name" = zrefs/heads/both
255
256'
257
258test_expect_success 'checkout with ambiguous tag/branch names' '
259
260 git reset --hard &&
261 git checkout master &&
262
263 git tag frotz side &&
264 git branch frotz master &&
265 git reset --hard &&
266 git checkout master &&
267
268 git checkout tags/frotz &&
269 H=$(git rev-parse --verify HEAD) &&
270 S=$(git show-ref -s --verify refs/heads/side) &&
271 test "z$H" = "z$S" &&
272 if name=$(git symbolic-ref HEAD 2>/dev/null)
273 then
274 echo "Bad -- should have detached"
275 false
276 else
277 : happy
278 fi
279
280'
281
6010d2d9
JH
282test_expect_success 'switch branches while in subdirectory' '
283
284 git reset --hard &&
285 git checkout master &&
286
287 mkdir subs &&
288 (
289 cd subs &&
290 git checkout side
291 ) &&
292 ! test -f subs/one &&
293 rm -fr subs
294
295'
296
297test_expect_success 'checkout specific path while in subdirectory' '
298
299 git reset --hard &&
300 git checkout side &&
301 mkdir subs &&
302 >subs/bero &&
303 git add subs/bero &&
304 git commit -m "add subs/bero" &&
305
306 git checkout master &&
307 mkdir -p subs &&
308 (
309 cd subs &&
310 git checkout side -- bero
311 ) &&
312 test -f subs/bero
313
314'
315
9ed36cfa
JS
316test_expect_success \
317 'checkout w/--track sets up tracking' '
318 git config branch.autosetupmerge false &&
319 git checkout master &&
320 git checkout --track -b track1 &&
321 test "$(git config branch.track1.remote)" &&
322 test "$(git config branch.track1.merge)"'
323
324test_expect_success \
325 'checkout w/autosetupmerge=always sets up tracking' '
326 git config branch.autosetupmerge always &&
327 git checkout master &&
328 git checkout -b track2 &&
329 test "$(git config branch.track2.remote)" &&
330 test "$(git config branch.track2.merge)"
331 git config branch.autosetupmerge false'
332
333test_expect_success \
334 'checkout w/--track from non-branch HEAD fails' '
335 git checkout -b delete-me master &&
336 rm .git/refs/heads/delete-me &&
337 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
03b9dfb1 338 test_must_fail git checkout --track -b track'
9ed36cfa 339
8fdcf312
JH
340test_expect_success 'checkout an unmerged path should fail' '
341 rm -f .git/index &&
342 O=$(echo original | git hash-object -w --stdin) &&
343 A=$(echo ourside | git hash-object -w --stdin) &&
344 B=$(echo theirside | git hash-object -w --stdin) &&
345 (
346 echo "100644 $A 0 fild" &&
347 echo "100644 $O 1 file" &&
348 echo "100644 $A 2 file" &&
349 echo "100644 $B 3 file" &&
350 echo "100644 $A 0 filf"
351 ) | git update-index --index-info &&
352 echo "none of the above" >sample &&
353 cat sample >fild &&
354 cat sample >file &&
355 cat sample >filf &&
356 test_must_fail git checkout fild file filf &&
357 test_cmp sample fild &&
358 test_cmp sample filf &&
359 test_cmp sample file
360'
361
db941099
JH
362test_expect_success 'checkout with an unmerged path can be ignored' '
363 rm -f .git/index &&
364 O=$(echo original | git hash-object -w --stdin) &&
365 A=$(echo ourside | git hash-object -w --stdin) &&
366 B=$(echo theirside | git hash-object -w --stdin) &&
367 (
368 echo "100644 $A 0 fild" &&
369 echo "100644 $O 1 file" &&
370 echo "100644 $A 2 file" &&
371 echo "100644 $B 3 file" &&
372 echo "100644 $A 0 filf"
373 ) | git update-index --index-info &&
374 echo "none of the above" >sample &&
375 echo ourside >expect &&
376 cat sample >fild &&
377 cat sample >file &&
378 cat sample >filf &&
379 git checkout -f fild file filf &&
380 test_cmp expect fild &&
381 test_cmp expect filf &&
382 test_cmp sample file
383'
384
38901a48
JH
385test_expect_success 'checkout unmerged stage' '
386 rm -f .git/index &&
387 O=$(echo original | git hash-object -w --stdin) &&
388 A=$(echo ourside | git hash-object -w --stdin) &&
389 B=$(echo theirside | git hash-object -w --stdin) &&
390 (
391 echo "100644 $A 0 fild" &&
392 echo "100644 $O 1 file" &&
393 echo "100644 $A 2 file" &&
394 echo "100644 $B 3 file" &&
395 echo "100644 $A 0 filf"
396 ) | git update-index --index-info &&
397 echo "none of the above" >sample &&
398 echo ourside >expect &&
399 cat sample >fild &&
400 cat sample >file &&
401 cat sample >filf &&
402 git checkout --ours . &&
403 test_cmp expect fild &&
404 test_cmp expect filf &&
405 test_cmp expect file &&
406 git checkout --theirs file &&
407 test ztheirside = "z$(cat file)"
408'
409
0cf8581e
JH
410test_expect_success 'checkout with --merge' '
411 rm -f .git/index &&
412 O=$(echo original | git hash-object -w --stdin) &&
413 A=$(echo ourside | git hash-object -w --stdin) &&
414 B=$(echo theirside | git hash-object -w --stdin) &&
415 (
416 echo "100644 $A 0 fild" &&
417 echo "100644 $O 1 file" &&
418 echo "100644 $A 2 file" &&
419 echo "100644 $B 3 file" &&
420 echo "100644 $A 0 filf"
421 ) | git update-index --index-info &&
422 echo "none of the above" >sample &&
423 echo ourside >expect &&
424 cat sample >fild &&
425 cat sample >file &&
426 cat sample >filf &&
427 git checkout -m -- fild file filf &&
428 (
429 echo "<<<<<<< ours"
430 echo ourside
431 echo "======="
432 echo theirside
433 echo ">>>>>>> theirs"
434 ) >merged &&
435 test_cmp expect fild &&
436 test_cmp expect filf &&
437 test_cmp merged file
438'
439
440test_expect_success 'checkout with --merge, in diff3 -m style' '
441 git config merge.conflictstyle diff3 &&
442 rm -f .git/index &&
443 O=$(echo original | git hash-object -w --stdin) &&
444 A=$(echo ourside | git hash-object -w --stdin) &&
445 B=$(echo theirside | git hash-object -w --stdin) &&
446 (
447 echo "100644 $A 0 fild" &&
448 echo "100644 $O 1 file" &&
449 echo "100644 $A 2 file" &&
450 echo "100644 $B 3 file" &&
451 echo "100644 $A 0 filf"
452 ) | git update-index --index-info &&
453 echo "none of the above" >sample &&
454 echo ourside >expect &&
455 cat sample >fild &&
456 cat sample >file &&
457 cat sample >filf &&
458 git checkout -m -- fild file filf &&
459 (
460 echo "<<<<<<< ours"
461 echo ourside
462 echo "|||||||"
463 echo original
464 echo "======="
465 echo theirside
466 echo ">>>>>>> theirs"
467 ) >merged &&
468 test_cmp expect fild &&
469 test_cmp expect filf &&
470 test_cmp merged file
471'
472
abc02670 473test_done