pathspec magic extension to search for attributes
v9 cover pathspec magic extension to search for attributes
This goes on top of origin/jc/attr, (396bf756f95, attr: expose validity check for attribute names)
Patches 1 is a small fix, which could go independently as well. I dropped the patch "string list: improve comment" Patches 3 and 4 are refactoring pathspec.c a little. These did not change since v7
Patch 5 contains all of Junios suggestions.
Thanks,
Stefan
diff to v8:
Documentation/glossary-content.txt +17 -14
@@ -385,20 +385,23 @@ full pathname may have special meaning: Glob magic is incompatible with literal magic. attr;; - Additionally to matching the pathspec, the path must have the - attribute as specified. The syntax for specifying the required - attributes is "`attr: [mode] <attribute name> [=value]`" -+ -Attributes can have 4 states (Set, Unset, Set to a value, unspecified) and -you can query each attribute for certain states. The "`[mode]`" is a special -character to indicate which attribute states are looked for. The following -modes are available: - - - an empty "`[mode]`" matches if the attribute is set - - "`-`" the attribute must be unset - - "`!`" the attribute must be unspecified - - an empty "`[mode]`" combined with "`[=value]`" matches if the attribute has - the given value. +After `attr:` comes a space separated list of "attribute +requirements", all of which must be met in order for the +path to be considered a match; this is in addition to the +usual non-magic pathspec pattern matching. + +Each of the attribute requirements for the path takes one of +these forms: + +- "`ATTR`" requires that the attribute `ATTR` must be set. + +- "`-ATTR`" requires that the attribute `ATTR` must be unset. + +- "`ATTR=VALUE`" requires that the attribute `ATTR` must be + set to the string `VALUE`. + +- "`!ATTR`" requires that the attribute `ATTR` must be + unspecified. + exclude;;
dir.c +3 -3
@@ -231,11 +231,11 @@ static int match_attrs(const char *name, int namelen, match_mode = item->attr_match[i].match_mode;if (ATTR_TRUE (value)) - matched = match_mode ==MATCH_SET ; + matched = (match_mode ==MATCH_SET );else if (ATTR_FALSE (value)) - matched = match_mode ==MATCH_UNSET ; + matched = (match_mode ==MATCH_UNSET );else if (ATTR_UNSET (value)) - matched = match_mode ==MATCH_UNSPECIFIED ; + matched = (match_mode ==MATCH_UNSPECIFIED );else matched = (match_mode ==MATCH_VALUE && !strcmp (item->attr_match[i].value, value));
pathspec.c +22 -18
@@ -115,34 +115,38 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *vaconst char *attr = si->string;struct attr_match *am = &item->attr_match[j]; -if (attr[0 ] =='!' ) + attr_len =strcspn (attr,"=" ); +switch (*attr) { +case '!' : am->match_mode =MATCH_UNSPECIFIED ; -else if (attr[0 ] =='-' ) + attr++; + attr_len--; +break ; +case '-' : am->match_mode =MATCH_UNSET ; -else - am->match_mode =MATCH_SET ; - -if (am->match_mode !=MATCH_SET ) -/* skip first character */ attr++; + attr_len--; +break ; +default : +if (attr[attr_len] !='=' ) + am->match_mode =MATCH_SET ; +else { + am->match_mode =MATCH_VALUE ; + am->value =xstrdup (&attr[attr_len +1 ]); +if (strchr (am->value,'\\' )) +die (_ ("attr spec values must not contain backslashes" )); + } +break ; + } - attr_len =strcspn (attr,"=" ); -if (attr[attr_len] =='=' ) { - am->match_mode =MATCH_VALUE ; - am->value =xstrdup (&attr[attr_len +1 ]); -if (strchr (am->value,'\\' )) -die (_ ("attr spec values must not contain backslashes" )); - }else - am->value =NULL ; - -if (!attr_name_valid (attr, attr_len)) { + am->attr =git_attr_counted (attr, attr_len); +if (!am->attr) {struct strbuf sb =STRBUF_INIT ; am->match_mode =INVALID_ATTR ;invalid_attr_name_message (&sb, attr, attr_len);die (_ ("invalid attribute in '%s': '%s'" ), value, sb.buf); } - am->attr =git_attr_counted (attr, attr_len);git_attr_check_append (item->attr_check, am->attr); }
string-list.h +1 -1
@@ -106,7 +106,7 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_* list->strdup_strings must be set, as new memory needs to be * allocated to hold the substrings. If maxsplit is non-negative, * then split at most maxsplit times. Return the number of substrings -* appended to list. The list may be non-empty already. +* appended to list. * * Examples: * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"]
t/t6134-pathspec-with-labels.sh +96 -99
243 lines not on this page · raw message
PATCH v9 1/4 Documentation: fix a typo
--- Documentation/gitattributes.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/gitattributes.txt +1 -1
@@ -86,7 +86,7 @@ is either not set or empty, $HOME/.config/git/attributes is used instead. Attributes for all users on a system should be placed in the `$(prefix)/etc/gitattributes` file. -Sometimes you would need to override an setting of an attribute +Sometimes you would need to override a setting of an attribute for a path to `Unspecified` state. This can be done by listing the name of the attribute prefixed with an exclamation point `!`.
Signature · 1 line
2.8.2.123.gb4ad9b6.dirty
PATCH v9 2/4 pathspec: move long magic parsing out of prefix_pathspec
In reply to Stefan Bellerprefix_pathspec is quite a lengthy function and we plan on adding more. Split it up for better readability. As we want to add code into the inner loop of the long magic parsing, we also benefit from lower indentation.
--- pathspec.c | 84 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 37 deletions(-)
pathspec.c +47 -37
@@ -88,6 +88,52 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static void eat_long_magic (struct pathspec_item *item,const char *elt, +unsigned *magic,int *pathspec_prefix, +const char **copyfrom_,const char **long_magic_end) +{ +int i; +const char *copyfrom = *copyfrom_; +/* longhand */ +const char *nextat; +for (copyfrom = elt +2 ; + *copyfrom && *copyfrom !=')' ; + copyfrom = nextat) { +size_t len =strcspn (copyfrom,",)" ); +if (copyfrom[len] ==',' ) + nextat = copyfrom + len +1 ; +else +/* handle ')' and '\0' */ + nextat = copyfrom + len; +if (!len) +continue ; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, copyfrom, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } +if (starts_with (copyfrom,"prefix:" )) { +char *endptr; + *pathspec_prefix =strtol (copyfrom +7 , + &endptr,10 ); +if (endptr - copyfrom != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +/* "i" would be wrong, but it does not matter */ +break ; + } + } +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, copyfrom, elt); + } +if (*copyfrom !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); + *long_magic_end = copyfrom; + copyfrom++; + *copyfrom_ = copyfrom; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -150,43 +196,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, (flags & PATHSPEC_LITERAL_PATH)) { ;/* nothing to do */ } else if (elt[1] == '(') { -/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; - copyfrom++; +eat_long_magic (item, elt, &magic, &pathspec_prefix, ©from, &long_magic_end); } else {/* shorthand */ for (copyfrom = elt + 1;
Signature · 1 line
2.8.2.123.gb4ad9b6.dirty
PATCH v9 3/4 pathspec: move prefix check out of the inner loop
In reply to Stefan BellerThe prefix check is not related the check of pathspec magic; also there is no code that is relevant after we'd break the loop on a match for "prefix:". So move the check before the loop and shortcircuit the outer loop.
--- pathspec.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
pathspec.c +10 -9
@@ -107,21 +107,22 @@ static void eat_long_magic(struct pathspec_item *item, const char *elt, nextat = copyfrom + len;if (!len)continue ; + +if (starts_with (copyfrom,"prefix:" )) { +char *endptr; + *pathspec_prefix =strtol (copyfrom +7 , + &endptr,10 ); +if (endptr - copyfrom != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { *magic |= pathspec_magic[i].bit;break ; } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - *pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } }if (ARRAY_SIZE (pathspec_magic) <= i) die(_("Invalid pathspec magic '%.*s' in '%s'"),
Signature · 1 line
2.8.2.123.gb4ad9b6.dirty
PATCH v9 4/4 pathspec: allow querying for attributes
In reply to Stefan BellerThe pathspec mechanism is extended via the new ":(attr:eol=input)pattern/to/match" syntax to filter paths so that it requires paths to not just match the given pattern but also have the specified attrs attached for them to be chosen.
--- Documentation/glossary-content.txt | 20 +++++ dir.c | 35 ++++++++ pathspec.c | 101 ++++++++++++++++++++++- pathspec.h | 16 ++++ t/t6134-pathspec-with-labels.sh | 163 +++++++++++++++++++++++++++++++++++++ 5 files changed, 331 insertions(+), 4 deletions(-) create mode 100755 t/t6134-pathspec-with-labels.sh
Documentation/glossary-content.txt +20 -0
@@ -384,6 +384,26 @@ full pathname may have special meaning: + Glob magic is incompatible with literal magic. +attr;; +After `attr:` comes a space separated list of "attribute +requirements", all of which must be met in order for the +path to be considered a match; this is in addition to the +usual non-magic pathspec pattern matching. + +Each of the attribute requirements for the path takes one of +these forms: + +- "`ATTR`" requires that the attribute `ATTR` must be set. + +- "`-ATTR`" requires that the attribute `ATTR` must be unset. + +- "`ATTR=VALUE`" requires that the attribute `ATTR` must be + set to the string `VALUE`. + +- "`!ATTR`" requires that the attribute `ATTR` must be + unspecified. ++ + exclude;; After a path matches any non-exclude pathspec, it will be run through all exclude pathspec (magic signature: `!`). If it
dir.c +35 -0
@@ -9,6 +9,7 @@*/ #include "cache.h" #include "dir.h" +#include "attr.h" #include "refs.h" #include "wildmatch.h" #include "pathspec.h"
@@ -215,6 +216,37 @@ int within_depth(const char *name, int namelen,return 1 ; } +static int match_attrs (const char *name,int namelen, +const struct pathspec_item *item) +{ +int i; + +git_check_attr_counted (name, namelen, item->attr_check); +for (i =0 ; i < item->attr_match_nr; i++) { +const char *value; +int matched; +enum attr_match_mode match_mode; + + value = item->attr_check->check[i].value; + match_mode = item->attr_match[i].match_mode; + +if (ATTR_TRUE (value)) + matched = (match_mode ==MATCH_SET ); +else if (ATTR_FALSE (value)) + matched = (match_mode ==MATCH_UNSET ); +else if (ATTR_UNSET (value)) + matched = (match_mode ==MATCH_UNSPECIFIED ); +else + matched = (match_mode ==MATCH_VALUE && + !strcmp (item->attr_match[i].value, value)); + +if (!matched) +return 0 ; + } + +return 1 ; +} +#define DO_MATCH_EXCLUDE 1#define DO_MATCH_DIRECTORY 2
@@ -270,6 +302,9 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix, strncmp(item->match, name - prefix, item->prefix))return 0 ; +if (item->attr_match_nr && !match_attrs (name, namelen, item)) +return 0 ; +/* If the match was just the prefix, we matched */ if (!*match)return MATCHED_RECURSIVELY ;
pathspec.c +97 -4
149 lines not on this page · raw message
pathspec.h +16 -0
@@ -32,6 +32,22 @@ struct pathspec {int len, prefix;int nowildcard_len;int flags; +int attr_match_nr; +int attr_match_alloc; +struct attr_match { +struct git_attr *attr; +char *value; +enum attr_match_mode { +MATCH_SET , +MATCH_UNSET , +MATCH_VALUE , +MATCH_UNSPECIFIED , +MATCH_NOT_UNSPECIFIED , +MATCH_SET_OR_VALUE , +INVALID_ATTR + } match_mode; + } *attr_match; +struct git_attr_check *attr_check; } *items; };
t/t6134-pathspec-with-labels.sh new file +163 -0
163 lines not on this page · raw message
Signature · 1 line
2.8.2.123.gb4ad9b6.dirty
Re: [PATCHv9 4/4] pathspec: allow querying for attributes
Documentation/glossary-content.txt full pathname may have special meaning: · line 405
Quoted 4 lines · Stefan Beller
Documentation/glossary-content.txt +4
-0+attr;; +After `attr:` comes a space separated list of "attribute +... ++
The text looks OK, but does it format well?
pathspec.c static void prefix_short_magic(struct strbuf *sb, int prefixlen, · line 118
Quoted 1 line
pathspec.c +1
-0+ attr_len =strcspn (attr,"=" );
Scanning for '=' here retains the same bug from the previous iteration where you take !VAR=VAL and silently ignore =VAL part without diagnosing the error, doesn't it?
Perhaps strlen(attr) here, and...
Quoted 12 lines
pathspec.c +12
-0+switch (*attr) { +case '!' : + am->match_mode =MATCH_UNSPECIFIED ; + attr++; + attr_len--; +break ; +case '-' : + am->match_mode =MATCH_UNSET ; + attr++; + attr_len--; +break ; +default :
pathspec.c static void prefix_short_magic(struct strbuf *sb, int prefixlen, · line 140
Quoted 10 lines
pathspec.c +10
-0+if (attr[attr_len] !='=' ) + am->match_mode =MATCH_SET ; +else { + am->match_mode =MATCH_VALUE ; + am->value =xstrdup (&attr[attr_len +1 ]); +if (strchr (am->value,'\\' )) +die (_ ("attr spec values must not contain backslashes" )); + } +break ; + }
... doing strcspn() only in default: part would be a quick fix.
Re: [PATCHv9 4/4] pathspec: allow querying for attributes
Quoted 8 lines · Junio C Hamano
Review
Quoted 4 lines · Stefan Beller
(quoted diff) +4
-0+attr;; +After `attr:` comes a space separated list of "attribute +... ++The text looks OK, but does it format well?
I didn't check this, but the remainder would look like this squashable patch.
You seem to i18ngrep for "fatal" but we are using test_must_fail for the exit status, so I am not sure if that adds much value, so the additional tests here do nto use that pattern.
pathspec.c +3 -3
@@ -115,19 +115,19 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *vaconst char *attr = si->string;struct attr_match *am = &item->attr_match[j]; - attr_len =strcspn (attr,"=" );switch (*attr) {case '!' : am->match_mode =MATCH_UNSPECIFIED ; attr++; - attr_len--; + attr_len =strlen (attr);break ;case '-' : am->match_mode =MATCH_UNSET ; attr++; - attr_len--; + attr_len =strlen (attr);break ;default : + attr_len =strcspn (attr,"=" );if (attr[attr_len] !='=' ) am->match_mode =MATCH_SET ;else {
t/t6134-pathspec-with-labels.sh +5 -0
@@ -160,4 +160,9 @@ test_expect_success 'abort on giving invalid label on the command line' ' test_i18ngrep "fatal" actual ' +test_expect_success 'abort on asking for wrong magic' ' + test_must_fail git ls-files . ":(attr:-label=foo)" && + test_must_fail git ls-files . ":(attr:!label=foo)" +' + test_done
Re: [PATCHv9 4/4] pathspec: allow querying for attributes
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Junio C Hamano
Review
Quoted 4 lines · Stefan Beller
(quoted diff) +4
-0+attr;; +After `attr:` comes a space separated list of "attribute +... ++The text looks OK, but does it format well?
Quoted 2 lines
I didn't check this, but the remainder would look like this squashable patch.
I checked and it looks wrong. the "exclude" section is indented below the new attr section
fix is:
--8<--
(quoted diff) +0 -0
b/Documentation/glossary-content.txt
index e06520b..181f52e 100644
Documentation/glossary-content.txt +1 -2
@@ -389,7 +389,7 @@ After `attr:` comes a space separated list of "attribute requirements", all of which must be met in order for the path to be considered a match; this is in addition to the usual non-magic pathspec pattern matching. - ++ Each of the attribute requirements for the path takes one of these forms: --8<--
I can resend with your proposed fixes as well.
Thanks,
Stefan
Quoted 46 lines
You seem to i18ngrep for "fatal" but we are using
test_must_failfor the exit status, so I am not sure if that adds much value, so the additional tests here do nto use that pattern.pathspec.c +3
-3@@ -115,19 +115,19 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *vaconst char *attr = si->string;struct attr_match *am = &item->attr_match[j]; - attr_len =strcspn (attr,"=" );switch (*attr) {case '!' : am->match_mode =MATCH_UNSPECIFIED ; attr++; - attr_len--; + attr_len =strlen (attr);break ;case '-' : am->match_mode =MATCH_UNSET ; attr++; - attr_len--; + attr_len =strlen (attr);break ;default : + attr_len =strcspn (attr,"=" );if (attr[attr_len] !='=' ) am->match_mode =MATCH_SET ;else {t/t6134-pathspec-with-labels.sh +5
-0@@ -160,4 +160,9 @@ test_expect_success 'abort on giving invalid label on the command line' ' test_i18ngrep "fatal" actual ' +test_expect_success 'abort on asking for wrong magic' ' + test_must_fail git ls-files . ":(attr:-label=foo)" && + test_must_fail git ls-files . ":(attr:!label=foo)" +' + test_done
Re: [PATCHv9 4/4] pathspec: allow querying for attributes
Quoted 4 lines · Stefan Beller
I checked and it looks wrong. the "exclude" section is indented below the new attr section
...
I can resend with your proposed fixes as well.
If you do so, please make sure that the way tests check for error condition are consistent. I personally do not think it adds any value to grep for "fatal", but I do not mind if you adjusted them to go the same test_i18ngrep route if you think it does (and if you agree that grepping is not necessary, please do not forget to update the tests you had that use test_i18ngrep).
Thanks.