hash: require hash algorithm in `empty_tree_oid_hex()`

The `empty_tree_oid_hex()` function use `the_repository` to derive the hash function that shall be used. Require callers to pass in the hash algorithm to get rid of this implicit dependency. While at it, remove the unused `empty_blob_oid_hex()` function. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 14, 2024 at 08:50 UTC 7abbca0e7470d1f7b6973933358c17022c6ee8a1
9 files changed +14 -20
add-interactive.c
+1 -1
@@ -557,7 +557,7 @@ static int get_modified_files(struct repository *r,
557 s.skip_unseen = filter && i;
558
559 opt.def = is_initial ?
560 - empty_tree_oid_hex() : oid_to_hex(&head_oid);
560 + empty_tree_oid_hex(the_repository->hash_algo) : oid_to_hex(&head_oid);
561
562 repo_init_revisions(r, &rev, NULL);
563 setup_revisions(0, NULL, &rev, &opt);
add-patch.c
+1 -1
@@ -420,7 +420,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
420 /* could be on an unborn branch */
421 !strcmp("HEAD", s->revision) &&
422 repo_get_oid(the_repository, "HEAD", &oid) ?
423 - empty_tree_oid_hex() : s->revision);
423 + empty_tree_oid_hex(the_repository->hash_algo) : s->revision);
424 }
425 color_arg_index = args.nr;
426 /* Use `--no-color` explicitly, just in case `diff.color = always`. */
builtin/merge.c
+2 -1
@@ -330,7 +330,8 @@ static void read_empty(const struct object_id *oid)
330 {
331 struct child_process cmd = CHILD_PROCESS_INIT;
332
333 - strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(),
333 + strvec_pushl(&cmd.args, "read-tree", "-m", "-u",
334 + empty_tree_oid_hex(the_repository->hash_algo),
335 oid_to_hex(oid), NULL);
336 cmd.git_cmd = 1;
337
builtin/receive-pack.c
+1 -1
@@ -1371,7 +1371,7 @@ static const char *push_to_deploy(unsigned char *sha1,
1371 strvec_pushl(&child.args, "diff-index", "--quiet", "--cached",
1372 "--ignore-submodules",
1373 /* diff-index with either HEAD or an empty tree */
1374 - head_has_history() ? "HEAD" : empty_tree_oid_hex(),
1374 + head_has_history() ? "HEAD" : empty_tree_oid_hex(the_repository->hash_algo),
1375 "--", NULL);
1376 strvec_pushv(&child.env, env->v);
1377 child.no_stdin = 1;
hash-ll.h
+1 -2
@@ -347,8 +347,7 @@ static inline int is_null_oid(const struct object_id *oid)
347 return !memcmp(oid->hash, null_hash, GIT_MAX_RAWSZ);
348 }
349
350 -const char *empty_tree_oid_hex(void);
351 -const char *empty_blob_oid_hex(void);
350 +const char *empty_tree_oid_hex(const struct git_hash_algo *algop);
351
352 static inline int is_empty_blob_oid(const struct object_id *oid,
353 const struct git_hash_algo *algop)
object-file.c
+2 -8
@@ -227,16 +227,10 @@ const struct object_id *null_oid(void)
227 return the_hash_algo->null_oid;
228 }
229
230 -const char *empty_tree_oid_hex(void)
230 +const char *empty_tree_oid_hex(const struct git_hash_algo *algop)
231 {
232 static char buf[GIT_MAX_HEXSZ + 1];
233 - return oid_to_hex_r(buf, the_hash_algo->empty_tree);
234 -}
235 -
236 -const char *empty_blob_oid_hex(void)
237 -{
238 - static char buf[GIT_MAX_HEXSZ + 1];
239 - return oid_to_hex_r(buf, the_hash_algo->empty_blob);
233 + return oid_to_hex_r(buf, algop->empty_tree);
234 }
235
236 int hash_algo_by_name(const char *name)
sequencer.c
+1 -1
@@ -2263,7 +2263,7 @@ static int do_pick_commit(struct repository *r,
2263 unborn = 1;
2264 } else if (unborn)
2265 oidcpy(&head, the_hash_algo->empty_tree);
2266 - if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
2266 + if (index_differs_from(r, unborn ? empty_tree_oid_hex(the_repository->hash_algo) : "HEAD",
2267 NULL, 0))
2268 return error_dirty_index(r, opts);
2269 }
submodule.c
+3 -3
@@ -2119,7 +2119,7 @@ static void submodule_reset_index(const char *path, const char *super_prefix)
2119 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2120 (super_prefix ? super_prefix : ""), path);
2121
2122 - strvec_push(&cp.args, empty_tree_oid_hex());
2122 + strvec_push(&cp.args, empty_tree_oid_hex(the_repository->hash_algo));
2123
2124 if (run_command(&cp))
2125 die(_("could not reset submodule index"));
@@ -2229,9 +2229,9 @@ int submodule_move_head(const char *path, const char *super_prefix,
2229 strvec_push(&cp.args, "-m");
2230
2231 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
2232 - strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
2232 + strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex(the_repository->hash_algo));
2233
2234 - strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
2234 + strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex(the_repository->hash_algo));
2235
2236 if (run_command(&cp)) {
2237 ret = error(_("Submodule '%s' could not be updated."), path);
wt-status.c
+2 -2
@@ -641,7 +641,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
641
642 repo_init_revisions(s->repo, &rev, NULL);
643 memset(&opt, 0, sizeof(opt));
644 - opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
644 + opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
645 setup_revisions(0, NULL, &rev, &opt);
646
647 rev.diffopt.flags.override_submodule_config = 1;
@@ -1136,7 +1136,7 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
1136 rev.diffopt.ita_invisible_in_index = 1;
1137
1138 memset(&opt, 0, sizeof(opt));
1139 - opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
1139 + opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
1140 setup_revisions(0, NULL, &rev, &opt);
1141
1142 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;