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 9struct diff_rename_pool { 10 struct diff_filespec **s; 11 int nr, alloc; 12}; 13 14static void diff_rename_pool_clear(struct diff_rename_pool *pool) 15{ 16 pool->s = NULL; pool->nr = pool->alloc = 0; 17} 18 19static void diff_rename_pool_add(struct diff_rename_pool *pool, 20 struct diff_filespec *s) 21{ 22 if (S_ISDIR(s->mode)) 23 return; /* no trees, please */ 24 25 if (pool->alloc <= pool->nr) { 26 pool->alloc = alloc_nr(pool->alloc); 27 pool->s = xrealloc(pool->s, 28 sizeof(*(pool->s)) * pool->alloc); 29 } 30 pool->s[pool->nr] = s; 31 pool->nr++; 32} 33 34static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst) 35{ 36 if (src->sha1_valid && dst->sha1_valid && 37 !memcmp(src->sha1, dst->sha1, 20)) 38 return 1; 39 if (diff_populate_filespec(src) || diff_populate_filespec(dst)) 40 /* this is an error but will be caught downstream */ 41 return 0; 42 if (src->size == dst->size && 43 !memcmp(src->data, dst->data, src->size)) 44 return 1; 45 return 0; 46} 47 48struct diff_score { 49 struct diff_filespec *src; 50 struct diff_filespec *dst; 51 int score; 52 int rank; 53}; 54 55static int estimate_similarity(struct diff_filespec *src, 56 struct diff_filespec *dst, 57 int minimum_score) 58{ 59 /* src points at a file that existed in the original tree (or 60 * optionally a file in the destination tree) and dst points 61 * at a newly created file. They may be quite similar, in which 62 * case we want to say src is renamed to dst or src is copied into 63 * dst, and then some edit has been applied to dst. 64 * 65 * Compare them and return how similar they are, representing 66 * the score as an integer between 0 and 10000, except 67 * where they match exactly it is considered better than anything 68 * else. 69 */ 70 void *delta; 71 unsigned long delta_size, base_size; 72 int score; 73 74 /* We deal only with regular files. Symlink renames are handled 75 * only when they are exact matches --- in other words, no edits 76 * after renaming. 77 */ 78 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) 79 return 0; 80 81 delta_size = ((src->size < dst->size) ? 82 (dst->size - src->size) : (src->size - dst->size)); 83 base_size = ((src->size < dst->size) ? src->size : dst->size); 84 85 /* We would not consider edits that change the file size so 86 * drastically. delta_size must be smaller than 87 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). 88 * Note that base_size == 0 case is handled here already 89 * and the final score computation below would not have a 90 * divide-by-zero issue. 91 */ 92 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) 93 return 0; 94 95 delta = diff_delta(src->data, src->size, 96 dst->data, dst->size, 97 &delta_size); 98 /* 99 * We currently punt here, but we may later end up parsing the 100 * delta to really assess the extent of damage. A big consecutive 101 * remove would produce small delta_size that affects quite a 102 * big portion of the file. 103 */ 104 free(delta); 105 106 /* 107 * Now we will give some score to it. 100% edit gets 0 points 108 * and 0% edit gets MAX_SCORE points. 109 */ 110 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); 111 if (score < 0) return 0; 112 if (MAX_SCORE < score) return MAX_SCORE; 113 return score; 114} 115 116static void record_rename_pair(struct diff_queue_struct *outq, 117 struct diff_filespec *src, 118 struct diff_filespec *dst, 119 int rank, 120 int score) 121{ 122 /* The rank is used to sort the final output, because there 123 * are certain dependencies. 124 * 125 * - rank #0 depends on deleted ones. 126 * - rank #1 depends on kept files before they are modified. 127 * - rank #2 depends on kept files after they are modified; 128 * currently not used. 129 * 130 * Therefore, the final output order should be: 131 * 132 * 1. rank #0 rename/copy diffs. 133 * 2. deletions in the original. 134 * 3. rank #1 rename/copy diffs. 135 * 4. additions and modifications in the original. 136 * 5. rank #2 rename/copy diffs; currently not used. 137 * 138 * To achieve this sort order, we give xform_work the number 139 * above. 140 */ 141 struct diff_filepair *dp = diff_queue(outq, src, dst); 142 dp->xfrm_work = (rank * 2 + 1) | (score<<RENAME_SCORE_SHIFT); 143 dst->xfrm_flags |= RENAME_DST_MATCHED; 144} 145 146#if 0 147static void debug_filespec(struct diff_filespec *s, int x, const char *one) 148{ 149 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", 150 x, one, 151 s->path, 152 DIFF_FILE_VALID(s) ? "valid" : "invalid", 153 s->mode, 154 s->sha1_valid ? sha1_to_hex(s->sha1) : ""); 155 fprintf(stderr, "queue[%d] %s size %lu flags %d\n", 156 x, one, 157 s->size, s->xfrm_flags); 158} 159 160static void debug_filepair(const struct diff_filepair *p, int i) 161{ 162 debug_filespec(p->one, i, "one"); 163 debug_filespec(p->two, i, "two"); 164 fprintf(stderr, "pair flags %d, orig order %d, score %d\n", 165 (p->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1)), 166 p->orig_order, 167 (p->xfrm_work >> RENAME_SCORE_SHIFT)); 168} 169 170static void debug_queue(const char *msg, struct diff_queue_struct *q) 171{ 172 int i; 173 if (msg) 174 fprintf(stderr, "%s\n", msg); 175 fprintf(stderr, "q->nr = %d\n", q->nr); 176 for (i = 0; i < q->nr; i++) { 177 struct diff_filepair *p = q->queue[i]; 178 debug_filepair(p, i); 179 } 180} 181#else 182#define debug_queue(a,b) do { ; /*nothing*/ } while(0) 183#endif 184 185/* 186 * We sort the outstanding diff entries according to the rank (see 187 * comment at the beginning of record_rename_pair) and tiebreak with 188 * the order in the original input. 189 */ 190static int rank_compare(const void *a_, const void *b_) 191{ 192 const struct diff_filepair *a = *(const struct diff_filepair **)a_; 193 const struct diff_filepair *b = *(const struct diff_filepair **)b_; 194 int a_rank = a->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1); 195 int b_rank = b->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1); 196 197 if (a_rank != b_rank) 198 return a_rank - b_rank; 199 return a->orig_order - b->orig_order; 200} 201 202/* 203 * We sort the rename similarity matrix with the score, in descending 204 * order (more similar first). 205 */ 206static int score_compare(const void *a_, const void *b_) 207{ 208 const struct diff_score *a = a_, *b = b_; 209 return b->score - a->score; 210} 211 212static int needs_to_stay(struct diff_queue_struct *q, int i, 213 struct diff_filespec *it) 214{ 215 /* If it will be used in later entry (either stay or used 216 * as the source of rename/copy), we need to copy, not rename. 217 */ 218 while (i < q->nr) { 219 struct diff_filepair *p = q->queue[i++]; 220 if (!DIFF_FILE_VALID(p->two)) 221 continue; /* removed is fine */ 222 if (strcmp(p->one->path, it->path)) 223 continue; /* not relevant */ 224 225 /* p has its src set to *it and it is not a delete; 226 * it will be used for in-place change or rename/copy, 227 * so we cannot rename it out. 228 */ 229 return 1; 230 } 231 return 0; 232} 233 234int diff_scoreopt_parse(const char *opt) 235{ 236 int diglen, num, scale, i; 237 if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C')) 238 return -1; /* that is not a -M nor -C option */ 239 diglen = strspn(opt+2, "0123456789"); 240 if (diglen == 0 || strlen(opt+2) != diglen) 241 return 0; /* use default */ 242 sscanf(opt+2, "%d", &num); 243 for (i = 0, scale = 1; i < diglen; i++) 244 scale *= 10; 245 246 /* user says num divided by scale and we say internally that 247 * is MAX_SCORE * num / scale. 248 */ 249 return MAX_SCORE * num / scale; 250} 251 252void diffcore_rename(int detect_rename, int minimum_score) 253{ 254 struct diff_queue_struct *q = &diff_queued_diff; 255 struct diff_queue_struct outq; 256 struct diff_rename_pool created, deleted, stay; 257 struct diff_rename_pool *(srcs[2]); 258 struct diff_score *mx; 259 int h, i, j; 260 int num_create, num_src, dst_cnt, src_cnt; 261 262 if (!minimum_score) 263 minimum_score = DEFAULT_MINIMUM_SCORE; 264 outq.queue = NULL; 265 outq.nr = outq.alloc = 0; 266 267 diff_rename_pool_clear(&created); 268 diff_rename_pool_clear(&deleted); 269 diff_rename_pool_clear(&stay); 270 271 srcs[0] = &deleted; 272 srcs[1] = &stay; 273 274 for (i = 0; i < q->nr; i++) { 275 struct diff_filepair *p = q->queue[i]; 276 if (!DIFF_FILE_VALID(p->one)) 277 if (!DIFF_FILE_VALID(p->two)) 278 continue; /* unmerged */ 279 else 280 diff_rename_pool_add(&created, p->two); 281 else if (!DIFF_FILE_VALID(p->two)) 282 diff_rename_pool_add(&deleted, p->one); 283 else if (1 < detect_rename) /* find copy, too */ 284 diff_rename_pool_add(&stay, p->one); 285 } 286 if (created.nr == 0) 287 goto cleanup; /* nothing to do */ 288 289 /* We really want to cull the candidates list early 290 * with cheap tests in order to avoid doing deltas. 291 * 292 * With the current callers, we should not have already 293 * matched entries at this point, but it is nonetheless 294 * checked for sanity. 295 */ 296 for (i = 0; i < created.nr; i++) { 297 if (created.s[i]->xfrm_flags & RENAME_DST_MATCHED) 298 continue; /* we have matched exactly already */ 299 for (h = 0; h < sizeof(srcs)/sizeof(srcs[0]); h++) { 300 struct diff_rename_pool *p = srcs[h]; 301 for (j = 0; j < p->nr; j++) { 302 if (!is_exact_match(p->s[j], created.s[i])) 303 continue; 304 record_rename_pair(&outq, 305 p->s[j], created.s[i], h, 306 MAX_SCORE); 307 break; /* we are done with this entry */ 308 } 309 } 310 } 311 debug_queue("done detecting exact", &outq); 312 313 /* Have we run out the created file pool? If so we can avoid 314 * doing the delta matrix altogether. 315 */ 316 if (outq.nr == created.nr) 317 goto flush_rest; 318 319 num_create = (created.nr - outq.nr); 320 num_src = deleted.nr + stay.nr; 321 mx = xmalloc(sizeof(*mx) * num_create * num_src); 322 for (dst_cnt = i = 0; i < created.nr; i++) { 323 int base = dst_cnt * num_src; 324 if (created.s[i]->xfrm_flags & RENAME_DST_MATCHED) 325 continue; /* dealt with exact match already. */ 326 for (src_cnt = h = 0; h < sizeof(srcs)/sizeof(srcs[0]); h++) { 327 struct diff_rename_pool *p = srcs[h]; 328 for (j = 0; j < p->nr; j++, src_cnt++) { 329 struct diff_score *m = &mx[base + src_cnt]; 330 m->src = p->s[j]; 331 m->dst = created.s[i]; 332 m->score = estimate_similarity(m->src, m->dst, 333 minimum_score); 334 m->rank = h; 335 } 336 } 337 dst_cnt++; 338 } 339 /* cost matrix sorted by most to least similar pair */ 340 qsort(mx, num_create * num_src, sizeof(*mx), score_compare); 341 for (i = 0; i < num_create * num_src; i++) { 342 if (mx[i].dst->xfrm_flags & RENAME_DST_MATCHED) 343 continue; /* alreayd done, either exact or fuzzy. */ 344 if (mx[i].score < minimum_score) 345 break; /* there is not any more diffs applicable. */ 346 record_rename_pair(&outq, 347 mx[i].src, mx[i].dst, mx[i].rank, 348 mx[i].score); 349 } 350 free(mx); 351 debug_queue("done detecting fuzzy", &outq); 352 353 flush_rest: 354 /* At this point, we have found some renames and copies and they 355 * are kept in outq. The original list is still in *q. 356 * 357 * Scan the original list and move them into the outq; we will sort 358 * outq and swap it into the queue supplied to pass that to 359 * downstream, so we assign the sort keys in this loop. 360 * 361 * See comments at the top of record_rename_pair for numbers used 362 * to assign xfrm_work. 363 * 364 * Note that we have not annotated the diff_filepair with any comment 365 * so there is nothing other than p to free. 366 */ 367 for (i = 0; i < q->nr; i++) { 368 struct diff_filepair *dp, *p = q->queue[i]; 369 if (!DIFF_FILE_VALID(p->one)) { 370 /* creation or unmerged entries */ 371 dp = diff_queue(&outq, p->one, p->two); 372 dp->xfrm_work = 4; 373 } 374 else if (!DIFF_FILE_VALID(p->two)) { 375 /* deletion */ 376 dp = diff_queue(&outq, p->one, p->two); 377 dp->xfrm_work = 2; 378 } 379 else { 380 /* modification, or stay as is */ 381 dp = diff_queue(&outq, p->one, p->two); 382 dp->xfrm_work = 4; 383 } 384 free(p); 385 } 386 debug_queue("done copying original", &outq); 387 388 /* Sort outq */ 389 qsort(outq.queue, outq.nr, sizeof(outq.queue[0]), rank_compare); 390 391 debug_queue("done sorting", &outq); 392 393 free(q->queue); 394 q->nr = q->alloc = 0; 395 q->queue = NULL; 396 397 /* Copy it out to q, removing duplicates. */ 398 for (i = 0; i < outq.nr; i++) { 399 struct diff_filepair *p = outq.queue[i]; 400 if (!DIFF_FILE_VALID(p->one)) { 401 /* created or unmerged */ 402 if (p->two->xfrm_flags & RENAME_DST_MATCHED) 403 ; /* rename/copy created it already */ 404 else 405 diff_queue(q, p->one, p->two); 406 } 407 else if (!DIFF_FILE_VALID(p->two)) { 408 /* deleted */ 409 if (p->one->xfrm_flags & RENAME_SRC_GONE) 410 ; /* rename/copy deleted it already */ 411 else 412 diff_queue(q, p->one, p->two); 413 } 414 else if (strcmp(p->one->path, p->two->path)) { 415 /* rename or copy */ 416 struct diff_filepair *dp = 417 diff_queue(q, p->one, p->two); 418 int msglen = (strlen(p->one->path) + 419 strlen(p->two->path) + 100); 420 int score = (p->xfrm_work >> RENAME_SCORE_SHIFT); 421 dp->xfrm_msg = xmalloc(msglen); 422 423 /* if we have a later entry that is a rename/copy 424 * that depends on p->one, then we copy here. 425 * otherwise we rename it. 426 */ 427 if (needs_to_stay(&outq, i+1, p->one)) { 428 /* copy it */ 429 sprintf(dp->xfrm_msg, 430 "similarity index %d%%\n" 431 "copy from %s\n" 432 "copy to %s\n", 433 (int)(0.5 + score * 100 / MAX_SCORE), 434 p->one->path, p->two->path); 435 } 436 else { 437 /* rename it, and mark it as gone. */ 438 p->one->xfrm_flags |= RENAME_SRC_GONE; 439 sprintf(dp->xfrm_msg, 440 "similarity index %d%%\n" 441 "rename old %s\n" 442 "rename new %s\n", 443 (int)(0.5 + score * 100 / MAX_SCORE), 444 p->one->path, p->two->path); 445 } 446 } 447 else 448 /* otherwise it is a modified (or stayed) entry */ 449 diff_queue(q, p->one, p->two); 450 diff_free_filepair(p); 451 } 452 453 free(outq.queue); 454 debug_queue("done collapsing", q); 455 456 cleanup: 457 free(created.s); 458 free(deleted.s); 459 free(stay.s); 460 return; 461}