Merge branch 'dk/blame-janitorial'
[git/mingw.git] / diffcore-rename.c
blob9b4f068eb390d9b9d53bdd91f8983432f6ec4563
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "hashmap.h"
8 #include "progress.h"
10 /* Table of rename/copy destinations */
12 static struct diff_rename_dst {
13 struct diff_filespec *two;
14 struct diff_filepair *pair;
15 } *rename_dst;
16 static int rename_dst_nr, rename_dst_alloc;
18 static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19 int insert_ok)
21 int first, last;
23 first = 0;
24 last = rename_dst_nr;
25 while (last > first) {
26 int next = (last + first) >> 1;
27 struct diff_rename_dst *dst = &(rename_dst[next]);
28 int cmp = strcmp(two->path, dst->two->path);
29 if (!cmp)
30 return dst;
31 if (cmp < 0) {
32 last = next;
33 continue;
35 first = next+1;
37 /* not found */
38 if (!insert_ok)
39 return NULL;
40 /* insert to make it at "first" */
41 if (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));
46 rename_dst_nr++;
47 if (first < rename_dst_nr)
48 memmove(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);
51 fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
52 rename_dst[first].pair = NULL;
53 return &(rename_dst[first]);
56 /* Table of rename/copy src files */
57 static struct diff_rename_src {
58 struct diff_filepair *p;
59 unsigned short score; /* to remember the break score */
60 } *rename_src;
61 static int rename_src_nr, rename_src_alloc;
63 static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
65 int first, last;
66 struct diff_filespec *one = p->one;
67 unsigned short score = p->score;
69 first = 0;
70 last = rename_src_nr;
71 while (last > first) {
72 int next = (last + first) >> 1;
73 struct diff_rename_src *src = &(rename_src[next]);
74 int cmp = strcmp(one->path, src->p->one->path);
75 if (!cmp)
76 return src;
77 if (cmp < 0) {
78 last = next;
79 continue;
81 first = next+1;
84 /* insert to make it at "first" */
85 if (rename_src_alloc <= rename_src_nr) {
86 rename_src_alloc = alloc_nr(rename_src_alloc);
87 rename_src = xrealloc(rename_src,
88 rename_src_alloc * sizeof(*rename_src));
90 rename_src_nr++;
91 if (first < rename_src_nr)
92 memmove(rename_src + first + 1, rename_src + first,
93 (rename_src_nr - first - 1) * sizeof(*rename_src));
94 rename_src[first].p = p;
95 rename_src[first].score = score;
96 return &(rename_src[first]);
99 static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
101 int src_len = strlen(src->path), dst_len = strlen(dst->path);
102 while (src_len && dst_len) {
103 char c1 = src->path[--src_len];
104 char c2 = dst->path[--dst_len];
105 if (c1 != c2)
106 return 0;
107 if (c1 == '/')
108 return 1;
110 return (!src_len || src->path[src_len - 1] == '/') &&
111 (!dst_len || dst->path[dst_len - 1] == '/');
114 struct diff_score {
115 int src; /* index in rename_src */
116 int dst; /* index in rename_dst */
117 unsigned short score;
118 short name_score;
121 static int estimate_similarity(struct diff_filespec *src,
122 struct diff_filespec *dst,
123 int minimum_score)
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.
131 * Compare them and return how similar they are, representing
132 * the score as an integer between 0 and MAX_SCORE.
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.
138 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
139 unsigned long delta_limit;
140 int score;
142 /* We deal only with regular files. Symlink renames are handled
143 * only when they are exact matches --- in other words, no edits
144 * after renaming.
146 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
147 return 0;
150 * Need to check that source and destination sizes are
151 * filled in before comparing them.
153 * If we already have "cnt_data" filled in, we know it's
154 * all good (avoid checking the size for zero, as that
155 * is a possible size - we really should have a flag to
156 * say whether the size is valid or not!)
158 if (!src->cnt_data && diff_populate_filespec(src, 1))
159 return 0;
160 if (!dst->cnt_data && diff_populate_filespec(dst, 1))
161 return 0;
163 max_size = ((src->size > dst->size) ? src->size : dst->size);
164 base_size = ((src->size < dst->size) ? src->size : dst->size);
165 delta_size = max_size - base_size;
167 /* We would not consider edits that change the file size so
168 * drastically. delta_size must be smaller than
169 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
171 * Note that base_size == 0 case is handled here already
172 * and the final score computation below would not have a
173 * divide-by-zero issue.
175 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
176 return 0;
178 if (!src->cnt_data && diff_populate_filespec(src, 0))
179 return 0;
180 if (!dst->cnt_data && diff_populate_filespec(dst, 0))
181 return 0;
183 delta_limit = (unsigned long)
184 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
185 if (diffcore_count_changes(src, dst,
186 &src->cnt_data, &dst->cnt_data,
187 delta_limit,
188 &src_copied, &literal_added))
189 return 0;
191 /* How similar are they?
192 * what percentage of material in dst are from source?
194 if (!dst->size)
195 score = 0; /* should not happen */
196 else
197 score = (int)(src_copied * MAX_SCORE / max_size);
198 return score;
201 static void record_rename_pair(int dst_index, int src_index, int score)
203 struct diff_filespec *src, *dst;
204 struct diff_filepair *dp;
206 if (rename_dst[dst_index].pair)
207 die("internal error: dst already matched.");
209 src = rename_src[src_index].p->one;
210 src->rename_used++;
211 src->count++;
213 dst = rename_dst[dst_index].two;
214 dst->count++;
216 dp = diff_queue(NULL, src, dst);
217 dp->renamed_pair = 1;
218 if (!strcmp(src->path, dst->path))
219 dp->score = rename_src[src_index].score;
220 else
221 dp->score = score;
222 rename_dst[dst_index].pair = dp;
226 * We sort the rename similarity matrix with the score, in descending
227 * order (the most similar first).
229 static int score_compare(const void *a_, const void *b_)
231 const struct diff_score *a = a_, *b = b_;
233 /* sink the unused ones to the bottom */
234 if (a->dst < 0)
235 return (0 <= b->dst);
236 else if (b->dst < 0)
237 return -1;
239 if (a->score == b->score)
240 return b->name_score - a->name_score;
242 return b->score - a->score;
245 struct file_similarity {
246 struct hashmap_entry entry;
247 int index;
248 struct diff_filespec *filespec;
251 static unsigned int hash_filespec(struct diff_filespec *filespec)
253 unsigned int hash;
254 if (!filespec->sha1_valid) {
255 if (diff_populate_filespec(filespec, 0))
256 return 0;
257 hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
259 memcpy(&hash, filespec->sha1, sizeof(hash));
260 return hash;
263 static int find_identical_files(struct hashmap *srcs,
264 int dst_index,
265 struct diff_options *options)
267 int renames = 0;
269 struct diff_filespec *target = rename_dst[dst_index].two;
270 struct file_similarity *p, *best, dst;
271 int i = 100, best_score = -1;
274 * Find the best source match for specified destination.
276 best = NULL;
277 hashmap_entry_init(&dst, hash_filespec(target));
278 for (p = hashmap_get(srcs, &dst, NULL); p; p = hashmap_get_next(srcs, p)) {
279 int score;
280 struct diff_filespec *source = p->filespec;
282 /* False hash collision? */
283 if (hashcmp(source->sha1, target->sha1))
284 continue;
285 /* Non-regular files? If so, the modes must match! */
286 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
287 if (source->mode != target->mode)
288 continue;
290 /* Give higher scores to sources that haven't been used already */
291 score = !source->rename_used;
292 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
293 continue;
294 score += basename_same(source, target);
295 if (score > best_score) {
296 best = p;
297 best_score = score;
298 if (score == 2)
299 break;
302 /* Too many identical alternatives? Pick one */
303 if (!--i)
304 break;
306 if (best) {
307 record_rename_pair(dst_index, best->index, MAX_SCORE);
308 renames++;
310 return renames;
313 static void insert_file_table(struct hashmap *table, int index, struct diff_filespec *filespec)
315 struct file_similarity *entry = xmalloc(sizeof(*entry));
317 entry->index = index;
318 entry->filespec = filespec;
320 hashmap_entry_init(entry, hash_filespec(filespec));
321 hashmap_add(table, entry);
325 * Find exact renames first.
327 * The first round matches up the up-to-date entries,
328 * and then during the second round we try to match
329 * cache-dirty entries as well.
331 static int find_exact_renames(struct diff_options *options)
333 int i, renames = 0;
334 struct hashmap file_table;
336 /* Add all sources to the hash table */
337 hashmap_init(&file_table, NULL, rename_src_nr);
338 for (i = 0; i < rename_src_nr; i++)
339 insert_file_table(&file_table, i, rename_src[i].p->one);
341 /* Walk the destinations and find best source match */
342 for (i = 0; i < rename_dst_nr; i++)
343 renames += find_identical_files(&file_table, i, options);
345 /* Free the hash data structure and entries */
346 hashmap_free(&file_table, 1);
348 return renames;
351 #define NUM_CANDIDATE_PER_DST 4
352 static void record_if_better(struct diff_score m[], struct diff_score *o)
354 int i, worst;
356 /* find the worst one */
357 worst = 0;
358 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
359 if (score_compare(&m[i], &m[worst]) > 0)
360 worst = i;
362 /* is it better than the worst one? */
363 if (score_compare(&m[worst], o) > 0)
364 m[worst] = *o;
368 * Returns:
369 * 0 if we are under the limit;
370 * 1 if we need to disable inexact rename detection;
371 * 2 if we would be under the limit if we were given -C instead of -C -C.
373 static int too_many_rename_candidates(int num_create,
374 struct diff_options *options)
376 int rename_limit = options->rename_limit;
377 int num_src = rename_src_nr;
378 int i;
380 options->needed_rename_limit = 0;
383 * This basically does a test for the rename matrix not
384 * growing larger than a "rename_limit" square matrix, ie:
386 * num_create * num_src > rename_limit * rename_limit
388 * but handles the potential overflow case specially (and we
389 * assume at least 32-bit integers)
391 if (rename_limit <= 0 || rename_limit > 32767)
392 rename_limit = 32767;
393 if ((num_create <= rename_limit || num_src <= rename_limit) &&
394 (num_create * num_src <= rename_limit * rename_limit))
395 return 0;
397 options->needed_rename_limit =
398 num_src > num_create ? num_src : num_create;
400 /* Are we running under -C -C? */
401 if (!DIFF_OPT_TST(options, FIND_COPIES_HARDER))
402 return 1;
404 /* Would we bust the limit if we were running under -C? */
405 for (num_src = i = 0; i < rename_src_nr; i++) {
406 if (diff_unmodified_pair(rename_src[i].p))
407 continue;
408 num_src++;
410 if ((num_create <= rename_limit || num_src <= rename_limit) &&
411 (num_create * num_src <= rename_limit * rename_limit))
412 return 2;
413 return 1;
416 static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
418 int count = 0, i;
420 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
421 struct diff_rename_dst *dst;
423 if ((mx[i].dst < 0) ||
424 (mx[i].score < minimum_score))
425 break; /* there is no more usable pair. */
426 dst = &rename_dst[mx[i].dst];
427 if (dst->pair)
428 continue; /* already done, either exact or fuzzy. */
429 if (!copies && rename_src[mx[i].src].p->one->rename_used)
430 continue;
431 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
432 count++;
434 return count;
437 void diffcore_rename(struct diff_options *options)
439 int detect_rename = options->detect_rename;
440 int minimum_score = options->rename_score;
441 struct diff_queue_struct *q = &diff_queued_diff;
442 struct diff_queue_struct outq;
443 struct diff_score *mx;
444 int i, j, rename_count, skip_unmodified = 0;
445 int num_create, dst_cnt;
446 struct progress *progress = NULL;
448 if (!minimum_score)
449 minimum_score = DEFAULT_RENAME_SCORE;
451 for (i = 0; i < q->nr; i++) {
452 struct diff_filepair *p = q->queue[i];
453 if (!DIFF_FILE_VALID(p->one)) {
454 if (!DIFF_FILE_VALID(p->two))
455 continue; /* unmerged */
456 else if (options->single_follow &&
457 strcmp(options->single_follow, p->two->path))
458 continue; /* not interested */
459 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
460 is_empty_blob_sha1(p->two->sha1))
461 continue;
462 else
463 locate_rename_dst(p->two, 1);
465 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
466 is_empty_blob_sha1(p->one->sha1))
467 continue;
468 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
470 * If the source is a broken "delete", and
471 * they did not really want to get broken,
472 * that means the source actually stays.
473 * So we increment the "rename_used" score
474 * by one, to indicate ourselves as a user
476 if (p->broken_pair && !p->score)
477 p->one->rename_used++;
478 register_rename_src(p);
480 else if (detect_rename == DIFF_DETECT_COPY) {
482 * Increment the "rename_used" score by
483 * one, to indicate ourselves as a user.
485 p->one->rename_used++;
486 register_rename_src(p);
489 if (rename_dst_nr == 0 || rename_src_nr == 0)
490 goto cleanup; /* nothing to do */
493 * We really want to cull the candidates list early
494 * with cheap tests in order to avoid doing deltas.
496 rename_count = find_exact_renames(options);
498 /* Did we only want exact renames? */
499 if (minimum_score == MAX_SCORE)
500 goto cleanup;
503 * Calculate how many renames are left (but all the source
504 * files still remain as options for rename/copies!)
506 num_create = (rename_dst_nr - rename_count);
508 /* All done? */
509 if (!num_create)
510 goto cleanup;
512 switch (too_many_rename_candidates(num_create, options)) {
513 case 1:
514 goto cleanup;
515 case 2:
516 options->degraded_cc_to_c = 1;
517 skip_unmodified = 1;
518 break;
519 default:
520 break;
523 if (options->show_rename_progress) {
524 progress = start_progress_delay(
525 "Performing inexact rename detection",
526 rename_dst_nr * rename_src_nr, 50, 1);
529 mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
530 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
531 struct diff_filespec *two = rename_dst[i].two;
532 struct diff_score *m;
534 if (rename_dst[i].pair)
535 continue; /* dealt with exact match already. */
537 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
538 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
539 m[j].dst = -1;
541 for (j = 0; j < rename_src_nr; j++) {
542 struct diff_filespec *one = rename_src[j].p->one;
543 struct diff_score this_src;
545 if (skip_unmodified &&
546 diff_unmodified_pair(rename_src[j].p))
547 continue;
549 this_src.score = estimate_similarity(one, two,
550 minimum_score);
551 this_src.name_score = basename_same(one, two);
552 this_src.dst = i;
553 this_src.src = j;
554 record_if_better(m, &this_src);
556 * Once we run estimate_similarity,
557 * We do not need the text anymore.
559 diff_free_filespec_blob(one);
560 diff_free_filespec_blob(two);
562 dst_cnt++;
563 display_progress(progress, (i+1)*rename_src_nr);
565 stop_progress(&progress);
567 /* cost matrix sorted by most to least similar pair */
568 qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
570 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
571 if (detect_rename == DIFF_DETECT_COPY)
572 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
573 free(mx);
575 cleanup:
576 /* At this point, we have found some renames and copies and they
577 * are recorded in rename_dst. The original list is still in *q.
579 DIFF_QUEUE_CLEAR(&outq);
580 for (i = 0; i < q->nr; i++) {
581 struct diff_filepair *p = q->queue[i];
582 struct diff_filepair *pair_to_free = NULL;
584 if (DIFF_PAIR_UNMERGED(p)) {
585 diff_q(&outq, p);
587 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
589 * Creation
591 * We would output this create record if it has
592 * not been turned into a rename/copy already.
594 struct diff_rename_dst *dst =
595 locate_rename_dst(p->two, 0);
596 if (dst && dst->pair) {
597 diff_q(&outq, dst->pair);
598 pair_to_free = p;
600 else
601 /* no matching rename/copy source, so
602 * record this as a creation.
604 diff_q(&outq, p);
606 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
608 * Deletion
610 * We would output this delete record if:
612 * (1) this is a broken delete and the counterpart
613 * broken create remains in the output; or
614 * (2) this is not a broken delete, and rename_dst
615 * does not have a rename/copy to move p->one->path
616 * out of existence.
618 * Otherwise, the counterpart broken create
619 * has been turned into a rename-edit; or
620 * delete did not have a matching create to
621 * begin with.
623 if (DIFF_PAIR_BROKEN(p)) {
624 /* broken delete */
625 struct diff_rename_dst *dst =
626 locate_rename_dst(p->one, 0);
627 if (dst && dst->pair)
628 /* counterpart is now rename/copy */
629 pair_to_free = p;
631 else {
632 if (p->one->rename_used)
633 /* this path remains */
634 pair_to_free = p;
637 if (pair_to_free)
639 else
640 diff_q(&outq, p);
642 else if (!diff_unmodified_pair(p))
643 /* all the usual ones need to be kept */
644 diff_q(&outq, p);
645 else
646 /* no need to keep unmodified pairs */
647 pair_to_free = p;
649 if (pair_to_free)
650 diff_free_filepair(pair_to_free);
652 diff_debug_queue("done copying original", &outq);
654 free(q->queue);
655 *q = outq;
656 diff_debug_queue("done collapsing", q);
658 for (i = 0; i < rename_dst_nr; i++)
659 free_filespec(rename_dst[i].two);
661 free(rename_dst);
662 rename_dst = NULL;
663 rename_dst_nr = rename_dst_alloc = 0;
664 free(rename_src);
665 rename_src = NULL;
666 rename_src_nr = rename_src_alloc = 0;
667 return;