Skip to content

Commit 9e32d72

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. Helped-by: Andreas Heiduk <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 92afe2d commit 9e32d72

File tree

7 files changed

+43
-5
lines changed

7 files changed

+43
-5
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ the Git commands' behavior. The files `.git/config` and optionally
77
repository are used to store the configuration for that repository, and
88
`$HOME/.gitconfig` is used to store a per-user configuration as
99
fallback values for the `.git/config` file. The file `/etc/gitconfig`
10-
can be used to store a system-wide default configuration.
10+
can be used to store a system-wide default configuration. On Windows,
11+
configuration can also be stored in `C:\ProgramData\Git\config`; This
12+
file will be used also by libgit2-based software.
1113

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

Documentation/git-config.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,16 @@ FILES
284284
If not set explicitly with `--file`, there are four files where
285285
'git config' will search for configuration options:
286286

287+
$PROGRAMDATA/Git/config::
288+
(Windows-only) System-wide configuration file shared with other Git
289+
implementations. Typically `$PROGRAMDATA` points to `C:\ProgramData`.
290+
287291
$(prefix)/etc/gitconfig::
288292
System-wide configuration file.
293+
(Windows-only) This file contains only the settings which are
294+
specific for this installation of Git for Windows and which should
295+
not be shared with other Git implementations like JGit, libgit2.
296+
`--system` will select this file.
289297

290298
$XDG_CONFIG_HOME/git/config::
291299
Second user-specific configuration file. If $XDG_CONFIG_HOME is not set

Documentation/git.txt

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

587587
`GIT_CONFIG_NOSYSTEM`::
588588
Whether to skip reading settings from the system-wide
589-
`$(prefix)/etc/gitconfig` file. This environment variable can
589+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
590+
`%PROGRAMDATA%\Git\config` file). This environment variable can
590591
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
591592
predictable environment for a picky script, or you can set it
592593
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
@@ -2622,3 +2622,17 @@ int uname(struct utsname *buf)
26222622
"%u", (v >> 16) & 0x7fff);
26232623
return 0;
26242624
}
2625+
2626+
const char *program_data_config(void)
2627+
{
2628+
static struct strbuf path = STRBUF_INIT;
2629+
static unsigned initialized;
2630+
2631+
if (!initialized) {
2632+
const char *env = mingw_getenv("PROGRAMDATA");
2633+
if (env)
2634+
strbuf_addf(&path, "%s/Git/config", env);
2635+
initialized = 1;
2636+
}
2637+
return *path.buf ? path.buf : NULL;
2638+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ static inline void convert_slashes(char *path)
451451
#define PATH_SEP ';'
452452
extern char *mingw_query_user_email(void);
453453
#define query_user_email mingw_query_user_email
454+
extern const char *program_data_config(void);
455+
#define git_program_data_config program_data_config
454456
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
455457
#define PRIuMAX "I64u"
456458
#define PRId64 "I64d"

config.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,16 @@ static int do_git_config_sequence(const struct config_options *opts,
16771677
worktree_config = NULL;
16781678

16791679
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1680-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
1681-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1682-
data);
1680+
if (git_config_system()) {
1681+
if (git_program_data_config() &&
1682+
!access_or_die(git_program_data_config(), R_OK, 0))
1683+
ret += git_config_from_file(fn,
1684+
git_program_data_config(),
1685+
data);
1686+
if (!access_or_die(git_etc_gitconfig(), R_OK, 0))
1687+
ret += git_config_from_file(fn, git_etc_gitconfig(),
1688+
data);
1689+
}
16831690

16841691
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
16851692
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
@@ -412,6 +412,10 @@ static inline char *git_find_last_dir_sep(const char *path)
412412
#endif
413413
#endif
414414

415+
#ifndef git_program_data_config
416+
#define git_program_data_config() NULL
417+
#endif
418+
415419
#if defined(__HP_cc) && (__HP_cc >= 61000)
416420
#define NORETURN __attribute__((noreturn))
417421
#define NORETURN_PTR

0 commit comments

Comments
 (0)