]>
Commit | Line | Data |
---|---|---|
a97a7468 JS |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "notes.h" | |
4 | #include "refs.h" | |
5 | #include "utf8.h" | |
6 | #include "strbuf.h" | |
fd53c9eb JS |
7 | #include "tree-walk.h" |
8 | ||
23123aec JH |
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]; | |
fd53c9eb JS |
23 | }; |
24 | ||
23123aec JH |
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]; | |
fd53c9eb | 38 | }; |
a97a7468 | 39 | |
23123aec JH |
40 | #define PTR_TYPE_NULL 0 |
41 | #define PTR_TYPE_INTERNAL 1 | |
42 | #define PTR_TYPE_NOTE 2 | |
43 | #define PTR_TYPE_SUBTREE 3 | |
fd53c9eb | 44 | |
23123aec JH |
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))) | |
fd53c9eb | 48 | |
23123aec | 49 | #define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f) |
fd53c9eb | 50 | |
23123aec JH |
51 | #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ |
52 | (memcmp(key_sha1, subtree_sha1, subtree_sha1[19])) | |
fd53c9eb | 53 | |
23123aec | 54 | static struct int_node root_node; |
fd53c9eb | 55 | |
23123aec JH |
56 | static int initialized; |
57 | ||
58 | static void load_subtree(struct leaf_node *subtree, struct int_node *node, | |
59 | unsigned int n); | |
60 | ||
61 | /* | |
ef8db638 | 62 | * Search the tree until the appropriate location for the given key is found: |
23123aec | 63 | * 1. Start at the root node, with n = 0 |
ef8db638 JH |
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 | |
23123aec JH |
68 | * - If a matching subtree entry, unpack that subtree entry (and remove it); |
69 | * restart search at the current level. | |
ef8db638 JH |
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. | |
23123aec | 75 | */ |
ef8db638 JH |
76 | static void **note_tree_search(struct int_node **tree, |
77 | unsigned char *n, const unsigned char *key_sha1) | |
23123aec JH |
78 | { |
79 | struct leaf_node *l; | |
ef8db638 JH |
80 | unsigned char i; |
81 | void *p = (*tree)->a[0]; | |
23123aec | 82 | |
ef8db638 JH |
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]; | |
23123aec JH |
96 | switch(GET_PTR_TYPE(p)) { |
97 | case PTR_TYPE_INTERNAL: | |
ef8db638 JH |
98 | *tree = CLR_PTR_TYPE(p); |
99 | (*n)++; | |
100 | return note_tree_search(tree, n, key_sha1); | |
23123aec JH |
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 */ | |
ef8db638 JH |
105 | (*tree)->a[i] = NULL; |
106 | load_subtree(l, *tree, *n); | |
23123aec | 107 | free(l); |
ef8db638 | 108 | return note_tree_search(tree, n, key_sha1); |
23123aec | 109 | } |
ef8db638 | 110 | /* fall through */ |
23123aec | 111 | default: |
ef8db638 | 112 | return &((*tree)->a[i]); |
fd53c9eb | 113 | } |
ef8db638 | 114 | } |
23123aec | 115 | |
ef8db638 JH |
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; | |
23123aec JH |
129 | } |
130 | return NULL; | |
fd53c9eb JS |
131 | } |
132 | ||
ef8db638 JH |
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 | ||
23123aec JH |
176 | /* |
177 | * To insert a leaf_node: | |
ef8db638 JH |
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. | |
23123aec | 188 | */ |
ef8db638 JH |
189 | static void note_tree_insert(struct int_node *tree, unsigned char n, |
190 | struct leaf_node *entry, unsigned char type) | |
fd53c9eb | 191 | { |
23123aec | 192 | struct int_node *new_node; |
ef8db638 JH |
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)) { | |
23123aec | 199 | case PTR_TYPE_NULL: |
ef8db638 JH |
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; | |
23123aec | 241 | } |
ef8db638 | 242 | break; |
fd53c9eb | 243 | } |
ef8db638 JH |
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); | |
23123aec | 252 | } |
fd53c9eb | 253 | |
23123aec JH |
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 | } | |
fd53c9eb | 268 | } |
23123aec | 269 | } |
fd53c9eb | 270 | |
23123aec JH |
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; | |
fd53c9eb JS |
297 | } |
298 | ||
23123aec JH |
299 | static void load_subtree(struct leaf_node *subtree, struct int_node *node, |
300 | unsigned int n) | |
fd53c9eb | 301 | { |
23123aec JH |
302 | unsigned char commit_sha1[20]; |
303 | unsigned int prefix_len; | |
23123aec | 304 | void *buf; |
fd53c9eb JS |
305 | struct tree_desc desc; |
306 | struct name_entry entry; | |
23123aec JH |
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) { | |
488bdf2e JH |
334 | if (!S_ISDIR(entry.mode)) |
335 | continue; /* entry cannot be subtree */ | |
23123aec JH |
336 | l->key_sha1[19] = (unsigned char) len; |
337 | type = PTR_TYPE_SUBTREE; | |
338 | } | |
ef8db638 | 339 | note_tree_insert(node, n, l, type); |
23123aec JH |
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; | |
fd53c9eb JS |
350 | |
351 | if (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) || | |
352 | get_tree_entry(commit_sha1, "", sha1, &mode)) | |
353 | return; | |
354 | ||
23123aec JH |
355 | hashclr(root_tree.key_sha1); |
356 | hashcpy(root_tree.val_sha1, sha1); | |
357 | load_subtree(&root_tree, &root_node, 0); | |
fd53c9eb JS |
358 | } |
359 | ||
360 | static unsigned char *lookup_notes(const unsigned char *commit_sha1) | |
361 | { | |
23123aec JH |
362 | struct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1); |
363 | if (found) | |
364 | return found->val_sha1; | |
365 | return NULL; | |
fd53c9eb | 366 | } |
a97a7468 | 367 | |
27d57564 JH |
368 | void free_notes(void) |
369 | { | |
23123aec JH |
370 | note_tree_free(&root_node); |
371 | memset(&root_node, 0, sizeof(struct int_node)); | |
27d57564 JH |
372 | initialized = 0; |
373 | } | |
374 | ||
a97a7468 | 375 | void get_commit_notes(const struct commit *commit, struct strbuf *sb, |
c56fcc89 | 376 | const char *output_encoding, int flags) |
a97a7468 JS |
377 | { |
378 | static const char utf8[] = "utf-8"; | |
fd53c9eb | 379 | unsigned char *sha1; |
a97a7468 JS |
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; | |
23123aec | 390 | initialize_notes(notes_ref_name); |
a97a7468 JS |
391 | initialized = 1; |
392 | } | |
393 | ||
fd53c9eb JS |
394 | sha1 = lookup_notes(commit->object.sha1); |
395 | if (!sha1) | |
a97a7468 JS |
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 | ||
c56fcc89 JH |
418 | if (flags & NOTES_SHOW_HEADER) |
419 | strbuf_addstr(sb, "\nNotes:\n"); | |
a97a7468 JS |
420 | |
421 | for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) { | |
422 | linelen = strchrnul(msg_p, '\n') - msg_p; | |
423 | ||
c56fcc89 JH |
424 | if (flags & NOTES_INDENT) |
425 | strbuf_addstr(sb, " "); | |
a97a7468 JS |
426 | strbuf_add(sb, msg_p, linelen); |
427 | strbuf_addch(sb, '\n'); | |
428 | } | |
429 | ||
430 | free(msg); | |
431 | } |