checkout: don't check worktrees when not necessary

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>

Nguyễn Thái Ngọc Duy committed Jun 12, 2015 at 17:49 UTC e1c1ab9d25864e8de0009f3f3149ec4386a14bf8
2 files changed +19 -12
builtin/checkout.c
+11 -12
@@ -1085,7 +1085,6 @@ static int parse_branchname_arg(int argc, const char **argv,
1085 {
1086 struct tree **source_tree = &opts->source_tree;
1087 const char **new_branch = &opts->new_branch;
1088 - int force_detach = opts->force_detach;
1088 int argcount = 0;
1089 unsigned char branch_rev[20];
1090 const char *arg;
@@ -1206,17 +1205,6 @@ static int parse_branchname_arg(int argc, const char **argv,
1205 else
1206 new->path = NULL; /* not an existing branch */
1207
1209 - if (new->path && !force_detach && !*new_branch) {
1210 - unsigned char sha1[20];
1211 - int flag;
1212 - char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
1213 - if (head_ref &&
1214 - (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
1215 - !opts->ignore_other_worktrees)
1216 - check_linked_checkouts(new);
1217 - free(head_ref);
1218 - }
1219 -
1208 new->commit = lookup_commit_reference_gently(rev, 1);
1209 if (!new->commit) {
1210 /* not a commit */
@@ -1296,6 +1284,17 @@ static int checkout_branch(struct checkout_opts *opts,
1284 die(_("Cannot switch branch to a non-commit '%s'"),
1285 new->name);
1286
1287 + if (new->path && !opts->force_detach && !opts->new_branch) {
1288 + unsigned char sha1[20];
1289 + int flag;
1290 + char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
1291 + if (head_ref &&
1292 + (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
1293 + !opts->ignore_other_worktrees)
1294 + check_linked_checkouts(new);
1295 + free(head_ref);
1296 + }
1297 +
1298 if (opts->new_worktree)
1299 return prepare_linked_checkout(opts, new);
1300
t/t2025-checkout-to.sh
+8
@@ -28,6 +28,14 @@ test_expect_success 'checkout --to refuses to checkout locked branch' '
28 ! test -d .git/worktrees/zere
29 '
30
31 +test_expect_success 'checking out paths not complaining about linked checkouts' '
32 + (
33 + cd existing_empty &&
34 + echo dirty >>init.t &&
35 + git checkout master -- init.t
36 + )
37 +'
38 +
39 test_expect_success 'checkout --to a new worktree' '
40 git rev-parse HEAD >expect &&
41 git checkout --detach --to here master &&