Skip to content

Commit 0ebf4a2

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4943984 commit 0ebf4a2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

builtin/worktree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ static int add_worktree(const char *path, const char *refname,
201201
die(_("'%s' already exists"), path);
202202

203203
/* 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) &&
207205
ref_exists(symref.buf)) { /* it's a branch */
208206
if (!opts->force)
209207
die_if_checked_out(symref.buf);

t/t2025-worktree-add.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
193193
test_must_fail git worktree add -B poodle --detach bamboo master
194194
'
195195

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+
196204
test_expect_success 'local clone from linked checkout' '
197205
git clone --local here here-clone &&
198206
( cd here-clone && git fsck )

0 commit comments

Comments
 (0)