| Elijah Newren | 4f6728d | 2023-03-21 06:25:56 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 2 | #include "diff.h" |
| 3 | #include "commit.h" |
| Elijah Newren | df6e874 | 2023-05-16 06:34:00 | [diff] [blame] | 4 | #include "hash.h" |
| Martin Ågren | bc62692 | 2020-12-31 11:56:23 | [diff] [blame] | 5 | #include "hash-lookup.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 6 | #include "hex.h" |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 7 | #include "patch-ids.h" |
| 8 | |
| Jeff King | 7c81040 | 2016-09-12 17:56:41 | [diff] [blame] | 9 | static 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 Ye | ded2c09 | 2016-04-26 07:51:21 | [diff] [blame] | 15 | int commit_patch_id(struct commit *commit, struct diff_options *options, |
| Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 | [diff] [blame] | 16 | struct object_id *oid, int diff_header_only) |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 17 | { |
| Jeff King | 7c81040 | 2016-09-12 17:56:41 | [diff] [blame] | 18 | if (!patch_id_defined(commit)) |
| 19 | return -1; |
| 20 | |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 21 | if (commit->parents) |
| Brandon Williams | 66f414f | 2017-05-30 17:31:03 | [diff] [blame] | 22 | diff_tree_oid(&commit->parents->item->object.oid, |
| 23 | &commit->object.oid, "", options); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 24 | else |
| Brandon Williams | 7b8dea0 | 2017-05-30 17:30:57 | [diff] [blame] | 25 | diff_root_tree_oid(&commit->object.oid, "", options); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 26 | diffcore_std(options); |
| Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 | [diff] [blame] | 27 | return diff_flush_patch_id(options, oid, diff_header_only); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 28 | } |
| 29 | |
| Kevin Willford | b3dfeeb | 2016-07-29 16:19:20 | [diff] [blame] | 30 | /* |
| 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 King | cc00e5c | 2018-08-28 21:22:55 | [diff] [blame] | 33 | * the "neq" in the name of this function, the caller only cares about |
| Kevin Willford | b3dfeeb | 2016-07-29 16:19:20 | [diff] [blame] | 34 | * 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 King | cc00e5c | 2018-08-28 21:22:55 | [diff] [blame] | 40 | static int patch_id_neq(const void *cmpfn_data, |
| Eric Wong | 939af16 | 2019-10-06 23:30:37 | [diff] [blame] | 41 | const struct hashmap_entry *eptr, |
| 42 | const struct hashmap_entry *entry_or_key, |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 43 | const void *keydata UNUSED) |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 44 | { |
| Stefan Beller | 8d0017d | 2017-07-01 00:28:34 | [diff] [blame] | 45 | /* NEEDSWORK: const correctness? */ |
| 46 | struct diff_options *opt = (void *)cmpfn_data; |
| Eric Wong | 939af16 | 2019-10-06 23:30:37 | [diff] [blame] | 47 | 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 Beller | 8d0017d | 2017-07-01 00:28:34 | [diff] [blame] | 51 | |
| Brandon Williams | 34f3c0e | 2017-05-30 17:30:53 | [diff] [blame] | 52 | if (is_null_oid(&a->patch_id) && |
| Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 | [diff] [blame] | 53 | commit_patch_id(a->commit, opt, &a->patch_id, 0)) |
| Kevin Willford | b3dfeeb | 2016-07-29 16:19:20 | [diff] [blame] | 54 | return error("Could not get patch ID for %s", |
| 55 | oid_to_hex(&a->commit->object.oid)); |
| Brandon Williams | 34f3c0e | 2017-05-30 17:30:53 | [diff] [blame] | 56 | if (is_null_oid(&b->patch_id) && |
| Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 | [diff] [blame] | 57 | commit_patch_id(b->commit, opt, &b->patch_id, 0)) |
| Kevin Willford | b3dfeeb | 2016-07-29 16:19:20 | [diff] [blame] | 58 | return error("Could not get patch ID for %s", |
| 59 | oid_to_hex(&b->commit->object.oid)); |
| Jeff King | cc00e5c | 2018-08-28 21:22:55 | [diff] [blame] | 60 | return !oideq(&a->patch_id, &b->patch_id); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 61 | } |
| 62 | |
| Nguyễn Thái Ngọc Duy | a7edadd | 2018-09-21 15:57:30 | [diff] [blame] | 63 | int init_patch_ids(struct repository *r, struct patch_ids *ids) |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 64 | { |
| 65 | memset(ids, 0, sizeof(*ids)); |
| Nguyễn Thái Ngọc Duy | a7edadd | 2018-09-21 15:57:30 | [diff] [blame] | 66 | repo_diff_setup(r, &ids->diffopts); |
| Jeff King | 5a29cbc | 2016-09-09 20:34:34 | [diff] [blame] | 67 | ids->diffopts.detect_rename = 0; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 68 | ids->diffopts.flags.recursive = 1; |
| Thomas Rast | 2845265 | 2012-08-03 12:16:24 | [diff] [blame] | 69 | diff_setup_done(&ids->diffopts); |
| Jeff King | cc00e5c | 2018-08-28 21:22:55 | [diff] [blame] | 70 | hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int free_patch_ids(struct patch_ids *ids) |
| 75 | { |
| Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 | [diff] [blame] | 76 | hashmap_clear_and_free(&ids->patches, struct patch_id, ent); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 80 | static int init_patch_id_entry(struct patch_id *patch, |
| 81 | struct commit *commit, |
| 82 | struct patch_ids *ids) |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 83 | { |
| Brandon Williams | 34f3c0e | 2017-05-30 17:30:53 | [diff] [blame] | 84 | struct object_id header_only_patch_id; |
| Kevin Willford | b3dfeeb | 2016-07-29 16:19:20 | [diff] [blame] | 85 | |
| Kevin Willford | 683f17e | 2016-07-29 16:19:18 | [diff] [blame] | 86 | patch->commit = commit; |
| Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 | [diff] [blame] | 87 | if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1)) |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 88 | return -1; |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 89 | |
| Eric Wong | d22245a | 2019-10-06 23:30:27 | [diff] [blame] | 90 | hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id)); |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 91 | return 0; |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 92 | } |
| 93 | |
| Jeff King | c9e3a4e | 2021-01-12 15:52:32 | [diff] [blame] | 94 | struct patch_id *patch_id_iter_first(struct commit *commit, |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 95 | struct patch_ids *ids) |
| 96 | { |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 97 | struct patch_id patch; |
| 98 | |
| Jeff King | 7c81040 | 2016-09-12 17:56:41 | [diff] [blame] | 99 | if (!patch_id_defined(commit)) |
| 100 | return NULL; |
| 101 | |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 102 | memset(&patch, 0, sizeof(patch)); |
| 103 | if (init_patch_id_entry(&patch, commit, ids)) |
| 104 | return NULL; |
| 105 | |
| Eric Wong | 404ab78 | 2019-10-06 23:30:42 | [diff] [blame] | 106 | return hashmap_get_entry(&ids->patches, &patch, ent, NULL); |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 107 | } |
| 108 | |
| Jeff King | c9e3a4e | 2021-01-12 15:52:32 | [diff] [blame] | 109 | struct 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 | |
| 115 | int 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 Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 121 | struct patch_id *add_commit_patch_id(struct commit *commit, |
| 122 | struct patch_ids *ids) |
| 123 | { |
| Johannes Schindelin | 5748693 | 2017-05-04 13:55:38 | [diff] [blame] | 124 | struct patch_id *key; |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 125 | |
| Jeff King | 7c81040 | 2016-09-12 17:56:41 | [diff] [blame] | 126 | if (!patch_id_defined(commit)) |
| 127 | return NULL; |
| 128 | |
| René Scharfe | ca56dad | 2021-03-13 16:17:22 | [diff] [blame] | 129 | CALLOC_ARRAY(key, 1); |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 130 | if (init_patch_id_entry(key, commit, ids)) { |
| 131 | free(key); |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| Eric Wong | b94e5c1 | 2019-10-06 23:30:29 | [diff] [blame] | 135 | hashmap_add(&ids->patches, &key->ent); |
| Kevin Willford | dfb7a1b | 2016-07-29 16:19:17 | [diff] [blame] | 136 | return key; |
| Junio C Hamano | 5d23e13 | 2007-04-10 00:01:27 | [diff] [blame] | 137 | } |