blob: 564baac64d762044064b0b0ba3b548f696df6105 [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"
Johannes Schindelina97a7462009-10-09 12:21:57 +02003#include "notes.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Johan Herland73f464b2010-02-13 22:28:19 +01005#include "blob.h"
Johan Herland61a7cca2010-02-13 22:28:17 +01006#include "tree.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02007#include "utf8.h"
8#include "strbuf.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02009#include "tree-walk.h"
Thomas Rast894a9d32010-03-12 18:04:26 +010010#include "string-list.h"
11#include "refs.h"
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020012
Johan Herland23123ae2009-10-09 12:22:07 +020013/*
14 * Use a non-balancing simple 16-tree structure with struct int_node as
15 * internal nodes, and struct leaf_node as leaf nodes. Each int_node has a
16 * 16-array of pointers to its children.
17 * The bottom 2 bits of each pointer is used to identify the pointer type
18 * - ptr & 3 == 0 - NULL pointer, assert(ptr == NULL)
19 * - ptr & 3 == 1 - pointer to next internal node - cast to struct int_node *
20 * - ptr & 3 == 2 - pointer to note entry - cast to struct leaf_node *
21 * - ptr & 3 == 3 - pointer to subtree entry - cast to struct leaf_node *
22 *
23 * The root node is a statically allocated struct int_node.
24 */
25struct int_node {
26 void *a[16];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020027};
28
Johan Herland23123ae2009-10-09 12:22:07 +020029/*
30 * Leaf nodes come in two variants, note entries and subtree entries,
31 * distinguished by the LSb of the leaf node pointer (see above).
Johan Herlanda7e7eff2010-02-13 22:28:10 +010032 * As a note entry, the key is the SHA1 of the referenced object, and the
Johan Herland23123ae2009-10-09 12:22:07 +020033 * value is the SHA1 of the note object.
34 * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the
Johan Herlanda7e7eff2010-02-13 22:28:10 +010035 * referenced object, using the last byte of the key to store the length of
Johan Herland23123ae2009-10-09 12:22:07 +020036 * the prefix. The value is the SHA1 of the tree object containing the notes
37 * subtree.
38 */
39struct leaf_node {
brian m. carlson5dcc9692017-05-30 10:30:37 -070040 struct object_id key_oid;
41 struct object_id val_oid;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020042};
Johannes Schindelina97a7462009-10-09 12:21:57 +020043
Johan Herland851c2b32010-02-13 22:28:23 +010044/*
45 * A notes tree may contain entries that are not notes, and that do not follow
46 * the naming conventions of notes. There are typically none/few of these, but
47 * we still need to keep track of them. Keep a simple linked list sorted alpha-
48 * betically on the non-note path. The list is populated when parsing tree
49 * objects in load_subtree(), and the non-notes are correctly written back into
50 * the tree objects produced by write_notes_tree().
51 */
52struct non_note {
53 struct non_note *next; /* grounded (last->next == NULL) */
54 char *path;
55 unsigned int mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -070056 struct object_id oid;
Johan Herland851c2b32010-02-13 22:28:23 +010057};
58
Johan Herland23123ae2009-10-09 12:22:07 +020059#define PTR_TYPE_NULL 0
60#define PTR_TYPE_INTERNAL 1
61#define PTR_TYPE_NOTE 2
62#define PTR_TYPE_SUBTREE 3
63
64#define GET_PTR_TYPE(ptr) ((uintptr_t) (ptr) & 3)
65#define CLR_PTR_TYPE(ptr) ((void *) ((uintptr_t) (ptr) & ~3))
66#define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))
67
Michael Haggerty65eb8e02017-08-26 10:28:01 +020068#define GET_NIBBLE(n, sha1) ((((sha1)[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)
Johan Herland23123ae2009-10-09 12:22:07 +020069
brian m. carlsondd437452019-02-19 00:05:01 +000070#define KEY_INDEX (the_hash_algo->rawsz - 1)
71#define FANOUT_PATH_SEPARATORS (the_hash_algo->rawsz - 1)
72#define FANOUT_PATH_SEPARATORS_MAX ((GIT_MAX_HEXSZ / 2) - 1)
Johan Herland23123ae2009-10-09 12:22:07 +020073#define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \
brian m. carlson89c149f2017-05-30 10:30:38 -070074 (memcmp(key_sha1, subtree_sha1, subtree_sha1[KEY_INDEX]))
Johan Herland23123ae2009-10-09 12:22:07 +020075
Johan Herlandcd305392010-02-13 22:28:18 +010076struct notes_tree default_notes_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +020077
Jeff King2721ce22016-06-13 06:04:20 -040078static struct string_list display_notes_refs = STRING_LIST_INIT_NODUP;
Thomas Rast894a9d32010-03-12 18:04:26 +010079static struct notes_tree **display_notes_trees;
80
Johan Herland851c2b32010-02-13 22:28:23 +010081static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
82 struct int_node *node, unsigned int n);
Johan Herland23123ae2009-10-09 12:22:07 +020083
84/*
Johan Herlandef8db632009-10-09 12:22:09 +020085 * Search the tree until the appropriate location for the given key is found:
Johan Herland23123ae2009-10-09 12:22:07 +020086 * 1. Start at the root node, with n = 0
Johan Herlandef8db632009-10-09 12:22:09 +020087 * 2. If a[0] at the current level is a matching subtree entry, unpack that
88 * subtree entry and remove it; restart search at the current level.
89 * 3. Use the nth nibble of the key as an index into a:
90 * - If a[n] is an int_node, recurse from #2 into that node and increment n
Johan Herland23123ae2009-10-09 12:22:07 +020091 * - If a matching subtree entry, unpack that subtree entry (and remove it);
92 * restart search at the current level.
Johan Herlandef8db632009-10-09 12:22:09 +020093 * - Otherwise, we have found one of the following:
94 * - a subtree entry which does not match the key
95 * - a note entry which may or may not match the key
96 * - an unused leaf node (NULL)
97 * In any case, set *tree and *n, and return pointer to the tree location.
Johan Herland23123ae2009-10-09 12:22:07 +020098 */
Johan Herland851c2b32010-02-13 22:28:23 +010099static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
Johan Herlandef8db632009-10-09 12:22:09 +0200100 unsigned char *n, const unsigned char *key_sha1)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200101{
Johan Herland23123ae2009-10-09 12:22:07 +0200102 struct leaf_node *l;
Johan Herlandef8db632009-10-09 12:22:09 +0200103 unsigned char i;
104 void *p = (*tree)->a[0];
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200105
Johan Herlandef8db632009-10-09 12:22:09 +0200106 if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
107 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700108 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herlandef8db632009-10-09 12:22:09 +0200109 /* unpack tree and resume search */
110 (*tree)->a[0] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100111 load_subtree(t, l, *tree, *n);
Johan Herlandef8db632009-10-09 12:22:09 +0200112 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100113 return note_tree_search(t, tree, n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200114 }
115 }
116
117 i = GET_NIBBLE(*n, key_sha1);
118 p = (*tree)->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100119 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200120 case PTR_TYPE_INTERNAL:
Johan Herlandef8db632009-10-09 12:22:09 +0200121 *tree = CLR_PTR_TYPE(p);
122 (*n)++;
Johan Herland851c2b32010-02-13 22:28:23 +0100123 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200124 case PTR_TYPE_SUBTREE:
125 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700126 if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200127 /* unpack tree and resume search */
Johan Herlandef8db632009-10-09 12:22:09 +0200128 (*tree)->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100129 load_subtree(t, l, *tree, *n);
Johan Herland23123ae2009-10-09 12:22:07 +0200130 free(l);
Johan Herland851c2b32010-02-13 22:28:23 +0100131 return note_tree_search(t, tree, n, key_sha1);
Johan Herland23123ae2009-10-09 12:22:07 +0200132 }
Johan Herlandef8db632009-10-09 12:22:09 +0200133 /* fall through */
Johan Herland23123ae2009-10-09 12:22:07 +0200134 default:
Johan Herlandef8db632009-10-09 12:22:09 +0200135 return &((*tree)->a[i]);
Johan Herland23123ae2009-10-09 12:22:07 +0200136 }
Johan Herlandef8db632009-10-09 12:22:09 +0200137}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200138
Johan Herlandef8db632009-10-09 12:22:09 +0200139/*
140 * To find a leaf_node:
141 * Search to the tree location appropriate for the given key:
142 * If a note entry with matching key, return the note entry, else return NULL.
143 */
Johan Herland851c2b32010-02-13 22:28:23 +0100144static struct leaf_node *note_tree_find(struct notes_tree *t,
145 struct int_node *tree, unsigned char n,
Johan Herlandef8db632009-10-09 12:22:09 +0200146 const unsigned char *key_sha1)
147{
Johan Herland851c2b32010-02-13 22:28:23 +0100148 void **p = note_tree_search(t, &tree, &n, key_sha1);
Johan Herlandef8db632009-10-09 12:22:09 +0200149 if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
150 struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff Kinge3ff0682018-08-28 17:22:44 -0400151 if (hasheq(key_sha1, l->key_oid.hash))
Johan Herlandef8db632009-10-09 12:22:09 +0200152 return l;
Johan Herland23123ae2009-10-09 12:22:07 +0200153 }
154 return NULL;
155}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200156
Johan Herland23123ae2009-10-09 12:22:07 +0200157/*
Johan Herland1ec666b2010-02-13 22:28:14 +0100158 * How to consolidate an int_node:
159 * If there are > 1 non-NULL entries, give up and return non-zero.
160 * Otherwise replace the int_node at the given index in the given parent node
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900161 * with the only NOTE entry (or a NULL entry if no entries) from the given
162 * tree, and return 0.
Johan Herland1ec666b2010-02-13 22:28:14 +0100163 */
164static int note_tree_consolidate(struct int_node *tree,
165 struct int_node *parent, unsigned char index)
166{
167 unsigned int i;
168 void *p = NULL;
169
170 assert(tree && parent);
171 assert(CLR_PTR_TYPE(parent->a[index]) == tree);
172
173 for (i = 0; i < 16; i++) {
174 if (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {
175 if (p) /* more than one entry */
176 return -2;
177 p = tree->a[i];
178 }
179 }
180
Mike Hommey5a8e7c32017-03-26 10:52:12 +0900181 if (p && (GET_PTR_TYPE(p) != PTR_TYPE_NOTE))
182 return -2;
Johan Herland1ec666b2010-02-13 22:28:14 +0100183 /* replace tree with p in parent[index] */
184 parent->a[index] = p;
185 free(tree);
186 return 0;
187}
188
189/*
190 * To remove a leaf_node:
191 * Search to the tree location appropriate for the given leaf_node's key:
192 * - If location does not hold a matching entry, abort and do nothing.
Johan Herland1ee1e432010-08-31 17:56:50 +0200193 * - Copy the matching entry's value into the given entry.
Johan Herland1ec666b2010-02-13 22:28:14 +0100194 * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).
195 * - Consolidate int_nodes repeatedly, while walking up the tree towards root.
196 */
Johan Herland1ee1e432010-08-31 17:56:50 +0200197static void note_tree_remove(struct notes_tree *t,
198 struct int_node *tree, unsigned char n,
199 struct leaf_node *entry)
Johan Herland1ec666b2010-02-13 22:28:14 +0100200{
201 struct leaf_node *l;
brian m. carlsondd437452019-02-19 00:05:01 +0000202 struct int_node *parent_stack[GIT_MAX_RAWSZ];
Johan Herland1ec666b2010-02-13 22:28:14 +0100203 unsigned char i, j;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700204 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100205
206 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
207 if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
208 return; /* type mismatch, nothing to remove */
209 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
Jeff King9001dc22018-08-28 17:22:48 -0400210 if (!oideq(&l->key_oid, &entry->key_oid))
Johan Herland1ec666b2010-02-13 22:28:14 +0100211 return; /* key mismatch, nothing to remove */
212
213 /* we have found a matching entry */
brian m. carlson5dcc9692017-05-30 10:30:37 -0700214 oidcpy(&entry->val_oid, &l->val_oid);
Johan Herland1ec666b2010-02-13 22:28:14 +0100215 free(l);
216 *p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
217
218 /* consolidate this tree level, and parent levels, if possible */
219 if (!n)
220 return; /* cannot consolidate top level */
221 /* first, build stack of ancestors between root and current node */
Johan Herlandcd305392010-02-13 22:28:18 +0100222 parent_stack[0] = t->root;
Johan Herland1ec666b2010-02-13 22:28:14 +0100223 for (i = 0; i < n; i++) {
brian m. carlson5dcc9692017-05-30 10:30:37 -0700224 j = GET_NIBBLE(i, entry->key_oid.hash);
Johan Herland1ec666b2010-02-13 22:28:14 +0100225 parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
226 }
227 assert(i == n && parent_stack[i] == tree);
228 /* next, unwind stack until note_tree_consolidate() is done */
229 while (i > 0 &&
230 !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
brian m. carlson5dcc9692017-05-30 10:30:37 -0700231 GET_NIBBLE(i - 1, entry->key_oid.hash)))
Johan Herland1ec666b2010-02-13 22:28:14 +0100232 i--;
233}
234
Johan Herlanda5cdebe2010-11-09 22:49:40 +0100235/*
Johannes Schindelina97a7462009-10-09 12:21:57 +0200236 * To insert a leaf_node:
237 * Search to the tree location appropriate for the given leaf_node's key:
238 * - If location is unused (NULL), store the tweaked pointer directly there
239 * - If location holds a note entry that matches the note-to-be-inserted, then
240 * combine the two notes (by calling the given combine_notes function).
241 * - If location holds a note entry that matches the subtree-to-be-inserted,
242 * then unpack the subtree-to-be-inserted into the location.
243 * - If location holds a matching subtree entry, unpack the subtree at that
244 * location, and restart the insert operation from that level.
245 * - Else, create a new int_node, holding both the node-at-location and the
246 * node-to-be-inserted, and store the new int_node into the location.
247 */
Johan Herland180619a2010-11-15 00:52:26 +0100248static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
Johannes Schindelina97a7462009-10-09 12:21:57 +0200249 unsigned char n, struct leaf_node *entry, unsigned char type,
250 combine_notes_fn combine_notes)
251{
252 struct int_node *new_node;
253 struct leaf_node *l;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700254 void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
Johan Herland180619a2010-11-15 00:52:26 +0100255 int ret = 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200256
257 assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
258 l = (struct leaf_node *) CLR_PTR_TYPE(*p);
259 switch (GET_PTR_TYPE(*p)) {
260 case PTR_TYPE_NULL:
261 assert(!*p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700262 if (is_null_oid(&entry->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100263 free(entry);
264 else
265 *p = SET_PTR_TYPE(entry, type);
Johan Herland180619a2010-11-15 00:52:26 +0100266 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200267 case PTR_TYPE_NOTE:
268 switch (type) {
269 case PTR_TYPE_NOTE:
Jeff King4a7e27e2018-08-28 17:22:40 -0400270 if (oideq(&l->key_oid, &entry->key_oid)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200271 /* skip concatenation if l == entry */
Mike Hommey779ad662019-08-25 14:18:18 +0900272 if (oideq(&l->val_oid, &entry->val_oid)) {
273 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100274 return 0;
Mike Hommey779ad662019-08-25 14:18:18 +0900275 }
Johannes Schindelina97a7462009-10-09 12:21:57 +0200276
Patryk Obarab7d591d2018-01-28 01:13:17 +0100277 ret = combine_notes(&l->val_oid,
278 &entry->val_oid);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700279 if (!ret && is_null_oid(&l->val_oid))
Johan Herlande2656c82010-11-09 22:49:41 +0100280 note_tree_remove(t, tree, n, entry);
Johannes Schindelina97a7462009-10-09 12:21:57 +0200281 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100282 return ret;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200283 }
284 break;
285 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700286 if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
287 entry->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200288 /* unpack 'entry' */
289 load_subtree(t, entry, tree, n);
290 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100291 return 0;
Johannes Schindelina97a7462009-10-09 12:21:57 +0200292 }
293 break;
294 }
295 break;
296 case PTR_TYPE_SUBTREE:
brian m. carlson5dcc9692017-05-30 10:30:37 -0700297 if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +0200298 /* unpack 'l' and restart insert */
299 *p = NULL;
300 load_subtree(t, l, tree, n);
301 free(l);
Johan Herland180619a2010-11-15 00:52:26 +0100302 return note_tree_insert(t, tree, n, entry, type,
303 combine_notes);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200304 }
305 break;
306 }
307
308 /* non-matching leaf_node */
309 assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
310 GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700311 if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
Johan Herlande2656c82010-11-09 22:49:41 +0100312 free(entry);
Johan Herland180619a2010-11-15 00:52:26 +0100313 return 0;
Johan Herlande2656c82010-11-09 22:49:41 +0100314 }
Brian Gesiak65bbf082014-05-27 00:33:52 +0900315 new_node = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland180619a2010-11-15 00:52:26 +0100316 ret = note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
317 combine_notes);
318 if (ret)
319 return ret;
Johan Herland23123ae2009-10-09 12:22:07 +0200320 *p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
Johan Herland180619a2010-11-15 00:52:26 +0100321 return note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
Johan Herlandef8db632009-10-09 12:22:09 +0200322}
323
Johan Herland23123ae2009-10-09 12:22:07 +0200324/* Free the entire notes data contained in the given tree */
325static void note_tree_free(struct int_node *tree)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200326{
Johan Herland23123ae2009-10-09 12:22:07 +0200327 unsigned int i;
328 for (i = 0; i < 16; i++) {
329 void *p = tree->a[i];
Johan Herland0ab1faa2010-02-13 22:28:09 +0100330 switch (GET_PTR_TYPE(p)) {
Johan Herland23123ae2009-10-09 12:22:07 +0200331 case PTR_TYPE_INTERNAL:
332 note_tree_free(CLR_PTR_TYPE(p));
333 /* fall through */
334 case PTR_TYPE_NOTE:
335 case PTR_TYPE_SUBTREE:
336 free(CLR_PTR_TYPE(p));
337 }
338 }
339}
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200340
Johan Herland851c2b32010-02-13 22:28:23 +0100341static int non_note_cmp(const struct non_note *a, const struct non_note *b)
342{
343 return strcmp(a->path, b->path);
344}
345
Jeff Kingc29edfe2015-08-19 14:12:41 -0400346/* note: takes ownership of path string */
347static void add_non_note(struct notes_tree *t, char *path,
Johan Herland851c2b32010-02-13 22:28:23 +0100348 unsigned int mode, const unsigned char *sha1)
349{
350 struct non_note *p = t->prev_non_note, *n;
351 n = (struct non_note *) xmalloc(sizeof(struct non_note));
352 n->next = NULL;
Jeff Kingc29edfe2015-08-19 14:12:41 -0400353 n->path = path;
Johan Herland851c2b32010-02-13 22:28:23 +0100354 n->mode = mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700355 hashcpy(n->oid.hash, sha1);
Johan Herland851c2b32010-02-13 22:28:23 +0100356 t->prev_non_note = n;
357
358 if (!t->first_non_note) {
359 t->first_non_note = n;
360 return;
361 }
362
363 if (non_note_cmp(p, n) < 0)
364 ; /* do nothing */
365 else if (non_note_cmp(t->first_non_note, n) <= 0)
366 p = t->first_non_note;
367 else {
368 /* n sorts before t->first_non_note */
369 n->next = t->first_non_note;
370 t->first_non_note = n;
371 return;
372 }
373
374 /* n sorts equal or after p */
375 while (p->next && non_note_cmp(p->next, n) <= 0)
376 p = p->next;
377
378 if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
379 assert(strcmp(p->path, n->path) == 0);
380 p->mode = n->mode;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700381 oidcpy(&p->oid, &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100382 free(n);
383 t->prev_non_note = p;
384 return;
385 }
386
387 /* n sorts between p and p->next */
388 n->next = p->next;
389 p->next = n;
390}
391
392static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
393 struct int_node *node, unsigned int n)
Johan Herland23123ae2009-10-09 12:22:07 +0200394{
brian m. carlson89c149f2017-05-30 10:30:38 -0700395 struct object_id object_oid;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200396 size_t prefix_len;
Johan Herland23123ae2009-10-09 12:22:07 +0200397 void *buf;
398 struct tree_desc desc;
399 struct name_entry entry;
brian m. carlsondd437452019-02-19 00:05:01 +0000400 const unsigned hashsz = the_hash_algo->rawsz;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200401
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700402 buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
Johan Herland23123ae2009-10-09 12:22:07 +0200403 if (!buf)
404 die("Could not read %s for notes-index",
brian m. carlson5dcc9692017-05-30 10:30:37 -0700405 oid_to_hex(&subtree->val_oid));
Johan Herland23123ae2009-10-09 12:22:07 +0200406
brian m. carlson89c149f2017-05-30 10:30:38 -0700407 prefix_len = subtree->key_oid.hash[KEY_INDEX];
brian m. carlsondd437452019-02-19 00:05:01 +0000408 if (prefix_len >= hashsz)
Michael Haggerty39642812017-09-08 18:10:10 +0200409 BUG("prefix_len (%"PRIuMAX") is out of range", (uintmax_t)prefix_len);
410 if (prefix_len * 2 < n)
411 BUG("prefix_len (%"PRIuMAX") is too small", (uintmax_t)prefix_len);
brian m. carlson89c149f2017-05-30 10:30:38 -0700412 memcpy(object_oid.hash, subtree->key_oid.hash, prefix_len);
Johan Herland23123ae2009-10-09 12:22:07 +0200413 while (tree_entry(&desc, &entry)) {
Michael Haggertya2816392017-08-26 10:28:03 +0200414 unsigned char type;
415 struct leaf_node *l;
Michael Haggerty06cfa752017-08-26 10:28:12 +0200416 size_t path_len = strlen(entry.path);
Johan Herland23123ae2009-10-09 12:22:07 +0200417
brian m. carlsondd437452019-02-19 00:05:01 +0000418 if (path_len == 2 * (hashsz - prefix_len)) {
Michael Haggerty98c98972017-08-26 10:28:05 +0200419 /* This is potentially the remainder of the SHA-1 */
Michael Haggerty40432182017-08-26 10:28:07 +0200420
421 if (!S_ISREG(entry.mode))
422 /* notes must be blobs */
423 goto handle_non_note;
424
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200425 if (hex_to_bytes(object_oid.hash + prefix_len, entry.path,
brian m. carlsondd437452019-02-19 00:05:01 +0000426 hashsz - prefix_len))
Michael Haggerty98c98972017-08-26 10:28:05 +0200427 goto handle_non_note; /* entry.path is not a SHA1 */
Johan Herland23123ae2009-10-09 12:22:07 +0200428
Johan Herland851c2b32010-02-13 22:28:23 +0100429 type = PTR_TYPE_NOTE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200430 } else if (path_len == 2) {
431 /* This is potentially an internal node */
Michael Haggertyd49852d2017-08-26 10:28:10 +0200432 size_t len = prefix_len;
Michael Haggerty4d589b82017-08-26 10:28:06 +0200433
434 if (!S_ISDIR(entry.mode))
435 /* internal nodes must be trees */
436 goto handle_non_note;
437
Michael Haggertycfdc88f2017-08-26 10:28:11 +0200438 if (hex_to_bytes(object_oid.hash + len++, entry.path, 1))
Michael Haggerty98c98972017-08-26 10:28:05 +0200439 goto handle_non_note; /* entry.path is not a SHA1 */
440
Michael Haggertyd49852d2017-08-26 10:28:10 +0200441 /*
442 * Pad the rest of the SHA-1 with zeros,
443 * except for the last byte, where we write
444 * the length:
445 */
brian m. carlsondd437452019-02-19 00:05:01 +0000446 memset(object_oid.hash + len, 0, hashsz - len - 1);
Michael Haggertyd49852d2017-08-26 10:28:10 +0200447 object_oid.hash[KEY_INDEX] = (unsigned char)len;
Michael Haggerty4ebef532017-08-26 10:28:09 +0200448
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200449 type = PTR_TYPE_SUBTREE;
Michael Haggerty98c98972017-08-26 10:28:05 +0200450 } else {
451 /* This can't be part of a note */
452 goto handle_non_note;
Johan Herland23123ae2009-10-09 12:22:07 +0200453 }
Michael Haggerty98c98972017-08-26 10:28:05 +0200454
Michael Haggerty4ebef532017-08-26 10:28:09 +0200455 l = xcalloc(1, sizeof(*l));
456 oidcpy(&l->key_oid, &object_oid);
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000457 oidcpy(&l->val_oid, &entry.oid);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200458 if (note_tree_insert(t, node, n, l, type,
459 combine_notes_concatenate))
460 die("Failed to load %s %s into notes tree "
461 "from %s",
462 type == PTR_TYPE_NOTE ? "note" : "subtree",
Jeff King60fe4772019-08-25 03:19:51 -0400463 oid_to_hex(&object_oid), t->ref);
Michael Haggertyd3b0c6b2017-08-26 10:28:02 +0200464
Johan Herland851c2b32010-02-13 22:28:23 +0100465 continue;
466
467handle_non_note:
468 /*
Michael Haggertycbeed9a2017-08-26 10:28:04 +0200469 * Determine full path for this non-note entry. The
470 * filename is already found in entry.path, but the
471 * directory part of the path must be deduced from the
472 * subtree containing this entry based on our
473 * knowledge that the overall notes tree follows a
474 * strict byte-based progressive fanout structure
475 * (i.e. using 2/38, 2/2/36, etc. fanouts).
Johan Herland851c2b32010-02-13 22:28:23 +0100476 */
477 {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400478 struct strbuf non_note_path = STRBUF_INIT;
brian m. carlson5dcc9692017-05-30 10:30:37 -0700479 const char *q = oid_to_hex(&subtree->key_oid);
Michael Haggerty06cfa752017-08-26 10:28:12 +0200480 size_t i;
Johan Herland851c2b32010-02-13 22:28:23 +0100481 for (i = 0; i < prefix_len; i++) {
Jeff Kingc29edfe2015-08-19 14:12:41 -0400482 strbuf_addch(&non_note_path, *q++);
483 strbuf_addch(&non_note_path, *q++);
484 strbuf_addch(&non_note_path, '/');
Johan Herland851c2b32010-02-13 22:28:23 +0100485 }
Jeff Kingc29edfe2015-08-19 14:12:41 -0400486 strbuf_addstr(&non_note_path, entry.path);
487 add_non_note(t, strbuf_detach(&non_note_path, NULL),
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000488 entry.mode, entry.oid.hash);
Johan Herland851c2b32010-02-13 22:28:23 +0100489 }
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200490 }
Johan Herland23123ae2009-10-09 12:22:07 +0200491 free(buf);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200492}
493
Johan Herland73f77b92010-02-13 22:28:16 +0100494/*
495 * Determine optimal on-disk fanout for this part of the notes tree
496 *
497 * Given a (sub)tree and the level in the internal tree structure, determine
498 * whether or not the given existing fanout should be expanded for this
499 * (sub)tree.
500 *
501 * Values of the 'fanout' variable:
502 * - 0: No fanout (all notes are stored directly in the root notes tree)
503 * - 1: 2/38 fanout
504 * - 2: 2/2/36 fanout
505 * - 3: 2/2/2/34 fanout
506 * etc.
507 */
508static unsigned char determine_fanout(struct int_node *tree, unsigned char n,
509 unsigned char fanout)
510{
511 /*
512 * The following is a simple heuristic that works well in practice:
513 * For each even-numbered 16-tree level (remember that each on-disk
514 * fanout level corresponds to _two_ 16-tree levels), peek at all 16
515 * entries at that tree level. If all of them are either int_nodes or
516 * subtree entries, then there are likely plenty of notes below this
517 * level, so we return an incremented fanout.
518 */
519 unsigned int i;
520 if ((n % 2) || (n > 2 * fanout))
521 return fanout;
522 for (i = 0; i < 16; i++) {
523 switch (GET_PTR_TYPE(tree->a[i])) {
524 case PTR_TYPE_SUBTREE:
525 case PTR_TYPE_INTERNAL:
526 continue;
527 default:
528 return fanout;
529 }
530 }
531 return fanout + 1;
532}
533
brian m. carlsondd437452019-02-19 00:05:01 +0000534/* hex oid + '/' between each pair of hex digits + NUL */
535#define FANOUT_PATH_MAX GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS_MAX + 1
Jeff King02e32b72015-09-24 17:08:24 -0400536
brian m. carlson0dbc6462019-02-19 00:05:02 +0000537static void construct_path_with_fanout(const unsigned char *hash,
Johan Herland73f77b92010-02-13 22:28:16 +0100538 unsigned char fanout, char *path)
539{
540 unsigned int i = 0, j = 0;
brian m. carlson0dbc6462019-02-19 00:05:02 +0000541 const char *hex_hash = hash_to_hex(hash);
brian m. carlsondd437452019-02-19 00:05:01 +0000542 assert(fanout < the_hash_algo->rawsz);
Johan Herland73f77b92010-02-13 22:28:16 +0100543 while (fanout) {
brian m. carlson0dbc6462019-02-19 00:05:02 +0000544 path[i++] = hex_hash[j++];
545 path[i++] = hex_hash[j++];
Johan Herland73f77b92010-02-13 22:28:16 +0100546 path[i++] = '/';
547 fanout--;
548 }
brian m. carlson0dbc6462019-02-19 00:05:02 +0000549 xsnprintf(path + i, FANOUT_PATH_MAX - i, "%s", hex_hash + j);
Johan Herland73f77b92010-02-13 22:28:16 +0100550}
551
Johan Herland851c2b32010-02-13 22:28:23 +0100552static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
553 unsigned char n, unsigned char fanout, int flags,
554 each_note_fn fn, void *cb_data)
Johan Herland73f77b92010-02-13 22:28:16 +0100555{
556 unsigned int i;
557 void *p;
558 int ret = 0;
559 struct leaf_node *l;
Jeff King02e32b72015-09-24 17:08:24 -0400560 static char path[FANOUT_PATH_MAX];
Johan Herland73f77b92010-02-13 22:28:16 +0100561
562 fanout = determine_fanout(tree, n, fanout);
563 for (i = 0; i < 16; i++) {
564redo:
565 p = tree->a[i];
566 switch (GET_PTR_TYPE(p)) {
567 case PTR_TYPE_INTERNAL:
568 /* recurse into int_node */
Johan Herland851c2b32010-02-13 22:28:23 +0100569 ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
Johan Herland73f77b92010-02-13 22:28:16 +0100570 fanout, flags, fn, cb_data);
571 break;
572 case PTR_TYPE_SUBTREE:
573 l = (struct leaf_node *) CLR_PTR_TYPE(p);
574 /*
575 * Subtree entries in the note tree represent parts of
576 * the note tree that have not yet been explored. There
577 * is a direct relationship between subtree entries at
578 * level 'n' in the tree, and the 'fanout' variable:
Johan Herlanddbc27472020-02-03 22:04:45 +0100579 * Subtree entries at level 'n < 2 * fanout' should be
Johan Herland73f77b92010-02-13 22:28:16 +0100580 * preserved, since they correspond exactly to a fanout
581 * directory in the on-disk structure. However, subtree
Johan Herlanddbc27472020-02-03 22:04:45 +0100582 * entries at level 'n >= 2 * fanout' should NOT be
Johan Herland73f77b92010-02-13 22:28:16 +0100583 * preserved, but rather consolidated into the above
584 * notes tree level. We achieve this by unconditionally
585 * unpacking subtree entries that exist below the
586 * threshold level at 'n = 2 * fanout'.
587 */
Johan Herlanddbc27472020-02-03 22:04:45 +0100588 if (n < 2 * fanout &&
Johan Herland73f77b92010-02-13 22:28:16 +0100589 flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
590 /* invoke callback with subtree */
591 unsigned int path_len =
brian m. carlson89c149f2017-05-30 10:30:38 -0700592 l->key_oid.hash[KEY_INDEX] * 2 + fanout;
Jeff King02e32b72015-09-24 17:08:24 -0400593 assert(path_len < FANOUT_PATH_MAX - 1);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700594 construct_path_with_fanout(l->key_oid.hash,
595 fanout,
Johan Herland73f77b92010-02-13 22:28:16 +0100596 path);
597 /* Create trailing slash, if needed */
598 if (path[path_len - 1] != '/')
599 path[path_len++] = '/';
600 path[path_len] = '\0';
brian m. carlson490bc832017-05-30 10:30:39 -0700601 ret = fn(&l->key_oid, &l->val_oid,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700602 path,
Johan Herland73f77b92010-02-13 22:28:16 +0100603 cb_data);
604 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100605 if (n >= 2 * fanout ||
Johan Herland73f77b92010-02-13 22:28:16 +0100606 !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
607 /* unpack subtree and resume traversal */
608 tree->a[i] = NULL;
Johan Herland851c2b32010-02-13 22:28:23 +0100609 load_subtree(t, l, tree, n);
Johan Herland73f77b92010-02-13 22:28:16 +0100610 free(l);
611 goto redo;
612 }
613 break;
614 case PTR_TYPE_NOTE:
615 l = (struct leaf_node *) CLR_PTR_TYPE(p);
brian m. carlson5dcc9692017-05-30 10:30:37 -0700616 construct_path_with_fanout(l->key_oid.hash, fanout,
617 path);
brian m. carlson490bc832017-05-30 10:30:39 -0700618 ret = fn(&l->key_oid, &l->val_oid, path,
brian m. carlson5dcc9692017-05-30 10:30:37 -0700619 cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +0100620 break;
621 }
622 if (ret)
623 return ret;
624 }
625 return 0;
626}
627
Johan Herland61a7cca2010-02-13 22:28:17 +0100628struct tree_write_stack {
629 struct tree_write_stack *next;
630 struct strbuf buf;
631 char path[2]; /* path to subtree in next, if any */
632};
633
634static inline int matches_tree_write_stack(struct tree_write_stack *tws,
635 const char *full_path)
636{
637 return full_path[0] == tws->path[0] &&
638 full_path[1] == tws->path[1] &&
639 full_path[2] == '/';
640}
641
642static void write_tree_entry(struct strbuf *buf, unsigned int mode,
643 const char *path, unsigned int path_len, const
brian m. carlsondd437452019-02-19 00:05:01 +0000644 unsigned char *hash)
Johan Herland61a7cca2010-02-13 22:28:17 +0100645{
Junio C Hamanoc88f0cc2010-02-24 21:39:06 -0800646 strbuf_addf(buf, "%o %.*s%c", mode, path_len, path, '\0');
brian m. carlsondd437452019-02-19 00:05:01 +0000647 strbuf_add(buf, hash, the_hash_algo->rawsz);
Johan Herland61a7cca2010-02-13 22:28:17 +0100648}
649
650static void tree_write_stack_init_subtree(struct tree_write_stack *tws,
651 const char *path)
652{
653 struct tree_write_stack *n;
654 assert(!tws->next);
655 assert(tws->path[0] == '\0' && tws->path[1] == '\0');
656 n = (struct tree_write_stack *)
657 xmalloc(sizeof(struct tree_write_stack));
658 n->next = NULL;
brian m. carlsondd437452019-02-19 00:05:01 +0000659 strbuf_init(&n->buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries per tree */
Johan Herland61a7cca2010-02-13 22:28:17 +0100660 n->path[0] = n->path[1] = '\0';
661 tws->next = n;
662 tws->path[0] = path[0];
663 tws->path[1] = path[1];
664}
665
666static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
667{
668 int ret;
669 struct tree_write_stack *n = tws->next;
brian m. carlson89c149f2017-05-30 10:30:38 -0700670 struct object_id s;
Johan Herland61a7cca2010-02-13 22:28:17 +0100671 if (n) {
672 ret = tree_write_stack_finish_subtree(n);
673 if (ret)
674 return ret;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100675 ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s);
Johan Herland61a7cca2010-02-13 22:28:17 +0100676 if (ret)
677 return ret;
678 strbuf_release(&n->buf);
679 free(n);
680 tws->next = NULL;
brian m. carlson89c149f2017-05-30 10:30:38 -0700681 write_tree_entry(&tws->buf, 040000, tws->path, 2, s.hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100682 tws->path[0] = tws->path[1] = '\0';
683 }
684 return 0;
685}
686
687static int write_each_note_helper(struct tree_write_stack *tws,
688 const char *path, unsigned int mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700689 const struct object_id *oid)
Johan Herland61a7cca2010-02-13 22:28:17 +0100690{
691 size_t path_len = strlen(path);
692 unsigned int n = 0;
693 int ret;
694
695 /* Determine common part of tree write stack */
696 while (tws && 3 * n < path_len &&
697 matches_tree_write_stack(tws, path + 3 * n)) {
698 n++;
699 tws = tws->next;
700 }
701
702 /* tws point to last matching tree_write_stack entry */
703 ret = tree_write_stack_finish_subtree(tws);
704 if (ret)
705 return ret;
706
707 /* Start subtrees needed to satisfy path */
708 while (3 * n + 2 < path_len && path[3 * n + 2] == '/') {
709 tree_write_stack_init_subtree(tws, path + 3 * n);
710 n++;
711 tws = tws->next;
712 }
713
714 /* There should be no more directory components in the given path */
715 assert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);
716
717 /* Finally add given entry to the current tree object */
718 write_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),
brian m. carlson490bc832017-05-30 10:30:39 -0700719 oid->hash);
Johan Herland61a7cca2010-02-13 22:28:17 +0100720
721 return 0;
722}
723
724struct write_each_note_data {
725 struct tree_write_stack *root;
Johan Herlanddbc27472020-02-03 22:04:45 +0100726 struct non_note **nn_list;
727 struct non_note *nn_prev;
Johan Herland61a7cca2010-02-13 22:28:17 +0100728};
729
Johan Herland851c2b32010-02-13 22:28:23 +0100730static int write_each_non_note_until(const char *note_path,
731 struct write_each_note_data *d)
732{
Johan Herlanddbc27472020-02-03 22:04:45 +0100733 struct non_note *p = d->nn_prev;
734 struct non_note *n = p ? p->next : *d->nn_list;
Ramsay Jones89fe1212010-06-21 19:52:29 +0100735 int cmp = 0, ret;
Johan Herland851c2b32010-02-13 22:28:23 +0100736 while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
737 if (note_path && cmp == 0)
738 ; /* do nothing, prefer note to non-note */
739 else {
740 ret = write_each_note_helper(d->root, n->path, n->mode,
brian m. carlson490bc832017-05-30 10:30:39 -0700741 &n->oid);
Johan Herland851c2b32010-02-13 22:28:23 +0100742 if (ret)
743 return ret;
744 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100745 p = n;
Johan Herland851c2b32010-02-13 22:28:23 +0100746 n = n->next;
747 }
Johan Herlanddbc27472020-02-03 22:04:45 +0100748 d->nn_prev = p;
Johan Herland851c2b32010-02-13 22:28:23 +0100749 return 0;
750}
751
brian m. carlson490bc832017-05-30 10:30:39 -0700752static int write_each_note(const struct object_id *object_oid,
753 const struct object_id *note_oid, char *note_path,
Johan Herland61a7cca2010-02-13 22:28:17 +0100754 void *cb_data)
755{
756 struct write_each_note_data *d =
757 (struct write_each_note_data *) cb_data;
758 size_t note_path_len = strlen(note_path);
759 unsigned int mode = 0100644;
760
761 if (note_path[note_path_len - 1] == '/') {
762 /* subtree entry */
763 note_path_len--;
764 note_path[note_path_len] = '\0';
765 mode = 040000;
766 }
brian m. carlsondd437452019-02-19 00:05:01 +0000767 assert(note_path_len <= GIT_MAX_HEXSZ + FANOUT_PATH_SEPARATORS);
Johan Herland61a7cca2010-02-13 22:28:17 +0100768
Johan Herland851c2b32010-02-13 22:28:23 +0100769 /* Weave non-note entries into note entries */
770 return write_each_non_note_until(note_path, d) ||
brian m. carlson490bc832017-05-30 10:30:39 -0700771 write_each_note_helper(d->root, note_path, mode, note_oid);
Johan Herland61a7cca2010-02-13 22:28:17 +0100772}
773
Johan Herland00fbe632010-02-13 22:28:27 +0100774struct note_delete_list {
775 struct note_delete_list *next;
776 const unsigned char *sha1;
777};
778
brian m. carlson490bc832017-05-30 10:30:39 -0700779static int prune_notes_helper(const struct object_id *object_oid,
780 const struct object_id *note_oid, char *note_path,
Johan Herland00fbe632010-02-13 22:28:27 +0100781 void *cb_data)
782{
783 struct note_delete_list **l = (struct note_delete_list **) cb_data;
784 struct note_delete_list *n;
785
brian m. carlson490bc832017-05-30 10:30:39 -0700786 if (has_object_file(object_oid))
Johan Herland00fbe632010-02-13 22:28:27 +0100787 return 0; /* nothing to do for this note */
788
789 /* failed to find object => prune this note */
790 n = (struct note_delete_list *) xmalloc(sizeof(*n));
791 n->next = *l;
brian m. carlson490bc832017-05-30 10:30:39 -0700792 n->sha1 = object_oid->hash;
Johan Herland00fbe632010-02-13 22:28:27 +0100793 *l = n;
794 return 0;
795}
796
Patryk Obarab7d591d2018-01-28 01:13:17 +0100797int combine_notes_concatenate(struct object_id *cur_oid,
798 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100799{
800 char *cur_msg = NULL, *new_msg = NULL, *buf;
801 unsigned long cur_len, new_len, buf_len;
802 enum object_type cur_type, new_type;
803 int ret;
804
805 /* read in both note blob objects */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100806 if (!is_null_oid(new_oid))
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000807 new_msg = read_object_file(new_oid, &new_type, &new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100808 if (!new_msg || !new_len || new_type != OBJ_BLOB) {
809 free(new_msg);
810 return 0;
811 }
Patryk Obarab7d591d2018-01-28 01:13:17 +0100812 if (!is_null_oid(cur_oid))
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000813 cur_msg = read_object_file(cur_oid, &cur_type, &cur_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100814 if (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {
815 free(cur_msg);
816 free(new_msg);
Patryk Obarab7d591d2018-01-28 01:13:17 +0100817 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100818 return 0;
819 }
820
Johan Herlandd4990c42010-11-09 22:49:44 +0100821 /* we will separate the notes by two newlines anyway */
Johan Herland73f464b2010-02-13 22:28:19 +0100822 if (cur_msg[cur_len - 1] == '\n')
823 cur_len--;
824
825 /* concatenate cur_msg and new_msg into buf */
Johan Herlandd4990c42010-11-09 22:49:44 +0100826 buf_len = cur_len + 2 + new_len;
Johan Herland73f464b2010-02-13 22:28:19 +0100827 buf = (char *) xmalloc(buf_len);
828 memcpy(buf, cur_msg, cur_len);
829 buf[cur_len] = '\n';
Johan Herlandd4990c42010-11-09 22:49:44 +0100830 buf[cur_len + 1] = '\n';
831 memcpy(buf + cur_len + 2, new_msg, new_len);
Johan Herland73f464b2010-02-13 22:28:19 +0100832 free(cur_msg);
833 free(new_msg);
834
835 /* create a new blob object from buf */
Patryk Obaraa09c9852018-01-28 01:13:19 +0100836 ret = write_object_file(buf, buf_len, blob_type, cur_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100837 free(buf);
838 return ret;
839}
840
Patryk Obarab7d591d2018-01-28 01:13:17 +0100841int combine_notes_overwrite(struct object_id *cur_oid,
842 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100843{
Patryk Obarab7d591d2018-01-28 01:13:17 +0100844 oidcpy(cur_oid, new_oid);
Johan Herland73f464b2010-02-13 22:28:19 +0100845 return 0;
846}
847
Patryk Obarab7d591d2018-01-28 01:13:17 +0100848int combine_notes_ignore(struct object_id *cur_oid,
849 const struct object_id *new_oid)
Johan Herland73f464b2010-02-13 22:28:19 +0100850{
851 return 0;
852}
853
Michael Haggerty13135242012-11-04 08:07:08 +0100854/*
855 * Add the lines from the named object to list, with trailing
856 * newlines removed.
857 */
858static int string_list_add_note_lines(struct string_list *list,
Patryk Obarab7d591d2018-01-28 01:13:17 +0100859 const struct object_id *oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100860{
861 char *data;
862 unsigned long len;
863 enum object_type t;
Johan Herlanda6a09092010-11-15 00:57:17 +0100864
Patryk Obarab7d591d2018-01-28 01:13:17 +0100865 if (is_null_oid(oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100866 return 0;
867
868 /* read_sha1_file NUL-terminates */
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000869 data = read_object_file(oid, &t, &len);
Johan Herlanda6a09092010-11-15 00:57:17 +0100870 if (t != OBJ_BLOB || !data || !len) {
871 free(data);
872 return t != OBJ_BLOB || !data;
873 }
874
Michael Haggerty13135242012-11-04 08:07:08 +0100875 /*
876 * If the last line of the file is EOL-terminated, this will
877 * add an empty string to the list. But it will be removed
878 * later, along with any empty strings that came from empty
879 * lines within the file.
880 */
881 string_list_split(list, data, '\n', -1);
882 free(data);
Johan Herlanda6a09092010-11-15 00:57:17 +0100883 return 0;
884}
885
886static int string_list_join_lines_helper(struct string_list_item *item,
887 void *cb_data)
888{
889 struct strbuf *buf = cb_data;
890 strbuf_addstr(buf, item->string);
891 strbuf_addch(buf, '\n');
892 return 0;
893}
894
Patryk Obarab7d591d2018-01-28 01:13:17 +0100895int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
896 const struct object_id *new_oid)
Johan Herlanda6a09092010-11-15 00:57:17 +0100897{
Michael Haggertyf992f0c2012-11-04 08:07:07 +0100898 struct string_list sort_uniq_list = STRING_LIST_INIT_DUP;
Johan Herlanda6a09092010-11-15 00:57:17 +0100899 struct strbuf buf = STRBUF_INIT;
900 int ret = 1;
901
902 /* read both note blob objects into unique_lines */
Patryk Obarab7d591d2018-01-28 01:13:17 +0100903 if (string_list_add_note_lines(&sort_uniq_list, cur_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100904 goto out;
Patryk Obarab7d591d2018-01-28 01:13:17 +0100905 if (string_list_add_note_lines(&sort_uniq_list, new_oid))
Johan Herlanda6a09092010-11-15 00:57:17 +0100906 goto out;
Michael Haggerty13135242012-11-04 08:07:08 +0100907 string_list_remove_empty_items(&sort_uniq_list, 0);
Michael Haggerty3383e192014-11-25 09:02:35 +0100908 string_list_sort(&sort_uniq_list);
Michael Haggerty13135242012-11-04 08:07:08 +0100909 string_list_remove_duplicates(&sort_uniq_list, 0);
Johan Herlanda6a09092010-11-15 00:57:17 +0100910
911 /* create a new blob object from sort_uniq_list */
912 if (for_each_string_list(&sort_uniq_list,
913 string_list_join_lines_helper, &buf))
914 goto out;
915
Patryk Obaraa09c9852018-01-28 01:13:19 +0100916 ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid);
Johan Herlanda6a09092010-11-15 00:57:17 +0100917
918out:
919 strbuf_release(&buf);
920 string_list_clear(&sort_uniq_list, 0);
921 return ret;
922}
923
Michael Haggertyfd950352015-05-25 18:38:59 +0000924static int string_list_add_one_ref(const char *refname, const struct object_id *oid,
Thomas Rast894a9d32010-03-12 18:04:26 +0100925 int flag, void *cb)
926{
927 struct string_list *refs = cb;
Michael Haggertyd235e992013-05-25 11:08:20 +0200928 if (!unsorted_string_list_has_string(refs, refname))
929 string_list_append(refs, refname);
Thomas Rast894a9d32010-03-12 18:04:26 +0100930 return 0;
931}
932
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200933/*
934 * The list argument must have strdup_strings set on it.
935 */
Thomas Rast894a9d32010-03-12 18:04:26 +0100936void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
937{
Michael Haggerty8c46bf92013-05-25 11:08:21 +0200938 assert(list->strdup_strings);
Thomas Rast894a9d32010-03-12 18:04:26 +0100939 if (has_glob_specials(glob)) {
Michael Haggertyfd950352015-05-25 18:38:59 +0000940 for_each_glob_ref(string_list_add_one_ref, glob, list);
Thomas Rast894a9d32010-03-12 18:04:26 +0100941 } else {
brian m. carlson89c149f2017-05-30 10:30:38 -0700942 struct object_id oid;
943 if (get_oid(glob, &oid))
Thomas Rast894a9d32010-03-12 18:04:26 +0100944 warning("notes ref %s is invalid", glob);
945 if (!unsorted_string_list_has_string(list, glob))
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100946 string_list_append(list, glob);
Thomas Rast894a9d32010-03-12 18:04:26 +0100947 }
948}
949
950void string_list_add_refs_from_colon_sep(struct string_list *list,
951 const char *globs)
952{
Michael Haggerty6fa23772012-11-04 08:07:10 +0100953 struct string_list split = STRING_LIST_INIT_NODUP;
954 char *globs_copy = xstrdup(globs);
Thomas Rast894a9d32010-03-12 18:04:26 +0100955 int i;
956
Michael Haggerty6fa23772012-11-04 08:07:10 +0100957 string_list_split_in_place(&split, globs_copy, ':', -1);
958 string_list_remove_empty_items(&split, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +0100959
Michael Haggerty6fa23772012-11-04 08:07:10 +0100960 for (i = 0; i < split.nr; i++)
961 string_list_add_refs_by_glob(list, split.items[i].string);
Thomas Rast894a9d32010-03-12 18:04:26 +0100962
Michael Haggerty6fa23772012-11-04 08:07:10 +0100963 string_list_clear(&split, 0);
964 free(globs_copy);
Thomas Rast894a9d32010-03-12 18:04:26 +0100965}
966
Thomas Rast894a9d32010-03-12 18:04:26 +0100967static int notes_display_config(const char *k, const char *v, void *cb)
968{
969 int *load_refs = cb;
970
971 if (*load_refs && !strcmp(k, "notes.displayref")) {
972 if (!v)
973 config_error_nonbool(k);
974 string_list_add_refs_by_glob(&display_notes_refs, v);
975 }
976
977 return 0;
978}
979
Johan Herland4a9cf1c2010-11-09 22:49:39 +0100980const char *default_notes_ref(void)
Thomas Rast894a9d32010-03-12 18:04:26 +0100981{
982 const char *notes_ref = NULL;
983 if (!notes_ref)
984 notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
985 if (!notes_ref)
986 notes_ref = notes_ref_name; /* value of core.notesRef config */
987 if (!notes_ref)
988 notes_ref = GIT_NOTES_DEFAULT_REF;
989 return notes_ref;
990}
991
Johan Herland73f464b2010-02-13 22:28:19 +0100992void init_notes(struct notes_tree *t, const char *notes_ref,
993 combine_notes_fn combine_notes, int flags)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200994{
brian m. carlson13ac1412016-09-05 20:08:05 +0000995 struct object_id oid, object_oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -0700996 unsigned short mode;
Johan Herland23123ae2009-10-09 12:22:07 +0200997 struct leaf_node root_tree;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +0200998
Johan Herlandcd305392010-02-13 22:28:18 +0100999 if (!t)
1000 t = &default_notes_tree;
1001 assert(!t->initialized);
Johan Herland709f79b2010-02-13 22:28:12 +01001002
1003 if (!notes_ref)
Thomas Rast894a9d32010-03-12 18:04:26 +01001004 notes_ref = default_notes_ref();
Johan Herland709f79b2010-02-13 22:28:12 +01001005
Johan Herland73f464b2010-02-13 22:28:19 +01001006 if (!combine_notes)
1007 combine_notes = combine_notes_concatenate;
1008
Brian Gesiak65bbf082014-05-27 00:33:52 +09001009 t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
Johan Herland851c2b32010-02-13 22:28:23 +01001010 t->first_non_note = NULL;
1011 t->prev_non_note = NULL;
Jeff King8c53f072015-01-12 20:59:09 -05001012 t->ref = xstrdup_or_null(notes_ref);
Mike Hommeyee76f922015-10-08 11:54:43 +09001013 t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
Johan Herland73f464b2010-02-13 22:28:19 +01001014 t->combine_notes = combine_notes;
Johan Herlandcd305392010-02-13 22:28:18 +01001015 t->initialized = 1;
Thomas Rast7f710ea2010-03-12 18:04:36 +01001016 t->dirty = 0;
Johan Herlandcd305392010-02-13 22:28:18 +01001017
Johan Herland709f79b2010-02-13 22:28:12 +01001018 if (flags & NOTES_INIT_EMPTY || !notes_ref ||
brian m. carlsone82caf32017-07-13 23:49:28 +00001019 get_oid_treeish(notes_ref, &object_oid))
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001020 return;
brian m. carlson34c290a2017-10-15 22:06:56 +00001021 if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
Mike Hommeyee76f922015-10-08 11:54:43 +09001022 die("Cannot use notes ref %s", notes_ref);
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001023 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
Johan Herland709f79b2010-02-13 22:28:12 +01001024 die("Failed to read notes tree referenced by %s (%s)",
brian m. carlson13ac1412016-09-05 20:08:05 +00001025 notes_ref, oid_to_hex(&object_oid));
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001026
brian m. carlson5dcc9692017-05-30 10:30:37 -07001027 oidclr(&root_tree.key_oid);
1028 oidcpy(&root_tree.val_oid, &oid);
Johan Herland851c2b32010-02-13 22:28:23 +01001029 load_subtree(t, &root_tree, t->root, 0);
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001030}
1031
Mike Hommeyee76f922015-10-08 11:54:43 +09001032struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
Thomas Rast894a9d32010-03-12 18:04:26 +01001033{
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001034 struct string_list_item *item;
1035 int counter = 0;
Thomas Rast894a9d32010-03-12 18:04:26 +01001036 struct notes_tree **trees;
Jeff Kingb32fa952016-02-22 17:44:25 -05001037 ALLOC_ARRAY(trees, refs->nr + 1);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001038 for_each_string_list_item(item, refs) {
1039 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
Mike Hommeyee76f922015-10-08 11:54:43 +09001040 init_notes(t, item->string, combine_notes_ignore, flags);
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001041 trees[counter++] = t;
1042 }
1043 trees[counter] = NULL;
Thomas Rast894a9d32010-03-12 18:04:26 +01001044 return trees;
1045}
1046
1047void init_display_notes(struct display_notes_opt *opt)
1048{
Denton Liue6e230e2019-12-09 05:10:41 -08001049 memset(opt, 0, sizeof(*opt));
1050 opt->use_default_notes = -1;
1051}
1052
Denton Liu1d729752019-12-11 16:49:50 -08001053void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
Denton Liu452538c2019-12-09 05:10:44 -08001054{
Denton Liu1d729752019-12-11 16:49:50 -08001055 opt->use_default_notes = 1;
1056 *show_notes = 1;
1057}
Denton Liu452538c2019-12-09 05:10:44 -08001058
Denton Liu1d729752019-12-11 16:49:50 -08001059void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1060 const char *ref) {
1061 struct strbuf buf = STRBUF_INIT;
1062 strbuf_addstr(&buf, ref);
1063 expand_notes_ref(&buf);
1064 string_list_append(&opt->extra_notes_refs,
1065 strbuf_detach(&buf, NULL));
1066 *show_notes = 1;
1067}
1068
1069void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1070{
1071 opt->use_default_notes = -1;
1072 /* we have been strdup'ing ourselves, so trick
1073 * string_list into free()ing strings */
1074 opt->extra_notes_refs.strdup_strings = 1;
1075 string_list_clear(&opt->extra_notes_refs, 0);
1076 opt->extra_notes_refs.strdup_strings = 0;
1077 *show_notes = 0;
Denton Liu452538c2019-12-09 05:10:44 -08001078}
1079
Denton Liu1e6ed542019-12-09 05:10:39 -08001080void load_display_notes(struct display_notes_opt *opt)
Thomas Rast894a9d32010-03-12 18:04:26 +01001081{
1082 char *display_ref_env;
1083 int load_config_refs = 0;
1084 display_notes_refs.strdup_strings = 1;
1085
1086 assert(!display_notes_trees);
1087
Jeff King3a03cf62011-03-29 16:57:27 -04001088 if (!opt || opt->use_default_notes > 0 ||
1089 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +01001090 string_list_append(&display_notes_refs, default_notes_ref());
Thomas Rast894a9d32010-03-12 18:04:26 +01001091 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1092 if (display_ref_env) {
1093 string_list_add_refs_from_colon_sep(&display_notes_refs,
1094 display_ref_env);
1095 load_config_refs = 0;
1096 } else
1097 load_config_refs = 1;
1098 }
1099
1100 git_config(notes_display_config, &load_config_refs);
1101
Jeff King304cc112011-03-29 16:56:53 -04001102 if (opt) {
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001103 struct string_list_item *item;
Jeff King304cc112011-03-29 16:56:53 -04001104 for_each_string_list_item(item, &opt->extra_notes_refs)
Alex Riesen8a57c6e2010-07-03 14:41:54 +02001105 string_list_add_refs_by_glob(&display_notes_refs,
1106 item->string);
1107 }
Thomas Rast894a9d32010-03-12 18:04:26 +01001108
Mike Hommeyee76f922015-10-08 11:54:43 +09001109 display_notes_trees = load_notes_trees(&display_notes_refs, 0);
Thomas Rast894a9d32010-03-12 18:04:26 +01001110 string_list_clear(&display_notes_refs, 0);
1111}
1112
brian m. carlson5ee8a952017-05-30 10:30:43 -07001113int add_note(struct notes_tree *t, const struct object_id *object_oid,
1114 const struct object_id *note_oid, combine_notes_fn combine_notes)
Johan Herland2626b532010-02-13 22:28:13 +01001115{
1116 struct leaf_node *l;
1117
Johan Herlandcd305392010-02-13 22:28:18 +01001118 if (!t)
1119 t = &default_notes_tree;
1120 assert(t->initialized);
Thomas Rast7f710ea2010-03-12 18:04:36 +01001121 t->dirty = 1;
Johan Herland73f464b2010-02-13 22:28:19 +01001122 if (!combine_notes)
1123 combine_notes = t->combine_notes;
Johan Herland2626b532010-02-13 22:28:13 +01001124 l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
brian m. carlson5ee8a952017-05-30 10:30:43 -07001125 oidcpy(&l->key_oid, object_oid);
1126 oidcpy(&l->val_oid, note_oid);
Johan Herland180619a2010-11-15 00:52:26 +01001127 return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
Johan Herland2626b532010-02-13 22:28:13 +01001128}
1129
Johan Herland1ee1e432010-08-31 17:56:50 +02001130int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
Johan Herland1ec666b2010-02-13 22:28:14 +01001131{
1132 struct leaf_node l;
1133
Johan Herlandcd305392010-02-13 22:28:18 +01001134 if (!t)
1135 t = &default_notes_tree;
1136 assert(t->initialized);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001137 hashcpy(l.key_oid.hash, object_sha1);
1138 oidclr(&l.val_oid);
Brandon Caseya502ab92010-03-18 10:03:43 -05001139 note_tree_remove(t, t->root, 0, &l);
brian m. carlson5dcc9692017-05-30 10:30:37 -07001140 if (is_null_oid(&l.val_oid)) /* no note was removed */
Johan Herland1ee1e432010-08-31 17:56:50 +02001141 return 1;
1142 t->dirty = 1;
1143 return 0;
Johan Herland1ec666b2010-02-13 22:28:14 +01001144}
1145
brian m. carlson9ef72232017-05-30 10:30:40 -07001146const struct object_id *get_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001147 const struct object_id *oid)
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001148{
Johan Herland9b391f22010-02-13 22:28:15 +01001149 struct leaf_node *found;
1150
Johan Herlandcd305392010-02-13 22:28:18 +01001151 if (!t)
1152 t = &default_notes_tree;
1153 assert(t->initialized);
brian m. carlson5ee8a952017-05-30 10:30:43 -07001154 found = note_tree_find(t, t->root, 0, oid->hash);
brian m. carlson9ef72232017-05-30 10:30:40 -07001155 return found ? &found->val_oid : NULL;
Johannes Schindelinfd53c9e2009-10-09 12:21:59 +02001156}
Johannes Schindelina97a7462009-10-09 12:21:57 +02001157
Johan Herlandcd305392010-02-13 22:28:18 +01001158int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
1159 void *cb_data)
Johan Herland73f77b92010-02-13 22:28:16 +01001160{
Johan Herlandcd305392010-02-13 22:28:18 +01001161 if (!t)
1162 t = &default_notes_tree;
1163 assert(t->initialized);
Johan Herland851c2b32010-02-13 22:28:23 +01001164 return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
Johan Herland73f77b92010-02-13 22:28:16 +01001165}
1166
Patryk Obarabbca96d2018-01-28 01:13:18 +01001167int write_notes_tree(struct notes_tree *t, struct object_id *result)
Johan Herland61a7cca2010-02-13 22:28:17 +01001168{
1169 struct tree_write_stack root;
1170 struct write_each_note_data cb_data;
1171 int ret;
Patryk Obarabbca96d2018-01-28 01:13:18 +01001172 int flags;
Johan Herland61a7cca2010-02-13 22:28:17 +01001173
Johan Herlandcd305392010-02-13 22:28:18 +01001174 if (!t)
1175 t = &default_notes_tree;
1176 assert(t->initialized);
Johan Herland61a7cca2010-02-13 22:28:17 +01001177
1178 /* Prepare for traversal of current notes tree */
1179 root.next = NULL; /* last forward entry in list is grounded */
brian m. carlsondd437452019-02-19 00:05:01 +00001180 strbuf_init(&root.buf, 256 * (32 + the_hash_algo->hexsz)); /* assume 256 entries */
Johan Herland61a7cca2010-02-13 22:28:17 +01001181 root.path[0] = root.path[1] = '\0';
1182 cb_data.root = &root;
Johan Herlanddbc27472020-02-03 22:04:45 +01001183 cb_data.nn_list = &(t->first_non_note);
1184 cb_data.nn_prev = NULL;
Johan Herland61a7cca2010-02-13 22:28:17 +01001185
1186 /* Write tree objects representing current notes tree */
Patryk Obarabbca96d2018-01-28 01:13:18 +01001187 flags = FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
1188 FOR_EACH_NOTE_YIELD_SUBTREES;
1189 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1190 write_each_non_note_until(NULL, &cb_data) ||
1191 tree_write_stack_finish_subtree(&root) ||
Patryk Obaraa09c9852018-01-28 01:13:19 +01001192 write_object_file(root.buf.buf, root.buf.len, tree_type, result);
Johan Herland61a7cca2010-02-13 22:28:17 +01001193 strbuf_release(&root.buf);
1194 return ret;
1195}
1196
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001197void prune_notes(struct notes_tree *t, int flags)
Johan Herland00fbe632010-02-13 22:28:27 +01001198{
1199 struct note_delete_list *l = NULL;
1200
1201 if (!t)
1202 t = &default_notes_tree;
1203 assert(t->initialized);
1204
1205 for_each_note(t, 0, prune_notes_helper, &l);
1206
1207 while (l) {
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001208 if (flags & NOTES_PRUNE_VERBOSE)
brian m. carlson0dbc6462019-02-19 00:05:02 +00001209 printf("%s\n", hash_to_hex(l->sha1));
Michael J Grubera9f2adf2010-05-14 23:42:07 +02001210 if (!(flags & NOTES_PRUNE_DRYRUN))
1211 remove_note(t, l->sha1);
Johan Herland00fbe632010-02-13 22:28:27 +01001212 l = l->next;
1213 }
1214}
1215
Johan Herlandcd305392010-02-13 22:28:18 +01001216void free_notes(struct notes_tree *t)
Johan Herland27d57562009-10-09 12:22:06 +02001217{
Johan Herlandcd305392010-02-13 22:28:18 +01001218 if (!t)
1219 t = &default_notes_tree;
1220 if (t->root)
1221 note_tree_free(t->root);
1222 free(t->root);
Johan Herland851c2b32010-02-13 22:28:23 +01001223 while (t->first_non_note) {
1224 t->prev_non_note = t->first_non_note->next;
1225 free(t->first_non_note->path);
1226 free(t->first_non_note);
1227 t->first_non_note = t->prev_non_note;
1228 }
Johan Herlandcd305392010-02-13 22:28:18 +01001229 free(t->ref);
1230 memset(t, 0, sizeof(struct notes_tree));
Johan Herland27d57562009-10-09 12:22:06 +02001231}
1232
Junio C Hamano96531a52012-09-15 14:08:39 -07001233/*
1234 * Fill the given strbuf with the notes associated with the given object.
1235 *
1236 * If the given notes_tree structure is not initialized, it will be auto-
1237 * initialized to the default value (see documentation for init_notes() above).
1238 * If the given notes_tree is NULL, the internal/default notes_tree will be
1239 * used instead.
1240 *
Junio C Hamano76141e22012-10-17 21:41:54 -07001241 * (raw != 0) gives the %N userformat; otherwise, the note message is given
1242 * for human consumption.
Junio C Hamano96531a52012-09-15 14:08:39 -07001243 */
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001244static void format_note(struct notes_tree *t, const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001245 struct strbuf *sb, const char *output_encoding, int raw)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001246{
1247 static const char utf8[] = "utf-8";
brian m. carlson9ef72232017-05-30 10:30:40 -07001248 const struct object_id *oid;
Johannes Schindelina97a7462009-10-09 12:21:57 +02001249 char *msg, *msg_p;
1250 unsigned long linelen, msglen;
1251 enum object_type type;
1252
Johan Herlandcd305392010-02-13 22:28:18 +01001253 if (!t)
1254 t = &default_notes_tree;
1255 if (!t->initialized)
Johan Herland73f464b2010-02-13 22:28:19 +01001256 init_notes(t, NULL, NULL, 0);
Johannes Schindelina97a7462009-10-09 12:21:57 +02001257
brian m. carlson5ee8a952017-05-30 10:30:43 -07001258 oid = get_note(t, object_oid);
brian m. carlson9ef72232017-05-30 10:30:40 -07001259 if (!oid)
Johannes Schindelina97a7462009-10-09 12:21:57 +02001260 return;
1261
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001262 if (!(msg = read_object_file(oid, &type, &msglen)) || type != OBJ_BLOB) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001263 free(msg);
1264 return;
1265 }
1266
1267 if (output_encoding && *output_encoding &&
Junio C Hamano0e18bcd2012-10-18 22:41:56 -07001268 !is_encoding_utf8(output_encoding)) {
Johannes Schindelina97a7462009-10-09 12:21:57 +02001269 char *reencoded = reencode_string(msg, output_encoding, utf8);
1270 if (reencoded) {
1271 free(msg);
1272 msg = reencoded;
1273 msglen = strlen(msg);
1274 }
1275 }
1276
1277 /* we will end the annotation by a newline anyway */
1278 if (msglen && msg[msglen - 1] == '\n')
1279 msglen--;
1280
Junio C Hamano76141e22012-10-17 21:41:54 -07001281 if (!raw) {
Thomas Rast894a9d32010-03-12 18:04:26 +01001282 const char *ref = t->ref;
1283 if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
1284 strbuf_addstr(sb, "\nNotes:\n");
1285 } else {
Junio C Hamano145136a2020-01-30 11:35:46 -08001286 skip_prefix(ref, "refs/", &ref);
1287 skip_prefix(ref, "notes/", &ref);
Thomas Rast894a9d32010-03-12 18:04:26 +01001288 strbuf_addf(sb, "\nNotes (%s):\n", ref);
1289 }
1290 }
Johannes Schindelina97a7462009-10-09 12:21:57 +02001291
1292 for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
1293 linelen = strchrnul(msg_p, '\n') - msg_p;
1294
Junio C Hamano76141e22012-10-17 21:41:54 -07001295 if (!raw)
Johan Herlandc56fcc82009-10-09 12:22:04 +02001296 strbuf_addstr(sb, " ");
Johannes Schindelina97a7462009-10-09 12:21:57 +02001297 strbuf_add(sb, msg_p, linelen);
1298 strbuf_addch(sb, '\n');
1299 }
1300
1301 free(msg);
1302}
Thomas Rast894a9d32010-03-12 18:04:26 +01001303
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001304void format_display_notes(const struct object_id *object_oid,
Junio C Hamano76141e22012-10-17 21:41:54 -07001305 struct strbuf *sb, const char *output_encoding, int raw)
Thomas Rast894a9d32010-03-12 18:04:26 +01001306{
1307 int i;
1308 assert(display_notes_trees);
1309 for (i = 0; display_notes_trees[i]; i++)
brian m. carlsonfb61e4d2017-05-30 10:30:41 -07001310 format_note(display_notes_trees[i], object_oid, sb,
Junio C Hamano76141e22012-10-17 21:41:54 -07001311 output_encoding, raw);
Thomas Rast894a9d32010-03-12 18:04:26 +01001312}
Thomas Rast160baa02010-03-12 18:04:31 +01001313
1314int copy_note(struct notes_tree *t,
brian m. carlson5ee8a952017-05-30 10:30:43 -07001315 const struct object_id *from_obj, const struct object_id *to_obj,
Johan Herland180619a2010-11-15 00:52:26 +01001316 int force, combine_notes_fn combine_notes)
Thomas Rast160baa02010-03-12 18:04:31 +01001317{
brian m. carlson9ef72232017-05-30 10:30:40 -07001318 const struct object_id *note = get_note(t, from_obj);
1319 const struct object_id *existing_note = get_note(t, to_obj);
Thomas Rast160baa02010-03-12 18:04:31 +01001320
1321 if (!force && existing_note)
1322 return 1;
1323
1324 if (note)
brian m. carlson5ee8a952017-05-30 10:30:43 -07001325 return add_note(t, to_obj, note, combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001326 else if (existing_note)
brian m. carlson5ee8a952017-05-30 10:30:43 -07001327 return add_note(t, to_obj, &null_oid, combine_notes);
Thomas Rast160baa02010-03-12 18:04:31 +01001328
1329 return 0;
1330}
Jeff King03bb5782011-03-29 16:55:32 -04001331
1332void expand_notes_ref(struct strbuf *sb)
1333{
Christian Couder59556542013-11-30 21:55:40 +01001334 if (starts_with(sb->buf, "refs/notes/"))
Jeff King03bb5782011-03-29 16:55:32 -04001335 return; /* we're happy */
Christian Couder59556542013-11-30 21:55:40 +01001336 else if (starts_with(sb->buf, "notes/"))
René Scharfea91cc7f2020-02-09 14:44:23 +01001337 strbuf_insertstr(sb, 0, "refs/");
Jeff King03bb5782011-03-29 16:55:32 -04001338 else
René Scharfea91cc7f2020-02-09 14:44:23 +01001339 strbuf_insertstr(sb, 0, "refs/notes/");
Jeff King03bb5782011-03-29 16:55:32 -04001340}
Jacob Kellerb3715b72015-12-29 14:40:28 -08001341
1342void expand_loose_notes_ref(struct strbuf *sb)
1343{
brian m. carlson89c149f2017-05-30 10:30:38 -07001344 struct object_id object;
Jacob Kellerb3715b72015-12-29 14:40:28 -08001345
brian m. carlson89c149f2017-05-30 10:30:38 -07001346 if (get_oid(sb->buf, &object)) {
Jacob Kellerb3715b72015-12-29 14:40:28 -08001347 /* fallback to expand_notes_ref */
1348 expand_notes_ref(sb);
1349 }
1350}