Skip to content

Commit 3e1e9c5

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 2db9012 + 52c2d0f commit 3e1e9c5

File tree

7 files changed

+75
-27
lines changed

7 files changed

+75
-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: 5 additions & 0 deletions
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

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

963963
'GIT_CONFIG_NOSYSTEM'::
964964
Whether to skip reading settings from the system-wide
965-
`$(prefix)/etc/gitconfig` file. This environment variable can
965+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
966+
`%PROGRAMDATA%\Git\config` file). This environment variable can
966967
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
967968
predictable environment for a picky script, or you can set it
968969
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
@@ -2689,3 +2689,22 @@ int uname(struct utsname *buf)
26892689
sprintf(buf->version, "%u", (v >> 16) & 0x7fff);
26902690
return 0;
26912691
}
2692+
2693+
const char *program_data_config(void)
2694+
{
2695+
static struct strbuf path = STRBUF_INIT;
2696+
static unsigned initialized;
2697+
2698+
if (!initialized) {
2699+
const char *env = mingw_getenv("PROGRAMDATA");
2700+
const char *extra = "";
2701+
if (!env) {
2702+
env = mingw_getenv("ALLUSERSPROFILE");
2703+
extra = "/Application Data";
2704+
}
2705+
if (env)
2706+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2707+
initialized = 1;
2708+
}
2709+
return *path.buf ? path.buf : NULL;
2710+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ static inline char *mingw_find_last_dir_sep(const char *path)
403403
int mingw_offset_1st_component(const char *path);
404404
#define offset_1st_component mingw_offset_1st_component
405405
#define PATH_SEP ';'
406+
extern const char *program_data_config(void);
407+
#define git_program_data_config program_data_config
406408
#ifndef __MINGW64_VERSION_MAJOR
407409
#define PRIuMAX "I64u"
408410
#define PRId64 "I64d"

config.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,32 +1203,34 @@ int git_config_system(void)
12031203
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12041204
}
12051205

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

1212-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1213-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1214-
data);
1215-
found += 1;
1221+
if (git_config_system()) {
1222+
config_from_file_gently(fn, git_program_data_config(), data,
1223+
0, &ret, &found);
1224+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1225+
&ret, &found);
12161226
}
12171227

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

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

12331235
switch (git_config_from_parameters(fn, data)) {
12341236
case -1: /* error */
@@ -1925,6 +1927,24 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
19251927
return -CONFIG_INVALID_KEY;
19261928
}
19271929

1930+
static int lock_config_file(const char *config_filename,
1931+
struct lock_file **result)
1932+
{
1933+
int fd;
1934+
1935+
/* make sure the parent directory exists */
1936+
if (safe_create_leading_directories_const(config_filename))
1937+
return error("could not create parent directory of %s",
1938+
config_filename);
1939+
*result = xcalloc(1, sizeof(struct lock_file));
1940+
fd = hold_lock_file_for_update(*result, config_filename, 0);
1941+
if (fd < 0)
1942+
error("could not lock config file %s: %s", config_filename,
1943+
strerror(errno));
1944+
1945+
return fd;
1946+
}
1947+
19281948
/*
19291949
* If value==NULL, unset in (remove from) config,
19301950
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -1975,10 +1995,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
19751995
* The lock serves a purpose in addition to locking: the new
19761996
* contents of .git/config will be written into it.
19771997
*/
1978-
lock = xcalloc(1, sizeof(struct lock_file));
1979-
fd = hold_lock_file_for_update(lock, config_filename, 0);
1998+
fd = lock_config_file(config_filename, &lock);
19801999
if (fd < 0) {
1981-
error("could not lock config file %s: %s", config_filename, strerror(errno));
19822000
free(store.key);
19832001
ret = CONFIG_NO_LOCK;
19842002
goto out_free;
@@ -2257,12 +2275,9 @@ int git_config_rename_section_in_file(const char *config_filename,
22572275
if (!config_filename)
22582276
config_filename = filename_buf = git_pathdup("config");
22592277

2260-
lock = xcalloc(1, sizeof(struct lock_file));
2261-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2262-
if (out_fd < 0) {
2263-
ret = error("could not lock config file %s", config_filename);
2278+
out_fd = lock_config_file(config_filename, &lock);
2279+
if (out_fd < 0)
22642280
goto out;
2265-
}
22662281

22672282
if (!(config_file = fopen(config_filename, "rb"))) {
22682283
/* 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
@@ -361,6 +361,10 @@ static inline char *git_find_last_dir_sep(const char *path)
361361
#define find_last_dir_sep git_find_last_dir_sep
362362
#endif
363363

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

0 commit comments

Comments
 (0)