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

[PATCH 6/6] object-file: inline empty tree and blob literals

From
Jeff King <peff@peff.net>
Date
Nov 18, 2024, 09:55 UTC
Message-ID
<20241118095522.GF3992317@coredump.intra.peff.net>
In-Reply-To
<20241118095423.GA3990835@coredump.intra.peff.net>

We define macros with the bytes of the empty trees and blobs for sha1 and sha256. But since e1ccd7e2b1 (sha1_file: only expose empty object constants through git_hash_algo, 2018-05-02), those are used only for initializing the git_hash_algo entries. Any other code using the macros directly would be suspicious, since a hash_algo pointer is the level of indirection we use to make everything work with both sha1 and sha256.

So let's future proof against code doing the wrong thing by dropping the macros entirely and just initializing the structs directly.

Signed-off-by: Jeff King <peff@peff.net>
---
 object-file.c | 47 ++++++++++++++++++++---------------------------
 1 file changed, 20 insertions(+), 27 deletions(-)
diff --git a/object-file.c b/object-file.c
index ec62e5fb3b..891eaa2b4b 100644
--- a/object-file.c
+++ b/object-file.c
@@ -44,47 +44,40 @@
 /* The maximum size for an object header. */
 #define MAX_HEADER_LEN 32
 
-
-#define EMPTY_TREE_SHA1_BIN_LITERAL { \
-	0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60, \
-	0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04  \
-}
-#define EMPTY_TREE_SHA256_BIN_LITERAL { \
-	0x6e, 0xf1, 0x9b, 0x41, 0x22, 0x5c, 0x53, 0x69, 0xf1, 0xc1, \
-	0x04, 0xd4, 0x5d, 0x8d, 0x85, 0xef, 0xa9, 0xb0, 0x57, 0xb5, \
-	0x3b, 0x14, 0xb4, 0xb9, 0xb9, 0x39, 0xdd, 0x74, 0xde, 0xcc, \
-	0x53, 0x21 \
-}
-
-#define EMPTY_BLOB_SHA1_BIN_LITERAL { \
-	0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b, \
-	0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91  \
-}
-#define EMPTY_BLOB_SHA256_BIN_LITERAL { \
-	0x47, 0x3a, 0x0f, 0x4c, 0x3b, 0xe8, 0xa9, 0x36, 0x81, 0xa2, \
-	0x67, 0xe3, 0xb1, 0xe9, 0xa7, 0xdc, 0xda, 0x11, 0x85, 0x43, \
-	0x6f, 0xe1, 0x41, 0xf7, 0x74, 0x91, 0x20, 0xa3, 0x03, 0x72, \
-	0x18, 0x13 \
-}
-
 static const struct object_id empty_tree_oid = {
-	.hash = EMPTY_TREE_SHA1_BIN_LITERAL,
+	.hash = {
+		0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
+		0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04
+	},
 	.algo = GIT_HASH_SHA1,
 };
 static const struct object_id empty_blob_oid = {
-	.hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
+	.hash = {
+		0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
+		0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91
+	},
 	.algo = GIT_HASH_SHA1,
 };
 static const struct object_id null_oid_sha1 = {
 	.hash = {0},
 	.algo = GIT_HASH_SHA1,
 };
 static const struct object_id empty_tree_oid_sha256 = {
-	.hash = EMPTY_TREE_SHA256_BIN_LITERAL,
+	.hash = {
+		0x6e, 0xf1, 0x9b, 0x41, 0x22, 0x5c, 0x53, 0x69, 0xf1, 0xc1,
+		0x04, 0xd4, 0x5d, 0x8d, 0x85, 0xef, 0xa9, 0xb0, 0x57, 0xb5,
+		0x3b, 0x14, 0xb4, 0xb9, 0xb9, 0x39, 0xdd, 0x74, 0xde, 0xcc,
+		0x53, 0x21
+	},
 	.algo = GIT_HASH_SHA256,
 };
 static const struct object_id empty_blob_oid_sha256 = {
-	.hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
+	.hash = {
+		0x47, 0x3a, 0x0f, 0x4c, 0x3b, 0xe8, 0xa9, 0x36, 0x81, 0xa2,
+		0x67, 0xe3, 0xb1, 0xe9, 0xa7, 0xdc, 0xda, 0x11, 0x85, 0x43,
+		0x6f, 0xe1, 0x41, 0xf7, 0x74, 0x91, 0x20, 0xa3, 0x03, 0x72,
+		0x18, 0x13
+	},
 	.algo = GIT_HASH_SHA256,
 };
 static const struct object_id null_oid_sha256 = {
-- 
2.47.0.547.g778689293a
Previous: Jeff KingNext: Patrick Steinhardt
Message 23 of 25 in “-Wunterminated-string-initialization warning with GCC 15 in object-file.c”
  1. Sam JamesNov 17, 2024
  2. Jeff KingNov 17, 2024
  3. 1/5 object-file: prefer array-of-bytes initializer for hash literalsJeff King, Nov 17, 2024
  4. René ScharfeNov 17, 2024
  5. Jeff KingNov 18, 2024
  6. 2/5 object-file: drop confusing oid initializer of empty_tree structJeff King, Nov 17, 2024
  7. 3/5 object-file: move empty_tree struct into find_cached_object()Jeff King, Nov 17, 2024
  8. Patrick SteinhardtNov 18, 2024
  9. Jeff KingNov 18, 2024
  10. 4/5 object-file: drop oid field from find_cached_object() return valueJeff King, Nov 17, 2024
  11. 5/5 object-file: inline empty tree and blob literalsJeff King, Nov 17, 2024
  12. Patrick SteinhardtNov 18, 2024
  13. brian m. carlsonNov 17, 2024
  14. Jeff KingNov 18, 2024
  15. Sam JamesNov 18, 2024
  16. Patrick SteinhardtNov 18, 2024
  17. 0/6 -Wunterminated-string-initialization warning + cleanupsJeff King, Nov 18, 2024
  18. 1/6 object-file: prefer array-of-bytes initializer for hash literalsJeff King, Nov 18, 2024
  19. 2/6 object-file: drop confusing oid initializer of empty_tree structJeff King, Nov 18, 2024
  20. 3/6 object-file: move empty_tree struct into find_cached_object()Jeff King, Nov 18, 2024
  21. 4/6 object-file: drop oid field from find_cached_object() return valueJeff King, Nov 18, 2024
  22. 5/6 object-file: treat cached_object values as constJeff King, Nov 18, 2024
  23. 6/6 object-file: inline empty tree and blob literalsJeff King, Nov 18, 2024
  24. Patrick SteinhardtNov 18, 2024
  25. Junio C HamanoNov 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.