blob: a4473a88fa2f55b4f8e399fdcf4747f0e666a015 [file] [log] [blame]
Junio C Hamano5d23e132007-04-09 17:01:27 -07001#include "cache.h"
2#include "diff.h"
3#include "commit.h"
Martin Ågrenbc626922020-12-31 12:56:23 +01004#include "hash-lookup.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Junio C Hamano5d23e132007-04-09 17:01:27 -07006#include "patch-ids.h"
7
Jeff King7c810402016-09-12 13:56:41 -04008static int patch_id_defined(struct commit *commit)
9{
10 /* must be 0 or 1 parents */
11 return !commit->parents || !commit->parents->next;
12}
13
Xiaolong Yeded2c092016-04-26 15:51:21 +080014int commit_patch_id(struct commit *commit, struct diff_options *options,
Jerry Zhang51276c12022-10-24 20:07:40 +000015 struct object_id *oid, int diff_header_only)
Junio C Hamano5d23e132007-04-09 17:01:27 -070016{
Jeff King7c810402016-09-12 13:56:41 -040017 if (!patch_id_defined(commit))
18 return -1;
19
Junio C Hamano5d23e132007-04-09 17:01:27 -070020 if (commit->parents)
Brandon Williams66f414f2017-05-30 10:31:03 -070021 diff_tree_oid(&commit->parents->item->object.oid,
22 &commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070023 else
Brandon Williams7b8dea02017-05-30 10:30:57 -070024 diff_root_tree_oid(&commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070025 diffcore_std(options);
Jerry Zhang51276c12022-10-24 20:07:40 +000026 return diff_flush_patch_id(options, oid, diff_header_only);
Junio C Hamano5d23e132007-04-09 17:01:27 -070027}
28
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040029/*
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
Jeff Kingcc00e5c2018-08-28 17:22:55 -040032 * the "neq" in the name of this function, the caller only cares about
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040033 * 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 */
Jeff Kingcc00e5c2018-08-28 17:22:55 -040039static int patch_id_neq(const void *cmpfn_data,
Eric Wong939af162019-10-06 23:30:37 +000040 const struct hashmap_entry *eptr,
41 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020042 const void *keydata UNUSED)
Junio C Hamano5d23e132007-04-09 17:01:27 -070043{
Stefan Beller8d0017d2017-06-30 17:28:34 -070044 /* NEEDSWORK: const correctness? */
45 struct diff_options *opt = (void *)cmpfn_data;
Eric Wong939af162019-10-06 23:30:37 +000046 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);
Stefan Beller8d0017d2017-06-30 17:28:34 -070050
Brandon Williams34f3c0e2017-05-30 10:30:53 -070051 if (is_null_oid(&a->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:40 +000052 commit_patch_id(a->commit, opt, &a->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040053 return error("Could not get patch ID for %s",
54 oid_to_hex(&a->commit->object.oid));
Brandon Williams34f3c0e2017-05-30 10:30:53 -070055 if (is_null_oid(&b->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:40 +000056 commit_patch_id(b->commit, opt, &b->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040057 return error("Could not get patch ID for %s",
58 oid_to_hex(&b->commit->object.oid));
Jeff Kingcc00e5c2018-08-28 17:22:55 -040059 return !oideq(&a->patch_id, &b->patch_id);
Junio C Hamano5d23e132007-04-09 17:01:27 -070060}
61
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020062int init_patch_ids(struct repository *r, struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070063{
64 memset(ids, 0, sizeof(*ids));
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020065 repo_diff_setup(r, &ids->diffopts);
Jeff King5a29cbc2016-09-09 16:34:34 -040066 ids->diffopts.detect_rename = 0;
Brandon Williams0d1e0e72017-10-31 11:19:11 -070067 ids->diffopts.flags.recursive = 1;
Thomas Rast28452652012-08-03 14:16:24 +020068 diff_setup_done(&ids->diffopts);
Jeff Kingcc00e5c2018-08-28 17:22:55 -040069 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
Junio C Hamano5d23e132007-04-09 17:01:27 -070070 return 0;
71}
72
73int free_patch_ids(struct patch_ids *ids)
74{
Elijah Newren6da1a252020-11-02 18:55:05 +000075 hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
Junio C Hamano5d23e132007-04-09 17:01:27 -070076 return 0;
77}
78
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040079static int init_patch_id_entry(struct patch_id *patch,
80 struct commit *commit,
81 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070082{
Brandon Williams34f3c0e2017-05-30 10:30:53 -070083 struct object_id header_only_patch_id;
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040084
Kevin Willford683f17e2016-07-29 12:19:18 -040085 patch->commit = commit;
Jerry Zhang51276c12022-10-24 20:07:40 +000086 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040087 return -1;
Junio C Hamano5d23e132007-04-09 17:01:27 -070088
Eric Wongd22245a2019-10-06 23:30:27 +000089 hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040090 return 0;
Junio C Hamano5d23e132007-04-09 17:01:27 -070091}
92
Jeff Kingc9e3a4e2021-01-12 10:52:32 -050093struct patch_id *patch_id_iter_first(struct commit *commit,
Junio C Hamano5d23e132007-04-09 17:01:27 -070094 struct patch_ids *ids)
95{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040096 struct patch_id patch;
97
Jeff King7c810402016-09-12 13:56:41 -040098 if (!patch_id_defined(commit))
99 return NULL;
100
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400101 memset(&patch, 0, sizeof(patch));
102 if (init_patch_id_entry(&patch, commit, ids))
103 return NULL;
104
Eric Wong404ab782019-10-06 23:30:42 +0000105 return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
Junio C Hamano5d23e132007-04-09 17:01:27 -0700106}
107
Jeff Kingc9e3a4e2021-01-12 10:52:32 -0500108struct 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
Junio C Hamano5d23e132007-04-09 17:01:27 -0700120struct patch_id *add_commit_patch_id(struct commit *commit,
121 struct patch_ids *ids)
122{
Johannes Schindelin57486932017-05-04 15:55:38 +0200123 struct patch_id *key;
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400124
Jeff King7c810402016-09-12 13:56:41 -0400125 if (!patch_id_defined(commit))
126 return NULL;
127
René Scharfeca56dad2021-03-13 17:17:22 +0100128 CALLOC_ARRAY(key, 1);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400129 if (init_patch_id_entry(key, commit, ids)) {
130 free(key);
131 return NULL;
132 }
133
Eric Wongb94e5c12019-10-06 23:30:29 +0000134 hashmap_add(&ids->patches, &key->ent);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400135 return key;
Junio C Hamano5d23e132007-04-09 17:01:27 -0700136}