Skip to content

Commit 813f17f

Browse files
pks-tgitster
authored andcommitted
attr: fix BUG() when parsing attrs outside of repo
If either the `--attr-source` option or the `GIT_ATTR_SOURCE` envvar are set, then `compute_default_attr_source()` will try to look up the value as a treeish. It is possible to hit that function while outside of a Git repository though, for example when using `git grep --no-index`. In that case, Git will hit a bug because we try to look up the main ref store outside of a repository. Handle the case gracefully and detect when we try to look up an attr source without a repository. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bbb82f8 commit 813f17f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

attr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,12 @@ static int compute_default_attr_source(struct object_id *attr_source)
12271227
if (!default_attr_source_tree_object_name)
12281228
return 0;
12291229

1230+
if (!startup_info->have_repository) {
1231+
if (!ignore_bad_attr_tree)
1232+
die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo"));
1233+
return 0;
1234+
}
1235+
12301236
if (repo_get_oid_treeish(the_repository,
12311237
default_attr_source_tree_object_name,
12321238
attr_source)) {

t/t0003-attributes.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@ test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tre
434434
)
435435
'
436436

437+
test_expect_success 'diff without repository with attr source' '
438+
mkdir -p "$TRASH_DIRECTORY/outside/nongit" &&
439+
(
440+
cd "$TRASH_DIRECTORY/outside/nongit" &&
441+
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/outside" &&
442+
export GIT_CEILING_DIRECTORIES &&
443+
touch file &&
444+
cat >expect <<-EOF &&
445+
fatal: cannot use --attr-source or GIT_ATTR_SOURCE without repo
446+
EOF
447+
test_must_fail env GIT_ATTR_SOURCE=HEAD git grep --no-index foo file 2>err &&
448+
test_cmp expect err
449+
)
450+
'
451+
437452
test_expect_success 'bare repository: with --source' '
438453
(
439454
cd bare.git &&

0 commit comments

Comments
 (0)