THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1316 lines
36KB

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