Git development
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: JAYATHEERTH K <jayatheerthkulkarni2005@gmail.com>
Cc: piotrsiupa@gmail.com, git@vger.kernel.org
Subject: Re: Bug: Git sometimes disregards wildcards in pathspecs if a file name matches exactly
Date: Sat, 12 Apr 2025 02:27:53 -0400	[thread overview]
Message-ID: <20250412062753.GA3763415@coredump.intra.peff.net> (raw)
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

  reply	other threads:[~2025-04-12  6:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-11 19:08 Bug: Git sometimes disregards wildcards in pathspecs if a file name matches exactly Piotr Siupa
2025-04-12  1:57 ` K Jayatheerth
2025-04-12  3:00   ` JAYATHEERTH K
2025-04-12  6:27     ` Jeff King [this message]
2025-04-12  9:29       ` JAYATHEERTH K

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250412062753.GA3763415@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=jayatheerthkulkarni2005@gmail.com \
    --cc=piotrsiupa@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox