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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 17, 2015 at 19:00 UTC 5c942570fe2a48d8fde348e89392c2e9e23aa483
3 files changed +18 -4
Documentation/git-worktree.txt
+3 -3
@@ -51,9 +51,9 @@ Create `<path>` and checkout `<branch>` into it. The new working directory
51 is linked to the current repository, sharing everything except working
52 directory specific files such as HEAD, index, etc.
53 +
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.
57
58 prune::
59
builtin/worktree.c
+1 -1
@@ -308,7 +308,7 @@ static int add(int ac, const char **av, const char *prefix)
308 if (opts.force_new_branch)
309 opts.new_branch = new_branch_force;
310
311 - if (ac < 2 && !opts.new_branch) {
311 + if (ac < 2 && !opts.new_branch && !opts.detach) {
312 int n;
313 const char *s = worktree_basename(path, &n);
314 opts.new_branch = xstrndup(s, n);
t/t2025-worktree-add.sh
+14
@@ -153,6 +153,14 @@ test_expect_success '"add -b" with <branch> omitted' '
153 test_cmp_rev HEAD burble
154 '
155
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 +
164 test_expect_success '"add" with <branch> omitted' '
165 git worktree add wiffle/bat &&
166 test_cmp_rev HEAD bat
@@ -167,6 +175,12 @@ test_expect_success '"add" auto-vivify does not clobber existing branch' '
175 test_path_is_missing precious
176 '
177
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 +
184 test_expect_success '"add" -b/-B mutually exclusive' '
185 test_must_fail git worktree add -b poodle -B poodle bamboo master
186 '