notes: make hash size independent
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 19, 2019 at 00:05 UTC
dd43745131ca20f728746c37ffbbf1f8946052d1
1 file changed
+17
-15
notes.c
+17
-15
@@ -67,8 +67,9 @@ struct non_note {
67
68
#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
69
70
-#define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
71
-#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
70
+#define KEY_INDEX (the_hash_algo->rawsz - 1)
71
+#define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
72
+#define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
73
#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
74
(memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
75
@@ -198,7 +199,7 @@ static void note_tree_remove(struct notes_tree *t,
199
struct leaf_node *entry)
200
{
201
struct leaf_node *l;
201
- struct int_node *parent_stack[GIT_SHA1_RAWSZ];
202
+ struct int_node *parent_stack[GIT_MAX_RAWSZ];
203
unsigned char i, j;
204
void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
205
@@ -394,6 +395,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
395
void *buf;
396
struct tree_desc desc;
397
struct name_entry entry;
398
+ const unsigned hashsz = the_hash_algo->rawsz;
399
400
buf = fill_tree_descriptor(&desc, &subtree->val_oid);
401
if (!buf)
@@ -401,7 +403,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
403
oid_to_hex(&subtree->val_oid));
404
405
prefix_len = subtree->key_oid.hash[KEY_INDEX];
404
- if (prefix_len >= GIT_SHA1_RAWSZ)
406
+ if (prefix_len >= hashsz)
407
BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
408
if (prefix_len * 2 < n)
409
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,
413
struct leaf_node *l;
414
size_t path_len = strlen(entry.path);
415
414
- if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) {
416
+ if (path_len == 2 * (hashsz - prefix_len)) {
417
/* This is potentially the remainder of the SHA-1 */
418
419
if (!S_ISREG(entry.mode))
@@ -419,7 +421,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
421
goto handle_non_note;
422
423
if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
422
- GIT_SHA1_RAWSZ - prefix_len))
424
+ hashsz - prefix_len))
425
goto handle_non_note; /* entry.path is not a SHA1 */
426
427
type = PTR_TYPE_NOTE;
@@ -439,7 +441,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
441
* except for the last byte, where we write
442
* the length:
443
*/
442
- memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1);
444
+ memset(object_oid.hash + len, 0, hashsz - len - 1);
445
object_oid.hash[KEY_INDEX] = (unsigned char)len;
446
447
type = PTR_TYPE_SUBTREE;
@@ -527,15 +529,15 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
529
return fanout + 1;
530
}
531
530
-/* hex SHA1 + 19 * '/' + NUL */
531
-#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
532
+/* hex oid + '/' between each pair of hex digits + NUL */
533
+#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
534
535
static void construct_path_with_fanout(const unsigned char *sha1,
536
unsigned char fanout, char *path)
537
{
538
unsigned int i = 0, j = 0;
539
const char *hex_sha1 = sha1_to_hex(sha1);
538
- assert(fanout < GIT_SHA1_RAWSZ);
540
+ assert(fanout < the_hash_algo->rawsz);
541
while (fanout) {
542
path[i++] = hex_sha1[j++];
543
path[i++] = hex_sha1[j++];
@@ -637,10 +639,10 @@ static inline int matches_tree_write_stack(struct tree_write_stack *tws,
639
640
static void write_tree_entry(struct strbuf *buf, unsigned int mode,
641
const char *path, unsigned int path_len, const
640
- unsigned char *sha1)
642
+ unsigned char *hash)
643
{
644
strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
643
- strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
645
+ strbuf_add(buf, hash, the_hash_algo->rawsz);
646
}
647
648
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,
654
n = (struct tree_write_stack *)
655
xmalloc(sizeof(struct tree_write_stack));
656
n->next = NULL;
655
- strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
657
+ strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
658
n->path[0] = n->path[1] = '\0';
659
tws->next = n;
660
tws->path[0] = path[0];
@@ -757,7 +759,7 @@ static int write_each_note(const struct object_id *object_oid,
759
note_path[note_path_len] = '\0';
760
mode = 040000;
761
}
760
- assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
762
+ assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
763
764
/* Weave non-note entries into note entries */
765
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)
1139
1140
/* Prepare for traversal of current notes tree */
1141
root.next = NULL; /* last forward entry in list is grounded */
1140
- strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
1142
+ strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
1143
root.path[0] = root.path[1] = '\0';
1144
cb_data.root = &root;
1145
cb_data.next_non_note = t->first_non_note;