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