worktree: fix "add -B"
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 15 Feb 2016 13:35:32 +0000 (20:35 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 15 Feb 2016 23:49:33 +0000 (15:49 -0800)
Current code does not update "symref" when -B is used. This string
contains the new HEAD. Because it's empty "git worktree add -B" fails at
symbolic-ref step.

Because branch creation is already done before calling add_worktree(),
-B is equivalent to -b from add_worktree() point of view. We do not need
the special case for -B.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/worktree.c
t/t2025-worktree-add.sh
index 475b9581a5583166c43199f1662b510842c59c7d..6b9c946693ff5e0127dd4a6d0f6c93ba84631edb 100644 (file)
@@ -201,9 +201,7 @@ static int add_worktree(const char *path, const char *refname,
                die(_("'%s' already exists"), path);
 
        /* is 'refname' a branch or commit? */
-       if (opts->force_new_branch) /* definitely a branch */
-               ;
-       else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
+       if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
                 ref_exists(symref.buf)) { /* it's a branch */
                if (!opts->force)
                        die_if_checked_out(symref.buf);
index 0a804dab634d0a72bfabd8826d0cb38e1b4f7dbc..a4d36c0892db686a7677cf1ab23a9f8ed81859eb 100755 (executable)
@@ -193,6 +193,14 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
        test_must_fail git worktree add -B poodle --detach bamboo master
 '
 
+test_expect_success 'add -B' '
+       git worktree add -B poodle bamboo2 master^ &&
+       git -C bamboo2 symbolic-ref HEAD >actual &&
+       echo refs/heads/poodle >expected &&
+       test_cmp expected actual &&
+       test_cmp_rev master^ poodle
+'
+
 test_expect_success 'local clone from linked checkout' '
        git clone --local here here-clone &&
        ( cd here-clone && git fsck )