object-file-convert: stop depending on `the_repository`

There are multiple sites in "object-file-convert.c" where we use the global `the_repository` variable, either explicitly or implicitly by using `the_hash_algo`. All of these callsites are transitively called from `convert_object_file()`, which indeed has no repo as input. Refactor the function so that it receives a repository as a parameter and pass it through to all internal functions to get rid of the dependency. Remove the `USE_THE_REPOSITORY_VARIABLE` define. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 10, 2025 at 08:13 UTC f6e174b2d8b83c17376b163fc4f8062fa3140a04
5 files changed +24 -19
builtin/tag.c
+1 -1
@@ -172,7 +172,7 @@ static int do_sign(struct strbuf *buffer, struct object_id **compat_oid,
172 if (compat) {
173 const struct git_hash_algo *algo = the_repository->hash_algo;
174
175 - if (convert_object_file(&compat_buf, algo, compat,
175 + if (convert_object_file(the_repository ,&compat_buf, algo, compat,
176 buffer->buf, buffer->len, OBJ_TAG, 1))
177 goto out;
178 if (sign_buffer(&compat_buf, &compat_sig, keyid))
commit.c
+1 -1
@@ -1380,7 +1380,7 @@ static int convert_commit_extra_headers(const struct commit_extra_header *orig,
1380 struct commit_extra_header *new;
1381 CALLOC_ARRAY(new, 1);
1382 if (!strcmp(orig->key, "mergetag")) {
1383 - if (convert_object_file(&out, algo, compat,
1383 + if (convert_object_file(the_repository, &out, algo, compat,
1384 orig->value, orig->len,
1385 OBJ_TAG, 1)) {
1386 free(new);
object-file-convert.c
+16 -13
@@ -1,4 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
@@ -63,7 +62,8 @@ static int decode_tree_entry_raw(struct object_id *oid, const char **path,
62 return 0;
63 }
64
66 -static int convert_tree_object(struct strbuf *out,
65 +static int convert_tree_object(struct repository *repo,
66 + struct strbuf *out,
67 const struct git_hash_algo *from,
68 const struct git_hash_algo *to,
69 const char *buffer, size_t size)
@@ -78,7 +78,7 @@ static int convert_tree_object(struct strbuf *out,
78 if (decode_tree_entry_raw(&entry_oid, &path, &pathlen, from, p,
79 end - p))
80 return error(_("failed to decode tree entry"));
81 - if (repo_oid_to_algop(the_repository, &entry_oid, to, &mapped_oid))
81 + if (repo_oid_to_algop(repo, &entry_oid, to, &mapped_oid))
82 return error(_("failed to map tree entry for %s"), oid_to_hex(&entry_oid));
83 strbuf_add(out, p, path - p);
84 strbuf_add(out, path, pathlen);
@@ -88,7 +88,8 @@ static int convert_tree_object(struct strbuf *out,
88 return 0;
89 }
90
91 -static int convert_tag_object(struct strbuf *out,
91 +static int convert_tag_object(struct repository *repo,
92 + struct strbuf *out,
93 const struct git_hash_algo *from,
94 const struct git_hash_algo *to,
95 const char *buffer, size_t size)
@@ -105,7 +106,7 @@ static int convert_tag_object(struct strbuf *out,
106 return error("bogus tag object");
107 if (parse_oid_hex_algop(buffer + 7, &oid, &p, from) < 0)
108 return error("bad tag object ID");
108 - if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid))
109 + if (repo_oid_to_algop(repo, &oid, to, &mapped_oid))
110 return error("unable to map tree %s in tag object",
111 oid_to_hex(&oid));
112 size -= ((p + 1) - buffer);
@@ -139,7 +140,8 @@ static int convert_tag_object(struct strbuf *out,
140 return 0;
141 }
142
142 -static int convert_commit_object(struct strbuf *out,
143 +static int convert_commit_object(struct repository *repo,
144 + struct strbuf *out,
145 const struct git_hash_algo *from,
146 const struct git_hash_algo *to,
147 const char *buffer, size_t size)
@@ -165,7 +167,7 @@ static int convert_commit_object(struct strbuf *out,
167 (p != eol))
168 return error(_("bad %s in commit"), "tree");
169
168 - if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid))
170 + if (repo_oid_to_algop(repo, &oid, to, &mapped_oid))
171 return error(_("unable to map %s %s in commit object"),
172 "tree", oid_to_hex(&oid));
173 strbuf_addf(out, "tree %s\n", oid_to_hex(&mapped_oid));
@@ -177,7 +179,7 @@ static int convert_commit_object(struct strbuf *out,
179 (p != eol))
180 return error(_("bad %s in commit"), "parent");
181
180 - if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid))
182 + if (repo_oid_to_algop(repo, &oid, to, &mapped_oid))
183 return error(_("unable to map %s %s in commit object"),
184 "parent", oid_to_hex(&oid));
185
@@ -202,7 +204,7 @@ static int convert_commit_object(struct strbuf *out,
204 }
205
206 /* Compute the new tag object */
205 - if (convert_tag_object(&new_tag, from, to, tag.buf, tag.len)) {
207 + if (convert_tag_object(repo, &new_tag, from, to, tag.buf, tag.len)) {
208 strbuf_release(&tag);
209 strbuf_release(&new_tag);
210 return -1;
@@ -241,7 +243,8 @@ static int convert_commit_object(struct strbuf *out,
243 return 0;
244 }
245
244 -int convert_object_file(struct strbuf *outbuf,
246 +int convert_object_file(struct repository *repo,
247 + struct strbuf *outbuf,
248 const struct git_hash_algo *from,
249 const struct git_hash_algo *to,
250 const void *buf, size_t len,
@@ -256,13 +259,13 @@ int convert_object_file(struct strbuf *outbuf,
259
260 switch (type) {
261 case OBJ_COMMIT:
259 - ret = convert_commit_object(outbuf, from, to, buf, len);
262 + ret = convert_commit_object(repo, outbuf, from, to, buf, len);
263 break;
264 case OBJ_TREE:
262 - ret = convert_tree_object(outbuf, from, to, buf, len);
265 + ret = convert_tree_object(repo, outbuf, from, to, buf, len);
266 break;
267 case OBJ_TAG:
265 - ret = convert_tag_object(outbuf, from, to, buf, len);
268 + ret = convert_tag_object(repo, outbuf, from, to, buf, len);
269 break;
270 default:
271 /* Not implemented yet, so fail. */
object-file-convert.h
+2 -1
@@ -14,7 +14,8 @@ int repo_oid_to_algop(struct repository *repo, const struct object_id *src,
14 * Convert an object file from one hash algorithm to another algorithm.
15 * Return -1 on failure, 0 on success.
16 */
17 -int convert_object_file(struct strbuf *outbuf,
17 +int convert_object_file(struct repository *repo,
18 + struct strbuf *outbuf,
19 const struct git_hash_algo *from,
20 const struct git_hash_algo *to,
21 const void *buf, size_t len,
object-file.c
+4 -3
@@ -1793,7 +1793,7 @@ static int oid_object_info_convert(struct repository *r,
1793 if (type == -1)
1794 return -1;
1795 if (type != OBJ_BLOB) {
1796 - ret = convert_object_file(&outbuf,
1796 + ret = convert_object_file(the_repository, &outbuf,
1797 the_hash_algo, input_algo,
1798 content, size, type, !do_die);
1799 free(content);
@@ -2510,7 +2510,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
2510 hash_object_file(compat, buf, len, type, &compat_oid);
2511 else {
2512 struct strbuf converted = STRBUF_INIT;
2513 - convert_object_file(&converted, algo, compat,
2513 + convert_object_file(the_repository, &converted, algo, compat,
2514 buf, len, type, 0);
2515 hash_object_file(compat, converted.buf, converted.len,
2516 type, &compat_oid);
@@ -2550,7 +2550,8 @@ int write_object_file_literally(const void *buf, unsigned long len,
2550 &compat_oid);
2551 else if (compat_type != -1) {
2552 struct strbuf converted = STRBUF_INIT;
2553 - convert_object_file(&converted, algo, compat,
2553 + convert_object_file(the_repository,
2554 + &converted, algo, compat,
2555 buf, len, compat_type, 0);
2556 hash_object_file(compat, converted.buf, converted.len,
2557 compat_type, &compat_oid);