Merge branch 'rs/apply-validate-input' into maint
[git/raj.git] / t / t2025-worktree-add.sh
blobb5c47ac602d65ebfb0f4baebc098f26d5d1a464c
1 #!/bin/sh
3 test_description='test git worktree add'
5 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-rebase.sh
9 test_expect_success 'setup' '
10 test_commit init
13 test_expect_success '"add" an existing worktree' '
14 mkdir -p existing/subtree &&
15 test_must_fail git worktree add --detach existing master
18 test_expect_success '"add" an existing empty worktree' '
19 mkdir existing_empty &&
20 git worktree add --detach existing_empty master
23 test_expect_success '"add" using shorthand - fails when no previous branch' '
24 test_must_fail git worktree add existing_short -
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
39 test_expect_success '"add" refuses to checkout locked branch' '
40 test_must_fail git worktree add zere master &&
41 ! test -d zere &&
42 ! test -d .git/worktrees/zere
45 test_expect_success 'checking out paths not complaining about linked checkouts' '
47 cd existing_empty &&
48 echo dirty >>init.t &&
49 git checkout master -- init.t
53 test_expect_success '"add" worktree' '
54 git rev-parse HEAD >expect &&
55 git worktree add --detach here master &&
57 cd here &&
58 test_cmp ../init.t init.t &&
59 test_must_fail git symbolic-ref HEAD &&
60 git rev-parse HEAD >actual &&
61 test_cmp ../expect actual &&
62 git fsck
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
72 test_expect_success '"add" worktree from a subdir' '
74 mkdir sub &&
75 cd sub &&
76 git worktree add --detach here master &&
77 cd here &&
78 test_cmp ../../init.t init.t
82 test_expect_success '"add" from a linked checkout' '
84 cd here &&
85 git worktree add --detach nested-here master &&
86 cd nested-here &&
87 git fsck
91 test_expect_success '"add" worktree creating new branch' '
92 git worktree add -b newmaster there master &&
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
103 test_expect_success 'die the same branch is already checked out' '
105 cd here &&
106 test_must_fail git checkout newmaster
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
118 test_expect_success 'not die the same branch is already checked out' '
120 cd here &&
121 git worktree add --force anothernewmaster newmaster
125 test_expect_success 'not die on re-checking out current branch' '
127 cd there &&
128 git checkout newmaster
132 test_expect_success '"add" from a bare repo' '
134 git clone --bare . bare &&
135 cd bare &&
136 git worktree add -b bare-master ../there2 master
140 test_expect_success 'checkout from a bare repo without "add"' '
142 cd bare &&
143 test_must_fail git checkout master
147 test_expect_success '"add" default branch of a bare repo' '
149 git clone --bare . bare2 &&
150 cd bare2 &&
151 git worktree add ../there3 master
155 test_expect_success 'checkout with grafts' '
156 test_when_finished rm .git/info/grafts &&
157 test_commit abc &&
158 SHA1=$(git rev-parse HEAD) &&
159 test_commit def &&
160 test_commit xyz &&
161 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
162 cat >expected <<-\EOF &&
166 git log --format=%s -2 >actual &&
167 test_cmp expected actual &&
168 git worktree add --detach grafted master &&
169 git --git-dir=grafted/.git log --format=%s -2 >actual &&
170 test_cmp expected actual
173 test_expect_success '"add" from relative HEAD' '
174 test_commit a &&
175 test_commit b &&
176 test_commit c &&
177 git rev-parse HEAD~1 >expected &&
178 git worktree add relhead HEAD~1 &&
179 git -C relhead rev-parse HEAD >actual &&
180 test_cmp expected actual
183 test_expect_success '"add -b" with <branch> omitted' '
184 git worktree add -b burble flornk &&
185 test_cmp_rev HEAD burble
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
196 test_expect_success '"add" with <branch> omitted' '
197 git worktree add wiffle/bat &&
198 test_cmp_rev HEAD bat
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
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
216 test_expect_success '"add" -b/-B mutually exclusive' '
217 test_must_fail git worktree add -b poodle -B poodle bamboo master
220 test_expect_success '"add" -b/--detach mutually exclusive' '
221 test_must_fail git worktree add -b poodle --detach bamboo master
224 test_expect_success '"add" -B/--detach mutually exclusive' '
225 test_must_fail git worktree add -B poodle --detach bamboo master
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
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
243 test_expect_success 'local clone from linked checkout' '
244 git clone --local here here-clone &&
245 ( cd here-clone && git fsck )
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
255 test_expect_success '"add" worktree with --checkout' '
256 git worktree add --checkout -b swmap2 swamp2 &&
257 test_cmp init.t swamp2/init.t
260 test_expect_success 'put a worktree under rebase' '
261 git worktree add under-rebase &&
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"
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
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
280 test_expect_success 'not allow to delete a branch under rebase' '
282 cd under-rebase &&
283 test_must_fail git branch -D under-rebase
287 test_expect_success 'rename a branch under rebase not allowed' '
288 test_must_fail git branch -M under-rebase rebase-with-new-name
291 test_expect_success 'check out from current worktree branch ok' '
293 cd under-rebase &&
294 git checkout under-rebase &&
295 git checkout - &&
296 git rebase --abort
300 test_expect_success 'checkout a branch under bisect' '
301 git worktree add under-bisect &&
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
313 test_expect_success 'rename a branch under bisect not allowed' '
314 test_must_fail git branch -M under-bisect bisect-with-new-name
317 test_done