Skip to content

Commit

Permalink
Windows: add support for a Windows-wide configuration
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed May 27, 2015
1 parent fd9fa26 commit 4b738e2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Documentation/git-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ $XDG_CONFIG_HOME/git/config::
$GIT_DIR/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
Expand Down
3 changes: 2 additions & 1 deletion Documentation/git.txt
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,8 @@ for further details.

'GIT_CONFIG_NOSYSTEM'::
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
Expand Down
16 changes: 16 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2371,3 +2371,19 @@ void mingw_startup()
/* init length of current directory for handle_long_path */
current_directory_len = GetCurrentDirectoryW(0, NULL);
}

const char *program_data_config(void)
{
static struct strbuf path = STRBUF_INIT;
static unsigned initialized;

if (!initialized) {
const char *env = mingw_getenv("PROGRAMDATA");
if (!env)
env = mingw_getenv("ALLUSERSPROFILE");
if (env)
strbuf_addf(&path, "%s/Git/config", env);
initialized = 1;
}
return *path.buf ? path.buf : NULL;
}
2 changes: 2 additions & 0 deletions compat/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,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
#ifndef __MINGW64_VERSION_MAJOR
#define PRIuMAX "I64u"
#define PRId64 "I64d"
Expand Down
5 changes: 4 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,9 +1218,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)

home_config_paths(&user_config, &xdg_config, "config");

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);
Expand Down
4 changes: 4 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ static inline char *git_find_last_dir_sep(const char *path)
#define find_last_dir_sep git_find_last_dir_sep
#endif

#ifndef git_program_data_config
#define git_program_data_config() NULL
#endif

#if defined(__HP_cc) && (__HP_cc >= 61000)
#define NORETURN __attribute__((noreturn))
#define NORETURN_PTR
Expand Down

0 comments on commit 4b738e2

Please sign in to comment.