Skip to content

Commit e1c1ab9

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 562bc08 commit e1c1ab9

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

builtin/checkout.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,6 @@ static int parse_branchname_arg(int argc, const char **argv,
10851085
{
10861086
struct tree **source_tree = &opts->source_tree;
10871087
const char **new_branch = &opts->new_branch;
1088-
int force_detach = opts->force_detach;
10891088
int argcount = 0;
10901089
unsigned char branch_rev[20];
10911090
const char *arg;
@@ -1206,17 +1205,6 @@ static int parse_branchname_arg(int argc, const char **argv,
12061205
else
12071206
new->path = NULL; /* not an existing branch */
12081207

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-
12201208
new->commit = lookup_commit_reference_gently(rev, 1);
12211209
if (!new->commit) {
12221210
/* not a commit */
@@ -1296,6 +1284,17 @@ static int checkout_branch(struct checkout_opts *opts,
12961284
die(_("Cannot switch branch to a non-commit '%s'"),
12971285
new->name);
12981286

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+
12991298
if (opts->new_worktree)
13001299
return prepare_linked_checkout(opts, new);
13011300

t/t2025-checkout-to.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ test_expect_success 'checkout --to refuses to checkout locked branch' '
2828
! test -d .git/worktrees/zere
2929
'
3030

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+
3139
test_expect_success 'checkout --to a new worktree' '
3240
git rev-parse HEAD >expect &&
3341
git checkout --detach --to here master &&

0 commit comments

Comments
 (0)