1 #include "git-compat-util.h"
3 #include "environment.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
12 #include "tree-walk.h"
13 #include "string-list.h"
17 * Use a non-balancing simple 16-tree structure with struct int_node as
18 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
19 * 16-array of pointers to its children.
20 * The bottom 2 bits of each pointer is used to identify the pointer type
21 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
22 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
23 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
24 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
26 * The root node is a statically allocated struct int_node.
33 * Leaf nodes come in two variants, note entries and subtree entries,
34 * distinguished by the LSb of the leaf node pointer (see above).
35 * As a note entry, the key is the SHA1 of the referenced object, and the
36 * value is the SHA1 of the note object.
37 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
38 * referenced object, using the last byte of the key to store the length of
39 * the prefix. The value is the SHA1 of the tree object containing the notes
43 struct object_id key_oid
;
44 struct object_id val_oid
;
48 * A notes tree may contain entries that are not notes, and that do not follow
49 * the naming conventions of notes. There are typically none/few of these, but
50 * we still need to keep track of them. Keep a simple linked list sorted alpha-
51 * betically on the non-note path. The list is populated when parsing tree
52 * objects in load_subtree(), and the non-notes are correctly written back into
53 * the tree objects produced by write_notes_tree().
56 struct non_note
*next
; /* grounded (last->next == NULL) */
62 #define PTR_TYPE_NULL 0
63 #define PTR_TYPE_INTERNAL 1
64 #define PTR_TYPE_NOTE 2
65 #define PTR_TYPE_SUBTREE 3
67 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
68 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
69 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
71 #define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
73 #define KEY_INDEX (the_hash_algo->rawsz - 1)
74 #define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
75 #define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
76 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
77 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
79 struct notes_tree default_notes_tree
;
81 static struct string_list display_notes_refs
= STRING_LIST_INIT_NODUP
;
82 static struct notes_tree
**display_notes_trees
;
84 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
85 struct int_node
*node
, unsigned int n
);
88 * Search the tree until the appropriate location for the given key is found:
89 * 1. Start at the root node, with n = 0
90 * 2. If a[0] at the current level is a matching subtree entry, unpack that
91 * subtree entry and remove it; restart search at the current level.
92 * 3. Use the nth nibble of the key as an index into a:
93 * - If a[n] is an int_node, recurse from #2 into that node and increment n
94 * - If a matching subtree entry, unpack that subtree entry (and remove it);
95 * restart search at the current level.
96 * - Otherwise, we have found one of the following:
97 * - a subtree entry which does not match the key
98 * - a note entry which may or may not match the key
99 * - an unused leaf node (NULL)
100 * In any case, set *tree and *n, and return pointer to the tree location.
102 static void **note_tree_search(struct notes_tree
*t
, struct int_node
**tree
,
103 unsigned char *n
, const unsigned char *key_sha1
)
107 void *p
= (*tree
)->a
[0];
109 if (GET_PTR_TYPE(p
) == PTR_TYPE_SUBTREE
) {
110 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
111 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_oid
.hash
)) {
112 /* unpack tree and resume search */
113 (*tree
)->a
[0] = NULL
;
114 load_subtree(t
, l
, *tree
, *n
);
116 return note_tree_search(t
, tree
, n
, key_sha1
);
120 i
= GET_NIBBLE(*n
, key_sha1
);
122 switch (GET_PTR_TYPE(p
)) {
123 case PTR_TYPE_INTERNAL
:
124 *tree
= CLR_PTR_TYPE(p
);
126 return note_tree_search(t
, tree
, n
, key_sha1
);
127 case PTR_TYPE_SUBTREE
:
128 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
129 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_oid
.hash
)) {
130 /* unpack tree and resume search */
131 (*tree
)->a
[i
] = NULL
;
132 load_subtree(t
, l
, *tree
, *n
);
134 return note_tree_search(t
, tree
, n
, key_sha1
);
138 return &((*tree
)->a
[i
]);
143 * To find a leaf_node:
144 * Search to the tree location appropriate for the given key:
145 * If a note entry with matching key, return the note entry, else return NULL.
147 static struct leaf_node
*note_tree_find(struct notes_tree
*t
,
148 struct int_node
*tree
, unsigned char n
,
149 const unsigned char *key_sha1
)
151 void **p
= note_tree_search(t
, &tree
, &n
, key_sha1
);
152 if (GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
) {
153 struct leaf_node
*l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
154 if (hasheq(key_sha1
, l
->key_oid
.hash
))
161 * How to consolidate an int_node:
162 * If there are > 1 non-NULL entries, give up and return non-zero.
163 * Otherwise replace the int_node at the given index in the given parent node
164 * with the only NOTE entry (or a NULL entry if no entries) from the given
165 * tree, and return 0.
167 static int note_tree_consolidate(struct int_node
*tree
,
168 struct int_node
*parent
, unsigned char index
)
173 assert(tree
&& parent
);
174 assert(CLR_PTR_TYPE(parent
->a
[index
]) == tree
);
176 for (i
= 0; i
< 16; i
++) {
177 if (GET_PTR_TYPE(tree
->a
[i
]) != PTR_TYPE_NULL
) {
178 if (p
) /* more than one entry */
184 if (p
&& (GET_PTR_TYPE(p
) != PTR_TYPE_NOTE
))
186 /* replace tree with p in parent[index] */
187 parent
->a
[index
] = p
;
193 * To remove a leaf_node:
194 * Search to the tree location appropriate for the given leaf_node's key:
195 * - If location does not hold a matching entry, abort and do nothing.
196 * - Copy the matching entry's value into the given entry.
197 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
198 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
200 static void note_tree_remove(struct notes_tree
*t
,
201 struct int_node
*tree
, unsigned char n
,
202 struct leaf_node
*entry
)
205 struct int_node
*parent_stack
[GIT_MAX_RAWSZ
];
207 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_oid
.hash
);
209 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
210 if (GET_PTR_TYPE(*p
) != PTR_TYPE_NOTE
)
211 return; /* type mismatch, nothing to remove */
212 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
213 if (!oideq(&l
->key_oid
, &entry
->key_oid
))
214 return; /* key mismatch, nothing to remove */
216 /* we have found a matching entry */
217 oidcpy(&entry
->val_oid
, &l
->val_oid
);
219 *p
= SET_PTR_TYPE(NULL
, PTR_TYPE_NULL
);
221 /* consolidate this tree level, and parent levels, if possible */
223 return; /* cannot consolidate top level */
224 /* first, build stack of ancestors between root and current node */
225 parent_stack
[0] = t
->root
;
226 for (i
= 0; i
< n
; i
++) {
227 j
= GET_NIBBLE(i
, entry
->key_oid
.hash
);
228 parent_stack
[i
+ 1] = CLR_PTR_TYPE(parent_stack
[i
]->a
[j
]);
230 assert(i
== n
&& parent_stack
[i
] == tree
);
231 /* next, unwind stack until note_tree_consolidate() is done */
233 !note_tree_consolidate(parent_stack
[i
], parent_stack
[i
- 1],
234 GET_NIBBLE(i
- 1, entry
->key_oid
.hash
)))
239 * To insert a leaf_node:
240 * Search to the tree location appropriate for the given leaf_node's key:
241 * - If location is unused (NULL), store the tweaked pointer directly there
242 * - If location holds a note entry that matches the note-to-be-inserted, then
243 * combine the two notes (by calling the given combine_notes function).
244 * - If location holds a note entry that matches the subtree-to-be-inserted,
245 * then unpack the subtree-to-be-inserted into the location.
246 * - If location holds a matching subtree entry, unpack the subtree at that
247 * location, and restart the insert operation from that level.
248 * - Else, create a new int_node, holding both the node-at-location and the
249 * node-to-be-inserted, and store the new int_node into the location.
251 static int note_tree_insert(struct notes_tree
*t
, struct int_node
*tree
,
252 unsigned char n
, struct leaf_node
*entry
, unsigned char type
,
253 combine_notes_fn combine_notes
)
255 struct int_node
*new_node
;
257 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_oid
.hash
);
260 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
261 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
262 switch (GET_PTR_TYPE(*p
)) {
265 if (is_null_oid(&entry
->val_oid
))
268 *p
= SET_PTR_TYPE(entry
, type
);
273 if (oideq(&l
->key_oid
, &entry
->key_oid
)) {
274 /* skip concatenation if l == entry */
275 if (oideq(&l
->val_oid
, &entry
->val_oid
)) {
280 ret
= combine_notes(&l
->val_oid
,
282 if (!ret
&& is_null_oid(&l
->val_oid
))
283 note_tree_remove(t
, tree
, n
, entry
);
288 case PTR_TYPE_SUBTREE
:
289 if (!SUBTREE_SHA1_PREFIXCMP(l
->key_oid
.hash
,
290 entry
->key_oid
.hash
)) {
292 load_subtree(t
, entry
, tree
, n
);
299 case PTR_TYPE_SUBTREE
:
300 if (!SUBTREE_SHA1_PREFIXCMP(entry
->key_oid
.hash
, l
->key_oid
.hash
)) {
301 /* unpack 'l' and restart insert */
303 load_subtree(t
, l
, tree
, n
);
305 return note_tree_insert(t
, tree
, n
, entry
, type
,
311 /* non-matching leaf_node */
312 assert(GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
||
313 GET_PTR_TYPE(*p
) == PTR_TYPE_SUBTREE
);
314 if (is_null_oid(&entry
->val_oid
)) { /* skip insertion of empty note */
318 new_node
= (struct int_node
*) xcalloc(1, sizeof(struct int_node
));
319 ret
= note_tree_insert(t
, new_node
, n
+ 1, l
, GET_PTR_TYPE(*p
),
323 *p
= SET_PTR_TYPE(new_node
, PTR_TYPE_INTERNAL
);
324 return note_tree_insert(t
, new_node
, n
+ 1, entry
, type
, combine_notes
);
327 /* Free the entire notes data contained in the given tree */
328 static void note_tree_free(struct int_node
*tree
)
331 for (i
= 0; i
< 16; i
++) {
332 void *p
= tree
->a
[i
];
333 switch (GET_PTR_TYPE(p
)) {
334 case PTR_TYPE_INTERNAL
:
335 note_tree_free(CLR_PTR_TYPE(p
));
338 case PTR_TYPE_SUBTREE
:
339 free(CLR_PTR_TYPE(p
));
344 static int non_note_cmp(const struct non_note
*a
, const struct non_note
*b
)
346 return strcmp(a
->path
, b
->path
);
349 /* note: takes ownership of path string */
350 static void add_non_note(struct notes_tree
*t
, 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
));
358 oidread(&n
->oid
, 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 oidcpy(&p
->oid
, &n
->oid
);
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 struct object_id object_oid
;
401 struct tree_desc desc
;
402 struct name_entry entry
;
403 const unsigned hashsz
= the_hash_algo
->rawsz
;
405 buf
= fill_tree_descriptor(the_repository
, &desc
, &subtree
->val_oid
);
407 die("Could not read %s for notes-index",
408 oid_to_hex(&subtree
->val_oid
));
410 prefix_len
= subtree
->key_oid
.hash
[KEY_INDEX
];
411 if (prefix_len
>= hashsz
)
412 BUG("prefix_len (%"PRIuMAX
") is out of range", (uintmax_t)prefix_len
);
413 if (prefix_len
* 2 < n
)
414 BUG("prefix_len (%"PRIuMAX
") is too small", (uintmax_t)prefix_len
);
415 memcpy(object_oid
.hash
, subtree
->key_oid
.hash
, prefix_len
);
416 while (tree_entry(&desc
, &entry
)) {
419 size_t path_len
= strlen(entry
.path
);
421 if (path_len
== 2 * (hashsz
- prefix_len
)) {
422 /* This is potentially the remainder of the SHA-1 */
424 if (!S_ISREG(entry
.mode
))
425 /* notes must be blobs */
426 goto handle_non_note
;
428 if (hex_to_bytes(object_oid
.hash
+ prefix_len
, entry
.path
,
429 hashsz
- prefix_len
))
430 goto handle_non_note
; /* entry.path is not a SHA1 */
432 type
= PTR_TYPE_NOTE
;
433 } else if (path_len
== 2) {
434 /* This is potentially an internal node */
435 size_t len
= prefix_len
;
437 if (!S_ISDIR(entry
.mode
))
438 /* internal nodes must be trees */
439 goto handle_non_note
;
441 if (hex_to_bytes(object_oid
.hash
+ len
++, entry
.path
, 1))
442 goto handle_non_note
; /* entry.path is not a SHA1 */
445 * Pad the rest of the SHA-1 with zeros,
446 * except for the last byte, where we write
449 memset(object_oid
.hash
+ len
, 0, hashsz
- len
- 1);
450 object_oid
.hash
[KEY_INDEX
] = (unsigned char)len
;
452 type
= PTR_TYPE_SUBTREE
;
454 /* This can't be part of a note */
455 goto handle_non_note
;
459 oidcpy(&l
->key_oid
, &object_oid
);
460 oidcpy(&l
->val_oid
, &entry
.oid
);
461 oid_set_algo(&l
->key_oid
, the_hash_algo
);
462 oid_set_algo(&l
->val_oid
, the_hash_algo
);
463 if (note_tree_insert(t
, node
, n
, l
, type
,
464 combine_notes_concatenate
))
465 die("Failed to load %s %s into notes tree "
467 type
== PTR_TYPE_NOTE
? "note" : "subtree",
468 oid_to_hex(&object_oid
), t
->ref
);
474 * Determine full path for this non-note entry. The
475 * filename is already found in entry.path, but the
476 * directory part of the path must be deduced from the
477 * subtree containing this entry based on our
478 * knowledge that the overall notes tree follows a
479 * strict byte-based progressive fanout structure
480 * (i.e. using 2/38, 2/2/36, etc. fanouts).
483 struct strbuf non_note_path
= STRBUF_INIT
;
484 const char *q
= oid_to_hex(&subtree
->key_oid
);
486 for (i
= 0; i
< prefix_len
; i
++) {
487 strbuf_addch(&non_note_path
, *q
++);
488 strbuf_addch(&non_note_path
, *q
++);
489 strbuf_addch(&non_note_path
, '/');
491 strbuf_addstr(&non_note_path
, entry
.path
);
492 oid_set_algo(&entry
.oid
, the_hash_algo
);
493 add_non_note(t
, strbuf_detach(&non_note_path
, NULL
),
494 entry
.mode
, entry
.oid
.hash
);
501 * Determine optimal on-disk fanout for this part of the notes tree
503 * Given a (sub)tree and the level in the internal tree structure, determine
504 * whether or not the given existing fanout should be expanded for this
507 * Values of the 'fanout' variable:
508 * - 0: No fanout (all notes are stored directly in the root notes tree)
511 * - 3: 2/2/2/34 fanout
514 static unsigned char determine_fanout(struct int_node
*tree
, unsigned char n
,
515 unsigned char fanout
)
518 * The following is a simple heuristic that works well in practice:
519 * For each even-numbered 16-tree level (remember that each on-disk
520 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
521 * entries at that tree level. If all of them are either int_nodes or
522 * subtree entries, then there are likely plenty of notes below this
523 * level, so we return an incremented fanout.
526 if ((n
% 2) || (n
> 2 * fanout
))
528 for (i
= 0; i
< 16; i
++) {
529 switch (GET_PTR_TYPE(tree
->a
[i
])) {
530 case PTR_TYPE_SUBTREE
:
531 case PTR_TYPE_INTERNAL
:
540 /* hex oid + '/' between each pair of hex digits + NUL */
541 #define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
543 static void construct_path_with_fanout(const unsigned char *hash
,
544 unsigned char fanout
, char *path
)
546 unsigned int i
= 0, j
= 0;
547 const char *hex_hash
= hash_to_hex(hash
);
548 assert(fanout
< the_hash_algo
->rawsz
);
550 path
[i
++] = hex_hash
[j
++];
551 path
[i
++] = hex_hash
[j
++];
555 xsnprintf(path
+ i
, FANOUT_PATH_MAX
- i
, "%s", hex_hash
+ j
);
558 static int for_each_note_helper(struct notes_tree
*t
, struct int_node
*tree
,
559 unsigned char n
, unsigned char fanout
, int flags
,
560 each_note_fn fn
, void *cb_data
)
566 static char path
[FANOUT_PATH_MAX
];
568 fanout
= determine_fanout(tree
, n
, fanout
);
569 for (i
= 0; i
< 16; i
++) {
572 switch (GET_PTR_TYPE(p
)) {
573 case PTR_TYPE_INTERNAL
:
574 /* recurse into int_node */
575 ret
= for_each_note_helper(t
, CLR_PTR_TYPE(p
), n
+ 1,
576 fanout
, flags
, fn
, cb_data
);
578 case PTR_TYPE_SUBTREE
:
579 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
581 * Subtree entries in the note tree represent parts of
582 * the note tree that have not yet been explored. There
583 * is a direct relationship between subtree entries at
584 * level 'n' in the tree, and the 'fanout' variable:
585 * Subtree entries at level 'n < 2 * fanout' should be
586 * preserved, since they correspond exactly to a fanout
587 * directory in the on-disk structure. However, subtree
588 * entries at level 'n >= 2 * fanout' should NOT be
589 * preserved, but rather consolidated into the above
590 * notes tree level. We achieve this by unconditionally
591 * unpacking subtree entries that exist below the
592 * threshold level at 'n = 2 * fanout'.
594 if (n
< 2 * fanout
&&
595 flags
& FOR_EACH_NOTE_YIELD_SUBTREES
) {
596 /* invoke callback with subtree */
597 unsigned int path_len
=
598 l
->key_oid
.hash
[KEY_INDEX
] * 2 + fanout
;
599 assert(path_len
< FANOUT_PATH_MAX
- 1);
600 construct_path_with_fanout(l
->key_oid
.hash
,
603 /* Create trailing slash, if needed */
604 if (path
[path_len
- 1] != '/')
605 path
[path_len
++] = '/';
606 path
[path_len
] = '\0';
607 ret
= fn(&l
->key_oid
, &l
->val_oid
,
611 if (n
>= 2 * fanout
||
612 !(flags
& FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
)) {
613 /* unpack subtree and resume traversal */
615 load_subtree(t
, l
, tree
, n
);
621 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
622 construct_path_with_fanout(l
->key_oid
.hash
, fanout
,
624 ret
= fn(&l
->key_oid
, &l
->val_oid
, path
,
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
, hash
, the_hash_algo
->rawsz
);
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 + the_hash_algo
->hexsz
)); /* 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_object_file(n
->buf
.buf
, n
->buf
.len
, OBJ_TREE
, &s
);
684 strbuf_release(&n
->buf
);
687 write_tree_entry(&tws
->buf
, 040000, tws
->path
, 2, s
.hash
);
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 struct object_id
*oid
)
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
**nn_list
;
733 struct non_note
*nn_prev
;
736 static int write_each_non_note_until(const char *note_path
,
737 struct write_each_note_data
*d
)
739 struct non_note
*p
= d
->nn_prev
;
740 struct non_note
*n
= p
? p
->next
: *d
->nn_list
;
742 while (n
&& (!note_path
|| (cmp
= strcmp(n
->path
, note_path
)) <= 0)) {
743 if (note_path
&& cmp
== 0)
744 ; /* do nothing, prefer note to non-note */
746 ret
= write_each_note_helper(d
->root
, n
->path
, n
->mode
,
758 static int write_each_note(const struct object_id
*object_oid UNUSED
,
759 const struct object_id
*note_oid
, char *note_path
,
762 struct write_each_note_data
*d
=
763 (struct write_each_note_data
*) cb_data
;
764 size_t note_path_len
= strlen(note_path
);
765 unsigned int mode
= 0100644;
767 if (note_path
[note_path_len
- 1] == '/') {
770 note_path
[note_path_len
] = '\0';
773 assert(note_path_len
<= GIT_MAX_HEXSZ
+ FANOUT_PATH_SEPARATORS
);
775 /* Weave non-note entries into note entries */
776 return write_each_non_note_until(note_path
, d
) ||
777 write_each_note_helper(d
->root
, note_path
, mode
, note_oid
);
780 struct note_delete_list
{
781 struct note_delete_list
*next
;
782 const unsigned char *sha1
;
785 static int prune_notes_helper(const struct object_id
*object_oid
,
786 const struct object_id
*note_oid UNUSED
,
787 char *note_path UNUSED
,
790 struct note_delete_list
**l
= (struct note_delete_list
**) cb_data
;
791 struct note_delete_list
*n
;
793 if (repo_has_object_file(the_repository
, object_oid
))
794 return 0; /* nothing to do for this note */
796 /* failed to find object => prune this note */
797 n
= (struct note_delete_list
*) xmalloc(sizeof(*n
));
799 n
->sha1
= object_oid
->hash
;
804 int combine_notes_concatenate(struct object_id
*cur_oid
,
805 const struct object_id
*new_oid
)
807 char *cur_msg
= NULL
, *new_msg
= NULL
, *buf
;
808 unsigned long cur_len
, new_len
, buf_len
;
809 enum object_type cur_type
, new_type
;
812 /* read in both note blob objects */
813 if (!is_null_oid(new_oid
))
814 new_msg
= repo_read_object_file(the_repository
, new_oid
,
815 &new_type
, &new_len
);
816 if (!new_msg
|| !new_len
|| new_type
!= OBJ_BLOB
) {
820 if (!is_null_oid(cur_oid
))
821 cur_msg
= repo_read_object_file(the_repository
, cur_oid
,
822 &cur_type
, &cur_len
);
823 if (!cur_msg
|| !cur_len
|| cur_type
!= OBJ_BLOB
) {
826 oidcpy(cur_oid
, new_oid
);
830 /* we will separate the notes by two newlines anyway */
831 if (cur_msg
[cur_len
- 1] == '\n')
834 /* concatenate cur_msg and new_msg into buf */
835 buf_len
= cur_len
+ 2 + new_len
;
836 buf
= (char *) xmalloc(buf_len
);
837 memcpy(buf
, cur_msg
, cur_len
);
839 buf
[cur_len
+ 1] = '\n';
840 memcpy(buf
+ cur_len
+ 2, new_msg
, new_len
);
844 /* create a new blob object from buf */
845 ret
= write_object_file(buf
, buf_len
, OBJ_BLOB
, cur_oid
);
850 int combine_notes_overwrite(struct object_id
*cur_oid
,
851 const struct object_id
*new_oid
)
853 oidcpy(cur_oid
, new_oid
);
857 int combine_notes_ignore(struct object_id
*cur_oid UNUSED
,
858 const struct object_id
*new_oid UNUSED
)
864 * Add the lines from the named object to list, with trailing
867 static int string_list_add_note_lines(struct string_list
*list
,
868 const struct object_id
*oid
)
874 if (is_null_oid(oid
))
877 /* read_sha1_file NUL-terminates */
878 data
= repo_read_object_file(the_repository
, oid
, &t
, &len
);
879 if (t
!= OBJ_BLOB
|| !data
|| !len
) {
881 return t
!= OBJ_BLOB
|| !data
;
885 * If the last line of the file is EOL-terminated, this will
886 * add an empty string to the list. But it will be removed
887 * later, along with any empty strings that came from empty
888 * lines within the file.
890 string_list_split(list
, data
, '\n', -1);
895 static int string_list_join_lines_helper(struct string_list_item
*item
,
898 struct strbuf
*buf
= cb_data
;
899 strbuf_addstr(buf
, item
->string
);
900 strbuf_addch(buf
, '\n');
904 int combine_notes_cat_sort_uniq(struct object_id
*cur_oid
,
905 const struct object_id
*new_oid
)
907 struct string_list sort_uniq_list
= STRING_LIST_INIT_DUP
;
908 struct strbuf buf
= STRBUF_INIT
;
911 /* read both note blob objects into unique_lines */
912 if (string_list_add_note_lines(&sort_uniq_list
, cur_oid
))
914 if (string_list_add_note_lines(&sort_uniq_list
, new_oid
))
916 string_list_remove_empty_items(&sort_uniq_list
, 0);
917 string_list_sort(&sort_uniq_list
);
918 string_list_remove_duplicates(&sort_uniq_list
, 0);
920 /* create a new blob object from sort_uniq_list */
921 if (for_each_string_list(&sort_uniq_list
,
922 string_list_join_lines_helper
, &buf
))
925 ret
= write_object_file(buf
.buf
, buf
.len
, OBJ_BLOB
, cur_oid
);
928 strbuf_release(&buf
);
929 string_list_clear(&sort_uniq_list
, 0);
933 static int string_list_add_one_ref(const char *refname
,
934 const struct object_id
*oid UNUSED
,
935 int flag UNUSED
, void *cb
)
937 struct string_list
*refs
= cb
;
938 if (!unsorted_string_list_has_string(refs
, refname
))
939 string_list_append(refs
, refname
);
944 * The list argument must have strdup_strings set on it.
946 void string_list_add_refs_by_glob(struct string_list
*list
, const char *glob
)
948 assert(list
->strdup_strings
);
949 if (has_glob_specials(glob
)) {
950 for_each_glob_ref(string_list_add_one_ref
, glob
, list
);
952 struct object_id oid
;
953 if (repo_get_oid(the_repository
, glob
, &oid
))
954 warning("notes ref %s is invalid", glob
);
955 if (!unsorted_string_list_has_string(list
, glob
))
956 string_list_append(list
, glob
);
960 void string_list_add_refs_from_colon_sep(struct string_list
*list
,
963 struct string_list split
= STRING_LIST_INIT_NODUP
;
964 char *globs_copy
= xstrdup(globs
);
967 string_list_split_in_place(&split
, globs_copy
, ":", -1);
968 string_list_remove_empty_items(&split
, 0);
970 for (i
= 0; i
< split
.nr
; i
++)
971 string_list_add_refs_by_glob(list
, split
.items
[i
].string
);
973 string_list_clear(&split
, 0);
977 static int notes_display_config(const char *k
, const char *v
,
978 const struct config_context
*ctx UNUSED
,
983 if (*load_refs
&& !strcmp(k
, "notes.displayref")) {
985 return config_error_nonbool(k
);
986 string_list_add_refs_by_glob(&display_notes_refs
, v
);
992 const char *default_notes_ref(void)
994 const char *notes_ref
= NULL
;
996 notes_ref
= getenv(GIT_NOTES_REF_ENVIRONMENT
);
998 notes_ref
= notes_ref_name
; /* value of core.notesRef config */
1000 notes_ref
= GIT_NOTES_DEFAULT_REF
;
1004 void init_notes(struct notes_tree
*t
, const char *notes_ref
,
1005 combine_notes_fn combine_notes
, int flags
)
1007 struct object_id oid
, object_oid
;
1008 unsigned short mode
;
1009 struct leaf_node root_tree
;
1012 t
= &default_notes_tree
;
1013 assert(!t
->initialized
);
1016 notes_ref
= default_notes_ref();
1017 update_ref_namespace(NAMESPACE_NOTES
, xstrdup(notes_ref
));
1020 combine_notes
= combine_notes_concatenate
;
1022 t
->root
= (struct int_node
*) xcalloc(1, sizeof(struct int_node
));
1023 t
->first_non_note
= NULL
;
1024 t
->prev_non_note
= NULL
;
1025 t
->ref
= xstrdup(notes_ref
);
1026 t
->update_ref
= (flags
& NOTES_INIT_WRITABLE
) ? t
->ref
: NULL
;
1027 t
->combine_notes
= combine_notes
;
1031 if (flags
& NOTES_INIT_EMPTY
||
1032 repo_get_oid_treeish(the_repository
, notes_ref
, &object_oid
))
1034 if (flags
& NOTES_INIT_WRITABLE
&& read_ref(notes_ref
, &object_oid
))
1035 die("Cannot use notes ref %s", notes_ref
);
1036 if (get_tree_entry(the_repository
, &object_oid
, "", &oid
, &mode
))
1037 die("Failed to read notes tree referenced by %s (%s)",
1038 notes_ref
, oid_to_hex(&object_oid
));
1040 oidclr(&root_tree
.key_oid
);
1041 oidcpy(&root_tree
.val_oid
, &oid
);
1042 load_subtree(t
, &root_tree
, t
->root
, 0);
1045 struct notes_tree
**load_notes_trees(struct string_list
*refs
, int flags
)
1047 struct string_list_item
*item
;
1049 struct notes_tree
**trees
;
1050 ALLOC_ARRAY(trees
, refs
->nr
+ 1);
1051 for_each_string_list_item(item
, refs
) {
1052 struct notes_tree
*t
= xcalloc(1, sizeof(struct notes_tree
));
1053 init_notes(t
, item
->string
, combine_notes_ignore
, flags
);
1054 trees
[counter
++] = t
;
1056 trees
[counter
] = NULL
;
1060 void init_display_notes(struct display_notes_opt
*opt
)
1062 memset(opt
, 0, sizeof(*opt
));
1063 opt
->use_default_notes
= -1;
1066 void enable_default_display_notes(struct display_notes_opt
*opt
, int *show_notes
)
1068 opt
->use_default_notes
= 1;
1072 void enable_ref_display_notes(struct display_notes_opt
*opt
, int *show_notes
,
1074 struct strbuf buf
= STRBUF_INIT
;
1075 strbuf_addstr(&buf
, ref
);
1076 expand_notes_ref(&buf
);
1077 string_list_append(&opt
->extra_notes_refs
,
1078 strbuf_detach(&buf
, NULL
));
1082 void disable_display_notes(struct display_notes_opt
*opt
, int *show_notes
)
1084 opt
->use_default_notes
= -1;
1085 /* we have been strdup'ing ourselves, so trick
1086 * string_list into free()ing strings */
1087 opt
->extra_notes_refs
.strdup_strings
= 1;
1088 string_list_clear(&opt
->extra_notes_refs
, 0);
1089 opt
->extra_notes_refs
.strdup_strings
= 0;
1093 void load_display_notes(struct display_notes_opt
*opt
)
1095 char *display_ref_env
;
1096 int load_config_refs
= 0;
1097 display_notes_refs
.strdup_strings
= 1;
1099 assert(!display_notes_trees
);
1101 if (!opt
|| opt
->use_default_notes
> 0 ||
1102 (opt
->use_default_notes
== -1 && !opt
->extra_notes_refs
.nr
)) {
1103 string_list_append(&display_notes_refs
, default_notes_ref());
1104 display_ref_env
= getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT
);
1105 if (display_ref_env
) {
1106 string_list_add_refs_from_colon_sep(&display_notes_refs
,
1108 load_config_refs
= 0;
1110 load_config_refs
= 1;
1113 git_config(notes_display_config
, &load_config_refs
);
1116 struct string_list_item
*item
;
1117 for_each_string_list_item(item
, &opt
->extra_notes_refs
)
1118 string_list_add_refs_by_glob(&display_notes_refs
,
1122 display_notes_trees
= load_notes_trees(&display_notes_refs
, 0);
1123 string_list_clear(&display_notes_refs
, 0);
1126 int add_note(struct notes_tree
*t
, const struct object_id
*object_oid
,
1127 const struct object_id
*note_oid
, combine_notes_fn combine_notes
)
1129 struct leaf_node
*l
;
1132 t
= &default_notes_tree
;
1133 assert(t
->initialized
);
1136 combine_notes
= t
->combine_notes
;
1137 l
= (struct leaf_node
*) xmalloc(sizeof(struct leaf_node
));
1138 oidcpy(&l
->key_oid
, object_oid
);
1139 oidcpy(&l
->val_oid
, note_oid
);
1140 return note_tree_insert(t
, t
->root
, 0, l
, PTR_TYPE_NOTE
, combine_notes
);
1143 int remove_note(struct notes_tree
*t
, const unsigned char *object_sha1
)
1148 t
= &default_notes_tree
;
1149 assert(t
->initialized
);
1150 oidread(&l
.key_oid
, object_sha1
);
1152 note_tree_remove(t
, t
->root
, 0, &l
);
1153 if (is_null_oid(&l
.val_oid
)) /* no note was removed */
1159 const struct object_id
*get_note(struct notes_tree
*t
,
1160 const struct object_id
*oid
)
1162 struct leaf_node
*found
;
1165 t
= &default_notes_tree
;
1166 assert(t
->initialized
);
1167 found
= note_tree_find(t
, t
->root
, 0, oid
->hash
);
1168 return found
? &found
->val_oid
: NULL
;
1171 int for_each_note(struct notes_tree
*t
, int flags
, each_note_fn fn
,
1175 t
= &default_notes_tree
;
1176 assert(t
->initialized
);
1177 return for_each_note_helper(t
, t
->root
, 0, 0, flags
, fn
, cb_data
);
1180 int write_notes_tree(struct notes_tree
*t
, struct object_id
*result
)
1182 struct tree_write_stack root
;
1183 struct write_each_note_data cb_data
;
1188 t
= &default_notes_tree
;
1189 assert(t
->initialized
);
1191 /* Prepare for traversal of current notes tree */
1192 root
.next
= NULL
; /* last forward entry in list is grounded */
1193 strbuf_init(&root
.buf
, 256 * (32 + the_hash_algo
->hexsz
)); /* assume 256 entries */
1194 root
.path
[0] = root
.path
[1] = '\0';
1195 cb_data
.root
= &root
;
1196 cb_data
.nn_list
= &(t
->first_non_note
);
1197 cb_data
.nn_prev
= NULL
;
1199 /* Write tree objects representing current notes tree */
1200 flags
= FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
|
1201 FOR_EACH_NOTE_YIELD_SUBTREES
;
1202 ret
= for_each_note(t
, flags
, write_each_note
, &cb_data
) ||
1203 write_each_non_note_until(NULL
, &cb_data
) ||
1204 tree_write_stack_finish_subtree(&root
) ||
1205 write_object_file(root
.buf
.buf
, root
.buf
.len
, OBJ_TREE
, result
);
1206 strbuf_release(&root
.buf
);
1210 void prune_notes(struct notes_tree
*t
, int flags
)
1212 struct note_delete_list
*l
= NULL
;
1215 t
= &default_notes_tree
;
1216 assert(t
->initialized
);
1218 for_each_note(t
, 0, prune_notes_helper
, &l
);
1221 if (flags
& NOTES_PRUNE_VERBOSE
)
1222 printf("%s\n", hash_to_hex(l
->sha1
));
1223 if (!(flags
& NOTES_PRUNE_DRYRUN
))
1224 remove_note(t
, l
->sha1
);
1229 void free_notes(struct notes_tree
*t
)
1232 t
= &default_notes_tree
;
1234 note_tree_free(t
->root
);
1236 while (t
->first_non_note
) {
1237 t
->prev_non_note
= t
->first_non_note
->next
;
1238 free(t
->first_non_note
->path
);
1239 free(t
->first_non_note
);
1240 t
->first_non_note
= t
->prev_non_note
;
1243 memset(t
, 0, sizeof(struct notes_tree
));
1247 * Fill the given strbuf with the notes associated with the given object.
1249 * If the given notes_tree structure is not initialized, it will be auto-
1250 * initialized to the default value (see documentation for init_notes() above).
1251 * If the given notes_tree is NULL, the internal/default notes_tree will be
1254 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1255 * for human consumption.
1257 static void format_note(struct notes_tree
*t
, const struct object_id
*object_oid
,
1258 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1260 static const char utf8
[] = "utf-8";
1261 const struct object_id
*oid
;
1263 unsigned long linelen
, msglen
;
1264 enum object_type type
;
1267 t
= &default_notes_tree
;
1268 if (!t
->initialized
)
1269 init_notes(t
, NULL
, NULL
, 0);
1271 oid
= get_note(t
, object_oid
);
1275 if (!(msg
= repo_read_object_file(the_repository
, oid
, &type
, &msglen
)) || type
!= OBJ_BLOB
) {
1280 if (output_encoding
&& *output_encoding
&&
1281 !is_encoding_utf8(output_encoding
)) {
1282 char *reencoded
= reencode_string(msg
, output_encoding
, utf8
);
1286 msglen
= strlen(msg
);
1290 /* we will end the annotation by a newline anyway */
1291 if (msglen
&& msg
[msglen
- 1] == '\n')
1295 const char *ref
= t
->ref
;
1296 if (!ref
|| !strcmp(ref
, GIT_NOTES_DEFAULT_REF
)) {
1297 strbuf_addstr(sb
, "\nNotes:\n");
1299 skip_prefix(ref
, "refs/", &ref
);
1300 skip_prefix(ref
, "notes/", &ref
);
1301 strbuf_addf(sb
, "\nNotes (%s):\n", ref
);
1305 for (msg_p
= msg
; msg_p
< msg
+ msglen
; msg_p
+= linelen
+ 1) {
1306 linelen
= strchrnul(msg_p
, '\n') - msg_p
;
1309 strbuf_addstr(sb
, " ");
1310 strbuf_add(sb
, msg_p
, linelen
);
1311 strbuf_addch(sb
, '\n');
1317 void format_display_notes(const struct object_id
*object_oid
,
1318 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1321 assert(display_notes_trees
);
1322 for (i
= 0; display_notes_trees
[i
]; i
++)
1323 format_note(display_notes_trees
[i
], object_oid
, sb
,
1324 output_encoding
, raw
);
1327 int copy_note(struct notes_tree
*t
,
1328 const struct object_id
*from_obj
, const struct object_id
*to_obj
,
1329 int force
, combine_notes_fn combine_notes
)
1331 const struct object_id
*note
= get_note(t
, from_obj
);
1332 const struct object_id
*existing_note
= get_note(t
, to_obj
);
1334 if (!force
&& existing_note
)
1338 return add_note(t
, to_obj
, note
, combine_notes
);
1339 else if (existing_note
)
1340 return add_note(t
, to_obj
, null_oid(), combine_notes
);
1345 void expand_notes_ref(struct strbuf
*sb
)
1347 if (starts_with(sb
->buf
, "refs/notes/"))
1348 return; /* we're happy */
1349 else if (starts_with(sb
->buf
, "notes/"))
1350 strbuf_insertstr(sb
, 0, "refs/");
1352 strbuf_insertstr(sb
, 0, "refs/notes/");
1355 void expand_loose_notes_ref(struct strbuf
*sb
)
1357 struct object_id object
;
1359 if (repo_get_oid(the_repository
, sb
->buf
, &object
)) {
1360 /* fallback to expand_notes_ref */
1361 expand_notes_ref(sb
);