1#!/bin/sh
2
3test_description='test git checkout --to'
4
5. ./test-lib.sh
6
7test_expect_success 'setup' '
8 test_commit init
9'
10
11test_expect_success 'checkout --to not updating paths' '
12 test_must_fail git checkout --to -- init.t
13'
14
15test_expect_success 'checkout --to an existing worktree' '
16 mkdir -p existing/subtree &&
17 test_must_fail git checkout --detach --to existing master
18'
19
20test_expect_success 'checkout --to an existing empty worktree' '
21 mkdir existing_empty &&
22 git checkout --detach --to existing_empty master
23'
24
25test_expect_success 'checkout --to refuses to checkout locked branch' '
26 test_must_fail git checkout --to zere master &&
27 ! test -d zere &&
28 ! test -d .git/worktrees/zere
29'
30
31test_expect_success 'checking out paths not complaining about linked checkouts' '
32 (
33 cd existing_empty &&
34 echo dirty >>init.t &&
35 git checkout master -- init.t
36 )
37'
38
39test_expect_success 'checkout --to a new worktree' '
40 git rev-parse HEAD >expect &&
41 git checkout --detach --to here master &&
42 (
43 cd here &&
44 test_cmp ../init.t init.t &&
45 test_must_fail git symbolic-ref HEAD &&
46 git rev-parse HEAD >actual &&
47 test_cmp ../expect actual &&
48 git fsck
49 )
50'
51
52test_expect_success 'checkout --to a new worktree from a subdir' '
53 (
54 mkdir sub &&
55 cd sub &&
56 git checkout --detach --to here master &&
57 cd here &&
58 test_cmp ../../init.t init.t
59 )
60'
61
62test_expect_success 'checkout --to from a linked checkout' '
63 (
64 cd here &&
65 git checkout --detach --to nested-here master &&
66 cd nested-here &&
67 git fsck
68 )
69'
70
71test_expect_success 'checkout --to a new worktree creating new branch' '
72 git checkout --to there -b newmaster master &&
73 (
74 cd there &&
75 test_cmp ../init.t init.t &&
76 git symbolic-ref HEAD >actual &&
77 echo refs/heads/newmaster >expect &&
78 test_cmp expect actual &&
79 git fsck
80 )
81'
82
83test_expect_success 'die the same branch is already checked out' '
84 (
85 cd here &&
86 test_must_fail git checkout newmaster
87 )
88'
89
90test_expect_success 'not die the same branch is already checked out' '
91 (
92 cd here &&
93 git checkout --ignore-other-worktrees --to anothernewmaster newmaster
94 )
95'
96
97test_expect_success 'not die on re-checking out current branch' '
98 (
99 cd there &&
100 git checkout newmaster
101 )
102'
103
104test_expect_success 'checkout --to from a bare repo' '
105 (
106 git clone --bare . bare &&
107 cd bare &&
108 git checkout --to ../there2 -b bare-master master
109 )
110'
111
112test_expect_success 'checkout from a bare repo without --to' '
113 (
114 cd bare &&
115 test_must_fail git checkout master
116 )
117'
118
119test_expect_success 'checkout with grafts' '
120 test_when_finished rm .git/info/grafts &&
121 test_commit abc &&
122 SHA1=`git rev-parse HEAD` &&
123 test_commit def &&
124 test_commit xyz &&
125 echo "`git rev-parse HEAD` $SHA1" >.git/info/grafts &&
126 cat >expected <<-\EOF &&
127 xyz
128 abc
129 EOF
130 git log --format=%s -2 >actual &&
131 test_cmp expected actual &&
132 git checkout --detach --to grafted master &&
133 git --git-dir=grafted/.git log --format=%s -2 >actual &&
134 test_cmp expected actual
135'
136
137test_expect_success 'checkout --to from relative HEAD' '
138 test_commit a &&
139 test_commit b &&
140 test_commit c &&
141 git rev-parse HEAD~1 >expected &&
142 git checkout --to relhead HEAD~1 &&
143 git -C relhead rev-parse HEAD >actual &&
144 test_cmp expected actual
145'
146
147test_done