Skip to content

pathspec magic extension to search for attributes

Stefan Beller git 3 versions In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v9 Latest4 patches 4 repliesIn review
  2. v85 patches 16 repliesSuperseded
  3. v75 patches 6 repliesSuperseded

Patches in v9 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4Documentation: fix a typoDocumentation/gitattributes.txt+1 -10 replies
2/4pathspec: move long magic parsing out of prefix_pathspecpathspec.c+47 -370 replies
3/4pathspec: move prefix check out of the inner looppathspec.c+10 -90 replies
4/4pathspec: allow querying for attributesDocumentation/glossary-content.txt dir.c pathspec.c +2 more+331 -44 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v9 yet.

Read the v9 thread

Cover letter [PATCHv9 0/4] 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
index aa9f220..e06520b 100644
@@ -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
index f60a503..fc071af 100644
@@ -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
index b795a9c..693a5e7 100644
@@ -115,34 +115,38 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
 		const 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
index 465a1f0..d3809a1 100644
@@ -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