blob: 5bc559f79e95a0dc215c9058c87d06ddd3016e4e [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"
Elijah Newren9db2ac52020-12-11 09:08:47 +000012#include "strmap.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -070013
Junio C Hamano25d5ea42005-05-24 01:10:48 -070014/* Table of rename/copy destinations */
Junio C Hamano427dcb42005-05-21 02:39:09 -070015
Junio C Hamano25d5ea42005-05-24 01:10:48 -070016static struct diff_rename_dst {
Elijah Newren9db2ac52020-12-11 09:08:47 +000017 struct diff_filepair *p;
18 struct diff_filespec *filespec_to_free;
19 int is_rename; /* false -> just a create; true -> rename or copy */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070020} *rename_dst;
21static int rename_dst_nr, rename_dst_alloc;
Elijah Newren9db2ac52020-12-11 09:08:47 +000022/* Mapping from break source pathname to break destination index */
23static struct strintmap *break_idx = NULL;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070024
Elijah Newren9db2ac52020-12-11 09:08:47 +000025static struct diff_rename_dst *locate_rename_dst(struct diff_filepair *p)
Junio C Hamano427dcb42005-05-21 02:39:09 -070026{
Elijah Newren9db2ac52020-12-11 09:08:47 +000027 /* Lookup by p->ONE->path */
28 int idx = break_idx ? strintmap_get(break_idx, p->one->path) : -1;
29 return (idx == -1) ? NULL : &rename_dst[idx];
Jeff Kingf98c2f72015-02-26 20:39:48 -050030}
31
32/*
33 * Returns 0 on success, -1 if we found a duplicate.
34 */
Elijah Newren9db2ac52020-12-11 09:08:47 +000035static int add_rename_dst(struct diff_filepair *p)
Jeff Kingf98c2f72015-02-26 20:39:48 -050036{
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040037 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
Elijah Newren9db2ac52020-12-11 09:08:47 +000038 rename_dst[rename_dst_nr].p = p;
39 rename_dst[rename_dst_nr].filespec_to_free = NULL;
40 rename_dst[rename_dst_nr].is_rename = 0;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070041 rename_dst_nr++;
Jeff Kingf98c2f72015-02-26 20:39:48 -050042 return 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -070043}
44
Junio C Hamano15d061b2005-05-27 15:55:55 -070045/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070046static struct diff_rename_src {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080047 struct diff_filepair *p;
Junio C Hamanofc580712006-04-08 20:17:46 -070048 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070049} *rename_src;
50static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070051
Elijah Newrenb970b4e2020-12-11 09:08:46 +000052static void register_rename_src(struct diff_filepair *p)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070053{
Elijah Newren9db2ac52020-12-11 09:08:47 +000054 if (p->broken_pair) {
55 if (!break_idx) {
56 break_idx = xmalloc(sizeof(*break_idx));
Elijah Newren61bf4492021-06-08 16:11:40 +000057 strintmap_init_with_options(break_idx, -1, NULL, 0);
Elijah Newren9db2ac52020-12-11 09:08:47 +000058 }
59 strintmap_set(break_idx, p->one->path, rename_dst_nr);
60 }
61
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040062 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
Elijah Newrenb970b4e2020-12-11 09:08:46 +000063 rename_src[rename_src_nr].p = p;
64 rename_src[rename_src_nr].score = p->score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070065 rename_src_nr++;
Junio C Hamano427dcb42005-05-21 02:39:09 -070066}
67
Johannes Schindelin0ce39642007-06-21 12:52:11 +010068static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
69{
70 int src_len = strlen(src->path), dst_len = strlen(dst->path);
71 while (src_len && dst_len) {
72 char c1 = src->path[--src_len];
73 char c2 = dst->path[--dst_len];
74 if (c1 != c2)
75 return 0;
76 if (c1 == '/')
77 return 1;
78 }
79 return (!src_len || src->path[src_len - 1] == '/') &&
80 (!dst_len || dst->path[dst_len - 1] == '/');
81}
82
Junio C Hamano427dcb42005-05-21 02:39:09 -070083struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -070084 int src; /* index in rename_src */
85 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -080086 unsigned short score;
87 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -070088};
89
Elijah Newren1aedd032021-06-22 08:04:40 +000090struct inexact_prefetch_options {
Jonathan Tan95acf112020-04-07 15:11:43 -070091 struct repository *repo;
92 int skip_unmodified;
93};
Elijah Newren1aedd032021-06-22 08:04:40 +000094static void inexact_prefetch(void *prefetch_options)
Jonathan Tan95acf112020-04-07 15:11:43 -070095{
Elijah Newren1aedd032021-06-22 08:04:40 +000096 struct inexact_prefetch_options *options = prefetch_options;
Jonathan Tan95acf112020-04-07 15:11:43 -070097 int i;
98 struct oid_array to_fetch = OID_ARRAY_INIT;
99
100 for (i = 0; i < rename_dst_nr; i++) {
Elijah Newren9db2ac52020-12-11 09:08:47 +0000101 if (rename_dst[i].p->renamed_pair)
Jonathan Tan95acf112020-04-07 15:11:43 -0700102 /*
103 * The loop in diffcore_rename() will not need these
104 * blobs, so skip prefetching.
105 */
106 continue; /* already found exact match */
107 diff_add_if_missing(options->repo, &to_fetch,
Elijah Newren9db2ac52020-12-11 09:08:47 +0000108 rename_dst[i].p->two);
Jonathan Tan95acf112020-04-07 15:11:43 -0700109 }
110 for (i = 0; i < rename_src_nr; i++) {
111 if (options->skip_unmodified &&
112 diff_unmodified_pair(rename_src[i].p))
113 /*
114 * The loop in diffcore_rename() will not need these
115 * blobs, so skip prefetching.
116 */
117 continue;
118 diff_add_if_missing(options->repo, &to_fetch,
119 rename_src[i].p->one);
120 }
121 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
122 oid_array_clear(&to_fetch);
123}
124
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200125static int estimate_similarity(struct repository *r,
126 struct diff_filespec *src,
Junio C Hamano427dcb42005-05-21 02:39:09 -0700127 struct diff_filespec *dst,
Jonathan Tan95acf112020-04-07 15:11:43 -0700128 int minimum_score,
Elijah Newrend331dd32021-06-22 08:04:39 +0000129 struct diff_populate_filespec_options *dpf_opt)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700130{
131 /* src points at a file that existed in the original tree (or
132 * optionally a file in the destination tree) and dst points
133 * at a newly created file. They may be quite similar, in which
134 * case we want to say src is renamed to dst or src is copied into
135 * dst, and then some edit has been applied to dst.
136 *
137 * Compare them and return how similar they are, representing
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700138 * the score as an integer between 0 and MAX_SCORE.
139 *
140 * When there is an exact match, it is considered a better
141 * match than anything else; the destination does not even
142 * call into this function in that case.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700143 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800144 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700145 int score;
146
Junio C Hamano60896c72005-05-22 21:24:49 -0700147 /* We deal only with regular files. Symlink renames are handled
148 * only when they are exact matches --- in other words, no edits
149 * after renaming.
150 */
151 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
152 return 0;
153
Linus Torvalds81ac0512007-10-26 16:51:28 -0700154 /*
155 * Need to check that source and destination sizes are
156 * filled in before comparing them.
157 *
158 * If we already have "cnt_data" filled in, we know it's
159 * all good (avoid checking the size for zero, as that
160 * is a possible size - we really should have a flag to
161 * say whether the size is valid or not!)
162 */
Elijah Newrend331dd32021-06-22 08:04:39 +0000163 dpf_opt->check_size_only = 1;
164
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700165 if (!src->cnt_data &&
Elijah Newrend331dd32021-06-22 08:04:39 +0000166 diff_populate_filespec(r, src, dpf_opt))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700167 return 0;
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700168 if (!dst->cnt_data &&
Elijah Newrend331dd32021-06-22 08:04:39 +0000169 diff_populate_filespec(r, dst, dpf_opt))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700170 return 0;
171
Linus Torvalds90bd9322006-03-12 22:26:34 -0800172 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700173 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800174 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700175
Junio C Hamano58b103f2005-05-21 15:55:18 -0700176 /* We would not consider edits that change the file size so
177 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700178 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700179 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700180 * Note that base_size == 0 case is handled here already
181 * and the final score computation below would not have a
182 * divide-by-zero issue.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700183 */
Linus Torvalds3a4d6762011-02-18 20:12:06 -0800184 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700185 return 0;
186
Elijah Newrend331dd32021-06-22 08:04:39 +0000187 dpf_opt->check_size_only = 0;
Jonathan Tan95acf112020-04-07 15:11:43 -0700188
Elijah Newrend331dd32021-06-22 08:04:39 +0000189 if (!src->cnt_data && diff_populate_filespec(r, src, dpf_opt))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100190 return 0;
Elijah Newrend331dd32021-06-22 08:04:39 +0000191 if (!dst->cnt_data && diff_populate_filespec(r, dst, dpf_opt))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100192 return 0;
193
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200194 if (diffcore_count_changes(r, src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800195 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800196 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700197 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700198
Junio C Hamano17063062006-03-02 22:11:25 -0800199 /* How similar are they?
200 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700201 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800202 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800203 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200204 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500205 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700206 return score;
207}
208
Junio C Hamano5098baf2005-09-15 16:13:43 -0700209static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700210{
Elijah Newren9db2ac52020-12-11 09:08:47 +0000211 struct diff_filepair *src = rename_src[src_index].p;
212 struct diff_filepair *dst = rename_dst[dst_index].p;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700213
Elijah Newren9db2ac52020-12-11 09:08:47 +0000214 if (dst->renamed_pair)
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700215 die("internal error: dst already matched.");
216
Elijah Newren9db2ac52020-12-11 09:08:47 +0000217 src->one->rename_used++;
218 src->one->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700219
Elijah Newren9db2ac52020-12-11 09:08:47 +0000220 rename_dst[dst_index].filespec_to_free = dst->one;
221 rename_dst[dst_index].is_rename = 1;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700222
Elijah Newren9db2ac52020-12-11 09:08:47 +0000223 dst->one = src->one;
224 dst->renamed_pair = 1;
225 if (!strcmp(dst->one->path, dst->two->path))
226 dst->score = rename_src[src_index].score;
Junio C Hamanofc580712006-04-08 20:17:46 -0700227 else
Elijah Newren9db2ac52020-12-11 09:08:47 +0000228 dst->score = score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700229}
230
231/*
232 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700233 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700234 */
235static int score_compare(const void *a_, const void *b_)
236{
237 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200238
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800239 /* sink the unused ones to the bottom */
240 if (a->dst < 0)
241 return (0 <= b->dst);
242 else if (b->dst < 0)
243 return -1;
244
René Scharfecfc0aef2007-06-25 00:23:28 +0200245 if (a->score == b->score)
246 return b->name_score - a->name_score;
247
Junio C Hamano427dcb42005-05-21 02:39:09 -0700248 return b->score - a->score;
249}
250
Linus Torvalds9027f532007-10-25 11:23:26 -0700251struct file_similarity {
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100252 struct hashmap_entry entry;
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100253 int index;
Linus Torvalds9027f532007-10-25 11:23:26 -0700254 struct diff_filespec *filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700255};
256
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200257static unsigned int hash_filespec(struct repository *r,
258 struct diff_filespec *filespec)
Karsten Blees48f64072013-11-14 20:19:04 +0100259{
brian m. carlson41c95602016-06-24 23:09:24 +0000260 if (!filespec->oid_valid) {
Jonathan Tan1c37e862020-04-07 15:11:41 -0700261 if (diff_populate_filespec(r, filespec, NULL))
Karsten Blees48f64072013-11-14 20:19:04 +0100262 return 0;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300263 hash_object_file(r->hash_algo, filespec->data, filespec->size,
264 "blob", &filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100265 }
Jeff Kingd40abc82019-06-20 03:41:49 -0400266 return oidhash(&filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100267}
268
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100269static int find_identical_files(struct hashmap *srcs,
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100270 int dst_index,
Linus Torvalds11f944d2011-02-18 19:55:19 -0800271 struct diff_options *options)
Linus Torvalds9027f532007-10-25 11:23:26 -0700272{
273 int renames = 0;
Elijah Newren9db2ac52020-12-11 09:08:47 +0000274 struct diff_filespec *target = rename_dst[dst_index].p->two;
Karsten Bleesab73a9d2014-07-03 00:22:11 +0200275 struct file_similarity *p, *best = NULL;
Karsten Blees48f64072013-11-14 20:19:04 +0100276 int i = 100, best_score = -1;
Eric Wongf0e63c42019-10-06 23:30:35 +0000277 unsigned int hash = hash_filespec(options->repo, target);
Linus Torvalds9027f532007-10-25 11:23:26 -0700278
Karsten Blees48f64072013-11-14 20:19:04 +0100279 /*
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100280 * Find the best source match for specified destination.
Karsten Blees48f64072013-11-14 20:19:04 +0100281 */
Eric Wongf0e63c42019-10-06 23:30:35 +0000282 p = hashmap_get_entry_from_hash(srcs, hash, NULL,
283 struct file_similarity, entry);
Eric Wong23dee692019-10-06 23:30:41 +0000284 hashmap_for_each_entry_from(srcs, p, entry) {
Karsten Blees48f64072013-11-14 20:19:04 +0100285 int score;
286 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700287
Karsten Blees48f64072013-11-14 20:19:04 +0100288 /* False hash collision? */
Jeff King9001dc22018-08-28 17:22:48 -0400289 if (!oideq(&source->oid, &target->oid))
Karsten Blees48f64072013-11-14 20:19:04 +0100290 continue;
291 /* Non-regular files? If so, the modes must match! */
292 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
293 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700294 continue;
Karsten Blees48f64072013-11-14 20:19:04 +0100295 }
296 /* Give higher scores to sources that haven't been used already */
297 score = !source->rename_used;
298 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
299 continue;
300 score += basename_same(source, target);
301 if (score > best_score) {
302 best = p;
303 best_score = score;
304 if (score == 2)
Linus Torvalds9027f532007-10-25 11:23:26 -0700305 break;
306 }
Karsten Blees48f64072013-11-14 20:19:04 +0100307
308 /* Too many identical alternatives? Pick one */
309 if (!--i)
310 break;
311 }
312 if (best) {
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100313 record_rename_pair(dst_index, best->index, MAX_SCORE);
Karsten Blees48f64072013-11-14 20:19:04 +0100314 renames++;
315 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700316 return renames;
317}
318
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200319static void insert_file_table(struct repository *r,
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000320 struct mem_pool *pool,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200321 struct hashmap *table, int index,
322 struct diff_filespec *filespec)
Linus Torvalds9027f532007-10-25 11:23:26 -0700323{
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000324 struct file_similarity *entry = mem_pool_alloc(pool, sizeof(*entry));
Linus Torvalds9027f532007-10-25 11:23:26 -0700325
Linus Torvalds9027f532007-10-25 11:23:26 -0700326 entry->index = index;
327 entry->filespec = filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700328
Eric Wongd22245a2019-10-06 23:30:27 +0000329 hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
Eric Wongb94e5c12019-10-06 23:30:29 +0000330 hashmap_add(table, &entry->entry);
Linus Torvalds9027f532007-10-25 11:23:26 -0700331}
332
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700333/*
334 * Find exact renames first.
335 *
336 * The first round matches up the up-to-date entries,
337 * and then during the second round we try to match
338 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700339 */
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000340static int find_exact_renames(struct diff_options *options,
341 struct mem_pool *pool)
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700342{
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100343 int i, renames = 0;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100344 struct hashmap file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700345
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200346 /* Add all sources to the hash table in reverse order, because
347 * later on they will be retrieved in LIFO order.
348 */
Stefan Beller7663cdc2017-06-30 12:14:05 -0700349 hashmap_init(&file_table, NULL, NULL, rename_src_nr);
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200350 for (i = rename_src_nr-1; i >= 0; i--)
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000351 insert_file_table(options->repo, pool,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200352 &file_table, i,
353 rename_src[i].p->one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700354
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100355 /* Walk the destinations and find best source match */
Linus Torvalds9027f532007-10-25 11:23:26 -0700356 for (i = 0; i < rename_dst_nr; i++)
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100357 renames += find_identical_files(&file_table, i, options);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700358
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000359 /* Free the hash data structure (entries will be freed with the pool) */
360 hashmap_clear(&file_table);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700361
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100362 return renames;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700363}
364
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000365struct dir_rename_info {
366 struct strintmap idx_map;
367 struct strmap dir_rename_guess;
368 struct strmap *dir_rename_count;
Elijah Newrena49b55d2021-03-13 22:22:02 +0000369 struct strintmap *relevant_source_dirs;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000370 unsigned setup;
371};
372
373static char *get_dirname(const char *filename)
374{
375 char *slash = strrchr(filename, '/');
376 return slash ? xstrndup(filename, slash - filename) : xstrdup("");
377}
378
Elijah Newren0c4fd732021-02-27 00:30:42 +0000379static void dirname_munge(char *filename)
380{
381 char *slash = strrchr(filename, '/');
382 if (!slash)
383 slash = filename;
384 *slash = '\0';
385}
386
Elijah Newren81afdf72021-02-27 00:30:48 +0000387static const char *get_highest_rename_path(struct strintmap *counts)
388{
389 int highest_count = 0;
390 const char *highest_destination_dir = NULL;
391 struct hashmap_iter iter;
392 struct strmap_entry *entry;
393
394 strintmap_for_each_entry(counts, &iter, entry) {
395 const char *destination_dir = entry->key;
396 intptr_t count = (intptr_t)entry->value;
397 if (count > highest_count) {
398 highest_count = count;
399 highest_destination_dir = destination_dir;
400 }
401 }
402 return highest_destination_dir;
403}
404
Elijah Newren0491d392021-03-13 22:22:05 +0000405static char *UNKNOWN_DIR = "/"; /* placeholder -- short, illegal directory */
406
407static int dir_rename_already_determinable(struct strintmap *counts)
408{
409 struct hashmap_iter iter;
410 struct strmap_entry *entry;
411 int first = 0, second = 0, unknown = 0;
412 strintmap_for_each_entry(counts, &iter, entry) {
413 const char *destination_dir = entry->key;
414 intptr_t count = (intptr_t)entry->value;
415 if (!strcmp(destination_dir, UNKNOWN_DIR)) {
416 unknown = count;
417 } else if (count >= first) {
418 second = first;
419 first = count;
420 } else if (count >= second) {
421 second = count;
422 }
423 }
424 return first > second + unknown;
425}
426
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000427static void increment_count(struct dir_rename_info *info,
Elijah Newren0c4fd732021-02-27 00:30:42 +0000428 char *old_dir,
429 char *new_dir)
430{
431 struct strintmap *counts;
432 struct strmap_entry *e;
433
434 /* Get the {new_dirs -> counts} mapping using old_dir */
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000435 e = strmap_get_entry(info->dir_rename_count, old_dir);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000436 if (e) {
437 counts = e->value;
438 } else {
439 counts = xmalloc(sizeof(*counts));
440 strintmap_init_with_options(counts, 0, NULL, 1);
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000441 strmap_put(info->dir_rename_count, old_dir, counts);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000442 }
443
444 /* Increment the count for new_dir */
445 strintmap_incr(counts, new_dir, 1);
446}
447
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000448static void update_dir_rename_counts(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000449 struct strintmap *dirs_removed,
Elijah Newren0c4fd732021-02-27 00:30:42 +0000450 const char *oldname,
451 const char *newname)
452{
453 char *old_dir = xstrdup(oldname);
454 char *new_dir = xstrdup(newname);
455 char new_dir_first_char = new_dir[0];
456 int first_time_in_loop = 1;
457
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000458 if (!info->setup)
459 /*
460 * info->setup is 0 here in two cases: (1) all auxiliary
461 * vars (like dirs_removed) were NULL so
462 * initialize_dir_rename_info() returned early, or (2)
463 * either break detection or copy detection are active so
464 * that we never called initialize_dir_rename_info(). In
465 * the former case, we don't have enough info to know if
466 * directories were renamed (because dirs_removed lets us
467 * know about a necessary prerequisite, namely if they were
468 * removed), and in the latter, we don't care about
469 * directory renames or find_basename_matches.
470 *
471 * This matters because both basename and inexact matching
472 * will also call update_dir_rename_counts(). In either of
473 * the above two cases info->dir_rename_counts will not
474 * have been properly initialized which prevents us from
475 * updating it, but in these two cases we don't care about
476 * dir_rename_counts anyway, so we can just exit early.
477 */
478 return;
479
Elijah Newren0c4fd732021-02-27 00:30:42 +0000480 while (1) {
Elijah Newrene54385b2021-03-13 22:22:04 +0000481 int drd_flag = NOT_RELEVANT;
482
Elijah Newren333899e2021-02-27 00:30:47 +0000483 /* Get old_dir, skip if its directory isn't relevant. */
Elijah Newren0c4fd732021-02-27 00:30:42 +0000484 dirname_munge(old_dir);
Elijah Newren333899e2021-02-27 00:30:47 +0000485 if (info->relevant_source_dirs &&
Elijah Newrena49b55d2021-03-13 22:22:02 +0000486 !strintmap_contains(info->relevant_source_dirs, old_dir))
Elijah Newren333899e2021-02-27 00:30:47 +0000487 break;
488
489 /* Get new_dir */
Elijah Newren0c4fd732021-02-27 00:30:42 +0000490 dirname_munge(new_dir);
491
492 /*
493 * When renaming
494 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
495 * then this suggests that both
496 * a/b/c/d/e/ => a/b/some/thing/else/e/
497 * a/b/c/d/ => a/b/some/thing/else/
498 * so we want to increment counters for both. We do NOT,
499 * however, also want to suggest that there was the following
500 * rename:
501 * a/b/c/ => a/b/some/thing/
502 * so we need to quit at that point.
503 *
504 * Note the when first_time_in_loop, we only strip off the
505 * basename, and we don't care if that's different.
506 */
507 if (!first_time_in_loop) {
508 char *old_sub_dir = strchr(old_dir, '\0')+1;
509 char *new_sub_dir = strchr(new_dir, '\0')+1;
510 if (!*new_dir) {
511 /*
512 * Special case when renaming to root directory,
513 * i.e. when new_dir == "". In this case, we had
514 * something like
515 * a/b/subdir => subdir
516 * and so dirname_munge() sets things up so that
517 * old_dir = "a/b\0subdir\0"
518 * new_dir = "\0ubdir\0"
519 * We didn't have a '/' to overwrite a '\0' onto
520 * in new_dir, so we have to compare differently.
521 */
522 if (new_dir_first_char != old_sub_dir[0] ||
523 strcmp(old_sub_dir+1, new_sub_dir))
524 break;
525 } else {
526 if (strcmp(old_sub_dir, new_sub_dir))
527 break;
528 }
529 }
530
Elijah Newrene54385b2021-03-13 22:22:04 +0000531 /*
532 * Above we suggested that we'd keep recording renames for
533 * all ancestor directories where the trailing directories
534 * matched, i.e. for
535 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
536 * we'd increment rename counts for each of
537 * a/b/c/d/e/ => a/b/some/thing/else/e/
538 * a/b/c/d/ => a/b/some/thing/else/
539 * However, we only need the rename counts for directories
540 * in dirs_removed whose value is RELEVANT_FOR_SELF.
541 * However, we add one special case of also recording it for
542 * first_time_in_loop because find_basename_matches() can
543 * use that as a hint to find a good pairing.
544 */
545 if (dirs_removed)
546 drd_flag = strintmap_get(dirs_removed, old_dir);
547 if (drd_flag == RELEVANT_FOR_SELF || first_time_in_loop)
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000548 increment_count(info, old_dir, new_dir);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000549
Elijah Newrene54385b2021-03-13 22:22:04 +0000550 first_time_in_loop = 0;
551 if (drd_flag == NOT_RELEVANT)
552 break;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000553 /* If we hit toplevel directory ("") for old or new dir, quit */
554 if (!*old_dir || !*new_dir)
555 break;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000556 }
557
558 /* Free resources we don't need anymore */
559 free(old_dir);
560 free(new_dir);
561}
562
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000563static void initialize_dir_rename_info(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000564 struct strintmap *relevant_sources,
565 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +0000566 struct strmap *dir_rename_count,
567 struct strmap *cached_pairs)
Elijah Newren0c4fd732021-02-27 00:30:42 +0000568{
Elijah Newren81afdf72021-02-27 00:30:48 +0000569 struct hashmap_iter iter;
570 struct strmap_entry *entry;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000571 int i;
572
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000573 if (!dirs_removed && !relevant_sources) {
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000574 info->setup = 0;
575 return;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000576 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000577 info->setup = 1;
578
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000579 info->dir_rename_count = dir_rename_count;
580 if (!info->dir_rename_count) {
581 info->dir_rename_count = xmalloc(sizeof(*dir_rename_count));
582 strmap_init(info->dir_rename_count);
583 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000584 strintmap_init_with_options(&info->idx_map, -1, NULL, 0);
585 strmap_init_with_options(&info->dir_rename_guess, NULL, 0);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000586
Elijah Newren333899e2021-02-27 00:30:47 +0000587 /* Setup info->relevant_source_dirs */
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000588 info->relevant_source_dirs = NULL;
589 if (dirs_removed || !relevant_sources) {
590 info->relevant_source_dirs = dirs_removed; /* might be NULL */
591 } else {
592 info->relevant_source_dirs = xmalloc(sizeof(struct strintmap));
Elijah Newrena49b55d2021-03-13 22:22:02 +0000593 strintmap_init(info->relevant_source_dirs, 0 /* unused */);
594 strintmap_for_each_entry(relevant_sources, &iter, entry) {
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000595 char *dirname = get_dirname(entry->key);
596 if (!dirs_removed ||
Elijah Newrena49b55d2021-03-13 22:22:02 +0000597 strintmap_contains(dirs_removed, dirname))
598 strintmap_set(info->relevant_source_dirs,
599 dirname, 0 /* value irrelevant */);
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000600 free(dirname);
601 }
602 }
Elijah Newren333899e2021-02-27 00:30:47 +0000603
Elijah Newrenae8cf742021-02-27 00:30:41 +0000604 /*
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000605 * Loop setting up both info->idx_map, and doing setup of
606 * info->dir_rename_count.
Elijah Newrenae8cf742021-02-27 00:30:41 +0000607 */
608 for (i = 0; i < rename_dst_nr; ++i) {
609 /*
610 * For non-renamed files, make idx_map contain mapping of
611 * filename -> index (index within rename_dst, that is)
612 */
613 if (!rename_dst[i].is_rename) {
614 char *filename = rename_dst[i].p->two->path;
615 strintmap_set(&info->idx_map, filename, i);
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000616 continue;
Elijah Newrenae8cf742021-02-27 00:30:41 +0000617 }
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000618
619 /*
620 * For everything else (i.e. renamed files), make
621 * dir_rename_count contain a map of a map:
622 * old_directory -> {new_directory -> count}
623 * In other words, for every pair look at the directories for
624 * the old filename and the new filename and count how many
625 * times that pairing occurs.
626 */
627 update_dir_rename_counts(info, dirs_removed,
628 rename_dst[i].p->one->path,
629 rename_dst[i].p->two->path);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000630 }
Elijah Newren81afdf72021-02-27 00:30:48 +0000631
Elijah Newren25e65b62021-05-20 06:09:41 +0000632 /* Add cached_pairs to counts */
633 strmap_for_each_entry(cached_pairs, &iter, entry) {
634 const char *old_name = entry->key;
635 const char *new_name = entry->value;
636 if (!new_name)
637 /* known delete; ignore it */
638 continue;
639
640 update_dir_rename_counts(info, dirs_removed, old_name, new_name);
641 }
642
Elijah Newren81afdf72021-02-27 00:30:48 +0000643 /*
644 * Now we collapse
645 * dir_rename_count: old_directory -> {new_directory -> count}
646 * down to
647 * dir_rename_guess: old_directory -> best_new_directory
648 * where best_new_directory is the one with the highest count.
649 */
650 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
651 /* entry->key is source_dir */
652 struct strintmap *counts = entry->value;
653 char *best_newdir;
654
655 best_newdir = xstrdup(get_highest_rename_path(counts));
656 strmap_put(&info->dir_rename_guess, entry->key,
657 best_newdir);
658 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000659}
660
Elijah Newrencd52e002021-02-27 00:30:43 +0000661void partial_clear_dir_rename_count(struct strmap *dir_rename_count)
662{
663 struct hashmap_iter iter;
664 struct strmap_entry *entry;
665
666 strmap_for_each_entry(dir_rename_count, &iter, entry) {
667 struct strintmap *counts = entry->value;
668 strintmap_clear(counts);
669 }
670 strmap_partial_clear(dir_rename_count, 1);
671}
672
Elijah Newrenb1473012021-02-27 00:30:45 +0000673static void cleanup_dir_rename_info(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000674 struct strintmap *dirs_removed,
Elijah Newrenb1473012021-02-27 00:30:45 +0000675 int keep_dir_rename_count)
Elijah Newrenae8cf742021-02-27 00:30:41 +0000676{
Elijah Newrenb1473012021-02-27 00:30:45 +0000677 struct hashmap_iter iter;
678 struct strmap_entry *entry;
679 struct string_list to_remove = STRING_LIST_INIT_NODUP;
680 int i;
681
Elijah Newrenae8cf742021-02-27 00:30:41 +0000682 if (!info->setup)
683 return;
684
685 /* idx_map */
686 strintmap_clear(&info->idx_map);
687
688 /* dir_rename_guess */
689 strmap_clear(&info->dir_rename_guess, 1);
690
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000691 /* relevant_source_dirs */
692 if (info->relevant_source_dirs &&
693 info->relevant_source_dirs != dirs_removed) {
Elijah Newrena49b55d2021-03-13 22:22:02 +0000694 strintmap_clear(info->relevant_source_dirs);
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000695 FREE_AND_NULL(info->relevant_source_dirs);
696 }
697
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000698 /* dir_rename_count */
Elijah Newrenb1473012021-02-27 00:30:45 +0000699 if (!keep_dir_rename_count) {
700 partial_clear_dir_rename_count(info->dir_rename_count);
701 strmap_clear(info->dir_rename_count, 1);
702 FREE_AND_NULL(info->dir_rename_count);
703 return;
704 }
705
706 /*
707 * Although dir_rename_count was passed in
708 * diffcore_rename_extended() and we want to keep it around and
Elijah Newrenbf238b72021-03-13 22:22:06 +0000709 * return it to that caller, we first want to remove any counts in
710 * the maps associated with UNKNOWN_DIR entries and any data
Elijah Newrenb1473012021-02-27 00:30:45 +0000711 * associated with directories that weren't renamed.
712 */
713 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
714 const char *source_dir = entry->key;
715 struct strintmap *counts = entry->value;
716
Elijah Newrenfb529382021-03-13 22:22:03 +0000717 if (!strintmap_get(dirs_removed, source_dir)) {
Elijah Newrenb1473012021-02-27 00:30:45 +0000718 string_list_append(&to_remove, source_dir);
719 strintmap_clear(counts);
720 continue;
721 }
Elijah Newrenbf238b72021-03-13 22:22:06 +0000722
723 if (strintmap_contains(counts, UNKNOWN_DIR))
724 strintmap_remove(counts, UNKNOWN_DIR);
Elijah Newrenb1473012021-02-27 00:30:45 +0000725 }
726 for (i = 0; i < to_remove.nr; ++i)
727 strmap_remove(info->dir_rename_count,
728 to_remove.items[i].string, 1);
729 string_list_clear(&to_remove, 0);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000730}
731
Elijah Newrena35df332021-02-14 07:51:47 +0000732static const char *get_basename(const char *filename)
733{
734 /*
735 * gitbasename() has to worry about special drives, multiple
736 * directory separator characters, trailing slashes, NULL or
737 * empty strings, etc. We only work on filenames as stored in
738 * git, and thus get to ignore all those complications.
739 */
740 const char *base = strrchr(filename, '/');
741 return base ? base + 1 : filename;
742}
743
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000744static int idx_possible_rename(char *filename, struct dir_rename_info *info)
Elijah Newren37a25142021-02-27 00:30:39 +0000745{
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000746 /*
747 * Our comparison of files with the same basename (see
748 * find_basename_matches() below), is only helpful when after exact
749 * rename detection we have exactly one file with a given basename
750 * among the rename sources and also only exactly one file with
751 * that basename among the rename destinations. When we have
752 * multiple files with the same basename in either set, we do not
753 * know which to compare against. However, there are some
754 * filenames that occur in large numbers (particularly
755 * build-related filenames such as 'Makefile', '.gitignore', or
756 * 'build.gradle' that potentially exist within every single
757 * subdirectory), and for performance we want to be able to quickly
758 * find renames for these files too.
759 *
760 * The reason basename comparisons are a useful heuristic was that it
761 * is common for people to move files across directories while keeping
762 * their filename the same. If we had a way of determining or even
763 * making a good educated guess about which directory these non-unique
764 * basename files had moved the file to, we could check it.
765 * Luckily...
766 *
767 * When an entire directory is in fact renamed, we have two factors
768 * helping us out:
769 * (a) the original directory disappeared giving us a hint
770 * about when we can apply an extra heuristic.
771 * (a) we often have several files within that directory and
772 * subdirectories that are renamed without changes
773 * So, rules for a heuristic:
774 * (0) If there basename matches are non-unique (the condition under
775 * which this function is called) AND
776 * (1) the directory in which the file was found has disappeared
777 * (i.e. dirs_removed is non-NULL and has a relevant entry) THEN
778 * (2) use exact renames of files within the directory to determine
779 * where the directory is likely to have been renamed to. IF
780 * there is at least one exact rename from within that
781 * directory, we can proceed.
782 * (3) If there are multiple places the directory could have been
783 * renamed to based on exact renames, ignore all but one of them.
784 * Just use the destination with the most renames going to it.
785 * (4) Check if applying that directory rename to the original file
786 * would result in a destination filename that is in the
787 * potential rename set. If so, return the index of the
788 * destination file (the index within rename_dst).
789 * (5) Compare the original file and returned destination for
790 * similarity, and if they are sufficiently similar, record the
791 * rename.
792 *
793 * This function, idx_possible_rename(), is only responsible for (4).
Elijah Newren81afdf72021-02-27 00:30:48 +0000794 * The conditions/steps in (1)-(3) are handled via setting up
795 * dir_rename_count and dir_rename_guess in
796 * initialize_dir_rename_info(). Steps (0) and (5) are handled by
797 * the caller of this function.
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000798 */
799 char *old_dir, *new_dir;
800 struct strbuf new_path = STRBUF_INIT;
801 int idx;
802
803 if (!info->setup)
804 return -1;
805
806 old_dir = get_dirname(filename);
807 new_dir = strmap_get(&info->dir_rename_guess, old_dir);
808 free(old_dir);
809 if (!new_dir)
810 return -1;
811
812 strbuf_addstr(&new_path, new_dir);
813 strbuf_addch(&new_path, '/');
814 strbuf_addstr(&new_path, get_basename(filename));
815
816 idx = strintmap_get(&info->idx_map, new_path.buf);
817 strbuf_release(&new_path);
818 return idx;
Elijah Newren37a25142021-02-27 00:30:39 +0000819}
820
Elijah Newren1aedd032021-06-22 08:04:40 +0000821struct basename_prefetch_options {
822 struct repository *repo;
823 struct strintmap *relevant_sources;
824 struct strintmap *sources;
825 struct strintmap *dests;
826 struct dir_rename_info *info;
827};
828static void basename_prefetch(void *prefetch_options)
829{
830 struct basename_prefetch_options *options = prefetch_options;
831 struct strintmap *relevant_sources = options->relevant_sources;
832 struct strintmap *sources = options->sources;
833 struct strintmap *dests = options->dests;
834 struct dir_rename_info *info = options->info;
835 int i;
836 struct oid_array to_fetch = OID_ARRAY_INIT;
837
838 /*
839 * TODO: The following loops mirror the code/logic from
840 * find_basename_matches(), though not quite exactly. Maybe
841 * abstract the iteration logic out somehow?
842 */
843 for (i = 0; i < rename_src_nr; ++i) {
844 char *filename = rename_src[i].p->one->path;
845 const char *base = NULL;
846 intptr_t src_index;
847 intptr_t dst_index;
848
849 /* Skip irrelevant sources */
850 if (relevant_sources &&
851 !strintmap_contains(relevant_sources, filename))
852 continue;
853
854 /*
855 * If the basename is unique among remaining sources, then
856 * src_index will equal 'i' and we can attempt to match it
857 * to a unique basename in the destinations. Otherwise,
858 * use directory rename heuristics, if possible.
859 */
860 base = get_basename(filename);
861 src_index = strintmap_get(sources, base);
862 assert(src_index == -1 || src_index == i);
863
864 if (strintmap_contains(dests, base)) {
865 struct diff_filespec *one, *two;
866
867 /* Find a matching destination, if possible */
868 dst_index = strintmap_get(dests, base);
869 if (src_index == -1 || dst_index == -1) {
870 src_index = i;
871 dst_index = idx_possible_rename(filename, info);
872 }
873 if (dst_index == -1)
874 continue;
875
876 /* Ignore this dest if already used in a rename */
877 if (rename_dst[dst_index].is_rename)
878 continue; /* already used previously */
879
880 one = rename_src[src_index].p->one;
881 two = rename_dst[dst_index].p->two;
882
883 /* Add the pairs */
884 diff_add_if_missing(options->repo, &to_fetch, two);
885 diff_add_if_missing(options->repo, &to_fetch, one);
886 }
887 }
888
889 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
890 oid_array_clear(&to_fetch);
891}
892
Elijah Newrena35df332021-02-14 07:51:47 +0000893static int find_basename_matches(struct diff_options *options,
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000894 int minimum_score,
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000895 struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000896 struct strintmap *relevant_sources,
897 struct strintmap *dirs_removed)
Elijah Newrena35df332021-02-14 07:51:47 +0000898{
Elijah Newrenda09f652021-02-14 07:51:48 +0000899 /*
900 * When I checked in early 2020, over 76% of file renames in linux
901 * just moved files to a different directory but kept the same
902 * basename. gcc did that with over 64% of renames, gecko did it
903 * with over 79%, and WebKit did it with over 89%.
904 *
905 * Therefore we can bypass the normal exhaustive NxM matrix
906 * comparison of similarities between all potential rename sources
907 * and destinations by instead using file basename as a hint (i.e.
908 * the portion of the filename after the last '/'), checking for
909 * similarity between files with the same basename, and if we find
910 * a pair that are sufficiently similar, record the rename pair and
911 * exclude those two from the NxM matrix.
912 *
913 * This *might* cause us to find a less than optimal pairing (if
914 * there is another file that we are even more similar to but has a
915 * different basename). Given the huge performance advantage
916 * basename matching provides, and given the frequency with which
917 * people use the same basename in real world projects, that's a
918 * trade-off we are willing to accept when doing just rename
919 * detection.
920 *
921 * If someone wants copy detection that implies they are willing to
922 * spend more cycles to find similarities between files, so it may
923 * be less likely that this heuristic is wanted. If someone is
924 * doing break detection, that means they do not want filename
925 * similarity to imply any form of content similiarity, and thus
926 * this heuristic would definitely be incompatible.
927 */
928
929 int i, renames = 0;
Elijah Newrena35df332021-02-14 07:51:47 +0000930 struct strintmap sources;
931 struct strintmap dests;
Elijah Newrend331dd32021-06-22 08:04:39 +0000932 struct diff_populate_filespec_options dpf_options = {
933 .check_binary = 0,
934 .missing_object_cb = NULL,
935 .missing_object_data = NULL
936 };
Elijah Newren1aedd032021-06-22 08:04:40 +0000937 struct basename_prefetch_options prefetch_options = {
Elijah Newrend331dd32021-06-22 08:04:39 +0000938 .repo = options->repo,
Elijah Newren1aedd032021-06-22 08:04:40 +0000939 .relevant_sources = relevant_sources,
940 .sources = &sources,
941 .dests = &dests,
942 .info = info
Elijah Newrend331dd32021-06-22 08:04:39 +0000943 };
Elijah Newrena35df332021-02-14 07:51:47 +0000944
945 /*
946 * Create maps of basename -> fullname(s) for remaining sources and
947 * dests.
948 */
949 strintmap_init_with_options(&sources, -1, NULL, 0);
950 strintmap_init_with_options(&dests, -1, NULL, 0);
951 for (i = 0; i < rename_src_nr; ++i) {
952 char *filename = rename_src[i].p->one->path;
953 const char *base;
954
955 /* exact renames removed in remove_unneeded_paths_from_src() */
956 assert(!rename_src[i].p->one->rename_used);
957
958 /* Record index within rename_src (i) if basename is unique */
959 base = get_basename(filename);
960 if (strintmap_contains(&sources, base))
961 strintmap_set(&sources, base, -1);
962 else
963 strintmap_set(&sources, base, i);
964 }
965 for (i = 0; i < rename_dst_nr; ++i) {
966 char *filename = rename_dst[i].p->two->path;
967 const char *base;
968
969 if (rename_dst[i].is_rename)
970 continue; /* involved in exact match already. */
971
972 /* Record index within rename_dst (i) if basename is unique */
973 base = get_basename(filename);
974 if (strintmap_contains(&dests, base))
975 strintmap_set(&dests, base, -1);
976 else
977 strintmap_set(&dests, base, i);
978 }
979
Elijah Newrend331dd32021-06-22 08:04:39 +0000980 if (options->repo == the_repository && has_promisor_remote()) {
Elijah Newren1aedd032021-06-22 08:04:40 +0000981 dpf_options.missing_object_cb = basename_prefetch;
Elijah Newrend331dd32021-06-22 08:04:39 +0000982 dpf_options.missing_object_data = &prefetch_options;
983 }
984
Elijah Newrenda09f652021-02-14 07:51:48 +0000985 /* Now look for basename matchups and do similarity estimation */
Elijah Newren37a25142021-02-27 00:30:39 +0000986 for (i = 0; i < rename_src_nr; ++i) {
987 char *filename = rename_src[i].p->one->path;
988 const char *base = NULL;
989 intptr_t src_index;
Elijah Newrenda09f652021-02-14 07:51:48 +0000990 intptr_t dst_index;
Elijah Newrenda09f652021-02-14 07:51:48 +0000991
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000992 /* Skip irrelevant sources */
993 if (relevant_sources &&
Elijah Newrena49b55d2021-03-13 22:22:02 +0000994 !strintmap_contains(relevant_sources, filename))
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000995 continue;
996
Elijah Newren37a25142021-02-27 00:30:39 +0000997 /*
998 * If the basename is unique among remaining sources, then
999 * src_index will equal 'i' and we can attempt to match it
1000 * to a unique basename in the destinations. Otherwise,
1001 * use directory rename heuristics, if possible.
1002 */
1003 base = get_basename(filename);
1004 src_index = strintmap_get(&sources, base);
1005 assert(src_index == -1 || src_index == i);
1006
1007 if (strintmap_contains(&dests, base)) {
Elijah Newrenda09f652021-02-14 07:51:48 +00001008 struct diff_filespec *one, *two;
1009 int score;
1010
Elijah Newren37a25142021-02-27 00:30:39 +00001011 /* Find a matching destination, if possible */
1012 dst_index = strintmap_get(&dests, base);
1013 if (src_index == -1 || dst_index == -1) {
1014 src_index = i;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001015 dst_index = idx_possible_rename(filename, info);
Elijah Newren37a25142021-02-27 00:30:39 +00001016 }
1017 if (dst_index == -1)
1018 continue;
1019
1020 /* Ignore this dest if already used in a rename */
1021 if (rename_dst[dst_index].is_rename)
1022 continue; /* already used previously */
1023
Elijah Newrenda09f652021-02-14 07:51:48 +00001024 /* Estimate the similarity */
1025 one = rename_src[src_index].p->one;
1026 two = rename_dst[dst_index].p->two;
1027 score = estimate_similarity(options->repo, one, two,
Elijah Newrend331dd32021-06-22 08:04:39 +00001028 minimum_score, &dpf_options);
Elijah Newrenda09f652021-02-14 07:51:48 +00001029
1030 /* If sufficiently similar, record as rename pair */
1031 if (score < minimum_score)
1032 continue;
1033 record_rename_pair(dst_index, src_index, score);
1034 renames++;
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001035 update_dir_rename_counts(info, dirs_removed,
1036 one->path, two->path);
Elijah Newrenda09f652021-02-14 07:51:48 +00001037
1038 /*
1039 * Found a rename so don't need text anymore; if we
1040 * didn't find a rename, the filespec_blob would get
1041 * re-used when doing the matrix of comparisons.
1042 */
1043 diff_free_filespec_blob(one);
1044 diff_free_filespec_blob(two);
1045 }
1046 }
Elijah Newrena35df332021-02-14 07:51:47 +00001047
1048 strintmap_clear(&sources);
1049 strintmap_clear(&dests);
1050
Elijah Newrenda09f652021-02-14 07:51:48 +00001051 return renames;
Elijah Newrena35df332021-02-14 07:51:47 +00001052}
1053
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001054#define NUM_CANDIDATE_PER_DST 4
1055static void record_if_better(struct diff_score m[], struct diff_score *o)
1056{
1057 int i, worst;
1058
1059 /* find the worst one */
1060 worst = 0;
1061 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
1062 if (score_compare(&m[i], &m[worst]) > 0)
1063 worst = i;
1064
1065 /* is it better than the worst one? */
1066 if (score_compare(&m[worst], o) > 0)
1067 m[worst] = *o;
1068}
1069
Junio C Hamanof31027c2011-01-06 13:50:06 -08001070/*
1071 * Returns:
1072 * 0 if we are under the limit;
1073 * 1 if we need to disable inexact rename detection;
1074 * 2 if we would be under the limit if we were given -C instead of -C -C.
1075 */
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001076static int too_many_rename_candidates(int num_destinations, int num_sources,
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001077 struct diff_options *options)
1078{
1079 int rename_limit = options->rename_limit;
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001080 int i, limited_sources;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001081
1082 options->needed_rename_limit = 0;
1083
1084 /*
1085 * This basically does a test for the rename matrix not
1086 * growing larger than a "rename_limit" square matrix, ie:
1087 *
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001088 * num_destinations * num_sources > rename_limit * rename_limit
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001089 *
1090 * We use st_mult() to check overflow conditions; in the
1091 * exceptional circumstance that size_t isn't large enough to hold
1092 * the multiplication, the system won't be able to allocate enough
1093 * memory for the matrix anyway.
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001094 */
Jonathan Tan89973552017-11-29 12:11:54 -08001095 if (rename_limit <= 0)
1096 rename_limit = 32767;
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001097 if (st_mult(num_destinations, num_sources)
1098 <= st_mult(rename_limit, rename_limit))
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001099 return 0;
1100
1101 options->needed_rename_limit =
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001102 num_sources > num_destinations ? num_sources : num_destinations;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001103
1104 /* Are we running under -C -C? */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001105 if (!options->flags.find_copies_harder)
Junio C Hamanof31027c2011-01-06 13:50:06 -08001106 return 1;
1107
1108 /* Would we bust the limit if we were running under -C? */
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001109 for (limited_sources = i = 0; i < num_sources; i++) {
Junio C Hamanof31027c2011-01-06 13:50:06 -08001110 if (diff_unmodified_pair(rename_src[i].p))
1111 continue;
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001112 limited_sources++;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001113 }
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001114 if (st_mult(num_destinations, limited_sources)
1115 <= st_mult(rename_limit, rename_limit))
Junio C Hamanof31027c2011-01-06 13:50:06 -08001116 return 2;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001117 return 1;
1118}
1119
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001120static int find_renames(struct diff_score *mx,
1121 int dst_cnt,
1122 int minimum_score,
1123 int copies,
1124 struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001125 struct strintmap *dirs_removed)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001126{
1127 int count = 0, i;
1128
1129 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
1130 struct diff_rename_dst *dst;
1131
1132 if ((mx[i].dst < 0) ||
1133 (mx[i].score < minimum_score))
1134 break; /* there is no more usable pair. */
1135 dst = &rename_dst[mx[i].dst];
Elijah Newren9db2ac52020-12-11 09:08:47 +00001136 if (dst->is_rename)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001137 continue; /* already done, either exact or fuzzy. */
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001138 if (!copies && rename_src[mx[i].src].p->one->rename_used)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001139 continue;
1140 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
1141 count++;
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001142 update_dir_rename_counts(info, dirs_removed,
1143 rename_src[mx[i].src].p->one->path,
1144 rename_dst[mx[i].dst].p->two->path);
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001145 }
1146 return count;
1147}
1148
Elijah Newren97998892021-03-11 00:38:24 +00001149static void remove_unneeded_paths_from_src(int detecting_copies,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001150 struct strintmap *interesting)
Elijah Newren829514c2021-02-14 07:35:01 +00001151{
1152 int i, new_num_src;
1153
Elijah Newren97998892021-03-11 00:38:24 +00001154 if (detecting_copies && !interesting)
Elijah Newren829514c2021-02-14 07:35:01 +00001155 return; /* nothing to remove */
1156 if (break_idx)
1157 return; /* culling incompatible with break detection */
1158
1159 /*
1160 * Note on reasons why we cull unneeded sources but not destinations:
1161 * 1) Pairings are stored in rename_dst (not rename_src), which we
1162 * need to keep around. So, we just can't cull rename_dst even
1163 * if we wanted to. But doing so wouldn't help because...
1164 *
1165 * 2) There is a matrix pairwise comparison that follows the
1166 * "Performing inexact rename detection" progress message.
1167 * Iterating over the destinations is done in the outer loop,
1168 * hence we only iterate over each of those once and we can
1169 * easily skip the outer loop early if the destination isn't
1170 * relevant. That's only one check per destination path to
1171 * skip.
1172 *
1173 * By contrast, the sources are iterated in the inner loop; if
1174 * we check whether a source can be skipped, then we'll be
1175 * checking it N separate times, once for each destination.
1176 * We don't want to have to iterate over known-not-needed
1177 * sources N times each, so avoid that by removing the sources
1178 * from rename_src here.
1179 */
1180 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
Elijah Newren97998892021-03-11 00:38:24 +00001181 struct diff_filespec *one = rename_src[i].p->one;
1182
Elijah Newren829514c2021-02-14 07:35:01 +00001183 /*
1184 * renames are stored in rename_dst, so if a rename has
1185 * already been detected using this source, we can just
1186 * remove the source knowing rename_dst has its info.
1187 */
Elijah Newren97998892021-03-11 00:38:24 +00001188 if (!detecting_copies && one->rename_used)
1189 continue;
1190
1191 /* If we don't care about the source path, skip it */
Elijah Newrena49b55d2021-03-13 22:22:02 +00001192 if (interesting && !strintmap_contains(interesting, one->path))
Elijah Newren829514c2021-02-14 07:35:01 +00001193 continue;
1194
1195 if (new_num_src < i)
1196 memcpy(&rename_src[new_num_src], &rename_src[i],
1197 sizeof(struct diff_rename_src));
1198 new_num_src++;
1199 }
1200
1201 rename_src_nr = new_num_src;
1202}
1203
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001204static void handle_early_known_dir_renames(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001205 struct strintmap *relevant_sources,
1206 struct strintmap *dirs_removed)
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001207{
1208 /*
Elijah Newren0491d392021-03-13 22:22:05 +00001209 * Directory renames are determined via an aggregate of all renames
1210 * under them and using a "majority wins" rule. The fact that
1211 * "majority wins", though, means we don't need all the renames
1212 * under the given directory, we only need enough to ensure we have
1213 * a majority.
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001214 */
Elijah Newren0491d392021-03-13 22:22:05 +00001215
Elijah Newren9bd34212021-03-13 22:22:08 +00001216 int i, new_num_src;
Elijah Newren0491d392021-03-13 22:22:05 +00001217 struct hashmap_iter iter;
1218 struct strmap_entry *entry;
1219
1220 if (!dirs_removed || !relevant_sources)
1221 return; /* nothing to cull */
1222 if (break_idx)
1223 return; /* culling incompatbile with break detection */
1224
1225 /*
Elijah Newrenbf238b72021-03-13 22:22:06 +00001226 * Supplement dir_rename_count with number of potential renames,
1227 * marking all potential rename sources as mapping to UNKNOWN_DIR.
Elijah Newren0491d392021-03-13 22:22:05 +00001228 */
Elijah Newrenbf238b72021-03-13 22:22:06 +00001229 for (i = 0; i < rename_src_nr; i++) {
1230 char *old_dir;
1231 struct diff_filespec *one = rename_src[i].p->one;
1232
1233 /*
1234 * sources that are part of a rename will have already been
1235 * removed by a prior call to remove_unneeded_paths_from_src()
1236 */
1237 assert(!one->rename_used);
1238
1239 old_dir = get_dirname(one->path);
1240 while (*old_dir != '\0' &&
1241 NOT_RELEVANT != strintmap_get(dirs_removed, old_dir)) {
1242 char *freeme = old_dir;
1243
1244 increment_count(info, old_dir, UNKNOWN_DIR);
1245 old_dir = get_dirname(old_dir);
1246
1247 /* Free resources we don't need anymore */
1248 free(freeme);
1249 }
1250 /*
1251 * old_dir and new_dir free'd in increment_count, but
1252 * get_dirname() gives us a new pointer we need to free for
1253 * old_dir. Also, if the loop runs 0 times we need old_dir
1254 * to be freed.
1255 */
1256 free(old_dir);
1257 }
Elijah Newren0491d392021-03-13 22:22:05 +00001258
1259 /*
1260 * For any directory which we need a potential rename detected for
1261 * (i.e. those marked as RELEVANT_FOR_SELF in dirs_removed), check
1262 * whether we have enough renames to satisfy the "majority rules"
1263 * requirement such that detecting any more renames of files under
1264 * it won't change the result. For any such directory, mark that
1265 * we no longer need to detect a rename for it. However, since we
1266 * might need to still detect renames for an ancestor of that
1267 * directory, use RELEVANT_FOR_ANCESTOR.
1268 */
1269 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
1270 /* entry->key is source_dir */
1271 struct strintmap *counts = entry->value;
1272
1273 if (strintmap_get(dirs_removed, entry->key) ==
1274 RELEVANT_FOR_SELF &&
1275 dir_rename_already_determinable(counts)) {
1276 strintmap_set(dirs_removed, entry->key,
1277 RELEVANT_FOR_ANCESTOR);
1278 }
1279 }
Elijah Newren9bd34212021-03-13 22:22:08 +00001280
1281 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
1282 struct diff_filespec *one = rename_src[i].p->one;
1283 int val;
1284
1285 val = strintmap_get(relevant_sources, one->path);
1286
1287 /*
1288 * sources that were not found in relevant_sources should
1289 * have already been removed by a prior call to
1290 * remove_unneeded_paths_from_src()
1291 */
1292 assert(val != -1);
1293
1294 if (val == RELEVANT_LOCATION) {
1295 int removable = 1;
1296 char *dir = get_dirname(one->path);
1297 while (1) {
1298 char *freeme = dir;
1299 int res = strintmap_get(dirs_removed, dir);
1300
1301 /* Quit if not found or irrelevant */
1302 if (res == NOT_RELEVANT)
1303 break;
1304 /* If RELEVANT_FOR_SELF, can't remove */
1305 if (res == RELEVANT_FOR_SELF) {
1306 removable = 0;
1307 break;
1308 }
1309 /* Else continue searching upwards */
1310 assert(res == RELEVANT_FOR_ANCESTOR);
1311 dir = get_dirname(dir);
1312 free(freeme);
1313 }
1314 free(dir);
1315 if (removable) {
1316 strintmap_set(relevant_sources, one->path,
1317 RELEVANT_NO_MORE);
1318 continue;
1319 }
1320 }
1321
1322 if (new_num_src < i)
1323 memcpy(&rename_src[new_num_src], &rename_src[i],
1324 sizeof(struct diff_rename_src));
1325 new_num_src++;
1326 }
1327
1328 rename_src_nr = new_num_src;
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001329}
1330
Elijah Newrena8791ef2021-07-30 11:47:41 +00001331static void free_filespec_data(struct diff_filespec *spec)
1332{
1333 if (!--spec->count)
1334 diff_free_filespec_data(spec);
1335}
1336
1337MAYBE_UNUSED
1338static void pool_free_filespec(struct mem_pool *pool,
1339 struct diff_filespec *spec)
1340{
1341 if (!pool) {
1342 free_filespec(spec);
1343 return;
1344 }
1345
1346 /*
1347 * Similar to free_filespec(), but only frees the data. The spec
1348 * itself was allocated in the pool and should not be individually
1349 * freed.
1350 */
1351 free_filespec_data(spec);
1352}
1353
1354MAYBE_UNUSED
1355void pool_diff_free_filepair(struct mem_pool *pool,
1356 struct diff_filepair *p)
1357{
1358 if (!pool) {
1359 diff_free_filepair(p);
1360 return;
1361 }
1362
1363 /*
1364 * Similar to diff_free_filepair() but only frees the data from the
1365 * filespecs; not the filespecs or the filepair which were
1366 * allocated from the pool.
1367 */
1368 free_filespec_data(p->one);
1369 free_filespec_data(p->two);
1370}
1371
Elijah Newren0c4fd732021-02-27 00:30:42 +00001372void diffcore_rename_extended(struct diff_options *options,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001373 struct strintmap *relevant_sources,
1374 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +00001375 struct strmap *dir_rename_count,
1376 struct strmap *cached_pairs)
Junio C Hamano427dcb42005-05-21 02:39:09 -07001377{
Junio C Hamano8082d8d2005-09-21 00:18:27 -07001378 int detect_rename = options->detect_rename;
1379 int minimum_score = options->rename_score;
Junio C Hamano38c6f782005-05-21 19:40:36 -07001380 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -07001381 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001382 struct diff_score *mx;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001383 int i, j, rename_count, skip_unmodified = 0;
Elijah Newren26a66a62020-12-11 09:08:40 +00001384 int num_destinations, dst_cnt;
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001385 int num_sources, want_copies;
Jeff King3ac942d2011-02-20 04:51:16 -05001386 struct progress *progress = NULL;
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001387 struct mem_pool local_pool;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001388 struct dir_rename_info info;
Elijah Newrend331dd32021-06-22 08:04:39 +00001389 struct diff_populate_filespec_options dpf_options = {
1390 .check_binary = 0,
1391 .missing_object_cb = NULL,
1392 .missing_object_data = NULL
1393 };
Elijah Newren1aedd032021-06-22 08:04:40 +00001394 struct inexact_prefetch_options prefetch_options = {
Elijah Newrend331dd32021-06-22 08:04:39 +00001395 .repo = options->repo
1396 };
Junio C Hamano427dcb42005-05-21 02:39:09 -07001397
Elijah Newren557ac032021-01-23 22:01:12 -08001398 trace2_region_enter("diff", "setup", options->repo);
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001399 info.setup = 0;
Elijah Newren0c4fd732021-02-27 00:30:42 +00001400 assert(!dir_rename_count || strmap_empty(dir_rename_count));
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001401 want_copies = (detect_rename == DIFF_DETECT_COPY);
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001402 if (dirs_removed && (break_idx || want_copies))
1403 BUG("dirs_removed incompatible with break/copy detection");
Elijah Newren97998892021-03-11 00:38:24 +00001404 if (break_idx && relevant_sources)
1405 BUG("break detection incompatible with source specification");
Junio C Hamano26dee0a2005-05-21 23:33:32 -07001406 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001407 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001408
Junio C Hamano427dcb42005-05-21 02:39:09 -07001409 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -07001410 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001411 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -07001412 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -07001413 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001414 else if (options->single_follow &&
1415 strcmp(options->single_follow, p->two->path))
1416 continue; /* not interested */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001417 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -07001418 is_empty_blob_oid(&p->two->oid))
Jeff King90d43b02012-03-22 18:52:13 -04001419 continue;
Elijah Newren9db2ac52020-12-11 09:08:47 +00001420 else if (add_rename_dst(p) < 0) {
Jeff King4d6be032015-02-26 20:42:27 -05001421 warning("skipping rename detection, detected"
1422 " duplicate destination '%s'",
1423 p->two->path);
1424 goto cleanup;
1425 }
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001426 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001427 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -07001428 is_empty_blob_oid(&p->one->oid))
Jeff King90d43b02012-03-22 18:52:13 -04001429 continue;
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -04001430 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -07001431 /*
1432 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -07001433 * they did not really want to get broken,
1434 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -07001435 * So we increment the "rename_used" score
1436 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -07001437 */
Linus Torvalds64479712007-10-25 11:20:56 -07001438 if (p->broken_pair && !p->score)
1439 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001440 register_rename_src(p);
Junio C Hamano22101002005-06-11 20:55:20 -07001441 }
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001442 else if (want_copies) {
Linus Torvalds64479712007-10-25 11:20:56 -07001443 /*
1444 * Increment the "rename_used" score by
1445 * one, to indicate ourselves as a user.
1446 */
1447 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001448 register_rename_src(p);
Linus Torvalds64479712007-10-25 11:20:56 -07001449 }
Junio C Hamano427dcb42005-05-21 02:39:09 -07001450 }
Elijah Newren557ac032021-01-23 22:01:12 -08001451 trace2_region_leave("diff", "setup", options->repo);
Linus Torvalds0024a542007-09-14 10:39:48 -07001452 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -07001453 goto cleanup; /* nothing to do */
1454
Elijah Newren557ac032021-01-23 22:01:12 -08001455 trace2_region_enter("diff", "exact renames", options->repo);
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001456 mem_pool_init(&local_pool, 32*1024);
Linus Torvalds0024a542007-09-14 10:39:48 -07001457 /*
Linus Torvalds17559a62007-10-25 11:24:47 -07001458 * We really want to cull the candidates list early
1459 * with cheap tests in order to avoid doing deltas.
1460 */
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001461 rename_count = find_exact_renames(options, &local_pool);
1462 /*
1463 * Discard local_pool immediately instead of at "cleanup:" in order
1464 * to reduce maximum memory usage; inexact rename detection uses up
1465 * a fair amount of memory, and mem_pools can too.
1466 */
1467 mem_pool_discard(&local_pool, 0);
Elijah Newren557ac032021-01-23 22:01:12 -08001468 trace2_region_leave("diff", "exact renames", options->repo);
Linus Torvalds17559a62007-10-25 11:24:47 -07001469
Linus Torvalds42899ac2007-10-26 16:56:34 -07001470 /* Did we only want exact renames? */
1471 if (minimum_score == MAX_SCORE)
1472 goto cleanup;
1473
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001474 num_sources = rename_src_nr;
Linus Torvalds42899ac2007-10-26 16:56:34 -07001475
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001476 if (want_copies || break_idx) {
1477 /*
1478 * Cull sources:
1479 * - remove ones corresponding to exact renames
Elijah Newren97998892021-03-11 00:38:24 +00001480 * - remove ones not found in relevant_sources
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001481 */
1482 trace2_region_enter("diff", "cull after exact", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001483 remove_unneeded_paths_from_src(want_copies, relevant_sources);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001484 trace2_region_leave("diff", "cull after exact", options->repo);
1485 } else {
1486 /* Determine minimum score to match basenames */
1487 double factor = 0.5;
1488 char *basename_factor = getenv("GIT_BASENAME_FACTOR");
1489 int min_basename_score;
1490
1491 if (basename_factor)
1492 factor = strtol(basename_factor, NULL, 10)/100.0;
1493 assert(factor >= 0.0 && factor <= 1.0);
1494 min_basename_score = minimum_score +
1495 (int)(factor * (MAX_SCORE - minimum_score));
1496
1497 /*
1498 * Cull sources:
1499 * - remove ones involved in renames (found via exact match)
1500 */
1501 trace2_region_enter("diff", "cull after exact", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001502 remove_unneeded_paths_from_src(want_copies, NULL);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001503 trace2_region_leave("diff", "cull after exact", options->repo);
1504
Elijah Newrenae8cf742021-02-27 00:30:41 +00001505 /* Preparation for basename-driven matching. */
1506 trace2_region_enter("diff", "dir rename setup", options->repo);
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001507 initialize_dir_rename_info(&info, relevant_sources,
Elijah Newren25e65b62021-05-20 06:09:41 +00001508 dirs_removed, dir_rename_count,
1509 cached_pairs);
Elijah Newrenae8cf742021-02-27 00:30:41 +00001510 trace2_region_leave("diff", "dir rename setup", options->repo);
1511
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001512 /* Utilize file basenames to quickly find renames. */
1513 trace2_region_enter("diff", "basename matches", options->repo);
1514 rename_count += find_basename_matches(options,
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001515 min_basename_score,
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001516 &info,
1517 relevant_sources,
1518 dirs_removed);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001519 trace2_region_leave("diff", "basename matches", options->repo);
1520
1521 /*
1522 * Cull sources, again:
1523 * - remove ones involved in renames (found via basenames)
Elijah Newren97998892021-03-11 00:38:24 +00001524 * - remove ones not found in relevant_sources
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001525 * and
1526 * - remove ones in relevant_sources which are needed only
1527 * for directory renames IF no ancestory directory
1528 * actually needs to know any more individual path
1529 * renames under them
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001530 */
1531 trace2_region_enter("diff", "cull basename", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001532 remove_unneeded_paths_from_src(want_copies, relevant_sources);
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001533 handle_early_known_dir_renames(&info, relevant_sources,
1534 dirs_removed);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001535 trace2_region_leave("diff", "cull basename", options->repo);
1536 }
1537
1538 /* Calculate how many rename destinations are left */
1539 num_destinations = (rename_dst_nr - rename_count);
1540 num_sources = rename_src_nr; /* rename_src_nr reflects lower number */
1541
Linus Torvalds42899ac2007-10-26 16:56:34 -07001542 /* All done? */
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001543 if (!num_destinations || !num_sources)
Linus Torvalds42899ac2007-10-26 16:56:34 -07001544 goto cleanup;
1545
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001546 switch (too_many_rename_candidates(num_destinations, num_sources,
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001547 options)) {
Junio C Hamanof31027c2011-01-06 13:50:06 -08001548 case 1:
Linus Torvalds0024a542007-09-14 10:39:48 -07001549 goto cleanup;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001550 case 2:
1551 options->degraded_cc_to_c = 1;
1552 skip_unmodified = 1;
1553 break;
1554 default:
1555 break;
1556 }
Linus Torvalds0024a542007-09-14 10:39:48 -07001557
Elijah Newren557ac032021-01-23 22:01:12 -08001558 trace2_region_enter("diff", "inexact renames", options->repo);
Jeff King3ac942d2011-02-20 04:51:16 -05001559 if (options->show_rename_progress) {
Junio C Hamano8aade102017-08-19 10:39:41 -07001560 progress = start_delayed_progress(
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07001561 _("Performing inexact rename detection"),
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001562 (uint64_t)num_destinations * (uint64_t)num_sources);
Jeff King3ac942d2011-02-20 04:51:16 -05001563 }
1564
Elijah Newrend331dd32021-06-22 08:04:39 +00001565 /* Finish setting up dpf_options */
1566 prefetch_options.skip_unmodified = skip_unmodified;
1567 if (options->repo == the_repository && has_promisor_remote()) {
Elijah Newren1aedd032021-06-22 08:04:40 +00001568 dpf_options.missing_object_cb = inexact_prefetch;
Elijah Newrend331dd32021-06-22 08:04:39 +00001569 dpf_options.missing_object_data = &prefetch_options;
1570 }
1571
René Scharfeca56dad2021-03-13 17:17:22 +01001572 CALLOC_ARRAY(mx, st_mult(NUM_CANDIDATE_PER_DST, num_destinations));
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001573 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Elijah Newren9db2ac52020-12-11 09:08:47 +00001574 struct diff_filespec *two = rename_dst[i].p->two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001575 struct diff_score *m;
1576
Elijah Newren9db2ac52020-12-11 09:08:47 +00001577 if (rename_dst[i].is_rename)
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001578 continue; /* exact or basename match already handled */
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001579
1580 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
1581 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
1582 m[j].dst = -1;
1583
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001584 for (j = 0; j < rename_src_nr; j++) {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001585 struct diff_filespec *one = rename_src[j].p->one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001586 struct diff_score this_src;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001587
Elijah Newren829514c2021-02-14 07:35:01 +00001588 assert(!one->rename_used || want_copies || break_idx);
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001589
Junio C Hamanof31027c2011-01-06 13:50:06 -08001590 if (skip_unmodified &&
1591 diff_unmodified_pair(rename_src[j].p))
1592 continue;
1593
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02001594 this_src.score = estimate_similarity(options->repo,
1595 one, two,
Jonathan Tan95acf112020-04-07 15:11:43 -07001596 minimum_score,
Elijah Newrend331dd32021-06-22 08:04:39 +00001597 &dpf_options);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001598 this_src.name_score = basename_same(one, two);
1599 this_src.dst = i;
1600 this_src.src = j;
1601 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -08001602 /*
1603 * Once we run estimate_similarity,
1604 * We do not need the text anymore.
1605 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -07001606 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -08001607 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001608 }
1609 dst_cnt++;
Elijah Newren81c4bf02020-12-11 09:08:43 +00001610 display_progress(progress,
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001611 (uint64_t)dst_cnt * (uint64_t)num_sources);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001612 }
Jeff King3ac942d2011-02-20 04:51:16 -05001613 stop_progress(&progress);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001614
Junio C Hamano427dcb42005-05-21 02:39:09 -07001615 /* cost matrix sorted by most to least similar pair */
Johannes Schindelin2049b8d2019-09-30 10:21:55 -07001616 STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001617
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001618 rename_count += find_renames(mx, dst_cnt, minimum_score, 0,
1619 &info, dirs_removed);
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001620 if (want_copies)
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001621 rename_count += find_renames(mx, dst_cnt, minimum_score, 1,
1622 &info, dirs_removed);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001623 free(mx);
Elijah Newren557ac032021-01-23 22:01:12 -08001624 trace2_region_leave("diff", "inexact renames", options->repo);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001625
Junio C Hamano15d061b2005-05-27 15:55:55 -07001626 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -07001627 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -07001628 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -07001629 */
Elijah Newren557ac032021-01-23 22:01:12 -08001630 trace2_region_enter("diff", "write back to queue", options->repo);
Bo Yang9ca5df92010-05-06 21:52:27 -07001631 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001632 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001633 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001634 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001635
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -04001636 if (DIFF_PAIR_UNMERGED(p)) {
1637 diff_q(&outq, p);
1638 }
1639 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Elijah Newren9db2ac52020-12-11 09:08:47 +00001640 /* Creation */
1641 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001642 }
Junio C Hamano2cd68882005-05-30 00:08:07 -07001643 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
1644 /*
1645 * Deletion
1646 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001647 * We would output this delete record if:
1648 *
1649 * (1) this is a broken delete and the counterpart
1650 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -07001651 * (2) this is not a broken delete, and rename_dst
1652 * does not have a rename/copy to move p->one->path
1653 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001654 *
1655 * Otherwise, the counterpart broken create
1656 * has been turned into a rename-edit; or
1657 * delete did not have a matching create to
1658 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -07001659 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001660 if (DIFF_PAIR_BROKEN(p)) {
1661 /* broken delete */
Elijah Newren9db2ac52020-12-11 09:08:47 +00001662 struct diff_rename_dst *dst = locate_rename_dst(p);
1663 if (!dst)
1664 BUG("tracking failed somehow; failed to find associated dst for broken pair");
1665 if (dst->is_rename)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001666 /* counterpart is now rename/copy */
1667 pair_to_free = p;
1668 }
1669 else {
Linus Torvalds64479712007-10-25 11:20:56 -07001670 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001671 /* this path remains */
1672 pair_to_free = p;
1673 }
Junio C Hamano2cd68882005-05-30 00:08:07 -07001674
Elijah Newren9db2ac52020-12-11 09:08:47 +00001675 if (!pair_to_free)
Junio C Hamano2cd68882005-05-30 00:08:07 -07001676 diff_q(&outq, p);
1677 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001678 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -07001679 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001680 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -07001681 else
Elijah Newren356da0f2021-06-08 16:11:41 +00001682 /* no need to keep unmodified pairs */
Junio C Hamano15d061b2005-05-27 15:55:55 -07001683 pair_to_free = p;
1684
Junio C Hamano226406f2005-05-27 15:50:30 -07001685 if (pair_to_free)
1686 diff_free_filepair(pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001687 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001688 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001689
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001690 free(q->queue);
1691 *q = outq;
1692 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001693
Linus Torvalds9fb88412007-10-25 11:19:10 -07001694 for (i = 0; i < rename_dst_nr; i++)
Elijah Newren9db2ac52020-12-11 09:08:47 +00001695 if (rename_dst[i].filespec_to_free)
1696 free_filespec(rename_dst[i].filespec_to_free);
Junio C Hamano5098baf2005-09-15 16:13:43 -07001697
Elijah Newrenb1473012021-02-27 00:30:45 +00001698 cleanup_dir_rename_info(&info, dirs_removed, dir_rename_count != NULL);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001699 FREE_AND_NULL(rename_dst);
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001700 rename_dst_nr = rename_dst_alloc = 0;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001701 FREE_AND_NULL(rename_src);
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001702 rename_src_nr = rename_src_alloc = 0;
Elijah Newren9db2ac52020-12-11 09:08:47 +00001703 if (break_idx) {
1704 strintmap_clear(break_idx);
1705 FREE_AND_NULL(break_idx);
1706 }
Elijah Newren557ac032021-01-23 22:01:12 -08001707 trace2_region_leave("diff", "write back to queue", options->repo);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001708 return;
1709}
Elijah Newren0c4fd732021-02-27 00:30:42 +00001710
1711void diffcore_rename(struct diff_options *options)
1712{
Elijah Newren25e65b62021-05-20 06:09:41 +00001713 diffcore_rename_extended(options, NULL, NULL, NULL, NULL);
Elijah Newren0c4fd732021-02-27 00:30:42 +00001714}