blob: 749a35d2c2ab3271c6503a19271a5be6a6e97b4d [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"
Karsten Bleesf79d9c52013-11-14 20:20:26 +01007#include "hashmap.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" */
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040041 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070042 rename_dst_nr++;
43 if (first < rename_dst_nr)
44 memmove(rename_dst + first + 1, rename_dst + first,
45 (rename_dst_nr - first - 1) * sizeof(*rename_dst));
Junio C Hamano5098baf2005-09-15 16:13:43 -070046 rename_dst[first].two = alloc_filespec(two->path);
Jeff Kinge5450102012-07-28 11:03:01 -040047 fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070048 rename_dst[first].pair = NULL;
49 return &(rename_dst[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070050}
51
Junio C Hamano15d061b2005-05-27 15:55:55 -070052/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070053static struct diff_rename_src {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080054 struct diff_filepair *p;
Junio C Hamanofc580712006-04-08 20:17:46 -070055 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070056} *rename_src;
57static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070058
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080059static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070060{
61 int first, last;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080062 struct diff_filespec *one = p->one;
63 unsigned short score = p->score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070064
65 first = 0;
66 last = rename_src_nr;
67 while (last > first) {
68 int next = (last + first) >> 1;
69 struct diff_rename_src *src = &(rename_src[next]);
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080070 int cmp = strcmp(one->path, src->p->one->path);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070071 if (!cmp)
72 return src;
73 if (cmp < 0) {
74 last = next;
75 continue;
76 }
77 first = next+1;
Junio C Hamano427dcb42005-05-21 02:39:09 -070078 }
Junio C Hamano15d061b2005-05-27 15:55:55 -070079
Junio C Hamano25d5ea42005-05-24 01:10:48 -070080 /* insert to make it at "first" */
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040081 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070082 rename_src_nr++;
83 if (first < rename_src_nr)
84 memmove(rename_src + first + 1, rename_src + first,
85 (rename_src_nr - first - 1) * sizeof(*rename_src));
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080086 rename_src[first].p = p;
Junio C Hamanofc580712006-04-08 20:17:46 -070087 rename_src[first].score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070088 return &(rename_src[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -070089}
90
Johannes Schindelin0ce39642007-06-21 12:52:11 +010091static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
92{
93 int src_len = strlen(src->path), dst_len = strlen(dst->path);
94 while (src_len && dst_len) {
95 char c1 = src->path[--src_len];
96 char c2 = dst->path[--dst_len];
97 if (c1 != c2)
98 return 0;
99 if (c1 == '/')
100 return 1;
101 }
102 return (!src_len || src->path[src_len - 1] == '/') &&
103 (!dst_len || dst->path[dst_len - 1] == '/');
104}
105
Junio C Hamano427dcb42005-05-21 02:39:09 -0700106struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700107 int src; /* index in rename_src */
108 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800109 unsigned short score;
110 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700111};
112
113static int estimate_similarity(struct diff_filespec *src,
114 struct diff_filespec *dst,
115 int minimum_score)
116{
117 /* src points at a file that existed in the original tree (or
118 * optionally a file in the destination tree) and dst points
119 * at a newly created file. They may be quite similar, in which
120 * case we want to say src is renamed to dst or src is copied into
121 * dst, and then some edit has been applied to dst.
122 *
123 * Compare them and return how similar they are, representing
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700124 * the score as an integer between 0 and MAX_SCORE.
125 *
126 * When there is an exact match, it is considered a better
127 * match than anything else; the destination does not even
128 * call into this function in that case.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700129 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800130 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano75c660a2005-06-28 16:58:27 -0700131 unsigned long delta_limit;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700132 int score;
133
Junio C Hamano60896c72005-05-22 21:24:49 -0700134 /* We deal only with regular files. Symlink renames are handled
135 * only when they are exact matches --- in other words, no edits
136 * after renaming.
137 */
138 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
139 return 0;
140
Linus Torvalds81ac0512007-10-26 16:51:28 -0700141 /*
142 * Need to check that source and destination sizes are
143 * filled in before comparing them.
144 *
145 * If we already have "cnt_data" filled in, we know it's
146 * all good (avoid checking the size for zero, as that
147 * is a possible size - we really should have a flag to
148 * say whether the size is valid or not!)
149 */
Björn Steinbrink885c7162009-01-20 16:59:57 +0100150 if (!src->cnt_data && diff_populate_filespec(src, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700151 return 0;
Björn Steinbrink885c7162009-01-20 16:59:57 +0100152 if (!dst->cnt_data && diff_populate_filespec(dst, 1))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700153 return 0;
154
Linus Torvalds90bd9322006-03-12 22:26:34 -0800155 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700156 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800157 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700158
Junio C Hamano58b103f2005-05-21 15:55:18 -0700159 /* We would not consider edits that change the file size so
160 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700161 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700162 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700163 * Note that base_size == 0 case is handled here already
164 * and the final score computation below would not have a
165 * divide-by-zero issue.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700166 */
Linus Torvalds3a4d6762011-02-18 20:12:06 -0800167 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700168 return 0;
169
Björn Steinbrink885c7162009-01-20 16:59:57 +0100170 if (!src->cnt_data && diff_populate_filespec(src, 0))
171 return 0;
172 if (!dst->cnt_data && diff_populate_filespec(dst, 0))
173 return 0;
174
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500175 delta_limit = (unsigned long)
176 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
Junio C Hamanod8c3d032007-06-28 22:54:37 -0700177 if (diffcore_count_changes(src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800178 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800179 delta_limit,
180 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700181 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700182
Junio C Hamano17063062006-03-02 22:11:25 -0800183 /* How similar are they?
184 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700185 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800186 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800187 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200188 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500189 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700190 return score;
191}
192
Junio C Hamano5098baf2005-09-15 16:13:43 -0700193static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700194{
Linus Torvalds9fb88412007-10-25 11:19:10 -0700195 struct diff_filespec *src, *dst;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700196 struct diff_filepair *dp;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700197
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700198 if (rename_dst[dst_index].pair)
199 die("internal error: dst already matched.");
200
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800201 src = rename_src[src_index].p->one;
Linus Torvalds64479712007-10-25 11:20:56 -0700202 src->rename_used++;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700203 src->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700204
205 dst = rename_dst[dst_index].two;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700206 dst->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700207
Linus Torvalds9fb88412007-10-25 11:19:10 -0700208 dp = diff_queue(NULL, src, dst);
Junio C Hamanoef677682006-08-03 12:01:01 -0700209 dp->renamed_pair = 1;
Junio C Hamanofc580712006-04-08 20:17:46 -0700210 if (!strcmp(src->path, dst->path))
211 dp->score = rename_src[src_index].score;
212 else
213 dp->score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700214 rename_dst[dst_index].pair = dp;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700215}
216
217/*
218 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700219 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700220 */
221static int score_compare(const void *a_, const void *b_)
222{
223 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200224
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800225 /* sink the unused ones to the bottom */
226 if (a->dst < 0)
227 return (0 <= b->dst);
228 else if (b->dst < 0)
229 return -1;
230
René Scharfecfc0aef2007-06-25 00:23:28 +0200231 if (a->score == b->score)
232 return b->name_score - a->name_score;
233
Junio C Hamano427dcb42005-05-21 02:39:09 -0700234 return b->score - a->score;
235}
236
Linus Torvalds9027f532007-10-25 11:23:26 -0700237struct file_similarity {
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100238 struct hashmap_entry entry;
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100239 int index;
Linus Torvalds9027f532007-10-25 11:23:26 -0700240 struct diff_filespec *filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700241};
242
Karsten Blees48f64072013-11-14 20:19:04 +0100243static unsigned int hash_filespec(struct diff_filespec *filespec)
244{
245 unsigned int hash;
246 if (!filespec->sha1_valid) {
247 if (diff_populate_filespec(filespec, 0))
248 return 0;
249 hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
250 }
251 memcpy(&hash, filespec->sha1, sizeof(hash));
252 return hash;
253}
254
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100255static int find_identical_files(struct hashmap *srcs,
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100256 int dst_index,
Linus Torvalds11f944d2011-02-18 19:55:19 -0800257 struct diff_options *options)
Linus Torvalds9027f532007-10-25 11:23:26 -0700258{
259 int renames = 0;
260
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100261 struct diff_filespec *target = rename_dst[dst_index].two;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100262 struct file_similarity *p, *best, dst;
Karsten Blees48f64072013-11-14 20:19:04 +0100263 int i = 100, best_score = -1;
Linus Torvalds9027f532007-10-25 11:23:26 -0700264
Karsten Blees48f64072013-11-14 20:19:04 +0100265 /*
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100266 * Find the best source match for specified destination.
Karsten Blees48f64072013-11-14 20:19:04 +0100267 */
268 best = NULL;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100269 hashmap_entry_init(&dst, hash_filespec(target));
270 for (p = hashmap_get(srcs, &dst, NULL); p; p = hashmap_get_next(srcs, p)) {
Karsten Blees48f64072013-11-14 20:19:04 +0100271 int score;
272 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700273
Karsten Blees48f64072013-11-14 20:19:04 +0100274 /* False hash collision? */
275 if (hashcmp(source->sha1, target->sha1))
276 continue;
277 /* Non-regular files? If so, the modes must match! */
278 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
279 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700280 continue;
Karsten Blees48f64072013-11-14 20:19:04 +0100281 }
282 /* Give higher scores to sources that haven't been used already */
283 score = !source->rename_used;
284 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
285 continue;
286 score += basename_same(source, target);
287 if (score > best_score) {
288 best = p;
289 best_score = score;
290 if (score == 2)
Linus Torvalds9027f532007-10-25 11:23:26 -0700291 break;
292 }
Karsten Blees48f64072013-11-14 20:19:04 +0100293
294 /* Too many identical alternatives? Pick one */
295 if (!--i)
296 break;
297 }
298 if (best) {
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100299 record_rename_pair(dst_index, best->index, MAX_SCORE);
Karsten Blees48f64072013-11-14 20:19:04 +0100300 renames++;
301 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700302 return renames;
303}
304
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100305static void insert_file_table(struct hashmap *table, int index, struct diff_filespec *filespec)
Linus Torvalds9027f532007-10-25 11:23:26 -0700306{
Linus Torvalds9027f532007-10-25 11:23:26 -0700307 struct file_similarity *entry = xmalloc(sizeof(*entry));
308
Linus Torvalds9027f532007-10-25 11:23:26 -0700309 entry->index = index;
310 entry->filespec = filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700311
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100312 hashmap_entry_init(entry, hash_filespec(filespec));
313 hashmap_add(table, entry);
Linus Torvalds9027f532007-10-25 11:23:26 -0700314}
315
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700316/*
317 * Find exact renames first.
318 *
319 * The first round matches up the up-to-date entries,
320 * and then during the second round we try to match
321 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700322 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800323static int find_exact_renames(struct diff_options *options)
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700324{
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100325 int i, renames = 0;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100326 struct hashmap file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700327
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100328 /* Add all sources to the hash table */
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100329 hashmap_init(&file_table, NULL, rename_src_nr);
Linus Torvalds9027f532007-10-25 11:23:26 -0700330 for (i = 0; i < rename_src_nr; i++)
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100331 insert_file_table(&file_table, i, rename_src[i].p->one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700332
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100333 /* Walk the destinations and find best source match */
Linus Torvalds9027f532007-10-25 11:23:26 -0700334 for (i = 0; i < rename_dst_nr; i++)
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100335 renames += find_identical_files(&file_table, i, options);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700336
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100337 /* Free the hash data structure and entries */
338 hashmap_free(&file_table, 1);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700339
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100340 return renames;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700341}
342
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800343#define NUM_CANDIDATE_PER_DST 4
344static void record_if_better(struct diff_score m[], struct diff_score *o)
345{
346 int i, worst;
347
348 /* find the worst one */
349 worst = 0;
350 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
351 if (score_compare(&m[i], &m[worst]) > 0)
352 worst = i;
353
354 /* is it better than the worst one? */
355 if (score_compare(&m[worst], o) > 0)
356 m[worst] = *o;
357}
358
Junio C Hamanof31027c2011-01-06 13:50:06 -0800359/*
360 * Returns:
361 * 0 if we are under the limit;
362 * 1 if we need to disable inexact rename detection;
363 * 2 if we would be under the limit if we were given -C instead of -C -C.
364 */
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800365static int too_many_rename_candidates(int num_create,
366 struct diff_options *options)
367{
368 int rename_limit = options->rename_limit;
369 int num_src = rename_src_nr;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800370 int i;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800371
372 options->needed_rename_limit = 0;
373
374 /*
375 * This basically does a test for the rename matrix not
376 * growing larger than a "rename_limit" square matrix, ie:
377 *
378 * num_create * num_src > rename_limit * rename_limit
379 *
380 * but handles the potential overflow case specially (and we
381 * assume at least 32-bit integers)
382 */
383 if (rename_limit <= 0 || rename_limit > 32767)
384 rename_limit = 32767;
385 if ((num_create <= rename_limit || num_src <= rename_limit) &&
386 (num_create * num_src <= rename_limit * rename_limit))
387 return 0;
388
389 options->needed_rename_limit =
390 num_src > num_create ? num_src : num_create;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800391
392 /* Are we running under -C -C? */
393 if (!DIFF_OPT_TST(options, FIND_COPIES_HARDER))
394 return 1;
395
396 /* Would we bust the limit if we were running under -C? */
397 for (num_src = i = 0; i < rename_src_nr; i++) {
398 if (diff_unmodified_pair(rename_src[i].p))
399 continue;
400 num_src++;
401 }
402 if ((num_create <= rename_limit || num_src <= rename_limit) &&
403 (num_create * num_src <= rename_limit * rename_limit))
404 return 2;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800405 return 1;
406}
407
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800408static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
409{
410 int count = 0, i;
411
412 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
413 struct diff_rename_dst *dst;
414
415 if ((mx[i].dst < 0) ||
416 (mx[i].score < minimum_score))
417 break; /* there is no more usable pair. */
418 dst = &rename_dst[mx[i].dst];
419 if (dst->pair)
420 continue; /* already done, either exact or fuzzy. */
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800421 if (!copies && rename_src[mx[i].src].p->one->rename_used)
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800422 continue;
423 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
424 count++;
425 }
426 return count;
427}
428
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700429void diffcore_rename(struct diff_options *options)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700430{
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700431 int detect_rename = options->detect_rename;
432 int minimum_score = options->rename_score;
Junio C Hamano38c6f782005-05-21 19:40:36 -0700433 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -0700434 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700435 struct diff_score *mx;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800436 int i, j, rename_count, skip_unmodified = 0;
Jim Meyeringdabdbee2011-04-29 11:42:41 +0200437 int num_create, dst_cnt;
Jeff King3ac942d2011-02-20 04:51:16 -0500438 struct progress *progress = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700439
Junio C Hamano26dee0a2005-05-21 23:33:32 -0700440 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700441 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700442
Junio C Hamano427dcb42005-05-21 02:39:09 -0700443 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -0700444 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800445 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700446 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -0700447 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800448 else if (options->single_follow &&
449 strcmp(options->single_follow, p->two->path))
450 continue; /* not interested */
Jeff King90d43b02012-03-22 18:52:13 -0400451 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
452 is_empty_blob_sha1(p->two->sha1))
453 continue;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700454 else
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700455 locate_rename_dst(p->two, 1);
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800456 }
Jeff King90d43b02012-03-22 18:52:13 -0400457 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
458 is_empty_blob_sha1(p->one->sha1))
459 continue;
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400460 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -0700461 /*
462 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -0700463 * they did not really want to get broken,
464 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -0700465 * So we increment the "rename_used" score
466 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -0700467 */
Linus Torvalds64479712007-10-25 11:20:56 -0700468 if (p->broken_pair && !p->score)
469 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800470 register_rename_src(p);
Junio C Hamano22101002005-06-11 20:55:20 -0700471 }
Linus Torvalds64479712007-10-25 11:20:56 -0700472 else if (detect_rename == DIFF_DETECT_COPY) {
473 /*
474 * Increment the "rename_used" score by
475 * one, to indicate ourselves as a user.
476 */
477 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800478 register_rename_src(p);
Linus Torvalds64479712007-10-25 11:20:56 -0700479 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700480 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700481 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700482 goto cleanup; /* nothing to do */
483
Linus Torvalds0024a542007-09-14 10:39:48 -0700484 /*
Linus Torvalds17559a62007-10-25 11:24:47 -0700485 * We really want to cull the candidates list early
486 * with cheap tests in order to avoid doing deltas.
487 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800488 rename_count = find_exact_renames(options);
Linus Torvalds17559a62007-10-25 11:24:47 -0700489
Linus Torvalds42899ac2007-10-26 16:56:34 -0700490 /* Did we only want exact renames? */
491 if (minimum_score == MAX_SCORE)
492 goto cleanup;
493
494 /*
495 * Calculate how many renames are left (but all the source
496 * files still remain as options for rename/copies!)
497 */
498 num_create = (rename_dst_nr - rename_count);
Linus Torvalds42899ac2007-10-26 16:56:34 -0700499
500 /* All done? */
501 if (!num_create)
502 goto cleanup;
503
Junio C Hamanof31027c2011-01-06 13:50:06 -0800504 switch (too_many_rename_candidates(num_create, options)) {
505 case 1:
Linus Torvalds0024a542007-09-14 10:39:48 -0700506 goto cleanup;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800507 case 2:
508 options->degraded_cc_to_c = 1;
509 skip_unmodified = 1;
510 break;
511 default:
512 break;
513 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700514
Jeff King3ac942d2011-02-20 04:51:16 -0500515 if (options->show_rename_progress) {
516 progress = start_progress_delay(
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700517 _("Performing inexact rename detection"),
Jeff King3ac942d2011-02-20 04:51:16 -0500518 rename_dst_nr * rename_src_nr, 50, 1);
519 }
520
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800521 mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700522 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700523 struct diff_filespec *two = rename_dst[i].two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800524 struct diff_score *m;
525
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700526 if (rename_dst[i].pair)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700527 continue; /* dealt with exact match already. */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800528
529 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
530 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
531 m[j].dst = -1;
532
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700533 for (j = 0; j < rename_src_nr; j++) {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800534 struct diff_filespec *one = rename_src[j].p->one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800535 struct diff_score this_src;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800536
537 if (skip_unmodified &&
538 diff_unmodified_pair(rename_src[j].p))
539 continue;
540
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800541 this_src.score = estimate_similarity(one, two,
542 minimum_score);
543 this_src.name_score = basename_same(one, two);
544 this_src.dst = i;
545 this_src.src = j;
546 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -0800547 /*
548 * Once we run estimate_similarity,
549 * We do not need the text anymore.
550 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -0700551 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -0800552 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700553 }
554 dst_cnt++;
Jeff King3ac942d2011-02-20 04:51:16 -0500555 display_progress(progress, (i+1)*rename_src_nr);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700556 }
Jeff King3ac942d2011-02-20 04:51:16 -0500557 stop_progress(&progress);
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800558
Junio C Hamano427dcb42005-05-21 02:39:09 -0700559 /* cost matrix sorted by most to least similar pair */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800560 qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
561
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800562 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
563 if (detect_rename == DIFF_DETECT_COPY)
564 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700565 free(mx);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700566
Junio C Hamano15d061b2005-05-27 15:55:55 -0700567 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -0700568 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -0700569 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700570 */
Bo Yang9ca5df92010-05-06 21:52:27 -0700571 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700572 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700573 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700574 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700575
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400576 if (DIFF_PAIR_UNMERGED(p)) {
577 diff_q(&outq, p);
578 }
579 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Junio C Hamano2cd68882005-05-30 00:08:07 -0700580 /*
581 * Creation
582 *
583 * We would output this create record if it has
584 * not been turned into a rename/copy already.
585 */
586 struct diff_rename_dst *dst =
587 locate_rename_dst(p->two, 0);
588 if (dst && dst->pair) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700589 diff_q(&outq, dst->pair);
590 pair_to_free = p;
591 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700592 else
Junio C Hamano2cd68882005-05-30 00:08:07 -0700593 /* no matching rename/copy source, so
594 * record this as a creation.
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700595 */
596 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700597 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700598 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
599 /*
600 * Deletion
601 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700602 * We would output this delete record if:
603 *
604 * (1) this is a broken delete and the counterpart
605 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -0700606 * (2) this is not a broken delete, and rename_dst
607 * does not have a rename/copy to move p->one->path
608 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700609 *
610 * Otherwise, the counterpart broken create
611 * has been turned into a rename-edit; or
612 * delete did not have a matching create to
613 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -0700614 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700615 if (DIFF_PAIR_BROKEN(p)) {
616 /* broken delete */
617 struct diff_rename_dst *dst =
618 locate_rename_dst(p->one, 0);
619 if (dst && dst->pair)
620 /* counterpart is now rename/copy */
621 pair_to_free = p;
622 }
623 else {
Linus Torvalds64479712007-10-25 11:20:56 -0700624 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700625 /* this path remains */
626 pair_to_free = p;
627 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700628
629 if (pair_to_free)
630 ;
631 else
632 diff_q(&outq, p);
633 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700634 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -0700635 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700636 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -0700637 else
638 /* no need to keep unmodified pairs */
639 pair_to_free = p;
640
Junio C Hamano226406f2005-05-27 15:50:30 -0700641 if (pair_to_free)
642 diff_free_filepair(pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700643 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700644 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700645
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700646 free(q->queue);
647 *q = outq;
648 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700649
Linus Torvalds9fb88412007-10-25 11:19:10 -0700650 for (i = 0; i < rename_dst_nr; i++)
651 free_filespec(rename_dst[i].two);
Junio C Hamano5098baf2005-09-15 16:13:43 -0700652
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700653 free(rename_dst);
654 rename_dst = NULL;
655 rename_dst_nr = rename_dst_alloc = 0;
656 free(rename_src);
657 rename_src = NULL;
658 rename_src_nr = rename_src_alloc = 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700659 return;
660}