blob: fed1eda80cd7e41a2e21af26aed81e1dd472b27c [file] [log] [blame]
Elijah Newrene93fc5d2023-04-11 00:41:50 -07001#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00003#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02005#include "notes.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07006#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +00007#include "object-store-ll.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02008#include "utf8.h"
9#include "strbuf.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020010#include "tree-walk.h"
Thomas Rast894a9d32010-03-12 18:04:26 +010011#include "string-list.h"
12#include "refs.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020013
Johan Herland23123ae2009-10-09 12:22:07 +020014/*
15 * Use a non-balancing simple 16-tree structure with struct int_node as
16 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
17 * 16-array of pointers to its children.
18 * The bottom 2 bits of each pointer is used to identify the pointer type
19 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
20 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
21 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
22 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
23 *
24 * The root node is a statically allocated struct int_node.
25 */
26struct int_node {
27 void *a[16];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020028};
29
Johan Herland23123ae2009-10-09 12:22:07 +020030/*
31 * Leaf nodes come in two variants, note entries and subtree entries,
32 * distinguished by the LSb of the leaf node pointer (see above).
Johan Herlanda7e7eff2010-02-13 22:28:10 +010033 * As a note entry, the key is the SHA1 of the referenced object, and the
Johan Herland23123ae2009-10-09 12:22:07 +020034 * value is the SHA1 of the note object.
35 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
Johan Herlanda7e7eff2010-02-13 22:28:10 +010036 * referenced object, using the last byte of the key to store the length of
Johan Herland23123ae2009-10-09 12:22:07 +020037 * the prefix. The value is the SHA1 of the tree object containing the notes
38 * subtree.
39 */
40struct leaf_node {
brian m. carlson5dcc9692017-05-30 10:30:37 -070041 struct object_id key_oid;
42 struct object_id val_oid;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020043};
Johannes Schindelina97a7462009-10-09 12:21:57 +020044
Johan Herland851c2b32010-02-13 22:28:23 +010045/*
46 * A notes tree may contain entries that are not notes, and that do not follow
47 * the naming conventions of notes. There are typically none/few of these, but
48 * we still need to keep track of them. Keep a simple linked list sorted alpha-
49 * betically on the non-note path. The list is populated when parsing tree
50 * objects in load_subtree(), and the non-notes are correctly written back into
51 * the tree objects produced by write_notes_tree().
52 */
53struct non_note {
54 struct non_note *next; /* grounded (last->next == NULL) */
55 char *path;
56 unsigned int mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -070057 struct object_id oid;
Johan Herland851c2b32010-02-13 22:28:23 +010058};
59
Johan Herland23123ae2009-10-09 12:22:07 +020060#define PTR_TYPE_NULL 0
61#define PTR_TYPE_INTERNAL 1
62#define PTR_TYPE_NOTE 2
63#define PTR_TYPE_SUBTREE 3
64
65#define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
66#define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
67#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
68
Michael Haggerty65eb8e02017-08-26 10:28:01 +020069#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
Johan Herland23123ae2009-10-09 12:22:07 +020070
brian m. carlsondd437452019-02-19 00:05:01 +000071#define KEY_INDEX (the_hash_algo->rawsz - 1)
72#define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
73#define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
Johan Herland23123ae2009-10-09 12:22:07 +020074#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
brian m. carlson89c149f2017-05-30 10:30:38 -070075 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
Johan Herland23123ae2009-10-09 12:22:07 +020076
Johan Herlandcd305392010-02-13 22:28:18 +010077struct notes_tree default_notes_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020078
Jeff King2721ce22016-06-13 06:04:20 -040079static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
Thomas Rast894a9d32010-03-12 18:04:26 +010080static struct notes_tree **display_notes_trees;
81
Johan Herland851c2b32010-02-13 22:28:23 +010082static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
83 struct int_node *node, unsigned int n);
Johan Herland23123ae2009-10-09 12:22:07 +020084
85/*
Johan Herlandef8db632009-10-09 12:22:09 +020086 * Search the tree until the appropriate location for the given key is found:
Johan Herland23123ae2009-10-09 12:22:07 +020087 * 1. Start at the root node, with n = 0
Johan Herlandef8db632009-10-09 12:22:09 +020088 * 2. If a[0] at the current level is a matching subtree entry, unpack that
89 * subtree entry and remove it; restart search at the current level.
90 * 3. Use the nth nibble of the key as an index into a:
91 * - If a[n] is an int_node, recurse from #2 into that node and increment n
Johan Herland23123ae2009-10-09 12:22:07 +020092 * - If a matching subtree entry, unpack that subtree entry (and remove it);
93 * restart search at the current level.
Johan Herlandef8db632009-10-09 12:22:09 +020094 * - Otherwise, we have found one of the following:
95 * - a subtree entry which does not match the key
96 * - a note entry which may or may not match the key
97 * - an unused leaf node (NULL)
98 * In any case, set *tree and *n, and return pointer to the tree location.
Johan Herland23123ae2009-10-09 12:22:07 +020099 */
Johan Herland851c2b32010-02-13 22:28:23 +0100100static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
Johan Herlandef8db632009-10-09 12:22:09 +0200101 unsigned char *n, const unsigned char *key_sha1)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200102{
Johan Herland23123ae2009-10-09 12:22:07 +0200103 struct leaf_node *l;
Johan Herlandef8db632009-10-09 12:22:09 +0200104 unsigned char i;
105 void *p = (*tree)->a[0];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200106
Johan Herlandef8db632009-10-09 12:22:09 +0200107 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
108 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700109 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herlandef8db632009-10-09 12:22:09 +0200110 /* unpack tree and resume search */
111 (*tree)->a[0] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100112 load_subtree(t, l, *tree, *n);
Johan Herlandef8db632009-10-09 12:22:09 +0200113 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100114 return note_tree_search(t, tree, n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200115 }
116 }
117
118 i = GET_NIBBLE(*n, key_sha1);
119 p = (*tree)->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100120 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200121 case PTR_TYPE_INTERNAL:
Johan Herlandef8db632009-10-09 12:22:09 +0200122 *tree = CLR_PTR_TYPE(p);
123 (*n)++;
Johan Herland851c2b32010-02-13 22:28:23 +0100124 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200125 case PTR_TYPE_SUBTREE:
126 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700127 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200128 /* unpack tree and resume search */
Johan Herlandef8db632009-10-09 12:22:09 +0200129 (*tree)->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100130 load_subtree(t, l, *tree, *n);
Johan Herland23123ae2009-10-09 12:22:07 +0200131 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100132 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200133 }
Johan Herlandef8db632009-10-09 12:22:09 +0200134 /* fall through */
Johan Herland23123ae2009-10-09 12:22:07 +0200135 default:
Johan Herlandef8db632009-10-09 12:22:09 +0200136 return &((*tree)->a[i]);
Johan Herland23123ae2009-10-09 12:22:07 +0200137 }
Johan Herlandef8db632009-10-09 12:22:09 +0200138}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200139
Johan Herlandef8db632009-10-09 12:22:09 +0200140/*
141 * To find a leaf_node:
142 * Search to the tree location appropriate for the given key:
143 * If a note entry with matching key, return the note entry, else return NULL.
144 */
Johan Herland851c2b32010-02-13 22:28:23 +0100145static struct leaf_node *note_tree_find(struct notes_tree *t,
146 struct int_node *tree, unsigned char n,
Johan Herlandef8db632009-10-09 12:22:09 +0200147 const unsigned char *key_sha1)
148{
Johan Herland851c2b32010-02-13 22:28:23 +0100149 void **p = note_tree_search(t, &tree, &n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200150 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
151 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff Kinge3ff0682018-08-28 17:22:44 -0400152 if (hasheq(key_sha1, l->key_oid.hash))
Johan Herlandef8db632009-10-09 12:22:09 +0200153 return l;
Johan Herland23123ae2009-10-09 12:22:07 +0200154 }
155 return NULL;
156}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200157
Johan Herland23123ae2009-10-09 12:22:07 +0200158/*
Johan Herland1ec666b2010-02-13 22:28:14 +0100159 * How to consolidate an int_node:
160 * If there are > 1 non-NULL entries, give up and return non-zero.
161 * Otherwise replace the int_node at the given index in the given parent node
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900162 * with the only NOTE entry (or a NULL entry if no entries) from the given
163 * tree, and return 0.
Johan Herland1ec666b2010-02-13 22:28:14 +0100164 */
165static int note_tree_consolidate(struct int_node *tree,
166 struct int_node *parent, unsigned char index)
167{
168 unsigned int i;
169 void *p = NULL;
170
171 assert(tree && parent);
172 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
173
174 for (i = 0; i < 16; i++) {
175 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
176 if (p) /* more than one entry */
177 return -2;
178 p = tree->a[i];
179 }
180 }
181
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900182 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
183 return -2;
Johan Herland1ec666b2010-02-13 22:28:14 +0100184 /* replace tree with p in parent[index] */
185 parent->a[index] = p;
186 free(tree);
187 return 0;
188}
189
190/*
191 * To remove a leaf_node:
192 * Search to the tree location appropriate for the given leaf_node's key:
193 * - If location does not hold a matching entry, abort and do nothing.
Johan Herland1ee1e432010-08-31 17:56:50 +0200194 * - Copy the matching entry's value into the given entry.
Johan Herland1ec666b2010-02-13 22:28:14 +0100195 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
196 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
197 */
Johan Herland1ee1e432010-08-31 17:56:50 +0200198static void note_tree_remove(struct notes_tree *t,
199 struct int_node *tree, unsigned char n,
200 struct leaf_node *entry)
Johan Herland1ec666b2010-02-13 22:28:14 +0100201{
202 struct leaf_node *l;
brian m. carlsondd437452019-02-19 00:05:01 +0000203 struct int_node *parent_stack[GIT_MAX_RAWSZ];
Johan Herland1ec666b2010-02-13 22:28:14 +0100204 unsigned char i, j;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700205 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100206
207 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
208 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
209 return; /* type mismatch, nothing to remove */
210 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff King9001dc22018-08-28 17:22:48 -0400211 if (!oideq(&l->key_oid, &entry->key_oid))
Johan Herland1ec666b2010-02-13 22:28:14 +0100212 return; /* key mismatch, nothing to remove */
213
214 /* we have found a matching entry */
brian m. carlson5dcc9692017-05-30 10:30:37 -0700215 oidcpy(&entry->val_oid, &l->val_oid);
Johan Herland1ec666b2010-02-13 22:28:14 +0100216 free(l);
217 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
218
219 /* consolidate this tree level, and parent levels, if possible */
220 if (!n)
221 return; /* cannot consolidate top level */
222 /* first, build stack of ancestors between root and current node */
Johan Herlandcd305392010-02-13 22:28:18 +0100223 parent_stack[0] = t->root;
Johan Herland1ec666b2010-02-13 22:28:14 +0100224 for (i = 0; i < n; i++) {
brian m. carlson5dcc9692017-05-30 10:30:37 -0700225 j = GET_NIBBLE(i, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100226 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
227 }
228 assert(i == n && parent_stack[i] == tree);
229 /* next, unwind stack until note_tree_consolidate() is done */
230 while (i > 0 &&
231 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
brian m. carlson5dcc9692017-05-30 10:30:37 -0700232 GET_NIBBLE(i - 1, entry->key_oid.hash)))
Johan Herland1ec666b2010-02-13 22:28:14 +0100233 i--;
234}
235
Johan Herlanda5cdebe2010-11-09 22:49:40 +0100236/*
Johannes Schindelina97a7462009-10-09 12:21:57 +0200237 * To insert a leaf_node:
238 * Search to the tree location appropriate for the given leaf_node's key:
239 * - If location is unused (NULL), store the tweaked pointer directly there
240 * - If location holds a note entry that matches the note-to-be-inserted, then
241 * combine the two notes (by calling the given combine_notes function).
242 * - If location holds a note entry that matches the subtree-to-be-inserted,
243 * then unpack the subtree-to-be-inserted into the location.
244 * - If location holds a matching subtree entry, unpack the subtree at that
245 * location, and restart the insert operation from that level.
246 * - Else, create a new int_node, holding both the node-at-location and the
247 * node-to-be-inserted, and store the new int_node into the location.
248 */
Johan Herland180619a2010-11-15 00:52:26 +0100249static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
Johannes Schindelina97a7462009-10-09 12:21:57 +0200250 unsigned char n, struct leaf_node *entry, unsigned char type,
251 combine_notes_fn combine_notes)
252{
253 struct int_node *new_node;
254 struct leaf_node *l;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700255 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland180619a2010-11-15 00:52:26 +0100256 int ret = 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200257
258 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
259 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
260 switch (GET_PTR_TYPE(*p)) {
261 case PTR_TYPE_NULL:
262 assert(!*p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700263 if (is_null_oid(&entry->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100264 free(entry);
265 else
266 *p = SET_PTR_TYPE(entry, type);
Johan Herland180619a2010-11-15 00:52:26 +0100267 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200268 case PTR_TYPE_NOTE:
269 switch (type) {
270 case PTR_TYPE_NOTE:
Jeff King4a7e27e2018-08-28 17:22:40 -0400271 if (oideq(&l->key_oid, &entry->key_oid)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200272 /* skip concatenation if l == entry */
Mike Hommey779ad662019-08-25 14:18:18 +0900273 if (oideq(&l->val_oid, &entry->val_oid)) {
274 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100275 return 0;
Mike Hommey779ad662019-08-25 14:18:18 +0900276 }
Johannes Schindelina97a7462009-10-09 12:21:57 +0200277
Patryk Obarab7d591d2018-01-28 01:13:17 +0100278 ret = combine_notes(&l->val_oid,
279 &entry->val_oid);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700280 if (!ret && is_null_oid(&l->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100281 note_tree_remove(t, tree, n, entry);
Johannes Schindelina97a7462009-10-09 12:21:57 +0200282 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100283 return ret;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200284 }
285 break;
286 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700287 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
288 entry->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200289 /* unpack 'entry' */
290 load_subtree(t, entry, tree, n);
291 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100292 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200293 }
294 break;
295 }
296 break;
297 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700298 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200299 /* unpack 'l' and restart insert */
300 *p = NULL;
301 load_subtree(t, l, tree, n);
302 free(l);
Johan Herland180619a2010-11-15 00:52:26 +0100303 return note_tree_insert(t, tree, n, entry, type,
304 combine_notes);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200305 }
306 break;
307 }
308
309 /* non-matching leaf_node */
310 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
311 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700312 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
Johan Herlande2656c82010-11-09 22:49:41 +0100313 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100314 return 0;
Johan Herlande2656c82010-11-09 22:49:41 +0100315 }
Brian Gesiak65bbf082014-05-27 00:33:52 +0900316 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland180619a2010-11-15 00:52:26 +0100317 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
318 combine_notes);
319 if (ret)
320 return ret;
Johan Herland23123ae2009-10-09 12:22:07 +0200321 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
Johan Herland180619a2010-11-15 00:52:26 +0100322 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
Johan Herlandef8db632009-10-09 12:22:09 +0200323}
324
Johan Herland23123ae2009-10-09 12:22:07 +0200325/* Free the entire notes data contained in the given tree */
326static void note_tree_free(struct int_node *tree)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200327{
Johan Herland23123ae2009-10-09 12:22:07 +0200328 unsigned int i;
329 for (i = 0; i < 16; i++) {
330 void *p = tree->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100331 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200332 case PTR_TYPE_INTERNAL:
333 note_tree_free(CLR_PTR_TYPE(p));
334 /* fall through */
335 case PTR_TYPE_NOTE:
336 case PTR_TYPE_SUBTREE:
337 free(CLR_PTR_TYPE(p));
338 }
339 }
340}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200341
Johan Herland851c2b32010-02-13 22:28:23 +0100342static int non_note_cmp(const struct non_note *a, const struct non_note *b)
343{
344 return strcmp(a->path, b->path);
345}
346
Jeff Kingc29edfe2015-08-19 14:12:41 -0400347/* note: takes ownership of path string */
348static void add_non_note(struct notes_tree *t, char *path,
Johan Herland851c2b32010-02-13 22:28:23 +0100349 unsigned int mode, const unsigned char *sha1)
350{
351 struct non_note *p = t->prev_non_note, *n;
352 n = (struct non_note *) xmalloc(sizeof(struct non_note));
353 n->next = NULL;
Jeff Kingc29edfe2015-08-19 14:12:41 -0400354 n->path = path;
Johan Herland851c2b32010-02-13 22:28:23 +0100355 n->mode = mode;
brian m. carlson92e2cab2021-04-26 01:02:50 +0000356 oidread(&n->oid, sha1);
Johan Herland851c2b32010-02-13 22:28:23 +0100357 t->prev_non_note = n;
358
359 if (!t->first_non_note) {
360 t->first_non_note = n;
361 return;
362 }
363
364 if (non_note_cmp(p, n) < 0)
365 ; /* do nothing */
366 else if (non_note_cmp(t->first_non_note, n) <= 0)
367 p = t->first_non_note;
368 else {
369 /* n sorts before t->first_non_note */
370 n->next = t->first_non_note;
371 t->first_non_note = n;
372 return;
373 }
374
375 /* n sorts equal or after p */
376 while (p->next && non_note_cmp(p->next, n) <= 0)
377 p = p->next;
378
379 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
380 assert(strcmp(p->path, n->path) == 0);
381 p->mode = n->mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700382 oidcpy(&p->oid, &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100383 free(n);
384 t->prev_non_note = p;
385 return;
386 }
387
388 /* n sorts between p and p->next */
389 n->next = p->next;
390 p->next = n;
391}
392
393static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
394 struct int_node *node, unsigned int n)
Johan Herland23123ae2009-10-09 12:22:07 +0200395{
brian m. carlson89c149f2017-05-30 10:30:38 -0700396 struct object_id object_oid;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200397 size_t prefix_len;
Johan Herland23123ae2009-10-09 12:22:07 +0200398 void *buf;
399 struct tree_desc desc;
400 struct name_entry entry;
brian m. carlsondd437452019-02-19 00:05:01 +0000401 const unsigned hashsz = the_hash_algo->rawsz;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200402
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700403 buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
Johan Herland23123ae2009-10-09 12:22:07 +0200404 if (!buf)
405 die("Could not read %s for notes-index",
brian m. carlson5dcc9692017-05-30 10:30:37 -0700406 oid_to_hex(&subtree->val_oid));
Johan Herland23123ae2009-10-09 12:22:07 +0200407
brian m. carlson89c149f2017-05-30 10:30:38 -0700408 prefix_len = subtree->key_oid.hash[KEY_INDEX];
brian m. carlsondd437452019-02-19 00:05:01 +0000409 if (prefix_len >= hashsz)
Michael Haggerty39642812017-09-08 18:10:10 +0200410 BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
411 if (prefix_len * 2 < n)
412 BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
brian m. carlson89c149f2017-05-30 10:30:38 -0700413 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
Johan Herland23123ae2009-10-09 12:22:07 +0200414 while (tree_entry(&desc, &entry)) {
Michael Haggertya2816392017-08-26 10:28:03 +0200415 unsigned char type;
416 struct leaf_node *l;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200417 size_t path_len = strlen(entry.path);
Johan Herland23123ae2009-10-09 12:22:07 +0200418
brian m. carlsondd437452019-02-19 00:05:01 +0000419 if (path_len == 2 * (hashsz - prefix_len)) {
Michael Haggerty98c98972017-08-26 10:28:05 +0200420 /* This is potentially the remainder of the SHA-1 */
Michael Haggerty40432182017-08-26 10:28:07 +0200421
422 if (!S_ISREG(entry.mode))
423 /* notes must be blobs */
424 goto handle_non_note;
425
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200426 if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
brian m. carlsondd437452019-02-19 00:05:01 +0000427 hashsz - prefix_len))
Michael Haggerty98c98972017-08-26 10:28:05 +0200428 goto handle_non_note; /* entry.path is not a SHA1 */
Johan Herland23123ae2009-10-09 12:22:07 +0200429
Johan Herland851c2b32010-02-13 22:28:23 +0100430 type = PTR_TYPE_NOTE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200431 } else if (path_len == 2) {
432 /* This is potentially an internal node */
Michael Haggertyd49852d2017-08-26 10:28:10 +0200433 size_t len = prefix_len;
Michael Haggerty4d589b82017-08-26 10:28:06 +0200434
435 if (!S_ISDIR(entry.mode))
436 /* internal nodes must be trees */
437 goto handle_non_note;
438
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200439 if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
Michael Haggerty98c98972017-08-26 10:28:05 +0200440 goto handle_non_note; /* entry.path is not a SHA1 */
441
Michael Haggertyd49852d2017-08-26 10:28:10 +0200442 /*
443 * Pad the rest of the SHA-1 with zeros,
444 * except for the last byte, where we write
445 * the length:
446 */
brian m. carlsondd437452019-02-19 00:05:01 +0000447 memset(object_oid.hash + len, 0, hashsz - len - 1);
Michael Haggertyd49852d2017-08-26 10:28:10 +0200448 object_oid.hash[KEY_INDEX] = (unsigned char)len;
Michael Haggerty4ebef532017-08-26 10:28:09 +0200449
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200450 type = PTR_TYPE_SUBTREE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200451 } else {
452 /* This can't be part of a note */
453 goto handle_non_note;
Johan Herland23123ae2009-10-09 12:22:07 +0200454 }
Michael Haggerty98c98972017-08-26 10:28:05 +0200455
René Scharfeca56dad2021-03-13 17:17:22 +0100456 CALLOC_ARRAY(l, 1);
Michael Haggerty4ebef532017-08-26 10:28:09 +0200457 oidcpy(&l->key_oid, &object_oid);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000458 oidcpy(&l->val_oid, &entry.oid);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000459 oid_set_algo(&l->key_oid, the_hash_algo);
460 oid_set_algo(&l->val_oid, the_hash_algo);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200461 if (note_tree_insert(t, node, n, l, type,
462 combine_notes_concatenate))
463 die("Failed to load %s %s into notes tree "
464 "from %s",
465 type == PTR_TYPE_NOTE ? "note" : "subtree",
Jeff King60fe4772019-08-25 03:19:51 -0400466 oid_to_hex(&object_oid), t->ref);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200467
Johan Herland851c2b32010-02-13 22:28:23 +0100468 continue;
469
470handle_non_note:
471 /*
Michael Haggertycbeed9a2017-08-26 10:28:04 +0200472 * Determine full path for this non-note entry. The
473 * filename is already found in entry.path, but the
474 * directory part of the path must be deduced from the
475 * subtree containing this entry based on our
476 * knowledge that the overall notes tree follows a
477 * strict byte-based progressive fanout structure
478 * (i.e. using 2/38, 2/2/36, etc. fanouts).
Johan Herland851c2b32010-02-13 22:28:23 +0100479 */
480 {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400481 struct strbuf non_note_path = STRBUF_INIT;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700482 const char *q = oid_to_hex(&subtree->key_oid);
Michael Haggerty06cfa752017-08-26 10:28:12 +0200483 size_t i;
Johan Herland851c2b32010-02-13 22:28:23 +0100484 for (i = 0; i < prefix_len; i++) {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400485 strbuf_addch(&non_note_path, *q++);
486 strbuf_addch(&non_note_path, *q++);
487 strbuf_addch(&non_note_path, '/');
Johan Herland851c2b32010-02-13 22:28:23 +0100488 }
Jeff Kingc29edfe2015-08-19 14:12:41 -0400489 strbuf_addstr(&non_note_path, entry.path);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000490 oid_set_algo(&entry.oid, the_hash_algo);
Jeff Kingc29edfe2015-08-19 14:12:41 -0400491 add_non_note(t, strbuf_detach(&non_note_path, NULL),
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000492 entry.mode, entry.oid.hash);
Johan Herland851c2b32010-02-13 22:28:23 +0100493 }
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200494 }
Johan Herland23123ae2009-10-09 12:22:07 +0200495 free(buf);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200496}
497
Johan Herland73f77b92010-02-13 22:28:16 +0100498/*
499 * Determine optimal on-disk fanout for this part of the notes tree
500 *
501 * Given a (sub)tree and the level in the internal tree structure, determine
502 * whether or not the given existing fanout should be expanded for this
503 * (sub)tree.
504 *
505 * Values of the 'fanout' variable:
506 * - 0: No fanout (all notes are stored directly in the root notes tree)
507 * - 1: 2/38 fanout
508 * - 2: 2/2/36 fanout
509 * - 3: 2/2/2/34 fanout
510 * etc.
511 */
512static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
513 unsigned char fanout)
514{
515 /*
516 * The following is a simple heuristic that works well in practice:
517 * For each even-numbered 16-tree level (remember that each on-disk
518 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
519 * entries at that tree level. If all of them are either int_nodes or
520 * subtree entries, then there are likely plenty of notes below this
521 * level, so we return an incremented fanout.
522 */
523 unsigned int i;
524 if ((n % 2) || (n > 2 * fanout))
525 return fanout;
526 for (i = 0; i < 16; i++) {
527 switch (GET_PTR_TYPE(tree->a[i])) {
528 case PTR_TYPE_SUBTREE:
529 case PTR_TYPE_INTERNAL:
530 continue;
531 default:
532 return fanout;
533 }
534 }
535 return fanout + 1;
536}
537
brian m. carlsondd437452019-02-19 00:05:01 +0000538/* hex oid + '/' between each pair of hex digits + NUL */
539#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
Jeff King02e32b72015-09-24 17:08:24 -0400540
brian m. carlson0dbc6462019-02-19 00:05:02 +0000541static void construct_path_with_fanout(const unsigned char *hash,
Johan Herland73f77b92010-02-13 22:28:16 +0100542 unsigned char fanout, char *path)
543{
544 unsigned int i = 0, j = 0;
brian m. carlson0dbc6462019-02-19 00:05:02 +0000545 const char *hex_hash = hash_to_hex(hash);
brian m. carlsondd437452019-02-19 00:05:01 +0000546 assert(fanout < the_hash_algo->rawsz);
Johan Herland73f77b92010-02-13 22:28:16 +0100547 while (fanout) {
brian m. carlson0dbc6462019-02-19 00:05:02 +0000548 path[i++] = hex_hash[j++];
549 path[i++] = hex_hash[j++];
Johan Herland73f77b92010-02-13 22:28:16 +0100550 path[i++] = '/';
551 fanout--;
552 }
brian m. carlson0dbc6462019-02-19 00:05:02 +0000553 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
Johan Herland73f77b92010-02-13 22:28:16 +0100554}
555
Johan Herland851c2b32010-02-13 22:28:23 +0100556static 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)
Johan Herland73f77b92010-02-13 22:28:16 +0100559{
560 unsigned int i;
561 void *p;
562 int ret = 0;
563 struct leaf_node *l;
Jeff King02e32b72015-09-24 17:08:24 -0400564 static char path[FANOUT_PATH_MAX];
Johan Herland73f77b92010-02-13 22:28:16 +0100565
566 fanout = determine_fanout(tree, n, fanout);
567 for (i = 0; i < 16; i++) {
568redo:
569 p = tree->a[i];
570 switch (GET_PTR_TYPE(p)) {
571 case PTR_TYPE_INTERNAL:
572 /* recurse into int_node */
Johan Herland851c2b32010-02-13 22:28:23 +0100573 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
Johan Herland73f77b92010-02-13 22:28:16 +0100574 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:
Johan Herlanddbc27472020-02-03 22:04:45 +0100583 * Subtree entries at level 'n < 2 * fanout' should be
Johan Herland73f77b92010-02-13 22:28:16 +0100584 * preserved, since they correspond exactly to a fanout
585 * directory in the on-disk structure. However, subtree
Johan Herlanddbc27472020-02-03 22:04:45 +0100586 * entries at level 'n >= 2 * fanout' should NOT be
Johan Herland73f77b92010-02-13 22:28:16 +0100587 * 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 */
Johan Herlanddbc27472020-02-03 22:04:45 +0100592 if (n < 2 * fanout &&
Johan Herland73f77b92010-02-13 22:28:16 +0100593 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
594 /* invoke callback with subtree */
595 unsigned int path_len =
brian m. carlson89c149f2017-05-30 10:30:38 -0700596 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
Jeff King02e32b72015-09-24 17:08:24 -0400597 assert(path_len < FANOUT_PATH_MAX - 1);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700598 construct_path_with_fanout(l->key_oid.hash,
599 fanout,
Johan Herland73f77b92010-02-13 22:28:16 +0100600 path);
601 /* Create trailing slash, if needed */
602 if (path[path_len - 1] != '/')
603 path[path_len++] = '/';
604 path[path_len] = '\0';
brian m. carlson490bc832017-05-30 10:30:39 -0700605 ret = fn(&l->key_oid, &l->val_oid,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700606 path,
Johan Herland73f77b92010-02-13 22:28:16 +0100607 cb_data);
608 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100609 if (n >= 2 * fanout ||
Johan Herland73f77b92010-02-13 22:28:16 +0100610 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
611 /* unpack subtree and resume traversal */
612 tree->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100613 load_subtree(t, l, tree, n);
Johan Herland73f77b92010-02-13 22:28:16 +0100614 free(l);
615 goto redo;
616 }
617 break;
618 case PTR_TYPE_NOTE:
619 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700620 construct_path_with_fanout(l->key_oid.hash, fanout,
621 path);
brian m. carlson490bc832017-05-30 10:30:39 -0700622 ret = fn(&l->key_oid, &l->val_oid, path,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700623 cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +0100624 break;
625 }
626 if (ret)
627 return ret;
628 }
629 return 0;
630}
631
Johan Herland61a7cca2010-02-13 22:28:17 +0100632struct tree_write_stack {
633 struct tree_write_stack *next;
634 struct strbuf buf;
635 char path[2]; /* path to subtree in next, if any */
636};
637
638static inline int matches_tree_write_stack(struct tree_write_stack *tws,
639 const char *full_path)
640{
641 return full_path[0] == tws->path[0] &&
642 full_path[1] == tws->path[1] &&
643 full_path[2] == '/';
644}
645
646static void write_tree_entry(struct strbuf *buf, unsigned int mode,
647 const char *path, unsigned int path_len, const
brian m. carlsondd437452019-02-19 00:05:01 +0000648 unsigned char *hash)
Johan Herland61a7cca2010-02-13 22:28:17 +0100649{
Junio C Hamanoc88f0cc2010-02-24 21:39:06 -0800650 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
brian m. carlsondd437452019-02-19 00:05:01 +0000651 strbuf_add(buf, hash, the_hash_algo->rawsz);
Johan Herland61a7cca2010-02-13 22:28:17 +0100652}
653
654static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
655 const char *path)
656{
657 struct tree_write_stack *n;
658 assert(!tws->next);
659 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
660 n = (struct tree_write_stack *)
661 xmalloc(sizeof(struct tree_write_stack));
662 n->next = NULL;
brian m. carlsondd437452019-02-19 00:05:01 +0000663 strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
Johan Herland61a7cca2010-02-13 22:28:17 +0100664 n->path[0] = n->path[1] = '\0';
665 tws->next = n;
666 tws->path[0] = path[0];
667 tws->path[1] = path[1];
668}
669
670static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
671{
672 int ret;
673 struct tree_write_stack *n = tws->next;
brian m. carlson89c149f2017-05-30 10:30:38 -0700674 struct object_id s;
Johan Herland61a7cca2010-02-13 22:28:17 +0100675 if (n) {
676 ret = tree_write_stack_finish_subtree(n);
677 if (ret)
678 return ret;
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100679 ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
Johan Herland61a7cca2010-02-13 22:28:17 +0100680 if (ret)
681 return ret;
682 strbuf_release(&n->buf);
683 free(n);
684 tws->next = NULL;
brian m. carlson89c149f2017-05-30 10:30:38 -0700685 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100686 tws->path[0] = tws->path[1] = '\0';
687 }
688 return 0;
689}
690
691static int write_each_note_helper(struct tree_write_stack *tws,
692 const char *path, unsigned int mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700693 const struct object_id *oid)
Johan Herland61a7cca2010-02-13 22:28:17 +0100694{
695 size_t path_len = strlen(path);
696 unsigned int n = 0;
697 int ret;
698
699 /* Determine common part of tree write stack */
700 while (tws && 3 * n < path_len &&
701 matches_tree_write_stack(tws, path + 3 * n)) {
702 n++;
703 tws = tws->next;
704 }
705
706 /* tws point to last matching tree_write_stack entry */
707 ret = tree_write_stack_finish_subtree(tws);
708 if (ret)
709 return ret;
710
711 /* Start subtrees needed to satisfy path */
712 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
713 tree_write_stack_init_subtree(tws, path + 3 * n);
714 n++;
715 tws = tws->next;
716 }
717
718 /* There should be no more directory components in the given path */
719 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
720
721 /* Finally add given entry to the current tree object */
722 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
brian m. carlson490bc832017-05-30 10:30:39 -0700723 oid->hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100724
725 return 0;
726}
727
728struct write_each_note_data {
729 struct tree_write_stack *root;
Johan Herlanddbc27472020-02-03 22:04:45 +0100730 struct non_note **nn_list;
731 struct non_note *nn_prev;
Johan Herland61a7cca2010-02-13 22:28:17 +0100732};
733
Johan Herland851c2b32010-02-13 22:28:23 +0100734static int write_each_non_note_until(const char *note_path,
735 struct write_each_note_data *d)
736{
Johan Herlanddbc27472020-02-03 22:04:45 +0100737 struct non_note *p = d->nn_prev;
738 struct non_note *n = p ? p->next : *d->nn_list;
Ramsay Jones89fe1212010-06-21 19:52:29 +0100739 int cmp = 0, ret;
Johan Herland851c2b32010-02-13 22:28:23 +0100740 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
741 if (note_path && cmp == 0)
742 ; /* do nothing, prefer note to non-note */
743 else {
744 ret = write_each_note_helper(d->root, n->path, n->mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700745 &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100746 if (ret)
747 return ret;
748 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100749 p = n;
Johan Herland851c2b32010-02-13 22:28:23 +0100750 n = n->next;
751 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100752 d->nn_prev = p;
Johan Herland851c2b32010-02-13 22:28:23 +0100753 return 0;
754}
755
Jeff King3c50c882023-02-24 01:39:31 -0500756static int write_each_note(const struct object_id *object_oid UNUSED,
brian m. carlson490bc832017-05-30 10:30:39 -0700757 const struct object_id *note_oid, char *note_path,
Johan Herland61a7cca2010-02-13 22:28:17 +0100758 void *cb_data)
759{
760 struct write_each_note_data *d =
761 (struct write_each_note_data *) cb_data;
762 size_t note_path_len = strlen(note_path);
763 unsigned int mode = 0100644;
764
765 if (note_path[note_path_len - 1] == '/') {
766 /* subtree entry */
767 note_path_len--;
768 note_path[note_path_len] = '\0';
769 mode = 040000;
770 }
brian m. carlsondd437452019-02-19 00:05:01 +0000771 assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
Johan Herland61a7cca2010-02-13 22:28:17 +0100772
Johan Herland851c2b32010-02-13 22:28:23 +0100773 /* Weave non-note entries into note entries */
774 return write_each_non_note_until(note_path, d) ||
brian m. carlson490bc832017-05-30 10:30:39 -0700775 write_each_note_helper(d->root, note_path, mode, note_oid);
Johan Herland61a7cca2010-02-13 22:28:17 +0100776}
777
Johan Herland00fbe632010-02-13 22:28:27 +0100778struct note_delete_list {
779 struct note_delete_list *next;
780 const unsigned char *sha1;
781};
782
brian m. carlson490bc832017-05-30 10:30:39 -0700783static int prune_notes_helper(const struct object_id *object_oid,
Jeff King3c50c882023-02-24 01:39:31 -0500784 const struct object_id *note_oid UNUSED,
785 char *note_path UNUSED,
786 void *cb_data)
Johan Herland00fbe632010-02-13 22:28:27 +0100787{
788 struct note_delete_list **l = (struct note_delete_list **) cb_data;
789 struct note_delete_list *n;
790
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200791 if (repo_has_object_file(the_repository, object_oid))
Johan Herland00fbe632010-02-13 22:28:27 +0100792 return 0; /* nothing to do for this note */
793
794 /* failed to find object => prune this note */
795 n = (struct note_delete_list *) xmalloc(sizeof(*n));
796 n->next = *l;
brian m. carlson490bc832017-05-30 10:30:39 -0700797 n->sha1 = object_oid->hash;
Johan Herland00fbe632010-02-13 22:28:27 +0100798 *l = n;
799 return 0;
800}
801
Patryk Obarab7d591d2018-01-28 01:13:17 +0100802int combine_notes_concatenate(struct object_id *cur_oid,
803 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100804{
805 char *cur_msg = NULL, *new_msg = NULL, *buf;
806 unsigned long cur_len, new_len, buf_len;
807 enum object_type cur_type, new_type;
808 int ret;
809
810 /* read in both note blob objects */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100811 if (!is_null_oid(new_oid))
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200812 new_msg = repo_read_object_file(the_repository, new_oid,
813 &new_type, &new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100814 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
815 free(new_msg);
816 return 0;
817 }
Patryk Obarab7d591d2018-01-28 01:13:17 +0100818 if (!is_null_oid(cur_oid))
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200819 cur_msg = repo_read_object_file(the_repository, cur_oid,
820 &cur_type, &cur_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100821 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
822 free(cur_msg);
823 free(new_msg);
Patryk Obarab7d591d2018-01-28 01:13:17 +0100824 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100825 return 0;
826 }
827
Johan Herlandd4990c42010-11-09 22:49:44 +0100828 /* we will separate the notes by two newlines anyway */
Johan Herland73f464b2010-02-13 22:28:19 +0100829 if (cur_msg[cur_len - 1] == '\n')
830 cur_len--;
831
832 /* concatenate cur_msg and new_msg into buf */
Johan Herlandd4990c42010-11-09 22:49:44 +0100833 buf_len = cur_len + 2 + new_len;
Johan Herland73f464b2010-02-13 22:28:19 +0100834 buf = (char *) xmalloc(buf_len);
835 memcpy(buf, cur_msg, cur_len);
836 buf[cur_len] = '\n';
Johan Herlandd4990c42010-11-09 22:49:44 +0100837 buf[cur_len + 1] = '\n';
838 memcpy(buf + cur_len + 2, new_msg, new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100839 free(cur_msg);
840 free(new_msg);
841
842 /* create a new blob object from buf */
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100843 ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100844 free(buf);
845 return ret;
846}
847
Patryk Obarab7d591d2018-01-28 01:13:17 +0100848int combine_notes_overwrite(struct object_id *cur_oid,
849 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100850{
Patryk Obarab7d591d2018-01-28 01:13:17 +0100851 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100852 return 0;
853}
854
Jeff King3c50c882023-02-24 01:39:31 -0500855int combine_notes_ignore(struct object_id *cur_oid UNUSED,
856 const struct object_id *new_oid UNUSED)
Johan Herland73f464b2010-02-13 22:28:19 +0100857{
858 return 0;
859}
860
Michael Haggerty13135242012-11-04 08:07:08 +0100861/*
862 * Add the lines from the named object to list, with trailing
863 * newlines removed.
864 */
865static int string_list_add_note_lines(struct string_list *list,
Patryk Obarab7d591d2018-01-28 01:13:17 +0100866 const struct object_id *oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100867{
868 char *data;
869 unsigned long len;
870 enum object_type t;
Johan Herlanda6a09092010-11-15 00:57:17 +0100871
Patryk Obarab7d591d2018-01-28 01:13:17 +0100872 if (is_null_oid(oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100873 return 0;
874
875 /* read_sha1_file NUL-terminates */
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200876 data = repo_read_object_file(the_repository, oid, &t, &len);
Johan Herlanda6a09092010-11-15 00:57:17 +0100877 if (t != OBJ_BLOB || !data || !len) {
878 free(data);
879 return t != OBJ_BLOB || !data;
880 }
881
Michael Haggerty13135242012-11-04 08:07:08 +0100882 /*
883 * If the last line of the file is EOL-terminated, this will
884 * add an empty string to the list. But it will be removed
885 * later, along with any empty strings that came from empty
886 * lines within the file.
887 */
888 string_list_split(list, data, '\n', -1);
889 free(data);
Johan Herlanda6a09092010-11-15 00:57:17 +0100890 return 0;
891}
892
893static int string_list_join_lines_helper(struct string_list_item *item,
894 void *cb_data)
895{
896 struct strbuf *buf = cb_data;
897 strbuf_addstr(buf, item->string);
898 strbuf_addch(buf, '\n');
899 return 0;
900}
901
Patryk Obarab7d591d2018-01-28 01:13:17 +0100902int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
903 const struct object_id *new_oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100904{
Michael Haggertyf992f0c2012-11-04 08:07:07 +0100905 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
Johan Herlanda6a09092010-11-15 00:57:17 +0100906 struct strbuf buf = STRBUF_INIT;
907 int ret = 1;
908
909 /* read both note blob objects into unique_lines */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100910 if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100911 goto out;
Patryk Obarab7d591d2018-01-28 01:13:17 +0100912 if (string_list_add_note_lines(&sort_uniq_list, new_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100913 goto out;
Michael Haggerty13135242012-11-04 08:07:08 +0100914 string_list_remove_empty_items(&sort_uniq_list, 0);
Michael Haggerty3383e192014-11-25 09:02:35 +0100915 string_list_sort(&sort_uniq_list);
Michael Haggerty13135242012-11-04 08:07:08 +0100916 string_list_remove_duplicates(&sort_uniq_list, 0);
Johan Herlanda6a09092010-11-15 00:57:17 +0100917
918 /* create a new blob object from sort_uniq_list */
919 if (for_each_string_list(&sort_uniq_list,
920 string_list_join_lines_helper, &buf))
921 goto out;
922
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100923 ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
Johan Herlanda6a09092010-11-15 00:57:17 +0100924
925out:
926 strbuf_release(&buf);
927 string_list_clear(&sort_uniq_list, 0);
928 return ret;
929}
930
Jeff King63e14ee2022-08-19 06:08:32 -0400931static int string_list_add_one_ref(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200932 const struct object_id *oid UNUSED,
933 int flag UNUSED, void *cb)
Thomas Rast894a9d32010-03-12 18:04:26 +0100934{
935 struct string_list *refs = cb;
Michael Haggertyd235e992013-05-25 11:08:20 +0200936 if (!unsorted_string_list_has_string(refs, refname))
937 string_list_append(refs, refname);
Thomas Rast894a9d32010-03-12 18:04:26 +0100938 return 0;
939}
940
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200941/*
942 * The list argument must have strdup_strings set on it.
943 */
Thomas Rast894a9d32010-03-12 18:04:26 +0100944void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
945{
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200946 assert(list->strdup_strings);
Thomas Rast894a9d32010-03-12 18:04:26 +0100947 if (has_glob_specials(glob)) {
Michael Haggertyfd950352015-05-25 18:38:59 +0000948 for_each_glob_ref(string_list_add_one_ref, glob, list);
Thomas Rast894a9d32010-03-12 18:04:26 +0100949 } else {
brian m. carlson89c149f2017-05-30 10:30:38 -0700950 struct object_id oid;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200951 if (repo_get_oid(the_repository, glob, &oid))
Thomas Rast894a9d32010-03-12 18:04:26 +0100952 warning("notes ref %s is invalid", glob);
953 if (!unsorted_string_list_has_string(list, glob))
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100954 string_list_append(list, glob);
Thomas Rast894a9d32010-03-12 18:04:26 +0100955 }
956}
957
958void string_list_add_refs_from_colon_sep(struct string_list *list,
959 const char *globs)
960{
Michael Haggerty6fa23772012-11-04 08:07:10 +0100961 struct string_list split = STRING_LIST_INIT_NODUP;
962 char *globs_copy = xstrdup(globs);
Thomas Rast894a9d32010-03-12 18:04:26 +0100963 int i;
964
Taylor Blau52acddf2023-04-24 18:20:10 -0400965 string_list_split_in_place(&split, globs_copy, ":", -1);
Michael Haggerty6fa23772012-11-04 08:07:10 +0100966 string_list_remove_empty_items(&split, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +0100967
Michael Haggerty6fa23772012-11-04 08:07:10 +0100968 for (i = 0; i < split.nr; i++)
969 string_list_add_refs_by_glob(list, split.items[i].string);
Thomas Rast894a9d32010-03-12 18:04:26 +0100970
Michael Haggerty6fa23772012-11-04 08:07:10 +0100971 string_list_clear(&split, 0);
972 free(globs_copy);
Thomas Rast894a9d32010-03-12 18:04:26 +0100973}
974
Glen Chooa4e7e312023-06-28 19:26:22 +0000975static int notes_display_config(const char *k, const char *v,
976 const struct config_context *ctx UNUSED,
977 void *cb)
Thomas Rast894a9d32010-03-12 18:04:26 +0100978{
979 int *load_refs = cb;
980
981 if (*load_refs && !strcmp(k, "notes.displayref")) {
982 if (!v)
Nate Aversc3eb95a2020-11-22 22:23:41 -0500983 return config_error_nonbool(k);
Thomas Rast894a9d32010-03-12 18:04:26 +0100984 string_list_add_refs_by_glob(&display_notes_refs, v);
985 }
986
987 return 0;
988}
989
Johan Herland4a9cf1c2010-11-09 22:49:39 +0100990const char *default_notes_ref(void)
Thomas Rast894a9d32010-03-12 18:04:26 +0100991{
992 const char *notes_ref = NULL;
993 if (!notes_ref)
994 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
995 if (!notes_ref)
996 notes_ref = notes_ref_name; /* value of core.notesRef config */
997 if (!notes_ref)
998 notes_ref = GIT_NOTES_DEFAULT_REF;
999 return notes_ref;
1000}
1001
Johan Herland73f464b2010-02-13 22:28:19 +01001002void init_notes(struct notes_tree *t, const char *notes_ref,
1003 combine_notes_fn combine_notes, int flags)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001004{
brian m. carlson13ac1412016-09-05 20:08:05 +00001005 struct object_id oid, object_oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -07001006 unsigned short mode;
Johan Herland23123ae2009-10-09 12:22:07 +02001007 struct leaf_node root_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001008
Johan Herlandcd305392010-02-13 22:28:18 +01001009 if (!t)
1010 t = &default_notes_tree;
1011 assert(!t->initialized);
Johan Herland709f79b2010-02-13 22:28:12 +01001012
1013 if (!notes_ref)
Thomas Rast894a9d32010-03-12 18:04:26 +01001014 notes_ref = default_notes_ref();
Derrick Stoleeb9342b32022-08-05 17:58:36 +00001015 update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
Johan Herland709f79b2010-02-13 22:28:12 +01001016
Johan Herland73f464b2010-02-13 22:28:19 +01001017 if (!combine_notes)
1018 combine_notes = combine_notes_concatenate;
1019
Brian Gesiak65bbf082014-05-27 00:33:52 +09001020 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland851c2b32010-02-13 22:28:23 +01001021 t->first_non_note = NULL;
1022 t->prev_non_note = NULL;
Jeff Kingae6f0642023-04-22 09:55:43 -04001023 t->ref = xstrdup(notes_ref);
Mike Hommeyee76f922015-10-08 11:54:43 +09001024 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
Johan Herland73f464b2010-02-13 22:28:19 +01001025 t->combine_notes = combine_notes;
Johan Herlandcd305392010-02-13 22:28:18 +01001026 t->initialized = 1;
Thomas Rast7f710ea2010-03-12 18:04:36 +01001027 t->dirty = 0;
Johan Herlandcd305392010-02-13 22:28:18 +01001028
Jeff Kingae6f0642023-04-22 09:55:43 -04001029 if (flags & NOTES_INIT_EMPTY ||
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001030 repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001031 return;
brian m. carlson34c290a2017-10-15 22:06:56 +00001032 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
Mike Hommeyee76f922015-10-08 11:54:43 +09001033 die("Cannot use notes ref %s", notes_ref);
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001034 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
Johan Herland709f79b2010-02-13 22:28:12 +01001035 die("Failed to read notes tree referenced by %s (%s)",
brian m. carlson13ac1412016-09-05 20:08:05 +00001036 notes_ref, oid_to_hex(&object_oid));
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001037
brian m. carlson5dcc9692017-05-30 10:30:37 -07001038 oidclr(&root_tree.key_oid);
1039 oidcpy(&root_tree.val_oid, &oid);
Johan Herland851c2b32010-02-13 22:28:23 +01001040 load_subtree(t, &root_tree, t->root, 0);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001041}
1042
Mike Hommeyee76f922015-10-08 11:54:43 +09001043struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
Thomas Rast894a9d32010-03-12 18:04:26 +01001044{
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001045 struct string_list_item *item;
1046 int counter = 0;
Thomas Rast894a9d32010-03-12 18:04:26 +01001047 struct notes_tree **trees;
Jeff Kingb32fa952016-02-22 17:44:25 -05001048 ALLOC_ARRAY(trees, refs->nr + 1);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001049 for_each_string_list_item(item, refs) {
1050 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
Mike Hommeyee76f922015-10-08 11:54:43 +09001051 init_notes(t, item->string, combine_notes_ignore, flags);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001052 trees[counter++] = t;
1053 }
1054 trees[counter] = NULL;
Thomas Rast894a9d32010-03-12 18:04:26 +01001055 return trees;
1056}
1057
1058void init_display_notes(struct display_notes_opt *opt)
1059{
Denton Liue6e230e2019-12-09 05:10:41 -08001060 memset(opt, 0, sizeof(*opt));
1061 opt->use_default_notes = -1;
1062}
1063
Denton Liu1d729752019-12-11 16:49:50 -08001064void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
Denton Liu452538c2019-12-09 05:10:44 -08001065{
Denton Liu1d729752019-12-11 16:49:50 -08001066 opt->use_default_notes = 1;
1067 *show_notes = 1;
1068}
Denton Liu452538c2019-12-09 05:10:44 -08001069
Denton Liu1d729752019-12-11 16:49:50 -08001070void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1071 const char *ref) {
1072 struct strbuf buf = STRBUF_INIT;
1073 strbuf_addstr(&buf, ref);
1074 expand_notes_ref(&buf);
1075 string_list_append(&opt->extra_notes_refs,
1076 strbuf_detach(&buf, NULL));
1077 *show_notes = 1;
1078}
1079
1080void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1081{
1082 opt->use_default_notes = -1;
1083 /* we have been strdup'ing ourselves, so trick
1084 * string_list into free()ing strings */
1085 opt->extra_notes_refs.strdup_strings = 1;
1086 string_list_clear(&opt->extra_notes_refs, 0);
1087 opt->extra_notes_refs.strdup_strings = 0;
1088 *show_notes = 0;
Denton Liu452538c2019-12-09 05:10:44 -08001089}
1090
Denton Liu1e6ed542019-12-09 05:10:39 -08001091void load_display_notes(struct display_notes_opt *opt)
Thomas Rast894a9d32010-03-12 18:04:26 +01001092{
1093 char *display_ref_env;
1094 int load_config_refs = 0;
1095 display_notes_refs.strdup_strings = 1;
1096
1097 assert(!display_notes_trees);
1098
Jeff King3a03cf62011-03-29 16:57:27 -04001099 if (!opt || opt->use_default_notes > 0 ||
1100 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001101 string_list_append(&display_notes_refs, default_notes_ref());
Thomas Rast894a9d32010-03-12 18:04:26 +01001102 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1103 if (display_ref_env) {
1104 string_list_add_refs_from_colon_sep(&display_notes_refs,
1105 display_ref_env);
1106 load_config_refs = 0;
1107 } else
1108 load_config_refs = 1;
1109 }
1110
1111 git_config(notes_display_config, &load_config_refs);
1112
Jeff King304cc112011-03-29 16:56:53 -04001113 if (opt) {
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001114 struct string_list_item *item;
Jeff King304cc112011-03-29 16:56:53 -04001115 for_each_string_list_item(item, &opt->extra_notes_refs)
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001116 string_list_add_refs_by_glob(&display_notes_refs,
1117 item->string);
1118 }
Thomas Rast894a9d32010-03-12 18:04:26 +01001119
Mike Hommeyee76f922015-10-08 11:54:43 +09001120 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +01001121 string_list_clear(&display_notes_refs, 0);
1122}
1123
brian m. carlson5ee8a952017-05-30 10:30:43 -07001124int add_note(struct notes_tree *t, const struct object_id *object_oid,
1125 const struct object_id *note_oid, combine_notes_fn combine_notes)
Johan Herland2626b532010-02-13 22:28:13 +01001126{
1127 struct leaf_node *l;
1128
Johan Herlandcd305392010-02-13 22:28:18 +01001129 if (!t)
1130 t = &default_notes_tree;
1131 assert(t->initialized);
Thomas Rast7f710ea2010-03-12 18:04:36 +01001132 t->dirty = 1;
Johan Herland73f464b2010-02-13 22:28:19 +01001133 if (!combine_notes)
1134 combine_notes = t->combine_notes;
Johan Herland2626b532010-02-13 22:28:13 +01001135 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
brian m. carlson5ee8a952017-05-30 10:30:43 -07001136 oidcpy(&l->key_oid, object_oid);
1137 oidcpy(&l->val_oid, note_oid);
Johan Herland180619a2010-11-15 00:52:26 +01001138 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
Johan Herland2626b532010-02-13 22:28:13 +01001139}
1140
Johan Herland1ee1e432010-08-31 17:56:50 +02001141int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
Johan Herland1ec666b2010-02-13 22:28:14 +01001142{
1143 struct leaf_node l;
1144
Johan Herlandcd305392010-02-13 22:28:18 +01001145 if (!t)
1146 t = &default_notes_tree;
1147 assert(t->initialized);
brian m. carlson92e2cab2021-04-26 01:02:50 +00001148 oidread(&l.key_oid, object_sha1);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001149 oidclr(&l.val_oid);
Brandon Caseya502ab92010-03-18 10:03:43 -05001150 note_tree_remove(t, t->root, 0, &l);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001151 if (is_null_oid(&l.val_oid)) /* no note was removed */
Johan Herland1ee1e432010-08-31 17:56:50 +02001152 return 1;
1153 t->dirty = 1;
1154 return 0;
Johan Herland1ec666b2010-02-13 22:28:14 +01001155}
1156
brian m. carlson9ef72232017-05-30 10:30:40 -07001157const struct object_id *get_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001158 const struct object_id *oid)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001159{
Johan Herland9b391f22010-02-13 22:28:15 +01001160 struct leaf_node *found;
1161
Johan Herlandcd305392010-02-13 22:28:18 +01001162 if (!t)
1163 t = &default_notes_tree;
1164 assert(t->initialized);
brian m. carlson5ee8a952017-05-30 10:30:43 -07001165 found = note_tree_find(t, t->root, 0, oid->hash);
brian m. carlson9ef72232017-05-30 10:30:40 -07001166 return found ? &found->val_oid : NULL;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001167}
Johannes Schindelina97a7462009-10-09 12:21:57 +02001168
Johan Herlandcd305392010-02-13 22:28:18 +01001169int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1170 void *cb_data)
Johan Herland73f77b92010-02-13 22:28:16 +01001171{
Johan Herlandcd305392010-02-13 22:28:18 +01001172 if (!t)
1173 t = &default_notes_tree;
1174 assert(t->initialized);
Johan Herland851c2b32010-02-13 22:28:23 +01001175 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +01001176}
1177
Patryk Obarabbca96d2018-01-28 01:13:18 +01001178int write_notes_tree(struct notes_tree *t, struct object_id *result)
Johan Herland61a7cca2010-02-13 22:28:17 +01001179{
1180 struct tree_write_stack root;
1181 struct write_each_note_data cb_data;
1182 int ret;
Patryk Obarabbca96d2018-01-28 01:13:18 +01001183 int flags;
Johan Herland61a7cca2010-02-13 22:28:17 +01001184
Johan Herlandcd305392010-02-13 22:28:18 +01001185 if (!t)
1186 t = &default_notes_tree;
1187 assert(t->initialized);
Johan Herland61a7cca2010-02-13 22:28:17 +01001188
1189 /* Prepare for traversal of current notes tree */
1190 root.next = NULL; /* last forward entry in list is grounded */
brian m. carlsondd437452019-02-19 00:05:01 +00001191 strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
Johan Herland61a7cca2010-02-13 22:28:17 +01001192 root.path[0] = root.path[1] = '\0';
1193 cb_data.root = &root;
Johan Herlanddbc27472020-02-03 22:04:45 +01001194 cb_data.nn_list = &(t->first_non_note);
1195 cb_data.nn_prev = NULL;
Johan Herland61a7cca2010-02-13 22:28:17 +01001196
1197 /* Write tree objects representing current notes tree */
Patryk Obarabbca96d2018-01-28 01:13:18 +01001198 flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1199 FOR_EACH_NOTE_YIELD_SUBTREES;
1200 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1201 write_each_non_note_until(NULL, &cb_data) ||
1202 tree_write_stack_finish_subtree(&root) ||
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001203 write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
Johan Herland61a7cca2010-02-13 22:28:17 +01001204 strbuf_release(&root.buf);
1205 return ret;
1206}
1207
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001208void prune_notes(struct notes_tree *t, int flags)
Johan Herland00fbe632010-02-13 22:28:27 +01001209{
1210 struct note_delete_list *l = NULL;
1211
1212 if (!t)
1213 t = &default_notes_tree;
1214 assert(t->initialized);
1215
1216 for_each_note(t, 0, prune_notes_helper, &l);
1217
1218 while (l) {
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001219 if (flags & NOTES_PRUNE_VERBOSE)
brian m. carlson0dbc6462019-02-19 00:05:02 +00001220 printf("%s\n", hash_to_hex(l->sha1));
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001221 if (!(flags & NOTES_PRUNE_DRYRUN))
1222 remove_note(t, l->sha1);
Johan Herland00fbe632010-02-13 22:28:27 +01001223 l = l->next;
1224 }
1225}
1226
Johan Herlandcd305392010-02-13 22:28:18 +01001227void free_notes(struct notes_tree *t)
Johan Herland27d57562009-10-09 12:22:06 +02001228{
Johan Herlandcd305392010-02-13 22:28:18 +01001229 if (!t)
1230 t = &default_notes_tree;
1231 if (t->root)
1232 note_tree_free(t->root);
1233 free(t->root);
Johan Herland851c2b32010-02-13 22:28:23 +01001234 while (t->first_non_note) {
1235 t->prev_non_note = t->first_non_note->next;
1236 free(t->first_non_note->path);
1237 free(t->first_non_note);
1238 t->first_non_note = t->prev_non_note;
1239 }
Johan Herlandcd305392010-02-13 22:28:18 +01001240 free(t->ref);
1241 memset(t, 0, sizeof(struct notes_tree));
Johan Herland27d57562009-10-09 12:22:06 +02001242}
1243
Junio C Hamano96531a52012-09-15 14:08:39 -07001244/*
1245 * Fill the given strbuf with the notes associated with the given object.
1246 *
1247 * If the given notes_tree structure is not initialized, it will be auto-
1248 * initialized to the default value (see documentation for init_notes() above).
1249 * If the given notes_tree is NULL, the internal/default notes_tree will be
1250 * used instead.
1251 *
Junio C Hamano76141e22012-10-17 21:41:54 -07001252 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1253 * for human consumption.
Junio C Hamano96531a52012-09-15 14:08:39 -07001254 */
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001255static void format_note(struct notes_tree *t, const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001256 struct strbuf *sb, const char *output_encoding, int raw)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001257{
1258 static const char utf8[] = "utf-8";
brian m. carlson9ef72232017-05-30 10:30:40 -07001259 const struct object_id *oid;
Johannes Schindelina97a7462009-10-09 12:21:57 +02001260 char *msg, *msg_p;
1261 unsigned long linelen, msglen;
1262 enum object_type type;
1263
Johan Herlandcd305392010-02-13 22:28:18 +01001264 if (!t)
1265 t = &default_notes_tree;
1266 if (!t->initialized)
Johan Herland73f464b2010-02-13 22:28:19 +01001267 init_notes(t, NULL, NULL, 0);
Johannes Schindelina97a7462009-10-09 12:21:57 +02001268
brian m. carlson5ee8a952017-05-30 10:30:43 -07001269 oid = get_note(t, object_oid);
brian m. carlson9ef72232017-05-30 10:30:40 -07001270 if (!oid)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001271 return;
1272
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001273 if (!(msg = repo_read_object_file(the_repository, oid, &type, &msglen)) || type != OBJ_BLOB) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001274 free(msg);
1275 return;
1276 }
1277
1278 if (output_encoding && *output_encoding &&
Junio C Hamano0e18bcd2012-10-18 22:41:56 -07001279 !is_encoding_utf8(output_encoding)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001280 char *reencoded = reencode_string(msg, output_encoding, utf8);
1281 if (reencoded) {
1282 free(msg);
1283 msg = reencoded;
1284 msglen = strlen(msg);
1285 }
1286 }
1287
1288 /* we will end the annotation by a newline anyway */
1289 if (msglen && msg[msglen - 1] == '\n')
1290 msglen--;
1291
Junio C Hamano76141e22012-10-17 21:41:54 -07001292 if (!raw) {
Thomas Rast894a9d32010-03-12 18:04:26 +01001293 const char *ref = t->ref;
1294 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1295 strbuf_addstr(sb, "\nNotes:\n");
1296 } else {
Junio C Hamano145136a2020-01-30 11:35:46 -08001297 skip_prefix(ref, "refs/", &ref);
1298 skip_prefix(ref, "notes/", &ref);
Thomas Rast894a9d32010-03-12 18:04:26 +01001299 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1300 }
1301 }
Johannes Schindelina97a7462009-10-09 12:21:57 +02001302
1303 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1304 linelen = strchrnul(msg_p, '\n') - msg_p;
1305
Junio C Hamano76141e22012-10-17 21:41:54 -07001306 if (!raw)
Johan Herlandc56fcc82009-10-09 12:22:04 +02001307 strbuf_addstr(sb, " ");
Johannes Schindelina97a7462009-10-09 12:21:57 +02001308 strbuf_add(sb, msg_p, linelen);
1309 strbuf_addch(sb, '\n');
1310 }
1311
1312 free(msg);
1313}
Thomas Rast894a9d32010-03-12 18:04:26 +01001314
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001315void format_display_notes(const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001316 struct strbuf *sb, const char *output_encoding, int raw)
Thomas Rast894a9d32010-03-12 18:04:26 +01001317{
1318 int i;
1319 assert(display_notes_trees);
1320 for (i = 0; display_notes_trees[i]; i++)
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001321 format_note(display_notes_trees[i], object_oid, sb,
Junio C Hamano76141e22012-10-17 21:41:54 -07001322 output_encoding, raw);
Thomas Rast894a9d32010-03-12 18:04:26 +01001323}
Thomas Rast160baa02010-03-12 18:04:31 +01001324
1325int copy_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001326 const struct object_id *from_obj, const struct object_id *to_obj,
Johan Herland180619a2010-11-15 00:52:26 +01001327 int force, combine_notes_fn combine_notes)
Thomas Rast160baa02010-03-12 18:04:31 +01001328{
brian m. carlson9ef72232017-05-30 10:30:40 -07001329 const struct object_id *note = get_note(t, from_obj);
1330 const struct object_id *existing_note = get_note(t, to_obj);
Thomas Rast160baa02010-03-12 18:04:31 +01001331
1332 if (!force && existing_note)
1333 return 1;
1334
1335 if (note)
brian m. carlson5ee8a952017-05-30 10:30:43 -07001336 return add_note(t, to_obj, note, combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001337 else if (existing_note)
brian m. carlson14228442021-04-26 01:02:56 +00001338 return add_note(t, to_obj, null_oid(), combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001339
1340 return 0;
1341}
Jeff King03bb5782011-03-29 16:55:32 -04001342
1343void expand_notes_ref(struct strbuf *sb)
1344{
Christian Couder59556542013-11-30 21:55:40 +01001345 if (starts_with(sb->buf, "refs/notes/"))
Jeff King03bb5782011-03-29 16:55:32 -04001346 return; /* we're happy */
Christian Couder59556542013-11-30 21:55:40 +01001347 else if (starts_with(sb->buf, "notes/"))
René Scharfea91cc7f2020-02-09 14:44:23 +01001348 strbuf_insertstr(sb, 0, "refs/");
Jeff King03bb5782011-03-29 16:55:32 -04001349 else
René Scharfea91cc7f2020-02-09 14:44:23 +01001350 strbuf_insertstr(sb, 0, "refs/notes/");
Jeff King03bb5782011-03-29 16:55:32 -04001351}
Jacob Kellerb3715b72015-12-29 14:40:28 -08001352
1353void expand_loose_notes_ref(struct strbuf *sb)
1354{
brian m. carlson89c149f2017-05-30 10:30:38 -07001355 struct object_id object;
Jacob Kellerb3715b72015-12-29 14:40:28 -08001356
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001357 if (repo_get_oid(the_repository, sb->buf, &object)) {
Jacob Kellerb3715b72015-12-29 14:40:28 -08001358 /* fallback to expand_notes_ref */
1359 expand_notes_ref(sb);
1360 }
1361}