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