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