8 #include "string-list.h"
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 *
21 * The root node is a statically allocated struct int_node.
28 * Leaf nodes come in two variants, note entries and subtree entries,
29 * distinguished by the LSb of the leaf node pointer (see above).
30 * As a note entry, the key is the SHA1 of the referenced object, and the
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
33 * referenced object, using the last byte of the key to store the length of
34 * the prefix. The value is the SHA1 of the tree object containing the notes
38 unsigned char key_sha1
[20];
39 unsigned char val_sha1
[20];
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().
51 struct non_note
*next
; /* grounded (last->next == NULL) */
54 unsigned char sha1
[20];
57 #define PTR_TYPE_NULL 0
58 #define PTR_TYPE_INTERNAL 1
59 #define PTR_TYPE_NOTE 2
60 #define PTR_TYPE_SUBTREE 3
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)))
66 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
68 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
69 (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
71 struct notes_tree default_notes_tree
;
73 static struct string_list display_notes_refs
= STRING_LIST_INIT_NODUP
;
74 static struct notes_tree
**display_notes_trees
;
76 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
77 struct int_node
*node
, unsigned int n
);
80 * Search the tree until the appropriate location for the given key is found:
81 * 1. Start at the root node, with n = 0
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
86 * - If a matching subtree entry, unpack that subtree entry (and remove it);
87 * restart search at the current level.
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.
94 static void **note_tree_search(struct notes_tree
*t
, struct int_node
**tree
,
95 unsigned char *n
, const unsigned char *key_sha1
)
99 void *p
= (*tree
)->a
[0];
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
;
106 load_subtree(t
, l
, *tree
, *n
);
108 return note_tree_search(t
, tree
, n
, key_sha1
);
112 i
= GET_NIBBLE(*n
, key_sha1
);
114 switch (GET_PTR_TYPE(p
)) {
115 case PTR_TYPE_INTERNAL
:
116 *tree
= CLR_PTR_TYPE(p
);
118 return note_tree_search(t
, tree
, n
, key_sha1
);
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 */
123 (*tree
)->a
[i
] = NULL
;
124 load_subtree(t
, l
, *tree
, *n
);
126 return note_tree_search(t
, tree
, n
, key_sha1
);
130 return &((*tree
)->a
[i
]);
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.
139 static struct leaf_node
*note_tree_find(struct notes_tree
*t
,
140 struct int_node
*tree
, unsigned char n
,
141 const unsigned char *key_sha1
)
143 void **p
= note_tree_search(t
, &tree
, &n
, key_sha1
);
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
))
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 NOTE entry (or a NULL entry if no entries) from the given
157 * tree, and return 0.
159 static int note_tree_consolidate(struct int_node
*tree
,
160 struct int_node
*parent
, unsigned char index
)
165 assert(tree
&& parent
);
166 assert(CLR_PTR_TYPE(parent
->a
[index
]) == tree
);
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 */
176 if (p
&& (GET_PTR_TYPE(p
) != PTR_TYPE_NOTE
))
178 /* replace tree with p in parent[index] */
179 parent
->a
[index
] = p
;
185 * To remove a leaf_node:
186 * Search to the tree location appropriate for the given leaf_node's key:
187 * - If location does not hold a matching entry, abort and do nothing.
188 * - Copy the matching entry's value into the given entry.
189 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
190 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
192 static void note_tree_remove(struct notes_tree
*t
,
193 struct int_node
*tree
, unsigned char n
,
194 struct leaf_node
*entry
)
197 struct int_node
*parent_stack
[20];
199 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_sha1
);
201 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
202 if (GET_PTR_TYPE(*p
) != PTR_TYPE_NOTE
)
203 return; /* type mismatch, nothing to remove */
204 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
205 if (hashcmp(l
->key_sha1
, entry
->key_sha1
))
206 return; /* key mismatch, nothing to remove */
208 /* we have found a matching entry */
209 hashcpy(entry
->val_sha1
, l
->val_sha1
);
211 *p
= SET_PTR_TYPE(NULL
, PTR_TYPE_NULL
);
213 /* consolidate this tree level, and parent levels, if possible */
215 return; /* cannot consolidate top level */
216 /* first, build stack of ancestors between root and current node */
217 parent_stack
[0] = t
->root
;
218 for (i
= 0; i
< n
; i
++) {
219 j
= GET_NIBBLE(i
, entry
->key_sha1
);
220 parent_stack
[i
+ 1] = CLR_PTR_TYPE(parent_stack
[i
]->a
[j
]);
222 assert(i
== n
&& parent_stack
[i
] == tree
);
223 /* next, unwind stack until note_tree_consolidate() is done */
225 !note_tree_consolidate(parent_stack
[i
], parent_stack
[i
- 1],
226 GET_NIBBLE(i
- 1, entry
->key_sha1
)))
231 * To insert a leaf_node:
232 * Search to the tree location appropriate for the given leaf_node's key:
233 * - If location is unused (NULL), store the tweaked pointer directly there
234 * - If location holds a note entry that matches the note-to-be-inserted, then
235 * combine the two notes (by calling the given combine_notes function).
236 * - If location holds a note entry that matches the subtree-to-be-inserted,
237 * then unpack the subtree-to-be-inserted into the location.
238 * - If location holds a matching subtree entry, unpack the subtree at that
239 * location, and restart the insert operation from that level.
240 * - Else, create a new int_node, holding both the node-at-location and the
241 * node-to-be-inserted, and store the new int_node into the location.
243 static int note_tree_insert(struct notes_tree
*t
, struct int_node
*tree
,
244 unsigned char n
, struct leaf_node
*entry
, unsigned char type
,
245 combine_notes_fn combine_notes
)
247 struct int_node
*new_node
;
249 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_sha1
);
252 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
253 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
254 switch (GET_PTR_TYPE(*p
)) {
257 if (is_null_sha1(entry
->val_sha1
))
260 *p
= SET_PTR_TYPE(entry
, type
);
265 if (!hashcmp(l
->key_sha1
, entry
->key_sha1
)) {
266 /* skip concatenation if l == entry */
267 if (!hashcmp(l
->val_sha1
, entry
->val_sha1
))
270 ret
= combine_notes(l
->val_sha1
,
272 if (!ret
&& is_null_sha1(l
->val_sha1
))
273 note_tree_remove(t
, tree
, n
, entry
);
278 case PTR_TYPE_SUBTREE
:
279 if (!SUBTREE_SHA1_PREFIXCMP(l
->key_sha1
,
282 load_subtree(t
, entry
, tree
, n
);
289 case PTR_TYPE_SUBTREE
:
290 if (!SUBTREE_SHA1_PREFIXCMP(entry
->key_sha1
, l
->key_sha1
)) {
291 /* unpack 'l' and restart insert */
293 load_subtree(t
, l
, tree
, n
);
295 return note_tree_insert(t
, tree
, n
, entry
, type
,
301 /* non-matching leaf_node */
302 assert(GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
||
303 GET_PTR_TYPE(*p
) == PTR_TYPE_SUBTREE
);
304 if (is_null_sha1(entry
->val_sha1
)) { /* skip insertion of empty note */
308 new_node
= (struct int_node
*) xcalloc(1, sizeof(struct int_node
));
309 ret
= note_tree_insert(t
, new_node
, n
+ 1, l
, GET_PTR_TYPE(*p
),
313 *p
= SET_PTR_TYPE(new_node
, PTR_TYPE_INTERNAL
);
314 return note_tree_insert(t
, new_node
, n
+ 1, entry
, type
, combine_notes
);
317 /* Free the entire notes data contained in the given tree */
318 static void note_tree_free(struct int_node
*tree
)
321 for (i
= 0; i
< 16; i
++) {
322 void *p
= tree
->a
[i
];
323 switch (GET_PTR_TYPE(p
)) {
324 case PTR_TYPE_INTERNAL
:
325 note_tree_free(CLR_PTR_TYPE(p
));
328 case PTR_TYPE_SUBTREE
:
329 free(CLR_PTR_TYPE(p
));
335 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
336 * - hex - Partial SHA1 segment in ASCII hex format
337 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
338 * - sha1 - Partial SHA1 value is written here
339 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
340 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
341 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
342 * Pads sha1 with NULs up to sha1_len (not included in returned length).
344 static int get_sha1_hex_segment(const char *hex
, unsigned int hex_len
,
345 unsigned char *sha1
, unsigned int sha1_len
)
347 unsigned int i
, len
= hex_len
>> 1;
348 if (hex_len
% 2 != 0 || len
> sha1_len
)
350 for (i
= 0; i
< len
; i
++) {
351 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
357 for (; i
< sha1_len
; i
++)
362 static int non_note_cmp(const struct non_note
*a
, const struct non_note
*b
)
364 return strcmp(a
->path
, b
->path
);
367 /* note: takes ownership of path string */
368 static void add_non_note(struct notes_tree
*t
, char *path
,
369 unsigned int mode
, const unsigned char *sha1
)
371 struct non_note
*p
= t
->prev_non_note
, *n
;
372 n
= (struct non_note
*) xmalloc(sizeof(struct non_note
));
376 hashcpy(n
->sha1
, sha1
);
377 t
->prev_non_note
= n
;
379 if (!t
->first_non_note
) {
380 t
->first_non_note
= n
;
384 if (non_note_cmp(p
, n
) < 0)
386 else if (non_note_cmp(t
->first_non_note
, n
) <= 0)
387 p
= t
->first_non_note
;
389 /* n sorts before t->first_non_note */
390 n
->next
= t
->first_non_note
;
391 t
->first_non_note
= n
;
395 /* n sorts equal or after p */
396 while (p
->next
&& non_note_cmp(p
->next
, n
) <= 0)
399 if (non_note_cmp(p
, n
) == 0) { /* n ~= p; overwrite p with n */
400 assert(strcmp(p
->path
, n
->path
) == 0);
402 hashcpy(p
->sha1
, n
->sha1
);
404 t
->prev_non_note
= p
;
408 /* n sorts between p and p->next */
413 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
414 struct int_node
*node
, unsigned int n
)
416 unsigned char object_sha1
[20];
417 unsigned int prefix_len
;
419 struct tree_desc desc
;
420 struct name_entry entry
;
425 buf
= fill_tree_descriptor(&desc
, subtree
->val_sha1
);
427 die("Could not read %s for notes-index",
428 sha1_to_hex(subtree
->val_sha1
));
430 prefix_len
= subtree
->key_sha1
[19];
431 assert(prefix_len
* 2 >= n
);
432 memcpy(object_sha1
, subtree
->key_sha1
, prefix_len
);
433 while (tree_entry(&desc
, &entry
)) {
434 path_len
= strlen(entry
.path
);
435 len
= get_sha1_hex_segment(entry
.path
, path_len
,
436 object_sha1
+ prefix_len
, 20 - prefix_len
);
438 goto handle_non_note
; /* entry.path is not a SHA1 */
442 * If object SHA1 is complete (len == 20), assume note object
443 * If object SHA1 is incomplete (len < 20), and current
444 * component consists of 2 hex chars, assume note subtree
447 type
= PTR_TYPE_NOTE
;
448 l
= (struct leaf_node
*)
449 xcalloc(1, sizeof(struct leaf_node
));
450 hashcpy(l
->key_sha1
, object_sha1
);
451 hashcpy(l
->val_sha1
, entry
.oid
->hash
);
453 if (!S_ISDIR(entry
.mode
) || path_len
!= 2)
454 goto handle_non_note
; /* not subtree */
455 l
->key_sha1
[19] = (unsigned char) len
;
456 type
= PTR_TYPE_SUBTREE
;
458 if (note_tree_insert(t
, node
, n
, l
, type
,
459 combine_notes_concatenate
))
460 die("Failed to load %s %s into notes tree "
462 type
== PTR_TYPE_NOTE
? "note" : "subtree",
463 sha1_to_hex(l
->key_sha1
), t
->ref
);
469 * Determine full path for this non-note entry:
470 * The filename is already found in entry.path, but the
471 * directory part of the path must be deduced from the subtree
472 * containing this entry. We assume here that the overall notes
473 * tree follows a strict byte-based progressive fanout
474 * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
475 * e.g. 4/36 fanout). This means that if a non-note is found at
476 * path "dead/beef", the following code will register it as
477 * being found on "de/ad/beef".
478 * On the other hand, if you use such non-obvious non-note
479 * paths in the middle of a notes tree, you deserve what's
480 * coming to you ;). Note that for non-notes that are not
481 * SHA1-like at the top level, there will be no problems.
483 * To conclude, it is strongly advised to make sure non-notes
484 * have at least one non-hex character in the top-level path
488 struct strbuf non_note_path
= STRBUF_INIT
;
489 const char *q
= sha1_to_hex(subtree
->key_sha1
);
491 for (i
= 0; i
< prefix_len
; i
++) {
492 strbuf_addch(&non_note_path
, *q
++);
493 strbuf_addch(&non_note_path
, *q
++);
494 strbuf_addch(&non_note_path
, '/');
496 strbuf_addstr(&non_note_path
, entry
.path
);
497 add_non_note(t
, strbuf_detach(&non_note_path
, NULL
),
498 entry
.mode
, entry
.oid
->hash
);
505 * Determine optimal on-disk fanout for this part of the notes tree
507 * Given a (sub)tree and the level in the internal tree structure, determine
508 * whether or not the given existing fanout should be expanded for this
511 * Values of the 'fanout' variable:
512 * - 0: No fanout (all notes are stored directly in the root notes tree)
515 * - 3: 2/2/2/34 fanout
518 static unsigned char determine_fanout(struct int_node
*tree
, unsigned char n
,
519 unsigned char fanout
)
522 * The following is a simple heuristic that works well in practice:
523 * For each even-numbered 16-tree level (remember that each on-disk
524 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
525 * entries at that tree level. If all of them are either int_nodes or
526 * subtree entries, then there are likely plenty of notes below this
527 * level, so we return an incremented fanout.
530 if ((n
% 2) || (n
> 2 * fanout
))
532 for (i
= 0; i
< 16; i
++) {
533 switch (GET_PTR_TYPE(tree
->a
[i
])) {
534 case PTR_TYPE_SUBTREE
:
535 case PTR_TYPE_INTERNAL
:
544 /* hex SHA1 + 19 * '/' + NUL */
545 #define FANOUT_PATH_MAX 40 + 19 + 1
547 static void construct_path_with_fanout(const unsigned char *sha1
,
548 unsigned char fanout
, char *path
)
550 unsigned int i
= 0, j
= 0;
551 const char *hex_sha1
= sha1_to_hex(sha1
);
554 path
[i
++] = hex_sha1
[j
++];
555 path
[i
++] = hex_sha1
[j
++];
559 xsnprintf(path
+ i
, FANOUT_PATH_MAX
- i
, "%s", hex_sha1
+ j
);
562 static int for_each_note_helper(struct notes_tree
*t
, struct int_node
*tree
,
563 unsigned char n
, unsigned char fanout
, int flags
,
564 each_note_fn fn
, void *cb_data
)
570 static char path
[FANOUT_PATH_MAX
];
572 fanout
= determine_fanout(tree
, n
, fanout
);
573 for (i
= 0; i
< 16; i
++) {
576 switch (GET_PTR_TYPE(p
)) {
577 case PTR_TYPE_INTERNAL
:
578 /* recurse into int_node */
579 ret
= for_each_note_helper(t
, CLR_PTR_TYPE(p
), n
+ 1,
580 fanout
, flags
, fn
, cb_data
);
582 case PTR_TYPE_SUBTREE
:
583 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
585 * Subtree entries in the note tree represent parts of
586 * the note tree that have not yet been explored. There
587 * is a direct relationship between subtree entries at
588 * level 'n' in the tree, and the 'fanout' variable:
589 * Subtree entries at level 'n <= 2 * fanout' should be
590 * preserved, since they correspond exactly to a fanout
591 * directory in the on-disk structure. However, subtree
592 * entries at level 'n > 2 * fanout' should NOT be
593 * preserved, but rather consolidated into the above
594 * notes tree level. We achieve this by unconditionally
595 * unpacking subtree entries that exist below the
596 * threshold level at 'n = 2 * fanout'.
598 if (n
<= 2 * fanout
&&
599 flags
& FOR_EACH_NOTE_YIELD_SUBTREES
) {
600 /* invoke callback with subtree */
601 unsigned int path_len
=
602 l
->key_sha1
[19] * 2 + fanout
;
603 assert(path_len
< FANOUT_PATH_MAX
- 1);
604 construct_path_with_fanout(l
->key_sha1
, fanout
,
606 /* Create trailing slash, if needed */
607 if (path
[path_len
- 1] != '/')
608 path
[path_len
++] = '/';
609 path
[path_len
] = '\0';
610 ret
= fn(l
->key_sha1
, l
->val_sha1
, path
,
613 if (n
> fanout
* 2 ||
614 !(flags
& FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
)) {
615 /* unpack subtree and resume traversal */
617 load_subtree(t
, l
, tree
, n
);
623 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
624 construct_path_with_fanout(l
->key_sha1
, fanout
, path
);
625 ret
= fn(l
->key_sha1
, l
->val_sha1
, path
, cb_data
);
634 struct tree_write_stack
{
635 struct tree_write_stack
*next
;
637 char path
[2]; /* path to subtree in next, if any */
640 static inline int matches_tree_write_stack(struct tree_write_stack
*tws
,
641 const char *full_path
)
643 return full_path
[0] == tws
->path
[0] &&
644 full_path
[1] == tws
->path
[1] &&
648 static void write_tree_entry(struct strbuf
*buf
, unsigned int mode
,
649 const char *path
, unsigned int path_len
, const
652 strbuf_addf(buf
, "%o %.*s%c", mode
, path_len
, path
, '\0');
653 strbuf_add(buf
, sha1
, 20);
656 static void tree_write_stack_init_subtree(struct tree_write_stack
*tws
,
659 struct tree_write_stack
*n
;
661 assert(tws
->path
[0] == '\0' && tws
->path
[1] == '\0');
662 n
= (struct tree_write_stack
*)
663 xmalloc(sizeof(struct tree_write_stack
));
665 strbuf_init(&n
->buf
, 256 * (32 + 40)); /* assume 256 entries per tree */
666 n
->path
[0] = n
->path
[1] = '\0';
668 tws
->path
[0] = path
[0];
669 tws
->path
[1] = path
[1];
672 static int tree_write_stack_finish_subtree(struct tree_write_stack
*tws
)
675 struct tree_write_stack
*n
= tws
->next
;
678 ret
= tree_write_stack_finish_subtree(n
);
681 ret
= write_sha1_file(n
->buf
.buf
, n
->buf
.len
, tree_type
, s
);
684 strbuf_release(&n
->buf
);
687 write_tree_entry(&tws
->buf
, 040000, tws
->path
, 2, s
);
688 tws
->path
[0] = tws
->path
[1] = '\0';
693 static int write_each_note_helper(struct tree_write_stack
*tws
,
694 const char *path
, unsigned int mode
,
695 const unsigned char *sha1
)
697 size_t path_len
= strlen(path
);
701 /* Determine common part of tree write stack */
702 while (tws
&& 3 * n
< path_len
&&
703 matches_tree_write_stack(tws
, path
+ 3 * n
)) {
708 /* tws point to last matching tree_write_stack entry */
709 ret
= tree_write_stack_finish_subtree(tws
);
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
);
720 /* There should be no more directory components in the given path */
721 assert(memchr(path
+ 3 * n
, '/', path_len
- (3 * n
)) == NULL
);
723 /* Finally add given entry to the current tree object */
724 write_tree_entry(&tws
->buf
, mode
, path
+ 3 * n
, path_len
- (3 * n
),
730 struct write_each_note_data
{
731 struct tree_write_stack
*root
;
732 struct non_note
*next_non_note
;
735 static int write_each_non_note_until(const char *note_path
,
736 struct write_each_note_data
*d
)
738 struct non_note
*n
= d
->next_non_note
;
740 while (n
&& (!note_path
|| (cmp
= strcmp(n
->path
, note_path
)) <= 0)) {
741 if (note_path
&& cmp
== 0)
742 ; /* do nothing, prefer note to non-note */
744 ret
= write_each_note_helper(d
->root
, n
->path
, n
->mode
,
751 d
->next_non_note
= n
;
755 static int write_each_note(const unsigned char *object_sha1
,
756 const unsigned char *note_sha1
, char *note_path
,
759 struct write_each_note_data
*d
=
760 (struct write_each_note_data
*) cb_data
;
761 size_t note_path_len
= strlen(note_path
);
762 unsigned int mode
= 0100644;
764 if (note_path
[note_path_len
- 1] == '/') {
767 note_path
[note_path_len
] = '\0';
770 assert(note_path_len
<= 40 + 19);
772 /* Weave non-note entries into note entries */
773 return write_each_non_note_until(note_path
, d
) ||
774 write_each_note_helper(d
->root
, note_path
, mode
, note_sha1
);
777 struct note_delete_list
{
778 struct note_delete_list
*next
;
779 const unsigned char *sha1
;
782 static int prune_notes_helper(const unsigned char *object_sha1
,
783 const unsigned char *note_sha1
, char *note_path
,
786 struct note_delete_list
**l
= (struct note_delete_list
**) cb_data
;
787 struct note_delete_list
*n
;
789 if (has_sha1_file(object_sha1
))
790 return 0; /* nothing to do for this note */
792 /* failed to find object => prune this note */
793 n
= (struct note_delete_list
*) xmalloc(sizeof(*n
));
795 n
->sha1
= object_sha1
;
800 int combine_notes_concatenate(unsigned char *cur_sha1
,
801 const unsigned char *new_sha1
)
803 char *cur_msg
= NULL
, *new_msg
= NULL
, *buf
;
804 unsigned long cur_len
, new_len
, buf_len
;
805 enum object_type cur_type
, new_type
;
808 /* read in both note blob objects */
809 if (!is_null_sha1(new_sha1
))
810 new_msg
= read_sha1_file(new_sha1
, &new_type
, &new_len
);
811 if (!new_msg
|| !new_len
|| new_type
!= OBJ_BLOB
) {
815 if (!is_null_sha1(cur_sha1
))
816 cur_msg
= read_sha1_file(cur_sha1
, &cur_type
, &cur_len
);
817 if (!cur_msg
|| !cur_len
|| cur_type
!= OBJ_BLOB
) {
820 hashcpy(cur_sha1
, new_sha1
);
824 /* we will separate the notes by two newlines anyway */
825 if (cur_msg
[cur_len
- 1] == '\n')
828 /* concatenate cur_msg and new_msg into buf */
829 buf_len
= cur_len
+ 2 + new_len
;
830 buf
= (char *) xmalloc(buf_len
);
831 memcpy(buf
, cur_msg
, cur_len
);
833 buf
[cur_len
+ 1] = '\n';
834 memcpy(buf
+ cur_len
+ 2, new_msg
, new_len
);
838 /* create a new blob object from buf */
839 ret
= write_sha1_file(buf
, buf_len
, blob_type
, cur_sha1
);
844 int combine_notes_overwrite(unsigned char *cur_sha1
,
845 const unsigned char *new_sha1
)
847 hashcpy(cur_sha1
, new_sha1
);
851 int combine_notes_ignore(unsigned char *cur_sha1
,
852 const unsigned char *new_sha1
)
858 * Add the lines from the named object to list, with trailing
861 static int string_list_add_note_lines(struct string_list
*list
,
862 const unsigned char *sha1
)
868 if (is_null_sha1(sha1
))
871 /* read_sha1_file NUL-terminates */
872 data
= read_sha1_file(sha1
, &t
, &len
);
873 if (t
!= OBJ_BLOB
|| !data
|| !len
) {
875 return t
!= OBJ_BLOB
|| !data
;
879 * If the last line of the file is EOL-terminated, this will
880 * add an empty string to the list. But it will be removed
881 * later, along with any empty strings that came from empty
882 * lines within the file.
884 string_list_split(list
, data
, '\n', -1);
889 static int string_list_join_lines_helper(struct string_list_item
*item
,
892 struct strbuf
*buf
= cb_data
;
893 strbuf_addstr(buf
, item
->string
);
894 strbuf_addch(buf
, '\n');
898 int combine_notes_cat_sort_uniq(unsigned char *cur_sha1
,
899 const unsigned char *new_sha1
)
901 struct string_list sort_uniq_list
= STRING_LIST_INIT_DUP
;
902 struct strbuf buf
= STRBUF_INIT
;
905 /* read both note blob objects into unique_lines */
906 if (string_list_add_note_lines(&sort_uniq_list
, cur_sha1
))
908 if (string_list_add_note_lines(&sort_uniq_list
, new_sha1
))
910 string_list_remove_empty_items(&sort_uniq_list
, 0);
911 string_list_sort(&sort_uniq_list
);
912 string_list_remove_duplicates(&sort_uniq_list
, 0);
914 /* create a new blob object from sort_uniq_list */
915 if (for_each_string_list(&sort_uniq_list
,
916 string_list_join_lines_helper
, &buf
))
919 ret
= write_sha1_file(buf
.buf
, buf
.len
, blob_type
, cur_sha1
);
922 strbuf_release(&buf
);
923 string_list_clear(&sort_uniq_list
, 0);
927 static int string_list_add_one_ref(const char *refname
, const struct object_id
*oid
,
930 struct string_list
*refs
= cb
;
931 if (!unsorted_string_list_has_string(refs
, refname
))
932 string_list_append(refs
, refname
);
937 * The list argument must have strdup_strings set on it.
939 void string_list_add_refs_by_glob(struct string_list
*list
, const char *glob
)
941 assert(list
->strdup_strings
);
942 if (has_glob_specials(glob
)) {
943 for_each_glob_ref(string_list_add_one_ref
, glob
, list
);
945 unsigned char sha1
[20];
946 if (get_sha1(glob
, sha1
))
947 warning("notes ref %s is invalid", glob
);
948 if (!unsorted_string_list_has_string(list
, glob
))
949 string_list_append(list
, glob
);
953 void string_list_add_refs_from_colon_sep(struct string_list
*list
,
956 struct string_list split
= STRING_LIST_INIT_NODUP
;
957 char *globs_copy
= xstrdup(globs
);
960 string_list_split_in_place(&split
, globs_copy
, ':', -1);
961 string_list_remove_empty_items(&split
, 0);
963 for (i
= 0; i
< split
.nr
; i
++)
964 string_list_add_refs_by_glob(list
, split
.items
[i
].string
);
966 string_list_clear(&split
, 0);
970 static int notes_display_config(const char *k
, const char *v
, void *cb
)
974 if (*load_refs
&& !strcmp(k
, "notes.displayref")) {
976 config_error_nonbool(k
);
977 string_list_add_refs_by_glob(&display_notes_refs
, v
);
983 const char *default_notes_ref(void)
985 const char *notes_ref
= NULL
;
987 notes_ref
= getenv(GIT_NOTES_REF_ENVIRONMENT
);
989 notes_ref
= notes_ref_name
; /* value of core.notesRef config */
991 notes_ref
= GIT_NOTES_DEFAULT_REF
;
995 void init_notes(struct notes_tree
*t
, const char *notes_ref
,
996 combine_notes_fn combine_notes
, int flags
)
998 struct object_id oid
, object_oid
;
1000 struct leaf_node root_tree
;
1003 t
= &default_notes_tree
;
1004 assert(!t
->initialized
);
1007 notes_ref
= default_notes_ref();
1010 combine_notes
= combine_notes_concatenate
;
1012 t
->root
= (struct int_node
*) xcalloc(1, sizeof(struct int_node
));
1013 t
->first_non_note
= NULL
;
1014 t
->prev_non_note
= NULL
;
1015 t
->ref
= xstrdup_or_null(notes_ref
);
1016 t
->update_ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->ref
: NULL
;
1017 t
->combine_notes
= combine_notes
;
1021 if (flags
& NOTES_INIT_EMPTY
|| !notes_ref
||
1022 get_sha1_treeish(notes_ref
, object_oid
.hash
))
1024 if (flags
& NOTES_INIT_WRITABLE
&& read_ref(notes_ref
, object_oid
.hash
))
1025 die("Cannot use notes ref %s", notes_ref
);
1026 if (get_tree_entry(object_oid
.hash
, "", oid
.hash
, &mode
))
1027 die("Failed to read notes tree referenced by %s (%s)",
1028 notes_ref
, oid_to_hex(&object_oid
));
1030 hashclr(root_tree
.key_sha1
);
1031 hashcpy(root_tree
.val_sha1
, oid
.hash
);
1032 load_subtree(t
, &root_tree
, t
->root
, 0);
1035 struct notes_tree
**load_notes_trees(struct string_list
*refs
, int flags
)
1037 struct string_list_item
*item
;
1039 struct notes_tree
**trees
;
1040 ALLOC_ARRAY(trees
, refs
->nr
+ 1);
1041 for_each_string_list_item(item
, refs
) {
1042 struct notes_tree
*t
= xcalloc(1, sizeof(struct notes_tree
));
1043 init_notes(t
, item
->string
, combine_notes_ignore
, flags
);
1044 trees
[counter
++] = t
;
1046 trees
[counter
] = NULL
;
1050 void init_display_notes(struct display_notes_opt
*opt
)
1052 char *display_ref_env
;
1053 int load_config_refs
= 0;
1054 display_notes_refs
.strdup_strings
= 1;
1056 assert(!display_notes_trees
);
1058 if (!opt
|| opt
->use_default_notes
> 0 ||
1059 (opt
->use_default_notes
== -1 && !opt
->extra_notes_refs
.nr
)) {
1060 string_list_append(&display_notes_refs
, default_notes_ref());
1061 display_ref_env
= getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT
);
1062 if (display_ref_env
) {
1063 string_list_add_refs_from_colon_sep(&display_notes_refs
,
1065 load_config_refs
= 0;
1067 load_config_refs
= 1;
1070 git_config(notes_display_config
, &load_config_refs
);
1073 struct string_list_item
*item
;
1074 for_each_string_list_item(item
, &opt
->extra_notes_refs
)
1075 string_list_add_refs_by_glob(&display_notes_refs
,
1079 display_notes_trees
= load_notes_trees(&display_notes_refs
, 0);
1080 string_list_clear(&display_notes_refs
, 0);
1083 int add_note(struct notes_tree
*t
, const unsigned char *object_sha1
,
1084 const unsigned char *note_sha1
, combine_notes_fn combine_notes
)
1086 struct leaf_node
*l
;
1089 t
= &default_notes_tree
;
1090 assert(t
->initialized
);
1093 combine_notes
= t
->combine_notes
;
1094 l
= (struct leaf_node
*) xmalloc(sizeof(struct leaf_node
));
1095 hashcpy(l
->key_sha1
, object_sha1
);
1096 hashcpy(l
->val_sha1
, note_sha1
);
1097 return note_tree_insert(t
, t
->root
, 0, l
, PTR_TYPE_NOTE
, combine_notes
);
1100 int remove_note(struct notes_tree
*t
, const unsigned char *object_sha1
)
1105 t
= &default_notes_tree
;
1106 assert(t
->initialized
);
1107 hashcpy(l
.key_sha1
, object_sha1
);
1108 hashclr(l
.val_sha1
);
1109 note_tree_remove(t
, t
->root
, 0, &l
);
1110 if (is_null_sha1(l
.val_sha1
)) /* no note was removed */
1116 const unsigned char *get_note(struct notes_tree
*t
,
1117 const unsigned char *object_sha1
)
1119 struct leaf_node
*found
;
1122 t
= &default_notes_tree
;
1123 assert(t
->initialized
);
1124 found
= note_tree_find(t
, t
->root
, 0, object_sha1
);
1125 return found
? found
->val_sha1
: NULL
;
1128 int for_each_note(struct notes_tree
*t
, int flags
, each_note_fn fn
,
1132 t
= &default_notes_tree
;
1133 assert(t
->initialized
);
1134 return for_each_note_helper(t
, t
->root
, 0, 0, flags
, fn
, cb_data
);
1137 int write_notes_tree(struct notes_tree
*t
, unsigned char *result
)
1139 struct tree_write_stack root
;
1140 struct write_each_note_data cb_data
;
1144 t
= &default_notes_tree
;
1145 assert(t
->initialized
);
1147 /* Prepare for traversal of current notes tree */
1148 root
.next
= NULL
; /* last forward entry in list is grounded */
1149 strbuf_init(&root
.buf
, 256 * (32 + 40)); /* assume 256 entries */
1150 root
.path
[0] = root
.path
[1] = '\0';
1151 cb_data
.root
= &root
;
1152 cb_data
.next_non_note
= t
->first_non_note
;
1154 /* Write tree objects representing current notes tree */
1155 ret
= for_each_note(t
, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
|
1156 FOR_EACH_NOTE_YIELD_SUBTREES
,
1157 write_each_note
, &cb_data
) ||
1158 write_each_non_note_until(NULL
, &cb_data
) ||
1159 tree_write_stack_finish_subtree(&root
) ||
1160 write_sha1_file(root
.buf
.buf
, root
.buf
.len
, tree_type
, result
);
1161 strbuf_release(&root
.buf
);
1165 void prune_notes(struct notes_tree
*t
, int flags
)
1167 struct note_delete_list
*l
= NULL
;
1170 t
= &default_notes_tree
;
1171 assert(t
->initialized
);
1173 for_each_note(t
, 0, prune_notes_helper
, &l
);
1176 if (flags
& NOTES_PRUNE_VERBOSE
)
1177 printf("%s\n", sha1_to_hex(l
->sha1
));
1178 if (!(flags
& NOTES_PRUNE_DRYRUN
))
1179 remove_note(t
, l
->sha1
);
1184 void free_notes(struct notes_tree
*t
)
1187 t
= &default_notes_tree
;
1189 note_tree_free(t
->root
);
1191 while (t
->first_non_note
) {
1192 t
->prev_non_note
= t
->first_non_note
->next
;
1193 free(t
->first_non_note
->path
);
1194 free(t
->first_non_note
);
1195 t
->first_non_note
= t
->prev_non_note
;
1198 memset(t
, 0, sizeof(struct notes_tree
));
1202 * Fill the given strbuf with the notes associated with the given object.
1204 * If the given notes_tree structure is not initialized, it will be auto-
1205 * initialized to the default value (see documentation for init_notes() above).
1206 * If the given notes_tree is NULL, the internal/default notes_tree will be
1209 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1210 * for human consumption.
1212 static void format_note(struct notes_tree
*t
, const unsigned char *object_sha1
,
1213 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1215 static const char utf8
[] = "utf-8";
1216 const unsigned char *sha1
;
1218 unsigned long linelen
, msglen
;
1219 enum object_type type
;
1222 t
= &default_notes_tree
;
1223 if (!t
->initialized
)
1224 init_notes(t
, NULL
, NULL
, 0);
1226 sha1
= get_note(t
, object_sha1
);
1230 if (!(msg
= read_sha1_file(sha1
, &type
, &msglen
)) || type
!= OBJ_BLOB
) {
1235 if (output_encoding
&& *output_encoding
&&
1236 !is_encoding_utf8(output_encoding
)) {
1237 char *reencoded
= reencode_string(msg
, output_encoding
, utf8
);
1241 msglen
= strlen(msg
);
1245 /* we will end the annotation by a newline anyway */
1246 if (msglen
&& msg
[msglen
- 1] == '\n')
1250 const char *ref
= t
->ref
;
1251 if (!ref
|| !strcmp(ref
, GIT_NOTES_DEFAULT_REF
)) {
1252 strbuf_addstr(sb
, "\nNotes:\n");
1254 if (starts_with(ref
, "refs/"))
1256 if (starts_with(ref
, "notes/"))
1258 strbuf_addf(sb
, "\nNotes (%s):\n", ref
);
1262 for (msg_p
= msg
; msg_p
< msg
+ msglen
; msg_p
+= linelen
+ 1) {
1263 linelen
= strchrnul(msg_p
, '\n') - msg_p
;
1266 strbuf_addstr(sb
, " ");
1267 strbuf_add(sb
, msg_p
, linelen
);
1268 strbuf_addch(sb
, '\n');
1274 void format_display_notes(const unsigned char *object_sha1
,
1275 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1278 assert(display_notes_trees
);
1279 for (i
= 0; display_notes_trees
[i
]; i
++)
1280 format_note(display_notes_trees
[i
], object_sha1
, sb
,
1281 output_encoding
, raw
);
1284 int copy_note(struct notes_tree
*t
,
1285 const unsigned char *from_obj
, const unsigned char *to_obj
,
1286 int force
, combine_notes_fn combine_notes
)
1288 const unsigned char *note
= get_note(t
, from_obj
);
1289 const unsigned char *existing_note
= get_note(t
, to_obj
);
1291 if (!force
&& existing_note
)
1295 return add_note(t
, to_obj
, note
, combine_notes
);
1296 else if (existing_note
)
1297 return add_note(t
, to_obj
, null_sha1
, combine_notes
);
1302 void expand_notes_ref(struct strbuf
*sb
)
1304 if (starts_with(sb
->buf
, "refs/notes/"))
1305 return; /* we're happy */
1306 else if (starts_with(sb
->buf
, "notes/"))
1307 strbuf_insert(sb
, 0, "refs/", 5);
1309 strbuf_insert(sb
, 0, "refs/notes/", 11);
1312 void expand_loose_notes_ref(struct strbuf
*sb
)
1314 unsigned char object
[20];
1316 if (get_sha1(sb
->buf
, object
)) {
1317 /* fallback to expand_notes_ref */
1318 expand_notes_ref(sb
);