Skip to content

Commit e96e074

Browse files
committed
Merge branch 'program-data-config'
This branch introduces support for reading the "Windows-wide" Git configuration from `%PROGRAMDATA%\Git\config`. As these settings are intended to be shared between *all* Git-related software, that config file takes an even lower precedence than `$(prefix)/etc/gitconfig`. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 8852f60 + ddbb590 commit e96e074

File tree

7 files changed

+75
-27
lines changed

7 files changed

+75
-27
lines changed

Documentation/config.txt

+3-1
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

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ $XDG_CONFIG_HOME/git/config::
242242
$GIT_DIR/config::
243243
Repository specific configuration file.
244244

245+
On Windows, as there is no central `/etc/` directory, there is yet another
246+
config file, intended to contain settings for *all* Git-related software
247+
running on the machine. Consequently, this config file takes an even lower
248+
precedence than the `$(prefix)/etc/gitconfig` file.
249+
245250
If no further options are given, all reading options will read all of these
246251
files that are available. If the global or the system-wide configuration
247252
file are not available they will be ignored. If the repository configuration

Documentation/git.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ for further details.
943943

944944
'GIT_CONFIG_NOSYSTEM'::
945945
Whether to skip reading settings from the system-wide
946-
`$(prefix)/etc/gitconfig` file. This environment variable can
946+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
947+
`%PROGRAMDATA%\Git\config` file). This environment variable can
947948
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
948949
predictable environment for a picky script, or you can set it
949950
temporarily to avoid using a buggy `/etc/gitconfig` file while

compat/mingw.c

+19
Original file line numberDiff line numberDiff line change
@@ -2691,3 +2691,22 @@ void mingw_startup()
26912691
/* init length of current directory for handle_long_path */
26922692
current_directory_len = GetCurrentDirectoryW(0, NULL);
26932693
}
2694+
2695+
const char *program_data_config(void)
2696+
{
2697+
static struct strbuf path = STRBUF_INIT;
2698+
static unsigned initialized;
2699+
2700+
if (!initialized) {
2701+
const char *env = mingw_getenv("PROGRAMDATA");
2702+
const char *extra = "";
2703+
if (!env) {
2704+
env = mingw_getenv("ALLUSERSPROFILE");
2705+
extra = "/Application Data";
2706+
}
2707+
if (env)
2708+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2709+
initialized = 1;
2710+
}
2711+
return *path.buf ? path.buf : NULL;
2712+
}

compat/mingw.h

+2
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ static inline char *mingw_find_last_dir_sep(const char *path)
390390
int mingw_offset_1st_component(const char *path);
391391
#define offset_1st_component mingw_offset_1st_component
392392
#define PATH_SEP ';'
393+
extern const char *program_data_config(void);
394+
#define git_program_data_config program_data_config
393395
#ifndef __MINGW64_VERSION_MAJOR
394396
#define PRIuMAX "I64u"
395397
#define PRId64 "I64d"

config.c

+40-25
Original file line numberDiff line numberDiff line change
@@ -1201,32 +1201,34 @@ int git_config_system(void)
12011201
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12021202
}
12031203

1204+
static inline void config_from_file_gently(config_fn_t fn, const char *filename,
1205+
void *data, unsigned access_flags, int *ret, int *found) {
1206+
if (!filename || access_or_die(filename, R_OK, access_flags))
1207+
return;
1208+
1209+
*ret += git_config_from_file(fn, filename, data);
1210+
(*found)++;
1211+
}
1212+
12041213
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12051214
{
12061215
int ret = 0, found = 0;
12071216
char *xdg_config = xdg_config_home("config");
12081217
char *user_config = expand_user_path("~/.gitconfig");
12091218

1210-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1211-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1212-
data);
1213-
found += 1;
1219+
if (git_config_system()) {
1220+
config_from_file_gently(fn, git_program_data_config(), data,
1221+
0, &ret, &found);
1222+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1223+
&ret, &found);
12141224
}
12151225

1216-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1217-
ret += git_config_from_file(fn, xdg_config, data);
1218-
found += 1;
1219-
}
1220-
1221-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1222-
ret += git_config_from_file(fn, user_config, data);
1223-
found += 1;
1224-
}
1226+
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
1227+
&ret, &found);
1228+
config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK,
1229+
&ret, &found);
12251230

1226-
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
1227-
ret += git_config_from_file(fn, repo_config, data);
1228-
found += 1;
1229-
}
1231+
config_from_file_gently(fn, repo_config, data, 0, &ret, &found);
12301232

12311233
switch (git_config_from_parameters(fn, data)) {
12321234
case -1: /* error */
@@ -1923,6 +1925,24 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
19231925
return -CONFIG_INVALID_KEY;
19241926
}
19251927

1928+
static int lock_config_file(const char *config_filename,
1929+
struct lock_file **result)
1930+
{
1931+
int fd;
1932+
1933+
/* make sure the parent directory exists */
1934+
if (safe_create_leading_directories_const(config_filename))
1935+
return error("could not create parent directory of %s",
1936+
config_filename);
1937+
*result = xcalloc(1, sizeof(struct lock_file));
1938+
fd = hold_lock_file_for_update(*result, config_filename, 0);
1939+
if (fd < 0)
1940+
error("could not lock config file %s: %s", config_filename,
1941+
strerror(errno));
1942+
1943+
return fd;
1944+
}
1945+
19261946
/*
19271947
* If value==NULL, unset in (remove from) config,
19281948
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -1971,10 +1991,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
19711991
* The lock serves a purpose in addition to locking: the new
19721992
* contents of .git/config will be written into it.
19731993
*/
1974-
lock = xcalloc(1, sizeof(struct lock_file));
1975-
fd = hold_lock_file_for_update(lock, config_filename, 0);
1994+
fd = lock_config_file(config_filename, &lock);
19761995
if (fd < 0) {
1977-
error("could not lock config file %s: %s", config_filename, strerror(errno));
19781996
free(store.key);
19791997
ret = CONFIG_NO_LOCK;
19801998
goto out_free;
@@ -2242,12 +2260,9 @@ int git_config_rename_section_in_file(const char *config_filename,
22422260
if (!config_filename)
22432261
config_filename = filename_buf = git_pathdup("config");
22442262

2245-
lock = xcalloc(1, sizeof(struct lock_file));
2246-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2247-
if (out_fd < 0) {
2248-
ret = error("could not lock config file %s", config_filename);
2263+
out_fd = lock_config_file(config_filename, &lock);
2264+
if (out_fd < 0)
22492265
goto out;
2250-
}
22512266

22522267
if (!(config_file = fopen(config_filename, "rb"))) {
22532268
/* no config file means nothing to rename, no error */

git-compat-util.h

+4
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ static inline char *git_find_last_dir_sep(const char *path)
362362
#define find_last_dir_sep git_find_last_dir_sep
363363
#endif
364364

365+
#ifndef git_program_data_config
366+
#define git_program_data_config() NULL
367+
#endif
368+
365369
#if defined(__HP_cc) && (__HP_cc >= 61000)
366370
#define NORETURN __attribute__((noreturn))
367371
#define NORETURN_PTR

0 commit comments

Comments
 (0)