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

[PATCH v4 09/12] commit-graph: convert to using the_hash_algo

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Oct 25, 2018, 02:40 UTC
Message-ID
<20181025024005.154208-10-sandals@crustytoothpaste.net>
In-Reply-To
<20181025024005.154208-1-sandals@crustytoothpaste.net>

Instead of using hard-coded constants for object sizes, use the_hash_algo to look them up. In addition, use a function call to look up the object ID version and produce the correct value. For now, we use version 1, which means to use the default algorithm used in the rest of the repository.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 commit-graph.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/commit-graph.c b/commit-graph.c
index 40c855f185..6763d19288 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -23,16 +23,11 @@
 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
 
-#define GRAPH_DATA_WIDTH 36
+#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
 
 #define GRAPH_VERSION_1 0x1
 #define GRAPH_VERSION GRAPH_VERSION_1
 
-#define GRAPH_OID_VERSION_SHA1 1
-#define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
-#define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
-#define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
-
 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
 #define GRAPH_PARENT_MISSING 0x7fffffff
 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
@@ -44,13 +39,18 @@
 #define GRAPH_FANOUT_SIZE (4 * 256)
 #define GRAPH_CHUNKLOOKUP_WIDTH 12
 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
-			+ GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
+			+ GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
 
 char *get_commit_graph_filename(const char *obj_dir)
 {
 	return xstrfmt("%s/info/commit-graph", obj_dir);
 }
 
+static uint8_t oid_version(void)
+{
+	return 1;
+}
+
 static struct commit_graph *alloc_commit_graph(void)
 {
 	struct commit_graph *g = xcalloc(1, sizeof(*g));
@@ -125,15 +125,15 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
 	}
 
 	hash_version = *(unsigned char*)(data + 5);
-	if (hash_version != GRAPH_OID_VERSION) {
+	if (hash_version != oid_version()) {
 		error(_("hash version %X does not match version %X"),
-		      hash_version, GRAPH_OID_VERSION);
+		      hash_version, oid_version());
 		goto cleanup_fail;
 	}
 
 	graph = alloc_commit_graph();
 
-	graph->hash_len = GRAPH_OID_LEN;
+	graph->hash_len = the_hash_algo->rawsz;
 	graph->num_chunks = *(unsigned char*)(data + 6);
 	graph->graph_fd = fd;
 	graph->data = graph_map;
@@ -149,7 +149,7 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
 
 		chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
 
-		if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
+		if (chunk_offset > graph_size - the_hash_algo->rawsz) {
 			error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
 			      (uint32_t)chunk_offset);
 			goto cleanup_fail;
@@ -764,6 +764,7 @@ void write_commit_graph(const char *obj_dir,
 	int num_extra_edges;
 	struct commit_list *parent;
 	struct progress *progress = NULL;
+	const unsigned hashsz = the_hash_algo->rawsz;
 
 	if (!commit_graph_compatible(the_repository))
 		return;
@@ -909,7 +910,7 @@ void write_commit_graph(const char *obj_dir,
 	hashwrite_be32(f, GRAPH_SIGNATURE);
 
 	hashwrite_u8(f, GRAPH_VERSION);
-	hashwrite_u8(f, GRAPH_OID_VERSION);
+	hashwrite_u8(f, oid_version());
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, 0); /* unused padding byte */
 
@@ -924,8 +925,8 @@ void write_commit_graph(const char *obj_dir,
 
 	chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
 	chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
-	chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
-	chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
+	chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
+	chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
 	chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
 
 	for (i = 0; i <= num_chunks; i++) {
@@ -938,8 +939,8 @@ void write_commit_graph(const char *obj_dir,
 	}
 
 	write_graph_chunk_fanout(f, commits.list, commits.nr);
-	write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
-	write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
+	write_graph_chunk_oids(f, hashsz, commits.list, commits.nr);
+	write_graph_chunk_data(f, hashsz, commits.list, commits.nr);
 	write_graph_chunk_large_edges(f, commits.list, commits.nr);
 
 	close_commit_graph(the_repository);
Previous: brian m. carlsonNext: brian m. carlson
Message 10 of 58 in “Base SHA-256 implementation”
  1. 00/12 Base SHA-256 implementationbrian m. carlson, Oct 25, 2018
  2. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Oct 25, 2018
  3. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Oct 25, 2018
  4. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Oct 25, 2018
  5. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Oct 25, 2018
  6. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Oct 25, 2018
  7. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Oct 25, 2018
  8. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Oct 25, 2018
  9. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Oct 25, 2018
  10. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Oct 25, 2018
  11. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Oct 25, 2018
  12. Carlo ArenasOct 25, 2018
  13. brian m. carlsonOct 28, 2018
  14. Junio C HamanoOct 29, 2018
  15. brian m. carlsonOct 31, 2018
  16. Junio C HamanoNov 1, 2018
  17. Christian CouderOct 27, 2018
  18. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Oct 25, 2018
  19. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Oct 25, 2018
  20. 00/12 Base SHA-256 implementationbrian m. carlson, Nov 4, 2018
  21. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Nov 4, 2018
  22. Ævar Arnfjörð BjarmasonNov 5, 2018
  23. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Nov 4, 2018
  24. Derrick StoleeNov 13, 2018
  25. Duy NguyenNov 13, 2018
  26. brian m. carlsonNov 14, 2018
  27. Ramsay JonesNov 14, 2018
  28. Ramsay JonesNov 14, 2018
  29. Jeff KingNov 14, 2018
  30. brian m. carlsonNov 14, 2018
  31. Ramsay JonesNov 14, 2018
  32. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Nov 4, 2018
  33. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Nov 4, 2018
  34. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Nov 4, 2018
  35. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Nov 4, 2018
  36. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Nov 4, 2018
  37. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Nov 4, 2018
  38. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Nov 4, 2018
  39. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Nov 4, 2018
  40. Ævar Arnfjörð BjarmasonNov 5, 2018
  41. brian m. carlsonNov 7, 2018
  42. Ævar Arnfjörð BjarmasonNov 10, 2018
  43. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Nov 4, 2018
  44. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Nov 4, 2018
  45. Junio C HamanoNov 5, 2018
  46. 00/12 Base SHA-256 implementationbrian m. carlson, Nov 14, 2018
  47. 01/12 sha1-file: rename algorithm to "sha1"brian m. carlson, Nov 14, 2018
  48. 02/12 sha1-file: provide functions to look up hash algorithmsbrian m. carlson, Nov 14, 2018
  49. 03/12 hex: introduce functions to print arbitrary hashesbrian m. carlson, Nov 14, 2018
  50. 04/12 cache: make hashcmp and hasheq work with larger hashesbrian m. carlson, Nov 14, 2018
  51. 06/12 t: make the sha1 test-tool helper genericbrian m. carlson, Nov 14, 2018
  52. 05/12 t: add basic tests for our SHA-1 implementationbrian m. carlson, Nov 14, 2018
  53. 07/12 sha1-file: add a constant for hash block sizebrian m. carlson, Nov 14, 2018
  54. 08/12 t/helper: add a test helper to compute hash speedbrian m. carlson, Nov 14, 2018
  55. 09/12 commit-graph: convert to using the_hash_algobrian m. carlson, Nov 14, 2018
  56. 10/12 Add a base implementation of SHA-256 supportbrian m. carlson, Nov 14, 2018
  57. 11/12 sha256: add an SHA-256 implementation using libgcryptbrian m. carlson, Nov 14, 2018
  58. 12/12 hash: add an SHA-256 implementation using OpenSSLbrian m. carlson, Nov 14, 2018

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.