]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-rename.c
diffcore-rename: filter rename_src list when possible
[thirdparty/git.git] / diffcore-rename.c
CommitLineData
427dcb4b 1/*
95acf11a 2 *
427dcb4b
JH
3 * Copyright (C) 2005 Junio C Hamano
4 */
5#include "cache.h"
6#include "diff.h"
7#include "diffcore.h"
cbd53a21 8#include "object-store.h"
f79d9c58 9#include "hashmap.h"
3ac942d4 10#include "progress.h"
95acf11a 11#include "promisor-remote.h"
9db2ac56 12#include "strmap.h"
427dcb4b 13
25d5ea41
JH
14/* Table of rename/copy destinations */
15
16static struct diff_rename_dst {
9db2ac56
EN
17 struct diff_filepair *p;
18 struct diff_filespec *filespec_to_free;
19 int is_rename; /* false -> just a create; true -> rename or copy */
25d5ea41
JH
20} *rename_dst;
21static int rename_dst_nr, rename_dst_alloc;
9db2ac56
EN
22/* Mapping from break source pathname to break destination index */
23static struct strintmap *break_idx = NULL;
427dcb4b 24
9db2ac56 25static struct diff_rename_dst *locate_rename_dst(struct diff_filepair *p)
f98c2f7e 26{
9db2ac56
EN
27 /* Lookup by p->ONE->path */
28 int idx = break_idx ? strintmap_get(break_idx, p->one->path) : -1;
29 return (idx == -1) ? NULL : &rename_dst[idx];
f98c2f7e
JK
30}
31
32/*
33 * Returns 0 on success, -1 if we found a duplicate.
34 */
9db2ac56 35static int add_rename_dst(struct diff_filepair *p)
f98c2f7e 36{
337ce247 37 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
9db2ac56
EN
38 rename_dst[rename_dst_nr].p = p;
39 rename_dst[rename_dst_nr].filespec_to_free = NULL;
40 rename_dst[rename_dst_nr].is_rename = 0;
25d5ea41 41 rename_dst_nr++;
f98c2f7e 42 return 0;
427dcb4b
JH
43}
44
15d061b4 45/* Table of rename/copy src files */
25d5ea41 46static struct diff_rename_src {
e88d6bc6 47 struct diff_filepair *p;
fc580719 48 unsigned short score; /* to remember the break score */
25d5ea41
JH
49} *rename_src;
50static int rename_src_nr, rename_src_alloc;
427dcb4b 51
b970b4ef 52static void register_rename_src(struct diff_filepair *p)
25d5ea41 53{
9db2ac56
EN
54 if (p->broken_pair) {
55 if (!break_idx) {
56 break_idx = xmalloc(sizeof(*break_idx));
57 strintmap_init(break_idx, -1);
58 }
59 strintmap_set(break_idx, p->one->path, rename_dst_nr);
60 }
61
337ce247 62 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
b970b4ef
EN
63 rename_src[rename_src_nr].p = p;
64 rename_src[rename_src_nr].score = p->score;
25d5ea41 65 rename_src_nr++;
427dcb4b
JH
66}
67
0ce39643
JS
68static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
69{
70 int src_len = strlen(src->path), dst_len = strlen(dst->path);
71 while (src_len && dst_len) {
72 char c1 = src->path[--src_len];
73 char c2 = dst->path[--dst_len];
74 if (c1 != c2)
75 return 0;
76 if (c1 == '/')
77 return 1;
78 }
79 return (!src_len || src->path[src_len - 1] == '/') &&
80 (!dst_len || dst->path[dst_len - 1] == '/');
81}
82
427dcb4b 83struct diff_score {
25d5ea41
JH
84 int src; /* index in rename_src */
85 int dst; /* index in rename_dst */
6d24ad97
JH
86 unsigned short score;
87 short name_score;
427dcb4b
JH
88};
89
95acf11a
JT
90struct prefetch_options {
91 struct repository *repo;
92 int skip_unmodified;
93};
94static void prefetch(void *prefetch_options)
95{
96 struct prefetch_options *options = prefetch_options;
97 int i;
98 struct oid_array to_fetch = OID_ARRAY_INIT;
99
100 for (i = 0; i < rename_dst_nr; i++) {
9db2ac56 101 if (rename_dst[i].p->renamed_pair)
95acf11a
JT
102 /*
103 * The loop in diffcore_rename() will not need these
104 * blobs, so skip prefetching.
105 */
106 continue; /* already found exact match */
107 diff_add_if_missing(options->repo, &to_fetch,
9db2ac56 108 rename_dst[i].p->two);
95acf11a
JT
109 }
110 for (i = 0; i < rename_src_nr; i++) {
111 if (options->skip_unmodified &&
112 diff_unmodified_pair(rename_src[i].p))
113 /*
114 * The loop in diffcore_rename() will not need these
115 * blobs, so skip prefetching.
116 */
117 continue;
118 diff_add_if_missing(options->repo, &to_fetch,
119 rename_src[i].p->one);
120 }
121 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
122 oid_array_clear(&to_fetch);
123}
124
b78ea5fc
NTND
125static int estimate_similarity(struct repository *r,
126 struct diff_filespec *src,
427dcb4b 127 struct diff_filespec *dst,
95acf11a
JT
128 int minimum_score,
129 int skip_unmodified)
427dcb4b
JH
130{
131 /* src points at a file that existed in the original tree (or
132 * optionally a file in the destination tree) and dst points
133 * at a newly created file. They may be quite similar, in which
134 * case we want to say src is renamed to dst or src is copied into
135 * dst, and then some edit has been applied to dst.
136 *
137 * Compare them and return how similar they are, representing
f0c6b2a2
JH
138 * the score as an integer between 0 and MAX_SCORE.
139 *
140 * When there is an exact match, it is considered a better
141 * match than anything else; the destination does not even
142 * call into this function in that case.
427dcb4b 143 */
90bd932c 144 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
427dcb4b 145 int score;
1c37e86a
JT
146 struct diff_populate_filespec_options dpf_options = {
147 .check_size_only = 1
148 };
95acf11a
JT
149 struct prefetch_options prefetch_options = {r, skip_unmodified};
150
151 if (r == the_repository && has_promisor_remote()) {
152 dpf_options.missing_object_cb = prefetch;
153 dpf_options.missing_object_data = &prefetch_options;
154 }
427dcb4b 155
60896c7b
JH
156 /* We deal only with regular files. Symlink renames are handled
157 * only when they are exact matches --- in other words, no edits
158 * after renaming.
159 */
160 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
161 return 0;
162
81ac051d
LT
163 /*
164 * Need to check that source and destination sizes are
165 * filled in before comparing them.
166 *
167 * If we already have "cnt_data" filled in, we know it's
168 * all good (avoid checking the size for zero, as that
169 * is a possible size - we really should have a flag to
170 * say whether the size is valid or not!)
171 */
8e5dd3d6 172 if (!src->cnt_data &&
1c37e86a 173 diff_populate_filespec(r, src, &dpf_options))
81ac051d 174 return 0;
8e5dd3d6 175 if (!dst->cnt_data &&
1c37e86a 176 diff_populate_filespec(r, dst, &dpf_options))
81ac051d
LT
177 return 0;
178
90bd932c 179 max_size = ((src->size > dst->size) ? src->size : dst->size);
58b103f5 180 base_size = ((src->size < dst->size) ? src->size : dst->size);
90bd932c 181 delta_size = max_size - base_size;
427dcb4b 182
58b103f5
JH
183 /* We would not consider edits that change the file size so
184 * drastically. delta_size must be smaller than
cd1870ed 185 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
f0c6b2a2 186 *
58b103f5
JH
187 * Note that base_size == 0 case is handled here already
188 * and the final score computation below would not have a
189 * divide-by-zero issue.
427dcb4b 190 */
3a4d6769 191 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
427dcb4b
JH
192 return 0;
193
95acf11a
JT
194 dpf_options.check_size_only = 0;
195
196 if (!src->cnt_data && diff_populate_filespec(r, src, &dpf_options))
885c716f 197 return 0;
95acf11a 198 if (!dst->cnt_data && diff_populate_filespec(r, dst, &dpf_options))
885c716f
BS
199 return 0;
200
b78ea5fc 201 if (diffcore_count_changes(r, src, dst,
c06c7966 202 &src->cnt_data, &dst->cnt_data,
65416758 203 &src_copied, &literal_added))
85976974 204 return 0;
355e76a4 205
1706306a
JH
206 /* How similar are they?
207 * what percentage of material in dst are from source?
427dcb4b 208 */
90bd932c 209 if (!dst->size)
1706306a 210 score = 0; /* should not happen */
cfc0aef1 211 else
dc49cd76 212 score = (int)(src_copied * MAX_SCORE / max_size);
427dcb4b
JH
213 return score;
214}
215
5098bafb 216static void record_rename_pair(int dst_index, int src_index, int score)
427dcb4b 217{
9db2ac56
EN
218 struct diff_filepair *src = rename_src[src_index].p;
219 struct diff_filepair *dst = rename_dst[dst_index].p;
f7c1512a 220
9db2ac56 221 if (dst->renamed_pair)
25d5ea41 222 die("internal error: dst already matched.");
427dcb4b 223
9db2ac56
EN
224 src->one->rename_used++;
225 src->one->count++;
427dcb4b 226
9db2ac56
EN
227 rename_dst[dst_index].filespec_to_free = dst->one;
228 rename_dst[dst_index].is_rename = 1;
427dcb4b 229
9db2ac56
EN
230 dst->one = src->one;
231 dst->renamed_pair = 1;
232 if (!strcmp(dst->one->path, dst->two->path))
233 dst->score = rename_src[src_index].score;
fc580719 234 else
9db2ac56 235 dst->score = score;
427dcb4b
JH
236}
237
238/*
239 * We sort the rename similarity matrix with the score, in descending
15d061b4 240 * order (the most similar first).
427dcb4b
JH
241 */
242static int score_compare(const void *a_, const void *b_)
243{
244 const struct diff_score *a = a_, *b = b_;
cfc0aef1 245
6d24ad97
JH
246 /* sink the unused ones to the bottom */
247 if (a->dst < 0)
248 return (0 <= b->dst);
249 else if (b->dst < 0)
250 return -1;
251
cfc0aef1
RS
252 if (a->score == b->score)
253 return b->name_score - a->name_score;
254
427dcb4b
JH
255 return b->score - a->score;
256}
257
9027f53c 258struct file_similarity {
f79d9c58 259 struct hashmap_entry entry;
7c85f8ac 260 int index;
9027f53c 261 struct diff_filespec *filespec;
9027f53c
LT
262};
263
b78ea5fc
NTND
264static unsigned int hash_filespec(struct repository *r,
265 struct diff_filespec *filespec)
48f6407f 266{
41c9560e 267 if (!filespec->oid_valid) {
1c37e86a 268 if (diff_populate_filespec(r, filespec, NULL))
48f6407f 269 return 0;
2dcde20e
MT
270 hash_object_file(r->hash_algo, filespec->data, filespec->size,
271 "blob", &filespec->oid);
48f6407f 272 }
d40abc8e 273 return oidhash(&filespec->oid);
48f6407f
KB
274}
275
f79d9c58 276static int find_identical_files(struct hashmap *srcs,
7c85f8ac 277 int dst_index,
11f944dd 278 struct diff_options *options)
9027f53c
LT
279{
280 int renames = 0;
9db2ac56 281 struct diff_filespec *target = rename_dst[dst_index].p->two;
ab73a9d1 282 struct file_similarity *p, *best = NULL;
48f6407f 283 int i = 100, best_score = -1;
f0e63c41 284 unsigned int hash = hash_filespec(options->repo, target);
48f6407f
KB
285
286 /*
7c85f8ac 287 * Find the best source match for specified destination.
48f6407f 288 */
f0e63c41
EW
289 p = hashmap_get_entry_from_hash(srcs, hash, NULL,
290 struct file_similarity, entry);
23dee69f 291 hashmap_for_each_entry_from(srcs, p, entry) {
48f6407f
KB
292 int score;
293 struct diff_filespec *source = p->filespec;
294
295 /* False hash collision? */
9001dc2a 296 if (!oideq(&source->oid, &target->oid))
48f6407f
KB
297 continue;
298 /* Non-regular files? If so, the modes must match! */
299 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
300 if (source->mode != target->mode)
0940e5f2 301 continue;
9027f53c 302 }
48f6407f
KB
303 /* Give higher scores to sources that haven't been used already */
304 score = !source->rename_used;
305 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
306 continue;
307 score += basename_same(source, target);
308 if (score > best_score) {
309 best = p;
310 best_score = score;
311 if (score == 2)
312 break;
9027f53c 313 }
48f6407f
KB
314
315 /* Too many identical alternatives? Pick one */
316 if (!--i)
317 break;
318 }
319 if (best) {
7c85f8ac 320 record_rename_pair(dst_index, best->index, MAX_SCORE);
48f6407f
KB
321 renames++;
322 }
9027f53c
LT
323 return renames;
324}
325
b78ea5fc
NTND
326static void insert_file_table(struct repository *r,
327 struct hashmap *table, int index,
328 struct diff_filespec *filespec)
9027f53c 329{
9027f53c
LT
330 struct file_similarity *entry = xmalloc(sizeof(*entry));
331
9027f53c
LT
332 entry->index = index;
333 entry->filespec = filespec;
9027f53c 334
d22245a2 335 hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
b94e5c1d 336 hashmap_add(table, &entry->entry);
9027f53c
LT
337}
338
cb1491b6
LT
339/*
340 * Find exact renames first.
341 *
342 * The first round matches up the up-to-date entries,
343 * and then during the second round we try to match
344 * cache-dirty entries as well.
cb1491b6 345 */
11f944dd 346static int find_exact_renames(struct diff_options *options)
cb1491b6 347{
7c85f8ac 348 int i, renames = 0;
f79d9c58 349 struct hashmap file_table;
cb1491b6 350
ca4e3ca0
SG
351 /* Add all sources to the hash table in reverse order, because
352 * later on they will be retrieved in LIFO order.
353 */
7663cdc8 354 hashmap_init(&file_table, NULL, NULL, rename_src_nr);
ca4e3ca0 355 for (i = rename_src_nr-1; i >= 0; i--)
b78ea5fc
NTND
356 insert_file_table(options->repo,
357 &file_table, i,
358 rename_src[i].p->one);
9027f53c 359
7c85f8ac 360 /* Walk the destinations and find best source match */
9027f53c 361 for (i = 0; i < rename_dst_nr; i++)
7c85f8ac 362 renames += find_identical_files(&file_table, i, options);
9027f53c 363
f79d9c58 364 /* Free the hash data structure and entries */
6da1a258 365 hashmap_clear_and_free(&file_table, struct file_similarity, entry);
9027f53c 366
7c85f8ac 367 return renames;
cb1491b6
LT
368}
369
6d24ad97
JH
370#define NUM_CANDIDATE_PER_DST 4
371static void record_if_better(struct diff_score m[], struct diff_score *o)
372{
373 int i, worst;
374
375 /* find the worst one */
376 worst = 0;
377 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
378 if (score_compare(&m[i], &m[worst]) > 0)
379 worst = i;
380
381 /* is it better than the worst one? */
382 if (score_compare(&m[worst], o) > 0)
383 m[worst] = *o;
384}
385
f31027c9
JH
386/*
387 * Returns:
388 * 0 if we are under the limit;
389 * 1 if we need to disable inexact rename detection;
390 * 2 if we would be under the limit if we were given -C instead of -C -C.
391 */
00b8cccd 392static int too_many_rename_candidates(int num_destinations, int num_sources,
9d8a5a50
JH
393 struct diff_options *options)
394{
395 int rename_limit = options->rename_limit;
00b8cccd 396 int i, limited_sources;
9d8a5a50
JH
397
398 options->needed_rename_limit = 0;
399
400 /*
401 * This basically does a test for the rename matrix not
402 * growing larger than a "rename_limit" square matrix, ie:
403 *
00b8cccd 404 * num_destinations * num_sources > rename_limit * rename_limit
ad8a1be5
EN
405 *
406 * We use st_mult() to check overflow conditions; in the
407 * exceptional circumstance that size_t isn't large enough to hold
408 * the multiplication, the system won't be able to allocate enough
409 * memory for the matrix anyway.
9d8a5a50 410 */
89973554
JT
411 if (rename_limit <= 0)
412 rename_limit = 32767;
ad8a1be5
EN
413 if (st_mult(num_destinations, num_sources)
414 <= st_mult(rename_limit, rename_limit))
9d8a5a50
JH
415 return 0;
416
417 options->needed_rename_limit =
00b8cccd 418 num_sources > num_destinations ? num_sources : num_destinations;
f31027c9
JH
419
420 /* Are we running under -C -C? */
0d1e0e78 421 if (!options->flags.find_copies_harder)
f31027c9
JH
422 return 1;
423
424 /* Would we bust the limit if we were running under -C? */
00b8cccd 425 for (limited_sources = i = 0; i < num_sources; i++) {
f31027c9
JH
426 if (diff_unmodified_pair(rename_src[i].p))
427 continue;
00b8cccd 428 limited_sources++;
f31027c9 429 }
ad8a1be5
EN
430 if (st_mult(num_destinations, limited_sources)
431 <= st_mult(rename_limit, rename_limit))
f31027c9 432 return 2;
9d8a5a50
JH
433 return 1;
434}
435
0940e5f2
LT
436static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
437{
438 int count = 0, i;
439
440 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
441 struct diff_rename_dst *dst;
442
443 if ((mx[i].dst < 0) ||
444 (mx[i].score < minimum_score))
445 break; /* there is no more usable pair. */
446 dst = &rename_dst[mx[i].dst];
9db2ac56 447 if (dst->is_rename)
0940e5f2 448 continue; /* already done, either exact or fuzzy. */
e88d6bc6 449 if (!copies && rename_src[mx[i].src].p->one->rename_used)
0940e5f2
LT
450 continue;
451 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
452 count++;
453 }
454 return count;
455}
456
829514c5
EN
457static void remove_unneeded_paths_from_src(int detecting_copies)
458{
459 int i, new_num_src;
460
461 if (detecting_copies)
462 return; /* nothing to remove */
463 if (break_idx)
464 return; /* culling incompatible with break detection */
465
466 /*
467 * Note on reasons why we cull unneeded sources but not destinations:
468 * 1) Pairings are stored in rename_dst (not rename_src), which we
469 * need to keep around. So, we just can't cull rename_dst even
470 * if we wanted to. But doing so wouldn't help because...
471 *
472 * 2) There is a matrix pairwise comparison that follows the
473 * "Performing inexact rename detection" progress message.
474 * Iterating over the destinations is done in the outer loop,
475 * hence we only iterate over each of those once and we can
476 * easily skip the outer loop early if the destination isn't
477 * relevant. That's only one check per destination path to
478 * skip.
479 *
480 * By contrast, the sources are iterated in the inner loop; if
481 * we check whether a source can be skipped, then we'll be
482 * checking it N separate times, once for each destination.
483 * We don't want to have to iterate over known-not-needed
484 * sources N times each, so avoid that by removing the sources
485 * from rename_src here.
486 */
487 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
488 /*
489 * renames are stored in rename_dst, so if a rename has
490 * already been detected using this source, we can just
491 * remove the source knowing rename_dst has its info.
492 */
493 if (rename_src[i].p->one->rename_used)
494 continue;
495
496 if (new_num_src < i)
497 memcpy(&rename_src[new_num_src], &rename_src[i],
498 sizeof(struct diff_rename_src));
499 new_num_src++;
500 }
501
502 rename_src_nr = new_num_src;
503}
504
8082d8d3 505void diffcore_rename(struct diff_options *options)
427dcb4b 506{
8082d8d3
JH
507 int detect_rename = options->detect_rename;
508 int minimum_score = options->rename_score;
38c6f780 509 struct diff_queue_struct *q = &diff_queued_diff;
5098bafb 510 struct diff_queue_struct outq;
427dcb4b 511 struct diff_score *mx;
f31027c9 512 int i, j, rename_count, skip_unmodified = 0;
26a66a6b 513 int num_destinations, dst_cnt;
f15eb7c1 514 int num_sources, want_copies;
3ac942d4 515 struct progress *progress = NULL;
427dcb4b 516
557ac035 517 trace2_region_enter("diff", "setup", options->repo);
f15eb7c1 518 want_copies = (detect_rename == DIFF_DETECT_COPY);
26dee0ad 519 if (!minimum_score)
f345b0a0 520 minimum_score = DEFAULT_RENAME_SCORE;
427dcb4b 521
427dcb4b 522 for (i = 0; i < q->nr; i++) {
52e95789 523 struct diff_filepair *p = q->queue[i];
2f3f8b21 524 if (!DIFF_FILE_VALID(p->one)) {
81e50eab 525 if (!DIFF_FILE_VALID(p->two))
60896c7b 526 continue; /* unmerged */
2f3f8b21
JH
527 else if (options->single_follow &&
528 strcmp(options->single_follow, p->two->path))
529 continue; /* not interested */
0d1e0e78 530 else if (!options->flags.rename_empty &&
02491b67 531 is_empty_blob_oid(&p->two->oid))
90d43b07 532 continue;
9db2ac56 533 else if (add_rename_dst(p) < 0) {
4d6be03b
JK
534 warning("skipping rename detection, detected"
535 " duplicate destination '%s'",
536 p->two->path);
537 goto cleanup;
538 }
2f3f8b21 539 }
0d1e0e78 540 else if (!options->flags.rename_empty &&
02491b67 541 is_empty_blob_oid(&p->one->oid))
90d43b07 542 continue;
d7c9bf22 543 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
64479711
LT
544 /*
545 * If the source is a broken "delete", and
2210100a
JH
546 * they did not really want to get broken,
547 * that means the source actually stays.
64479711
LT
548 * So we increment the "rename_used" score
549 * by one, to indicate ourselves as a user
550 */
551 if (p->broken_pair && !p->score)
552 p->one->rename_used++;
e88d6bc6 553 register_rename_src(p);
64479711 554 }
f15eb7c1 555 else if (want_copies) {
64479711
LT
556 /*
557 * Increment the "rename_used" score by
558 * one, to indicate ourselves as a user.
2210100a 559 */
64479711 560 p->one->rename_used++;
e88d6bc6 561 register_rename_src(p);
2210100a 562 }
427dcb4b 563 }
557ac035 564 trace2_region_leave("diff", "setup", options->repo);
0024a549 565 if (rename_dst_nr == 0 || rename_src_nr == 0)
427dcb4b
JH
566 goto cleanup; /* nothing to do */
567
557ac035 568 trace2_region_enter("diff", "exact renames", options->repo);
17559a64
LT
569 /*
570 * We really want to cull the candidates list early
571 * with cheap tests in order to avoid doing deltas.
572 */
11f944dd 573 rename_count = find_exact_renames(options);
557ac035 574 trace2_region_leave("diff", "exact renames", options->repo);
17559a64 575
42899ac8
LT
576 /* Did we only want exact renames? */
577 if (minimum_score == MAX_SCORE)
578 goto cleanup;
579
829514c5 580 /* Calculate how many renames are left */
26a66a6b 581 num_destinations = (rename_dst_nr - rename_count);
829514c5 582 remove_unneeded_paths_from_src(want_copies);
f15eb7c1 583 num_sources = rename_src_nr;
42899ac8
LT
584
585 /* All done? */
f15eb7c1 586 if (!num_destinations || !num_sources)
42899ac8
LT
587 goto cleanup;
588
f15eb7c1 589 switch (too_many_rename_candidates(num_destinations, num_sources,
00b8cccd 590 options)) {
f31027c9 591 case 1:
0024a549 592 goto cleanup;
f31027c9
JH
593 case 2:
594 options->degraded_cc_to_c = 1;
595 skip_unmodified = 1;
596 break;
597 default:
598 break;
599 }
0024a549 600
557ac035 601 trace2_region_enter("diff", "inexact renames", options->repo);
3ac942d4 602 if (options->show_rename_progress) {
8aade107 603 progress = start_delayed_progress(
754dbc43 604 _("Performing inexact rename detection"),
f15eb7c1 605 (uint64_t)num_destinations * (uint64_t)num_sources);
3ac942d4
JK
606 }
607
26a66a6b
EN
608 mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_destinations),
609 sizeof(*mx));
25d5ea41 610 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
9db2ac56 611 struct diff_filespec *two = rename_dst[i].p->two;
6d24ad97
JH
612 struct diff_score *m;
613
9db2ac56 614 if (rename_dst[i].is_rename)
427dcb4b 615 continue; /* dealt with exact match already. */
6d24ad97
JH
616
617 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
618 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
619 m[j].dst = -1;
620
25d5ea41 621 for (j = 0; j < rename_src_nr; j++) {
e88d6bc6 622 struct diff_filespec *one = rename_src[j].p->one;
6d24ad97 623 struct diff_score this_src;
f31027c9 624
829514c5 625 assert(!one->rename_used || want_copies || break_idx);
f15eb7c1 626
f31027c9
JH
627 if (skip_unmodified &&
628 diff_unmodified_pair(rename_src[j].p))
629 continue;
630
b78ea5fc
NTND
631 this_src.score = estimate_similarity(options->repo,
632 one, two,
95acf11a
JT
633 minimum_score,
634 skip_unmodified);
6d24ad97
JH
635 this_src.name_score = basename_same(one, two);
636 this_src.dst = i;
637 this_src.src = j;
638 record_if_better(m, &this_src);
809809bb
JH
639 /*
640 * Once we run estimate_similarity,
641 * We do not need the text anymore.
642 */
8ae92e63 643 diff_free_filespec_blob(one);
809809bb 644 diff_free_filespec_blob(two);
427dcb4b
JH
645 }
646 dst_cnt++;
81c4bf02 647 display_progress(progress,
f15eb7c1 648 (uint64_t)dst_cnt * (uint64_t)num_sources);
427dcb4b 649 }
3ac942d4 650 stop_progress(&progress);
6d24ad97 651
427dcb4b 652 /* cost matrix sorted by most to least similar pair */
2049b8dc 653 STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
6d24ad97 654
0940e5f2 655 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
f15eb7c1 656 if (want_copies)
0940e5f2 657 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
427dcb4b 658 free(mx);
557ac035 659 trace2_region_leave("diff", "inexact renames", options->repo);
427dcb4b 660
15d061b4 661 cleanup:
427dcb4b 662 /* At this point, we have found some renames and copies and they
5098bafb 663 * are recorded in rename_dst. The original list is still in *q.
427dcb4b 664 */
557ac035 665 trace2_region_enter("diff", "write back to queue", options->repo);
9ca5df90 666 DIFF_QUEUE_CLEAR(&outq);
427dcb4b 667 for (i = 0; i < q->nr; i++) {
25d5ea41 668 struct diff_filepair *p = q->queue[i];
25d5ea41
JH
669 struct diff_filepair *pair_to_free = NULL;
670
d7c9bf22
MZ
671 if (DIFF_PAIR_UNMERGED(p)) {
672 diff_q(&outq, p);
673 }
674 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
9db2ac56
EN
675 /* Creation */
676 diff_q(&outq, p);
427dcb4b 677 }
2cd68882
JH
678 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
679 /*
680 * Deletion
681 *
f345b0a0
JH
682 * We would output this delete record if:
683 *
684 * (1) this is a broken delete and the counterpart
685 * broken create remains in the output; or
5098bafb
JH
686 * (2) this is not a broken delete, and rename_dst
687 * does not have a rename/copy to move p->one->path
688 * out of existence.
f345b0a0
JH
689 *
690 * Otherwise, the counterpart broken create
691 * has been turned into a rename-edit; or
692 * delete did not have a matching create to
693 * begin with.
2cd68882 694 */
f345b0a0
JH
695 if (DIFF_PAIR_BROKEN(p)) {
696 /* broken delete */
9db2ac56
EN
697 struct diff_rename_dst *dst = locate_rename_dst(p);
698 if (!dst)
699 BUG("tracking failed somehow; failed to find associated dst for broken pair");
700 if (dst->is_rename)
f345b0a0
JH
701 /* counterpart is now rename/copy */
702 pair_to_free = p;
703 }
704 else {
64479711 705 if (p->one->rename_used)
f345b0a0
JH
706 /* this path remains */
707 pair_to_free = p;
708 }
2cd68882 709
9db2ac56 710 if (!pair_to_free)
2cd68882
JH
711 diff_q(&outq, p);
712 }
25d5ea41 713 else if (!diff_unmodified_pair(p))
15d061b4 714 /* all the usual ones need to be kept */
25d5ea41 715 diff_q(&outq, p);
15d061b4 716 else
9db2ac56 717 /* no need to keep unmodified pairs; FIXME: remove earlier? */
15d061b4
JH
718 pair_to_free = p;
719
226406f6
JH
720 if (pair_to_free)
721 diff_free_filepair(pair_to_free);
427dcb4b 722 }
25d5ea41 723 diff_debug_queue("done copying original", &outq);
427dcb4b 724
25d5ea41
JH
725 free(q->queue);
726 *q = outq;
727 diff_debug_queue("done collapsing", q);
427dcb4b 728
9fb88419 729 for (i = 0; i < rename_dst_nr; i++)
9db2ac56
EN
730 if (rename_dst[i].filespec_to_free)
731 free_filespec(rename_dst[i].filespec_to_free);
5098bafb 732
6a83d902 733 FREE_AND_NULL(rename_dst);
25d5ea41 734 rename_dst_nr = rename_dst_alloc = 0;
6a83d902 735 FREE_AND_NULL(rename_src);
25d5ea41 736 rename_src_nr = rename_src_alloc = 0;
9db2ac56
EN
737 if (break_idx) {
738 strintmap_clear(break_idx);
739 FREE_AND_NULL(break_idx);
740 }
557ac035 741 trace2_region_leave("diff", "write back to queue", options->repo);
427dcb4b
JH
742 return;
743}