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

Re: Why does git-grep appear to treat exclude pathspecs differently?

From
D. Ben Knoble <ben.knoble@gmail.com>
Date
Aug 2, 2025, 16:13 UTC
Message-ID
<CALnO6CALb-_RjNZw9u-0R33O+KfccfZK0+mF-W7G659wc4PW7Q@mail.gmail.com>
In-Reply-To
<20250802094657.GG3711639@coredump.intra.peff.net>
On Sat, Aug 2, 2025 at 5:46 AM Jeff King <peff@peff.net> wrote:
Show 34 quoted lines
>
> On Wed, Jul 30, 2025 at 05:49:29PM -0400, D. Ben Knoble wrote:
>
> > which eventually dies as noted. However:
> >
> > - looks_like_pathspec() only checks for long magic, as you noted
> > - setup.c:check_filename() looks for short-magic, too, but only
> > considers ":^" to work like a pathname if we're excluding everything?
> >
> > I think what I find confusing is that, while this is definitely a DWIM
> > case for git-grep, it doesn't seem to do DWIM :) We are verifying that
> > the remaining arguments are filenames, but couldn't they really be
> > full pathspecs, as long as they aren't revisions?
> >
> > The difference for "^:<path>" from ":^:<path>", just to complete the
> > story, is that in setup.c:check_filename() we try to stat whatever
> > comes after ":^":
> > - for the former, that's <path> and we are ok
> > - for the latter, that's :<path>, fail.
> >
> > So it seems like the places that check for short-magic should also
> > consider the optional trailing colon?
>
> Yeah, I'd think so. But it's worse than that, even. According to the
> glossary definition you showed:
>
>   In the short form, the leading colon `:` is followed by zero or more
>   "magic signature" letters (which optionally is terminated by another
>   colon `:`)
>
> we allow multiple bits of magic. So the code in check_filename() that
> looks for ":/", ":^", etc would be fooled when seeing more than one
> character, like: ":/^exclude-from-root". AFAICT there are only two short
> magic types, so I guess nobody has really run into this before.
Good call out! I also see only 2 magic items (one with a synonym) at the moment.
Show 7 quoted lines
>
> Also, I guess this function ought to be respecting the literal_pathspecs
> global? The actual pathspec parser does.
>
> If we can, we probably ought to be feeding the paths to a function like
> pathspec.c:parse_element_magic() and then checking the resulting flags
> (and skipping past the prefix as it indicates).

Thanks for pointing me at this; maybe I'll find some time for patches unless someone beats me to it.

Show 5 quoted lines
>
> -Peff
>
> PS I didn't even know that we allowed multiple short items or a trailing
>    colon until your email! Hidden corners of Git.
;)
-- 
D. Ben Knoble
Previous: Jeff KingNext: Jeff King
Message 5 of 12 in “Why does git-grep appear to treat exclude pathspecs differently?”
  1. D. Ben KnobleJul 26, 2025
  2. Junio C HamanoJul 27, 2025
  3. D. Ben KnobleJul 30, 2025
  4. Jeff KingAug 2, 2025
  5. D. Ben KnobleAug 2, 2025
  6. Jeff KingAug 2, 2025
  7. Junio C HamanoAug 3, 2025
  8. Jeff KingAug 5, 2025
  9. Junio C HamanoAug 5, 2025
  10. Junio C HamanoAug 2, 2025
  11. D. Ben KnobleAug 2, 2025
  12. Junio C HamanoAug 2, 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.