notes.con commit general improvements (43abf13)
   1#include "cache.h"
   2#include "config.h"
   3#include "notes.h"
   4#include "object-store.h"
   5#include "blob.h"
   6#include "tree.h"
   7#include "utf8.h"
   8#include "strbuf.h"
   9#include "tree-walk.h"
  10#include "string-list.h"
  11#include "refs.h"
  12
  13/*
  14 * Use a non-balancing simple 16-tree structure with struct int_node as
  15 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
  16 * 16-array of pointers to its children.
  17 * The bottom 2 bits of each pointer is used to identify the pointer type
  18 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
  19 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
  20 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
  21 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
  22 *
  23 * The root node is a statically allocated struct int_node.
  24 */
  25struct int_node {
  26        void *a[16];
  27};
  28
  29/*
  30 * Leaf nodes come in two variants, note entries and subtree entries,
  31 * distinguished by the LSb of the leaf node pointer (see above).
  32 * As a note entry, the key is the SHA1 of the referenced object, and the
  33 * value is the SHA1 of the note object.
  34 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
  35 * referenced object, using the last byte of the key to store the length of
  36 * the prefix. The value is the SHA1 of the tree object containing the notes
  37 * subtree.
  38 */
  39struct leaf_node {
  40        struct object_id key_oid;
  41        struct object_id val_oid;
  42};
  43
  44/*
  45 * A notes tree may contain entries that are not notes, and that do not follow
  46 * the naming conventions of notes. There are typically none/few of these, but
  47 * we still need to keep track of them. Keep a simple linked list sorted alpha-
  48 * betically on the non-note path. The list is populated when parsing tree
  49 * objects in load_subtree(), and the non-notes are correctly written back into
  50 * the tree objects produced by write_notes_tree().
  51 */
  52struct non_note {
  53        struct non_note *next; /* grounded (last->next == NULL) */
  54        char *path;
  55        unsigned int mode;
  56        struct object_id oid;
  57};
  58
  59#define PTR_TYPE_NULL     0
  60#define PTR_TYPE_INTERNAL 1
  61#define PTR_TYPE_NOTE     2
  62#define PTR_TYPE_SUBTREE  3
  63
  64#define GET_PTR_TYPE(ptr)       ((uintptr_t) (ptr) & 3)
  65#define CLR_PTR_TYPE(ptr)       ((void *) ((uintptr_t) (ptr) & ~3))
  66#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
  67
  68#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
  69
  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
  76struct notes_tree default_notes_tree;
  77
  78static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
  79static struct notes_tree **display_notes_trees;
  80
  81static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
  82                struct int_node *node, unsigned int n);
  83
  84/*
  85 * Search the tree until the appropriate location for the given key is found:
  86 * 1. Start at the root node, with n = 0
  87 * 2. If a[0] at the current level is a matching subtree entry, unpack that
  88 *    subtree entry and remove it; restart search at the current level.
  89 * 3. Use the nth nibble of the key as an index into a:
  90 *    - If a[n] is an int_node, recurse from #2 into that node and increment n
  91 *    - If a matching subtree entry, unpack that subtree entry (and remove it);
  92 *      restart search at the current level.
  93 *    - Otherwise, we have found one of the following:
  94 *      - a subtree entry which does not match the key
  95 *      - a note entry which may or may not match the key
  96 *      - an unused leaf node (NULL)
  97 *      In any case, set *tree and *n, and return pointer to the tree location.
  98 */
  99static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
 100                unsigned char *n, const unsigned char *key_sha1)
 101{
 102        struct leaf_node *l;
 103        unsigned char i;
 104        void *p = (*tree)->a[0];
 105
 106        if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
 107                l = (struct leaf_node *) CLR_PTR_TYPE(p);
 108                if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
 109                        /* unpack tree and resume search */
 110                        (*tree)->a[0] = NULL;
 111                        load_subtree(t, l, *tree, *n);
 112                        free(l);
 113                        return note_tree_search(t, tree, n, key_sha1);
 114                }
 115        }
 116
 117        i = GET_NIBBLE(*n, key_sha1);
 118        p = (*tree)->a[i];
 119        switch (GET_PTR_TYPE(p)) {
 120        case PTR_TYPE_INTERNAL:
 121                *tree = CLR_PTR_TYPE(p);
 122                (*n)++;
 123                return note_tree_search(t, tree, n, key_sha1);
 124        case PTR_TYPE_SUBTREE:
 125                l = (struct leaf_node *) CLR_PTR_TYPE(p);
 126                if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
 127                        /* unpack tree and resume search */
 128                        (*tree)->a[i] = NULL;
 129                        load_subtree(t, l, *tree, *n);
 130                        free(l);
 131                        return note_tree_search(t, tree, n, key_sha1);
 132                }
 133                /* fall through */
 134        default:
 135                return &((*tree)->a[i]);
 136        }
 137}
 138
 139/*
 140 * To find a leaf_node:
 141 * Search to the tree location appropriate for the given key:
 142 * If a note entry with matching key, return the note entry, else return NULL.
 143 */
 144static struct leaf_node *note_tree_find(struct notes_tree *t,
 145                struct int_node *tree, unsigned char n,
 146                const unsigned char *key_sha1)
 147{
 148        void **p = note_tree_search(t, &tree, &n, key_sha1);
 149        if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
 150                struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
 151                if (hasheq(key_sha1, l->key_oid.hash))
 152                        return l;
 153        }
 154        return NULL;
 155}
 156
 157/*
 158 * How to consolidate an int_node:
 159 * If there are > 1 non-NULL entries, give up and return non-zero.
 160 * Otherwise replace the int_node at the given index in the given parent node
 161 * with the only NOTE entry (or a NULL entry if no entries) from the given
 162 * tree, and return 0.
 163 */
 164static int note_tree_consolidate(struct int_node *tree,
 165        struct int_node *parent, unsigned char index)
 166{
 167        unsigned int i;
 168        void *p = NULL;
 169
 170        assert(tree && parent);
 171        assert(CLR_PTR_TYPE(parent->a[index]) == tree);
 172
 173        for (i = 0; i < 16; i++) {
 174                if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
 175                        if (p) /* more than one entry */
 176                                return -2;
 177                        p = tree->a[i];
 178                }
 179        }
 180
 181        if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
 182                return -2;
 183        /* replace tree with p in parent[index] */
 184        parent->a[index] = p;
 185        free(tree);
 186        return 0;
 187}
 188
 189/*
 190 * To remove a leaf_node:
 191 * Search to the tree location appropriate for the given leaf_node's key:
 192 * - If location does not hold a matching entry, abort and do nothing.
 193 * - Copy the matching entry's value into the given entry.
 194 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
 195 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
 196 */
 197static void note_tree_remove(struct notes_tree *t,
 198                struct int_node *tree, unsigned char n,
 199                struct leaf_node *entry)
 200{
 201        struct leaf_node *l;
 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
 206        assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
 207        if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
 208                return; /* type mismatch, nothing to remove */
 209        l = (struct leaf_node *) CLR_PTR_TYPE(*p);
 210        if (!oideq(&l->key_oid, &entry->key_oid))
 211                return; /* key mismatch, nothing to remove */
 212
 213        /* we have found a matching entry */
 214        oidcpy(&entry->val_oid, &l->val_oid);
 215        free(l);
 216        *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
 217
 218        /* consolidate this tree level, and parent levels, if possible */
 219        if (!n)
 220                return; /* cannot consolidate top level */
 221        /* first, build stack of ancestors between root and current node */
 222        parent_stack[0] = t->root;
 223        for (i = 0; i < n; i++) {
 224                j = GET_NIBBLE(i, entry->key_oid.hash);
 225                parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
 226        }
 227        assert(i == n && parent_stack[i] == tree);
 228        /* next, unwind stack until note_tree_consolidate() is done */
 229        while (i > 0 &&
 230               !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
 231                                      GET_NIBBLE(i - 1, entry->key_oid.hash)))
 232                i--;
 233}
 234
 235/*
 236 * To insert a leaf_node:
 237 * Search to the tree location appropriate for the given leaf_node's key:
 238 * - If location is unused (NULL), store the tweaked pointer directly there
 239 * - If location holds a note entry that matches the note-to-be-inserted, then
 240 *   combine the two notes (by calling the given combine_notes function).
 241 * - If location holds a note entry that matches the subtree-to-be-inserted,
 242 *   then unpack the subtree-to-be-inserted into the location.
 243 * - If location holds a matching subtree entry, unpack the subtree at that
 244 *   location, and restart the insert operation from that level.
 245 * - Else, create a new int_node, holding both the node-at-location and the
 246 *   node-to-be-inserted, and store the new int_node into the location.
 247 */
 248static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
 249                unsigned char n, struct leaf_node *entry, unsigned char type,
 250                combine_notes_fn combine_notes)
 251{
 252        struct int_node *new_node;
 253        struct leaf_node *l;
 254        void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
 255        int ret = 0;
 256
 257        assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
 258        l = (struct leaf_node *) CLR_PTR_TYPE(*p);
 259        switch (GET_PTR_TYPE(*p)) {
 260        case PTR_TYPE_NULL:
 261                assert(!*p);
 262                if (is_null_oid(&entry->val_oid))
 263                        free(entry);
 264                else
 265                        *p = SET_PTR_TYPE(entry, type);
 266                return 0;
 267        case PTR_TYPE_NOTE:
 268                switch (type) {
 269                case PTR_TYPE_NOTE:
 270                        if (oideq(&l->key_oid, &entry->key_oid)) {
 271                                /* skip concatenation if l == entry */
 272                                if (oideq(&l->val_oid, &entry->val_oid)) {
 273                                        free(entry);
 274                                        return 0;
 275                                }
 276
 277                                ret = combine_notes(&l->val_oid,
 278                                                    &entry->val_oid);
 279                                if (!ret && is_null_oid(&l->val_oid))
 280                                        note_tree_remove(t, tree, n, entry);
 281                                free(entry);
 282                                return ret;
 283                        }
 284                        break;
 285                case PTR_TYPE_SUBTREE:
 286                        if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
 287                                                    entry->key_oid.hash)) {
 288                                /* unpack 'entry' */
 289                                load_subtree(t, entry, tree, n);
 290                                free(entry);
 291                                return 0;
 292                        }
 293                        break;
 294                }
 295                break;
 296        case PTR_TYPE_SUBTREE:
 297                if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
 298                        /* unpack 'l' and restart insert */
 299                        *p = NULL;
 300                        load_subtree(t, l, tree, n);
 301                        free(l);
 302                        return note_tree_insert(t, tree, n, entry, type,
 303                                                combine_notes);
 304                }
 305                break;
 306        }
 307
 308        /* non-matching leaf_node */
 309        assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
 310               GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
 311        if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
 312                free(entry);
 313                return 0;
 314        }
 315        new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
 316        ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
 317                               combine_notes);
 318        if (ret)
 319                return ret;
 320        *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
 321        return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
 322}
 323
 324/* Free the entire notes data contained in the given tree */
 325static void note_tree_free(struct int_node *tree)
 326{
 327        unsigned int i;
 328        for (i = 0; i < 16; i++) {
 329                void *p = tree->a[i];
 330                switch (GET_PTR_TYPE(p)) {
 331                case PTR_TYPE_INTERNAL:
 332                        note_tree_free(CLR_PTR_TYPE(p));
 333                        /* fall through */
 334                case PTR_TYPE_NOTE:
 335                case PTR_TYPE_SUBTREE:
 336                        free(CLR_PTR_TYPE(p));
 337                }
 338        }
 339}
 340
 341static int non_note_cmp(const struct non_note *a, const struct non_note *b)
 342{
 343        return strcmp(a->path, b->path);
 344}
 345
 346/* note: takes ownership of path string */
 347static void add_non_note(struct notes_tree *t, char *path,
 348                unsigned int mode, const unsigned char *sha1)
 349{
 350        struct non_note *p = t->prev_non_note, *n;
 351        n = (struct non_note *) xmalloc(sizeof(struct non_note));
 352        n->next = NULL;
 353        n->path = path;
 354        n->mode = mode;
 355        hashcpy(n->oid.hash, sha1);
 356        t->prev_non_note = n;
 357
 358        if (!t->first_non_note) {
 359                t->first_non_note = n;
 360                return;
 361        }
 362
 363        if (non_note_cmp(p, n) < 0)
 364                ; /* do nothing  */
 365        else if (non_note_cmp(t->first_non_note, n) <= 0)
 366                p = t->first_non_note;
 367        else {
 368                /* n sorts before t->first_non_note */
 369                n->next = t->first_non_note;
 370                t->first_non_note = n;
 371                return;
 372        }
 373
 374        /* n sorts equal or after p */
 375        while (p->next && non_note_cmp(p->next, n) <= 0)
 376                p = p->next;
 377
 378        if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
 379                assert(strcmp(p->path, n->path) == 0);
 380                p->mode = n->mode;
 381                oidcpy(&p->oid, &n->oid);
 382                free(n);
 383                t->prev_non_note = p;
 384                return;
 385        }
 386
 387        /* n sorts between p and p->next */
 388        n->next = p->next;
 389        p->next = n;
 390}
 391
 392static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 393                struct int_node *node, unsigned int n)
 394{
 395        struct object_id object_oid;
 396        size_t prefix_len;
 397        void *buf;
 398        struct tree_desc desc;
 399        struct name_entry entry;
 400        const unsigned hashsz = the_hash_algo->rawsz;
 401
 402        buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
 403        if (!buf)
 404                die("Could not read %s for notes-index",
 405                     oid_to_hex(&subtree->val_oid));
 406
 407        prefix_len = subtree->key_oid.hash[KEY_INDEX];
 408        if (prefix_len >= hashsz)
 409                BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
 410        if (prefix_len * 2 < n)
 411                BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
 412        memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
 413        while (tree_entry(&desc, &entry)) {
 414                unsigned char type;
 415                struct leaf_node *l;
 416                size_t path_len = strlen(entry.path);
 417
 418                if (path_len == 2 * (hashsz - prefix_len)) {
 419                        /* This is potentially the remainder of the SHA-1 */
 420
 421                        if (!S_ISREG(entry.mode))
 422                                /* notes must be blobs */
 423                                goto handle_non_note;
 424
 425                        if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
 426                                         hashsz - prefix_len))
 427                                goto handle_non_note; /* entry.path is not a SHA1 */
 428
 429                        type = PTR_TYPE_NOTE;
 430                } else if (path_len == 2) {
 431                        /* This is potentially an internal node */
 432                        size_t len = prefix_len;
 433
 434                        if (!S_ISDIR(entry.mode))
 435                                /* internal nodes must be trees */
 436                                goto handle_non_note;
 437
 438                        if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
 439                                goto handle_non_note; /* entry.path is not a SHA1 */
 440
 441                        /*
 442                         * Pad the rest of the SHA-1 with zeros,
 443                         * except for the last byte, where we write
 444                         * the length:
 445                         */
 446                        memset(object_oid.hash + len, 0, hashsz - len - 1);
 447                        object_oid.hash[KEY_INDEX] = (unsigned char)len;
 448
 449                        type = PTR_TYPE_SUBTREE;
 450                } else {
 451                        /* This can't be part of a note */
 452                        goto handle_non_note;
 453                }
 454
 455                l = xcalloc(1, sizeof(*l));
 456                oidcpy(&l->key_oid, &object_oid);
 457                oidcpy(&l->val_oid, &entry.oid);
 458                if (note_tree_insert(t, node, n, l, type,
 459                                     combine_notes_concatenate))
 460                        die("Failed to load %s %s into notes tree "
 461                            "from %s",
 462                            type == PTR_TYPE_NOTE ? "note" : "subtree",
 463                            oid_to_hex(&object_oid), t->ref);
 464
 465                continue;
 466
 467handle_non_note:
 468                /*
 469                 * Determine full path for this non-note entry. The
 470                 * filename is already found in entry.path, but the
 471                 * directory part of the path must be deduced from the
 472                 * subtree containing this entry based on our
 473                 * knowledge that the overall notes tree follows a
 474                 * strict byte-based progressive fanout structure
 475                 * (i.e. using 2/38, 2/2/36, etc. fanouts).
 476                 */
 477                {
 478                        struct strbuf non_note_path = STRBUF_INIT;
 479                        const char *q = oid_to_hex(&subtree->key_oid);
 480                        size_t i;
 481                        for (i = 0; i < prefix_len; i++) {
 482                                strbuf_addch(&non_note_path, *q++);
 483                                strbuf_addch(&non_note_path, *q++);
 484                                strbuf_addch(&non_note_path, '/');
 485                        }
 486                        strbuf_addstr(&non_note_path, entry.path);
 487                        add_non_note(t, strbuf_detach(&non_note_path, NULL),
 488                                     entry.mode, entry.oid.hash);
 489                }
 490        }
 491        free(buf);
 492}
 493
 494/*
 495 * Determine optimal on-disk fanout for this part of the notes tree
 496 *
 497 * Given a (sub)tree and the level in the internal tree structure, determine
 498 * whether or not the given existing fanout should be expanded for this
 499 * (sub)tree.
 500 *
 501 * Values of the 'fanout' variable:
 502 * - 0: No fanout (all notes are stored directly in the root notes tree)
 503 * - 1: 2/38 fanout
 504 * - 2: 2/2/36 fanout
 505 * - 3: 2/2/2/34 fanout
 506 * etc.
 507 */
 508static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
 509                unsigned char fanout)
 510{
 511        /*
 512         * The following is a simple heuristic that works well in practice:
 513         * For each even-numbered 16-tree level (remember that each on-disk
 514         * fanout level corresponds to _two_ 16-tree levels), peek at all 16
 515         * entries at that tree level. If all of them are either int_nodes or
 516         * subtree entries, then there are likely plenty of notes below this
 517         * level, so we return an incremented fanout.
 518         */
 519        unsigned int i;
 520        if ((n % 2) || (n > 2 * fanout))
 521                return fanout;
 522        for (i = 0; i < 16; i++) {
 523                switch (GET_PTR_TYPE(tree->a[i])) {
 524                case PTR_TYPE_SUBTREE:
 525                case PTR_TYPE_INTERNAL:
 526                        continue;
 527                default:
 528                        return fanout;
 529                }
 530        }
 531        return fanout + 1;
 532}
 533
 534/* hex oid + '/' between each pair of hex digits + NUL */
 535#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
 536
 537static void construct_path_with_fanout(const unsigned char *hash,
 538                unsigned char fanout, char *path)
 539{
 540        unsigned int i = 0, j = 0;
 541        const char *hex_hash = hash_to_hex(hash);
 542        assert(fanout < the_hash_algo->rawsz);
 543        while (fanout) {
 544                path[i++] = hex_hash[j++];
 545                path[i++] = hex_hash[j++];
 546                path[i++] = '/';
 547                fanout--;
 548        }
 549        xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
 550}
 551
 552static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
 553                unsigned char n, unsigned char fanout, int flags,
 554                each_note_fn fn, void *cb_data)
 555{
 556        unsigned int i;
 557        void *p;
 558        int ret = 0;
 559        struct leaf_node *l;
 560        static char path[FANOUT_PATH_MAX];
 561
 562        fanout = determine_fanout(tree, n, fanout);
 563        for (i = 0; i < 16; i++) {
 564redo:
 565                p = tree->a[i];
 566                switch (GET_PTR_TYPE(p)) {
 567                case PTR_TYPE_INTERNAL:
 568                        /* recurse into int_node */
 569                        ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
 570                                fanout, flags, fn, cb_data);
 571                        break;
 572                case PTR_TYPE_SUBTREE:
 573                        l = (struct leaf_node *) CLR_PTR_TYPE(p);
 574                        /*
 575                         * Subtree entries in the note tree represent parts of
 576                         * the note tree that have not yet been explored. There
 577                         * is a direct relationship between subtree entries at
 578                         * level 'n' in the tree, and the 'fanout' variable:
 579                         * Subtree entries at level 'n <= 2 * fanout' should be
 580                         * preserved, since they correspond exactly to a fanout
 581                         * directory in the on-disk structure. However, subtree
 582                         * entries at level 'n > 2 * fanout' should NOT be
 583                         * preserved, but rather consolidated into the above
 584                         * notes tree level. We achieve this by unconditionally
 585                         * unpacking subtree entries that exist below the
 586                         * threshold level at 'n = 2 * fanout'.
 587                         */
 588                        if (n <= 2 * fanout &&
 589                            flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
 590                                /* invoke callback with subtree */
 591                                unsigned int path_len =
 592                                        l->key_oid.hash[KEY_INDEX] * 2 + fanout;
 593                                assert(path_len < FANOUT_PATH_MAX - 1);
 594                                construct_path_with_fanout(l->key_oid.hash,
 595                                                           fanout,
 596                                                           path);
 597                                /* Create trailing slash, if needed */
 598                                if (path[path_len - 1] != '/')
 599                                        path[path_len++] = '/';
 600                                path[path_len] = '\0';
 601                                ret = fn(&l->key_oid, &l->val_oid,
 602                                         path,
 603                                         cb_data);
 604                        }
 605                        if (n > fanout * 2 ||
 606                            !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
 607                                /* unpack subtree and resume traversal */
 608                                tree->a[i] = NULL;
 609                                load_subtree(t, l, tree, n);
 610                                free(l);
 611                                goto redo;
 612                        }
 613                        break;
 614                case PTR_TYPE_NOTE:
 615                        l = (struct leaf_node *) CLR_PTR_TYPE(p);
 616                        construct_path_with_fanout(l->key_oid.hash, fanout,
 617                                                   path);
 618                        ret = fn(&l->key_oid, &l->val_oid, path,
 619                                 cb_data);
 620                        break;
 621                }
 622                if (ret)
 623                        return ret;
 624        }
 625        return 0;
 626}
 627
 628struct tree_write_stack {
 629        struct tree_write_stack *next;
 630        struct strbuf buf;
 631        char path[2]; /* path to subtree in next, if any */
 632};
 633
 634static inline int matches_tree_write_stack(struct tree_write_stack *tws,
 635                const char *full_path)
 636{
 637        return  full_path[0] == tws->path[0] &&
 638                full_path[1] == tws->path[1] &&
 639                full_path[2] == '/';
 640}
 641
 642static void write_tree_entry(struct strbuf *buf, unsigned int mode,
 643                const char *path, unsigned int path_len, const
 644                unsigned char *hash)
 645{
 646        strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
 647        strbuf_add(buf, hash, the_hash_algo->rawsz);
 648}
 649
 650static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
 651                const char *path)
 652{
 653        struct tree_write_stack *n;
 654        assert(!tws->next);
 655        assert(tws->path[0] == '\0' && tws->path[1] == '\0');
 656        n = (struct tree_write_stack *)
 657                xmalloc(sizeof(struct tree_write_stack));
 658        n->next = NULL;
 659        strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
 660        n->path[0] = n->path[1] = '\0';
 661        tws->next = n;
 662        tws->path[0] = path[0];
 663        tws->path[1] = path[1];
 664}
 665
 666static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
 667{
 668        int ret;
 669        struct tree_write_stack *n = tws->next;
 670        struct object_id s;
 671        if (n) {
 672                ret = tree_write_stack_finish_subtree(n);
 673                if (ret)
 674                        return ret;
 675                ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s);
 676                if (ret)
 677                        return ret;
 678                strbuf_release(&n->buf);
 679                free(n);
 680                tws->next = NULL;
 681                write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
 682                tws->path[0] = tws->path[1] = '\0';
 683        }
 684        return 0;
 685}
 686
 687static int write_each_note_helper(struct tree_write_stack *tws,
 688                const char *path, unsigned int mode,
 689                const struct object_id *oid)
 690{
 691        size_t path_len = strlen(path);
 692        unsigned int n = 0;
 693        int ret;
 694
 695        /* Determine common part of tree write stack */
 696        while (tws && 3 * n < path_len &&
 697               matches_tree_write_stack(tws, path + 3 * n)) {
 698                n++;
 699                tws = tws->next;
 700        }
 701
 702        /* tws point to last matching tree_write_stack entry */
 703        ret = tree_write_stack_finish_subtree(tws);
 704        if (ret)
 705                return ret;
 706
 707        /* Start subtrees needed to satisfy path */
 708        while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
 709                tree_write_stack_init_subtree(tws, path + 3 * n);
 710                n++;
 711                tws = tws->next;
 712        }
 713
 714        /* There should be no more directory components in the given path */
 715        assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
 716
 717        /* Finally add given entry to the current tree object */
 718        write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
 719                         oid->hash);
 720
 721        return 0;
 722}
 723
 724struct write_each_note_data {
 725        struct tree_write_stack *root;
 726        struct non_note *next_non_note;
 727};
 728
 729static int write_each_non_note_until(const char *note_path,
 730                struct write_each_note_data *d)
 731{
 732        struct non_note *n = d->next_non_note;
 733        int cmp = 0, ret;
 734        while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
 735                if (note_path && cmp == 0)
 736                        ; /* do nothing, prefer note to non-note */
 737                else {
 738                        ret = write_each_note_helper(d->root, n->path, n->mode,
 739                                                     &n->oid);
 740                        if (ret)
 741                                return ret;
 742                }
 743                n = n->next;
 744        }
 745        d->next_non_note = n;
 746        return 0;
 747}
 748
 749static int write_each_note(const struct object_id *object_oid,
 750                const struct object_id *note_oid, char *note_path,
 751                void *cb_data)
 752{
 753        struct write_each_note_data *d =
 754                (struct write_each_note_data *) cb_data;
 755        size_t note_path_len = strlen(note_path);
 756        unsigned int mode = 0100644;
 757
 758        if (note_path[note_path_len - 1] == '/') {
 759                /* subtree entry */
 760                note_path_len--;
 761                note_path[note_path_len] = '\0';
 762                mode = 040000;
 763        }
 764        assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
 765
 766        /* Weave non-note entries into note entries */
 767        return  write_each_non_note_until(note_path, d) ||
 768                write_each_note_helper(d->root, note_path, mode, note_oid);
 769}
 770
 771struct note_delete_list {
 772        struct note_delete_list *next;
 773        const unsigned char *sha1;
 774};
 775
 776static int prune_notes_helper(const struct object_id *object_oid,
 777                const struct object_id *note_oid, char *note_path,
 778                void *cb_data)
 779{
 780        struct note_delete_list **l = (struct note_delete_list **) cb_data;
 781        struct note_delete_list *n;
 782
 783        if (has_object_file(object_oid))
 784                return 0; /* nothing to do for this note */
 785
 786        /* failed to find object => prune this note */
 787        n = (struct note_delete_list *) xmalloc(sizeof(*n));
 788        n->next = *l;
 789        n->sha1 = object_oid->hash;
 790        *l = n;
 791        return 0;
 792}
 793
 794int combine_notes_concatenate(struct object_id *cur_oid,
 795                              const struct object_id *new_oid)
 796{
 797        char *cur_msg = NULL, *new_msg = NULL, *buf;
 798        unsigned long cur_len, new_len, buf_len;
 799        enum object_type cur_type, new_type;
 800        int ret;
 801
 802        /* read in both note blob objects */
 803        if (!is_null_oid(new_oid))
 804                new_msg = read_object_file(new_oid, &new_type, &new_len);
 805        if (!new_msg || !new_len || new_type != OBJ_BLOB) {
 806                free(new_msg);
 807                return 0;
 808        }
 809        if (!is_null_oid(cur_oid))
 810                cur_msg = read_object_file(cur_oid, &cur_type, &cur_len);
 811        if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
 812                free(cur_msg);
 813                free(new_msg);
 814                oidcpy(cur_oid, new_oid);
 815                return 0;
 816        }
 817
 818        /* we will separate the notes by two newlines anyway */
 819        if (cur_msg[cur_len - 1] == '\n')
 820                cur_len--;
 821
 822        /* concatenate cur_msg and new_msg into buf */
 823        buf_len = cur_len + 2 + new_len;
 824        buf = (char *) xmalloc(buf_len);
 825        memcpy(buf, cur_msg, cur_len);
 826        buf[cur_len] = '\n';
 827        buf[cur_len + 1] = '\n';
 828        memcpy(buf + cur_len + 2, new_msg, new_len);
 829        free(cur_msg);
 830        free(new_msg);
 831
 832        /* create a new blob object from buf */
 833        ret = write_object_file(buf, buf_len, blob_type, cur_oid);
 834        free(buf);
 835        return ret;
 836}
 837
 838int combine_notes_overwrite(struct object_id *cur_oid,
 839                            const struct object_id *new_oid)
 840{
 841        oidcpy(cur_oid, new_oid);
 842        return 0;
 843}
 844
 845int combine_notes_ignore(struct object_id *cur_oid,
 846                         const struct object_id *new_oid)
 847{
 848        return 0;
 849}
 850
 851/*
 852 * Add the lines from the named object to list, with trailing
 853 * newlines removed.
 854 */
 855static int string_list_add_note_lines(struct string_list *list,
 856                                      const struct object_id *oid)
 857{
 858        char *data;
 859        unsigned long len;
 860        enum object_type t;
 861
 862        if (is_null_oid(oid))
 863                return 0;
 864
 865        /* read_sha1_file NUL-terminates */
 866        data = read_object_file(oid, &t, &len);
 867        if (t != OBJ_BLOB || !data || !len) {
 868                free(data);
 869                return t != OBJ_BLOB || !data;
 870        }
 871
 872        /*
 873         * If the last line of the file is EOL-terminated, this will
 874         * add an empty string to the list.  But it will be removed
 875         * later, along with any empty strings that came from empty
 876         * lines within the file.
 877         */
 878        string_list_split(list, data, '\n', -1);
 879        free(data);
 880        return 0;
 881}
 882
 883static int string_list_join_lines_helper(struct string_list_item *item,
 884                                         void *cb_data)
 885{
 886        struct strbuf *buf = cb_data;
 887        strbuf_addstr(buf, item->string);
 888        strbuf_addch(buf, '\n');
 889        return 0;
 890}
 891
 892int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
 893                                const struct object_id *new_oid)
 894{
 895        struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
 896        struct strbuf buf = STRBUF_INIT;
 897        int ret = 1;
 898
 899        /* read both note blob objects into unique_lines */
 900        if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
 901                goto out;
 902        if (string_list_add_note_lines(&sort_uniq_list, new_oid))
 903                goto out;
 904        string_list_remove_empty_items(&sort_uniq_list, 0);
 905        string_list_sort(&sort_uniq_list);
 906        string_list_remove_duplicates(&sort_uniq_list, 0);
 907
 908        /* create a new blob object from sort_uniq_list */
 909        if (for_each_string_list(&sort_uniq_list,
 910                                 string_list_join_lines_helper, &buf))
 911                goto out;
 912
 913        ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid);
 914
 915out:
 916        strbuf_release(&buf);
 917        string_list_clear(&sort_uniq_list, 0);
 918        return ret;
 919}
 920
 921static int string_list_add_one_ref(const char *refname, const struct object_id *oid,
 922                                   int flag, void *cb)
 923{
 924        struct string_list *refs = cb;
 925        if (!unsorted_string_list_has_string(refs, refname))
 926                string_list_append(refs, refname);
 927        return 0;
 928}
 929
 930/*
 931 * The list argument must have strdup_strings set on it.
 932 */
 933void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
 934{
 935        assert(list->strdup_strings);
 936        if (has_glob_specials(glob)) {
 937                for_each_glob_ref(string_list_add_one_ref, glob, list);
 938        } else {
 939                struct object_id oid;
 940                if (get_oid(glob, &oid))
 941                        warning("notes ref %s is invalid", glob);
 942                if (!unsorted_string_list_has_string(list, glob))
 943                        string_list_append(list, glob);
 944        }
 945}
 946
 947void string_list_add_refs_from_colon_sep(struct string_list *list,
 948                                         const char *globs)
 949{
 950        struct string_list split = STRING_LIST_INIT_NODUP;
 951        char *globs_copy = xstrdup(globs);
 952        int i;
 953
 954        string_list_split_in_place(&split, globs_copy, ':', -1);
 955        string_list_remove_empty_items(&split, 0);
 956
 957        for (i = 0; i < split.nr; i++)
 958                string_list_add_refs_by_glob(list, split.items[i].string);
 959
 960        string_list_clear(&split, 0);
 961        free(globs_copy);
 962}
 963
 964static int notes_display_config(const char *k, const char *v, void *cb)
 965{
 966        int *load_refs = cb;
 967
 968        if (*load_refs && !strcmp(k, "notes.displayref")) {
 969                if (!v)
 970                        config_error_nonbool(k);
 971                string_list_add_refs_by_glob(&display_notes_refs, v);
 972        }
 973
 974        return 0;
 975}
 976
 977const char *default_notes_ref(void)
 978{
 979        const char *notes_ref = NULL;
 980        if (!notes_ref)
 981                notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
 982        if (!notes_ref)
 983                notes_ref = notes_ref_name; /* value of core.notesRef config */
 984        if (!notes_ref)
 985                notes_ref = GIT_NOTES_DEFAULT_REF;
 986        return notes_ref;
 987}
 988
 989void init_notes(struct notes_tree *t, const char *notes_ref,
 990                combine_notes_fn combine_notes, int flags)
 991{
 992        struct object_id oid, object_oid;
 993        unsigned short mode;
 994        struct leaf_node root_tree;
 995
 996        if (!t)
 997                t = &default_notes_tree;
 998        assert(!t->initialized);
 999
1000        if (!notes_ref)
1001                notes_ref = default_notes_ref();
1002
1003        if (!combine_notes)
1004                combine_notes = combine_notes_concatenate;
1005
1006        t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
1007        t->first_non_note = NULL;
1008        t->prev_non_note = NULL;
1009        t->ref = xstrdup_or_null(notes_ref);
1010        t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
1011        t->combine_notes = combine_notes;
1012        t->initialized = 1;
1013        t->dirty = 0;
1014
1015        if (flags & NOTES_INIT_EMPTY || !notes_ref ||
1016            get_oid_treeish(notes_ref, &object_oid))
1017                return;
1018        if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
1019                die("Cannot use notes ref %s", notes_ref);
1020        if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
1021                die("Failed to read notes tree referenced by %s (%s)",
1022                    notes_ref, oid_to_hex(&object_oid));
1023
1024        oidclr(&root_tree.key_oid);
1025        oidcpy(&root_tree.val_oid, &oid);
1026        load_subtree(t, &root_tree, t->root, 0);
1027}
1028
1029struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
1030{
1031        struct string_list_item *item;
1032        int counter = 0;
1033        struct notes_tree **trees;
1034        ALLOC_ARRAY(trees, refs->nr + 1);
1035        for_each_string_list_item(item, refs) {
1036                struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
1037                init_notes(t, item->string, combine_notes_ignore, flags);
1038                trees[counter++] = t;
1039        }
1040        trees[counter] = NULL;
1041        return trees;
1042}
1043
1044void init_display_notes(struct display_notes_opt *opt)
1045{
1046        char *display_ref_env;
1047        int load_config_refs = 0;
1048        display_notes_refs.strdup_strings = 1;
1049
1050        assert(!display_notes_trees);
1051
1052        if (!opt || opt->use_default_notes > 0 ||
1053            (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1054                string_list_append(&display_notes_refs, default_notes_ref());
1055                display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1056                if (display_ref_env) {
1057                        string_list_add_refs_from_colon_sep(&display_notes_refs,
1058                                                            display_ref_env);
1059                        load_config_refs = 0;
1060                } else
1061                        load_config_refs = 1;
1062        }
1063
1064        git_config(notes_display_config, &load_config_refs);
1065
1066        if (opt) {
1067                struct string_list_item *item;
1068                for_each_string_list_item(item, &opt->extra_notes_refs)
1069                        string_list_add_refs_by_glob(&display_notes_refs,
1070                                                     item->string);
1071        }
1072
1073        display_notes_trees = load_notes_trees(&display_notes_refs, 0);
1074        string_list_clear(&display_notes_refs, 0);
1075}
1076
1077int add_note(struct notes_tree *t, const struct object_id *object_oid,
1078                const struct object_id *note_oid, combine_notes_fn combine_notes)
1079{
1080        struct leaf_node *l;
1081
1082        if (!t)
1083                t = &default_notes_tree;
1084        assert(t->initialized);
1085        t->dirty = 1;
1086        if (!combine_notes)
1087                combine_notes = t->combine_notes;
1088        l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
1089        oidcpy(&l->key_oid, object_oid);
1090        oidcpy(&l->val_oid, note_oid);
1091        return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
1092}
1093
1094int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1095{
1096        struct leaf_node l;
1097
1098        if (!t)
1099                t = &default_notes_tree;
1100        assert(t->initialized);
1101        hashcpy(l.key_oid.hash, object_sha1);
1102        oidclr(&l.val_oid);
1103        note_tree_remove(t, t->root, 0, &l);
1104        if (is_null_oid(&l.val_oid)) /* no note was removed */
1105                return 1;
1106        t->dirty = 1;
1107        return 0;
1108}
1109
1110const struct object_id *get_note(struct notes_tree *t,
1111                const struct object_id *oid)
1112{
1113        struct leaf_node *found;
1114
1115        if (!t)
1116                t = &default_notes_tree;
1117        assert(t->initialized);
1118        found = note_tree_find(t, t->root, 0, oid->hash);
1119        return found ? &found->val_oid : NULL;
1120}
1121
1122int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1123                void *cb_data)
1124{
1125        if (!t)
1126                t = &default_notes_tree;
1127        assert(t->initialized);
1128        return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
1129}
1130
1131int write_notes_tree(struct notes_tree *t, struct object_id *result)
1132{
1133        struct tree_write_stack root;
1134        struct write_each_note_data cb_data;
1135        int ret;
1136        int flags;
1137
1138        if (!t)
1139                t = &default_notes_tree;
1140        assert(t->initialized);
1141
1142        /* Prepare for traversal of current notes tree */
1143        root.next = NULL; /* last forward entry in list is grounded */
1144        strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
1145        root.path[0] = root.path[1] = '\0';
1146        cb_data.root = &root;
1147        cb_data.next_non_note = t->first_non_note;
1148
1149        /* Write tree objects representing current notes tree */
1150        flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1151                FOR_EACH_NOTE_YIELD_SUBTREES;
1152        ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1153              write_each_non_note_until(NULL, &cb_data) ||
1154              tree_write_stack_finish_subtree(&root) ||
1155              write_object_file(root.buf.buf, root.buf.len, tree_type, result);
1156        strbuf_release(&root.buf);
1157        return ret;
1158}
1159
1160void prune_notes(struct notes_tree *t, int flags)
1161{
1162        struct note_delete_list *l = NULL;
1163
1164        if (!t)
1165                t = &default_notes_tree;
1166        assert(t->initialized);
1167
1168        for_each_note(t, 0, prune_notes_helper, &l);
1169
1170        while (l) {
1171                if (flags & NOTES_PRUNE_VERBOSE)
1172                        printf("%s\n", hash_to_hex(l->sha1));
1173                if (!(flags & NOTES_PRUNE_DRYRUN))
1174                        remove_note(t, l->sha1);
1175                l = l->next;
1176        }
1177}
1178
1179void free_notes(struct notes_tree *t)
1180{
1181        if (!t)
1182                t = &default_notes_tree;
1183        if (t->root)
1184                note_tree_free(t->root);
1185        free(t->root);
1186        while (t->first_non_note) {
1187                t->prev_non_note = t->first_non_note->next;
1188                free(t->first_non_note->path);
1189                free(t->first_non_note);
1190                t->first_non_note = t->prev_non_note;
1191        }
1192        free(t->ref);
1193        memset(t, 0, sizeof(struct notes_tree));
1194}
1195
1196/*
1197 * Fill the given strbuf with the notes associated with the given object.
1198 *
1199 * If the given notes_tree structure is not initialized, it will be auto-
1200 * initialized to the default value (see documentation for init_notes() above).
1201 * If the given notes_tree is NULL, the internal/default notes_tree will be
1202 * used instead.
1203 *
1204 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1205 * for human consumption.
1206 */
1207static void format_note(struct notes_tree *t, const struct object_id *object_oid,
1208                        struct strbuf *sb, const char *output_encoding, int raw)
1209{
1210        static const char utf8[] = "utf-8";
1211        const struct object_id *oid;
1212        char *msg, *msg_p;
1213        unsigned long linelen, msglen;
1214        enum object_type type;
1215
1216        if (!t)
1217                t = &default_notes_tree;
1218        if (!t->initialized)
1219                init_notes(t, NULL, NULL, 0);
1220
1221        oid = get_note(t, object_oid);
1222        if (!oid)
1223                return;
1224
1225        if (!(msg = read_object_file(oid, &type, &msglen)) || type != OBJ_BLOB) {
1226                free(msg);
1227                return;
1228        }
1229
1230        if (output_encoding && *output_encoding &&
1231            !is_encoding_utf8(output_encoding)) {
1232                char *reencoded = reencode_string(msg, output_encoding, utf8);
1233                if (reencoded) {
1234                        free(msg);
1235                        msg = reencoded;
1236                        msglen = strlen(msg);
1237                }
1238        }
1239
1240        /* we will end the annotation by a newline anyway */
1241        if (msglen && msg[msglen - 1] == '\n')
1242                msglen--;
1243
1244        if (!raw) {
1245                const char *ref = t->ref;
1246                if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1247                        strbuf_addstr(sb, "\nNotes:\n");
1248                } else {
1249                        if (starts_with(ref, "refs/"))
1250                                ref += 5;
1251                        if (starts_with(ref, "notes/"))
1252                                ref += 6;
1253                        strbuf_addf(sb, "\nNotes (%s):\n", ref);
1254                }
1255        }
1256
1257        for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1258                linelen = strchrnul(msg_p, '\n') - msg_p;
1259
1260                if (!raw)
1261                        strbuf_addstr(sb, "    ");
1262                strbuf_add(sb, msg_p, linelen);
1263                strbuf_addch(sb, '\n');
1264        }
1265
1266        free(msg);
1267}
1268
1269void format_display_notes(const struct object_id *object_oid,
1270                          struct strbuf *sb, const char *output_encoding, int raw)
1271{
1272        int i;
1273        assert(display_notes_trees);
1274        for (i = 0; display_notes_trees[i]; i++)
1275                format_note(display_notes_trees[i], object_oid, sb,
1276                            output_encoding, raw);
1277}
1278
1279int copy_note(struct notes_tree *t,
1280              const struct object_id *from_obj, const struct object_id *to_obj,
1281              int force, combine_notes_fn combine_notes)
1282{
1283        const struct object_id *note = get_note(t, from_obj);
1284        const struct object_id *existing_note = get_note(t, to_obj);
1285
1286        if (!force && existing_note)
1287                return 1;
1288
1289        if (note)
1290                return add_note(t, to_obj, note, combine_notes);
1291        else if (existing_note)
1292                return add_note(t, to_obj, &null_oid, combine_notes);
1293
1294        return 0;
1295}
1296
1297void expand_notes_ref(struct strbuf *sb)
1298{
1299        if (starts_with(sb->buf, "refs/notes/"))
1300                return; /* we're happy */
1301        else if (starts_with(sb->buf, "notes/"))
1302                strbuf_insert(sb, 0, "refs/", 5);
1303        else
1304                strbuf_insert(sb, 0, "refs/notes/", 11);
1305}
1306
1307void expand_loose_notes_ref(struct strbuf *sb)
1308{
1309        struct object_id object;
1310
1311        if (get_oid(sb->buf, &object)) {
1312                /* fallback to expand_notes_ref */
1313                expand_notes_ref(sb);
1314        }
1315}