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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-05-07 06:53:05 +02:00
committed by Junio C Hamano
parent bbb82f8dc8
commit 813f17fd6b
2 changed files with 21 additions and 0 deletions

6
attr.c
View File

@ -1227,6 +1227,12 @@ static int compute_default_attr_source(struct object_id *attr_source)
if (!default_attr_source_tree_object_name)
return 0;
if (!startup_info->have_repository) {
if (!ignore_bad_attr_tree)
die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo"));
return 0;
}
if (repo_get_oid_treeish(the_repository,
default_attr_source_tree_object_name,
attr_source)) {