37
unsigned char val_sha1[20];
38
};
39
40
+/*
41
+ * A notes tree may contain entries that are not notes, and that do not follow
42
+ * the naming conventions of notes. There are typically none/few of these, but
43
+ * we still need to keep track of them. Keep a simple linked list sorted alpha-
44
+ * betically on the non-note path. The list is populated when parsing tree
45
+ * objects in load_subtree(), and the non-notes are correctly written back into
46
+ * the tree objects produced by write_notes_tree().
47
+ */
48
+struct non_note {
49
+ struct non_note *next; /* grounded (last->next == NULL) */
50
+ char *path;
51
+ unsigned int mode;
52
+ unsigned char sha1[20];
53
+};
54
+
55
#define PTR_TYPE_NULL 0
56
#define PTR_TYPE_INTERNAL 1
57
#define PTR_TYPE_NOTE 2
68
69
struct notes_tree default_notes_tree;
70
56
-static void load_subtree(struct leaf_node *subtree, struct int_node *node,
57
- unsigned int n);
71
+static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
72
+ struct int_node *node, unsigned int n);
73
74
/*
75
* Search the tree until the appropriate location for the given key is found:
86
* - an unused leaf node (NULL)
87
* In any case, set *tree and *n, and return pointer to the tree location.
88
*/
74
-static void **note_tree_search(struct int_node **tree,
89
+static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
90
unsigned char *n, const unsigned char *key_sha1)
91
{
92
struct leaf_node *l;
98
if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
99
/* unpack tree and resume search */
100
(*tree)->a[0] = NULL;
86
- load_subtree(l, *tree, *n);
101
+ load_subtree(t, l, *tree, *n);
102
free(l);
88
- return note_tree_search(tree, n, key_sha1);
103
+ return note_tree_search(t, tree, n, key_sha1);
104
}
105
}
106
110
case PTR_TYPE_INTERNAL:
111
*tree = CLR_PTR_TYPE(p);
112
(*n)++;
98
- return note_tree_search(tree, n, key_sha1);
113
+ return note_tree_search(t, tree, n, key_sha1);
114
case PTR_TYPE_SUBTREE:
115
l = (struct leaf_node *) CLR_PTR_TYPE(p);
116
if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
117
/* unpack tree and resume search */
118
(*tree)->a[i] = NULL;
104
- load_subtree(l, *tree, *n);
119
+ load_subtree(t, l, *tree, *n);
120
free(l);
106
- return note_tree_search(tree, n, key_sha1);
121
+ return note_tree_search(t, tree, n, key_sha1);
122
}
123
/* fall through */
124
default:
131
* Search to the tree location appropriate for the given key:
132
* If a note entry with matching key, return the note entry, else return NULL.
133
*/
119
-static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,
134
+static struct leaf_node *note_tree_find(struct notes_tree *t,
135
+ struct int_node *tree, unsigned char n,
136
const unsigned char *key_sha1)
137
{
122
- void **p = note_tree_search(&tree, &n, key_sha1);
138
+ void **p = note_tree_search(t, &tree, &n, key_sha1);
139
if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
140
struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
141
if (!hashcmp(key_sha1, l->key_sha1))
157
* - Else, create a new int_node, holding both the node-at-location and the
158
* node-to-be-inserted, and store the new int_node into the location.
159
*/
144
-static void note_tree_insert(struct int_node *tree, unsigned char n,
145
- struct leaf_node *entry, unsigned char type,
160
+static void note_tree_insert(struct notes_tree *t, struct int_node *tree,
161
+ unsigned char n, struct leaf_node *entry, unsigned char type,
162
combine_notes_fn combine_notes)
163
{
164
struct int_node *new_node;
165
struct leaf_node *l;
150
- void **p = note_tree_search(&tree, &n, entry->key_sha1);
166
+ void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
167
168
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
169
l = (struct leaf_node *) CLR_PTR_TYPE(*p);
194
if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
195
entry->key_sha1)) {
196
/* unpack 'entry' */
181
- load_subtree(entry, tree, n);
197
+ load_subtree(t, entry, tree, n);
198
free(entry);
199
return;
200
}
205
if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
206
/* unpack 'l' and restart insert */
207
*p = NULL;
192
- load_subtree(l, tree, n);
208
+ load_subtree(t, l, tree, n);
209
free(l);
194
- note_tree_insert(tree, n, entry, type, combine_notes);
210
+ note_tree_insert(t, tree, n, entry, type,
211
+ combine_notes);
212
return;
213
}
214
break;
218
assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
219
GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
220
new_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
204
- note_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes);
221
+ note_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),
222
+ combine_notes);
223
*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);
206
- note_tree_insert(new_node, n + 1, entry, type, combine_notes);
224
+ note_tree_insert(t, new_node, n + 1, entry, type, combine_notes);
225
}
226
227
/*
267
struct leaf_node *l;
268
struct int_node *parent_stack[20];
269
unsigned char i, j;
252
- void **p = note_tree_search(&tree, &n, entry->key_sha1);
270
+ void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
271
272
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
273
if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
342
return len;
343
}
344
327
-static void load_subtree(struct leaf_node *subtree, struct int_node *node,
328
- unsigned int n)
345
+static int non_note_cmp(const struct non_note *a, const struct non_note *b)
346
+{
347
+ return strcmp(a->path, b->path);
348
+}
349
+
350
+static void add_non_note(struct notes_tree *t, const char *path,
351
+ unsigned int mode, const unsigned char *sha1)
352
+{
353
+ struct non_note *p = t->prev_non_note, *n;
354
+ n = (struct non_note *) xmalloc(sizeof(struct non_note));
355
+ n->next = NULL;
356
+ n->path = xstrdup(path);
357
+ n->mode = mode;
358
+ hashcpy(n->sha1, sha1);
359
+ t->prev_non_note = n;
360
+
361
+ if (!t->first_non_note) {
362
+ t->first_non_note = n;
363
+ return;
364
+ }
365
+
366
+ if (non_note_cmp(p, n) < 0)
367
+ ; /* do nothing */
368
+ else if (non_note_cmp(t->first_non_note, n) <= 0)
369
+ p = t->first_non_note;
370
+ else {
371
+ /* n sorts before t->first_non_note */
372
+ n->next = t->first_non_note;
373
+ t->first_non_note = n;
374
+ return;
375
+ }
376
+
377
+ /* n sorts equal or after p */
378
+ while (p->next && non_note_cmp(p->next, n) <= 0)
379
+ p = p->next;
380
+
381
+ if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
382
+ assert(strcmp(p->path, n->path) == 0);
383
+ p->mode = n->mode;
384
+ hashcpy(p->sha1, n->sha1);
385
+ free(n);
386
+ t->prev_non_note = p;
387
+ return;
388
+ }
389
+
390
+ /* n sorts between p and p->next */
391
+ n->next = p->next;
392
+ p->next = n;
393
+}
394
+
395
+static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
396
+ struct int_node *node, unsigned int n)
397
{
398
unsigned char object_sha1[20];
399
unsigned int prefix_len;
400
void *buf;
401
struct tree_desc desc;
402
struct name_entry entry;
403
+ int len, path_len;
404
+ unsigned char type;
405
+ struct leaf_node *l;
406
407
buf = fill_tree_descriptor(&desc, subtree->val_sha1);
408
if (!buf)
413
assert(prefix_len * 2 >= n);
414
memcpy(object_sha1, subtree->key_sha1, prefix_len);
415
while (tree_entry(&desc, &entry)) {
345
- int len = get_sha1_hex_segment(entry.path, strlen(entry.path),
416
+ path_len = strlen(entry.path);
417
+ len = get_sha1_hex_segment(entry.path, path_len,
418
object_sha1 + prefix_len, 20 - prefix_len);
419
if (len < 0)
348
- continue; /* entry.path is not a SHA1 sum. Skip */
420
+ goto handle_non_note; /* entry.path is not a SHA1 */
421
len += prefix_len;
422
423
/*
424
* If object SHA1 is complete (len == 20), assume note object
353
- * If object SHA1 is incomplete (len < 20), assume note subtree
425
+ * If object SHA1 is incomplete (len < 20), and current
426
+ * component consists of 2 hex chars, assume note subtree
427
*/
428
if (len <= 20) {
356
- unsigned char type = PTR_TYPE_NOTE;
357
- struct leaf_node *l = (struct leaf_node *)
429
+ type = PTR_TYPE_NOTE;
430
+ l = (struct leaf_node *)
431
xcalloc(sizeof(struct leaf_node), 1);
432
hashcpy(l->key_sha1, object_sha1);
433
hashcpy(l->val_sha1, entry.sha1);
434
if (len < 20) {
362
- if (!S_ISDIR(entry.mode))
363
- continue; /* entry cannot be subtree */
435
+ if (!S_ISDIR(entry.mode) || path_len != 2)
436
+ goto handle_non_note; /* not subtree */
437
l->key_sha1[19] = (unsigned char) len;
438
type = PTR_TYPE_SUBTREE;
439
}
367
- note_tree_insert(node, n, l, type,
440
+ note_tree_insert(t, node, n, l, type,
441
combine_notes_concatenate);
442
}
443
+ continue;
444
+
445
+handle_non_note:
446
+ /*
447
+ * Determine full path for this non-note entry:
448
+ * The filename is already found in entry.path, but the
449
+ * directory part of the path must be deduced from the subtree
450
+ * containing this entry. We assume here that the overall notes
451
+ * tree follows a strict byte-based progressive fanout
452
+ * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
453
+ * e.g. 4/36 fanout). This means that if a non-note is found at
454
+ * path "dead/beef", the following code will register it as
455
+ * being found on "de/ad/beef".
456
+ * On the other hand, if you use such non-obvious non-note
457
+ * paths in the middle of a notes tree, you deserve what's
458
+ * coming to you ;). Note that for non-notes that are not
459
+ * SHA1-like at the top level, there will be no problems.
460
+ *
461
+ * To conclude, it is strongly advised to make sure non-notes
462
+ * have at least one non-hex character in the top-level path
463
+ * component.
464
+ */
465
+ {
466
+ char non_note_path[PATH_MAX];
467
+ char *p = non_note_path;
468
+ const char *q = sha1_to_hex(subtree->key_sha1);
469
+ int i;
470
+ for (i = 0; i < prefix_len; i++) {
471
+ *p++ = *q++;
472
+ *p++ = *q++;
473
+ *p++ = '/';
474
+ }
475
+ strcpy(p, entry.path);
476
+ add_non_note(t, non_note_path, entry.mode, entry.sha1);
477
+ }
478
}
479
free(buf);
480
}
534
strcpy(path + i, hex_sha1 + j);
535
}
536
429
-static int for_each_note_helper(struct int_node *tree, unsigned char n,
430
- unsigned char fanout, int flags, each_note_fn fn,
431
- void *cb_data)
537
+static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,
538
+ unsigned char n, unsigned char fanout, int flags,
539
+ each_note_fn fn, void *cb_data)
540
{
541
unsigned int i;
542
void *p;
551
switch (GET_PTR_TYPE(p)) {
552
case PTR_TYPE_INTERNAL:
553
/* recurse into int_node */
446
- ret = for_each_note_helper(CLR_PTR_TYPE(p), n + 1,
554
+ ret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,
555
fanout, flags, fn, cb_data);
556
break;
557
case PTR_TYPE_SUBTREE:
589
!(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {
590
/* unpack subtree and resume traversal */
591
tree->a[i] = NULL;
484
- load_subtree(l, tree, n);
592
+ load_subtree(t, l, tree, n);
593
free(l);
594
goto redo;
595
}
704
705
struct write_each_note_data {
706
struct tree_write_stack *root;
707
+ struct non_note *next_non_note;
708
};
709
710
+static int write_each_non_note_until(const char *note_path,
711
+ struct write_each_note_data *d)
712
+{
713
+ struct non_note *n = d->next_non_note;
714
+ int cmp, ret;
715
+ while (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {
716
+ if (note_path && cmp == 0)
717
+ ; /* do nothing, prefer note to non-note */
718
+ else {
719
+ ret = write_each_note_helper(d->root, n->path, n->mode,
720
+ n->sha1);
721
+ if (ret)
722
+ return ret;
723
+ }
724
+ n = n->next;
725
+ }
726
+ d->next_non_note = n;
727
+ return 0;
728
+}
729
+
730
static int write_each_note(const unsigned char *object_sha1,
731
const unsigned char *note_sha1, char *note_path,
732
void *cb_data)
744
}
745
assert(note_path_len <= 40 + 19);
746
618
- return write_each_note_helper(d->root, note_path, mode, note_sha1);
747
+ /* Weave non-note entries into note entries */
748
+ return write_each_non_note_until(note_path, d) ||
749
+ write_each_note_helper(d->root, note_path, mode, note_sha1);
750
}
751
752
int combine_notes_concatenate(unsigned char *cur_sha1,
827
combine_notes = combine_notes_concatenate;
828
829
t->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1);
830
+ t->first_non_note = NULL;
831
+ t->prev_non_note = NULL;
832
t->ref = notes_ref ? xstrdup(notes_ref) : NULL;
833
t->combine_notes = combine_notes;
834
t->initialized = 1;
842
843
hashclr(root_tree.key_sha1);
844
hashcpy(root_tree.val_sha1, sha1);
712
- load_subtree(&root_tree, t->root, 0);
845
+ load_subtree(t, &root_tree, t->root, 0);
846
}
847
848
void add_note(struct notes_tree *t, const unsigned char *object_sha1,
858
l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
859
hashcpy(l->key_sha1, object_sha1);
860
hashcpy(l->val_sha1, note_sha1);
728
- note_tree_insert(t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
861
+ note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
862
}
863
864
void remove_note(struct notes_tree *t, const unsigned char *object_sha1)
881
if (!t)
882
t = &default_notes_tree;
883
assert(t->initialized);
751
- found = note_tree_find(t->root, 0, object_sha1);
884
+ found = note_tree_find(t, t->root, 0, object_sha1);
885
return found ? found->val_sha1 : NULL;
886
}
887
891
if (!t)
892
t = &default_notes_tree;
893
assert(t->initialized);
761
- return for_each_note_helper(t->root, 0, 0, flags, fn, cb_data);
894
+ return for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);
895
}
896
897
int write_notes_tree(struct notes_tree *t, unsigned char *result)
909
strbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */
910
root.path[0] = root.path[1] = '\0';
911
cb_data.root = &root;
912
+ cb_data.next_non_note = t->first_non_note;
913
914
/* Write tree objects representing current notes tree */
915
ret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |
916
FOR_EACH_NOTE_YIELD_SUBTREES,
917
write_each_note, &cb_data) ||
918
+ write_each_non_note_until(NULL, &cb_data) ||
919
tree_write_stack_finish_subtree(&root) ||
920
write_sha1_file(root.buf.buf, root.buf.len, tree_type, result);
921
strbuf_release(&root.buf);
929
if (t->root)
930
note_tree_free(t->root);
931
free(t->root);
932
+ while (t->first_non_note) {
933
+ t->prev_non_note = t->first_non_note->next;
934
+ free(t->first_non_note->path);
935
+ free(t->first_non_note);
936
+ t->first_non_note = t->prev_non_note;
937
+ }
938
free(t->ref);
939
memset(t, 0, sizeof(struct notes_tree));
940
}