Skip to content

Commit dfce1fb

Browse files
committed
Windows: add support for a Windows-wide configuration
Between the libgit2 and the Git for Windows project, there has been a discussion how we could share Git configuration to avoid duplication (or worse: skew). Earlier, libgit2 was nice enough to just re-use Git for Windows' C:\Program Files (x86)\Git\etc\gitconfig but with the upcoming Git for Windows 2.x, there would be more paths to search, as we will have 64-bit and 32-bit versions, and the corresponding config files will be in %PROGRAMFILES%\Git\mingw64\etc and ...\mingw32\etc, respectively. Worse: there are portable Git for Windows versions out there which live in totally unrelated directories, still. Therefore we came to a consensus to use `%PROGRAMDATA%\Git\config` as the location for shared Git settings that are of wider interest than just Git for Windows. On XP, there is no %PROGRAMDATA%, therefore we need to use "%ALLUSERSPROFILE%\Application Data\Git\config" in those setups. Of course, the configuration in `%PROGRAMDATA%\Git\config` has the widest reach, therefore it must take the lowest precedence, i.e. Git for Windows can still override settings in its `etc/gitconfig` file. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c230201 commit dfce1fb

File tree

7 files changed

+39
-3
lines changed

7 files changed

+39
-3
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ 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. On Windows,
10+
configuration can also be stored in `C:\ProgramData\Git\config`; This
11+
file will be used also by libgit2-based software.
1012

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

Documentation/git-config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ $XDG_CONFIG_HOME/git/config::
246246
$GIT_DIR/config::
247247
Repository specific configuration file.
248248

249+
On Windows, as there is no central `/etc/` directory, there is yet another
250+
config file, intended to contain settings for *all* Git-related software
251+
running on the machine. Consequently, this config file takes an even lower
252+
precedence than the `$(prefix)/etc/gitconfig` file.
253+
249254
If no further options are given, all reading options will read all of these
250255
files that are available. If the global or the system-wide configuration
251256
file are not available they will be ignored. If the repository configuration

Documentation/git.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ for further details.
985985

986986
'GIT_CONFIG_NOSYSTEM'::
987987
Whether to skip reading settings from the system-wide
988-
`$(prefix)/etc/gitconfig` file. This environment variable can
988+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
989+
`%PROGRAMDATA%\Git\config` file). This environment variable can
989990
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
990991
predictable environment for a picky script, or you can set it
991992
temporarily to avoid using a buggy `/etc/gitconfig` file while

compat/mingw.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,3 +2396,22 @@ int uname(struct utsname *buf)
23962396
"%u", (v >> 16) & 0x7fff);
23972397
return 0;
23982398
}
2399+
2400+
const char *program_data_config(void)
2401+
{
2402+
static struct strbuf path = STRBUF_INIT;
2403+
static unsigned initialized;
2404+
2405+
if (!initialized) {
2406+
const char *env = mingw_getenv("PROGRAMDATA");
2407+
const char *extra = "";
2408+
if (!env) {
2409+
env = mingw_getenv("ALLUSERSPROFILE");
2410+
extra = "/Application Data";
2411+
}
2412+
if (env)
2413+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2414+
initialized = 1;
2415+
}
2416+
return *path.buf ? path.buf : NULL;
2417+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ static inline char *mingw_find_last_dir_sep(const char *path)
408408
int mingw_offset_1st_component(const char *path);
409409
#define offset_1st_component mingw_offset_1st_component
410410
#define PATH_SEP ';'
411+
extern const char *program_data_config(void);
412+
#define git_program_data_config program_data_config
411413
#ifndef __MINGW64_VERSION_MAJOR
412414
#define PRIuMAX "I64u"
413415
#define PRId64 "I64d"

config.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,9 +1218,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12181218
char *xdg_config = xdg_config_home("config");
12191219
char *user_config = expand_user_path("~/.gitconfig");
12201220

1221-
if (git_config_system())
1221+
if (git_config_system()) {
1222+
config_from_file_gently(fn, git_program_data_config(), data,
1223+
0, &ret, &found);
12221224
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
12231225
&ret, &found);
1226+
}
12241227

12251228
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
12261229
&ret, &found);

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ static inline char *git_find_last_dir_sep(const char *path)
361361
#define find_last_dir_sep git_find_last_dir_sep
362362
#endif
363363

364+
#ifndef git_program_data_config
365+
#define git_program_data_config() NULL
366+
#endif
367+
364368
#if defined(__HP_cc) && (__HP_cc >= 61000)
365369
#define NORETURN __attribute__((noreturn))
366370
#define NORETURN_PTR

0 commit comments

Comments
 (0)