summaryrefslogtreecommitdiff
path: root/builtin/worktree.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2016-02-15 20:35:33 +0700
committerJunio C Hamano <gitster@pobox.com>2016-02-15 15:54:13 -0800
commitbeb6f24beef67f6f825c8c99e408f67d23b784d4 (patch)
tree943606669576499fc5e0fd4a3f1c05b7bc50c75f /builtin/worktree.c
parent0ebf4a2af3b72847327e977c5710fd123926570e (diff)
downloadgit-beb6f24beef67f6f825c8c99e408f67d23b784d4.tar.gz
worktree add -B: do the checkout test before update branchnd/worktree-add-B
If --force is not given but -B is, we should not proceed if the given branch is already checked out elsewhere. add_worktree() has this test, but it kicks in too late when "git branch --force" is already executed. As a result, even though we correctly refuse to create a new worktree, we have already updated the branch and mess up the other checkout. Repeat the die_if_checked_out() test again for this specific case before "git branch" runs. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/worktree.c')
-rw-r--r--builtin/worktree.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 6b9c946693..20cf67a549 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -334,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);