4 #include "object-store.h"
10 #include "string-list.h"
14 * Use a non-balancing simple 16-tree structure with struct int_node as
15 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
16 * 16-array of pointers to its children.
17 * The bottom 2 bits of each pointer is used to identify the pointer type
18 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
19 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
20 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
21 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
23 * The root node is a statically allocated struct int_node.
30 * Leaf nodes come in two variants, note entries and subtree entries,
31 * distinguished by the LSb of the leaf node pointer (see above).
32 * As a note entry, the key is the SHA1 of the referenced object, and the
33 * value is the SHA1 of the note object.
34 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
35 * referenced object, using the last byte of the key to store the length of
36 * the prefix. The value is the SHA1 of the tree object containing the notes
40 struct object_id key_oid
;
41 struct object_id val_oid
;
45 * A notes tree may contain entries that are not notes, and that do not follow
46 * the naming conventions of notes. There are typically none/few of these, but
47 * we still need to keep track of them. Keep a simple linked list sorted alpha-
48 * betically on the non-note path. The list is populated when parsing tree
49 * objects in load_subtree(), and the non-notes are correctly written back into
50 * the tree objects produced by write_notes_tree().
53 struct non_note
*next
; /* grounded (last->next == NULL) */
59 #define PTR_TYPE_NULL 0
60 #define PTR_TYPE_INTERNAL 1
61 #define PTR_TYPE_NOTE 2
62 #define PTR_TYPE_SUBTREE 3
64 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
65 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
66 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
68 #define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
70 #define KEY_INDEX (the_hash_algo->rawsz - 1)
71 #define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
72 #define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
73 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
74 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
76 struct notes_tree default_notes_tree
;
78 static struct string_list display_notes_refs
= STRING_LIST_INIT_NODUP
;
79 static struct notes_tree
**display_notes_trees
;
81 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
82 struct int_node
*node
, unsigned int n
);
85 * Search the tree until the appropriate location for the given key is found:
86 * 1. Start at the root node, with n = 0
87 * 2. If a[0] at the current level is a matching subtree entry, unpack that
88 * subtree entry and remove it; restart search at the current level.
89 * 3. Use the nth nibble of the key as an index into a:
90 * - If a[n] is an int_node, recurse from #2 into that node and increment n
91 * - If a matching subtree entry, unpack that subtree entry (and remove it);
92 * restart search at the current level.
93 * - Otherwise, we have found one of the following:
94 * - a subtree entry which does not match the key
95 * - a note entry which may or may not match the key
96 * - an unused leaf node (NULL)
97 * In any case, set *tree and *n, and return pointer to the tree location.
99 static void **note_tree_search(struct notes_tree
*t
, struct int_node
**tree
,
100 unsigned char *n
, const unsigned char *key_sha1
)
104 void *p
= (*tree
)->a
[0];
106 if (GET_PTR_TYPE(p
) == PTR_TYPE_SUBTREE
) {
107 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
108 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_oid
.hash
)) {
109 /* unpack tree and resume search */
110 (*tree
)->a
[0] = NULL
;
111 load_subtree(t
, l
, *tree
, *n
);
113 return note_tree_search(t
, tree
, n
, key_sha1
);
117 i
= GET_NIBBLE(*n
, key_sha1
);
119 switch (GET_PTR_TYPE(p
)) {
120 case PTR_TYPE_INTERNAL
:
121 *tree
= CLR_PTR_TYPE(p
);
123 return note_tree_search(t
, tree
, n
, key_sha1
);
124 case PTR_TYPE_SUBTREE
:
125 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
126 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_oid
.hash
)) {
127 /* unpack tree and resume search */
128 (*tree
)->a
[i
] = NULL
;
129 load_subtree(t
, l
, *tree
, *n
);
131 return note_tree_search(t
, tree
, n
, key_sha1
);
135 return &((*tree
)->a
[i
]);
140 * To find a leaf_node:
141 * Search to the tree location appropriate for the given key:
142 * If a note entry with matching key, return the note entry, else return NULL.
144 static struct leaf_node
*note_tree_find(struct notes_tree
*t
,
145 struct int_node
*tree
, unsigned char n
,
146 const unsigned char *key_sha1
)
148 void **p
= note_tree_search(t
, &tree
, &n
, key_sha1
);
149 if (GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
) {
150 struct leaf_node
*l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
151 if (hasheq(key_sha1
, l
->key_oid
.hash
))
158 * How to consolidate an int_node:
159 * If there are > 1 non-NULL entries, give up and return non-zero.
160 * Otherwise replace the int_node at the given index in the given parent node
161 * with the only NOTE entry (or a NULL entry if no entries) from the given
162 * tree, and return 0.
164 static int note_tree_consolidate(struct int_node
*tree
,
165 struct int_node
*parent
, unsigned char index
)
170 assert(tree
&& parent
);
171 assert(CLR_PTR_TYPE(parent
->a
[index
]) == tree
);
173 for (i
= 0; i
< 16; i
++) {
174 if (GET_PTR_TYPE(tree
->a
[i
]) != PTR_TYPE_NULL
) {
175 if (p
) /* more than one entry */
181 if (p
&& (GET_PTR_TYPE(p
) != PTR_TYPE_NOTE
))
183 /* replace tree with p in parent[index] */
184 parent
->a
[index
] = p
;
190 * To remove a leaf_node:
191 * Search to the tree location appropriate for the given leaf_node's key:
192 * - If location does not hold a matching entry, abort and do nothing.
193 * - Copy the matching entry's value into the given entry.
194 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
195 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
197 static void note_tree_remove(struct notes_tree
*t
,
198 struct int_node
*tree
, unsigned char n
,
199 struct leaf_node
*entry
)
202 struct int_node
*parent_stack
[GIT_MAX_RAWSZ
];
204 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_oid
.hash
);
206 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
207 if (GET_PTR_TYPE(*p
) != PTR_TYPE_NOTE
)
208 return; /* type mismatch, nothing to remove */
209 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
210 if (!oideq(&l
->key_oid
, &entry
->key_oid
))
211 return; /* key mismatch, nothing to remove */
213 /* we have found a matching entry */
214 oidcpy(&entry
->val_oid
, &l
->val_oid
);
216 *p
= SET_PTR_TYPE(NULL
, PTR_TYPE_NULL
);
218 /* consolidate this tree level, and parent levels, if possible */
220 return; /* cannot consolidate top level */
221 /* first, build stack of ancestors between root and current node */
222 parent_stack
[0] = t
->root
;
223 for (i
= 0; i
< n
; i
++) {
224 j
= GET_NIBBLE(i
, entry
->key_oid
.hash
);
225 parent_stack
[i
+ 1] = CLR_PTR_TYPE(parent_stack
[i
]->a
[j
]);
227 assert(i
== n
&& parent_stack
[i
] == tree
);
228 /* next, unwind stack until note_tree_consolidate() is done */
230 !note_tree_consolidate(parent_stack
[i
], parent_stack
[i
- 1],
231 GET_NIBBLE(i
- 1, entry
->key_oid
.hash
)))
236 * To insert a leaf_node:
237 * Search to the tree location appropriate for the given leaf_node's key:
238 * - If location is unused (NULL), store the tweaked pointer directly there
239 * - If location holds a note entry that matches the note-to-be-inserted, then
240 * combine the two notes (by calling the given combine_notes function).
241 * - If location holds a note entry that matches the subtree-to-be-inserted,
242 * then unpack the subtree-to-be-inserted into the location.
243 * - If location holds a matching subtree entry, unpack the subtree at that
244 * location, and restart the insert operation from that level.
245 * - Else, create a new int_node, holding both the node-at-location and the
246 * node-to-be-inserted, and store the new int_node into the location.
248 static int note_tree_insert(struct notes_tree
*t
, struct int_node
*tree
,
249 unsigned char n
, struct leaf_node
*entry
, unsigned char type
,
250 combine_notes_fn combine_notes
)
252 struct int_node
*new_node
;
254 void **p
= note_tree_search(t
, &tree
, &n
, entry
->key_oid
.hash
);
257 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
258 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
259 switch (GET_PTR_TYPE(*p
)) {
262 if (is_null_oid(&entry
->val_oid
))
265 *p
= SET_PTR_TYPE(entry
, type
);
270 if (oideq(&l
->key_oid
, &entry
->key_oid
)) {
271 /* skip concatenation if l == entry */
272 if (oideq(&l
->val_oid
, &entry
->val_oid
)) {
277 ret
= combine_notes(&l
->val_oid
,
279 if (!ret
&& is_null_oid(&l
->val_oid
))
280 note_tree_remove(t
, tree
, n
, entry
);
285 case PTR_TYPE_SUBTREE
:
286 if (!SUBTREE_SHA1_PREFIXCMP(l
->key_oid
.hash
,
287 entry
->key_oid
.hash
)) {
289 load_subtree(t
, entry
, tree
, n
);
296 case PTR_TYPE_SUBTREE
:
297 if (!SUBTREE_SHA1_PREFIXCMP(entry
->key_oid
.hash
, l
->key_oid
.hash
)) {
298 /* unpack 'l' and restart insert */
300 load_subtree(t
, l
, tree
, n
);
302 return note_tree_insert(t
, tree
, n
, entry
, type
,
308 /* non-matching leaf_node */
309 assert(GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
||
310 GET_PTR_TYPE(*p
) == PTR_TYPE_SUBTREE
);
311 if (is_null_oid(&entry
->val_oid
)) { /* skip insertion of empty note */
315 new_node
= (struct int_node
*) xcalloc(1, sizeof(struct int_node
));
316 ret
= note_tree_insert(t
, new_node
, n
+ 1, l
, GET_PTR_TYPE(*p
),
320 *p
= SET_PTR_TYPE(new_node
, PTR_TYPE_INTERNAL
);
321 return note_tree_insert(t
, new_node
, n
+ 1, entry
, type
, combine_notes
);
324 /* Free the entire notes data contained in the given tree */
325 static void note_tree_free(struct int_node
*tree
)
328 for (i
= 0; i
< 16; i
++) {
329 void *p
= tree
->a
[i
];
330 switch (GET_PTR_TYPE(p
)) {
331 case PTR_TYPE_INTERNAL
:
332 note_tree_free(CLR_PTR_TYPE(p
));
335 case PTR_TYPE_SUBTREE
:
336 free(CLR_PTR_TYPE(p
));
341 static int non_note_cmp(const struct non_note
*a
, const struct non_note
*b
)
343 return strcmp(a
->path
, b
->path
);
346 /* note: takes ownership of path string */
347 static void add_non_note(struct notes_tree
*t
, char *path
,
348 unsigned int mode
, const unsigned char *sha1
)
350 struct non_note
*p
= t
->prev_non_note
, *n
;
351 n
= (struct non_note
*) xmalloc(sizeof(struct non_note
));
355 oidread(&n
->oid
, sha1
);
356 t
->prev_non_note
= n
;
358 if (!t
->first_non_note
) {
359 t
->first_non_note
= n
;
363 if (non_note_cmp(p
, n
) < 0)
365 else if (non_note_cmp(t
->first_non_note
, n
) <= 0)
366 p
= t
->first_non_note
;
368 /* n sorts before t->first_non_note */
369 n
->next
= t
->first_non_note
;
370 t
->first_non_note
= n
;
374 /* n sorts equal or after p */
375 while (p
->next
&& non_note_cmp(p
->next
, n
) <= 0)
378 if (non_note_cmp(p
, n
) == 0) { /* n ~= p; overwrite p with n */
379 assert(strcmp(p
->path
, n
->path
) == 0);
381 oidcpy(&p
->oid
, &n
->oid
);
383 t
->prev_non_note
= p
;
387 /* n sorts between p and p->next */
392 static void load_subtree(struct notes_tree
*t
, struct leaf_node
*subtree
,
393 struct int_node
*node
, unsigned int n
)
395 struct object_id object_oid
;
398 struct tree_desc desc
;
399 struct name_entry entry
;
400 const unsigned hashsz
= the_hash_algo
->rawsz
;
402 buf
= fill_tree_descriptor(the_repository
, &desc
, &subtree
->val_oid
);
404 die("Could not read %s for notes-index",
405 oid_to_hex(&subtree
->val_oid
));
407 prefix_len
= subtree
->key_oid
.hash
[KEY_INDEX
];
408 if (prefix_len
>= hashsz
)
409 BUG("prefix_len (%"PRIuMAX
") is out of range", (uintmax_t)prefix_len
);
410 if (prefix_len
* 2 < n
)
411 BUG("prefix_len (%"PRIuMAX
") is too small", (uintmax_t)prefix_len
);
412 memcpy(object_oid
.hash
, subtree
->key_oid
.hash
, prefix_len
);
413 while (tree_entry(&desc
, &entry
)) {
416 size_t path_len
= strlen(entry
.path
);
418 if (path_len
== 2 * (hashsz
- prefix_len
)) {
419 /* This is potentially the remainder of the SHA-1 */
421 if (!S_ISREG(entry
.mode
))
422 /* notes must be blobs */
423 goto handle_non_note
;
425 if (hex_to_bytes(object_oid
.hash
+ prefix_len
, entry
.path
,
426 hashsz
- prefix_len
))
427 goto handle_non_note
; /* entry.path is not a SHA1 */
429 type
= PTR_TYPE_NOTE
;
430 } else if (path_len
== 2) {
431 /* This is potentially an internal node */
432 size_t len
= prefix_len
;
434 if (!S_ISDIR(entry
.mode
))
435 /* internal nodes must be trees */
436 goto handle_non_note
;
438 if (hex_to_bytes(object_oid
.hash
+ len
++, entry
.path
, 1))
439 goto handle_non_note
; /* entry.path is not a SHA1 */
442 * Pad the rest of the SHA-1 with zeros,
443 * except for the last byte, where we write
446 memset(object_oid
.hash
+ len
, 0, hashsz
- len
- 1);
447 object_oid
.hash
[KEY_INDEX
] = (unsigned char)len
;
449 type
= PTR_TYPE_SUBTREE
;
451 /* This can't be part of a note */
452 goto handle_non_note
;
456 oidcpy(&l
->key_oid
, &object_oid
);
457 oidcpy(&l
->val_oid
, &entry
.oid
);
458 oid_set_algo(&l
->key_oid
, the_hash_algo
);
459 oid_set_algo(&l
->val_oid
, the_hash_algo
);
460 if (note_tree_insert(t
, node
, n
, l
, type
,
461 combine_notes_concatenate
))
462 die("Failed to load %s %s into notes tree "
464 type
== PTR_TYPE_NOTE
? "note" : "subtree",
465 oid_to_hex(&object_oid
), t
->ref
);
471 * Determine full path for this non-note entry. The
472 * filename is already found in entry.path, but the
473 * directory part of the path must be deduced from the
474 * subtree containing this entry based on our
475 * knowledge that the overall notes tree follows a
476 * strict byte-based progressive fanout structure
477 * (i.e. using 2/38, 2/2/36, etc. fanouts).
480 struct strbuf non_note_path
= STRBUF_INIT
;
481 const char *q
= oid_to_hex(&subtree
->key_oid
);
483 for (i
= 0; i
< prefix_len
; i
++) {
484 strbuf_addch(&non_note_path
, *q
++);
485 strbuf_addch(&non_note_path
, *q
++);
486 strbuf_addch(&non_note_path
, '/');
488 strbuf_addstr(&non_note_path
, entry
.path
);
489 oid_set_algo(&entry
.oid
, the_hash_algo
);
490 add_non_note(t
, strbuf_detach(&non_note_path
, NULL
),
491 entry
.mode
, entry
.oid
.hash
);
498 * Determine optimal on-disk fanout for this part of the notes tree
500 * Given a (sub)tree and the level in the internal tree structure, determine
501 * whether or not the given existing fanout should be expanded for this
504 * Values of the 'fanout' variable:
505 * - 0: No fanout (all notes are stored directly in the root notes tree)
508 * - 3: 2/2/2/34 fanout
511 static unsigned char determine_fanout(struct int_node
*tree
, unsigned char n
,
512 unsigned char fanout
)
515 * The following is a simple heuristic that works well in practice:
516 * For each even-numbered 16-tree level (remember that each on-disk
517 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
518 * entries at that tree level. If all of them are either int_nodes or
519 * subtree entries, then there are likely plenty of notes below this
520 * level, so we return an incremented fanout.
523 if ((n
% 2) || (n
> 2 * fanout
))
525 for (i
= 0; i
< 16; i
++) {
526 switch (GET_PTR_TYPE(tree
->a
[i
])) {
527 case PTR_TYPE_SUBTREE
:
528 case PTR_TYPE_INTERNAL
:
537 /* hex oid + '/' between each pair of hex digits + NUL */
538 #define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
540 static void construct_path_with_fanout(const unsigned char *hash
,
541 unsigned char fanout
, char *path
)
543 unsigned int i
= 0, j
= 0;
544 const char *hex_hash
= hash_to_hex(hash
);
545 assert(fanout
< the_hash_algo
->rawsz
);
547 path
[i
++] = hex_hash
[j
++];
548 path
[i
++] = hex_hash
[j
++];
552 xsnprintf(path
+ i
, FANOUT_PATH_MAX
- i
, "%s", hex_hash
+ j
);
555 static int for_each_note_helper(struct notes_tree
*t
, struct int_node
*tree
,
556 unsigned char n
, unsigned char fanout
, int flags
,
557 each_note_fn fn
, void *cb_data
)
563 static char path
[FANOUT_PATH_MAX
];
565 fanout
= determine_fanout(tree
, n
, fanout
);
566 for (i
= 0; i
< 16; i
++) {
569 switch (GET_PTR_TYPE(p
)) {
570 case PTR_TYPE_INTERNAL
:
571 /* recurse into int_node */
572 ret
= for_each_note_helper(t
, CLR_PTR_TYPE(p
), n
+ 1,
573 fanout
, flags
, fn
, cb_data
);
575 case PTR_TYPE_SUBTREE
:
576 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
578 * Subtree entries in the note tree represent parts of
579 * the note tree that have not yet been explored. There
580 * is a direct relationship between subtree entries at
581 * level 'n' in the tree, and the 'fanout' variable:
582 * Subtree entries at level 'n < 2 * fanout' should be
583 * preserved, since they correspond exactly to a fanout
584 * directory in the on-disk structure. However, subtree
585 * entries at level 'n >= 2 * fanout' should NOT be
586 * preserved, but rather consolidated into the above
587 * notes tree level. We achieve this by unconditionally
588 * unpacking subtree entries that exist below the
589 * threshold level at 'n = 2 * fanout'.
591 if (n
< 2 * fanout
&&
592 flags
& FOR_EACH_NOTE_YIELD_SUBTREES
) {
593 /* invoke callback with subtree */
594 unsigned int path_len
=
595 l
->key_oid
.hash
[KEY_INDEX
] * 2 + fanout
;
596 assert(path_len
< FANOUT_PATH_MAX
- 1);
597 construct_path_with_fanout(l
->key_oid
.hash
,
600 /* Create trailing slash, if needed */
601 if (path
[path_len
- 1] != '/')
602 path
[path_len
++] = '/';
603 path
[path_len
] = '\0';
604 ret
= fn(&l
->key_oid
, &l
->val_oid
,
608 if (n
>= 2 * fanout
||
609 !(flags
& FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
)) {
610 /* unpack subtree and resume traversal */
612 load_subtree(t
, l
, tree
, n
);
618 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
619 construct_path_with_fanout(l
->key_oid
.hash
, fanout
,
621 ret
= fn(&l
->key_oid
, &l
->val_oid
, path
,
631 struct tree_write_stack
{
632 struct tree_write_stack
*next
;
634 char path
[2]; /* path to subtree in next, if any */
637 static inline int matches_tree_write_stack(struct tree_write_stack
*tws
,
638 const char *full_path
)
640 return full_path
[0] == tws
->path
[0] &&
641 full_path
[1] == tws
->path
[1] &&
645 static void write_tree_entry(struct strbuf
*buf
, unsigned int mode
,
646 const char *path
, unsigned int path_len
, const
649 strbuf_addf(buf
, "%o %.*s%c", mode
, path_len
, path
, '\0');
650 strbuf_add(buf
, hash
, the_hash_algo
->rawsz
);
653 static void tree_write_stack_init_subtree(struct tree_write_stack
*tws
,
656 struct tree_write_stack
*n
;
658 assert(tws
->path
[0] == '\0' && tws
->path
[1] == '\0');
659 n
= (struct tree_write_stack
*)
660 xmalloc(sizeof(struct tree_write_stack
));
662 strbuf_init(&n
->buf
, 256 * (32 + the_hash_algo
->hexsz
)); /* assume 256 entries per tree */
663 n
->path
[0] = n
->path
[1] = '\0';
665 tws
->path
[0] = path
[0];
666 tws
->path
[1] = path
[1];
669 static int tree_write_stack_finish_subtree(struct tree_write_stack
*tws
)
672 struct tree_write_stack
*n
= tws
->next
;
675 ret
= tree_write_stack_finish_subtree(n
);
678 ret
= write_object_file(n
->buf
.buf
, n
->buf
.len
, tree_type
, &s
);
681 strbuf_release(&n
->buf
);
684 write_tree_entry(&tws
->buf
, 040000, tws
->path
, 2, s
.hash
);
685 tws
->path
[0] = tws
->path
[1] = '\0';
690 static int write_each_note_helper(struct tree_write_stack
*tws
,
691 const char *path
, unsigned int mode
,
692 const struct object_id
*oid
)
694 size_t path_len
= strlen(path
);
698 /* Determine common part of tree write stack */
699 while (tws
&& 3 * n
< path_len
&&
700 matches_tree_write_stack(tws
, path
+ 3 * n
)) {
705 /* tws point to last matching tree_write_stack entry */
706 ret
= tree_write_stack_finish_subtree(tws
);
710 /* Start subtrees needed to satisfy path */
711 while (3 * n
+ 2 < path_len
&& path
[3 * n
+ 2] == '/') {
712 tree_write_stack_init_subtree(tws
, path
+ 3 * n
);
717 /* There should be no more directory components in the given path */
718 assert(memchr(path
+ 3 * n
, '/', path_len
- (3 * n
)) == NULL
);
720 /* Finally add given entry to the current tree object */
721 write_tree_entry(&tws
->buf
, mode
, path
+ 3 * n
, path_len
- (3 * n
),
727 struct write_each_note_data
{
728 struct tree_write_stack
*root
;
729 struct non_note
**nn_list
;
730 struct non_note
*nn_prev
;
733 static int write_each_non_note_until(const char *note_path
,
734 struct write_each_note_data
*d
)
736 struct non_note
*p
= d
->nn_prev
;
737 struct non_note
*n
= p
? p
->next
: *d
->nn_list
;
739 while (n
&& (!note_path
|| (cmp
= strcmp(n
->path
, note_path
)) <= 0)) {
740 if (note_path
&& cmp
== 0)
741 ; /* do nothing, prefer note to non-note */
743 ret
= write_each_note_helper(d
->root
, n
->path
, n
->mode
,
755 static int write_each_note(const struct object_id
*object_oid
,
756 const struct object_id
*note_oid
, 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
<= GIT_MAX_HEXSZ
+ FANOUT_PATH_SEPARATORS
);
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_oid
);
777 struct note_delete_list
{
778 struct note_delete_list
*next
;
779 const unsigned char *sha1
;
782 static int prune_notes_helper(const struct object_id
*object_oid
,
783 const struct object_id
*note_oid
, char *note_path
,
786 struct note_delete_list
**l
= (struct note_delete_list
**) cb_data
;
787 struct note_delete_list
*n
;
789 if (has_object_file(object_oid
))
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_oid
->hash
;
800 int combine_notes_concatenate(struct object_id
*cur_oid
,
801 const struct object_id
*new_oid
)
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_oid(new_oid
))
810 new_msg
= read_object_file(new_oid
, &new_type
, &new_len
);
811 if (!new_msg
|| !new_len
|| new_type
!= OBJ_BLOB
) {
815 if (!is_null_oid(cur_oid
))
816 cur_msg
= read_object_file(cur_oid
, &cur_type
, &cur_len
);
817 if (!cur_msg
|| !cur_len
|| cur_type
!= OBJ_BLOB
) {
820 oidcpy(cur_oid
, new_oid
);
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_object_file(buf
, buf_len
, blob_type
, cur_oid
);
844 int combine_notes_overwrite(struct object_id
*cur_oid
,
845 const struct object_id
*new_oid
)
847 oidcpy(cur_oid
, new_oid
);
851 int combine_notes_ignore(struct object_id
*cur_oid
,
852 const struct object_id
*new_oid
)
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 struct object_id
*oid
)
868 if (is_null_oid(oid
))
871 /* read_sha1_file NUL-terminates */
872 data
= read_object_file(oid
, &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(struct object_id
*cur_oid
,
899 const struct object_id
*new_oid
)
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_oid
))
908 if (string_list_add_note_lines(&sort_uniq_list
, new_oid
))
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_object_file(buf
.buf
, buf
.len
, blob_type
, cur_oid
);
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 struct object_id oid
;
946 if (get_oid(glob
, &oid
))
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 return 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_oid_treeish(notes_ref
, &object_oid
))
1024 if (flags
& NOTES_INIT_WRITABLE
&& read_ref(notes_ref
, &object_oid
))
1025 die("Cannot use notes ref %s", notes_ref
);
1026 if (get_tree_entry(the_repository
, &object_oid
, "", &oid
, &mode
))
1027 die("Failed to read notes tree referenced by %s (%s)",
1028 notes_ref
, oid_to_hex(&object_oid
));
1030 oidclr(&root_tree
.key_oid
);
1031 oidcpy(&root_tree
.val_oid
, &oid
);
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 memset(opt
, 0, sizeof(*opt
));
1053 opt
->use_default_notes
= -1;
1056 void enable_default_display_notes(struct display_notes_opt
*opt
, int *show_notes
)
1058 opt
->use_default_notes
= 1;
1062 void enable_ref_display_notes(struct display_notes_opt
*opt
, int *show_notes
,
1064 struct strbuf buf
= STRBUF_INIT
;
1065 strbuf_addstr(&buf
, ref
);
1066 expand_notes_ref(&buf
);
1067 string_list_append(&opt
->extra_notes_refs
,
1068 strbuf_detach(&buf
, NULL
));
1072 void disable_display_notes(struct display_notes_opt
*opt
, int *show_notes
)
1074 opt
->use_default_notes
= -1;
1075 /* we have been strdup'ing ourselves, so trick
1076 * string_list into free()ing strings */
1077 opt
->extra_notes_refs
.strdup_strings
= 1;
1078 string_list_clear(&opt
->extra_notes_refs
, 0);
1079 opt
->extra_notes_refs
.strdup_strings
= 0;
1083 void load_display_notes(struct display_notes_opt
*opt
)
1085 char *display_ref_env
;
1086 int load_config_refs
= 0;
1087 display_notes_refs
.strdup_strings
= 1;
1089 assert(!display_notes_trees
);
1091 if (!opt
|| opt
->use_default_notes
> 0 ||
1092 (opt
->use_default_notes
== -1 && !opt
->extra_notes_refs
.nr
)) {
1093 string_list_append(&display_notes_refs
, default_notes_ref());
1094 display_ref_env
= getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT
);
1095 if (display_ref_env
) {
1096 string_list_add_refs_from_colon_sep(&display_notes_refs
,
1098 load_config_refs
= 0;
1100 load_config_refs
= 1;
1103 git_config(notes_display_config
, &load_config_refs
);
1106 struct string_list_item
*item
;
1107 for_each_string_list_item(item
, &opt
->extra_notes_refs
)
1108 string_list_add_refs_by_glob(&display_notes_refs
,
1112 display_notes_trees
= load_notes_trees(&display_notes_refs
, 0);
1113 string_list_clear(&display_notes_refs
, 0);
1116 int add_note(struct notes_tree
*t
, const struct object_id
*object_oid
,
1117 const struct object_id
*note_oid
, combine_notes_fn combine_notes
)
1119 struct leaf_node
*l
;
1122 t
= &default_notes_tree
;
1123 assert(t
->initialized
);
1126 combine_notes
= t
->combine_notes
;
1127 l
= (struct leaf_node
*) xmalloc(sizeof(struct leaf_node
));
1128 oidcpy(&l
->key_oid
, object_oid
);
1129 oidcpy(&l
->val_oid
, note_oid
);
1130 return note_tree_insert(t
, t
->root
, 0, l
, PTR_TYPE_NOTE
, combine_notes
);
1133 int remove_note(struct notes_tree
*t
, const unsigned char *object_sha1
)
1138 t
= &default_notes_tree
;
1139 assert(t
->initialized
);
1140 oidread(&l
.key_oid
, object_sha1
);
1142 note_tree_remove(t
, t
->root
, 0, &l
);
1143 if (is_null_oid(&l
.val_oid
)) /* no note was removed */
1149 const struct object_id
*get_note(struct notes_tree
*t
,
1150 const struct object_id
*oid
)
1152 struct leaf_node
*found
;
1155 t
= &default_notes_tree
;
1156 assert(t
->initialized
);
1157 found
= note_tree_find(t
, t
->root
, 0, oid
->hash
);
1158 return found
? &found
->val_oid
: NULL
;
1161 int for_each_note(struct notes_tree
*t
, int flags
, each_note_fn fn
,
1165 t
= &default_notes_tree
;
1166 assert(t
->initialized
);
1167 return for_each_note_helper(t
, t
->root
, 0, 0, flags
, fn
, cb_data
);
1170 int write_notes_tree(struct notes_tree
*t
, struct object_id
*result
)
1172 struct tree_write_stack root
;
1173 struct write_each_note_data cb_data
;
1178 t
= &default_notes_tree
;
1179 assert(t
->initialized
);
1181 /* Prepare for traversal of current notes tree */
1182 root
.next
= NULL
; /* last forward entry in list is grounded */
1183 strbuf_init(&root
.buf
, 256 * (32 + the_hash_algo
->hexsz
)); /* assume 256 entries */
1184 root
.path
[0] = root
.path
[1] = '\0';
1185 cb_data
.root
= &root
;
1186 cb_data
.nn_list
= &(t
->first_non_note
);
1187 cb_data
.nn_prev
= NULL
;
1189 /* Write tree objects representing current notes tree */
1190 flags
= FOR_EACH_NOTE_DONT_UNPACK_SUBTREES
|
1191 FOR_EACH_NOTE_YIELD_SUBTREES
;
1192 ret
= for_each_note(t
, flags
, write_each_note
, &cb_data
) ||
1193 write_each_non_note_until(NULL
, &cb_data
) ||
1194 tree_write_stack_finish_subtree(&root
) ||
1195 write_object_file(root
.buf
.buf
, root
.buf
.len
, tree_type
, result
);
1196 strbuf_release(&root
.buf
);
1200 void prune_notes(struct notes_tree
*t
, int flags
)
1202 struct note_delete_list
*l
= NULL
;
1205 t
= &default_notes_tree
;
1206 assert(t
->initialized
);
1208 for_each_note(t
, 0, prune_notes_helper
, &l
);
1211 if (flags
& NOTES_PRUNE_VERBOSE
)
1212 printf("%s\n", hash_to_hex(l
->sha1
));
1213 if (!(flags
& NOTES_PRUNE_DRYRUN
))
1214 remove_note(t
, l
->sha1
);
1219 void free_notes(struct notes_tree
*t
)
1222 t
= &default_notes_tree
;
1224 note_tree_free(t
->root
);
1226 while (t
->first_non_note
) {
1227 t
->prev_non_note
= t
->first_non_note
->next
;
1228 free(t
->first_non_note
->path
);
1229 free(t
->first_non_note
);
1230 t
->first_non_note
= t
->prev_non_note
;
1233 memset(t
, 0, sizeof(struct notes_tree
));
1237 * Fill the given strbuf with the notes associated with the given object.
1239 * If the given notes_tree structure is not initialized, it will be auto-
1240 * initialized to the default value (see documentation for init_notes() above).
1241 * If the given notes_tree is NULL, the internal/default notes_tree will be
1244 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1245 * for human consumption.
1247 static void format_note(struct notes_tree
*t
, const struct object_id
*object_oid
,
1248 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1250 static const char utf8
[] = "utf-8";
1251 const struct object_id
*oid
;
1253 unsigned long linelen
, msglen
;
1254 enum object_type type
;
1257 t
= &default_notes_tree
;
1258 if (!t
->initialized
)
1259 init_notes(t
, NULL
, NULL
, 0);
1261 oid
= get_note(t
, object_oid
);
1265 if (!(msg
= read_object_file(oid
, &type
, &msglen
)) || type
!= OBJ_BLOB
) {
1270 if (output_encoding
&& *output_encoding
&&
1271 !is_encoding_utf8(output_encoding
)) {
1272 char *reencoded
= reencode_string(msg
, output_encoding
, utf8
);
1276 msglen
= strlen(msg
);
1280 /* we will end the annotation by a newline anyway */
1281 if (msglen
&& msg
[msglen
- 1] == '\n')
1285 const char *ref
= t
->ref
;
1286 if (!ref
|| !strcmp(ref
, GIT_NOTES_DEFAULT_REF
)) {
1287 strbuf_addstr(sb
, "\nNotes:\n");
1289 skip_prefix(ref
, "refs/", &ref
);
1290 skip_prefix(ref
, "notes/", &ref
);
1291 strbuf_addf(sb
, "\nNotes (%s):\n", ref
);
1295 for (msg_p
= msg
; msg_p
< msg
+ msglen
; msg_p
+= linelen
+ 1) {
1296 linelen
= strchrnul(msg_p
, '\n') - msg_p
;
1299 strbuf_addstr(sb
, " ");
1300 strbuf_add(sb
, msg_p
, linelen
);
1301 strbuf_addch(sb
, '\n');
1307 void format_display_notes(const struct object_id
*object_oid
,
1308 struct strbuf
*sb
, const char *output_encoding
, int raw
)
1311 assert(display_notes_trees
);
1312 for (i
= 0; display_notes_trees
[i
]; i
++)
1313 format_note(display_notes_trees
[i
], object_oid
, sb
,
1314 output_encoding
, raw
);
1317 int copy_note(struct notes_tree
*t
,
1318 const struct object_id
*from_obj
, const struct object_id
*to_obj
,
1319 int force
, combine_notes_fn combine_notes
)
1321 const struct object_id
*note
= get_note(t
, from_obj
);
1322 const struct object_id
*existing_note
= get_note(t
, to_obj
);
1324 if (!force
&& existing_note
)
1328 return add_note(t
, to_obj
, note
, combine_notes
);
1329 else if (existing_note
)
1330 return add_note(t
, to_obj
, null_oid(), combine_notes
);
1335 void expand_notes_ref(struct strbuf
*sb
)
1337 if (starts_with(sb
->buf
, "refs/notes/"))
1338 return; /* we're happy */
1339 else if (starts_with(sb
->buf
, "notes/"))
1340 strbuf_insertstr(sb
, 0, "refs/");
1342 strbuf_insertstr(sb
, 0, "refs/notes/");
1345 void expand_loose_notes_ref(struct strbuf
*sb
)
1347 struct object_id object
;
1349 if (get_oid(sb
->buf
, &object
)) {
1350 /* fallback to expand_notes_ref */
1351 expand_notes_ref(sb
);