]> git.ipfire.org Git - thirdparty/git.git/blame - notes.c
Notes API: init_notes(): Initialize the notes tree from the given notes ref
[thirdparty/git.git] / notes.c
CommitLineData
a97a7468 1#include "cache.h"
a97a7468 2#include "notes.h"
a97a7468
JS
3#include "utf8.h"
4#include "strbuf.h"
fd53c9eb
JS
5#include "tree-walk.h"
6
23123aec
JH
7/*
8 * Use a non-balancing simple 16-tree structure with struct int_node as
9 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
10 * 16-array of pointers to its children.
11 * The bottom 2 bits of each pointer is used to identify the pointer type
12 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
13 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
14 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
15 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
16 *
17 * The root node is a statically allocated struct int_node.
18 */
19struct int_node {
20 void *a[16];
fd53c9eb
JS
21};
22
23123aec
JH
23/*
24 * Leaf nodes come in two variants, note entries and subtree entries,
25 * distinguished by the LSb of the leaf node pointer (see above).
a7e7eff6 26 * As a note entry, the key is the SHA1 of the referenced object, and the
23123aec
JH
27 * value is the SHA1 of the note object.
28 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
a7e7eff6 29 * referenced object, using the last byte of the key to store the length of
23123aec
JH
30 * the prefix. The value is the SHA1 of the tree object containing the notes
31 * subtree.
32 */
33struct leaf_node {
34 unsigned char key_sha1[20];
35 unsigned char val_sha1[20];
fd53c9eb 36};
a97a7468 37
23123aec
JH
38#define PTR_TYPE_NULL 0
39#define PTR_TYPE_INTERNAL 1
40#define PTR_TYPE_NOTE 2
41#define PTR_TYPE_SUBTREE 3
fd53c9eb 42
23123aec
JH
43#define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
44#define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
45#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
fd53c9eb 46
23123aec 47#define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f)
fd53c9eb 48
23123aec
JH
49#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
50 (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
fd53c9eb 51
23123aec 52static struct int_node root_node;
fd53c9eb 53
23123aec
JH
54static int initialized;
55
56static void load_subtree(struct leaf_node *subtree, struct int_node *node,
57 unsigned int n);
58
59/*
ef8db638 60 * Search the tree until the appropriate location for the given key is found:
23123aec 61 * 1. Start at the root node, with n = 0
ef8db638
JH
62 * 2. If a[0] at the current level is a matching subtree entry, unpack that
63 * subtree entry and remove it; restart search at the current level.
64 * 3. Use the nth nibble of the key as an index into a:
65 * - If a[n] is an int_node, recurse from #2 into that node and increment n
23123aec
JH
66 * - If a matching subtree entry, unpack that subtree entry (and remove it);
67 * restart search at the current level.
ef8db638
JH
68 * - Otherwise, we have found one of the following:
69 * - a subtree entry which does not match the key
70 * - a note entry which may or may not match the key
71 * - an unused leaf node (NULL)
72 * In any case, set *tree and *n, and return pointer to the tree location.
23123aec 73 */
ef8db638
JH
74static void **note_tree_search(struct int_node **tree,
75 unsigned char *n, const unsigned char *key_sha1)
23123aec
JH
76{
77 struct leaf_node *l;
ef8db638
JH
78 unsigned char i;
79 void *p = (*tree)->a[0];
23123aec 80
ef8db638
JH
81 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
82 l = (struct leaf_node *) CLR_PTR_TYPE(p);
83 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
84 /* unpack tree and resume search */
85 (*tree)->a[0] = NULL;
86 load_subtree(l, *tree, *n);
87 free(l);
88 return note_tree_search(tree, n, key_sha1);
89 }
90 }
91
92 i = GET_NIBBLE(*n, key_sha1);
93 p = (*tree)->a[i];
0ab1faae 94 switch (GET_PTR_TYPE(p)) {
23123aec 95 case PTR_TYPE_INTERNAL:
ef8db638
JH
96 *tree = CLR_PTR_TYPE(p);
97 (*n)++;
98 return note_tree_search(tree, n, key_sha1);
23123aec
JH
99 case PTR_TYPE_SUBTREE:
100 l = (struct leaf_node *) CLR_PTR_TYPE(p);
101 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
102 /* unpack tree and resume search */
ef8db638
JH
103 (*tree)->a[i] = NULL;
104 load_subtree(l, *tree, *n);
23123aec 105 free(l);
ef8db638 106 return note_tree_search(tree, n, key_sha1);
23123aec 107 }
ef8db638 108 /* fall through */
23123aec 109 default:
ef8db638 110 return &((*tree)->a[i]);
fd53c9eb 111 }
ef8db638 112}
23123aec 113
ef8db638
JH
114/*
115 * To find a leaf_node:
116 * Search to the tree location appropriate for the given key:
117 * If a note entry with matching key, return the note entry, else return NULL.
118 */
119static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,
120 const unsigned char *key_sha1)
121{
122 void **p = note_tree_search(&tree, &n, key_sha1);
123 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
124 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
125 if (!hashcmp(key_sha1, l->key_sha1))
126 return l;
23123aec
JH
127 }
128 return NULL;
fd53c9eb
JS
129}
130
ef8db638
JH
131/* Create a new blob object by concatenating the two given blob objects */
132static int concatenate_notes(unsigned char *cur_sha1,
133 const unsigned char *new_sha1)
134{
135 char *cur_msg, *new_msg, *buf;
136 unsigned long cur_len, new_len, buf_len;
137 enum object_type cur_type, new_type;
138 int ret;
139
140 /* read in both note blob objects */
141 new_msg = read_sha1_file(new_sha1, &new_type, &new_len);
142 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
143 free(new_msg);
144 return 0;
145 }
146 cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);
147 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
148 free(cur_msg);
149 free(new_msg);
150 hashcpy(cur_sha1, new_sha1);
151 return 0;
152 }
153
154 /* we will separate the notes by a newline anyway */
155 if (cur_msg[cur_len - 1] == '\n')
156 cur_len--;
157
158 /* concatenate cur_msg and new_msg into buf */
159 buf_len = cur_len + 1 + new_len;
160 buf = (char *) xmalloc(buf_len);
161 memcpy(buf, cur_msg, cur_len);
162 buf[cur_len] = '\n';
163 memcpy(buf + cur_len + 1, new_msg, new_len);
164
165 free(cur_msg);
166 free(new_msg);
167
168 /* create a new blob object from buf */
169 ret = write_sha1_file(buf, buf_len, "blob", cur_sha1);
170 free(buf);
171 return ret;
172}
173
23123aec
JH
174/*
175 * To insert a leaf_node:
ef8db638
JH
176 * Search to the tree location appropriate for the given leaf_node's key:
177 * - If location is unused (NULL), store the tweaked pointer directly there
178 * - If location holds a note entry that matches the note-to-be-inserted, then
179 * concatenate the two notes.
180 * - If location holds a note entry that matches the subtree-to-be-inserted,
181 * then unpack the subtree-to-be-inserted into the location.
182 * - If location holds a matching subtree entry, unpack the subtree at that
183 * location, and restart the insert operation from that level.
184 * - Else, create a new int_node, holding both the node-at-location and the
185 * node-to-be-inserted, and store the new int_node into the location.
23123aec 186 */
ef8db638
JH
187static void note_tree_insert(struct int_node *tree, unsigned char n,
188 struct leaf_node *entry, unsigned char type)
fd53c9eb 189{
23123aec 190 struct int_node *new_node;
ef8db638
JH
191 struct leaf_node *l;
192 void **p = note_tree_search(&tree, &n, entry->key_sha1);
193
194 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
195 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
0ab1faae 196 switch (GET_PTR_TYPE(*p)) {
23123aec 197 case PTR_TYPE_NULL:
ef8db638
JH
198 assert(!*p);
199 *p = SET_PTR_TYPE(entry, type);
200 return;
201 case PTR_TYPE_NOTE:
202 switch (type) {
203 case PTR_TYPE_NOTE:
204 if (!hashcmp(l->key_sha1, entry->key_sha1)) {
205 /* skip concatenation if l == entry */
206 if (!hashcmp(l->val_sha1, entry->val_sha1))
207 return;
208
209 if (concatenate_notes(l->val_sha1,
210 entry->val_sha1))
211 die("failed to concatenate note %s "
a7e7eff6 212 "into note %s for object %s",
ef8db638
JH
213 sha1_to_hex(entry->val_sha1),
214 sha1_to_hex(l->val_sha1),
215 sha1_to_hex(l->key_sha1));
216 free(entry);
217 return;
218 }
219 break;
220 case PTR_TYPE_SUBTREE:
221 if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
222 entry->key_sha1)) {
223 /* unpack 'entry' */
224 load_subtree(entry, tree, n);
225 free(entry);
226 return;
227 }
228 break;
229 }
230 break;
231 case PTR_TYPE_SUBTREE:
232 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
233 /* unpack 'l' and restart insert */
234 *p = NULL;
235 load_subtree(l, tree, n);
236 free(l);
237 note_tree_insert(tree, n, entry, type);
238 return;
23123aec 239 }
ef8db638 240 break;
fd53c9eb 241 }
ef8db638
JH
242
243 /* non-matching leaf_node */
244 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
245 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
246 new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
247 note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p));
248 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
249 note_tree_insert(new_node, n + 1, entry, type);
23123aec 250}
fd53c9eb 251
23123aec
JH
252/* Free the entire notes data contained in the given tree */
253static void note_tree_free(struct int_node *tree)
254{
255 unsigned int i;
256 for (i = 0; i < 16; i++) {
257 void *p = tree->a[i];
0ab1faae 258 switch (GET_PTR_TYPE(p)) {
23123aec
JH
259 case PTR_TYPE_INTERNAL:
260 note_tree_free(CLR_PTR_TYPE(p));
261 /* fall through */
262 case PTR_TYPE_NOTE:
263 case PTR_TYPE_SUBTREE:
264 free(CLR_PTR_TYPE(p));
265 }
fd53c9eb 266 }
23123aec 267}
fd53c9eb 268
23123aec
JH
269/*
270 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
271 * - hex - Partial SHA1 segment in ASCII hex format
272 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
273 * - sha1 - Partial SHA1 value is written here
274 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
0ab1faae 275 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
23123aec
JH
276 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
277 * Pads sha1 with NULs up to sha1_len (not included in returned length).
278 */
279static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
280 unsigned char *sha1, unsigned int sha1_len)
281{
282 unsigned int i, len = hex_len >> 1;
283 if (hex_len % 2 != 0 || len > sha1_len)
284 return -1;
285 for (i = 0; i < len; i++) {
286 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
287 if (val & ~0xff)
288 return -1;
289 *sha1++ = val;
290 hex += 2;
291 }
292 for (; i < sha1_len; i++)
293 *sha1++ = 0;
294 return len;
fd53c9eb
JS
295}
296
23123aec
JH
297static void load_subtree(struct leaf_node *subtree, struct int_node *node,
298 unsigned int n)
fd53c9eb 299{
a7e7eff6 300 unsigned char object_sha1[20];
23123aec 301 unsigned int prefix_len;
23123aec 302 void *buf;
fd53c9eb
JS
303 struct tree_desc desc;
304 struct name_entry entry;
23123aec
JH
305
306 buf = fill_tree_descriptor(&desc, subtree->val_sha1);
307 if (!buf)
308 die("Could not read %s for notes-index",
309 sha1_to_hex(subtree->val_sha1));
310
311 prefix_len = subtree->key_sha1[19];
312 assert(prefix_len * 2 >= n);
a7e7eff6 313 memcpy(object_sha1, subtree->key_sha1, prefix_len);
23123aec
JH
314 while (tree_entry(&desc, &entry)) {
315 int len = get_sha1_hex_segment(entry.path, strlen(entry.path),
a7e7eff6 316 object_sha1 + prefix_len, 20 - prefix_len);
23123aec
JH
317 if (len < 0)
318 continue; /* entry.path is not a SHA1 sum. Skip */
319 len += prefix_len;
320
321 /*
a7e7eff6
JH
322 * If object SHA1 is complete (len == 20), assume note object
323 * If object SHA1 is incomplete (len < 20), assume note subtree
23123aec
JH
324 */
325 if (len <= 20) {
326 unsigned char type = PTR_TYPE_NOTE;
327 struct leaf_node *l = (struct leaf_node *)
328 xcalloc(sizeof(struct leaf_node), 1);
a7e7eff6 329 hashcpy(l->key_sha1, object_sha1);
23123aec
JH
330 hashcpy(l->val_sha1, entry.sha1);
331 if (len < 20) {
488bdf2e
JH
332 if (!S_ISDIR(entry.mode))
333 continue; /* entry cannot be subtree */
23123aec
JH
334 l->key_sha1[19] = (unsigned char) len;
335 type = PTR_TYPE_SUBTREE;
336 }
ef8db638 337 note_tree_insert(node, n, l, type);
23123aec
JH
338 }
339 }
340 free(buf);
341}
342
709f79b0 343void init_notes(const char *notes_ref, int flags)
23123aec 344{
a7e7eff6 345 unsigned char sha1[20], object_sha1[20];
23123aec
JH
346 unsigned mode;
347 struct leaf_node root_tree;
fd53c9eb 348
709f79b0
JH
349 assert(!initialized);
350 initialized = 1;
351
352 if (!notes_ref)
353 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
354 if (!notes_ref)
355 notes_ref = notes_ref_name; /* value of core.notesRef config */
356 if (!notes_ref)
357 notes_ref = GIT_NOTES_DEFAULT_REF;
358
359 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
360 read_ref(notes_ref, object_sha1))
fd53c9eb 361 return;
709f79b0
JH
362 if (get_tree_entry(object_sha1, "", sha1, &mode))
363 die("Failed to read notes tree referenced by %s (%s)",
364 notes_ref, object_sha1);
fd53c9eb 365
23123aec
JH
366 hashclr(root_tree.key_sha1);
367 hashcpy(root_tree.val_sha1, sha1);
368 load_subtree(&root_tree, &root_node, 0);
fd53c9eb
JS
369}
370
a7e7eff6 371static unsigned char *lookup_notes(const unsigned char *object_sha1)
fd53c9eb 372{
a7e7eff6 373 struct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);
23123aec
JH
374 if (found)
375 return found->val_sha1;
376 return NULL;
fd53c9eb 377}
a97a7468 378
27d57564
JH
379void free_notes(void)
380{
23123aec
JH
381 note_tree_free(&root_node);
382 memset(&root_node, 0, sizeof(struct int_node));
27d57564
JH
383 initialized = 0;
384}
385
a7e7eff6 386void format_note(const unsigned char *object_sha1, struct strbuf *sb,
c56fcc89 387 const char *output_encoding, int flags)
a97a7468
JS
388{
389 static const char utf8[] = "utf-8";
fd53c9eb 390 unsigned char *sha1;
a97a7468
JS
391 char *msg, *msg_p;
392 unsigned long linelen, msglen;
393 enum object_type type;
394
709f79b0
JH
395 if (!initialized)
396 init_notes(NULL, 0);
a97a7468 397
a7e7eff6 398 sha1 = lookup_notes(object_sha1);
fd53c9eb 399 if (!sha1)
a97a7468
JS
400 return;
401
402 if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
403 type != OBJ_BLOB) {
404 free(msg);
405 return;
406 }
407
408 if (output_encoding && *output_encoding &&
409 strcmp(utf8, output_encoding)) {
410 char *reencoded = reencode_string(msg, output_encoding, utf8);
411 if (reencoded) {
412 free(msg);
413 msg = reencoded;
414 msglen = strlen(msg);
415 }
416 }
417
418 /* we will end the annotation by a newline anyway */
419 if (msglen && msg[msglen - 1] == '\n')
420 msglen--;
421
c56fcc89
JH
422 if (flags & NOTES_SHOW_HEADER)
423 strbuf_addstr(sb, "\nNotes:\n");
a97a7468
JS
424
425 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
426 linelen = strchrnul(msg_p, '\n') - msg_p;
427
c56fcc89
JH
428 if (flags & NOTES_INDENT)
429 strbuf_addstr(sb, " ");
a97a7468
JS
430 strbuf_add(sb, msg_p, linelen);
431 strbuf_addch(sb, '\n');
432 }
433
434 free(msg);
435}