Coverage Report

Created: 2026-01-09 07:10

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