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 | |
Xiaolong Ye | ded2c09 | 2016-04-26 15:51:21 +0800 | [diff] [blame] | 7 | int commit_patch_id(struct commit *commit, struct diff_options *options, |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 8 | unsigned char *sha1) |
| 9 | { |
| 10 | if (commit->parents) |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 11 | diff_tree_sha1(commit->parents->item->object.oid.hash, |
| 12 | commit->object.oid.hash, "", options); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 13 | else |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 14 | diff_root_tree_sha1(commit->object.oid.hash, "", options); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 15 | diffcore_std(options); |
Kevin Willford | 3e8e32c | 2016-07-29 12:19:19 -0400 | [diff] [blame^] | 16 | return diff_flush_patch_id(options, sha1, 0); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 17 | } |
| 18 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 19 | static int patch_id_cmp(struct patch_id *a, |
| 20 | struct patch_id *b, |
| 21 | void *keydata) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 22 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 23 | return hashcmp(a->patch_id, b->patch_id); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 26 | int init_patch_ids(struct patch_ids *ids) |
| 27 | { |
| 28 | memset(ids, 0, sizeof(*ids)); |
| 29 | diff_setup(&ids->diffopts); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 30 | DIFF_OPT_SET(&ids->diffopts, RECURSIVE); |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 31 | diff_setup_done(&ids->diffopts); |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 32 | hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int free_patch_ids(struct patch_ids *ids) |
| 37 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 38 | hashmap_free(&ids->patches, 1); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 42 | static int init_patch_id_entry(struct patch_id *patch, |
| 43 | struct commit *commit, |
| 44 | struct patch_ids *ids) |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 45 | { |
Kevin Willford | 683f17e | 2016-07-29 12:19:18 -0400 | [diff] [blame] | 46 | patch->commit = commit; |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 47 | if (commit_patch_id(commit, &ids->diffopts, patch->patch_id)) |
| 48 | return -1; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 49 | |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 50 | hashmap_entry_init(patch, sha1hash(patch->patch_id)); |
| 51 | return 0; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | struct patch_id *has_commit_patch_id(struct commit *commit, |
| 55 | struct patch_ids *ids) |
| 56 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 57 | struct patch_id patch; |
| 58 | |
| 59 | memset(&patch, 0, sizeof(patch)); |
| 60 | if (init_patch_id_entry(&patch, commit, ids)) |
| 61 | return NULL; |
| 62 | |
| 63 | return hashmap_get(&ids->patches, &patch, NULL); |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | struct patch_id *add_commit_patch_id(struct commit *commit, |
| 67 | struct patch_ids *ids) |
| 68 | { |
Kevin Willford | dfb7a1b | 2016-07-29 12:19:17 -0400 | [diff] [blame] | 69 | struct patch_id *key = xcalloc(1, sizeof(*key)); |
| 70 | |
| 71 | if (init_patch_id_entry(key, commit, ids)) { |
| 72 | free(key); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | hashmap_add(&ids->patches, key); |
| 77 | return key; |
Junio C Hamano | 5d23e13 | 2007-04-09 17:01:27 -0700 | [diff] [blame] | 78 | } |