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

[PATCH v2 07/20] hash: require hash algorithm in `is_empty_{blob,tree}_oid()`

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 13, 2024, 06:13 UTC
Message-ID
<d26584dc8f6f7cca5d3173c80f694b2cb0491ba7.1718259125.git.ps@pks.im>
In-Reply-To
<cover.1718259125.git.ps@pks.im>

Both functions `is_empty_{blob,tree}_oid()` 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.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/fast-import.c |  4 +++-
 cache-tree.c          |  2 +-
 diffcore-rename.c     |  4 ++--
 hash-ll.h             | 12 ++++++++++++
 hash.h                | 10 ----------
 read-cache.c          |  2 +-
 6 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 12543488f3..d21c4053a7 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -2361,7 +2361,9 @@ static void file_change_m(const char *p, struct branch *b)
 	parse_path_eol(&path, p, "path");
 
 	/* Git does not track empty, non-toplevel directories. */
-	if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *path.buf) {
+	if (S_ISDIR(mode) &&
+	    is_empty_tree_oid(&oid, the_repository->hash_algo) &&
+	    *path.buf) {
 		tree_content_remove(&b->branch_tree, path.buf, NULL, 0);
 		return;
 	}
diff --git a/cache-tree.c b/cache-tree.c
index e4255c4d02..3290a1b8dd 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -422,7 +422,7 @@ static int update_one(struct cache_tree *it,
 		/*
 		 * "sub" can be an empty tree if all subentries are i-t-a.
 		 */
-		if (contains_ita && is_empty_tree_oid(oid))
+		if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo))
 			continue;
 
 		strbuf_grow(&buffer, entlen + 100);
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 5a6e2bcac7..5abb958651 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -1422,7 +1422,7 @@ void diffcore_rename_extended(struct diff_options *options,
 				 strcmp(options->single_follow, p->two->path))
 				continue; /* not interested */
 			else if (!options->flags.rename_empty &&
-				 is_empty_blob_oid(&p->two->oid))
+				 is_empty_blob_oid(&p->two->oid, the_repository->hash_algo))
 				continue;
 			else if (add_rename_dst(p) < 0) {
 				warning("skipping rename detection, detected"
@@ -1432,7 +1432,7 @@ void diffcore_rename_extended(struct diff_options *options,
 			}
 		}
 		else if (!options->flags.rename_empty &&
-			 is_empty_blob_oid(&p->one->oid))
+			 is_empty_blob_oid(&p->one->oid, the_repository->hash_algo))
 			continue;
 		else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
 			/*
diff --git a/hash-ll.h b/hash-ll.h
index faf6c292d2..1000a9af22 100644
--- a/hash-ll.h
+++ b/hash-ll.h
@@ -350,4 +350,16 @@ static inline int is_null_oid(const struct object_id *oid)
 const char *empty_tree_oid_hex(void);
 const char *empty_blob_oid_hex(void);
 
+static inline int is_empty_blob_oid(const struct object_id *oid,
+				    const struct git_hash_algo *algop)
+{
+	return oideq(oid, algop->empty_blob);
+}
+
+static inline int is_empty_tree_oid(const struct object_id *oid,
+				    const struct git_hash_algo *algop)
+{
+	return oideq(oid, algop->empty_tree);
+}
+
 #endif
diff --git a/hash.h b/hash.h
index 84f2296cfb..39a0164be3 100644
--- a/hash.h
+++ b/hash.h
@@ -6,14 +6,4 @@
 
 #define the_hash_algo the_repository->hash_algo
 
-static inline int is_empty_blob_oid(const struct object_id *oid)
-{
-	return oideq(oid, the_hash_algo->empty_blob);
-}
-
-static inline int is_empty_tree_oid(const struct object_id *oid)
-{
-	return oideq(oid, the_hash_algo->empty_tree);
-}
-
 #endif
diff --git a/read-cache.c b/read-cache.c
index 836f1db721..085b22faf3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -337,7 +337,7 @@ static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
 
 	/* Racily smudged entry? */
 	if (!ce->ce_stat_data.sd_size) {
-		if (!is_empty_blob_oid(&ce->oid))
+		if (!is_empty_blob_oid(&ce->oid, the_repository->hash_algo))
 			changed |= DATA_CHANGED;
 	}
 
-- 
2.45.2.457.g8d94cfb545.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 40 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.