refs: use ref_namespaces for replace refs base

The git_replace_ref_base global is used to store the value of the GIT_REPLACE_REF_BASE environment variable or the default of "refs/replace/". This is initialized within setup_git_env(). The ref_namespaces array is a new centralized location for information such as the ref namespace used for replace refs. Instead of having this namespace stored in two places, use the ref_namespaces array instead. For simplicity, create a local git_replace_ref_base variable wherever the global was previously used. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 5, 2022 at 17:58 UTC 97e61e0f9cd96f403504ec97cfdf38f238777560
5 files changed +6 -3
builtin/replace.c
+3
@@ -106,6 +106,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
106 size_t base_len;
107 int had_error = 0;
108 struct object_id oid;
109 + const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
110
111 strbuf_addstr(&ref, git_replace_ref_base);
112 base_len = ref.len;
@@ -147,6 +148,8 @@ static int check_ref_valid(struct object_id *object,
148 struct strbuf *ref,
149 int force)
150 {
151 + const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
152 +
153 strbuf_reset(ref);
154 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
155 if (check_refname_format(ref->buf, 0))
cache.h
-1
@@ -1008,7 +1008,6 @@ void reset_shared_repository(void);
1008 * commands that do not want replace references to be active.
1009 */
1010 extern int read_replace_refs;
1011 -extern char *git_replace_ref_base;
1011
1012 /*
1013 * These values are used to help identify parts of a repository to fsync.
environment.c
+1 -2
@@ -56,7 +56,6 @@ const char *askpass_program;
56 const char *excludes_file;
57 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
58 int read_replace_refs = 1;
59 -char *git_replace_ref_base;
59 enum eol core_eol = EOL_UNSET;
60 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
61 char *check_roundtrip_encoding = "SHIFT-JIS";
@@ -162,6 +161,7 @@ const char *getenv_safe(struct strvec *argv, const char *name)
161
162 void setup_git_env(const char *git_dir)
163 {
164 + char *git_replace_ref_base;
165 const char *shallow_file;
166 const char *replace_ref_base;
167 struct set_gitdir_args args = { NULL };
@@ -182,7 +182,6 @@ void setup_git_env(const char *git_dir)
182 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
183 read_replace_refs = 0;
184 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
185 - free(git_replace_ref_base);
185 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
186 : "refs/replace/");
187 update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
log-tree.c
+1
@@ -141,6 +141,7 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
141 enum object_type objtype;
142 enum decoration_type deco_type = DECORATION_NONE;
143 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
144 + const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
145
146 if (filter && !ref_filter_match(refname, filter))
147 return 0;
refs.c
+1
@@ -1612,6 +1612,7 @@ int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
1612
1613 int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
1614 {
1615 + const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
1616 return do_for_each_repo_ref(r, git_replace_ref_base, fn,
1617 strlen(git_replace_ref_base),
1618 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);