Skip to content

Commit

Permalink
worktree: add: suppress auto-vivication with --detach and no <branch>
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Eric Sunshine authored and Junio C Hamano committed Jul 20, 2015
1 parent ab0b2c5 commit 5c94257
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Documentation/git-worktree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Create `<path>` and checkout `<branch>` into it. The new working directory
is linked to the current repository, sharing everything except working
directory specific files such as HEAD, index, etc.
+
If `<branch>` is omitted and neither `-b` nor `-B` is used, then, as a
convenience, a new branch based at HEAD is created automatically, as if
`-b $(basename <path>)` was specified.
If `<branch>` is omitted and neither `-b` nor `-B` nor `--detached` used,
then, as a convenience, a new branch based at HEAD is created automatically,
as if `-b $(basename <path>)` was specified.

prune::

Expand Down
2 changes: 1 addition & 1 deletion builtin/worktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ static int add(int ac, const char **av, const char *prefix)
if (opts.force_new_branch)
opts.new_branch = new_branch_force;

if (ac < 2 && !opts.new_branch) {
if (ac < 2 && !opts.new_branch && !opts.detach) {
int n;
const char *s = worktree_basename(path, &n);
opts.new_branch = xstrndup(s, n);
Expand Down
14 changes: 14 additions & 0 deletions t/t2025-worktree-add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ test_expect_success '"add -b" with <branch> omitted' '
test_cmp_rev HEAD burble
'

test_expect_success '"add --detach" with <branch> omitted' '
git worktree add --detach fishhook &&
git rev-parse HEAD >expected &&
git -C fishhook rev-parse HEAD >actual &&
test_cmp expected actual &&
test_must_fail git -C fishhook symbolic-ref HEAD
'

test_expect_success '"add" with <branch> omitted' '
git worktree add wiffle/bat &&
test_cmp_rev HEAD bat
Expand All @@ -167,6 +175,12 @@ test_expect_success '"add" auto-vivify does not clobber existing branch' '
test_path_is_missing precious
'

test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
git worktree add --detach mish/mash &&
test_must_fail git rev-parse mash -- &&
test_must_fail git -C mish/mash symbolic-ref HEAD
'

test_expect_success '"add" -b/-B mutually exclusive' '
test_must_fail git worktree add -b poodle -B poodle bamboo master
'
Expand Down

0 comments on commit 5c94257

Please sign in to comment.