blob: 69a14a3b50eeb62d4ec4561fe5395db12b06b366 [file] [log] [blame]
Junio C Hamano5d23e132007-04-09 17:01:27 -07001#include "cache.h"
2#include "diff.h"
3#include "commit.h"
Christian Couder5289bae2009-04-04 22:59:31 +02004#include "sha1-lookup.h"
Junio C Hamano5d23e132007-04-09 17:01:27 -07005#include "patch-ids.h"
6
Xiaolong Yeded2c092016-04-26 15:51:21 +08007int commit_patch_id(struct commit *commit, struct diff_options *options,
Junio C Hamano5d23e132007-04-09 17:01:27 -07008 unsigned char *sha1)
9{
10 if (commit->parents)
brian m. carlsoned1c9972015-11-10 02:22:29 +000011 diff_tree_sha1(commit->parents->item->object.oid.hash,
12 commit->object.oid.hash, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070013 else
brian m. carlsoned1c9972015-11-10 02:22:29 +000014 diff_root_tree_sha1(commit->object.oid.hash, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070015 diffcore_std(options);
Kevin Willford3e8e32c2016-07-29 12:19:19 -040016 return diff_flush_patch_id(options, sha1, 0);
Junio C Hamano5d23e132007-04-09 17:01:27 -070017}
18
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040019static int patch_id_cmp(struct patch_id *a,
20 struct patch_id *b,
21 void *keydata)
Junio C Hamano5d23e132007-04-09 17:01:27 -070022{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040023 return hashcmp(a->patch_id, b->patch_id);
Junio C Hamano5d23e132007-04-09 17:01:27 -070024}
25
Junio C Hamano5d23e132007-04-09 17:01:27 -070026int init_patch_ids(struct patch_ids *ids)
27{
28 memset(ids, 0, sizeof(*ids));
29 diff_setup(&ids->diffopts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010030 DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
Thomas Rast28452652012-08-03 14:16:24 +020031 diff_setup_done(&ids->diffopts);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040032 hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
Junio C Hamano5d23e132007-04-09 17:01:27 -070033 return 0;
34}
35
36int free_patch_ids(struct patch_ids *ids)
37{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040038 hashmap_free(&ids->patches, 1);
Junio C Hamano5d23e132007-04-09 17:01:27 -070039 return 0;
40}
41
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040042static int init_patch_id_entry(struct patch_id *patch,
43 struct commit *commit,
44 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070045{
Kevin Willford683f17e2016-07-29 12:19:18 -040046 patch->commit = commit;
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040047 if (commit_patch_id(commit, &ids->diffopts, patch->patch_id))
48 return -1;
Junio C Hamano5d23e132007-04-09 17:01:27 -070049
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040050 hashmap_entry_init(patch, sha1hash(patch->patch_id));
51 return 0;
Junio C Hamano5d23e132007-04-09 17:01:27 -070052}
53
54struct patch_id *has_commit_patch_id(struct commit *commit,
55 struct patch_ids *ids)
56{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040057 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 Hamano5d23e132007-04-09 17:01:27 -070064}
65
66struct patch_id *add_commit_patch_id(struct commit *commit,
67 struct patch_ids *ids)
68{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040069 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 Hamano5d23e132007-04-09 17:01:27 -070078}