10 * Use a non-balancing simple 16-tree structure with struct int_node as
11 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
12 * 16-array of pointers to its children.
13 * The bottom 2 bits of each pointer is used to identify the pointer type
14 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
15 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
16 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
17 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
19 * The root node is a statically allocated struct int_node.
26 * Leaf nodes come in two variants, note entries and subtree entries,
27 * distinguished by the LSb of the leaf node pointer (see above).
28 * As a note entry, the key is the SHA1 of the referenced commit, and the
29 * value is the SHA1 of the note object.
30 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
31 * referenced commit, using the last byte of the key to store the length of
32 * the prefix. The value is the SHA1 of the tree object containing the notes
36 unsigned char key_sha1
[20];
37 unsigned char val_sha1
[20];
40 #define PTR_TYPE_NULL 0
41 #define PTR_TYPE_INTERNAL 1
42 #define PTR_TYPE_NOTE 2
43 #define PTR_TYPE_SUBTREE 3
45 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
46 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
47 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
49 #define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f)
51 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
52 (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
54 static struct int_node root_node
;
56 static int initialized
;
58 static void load_subtree(struct leaf_node
*subtree
, struct int_node
*node
,
62 * Search the tree until the appropriate location for the given key is found:
63 * 1. Start at the root node, with n = 0
64 * 2. If a[0] at the current level is a matching subtree entry, unpack that
65 * subtree entry and remove it; restart search at the current level.
66 * 3. Use the nth nibble of the key as an index into a:
67 * - If a[n] is an int_node, recurse from #2 into that node and increment n
68 * - If a matching subtree entry, unpack that subtree entry (and remove it);
69 * restart search at the current level.
70 * - Otherwise, we have found one of the following:
71 * - a subtree entry which does not match the key
72 * - a note entry which may or may not match the key
73 * - an unused leaf node (NULL)
74 * In any case, set *tree and *n, and return pointer to the tree location.
76 static void **note_tree_search(struct int_node
**tree
,
77 unsigned char *n
, const unsigned char *key_sha1
)
81 void *p
= (*tree
)->a
[0];
83 if (GET_PTR_TYPE(p
) == PTR_TYPE_SUBTREE
) {
84 l
= (struct leaf_node
*) CLR_PTR_TYPE(p
);
85 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1
, l
->key_sha1
)) {
86 /* unpack tree and resume search */
88 load_subtree(l
, *tree
, *n
);
90 return note_tree_search(tree
, n
, key_sha1
);
94 i
= GET_NIBBLE(*n
, key_sha1
);
96 switch(GET_PTR_TYPE(p
)) {
97 case PTR_TYPE_INTERNAL
:
98 *tree
= CLR_PTR_TYPE(p
);
100 return note_tree_search(tree
, n
, key_sha1
);
101 case 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
[i
] = NULL
;
106 load_subtree(l
, *tree
, *n
);
108 return note_tree_search(tree
, n
, key_sha1
);
112 return &((*tree
)->a
[i
]);
117 * To find a leaf_node:
118 * Search to the tree location appropriate for the given key:
119 * If a note entry with matching key, return the note entry, else return NULL.
121 static struct leaf_node
*note_tree_find(struct int_node
*tree
, unsigned char n
,
122 const unsigned char *key_sha1
)
124 void **p
= note_tree_search(&tree
, &n
, key_sha1
);
125 if (GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
) {
126 struct leaf_node
*l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
127 if (!hashcmp(key_sha1
, l
->key_sha1
))
133 /* Create a new blob object by concatenating the two given blob objects */
134 static int concatenate_notes(unsigned char *cur_sha1
,
135 const unsigned char *new_sha1
)
137 char *cur_msg
, *new_msg
, *buf
;
138 unsigned long cur_len
, new_len
, buf_len
;
139 enum object_type cur_type
, new_type
;
142 /* read in both note blob objects */
143 new_msg
= read_sha1_file(new_sha1
, &new_type
, &new_len
);
144 if (!new_msg
|| !new_len
|| new_type
!= OBJ_BLOB
) {
148 cur_msg
= read_sha1_file(cur_sha1
, &cur_type
, &cur_len
);
149 if (!cur_msg
|| !cur_len
|| cur_type
!= OBJ_BLOB
) {
152 hashcpy(cur_sha1
, new_sha1
);
156 /* we will separate the notes by a newline anyway */
157 if (cur_msg
[cur_len
- 1] == '\n')
160 /* concatenate cur_msg and new_msg into buf */
161 buf_len
= cur_len
+ 1 + new_len
;
162 buf
= (char *) xmalloc(buf_len
);
163 memcpy(buf
, cur_msg
, cur_len
);
165 memcpy(buf
+ cur_len
+ 1, new_msg
, new_len
);
170 /* create a new blob object from buf */
171 ret
= write_sha1_file(buf
, buf_len
, "blob", cur_sha1
);
177 * To insert a leaf_node:
178 * Search to the tree location appropriate for the given leaf_node's key:
179 * - If location is unused (NULL), store the tweaked pointer directly there
180 * - If location holds a note entry that matches the note-to-be-inserted, then
181 * concatenate the two notes.
182 * - If location holds a note entry that matches the subtree-to-be-inserted,
183 * then unpack the subtree-to-be-inserted into the location.
184 * - If location holds a matching subtree entry, unpack the subtree at that
185 * location, and restart the insert operation from that level.
186 * - Else, create a new int_node, holding both the node-at-location and the
187 * node-to-be-inserted, and store the new int_node into the location.
189 static void note_tree_insert(struct int_node
*tree
, unsigned char n
,
190 struct leaf_node
*entry
, unsigned char type
)
192 struct int_node
*new_node
;
194 void **p
= note_tree_search(&tree
, &n
, entry
->key_sha1
);
196 assert(GET_PTR_TYPE(entry
) == 0); /* no type bits set */
197 l
= (struct leaf_node
*) CLR_PTR_TYPE(*p
);
198 switch(GET_PTR_TYPE(*p
)) {
201 *p
= SET_PTR_TYPE(entry
, type
);
206 if (!hashcmp(l
->key_sha1
, entry
->key_sha1
)) {
207 /* skip concatenation if l == entry */
208 if (!hashcmp(l
->val_sha1
, entry
->val_sha1
))
211 if (concatenate_notes(l
->val_sha1
,
213 die("failed to concatenate note %s "
214 "into note %s for commit %s",
215 sha1_to_hex(entry
->val_sha1
),
216 sha1_to_hex(l
->val_sha1
),
217 sha1_to_hex(l
->key_sha1
));
222 case PTR_TYPE_SUBTREE
:
223 if (!SUBTREE_SHA1_PREFIXCMP(l
->key_sha1
,
226 load_subtree(entry
, tree
, n
);
233 case PTR_TYPE_SUBTREE
:
234 if (!SUBTREE_SHA1_PREFIXCMP(entry
->key_sha1
, l
->key_sha1
)) {
235 /* unpack 'l' and restart insert */
237 load_subtree(l
, tree
, n
);
239 note_tree_insert(tree
, n
, entry
, type
);
245 /* non-matching leaf_node */
246 assert(GET_PTR_TYPE(*p
) == PTR_TYPE_NOTE
||
247 GET_PTR_TYPE(*p
) == PTR_TYPE_SUBTREE
);
248 new_node
= (struct int_node
*) xcalloc(sizeof(struct int_node
), 1);
249 note_tree_insert(new_node
, n
+ 1, l
, GET_PTR_TYPE(*p
));
250 *p
= SET_PTR_TYPE(new_node
, PTR_TYPE_INTERNAL
);
251 note_tree_insert(new_node
, n
+ 1, entry
, type
);
254 /* Free the entire notes data contained in the given tree */
255 static void note_tree_free(struct int_node
*tree
)
258 for (i
= 0; i
< 16; i
++) {
259 void *p
= tree
->a
[i
];
260 switch(GET_PTR_TYPE(p
)) {
261 case PTR_TYPE_INTERNAL
:
262 note_tree_free(CLR_PTR_TYPE(p
));
265 case PTR_TYPE_SUBTREE
:
266 free(CLR_PTR_TYPE(p
));
272 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
273 * - hex - Partial SHA1 segment in ASCII hex format
274 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
275 * - sha1 - Partial SHA1 value is written here
276 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
277 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format).
278 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
279 * Pads sha1 with NULs up to sha1_len (not included in returned length).
281 static int get_sha1_hex_segment(const char *hex
, unsigned int hex_len
,
282 unsigned char *sha1
, unsigned int sha1_len
)
284 unsigned int i
, len
= hex_len
>> 1;
285 if (hex_len
% 2 != 0 || len
> sha1_len
)
287 for (i
= 0; i
< len
; i
++) {
288 unsigned int val
= (hexval(hex
[0]) << 4) | hexval(hex
[1]);
294 for (; i
< sha1_len
; i
++)
299 static void load_subtree(struct leaf_node
*subtree
, struct int_node
*node
,
302 unsigned char commit_sha1
[20];
303 unsigned int prefix_len
;
305 struct tree_desc desc
;
306 struct name_entry entry
;
308 buf
= fill_tree_descriptor(&desc
, subtree
->val_sha1
);
310 die("Could not read %s for notes-index",
311 sha1_to_hex(subtree
->val_sha1
));
313 prefix_len
= subtree
->key_sha1
[19];
314 assert(prefix_len
* 2 >= n
);
315 memcpy(commit_sha1
, subtree
->key_sha1
, prefix_len
);
316 while (tree_entry(&desc
, &entry
)) {
317 int len
= get_sha1_hex_segment(entry
.path
, strlen(entry
.path
),
318 commit_sha1
+ prefix_len
, 20 - prefix_len
);
320 continue; /* entry.path is not a SHA1 sum. Skip */
324 * If commit SHA1 is complete (len == 20), assume note object
325 * If commit SHA1 is incomplete (len < 20), assume note subtree
328 unsigned char type
= PTR_TYPE_NOTE
;
329 struct leaf_node
*l
= (struct leaf_node
*)
330 xcalloc(sizeof(struct leaf_node
), 1);
331 hashcpy(l
->key_sha1
, commit_sha1
);
332 hashcpy(l
->val_sha1
, entry
.sha1
);
334 if (!S_ISDIR(entry
.mode
))
335 continue; /* entry cannot be subtree */
336 l
->key_sha1
[19] = (unsigned char) len
;
337 type
= PTR_TYPE_SUBTREE
;
339 note_tree_insert(node
, n
, l
, type
);
345 static void initialize_notes(const char *notes_ref_name
)
347 unsigned char sha1
[20], commit_sha1
[20];
349 struct leaf_node root_tree
;
351 if (!notes_ref_name
|| read_ref(notes_ref_name
, commit_sha1
) ||
352 get_tree_entry(commit_sha1
, "", sha1
, &mode
))
355 hashclr(root_tree
.key_sha1
);
356 hashcpy(root_tree
.val_sha1
, sha1
);
357 load_subtree(&root_tree
, &root_node
, 0);
360 static unsigned char *lookup_notes(const unsigned char *commit_sha1
)
362 struct leaf_node
*found
= note_tree_find(&root_node
, 0, commit_sha1
);
364 return found
->val_sha1
;
368 void free_notes(void)
370 note_tree_free(&root_node
);
371 memset(&root_node
, 0, sizeof(struct int_node
));
375 void get_commit_notes(const struct commit
*commit
, struct strbuf
*sb
,
376 const char *output_encoding
, int flags
)
378 static const char utf8
[] = "utf-8";
381 unsigned long linelen
, msglen
;
382 enum object_type type
;
385 const char *env
= getenv(GIT_NOTES_REF_ENVIRONMENT
);
387 notes_ref_name
= getenv(GIT_NOTES_REF_ENVIRONMENT
);
388 else if (!notes_ref_name
)
389 notes_ref_name
= GIT_NOTES_DEFAULT_REF
;
390 initialize_notes(notes_ref_name
);
394 sha1
= lookup_notes(commit
->object
.sha1
);
398 if (!(msg
= read_sha1_file(sha1
, &type
, &msglen
)) || !msglen
||
404 if (output_encoding
&& *output_encoding
&&
405 strcmp(utf8
, output_encoding
)) {
406 char *reencoded
= reencode_string(msg
, output_encoding
, utf8
);
410 msglen
= strlen(msg
);
414 /* we will end the annotation by a newline anyway */
415 if (msglen
&& msg
[msglen
- 1] == '\n')
418 if (flags
& NOTES_SHOW_HEADER
)
419 strbuf_addstr(sb
, "\nNotes:\n");
421 for (msg_p
= msg
; msg_p
< msg
+ msglen
; msg_p
+= linelen
+ 1) {
422 linelen
= strchrnul(msg_p
, '\n') - msg_p
;
424 if (flags
& NOTES_INDENT
)
425 strbuf_addstr(sb
, " ");
426 strbuf_add(sb
, msg_p
, linelen
);
427 strbuf_addch(sb
, '\n');