]> git.ipfire.org Git - thirdparty/git.git/blame - patch-ids.c
t4039: abstract away SHA-1-specific constants
[thirdparty/git.git] / patch-ids.c
CommitLineData
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
7c810407
JK
7static int patch_id_defined(struct commit *commit)
8{
9 /* must be 0 or 1 parents */
10 return !commit->parents || !commit->parents->next;
11}
12
ded2c097 13int commit_patch_id(struct commit *commit, struct diff_options *options,
a8f6855f 14 struct object_id *oid, int diff_header_only, int stable)
5d23e133 15{
7c810407
JK
16 if (!patch_id_defined(commit))
17 return -1;
18
5d23e133 19 if (commit->parents)
66f414f8
BW
20 diff_tree_oid(&commit->parents->item->object.oid,
21 &commit->object.oid, "", options);
5d23e133 22 else
7b8dea0c 23 diff_root_tree_oid(&commit->object.oid, "", options);
5d23e133 24 diffcore_std(options);
a8f6855f 25 return diff_flush_patch_id(options, oid, diff_header_only, stable);
5d23e133
JH
26}
27
b3dfeebb
KW
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
cc00e5ce 31 * the "neq" in the name of this function, the caller only cares about
b3dfeebb
KW
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 */
cc00e5ce 38static int patch_id_neq(const void *cmpfn_data,
8d0017da
SB
39 const void *entry,
40 const void *entry_or_key,
3da492f8 41 const void *unused_keydata)
5d23e133 42{
8d0017da
SB
43 /* NEEDSWORK: const correctness? */
44 struct diff_options *opt = (void *)cmpfn_data;
45 struct patch_id *a = (void *)entry;
46 struct patch_id *b = (void *)entry_or_key;
47
34f3c0eb 48 if (is_null_oid(&a->patch_id) &&
a8f6855f 49 commit_patch_id(a->commit, opt, &a->patch_id, 0, 0))
b3dfeebb
KW
50 return error("Could not get patch ID for %s",
51 oid_to_hex(&a->commit->object.oid));
34f3c0eb 52 if (is_null_oid(&b->patch_id) &&
a8f6855f 53 commit_patch_id(b->commit, opt, &b->patch_id, 0, 0))
b3dfeebb
KW
54 return error("Could not get patch ID for %s",
55 oid_to_hex(&b->commit->object.oid));
cc00e5ce 56 return !oideq(&a->patch_id, &b->patch_id);
5d23e133
JH
57}
58
a7edadda 59int init_patch_ids(struct repository *r, struct patch_ids *ids)
5d23e133
JH
60{
61 memset(ids, 0, sizeof(*ids));
a7edadda 62 repo_diff_setup(r, &ids->diffopts);
5a29cbc6 63 ids->diffopts.detect_rename = 0;
0d1e0e78 64 ids->diffopts.flags.recursive = 1;
28452655 65 diff_setup_done(&ids->diffopts);
cc00e5ce 66 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
5d23e133
JH
67 return 0;
68}
69
70int free_patch_ids(struct patch_ids *ids)
71{
dfb7a1b4 72 hashmap_free(&ids->patches, 1);
5d23e133
JH
73 return 0;
74}
75
dfb7a1b4
KW
76static int init_patch_id_entry(struct patch_id *patch,
77 struct commit *commit,
78 struct patch_ids *ids)
5d23e133 79{
34f3c0eb 80 struct object_id header_only_patch_id;
b3dfeebb 81
683f17ec 82 patch->commit = commit;
a8f6855f 83 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
dfb7a1b4 84 return -1;
5d23e133 85
d40abc8e 86 hashmap_entry_init(patch, oidhash(&header_only_patch_id));
dfb7a1b4 87 return 0;
5d23e133
JH
88}
89
90struct patch_id *has_commit_patch_id(struct commit *commit,
91 struct patch_ids *ids)
92{
dfb7a1b4
KW
93 struct patch_id patch;
94
7c810407
JK
95 if (!patch_id_defined(commit))
96 return NULL;
97
dfb7a1b4
KW
98 memset(&patch, 0, sizeof(patch));
99 if (init_patch_id_entry(&patch, commit, ids))
100 return NULL;
101
3da492f8 102 return hashmap_get(&ids->patches, &patch, NULL);
5d23e133
JH
103}
104
105struct patch_id *add_commit_patch_id(struct commit *commit,
106 struct patch_ids *ids)
107{
5748693b 108 struct patch_id *key;
dfb7a1b4 109
7c810407
JK
110 if (!patch_id_defined(commit))
111 return NULL;
112
5748693b 113 key = xcalloc(1, sizeof(*key));
dfb7a1b4
KW
114 if (init_patch_id_entry(key, commit, ids)) {
115 free(key);
116 return NULL;
117 }
118
119 hashmap_add(&ids->patches, key);
120 return key;
5d23e133 121}