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

Re: [PATCH] grep: die gracefully when outside repository

From
Jeff King <peff@peff.net>
Date
Oct 15, 2023, 03:26 UTC
Message-ID
<20231015032636.GC554702@coredump.intra.peff.net>
In-Reply-To
<087c92e3904dd774f672373727c300bf7f5f6369.1697317276.git.code@khaugsbakk.name>
On Sat, Oct 14, 2023 at 11:02:38PM +0200, Kristoffer Haugsbakk wrote:
Show 16 quoted lines
> Die gracefully when `git grep --no-index` is run outside of a Git
> repository and the path is outside the directory tree.
> 
> If you are not in a Git repository and say:
> 
>     git grep --no-index search ..
> 
> You trigger a `BUG`:
> 
>     BUG: environment.c:213: git environment hasn't been setup
>     Aborted (core dumped)
> 
> Because `..` is a valid path which is treated as a pathspec. Then
> `pathspec` figures out that it is not in the current directory tree. The
> `BUG` is triggered when `pathspec` tries to advice the user about the path
> to the (non-existing) repository.

Is it even reasonable for "grep --no-index" to care about leaving the tree in the first place? That is, is there a reason we should not allow:

  git grep --no-index foo ../bar

? And if we do want to care, there is a weirdness here that even with your patch, we check to see if the file exists:

  $ git grep --no-index foo ../does-exist
  fatal: '../does-exist' is outside the directory tree
  $ git grep --no-index foo ../does-not-exist
  fatal: ../does-not-exist: no such path in the working tree.

If we want to avoid leaving the current directory, then I think we need to be checking much sooner (but again, I would argue that it is not worth caring about in no-index mode).

I do think your patch does not make anything worse (and indeed makes the error output much better). So I do not mind it in the meantime. But I have a feeling that we'd end up reverting it as part of the fix for the larger issue.

-Peff
Previous: Kristoffer HaugsbakkNext: Kristoffer Haugsbakk
Message 5 of 15 in “Bug: git grep --no-index 123 /dev/stdin crashes with SIGABRT”
  1. ks1322 ks1322Oct 14, 2023
  2. Kristoffer HaugsbakkOct 14, 2023
  3. Kristoffer HaugsbakkOct 14, 2023
  4. grep: die gracefully when outside repositoryKristoffer Haugsbakk, Oct 14, 2023
  5. Jeff KingOct 15, 2023
  6. Kristoffer HaugsbakkOct 15, 2023
  7. Junio C HamanoOct 15, 2023
  8. Junio C HamanoOct 17, 2023
  9. Kristoffer HaugsbakkOct 17, 2023
  10. Junio C HamanoOct 17, 2023
  11. Junio C HamanoOct 17, 2023
  12. grep: die gracefully when outside repositoryKristoffer Haugsbakk, Oct 20, 2023
  13. Eric SunshineOct 20, 2023
  14. Junio C HamanoOct 20, 2023
  15. grep: die gracefully when outside repositoryKristoffer Haugsbakk, Oct 20, 2023

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.