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