Merge branch 'lf/sideband-returns-void'
[git/git-svn.git] / t / t2025-worktree-add.sh
blob4bcc335a19f9d7864560945ed0f430728d0f1986
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 from a subdir' '
68 mkdir sub &&
69 cd sub &&
70 git worktree add --detach here master &&
71 cd here &&
72 test_cmp ../../init.t init.t
76 test_expect_success '"add" from a linked checkout' '
78 cd here &&
79 git worktree add --detach nested-here master &&
80 cd nested-here &&
81 git fsck
85 test_expect_success '"add" worktree creating new branch' '
86 git worktree add -b newmaster there master &&
88 cd there &&
89 test_cmp ../init.t init.t &&
90 git symbolic-ref HEAD >actual &&
91 echo refs/heads/newmaster >expect &&
92 test_cmp expect actual &&
93 git fsck
97 test_expect_success 'die the same branch is already checked out' '
99 cd here &&
100 test_must_fail git checkout newmaster
104 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
105 head=$(git -C there rev-parse --git-path HEAD) &&
106 ref=$(git -C there symbolic-ref HEAD) &&
107 rm "$head" &&
108 ln -s "$ref" "$head" &&
109 test_must_fail git -C here checkout newmaster
112 test_expect_success 'not die the same branch is already checked out' '
114 cd here &&
115 git worktree add --force anothernewmaster newmaster
119 test_expect_success 'not die on re-checking out current branch' '
121 cd there &&
122 git checkout newmaster
126 test_expect_success '"add" from a bare repo' '
128 git clone --bare . bare &&
129 cd bare &&
130 git worktree add -b bare-master ../there2 master
134 test_expect_success 'checkout from a bare repo without "add"' '
136 cd bare &&
137 test_must_fail git checkout master
141 test_expect_success 'checkout with grafts' '
142 test_when_finished rm .git/info/grafts &&
143 test_commit abc &&
144 SHA1=$(git rev-parse HEAD) &&
145 test_commit def &&
146 test_commit xyz &&
147 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
148 cat >expected <<-\EOF &&
152 git log --format=%s -2 >actual &&
153 test_cmp expected actual &&
154 git worktree add --detach grafted master &&
155 git --git-dir=grafted/.git log --format=%s -2 >actual &&
156 test_cmp expected actual
159 test_expect_success '"add" from relative HEAD' '
160 test_commit a &&
161 test_commit b &&
162 test_commit c &&
163 git rev-parse HEAD~1 >expected &&
164 git worktree add relhead HEAD~1 &&
165 git -C relhead rev-parse HEAD >actual &&
166 test_cmp expected actual
169 test_expect_success '"add -b" with <branch> omitted' '
170 git worktree add -b burble flornk &&
171 test_cmp_rev HEAD burble
174 test_expect_success '"add --detach" with <branch> omitted' '
175 git worktree add --detach fishhook &&
176 git rev-parse HEAD >expected &&
177 git -C fishhook rev-parse HEAD >actual &&
178 test_cmp expected actual &&
179 test_must_fail git -C fishhook symbolic-ref HEAD
182 test_expect_success '"add" with <branch> omitted' '
183 git worktree add wiffle/bat &&
184 test_cmp_rev HEAD bat
187 test_expect_success '"add" auto-vivify does not clobber existing branch' '
188 test_commit c1 &&
189 test_commit c2 &&
190 git branch precious HEAD~1 &&
191 test_must_fail git worktree add precious &&
192 test_cmp_rev HEAD~1 precious &&
193 test_path_is_missing precious
196 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
197 git worktree add --detach mish/mash &&
198 test_must_fail git rev-parse mash -- &&
199 test_must_fail git -C mish/mash symbolic-ref HEAD
202 test_expect_success '"add" -b/-B mutually exclusive' '
203 test_must_fail git worktree add -b poodle -B poodle bamboo master
206 test_expect_success '"add" -b/--detach mutually exclusive' '
207 test_must_fail git worktree add -b poodle --detach bamboo master
210 test_expect_success '"add" -B/--detach mutually exclusive' '
211 test_must_fail git worktree add -B poodle --detach bamboo master
214 test_expect_success '"add -B" fails if the branch is checked out' '
215 git rev-parse newmaster >before &&
216 test_must_fail git worktree add -B newmaster bamboo master &&
217 git rev-parse newmaster >after &&
218 test_cmp before after
221 test_expect_success 'add -B' '
222 git worktree add -B poodle bamboo2 master^ &&
223 git -C bamboo2 symbolic-ref HEAD >actual &&
224 echo refs/heads/poodle >expected &&
225 test_cmp expected actual &&
226 test_cmp_rev master^ poodle
229 test_expect_success 'local clone from linked checkout' '
230 git clone --local here here-clone &&
231 ( cd here-clone && git fsck )
234 test_expect_success '"add" worktree with --no-checkout' '
235 git worktree add --no-checkout -b swamp swamp &&
236 ! test -e swamp/init.t &&
237 git -C swamp reset --hard &&
238 test_cmp init.t swamp/init.t
241 test_expect_success '"add" worktree with --checkout' '
242 git worktree add --checkout -b swmap2 swamp2 &&
243 test_cmp init.t swamp2/init.t
246 test_expect_success 'put a worktree under rebase' '
247 git worktree add under-rebase &&
249 cd under-rebase &&
250 set_fake_editor &&
251 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
252 git worktree list | grep "under-rebase.*detached HEAD"
256 test_expect_success 'add a worktree, checking out a rebased branch' '
257 test_must_fail git worktree add new-rebase under-rebase &&
258 ! test -d new-rebase
261 test_expect_success 'checking out a rebased branch from another worktree' '
262 git worktree add new-place &&
263 test_must_fail git -C new-place checkout under-rebase
266 test_expect_success 'not allow to delete a branch under rebase' '
268 cd under-rebase &&
269 test_must_fail git branch -D under-rebase
273 test_expect_success 'rename a branch under rebase not allowed' '
274 test_must_fail git branch -M under-rebase rebase-with-new-name
277 test_expect_success 'check out from current worktree branch ok' '
279 cd under-rebase &&
280 git checkout under-rebase &&
281 git checkout - &&
282 git rebase --abort
286 test_expect_success 'checkout a branch under bisect' '
287 git worktree add under-bisect &&
289 cd under-bisect &&
290 git bisect start &&
291 git bisect bad &&
292 git bisect good HEAD~2 &&
293 git worktree list | grep "under-bisect.*detached HEAD" &&
294 test_must_fail git worktree add new-bisect under-bisect &&
295 ! test -d new-bisect
299 test_expect_success 'rename a branch under bisect not allowed' '
300 test_must_fail git branch -M under-bisect bisect-with-new-name
303 test_done