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>
Patrick Steinhardt committed
May 7, 2024 at 06:53 UTC
813f17fd6bd9289185a50f1d1179393bd1339b9b
2 files changed
+21
attr.c
+6
@@ -1227,6 +1227,12 @@ static int compute_default_attr_source(struct object_id *attr_source)
1227
if (!default_attr_source_tree_object_name)
1228
return 0;
1229
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
+
1236
if (repo_get_oid_treeish(the_repository,
1237
default_attr_source_tree_object_name,
1238
attr_source)) {
t/t0003-attributes.sh
+15
@@ -434,6 +434,21 @@ test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tre
434
)
435
'
436
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
+
452
test_expect_success 'bare repository: with --source' '
453
(
454
cd bare.git &&