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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 17, 2023, 20:25 UTC
Message-ID
<xmqqcyxdgfn2.fsf@gitster.g>
In-Reply-To
<f8a2abc0f610912af3eb56536ed217b8f90db2f9.1697571664.git.code@khaugsbakk.name>
Kristoffer Haugsbakk <code@khaugsbakk.name> writes:
Show 25 quoted lines
> On Tue, Oct 17, 2023, at 18:42, Junio C Hamano wrote:
>> It is curious that the original has two sources of hint_path (i.e.,
>> get_git_dir() is used as a fallback for get_git_work_tree()).  Are
>> we certain that the check is at the right place?  If we do not have
>> a repository, then both would fail by returning NULL, so it should
>> not matter if we add the new check before we check either or both,
>> or even after we checked both before dying.
>>
>> I wonder if
>>
>> 	const char *hint_path = get_git_work_tree();
>>
>> 	if (!hint_path)
>> 	        hint_path = get_git_dir();
>> 	if (hint_path)
>> 		die(_("%s: '%s' is outside repository at '%s'"),
>> 		    elt, copyfrom, absolute_path(hint_path));
>> 	else
>> 		die(_("%s: '%s' is outside the directory tree"),
>> 		    elt, copyfrom);
>>
>> makes the intent of the code clearer.
>
> That doesn't work since `get_git_dir()` triggers `BUG` instead of
> returning `NULL`.
Ah, interesting.
> The `hint_path` declaration has to be at the start because of style
> rules. But we can initialize it after.

Yes, what you have below (but please leave a blank line between the last line of decl and the first line of statement for readablility) looks very readable and sensible.

> I can also have a second look at the test since I am using `grep` to
> test the failure output and not the translation string variant.

That is not necessary, as we no longer run under phoney i18n that required us to use test_i18ngrep. It is OK to assume that the tests are run under "C" locale.

Thanks.
Show 24 quoted lines
> -- >8 --
> Subject: [PATCH] fixup! grep: die gracefully when outside repository
>
> ---
>  pathspec.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/pathspec.c b/pathspec.c
> index e115832f17a..0c1061fad11 100644
> --- a/pathspec.c
> +++ b/pathspec.c
> @@ -467,10 +467,11 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
>  		match = prefix_path_gently(prefix, prefixlen,
>  					   &prefixlen, copyfrom);
>  		if (!match) {
> -			const char *hint_path = get_git_work_tree();
> +			const char *hint_path;
>  			if (!have_git_dir())
>  				die(_("'%s' is outside the directory tree"),
>  				    copyfrom);
> +			hint_path = get_git_work_tree();
>  			if (!hint_path)
>  				hint_path = get_git_dir();
>  			die(_("%s: '%s' is outside repository at '%s'"), elt,
Previous: Kristoffer HaugsbakkNext: Junio C Hamano
Message 10 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.