blob: 99e63e90f89afaf55ef16bb3c11c1546c4d76c2a [file] [log] [blame]
Junio C Hamano427dcb42005-05-21 02:39:09 -07001/*
Jonathan Tan95acf112020-04-07 15:11:43 -07002 *
Junio C Hamano427dcb42005-05-21 02:39:09 -07003 * Copyright (C) 2005 Junio C Hamano
4 */
5#include "cache.h"
6#include "diff.h"
7#include "diffcore.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07008#include "object-store.h"
Karsten Bleesf79d9c52013-11-14 20:20:26 +01009#include "hashmap.h"
Jeff King3ac942d2011-02-20 04:51:16 -050010#include "progress.h"
Jonathan Tan95acf112020-04-07 15:11:43 -070011#include "promisor-remote.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -070012
Junio C Hamano25d5ea42005-05-24 01:10:48 -070013/* Table of rename/copy destinations */
Junio C Hamano427dcb42005-05-21 02:39:09 -070014
Junio C Hamano25d5ea42005-05-24 01:10:48 -070015static struct diff_rename_dst {
16 struct diff_filespec *two;
17 struct diff_filepair *pair;
18} *rename_dst;
19static int rename_dst_nr, rename_dst_alloc;
20
Jeff Kingf98c2f72015-02-26 20:39:48 -050021static int find_rename_dst(struct diff_filespec *two)
Junio C Hamano427dcb42005-05-21 02:39:09 -070022{
Junio C Hamano25d5ea42005-05-24 01:10:48 -070023 int first, last;
24
25 first = 0;
26 last = rename_dst_nr;
27 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +020028 int next = first + ((last - first) >> 1);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070029 struct diff_rename_dst *dst = &(rename_dst[next]);
30 int cmp = strcmp(two->path, dst->two->path);
31 if (!cmp)
Jeff Kingf98c2f72015-02-26 20:39:48 -050032 return next;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070033 if (cmp < 0) {
34 last = next;
35 continue;
36 }
37 first = next+1;
38 }
Jeff Kingf98c2f72015-02-26 20:39:48 -050039 return -first - 1;
40}
41
42static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
43{
44 int ofs = find_rename_dst(two);
45 return ofs < 0 ? NULL : &rename_dst[ofs];
46}
47
48/*
49 * Returns 0 on success, -1 if we found a duplicate.
50 */
51static int add_rename_dst(struct diff_filespec *two)
52{
53 int first = find_rename_dst(two);
54
55 if (first >= 0)
56 return -1;
57 first = -first - 1;
58
Junio C Hamano25d5ea42005-05-24 01:10:48 -070059 /* insert to make it at "first" */
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040060 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070061 rename_dst_nr++;
62 if (first < rename_dst_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +010063 MOVE_ARRAY(rename_dst + first + 1, rename_dst + first,
64 rename_dst_nr - first - 1);
Junio C Hamano5098baf2005-09-15 16:13:43 -070065 rename_dst[first].two = alloc_filespec(two->path);
Brandon Williamsf9704c22017-05-30 10:30:50 -070066 fill_filespec(rename_dst[first].two, &two->oid, two->oid_valid,
brian m. carlsona0d12c42016-06-24 23:09:23 +000067 two->mode);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070068 rename_dst[first].pair = NULL;
Jeff Kingf98c2f72015-02-26 20:39:48 -050069 return 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -070070}
71
Junio C Hamano15d061b2005-05-27 15:55:55 -070072/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070073static struct diff_rename_src {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080074 struct diff_filepair *p;
Junio C Hamanofc580712006-04-08 20:17:46 -070075 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070076} *rename_src;
77static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070078
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080079static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070080{
81 int first, last;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080082 struct diff_filespec *one = p->one;
83 unsigned short score = p->score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070084
85 first = 0;
86 last = rename_src_nr;
87 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +020088 int next = first + ((last - first) >> 1);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070089 struct diff_rename_src *src = &(rename_src[next]);
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080090 int cmp = strcmp(one->path, src->p->one->path);
Junio C Hamano25d5ea42005-05-24 01:10:48 -070091 if (!cmp)
92 return src;
93 if (cmp < 0) {
94 last = next;
95 continue;
96 }
97 first = next+1;
Junio C Hamano427dcb42005-05-21 02:39:09 -070098 }
Junio C Hamano15d061b2005-05-27 15:55:55 -070099
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700100 /* insert to make it at "first" */
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +0400101 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700102 rename_src_nr++;
103 if (first < rename_src_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +0100104 MOVE_ARRAY(rename_src + first + 1, rename_src + first,
105 rename_src_nr - first - 1);
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800106 rename_src[first].p = p;
Junio C Hamanofc580712006-04-08 20:17:46 -0700107 rename_src[first].score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700108 return &(rename_src[first]);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700109}
110
Johannes Schindelin0ce39642007-06-21 12:52:11 +0100111static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
112{
113 int src_len = strlen(src->path), dst_len = strlen(dst->path);
114 while (src_len && dst_len) {
115 char c1 = src->path[--src_len];
116 char c2 = dst->path[--dst_len];
117 if (c1 != c2)
118 return 0;
119 if (c1 == '/')
120 return 1;
121 }
122 return (!src_len || src->path[src_len - 1] == '/') &&
123 (!dst_len || dst->path[dst_len - 1] == '/');
124}
125
Junio C Hamano427dcb42005-05-21 02:39:09 -0700126struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700127 int src; /* index in rename_src */
128 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800129 unsigned short score;
130 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700131};
132
Jonathan Tan95acf112020-04-07 15:11:43 -0700133struct prefetch_options {
134 struct repository *repo;
135 int skip_unmodified;
136};
137static void prefetch(void *prefetch_options)
138{
139 struct prefetch_options *options = prefetch_options;
140 int i;
141 struct oid_array to_fetch = OID_ARRAY_INIT;
142
143 for (i = 0; i < rename_dst_nr; i++) {
144 if (rename_dst[i].pair)
145 /*
146 * The loop in diffcore_rename() will not need these
147 * blobs, so skip prefetching.
148 */
149 continue; /* already found exact match */
150 diff_add_if_missing(options->repo, &to_fetch,
151 rename_dst[i].two);
152 }
153 for (i = 0; i < rename_src_nr; i++) {
154 if (options->skip_unmodified &&
155 diff_unmodified_pair(rename_src[i].p))
156 /*
157 * The loop in diffcore_rename() will not need these
158 * blobs, so skip prefetching.
159 */
160 continue;
161 diff_add_if_missing(options->repo, &to_fetch,
162 rename_src[i].p->one);
163 }
164 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
165 oid_array_clear(&to_fetch);
166}
167
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200168static int estimate_similarity(struct repository *r,
169 struct diff_filespec *src,
Junio C Hamano427dcb42005-05-21 02:39:09 -0700170 struct diff_filespec *dst,
Jonathan Tan95acf112020-04-07 15:11:43 -0700171 int minimum_score,
172 int skip_unmodified)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700173{
174 /* src points at a file that existed in the original tree (or
175 * optionally a file in the destination tree) and dst points
176 * at a newly created file. They may be quite similar, in which
177 * case we want to say src is renamed to dst or src is copied into
178 * dst, and then some edit has been applied to dst.
179 *
180 * Compare them and return how similar they are, representing
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700181 * the score as an integer between 0 and MAX_SCORE.
182 *
183 * When there is an exact match, it is considered a better
184 * match than anything else; the destination does not even
185 * call into this function in that case.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700186 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800187 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700188 int score;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700189 struct diff_populate_filespec_options dpf_options = {
190 .check_size_only = 1
191 };
Jonathan Tan95acf112020-04-07 15:11:43 -0700192 struct prefetch_options prefetch_options = {r, skip_unmodified};
193
194 if (r == the_repository && has_promisor_remote()) {
195 dpf_options.missing_object_cb = prefetch;
196 dpf_options.missing_object_data = &prefetch_options;
197 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700198
Junio C Hamano60896c72005-05-22 21:24:49 -0700199 /* We deal only with regular files. Symlink renames are handled
200 * only when they are exact matches --- in other words, no edits
201 * after renaming.
202 */
203 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
204 return 0;
205
Linus Torvalds81ac0512007-10-26 16:51:28 -0700206 /*
207 * Need to check that source and destination sizes are
208 * filled in before comparing them.
209 *
210 * If we already have "cnt_data" filled in, we know it's
211 * all good (avoid checking the size for zero, as that
212 * is a possible size - we really should have a flag to
213 * say whether the size is valid or not!)
214 */
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700215 if (!src->cnt_data &&
Jonathan Tan1c37e862020-04-07 15:11:41 -0700216 diff_populate_filespec(r, src, &dpf_options))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700217 return 0;
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700218 if (!dst->cnt_data &&
Jonathan Tan1c37e862020-04-07 15:11:41 -0700219 diff_populate_filespec(r, dst, &dpf_options))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700220 return 0;
221
Linus Torvalds90bd9322006-03-12 22:26:34 -0800222 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700223 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800224 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700225
Junio C Hamano58b103f2005-05-21 15:55:18 -0700226 /* We would not consider edits that change the file size so
227 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700228 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700229 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700230 * Note that base_size == 0 case is handled here already
231 * and the final score computation below would not have a
232 * divide-by-zero issue.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700233 */
Linus Torvalds3a4d6762011-02-18 20:12:06 -0800234 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700235 return 0;
236
Jonathan Tan95acf112020-04-07 15:11:43 -0700237 dpf_options.check_size_only = 0;
238
239 if (!src->cnt_data && diff_populate_filespec(r, src, &dpf_options))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100240 return 0;
Jonathan Tan95acf112020-04-07 15:11:43 -0700241 if (!dst->cnt_data && diff_populate_filespec(r, dst, &dpf_options))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100242 return 0;
243
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200244 if (diffcore_count_changes(r, src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800245 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800246 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700247 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700248
Junio C Hamano17063062006-03-02 22:11:25 -0800249 /* How similar are they?
250 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700251 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800252 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800253 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200254 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500255 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700256 return score;
257}
258
Junio C Hamano5098baf2005-09-15 16:13:43 -0700259static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700260{
Linus Torvalds9fb88412007-10-25 11:19:10 -0700261 struct diff_filespec *src, *dst;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700262 struct diff_filepair *dp;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700263
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700264 if (rename_dst[dst_index].pair)
265 die("internal error: dst already matched.");
266
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800267 src = rename_src[src_index].p->one;
Linus Torvalds64479712007-10-25 11:20:56 -0700268 src->rename_used++;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700269 src->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700270
271 dst = rename_dst[dst_index].two;
Linus Torvalds9fb88412007-10-25 11:19:10 -0700272 dst->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700273
Linus Torvalds9fb88412007-10-25 11:19:10 -0700274 dp = diff_queue(NULL, src, dst);
Junio C Hamanoef677682006-08-03 12:01:01 -0700275 dp->renamed_pair = 1;
Junio C Hamanofc580712006-04-08 20:17:46 -0700276 if (!strcmp(src->path, dst->path))
277 dp->score = rename_src[src_index].score;
278 else
279 dp->score = score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700280 rename_dst[dst_index].pair = dp;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700281}
282
283/*
284 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700285 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700286 */
287static int score_compare(const void *a_, const void *b_)
288{
289 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200290
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800291 /* sink the unused ones to the bottom */
292 if (a->dst < 0)
293 return (0 <= b->dst);
294 else if (b->dst < 0)
295 return -1;
296
René Scharfecfc0aef2007-06-25 00:23:28 +0200297 if (a->score == b->score)
298 return b->name_score - a->name_score;
299
Junio C Hamano427dcb42005-05-21 02:39:09 -0700300 return b->score - a->score;
301}
302
Linus Torvalds9027f532007-10-25 11:23:26 -0700303struct file_similarity {
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100304 struct hashmap_entry entry;
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100305 int index;
Linus Torvalds9027f532007-10-25 11:23:26 -0700306 struct diff_filespec *filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700307};
308
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200309static unsigned int hash_filespec(struct repository *r,
310 struct diff_filespec *filespec)
Karsten Blees48f64072013-11-14 20:19:04 +0100311{
brian m. carlson41c95602016-06-24 23:09:24 +0000312 if (!filespec->oid_valid) {
Jonathan Tan1c37e862020-04-07 15:11:41 -0700313 if (diff_populate_filespec(r, filespec, NULL))
Karsten Blees48f64072013-11-14 20:19:04 +0100314 return 0;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300315 hash_object_file(r->hash_algo, filespec->data, filespec->size,
316 "blob", &filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100317 }
Jeff Kingd40abc82019-06-20 03:41:49 -0400318 return oidhash(&filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100319}
320
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100321static int find_identical_files(struct hashmap *srcs,
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100322 int dst_index,
Linus Torvalds11f944d2011-02-18 19:55:19 -0800323 struct diff_options *options)
Linus Torvalds9027f532007-10-25 11:23:26 -0700324{
325 int renames = 0;
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100326 struct diff_filespec *target = rename_dst[dst_index].two;
Karsten Bleesab73a9d2014-07-03 00:22:11 +0200327 struct file_similarity *p, *best = NULL;
Karsten Blees48f64072013-11-14 20:19:04 +0100328 int i = 100, best_score = -1;
Eric Wongf0e63c42019-10-06 23:30:35 +0000329 unsigned int hash = hash_filespec(options->repo, target);
Linus Torvalds9027f532007-10-25 11:23:26 -0700330
Karsten Blees48f64072013-11-14 20:19:04 +0100331 /*
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100332 * Find the best source match for specified destination.
Karsten Blees48f64072013-11-14 20:19:04 +0100333 */
Eric Wongf0e63c42019-10-06 23:30:35 +0000334 p = hashmap_get_entry_from_hash(srcs, hash, NULL,
335 struct file_similarity, entry);
Eric Wong23dee692019-10-06 23:30:41 +0000336 hashmap_for_each_entry_from(srcs, p, entry) {
Karsten Blees48f64072013-11-14 20:19:04 +0100337 int score;
338 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700339
Karsten Blees48f64072013-11-14 20:19:04 +0100340 /* False hash collision? */
Jeff King9001dc22018-08-28 17:22:48 -0400341 if (!oideq(&source->oid, &target->oid))
Karsten Blees48f64072013-11-14 20:19:04 +0100342 continue;
343 /* Non-regular files? If so, the modes must match! */
344 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
345 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700346 continue;
Karsten Blees48f64072013-11-14 20:19:04 +0100347 }
348 /* Give higher scores to sources that haven't been used already */
349 score = !source->rename_used;
350 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
351 continue;
352 score += basename_same(source, target);
353 if (score > best_score) {
354 best = p;
355 best_score = score;
356 if (score == 2)
Linus Torvalds9027f532007-10-25 11:23:26 -0700357 break;
358 }
Karsten Blees48f64072013-11-14 20:19:04 +0100359
360 /* Too many identical alternatives? Pick one */
361 if (!--i)
362 break;
363 }
364 if (best) {
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100365 record_rename_pair(dst_index, best->index, MAX_SCORE);
Karsten Blees48f64072013-11-14 20:19:04 +0100366 renames++;
367 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700368 return renames;
369}
370
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200371static void insert_file_table(struct repository *r,
372 struct hashmap *table, int index,
373 struct diff_filespec *filespec)
Linus Torvalds9027f532007-10-25 11:23:26 -0700374{
Linus Torvalds9027f532007-10-25 11:23:26 -0700375 struct file_similarity *entry = xmalloc(sizeof(*entry));
376
Linus Torvalds9027f532007-10-25 11:23:26 -0700377 entry->index = index;
378 entry->filespec = filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700379
Eric Wongd22245a2019-10-06 23:30:27 +0000380 hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
Eric Wongb94e5c12019-10-06 23:30:29 +0000381 hashmap_add(table, &entry->entry);
Linus Torvalds9027f532007-10-25 11:23:26 -0700382}
383
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700384/*
385 * Find exact renames first.
386 *
387 * The first round matches up the up-to-date entries,
388 * and then during the second round we try to match
389 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700390 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800391static int find_exact_renames(struct diff_options *options)
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700392{
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100393 int i, renames = 0;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100394 struct hashmap file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700395
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200396 /* Add all sources to the hash table in reverse order, because
397 * later on they will be retrieved in LIFO order.
398 */
Stefan Beller7663cdc2017-06-30 12:14:05 -0700399 hashmap_init(&file_table, NULL, NULL, rename_src_nr);
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200400 for (i = rename_src_nr-1; i >= 0; i--)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200401 insert_file_table(options->repo,
402 &file_table, i,
403 rename_src[i].p->one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700404
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100405 /* Walk the destinations and find best source match */
Linus Torvalds9027f532007-10-25 11:23:26 -0700406 for (i = 0; i < rename_dst_nr; i++)
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100407 renames += find_identical_files(&file_table, i, options);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700408
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100409 /* Free the hash data structure and entries */
Eric Wongc8e424c2019-10-06 23:30:40 +0000410 hashmap_free_entries(&file_table, struct file_similarity, entry);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700411
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100412 return renames;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700413}
414
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800415#define NUM_CANDIDATE_PER_DST 4
416static void record_if_better(struct diff_score m[], struct diff_score *o)
417{
418 int i, worst;
419
420 /* find the worst one */
421 worst = 0;
422 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
423 if (score_compare(&m[i], &m[worst]) > 0)
424 worst = i;
425
426 /* is it better than the worst one? */
427 if (score_compare(&m[worst], o) > 0)
428 m[worst] = *o;
429}
430
Junio C Hamanof31027c2011-01-06 13:50:06 -0800431/*
432 * Returns:
433 * 0 if we are under the limit;
434 * 1 if we need to disable inexact rename detection;
435 * 2 if we would be under the limit if we were given -C instead of -C -C.
436 */
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800437static int too_many_rename_candidates(int num_create,
438 struct diff_options *options)
439{
440 int rename_limit = options->rename_limit;
441 int num_src = rename_src_nr;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800442 int i;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800443
444 options->needed_rename_limit = 0;
445
446 /*
447 * This basically does a test for the rename matrix not
448 * growing larger than a "rename_limit" square matrix, ie:
449 *
450 * num_create * num_src > rename_limit * rename_limit
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800451 */
Jonathan Tan89973552017-11-29 12:11:54 -0800452 if (rename_limit <= 0)
453 rename_limit = 32767;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800454 if ((num_create <= rename_limit || num_src <= rename_limit) &&
Elijah Newren9f7e4bf2017-11-13 12:15:59 -0800455 ((uint64_t)num_create * (uint64_t)num_src
456 <= (uint64_t)rename_limit * (uint64_t)rename_limit))
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800457 return 0;
458
459 options->needed_rename_limit =
460 num_src > num_create ? num_src : num_create;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800461
462 /* Are we running under -C -C? */
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700463 if (!options->flags.find_copies_harder)
Junio C Hamanof31027c2011-01-06 13:50:06 -0800464 return 1;
465
466 /* Would we bust the limit if we were running under -C? */
467 for (num_src = i = 0; i < rename_src_nr; i++) {
468 if (diff_unmodified_pair(rename_src[i].p))
469 continue;
470 num_src++;
471 }
472 if ((num_create <= rename_limit || num_src <= rename_limit) &&
Elijah Newren9f7e4bf2017-11-13 12:15:59 -0800473 ((uint64_t)num_create * (uint64_t)num_src
474 <= (uint64_t)rename_limit * (uint64_t)rename_limit))
Junio C Hamanof31027c2011-01-06 13:50:06 -0800475 return 2;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -0800476 return 1;
477}
478
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800479static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
480{
481 int count = 0, i;
482
483 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
484 struct diff_rename_dst *dst;
485
486 if ((mx[i].dst < 0) ||
487 (mx[i].score < minimum_score))
488 break; /* there is no more usable pair. */
489 dst = &rename_dst[mx[i].dst];
490 if (dst->pair)
491 continue; /* already done, either exact or fuzzy. */
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800492 if (!copies && rename_src[mx[i].src].p->one->rename_used)
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800493 continue;
494 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
495 count++;
496 }
497 return count;
498}
499
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700500void diffcore_rename(struct diff_options *options)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700501{
Junio C Hamano8082d8d2005-09-21 00:18:27 -0700502 int detect_rename = options->detect_rename;
503 int minimum_score = options->rename_score;
Junio C Hamano38c6f782005-05-21 19:40:36 -0700504 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -0700505 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700506 struct diff_score *mx;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800507 int i, j, rename_count, skip_unmodified = 0;
Jim Meyeringdabdbee2011-04-29 11:42:41 +0200508 int num_create, dst_cnt;
Jeff King3ac942d2011-02-20 04:51:16 -0500509 struct progress *progress = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700510
Junio C Hamano26dee0a2005-05-21 23:33:32 -0700511 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700512 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700513
Junio C Hamano427dcb42005-05-21 02:39:09 -0700514 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -0700515 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800516 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700517 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -0700518 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800519 else if (options->single_follow &&
520 strcmp(options->single_follow, p->two->path))
521 continue; /* not interested */
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700522 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -0700523 is_empty_blob_oid(&p->two->oid))
Jeff King90d43b02012-03-22 18:52:13 -0400524 continue;
Jeff King4d6be032015-02-26 20:42:27 -0500525 else if (add_rename_dst(p->two) < 0) {
526 warning("skipping rename detection, detected"
527 " duplicate destination '%s'",
528 p->two->path);
529 goto cleanup;
530 }
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800531 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700532 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -0700533 is_empty_blob_oid(&p->one->oid))
Jeff King90d43b02012-03-22 18:52:13 -0400534 continue;
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400535 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -0700536 /*
537 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -0700538 * they did not really want to get broken,
539 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -0700540 * So we increment the "rename_used" score
541 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -0700542 */
Linus Torvalds64479712007-10-25 11:20:56 -0700543 if (p->broken_pair && !p->score)
544 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800545 register_rename_src(p);
Junio C Hamano22101002005-06-11 20:55:20 -0700546 }
Linus Torvalds64479712007-10-25 11:20:56 -0700547 else if (detect_rename == DIFF_DETECT_COPY) {
548 /*
549 * Increment the "rename_used" score by
550 * one, to indicate ourselves as a user.
551 */
552 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800553 register_rename_src(p);
Linus Torvalds64479712007-10-25 11:20:56 -0700554 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700555 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700556 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700557 goto cleanup; /* nothing to do */
558
Linus Torvalds0024a542007-09-14 10:39:48 -0700559 /*
Linus Torvalds17559a62007-10-25 11:24:47 -0700560 * We really want to cull the candidates list early
561 * with cheap tests in order to avoid doing deltas.
562 */
Linus Torvalds11f944d2011-02-18 19:55:19 -0800563 rename_count = find_exact_renames(options);
Linus Torvalds17559a62007-10-25 11:24:47 -0700564
Linus Torvalds42899ac2007-10-26 16:56:34 -0700565 /* Did we only want exact renames? */
566 if (minimum_score == MAX_SCORE)
567 goto cleanup;
568
569 /*
570 * Calculate how many renames are left (but all the source
571 * files still remain as options for rename/copies!)
572 */
573 num_create = (rename_dst_nr - rename_count);
Linus Torvalds42899ac2007-10-26 16:56:34 -0700574
575 /* All done? */
576 if (!num_create)
577 goto cleanup;
578
Junio C Hamanof31027c2011-01-06 13:50:06 -0800579 switch (too_many_rename_candidates(num_create, options)) {
580 case 1:
Linus Torvalds0024a542007-09-14 10:39:48 -0700581 goto cleanup;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800582 case 2:
583 options->degraded_cc_to_c = 1;
584 skip_unmodified = 1;
585 break;
586 default:
587 break;
588 }
Linus Torvalds0024a542007-09-14 10:39:48 -0700589
Jeff King3ac942d2011-02-20 04:51:16 -0500590 if (options->show_rename_progress) {
Junio C Hamano8aade102017-08-19 10:39:41 -0700591 progress = start_delayed_progress(
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700592 _("Performing inexact rename detection"),
Elijah Newrend6861d02017-11-13 12:15:58 -0800593 (uint64_t)rename_dst_nr * (uint64_t)rename_src_nr);
Jeff King3ac942d2011-02-20 04:51:16 -0500594 }
595
René Scharfe50492f72016-07-30 20:18:31 +0200596 mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_create), sizeof(*mx));
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700597 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700598 struct diff_filespec *two = rename_dst[i].two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800599 struct diff_score *m;
600
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700601 if (rename_dst[i].pair)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700602 continue; /* dealt with exact match already. */
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800603
604 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
605 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
606 m[j].dst = -1;
607
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700608 for (j = 0; j < rename_src_nr; j++) {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -0800609 struct diff_filespec *one = rename_src[j].p->one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800610 struct diff_score this_src;
Junio C Hamanof31027c2011-01-06 13:50:06 -0800611
612 if (skip_unmodified &&
613 diff_unmodified_pair(rename_src[j].p))
614 continue;
615
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200616 this_src.score = estimate_similarity(options->repo,
617 one, two,
Jonathan Tan95acf112020-04-07 15:11:43 -0700618 minimum_score,
619 skip_unmodified);
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800620 this_src.name_score = basename_same(one, two);
621 this_src.dst = i;
622 this_src.src = j;
623 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -0800624 /*
625 * Once we run estimate_similarity,
626 * We do not need the text anymore.
627 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -0700628 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -0800629 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700630 }
631 dst_cnt++;
Elijah Newrend6861d02017-11-13 12:15:58 -0800632 display_progress(progress, (uint64_t)(i+1)*(uint64_t)rename_src_nr);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700633 }
Jeff King3ac942d2011-02-20 04:51:16 -0500634 stop_progress(&progress);
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800635
Junio C Hamano427dcb42005-05-21 02:39:09 -0700636 /* cost matrix sorted by most to least similar pair */
Johannes Schindelin2049b8d2019-09-30 10:21:55 -0700637 STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800638
Linus Torvalds0940e5f2011-02-18 20:10:32 -0800639 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
640 if (detect_rename == DIFF_DETECT_COPY)
641 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700642 free(mx);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700643
Junio C Hamano15d061b2005-05-27 15:55:55 -0700644 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -0700645 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -0700646 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700647 */
Bo Yang9ca5df92010-05-06 21:52:27 -0700648 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700649 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700650 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700651 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700652
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -0400653 if (DIFF_PAIR_UNMERGED(p)) {
654 diff_q(&outq, p);
655 }
656 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Junio C Hamano2cd68882005-05-30 00:08:07 -0700657 /*
658 * Creation
659 *
660 * We would output this create record if it has
661 * not been turned into a rename/copy already.
662 */
Jeff Kingf98c2f72015-02-26 20:39:48 -0500663 struct diff_rename_dst *dst = locate_rename_dst(p->two);
Junio C Hamano2cd68882005-05-30 00:08:07 -0700664 if (dst && dst->pair) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700665 diff_q(&outq, dst->pair);
666 pair_to_free = p;
667 }
Junio C Hamano427dcb42005-05-21 02:39:09 -0700668 else
Junio C Hamano2cd68882005-05-30 00:08:07 -0700669 /* no matching rename/copy source, so
670 * record this as a creation.
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700671 */
672 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700673 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700674 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
675 /*
676 * Deletion
677 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700678 * We would output this delete record if:
679 *
680 * (1) this is a broken delete and the counterpart
681 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -0700682 * (2) this is not a broken delete, and rename_dst
683 * does not have a rename/copy to move p->one->path
684 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700685 *
686 * Otherwise, the counterpart broken create
687 * has been turned into a rename-edit; or
688 * delete did not have a matching create to
689 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -0700690 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700691 if (DIFF_PAIR_BROKEN(p)) {
692 /* broken delete */
Jeff Kingf98c2f72015-02-26 20:39:48 -0500693 struct diff_rename_dst *dst = locate_rename_dst(p->one);
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700694 if (dst && dst->pair)
695 /* counterpart is now rename/copy */
696 pair_to_free = p;
697 }
698 else {
Linus Torvalds64479712007-10-25 11:20:56 -0700699 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700700 /* this path remains */
701 pair_to_free = p;
702 }
Junio C Hamano2cd68882005-05-30 00:08:07 -0700703
704 if (pair_to_free)
705 ;
706 else
707 diff_q(&outq, p);
708 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700709 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -0700710 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700711 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -0700712 else
713 /* no need to keep unmodified pairs */
714 pair_to_free = p;
715
Junio C Hamano226406f2005-05-27 15:50:30 -0700716 if (pair_to_free)
717 diff_free_filepair(pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700718 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700719 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700720
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700721 free(q->queue);
722 *q = outq;
723 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700724
Linus Torvalds9fb88412007-10-25 11:19:10 -0700725 for (i = 0; i < rename_dst_nr; i++)
726 free_filespec(rename_dst[i].two);
Junio C Hamano5098baf2005-09-15 16:13:43 -0700727
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000728 FREE_AND_NULL(rename_dst);
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700729 rename_dst_nr = rename_dst_alloc = 0;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000730 FREE_AND_NULL(rename_src);
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700731 rename_src_nr = rename_src_alloc = 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700732 return;
733}