cocci: remove 'unused.cocci'
[git.git] / notes.c
blob45fb7f22d1df011396ec86e56f1f1bd3bd1645b9
1 #include "cache.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "hex.h"
5 #include "notes.h"
6 #include "object-store.h"
7 #include "blob.h"
8 #include "tree.h"
9 #include "utf8.h"
10 #include "strbuf.h"
11 #include "tree-walk.h"
12 #include "string-list.h"
13 #include "refs.h"
16 * Use a non-balancing simple 16-tree structure with struct int_node as
17 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
18 * 16-array of pointers to its children.
19 * The bottom 2 bits of each pointer is used to identify the pointer type
20 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
21 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
22 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
23 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
25 * The root node is a statically allocated struct int_node.
27 struct int_node {
28 void *a[16];
32 * Leaf nodes come in two variants, note entries and subtree entries,
33 * distinguished by the LSb of the leaf node pointer (see above).
34 * As a note entry, the key is the SHA1 of the referenced object, and the
35 * value is the SHA1 of the note object.
36 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
37 * referenced object, using the last byte of the key to store the length of
38 * the prefix. The value is the SHA1 of the tree object containing the notes
39 * subtree.
41 struct leaf_node {
42 struct object_id key_oid;
43 struct object_id val_oid;
47 * A notes tree may contain entries that are not notes, and that do not follow
48 * the naming conventions of notes. There are typically none/few of these, but
49 * we still need to keep track of them. Keep a simple linked list sorted alpha-
50 * betically on the non-note path. The list is populated when parsing tree
51 * objects in load_subtree(), and the non-notes are correctly written back into
52 * the tree objects produced by write_notes_tree().
54 struct non_note {
55 struct non_note *next; /* grounded (last->next == NULL) */
56 char *path;
57 unsigned int mode;
58 struct object_id oid;
61 #define PTR_TYPE_NULL 0
62 #define PTR_TYPE_INTERNAL 1
63 #define PTR_TYPE_NOTE 2
64 #define PTR_TYPE_SUBTREE 3
66 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
67 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
68 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
70 #define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
72 #define KEY_INDEX (the_hash_algo->rawsz - 1)
73 #define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
74 #define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
75 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
76 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
78 struct notes_tree default_notes_tree;
80 static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
81 static struct notes_tree **display_notes_trees;
83 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
84 struct int_node *node, unsigned int n);
87 * Search the tree until the appropriate location for the given key is found:
88 * 1. Start at the root node, with n = 0
89 * 2. If a[0] at the current level is a matching subtree entry, unpack that
90 * subtree entry and remove it; restart search at the current level.
91 * 3. Use the nth nibble of the key as an index into a:
92 * - If a[n] is an int_node, recurse from #2 into that node and increment n
93 * - If a matching subtree entry, unpack that subtree entry (and remove it);
94 * restart search at the current level.
95 * - Otherwise, we have found one of the following:
96 * - a subtree entry which does not match the key
97 * - a note entry which may or may not match the key
98 * - an unused leaf node (NULL)
99 * In any case, set *tree and *n, and return pointer to the tree location.
101 static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
102 unsigned char *n, const unsigned char *key_sha1)
104 struct leaf_node *l;
105 unsigned char i;
106 void *p = (*tree)->a[0];
108 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
109 l = (struct leaf_node *) CLR_PTR_TYPE(p);
110 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
111 /* unpack tree and resume search */
112 (*tree)->a[0] = NULL;
113 load_subtree(t, l, *tree, *n);
114 free(l);
115 return note_tree_search(t, tree, n, key_sha1);
119 i = GET_NIBBLE(*n, key_sha1);
120 p = (*tree)->a[i];
121 switch (GET_PTR_TYPE(p)) {
122 case PTR_TYPE_INTERNAL:
123 *tree = CLR_PTR_TYPE(p);
124 (*n)++;
125 return note_tree_search(t, tree, n, key_sha1);
126 case PTR_TYPE_SUBTREE:
127 l = (struct leaf_node *) CLR_PTR_TYPE(p);
128 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
129 /* unpack tree and resume search */
130 (*tree)->a[i] = NULL;
131 load_subtree(t, l, *tree, *n);
132 free(l);
133 return note_tree_search(t, tree, n, key_sha1);
135 /* fall through */
136 default:
137 return &((*tree)->a[i]);
142 * To find a leaf_node:
143 * Search to the tree location appropriate for the given key:
144 * If a note entry with matching key, return the note entry, else return NULL.
146 static struct leaf_node *note_tree_find(struct notes_tree *t,
147 struct int_node *tree, unsigned char n,
148 const unsigned char *key_sha1)
150 void **p = note_tree_search(t, &tree, &n, key_sha1);
151 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
152 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
153 if (hasheq(key_sha1, l->key_oid.hash))
154 return l;
156 return NULL;
160 * How to consolidate an int_node:
161 * If there are > 1 non-NULL entries, give up and return non-zero.
162 * Otherwise replace the int_node at the given index in the given parent node
163 * with the only NOTE entry (or a NULL entry if no entries) from the given
164 * tree, and return 0.
166 static int note_tree_consolidate(struct int_node *tree,
167 struct int_node *parent, unsigned char index)
169 unsigned int i;
170 void *p = NULL;
172 assert(tree && parent);
173 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
175 for (i = 0; i < 16; i++) {
176 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
177 if (p) /* more than one entry */
178 return -2;
179 p = tree->a[i];
183 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
184 return -2;
185 /* replace tree with p in parent[index] */
186 parent->a[index] = p;
187 free(tree);
188 return 0;
192 * To remove a leaf_node:
193 * Search to the tree location appropriate for the given leaf_node's key:
194 * - If location does not hold a matching entry, abort and do nothing.
195 * - Copy the matching entry's value into the given entry.
196 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
197 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
199 static void note_tree_remove(struct notes_tree *t,
200 struct int_node *tree, unsigned char n,
201 struct leaf_node *entry)
203 struct leaf_node *l;
204 struct int_node *parent_stack[GIT_MAX_RAWSZ];
205 unsigned char i, j;
206 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
208 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
209 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
210 return; /* type mismatch, nothing to remove */
211 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
212 if (!oideq(&l->key_oid, &entry->key_oid))
213 return; /* key mismatch, nothing to remove */
215 /* we have found a matching entry */
216 oidcpy(&entry->val_oid, &l->val_oid);
217 free(l);
218 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
220 /* consolidate this tree level, and parent levels, if possible */
221 if (!n)
222 return; /* cannot consolidate top level */
223 /* first, build stack of ancestors between root and current node */
224 parent_stack[0] = t->root;
225 for (i = 0; i < n; i++) {
226 j = GET_NIBBLE(i, entry->key_oid.hash);
227 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
229 assert(i == n && parent_stack[i] == tree);
230 /* next, unwind stack until note_tree_consolidate() is done */
231 while (i > 0 &&
232 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
233 GET_NIBBLE(i - 1, entry->key_oid.hash)))
234 i--;
238 * To insert a leaf_node:
239 * Search to the tree location appropriate for the given leaf_node's key:
240 * - If location is unused (NULL), store the tweaked pointer directly there
241 * - If location holds a note entry that matches the note-to-be-inserted, then
242 * combine the two notes (by calling the given combine_notes function).
243 * - If location holds a note entry that matches the subtree-to-be-inserted,
244 * then unpack the subtree-to-be-inserted into the location.
245 * - If location holds a matching subtree entry, unpack the subtree at that
246 * location, and restart the insert operation from that level.
247 * - Else, create a new int_node, holding both the node-at-location and the
248 * node-to-be-inserted, and store the new int_node into the location.
250 static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
251 unsigned char n, struct leaf_node *entry, unsigned char type,
252 combine_notes_fn combine_notes)
254 struct int_node *new_node;
255 struct leaf_node *l;
256 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
257 int ret = 0;
259 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
260 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
261 switch (GET_PTR_TYPE(*p)) {
262 case PTR_TYPE_NULL:
263 assert(!*p);
264 if (is_null_oid(&entry->val_oid))
265 free(entry);
266 else
267 *p = SET_PTR_TYPE(entry, type);
268 return 0;
269 case PTR_TYPE_NOTE:
270 switch (type) {
271 case PTR_TYPE_NOTE:
272 if (oideq(&l->key_oid, &entry->key_oid)) {
273 /* skip concatenation if l == entry */
274 if (oideq(&l->val_oid, &entry->val_oid)) {
275 free(entry);
276 return 0;
279 ret = combine_notes(&l->val_oid,
280 &entry->val_oid);
281 if (!ret && is_null_oid(&l->val_oid))
282 note_tree_remove(t, tree, n, entry);
283 free(entry);
284 return ret;
286 break;
287 case PTR_TYPE_SUBTREE:
288 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
289 entry->key_oid.hash)) {
290 /* unpack 'entry' */
291 load_subtree(t, entry, tree, n);
292 free(entry);
293 return 0;
295 break;
297 break;
298 case PTR_TYPE_SUBTREE:
299 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
300 /* unpack 'l' and restart insert */
301 *p = NULL;
302 load_subtree(t, l, tree, n);
303 free(l);
304 return note_tree_insert(t, tree, n, entry, type,
305 combine_notes);
307 break;
310 /* non-matching leaf_node */
311 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
312 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
313 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
314 free(entry);
315 return 0;
317 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
318 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
319 combine_notes);
320 if (ret)
321 return ret;
322 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
323 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
326 /* Free the entire notes data contained in the given tree */
327 static void note_tree_free(struct int_node *tree)
329 unsigned int i;
330 for (i = 0; i < 16; i++) {
331 void *p = tree->a[i];
332 switch (GET_PTR_TYPE(p)) {
333 case PTR_TYPE_INTERNAL:
334 note_tree_free(CLR_PTR_TYPE(p));
335 /* fall through */
336 case PTR_TYPE_NOTE:
337 case PTR_TYPE_SUBTREE:
338 free(CLR_PTR_TYPE(p));
343 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
345 return strcmp(a->path, b->path);
348 /* note: takes ownership of path string */
349 static void add_non_note(struct notes_tree *t, char *path,
350 unsigned int mode, const unsigned char *sha1)
352 struct non_note *p = t->prev_non_note, *n;
353 n = (struct non_note *) xmalloc(sizeof(struct non_note));
354 n->next = NULL;
355 n->path = path;
356 n->mode = mode;
357 oidread(&n->oid, sha1);
358 t->prev_non_note = n;
360 if (!t->first_non_note) {
361 t->first_non_note = n;
362 return;
365 if (non_note_cmp(p, n) < 0)
366 ; /* do nothing */
367 else if (non_note_cmp(t->first_non_note, n) <= 0)
368 p = t->first_non_note;
369 else {
370 /* n sorts before t->first_non_note */
371 n->next = t->first_non_note;
372 t->first_non_note = n;
373 return;
376 /* n sorts equal or after p */
377 while (p->next && non_note_cmp(p->next, n) <= 0)
378 p = p->next;
380 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
381 assert(strcmp(p->path, n->path) == 0);
382 p->mode = n->mode;
383 oidcpy(&p->oid, &n->oid);
384 free(n);
385 t->prev_non_note = p;
386 return;
389 /* n sorts between p and p->next */
390 n->next = p->next;
391 p->next = n;
394 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
395 struct int_node *node, unsigned int n)
397 struct object_id object_oid;
398 size_t prefix_len;
399 void *buf;
400 struct tree_desc desc;
401 struct name_entry entry;
402 const unsigned hashsz = the_hash_algo->rawsz;
404 buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
405 if (!buf)
406 die("Could not read %s for notes-index",
407 oid_to_hex(&subtree->val_oid));
409 prefix_len = subtree->key_oid.hash[KEY_INDEX];
410 if (prefix_len >= hashsz)
411 BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
412 if (prefix_len * 2 < n)
413 BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
414 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
415 while (tree_entry(&desc, &entry)) {
416 unsigned char type;
417 struct leaf_node *l;
418 size_t path_len = strlen(entry.path);
420 if (path_len == 2 * (hashsz - prefix_len)) {
421 /* This is potentially the remainder of the SHA-1 */
423 if (!S_ISREG(entry.mode))
424 /* notes must be blobs */
425 goto handle_non_note;
427 if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
428 hashsz - prefix_len))
429 goto handle_non_note; /* entry.path is not a SHA1 */
431 type = PTR_TYPE_NOTE;
432 } else if (path_len == 2) {
433 /* This is potentially an internal node */
434 size_t len = prefix_len;
436 if (!S_ISDIR(entry.mode))
437 /* internal nodes must be trees */
438 goto handle_non_note;
440 if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
441 goto handle_non_note; /* entry.path is not a SHA1 */
444 * Pad the rest of the SHA-1 with zeros,
445 * except for the last byte, where we write
446 * the length:
448 memset(object_oid.hash + len, 0, hashsz - len - 1);
449 object_oid.hash[KEY_INDEX] = (unsigned char)len;
451 type = PTR_TYPE_SUBTREE;
452 } else {
453 /* This can't be part of a note */
454 goto handle_non_note;
457 CALLOC_ARRAY(l, 1);
458 oidcpy(&l->key_oid, &object_oid);
459 oidcpy(&l->val_oid, &entry.oid);
460 oid_set_algo(&l->key_oid, the_hash_algo);
461 oid_set_algo(&l->val_oid, the_hash_algo);
462 if (note_tree_insert(t, node, n, l, type,
463 combine_notes_concatenate))
464 die("Failed to load %s %s into notes tree "
465 "from %s",
466 type == PTR_TYPE_NOTE ? "note" : "subtree",
467 oid_to_hex(&object_oid), t->ref);
469 continue;
471 handle_non_note:
473 * Determine full path for this non-note entry. The
474 * filename is already found in entry.path, but the
475 * directory part of the path must be deduced from the
476 * subtree containing this entry based on our
477 * knowledge that the overall notes tree follows a
478 * strict byte-based progressive fanout structure
479 * (i.e. using 2/38, 2/2/36, etc. fanouts).
482 struct strbuf non_note_path = STRBUF_INIT;
483 const char *q = oid_to_hex(&subtree->key_oid);
484 size_t i;
485 for (i = 0; i < prefix_len; i++) {
486 strbuf_addch(&non_note_path, *q++);
487 strbuf_addch(&non_note_path, *q++);
488 strbuf_addch(&non_note_path, '/');
490 strbuf_addstr(&non_note_path, entry.path);
491 oid_set_algo(&entry.oid, the_hash_algo);
492 add_non_note(t, strbuf_detach(&non_note_path, NULL),
493 entry.mode, entry.oid.hash);
496 free(buf);
500 * Determine optimal on-disk fanout for this part of the notes tree
502 * Given a (sub)tree and the level in the internal tree structure, determine
503 * whether or not the given existing fanout should be expanded for this
504 * (sub)tree.
506 * Values of the 'fanout' variable:
507 * - 0: No fanout (all notes are stored directly in the root notes tree)
508 * - 1: 2/38 fanout
509 * - 2: 2/2/36 fanout
510 * - 3: 2/2/2/34 fanout
511 * etc.
513 static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
514 unsigned char fanout)
517 * The following is a simple heuristic that works well in practice:
518 * For each even-numbered 16-tree level (remember that each on-disk
519 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
520 * entries at that tree level. If all of them are either int_nodes or
521 * subtree entries, then there are likely plenty of notes below this
522 * level, so we return an incremented fanout.
524 unsigned int i;
525 if ((n % 2) || (n > 2 * fanout))
526 return fanout;
527 for (i = 0; i < 16; i++) {
528 switch (GET_PTR_TYPE(tree->a[i])) {
529 case PTR_TYPE_SUBTREE:
530 case PTR_TYPE_INTERNAL:
531 continue;
532 default:
533 return fanout;
536 return fanout + 1;
539 /* hex oid + '/' between each pair of hex digits + NUL */
540 #define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
542 static void construct_path_with_fanout(const unsigned char *hash,
543 unsigned char fanout, char *path)
545 unsigned int i = 0, j = 0;
546 const char *hex_hash = hash_to_hex(hash);
547 assert(fanout < the_hash_algo->rawsz);
548 while (fanout) {
549 path[i++] = hex_hash[j++];
550 path[i++] = hex_hash[j++];
551 path[i++] = '/';
552 fanout--;
554 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
557 static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
558 unsigned char n, unsigned char fanout, int flags,
559 each_note_fn fn, void *cb_data)
561 unsigned int i;
562 void *p;
563 int ret = 0;
564 struct leaf_node *l;
565 static char path[FANOUT_PATH_MAX];
567 fanout = determine_fanout(tree, n, fanout);
568 for (i = 0; i < 16; i++) {
569 redo:
570 p = tree->a[i];
571 switch (GET_PTR_TYPE(p)) {
572 case PTR_TYPE_INTERNAL:
573 /* recurse into int_node */
574 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
575 fanout, flags, fn, cb_data);
576 break;
577 case PTR_TYPE_SUBTREE:
578 l = (struct leaf_node *) CLR_PTR_TYPE(p);
580 * Subtree entries in the note tree represent parts of
581 * the note tree that have not yet been explored. There
582 * is a direct relationship between subtree entries at
583 * level 'n' in the tree, and the 'fanout' variable:
584 * Subtree entries at level 'n < 2 * fanout' should be
585 * preserved, since they correspond exactly to a fanout
586 * directory in the on-disk structure. However, subtree
587 * entries at level 'n >= 2 * fanout' should NOT be
588 * preserved, but rather consolidated into the above
589 * notes tree level. We achieve this by unconditionally
590 * unpacking subtree entries that exist below the
591 * threshold level at 'n = 2 * fanout'.
593 if (n < 2 * fanout &&
594 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
595 /* invoke callback with subtree */
596 unsigned int path_len =
597 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
598 assert(path_len < FANOUT_PATH_MAX - 1);
599 construct_path_with_fanout(l->key_oid.hash,
600 fanout,
601 path);
602 /* Create trailing slash, if needed */
603 if (path[path_len - 1] != '/')
604 path[path_len++] = '/';
605 path[path_len] = '\0';
606 ret = fn(&l->key_oid, &l->val_oid,
607 path,
608 cb_data);
610 if (n >= 2 * fanout ||
611 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
612 /* unpack subtree and resume traversal */
613 tree->a[i] = NULL;
614 load_subtree(t, l, tree, n);
615 free(l);
616 goto redo;
618 break;
619 case PTR_TYPE_NOTE:
620 l = (struct leaf_node *) CLR_PTR_TYPE(p);
621 construct_path_with_fanout(l->key_oid.hash, fanout,
622 path);
623 ret = fn(&l->key_oid, &l->val_oid, path,
624 cb_data);
625 break;
627 if (ret)
628 return ret;
630 return 0;
633 struct tree_write_stack {
634 struct tree_write_stack *next;
635 struct strbuf buf;
636 char path[2]; /* path to subtree in next, if any */
639 static inline int matches_tree_write_stack(struct tree_write_stack *tws,
640 const char *full_path)
642 return full_path[0] == tws->path[0] &&
643 full_path[1] == tws->path[1] &&
644 full_path[2] == '/';
647 static void write_tree_entry(struct strbuf *buf, unsigned int mode,
648 const char *path, unsigned int path_len, const
649 unsigned char *hash)
651 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
652 strbuf_add(buf, hash, the_hash_algo->rawsz);
655 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
656 const char *path)
658 struct tree_write_stack *n;
659 assert(!tws->next);
660 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
661 n = (struct tree_write_stack *)
662 xmalloc(sizeof(struct tree_write_stack));
663 n->next = NULL;
664 strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
665 n->path[0] = n->path[1] = '\0';
666 tws->next = n;
667 tws->path[0] = path[0];
668 tws->path[1] = path[1];
671 static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
673 int ret;
674 struct tree_write_stack *n = tws->next;
675 struct object_id s;
676 if (n) {
677 ret = tree_write_stack_finish_subtree(n);
678 if (ret)
679 return ret;
680 ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
681 if (ret)
682 return ret;
683 strbuf_release(&n->buf);
684 free(n);
685 tws->next = NULL;
686 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
687 tws->path[0] = tws->path[1] = '\0';
689 return 0;
692 static int write_each_note_helper(struct tree_write_stack *tws,
693 const char *path, unsigned int mode,
694 const struct object_id *oid)
696 size_t path_len = strlen(path);
697 unsigned int n = 0;
698 int ret;
700 /* Determine common part of tree write stack */
701 while (tws && 3 * n < path_len &&
702 matches_tree_write_stack(tws, path + 3 * n)) {
703 n++;
704 tws = tws->next;
707 /* tws point to last matching tree_write_stack entry */
708 ret = tree_write_stack_finish_subtree(tws);
709 if (ret)
710 return ret;
712 /* Start subtrees needed to satisfy path */
713 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
714 tree_write_stack_init_subtree(tws, path + 3 * n);
715 n++;
716 tws = tws->next;
719 /* There should be no more directory components in the given path */
720 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
722 /* Finally add given entry to the current tree object */
723 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
724 oid->hash);
726 return 0;
729 struct write_each_note_data {
730 struct tree_write_stack *root;
731 struct non_note **nn_list;
732 struct non_note *nn_prev;
735 static int write_each_non_note_until(const char *note_path,
736 struct write_each_note_data *d)
738 struct non_note *p = d->nn_prev;
739 struct non_note *n = p ? p->next : *d->nn_list;
740 int cmp = 0, ret;
741 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
742 if (note_path && cmp == 0)
743 ; /* do nothing, prefer note to non-note */
744 else {
745 ret = write_each_note_helper(d->root, n->path, n->mode,
746 &n->oid);
747 if (ret)
748 return ret;
750 p = n;
751 n = n->next;
753 d->nn_prev = p;
754 return 0;
757 static int write_each_note(const struct object_id *object_oid UNUSED,
758 const struct object_id *note_oid, char *note_path,
759 void *cb_data)
761 struct write_each_note_data *d =
762 (struct write_each_note_data *) cb_data;
763 size_t note_path_len = strlen(note_path);
764 unsigned int mode = 0100644;
766 if (note_path[note_path_len - 1] == '/') {
767 /* subtree entry */
768 note_path_len--;
769 note_path[note_path_len] = '\0';
770 mode = 040000;
772 assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
774 /* Weave non-note entries into note entries */
775 return write_each_non_note_until(note_path, d) ||
776 write_each_note_helper(d->root, note_path, mode, note_oid);
779 struct note_delete_list {
780 struct note_delete_list *next;
781 const unsigned char *sha1;
784 static int prune_notes_helper(const struct object_id *object_oid,
785 const struct object_id *note_oid UNUSED,
786 char *note_path UNUSED,
787 void *cb_data)
789 struct note_delete_list **l = (struct note_delete_list **) cb_data;
790 struct note_delete_list *n;
792 if (repo_has_object_file(the_repository, object_oid))
793 return 0; /* nothing to do for this note */
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;
803 int combine_notes_concatenate(struct object_id *cur_oid,
804 const struct object_id *new_oid)
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;
811 /* read in both note blob objects */
812 if (!is_null_oid(new_oid))
813 new_msg = repo_read_object_file(the_repository, new_oid,
814 &new_type, &new_len);
815 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
816 free(new_msg);
817 return 0;
819 if (!is_null_oid(cur_oid))
820 cur_msg = repo_read_object_file(the_repository, cur_oid,
821 &cur_type, &cur_len);
822 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
823 free(cur_msg);
824 free(new_msg);
825 oidcpy(cur_oid, new_oid);
826 return 0;
829 /* we will separate the notes by two newlines anyway */
830 if (cur_msg[cur_len - 1] == '\n')
831 cur_len--;
833 /* concatenate cur_msg and new_msg into buf */
834 buf_len = cur_len + 2 + new_len;
835 buf = (char *) xmalloc(buf_len);
836 memcpy(buf, cur_msg, cur_len);
837 buf[cur_len] = '\n';
838 buf[cur_len + 1] = '\n';
839 memcpy(buf + cur_len + 2, new_msg, new_len);
840 free(cur_msg);
841 free(new_msg);
843 /* create a new blob object from buf */
844 ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
845 free(buf);
846 return ret;
849 int combine_notes_overwrite(struct object_id *cur_oid,
850 const struct object_id *new_oid)
852 oidcpy(cur_oid, new_oid);
853 return 0;
856 int combine_notes_ignore(struct object_id *cur_oid UNUSED,
857 const struct object_id *new_oid UNUSED)
859 return 0;
863 * Add the lines from the named object to list, with trailing
864 * newlines removed.
866 static int string_list_add_note_lines(struct string_list *list,
867 const struct object_id *oid)
869 char *data;
870 unsigned long len;
871 enum object_type t;
873 if (is_null_oid(oid))
874 return 0;
876 /* read_sha1_file NUL-terminates */
877 data = repo_read_object_file(the_repository, oid, &t, &len);
878 if (t != OBJ_BLOB || !data || !len) {
879 free(data);
880 return t != OBJ_BLOB || !data;
884 * If the last line of the file is EOL-terminated, this will
885 * add an empty string to the list. But it will be removed
886 * later, along with any empty strings that came from empty
887 * lines within the file.
889 string_list_split(list, data, '\n', -1);
890 free(data);
891 return 0;
894 static int string_list_join_lines_helper(struct string_list_item *item,
895 void *cb_data)
897 struct strbuf *buf = cb_data;
898 strbuf_addstr(buf, item->string);
899 strbuf_addch(buf, '\n');
900 return 0;
903 int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
904 const struct object_id *new_oid)
906 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
907 struct strbuf buf = STRBUF_INIT;
908 int ret = 1;
910 /* read both note blob objects into unique_lines */
911 if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
912 goto out;
913 if (string_list_add_note_lines(&sort_uniq_list, new_oid))
914 goto out;
915 string_list_remove_empty_items(&sort_uniq_list, 0);
916 string_list_sort(&sort_uniq_list);
917 string_list_remove_duplicates(&sort_uniq_list, 0);
919 /* create a new blob object from sort_uniq_list */
920 if (for_each_string_list(&sort_uniq_list,
921 string_list_join_lines_helper, &buf))
922 goto out;
924 ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
926 out:
927 strbuf_release(&buf);
928 string_list_clear(&sort_uniq_list, 0);
929 return ret;
932 static int string_list_add_one_ref(const char *refname,
933 const struct object_id *oid UNUSED,
934 int flag UNUSED, void *cb)
936 struct string_list *refs = cb;
937 if (!unsorted_string_list_has_string(refs, refname))
938 string_list_append(refs, refname);
939 return 0;
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);
950 } else {
951 struct object_id oid;
952 if (repo_get_oid(the_repository, 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,
960 const char *globs)
962 struct string_list split = STRING_LIST_INIT_NODUP;
963 char *globs_copy = xstrdup(globs);
964 int i;
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);
973 free(globs_copy);
976 static int notes_display_config(const char *k, const char *v, void *cb)
978 int *load_refs = cb;
980 if (*load_refs && !strcmp(k, "notes.displayref")) {
981 if (!v)
982 return config_error_nonbool(k);
983 string_list_add_refs_by_glob(&display_notes_refs, v);
986 return 0;
989 const char *default_notes_ref(void)
991 const char *notes_ref = NULL;
992 if (!notes_ref)
993 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
994 if (!notes_ref)
995 notes_ref = notes_ref_name; /* value of core.notesRef config */
996 if (!notes_ref)
997 notes_ref = GIT_NOTES_DEFAULT_REF;
998 return notes_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;
1005 unsigned short mode;
1006 struct leaf_node root_tree;
1008 if (!t)
1009 t = &default_notes_tree;
1010 assert(!t->initialized);
1012 if (!notes_ref)
1013 notes_ref = default_notes_ref();
1014 update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
1016 if (!combine_notes)
1017 combine_notes = combine_notes_concatenate;
1019 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
1020 t->first_non_note = NULL;
1021 t->prev_non_note = NULL;
1022 t->ref = xstrdup_or_null(notes_ref);
1023 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
1024 t->combine_notes = combine_notes;
1025 t->initialized = 1;
1026 t->dirty = 0;
1028 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
1029 repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
1030 return;
1031 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
1032 die("Cannot use notes ref %s", notes_ref);
1033 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
1034 die("Failed to read notes tree referenced by %s (%s)",
1035 notes_ref, oid_to_hex(&object_oid));
1037 oidclr(&root_tree.key_oid);
1038 oidcpy(&root_tree.val_oid, &oid);
1039 load_subtree(t, &root_tree, t->root, 0);
1042 struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
1044 struct string_list_item *item;
1045 int counter = 0;
1046 struct notes_tree **trees;
1047 ALLOC_ARRAY(trees, refs->nr + 1);
1048 for_each_string_list_item(item, refs) {
1049 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
1050 init_notes(t, item->string, combine_notes_ignore, flags);
1051 trees[counter++] = t;
1053 trees[counter] = NULL;
1054 return trees;
1057 void init_display_notes(struct display_notes_opt *opt)
1059 memset(opt, 0, sizeof(*opt));
1060 opt->use_default_notes = -1;
1063 void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
1065 opt->use_default_notes = 1;
1066 *show_notes = 1;
1069 void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1070 const char *ref) {
1071 struct strbuf buf = STRBUF_INIT;
1072 strbuf_addstr(&buf, ref);
1073 expand_notes_ref(&buf);
1074 string_list_append(&opt->extra_notes_refs,
1075 strbuf_detach(&buf, NULL));
1076 *show_notes = 1;
1079 void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1081 opt->use_default_notes = -1;
1082 /* we have been strdup'ing ourselves, so trick
1083 * string_list into free()ing strings */
1084 opt->extra_notes_refs.strdup_strings = 1;
1085 string_list_clear(&opt->extra_notes_refs, 0);
1086 opt->extra_notes_refs.strdup_strings = 0;
1087 *show_notes = 0;
1090 void load_display_notes(struct display_notes_opt *opt)
1092 char *display_ref_env;
1093 int load_config_refs = 0;
1094 display_notes_refs.strdup_strings = 1;
1096 assert(!display_notes_trees);
1098 if (!opt || opt->use_default_notes > 0 ||
1099 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1100 string_list_append(&display_notes_refs, default_notes_ref());
1101 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1102 if (display_ref_env) {
1103 string_list_add_refs_from_colon_sep(&display_notes_refs,
1104 display_ref_env);
1105 load_config_refs = 0;
1106 } else
1107 load_config_refs = 1;
1110 git_config(notes_display_config, &load_config_refs);
1112 if (opt) {
1113 struct string_list_item *item;
1114 for_each_string_list_item(item, &opt->extra_notes_refs)
1115 string_list_add_refs_by_glob(&display_notes_refs,
1116 item->string);
1119 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
1120 string_list_clear(&display_notes_refs, 0);
1123 int add_note(struct notes_tree *t, const struct object_id *object_oid,
1124 const struct object_id *note_oid, combine_notes_fn combine_notes)
1126 struct leaf_node *l;
1128 if (!t)
1129 t = &default_notes_tree;
1130 assert(t->initialized);
1131 t->dirty = 1;
1132 if (!combine_notes)
1133 combine_notes = t->combine_notes;
1134 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
1135 oidcpy(&l->key_oid, object_oid);
1136 oidcpy(&l->val_oid, note_oid);
1137 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
1140 int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1142 struct leaf_node l;
1144 if (!t)
1145 t = &default_notes_tree;
1146 assert(t->initialized);
1147 oidread(&l.key_oid, object_sha1);
1148 oidclr(&l.val_oid);
1149 note_tree_remove(t, t->root, 0, &l);
1150 if (is_null_oid(&l.val_oid)) /* no note was removed */
1151 return 1;
1152 t->dirty = 1;
1153 return 0;
1156 const struct object_id *get_note(struct notes_tree *t,
1157 const struct object_id *oid)
1159 struct leaf_node *found;
1161 if (!t)
1162 t = &default_notes_tree;
1163 assert(t->initialized);
1164 found = note_tree_find(t, t->root, 0, oid->hash);
1165 return found ? &found->val_oid : NULL;
1168 int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1169 void *cb_data)
1171 if (!t)
1172 t = &default_notes_tree;
1173 assert(t->initialized);
1174 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
1177 int write_notes_tree(struct notes_tree *t, struct object_id *result)
1179 struct tree_write_stack root;
1180 struct write_each_note_data cb_data;
1181 int ret;
1182 int flags;
1184 if (!t)
1185 t = &default_notes_tree;
1186 assert(t->initialized);
1188 /* Prepare for traversal of current notes tree */
1189 root.next = NULL; /* last forward entry in list is grounded */
1190 strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
1191 root.path[0] = root.path[1] = '\0';
1192 cb_data.root = &root;
1193 cb_data.nn_list = &(t->first_non_note);
1194 cb_data.nn_prev = NULL;
1196 /* Write tree objects representing current notes tree */
1197 flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1198 FOR_EACH_NOTE_YIELD_SUBTREES;
1199 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1200 write_each_non_note_until(NULL, &cb_data) ||
1201 tree_write_stack_finish_subtree(&root) ||
1202 write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
1203 strbuf_release(&root.buf);
1204 return ret;
1207 void prune_notes(struct notes_tree *t, int flags)
1209 struct note_delete_list *l = NULL;
1211 if (!t)
1212 t = &default_notes_tree;
1213 assert(t->initialized);
1215 for_each_note(t, 0, prune_notes_helper, &l);
1217 while (l) {
1218 if (flags & NOTES_PRUNE_VERBOSE)
1219 printf("%s\n", hash_to_hex(l->sha1));
1220 if (!(flags & NOTES_PRUNE_DRYRUN))
1221 remove_note(t, l->sha1);
1222 l = l->next;
1226 void free_notes(struct notes_tree *t)
1228 if (!t)
1229 t = &default_notes_tree;
1230 if (t->root)
1231 note_tree_free(t->root);
1232 free(t->root);
1233 while (t->first_non_note) {
1234 t->prev_non_note = t->first_non_note->next;
1235 free(t->first_non_note->path);
1236 free(t->first_non_note);
1237 t->first_non_note = t->prev_non_note;
1239 free(t->ref);
1240 memset(t, 0, sizeof(struct notes_tree));
1244 * Fill the given strbuf with the notes associated with the given object.
1246 * If the given notes_tree structure is not initialized, it will be auto-
1247 * initialized to the default value (see documentation for init_notes() above).
1248 * If the given notes_tree is NULL, the internal/default notes_tree will be
1249 * used instead.
1251 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1252 * for human consumption.
1254 static void format_note(struct notes_tree *t, const struct object_id *object_oid,
1255 struct strbuf *sb, const char *output_encoding, int raw)
1257 static const char utf8[] = "utf-8";
1258 const struct object_id *oid;
1259 char *msg, *msg_p;
1260 unsigned long linelen, msglen;
1261 enum object_type type;
1263 if (!t)
1264 t = &default_notes_tree;
1265 if (!t->initialized)
1266 init_notes(t, NULL, NULL, 0);
1268 oid = get_note(t, object_oid);
1269 if (!oid)
1270 return;
1272 if (!(msg = repo_read_object_file(the_repository, oid, &type, &msglen)) || type != OBJ_BLOB) {
1273 free(msg);
1274 return;
1277 if (output_encoding && *output_encoding &&
1278 !is_encoding_utf8(output_encoding)) {
1279 char *reencoded = reencode_string(msg, output_encoding, utf8);
1280 if (reencoded) {
1281 free(msg);
1282 msg = reencoded;
1283 msglen = strlen(msg);
1287 /* we will end the annotation by a newline anyway */
1288 if (msglen && msg[msglen - 1] == '\n')
1289 msglen--;
1291 if (!raw) {
1292 const char *ref = t->ref;
1293 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1294 strbuf_addstr(sb, "\nNotes:\n");
1295 } else {
1296 skip_prefix(ref, "refs/", &ref);
1297 skip_prefix(ref, "notes/", &ref);
1298 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1302 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1303 linelen = strchrnul(msg_p, '\n') - msg_p;
1305 if (!raw)
1306 strbuf_addstr(sb, " ");
1307 strbuf_add(sb, msg_p, linelen);
1308 strbuf_addch(sb, '\n');
1311 free(msg);
1314 void format_display_notes(const struct object_id *object_oid,
1315 struct strbuf *sb, const char *output_encoding, int raw)
1317 int i;
1318 assert(display_notes_trees);
1319 for (i = 0; display_notes_trees[i]; i++)
1320 format_note(display_notes_trees[i], object_oid, sb,
1321 output_encoding, raw);
1324 int copy_note(struct notes_tree *t,
1325 const struct object_id *from_obj, const struct object_id *to_obj,
1326 int force, combine_notes_fn combine_notes)
1328 const struct object_id *note = get_note(t, from_obj);
1329 const struct object_id *existing_note = get_note(t, to_obj);
1331 if (!force && existing_note)
1332 return 1;
1334 if (note)
1335 return add_note(t, to_obj, note, combine_notes);
1336 else if (existing_note)
1337 return add_note(t, to_obj, null_oid(), combine_notes);
1339 return 0;
1342 void expand_notes_ref(struct strbuf *sb)
1344 if (starts_with(sb->buf, "refs/notes/"))
1345 return; /* we're happy */
1346 else if (starts_with(sb->buf, "notes/"))
1347 strbuf_insertstr(sb, 0, "refs/");
1348 else
1349 strbuf_insertstr(sb, 0, "refs/notes/");
1352 void expand_loose_notes_ref(struct strbuf *sb)
1354 struct object_id object;
1356 if (repo_get_oid(the_repository, sb->buf, &object)) {
1357 /* fallback to expand_notes_ref */
1358 expand_notes_ref(sb);