blob: c262e1be9c9c912cc12c5b60a08e69f8cf9a30c9 [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
Jeff King7c810402016-09-12 13:56:41 -04007static 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 Yeded2c092016-04-26 15:51:21 +080013int commit_patch_id(struct commit *commit, struct diff_options *options,
Brandon Williams34f3c0e2017-05-30 10:30:53 -070014 struct object_id *oid, int diff_header_only)
Junio C Hamano5d23e132007-04-09 17:01:27 -070015{
Jeff King7c810402016-09-12 13:56:41 -040016 if (!patch_id_defined(commit))
17 return -1;
18
Junio C Hamano5d23e132007-04-09 17:01:27 -070019 if (commit->parents)
Brandon Williams66f414f2017-05-30 10:31:03 -070020 diff_tree_oid(&commit->parents->item->object.oid,
21 &commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070022 else
Brandon Williams7b8dea02017-05-30 10:30:57 -070023 diff_root_tree_oid(&commit->object.oid, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070024 diffcore_std(options);
Brandon Williamsbd25f282017-05-30 10:30:54 -070025 return diff_flush_patch_id(options, oid, diff_header_only);
Junio C Hamano5d23e132007-04-09 17:01:27 -070026}
27
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040028/*
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 Kingcc00e5c2018-08-28 17:22:55 -040031 * the "neq" in the name of this function, the caller only cares about
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040032 * 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 Kingcc00e5c2018-08-28 17:22:55 -040038static int patch_id_neq(const void *cmpfn_data,
Stefan Beller8d0017d2017-06-30 17:28:34 -070039 const void *entry,
40 const void *entry_or_key,
Stefan Beller3da492f2017-06-30 12:14:06 -070041 const void *unused_keydata)
Junio C Hamano5d23e132007-04-09 17:01:27 -070042{
Stefan Beller8d0017d2017-06-30 17:28:34 -070043 /* 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 Williams34f3c0e2017-05-30 10:30:53 -070048 if (is_null_oid(&a->patch_id) &&
49 commit_patch_id(a->commit, opt, &a->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040050 return error("Could not get patch ID for %s",
51 oid_to_hex(&a->commit->object.oid));
Brandon Williams34f3c0e2017-05-30 10:30:53 -070052 if (is_null_oid(&b->patch_id) &&
53 commit_patch_id(b->commit, opt, &b->patch_id, 0))
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040054 return error("Could not get patch ID for %s",
55 oid_to_hex(&b->commit->object.oid));
Jeff Kingcc00e5c2018-08-28 17:22:55 -040056 return !oideq(&a->patch_id, &b->patch_id);
Junio C Hamano5d23e132007-04-09 17:01:27 -070057}
58
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020059int init_patch_ids(struct repository *r, struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070060{
61 memset(ids, 0, sizeof(*ids));
Nguyễn Thái Ngọc Duya7edadd2018-09-21 17:57:30 +020062 repo_diff_setup(r, &ids->diffopts);
Jeff King5a29cbc2016-09-09 16:34:34 -040063 ids->diffopts.detect_rename = 0;
Brandon Williams0d1e0e72017-10-31 11:19:11 -070064 ids->diffopts.flags.recursive = 1;
Thomas Rast28452652012-08-03 14:16:24 +020065 diff_setup_done(&ids->diffopts);
Jeff Kingcc00e5c2018-08-28 17:22:55 -040066 hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
Junio C Hamano5d23e132007-04-09 17:01:27 -070067 return 0;
68}
69
70int free_patch_ids(struct patch_ids *ids)
71{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040072 hashmap_free(&ids->patches, 1);
Junio C Hamano5d23e132007-04-09 17:01:27 -070073 return 0;
74}
75
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040076static int init_patch_id_entry(struct patch_id *patch,
77 struct commit *commit,
78 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070079{
Brandon Williams34f3c0e2017-05-30 10:30:53 -070080 struct object_id header_only_patch_id;
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040081
Kevin Willford683f17e2016-07-29 12:19:18 -040082 patch->commit = commit;
Brandon Williams34f3c0e2017-05-30 10:30:53 -070083 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040084 return -1;
Junio C Hamano5d23e132007-04-09 17:01:27 -070085
Brandon Williams34f3c0e2017-05-30 10:30:53 -070086 hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040087 return 0;
Junio C Hamano5d23e132007-04-09 17:01:27 -070088}
89
90struct patch_id *has_commit_patch_id(struct commit *commit,
91 struct patch_ids *ids)
92{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040093 struct patch_id patch;
94
Jeff King7c810402016-09-12 13:56:41 -040095 if (!patch_id_defined(commit))
96 return NULL;
97
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040098 memset(&patch, 0, sizeof(patch));
99 if (init_patch_id_entry(&patch, commit, ids))
100 return NULL;
101
Stefan Beller3da492f2017-06-30 12:14:06 -0700102 return hashmap_get(&ids->patches, &patch, NULL);
Junio C Hamano5d23e132007-04-09 17:01:27 -0700103}
104
105struct patch_id *add_commit_patch_id(struct commit *commit,
106 struct patch_ids *ids)
107{
Johannes Schindelin57486932017-05-04 15:55:38 +0200108 struct patch_id *key;
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400109
Jeff King7c810402016-09-12 13:56:41 -0400110 if (!patch_id_defined(commit))
111 return NULL;
112
Johannes Schindelin57486932017-05-04 15:55:38 +0200113 key = xcalloc(1, sizeof(*key));
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400114 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 Hamano5d23e132007-04-09 17:01:27 -0700121}