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