Pathspec does not accept / does not warn for specific pathspecs
Hello there,
as I was trying to use git-archive to ... well, archive some artifacts. So I called:
git archive HEAD -o p.zip --prefix=p/
Then, I noticed that .gitignore files from the repo were packed (and a couple of others, but we don't care about that right now). I wanted to exclude all .gitignore files (there is /.gitignore and /resources/.gitignore). So I did:
git archive HEAD -o p.zip --prefix=p/ -- ':!.gitignore'
I noticed that the second-level .gitignore file was still included. I started thinking that this is not a .gitignore pathspec, but it matches a literal file.
I don't know why at-the-time I accepted that, since, if I write .ignoreFile at a .gitignore, it would exclude any .ignoreFile at any level. So I did:
git archive HEAD -o p.zip --prefix=p/ -- ':!/.gitignore'
Which, as expected, did not change anything; and I followed up with:
git archive HEAD -o p.zip --prefix=p/ -- ':!**/.gitignore'
Then, I noticed some weird behavior. The second-level .gitignore was getting ignored, but not the first one!
Someone at the chat indicated that, the ** was _magically_ interpreted as *, which is obvious to me why it won't help. So, they suggested:
git archive HEAD -o p.zip --prefix=p/ -- ':!(glob)**/.gitignore'
Which lead to **both of the .gitignores** to be included. And then I was baffled.
Now I know (I guess) that the pathspec magic was :!, and I tried to match against a (glob)**/.gitignore, i.e. (glob)*/.gitignore, which, of course, it doesn't exist in any form.
And I wonder:
Why doesn't git archive ... -- understand :!.gitignore as a .gitignore file would do (minus the :)?
Why doesn't git archive ... -- understand :!**/.gitignore as a .gitignore file would do (minus the :)?
Is there some reason that git archive ... -- doesn't understand :!(glob)**/.gitignore? It doesn't sound awfully complicated, or risking a lot of regression (a folder starting with (, and containing valid pathspec long forms IMHO is rare), or
Is there some reason that git archive ... -- doesn't warn me that :!(glob)**/.gitignore is invalid (and maybe I meant (exclude,glob))?
With regards,
Ntentos Stavros
Quoted 7 lines · Σταύρος Ντέντος
git archive HEAD -o p.zip --prefix=p/ -- ':!(glob)**/.gitignore'
Which lead to **both of the .gitignores** to be included. And then I was baffled.
Now I know (I guess) that the pathspec magic was:!, and I tried to match against a(glob)**/.gitignore, i.e.(glob)*/.gitignore, which, of course, it doesn't exist in any form.
You correctly guessed.
The section on pathspec in "git help glossary" may need to be clarified so that ":!(exclude,glob)" is the right way to spell what you meant, I think.
Quoted 3 lines
And I wonder:
Why doesn'tgit archive ... --understand:!.gitignoreas a .gitignore file would do (minus the:)?
At this point, asking "why" is fruitless. They are spelled differently, because pathspecs and .gitignore patterns are simply different.
Quoted 4 lines
Is there some reason that
git archive ... --doesn't understand:!(glob)**/.gitignore? It doesn't sound awfully complicated, or risking a lot of regression (a folder starting with(, and containing valid pathspec long forms IMHO is rare),
It was probably OK back in 2006 when the number of users and projects that are using Git were smaller, but these days, whatever anybody would think "rare" are used by somebody and it would hurt many people (in absolute terms---they may be a tiny minority of the population) if you make a backward incompatible change that breaks these "rare" cases.
Quoted 3 lines
Is there some reason that
git archive ... --doesn't warn me that:!(glob)**/.gitignoreis invalid (and maybe I meant(exclude,glob))?
This one does have merit. Patches are very much welcomed.
Thanks.
cover pathspec: warn for a no-glob entry that contains `**`
From: Stavros Ntentos <[email protected]>
This is my first contribution to the git project.
It is the first of the second takes that came from the
https://lore.kernel.org/git/[email protected]/ e-mail (hopefully I linked it correctly using the git-magic commands).
For the review, I pulled in the latest _meaningful_ contributer, as well as one big contributer to the file.
hopefully, all is in order and correctly done.
Please be gentle with "protocol violations". :-D
Stavros Ntentos (1):
pathspec: warn for a no-glob entry that contains **
pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)
--
2.31.0
PATCH 1/1 pathspec: warn for a no-glob entry that contains `**`
From: Stavros Ntentos <[email protected]>
If a pathspec is given that contains **, chances are that someone is naively expecting that it will do what the manual has told him that ** will match (i.e. 0-or-more directories).
However, without an explicit :(glob) magic, that will fall out the sky:
the two ** will merge into one star, which surrounded by slashes, will match any directory name.
These changes attempt to bring awareness to this issue.
--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)
pathspec.c +13 -0
@@ -1,3 +1,4 @@ +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h"
@@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } + +void check_missing_glob (const char *entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\n\tIt will not match 0 or more directories!" )); + } +}
pathspec.h +1 -0
@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_missing_glob (const char * pathspec_entry,int flags);#endif /* PATHSPEC_H */
t/t6130-pathspec-noglob.sh +13 -0
@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual ' +cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no glob magic. +EOF +test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns +' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' ' + test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns +' + test_done
Signature · 1 line
2.31.0
Re: [PATCH v1 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 40 lines · Σταύρος Ντέντος
From: Stavros Ntentos <[email protected]>
If a pathspec is given that contains
**, chances are that someone is naively expecting that it will do what the manual has told him that**will match (i.e. 0-or-more directories).However, without an explicit
:(glob)magic, that will fall out the sky:
the two**will merge into one star, which surrounded by slashes, will match any directory name.These changes attempt to bring awareness to this issue.
Signed-off-by Stavros Ntentos <[email protected]>--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)pathspec.c +3
-0@@ -1,3 +1,4 @@ +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h" @@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ;
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 753
Quoted 10 lines
pathspec.c +9
-0} + +void check_missing_glob (const char *entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\n\tIt will not match 0 or more directories!" )); + }
Why did you add an extra \t? I think it is unnecessary indentation.
Quoted 19 lines
pathspec.c +1
-0+}pathspec.h +1
-0@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_missing_glob (const char * pathspec_entry,int flags);#endif /* PATHSPEC_H */ t/t6130-pathspec-noglob.sh +0
-0@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual '
t/t6130-pathspec-noglob.sh test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' · line 168
Quoted 10 lines
t/t6130-pathspec-noglob.sh +9
-0+cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no glob magic. +EOF +test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns +' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' '
Padding maybe?
Quoted 6 lines
t/t6130-pathspec-noglob.sh +4
-0+ test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns +' + test_done
Signature · 1 line
An old man doll... just what I always wanted! - Clara
Re: [PATCH v1 1/1] pathspec: warn for a no-glob entry that contains `**`
In reply to Σταύρος ΝτέντοςQuoted 40 lines · Σταύρος Ντέντος
From: Stavros Ntentos <[email protected]>
If a pathspec is given that contains
**, chances are that someone is naively expecting that it will do what the manual has told him that**will match (i.e. 0-or-more directories).However, without an explicit
:(glob)magic, that will fall out the sky:
the two**will merge into one star, which surrounded by slashes, will match any directory name.These changes attempt to bring awareness to this issue.
Signed-off-by Stavros Ntentos <[email protected]>--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)pathspec.c +3
-0@@ -1,3 +1,4 @@ +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h" @@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ;
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 753
Quoted 10 lines
pathspec.c +9
-0} + +void check_missing_glob (const char *entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\n\tIt will not match 0 or more directories!" )); + }
Why an extra \t? Unnecessary indentation?
Quoted 19 lines
pathspec.c +1
-0+}pathspec.h +1
-0@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_missing_glob (const char * pathspec_entry,int flags);#endif /* PATHSPEC_H */ t/t6130-pathspec-noglob.sh +0
-0@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual '
t/t6130-pathspec-noglob.sh test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' · line 168
Quoted 10 lines
t/t6130-pathspec-noglob.sh +9
-0+cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no glob magic. +EOF +test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns +' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' '
Padding with without test above?
Quoted 6 lines
t/t6130-pathspec-noglob.sh +4
-0+ test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns +' + test_done
Signature · 1 line
An old man doll... just what I always wanted! - Clara
PATCH 1/1 pathspec: warn for a no-glob entry that contains `**`
Quoted 4 lines
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 753
Quoted 3 lines
pathspec.c +3
-0+if (strstr (entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\n\tIt will not match 0 or more directories!" )); + }Why an extra \t? Unnecessary indentation?
It brings out the warning label:
root@30f6bde171fe:/usr/src/git/t# ../git stash -- **/bar warning: Pathspec provided contains `**`, but no :(glob) magic. It will not match 0 or more directories! error: pathspec ':(,prefix:2)t/**/bar' did not match any file(s) known to git Did you forget to 'git add'?
and "makes the user feel" that these two lines are in reality one (but this is way over any sensible line limit to present as such).
I would've padded exactly, but:
* \t expansion is terminal-specific (usually set at 8, but not guaranteed)
* ` `-only would've been too long of a padding to an already long line
Ofc, if someone really wanted to solve this, someone could rework the void vreportf split and auto-pad prefix at newline, but sounds like a project on its own ...
Quoted 2 lines
Quoted 1 line
+test_expect_success '** with :(literal) does not warn of lacking glob magic' '
Padding with without test above?
Yes; it somewhat aligns individual test output:
root@30f6bde171fe:/usr/src/git/t# ./t6130-pathspec-noglob.sh ... ok 22 - ** without :(glob) warns of lacking glob magic ok 23 - ** with :(literal) does not warn of lacking glob magic # passed all 23 test(s)
to emphasize they are a positive/negative test pair.
I don't know how well I feel about this anyway; I can undo it.
Re: [PATCH v1 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 3 lines · Stavros Ntentos
Ofc, if someone really wanted to solve this, someone could rework the
void vreportfsplit and auto-pad prefix at newline, but sounds like a project on its own ...
But when it happens, this extra "\t" would get in the way and needs to be adjusted ;-)
It has been on the radar for quite some time to apply the same principle for die(), error(), and warning() as how advise() shows its "hint:" prefix.
The format strings given to these functions are end-user-facing and subject to localization, so there is no huge compatibility issues we need to be worried about.
The test suite have fixed expectations and such a change must be accompanied by adjustment to the affected tests, though.
v2 cover pathspec: warn for a no-glob entry that contains `**`
In reply to Junio C HamanoFrom: Stavros Ntentos <[email protected]>
Small amends, here and there, and fixed/reverted the whitespaces
Stavros Ntentos (1):
pathspec: warn for a no-glob entry that contains **
pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)
--
2.31.0
PATCH v2 1/1 pathspec: warn for a no-glob entry that contains `**`
In reply to Junio C HamanoFrom: Stavros Ntentos <[email protected]>
If a pathspec given that contains **, chances are that someone is naively expecting that it will do what the manual has told him (i.e. that ** will match 0-or-more directories).
However, without an explicit :(glob) magic, that will fall out the sky:
the two ** will merge into one star, which surrounded by slashes, will match any directory name.
These changes attempt to bring awareness to this issue.
--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)
pathspec.c +13 -0
@@ -1,3 +1,4 @@ +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h"
@@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } + +void check_missing_glob (const char *pathspec_entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (pathspec_entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\nIt will not match 0 or more directories!" )); + } +}
pathspec.h +1 -0
@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_missing_glob (const char * pathspec_entry,int flags);#endif /* PATHSPEC_H */
t/t6130-pathspec-noglob.sh +13 -0
@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual ' +cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no :(glob) magic. +EOF +test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns +' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' ' + test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns +' + test_done
Signature · 1 line
2.31.0
Re: [PATCH v2 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 5 lines · Σταύρος Ντέντος
From: Stavros Ntentos <[email protected]>
If a pathspec given that contains
**, chances are that someone is naively expecting that it will do what the manual has told him (i.e. that**will match 0-or-more directories).
OK. this is a well written introduction of the problem being solved.
But chances are that the user may have meant to give double-asterisk just out of habit, very well knowing that it would not be an error to give ** when a single * suffices.
Which leads us to suspect that it is a bad change to make this a warning that unconditionally fires and cannot squelch. It may be better to implement it as an advice, where the user who knowingly uses that construct can say "yes, I know what I am doing" and squelch it.
Quoted 3 lines
However, without an explicit
:(glob)magic, that will fall out the sky:
the two**will merge into one star, which surrounded by slashes, will match any directory name.
I am not sure everything after the "the sky:" you wrote is what you meant to write. Without being marked with a "glob" magic, a wildcard character in a pattern matches even a slash, so these two
git ls-files 'Documentation**v2.txt'
git ls-files 'Documentation*v2.txt'
give the identical result and there is nothing about "surrounded by slashes" involved in it.
So, perhaps taking the first two paragraphs together and rewriting:
When '/**/' appears in the pathspec, the user may be expecting that it would be matched using the "wildmatch" semantics, matching 0 or more directories. But that is not what happens without ":(glob)" magic.
or did you want to warn about "foo**bar" as if it were "foo/**/bar"?
The output from these commands:
git ls-files ':(glob)Documentation/**/*stash.txt' git ls-files ':(glob)Documentation***stash.txt' git ls-files ':(glob)Documentation**stash.txt'
seems to tell me that the "zero-or-more directories" magic happens only when /**/ form is used, not when two asterisks are placed next to each other in a random context.
Quoted 1 line
These changes attempt to bring awareness to this issue.
In any case, after presenting what problem we are trying to address, we present our approach / solution in a form as if we are giving orders to the codebase to "become like so".
Teach the pathspec parser to emit an advice message when a substring "/**/" appears in a pathspec element that does not have a ":(glob)" magic. Make sure we don't disturb users who use ":(literal)" magic with such a substring, as it is clear they want to find these strings literally.
perhaps.
pathspec.c line 1
Quoted 13 lines
Signed-off-by Stavros Ntentos <[email protected]>--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)pathspec.c +1
-0@@ -1,3 +1,4 @@ +#include <string.h>
Never include any header file before including <git-compat-util.h>. Including <git-compat-util.h> indirectly by including <cache.h> or <builtin.h> counts as including it as the first thing.
As all necessary standard system headers are supposed to be given by including <git-compat-util.h>, if you needed to explicitly include the above, that may mean <git-compat-util.h>, which should cause the header that lists necessary function, is not working properly on your platform and needs to be adjusted. Are you on a minority platform we haven't adjusted the header inclusion order for, and what function are you trying to use that does not work without adding this extra #include here? We already use strstr() in many places, so that is not the function we are missing system includes for.
Or perhaps this is a remnant of what you added while you were experimenting, without realizing that "#include <cache.h>" that is already there already gives you anything you need. If that is the case, iow, if the code works without the extra include, please remove that line.
pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 593
Quoted 6 lines
pathspec.c +2
-0@@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +
We have only one caller of the helper (i.e. this one) and nowhere else, and the helper is small enough ...
Quoted 4 lines
pathspec.c +0
-0@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; }
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 754
Quoted 10 lines
pathspec.c +10
-0+ +void check_missing_glob (const char *pathspec_entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (pathspec_entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\nIt will not match 0 or more directories!" )); + } +}
... hence, there is no reason why this helper should exist, let alone be a public function. Also, there is no reason to split the conditional into two. Just "it is OK if we have GLOB or LITERAL, otherwise see if there is /**/" is sufficient.
It would be more sensible to just add the check to parse_pathspec() directly.
Also, our warning messages do not begin with an uppercase.
See attached patch for all of the above, but it is for illustration purposes only; not even compile tested. I am not illustrating how to turn this into an advice message that the user can squelch with the illustration patch.
t/t6130-pathspec-noglob.sh test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' · line 163
Quoted 12 lines
t/t6130-pathspec-noglob.sh +3
-0@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual ' +cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no :(glob) magic. +EOF
Please don't imitate ancient tests. These days, all preparations for individual tests, including the expected outcome, are to be done inside the test itself. Study nearby tests in the same script for better examples.
t/t6130-pathspec-noglob.sh test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' · line 166
Quoted 3 lines
t/t6130-pathspec-noglob.sh +3
-0+test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns
Same comment. Nearby examples all set up expeced output and never uses "grep" to see if the expectation is satisfied. Imitate them.
t/t6130-pathspec-noglob.sh test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' · line 171
Quoted 5 lines
t/t6130-pathspec-noglob.sh +5
-0+' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' ' + test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns
Ditto.
Thanks.
pathspec.c | 4 ++++ 1 file changed, 4 insertions(+)
pathspec.c +4 -0
@@ -598,6 +598,10 @@ void parse_pathspec(struct pathspec *pathspec,die (_ ("pathspec '%s' is beyond a symbolic link" ), entry); } +if (!(item[i].magic & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) && +strstr (entry,"**" )) +warning (_ ("** in '%s'without :(glob) magic:" ), entry); +if (item[i].nowildcard_len < item[i].len) pathspec->has_wildcard =1 ; pathspec->magic |= item[i].magic;
Re: [PATCH v2 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 7 lines · Junio C Hamano
So, perhaps taking the first two paragraphs together and rewriting:
When '/**/' appears in the pathspec, the user may be expecting that it would be matched using the "wildmatch" semantics, matching 0 or more directories. But that is not what happens without ":(glob)" magic.
Quoted 5 lines
Teach the pathspec parser to emit an advice message when a substring "/**/" appears in a pathspec element that does not have a ":(glob)" magic. Make sure we don't disturb users who use ":(literal)" magic with such a substring, as it is clear they want to find these strings literally.
I haven't been following the discussion, but is there a reason we need to penalize the user with a warning rather than helping, for instance by inferring ":(glob)" in the presence of /**/ if not otherwise countermanded by ":(literal)" or whatnot?
Re: [PATCH v2 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 4 lines · Eric Sunshine
I haven't been following the discussion, but is there a reason we need to penalize the user with a warning rather than helping, for instance by inferring ":(glob)" in the presence of
/**/if not otherwise countermanded by ":(literal)" or whatnot?
Two reasons I can think of offhand are
- How /**/ is interpreted is not the only thing that is different between the normal mode and the glob magic mode. IIRC, an asterisk * or a question mark ? matches slash in normal mode (it started out as fnmatch() without FNM_PATHNAME). Should we warn about ":(glob)" if somebody asks for "foo*", "*foo", or "foo*bar". If not, why shouldn't?
- Thers is no explicit magic that says "there is no magic" to countermand such a DWIM.
Re: [PATCH v2 1/1] pathspec: warn for a no-glob entry that contains `**`
Quoted 16 lines · Junio C Hamano
Quoted 4 lines · Eric Sunshine
I haven't been following the discussion, but is there a reason we need to penalize the user with a warning rather than helping, for instance by inferring ":(glob)" in the presence of
/**/if not otherwise countermanded by ":(literal)" or whatnot?Two reasons I can think of offhand are
- How /**/ is interpreted is not the only thing that is different between the normal mode and the glob magic mode. IIRC, an asterisk * or a question mark ? matches slash in normal mode (it started out as
fnmatch()withoutFNM_PATHNAME). Should we warn about ":(glob)" if somebody asks for "foo*", "*foo", or "foo*bar". If not, why shouldn't?
Quoted 2 lines
- Thers is no explicit magic that says "there is no magic" to countermand such a DWIM.
I do wonder if this distinction creates more harm than good.
As somebody who has never used ":(glob)" myself, I was confused about what it even does (and it was not easy to find the documentation; I ended up finding the original commit in the history first!).
We have three modes:
- no globbing
- globbing with fnmatch(), with FNM_PATHNAME according to the docs
- globbing with wildmatch
You may notice that I would call both of those latter two "globbing", but only one of them is triggered by the ":(glob)" magic. :)
This just seems really confusing, and I wonder if anybody would be that sad if we just used wildmatch everywhere. The original bd30c2e484 (pathspec: support :(glob) syntax, 2013-07-14) even says:
The old fnmatch behavior is considered deprecated and discouraged to use.
but I guess it would be backwards-incompatible.
Maybe it would be less confusing if we named the three states explicitly:
:(literal)
:(fnmatch)
:(wildmatch)
(and keeping :(glob) as a synonym for compatibility).
-Peff
Re: [PATCH v2 1/1] pathspec: warn for a no-glob entry that contains `**`
I think I have managed to address everyone's questions in this thread. Hopefully everything will be addressed by this, and the patch that will soon follow:
Quoted 2 lines
You may notice that I would call both of those latter two "globbing", but only one of them is triggered by the ":(glob)" magic. :)
And that's why I argued a DWIM was warranted here
(https://lore.kernel.org/git/[email protected]/; it's more clear in Junio's quote, but you could of course read the original).
I would want to be considered as an above-average git user, and I still was oblivious to the fact that ** required a :(glob) from the command line. Especially since .gitignore files are treated differently (i.e. don't require :(glob))
I cannot / don't want to argue to "do it" or not, as I think my experience is not substantial enough to navigate such argument, and go from concept to materialization.
That being said, if there was a cli.pathspec.wildmatch flag, I would've had it set to true in my global gitconfig. Ofc I could set GIT_GLOB_PATHSPECS=1, but I am not that happy to force it by setting it in a shell rc, instead of where it belongs.
Quoted 9 lines
I am not sure everything after the "the sky:" you wrote is what you meant to write. Without being marked with a "glob" magic, a wildcard character in a pattern matches even a slash, so these two
git ls-files 'Documentation**v2.txt'
git ls-files 'Documentation*v2.txt'give the identical result and there is nothing about "surrounded by slashes" involved in it.
It seems you are right - in fnmatch mode, :!*.gitignore would've served me right (and avoided the whole thread).
If you think that it's just my (i.e. few people's) lacking of understanding fnmatch glob, then we can drop this.
However, given the disparity between .gitignore syntax and pathspec given from the command line (from my limited POV meaningless/confusing), and your (plural) arguments of backwards compatibility, I think we can draw the line at an advice been acceptable.
Unless I see other points (like the warn vs advice), or pure C code-review points, I am getting the impression that this thread will not move forward. As I don't know how reviews usually happen here, and lacking some other medium of discussion, I would appreciate (at some point) an explicit go/no-go decision - to save everyone's time.
Being unfamiliar to the project, and being who I am, I prefer explicit vs implicit points. Navigating an unknown process from top to bottom is pressure enough to me :-D
Quoted 3 lines
seems to tell me that the "zero-or-more directories" magic happens only when /**/ form is used, not when two asterisks are placed next to each other in a random context.
Not exactly: ** needs to either start or end with a slash (or both) for its wildmatch behavior. I can try to make the code more explicit, but of course the code (and tests) will increase - and then maybe disproportionately to the otherwise intended-to-be small change (since DWIM is not wanted).
PATCH v3 1/2 pathspec: warn for a no-glob entry that contains `**`
In reply to Σταύρος ΝτέντοςIf a pathspec given that contains **, chances are that someone is naively expecting that it will do what the manual has told him (i.e. that ** will match 0-or-more directories).
When ** appears in the pathspec, the user may be expecting that it would be matched using the "wildmatch" semantics, matching 0 or more directories.
That is not what happens without ":(glob)" magic.
Teach the pathspec parser to emit an advice message when a substring ** appears in a pathspec element that does not have a :(glob) magic. Make sure we don't disturb users who use ":(literal)" magic
with such a substring, as it is clear they want to find these strings literally.
--- pathspec.c | 13 +++++++++++++ pathspec.h | 1 + t/t6130-pathspec-noglob.sh | 13 +++++++++++++ 3 files changed, 27 insertions(+)
pathspec.c +13 -0
@@ -1,3 +1,4 @@ +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h"
@@ -588,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,init_pathspec_item (item + i, flags, prefix, prefixlen, entry); +check_missing_glob (entry, item[i].magic); +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
@@ -739,3 +742,13 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } + +void check_missing_glob (const char *pathspec_entry,int flags) { +if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) { +return ; + } + +if (strstr (pathspec_entry,"**" )) { +warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\nIt will not match 0 or more directories!" )); + } +}
pathspec.h +1 -0
@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_missing_glob (const char * pathspec_entry,int flags);#endif /* PATHSPEC_H */
t/t6130-pathspec-noglob.sh +13 -0
@@ -157,4 +157,17 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' test_must_be_empty actual ' +cat > expected <<"EOF" +warning: Pathspec provided contains `**`, but no :(glob) magic. +EOF +test_expect_success '** without :(glob) warns of lacking glob magic' ' + test_might_fail git stash -- "**/bar" 2>warns && + grep -Ff expected warns +' + +test_expect_success '** with :(literal) does not warn of lacking glob magic' ' + test_might_fail git stash -- ":(literal)**/bar" 2>warns && + ! grep -Ff expected warns +' + test_done
Signature · 1 line
2.31.0
PATCH v3 2/2 pathspec: convert no-glob warn to advice
Teach git that, if anyone wants to squelch such warning, can do so via git config advice.starStarNoGlobPathspec false
--- Documentation/config/advice.txt | 4 ++++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 6 ++++-- t/t6130-pathspec-noglob.sh | 4 +++- 5 files changed, 16 insertions(+), 3 deletions(-)
Documentation/config/advice.txt +4 -0
@@ -119,4 +119,8 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + starStarNoGlobPathspec:: + Advice shown if a user provides a pathspec via the terminal + that contains `**`, anticipating that the pattern will be + matched using wildmatch (aka `:(glob)` magic). --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_star_star_no_glob_pathspec =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "starStarNoGlobPathspec", &advice_star_star_no_glob_pathspec },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_STAR_STAR_NO_GLOB_PATHSPEC] = { "starStarNoGlobPathspec", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_star_star_no_glob_pathspec;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_STAR_STAR_NO_GLOB_PATHSPEC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +4 -2
@@ -1,4 +1,4 @@ -#include <string.h> +#include "git-compat-util.h" #include "cache.h" #include "config.h" #include "dir.h"
@@ -744,11 +744,13 @@ int match_pathspec_attrs(const struct index_state *istate, }void check_missing_glob (const char *pathspec_entry,int flags) { +const char *advice =NULL ;if (flags & (PATHSPEC_GLOB |PATHSPEC_LITERAL )) {return ; } + advice =_ ("Pathspec provided contains `**`, but no :(glob) magic.\nIt will not match 0 or more directories!" );if (strstr (pathspec_entry,"**" )) { -warning (_ ("Pathspec provided contains `**`, but no :(glob) magic.\nIt will not match 0 or more directories!" )); +advise_if_enabled (ADVICE_STAR_STAR_NO_GLOB_PATHSPEC , advice); } }
t/t6130-pathspec-noglob.sh +3 -1
@@ -158,7 +158,9 @@ test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' ' ' cat > expected <<"EOF" -warning: Pathspec provided contains `**`, but no :(glob) magic. +hint: Pathspec provided contains `**`, but no :(glob) magic. +hint: It will not match 0 or more directories! +hint: Disable this message with "git config advice.starStarNoGlobPathspec false" EOF test_expect_success '** without :(glob) warns of lacking glob magic' ' test_might_fail git stash -- "**/bar" 2>warns &&
Signature · 1 line
2.31.0
cover pathspec: warn: long and short forms are incompatible
In reply to Junio C HamanoFrom: Stavros Ntentos <[email protected]>
The second of the two issues identified in this thread.
Both patches work as expected.
There is a short solution, which is mostly okay. It does not compile with -Werror, but:
It avoids malloc/free, and keeps the logic/sloc low.
All of this could be my lack of C experience.
Stavros Ntentos (1):
pathspec: warn: long and short forms are incompatible
pathspec.c | 30 ++++++++++++++++++++++++++++++ pathspec.h | 1 + t/t6132-pathspec-exclude.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+)
--
2.31.0
RFC PATCH 1/2 pathspec: warn: long and short forms are incompatible
From: Stavros Ntentos <[email protected]>
Namely, ! and any other long magic form (e.g. glob) cannot be combined to one entry.
Issue a warning when such thing happens, and hint to the solution.
--- pathspec.c | 19 +++++++++++++++++++ pathspec.h | 1 + t/t6132-pathspec-exclude.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+)
pathspec.c +19 -0
@@ -1,3 +1,5 @@ +#include <stdio.h> +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h"
@@ -586,6 +588,8 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; +check_mishandled_exclude (entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE )
@@ -739,3 +743,18 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } + +void check_mishandled_exclude (const char *entry) { +size_t entry_len =strlen (entry); +char flags[entry_len]; +char path[entry_len]; + +if (sscanf (entry,":!(%4096[^)])%4096s" , &flags, &path) !=2 ) { +return ; + } +if (count_slashes (flags) >0 ) { +return ; + } + +warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); +}
pathspec.h +1 -0
@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_mishandled_exclude (const char * pathspec_entry);#endif /* PATHSPEC_H */
t/t6132-pathspec-exclude.sh +33 -0
@@ -244,4 +244,37 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +Pathspec provided matches `:!(...)` +EOF +test_expect_success 'warn pathspec :!(...) skips the parenthesized magics' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + grep -Ff expected_warn warn +' + +test_expect_success 'do not warn that pathspec :!(...) skips the parenthesized magics (if parenthesis would not be part of the magic)' ' + git log --oneline --format=%s -- '"'"':!(gl/ob)/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + test_cmp expect actual && + ! grep -Ff expected_warn warn +' + test_done
Signature · 1 line
2.31.0
Re: [RFC PATCH v1 1/2] pathspec: warn: long and short forms are incompatible
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 750
Quoted 4 lines · Σταύρος Ντέντος
pathspec.c +4
-0+void check_mishandled_exclude (const char *entry) { +size_t entry_len =strlen (entry); +char flags[entry_len]; +char path[entry_len];
We avoid using variable-length arrays in our codebase. For one thing, they were not historically supported by all platforms (we are slowly using more C99 features, but we are introducing them slowly and intentionally).
But two, they are limited in size and the failure mode is not graceful. If "entry" is larger than the available stack, then we'll get a segfault with no option to handle it better.
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (sscanf(entry, ":!(%4096[^)])%4096s", &flags, &path) != 2) { + return;
We also generally avoid using scanf, because it's error-prone. The "4096" is scary here, but I don't _think_ it's a buffer overflow, because "path" is already the same size as "entry" (not including the NUL terminator, but that is negated by the fact that we'll have skipped at least ":!").
Is this "%4096[^)]" actually valid? I don't think scanf understands regular expressions.
We'd want to avoid making an extra copy of the string anyway. So you'd probably want to just parse left-to-right in the original string, like:
const char *p = entry;
/* skip past stuff we know must be there */
if (!skip_prefix(p, ":!(", &p))
return;
/* this checks count_slashes() > 0 in the flags section, though I'm
* not sure I understand what that is looking for... */
for (; *p && *p != ')'; p++) {
if (*p == '/')
return;
}
if (*p++ != ')')
return; /* now p is pointing at "path", though we don't seem to do anything
* with it... */
-Peff
Re: [RFC PATCH v1 1/2] pathspec: warn: long and short forms are incompatible
Quoted 1 line
We avoid using variable-length arrays in our codebase. ...
Hear hear, however, I wanted to avoid the "small mess"
that allocate/free would cause (one or more of ++sloc, labels, if-nesting); ...
Quoted 1 line
But two, they are limited in size and the failure mode is not graceful. ...
... however, my main issue is that - I don't know what's a sane allocation size.
Quoted 1 line
... The "4096" is scary here ...
While scary, it is "a safe" upper high.
The first time the string ends up in pathspec.c for processing it's here:
entry = argv[i]; which comes from here parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
and I don't know what's the maximum size of parsed_file.v[0]
Quoted 2 lines
Is this "%4096[^)]" actually valid? I don't think scanf understands regular expressions.
I was suprised too - but it's not regex.
see: https://www.cplusplus.com/reference/cstdio/scanf/#parameters - 4th/3rd row from the end
%s is greedy and there would be no other way to limit sscanf to not include ) on its first variable.
Quoted 1 line
... though I'm not sure I understand what that is looking for ...
I think it will help you see what I am trying to achieve if you read at the warning message / testcase
https://lore.kernel.org/git/[email protected]/#iZ30t:t6132-pathspec-exclude.sh
And, to clean up the testcase:
git log --oneline --format=%s -- ':!(glob)**/file'
I guess it should be now obvious what am I targetting:
If someone naively mixes short and long pathspec magics (!, and (glob)), short form takes precedence and ignores long magic / assumes long magic as part of path.
(If it's not obvious, all the more reason to include such warning)
Re: [RFC PATCH v1 1/2] pathspec: warn: long and short forms are incompatible
Quoted 7 lines · Stavros Ntentos
Quoted 1 line
We avoid using variable-length arrays in our codebase. ...
Hear hear, however, I wanted to avoid the "small mess"
that allocate/free would cause (one or more of ++sloc, labels, if-nesting); ...Quoted 1 line
But two, they are limited in size and the failure mode is not graceful. ...
... however, my main issue is that - I don't know what's a sane allocation size.
I don't think a VLA gets you out of knowing the allocation size. Either way, you are using strlen(entry).
But I do think avoiding allocating altogether is better (as I showed in my previous response).
Quoted 8 lines
Quoted 1 line
... The "4096" is scary here ...
While scary, it is "a safe" upper high.
The first time the string ends up inpathspec.cfor processing it's here:entry = argv[i]; which comes from here parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);and I don't know what's the maximum size of
parsed_file.v[0]
There is no reasonable maximum size you can assume. Using 4096 is most definitely not a safe upper bound.
However, as I said, I don't think it is doing anything useful in the first place. You have sized the destination buffers as large as the original string, so they must be large enough to hold any subset of the original. Dropping them would be equally correct, but less distracting to a reader.
Quoted 6 lines
Quoted 2 lines
Is this "%4096[^)]" actually valid? I don't think scanf understands regular expressions.
I was suprised too - but it's not regex.
see: https://www.cplusplus.com/reference/cstdio/scanf/#parameters - 4th/3rd row from the end
Thanks, this is a corner of scanf I haven't looked at (mostly because again, we generally avoid scanf in our code base entirely).
Quoted 4 lines
I think it will help you see what I am trying to achieve if you read at the warning message / testcase
https://lore.kernel.org/git/[email protected]/#iZ30t:t6132-pathspec-exclude.shAnd, to clean up the testcase:
Quoted 8 lines
git log --oneline --format=%s -- ':!(glob)**/file'I guess it should be now obvious what am I targetting:
If someone naively mixes short and long pathspec magics (
!, and(glob)), short form takes precedence and ignores long magic / assumes long magic as part of path.(If it's not obvious, all the more reason to include such warning)
I understand the overall goal. I am not sure why slashes in the flags section are a reliable indicator that this mixing is not happening and we should not show the warning.
It also feels like any checks like this should be relying on the existing pathspec-magic parser a bit more. I don't know the pathspec code that well, but surely at some point it has a notion of which parts are magic flags (e.g., after parse_element_magic in init_pathspec_item).
-Peff
Re: [RFC PATCH v1 1/2] pathspec: warn: long and short forms are incompatible
Quoted 4 lines · Jeff King
It also feels like any checks like this should be relying on the existing pathspec-magic parser a bit more. I don't know the pathspec code that well, but surely at some point it has a notion of which parts are magic flags (e.g., after
parse_element_magicininit_pathspec_item).
Absolutely. parse_element_magic() decides, by looking at the character after the initial ':', the we are looking at the longhand form when magic sequence is introduced by ":(".
Otherwise, : must be followed by shorthand form, where the only usable ones are '/' (synonym for top), '!' and '^' (synonym for exclude), and ':' (end of short-form marker), but most importantly, '(' will *never* be a valid shorthand form (because it is not part of the GIT_PATHSPEC_MAGIC class of bytes).
So, presumably, inside parse_short_magic(), you could detect '(' and complain it as an attempt to switch to longform.
Here is to illustrate where to hook the check. With this, I can get something like this:
$ git show -s --oneline -- ':!/(foobar)frotz' ':/(bar)nitfol' .
warning: :!/: cannot mix shortform magic with longform like (exclude)
warning: :/: cannot mix shortform magic with longform like (exclude)
84d06cdc06 Sync with v2.31.1Note that this illustration does not show the best warning message. To improve it to a bit more useful level, I think two things need to happen on top.
* Use advice to allow users sequelch.
* Show a few letters after '(' (but pay attention to the end of the string, which ends with either '\0' or ':'), and lose the substring "like (exclude)" from the message.
That would have given
hint: ":!/(foo...": cannot mix shortform and longform magic
for the above example.
I initially thought it might make it even better if we looked ahead of what comes after '(', took the substring before ',' or ')', and looked up pathspec_magic[] array, BUT I do not think it is a good idea. It would be confusing if we do not give the same advice to ":!(literol)" when the user does get one for ":!(literal)". So the above "two things need to happen" list deliberately excludes an "improvement" doing so.
pathspec.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
pathspec.c +13 -0
@@ -336,6 +336,9 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } +static const char mixed_pathspec_magic[] = +N_ ("%.*s: cannot mix shortform magic with longform like (exclude)" ); +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +359,16 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) { +/* +* a possible common mistake: once you started +* a shortform you cannot add (longform), e.g. +* ":!(top)" +*/ +warning (_ (mixed_pathspec_magic), + (int )(pos-elem), elem); + } +if (!is_pathspec_magic (ch))break ;
Re: [RFC PATCH v1 1/2] pathspec: warn: long and short forms are incompatible
Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Jeff King
It also feels like any checks like this should be relying on the existing pathspec-magic parser a bit more. I don't know the pathspec code that well, but surely at some point it has a notion of which parts are magic flags (e.g., after
parse_element_magicininit_pathspec_item).Absolutely.
parse_element_magic()decides, by looking at the character after the initial ':', the we are looking at the longhand form when magic sequence is introduced by ":(".
Absolutely from me too! I would want to re-use more of the existing code. In response to that, patch
https://lore.kernel.org/git/[email protected]/
clearly shows that it is possible to re-use the de-facto long form magic parsing code to attempt to parse the string that continues as a long form magic.
This could theoretically pave the road for mixed short/long form parsing (in that order), if would be so desired.
However, as probably suspected, it suffers from one big problem:
if (ARRAY_SIZE(pathspec_magic) <= i)
die(_("Invalid pathspec magic '%.*s' in '%s'"),
(int) len, pos, elem);which is an unconditional exit(128).
I don't know if it's possible to rescue (or redirect) that die call. In any case, considering that, this again deviates from the "small informative" change this commit was supposed to be.
Having said that, I have cooked a working patch with the proposed code.
RFC PATCH 2/2 fixup! pathspec: warn: long and short forms are incompatible
In reply to Σταύρος ΝτέντοςFrom: Stavros Ntentos <[email protected]>
malloc and stuff
--- pathspec.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
pathspec.c +13 -8
@@ -7,6 +7,7 @@#include "attr.h" #include "strvec.h" #include "quote.h" +#include "git-compat-util.h" /* * Finds which of the given pathspecs match items in the index.
@@ -745,16 +746,20 @@ int match_pathspec_attrs(const struct index_state *istate, }void check_mishandled_exclude (const char *entry) { +char *flags, *path;size_t entry_len =strlen (entry); -char flags[entry_len]; -char path[entry_len]; -if (sscanf (entry,":!(%4096[^)])%4096s" , &flags, &path) !=2 ) { -return ; - } -if (count_slashes (flags) >0 ) { -return ; + flags =xstrdup (entry); +memset (flags,'\0' , entry_len); + path =xstrdup (entry); +memset (path,'\0' , entry_len); + +if (sscanf (entry,":!(%4096[^)])%4096s" , flags, path) ==2 ) { +if (count_slashes (flags) ==0 ) { +warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); + } } -warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); +FREE_AND_NULL (flags); +FREE_AND_NULL (path); }
Signature · 1 line
2.31.0
Re: [RFC PATCH v1 2/2] fixup! pathspec: warn: long and short forms are incompatible
Quoted 6 lines · Σταύρος Ντέντος
From: Stavros Ntentos <[email protected]>
malloc and stuff
--- pathspec.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
What does this fixup patch try to accomplish? Give more details.
Quoted 28 lines
pathspec.c +4
-7@@ -7,6 +7,7 @@#include "attr.h" #include "strvec.h" #include "quote.h" +#include "git-compat-util.h" /* * Finds which of the given pathspecs match items in the index. @@ -745,16 +746,20 @@ int match_pathspec_attrs(const struct index_state *istate, }void check_mishandled_exclude (const char *entry) { +char *flags, *path;size_t entry_len =strlen (entry); -char flags[entry_len]; -char path[entry_len]; -if (sscanf (entry,":!(%4096[^)])%4096s" , &flags, &path) !=2 ) { -return ; - } -if (count_slashes (flags) >0 ) { -return ; + flags =xstrdup (entry); +memset (flags,'\0' , entry_len);
pathspec.c int match_pathspec_attrs(const struct index_state *istate, · line 759
Quoted 10 lines
pathspec.c +7
-1+ path =xstrdup (entry); +memset (path,'\0' , entry_len); + +if (sscanf (entry,":!(%4096[^)])%4096s" , flags, path) ==2 ) { +if (count_slashes (flags) ==0 ) { +warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); + } } -warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" ));
Message looks ok.
Quoted 4 lines
pathspec.c +2
-0+FREE_AND_NULL (flags); +FREE_AND_NULL (path); }
Signature · 1 line
An old man doll... just what I always wanted! - Clara
Re: [RFC PATCH v1 2/2] fixup! pathspec: warn: long and short forms are incompatible
I merely wanted to give visibility on the "resulting" patch
https://lore.kernel.org/git/[email protected]/,
by giving the two versions of the implementation
(as discussed in the https://lore.kernel.org/git/[email protected]/ cover)
RFC PATCH 3/3 squash! fixup! pathspec: warn: long and short forms are incompatible
In reply to Σταύρος ΝτέντοςAttempt to force parsing long magic values to detect if there is actually long magic present or not.
--- pathspec.c | 35 +++++++++++++++++++---------------- pathspec.h | 2 +- t/t6132-pathspec-exclude.sh | 4 ++-- 3 files changed, 22 insertions(+), 19 deletions(-)
pathspec.c +19 -16
@@ -389,9 +389,11 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,else if (elem[1 ] =='(' )/* longhand */ return parse_long_magic (magic, prefix_len, item, elem); -else +else {/* shorthand */ +check_mixed_short_and_long_magic (elem);return parse_short_magic (magic, elem); + } }/*
@@ -589,8 +591,6 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; -check_mishandled_exclude (entry); -init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE )
@@ -745,21 +745,24 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } -void check_mishandled_exclude (const char *entry) { -char *flags, *path; -size_t entry_len =strlen (entry); +void check_mixed_short_and_long_magic (const char *entry) { +const char *parsed_magic; - flags =xstrdup (entry); -memset (flags,'\0' , entry_len); - path =xstrdup (entry); -memset (path,'\0' , entry_len); +/* skip past stuff we know must be there */ +if (!skip_prefix (entry,":" , &entry)) { +return ; + } + +/* Throwaway allocations */ +unsigned magic =0 ; +int prefix_len =-1 ; +struct pathspec_item *item; + item =xmallocz (sizeof (&item)); -if (sscanf (entry,":!(%4096[^)])%4096s" , flags, path) ==2 ) { -if (count_slashes (flags) ==0 ) { -warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); - } + parsed_magic =parse_long_magic (&magic, &prefix_len, item, entry); +if (entry != parsed_magic) { +warning (_ ("Pathspec provided matches both short and long forms.\nShort forms take presedence over long forms!" )); } -FREE_AND_NULL (flags); -FREE_AND_NULL (path); +FREE_AND_NULL (item); }
pathspec.h +1 -1
@@ -157,6 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); -void check_mishandled_exclude (const char * pathspec_entry); +void check_mixed_short_and_long_magic (const char * pathspec_entry);#endif /* PATHSPEC_H */
t/t6132-pathspec-exclude.sh +2 -2
@@ -245,7 +245,7 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' ' cat > expected_warn <<"EOF" -Pathspec provided matches `:!(...)` +Pathspec provided matches both short and long forms. EOF test_expect_success 'warn pathspec :!(...) skips the parenthesized magics' ' git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn &&
@@ -263,7 +263,7 @@ EOF grep -Ff expected_warn warn ' -test_expect_success 'do not warn that pathspec :!(...) skips the parenthesized magics (if parenthesis would not be part of the magic)' ' +test_expect_success 'do not warn that pathspec :!(...) skips the parenthesized magics (if parenthesized text would not be magic)' ' git log --oneline --format=%s -- '"'"':!(gl/ob)/file'"'"' >actual 2>warn && cat <<EOF >expect && sub2/file
Signature · 1 line
2.31.0
cover pathspec: warn: long and short forms are incompatible
In reply to Junio C HamanoFrom: Stavros Ntentos <[email protected]>
The second of the two issues identified in this thread.
Both patches work as expected.
There is a short solution, which is mostly okay. It does not compile with -Werror, but:
It avoids malloc/free, and keeps the logic/sloc low.
All of this could be my lack of C experience.
Stavros Ntentos (1):
pathspec: warn: long and short forms are incompatible
pathspec.c | 30 ++++++++++++++++++++++++++++++ pathspec.h | 1 + t/t6132-pathspec-exclude.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+)
--
2.31.0
RFC PATCH 1/1 pathspec: warn: long and short forms are incompatible
From: Stavros Ntentos <[email protected]>
Namely, ! and any other long magic form (e.g. glob) cannot be combined to one entry.
Issue a warning when such thing happens, and hint to the solution.
--- pathspec.c | 24 ++++++++++++++++++++++++ pathspec.h | 1 + t/t6132-pathspec-exclude.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+)
pathspec.c +24 -0
@@ -1,3 +1,5 @@ +#include <stdio.h> +#include <string.h> #include "cache.h" #include "config.h" #include "dir.h"
@@ -5,6 +7,7 @@#include "attr.h" #include "strvec.h" #include "quote.h" +#include "git-compat-util.h" /* * Finds which of the given pathspecs match items in the index.
@@ -586,6 +589,8 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; +check_mishandled_exclude (entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE )
@@ -739,3 +744,22 @@ int match_pathspec_attrs(const struct index_state *istate,return 1 ; } + +void check_mishandled_exclude (const char *entry) { +char *flags, *path; +size_t entry_len =strlen (entry); + + flags =xstrdup (entry); +memset (flags,'\0' , entry_len); + path =xstrdup (entry); +memset (path,'\0' , entry_len); + +if (sscanf (entry,":!(%4096[^)])%4096s" , flags, path) ==2 ) { +if (count_slashes (flags) ==0 ) { +warning (_ ("Pathspec provided matches `:!(...)`\n\tDid you mean `:(exclude,...)`?" )); + } + } + +FREE_AND_NULL (flags); +FREE_AND_NULL (path); +}
pathspec.h +1 -0
@@ -157,5 +157,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item); +void check_mishandled_exclude (const char * pathspec_entry);#endif /* PATHSPEC_H */
t/t6132-pathspec-exclude.sh +33 -0
@@ -244,4 +244,37 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +Pathspec provided matches `:!(...)` +EOF +test_expect_success 'warn pathspec :!(...) skips the parenthesized magics' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + grep -Ff expected_warn warn +' + +test_expect_success 'do not warn that pathspec :!(...) skips the parenthesized magics (if parenthesis would not be part of the magic)' ' + git log --oneline --format=%s -- '"'"':!(gl/ob)/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + test_cmp expect actual && + ! grep -Ff expected_warn warn +' + test_done
Signature · 1 line
2.31.0
PATCH v3 1/1 pathspec: advice: long and short forms are incompatible
In reply to Σταύρος ΝτέντοςIt can be a "reasonable" mistake to mix short and long forms, e.g. :!(glob), instead of the (correct) :(exclude,glob).
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.
Based on "Junio C Hamano <[email protected]>"'s code suggestions:
* https://lore.kernel.org/git/[email protected]/
* https://lore.kernel.org/git/[email protected]/
--- Documentation/config/advice.txt | 3 +++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 33 ++++++++++++++++++++++++++++ t/t6132-pathspec-exclude.sh | 38 +++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+)
Documentation/config/advice.txt +3 -0
@@ -119,4 +119,7 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic. --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_pathspec_magic =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedPathspecMagic", &advice_mixed_pathspec_magic },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_MIXED_PATHSPEC_MAGIC] = { "mixedPathspecMagic", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_pathspec_magic;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_MIXED_PATHSPEC_MAGIC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +33 -0
@@ -336,6 +336,36 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } + +/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic, as well as possible corrections +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; + +if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + } +advise_if_enabled (ADVICE_MIXED_PATHSPEC_MAGIC , +_ ("'%.*s(...': cannot mix short- and longform pathspec magic!\n" +"Either spell the shortform magic '%.*s' as ':(%s,...'\n" +"or end magic pathspec matching with '%.*s:'." ), + (int )(pos - elem), elem, + (int )(pos - (elem +1 )), elem +1 , + longform.buf, + (int )(pos - elem), elem); +} +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +386,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;
t/t6132-pathspec-exclude.sh +38 -0
@@ -244,4 +244,42 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic! +hint: Either spell the shortform magic '!' as ':(exclude,...' +hint: or end magic pathspec matching with ':!:'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Signature · 1 line
2.31.1
Re: [PATCH v3] pathspec: advice: long and short forms are incompatible
Quoted 2 lines · Stavros Ntentos
It can be a "reasonable" mistake to mix short and long forms, e.g.
:!(glob), instead of the (correct):(exclude,glob).
The word "form" may be sufficient for today's us because we have been so focused on this particular pathspec magic issue.
But reviewers are not, and those who read the "git log" output later are not, either. Let's be friendly and helpful to them by saying "form of pathspec magic" or somesuch.
The same comment applies to the patch title.
It also might be more friendly to readers what the mistaken form would do, too.
Here is my attempt, taking all of the above into account.
It is a hard-to-notice mistake to try mixing short and long forms of pathspec magic, e.g. instead of ':(exclude,glob)', it may be tempting to write ':!(glob)', which stops at ":!", i.e. the end of the short-form pathspec magic, and the "(glob)" is taken as the beginning part of the pathspec, wanting to match a file or a directory whose name begins with that literal string.
Quoted 5 lines
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.
OK.
Quoted 3 lines
Based on "Junio C Hamano <[email protected]>"'s code suggestions:
* https://lore.kernel.org/git/[email protected]/
* https://lore.kernel.org/git/[email protected]/
It is sufficient to just write a single line
immediately before your sign-off, I would think.
Quoted 1 line
Signed-off-by Stavros Ntentos <[email protected]>
The sign-off name and address must match the name and address of the patch author (i.e. "Stavros Ntentos <[email protected]>").
Documentation/config/advice.txt advice.*:: · line 124
Quoted 3 lines
Documentation/config/advice.txt +3
-0+ mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic.
Good. Here the phrase "pathspec magic" is used.
Quoted 9 lines
pathspec.c +9
-0+/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic, as well as possible corrections +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; +
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 358
Quoted 10 lines
pathspec.c +10
-0+if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + }
OK, so we are collecting the ones given by the short form so far. For e.g. ":!(glob)", we write "exclude" in the longform buffer. If we had more than one, then before adding the second one, we add a comma, so we may see "top,exclude" for ":/!(glob)". Good.
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 360
Quoted 2 lines
pathspec.c +2
-0+ advise_if_enabled(ADVICE_MIXED_PATHSPEC_MAGIC, + _("'%.*s(...': cannot mix short- and longform pathspec magic!\n"
Do we need to "shout!"? I think a normal full-stop would be sufficient.
'elem' is pointing at ':', 'pos' is where we read '(' from, so the above gives us "':/!(...': cannot..." for "':/!(glob)". OK.
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 361
Quoted 1 line
pathspec.c +1
-0+"Either spell the shortform magic '%.*s' as ':(%s,...'\n"
Here, the shortform %.*s excludes the ':' that introduced the magic, so we would see "shortform magic '/!' as ':(top,exclude,...'". Good.
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 362
Quoted 1 line
pathspec.c +1
-0+ "or end magic pathspec matching with '%.*s:'."),
This one I am not sure about. Something like
$ git add -- ":!(0) preface.txt" \*.txt
may be plausible, albeit rare, and it may be a good advice to explicitly terminate the shortform pathspec magic before the '(' in such a case.
But presumably it is much rarer for '(' to be a part of a pathspec element than an attempt to introduce a longform magic, it might be worth spending an extra line to explain in what narrow cases the latter choice may make sense. Here is my attempt.
or if '(...' is indeed the beginning of a pathname, end the shortform magic sequence explicitly with another ':' before it, e.g. '%.*s:(...'
Quoted 34 lines
pathspec.c +8
-0+ (int )(pos - elem), elem, + (int )(pos - (elem +1 )), elem +1 , + longform.buf, + (int)(pos - elem), elem); +}/* * Parse the pathspec element looking for short magic * @@ -356,6 +386,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;t/t6132-pathspec-exclude.sh +8
-0@@ -244,4 +244,42 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic! +hint: Either spell the shortform magic '!' as ':(exclude,...' +hint: or end magic pathspec matching with ':!:'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn &&
t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 264
Quoted 10 lines
t/t6132-pathspec-exclude.sh +10
-0+ cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn &&
Are these leftover debugging aid?
Quoted 6 lines
t/t6132-pathspec-exclude.sh +6
-0+ test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn &&
t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 280
Quoted 10 lines
t/t6132-pathspec-exclude.sh +10
-0+ cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn &&
Are these leftover debugging aid?
t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 285
Quoted 5 lines
t/t6132-pathspec-exclude.sh +4
-0+ test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Thanks.
Re: [PATCH v3] pathspec: advice: long and short forms are incompatible
Hello there,
Implemented the last fixes
Quoted 4 lines
Quoted 1 line
Signed-off-by Stavros Ntentos <[email protected]>The sign-off name and address must match the name and address of the patch author (i.e. "Stavros Ntentos <[email protected]>").
The author *is* Stavros Ntentos <[email protected]>; I don't know why it is messed up. Maybe if I send the patch as an attachment instead.
With regards,
Ntentos Stavros
Quoted 179 lines · Junio C Hamano
Quoted 2 lines · Stavros Ntentos
It can be a "reasonable" mistake to mix short and long forms, e.g.
:!(glob), instead of the (correct):(exclude,glob).The word "form" may be sufficient for today's us because we have been so focused on this particular pathspec magic issue.
But reviewers are not, and those who read the "git log" output later are not, either. Let's be friendly and helpful to them by saying "form of pathspec magic" or somesuch.
The same comment applies to the patch title.
It also might be more friendly to readers what the mistaken form would do, too.
Here is my attempt, taking all of the above into account.
It is a hard-to-notice mistake to try mixing short and long forms of pathspec magic, e.g. instead of ':(exclude,glob)', it may be tempting to write ':!(glob)', which stops at ":!", i.e. the end of the short-form pathspec magic, and the "(glob)" is taken as the beginning part of the pathspec, wanting to match a file or a directory whose name begins with that literal string.
Quoted 5 lines
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.OK.
Quoted 3 lines
Based on "Junio C Hamano <[email protected]>"'s code suggestions:
* https://lore.kernel.org/git/[email protected]/
* https://lore.kernel.org/git/[email protected]/It is sufficient to just write a single line
Helped-by Junio C Hamano <[email protected]>immediately before your sign-off, I would think.
Quoted 1 line
Signed-off-by Stavros Ntentos <[email protected]>The sign-off name and address must match the name and address of the patch author (i.e. "Stavros Ntentos <[email protected]>").
Review
Quoted 3 lines
(quoted diff) +3
-0+ mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic.Good. Here the phrase "pathspec magic" is used.
Quoted 9 lines
(quoted diff) +9
-0+/* + * Give hint for a common mistake of mixing short and long + * form of pathspec magic, as well as possible corrections + */ +static void warn_mixed_magic(unsigned magic, const char *elem, const char *pos) +{ + struct strbuf longform = STRBUF_INIT; + int i; +Review
Quoted 10 lines
(quoted diff) +10
-0+ if (!advice_enabled(ADVICE_MIXED_PATHSPEC_MAGIC)) + return; + + for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { + if (pathspec_magic[i].bit & magic) { + if (longform.len) + strbuf_addch(&longform, ','); + strbuf_addstr(&longform, pathspec_magic[i].name); + } + }OK, so we are collecting the ones given by the short form so far. For e.g. ":!(glob)", we write "exclude" in the longform buffer. If we had more than one, then before adding the second one, we add a comma, so we may see "top,exclude" for ":/!(glob)". Good.
Review
Quoted 2 lines
(quoted diff) +2
-0+ advise_if_enabled(ADVICE_MIXED_PATHSPEC_MAGIC, + _("'%.*s(...': cannot mix short- and longform pathspec magic!\n"Do we need to "shout!"? I think a normal full-stop would be sufficient.
'elem' is pointing at ':', 'pos' is where we read '(' from, so the above gives us "':/!(...': cannot..." for "':/!(glob)". OK.
Quoted 1 line
+ "Either spell the shortform magic '%.*s' as ':(%s,...'\n"
Here, the shortform %.*s excludes the ':' that introduced the magic, so we would see "shortform magic '/!' as ':(top,exclude,...'". Good.
Quoted 1 line
+ "or end magic pathspec matching with '%.*s:'."),
This one I am not sure about. Something like
$ git add -- ":!(0) preface.txt" \*.txtmay be plausible, albeit rare, and it may be a good advice to explicitly terminate the shortform pathspec magic before the '(' in such a case.
But presumably it is much rarer for '(' to be a part of a pathspec element than an attempt to introduce a longform magic, it might be worth spending an extra line to explain in what narrow cases the latter choice may make sense. Here is my attempt.
or if '(...' is indeed the beginning of a pathname, end the shortform magic sequence explicitly with another ':' before it, e.g. '%.*s:(...'
Quoted 34 lines
(quoted diff) +8
-0+ (int)(pos - elem), elem, + (int)(pos - (elem + 1)), elem + 1, + longform.buf, + (int)(pos - elem), elem); +} /* * Parse the pathspec element looking for short magic *@@ -356,6 +386,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem) continue; } + if (ch == '(') + warn_mixed_magic(*magic, elem, pos); + if (!is_pathspec_magic(ch)) break;t/t6132-pathspec-exclude.sh +8
-0@@ -244,4 +244,42 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic! +hint: Either spell the shortform magic '!' as ':(exclude,...' +hint: or end magic pathspec matching with ':!:'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn &&t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 264
Quoted 10 lines
t/t6132-pathspec-exclude.sh +10
-0+ cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn &&Are these leftover debugging aid?
Quoted 6 lines
t/t6132-pathspec-exclude.sh +6
-0+ test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn &&t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 280
Quoted 10 lines
t/t6132-pathspec-exclude.sh +10
-0+ cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn &&Are these leftover debugging aid?
Quoted 8 lines
t/t6132-pathspec-exclude.sh test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' · line 285
Quoted 5 lines
t/t6132-pathspec-exclude.sh +4
-0+ test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_doneThanks.
From edb4af00979124c8cc1674a5ad76f9634f74d42e Mon Sep 17 00:00:00 2001
From: Stavros Ntentos <[email protected]>
Date: Sun, 28 Mar 2021 15:53:50 +0300
Subject: [PATCH v4] pathspec: advice: long and short form of pathspec magic
are incompatible
It is a hard-to-notice mistake to try mixing short and long forms of pathspec magic, e.g. instead of ':(exclude,glob)', it may be tempting to write ':!(glob)'.
The end of the short-form pathspec magic (:!), and the (glob) is taken as the beginning part of the pathspec, wanting to match a file or a directory whose name begins with that literal string.
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the parsing is not explicitly terminated with :, and
* if the string contains an open parenthesis [(] issue an advice hinting to that fact.
--- Documentation/config/advice.txt | 3 +++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 34 ++++++++++++++++++++++++++++++++ t/t6132-pathspec-exclude.sh | 35 +++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+)
Documentation/config/advice.txt +3 -0
@@ -119,4 +119,7 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic. --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_pathspec_magic =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedPathspecMagic", &advice_mixed_pathspec_magic },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_MIXED_PATHSPEC_MAGIC] = { "mixedPathspecMagic", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_pathspec_magic;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_MIXED_PATHSPEC_MAGIC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +34 -0
@@ -336,6 +336,37 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } + +/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic, as well as possible corrections +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; + +if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + } +advise_if_enabled (ADVICE_MIXED_PATHSPEC_MAGIC , +_ ("'%.*s(...': cannot mix short- and longform pathspec magic.\n" +"Either spell the shortform magic '%.*s' as ':(%s,...',\n" +"or if '(...' is indeed the beginning of a pathname, end the shortform\n" +"magic sequence explicitly with another ':' before it, e.g. '%.*s:(...'." ), + (int )(pos - elem), elem, + (int )(pos - (elem +1 )), elem +1 , + longform.buf, + (int )(pos - elem), elem); +} +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +387,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;
t/t6132-pathspec-exclude.sh +35 -0
@@ -244,4 +244,39 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic. +hint: Either spell the shortform magic '!' as ':(exclude,...', +hint: or if '(...' is indeed the beginning of a pathname, end the shortform +hint: magic sequence explicitly with another ':' before it, e.g. ':!:(...'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Signature · 1 line
2.31.1
Re: [PATCH v3] pathspec: advice: long and short forms are incompatible
Quoted 2 lines · Σταύρος Ντέντος
The author *is* Stavros Ntentos <[email protected]>; I don't know why it is messed up.
Hmph, that is unfortunate. As authorship and sign-off is a part of the mechanism we use to keep track of provenance of our codebase, we'd prefer to be talking to somebody whom we can reach when needed; the noreply address apparently is designed to be unreachable, no?
Re: [PATCH v3] pathspec: advice: long and short forms are incompatible
Quoted 6 lines · Junio C Hamano
Hmph, that is unfortunate. As authorship and sign-off is a part of the mechanism we use to keep track of provenance of our codebase, we'd prefer to be talking to somebody whom we can reach when needed; the noreply address apparently is designed to be unreachable, no?
I would like to "just" contribute, if possible.
If someone *really* needs to track me down, I assume the mailing list is anyway already public,
and the patchset title already will directly link to the thread.
With regards,
Ntentos Stavros
PATCH v2 1/1 pathspec: advice: long and short forms are incompatible
In reply to Junio C HamanoIt can be a "reasonable" mistake to mix short and long forms, e.g. :!(glob), instead of the (correct) :(exclude,glob).
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.
Based on "Junio C Hamano <[email protected]>"'s code suggestion (in https://lore.kernel.org/git/[email protected]/).
--- Documentation/config/advice.txt | 5 +++++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 16 ++++++++++++++ t/t6132-pathspec-exclude.sh | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+)
Documentation/config/advice.txt +5 -0
@@ -119,4 +119,9 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedShortLongMagic:: + Advice shown if a user provides a pathspec that could be + interpreted as a mixed short and long magic modifier(s) + (i.e. contains an open parenthesis `(` without explictly + terminating pathspec magic parsing with `:`). --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_short_long_magic_pathspec =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedShortLongMagicPathspec", &advice_mixed_short_long_magic_pathspec },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_MIXED_SHORT_LONG_MAGIC_PATHSPEC] = { "mixedShortLongMagicPathspec", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_short_long_magic_pathspec;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_MIXED_SHORT_LONG_MAGIC_PATHSPEC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +16 -0
@@ -336,6 +336,11 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } +static const char mixed_pathspec_magic[] =N_ ( +"'%.*s...': cannot mix shortform magic with longform [e.g. like :(glob)].\n" +"If '%.*s...' is a valid path, explicitly terminate magic parsing with ':'; or" ); +static int extra_lookahead_chars =7 ; +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +361,17 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) { +/* +* a possible mistake: once you started a shortform +* you cannot add a longform, e.g. ":!(top)" +*/ +advise_if_enabled (ADVICE_MIXED_SHORT_LONG_MAGIC_PATHSPEC , +_ (mixed_pathspec_magic), + (int )(pos-elem+extra_lookahead_chars), elem, + extra_lookahead_chars, pos); + } +if (!is_pathspec_magic (ch))break ;
t/t6132-pathspec-exclude.sh +37 -0
@@ -244,4 +244,41 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(glob)*...': cannot mix shortform magic with longform [e.g. like :(glob)]. +hint: If '(glob)*...' is a valid path, explicitly terminate magic parsing with ':'; or +hint: Disable this message with "git config advice.mixedShortLongMagicPathspec false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Signature · 1 line
2.31.0
Re: [PATCH v2] pathspec: advice: long and short forms are incompatible
Stavros Ntentos <[email protected]> writes:
Administrivia.
If "Stavros Ntentos <[email protected]>" is an address that is not meant to receive any e-mail, please do not include it on the Cc line and force those who respond to you to remove it when replying.
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 340
Quoted 2 lines
pathspec.c +2
-0+static const char mixed_pathspec_magic[] =N_ ( + "'%.*s...': cannot mix shortform magic with longform [e.g. like :(glob)].\n"
OK. Just a bit of bikeshedding.
cannot mix short and long form magic
cannot mix shortform magic with longform
The former is a bit shorter. Also, if we show (with %.*s) the actual beginning of their attempt, e.g. when they gave us [*]
git show -- ':!(global,icase)foo'
if we show
':!(glo...': cannot mix short and long form pathspec magic
or even just
':!(...': cannot mix short and long form pathspec magic
it may be sufficiently clear where the problem is.
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 341
Quoted 1 line
pathspec.c +1
-0+ "If '%.*s...' is a valid path, explicitly terminate magic parsing with ':'; or");
The seemingly-stray '; or' at the end aside, I am not sure what this is trying to say. If the sample input were [*] above, what are we asking? "if 'foo' is a valid path"? No, we are showing 7 chars starting at pos, so "if 'global,i...' is a valid path"?
If ':(global,icase)foo' were the exact path the user wants to match with, then "prefix the whole thing with ":(literal)" would be an understandable hint, but that is not what you are suggesting.
In short, I do not quite agree with the second line of the message.
It may be more helpful if, rather than looking at what comes after '(', we looked at what came before '(' and helped the user write them out in the longform, i.e. perhaps we can tell them the moral equivalent of:
If you meant by to use the pathspec magic '!' with other longform magic after '(' with ":!(...", use ":(exclude,..." instead. Short and long form of pathspec magic do not mix.
Quoted 1 line
+static int
extra_lookahead_chars= 7;
A few problems:
- This is not something we want to configure. It does not need to be a variable.
- This is not something anybody other than the code in the new block "if (ch == '(')" in parse_short_magic() needs to know. It does not need to be a file-scope static.
- 7 is way too long for warning against something like ":!(glob)", no?
But with the above "It may be more helpful" suggestion, notice that I am deliberately refraining from looking at what comes after '(', so extra-lookahead may not be necessary after all, and nitpicking about it may be moot.
Thanks.
Quoted 17 lines
pathspec.c +11
-0@@ -356,6 +361,17 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) { +/* +* a possible mistake: once you started a shortform +* you cannot add a longform, e.g. ":!(top)" +*/ +advise_if_enabled (ADVICE_MIXED_SHORT_LONG_MAGIC_PATHSPEC , +_ (mixed_pathspec_magic), + (int )(pos-elem+extra_lookahead_chars), elem, + extra_lookahead_chars, pos); + } +if (!is_pathspec_magic (ch))break ;
Re: [PATCH v2] pathspec: advice: long and short forms are incompatible
Quoted 11 lines · Junio C Hamano
pathspec.c static const char *parse_long_magic(unsigned *magic, int *prefix_len, · line 340
Quoted 2 lines
pathspec.c +2
-0+static const char mixed_pathspec_magic[] =N_ ( + "'%.*s...': cannot mix shortform magic with longform [e.g. like :(glob)].\n"OK. Just a bit of bikeshedding.
cannot mix short and long form magic
cannot mix shortform magic with longformThe former is a bit shorter. Also, if we show (with %.*s) the actual beginning of their attempt, e.g. when they gave us [*]
Quoted 7 lines
git show -- ':!(global,icase)foo'if we show
...
':!(...': cannot mix short and long form pathspec magicit may be sufficiently clear where the problem is.
I meant "clear where the problem is, without adding [e.g. like :(glob)]".
Thanks.
Re: [PATCH v2] pathspec: advice: long and short forms are incompatible
In reply to Junio C HamanoQuoted 6 lines
Administrivia.
If "Stavros Ntentos <[email protected]>" is an address that is not meant to receive any e-mail, please do not include it on the Cc line and force those who respond to you to remove it when replying.
I am trying. However, git-send-email keeps pulling that no-reply address, and git-send-email does not offer any --exclude-addresses="*glob*"-like option.
Quoted 5 lines
or even just
':!(...': cannot mix short and long form pathspec magic
it may be sufficiently clear where the problem is.
I slightly disagree, and prefer the extra_lookahead_chars. Just 3 characters [:!(] is a bit too little, and the total message sits below the "you can disable this message" hint.
Quoted 2 lines
The seemingly-stray '; or' at the end aside, I am not sure what this is trying to say.
See the testcase:
Quoted 2 lines
hint: If '(glob)*...' is a valid path, explicitly terminate magic parsing with ':'; or
hint: Disable this message with "git config advice.mixedShortLongMagicPathspec false"
I am segway-ing from the "explicitly stop parsing" to the "disable this message" sentence.
Quoted 3 lines
If ':(global,icase)foo' were the exact path the user wants to match with, then "prefix the whole thing with ":(literal)" would be an understandable hint, but that is not what you are suggesting.
I am siding with the "user entered this situation by mistake", and not with the "user is explicitly trying to match a file named :(global,icase)foo" side.
Offering a more complete message, will become more complex. I disagree with that. I can settle by offering examples (mine and yours) in the documentation.
Quoted 3 lines
It may be more helpful if, rather than looking at what comes after '(', we looked at what came before '(' and helped the user write them out in the longform
I don't see any explicit code in parsing the shortform magics, except:
/* Special case alias for '!' */
if (ch == '^') {
*magic |= PATHSPEC_EXCLUDE;
continue;
}and therefore I would like to avoid such task (although I love well-written DWIMs-or-close-to-them).
Quoted 6 lines
Quoted 1 line
+static int
extra_lookahead_chars= 7;A few problems:
- This is not something we want to configure. It does not need to be a variable.
I hate macros, only because I cannot expand or modify them during gdb. (Suggestions welcome! :-D)
Quoted 4 lines
- This is not something anybody other than the code in the new block "if (ch == '(')" in
parse_short_magic()needs to know. It does not need to be a file-scope static.
True, but the message was explicitly referred to with i18n code specifically targeted for such initialization.
I like code doing the same job, sitting together.
I'd prefer to either move both inside (since no one else will ever refer to this message either), or keep them as-is.
Quoted 2 lines
- 7 is way too long for warning against something like ":!(glob)", no?
GRRRRR C :-p
(I'll push the changes on the next iteration; including the like glob removed, and whatever comes from our discussion.)
Quoted 4 lines
But with the above "It may be more helpful" suggestion, notice that I am deliberately refraining from looking at what comes after '(', so extra-lookahead may not be necessary after all, and nitpicking about it may be moot.
Lookahead is simply to inform user what git will do with the current state of affairs, i.e.:
git log --oneline --format=%s -- ':!(glob)**/file'
will filter with
NOT '(glob)**/file'
path (truncated for brevity)
Re: [PATCH v2] pathspec: advice: long and short forms are incompatible
Quoted 6 lines
Quoted 2 lines
- 7 is way too long for warning against something like ":!(glob)", no?
GRRRRR C :-p
(I'll push the changes on the next iteration; including thelike globremoved, and whatever comes from our discussion.)
Actually ... C does not have a problem with it:
root@94ae60990b85:/usr/src/git/t# ../git log -- ':!(glob)' hint: ':!(glob)...': cannot mix shortform magic with longform [e.g. like :(glob)]. hint: If '(glob)...' is a valid path, explicitly terminate magic parsing with ':'; or hint: Disable this message with "git config advice.mixedShortLongMagicPathspec false" commit 14b8580dde326a0bbf9abacac7e09dbe4c38c25e (HEAD -> fix/mixed-short-long-magic)
True, it does not look nice; theoretically, any number (even 1!) could be too much to contain ellipsis after that text.
Given the size of the change, I would want to say that it is acceptable to leave this as-is.
Unless you'd like me to write a generic
truncate_at_chars(char *text, int width, char *ellipsis_char_or_default)
function.
Re: [PATCH v2] pathspec: advice: long and short forms are incompatible
In reply to Stavros NtentosQuoted 4 lines · Stavros Ntentos
Quoted 3 lines
It may be more helpful if, rather than looking at what comes after '(', we looked at what came before '(' and helped the user write them out in the longform
Quoted 8 lines
I don't see any explicit code in parsing the shortform magics, except:
/* Special case alias for '!' */ if (ch == '^') { *magic |= PATHSPEC_EXCLUDE; continue; }and therefore I would like to avoid such task (although I love well-written DWIMs-or-close-to-them).
I am not yet interested in doing DWIM, but a good advice is always welcome.
Along the lines of the attached illustration patch, perhaps.
$ git show --oneline --stat -- ':/!(glob)**/*.txt'
hint: ':/!(...': cannot mix short- and longform pathspec magic
hint: instead, spell the shortform magic '/!' as 'top,exclude' inside :(...Documentation/config/advice.txt | 3 +++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+)
Documentation/config/advice.txt +3 -0
@@ -119,4 +119,7 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic. --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_pathspec_magic =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedPathspecMagic", &advice_mixed_pathspec_magic },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -113,6 +115,7 @@ static struct { [ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 }, [ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 }, [ADVICE_IMPLICIT_IDENTITY] = { "implicitIdentity", 1 }, + [ADVICE_MIXED_PATHSPEC_MAGIC] = { "mixedPathspecMagic", 1 }, [ADVICE_NESTED_TAG] = { "nestedTag", 1 }, [ADVICE_OBJECT_NAME_WARNING] = { "objectNameWarning", 1 }, [ADVICE_PUSH_ALREADY_EXISTS] = { "pushAlreadyExists", 1 },
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_pathspec_magic;/* * To add a new advice, you need to:
@@ -51,6 +52,7 @@ extern int advice_add_empty_pathspec;ADVICE_GRAFT_FILE_DEPRECATED ,ADVICE_IGNORED_HOOK ,ADVICE_IMPLICIT_IDENTITY , +ADVICE_MIXED_PATHSPEC_MAGIC ,ADVICE_NESTED_TAG ,ADVICE_OBJECT_NAME_WARNING , ADVICE_PUSH_ALREADY_EXISTS,
pathspec.c +29 -0
@@ -336,6 +336,32 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } +/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; + +if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + } +advise (_ ("'%.*s(...': cannot mix short- and longform pathspec magic" ), + (int )(pos - elem), elem); +advise (_ ("instead, spell the shortform magic '%.*s' as '%s' inside :(..." ), + (int )(pos - (elem +1 )), elem +1 , + longform.buf); +} + +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +382,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;
PATCH v3 1/1 pathspec: advice: long and short forms are incompatible
In reply to Junio C HamanoIt can be a "reasonable" mistake to mix short and long forms, e.g. :!(glob), instead of the (correct) :(exclude,glob).
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.
Based on "Junio C Hamano <[email protected]>"'s code suggestions:
* https://lore.kernel.org/git/[email protected]/
* https://lore.kernel.org/git/[email protected]/
--- Documentation/config/advice.txt | 3 +++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 33 ++++++++++++++++++++++++++++ t/t6132-pathspec-exclude.sh | 38 +++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+)
Documentation/config/advice.txt +3 -0
@@ -119,4 +119,7 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic. --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_pathspec_magic =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedPathspecMagic", &advice_mixed_pathspec_magic },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_MIXED_PATHSPEC_MAGIC] = { "mixedPathspecMagic", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_pathspec_magic;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_MIXED_PATHSPEC_MAGIC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +33 -0
@@ -336,6 +336,36 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } + +/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic, as well as possible corrections +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; + +if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + } +advise_if_enabled (ADVICE_MIXED_PATHSPEC_MAGIC , +_ ("'%.*s(...': cannot mix short- and longform pathspec magic!\n" +"Either spell the shortform magic '%.*s' as ':(%s,...'\n" +"or end magic pathspec matching with '%.*s:'." ), + (int )(pos - elem), elem, + (int )(pos - (elem +1 )), elem +1 , + longform.buf, + (int )(pos - elem), elem); +} +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +386,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;
t/t6132-pathspec-exclude.sh +38 -0
@@ -244,4 +244,42 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic! +hint: Either spell the shortform magic '!' as ':(exclude,...' +hint: or end magic pathspec matching with ':!:'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Signature · 1 line
2.31.1
PATCH v4 1/1 pathspec: advice: long and short forms are incompatible
In reply to Junio C HamanoIt can be a "reasonable" mistake to mix short and long forms, e.g. :!(glob), instead of the (correct) :(exclude,glob).
Teach git to issue an advice when such a pathspec is given.
i.e.: While in short form parsing:
* if the string contains an open parenthesis [(], and
* without having explicitly terminated magic parsing (:) issue an advice hinting to that fact.
Based on "Junio C Hamano <[email protected]>"'s code suggestions:
* https://lore.kernel.org/git/[email protected]/
* https://lore.kernel.org/git/[email protected]/
--- Documentation/config/advice.txt | 3 +++ advice.c | 3 +++ advice.h | 2 ++ pathspec.c | 33 ++++++++++++++++++++++++++++ t/t6132-pathspec-exclude.sh | 38 +++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+)
Documentation/config/advice.txt +3 -0
@@ -119,4 +119,7 @@ advice.*:: addEmptyPathspec:: Advice shown if a user runs the add command without providing the pathspec parameter. + mixedPathspecMagic:: + Advice shown if a user tries to mix short- and + longform pathspec magic. --
advice.c +3 -0
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;int advice_submodule_alternate_error_strategy_die =1 ;int advice_add_ignored_file =1 ;int advice_add_empty_pathspec =1 ; +int advice_mixed_pathspec_magic =1 ;static int advice_use_color =-1 ;static char advice_colors[][COLOR_MAXLEN ] = {
@@ -95,6 +96,7 @@ static struct { { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die }, { "addIgnoredFile", &advice_add_ignored_file }, { "addEmptyPathspec", &advice_add_empty_pathspec }, + { "mixedPathspecMagic", &advice_mixed_pathspec_magic },/* make this an alias for backward compatibility */ { "pushNonFastForward", &advice_push_update_rejected }
@@ -137,6 +139,7 @@ static struct { [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 }, [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_MIXED_PATHSPEC_MAGIC] = { "mixedPathspecMagic", 1 }, }; static const char turn_off_instructions[] =
advice.h +2 -0
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;extern int advice_submodule_alternate_error_strategy_die;extern int advice_add_ignored_file;extern int advice_add_empty_pathspec; +extern int advice_mixed_pathspec_magic;/* * To add a new advice, you need to:
@@ -72,6 +73,7 @@ extern int advice_add_empty_pathspec;ADVICE_STATUS_U_OPTION ,ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE ,ADVICE_WAITING_FOR_EDITOR , + ADVICE_MIXED_PATHSPEC_MAGIC, };int git_default_advice_config (const char *var,const char *value);
pathspec.c +33 -0
@@ -336,6 +336,36 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,return pos; } + +/* +* Give hint for a common mistake of mixing short and long +* form of pathspec magic, as well as possible corrections +*/ +static void warn_mixed_magic (unsigned magic,const char *elem,const char *pos) +{ +struct strbuf longform =STRBUF_INIT ; +int i; + +if (!advice_enabled (ADVICE_MIXED_PATHSPEC_MAGIC )) +return ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].bit & magic) { +if (longform.len) +strbuf_addch (&longform,',' ); +strbuf_addstr (&longform, pathspec_magic[i].name); + } + } +advise_if_enabled (ADVICE_MIXED_PATHSPEC_MAGIC , +_ ("'%.*s(...': cannot mix short- and longform pathspec magic!\n" +"Either spell the shortform magic '%.*s' as ':(%s,...'\n" +"or end magic pathspec matching with '%.*s:'." ), + (int )(pos - elem), elem, + (int )(pos - (elem +1 )), elem +1 , + longform.buf, + (int )(pos - elem), elem); +} +/* * Parse the pathspec element looking for short magic *
@@ -356,6 +386,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)continue ; } +if (ch =='(' ) +warn_mixed_magic (*magic, elem, pos); +if (!is_pathspec_magic (ch))break ;
t/t6132-pathspec-exclude.sh +38 -0
@@ -244,4 +244,42 @@ test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' ' test_cmp expect-grep actual-grep ' +cat > expected_warn <<"EOF" +hint: ':!(...': cannot mix short- and longform pathspec magic! +hint: Either spell the shortform magic '!' as ':(exclude,...' +hint: or end magic pathspec matching with ':!:'. +hint: Disable this message with "git config advice.mixedPathspecMagic false" +EOF +test_expect_success 'warn pathspec not matching longform magic in :!(...)' ' + git log --oneline --format=%s -- '"'"':!(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + test_cmp expected_warn warn +' + +test_expect_success 'do not warn pathspec not matching longform magic in :!:(...) (i.e. if magic parsing is explicitly stopped)' ' + git log --oneline --format=%s -- '"'"':!:(glob)**/file'"'"' >actual 2>warn && + cat <<EOF >expect && +sub2/file +sub/sub/sub/file +sub/file2 +sub/sub/file +sub/file +file +EOF + cat actual && + cat warn && + test_cmp expect actual && + ! test_cmp expected_warn warn +' + test_done
Signature · 1 line
2.31.1