Skip to content

Commit 5c94257

Browse files
sunshinecogitster
authored andcommitted
worktree: add: suppress auto-vivication with --detach and no <branch>
Fix oversight where branch auto-vivication incorrectly kicks in when --detach is specified and <branch> omitted. Instead, treat: git worktree add --detach <path> as shorthand for: git worktree add --detach <path> HEAD Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab0b2c5 commit 5c94257

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Documentation/git-worktree.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ Create `<path>` and checkout `<branch>` into it. The new working directory
5151
is linked to the current repository, sharing everything except working
5252
directory specific files such as HEAD, index, etc.
5353
+
54-
If `<branch>` is omitted and neither `-b` nor `-B` is used, then, as a
55-
convenience, a new branch based at HEAD is created automatically, as if
56-
`-b $(basename <path>)` was specified.
54+
If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,
55+
then, as a convenience, a new branch based at HEAD is created automatically,
56+
as if `-b $(basename <path>)` was specified.
5757

5858
prune::
5959

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static int add(int ac, const char **av, const char *prefix)
308308
if (opts.force_new_branch)
309309
opts.new_branch = new_branch_force;
310310

311-
if (ac < 2 && !opts.new_branch) {
311+
if (ac < 2 && !opts.new_branch && !opts.detach) {
312312
int n;
313313
const char *s = worktree_basename(path, &n);
314314
opts.new_branch = xstrndup(s, n);

t/t2025-worktree-add.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ test_expect_success '"add -b" with <branch> omitted' '
153153
test_cmp_rev HEAD burble
154154
'
155155

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
162+
'
163+
156164
test_expect_success '"add" with <branch> omitted' '
157165
git worktree add wiffle/bat &&
158166
test_cmp_rev HEAD bat
@@ -167,6 +175,12 @@ test_expect_success '"add" auto-vivify does not clobber existing branch' '
167175
test_path_is_missing precious
168176
'
169177

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
182+
'
183+
170184
test_expect_success '"add" -b/-B mutually exclusive' '
171185
test_must_fail git worktree add -b poodle -B poodle bamboo master
172186
'

0 commit comments

Comments
 (0)