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

From: Jeff King <[email-masked]>
Date: Sat, 12 Apr 2025 02:27:53 -0400

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