Skip to contentSkip to conversation

pathspec labels [WAS: submodule groups]

PATCH Superseded Stefan Beller 2 participants 17 messages Jun 16, 2016 Documentation

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover pathspec labels [WAS: submodule groups]

I am happy with the results of the label experiments. (Mainly writing the tests)

Quoted 4 lines
Quoted 1 line

Isn't the former be "label="

I do not know what you mean by the latter. I would understand "pretend this has all the labels under the sun", though.

See the new tests what I mean. It cleared up for me as well when writing them.

Quoted 3 lines

I am wondering where we should sanity check (and die, pointing out an error in .gitattributes file). Is this function a good place to reject TRUE and FALSE?

TRUE and FALSE are solved issues in this series. However we still want funny characters not be used. What if we want to use them as special characters in the future? (e.g. 'label*' for 'labelA' or 'labelB' or label{A,B} for the same)

I think it is too late in the matching, so ideally we would check label names when they are assigned (i.e. while editing the .gitattributes file) That is not in our control, though.

And during use of match_pathspec it is already to late to die(...) for bad labels, because the labels may not be in control for the user (She just cloned the repository and wants to inspect certain files, which are labeled "$*&^".) So for now we just output a warning for these cases, in the hope that content creatos upstream respect the recommendation for labels.

Feedback welcome!

Thanks,
Stefan

Stefan Beller (5):
Documentation: fix a typo
Documentation: correct typo in example for querying attributes
pathspec: move long magic parsing out of prefix_pathspec
pathspec: move prefix check out of the inner loop
pathspec: record labels

 Documentation/gitattributes.txt               |  14 ++-
 Documentation/glossary-content.txt            |   4 +
 Documentation/technical/api-gitattributes.txt |   2 +-
 dir.c                                         |  50 ++++++++
 pathspec.c                                    | 110 +++++++++++------
 pathspec.h                                    |   1 +
 t/t6134-pathspec-with-labels.sh               | 164 ++++++++++++++++++++++++++
 7 files changed, 305 insertions(+), 40 deletions(-)
 create mode 100755 t/t6134-pathspec-with-labels.sh
Signature · 1 line
2.8.2.401.gb7348ac.dirty
#1 lore raw

PATCH v2 1/5 Documentation: fix a typo

---
 Documentation/gitattributes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/gitattributes.txt +1 -1
index e3b1de8..af2c682 100644
@@ -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.401.g6647087.dirty
#2 lore raw

PATCH v2 2/5 Documentation: correct typo in example for querying attributes

In reply to Stefan Beller

The introductory text of the example has a typo in the specification which makes it harder to follow that example.

---
 Documentation/technical/api-gitattributes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/technical/api-gitattributes.txt +1 -1
index 2602668..36fc9af 100644
@@ -61,7 +61,7 @@ Querying Specific Attributes
 Example
 -------
 
-To see how attributes "crlf" and "indent" are set for different paths.
+To see how attributes "crlf" and "ident" are set for different paths.
 
 . Prepare an array of `struct git_attr_check` with two elements (because
   we are checking two attributes).  Initialize their `attr` member with
Signature · 1 line
2.8.2.401.g6647087.dirty
#3 lore raw

PATCH v2 3/5 pathspec: move long magic parsing out of prefix_pathspec

In reply to Stefan Beller

prefix_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
index c9e9b6c..eba37c2 100644
@@ -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, &copyfrom, &long_magic_end);
 	} else {
 		/* shorthand */
 		for (copyfrom = elt + 1;
Signature · 1 line
2.8.2.401.g6647087.dirty
#4 lore raw

PATCH v2 4/5 pathspec: move prefix check out of the inner loop

In reply to Stefan Beller

The 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
index eba37c2..4dff252 100644
@@ -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.401.g6647087.dirty
#5 lore raw

PATCH v2 5/5 pathspec: record labels

In reply to Stefan Beller

Labels were originally designed to manage large amount of submodules, the discussion steered this in a more general direction, such that other files can be labeled as well.

Labels are meant to describe arbitrary set of files, which is not described via the tree layout.

---
 Documentation/gitattributes.txt    |  12 +++
 Documentation/glossary-content.txt |   4 +
 dir.c                              |  50 +++++++++++
 pathspec.c                         |  64 ++++++++++++++-
 pathspec.h                         |   1 +
 t/t6134-pathspec-with-labels.sh    | 164 +++++++++++++++++++++++++++++++++++++
 6 files changed, 291 insertions(+), 4 deletions(-)
 create mode 100755 t/t6134-pathspec-with-labels.sh
Documentation/gitattributes.txt +12 -0
index af2c682..7fd764f 100644
@@ -906,6 +906,18 @@ If this attribute is not set or has an invalid value, the value of the
 (See linkgit:git-config[1]).
 
 
+
+Attaching labels to path
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+`label`
+^^^^^^^^^^
+By the value of this attribute you can specify a list of arbitrary strings
+to be attached to a path as labels. These labels can be used for
+easier paths matching using pathspecs (linkgit:gitglossary[1]).
+It is recommended to use only alphanumeric characters, dashes and
+underscores for the labels.
+
 USING MACRO ATTRIBUTES
 ----------------------
 
Documentation/glossary-content.txt +4 -0
index 8ad29e6..f990017 100644
@@ -384,6 +384,10 @@ full pathname may have special meaning:
 +
 Glob magic is incompatible with literal magic.
 
+label:<white space separated list>;;
+	Additionally to matching the pathspec, the path must habe a 'label'
+	attribute having set all labels listed here.
+
 exclude;;
 	After a path matches any non-exclude pathspec, it will be run
 	through all exclude pathspec (magic signature: `!`). If it
dir.c +50 -0

68 lines not on this page · raw message

pathspec.c +60 -4

106 lines not on this page · raw message

pathspec.h +1 -0
index 0c11262..12ed2b8 100644
@@ -32,6 +32,7 @@ struct pathspec {
 		int len, prefix;
 		int nowildcard_len;
 		int flags;
+		struct string_list *labels;
 	} *items;
 };
 
t/t6134-pathspec-with-labels.sh new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
2.8.2.401.g6647087.dirty
#6 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 3 lines · Stefan Beller

Labels were originally designed to manage large amount of submodules, the discussion steered this in a more general direction, such that other files can be labeled as well.

s/other files/any path/.

Quoted 2 lines

Labels are meant to describe arbitrary set of files, which is not described via the tree layout.

Let's avoid "are meant to", which is merely to give you an excuse to say "it was meant to ... but the implementation did not quite achieve that goal".

The "label" attribute can be attached to paths, and the pathspec mechanism is extended via the new ":(label=X)pattern/to/match" syntax to filter paths so that it requires paths to not just match the given pattern but also have the specified labels attached for them to be chosen.

or something?

Documentation/gitattributes.txt If this attribute is not set or has an invalid value, the value of the · line 919

Quoted 10 lines
Documentation/gitattributes.txt +10 -0
+Attaching labels to path
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+`label`
+^^^^^^^^^^
+By the value of this attribute you can specify a list of arbitrary strings
+to be attached to a path as labels. These labels can be used for
+easier paths matching using pathspecs (linkgit:gitglossary[1]).
+It is recommended to use only alphanumeric characters, dashes and
+underscores for the labels.

Make this recomendation stronger to requirement. It is always possible to loosen it later, but once we allow random things even with a warning, it gets impossible to take them back. Users will say "Even though we got a warning, it used to correctly filter; now Git is broken and my setup does not work."

Documentation/glossary-content.txt full pathname may have special meaning: · line 389

Quoted 11 lines
Documentation/glossary-content.txt +3 -0
index 8ad29e6..f990017 100644
@@ -384,6 +384,10 @@ full pathname may have special meaning:
 +
 Glob magic is incompatible with literal magic.
 
+label:<white space separated list>;;
+	Additionally to matching the pathspec, the path must habe a 'label'
+	attribute having set all labels listed here.

s/habe/have/

Documentation/glossary-content.txt full pathname may have special meaning: · line 391

Quoted 2 lines
Documentation/glossary-content.txt +2 -0
+	if (ATTR_TRUE(check.value))
+		ret = 1; /* has all the labels */

So this is "pretend that I have all the labels under the sun".

Documentation/glossary-content.txt full pathname may have special meaning: · line 393

Quoted 2 lines
Documentation/glossary-content.txt +2 -0
+	else if (ATTR_FALSE(check.value))
+		ret = 0; /* has no labels */

I do not see a value in this. What difference does it have compared to having a "label=" that explicitly says "I do not have one"? What is your answer to an end-user question "When should I say 'label=' and when should I say '!label'?"

Just forbid it for now; we may find a better use for it later.

Documentation/glossary-content.txt full pathname may have special meaning:

Quoted 6 lines
Documentation/glossary-content.txt +6 -0
+	else if (ATTR_UNSET(check.value))
+		/*
+		 * If no label was specified this matches, otherwise
+		 * there is a missing label.
+		 */
+		ret = (required_labels->nr > 0) ? 0 : 1;

Hmm, I can see that this is making things more complicated to explain and understand, but I cannot see what the expected use case is.

Unless there is any compelling use case, I'd say we should forbid it for now.

Documentation/glossary-content.txt

Quoted 5 lines
Documentation/glossary-content.txt +5 -0
+	else {
+		struct string_list_item *si;
+		struct string_list attr_labels = STRING_LIST_INIT_DUP;
+		string_list_split(&attr_labels, check.value, ',', -1);
+		string_list_sort(&attr_labels);

Filter out a non-compliant label values here, so that they are ignored from day one. That way you would not have to deal with "I know I got the warning, but it used to work and you broke it" later.

Quoted 10 lines
Documentation/glossary-content.txt +10 -0
+		ret = 1;
+		for_each_string_list_item(si, required_labels) {
+			if (!string_list_has_string(&attr_labels, si->string)) {
+				ret = 0;
+				break;
+			}
+		}
+		string_list_clear(&attr_labels, 0);
+	}
+

Documentation/glossary-content.txt

Quoted 10 lines
Documentation/glossary-content.txt +9 -0
+	return ret;
+}
 
+static void validate_label_name(const char *label)
+{
+	if (check_valid_label_name(label))
+		warning(_("Label '%s' discouraged. Recommended label names start "
+			"with an alphabetic character and use only alphanumeric "
+			"characters, dashes and underscores."), label);
+}

Instead of returning void, parrot the return value from check_valid_label_name().

Documentation/glossary-content.txt

Quoted 1 line
Documentation/glossary-content.txt +1 -0
+		/* The label token may have no argument, so no trailing ':' */

Why close the door for future pathspec magic "labelfoo" by being unnecessarily[*1*] lenient?

[*1*] Does ":(label)" mean anything useful, and is there a good
reason why it should behave differently from ":(label:)"?

Unless there is a good reason why users would want ":(label)", I'd say we should make it a syntax error.

Documentation/glossary-content.txt

Quoted 9 lines
Documentation/glossary-content.txt +9 -0
+		if (skip_prefix(copyfrom, "label", &body)) {
+			struct strbuf sb = STRBUF_INIT;
+			skip_prefix(body, ":", &body);
+			strbuf_add(&sb, body, nextat - body);
+			if (!item->labels) {
+				item->labels = xmalloc(sizeof(*item->labels));
+				string_list_init(item->labels, 1);
+			} else
+				die(_("multiple labels not supported in pathspec magic"));

I am NOT suggesting to make this enhancement in the prototype to allow us experiment with submodule selection use case, but this is an obvious place to allow

	:(label=A B):(label=C D)

to mean ((A & B) | (C & D)) by making item->labels an array of set of labels.

And no, I do not think arbitrary boolean expression is too complex to understand to the end-users, especially if we have to stay within the pathspec magic syntax. And my gut feeling is that it is not worth it to support anything more complex than "OR of these ANDed ones".

Documentation/glossary-content.txt

Quoted 4 lines
Documentation/glossary-content.txt +4 -0
+			string_list_split(item->labels, sb.buf, ' ', -1);
+			string_list_remove_empty_items(item->labels, 0);
+			strbuf_release(&sb);
+			continue;

The data structure to record the "required labels" is shared knowledge between this function and the has_all_labels() and nobody else knows it is done with string_list, so I think this is a good balance between expediency and future optimization possibilities (I am anticipating that linear search of string list would be found as performance bottleneck).

Documentation/glossary-content.txt void parse_pathspec(struct pathspec *pathspec, · line 497

Quoted 9 lines
Documentation/glossary-content.txt +5 -0
@@ -447,6 +490,12 @@ void parse_pathspec(struct pathspec *pathspec,
 		if (item[i].nowildcard_len < item[i].len)
 			pathspec->has_wildcard = 1;
 		pathspec->magic |= item[i].magic;
+
+		if (item[i].labels) {
+			struct string_list_item *si;
+			for_each_string_list_item(si, item[i].labels)
+				validate_label_name(si->string);

Reject a request to use a label that does not validate here.

The series is surprisingly simple and was a pleasant read (I no longer recall how this compares with the earlier change to do this as [submodule "x"] label=X). Especially, given that the first two patches are unrelated clean-ups that can be split out, this now consists of merely 3 patches, each of which is an easily digestable size.

#7 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Stefan Beller

Labels were originally designed to manage large amount of submodules, the discussion steered this in a more general direction, such that other files can be labeled as well.

s/other files/any path/.

Quoted 2 lines

Labels are meant to describe arbitrary set of files, which is not described via the tree layout.

Quoted 6 lines

Let's avoid "are meant to", which is merely to give you an excuse to say "it was meant to ... but the implementation did not quite achieve that goal".

The "label" attribute can be attached to paths, and the pathspec mechanism is extended via the new ":(label=X)pattern/to/match"

I wasn't sure about whether we want to have '=' or ':' as the separator of "label" and its values. The prefix which is passed around uses a colon instead of equal sign, so I thought consistency would be neat there. But as that is only internal, we can be inconsistent to a new public feature, so '=' seems better for the labeling system.

Quoted 6 lines

syntax to filter paths so that it requires paths to not just match the given pattern but also have the specified labels attached for them to be chosen.

or something?

Quoted 16 lines

Documentation/gitattributes.txt If this attribute is not set or has an invalid value, the value of the · line 919

Quoted 10 lines
Documentation/gitattributes.txt +10 -0
+Attaching labels to path
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+`label`
+^^^^^^^^^^
+By the value of this attribute you can specify a list of arbitrary strings
+to be attached to a path as labels. These labels can be used for
+easier paths matching using pathspecs (linkgit:gitglossary[1]).
+It is recommended to use only alphanumeric characters, dashes and
+underscores for the labels.

Make this recomendation stronger to requirement. It is always possible to loosen it later, but once we allow random things even with a warning, it gets impossible to take them back. Users will say "Even though we got a warning, it used to correctly filter; now Git is broken and my setup does not work."

I'll change the text here with s/recommended/required/ and error out for that.

But still we do not enforce it early enough. Some crazy upstream may add some labels which do not follow the requirements and then tell downstream to run a command like `git foo "(label=!@#$)". and then downstream curses at upstream as well as Git.

Quoted 2 lines

s/habe/have/

done

Quoted 9 lines

dir.c int within_depth(const char *name, int namelen, · line 231

Quoted 3 lines
dir.c +2 -0
+     if (ATTR_TRUE(check.value))
+             ret = 1; /* has all the labels */

So this is "pretend that I have all the labels under the sun".

dir.c int within_depth(const char *name, int namelen, · line 233

Quoted 2 lines
dir.c +2 -0
+     else if (ATTR_FALSE(check.value))
+             ret = 0; /* has no labels */
Quoted 6 lines

I do not see a value in this. What difference does it have compared to having a "label=" that explicitly says "I do not have one"? What is your answer to an end-user question "When should I say 'label=' and when should I say '!label'?"

Just forbid it for now; we may find a better use for it later.

I don't think we want to die in that code as it is in the data already. If we were to die in that case, you cannot query any path with labels any more if asking for any kind of label.

Quoted 8 lines

dir.c int within_depth(const char *name, int namelen, · line 239

Quoted 7 lines
dir.c +6 -0
+     else if (ATTR_UNSET(check.value))
+             /*
+              * If no label was specified this matches, otherwise
+              * there is a missing label.
+              */
+             ret = (required_labels->nr > 0) ? 0 : 1;
Quoted 6 lines

Hmm, I can see that this is making things more complicated to explain and understand, but I cannot see what the expected use case is.

Unless there is any compelling use case, I'd say we should forbid it for now.

We need to allow the UNSET case, as otherwise you'd need to label any path if using labels? In the next iteration I'll go with

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value))
die(_("Label attributes must not be unset"));
else if (ATTR_UNSET(check.value))
ret = 0; /* has no labels */
else {
...

Quoted 10 lines

dir.c int within_depth(const char *name, int namelen, · line 244

Quoted 6 lines
dir.c +5 -0
+     else {
+             struct string_list_item *si;
+             struct string_list attr_labels = STRING_LIST_INIT_DUP;
+             string_list_split(&attr_labels, check.value, ',', -1);
+             string_list_sort(&attr_labels);

Filter out a non-compliant label values here, so that they are ignored from day one. That way you would not have to deal with "I know I got the warning, but it used to work and you broke it" later.

So you're saying we should not die(...) but just ignore those labels?

Quoted 25 lines
Quoted 11 lines
dir.c +10 -0
+             ret = 1;
+             for_each_string_list_item(si, required_labels) {
+                     if (!string_list_has_string(&attr_labels, si->string)) {
+                             ret = 0;
+                             break;
+                     }
+             }
+             string_list_clear(&attr_labels, 0);
+     }
+

dir.c int within_depth(const char *name, int namelen,

Quoted 10 lines
dir.c +9 -0
+     return ret;
+}
 
+static void validate_label_name(const char *label)
+{
+     if (check_valid_label_name(label))
+             warning(_("Label '%s' discouraged. Recommended label names start "
+                     "with an alphabetic character and use only alphanumeric "
+                     "characters, dashes and underscores."), label);
+}

Instead of returning void, parrot the return value from check_valid_label_name().

Quoted 7 lines
Quoted 1 line

+ /* The label token may have no argument, so no trailing ':' */

Why close the door for future pathspec magic "labelfoo" by being unnecessarily[*1*] lenient?

[*1*] Does ":(label)" mean anything useful, and is there a good
reason why it should behave differently from ":(label:)"?

ok fixed.

Quoted 20 lines

Unless there is a good reason why users would want ":(label)", I'd say we should make it a syntax error.

pathspec.c static void eat_long_magic(struct pathspec_item *item, const char *elt, · line 156

Quoted 9 lines
pathspec.c +9 -0
+             if (skip_prefix(copyfrom, "label", &body)) {
+                     struct strbuf sb = STRBUF_INIT;
+                     skip_prefix(body, ":", &body);
+                     strbuf_add(&sb, body, nextat - body);
+                     if (!item->labels) {
+                             item->labels = xmalloc(sizeof(*item->labels));
+                             string_list_init(item->labels, 1);
+                     } else
+                             die(_("multiple labels not supported in pathspec magic"));

I am NOT suggesting to make this enhancement in the prototype to allow us experiment with submodule selection use case, but this is an obvious place to allow

        :(label=A B):(label=C D)
Quoted 8 lines

to mean ((A & B) | (C & D)) by making item->labels an array of set of labels.

And no, I do not think arbitrary boolean expression is too complex to understand to the end-users, especially if we have to stay within the pathspec magic syntax. And my gut feeling is that it is not worth it to support anything more complex than "OR of these ANDed ones".

That makes sense.

Quoted 13 lines

pathspec.c static void eat_long_magic(struct pathspec_item *item, const char *elt, · line 160

Quoted 5 lines
pathspec.c +4 -0
+                     string_list_split(item->labels, sb.buf, ' ', -1);
+                     string_list_remove_empty_items(item->labels, 0);
+                     strbuf_release(&sb);
+                     continue;

The data structure to record the "required labels" is shared knowledge between this function and the has_all_labels() and nobody else knows it is done with string_list, so I think this is a good balance between expediency and future optimization possibilities (I am anticipating that linear search of string list would be found as performance bottleneck).

Quoted 11 lines

pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 497

Quoted 9 lines
pathspec.c +5 -0
@@ -447,6 +490,12 @@ void parse_pathspec(struct pathspec *pathspec,
              if (item[i].nowildcard_len < item[i].len)
                      pathspec->has_wildcard = 1;
              pathspec->magic |= item[i].magic;
+
+             if (item[i].labels) {
+                     struct string_list_item *si;
+                     for_each_string_list_item(si, item[i].labels)
+                             validate_label_name(si->string);

Reject a request to use a label that does not validate here.

done.

#8 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 9 lines · Stefan Beller
Quoted 6 lines

Let's avoid "are meant to", which is merely to give you an excuse to say "it was meant to ... but the implementation did not quite achieve that goal".

The "label" attribute can be attached to paths, and the pathspec mechanism is extended via the new ":(label=X)pattern/to/match"

I wasn't sure about whether we want to have '=' or ':' as the separator of "label" and its values. ...

Oh, sorry, the above wasn't a suggestion to change : to = at all. I was merely being sloppy in the details that is irrelevant to my main point (which is, it is better spend the bits you spent for "meant to" instead for saying clearly what it does).

Quoted 2 lines

... But as that is only internal, we can be inconsistent to a new public feature, so '=' seems better for the labeling system.

I can buy that, too. Good that my sloppy wording helped you think about it further ;-).

Quoted 4 lines

But still we do not enforce it early enough. Some crazy upstream may add some labels which do not follow the requirements and then tell downstream to run a command like `git foo "(label=!@#$)". and then downstream curses at upstream as well as Git.

That is why it is "warn and ignore", not "die".

Quoted 8 lines
Quoted 8 lines

dir.c int within_depth(const char *name, int namelen, · line 231

Quoted 2 lines
dir.c +2 -0
+     if (ATTR_TRUE(check.value))
+             ret = 1; /* has all the labels */

So this is "pretend that I have all the labels under the sun".

Quoted 2 lines
dir.c +2 -0
+     else if (ATTR_FALSE(check.value))
+             ret = 0; /* has no labels */
Quoted 8 lines
Quoted 6 lines

I do not see a value in this. What difference does it have compared to having a "label=" that explicitly says "I do not have one"? What is your answer to an end-user question "When should I say 'label=' and when should I say '!label'?"

Just forbid it for now; we may find a better use for it later.

I don't think we want to die in that code as it is in the data already.

Is it? I think this is coming from the command line pathspec magic, not from .gitattributes file recorded in the tree.

Quoted 2 lines

We need to allow the UNSET case, as otherwise you'd need to label any path if using labels?

You do need UNSET (roughly, "no label is mentioned for the path").

If I want to say "Everything under Documentation/ and also things with label=doc", I'd say

git cmd "Documentation/" ":(label=doc)"

and no path in Documentation/ need any label, i.e. for them, the labels are unset. They will not be caught with ":(label=doc)" because they are not set, but that is OK.

FALSE is different from UNSET. It needs an explicit mention, i.e.

	*.doc	doc
        false.doc	-doc

What's the difference between saying "-doc" and not saying anything? If you really want to explicitly say "doc attribute is unset for this path (perhaps because you may have other patterns that set the doc elsewhere), you would say "!doc", and you already have code for that.

Quoted 4 lines

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value))
die(_("Label attributes must not be unset"));

The message is wrong. You are dying because the user explicitly set it to false, not because the user made it unset.

Quoted 8 lines

else if (ATTR_UNSET(check.value))
ret = 0; /* has no labels */

Quoted 5 lines
Quoted 4 lines
dir.c +4 -0
+             struct string_list_item *si;
+             struct string_list attr_labels = STRING_LIST_INIT_DUP;
+             string_list_split(&attr_labels, check.value, ',', -1);
+             string_list_sort(&attr_labels);
Quoted 5 lines
Quoted 3 lines

Filter out a non-compliant label values here, so that they are ignored from day one. That way you would not have to deal with "I know I got the warning, but it used to work and you broke it" later.

So you're saying we should not die(...) but just ignore those labels?

Do not die() but warn-and-ignore when you see funnies in .gitattributes; do die() if you see funnies in pathspec magic.

Quoted 10 lines
Quoted 10 lines

I am NOT suggesting to make this enhancement in the prototype to allow us experiment with submodule selection use case, but this is an obvious place to allow

        :(label=A B):(label=C D)

to mean ((A & B) | (C & D)) by making item->labels an array of set of labels.

And no, I do not think arbitrary boolean expression is too complex

s/do not/do/

Quoted 6 lines
Quoted 4 lines

to understand to the end-users, especially if we have to stay within the pathspec magic syntax. And my gut feeling is that it is not worth it to support anything more complex than "OR of these ANDed ones".

That makes sense.

Thanks.

#9 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 23 lines · Junio C Hamano
Quoted 9 lines · Stefan Beller
Quoted 6 lines

Let's avoid "are meant to", which is merely to give you an excuse to say "it was meant to ... but the implementation did not quite achieve that goal".

The "label" attribute can be attached to paths, and the pathspec mechanism is extended via the new ":(label=X)pattern/to/match"

I wasn't sure about whether we want to have '=' or ':' as the separator of "label" and its values. ...

Oh, sorry, the above wasn't a suggestion to change : to = at all. I was merely being sloppy in the details that is irrelevant to my main point (which is, it is better spend the bits you spent for "meant to" instead for saying clearly what it does).

Quoted 2 lines

... But as that is only internal, we can be inconsistent to a new public feature, so '=' seems better for the labeling system.

I can buy that, too. Good that my sloppy wording helped you think about it further ;-).

Quoted 6 lines
Quoted 4 lines

But still we do not enforce it early enough. Some crazy upstream may add some labels which do not follow the requirements and then tell downstream to run a command like `git foo "(label=!@#$)". and then downstream curses at upstream as well as Git.

That is why it is "warn and ignore", not "die".

So "warn and ignore" for data from .gitattributes and die for commandline arguments? That makes sense.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

dir.c int within_depth(const char *name, int namelen, · line 231

Quoted 3 lines
dir.c +2 -0
+     if (ATTR_TRUE(check.value))
+             ret = 1; /* has all the labels */

So this is "pretend that I have all the labels under the sun".

dir.c int within_depth(const char *name, int namelen, · line 233

Quoted 2 lines
dir.c +2 -0
+     else if (ATTR_FALSE(check.value))
+             ret = 0; /* has no labels */

I do not see a value in this. What difference does it have compared to having a "label=" that explicitly says "I do not have one"? What is your answer to an end-user question "When should I say 'label=' and when should I say '!label'?"

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Just forbid it for now; we may find a better use for it later.

I don't think we want to die in that code as it is in the data already.

Is it? I think this is coming from the command line pathspec magic, not from .gitattributes file recorded in the tree.

The 'check.value' is coming from the .gitattributes.

The 'required_labels' string list comes from the command line. ("The command line requires these labels")

Quoted 6 lines
Quoted 3 lines

We need to allow the UNSET case, as otherwise you'd need to label any path if using labels?

You do need UNSET (roughly, "no label is mentioned for the path").

Quoted 8 lines

If I want to say "Everything under Documentation/ and also things with label=doc", I'd say

git cmd "Documentation/" ":(label=doc)"

and no path in Documentation/ need any label, i.e. for them, the labels are unset. They will not be caught with ":(label=doc)" because they are not set, but that is OK.

right. that is what the end user should be able to do. That also means, we need to handle files with UNSET labels (all under Documentation/)

Quoted 5 lines

FALSE is different from UNSET. It needs an explicit mention, i.e.

*.doc doc
false.doc -doc

and this is in the .gitattribues, so warn-and-ignore and by ignore we mean treat it as UNSET?

Quoted 7 lines

What's the difference between saying "-doc" and not saying anything? If you really want to explicitly say "doc attribute is unset for this path (perhaps because you may have other patterns that set the doc elsewhere), you would say "!doc", and you already have code for that.

Quoted 7 lines
Quoted 4 lines

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value))
die(_("Label attributes must not be unset"));

The message is wrong. You are dying because the user explicitly set it to false, not because the user made it unset.

Ok, so here is the warn-and-ignore code:

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value)) {
warning(_("Path '%s': Label must not be false. Treat
as if no label was set"), path);
ret = 0;
else if (ATTR_UNSET(check.value))
ret = 0; /* has no labels */
else {

Thanks,
Stefan

#15 lore raw

Re: [PATCH 5/5] pathspec: record labels

In reply to Junio C Hamano
Quoted 8 lines

I am NOT suggesting to make this enhancement in the prototype to allow us experiment with submodule selection use case, but this is an obvious place to allow

        :(label=A B):(label=C D)

to mean ((A & B) | (C & D)) by making item->labels an array of set of labels.

This is what already works with the series. Or rather:

    ":(label=A B)" ":(label=C D)"

works as you would expect for (A&B) | (C&D).

So I am a bit hesitant to replace the string list by an array of stringlists or such, as the future enhancement can also do that?

The enhancement may bring in more expressions into the label string, so it may even parse that string into a tree for Lexicographical order instead of just using an array of lists.

Quoted 19 lines

And no, I do not think arbitrary boolean expression is too complex to understand to the end-users, especially if we have to stay within the pathspec magic syntax. And my gut feeling is that it is not worth it to support anything more complex than "OR of these ANDed ones".

pathspec.c static void eat_long_magic(struct pathspec_item *item, const char *elt, · line 160

Quoted 4 lines
pathspec.c +4 -0
+                     string_list_split(item->labels, sb.buf, ' ', -1);
+                     string_list_remove_empty_items(item->labels, 0);
+                     strbuf_release(&sb);
+                     continue;

The data structure to record the "required labels" is shared knowledge between this function and the has_all_labels() and nobody else knows it is done with string_list, so I think this is a good balance between expediency and future optimization possibilities (I am anticipating that linear search of string list would be found as performance bottleneck).

#10 lore raw

Re: [PATCH 5/5] pathspec: record labels

In reply to Stefan Beller
Quoted 2 lines · Stefan Beller

So "warn and ignore" for data from .gitattributes and die for commandline arguments? That makes sense.

Yes.

On the "command line" front, because we may want to give different meanings to these two entries in the future:

:(label=-doc)Documentation/
:(label=!doc)Documentation/

we should diagnose -doc (FALSE) as an error, not treating it as the same as !doc (UNSET). And we should warn and ignore -doc (FALSE) in .gitattributes. Yes, ignoring it would be more or less equivalent to treating it as UNSET, but because we may use -doc (FALSE) for a better purpose later, we should still warn.

Quoted 9 lines

Ok, so here is the warn-and-ignore code:

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value)) {
warning(_("Path '%s': Label must not be false. Treat
as if no label was set"), path);

                ret = 0;

s/Treat as if .../The -label may be used differently in future versions of Git, so do not use it/;

But if we are going in the direction of :(attr:crlf=auto), all this discussion is moot, isn't it? I haven't formed a firm opinion on this, but it sure does sound tempting, doesn't it?

#11 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 5 lines · Junio C Hamano
Quoted 2 lines · Stefan Beller

So "warn and ignore" for data from .gitattributes and die for commandline arguments? That makes sense.

Quoted 7 lines

Yes.

On the "command line" front, because we may want to give different meanings to these two entries in the future:

:(label=-doc)Documentation/
:(label=!doc)Documentation/

Yes but both of these already errors out with:

Label -doc': Label names must start with an alphabetic character and use only alphanumeric characters, dashes and underscores.
fatal: Labels in the wrong syntax are prohibited.

The command line arguments are not parsed via the attr system. Only data from .gitattributes are parsed via the attr system.

I can add tests for those to fail though.

Quoted 17 lines

we should diagnose -doc (FALSE) as an error, not treating it as the same as !doc (UNSET). And we should warn and ignore -doc (FALSE) in .gitattributes. Yes, ignoring it would be more or less equivalent to treating it as UNSET, but because we may use -doc (FALSE) for a better purpose later, we should still warn.

Quoted 9 lines

Ok, so here is the warn-and-ignore code:

if (ATTR_TRUE(check.value))
ret = 1; /* has all the labels */
else if (ATTR_FALSE(check.value)) {
warning(_("Path '%s': Label must not be false. Treat
as if no label was set"), path);

                ret = 0;
Quoted 2 lines

s/Treat as if .../The -label may be used differently in future versions of Git, so do not use it/;

"... but for now Git treats it as if it is not set at all" is a valuable information to the user, still?

That code path is only used for data coming from .gitattributes, so we can bike shed the best warning message.

Quoted 5 lines

But if we are going in the direction of :(attr:crlf=auto), all this discussion is moot, isn't it? I haven't formed a firm opinion on this, but it sure does sound tempting, doesn't it?

That direction sounds scary as people may use any sort of labels, so we can no longer add labels later on sanely.

    :(attr:random-attr=foo)

should also fall into the "warn and ignore" space. We only want to allow

    :(attr:known-attr)
    :(attr:label=..)

instead of label= we may want to allow a little bit more, such as abbreviated 'l='
or 'group=', but not any sort of labeling.

#12 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 2 lines · Stefan Beller

"... but for now Git treats it as if it is not set at all" is a valuable information to the user, still?

Not at all. "What you are using is wrong and there is no guarantee what behaviour you would get" is the message we need to convey.

#13 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 7 lines · Junio C Hamano
Quoted 2 lines · Stefan Beller

"... but for now Git treats it as if it is not set at all" is a valuable information to the user, still?

Not at all. "What you are using is wrong and there is no guarantee what behaviour you would get" is the message we need to convey.

"Path '%s': Ignoring label set to false; This may behave
differently in future versions of Git."

Maybe even with s/may/will/

I think that conveys both the message about the future as well as "what happened just now in that command"

#14 lore raw

Re: [PATCH 5/5] pathspec: record labels

Quoted 2 lines · Stefan Beller

"Path '%s': Ignoring label set to false; This may behave
differently in future versions of Git."

I agree that mentioning "ignoring" is good enough. I think our recent messages begin with lowercase, though.

#16 lore raw

Re: [PATCH 5/5] pathspec: record labels

In reply to Stefan Beller
Quoted 6 lines · Stefan Beller
Quoted 6 lines

I am NOT suggesting to make this enhancement in the prototype to allow us experiment with submodule selection use case, but this is an obvious place to allow

        :(label=A B):(label=C D)
Quoted 8 lines
Quoted 2 lines

to mean ((A & B) | (C & D)) by making item->labels an array of set of labels.

This is what already works with the series. Or rather:

    ":(label=A B)" ":(label=C D)"

works as you would expect for (A&B) | (C&D).

That is "duplicationg path". I was envisioning a single

	":(label=A B):(label=C D)tediously/long/path/because/java/"

a shorter and sweeter way to say your two pathspec variant, i.e.

	":(label=A B)tediously/long/path/because/java/" \
        ":(label=C D)tediously/long/path/because/java/"