1#include"cache.h" 2#include"diff.h" 3#include"commit.h" 4#include"sha1-lookup.h" 5#include"patch-ids.h" 6 7static intpatch_id_defined(struct commit *commit) 8{ 9/* must be 0 or 1 parents */ 10return!commit->parents || !commit->parents->next; 11} 12 13intcommit_patch_id(struct commit *commit,struct diff_options *options, 14struct object_id *oid,int diff_header_only) 15{ 16if(!patch_id_defined(commit)) 17return-1; 18 19if(commit->parents) 20diff_tree_oid(&commit->parents->item->object.oid, 21&commit->object.oid,"", options); 22else 23diff_root_tree_oid(&commit->object.oid,"", options); 24diffcore_std(options); 25returndiff_flush_patch_id(options, oid, diff_header_only); 26} 27 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 */ 38static intpatch_id_cmp(const void*cmpfn_data, 39const void*entry, 40const void*entry_or_key, 41const void*unused_keydata) 42{ 43/* NEEDSWORK: const correctness? */ 44struct diff_options *opt = (void*)cmpfn_data; 45struct patch_id *a = (void*)entry; 46struct patch_id *b = (void*)entry_or_key; 47 48if(is_null_oid(&a->patch_id) && 49commit_patch_id(a->commit, opt, &a->patch_id,0)) 50returnerror("Could not get patch ID for%s", 51oid_to_hex(&a->commit->object.oid)); 52if(is_null_oid(&b->patch_id) && 53commit_patch_id(b->commit, opt, &b->patch_id,0)) 54returnerror("Could not get patch ID for%s", 55oid_to_hex(&b->commit->object.oid)); 56returnoidcmp(&a->patch_id, &b->patch_id); 57} 58 59intinit_patch_ids(struct patch_ids *ids) 60{ 61memset(ids,0,sizeof(*ids)); 62diff_setup(&ids->diffopts); 63 ids->diffopts.detect_rename =0; 64DIFF_OPT_SET(&ids->diffopts, RECURSIVE); 65diff_setup_done(&ids->diffopts); 66hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts,256); 67return0; 68} 69 70intfree_patch_ids(struct patch_ids *ids) 71{ 72hashmap_free(&ids->patches,1); 73return0; 74} 75 76static intinit_patch_id_entry(struct patch_id *patch, 77struct commit *commit, 78struct patch_ids *ids) 79{ 80struct object_id header_only_patch_id; 81 82 patch->commit = commit; 83if(commit_patch_id(commit, &ids->diffopts, &header_only_patch_id,1)) 84return-1; 85 86hashmap_entry_init(patch,sha1hash(header_only_patch_id.hash)); 87return0; 88} 89 90struct patch_id *has_commit_patch_id(struct commit *commit, 91struct patch_ids *ids) 92{ 93struct patch_id patch; 94 95if(!patch_id_defined(commit)) 96return NULL; 97 98memset(&patch,0,sizeof(patch)); 99if(init_patch_id_entry(&patch, commit, ids)) 100return NULL; 101 102returnhashmap_get(&ids->patches, &patch, NULL); 103} 104 105struct patch_id *add_commit_patch_id(struct commit *commit, 106struct patch_ids *ids) 107{ 108struct patch_id *key; 109 110if(!patch_id_defined(commit)) 111return NULL; 112 113 key =xcalloc(1,sizeof(*key)); 114if(init_patch_id_entry(key, commit, ids)) { 115free(key); 116return NULL; 117} 118 119hashmap_add(&ids->patches, key); 120return key; 121}