]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "diff.h" | |
3 | #include "commit.h" | |
4 | #include "sha1-lookup.h" | |
5 | #include "patch-ids.h" | |
6 | ||
7 | static int patch_id_defined(struct commit *commit) | |
8 | { | |
9 | /* must be 0 or 1 parents */ | |
10 | return !commit->parents || !commit->parents->next; | |
11 | } | |
12 | ||
13 | int commit_patch_id(struct commit *commit, struct diff_options *options, | |
14 | struct object_id *oid, int diff_header_only, int stable) | |
15 | { | |
16 | if (!patch_id_defined(commit)) | |
17 | return -1; | |
18 | ||
19 | if (commit->parents) | |
20 | diff_tree_oid(&commit->parents->item->object.oid, | |
21 | &commit->object.oid, "", options); | |
22 | else | |
23 | diff_root_tree_oid(&commit->object.oid, "", options); | |
24 | diffcore_std(options); | |
25 | return diff_flush_patch_id(options, oid, diff_header_only, stable); | |
26 | } | |
27 | ||
28 | /* | |
29 | * When we cannot load the full patch-id for both commits for whatever | |
30 | * reason, the function returns -1 (i.e. return error(...)). Despite | |
31 | * the "neq" in the name of this function, the caller only cares about | |
32 | * the return value being zero (a and b are equivalent) or non-zero (a | |
33 | * and b are different), and returning non-zero would keep both in the | |
34 | * result, even if they actually were equivalent, in order to err on | |
35 | * the side of safety. The actual value being negative does not have | |
36 | * any significance; only that it is non-zero matters. | |
37 | */ | |
38 | static int patch_id_neq(const void *cmpfn_data, | |
39 | const struct hashmap_entry *eptr, | |
40 | const struct hashmap_entry *entry_or_key, | |
41 | const void *unused_keydata) | |
42 | { | |
43 | /* NEEDSWORK: const correctness? */ | |
44 | struct diff_options *opt = (void *)cmpfn_data; | |
45 | struct patch_id *a, *b; | |
46 | ||
47 | a = container_of(eptr, struct patch_id, ent); | |
48 | b = container_of(entry_or_key, struct patch_id, ent); | |
49 | ||
50 | if (is_null_oid(&a->patch_id) && | |
51 | commit_patch_id(a->commit, opt, &a->patch_id, 0, 0)) | |
52 | return error("Could not get patch ID for %s", | |
53 | oid_to_hex(&a->commit->object.oid)); | |
54 | if (is_null_oid(&b->patch_id) && | |
55 | commit_patch_id(b->commit, opt, &b->patch_id, 0, 0)) | |
56 | return error("Could not get patch ID for %s", | |
57 | oid_to_hex(&b->commit->object.oid)); | |
58 | return !oideq(&a->patch_id, &b->patch_id); | |
59 | } | |
60 | ||
61 | int init_patch_ids(struct repository *r, struct patch_ids *ids) | |
62 | { | |
63 | memset(ids, 0, sizeof(*ids)); | |
64 | repo_diff_setup(r, &ids->diffopts); | |
65 | ids->diffopts.detect_rename = 0; | |
66 | ids->diffopts.flags.recursive = 1; | |
67 | diff_setup_done(&ids->diffopts); | |
68 | hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256); | |
69 | return 0; | |
70 | } | |
71 | ||
72 | int free_patch_ids(struct patch_ids *ids) | |
73 | { | |
74 | hashmap_free_entries(&ids->patches, struct patch_id, ent); | |
75 | return 0; | |
76 | } | |
77 | ||
78 | static int init_patch_id_entry(struct patch_id *patch, | |
79 | struct commit *commit, | |
80 | struct patch_ids *ids) | |
81 | { | |
82 | struct object_id header_only_patch_id; | |
83 | ||
84 | patch->commit = commit; | |
85 | if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0)) | |
86 | return -1; | |
87 | ||
88 | hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id)); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | struct patch_id *has_commit_patch_id(struct commit *commit, | |
93 | struct patch_ids *ids) | |
94 | { | |
95 | struct patch_id patch; | |
96 | ||
97 | if (!patch_id_defined(commit)) | |
98 | return NULL; | |
99 | ||
100 | memset(&patch, 0, sizeof(patch)); | |
101 | if (init_patch_id_entry(&patch, commit, ids)) | |
102 | return NULL; | |
103 | ||
104 | return hashmap_get_entry(&ids->patches, &patch, ent, NULL); | |
105 | } | |
106 | ||
107 | struct patch_id *add_commit_patch_id(struct commit *commit, | |
108 | struct patch_ids *ids) | |
109 | { | |
110 | struct patch_id *key; | |
111 | ||
112 | if (!patch_id_defined(commit)) | |
113 | return NULL; | |
114 | ||
115 | key = xcalloc(1, sizeof(*key)); | |
116 | if (init_patch_id_entry(key, commit, ids)) { | |
117 | free(key); | |
118 | return NULL; | |
119 | } | |
120 | ||
121 | hashmap_add(&ids->patches, &key->ent); | |
122 | return key; | |
123 | } |