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