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