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