config: handle conditional include when $GIT_DIR is not set up

If setup_git_directory() and friends have not been called, get_git_dir() (because of includeIf.gitdir:XXX) would lead to die("BUG: setup_git_env called without repository"); There are two cases when a config file could be read before $GIT_DIR is located. The first one is check_repository_format(), where we read just the one file $GIT_DIR/config to check if we could understand this repository. This case should be safe. We do not parse include directives, which can only be triggered from git_config_with_options, but setup code uses a lower-level function. The concerned variables should never be hidden away behind includes anyway. The second one is triggered in check_pager_config() when we're about to run an external git command. We might be able to find $GIT_DIR in this case, which is exactly what read_early_config() does (and also is what check_pager_config() uses). Conditional includes and get_git_dir() could be triggered by the first git_config_with_options() call there, before discover_git_directory() is used as a fallback $GIT_DIR detection. Detect this special "early reading" case, pass down the $GIT_DIR, either from previous setup or detected by discover_git_directory(), and make conditional include use it. Noticed-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 17, 2017 at 17:10 UTC 2185fde56328942d5be9603cc199ee7c6d004085
3 files changed +38 -8
cache.h
+1
@@ -1887,6 +1887,7 @@ enum config_origin_type {
1887
1888 struct config_options {
1889 unsigned int respect_includes : 1;
1890 + const char *git_dir;
1891 };
1892
1893 typedef int (*config_fn_t)(const char *, const char *, void *);
config.c
+26 -8
@@ -207,13 +207,22 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
207 return prefix;
208 }
209
210 -static int include_by_gitdir(const char *cond, size_t cond_len, int icase)
210 +static int include_by_gitdir(const struct config_options *opts,
211 + const char *cond, size_t cond_len, int icase)
212 {
213 struct strbuf text = STRBUF_INIT;
214 struct strbuf pattern = STRBUF_INIT;
215 int ret = 0, prefix;
216 + const char *git_dir;
217
216 - strbuf_add_absolute_path(&text, get_git_dir());
218 + if (opts->git_dir)
219 + git_dir = opts->git_dir;
220 + else if (have_git_dir())
221 + git_dir = get_git_dir();
222 + else
223 + goto done;
224 +
225 + strbuf_add_absolute_path(&text, git_dir);
226 strbuf_add(&pattern, cond, cond_len);
227 prefix = prepare_include_condition_pattern(&pattern);
228
@@ -242,13 +251,14 @@ done:
251 return ret;
252 }
253
245 -static int include_condition_is_true(const char *cond, size_t cond_len)
254 +static int include_condition_is_true(const struct config_options *opts,
255 + const char *cond, size_t cond_len)
256 {
257
258 if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
249 - return include_by_gitdir(cond, cond_len, 0);
259 + return include_by_gitdir(opts, cond, cond_len, 0);
260 else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
251 - return include_by_gitdir(cond, cond_len, 1);
261 + return include_by_gitdir(opts, cond, cond_len, 1);
262
263 /* unknown conditionals are always false */
264 return 0;
@@ -273,7 +283,7 @@ int git_config_include(const char *var, const char *value, void *data)
283 ret = handle_path_include(value, inc);
284
285 if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
276 - (cond && include_condition_is_true(cond, cond_len)) &&
286 + (cond && include_condition_is_true(inc->opts, cond, cond_len)) &&
287 !strcmp(key, "path"))
288 ret = handle_path_include(value, inc);
289
@@ -1603,10 +1613,12 @@ void read_early_config(config_fn_t cb, void *data)
1613 {
1614 struct config_options opts = {0};
1615 struct strbuf buf = STRBUF_INIT;
1616 + char *to_free = NULL;
1617
1618 opts.respect_includes = 1;
1608 - git_config_with_options(cb, data, NULL, &opts);
1619
1620 + if (have_git_dir())
1621 + opts.git_dir = get_git_dir();
1622 /*
1623 * When setup_git_directory() was not yet asked to discover the
1624 * GIT_DIR, we ask discover_git_directory() to figure out whether there
@@ -1615,7 +1627,12 @@ void read_early_config(config_fn_t cb, void *data)
1627 * notably, the current working directory is still the same after the
1628 * call).
1629 */
1618 - if (!have_git_dir() && discover_git_directory(&buf)) {
1630 + else if (discover_git_directory(&buf))
1631 + opts.git_dir = to_free = xstrdup(buf.buf);
1632 +
1633 + git_config_with_options(cb, data, NULL, &opts);
1634 +
1635 + if (!have_git_dir() && opts.git_dir) {
1636 struct git_config_source repo_config;
1637
1638 memset(&repo_config, 0, sizeof(repo_config));
@@ -1624,6 +1641,7 @@ void read_early_config(config_fn_t cb, void *data)
1641 git_config_with_options(cb, data, &repo_config, &opts);
1642 }
1643 strbuf_release(&buf);
1644 + free(to_free);
1645 }
1646
1647 static void git_config_check_init(void);
t/t1305-config-include.sh
+11
@@ -208,6 +208,17 @@ test_expect_success 'conditional include, both unanchored, icase' '
208 )
209 '
210
211 +test_expect_success 'conditional include, early config reading' '
212 + (
213 + cd foo &&
214 + echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
215 + echo "[test]six=6" >.git/bar6 &&
216 + echo 6 >expect &&
217 + test-config read_early_config test.six >actual &&
218 + test_cmp expect actual
219 + )
220 +'
221 +
222 test_expect_success 'include cycles are detected' '
223 cat >.gitconfig <<-\EOF &&
224 [test]value = gitconfig