Skip to content

Commit 64912cc

Browse files
committed
Merge branch 'kh/pathspec-error-wo-repository-fix'
The pathspec code carelessly dereferenced NULL while emitting an error message, which has been corrected. * kh/pathspec-error-wo-repository-fix: grep: die gracefully when outside repository
2 parents 6597631 + b1688ea commit 64912cc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pathspec.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
467467
match = prefix_path_gently(prefix, prefixlen,
468468
&prefixlen, copyfrom);
469469
if (!match) {
470-
const char *hint_path = get_git_work_tree();
470+
const char *hint_path;
471+
472+
if (!have_git_dir())
473+
die(_("'%s' is outside the directory tree"),
474+
copyfrom);
475+
hint_path = get_git_work_tree();
471476
if (!hint_path)
472477
hint_path = get_git_dir();
473478
die(_("%s: '%s' is outside repository at '%s'"), elt,

t/t7810-grep.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,33 @@ test_expect_success 'outside of git repository with fallbackToNoIndex' '
12341234
)
12351235
'
12361236

1237+
test_expect_success 'no repository with path outside $cwd' '
1238+
test_when_finished rm -fr non &&
1239+
rm -fr non &&
1240+
mkdir -p non/git/sub non/tig &&
1241+
(
1242+
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1243+
export GIT_CEILING_DIRECTORIES &&
1244+
cd non/git &&
1245+
test_expect_code 128 git grep --no-index search .. 2>error &&
1246+
grep "is outside the directory tree" error
1247+
) &&
1248+
(
1249+
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1250+
export GIT_CEILING_DIRECTORIES &&
1251+
cd non/git &&
1252+
test_expect_code 128 git grep --no-index search ../tig 2>error &&
1253+
grep "is outside the directory tree" error
1254+
) &&
1255+
(
1256+
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1257+
export GIT_CEILING_DIRECTORIES &&
1258+
cd non/git &&
1259+
test_expect_code 128 git grep --no-index search ../non 2>error &&
1260+
grep "no such path in the working tree" error
1261+
)
1262+
'
1263+
12371264
test_expect_success 'inside git repository but with --no-index' '
12381265
rm -fr is &&
12391266
mkdir -p is/git/sub &&

0 commit comments

Comments
 (0)