Browse files

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 <>
  • Loading branch information...
1 parent ecbcc74 commit 153328ba92ca6cf921d2272fa7e355603cbf71b7 @dscho dscho committed Apr 22, 2015
Showing with 39 additions and 3 deletions.
  1. +3 −1 Documentation/config.txt
  2. +5 −0 Documentation/git-config.txt
  3. +2 −1 Documentation/git.txt
  4. +19 −0 compat/mingw.c
  5. +2 −0 compat/mingw.h
  6. +4 −1 config.c
  7. +4 −0 git-compat-util.h
@@ -6,7 +6,9 @@ the Git commands' behavior. The `.git/config` file in each repository
is used to store the configuration for that repository, and
`$HOME/.gitconfig` is used to store a per-user configuration as
fallback values for the `.git/config` file. The file `/etc/gitconfig`
-can be used to store a system-wide default configuration.
+can be used to store a system-wide default configuration. On Windows,
+configuration can also be stored in `C:\ProgramData\Git\config`; This
+file will be used also by libgit2-based software.
The configuration variables are used by both the Git plumbing
and the porcelains. The variables are divided into sections, wherein
@@ -242,6 +242,11 @@ $XDG_CONFIG_HOME/git/config::
Repository specific configuration file.
+On Windows, as there is no central `/etc/` directory, there is yet another
+config file, intended to contain settings for *all* Git-related software
+running on the machine. Consequently, this config file takes an even lower
+precedence than the `$(prefix)/etc/gitconfig` file.
If no further options are given, all reading options will read all of these
files that are available. If the global or the system-wide configuration
file are not available they will be ignored. If the repository configuration
@@ -967,7 +967,8 @@ for further details.
Whether to skip reading settings from the system-wide
- `$(prefix)/etc/gitconfig` file. This environment variable can
+ `$(prefix)/etc/gitconfig` file (and on Windows, also from the
+ `%PROGRAMDATA%\Git\config` file). This environment variable can
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
predictable environment for a picky script, or you can set it
temporarily to avoid using a buggy `/etc/gitconfig` file while
@@ -2377,3 +2377,22 @@ int uname(struct utsname *buf)
sprintf(buf->version, "%u", (v >> 16) & 0x7fff);
return 0;
+const char *program_data_config(void)
+ static struct strbuf path = STRBUF_INIT;
+ static unsigned initialized;
+ if (!initialized) {
+ const char *env = mingw_getenv("PROGRAMDATA");
+ const char *extra = "";
+ if (!env) {
+ env = mingw_getenv("ALLUSERSPROFILE");
+ extra = "/Application Data";
+ }
+ if (env)
+ strbuf_addf(&path, "%s%s/Git/config", env, extra);
+ initialized = 1;
+ }
+ return *path.buf ? path.buf : NULL;
@@ -405,6 +405,8 @@ static inline char *mingw_find_last_dir_sep(const char *path)
int mingw_offset_1st_component(const char *path);
#define offset_1st_component mingw_offset_1st_component
#define PATH_SEP ';'
+extern const char *program_data_config(void);
+#define git_program_data_config program_data_config
#define PRIuMAX "I64u"
#define PRId64 "I64d"
@@ -1218,9 +1218,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
char *xdg_config = xdg_config_home("config");
char *user_config = expand_user_path("~/.gitconfig");
- if (git_config_system())
+ if (git_config_system()) {
+ config_from_file_gently(fn, git_program_data_config(), data,
+ 0, &ret, &found);
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
&ret, &found);
+ }
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
&ret, &found);
@@ -357,6 +357,10 @@ static inline char *git_find_last_dir_sep(const char *path)
#define find_last_dir_sep git_find_last_dir_sep
+#ifndef git_program_data_config
+#define git_program_data_config() NULL
#if defined(__HP_cc) && (__HP_cc >= 61000)
#define NORETURN __attribute__((noreturn))

0 comments on commit 153328b

Please sign in to comment.