setup: unify setup of shallow file

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. Note that on first sight, this change looks like it might change the precedence order. Before this change, we used to configure the shallow file in the arguments handler first, and then it looks like we override it via the environment variable. What's important to note though is the last parameter to `set_alternate_shallow_file()`, which tells us whether we want to overwrite a preexisting value, and when applying the value from the environment we tell it not to overwrite preexisting values. So in effect, the command line has precedence over the environment. After this change, we now overwrite preexisting environment variables when we see the argument, and consequently we keep the precedence order in tact. With this change though we don't need the final parameter anymore that tells `set_alternate_shallow_file()` whether or not to overwrite. We only have a single callsite for this function now, and that function is itself only ever called exactly once. Remove that parameter. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 7, 2026 at 09:21 UTC d43b9acb05b666627e63f9e5fe25a818ca4c2aba
4 files changed +8 -10
git.c
+1 -1
@@ -306,7 +306,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
306 } else if (!strcmp(cmd, "--shallow-file")) {
307 (*argv)++;
308 (*argc)--;
309 - set_alternate_shallow_file(the_repository, (*argv)[0], 1);
309 + setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
310 if (envchanged)
311 *envchanged = 1;
312 } else if (!strcmp(cmd, "-C")) {
setup.c
+5 -5
@@ -1046,7 +1046,6 @@ static void setup_git_env_internal(struct repository *repo,
1046 const char *git_dir)
1047 {
1048 char *git_replace_ref_base;
1049 - const char *shallow_file;
1049 const char *replace_ref_base;
1050 struct set_gitdir_args args = { NULL };
1051 struct strvec to_free = STRVEC_INIT;
@@ -1067,10 +1066,6 @@ static void setup_git_env_internal(struct repository *repo,
1066 : "refs/replace/");
1067 update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
1068
1070 - shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
1071 - if (shallow_file)
1072 - set_alternate_shallow_file(repo, shallow_file, 0);
1073 -
1069 if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0))
1070 fetch_if_missing = 0;
1071 }
@@ -1774,8 +1769,13 @@ int apply_repository_format(struct repository *repo,
1769 }
1770
1771 if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
1772 + const char *shallow_file;
1773 +
1774 object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1775 alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1776 + shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
1777 + if (shallow_file)
1778 + set_alternate_shallow_file(repo, shallow_file);
1779 }
1780
1781 repo->bare_cfg = format->is_bare;
shallow.c
+1 -3
@@ -21,12 +21,10 @@
21 #include "statinfo.h"
22 #include "trace.h"
23
24 -void set_alternate_shallow_file(struct repository *r, const char *path, int override)
24 +void set_alternate_shallow_file(struct repository *r, const char *path)
25 {
26 if (r->parsed_objects->is_shallow != -1)
27 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
28 - if (r->parsed_objects->alternate_shallow_file && !override)
29 - return;
28 free(r->parsed_objects->alternate_shallow_file);
29 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
30 }
shallow.h
+1 -1
@@ -10,7 +10,7 @@
10 struct oid_array;
11 struct strvec;
12
13 -void set_alternate_shallow_file(struct repository *r, const char *path, int override);
13 +void set_alternate_shallow_file(struct repository *r, const char *path);
14 int register_shallow(struct repository *r, const struct object_id *oid);
15 int unregister_shallow(const struct object_id *oid);
16 int is_repository_shallow(struct repository *r);