Skip to content

Commit 2e28929

Browse files
author
Git for Windows Build Agent
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 4fb7009 + 8aa8909 commit 2e28929

File tree

7 files changed

+65
-12
lines changed

7 files changed

+65
-12
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
@@ -253,6 +253,14 @@ $XDG_CONFIG_HOME/git/config::
253253
$GIT_DIR/config::
254254
Repository specific configuration file.
255255

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

10161016
`GIT_CONFIG_NOSYSTEM`::
10171017
Whether to skip reading settings from the system-wide
1018-
`$(prefix)/etc/gitconfig` file. This environment variable can
1018+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
1019+
`%PROGRAMDATA%\Git\config` file). This environment variable can
10191020
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
10201021
predictable environment for a picky script, or you can set it
10211022
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
@@ -2772,3 +2772,22 @@ int uname(struct utsname *buf)
27722772
"%u", (v >> 16) & 0x7fff);
27732773
return 0;
27742774
}
2775+
2776+
const char *program_data_config(void)
2777+
{
2778+
static struct strbuf path = STRBUF_INIT;
2779+
static unsigned initialized;
2780+
2781+
if (!initialized) {
2782+
const char *env = mingw_getenv("PROGRAMDATA");
2783+
const char *extra = "";
2784+
if (!env) {
2785+
env = mingw_getenv("ALLUSERSPROFILE");
2786+
extra = "/Application Data";
2787+
}
2788+
if (env)
2789+
strbuf_addf(&path, "%s%s/Git/config", env, extra);
2790+
initialized = 1;
2791+
}
2792+
return *path.buf ? path.buf : NULL;
2793+
}

compat/mingw.h

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

config.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,16 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
13021302
char *repo_config = git_pathdup("config");
13031303

13041304
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1305-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
1306-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1307-
data);
1305+
if (git_config_system()) {
1306+
if (git_program_data_config() &&
1307+
!access_or_die(git_program_data_config(), R_OK, 0))
1308+
ret += git_config_from_file(fn,
1309+
git_program_data_config(),
1310+
data);
1311+
if (!access_or_die(git_etc_gitconfig(), R_OK, 0))
1312+
ret += git_config_from_file(fn, git_etc_gitconfig(),
1313+
data);
1314+
}
13081315

13091316
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
13101317
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
@@ -2059,6 +2066,20 @@ int git_config_key_is_valid(const char *key)
20592066
return !git_config_parse_key_1(key, NULL, NULL, 1);
20602067
}
20612068

2069+
static int lock_config_file(const char *config_filename,
2070+
struct lock_file **result)
2071+
{
2072+
int fd;
2073+
2074+
*result = xcalloc(1, sizeof(struct lock_file));
2075+
fd = hold_lock_file_for_update(*result, config_filename, 0);
2076+
if (fd < 0)
2077+
error("could not lock config file %s: %s", config_filename,
2078+
strerror(errno));
2079+
2080+
return fd;
2081+
}
2082+
20622083
/*
20632084
* If value==NULL, unset in (remove from) config,
20642085
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -2110,8 +2131,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
21102131
* The lock serves a purpose in addition to locking: the new
21112132
* contents of .git/config will be written into it.
21122133
*/
2113-
lock = xcalloc(1, sizeof(struct lock_file));
2114-
fd = hold_lock_file_for_update(lock, config_filename, 0);
2134+
fd = lock_config_file(config_filename, &lock);
21152135
if (fd < 0) {
21162136
error_errno("could not lock config file %s", config_filename);
21172137
free(store.key);
@@ -2412,12 +2432,9 @@ int git_config_rename_section_in_file(const char *config_filename,
24122432
if (!config_filename)
24132433
config_filename = filename_buf = git_pathdup("config");
24142434

2415-
lock = xcalloc(1, sizeof(struct lock_file));
2416-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2417-
if (out_fd < 0) {
2418-
ret = error("could not lock config file %s", config_filename);
2435+
out_fd = lock_config_file(config_filename, &lock);
2436+
if (out_fd < 0)
24192437
goto out;
2420-
}
24212438

24222439
if (!(config_file = fopen(config_filename, "rb"))) {
24232440
/* 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
@@ -376,6 +376,10 @@ static inline char *git_find_last_dir_sep(const char *path)
376376
#define query_user_email() NULL
377377
#endif
378378

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

0 commit comments

Comments
 (0)