]> git.ipfire.org Git - thirdparty/git.git/blame - patch-ids.c
The twentieth batch
[thirdparty/git.git] / patch-ids.c
CommitLineData
4f6728d5 1#include "git-compat-util.h"
5d23e133
JH
2#include "diff.h"
3#include "commit.h"
df6e8744 4#include "hash.h"
41771fa4 5#include "hex.h"
5d23e133
JH
6#include "patch-ids.h"
7
7c810407
JK
8static int patch_id_defined(struct commit *commit)
9{
10 /* must be 0 or 1 parents */
11 return !commit->parents || !commit->parents->next;
12}
13
ded2c097 14int commit_patch_id(struct commit *commit, struct diff_options *options,
51276c18 15 struct object_id *oid, int diff_header_only)
5d23e133 16{
7c810407
JK
17 if (!patch_id_defined(commit))
18 return -1;
19
5d23e133 20 if (commit->parents)
66f414f8
BW
21 diff_tree_oid(&commit->parents->item->object.oid,
22 &commit->object.oid, "", options);
5d23e133 23 else
7b8dea0c 24 diff_root_tree_oid(&commit->object.oid, "", options);
5d23e133 25 diffcore_std(options);
51276c18 26 return diff_flush_patch_id(options, oid, diff_header_only);
5d23e133
JH
27}
28
b3dfeebb
KW
29/*
30 * When we cannot load the full patch-id for both commits for whatever
31 * reason, the function returns -1 (i.e. return error(...)). Despite
cc00e5ce 32 * the "neq" in the name of this function, the caller only cares about
b3dfeebb
KW
33 * the return value being zero (a and b are equivalent) or non-zero (a
34 * and b are different), and returning non-zero would keep both in the
35 * result, even if they actually were equivalent, in order to err on
36 * the side of safety. The actual value being negative does not have
37 * any significance; only that it is non-zero matters.
38 */
cc00e5ce 39static int patch_id_neq(const void *cmpfn_data,
939af16e
EW
40 const struct hashmap_entry *eptr,
41 const struct hashmap_entry *entry_or_key,
5cf88fd8 42 const void *keydata UNUSED)
5d23e133 43{
8d0017da
SB
44 /* NEEDSWORK: const correctness? */
45 struct diff_options *opt = (void *)cmpfn_data;
939af16e
EW
46 struct patch_id *a, *b;
47
48 a = container_of(eptr, struct patch_id, ent);
49 b = container_of(entry_or_key, struct patch_id, ent);
8d0017da 50
34f3c0eb 51 if (is_null_oid(&a->patch_id) &&
51276c18 52 commit_patch_id(a->commit, opt, &a->patch_id, 0))
b3dfeebb
KW
53 return error("Could not get patch ID for %s",
54 oid_to_hex(&a->commit->object.oid));
34f3c0eb 55 if (is_null_oid(&b->patch_id) &&
51276c18 56 commit_patch_id(b->commit, opt, &b->patch_id, 0))
b3dfeebb
KW
57 return error("Could not get patch ID for %s",
58 oid_to_hex(&b->commit->object.oid));
cc00e5ce 59 return !oideq(&a->patch_id, &b->patch_id);
5d23e133
JH
60}
61
a7edadda 62int init_patch_ids(struct repository *r, struct patch_ids *ids)
5d23e133
JH
63{
64 memset(ids, 0, sizeof(*ids));
a7edadda 65 repo_diff_setup(r, &ids->diffopts);
5a29cbc6 66 ids->diffopts.detect_rename = 0;
0d1e0e78 67 ids->diffopts.flags.recursive = 1;
28452655 68 diff_setup_done(&ids->diffopts);
cc00e5ce 69 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
5d23e133
JH
70 return 0;
71}
72
73int free_patch_ids(struct patch_ids *ids)
74{
6da1a258 75 hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
5d23e133
JH
76 return 0;
77}
78
dfb7a1b4
KW
79static int init_patch_id_entry(struct patch_id *patch,
80 struct commit *commit,
81 struct patch_ids *ids)
5d23e133 82{
34f3c0eb 83 struct object_id header_only_patch_id;
b3dfeebb 84
683f17ec 85 patch->commit = commit;
51276c18 86 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
dfb7a1b4 87 return -1;
5d23e133 88
d22245a2 89 hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
dfb7a1b4 90 return 0;
5d23e133
JH
91}
92
c9e3a4e7 93struct patch_id *patch_id_iter_first(struct commit *commit,
5d23e133
JH
94 struct patch_ids *ids)
95{
dfb7a1b4
KW
96 struct patch_id patch;
97
7c810407
JK
98 if (!patch_id_defined(commit))
99 return NULL;
100
dfb7a1b4
KW
101 memset(&patch, 0, sizeof(patch));
102 if (init_patch_id_entry(&patch, commit, ids))
103 return NULL;
104
404ab78e 105 return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
5d23e133
JH
106}
107
c9e3a4e7
JK
108struct patch_id *patch_id_iter_next(struct patch_id *cur,
109 struct patch_ids *ids)
110{
111 return hashmap_get_next_entry(&ids->patches, cur, ent);
112}
113
114int has_commit_patch_id(struct commit *commit,
115 struct patch_ids *ids)
116{
117 return !!patch_id_iter_first(commit, ids);
118}
119
5d23e133
JH
120struct patch_id *add_commit_patch_id(struct commit *commit,
121 struct patch_ids *ids)
122{
5748693b 123 struct patch_id *key;
dfb7a1b4 124
7c810407
JK
125 if (!patch_id_defined(commit))
126 return NULL;
127
ca56dadb 128 CALLOC_ARRAY(key, 1);
dfb7a1b4
KW
129 if (init_patch_id_entry(key, commit, ids)) {
130 free(key);
131 return NULL;
132 }
133
b94e5c1d 134 hashmap_add(&ids->patches, &key->ent);
dfb7a1b4 135 return key;
5d23e133 136}