blob: df41be56deab60d4d39a45920a1e62b05d0474f6 [file] [log] [blame]
Junio C Hamano427dcb42005-05-21 02:39:09 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Linus Torvalds9027f532007-10-25 11:23:26 -07007#include "hash.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -07008
Junio C Hamano25d5ea42005-05-24 01:10:48 -07009/* Table of rename/copy destinations */
Junio C Hamano427dcb42005-05-21 02:39:09 -070010
Junio C Hamano25d5ea42005-05-24 01:10:48 -070011static struct diff_rename_dst {
12 struct diff_filespec *two;
13 struct diff_filepair *pair;
14} *rename_dst;
15static int rename_dst_nr, rename_dst_alloc;
16
17static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
18 int insert_ok)
Junio C Hamano427dcb42005-05-21 02:39:09 -070019{
Junio C Hamano25d5ea42005-05-24 01:10:48 -070020 int first, last;
21
22 first = 0;
23 last = rename_dst_nr;
24 while (last > first) {
25 int next = (last + first) >> 1;
26 struct diff_rename_dst *dst = &(rename_dst[next]);
27 int cmp = strcmp(two->path, dst->two->path);
28 if (!cmp)
29 return dst;
30 if (cmp < 0) {
31 last = next;
32 continue;
33 }
34 first = next+1;
35 }
36 /* not found */
37 if (!insert_ok)
38 return NULL;
39 /* insert to make it at "first" */
40 if (rename_dst_alloc <= rename_dst_nr) {
41 rename_dst_alloc = alloc_nr(rename_dst_alloc);
42 rename_dst = xrealloc(rename_dst,
43 rename_dst_alloc * sizeof(*rename_dst));
44 }
45 rename_dst_nr++;
46 if (first < rename_dst_nr)
47 memmove(rename_dst + first + 1, rename_dst + first,
48 (rename_dst_nr - first - 1) * sizeof(*rename_dst));
Junio C Hamano5098baf2005-09-15 16:13:43 -070049 rename_dst[first].two = alloc_filespec(two->path);
50 fill_filespec(rename_dst[first].two, two->sha1, two->mode);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070051 rename_dst[first].pair = NULL;
52 return &(rename_dst[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070053}
54
Junio C Hamano15d061b2005-05-27 15:55:55 -070055/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070056static struct diff_rename_src {
57 struct diff_filespec *one;
Junio C Hamanofc580712006-04-08 20:17:46 -070058 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070059} *rename_src;
60static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070061
Junio C Hamano15d061b2005-05-27 15:55:55 -070062static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
Junio C Hamanofc580712006-04-08 20:17:46 -070063 unsigned short score)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070064{
65 int first, last;
66
67 first = 0;
68 last = rename_src_nr;
69 while (last > first) {
70 int next = (last + first) >> 1;
71 struct diff_rename_src *src = &(rename_src[next]);
72 int cmp = strcmp(one->path, src->one->path);
73 if (!cmp)
74 return src;
75 if (cmp < 0) {
76 last = next;
77 continue;
78 }
79 first = next+1;
Junio C Hamano427dcb42005-05-21 02:39:09 -070080 }
Junio C Hamano15d061b2005-05-27 15:55:55 -070081
Junio C Hamano25d5ea42005-05-24 01:10:48 -070082 /* insert to make it at "first" */
83 if (rename_src_alloc <= rename_src_nr) {
84 rename_src_alloc = alloc_nr(rename_src_alloc);
85 rename_src = xrealloc(rename_src,
86 rename_src_alloc * sizeof(*rename_src));
87 }
88 rename_src_nr++;
89 if (first < rename_src_nr)
90 memmove(rename_src + first + 1, rename_src + first,
91 (rename_src_nr - first - 1) * sizeof(*rename_src));
92 rename_src[first].one = one;
Junio C Hamanofc580712006-04-08 20:17:46 -070093 rename_src[first].score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070094 return &(rename_src[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070095}
96
Johannes Schindelin0ce39642007-06-21 12:52:11 +010097static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
98{
99 int src_len = strlen(src->path), dst_len = strlen(dst->path);
100 while (src_len && dst_len) {
101 char c1 = src->path[--src_len];
102 char c2 = dst->path[--dst_len];
103 if (c1 != c2)
104 return 0;
105 if (c1 == '/')
106 return 1;
107 }
108 return (!src_len || src->path[src_len - 1] == '/') &&
109 (!dst_len || dst->path[dst_len - 1] == '/');
110}
111
Junio C Hamano427dcb42005-05-21 02:39:09 -0700112struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700113 int src; /* index in rename_src */
114 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800115 unsigned short score;
116 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700117};
118
119static int estimate_similarity(struct diff_filespec *src,
120 struct diff_filespec *dst,
121 int minimum_score)
122{
123 /* src points at a file that existed in the original tree (or
124 * optionally a file in the destination tree) and dst points
125 * at a newly created file. They may be quite similar, in which
126 * case we want to say src is renamed to dst or src is copied into
127 * dst, and then some edit has been applied to dst.
128 *
129 * Compare them and return how similar they are, representing
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700130 * the score as an integer between 0 and MAX_SCORE.
131 *
132 * When there is an exact match, it is considered a better
133 * match than anything else; the destination does not even
134 * call into this function in that case.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700135 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800136 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano75c660a2005-06-28 16:58:27 -0700137 unsigned long delta_limit;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700138 int score;
139
Junio C Hamano60896c72005-05-22 21:24:49 -0700140 /* We deal only with regular files. Symlink renames are handled
141 * only when they are exact matches --- in other words, no edits
142 * after renaming.
143 */
144 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
145 return 0;
146
Linus Torvalds81ac0512007-10-26 16:51:28 -0700147 /*
148 * Need to check that source and destination sizes are
149 * filled in before comparing them.
150 *
151 * If we already have "cnt_data" filled in, we know it's
152 * all good (avoid checking the size for zero, as that
153 * is a possible size - we really should have a flag to
154 * say whether the size is valid or not!)
155 */
Björn Steinbrink885c7162009-01-20 16:59:57 +0100156 if (!src->cnt_data && diff_populate_filespec(src, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700157 return 0;
Björn Steinbrink885c7162009-01-20 16:59:57 +0100158 if (!dst->cnt_data && diff_populate_filespec(dst, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700159 return 0;
160
Linus Torvalds90bd9322006-03-12 22:26:34 -0800161 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700162 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800163 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700164
Junio C Hamano58b103f2005-05-21 15:55:18 -0700165 /* We would not consider edits that change the file size so
166 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700167 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700168 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700169 * Note that base_size == 0 case is handled here already
170 * and the final score computation below would not have a
171 * divide-by-zero issue.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700172 */
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700173 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700174 return 0;
175
Björn Steinbrink885c7162009-01-20 16:59:57 +0100176 if (!src->cnt_data && diff_populate_filespec(src, 0))
177 return 0;
178 if (!dst->cnt_data && diff_populate_filespec(dst, 0))
179 return 0;
180
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500181 delta_limit = (unsigned long)
182 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
Junio C Hamanod8c3d032007-06-28 22:54:37 -0700183 if (diffcore_count_changes(src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800184 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800185 delta_limit,
186 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700187 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700188
Junio C Hamano17063062006-03-02 22:11:25 -0800189 /* How similar are they?
190 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700191 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800192 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800193 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200194 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500195 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700196 return score;
197}
198
Junio C Hamano5098baf2005-09-15 16:13:43 -0700199static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700200{
Linus Torvalds9fb88412007-10-25 11:19:10 -0700201 struct diff_filespec *src, *dst;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700202 struct diff_filepair *dp;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700203
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700204 if (rename_dst[dst_index].pair)
205 die("internal error: dst already matched.");
206
207 src = rename_src[src_index].one;
Linus Torvalds64479712007-10-25 11:20:56 -0700208 src->rename_used++;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700209 src->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700210
211 dst = rename_dst[dst_index].two;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700212 dst->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700213
Linus Torvalds9fb88412007-10-25 11:19:10 -0700214 dp = diff_queue(NULL, src, dst);
Junio C Hamanoef677682006-08-03 12:01:01 -0700215 dp->renamed_pair = 1;
Junio C Hamanofc580712006-04-08 20:17:46 -0700216 if (!strcmp(src->path, dst->path))
217 dp->score = rename_src[src_index].score;
218 else
219 dp->score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700220 rename_dst[dst_index].pair = dp;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700221}
222
223/*
224 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700225 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700226 */
227static int score_compare(const void *a_, const void *b_)
228{
229 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200230
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800231 /* sink the unused ones to the bottom */
232 if (a->dst < 0)
233 return (0 <= b->dst);
234 else if (b->dst < 0)
235 return -1;
236
René Scharfecfc0aef2007-06-25 00:23:28 +0200237 if (a->score == b->score)
238 return b->name_score - a->name_score;
239
Junio C Hamano427dcb42005-05-21 02:39:09 -0700240 return b->score - a->score;
241}
242
Linus Torvalds9027f532007-10-25 11:23:26 -0700243struct file_similarity {
244 int src_dst, index;
245 struct diff_filespec *filespec;
246 struct file_similarity *next;
247};
248
249static int find_identical_files(struct file_similarity *src,
250 struct file_similarity *dst)
251{
252 int renames = 0;
253
254 /*
255 * Walk over all the destinations ...
256 */
257 do {
Linus Torvalds32d75d22007-11-29 13:30:13 -0800258 struct diff_filespec *target = dst->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700259 struct file_similarity *p, *best;
Linus Torvalds32d75d22007-11-29 13:30:13 -0800260 int i = 100, best_score = -1;
Linus Torvalds9027f532007-10-25 11:23:26 -0700261
262 /*
263 * .. to find the best source match
264 */
265 best = NULL;
266 for (p = src; p; p = p->next) {
Linus Torvalds32d75d22007-11-29 13:30:13 -0800267 int score;
268 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700269
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100270 /* False hash collision? */
Linus Torvalds32d75d22007-11-29 13:30:13 -0800271 if (hashcmp(source->sha1, target->sha1))
Linus Torvalds9027f532007-10-25 11:23:26 -0700272 continue;
273 /* Non-regular files? If so, the modes must match! */
Linus Torvalds32d75d22007-11-29 13:30:13 -0800274 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
275 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700276 continue;
277 }
Linus Torvalds32d75d22007-11-29 13:30:13 -0800278 /* Give higher scores to sources that haven't been used already */
279 score = !source->rename_used;
280 score += basename_same(source, target);
281 if (score > best_score) {
282 best = p;
283 best_score = score;
284 if (score == 2)
285 break;
286 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700287
288 /* Too many identical alternatives? Pick one */
289 if (!--i)
290 break;
291 }
292 if (best) {
293 record_rename_pair(dst->index, best->index, MAX_SCORE);
294 renames++;
295 }
296 } while ((dst = dst->next) != NULL);
297 return renames;
298}
299
Linus Torvalds9027f532007-10-25 11:23:26 -0700300static void free_similarity_list(struct file_similarity *p)
301{
302 while (p) {
303 struct file_similarity *entry = p;
304 p = p->next;
Linus Torvalds9027f532007-10-25 11:23:26 -0700305 free(entry);
306 }
307}
308
309static int find_same_files(void *ptr)
310{
311 int ret;
312 struct file_similarity *p = ptr;
313 struct file_similarity *src = NULL, *dst = NULL;
314
315 /* Split the hash list up into sources and destinations */
316 do {
317 struct file_similarity *entry = p;
318 p = p->next;
319 if (entry->src_dst < 0) {
320 entry->next = src;
321 src = entry;
322 } else {
323 entry->next = dst;
324 dst = entry;
325 }
326 } while (p);
327
328 /*
329 * If we have both sources *and* destinations, see if
330 * we can match them up
331 */
332 ret = (src && dst) ? find_identical_files(src, dst) : 0;
333
334 /* Free the hashes and return the number of renames found */
335 free_similarity_list(src);
336 free_similarity_list(dst);
337 return ret;
338}
339
340static unsigned int hash_filespec(struct diff_filespec *filespec)
341{
342 unsigned int hash;
343 if (!filespec->sha1_valid) {
344 if (diff_populate_filespec(filespec, 0))
345 return 0;
346 hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
347 }
348 memcpy(&hash, filespec->sha1, sizeof(hash));
349 return hash;
350}
351
352static void insert_file_table(struct hash_table *table, int src_dst, int index, struct diff_filespec *filespec)
353{
354 void **pos;
355 unsigned int hash;
356 struct file_similarity *entry = xmalloc(sizeof(*entry));
357
358 entry->src_dst = src_dst;
359 entry->index = index;
360 entry->filespec = filespec;
361 entry->next = NULL;
362
363 hash = hash_filespec(filespec);
364 pos = insert_hash(hash, entry, table);
365
366 /* We already had an entry there? */
367 if (pos) {
368 entry->next = *pos;
369 *pos = entry;
370 }
371}
372
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700373/*
374 * Find exact renames first.
375 *
376 * The first round matches up the up-to-date entries,
377 * and then during the second round we try to match
378 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700379 */
380static int find_exact_renames(void)
381{
Linus Torvalds9027f532007-10-25 11:23:26 -0700382 int i;
383 struct hash_table file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700384
Linus Torvalds9027f532007-10-25 11:23:26 -0700385 init_hash(&file_table);
386 for (i = 0; i < rename_src_nr; i++)
387 insert_file_table(&file_table, -1, i, rename_src[i].one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700388
Linus Torvalds9027f532007-10-25 11:23:26 -0700389 for (i = 0; i < rename_dst_nr; i++)
390 insert_file_table(&file_table, 1, i, rename_dst[i].two);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700391
Linus Torvalds9027f532007-10-25 11:23:26 -0700392 /* Find the renames */
393 i = for_each_hash(&file_table, find_same_files);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700394
Linus Torvalds9027f532007-10-25 11:23:26 -0700395 /* .. and free the hash data structure */
396 free_hash(&file_table);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700397
Linus Torvalds9027f532007-10-25 11:23:26 -0700398 return i;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700399}
400
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800401#define NUM_CANDIDATE_PER_DST 4
402static void record_if_better(struct diff_score m[], struct diff_score *o)
403{
404 int i, worst;
405
406 /* find the worst one */
407 worst = 0;
408 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
409 if (score_compare(&m[i], &m[worst]) > 0)
410 worst = i;
411
412 /* is it better than the worst one? */
413 if (score_compare(&m[worst], o) > 0)
414 m[worst] = *o;
415}
416
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700417void diffcore_rename(struct diff_options *options)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700418{
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700419 int detect_rename = options->detect_rename;
420 int minimum_score = options->rename_score;
421 int rename_limit = options->rename_limit;
Junio C Hamano38c6f782005-05-21 19:40:36 -0700422 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -0700423 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700424 struct diff_score *mx;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700425 int i, j, rename_count;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700426 int num_create, num_src, dst_cnt;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700427
Junio C Hamano26dee0a2005-05-21 23:33:32 -0700428 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700429 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700430
Junio C Hamano427dcb42005-05-21 02:39:09 -0700431 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -0700432 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800433 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700434 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -0700435 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800436 else if (options->single_follow &&
437 strcmp(options->single_follow, p->two->path))
438 continue; /* not interested */
Junio C Hamano427dcb42005-05-21 02:39:09 -0700439 else
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700440 locate_rename_dst(p->two, 1);
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800441 }
Junio C Hamano22101002005-06-11 20:55:20 -0700442 else if (!DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -0700443 /*
444 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -0700445 * they did not really want to get broken,
446 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -0700447 * So we increment the "rename_used" score
448 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -0700449 */
Linus Torvalds64479712007-10-25 11:20:56 -0700450 if (p->broken_pair && !p->score)
451 p->one->rename_used++;
452 register_rename_src(p->one, p->score);
Junio C Hamano22101002005-06-11 20:55:20 -0700453 }
Linus Torvalds64479712007-10-25 11:20:56 -0700454 else if (detect_rename == DIFF_DETECT_COPY) {
455 /*
456 * Increment the "rename_used" score by
457 * one, to indicate ourselves as a user.
458 */
459 p->one->rename_used++;
460 register_rename_src(p->one, p->score);
461 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700462 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700463 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700464 goto cleanup; /* nothing to do */
465
Linus Torvalds0024a542007-09-14 10:39:48 -0700466 /*
Linus Torvalds17559a62007-10-25 11:24:47 -0700467 * We really want to cull the candidates list early
468 * with cheap tests in order to avoid doing deltas.
469 */
470 rename_count = find_exact_renames();
471
Linus Torvalds42899ac2007-10-26 16:56:34 -0700472 /* Did we only want exact renames? */
473 if (minimum_score == MAX_SCORE)
474 goto cleanup;
475
476 /*
477 * Calculate how many renames are left (but all the source
478 * files still remain as options for rename/copies!)
479 */
480 num_create = (rename_dst_nr - rename_count);
481 num_src = rename_src_nr;
482
483 /* All done? */
484 if (!num_create)
485 goto cleanup;
486
Linus Torvalds17559a62007-10-25 11:24:47 -0700487 /*
Linus Torvalds0024a542007-09-14 10:39:48 -0700488 * This basically does a test for the rename matrix not
489 * growing larger than a "rename_limit" square matrix, ie:
490 *
Linus Torvalds42899ac2007-10-26 16:56:34 -0700491 * num_create * num_src > rename_limit * rename_limit
Linus Torvalds0024a542007-09-14 10:39:48 -0700492 *
493 * but handles the potential overflow case specially (and we
494 * assume at least 32-bit integers)
495 */
496 if (rename_limit <= 0 || rename_limit > 32767)
497 rename_limit = 32767;
Jeff Kingee542ee2008-03-01 01:14:31 -0500498 if ((num_create > rename_limit && num_src > rename_limit) ||
499 (num_create * num_src > rename_limit * rename_limit)) {
Jeff Kingb8960bb2008-04-30 13:25:53 -0400500 if (options->warn_on_too_large_rename)
Linus Torvalds6e381d32008-10-27 13:06:16 -0700501 warning("too many files (created: %d deleted: %d), skipping inexact rename detection", num_create, num_src);
Linus Torvalds0024a542007-09-14 10:39:48 -0700502 goto cleanup;
Jeff Kingee542ee2008-03-01 01:14:31 -0500503 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700504
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800505 mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700506 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700507 struct diff_filespec *two = rename_dst[i].two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800508 struct diff_score *m;
509
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700510 if (rename_dst[i].pair)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700511 continue; /* dealt with exact match already. */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800512
513 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
514 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
515 m[j].dst = -1;
516
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700517 for (j = 0; j < rename_src_nr; j++) {
518 struct diff_filespec *one = rename_src[j].one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800519 struct diff_score this_src;
520 this_src.score = estimate_similarity(one, two,
521 minimum_score);
522 this_src.name_score = basename_same(one, two);
523 this_src.dst = i;
524 this_src.src = j;
525 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -0800526 /*
527 * Once we run estimate_similarity,
528 * We do not need the text anymore.
529 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -0700530 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -0800531 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700532 }
533 dst_cnt++;
534 }
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800535
Junio C Hamano427dcb42005-05-21 02:39:09 -0700536 /* cost matrix sorted by most to least similar pair */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800537 qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
538
539 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
540 struct diff_rename_dst *dst;
541
542 if ((mx[i].dst < 0) ||
543 (mx[i].score < minimum_score))
544 break; /* there is no more usable pair. */
545 dst = &rename_dst[mx[i].dst];
Linus Torvalds9ae8fcb2007-11-29 16:41:09 -0800546 if (dst->pair)
547 continue; /* already done, either exact or fuzzy. */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800548 if (rename_src[mx[i].src].one->rename_used)
Linus Torvalds9ae8fcb2007-11-29 16:41:09 -0800549 continue;
550 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
551 rename_count++;
552 }
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800553
554 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
555 struct diff_rename_dst *dst;
556
557 if ((mx[i].dst < 0) ||
558 (mx[i].score < minimum_score))
559 break; /* there is no more usable pair. */
560 dst = &rename_dst[mx[i].dst];
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700561 if (dst->pair)
562 continue; /* already done, either exact or fuzzy. */
Junio C Hamano5098baf2005-09-15 16:13:43 -0700563 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
564 rename_count++;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700565 }
566 free(mx);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700567
Junio C Hamano15d061b2005-05-27 15:55:55 -0700568 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -0700569 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -0700570 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700571 */
Bo Yang9ca5df92010-05-06 21:52:27 -0700572 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700573 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700574 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700575 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700576
Junio C Hamano2cd68882005-05-30 00:08:07 -0700577 if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
578 /*
579 * Creation
580 *
581 * We would output this create record if it has
582 * not been turned into a rename/copy already.
583 */
584 struct diff_rename_dst *dst =
585 locate_rename_dst(p->two, 0);
586 if (dst && dst->pair) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700587 diff_q(&outq, dst->pair);
588 pair_to_free = p;
589 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700590 else
Junio C Hamano2cd68882005-05-30 00:08:07 -0700591 /* no matching rename/copy source, so
592 * record this as a creation.
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700593 */
594 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700595 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700596 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
597 /*
598 * Deletion
599 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700600 * We would output this delete record if:
601 *
602 * (1) this is a broken delete and the counterpart
603 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -0700604 * (2) this is not a broken delete, and rename_dst
605 * does not have a rename/copy to move p->one->path
606 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700607 *
608 * Otherwise, the counterpart broken create
609 * has been turned into a rename-edit; or
610 * delete did not have a matching create to
611 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -0700612 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700613 if (DIFF_PAIR_BROKEN(p)) {
614 /* broken delete */
615 struct diff_rename_dst *dst =
616 locate_rename_dst(p->one, 0);
617 if (dst && dst->pair)
618 /* counterpart is now rename/copy */
619 pair_to_free = p;
620 }
621 else {
Linus Torvalds64479712007-10-25 11:20:56 -0700622 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700623 /* this path remains */
624 pair_to_free = p;
625 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700626
627 if (pair_to_free)
628 ;
629 else
630 diff_q(&outq, p);
631 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700632 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -0700633 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700634 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -0700635 else
636 /* no need to keep unmodified pairs */
637 pair_to_free = p;
638
Junio C Hamano226406f2005-05-27 15:50:30 -0700639 if (pair_to_free)
640 diff_free_filepair(pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700641 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700642 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700643
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700644 free(q->queue);
645 *q = outq;
646 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700647
Linus Torvalds9fb88412007-10-25 11:19:10 -0700648 for (i = 0; i < rename_dst_nr; i++)
649 free_filespec(rename_dst[i].two);
Junio C Hamano5098baf2005-09-15 16:13:43 -0700650
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700651 free(rename_dst);
652 rename_dst = NULL;
653 rename_dst_nr = rename_dst_alloc = 0;
654 free(rename_src);
655 rename_src = NULL;
656 rename_src_nr = rename_src_alloc = 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700657 return;
658}