blob: a5683b462c6e7692ceff3218213b9747a9b6cc02 [file] [log] [blame]
Elijah Newren4f6728d2023-03-21 06:25:561#include "git-compat-util.h"
Junio C Hamano5d23e132007-04-10 00:01:272#include "diff.h"
3#include "commit.h"
Elijah Newrendf6e8742023-05-16 06:34:004#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:275#include "hex.h"
Junio C Hamano5d23e132007-04-10 00:01:276#include "patch-ids.h"
7
Jeff King7c810402016-09-12 17:56:418static 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 07:51:2114int commit_patch_id(struct commit *commit, struct diff_options *options,
Jerry Zhang51276c12022-10-24 20:07:4015 struct object_id *oid, int diff_header_only)
Junio C Hamano5d23e132007-04-10 00:01:2716{
Jeff King7c810402016-09-12 17:56:4117 if (!patch_id_defined(commit))
18 return -1;
19
Junio C Hamano5d23e132007-04-10 00:01:2720 if (commit->parents)
Brandon Williams66f414f2017-05-30 17:31:0321 diff_tree_oid(&commit->parents->item->object.oid,
22 &commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-10 00:01:2723 else
Brandon Williams7b8dea02017-05-30 17:30:5724 diff_root_tree_oid(&commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-10 00:01:2725 diffcore_std(options);
Jerry Zhang51276c12022-10-24 20:07:4026 return diff_flush_patch_id(options, oid, diff_header_only);
Junio C Hamano5d23e132007-04-10 00:01:2727}
28
Kevin Willfordb3dfeeb2016-07-29 16:19:2029/*
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 21:22:5532 * the "neq" in the name of this function, the caller only cares about
Kevin Willfordb3dfeeb2016-07-29 16:19:2033 * 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 21:22:5539static int patch_id_neq(const void *cmpfn_data,
Eric Wong939af162019-10-06 23:30:3740 const struct hashmap_entry *eptr,
41 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:4842 const void *keydata UNUSED)
Junio C Hamano5d23e132007-04-10 00:01:2743{
Stefan Beller8d0017d2017-07-01 00:28:3444 /* NEEDSWORK: const correctness? */
45 struct diff_options *opt = (void *)cmpfn_data;
Eric Wong939af162019-10-06 23:30:3746 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-07-01 00:28:3450
Brandon Williams34f3c0e2017-05-30 17:30:5351 if (is_null_oid(&a->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:4052 commit_patch_id(a->commit, opt, &a->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 16:19:2053 return error("Could not get patch ID for %s",
54 oid_to_hex(&a->commit->object.oid));
Brandon Williams34f3c0e2017-05-30 17:30:5355 if (is_null_oid(&b->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:4056 commit_patch_id(b->commit, opt, &b->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 16:19:2057 return error("Could not get patch ID for %s",
58 oid_to_hex(&b->commit->object.oid));
Jeff Kingcc00e5c2018-08-28 21:22:5559 return !oideq(&a->patch_id, &b->patch_id);
Junio C Hamano5d23e132007-04-10 00:01:2760}
61
Nguyễn Thái Ngọc Duya7edadd2018-09-21 15:57:3062int init_patch_ids(struct repository *r, struct patch_ids *ids)
Junio C Hamano5d23e132007-04-10 00:01:2763{
64 memset(ids, 0, sizeof(*ids));
Nguyễn Thái Ngọc Duya7edadd2018-09-21 15:57:3065 repo_diff_setup(r, &ids->diffopts);
Jeff King5a29cbc2016-09-09 20:34:3466 ids->diffopts.detect_rename = 0;
Brandon Williams0d1e0e72017-10-31 18:19:1167 ids->diffopts.flags.recursive = 1;
Thomas Rast28452652012-08-03 12:16:2468 diff_setup_done(&ids->diffopts);
Jeff Kingcc00e5c2018-08-28 21:22:5569 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
Junio C Hamano5d23e132007-04-10 00:01:2770 return 0;
71}
72
73int free_patch_ids(struct patch_ids *ids)
74{
Elijah Newren6da1a252020-11-02 18:55:0575 hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
Junio C Hamano5d23e132007-04-10 00:01:2776 return 0;
77}
78
Kevin Willforddfb7a1b2016-07-29 16:19:1779static int init_patch_id_entry(struct patch_id *patch,
80 struct commit *commit,
81 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-10 00:01:2782{
Brandon Williams34f3c0e2017-05-30 17:30:5383 struct object_id header_only_patch_id;
Kevin Willfordb3dfeeb2016-07-29 16:19:2084
Kevin Willford683f17e2016-07-29 16:19:1885 patch->commit = commit;
Jerry Zhang51276c12022-10-24 20:07:4086 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
Kevin Willforddfb7a1b2016-07-29 16:19:1787 return -1;
Junio C Hamano5d23e132007-04-10 00:01:2788
Eric Wongd22245a2019-10-06 23:30:2789 hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
Kevin Willforddfb7a1b2016-07-29 16:19:1790 return 0;
Junio C Hamano5d23e132007-04-10 00:01:2791}
92
Jeff Kingc9e3a4e2021-01-12 15:52:3293struct patch_id *patch_id_iter_first(struct commit *commit,
Junio C Hamano5d23e132007-04-10 00:01:2794 struct patch_ids *ids)
95{
Kevin Willforddfb7a1b2016-07-29 16:19:1796 struct patch_id patch;
97
Jeff King7c810402016-09-12 17:56:4198 if (!patch_id_defined(commit))
99 return NULL;
100
Kevin Willforddfb7a1b2016-07-29 16:19:17101 memset(&patch, 0, sizeof(patch));
102 if (init_patch_id_entry(&patch, commit, ids))
103 return NULL;
104
Eric Wong404ab782019-10-06 23:30:42105 return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
Junio C Hamano5d23e132007-04-10 00:01:27106}
107
Jeff Kingc9e3a4e2021-01-12 15:52:32108struct 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-10 00:01:27120struct patch_id *add_commit_patch_id(struct commit *commit,
121 struct patch_ids *ids)
122{
Johannes Schindelin57486932017-05-04 13:55:38123 struct patch_id *key;
Kevin Willforddfb7a1b2016-07-29 16:19:17124
Jeff King7c810402016-09-12 17:56:41125 if (!patch_id_defined(commit))
126 return NULL;
127
René Scharfeca56dad2021-03-13 16:17:22128 CALLOC_ARRAY(key, 1);
Kevin Willforddfb7a1b2016-07-29 16:19:17129 if (init_patch_id_entry(key, commit, ids)) {
130 free(key);
131 return NULL;
132 }
133
Eric Wongb94e5c12019-10-06 23:30:29134 hashmap_add(&ids->patches, &key->ent);
Kevin Willforddfb7a1b2016-07-29 16:19:17135 return key;
Junio C Hamano5d23e132007-04-10 00:01:27136}