Skip to contentSkip to conversation

pathspec: allow querying for attributes

PATCH In review Stefan Beller 2 participants 2 messages Jun 16, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 pathspec: allow querying for attributes

The 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.

---

Junio, please replace the last patch of sb/pathspec-label with this one.

diff to last round:
# diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
# index e06520b..181f52e 100644
# --- a/Documentation/glossary-content.txt
# +++ b/Documentation/glossary-content.txt
# @@ -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:
#
# diff --git a/pathspec.c b/pathspec.c
# index 693a5e7..0a02255 100644
# --- a/pathspec.c
# +++ b/pathspec.c
# @@ -115,19 +115,19 @@ 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];
#
# - 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 {
# diff --git a/t/t6134-pathspec-with-labels.sh b/t/t6134-pathspec-with-labels.sh
# index 5da1a63..a5c9632 100755
# --- a/t/t6134-pathspec-with-labels.sh
# +++ b/t/t6134-pathspec-with-labels.sh
# @@ -40,8 +40,7 @@ test_expect_success 'setup a tree' '
# '
#
# test_expect_success 'pathspec with no attr' '
# - test_must_fail git ls-files ":(attr:)" 2>actual &&
# - test_i18ngrep fatal actual
# + test_must_fail git ls-files ":(attr:)"
# '
#
# test_expect_success 'pathspec with labels and non existent .gitattributes' '
# @@ -156,8 +155,12 @@ test_expect_success 'check label excluding other labels' '
# '
#
# test_expect_success 'abort on giving invalid label on the command line' '
# - test_must_fail git ls-files . ":(attr:☺)" 2>actual &&
# - test_i18ngrep "fatal" actual
# + test_must_fail git ls-files . ":(attr:☺)"
# +'
# +
# +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

 Documentation/glossary-content.txt |  20 +++++
 dir.c                              |  35 ++++++++
 pathspec.c                         | 101 +++++++++++++++++++++-
 pathspec.h                         |  16 ++++
 t/t6134-pathspec-with-labels.sh    | 166 +++++++++++++++++++++++++++++++++++++
 5 files changed, 334 insertions(+), 4 deletions(-)
 create mode 100755 t/t6134-pathspec-with-labels.sh
Documentation/glossary-content.txt +20 -0
index cafc284..181f52e 100644
@@ -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
index 996653b..fc071af 100644
@@ -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
index 0c11262..5308137 100644
@@ -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 +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.8.2.123.gb4ad9b6.dirty