Skip to content

Commit f117838

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d24eda4 commit f117838

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

commit-graph.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,12 @@ static struct commit_graph *alloc_commit_graph(void)
203203
return g;
204204
}
205205

206-
extern int read_replace_refs;
207-
208206
static int commit_graph_compatible(struct repository *r)
209207
{
210208
if (!r->gitdir)
211209
return 0;
212210

213-
if (read_replace_refs) {
211+
if (replace_refs_enabled(r)) {
214212
prepare_replace_object(r);
215213
if (hashmap_get_size(&r->objects->replace_map->map))
216214
return 0;

log-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
155155

156156
if (starts_with(refname, git_replace_ref_base)) {
157157
struct object_id original_oid;
158-
if (!read_replace_refs)
158+
if (!replace_refs_enabled(the_repository))
159159
return 0;
160160
if (get_oid_hex(refname + strlen(git_replace_ref_base),
161161
&original_oid)) {

replace-object.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ void disable_replace_refs(void)
8989
{
9090
read_replace_refs = 0;
9191
}
92+
93+
int replace_refs_enabled(struct repository *r)
94+
{
95+
return read_replace_refs;
96+
}

replace-object.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ void prepare_replace_object(struct repository *r);
2727
const struct object_id *do_lookup_replace_object(struct repository *r,
2828
const struct object_id *oid);
2929

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+
3042
/*
3143
* If object sha1 should be replaced, return the replacement object's
3244
* name (replaced recursively, if necessary). The return value is
@@ -41,7 +53,7 @@ const struct object_id *do_lookup_replace_object(struct repository *r,
4153
static inline const struct object_id *lookup_replace_object(struct repository *r,
4254
const struct object_id *oid)
4355
{
44-
if (!read_replace_refs ||
56+
if (!replace_refs_enabled(r) ||
4557
(r->objects->replace_map_initialized &&
4658
r->objects->replace_map->map.tablesize == 0))
4759
return oid;

0 commit comments

Comments
 (0)