1#!/bin/sh
2
3test_description='test git worktree add'
4
5. ./test-lib.sh
6
7test_expect_success 'setup' '
8 test_commit init
9'
10
11test_expect_success '"add" an existing worktree' '
12 mkdir -p existing/subtree &&
13 test_must_fail git worktree add --detach existing master
14'
15
16test_expect_success '"add" an existing empty worktree' '
17 mkdir existing_empty &&
18 git worktree add --detach existing_empty master
19'
20
21test_expect_success '"add" refuses to checkout locked branch' '
22 test_must_fail git worktree add zere master &&
23 ! test -d zere &&
24 ! test -d .git/worktrees/zere
25'
26
27test_expect_success 'checking out paths not complaining about linked checkouts' '
28 (
29 cd existing_empty &&
30 echo dirty >>init.t &&
31 git checkout master -- init.t
32 )
33'
34
35test_expect_success '"add" worktree' '
36 git rev-parse HEAD >expect &&
37 git worktree add --detach here master &&
38 (
39 cd here &&
40 test_cmp ../init.t init.t &&
41 test_must_fail git symbolic-ref HEAD &&
42 git rev-parse HEAD >actual &&
43 test_cmp ../expect actual &&
44 git fsck
45 )
46'
47
48test_expect_success '"add" worktree from a subdir' '
49 (
50 mkdir sub &&
51 cd sub &&
52 git worktree add --detach here master &&
53 cd here &&
54 test_cmp ../../init.t init.t
55 )
56'
57
58test_expect_success '"add" from a linked checkout' '
59 (
60 cd here &&
61 git worktree add --detach nested-here master &&
62 cd nested-here &&
63 git fsck
64 )
65'
66
67test_expect_success '"add" worktree creating new branch' '
68 git worktree add -b newmaster there master &&
69 (
70 cd there &&
71 test_cmp ../init.t init.t &&
72 git symbolic-ref HEAD >actual &&
73 echo refs/heads/newmaster >expect &&
74 test_cmp expect actual &&
75 git fsck
76 )
77'
78
79test_expect_success 'die the same branch is already checked out' '
80 (
81 cd here &&
82 test_must_fail git checkout newmaster
83 )
84'
85
86test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
87 head=$(git -C there rev-parse --git-path HEAD) &&
88 ref=$(git -C there symbolic-ref HEAD) &&
89 rm "$head" &&
90 ln -s "$ref" "$head" &&
91 test_must_fail git -C here checkout newmaster
92'
93
94test_expect_success 'not die the same branch is already checked out' '
95 (
96 cd here &&
97 git worktree add --force anothernewmaster newmaster
98 )
99'
100
101test_expect_success 'not die on re-checking out current branch' '
102 (
103 cd there &&
104 git checkout newmaster
105 )
106'
107
108test_expect_success '"add" from a bare repo' '
109 (
110 git clone --bare . bare &&
111 cd bare &&
112 git worktree add -b bare-master ../there2 master
113 )
114'
115
116test_expect_success 'checkout from a bare repo without "add"' '
117 (
118 cd bare &&
119 test_must_fail git checkout master
120 )
121'
122
123test_expect_success 'checkout with grafts' '
124 test_when_finished rm .git/info/grafts &&
125 test_commit abc &&
126 SHA1=$(git rev-parse HEAD) &&
127 test_commit def &&
128 test_commit xyz &&
129 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
130 cat >expected <<-\EOF &&
131 xyz
132 abc
133 EOF
134 git log --format=%s -2 >actual &&
135 test_cmp expected actual &&
136 git worktree add --detach grafted master &&
137 git --git-dir=grafted/.git log --format=%s -2 >actual &&
138 test_cmp expected actual
139'
140
141test_expect_success '"add" from relative HEAD' '
142 test_commit a &&
143 test_commit b &&
144 test_commit c &&
145 git rev-parse HEAD~1 >expected &&
146 git worktree add relhead HEAD~1 &&
147 git -C relhead rev-parse HEAD >actual &&
148 test_cmp expected actual
149'
150
151test_expect_success '"add -b" with <branch> omitted' '
152 git worktree add -b burble flornk &&
153 test_cmp_rev HEAD burble
154'
155
156test_expect_success '"add --detach" with <branch> omitted' '
157 git worktree add --detach fishhook &&
158 git rev-parse HEAD >expected &&
159 git -C fishhook rev-parse HEAD >actual &&
160 test_cmp expected actual &&
161 test_must_fail git -C fishhook symbolic-ref HEAD
162'
163
164test_expect_success '"add" with <branch> omitted' '
165 git worktree add wiffle/bat &&
166 test_cmp_rev HEAD bat
167'
168
169test_expect_success '"add" auto-vivify does not clobber existing branch' '
170 test_commit c1 &&
171 test_commit c2 &&
172 git branch precious HEAD~1 &&
173 test_must_fail git worktree add precious &&
174 test_cmp_rev HEAD~1 precious &&
175 test_path_is_missing precious
176'
177
178test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
179 git worktree add --detach mish/mash &&
180 test_must_fail git rev-parse mash -- &&
181 test_must_fail git -C mish/mash symbolic-ref HEAD
182'
183
184test_expect_success '"add" -b/-B mutually exclusive' '
185 test_must_fail git worktree add -b poodle -B poodle bamboo master
186'
187
188test_expect_success '"add" -b/--detach mutually exclusive' '
189 test_must_fail git worktree add -b poodle --detach bamboo master
190'
191
192test_expect_success '"add" -B/--detach mutually exclusive' '
193 test_must_fail git worktree add -B poodle --detach bamboo master
194'
195
196test_expect_success '"add -B" fails if the branch is checked out' '
197 git rev-parse newmaster >before &&
198 test_must_fail git worktree add -B newmaster bamboo master &&
199 git rev-parse newmaster >after &&
200 test_cmp before after
201'
202
203test_expect_success 'add -B' '
204 git worktree add -B poodle bamboo2 master^ &&
205 git -C bamboo2 symbolic-ref HEAD >actual &&
206 echo refs/heads/poodle >expected &&
207 test_cmp expected actual &&
208 test_cmp_rev master^ poodle
209'
210
211test_expect_success 'local clone from linked checkout' '
212 git clone --local here here-clone &&
213 ( cd here-clone && git fsck )
214'
215
216test_expect_success '"add" worktree with --no-checkout' '
217 git worktree add --no-checkout -b swamp swamp &&
218 ! test -e swamp/init.t &&
219 git -C swamp reset --hard &&
220 test_cmp init.t swamp/init.t
221'
222
223test_expect_success '"add" worktree with --checkout' '
224 git worktree add --checkout -b swmap2 swamp2 &&
225 test_cmp init.t swamp2/init.t
226'
227
228test_done