threads / patch / 43903

patchcheckout: swap the order of ambiguity check for :/ syntax

Subject: [PATCH] checkout: swap the order of ambiguity check for :/ syntax

## tl;dr

12 messages between Aug 22, 2016 and Sep 9, 2016. Diffs are folded; open one to read it.

replies: 11people: 2as markdown or json

Nguyễn Thái Ngọc Duy· Aug 22, 2016, 12:35 UTC · lore

This should speed up "git checkout :/long/path/taken/from/diffstat" because we don't have to walk through the commit graph to see if "long/path/taken/from/diffstat" exists in any commit message.

This is in the the same spirit as 4db86e8 (Update :/abc ambiguity check
- 2013-01-21), but instead of considering this case ("abc" in ":/abc"
exists on worktree) ambiguous and shouting, we just assume the user
means "pathspec" not "ref".

It's not wonderful, but it's in line with how git-checkout stops caring about ambiguity after the first argument can be resolved as a ref (there's even a test for it, t2010.6). In other words, we assume the user means "ref" not "pathspec" when that happens. Here we simply swap the order of checking specifically for :/ on practical ground.

Last note, to be pedantic, we should check if "abc" from ":/abc" exists as an _index_ entry, not on disk. But chances are they exist in both places anyway...

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/checkout.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Show changes to builtin/checkout.c +16 −0
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 8672d07..6f016db 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -973,6 +973,22 @@ static int parse_branchname_arg(int argc, const char **argv,
 	if (!strcmp(arg, "-"))
 		arg = "@{-1}";
 
+	if (dash_dash_pos < 0 && starts_with(arg, ":/") &&
+	    check_filename(opts->prefix, arg)) {
+		/*
+		 * Normally if the first argument is ambiguous, we
+		 * choose to believe the user specifies an extended
+		 * SHA-1 syntax unless it turns out not true, then we
+		 * see if it's a pathspec.
+		 *
+		 * :/ here is an exception because resolving :/abc may
+		 * involve walking through the entire commit
+		 * graph. Expensive and slow. If :/abc points to an
+		 * existing file, ignore ambiguity and go with
+		 * pathspec (i.e. skip get_sha1_mb()).
+		 */
+		return 0;	/* case (2) */
+	}
 	if (get_sha1_mb(arg, rev)) {
 		/*
 		 * Either case (3) or (4), with <something> not being
-- 
2.8.2.524.g6ff3d78
Junio C Hamano· Aug 24, 2016, 16:35 UTC · re: Nguyễn Thái Ngọc Duy · lore

Re: [PATCH] checkout: swap the order of ambiguity check for :/ syntax

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> It's not wonderful, but it's in line with how git-checkout stops caring
> about ambiguity after the first argument can be resolved as a ref
> (there's even a test for it, t2010.6).

But that is justifiable because checkout can only ever take one revision. What follows, if there are any, must be paths, and more importantly, it would be perfectly reasonable if some of them were missing in the working tree ("ow, I accidentally removed that file, I need to resurrect it from the index"). Does the same justification apply to this change?

Duy Nguyen· Aug 25, 2016, 09:21 UTC · re: Junio C Hamano · lore

Re: [PATCH] checkout: swap the order of ambiguity check for :/ syntax

On Wed, Aug 24, 2016 at 11:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 12 quoted lines
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>
>> It's not wonderful, but it's in line with how git-checkout stops caring
>> about ambiguity after the first argument can be resolved as a ref
>> (there's even a test for it, t2010.6).
>
> But that is justifiable because checkout can only ever take one
> revision.  What follows, if there are any, must be paths, and more
> importantly, it would be perfectly reasonable if some of them were
> missing in the working tree ("ow, I accidentally removed that file,
> I need to resurrect it from the index").  Does the same justification
> apply to this change?

I think there is a misunderstanding. My "after" is in "after the first argument can be resolved, check if it exists in worktree too, if so it's ambiguous and bail". This is usually how we detect ambiguation. But git-checkout does not do the "check if it exists..." clause.

-- 
Duy
Junio C Hamano· Aug 25, 2016, 17:19 UTC · re: Duy Nguyen · lore

Re: [PATCH] checkout: swap the order of ambiguity check for :/ syntax

Duy Nguyen <pclouds@gmail.com> writes:
Show 18 quoted lines
> On Wed, Aug 24, 2016 at 11:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>>
>>> It's not wonderful, but it's in line with how git-checkout stops caring
>>> about ambiguity after the first argument can be resolved as a ref
>>> (there's even a test for it, t2010.6).
>>
>> But that is justifiable because checkout can only ever take one
>> revision.  What follows, if there are any, must be paths, and more
>> importantly, it would be perfectly reasonable if some of them were
>> missing in the working tree ("ow, I accidentally removed that file,
>> I need to resurrect it from the index").  Does the same justification
>> apply to this change?
>
> I think there is a misunderstanding. My "after" is in "after the first
> argument can be resolved, check if it exists in worktree too, if so
> it's ambiguous and bail". This is usually how we detect ambiguation.
> But git-checkout does not do the "check if it exists..." clause.
Hmph.  The "case 4" in the function you touched says
         * case 4: git checkout <something> <paths>
         *
         *   The first argument must not be ambiguous.
         *   - If it's *only* a reference, treat it like case (1).
         *   - If it's only a path, treat it like case (2).
         *   - else: fail.
Did we break it recently?
Duy Nguyen· Aug 26, 2016, 13:34 UTC · re: Junio C Hamano · lore

Re: [PATCH] checkout: swap the order of ambiguity check for :/ syntax

On Fri, Aug 26, 2016 at 12:19 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 31 quoted lines
> Duy Nguyen <pclouds@gmail.com> writes:
>
>> On Wed, Aug 24, 2016 at 11:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>>>
>>>> It's not wonderful, but it's in line with how git-checkout stops caring
>>>> about ambiguity after the first argument can be resolved as a ref
>>>> (there's even a test for it, t2010.6).
>>>
>>> But that is justifiable because checkout can only ever take one
>>> revision.  What follows, if there are any, must be paths, and more
>>> importantly, it would be perfectly reasonable if some of them were
>>> missing in the working tree ("ow, I accidentally removed that file,
>>> I need to resurrect it from the index").  Does the same justification
>>> apply to this change?
>>
>> I think there is a misunderstanding. My "after" is in "after the first
>> argument can be resolved, check if it exists in worktree too, if so
>> it's ambiguous and bail". This is usually how we detect ambiguation.
>> But git-checkout does not do the "check if it exists..." clause.
>
> Hmph.  The "case 4" in the function you touched says
>
>          * case 4: git checkout <something> <paths>
>          *
>          *   The first argument must not be ambiguous.
>          *   - If it's *only* a reference, treat it like case (1).
>          *   - If it's only a path, treat it like case (2).
>          *   - else: fail.
>
> Did we break it recently?

I was driven by testing and did not read the code carefully (but on the other hand, ambiguation plus dwim is never a pleasant read). Near the end of this function we have

        if (!has_dash_dash) {/* case (3).(d) -> (1) */
                /*
                 * Do not complain the most common case
                 *      git checkout branch
                 * even if there happen to be a file called 'branch';
                 * it would be extremely annoying.
                 */
                if (argc)
                        verify_non_filename(NULL, arg);

argc == 0 would be case 3d, argc != 0 is case 4 and "touch abc; git checkout :/abc def" does complain about :/abc being ambiguous. So no we haven't broken anything yet, but I do with my patch. If I want to make a special case for :/ to speed it up, I need to be careful about this (and I probably need a test too because it's not detected).

While at there, verify_non_filename() in this function (and check_filename() too) should pass the right prefix in argument 1, not NULL, I think. I'll need to do some digging to see why they are NULL in the first place though.

-- 
Duy
Nguyễn Thái Ngọc Duy· Sep 7, 2016, 11:19 UTC · re: Nguyễn Thái Ngọc Duy · lore

[PATCH 0/3] fix checkout ambiguation in subdir

I need some more time (which I don't have) to convince myself about the "git checkout :/abc" patch. But these look like good bug fix/improvement.

Nguyễn Thái Ngọc Duy (3):
  checkout: add some spaces between code and comment
  checkout.txt: document a common case that ignores ambiguation rules
  checkout: fix ambiguity check in subdir
 Documentation/git-checkout.txt |  9 +++++++++
 builtin/checkout.c             |  6 +++---
 t/t2010-checkout-ambiguous.sh  |  9 +++++++++
 t/t2024-checkout-dwim.sh       | 12 ++++++++++++
 4 files changed, 33 insertions(+), 3 deletions(-)
-- 
2.8.2.524.g6ff3d78
Nguyễn Thái Ngọc Duy· Sep 7, 2016, 11:19 UTC · re: Nguyễn Thái Ngọc Duy · lore

[PATCH 1/3] checkout: add some spaces between code and comment

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/checkout.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to builtin/checkout.c +1 −1
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 8672d07..1f71d06 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1038,7 +1038,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 
 	if (!*source_tree)                   /* case (1): want a tree */
 		die(_("reference is not a tree: %s"), arg);
-	if (!has_dash_dash) {/* case (3).(d) -> (1) */
+	if (!has_dash_dash) {	/* case (3).(d) -> (1) */
 		/*
 		 * Do not complain the most common case
 		 *	git checkout branch
-- 
2.8.2.524.g6ff3d78
Nguyễn Thái Ngọc Duy· Sep 7, 2016, 11:19 UTC · re: Nguyễn Thái Ngọc Duy · lore

[PATCH 2/3] checkout.txt: document a common case that ignores ambiguation rules

Normally we err on the safe side: if something can be seen as both an SHA1 and a pathspec, we stop and scream. In checkout, there is one exception added in 859fdab (git-checkout: improve error messages, detect ambiguities. - 2008-07-23), to allow the common case "git checkout branch". Let's document this exception.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-checkout.txt | 9 +++++++++
 1 file changed, 9 insertions(+)
Show changes to Documentation/git-checkout.txt +9 −0
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index 7a2201b..94eb238 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -419,6 +419,15 @@ $ git reflog -2 HEAD # or
 $ git log -g -2 HEAD
 ------------
 
+ARGUMENT AMBIGUATION
+--------------------
+
+When there is only one argument given and it is not `--` (e.g. "git
+checkout abc"), "abc" could be seen as either a `<tree-ish>` or a
+`<pathspec>`, but Git will assume the argument is a `<tree-ish>`, which is
+a common case for switching branches. Use `git checkout -- <pathspec>`
+form if you mean it to be a pathspec.
+
 EXAMPLES
 --------
 
-- 
2.8.2.524.g6ff3d78
Junio C Hamano· Sep 8, 2016, 20:03 UTC · re: Nguyễn Thái Ngọc Duy · lore

Re: [PATCH 2/3] checkout.txt: document a common case that ignores ambiguation rules

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 5 quoted lines
> Normally we err on the safe side: if something can be seen as both an
> SHA1 and a pathspec, we stop and scream. In checkout, there is one
> exception added in 859fdab (git-checkout: improve error messages, detect
> ambiguities. - 2008-07-23), to allow the common case "git checkout
> branch". Let's document this exception.
Good idea, but...
Show 8 quoted lines
> +ARGUMENT AMBIGUATION
> +--------------------
> +
> +When there is only one argument given and it is not `--` (e.g. "git
> +checkout abc"), "abc" could be seen as either a `<tree-ish>` or a
> +`<pathspec>`, but Git will assume the argument is a `<tree-ish>`, which is
> +a common case for switching branches. Use `git checkout -- <pathspec>`
> +form if you mean it to be a pathspec.

... this is far from reasonable. I'd read "but Git will assume the argument is a tree-ish" to mean "git checkout Makefile" would attempt to checkout the Makefile branch and fail.

    When there is only one argument given and it is not `--` (e.g. "git
    checkout abc"), and when the argument is both a valid `<tree-ish>`
    (e.g. a branch "abc" exists) and a valid `<pathspec>` (e.g. a file
    or a directory whose name is "abc" exists), Git would usually ask
    you to disambiguate.  Because checking out a branch is so common an
    operation, however, "git checkout abc" takes "abc" as a `<tree-ish>`
    in such situation.  Use `git checkout -- <pathspec>` if you want to
    checkout these paths out of the index.
or something like that?
Junio C Hamano· Sep 9, 2016, 19:21 UTC · re: Junio C Hamano · lore

Re: [PATCH 2/3] checkout.txt: document a common case that ignores ambiguation rules

I'll queue the following as "fixup!" for now. It reminds me that the title also needs rewording; we do not have rules to make things ambiguous ;-)

Show changes to Documentation/git-checkout.txt +9 −5
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index 46b04b1..8e2c066 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -419,14 +419,17 @@ $ git reflog -2 HEAD # or
 $ git log -g -2 HEAD
 ------------
 
-ARGUMENT AMBIGUATION
---------------------
+ARGUMENT DISAMBIGUATION
+-----------------------
 
 When there is only one argument given and it is not `--` (e.g. "git
-checkout abc"), "abc" could be seen as either a `<tree-ish>` or a
-`<pathspec>`, but Git will assume the argument is a `<tree-ish>`, which is
-a common case for switching branches. Use `git checkout -- <pathspec>`
-form if you mean it to be a pathspec.
+checkout abc"), and when the argument is both a valid `<tree-ish>`
+(e.g. a branch "abc" exists) and a valid `<pathspec>` (e.g. a file
+or a directory whose name is "abc" exists), Git would usually ask
+you to disambiguate.  Because checking out a branch is so common an
+operation, however, "git checkout abc" takes "abc" as a `<tree-ish>`
+in such a situation.  Use `git checkout -- <pathspec>` if you want
+to checkout these paths out of the index.
 
 EXAMPLES
 --------
-- 
2.10.0-339-gc0c747f
Nguyễn Thái Ngọc Duy· Sep 7, 2016, 11:19 UTC · re: Nguyễn Thái Ngọc Duy · lore

[PATCH 3/3] checkout: fix ambiguity check in subdir

The two functions in parse_branchname_arg(), verify_non_filename and check_filename, need correct prefix in order to reconstruct the paths and check for their existence. With NULL prefix, they just check paths at top dir instead.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/checkout.c            |  4 ++--
 t/t2010-checkout-ambiguous.sh |  9 +++++++++
 t/t2024-checkout-dwim.sh      | 12 ++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
Show changes to 3 files +23 −2

builtin/checkout.c, t/t2010-checkout-ambiguous.sh, t/t2024-checkout-dwim.sh

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 1f71d06..53c7284 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -985,7 +985,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 		int recover_with_dwim = dwim_new_local_branch_ok;
 
 		if (!has_dash_dash &&
-		    (check_filename(NULL, arg) || !no_wildcard(arg)))
+		    (check_filename(opts->prefix, arg) || !no_wildcard(arg)))
 			recover_with_dwim = 0;
 		/*
 		 * Accept "git checkout foo" and "git checkout foo --"
@@ -1046,7 +1046,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 		 * it would be extremely annoying.
 		 */
 		if (argc)
-			verify_non_filename(NULL, arg);
+			verify_non_filename(opts->prefix, arg);
 	} else {
 		argcount++;
 		argv++;
diff --git a/t/t2010-checkout-ambiguous.sh b/t/t2010-checkout-ambiguous.sh
index e76e84a..2e47fe0 100755
--- a/t/t2010-checkout-ambiguous.sh
+++ b/t/t2010-checkout-ambiguous.sh
@@ -41,6 +41,15 @@ test_expect_success 'check ambiguity' '
 	test_must_fail git checkout world all
 '
 
+test_expect_success 'check ambiguity in subdir' '
+	mkdir sub &&
+	# not ambiguous because sub/world does not exist
+	git -C sub checkout world ../all &&
+	echo hello >sub/world &&
+	# ambiguous because sub/world does exist
+	test_must_fail git -C sub checkout world ../all
+'
+
 test_expect_success 'disambiguate checking out from a tree-ish' '
 	echo bye > world &&
 	git checkout world -- world &&
diff --git a/t/t2024-checkout-dwim.sh b/t/t2024-checkout-dwim.sh
index 468a000..3e5ac81 100755
--- a/t/t2024-checkout-dwim.sh
+++ b/t/t2024-checkout-dwim.sh
@@ -174,6 +174,18 @@ test_expect_success 'checkout of branch with a file having the same name fails'
 	test_branch master
 '
 
+test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
+	git checkout -B master &&
+	test_might_fail git branch -D spam &&
+
+	>spam &&
+	mkdir sub &&
+	mv spam sub/spam &&
+	test_must_fail git -C sub checkout spam &&
+	test_must_fail git rev-parse --verify refs/heads/spam &&
+	test_branch master
+'
+
 test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
 	git checkout -B master &&
 	test_might_fail git branch -D spam &&
-- 
2.8.2.524.g6ff3d78
Junio C Hamano· Sep 8, 2016, 20:04 UTC · re: Nguyễn Thái Ngọc Duy · lore

Re: [PATCH 3/3] checkout: fix ambiguity check in subdir

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> The two functions in parse_branchname_arg(), verify_non_filename and
> check_filename, need correct prefix in order to reconstruct the paths
> and check for their existence. With NULL prefix, they just check paths
> at top dir instead.
Good eyes.  Will queue.
Show 73 quoted lines
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  builtin/checkout.c            |  4 ++--
>  t/t2010-checkout-ambiguous.sh |  9 +++++++++
>  t/t2024-checkout-dwim.sh      | 12 ++++++++++++
>  3 files changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 1f71d06..53c7284 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -985,7 +985,7 @@ static int parse_branchname_arg(int argc, const char **argv,
>  		int recover_with_dwim = dwim_new_local_branch_ok;
>  
>  		if (!has_dash_dash &&
> -		    (check_filename(NULL, arg) || !no_wildcard(arg)))
> +		    (check_filename(opts->prefix, arg) || !no_wildcard(arg)))
>  			recover_with_dwim = 0;
>  		/*
>  		 * Accept "git checkout foo" and "git checkout foo --"
> @@ -1046,7 +1046,7 @@ static int parse_branchname_arg(int argc, const char **argv,
>  		 * it would be extremely annoying.
>  		 */
>  		if (argc)
> -			verify_non_filename(NULL, arg);
> +			verify_non_filename(opts->prefix, arg);
>  	} else {
>  		argcount++;
>  		argv++;
> diff --git a/t/t2010-checkout-ambiguous.sh b/t/t2010-checkout-ambiguous.sh
> index e76e84a..2e47fe0 100755
> --- a/t/t2010-checkout-ambiguous.sh
> +++ b/t/t2010-checkout-ambiguous.sh
> @@ -41,6 +41,15 @@ test_expect_success 'check ambiguity' '
>  	test_must_fail git checkout world all
>  '
>  
> +test_expect_success 'check ambiguity in subdir' '
> +	mkdir sub &&
> +	# not ambiguous because sub/world does not exist
> +	git -C sub checkout world ../all &&
> +	echo hello >sub/world &&
> +	# ambiguous because sub/world does exist
> +	test_must_fail git -C sub checkout world ../all
> +'
> +
>  test_expect_success 'disambiguate checking out from a tree-ish' '
>  	echo bye > world &&
>  	git checkout world -- world &&
> diff --git a/t/t2024-checkout-dwim.sh b/t/t2024-checkout-dwim.sh
> index 468a000..3e5ac81 100755
> --- a/t/t2024-checkout-dwim.sh
> +++ b/t/t2024-checkout-dwim.sh
> @@ -174,6 +174,18 @@ test_expect_success 'checkout of branch with a file having the same name fails'
>  	test_branch master
>  '
>  
> +test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
> +	git checkout -B master &&
> +	test_might_fail git branch -D spam &&
> +
> +	>spam &&
> +	mkdir sub &&
> +	mv spam sub/spam &&
> +	test_must_fail git -C sub checkout spam &&
> +	test_must_fail git rev-parse --verify refs/heads/spam &&
> +	test_branch master
> +'
> +
>  test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
>  	git checkout -B master &&
>  	test_might_fail git branch -D spam &&

← back to recent threads