test-online-cpus: helper to return cpu count
[git/debian.git] / t / t2025-worktree-add.sh
blobb618d6be21ff8926cb0d9dff1881c6e5e9eeb35a
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 '"add" default branch of a bare repo' '
143 git clone --bare . bare2 &&
144 cd bare2 &&
145 git worktree add ../there3 master
149 test_expect_success 'checkout with grafts' '
150 test_when_finished rm .git/info/grafts &&
151 test_commit abc &&
152 SHA1=$(git rev-parse HEAD) &&
153 test_commit def &&
154 test_commit xyz &&
155 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
156 cat >expected <<-\EOF &&
160 git log --format=%s -2 >actual &&
161 test_cmp expected actual &&
162 git worktree add --detach grafted master &&
163 git --git-dir=grafted/.git log --format=%s -2 >actual &&
164 test_cmp expected actual
167 test_expect_success '"add" from relative HEAD' '
168 test_commit a &&
169 test_commit b &&
170 test_commit c &&
171 git rev-parse HEAD~1 >expected &&
172 git worktree add relhead HEAD~1 &&
173 git -C relhead rev-parse HEAD >actual &&
174 test_cmp expected actual
177 test_expect_success '"add -b" with <branch> omitted' '
178 git worktree add -b burble flornk &&
179 test_cmp_rev HEAD burble
182 test_expect_success '"add --detach" with <branch> omitted' '
183 git worktree add --detach fishhook &&
184 git rev-parse HEAD >expected &&
185 git -C fishhook rev-parse HEAD >actual &&
186 test_cmp expected actual &&
187 test_must_fail git -C fishhook symbolic-ref HEAD
190 test_expect_success '"add" with <branch> omitted' '
191 git worktree add wiffle/bat &&
192 test_cmp_rev HEAD bat
195 test_expect_success '"add" auto-vivify does not clobber existing branch' '
196 test_commit c1 &&
197 test_commit c2 &&
198 git branch precious HEAD~1 &&
199 test_must_fail git worktree add precious &&
200 test_cmp_rev HEAD~1 precious &&
201 test_path_is_missing precious
204 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
205 git worktree add --detach mish/mash &&
206 test_must_fail git rev-parse mash -- &&
207 test_must_fail git -C mish/mash symbolic-ref HEAD
210 test_expect_success '"add" -b/-B mutually exclusive' '
211 test_must_fail git worktree add -b poodle -B poodle bamboo master
214 test_expect_success '"add" -b/--detach mutually exclusive' '
215 test_must_fail git worktree add -b poodle --detach bamboo master
218 test_expect_success '"add" -B/--detach mutually exclusive' '
219 test_must_fail git worktree add -B poodle --detach bamboo master
222 test_expect_success '"add -B" fails if the branch is checked out' '
223 git rev-parse newmaster >before &&
224 test_must_fail git worktree add -B newmaster bamboo master &&
225 git rev-parse newmaster >after &&
226 test_cmp before after
229 test_expect_success 'add -B' '
230 git worktree add -B poodle bamboo2 master^ &&
231 git -C bamboo2 symbolic-ref HEAD >actual &&
232 echo refs/heads/poodle >expected &&
233 test_cmp expected actual &&
234 test_cmp_rev master^ poodle
237 test_expect_success 'local clone from linked checkout' '
238 git clone --local here here-clone &&
239 ( cd here-clone && git fsck )
242 test_expect_success '"add" worktree with --no-checkout' '
243 git worktree add --no-checkout -b swamp swamp &&
244 ! test -e swamp/init.t &&
245 git -C swamp reset --hard &&
246 test_cmp init.t swamp/init.t
249 test_expect_success '"add" worktree with --checkout' '
250 git worktree add --checkout -b swmap2 swamp2 &&
251 test_cmp init.t swamp2/init.t
254 test_expect_success 'put a worktree under rebase' '
255 git worktree add under-rebase &&
257 cd under-rebase &&
258 set_fake_editor &&
259 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
260 git worktree list | grep "under-rebase.*detached HEAD"
264 test_expect_success 'add a worktree, checking out a rebased branch' '
265 test_must_fail git worktree add new-rebase under-rebase &&
266 ! test -d new-rebase
269 test_expect_success 'checking out a rebased branch from another worktree' '
270 git worktree add new-place &&
271 test_must_fail git -C new-place checkout under-rebase
274 test_expect_success 'not allow to delete a branch under rebase' '
276 cd under-rebase &&
277 test_must_fail git branch -D under-rebase
281 test_expect_success 'rename a branch under rebase not allowed' '
282 test_must_fail git branch -M under-rebase rebase-with-new-name
285 test_expect_success 'check out from current worktree branch ok' '
287 cd under-rebase &&
288 git checkout under-rebase &&
289 git checkout - &&
290 git rebase --abort
294 test_expect_success 'checkout a branch under bisect' '
295 git worktree add under-bisect &&
297 cd under-bisect &&
298 git bisect start &&
299 git bisect bad &&
300 git bisect good HEAD~2 &&
301 git worktree list | grep "under-bisect.*detached HEAD" &&
302 test_must_fail git worktree add new-bisect under-bisect &&
303 ! test -d new-bisect
307 test_expect_success 'rename a branch under bisect not allowed' '
308 test_must_fail git branch -M under-bisect bisect-with-new-name
311 test_done