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