Skip to content

Commit 1a27409

Browse files
dschogitster
authored andcommitted
read_early_config(): really discover .git/
Earlier, we punted and simply assumed that we are in the top-level directory of the project, and that there is no .git file but a .git/ directory so that we can read directly from .git/config. However, that is not necessarily true. We may be in a subdirectory. Or .git may be a gitfile. Or the environment variable GIT_DIR may be set. To remedy this situation, we just refactored the way setup_git_directory() discovers the .git/ directory, to make it reusable, and more importantly, to leave all global variables and the current working directory alone. Let's discover the .git/ directory correctly in read_early_config() by using that new function. This fixes 4 known breakages in t7006. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 267b453 commit 1a27409

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

config.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,34 +1414,27 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
14141414

14151415
void read_early_config(config_fn_t cb, void *data)
14161416
{
1417+
struct strbuf buf = STRBUF_INIT;
1418+
14171419
git_config_with_options(cb, data, NULL, 1);
14181420

14191421
/*
1420-
* Note that this is a really dirty hack that does the wrong thing in
1421-
* many cases. The crux of the problem is that we cannot run
1422-
* setup_git_directory() early on in git's setup, so we have no idea if
1423-
* we are in a repository or not, and therefore are not sure whether
1424-
* and how to read repository-local config.
1425-
*
1426-
* So if we _aren't_ in a repository (or we are but we would reject its
1427-
* core.repositoryformatversion), we'll read whatever is in .git/config
1428-
* blindly. Similarly, if we _are_ in a repository, but not at the
1429-
* root, we'll fail to find .git/config (because it's really
1430-
* ../.git/config, etc), unless setup_git_directory() was already called.
1431-
* See t7006 for a complete set of failures.
1432-
*
1433-
* However, we have historically provided this hack because it does
1434-
* work some of the time (namely when you are at the top-level of a
1435-
* valid repository), and would rarely make things worse (i.e., you do
1436-
* not generally have a .git/config file sitting around).
1422+
* When setup_git_directory() was not yet asked to discover the
1423+
* GIT_DIR, we ask discover_git_directory() to figure out whether there
1424+
* is any repository config we should use (but unlike
1425+
* setup_git_directory_gently(), no global state is changed, most
1426+
* notably, the current working directory is still the same after the
1427+
* call).
14371428
*/
1438-
if (!have_git_dir()) {
1429+
if (!have_git_dir() && discover_git_directory(&buf)) {
14391430
struct git_config_source repo_config;
14401431

14411432
memset(&repo_config, 0, sizeof(repo_config));
1442-
repo_config.file = ".git/config";
1433+
strbuf_addstr(&buf, "/config");
1434+
repo_config.file = buf.buf;
14431435
git_config_with_options(cb, data, &repo_config, 1);
14441436
}
1437+
strbuf_release(&buf);
14451438
}
14461439

14471440
static void git_config_check_init(void);

t/t7006-pager.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,27 +360,27 @@ test_pager_choices 'git aliasedlog'
360360
test_default_pager expect_success 'git -p aliasedlog'
361361
test_PAGER_overrides expect_success 'git -p aliasedlog'
362362
test_core_pager_overrides expect_success 'git -p aliasedlog'
363-
test_core_pager_subdir expect_failure 'git -p aliasedlog'
363+
test_core_pager_subdir expect_success 'git -p aliasedlog'
364364
test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
365365

366366
test_default_pager expect_success 'git -p true'
367367
test_PAGER_overrides expect_success 'git -p true'
368368
test_core_pager_overrides expect_success 'git -p true'
369-
test_core_pager_subdir expect_failure 'git -p true'
369+
test_core_pager_subdir expect_success 'git -p true'
370370
test_GIT_PAGER_overrides expect_success 'git -p true'
371371

372372
test_default_pager expect_success test_must_fail 'git -p request-pull'
373373
test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
374374
test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
375-
test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
375+
test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
376376
test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
377377

378378
test_default_pager expect_success test_must_fail 'git -p'
379379
test_PAGER_overrides expect_success test_must_fail 'git -p'
380380
test_local_config_ignored expect_failure test_must_fail 'git -p'
381381
test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
382382

383-
test_expect_failure TTY 'core.pager in repo config works and retains cwd' '
383+
test_expect_success TTY 'core.pager in repo config works and retains cwd' '
384384
sane_unset GIT_PAGER &&
385385
test_config core.pager "cat >cwd-retained" &&
386386
(

0 commit comments

Comments
 (0)