worktree: fix "add -B"
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>
Nguyễn Thái Ngọc Duy committed
Feb 15, 2016 at 20:35 UTC
0ebf4a2af3b72847327e977c5710fd123926570e
2 files changed
+9
-3
builtin/worktree.c
+1
-3
@@ -201,9 +201,7 @@ static int add_worktree(const char *path, const char *refname,
201
die(_("'%s' already exists"), path);
202
203
/* is 'refname' a branch or commit? */
204
- if (opts->force_new_branch) /* definitely a branch */
205
- ;
206
- else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
204
+ if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
205
ref_exists(symref.buf)) { /* it's a branch */
206
if (!opts->force)
207
die_if_checked_out(symref.buf);
t/t2025-worktree-add.sh
+8
@@ -193,6 +193,14 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
193
test_must_fail git worktree add -B poodle --detach bamboo master
194
'
195
196
+test_expect_success 'add -B' '
197
+ git worktree add -B poodle bamboo2 master^ &&
198
+ git -C bamboo2 symbolic-ref HEAD >actual &&
199
+ echo refs/heads/poodle >expected &&
200
+ test_cmp expected actual &&
201
+ test_cmp_rev master^ poodle
202
+'
203
+
204
test_expect_success 'local clone from linked checkout' '
205
git clone --local here here-clone &&
206
( cd here-clone && git fsck )