notes: convert internal structures to struct object_id
Convert the internal structures using unsigned char [20] to take struct object_id using the following semantic patch and the standard object_id transforms: @@ struct leaf_node E1; @@ - E1.key_sha1 + E1.key_oid.hash @@ struct leaf_node *E1; @@ - E1->key_sha1 + E1->key_oid.hash @@ struct leaf_node E1; @@ - E1.key_sha1 + E1.key_oid.hash @@ struct leaf_node *E1; @@ - E1->key_sha1 + E1->key_oid.hash @@ struct non_note E1; @@ - E1.sha1 + E1.oid.hash @@ struct non_note *E1; @@ - E1->sha1 + E1->oid.hash Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
May 30, 2017 at 10:30 UTC
5dcc969e79ebb1e9c1996ffd972bb76467d3bc84
1 file changed
+51
-47
notes.c
+51
-47
@@ -35,8 +35,8 @@ struct int_node {
35
* subtree.
36
*/
37
struct leaf_node {
38
- unsigned char key_sha1[20];
39
- unsigned char val_sha1[20];
38
+ struct object_id key_oid;
39
+ struct object_id val_oid;
40
};
41
42
/*
@@ -51,7 +51,7 @@ struct non_note {
51
struct non_note *next; /* grounded (last->next == NULL) */
52
char *path;
53
unsigned int mode;
54
- unsigned char sha1[20];
54
+ struct object_id oid;
55
};
56
57
#define PTR_TYPE_NULL 0
@@ -100,7 +100,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
100
101
if (GET_PTR_TYPE(p) == PTR_TYPE_SUBTREE) {
102
l = (struct leaf_node *) CLR_PTR_TYPE(p);
103
- if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
103
+ if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
104
/* unpack tree and resume search */
105
(*tree)->a[0] = NULL;
106
load_subtree(t, l, *tree, *n);
@@ -118,7 +118,7 @@ static void **note_tree_search(struct notes_tree *t, struct int_node **tree,
118
return note_tree_search(t, tree, n, key_sha1);
119
case PTR_TYPE_SUBTREE:
120
l = (struct leaf_node *) CLR_PTR_TYPE(p);
121
- if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {
121
+ if (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_oid.hash)) {
122
/* unpack tree and resume search */
123
(*tree)->a[i] = NULL;
124
load_subtree(t, l, *tree, *n);
@@ -143,7 +143,7 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
143
void **p = note_tree_search(t, &tree, &n, key_sha1);
144
if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
145
struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
146
- if (!hashcmp(key_sha1, l->key_sha1))
146
+ if (!hashcmp(key_sha1, l->key_oid.hash))
147
return l;
148
}
149
return NULL;
@@ -196,17 +196,17 @@ static void note_tree_remove(struct notes_tree *t,
196
struct leaf_node *l;
197
struct int_node *parent_stack[20];
198
unsigned char i, j;
199
- void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
199
+ void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
200
201
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
202
if (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)
203
return; /* type mismatch, nothing to remove */
204
l = (struct leaf_node *) CLR_PTR_TYPE(*p);
205
- if (hashcmp(l->key_sha1, entry->key_sha1))
205
+ if (oidcmp(&l->key_oid, &entry->key_oid))
206
return; /* key mismatch, nothing to remove */
207
208
/* we have found a matching entry */
209
- hashcpy(entry->val_sha1, l->val_sha1);
209
+ oidcpy(&entry->val_oid, &l->val_oid);
210
free(l);
211
*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);
212
@@ -216,14 +216,14 @@ static void note_tree_remove(struct notes_tree *t,
216
/* first, build stack of ancestors between root and current node */
217
parent_stack[0] = t->root;
218
for (i = 0; i < n; i++) {
219
- j = GET_NIBBLE(i, entry->key_sha1);
219
+ j = GET_NIBBLE(i, entry->key_oid.hash);
220
parent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);
221
}
222
assert(i == n && parent_stack[i] == tree);
223
/* next, unwind stack until note_tree_consolidate() is done */
224
while (i > 0 &&
225
!note_tree_consolidate(parent_stack[i], parent_stack[i - 1],
226
- GET_NIBBLE(i - 1, entry->key_sha1)))
226
+ GET_NIBBLE(i - 1, entry->key_oid.hash)))
227
i--;
228
}
229
@@ -246,7 +246,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
246
{
247
struct int_node *new_node;
248
struct leaf_node *l;
249
- void **p = note_tree_search(t, &tree, &n, entry->key_sha1);
249
+ void **p = note_tree_search(t, &tree, &n, entry->key_oid.hash);
250
int ret = 0;
251
252
assert(GET_PTR_TYPE(entry) == 0); /* no type bits set */
@@ -254,7 +254,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
254
switch (GET_PTR_TYPE(*p)) {
255
case PTR_TYPE_NULL:
256
assert(!*p);
257
- if (is_null_sha1(entry->val_sha1))
257
+ if (is_null_oid(&entry->val_oid))
258
free(entry);
259
else
260
*p = SET_PTR_TYPE(entry, type);
@@ -262,22 +262,22 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
262
case PTR_TYPE_NOTE:
263
switch (type) {
264
case PTR_TYPE_NOTE:
265
- if (!hashcmp(l->key_sha1, entry->key_sha1)) {
265
+ if (!oidcmp(&l->key_oid, &entry->key_oid)) {
266
/* skip concatenation if l == entry */
267
- if (!hashcmp(l->val_sha1, entry->val_sha1))
267
+ if (!oidcmp(&l->val_oid, &entry->val_oid))
268
return 0;
269
270
- ret = combine_notes(l->val_sha1,
271
- entry->val_sha1);
272
- if (!ret && is_null_sha1(l->val_sha1))
270
+ ret = combine_notes(l->val_oid.hash,
271
+ entry->val_oid.hash);
272
+ if (!ret && is_null_oid(&l->val_oid))
273
note_tree_remove(t, tree, n, entry);
274
free(entry);
275
return ret;
276
}
277
break;
278
case PTR_TYPE_SUBTREE:
279
- if (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,
280
- entry->key_sha1)) {
279
+ if (!SUBTREE_SHA1_PREFIXCMP(l->key_oid.hash,
280
+ entry->key_oid.hash)) {
281
/* unpack 'entry' */
282
load_subtree(t, entry, tree, n);
283
free(entry);
@@ -287,7 +287,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
287
}
288
break;
289
case PTR_TYPE_SUBTREE:
290
- if (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {
290
+ if (!SUBTREE_SHA1_PREFIXCMP(entry->key_oid.hash, l->key_oid.hash)) {
291
/* unpack 'l' and restart insert */
292
*p = NULL;
293
load_subtree(t, l, tree, n);
@@ -301,7 +301,7 @@ static int note_tree_insert(struct notes_tree *t, struct int_node *tree,
301
/* non-matching leaf_node */
302
assert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||
303
GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);
304
- if (is_null_sha1(entry->val_sha1)) { /* skip insertion of empty note */
304
+ if (is_null_oid(&entry->val_oid)) { /* skip insertion of empty note */
305
free(entry);
306
return 0;
307
}
@@ -373,7 +373,7 @@ static void add_non_note(struct notes_tree *t, char *path,
373
n->next = NULL;
374
n->path = path;
375
n->mode = mode;
376
- hashcpy(n->sha1, sha1);
376
+ hashcpy(n->oid.hash, sha1);
377
t->prev_non_note = n;
378
379
if (!t->first_non_note) {
@@ -399,7 +399,7 @@ static void add_non_note(struct notes_tree *t, char *path,
399
if (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */
400
assert(strcmp(p->path, n->path) == 0);
401
p->mode = n->mode;
402
- hashcpy(p->sha1, n->sha1);
402
+ oidcpy(&p->oid, &n->oid);
403
free(n);
404
t->prev_non_note = p;
405
return;
@@ -422,14 +422,14 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
422
unsigned char type;
423
struct leaf_node *l;
424
425
- buf = fill_tree_descriptor(&desc, subtree->val_sha1);
425
+ buf = fill_tree_descriptor(&desc, subtree->val_oid.hash);
426
if (!buf)
427
die("Could not read %s for notes-index",
428
- sha1_to_hex(subtree->val_sha1));
428
+ oid_to_hex(&subtree->val_oid));
429
430
- prefix_len = subtree->key_sha1[19];
430
+ prefix_len = subtree->key_oid.hash[19];
431
assert(prefix_len * 2 >= n);
432
- memcpy(object_sha1, subtree->key_sha1, prefix_len);
432
+ memcpy(object_sha1, subtree->key_oid.hash, prefix_len);
433
while (tree_entry(&desc, &entry)) {
434
path_len = strlen(entry.path);
435
len = get_sha1_hex_segment(entry.path, path_len,
@@ -447,12 +447,12 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
447
type = PTR_TYPE_NOTE;
448
l = (struct leaf_node *)
449
xcalloc(1, sizeof(struct leaf_node));
450
- hashcpy(l->key_sha1, object_sha1);
451
- hashcpy(l->val_sha1, entry.oid->hash);
450
+ hashcpy(l->key_oid.hash, object_sha1);
451
+ oidcpy(&l->val_oid, entry.oid);
452
if (len < 20) {
453
if (!S_ISDIR(entry.mode) || path_len != 2)
454
goto handle_non_note; /* not subtree */
455
- l->key_sha1[19] = (unsigned char) len;
455
+ l->key_oid.hash[19] = (unsigned char) len;
456
type = PTR_TYPE_SUBTREE;
457
}
458
if (note_tree_insert(t, node, n, l, type,
@@ -460,7 +460,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
460
die("Failed to load %s %s into notes tree "
461
"from %s",
462
type == PTR_TYPE_NOTE ? "note" : "subtree",
463
- sha1_to_hex(l->key_sha1), t->ref);
463
+ oid_to_hex(&l->key_oid), t->ref);
464
}
465
continue;
466
@@ -486,7 +486,7 @@ handle_non_note:
486
*/
487
{
488
struct strbuf non_note_path = STRBUF_INIT;
489
- const char *q = sha1_to_hex(subtree->key_sha1);
489
+ const char *q = oid_to_hex(&subtree->key_oid);
490
int i;
491
for (i = 0; i < prefix_len; i++) {
492
strbuf_addch(&non_note_path, *q++);
@@ -599,15 +599,17 @@ redo:
599
flags & FOR_EACH_NOTE_YIELD_SUBTREES) {
600
/* invoke callback with subtree */
601
unsigned int path_len =
602
- l->key_sha1[19] * 2 + fanout;
602
+ l->key_oid.hash[19] * 2 + fanout;
603
assert(path_len < FANOUT_PATH_MAX - 1);
604
- construct_path_with_fanout(l->key_sha1, fanout,
604
+ construct_path_with_fanout(l->key_oid.hash,
605
+ fanout,
606
path);
607
/* Create trailing slash, if needed */
608
if (path[path_len - 1] != '/')
609
path[path_len++] = '/';
610
path[path_len] = '\0';
610
- ret = fn(l->key_sha1, l->val_sha1, path,
611
+ ret = fn(l->key_oid.hash, l->val_oid.hash,
612
+ path,
613
cb_data);
614
}
615
if (n > fanout * 2 ||
@@ -621,8 +623,10 @@ redo:
623
break;
624
case PTR_TYPE_NOTE:
625
l = (struct leaf_node *) CLR_PTR_TYPE(p);
624
- construct_path_with_fanout(l->key_sha1, fanout, path);
625
- ret = fn(l->key_sha1, l->val_sha1, path, cb_data);
626
+ construct_path_with_fanout(l->key_oid.hash, fanout,
627
+ path);
628
+ ret = fn(l->key_oid.hash, l->val_oid.hash, path,
629
+ cb_data);
630
break;
631
}
632
if (ret)
@@ -742,7 +746,7 @@ static int write_each_non_note_until(const char *note_path,
746
; /* do nothing, prefer note to non-note */
747
else {
748
ret = write_each_note_helper(d->root, n->path, n->mode,
745
- n->sha1);
749
+ n->oid.hash);
750
if (ret)
751
return ret;
752
}
@@ -1027,8 +1031,8 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
1031
die("Failed to read notes tree referenced by %s (%s)",
1032
notes_ref, oid_to_hex(&object_oid));
1033
1030
- hashclr(root_tree.key_sha1);
1031
- hashcpy(root_tree.val_sha1, oid.hash);
1034
+ oidclr(&root_tree.key_oid);
1035
+ oidcpy(&root_tree.val_oid, &oid);
1036
load_subtree(t, &root_tree, t->root, 0);
1037
}
1038
@@ -1092,8 +1096,8 @@ int add_note(struct notes_tree *t, const unsigned char *object_sha1,
1096
if (!combine_notes)
1097
combine_notes = t->combine_notes;
1098
l = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));
1095
- hashcpy(l->key_sha1, object_sha1);
1096
- hashcpy(l->val_sha1, note_sha1);
1099
+ hashcpy(l->key_oid.hash, object_sha1);
1100
+ hashcpy(l->val_oid.hash, note_sha1);
1101
return note_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);
1102
}
1103
@@ -1104,10 +1108,10 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
1108
if (!t)
1109
t = &default_notes_tree;
1110
assert(t->initialized);
1107
- hashcpy(l.key_sha1, object_sha1);
1108
- hashclr(l.val_sha1);
1111
+ hashcpy(l.key_oid.hash, object_sha1);
1112
+ oidclr(&l.val_oid);
1113
note_tree_remove(t, t->root, 0, &l);
1110
- if (is_null_sha1(l.val_sha1)) /* no note was removed */
1114
+ if (is_null_oid(&l.val_oid)) /* no note was removed */
1115
return 1;
1116
t->dirty = 1;
1117
return 0;
@@ -1122,7 +1126,7 @@ const unsigned char *get_note(struct notes_tree *t,
1126
t = &default_notes_tree;
1127
assert(t->initialized);
1128
found = note_tree_find(t, t->root, 0, object_sha1);
1125
- return found ? found->val_sha1 : NULL;
1129
+ return found ? found->val_oid.hash : NULL;
1130
}
1131
1132
int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,