|
5 | 5 | #include "patch-ids.h"
|
6 | 6 |
|
7 | 7 | int commit_patch_id(struct commit *commit, struct diff_options *options,
|
8 |
| - unsigned char *sha1) |
| 8 | + unsigned char *sha1, int diff_header_only) |
9 | 9 | {
|
10 | 10 | if (commit->parents)
|
11 | 11 | diff_tree_sha1(commit->parents->item->object.oid.hash,
|
12 | 12 | commit->object.oid.hash, "", options);
|
13 | 13 | else
|
14 | 14 | diff_root_tree_sha1(commit->object.oid.hash, "", options);
|
15 | 15 | diffcore_std(options);
|
16 |
| - return diff_flush_patch_id(options, sha1); |
| 16 | + return diff_flush_patch_id(options, sha1, diff_header_only); |
17 | 17 | }
|
18 | 18 |
|
19 |
| -static const unsigned char *patch_id_access(size_t index, void *table) |
| 19 | +/* |
| 20 | + * When we cannot load the full patch-id for both commits for whatever |
| 21 | + * reason, the function returns -1 (i.e. return error(...)). Despite |
| 22 | + * the "cmp" in the name of this function, the caller only cares about |
| 23 | + * the return value being zero (a and b are equivalent) or non-zero (a |
| 24 | + * and b are different), and returning non-zero would keep both in the |
| 25 | + * result, even if they actually were equivalent, in order to err on |
| 26 | + * the side of safety. The actual value being negative does not have |
| 27 | + * any significance; only that it is non-zero matters. |
| 28 | + */ |
| 29 | +static int patch_id_cmp(struct patch_id *a, |
| 30 | + struct patch_id *b, |
| 31 | + struct diff_options *opt) |
20 | 32 | {
|
21 |
| - struct patch_id **id_table = table; |
22 |
| - return id_table[index]->patch_id; |
| 33 | + if (is_null_sha1(a->patch_id) && |
| 34 | + commit_patch_id(a->commit, opt, a->patch_id, 0)) |
| 35 | + return error("Could not get patch ID for %s", |
| 36 | + oid_to_hex(&a->commit->object.oid)); |
| 37 | + if (is_null_sha1(b->patch_id) && |
| 38 | + commit_patch_id(b->commit, opt, b->patch_id, 0)) |
| 39 | + return error("Could not get patch ID for %s", |
| 40 | + oid_to_hex(&b->commit->object.oid)); |
| 41 | + return hashcmp(a->patch_id, b->patch_id); |
23 | 42 | }
|
24 | 43 |
|
25 |
| -static int patch_pos(struct patch_id **table, int nr, const unsigned char *id) |
26 |
| -{ |
27 |
| - return sha1_pos(id, table, nr, patch_id_access); |
28 |
| -} |
29 |
| - |
30 |
| -#define BUCKET_SIZE 190 /* 190 * 21 = 3990, with slop close enough to 4K */ |
31 |
| -struct patch_id_bucket { |
32 |
| - struct patch_id_bucket *next; |
33 |
| - int nr; |
34 |
| - struct patch_id bucket[BUCKET_SIZE]; |
35 |
| -}; |
36 |
| - |
37 | 44 | int init_patch_ids(struct patch_ids *ids)
|
38 | 45 | {
|
39 | 46 | memset(ids, 0, sizeof(*ids));
|
40 | 47 | diff_setup(&ids->diffopts);
|
41 | 48 | DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
|
42 | 49 | diff_setup_done(&ids->diffopts);
|
| 50 | + hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256); |
43 | 51 | return 0;
|
44 | 52 | }
|
45 | 53 |
|
46 | 54 | int free_patch_ids(struct patch_ids *ids)
|
47 | 55 | {
|
48 |
| - struct patch_id_bucket *next, *patches; |
49 |
| - |
50 |
| - free(ids->table); |
51 |
| - for (patches = ids->patches; patches; patches = next) { |
52 |
| - next = patches->next; |
53 |
| - free(patches); |
54 |
| - } |
| 56 | + hashmap_free(&ids->patches, 1); |
55 | 57 | return 0;
|
56 | 58 | }
|
57 | 59 |
|
58 |
| -static struct patch_id *add_commit(struct commit *commit, |
59 |
| - struct patch_ids *ids, |
60 |
| - int no_add) |
| 60 | +static int init_patch_id_entry(struct patch_id *patch, |
| 61 | + struct commit *commit, |
| 62 | + struct patch_ids *ids) |
61 | 63 | {
|
62 |
| - struct patch_id_bucket *bucket; |
63 |
| - struct patch_id *ent; |
64 |
| - unsigned char sha1[20]; |
65 |
| - int pos; |
| 64 | + unsigned char header_only_patch_id[GIT_SHA1_RAWSZ]; |
66 | 65 |
|
67 |
| - if (commit_patch_id(commit, &ids->diffopts, sha1)) |
68 |
| - return NULL; |
69 |
| - pos = patch_pos(ids->table, ids->nr, sha1); |
70 |
| - if (0 <= pos) |
71 |
| - return ids->table[pos]; |
72 |
| - if (no_add) |
73 |
| - return NULL; |
| 66 | + patch->commit = commit; |
| 67 | + if (commit_patch_id(commit, &ids->diffopts, header_only_patch_id, 1)) |
| 68 | + return -1; |
74 | 69 |
|
75 |
| - pos = -1 - pos; |
76 |
| - |
77 |
| - bucket = ids->patches; |
78 |
| - if (!bucket || (BUCKET_SIZE <= bucket->nr)) { |
79 |
| - bucket = xcalloc(1, sizeof(*bucket)); |
80 |
| - bucket->next = ids->patches; |
81 |
| - ids->patches = bucket; |
82 |
| - } |
83 |
| - ent = &bucket->bucket[bucket->nr++]; |
84 |
| - hashcpy(ent->patch_id, sha1); |
85 |
| - |
86 |
| - ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc); |
87 |
| - if (pos < ids->nr) |
88 |
| - memmove(ids->table + pos + 1, ids->table + pos, |
89 |
| - sizeof(ent) * (ids->nr - pos)); |
90 |
| - ids->nr++; |
91 |
| - ids->table[pos] = ent; |
92 |
| - return ids->table[pos]; |
| 70 | + hashmap_entry_init(patch, sha1hash(header_only_patch_id)); |
| 71 | + return 0; |
93 | 72 | }
|
94 | 73 |
|
95 | 74 | struct patch_id *has_commit_patch_id(struct commit *commit,
|
96 | 75 | struct patch_ids *ids)
|
97 | 76 | {
|
98 |
| - return add_commit(commit, ids, 1); |
| 77 | + struct patch_id patch; |
| 78 | + |
| 79 | + memset(&patch, 0, sizeof(patch)); |
| 80 | + if (init_patch_id_entry(&patch, commit, ids)) |
| 81 | + return NULL; |
| 82 | + |
| 83 | + return hashmap_get(&ids->patches, &patch, &ids->diffopts); |
99 | 84 | }
|
100 | 85 |
|
101 | 86 | struct patch_id *add_commit_patch_id(struct commit *commit,
|
102 | 87 | struct patch_ids *ids)
|
103 | 88 | {
|
104 |
| - return add_commit(commit, ids, 0); |
| 89 | + struct patch_id *key = xcalloc(1, sizeof(*key)); |
| 90 | + |
| 91 | + if (init_patch_id_entry(key, commit, ids)) { |
| 92 | + free(key); |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + |
| 96 | + hashmap_add(&ids->patches, key); |
| 97 | + return key; |
105 | 98 | }
|
0 commit comments