Skip to content

Commit

Permalink
Merge branch 'program-data-config'
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed May 27, 2015
2 parents 9967f9f + 4b738e2 commit 57190ae
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 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 @@ -2654,3 +2654,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 @@ -390,6 +390,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
65 changes: 40 additions & 25 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,15 @@ int git_config_system(void)
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
}

static inline void config_from_file_gently(config_fn_t fn, const char *filename,
void *data, unsigned access_flags, int *ret, int *found) {
if (!filename || access_or_die(filename, R_OK, access_flags))
return;

*ret += git_config_from_file(fn, filename, data);
(*found)++;
}

int git_config_early(config_fn_t fn, void *data, const char *repo_config)
{
int ret = 0, found = 0;
Expand All @@ -1209,26 +1218,19 @@ 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() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
ret += git_config_from_file(fn, git_etc_gitconfig(),
data);
found += 1;
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);
}

if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
ret += git_config_from_file(fn, xdg_config, data);
found += 1;
}

if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
ret += git_config_from_file(fn, user_config, data);
found += 1;
}
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
&ret, &found);
config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK,
&ret, &found);

if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
ret += git_config_from_file(fn, repo_config, data);
found += 1;
}
config_from_file_gently(fn, repo_config, data, 0, &ret, &found);

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

static int lock_config_file(const char *config_filename,
struct lock_file **result)
{
int fd;

/* make sure the parent directory exists */
if (safe_create_leading_directories_const(config_filename))
return error("could not create parent directory of %s",
config_filename);
*result = xcalloc(1, sizeof(struct lock_file));
fd = hold_lock_file_for_update(*result, config_filename, 0);
if (fd < 0)
error("could not lock config file %s: %s", config_filename,
strerror(errno));

return fd;
}

/*
* If value==NULL, unset in (remove from) config,
* if value_regex!=NULL, disregard key/value pairs where value does not match.
Expand Down Expand Up @@ -1973,10 +1993,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
* The lock serves a purpose in addition to locking: the new
* contents of .git/config will be written into it.
*/
lock = xcalloc(1, sizeof(struct lock_file));
fd = hold_lock_file_for_update(lock, config_filename, 0);
fd = lock_config_file(config_filename, &lock);
if (fd < 0) {
error("could not lock config file %s: %s", config_filename, strerror(errno));
free(store.key);
ret = CONFIG_NO_LOCK;
goto out_free;
Expand Down Expand Up @@ -2244,12 +2262,9 @@ int git_config_rename_section_in_file(const char *config_filename,
if (!config_filename)
config_filename = filename_buf = git_pathdup("config");

lock = xcalloc(1, sizeof(struct lock_file));
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
if (out_fd < 0) {
ret = error("could not lock config file %s", config_filename);
out_fd = lock_config_file(config_filename, &lock);
if (out_fd < 0)
goto out;
}

if (!(config_file = fopen(config_filename, "rb"))) {
/* no config file means nothing to rename, no error */
Expand Down
4 changes: 4 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,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 57190ae

Please sign in to comment.