1#include"cache.h" 2#include"diff.h" 3#include"commit.h" 4#include"sha1-lookup.h" 5#include"patch-ids.h" 6 7intcommit_patch_id(struct commit *commit,struct diff_options *options, 8unsigned char*sha1,int diff_header_only) 9{ 10if(commit->parents) 11diff_tree_sha1(commit->parents->item->object.oid.hash, 12 commit->object.oid.hash,"", options); 13else 14diff_root_tree_sha1(commit->object.oid.hash,"", options); 15diffcore_std(options); 16returndiff_flush_patch_id(options, sha1, diff_header_only); 17} 18 19/* 20 * When we cannot load the full patch-id for both commits for whatever 21 * reason, the function returns -1 (i.e. return error(...)). Despite 22 * the "cmp" in the name of this function, the caller only cares about 23 * the return value being zero (a and b are equivalent) or non-zero (a 24 * and b are different), and returning non-zero would keep both in the 25 * result, even if they actually were equivalent, in order to err on 26 * the side of safety. The actual value being negative does not have 27 * any significance; only that it is non-zero matters. 28 */ 29static intpatch_id_cmp(struct patch_id *a, 30struct patch_id *b, 31struct diff_options *opt) 32{ 33if(is_null_sha1(a->patch_id) && 34commit_patch_id(a->commit, opt, a->patch_id,0)) 35returnerror("Could not get patch ID for%s", 36oid_to_hex(&a->commit->object.oid)); 37if(is_null_sha1(b->patch_id) && 38commit_patch_id(b->commit, opt, b->patch_id,0)) 39returnerror("Could not get patch ID for%s", 40oid_to_hex(&b->commit->object.oid)); 41returnhashcmp(a->patch_id, b->patch_id); 42} 43 44intinit_patch_ids(struct patch_ids *ids) 45{ 46memset(ids,0,sizeof(*ids)); 47diff_setup(&ids->diffopts); 48 ids->diffopts.detect_rename =0; 49DIFF_OPT_SET(&ids->diffopts, RECURSIVE); 50diff_setup_done(&ids->diffopts); 51hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,256); 52return0; 53} 54 55intfree_patch_ids(struct patch_ids *ids) 56{ 57hashmap_free(&ids->patches,1); 58return0; 59} 60 61static intinit_patch_id_entry(struct patch_id *patch, 62struct commit *commit, 63struct patch_ids *ids) 64{ 65unsigned char header_only_patch_id[GIT_SHA1_RAWSZ]; 66 67 patch->commit = commit; 68if(commit_patch_id(commit, &ids->diffopts, header_only_patch_id,1)) 69return-1; 70 71hashmap_entry_init(patch,sha1hash(header_only_patch_id)); 72return0; 73} 74 75struct patch_id *has_commit_patch_id(struct commit *commit, 76struct patch_ids *ids) 77{ 78struct patch_id patch; 79 80memset(&patch,0,sizeof(patch)); 81if(init_patch_id_entry(&patch, commit, ids)) 82return NULL; 83 84returnhashmap_get(&ids->patches, &patch, &ids->diffopts); 85} 86 87struct patch_id *add_commit_patch_id(struct commit *commit, 88struct patch_ids *ids) 89{ 90struct patch_id *key =xcalloc(1,sizeof(*key)); 91 92if(init_patch_id_entry(key, commit, ids)) { 93free(key); 94return NULL; 95} 96 97hashmap_add(&ids->patches, key); 98return key; 99}