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