]>
Commit | Line | Data |
---|---|---|
a97a7468 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
a97a7468 | 3 | #include "notes.h" |
73f464b5 | 4 | #include "blob.h" |
61a7cca0 | 5 | #include "tree.h" |
a97a7468 JS |
6 | #include "utf8.h" |
7 | #include "strbuf.h" | |
fd53c9eb | 8 | #include "tree-walk.h" |
894a9d33 TR |
9 | #include "string-list.h" |
10 | #include "refs.h" | |
fd53c9eb | 11 | |
23123aec JH |
12 | /* |
13 | * Use a non-balancing simple 16-tree structure with struct int_node as | |
14 | * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a | |
15 | * 16-array of pointers to its children. | |
16 | * The bottom 2 bits of each pointer is used to identify the pointer type | |
17 | * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL) | |
18 | * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node * | |
19 | * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node * | |
20 | * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node * | |
21 | * | |
22 | * The root node is a statically allocated struct int_node. | |
23 | */ | |
24 | struct int_node { | |
25 | void *a[16]; | |
fd53c9eb JS |
26 | }; |
27 | ||
23123aec JH |
28 | /* |
29 | * Leaf nodes come in two variants, note entries and subtree entries, | |
30 | * distinguished by the LSb of the leaf node pointer (see above). | |
a7e7eff6 | 31 | * As a note entry, the key is the SHA1 of the referenced object, and the |
23123aec JH |
32 | * value is the SHA1 of the note object. |
33 | * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the | |
a7e7eff6 | 34 | * referenced object, using the last byte of the key to store the length of |
23123aec JH |
35 | * the prefix. The value is the SHA1 of the tree object containing the notes |
36 | * subtree. | |
37 | */ | |
38 | struct leaf_node { | |
5dcc969e | 39 | struct object_id key_oid; |
40 | struct object_id val_oid; | |
fd53c9eb | 41 | }; |
a97a7468 | 42 | |
851c2b37 JH |
43 | /* |
44 | * A notes tree may contain entries that are not notes, and that do not follow | |
45 | * the naming conventions of notes. There are typically none/few of these, but | |
46 | * we still need to keep track of them. Keep a simple linked list sorted alpha- | |
47 | * betically on the non-note path. The list is populated when parsing tree | |
48 | * objects in load_subtree(), and the non-notes are correctly written back into | |
49 | * the tree objects produced by write_notes_tree(). | |
50 | */ | |
51 | struct non_note { | |
52 | struct non_note *next; /* grounded (last->next == NULL) */ | |
53 | char *path; | |
54 | unsigned int mode; | |
5dcc969e | 55 | struct object_id oid; |
851c2b37 JH |
56 | }; |
57 | ||
23123aec JH |
58 | #define PTR_TYPE_NULL 0 |
59 | #define PTR_TYPE_INTERNAL 1 | |
60 | #define PTR_TYPE_NOTE 2 | |
61 | #define PTR_TYPE_SUBTREE 3 | |
fd53c9eb | 62 | |
23123aec JH |
63 | #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3) |
64 | #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3)) | |
65 | #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type))) | |
fd53c9eb | 66 | |
65eb8e0c | 67 | #define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f) |
fd53c9eb | 68 | |
89c149f5 | 69 | #define KEY_INDEX (GIT_SHA1_RAWSZ - 1) |
70 | #define FANOUT_PATH_SEPARATORS ((GIT_SHA1_HEXSZ / 2) - 1) | |
23123aec | 71 | #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \ |
89c149f5 | 72 | (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX])) |
fd53c9eb | 73 | |
cd305392 | 74 | struct notes_tree default_notes_tree; |
23123aec | 75 | |
2721ce21 | 76 | static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP; |
894a9d33 TR |
77 | static struct notes_tree **display_notes_trees; |
78 | ||
851c2b37 JH |
79 | static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, |
80 | struct int_node *node, unsigned int n); | |
23123aec JH |
81 | |
82 | /* | |
ef8db638 | 83 | * Search the tree until the appropriate location for the given key is found: |
23123aec | 84 | * 1. Start at the root node, with n = 0 |
ef8db638 JH |
85 | * 2. If a[0] at the current level is a matching subtree entry, unpack that |
86 | * subtree entry and remove it; restart search at the current level. | |
87 | * 3. Use the nth nibble of the key as an index into a: | |
88 | * - If a[n] is an int_node, recurse from #2 into that node and increment n | |
23123aec JH |
89 | * - If a matching subtree entry, unpack that subtree entry (and remove it); |
90 | * restart search at the current level. | |
ef8db638 JH |
91 | * - Otherwise, we have found one of the following: |
92 | * - a subtree entry which does not match the key | |
93 | * - a note entry which may or may not match the key | |
94 | * - an unused leaf node (NULL) | |
95 | * In any case, set *tree and *n, and return pointer to the tree location. | |
23123aec | 96 | */ |
851c2b37 | 97 | static void **note_tree_search(struct notes_tree *t, struct int_node **tree, |
ef8db638 | 98 | unsigned char *n, const unsigned char *key_sha1) |
23123aec JH |
99 | { |
100 | struct leaf_node *l; | |
ef8db638 JH |
101 | unsigned char i; |
102 | void *p = (*tree)->a[0]; | |
23123aec | 103 | |
ef8db638 JH |
104 | if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) { |
105 | l = (struct leaf_node *) CLR_PTR_TYPE(p); | |
5dcc969e | 106 | if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) { |
ef8db638 JH |
107 | /* unpack tree and resume search */ |
108 | (*tree)->a[0] = NULL; | |
851c2b37 | 109 | load_subtree(t, l, *tree, *n); |
ef8db638 | 110 | free(l); |
851c2b37 | 111 | return note_tree_search(t, tree, n, key_sha1); |
ef8db638 JH |
112 | } |
113 | } | |
114 | ||
115 | i = GET_NIBBLE(*n, key_sha1); | |
116 | p = (*tree)->a[i]; | |
0ab1faae | 117 | switch (GET_PTR_TYPE(p)) { |
23123aec | 118 | case PTR_TYPE_INTERNAL: |
ef8db638 JH |
119 | *tree = CLR_PTR_TYPE(p); |
120 | (*n)++; | |
851c2b37 | 121 | return note_tree_search(t, tree, n, key_sha1); |
23123aec JH |
122 | case PTR_TYPE_SUBTREE: |
123 | l = (struct leaf_node *) CLR_PTR_TYPE(p); | |
5dcc969e | 124 | if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) { |
23123aec | 125 | /* unpack tree and resume search */ |
ef8db638 | 126 | (*tree)->a[i] = NULL; |
851c2b37 | 127 | load_subtree(t, l, *tree, *n); |
23123aec | 128 | free(l); |
851c2b37 | 129 | return note_tree_search(t, tree, n, key_sha1); |
23123aec | 130 | } |
ef8db638 | 131 | /* fall through */ |
23123aec | 132 | default: |
ef8db638 | 133 | return &((*tree)->a[i]); |
fd53c9eb | 134 | } |
ef8db638 | 135 | } |
23123aec | 136 | |
ef8db638 JH |
137 | /* |
138 | * To find a leaf_node: | |
139 | * Search to the tree location appropriate for the given key: | |
140 | * If a note entry with matching key, return the note entry, else return NULL. | |
141 | */ | |
851c2b37 JH |
142 | static struct leaf_node *note_tree_find(struct notes_tree *t, |
143 | struct int_node *tree, unsigned char n, | |
ef8db638 JH |
144 | const unsigned char *key_sha1) |
145 | { | |
851c2b37 | 146 | void **p = note_tree_search(t, &tree, &n, key_sha1); |
ef8db638 JH |
147 | if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) { |
148 | struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p); | |
5dcc969e | 149 | if (!hashcmp(key_sha1, l->key_oid.hash)) |
ef8db638 | 150 | return l; |
23123aec JH |
151 | } |
152 | return NULL; | |
fd53c9eb JS |
153 | } |
154 | ||
1ec666b0 JH |
155 | /* |
156 | * How to consolidate an int_node: | |
157 | * If there are > 1 non-NULL entries, give up and return non-zero. | |
158 | * Otherwise replace the int_node at the given index in the given parent node | |
5a8e7c34 MH |
159 | * with the only NOTE entry (or a NULL entry if no entries) from the given |
160 | * tree, and return 0. | |
1ec666b0 JH |
161 | */ |
162 | static int note_tree_consolidate(struct int_node *tree, | |
163 | struct int_node *parent, unsigned char index) | |
164 | { | |
165 | unsigned int i; | |
166 | void *p = NULL; | |
167 | ||
168 | assert(tree && parent); | |
169 | assert(CLR_PTR_TYPE(parent->a[index]) == tree); | |
170 | ||
171 | for (i = 0; i < 16; i++) { | |
172 | if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) { | |
173 | if (p) /* more than one entry */ | |
174 | return -2; | |
175 | p = tree->a[i]; | |
176 | } | |
177 | } | |
178 | ||
5a8e7c34 MH |
179 | if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE)) |
180 | return -2; | |
1ec666b0 JH |
181 | /* replace tree with p in parent[index] */ |
182 | parent->a[index] = p; | |
183 | free(tree); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | /* | |
188 | * To remove a leaf_node: | |
189 | * Search to the tree location appropriate for the given leaf_node's key: | |
190 | * - If location does not hold a matching entry, abort and do nothing. | |
1ee1e43d | 191 | * - Copy the matching entry's value into the given entry. |
1ec666b0 JH |
192 | * - Replace the matching leaf_node with a NULL entry (and free the leaf_node). |
193 | * - Consolidate int_nodes repeatedly, while walking up the tree towards root. | |
194 | */ | |
1ee1e43d JH |
195 | static void note_tree_remove(struct notes_tree *t, |
196 | struct int_node *tree, unsigned char n, | |
197 | struct leaf_node *entry) | |
1ec666b0 JH |
198 | { |
199 | struct leaf_node *l; | |
89c149f5 | 200 | struct int_node *parent_stack[GIT_SHA1_RAWSZ]; |
1ec666b0 | 201 | unsigned char i, j; |
5dcc969e | 202 | void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash); |
1ec666b0 JH |
203 | |
204 | assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ | |
205 | if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE) | |
206 | return; /* type mismatch, nothing to remove */ | |
207 | l = (struct leaf_node *) CLR_PTR_TYPE(*p); | |
5dcc969e | 208 | if (oidcmp(&l->key_oid, &entry->key_oid)) |
1ec666b0 JH |
209 | return; /* key mismatch, nothing to remove */ |
210 | ||
211 | /* we have found a matching entry */ | |
5dcc969e | 212 | oidcpy(&entry->val_oid, &l->val_oid); |
1ec666b0 JH |
213 | free(l); |
214 | *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL); | |
215 | ||
216 | /* consolidate this tree level, and parent levels, if possible */ | |
217 | if (!n) | |
218 | return; /* cannot consolidate top level */ | |
219 | /* first, build stack of ancestors between root and current node */ | |
cd305392 | 220 | parent_stack[0] = t->root; |
1ec666b0 | 221 | for (i = 0; i < n; i++) { |
5dcc969e | 222 | j = GET_NIBBLE(i, entry->key_oid.hash); |
1ec666b0 JH |
223 | parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]); |
224 | } | |
225 | assert(i == n && parent_stack[i] == tree); | |
226 | /* next, unwind stack until note_tree_consolidate() is done */ | |
227 | while (i > 0 && | |
228 | !note_tree_consolidate(parent_stack[i], parent_stack[i - 1], | |
5dcc969e | 229 | GET_NIBBLE(i - 1, entry->key_oid.hash))) |
1ec666b0 JH |
230 | i--; |
231 | } | |
232 | ||
23123aec JH |
233 | /* |
234 | * To insert a leaf_node: | |
ef8db638 JH |
235 | * Search to the tree location appropriate for the given leaf_node's key: |
236 | * - If location is unused (NULL), store the tweaked pointer directly there | |
237 | * - If location holds a note entry that matches the note-to-be-inserted, then | |
73f464b5 | 238 | * combine the two notes (by calling the given combine_notes function). |
ef8db638 JH |
239 | * - If location holds a note entry that matches the subtree-to-be-inserted, |
240 | * then unpack the subtree-to-be-inserted into the location. | |
241 | * - If location holds a matching subtree entry, unpack the subtree at that | |
242 | * location, and restart the insert operation from that level. | |
243 | * - Else, create a new int_node, holding both the node-at-location and the | |
244 | * node-to-be-inserted, and store the new int_node into the location. | |
23123aec | 245 | */ |
180619a5 | 246 | static int note_tree_insert(struct notes_tree *t, struct int_node *tree, |
851c2b37 | 247 | unsigned char n, struct leaf_node *entry, unsigned char type, |
73f464b5 | 248 | combine_notes_fn combine_notes) |
fd53c9eb | 249 | { |
23123aec | 250 | struct int_node *new_node; |
ef8db638 | 251 | struct leaf_node *l; |
5dcc969e | 252 | void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash); |
180619a5 | 253 | int ret = 0; |
ef8db638 JH |
254 | |
255 | assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */ | |
256 | l = (struct leaf_node *) CLR_PTR_TYPE(*p); | |
0ab1faae | 257 | switch (GET_PTR_TYPE(*p)) { |
23123aec | 258 | case PTR_TYPE_NULL: |
ef8db638 | 259 | assert(!*p); |
5dcc969e | 260 | if (is_null_oid(&entry->val_oid)) |
e2656c82 JH |
261 | free(entry); |
262 | else | |
263 | *p = SET_PTR_TYPE(entry, type); | |
180619a5 | 264 | return 0; |
ef8db638 JH |
265 | case PTR_TYPE_NOTE: |
266 | switch (type) { | |
267 | case PTR_TYPE_NOTE: | |
5dcc969e | 268 | if (!oidcmp(&l->key_oid, &entry->key_oid)) { |
ef8db638 | 269 | /* skip concatenation if l == entry */ |
5dcc969e | 270 | if (!oidcmp(&l->val_oid, &entry->val_oid)) |
180619a5 | 271 | return 0; |
ef8db638 | 272 | |
5dcc969e | 273 | ret = combine_notes(l->val_oid.hash, |
274 | entry->val_oid.hash); | |
275 | if (!ret && is_null_oid(&l->val_oid)) | |
e2656c82 | 276 | note_tree_remove(t, tree, n, entry); |
ef8db638 | 277 | free(entry); |
180619a5 | 278 | return ret; |
ef8db638 JH |
279 | } |
280 | break; | |
281 | case PTR_TYPE_SUBTREE: | |
5dcc969e | 282 | if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash, |
283 | entry->key_oid.hash)) { | |
ef8db638 | 284 | /* unpack 'entry' */ |
851c2b37 | 285 | load_subtree(t, entry, tree, n); |
ef8db638 | 286 | free(entry); |
180619a5 | 287 | return 0; |
ef8db638 JH |
288 | } |
289 | break; | |
290 | } | |
291 | break; | |
292 | case PTR_TYPE_SUBTREE: | |
5dcc969e | 293 | if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) { |
ef8db638 JH |
294 | /* unpack 'l' and restart insert */ |
295 | *p = NULL; | |
851c2b37 | 296 | load_subtree(t, l, tree, n); |
ef8db638 | 297 | free(l); |
180619a5 JH |
298 | return note_tree_insert(t, tree, n, entry, type, |
299 | combine_notes); | |
23123aec | 300 | } |
ef8db638 | 301 | break; |
fd53c9eb | 302 | } |
ef8db638 JH |
303 | |
304 | /* non-matching leaf_node */ | |
305 | assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE || | |
306 | GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE); | |
5dcc969e | 307 | if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */ |
e2656c82 | 308 | free(entry); |
180619a5 | 309 | return 0; |
e2656c82 | 310 | } |
65bbf082 | 311 | new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node)); |
180619a5 JH |
312 | ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p), |
313 | combine_notes); | |
314 | if (ret) | |
315 | return ret; | |
ef8db638 | 316 | *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL); |
180619a5 | 317 | return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes); |
23123aec | 318 | } |
fd53c9eb | 319 | |
23123aec JH |
320 | /* Free the entire notes data contained in the given tree */ |
321 | static void note_tree_free(struct int_node *tree) | |
322 | { | |
323 | unsigned int i; | |
324 | for (i = 0; i < 16; i++) { | |
325 | void *p = tree->a[i]; | |
0ab1faae | 326 | switch (GET_PTR_TYPE(p)) { |
23123aec JH |
327 | case PTR_TYPE_INTERNAL: |
328 | note_tree_free(CLR_PTR_TYPE(p)); | |
329 | /* fall through */ | |
330 | case PTR_TYPE_NOTE: | |
331 | case PTR_TYPE_SUBTREE: | |
332 | free(CLR_PTR_TYPE(p)); | |
333 | } | |
fd53c9eb | 334 | } |
23123aec | 335 | } |
fd53c9eb | 336 | |
23123aec | 337 | /* |
cfdc88f1 MH |
338 | * Read `len` pairs of hexadecimal digits from `hex` and write the |
339 | * values to `binary` as `len` bytes. Return 0 on success, or -1 if | |
340 | * the input does not consist of hex digits). | |
23123aec | 341 | */ |
cfdc88f1 | 342 | static int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) |
23123aec | 343 | { |
cfdc88f1 | 344 | for (; len; len--, hex += 2) { |
23123aec | 345 | unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); |
cfdc88f1 | 346 | |
23123aec JH |
347 | if (val & ~0xff) |
348 | return -1; | |
cfdc88f1 | 349 | *binary++ = val; |
23123aec | 350 | } |
67c9b422 | 351 | return 0; |
fd53c9eb JS |
352 | } |
353 | ||
851c2b37 JH |
354 | static int non_note_cmp(const struct non_note *a, const struct non_note *b) |
355 | { | |
356 | return strcmp(a->path, b->path); | |
357 | } | |
358 | ||
c29edfef JK |
359 | /* note: takes ownership of path string */ |
360 | static void add_non_note(struct notes_tree *t, char *path, | |
851c2b37 JH |
361 | unsigned int mode, const unsigned char *sha1) |
362 | { | |
363 | struct non_note *p = t->prev_non_note, *n; | |
364 | n = (struct non_note *) xmalloc(sizeof(struct non_note)); | |
365 | n->next = NULL; | |
c29edfef | 366 | n->path = path; |
851c2b37 | 367 | n->mode = mode; |
5dcc969e | 368 | hashcpy(n->oid.hash, sha1); |
851c2b37 JH |
369 | t->prev_non_note = n; |
370 | ||
371 | if (!t->first_non_note) { | |
372 | t->first_non_note = n; | |
373 | return; | |
374 | } | |
375 | ||
376 | if (non_note_cmp(p, n) < 0) | |
377 | ; /* do nothing */ | |
378 | else if (non_note_cmp(t->first_non_note, n) <= 0) | |
379 | p = t->first_non_note; | |
380 | else { | |
381 | /* n sorts before t->first_non_note */ | |
382 | n->next = t->first_non_note; | |
383 | t->first_non_note = n; | |
384 | return; | |
385 | } | |
386 | ||
387 | /* n sorts equal or after p */ | |
388 | while (p->next && non_note_cmp(p->next, n) <= 0) | |
389 | p = p->next; | |
390 | ||
391 | if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */ | |
392 | assert(strcmp(p->path, n->path) == 0); | |
393 | p->mode = n->mode; | |
5dcc969e | 394 | oidcpy(&p->oid, &n->oid); |
851c2b37 JH |
395 | free(n); |
396 | t->prev_non_note = p; | |
397 | return; | |
398 | } | |
399 | ||
400 | /* n sorts between p and p->next */ | |
401 | n->next = p->next; | |
402 | p->next = n; | |
403 | } | |
404 | ||
405 | static void load_subtree(struct notes_tree *t, struct leaf_node *subtree, | |
406 | struct int_node *node, unsigned int n) | |
fd53c9eb | 407 | { |
89c149f5 | 408 | struct object_id object_oid; |
06cfa756 | 409 | size_t prefix_len; |
23123aec | 410 | void *buf; |
fd53c9eb JS |
411 | struct tree_desc desc; |
412 | struct name_entry entry; | |
23123aec | 413 | |
5c377d3d | 414 | buf = fill_tree_descriptor(&desc, &subtree->val_oid); |
23123aec JH |
415 | if (!buf) |
416 | die("Could not read %s for notes-index", | |
5dcc969e | 417 | oid_to_hex(&subtree->val_oid)); |
23123aec | 418 | |
89c149f5 | 419 | prefix_len = subtree->key_oid.hash[KEY_INDEX]; |
39642815 MH |
420 | if (prefix_len >= GIT_SHA1_RAWSZ) |
421 | BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len); | |
422 | if (prefix_len * 2 < n) | |
423 | BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len); | |
89c149f5 | 424 | memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len); |
23123aec | 425 | while (tree_entry(&desc, &entry)) { |
a2816392 MH |
426 | unsigned char type; |
427 | struct leaf_node *l; | |
06cfa756 | 428 | size_t path_len = strlen(entry.path); |
98c9897d MH |
429 | |
430 | if (path_len == 2 * (GIT_SHA1_RAWSZ - prefix_len)) { | |
431 | /* This is potentially the remainder of the SHA-1 */ | |
40432187 MH |
432 | |
433 | if (!S_ISREG(entry.mode)) | |
434 | /* notes must be blobs */ | |
435 | goto handle_non_note; | |
436 | ||
cfdc88f1 MH |
437 | if (hex_to_bytes(object_oid.hash + prefix_len, entry.path, |
438 | GIT_SHA1_RAWSZ - prefix_len)) | |
98c9897d | 439 | goto handle_non_note; /* entry.path is not a SHA1 */ |
23123aec | 440 | |
851c2b37 | 441 | type = PTR_TYPE_NOTE; |
98c9897d MH |
442 | } else if (path_len == 2) { |
443 | /* This is potentially an internal node */ | |
d49852d6 | 444 | size_t len = prefix_len; |
4d589b87 MH |
445 | |
446 | if (!S_ISDIR(entry.mode)) | |
447 | /* internal nodes must be trees */ | |
448 | goto handle_non_note; | |
449 | ||
cfdc88f1 | 450 | if (hex_to_bytes(object_oid.hash + len++, entry.path, 1)) |
98c9897d | 451 | goto handle_non_note; /* entry.path is not a SHA1 */ |
a2816392 | 452 | |
d49852d6 MH |
453 | /* |
454 | * Pad the rest of the SHA-1 with zeros, | |
455 | * except for the last byte, where we write | |
456 | * the length: | |
457 | */ | |
458 | memset(object_oid.hash + len, 0, GIT_SHA1_RAWSZ - len - 1); | |
459 | object_oid.hash[KEY_INDEX] = (unsigned char)len; | |
4ebef533 | 460 | |
d3b0c6be | 461 | type = PTR_TYPE_SUBTREE; |
98c9897d MH |
462 | } else { |
463 | /* This can't be part of a note */ | |
464 | goto handle_non_note; | |
23123aec | 465 | } |
98c9897d | 466 | |
4ebef533 MH |
467 | l = xcalloc(1, sizeof(*l)); |
468 | oidcpy(&l->key_oid, &object_oid); | |
469 | oidcpy(&l->val_oid, entry.oid); | |
d3b0c6be MH |
470 | if (note_tree_insert(t, node, n, l, type, |
471 | combine_notes_concatenate)) | |
472 | die("Failed to load %s %s into notes tree " | |
473 | "from %s", | |
474 | type == PTR_TYPE_NOTE ? "note" : "subtree", | |
475 | oid_to_hex(&l->key_oid), t->ref); | |
476 | ||
851c2b37 JH |
477 | continue; |
478 | ||
479 | handle_non_note: | |
480 | /* | |
cbeed9aa MH |
481 | * Determine full path for this non-note entry. The |
482 | * filename is already found in entry.path, but the | |
483 | * directory part of the path must be deduced from the | |
484 | * subtree containing this entry based on our | |
485 | * knowledge that the overall notes tree follows a | |
486 | * strict byte-based progressive fanout structure | |
487 | * (i.e. using 2/38, 2/2/36, etc. fanouts). | |
851c2b37 JH |
488 | */ |
489 | { | |
c29edfef | 490 | struct strbuf non_note_path = STRBUF_INIT; |
5dcc969e | 491 | const char *q = oid_to_hex(&subtree->key_oid); |
06cfa756 | 492 | size_t i; |
851c2b37 | 493 | for (i = 0; i < prefix_len; i++) { |
c29edfef JK |
494 | strbuf_addch(&non_note_path, *q++); |
495 | strbuf_addch(&non_note_path, *q++); | |
496 | strbuf_addch(&non_note_path, '/'); | |
851c2b37 | 497 | } |
c29edfef JK |
498 | strbuf_addstr(&non_note_path, entry.path); |
499 | add_non_note(t, strbuf_detach(&non_note_path, NULL), | |
7d924c91 | 500 | entry.mode, entry.oid->hash); |
851c2b37 | 501 | } |
23123aec JH |
502 | } |
503 | free(buf); | |
504 | } | |
505 | ||
73f77b90 JH |
506 | /* |
507 | * Determine optimal on-disk fanout for this part of the notes tree | |
508 | * | |
509 | * Given a (sub)tree and the level in the internal tree structure, determine | |
510 | * whether or not the given existing fanout should be expanded for this | |
511 | * (sub)tree. | |
512 | * | |
513 | * Values of the 'fanout' variable: | |
514 | * - 0: No fanout (all notes are stored directly in the root notes tree) | |
515 | * - 1: 2/38 fanout | |
516 | * - 2: 2/2/36 fanout | |
517 | * - 3: 2/2/2/34 fanout | |
518 | * etc. | |
519 | */ | |
520 | static unsigned char determine_fanout(struct int_node *tree, unsigned char n, | |
521 | unsigned char fanout) | |
522 | { | |
523 | /* | |
524 | * The following is a simple heuristic that works well in practice: | |
525 | * For each even-numbered 16-tree level (remember that each on-disk | |
526 | * fanout level corresponds to _two_ 16-tree levels), peek at all 16 | |
527 | * entries at that tree level. If all of them are either int_nodes or | |
528 | * subtree entries, then there are likely plenty of notes below this | |
529 | * level, so we return an incremented fanout. | |
530 | */ | |
531 | unsigned int i; | |
532 | if ((n % 2) || (n > 2 * fanout)) | |
533 | return fanout; | |
534 | for (i = 0; i < 16; i++) { | |
535 | switch (GET_PTR_TYPE(tree->a[i])) { | |
536 | case PTR_TYPE_SUBTREE: | |
537 | case PTR_TYPE_INTERNAL: | |
538 | continue; | |
539 | default: | |
540 | return fanout; | |
541 | } | |
542 | } | |
543 | return fanout + 1; | |
544 | } | |
545 | ||
02e32b7d | 546 | /* hex SHA1 + 19 * '/' + NUL */ |
89c149f5 | 547 | #define FANOUT_PATH_MAX GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS + 1 |
02e32b7d | 548 | |
73f77b90 JH |
549 | static void construct_path_with_fanout(const unsigned char *sha1, |
550 | unsigned char fanout, char *path) | |
551 | { | |
552 | unsigned int i = 0, j = 0; | |
553 | const char *hex_sha1 = sha1_to_hex(sha1); | |
89c149f5 | 554 | assert(fanout < GIT_SHA1_RAWSZ); |
73f77b90 JH |
555 | while (fanout) { |
556 | path[i++] = hex_sha1[j++]; | |
557 | path[i++] = hex_sha1[j++]; | |
558 | path[i++] = '/'; | |
559 | fanout--; | |
560 | } | |
02e32b7d | 561 | xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_sha1 + j); |
73f77b90 JH |
562 | } |
563 | ||
851c2b37 JH |
564 | static int for_each_note_helper(struct notes_tree *t, struct int_node *tree, |
565 | unsigned char n, unsigned char fanout, int flags, | |
566 | each_note_fn fn, void *cb_data) | |
73f77b90 JH |
567 | { |
568 | unsigned int i; | |
569 | void *p; | |
570 | int ret = 0; | |
571 | struct leaf_node *l; | |
02e32b7d | 572 | static char path[FANOUT_PATH_MAX]; |
73f77b90 JH |
573 | |
574 | fanout = determine_fanout(tree, n, fanout); | |
575 | for (i = 0; i < 16; i++) { | |
576 | redo: | |
577 | p = tree->a[i]; | |
578 | switch (GET_PTR_TYPE(p)) { | |
579 | case PTR_TYPE_INTERNAL: | |
580 | /* recurse into int_node */ | |
851c2b37 | 581 | ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1, |
73f77b90 JH |
582 | fanout, flags, fn, cb_data); |
583 | break; | |
584 | case PTR_TYPE_SUBTREE: | |
585 | l = (struct leaf_node *) CLR_PTR_TYPE(p); | |
586 | /* | |
587 | * Subtree entries in the note tree represent parts of | |
588 | * the note tree that have not yet been explored. There | |
589 | * is a direct relationship between subtree entries at | |
590 | * level 'n' in the tree, and the 'fanout' variable: | |
591 | * Subtree entries at level 'n <= 2 * fanout' should be | |
592 | * preserved, since they correspond exactly to a fanout | |
593 | * directory in the on-disk structure. However, subtree | |
594 | * entries at level 'n > 2 * fanout' should NOT be | |
595 | * preserved, but rather consolidated into the above | |
596 | * notes tree level. We achieve this by unconditionally | |
597 | * unpacking subtree entries that exist below the | |
598 | * threshold level at 'n = 2 * fanout'. | |
599 | */ | |
600 | if (n <= 2 * fanout && | |
601 | flags & FOR_EACH_NOTE_YIELD_SUBTREES) { | |
602 | /* invoke callback with subtree */ | |
603 | unsigned int path_len = | |
89c149f5 | 604 | l->key_oid.hash[KEY_INDEX] * 2 + fanout; |
02e32b7d | 605 | assert(path_len < FANOUT_PATH_MAX - 1); |
5dcc969e | 606 | construct_path_with_fanout(l->key_oid.hash, |
607 | fanout, | |
73f77b90 JH |
608 | path); |
609 | /* Create trailing slash, if needed */ | |
610 | if (path[path_len - 1] != '/') | |
611 | path[path_len++] = '/'; | |
612 | path[path_len] = '\0'; | |
490bc83a | 613 | ret = fn(&l->key_oid, &l->val_oid, |
5dcc969e | 614 | path, |
73f77b90 JH |
615 | cb_data); |
616 | } | |
617 | if (n > fanout * 2 || | |
618 | !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) { | |
619 | /* unpack subtree and resume traversal */ | |
620 | tree->a[i] = NULL; | |
851c2b37 | 621 | load_subtree(t, l, tree, n); |
73f77b90 JH |
622 | free(l); |
623 | goto redo; | |
624 | } | |
625 | break; | |
626 | case PTR_TYPE_NOTE: | |
627 | l = (struct leaf_node *) CLR_PTR_TYPE(p); | |
5dcc969e | 628 | construct_path_with_fanout(l->key_oid.hash, fanout, |
629 | path); | |
490bc83a | 630 | ret = fn(&l->key_oid, &l->val_oid, path, |
5dcc969e | 631 | cb_data); |
73f77b90 JH |
632 | break; |
633 | } | |
634 | if (ret) | |
635 | return ret; | |
636 | } | |
637 | return 0; | |
638 | } | |
639 | ||
61a7cca0 JH |
640 | struct tree_write_stack { |
641 | struct tree_write_stack *next; | |
642 | struct strbuf buf; | |
643 | char path[2]; /* path to subtree in next, if any */ | |
644 | }; | |
645 | ||
646 | static inline int matches_tree_write_stack(struct tree_write_stack *tws, | |
647 | const char *full_path) | |
648 | { | |
649 | return full_path[0] == tws->path[0] && | |
650 | full_path[1] == tws->path[1] && | |
651 | full_path[2] == '/'; | |
652 | } | |
653 | ||
654 | static void write_tree_entry(struct strbuf *buf, unsigned int mode, | |
655 | const char *path, unsigned int path_len, const | |
656 | unsigned char *sha1) | |
657 | { | |
c88f0cc7 | 658 | strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0'); |
89c149f5 | 659 | strbuf_add(buf, sha1, GIT_SHA1_RAWSZ); |
61a7cca0 JH |
660 | } |
661 | ||
662 | static void tree_write_stack_init_subtree(struct tree_write_stack *tws, | |
663 | const char *path) | |
664 | { | |
665 | struct tree_write_stack *n; | |
666 | assert(!tws->next); | |
667 | assert(tws->path[0] == '\0' && tws->path[1] == '\0'); | |
668 | n = (struct tree_write_stack *) | |
669 | xmalloc(sizeof(struct tree_write_stack)); | |
670 | n->next = NULL; | |
89c149f5 | 671 | strbuf_init(&n->buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries per tree */ |
61a7cca0 JH |
672 | n->path[0] = n->path[1] = '\0'; |
673 | tws->next = n; | |
674 | tws->path[0] = path[0]; | |
675 | tws->path[1] = path[1]; | |
676 | } | |
677 | ||
678 | static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) | |
679 | { | |
680 | int ret; | |
681 | struct tree_write_stack *n = tws->next; | |
89c149f5 | 682 | struct object_id s; |
61a7cca0 JH |
683 | if (n) { |
684 | ret = tree_write_stack_finish_subtree(n); | |
685 | if (ret) | |
686 | return ret; | |
89c149f5 | 687 | ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash); |
61a7cca0 JH |
688 | if (ret) |
689 | return ret; | |
690 | strbuf_release(&n->buf); | |
691 | free(n); | |
692 | tws->next = NULL; | |
89c149f5 | 693 | write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash); |
61a7cca0 JH |
694 | tws->path[0] = tws->path[1] = '\0'; |
695 | } | |
696 | return 0; | |
697 | } | |
698 | ||
699 | static int write_each_note_helper(struct tree_write_stack *tws, | |
700 | const char *path, unsigned int mode, | |
490bc83a | 701 | const struct object_id *oid) |
61a7cca0 JH |
702 | { |
703 | size_t path_len = strlen(path); | |
704 | unsigned int n = 0; | |
705 | int ret; | |
706 | ||
707 | /* Determine common part of tree write stack */ | |
708 | while (tws && 3 * n < path_len && | |
709 | matches_tree_write_stack(tws, path + 3 * n)) { | |
710 | n++; | |
711 | tws = tws->next; | |
712 | } | |
713 | ||
714 | /* tws point to last matching tree_write_stack entry */ | |
715 | ret = tree_write_stack_finish_subtree(tws); | |
716 | if (ret) | |
717 | return ret; | |
718 | ||
719 | /* Start subtrees needed to satisfy path */ | |
720 | while (3 * n + 2 < path_len && path[3 * n + 2] == '/') { | |
721 | tree_write_stack_init_subtree(tws, path + 3 * n); | |
722 | n++; | |
723 | tws = tws->next; | |
724 | } | |
725 | ||
726 | /* There should be no more directory components in the given path */ | |
727 | assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL); | |
728 | ||
729 | /* Finally add given entry to the current tree object */ | |
730 | write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n), | |
490bc83a | 731 | oid->hash); |
61a7cca0 JH |
732 | |
733 | return 0; | |
734 | } | |
735 | ||
736 | struct write_each_note_data { | |
737 | struct tree_write_stack *root; | |
851c2b37 | 738 | struct non_note *next_non_note; |
61a7cca0 JH |
739 | }; |
740 | ||
851c2b37 JH |
741 | static int write_each_non_note_until(const char *note_path, |
742 | struct write_each_note_data *d) | |
743 | { | |
744 | struct non_note *n = d->next_non_note; | |
89fe121d | 745 | int cmp = 0, ret; |
851c2b37 JH |
746 | while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) { |
747 | if (note_path && cmp == 0) | |
748 | ; /* do nothing, prefer note to non-note */ | |
749 | else { | |
750 | ret = write_each_note_helper(d->root, n->path, n->mode, | |
490bc83a | 751 | &n->oid); |
851c2b37 JH |
752 | if (ret) |
753 | return ret; | |
754 | } | |
755 | n = n->next; | |
756 | } | |
757 | d->next_non_note = n; | |
758 | return 0; | |
759 | } | |
760 | ||
490bc83a | 761 | static int write_each_note(const struct object_id *object_oid, |
762 | const struct object_id *note_oid, char *note_path, | |
61a7cca0 JH |
763 | void *cb_data) |
764 | { | |
765 | struct write_each_note_data *d = | |
766 | (struct write_each_note_data *) cb_data; | |
767 | size_t note_path_len = strlen(note_path); | |
768 | unsigned int mode = 0100644; | |
769 | ||
770 | if (note_path[note_path_len - 1] == '/') { | |
771 | /* subtree entry */ | |
772 | note_path_len--; | |
773 | note_path[note_path_len] = '\0'; | |
774 | mode = 040000; | |
775 | } | |
89c149f5 | 776 | assert(note_path_len <= GIT_SHA1_HEXSZ + FANOUT_PATH_SEPARATORS); |
61a7cca0 | 777 | |
851c2b37 JH |
778 | /* Weave non-note entries into note entries */ |
779 | return write_each_non_note_until(note_path, d) || | |
490bc83a | 780 | write_each_note_helper(d->root, note_path, mode, note_oid); |
61a7cca0 JH |
781 | } |
782 | ||
00fbe636 JH |
783 | struct note_delete_list { |
784 | struct note_delete_list *next; | |
785 | const unsigned char *sha1; | |
786 | }; | |
787 | ||
490bc83a | 788 | static int prune_notes_helper(const struct object_id *object_oid, |
789 | const struct object_id *note_oid, char *note_path, | |
00fbe636 JH |
790 | void *cb_data) |
791 | { | |
792 | struct note_delete_list **l = (struct note_delete_list **) cb_data; | |
793 | struct note_delete_list *n; | |
794 | ||
490bc83a | 795 | if (has_object_file(object_oid)) |
00fbe636 JH |
796 | return 0; /* nothing to do for this note */ |
797 | ||
798 | /* failed to find object => prune this note */ | |
799 | n = (struct note_delete_list *) xmalloc(sizeof(*n)); | |
800 | n->next = *l; | |
490bc83a | 801 | n->sha1 = object_oid->hash; |
00fbe636 JH |
802 | *l = n; |
803 | return 0; | |
804 | } | |
805 | ||
73f464b5 JH |
806 | int combine_notes_concatenate(unsigned char *cur_sha1, |
807 | const unsigned char *new_sha1) | |
808 | { | |
809 | char *cur_msg = NULL, *new_msg = NULL, *buf; | |
810 | unsigned long cur_len, new_len, buf_len; | |
811 | enum object_type cur_type, new_type; | |
812 | int ret; | |
813 | ||
814 | /* read in both note blob objects */ | |
815 | if (!is_null_sha1(new_sha1)) | |
816 | new_msg = read_sha1_file(new_sha1, &new_type, &new_len); | |
817 | if (!new_msg || !new_len || new_type != OBJ_BLOB) { | |
818 | free(new_msg); | |
819 | return 0; | |
820 | } | |
821 | if (!is_null_sha1(cur_sha1)) | |
822 | cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len); | |
823 | if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) { | |
824 | free(cur_msg); | |
825 | free(new_msg); | |
826 | hashcpy(cur_sha1, new_sha1); | |
827 | return 0; | |
828 | } | |
829 | ||
d4990c4b | 830 | /* we will separate the notes by two newlines anyway */ |
73f464b5 JH |
831 | if (cur_msg[cur_len - 1] == '\n') |
832 | cur_len--; | |
833 | ||
834 | /* concatenate cur_msg and new_msg into buf */ | |
d4990c4b | 835 | buf_len = cur_len + 2 + new_len; |
73f464b5 JH |
836 | buf = (char *) xmalloc(buf_len); |
837 | memcpy(buf, cur_msg, cur_len); | |
838 | buf[cur_len] = '\n'; | |
d4990c4b JH |
839 | buf[cur_len + 1] = '\n'; |
840 | memcpy(buf + cur_len + 2, new_msg, new_len); | |
73f464b5 JH |
841 | free(cur_msg); |
842 | free(new_msg); | |
843 | ||
844 | /* create a new blob object from buf */ | |
845 | ret = write_sha1_file(buf, buf_len, blob_type, cur_sha1); | |
846 | free(buf); | |
847 | return ret; | |
848 | } | |
849 | ||
850 | int combine_notes_overwrite(unsigned char *cur_sha1, | |
851 | const unsigned char *new_sha1) | |
852 | { | |
853 | hashcpy(cur_sha1, new_sha1); | |
854 | return 0; | |
855 | } | |
856 | ||
857 | int combine_notes_ignore(unsigned char *cur_sha1, | |
858 | const unsigned char *new_sha1) | |
859 | { | |
860 | return 0; | |
861 | } | |
862 | ||
13135243 MH |
863 | /* |
864 | * Add the lines from the named object to list, with trailing | |
865 | * newlines removed. | |
866 | */ | |
867 | static int string_list_add_note_lines(struct string_list *list, | |
a6a09095 JH |
868 | const unsigned char *sha1) |
869 | { | |
870 | char *data; | |
871 | unsigned long len; | |
872 | enum object_type t; | |
a6a09095 JH |
873 | |
874 | if (is_null_sha1(sha1)) | |
875 | return 0; | |
876 | ||
877 | /* read_sha1_file NUL-terminates */ | |
878 | data = read_sha1_file(sha1, &t, &len); | |
879 | if (t != OBJ_BLOB || !data || !len) { | |
880 | free(data); | |
881 | return t != OBJ_BLOB || !data; | |
882 | } | |
883 | ||
13135243 MH |
884 | /* |
885 | * If the last line of the file is EOL-terminated, this will | |
886 | * add an empty string to the list. But it will be removed | |
887 | * later, along with any empty strings that came from empty | |
888 | * lines within the file. | |
889 | */ | |
890 | string_list_split(list, data, '\n', -1); | |
891 | free(data); | |
a6a09095 JH |
892 | return 0; |
893 | } | |
894 | ||
895 | static int string_list_join_lines_helper(struct string_list_item *item, | |
896 | void *cb_data) | |
897 | { | |
898 | struct strbuf *buf = cb_data; | |
899 | strbuf_addstr(buf, item->string); | |
900 | strbuf_addch(buf, '\n'); | |
901 | return 0; | |
902 | } | |
903 | ||
904 | int combine_notes_cat_sort_uniq(unsigned char *cur_sha1, | |
905 | const unsigned char *new_sha1) | |
906 | { | |
f992f0c8 | 907 | struct string_list sort_uniq_list = STRING_LIST_INIT_DUP; |
a6a09095 JH |
908 | struct strbuf buf = STRBUF_INIT; |
909 | int ret = 1; | |
910 | ||
911 | /* read both note blob objects into unique_lines */ | |
912 | if (string_list_add_note_lines(&sort_uniq_list, cur_sha1)) | |
913 | goto out; | |
914 | if (string_list_add_note_lines(&sort_uniq_list, new_sha1)) | |
915 | goto out; | |
13135243 | 916 | string_list_remove_empty_items(&sort_uniq_list, 0); |
3383e199 | 917 | string_list_sort(&sort_uniq_list); |
13135243 | 918 | string_list_remove_duplicates(&sort_uniq_list, 0); |
a6a09095 JH |
919 | |
920 | /* create a new blob object from sort_uniq_list */ | |
921 | if (for_each_string_list(&sort_uniq_list, | |
922 | string_list_join_lines_helper, &buf)) | |
923 | goto out; | |
924 | ||
925 | ret = write_sha1_file(buf.buf, buf.len, blob_type, cur_sha1); | |
926 | ||
927 | out: | |
928 | strbuf_release(&buf); | |
929 | string_list_clear(&sort_uniq_list, 0); | |
930 | return ret; | |
931 | } | |
932 | ||
fd95035f | 933 | static int string_list_add_one_ref(const char *refname, const struct object_id *oid, |
894a9d33 TR |
934 | int flag, void *cb) |
935 | { | |
936 | struct string_list *refs = cb; | |
d235e994 MH |
937 | if (!unsorted_string_list_has_string(refs, refname)) |
938 | string_list_append(refs, refname); | |
894a9d33 TR |
939 | return 0; |
940 | } | |
941 | ||
8c46bf90 MH |
942 | /* |
943 | * The list argument must have strdup_strings set on it. | |
944 | */ | |
894a9d33 TR |
945 | void string_list_add_refs_by_glob(struct string_list *list, const char *glob) |
946 | { | |
8c46bf90 | 947 | assert(list->strdup_strings); |
894a9d33 | 948 | if (has_glob_specials(glob)) { |
fd95035f | 949 | for_each_glob_ref(string_list_add_one_ref, glob, list); |
894a9d33 | 950 | } else { |
89c149f5 | 951 | struct object_id oid; |
952 | if (get_oid(glob, &oid)) | |
894a9d33 TR |
953 | warning("notes ref %s is invalid", glob); |
954 | if (!unsorted_string_list_has_string(list, glob)) | |
1d2f80fa | 955 | string_list_append(list, glob); |
894a9d33 TR |
956 | } |
957 | } | |
958 | ||
959 | void string_list_add_refs_from_colon_sep(struct string_list *list, | |
960 | const char *globs) | |
961 | { | |
6fa23773 MH |
962 | struct string_list split = STRING_LIST_INIT_NODUP; |
963 | char *globs_copy = xstrdup(globs); | |
894a9d33 TR |
964 | int i; |
965 | ||
6fa23773 MH |
966 | string_list_split_in_place(&split, globs_copy, ':', -1); |
967 | string_list_remove_empty_items(&split, 0); | |
894a9d33 | 968 | |
6fa23773 MH |
969 | for (i = 0; i < split.nr; i++) |
970 | string_list_add_refs_by_glob(list, split.items[i].string); | |
894a9d33 | 971 | |
6fa23773 MH |
972 | string_list_clear(&split, 0); |
973 | free(globs_copy); | |
894a9d33 TR |
974 | } |
975 | ||
894a9d33 TR |
976 | static int notes_display_config(const char *k, const char *v, void *cb) |
977 | { | |
978 | int *load_refs = cb; | |
979 | ||
980 | if (*load_refs && !strcmp(k, "notes.displayref")) { | |
981 | if (!v) | |
982 | config_error_nonbool(k); | |
983 | string_list_add_refs_by_glob(&display_notes_refs, v); | |
984 | } | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
4a9cf1ce | 989 | const char *default_notes_ref(void) |
894a9d33 TR |
990 | { |
991 | const char *notes_ref = NULL; | |
992 | if (!notes_ref) | |
993 | notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT); | |
994 | if (!notes_ref) | |
995 | notes_ref = notes_ref_name; /* value of core.notesRef config */ | |
996 | if (!notes_ref) | |
997 | notes_ref = GIT_NOTES_DEFAULT_REF; | |
998 | return notes_ref; | |
999 | } | |
1000 | ||
73f464b5 JH |
1001 | void init_notes(struct notes_tree *t, const char *notes_ref, |
1002 | combine_notes_fn combine_notes, int flags) | |
23123aec | 1003 | { |
13ac1410 | 1004 | struct object_id oid, object_oid; |
23123aec JH |
1005 | unsigned mode; |
1006 | struct leaf_node root_tree; | |
fd53c9eb | 1007 | |
cd305392 JH |
1008 | if (!t) |
1009 | t = &default_notes_tree; | |
1010 | assert(!t->initialized); | |
709f79b0 JH |
1011 | |
1012 | if (!notes_ref) | |
894a9d33 | 1013 | notes_ref = default_notes_ref(); |
709f79b0 | 1014 | |
73f464b5 JH |
1015 | if (!combine_notes) |
1016 | combine_notes = combine_notes_concatenate; | |
1017 | ||
65bbf082 | 1018 | t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node)); |
851c2b37 JH |
1019 | t->first_non_note = NULL; |
1020 | t->prev_non_note = NULL; | |
8c53f071 | 1021 | t->ref = xstrdup_or_null(notes_ref); |
ee76f92f | 1022 | t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL; |
73f464b5 | 1023 | t->combine_notes = combine_notes; |
cd305392 | 1024 | t->initialized = 1; |
7f710ea9 | 1025 | t->dirty = 0; |
cd305392 | 1026 | |
709f79b0 | 1027 | if (flags & NOTES_INIT_EMPTY || !notes_ref || |
e82caf38 | 1028 | get_oid_treeish(notes_ref, &object_oid)) |
fd53c9eb | 1029 | return; |
13ac1410 | 1030 | if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, object_oid.hash)) |
ee76f92f | 1031 | die("Cannot use notes ref %s", notes_ref); |
13ac1410 | 1032 | if (get_tree_entry(object_oid.hash, "", oid.hash, &mode)) |
709f79b0 | 1033 | die("Failed to read notes tree referenced by %s (%s)", |
13ac1410 | 1034 | notes_ref, oid_to_hex(&object_oid)); |
fd53c9eb | 1035 | |
5dcc969e | 1036 | oidclr(&root_tree.key_oid); |
1037 | oidcpy(&root_tree.val_oid, &oid); | |
851c2b37 | 1038 | load_subtree(t, &root_tree, t->root, 0); |
fd53c9eb JS |
1039 | } |
1040 | ||
ee76f92f | 1041 | struct notes_tree **load_notes_trees(struct string_list *refs, int flags) |
894a9d33 | 1042 | { |
8a57c6e9 AR |
1043 | struct string_list_item *item; |
1044 | int counter = 0; | |
894a9d33 | 1045 | struct notes_tree **trees; |
b32fa95f | 1046 | ALLOC_ARRAY(trees, refs->nr + 1); |
8a57c6e9 AR |
1047 | for_each_string_list_item(item, refs) { |
1048 | struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree)); | |
ee76f92f | 1049 | init_notes(t, item->string, combine_notes_ignore, flags); |
8a57c6e9 AR |
1050 | trees[counter++] = t; |
1051 | } | |
1052 | trees[counter] = NULL; | |
894a9d33 TR |
1053 | return trees; |
1054 | } | |
1055 | ||
1056 | void init_display_notes(struct display_notes_opt *opt) | |
1057 | { | |
1058 | char *display_ref_env; | |
1059 | int load_config_refs = 0; | |
1060 | display_notes_refs.strdup_strings = 1; | |
1061 | ||
1062 | assert(!display_notes_trees); | |
1063 | ||
3a03cf6b JK |
1064 | if (!opt || opt->use_default_notes > 0 || |
1065 | (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) { | |
1d2f80fa | 1066 | string_list_append(&display_notes_refs, default_notes_ref()); |
894a9d33 TR |
1067 | display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT); |
1068 | if (display_ref_env) { | |
1069 | string_list_add_refs_from_colon_sep(&display_notes_refs, | |
1070 | display_ref_env); | |
1071 | load_config_refs = 0; | |
1072 | } else | |
1073 | load_config_refs = 1; | |
1074 | } | |
1075 | ||
1076 | git_config(notes_display_config, &load_config_refs); | |
1077 | ||
304cc11c | 1078 | if (opt) { |
8a57c6e9 | 1079 | struct string_list_item *item; |
304cc11c | 1080 | for_each_string_list_item(item, &opt->extra_notes_refs) |
8a57c6e9 AR |
1081 | string_list_add_refs_by_glob(&display_notes_refs, |
1082 | item->string); | |
1083 | } | |
894a9d33 | 1084 | |
ee76f92f | 1085 | display_notes_trees = load_notes_trees(&display_notes_refs, 0); |
894a9d33 TR |
1086 | string_list_clear(&display_notes_refs, 0); |
1087 | } | |
1088 | ||
5ee8a954 | 1089 | int add_note(struct notes_tree *t, const struct object_id *object_oid, |
1090 | const struct object_id *note_oid, combine_notes_fn combine_notes) | |
2626b536 JH |
1091 | { |
1092 | struct leaf_node *l; | |
1093 | ||
cd305392 JH |
1094 | if (!t) |
1095 | t = &default_notes_tree; | |
1096 | assert(t->initialized); | |
7f710ea9 | 1097 | t->dirty = 1; |
73f464b5 JH |
1098 | if (!combine_notes) |
1099 | combine_notes = t->combine_notes; | |
2626b536 | 1100 | l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node)); |
5ee8a954 | 1101 | oidcpy(&l->key_oid, object_oid); |
1102 | oidcpy(&l->val_oid, note_oid); | |
180619a5 | 1103 | return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes); |
2626b536 JH |
1104 | } |
1105 | ||
1ee1e43d | 1106 | int remove_note(struct notes_tree *t, const unsigned char *object_sha1) |
1ec666b0 JH |
1107 | { |
1108 | struct leaf_node l; | |
1109 | ||
cd305392 JH |
1110 | if (!t) |
1111 | t = &default_notes_tree; | |
1112 | assert(t->initialized); | |
5dcc969e | 1113 | hashcpy(l.key_oid.hash, object_sha1); |
1114 | oidclr(&l.val_oid); | |
a502ab93 | 1115 | note_tree_remove(t, t->root, 0, &l); |
5dcc969e | 1116 | if (is_null_oid(&l.val_oid)) /* no note was removed */ |
1ee1e43d JH |
1117 | return 1; |
1118 | t->dirty = 1; | |
1119 | return 0; | |
1ec666b0 JH |
1120 | } |
1121 | ||
9ef72230 | 1122 | const struct object_id *get_note(struct notes_tree *t, |
5ee8a954 | 1123 | const struct object_id *oid) |
fd53c9eb | 1124 | { |
9b391f21 JH |
1125 | struct leaf_node *found; |
1126 | ||
cd305392 JH |
1127 | if (!t) |
1128 | t = &default_notes_tree; | |
1129 | assert(t->initialized); | |
5ee8a954 | 1130 | found = note_tree_find(t, t->root, 0, oid->hash); |
9ef72230 | 1131 | return found ? &found->val_oid : NULL; |
fd53c9eb | 1132 | } |
a97a7468 | 1133 | |
cd305392 JH |
1134 | int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, |
1135 | void *cb_data) | |
73f77b90 | 1136 | { |
cd305392 JH |
1137 | if (!t) |
1138 | t = &default_notes_tree; | |
1139 | assert(t->initialized); | |
851c2b37 | 1140 | return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data); |
73f77b90 JH |
1141 | } |
1142 | ||
cd305392 | 1143 | int write_notes_tree(struct notes_tree *t, unsigned char *result) |
61a7cca0 JH |
1144 | { |
1145 | struct tree_write_stack root; | |
1146 | struct write_each_note_data cb_data; | |
1147 | int ret; | |
1148 | ||
cd305392 JH |
1149 | if (!t) |
1150 | t = &default_notes_tree; | |
1151 | assert(t->initialized); | |
61a7cca0 JH |
1152 | |
1153 | /* Prepare for traversal of current notes tree */ | |
1154 | root.next = NULL; /* last forward entry in list is grounded */ | |
89c149f5 | 1155 | strbuf_init(&root.buf, 256 * (32 + GIT_SHA1_HEXSZ)); /* assume 256 entries */ |
61a7cca0 JH |
1156 | root.path[0] = root.path[1] = '\0'; |
1157 | cb_data.root = &root; | |
851c2b37 | 1158 | cb_data.next_non_note = t->first_non_note; |
61a7cca0 JH |
1159 | |
1160 | /* Write tree objects representing current notes tree */ | |
cd305392 | 1161 | ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES | |
61a7cca0 JH |
1162 | FOR_EACH_NOTE_YIELD_SUBTREES, |
1163 | write_each_note, &cb_data) || | |
851c2b37 | 1164 | write_each_non_note_until(NULL, &cb_data) || |
61a7cca0 JH |
1165 | tree_write_stack_finish_subtree(&root) || |
1166 | write_sha1_file(root.buf.buf, root.buf.len, tree_type, result); | |
1167 | strbuf_release(&root.buf); | |
1168 | return ret; | |
1169 | } | |
1170 | ||
a9f2adff | 1171 | void prune_notes(struct notes_tree *t, int flags) |
00fbe636 JH |
1172 | { |
1173 | struct note_delete_list *l = NULL; | |
1174 | ||
1175 | if (!t) | |
1176 | t = &default_notes_tree; | |
1177 | assert(t->initialized); | |
1178 | ||
1179 | for_each_note(t, 0, prune_notes_helper, &l); | |
1180 | ||
1181 | while (l) { | |
a9f2adff MG |
1182 | if (flags & NOTES_PRUNE_VERBOSE) |
1183 | printf("%s\n", sha1_to_hex(l->sha1)); | |
1184 | if (!(flags & NOTES_PRUNE_DRYRUN)) | |
1185 | remove_note(t, l->sha1); | |
00fbe636 JH |
1186 | l = l->next; |
1187 | } | |
1188 | } | |
1189 | ||
cd305392 | 1190 | void free_notes(struct notes_tree *t) |
27d57564 | 1191 | { |
cd305392 JH |
1192 | if (!t) |
1193 | t = &default_notes_tree; | |
1194 | if (t->root) | |
1195 | note_tree_free(t->root); | |
1196 | free(t->root); | |
851c2b37 JH |
1197 | while (t->first_non_note) { |
1198 | t->prev_non_note = t->first_non_note->next; | |
1199 | free(t->first_non_note->path); | |
1200 | free(t->first_non_note); | |
1201 | t->first_non_note = t->prev_non_note; | |
1202 | } | |
cd305392 JH |
1203 | free(t->ref); |
1204 | memset(t, 0, sizeof(struct notes_tree)); | |
27d57564 JH |
1205 | } |
1206 | ||
96531a5e JH |
1207 | /* |
1208 | * Fill the given strbuf with the notes associated with the given object. | |
1209 | * | |
1210 | * If the given notes_tree structure is not initialized, it will be auto- | |
1211 | * initialized to the default value (see documentation for init_notes() above). | |
1212 | * If the given notes_tree is NULL, the internal/default notes_tree will be | |
1213 | * used instead. | |
1214 | * | |
76141e2e JH |
1215 | * (raw != 0) gives the %N userformat; otherwise, the note message is given |
1216 | * for human consumption. | |
96531a5e | 1217 | */ |
fb61e4d3 | 1218 | static void format_note(struct notes_tree *t, const struct object_id *object_oid, |
76141e2e | 1219 | struct strbuf *sb, const char *output_encoding, int raw) |
a97a7468 JS |
1220 | { |
1221 | static const char utf8[] = "utf-8"; | |
9ef72230 | 1222 | const struct object_id *oid; |
a97a7468 JS |
1223 | char *msg, *msg_p; |
1224 | unsigned long linelen, msglen; | |
1225 | enum object_type type; | |
1226 | ||
cd305392 JH |
1227 | if (!t) |
1228 | t = &default_notes_tree; | |
1229 | if (!t->initialized) | |
73f464b5 | 1230 | init_notes(t, NULL, NULL, 0); |
a97a7468 | 1231 | |
5ee8a954 | 1232 | oid = get_note(t, object_oid); |
9ef72230 | 1233 | if (!oid) |
a97a7468 JS |
1234 | return; |
1235 | ||
9ef72230 | 1236 | if (!(msg = read_sha1_file(oid->hash, &type, &msglen)) || type != OBJ_BLOB) { |
a97a7468 JS |
1237 | free(msg); |
1238 | return; | |
1239 | } | |
1240 | ||
1241 | if (output_encoding && *output_encoding && | |
0e18bcd5 | 1242 | !is_encoding_utf8(output_encoding)) { |
a97a7468 JS |
1243 | char *reencoded = reencode_string(msg, output_encoding, utf8); |
1244 | if (reencoded) { | |
1245 | free(msg); | |
1246 | msg = reencoded; | |
1247 | msglen = strlen(msg); | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | /* we will end the annotation by a newline anyway */ | |
1252 | if (msglen && msg[msglen - 1] == '\n') | |
1253 | msglen--; | |
1254 | ||
76141e2e | 1255 | if (!raw) { |
894a9d33 TR |
1256 | const char *ref = t->ref; |
1257 | if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) { | |
1258 | strbuf_addstr(sb, "\nNotes:\n"); | |
1259 | } else { | |
59556548 | 1260 | if (starts_with(ref, "refs/")) |
894a9d33 | 1261 | ref += 5; |
59556548 | 1262 | if (starts_with(ref, "notes/")) |
894a9d33 TR |
1263 | ref += 6; |
1264 | strbuf_addf(sb, "\nNotes (%s):\n", ref); | |
1265 | } | |
1266 | } | |
a97a7468 JS |
1267 | |
1268 | for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) { | |
1269 | linelen = strchrnul(msg_p, '\n') - msg_p; | |
1270 | ||
76141e2e | 1271 | if (!raw) |
c56fcc89 | 1272 | strbuf_addstr(sb, " "); |
a97a7468 JS |
1273 | strbuf_add(sb, msg_p, linelen); |
1274 | strbuf_addch(sb, '\n'); | |
1275 | } | |
1276 | ||
1277 | free(msg); | |
1278 | } | |
894a9d33 | 1279 | |
fb61e4d3 | 1280 | void format_display_notes(const struct object_id *object_oid, |
76141e2e | 1281 | struct strbuf *sb, const char *output_encoding, int raw) |
894a9d33 TR |
1282 | { |
1283 | int i; | |
1284 | assert(display_notes_trees); | |
1285 | for (i = 0; display_notes_trees[i]; i++) | |
fb61e4d3 | 1286 | format_note(display_notes_trees[i], object_oid, sb, |
76141e2e | 1287 | output_encoding, raw); |
894a9d33 | 1288 | } |
160baa0d TR |
1289 | |
1290 | int copy_note(struct notes_tree *t, | |
5ee8a954 | 1291 | const struct object_id *from_obj, const struct object_id *to_obj, |
180619a5 | 1292 | int force, combine_notes_fn combine_notes) |
160baa0d | 1293 | { |
9ef72230 | 1294 | const struct object_id *note = get_note(t, from_obj); |
1295 | const struct object_id *existing_note = get_note(t, to_obj); | |
160baa0d TR |
1296 | |
1297 | if (!force && existing_note) | |
1298 | return 1; | |
1299 | ||
1300 | if (note) | |
5ee8a954 | 1301 | return add_note(t, to_obj, note, combine_notes); |
160baa0d | 1302 | else if (existing_note) |
5ee8a954 | 1303 | return add_note(t, to_obj, &null_oid, combine_notes); |
160baa0d TR |
1304 | |
1305 | return 0; | |
1306 | } | |
03bb5789 JK |
1307 | |
1308 | void expand_notes_ref(struct strbuf *sb) | |
1309 | { | |
59556548 | 1310 | if (starts_with(sb->buf, "refs/notes/")) |
03bb5789 | 1311 | return; /* we're happy */ |
59556548 | 1312 | else if (starts_with(sb->buf, "notes/")) |
03bb5789 JK |
1313 | strbuf_insert(sb, 0, "refs/", 5); |
1314 | else | |
1315 | strbuf_insert(sb, 0, "refs/notes/", 11); | |
1316 | } | |
b3715b75 JK |
1317 | |
1318 | void expand_loose_notes_ref(struct strbuf *sb) | |
1319 | { | |
89c149f5 | 1320 | struct object_id object; |
b3715b75 | 1321 | |
89c149f5 | 1322 | if (get_oid(sb->buf, &object)) { |
b3715b75 JK |
1323 | /* fallback to expand_notes_ref */ |
1324 | expand_notes_ref(sb); | |
1325 | } | |
1326 | } |