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

[PATCH 03/13] setup: unify setup of shallow file

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

It is possible to configure an arbitrary "shallow" file via two mechanisms, and the respective logic to handle these is split across two locations:

  - Via the "GIT_SHALLOW_FILE" environment variable, which is handled in
    `setup_git_env_internal()`.
  - Via the global "--shallow-file=" command line option, which is
    handled in `handle_options()`.

We can rather easily unify this logic by not configuring the shallow file in `handle_options()`, but instead overwriting the environment variable. The environment variable itself is then handled inside of `apply_repository_format()`, which is responsible for configuring a discovered Git directory.

This new logic is similar in nature to how we handle the other global options already, all of which end up setting an environment variable. So for one this gives us more consistency. But more importantly, this change means that `the_repository` will not contain any relevant state anymore before we hit `apply_repository_format()` once we're at the end of this patch series. Consequently, it will become possible for us to completely discard `the_repository` and populate it anew.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 git.c   |  2 +-
 setup.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/git.c b/git.c
index 387eabe38c..e5f1811b6b 100644
--- a/git.c
+++ b/git.c
@@ -306,7 +306,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 		} else if (!strcmp(cmd, "--shallow-file")) {
 			(*argv)++;
 			(*argc)--;
-			set_alternate_shallow_file(the_repository, (*argv)[0], 1);
+			setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
 			if (envchanged)
 				*envchanged = 1;
 		} else if (!strcmp(cmd, "-C")) {
diff --git a/setup.c b/setup.c
index f54eac5e5a..5e6b959f68 100644
--- a/setup.c
+++ b/setup.c
@@ -1046,7 +1046,6 @@ static void setup_git_env_internal(struct repository *repo,
 				   const char *git_dir)
 {
 	char *git_replace_ref_base;
-	const char *shallow_file;
 	const char *replace_ref_base;
 	struct set_gitdir_args args = { NULL };
 	struct strvec to_free = STRVEC_INIT;
@@ -1067,10 +1066,6 @@ static void setup_git_env_internal(struct repository *repo,
 							  : "refs/replace/");
 	update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
 
-	shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
-	if (shallow_file)
-		set_alternate_shallow_file(repo, shallow_file, 0);
-
 	if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0))
 		fetch_if_missing = 0;
 }
@@ -1774,8 +1769,13 @@ int apply_repository_format(struct repository *repo,
 	}
 
 	if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
+		const char *shallow_file;
+
 		object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
 		alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
+		shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
+		if (shallow_file)
+			set_alternate_shallow_file(repo, shallow_file, 0);
 	}
 
 	repo->bare_cfg = format->is_bare;
-- 
2.55.0.795.g602f6c329a.dirty
Previous: Patrick SteinhardtNext: Justin Tobler
Message 9 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.