threads / patch / 39472

patchcheckout: don't check worktrees when not necessary

Subject: [PATCH] checkout: don't check worktrees when not necessary

## tl;dr

5 messages between May 31, 2015 and Jun 12, 2015. Diffs are folded; open one to read it.

replies: 4people: 4as markdown or json

Spencer Baugh· May 31, 2015, 23:16 UTC · lore

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.

Signed-off-by: Spencer Baugh <sbaugh@catern.com>
---
 builtin/checkout.c | 1 +
 1 file changed, 1 insertion(+)
Show changes to builtin/checkout.c +1 −0
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2f92328..7039c5c 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1237,6 +1237,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 		char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
 		if (head_ref &&
 		    (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
+		    !(opts->patch_mode || opts->pathspec.nr) &&
 		    !opts->ignore_other_worktrees)
 			check_linked_checkouts(new);
 		free(head_ref);
-- 
2.4.2.339.g77bd3ea
Duy Nguyen· Jun 1, 2015, 10:23 UTC · re: Spencer Baugh · lore

Re: [PATCH] checkout: don't check worktrees when not necessary

On Sun, May 31, 2015 at 07:16:29PM -0400, Spencer Baugh wrote:
Show 10 quoted lines
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -1237,6 +1237,7 @@ static int parse_branchname_arg(int argc, const char **argv,
>  		char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
>  		if (head_ref &&
>  		    (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
> +		    !(opts->patch_mode || opts->pathspec.nr) &&
>  		    !opts->ignore_other_worktrees)
>  			check_linked_checkouts(new);
>  		free(head_ref);

Simple and effective. But if in future we add more options for non-switching-branch checkout, we need to update both places, here and near the end of cmd_checkout().

Perhaps we can move all this block inside checkout_branch() so we only need to test "opts->patch_mode || opts->pathspec.nr" once, at the end of cmd_checkout(). Something like below?

I'm not opposed to your change, but if you go with it, you should cherry pick my test in the below patch. Or create a similar test.

-- 8< --
Show changes to 2 files +19 −14

builtin/checkout.c, t/t2025-checkout-to.sh

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2f92328..e9aee58 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1110,7 +1110,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;
@@ -1231,17 +1230,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 */
@@ -1321,6 +1309,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);
 
diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
index f8e4df4..a8d9336 100755
--- a/t/t2025-checkout-to.sh
+++ b/t/t2025-checkout-to.sh
@@ -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 &&
-- 8< --
--
Duy
Spencer Baugh· Jun 10, 2015, 19:21 UTC · re: Duy Nguyen · lore

Re: [PATCH] checkout: don't check worktrees when not necessary

Duy Nguyen <pclouds@gmail.com> writes:
Show 22 quoted lines
> On Sun, May 31, 2015 at 07:16:29PM -0400, Spencer Baugh wrote:
>> --- a/builtin/checkout.c
>> +++ b/builtin/checkout.c
>> @@ -1237,6 +1237,7 @@ static int parse_branchname_arg(int argc, const char **argv,
>>  		char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
>>  		if (head_ref &&
>>  		    (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
>> +		    !(opts->patch_mode || opts->pathspec.nr) &&
>>  		    !opts->ignore_other_worktrees)
>>  			check_linked_checkouts(new);
>>  		free(head_ref);
>
> Simple and effective. But if in future we add more options for
> non-switching-branch checkout, we need to update both places, here and
> near the end of cmd_checkout().
>
> Perhaps we can move all this block inside checkout_branch() so we only
> need to test "opts->patch_mode || opts->pathspec.nr" once, at the end
> of cmd_checkout(). Something like below?
>
> I'm not opposed to your change, but if you go with it, you should
> cherry pick my test in the below patch. Or create a similar test.

Sorry for late reply, but I think your change is much better than mine so I'd suggest just using that instead.

Nguyễn Thái Ngọc Duy· Jun 12, 2015, 10:49 UTC · re: Spencer Baugh · lore

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>
---
 builtin/checkout.c     | 23 +++++++++++------------
 t/t2025-checkout-to.sh |  8 ++++++++
 2 files changed, 19 insertions(+), 12 deletions(-)
Show changes to 2 files +19 −12

builtin/checkout.c, t/t2025-checkout-to.sh

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 9b49f0e..e227f64 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1110,7 +1110,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;
@@ -1231,17 +1230,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 */
@@ -1321,6 +1309,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);
 
diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
index f8e4df4..a8d9336 100755
--- a/t/t2025-checkout-to.sh
+++ b/t/t2025-checkout-to.sh
@@ -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 &&
-- 
2.3.0.rc1.137.g477eb31
Junio C Hamano· Jun 12, 2015, 22:24 UTC · re: Nguyễn Thái Ngọc Duy · lore

Re: [PATCH] checkout: don't check worktrees when not necessary

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> 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.

Yeah, I agree that having this check in parse_branchname_arg() does not make any sense, as that function is still trying to decide if we are switching to a different branch or not. The new location looks much more sensible.

Will queue this on nd/multiple-work-trees (which has been in 'master' for about a month). Good thing that we caught it before it got in any tagged release.

Thanks.
Show 75 quoted lines
>
> Original-patch-by: Spencer Baugh <sbaugh@catern.com>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  builtin/checkout.c     | 23 +++++++++++------------
>  t/t2025-checkout-to.sh |  8 ++++++++
>  2 files changed, 19 insertions(+), 12 deletions(-)
>
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 9b49f0e..e227f64 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -1110,7 +1110,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;
> @@ -1231,17 +1230,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 */
> @@ -1321,6 +1309,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);
>  
> diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
> index f8e4df4..a8d9336 100755
> --- a/t/t2025-checkout-to.sh
> +++ b/t/t2025-checkout-to.sh
> @@ -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 &&

← back to recent threads