1#include"cache.h" 2#include"diff.h" 3#include"commit.h" 4#include"patch-ids.h" 5 6static intcommit_patch_id(struct commit *commit,struct diff_options *options, 7unsigned char*sha1) 8{ 9if(commit->parents) 10diff_tree_sha1(commit->parents->item->object.sha1, 11 commit->object.sha1,"", options); 12else 13diff_root_tree_sha1(commit->object.sha1,"", options); 14diffcore_std(options); 15returndiff_flush_patch_id(options, sha1); 16} 17 18static uint32_ttake2(const unsigned char*id) 19{ 20return((id[0] <<8) | id[1]); 21} 22 23/* 24 * Conventional binary search loop looks like this: 25 * 26 * do { 27 * int mi = (lo + hi) / 2; 28 * int cmp = "entry pointed at by mi" minus "target"; 29 * if (!cmp) 30 * return (mi is the wanted one) 31 * if (cmp > 0) 32 * hi = mi; "mi is larger than target" 33 * else 34 * lo = mi+1; "mi is smaller than target" 35 * } while (lo < hi); 36 * 37 * The invariants are: 38 * 39 * - When entering the loop, lo points at a slot that is never 40 * above the target (it could be at the target), hi points at a 41 * slot that is guaranteed to be above the target (it can never 42 * be at the target). 43 * 44 * - We find a point 'mi' between lo and hi (mi could be the same 45 * as lo, but never can be the same as hi), and check if it hits 46 * the target. There are three cases: 47 * 48 * - if it is a hit, we are happy. 49 * 50 * - if it is strictly higher than the target, we update hi with 51 * it. 52 * 53 * - if it is strictly lower than the target, we update lo to be 54 * one slot after it, because we allow lo to be at the target. 55 * 56 * When choosing 'mi', we do not have to take the "middle" but 57 * anywhere in between lo and hi, as long as lo <= mi < hi is 58 * satisfied. When we somehow know that the distance between the 59 * target and lo is much shorter than the target and hi, we could 60 * pick mi that is much closer to lo than the midway. 61 */ 62static intpatch_pos(struct patch_id **table,int nr,const unsigned char*id) 63{ 64int hi = nr; 65int lo =0; 66int mi =0; 67 68if(!nr) 69return-1; 70 71if(nr !=1) { 72unsigned lov, hiv, miv, ofs; 73 74for(ofs =0; ofs <18; ofs +=2) { 75 lov =take2(table[0]->patch_id + ofs); 76 hiv =take2(table[nr-1]->patch_id + ofs); 77 miv =take2(id + ofs); 78if(miv < lov) 79return-1; 80if(hiv < miv) 81return-1- nr; 82if(lov != hiv) { 83/* 84 * At this point miv could be equal 85 * to hiv (but id could still be higher); 86 * the invariant of (mi < hi) should be 87 * kept. 88 */ 89 mi = (nr-1) * (miv - lov) / (hiv - lov); 90if(lo <= mi && mi < hi) 91break; 92die("oops"); 93} 94} 95if(18<= ofs) 96die("cannot happen -- lo and hi are identical"); 97} 98 99do{ 100int cmp; 101 cmp =hashcmp(table[mi]->patch_id, id); 102if(!cmp) 103return mi; 104if(cmp >0) 105 hi = mi; 106else 107 lo = mi +1; 108 mi = (hi + lo) /2; 109}while(lo < hi); 110return-lo-1; 111} 112 113#define BUCKET_SIZE 190/* 190 * 21 = 3990, with slop close enough to 4K */ 114struct patch_id_bucket { 115struct patch_id_bucket *next; 116int nr; 117struct patch_id bucket[BUCKET_SIZE]; 118}; 119 120intinit_patch_ids(struct patch_ids *ids) 121{ 122memset(ids,0,sizeof(*ids)); 123diff_setup(&ids->diffopts); 124 ids->diffopts.recursive =1; 125if(diff_setup_done(&ids->diffopts) <0) 126returnerror("diff_setup_done failed"); 127return0; 128} 129 130intfree_patch_ids(struct patch_ids *ids) 131{ 132struct patch_id_bucket *next, *patches; 133 134free(ids->table); 135for(patches = ids->patches; patches; patches = next) { 136 next = patches->next; 137free(patches); 138} 139return0; 140} 141 142static struct patch_id *add_commit(struct commit *commit, 143struct patch_ids *ids, 144int no_add) 145{ 146struct patch_id_bucket *bucket; 147struct patch_id *ent; 148unsigned char sha1[20]; 149int pos; 150 151if(commit_patch_id(commit, &ids->diffopts, sha1)) 152return NULL; 153 pos =patch_pos(ids->table, ids->nr, sha1); 154if(0<= pos) 155return ids->table[pos]; 156if(no_add) 157return NULL; 158 159 pos = -1- pos; 160 161 bucket = ids->patches; 162if(!bucket || (BUCKET_SIZE <= bucket->nr)) { 163 bucket =xcalloc(1,sizeof(*bucket)); 164 bucket->next = ids->patches; 165 ids->patches = bucket; 166} 167 ent = &bucket->bucket[bucket->nr++]; 168hashcpy(ent->patch_id, sha1); 169 170if(ids->alloc <= ids->nr) { 171 ids->alloc =alloc_nr(ids->nr); 172 ids->table =xrealloc(ids->table,sizeof(ent) * ids->alloc); 173} 174if(pos < ids->nr) 175memmove(ids->table + pos +1, ids->table + pos, 176sizeof(ent) * (ids->nr - pos)); 177 ids->nr++; 178 ids->table[pos] = ent; 179return ids->table[pos]; 180} 181 182struct patch_id *has_commit_patch_id(struct commit *commit, 183struct patch_ids *ids) 184{ 185returnadd_commit(commit, ids,1); 186} 187 188struct patch_id *add_commit_patch_id(struct commit *commit, 189struct patch_ids *ids) 190{ 191returnadd_commit(commit, ids,0); 192}