summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2017-04-26 06:39:05 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-04-26 06:39:05 (GMT)
commitc9672ba4c86a5fb18ea20d1c4a2c0eb6a731f3cb (patch)
tree47c0d32f51053046b1a47b3113d8803f2a7c1279 /config.c
parent46bdfa3975665aaa9b7383466b529354d117e14d (diff)
parente145a0bc9b8711fe1c6cfad29af52ef06ce4c1ec (diff)
downloadgit-c9672ba4c86a5fb18ea20d1c4a2c0eb6a731f3cb.zip
git-c9672ba4c86a5fb18ea20d1c4a2c0eb6a731f3cb.tar.gz
git-c9672ba4c86a5fb18ea20d1c4a2c0eb6a731f3cb.tar.bz2
Merge branch 'nd/conditional-config-in-early-config'
The recently introduced conditional inclusion of configuration did not work well when early-config mechanism was involved. * nd/conditional-config-in-early-config: config: correct file reading order in read_early_config() config: handle conditional include when $GIT_DIR is not set up config: prepare to pass more info in git_config_with_options()
Diffstat (limited to 'config.c')
-rw-r--r--config.c62
1 files changed, 42 insertions, 20 deletions
diff --git a/config.c b/config.c
index 0daaed3..b4a3205 100644
--- a/config.c
+++ b/config.c
@@ -207,13 +207,22 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
return prefix;
}
-static int include_by_gitdir(const char *cond, size_t cond_len, int icase)
+static int include_by_gitdir(const struct config_options *opts,
+ const char *cond, size_t cond_len, int icase)
{
struct strbuf text = STRBUF_INIT;
struct strbuf pattern = STRBUF_INIT;
int ret = 0, prefix;
+ const char *git_dir;
- strbuf_realpath(&text, get_git_dir(), 1);
+ if (opts->git_dir)
+ git_dir = opts->git_dir;
+ else if (have_git_dir())
+ git_dir = get_git_dir();
+ else
+ goto done;
+
+ strbuf_realpath(&text, git_dir, 1);
strbuf_add(&pattern, cond, cond_len);
prefix = prepare_include_condition_pattern(&pattern);
@@ -242,13 +251,14 @@ done:
return ret;
}
-static int include_condition_is_true(const char *cond, size_t cond_len)
+static int include_condition_is_true(const struct config_options *opts,
+ const char *cond, size_t cond_len)
{
if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
- return include_by_gitdir(cond, cond_len, 0);
+ return include_by_gitdir(opts, cond, cond_len, 0);
else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
- return include_by_gitdir(cond, cond_len, 1);
+ return include_by_gitdir(opts, cond, cond_len, 1);
/* unknown conditionals are always false */
return 0;
@@ -273,7 +283,7 @@ int git_config_include(const char *var, const char *value, void *data)
ret = handle_path_include(value, inc);
if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
- (cond && include_condition_is_true(cond, cond_len)) &&
+ (cond && include_condition_is_true(inc->opts, cond, cond_len)) &&
!strcmp(key, "path"))
ret = handle_path_include(value, inc);
@@ -1511,12 +1521,20 @@ int git_config_system(void)
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
}
-static int do_git_config_sequence(config_fn_t fn, void *data)
+static int do_git_config_sequence(const struct config_options *opts,
+ config_fn_t fn, void *data)
{
int ret = 0;
char *xdg_config = xdg_config_home("config");
char *user_config = expand_user_path("~/.gitconfig", 0);
- char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
+ char *repo_config;
+
+ if (opts->git_dir)
+ repo_config = mkpathdup("%s/config", opts->git_dir);
+ else if (have_git_dir())
+ repo_config = git_pathdup("config");
+ else
+ repo_config = NULL;
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
@@ -1547,13 +1565,14 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
int git_config_with_options(config_fn_t fn, void *data,
struct git_config_source *config_source,
- int respect_includes)
+ const struct config_options *opts)
{
struct config_include_data inc = CONFIG_INCLUDE_INIT;
- if (respect_includes) {
+ if (opts->respect_includes) {
inc.fn = fn;
inc.data = data;
+ inc.opts = opts;
fn = git_config_include;
data = &inc;
}
@@ -1569,12 +1588,15 @@ int git_config_with_options(config_fn_t fn, void *data,
else if (config_source && config_source->blob)
return git_config_from_blob_ref(fn, config_source->blob, data);
- return do_git_config_sequence(fn, data);
+ return do_git_config_sequence(opts, fn, data);
}
static void git_config_raw(config_fn_t fn, void *data)
{
- if (git_config_with_options(fn, data, NULL, 1) < 0)
+ struct config_options opts = {0};
+
+ opts.respect_includes = 1;
+ if (git_config_with_options(fn, data, NULL, &opts) < 0)
/*
* git_config_with_options() normally returns only
* zero, as most errors are fatal, and
@@ -1614,10 +1636,13 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
void read_early_config(config_fn_t cb, void *data)
{
+ struct config_options opts = {0};
struct strbuf buf = STRBUF_INIT;
- git_config_with_options(cb, data, NULL, 1);
+ opts.respect_includes = 1;
+ if (have_git_dir())
+ opts.git_dir = get_git_dir();
/*
* When setup_git_directory() was not yet asked to discover the
* GIT_DIR, we ask discover_git_directory() to figure out whether there
@@ -1626,14 +1651,11 @@ void read_early_config(config_fn_t cb, void *data)
* notably, the current working directory is still the same after the
* call).
*/
- if (!have_git_dir() && discover_git_directory(&buf)) {
- struct git_config_source repo_config;
+ else if (discover_git_directory(&buf))
+ opts.git_dir = buf.buf;
+
+ git_config_with_options(cb, data, NULL, &opts);
- memset(&repo_config, 0, sizeof(repo_config));
- strbuf_addstr(&buf, "/config");
- repo_config.file = buf.buf;
- git_config_with_options(cb, data, &repo_config, 1);
- }
strbuf_release(&buf);
}