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