]>
Commit | Line | Data |
---|---|---|
5d23e133 JH |
1 | #include "cache.h" |
2 | #include "diff.h" | |
3 | #include "commit.h" | |
5289bae1 | 4 | #include "sha1-lookup.h" |
5d23e133 JH |
5 | #include "patch-ids.h" |
6 | ||
ded2c097 | 7 | int commit_patch_id(struct commit *commit, struct diff_options *options, |
b3dfeebb | 8 | unsigned char *sha1, int diff_header_only) |
5d23e133 JH |
9 | { |
10 | if (commit->parents) | |
ed1c9977 | 11 | diff_tree_sha1(commit->parents->item->object.oid.hash, |
12 | commit->object.oid.hash, "", options); | |
5d23e133 | 13 | else |
ed1c9977 | 14 | diff_root_tree_sha1(commit->object.oid.hash, "", options); |
5d23e133 | 15 | diffcore_std(options); |
b3dfeebb | 16 | return diff_flush_patch_id(options, sha1, diff_header_only); |
5d23e133 JH |
17 | } |
18 | ||
b3dfeebb KW |
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 | */ | |
dfb7a1b4 KW |
29 | static int patch_id_cmp(struct patch_id *a, |
30 | struct patch_id *b, | |
b3dfeebb | 31 | struct diff_options *opt) |
5d23e133 | 32 | { |
b3dfeebb KW |
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)); | |
dfb7a1b4 | 41 | return hashcmp(a->patch_id, b->patch_id); |
5d23e133 JH |
42 | } |
43 | ||
5d23e133 JH |
44 | int init_patch_ids(struct patch_ids *ids) |
45 | { | |
46 | memset(ids, 0, sizeof(*ids)); | |
47 | diff_setup(&ids->diffopts); | |
5a29cbc6 | 48 | ids->diffopts.detect_rename = 0; |
8f67f8ae | 49 | DIFF_OPT_SET(&ids->diffopts, RECURSIVE); |
28452655 | 50 | diff_setup_done(&ids->diffopts); |
dfb7a1b4 | 51 | hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256); |
5d23e133 JH |
52 | return 0; |
53 | } | |
54 | ||
55 | int free_patch_ids(struct patch_ids *ids) | |
56 | { | |
dfb7a1b4 | 57 | hashmap_free(&ids->patches, 1); |
5d23e133 JH |
58 | return 0; |
59 | } | |
60 | ||
dfb7a1b4 KW |
61 | static int init_patch_id_entry(struct patch_id *patch, |
62 | struct commit *commit, | |
63 | struct patch_ids *ids) | |
5d23e133 | 64 | { |
b3dfeebb KW |
65 | unsigned char header_only_patch_id[GIT_SHA1_RAWSZ]; |
66 | ||
683f17ec | 67 | patch->commit = commit; |
b3dfeebb | 68 | if (commit_patch_id(commit, &ids->diffopts, header_only_patch_id, 1)) |
dfb7a1b4 | 69 | return -1; |
5d23e133 | 70 | |
b3dfeebb | 71 | hashmap_entry_init(patch, sha1hash(header_only_patch_id)); |
dfb7a1b4 | 72 | return 0; |
5d23e133 JH |
73 | } |
74 | ||
75 | struct patch_id *has_commit_patch_id(struct commit *commit, | |
76 | struct patch_ids *ids) | |
77 | { | |
dfb7a1b4 KW |
78 | struct patch_id patch; |
79 | ||
80 | memset(&patch, 0, sizeof(patch)); | |
81 | if (init_patch_id_entry(&patch, commit, ids)) | |
82 | return NULL; | |
83 | ||
b3dfeebb | 84 | return hashmap_get(&ids->patches, &patch, &ids->diffopts); |
5d23e133 JH |
85 | } |
86 | ||
87 | struct patch_id *add_commit_patch_id(struct commit *commit, | |
88 | struct patch_ids *ids) | |
89 | { | |
dfb7a1b4 KW |
90 | struct patch_id *key = xcalloc(1, sizeof(*key)); |
91 | ||
92 | if (init_patch_id_entry(key, commit, ids)) { | |
93 | free(key); | |
94 | return NULL; | |
95 | } | |
96 | ||
97 | hashmap_add(&ids->patches, key); | |
98 | return key; | |
5d23e133 | 99 | } |