Skip to content

Commit 576a37f

Browse files
bk2204gitster
authored andcommitted
var: add attributes files locations
Currently, there are some programs which would like to read and parse the gitattributes files at the global or system levels. However, it's not always obvious where these files live, especially for the system file, which may have been hard-coded at compile time or computed dynamically based on the runtime prefix. It's not reasonable to expect all callers of Git to intuitively know where the Git distributor or user has configured these locations to be, so add some entries to allow us to determine their location. Honor the GIT_ATTR_NOSYSTEM environment variable if one is specified. Expose the accessor functions in a way that we can reuse them from within the var code. In order to make our paths consistent on Windows and also use the same form as paths use in "git rev-parse", let's normalize the path before we return it. This results in Windows-style paths that use slashes, which is convenient for making our tests function in a consistent way across platforms. Note that this requires that some of our values be freed, so let's add a flag about whether the value needs to be freed and use it accordingly. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15780bb commit 576a37f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Documentation/git-var.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ GIT_DEFAULT_BRANCH::
7474
GIT_SHELL_PATH::
7575
The path of the binary providing the POSIX shell for commands which use the shell.
7676

77+
GIT_ATTR_SYSTEM::
78+
The path to the system linkgit:gitattributes[5] file, if one is enabled.
79+
80+
GIT_ATTR_GLOBAL::
81+
The path to the global (per-user) linkgit:gitattributes[5] file.
82+
7783
SEE ALSO
7884
--------
7985
linkgit:git-commit-tree[1]

builtin/var.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (C) Eric Biederman, 2005
55
*/
66
#include "builtin.h"
7+
#include "attr.h"
78
#include "config.h"
89
#include "editor.h"
910
#include "ident.h"
@@ -51,6 +52,26 @@ static char *shell_path(int ident_flag UNUSED)
5152
return xstrdup(SHELL_PATH);
5253
}
5354

55+
static char *git_attr_val_system(int ident_flag UNUSED)
56+
{
57+
if (git_attr_system_is_enabled()) {
58+
char *file = xstrdup(git_attr_system_file());
59+
normalize_path_copy(file, file);
60+
return file;
61+
}
62+
return NULL;
63+
}
64+
65+
static char *git_attr_val_global(int ident_flag UNUSED)
66+
{
67+
char *file = xstrdup(git_attr_global_file());
68+
if (file) {
69+
normalize_path_copy(file, file);
70+
return file;
71+
}
72+
return NULL;
73+
}
74+
5475
struct git_var {
5576
const char *name;
5677
char *(*read)(int);
@@ -84,6 +105,14 @@ static struct git_var git_vars[] = {
84105
.name = "GIT_SHELL_PATH",
85106
.read = shell_path,
86107
},
108+
{
109+
.name = "GIT_ATTR_SYSTEM",
110+
.read = git_attr_val_system,
111+
},
112+
{
113+
.name = "GIT_ATTR_GLOBAL",
114+
.read = git_attr_val_global,
115+
},
87116
{
88117
.name = "",
89118
.read = NULL,

t/t0007-git-var.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' '
162162
esac
163163
'
164164

165+
test_expect_success 'GIT_ATTR_SYSTEM produces expected output' '
166+
test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM &&
167+
(
168+
sane_unset GIT_ATTR_NOSYSTEM &&
169+
systempath=$(git var GIT_ATTR_SYSTEM) &&
170+
test "$systempath" != ""
171+
)
172+
'
173+
174+
test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' '
175+
TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
176+
globalpath=$(XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL) &&
177+
test "$globalpath" = "$TRASHDIR/.config/git/attributes" &&
178+
(
179+
sane_unset XDG_CONFIG_HOME &&
180+
globalpath=$(HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL) &&
181+
test "$globalpath" = "$TRASHDIR/.config/git/attributes"
182+
)
183+
'
184+
165185
# For git var -l, we check only a representative variable;
166186
# testing the whole output would make our test too brittle with
167187
# respect to unrelated changes in the test suite's environment.

0 commit comments

Comments
 (0)