Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1309 lines (1168 sloc) 37.048 kB
a97a746 @dscho Introduce commit notes
dscho authored
1 #include "cache.h"
2 #include "notes.h"
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
3 #include "blob.h"
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
4 #include "tree.h"
a97a746 @dscho Introduce commit notes
dscho authored
5 #include "utf8.h"
6 #include "strbuf.h"
fd53c9e @dscho Speed up git notes lookup
dscho authored
7 #include "tree-walk.h"
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
8 #include "string-list.h"
9 #include "refs.h"
fd53c9e @dscho Speed up git notes lookup
dscho authored
10
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
11 /*
12 * Use a non-balancing simple 16-tree structure with struct int_node as
13 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
14 * 16-array of pointers to its children.
15 * The bottom 2 bits of each pointer is used to identify the pointer type
16 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
17 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
18 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
19 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
20 *
21 * The root node is a statically allocated struct int_node.
22 */
23 struct int_node {
24 void *a[16];
fd53c9e @dscho Speed up git notes lookup
dscho authored
25 };
26
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
27 /*
28 * Leaf nodes come in two variants, note entries and subtree entries,
29 * distinguished by the LSb of the leaf node pointer (see above).
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
30 * As a note entry, the key is the SHA1 of the referenced object, and the
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
31 * value is the SHA1 of the note object.
32 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
33 * referenced object, using the last byte of the key to store the length of
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
34 * the prefix. The value is the SHA1 of the tree object containing the notes
35 * subtree.
36 */
37 struct leaf_node {
38 unsigned char key_sha1[20];
39 unsigned char val_sha1[20];
fd53c9e @dscho Speed up git notes lookup
dscho authored
40 };
a97a746 @dscho Introduce commit notes
dscho authored
41
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
42 /*
43 * A notes tree may contain entries that are not notes, and that do not follow
44 * the naming conventions of notes. There are typically none/few of these, but
45 * we still need to keep track of them. Keep a simple linked list sorted alpha-
46 * betically on the non-note path. The list is populated when parsing tree
47 * objects in load_subtree(), and the non-notes are correctly written back into
48 * the tree objects produced by write_notes_tree().
49 */
50 struct non_note {
51 struct non_note *next; /* grounded (last->next == NULL) */
52 char *path;
53 unsigned int mode;
54 unsigned char sha1[20];
55 };
56
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
57 #define PTR_TYPE_NULL 0
58 #define PTR_TYPE_INTERNAL 1
59 #define PTR_TYPE_NOTE 2
60 #define PTR_TYPE_SUBTREE 3
fd53c9e @dscho Speed up git notes lookup
dscho authored
61
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
62 #define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
63 #define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
64 #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
fd53c9e @dscho Speed up git notes lookup
dscho authored
65
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
66 #define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
fd53c9e @dscho Speed up git notes lookup
dscho authored
67
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
68 #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
69 (memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))
fd53c9e @dscho Speed up git notes lookup
dscho authored
70
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
71 struct notes_tree default_notes_tree;
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
72
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
73 static struct string_list display_notes_refs;
74 static struct notes_tree **display_notes_trees;
75
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
76 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
77 struct int_node *node, unsigned int n);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
78
79 /*
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
80 * Search the tree until the appropriate location for the given key is found:
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
81 * 1. Start at the root node, with n = 0
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
82 * 2. If a[0] at the current level is a matching subtree entry, unpack that
83 * subtree entry and remove it; restart search at the current level.
84 * 3. Use the nth nibble of the key as an index into a:
85 * - If a[n] is an int_node, recurse from #2 into that node and increment n
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
86 * - If a matching subtree entry, unpack that subtree entry (and remove it);
87 * restart search at the current level.
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
88 * - Otherwise, we have found one of the following:
89 * - a subtree entry which does not match the key
90 * - a note entry which may or may not match the key
91 * - an unused leaf node (NULL)
92 * In any case, set *tree and *n, and return pointer to the tree location.
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
93 */
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
94 static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
95 unsigned char *n, const unsigned char *key_sha1)
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
96 {
97 struct leaf_node *l;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
98 unsigned char i;
99 void *p = (*tree)->a[0];
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
100
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
101 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
102 l = (struct leaf_node *) CLR_PTR_TYPE(p);
103 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
104 /* unpack tree and resume search */
105 (*tree)->a[0] = NULL;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
106 load_subtree(t, l, *tree, *n);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
107 free(l);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
108 return note_tree_search(t, tree, n, key_sha1);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
109 }
110 }
111
112 i = GET_NIBBLE(*n, key_sha1);
113 p = (*tree)->a[i];
0ab1faa @jherland Minor cosmetic fixes to notes.c
jherland authored
114 switch (GET_PTR_TYPE(p)) {
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
115 case PTR_TYPE_INTERNAL:
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
116 *tree = CLR_PTR_TYPE(p);
117 (*n)++;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
118 return note_tree_search(t, tree, n, key_sha1);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
119 case PTR_TYPE_SUBTREE:
120 l = (struct leaf_node *) CLR_PTR_TYPE(p);
121 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
122 /* unpack tree and resume search */
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
123 (*tree)->a[i] = NULL;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
124 load_subtree(t, l, *tree, *n);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
125 free(l);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
126 return note_tree_search(t, tree, n, key_sha1);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
127 }
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
128 /* fall through */
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
129 default:
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
130 return &((*tree)->a[i]);
fd53c9e @dscho Speed up git notes lookup
dscho authored
131 }
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
132 }
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
133
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
134 /*
135 * To find a leaf_node:
136 * Search to the tree location appropriate for the given key:
137 * If a note entry with matching key, return the note entry, else return NULL.
138 */
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
139 static struct leaf_node *note_tree_find(struct notes_tree *t,
140 struct int_node *tree, unsigned char n,
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
141 const unsigned char *key_sha1)
142 {
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
143 void **p = note_tree_search(t, &tree, &n, key_sha1);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
144 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
145 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
146 if (!hashcmp(key_sha1, l->key_sha1))
147 return l;
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
148 }
149 return NULL;
fd53c9e @dscho Speed up git notes lookup
dscho authored
150 }
151
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
152 /*
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
153 * How to consolidate an int_node:
154 * If there are > 1 non-NULL entries, give up and return non-zero.
155 * Otherwise replace the int_node at the given index in the given parent node
156 * with the only entry (or a NULL entry if no entries) from the given tree,
157 * and return 0.
158 */
159 static int note_tree_consolidate(struct int_node *tree,
160 struct int_node *parent, unsigned char index)
161 {
162 unsigned int i;
163 void *p = NULL;
164
165 assert(tree && parent);
166 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
167
168 for (i = 0; i < 16; i++) {
169 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
170 if (p) /* more than one entry */
171 return -2;
172 p = tree->a[i];
173 }
174 }
175
176 /* replace tree with p in parent[index] */
177 parent->a[index] = p;
178 free(tree);
179 return 0;
180 }
181
182 /*
183 * To remove a leaf_node:
184 * Search to the tree location appropriate for the given leaf_node's key:
185 * - If location does not hold a matching entry, abort and do nothing.
1ee1e43 @jherland notes: Don't create (empty) commit when removing non-existing notes
jherland authored
186 * - Copy the matching entry's value into the given entry.
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
187 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
188 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
189 */
1ee1e43 @jherland notes: Don't create (empty) commit when removing non-existing notes
jherland authored
190 static void note_tree_remove(struct notes_tree *t,
191 struct int_node *tree, unsigned char n,
192 struct leaf_node *entry)
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
193 {
194 struct leaf_node *l;
195 struct int_node *parent_stack[20];
196 unsigned char i, j;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
197 void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
198
199 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
200 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
201 return; /* type mismatch, nothing to remove */
202 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
203 if (hashcmp(l->key_sha1, entry->key_sha1))
204 return; /* key mismatch, nothing to remove */
205
206 /* we have found a matching entry */
1ee1e43 @jherland notes: Don't create (empty) commit when removing non-existing notes
jherland authored
207 hashcpy(entry->val_sha1, l->val_sha1);
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
208 free(l);
209 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
210
211 /* consolidate this tree level, and parent levels, if possible */
212 if (!n)
213 return; /* cannot consolidate top level */
214 /* first, build stack of ancestors between root and current node */
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
215 parent_stack[0] = t->root;
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
216 for (i = 0; i < n; i++) {
217 j = GET_NIBBLE(i, entry->key_sha1);
218 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
219 }
220 assert(i == n && parent_stack[i] == tree);
221 /* next, unwind stack until note_tree_consolidate() is done */
222 while (i > 0 &&
223 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
224 GET_NIBBLE(i - 1, entry->key_sha1)))
225 i--;
226 }
227
a5cdebe @jherland notes.c: Reorder functions in preparation for next commit
jherland authored
228 /*
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
229 * To insert a leaf_node:
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
230 * Search to the tree location appropriate for the given leaf_node's key:
231 * - If location is unused (NULL), store the tweaked pointer directly there
232 * - If location holds a note entry that matches the note-to-be-inserted, then
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
233 * combine the two notes (by calling the given combine_notes function).
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
234 * - If location holds a note entry that matches the subtree-to-be-inserted,
235 * then unpack the subtree-to-be-inserted into the location.
236 * - If location holds a matching subtree entry, unpack the subtree at that
237 * location, and restart the insert operation from that level.
238 * - Else, create a new int_node, holding both the node-at-location and the
239 * node-to-be-inserted, and store the new int_node into the location.
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
240 */
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
241 static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
242 unsigned char n, struct leaf_node *entry, unsigned char type,
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
243 combine_notes_fn combine_notes)
fd53c9e @dscho Speed up git notes lookup
dscho authored
244 {
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
245 struct int_node *new_node;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
246 struct leaf_node *l;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
247 void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
248 int ret = 0;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
249
250 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
251 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
0ab1faa @jherland Minor cosmetic fixes to notes.c
jherland authored
252 switch (GET_PTR_TYPE(*p)) {
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
253 case PTR_TYPE_NULL:
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
254 assert(!*p);
e2656c8 @jherland notes.h/c: Allow combine_notes functions to remove notes
jherland authored
255 if (is_null_sha1(entry->val_sha1))
256 free(entry);
257 else
258 *p = SET_PTR_TYPE(entry, type);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
259 return 0;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
260 case PTR_TYPE_NOTE:
261 switch (type) {
262 case PTR_TYPE_NOTE:
263 if (!hashcmp(l->key_sha1, entry->key_sha1)) {
264 /* skip concatenation if l == entry */
265 if (!hashcmp(l->val_sha1, entry->val_sha1))
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
266 return 0;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
267
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
268 ret = combine_notes(l->val_sha1,
269 entry->val_sha1);
270 if (!ret && is_null_sha1(l->val_sha1))
e2656c8 @jherland notes.h/c: Allow combine_notes functions to remove notes
jherland authored
271 note_tree_remove(t, tree, n, entry);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
272 free(entry);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
273 return ret;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
274 }
275 break;
276 case PTR_TYPE_SUBTREE:
277 if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
278 entry->key_sha1)) {
279 /* unpack 'entry' */
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
280 load_subtree(t, entry, tree, n);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
281 free(entry);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
282 return 0;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
283 }
284 break;
285 }
286 break;
287 case PTR_TYPE_SUBTREE:
288 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
289 /* unpack 'l' and restart insert */
290 *p = NULL;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
291 load_subtree(t, l, tree, n);
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
292 free(l);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
293 return note_tree_insert(t, tree, n, entry, type,
294 combine_notes);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
295 }
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
296 break;
fd53c9e @dscho Speed up git notes lookup
dscho authored
297 }
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
298
299 /* non-matching leaf_node */
300 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
301 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
e2656c8 @jherland notes.h/c: Allow combine_notes functions to remove notes
jherland authored
302 if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
303 free(entry);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
304 return 0;
e2656c8 @jherland notes.h/c: Allow combine_notes functions to remove notes
jherland authored
305 }
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
306 new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
307 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
308 combine_notes);
309 if (ret)
310 return ret;
ef8db63 @jherland Refactor notes code to concatenate multiple notes annotating the same…
jherland authored
311 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
312 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
313 }
fd53c9e @dscho Speed up git notes lookup
dscho authored
314
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
315 /* Free the entire notes data contained in the given tree */
316 static void note_tree_free(struct int_node *tree)
317 {
318 unsigned int i;
319 for (i = 0; i < 16; i++) {
320 void *p = tree->a[i];
0ab1faa @jherland Minor cosmetic fixes to notes.c
jherland authored
321 switch (GET_PTR_TYPE(p)) {
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
322 case PTR_TYPE_INTERNAL:
323 note_tree_free(CLR_PTR_TYPE(p));
324 /* fall through */
325 case PTR_TYPE_NOTE:
326 case PTR_TYPE_SUBTREE:
327 free(CLR_PTR_TYPE(p));
328 }
fd53c9e @dscho Speed up git notes lookup
dscho authored
329 }
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
330 }
fd53c9e @dscho Speed up git notes lookup
dscho authored
331
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
332 /*
333 * Convert a partial SHA1 hex string to the corresponding partial SHA1 value.
334 * - hex - Partial SHA1 segment in ASCII hex format
335 * - hex_len - Length of above segment. Must be multiple of 2 between 0 and 40
336 * - sha1 - Partial SHA1 value is written here
337 * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20
0ab1faa @jherland Minor cosmetic fixes to notes.c
jherland authored
338 * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
339 * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).
340 * Pads sha1 with NULs up to sha1_len (not included in returned length).
341 */
342 static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,
343 unsigned char *sha1, unsigned int sha1_len)
344 {
345 unsigned int i, len = hex_len >> 1;
346 if (hex_len % 2 != 0 || len > sha1_len)
347 return -1;
348 for (i = 0; i < len; i++) {
349 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
350 if (val & ~0xff)
351 return -1;
352 *sha1++ = val;
353 hex += 2;
354 }
355 for (; i < sha1_len; i++)
356 *sha1++ = 0;
357 return len;
fd53c9e @dscho Speed up git notes lookup
dscho authored
358 }
359
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
360 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
361 {
362 return strcmp(a->path, b->path);
363 }
364
365 static void add_non_note(struct notes_tree *t, const char *path,
366 unsigned int mode, const unsigned char *sha1)
367 {
368 struct non_note *p = t->prev_non_note, *n;
369 n = (struct non_note *) xmalloc(sizeof(struct non_note));
370 n->next = NULL;
371 n->path = xstrdup(path);
372 n->mode = mode;
373 hashcpy(n->sha1, sha1);
374 t->prev_non_note = n;
375
376 if (!t->first_non_note) {
377 t->first_non_note = n;
378 return;
379 }
380
381 if (non_note_cmp(p, n) < 0)
382 ; /* do nothing */
383 else if (non_note_cmp(t->first_non_note, n) <= 0)
384 p = t->first_non_note;
385 else {
386 /* n sorts before t->first_non_note */
387 n->next = t->first_non_note;
388 t->first_non_note = n;
389 return;
390 }
391
392 /* n sorts equal or after p */
393 while (p->next && non_note_cmp(p->next, n) <= 0)
394 p = p->next;
395
396 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
397 assert(strcmp(p->path, n->path) == 0);
398 p->mode = n->mode;
399 hashcpy(p->sha1, n->sha1);
400 free(n);
401 t->prev_non_note = p;
402 return;
403 }
404
405 /* n sorts between p and p->next */
406 n->next = p->next;
407 p->next = n;
408 }
409
410 static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
411 struct int_node *node, unsigned int n)
fd53c9e @dscho Speed up git notes lookup
dscho authored
412 {
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
413 unsigned char object_sha1[20];
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
414 unsigned int prefix_len;
415 void *buf;
fd53c9e @dscho Speed up git notes lookup
dscho authored
416 struct tree_desc desc;
417 struct name_entry entry;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
418 int len, path_len;
419 unsigned char type;
420 struct leaf_node *l;
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
421
422 buf = fill_tree_descriptor(&desc, subtree->val_sha1);
423 if (!buf)
424 die("Could not read %s for notes-index",
425 sha1_to_hex(subtree->val_sha1));
426
427 prefix_len = subtree->key_sha1[19];
428 assert(prefix_len * 2 >= n);
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
429 memcpy(object_sha1, subtree->key_sha1, prefix_len);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
430 while (tree_entry(&desc, &entry)) {
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
431 path_len = strlen(entry.path);
432 len = get_sha1_hex_segment(entry.path, path_len,
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
433 object_sha1 + prefix_len, 20 - prefix_len);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
434 if (len < 0)
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
435 goto handle_non_note; /* entry.path is not a SHA1 */
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
436 len += prefix_len;
437
438 /*
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
439 * If object SHA1 is complete (len == 20), assume note object
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
440 * If object SHA1 is incomplete (len < 20), and current
441 * component consists of 2 hex chars, assume note subtree
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
442 */
443 if (len <= 20) {
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
444 type = PTR_TYPE_NOTE;
445 l = (struct leaf_node *)
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
446 xcalloc(sizeof(struct leaf_node), 1);
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
447 hashcpy(l->key_sha1, object_sha1);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
448 hashcpy(l->val_sha1, entry.sha1);
449 if (len < 20) {
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
450 if (!S_ISDIR(entry.mode) || path_len != 2)
451 goto handle_non_note; /* not subtree */
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
452 l->key_sha1[19] = (unsigned char) len;
453 type = PTR_TYPE_SUBTREE;
454 }
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
455 if (note_tree_insert(t, node, n, l, type,
456 combine_notes_concatenate))
457 die("Failed to load %s %s into notes tree "
458 "from %s",
459 type == PTR_TYPE_NOTE ? "note" : "subtree",
460 sha1_to_hex(l->key_sha1), t->ref);
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
461 }
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
462 continue;
463
464 handle_non_note:
465 /*
466 * Determine full path for this non-note entry:
467 * The filename is already found in entry.path, but the
468 * directory part of the path must be deduced from the subtree
469 * containing this entry. We assume here that the overall notes
470 * tree follows a strict byte-based progressive fanout
471 * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
472 * e.g. 4/36 fanout). This means that if a non-note is found at
473 * path "dead/beef", the following code will register it as
474 * being found on "de/ad/beef".
475 * On the other hand, if you use such non-obvious non-note
476 * paths in the middle of a notes tree, you deserve what's
477 * coming to you ;). Note that for non-notes that are not
478 * SHA1-like at the top level, there will be no problems.
479 *
480 * To conclude, it is strongly advised to make sure non-notes
481 * have at least one non-hex character in the top-level path
482 * component.
483 */
484 {
485 char non_note_path[PATH_MAX];
486 char *p = non_note_path;
487 const char *q = sha1_to_hex(subtree->key_sha1);
488 int i;
489 for (i = 0; i < prefix_len; i++) {
490 *p++ = *q++;
491 *p++ = *q++;
492 *p++ = '/';
493 }
494 strcpy(p, entry.path);
495 add_non_note(t, non_note_path, entry.mode, entry.sha1);
496 }
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
497 }
498 free(buf);
499 }
500
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
501 /*
502 * Determine optimal on-disk fanout for this part of the notes tree
503 *
504 * Given a (sub)tree and the level in the internal tree structure, determine
505 * whether or not the given existing fanout should be expanded for this
506 * (sub)tree.
507 *
508 * Values of the 'fanout' variable:
509 * - 0: No fanout (all notes are stored directly in the root notes tree)
510 * - 1: 2/38 fanout
511 * - 2: 2/2/36 fanout
512 * - 3: 2/2/2/34 fanout
513 * etc.
514 */
515 static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
516 unsigned char fanout)
517 {
518 /*
519 * The following is a simple heuristic that works well in practice:
520 * For each even-numbered 16-tree level (remember that each on-disk
521 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
522 * entries at that tree level. If all of them are either int_nodes or
523 * subtree entries, then there are likely plenty of notes below this
524 * level, so we return an incremented fanout.
525 */
526 unsigned int i;
527 if ((n % 2) || (n > 2 * fanout))
528 return fanout;
529 for (i = 0; i < 16; i++) {
530 switch (GET_PTR_TYPE(tree->a[i])) {
531 case PTR_TYPE_SUBTREE:
532 case PTR_TYPE_INTERNAL:
533 continue;
534 default:
535 return fanout;
536 }
537 }
538 return fanout + 1;
539 }
540
541 static void construct_path_with_fanout(const unsigned char *sha1,
542 unsigned char fanout, char *path)
543 {
544 unsigned int i = 0, j = 0;
545 const char *hex_sha1 = sha1_to_hex(sha1);
546 assert(fanout < 20);
547 while (fanout) {
548 path[i++] = hex_sha1[j++];
549 path[i++] = hex_sha1[j++];
550 path[i++] = '/';
551 fanout--;
552 }
553 strcpy(path + i, hex_sha1 + j);
554 }
555
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
556 static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
557 unsigned char n, unsigned char fanout, int flags,
558 each_note_fn fn, void *cb_data)
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
559 {
560 unsigned int i;
561 void *p;
562 int ret = 0;
563 struct leaf_node *l;
564 static char path[40 + 19 + 1]; /* hex SHA1 + 19 * '/' + NUL */
565
566 fanout = determine_fanout(tree, n, fanout);
567 for (i = 0; i < 16; i++) {
568 redo:
569 p = tree->a[i];
570 switch (GET_PTR_TYPE(p)) {
571 case PTR_TYPE_INTERNAL:
572 /* recurse into int_node */
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
573 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
574 fanout, flags, fn, cb_data);
575 break;
576 case PTR_TYPE_SUBTREE:
577 l = (struct leaf_node *) CLR_PTR_TYPE(p);
578 /*
579 * Subtree entries in the note tree represent parts of
580 * the note tree that have not yet been explored. There
581 * is a direct relationship between subtree entries at
582 * level 'n' in the tree, and the 'fanout' variable:
583 * Subtree entries at level 'n <= 2 * fanout' should be
584 * preserved, since they correspond exactly to a fanout
585 * directory in the on-disk structure. However, subtree
586 * entries at level 'n > 2 * fanout' should NOT be
587 * preserved, but rather consolidated into the above
588 * notes tree level. We achieve this by unconditionally
589 * unpacking subtree entries that exist below the
590 * threshold level at 'n = 2 * fanout'.
591 */
592 if (n <= 2 * fanout &&
593 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
594 /* invoke callback with subtree */
595 unsigned int path_len =
596 l->key_sha1[19] * 2 + fanout;
597 assert(path_len < 40 + 19);
598 construct_path_with_fanout(l->key_sha1, fanout,
599 path);
600 /* Create trailing slash, if needed */
601 if (path[path_len - 1] != '/')
602 path[path_len++] = '/';
603 path[path_len] = '\0';
604 ret = fn(l->key_sha1, l->val_sha1, path,
605 cb_data);
606 }
607 if (n > fanout * 2 ||
608 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
609 /* unpack subtree and resume traversal */
610 tree->a[i] = NULL;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
611 load_subtree(t, l, tree, n);
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
612 free(l);
613 goto redo;
614 }
615 break;
616 case PTR_TYPE_NOTE:
617 l = (struct leaf_node *) CLR_PTR_TYPE(p);
618 construct_path_with_fanout(l->key_sha1, fanout, path);
619 ret = fn(l->key_sha1, l->val_sha1, path, cb_data);
620 break;
621 }
622 if (ret)
623 return ret;
624 }
625 return 0;
626 }
627
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
628 struct tree_write_stack {
629 struct tree_write_stack *next;
630 struct strbuf buf;
631 char path[2]; /* path to subtree in next, if any */
632 };
633
634 static inline int matches_tree_write_stack(struct tree_write_stack *tws,
635 const char *full_path)
636 {
637 return full_path[0] == tws->path[0] &&
638 full_path[1] == tws->path[1] &&
639 full_path[2] == '/';
640 }
641
642 static void write_tree_entry(struct strbuf *buf, unsigned int mode,
643 const char *path, unsigned int path_len, const
644 unsigned char *sha1)
645 {
c88f0cc @gitster notes: fix malformed tree entry
authored
646 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
647 strbuf_add(buf, sha1, 20);
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
648 }
649
650 static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
651 const char *path)
652 {
653 struct tree_write_stack *n;
654 assert(!tws->next);
655 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
656 n = (struct tree_write_stack *)
657 xmalloc(sizeof(struct tree_write_stack));
658 n->next = NULL;
659 strbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */
660 n->path[0] = n->path[1] = '\0';
661 tws->next = n;
662 tws->path[0] = path[0];
663 tws->path[1] = path[1];
664 }
665
666 static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
667 {
668 int ret;
669 struct tree_write_stack *n = tws->next;
670 unsigned char s[20];
671 if (n) {
672 ret = tree_write_stack_finish_subtree(n);
673 if (ret)
674 return ret;
675 ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);
676 if (ret)
677 return ret;
678 strbuf_release(&n->buf);
679 free(n);
680 tws->next = NULL;
681 write_tree_entry(&tws->buf, 040000, tws->path, 2, s);
682 tws->path[0] = tws->path[1] = '\0';
683 }
684 return 0;
685 }
686
687 static int write_each_note_helper(struct tree_write_stack *tws,
688 const char *path, unsigned int mode,
689 const unsigned char *sha1)
690 {
691 size_t path_len = strlen(path);
692 unsigned int n = 0;
693 int ret;
694
695 /* Determine common part of tree write stack */
696 while (tws && 3 * n < path_len &&
697 matches_tree_write_stack(tws, path + 3 * n)) {
698 n++;
699 tws = tws->next;
700 }
701
702 /* tws point to last matching tree_write_stack entry */
703 ret = tree_write_stack_finish_subtree(tws);
704 if (ret)
705 return ret;
706
707 /* Start subtrees needed to satisfy path */
708 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
709 tree_write_stack_init_subtree(tws, path + 3 * n);
710 n++;
711 tws = tws->next;
712 }
713
714 /* There should be no more directory components in the given path */
715 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
716
717 /* Finally add given entry to the current tree object */
718 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
719 sha1);
720
721 return 0;
722 }
723
724 struct write_each_note_data {
725 struct tree_write_stack *root;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
726 struct non_note *next_non_note;
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
727 };
728
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
729 static int write_each_non_note_until(const char *note_path,
730 struct write_each_note_data *d)
731 {
732 struct non_note *n = d->next_non_note;
89fe121 notes: Initialise variable to appease gcc
Ramsay Jones authored
733 int cmp = 0, ret;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
734 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
735 if (note_path && cmp == 0)
736 ; /* do nothing, prefer note to non-note */
737 else {
738 ret = write_each_note_helper(d->root, n->path, n->mode,
739 n->sha1);
740 if (ret)
741 return ret;
742 }
743 n = n->next;
744 }
745 d->next_non_note = n;
746 return 0;
747 }
748
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
749 static int write_each_note(const unsigned char *object_sha1,
750 const unsigned char *note_sha1, char *note_path,
751 void *cb_data)
752 {
753 struct write_each_note_data *d =
754 (struct write_each_note_data *) cb_data;
755 size_t note_path_len = strlen(note_path);
756 unsigned int mode = 0100644;
757
758 if (note_path[note_path_len - 1] == '/') {
759 /* subtree entry */
760 note_path_len--;
761 note_path[note_path_len] = '\0';
762 mode = 040000;
763 }
764 assert(note_path_len <= 40 + 19);
765
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
766 /* Weave non-note entries into note entries */
767 return write_each_non_note_until(note_path, d) ||
768 write_each_note_helper(d->root, note_path, mode, note_sha1);
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
769 }
770
00fbe63 @jherland Notes API: prune_notes(): Prune notes that belong to non-existing obj…
jherland authored
771 struct note_delete_list {
772 struct note_delete_list *next;
773 const unsigned char *sha1;
774 };
775
776 static int prune_notes_helper(const unsigned char *object_sha1,
777 const unsigned char *note_sha1, char *note_path,
778 void *cb_data)
779 {
780 struct note_delete_list **l = (struct note_delete_list **) cb_data;
781 struct note_delete_list *n;
782
783 if (has_sha1_file(object_sha1))
784 return 0; /* nothing to do for this note */
785
786 /* failed to find object => prune this note */
787 n = (struct note_delete_list *) xmalloc(sizeof(*n));
788 n->next = *l;
789 n->sha1 = object_sha1;
790 *l = n;
791 return 0;
792 }
793
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
794 int combine_notes_concatenate(unsigned char *cur_sha1,
795 const unsigned char *new_sha1)
796 {
797 char *cur_msg = NULL, *new_msg = NULL, *buf;
798 unsigned long cur_len, new_len, buf_len;
799 enum object_type cur_type, new_type;
800 int ret;
801
802 /* read in both note blob objects */
803 if (!is_null_sha1(new_sha1))
804 new_msg = read_sha1_file(new_sha1, &new_type, &new_len);
805 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
806 free(new_msg);
807 return 0;
808 }
809 if (!is_null_sha1(cur_sha1))
810 cur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);
811 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
812 free(cur_msg);
813 free(new_msg);
814 hashcpy(cur_sha1, new_sha1);
815 return 0;
816 }
817
d4990c4 @jherland notes.c: Use two newlines (instead of one) when concatenating notes
jherland authored
818 /* we will separate the notes by two newlines anyway */
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
819 if (cur_msg[cur_len - 1] == '\n')
820 cur_len--;
821
822 /* concatenate cur_msg and new_msg into buf */
d4990c4 @jherland notes.c: Use two newlines (instead of one) when concatenating notes
jherland authored
823 buf_len = cur_len + 2 + new_len;
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
824 buf = (char *) xmalloc(buf_len);
825 memcpy(buf, cur_msg, cur_len);
826 buf[cur_len] = '\n';
d4990c4 @jherland notes.c: Use two newlines (instead of one) when concatenating notes
jherland authored
827 buf[cur_len + 1] = '\n';
828 memcpy(buf + cur_len + 2, new_msg, new_len);
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
829 free(cur_msg);
830 free(new_msg);
831
832 /* create a new blob object from buf */
833 ret = write_sha1_file(buf, buf_len, blob_type, cur_sha1);
834 free(buf);
835 return ret;
836 }
837
838 int combine_notes_overwrite(unsigned char *cur_sha1,
839 const unsigned char *new_sha1)
840 {
841 hashcpy(cur_sha1, new_sha1);
842 return 0;
843 }
844
845 int combine_notes_ignore(unsigned char *cur_sha1,
846 const unsigned char *new_sha1)
847 {
848 return 0;
849 }
850
a6a0909 @jherland git notes merge: Add another auto-resolving strategy: "cat_sort_uniq"
jherland authored
851 static int string_list_add_note_lines(struct string_list *sort_uniq_list,
852 const unsigned char *sha1)
853 {
854 char *data;
855 unsigned long len;
856 enum object_type t;
857 struct strbuf buf = STRBUF_INIT;
858 struct strbuf **lines = NULL;
859 int i, list_index;
860
861 if (is_null_sha1(sha1))
862 return 0;
863
864 /* read_sha1_file NUL-terminates */
865 data = read_sha1_file(sha1, &t, &len);
866 if (t != OBJ_BLOB || !data || !len) {
867 free(data);
868 return t != OBJ_BLOB || !data;
869 }
870
871 strbuf_attach(&buf, data, len, len + 1);
872 lines = strbuf_split(&buf, '\n');
873
874 for (i = 0; lines[i]; i++) {
875 if (lines[i]->buf[lines[i]->len - 1] == '\n')
876 strbuf_setlen(lines[i], lines[i]->len - 1);
877 if (!lines[i]->len)
878 continue; /* skip empty lines */
879 list_index = string_list_find_insert_index(sort_uniq_list,
880 lines[i]->buf, 0);
881 if (list_index < 0)
882 continue; /* skip duplicate lines */
883 string_list_insert_at_index(sort_uniq_list, list_index,
884 lines[i]->buf);
885 }
886
887 strbuf_list_free(lines);
888 strbuf_release(&buf);
889 return 0;
890 }
891
892 static int string_list_join_lines_helper(struct string_list_item *item,
893 void *cb_data)
894 {
895 struct strbuf *buf = cb_data;
896 strbuf_addstr(buf, item->string);
897 strbuf_addch(buf, '\n');
898 return 0;
899 }
900
901 int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,
902 const unsigned char *new_sha1)
903 {
904 struct string_list sort_uniq_list = { NULL, 0, 0, 1 };
905 struct strbuf buf = STRBUF_INIT;
906 int ret = 1;
907
908 /* read both note blob objects into unique_lines */
909 if (string_list_add_note_lines(&sort_uniq_list, cur_sha1))
910 goto out;
911 if (string_list_add_note_lines(&sort_uniq_list, new_sha1))
912 goto out;
913
914 /* create a new blob object from sort_uniq_list */
915 if (for_each_string_list(&sort_uniq_list,
916 string_list_join_lines_helper, &buf))
917 goto out;
918
919 ret = write_sha1_file(buf.buf, buf.len, blob_type, cur_sha1);
920
921 out:
922 strbuf_release(&buf);
923 string_list_clear(&sort_uniq_list, 0);
924 return ret;
925 }
926
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
927 static int string_list_add_one_ref(const char *path, const unsigned char *sha1,
928 int flag, void *cb)
929 {
930 struct string_list *refs = cb;
931 if (!unsorted_string_list_has_string(refs, path))
1d2f80f @qur string_list: Fix argument order for string_list_append
qur authored
932 string_list_append(refs, path);
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
933 return 0;
934 }
935
936 void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
937 {
938 if (has_glob_specials(glob)) {
939 for_each_glob_ref(string_list_add_one_ref, glob, list);
940 } else {
941 unsigned char sha1[20];
942 if (get_sha1(glob, sha1))
943 warning("notes ref %s is invalid", glob);
944 if (!unsorted_string_list_has_string(list, glob))
1d2f80f @qur string_list: Fix argument order for string_list_append
qur authored
945 string_list_append(list, glob);
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
946 }
947 }
948
949 void string_list_add_refs_from_colon_sep(struct string_list *list,
950 const char *globs)
951 {
952 struct strbuf globbuf = STRBUF_INIT;
953 struct strbuf **split;
954 int i;
955
956 strbuf_addstr(&globbuf, globs);
957 split = strbuf_split(&globbuf, ':');
958
959 for (i = 0; split[i]; i++) {
960 if (!split[i]->len)
961 continue;
962 if (split[i]->buf[split[i]->len-1] == ':')
963 strbuf_setlen(split[i], split[i]->len-1);
964 string_list_add_refs_by_glob(list, split[i]->buf);
965 }
966
967 strbuf_list_free(split);
968 strbuf_release(&globbuf);
969 }
970
971 static int notes_display_config(const char *k, const char *v, void *cb)
972 {
973 int *load_refs = cb;
974
975 if (*load_refs && !strcmp(k, "notes.displayref")) {
976 if (!v)
977 config_error_nonbool(k);
978 string_list_add_refs_by_glob(&display_notes_refs, v);
979 }
980
981 return 0;
982 }
983
4a9cf1c @jherland notes.h: Make default_notes_ref() available in notes API
jherland authored
984 const char *default_notes_ref(void)
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
985 {
986 const char *notes_ref = NULL;
987 if (!notes_ref)
988 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
989 if (!notes_ref)
990 notes_ref = notes_ref_name; /* value of core.notesRef config */
991 if (!notes_ref)
992 notes_ref = GIT_NOTES_DEFAULT_REF;
993 return notes_ref;
994 }
995
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
996 void init_notes(struct notes_tree *t, const char *notes_ref,
997 combine_notes_fn combine_notes, int flags)
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
998 {
a7e7eff @jherland Notes API: get_commit_notes() -> format_note() + remove the commit re…
jherland authored
999 unsigned char sha1[20], object_sha1[20];
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
1000 unsigned mode;
1001 struct leaf_node root_tree;
fd53c9e @dscho Speed up git notes lookup
dscho authored
1002
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1003 if (!t)
1004 t = &default_notes_tree;
1005 assert(!t->initialized);
709f79b @jherland Notes API: init_notes(): Initialize the notes tree from the given not…
jherland authored
1006
1007 if (!notes_ref)
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1008 notes_ref = default_notes_ref();
709f79b @jherland Notes API: init_notes(): Initialize the notes tree from the given not…
jherland authored
1009
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
1010 if (!combine_notes)
1011 combine_notes = combine_notes_concatenate;
1012
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1013 t->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1014 t->first_non_note = NULL;
1015 t->prev_non_note = NULL;
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1016 t->ref = notes_ref ? xstrdup(notes_ref) : NULL;
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
1017 t->combine_notes = combine_notes;
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1018 t->initialized = 1;
7f710ea @trast notes: track whether notes_trees were changed at all
trast authored
1019 t->dirty = 0;
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1020
709f79b @jherland Notes API: init_notes(): Initialize the notes tree from the given not…
jherland authored
1021 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
1022 read_ref(notes_ref, object_sha1))
fd53c9e @dscho Speed up git notes lookup
dscho authored
1023 return;
709f79b @jherland Notes API: init_notes(): Initialize the notes tree from the given not…
jherland authored
1024 if (get_tree_entry(object_sha1, "", sha1, &mode))
1025 die("Failed to read notes tree referenced by %s (%s)",
327a89d @jherland notes.c: Hexify SHA1 in die() message from init_notes()
jherland authored
1026 notes_ref, sha1_to_hex(object_sha1));
fd53c9e @dscho Speed up git notes lookup
dscho authored
1027
23123ae @jherland Teach the notes lookup code to parse notes trees with various fanout …
jherland authored
1028 hashclr(root_tree.key_sha1);
1029 hashcpy(root_tree.val_sha1, sha1);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1030 load_subtree(t, &root_tree, t->root, 0);
fd53c9e @dscho Speed up git notes lookup
dscho authored
1031 }
1032
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1033 struct notes_tree **load_notes_trees(struct string_list *refs)
1034 {
8a57c6e @raalkml Convert the users of for_each_string_list to for_each_string_list_ite…
raalkml authored
1035 struct string_list_item *item;
1036 int counter = 0;
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1037 struct notes_tree **trees;
1038 trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
8a57c6e @raalkml Convert the users of for_each_string_list to for_each_string_list_ite…
raalkml authored
1039 for_each_string_list_item(item, refs) {
1040 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
1041 init_notes(t, item->string, combine_notes_ignore, 0);
1042 trees[counter++] = t;
1043 }
1044 trees[counter] = NULL;
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1045 return trees;
1046 }
1047
1048 void init_display_notes(struct display_notes_opt *opt)
1049 {
1050 char *display_ref_env;
1051 int load_config_refs = 0;
1052 display_notes_refs.strdup_strings = 1;
1053
1054 assert(!display_notes_trees);
1055
3a03cf6 @peff notes: refactor display notes default handling
peff authored
1056 if (!opt || opt->use_default_notes > 0 ||
1057 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1d2f80f @qur string_list: Fix argument order for string_list_append
qur authored
1058 string_list_append(&display_notes_refs, default_notes_ref());
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1059 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1060 if (display_ref_env) {
1061 string_list_add_refs_from_colon_sep(&display_notes_refs,
1062 display_ref_env);
1063 load_config_refs = 0;
1064 } else
1065 load_config_refs = 1;
1066 }
1067
1068 git_config(notes_display_config, &load_config_refs);
1069
304cc11 @peff notes: refactor display notes extra refs field
peff authored
1070 if (opt) {
8a57c6e @raalkml Convert the users of for_each_string_list to for_each_string_list_ite…
raalkml authored
1071 struct string_list_item *item;
304cc11 @peff notes: refactor display notes extra refs field
peff authored
1072 for_each_string_list_item(item, &opt->extra_notes_refs)
8a57c6e @raalkml Convert the users of for_each_string_list to for_each_string_list_ite…
raalkml authored
1073 string_list_add_refs_by_glob(&display_notes_refs,
1074 item->string);
1075 }
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1076
1077 display_notes_trees = load_notes_trees(&display_notes_refs);
1078 string_list_clear(&display_notes_refs, 0);
1079 }
1080
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
1081 int add_note(struct notes_tree *t, const unsigned char *object_sha1,
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
1082 const unsigned char *note_sha1, combine_notes_fn combine_notes)
2626b53 @jherland Notes API: add_note(): Add note objects to the internal notes tree st…
jherland authored
1083 {
1084 struct leaf_node *l;
1085
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1086 if (!t)
1087 t = &default_notes_tree;
1088 assert(t->initialized);
7f710ea @trast notes: track whether notes_trees were changed at all
trast authored
1089 t->dirty = 1;
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
1090 if (!combine_notes)
1091 combine_notes = t->combine_notes;
2626b53 @jherland Notes API: add_note(): Add note objects to the internal notes tree st…
jherland authored
1092 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
1093 hashcpy(l->key_sha1, object_sha1);
1094 hashcpy(l->val_sha1, note_sha1);
180619a @jherland notes.h/c: Propagate combine_notes_fn return value to add_note() and …
jherland authored
1095 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
2626b53 @jherland Notes API: add_note(): Add note objects to the internal notes tree st…
jherland authored
1096 }
1097
1ee1e43 @jherland notes: Don't create (empty) commit when removing non-existing notes
jherland authored
1098 int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
1099 {
1100 struct leaf_node l;
1101
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1102 if (!t)
1103 t = &default_notes_tree;
1104 assert(t->initialized);
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
1105 hashcpy(l.key_sha1, object_sha1);
1106 hashclr(l.val_sha1);
a502ab9 @drafnel notes.c: remove inappropriate call to return
drafnel authored
1107 note_tree_remove(t, t->root, 0, &l);
885b797 @mhagger Do not use C++-style comments
mhagger authored
1108 if (is_null_sha1(l.val_sha1)) /* no note was removed */
1ee1e43 @jherland notes: Don't create (empty) commit when removing non-existing notes
jherland authored
1109 return 1;
1110 t->dirty = 1;
1111 return 0;
1ec666b @jherland Notes API: remove_note(): Remove note objects from the notes tree str…
jherland authored
1112 }
1113
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1114 const unsigned char *get_note(struct notes_tree *t,
1115 const unsigned char *object_sha1)
fd53c9e @dscho Speed up git notes lookup
dscho authored
1116 {
9b391f2 @jherland Notes API: get_note(): Return the note annotating the given object
jherland authored
1117 struct leaf_node *found;
1118
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1119 if (!t)
1120 t = &default_notes_tree;
1121 assert(t->initialized);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1122 found = note_tree_find(t, t->root, 0, object_sha1);
9b391f2 @jherland Notes API: get_note(): Return the note annotating the given object
jherland authored
1123 return found ? found->val_sha1 : NULL;
fd53c9e @dscho Speed up git notes lookup
dscho authored
1124 }
a97a746 @dscho Introduce commit notes
dscho authored
1125
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1126 int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1127 void *cb_data)
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
1128 {
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1129 if (!t)
1130 t = &default_notes_tree;
1131 assert(t->initialized);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1132 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
73f77b9 @jherland Notes API: for_each_note(): Traverse the entire notes tree with a cal…
jherland authored
1133 }
1134
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1135 int write_notes_tree(struct notes_tree *t, unsigned char *result)
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
1136 {
1137 struct tree_write_stack root;
1138 struct write_each_note_data cb_data;
1139 int ret;
1140
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1141 if (!t)
1142 t = &default_notes_tree;
1143 assert(t->initialized);
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
1144
1145 /* Prepare for traversal of current notes tree */
1146 root.next = NULL; /* last forward entry in list is grounded */
1147 strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
1148 root.path[0] = root.path[1] = '\0';
1149 cb_data.root = &root;
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1150 cb_data.next_non_note = t->first_non_note;
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
1151
1152 /* Write tree objects representing current notes tree */
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1153 ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
1154 FOR_EACH_NOTE_YIELD_SUBTREES,
1155 write_each_note, &cb_data) ||
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1156 write_each_non_note_until(NULL, &cb_data) ||
61a7cca @jherland Notes API: write_notes_tree(): Store the notes tree in the database
jherland authored
1157 tree_write_stack_finish_subtree(&root) ||
1158 write_sha1_file(root.buf.buf, root.buf.len, tree_type, result);
1159 strbuf_release(&root.buf);
1160 return ret;
1161 }
1162
a9f2adf @mjg notes: dry-run and verbose options for prune
mjg authored
1163 void prune_notes(struct notes_tree *t, int flags)
00fbe63 @jherland Notes API: prune_notes(): Prune notes that belong to non-existing obj…
jherland authored
1164 {
1165 struct note_delete_list *l = NULL;
1166
1167 if (!t)
1168 t = &default_notes_tree;
1169 assert(t->initialized);
1170
1171 for_each_note(t, 0, prune_notes_helper, &l);
1172
1173 while (l) {
a9f2adf @mjg notes: dry-run and verbose options for prune
mjg authored
1174 if (flags & NOTES_PRUNE_VERBOSE)
1175 printf("%s\n", sha1_to_hex(l->sha1));
1176 if (!(flags & NOTES_PRUNE_DRYRUN))
1177 remove_note(t, l->sha1);
00fbe63 @jherland Notes API: prune_notes(): Prune notes that belong to non-existing obj…
jherland authored
1178 l = l->next;
1179 }
1180 }
1181
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1182 void free_notes(struct notes_tree *t)
27d5756 @jherland Teach notes code to free its internal data structures on request
jherland authored
1183 {
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1184 if (!t)
1185 t = &default_notes_tree;
1186 if (t->root)
1187 note_tree_free(t->root);
1188 free(t->root);
851c2b3 @jherland Teach notes code to properly preserve non-notes in the notes tree
jherland authored
1189 while (t->first_non_note) {
1190 t->prev_non_note = t->first_non_note->next;
1191 free(t->first_non_note->path);
1192 free(t->first_non_note);
1193 t->first_non_note = t->prev_non_note;
1194 }
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1195 free(t->ref);
1196 memset(t, 0, sizeof(struct notes_tree));
27d5756 @jherland Teach notes code to free its internal data structures on request
jherland authored
1197 }
1198
96531a5 @gitster notes.c: mark a private file-scope symbol as static
authored
1199 /*
1200 * Fill the given strbuf with the notes associated with the given object.
1201 *
1202 * If the given notes_tree structure is not initialized, it will be auto-
1203 * initialized to the default value (see documentation for init_notes() above).
1204 * If the given notes_tree is NULL, the internal/default notes_tree will be
1205 * used instead.
1206 *
1207 * 'flags' is a bitwise combination of the flags for format_display_notes.
1208 */
1209 static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
1210 struct strbuf *sb, const char *output_encoding, int flags)
a97a746 @dscho Introduce commit notes
dscho authored
1211 {
1212 static const char utf8[] = "utf-8";
9b391f2 @jherland Notes API: get_note(): Return the note annotating the given object
jherland authored
1213 const unsigned char *sha1;
a97a746 @dscho Introduce commit notes
dscho authored
1214 char *msg, *msg_p;
1215 unsigned long linelen, msglen;
1216 enum object_type type;
1217
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1218 if (!t)
1219 t = &default_notes_tree;
1220 if (!t->initialized)
73f464b @jherland Refactor notes concatenation into a flexible interface for combining …
jherland authored
1221 init_notes(t, NULL, NULL, 0);
a97a746 @dscho Introduce commit notes
dscho authored
1222
cd30539 @jherland Notes API: Allow multiple concurrent notes trees with new struct note…
jherland authored
1223 sha1 = get_note(t, object_sha1);
fd53c9e @dscho Speed up git notes lookup
dscho authored
1224 if (!sha1)
a97a746 @dscho Introduce commit notes
dscho authored
1225 return;
1226
1227 if (!(msg = read_sha1_file(sha1, &type, &msglen)) || !msglen ||
1228 type != OBJ_BLOB) {
1229 free(msg);
1230 return;
1231 }
1232
1233 if (output_encoding && *output_encoding &&
1234 strcmp(utf8, output_encoding)) {
1235 char *reencoded = reencode_string(msg, output_encoding, utf8);
1236 if (reencoded) {
1237 free(msg);
1238 msg = reencoded;
1239 msglen = strlen(msg);
1240 }
1241 }
1242
1243 /* we will end the annotation by a newline anyway */
1244 if (msglen && msg[msglen - 1] == '\n')
1245 msglen--;
1246
894a9d3 @trast Support showing notes from more than one notes tree
trast authored
1247 if (flags & NOTES_SHOW_HEADER) {
1248 const char *ref = t->ref;
1249 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1250 strbuf_addstr(sb, "\nNotes:\n");
1251 } else {
1252 if (!prefixcmp(ref, "refs/"))
1253 ref += 5;
1254 if (!prefixcmp(ref, "notes/"))
1255 ref += 6;
1256 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1257 }
1258 }
a97a746 @dscho Introduce commit notes
dscho authored
1259
1260 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1261 linelen = strchrnul(msg_p, '\n') - msg_p;
1262
c56fcc8 @jherland Add flags to get_commit_notes() to control the format of the note string
jherland authored
1263 if (flags & NOTES_INDENT)
1264 strbuf_addstr(sb, " ");
a97a746 @dscho