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

[PATCH v2 10/35] notes: make hash size independent

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Feb 19, 2019, 00:05 UTC
Message-ID
<20190219000526.476553-11-sandals@crustytoothpaste.net>
In-Reply-To
<20190219000526.476553-1-sandals@crustytoothpaste.net>

Switch out various uses of the GIT_SHA1_* constants with GIT_MAX_* constants for allocations and the_hash_algo for general parsing. Update a comment to no longer be SHA-1 specific.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 notes.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/notes.c b/notes.c
index 7f7cc4d511..5457bd70c6 100644
--- a/notes.c
+++ b/notes.c
@@ -67,8 +67,9 @@ struct non_note {
 
 #define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
 
-#define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
-#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
+#define KEY_INDEX (the_hash_algo->rawsz - 1)
+#define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
+#define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
 	(memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
 
@@ -198,7 +199,7 @@ static void note_tree_remove(struct notes_tree *t,
 		struct leaf_node *entry)
 {
 	struct leaf_node *l;
-	struct int_node *parent_stack[GIT_SHA1_RAWSZ];
+	struct int_node *parent_stack[GIT_MAX_RAWSZ];
 	unsigned char i, j;
 	void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
 
@@ -394,6 +395,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 	void *buf;
 	struct tree_desc desc;
 	struct name_entry entry;
+	const unsigned hashsz = the_hash_algo->rawsz;
 
 	buf = fill_tree_descriptor(&desc, &subtree->val_oid);
 	if (!buf)
@@ -401,7 +403,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		     oid_to_hex(&subtree->val_oid));
 
 	prefix_len = subtree->key_oid.hash[KEY_INDEX];
-	if (prefix_len >= GIT_SHA1_RAWSZ)
+	if (prefix_len >= hashsz)
 		BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
 	if (prefix_len * 2 < n)
 		BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
@@ -411,7 +413,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		struct leaf_node *l;
 		size_t path_len = strlen(entry.path);
 
-		if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) {
+		if (path_len == 2 * (hashsz - prefix_len)) {
 			/* This is potentially the remainder of the SHA-1 */
 
 			if (!S_ISREG(entry.mode))
@@ -419,7 +421,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 				goto handle_non_note;
 
 			if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
-					 GIT_SHA1_RAWSZ - prefix_len))
+					 hashsz - prefix_len))
 				goto handle_non_note; /* entry.path is not a SHA1 */
 
 			type = PTR_TYPE_NOTE;
@@ -439,7 +441,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 			 * except for the last byte, where we write
 			 * the length:
 			 */
-			memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1);
+			memset(object_oid.hash + len, 0, hashsz - len - 1);
 			object_oid.hash[KEY_INDEX] = (unsigned char)len;
 
 			type = PTR_TYPE_SUBTREE;
@@ -527,15 +529,15 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
 	return fanout + 1;
 }
 
-/* hex SHA1 + 19 * '/' + NUL */
-#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
+/* hex oid + '/' between each pair of hex digits + NUL */
+#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
 
 static void construct_path_with_fanout(const unsigned char *sha1,
 		unsigned char fanout, char *path)
 {
 	unsigned int i = 0, j = 0;
 	const char *hex_sha1 = sha1_to_hex(sha1);
-	assert(fanout < GIT_SHA1_RAWSZ);
+	assert(fanout < the_hash_algo->rawsz);
 	while (fanout) {
 		path[i++] = hex_sha1[j++];
 		path[i++] = hex_sha1[j++];
@@ -637,10 +639,10 @@ static inline int matches_tree_write_stack(struct tree_write_stack *tws,
 
 static void write_tree_entry(struct strbuf *buf, unsigned int mode,
 		const char *path, unsigned int path_len, const
-		unsigned char *sha1)
+		unsigned char *hash)
 {
 	strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
-	strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
+	strbuf_add(buf, hash, the_hash_algo->rawsz);
 }
 
 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
@@ -652,7 +654,7 @@ static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
 	n = (struct tree_write_stack *)
 		xmalloc(sizeof(struct tree_write_stack));
 	n->next = NULL;
-	strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
+	strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
 	n->path[0] = n->path[1] = '\0';
 	tws->next = n;
 	tws->path[0] = path[0];
@@ -757,7 +759,7 @@ static int write_each_note(const struct object_id *object_oid,
 		note_path[note_path_len] = '\0';
 		mode = 040000;
 	}
-	assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
+	assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
 
 	/* Weave non-note entries into note entries */
 	return  write_each_non_note_until(note_path, d) ||
@@ -1137,7 +1139,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
 
 	/* Prepare for traversal of current notes tree */
 	root.next = NULL; /* last forward entry in list is grounded */
-	strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
+	strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
 	root.path[0] = root.path[1] = '\0';
 	cb_data.root = &root;
 	cb_data.next_non_note = t->first_non_note;
Previous: brian m. carlson
Message 36 of 36 in “Hash function transition part 16”
  1. 00/35 Hash function transition part 16brian m. carlson, Feb 19, 2019
  2. 01/35 t/lib-submodule-update: use appropriate length constantbrian m. carlson, Feb 19, 2019
  3. 02/35 khash: move oid hash table definitionbrian m. carlson, Feb 19, 2019
  4. 03/35 pack-bitmap: make bitmap header handling hash agnosticbrian m. carlson, Feb 19, 2019
  5. 06/35 pack-bitmap: switch hard-coded constants to the_hash_algobrian m. carlson, Feb 19, 2019
  6. 05/35 pack-bitmap: replace sha1_to_hexbrian m. carlson, Feb 19, 2019
  7. 07/35 pack-bitmap: switch hash tables to use struct object_idbrian m. carlson, Feb 19, 2019
  8. 09/35 notes-merge: switch to use the_hash_algobrian m. carlson, Feb 19, 2019
  9. 08/35 submodule: avoid hard-coded constantsbrian m. carlson, Feb 19, 2019
  10. 11/35 notes: replace sha1_to_hexbrian m. carlson, Feb 19, 2019
  11. 14/35 fast-import: make hash-size independentbrian m. carlson, Feb 19, 2019
  12. 17/35 builtin/pull: make hash-size independentbrian m. carlson, Feb 19, 2019
  13. 15/35 fast-import: replace sha1_to_hexbrian m. carlson, Feb 19, 2019
  14. 12/35 object-store: rename and expand packed_git's sha1 memberbrian m. carlson, Feb 19, 2019
  15. 19/35 http-backend: allow 64-character hex namesbrian m. carlson, Feb 19, 2019
  16. 18/35 http-push: convert to use the_hash_algobrian m. carlson, Feb 19, 2019
  17. 21/35 http-walker: replace sha1_to_hexbrian m. carlson, Feb 19, 2019
  18. 22/35 http: replace hard-coded constant with the_hash_algobrian m. carlson, Feb 19, 2019
  19. 20/35 http-push: remove remaining uses of sha1_to_hexbrian m. carlson, Feb 19, 2019
  20. 23/35 http: compute hash of downloaded objects using the_hash_algobrian m. carlson, Feb 19, 2019
  21. 24/35 http: replace sha1_to_hexbrian m. carlson, Feb 19, 2019
  22. 25/35 remote-curl: make hash size independentbrian m. carlson, Feb 19, 2019
  23. 26/35 hash: add a function to lookup hash algorithm by lengthbrian m. carlson, Feb 19, 2019
  24. 30/35 refspec: make hash size independentbrian m. carlson, Feb 19, 2019
  25. 27/35 get-tar-commit-id: parse comment recordbrian m. carlson, Feb 19, 2019
  26. 29/35 archive: convert struct archiver_args to object_idbrian m. carlson, Feb 19, 2019
  27. 34/35 Git.pm: make hash size independentbrian m. carlson, Feb 19, 2019
  28. 33/35 read-cache: read data in a hash-independent waybrian m. carlson, Feb 19, 2019
  29. 31/35 builtin/difftool: use parse_oid_hexbrian m. carlson, Feb 19, 2019
  30. 35/35 gitweb: make hash size independentbrian m. carlson, Feb 19, 2019
  31. 32/35 dir: make untracked cache extension hash size independentbrian m. carlson, Feb 19, 2019
  32. 16/35 builtin/am: make hash size independentbrian m. carlson, Feb 19, 2019
  33. 28/35 builtin/get-tar-commit-id: make hash size independentbrian m. carlson, Feb 19, 2019
  34. 13/35 builtin/name-rev: make hash-size independentbrian m. carlson, Feb 19, 2019
  35. 04/35 pack-bitmap: convert struct stored_bitmap to object_idbrian m. carlson, Feb 19, 2019
  36. 10/35 notes: make hash size independentbrian m. carlson, Feb 19, 2019

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.