repository: create disable_replace_refs()

Several builtins depend on being able to disable the replace references so we actually operate on each object individually. These currently do so by directly mutating the 'read_replace_refs' global. A future change will move this global into a different place, so it will be necessary to change all of these lines. However, we can simplify that transition by abstracting the purpose of these global assignments with a method call. We will need to keep this read_replace_refs global forever, as we want to make sure that we never use replace refs throughout the life of the process if this method is called. Future changes may present a repository-scoped version of the variable to represent that repository's core.useReplaceRefs config value, but a zero-valued read_replace_refs will always override such a setting. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 6, 2023 at 13:24 UTC d24eda4e03cabbd39ce06827d47e52a97b9095db
13 files changed +24 -11
builtin/cat-file.c
+1 -1
@@ -805,7 +805,7 @@ static int batch_objects(struct batch_options *opt)
805 if (repo_has_promisor_remote(the_repository))
806 warning("This repository uses promisor remotes. Some objects may not be loaded.");
807
808 - read_replace_refs = 0;
808 + disable_replace_refs();
809
810 cb.opt = opt;
811 cb.expand = &data;
builtin/commit-graph.c
+1 -1
@@ -324,7 +324,7 @@ int cmd_commit_graph(int argc, const char **argv, const char *prefix)
324
325 git_config(git_default_config, NULL);
326
327 - read_replace_refs = 0;
327 + disable_replace_refs();
328 save_commit_buffer = 0;
329
330 argc = parse_options(argc, argv, prefix, options,
builtin/fsck.c
+1 -1
@@ -927,7 +927,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
927 fetch_if_missing = 0;
928
929 errors_found = 0;
930 - read_replace_refs = 0;
930 + disable_replace_refs();
931 save_commit_buffer = 0;
932
933 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
builtin/index-pack.c
+1 -1
@@ -1752,7 +1752,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1752 if (argc == 2 && !strcmp(argv[1], "-h"))
1753 usage(index_pack_usage);
1754
1755 - read_replace_refs = 0;
1755 + disable_replace_refs();
1756 fsck_options.walk = mark_link;
1757
1758 reset_pack_idx_option(&opts);
builtin/pack-objects.c
+1 -1
@@ -4284,7 +4284,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4284 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4285 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4286
4287 - read_replace_refs = 0;
4287 + disable_replace_refs();
4288
4289 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4290 if (the_repository->gitdir) {
builtin/prune.c
+1 -1
@@ -164,7 +164,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
164
165 expire = TIME_MAX;
166 save_commit_buffer = 0;
167 - read_replace_refs = 0;
167 + disable_replace_refs();
168 repo_init_revisions(the_repository, &revs, prefix);
169
170 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
builtin/replace.c
+1 -1
@@ -566,7 +566,7 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
566 OPT_END()
567 };
568
569 - read_replace_refs = 0;
569 + disable_replace_refs();
570 git_config(git_default_config, NULL);
571
572 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
builtin/unpack-objects.c
+1 -1
@@ -609,7 +609,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
609 int i;
610 struct object_id oid;
611
612 - read_replace_refs = 0;
612 + disable_replace_refs();
613
614 git_config(git_default_config, NULL);
615
builtin/upload-pack.c
+1 -1
@@ -36,7 +36,7 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix)
36 };
37
38 packet_trace_identity("upload-pack");
39 - read_replace_refs = 0;
39 + disable_replace_refs();
40
41 argc = parse_options(argc, argv, prefix, options, upload_pack_usage, 0);
42
environment.c
+1 -1
@@ -185,7 +185,7 @@ void setup_git_env(const char *git_dir)
185 strvec_clear(&to_free);
186
187 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
188 - read_replace_refs = 0;
188 + disable_replace_refs();
189 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
190 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
191 : "refs/replace/");
git.c
+1 -1
@@ -185,7 +185,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
185 if (envchanged)
186 *envchanged = 1;
187 } else if (!strcmp(cmd, "--no-replace-objects")) {
188 - read_replace_refs = 0;
188 + disable_replace_refs();
189 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
190 if (envchanged)
191 *envchanged = 1;
replace-object.c
+5
@@ -84,3 +84,8 @@ const struct object_id *do_lookup_replace_object(struct repository *r,
84 }
85 die(_("replace depth too high for object %s"), oid_to_hex(oid));
86 }
87 +
88 +void disable_replace_refs(void)
89 +{
90 + read_replace_refs = 0;
91 +}
replace-object.h
+8
@@ -48,4 +48,12 @@ static inline const struct object_id *lookup_replace_object(struct repository *r
48 return do_lookup_replace_object(r, oid);
49 }
50
51 +/*
52 + * Some commands override config and environment settings for using
53 + * replace references. Use this method to disable the setting and ensure
54 + * those other settings will not override this choice. This applies
55 + * globally to all in-process repositories.
56 + */
57 +void disable_replace_refs(void);
58 +
59 #endif /* REPLACE_OBJECT_H */