replace-objects: create wrapper around setting

The 'read_replace_objects' constant is initialized by git_default_config (if core.useReplaceRefs is disabled) and within setup_git_env (if GIT_NO_REPLACE_OBJECTS) is set. To ensure that this variable cannot be set accidentally in other places, wrap it in a replace_refs_enabled() method. Since we still assign this global in config.c, we are not able to remove the global scope of this variable and make it a static within replace-object.c. This will happen in a later change which will also prevent the variable from being read before it is initialized. Centralizing read access to the variable is an important first step. 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 f1178380ac645684fcf0519488f9336e55f0946f
4 files changed +20 -5
commit-graph.c
+1 -3
@@ -203,14 +203,12 @@ static struct commit_graph *alloc_commit_graph(void)
203 return g;
204 }
205
206 -extern int read_replace_refs;
207 -
206 static int commit_graph_compatible(struct repository *r)
207 {
208 if (!r->gitdir)
209 return 0;
210
213 - if (read_replace_refs) {
211 + if (replace_refs_enabled(r)) {
212 prepare_replace_object(r);
213 if (hashmap_get_size(&r->objects->replace_map->map))
214 return 0;
log-tree.c
+1 -1
@@ -155,7 +155,7 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
155
156 if (starts_with(refname, git_replace_ref_base)) {
157 struct object_id original_oid;
158 - if (!read_replace_refs)
158 + if (!replace_refs_enabled(the_repository))
159 return 0;
160 if (get_oid_hex(refname + strlen(git_replace_ref_base),
161 &original_oid)) {
replace-object.c
+5
@@ -89,3 +89,8 @@ void disable_replace_refs(void)
89 {
90 read_replace_refs = 0;
91 }
92 +
93 +int replace_refs_enabled(struct repository *r)
94 +{
95 + return read_replace_refs;
96 +}
replace-object.h
+13 -1
@@ -27,6 +27,18 @@ void prepare_replace_object(struct repository *r);
27 const struct object_id *do_lookup_replace_object(struct repository *r,
28 const struct object_id *oid);
29
30 +/*
31 + * Some commands disable replace-refs unconditionally, and otherwise each
32 + * repository could alter the core.useReplaceRefs config value.
33 + *
34 + * Return 1 if and only if all of the following are true:
35 + *
36 + * a. disable_replace_refs() has not been called.
37 + * b. GIT_NO_REPLACE_OBJECTS is unset or zero.
38 + * c. the given repository does not have core.useReplaceRefs=false.
39 + */
40 +int replace_refs_enabled(struct repository *r);
41 +
42 /*
43 * If object sha1 should be replaced, return the replacement object's
44 * name (replaced recursively, if necessary). The return value is
@@ -41,7 +53,7 @@ const struct object_id *do_lookup_replace_object(struct repository *r,
53 static inline const struct object_id *lookup_replace_object(struct repository *r,
54 const struct object_id *oid)
55 {
44 - if (!read_replace_refs ||
56 + if (!replace_refs_enabled(r) ||
57 (r->objects->replace_map_initialized &&
58 r->objects->replace_map->map.tablesize == 0))
59 return oid;