blob: 8bf425555de252f12935b4fae68f375f3a815987 [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"
Junio C Hamano5d23e132007-04-09 17:01:27 -07005#include "patch-ids.h"
6
Jeff King7c810402016-09-12 13:56:41 -04007static int patch_id_defined(struct commit *commit)
8{
9 /* must be 0 or 1 parents */
10 return !commit->parents || !commit->parents->next;
11}
12
Xiaolong Yeded2c092016-04-26 15:51:21 +080013int commit_patch_id(struct commit *commit, struct diff_options *options,
Stephen Boyda8f68552019-04-26 16:51:57 -070014 struct object_id *oid, int diff_header_only, int stable)
Junio C Hamano5d23e132007-04-09 17:01:27 -070015{
Jeff King7c810402016-09-12 13:56:41 -040016 if (!patch_id_defined(commit))
17 return -1;
18
Junio C Hamano5d23e132007-04-09 17:01:27 -070019 if (commit->parents)
Brandon Williams66f414f2017-05-30 10:31:03 -070020 diff_tree_oid(&commit->parents->item->object.oid,
21 &commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070022 else
Brandon Williams7b8dea02017-05-30 10:30:57 -070023 diff_root_tree_oid(&commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070024 diffcore_std(options);
Stephen Boyda8f68552019-04-26 16:51:57 -070025 return diff_flush_patch_id(options, oid, diff_header_only, stable);
Junio C Hamano5d23e132007-04-09 17:01:27 -070026}
27
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040028/*
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
Jeff Kingcc00e5c2018-08-28 17:22:55 -040031 * the "neq" in the name of this function, the caller only cares about
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040032 * 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 */
Jeff Kingcc00e5c2018-08-28 17:22:55 -040038static int patch_id_neq(const void *cmpfn_data,
Eric Wong939af162019-10-06 23:30:37 +000039 const struct hashmap_entry *eptr,
40 const struct hashmap_entry *entry_or_key,
Stefan Beller3da492f2017-06-30 12:14:06 -070041 const void *unused_keydata)
Junio C Hamano5d23e132007-04-09 17:01:27 -070042{
Stefan Beller8d0017d2017-06-30 17:28:34 -070043 /* NEEDSWORK: const correctness? */
44 struct diff_options *opt = (void *)cmpfn_data;
Eric Wong939af162019-10-06 23:30:37 +000045 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);
Stefan Beller8d0017d2017-06-30 17:28:34 -070049
Brandon Williams34f3c0e2017-05-30 10:30:53 -070050 if (is_null_oid(&a->patch_id) &&
Stephen Boyda8f68552019-04-26 16:51:57 -070051 commit_patch_id(a->commit, opt, &a->patch_id, 0, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040052 return error("Could not get patch ID for %s",
53 oid_to_hex(&a->commit->object.oid));
Brandon Williams34f3c0e2017-05-30 10:30:53 -070054 if (is_null_oid(&b->patch_id) &&
Stephen Boyda8f68552019-04-26 16:51:57 -070055 commit_patch_id(b->commit, opt, &b->patch_id, 0, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040056 return error("Could not get patch ID for %s",
57 oid_to_hex(&b->commit->object.oid));
Jeff Kingcc00e5c2018-08-28 17:22:55 -040058 return !oideq(&a->patch_id, &b->patch_id);
Junio C Hamano5d23e132007-04-09 17:01:27 -070059}
60
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020061int init_patch_ids(struct repository *r, struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070062{
63 memset(ids, 0, sizeof(*ids));
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020064 repo_diff_setup(r, &ids->diffopts);
Jeff King5a29cbc2016-09-09 16:34:34 -040065 ids->diffopts.detect_rename = 0;
Brandon Williams0d1e0e72017-10-31 11:19:11 -070066 ids->diffopts.flags.recursive = 1;
Thomas Rast28452652012-08-03 14:16:24 +020067 diff_setup_done(&ids->diffopts);
Jeff Kingcc00e5c2018-08-28 17:22:55 -040068 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
Junio C Hamano5d23e132007-04-09 17:01:27 -070069 return 0;
70}
71
72int free_patch_ids(struct patch_ids *ids)
73{
Elijah Newren6da1a252020-11-02 18:55:05 +000074 hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
Junio C Hamano5d23e132007-04-09 17:01:27 -070075 return 0;
76}
77
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040078static int init_patch_id_entry(struct patch_id *patch,
79 struct commit *commit,
80 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070081{
Brandon Williams34f3c0e2017-05-30 10:30:53 -070082 struct object_id header_only_patch_id;
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040083
Kevin Willford683f17e2016-07-29 12:19:18 -040084 patch->commit = commit;
Stephen Boyda8f68552019-04-26 16:51:57 -070085 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040086 return -1;
Junio C Hamano5d23e132007-04-09 17:01:27 -070087
Eric Wongd22245a2019-10-06 23:30:27 +000088 hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040089 return 0;
Junio C Hamano5d23e132007-04-09 17:01:27 -070090}
91
Jeff Kingc9e3a4e2021-01-12 10:52:32 -050092struct patch_id *patch_id_iter_first(struct commit *commit,
Junio C Hamano5d23e132007-04-09 17:01:27 -070093 struct patch_ids *ids)
94{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040095 struct patch_id patch;
96
Jeff King7c810402016-09-12 13:56:41 -040097 if (!patch_id_defined(commit))
98 return NULL;
99
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400100 memset(&patch, 0, sizeof(patch));
101 if (init_patch_id_entry(&patch, commit, ids))
102 return NULL;
103
Eric Wong404ab782019-10-06 23:30:42 +0000104 return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
Junio C Hamano5d23e132007-04-09 17:01:27 -0700105}
106
Jeff Kingc9e3a4e2021-01-12 10:52:32 -0500107struct patch_id *patch_id_iter_next(struct patch_id *cur,
108 struct patch_ids *ids)
109{
110 return hashmap_get_next_entry(&ids->patches, cur, ent);
111}
112
113int has_commit_patch_id(struct commit *commit,
114 struct patch_ids *ids)
115{
116 return !!patch_id_iter_first(commit, ids);
117}
118
Junio C Hamano5d23e132007-04-09 17:01:27 -0700119struct patch_id *add_commit_patch_id(struct commit *commit,
120 struct patch_ids *ids)
121{
Johannes Schindelin57486932017-05-04 15:55:38 +0200122 struct patch_id *key;
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400123
Jeff King7c810402016-09-12 13:56:41 -0400124 if (!patch_id_defined(commit))
125 return NULL;
126
René Scharfeca56dad2021-03-13 17:17:22 +0100127 CALLOC_ARRAY(key, 1);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400128 if (init_patch_id_entry(key, commit, ids)) {
129 free(key);
130 return NULL;
131 }
132
Eric Wongb94e5c12019-10-06 23:30:29 +0000133 hashmap_add(&ids->patches, &key->ent);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400134 return key;
Junio C Hamano5d23e132007-04-09 17:01:27 -0700135}