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