setup: make repository discovery self-contained
In the preceding commits we have introduced a separate repository discovery phase and refactored the logic so that we have two clear phases: 1. Repository discovery, which doesn't modify the repository itself at all. 2. Repository configuration, which takes the information we have discovered to set up the repository. Extract the first phase into a new function `repo_discover()` to further stress these two different phases. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jul 7, 2026 at 09:21 UTC
2513a4d6f37144841cfd00183fce3dd8f5ba967d
1 file changed
+25
-18
setup.c
+25
-18
@@ -1922,20 +1922,10 @@ void set_git_work_tree(struct repository *repo, const char *new_work_tree)
1922
repo_set_worktree(repo, new_work_tree);
1923
}
1924
1925
-const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
1925
+static void repo_discover(struct repo_discovery *discovery, int *nongit_ok)
1926
{
1927
struct strbuf cwd = STRBUF_INIT;
1928
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
1929
- struct repo_discovery discovery = REPO_DISCOVERY_INIT;
1930
-
1931
- /*
1932
- * We may have read an incomplete configuration before
1933
- * setting-up the git directory. If so, clear the cache so
1934
- * that the next queries to the configuration reload complete
1935
- * configuration (including the per-repo config file that we
1936
- * ignored previously).
1937
- */
1938
- repo_config_clear(repo);
1929
1930
/*
1931
* Let's assume that we are in a git repository.
@@ -1951,19 +1941,19 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
1941
1942
switch (repo_discovery_find_dir(&dir, &gitdir, &report, 1)) {
1943
case GIT_DIR_EXPLICIT:
1954
- repo_discover_explicit_gitdir(&discovery, gitdir.buf, &cwd,
1944
+ repo_discover_explicit_gitdir(discovery, gitdir.buf, &cwd,
1945
nongit_ok);
1946
break;
1947
case GIT_DIR_DISCOVERED:
1948
if (dir.len < cwd.len && chdir(dir.buf))
1949
die(_("cannot change to '%s'"), dir.buf);
1960
- repo_discover_implicit_gitdir(&discovery, gitdir.buf, &cwd, dir.len,
1950
+ repo_discover_implicit_gitdir(discovery, gitdir.buf, &cwd, dir.len,
1951
nongit_ok);
1952
break;
1953
case GIT_DIR_BARE:
1954
if (dir.len < cwd.len && chdir(dir.buf))
1955
die(_("cannot change to '%s'"), dir.buf);
1966
- repo_discover_bare_gitdir(&discovery, &cwd, dir.len, nongit_ok);
1956
+ repo_discover_bare_gitdir(discovery, &cwd, dir.len, nongit_ok);
1957
break;
1958
case GIT_DIR_HIT_CEILING:
1959
if (!nongit_ok)
@@ -2013,6 +2003,27 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2003
BUG("unhandled repo_discovery_find_dir() result");
2004
}
2005
2006
+ strbuf_release(&dir);
2007
+ strbuf_release(&cwd);
2008
+ strbuf_release(&gitdir);
2009
+ strbuf_release(&report);
2010
+}
2011
+
2012
+const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2013
+{
2014
+ struct repo_discovery discovery = REPO_DISCOVERY_INIT;
2015
+
2016
+ /*
2017
+ * We may have read an incomplete configuration before
2018
+ * setting-up the git directory. If so, clear the cache so
2019
+ * that the next queries to the configuration reload complete
2020
+ * configuration (including the per-repo config file that we
2021
+ * ignored previously).
2022
+ */
2023
+ repo_config_clear(repo);
2024
+
2025
+ repo_discover(&discovery, nongit_ok);
2026
+
2027
/*
2028
* At this point, nongit_ok is stable. If it is non-NULL and points
2029
* to a non-zero value, then this means that we haven't found a
@@ -2104,10 +2115,6 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2115
setup_original_cwd(repo);
2116
2117
repo_discovery_release(&discovery);
2107
- strbuf_release(&dir);
2108
- strbuf_release(&cwd);
2109
- strbuf_release(&gitdir);
2110
- strbuf_release(&report);
2118
return repo->prefix;
2119
}
2120