git
/
patch-ids.c
136 строк · 3.7 Кб
1#include "git-compat-util.h"2#include "diff.h"3#include "commit.h"4#include "hash.h"5#include "hex.h"6#include "patch-ids.h"7
8static int patch_id_defined(struct commit *commit)9{
10/* must be 0 or 1 parents */11return !commit->parents || !commit->parents->next;12}
13
14int commit_patch_id(struct commit *commit, struct diff_options *options,15struct object_id *oid, int diff_header_only)16{
17if (!patch_id_defined(commit))18return -1;19
20if (commit->parents)21diff_tree_oid(&commit->parents->item->object.oid,22&commit->object.oid, "", options);23else24diff_root_tree_oid(&commit->object.oid, "", options);25diffcore_std(options);26return diff_flush_patch_id(options, oid, diff_header_only);27}
28
29/*
30* When we cannot load the full patch-id for both commits for whatever
31* reason, the function returns -1 (i.e. return error(...)). Despite
32* the "neq" in the name of this function, the caller only cares about
33* the return value being zero (a and b are equivalent) or non-zero (a
34* and b are different), and returning non-zero would keep both in the
35* result, even if they actually were equivalent, in order to err on
36* the side of safety. The actual value being negative does not have
37* any significance; only that it is non-zero matters.
38*/
39static int patch_id_neq(const void *cmpfn_data,40const struct hashmap_entry *eptr,41const struct hashmap_entry *entry_or_key,42const void *keydata UNUSED)43{
44/* NEEDSWORK: const correctness? */45struct diff_options *opt = (void *)cmpfn_data;46struct patch_id *a, *b;47
48a = container_of(eptr, struct patch_id, ent);49b = container_of(entry_or_key, struct patch_id, ent);50
51if (is_null_oid(&a->patch_id) &&52commit_patch_id(a->commit, opt, &a->patch_id, 0))53return error("Could not get patch ID for %s",54oid_to_hex(&a->commit->object.oid));55if (is_null_oid(&b->patch_id) &&56commit_patch_id(b->commit, opt, &b->patch_id, 0))57return error("Could not get patch ID for %s",58oid_to_hex(&b->commit->object.oid));59return !oideq(&a->patch_id, &b->patch_id);60}
61
62int init_patch_ids(struct repository *r, struct patch_ids *ids)63{
64memset(ids, 0, sizeof(*ids));65repo_diff_setup(r, &ids->diffopts);66ids->diffopts.detect_rename = 0;67ids->diffopts.flags.recursive = 1;68diff_setup_done(&ids->diffopts);69hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);70return 0;71}
72
73int free_patch_ids(struct patch_ids *ids)74{
75hashmap_clear_and_free(&ids->patches, struct patch_id, ent);76return 0;77}
78
79static int init_patch_id_entry(struct patch_id *patch,80struct commit *commit,81struct patch_ids *ids)82{
83struct object_id header_only_patch_id;84
85patch->commit = commit;86if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))87return -1;88
89hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));90return 0;91}
92
93struct patch_id *patch_id_iter_first(struct commit *commit,94struct patch_ids *ids)95{
96struct patch_id patch;97
98if (!patch_id_defined(commit))99return NULL;100
101memset(&patch, 0, sizeof(patch));102if (init_patch_id_entry(&patch, commit, ids))103return NULL;104
105return hashmap_get_entry(&ids->patches, &patch, ent, NULL);106}
107
108struct patch_id *patch_id_iter_next(struct patch_id *cur,109struct patch_ids *ids)110{
111return hashmap_get_next_entry(&ids->patches, cur, ent);112}
113
114int has_commit_patch_id(struct commit *commit,115struct patch_ids *ids)116{
117return !!patch_id_iter_first(commit, ids);118}
119
120struct patch_id *add_commit_patch_id(struct commit *commit,121struct patch_ids *ids)122{
123struct patch_id *key;124
125if (!patch_id_defined(commit))126return NULL;127
128CALLOC_ARRAY(key, 1);129if (init_patch_id_entry(key, commit, ids)) {130free(key);131return NULL;132}133
134hashmap_add(&ids->patches, &key->ent);135return key;136}
137