From b1688ea02df6fdefd5a228a0d52583356d433a99 Mon Sep 17 00:00:00 2001 From: Kristoffer Haugsbakk Date: Fri, 20 Oct 2023 18:40:07 +0200 Subject: grep: die gracefully when outside repository 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 advise the user about how the path is not in the current (non-existing) repository. Reported-by: ks1322 ks1322 Helped-by: Junio C Hamano Signed-off-by: Kristoffer Haugsbakk Signed-off-by: Junio C Hamano --- pathspec.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'pathspec.c') diff --git a/pathspec.c b/pathspec.c index 7f88f1c02b..bb1efe1f39 100644 --- a/pathspec.c +++ b/pathspec.c @@ -467,7 +467,12 @@ 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, -- cgit v1.2.3