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