1/* 2 * Copyright (C) 2005 Junio C Hamano 3 */ 4#include"cache.h" 5#include"diff.h" 6#include"diffcore.h" 7#include"delta.h" 8#include"count-delta.h" 9 10/* Table of rename/copy destinations */ 11 12static struct diff_rename_dst { 13struct diff_filespec *two; 14struct diff_filepair *pair; 15} *rename_dst; 16static int rename_dst_nr, rename_dst_alloc; 17 18static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two, 19int insert_ok) 20{ 21int first, last; 22 23 first =0; 24 last = rename_dst_nr; 25while(last > first) { 26int next = (last + first) >>1; 27struct diff_rename_dst *dst = &(rename_dst[next]); 28int cmp =strcmp(two->path, dst->two->path); 29if(!cmp) 30return dst; 31if(cmp <0) { 32 last = next; 33continue; 34} 35 first = next+1; 36} 37/* not found */ 38if(!insert_ok) 39return NULL; 40/* insert to make it at "first" */ 41if(rename_dst_alloc <= rename_dst_nr) { 42 rename_dst_alloc =alloc_nr(rename_dst_alloc); 43 rename_dst =xrealloc(rename_dst, 44 rename_dst_alloc *sizeof(*rename_dst)); 45} 46 rename_dst_nr++; 47if(first < rename_dst_nr) 48memmove(rename_dst + first +1, rename_dst + first, 49(rename_dst_nr - first -1) *sizeof(*rename_dst)); 50 rename_dst[first].two =alloc_filespec(two->path); 51fill_filespec(rename_dst[first].two, two->sha1, two->mode); 52 rename_dst[first].pair = NULL; 53return&(rename_dst[first]); 54} 55 56/* Table of rename/copy src files */ 57static struct diff_rename_src { 58struct diff_filespec *one; 59unsigned src_path_left :1; 60} *rename_src; 61static int rename_src_nr, rename_src_alloc; 62 63static struct diff_rename_src *register_rename_src(struct diff_filespec *one, 64int src_path_left) 65{ 66int first, last; 67 68 first =0; 69 last = rename_src_nr; 70while(last > first) { 71int next = (last + first) >>1; 72struct diff_rename_src *src = &(rename_src[next]); 73int cmp =strcmp(one->path, src->one->path); 74if(!cmp) 75return src; 76if(cmp <0) { 77 last = next; 78continue; 79} 80 first = next+1; 81} 82 83/* insert to make it at "first" */ 84if(rename_src_alloc <= rename_src_nr) { 85 rename_src_alloc =alloc_nr(rename_src_alloc); 86 rename_src =xrealloc(rename_src, 87 rename_src_alloc *sizeof(*rename_src)); 88} 89 rename_src_nr++; 90if(first < rename_src_nr) 91memmove(rename_src + first +1, rename_src + first, 92(rename_src_nr - first -1) *sizeof(*rename_src)); 93 rename_src[first].one = one; 94 rename_src[first].src_path_left = src_path_left; 95return&(rename_src[first]); 96} 97 98static intis_exact_match(struct diff_filespec *src,struct diff_filespec *dst) 99{ 100if(src->sha1_valid && dst->sha1_valid && 101!memcmp(src->sha1, dst->sha1,20)) 102return1; 103if(diff_populate_filespec(src,1) ||diff_populate_filespec(dst,1)) 104return0; 105if(src->size != dst->size) 106return0; 107if(diff_populate_filespec(src,0) ||diff_populate_filespec(dst,0)) 108return0; 109if(src->size == dst->size && 110!memcmp(src->data, dst->data, src->size)) 111return1; 112return0; 113} 114 115struct diff_score { 116int src;/* index in rename_src */ 117int dst;/* index in rename_dst */ 118int score; 119}; 120 121static intestimate_similarity(struct diff_filespec *src, 122struct diff_filespec *dst, 123int minimum_score) 124{ 125/* src points at a file that existed in the original tree (or 126 * optionally a file in the destination tree) and dst points 127 * at a newly created file. They may be quite similar, in which 128 * case we want to say src is renamed to dst or src is copied into 129 * dst, and then some edit has been applied to dst. 130 * 131 * Compare them and return how similar they are, representing 132 * the score as an integer between 0 and MAX_SCORE. 133 * 134 * When there is an exact match, it is considered a better 135 * match than anything else; the destination does not even 136 * call into this function in that case. 137 */ 138void*delta; 139unsigned long delta_size, base_size, src_copied, literal_added; 140unsigned long delta_limit; 141int score; 142 143/* We deal only with regular files. Symlink renames are handled 144 * only when they are exact matches --- in other words, no edits 145 * after renaming. 146 */ 147if(!S_ISREG(src->mode) || !S_ISREG(dst->mode)) 148return0; 149 150 delta_size = ((src->size < dst->size) ? 151(dst->size - src->size) : (src->size - dst->size)); 152 base_size = ((src->size < dst->size) ? src->size : dst->size); 153 154/* We would not consider edits that change the file size so 155 * drastically. delta_size must be smaller than 156 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). 157 * 158 * Note that base_size == 0 case is handled here already 159 * and the final score computation below would not have a 160 * divide-by-zero issue. 161 */ 162if(base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) 163return0; 164 165if(diff_populate_filespec(src,0) ||diff_populate_filespec(dst,0)) 166return0;/* error but caught downstream */ 167 168 delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE; 169 delta =diff_delta(src->data, src->size, 170 dst->data, dst->size, 171&delta_size, delta_limit); 172if(!delta) 173/* If delta_limit is exceeded, we have too much differences */ 174return0; 175 176/* A delta that has a lot of literal additions would have 177 * big delta_size no matter what else it does. 178 */ 179if(base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) { 180free(delta); 181return0; 182} 183 184/* Estimate the edit size by interpreting delta. */ 185if(count_delta(delta, delta_size, &src_copied, &literal_added)) { 186free(delta); 187return0; 188} 189free(delta); 190 191/* Extent of damage */ 192if(src->size + literal_added < src_copied) 193 delta_size =0; 194else 195 delta_size = (src->size - src_copied) + literal_added; 196 197/* 198 * Now we will give some score to it. 100% edit gets 0 points 199 * and 0% edit gets MAX_SCORE points. 200 */ 201 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); 202if(score <0)return0; 203if(MAX_SCORE < score)return MAX_SCORE; 204return score; 205} 206 207static voidrecord_rename_pair(int dst_index,int src_index,int score) 208{ 209struct diff_filespec *one, *two, *src, *dst; 210struct diff_filepair *dp; 211 212if(rename_dst[dst_index].pair) 213die("internal error: dst already matched."); 214 215 src = rename_src[src_index].one; 216 one =alloc_filespec(src->path); 217fill_filespec(one, src->sha1, src->mode); 218 219 dst = rename_dst[dst_index].two; 220 two =alloc_filespec(dst->path); 221fill_filespec(two, dst->sha1, dst->mode); 222 223 dp =diff_queue(NULL, one, two); 224 dp->score = score; 225 dp->source_stays = rename_src[src_index].src_path_left; 226 rename_dst[dst_index].pair = dp; 227} 228 229/* 230 * We sort the rename similarity matrix with the score, in descending 231 * order (the most similar first). 232 */ 233static intscore_compare(const void*a_,const void*b_) 234{ 235const struct diff_score *a = a_, *b = b_; 236return b->score - a->score; 237} 238 239static intcompute_stays(struct diff_queue_struct *q, 240struct diff_filespec *one) 241{ 242int i; 243for(i =0; i < q->nr; i++) { 244struct diff_filepair *p = q->queue[i]; 245if(strcmp(one->path, p->two->path)) 246continue; 247if(DIFF_PAIR_RENAME(p)) { 248return0;/* something else is renamed into this */ 249} 250} 251return1; 252} 253 254voiddiffcore_rename(struct diff_options *options) 255{ 256int detect_rename = options->detect_rename; 257int minimum_score = options->rename_score; 258int rename_limit = options->rename_limit; 259struct diff_queue_struct *q = &diff_queued_diff; 260struct diff_queue_struct outq; 261struct diff_score *mx; 262int i, j, rename_count; 263int num_create, num_src, dst_cnt; 264 265if(!minimum_score) 266 minimum_score = DEFAULT_RENAME_SCORE; 267 rename_count =0; 268 269for(i =0; i < q->nr; i++) { 270struct diff_filepair *p = q->queue[i]; 271if(!DIFF_FILE_VALID(p->one)) 272if(!DIFF_FILE_VALID(p->two)) 273continue;/* unmerged */ 274else 275locate_rename_dst(p->two,1); 276else if(!DIFF_FILE_VALID(p->two)) { 277/* If the source is a broken "delete", and 278 * they did not really want to get broken, 279 * that means the source actually stays. 280 */ 281int stays = (p->broken_pair && !p->score); 282register_rename_src(p->one, stays); 283} 284else if(detect_rename == DIFF_DETECT_COPY) 285register_rename_src(p->one,1); 286} 287if(rename_dst_nr ==0|| rename_src_nr ==0|| 288(0< rename_limit && rename_limit < rename_dst_nr)) 289goto cleanup;/* nothing to do */ 290 291/* We really want to cull the candidates list early 292 * with cheap tests in order to avoid doing deltas. 293 */ 294for(i =0; i < rename_dst_nr; i++) { 295struct diff_filespec *two = rename_dst[i].two; 296for(j =0; j < rename_src_nr; j++) { 297struct diff_filespec *one = rename_src[j].one; 298if(!is_exact_match(one, two)) 299continue; 300record_rename_pair(i, j, MAX_SCORE); 301 rename_count++; 302break;/* we are done with this entry */ 303} 304} 305 306/* Have we run out the created file pool? If so we can avoid 307 * doing the delta matrix altogether. 308 */ 309if(rename_count == rename_dst_nr) 310goto cleanup; 311 312if(minimum_score == MAX_SCORE) 313goto cleanup; 314 315 num_create = (rename_dst_nr - rename_count); 316 num_src = rename_src_nr; 317 mx =xmalloc(sizeof(*mx) * num_create * num_src); 318for(dst_cnt = i =0; i < rename_dst_nr; i++) { 319int base = dst_cnt * num_src; 320struct diff_filespec *two = rename_dst[i].two; 321if(rename_dst[i].pair) 322continue;/* dealt with exact match already. */ 323for(j =0; j < rename_src_nr; j++) { 324struct diff_filespec *one = rename_src[j].one; 325struct diff_score *m = &mx[base+j]; 326 m->src = j; 327 m->dst = i; 328 m->score =estimate_similarity(one, two, 329 minimum_score); 330} 331 dst_cnt++; 332} 333/* cost matrix sorted by most to least similar pair */ 334qsort(mx, num_create * num_src,sizeof(*mx), score_compare); 335for(i =0; i < num_create * num_src; i++) { 336struct diff_rename_dst *dst = &rename_dst[mx[i].dst]; 337if(dst->pair) 338continue;/* already done, either exact or fuzzy. */ 339if(mx[i].score < minimum_score) 340break;/* there is no more usable pair. */ 341record_rename_pair(mx[i].dst, mx[i].src, mx[i].score); 342 rename_count++; 343} 344free(mx); 345 346 cleanup: 347/* At this point, we have found some renames and copies and they 348 * are recorded in rename_dst. The original list is still in *q. 349 */ 350 outq.queue = NULL; 351 outq.nr = outq.alloc =0; 352for(i =0; i < q->nr; i++) { 353struct diff_filepair *p = q->queue[i]; 354struct diff_filepair *pair_to_free = NULL; 355 356if(!DIFF_FILE_VALID(p->one) &&DIFF_FILE_VALID(p->two)) { 357/* 358 * Creation 359 * 360 * We would output this create record if it has 361 * not been turned into a rename/copy already. 362 */ 363struct diff_rename_dst *dst = 364locate_rename_dst(p->two,0); 365if(dst && dst->pair) { 366diff_q(&outq, dst->pair); 367 pair_to_free = p; 368} 369else 370/* no matching rename/copy source, so 371 * record this as a creation. 372 */ 373diff_q(&outq, p); 374} 375else if(DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) { 376/* 377 * Deletion 378 * 379 * We would output this delete record if: 380 * 381 * (1) this is a broken delete and the counterpart 382 * broken create remains in the output; or 383 * (2) this is not a broken delete, and rename_dst 384 * does not have a rename/copy to move p->one->path 385 * out of existence. 386 * 387 * Otherwise, the counterpart broken create 388 * has been turned into a rename-edit; or 389 * delete did not have a matching create to 390 * begin with. 391 */ 392if(DIFF_PAIR_BROKEN(p)) { 393/* broken delete */ 394struct diff_rename_dst *dst = 395locate_rename_dst(p->one,0); 396if(dst && dst->pair) 397/* counterpart is now rename/copy */ 398 pair_to_free = p; 399} 400else{ 401for(j =0; j < rename_dst_nr; j++) { 402if(!rename_dst[j].pair) 403continue; 404if(strcmp(rename_dst[j].pair-> 405 one->path, 406 p->one->path)) 407continue; 408break; 409} 410if(j < rename_dst_nr) 411/* this path remains */ 412 pair_to_free = p; 413} 414 415if(pair_to_free) 416; 417else 418diff_q(&outq, p); 419} 420else if(!diff_unmodified_pair(p)) 421/* all the usual ones need to be kept */ 422diff_q(&outq, p); 423else 424/* no need to keep unmodified pairs */ 425 pair_to_free = p; 426 427if(pair_to_free) 428diff_free_filepair(pair_to_free); 429} 430diff_debug_queue("done copying original", &outq); 431 432free(q->queue); 433*q = outq; 434diff_debug_queue("done collapsing", q); 435 436/* We need to see which rename source really stays here; 437 * earlier we only checked if the path is left in the result, 438 * but even if a path remains in the result, if that is coming 439 * from copying something else on top of it, then the original 440 * source is lost and does not stay. 441 */ 442for(i =0; i < q->nr; i++) { 443struct diff_filepair *p = q->queue[i]; 444if(DIFF_PAIR_RENAME(p) && p->source_stays) { 445/* If one appears as the target of a rename-copy, 446 * then mark p->source_stays = 0; otherwise 447 * leave it as is. 448 */ 449 p->source_stays =compute_stays(q, p->one); 450} 451} 452 453for(i =0; i < rename_dst_nr; i++) { 454diff_free_filespec_data(rename_dst[i].two); 455free(rename_dst[i].two); 456} 457 458free(rename_dst); 459 rename_dst = NULL; 460 rename_dst_nr = rename_dst_alloc =0; 461free(rename_src); 462 rename_src = NULL; 463 rename_src_nr = rename_src_alloc =0; 464return; 465}