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