Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'nd/worktree-add-B'
"git worktree add -B <branchname>" did not work.

* nd/worktree-add-B:
  worktree add -B: do the checkout test before update branch
  worktree: fix "add -B"
  • Loading branch information
Junio C Hamano committed Feb 24, 2016
2 parents 5e57f9c + beb6f24 commit 595bfef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions builtin/worktree.c
Expand Up @@ -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);
Expand Down Expand Up @@ -336,9 +334,18 @@ static int add(int ac, const char **av, const char *prefix)
branch = ac < 2 ? "HEAD" : av[1];

opts.force_new_branch = !!new_branch_force;
if (opts.force_new_branch)
if (opts.force_new_branch) {
struct strbuf symref = STRBUF_INIT;

opts.new_branch = new_branch_force;

if (!opts.force &&
!strbuf_check_branch_ref(&symref, opts.new_branch) &&
ref_exists(symref.buf))
die_if_checked_out(symref.buf);
strbuf_release(&symref);
}

if (ac < 2 && !opts.new_branch && !opts.detach) {
int n;
const char *s = worktree_basename(path, &n);
Expand Down
15 changes: 15 additions & 0 deletions t/t2025-worktree-add.sh
Expand Up @@ -193,6 +193,21 @@ test_expect_success '"add" -B/--detach mutually exclusive' '
test_must_fail git worktree add -B poodle --detach bamboo master
'

test_expect_success '"add -B" fails if the branch is checked out' '
git rev-parse newmaster >before &&
test_must_fail git worktree add -B newmaster bamboo master &&
git rev-parse newmaster >after &&
test_cmp before after
'

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 )
Expand Down

0 comments on commit 595bfef

Please sign in to comment.