checkout: don't check worktrees when not necessary
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Fri, 12 Jun 2015 10:49:24 +0000 (17:49 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 12 Jun 2015 22:21:44 +0000 (15:21 -0700)
When --patch or pathspecs are passed to git checkout, the working tree
will not be switching branch, so there's no need to check if the branch
that we are running checkout on is already checked out.

Original-patch-by: Spencer Baugh <sbaugh@catern.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2025-checkout-to.sh
index 8b2bf2093bfe3ce1dbae7d5d7c50886386be6f7b..2079aa417076201dd6bafe832393eb27aaa83ec9 100644 (file)
@@ -1085,7 +1085,6 @@ static int parse_branchname_arg(int argc, const char **argv,
 {
        struct tree **source_tree = &opts->source_tree;
        const char **new_branch = &opts->new_branch;
-       int force_detach = opts->force_detach;
        int argcount = 0;
        unsigned char branch_rev[20];
        const char *arg;
@@ -1206,17 +1205,6 @@ static int parse_branchname_arg(int argc, const char **argv,
        else
                new->path = NULL; /* not an existing branch */
 
-       if (new->path && !force_detach && !*new_branch) {
-               unsigned char sha1[20];
-               int flag;
-               char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
-               if (head_ref &&
-                   (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
-                   !opts->ignore_other_worktrees)
-                       check_linked_checkouts(new);
-               free(head_ref);
-       }
-
        new->commit = lookup_commit_reference_gently(rev, 1);
        if (!new->commit) {
                /* not a commit */
@@ -1296,6 +1284,17 @@ static int checkout_branch(struct checkout_opts *opts,
                die(_("Cannot switch branch to a non-commit '%s'"),
                    new->name);
 
+       if (new->path && !opts->force_detach && !opts->new_branch) {
+               unsigned char sha1[20];
+               int flag;
+               char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
+               if (head_ref &&
+                   (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
+                   !opts->ignore_other_worktrees)
+                       check_linked_checkouts(new);
+               free(head_ref);
+       }
+
        if (opts->new_worktree)
                return prepare_linked_checkout(opts, new);
 
index f8e4df4818523de74ff7330f015415bfb9cc8227..a8d93366f6bff23ec8edab5227f4931215a6dd3d 100755 (executable)
@@ -28,6 +28,14 @@ test_expect_success 'checkout --to refuses to checkout locked branch' '
        ! test -d .git/worktrees/zere
 '
 
+test_expect_success 'checking out paths not complaining about linked checkouts' '
+       (
+       cd existing_empty &&
+       echo dirty >>init.t &&
+       git checkout master -- init.t
+       )
+'
+
 test_expect_success 'checkout --to a new worktree' '
        git rev-parse HEAD >expect &&
        git checkout --detach --to here master &&