git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 08/20] hash: require hash algorithm in `empty_tree_oid_hex()`

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 14, 2024, 06:50 UTC
Message-ID
<1cf25bec3eef9f590abc7ae2a84845a8312e9a4b.1718347699.git.ps@pks.im>
In-Reply-To
<cover.1718347699.git.ps@pks.im>

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>
---
 add-interactive.c      |  2 +-
 add-patch.c            |  2 +-
 builtin/merge.c        |  3 ++-
 builtin/receive-pack.c |  2 +-
 hash-ll.h              |  3 +--
 object-file.c          | 10 ++--------
 sequencer.c            |  2 +-
 submodule.c            |  6 +++---
 wt-status.c            |  4 ++--
 9 files changed, 14 insertions(+), 20 deletions(-)
diff --git a/add-interactive.c b/add-interactive.c
index b5d6cd689a..a0961096cd 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -557,7 +557,7 @@ static int get_modified_files(struct repository *r,
 		s.skip_unseen = filter && i;
 
 		opt.def = is_initial ?
-			empty_tree_oid_hex() : oid_to_hex(&head_oid);
+			empty_tree_oid_hex(the_repository->hash_algo) : oid_to_hex(&head_oid);
 
 		repo_init_revisions(r, &rev, NULL);
 		setup_revisions(0, NULL, &rev, &opt);
diff --git a/add-patch.c b/add-patch.c
index 814de57c4a..86181770f2 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -420,7 +420,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
 			    /* could be on an unborn branch */
 			    !strcmp("HEAD", s->revision) &&
 			    repo_get_oid(the_repository, "HEAD", &oid) ?
-			    empty_tree_oid_hex() : s->revision);
+			    empty_tree_oid_hex(the_repository->hash_algo) : s->revision);
 	}
 	color_arg_index = args.nr;
 	/* Use `--no-color` explicitly, just in case `diff.color = always`. */
diff --git a/builtin/merge.c b/builtin/merge.c
index abe66311c7..bb94b7df21 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -330,7 +330,8 @@ static void read_empty(const struct object_id *oid)
 {
 	struct child_process cmd = CHILD_PROCESS_INIT;
 
-	strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(),
+	strvec_pushl(&cmd.args, "read-tree", "-m", "-u",
+		     empty_tree_oid_hex(the_repository->hash_algo),
 		     oid_to_hex(oid), NULL);
 	cmd.git_cmd = 1;
 
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index aa5ba27d17..41d5fb8e60 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1371,7 +1371,7 @@ static const char *push_to_deploy(unsigned char *sha1,
 	strvec_pushl(&child.args, "diff-index", "--quiet", "--cached",
 		     "--ignore-submodules",
 		     /* diff-index with either HEAD or an empty tree */
-		     head_has_history() ? "HEAD" : empty_tree_oid_hex(),
+		     head_has_history() ? "HEAD" : empty_tree_oid_hex(the_repository->hash_algo),
 		     "--", NULL);
 	strvec_pushv(&child.env, env->v);
 	child.no_stdin = 1;
diff --git a/hash-ll.h b/hash-ll.h
index 1000a9af22..3161c778b9 100644
--- a/hash-ll.h
+++ b/hash-ll.h
@@ -347,8 +347,7 @@ static inline int is_null_oid(const struct object_id *oid)
 	return !memcmp(oid->hash, null_hash, GIT_MAX_RAWSZ);
 }
 
-const char *empty_tree_oid_hex(void);
-const char *empty_blob_oid_hex(void);
+const char *empty_tree_oid_hex(const struct git_hash_algo *algop);
 
 static inline int is_empty_blob_oid(const struct object_id *oid,
 				    const struct git_hash_algo *algop)
diff --git a/object-file.c b/object-file.c
index bb97f8a809..72318c8dd4 100644
--- a/object-file.c
+++ b/object-file.c
@@ -227,16 +227,10 @@ const struct object_id *null_oid(void)
 	return the_hash_algo->null_oid;
 }
 
-const char *empty_tree_oid_hex(void)
+const char *empty_tree_oid_hex(const struct git_hash_algo *algop)
 {
 	static char buf[GIT_MAX_HEXSZ + 1];
-	return oid_to_hex_r(buf, the_hash_algo->empty_tree);
-}
-
-const char *empty_blob_oid_hex(void)
-{
-	static char buf[GIT_MAX_HEXSZ + 1];
-	return oid_to_hex_r(buf, the_hash_algo->empty_blob);
+	return oid_to_hex_r(buf, algop->empty_tree);
 }
 
 int hash_algo_by_name(const char *name)
diff --git a/sequencer.c b/sequencer.c
index 68d62a12ff..823691e379 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2263,7 +2263,7 @@ static int do_pick_commit(struct repository *r,
 			unborn = 1;
 		} else if (unborn)
 			oidcpy(&head, the_hash_algo->empty_tree);
-		if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
+		if (index_differs_from(r, unborn ? empty_tree_oid_hex(the_repository->hash_algo) : "HEAD",
 				       NULL, 0))
 			return error_dirty_index(r, opts);
 	}
diff --git a/submodule.c b/submodule.c
index 759cf1e1cd..caf3aa5600 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2119,7 +2119,7 @@ static void submodule_reset_index(const char *path, const char *super_prefix)
 	strvec_pushf(&cp.args, "--super-prefix=%s%s/",
 		     (super_prefix ? super_prefix : ""), path);
 
-	strvec_push(&cp.args, empty_tree_oid_hex());
+	strvec_push(&cp.args, empty_tree_oid_hex(the_repository->hash_algo));
 
 	if (run_command(&cp))
 		die(_("could not reset submodule index"));
@@ -2229,9 +2229,9 @@ int submodule_move_head(const char *path, const char *super_prefix,
 		strvec_push(&cp.args, "-m");
 
 	if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
-		strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
+		strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex(the_repository->hash_algo));
 
-	strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
+	strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex(the_repository->hash_algo));
 
 	if (run_command(&cp)) {
 		ret = error(_("Submodule '%s' could not be updated."), path);
diff --git a/wt-status.c b/wt-status.c
index ff4be071ca..5051f5e599 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -641,7 +641,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
 
 	repo_init_revisions(s->repo, &rev, NULL);
 	memset(&opt, 0, sizeof(opt));
-	opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
+	opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
 	setup_revisions(0, NULL, &rev, &opt);
 
 	rev.diffopt.flags.override_submodule_config = 1;
@@ -1136,7 +1136,7 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
 	rev.diffopt.ita_invisible_in_index = 1;
 
 	memset(&opt, 0, sizeof(opt));
-	opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
+	opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
 	setup_revisions(0, NULL, &rev, &opt);
 
 	rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
-- 
2.45.2.457.g8d94cfb545.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 74 of 94 in “Introduce `USE_THE_REPOSITORY_VARIABLE` macro”
  1. 00/21 Introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 11, 2024
  2. 01/21 hash: drop (mostly) unused `is_empty_{blob,tree}_sha1()` functionsPatrick Steinhardt, Jun 11, 2024
  3. 02/21 hash: require hash algorithm in `hasheq()`, `hashcmp()` and `hashclr()`Patrick Steinhardt, Jun 11, 2024
  4. 03/21 hash: require hash algorithm in `oidread()` and `oidclr()`Patrick Steinhardt, Jun 11, 2024
  5. 04/21 global: ensure that object IDs are always paddedPatrick Steinhardt, Jun 11, 2024
  6. 05/21 hash: convert `oidcmp()` and `oideq()` to compare whole hashPatrick Steinhardt, Jun 11, 2024
  7. 06/21 hash: make `is_null_oid()` independent of `the_repository`Patrick Steinhardt, Jun 11, 2024
  8. 07/21 hash: require hash algorithm in `is_empty_{blob,tree}_oid()`Patrick Steinhardt, Jun 11, 2024
  9. 08/21 hash: require hash algorithm in `empty_tree_oid_hex()`Patrick Steinhardt, Jun 11, 2024
  10. 09/21 global: introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 11, 2024
  11. 10/21 refs: avoid include cycle with "repository.h"Patrick Steinhardt, Jun 11, 2024
  12. 11/21 hash-ll: merge with "hash.h"Patrick Steinhardt, Jun 11, 2024
  13. 12/21 http-fetch: don't crash when parsing packfile without a repoPatrick Steinhardt, Jun 11, 2024
  14. 13/21 oidset: pass hash algorithm when parsing filePatrick Steinhardt, Jun 11, 2024
  15. 14/21 protocol-caps: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 11, 2024
  16. 15/21 replace-object: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 11, 2024
  17. 16/21 compat/fsmonitor: remove dependency on `the_repository` in Darwin IPCPatrick Steinhardt, Jun 11, 2024
  18. brian m. carlsonJun 11, 2024
  19. Patrick SteinhardtJun 12, 2024
  20. 17/21 t/helper: use correct object hash in partial-clone helperPatrick Steinhardt, Jun 11, 2024
  21. 18/21 t/helper: fix segfault in "oid-array" command without repositoryPatrick Steinhardt, Jun 11, 2024
  22. Ghanshyam ThakkarJun 11, 2024
  23. 19/21 t/helper: remove dependency on `the_repository` in "oidtree"Patrick Steinhardt, Jun 11, 2024
  24. Ghanshyam ThakkarJun 11, 2024
  25. Patrick SteinhardtJun 12, 2024
  26. brian m. carlsonJun 11, 2024
  27. Patrick SteinhardtJun 12, 2024
  28. 20/21 t/helper: remove dependency on `the_repository` in "proc-receive"Patrick Steinhardt, Jun 11, 2024
  29. 21/21 hex: guard declarations with `USE_THE_REPOSITORY_VARIABLE`Patrick Steinhardt, Jun 11, 2024
  30. brian m. carlsonJun 11, 2024
  31. Patrick SteinhardtJun 12, 2024
  32. Ghanshyam ThakkarJun 12, 2024
  33. 00/20 Introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 13, 2024
  34. 01/20 hash: drop (mostly) unused `is_empty_{blob,tree}_sha1()` functionsPatrick Steinhardt, Jun 13, 2024
  35. 02/20 hash: require hash algorithm in `hasheq()`, `hashcmp()` and `hashclr()`Patrick Steinhardt, Jun 13, 2024
  36. 03/20 hash: require hash algorithm in `oidread()` and `oidclr()`Patrick Steinhardt, Jun 13, 2024
  37. 04/20 global: ensure that object IDs are always paddedPatrick Steinhardt, Jun 13, 2024
  38. 05/20 hash: convert `oidcmp()` and `oideq()` to compare whole hashPatrick Steinhardt, Jun 13, 2024
  39. 06/20 hash: make `is_null_oid()` independent of `the_repository`Patrick Steinhardt, Jun 13, 2024
  40. 07/20 hash: require hash algorithm in `is_empty_{blob,tree}_oid()`Patrick Steinhardt, Jun 13, 2024
  41. 08/20 hash: require hash algorithm in `empty_tree_oid_hex()`Patrick Steinhardt, Jun 13, 2024
  42. Phillip WoodJun 13, 2024
  43. Junio C HamanoJun 13, 2024
  44. Patrick SteinhardtJun 14, 2024
  45. 09/20 global: introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 13, 2024
  46. 10/20 refs: avoid include cycle with "repository.h"Patrick Steinhardt, Jun 13, 2024
  47. 11/20 hash-ll: merge with "hash.h"Patrick Steinhardt, Jun 13, 2024
  48. 12/20 http-fetch: don't crash when parsing packfile without a repoPatrick Steinhardt, Jun 13, 2024
  49. 13/20 oidset: pass hash algorithm when parsing filePatrick Steinhardt, Jun 13, 2024
  50. 14/20 protocol-caps: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 13, 2024
  51. 15/20 replace-object: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 13, 2024
  52. 16/20 compat/fsmonitor: fix socket path in networked SHA256 reposPatrick Steinhardt, Jun 13, 2024
  53. 17/20 t/helper: use correct object hash in partial-clone helperPatrick Steinhardt, Jun 13, 2024
  54. 18/20 t/helper: fix segfault in "oid-array" command without repositoryPatrick Steinhardt, Jun 13, 2024
  55. 19/20 t/helper: remove dependency on `the_repository` in "proc-receive"Patrick Steinhardt, Jun 13, 2024
  56. 20/20 hex: guard declarations with `USE_THE_REPOSITORY_VARIABLE`Patrick Steinhardt, Jun 13, 2024
  57. Junio C HamanoJun 13, 2024
  58. Junio C HamanoJun 13, 2024
  59. Ramsay JonesJun 13, 2024
  60. Junio C HamanoJun 13, 2024
  61. Ramsay JonesJun 13, 2024
  62. Junio C HamanoJun 14, 2024
  63. Patrick SteinhardtJun 14, 2024
  64. Junio C HamanoJun 14, 2024
  65. 00/20 Introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 14, 2024
  66. 01/20 hash: drop (mostly) unused `is_empty_{blob,tree}_sha1()` functionsPatrick Steinhardt, Jun 14, 2024
  67. 02/20 hash: require hash algorithm in `hasheq()`, `hashcmp()` and `hashclr()`Patrick Steinhardt, Jun 14, 2024
  68. 03/20 hash: require hash algorithm in `oidread()` and `oidclr()`Patrick Steinhardt, Jun 14, 2024
  69. 04/20 global: ensure that object IDs are always paddedPatrick Steinhardt, Jun 14, 2024
  70. 05/20 hash: convert `oidcmp()` and `oideq()` to compare whole hashPatrick Steinhardt, Jun 14, 2024
  71. Karthik NayakJun 17, 2024
  72. 06/20 hash: make `is_null_oid()` independent of `the_repository`Patrick Steinhardt, Jun 14, 2024
  73. 07/20 hash: require hash algorithm in `is_empty_{blob,tree}_oid()`Patrick Steinhardt, Jun 14, 2024
  74. 08/20 hash: require hash algorithm in `empty_tree_oid_hex()`Patrick Steinhardt, Jun 14, 2024
  75. 09/20 global: introduce `USE_THE_REPOSITORY_VARIABLE` macroPatrick Steinhardt, Jun 14, 2024
  76. Karthik NayakJun 17, 2024
  77. Patrick SteinhardtJun 18, 2024
  78. Karthik NayakJun 18, 2024
  79. Junio C HamanoJun 18, 2024
  80. Junio C HamanoJun 18, 2024
  81. Karthik NayakJun 18, 2024
  82. 10/20 refs: avoid include cycle with "repository.h"Patrick Steinhardt, Jun 14, 2024
  83. 11/20 hash-ll: merge with "hash.h"Patrick Steinhardt, Jun 14, 2024
  84. 12/20 http-fetch: don't crash when parsing packfile without a repoPatrick Steinhardt, Jun 14, 2024
  85. 13/20 oidset: pass hash algorithm when parsing filePatrick Steinhardt, Jun 14, 2024
  86. 14/20 protocol-caps: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 14, 2024
  87. 15/20 replace-object: use hash algorithm from passed-in repositoryPatrick Steinhardt, Jun 14, 2024
  88. 16/20 compat/fsmonitor: fix socket path in networked SHA256 reposPatrick Steinhardt, Jun 14, 2024
  89. 17/20 t/helper: use correct object hash in partial-clone helperPatrick Steinhardt, Jun 14, 2024
  90. 18/20 t/helper: fix segfault in "oid-array" command without repositoryPatrick Steinhardt, Jun 14, 2024
  91. 19/20 t/helper: remove dependency on `the_repository` in "proc-receive"Patrick Steinhardt, Jun 14, 2024
  92. 20/20 hex: guard declarations with `USE_THE_REPOSITORY_VARIABLE`Patrick Steinhardt, Jun 14, 2024
  93. Phillip WoodJun 18, 2024
  94. Karthik NayakJun 18, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.