patch-ids.c 3.24 KB
Newer Older
1 2 3
#include "cache.h"
#include "diff.h"
#include "commit.h"
4
#include "sha1-lookup.h"
5 6
#include "patch-ids.h"

7 8 9 10 11 12
static int patch_id_defined(struct commit *commit)
{
	/* must be 0 or 1 parents */
	return !commit->parents || !commit->parents->next;
}

13
int commit_patch_id(struct commit *commit, struct diff_options *options,
14
		    struct object_id *oid, int diff_header_only)
15
{
16 17 18
	if (!patch_id_defined(commit))
		return -1;

19
	if (commit->parents)
20 21
		diff_tree_oid(&commit->parents->item->object.oid,
			      &commit->object.oid, "", options);
22
	else
23
		diff_root_tree_oid(&commit->object.oid, "", options);
24
	diffcore_std(options);
25
	return diff_flush_patch_id(options, oid, diff_header_only);
26 27
}

28 29 30 31 32 33 34 35 36 37
/*
 * When we cannot load the full patch-id for both commits for whatever
 * reason, the function returns -1 (i.e. return error(...)). Despite
 * the "cmp" in the name of this function, the caller only cares about
 * the return value being zero (a and b are equivalent) or non-zero (a
 * and b are different), and returning non-zero would keep both in the
 * result, even if they actually were equivalent, in order to err on
 * the side of safety.  The actual value being negative does not have
 * any significance; only that it is non-zero matters.
 */
38 39 40
static int patch_id_cmp(const void *cmpfn_data,
			const void *entry,
			const void *entry_or_key,
41
			const void *unused_keydata)
42
{
43 44 45 46 47
	/* NEEDSWORK: const correctness? */
	struct diff_options *opt = (void *)cmpfn_data;
	struct patch_id *a = (void *)entry;
	struct patch_id *b = (void *)entry_or_key;

48 49
	if (is_null_oid(&a->patch_id) &&
	    commit_patch_id(a->commit, opt, &a->patch_id, 0))
50 51
		return error("Could not get patch ID for %s",
			oid_to_hex(&a->commit->object.oid));
52 53
	if (is_null_oid(&b->patch_id) &&
	    commit_patch_id(b->commit, opt, &b->patch_id, 0))
54 55
		return error("Could not get patch ID for %s",
			oid_to_hex(&b->commit->object.oid));
56
	return oidcmp(&a->patch_id, &b->patch_id);
57 58 59 60 61 62
}

int init_patch_ids(struct patch_ids *ids)
{
	memset(ids, 0, sizeof(*ids));
	diff_setup(&ids->diffopts);
63
	ids->diffopts.detect_rename = 0;
64
	ids->diffopts.flags.recursive = 1;
Thomas Rast's avatar
Thomas Rast committed
65
	diff_setup_done(&ids->diffopts);
66
	hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts, 256);
67 68 69 70 71
	return 0;
}

int free_patch_ids(struct patch_ids *ids)
{
72
	hashmap_free(&ids->patches, 1);
73 74 75
	return 0;
}

76 77 78
static int init_patch_id_entry(struct patch_id *patch,
			       struct commit *commit,
			       struct patch_ids *ids)
79
{
80
	struct object_id header_only_patch_id;
81

82
	patch->commit = commit;
83
	if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
84
		return -1;
85

86
	hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));
87
	return 0;
88 89 90 91 92
}

struct patch_id *has_commit_patch_id(struct commit *commit,
				     struct patch_ids *ids)
{
93 94
	struct patch_id patch;

95 96 97
	if (!patch_id_defined(commit))
		return NULL;

98 99 100 101
	memset(&patch, 0, sizeof(patch));
	if (init_patch_id_entry(&patch, commit, ids))
		return NULL;

102
	return hashmap_get(&ids->patches, &patch, NULL);
103 104 105 106 107
}

struct patch_id *add_commit_patch_id(struct commit *commit,
				     struct patch_ids *ids)
{
108
	struct patch_id *key;
109

110 111 112
	if (!patch_id_defined(commit))
		return NULL;

113
	key = xcalloc(1, sizeof(*key));
114 115 116 117 118 119 120
	if (init_patch_id_entry(key, commit, ids)) {
		free(key);
		return NULL;
	}

	hashmap_add(&ids->patches, key);
	return key;
121
}