attr: add attr.tree for setting the treeish to read attributes from

44451a2 (attr: teach "--attr-source=<tree>" global option to "git", 2023-05-06) provided the ability to pass in a treeish as the attr source. In the context of serving Git repositories as bare repos like we do at GitLab however, it would be easier to point --attr-source to HEAD for all commands by setting it once. Add a new config attr.tree that allows this. 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 9f9c40cf34c29d4ad700d9869435d159056fa6fb
6 files changed +95
Documentation/config.txt
+2
@@ -371,6 +371,8 @@ other popular tools, and describe them in your documentation.
371
372 include::config/advice.txt[]
373
374 +include::config/attr.txt[]
375 +
376 include::config/core.txt[]
377
378 include::config/add.txt[]
Documentation/config/attr.txt new
+7
@@ -0,0 +1,7 @@
1 +attr.tree::
2 + A reference to a tree in the repository from which to read attributes,
3 + instead of the `.gitattributes` file in the working tree. In a bare
4 + repository, this defaults to `HEAD:.gitattributes`. If the value does
5 + not resolve to a valid tree object, an empty tree is used instead.
6 + When the `GIT_ATTR_SOURCE` environment variable or `--attr-source`
7 + command line option are used, this configuration variable has no effect.
attr.c
+7
@@ -24,6 +24,8 @@
24 #include "tree-walk.h"
25 #include "object-name.h"
26
27 +const char *git_attr_tree;
28 +
29 const char git_attr__true[] = "(builtin)true";
30 const char git_attr__false[] = "\0(builtin)false";
31 static const char git_attr__unknown[] = "(builtin)unknown";
@@ -1185,6 +1187,11 @@ static void compute_default_attr_source(struct object_id *attr_source)
1187 if (!default_attr_source_tree_object_name)
1188 default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
1189
1190 + if (!default_attr_source_tree_object_name && git_attr_tree) {
1191 + default_attr_source_tree_object_name = git_attr_tree;
1192 + ignore_bad_attr_tree = 1;
1193 + }
1194 +
1195 if (!default_attr_source_tree_object_name &&
1196 startup_info->have_repository &&
1197 is_bare_repository()) {
attr.h
+2
@@ -236,4 +236,6 @@ const char *git_attr_global_file(void);
236 /* Return whether the system gitattributes file is enabled and should be used. */
237 int git_attr_system_is_enabled(void);
238
239 +extern const char *git_attr_tree;
240 +
241 #endif /* ATTR_H */
config.c
+16
@@ -18,6 +18,7 @@
18 #include "repository.h"
19 #include "lockfile.h"
20 #include "mailmap.h"
21 +#include "attr.h"
22 #include "exec-cmd.h"
23 #include "strbuf.h"
24 #include "quote.h"
@@ -1904,6 +1905,18 @@ static int git_default_mailmap_config(const char *var, const char *value)
1905 return 0;
1906 }
1907
1908 +static int git_default_attr_config(const char *var, const char *value)
1909 +{
1910 + if (!strcmp(var, "attr.tree"))
1911 + return git_config_string(&git_attr_tree, var, value);
1912 +
1913 + /*
1914 + * Add other attribute related config variables here and to
1915 + * Documentation/config/attr.txt.
1916 + */
1917 + return 0;
1918 +}
1919 +
1920 int git_default_config(const char *var, const char *value,
1921 const struct config_context *ctx, void *cb)
1922 {
@@ -1927,6 +1940,9 @@ int git_default_config(const char *var, const char *value,
1940 if (starts_with(var, "mailmap."))
1941 return git_default_mailmap_config(var, value);
1942
1943 + if (starts_with(var, "attr."))
1944 + return git_default_attr_config(var, value);
1945 +
1946 if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
1947 return git_default_advice_config(var, value);
1948
t/t0003-attributes.sh
+61
@@ -40,6 +40,10 @@ attr_check_source () {
40 test_cmp expect actual &&
41 test_must_be_empty err
42
43 + git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
44 + test_cmp expect actual &&
45 + test_must_be_empty err
46 +
47 GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
48 test_cmp expect actual &&
49 test_must_be_empty err
@@ -342,6 +346,43 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
346 )
347 '
348
349 +bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
350 +
351 +test_expect_success '--attr-source is bad' '
352 + test_when_finished rm -rf empty &&
353 + git init empty &&
354 + (
355 + cd empty &&
356 + echo "$bad_attr_source_err" >expect_err &&
357 + test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
358 + test_cmp expect_err err
359 + )
360 +'
361 +
362 +test_expect_success 'attr.tree when HEAD is unborn' '
363 + test_when_finished rm -rf empty &&
364 + git init empty &&
365 + (
366 + cd empty &&
367 + echo "f/path: test: unspecified" >expect &&
368 + git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
369 + test_must_be_empty err &&
370 + test_cmp expect actual
371 + )
372 +'
373 +
374 +test_expect_success 'bad attr source defaults to reading .gitattributes file' '
375 + test_when_finished rm -rf empty &&
376 + git init empty &&
377 + (
378 + cd empty &&
379 + echo "f/path test=val" >.gitattributes &&
380 + echo "f/path: test: val" >expect &&
381 + git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
382 + test_must_be_empty err &&
383 + test_cmp expect actual
384 + )
385 +'
386
387 test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
388 test_when_finished rm -rf test bare_with_gitattribute &&
@@ -353,6 +394,26 @@ test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
394 test_cmp expect actual
395 '
396
397 +test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
398 + test_when_finished rm -rf empty &&
399 + git init empty &&
400 + (
401 + cd empty &&
402 + git checkout -b attr-source &&
403 + test_commit "val1" .gitattributes "f/path test=val1" &&
404 + git checkout -b attr-tree &&
405 + test_commit "val2" .gitattributes "f/path test=val2" &&
406 + git checkout attr-source &&
407 + echo "f/path: test: val1" >expect &&
408 + GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
409 + check-attr test -- f/path >actual &&
410 + test_cmp expect actual &&
411 + GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
412 + check-attr test -- f/path >actual &&
413 + test_cmp expect actual
414 + )
415 +'
416 +
417 test_expect_success 'bare repository: with --source' '
418 (
419 cd bare.git &&