git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH v1 3/3] squash! fixup! pathspec: warn: long and short forms are incompatible

From
SNStavros Ntentos <stdedos@gmail.com>
Date
Mar 28, 2021, 15:26 UTC
Message-ID
<20210328152629.16486-1-133706+stdedos@users.noreply.github.com>
In-Reply-To
<20210326024005.26962-3-stdedos+git@gmail.com>

Attempt to force parsing long magic values to detect if there is actually long magic present or not.

Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com>
---
 pathspec.c                  | 35 +++++++++++++++++++----------------
 pathspec.h                  |  2 +-
 t/t6132-pathspec-exclude.sh |  4 ++--
 3 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/pathspec.c b/pathspec.c
index 857519fda4..447d765112 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -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);
 }
diff --git a/pathspec.h b/pathspec.h
index 879d4e82c6..af6c458a06 100644
--- a/pathspec.h
+++ b/pathspec.h
@@ -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 */
diff --git a/t/t6132-pathspec-exclude.sh b/t/t6132-pathspec-exclude.sh
index b32ddb2a56..a1580a89ae 100755
--- a/t/t6132-pathspec-exclude.sh
+++ b/t/t6132-pathspec-exclude.sh
@@ -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
-- 
2.31.0
Previous: Stavros NtentosNext: Σταύρος Ντέντος
Message 22 of 43 in “Pathspec does not accept / does not warn for specific pathspecs”
  1. Σταύρος ΝτέντοςFeb 26, 2021
  2. Junio C HamanoFeb 26, 2021
  3. 0/1 pathspec: warn for a no-glob entry that contains `**`Σταύρος Ντέντος, Mar 25, 2021
  4. 1/1 pathspec: warn for a no-glob entry that contains `**`Σταύρος Ντέντος, Mar 25, 2021
  5. Bagas SanjayaMar 25, 2021
  6. Bagas SanjayaMar 25, 2021
  7. 1/1 pathspec: warn for a no-glob entry that contains `**`Stavros Ntentos, Mar 25, 2021
  8. Junio C HamanoMar 25, 2021
  9. 0/1 pathspec: warn for a no-glob entry that contains `**`Σταύρος Ντέντος, Mar 25, 2021
  10. 1/1 pathspec: warn for a no-glob entry that contains `**`Σταύρος Ντέντος, Mar 25, 2021
  11. Junio C HamanoMar 26, 2021
  12. Eric SunshineMar 26, 2021
  13. Junio C HamanoMar 26, 2021
  14. Jeff KingMar 26, 2021
  15. Stavros NtentosMar 26, 2021
  16. 1/2 pathspec: warn for a no-glob entry that contains `**`Stavros Ntentos, Mar 26, 2021
  17. 2/2 pathspec: convert no-glob warn to adviceStavros Ntentos, Mar 26, 2021
  18. 0/1 pathspec: warn: long and short forms are incompatibleΣταύρος Ντέντος, Mar 26, 2021
  19. 2/2 fixup! pathspec: warn: long and short forms are incompatibleΣταύρος Ντέντος, Mar 26, 2021
  20. Bagas SanjayaMar 26, 2021
  21. Stavros NtentosMar 26, 2021
  22. 3/3 squash! fixup! pathspec: warn: long and short forms are incompatibleStavros Ntentos, Mar 28, 2021
  23. 1/2 pathspec: warn: long and short forms are incompatibleΣταύρος Ντέντος, Mar 26, 2021
  24. Jeff KingMar 26, 2021
  25. Stavros NtentosMar 26, 2021
  26. Jeff KingMar 27, 2021
  27. Junio C HamanoMar 27, 2021
  28. Stavros NtentosMar 28, 2021
  29. 0/1 pathspec: warn: long and short forms are incompatibleΣταύρος Ντέντος, Mar 26, 2021
  30. pathspec: warn: long and short forms are incompatibleΣταύρος Ντέντος, Mar 26, 2021
  31. pathspec: advice: long and short forms are incompatibleStavros Ntentos, Apr 3, 2021
  32. Junio C HamanoApr 4, 2021
  33. Σταύρος ΝτέντοςApr 11, 2021
  34. Junio C HamanoApr 11, 2021
  35. Σταύρος ΝτέντοςApr 11, 2021
  36. pathspec: advice: long and short forms are incompatibleStavros Ntentos, Mar 28, 2021
  37. Junio C HamanoMar 28, 2021
  38. Junio C HamanoMar 30, 2021
  39. Stavros NtentosMar 30, 2021
  40. Stavros NtentosMar 30, 2021
  41. Junio C HamanoMar 30, 2021
  42. pathspec: advice: long and short forms are incompatibleStavros Ntentos, Apr 3, 2021
  43. pathspec: advice: long and short forms are incompatibleStavros Ntentos, Apr 3, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.