]>
Commit | Line | Data |
---|---|---|
529fef20 NTND |
1 | #!/bin/sh |
2 | ||
f194b1ef | 3 | test_description='test git worktree add' |
529fef20 NTND |
4 | |
5 | . ./test-lib.sh | |
6 | ||
8d9fdd70 NTND |
7 | . "$TEST_DIRECTORY"/lib-rebase.sh |
8 | ||
529fef20 NTND |
9 | test_expect_success 'setup' ' |
10 | test_commit init | |
11 | ' | |
12 | ||
f194b1ef | 13 | test_expect_success '"add" an existing worktree' ' |
ee4fb843 | 14 | mkdir -p existing/subtree && |
f194b1ef | 15 | test_must_fail git worktree add --detach existing master |
529fef20 NTND |
16 | ' |
17 | ||
f194b1ef | 18 | test_expect_success '"add" an existing empty worktree' ' |
ee4fb843 | 19 | mkdir existing_empty && |
f194b1ef | 20 | git worktree add --detach existing_empty master |
ee4fb843 MK |
21 | ' |
22 | ||
1a450e2f JDG |
23 | test_expect_success '"add" using shorthand - fails when no previous branch' ' |
24 | test_must_fail git worktree add existing_short - | |
25 | ' | |
26 | ||
27 | test_expect_success '"add" using - shorthand' ' | |
28 | git checkout -b newbranch && | |
29 | echo hello >myworld && | |
30 | git add myworld && | |
31 | git commit -m myworld && | |
32 | git checkout master && | |
33 | git worktree add short-hand - && | |
34 | echo refs/heads/newbranch >expect && | |
35 | git -C short-hand rev-parse --symbolic-full-name HEAD >actual && | |
36 | test_cmp expect actual | |
37 | ' | |
38 | ||
f194b1ef ES |
39 | test_expect_success '"add" refuses to checkout locked branch' ' |
40 | test_must_fail git worktree add zere master && | |
3b8925c7 NTND |
41 | ! test -d zere && |
42 | ! test -d .git/worktrees/zere | |
43 | ' | |
44 | ||
e1c1ab9d NTND |
45 | test_expect_success 'checking out paths not complaining about linked checkouts' ' |
46 | ( | |
47 | cd existing_empty && | |
48 | echo dirty >>init.t && | |
49 | git checkout master -- init.t | |
50 | ) | |
51 | ' | |
52 | ||
f194b1ef | 53 | test_expect_success '"add" worktree' ' |
5883034c | 54 | git rev-parse HEAD >expect && |
f194b1ef | 55 | git worktree add --detach here master && |
529fef20 NTND |
56 | ( |
57 | cd here && | |
58 | test_cmp ../init.t init.t && | |
5883034c NTND |
59 | test_must_fail git symbolic-ref HEAD && |
60 | git rev-parse HEAD >actual && | |
61 | test_cmp ../expect actual && | |
529fef20 NTND |
62 | git fsck |
63 | ) | |
64 | ' | |
65 | ||
507e6e9e NTND |
66 | test_expect_success '"add" worktree with lock' ' |
67 | git rev-parse HEAD >expect && | |
68 | git worktree add --detach --lock here-with-lock master && | |
69 | test -f .git/worktrees/here-with-lock/locked | |
70 | ' | |
71 | ||
f194b1ef | 72 | test_expect_success '"add" worktree from a subdir' ' |
529fef20 NTND |
73 | ( |
74 | mkdir sub && | |
75 | cd sub && | |
f194b1ef | 76 | git worktree add --detach here master && |
529fef20 NTND |
77 | cd here && |
78 | test_cmp ../../init.t init.t | |
79 | ) | |
80 | ' | |
81 | ||
f194b1ef | 82 | test_expect_success '"add" from a linked checkout' ' |
529fef20 NTND |
83 | ( |
84 | cd here && | |
f194b1ef | 85 | git worktree add --detach nested-here master && |
529fef20 NTND |
86 | cd nested-here && |
87 | git fsck | |
88 | ) | |
89 | ' | |
90 | ||
f194b1ef ES |
91 | test_expect_success '"add" worktree creating new branch' ' |
92 | git worktree add -b newmaster there master && | |
529fef20 NTND |
93 | ( |
94 | cd there && | |
95 | test_cmp ../init.t init.t && | |
96 | git symbolic-ref HEAD >actual && | |
97 | echo refs/heads/newmaster >expect && | |
98 | test_cmp expect actual && | |
99 | git fsck | |
100 | ) | |
101 | ' | |
102 | ||
5883034c NTND |
103 | test_expect_success 'die the same branch is already checked out' ' |
104 | ( | |
105 | cd here && | |
106 | test_must_fail git checkout newmaster | |
107 | ) | |
108 | ' | |
109 | ||
746bbdc6 ES |
110 | test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' ' |
111 | head=$(git -C there rev-parse --git-path HEAD) && | |
112 | ref=$(git -C there symbolic-ref HEAD) && | |
113 | rm "$head" && | |
114 | ln -s "$ref" "$head" && | |
115 | test_must_fail git -C here checkout newmaster | |
116 | ' | |
117 | ||
1d0fa898 NTND |
118 | test_expect_success 'not die the same branch is already checked out' ' |
119 | ( | |
120 | cd here && | |
f194b1ef | 121 | git worktree add --force anothernewmaster newmaster |
1d0fa898 NTND |
122 | ) |
123 | ' | |
124 | ||
5883034c NTND |
125 | test_expect_success 'not die on re-checking out current branch' ' |
126 | ( | |
127 | cd there && | |
128 | git checkout newmaster | |
129 | ) | |
130 | ' | |
131 | ||
f194b1ef | 132 | test_expect_success '"add" from a bare repo' ' |
3473ad0c DK |
133 | ( |
134 | git clone --bare . bare && | |
135 | cd bare && | |
f194b1ef | 136 | git worktree add -b bare-master ../there2 master |
3473ad0c DK |
137 | ) |
138 | ' | |
139 | ||
f194b1ef | 140 | test_expect_success 'checkout from a bare repo without "add"' ' |
3473ad0c DK |
141 | ( |
142 | cd bare && | |
143 | test_must_fail git checkout master | |
144 | ) | |
145 | ' | |
146 | ||
171c646f DK |
147 | test_expect_success '"add" default branch of a bare repo' ' |
148 | ( | |
149 | git clone --bare . bare2 && | |
150 | cd bare2 && | |
151 | git worktree add ../there3 master | |
152 | ) | |
153 | ' | |
154 | ||
ad35f615 NTND |
155 | test_expect_success 'checkout with grafts' ' |
156 | test_when_finished rm .git/info/grafts && | |
157 | test_commit abc && | |
697b90d7 | 158 | SHA1=$(git rev-parse HEAD) && |
ad35f615 NTND |
159 | test_commit def && |
160 | test_commit xyz && | |
697b90d7 | 161 | echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts && |
ad35f615 NTND |
162 | cat >expected <<-\EOF && |
163 | xyz | |
164 | abc | |
165 | EOF | |
166 | git log --format=%s -2 >actual && | |
167 | test_cmp expected actual && | |
f194b1ef | 168 | git worktree add --detach grafted master && |
ad35f615 NTND |
169 | git --git-dir=grafted/.git log --format=%s -2 >actual && |
170 | test_cmp expected actual | |
171 | ' | |
172 | ||
f194b1ef | 173 | test_expect_success '"add" from relative HEAD' ' |
c990a4c1 ES |
174 | test_commit a && |
175 | test_commit b && | |
176 | test_commit c && | |
177 | git rev-parse HEAD~1 >expected && | |
f194b1ef | 178 | git worktree add relhead HEAD~1 && |
c990a4c1 ES |
179 | git -C relhead rev-parse HEAD >actual && |
180 | test_cmp expected actual | |
181 | ' | |
182 | ||
0f4af3b9 ES |
183 | test_expect_success '"add -b" with <branch> omitted' ' |
184 | git worktree add -b burble flornk && | |
185 | test_cmp_rev HEAD burble | |
186 | ' | |
187 | ||
5c942570 ES |
188 | test_expect_success '"add --detach" with <branch> omitted' ' |
189 | git worktree add --detach fishhook && | |
190 | git rev-parse HEAD >expected && | |
191 | git -C fishhook rev-parse HEAD >actual && | |
192 | test_cmp expected actual && | |
193 | test_must_fail git -C fishhook symbolic-ref HEAD | |
194 | ' | |
195 | ||
1eb07d82 ES |
196 | test_expect_success '"add" with <branch> omitted' ' |
197 | git worktree add wiffle/bat && | |
198 | test_cmp_rev HEAD bat | |
199 | ' | |
200 | ||
201 | test_expect_success '"add" auto-vivify does not clobber existing branch' ' | |
202 | test_commit c1 && | |
203 | test_commit c2 && | |
204 | git branch precious HEAD~1 && | |
205 | test_must_fail git worktree add precious && | |
206 | test_cmp_rev HEAD~1 precious && | |
207 | test_path_is_missing precious | |
208 | ' | |
209 | ||
5c942570 ES |
210 | test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' ' |
211 | git worktree add --detach mish/mash && | |
212 | test_must_fail git rev-parse mash -- && | |
213 | test_must_fail git -C mish/mash symbolic-ref HEAD | |
214 | ' | |
215 | ||
ab0b2c53 ES |
216 | test_expect_success '"add" -b/-B mutually exclusive' ' |
217 | test_must_fail git worktree add -b poodle -B poodle bamboo master | |
218 | ' | |
219 | ||
220 | test_expect_success '"add" -b/--detach mutually exclusive' ' | |
221 | test_must_fail git worktree add -b poodle --detach bamboo master | |
222 | ' | |
223 | ||
224 | test_expect_success '"add" -B/--detach mutually exclusive' ' | |
225 | test_must_fail git worktree add -B poodle --detach bamboo master | |
226 | ' | |
227 | ||
beb6f24b NTND |
228 | test_expect_success '"add -B" fails if the branch is checked out' ' |
229 | git rev-parse newmaster >before && | |
230 | test_must_fail git worktree add -B newmaster bamboo master && | |
231 | git rev-parse newmaster >after && | |
232 | test_cmp before after | |
233 | ' | |
234 | ||
0ebf4a2a NTND |
235 | test_expect_success 'add -B' ' |
236 | git worktree add -B poodle bamboo2 master^ && | |
237 | git -C bamboo2 symbolic-ref HEAD >actual && | |
238 | echo refs/heads/poodle >expected && | |
239 | test_cmp expected actual && | |
240 | test_cmp_rev master^ poodle | |
241 | ' | |
242 | ||
744e4697 NTND |
243 | test_expect_success 'local clone from linked checkout' ' |
244 | git clone --local here here-clone && | |
245 | ( cd here-clone && git fsck ) | |
246 | ' | |
247 | ||
ef2a0ac9 RZ |
248 | test_expect_success '"add" worktree with --no-checkout' ' |
249 | git worktree add --no-checkout -b swamp swamp && | |
250 | ! test -e swamp/init.t && | |
251 | git -C swamp reset --hard && | |
252 | test_cmp init.t swamp/init.t | |
253 | ' | |
254 | ||
255 | test_expect_success '"add" worktree with --checkout' ' | |
256 | git worktree add --checkout -b swmap2 swamp2 && | |
257 | test_cmp init.t swamp2/init.t | |
258 | ' | |
259 | ||
8d9fdd70 NTND |
260 | test_expect_success 'put a worktree under rebase' ' |
261 | git worktree add under-rebase && | |
262 | ( | |
263 | cd under-rebase && | |
264 | set_fake_editor && | |
265 | FAKE_LINES="edit 1" git rebase -i HEAD^ && | |
266 | git worktree list | grep "under-rebase.*detached HEAD" | |
267 | ) | |
268 | ' | |
269 | ||
270 | test_expect_success 'add a worktree, checking out a rebased branch' ' | |
271 | test_must_fail git worktree add new-rebase under-rebase && | |
272 | ! test -d new-rebase | |
273 | ' | |
274 | ||
275 | test_expect_success 'checking out a rebased branch from another worktree' ' | |
276 | git worktree add new-place && | |
277 | test_must_fail git -C new-place checkout under-rebase | |
278 | ' | |
279 | ||
280 | test_expect_success 'not allow to delete a branch under rebase' ' | |
281 | ( | |
282 | cd under-rebase && | |
283 | test_must_fail git branch -D under-rebase | |
284 | ) | |
285 | ' | |
286 | ||
14ace5b7 NTND |
287 | test_expect_success 'rename a branch under rebase not allowed' ' |
288 | test_must_fail git branch -M under-rebase rebase-with-new-name | |
289 | ' | |
290 | ||
8d9fdd70 NTND |
291 | test_expect_success 'check out from current worktree branch ok' ' |
292 | ( | |
293 | cd under-rebase && | |
294 | git checkout under-rebase && | |
295 | git checkout - && | |
296 | git rebase --abort | |
297 | ) | |
298 | ' | |
299 | ||
04a3dfb8 NTND |
300 | test_expect_success 'checkout a branch under bisect' ' |
301 | git worktree add under-bisect && | |
302 | ( | |
303 | cd under-bisect && | |
304 | git bisect start && | |
305 | git bisect bad && | |
306 | git bisect good HEAD~2 && | |
307 | git worktree list | grep "under-bisect.*detached HEAD" && | |
308 | test_must_fail git worktree add new-bisect under-bisect && | |
309 | ! test -d new-bisect | |
310 | ) | |
311 | ' | |
312 | ||
14ace5b7 NTND |
313 | test_expect_success 'rename a branch under bisect not allowed' ' |
314 | test_must_fail git branch -M under-bisect bisect-with-new-name | |
315 | ' | |
e284e892 TG |
316 | # Is branch "refs/heads/$1" set to pull from "$2/$3"? |
317 | test_branch_upstream () { | |
318 | printf "%s\n" "$2" "refs/heads/$3" >expect.upstream && | |
319 | { | |
320 | git config "branch.$1.remote" && | |
321 | git config "branch.$1.merge" | |
322 | } >actual.upstream && | |
323 | test_cmp expect.upstream actual.upstream | |
324 | } | |
325 | ||
326 | test_expect_success '--track sets up tracking' ' | |
327 | test_when_finished rm -rf track && | |
328 | git worktree add --track -b track track master && | |
329 | test_branch_upstream track . master | |
330 | ' | |
331 | ||
332 | # setup remote repository $1 and repository $2 with $1 set up as | |
333 | # remote. The remote has two branches, master and foo. | |
334 | setup_remote_repo () { | |
335 | git init $1 && | |
336 | ( | |
337 | cd $1 && | |
338 | test_commit $1_master && | |
339 | git checkout -b foo && | |
340 | test_commit upstream_foo | |
341 | ) && | |
342 | git init $2 && | |
343 | ( | |
344 | cd $2 && | |
345 | test_commit $2_master && | |
346 | git remote add $1 ../$1 && | |
347 | git config remote.$1.fetch \ | |
348 | "refs/heads/*:refs/remotes/$1/*" && | |
349 | git fetch --all | |
350 | ) | |
351 | } | |
352 | ||
353 | test_expect_success '--no-track avoids setting up tracking' ' | |
354 | test_when_finished rm -rf repo_upstream repo_local foo && | |
355 | setup_remote_repo repo_upstream repo_local && | |
356 | ( | |
357 | cd repo_local && | |
358 | git worktree add --no-track -b foo ../foo repo_upstream/foo | |
359 | ) && | |
360 | ( | |
361 | cd foo && | |
362 | test_must_fail git config "branch.foo.remote" && | |
363 | test_must_fail git config "branch.foo.merge" && | |
364 | test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo | |
365 | ) | |
366 | ' | |
14ace5b7 | 367 | |
4e853331 TG |
368 | test_expect_success '"add" <path> <non-existent-branch> fails' ' |
369 | test_must_fail git worktree add foo non-existent | |
370 | ' | |
371 | ||
372 | test_expect_success '"add" <path> <branch> dwims' ' | |
373 | test_when_finished rm -rf repo_upstream repo_dwim foo && | |
374 | setup_remote_repo repo_upstream repo_dwim && | |
375 | git init repo_dwim && | |
376 | ( | |
377 | cd repo_dwim && | |
378 | git worktree add ../foo foo | |
379 | ) && | |
380 | ( | |
381 | cd foo && | |
382 | test_branch_upstream foo repo_upstream foo && | |
383 | test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo | |
384 | ) | |
385 | ' | |
386 | ||
71d6682d TG |
387 | test_expect_success 'git worktree add does not match remote' ' |
388 | test_when_finished rm -rf repo_a repo_b foo && | |
389 | setup_remote_repo repo_a repo_b && | |
390 | ( | |
391 | cd repo_b && | |
392 | git worktree add ../foo | |
393 | ) && | |
394 | ( | |
395 | cd foo && | |
396 | test_must_fail git config "branch.foo.remote" && | |
397 | test_must_fail git config "branch.foo.merge" && | |
398 | ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
399 | ) | |
400 | ' | |
401 | ||
402 | test_expect_success 'git worktree add --guess-remote sets up tracking' ' | |
403 | test_when_finished rm -rf repo_a repo_b foo && | |
404 | setup_remote_repo repo_a repo_b && | |
405 | ( | |
406 | cd repo_b && | |
407 | git worktree add --guess-remote ../foo | |
408 | ) && | |
409 | ( | |
410 | cd foo && | |
411 | test_branch_upstream foo repo_a foo && | |
412 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
413 | ) | |
414 | ' | |
415 | ||
e92445a7 TG |
416 | test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' ' |
417 | test_when_finished rm -rf repo_a repo_b foo && | |
418 | setup_remote_repo repo_a repo_b && | |
419 | ( | |
420 | cd repo_b && | |
421 | git config worktree.guessRemote true && | |
422 | git worktree add ../foo | |
423 | ) && | |
424 | ( | |
425 | cd foo && | |
426 | test_branch_upstream foo repo_a foo && | |
427 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
428 | ) | |
429 | ' | |
430 | ||
431 | test_expect_success 'git worktree --no-guess-remote option overrides config' ' | |
432 | test_when_finished rm -rf repo_a repo_b foo && | |
433 | setup_remote_repo repo_a repo_b && | |
434 | ( | |
435 | cd repo_b && | |
436 | git config worktree.guessRemote true && | |
437 | git worktree add --no-guess-remote ../foo | |
438 | ) && | |
439 | ( | |
440 | cd foo && | |
441 | test_must_fail git config "branch.foo.remote" && | |
442 | test_must_fail git config "branch.foo.merge" && | |
443 | ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
444 | ) | |
445 | ' | |
446 | ||
529fef20 | 447 | test_done |