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 2026-06-30 13:47:49 +02:00 committed by Junio C Hamano
parent f04fea749c
commit 4ce6d5814e
1 changed files with 25 additions and 18 deletions

43
setup.c
View File

@ -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;
}