]> git.ipfire.org Git - thirdparty/git.git/blame - notes.c
treewide: remove unnecessary cache.h inclusion
[thirdparty/git.git] / notes.c
CommitLineData
a97a7468 1#include "cache.h"
b2141fc1 2#include "config.h"
32a8f510 3#include "environment.h"
41771fa4 4#include "hex.h"
a97a7468 5#include "notes.h"
cbd53a21 6#include "object-store.h"
73f464b5 7#include "blob.h"
61a7cca0 8#include "tree.h"
a97a7468
JS
9#include "utf8.h"
10#include "strbuf.h"
fd53c9eb 11#include "tree-walk.h"
894a9d33
TR
12#include "string-list.h"
13#include "refs.h"
fd53c9eb 14
23123aec
JH
15/*
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 *
24 *
25 * The root node is a statically allocated struct int_node.
26 */
27struct int_node {
28 void *a[16];
fd53c9eb
JS
29};
30
23123aec
JH
31/*
32 * Leaf nodes come in two variants, note entries and subtree entries,
33 * distinguished by the LSb of the leaf node pointer (see above).
a7e7eff6 34 * As a note entry, the key is the SHA1 of the referenced object, and the
23123aec
JH
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
a7e7eff6 37 * referenced object, using the last byte of the key to store the length of
23123aec
JH
38 * the prefix. The value is the SHA1 of the tree object containing the notes
39 * subtree.
40 */
41struct leaf_node {
5dcc969e 42 struct object_id key_oid;
43 struct object_id val_oid;
fd53c9eb 44};
a97a7468 45
851c2b37
JH
46/*
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().
53 */
54struct non_note {
55 struct non_note *next; /* grounded (last->next == NULL) */
56 char *path;
57 unsigned int mode;
5dcc969e 58 struct object_id oid;
851c2b37
JH
59};
60
23123aec
JH
61#define PTR_TYPE_NULL 0
62#define PTR_TYPE_INTERNAL 1
63#define PTR_TYPE_NOTE 2
64#define PTR_TYPE_SUBTREE 3
fd53c9eb 65
23123aec
JH
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)))
fd53c9eb 69
65eb8e0c 70#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
fd53c9eb 71
dd437451 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)
23123aec 75#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
89c149f5 76 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
fd53c9eb 77
cd305392 78struct notes_tree default_notes_tree;
23123aec 79
2721ce21 80static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
894a9d33
TR
81static struct notes_tree **display_notes_trees;
82
851c2b37
JH
83static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
84 struct int_node *node, unsigned int n);
23123aec
JH
85
86/*
ef8db638 87 * Search the tree until the appropriate location for the given key is found:
23123aec 88 * 1. Start at the root node, with n = 0
ef8db638
JH
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
23123aec
JH
93 * - If a matching subtree entry, unpack that subtree entry (and remove it);
94 * restart search at the current level.
ef8db638
JH
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.
23123aec 100 */
851c2b37 101static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
ef8db638 102 unsigned char *n, const unsigned char *key_sha1)
23123aec
JH
103{
104 struct leaf_node *l;
ef8db638
JH
105 unsigned char i;
106 void *p = (*tree)->a[0];
23123aec 107
ef8db638
JH
108 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
109 l = (struct leaf_node *) CLR_PTR_TYPE(p);
5dcc969e 110 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
ef8db638
JH
111 /* unpack tree and resume search */
112 (*tree)->a[0] = NULL;
851c2b37 113 load_subtree(t, l, *tree, *n);
ef8db638 114 free(l);
851c2b37 115 return note_tree_search(t, tree, n, key_sha1);
ef8db638
JH
116 }
117 }
118
119 i = GET_NIBBLE(*n, key_sha1);
120 p = (*tree)->a[i];
0ab1faae 121 switch (GET_PTR_TYPE(p)) {
23123aec 122 case PTR_TYPE_INTERNAL:
ef8db638
JH
123 *tree = CLR_PTR_TYPE(p);
124 (*n)++;
851c2b37 125 return note_tree_search(t, tree, n, key_sha1);
23123aec
JH
126 case PTR_TYPE_SUBTREE:
127 l = (struct leaf_node *) CLR_PTR_TYPE(p);
5dcc969e 128 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
23123aec 129 /* unpack tree and resume search */
ef8db638 130 (*tree)->a[i] = NULL;
851c2b37 131 load_subtree(t, l, *tree, *n);
23123aec 132 free(l);
851c2b37 133 return note_tree_search(t, tree, n, key_sha1);
23123aec 134 }
ef8db638 135 /* fall through */
23123aec 136 default:
ef8db638 137 return &((*tree)->a[i]);
fd53c9eb 138 }
ef8db638 139}
23123aec 140
ef8db638
JH
141/*
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.
145 */
851c2b37
JH
146static struct leaf_node *note_tree_find(struct notes_tree *t,
147 struct int_node *tree, unsigned char n,
ef8db638
JH
148 const unsigned char *key_sha1)
149{
851c2b37 150 void **p = note_tree_search(t, &tree, &n, key_sha1);
ef8db638
JH
151 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
152 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
e3ff0683 153 if (hasheq(key_sha1, l->key_oid.hash))
ef8db638 154 return l;
23123aec
JH
155 }
156 return NULL;
fd53c9eb
JS
157}
158
1ec666b0
JH
159/*
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
5a8e7c34
MH
163 * with the only NOTE entry (or a NULL entry if no entries) from the given
164 * tree, and return 0.
1ec666b0
JH
165 */
166static int note_tree_consolidate(struct int_node *tree,
167 struct int_node *parent, unsigned char index)
168{
169 unsigned int i;
170 void *p = NULL;
171
172 assert(tree && parent);
173 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
174
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];
180 }
181 }
182
5a8e7c34
MH
183 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
184 return -2;
1ec666b0
JH
185 /* replace tree with p in parent[index] */
186 parent->a[index] = p;
187 free(tree);
188 return 0;
189}
190
191/*
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.
1ee1e43d 195 * - Copy the matching entry's value into the given entry.
1ec666b0
JH
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.
198 */
1ee1e43d
JH
199static void note_tree_remove(struct notes_tree *t,
200 struct int_node *tree, unsigned char n,
201 struct leaf_node *entry)
1ec666b0
JH
202{
203 struct leaf_node *l;
dd437451 204 struct int_node *parent_stack[GIT_MAX_RAWSZ];
1ec666b0 205 unsigned char i, j;
5dcc969e 206 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
1ec666b0
JH
207
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);
9001dc2a 212 if (!oideq(&l->key_oid, &entry->key_oid))
1ec666b0
JH
213 return; /* key mismatch, nothing to remove */
214
215 /* we have found a matching entry */
5dcc969e 216 oidcpy(&entry->val_oid, &l->val_oid);
1ec666b0
JH
217 free(l);
218 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
219
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 */
cd305392 224 parent_stack[0] = t->root;
1ec666b0 225 for (i = 0; i < n; i++) {
5dcc969e 226 j = GET_NIBBLE(i, entry->key_oid.hash);
1ec666b0
JH
227 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
228 }
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],
5dcc969e 233 GET_NIBBLE(i - 1, entry->key_oid.hash)))
1ec666b0
JH
234 i--;
235}
236
23123aec
JH
237/*
238 * To insert a leaf_node:
ef8db638
JH
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
73f464b5 242 * combine the two notes (by calling the given combine_notes function).
ef8db638
JH
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.
23123aec 249 */
180619a5 250static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
851c2b37 251 unsigned char n, struct leaf_node *entry, unsigned char type,
73f464b5 252 combine_notes_fn combine_notes)
fd53c9eb 253{
23123aec 254 struct int_node *new_node;
ef8db638 255 struct leaf_node *l;
5dcc969e 256 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
180619a5 257 int ret = 0;
ef8db638
JH
258
259 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
260 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
0ab1faae 261 switch (GET_PTR_TYPE(*p)) {
23123aec 262 case PTR_TYPE_NULL:
ef8db638 263 assert(!*p);
5dcc969e 264 if (is_null_oid(&entry->val_oid))
e2656c82
JH
265 free(entry);
266 else
267 *p = SET_PTR_TYPE(entry, type);
180619a5 268 return 0;
ef8db638
JH
269 case PTR_TYPE_NOTE:
270 switch (type) {
271 case PTR_TYPE_NOTE:
4a7e27e9 272 if (oideq(&l->key_oid, &entry->key_oid)) {
ef8db638 273 /* skip concatenation if l == entry */
779ad664
MH
274 if (oideq(&l->val_oid, &entry->val_oid)) {
275 free(entry);
180619a5 276 return 0;
779ad664 277 }
ef8db638 278
b7d591d1
PO
279 ret = combine_notes(&l->val_oid,
280 &entry->val_oid);
5dcc969e 281 if (!ret && is_null_oid(&l->val_oid))
e2656c82 282 note_tree_remove(t, tree, n, entry);
ef8db638 283 free(entry);
180619a5 284 return ret;
ef8db638
JH
285 }
286 break;
287 case PTR_TYPE_SUBTREE:
5dcc969e 288 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
289 entry->key_oid.hash)) {
ef8db638 290 /* unpack 'entry' */
851c2b37 291 load_subtree(t, entry, tree, n);
ef8db638 292 free(entry);
180619a5 293 return 0;
ef8db638
JH
294 }
295 break;
296 }
297 break;
298 case PTR_TYPE_SUBTREE:
5dcc969e 299 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
ef8db638
JH
300 /* unpack 'l' and restart insert */
301 *p = NULL;
851c2b37 302 load_subtree(t, l, tree, n);
ef8db638 303 free(l);
180619a5
JH
304 return note_tree_insert(t, tree, n, entry, type,
305 combine_notes);
23123aec 306 }
ef8db638 307 break;
fd53c9eb 308 }
ef8db638
JH
309
310 /* non-matching leaf_node */
311 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
312 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
5dcc969e 313 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
e2656c82 314 free(entry);
180619a5 315 return 0;
e2656c82 316 }
65bbf082 317 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
180619a5
JH
318 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
319 combine_notes);
320 if (ret)
321 return ret;
ef8db638 322 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
180619a5 323 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
23123aec 324}
fd53c9eb 325
23123aec
JH
326/* Free the entire notes data contained in the given tree */
327static void note_tree_free(struct int_node *tree)
328{
329 unsigned int i;
330 for (i = 0; i < 16; i++) {
331 void *p = tree->a[i];
0ab1faae 332 switch (GET_PTR_TYPE(p)) {
23123aec
JH
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));
339 }
fd53c9eb 340 }
23123aec 341}
fd53c9eb 342
851c2b37
JH
343static int non_note_cmp(const struct non_note *a, const struct non_note *b)
344{
345 return strcmp(a->path, b->path);
346}
347
c29edfef
JK
348/* note: takes ownership of path string */
349static void add_non_note(struct notes_tree *t, char *path,
851c2b37
JH
350 unsigned int mode, const unsigned char *sha1)
351{
352 struct non_note *p = t->prev_non_note, *n;
353 n = (struct non_note *) xmalloc(sizeof(struct non_note));
354 n->next = NULL;
c29edfef 355 n->path = path;
851c2b37 356 n->mode = mode;
92e2cab9 357 oidread(&n->oid, sha1);
851c2b37
JH
358 t->prev_non_note = n;
359
360 if (!t->first_non_note) {
361 t->first_non_note = n;
362 return;
363 }
364
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;
374 }
375
376 /* n sorts equal or after p */
377 while (p->next && non_note_cmp(p->next, n) <= 0)
378 p = p->next;
379
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;
5dcc969e 383 oidcpy(&p->oid, &n->oid);
851c2b37
JH
384 free(n);
385 t->prev_non_note = p;
386 return;
387 }
388
389 /* n sorts between p and p->next */
390 n->next = p->next;
391 p->next = n;
392}
393
394static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
395 struct int_node *node, unsigned int n)
fd53c9eb 396{
89c149f5 397 struct object_id object_oid;
06cfa756 398 size_t prefix_len;
23123aec 399 void *buf;
fd53c9eb
JS
400 struct tree_desc desc;
401 struct name_entry entry;
dd437451 402 const unsigned hashsz = the_hash_algo->rawsz;
23123aec 403
5e575807 404 buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
23123aec
JH
405 if (!buf)
406 die("Could not read %s for notes-index",
5dcc969e 407 oid_to_hex(&subtree->val_oid));
23123aec 408
89c149f5 409 prefix_len = subtree->key_oid.hash[KEY_INDEX];
dd437451 410 if (prefix_len >= hashsz)
39642815
MH
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);
89c149f5 414 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
23123aec 415 while (tree_entry(&desc, &entry)) {
a2816392
MH
416 unsigned char type;
417 struct leaf_node *l;
06cfa756 418 size_t path_len = strlen(entry.path);
98c9897d 419
dd437451 420 if (path_len == 2 * (hashsz - prefix_len)) {
98c9897d 421 /* This is potentially the remainder of the SHA-1 */
40432187
MH
422
423 if (!S_ISREG(entry.mode))
424 /* notes must be blobs */
425 goto handle_non_note;
426
cfdc88f1 427 if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
dd437451 428 hashsz - prefix_len))
98c9897d 429 goto handle_non_note; /* entry.path is not a SHA1 */
23123aec 430
851c2b37 431 type = PTR_TYPE_NOTE;
98c9897d
MH
432 } else if (path_len == 2) {
433 /* This is potentially an internal node */
d49852d6 434 size_t len = prefix_len;
4d589b87
MH
435
436 if (!S_ISDIR(entry.mode))
437 /* internal nodes must be trees */
438 goto handle_non_note;
439
cfdc88f1 440 if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
98c9897d 441 goto handle_non_note; /* entry.path is not a SHA1 */
a2816392 442
d49852d6
MH
443 /*
444 * Pad the rest of the SHA-1 with zeros,
445 * except for the last byte, where we write
446 * the length:
447 */
dd437451 448 memset(object_oid.hash + len, 0, hashsz - len - 1);
d49852d6 449 object_oid.hash[KEY_INDEX] = (unsigned char)len;
4ebef533 450
d3b0c6be 451 type = PTR_TYPE_SUBTREE;
98c9897d
MH
452 } else {
453 /* This can't be part of a note */
454 goto handle_non_note;
23123aec 455 }
98c9897d 456
ca56dadb 457 CALLOC_ARRAY(l, 1);
4ebef533 458 oidcpy(&l->key_oid, &object_oid);
ea82b2a0 459 oidcpy(&l->val_oid, &entry.oid);
5a6dce70 460 oid_set_algo(&l->key_oid, the_hash_algo);
461 oid_set_algo(&l->val_oid, the_hash_algo);
d3b0c6be
MH
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",
60fe477a 467 oid_to_hex(&object_oid), t->ref);
d3b0c6be 468
851c2b37
JH
469 continue;
470
471handle_non_note:
472 /*
cbeed9aa
MH
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).
851c2b37
JH
480 */
481 {
c29edfef 482 struct strbuf non_note_path = STRBUF_INIT;
5dcc969e 483 const char *q = oid_to_hex(&subtree->key_oid);
06cfa756 484 size_t i;
851c2b37 485 for (i = 0; i < prefix_len; i++) {
c29edfef
JK
486 strbuf_addch(&non_note_path, *q++);
487 strbuf_addch(&non_note_path, *q++);
488 strbuf_addch(&non_note_path, '/');
851c2b37 489 }
c29edfef 490 strbuf_addstr(&non_note_path, entry.path);
5a6dce70 491 oid_set_algo(&entry.oid, the_hash_algo);
c29edfef 492 add_non_note(t, strbuf_detach(&non_note_path, NULL),
ea82b2a0 493 entry.mode, entry.oid.hash);
851c2b37 494 }
23123aec
JH
495 }
496 free(buf);
497}
498
73f77b90
JH
499/*
500 * Determine optimal on-disk fanout for this part of the notes tree
501 *
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.
505 *
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.
512 */
513static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
514 unsigned char fanout)
515{
516 /*
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.
523 */
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;
534 }
535 }
536 return fanout + 1;
537}
538
dd437451 539/* hex oid + '/' between each pair of hex digits + NUL */
540#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
02e32b7d 541
0dbc6462 542static void construct_path_with_fanout(const unsigned char *hash,
73f77b90
JH
543 unsigned char fanout, char *path)
544{
545 unsigned int i = 0, j = 0;
0dbc6462 546 const char *hex_hash = hash_to_hex(hash);
dd437451 547 assert(fanout < the_hash_algo->rawsz);
73f77b90 548 while (fanout) {
0dbc6462 549 path[i++] = hex_hash[j++];
550 path[i++] = hex_hash[j++];
73f77b90
JH
551 path[i++] = '/';
552 fanout--;
553 }
0dbc6462 554 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
73f77b90
JH
555}
556
851c2b37
JH
557static 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)
73f77b90
JH
560{
561 unsigned int i;
562 void *p;
563 int ret = 0;
564 struct leaf_node *l;
02e32b7d 565 static char path[FANOUT_PATH_MAX];
73f77b90
JH
566
567 fanout = determine_fanout(tree, n, fanout);
568 for (i = 0; i < 16; i++) {
569redo:
570 p = tree->a[i];
571 switch (GET_PTR_TYPE(p)) {
572 case PTR_TYPE_INTERNAL:
573 /* recurse into int_node */
851c2b37 574 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
73f77b90
JH
575 fanout, flags, fn, cb_data);
576 break;
577 case PTR_TYPE_SUBTREE:
578 l = (struct leaf_node *) CLR_PTR_TYPE(p);
579 /*
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:
dbc27477 584 * Subtree entries at level 'n < 2 * fanout' should be
73f77b90
JH
585 * preserved, since they correspond exactly to a fanout
586 * directory in the on-disk structure. However, subtree
dbc27477 587 * entries at level 'n >= 2 * fanout' should NOT be
73f77b90
JH
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'.
592 */
dbc27477 593 if (n < 2 * fanout &&
73f77b90
JH
594 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
595 /* invoke callback with subtree */
596 unsigned int path_len =
89c149f5 597 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
02e32b7d 598 assert(path_len < FANOUT_PATH_MAX - 1);
5dcc969e 599 construct_path_with_fanout(l->key_oid.hash,
600 fanout,
73f77b90
JH
601 path);
602 /* Create trailing slash, if needed */
603 if (path[path_len - 1] != '/')
604 path[path_len++] = '/';
605 path[path_len] = '\0';
490bc83a 606 ret = fn(&l->key_oid, &l->val_oid,
5dcc969e 607 path,
73f77b90
JH
608 cb_data);
609 }
dbc27477 610 if (n >= 2 * fanout ||
73f77b90
JH
611 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
612 /* unpack subtree and resume traversal */
613 tree->a[i] = NULL;
851c2b37 614 load_subtree(t, l, tree, n);
73f77b90
JH
615 free(l);
616 goto redo;
617 }
618 break;
619 case PTR_TYPE_NOTE:
620 l = (struct leaf_node *) CLR_PTR_TYPE(p);
5dcc969e 621 construct_path_with_fanout(l->key_oid.hash, fanout,
622 path);
490bc83a 623 ret = fn(&l->key_oid, &l->val_oid, path,
5dcc969e 624 cb_data);
73f77b90
JH
625 break;
626 }
627 if (ret)
628 return ret;
629 }
630 return 0;
631}
632
61a7cca0
JH
633struct tree_write_stack {
634 struct tree_write_stack *next;
635 struct strbuf buf;
636 char path[2]; /* path to subtree in next, if any */
637};
638
639static inline int matches_tree_write_stack(struct tree_write_stack *tws,
640 const char *full_path)
641{
642 return full_path[0] == tws->path[0] &&
643 full_path[1] == tws->path[1] &&
644 full_path[2] == '/';
645}
646
647static void write_tree_entry(struct strbuf *buf, unsigned int mode,
648 const char *path, unsigned int path_len, const
dd437451 649 unsigned char *hash)
61a7cca0 650{
c88f0cc7 651 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
dd437451 652 strbuf_add(buf, hash, the_hash_algo->rawsz);
61a7cca0
JH
653}
654
655static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
656 const char *path)
657{
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;
dd437451 664 strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
61a7cca0
JH
665 n->path[0] = n->path[1] = '\0';
666 tws->next = n;
667 tws->path[0] = path[0];
668 tws->path[1] = path[1];
669}
670
671static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
672{
673 int ret;
674 struct tree_write_stack *n = tws->next;
89c149f5 675 struct object_id s;
61a7cca0
JH
676 if (n) {
677 ret = tree_write_stack_finish_subtree(n);
678 if (ret)
679 return ret;
c80d226a 680 ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
61a7cca0
JH
681 if (ret)
682 return ret;
683 strbuf_release(&n->buf);
684 free(n);
685 tws->next = NULL;
89c149f5 686 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
61a7cca0
JH
687 tws->path[0] = tws->path[1] = '\0';
688 }
689 return 0;
690}
691
692static int write_each_note_helper(struct tree_write_stack *tws,
693 const char *path, unsigned int mode,
490bc83a 694 const struct object_id *oid)
61a7cca0
JH
695{
696 size_t path_len = strlen(path);
697 unsigned int n = 0;
698 int ret;
699
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;
705 }
706
707 /* tws point to last matching tree_write_stack entry */
708 ret = tree_write_stack_finish_subtree(tws);
709 if (ret)
710 return ret;
711
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;
717 }
718
719 /* There should be no more directory components in the given path */
720 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
721
722 /* Finally add given entry to the current tree object */
723 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
490bc83a 724 oid->hash);
61a7cca0
JH
725
726 return 0;
727}
728
729struct write_each_note_data {
730 struct tree_write_stack *root;
dbc27477
JH
731 struct non_note **nn_list;
732 struct non_note *nn_prev;
61a7cca0
JH
733};
734
851c2b37
JH
735static int write_each_non_note_until(const char *note_path,
736 struct write_each_note_data *d)
737{
dbc27477
JH
738 struct non_note *p = d->nn_prev;
739 struct non_note *n = p ? p->next : *d->nn_list;
89fe121d 740 int cmp = 0, ret;
851c2b37
JH
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,
490bc83a 746 &n->oid);
851c2b37
JH
747 if (ret)
748 return ret;
749 }
dbc27477 750 p = n;
851c2b37
JH
751 n = n->next;
752 }
dbc27477 753 d->nn_prev = p;
851c2b37
JH
754 return 0;
755}
756
3c50c88f 757static int write_each_note(const struct object_id *object_oid UNUSED,
490bc83a 758 const struct object_id *note_oid, char *note_path,
61a7cca0
JH
759 void *cb_data)
760{
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;
765
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;
771 }
dd437451 772 assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
61a7cca0 773
851c2b37
JH
774 /* Weave non-note entries into note entries */
775 return write_each_non_note_until(note_path, d) ||
490bc83a 776 write_each_note_helper(d->root, note_path, mode, note_oid);
61a7cca0
JH
777}
778
00fbe636
JH
779struct note_delete_list {
780 struct note_delete_list *next;
781 const unsigned char *sha1;
782};
783
490bc83a 784static int prune_notes_helper(const struct object_id *object_oid,
3c50c88f
JK
785 const struct object_id *note_oid UNUSED,
786 char *note_path UNUSED,
787 void *cb_data)
00fbe636
JH
788{
789 struct note_delete_list **l = (struct note_delete_list **) cb_data;
790 struct note_delete_list *n;
791
bc726bd0 792 if (repo_has_object_file(the_repository, object_oid))
00fbe636
JH
793 return 0; /* nothing to do for this note */
794
795 /* failed to find object => prune this note */
796 n = (struct note_delete_list *) xmalloc(sizeof(*n));
797 n->next = *l;
490bc83a 798 n->sha1 = object_oid->hash;
00fbe636
JH
799 *l = n;
800 return 0;
801}
802
b7d591d1
PO
803int combine_notes_concatenate(struct object_id *cur_oid,
804 const struct object_id *new_oid)
73f464b5
JH
805{
806 char *cur_msg = NULL, *new_msg = NULL, *buf;
807 unsigned long cur_len, new_len, buf_len;
808 enum object_type cur_type, new_type;
809 int ret;
810
811 /* read in both note blob objects */
b7d591d1 812 if (!is_null_oid(new_oid))
bc726bd0
ÆAB
813 new_msg = repo_read_object_file(the_repository, new_oid,
814 &new_type, &new_len);
73f464b5
JH
815 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
816 free(new_msg);
817 return 0;
818 }
b7d591d1 819 if (!is_null_oid(cur_oid))
bc726bd0
ÆAB
820 cur_msg = repo_read_object_file(the_repository, cur_oid,
821 &cur_type, &cur_len);
73f464b5
JH
822 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
823 free(cur_msg);
824 free(new_msg);
b7d591d1 825 oidcpy(cur_oid, new_oid);
73f464b5
JH
826 return 0;
827 }
828
d4990c4b 829 /* we will separate the notes by two newlines anyway */
73f464b5
JH
830 if (cur_msg[cur_len - 1] == '\n')
831 cur_len--;
832
833 /* concatenate cur_msg and new_msg into buf */
d4990c4b 834 buf_len = cur_len + 2 + new_len;
73f464b5
JH
835 buf = (char *) xmalloc(buf_len);
836 memcpy(buf, cur_msg, cur_len);
837 buf[cur_len] = '\n';
d4990c4b
JH
838 buf[cur_len + 1] = '\n';
839 memcpy(buf + cur_len + 2, new_msg, new_len);
73f464b5
JH
840 free(cur_msg);
841 free(new_msg);
842
843 /* create a new blob object from buf */
c80d226a 844 ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
73f464b5
JH
845 free(buf);
846 return ret;
847}
848
b7d591d1
PO
849int combine_notes_overwrite(struct object_id *cur_oid,
850 const struct object_id *new_oid)
73f464b5 851{
b7d591d1 852 oidcpy(cur_oid, new_oid);
73f464b5
JH
853 return 0;
854}
855
3c50c88f
JK
856int combine_notes_ignore(struct object_id *cur_oid UNUSED,
857 const struct object_id *new_oid UNUSED)
73f464b5
JH
858{
859 return 0;
860}
861
13135243
MH
862/*
863 * Add the lines from the named object to list, with trailing
864 * newlines removed.
865 */
866static int string_list_add_note_lines(struct string_list *list,
b7d591d1 867 const struct object_id *oid)
a6a09095
JH
868{
869 char *data;
870 unsigned long len;
871 enum object_type t;
a6a09095 872
b7d591d1 873 if (is_null_oid(oid))
a6a09095
JH
874 return 0;
875
876 /* read_sha1_file NUL-terminates */
bc726bd0 877 data = repo_read_object_file(the_repository, oid, &t, &len);
a6a09095
JH
878 if (t != OBJ_BLOB || !data || !len) {
879 free(data);
880 return t != OBJ_BLOB || !data;
881 }
882
13135243
MH
883 /*
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.
888 */
889 string_list_split(list, data, '\n', -1);
890 free(data);
a6a09095
JH
891 return 0;
892}
893
894static int string_list_join_lines_helper(struct string_list_item *item,
895 void *cb_data)
896{
897 struct strbuf *buf = cb_data;
898 strbuf_addstr(buf, item->string);
899 strbuf_addch(buf, '\n');
900 return 0;
901}
902
b7d591d1
PO
903int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
904 const struct object_id *new_oid)
a6a09095 905{
f992f0c8 906 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
a6a09095
JH
907 struct strbuf buf = STRBUF_INIT;
908 int ret = 1;
909
910 /* read both note blob objects into unique_lines */
b7d591d1 911 if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
a6a09095 912 goto out;
b7d591d1 913 if (string_list_add_note_lines(&sort_uniq_list, new_oid))
a6a09095 914 goto out;
13135243 915 string_list_remove_empty_items(&sort_uniq_list, 0);
3383e199 916 string_list_sort(&sort_uniq_list);
13135243 917 string_list_remove_duplicates(&sort_uniq_list, 0);
a6a09095
JH
918
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;
923
c80d226a 924 ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
a6a09095
JH
925
926out:
927 strbuf_release(&buf);
928 string_list_clear(&sort_uniq_list, 0);
929 return ret;
930}
931
63e14ee2 932static int string_list_add_one_ref(const char *refname,
5cf88fd8
ÆAB
933 const struct object_id *oid UNUSED,
934 int flag UNUSED, void *cb)
894a9d33
TR
935{
936 struct string_list *refs = cb;
d235e994
MH
937 if (!unsorted_string_list_has_string(refs, refname))
938 string_list_append(refs, refname);
894a9d33
TR
939 return 0;
940}
941
8c46bf90
MH
942/*
943 * The list argument must have strdup_strings set on it.
944 */
894a9d33
TR
945void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
946{
8c46bf90 947 assert(list->strdup_strings);
894a9d33 948 if (has_glob_specials(glob)) {
fd95035f 949 for_each_glob_ref(string_list_add_one_ref, glob, list);
894a9d33 950 } else {
89c149f5 951 struct object_id oid;
d850b7a5 952 if (repo_get_oid(the_repository, glob, &oid))
894a9d33
TR
953 warning("notes ref %s is invalid", glob);
954 if (!unsorted_string_list_has_string(list, glob))
1d2f80fa 955 string_list_append(list, glob);
894a9d33
TR
956 }
957}
958
959void string_list_add_refs_from_colon_sep(struct string_list *list,
960 const char *globs)
961{
6fa23773
MH
962 struct string_list split = STRING_LIST_INIT_NODUP;
963 char *globs_copy = xstrdup(globs);
894a9d33
TR
964 int i;
965
6fa23773
MH
966 string_list_split_in_place(&split, globs_copy, ':', -1);
967 string_list_remove_empty_items(&split, 0);
894a9d33 968
6fa23773
MH
969 for (i = 0; i < split.nr; i++)
970 string_list_add_refs_by_glob(list, split.items[i].string);
894a9d33 971
6fa23773
MH
972 string_list_clear(&split, 0);
973 free(globs_copy);
894a9d33
TR
974}
975
894a9d33
TR
976static int notes_display_config(const char *k, const char *v, void *cb)
977{
978 int *load_refs = cb;
979
980 if (*load_refs && !strcmp(k, "notes.displayref")) {
981 if (!v)
c3eb95a0 982 return config_error_nonbool(k);
894a9d33
TR
983 string_list_add_refs_by_glob(&display_notes_refs, v);
984 }
985
986 return 0;
987}
988
4a9cf1ce 989const char *default_notes_ref(void)
894a9d33
TR
990{
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;
999}
1000
73f464b5
JH
1001void init_notes(struct notes_tree *t, const char *notes_ref,
1002 combine_notes_fn combine_notes, int flags)
23123aec 1003{
13ac1410 1004 struct object_id oid, object_oid;
5ec1e728 1005 unsigned short mode;
23123aec 1006 struct leaf_node root_tree;
fd53c9eb 1007
cd305392
JH
1008 if (!t)
1009 t = &default_notes_tree;
1010 assert(!t->initialized);
709f79b0
JH
1011
1012 if (!notes_ref)
894a9d33 1013 notes_ref = default_notes_ref();
b9342b3f 1014 update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
709f79b0 1015
73f464b5
JH
1016 if (!combine_notes)
1017 combine_notes = combine_notes_concatenate;
1018
65bbf082 1019 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
851c2b37
JH
1020 t->first_non_note = NULL;
1021 t->prev_non_note = NULL;
8c53f071 1022 t->ref = xstrdup_or_null(notes_ref);
ee76f92f 1023 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
73f464b5 1024 t->combine_notes = combine_notes;
cd305392 1025 t->initialized = 1;
7f710ea9 1026 t->dirty = 0;
cd305392 1027
709f79b0 1028 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
d850b7a5 1029 repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
fd53c9eb 1030 return;
34c290a6 1031 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
ee76f92f 1032 die("Cannot use notes ref %s", notes_ref);
50ddb089 1033 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
709f79b0 1034 die("Failed to read notes tree referenced by %s (%s)",
13ac1410 1035 notes_ref, oid_to_hex(&object_oid));
fd53c9eb 1036
5dcc969e 1037 oidclr(&root_tree.key_oid);
1038 oidcpy(&root_tree.val_oid, &oid);
851c2b37 1039 load_subtree(t, &root_tree, t->root, 0);
fd53c9eb
JS
1040}
1041
ee76f92f 1042struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
894a9d33 1043{
8a57c6e9
AR
1044 struct string_list_item *item;
1045 int counter = 0;
894a9d33 1046 struct notes_tree **trees;
b32fa95f 1047 ALLOC_ARRAY(trees, refs->nr + 1);
8a57c6e9
AR
1048 for_each_string_list_item(item, refs) {
1049 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
ee76f92f 1050 init_notes(t, item->string, combine_notes_ignore, flags);
8a57c6e9
AR
1051 trees[counter++] = t;
1052 }
1053 trees[counter] = NULL;
894a9d33
TR
1054 return trees;
1055}
1056
1057void init_display_notes(struct display_notes_opt *opt)
e6e230ee
DL
1058{
1059 memset(opt, 0, sizeof(*opt));
1060 opt->use_default_notes = -1;
1061}
1062
1d729751 1063void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
452538c3 1064{
1d729751
DL
1065 opt->use_default_notes = 1;
1066 *show_notes = 1;
1067}
452538c3 1068
1d729751
DL
1069void 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;
1077}
1078
1079void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1080{
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;
452538c3
DL
1088}
1089
1e6ed544 1090void load_display_notes(struct display_notes_opt *opt)
894a9d33
TR
1091{
1092 char *display_ref_env;
1093 int load_config_refs = 0;
1094 display_notes_refs.strdup_strings = 1;
1095
1096 assert(!display_notes_trees);
1097
3a03cf6b
JK
1098 if (!opt || opt->use_default_notes > 0 ||
1099 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1d2f80fa 1100 string_list_append(&display_notes_refs, default_notes_ref());
894a9d33
TR
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;
1108 }
1109
1110 git_config(notes_display_config, &load_config_refs);
1111
304cc11c 1112 if (opt) {
8a57c6e9 1113 struct string_list_item *item;
304cc11c 1114 for_each_string_list_item(item, &opt->extra_notes_refs)
8a57c6e9
AR
1115 string_list_add_refs_by_glob(&display_notes_refs,
1116 item->string);
1117 }
894a9d33 1118
ee76f92f 1119 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
894a9d33
TR
1120 string_list_clear(&display_notes_refs, 0);
1121}
1122
5ee8a954 1123int add_note(struct notes_tree *t, const struct object_id *object_oid,
1124 const struct object_id *note_oid, combine_notes_fn combine_notes)
2626b536
JH
1125{
1126 struct leaf_node *l;
1127
cd305392
JH
1128 if (!t)
1129 t = &default_notes_tree;
1130 assert(t->initialized);
7f710ea9 1131 t->dirty = 1;
73f464b5
JH
1132 if (!combine_notes)
1133 combine_notes = t->combine_notes;
2626b536 1134 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
5ee8a954 1135 oidcpy(&l->key_oid, object_oid);
1136 oidcpy(&l->val_oid, note_oid);
180619a5 1137 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
2626b536
JH
1138}
1139
1ee1e43d 1140int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1ec666b0
JH
1141{
1142 struct leaf_node l;
1143
cd305392
JH
1144 if (!t)
1145 t = &default_notes_tree;
1146 assert(t->initialized);
92e2cab9 1147 oidread(&l.key_oid, object_sha1);
5dcc969e 1148 oidclr(&l.val_oid);
a502ab93 1149 note_tree_remove(t, t->root, 0, &l);
5dcc969e 1150 if (is_null_oid(&l.val_oid)) /* no note was removed */
1ee1e43d
JH
1151 return 1;
1152 t->dirty = 1;
1153 return 0;
1ec666b0
JH
1154}
1155
9ef72230 1156const struct object_id *get_note(struct notes_tree *t,
5ee8a954 1157 const struct object_id *oid)
fd53c9eb 1158{
9b391f21
JH
1159 struct leaf_node *found;
1160
cd305392
JH
1161 if (!t)
1162 t = &default_notes_tree;
1163 assert(t->initialized);
5ee8a954 1164 found = note_tree_find(t, t->root, 0, oid->hash);
9ef72230 1165 return found ? &found->val_oid : NULL;
fd53c9eb 1166}
a97a7468 1167
cd305392
JH
1168int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1169 void *cb_data)
73f77b90 1170{
cd305392
JH
1171 if (!t)
1172 t = &default_notes_tree;
1173 assert(t->initialized);
851c2b37 1174 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
73f77b90
JH
1175}
1176
bbca96d5 1177int write_notes_tree(struct notes_tree *t, struct object_id *result)
61a7cca0
JH
1178{
1179 struct tree_write_stack root;
1180 struct write_each_note_data cb_data;
1181 int ret;
bbca96d5 1182 int flags;
61a7cca0 1183
cd305392
JH
1184 if (!t)
1185 t = &default_notes_tree;
1186 assert(t->initialized);
61a7cca0
JH
1187
1188 /* Prepare for traversal of current notes tree */
1189 root.next = NULL; /* last forward entry in list is grounded */
dd437451 1190 strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
61a7cca0
JH
1191 root.path[0] = root.path[1] = '\0';
1192 cb_data.root = &root;
dbc27477
JH
1193 cb_data.nn_list = &(t->first_non_note);
1194 cb_data.nn_prev = NULL;
61a7cca0
JH
1195
1196 /* Write tree objects representing current notes tree */
bbca96d5
PO
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) ||
c80d226a 1202 write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
61a7cca0
JH
1203 strbuf_release(&root.buf);
1204 return ret;
1205}
1206
a9f2adff 1207void prune_notes(struct notes_tree *t, int flags)
00fbe636
JH
1208{
1209 struct note_delete_list *l = NULL;
1210
1211 if (!t)
1212 t = &default_notes_tree;
1213 assert(t->initialized);
1214
1215 for_each_note(t, 0, prune_notes_helper, &l);
1216
1217 while (l) {
a9f2adff 1218 if (flags & NOTES_PRUNE_VERBOSE)
0dbc6462 1219 printf("%s\n", hash_to_hex(l->sha1));
a9f2adff
MG
1220 if (!(flags & NOTES_PRUNE_DRYRUN))
1221 remove_note(t, l->sha1);
00fbe636
JH
1222 l = l->next;
1223 }
1224}
1225
cd305392 1226void free_notes(struct notes_tree *t)
27d57564 1227{
cd305392
JH
1228 if (!t)
1229 t = &default_notes_tree;
1230 if (t->root)
1231 note_tree_free(t->root);
1232 free(t->root);
851c2b37
JH
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;
1238 }
cd305392
JH
1239 free(t->ref);
1240 memset(t, 0, sizeof(struct notes_tree));
27d57564
JH
1241}
1242
96531a5e
JH
1243/*
1244 * Fill the given strbuf with the notes associated with the given object.
1245 *
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.
1250 *
76141e2e
JH
1251 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1252 * for human consumption.
96531a5e 1253 */
fb61e4d3 1254static void format_note(struct notes_tree *t, const struct object_id *object_oid,
76141e2e 1255 struct strbuf *sb, const char *output_encoding, int raw)
a97a7468
JS
1256{
1257 static const char utf8[] = "utf-8";
9ef72230 1258 const struct object_id *oid;
a97a7468
JS
1259 char *msg, *msg_p;
1260 unsigned long linelen, msglen;
1261 enum object_type type;
1262
cd305392
JH
1263 if (!t)
1264 t = &default_notes_tree;
1265 if (!t->initialized)
73f464b5 1266 init_notes(t, NULL, NULL, 0);
a97a7468 1267
5ee8a954 1268 oid = get_note(t, object_oid);
9ef72230 1269 if (!oid)
a97a7468
JS
1270 return;
1271
bc726bd0 1272 if (!(msg = repo_read_object_file(the_repository, oid, &type, &msglen)) || type != OBJ_BLOB) {
a97a7468
JS
1273 free(msg);
1274 return;
1275 }
1276
1277 if (output_encoding && *output_encoding &&
0e18bcd5 1278 !is_encoding_utf8(output_encoding)) {
a97a7468
JS
1279 char *reencoded = reencode_string(msg, output_encoding, utf8);
1280 if (reencoded) {
1281 free(msg);
1282 msg = reencoded;
1283 msglen = strlen(msg);
1284 }
1285 }
1286
1287 /* we will end the annotation by a newline anyway */
1288 if (msglen && msg[msglen - 1] == '\n')
1289 msglen--;
1290
76141e2e 1291 if (!raw) {
894a9d33
TR
1292 const char *ref = t->ref;
1293 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1294 strbuf_addstr(sb, "\nNotes:\n");
1295 } else {
145136a9
JH
1296 skip_prefix(ref, "refs/", &ref);
1297 skip_prefix(ref, "notes/", &ref);
894a9d33
TR
1298 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1299 }
1300 }
a97a7468
JS
1301
1302 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1303 linelen = strchrnul(msg_p, '\n') - msg_p;
1304
76141e2e 1305 if (!raw)
c56fcc89 1306 strbuf_addstr(sb, " ");
a97a7468
JS
1307 strbuf_add(sb, msg_p, linelen);
1308 strbuf_addch(sb, '\n');
1309 }
1310
1311 free(msg);
1312}
894a9d33 1313
fb61e4d3 1314void format_display_notes(const struct object_id *object_oid,
76141e2e 1315 struct strbuf *sb, const char *output_encoding, int raw)
894a9d33
TR
1316{
1317 int i;
1318 assert(display_notes_trees);
1319 for (i = 0; display_notes_trees[i]; i++)
fb61e4d3 1320 format_note(display_notes_trees[i], object_oid, sb,
76141e2e 1321 output_encoding, raw);
894a9d33 1322}
160baa0d
TR
1323
1324int copy_note(struct notes_tree *t,
5ee8a954 1325 const struct object_id *from_obj, const struct object_id *to_obj,
180619a5 1326 int force, combine_notes_fn combine_notes)
160baa0d 1327{
9ef72230 1328 const struct object_id *note = get_note(t, from_obj);
1329 const struct object_id *existing_note = get_note(t, to_obj);
160baa0d
TR
1330
1331 if (!force && existing_note)
1332 return 1;
1333
1334 if (note)
5ee8a954 1335 return add_note(t, to_obj, note, combine_notes);
160baa0d 1336 else if (existing_note)
14228447 1337 return add_note(t, to_obj, null_oid(), combine_notes);
160baa0d
TR
1338
1339 return 0;
1340}
03bb5789
JK
1341
1342void expand_notes_ref(struct strbuf *sb)
1343{
59556548 1344 if (starts_with(sb->buf, "refs/notes/"))
03bb5789 1345 return; /* we're happy */
59556548 1346 else if (starts_with(sb->buf, "notes/"))
a91cc7fa 1347 strbuf_insertstr(sb, 0, "refs/");
03bb5789 1348 else
a91cc7fa 1349 strbuf_insertstr(sb, 0, "refs/notes/");
03bb5789 1350}
b3715b75
JK
1351
1352void expand_loose_notes_ref(struct strbuf *sb)
1353{
89c149f5 1354 struct object_id object;
b3715b75 1355
d850b7a5 1356 if (repo_get_oid(the_repository, sb->buf, &object)) {
b3715b75
JK
1357 /* fallback to expand_notes_ref */
1358 expand_notes_ref(sb);
1359 }
1360}