Skip to content

Commit a0393a2

Browse files
committed
Merge branch 'js/early-config'
The start-up sequence of "git" needs to figure out some configured settings before it finds and set itself up in the location of the repository and was quite messy due to its "chicken-and-egg" nature. The code has been restructured. * js/early-config: setup.c: mention unresolved problems t1309: document cases where we would want early config not to die() setup_git_directory_gently_1(): avoid die()ing t1309: test read_early_config() read_early_config(): really discover .git/ read_early_config(): avoid .git/config hack when unneeded setup: make read_early_config() reusable setup: introduce the discover_git_directory() function setup_git_directory_1(): avoid changing global state setup: prepare setup_discovered_git_dir() for the root directory setup_git_directory(): use is_dir_sep() helper t7006: replace dubious test
2 parents 81944e9 + 5c4003c commit a0393a2

File tree

7 files changed

+313
-111
lines changed

7 files changed

+313
-111
lines changed

cache.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ extern void set_git_work_tree(const char *tree);
518518
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
519519

520520
extern void setup_work_tree(void);
521+
/*
522+
* Find GIT_DIR of the repository that contains the current working directory,
523+
* without changing the working directory or other global state. The result is
524+
* appended to gitdir. The return value is either NULL if no repository was
525+
* found, or pointing to the path inside gitdir's buffer.
526+
*/
527+
extern const char *discover_git_directory(struct strbuf *gitdir);
521528
extern const char *setup_git_directory_gently(int *);
522529
extern const char *setup_git_directory(void);
523530
extern char *prefix_path(const char *prefix, int len, const char *path);
@@ -1842,6 +1849,7 @@ extern int git_config_from_blob_sha1(config_fn_t fn, const char *name,
18421849
const unsigned char *sha1, void *data);
18431850
extern void git_config_push_parameter(const char *text);
18441851
extern int git_config_from_parameters(config_fn_t fn, void *data);
1852+
extern void read_early_config(config_fn_t cb, void *data);
18451853
extern void git_config(config_fn_t fn, void *);
18461854
extern int git_config_with_options(config_fn_t fn, void *,
18471855
struct git_config_source *config_source,

config.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,31 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
15031503
}
15041504
}
15051505

1506+
void read_early_config(config_fn_t cb, void *data)
1507+
{
1508+
struct strbuf buf = STRBUF_INIT;
1509+
1510+
git_config_with_options(cb, data, NULL, 1);
1511+
1512+
/*
1513+
* When setup_git_directory() was not yet asked to discover the
1514+
* GIT_DIR, we ask discover_git_directory() to figure out whether there
1515+
* is any repository config we should use (but unlike
1516+
* setup_git_directory_gently(), no global state is changed, most
1517+
* notably, the current working directory is still the same after the
1518+
* call).
1519+
*/
1520+
if (!have_git_dir() && discover_git_directory(&buf)) {
1521+
struct git_config_source repo_config;
1522+
1523+
memset(&repo_config, 0, sizeof(repo_config));
1524+
strbuf_addstr(&buf, "/config");
1525+
repo_config.file = buf.buf;
1526+
git_config_with_options(cb, data, &repo_config, 1);
1527+
}
1528+
strbuf_release(&buf);
1529+
}
1530+
15061531
static void git_config_check_init(void);
15071532

15081533
void git_config(config_fn_t fn, void *data)

pager.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,6 @@ static int core_pager_config(const char *var, const char *value, void *data)
4343
return 0;
4444
}
4545

46-
static void read_early_config(config_fn_t cb, void *data)
47-
{
48-
git_config_with_options(cb, data, NULL, 1);
49-
50-
/*
51-
* Note that this is a really dirty hack that does the wrong thing in
52-
* many cases. The crux of the problem is that we cannot run
53-
* setup_git_directory() early on in git's setup, so we have no idea if
54-
* we are in a repository or not, and therefore are not sure whether
55-
* and how to read repository-local config.
56-
*
57-
* So if we _aren't_ in a repository (or we are but we would reject its
58-
* core.repositoryformatversion), we'll read whatever is in .git/config
59-
* blindly. Similarly, if we _are_ in a repository, but not at the
60-
* root, we'll fail to find .git/config (because it's really
61-
* ../.git/config, etc). See t7006 for a complete set of failures.
62-
*
63-
* However, we have historically provided this hack because it does
64-
* work some of the time (namely when you are at the top-level of a
65-
* valid repository), and would rarely make things worse (i.e., you do
66-
* not generally have a .git/config file sitting around).
67-
*/
68-
if (!startup_info->have_repository) {
69-
struct git_config_source repo_config;
70-
71-
memset(&repo_config, 0, sizeof(repo_config));
72-
repo_config.file = ".git/config";
73-
git_config_with_options(cb, data, &repo_config, 1);
74-
}
75-
}
76-
7746
const char *git_pager(int stdout_is_tty)
7847
{
7948
const char *pager;

0 commit comments

Comments
 (0)