]> git.ipfire.org Git - thirdparty/git.git/blame - patch-ids.c
patch-ids: add flag to create the diff patch id using header only data
[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
ded2c097 7int commit_patch_id(struct commit *commit, struct diff_options *options,
5d23e133
JH
8 unsigned char *sha1)
9{
10 if (commit->parents)
ed1c9977 11 diff_tree_sha1(commit->parents->item->object.oid.hash,
12 commit->object.oid.hash, "", options);
5d23e133 13 else
ed1c9977 14 diff_root_tree_sha1(commit->object.oid.hash, "", options);
5d23e133 15 diffcore_std(options);
3e8e32c3 16 return diff_flush_patch_id(options, sha1, 0);
5d23e133
JH
17}
18
dfb7a1b4
KW
19static int patch_id_cmp(struct patch_id *a,
20 struct patch_id *b,
21 void *keydata)
5d23e133 22{
dfb7a1b4 23 return hashcmp(a->patch_id, b->patch_id);
5d23e133
JH
24}
25
5d23e133
JH
26int init_patch_ids(struct patch_ids *ids)
27{
28 memset(ids, 0, sizeof(*ids));
29 diff_setup(&ids->diffopts);
8f67f8ae 30 DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
28452655 31 diff_setup_done(&ids->diffopts);
dfb7a1b4 32 hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
5d23e133
JH
33 return 0;
34}
35
36int free_patch_ids(struct patch_ids *ids)
37{
dfb7a1b4 38 hashmap_free(&ids->patches, 1);
5d23e133
JH
39 return 0;
40}
41
dfb7a1b4
KW
42static int init_patch_id_entry(struct patch_id *patch,
43 struct commit *commit,
44 struct patch_ids *ids)
5d23e133 45{
683f17ec 46 patch->commit = commit;
dfb7a1b4
KW
47 if (commit_patch_id(commit, &ids->diffopts, patch->patch_id))
48 return -1;
5d23e133 49
dfb7a1b4
KW
50 hashmap_entry_init(patch, sha1hash(patch->patch_id));
51 return 0;
5d23e133
JH
52}
53
54struct patch_id *has_commit_patch_id(struct commit *commit,
55 struct patch_ids *ids)
56{
dfb7a1b4
KW
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);
5d23e133
JH
64}
65
66struct patch_id *add_commit_patch_id(struct commit *commit,
67 struct patch_ids *ids)
68{
dfb7a1b4
KW
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;
5d23e133 78}