blob: fa8f11de82657c3edf4e5c0869d76ae6f12b57bb [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,
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040014 unsigned char *sha1, 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)
brian m. carlsoned1c9972015-11-10 02:22:29 +000020 diff_tree_sha1(commit->parents->item->object.oid.hash,
21 commit->object.oid.hash, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070022 else
brian m. carlsoned1c9972015-11-10 02:22:29 +000023 diff_root_tree_sha1(commit->object.oid.hash, "", options);
Junio C Hamano5d23e132007-04-09 17:01:27 -070024 diffcore_std(options);
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040025 return diff_flush_patch_id(options, sha1, 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
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 Willforddfb7a1b2016-07-29 12:19:17 -040038static int patch_id_cmp(struct patch_id *a,
39 struct patch_id *b,
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040040 struct diff_options *opt)
Junio C Hamano5d23e132007-04-09 17:01:27 -070041{
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040042 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 Willforddfb7a1b2016-07-29 12:19:17 -040050 return hashcmp(a->patch_id, b->patch_id);
Junio C Hamano5d23e132007-04-09 17:01:27 -070051}
52
Junio C Hamano5d23e132007-04-09 17:01:27 -070053int init_patch_ids(struct patch_ids *ids)
54{
55 memset(ids, 0, sizeof(*ids));
56 diff_setup(&ids->diffopts);
Jeff King5a29cbc2016-09-09 16:34:34 -040057 ids->diffopts.detect_rename = 0;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +010058 DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
Thomas Rast28452652012-08-03 14:16:24 +020059 diff_setup_done(&ids->diffopts);
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040060 hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
Junio C Hamano5d23e132007-04-09 17:01:27 -070061 return 0;
62}
63
64int free_patch_ids(struct patch_ids *ids)
65{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040066 hashmap_free(&ids->patches, 1);
Junio C Hamano5d23e132007-04-09 17:01:27 -070067 return 0;
68}
69
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040070static int init_patch_id_entry(struct patch_id *patch,
71 struct commit *commit,
72 struct patch_ids *ids)
Junio C Hamano5d23e132007-04-09 17:01:27 -070073{
brian m. carlsoncd025992017-03-26 16:01:25 +000074 unsigned char header_only_patch_id[GIT_MAX_RAWSZ];
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040075
Kevin Willford683f17e2016-07-29 12:19:18 -040076 patch->commit = commit;
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040077 if (commit_patch_id(commit, &ids->diffopts, header_only_patch_id, 1))
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040078 return -1;
Junio C Hamano5d23e132007-04-09 17:01:27 -070079
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040080 hashmap_entry_init(patch, sha1hash(header_only_patch_id));
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040081 return 0;
Junio C Hamano5d23e132007-04-09 17:01:27 -070082}
83
84struct patch_id *has_commit_patch_id(struct commit *commit,
85 struct patch_ids *ids)
86{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040087 struct patch_id patch;
88
Jeff King7c810402016-09-12 13:56:41 -040089 if (!patch_id_defined(commit))
90 return NULL;
91
Kevin Willforddfb7a1b2016-07-29 12:19:17 -040092 memset(&patch, 0, sizeof(patch));
93 if (init_patch_id_entry(&patch, commit, ids))
94 return NULL;
95
Kevin Willfordb3dfeeb2016-07-29 12:19:20 -040096 return hashmap_get(&ids->patches, &patch, &ids->diffopts);
Junio C Hamano5d23e132007-04-09 17:01:27 -070097}
98
99struct patch_id *add_commit_patch_id(struct commit *commit,
100 struct patch_ids *ids)
101{
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400102 struct patch_id *key = xcalloc(1, sizeof(*key));
103
Jeff King7c810402016-09-12 13:56:41 -0400104 if (!patch_id_defined(commit))
105 return NULL;
106
Kevin Willforddfb7a1b2016-07-29 12:19:17 -0400107 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 Hamano5d23e132007-04-09 17:01:27 -0700114}