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, |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 14 | unsigned char *sha1, 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) |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 20 | diff_tree_sha1(commit->parents->item->object.oid.hash, |
| 21 | commit->object.oid.hash, "", options); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 22 | else |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 23 | diff_root_tree_sha1(commit->object.oid.hash, "", options); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 24 | diffcore_std(options); |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 25 | return diff_flush_patch_id(options, sha1, 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 |
| 31 | * the "cmp" in the name of this function, the caller only cares about |
| 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 | */ |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 38 | static int patch_id_cmp(struct patch_id *a, |
| 39 | struct patch_id *b, |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 40 | struct diff_options *opt) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 41 | { |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 42 | if (is_null_sha1(a->patch_id) && |
| 43 | commit_patch_id(a->commit, opt, a->patch_id, 0)) |
| 44 | return error("Could not get patch ID for %s", |
| 45 | oid_to_hex(&a->commit->object.oid)); |
| 46 | if (is_null_sha1(b->patch_id) && |
| 47 | commit_patch_id(b->commit, opt, b->patch_id, 0)) |
| 48 | return error("Could not get patch ID for %s", |
| 49 | oid_to_hex(&b->commit->object.oid)); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 50 | return hashcmp(a->patch_id, b->patch_id); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 53 | int init_patch_ids(struct patch_ids *ids) |
| 54 | { |
| 55 | memset(ids, 0, sizeof(*ids)); |
| 56 | diff_setup(&ids->diffopts); |
Jeff King | 5a29cbc | 2016-09-09 16:34:34 -0400 | [diff] [blame] | 57 | ids->diffopts.detect_rename = 0; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 58 | DIFF_OPT_SET(&ids->diffopts, RECURSIVE); |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 59 | diff_setup_done(&ids->diffopts); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 60 | hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int free_patch_ids(struct patch_ids *ids) |
| 65 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 66 | hashmap_free(&ids->patches, 1); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 70 | static int init_patch_id_entry(struct patch_id *patch, |
| 71 | struct commit *commit, |
| 72 | struct patch_ids *ids) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 73 | { |
brian m. carlson | cd02599 | 2017-03-26 16:01:25 +0000 | [diff] [blame] | 74 | unsigned char header_only_patch_id[GIT_MAX_RAWSZ]; |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 75 | |
Kevin Willford | 683f17e | 2016-07-29 12:19:18 -0400 | [diff] [blame] | 76 | patch->commit = commit; |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 77 | if (commit_patch_id(commit, &ids->diffopts, header_only_patch_id, 1)) |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 78 | return -1; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 79 | |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 80 | hashmap_entry_init(patch, sha1hash(header_only_patch_id)); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 81 | return 0; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | struct patch_id *has_commit_patch_id(struct commit *commit, |
| 85 | struct patch_ids *ids) |
| 86 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 87 | struct patch_id patch; |
| 88 | |
Jeff King | 7c81040 | 2016-09-12 13:56:41 -0400 | [diff] [blame] | 89 | if (!patch_id_defined(commit)) |
| 90 | return NULL; |
| 91 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 92 | memset(&patch, 0, sizeof(patch)); |
| 93 | if (init_patch_id_entry(&patch, commit, ids)) |
| 94 | return NULL; |
| 95 | |
Kevin Willford | b3dfeeb | 2016-07-29 12:19:20 -0400 | [diff] [blame] | 96 | return hashmap_get(&ids->patches, &patch, &ids->diffopts); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | struct patch_id *add_commit_patch_id(struct commit *commit, |
| 100 | struct patch_ids *ids) |
| 101 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 102 | struct patch_id *key = xcalloc(1, sizeof(*key)); |
| 103 | |
Jeff King | 7c81040 | 2016-09-12 13:56:41 -0400 | [diff] [blame] | 104 | if (!patch_id_defined(commit)) |
| 105 | return NULL; |
| 106 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 107 | if (init_patch_id_entry(key, commit, ids)) { |
| 108 | free(key); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | hashmap_add(&ids->patches, key); |
| 113 | return key; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 114 | } |