blob: 512d0ac5fd2bc0acfb57147a6eb77f61f92b7c7e [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"
Jeff King3ac942d2011-02-20 04:51:16 -05008#include "progress.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -07009
Junio C Hamano25d5ea42005-05-24 01:10:48 -070010/* Table of rename/copy destinations */
Junio C Hamano427dcb42005-05-21 02:39:09 -070011
Junio C Hamano25d5ea42005-05-24 01:10:48 -070012static struct diff_rename_dst {
13 struct diff_filespec *two;
14 struct diff_filepair *pair;
15} *rename_dst;
16static int rename_dst_nr, rename_dst_alloc;
17
18static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19 int insert_ok)
Junio C Hamano427dcb42005-05-21 02:39:09 -070020{
Junio C Hamano25d5ea42005-05-24 01:10:48 -070021 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));
Junio C Hamano5098baf2005-09-15 16:13:43 -070050 rename_dst[first].two = alloc_filespec(two->path);
Jeff Kinge5450102012-07-28 11:03:01 -040051 fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070052 rename_dst[first].pair = NULL;
53 return &(rename_dst[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070054}
55
Junio C Hamano15d061b2005-05-27 15:55:55 -070056/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070057static struct diff_rename_src {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080058 struct diff_filepair *p;
Junio C Hamanofc580712006-04-08 20:17:46 -070059 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070060} *rename_src;
61static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070062
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080063static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070064{
65 int first, last;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080066 struct diff_filespec *one = p->one;
67 unsigned short score = p->score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070068
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]);
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080074 int cmp = strcmp(one->path, src->p->one->path);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070075 if (!cmp)
76 return src;
77 if (cmp < 0) {
78 last = next;
79 continue;
80 }
81 first = next+1;
Junio C Hamano427dcb42005-05-21 02:39:09 -070082 }
Junio C Hamano15d061b2005-05-27 15:55:55 -070083
Junio C Hamano25d5ea42005-05-24 01:10:48 -070084 /* 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));
89 }
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));
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080094 rename_src[first].p = p;
Junio C Hamanofc580712006-04-08 20:17:46 -070095 rename_src[first].score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070096 return &(rename_src[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070097}
98
Johannes Schindelin0ce39642007-06-21 12:52:11 +010099static 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
Junio C Hamano427dcb42005-05-21 02:39:09 -0700114struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700115 int src; /* index in rename_src */
116 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800117 unsigned short score;
118 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700119};
120
121static 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
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700132 * 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.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700137 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800138 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano75c660a2005-06-28 16:58:27 -0700139 unsigned long delta_limit;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700140 int score;
141
Junio C Hamano60896c72005-05-22 21:24:49 -0700142 /* 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
Linus Torvalds81ac0512007-10-26 16:51:28 -0700149 /*
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 */
Björn Steinbrink885c7162009-01-20 16:59:57 +0100158 if (!src->cnt_data && diff_populate_filespec(src, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700159 return 0;
Björn Steinbrink885c7162009-01-20 16:59:57 +0100160 if (!dst->cnt_data && diff_populate_filespec(dst, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700161 return 0;
162
Linus Torvalds90bd9322006-03-12 22:26:34 -0800163 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700164 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800165 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700166
Junio C Hamano58b103f2005-05-21 15:55:18 -0700167 /* We would not consider edits that change the file size so
168 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700169 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700170 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700171 * 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.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700174 */
Linus Torvalds3a4d6762011-02-18 20:12:06 -0800175 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700176 return 0;
177
Björn Steinbrink885c7162009-01-20 16:59:57 +0100178 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
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500183 delta_limit = (unsigned long)
184 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
Junio C Hamanod8c3d032007-06-28 22:54:37 -0700185 if (diffcore_count_changes(src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800186 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800187 delta_limit,
188 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700189 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700190
Junio C Hamano17063062006-03-02 22:11:25 -0800191 /* How similar are they?
192 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700193 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800194 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800195 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200196 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500197 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700198 return score;
199}
200
Junio C Hamano5098baf2005-09-15 16:13:43 -0700201static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700202{
Linus Torvalds9fb88412007-10-25 11:19:10 -0700203 struct diff_filespec *src, *dst;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700204 struct diff_filepair *dp;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700205
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700206 if (rename_dst[dst_index].pair)
207 die("internal error: dst already matched.");
208
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800209 src = rename_src[src_index].p->one;
Linus Torvalds64479712007-10-25 11:20:56 -0700210 src->rename_used++;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700211 src->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700212
213 dst = rename_dst[dst_index].two;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700214 dst->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700215
Linus Torvalds9fb88412007-10-25 11:19:10 -0700216 dp = diff_queue(NULL, src, dst);
Junio C Hamanoef677682006-08-03 12:01:01 -0700217 dp->renamed_pair = 1;
Junio C Hamanofc580712006-04-08 20:17:46 -0700218 if (!strcmp(src->path, dst->path))
219 dp->score = rename_src[src_index].score;
220 else
221 dp->score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700222 rename_dst[dst_index].pair = dp;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700223}
224
225/*
226 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700227 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700228 */
229static int score_compare(const void *a_, const void *b_)
230{
231 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200232
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800233 /* 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
René Scharfecfc0aef2007-06-25 00:23:28 +0200239 if (a->score == b->score)
240 return b->name_score - a->name_score;
241
Junio C Hamano427dcb42005-05-21 02:39:09 -0700242 return b->score - a->score;
243}
244
Linus Torvalds9027f532007-10-25 11:23:26 -0700245struct file_similarity {
246 int src_dst, index;
247 struct diff_filespec *filespec;
248 struct file_similarity *next;
249};
250
251static int find_identical_files(struct file_similarity *src,
Linus Torvalds11f944d2011-02-18 19:55:19 -0800252 struct file_similarity *dst,
253 struct diff_options *options)
Linus Torvalds9027f532007-10-25 11:23:26 -0700254{
255 int renames = 0;
256
257 /*
258 * Walk over all the destinations ...
259 */
260 do {
Linus Torvalds32d75d22007-11-29 13:30:13 -0800261 struct diff_filespec *target = dst->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700262 struct file_similarity *p, *best;
Linus Torvalds32d75d22007-11-29 13:30:13 -0800263 int i = 100, best_score = -1;
Linus Torvalds9027f532007-10-25 11:23:26 -0700264
265 /*
266 * .. to find the best source match
267 */
268 best = NULL;
269 for (p = src; p; p = p->next) {
Linus Torvalds32d75d22007-11-29 13:30:13 -0800270 int score;
271 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700272
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100273 /* False hash collision? */
Linus Torvalds32d75d22007-11-29 13:30:13 -0800274 if (hashcmp(source->sha1, target->sha1))
Linus Torvalds9027f532007-10-25 11:23:26 -0700275 continue;
276 /* Non-regular files? If so, the modes must match! */
Linus Torvalds32d75d22007-11-29 13:30:13 -0800277 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
278 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700279 continue;
280 }
Linus Torvalds32d75d22007-11-29 13:30:13 -0800281 /* Give higher scores to sources that haven't been used already */
282 score = !source->rename_used;
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800283 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
284 continue;
Linus Torvalds32d75d22007-11-29 13:30:13 -0800285 score += basename_same(source, target);
286 if (score > best_score) {
287 best = p;
288 best_score = score;
289 if (score == 2)
290 break;
291 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700292
293 /* Too many identical alternatives? Pick one */
294 if (!--i)
295 break;
296 }
297 if (best) {
298 record_rename_pair(dst->index, best->index, MAX_SCORE);
299 renames++;
300 }
301 } while ((dst = dst->next) != NULL);
302 return renames;
303}
304
Linus Torvalds9027f532007-10-25 11:23:26 -0700305static void free_similarity_list(struct file_similarity *p)
306{
307 while (p) {
308 struct file_similarity *entry = p;
309 p = p->next;
Linus Torvalds9027f532007-10-25 11:23:26 -0700310 free(entry);
311 }
312}
313
Linus Torvalds11f944d2011-02-18 19:55:19 -0800314static int find_same_files(void *ptr, void *data)
Linus Torvalds9027f532007-10-25 11:23:26 -0700315{
316 int ret;
317 struct file_similarity *p = ptr;
318 struct file_similarity *src = NULL, *dst = NULL;
Linus Torvalds11f944d2011-02-18 19:55:19 -0800319 struct diff_options *options = data;
Linus Torvalds9027f532007-10-25 11:23:26 -0700320
321 /* Split the hash list up into sources and destinations */
322 do {
323 struct file_similarity *entry = p;
324 p = p->next;
325 if (entry->src_dst < 0) {
326 entry->next = src;
327 src = entry;
328 } else {
329 entry->next = dst;
330 dst = entry;
331 }
332 } while (p);
333
334 /*
335 * If we have both sources *and* destinations, see if
336 * we can match them up
337 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800338 ret = (src && dst) ? find_identical_files(src, dst, options) : 0;
Linus Torvalds9027f532007-10-25 11:23:26 -0700339
340 /* Free the hashes and return the number of renames found */
341 free_similarity_list(src);
342 free_similarity_list(dst);
343 return ret;
344}
345
346static unsigned int hash_filespec(struct diff_filespec *filespec)
347{
348 unsigned int hash;
349 if (!filespec->sha1_valid) {
350 if (diff_populate_filespec(filespec, 0))
351 return 0;
352 hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
353 }
354 memcpy(&hash, filespec->sha1, sizeof(hash));
355 return hash;
356}
357
358static void insert_file_table(struct hash_table *table, int src_dst, int index, struct diff_filespec *filespec)
359{
360 void **pos;
361 unsigned int hash;
362 struct file_similarity *entry = xmalloc(sizeof(*entry));
363
364 entry->src_dst = src_dst;
365 entry->index = index;
366 entry->filespec = filespec;
367 entry->next = NULL;
368
369 hash = hash_filespec(filespec);
370 pos = insert_hash(hash, entry, table);
371
372 /* We already had an entry there? */
373 if (pos) {
374 entry->next = *pos;
375 *pos = entry;
376 }
377}
378
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700379/*
380 * Find exact renames first.
381 *
382 * The first round matches up the up-to-date entries,
383 * and then during the second round we try to match
384 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700385 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800386static int find_exact_renames(struct diff_options *options)
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700387{
Linus Torvalds9027f532007-10-25 11:23:26 -0700388 int i;
389 struct hash_table file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700390
Linus Torvalds9027f532007-10-25 11:23:26 -0700391 init_hash(&file_table);
392 for (i = 0; i < rename_src_nr; i++)
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800393 insert_file_table(&file_table, -1, i, rename_src[i].p->one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700394
Linus Torvalds9027f532007-10-25 11:23:26 -0700395 for (i = 0; i < rename_dst_nr; i++)
396 insert_file_table(&file_table, 1, i, rename_dst[i].two);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700397
Linus Torvalds9027f532007-10-25 11:23:26 -0700398 /* Find the renames */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800399 i = for_each_hash(&file_table, find_same_files, options);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700400
Linus Torvalds9027f532007-10-25 11:23:26 -0700401 /* .. and free the hash data structure */
402 free_hash(&file_table);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700403
Linus Torvalds9027f532007-10-25 11:23:26 -0700404 return i;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700405}
406
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800407#define NUM_CANDIDATE_PER_DST 4
408static void record_if_better(struct diff_score m[], struct diff_score *o)
409{
410 int i, worst;
411
412 /* find the worst one */
413 worst = 0;
414 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
415 if (score_compare(&m[i], &m[worst]) > 0)
416 worst = i;
417
418 /* is it better than the worst one? */
419 if (score_compare(&m[worst], o) > 0)
420 m[worst] = *o;
421}
422
Junio C Hamanof31027c2011-01-06 13:50:06 -0800423/*
424 * Returns:
425 * 0 if we are under the limit;
426 * 1 if we need to disable inexact rename detection;
427 * 2 if we would be under the limit if we were given -C instead of -C -C.
428 */
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800429static int too_many_rename_candidates(int num_create,
430 struct diff_options *options)
431{
432 int rename_limit = options->rename_limit;
433 int num_src = rename_src_nr;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800434 int i;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800435
436 options->needed_rename_limit = 0;
437
438 /*
439 * This basically does a test for the rename matrix not
440 * growing larger than a "rename_limit" square matrix, ie:
441 *
442 * num_create * num_src > rename_limit * rename_limit
443 *
444 * but handles the potential overflow case specially (and we
445 * assume at least 32-bit integers)
446 */
447 if (rename_limit <= 0 || rename_limit > 32767)
448 rename_limit = 32767;
449 if ((num_create <= rename_limit || num_src <= rename_limit) &&
450 (num_create * num_src <= rename_limit * rename_limit))
451 return 0;
452
453 options->needed_rename_limit =
454 num_src > num_create ? num_src : num_create;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800455
456 /* Are we running under -C -C? */
457 if (!DIFF_OPT_TST(options, FIND_COPIES_HARDER))
458 return 1;
459
460 /* Would we bust the limit if we were running under -C? */
461 for (num_src = i = 0; i < rename_src_nr; i++) {
462 if (diff_unmodified_pair(rename_src[i].p))
463 continue;
464 num_src++;
465 }
466 if ((num_create <= rename_limit || num_src <= rename_limit) &&
467 (num_create * num_src <= rename_limit * rename_limit))
468 return 2;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800469 return 1;
470}
471
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800472static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
473{
474 int count = 0, i;
475
476 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
477 struct diff_rename_dst *dst;
478
479 if ((mx[i].dst < 0) ||
480 (mx[i].score < minimum_score))
481 break; /* there is no more usable pair. */
482 dst = &rename_dst[mx[i].dst];
483 if (dst->pair)
484 continue; /* already done, either exact or fuzzy. */
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800485 if (!copies && rename_src[mx[i].src].p->one->rename_used)
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800486 continue;
487 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
488 count++;
489 }
490 return count;
491}
492
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700493void diffcore_rename(struct diff_options *options)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700494{
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700495 int detect_rename = options->detect_rename;
496 int minimum_score = options->rename_score;
Junio C Hamano38c6f782005-05-21 19:40:36 -0700497 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -0700498 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700499 struct diff_score *mx;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800500 int i, j, rename_count, skip_unmodified = 0;
Jim Meyeringdabdbee2011-04-29 11:42:41 +0200501 int num_create, dst_cnt;
Jeff King3ac942d2011-02-20 04:51:16 -0500502 struct progress *progress = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700503
Junio C Hamano26dee0a2005-05-21 23:33:32 -0700504 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700505 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700506
Junio C Hamano427dcb42005-05-21 02:39:09 -0700507 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -0700508 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800509 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700510 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -0700511 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800512 else if (options->single_follow &&
513 strcmp(options->single_follow, p->two->path))
514 continue; /* not interested */
Jeff King90d43b02012-03-22 18:52:13 -0400515 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
516 is_empty_blob_sha1(p->two->sha1))
517 continue;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700518 else
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700519 locate_rename_dst(p->two, 1);
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800520 }
Jeff King90d43b02012-03-22 18:52:13 -0400521 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
522 is_empty_blob_sha1(p->one->sha1))
523 continue;
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400524 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -0700525 /*
526 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -0700527 * they did not really want to get broken,
528 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -0700529 * So we increment the "rename_used" score
530 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -0700531 */
Linus Torvalds64479712007-10-25 11:20:56 -0700532 if (p->broken_pair && !p->score)
533 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800534 register_rename_src(p);
Junio C Hamano22101002005-06-11 20:55:20 -0700535 }
Linus Torvalds64479712007-10-25 11:20:56 -0700536 else if (detect_rename == DIFF_DETECT_COPY) {
537 /*
538 * Increment the "rename_used" score by
539 * one, to indicate ourselves as a user.
540 */
541 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800542 register_rename_src(p);
Linus Torvalds64479712007-10-25 11:20:56 -0700543 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700544 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700545 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700546 goto cleanup; /* nothing to do */
547
Linus Torvalds0024a542007-09-14 10:39:48 -0700548 /*
Linus Torvalds17559a62007-10-25 11:24:47 -0700549 * We really want to cull the candidates list early
550 * with cheap tests in order to avoid doing deltas.
551 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800552 rename_count = find_exact_renames(options);
Linus Torvalds17559a62007-10-25 11:24:47 -0700553
Linus Torvalds42899ac2007-10-26 16:56:34 -0700554 /* Did we only want exact renames? */
555 if (minimum_score == MAX_SCORE)
556 goto cleanup;
557
558 /*
559 * Calculate how many renames are left (but all the source
560 * files still remain as options for rename/copies!)
561 */
562 num_create = (rename_dst_nr - rename_count);
Linus Torvalds42899ac2007-10-26 16:56:34 -0700563
564 /* All done? */
565 if (!num_create)
566 goto cleanup;
567
Junio C Hamanof31027c2011-01-06 13:50:06 -0800568 switch (too_many_rename_candidates(num_create, options)) {
569 case 1:
Linus Torvalds0024a542007-09-14 10:39:48 -0700570 goto cleanup;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800571 case 2:
572 options->degraded_cc_to_c = 1;
573 skip_unmodified = 1;
574 break;
575 default:
576 break;
577 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700578
Jeff King3ac942d2011-02-20 04:51:16 -0500579 if (options->show_rename_progress) {
580 progress = start_progress_delay(
581 "Performing inexact rename detection",
582 rename_dst_nr * rename_src_nr, 50, 1);
583 }
584
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800585 mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700586 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700587 struct diff_filespec *two = rename_dst[i].two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800588 struct diff_score *m;
589
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700590 if (rename_dst[i].pair)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700591 continue; /* dealt with exact match already. */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800592
593 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
594 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
595 m[j].dst = -1;
596
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700597 for (j = 0; j < rename_src_nr; j++) {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800598 struct diff_filespec *one = rename_src[j].p->one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800599 struct diff_score this_src;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800600
601 if (skip_unmodified &&
602 diff_unmodified_pair(rename_src[j].p))
603 continue;
604
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800605 this_src.score = estimate_similarity(one, two,
606 minimum_score);
607 this_src.name_score = basename_same(one, two);
608 this_src.dst = i;
609 this_src.src = j;
610 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -0800611 /*
612 * Once we run estimate_similarity,
613 * We do not need the text anymore.
614 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -0700615 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -0800616 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700617 }
618 dst_cnt++;
Jeff King3ac942d2011-02-20 04:51:16 -0500619 display_progress(progress, (i+1)*rename_src_nr);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700620 }
Jeff King3ac942d2011-02-20 04:51:16 -0500621 stop_progress(&progress);
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800622
Junio C Hamano427dcb42005-05-21 02:39:09 -0700623 /* cost matrix sorted by most to least similar pair */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800624 qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
625
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800626 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
627 if (detect_rename == DIFF_DETECT_COPY)
628 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700629 free(mx);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700630
Junio C Hamano15d061b2005-05-27 15:55:55 -0700631 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -0700632 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -0700633 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700634 */
Bo Yang9ca5df92010-05-06 21:52:27 -0700635 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700636 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700637 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700638 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700639
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400640 if (DIFF_PAIR_UNMERGED(p)) {
641 diff_q(&outq, p);
642 }
643 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Junio C Hamano2cd68882005-05-30 00:08:07 -0700644 /*
645 * Creation
646 *
647 * We would output this create record if it has
648 * not been turned into a rename/copy already.
649 */
650 struct diff_rename_dst *dst =
651 locate_rename_dst(p->two, 0);
652 if (dst && dst->pair) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700653 diff_q(&outq, dst->pair);
654 pair_to_free = p;
655 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700656 else
Junio C Hamano2cd68882005-05-30 00:08:07 -0700657 /* no matching rename/copy source, so
658 * record this as a creation.
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700659 */
660 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700661 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700662 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
663 /*
664 * Deletion
665 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700666 * We would output this delete record if:
667 *
668 * (1) this is a broken delete and the counterpart
669 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -0700670 * (2) this is not a broken delete, and rename_dst
671 * does not have a rename/copy to move p->one->path
672 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700673 *
674 * Otherwise, the counterpart broken create
675 * has been turned into a rename-edit; or
676 * delete did not have a matching create to
677 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -0700678 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700679 if (DIFF_PAIR_BROKEN(p)) {
680 /* broken delete */
681 struct diff_rename_dst *dst =
682 locate_rename_dst(p->one, 0);
683 if (dst && dst->pair)
684 /* counterpart is now rename/copy */
685 pair_to_free = p;
686 }
687 else {
Linus Torvalds64479712007-10-25 11:20:56 -0700688 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700689 /* this path remains */
690 pair_to_free = p;
691 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700692
693 if (pair_to_free)
694 ;
695 else
696 diff_q(&outq, p);
697 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700698 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -0700699 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700700 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -0700701 else
702 /* no need to keep unmodified pairs */
703 pair_to_free = p;
704
Junio C Hamano226406f2005-05-27 15:50:30 -0700705 if (pair_to_free)
706 diff_free_filepair(pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700707 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700708 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700709
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700710 free(q->queue);
711 *q = outq;
712 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700713
Linus Torvalds9fb88412007-10-25 11:19:10 -0700714 for (i = 0; i < rename_dst_nr; i++)
715 free_filespec(rename_dst[i].two);
Junio C Hamano5098baf2005-09-15 16:13:43 -0700716
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700717 free(rename_dst);
718 rename_dst = NULL;
719 rename_dst_nr = rename_dst_alloc = 0;
720 free(rename_src);
721 rename_src = NULL;
722 rename_src_nr = rename_src_alloc = 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700723 return;
724}