Skip to content

Commit 684e40f

Browse files
Huynh Khoi Nguyen Nguyengitster
authored andcommitted
Let core.attributesfile default to $XDG_CONFIG_HOME/git/attributes
This gives the default value for the core.attributesfile variable following the exact same logic of the previous change for the core.excludesfile setting. Signed-off-by: Huynh Khoi Nguyen Nguyen <[email protected]> Signed-off-by: Valentin Duperray <[email protected]> Signed-off-by: Franck Jonas <[email protected]> Signed-off-by: Lucien Kong <[email protected]> Signed-off-by: Thomas Nguy <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc79687 commit 684e40f

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,9 @@ core.attributesfile::
500500
In addition to '.gitattributes' (per-directory) and
501501
'.git/info/attributes', git looks into this file for attributes
502502
(see linkgit:gitattributes[5]). Path expansions are made the same
503-
way as for `core.excludesfile`.
503+
way as for `core.excludesfile`. Its default value is
504+
$XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME is either not
505+
set or empty, $HOME/.config/git/attributes is used instead.
504506

505507
core.editor::
506508
Commands such as `commit` and `tag` that lets you edit

Documentation/gitattributes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ repositories (i.e., attributes of interest to all users) should go into
7575
`.gitattributes` files. Attributes that should affect all repositories
7676
for a single user should be placed in a file specified by the
7777
`core.attributesfile` configuration option (see linkgit:git-config[1]).
78+
Its default value is $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME
79+
is either not set or empty, $HOME/.config/git/attributes is used instead.
7880
Attributes for all users on a system should be placed in the
7981
`$(prefix)/etc/gitattributes` file.
8082

attr.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ static int git_attr_system(void)
497497
static void bootstrap_attr_stack(void)
498498
{
499499
struct attr_stack *elem;
500+
char *xdg_attributes_file;
500501

501502
if (attr_stack)
502503
return;
@@ -515,13 +516,15 @@ static void bootstrap_attr_stack(void)
515516
}
516517
}
517518

518-
if (git_attributes_file) {
519-
elem = read_attr_from_file(git_attributes_file, 1);
520-
if (elem) {
521-
elem->origin = NULL;
522-
elem->prev = attr_stack;
523-
attr_stack = elem;
524-
}
519+
if (!git_attributes_file) {
520+
home_config_paths(NULL, &xdg_attributes_file, "attributes");
521+
git_attributes_file = xdg_attributes_file;
522+
}
523+
elem = read_attr_from_file(git_attributes_file, 1);
524+
if (elem) {
525+
elem->origin = NULL;
526+
elem->prev = attr_stack;
527+
attr_stack = elem;
525528
}
526529

527530
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {

t/t1306-xdg-files.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,33 @@ test_expect_success 'Exclusion in a non-XDG global ignore file' '
9696
'
9797

9898

99+
test_expect_success 'Checking attributes in the XDG attributes file' '
100+
echo foo >f &&
101+
git check-attr -a f >actual &&
102+
test_line_count -eq 0 actual &&
103+
echo "f attr_f" >"$HOME"/.config/git/attributes &&
104+
echo "f: attr_f: set" >expected &&
105+
git check-attr -a f >actual &&
106+
test_cmp expected actual
107+
'
108+
109+
110+
test_expect_success 'Checking attributes in both XDG and local attributes files' '
111+
echo "f -attr_f" >.gitattributes &&
112+
echo "f: attr_f: unset" >expected &&
113+
git check-attr -a f >actual &&
114+
test_cmp expected actual
115+
'
116+
117+
118+
test_expect_success 'Checking attributes in a non-XDG global attributes file' '
119+
test_might_fail rm .gitattributes &&
120+
echo "f attr_f=test" >"$HOME"/my_gitattributes &&
121+
git config core.attributesfile "$HOME"/my_gitattributes &&
122+
echo "f: attr_f: test" >expected &&
123+
git check-attr -a f >actual &&
124+
test_cmp expected actual
125+
'
126+
127+
99128
test_done

0 commit comments

Comments
 (0)