Skip to content

Commit 9f9c40c

Browse files
john-caigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2386535 commit 9f9c40c

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ other popular tools, and describe them in your documentation.
371371

372372
include::config/advice.txt[]
373373

374+
include::config/attr.txt[]
375+
374376
include::config/core.txt[]
375377

376378
include::config/add.txt[]

Documentation/config/attr.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "tree-walk.h"
2525
#include "object-name.h"
2626

27+
const char *git_attr_tree;
28+
2729
const char git_attr__true[] = "(builtin)true";
2830
const char git_attr__false[] = "\0(builtin)false";
2931
static const char git_attr__unknown[] = "(builtin)unknown";
@@ -1185,6 +1187,11 @@ static void compute_default_attr_source(struct object_id *attr_source)
11851187
if (!default_attr_source_tree_object_name)
11861188
default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
11871189

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+
11881195
if (!default_attr_source_tree_object_name &&
11891196
startup_info->have_repository &&
11901197
is_bare_repository()) {

attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,6 @@ const char *git_attr_global_file(void);
236236
/* Return whether the system gitattributes file is enabled and should be used. */
237237
int git_attr_system_is_enabled(void);
238238

239+
extern const char *git_attr_tree;
240+
239241
#endif /* ATTR_H */

config.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "repository.h"
1919
#include "lockfile.h"
2020
#include "mailmap.h"
21+
#include "attr.h"
2122
#include "exec-cmd.h"
2223
#include "strbuf.h"
2324
#include "quote.h"
@@ -1904,6 +1905,18 @@ static int git_default_mailmap_config(const char *var, const char *value)
19041905
return 0;
19051906
}
19061907

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+
19071920
int git_default_config(const char *var, const char *value,
19081921
const struct config_context *ctx, void *cb)
19091922
{
@@ -1927,6 +1940,9 @@ int git_default_config(const char *var, const char *value,
19271940
if (starts_with(var, "mailmap."))
19281941
return git_default_mailmap_config(var, value);
19291942

1943+
if (starts_with(var, "attr."))
1944+
return git_default_attr_config(var, value);
1945+
19301946
if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
19311947
return git_default_advice_config(var, value);
19321948

t/t0003-attributes.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ attr_check_source () {
4040
test_cmp expect actual &&
4141
test_must_be_empty err
4242

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+
4347
GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
4448
test_cmp expect actual &&
4549
test_must_be_empty err
@@ -342,6 +346,43 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
342346
)
343347
'
344348

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+
'
345386

346387
test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
347388
test_when_finished rm -rf test bare_with_gitattribute &&
@@ -353,6 +394,26 @@ test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
353394
test_cmp expect actual
354395
'
355396

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+
356417
test_expect_success 'bare repository: with --source' '
357418
(
358419
cd bare.git &&

0 commit comments

Comments
 (0)