Skip to content

Commit d5656ea

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 ca8ddb1 commit d5656ea

File tree

7 files changed

+45
-5
lines changed

7 files changed

+45
-5
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
@@ -253,6 +253,11 @@ $XDG_CONFIG_HOME/git/config::
253253
$GIT_DIR/config::
254254
Repository specific configuration file.
255255

256+
On Windows, as there is no central `/etc/` directory, there is yet another
257+
config file, intended to contain settings for *all* Git-related software
258+
running on the machine. Consequently, this config file takes an even lower
259+
precedence than the `$(prefix)/etc/gitconfig` file.
260+
256261
If no further options are given, all reading options will read all of these
257262
files that are available. If the global or the system-wide configuration
258263
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
@@ -1014,7 +1014,8 @@ for further details.
10141014

10151015
`GIT_CONFIG_NOSYSTEM`::
10161016
Whether to skip reading settings from the system-wide
1017-
`$(prefix)/etc/gitconfig` file. This environment variable can
1017+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
1018+
`%PROGRAMDATA%\Git\config` file). This environment variable can
10181019
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
10191020
predictable environment for a picky script, or you can set it
10201021
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
@@ -2413,3 +2413,22 @@ int uname(struct utsname *buf)
24132413
"%u", (v >> 16) & 0x7fff);
24142414
return 0;
24152415
}
2416+
2417+
const char *program_data_config(void)
2418+
{
2419+
static struct strbuf path = STRBUF_INIT;
2420+
static unsigned initialized;
2421+
2422+
if (!initialized) {
2423+
const char *env = mingw_getenv("PROGRAMDATA");
2424+
const char *extra = "";
2425+
if (!env) {
2426+
env = mingw_getenv("ALLUSERSPROFILE");
2427+
extra = "/Application Data";
2428+
}
2429+
if (env)
2430+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2431+
initialized = 1;
2432+
}
2433+
return *path.buf ? path.buf : NULL;
2434+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ static inline void convert_slashes(char *path)
434434
int mingw_offset_1st_component(const char *path);
435435
#define offset_1st_component mingw_offset_1st_component
436436
#define PATH_SEP ';'
437+
extern const char *program_data_config(void);
438+
#define git_program_data_config program_data_config
437439
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
438440
#define PRIuMAX "I64u"
439441
#define PRId64 "I64d"

config.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,16 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
13021302
char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
13031303

13041304
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1305-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
1306-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1307-
data);
1305+
if (git_config_system()) {
1306+
if (git_program_data_config() &&
1307+
!access_or_die(git_program_data_config(), R_OK, 0))
1308+
ret += git_config_from_file(fn,
1309+
git_program_data_config(),
1310+
data);
1311+
if (!access_or_die(git_etc_gitconfig(), R_OK, 0))
1312+
ret += git_config_from_file(fn, git_etc_gitconfig(),
1313+
data);
1314+
}
13081315

13091316
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
13101317
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))

git-compat-util.h

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

375+
#ifndef git_program_data_config
376+
#define git_program_data_config() NULL
377+
#endif
378+
375379
#if defined(__HP_cc) && (__HP_cc >= 61000)
376380
#define NORETURN __attribute__((noreturn))
377381
#define NORETURN_PTR

0 commit comments

Comments
 (0)