attr: read attributes from HEAD when bare repo

The motivation for 44451a2e5e (attr: teach "--attr-source=<tree>" global option to "git" , 2023-05-06), was to make it possible to use gitattributes with bare repositories. To make it easier to read gitattributes in bare repositories however, let's just make HEAD:.gitattributes the default. This is in line with how mailmap works, 8c473cecfd (mailmap: default mailmap.blob in bare repositories, 2012-12-13). Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed Oct 13, 2023 at 17:39 UTC 2386535511d1181afd4e892e2a866ffe5e1d7d21
3 files changed +23 -2
attr.c
+11 -1
@@ -1173,6 +1173,7 @@ static void collect_some_attrs(struct index_state *istate,
1173 }
1174
1175 static const char *default_attr_source_tree_object_name;
1176 +static int ignore_bad_attr_tree;
1177
1178 void set_git_attr_source(const char *tree_object_name)
1179 {
@@ -1184,10 +1185,19 @@ static void compute_default_attr_source(struct object_id *attr_source)
1185 if (!default_attr_source_tree_object_name)
1186 default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
1187
1188 + if (!default_attr_source_tree_object_name &&
1189 + startup_info->have_repository &&
1190 + is_bare_repository()) {
1191 + default_attr_source_tree_object_name = "HEAD";
1192 + ignore_bad_attr_tree = 1;
1193 + }
1194 +
1195 if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
1196 return;
1197
1190 - if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source))
1198 + if (repo_get_oid_treeish(the_repository,
1199 + default_attr_source_tree_object_name,
1200 + attr_source) && !ignore_bad_attr_tree)
1201 die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1202 }
1203
t/t0003-attributes.sh
+11
@@ -342,6 +342,17 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
342 )
343 '
344
345 +
346 +test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
347 + test_when_finished rm -rf test bare_with_gitattribute &&
348 + git init test &&
349 + test_commit -C test gitattributes .gitattributes "f/path test=val" &&
350 + git clone --bare test bare_with_gitattribute &&
351 + echo "f/path: test: val" >expect &&
352 + git -C bare_with_gitattribute check-attr test -- f/path >actual &&
353 + test_cmp expect actual
354 +'
355 +
356 test_expect_success 'bare repository: with --source' '
357 (
358 cd bare.git &&
t/t5001-archive-attr.sh
+1 -1
@@ -138,7 +138,7 @@ test_expect_success 'git archive with worktree attributes, bare' '
138 '
139
140 test_expect_missing bare-worktree/ignored
141 -test_expect_exists bare-worktree/ignored-by-tree
141 +test_expect_missing bare-worktree/ignored-by-tree
142 test_expect_exists bare-worktree/ignored-by-worktree
143
144 test_expect_success 'export-subst' '