copy vs rename detection: avoid unnecessary O(n*m) loops
[git/git.git] / diffcore-rename.c
CommitLineData
427dcb4b
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
427dcb4b 7
25d5ea41
JH
8/* Table of rename/copy destinations */
9
10static struct diff_rename_dst {
11 struct diff_filespec *two;
12 struct diff_filepair *pair;
13} *rename_dst;
14static int rename_dst_nr, rename_dst_alloc;
427dcb4b 15
25d5ea41
JH
16static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
17 int insert_ok)
427dcb4b 18{
25d5ea41
JH
19 int first, last;
20
21 first = 0;
22 last = rename_dst_nr;
23 while (last > first) {
24 int next = (last + first) >> 1;
25 struct diff_rename_dst *dst = &(rename_dst[next]);
26 int cmp = strcmp(two->path, dst->two->path);
27 if (!cmp)
28 return dst;
29 if (cmp < 0) {
30 last = next;
31 continue;
32 }
33 first = next+1;
34 }
35 /* not found */
36 if (!insert_ok)
37 return NULL;
38 /* insert to make it at "first" */
39 if (rename_dst_alloc <= rename_dst_nr) {
40 rename_dst_alloc = alloc_nr(rename_dst_alloc);
41 rename_dst = xrealloc(rename_dst,
42 rename_dst_alloc * sizeof(*rename_dst));
43 }
44 rename_dst_nr++;
45 if (first < rename_dst_nr)
46 memmove(rename_dst + first + 1, rename_dst + first,
47 (rename_dst_nr - first - 1) * sizeof(*rename_dst));
5098bafb
JH
48 rename_dst[first].two = alloc_filespec(two->path);
49 fill_filespec(rename_dst[first].two, two->sha1, two->mode);
25d5ea41
JH
50 rename_dst[first].pair = NULL;
51 return &(rename_dst[first]);
427dcb4b
JH
52}
53
15d061b4 54/* Table of rename/copy src files */
25d5ea41
JH
55static struct diff_rename_src {
56 struct diff_filespec *one;
fc580719 57 unsigned short score; /* to remember the break score */
25d5ea41
JH
58} *rename_src;
59static int rename_src_nr, rename_src_alloc;
427dcb4b 60
15d061b4 61static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
fc580719 62 unsigned short score)
25d5ea41
JH
63{
64 int first, last;
65
66 first = 0;
67 last = rename_src_nr;
68 while (last > first) {
69 int next = (last + first) >> 1;
70 struct diff_rename_src *src = &(rename_src[next]);
71 int cmp = strcmp(one->path, src->one->path);
72 if (!cmp)
73 return src;
74 if (cmp < 0) {
75 last = next;
76 continue;
77 }
78 first = next+1;
79 }
15d061b4 80
25d5ea41
JH
81 /* insert to make it at "first" */
82 if (rename_src_alloc <= rename_src_nr) {
83 rename_src_alloc = alloc_nr(rename_src_alloc);
84 rename_src = xrealloc(rename_src,
85 rename_src_alloc * sizeof(*rename_src));
427dcb4b 86 }
25d5ea41
JH
87 rename_src_nr++;
88 if (first < rename_src_nr)
89 memmove(rename_src + first + 1, rename_src + first,
90 (rename_src_nr - first - 1) * sizeof(*rename_src));
91 rename_src[first].one = one;
fc580719 92 rename_src[first].score = score;
25d5ea41 93 return &(rename_src[first]);
427dcb4b
JH
94}
95
17e6019a
JH
96static int is_exact_match(struct diff_filespec *src,
97 struct diff_filespec *dst,
98 int contents_too)
427dcb4b
JH
99{
100 if (src->sha1_valid && dst->sha1_valid &&
a89fccd2 101 !hashcmp(src->sha1, dst->sha1))
427dcb4b 102 return 1;
17e6019a
JH
103 if (!contents_too)
104 return 0;
f0c6b2a2
JH
105 if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
106 return 0;
107 if (src->size != dst->size)
108 return 0;
7da41f48
SP
109 if (src->sha1_valid && dst->sha1_valid)
110 return !hashcmp(src->sha1, dst->sha1);
f0c6b2a2 111 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
427dcb4b
JH
112 return 0;
113 if (src->size == dst->size &&
114 !memcmp(src->data, dst->data, src->size))
115 return 1;
116 return 0;
117}
118
0ce39643
JS
119static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
120{
121 int src_len = strlen(src->path), dst_len = strlen(dst->path);
122 while (src_len && dst_len) {
123 char c1 = src->path[--src_len];
124 char c2 = dst->path[--dst_len];
125 if (c1 != c2)
126 return 0;
127 if (c1 == '/')
128 return 1;
129 }
130 return (!src_len || src->path[src_len - 1] == '/') &&
131 (!dst_len || dst->path[dst_len - 1] == '/');
132}
133
427dcb4b 134struct diff_score {
25d5ea41
JH
135 int src; /* index in rename_src */
136 int dst; /* index in rename_dst */
427dcb4b 137 int score;
cfc0aef1 138 int name_score;
427dcb4b
JH
139};
140
141static int estimate_similarity(struct diff_filespec *src,
142 struct diff_filespec *dst,
143 int minimum_score)
144{
145 /* src points at a file that existed in the original tree (or
146 * optionally a file in the destination tree) and dst points
147 * at a newly created file. They may be quite similar, in which
148 * case we want to say src is renamed to dst or src is copied into
149 * dst, and then some edit has been applied to dst.
150 *
151 * Compare them and return how similar they are, representing
f0c6b2a2
JH
152 * the score as an integer between 0 and MAX_SCORE.
153 *
154 * When there is an exact match, it is considered a better
155 * match than anything else; the destination does not even
156 * call into this function in that case.
427dcb4b 157 */
90bd932c 158 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
75c660ac 159 unsigned long delta_limit;
427dcb4b
JH
160 int score;
161
60896c7b
JH
162 /* We deal only with regular files. Symlink renames are handled
163 * only when they are exact matches --- in other words, no edits
164 * after renaming.
165 */
166 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
167 return 0;
168
90bd932c 169 max_size = ((src->size > dst->size) ? src->size : dst->size);
58b103f5 170 base_size = ((src->size < dst->size) ? src->size : dst->size);
90bd932c 171 delta_size = max_size - base_size;
427dcb4b 172
58b103f5
JH
173 /* We would not consider edits that change the file size so
174 * drastically. delta_size must be smaller than
cd1870ed 175 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
f0c6b2a2 176 *
58b103f5
JH
177 * Note that base_size == 0 case is handled here already
178 * and the final score computation below would not have a
179 * divide-by-zero issue.
427dcb4b 180 */
cd1870ed 181 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
427dcb4b
JH
182 return 0;
183
eede7b7d
JK
184 if ((!src->cnt_data && diff_populate_filespec(src, 0))
185 || (!dst->cnt_data && diff_populate_filespec(dst, 0)))
f0c6b2a2
JH
186 return 0; /* error but caught downstream */
187
85976974 188
dc49cd76
SP
189 delta_limit = (unsigned long)
190 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
d8c3d03a 191 if (diffcore_count_changes(src, dst,
c06c7966 192 &src->cnt_data, &dst->cnt_data,
65416758
JH
193 delta_limit,
194 &src_copied, &literal_added))
85976974 195 return 0;
355e76a4 196
1706306a
JH
197 /* How similar are they?
198 * what percentage of material in dst are from source?
427dcb4b 199 */
90bd932c 200 if (!dst->size)
1706306a 201 score = 0; /* should not happen */
cfc0aef1 202 else
dc49cd76 203 score = (int)(src_copied * MAX_SCORE / max_size);
427dcb4b
JH
204 return score;
205}
206
5098bafb 207static void record_rename_pair(int dst_index, int src_index, int score)
427dcb4b 208{
9fb88419 209 struct diff_filespec *src, *dst;
25d5ea41 210 struct diff_filepair *dp;
f7c1512a 211
25d5ea41
JH
212 if (rename_dst[dst_index].pair)
213 die("internal error: dst already matched.");
427dcb4b 214
25d5ea41 215 src = rename_src[src_index].one;
64479711 216 src->rename_used++;
9fb88419 217 src->count++;
427dcb4b 218
25d5ea41 219 dst = rename_dst[dst_index].two;
9fb88419 220 dst->count++;
427dcb4b 221
9fb88419 222 dp = diff_queue(NULL, src, dst);
ef677686 223 dp->renamed_pair = 1;
fc580719
JH
224 if (!strcmp(src->path, dst->path))
225 dp->score = rename_src[src_index].score;
226 else
227 dp->score = score;
25d5ea41 228 rename_dst[dst_index].pair = dp;
427dcb4b
JH
229}
230
231/*
232 * We sort the rename similarity matrix with the score, in descending
15d061b4 233 * order (the most similar first).
427dcb4b
JH
234 */
235static int score_compare(const void *a_, const void *b_)
236{
237 const struct diff_score *a = a_, *b = b_;
cfc0aef1
RS
238
239 if (a->score == b->score)
240 return b->name_score - a->name_score;
241
427dcb4b
JH
242 return b->score - a->score;
243}
244
cb1491b6
LT
245/*
246 * Find exact renames first.
247 *
248 * The first round matches up the up-to-date entries,
249 * and then during the second round we try to match
250 * cache-dirty entries as well.
251 *
252 * Note: the rest of the rename logic depends on this
253 * phase also populating all the filespecs for any
254 * entry that isn't matched up with an exact rename,
255 * see "is_exact_match()".
256 */
257static int find_exact_renames(void)
258{
259 int rename_count = 0;
260 int contents_too;
261
262 for (contents_too = 0; contents_too < 2; contents_too++) {
263 int i;
264
265 for (i = 0; i < rename_dst_nr; i++) {
266 struct diff_filespec *two = rename_dst[i].two;
267 int j;
268
269 if (rename_dst[i].pair)
270 continue; /* dealt with an earlier round */
271 for (j = 0; j < rename_src_nr; j++) {
272 int k;
273 struct diff_filespec *one = rename_src[j].one;
274 if (!is_exact_match(one, two, contents_too))
275 continue;
276
277 /* see if there is a basename match, too */
278 for (k = j; k < rename_src_nr; k++) {
279 one = rename_src[k].one;
280 if (basename_same(one, two) &&
281 is_exact_match(one, two,
282 contents_too)) {
283 j = k;
284 break;
285 }
286 }
287
288 record_rename_pair(i, j, (int)MAX_SCORE);
289 rename_count++;
290 break; /* we are done with this entry */
291 }
292 }
293 }
294 return rename_count;
295}
296
8082d8d3 297void diffcore_rename(struct diff_options *options)
427dcb4b 298{
8082d8d3
JH
299 int detect_rename = options->detect_rename;
300 int minimum_score = options->rename_score;
301 int rename_limit = options->rename_limit;
38c6f780 302 struct diff_queue_struct *q = &diff_queued_diff;
5098bafb 303 struct diff_queue_struct outq;
427dcb4b 304 struct diff_score *mx;
cb1491b6 305 int i, j, rename_count;
25d5ea41 306 int num_create, num_src, dst_cnt;
427dcb4b 307
26dee0ad 308 if (!minimum_score)
f345b0a0 309 minimum_score = DEFAULT_RENAME_SCORE;
427dcb4b 310
427dcb4b 311 for (i = 0; i < q->nr; i++) {
52e95789 312 struct diff_filepair *p = q->queue[i];
2f3f8b21 313 if (!DIFF_FILE_VALID(p->one)) {
81e50eab 314 if (!DIFF_FILE_VALID(p->two))
60896c7b 315 continue; /* unmerged */
2f3f8b21
JH
316 else if (options->single_follow &&
317 strcmp(options->single_follow, p->two->path))
318 continue; /* not interested */
427dcb4b 319 else
25d5ea41 320 locate_rename_dst(p->two, 1);
2f3f8b21 321 }
2210100a 322 else if (!DIFF_FILE_VALID(p->two)) {
64479711
LT
323 /*
324 * If the source is a broken "delete", and
2210100a
JH
325 * they did not really want to get broken,
326 * that means the source actually stays.
64479711
LT
327 * So we increment the "rename_used" score
328 * by one, to indicate ourselves as a user
329 */
330 if (p->broken_pair && !p->score)
331 p->one->rename_used++;
332 register_rename_src(p->one, p->score);
333 }
334 else if (detect_rename == DIFF_DETECT_COPY) {
335 /*
336 * Increment the "rename_used" score by
337 * one, to indicate ourselves as a user.
2210100a 338 */
64479711
LT
339 p->one->rename_used++;
340 register_rename_src(p->one, p->score);
2210100a 341 }
427dcb4b 342 }
0024a549 343 if (rename_dst_nr == 0 || rename_src_nr == 0)
427dcb4b
JH
344 goto cleanup; /* nothing to do */
345
0024a549
LT
346 /*
347 * This basically does a test for the rename matrix not
348 * growing larger than a "rename_limit" square matrix, ie:
349 *
350 * rename_dst_nr * rename_src_nr > rename_limit * rename_limit
351 *
352 * but handles the potential overflow case specially (and we
353 * assume at least 32-bit integers)
354 */
355 if (rename_limit <= 0 || rename_limit > 32767)
356 rename_limit = 32767;
357 if (rename_dst_nr > rename_limit && rename_src_nr > rename_limit)
358 goto cleanup;
359 if (rename_dst_nr * rename_src_nr > rename_limit * rename_limit)
360 goto cleanup;
361
cb1491b6
LT
362 /*
363 * We really want to cull the candidates list early
427dcb4b 364 * with cheap tests in order to avoid doing deltas.
427dcb4b 365 */
cb1491b6 366 rename_count = find_exact_renames();
427dcb4b
JH
367
368 /* Have we run out the created file pool? If so we can avoid
369 * doing the delta matrix altogether.
370 */
5098bafb 371 if (rename_count == rename_dst_nr)
15d061b4 372 goto cleanup;
427dcb4b 373
9f70b806
JH
374 if (minimum_score == MAX_SCORE)
375 goto cleanup;
376
5098bafb 377 num_create = (rename_dst_nr - rename_count);
25d5ea41 378 num_src = rename_src_nr;
427dcb4b 379 mx = xmalloc(sizeof(*mx) * num_create * num_src);
25d5ea41 380 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
427dcb4b 381 int base = dst_cnt * num_src;
25d5ea41
JH
382 struct diff_filespec *two = rename_dst[i].two;
383 if (rename_dst[i].pair)
427dcb4b 384 continue; /* dealt with exact match already. */
25d5ea41
JH
385 for (j = 0; j < rename_src_nr; j++) {
386 struct diff_filespec *one = rename_src[j].one;
387 struct diff_score *m = &mx[base+j];
388 m->src = j;
389 m->dst = i;
390 m->score = estimate_similarity(one, two,
391 minimum_score);
cfc0aef1 392 m->name_score = basename_same(one, two);
8ae92e63 393 diff_free_filespec_blob(one);
427dcb4b 394 }
2821104d 395 /* We do not need the text anymore */
8ae92e63 396 diff_free_filespec_blob(two);
427dcb4b
JH
397 dst_cnt++;
398 }
399 /* cost matrix sorted by most to least similar pair */
400 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
401 for (i = 0; i < num_create * num_src; i++) {
25d5ea41
JH
402 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
403 if (dst->pair)
404 continue; /* already done, either exact or fuzzy. */
427dcb4b 405 if (mx[i].score < minimum_score)
15d061b4 406 break; /* there is no more usable pair. */
5098bafb
JH
407 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
408 rename_count++;
427dcb4b
JH
409 }
410 free(mx);
427dcb4b 411
15d061b4 412 cleanup:
427dcb4b 413 /* At this point, we have found some renames and copies and they
5098bafb 414 * are recorded in rename_dst. The original list is still in *q.
427dcb4b 415 */
25d5ea41
JH
416 outq.queue = NULL;
417 outq.nr = outq.alloc = 0;
427dcb4b 418 for (i = 0; i < q->nr; i++) {
25d5ea41 419 struct diff_filepair *p = q->queue[i];
25d5ea41
JH
420 struct diff_filepair *pair_to_free = NULL;
421
2cd68882
JH
422 if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
423 /*
424 * Creation
425 *
426 * We would output this create record if it has
427 * not been turned into a rename/copy already.
428 */
429 struct diff_rename_dst *dst =
430 locate_rename_dst(p->two, 0);
431 if (dst && dst->pair) {
25d5ea41
JH
432 diff_q(&outq, dst->pair);
433 pair_to_free = p;
434 }
435 else
2cd68882
JH
436 /* no matching rename/copy source, so
437 * record this as a creation.
25d5ea41
JH
438 */
439 diff_q(&outq, p);
427dcb4b 440 }
2cd68882
JH
441 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
442 /*
443 * Deletion
444 *
f345b0a0
JH
445 * We would output this delete record if:
446 *
447 * (1) this is a broken delete and the counterpart
448 * broken create remains in the output; or
5098bafb
JH
449 * (2) this is not a broken delete, and rename_dst
450 * does not have a rename/copy to move p->one->path
451 * out of existence.
f345b0a0
JH
452 *
453 * Otherwise, the counterpart broken create
454 * has been turned into a rename-edit; or
455 * delete did not have a matching create to
456 * begin with.
2cd68882 457 */
f345b0a0
JH
458 if (DIFF_PAIR_BROKEN(p)) {
459 /* broken delete */
460 struct diff_rename_dst *dst =
461 locate_rename_dst(p->one, 0);
462 if (dst && dst->pair)
463 /* counterpart is now rename/copy */
464 pair_to_free = p;
465 }
466 else {
64479711 467 if (p->one->rename_used)
f345b0a0
JH
468 /* this path remains */
469 pair_to_free = p;
470 }
2cd68882
JH
471
472 if (pair_to_free)
473 ;
474 else
475 diff_q(&outq, p);
476 }
25d5ea41 477 else if (!diff_unmodified_pair(p))
15d061b4 478 /* all the usual ones need to be kept */
25d5ea41 479 diff_q(&outq, p);
15d061b4
JH
480 else
481 /* no need to keep unmodified pairs */
482 pair_to_free = p;
483
226406f6
JH
484 if (pair_to_free)
485 diff_free_filepair(pair_to_free);
427dcb4b 486 }
25d5ea41 487 diff_debug_queue("done copying original", &outq);
427dcb4b 488
25d5ea41
JH
489 free(q->queue);
490 *q = outq;
491 diff_debug_queue("done collapsing", q);
427dcb4b 492
9fb88419
LT
493 for (i = 0; i < rename_dst_nr; i++)
494 free_filespec(rename_dst[i].two);
5098bafb 495
25d5ea41
JH
496 free(rename_dst);
497 rename_dst = NULL;
498 rename_dst_nr = rename_dst_alloc = 0;
499 free(rename_src);
500 rename_src = NULL;
501 rename_src_nr = rename_src_alloc = 0;
427dcb4b
JH
502 return;
503}