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