]> git.ipfire.org Git - thirdparty/git.git/blame - patch-ids.c
notes-merge: convert write_note_to_worktree to struct object_id
[thirdparty/git.git] / patch-ids.c
CommitLineData
5d23e133
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
5289bae1 4#include "sha1-lookup.h"
5d23e133
JH
5#include "patch-ids.h"
6
7c810407
JK
7static int patch_id_defined(struct commit *commit)
8{
9 /* must be 0 or 1 parents */
10 return !commit->parents || !commit->parents->next;
11}
12
ded2c097 13int commit_patch_id(struct commit *commit, struct diff_options *options,
34f3c0eb 14 struct object_id *oid, int diff_header_only)
5d23e133 15{
7c810407
JK
16 if (!patch_id_defined(commit))
17 return -1;
18
5d23e133 19 if (commit->parents)
ed1c9977 20 diff_tree_sha1(commit->parents->item->object.oid.hash,
21 commit->object.oid.hash, "", options);
5d23e133 22 else
7b8dea0c 23 diff_root_tree_oid(&commit->object.oid, "", options);
5d23e133 24 diffcore_std(options);
bd25f288 25 return diff_flush_patch_id(options, oid, diff_header_only);
5d23e133
JH
26}
27
b3dfeebb
KW
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 */
dfb7a1b4
KW
38static int patch_id_cmp(struct patch_id *a,
39 struct patch_id *b,
b3dfeebb 40 struct diff_options *opt)
5d23e133 41{
34f3c0eb
BW
42 if (is_null_oid(&a->patch_id) &&
43 commit_patch_id(a->commit, opt, &a->patch_id, 0))
b3dfeebb
KW
44 return error("Could not get patch ID for %s",
45 oid_to_hex(&a->commit->object.oid));
34f3c0eb
BW
46 if (is_null_oid(&b->patch_id) &&
47 commit_patch_id(b->commit, opt, &b->patch_id, 0))
b3dfeebb
KW
48 return error("Could not get patch ID for %s",
49 oid_to_hex(&b->commit->object.oid));
34f3c0eb 50 return oidcmp(&a->patch_id, &b->patch_id);
5d23e133
JH
51}
52
5d23e133
JH
53int init_patch_ids(struct patch_ids *ids)
54{
55 memset(ids, 0, sizeof(*ids));
56 diff_setup(&ids->diffopts);
5a29cbc6 57 ids->diffopts.detect_rename = 0;
8f67f8ae 58 DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
28452655 59 diff_setup_done(&ids->diffopts);
dfb7a1b4 60 hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
5d23e133
JH
61 return 0;
62}
63
64int free_patch_ids(struct patch_ids *ids)
65{
dfb7a1b4 66 hashmap_free(&ids->patches, 1);
5d23e133
JH
67 return 0;
68}
69
dfb7a1b4
KW
70static int init_patch_id_entry(struct patch_id *patch,
71 struct commit *commit,
72 struct patch_ids *ids)
5d23e133 73{
34f3c0eb 74 struct object_id header_only_patch_id;
b3dfeebb 75
683f17ec 76 patch->commit = commit;
34f3c0eb 77 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
dfb7a1b4 78 return -1;
5d23e133 79
34f3c0eb 80 hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));
dfb7a1b4 81 return 0;
5d23e133
JH
82}
83
84struct patch_id *has_commit_patch_id(struct commit *commit,
85 struct patch_ids *ids)
86{
dfb7a1b4
KW
87 struct patch_id patch;
88
7c810407
JK
89 if (!patch_id_defined(commit))
90 return NULL;
91
dfb7a1b4
KW
92 memset(&patch, 0, sizeof(patch));
93 if (init_patch_id_entry(&patch, commit, ids))
94 return NULL;
95
b3dfeebb 96 return hashmap_get(&ids->patches, &patch, &ids->diffopts);
5d23e133
JH
97}
98
99struct patch_id *add_commit_patch_id(struct commit *commit,
100 struct patch_ids *ids)
101{
5748693b 102 struct patch_id *key;
dfb7a1b4 103
7c810407
JK
104 if (!patch_id_defined(commit))
105 return NULL;
106
5748693b 107 key = xcalloc(1, sizeof(*key));
dfb7a1b4
KW
108 if (init_patch_id_entry(key, commit, ids)) {
109 free(key);
110 return NULL;
111 }
112
113 hashmap_add(&ids->patches, key);
114 return key;
5d23e133 115}