Skip to content

Commit 303df68

Browse files
committed
Windows: use central directory for system-wide config files
Git versions on Windows typically store system-wide config files within their installation directory. Other Git implementations (e.g. libgit2, JGit etc.) may wish to reuse the system-wide config, but finding the installation directory of an already installed Git version is problematic. On Windows, the %PROGRAMDATA% environment variable specifies the root directory for system-wide application data (%ALLUSERSPROFILE% for WinXP). This would be the equivalent of '/etc' (and '/var'), except that naming guidelines recommend organizing data in per-application sub-directories. When looking up a system_path() on Windows, replace the absolute path prefix '/etc/' with '%PROGRAMDATA%/Git/'. For MinGW and MSVC builds, activate system-wide configurations by changing the $(sysconfdir) Makefile variable to an absolute path ('/etc'). Cygwin and MSys2 builds get this automatically when building with 'prefix=/usr'. Update the documentation for /etc/gitconfig and /etc/gitattributes. IOW: All Git versions on Windows (Cygwin, MinGW, MSVC and MSys2) will load configuration settings from '$(prefix)/etc/gitconfig' followed by '%PROGRAMDATA%/Git/gitconfig'. Signed-off-by: Karsten Blees <blees@dcon.de>
1 parent 90c0a74 commit 303df68

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

Documentation/config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ the Git commands' behavior. The `.git/config` file in each repository
66
is used to store the configuration for that repository, and
77
`$HOME/.gitconfig` is used to store a per-user configuration as
88
fallback values for the `.git/config` file. The file `/etc/gitconfig`
9-
can be used to store a system-wide default configuration.
9+
can be used to store a system-wide default configuration
10+
(`%PROGRAMDATA%\Git\gitconfig` on Windows).
1011

1112
The configuration variables are used by both the Git plumbing
1213
and the porcelains. The variables are divided into sections, wherein

Documentation/git-config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ See also <<FILES>>.
123123
For reading options: read only from system-wide `/etc/gitconfig`
124124
rather than from all available files.
125125
+
126+
On Windows, the system-wide config file is `%PROGRAMDATA%\Git\gitconfig`.
127+
+
126128
See also <<FILES>>.
127129

128130
--local::
@@ -231,7 +233,7 @@ $(prefix)/etc/gitconfig::
231233
(e.g. 'help.format=html' if the installation only includes html pages).
232234

233235
/etc/gitconfig::
234-
System-wide configuration file.
236+
System-wide configuration file (`%PROGRAMDATA%\Git\gitconfig` on Windows).
235237
+
236238
If git was built with relative `$(sysconfdir)`, this file will not be
237239
used, and the '--system' option refers to the installation-specific

Documentation/gitattributes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ for a single user should be placed in a file specified by the
8484
Its default value is $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME
8585
is either not set or empty, $HOME/.config/git/attributes is used instead.
8686
Attributes for all users on a system should be placed in the
87-
`/etc/gitattributes` file.
87+
`/etc/gitattributes` file (`%PROGRAMDATA%\Git\gitattributes` on Windows).
8888

8989
Sometimes you would need to override an setting of an attribute
9090
for a path to `Unspecified` state. This can be done by listing

config.mak.uname

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ endif
330330
ifeq ($(uname_S),Windows)
331331
GIT_VERSION := $(GIT_VERSION).MSVC
332332
pathsep = ;
333+
sysconfdir = /etc
333334
HAVE_ALLOCA_H = YesPlease
334335
NO_PREAD = YesPlease
335336
NEEDS_CRYPTO_WITH_SSL = YesPlease
@@ -484,6 +485,7 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
484485
endif
485486
ifneq (,$(findstring MINGW,$(uname_S)))
486487
pathsep = ;
488+
sysconfdir = /etc
487489
HAVE_ALLOCA_H = YesPlease
488490
NO_PREAD = YesPlease
489491
NEEDS_CRYPTO_WITH_SSL = YesPlease
@@ -534,6 +536,10 @@ ifneq (,$(findstring MINGW,$(uname_S)))
534536
ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
535537
htmldir = doc/git/html/
536538
prefix =
539+
# prevent conversion to Windows path on MSys1 (see
540+
# http://www.mingw.org/wiki/Posix_path_conversion)
541+
ETC_GITCONFIG = //etc\gitconfig
542+
ETC_GITATTRIBUTES = //etc\gitattributes
537543
INSTALL = /bin/install
538544
EXTLIBS += /mingw/lib/libz.a
539545
NO_R_TO_GCC_LINKER = YesPlease

exec_cmd.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@ char *system_path(const char *path)
1515
#endif
1616
struct strbuf d = STRBUF_INIT;
1717

18-
if (is_absolute_path(path))
18+
if (is_absolute_path(path)) {
19+
#ifdef _WIN32
20+
/*
21+
* On Windows (all variants), replace '/etc/' with '%PROGRAMDATA%/Git/'
22+
* (or '%ALLUSERSPROFILE%/Git' in case of Windows XP)
23+
*/
24+
if (!strncmp(path, "/etc/", 5)) {
25+
const char *sysconfdir = getenv("PROGRAMDATA");
26+
if (!sysconfdir)
27+
sysconfdir = getenv("ALLUSERSPROFILE");
28+
if (sysconfdir) {
29+
strbuf_addf(&d, "%s/Git/%s", sysconfdir, path + 5);
30+
return strbuf_detach(&d, NULL);
31+
}
32+
}
33+
#endif
1934
return xstrdup(path);
35+
}
2036

2137
#ifdef RUNTIME_PREFIX
2238
assert(argv0_path);

0 commit comments

Comments
 (0)