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

[PATCH v3 12/18] setup: stop using `the_repository` in `setup_git_directory_gently()`

From
Patrick Steinhardt <ps@pks.im>
Date
May 19, 2026, 09:52 UTC
Message-ID
<20260519-pks-setup-wo-the-repository-v3-12-a00d8ea8b07f@pks.im>
In-Reply-To
<20260519-pks-setup-wo-the-repository-v3-0-a00d8ea8b07f@pks.im>

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>
---
 builtin/check-ref-format.c |  5 ++++-
 builtin/diff.c             |  2 +-
 builtin/hash-object.c      |  2 +-
 builtin/help.c             |  2 +-
 builtin/stripspace.c       |  2 +-
 git.c                      |  6 +++---
 http-fetch.c               |  2 +-
 imap-send.c                |  2 +-
 remote-curl.c              |  4 ++--
 setup.c                    | 36 ++++++++++++++++++------------------
 setup.h                    |  2 +-
 t/helper/test-path-utils.c |  2 +-
 t/helper/test-subprocess.c |  2 +-
 13 files changed, 36 insertions(+), 33 deletions(-)
diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index 5d80afeec0..e42b0444ea 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -1,6 +1,9 @@
 /*
  * GIT - The information manager from hell
  */
+
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "builtin.h"
 #include "refs.h"
 #include "setup.h"
@@ -41,7 +44,7 @@ static int check_ref_format_branch(const char *arg)
 	const char *name;
 	int nongit;
 
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(the_repository, &nongit);
 	if (check_branch_ref(&sb, arg) ||
 	    !skip_prefix(sb.buf, "refs/heads/", &name))
 		die("'%s' is not a valid branch name", arg);
diff --git a/builtin/diff.c b/builtin/diff.c
index 1ede873ac1..4b46e394ce 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -455,7 +455,7 @@ int cmd_diff(int argc,
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
+	prefix = setup_git_directory_gently(the_repository, &nongit);
 
 	if (!nongit) {
 		prepare_repo_settings(the_repository);
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index 5d900a6b8c..d7905bedc2 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -102,7 +102,7 @@ int cmd_hash_object(int argc,
 	if (flags & INDEX_WRITE_OBJECT)
 		prefix = setup_git_directory();
 	else
-		prefix = setup_git_directory_gently(&nongit);
+		prefix = setup_git_directory_gently(the_repository, &nongit);
 
 	if (nongit && !the_hash_algo)
 		repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
diff --git a/builtin/help.c b/builtin/help.c
index c0aece4da3..a140339999 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -740,7 +740,7 @@ int cmd_help(int argc,
 		return 0;
 	}
 
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(the_repository, &nongit);
 	repo_config(the_repository, git_help_config, NULL);
 
 	if (parsed_help_format != HELP_FORMAT_NONE)
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 4a566cbc5d..18705f1a5b 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -54,7 +54,7 @@ int cmd_stripspace(int argc,
 		usage_with_options(stripspace_usage, options);
 
 	if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
-		setup_git_directory_gently(&nongit);
+		setup_git_directory_gently(the_repository, &nongit);
 		repo_config(the_repository, git_default_config, NULL);
 	}
 
diff --git a/git.c b/git.c
index eaede42c4e..2cc018fc5c 100644
--- a/git.c
+++ b/git.c
@@ -84,7 +84,7 @@ static int list_cmds(const char *spec)
 	* Set up the repository so we can pick up any repo-level config (like
 	* completion.commands).
 	*/
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(the_repository, &nongit);
 
 	while (*spec) {
 		const char *sep = strchrnul(spec, ',');
@@ -386,7 +386,7 @@ static int handle_alias(struct strvec *args, struct string_list *expanded_aliase
 			int nongit_ok;
 
 			/* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
-			setup_git_directory_gently(&nongit_ok);
+			setup_git_directory_gently(the_repository, &nongit_ok);
 
 			commit_pager_choice();
 
@@ -480,7 +480,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct
 		prefix = setup_git_directory();
 		no_repo = 0;
 	} else if (run_setup & RUN_SETUP_GENTLY) {
-		prefix = setup_git_directory_gently(&no_repo);
+		prefix = setup_git_directory_gently(the_repository, &no_repo);
 	} else {
 		prefix = NULL;
 	}
diff --git a/http-fetch.c b/http-fetch.c
index 1922e23fcd..f9b6ecb061 100644
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -109,7 +109,7 @@ int cmd_main(int argc, const char **argv)
 	struct strvec index_pack_args = STRVEC_INIT;
 	int ret;
 
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(the_repository, &nongit);
 
 	while (arg < argc && argv[arg][0] == '-') {
 		const char *p;
diff --git a/imap-send.c b/imap-send.c
index af02c6a689..cfd6a5120c 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1799,7 +1799,7 @@ int cmd_main(int argc, const char **argv)
 	int nongit_ok;
 	int ret;
 
-	setup_git_directory_gently(&nongit_ok);
+	setup_git_directory_gently(the_repository, &nongit_ok);
 	repo_config(the_repository, git_imap_config, &server);
 
 	argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
diff --git a/remote-curl.c b/remote-curl.c
index aba60d5712..a84fc860ec 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1557,7 +1557,7 @@ int cmd_main(int argc, const char **argv)
 	int nongit;
 	int ret = 1;
 
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(the_repository, &nongit);
 	if (argc < 2) {
 		error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
 		goto cleanup;
@@ -1605,7 +1605,7 @@ int cmd_main(int argc, const char **argv)
 			break;
 		if (starts_with(buf.buf, "fetch ")) {
 			if (nongit) {
-				setup_git_directory_gently(&nongit);
+				setup_git_directory_gently(the_repository, &nongit);
 				if (nongit)
 					die(_("remote-curl: fetch attempted without a local repo"));
 			}
diff --git a/setup.c b/setup.c
index 8965f8ccaf..c12c6cbda2 100644
--- a/setup.c
+++ b/setup.c
@@ -1862,7 +1862,7 @@ 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(int *nongit_ok)
+const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
 {
 	static struct strbuf cwd = STRBUF_INIT;
 	struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
@@ -1877,7 +1877,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	 * configuration (including the per-repo config file that we
 	 * ignored previously).
 	 */
-	repo_config_clear(the_repository);
+	repo_config_clear(repo);
 
 	/*
 	 * Let's assume that we are in a git repository.
@@ -1893,18 +1893,18 @@ const char *setup_git_directory_gently(int *nongit_ok)
 
 	switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) {
 	case GIT_DIR_EXPLICIT:
-		prefix = setup_explicit_git_dir(the_repository, gitdir.buf, &cwd, &repo_fmt, nongit_ok);
+		prefix = setup_explicit_git_dir(repo, gitdir.buf, &cwd, &repo_fmt, nongit_ok);
 		break;
 	case GIT_DIR_DISCOVERED:
 		if (dir.len < cwd.len && chdir(dir.buf))
 			die(_("cannot change to '%s'"), dir.buf);
-		prefix = setup_discovered_git_dir(the_repository, gitdir.buf, &cwd, dir.len,
+		prefix = setup_discovered_git_dir(repo, gitdir.buf, &cwd, dir.len,
 						  &repo_fmt, nongit_ok);
 		break;
 	case GIT_DIR_BARE:
 		if (dir.len < cwd.len && chdir(dir.buf))
 			die(_("cannot change to '%s'"), dir.buf);
-		prefix = setup_bare_git_dir(the_repository, &cwd, dir.len, &repo_fmt, nongit_ok);
+		prefix = setup_bare_git_dir(repo, &cwd, dir.len, &repo_fmt, nongit_ok);
 		break;
 	case GIT_DIR_HIT_CEILING:
 		if (!nongit_ok)
@@ -1984,30 +1984,30 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	    startup_info->have_repository ||
 	    /* GIT_DIR_EXPLICIT */
 	    getenv(GIT_DIR_ENVIRONMENT)) {
-		if (!the_repository->gitdir) {
+		if (!repo->gitdir) {
 			const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 			if (!gitdir)
 				gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
-			setup_git_env(the_repository, gitdir);
+			setup_git_env(repo, gitdir);
 		}
 		if (startup_info->have_repository) {
-			repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
-			repo_set_compat_hash_algo(the_repository,
+			repo_set_hash_algo(repo, repo_fmt.hash_algo);
+			repo_set_compat_hash_algo(repo,
 						  repo_fmt.compat_hash_algo);
-			repo_set_ref_storage_format(the_repository,
+			repo_set_ref_storage_format(repo,
 						    repo_fmt.ref_storage_format,
 						    repo_fmt.ref_storage_payload);
-			the_repository->repository_format_worktree_config =
+			repo->repository_format_worktree_config =
 				repo_fmt.worktree_config;
-			the_repository->repository_format_relative_worktrees =
+			repo->repository_format_relative_worktrees =
 				repo_fmt.relative_worktrees;
-			the_repository->repository_format_submodule_path_cfg =
+			repo->repository_format_submodule_path_cfg =
 				repo_fmt.submodule_path_cfg;
 			/* take ownership of repo_fmt.partial_clone */
-			the_repository->repository_format_partial_clone =
+			repo->repository_format_partial_clone =
 				repo_fmt.partial_clone;
 			repo_fmt.partial_clone = NULL;
-			the_repository->repository_format_precious_objects =
+			repo->repository_format_precious_objects =
 				repo_fmt.precious_objects;
 		}
 	}
@@ -2040,13 +2040,13 @@ const char *setup_git_directory_gently(int *nongit_ok)
 		format = ref_storage_format_by_name(backend);
 		if (format == REF_STORAGE_FORMAT_UNKNOWN)
 			die(_("unknown ref storage format: '%s'"), backend);
-		repo_set_ref_storage_format(the_repository, format, payload);
+		repo_set_ref_storage_format(repo, format, payload);
 
 		free(backend);
 		free(payload);
 	}
 
-	setup_original_cwd(the_repository);
+	setup_original_cwd(repo);
 
 	strbuf_release(&dir);
 	strbuf_release(&gitdir);
@@ -2138,7 +2138,7 @@ void check_repository_format(struct repository_format *fmt)
  */
 const char *setup_git_directory(void)
 {
-	return setup_git_directory_gently(NULL);
+	return setup_git_directory_gently(the_repository, NULL);
 }
 
 const char *resolve_gitdir_gently(const char *suspect, int *return_error_code)
diff --git a/setup.h b/setup.h
index 1a37089fa0..18092fbf16 100644
--- a/setup.h
+++ b/setup.h
@@ -136,7 +136,7 @@ enum {
  */
 const char *enter_repo(struct repository *repo, const char *path, unsigned flags);
 
-const char *setup_git_directory_gently(int *);
+const char *setup_git_directory_gently(struct repository *repo, int *);
 const char *setup_git_directory(void);
 char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path);
 char *prefix_path_gently(struct repository *repo, const char *prefix, int len, int *remaining, const char *path);
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 163fdeefb0..15eb44485c 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -377,7 +377,7 @@ int cmd__path_utils(int argc, const char **argv)
 		const char *prefix = argv[2];
 		int prefix_len = strlen(prefix);
 		int nongit_ok;
-		setup_git_directory_gently(&nongit_ok);
+		setup_git_directory_gently(the_repository, &nongit_ok);
 		while (argc > 3) {
 			char *pfx = prefix_path(the_repository, prefix, prefix_len, argv[3]);
 
diff --git a/t/helper/test-subprocess.c b/t/helper/test-subprocess.c
index 8a070e47cd..a8194d24b3 100644
--- a/t/helper/test-subprocess.c
+++ b/t/helper/test-subprocess.c
@@ -9,7 +9,7 @@ int cmd__subprocess(int argc, const char **argv)
 	struct child_process cp = CHILD_PROCESS_INIT;
 	int nogit = 0;
 
-	setup_git_directory_gently(&nogit);
+	setup_git_directory_gently(the_repository, &nogit);
 	if (nogit)
 		die("No git repo found");
 	if (argc > 1 && !strcmp(argv[1], "--setup-work-tree")) {
-- 
2.54.0.771.g3ed373ac14.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 66 of 73 in “setup: drop uses of `the_repository`”
  1. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, Apr 20, 2026
  2. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, Apr 20, 2026
  3. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, Apr 20, 2026
  4. Tian YuchenMay 17, 2026
  5. Junio C HamanoMay 17, 2026
  6. Patrick SteinhardtMay 18, 2026
  7. Junio C HamanoMay 19, 2026
  8. Patrick SteinhardtMay 19, 2026
  9. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, Apr 20, 2026
  10. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, Apr 20, 2026
  11. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, Apr 20, 2026
  12. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, Apr 20, 2026
  13. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, Apr 20, 2026
  14. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, Apr 20, 2026
  15. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, Apr 20, 2026
  16. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, Apr 20, 2026
  17. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, Apr 20, 2026
  18. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, Apr 20, 2026
  19. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, Apr 20, 2026
  20. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, Apr 20, 2026
  21. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, Apr 20, 2026
  22. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, Apr 20, 2026
  23. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, Apr 20, 2026
  24. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, Apr 20, 2026
  25. Elijah NewrenApr 20, 2026
  26. Patrick SteinhardtApr 21, 2026
  27. Junio C HamanoApr 20, 2026
  28. Patrick SteinhardtApr 21, 2026
  29. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 18, 2026
  30. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 18, 2026
  31. Karthik NayakMay 19, 2026
  32. 02/18 setup: stop using `the_repository` in `is_inside_worktree()`Patrick Steinhardt, May 18, 2026
  33. Karthik NayakMay 19, 2026
  34. Patrick SteinhardtMay 19, 2026
  35. 03/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 18, 2026
  36. Karthik NayakMay 19, 2026
  37. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 18, 2026
  38. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 18, 2026
  39. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 18, 2026
  40. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 18, 2026
  41. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 18, 2026
  42. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 18, 2026
  43. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 18, 2026
  44. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 18, 2026
  45. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 18, 2026
  46. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 18, 2026
  47. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 18, 2026
  48. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 18, 2026
  49. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 18, 2026
  50. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 18, 2026
  51. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 18, 2026
  52. Karthik NayakMay 19, 2026
  53. Patrick SteinhardtMay 19, 2026
  54. 00/18 setup: drop uses of `the_repository`Patrick Steinhardt, May 19, 2026
  55. 01/18 setup: replace use of `the_repository` in static functionsPatrick Steinhardt, May 19, 2026
  56. 02/18 setup: stop using `the_repository` in `is_inside_git_dir()`Patrick Steinhardt, May 19, 2026
  57. 03/18 setup: stop using `the_repository` in `is_inside_work_tree()`Patrick Steinhardt, May 19, 2026
  58. 04/18 setup: stop using `the_repository` in `prefix_path()`Patrick Steinhardt, May 19, 2026
  59. 05/18 setup: stop using `the_repository` in `path_inside_repo()`Patrick Steinhardt, May 19, 2026
  60. 06/18 setup: stop using `the_repository` in `verify_filename()`Patrick Steinhardt, May 19, 2026
  61. 07/18 setup: stop using `the_repository` in `verify_non_filename()`Patrick Steinhardt, May 19, 2026
  62. 08/18 setup: stop using `the_repository` in `enter_repo()`Patrick Steinhardt, May 19, 2026
  63. 09/18 setup: stop using `the_repository` in `setup_work_tree()`Patrick Steinhardt, May 19, 2026
  64. 10/18 setup: stop using `the_repository` in `set_git_work_tree()`Patrick Steinhardt, May 19, 2026
  65. 11/18 setup: stop using `the_repository` in `setup_git_env()`Patrick Steinhardt, May 19, 2026
  66. 12/18 setup: stop using `the_repository` in `setup_git_directory_gently()`Patrick Steinhardt, May 19, 2026
  67. 14/18 setup: stop using `the_repository` in `upgrade_repository_format()`Patrick Steinhardt, May 19, 2026
  68. 13/18 setup: stop using `the_repository` in `setup_git_directory()`Patrick Steinhardt, May 19, 2026
  69. 15/18 setup: stop using `the_repository` in `check_repository_format()`Patrick Steinhardt, May 19, 2026
  70. 16/18 setup: stop using `the_repository` in `initialize_repository_version()`Patrick Steinhardt, May 19, 2026
  71. 17/18 setup: stop using `the_repository` in `create_reference_database()`Patrick Steinhardt, May 19, 2026
  72. 18/18 setup: stop using `the_repository` in `init_db()`Patrick Steinhardt, May 19, 2026
  73. Junio C HamanoMay 20, 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.