git-p4: fix bug in symlink handling
[git] / notes.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "notes.h"
4 #include "refs.h"
5 #include "utf8.h"
6 #include "strbuf.h"
7 #include "tree-walk.h"
8
9 /*
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 *
18  *
19  * The root node is a statically allocated struct int_node.
20  */
21 struct int_node {
22         void *a[16];
23 };
24
25 /*
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
33  * subtree.
34  */
35 struct leaf_node {
36         unsigned char key_sha1[20];
37         unsigned char val_sha1[20];
38 };
39
40 #define PTR_TYPE_NULL     0
41 #define PTR_TYPE_INTERNAL 1
42 #define PTR_TYPE_NOTE     2
43 #define PTR_TYPE_SUBTREE  3
44
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)))
48
49 #define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f)
50
51 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
52         (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
53
54 static struct int_node root_node;
55
56 static int initialized;
57
58 static void load_subtree(struct leaf_node *subtree, struct int_node *node,
59                 unsigned int n);
60
61 /*
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.
75  */
76 static void **note_tree_search(struct int_node **tree,
77                 unsigned char *n, const unsigned char *key_sha1)
78 {
79         struct leaf_node *l;
80         unsigned char i;
81         void *p = (*tree)->a[0];
82
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 */
87                         (*tree)->a[0] = NULL;
88                         load_subtree(l, *tree, *n);
89                         free(l);
90                         return note_tree_search(tree, n, key_sha1);
91                 }
92         }
93
94         i = GET_NIBBLE(*n, key_sha1);
95         p = (*tree)->a[i];
96         switch(GET_PTR_TYPE(p)) {
97         case PTR_TYPE_INTERNAL:
98                 *tree = CLR_PTR_TYPE(p);
99                 (*n)++;
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);
107                         free(l);
108                         return note_tree_search(tree, n, key_sha1);
109                 }
110                 /* fall through */
111         default:
112                 return &((*tree)->a[i]);
113         }
114 }
115
116 /*
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.
120  */
121 static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,
122                 const unsigned char *key_sha1)
123 {
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))
128                         return l;
129         }
130         return NULL;
131 }
132
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)
136 {
137         char *cur_msg, *new_msg, *buf;
138         unsigned long cur_len, new_len, buf_len;
139         enum object_type cur_type, new_type;
140         int ret;
141
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) {
145                 free(new_msg);
146                 return 0;
147         }
148         cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);
149         if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
150                 free(cur_msg);
151                 free(new_msg);
152                 hashcpy(cur_sha1, new_sha1);
153                 return 0;
154         }
155
156         /* we will separate the notes by a newline anyway */
157         if (cur_msg[cur_len - 1] == '\n')
158                 cur_len--;
159
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);
164         buf[cur_len] = '\n';
165         memcpy(buf + cur_len + 1, new_msg, new_len);
166
167         free(cur_msg);
168         free(new_msg);
169
170         /* create a new blob object from buf */
171         ret = write_sha1_file(buf, buf_len, "blob", cur_sha1);
172         free(buf);
173         return ret;
174 }
175
176 /*
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.
188  */
189 static void note_tree_insert(struct int_node *tree, unsigned char n,
190                 struct leaf_node *entry, unsigned char type)
191 {
192         struct int_node *new_node;
193         struct leaf_node *l;
194         void **p = note_tree_search(&tree, &n, entry->key_sha1);
195
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)) {
199         case PTR_TYPE_NULL:
200                 assert(!*p);
201                 *p = SET_PTR_TYPE(entry, type);
202                 return;
203         case PTR_TYPE_NOTE:
204                 switch (type) {
205                 case PTR_TYPE_NOTE:
206                         if (!hashcmp(l->key_sha1, entry->key_sha1)) {
207                                 /* skip concatenation if l == entry */
208                                 if (!hashcmp(l->val_sha1, entry->val_sha1))
209                                         return;
210
211                                 if (concatenate_notes(l->val_sha1,
212                                                 entry->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));
218                                 free(entry);
219                                 return;
220                         }
221                         break;
222                 case PTR_TYPE_SUBTREE:
223                         if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
224                                                     entry->key_sha1)) {
225                                 /* unpack 'entry' */
226                                 load_subtree(entry, tree, n);
227                                 free(entry);
228                                 return;
229                         }
230                         break;
231                 }
232                 break;
233         case PTR_TYPE_SUBTREE:
234                 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
235                         /* unpack 'l' and restart insert */
236                         *p = NULL;
237                         load_subtree(l, tree, n);
238                         free(l);
239                         note_tree_insert(tree, n, entry, type);
240                         return;
241                 }
242                 break;
243         }
244
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);
252 }
253
254 /* Free the entire notes data contained in the given tree */
255 static void note_tree_free(struct int_node *tree)
256 {
257         unsigned int i;
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));
263                         /* fall through */
264                 case PTR_TYPE_NOTE:
265                 case PTR_TYPE_SUBTREE:
266                         free(CLR_PTR_TYPE(p));
267                 }
268         }
269 }
270
271 /*
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).
280  */
281 static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
282                 unsigned char *sha1, unsigned int sha1_len)
283 {
284         unsigned int i, len = hex_len >> 1;
285         if (hex_len % 2 != 0 || len > sha1_len)
286                 return -1;
287         for (i = 0; i < len; i++) {
288                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
289                 if (val & ~0xff)
290                         return -1;
291                 *sha1++ = val;
292                 hex += 2;
293         }
294         for (; i < sha1_len; i++)
295                 *sha1++ = 0;
296         return len;
297 }
298
299 static void load_subtree(struct leaf_node *subtree, struct int_node *node,
300                 unsigned int n)
301 {
302         unsigned char commit_sha1[20];
303         unsigned int prefix_len;
304         void *buf;
305         struct tree_desc desc;
306         struct name_entry entry;
307
308         buf = fill_tree_descriptor(&desc, subtree->val_sha1);
309         if (!buf)
310                 die("Could not read %s for notes-index",
311                      sha1_to_hex(subtree->val_sha1));
312
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);
319                 if (len < 0)
320                         continue; /* entry.path is not a SHA1 sum. Skip */
321                 len += prefix_len;
322
323                 /*
324                  * If commit SHA1 is complete (len == 20), assume note object
325                  * If commit SHA1 is incomplete (len < 20), assume note subtree
326                  */
327                 if (len <= 20) {
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);
333                         if (len < 20) {
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;
338                         }
339                         note_tree_insert(node, n, l, type);
340                 }
341         }
342         free(buf);
343 }
344
345 static void initialize_notes(const char *notes_ref_name)
346 {
347         unsigned char sha1[20], commit_sha1[20];
348         unsigned mode;
349         struct leaf_node root_tree;
350
351         if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) ||
352             get_tree_entry(commit_sha1, "", sha1, &mode))
353                 return;
354
355         hashclr(root_tree.key_sha1);
356         hashcpy(root_tree.val_sha1, sha1);
357         load_subtree(&root_tree, &root_node, 0);
358 }
359
360 static unsigned char *lookup_notes(const unsigned char *commit_sha1)
361 {
362         struct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1);
363         if (found)
364                 return found->val_sha1;
365         return NULL;
366 }
367
368 void free_notes(void)
369 {
370         note_tree_free(&root_node);
371         memset(&root_node, 0, sizeof(struct int_node));
372         initialized = 0;
373 }
374
375 void get_commit_notes(const struct commit *commit, struct strbuf *sb,
376                 const char *output_encoding, int flags)
377 {
378         static const char utf8[] = "utf-8";
379         unsigned char *sha1;
380         char *msg, *msg_p;
381         unsigned long linelen, msglen;
382         enum object_type type;
383
384         if (!initialized) {
385                 const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);
386                 if (env)
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);
391                 initialized = 1;
392         }
393
394         sha1 = lookup_notes(commit->object.sha1);
395         if (!sha1)
396                 return;
397
398         if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
399                         type != OBJ_BLOB) {
400                 free(msg);
401                 return;
402         }
403
404         if (output_encoding && *output_encoding &&
405                         strcmp(utf8, output_encoding)) {
406                 char *reencoded = reencode_string(msg, output_encoding, utf8);
407                 if (reencoded) {
408                         free(msg);
409                         msg = reencoded;
410                         msglen = strlen(msg);
411                 }
412         }
413
414         /* we will end the annotation by a newline anyway */
415         if (msglen && msg[msglen - 1] == '\n')
416                 msglen--;
417
418         if (flags & NOTES_SHOW_HEADER)
419                 strbuf_addstr(sb, "\nNotes:\n");
420
421         for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
422                 linelen = strchrnul(msg_p, '\n') - msg_p;
423
424                 if (flags & NOTES_INDENT)
425                         strbuf_addstr(sb, "    ");
426                 strbuf_add(sb, msg_p, linelen);
427                 strbuf_addch(sb, '\n');
428         }
429
430         free(msg);
431 }