3 test_description='test git worktree add'
7 test_expect_success 'setup' '
11 test_expect_success '"add" an existing worktree' '
12 mkdir -p existing/subtree &&
13 test_must_fail git worktree add --detach existing master
16 test_expect_success '"add" an existing empty worktree' '
17 mkdir existing_empty &&
18 git worktree add --detach existing_empty master
21 test_expect_success '"add" refuses to checkout locked branch' '
22 test_must_fail git worktree add zere master &&
24 ! test -d .git/worktrees/zere
27 test_expect_success 'checking out paths not complaining about linked checkouts' '
30 echo dirty >>init.t &&
31 git checkout master -- init.t
35 test_expect_success '"add" worktree' '
36 git rev-parse HEAD >expect &&
37 git worktree add --detach here master &&
40 test_cmp ../init.t init.t &&
41 test_must_fail git symbolic-ref HEAD &&
42 git rev-parse HEAD >actual &&
43 test_cmp ../expect actual &&
48 test_expect_success '"add" worktree from a subdir' '
52 git worktree add --detach here master &&
54 test_cmp ../../init.t init.t
58 test_expect_success '"add" from a linked checkout' '
61 git worktree add --detach nested-here master &&
67 test_expect_success '"add" worktree creating new branch' '
68 git worktree add -b newmaster there master &&
71 test_cmp ../init.t init.t &&
72 git symbolic-ref HEAD >actual &&
73 echo refs/heads/newmaster >expect &&
74 test_cmp expect actual &&
79 test_expect_success 'die the same branch is already checked out' '
82 test_must_fail git checkout newmaster
86 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
87 head=$(git -C there rev-parse --git-path HEAD) &&
88 ref=$(git -C there symbolic-ref HEAD) &&
90 ln -s "$ref" "$head" &&
91 test_must_fail git -C here checkout newmaster
94 test_expect_success 'not die the same branch is already checked out' '
97 git worktree add --force anothernewmaster newmaster
101 test_expect_success 'not die on re-checking out current branch' '
104 git checkout newmaster
108 test_expect_success '"add" from a bare repo' '
110 git clone --bare . bare &&
112 git worktree add -b bare-master ../there2 master
116 test_expect_success 'checkout from a bare repo without "add"' '
119 test_must_fail git checkout master
123 test_expect_success 'checkout with grafts' '
124 test_when_finished rm .git/info/grafts &&
126 SHA1=`git rev-parse HEAD` &&
129 echo "`git rev-parse HEAD` $SHA1" >.git/info/grafts &&
130 cat >expected <<-\EOF &&
134 git log --format=%s -2 >actual &&
135 test_cmp expected actual &&
136 git worktree add --detach grafted master &&
137 git --git-dir=grafted/.git log --format=%s -2 >actual &&
138 test_cmp expected actual
141 test_expect_success '"add" from relative HEAD' '
145 git rev-parse HEAD~1 >expected &&
146 git worktree add relhead HEAD~1 &&
147 git -C relhead rev-parse HEAD >actual &&
148 test_cmp expected actual
151 test_expect_success '"add -b" with <branch> omitted' '
152 git worktree add -b burble flornk &&
153 test_cmp_rev HEAD burble
156 test_expect_success '"add --detach" with <branch> omitted' '
157 git worktree add --detach fishhook &&
158 git rev-parse HEAD >expected &&
159 git -C fishhook rev-parse HEAD >actual &&
160 test_cmp expected actual &&
161 test_must_fail git -C fishhook symbolic-ref HEAD
164 test_expect_success '"add" with <branch> omitted' '
165 git worktree add wiffle/bat &&
166 test_cmp_rev HEAD bat
169 test_expect_success '"add" auto-vivify does not clobber existing branch' '
172 git branch precious HEAD~1 &&
173 test_must_fail git worktree add precious &&
174 test_cmp_rev HEAD~1 precious &&
175 test_path_is_missing precious
178 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
179 git worktree add --detach mish/mash &&
180 test_must_fail git rev-parse mash -- &&
181 test_must_fail git -C mish/mash symbolic-ref HEAD
184 test_expect_success '"add" -b/-B mutually exclusive' '
185 test_must_fail git worktree add -b poodle -B poodle bamboo master
188 test_expect_success '"add" -b/--detach mutually exclusive' '
189 test_must_fail git worktree add -b poodle --detach bamboo master
192 test_expect_success '"add" -B/--detach mutually exclusive' '
193 test_must_fail git worktree add -B poodle --detach bamboo master