git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 10/13] setup: make repository discovery self-contained

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 30, 2026, 11:47 UTC
Message-ID
<20260630-pks-setup-split-discovery-and-setup-v1-10-13864eb5a032@pks.im>
In-Reply-To
<20260630-pks-setup-split-discovery-and-setup-v1-0-13864eb5a032@pks.im>

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>
---
 setup.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)
diff --git a/setup.c b/setup.c
index fc73276149..7715f3ea85 100644
--- a/setup.c
+++ b/setup.c
@@ -1922,20 +1922,10 @@ void set_git_work_tree(struct repository *repo, const char *new_work_tree)
 	repo_set_worktree(repo, new_work_tree);
 }
 
-const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
+static void repo_discover(struct repo_discovery *discovery, int *nongit_ok)
 {
 	struct strbuf cwd = STRBUF_INIT;
 	struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
-	struct repo_discovery discovery = REPO_DISCOVERY_INIT;
-
-	/*
-	 * We may have read an incomplete configuration before
-	 * setting-up the git directory. If so, clear the cache so
-	 * that the next queries to the configuration reload complete
-	 * configuration (including the per-repo config file that we
-	 * ignored previously).
-	 */
-	repo_config_clear(repo);
 
 	/*
 	 * 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)
 
 	switch (repo_discovery_find_dir(&dir, &gitdir, &report, 1)) {
 	case GIT_DIR_EXPLICIT:
-		repo_discover_explicit_gitdir(&discovery, gitdir.buf, &cwd,
+		repo_discover_explicit_gitdir(discovery, gitdir.buf, &cwd,
 					      nongit_ok);
 		break;
 	case GIT_DIR_DISCOVERED:
 		if (dir.len < cwd.len && chdir(dir.buf))
 			die(_("cannot change to '%s'"), dir.buf);
-		repo_discover_implicit_gitdir(&discovery, gitdir.buf, &cwd, dir.len,
+		repo_discover_implicit_gitdir(discovery, gitdir.buf, &cwd, dir.len,
 					      nongit_ok);
 		break;
 	case GIT_DIR_BARE:
 		if (dir.len < cwd.len && chdir(dir.buf))
 			die(_("cannot change to '%s'"), dir.buf);
-		repo_discover_bare_gitdir(&discovery, &cwd, dir.len, nongit_ok);
+		repo_discover_bare_gitdir(discovery, &cwd, dir.len, nongit_ok);
 		break;
 	case GIT_DIR_HIT_CEILING:
 		if (!nongit_ok)
@@ -2013,6 +2003,27 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
 		BUG("unhandled repo_discovery_find_dir() result");
 	}
 
+	strbuf_release(&dir);
+	strbuf_release(&cwd);
+	strbuf_release(&gitdir);
+	strbuf_release(&report);
+}
+
+const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
+{
+	struct repo_discovery discovery = REPO_DISCOVERY_INIT;
+
+	/*
+	 * We may have read an incomplete configuration before
+	 * setting-up the git directory. If so, clear the cache so
+	 * that the next queries to the configuration reload complete
+	 * configuration (including the per-repo config file that we
+	 * ignored previously).
+	 */
+	repo_config_clear(repo);
+
+	repo_discover(&discovery, nongit_ok);
+
 	/*
 	 * At this point, nongit_ok is stable. If it is non-NULL and points
 	 * 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)
 	setup_original_cwd(repo);
 
 	repo_discovery_release(&discovery);
-	strbuf_release(&dir);
-	strbuf_release(&cwd);
-	strbuf_release(&gitdir);
-	strbuf_release(&report);
 	return repo->prefix;
 }
 
-- 
2.55.0.795.g602f6c329a.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 21 of 24 in “setup: split up repository discovery and setup”
  1. 00/13 setup: split up repository discovery and setupPatrick Steinhardt, Jun 30, 2026
  2. 01/13 setup: rename `check_repository_format_gently()`Patrick Steinhardt, Jun 30, 2026
  3. Justin ToblerJul 6, 2026
  4. 02/13 setup: mark bogus worktree in `apply_repository_format()`Patrick Steinhardt, Jun 30, 2026
  5. Junio C HamanoJun 30, 2026
  6. Patrick SteinhardtJul 1, 2026
  7. Justin ToblerJul 6, 2026
  8. Patrick SteinhardtJul 7, 2026
  9. 03/13 setup: unify setup of shallow filePatrick Steinhardt, Jun 30, 2026
  10. Justin ToblerJul 6, 2026
  11. Patrick SteinhardtJul 7, 2026
  12. 04/13 setup: split up concerns of `setup_git_env_internal()`Patrick Steinhardt, Jun 30, 2026
  13. 05/13 setup: introduce explicit repository discoveryPatrick Steinhardt, Jun 30, 2026
  14. Justin ToblerJul 6, 2026
  15. Patrick SteinhardtJul 7, 2026
  16. 06/13 setup: embed repository format in discoveryPatrick Steinhardt, Jun 30, 2026
  17. 07/13 setup: move prefix into repositoryPatrick Steinhardt, Jun 30, 2026
  18. Justin ToblerJul 6, 2026
  19. 08/13 setup: drop static `cwd` variablePatrick Steinhardt, Jun 30, 2026
  20. 09/13 setup: propagate prefix via repository discoveryPatrick Steinhardt, Jun 30, 2026
  21. 10/13 setup: make repository discovery self-containedPatrick Steinhardt, Jun 30, 2026
  22. 11/13 setup: drop redundant configuration of `startup_info->have_repository`Patrick Steinhardt, Jun 30, 2026
  23. 12/13 setup: pass worktree to `init_db()`Patrick Steinhardt, Jun 30, 2026
  24. 13/13 setup: mark `set_git_work_tree()` as file-localPatrick Steinhardt, Jun 30, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.