Merge branch 'gk/tracing-optimization'
[gitweb.git] / notes.c
diff --git a/notes.c b/notes.c
index df4660fe62ae64356950ca51c1acead0398f93f1..c7f21fae441067f7250caf0b4186fcbcbc7630bb 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "notes.h"
 #include "blob.h"
 #include "tree.h"
@@ -35,8 +36,8 @@ struct int_node {
  * subtree.
  */
 struct leaf_node {
-       unsigned char key_sha1[20];
-       unsigned char val_sha1[20];
+       struct object_id key_oid;
+       struct object_id val_oid;
 };
 
 /*
@@ -51,7 +52,7 @@ struct non_note {
        struct non_note *next; /* grounded (last->next == NULL) */
        char *path;
        unsigned int mode;
-       unsigned char sha1[20];
+       struct object_id oid;
 };
 
 #define PTR_TYPE_NULL     0
@@ -63,10 +64,12 @@ struct non_note {
 #define CLR_PTR_TYPE(ptr)       ((void *) ((uintptr_t) (ptr) & ~3))
 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
 
-#define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
+#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 SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
-       (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
+       (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
 
 struct notes_tree default_notes_tree;
 
@@ -100,7 +103,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
 
        if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
                l = (struct leaf_node *) CLR_PTR_TYPE(p);
-               if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
+               if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
                        /* unpack tree and resume search */
                        (*tree)->a[0] = NULL;
                        load_subtree(t, l, *tree, *n);
@@ -118,7 +121,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
                return note_tree_search(t, tree, n, key_sha1);
        case PTR_TYPE_SUBTREE:
                l = (struct leaf_node *) CLR_PTR_TYPE(p);
-               if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
+               if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
                        /* unpack tree and resume search */
                        (*tree)->a[i] = NULL;
                        load_subtree(t, l, *tree, *n);
@@ -143,7 +146,7 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
        void **p = note_tree_search(t, &tree, &n, key_sha1);
        if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
                struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
-               if (!hashcmp(key_sha1, l->key_sha1))
+               if (!hashcmp(key_sha1, l->key_oid.hash))
                        return l;
        }
        return NULL;
@@ -153,8 +156,8 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
  * How to consolidate an int_node:
  * If there are > 1 non-NULL entries, give up and return non-zero.
  * Otherwise replace the int_node at the given index in the given parent node
- * with the only entry (or a NULL entry if no entries) from the given tree,
- * and return 0.
+ * with the only NOTE entry (or a NULL entry if no entries) from the given
+ * tree, and return 0.
  */
 static int note_tree_consolidate(struct int_node *tree,
        struct int_node *parent, unsigned char index)
@@ -173,6 +176,8 @@ static int note_tree_consolidate(struct int_node *tree,
                }
        }
 
+       if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
+               return -2;
        /* replace tree with p in parent[index] */
        parent->a[index] = p;
        free(tree);
@@ -192,19 +197,19 @@ static void note_tree_remove(struct notes_tree *t,
                struct leaf_node *entry)
 {
        struct leaf_node *l;
-       struct int_node *parent_stack[20];
+       struct int_node *parent_stack[GIT_SHA1_RAWSZ];
        unsigned char i, j;
-       void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+       void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
 
        assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
        if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
                return; /* type mismatch, nothing to remove */
        l = (struct leaf_node *) CLR_PTR_TYPE(*p);
-       if (hashcmp(l->key_sha1, entry->key_sha1))
+       if (oidcmp(&l->key_oid, &entry->key_oid))
                return; /* key mismatch, nothing to remove */
 
        /* we have found a matching entry */
-       hashcpy(entry->val_sha1, l->val_sha1);
+       oidcpy(&entry->val_oid, &l->val_oid);
        free(l);
        *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
 
@@ -214,14 +219,14 @@ static void note_tree_remove(struct notes_tree *t,
        /* first, build stack of ancestors between root and current node */
        parent_stack[0] = t->root;
        for (i = 0; i < n; i++) {
-               j = GET_NIBBLE(i, entry->key_sha1);
+               j = GET_NIBBLE(i, entry->key_oid.hash);
                parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
        }
        assert(i == n && parent_stack[i] == tree);
        /* next, unwind stack until note_tree_consolidate() is done */
        while (i > 0 &&
               !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
-                                     GET_NIBBLE(i - 1, entry->key_sha1)))
+                                     GET_NIBBLE(i - 1, entry->key_oid.hash)))
                i--;
 }
 
@@ -244,7 +249,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
 {
        struct int_node *new_node;
        struct leaf_node *l;
-       void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
+       void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
        int ret = 0;
 
        assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
@@ -252,7 +257,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
        switch (GET_PTR_TYPE(*p)) {
        case PTR_TYPE_NULL:
                assert(!*p);
-               if (is_null_sha1(entry->val_sha1))
+               if (is_null_oid(&entry->val_oid))
                        free(entry);
                else
                        *p = SET_PTR_TYPE(entry, type);
@@ -260,22 +265,22 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
        case PTR_TYPE_NOTE:
                switch (type) {
                case PTR_TYPE_NOTE:
-                       if (!hashcmp(l->key_sha1, entry->key_sha1)) {
+                       if (!oidcmp(&l->key_oid, &entry->key_oid)) {
                                /* skip concatenation if l == entry */
-                               if (!hashcmp(l->val_sha1, entry->val_sha1))
+                               if (!oidcmp(&l->val_oid, &entry->val_oid))
                                        return 0;
 
-                               ret = combine_notes(l->val_sha1,
-                                                   entry->val_sha1);
-                               if (!ret && is_null_sha1(l->val_sha1))
+                               ret = combine_notes(l->val_oid.hash,
+                                                   entry->val_oid.hash);
+                               if (!ret && is_null_oid(&l->val_oid))
                                        note_tree_remove(t, tree, n, entry);
                                free(entry);
                                return ret;
                        }
                        break;
                case PTR_TYPE_SUBTREE:
-                       if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
-                                                   entry->key_sha1)) {
+                       if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
+                                                   entry->key_oid.hash)) {
                                /* unpack 'entry' */
                                load_subtree(t, entry, tree, n);
                                free(entry);
@@ -285,7 +290,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
                }
                break;
        case PTR_TYPE_SUBTREE:
-               if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
+               if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
                        /* unpack 'l' and restart insert */
                        *p = NULL;
                        load_subtree(t, l, tree, n);
@@ -299,7 +304,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
        /* non-matching leaf_node */
        assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
               GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
-       if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
+       if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
                free(entry);
                return 0;
        }
@@ -329,34 +334,6 @@ static void note_tree_free(struct int_node *tree)
        }
 }
 
-/*
- * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
- * - hex      - Partial SHA1 segment in ASCII hex format
- * - hex_len  - Length of above segment. Must be multiple of 2 between 0 and 40
- * - sha1     - Partial SHA1 value is written here
- * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
- * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
- * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
- * Pads sha1 with NULs up to sha1_len (not included in returned length).
- */
-static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
-               unsigned char *sha1, unsigned int sha1_len)
-{
-       unsigned int i, len = hex_len >> 1;
-       if (hex_len % 2 != 0 || len > sha1_len)
-               return -1;
-       for (i = 0; i < len; i++) {
-               unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
-               if (val & ~0xff)
-                       return -1;
-               *sha1++ = val;
-               hex += 2;
-       }
-       for (; i < sha1_len; i++)
-               *sha1++ = 0;
-       return len;
-}
-
 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
 {
        return strcmp(a->path, b->path);
@@ -371,7 +348,7 @@ static void add_non_note(struct notes_tree *t, char *path,
        n->next = NULL;
        n->path = path;
        n->mode = mode;
-       hashcpy(n->sha1, sha1);
+       hashcpy(n->oid.hash, sha1);
        t->prev_non_note = n;
 
        if (!t->first_non_note) {
@@ -397,7 +374,7 @@ static void add_non_note(struct notes_tree *t, char *path,
        if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
                assert(strcmp(p->path, n->path) == 0);
                p->mode = n->mode;
-               hashcpy(p->sha1, n->sha1);
+               oidcpy(&p->oid, &n->oid);
                free(n);
                t->prev_non_note = p;
                return;
@@ -411,81 +388,91 @@ static void add_non_note(struct notes_tree *t, char *path,
 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
                struct int_node *node, unsigned int n)
 {
-       unsigned char object_sha1[20];
-       unsigned int prefix_len;
+       struct object_id object_oid;
+       size_t prefix_len;
        void *buf;
        struct tree_desc desc;
        struct name_entry entry;
-       int len, path_len;
-       unsigned char type;
-       struct leaf_node *l;
 
-       buf = fill_tree_descriptor(&desc, subtree->val_sha1);
+       buf = fill_tree_descriptor(&desc, &subtree->val_oid);
        if (!buf)
                die("Could not read %s for notes-index",
-                    sha1_to_hex(subtree->val_sha1));
-
-       prefix_len = subtree->key_sha1[19];
-       assert(prefix_len * 2 >= n);
-       memcpy(object_sha1, subtree->key_sha1, prefix_len);
+                    oid_to_hex(&subtree->val_oid));
+
+       prefix_len = subtree->key_oid.hash[KEY_INDEX];
+       if (prefix_len >= GIT_SHA1_RAWSZ)
+               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);
+       memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
        while (tree_entry(&desc, &entry)) {
-               path_len = strlen(entry.path);
-               len = get_sha1_hex_segment(entry.path, path_len,
-                               object_sha1 + prefix_len, 20 - prefix_len);
-               if (len < 0)
-                       goto handle_non_note; /* entry.path is not a SHA1 */
-               len += prefix_len;
+               unsigned char type;
+               struct leaf_node *l;
+               size_t path_len = strlen(entry.path);
+
+               if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) {
+                       /* This is potentially the remainder of the SHA-1 */
+
+                       if (!S_ISREG(entry.mode))
+                               /* notes must be blobs */
+                               goto handle_non_note;
+
+                       if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
+                                        GIT_SHA1_RAWSZ - prefix_len))
+                               goto handle_non_note; /* entry.path is not a SHA1 */
 
-               /*
-                * If object SHA1 is complete (len == 20), assume note object
-                * If object SHA1 is incomplete (len < 20), and current
-                * component consists of 2 hex chars, assume note subtree
-                */
-               if (len <= 20) {
                        type = PTR_TYPE_NOTE;
-                       l = (struct leaf_node *)
-                               xcalloc(1, sizeof(struct leaf_node));
-                       hashcpy(l->key_sha1, object_sha1);
-                       hashcpy(l->val_sha1, entry.oid->hash);
-                       if (len < 20) {
-                               if (!S_ISDIR(entry.mode) || path_len != 2)
-                                       goto handle_non_note; /* not subtree */
-                               l->key_sha1[19] = (unsigned char) len;
-                               type = PTR_TYPE_SUBTREE;
-                       }
-                       if (note_tree_insert(t, node, n, l, type,
-                                            combine_notes_concatenate))
-                               die("Failed to load %s %s into notes tree "
-                                   "from %s",
-                                   type == PTR_TYPE_NOTE ? "note" : "subtree",
-                                   sha1_to_hex(l->key_sha1), t->ref);
+               } else if (path_len == 2) {
+                       /* This is potentially an internal node */
+                       size_t len = prefix_len;
+
+                       if (!S_ISDIR(entry.mode))
+                               /* internal nodes must be trees */
+                               goto handle_non_note;
+
+                       if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
+                               goto handle_non_note; /* entry.path is not a SHA1 */
+
+                       /*
+                        * Pad the rest of the SHA-1 with zeros,
+                        * except for the last byte, where we write
+                        * the length:
+                        */
+                       memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1);
+                       object_oid.hash[KEY_INDEX] = (unsigned char)len;
+
+                       type = PTR_TYPE_SUBTREE;
+               } else {
+                       /* This can't be part of a note */
+                       goto handle_non_note;
                }
+
+               l = xcalloc(1, sizeof(*l));
+               oidcpy(&l->key_oid, &object_oid);
+               oidcpy(&l->val_oid, entry.oid);
+               if (note_tree_insert(t, node, n, l, type,
+                                    combine_notes_concatenate))
+                       die("Failed to load %s %s into notes tree "
+                           "from %s",
+                           type == PTR_TYPE_NOTE ? "note" : "subtree",
+                           oid_to_hex(&l->key_oid), t->ref);
+
                continue;
 
 handle_non_note:
                /*
-                * Determine full path for this non-note entry:
-                * The filename is already found in entry.path, but the
-                * directory part of the path must be deduced from the subtree
-                * containing this entry. We assume here that the overall notes
-                * tree follows a strict byte-based progressive fanout
-                * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
-                * e.g. 4/36 fanout). This means that if a non-note is found at
-                * path "dead/beef", the following code will register it as
-                * being found on "de/ad/beef".
-                * On the other hand, if you use such non-obvious non-note
-                * paths in the middle of a notes tree, you deserve what's
-                * coming to you ;). Note that for non-notes that are not
-                * SHA1-like at the top level, there will be no problems.
-                *
-                * To conclude, it is strongly advised to make sure non-notes
-                * have at least one non-hex character in the top-level path
-                * component.
+                * Determine full path for this non-note entry. The
+                * filename is already found in entry.path, but the
+                * directory part of the path must be deduced from the
+                * subtree containing this entry based on our
+                * knowledge that the overall notes tree follows a
+                * strict byte-based progressive fanout structure
+                * (i.e. using 2/38, 2/2/36, etc. fanouts).
                 */
                {
                        struct strbuf non_note_path = STRBUF_INIT;
-                       const char *q = sha1_to_hex(subtree->key_sha1);
-                       int i;
+                       const char *q = oid_to_hex(&subtree->key_oid);
+                       size_t i;
                        for (i = 0; i < prefix_len; i++) {
                                strbuf_addch(&non_note_path, *q++);
                                strbuf_addch(&non_note_path, *q++);
@@ -540,14 +527,14 @@ static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
 }
 
 /* hex SHA1 + 19 * '/' + NUL */
-#define FANOUT_PATH_MAX 40 + 19 + 1
+#define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 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 < 20);
+       assert(fanout < GIT_SHA1_RAWSZ);
        while (fanout) {
                path[i++] = hex_sha1[j++];
                path[i++] = hex_sha1[j++];
@@ -597,15 +584,17 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
                            flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
                                /* invoke callback with subtree */
                                unsigned int path_len =
-                                       l->key_sha1[19] * 2 + fanout;
+                                       l->key_oid.hash[KEY_INDEX] * 2 + fanout;
                                assert(path_len < FANOUT_PATH_MAX - 1);
-                               construct_path_with_fanout(l->key_sha1, fanout,
+                               construct_path_with_fanout(l->key_oid.hash,
+                                                          fanout,
                                                           path);
                                /* Create trailing slash, if needed */
                                if (path[path_len - 1] != '/')
                                        path[path_len++] = '/';
                                path[path_len] = '\0';
-                               ret = fn(l->key_sha1, l->val_sha1, path,
+                               ret = fn(&l->key_oid, &l->val_oid,
+                                        path,
                                         cb_data);
                        }
                        if (n > fanout * 2 ||
@@ -619,8 +608,10 @@ static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
                        break;
                case PTR_TYPE_NOTE:
                        l = (struct leaf_node *) CLR_PTR_TYPE(p);
-                       construct_path_with_fanout(l->key_sha1, fanout, path);
-                       ret = fn(l->key_sha1, l->val_sha1, path, cb_data);
+                       construct_path_with_fanout(l->key_oid.hash, fanout,
+                                                  path);
+                       ret = fn(&l->key_oid, &l->val_oid, path,
+                                cb_data);
                        break;
                }
                if (ret)
@@ -648,7 +639,7 @@ static void write_tree_entry(struct strbuf *buf, unsigned int mode,
                unsigned char *sha1)
 {
        strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
-       strbuf_add(buf, sha1, 20);
+       strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
 }
 
 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
@@ -660,7 +651,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 + 40)); /* assume 256 entries per tree */
+       strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
        n->path[0] = n->path[1] = '\0';
        tws->next = n;
        tws->path[0] = path[0];
@@ -671,18 +662,18 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
 {
        int ret;
        struct tree_write_stack *n = tws->next;
-       unsigned char s[20];
+       struct object_id s;
        if (n) {
                ret = tree_write_stack_finish_subtree(n);
                if (ret)
                        return ret;
-               ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);
+               ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
                if (ret)
                        return ret;
                strbuf_release(&n->buf);
                free(n);
                tws->next = NULL;
-               write_tree_entry(&tws->buf, 040000, tws->path, 2, s);
+               write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
                tws->path[0] = tws->path[1] = '\0';
        }
        return 0;
@@ -690,7 +681,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
 
 static int write_each_note_helper(struct tree_write_stack *tws,
                const char *path, unsigned int mode,
-               const unsigned char *sha1)
+               const struct object_id *oid)
 {
        size_t path_len = strlen(path);
        unsigned int n = 0;
@@ -720,7 +711,7 @@ static int write_each_note_helper(struct tree_write_stack *tws,
 
        /* Finally add given entry to the current tree object */
        write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
-                        sha1);
+                        oid->hash);
 
        return 0;
 }
@@ -740,7 +731,7 @@ static int write_each_non_note_until(const char *note_path,
                        ; /* do nothing, prefer note to non-note */
                else {
                        ret = write_each_note_helper(d->root, n->path, n->mode,
-                                                    n->sha1);
+                                                    &n->oid);
                        if (ret)
                                return ret;
                }
@@ -750,8 +741,8 @@ static int write_each_non_note_until(const char *note_path,
        return 0;
 }
 
-static int write_each_note(const unsigned char *object_sha1,
-               const unsigned char *note_sha1, char *note_path,
+static int write_each_note(const struct object_id *object_oid,
+               const struct object_id *note_oid, char *note_path,
                void *cb_data)
 {
        struct write_each_note_data *d =
@@ -765,11 +756,11 @@ static int write_each_note(const unsigned char *object_sha1,
                note_path[note_path_len] = '\0';
                mode = 040000;
        }
-       assert(note_path_len <= 40 + 19);
+       assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
 
        /* Weave non-note entries into note entries */
        return  write_each_non_note_until(note_path, d) ||
-               write_each_note_helper(d->root, note_path, mode, note_sha1);
+               write_each_note_helper(d->root, note_path, mode, note_oid);
 }
 
 struct note_delete_list {
@@ -777,20 +768,20 @@ struct note_delete_list {
        const unsigned char *sha1;
 };
 
-static int prune_notes_helper(const unsigned char *object_sha1,
-               const unsigned char *note_sha1, char *note_path,
+static int prune_notes_helper(const struct object_id *object_oid,
+               const struct object_id *note_oid, char *note_path,
                void *cb_data)
 {
        struct note_delete_list **l = (struct note_delete_list **) cb_data;
        struct note_delete_list *n;
 
-       if (has_sha1_file(object_sha1))
+       if (has_object_file(object_oid))
                return 0; /* nothing to do for this note */
 
        /* failed to find object => prune this note */
        n = (struct note_delete_list *) xmalloc(sizeof(*n));
        n->next = *l;
-       n->sha1 = object_sha1;
+       n->sha1 = object_oid->hash;
        *l = n;
        return 0;
 }
@@ -940,8 +931,8 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
        if (has_glob_specials(glob)) {
                for_each_glob_ref(string_list_add_one_ref, glob, list);
        } else {
-               unsigned char sha1[20];
-               if (get_sha1(glob, sha1))
+               struct object_id oid;
+               if (get_oid(glob, &oid))
                        warning("notes ref %s is invalid", glob);
                if (!unsorted_string_list_has_string(list, glob))
                        string_list_append(list, glob);
@@ -993,7 +984,7 @@ const char *default_notes_ref(void)
 void init_notes(struct notes_tree *t, const char *notes_ref,
                combine_notes_fn combine_notes, int flags)
 {
-       unsigned char sha1[20], object_sha1[20];
+       struct object_id oid, object_oid;
        unsigned mode;
        struct leaf_node root_tree;
 
@@ -1017,16 +1008,16 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
        t->dirty = 0;
 
        if (flags & NOTES_INIT_EMPTY || !notes_ref ||
-           get_sha1_treeish(notes_ref, object_sha1))
+           get_oid_treeish(notes_ref, &object_oid))
                return;
-       if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_sha1))
+       if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
                die("Cannot use notes ref %s", notes_ref);
-       if (get_tree_entry(object_sha1, "", sha1, &mode))
+       if (get_tree_entry(object_oid.hash, "", oid.hash, &mode))
                die("Failed to read notes tree referenced by %s (%s)",
-                   notes_ref, sha1_to_hex(object_sha1));
+                   notes_ref, oid_to_hex(&object_oid));
 
-       hashclr(root_tree.key_sha1);
-       hashcpy(root_tree.val_sha1, sha1);
+       oidclr(&root_tree.key_oid);
+       oidcpy(&root_tree.val_oid, &oid);
        load_subtree(t, &root_tree, t->root, 0);
 }
 
@@ -1078,8 +1069,8 @@ void init_display_notes(struct display_notes_opt *opt)
        string_list_clear(&display_notes_refs, 0);
 }
 
-int add_note(struct notes_tree *t, const unsigned char *object_sha1,
-               const unsigned char *note_sha1, combine_notes_fn combine_notes)
+int add_note(struct notes_tree *t, const struct object_id *object_oid,
+               const struct object_id *note_oid, combine_notes_fn combine_notes)
 {
        struct leaf_node *l;
 
@@ -1090,8 +1081,8 @@ int add_note(struct notes_tree *t, const unsigned char *object_sha1,
        if (!combine_notes)
                combine_notes = t->combine_notes;
        l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
-       hashcpy(l->key_sha1, object_sha1);
-       hashcpy(l->val_sha1, note_sha1);
+       oidcpy(&l->key_oid, object_oid);
+       oidcpy(&l->val_oid, note_oid);
        return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
 }
 
@@ -1102,25 +1093,25 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
        if (!t)
                t = &default_notes_tree;
        assert(t->initialized);
-       hashcpy(l.key_sha1, object_sha1);
-       hashclr(l.val_sha1);
+       hashcpy(l.key_oid.hash, object_sha1);
+       oidclr(&l.val_oid);
        note_tree_remove(t, t->root, 0, &l);
-       if (is_null_sha1(l.val_sha1)) /* no note was removed */
+       if (is_null_oid(&l.val_oid)) /* no note was removed */
                return 1;
        t->dirty = 1;
        return 0;
 }
 
-const unsigned char *get_note(struct notes_tree *t,
-               const unsigned char *object_sha1)
+const struct object_id *get_note(struct notes_tree *t,
+               const struct object_id *oid)
 {
        struct leaf_node *found;
 
        if (!t)
                t = &default_notes_tree;
        assert(t->initialized);
-       found = note_tree_find(t, t->root, 0, object_sha1);
-       return found ? found->val_sha1 : NULL;
+       found = note_tree_find(t, t->root, 0, oid->hash);
+       return found ? &found->val_oid : NULL;
 }
 
 int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
@@ -1144,7 +1135,7 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)
 
        /* Prepare for traversal of current notes tree */
        root.next = NULL; /* last forward entry in list is grounded */
-       strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
+       strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
        root.path[0] = root.path[1] = '\0';
        cb_data.root = &root;
        cb_data.next_non_note = t->first_non_note;
@@ -1207,11 +1198,11 @@ void free_notes(struct notes_tree *t)
  * (raw != 0) gives the %N userformat; otherwise, the note message is given
  * for human consumption.
  */
-static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
+static void format_note(struct notes_tree *t, const struct object_id *object_oid,
                        struct strbuf *sb, const char *output_encoding, int raw)
 {
        static const char utf8[] = "utf-8";
-       const unsigned char *sha1;
+       const struct object_id *oid;
        char *msg, *msg_p;
        unsigned long linelen, msglen;
        enum object_type type;
@@ -1221,11 +1212,11 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
        if (!t->initialized)
                init_notes(t, NULL, NULL, 0);
 
-       sha1 = get_note(t, object_sha1);
-       if (!sha1)
+       oid = get_note(t, object_oid);
+       if (!oid)
                return;
 
-       if (!(msg = read_sha1_file(sha1, &type, &msglen)) || type != OBJ_BLOB) {
+       if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) {
                free(msg);
                return;
        }
@@ -1269,22 +1260,22 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
        free(msg);
 }
 
-void format_display_notes(const unsigned char *object_sha1,
+void format_display_notes(const struct object_id *object_oid,
                          struct strbuf *sb, const char *output_encoding, int raw)
 {
        int i;
        assert(display_notes_trees);
        for (i = 0; display_notes_trees[i]; i++)
-               format_note(display_notes_trees[i], object_sha1, sb,
+               format_note(display_notes_trees[i], object_oid, sb,
                            output_encoding, raw);
 }
 
 int copy_note(struct notes_tree *t,
-             const unsigned char *from_obj, const unsigned char *to_obj,
+             const struct object_id *from_obj, const struct object_id *to_obj,
              int force, combine_notes_fn combine_notes)
 {
-       const unsigned char *note = get_note(t, from_obj);
-       const unsigned char *existing_note = get_note(t, to_obj);
+       const struct object_id *note = get_note(t, from_obj);
+       const struct object_id *existing_note = get_note(t, to_obj);
 
        if (!force && existing_note)
                return 1;
@@ -1292,7 +1283,7 @@ int copy_note(struct notes_tree *t,
        if (note)
                return add_note(t, to_obj, note, combine_notes);
        else if (existing_note)
-               return add_note(t, to_obj, null_sha1, combine_notes);
+               return add_note(t, to_obj, &null_oid, combine_notes);
 
        return 0;
 }
@@ -1309,9 +1300,9 @@ void expand_notes_ref(struct strbuf *sb)
 
 void expand_loose_notes_ref(struct strbuf *sb)
 {
-       unsigned char object[20];
+       struct object_id object;
 
-       if (get_sha1(sb->buf, object)) {
+       if (get_oid(sb->buf, &object)) {
                /* fallback to expand_notes_ref */
                expand_notes_ref(sb);
        }