10 * Use a non-balancing simple 16-tree structure with struct int_node as
11 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
12 * 16-array of pointers to its children.
13 * The bottom 2 bits of each pointer is used to identify the pointer type
14 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
15 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
16 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
17 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
19 * The root node is a statically allocated struct int_node.
26 * Leaf nodes come in two variants, note entries and subtree entries,
27 * distinguished by the LSb of the leaf node pointer (see above).
28 * As a note entry, the key is the SHA1 of the referenced object, and the
29 * value is the SHA1 of the note object.
30 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
31 * referenced object, using the last byte of the key to store the length of
32 * the prefix. The value is the SHA1 of the tree object containing the notes
36 unsigned char key_sha1
[20];
37 unsigned char val_sha1
[20];
41 * A notes tree may contain entries that are not notes, and that do not follow
42 * the naming conventions of notes. There are typically none/few of these, but
43 * we still need to keep track of them. Keep a simple linked list sorted alpha-
44 * betically on the non-note path. The list is populated when parsing tree
45 * objects in load_subtree(), and the non-notes are correctly written back into
46 * the tree objects produced by write_notes_tree().
49 struct non_note
*next
; /* grounded (last->next == NULL) */
52 unsigned char sha1
[20];
55 #define PTR_TYPE_NULL 0
56 #define PTR_TYPE_INTERNAL 1
57 #define PTR_TYPE_NOTE 2
58 #define PTR_TYPE_SUBTREE 3
60 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
61 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
62 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
64 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
66 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
67 (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
69 struct notes_tree default_notes_tree
;
71 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
72 struct int_node
*node
, unsigned int n
);
75 * Search the tree until the appropriate location for the given key is found:
76 * 1. Start at the root node, with n = 0
77 * 2. If a[0] at the current level is a matching subtree entry, unpack that
78 * subtree entry and remove it; restart search at the current level.
79 * 3. Use the nth nibble of the key as an index into a:
80 * - If a[n] is an int_node, recurse from #2 into that node and increment n
81 * - If a matching subtree entry, unpack that subtree entry (and remove it);
82 * restart search at the current level.
83 * - Otherwise, we have found one of the following:
84 * - a subtree entry which does not match the key
85 * - a note entry which may or may not match the key
86 * - an unused leaf node (NULL)
87 * In any case, set *tree and *n, and return pointer to the tree location.
89 static void **note_tree_search(struct notes_tree
*t
, struct int_node
**tree
,
90 unsigned char *n
, const unsigned char *key_sha1
)
94 void *p
= (*tree
)->a
[0];
96 if (GET_PTR_TYPE(p
) == PTR_TYPE_SUBTREE
) {
97 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
98 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_sha1
)) {
99 /* unpack tree and resume search */
100 (*tree
)->a
[0] = NULL
;
101 load_subtree(t
, l
, *tree
, *n
);
103 return note_tree_search(t
, tree
, n
, key_sha1
);
107 i
= GET_NIBBLE(*n
, key_sha1
);
109 switch (GET_PTR_TYPE(p
)) {
110 case PTR_TYPE_INTERNAL
:
111 *tree
= CLR_PTR_TYPE(p
);
113 return note_tree_search(t
, tree
, n
, key_sha1
);
114 case PTR_TYPE_SUBTREE
:
115 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
116 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_sha1
)) {
117 /* unpack tree and resume search */
118 (*tree
)->a
[i
] = NULL
;
119 load_subtree(t
, l
, *tree
, *n
);
121 return note_tree_search(t
, tree
, n
, key_sha1
);
125 return &((*tree
)->a
[i
]);
130 * To find a leaf_node:
131 * Search to the tree location appropriate for the given key:
132 * If a note entry with matching key, return the note entry, else return NULL.
134 static struct leaf_node
*note_tree_find(struct notes_tree
*t
,
135 struct int_node
*tree
, unsigned char n
,
136 const unsigned char *key_sha1
)
138 void **p
= note_tree_search(t
, &tree
, &n
, key_sha1
);
139 if (GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
) {
140 struct leaf_node
*l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
141 if (!hashcmp(key_sha1
, l
->key_sha1
))
148 * To insert a leaf_node:
149 * Search to the tree location appropriate for the given leaf_node's key:
150 * - If location is unused (NULL), store the tweaked pointer directly there
151 * - If location holds a note entry that matches the note-to-be-inserted, then
152 * combine the two notes (by calling the given combine_notes function).
153 * - If location holds a note entry that matches the subtree-to-be-inserted,
154 * then unpack the subtree-to-be-inserted into the location.
155 * - If location holds a matching subtree entry, unpack the subtree at that
156 * location, and restart the insert operation from that level.
157 * - Else, create a new int_node, holding both the node-at-location and the
158 * node-to-be-inserted, and store the new int_node into the location.
160 static void note_tree_insert(struct notes_tree
*t
, struct int_node
*tree
,
161 unsigned char n
, struct leaf_node
*entry
, unsigned char type
,
162 combine_notes_fn combine_notes
)
164 struct int_node
*new_node
;
166 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_sha1
);
168 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
169 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
170 switch (GET_PTR_TYPE(*p
)) {
173 *p
= SET_PTR_TYPE(entry
, type
);
178 if (!hashcmp(l
->key_sha1
, entry
->key_sha1
)) {
179 /* skip concatenation if l == entry */
180 if (!hashcmp(l
->val_sha1
, entry
->val_sha1
))
183 if (combine_notes(l
->val_sha1
, entry
->val_sha1
))
184 die("failed to combine notes %s and %s"
186 sha1_to_hex(l
->val_sha1
),
187 sha1_to_hex(entry
->val_sha1
),
188 sha1_to_hex(l
->key_sha1
));
193 case PTR_TYPE_SUBTREE
:
194 if (!SUBTREE_SHA1_PREFIXCMP(l
->key_sha1
,
197 load_subtree(t
, entry
, tree
, n
);
204 case PTR_TYPE_SUBTREE
:
205 if (!SUBTREE_SHA1_PREFIXCMP(entry
->key_sha1
, l
->key_sha1
)) {
206 /* unpack 'l' and restart insert */
208 load_subtree(t
, l
, tree
, n
);
210 note_tree_insert(t
, tree
, n
, entry
, type
,
217 /* non-matching leaf_node */
218 assert(GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
||
219 GET_PTR_TYPE(*p
) == PTR_TYPE_SUBTREE
);
220 new_node
= (struct int_node
*) xcalloc(sizeof(struct int_node
), 1);
221 note_tree_insert(t
, new_node
, n
+ 1, l
, GET_PTR_TYPE(*p
),
223 *p
= SET_PTR_TYPE(new_node
, PTR_TYPE_INTERNAL
);
224 note_tree_insert(t
, new_node
, n
+ 1, entry
, type
, combine_notes
);
228 * How to consolidate an int_node:
229 * If there are > 1 non-NULL entries, give up and return non-zero.
230 * Otherwise replace the int_node at the given index in the given parent node
231 * with the only entry (or a NULL entry if no entries) from the given tree,
234 static int note_tree_consolidate(struct int_node
*tree
,
235 struct int_node
*parent
, unsigned char index
)
240 assert(tree
&& parent
);
241 assert(CLR_PTR_TYPE(parent
->a
[index
]) == tree
);
243 for (i
= 0; i
< 16; i
++) {
244 if (GET_PTR_TYPE(tree
->a
[i
]) != PTR_TYPE_NULL
) {
245 if (p
) /* more than one entry */
251 /* replace tree with p in parent[index] */
252 parent
->a
[index
] = p
;
258 * To remove a leaf_node:
259 * Search to the tree location appropriate for the given leaf_node's key:
260 * - If location does not hold a matching entry, abort and do nothing.
261 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
262 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
264 static void note_tree_remove(struct notes_tree
*t
, struct int_node
*tree
,
265 unsigned char n
, struct leaf_node
*entry
)
268 struct int_node
*parent_stack
[20];
270 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_sha1
);
272 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
273 if (GET_PTR_TYPE(*p
) != PTR_TYPE_NOTE
)
274 return; /* type mismatch, nothing to remove */
275 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
276 if (hashcmp(l
->key_sha1
, entry
->key_sha1
))
277 return; /* key mismatch, nothing to remove */
279 /* we have found a matching entry */
281 *p
= SET_PTR_TYPE(NULL
, PTR_TYPE_NULL
);
283 /* consolidate this tree level, and parent levels, if possible */
285 return; /* cannot consolidate top level */
286 /* first, build stack of ancestors between root and current node */
287 parent_stack
[0] = t
->root
;
288 for (i
= 0; i
< n
; i
++) {
289 j
= GET_NIBBLE(i
, entry
->key_sha1
);
290 parent_stack
[i
+ 1] = CLR_PTR_TYPE(parent_stack
[i
]->a
[j
]);
292 assert(i
== n
&& parent_stack
[i
] == tree
);
293 /* next, unwind stack until note_tree_consolidate() is done */
295 !note_tree_consolidate(parent_stack
[i
], parent_stack
[i
- 1],
296 GET_NIBBLE(i
- 1, entry
->key_sha1
)))
300 /* Free the entire notes data contained in the given tree */
301 static void note_tree_free(struct int_node
*tree
)
304 for (i
= 0; i
< 16; i
++) {
305 void *p
= tree
->a
[i
];
306 switch (GET_PTR_TYPE(p
)) {
307 case PTR_TYPE_INTERNAL
:
308 note_tree_free(CLR_PTR_TYPE(p
));
311 case PTR_TYPE_SUBTREE
:
312 free(CLR_PTR_TYPE(p
));
318 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
319 * - hex - Partial SHA1 segment in ASCII hex format
320 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
321 * - sha1 - Partial SHA1 value is written here
322 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
323 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
324 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
325 * Pads sha1 with NULs up to sha1_len (not included in returned length).
327 static int get_sha1_hex_segment(const char *hex
, unsigned int hex_len
,
328 unsigned char *sha1
, unsigned int sha1_len
)
330 unsigned int i
, len
= hex_len
>> 1;
331 if (hex_len
% 2 != 0 || len
> sha1_len
)
333 for (i
= 0; i
< len
; i
++) {
334 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
340 for (; i
< sha1_len
; i
++)
345 static int non_note_cmp(const struct non_note
*a
, const struct non_note
*b
)
347 return strcmp(a
->path
, b
->path
);
350 static void add_non_note(struct notes_tree
*t
, const char *path
,
351 unsigned int mode
, const unsigned char *sha1
)
353 struct non_note
*p
= t
->prev_non_note
, *n
;
354 n
= (struct non_note
*) xmalloc(sizeof(struct non_note
));
356 n
->path
= xstrdup(path
);
358 hashcpy(n
->sha1
, sha1
);
359 t
->prev_non_note
= n
;
361 if (!t
->first_non_note
) {
362 t
->first_non_note
= n
;
366 if (non_note_cmp(p
, n
) < 0)
368 else if (non_note_cmp(t
->first_non_note
, n
) <= 0)
369 p
= t
->first_non_note
;
371 /* n sorts before t->first_non_note */
372 n
->next
= t
->first_non_note
;
373 t
->first_non_note
= n
;
377 /* n sorts equal or after p */
378 while (p
->next
&& non_note_cmp(p
->next
, n
) <= 0)
381 if (non_note_cmp(p
, n
) == 0) { /* n ~= p; overwrite p with n */
382 assert(strcmp(p
->path
, n
->path
) == 0);
384 hashcpy(p
->sha1
, n
->sha1
);
386 t
->prev_non_note
= p
;
390 /* n sorts between p and p->next */
395 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
396 struct int_node
*node
, unsigned int n
)
398 unsigned char object_sha1
[20];
399 unsigned int prefix_len
;
401 struct tree_desc desc
;
402 struct name_entry entry
;
407 buf
= fill_tree_descriptor(&desc
, subtree
->val_sha1
);
409 die("Could not read %s for notes-index",
410 sha1_to_hex(subtree
->val_sha1
));
412 prefix_len
= subtree
->key_sha1
[19];
413 assert(prefix_len
* 2 >= n
);
414 memcpy(object_sha1
, subtree
->key_sha1
, prefix_len
);
415 while (tree_entry(&desc
, &entry
)) {
416 path_len
= strlen(entry
.path
);
417 len
= get_sha1_hex_segment(entry
.path
, path_len
,
418 object_sha1
+ prefix_len
, 20 - prefix_len
);
420 goto handle_non_note
; /* entry.path is not a SHA1 */
424 * If object SHA1 is complete (len == 20), assume note object
425 * If object SHA1 is incomplete (len < 20), and current
426 * component consists of 2 hex chars, assume note subtree
429 type
= PTR_TYPE_NOTE
;
430 l
= (struct leaf_node
*)
431 xcalloc(sizeof(struct leaf_node
), 1);
432 hashcpy(l
->key_sha1
, object_sha1
);
433 hashcpy(l
->val_sha1
, entry
.sha1
);
435 if (!S_ISDIR(entry
.mode
) || path_len
!= 2)
436 goto handle_non_note
; /* not subtree */
437 l
->key_sha1
[19] = (unsigned char) len
;
438 type
= PTR_TYPE_SUBTREE
;
440 note_tree_insert(t
, node
, n
, l
, type
,
441 combine_notes_concatenate
);
447 * Determine full path for this non-note entry:
448 * The filename is already found in entry.path, but the
449 * directory part of the path must be deduced from the subtree
450 * containing this entry. We assume here that the overall notes
451 * tree follows a strict byte-based progressive fanout
452 * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
453 * e.g. 4/36 fanout). This means that if a non-note is found at
454 * path "dead/beef", the following code will register it as
455 * being found on "de/ad/beef".
456 * On the other hand, if you use such non-obvious non-note
457 * paths in the middle of a notes tree, you deserve what's
458 * coming to you ;). Note that for non-notes that are not
459 * SHA1-like at the top level, there will be no problems.
461 * To conclude, it is strongly advised to make sure non-notes
462 * have at least one non-hex character in the top-level path
466 char non_note_path
[PATH_MAX
];
467 char *p
= non_note_path
;
468 const char *q
= sha1_to_hex(subtree
->key_sha1
);
470 for (i
= 0; i
< prefix_len
; i
++) {
475 strcpy(p
, entry
.path
);
476 add_non_note(t
, non_note_path
, entry
.mode
, entry
.sha1
);
483 * Determine optimal on-disk fanout for this part of the notes tree
485 * Given a (sub)tree and the level in the internal tree structure, determine
486 * whether or not the given existing fanout should be expanded for this
489 * Values of the 'fanout' variable:
490 * - 0: No fanout (all notes are stored directly in the root notes tree)
493 * - 3: 2/2/2/34 fanout
496 static unsigned char determine_fanout(struct int_node
*tree
, unsigned char n
,
497 unsigned char fanout
)
500 * The following is a simple heuristic that works well in practice:
501 * For each even-numbered 16-tree level (remember that each on-disk
502 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
503 * entries at that tree level. If all of them are either int_nodes or
504 * subtree entries, then there are likely plenty of notes below this
505 * level, so we return an incremented fanout.
508 if ((n
% 2) || (n
> 2 * fanout
))
510 for (i
= 0; i
< 16; i
++) {
511 switch (GET_PTR_TYPE(tree
->a
[i
])) {
512 case PTR_TYPE_SUBTREE
:
513 case PTR_TYPE_INTERNAL
:
522 static void construct_path_with_fanout(const unsigned char *sha1
,
523 unsigned char fanout
, char *path
)
525 unsigned int i
= 0, j
= 0;
526 const char *hex_sha1
= sha1_to_hex(sha1
);
529 path
[i
++] = hex_sha1
[j
++];
530 path
[i
++] = hex_sha1
[j
++];
534 strcpy(path
+ i
, hex_sha1
+ j
);
537 static int for_each_note_helper(struct notes_tree
*t
, struct int_node
*tree
,
538 unsigned char n
, unsigned char fanout
, int flags
,
539 each_note_fn fn
, void *cb_data
)
545 static char path
[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
547 fanout
= determine_fanout(tree
, n
, fanout
);
548 for (i
= 0; i
< 16; i
++) {
551 switch (GET_PTR_TYPE(p
)) {
552 case PTR_TYPE_INTERNAL
:
553 /* recurse into int_node */
554 ret
= for_each_note_helper(t
, CLR_PTR_TYPE(p
), n
+ 1,
555 fanout
, flags
, fn
, cb_data
);
557 case PTR_TYPE_SUBTREE
:
558 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
560 * Subtree entries in the note tree represent parts of
561 * the note tree that have not yet been explored. There
562 * is a direct relationship between subtree entries at
563 * level 'n' in the tree, and the 'fanout' variable:
564 * Subtree entries at level 'n <= 2 * fanout' should be
565 * preserved, since they correspond exactly to a fanout
566 * directory in the on-disk structure. However, subtree
567 * entries at level 'n > 2 * fanout' should NOT be
568 * preserved, but rather consolidated into the above
569 * notes tree level. We achieve this by unconditionally
570 * unpacking subtree entries that exist below the
571 * threshold level at 'n = 2 * fanout'.
573 if (n
<= 2 * fanout
&&
574 flags
& FOR_EACH_NOTE_YIELD_SUBTREES
) {
575 /* invoke callback with subtree */
576 unsigned int path_len
=
577 l
->key_sha1
[19] * 2 + fanout
;
578 assert(path_len
< 40 + 19);
579 construct_path_with_fanout(l
->key_sha1
, fanout
,
581 /* Create trailing slash, if needed */
582 if (path
[path_len
- 1] != '/')
583 path
[path_len
++] = '/';
584 path
[path_len
] = '\0';
585 ret
= fn(l
->key_sha1
, l
->val_sha1
, path
,
588 if (n
> fanout
* 2 ||
589 !(flags
& FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
)) {
590 /* unpack subtree and resume traversal */
592 load_subtree(t
, l
, tree
, n
);
598 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
599 construct_path_with_fanout(l
->key_sha1
, fanout
, path
);
600 ret
= fn(l
->key_sha1
, l
->val_sha1
, path
, cb_data
);
609 struct tree_write_stack
{
610 struct tree_write_stack
*next
;
612 char path
[2]; /* path to subtree in next, if any */
615 static inline int matches_tree_write_stack(struct tree_write_stack
*tws
,
616 const char *full_path
)
618 return full_path
[0] == tws
->path
[0] &&
619 full_path
[1] == tws
->path
[1] &&
623 static void write_tree_entry(struct strbuf
*buf
, unsigned int mode
,
624 const char *path
, unsigned int path_len
, const
627 strbuf_addf(buf
, "%06o %.*s%c", mode
, path_len
, path
, '\0');
628 strbuf_add(buf
, sha1
, 20);
631 static void tree_write_stack_init_subtree(struct tree_write_stack
*tws
,
634 struct tree_write_stack
*n
;
636 assert(tws
->path
[0] == '\0' && tws
->path
[1] == '\0');
637 n
= (struct tree_write_stack
*)
638 xmalloc(sizeof(struct tree_write_stack
));
640 strbuf_init(&n
->buf
, 256 * (32 + 40)); /* assume 256 entries per tree */
641 n
->path
[0] = n
->path
[1] = '\0';
643 tws
->path
[0] = path
[0];
644 tws
->path
[1] = path
[1];
647 static int tree_write_stack_finish_subtree(struct tree_write_stack
*tws
)
650 struct tree_write_stack
*n
= tws
->next
;
653 ret
= tree_write_stack_finish_subtree(n
);
656 ret
= write_sha1_file(n
->buf
.buf
, n
->buf
.len
, tree_type
, s
);
659 strbuf_release(&n
->buf
);
662 write_tree_entry(&tws
->buf
, 040000, tws
->path
, 2, s
);
663 tws
->path
[0] = tws
->path
[1] = '\0';
668 static int write_each_note_helper(struct tree_write_stack
*tws
,
669 const char *path
, unsigned int mode
,
670 const unsigned char *sha1
)
672 size_t path_len
= strlen(path
);
676 /* Determine common part of tree write stack */
677 while (tws
&& 3 * n
< path_len
&&
678 matches_tree_write_stack(tws
, path
+ 3 * n
)) {
683 /* tws point to last matching tree_write_stack entry */
684 ret
= tree_write_stack_finish_subtree(tws
);
688 /* Start subtrees needed to satisfy path */
689 while (3 * n
+ 2 < path_len
&& path
[3 * n
+ 2] == '/') {
690 tree_write_stack_init_subtree(tws
, path
+ 3 * n
);
695 /* There should be no more directory components in the given path */
696 assert(memchr(path
+ 3 * n
, '/', path_len
- (3 * n
)) == NULL
);
698 /* Finally add given entry to the current tree object */
699 write_tree_entry(&tws
->buf
, mode
, path
+ 3 * n
, path_len
- (3 * n
),
705 struct write_each_note_data
{
706 struct tree_write_stack
*root
;
707 struct non_note
*next_non_note
;
710 static int write_each_non_note_until(const char *note_path
,
711 struct write_each_note_data
*d
)
713 struct non_note
*n
= d
->next_non_note
;
715 while (n
&& (!note_path
|| (cmp
= strcmp(n
->path
, note_path
)) <= 0)) {
716 if (note_path
&& cmp
== 0)
717 ; /* do nothing, prefer note to non-note */
719 ret
= write_each_note_helper(d
->root
, n
->path
, n
->mode
,
726 d
->next_non_note
= n
;
730 static int write_each_note(const unsigned char *object_sha1
,
731 const unsigned char *note_sha1
, char *note_path
,
734 struct write_each_note_data
*d
=
735 (struct write_each_note_data
*) cb_data
;
736 size_t note_path_len
= strlen(note_path
);
737 unsigned int mode
= 0100644;
739 if (note_path
[note_path_len
- 1] == '/') {
742 note_path
[note_path_len
] = '\0';
745 assert(note_path_len
<= 40 + 19);
747 /* Weave non-note entries into note entries */
748 return write_each_non_note_until(note_path
, d
) ||
749 write_each_note_helper(d
->root
, note_path
, mode
, note_sha1
);
752 int combine_notes_concatenate(unsigned char *cur_sha1
,
753 const unsigned char *new_sha1
)
755 char *cur_msg
= NULL
, *new_msg
= NULL
, *buf
;
756 unsigned long cur_len
, new_len
, buf_len
;
757 enum object_type cur_type
, new_type
;
760 /* read in both note blob objects */
761 if (!is_null_sha1(new_sha1
))
762 new_msg
= read_sha1_file(new_sha1
, &new_type
, &new_len
);
763 if (!new_msg
|| !new_len
|| new_type
!= OBJ_BLOB
) {
767 if (!is_null_sha1(cur_sha1
))
768 cur_msg
= read_sha1_file(cur_sha1
, &cur_type
, &cur_len
);
769 if (!cur_msg
|| !cur_len
|| cur_type
!= OBJ_BLOB
) {
772 hashcpy(cur_sha1
, new_sha1
);
776 /* we will separate the notes by a newline anyway */
777 if (cur_msg
[cur_len
- 1] == '\n')
780 /* concatenate cur_msg and new_msg into buf */
781 buf_len
= cur_len
+ 1 + new_len
;
782 buf
= (char *) xmalloc(buf_len
);
783 memcpy(buf
, cur_msg
, cur_len
);
785 memcpy(buf
+ cur_len
+ 1, new_msg
, new_len
);
789 /* create a new blob object from buf */
790 ret
= write_sha1_file(buf
, buf_len
, blob_type
, cur_sha1
);
795 int combine_notes_overwrite(unsigned char *cur_sha1
,
796 const unsigned char *new_sha1
)
798 hashcpy(cur_sha1
, new_sha1
);
802 int combine_notes_ignore(unsigned char *cur_sha1
,
803 const unsigned char *new_sha1
)
808 void init_notes(struct notes_tree
*t
, const char *notes_ref
,
809 combine_notes_fn combine_notes
, int flags
)
811 unsigned char sha1
[20], object_sha1
[20];
813 struct leaf_node root_tree
;
816 t
= &default_notes_tree
;
817 assert(!t
->initialized
);
820 notes_ref
= getenv(GIT_NOTES_REF_ENVIRONMENT
);
822 notes_ref
= notes_ref_name
; /* value of core.notesRef config */
824 notes_ref
= GIT_NOTES_DEFAULT_REF
;
827 combine_notes
= combine_notes_concatenate
;
829 t
->root
= (struct int_node
*) xcalloc(sizeof(struct int_node
), 1);
830 t
->first_non_note
= NULL
;
831 t
->prev_non_note
= NULL
;
832 t
->ref
= notes_ref
? xstrdup(notes_ref
) : NULL
;
833 t
->combine_notes
= combine_notes
;
836 if (flags
& NOTES_INIT_EMPTY
|| !notes_ref
||
837 read_ref(notes_ref
, object_sha1
))
839 if (get_tree_entry(object_sha1
, "", sha1
, &mode
))
840 die("Failed to read notes tree referenced by %s (%s)",
841 notes_ref
, object_sha1
);
843 hashclr(root_tree
.key_sha1
);
844 hashcpy(root_tree
.val_sha1
, sha1
);
845 load_subtree(t
, &root_tree
, t
->root
, 0);
848 void add_note(struct notes_tree
*t
, const unsigned char *object_sha1
,
849 const unsigned char *note_sha1
, combine_notes_fn combine_notes
)
854 t
= &default_notes_tree
;
855 assert(t
->initialized
);
857 combine_notes
= t
->combine_notes
;
858 l
= (struct leaf_node
*) xmalloc(sizeof(struct leaf_node
));
859 hashcpy(l
->key_sha1
, object_sha1
);
860 hashcpy(l
->val_sha1
, note_sha1
);
861 note_tree_insert(t
, t
->root
, 0, l
, PTR_TYPE_NOTE
, combine_notes
);
864 void remove_note(struct notes_tree
*t
, const unsigned char *object_sha1
)
869 t
= &default_notes_tree
;
870 assert(t
->initialized
);
871 hashcpy(l
.key_sha1
, object_sha1
);
873 return note_tree_remove(t
, t
->root
, 0, &l
);
876 const unsigned char *get_note(struct notes_tree
*t
,
877 const unsigned char *object_sha1
)
879 struct leaf_node
*found
;
882 t
= &default_notes_tree
;
883 assert(t
->initialized
);
884 found
= note_tree_find(t
, t
->root
, 0, object_sha1
);
885 return found
? found
->val_sha1
: NULL
;
888 int for_each_note(struct notes_tree
*t
, int flags
, each_note_fn fn
,
892 t
= &default_notes_tree
;
893 assert(t
->initialized
);
894 return for_each_note_helper(t
, t
->root
, 0, 0, flags
, fn
, cb_data
);
897 int write_notes_tree(struct notes_tree
*t
, unsigned char *result
)
899 struct tree_write_stack root
;
900 struct write_each_note_data cb_data
;
904 t
= &default_notes_tree
;
905 assert(t
->initialized
);
907 /* Prepare for traversal of current notes tree */
908 root
.next
= NULL
; /* last forward entry in list is grounded */
909 strbuf_init(&root
.buf
, 256 * (32 + 40)); /* assume 256 entries */
910 root
.path
[0] = root
.path
[1] = '\0';
911 cb_data
.root
= &root
;
912 cb_data
.next_non_note
= t
->first_non_note
;
914 /* Write tree objects representing current notes tree */
915 ret
= for_each_note(t
, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
|
916 FOR_EACH_NOTE_YIELD_SUBTREES
,
917 write_each_note
, &cb_data
) ||
918 write_each_non_note_until(NULL
, &cb_data
) ||
919 tree_write_stack_finish_subtree(&root
) ||
920 write_sha1_file(root
.buf
.buf
, root
.buf
.len
, tree_type
, result
);
921 strbuf_release(&root
.buf
);
925 void free_notes(struct notes_tree
*t
)
928 t
= &default_notes_tree
;
930 note_tree_free(t
->root
);
932 while (t
->first_non_note
) {
933 t
->prev_non_note
= t
->first_non_note
->next
;
934 free(t
->first_non_note
->path
);
935 free(t
->first_non_note
);
936 t
->first_non_note
= t
->prev_non_note
;
939 memset(t
, 0, sizeof(struct notes_tree
));
942 void format_note(struct notes_tree
*t
, const unsigned char *object_sha1
,
943 struct strbuf
*sb
, const char *output_encoding
, int flags
)
945 static const char utf8
[] = "utf-8";
946 const unsigned char *sha1
;
948 unsigned long linelen
, msglen
;
949 enum object_type type
;
952 t
= &default_notes_tree
;
954 init_notes(t
, NULL
, NULL
, 0);
956 sha1
= get_note(t
, object_sha1
);
960 if (!(msg
= read_sha1_file(sha1
, &type
, &msglen
)) || !msglen
||
966 if (output_encoding
&& *output_encoding
&&
967 strcmp(utf8
, output_encoding
)) {
968 char *reencoded
= reencode_string(msg
, output_encoding
, utf8
);
972 msglen
= strlen(msg
);
976 /* we will end the annotation by a newline anyway */
977 if (msglen
&& msg
[msglen
- 1] == '\n')
980 if (flags
& NOTES_SHOW_HEADER
)
981 strbuf_addstr(sb
, "\nNotes:\n");
983 for (msg_p
= msg
; msg_p
< msg
+ msglen
; msg_p
+= linelen
+ 1) {
984 linelen
= strchrnul(msg_p
, '\n') - msg_p
;
986 if (flags
& NOTES_INDENT
)
987 strbuf_addstr(sb
, " ");
988 strbuf_add(sb
, msg_p
, linelen
);
989 strbuf_addch(sb
, '\n');