From 5c942570fe2a48d8fde348e89392c2e9e23aa483 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Fri, 17 Jul 2015 19:00:09 -0400 Subject: [PATCH] worktree: add: suppress auto-vivication with --detach and no Fix oversight where branch auto-vivication incorrectly kicks in when --detach is specified and omitted. Instead, treat: git worktree add --detach as shorthand for: git worktree add --detach HEAD Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- Documentation/git-worktree.txt | 6 +++--- builtin/worktree.c | 2 +- t/t2025-worktree-add.sh | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 32344598c3..332dd7734d 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -51,9 +51,9 @@ Create `` and checkout `` 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 `` 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 )` was specified. +If `` 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 )` was specified. prune:: diff --git a/builtin/worktree.c b/builtin/worktree.c index 813e016130..83484ad502 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -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); diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh index 249e4540dc..8267411a0e 100755 --- a/t/t2025-worktree-add.sh +++ b/t/t2025-worktree-add.sh @@ -153,6 +153,14 @@ test_expect_success '"add -b" with omitted' ' test_cmp_rev HEAD burble ' +test_expect_success '"add --detach" with 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 omitted' ' git worktree add wiffle/bat && test_cmp_rev HEAD bat @@ -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 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 '