notes: convert internal parts to struct object_id

Convert several portions of the internals of the code to struct object_id. Introduce two macros to denote the different constants in the code: KEY_INDEX for the last byte of the object ID, and FANOUT_PATH_SEPARATORS for the number of possible path separators (on Unix, "/"). While these constants are both 19 (one less than the number of bytes in the hash), distinguish them to make the code more understandable, and define them logically based on their intended purpose. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 30, 2017 at 10:30 UTC 89c149f55b93e052e46e46ebc4d7e76b0b25a90c
1 file changed +33 -31
notes.c
+33 -31
@@ -65,8 +65,10 @@ struct non_note {
65
66 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
67
68 +#define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
69 +#define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
70 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
69 - (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
71 + (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
72
73 struct notes_tree default_notes_tree;
74
@@ -194,7 +196,7 @@ static void note_tree_remove(struct notes_tree *t,
196 struct leaf_node *entry)
197 {
198 struct leaf_node *l;
197 - struct int_node *parent_stack[20];
199 + struct int_node *parent_stack[GIT_SHA1_RAWSZ];
200 unsigned char i, j;
201 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
202
@@ -341,21 +343,21 @@ static void note_tree_free(struct int_node *tree)
343 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
344 * Pads sha1 with NULs up to sha1_len (not included in returned length).
345 */
344 -static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
345 - unsigned char *sha1, unsigned int sha1_len)
346 +static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
347 + unsigned char *oid, unsigned int oid_len)
348 {
349 unsigned int i, len = hex_len >> 1;
348 - if (hex_len % 2 != 0 || len > sha1_len)
350 + if (hex_len % 2 != 0 || len > oid_len)
351 return -1;
352 for (i = 0; i < len; i++) {
353 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
354 if (val & ~0xff)
355 return -1;
354 - *sha1++ = val;
356 + *oid++ = val;
357 hex += 2;
358 }
357 - for (; i < sha1_len; i++)
358 - *sha1++ = 0;
359 + for (; i < oid_len; i++)
360 + *oid++ = 0;
361 return len;
362 }
363
@@ -413,7 +415,7 @@ static void add_non_note(struct notes_tree *t, char *path,
415 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
416 struct int_node *node, unsigned int n)
417 {
416 - unsigned char object_sha1[20];
418 + struct object_id object_oid;
419 unsigned int prefix_len;
420 void *buf;
421 struct tree_desc desc;
@@ -427,13 +429,13 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
429 die("Could not read %s for notes-index",
430 oid_to_hex(&subtree->val_oid));
431
430 - prefix_len = subtree->key_oid.hash[19];
432 + prefix_len = subtree->key_oid.hash[KEY_INDEX];
433 assert(prefix_len * 2 >= n);
432 - memcpy(object_sha1, subtree->key_oid.hash, prefix_len);
434 + memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
435 while (tree_entry(&desc, &entry)) {
436 path_len = strlen(entry.path);
435 - len = get_sha1_hex_segment(entry.path, path_len,
436 - object_sha1 + prefix_len, 20 - prefix_len);
437 + len = get_oid_hex_segment(entry.path, path_len,
438 + object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len);
439 if (len < 0)
440 goto handle_non_note; /* entry.path is not a SHA1 */
441 len += prefix_len;
@@ -443,16 +445,16 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
445 * If object SHA1 is incomplete (len < 20), and current
446 * component consists of 2 hex chars, assume note subtree
447 */
446 - if (len <= 20) {
448 + if (len <= GIT_SHA1_RAWSZ) {
449 type = PTR_TYPE_NOTE;
450 l = (struct leaf_node *)
451 xcalloc(1, sizeof(struct leaf_node));
450 - hashcpy(l->key_oid.hash, object_sha1);
452 + oidcpy(&l->key_oid, &object_oid);
453 oidcpy(&l->val_oid, entry.oid);
452 - if (len < 20) {
454 + if (len < GIT_SHA1_RAWSZ) {
455 if (!S_ISDIR(entry.mode) || path_len != 2)
456 goto handle_non_note; /* not subtree */
455 - l->key_oid.hash[19] = (unsigned char) len;
457 + l->key_oid.hash[KEY_INDEX] = (unsigned char) len;
458 type = PTR_TYPE_SUBTREE;
459 }
460 if (note_tree_insert(t, node, n, l, type,
@@ -542,14 +544,14 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
544 }
545
546 /* hex SHA1 + 19 * '/' + NUL */
545 -#define FANOUT_PATH_MAX 40 + 19 + 1
547 +#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
548
549 static void construct_path_with_fanout(const unsigned char *sha1,
550 unsigned char fanout, char *path)
551 {
552 unsigned int i = 0, j = 0;
553 const char *hex_sha1 = sha1_to_hex(sha1);
552 - assert(fanout < 20);
554 + assert(fanout < GIT_SHA1_RAWSZ);
555 while (fanout) {
556 path[i++] = hex_sha1[j++];
557 path[i++] = hex_sha1[j++];
@@ -599,7 +601,7 @@ redo:
601 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
602 /* invoke callback with subtree */
603 unsigned int path_len =
602 - l->key_oid.hash[19] * 2 + fanout;
604 + l->key_oid.hash[KEY_INDEX] * 2 + fanout;
605 assert(path_len < FANOUT_PATH_MAX - 1);
606 construct_path_with_fanout(l->key_oid.hash,
607 fanout,
@@ -654,7 +656,7 @@ static void write_tree_entry(struct strbuf *buf, unsigned int mode,
656 unsigned char *sha1)
657 {
658 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
657 - strbuf_add(buf, sha1, 20);
659 + strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
660 }
661
662 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
@@ -666,7 +668,7 @@ static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
668 n = (struct tree_write_stack *)
669 xmalloc(sizeof(struct tree_write_stack));
670 n->next = NULL;
669 - strbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */
671 + strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
672 n->path[0] = n->path[1] = '\0';
673 tws->next = n;
674 tws->path[0] = path[0];
@@ -677,18 +679,18 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
679 {
680 int ret;
681 struct tree_write_stack *n = tws->next;
680 - unsigned char s[20];
682 + struct object_id s;
683 if (n) {
684 ret = tree_write_stack_finish_subtree(n);
685 if (ret)
686 return ret;
685 - ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);
687 + ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
688 if (ret)
689 return ret;
690 strbuf_release(&n->buf);
691 free(n);
692 tws->next = NULL;
691 - write_tree_entry(&tws->buf, 040000, tws->path, 2, s);
693 + write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
694 tws->path[0] = tws->path[1] = '\0';
695 }
696 return 0;
@@ -771,7 +773,7 @@ static int write_each_note(const unsigned char *object_sha1,
773 note_path[note_path_len] = '\0';
774 mode = 040000;
775 }
774 - assert(note_path_len <= 40 + 19);
776 + assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
777
778 /* Weave non-note entries into note entries */
779 return write_each_non_note_until(note_path, d) ||
@@ -946,8 +948,8 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
948 if (has_glob_specials(glob)) {
949 for_each_glob_ref(string_list_add_one_ref, glob, list);
950 } else {
949 - unsigned char sha1[20];
950 - if (get_sha1(glob, sha1))
951 + struct object_id oid;
952 + if (get_oid(glob, &oid))
953 warning("notes ref %s is invalid", glob);
954 if (!unsorted_string_list_has_string(list, glob))
955 string_list_append(list, glob);
@@ -1150,7 +1152,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)
1152
1153 /* Prepare for traversal of current notes tree */
1154 root.next = NULL; /* last forward entry in list is grounded */
1153 - strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
1155 + strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
1156 root.path[0] = root.path[1] = '\0';
1157 cb_data.root = &root;
1158 cb_data.next_non_note = t->first_non_note;
@@ -1315,9 +1317,9 @@ void expand_notes_ref(struct strbuf *sb)
1317
1318 void expand_loose_notes_ref(struct strbuf *sb)
1319 {
1318 - unsigned char object[20];
1320 + struct object_id object;
1321
1320 - if (get_sha1(sb->buf, object)) {
1322 + if (get_oid(sb->buf, &object)) {
1323 /* fallback to expand_notes_ref */
1324 expand_notes_ref(sb);
1325 }