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