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