From: Kristoffer Haugsbakk <code@khaugsbakk.name>
To: gitster@pobox.com
Cc: Kristoffer Haugsbakk <code@khaugsbakk.name>,
git@vger.kernel.org, ks1322@gmail.com, peff@peff.net
Subject: Re: [PATCH] grep: die gracefully when outside repository
Date: Tue, 17 Oct 2023 21:51:08 +0200 [thread overview]
Message-ID: <f8a2abc0f610912af3eb56536ed217b8f90db2f9.1697571664.git.code@khaugsbakk.name> (raw)
In-Reply-To: <xmqqmswhjj48.fsf@gitster.g>
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`.
The `hint_path` declaration has to be at the start because of style
rules. But we can initialize it after.
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.
-- >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,
--
2.42.0.2.g879ad04204
next prev parent reply other threads:[~2023-10-17 19:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-14 15:42 Bug: git grep --no-index 123 /dev/stdin crashes with SIGABRT ks1322 ks1322
2023-10-14 18:12 ` Kristoffer Haugsbakk
2023-10-14 19:37 ` Kristoffer Haugsbakk
2023-10-14 21:02 ` [PATCH] grep: die gracefully when outside repository Kristoffer Haugsbakk
2023-10-15 3:26 ` Jeff King
2023-10-15 8:00 ` Kristoffer Haugsbakk
2023-10-15 17:57 ` Junio C Hamano
2023-10-17 16:42 ` Junio C Hamano
2023-10-17 19:51 ` Kristoffer Haugsbakk [this message]
2023-10-17 20:25 ` Junio C Hamano
2023-10-17 20:39 ` Junio C Hamano
2023-10-20 16:40 ` [PATCH v2] " Kristoffer Haugsbakk
2023-10-20 17:05 ` Eric Sunshine
2023-10-20 18:06 ` Junio C Hamano
2023-10-20 18:04 ` [PATCH v3] " Kristoffer Haugsbakk
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=f8a2abc0f610912af3eb56536ed217b8f90db2f9.1697571664.git.code@khaugsbakk.name \
--to=code@khaugsbakk.name \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ks1322@gmail.com \
--cc=peff@peff.net \
/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;
as well as URLs for NNTP newsgroup(s).