Skip to content

Commit b8b6074

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. 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 215373f commit b8b6074

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-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
@@ -281,6 +281,11 @@ $XDG_CONFIG_HOME/git/config::
281281
$GIT_DIR/config::
282282
Repository specific configuration file.
283283

284+
On Windows, as there is no central `/etc/` directory, there is yet another
285+
config file, intended to contain settings for *all* Git-related software
286+
running on the machine. Consequently, this config file takes an even lower
287+
precedence than the `$(prefix)/etc/gitconfig` file.
288+
284289
If no further options are given, all reading options will read all of these
285290
files that are available. If the global or the system-wide configuration
286291
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
@@ -567,7 +567,8 @@ for further details.
567567

568568
`GIT_CONFIG_NOSYSTEM`::
569569
Whether to skip reading settings from the system-wide
570-
`$(prefix)/etc/gitconfig` file. This environment variable can
570+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
571+
`%PROGRAMDATA%\Git\config` file). This environment variable can
571572
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
572573
predictable environment for a picky script, or you can set it
573574
temporarily to avoid using a buggy `/etc/gitconfig` file while

compat/mingw.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,3 +2362,17 @@ int uname(struct utsname *buf)
23622362
"%u", (v >> 16) & 0x7fff);
23632363
return 0;
23642364
}
2365+
2366+
const char *program_data_config(void)
2367+
{
2368+
static struct strbuf path = STRBUF_INIT;
2369+
static unsigned initialized;
2370+
2371+
if (!initialized) {
2372+
const char *env = mingw_getenv("PROGRAMDATA");
2373+
if (env)
2374+
strbuf_addf(&path, "%s/Git/config", env);
2375+
initialized = 1;
2376+
}
2377+
return *path.buf ? path.buf : NULL;
2378+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ int mingw_offset_1st_component(const char *path);
426426
#define PATH_SEP ';'
427427
extern char *mingw_query_user_email(void);
428428
#define query_user_email mingw_query_user_email
429+
extern const char *program_data_config(void);
430+
#define git_program_data_config program_data_config
429431
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
430432
#define PRIuMAX "I64u"
431433
#define PRId64 "I64d"

config.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,9 +1680,16 @@ static int do_git_config_sequence(const struct config_options *opts,
16801680
repo_config = NULL;
16811681

16821682
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1683-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
1684-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1685-
data);
1683+
if (git_config_system()) {
1684+
if (git_program_data_config() &&
1685+
!access_or_die(git_program_data_config(), R_OK, 0))
1686+
ret += git_config_from_file(fn,
1687+
git_program_data_config(),
1688+
data);
1689+
if (!access_or_die(git_etc_gitconfig(), R_OK, 0))
1690+
ret += git_config_from_file(fn, git_etc_gitconfig(),
1691+
data);
1692+
}
16861693

16871694
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
16881695
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
@@ -386,6 +386,10 @@ static inline char *git_find_last_dir_sep(const char *path)
386386
#define query_user_email() NULL
387387
#endif
388388

389+
#ifndef git_program_data_config
390+
#define git_program_data_config() NULL
391+
#endif
392+
389393
#if defined(__HP_cc) && (__HP_cc >= 61000)
390394
#define NORETURN __attribute__((noreturn))
391395
#define NORETURN_PTR

0 commit comments

Comments
 (0)