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

Re: [PATCH v2] pathspec: advice: long and short forms are incompatible

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 30, 2021, 20:36 UTC
Message-ID
<xmqqo8f0cr9z.fsf@gitster.g>
In-Reply-To
<20210330190517.21138-1-133706+stdedos@users.noreply.github.com>
Stavros Ntentos <stdedos@gmail.com> writes:
Show 12 quoted lines
>> It may be more helpful if, rather than looking at what comes after
>> '(', we looked at what came before '(' and helped the user write
>> them out in the longform
>
> I don't see any explicit code in parsing the shortform magics, except:
> 		/* Special case alias for '!' */
> 		if (ch == '^') {
> 			*magic |= PATHSPEC_EXCLUDE;
> 			continue;
> 		}
> and therefore I would like to avoid such task (although I love well-written
> DWIMs-or-close-to-them).

I am not yet interested in doing DWIM, but a good advice is always welcome.

Along the lines of the attached illustration patch, perhaps.
    $ git show --oneline --stat -- ':/!(glob)**/*.txt'
    hint: ':/!(...': cannot mix short- and longform pathspec magic
    hint: instead, spell the shortform magic '/!' as 'top,exclude' inside :(...
 Documentation/config/advice.txt |  3 +++
 advice.c                        |  3 +++
 advice.h                        |  2 ++
 pathspec.c                      | 29 +++++++++++++++++++++++++++++
 4 files changed, 37 insertions(+)
diff --git c/Documentation/config/advice.txt w/Documentation/config/advice.txt
index acbd0c09aa..05a3cbc164 100644
--- c/Documentation/config/advice.txt
+++ w/Documentation/config/advice.txt
@@ -119,4 +119,7 @@ advice.*::
 	addEmptyPathspec::
 		Advice shown if a user runs the add command without providing
 		the pathspec parameter.
+	mixedPathspecMagic::
+		Advice shown if a user tries to mix short- and
+		longform pathspec magic.
 --
diff --git c/advice.c w/advice.c
index 164742305f..9426bc5295 100644
--- c/advice.c
+++ w/advice.c
@@ -33,6 +33,7 @@ int advice_checkout_ambiguous_remote_branch_name = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 int advice_add_ignored_file = 1;
 int advice_add_empty_pathspec = 1;
+int advice_mixed_pathspec_magic = 1;
 
 static int advice_use_color = -1;
 static char advice_colors[][COLOR_MAXLEN] = {
@@ -95,6 +96,7 @@ static struct {
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 	{ "addIgnoredFile", &advice_add_ignored_file },
 	{ "addEmptyPathspec", &advice_add_empty_pathspec },
+	{ "mixedPathspecMagic", &advice_mixed_pathspec_magic },
 
 	/* make this an alias for backward compatibility */
 	{ "pushNonFastForward", &advice_push_update_rejected }
@@ -113,6 +115,7 @@ static struct {
 	[ADVICE_GRAFT_FILE_DEPRECATED]			= { "graftFileDeprecated", 1 },
 	[ADVICE_IGNORED_HOOK]				= { "ignoredHook", 1 },
 	[ADVICE_IMPLICIT_IDENTITY]			= { "implicitIdentity", 1 },
+	[ADVICE_MIXED_PATHSPEC_MAGIC]			= { "mixedPathspecMagic", 1 },
 	[ADVICE_NESTED_TAG]				= { "nestedTag", 1 },
 	[ADVICE_OBJECT_NAME_WARNING]			= { "objectNameWarning", 1 },
 	[ADVICE_PUSH_ALREADY_EXISTS]			= { "pushAlreadyExists", 1 },
diff --git c/advice.h w/advice.h
index bc2432980a..d56c1896a0 100644
--- c/advice.h
+++ w/advice.h
@@ -33,6 +33,7 @@ extern int advice_checkout_ambiguous_remote_branch_name;
 extern int advice_submodule_alternate_error_strategy_die;
 extern int advice_add_ignored_file;
 extern int advice_add_empty_pathspec;
+extern int advice_mixed_pathspec_magic;
 
 /*
  * To add a new advice, you need to:
@@ -51,6 +52,7 @@ extern int advice_add_empty_pathspec;
 	ADVICE_GRAFT_FILE_DEPRECATED,
 	ADVICE_IGNORED_HOOK,
 	ADVICE_IMPLICIT_IDENTITY,
+	ADVICE_MIXED_PATHSPEC_MAGIC,
 	ADVICE_NESTED_TAG,
 	ADVICE_OBJECT_NAME_WARNING,
 	ADVICE_PUSH_ALREADY_EXISTS,
diff --git c/pathspec.c w/pathspec.c
index 18b3be362a..ce9d1738a8 100644
--- c/pathspec.c
+++ w/pathspec.c
@@ -336,6 +336,32 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
 	return pos;
 }
 
+/*
+ * Give hint for a common mistake of mixing short and long
+ * form of pathspec magic
+ */
+static void warn_mixed_magic(unsigned magic, const char *elem, const char *pos)
+{
+	struct strbuf longform = STRBUF_INIT;
+	int i;
+
+	if (!advice_enabled(ADVICE_MIXED_PATHSPEC_MAGIC))
+		return;
+	for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
+		if (pathspec_magic[i].bit & magic) {
+			if (longform.len)
+				strbuf_addch(&longform, ',');
+			strbuf_addstr(&longform, pathspec_magic[i].name);
+		}
+	}
+	advise(_("'%.*s(...': cannot mix short- and longform pathspec magic"),
+	       (int)(pos - elem), elem);
+	advise(_("instead, spell the shortform magic '%.*s' as '%s' inside :(..."),
+	       (int)(pos - (elem + 1)), elem + 1,
+	       longform.buf);
+}
+
+
 /*
  * Parse the pathspec element looking for short magic
  *
@@ -356,6 +382,9 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
 			continue;
 		}
 
+		if (ch == '(')
+			warn_mixed_magic(*magic, elem, pos);
+
 		if (!is_pathspec_magic(ch))
 			break;
 
Previous: Stavros NtentosNext: Stavros Ntentos
Message 41 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.