config: only read .git/config from configured repos

When git_config() runs, it looks in the system, user-wide, and repo-level config files. It gets the latter by calling git_pathdup(), which in turn calls get_git_dir(). If we haven't set up the git repository yet, this may simply return ".git", and we will look at ".git/config". This seems like it would be helpful (presumably we haven't set up the repository yet, so it tries to find it), but it turns out to be a bad idea for a few reasons: - it's not sufficient, and therefore hides bugs in a confusing way. Config will be respected if commands are run from the top-level of the working tree, but not from a subdirectory. - it's not always true that we haven't set up the repository _yet_; we may not want to do it at all. For instance, if you run "git init /some/path" from inside another repository, it should not load config from the existing repository. - there might be a path ".git/config", but it is not the actual repository we would find via setup_git_directory(). This may happen, e.g., if you are storing a git repository inside another git repository, but have munged one of the files in such a way that the inner repository is not valid (e.g., by removing HEAD). We have at least two bugs of the second type in git-init, introduced by ae5f677 (lazily load core.sharedrepository, 2016-03-11). It causes init to use git_configset(), which loads all of the config, including values from the current repo (if any). This shows up in two ways: 1. If we happen to be in an existing repository directory, we'll read and respect core.sharedrepository from it, even though it should have no bearing on the new repository. A new test in t1301 covers this. 2. Similarly, if we're in an existing repo that sets core.logallrefupdates, that will cause init to fail to set it in a newly created repository (because it thinks that the user's templates already did so). A new test in t0001 covers this. We also need to adjust an existing test in t1302, which gives another example of why this patch is an improvement. That test creates an embedded repository with a bogus core.repositoryformatversion of "99". It wants to make sure that we actually stop at the bogus repo rather than continuing upward to find the outer repo. So it checks that "git config core.repositoryformatversion" returns 99. But that only works because we blindly read ".git/config", even though we _know_ we're in a repository whose vintage we do not understand. After this patch, we avoid reading config from the unknown vintage repository at all, which is a safer choice. But we need to tweak the test, since core.repositoryformatversion will not return 99; it will claim that it could not find the variable at all. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 12, 2016 at 20:24 UTC b9605bc4f2e44042824571f70b9a3a74eeebabff
6 files changed +33 -4
cache.h
+6
@@ -453,6 +453,12 @@ static inline enum object_type object_type(unsigned int mode)
453 */
454 extern const char * const local_repo_env[];
455
456 +/*
457 + * Returns true iff we have a configured git repository (either via
458 + * setup_git_directory, or in the environment via $GIT_DIR).
459 + */
460 +int have_git_dir(void);
461 +
462 extern int is_bare_repository_cfg;
463 extern int is_bare_repository(void);
464 extern int is_inside_git_dir(void);
config.c
+1 -1
@@ -1194,7 +1194,7 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
1194 int ret = 0, found = 0;
1195 char *xdg_config = xdg_config_home("config");
1196 char *user_config = expand_user_path("~/.gitconfig");
1197 - char *repo_config = git_pathdup("config");
1197 + char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
1198
1199 if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1200 ret += git_config_from_file(fn, git_etc_gitconfig(),
environment.c
+7
@@ -194,6 +194,13 @@ int is_bare_repository(void)
194 return is_bare_repository_cfg && !get_git_work_tree();
195 }
196
197 +int have_git_dir(void)
198 +{
199 + return startup_info->have_repository
200 + || git_dir
201 + || getenv(GIT_DIR_ENVIRONMENT);
202 +}
203 +
204 const char *get_git_dir(void)
205 {
206 if (!git_dir)
t/t0001-init.sh
+9
@@ -384,4 +384,13 @@ test_expect_success MINGW 'bare git dir not hidden' '
384 ! is_hidden newdir
385 '
386
387 +test_expect_success 'remote init from does not use config from cwd' '
388 + rm -rf newdir &&
389 + test_config core.logallrefupdates true &&
390 + git init newdir &&
391 + echo true >expect &&
392 + git -C newdir config --bool core.logallrefupdates >actual &&
393 + test_cmp expect actual
394 +'
395 +
396 test_done
t/t1301-shared-repo.sh
+9
@@ -172,4 +172,13 @@ test_expect_success POSIXPERM 'forced modes' '
172 }" actual)"
173 '
174
175 +test_expect_success POSIXPERM 'remote init does not use config from cwd' '
176 + git config core.sharedrepository 0666 &&
177 + umask 0022 &&
178 + git init --bare child.git &&
179 + echo "-rw-r--r--" >expect &&
180 + modebits child.git/config >actual &&
181 + test_cmp expect actual
182 +'
183 +
184 test_done
t/t1302-repo-version.sh
+1 -3
@@ -32,9 +32,7 @@ test_expect_success 'gitdir selection on normal repos' '
32
33 test_expect_success 'gitdir selection on unsupported repo' '
34 # Make sure it would stop at test2, not trash
35 - echo 99 >expect &&
36 - git -C test2 config core.repositoryformatversion >actual &&
37 - test_cmp expect actual
35 + test_expect_code 1 git -C test2 config core.repositoryformatversion >actual
36 '
37
38 test_expect_success 'gitdir not required mode' '