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" |
Christian Couder | 5289bae | 2009-04-04 22:59:31 +0200 | [diff] [blame] | 4 | #include "sha1-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, |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 14 | struct object_id *oid, int diff_header_only) |
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); |
Brandon Williams | bd25f28 | 2017-05-30 10:30:54 -0700 | [diff] [blame] | 25 | return diff_flush_patch_id(options, oid, diff_header_only); |
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, |
Stefan Beller | 8d0017d | 2017-06-30 17:28:34 -0700 | [diff] [blame] | 39 | const void *entry, |
| 40 | const void *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; |
| 45 | struct patch_id *a = (void *)entry; |
| 46 | struct patch_id *b = (void *)entry_or_key; |
| 47 | |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 48 | if (is_null_oid(&a->patch_id) && |
| 49 | commit_patch_id(a->commit, opt, &a->patch_id, 0)) |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 50 | return error("Could not get patch ID for %s", |
| 51 | oid_to_hex(&a->commit->object.oid)); |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 52 | if (is_null_oid(&b->patch_id) && |
| 53 | commit_patch_id(b->commit, opt, &b->patch_id, 0)) |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 54 | return error("Could not get patch ID for %s", |
| 55 | oid_to_hex(&b->commit->object.oid)); |
Jeff King | cc00e5c | 2018-08-28 17:22:55 -0400 | [diff] [blame] | 56 | return !oideq(&a->patch_id, &b->patch_id); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Nguyễn Thái Ngọc Duy | a7edadd | 2018-09-21 17:57:30 +0200 | [diff] [blame] | 59 | int init_patch_ids(struct repository *r, struct patch_ids *ids) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 60 | { |
| 61 | memset(ids, 0, sizeof(*ids)); |
Nguyễn Thái Ngọc Duy | a7edadd | 2018-09-21 17:57:30 +0200 | [diff] [blame] | 62 | repo_diff_setup(r, &ids->diffopts); |
Jeff King | 5a29cbc | 2016-09-09 16:34:34 -0400 | [diff] [blame] | 63 | ids->diffopts.detect_rename = 0; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 64 | ids->diffopts.flags.recursive = 1; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 65 | diff_setup_done(&ids->diffopts); |
Jeff King | cc00e5c | 2018-08-28 17:22:55 -0400 | [diff] [blame] | 66 | hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int free_patch_ids(struct patch_ids *ids) |
| 71 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 72 | hashmap_free(&ids->patches, 1); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 76 | static int init_patch_id_entry(struct patch_id *patch, |
| 77 | struct commit *commit, |
| 78 | struct patch_ids *ids) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 79 | { |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 80 | struct object_id header_only_patch_id; |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 81 | |
Kevin Willford | 683f17e | 2016-07-29 12:19:18 -0400 | [diff] [blame] | 82 | patch->commit = commit; |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 83 | if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1)) |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 84 | return -1; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 85 | |
Brandon Williams | 34f3c0e | 2017-05-30 10:30:53 -0700 | [diff] [blame] | 86 | hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash)); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 87 | return 0; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | struct patch_id *has_commit_patch_id(struct commit *commit, |
| 91 | struct patch_ids *ids) |
| 92 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 93 | struct patch_id patch; |
| 94 | |
Jeff King | 7c81040 | 2016-09-12 13:56:41 -0400 | [diff] [blame] | 95 | if (!patch_id_defined(commit)) |
| 96 | return NULL; |
| 97 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 98 | memset(&patch, 0, sizeof(patch)); |
| 99 | if (init_patch_id_entry(&patch, commit, ids)) |
| 100 | return NULL; |
| 101 | |
Stefan Beller | 3da492f | 2017-06-30 12:14:06 -0700 | [diff] [blame] | 102 | return hashmap_get(&ids->patches, &patch, NULL); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | struct patch_id *add_commit_patch_id(struct commit *commit, |
| 106 | struct patch_ids *ids) |
| 107 | { |
Johannes Schindelin | 5748693 | 2017-05-04 15:55:38 +0200 | [diff] [blame] | 108 | struct patch_id *key; |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 109 | |
Jeff King | 7c81040 | 2016-09-12 13:56:41 -0400 | [diff] [blame] | 110 | if (!patch_id_defined(commit)) |
| 111 | return NULL; |
| 112 | |
Johannes Schindelin | 5748693 | 2017-05-04 15:55:38 +0200 | [diff] [blame] | 113 | key = xcalloc(1, sizeof(*key)); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 114 | if (init_patch_id_entry(key, commit, ids)) { |
| 115 | free(key); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | hashmap_add(&ids->patches, key); |
| 120 | return key; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 121 | } |