Skip to content

Commit 674df50

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 8867772 + 0067cb1 commit 674df50

File tree

7 files changed

+74
-27
lines changed

7 files changed

+74
-27
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ $XDG_CONFIG_HOME/git/config::
254254
$GIT_DIR/config::
255255
Repository specific configuration file.
256256

257+
On Windows, as there is no central `/etc/` directory, there is yet another
258+
config file (located at `$PROGRAMDATA/Git/config`), intended to contain
259+
settings for *all* Git-related software running on the machine. Consequently,
260+
this config file takes an even lower precedence than the
261+
`$(prefix)/etc/gitconfig` file. Typically `$PROGRAMDATA` points to
262+
`C:\ProgramData` (on Windows XP the equivalent in `$ALLUSERSPROFILE` is used,
263+
i.e. `C:\Documents and Settings\All Users\Application Data\Git\config`).
264+
257265
If no further options are given, all reading options will read all of these
258266
files that are available. If the global or the system-wide configuration
259267
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
@@ -997,7 +997,8 @@ for further details.
997997

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

compat/mingw.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,3 +2737,22 @@ int uname(struct utsname *buf)
27372737
"%u", (v >> 16) & 0x7fff);
27382738
return 0;
27392739
}
2740+
2741+
const char *program_data_config(void)
2742+
{
2743+
static struct strbuf path = STRBUF_INIT;
2744+
static unsigned initialized;
2745+
2746+
if (!initialized) {
2747+
const char *env = mingw_getenv("PROGRAMDATA");
2748+
const char *extra = "";
2749+
if (!env) {
2750+
env = mingw_getenv("ALLUSERSPROFILE");
2751+
extra = "/Application Data";
2752+
}
2753+
if (env)
2754+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2755+
initialized = 1;
2756+
}
2757+
return *path.buf ? path.buf : NULL;
2758+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ static inline void convert_slashes(char *path)
424424
int mingw_offset_1st_component(const char *path);
425425
#define offset_1st_component mingw_offset_1st_component
426426
#define PATH_SEP ';'
427+
extern const char *program_data_config(void);
428+
#define git_program_data_config program_data_config
427429
#ifndef __MINGW64_VERSION_MAJOR
428430
#define PRIuMAX "I64u"
429431
#define PRId64 "I64d"

config.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,32 +1207,34 @@ int git_config_system(void)
12071207
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12081208
}
12091209

1210+
static inline void config_from_file_gently(config_fn_t fn, const char *filename,
1211+
void *data, unsigned access_flags, int *ret, int *found) {
1212+
if (!filename || access_or_die(filename, R_OK, access_flags))
1213+
return;
1214+
1215+
*ret += git_config_from_file(fn, filename, data);
1216+
(*found)++;
1217+
}
1218+
12101219
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12111220
{
12121221
int ret = 0, found = 0;
12131222
char *xdg_config = xdg_config_home("config");
12141223
char *user_config = expand_user_path("~/.gitconfig");
12151224

1216-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1217-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1218-
data);
1219-
found += 1;
1225+
if (git_config_system()) {
1226+
config_from_file_gently(fn, git_program_data_config(), data,
1227+
0, &ret, &found);
1228+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1229+
&ret, &found);
12201230
}
12211231

1222-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1223-
ret += git_config_from_file(fn, xdg_config, data);
1224-
found += 1;
1225-
}
1226-
1227-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1228-
ret += git_config_from_file(fn, user_config, data);
1229-
found += 1;
1230-
}
1232+
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
1233+
&ret, &found);
1234+
config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK,
1235+
&ret, &found);
12311236

1232-
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
1233-
ret += git_config_from_file(fn, repo_config, data);
1234-
found += 1;
1235-
}
1237+
config_from_file_gently(fn, repo_config, data, 0, &ret, &found);
12361238

12371239
switch (git_config_from_parameters(fn, data)) {
12381240
case -1: /* error */
@@ -1981,6 +1983,20 @@ int git_config_key_is_valid(const char *key)
19811983
return !git_config_parse_key_1(key, NULL, NULL, 1);
19821984
}
19831985

1986+
static int lock_config_file(const char *config_filename,
1987+
struct lock_file **result)
1988+
{
1989+
int fd;
1990+
1991+
*result = xcalloc(1, sizeof(struct lock_file));
1992+
fd = hold_lock_file_for_update(*result, config_filename, 0);
1993+
if (fd < 0)
1994+
error("could not lock config file %s: %s", config_filename,
1995+
strerror(errno));
1996+
1997+
return fd;
1998+
}
1999+
19842000
/*
19852001
* If value==NULL, unset in (remove from) config,
19862002
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -2032,10 +2048,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
20322048
* The lock serves a purpose in addition to locking: the new
20332049
* contents of .git/config will be written into it.
20342050
*/
2035-
lock = xcalloc(1, sizeof(struct lock_file));
2036-
fd = hold_lock_file_for_update(lock, config_filename, 0);
2051+
fd = lock_config_file(config_filename, &lock);
20372052
if (fd < 0) {
2038-
error("could not lock config file %s: %s", config_filename, strerror(errno));
20392053
free(store.key);
20402054
ret = CONFIG_NO_LOCK;
20412055
goto out_free;
@@ -2334,12 +2348,9 @@ int git_config_rename_section_in_file(const char *config_filename,
23342348
if (!config_filename)
23352349
config_filename = filename_buf = git_pathdup("config");
23362350

2337-
lock = xcalloc(1, sizeof(struct lock_file));
2338-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2339-
if (out_fd < 0) {
2340-
ret = error("could not lock config file %s", config_filename);
2351+
out_fd = lock_config_file(config_filename, &lock);
2352+
if (out_fd < 0)
23412353
goto out;
2342-
}
23432354

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

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ static inline char *git_find_last_dir_sep(const char *path)
375375
#define find_last_dir_sep git_find_last_dir_sep
376376
#endif
377377

378+
#ifndef git_program_data_config
379+
#define git_program_data_config() NULL
380+
#endif
381+
378382
#if defined(__HP_cc) && (__HP_cc >= 61000)
379383
#define NORETURN __attribute__((noreturn))
380384
#define NORETURN_PTR

0 commit comments

Comments
 (0)