setup: stop using `the_repository` in `setup_git_directory_gently()`

Stop using `the_repository` in `setup_git_directory_gently()` and instead accept the repository as a parameter. The injection of `the_repository` is thus bumped one level higher, where callers now pass it in explicitly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 19, 2026 at 11:52 UTC a80a8e3ea6a070185840219778df24db832899f6
13 files changed +36 -33
builtin/check-ref-format.c
+4 -1
@@ -1,6 +1,9 @@
1 /*
2 * GIT - The information manager from hell
3 */
4 +
5 +#define USE_THE_REPOSITORY_VARIABLE
6 +
7 #include "builtin.h"
8 #include "refs.h"
9 #include "setup.h"
@@ -41,7 +44,7 @@ static int check_ref_format_branch(const char *arg)
44 const char *name;
45 int nongit;
46
44 - setup_git_directory_gently(&nongit);
47 + setup_git_directory_gently(the_repository, &nongit);
48 if (check_branch_ref(&sb, arg) ||
49 !skip_prefix(sb.buf, "refs/heads/", &name))
50 die("'%s' is not a valid branch name", arg);
builtin/diff.c
+1 -1
@@ -455,7 +455,7 @@ int cmd_diff(int argc,
455 break;
456 }
457
458 - prefix = setup_git_directory_gently(&nongit);
458 + prefix = setup_git_directory_gently(the_repository, &nongit);
459
460 if (!nongit) {
461 prepare_repo_settings(the_repository);
builtin/hash-object.c
+1 -1
@@ -102,7 +102,7 @@ int cmd_hash_object(int argc,
102 if (flags & INDEX_WRITE_OBJECT)
103 prefix = setup_git_directory();
104 else
105 - prefix = setup_git_directory_gently(&nongit);
105 + prefix = setup_git_directory_gently(the_repository, &nongit);
106
107 if (nongit && !the_hash_algo)
108 repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
builtin/help.c
+1 -1
@@ -740,7 +740,7 @@ int cmd_help(int argc,
740 return 0;
741 }
742
743 - setup_git_directory_gently(&nongit);
743 + setup_git_directory_gently(the_repository, &nongit);
744 repo_config(the_repository, git_help_config, NULL);
745
746 if (parsed_help_format != HELP_FORMAT_NONE)
builtin/stripspace.c
+1 -1
@@ -54,7 +54,7 @@ int cmd_stripspace(int argc,
54 usage_with_options(stripspace_usage, options);
55
56 if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
57 - setup_git_directory_gently(&nongit);
57 + setup_git_directory_gently(the_repository, &nongit);
58 repo_config(the_repository, git_default_config, NULL);
59 }
60
git.c
+3 -3
@@ -84,7 +84,7 @@ static int list_cmds(const char *spec)
84 * Set up the repository so we can pick up any repo-level config (like
85 * completion.commands).
86 */
87 - setup_git_directory_gently(&nongit);
87 + setup_git_directory_gently(the_repository, &nongit);
88
89 while (*spec) {
90 const char *sep = strchrnul(spec, ',');
@@ -386,7 +386,7 @@ static int handle_alias(struct strvec *args, struct string_list *expanded_aliase
386 int nongit_ok;
387
388 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
389 - setup_git_directory_gently(&nongit_ok);
389 + setup_git_directory_gently(the_repository, &nongit_ok);
390
391 commit_pager_choice();
392
@@ -480,7 +480,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
480 prefix = setup_git_directory();
481 no_repo = 0;
482 } else if (run_setup & RUN_SETUP_GENTLY) {
483 - prefix = setup_git_directory_gently(&no_repo);
483 + prefix = setup_git_directory_gently(the_repository, &no_repo);
484 } else {
485 prefix = NULL;
486 }
http-fetch.c
+1 -1
@@ -109,7 +109,7 @@ int cmd_main(int argc, const char **argv)
109 struct strvec index_pack_args = STRVEC_INIT;
110 int ret;
111
112 - setup_git_directory_gently(&nongit);
112 + setup_git_directory_gently(the_repository, &nongit);
113
114 while (arg < argc && argv[arg][0] == '-') {
115 const char *p;
imap-send.c
+1 -1
@@ -1799,7 +1799,7 @@ int cmd_main(int argc, const char **argv)
1799 int nongit_ok;
1800 int ret;
1801
1802 - setup_git_directory_gently(&nongit_ok);
1802 + setup_git_directory_gently(the_repository, &nongit_ok);
1803 repo_config(the_repository, git_imap_config, &server);
1804
1805 argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
remote-curl.c
+2 -2
@@ -1557,7 +1557,7 @@ int cmd_main(int argc, const char **argv)
1557 int nongit;
1558 int ret = 1;
1559
1560 - setup_git_directory_gently(&nongit);
1560 + setup_git_directory_gently(the_repository, &nongit);
1561 if (argc < 2) {
1562 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1563 goto cleanup;
@@ -1605,7 +1605,7 @@ int cmd_main(int argc, const char **argv)
1605 break;
1606 if (starts_with(buf.buf, "fetch ")) {
1607 if (nongit) {
1608 - setup_git_directory_gently(&nongit);
1608 + setup_git_directory_gently(the_repository, &nongit);
1609 if (nongit)
1610 die(_("remote-curl: fetch attempted without a local repo"));
1611 }
setup.c
+18 -18
@@ -1862,7 +1862,7 @@ void set_git_work_tree(struct repository *repo, const char *new_work_tree)
1862 repo_set_worktree(repo, new_work_tree);
1863 }
1864
1865 -const char *setup_git_directory_gently(int *nongit_ok)
1865 +const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
1866 {
1867 static struct strbuf cwd = STRBUF_INIT;
1868 struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
@@ -1877,7 +1877,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
1877 * configuration (including the per-repo config file that we
1878 * ignored previously).
1879 */
1880 - repo_config_clear(the_repository);
1880 + repo_config_clear(repo);
1881
1882 /*
1883 * Let's assume that we are in a git repository.
@@ -1893,18 +1893,18 @@ const char *setup_git_directory_gently(int *nongit_ok)
1893
1894 switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) {
1895 case GIT_DIR_EXPLICIT:
1896 - prefix = setup_explicit_git_dir(the_repository, gitdir.buf, &cwd, &repo_fmt, nongit_ok);
1896 + prefix = setup_explicit_git_dir(repo, gitdir.buf, &cwd, &repo_fmt, nongit_ok);
1897 break;
1898 case GIT_DIR_DISCOVERED:
1899 if (dir.len < cwd.len && chdir(dir.buf))
1900 die(_("cannot change to '%s'"), dir.buf);
1901 - prefix = setup_discovered_git_dir(the_repository, gitdir.buf, &cwd, dir.len,
1901 + prefix = setup_discovered_git_dir(repo, gitdir.buf, &cwd, dir.len,
1902 &repo_fmt, nongit_ok);
1903 break;
1904 case GIT_DIR_BARE:
1905 if (dir.len < cwd.len && chdir(dir.buf))
1906 die(_("cannot change to '%s'"), dir.buf);
1907 - prefix = setup_bare_git_dir(the_repository, &cwd, dir.len, &repo_fmt, nongit_ok);
1907 + prefix = setup_bare_git_dir(repo, &cwd, dir.len, &repo_fmt, nongit_ok);
1908 break;
1909 case GIT_DIR_HIT_CEILING:
1910 if (!nongit_ok)
@@ -1984,30 +1984,30 @@ const char *setup_git_directory_gently(int *nongit_ok)
1984 startup_info->have_repository ||
1985 /* GIT_DIR_EXPLICIT */
1986 getenv(GIT_DIR_ENVIRONMENT)) {
1987 - if (!the_repository->gitdir) {
1987 + if (!repo->gitdir) {
1988 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
1989 if (!gitdir)
1990 gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
1991 - setup_git_env(the_repository, gitdir);
1991 + setup_git_env(repo, gitdir);
1992 }
1993 if (startup_info->have_repository) {
1994 - repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
1995 - repo_set_compat_hash_algo(the_repository,
1994 + repo_set_hash_algo(repo, repo_fmt.hash_algo);
1995 + repo_set_compat_hash_algo(repo,
1996 repo_fmt.compat_hash_algo);
1997 - repo_set_ref_storage_format(the_repository,
1997 + repo_set_ref_storage_format(repo,
1998 repo_fmt.ref_storage_format,
1999 repo_fmt.ref_storage_payload);
2000 - the_repository->repository_format_worktree_config =
2000 + repo->repository_format_worktree_config =
2001 repo_fmt.worktree_config;
2002 - the_repository->repository_format_relative_worktrees =
2002 + repo->repository_format_relative_worktrees =
2003 repo_fmt.relative_worktrees;
2004 - the_repository->repository_format_submodule_path_cfg =
2004 + repo->repository_format_submodule_path_cfg =
2005 repo_fmt.submodule_path_cfg;
2006 /* take ownership of repo_fmt.partial_clone */
2007 - the_repository->repository_format_partial_clone =
2007 + repo->repository_format_partial_clone =
2008 repo_fmt.partial_clone;
2009 repo_fmt.partial_clone = NULL;
2010 - the_repository->repository_format_precious_objects =
2010 + repo->repository_format_precious_objects =
2011 repo_fmt.precious_objects;
2012 }
2013 }
@@ -2040,13 +2040,13 @@ const char *setup_git_directory_gently(int *nongit_ok)
2040 format = ref_storage_format_by_name(backend);
2041 if (format == REF_STORAGE_FORMAT_UNKNOWN)
2042 die(_("unknown ref storage format: '%s'"), backend);
2043 - repo_set_ref_storage_format(the_repository, format, payload);
2043 + repo_set_ref_storage_format(repo, format, payload);
2044
2045 free(backend);
2046 free(payload);
2047 }
2048
2049 - setup_original_cwd(the_repository);
2049 + setup_original_cwd(repo);
2050
2051 strbuf_release(&dir);
2052 strbuf_release(&gitdir);
@@ -2138,7 +2138,7 @@ void check_repository_format(struct repository_format *fmt)
2138 */
2139 const char *setup_git_directory(void)
2140 {
2141 - return setup_git_directory_gently(NULL);
2141 + return setup_git_directory_gently(the_repository, NULL);
2142 }
2143
2144 const char *resolve_gitdir_gently(const char *suspect, int *return_error_code)
setup.h
+1 -1
@@ -136,7 +136,7 @@ enum {
136 */
137 const char *enter_repo(struct repository *repo, const char *path, unsigned flags);
138
139 -const char *setup_git_directory_gently(int *);
139 +const char *setup_git_directory_gently(struct repository *repo, int *);
140 const char *setup_git_directory(void);
141 char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path);
142 char *prefix_path_gently(struct repository *repo, const char *prefix, int len, int *remaining, const char *path);
t/helper/test-path-utils.c
+1 -1
@@ -377,7 +377,7 @@ int cmd__path_utils(int argc, const char **argv)
377 const char *prefix = argv[2];
378 int prefix_len = strlen(prefix);
379 int nongit_ok;
380 - setup_git_directory_gently(&nongit_ok);
380 + setup_git_directory_gently(the_repository, &nongit_ok);
381 while (argc > 3) {
382 char *pfx = prefix_path(the_repository, prefix, prefix_len, argv[3]);
383
t/helper/test-subprocess.c
+1 -1
@@ -9,7 +9,7 @@ int cmd__subprocess(int argc, const char **argv)
9 struct child_process cp = CHILD_PROCESS_INIT;
10 int nogit = 0;
11
12 - setup_git_directory_gently(&nogit);
12 + setup_git_directory_gently(the_repository, &nogit);
13 if (nogit)
14 die("No git repo found");
15 if (argc > 1 && !strcmp(argv[1], "--setup-work-tree")) {