blob: 45fb7f22d1df011396ec86e56f1f1bd3bd1645b9 [file] [log] [blame]
Johannes Schindelina97a7462009-10-09 12:21:57 +02001#include "cache.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"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Johan Herland73f464b2010-02-13 22:28:19 +01007#include "blob.h"
Johan Herland61a7cca2010-02-13 22:28:17 +01008#include "tree.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02009#include "utf8.h"
10#include "strbuf.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020011#include "tree-walk.h"
Thomas Rast894a9d32010-03-12 18:04:26 +010012#include "string-list.h"
13#include "refs.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020014
Johan Herland23123ae2009-10-09 12:22:07 +020015/*
16 * Use a non-balancing simple 16-tree structure with struct int_node as
17 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
18 * 16-array of pointers to its children.
19 * The bottom 2 bits of each pointer is used to identify the pointer type
20 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
21 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
22 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
23 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
24 *
25 * The root node is a statically allocated struct int_node.
26 */
27struct int_node {
28 void *a[16];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020029};
30
Johan Herland23123ae2009-10-09 12:22:07 +020031/*
32 * Leaf nodes come in two variants, note entries and subtree entries,
33 * distinguished by the LSb of the leaf node pointer (see above).
Johan Herlanda7e7eff2010-02-13 22:28:10 +010034 * As a note entry, the key is the SHA1 of the referenced object, and the
Johan Herland23123ae2009-10-09 12:22:07 +020035 * value is the SHA1 of the note object.
36 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
Johan Herlanda7e7eff2010-02-13 22:28:10 +010037 * referenced object, using the last byte of the key to store the length of
Johan Herland23123ae2009-10-09 12:22:07 +020038 * the prefix. The value is the SHA1 of the tree object containing the notes
39 * subtree.
40 */
41struct leaf_node {
brian m. carlson5dcc9692017-05-30 10:30:37 -070042 struct object_id key_oid;
43 struct object_id val_oid;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020044};
Johannes Schindelina97a7462009-10-09 12:21:57 +020045
Johan Herland851c2b32010-02-13 22:28:23 +010046/*
47 * A notes tree may contain entries that are not notes, and that do not follow
48 * the naming conventions of notes. There are typically none/few of these, but
49 * we still need to keep track of them. Keep a simple linked list sorted alpha-
50 * betically on the non-note path. The list is populated when parsing tree
51 * objects in load_subtree(), and the non-notes are correctly written back into
52 * the tree objects produced by write_notes_tree().
53 */
54struct non_note {
55 struct non_note *next; /* grounded (last->next == NULL) */
56 char *path;
57 unsigned int mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -070058 struct object_id oid;
Johan Herland851c2b32010-02-13 22:28:23 +010059};
60
Johan Herland23123ae2009-10-09 12:22:07 +020061#define PTR_TYPE_NULL 0
62#define PTR_TYPE_INTERNAL 1
63#define PTR_TYPE_NOTE 2
64#define PTR_TYPE_SUBTREE 3
65
66#define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
67#define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
68#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
69
Michael Haggerty65eb8e02017-08-26 10:28:01 +020070#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
Johan Herland23123ae2009-10-09 12:22:07 +020071
brian m. carlsondd437452019-02-19 00:05:01 +000072#define KEY_INDEX (the_hash_algo->rawsz - 1)
73#define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
74#define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
Johan Herland23123ae2009-10-09 12:22:07 +020075#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
brian m. carlson89c149f2017-05-30 10:30:38 -070076 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
Johan Herland23123ae2009-10-09 12:22:07 +020077
Johan Herlandcd305392010-02-13 22:28:18 +010078struct notes_tree default_notes_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020079
Jeff King2721ce22016-06-13 06:04:20 -040080static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
Thomas Rast894a9d32010-03-12 18:04:26 +010081static struct notes_tree **display_notes_trees;
82
Johan Herland851c2b32010-02-13 22:28:23 +010083static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
84 struct int_node *node, unsigned int n);
Johan Herland23123ae2009-10-09 12:22:07 +020085
86/*
Johan Herlandef8db632009-10-09 12:22:09 +020087 * Search the tree until the appropriate location for the given key is found:
Johan Herland23123ae2009-10-09 12:22:07 +020088 * 1. Start at the root node, with n = 0
Johan Herlandef8db632009-10-09 12:22:09 +020089 * 2. If a[0] at the current level is a matching subtree entry, unpack that
90 * subtree entry and remove it; restart search at the current level.
91 * 3. Use the nth nibble of the key as an index into a:
92 * - If a[n] is an int_node, recurse from #2 into that node and increment n
Johan Herland23123ae2009-10-09 12:22:07 +020093 * - If a matching subtree entry, unpack that subtree entry (and remove it);
94 * restart search at the current level.
Johan Herlandef8db632009-10-09 12:22:09 +020095 * - Otherwise, we have found one of the following:
96 * - a subtree entry which does not match the key
97 * - a note entry which may or may not match the key
98 * - an unused leaf node (NULL)
99 * In any case, set *tree and *n, and return pointer to the tree location.
Johan Herland23123ae2009-10-09 12:22:07 +0200100 */
Johan Herland851c2b32010-02-13 22:28:23 +0100101static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
Johan Herlandef8db632009-10-09 12:22:09 +0200102 unsigned char *n, const unsigned char *key_sha1)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200103{
Johan Herland23123ae2009-10-09 12:22:07 +0200104 struct leaf_node *l;
Johan Herlandef8db632009-10-09 12:22:09 +0200105 unsigned char i;
106 void *p = (*tree)->a[0];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200107
Johan Herlandef8db632009-10-09 12:22:09 +0200108 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
109 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700110 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herlandef8db632009-10-09 12:22:09 +0200111 /* unpack tree and resume search */
112 (*tree)->a[0] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100113 load_subtree(t, l, *tree, *n);
Johan Herlandef8db632009-10-09 12:22:09 +0200114 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100115 return note_tree_search(t, tree, n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200116 }
117 }
118
119 i = GET_NIBBLE(*n, key_sha1);
120 p = (*tree)->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100121 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200122 case PTR_TYPE_INTERNAL:
Johan Herlandef8db632009-10-09 12:22:09 +0200123 *tree = CLR_PTR_TYPE(p);
124 (*n)++;
Johan Herland851c2b32010-02-13 22:28:23 +0100125 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200126 case PTR_TYPE_SUBTREE:
127 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700128 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200129 /* unpack tree and resume search */
Johan Herlandef8db632009-10-09 12:22:09 +0200130 (*tree)->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100131 load_subtree(t, l, *tree, *n);
Johan Herland23123ae2009-10-09 12:22:07 +0200132 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100133 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200134 }
Johan Herlandef8db632009-10-09 12:22:09 +0200135 /* fall through */
Johan Herland23123ae2009-10-09 12:22:07 +0200136 default:
Johan Herlandef8db632009-10-09 12:22:09 +0200137 return &((*tree)->a[i]);
Johan Herland23123ae2009-10-09 12:22:07 +0200138 }
Johan Herlandef8db632009-10-09 12:22:09 +0200139}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200140
Johan Herlandef8db632009-10-09 12:22:09 +0200141/*
142 * To find a leaf_node:
143 * Search to the tree location appropriate for the given key:
144 * If a note entry with matching key, return the note entry, else return NULL.
145 */
Johan Herland851c2b32010-02-13 22:28:23 +0100146static struct leaf_node *note_tree_find(struct notes_tree *t,
147 struct int_node *tree, unsigned char n,
Johan Herlandef8db632009-10-09 12:22:09 +0200148 const unsigned char *key_sha1)
149{
Johan Herland851c2b32010-02-13 22:28:23 +0100150 void **p = note_tree_search(t, &tree, &n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200151 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
152 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff Kinge3ff0682018-08-28 17:22:44 -0400153 if (hasheq(key_sha1, l->key_oid.hash))
Johan Herlandef8db632009-10-09 12:22:09 +0200154 return l;
Johan Herland23123ae2009-10-09 12:22:07 +0200155 }
156 return NULL;
157}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200158
Johan Herland23123ae2009-10-09 12:22:07 +0200159/*
Johan Herland1ec666b2010-02-13 22:28:14 +0100160 * How to consolidate an int_node:
161 * If there are > 1 non-NULL entries, give up and return non-zero.
162 * Otherwise replace the int_node at the given index in the given parent node
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900163 * with the only NOTE entry (or a NULL entry if no entries) from the given
164 * tree, and return 0.
Johan Herland1ec666b2010-02-13 22:28:14 +0100165 */
166static int note_tree_consolidate(struct int_node *tree,
167 struct int_node *parent, unsigned char index)
168{
169 unsigned int i;
170 void *p = NULL;
171
172 assert(tree && parent);
173 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
174
175 for (i = 0; i < 16; i++) {
176 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
177 if (p) /* more than one entry */
178 return -2;
179 p = tree->a[i];
180 }
181 }
182
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900183 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
184 return -2;
Johan Herland1ec666b2010-02-13 22:28:14 +0100185 /* replace tree with p in parent[index] */
186 parent->a[index] = p;
187 free(tree);
188 return 0;
189}
190
191/*
192 * To remove a leaf_node:
193 * Search to the tree location appropriate for the given leaf_node's key:
194 * - If location does not hold a matching entry, abort and do nothing.
Johan Herland1ee1e432010-08-31 17:56:50 +0200195 * - Copy the matching entry's value into the given entry.
Johan Herland1ec666b2010-02-13 22:28:14 +0100196 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
197 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
198 */
Johan Herland1ee1e432010-08-31 17:56:50 +0200199static void note_tree_remove(struct notes_tree *t,
200 struct int_node *tree, unsigned char n,
201 struct leaf_node *entry)
Johan Herland1ec666b2010-02-13 22:28:14 +0100202{
203 struct leaf_node *l;
brian m. carlsondd437452019-02-19 00:05:01 +0000204 struct int_node *parent_stack[GIT_MAX_RAWSZ];
Johan Herland1ec666b2010-02-13 22:28:14 +0100205 unsigned char i, j;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700206 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100207
208 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
209 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
210 return; /* type mismatch, nothing to remove */
211 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff King9001dc22018-08-28 17:22:48 -0400212 if (!oideq(&l->key_oid, &entry->key_oid))
Johan Herland1ec666b2010-02-13 22:28:14 +0100213 return; /* key mismatch, nothing to remove */
214
215 /* we have found a matching entry */
brian m. carlson5dcc9692017-05-30 10:30:37 -0700216 oidcpy(&entry->val_oid, &l->val_oid);
Johan Herland1ec666b2010-02-13 22:28:14 +0100217 free(l);
218 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
219
220 /* consolidate this tree level, and parent levels, if possible */
221 if (!n)
222 return; /* cannot consolidate top level */
223 /* first, build stack of ancestors between root and current node */
Johan Herlandcd305392010-02-13 22:28:18 +0100224 parent_stack[0] = t->root;
Johan Herland1ec666b2010-02-13 22:28:14 +0100225 for (i = 0; i < n; i++) {
brian m. carlson5dcc9692017-05-30 10:30:37 -0700226 j = GET_NIBBLE(i, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100227 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
228 }
229 assert(i == n && parent_stack[i] == tree);
230 /* next, unwind stack until note_tree_consolidate() is done */
231 while (i > 0 &&
232 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
brian m. carlson5dcc9692017-05-30 10:30:37 -0700233 GET_NIBBLE(i - 1, entry->key_oid.hash)))
Johan Herland1ec666b2010-02-13 22:28:14 +0100234 i--;
235}
236
Johan Herlanda5cdebe2010-11-09 22:49:40 +0100237/*
Johannes Schindelina97a7462009-10-09 12:21:57 +0200238 * To insert a leaf_node:
239 * Search to the tree location appropriate for the given leaf_node's key:
240 * - If location is unused (NULL), store the tweaked pointer directly there
241 * - If location holds a note entry that matches the note-to-be-inserted, then
242 * combine the two notes (by calling the given combine_notes function).
243 * - If location holds a note entry that matches the subtree-to-be-inserted,
244 * then unpack the subtree-to-be-inserted into the location.
245 * - If location holds a matching subtree entry, unpack the subtree at that
246 * location, and restart the insert operation from that level.
247 * - Else, create a new int_node, holding both the node-at-location and the
248 * node-to-be-inserted, and store the new int_node into the location.
249 */
Johan Herland180619a2010-11-15 00:52:26 +0100250static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
Johannes Schindelina97a7462009-10-09 12:21:57 +0200251 unsigned char n, struct leaf_node *entry, unsigned char type,
252 combine_notes_fn combine_notes)
253{
254 struct int_node *new_node;
255 struct leaf_node *l;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700256 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland180619a2010-11-15 00:52:26 +0100257 int ret = 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200258
259 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
260 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
261 switch (GET_PTR_TYPE(*p)) {
262 case PTR_TYPE_NULL:
263 assert(!*p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700264 if (is_null_oid(&entry->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100265 free(entry);
266 else
267 *p = SET_PTR_TYPE(entry, type);
Johan Herland180619a2010-11-15 00:52:26 +0100268 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200269 case PTR_TYPE_NOTE:
270 switch (type) {
271 case PTR_TYPE_NOTE:
Jeff King4a7e27e2018-08-28 17:22:40 -0400272 if (oideq(&l->key_oid, &entry->key_oid)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200273 /* skip concatenation if l == entry */
Mike Hommey779ad662019-08-25 14:18:18 +0900274 if (oideq(&l->val_oid, &entry->val_oid)) {
275 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100276 return 0;
Mike Hommey779ad662019-08-25 14:18:18 +0900277 }
Johannes Schindelina97a7462009-10-09 12:21:57 +0200278
Patryk Obarab7d591d2018-01-28 01:13:17 +0100279 ret = combine_notes(&l->val_oid,
280 &entry->val_oid);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700281 if (!ret && is_null_oid(&l->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100282 note_tree_remove(t, tree, n, entry);
Johannes Schindelina97a7462009-10-09 12:21:57 +0200283 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100284 return ret;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200285 }
286 break;
287 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700288 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
289 entry->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200290 /* unpack 'entry' */
291 load_subtree(t, entry, tree, n);
292 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100293 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200294 }
295 break;
296 }
297 break;
298 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700299 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200300 /* unpack 'l' and restart insert */
301 *p = NULL;
302 load_subtree(t, l, tree, n);
303 free(l);
Johan Herland180619a2010-11-15 00:52:26 +0100304 return note_tree_insert(t, tree, n, entry, type,
305 combine_notes);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200306 }
307 break;
308 }
309
310 /* non-matching leaf_node */
311 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
312 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700313 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
Johan Herlande2656c82010-11-09 22:49:41 +0100314 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100315 return 0;
Johan Herlande2656c82010-11-09 22:49:41 +0100316 }
Brian Gesiak65bbf082014-05-27 00:33:52 +0900317 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland180619a2010-11-15 00:52:26 +0100318 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
319 combine_notes);
320 if (ret)
321 return ret;
Johan Herland23123ae2009-10-09 12:22:07 +0200322 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
Johan Herland180619a2010-11-15 00:52:26 +0100323 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
Johan Herlandef8db632009-10-09 12:22:09 +0200324}
325
Johan Herland23123ae2009-10-09 12:22:07 +0200326/* Free the entire notes data contained in the given tree */
327static void note_tree_free(struct int_node *tree)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200328{
Johan Herland23123ae2009-10-09 12:22:07 +0200329 unsigned int i;
330 for (i = 0; i < 16; i++) {
331 void *p = tree->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100332 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200333 case PTR_TYPE_INTERNAL:
334 note_tree_free(CLR_PTR_TYPE(p));
335 /* fall through */
336 case PTR_TYPE_NOTE:
337 case PTR_TYPE_SUBTREE:
338 free(CLR_PTR_TYPE(p));
339 }
340 }
341}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200342
Johan Herland851c2b32010-02-13 22:28:23 +0100343static int non_note_cmp(const struct non_note *a, const struct non_note *b)
344{
345 return strcmp(a->path, b->path);
346}
347
Jeff Kingc29edfe2015-08-19 14:12:41 -0400348/* note: takes ownership of path string */
349static void add_non_note(struct notes_tree *t, char *path,
Johan Herland851c2b32010-02-13 22:28:23 +0100350 unsigned int mode, const unsigned char *sha1)
351{
352 struct non_note *p = t->prev_non_note, *n;
353 n = (struct non_note *) xmalloc(sizeof(struct non_note));
354 n->next = NULL;
Jeff Kingc29edfe2015-08-19 14:12:41 -0400355 n->path = path;
Johan Herland851c2b32010-02-13 22:28:23 +0100356 n->mode = mode;
brian m. carlson92e2cab2021-04-26 01:02:50 +0000357 oidread(&n->oid, sha1);
Johan Herland851c2b32010-02-13 22:28:23 +0100358 t->prev_non_note = n;
359
360 if (!t->first_non_note) {
361 t->first_non_note = n;
362 return;
363 }
364
365 if (non_note_cmp(p, n) < 0)
366 ; /* do nothing */
367 else if (non_note_cmp(t->first_non_note, n) <= 0)
368 p = t->first_non_note;
369 else {
370 /* n sorts before t->first_non_note */
371 n->next = t->first_non_note;
372 t->first_non_note = n;
373 return;
374 }
375
376 /* n sorts equal or after p */
377 while (p->next && non_note_cmp(p->next, n) <= 0)
378 p = p->next;
379
380 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
381 assert(strcmp(p->path, n->path) == 0);
382 p->mode = n->mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700383 oidcpy(&p->oid, &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100384 free(n);
385 t->prev_non_note = p;
386 return;
387 }
388
389 /* n sorts between p and p->next */
390 n->next = p->next;
391 p->next = n;
392}
393
394static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
395 struct int_node *node, unsigned int n)
Johan Herland23123ae2009-10-09 12:22:07 +0200396{
brian m. carlson89c149f2017-05-30 10:30:38 -0700397 struct object_id object_oid;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200398 size_t prefix_len;
Johan Herland23123ae2009-10-09 12:22:07 +0200399 void *buf;
400 struct tree_desc desc;
401 struct name_entry entry;
brian m. carlsondd437452019-02-19 00:05:01 +0000402 const unsigned hashsz = the_hash_algo->rawsz;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200403
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700404 buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
Johan Herland23123ae2009-10-09 12:22:07 +0200405 if (!buf)
406 die("Could not read %s for notes-index",
brian m. carlson5dcc9692017-05-30 10:30:37 -0700407 oid_to_hex(&subtree->val_oid));
Johan Herland23123ae2009-10-09 12:22:07 +0200408
brian m. carlson89c149f2017-05-30 10:30:38 -0700409 prefix_len = subtree->key_oid.hash[KEY_INDEX];
brian m. carlsondd437452019-02-19 00:05:01 +0000410 if (prefix_len >= hashsz)
Michael Haggerty39642812017-09-08 18:10:10 +0200411 BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
412 if (prefix_len * 2 < n)
413 BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
brian m. carlson89c149f2017-05-30 10:30:38 -0700414 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
Johan Herland23123ae2009-10-09 12:22:07 +0200415 while (tree_entry(&desc, &entry)) {
Michael Haggertya2816392017-08-26 10:28:03 +0200416 unsigned char type;
417 struct leaf_node *l;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200418 size_t path_len = strlen(entry.path);
Johan Herland23123ae2009-10-09 12:22:07 +0200419
brian m. carlsondd437452019-02-19 00:05:01 +0000420 if (path_len == 2 * (hashsz - prefix_len)) {
Michael Haggerty98c98972017-08-26 10:28:05 +0200421 /* This is potentially the remainder of the SHA-1 */
Michael Haggerty40432182017-08-26 10:28:07 +0200422
423 if (!S_ISREG(entry.mode))
424 /* notes must be blobs */
425 goto handle_non_note;
426
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200427 if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
brian m. carlsondd437452019-02-19 00:05:01 +0000428 hashsz - prefix_len))
Michael Haggerty98c98972017-08-26 10:28:05 +0200429 goto handle_non_note; /* entry.path is not a SHA1 */
Johan Herland23123ae2009-10-09 12:22:07 +0200430
Johan Herland851c2b32010-02-13 22:28:23 +0100431 type = PTR_TYPE_NOTE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200432 } else if (path_len == 2) {
433 /* This is potentially an internal node */
Michael Haggertyd49852d2017-08-26 10:28:10 +0200434 size_t len = prefix_len;
Michael Haggerty4d589b82017-08-26 10:28:06 +0200435
436 if (!S_ISDIR(entry.mode))
437 /* internal nodes must be trees */
438 goto handle_non_note;
439
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200440 if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
Michael Haggerty98c98972017-08-26 10:28:05 +0200441 goto handle_non_note; /* entry.path is not a SHA1 */
442
Michael Haggertyd49852d2017-08-26 10:28:10 +0200443 /*
444 * Pad the rest of the SHA-1 with zeros,
445 * except for the last byte, where we write
446 * the length:
447 */
brian m. carlsondd437452019-02-19 00:05:01 +0000448 memset(object_oid.hash + len, 0, hashsz - len - 1);
Michael Haggertyd49852d2017-08-26 10:28:10 +0200449 object_oid.hash[KEY_INDEX] = (unsigned char)len;
Michael Haggerty4ebef532017-08-26 10:28:09 +0200450
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200451 type = PTR_TYPE_SUBTREE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200452 } else {
453 /* This can't be part of a note */
454 goto handle_non_note;
Johan Herland23123ae2009-10-09 12:22:07 +0200455 }
Michael Haggerty98c98972017-08-26 10:28:05 +0200456
René Scharfeca56dad2021-03-13 17:17:22 +0100457 CALLOC_ARRAY(l, 1);
Michael Haggerty4ebef532017-08-26 10:28:09 +0200458 oidcpy(&l->key_oid, &object_oid);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000459 oidcpy(&l->val_oid, &entry.oid);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000460 oid_set_algo(&l->key_oid, the_hash_algo);
461 oid_set_algo(&l->val_oid, the_hash_algo);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200462 if (note_tree_insert(t, node, n, l, type,
463 combine_notes_concatenate))
464 die("Failed to load %s %s into notes tree "
465 "from %s",
466 type == PTR_TYPE_NOTE ? "note" : "subtree",
Jeff King60fe4772019-08-25 03:19:51 -0400467 oid_to_hex(&object_oid), t->ref);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200468
Johan Herland851c2b32010-02-13 22:28:23 +0100469 continue;
470
471handle_non_note:
472 /*
Michael Haggertycbeed9a2017-08-26 10:28:04 +0200473 * Determine full path for this non-note entry. The
474 * filename is already found in entry.path, but the
475 * directory part of the path must be deduced from the
476 * subtree containing this entry based on our
477 * knowledge that the overall notes tree follows a
478 * strict byte-based progressive fanout structure
479 * (i.e. using 2/38, 2/2/36, etc. fanouts).
Johan Herland851c2b32010-02-13 22:28:23 +0100480 */
481 {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400482 struct strbuf non_note_path = STRBUF_INIT;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700483 const char *q = oid_to_hex(&subtree->key_oid);
Michael Haggerty06cfa752017-08-26 10:28:12 +0200484 size_t i;
Johan Herland851c2b32010-02-13 22:28:23 +0100485 for (i = 0; i < prefix_len; i++) {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400486 strbuf_addch(&non_note_path, *q++);
487 strbuf_addch(&non_note_path, *q++);
488 strbuf_addch(&non_note_path, '/');
Johan Herland851c2b32010-02-13 22:28:23 +0100489 }
Jeff Kingc29edfe2015-08-19 14:12:41 -0400490 strbuf_addstr(&non_note_path, entry.path);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000491 oid_set_algo(&entry.oid, the_hash_algo);
Jeff Kingc29edfe2015-08-19 14:12:41 -0400492 add_non_note(t, strbuf_detach(&non_note_path, NULL),
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000493 entry.mode, entry.oid.hash);
Johan Herland851c2b32010-02-13 22:28:23 +0100494 }
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200495 }
Johan Herland23123ae2009-10-09 12:22:07 +0200496 free(buf);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200497}
498
Johan Herland73f77b92010-02-13 22:28:16 +0100499/*
500 * Determine optimal on-disk fanout for this part of the notes tree
501 *
502 * Given a (sub)tree and the level in the internal tree structure, determine
503 * whether or not the given existing fanout should be expanded for this
504 * (sub)tree.
505 *
506 * Values of the 'fanout' variable:
507 * - 0: No fanout (all notes are stored directly in the root notes tree)
508 * - 1: 2/38 fanout
509 * - 2: 2/2/36 fanout
510 * - 3: 2/2/2/34 fanout
511 * etc.
512 */
513static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
514 unsigned char fanout)
515{
516 /*
517 * The following is a simple heuristic that works well in practice:
518 * For each even-numbered 16-tree level (remember that each on-disk
519 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
520 * entries at that tree level. If all of them are either int_nodes or
521 * subtree entries, then there are likely plenty of notes below this
522 * level, so we return an incremented fanout.
523 */
524 unsigned int i;
525 if ((n % 2) || (n > 2 * fanout))
526 return fanout;
527 for (i = 0; i < 16; i++) {
528 switch (GET_PTR_TYPE(tree->a[i])) {
529 case PTR_TYPE_SUBTREE:
530 case PTR_TYPE_INTERNAL:
531 continue;
532 default:
533 return fanout;
534 }
535 }
536 return fanout + 1;
537}
538
brian m. carlsondd437452019-02-19 00:05:01 +0000539/* hex oid + '/' between each pair of hex digits + NUL */
540#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
Jeff King02e32b72015-09-24 17:08:24 -0400541
brian m. carlson0dbc6462019-02-19 00:05:02 +0000542static void construct_path_with_fanout(const unsigned char *hash,
Johan Herland73f77b92010-02-13 22:28:16 +0100543 unsigned char fanout, char *path)
544{
545 unsigned int i = 0, j = 0;
brian m. carlson0dbc6462019-02-19 00:05:02 +0000546 const char *hex_hash = hash_to_hex(hash);
brian m. carlsondd437452019-02-19 00:05:01 +0000547 assert(fanout < the_hash_algo->rawsz);
Johan Herland73f77b92010-02-13 22:28:16 +0100548 while (fanout) {
brian m. carlson0dbc6462019-02-19 00:05:02 +0000549 path[i++] = hex_hash[j++];
550 path[i++] = hex_hash[j++];
Johan Herland73f77b92010-02-13 22:28:16 +0100551 path[i++] = '/';
552 fanout--;
553 }
brian m. carlson0dbc6462019-02-19 00:05:02 +0000554 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
Johan Herland73f77b92010-02-13 22:28:16 +0100555}
556
Johan Herland851c2b32010-02-13 22:28:23 +0100557static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
558 unsigned char n, unsigned char fanout, int flags,
559 each_note_fn fn, void *cb_data)
Johan Herland73f77b92010-02-13 22:28:16 +0100560{
561 unsigned int i;
562 void *p;
563 int ret = 0;
564 struct leaf_node *l;
Jeff King02e32b72015-09-24 17:08:24 -0400565 static char path[FANOUT_PATH_MAX];
Johan Herland73f77b92010-02-13 22:28:16 +0100566
567 fanout = determine_fanout(tree, n, fanout);
568 for (i = 0; i < 16; i++) {
569redo:
570 p = tree->a[i];
571 switch (GET_PTR_TYPE(p)) {
572 case PTR_TYPE_INTERNAL:
573 /* recurse into int_node */
Johan Herland851c2b32010-02-13 22:28:23 +0100574 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
Johan Herland73f77b92010-02-13 22:28:16 +0100575 fanout, flags, fn, cb_data);
576 break;
577 case PTR_TYPE_SUBTREE:
578 l = (struct leaf_node *) CLR_PTR_TYPE(p);
579 /*
580 * Subtree entries in the note tree represent parts of
581 * the note tree that have not yet been explored. There
582 * is a direct relationship between subtree entries at
583 * level 'n' in the tree, and the 'fanout' variable:
Johan Herlanddbc27472020-02-03 22:04:45 +0100584 * Subtree entries at level 'n < 2 * fanout' should be
Johan Herland73f77b92010-02-13 22:28:16 +0100585 * preserved, since they correspond exactly to a fanout
586 * directory in the on-disk structure. However, subtree
Johan Herlanddbc27472020-02-03 22:04:45 +0100587 * entries at level 'n >= 2 * fanout' should NOT be
Johan Herland73f77b92010-02-13 22:28:16 +0100588 * preserved, but rather consolidated into the above
589 * notes tree level. We achieve this by unconditionally
590 * unpacking subtree entries that exist below the
591 * threshold level at 'n = 2 * fanout'.
592 */
Johan Herlanddbc27472020-02-03 22:04:45 +0100593 if (n < 2 * fanout &&
Johan Herland73f77b92010-02-13 22:28:16 +0100594 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
595 /* invoke callback with subtree */
596 unsigned int path_len =
brian m. carlson89c149f2017-05-30 10:30:38 -0700597 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
Jeff King02e32b72015-09-24 17:08:24 -0400598 assert(path_len < FANOUT_PATH_MAX - 1);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700599 construct_path_with_fanout(l->key_oid.hash,
600 fanout,
Johan Herland73f77b92010-02-13 22:28:16 +0100601 path);
602 /* Create trailing slash, if needed */
603 if (path[path_len - 1] != '/')
604 path[path_len++] = '/';
605 path[path_len] = '\0';
brian m. carlson490bc832017-05-30 10:30:39 -0700606 ret = fn(&l->key_oid, &l->val_oid,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700607 path,
Johan Herland73f77b92010-02-13 22:28:16 +0100608 cb_data);
609 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100610 if (n >= 2 * fanout ||
Johan Herland73f77b92010-02-13 22:28:16 +0100611 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
612 /* unpack subtree and resume traversal */
613 tree->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100614 load_subtree(t, l, tree, n);
Johan Herland73f77b92010-02-13 22:28:16 +0100615 free(l);
616 goto redo;
617 }
618 break;
619 case PTR_TYPE_NOTE:
620 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700621 construct_path_with_fanout(l->key_oid.hash, fanout,
622 path);
brian m. carlson490bc832017-05-30 10:30:39 -0700623 ret = fn(&l->key_oid, &l->val_oid, path,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700624 cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +0100625 break;
626 }
627 if (ret)
628 return ret;
629 }
630 return 0;
631}
632
Johan Herland61a7cca2010-02-13 22:28:17 +0100633struct tree_write_stack {
634 struct tree_write_stack *next;
635 struct strbuf buf;
636 char path[2]; /* path to subtree in next, if any */
637};
638
639static inline int matches_tree_write_stack(struct tree_write_stack *tws,
640 const char *full_path)
641{
642 return full_path[0] == tws->path[0] &&
643 full_path[1] == tws->path[1] &&
644 full_path[2] == '/';
645}
646
647static void write_tree_entry(struct strbuf *buf, unsigned int mode,
648 const char *path, unsigned int path_len, const
brian m. carlsondd437452019-02-19 00:05:01 +0000649 unsigned char *hash)
Johan Herland61a7cca2010-02-13 22:28:17 +0100650{
Junio C Hamanoc88f0cc2010-02-24 21:39:06 -0800651 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
brian m. carlsondd437452019-02-19 00:05:01 +0000652 strbuf_add(buf, hash, the_hash_algo->rawsz);
Johan Herland61a7cca2010-02-13 22:28:17 +0100653}
654
655static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
656 const char *path)
657{
658 struct tree_write_stack *n;
659 assert(!tws->next);
660 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
661 n = (struct tree_write_stack *)
662 xmalloc(sizeof(struct tree_write_stack));
663 n->next = NULL;
brian m. carlsondd437452019-02-19 00:05:01 +0000664 strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
Johan Herland61a7cca2010-02-13 22:28:17 +0100665 n->path[0] = n->path[1] = '\0';
666 tws->next = n;
667 tws->path[0] = path[0];
668 tws->path[1] = path[1];
669}
670
671static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
672{
673 int ret;
674 struct tree_write_stack *n = tws->next;
brian m. carlson89c149f2017-05-30 10:30:38 -0700675 struct object_id s;
Johan Herland61a7cca2010-02-13 22:28:17 +0100676 if (n) {
677 ret = tree_write_stack_finish_subtree(n);
678 if (ret)
679 return ret;
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100680 ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s);
Johan Herland61a7cca2010-02-13 22:28:17 +0100681 if (ret)
682 return ret;
683 strbuf_release(&n->buf);
684 free(n);
685 tws->next = NULL;
brian m. carlson89c149f2017-05-30 10:30:38 -0700686 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100687 tws->path[0] = tws->path[1] = '\0';
688 }
689 return 0;
690}
691
692static int write_each_note_helper(struct tree_write_stack *tws,
693 const char *path, unsigned int mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700694 const struct object_id *oid)
Johan Herland61a7cca2010-02-13 22:28:17 +0100695{
696 size_t path_len = strlen(path);
697 unsigned int n = 0;
698 int ret;
699
700 /* Determine common part of tree write stack */
701 while (tws && 3 * n < path_len &&
702 matches_tree_write_stack(tws, path + 3 * n)) {
703 n++;
704 tws = tws->next;
705 }
706
707 /* tws point to last matching tree_write_stack entry */
708 ret = tree_write_stack_finish_subtree(tws);
709 if (ret)
710 return ret;
711
712 /* Start subtrees needed to satisfy path */
713 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
714 tree_write_stack_init_subtree(tws, path + 3 * n);
715 n++;
716 tws = tws->next;
717 }
718
719 /* There should be no more directory components in the given path */
720 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
721
722 /* Finally add given entry to the current tree object */
723 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
brian m. carlson490bc832017-05-30 10:30:39 -0700724 oid->hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100725
726 return 0;
727}
728
729struct write_each_note_data {
730 struct tree_write_stack *root;
Johan Herlanddbc27472020-02-03 22:04:45 +0100731 struct non_note **nn_list;
732 struct non_note *nn_prev;
Johan Herland61a7cca2010-02-13 22:28:17 +0100733};
734
Johan Herland851c2b32010-02-13 22:28:23 +0100735static int write_each_non_note_until(const char *note_path,
736 struct write_each_note_data *d)
737{
Johan Herlanddbc27472020-02-03 22:04:45 +0100738 struct non_note *p = d->nn_prev;
739 struct non_note *n = p ? p->next : *d->nn_list;
Ramsay Jones89fe1212010-06-21 19:52:29 +0100740 int cmp = 0, ret;
Johan Herland851c2b32010-02-13 22:28:23 +0100741 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
742 if (note_path && cmp == 0)
743 ; /* do nothing, prefer note to non-note */
744 else {
745 ret = write_each_note_helper(d->root, n->path, n->mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700746 &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100747 if (ret)
748 return ret;
749 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100750 p = n;
Johan Herland851c2b32010-02-13 22:28:23 +0100751 n = n->next;
752 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100753 d->nn_prev = p;
Johan Herland851c2b32010-02-13 22:28:23 +0100754 return 0;
755}
756
Jeff King3c50c882023-02-24 01:39:31 -0500757static int write_each_note(const struct object_id *object_oid UNUSED,
brian m. carlson490bc832017-05-30 10:30:39 -0700758 const struct object_id *note_oid, char *note_path,
Johan Herland61a7cca2010-02-13 22:28:17 +0100759 void *cb_data)
760{
761 struct write_each_note_data *d =
762 (struct write_each_note_data *) cb_data;
763 size_t note_path_len = strlen(note_path);
764 unsigned int mode = 0100644;
765
766 if (note_path[note_path_len - 1] == '/') {
767 /* subtree entry */
768 note_path_len--;
769 note_path[note_path_len] = '\0';
770 mode = 040000;
771 }
brian m. carlsondd437452019-02-19 00:05:01 +0000772 assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
Johan Herland61a7cca2010-02-13 22:28:17 +0100773
Johan Herland851c2b32010-02-13 22:28:23 +0100774 /* Weave non-note entries into note entries */
775 return write_each_non_note_until(note_path, d) ||
brian m. carlson490bc832017-05-30 10:30:39 -0700776 write_each_note_helper(d->root, note_path, mode, note_oid);
Johan Herland61a7cca2010-02-13 22:28:17 +0100777}
778
Johan Herland00fbe632010-02-13 22:28:27 +0100779struct note_delete_list {
780 struct note_delete_list *next;
781 const unsigned char *sha1;
782};
783
brian m. carlson490bc832017-05-30 10:30:39 -0700784static int prune_notes_helper(const struct object_id *object_oid,
Jeff King3c50c882023-02-24 01:39:31 -0500785 const struct object_id *note_oid UNUSED,
786 char *note_path UNUSED,
787 void *cb_data)
Johan Herland00fbe632010-02-13 22:28:27 +0100788{
789 struct note_delete_list **l = (struct note_delete_list **) cb_data;
790 struct note_delete_list *n;
791
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200792 if (repo_has_object_file(the_repository, object_oid))
Johan Herland00fbe632010-02-13 22:28:27 +0100793 return 0; /* nothing to do for this note */
794
795 /* failed to find object => prune this note */
796 n = (struct note_delete_list *) xmalloc(sizeof(*n));
797 n->next = *l;
brian m. carlson490bc832017-05-30 10:30:39 -0700798 n->sha1 = object_oid->hash;
Johan Herland00fbe632010-02-13 22:28:27 +0100799 *l = n;
800 return 0;
801}
802
Patryk Obarab7d591d2018-01-28 01:13:17 +0100803int combine_notes_concatenate(struct object_id *cur_oid,
804 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100805{
806 char *cur_msg = NULL, *new_msg = NULL, *buf;
807 unsigned long cur_len, new_len, buf_len;
808 enum object_type cur_type, new_type;
809 int ret;
810
811 /* read in both note blob objects */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100812 if (!is_null_oid(new_oid))
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200813 new_msg = repo_read_object_file(the_repository, new_oid,
814 &new_type, &new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100815 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
816 free(new_msg);
817 return 0;
818 }
Patryk Obarab7d591d2018-01-28 01:13:17 +0100819 if (!is_null_oid(cur_oid))
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200820 cur_msg = repo_read_object_file(the_repository, cur_oid,
821 &cur_type, &cur_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100822 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
823 free(cur_msg);
824 free(new_msg);
Patryk Obarab7d591d2018-01-28 01:13:17 +0100825 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100826 return 0;
827 }
828
Johan Herlandd4990c42010-11-09 22:49:44 +0100829 /* we will separate the notes by two newlines anyway */
Johan Herland73f464b2010-02-13 22:28:19 +0100830 if (cur_msg[cur_len - 1] == '\n')
831 cur_len--;
832
833 /* concatenate cur_msg and new_msg into buf */
Johan Herlandd4990c42010-11-09 22:49:44 +0100834 buf_len = cur_len + 2 + new_len;
Johan Herland73f464b2010-02-13 22:28:19 +0100835 buf = (char *) xmalloc(buf_len);
836 memcpy(buf, cur_msg, cur_len);
837 buf[cur_len] = '\n';
Johan Herlandd4990c42010-11-09 22:49:44 +0100838 buf[cur_len + 1] = '\n';
839 memcpy(buf + cur_len + 2, new_msg, new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100840 free(cur_msg);
841 free(new_msg);
842
843 /* create a new blob object from buf */
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100844 ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100845 free(buf);
846 return ret;
847}
848
Patryk Obarab7d591d2018-01-28 01:13:17 +0100849int combine_notes_overwrite(struct object_id *cur_oid,
850 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100851{
Patryk Obarab7d591d2018-01-28 01:13:17 +0100852 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100853 return 0;
854}
855
Jeff King3c50c882023-02-24 01:39:31 -0500856int combine_notes_ignore(struct object_id *cur_oid UNUSED,
857 const struct object_id *new_oid UNUSED)
Johan Herland73f464b2010-02-13 22:28:19 +0100858{
859 return 0;
860}
861
Michael Haggerty13135242012-11-04 08:07:08 +0100862/*
863 * Add the lines from the named object to list, with trailing
864 * newlines removed.
865 */
866static int string_list_add_note_lines(struct string_list *list,
Patryk Obarab7d591d2018-01-28 01:13:17 +0100867 const struct object_id *oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100868{
869 char *data;
870 unsigned long len;
871 enum object_type t;
Johan Herlanda6a09092010-11-15 00:57:17 +0100872
Patryk Obarab7d591d2018-01-28 01:13:17 +0100873 if (is_null_oid(oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100874 return 0;
875
876 /* read_sha1_file NUL-terminates */
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200877 data = repo_read_object_file(the_repository, oid, &t, &len);
Johan Herlanda6a09092010-11-15 00:57:17 +0100878 if (t != OBJ_BLOB || !data || !len) {
879 free(data);
880 return t != OBJ_BLOB || !data;
881 }
882
Michael Haggerty13135242012-11-04 08:07:08 +0100883 /*
884 * If the last line of the file is EOL-terminated, this will
885 * add an empty string to the list. But it will be removed
886 * later, along with any empty strings that came from empty
887 * lines within the file.
888 */
889 string_list_split(list, data, '\n', -1);
890 free(data);
Johan Herlanda6a09092010-11-15 00:57:17 +0100891 return 0;
892}
893
894static int string_list_join_lines_helper(struct string_list_item *item,
895 void *cb_data)
896{
897 struct strbuf *buf = cb_data;
898 strbuf_addstr(buf, item->string);
899 strbuf_addch(buf, '\n');
900 return 0;
901}
902
Patryk Obarab7d591d2018-01-28 01:13:17 +0100903int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
904 const struct object_id *new_oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100905{
Michael Haggertyf992f0c2012-11-04 08:07:07 +0100906 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
Johan Herlanda6a09092010-11-15 00:57:17 +0100907 struct strbuf buf = STRBUF_INIT;
908 int ret = 1;
909
910 /* read both note blob objects into unique_lines */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100911 if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100912 goto out;
Patryk Obarab7d591d2018-01-28 01:13:17 +0100913 if (string_list_add_note_lines(&sort_uniq_list, new_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100914 goto out;
Michael Haggerty13135242012-11-04 08:07:08 +0100915 string_list_remove_empty_items(&sort_uniq_list, 0);
Michael Haggerty3383e192014-11-25 09:02:35 +0100916 string_list_sort(&sort_uniq_list);
Michael Haggerty13135242012-11-04 08:07:08 +0100917 string_list_remove_duplicates(&sort_uniq_list, 0);
Johan Herlanda6a09092010-11-15 00:57:17 +0100918
919 /* create a new blob object from sort_uniq_list */
920 if (for_each_string_list(&sort_uniq_list,
921 string_list_join_lines_helper, &buf))
922 goto out;
923
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100924 ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid);
Johan Herlanda6a09092010-11-15 00:57:17 +0100925
926out:
927 strbuf_release(&buf);
928 string_list_clear(&sort_uniq_list, 0);
929 return ret;
930}
931
Jeff King63e14ee2022-08-19 06:08:32 -0400932static int string_list_add_one_ref(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200933 const struct object_id *oid UNUSED,
934 int flag UNUSED, void *cb)
Thomas Rast894a9d32010-03-12 18:04:26 +0100935{
936 struct string_list *refs = cb;
Michael Haggertyd235e992013-05-25 11:08:20 +0200937 if (!unsorted_string_list_has_string(refs, refname))
938 string_list_append(refs, refname);
Thomas Rast894a9d32010-03-12 18:04:26 +0100939 return 0;
940}
941
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200942/*
943 * The list argument must have strdup_strings set on it.
944 */
Thomas Rast894a9d32010-03-12 18:04:26 +0100945void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
946{
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200947 assert(list->strdup_strings);
Thomas Rast894a9d32010-03-12 18:04:26 +0100948 if (has_glob_specials(glob)) {
Michael Haggertyfd950352015-05-25 18:38:59 +0000949 for_each_glob_ref(string_list_add_one_ref, glob, list);
Thomas Rast894a9d32010-03-12 18:04:26 +0100950 } else {
brian m. carlson89c149f2017-05-30 10:30:38 -0700951 struct object_id oid;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200952 if (repo_get_oid(the_repository, glob, &oid))
Thomas Rast894a9d32010-03-12 18:04:26 +0100953 warning("notes ref %s is invalid", glob);
954 if (!unsorted_string_list_has_string(list, glob))
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100955 string_list_append(list, glob);
Thomas Rast894a9d32010-03-12 18:04:26 +0100956 }
957}
958
959void string_list_add_refs_from_colon_sep(struct string_list *list,
960 const char *globs)
961{
Michael Haggerty6fa23772012-11-04 08:07:10 +0100962 struct string_list split = STRING_LIST_INIT_NODUP;
963 char *globs_copy = xstrdup(globs);
Thomas Rast894a9d32010-03-12 18:04:26 +0100964 int i;
965
Michael Haggerty6fa23772012-11-04 08:07:10 +0100966 string_list_split_in_place(&split, globs_copy, ':', -1);
967 string_list_remove_empty_items(&split, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +0100968
Michael Haggerty6fa23772012-11-04 08:07:10 +0100969 for (i = 0; i < split.nr; i++)
970 string_list_add_refs_by_glob(list, split.items[i].string);
Thomas Rast894a9d32010-03-12 18:04:26 +0100971
Michael Haggerty6fa23772012-11-04 08:07:10 +0100972 string_list_clear(&split, 0);
973 free(globs_copy);
Thomas Rast894a9d32010-03-12 18:04:26 +0100974}
975
Thomas Rast894a9d32010-03-12 18:04:26 +0100976static int notes_display_config(const char *k, const char *v, void *cb)
977{
978 int *load_refs = cb;
979
980 if (*load_refs && !strcmp(k, "notes.displayref")) {
981 if (!v)
Nate Aversc3eb95a2020-11-22 22:23:41 -0500982 return config_error_nonbool(k);
Thomas Rast894a9d32010-03-12 18:04:26 +0100983 string_list_add_refs_by_glob(&display_notes_refs, v);
984 }
985
986 return 0;
987}
988
Johan Herland4a9cf1c2010-11-09 22:49:39 +0100989const char *default_notes_ref(void)
Thomas Rast894a9d32010-03-12 18:04:26 +0100990{
991 const char *notes_ref = NULL;
992 if (!notes_ref)
993 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
994 if (!notes_ref)
995 notes_ref = notes_ref_name; /* value of core.notesRef config */
996 if (!notes_ref)
997 notes_ref = GIT_NOTES_DEFAULT_REF;
998 return notes_ref;
999}
1000
Johan Herland73f464b2010-02-13 22:28:19 +01001001void init_notes(struct notes_tree *t, const char *notes_ref,
1002 combine_notes_fn combine_notes, int flags)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001003{
brian m. carlson13ac1412016-09-05 20:08:05 +00001004 struct object_id oid, object_oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -07001005 unsigned short mode;
Johan Herland23123ae2009-10-09 12:22:07 +02001006 struct leaf_node root_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001007
Johan Herlandcd305392010-02-13 22:28:18 +01001008 if (!t)
1009 t = &default_notes_tree;
1010 assert(!t->initialized);
Johan Herland709f79b2010-02-13 22:28:12 +01001011
1012 if (!notes_ref)
Thomas Rast894a9d32010-03-12 18:04:26 +01001013 notes_ref = default_notes_ref();
Derrick Stoleeb9342b32022-08-05 17:58:36 +00001014 update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
Johan Herland709f79b2010-02-13 22:28:12 +01001015
Johan Herland73f464b2010-02-13 22:28:19 +01001016 if (!combine_notes)
1017 combine_notes = combine_notes_concatenate;
1018
Brian Gesiak65bbf082014-05-27 00:33:52 +09001019 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland851c2b32010-02-13 22:28:23 +01001020 t->first_non_note = NULL;
1021 t->prev_non_note = NULL;
Jeff King8c53f072015-01-12 20:59:09 -05001022 t->ref = xstrdup_or_null(notes_ref);
Mike Hommeyee76f922015-10-08 11:54:43 +09001023 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
Johan Herland73f464b2010-02-13 22:28:19 +01001024 t->combine_notes = combine_notes;
Johan Herlandcd305392010-02-13 22:28:18 +01001025 t->initialized = 1;
Thomas Rast7f710ea2010-03-12 18:04:36 +01001026 t->dirty = 0;
Johan Herlandcd305392010-02-13 22:28:18 +01001027
Johan Herland709f79b2010-02-13 22:28:12 +01001028 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001029 repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001030 return;
brian m. carlson34c290a2017-10-15 22:06:56 +00001031 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
Mike Hommeyee76f922015-10-08 11:54:43 +09001032 die("Cannot use notes ref %s", notes_ref);
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001033 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
Johan Herland709f79b2010-02-13 22:28:12 +01001034 die("Failed to read notes tree referenced by %s (%s)",
brian m. carlson13ac1412016-09-05 20:08:05 +00001035 notes_ref, oid_to_hex(&object_oid));
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001036
brian m. carlson5dcc9692017-05-30 10:30:37 -07001037 oidclr(&root_tree.key_oid);
1038 oidcpy(&root_tree.val_oid, &oid);
Johan Herland851c2b32010-02-13 22:28:23 +01001039 load_subtree(t, &root_tree, t->root, 0);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001040}
1041
Mike Hommeyee76f922015-10-08 11:54:43 +09001042struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
Thomas Rast894a9d32010-03-12 18:04:26 +01001043{
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001044 struct string_list_item *item;
1045 int counter = 0;
Thomas Rast894a9d32010-03-12 18:04:26 +01001046 struct notes_tree **trees;
Jeff Kingb32fa952016-02-22 17:44:25 -05001047 ALLOC_ARRAY(trees, refs->nr + 1);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001048 for_each_string_list_item(item, refs) {
1049 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
Mike Hommeyee76f922015-10-08 11:54:43 +09001050 init_notes(t, item->string, combine_notes_ignore, flags);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001051 trees[counter++] = t;
1052 }
1053 trees[counter] = NULL;
Thomas Rast894a9d32010-03-12 18:04:26 +01001054 return trees;
1055}
1056
1057void init_display_notes(struct display_notes_opt *opt)
1058{
Denton Liue6e230e2019-12-09 05:10:41 -08001059 memset(opt, 0, sizeof(*opt));
1060 opt->use_default_notes = -1;
1061}
1062
Denton Liu1d729752019-12-11 16:49:50 -08001063void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
Denton Liu452538c2019-12-09 05:10:44 -08001064{
Denton Liu1d729752019-12-11 16:49:50 -08001065 opt->use_default_notes = 1;
1066 *show_notes = 1;
1067}
Denton Liu452538c2019-12-09 05:10:44 -08001068
Denton Liu1d729752019-12-11 16:49:50 -08001069void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1070 const char *ref) {
1071 struct strbuf buf = STRBUF_INIT;
1072 strbuf_addstr(&buf, ref);
1073 expand_notes_ref(&buf);
1074 string_list_append(&opt->extra_notes_refs,
1075 strbuf_detach(&buf, NULL));
1076 *show_notes = 1;
1077}
1078
1079void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1080{
1081 opt->use_default_notes = -1;
1082 /* we have been strdup'ing ourselves, so trick
1083 * string_list into free()ing strings */
1084 opt->extra_notes_refs.strdup_strings = 1;
1085 string_list_clear(&opt->extra_notes_refs, 0);
1086 opt->extra_notes_refs.strdup_strings = 0;
1087 *show_notes = 0;
Denton Liu452538c2019-12-09 05:10:44 -08001088}
1089
Denton Liu1e6ed542019-12-09 05:10:39 -08001090void load_display_notes(struct display_notes_opt *opt)
Thomas Rast894a9d32010-03-12 18:04:26 +01001091{
1092 char *display_ref_env;
1093 int load_config_refs = 0;
1094 display_notes_refs.strdup_strings = 1;
1095
1096 assert(!display_notes_trees);
1097
Jeff King3a03cf62011-03-29 16:57:27 -04001098 if (!opt || opt->use_default_notes > 0 ||
1099 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001100 string_list_append(&display_notes_refs, default_notes_ref());
Thomas Rast894a9d32010-03-12 18:04:26 +01001101 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1102 if (display_ref_env) {
1103 string_list_add_refs_from_colon_sep(&display_notes_refs,
1104 display_ref_env);
1105 load_config_refs = 0;
1106 } else
1107 load_config_refs = 1;
1108 }
1109
1110 git_config(notes_display_config, &load_config_refs);
1111
Jeff King304cc112011-03-29 16:56:53 -04001112 if (opt) {
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001113 struct string_list_item *item;
Jeff King304cc112011-03-29 16:56:53 -04001114 for_each_string_list_item(item, &opt->extra_notes_refs)
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001115 string_list_add_refs_by_glob(&display_notes_refs,
1116 item->string);
1117 }
Thomas Rast894a9d32010-03-12 18:04:26 +01001118
Mike Hommeyee76f922015-10-08 11:54:43 +09001119 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +01001120 string_list_clear(&display_notes_refs, 0);
1121}
1122
brian m. carlson5ee8a952017-05-30 10:30:43 -07001123int add_note(struct notes_tree *t, const struct object_id *object_oid,
1124 const struct object_id *note_oid, combine_notes_fn combine_notes)
Johan Herland2626b532010-02-13 22:28:13 +01001125{
1126 struct leaf_node *l;
1127
Johan Herlandcd305392010-02-13 22:28:18 +01001128 if (!t)
1129 t = &default_notes_tree;
1130 assert(t->initialized);
Thomas Rast7f710ea2010-03-12 18:04:36 +01001131 t->dirty = 1;
Johan Herland73f464b2010-02-13 22:28:19 +01001132 if (!combine_notes)
1133 combine_notes = t->combine_notes;
Johan Herland2626b532010-02-13 22:28:13 +01001134 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
brian m. carlson5ee8a952017-05-30 10:30:43 -07001135 oidcpy(&l->key_oid, object_oid);
1136 oidcpy(&l->val_oid, note_oid);
Johan Herland180619a2010-11-15 00:52:26 +01001137 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
Johan Herland2626b532010-02-13 22:28:13 +01001138}
1139
Johan Herland1ee1e432010-08-31 17:56:50 +02001140int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
Johan Herland1ec666b2010-02-13 22:28:14 +01001141{
1142 struct leaf_node l;
1143
Johan Herlandcd305392010-02-13 22:28:18 +01001144 if (!t)
1145 t = &default_notes_tree;
1146 assert(t->initialized);
brian m. carlson92e2cab2021-04-26 01:02:50 +00001147 oidread(&l.key_oid, object_sha1);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001148 oidclr(&l.val_oid);
Brandon Caseya502ab92010-03-18 10:03:43 -05001149 note_tree_remove(t, t->root, 0, &l);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001150 if (is_null_oid(&l.val_oid)) /* no note was removed */
Johan Herland1ee1e432010-08-31 17:56:50 +02001151 return 1;
1152 t->dirty = 1;
1153 return 0;
Johan Herland1ec666b2010-02-13 22:28:14 +01001154}
1155
brian m. carlson9ef72232017-05-30 10:30:40 -07001156const struct object_id *get_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001157 const struct object_id *oid)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001158{
Johan Herland9b391f22010-02-13 22:28:15 +01001159 struct leaf_node *found;
1160
Johan Herlandcd305392010-02-13 22:28:18 +01001161 if (!t)
1162 t = &default_notes_tree;
1163 assert(t->initialized);
brian m. carlson5ee8a952017-05-30 10:30:43 -07001164 found = note_tree_find(t, t->root, 0, oid->hash);
brian m. carlson9ef72232017-05-30 10:30:40 -07001165 return found ? &found->val_oid : NULL;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001166}
Johannes Schindelina97a7462009-10-09 12:21:57 +02001167
Johan Herlandcd305392010-02-13 22:28:18 +01001168int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1169 void *cb_data)
Johan Herland73f77b92010-02-13 22:28:16 +01001170{
Johan Herlandcd305392010-02-13 22:28:18 +01001171 if (!t)
1172 t = &default_notes_tree;
1173 assert(t->initialized);
Johan Herland851c2b32010-02-13 22:28:23 +01001174 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +01001175}
1176
Patryk Obarabbca96d2018-01-28 01:13:18 +01001177int write_notes_tree(struct notes_tree *t, struct object_id *result)
Johan Herland61a7cca2010-02-13 22:28:17 +01001178{
1179 struct tree_write_stack root;
1180 struct write_each_note_data cb_data;
1181 int ret;
Patryk Obarabbca96d2018-01-28 01:13:18 +01001182 int flags;
Johan Herland61a7cca2010-02-13 22:28:17 +01001183
Johan Herlandcd305392010-02-13 22:28:18 +01001184 if (!t)
1185 t = &default_notes_tree;
1186 assert(t->initialized);
Johan Herland61a7cca2010-02-13 22:28:17 +01001187
1188 /* Prepare for traversal of current notes tree */
1189 root.next = NULL; /* last forward entry in list is grounded */
brian m. carlsondd437452019-02-19 00:05:01 +00001190 strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
Johan Herland61a7cca2010-02-13 22:28:17 +01001191 root.path[0] = root.path[1] = '\0';
1192 cb_data.root = &root;
Johan Herlanddbc27472020-02-03 22:04:45 +01001193 cb_data.nn_list = &(t->first_non_note);
1194 cb_data.nn_prev = NULL;
Johan Herland61a7cca2010-02-13 22:28:17 +01001195
1196 /* Write tree objects representing current notes tree */
Patryk Obarabbca96d2018-01-28 01:13:18 +01001197 flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1198 FOR_EACH_NOTE_YIELD_SUBTREES;
1199 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1200 write_each_non_note_until(NULL, &cb_data) ||
1201 tree_write_stack_finish_subtree(&root) ||
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001202 write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result);
Johan Herland61a7cca2010-02-13 22:28:17 +01001203 strbuf_release(&root.buf);
1204 return ret;
1205}
1206
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001207void prune_notes(struct notes_tree *t, int flags)
Johan Herland00fbe632010-02-13 22:28:27 +01001208{
1209 struct note_delete_list *l = NULL;
1210
1211 if (!t)
1212 t = &default_notes_tree;
1213 assert(t->initialized);
1214
1215 for_each_note(t, 0, prune_notes_helper, &l);
1216
1217 while (l) {
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001218 if (flags & NOTES_PRUNE_VERBOSE)
brian m. carlson0dbc6462019-02-19 00:05:02 +00001219 printf("%s\n", hash_to_hex(l->sha1));
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001220 if (!(flags & NOTES_PRUNE_DRYRUN))
1221 remove_note(t, l->sha1);
Johan Herland00fbe632010-02-13 22:28:27 +01001222 l = l->next;
1223 }
1224}
1225
Johan Herlandcd305392010-02-13 22:28:18 +01001226void free_notes(struct notes_tree *t)
Johan Herland27d57562009-10-09 12:22:06 +02001227{
Johan Herlandcd305392010-02-13 22:28:18 +01001228 if (!t)
1229 t = &default_notes_tree;
1230 if (t->root)
1231 note_tree_free(t->root);
1232 free(t->root);
Johan Herland851c2b32010-02-13 22:28:23 +01001233 while (t->first_non_note) {
1234 t->prev_non_note = t->first_non_note->next;
1235 free(t->first_non_note->path);
1236 free(t->first_non_note);
1237 t->first_non_note = t->prev_non_note;
1238 }
Johan Herlandcd305392010-02-13 22:28:18 +01001239 free(t->ref);
1240 memset(t, 0, sizeof(struct notes_tree));
Johan Herland27d57562009-10-09 12:22:06 +02001241}
1242
Junio C Hamano96531a52012-09-15 14:08:39 -07001243/*
1244 * Fill the given strbuf with the notes associated with the given object.
1245 *
1246 * If the given notes_tree structure is not initialized, it will be auto-
1247 * initialized to the default value (see documentation for init_notes() above).
1248 * If the given notes_tree is NULL, the internal/default notes_tree will be
1249 * used instead.
1250 *
Junio C Hamano76141e22012-10-17 21:41:54 -07001251 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1252 * for human consumption.
Junio C Hamano96531a52012-09-15 14:08:39 -07001253 */
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001254static void format_note(struct notes_tree *t, const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001255 struct strbuf *sb, const char *output_encoding, int raw)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001256{
1257 static const char utf8[] = "utf-8";
brian m. carlson9ef72232017-05-30 10:30:40 -07001258 const struct object_id *oid;
Johannes Schindelina97a7462009-10-09 12:21:57 +02001259 char *msg, *msg_p;
1260 unsigned long linelen, msglen;
1261 enum object_type type;
1262
Johan Herlandcd305392010-02-13 22:28:18 +01001263 if (!t)
1264 t = &default_notes_tree;
1265 if (!t->initialized)
Johan Herland73f464b2010-02-13 22:28:19 +01001266 init_notes(t, NULL, NULL, 0);
Johannes Schindelina97a7462009-10-09 12:21:57 +02001267
brian m. carlson5ee8a952017-05-30 10:30:43 -07001268 oid = get_note(t, object_oid);
brian m. carlson9ef72232017-05-30 10:30:40 -07001269 if (!oid)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001270 return;
1271
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001272 if (!(msg = repo_read_object_file(the_repository, oid, &type, &msglen)) || type != OBJ_BLOB) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001273 free(msg);
1274 return;
1275 }
1276
1277 if (output_encoding && *output_encoding &&
Junio C Hamano0e18bcd2012-10-18 22:41:56 -07001278 !is_encoding_utf8(output_encoding)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001279 char *reencoded = reencode_string(msg, output_encoding, utf8);
1280 if (reencoded) {
1281 free(msg);
1282 msg = reencoded;
1283 msglen = strlen(msg);
1284 }
1285 }
1286
1287 /* we will end the annotation by a newline anyway */
1288 if (msglen && msg[msglen - 1] == '\n')
1289 msglen--;
1290
Junio C Hamano76141e22012-10-17 21:41:54 -07001291 if (!raw) {
Thomas Rast894a9d32010-03-12 18:04:26 +01001292 const char *ref = t->ref;
1293 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1294 strbuf_addstr(sb, "\nNotes:\n");
1295 } else {
Junio C Hamano145136a2020-01-30 11:35:46 -08001296 skip_prefix(ref, "refs/", &ref);
1297 skip_prefix(ref, "notes/", &ref);
Thomas Rast894a9d32010-03-12 18:04:26 +01001298 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1299 }
1300 }
Johannes Schindelina97a7462009-10-09 12:21:57 +02001301
1302 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1303 linelen = strchrnul(msg_p, '\n') - msg_p;
1304
Junio C Hamano76141e22012-10-17 21:41:54 -07001305 if (!raw)
Johan Herlandc56fcc82009-10-09 12:22:04 +02001306 strbuf_addstr(sb, " ");
Johannes Schindelina97a7462009-10-09 12:21:57 +02001307 strbuf_add(sb, msg_p, linelen);
1308 strbuf_addch(sb, '\n');
1309 }
1310
1311 free(msg);
1312}
Thomas Rast894a9d32010-03-12 18:04:26 +01001313
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001314void format_display_notes(const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001315 struct strbuf *sb, const char *output_encoding, int raw)
Thomas Rast894a9d32010-03-12 18:04:26 +01001316{
1317 int i;
1318 assert(display_notes_trees);
1319 for (i = 0; display_notes_trees[i]; i++)
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001320 format_note(display_notes_trees[i], object_oid, sb,
Junio C Hamano76141e22012-10-17 21:41:54 -07001321 output_encoding, raw);
Thomas Rast894a9d32010-03-12 18:04:26 +01001322}
Thomas Rast160baa02010-03-12 18:04:31 +01001323
1324int copy_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001325 const struct object_id *from_obj, const struct object_id *to_obj,
Johan Herland180619a2010-11-15 00:52:26 +01001326 int force, combine_notes_fn combine_notes)
Thomas Rast160baa02010-03-12 18:04:31 +01001327{
brian m. carlson9ef72232017-05-30 10:30:40 -07001328 const struct object_id *note = get_note(t, from_obj);
1329 const struct object_id *existing_note = get_note(t, to_obj);
Thomas Rast160baa02010-03-12 18:04:31 +01001330
1331 if (!force && existing_note)
1332 return 1;
1333
1334 if (note)
brian m. carlson5ee8a952017-05-30 10:30:43 -07001335 return add_note(t, to_obj, note, combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001336 else if (existing_note)
brian m. carlson14228442021-04-26 01:02:56 +00001337 return add_note(t, to_obj, null_oid(), combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001338
1339 return 0;
1340}
Jeff King03bb5782011-03-29 16:55:32 -04001341
1342void expand_notes_ref(struct strbuf *sb)
1343{
Christian Couder59556542013-11-30 21:55:40 +01001344 if (starts_with(sb->buf, "refs/notes/"))
Jeff King03bb5782011-03-29 16:55:32 -04001345 return; /* we're happy */
Christian Couder59556542013-11-30 21:55:40 +01001346 else if (starts_with(sb->buf, "notes/"))
René Scharfea91cc7f2020-02-09 14:44:23 +01001347 strbuf_insertstr(sb, 0, "refs/");
Jeff King03bb5782011-03-29 16:55:32 -04001348 else
René Scharfea91cc7f2020-02-09 14:44:23 +01001349 strbuf_insertstr(sb, 0, "refs/notes/");
Jeff King03bb5782011-03-29 16:55:32 -04001350}
Jacob Kellerb3715b72015-12-29 14:40:28 -08001351
1352void expand_loose_notes_ref(struct strbuf *sb)
1353{
brian m. carlson89c149f2017-05-30 10:30:38 -07001354 struct object_id object;
Jacob Kellerb3715b72015-12-29 14:40:28 -08001355
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001356 if (repo_get_oid(the_repository, sb->buf, &object)) {
Jacob Kellerb3715b72015-12-29 14:40:28 -08001357 /* fallback to expand_notes_ref */
1358 expand_notes_ref(sb);
1359 }
1360}