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

Re: Bug: Git sometimes disregards wildcards in pathspecs if a file name matches exactly

From
Jeff King <peff@peff.net>
Date
Apr 12, 2025, 06:27 UTC
Message-ID
<20250412062753.GA3763415@coredump.intra.peff.net>
In-Reply-To
<CA+rGoLd-bSbaqfswD+qVrCv1fvG69QX+3BO1htbPJtinFkEQAA@mail.gmail.com>
On Sat, Apr 12, 2025 at 08:30:00AM +0530, JAYATHEERTH K wrote:
> Upon a much closer look into the Git source code, specifically
> init_pathspec_item within pathspec.c, my previous hypothesis regarding
> the optimization check in do_match_pathspec appears to be incorrect,
> or at least not the root cause.

I think you're still on the right track, but are just mis-interpreting the item->magic field. It's about user-specified "magic" flags, one of which is "treat this pathspec like a glob, even if the default (or an earlier "literal" magic flag) would tell you not to do so".

I don't think there is a bit flag in the pathspec item for "this item has glob meta characters". But we compute and record the offset of the first such character in the "nowildcard_len" field (which is for most cases much better, since we can optimize prefix matches for stuff like "foo/bar/*").

And you can compare that to "len" to see if it does any globbing at all (which should also naturally handle stuff like ":(literal)" because we then mark the whole thing as "nowildcard").

So something like this probably works:
diff --git a/dir.c b/dir.c
index cbd82be6c9..85cc08f4fc 100644
--- a/dir.c
+++ b/dir.c
@@ -519,7 +519,8 @@ static int do_match_pathspec(struct index_state *istate,
 		    ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE)))
 			continue;
 
-		if (seen && seen[i] == MATCHED_EXACTLY)
+		if (seen && seen[i] == MATCHED_EXACTLY &&
+		    ps->items[i].nowildcard_len == ps->items[i].len)
 			continue;
 		/*
 		 * Make exclude patterns optional and never report

I think if you grep around for 'nowildcard_len ==' you'll find one or
two similar spots.

-Peff
Previous: JAYATHEERTH KNext: JAYATHEERTH K
Message 4 of 5 in “Bug: Git sometimes disregards wildcards in pathspecs if a file name matches exactly”
  1. Piotr SiupaApr 11, 2025
  2. K JayatheerthApr 12, 2025
  3. JAYATHEERTH KApr 12, 2025
  4. Jeff KingApr 12, 2025
  5. JAYATHEERTH KApr 12, 2025

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.