🌐 AI搜索 & 代理 主页
blob: c3e1a0dd216c80c92c47f0e2a98d74a57c422742 [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"
Martin Ågrenbc626922020-12-31 11:56:235#include "hash-lookup.h"
Elijah Newren41771fa2023-02-24 00:09:276#include "hex.h"
Junio C Hamano5d23e132007-04-10 00:01:277#include "patch-ids.h"
8
Jeff King7c810402016-09-12 17:56:419static int patch_id_defined(struct commit *commit)
10{
11 /* must be 0 or 1 parents */
12 return !commit->parents || !commit->parents->next;
13}
14
Xiaolong Yeded2c092016-04-26 07:51:2115int commit_patch_id(struct commit *commit, struct diff_options *options,
Jerry Zhang51276c12022-10-24 20:07:4016 struct object_id *oid, int diff_header_only)
Junio C Hamano5d23e132007-04-10 00:01:2717{
Jeff King7c810402016-09-12 17:56:4118 if (!patch_id_defined(commit))
19 return -1;
20
Junio C Hamano5d23e132007-04-10 00:01:2721 if (commit->parents)
Brandon Williams66f414f2017-05-30 17:31:0322 diff_tree_oid(&commit->parents->item->object.oid,
23 &commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-10 00:01:2724 else
Brandon Williams7b8dea02017-05-30 17:30:5725 diff_root_tree_oid(&commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-10 00:01:2726 diffcore_std(options);
Jerry Zhang51276c12022-10-24 20:07:4027 return diff_flush_patch_id(options, oid, diff_header_only);
Junio C Hamano5d23e132007-04-10 00:01:2728}
29
Kevin Willfordb3dfeeb2016-07-29 16:19:2030/*
31 * When we cannot load the full patch-id for both commits for whatever
32 * reason, the function returns -1 (i.e. return error(...)). Despite
Jeff Kingcc00e5c2018-08-28 21:22:5533 * the "neq" in the name of this function, the caller only cares about
Kevin Willfordb3dfeeb2016-07-29 16:19:2034 * the return value being zero (a and b are equivalent) or non-zero (a
35 * and b are different), and returning non-zero would keep both in the
36 * result, even if they actually were equivalent, in order to err on
37 * the side of safety. The actual value being negative does not have
38 * any significance; only that it is non-zero matters.
39 */
Jeff Kingcc00e5c2018-08-28 21:22:5540static int patch_id_neq(const void *cmpfn_data,
Eric Wong939af162019-10-06 23:30:3741 const struct hashmap_entry *eptr,
42 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:4843 const void *keydata UNUSED)
Junio C Hamano5d23e132007-04-10 00:01:2744{
Stefan Beller8d0017d2017-07-01 00:28:3445 /* NEEDSWORK: const correctness? */
46 struct diff_options *opt = (void *)cmpfn_data;
Eric Wong939af162019-10-06 23:30:3747 struct patch_id *a, *b;
48
49 a = container_of(eptr, struct patch_id, ent);
50 b = container_of(entry_or_key, struct patch_id, ent);
Stefan Beller8d0017d2017-07-01 00:28:3451
Brandon Williams34f3c0e2017-05-30 17:30:5352 if (is_null_oid(&a->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:4053 commit_patch_id(a->commit, opt, &a->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 16:19:2054 return error("Could not get patch ID for %s",
55 oid_to_hex(&a->commit->object.oid));
Brandon Williams34f3c0e2017-05-30 17:30:5356 if (is_null_oid(&b->patch_id) &&
Jerry Zhang51276c12022-10-24 20:07:4057 commit_patch_id(b->commit, opt, &b->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 16:19:2058 return error("Could not get patch ID for %s",
59 oid_to_hex(&b->commit->object.oid));
Jeff Kingcc00e5c2018-08-28 21:22:5560 return !oideq(&a->patch_id, &b->patch_id);
Junio C Hamano5d23e132007-04-10 00:01:2761}
62
Nguyễn Thái Ngọc Duya7edadd2018-09-21 15:57:3063int init_patch_ids(struct repository *r, struct patch_ids *ids)
Junio C Hamano5d23e132007-04-10 00:01:2764{
65 memset(ids, 0, sizeof(*ids));
Nguyễn Thái Ngọc Duya7edadd2018-09-21 15:57:3066 repo_diff_setup(r, &ids->diffopts);
Jeff King5a29cbc2016-09-09 20:34:3467 ids->diffopts.detect_rename = 0;
Brandon Williams0d1e0e72017-10-31 18:19:1168 ids->diffopts.flags.recursive = 1;
Thomas Rast28452652012-08-03 12:16:2469 diff_setup_done(&ids->diffopts);
Jeff Kingcc00e5c2018-08-28 21:22:5570 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
Junio C Hamano5d23e132007-04-10 00:01:2771 return 0;
72}
73
74int free_patch_ids(struct patch_ids *ids)
75{
Elijah Newren6da1a252020-11-02 18:55:0576 hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
Junio C Hamano5d23e132007-04-10 00:01:2777 return 0;
78}
79
Kevin Willforddfb7a1b2016-07-29 16:19:1780static int init_patch_id_entry(struct patch_id *patch,
81 struct commit *commit,
82 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-10 00:01:2783{
Brandon Williams34f3c0e2017-05-30 17:30:5384 struct object_id header_only_patch_id;
Kevin Willfordb3dfeeb2016-07-29 16:19:2085
Kevin Willford683f17e2016-07-29 16:19:1886 patch->commit = commit;
Jerry Zhang51276c12022-10-24 20:07:4087 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
Kevin Willforddfb7a1b2016-07-29 16:19:1788 return -1;
Junio C Hamano5d23e132007-04-10 00:01:2789
Eric Wongd22245a2019-10-06 23:30:2790 hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
Kevin Willforddfb7a1b2016-07-29 16:19:1791 return 0;
Junio C Hamano5d23e132007-04-10 00:01:2792}
93
Jeff Kingc9e3a4e2021-01-12 15:52:3294struct patch_id *patch_id_iter_first(struct commit *commit,
Junio C Hamano5d23e132007-04-10 00:01:2795 struct patch_ids *ids)
96{
Kevin Willforddfb7a1b2016-07-29 16:19:1797 struct patch_id patch;
98
Jeff King7c810402016-09-12 17:56:4199 if (!patch_id_defined(commit))
100 return NULL;
101
Kevin Willforddfb7a1b2016-07-29 16:19:17102 memset(&patch, 0, sizeof(patch));
103 if (init_patch_id_entry(&patch, commit, ids))
104 return NULL;
105
Eric Wong404ab782019-10-06 23:30:42106 return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
Junio C Hamano5d23e132007-04-10 00:01:27107}
108
Jeff Kingc9e3a4e2021-01-12 15:52:32109struct patch_id *patch_id_iter_next(struct patch_id *cur,
110 struct patch_ids *ids)
111{
112 return hashmap_get_next_entry(&ids->patches, cur, ent);
113}
114
115int has_commit_patch_id(struct commit *commit,
116 struct patch_ids *ids)
117{
118 return !!patch_id_iter_first(commit, ids);
119}
120
Junio C Hamano5d23e132007-04-10 00:01:27121struct patch_id *add_commit_patch_id(struct commit *commit,
122 struct patch_ids *ids)
123{
Johannes Schindelin57486932017-05-04 13:55:38124 struct patch_id *key;
Kevin Willforddfb7a1b2016-07-29 16:19:17125
Jeff King7c810402016-09-12 17:56:41126 if (!patch_id_defined(commit))
127 return NULL;
128
René Scharfeca56dad2021-03-13 16:17:22129 CALLOC_ARRAY(key, 1);
Kevin Willforddfb7a1b2016-07-29 16:19:17130 if (init_patch_id_entry(key, commit, ids)) {
131 free(key);
132 return NULL;
133 }
134
Eric Wongb94e5c12019-10-06 23:30:29135 hashmap_add(&ids->patches, &key->ent);
Kevin Willforddfb7a1b2016-07-29 16:19:17136 return key;
Junio C Hamano5d23e132007-04-10 00:01:27137}