8 #include "string-list.h"
12 * Use a non-balancing simple 16-tree structure with struct int_node as
13 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
14 * 16-array of pointers to its children.
15 * The bottom 2 bits of each pointer is used to identify the pointer type
16 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
17 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
18 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
19 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
21 * The root node is a statically allocated struct int_node.
28 * Leaf nodes come in two variants, note entries and subtree entries,
29 * distinguished by the LSb of the leaf node pointer (see above).
30 * As a note entry, the key is the SHA1 of the referenced object, and the
31 * value is the SHA1 of the note object.
32 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
33 * referenced object, using the last byte of the key to store the length of
34 * the prefix. The value is the SHA1 of the tree object containing the notes
38 struct object_id key_oid;
39 struct object_id val_oid;
43 * A notes tree may contain entries that are not notes, and that do not follow
44 * the naming conventions of notes. There are typically none/few of these, but
45 * we still need to keep track of them. Keep a simple linked list sorted alpha-
46 * betically on the non-note path. The list is populated when parsing tree
47 * objects in load_subtree(), and the non-notes are correctly written back into
48 * the tree objects produced by write_notes_tree().
51 struct non_note *next; /* grounded (last->next == NULL) */
57 #define PTR_TYPE_NULL 0
58 #define PTR_TYPE_INTERNAL 1
59 #define PTR_TYPE_NOTE 2
60 #define PTR_TYPE_SUBTREE 3
62 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
63 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
64 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
66 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
68 #define KEY_INDEX (GIT_SHA1_RAWSZ - 1)
69 #define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1)
70 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
71 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
73 struct notes_tree default_notes_tree;
75 static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
76 static struct notes_tree **display_notes_trees;
78 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
79 struct int_node *node, unsigned int n);
82 * Search the tree until the appropriate location for the given key is found:
83 * 1. Start at the root node, with n = 0
84 * 2. If a[0] at the current level is a matching subtree entry, unpack that
85 * subtree entry and remove it; restart search at the current level.
86 * 3. Use the nth nibble of the key as an index into a:
87 * - If a[n] is an int_node, recurse from #2 into that node and increment n
88 * - If a matching subtree entry, unpack that subtree entry (and remove it);
89 * restart search at the current level.
90 * - Otherwise, we have found one of the following:
91 * - a subtree entry which does not match the key
92 * - a note entry which may or may not match the key
93 * - an unused leaf node (NULL)
94 * In any case, set *tree and *n, and return pointer to the tree location.
96 static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
97 unsigned char *n, const unsigned char *key_sha1)
101 void *p = (*tree)->a[0];
103 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
104 l = (struct leaf_node *) CLR_PTR_TYPE(p);
105 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
106 /* unpack tree and resume search */
107 (*tree)->a[0] = NULL;
108 load_subtree(t, l, *tree, *n);
110 return note_tree_search(t, tree, n, key_sha1);
114 i = GET_NIBBLE(*n, key_sha1);
116 switch (GET_PTR_TYPE(p)) {
117 case PTR_TYPE_INTERNAL:
118 *tree = CLR_PTR_TYPE(p);
120 return note_tree_search(t, tree, n, key_sha1);
121 case PTR_TYPE_SUBTREE:
122 l = (struct leaf_node *) CLR_PTR_TYPE(p);
123 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
124 /* unpack tree and resume search */
125 (*tree)->a[i] = NULL;
126 load_subtree(t, l, *tree, *n);
128 return note_tree_search(t, tree, n, key_sha1);
132 return &((*tree)->a[i]);
137 * To find a leaf_node:
138 * Search to the tree location appropriate for the given key:
139 * If a note entry with matching key, return the note entry, else return NULL.
141 static struct leaf_node *note_tree_find(struct notes_tree *t,
142 struct int_node *tree, unsigned char n,
143 const unsigned char *key_sha1)
145 void **p = note_tree_search(t, &tree, &n, key_sha1);
146 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
147 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
148 if (!hashcmp(key_sha1, l->key_oid.hash))
155 * How to consolidate an int_node:
156 * If there are > 1 non-NULL entries, give up and return non-zero.
157 * Otherwise replace the int_node at the given index in the given parent node
158 * with the only NOTE entry (or a NULL entry if no entries) from the given
159 * tree, and return 0.
161 static int note_tree_consolidate(struct int_node *tree,
162 struct int_node *parent, unsigned char index)
167 assert(tree && parent);
168 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
170 for (i = 0; i < 16; i++) {
171 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
172 if (p) /* more than one entry */
178 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
180 /* replace tree with p in parent[index] */
181 parent->a[index] = p;
187 * To remove a leaf_node:
188 * Search to the tree location appropriate for the given leaf_node's key:
189 * - If location does not hold a matching entry, abort and do nothing.
190 * - Copy the matching entry's value into the given entry.
191 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
192 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
194 static void note_tree_remove(struct notes_tree *t,
195 struct int_node *tree, unsigned char n,
196 struct leaf_node *entry)
199 struct int_node *parent_stack[GIT_SHA1_RAWSZ];
201 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
203 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
204 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
205 return; /* type mismatch, nothing to remove */
206 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
207 if (oidcmp(&l->key_oid, &entry->key_oid))
208 return; /* key mismatch, nothing to remove */
210 /* we have found a matching entry */
211 oidcpy(&entry->val_oid, &l->val_oid);
213 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
215 /* consolidate this tree level, and parent levels, if possible */
217 return; /* cannot consolidate top level */
218 /* first, build stack of ancestors between root and current node */
219 parent_stack[0] = t->root;
220 for (i = 0; i < n; i++) {
221 j = GET_NIBBLE(i, entry->key_oid.hash);
222 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
224 assert(i == n && parent_stack[i] == tree);
225 /* next, unwind stack until note_tree_consolidate() is done */
227 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
228 GET_NIBBLE(i - 1, entry->key_oid.hash)))
233 * To insert a leaf_node:
234 * Search to the tree location appropriate for the given leaf_node's key:
235 * - If location is unused (NULL), store the tweaked pointer directly there
236 * - If location holds a note entry that matches the note-to-be-inserted, then
237 * combine the two notes (by calling the given combine_notes function).
238 * - If location holds a note entry that matches the subtree-to-be-inserted,
239 * then unpack the subtree-to-be-inserted into the location.
240 * - If location holds a matching subtree entry, unpack the subtree at that
241 * location, and restart the insert operation from that level.
242 * - Else, create a new int_node, holding both the node-at-location and the
243 * node-to-be-inserted, and store the new int_node into the location.
245 static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
246 unsigned char n, struct leaf_node *entry, unsigned char type,
247 combine_notes_fn combine_notes)
249 struct int_node *new_node;
251 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
254 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
255 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
256 switch (GET_PTR_TYPE(*p)) {
259 if (is_null_oid(&entry->val_oid))
262 *p = SET_PTR_TYPE(entry, type);
267 if (!oidcmp(&l->key_oid, &entry->key_oid)) {
268 /* skip concatenation if l == entry */
269 if (!oidcmp(&l->val_oid, &entry->val_oid))
272 ret = combine_notes(l->val_oid.hash,
273 entry->val_oid.hash);
274 if (!ret && is_null_oid(&l->val_oid))
275 note_tree_remove(t, tree, n, entry);
280 case PTR_TYPE_SUBTREE:
281 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
282 entry->key_oid.hash)) {
284 load_subtree(t, entry, tree, n);
291 case PTR_TYPE_SUBTREE:
292 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
293 /* unpack 'l' and restart insert */
295 load_subtree(t, l, tree, n);
297 return note_tree_insert(t, tree, n, entry, type,
303 /* non-matching leaf_node */
304 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
305 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
306 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
310 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
311 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
315 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
316 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
319 /* Free the entire notes data contained in the given tree */
320 static void note_tree_free(struct int_node *tree)
323 for (i = 0; i < 16; i++) {
324 void *p = tree->a[i];
325 switch (GET_PTR_TYPE(p)) {
326 case PTR_TYPE_INTERNAL:
327 note_tree_free(CLR_PTR_TYPE(p));
330 case PTR_TYPE_SUBTREE:
331 free(CLR_PTR_TYPE(p));
337 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
338 * - hex - Partial SHA1 segment in ASCII hex format
339 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
340 * - sha1 - Partial SHA1 value is written here
341 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
342 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
343 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
344 * Pads sha1 with NULs up to sha1_len (not included in returned length).
346 static int get_oid_hex_segment(const char *hex, unsigned int hex_len,
347 unsigned char *oid, unsigned int oid_len)
349 unsigned int i, len = hex_len >> 1;
350 if (hex_len % 2 != 0 || len > oid_len)
352 for (i = 0; i < len; i++) {
353 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
359 for (; i < oid_len; i++)
364 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
366 return strcmp(a->path, b->path);
369 /* note: takes ownership of path string */
370 static void add_non_note(struct notes_tree *t, char *path,
371 unsigned int mode, const unsigned char *sha1)
373 struct non_note *p = t->prev_non_note, *n;
374 n = (struct non_note *) xmalloc(sizeof(struct non_note));
378 hashcpy(n->oid.hash, sha1);
379 t->prev_non_note = n;
381 if (!t->first_non_note) {
382 t->first_non_note = n;
386 if (non_note_cmp(p, n) < 0)
388 else if (non_note_cmp(t->first_non_note, n) <= 0)
389 p = t->first_non_note;
391 /* n sorts before t->first_non_note */
392 n->next = t->first_non_note;
393 t->first_non_note = n;
397 /* n sorts equal or after p */
398 while (p->next && non_note_cmp(p->next, n) <= 0)
401 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
402 assert(strcmp(p->path, n->path) == 0);
404 oidcpy(&p->oid, &n->oid);
406 t->prev_non_note = p;
410 /* n sorts between p and p->next */
415 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
416 struct int_node *node, unsigned int n)
418 struct object_id object_oid;
419 unsigned int prefix_len;
421 struct tree_desc desc;
422 struct name_entry entry;
427 buf = fill_tree_descriptor(&desc, subtree->val_oid.hash);
429 die("Could not read %s for notes-index",
430 oid_to_hex(&subtree->val_oid));
432 prefix_len = subtree->key_oid.hash[KEY_INDEX];
433 assert(prefix_len * 2 >= n);
434 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
435 while (tree_entry(&desc, &entry)) {
436 path_len = strlen(entry.path);
437 len = get_oid_hex_segment(entry.path, path_len,
438 object_oid.hash + prefix_len, GIT_SHA1_RAWSZ - prefix_len);
440 goto handle_non_note; /* entry.path is not a SHA1 */
444 * If object SHA1 is complete (len == 20), assume note object
445 * If object SHA1 is incomplete (len < 20), and current
446 * component consists of 2 hex chars, assume note subtree
448 if (len <= GIT_SHA1_RAWSZ) {
449 type = PTR_TYPE_NOTE;
450 l = (struct leaf_node *)
451 xcalloc(1, sizeof(struct leaf_node));
452 oidcpy(&l->key_oid, &object_oid);
453 oidcpy(&l->val_oid, entry.oid);
454 if (len < GIT_SHA1_RAWSZ) {
455 if (!S_ISDIR(entry.mode) || path_len != 2)
456 goto handle_non_note; /* not subtree */
457 l->key_oid.hash[KEY_INDEX] = (unsigned char) len;
458 type = PTR_TYPE_SUBTREE;
460 if (note_tree_insert(t, node, n, l, type,
461 combine_notes_concatenate))
462 die("Failed to load %s %s into notes tree "
464 type == PTR_TYPE_NOTE ? "note" : "subtree",
465 oid_to_hex(&l->key_oid), t->ref);
471 * Determine full path for this non-note entry:
472 * The filename is already found in entry.path, but the
473 * directory part of the path must be deduced from the subtree
474 * containing this entry. We assume here that the overall notes
475 * tree follows a strict byte-based progressive fanout
476 * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
477 * e.g. 4/36 fanout). This means that if a non-note is found at
478 * path "dead/beef", the following code will register it as
479 * being found on "de/ad/beef".
480 * On the other hand, if you use such non-obvious non-note
481 * paths in the middle of a notes tree, you deserve what's
482 * coming to you ;). Note that for non-notes that are not
483 * SHA1-like at the top level, there will be no problems.
485 * To conclude, it is strongly advised to make sure non-notes
486 * have at least one non-hex character in the top-level path
490 struct strbuf non_note_path = STRBUF_INIT;
491 const char *q = oid_to_hex(&subtree->key_oid);
493 for (i = 0; i < prefix_len; i++) {
494 strbuf_addch(&non_note_path, *q++);
495 strbuf_addch(&non_note_path, *q++);
496 strbuf_addch(&non_note_path, '/');
498 strbuf_addstr(&non_note_path, entry.path);
499 add_non_note(t, strbuf_detach(&non_note_path, NULL),
500 entry.mode, entry.oid->hash);
507 * Determine optimal on-disk fanout for this part of the notes tree
509 * Given a (sub)tree and the level in the internal tree structure, determine
510 * whether or not the given existing fanout should be expanded for this
513 * Values of the 'fanout' variable:
514 * - 0: No fanout (all notes are stored directly in the root notes tree)
517 * - 3: 2/2/2/34 fanout
520 static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
521 unsigned char fanout)
524 * The following is a simple heuristic that works well in practice:
525 * For each even-numbered 16-tree level (remember that each on-disk
526 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
527 * entries at that tree level. If all of them are either int_nodes or
528 * subtree entries, then there are likely plenty of notes below this
529 * level, so we return an incremented fanout.
532 if ((n % 2) || (n > 2 * fanout))
534 for (i = 0; i < 16; i++) {
535 switch (GET_PTR_TYPE(tree->a[i])) {
536 case PTR_TYPE_SUBTREE:
537 case PTR_TYPE_INTERNAL:
546 /* hex SHA1 + 19 * '/' + NUL */
547 #define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1
549 static void construct_path_with_fanout(const unsigned char *sha1,
550 unsigned char fanout, char *path)
552 unsigned int i = 0, j = 0;
553 const char *hex_sha1 = sha1_to_hex(sha1);
554 assert(fanout < GIT_SHA1_RAWSZ);
556 path[i++] = hex_sha1[j++];
557 path[i++] = hex_sha1[j++];
561 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j);
564 static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
565 unsigned char n, unsigned char fanout, int flags,
566 each_note_fn fn, void *cb_data)
572 static char path[FANOUT_PATH_MAX];
574 fanout = determine_fanout(tree, n, fanout);
575 for (i = 0; i < 16; i++) {
578 switch (GET_PTR_TYPE(p)) {
579 case PTR_TYPE_INTERNAL:
580 /* recurse into int_node */
581 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
582 fanout, flags, fn, cb_data);
584 case PTR_TYPE_SUBTREE:
585 l = (struct leaf_node *) CLR_PTR_TYPE(p);
587 * Subtree entries in the note tree represent parts of
588 * the note tree that have not yet been explored. There
589 * is a direct relationship between subtree entries at
590 * level 'n' in the tree, and the 'fanout' variable:
591 * Subtree entries at level 'n <= 2 * fanout' should be
592 * preserved, since they correspond exactly to a fanout
593 * directory in the on-disk structure. However, subtree
594 * entries at level 'n > 2 * fanout' should NOT be
595 * preserved, but rather consolidated into the above
596 * notes tree level. We achieve this by unconditionally
597 * unpacking subtree entries that exist below the
598 * threshold level at 'n = 2 * fanout'.
600 if (n <= 2 * fanout &&
601 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
602 /* invoke callback with subtree */
603 unsigned int path_len =
604 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
605 assert(path_len < FANOUT_PATH_MAX - 1);
606 construct_path_with_fanout(l->key_oid.hash,
609 /* Create trailing slash, if needed */
610 if (path[path_len - 1] != '/')
611 path[path_len++] = '/';
612 path[path_len] = '\0';
613 ret = fn(&l->key_oid, &l->val_oid,
617 if (n > fanout * 2 ||
618 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
619 /* unpack subtree and resume traversal */
621 load_subtree(t, l, tree, n);
627 l = (struct leaf_node *) CLR_PTR_TYPE(p);
628 construct_path_with_fanout(l->key_oid.hash, fanout,
630 ret = fn(&l->key_oid, &l->val_oid, path,
640 struct tree_write_stack {
641 struct tree_write_stack *next;
643 char path[2]; /* path to subtree in next, if any */
646 static inline int matches_tree_write_stack(struct tree_write_stack *tws,
647 const char *full_path)
649 return full_path[0] == tws->path[0] &&
650 full_path[1] == tws->path[1] &&
654 static void write_tree_entry(struct strbuf *buf, unsigned int mode,
655 const char *path, unsigned int path_len, const
658 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
659 strbuf_add(buf, sha1, GIT_SHA1_RAWSZ);
662 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
665 struct tree_write_stack *n;
667 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
668 n = (struct tree_write_stack *)
669 xmalloc(sizeof(struct tree_write_stack));
671 strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */
672 n->path[0] = n->path[1] = '\0';
674 tws->path[0] = path[0];
675 tws->path[1] = path[1];
678 static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
681 struct tree_write_stack *n = tws->next;
684 ret = tree_write_stack_finish_subtree(n);
687 ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
690 strbuf_release(&n->buf);
693 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
694 tws->path[0] = tws->path[1] = '\0';
699 static int write_each_note_helper(struct tree_write_stack *tws,
700 const char *path, unsigned int mode,
701 const struct object_id *oid)
703 size_t path_len = strlen(path);
707 /* Determine common part of tree write stack */
708 while (tws && 3 * n < path_len &&
709 matches_tree_write_stack(tws, path + 3 * n)) {
714 /* tws point to last matching tree_write_stack entry */
715 ret = tree_write_stack_finish_subtree(tws);
719 /* Start subtrees needed to satisfy path */
720 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
721 tree_write_stack_init_subtree(tws, path + 3 * n);
726 /* There should be no more directory components in the given path */
727 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
729 /* Finally add given entry to the current tree object */
730 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
736 struct write_each_note_data {
737 struct tree_write_stack *root;
738 struct non_note *next_non_note;
741 static int write_each_non_note_until(const char *note_path,
742 struct write_each_note_data *d)
744 struct non_note *n = d->next_non_note;
746 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
747 if (note_path && cmp == 0)
748 ; /* do nothing, prefer note to non-note */
750 ret = write_each_note_helper(d->root, n->path, n->mode,
757 d->next_non_note = n;
761 static int write_each_note(const struct object_id *object_oid,
762 const struct object_id *note_oid, char *note_path,
765 struct write_each_note_data *d =
766 (struct write_each_note_data *) cb_data;
767 size_t note_path_len = strlen(note_path);
768 unsigned int mode = 0100644;
770 if (note_path[note_path_len - 1] == '/') {
773 note_path[note_path_len] = '\0';
776 assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS);
778 /* Weave non-note entries into note entries */
779 return write_each_non_note_until(note_path, d) ||
780 write_each_note_helper(d->root, note_path, mode, note_oid);
783 struct note_delete_list {
784 struct note_delete_list *next;
785 const unsigned char *sha1;
788 static int prune_notes_helper(const struct object_id *object_oid,
789 const struct object_id *note_oid, char *note_path,
792 struct note_delete_list **l = (struct note_delete_list **) cb_data;
793 struct note_delete_list *n;
795 if (has_object_file(object_oid))
796 return 0; /* nothing to do for this note */
798 /* failed to find object => prune this note */
799 n = (struct note_delete_list *) xmalloc(sizeof(*n));
801 n->sha1 = object_oid->hash;
806 int combine_notes_concatenate(unsigned char *cur_sha1,
807 const unsigned char *new_sha1)
809 char *cur_msg = NULL, *new_msg = NULL, *buf;
810 unsigned long cur_len, new_len, buf_len;
811 enum object_type cur_type, new_type;
814 /* read in both note blob objects */
815 if (!is_null_sha1(new_sha1))
816 new_msg = read_sha1_file(new_sha1, &new_type, &new_len);
817 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
821 if (!is_null_sha1(cur_sha1))
822 cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);
823 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
826 hashcpy(cur_sha1, new_sha1);
830 /* we will separate the notes by two newlines anyway */
831 if (cur_msg[cur_len - 1] == '\n')
834 /* concatenate cur_msg and new_msg into buf */
835 buf_len = cur_len + 2 + new_len;
836 buf = (char *) xmalloc(buf_len);
837 memcpy(buf, cur_msg, cur_len);
839 buf[cur_len + 1] = '\n';
840 memcpy(buf + cur_len + 2, new_msg, new_len);
844 /* create a new blob object from buf */
845 ret = write_sha1_file(buf, buf_len, blob_type, cur_sha1);
850 int combine_notes_overwrite(unsigned char *cur_sha1,
851 const unsigned char *new_sha1)
853 hashcpy(cur_sha1, new_sha1);
857 int combine_notes_ignore(unsigned char *cur_sha1,
858 const unsigned char *new_sha1)
864 * Add the lines from the named object to list, with trailing
867 static int string_list_add_note_lines(struct string_list *list,
868 const unsigned char *sha1)
874 if (is_null_sha1(sha1))
877 /* read_sha1_file NUL-terminates */
878 data = read_sha1_file(sha1, &t, &len);
879 if (t != OBJ_BLOB || !data || !len) {
881 return t != OBJ_BLOB || !data;
885 * If the last line of the file is EOL-terminated, this will
886 * add an empty string to the list. But it will be removed
887 * later, along with any empty strings that came from empty
888 * lines within the file.
890 string_list_split(list, data, '\n', -1);
895 static int string_list_join_lines_helper(struct string_list_item *item,
898 struct strbuf *buf = cb_data;
899 strbuf_addstr(buf, item->string);
900 strbuf_addch(buf, '\n');
904 int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,
905 const unsigned char *new_sha1)
907 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
908 struct strbuf buf = STRBUF_INIT;
911 /* read both note blob objects into unique_lines */
912 if (string_list_add_note_lines(&sort_uniq_list, cur_sha1))
914 if (string_list_add_note_lines(&sort_uniq_list, new_sha1))
916 string_list_remove_empty_items(&sort_uniq_list, 0);
917 string_list_sort(&sort_uniq_list);
918 string_list_remove_duplicates(&sort_uniq_list, 0);
920 /* create a new blob object from sort_uniq_list */
921 if (for_each_string_list(&sort_uniq_list,
922 string_list_join_lines_helper, &buf))
925 ret = write_sha1_file(buf.buf, buf.len, blob_type, cur_sha1);
928 strbuf_release(&buf);
929 string_list_clear(&sort_uniq_list, 0);
933 static int string_list_add_one_ref(const char *refname, const struct object_id *oid,
936 struct string_list *refs = cb;
937 if (!unsorted_string_list_has_string(refs, refname))
938 string_list_append(refs, refname);
943 * The list argument must have strdup_strings set on it.
945 void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
947 assert(list->strdup_strings);
948 if (has_glob_specials(glob)) {
949 for_each_glob_ref(string_list_add_one_ref, glob, list);
951 struct object_id oid;
952 if (get_oid(glob, &oid))
953 warning("notes ref %s is invalid", glob);
954 if (!unsorted_string_list_has_string(list, glob))
955 string_list_append(list, glob);
959 void string_list_add_refs_from_colon_sep(struct string_list *list,
962 struct string_list split = STRING_LIST_INIT_NODUP;
963 char *globs_copy = xstrdup(globs);
966 string_list_split_in_place(&split, globs_copy, ':', -1);
967 string_list_remove_empty_items(&split, 0);
969 for (i = 0; i < split.nr; i++)
970 string_list_add_refs_by_glob(list, split.items[i].string);
972 string_list_clear(&split, 0);
976 static int notes_display_config(const char *k, const char *v, void *cb)
980 if (*load_refs && !strcmp(k, "notes.displayref")) {
982 config_error_nonbool(k);
983 string_list_add_refs_by_glob(&display_notes_refs, v);
989 const char *default_notes_ref(void)
991 const char *notes_ref = NULL;
993 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
995 notes_ref = notes_ref_name; /* value of core.notesRef config */
997 notes_ref = GIT_NOTES_DEFAULT_REF;
1001 void init_notes(struct notes_tree *t, const char *notes_ref,
1002 combine_notes_fn combine_notes, int flags)
1004 struct object_id oid, object_oid;
1006 struct leaf_node root_tree;
1009 t = &default_notes_tree;
1010 assert(!t->initialized);
1013 notes_ref = default_notes_ref();
1016 combine_notes = combine_notes_concatenate;
1018 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
1019 t->first_non_note = NULL;
1020 t->prev_non_note = NULL;
1021 t->ref = xstrdup_or_null(notes_ref);
1022 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
1023 t->combine_notes = combine_notes;
1027 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
1028 get_sha1_treeish(notes_ref, object_oid.hash))
1030 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_oid.hash))
1031 die("Cannot use notes ref %s", notes_ref);
1032 if (get_tree_entry(object_oid.hash, "", oid.hash, &mode))
1033 die("Failed to read notes tree referenced by %s (%s)",
1034 notes_ref, oid_to_hex(&object_oid));
1036 oidclr(&root_tree.key_oid);
1037 oidcpy(&root_tree.val_oid, &oid);
1038 load_subtree(t, &root_tree, t->root, 0);
1041 struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
1043 struct string_list_item *item;
1045 struct notes_tree **trees;
1046 ALLOC_ARRAY(trees, refs->nr + 1);
1047 for_each_string_list_item(item, refs) {
1048 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
1049 init_notes(t, item->string, combine_notes_ignore, flags);
1050 trees[counter++] = t;
1052 trees[counter] = NULL;
1056 void init_display_notes(struct display_notes_opt *opt)
1058 char *display_ref_env;
1059 int load_config_refs = 0;
1060 display_notes_refs.strdup_strings = 1;
1062 assert(!display_notes_trees);
1064 if (!opt || opt->use_default_notes > 0 ||
1065 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1066 string_list_append(&display_notes_refs, default_notes_ref());
1067 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1068 if (display_ref_env) {
1069 string_list_add_refs_from_colon_sep(&display_notes_refs,
1071 load_config_refs = 0;
1073 load_config_refs = 1;
1076 git_config(notes_display_config, &load_config_refs);
1079 struct string_list_item *item;
1080 for_each_string_list_item(item, &opt->extra_notes_refs)
1081 string_list_add_refs_by_glob(&display_notes_refs,
1085 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
1086 string_list_clear(&display_notes_refs, 0);
1089 int add_note(struct notes_tree *t, const struct object_id *object_oid,
1090 const struct object_id *note_oid, combine_notes_fn combine_notes)
1092 struct leaf_node *l;
1095 t = &default_notes_tree;
1096 assert(t->initialized);
1099 combine_notes = t->combine_notes;
1100 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
1101 oidcpy(&l->key_oid, object_oid);
1102 oidcpy(&l->val_oid, note_oid);
1103 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
1106 int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1111 t = &default_notes_tree;
1112 assert(t->initialized);
1113 hashcpy(l.key_oid.hash, object_sha1);
1115 note_tree_remove(t, t->root, 0, &l);
1116 if (is_null_oid(&l.val_oid)) /* no note was removed */
1122 const struct object_id *get_note(struct notes_tree *t,
1123 const struct object_id *oid)
1125 struct leaf_node *found;
1128 t = &default_notes_tree;
1129 assert(t->initialized);
1130 found = note_tree_find(t, t->root, 0, oid->hash);
1131 return found ? &found->val_oid : NULL;
1134 int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1138 t = &default_notes_tree;
1139 assert(t->initialized);
1140 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
1143 int write_notes_tree(struct notes_tree *t, unsigned char *result)
1145 struct tree_write_stack root;
1146 struct write_each_note_data cb_data;
1150 t = &default_notes_tree;
1151 assert(t->initialized);
1153 /* Prepare for traversal of current notes tree */
1154 root.next = NULL; /* last forward entry in list is grounded */
1155 strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */
1156 root.path[0] = root.path[1] = '\0';
1157 cb_data.root = &root;
1158 cb_data.next_non_note = t->first_non_note;
1160 /* Write tree objects representing current notes tree */
1161 ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1162 FOR_EACH_NOTE_YIELD_SUBTREES,
1163 write_each_note, &cb_data) ||
1164 write_each_non_note_until(NULL, &cb_data) ||
1165 tree_write_stack_finish_subtree(&root) ||
1166 write_sha1_file(root.buf.buf, root.buf.len, tree_type, result);
1167 strbuf_release(&root.buf);
1171 void prune_notes(struct notes_tree *t, int flags)
1173 struct note_delete_list *l = NULL;
1176 t = &default_notes_tree;
1177 assert(t->initialized);
1179 for_each_note(t, 0, prune_notes_helper, &l);
1182 if (flags & NOTES_PRUNE_VERBOSE)
1183 printf("%s\n", sha1_to_hex(l->sha1));
1184 if (!(flags & NOTES_PRUNE_DRYRUN))
1185 remove_note(t, l->sha1);
1190 void free_notes(struct notes_tree *t)
1193 t = &default_notes_tree;
1195 note_tree_free(t->root);
1197 while (t->first_non_note) {
1198 t->prev_non_note = t->first_non_note->next;
1199 free(t->first_non_note->path);
1200 free(t->first_non_note);
1201 t->first_non_note = t->prev_non_note;
1204 memset(t, 0, sizeof(struct notes_tree));
1208 * Fill the given strbuf with the notes associated with the given object.
1210 * If the given notes_tree structure is not initialized, it will be auto-
1211 * initialized to the default value (see documentation for init_notes() above).
1212 * If the given notes_tree is NULL, the internal/default notes_tree will be
1215 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1216 * for human consumption.
1218 static void format_note(struct notes_tree *t, const struct object_id *object_oid,
1219 struct strbuf *sb, const char *output_encoding, int raw)
1221 static const char utf8[] = "utf-8";
1222 const struct object_id *oid;
1224 unsigned long linelen, msglen;
1225 enum object_type type;
1228 t = &default_notes_tree;
1229 if (!t->initialized)
1230 init_notes(t, NULL, NULL, 0);
1232 oid = get_note(t, object_oid);
1236 if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) {
1241 if (output_encoding && *output_encoding &&
1242 !is_encoding_utf8(output_encoding)) {
1243 char *reencoded = reencode_string(msg, output_encoding, utf8);
1247 msglen = strlen(msg);
1251 /* we will end the annotation by a newline anyway */
1252 if (msglen && msg[msglen - 1] == '\n')
1256 const char *ref = t->ref;
1257 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1258 strbuf_addstr(sb, "\nNotes:\n");
1260 if (starts_with(ref, "refs/"))
1262 if (starts_with(ref, "notes/"))
1264 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1268 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1269 linelen = strchrnul(msg_p, '\n') - msg_p;
1272 strbuf_addstr(sb, " ");
1273 strbuf_add(sb, msg_p, linelen);
1274 strbuf_addch(sb, '\n');
1280 void format_display_notes(const struct object_id *object_oid,
1281 struct strbuf *sb, const char *output_encoding, int raw)
1284 assert(display_notes_trees);
1285 for (i = 0; display_notes_trees[i]; i++)
1286 format_note(display_notes_trees[i], object_oid, sb,
1287 output_encoding, raw);
1290 int copy_note(struct notes_tree *t,
1291 const struct object_id *from_obj, const struct object_id *to_obj,
1292 int force, combine_notes_fn combine_notes)
1294 const struct object_id *note = get_note(t, from_obj);
1295 const struct object_id *existing_note = get_note(t, to_obj);
1297 if (!force && existing_note)
1301 return add_note(t, to_obj, note, combine_notes);
1302 else if (existing_note)
1303 return add_note(t, to_obj, &null_oid, combine_notes);
1308 void expand_notes_ref(struct strbuf *sb)
1310 if (starts_with(sb->buf, "refs/notes/"))
1311 return; /* we're happy */
1312 else if (starts_with(sb->buf, "notes/"))
1313 strbuf_insert(sb, 0, "refs/", 5);
1315 strbuf_insert(sb, 0, "refs/notes/", 11);
1318 void expand_loose_notes_ref(struct strbuf *sb)
1320 struct object_id object;
1322 if (get_oid(sb->buf, &object)) {
1323 /* fallback to expand_notes_ref */
1324 expand_notes_ref(sb);