blob: 5a6e2bcac7147e487624c1bf53e1572a54b0d93d [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 */
Elijah Newrenfc7bd512023-02-24 00:09:34 +00005#include "git-compat-util.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -07006#include "diff.h"
7#include "diffcore.h"
Elijah Newrena034e912023-05-16 06:34:06 +00008#include "object-store-ll.h"
Karsten Bleesf79d9c52013-11-14 20:20:26 +01009#include "hashmap.h"
Elijah Newrenfc7bd512023-02-24 00:09:34 +000010#include "mem-pool.h"
11#include "oid-array.h"
Jeff King3ac942d2011-02-20 04:51:16 -050012#include "progress.h"
Jonathan Tan95acf112020-04-07 15:11:43 -070013#include "promisor-remote.h"
Elijah Newrenfc7bd512023-02-24 00:09:34 +000014#include "string-list.h"
Elijah Newren9db2ac52020-12-11 09:08:47 +000015#include "strmap.h"
Elijah Newrenfc7bd512023-02-24 00:09:34 +000016#include "trace2.h"
Junio C Hamano427dcb42005-05-21 02:39:09 -070017
Junio C Hamano25d5ea42005-05-24 01:10:48 -070018/* Table of rename/copy destinations */
Junio C Hamano427dcb42005-05-21 02:39:09 -070019
Junio C Hamano25d5ea42005-05-24 01:10:48 -070020static struct diff_rename_dst {
Elijah Newren9db2ac52020-12-11 09:08:47 +000021 struct diff_filepair *p;
22 struct diff_filespec *filespec_to_free;
23 int is_rename; /* false -> just a create; true -> rename or copy */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070024} *rename_dst;
25static int rename_dst_nr, rename_dst_alloc;
Elijah Newren9db2ac52020-12-11 09:08:47 +000026/* Mapping from break source pathname to break destination index */
27static struct strintmap *break_idx = NULL;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070028
Elijah Newren9db2ac52020-12-11 09:08:47 +000029static struct diff_rename_dst *locate_rename_dst(struct diff_filepair *p)
Junio C Hamano427dcb42005-05-21 02:39:09 -070030{
Elijah Newren9db2ac52020-12-11 09:08:47 +000031 /* Lookup by p->ONE->path */
32 int idx = break_idx ? strintmap_get(break_idx, p->one->path) : -1;
33 return (idx == -1) ? NULL : &rename_dst[idx];
Jeff Kingf98c2f72015-02-26 20:39:48 -050034}
35
36/*
37 * Returns 0 on success, -1 if we found a duplicate.
38 */
Elijah Newren9db2ac52020-12-11 09:08:47 +000039static int add_rename_dst(struct diff_filepair *p)
Jeff Kingf98c2f72015-02-26 20:39:48 -050040{
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040041 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
Elijah Newren9db2ac52020-12-11 09:08:47 +000042 rename_dst[rename_dst_nr].p = p;
43 rename_dst[rename_dst_nr].filespec_to_free = NULL;
44 rename_dst[rename_dst_nr].is_rename = 0;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070045 rename_dst_nr++;
Jeff Kingf98c2f72015-02-26 20:39:48 -050046 return 0;
Junio C Hamano427dcb42005-05-21 02:39:09 -070047}
48
Junio C Hamano15d061b2005-05-27 15:55:55 -070049/* Table of rename/copy src files */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070050static struct diff_rename_src {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -080051 struct diff_filepair *p;
Junio C Hamanofc580712006-04-08 20:17:46 -070052 unsigned short score; /* to remember the break score */
Junio C Hamano25d5ea42005-05-24 01:10:48 -070053} *rename_src;
54static int rename_src_nr, rename_src_alloc;
Junio C Hamano427dcb42005-05-21 02:39:09 -070055
Elijah Newrenb970b4e2020-12-11 09:08:46 +000056static void register_rename_src(struct diff_filepair *p)
Junio C Hamano25d5ea42005-05-24 01:10:48 -070057{
Elijah Newren9db2ac52020-12-11 09:08:47 +000058 if (p->broken_pair) {
59 if (!break_idx) {
60 break_idx = xmalloc(sizeof(*break_idx));
Elijah Newren61bf4492021-06-08 16:11:40 +000061 strintmap_init_with_options(break_idx, -1, NULL, 0);
Elijah Newren9db2ac52020-12-11 09:08:47 +000062 }
63 strintmap_set(break_idx, p->one->path, rename_dst_nr);
64 }
65
Dmitry S. Dolzhenko337ce242014-03-04 02:31:54 +040066 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
Elijah Newrenb970b4e2020-12-11 09:08:46 +000067 rename_src[rename_src_nr].p = p;
68 rename_src[rename_src_nr].score = p->score;
Junio C Hamano25d5ea42005-05-24 01:10:48 -070069 rename_src_nr++;
Junio C Hamano427dcb42005-05-21 02:39:09 -070070}
71
Johannes Schindelin0ce39642007-06-21 12:52:11 +010072static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
73{
74 int src_len = strlen(src->path), dst_len = strlen(dst->path);
75 while (src_len && dst_len) {
76 char c1 = src->path[--src_len];
77 char c2 = dst->path[--dst_len];
78 if (c1 != c2)
79 return 0;
80 if (c1 == '/')
81 return 1;
82 }
83 return (!src_len || src->path[src_len - 1] == '/') &&
84 (!dst_len || dst->path[dst_len - 1] == '/');
85}
86
Junio C Hamano427dcb42005-05-21 02:39:09 -070087struct diff_score {
Junio C Hamano25d5ea42005-05-24 01:10:48 -070088 int src; /* index in rename_src */
89 int dst; /* index in rename_dst */
Junio C Hamano6d24ad92008-01-29 20:54:56 -080090 unsigned short score;
91 short name_score;
Junio C Hamano427dcb42005-05-21 02:39:09 -070092};
93
Elijah Newren1aedd032021-06-22 08:04:40 +000094struct inexact_prefetch_options {
Jonathan Tan95acf112020-04-07 15:11:43 -070095 struct repository *repo;
96 int skip_unmodified;
97};
Elijah Newren1aedd032021-06-22 08:04:40 +000098static void inexact_prefetch(void *prefetch_options)
Jonathan Tan95acf112020-04-07 15:11:43 -070099{
Elijah Newren1aedd032021-06-22 08:04:40 +0000100 struct inexact_prefetch_options *options = prefetch_options;
Jonathan Tan95acf112020-04-07 15:11:43 -0700101 int i;
102 struct oid_array to_fetch = OID_ARRAY_INIT;
103
104 for (i = 0; i < rename_dst_nr; i++) {
Elijah Newren9db2ac52020-12-11 09:08:47 +0000105 if (rename_dst[i].p->renamed_pair)
Jonathan Tan95acf112020-04-07 15:11:43 -0700106 /*
107 * The loop in diffcore_rename() will not need these
108 * blobs, so skip prefetching.
109 */
110 continue; /* already found exact match */
111 diff_add_if_missing(options->repo, &to_fetch,
Elijah Newren9db2ac52020-12-11 09:08:47 +0000112 rename_dst[i].p->two);
Jonathan Tan95acf112020-04-07 15:11:43 -0700113 }
114 for (i = 0; i < rename_src_nr; i++) {
115 if (options->skip_unmodified &&
116 diff_unmodified_pair(rename_src[i].p))
117 /*
118 * The loop in diffcore_rename() will not need these
119 * blobs, so skip prefetching.
120 */
121 continue;
122 diff_add_if_missing(options->repo, &to_fetch,
123 rename_src[i].p->one);
124 }
125 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
126 oid_array_clear(&to_fetch);
127}
128
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200129static int estimate_similarity(struct repository *r,
130 struct diff_filespec *src,
Junio C Hamano427dcb42005-05-21 02:39:09 -0700131 struct diff_filespec *dst,
Jonathan Tan95acf112020-04-07 15:11:43 -0700132 int minimum_score,
Elijah Newrend331dd32021-06-22 08:04:39 +0000133 struct diff_populate_filespec_options *dpf_opt)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700134{
135 /* src points at a file that existed in the original tree (or
136 * optionally a file in the destination tree) and dst points
137 * at a newly created file. They may be quite similar, in which
138 * case we want to say src is renamed to dst or src is copied into
139 * dst, and then some edit has been applied to dst.
140 *
141 * Compare them and return how similar they are, representing
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700142 * the score as an integer between 0 and MAX_SCORE.
143 *
144 * When there is an exact match, it is considered a better
145 * match than anything else; the destination does not even
146 * call into this function in that case.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700147 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800148 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700149 int score;
150
Junio C Hamano60896c72005-05-22 21:24:49 -0700151 /* We deal only with regular files. Symlink renames are handled
152 * only when they are exact matches --- in other words, no edits
153 * after renaming.
154 */
155 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
156 return 0;
157
Linus Torvalds81ac0512007-10-26 16:51:28 -0700158 /*
159 * Need to check that source and destination sizes are
160 * filled in before comparing them.
161 *
162 * If we already have "cnt_data" filled in, we know it's
163 * all good (avoid checking the size for zero, as that
164 * is a possible size - we really should have a flag to
165 * say whether the size is valid or not!)
166 */
Elijah Newrend331dd32021-06-22 08:04:39 +0000167 dpf_opt->check_size_only = 1;
168
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700169 if (!src->cnt_data &&
Elijah Newrend331dd32021-06-22 08:04:39 +0000170 diff_populate_filespec(r, src, dpf_opt))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700171 return 0;
Nguyễn Thái Ngọc Duy8e5dd3d2014-08-16 10:08:04 +0700172 if (!dst->cnt_data &&
Elijah Newrend331dd32021-06-22 08:04:39 +0000173 diff_populate_filespec(r, dst, dpf_opt))
Linus Torvalds81ac0512007-10-26 16:51:28 -0700174 return 0;
175
Linus Torvalds90bd9322006-03-12 22:26:34 -0800176 max_size = ((src->size > dst->size) ? src->size : dst->size);
Junio C Hamano58b103f2005-05-21 15:55:18 -0700177 base_size = ((src->size < dst->size) ? src->size : dst->size);
Linus Torvalds90bd9322006-03-12 22:26:34 -0800178 delta_size = max_size - base_size;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700179
Junio C Hamano58b103f2005-05-21 15:55:18 -0700180 /* We would not consider edits that change the file size so
181 * drastically. delta_size must be smaller than
Junio C Hamanocd1870e2005-05-22 01:31:28 -0700182 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700183 *
Junio C Hamano58b103f2005-05-21 15:55:18 -0700184 * Note that base_size == 0 case is handled here already
185 * and the final score computation below would not have a
186 * divide-by-zero issue.
Junio C Hamano427dcb42005-05-21 02:39:09 -0700187 */
Linus Torvalds3a4d6762011-02-18 20:12:06 -0800188 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700189 return 0;
190
Elijah Newrend331dd32021-06-22 08:04:39 +0000191 dpf_opt->check_size_only = 0;
Jonathan Tan95acf112020-04-07 15:11:43 -0700192
Elijah Newrend331dd32021-06-22 08:04:39 +0000193 if (!src->cnt_data && diff_populate_filespec(r, src, dpf_opt))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100194 return 0;
Elijah Newrend331dd32021-06-22 08:04:39 +0000195 if (!dst->cnt_data && diff_populate_filespec(r, dst, dpf_opt))
Björn Steinbrink885c7162009-01-20 16:59:57 +0100196 return 0;
197
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200198 if (diffcore_count_changes(r, src, dst,
Junio C Hamanoc06c7962006-03-12 03:22:10 -0800199 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -0800200 &src_copied, &literal_added))
Junio C Hamano75c660a2005-06-28 16:58:27 -0700201 return 0;
Junio C Hamano85976972005-05-24 12:09:32 -0700202
Junio C Hamano17063062006-03-02 22:11:25 -0800203 /* How similar are they?
204 * what percentage of material in dst are from source?
Junio C Hamano427dcb42005-05-21 02:39:09 -0700205 */
Linus Torvalds90bd9322006-03-12 22:26:34 -0800206 if (!dst->size)
Junio C Hamano17063062006-03-02 22:11:25 -0800207 score = 0; /* should not happen */
René Scharfecfc0aef2007-06-25 00:23:28 +0200208 else
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500209 score = (int)(src_copied * MAX_SCORE / max_size);
Junio C Hamano427dcb42005-05-21 02:39:09 -0700210 return score;
211}
212
Junio C Hamano5098baf2005-09-15 16:13:43 -0700213static void record_rename_pair(int dst_index, int src_index, int score)
Junio C Hamano427dcb42005-05-21 02:39:09 -0700214{
Elijah Newren9db2ac52020-12-11 09:08:47 +0000215 struct diff_filepair *src = rename_src[src_index].p;
216 struct diff_filepair *dst = rename_dst[dst_index].p;
Junio C Hamanof7c15122005-05-22 21:26:09 -0700217
Elijah Newren9db2ac52020-12-11 09:08:47 +0000218 if (dst->renamed_pair)
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700219 die("internal error: dst already matched.");
220
Elijah Newren9db2ac52020-12-11 09:08:47 +0000221 src->one->rename_used++;
222 src->one->count++;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700223
Elijah Newren9db2ac52020-12-11 09:08:47 +0000224 rename_dst[dst_index].filespec_to_free = dst->one;
225 rename_dst[dst_index].is_rename = 1;
Junio C Hamano25d5ea42005-05-24 01:10:48 -0700226
Elijah Newren9db2ac52020-12-11 09:08:47 +0000227 dst->one = src->one;
228 dst->renamed_pair = 1;
229 if (!strcmp(dst->one->path, dst->two->path))
230 dst->score = rename_src[src_index].score;
Junio C Hamanofc580712006-04-08 20:17:46 -0700231 else
Elijah Newren9db2ac52020-12-11 09:08:47 +0000232 dst->score = score;
Junio C Hamano427dcb42005-05-21 02:39:09 -0700233}
234
235/*
236 * We sort the rename similarity matrix with the score, in descending
Junio C Hamano15d061b2005-05-27 15:55:55 -0700237 * order (the most similar first).
Junio C Hamano427dcb42005-05-21 02:39:09 -0700238 */
239static int score_compare(const void *a_, const void *b_)
240{
241 const struct diff_score *a = a_, *b = b_;
René Scharfecfc0aef2007-06-25 00:23:28 +0200242
Junio C Hamano6d24ad92008-01-29 20:54:56 -0800243 /* sink the unused ones to the bottom */
244 if (a->dst < 0)
245 return (0 <= b->dst);
246 else if (b->dst < 0)
247 return -1;
248
René Scharfecfc0aef2007-06-25 00:23:28 +0200249 if (a->score == b->score)
250 return b->name_score - a->name_score;
251
Junio C Hamano427dcb42005-05-21 02:39:09 -0700252 return b->score - a->score;
253}
254
Linus Torvalds9027f532007-10-25 11:23:26 -0700255struct file_similarity {
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100256 struct hashmap_entry entry;
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100257 int index;
Linus Torvalds9027f532007-10-25 11:23:26 -0700258 struct diff_filespec *filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700259};
260
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200261static unsigned int hash_filespec(struct repository *r,
262 struct diff_filespec *filespec)
Karsten Blees48f64072013-11-14 20:19:04 +0100263{
brian m. carlson41c95602016-06-24 23:09:24 +0000264 if (!filespec->oid_valid) {
Jonathan Tan1c37e862020-04-07 15:11:41 -0700265 if (diff_populate_filespec(r, filespec, NULL))
Karsten Blees48f64072013-11-14 20:19:04 +0100266 return 0;
Matheus Tavares2dcde202020-01-30 17:32:22 -0300267 hash_object_file(r->hash_algo, filespec->data, filespec->size,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100268 OBJ_BLOB, &filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100269 }
Jeff Kingd40abc82019-06-20 03:41:49 -0400270 return oidhash(&filespec->oid);
Karsten Blees48f64072013-11-14 20:19:04 +0100271}
272
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100273static int find_identical_files(struct hashmap *srcs,
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100274 int dst_index,
Linus Torvalds11f944d2011-02-18 19:55:19 -0800275 struct diff_options *options)
Linus Torvalds9027f532007-10-25 11:23:26 -0700276{
277 int renames = 0;
Elijah Newren9db2ac52020-12-11 09:08:47 +0000278 struct diff_filespec *target = rename_dst[dst_index].p->two;
Karsten Bleesab73a9d2014-07-03 00:22:11 +0200279 struct file_similarity *p, *best = NULL;
Karsten Blees48f64072013-11-14 20:19:04 +0100280 int i = 100, best_score = -1;
Eric Wongf0e63c42019-10-06 23:30:35 +0000281 unsigned int hash = hash_filespec(options->repo, target);
Linus Torvalds9027f532007-10-25 11:23:26 -0700282
Karsten Blees48f64072013-11-14 20:19:04 +0100283 /*
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100284 * Find the best source match for specified destination.
Karsten Blees48f64072013-11-14 20:19:04 +0100285 */
Eric Wongf0e63c42019-10-06 23:30:35 +0000286 p = hashmap_get_entry_from_hash(srcs, hash, NULL,
287 struct file_similarity, entry);
Eric Wong23dee692019-10-06 23:30:41 +0000288 hashmap_for_each_entry_from(srcs, p, entry) {
Karsten Blees48f64072013-11-14 20:19:04 +0100289 int score;
290 struct diff_filespec *source = p->filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700291
Karsten Blees48f64072013-11-14 20:19:04 +0100292 /* False hash collision? */
Jeff King9001dc22018-08-28 17:22:48 -0400293 if (!oideq(&source->oid, &target->oid))
Karsten Blees48f64072013-11-14 20:19:04 +0100294 continue;
295 /* Non-regular files? If so, the modes must match! */
296 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
297 if (source->mode != target->mode)
Linus Torvalds9027f532007-10-25 11:23:26 -0700298 continue;
Karsten Blees48f64072013-11-14 20:19:04 +0100299 }
300 /* Give higher scores to sources that haven't been used already */
301 score = !source->rename_used;
302 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
303 continue;
304 score += basename_same(source, target);
305 if (score > best_score) {
306 best = p;
307 best_score = score;
308 if (score == 2)
Linus Torvalds9027f532007-10-25 11:23:26 -0700309 break;
310 }
Karsten Blees48f64072013-11-14 20:19:04 +0100311
312 /* Too many identical alternatives? Pick one */
313 if (!--i)
314 break;
315 }
316 if (best) {
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100317 record_rename_pair(dst_index, best->index, MAX_SCORE);
Karsten Blees48f64072013-11-14 20:19:04 +0100318 renames++;
319 }
Linus Torvalds9027f532007-10-25 11:23:26 -0700320 return renames;
321}
322
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200323static void insert_file_table(struct repository *r,
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000324 struct mem_pool *pool,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200325 struct hashmap *table, int index,
326 struct diff_filespec *filespec)
Linus Torvalds9027f532007-10-25 11:23:26 -0700327{
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000328 struct file_similarity *entry = mem_pool_alloc(pool, sizeof(*entry));
Linus Torvalds9027f532007-10-25 11:23:26 -0700329
Linus Torvalds9027f532007-10-25 11:23:26 -0700330 entry->index = index;
331 entry->filespec = filespec;
Linus Torvalds9027f532007-10-25 11:23:26 -0700332
Eric Wongd22245a2019-10-06 23:30:27 +0000333 hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
Eric Wongb94e5c12019-10-06 23:30:29 +0000334 hashmap_add(table, &entry->entry);
Linus Torvalds9027f532007-10-25 11:23:26 -0700335}
336
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700337/*
338 * Find exact renames first.
339 *
340 * The first round matches up the up-to-date entries,
341 * and then during the second round we try to match
342 * cache-dirty entries as well.
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700343 */
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000344static int find_exact_renames(struct diff_options *options,
345 struct mem_pool *pool)
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700346{
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100347 int i, renames = 0;
Karsten Bleesf79d9c52013-11-14 20:20:26 +0100348 struct hashmap file_table;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700349
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200350 /* Add all sources to the hash table in reverse order, because
351 * later on they will be retrieved in LIFO order.
352 */
Stefan Beller7663cdc2017-06-30 12:14:05 -0700353 hashmap_init(&file_table, NULL, NULL, rename_src_nr);
SZEDER Gáborca4e3ca2016-03-30 10:35:07 +0200354 for (i = rename_src_nr-1; i >= 0; i--)
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000355 insert_file_table(options->repo, pool,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200356 &file_table, i,
357 rename_src[i].p->one);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700358
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100359 /* Walk the destinations and find best source match */
Linus Torvalds9027f532007-10-25 11:23:26 -0700360 for (i = 0; i < rename_dst_nr; i++)
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100361 renames += find_identical_files(&file_table, i, options);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700362
Elijah Newrenfa0e9362021-07-30 11:47:37 +0000363 /* Free the hash data structure (entries will be freed with the pool) */
364 hashmap_clear(&file_table);
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700365
Karsten Blees7c85f8a2013-11-14 20:19:34 +0100366 return renames;
Linus Torvaldscb1491b2007-10-25 11:17:55 -0700367}
368
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000369struct dir_rename_info {
370 struct strintmap idx_map;
371 struct strmap dir_rename_guess;
372 struct strmap *dir_rename_count;
Elijah Newrena49b55d2021-03-13 22:22:02 +0000373 struct strintmap *relevant_source_dirs;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000374 unsigned setup;
375};
376
377static char *get_dirname(const char *filename)
378{
379 char *slash = strrchr(filename, '/');
380 return slash ? xstrndup(filename, slash - filename) : xstrdup("");
381}
382
Elijah Newren0c4fd732021-02-27 00:30:42 +0000383static void dirname_munge(char *filename)
384{
385 char *slash = strrchr(filename, '/');
386 if (!slash)
387 slash = filename;
388 *slash = '\0';
389}
390
Elijah Newren81afdf72021-02-27 00:30:48 +0000391static const char *get_highest_rename_path(struct strintmap *counts)
392{
393 int highest_count = 0;
394 const char *highest_destination_dir = NULL;
395 struct hashmap_iter iter;
396 struct strmap_entry *entry;
397
398 strintmap_for_each_entry(counts, &iter, entry) {
399 const char *destination_dir = entry->key;
400 intptr_t count = (intptr_t)entry->value;
401 if (count > highest_count) {
402 highest_count = count;
403 highest_destination_dir = destination_dir;
404 }
405 }
406 return highest_destination_dir;
407}
408
Elijah Newren0491d392021-03-13 22:22:05 +0000409static char *UNKNOWN_DIR = "/"; /* placeholder -- short, illegal directory */
410
411static int dir_rename_already_determinable(struct strintmap *counts)
412{
413 struct hashmap_iter iter;
414 struct strmap_entry *entry;
415 int first = 0, second = 0, unknown = 0;
416 strintmap_for_each_entry(counts, &iter, entry) {
417 const char *destination_dir = entry->key;
418 intptr_t count = (intptr_t)entry->value;
419 if (!strcmp(destination_dir, UNKNOWN_DIR)) {
420 unknown = count;
421 } else if (count >= first) {
422 second = first;
423 first = count;
424 } else if (count >= second) {
425 second = count;
426 }
427 }
428 return first > second + unknown;
429}
430
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000431static void increment_count(struct dir_rename_info *info,
Elijah Newren0c4fd732021-02-27 00:30:42 +0000432 char *old_dir,
433 char *new_dir)
434{
435 struct strintmap *counts;
436 struct strmap_entry *e;
437
438 /* Get the {new_dirs -> counts} mapping using old_dir */
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000439 e = strmap_get_entry(info->dir_rename_count, old_dir);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000440 if (e) {
441 counts = e->value;
442 } else {
443 counts = xmalloc(sizeof(*counts));
444 strintmap_init_with_options(counts, 0, NULL, 1);
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000445 strmap_put(info->dir_rename_count, old_dir, counts);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000446 }
447
448 /* Increment the count for new_dir */
449 strintmap_incr(counts, new_dir, 1);
450}
451
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000452static void update_dir_rename_counts(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000453 struct strintmap *dirs_removed,
Elijah Newren0c4fd732021-02-27 00:30:42 +0000454 const char *oldname,
455 const char *newname)
456{
Andrzej Hunt4e3250b2021-07-25 15:08:23 +0200457 char *old_dir;
458 char *new_dir;
459 const char new_dir_first_char = newname[0];
Elijah Newren0c4fd732021-02-27 00:30:42 +0000460 int first_time_in_loop = 1;
461
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000462 if (!info->setup)
463 /*
464 * info->setup is 0 here in two cases: (1) all auxiliary
465 * vars (like dirs_removed) were NULL so
466 * initialize_dir_rename_info() returned early, or (2)
467 * either break detection or copy detection are active so
468 * that we never called initialize_dir_rename_info(). In
469 * the former case, we don't have enough info to know if
470 * directories were renamed (because dirs_removed lets us
471 * know about a necessary prerequisite, namely if they were
472 * removed), and in the latter, we don't care about
473 * directory renames or find_basename_matches.
474 *
475 * This matters because both basename and inexact matching
476 * will also call update_dir_rename_counts(). In either of
477 * the above two cases info->dir_rename_counts will not
478 * have been properly initialized which prevents us from
479 * updating it, but in these two cases we don't care about
480 * dir_rename_counts anyway, so we can just exit early.
481 */
482 return;
483
Andrzej Hunt4e3250b2021-07-25 15:08:23 +0200484
485 old_dir = xstrdup(oldname);
486 new_dir = xstrdup(newname);
487
Elijah Newren0c4fd732021-02-27 00:30:42 +0000488 while (1) {
Elijah Newrene54385b2021-03-13 22:22:04 +0000489 int drd_flag = NOT_RELEVANT;
490
Elijah Newren333899e2021-02-27 00:30:47 +0000491 /* Get old_dir, skip if its directory isn't relevant. */
Elijah Newren0c4fd732021-02-27 00:30:42 +0000492 dirname_munge(old_dir);
Elijah Newren333899e2021-02-27 00:30:47 +0000493 if (info->relevant_source_dirs &&
Elijah Newrena49b55d2021-03-13 22:22:02 +0000494 !strintmap_contains(info->relevant_source_dirs, old_dir))
Elijah Newren333899e2021-02-27 00:30:47 +0000495 break;
496
497 /* Get new_dir */
Elijah Newren0c4fd732021-02-27 00:30:42 +0000498 dirname_munge(new_dir);
499
500 /*
501 * When renaming
502 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
503 * then this suggests that both
504 * a/b/c/d/e/ => a/b/some/thing/else/e/
505 * a/b/c/d/ => a/b/some/thing/else/
506 * so we want to increment counters for both. We do NOT,
507 * however, also want to suggest that there was the following
508 * rename:
509 * a/b/c/ => a/b/some/thing/
510 * so we need to quit at that point.
511 *
512 * Note the when first_time_in_loop, we only strip off the
513 * basename, and we don't care if that's different.
514 */
515 if (!first_time_in_loop) {
516 char *old_sub_dir = strchr(old_dir, '\0')+1;
517 char *new_sub_dir = strchr(new_dir, '\0')+1;
518 if (!*new_dir) {
519 /*
520 * Special case when renaming to root directory,
521 * i.e. when new_dir == "". In this case, we had
522 * something like
523 * a/b/subdir => subdir
524 * and so dirname_munge() sets things up so that
525 * old_dir = "a/b\0subdir\0"
526 * new_dir = "\0ubdir\0"
527 * We didn't have a '/' to overwrite a '\0' onto
528 * in new_dir, so we have to compare differently.
529 */
530 if (new_dir_first_char != old_sub_dir[0] ||
531 strcmp(old_sub_dir+1, new_sub_dir))
532 break;
533 } else {
534 if (strcmp(old_sub_dir, new_sub_dir))
535 break;
536 }
537 }
538
Elijah Newrene54385b2021-03-13 22:22:04 +0000539 /*
540 * Above we suggested that we'd keep recording renames for
541 * all ancestor directories where the trailing directories
542 * matched, i.e. for
543 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
544 * we'd increment rename counts for each of
545 * a/b/c/d/e/ => a/b/some/thing/else/e/
546 * a/b/c/d/ => a/b/some/thing/else/
547 * However, we only need the rename counts for directories
548 * in dirs_removed whose value is RELEVANT_FOR_SELF.
549 * However, we add one special case of also recording it for
550 * first_time_in_loop because find_basename_matches() can
551 * use that as a hint to find a good pairing.
552 */
553 if (dirs_removed)
554 drd_flag = strintmap_get(dirs_removed, old_dir);
555 if (drd_flag == RELEVANT_FOR_SELF || first_time_in_loop)
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000556 increment_count(info, old_dir, new_dir);
Elijah Newren0c4fd732021-02-27 00:30:42 +0000557
Elijah Newrene54385b2021-03-13 22:22:04 +0000558 first_time_in_loop = 0;
559 if (drd_flag == NOT_RELEVANT)
560 break;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000561 /* If we hit toplevel directory ("") for old or new dir, quit */
562 if (!*old_dir || !*new_dir)
563 break;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000564 }
565
566 /* Free resources we don't need anymore */
567 free(old_dir);
568 free(new_dir);
569}
570
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000571static void initialize_dir_rename_info(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000572 struct strintmap *relevant_sources,
573 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +0000574 struct strmap *dir_rename_count,
575 struct strmap *cached_pairs)
Elijah Newren0c4fd732021-02-27 00:30:42 +0000576{
Elijah Newren81afdf72021-02-27 00:30:48 +0000577 struct hashmap_iter iter;
578 struct strmap_entry *entry;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000579 int i;
580
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000581 if (!dirs_removed && !relevant_sources) {
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000582 info->setup = 0;
583 return;
Elijah Newren0c4fd732021-02-27 00:30:42 +0000584 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000585 info->setup = 1;
586
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000587 info->dir_rename_count = dir_rename_count;
588 if (!info->dir_rename_count) {
589 info->dir_rename_count = xmalloc(sizeof(*dir_rename_count));
590 strmap_init(info->dir_rename_count);
591 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000592 strintmap_init_with_options(&info->idx_map, -1, NULL, 0);
593 strmap_init_with_options(&info->dir_rename_guess, NULL, 0);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000594
Elijah Newren333899e2021-02-27 00:30:47 +0000595 /* Setup info->relevant_source_dirs */
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000596 info->relevant_source_dirs = NULL;
597 if (dirs_removed || !relevant_sources) {
598 info->relevant_source_dirs = dirs_removed; /* might be NULL */
599 } else {
600 info->relevant_source_dirs = xmalloc(sizeof(struct strintmap));
Elijah Newrena49b55d2021-03-13 22:22:02 +0000601 strintmap_init(info->relevant_source_dirs, 0 /* unused */);
602 strintmap_for_each_entry(relevant_sources, &iter, entry) {
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000603 char *dirname = get_dirname(entry->key);
604 if (!dirs_removed ||
Elijah Newrena49b55d2021-03-13 22:22:02 +0000605 strintmap_contains(dirs_removed, dirname))
606 strintmap_set(info->relevant_source_dirs,
607 dirname, 0 /* value irrelevant */);
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000608 free(dirname);
609 }
610 }
Elijah Newren333899e2021-02-27 00:30:47 +0000611
Elijah Newrenae8cf742021-02-27 00:30:41 +0000612 /*
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000613 * Loop setting up both info->idx_map, and doing setup of
614 * info->dir_rename_count.
Elijah Newrenae8cf742021-02-27 00:30:41 +0000615 */
616 for (i = 0; i < rename_dst_nr; ++i) {
617 /*
618 * For non-renamed files, make idx_map contain mapping of
619 * filename -> index (index within rename_dst, that is)
620 */
621 if (!rename_dst[i].is_rename) {
622 char *filename = rename_dst[i].p->two->path;
623 strintmap_set(&info->idx_map, filename, i);
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000624 continue;
Elijah Newrenae8cf742021-02-27 00:30:41 +0000625 }
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000626
627 /*
628 * For everything else (i.e. renamed files), make
629 * dir_rename_count contain a map of a map:
630 * old_directory -> {new_directory -> count}
631 * In other words, for every pair look at the directories for
632 * the old filename and the new filename and count how many
633 * times that pairing occurs.
634 */
635 update_dir_rename_counts(info, dirs_removed,
636 rename_dst[i].p->one->path,
637 rename_dst[i].p->two->path);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000638 }
Elijah Newren81afdf72021-02-27 00:30:48 +0000639
Elijah Newren25e65b62021-05-20 06:09:41 +0000640 /* Add cached_pairs to counts */
641 strmap_for_each_entry(cached_pairs, &iter, entry) {
642 const char *old_name = entry->key;
643 const char *new_name = entry->value;
644 if (!new_name)
645 /* known delete; ignore it */
646 continue;
647
648 update_dir_rename_counts(info, dirs_removed, old_name, new_name);
649 }
650
Elijah Newren81afdf72021-02-27 00:30:48 +0000651 /*
652 * Now we collapse
653 * dir_rename_count: old_directory -> {new_directory -> count}
654 * down to
655 * dir_rename_guess: old_directory -> best_new_directory
656 * where best_new_directory is the one with the highest count.
657 */
658 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
659 /* entry->key is source_dir */
660 struct strintmap *counts = entry->value;
661 char *best_newdir;
662
663 best_newdir = xstrdup(get_highest_rename_path(counts));
664 strmap_put(&info->dir_rename_guess, entry->key,
665 best_newdir);
666 }
Elijah Newrenae8cf742021-02-27 00:30:41 +0000667}
668
Elijah Newrencd52e002021-02-27 00:30:43 +0000669void partial_clear_dir_rename_count(struct strmap *dir_rename_count)
670{
671 struct hashmap_iter iter;
672 struct strmap_entry *entry;
673
674 strmap_for_each_entry(dir_rename_count, &iter, entry) {
675 struct strintmap *counts = entry->value;
676 strintmap_clear(counts);
677 }
678 strmap_partial_clear(dir_rename_count, 1);
679}
680
Elijah Newrenb1473012021-02-27 00:30:45 +0000681static void cleanup_dir_rename_info(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000682 struct strintmap *dirs_removed,
Elijah Newrenb1473012021-02-27 00:30:45 +0000683 int keep_dir_rename_count)
Elijah Newrenae8cf742021-02-27 00:30:41 +0000684{
Elijah Newrenb1473012021-02-27 00:30:45 +0000685 struct hashmap_iter iter;
686 struct strmap_entry *entry;
687 struct string_list to_remove = STRING_LIST_INIT_NODUP;
688 int i;
689
Elijah Newrenae8cf742021-02-27 00:30:41 +0000690 if (!info->setup)
691 return;
692
693 /* idx_map */
694 strintmap_clear(&info->idx_map);
695
696 /* dir_rename_guess */
697 strmap_clear(&info->dir_rename_guess, 1);
698
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000699 /* relevant_source_dirs */
700 if (info->relevant_source_dirs &&
701 info->relevant_source_dirs != dirs_removed) {
Elijah Newrena49b55d2021-03-13 22:22:02 +0000702 strintmap_clear(info->relevant_source_dirs);
Elijah Newrene4fd06e2021-03-11 00:38:31 +0000703 FREE_AND_NULL(info->relevant_source_dirs);
704 }
705
Elijah Newrenb6e3d272021-02-27 00:30:44 +0000706 /* dir_rename_count */
Elijah Newrenb1473012021-02-27 00:30:45 +0000707 if (!keep_dir_rename_count) {
708 partial_clear_dir_rename_count(info->dir_rename_count);
709 strmap_clear(info->dir_rename_count, 1);
710 FREE_AND_NULL(info->dir_rename_count);
711 return;
712 }
713
714 /*
715 * Although dir_rename_count was passed in
716 * diffcore_rename_extended() and we want to keep it around and
Elijah Newrenbf238b72021-03-13 22:22:06 +0000717 * return it to that caller, we first want to remove any counts in
718 * the maps associated with UNKNOWN_DIR entries and any data
Elijah Newrenb1473012021-02-27 00:30:45 +0000719 * associated with directories that weren't renamed.
720 */
721 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
722 const char *source_dir = entry->key;
723 struct strintmap *counts = entry->value;
724
Elijah Newrenfb529382021-03-13 22:22:03 +0000725 if (!strintmap_get(dirs_removed, source_dir)) {
Elijah Newrenb1473012021-02-27 00:30:45 +0000726 string_list_append(&to_remove, source_dir);
727 strintmap_clear(counts);
728 continue;
729 }
Elijah Newrenbf238b72021-03-13 22:22:06 +0000730
731 if (strintmap_contains(counts, UNKNOWN_DIR))
732 strintmap_remove(counts, UNKNOWN_DIR);
Elijah Newrenb1473012021-02-27 00:30:45 +0000733 }
734 for (i = 0; i < to_remove.nr; ++i)
735 strmap_remove(info->dir_rename_count,
736 to_remove.items[i].string, 1);
737 string_list_clear(&to_remove, 0);
Elijah Newrenae8cf742021-02-27 00:30:41 +0000738}
739
Elijah Newrena35df332021-02-14 07:51:47 +0000740static const char *get_basename(const char *filename)
741{
742 /*
743 * gitbasename() has to worry about special drives, multiple
744 * directory separator characters, trailing slashes, NULL or
745 * empty strings, etc. We only work on filenames as stored in
746 * git, and thus get to ignore all those complications.
747 */
748 const char *base = strrchr(filename, '/');
749 return base ? base + 1 : filename;
750}
751
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000752static int idx_possible_rename(char *filename, struct dir_rename_info *info)
Elijah Newren37a25142021-02-27 00:30:39 +0000753{
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000754 /*
755 * Our comparison of files with the same basename (see
756 * find_basename_matches() below), is only helpful when after exact
757 * rename detection we have exactly one file with a given basename
758 * among the rename sources and also only exactly one file with
759 * that basename among the rename destinations. When we have
760 * multiple files with the same basename in either set, we do not
761 * know which to compare against. However, there are some
762 * filenames that occur in large numbers (particularly
763 * build-related filenames such as 'Makefile', '.gitignore', or
764 * 'build.gradle' that potentially exist within every single
765 * subdirectory), and for performance we want to be able to quickly
766 * find renames for these files too.
767 *
768 * The reason basename comparisons are a useful heuristic was that it
769 * is common for people to move files across directories while keeping
770 * their filename the same. If we had a way of determining or even
771 * making a good educated guess about which directory these non-unique
772 * basename files had moved the file to, we could check it.
773 * Luckily...
774 *
775 * When an entire directory is in fact renamed, we have two factors
776 * helping us out:
777 * (a) the original directory disappeared giving us a hint
778 * about when we can apply an extra heuristic.
779 * (a) we often have several files within that directory and
780 * subdirectories that are renamed without changes
781 * So, rules for a heuristic:
782 * (0) If there basename matches are non-unique (the condition under
783 * which this function is called) AND
784 * (1) the directory in which the file was found has disappeared
785 * (i.e. dirs_removed is non-NULL and has a relevant entry) THEN
786 * (2) use exact renames of files within the directory to determine
787 * where the directory is likely to have been renamed to. IF
788 * there is at least one exact rename from within that
789 * directory, we can proceed.
790 * (3) If there are multiple places the directory could have been
791 * renamed to based on exact renames, ignore all but one of them.
792 * Just use the destination with the most renames going to it.
793 * (4) Check if applying that directory rename to the original file
794 * would result in a destination filename that is in the
795 * potential rename set. If so, return the index of the
796 * destination file (the index within rename_dst).
797 * (5) Compare the original file and returned destination for
798 * similarity, and if they are sufficiently similar, record the
799 * rename.
800 *
801 * This function, idx_possible_rename(), is only responsible for (4).
Elijah Newren81afdf72021-02-27 00:30:48 +0000802 * The conditions/steps in (1)-(3) are handled via setting up
803 * dir_rename_count and dir_rename_guess in
804 * initialize_dir_rename_info(). Steps (0) and (5) are handled by
805 * the caller of this function.
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000806 */
807 char *old_dir, *new_dir;
808 struct strbuf new_path = STRBUF_INIT;
809 int idx;
810
811 if (!info->setup)
812 return -1;
813
814 old_dir = get_dirname(filename);
815 new_dir = strmap_get(&info->dir_rename_guess, old_dir);
816 free(old_dir);
817 if (!new_dir)
818 return -1;
819
820 strbuf_addstr(&new_path, new_dir);
821 strbuf_addch(&new_path, '/');
822 strbuf_addstr(&new_path, get_basename(filename));
823
824 idx = strintmap_get(&info->idx_map, new_path.buf);
825 strbuf_release(&new_path);
826 return idx;
Elijah Newren37a25142021-02-27 00:30:39 +0000827}
828
Elijah Newren1aedd032021-06-22 08:04:40 +0000829struct basename_prefetch_options {
830 struct repository *repo;
831 struct strintmap *relevant_sources;
832 struct strintmap *sources;
833 struct strintmap *dests;
834 struct dir_rename_info *info;
835};
836static void basename_prefetch(void *prefetch_options)
837{
838 struct basename_prefetch_options *options = prefetch_options;
839 struct strintmap *relevant_sources = options->relevant_sources;
840 struct strintmap *sources = options->sources;
841 struct strintmap *dests = options->dests;
842 struct dir_rename_info *info = options->info;
843 int i;
844 struct oid_array to_fetch = OID_ARRAY_INIT;
845
846 /*
847 * TODO: The following loops mirror the code/logic from
848 * find_basename_matches(), though not quite exactly. Maybe
849 * abstract the iteration logic out somehow?
850 */
851 for (i = 0; i < rename_src_nr; ++i) {
852 char *filename = rename_src[i].p->one->path;
853 const char *base = NULL;
854 intptr_t src_index;
855 intptr_t dst_index;
856
857 /* Skip irrelevant sources */
858 if (relevant_sources &&
859 !strintmap_contains(relevant_sources, filename))
860 continue;
861
862 /*
863 * If the basename is unique among remaining sources, then
864 * src_index will equal 'i' and we can attempt to match it
865 * to a unique basename in the destinations. Otherwise,
866 * use directory rename heuristics, if possible.
867 */
868 base = get_basename(filename);
869 src_index = strintmap_get(sources, base);
870 assert(src_index == -1 || src_index == i);
871
872 if (strintmap_contains(dests, base)) {
873 struct diff_filespec *one, *two;
874
875 /* Find a matching destination, if possible */
876 dst_index = strintmap_get(dests, base);
877 if (src_index == -1 || dst_index == -1) {
878 src_index = i;
879 dst_index = idx_possible_rename(filename, info);
880 }
881 if (dst_index == -1)
882 continue;
883
884 /* Ignore this dest if already used in a rename */
885 if (rename_dst[dst_index].is_rename)
886 continue; /* already used previously */
887
888 one = rename_src[src_index].p->one;
889 two = rename_dst[dst_index].p->two;
890
891 /* Add the pairs */
892 diff_add_if_missing(options->repo, &to_fetch, two);
893 diff_add_if_missing(options->repo, &to_fetch, one);
894 }
895 }
896
897 promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr);
898 oid_array_clear(&to_fetch);
899}
900
Elijah Newrena35df332021-02-14 07:51:47 +0000901static int find_basename_matches(struct diff_options *options,
Elijah Newrenbde8b9f2021-02-27 00:30:40 +0000902 int minimum_score,
Elijah Newren1ad69eb2021-02-27 00:30:46 +0000903 struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +0000904 struct strintmap *relevant_sources,
905 struct strintmap *dirs_removed)
Elijah Newrena35df332021-02-14 07:51:47 +0000906{
Elijah Newrenda09f652021-02-14 07:51:48 +0000907 /*
908 * When I checked in early 2020, over 76% of file renames in linux
909 * just moved files to a different directory but kept the same
910 * basename. gcc did that with over 64% of renames, gecko did it
911 * with over 79%, and WebKit did it with over 89%.
912 *
913 * Therefore we can bypass the normal exhaustive NxM matrix
914 * comparison of similarities between all potential rename sources
915 * and destinations by instead using file basename as a hint (i.e.
916 * the portion of the filename after the last '/'), checking for
917 * similarity between files with the same basename, and if we find
918 * a pair that are sufficiently similar, record the rename pair and
919 * exclude those two from the NxM matrix.
920 *
921 * This *might* cause us to find a less than optimal pairing (if
922 * there is another file that we are even more similar to but has a
923 * different basename). Given the huge performance advantage
924 * basename matching provides, and given the frequency with which
925 * people use the same basename in real world projects, that's a
926 * trade-off we are willing to accept when doing just rename
927 * detection.
928 *
929 * If someone wants copy detection that implies they are willing to
930 * spend more cycles to find similarities between files, so it may
931 * be less likely that this heuristic is wanted. If someone is
932 * doing break detection, that means they do not want filename
933 * similarity to imply any form of content similiarity, and thus
934 * this heuristic would definitely be incompatible.
935 */
936
937 int i, renames = 0;
Elijah Newrena35df332021-02-14 07:51:47 +0000938 struct strintmap sources;
939 struct strintmap dests;
Elijah Newrend331dd32021-06-22 08:04:39 +0000940 struct diff_populate_filespec_options dpf_options = {
941 .check_binary = 0,
942 .missing_object_cb = NULL,
943 .missing_object_data = NULL
944 };
Elijah Newren1aedd032021-06-22 08:04:40 +0000945 struct basename_prefetch_options prefetch_options = {
Elijah Newrend331dd32021-06-22 08:04:39 +0000946 .repo = options->repo,
Elijah Newren1aedd032021-06-22 08:04:40 +0000947 .relevant_sources = relevant_sources,
948 .sources = &sources,
949 .dests = &dests,
950 .info = info
Elijah Newrend331dd32021-06-22 08:04:39 +0000951 };
Elijah Newrena35df332021-02-14 07:51:47 +0000952
953 /*
954 * Create maps of basename -> fullname(s) for remaining sources and
955 * dests.
956 */
957 strintmap_init_with_options(&sources, -1, NULL, 0);
958 strintmap_init_with_options(&dests, -1, NULL, 0);
959 for (i = 0; i < rename_src_nr; ++i) {
960 char *filename = rename_src[i].p->one->path;
961 const char *base;
962
963 /* exact renames removed in remove_unneeded_paths_from_src() */
964 assert(!rename_src[i].p->one->rename_used);
965
966 /* Record index within rename_src (i) if basename is unique */
967 base = get_basename(filename);
968 if (strintmap_contains(&sources, base))
969 strintmap_set(&sources, base, -1);
970 else
971 strintmap_set(&sources, base, i);
972 }
973 for (i = 0; i < rename_dst_nr; ++i) {
974 char *filename = rename_dst[i].p->two->path;
975 const char *base;
976
977 if (rename_dst[i].is_rename)
978 continue; /* involved in exact match already. */
979
980 /* Record index within rename_dst (i) if basename is unique */
981 base = get_basename(filename);
982 if (strintmap_contains(&dests, base))
983 strintmap_set(&dests, base, -1);
984 else
985 strintmap_set(&dests, base, i);
986 }
987
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200988 if (options->repo == the_repository && repo_has_promisor_remote(the_repository)) {
Elijah Newren1aedd032021-06-22 08:04:40 +0000989 dpf_options.missing_object_cb = basename_prefetch;
Elijah Newrend331dd32021-06-22 08:04:39 +0000990 dpf_options.missing_object_data = &prefetch_options;
991 }
992
Elijah Newrenda09f652021-02-14 07:51:48 +0000993 /* Now look for basename matchups and do similarity estimation */
Elijah Newren37a25142021-02-27 00:30:39 +0000994 for (i = 0; i < rename_src_nr; ++i) {
995 char *filename = rename_src[i].p->one->path;
996 const char *base = NULL;
997 intptr_t src_index;
Elijah Newrenda09f652021-02-14 07:51:48 +0000998 intptr_t dst_index;
Elijah Newrenda09f652021-02-14 07:51:48 +0000999
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001000 /* Skip irrelevant sources */
1001 if (relevant_sources &&
Elijah Newrena49b55d2021-03-13 22:22:02 +00001002 !strintmap_contains(relevant_sources, filename))
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001003 continue;
1004
Elijah Newren37a25142021-02-27 00:30:39 +00001005 /*
1006 * If the basename is unique among remaining sources, then
1007 * src_index will equal 'i' and we can attempt to match it
1008 * to a unique basename in the destinations. Otherwise,
1009 * use directory rename heuristics, if possible.
1010 */
1011 base = get_basename(filename);
1012 src_index = strintmap_get(&sources, base);
1013 assert(src_index == -1 || src_index == i);
1014
1015 if (strintmap_contains(&dests, base)) {
Elijah Newrenda09f652021-02-14 07:51:48 +00001016 struct diff_filespec *one, *two;
1017 int score;
1018
Elijah Newren37a25142021-02-27 00:30:39 +00001019 /* Find a matching destination, if possible */
1020 dst_index = strintmap_get(&dests, base);
1021 if (src_index == -1 || dst_index == -1) {
1022 src_index = i;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001023 dst_index = idx_possible_rename(filename, info);
Elijah Newren37a25142021-02-27 00:30:39 +00001024 }
1025 if (dst_index == -1)
1026 continue;
1027
1028 /* Ignore this dest if already used in a rename */
1029 if (rename_dst[dst_index].is_rename)
1030 continue; /* already used previously */
1031
Elijah Newrenda09f652021-02-14 07:51:48 +00001032 /* Estimate the similarity */
1033 one = rename_src[src_index].p->one;
1034 two = rename_dst[dst_index].p->two;
1035 score = estimate_similarity(options->repo, one, two,
Elijah Newrend331dd32021-06-22 08:04:39 +00001036 minimum_score, &dpf_options);
Elijah Newrenda09f652021-02-14 07:51:48 +00001037
1038 /* If sufficiently similar, record as rename pair */
1039 if (score < minimum_score)
1040 continue;
1041 record_rename_pair(dst_index, src_index, score);
1042 renames++;
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001043 update_dir_rename_counts(info, dirs_removed,
1044 one->path, two->path);
Elijah Newrenda09f652021-02-14 07:51:48 +00001045
1046 /*
1047 * Found a rename so don't need text anymore; if we
1048 * didn't find a rename, the filespec_blob would get
1049 * re-used when doing the matrix of comparisons.
1050 */
1051 diff_free_filespec_blob(one);
1052 diff_free_filespec_blob(two);
1053 }
1054 }
Elijah Newrena35df332021-02-14 07:51:47 +00001055
1056 strintmap_clear(&sources);
1057 strintmap_clear(&dests);
1058
Elijah Newrenda09f652021-02-14 07:51:48 +00001059 return renames;
Elijah Newrena35df332021-02-14 07:51:47 +00001060}
1061
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001062#define NUM_CANDIDATE_PER_DST 4
1063static void record_if_better(struct diff_score m[], struct diff_score *o)
1064{
1065 int i, worst;
1066
1067 /* find the worst one */
1068 worst = 0;
1069 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
1070 if (score_compare(&m[i], &m[worst]) > 0)
1071 worst = i;
1072
1073 /* is it better than the worst one? */
1074 if (score_compare(&m[worst], o) > 0)
1075 m[worst] = *o;
1076}
1077
Junio C Hamanof31027c2011-01-06 13:50:06 -08001078/*
1079 * Returns:
1080 * 0 if we are under the limit;
1081 * 1 if we need to disable inexact rename detection;
1082 * 2 if we would be under the limit if we were given -C instead of -C -C.
1083 */
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001084static int too_many_rename_candidates(int num_destinations, int num_sources,
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001085 struct diff_options *options)
1086{
1087 int rename_limit = options->rename_limit;
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001088 int i, limited_sources;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001089
1090 options->needed_rename_limit = 0;
1091
1092 /*
1093 * This basically does a test for the rename matrix not
1094 * growing larger than a "rename_limit" square matrix, ie:
1095 *
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001096 * num_destinations * num_sources > rename_limit * rename_limit
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001097 *
1098 * We use st_mult() to check overflow conditions; in the
1099 * exceptional circumstance that size_t isn't large enough to hold
1100 * the multiplication, the system won't be able to allocate enough
1101 * memory for the matrix anyway.
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001102 */
Jonathan Tan89973552017-11-29 12:11:54 -08001103 if (rename_limit <= 0)
Elijah Newren9dd29db2021-07-15 00:45:23 +00001104 return 0; /* treat as unlimited */
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001105 if (st_mult(num_destinations, num_sources)
1106 <= st_mult(rename_limit, rename_limit))
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001107 return 0;
1108
1109 options->needed_rename_limit =
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001110 num_sources > num_destinations ? num_sources : num_destinations;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001111
1112 /* Are we running under -C -C? */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001113 if (!options->flags.find_copies_harder)
Junio C Hamanof31027c2011-01-06 13:50:06 -08001114 return 1;
1115
1116 /* Would we bust the limit if we were running under -C? */
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001117 for (limited_sources = i = 0; i < num_sources; i++) {
Junio C Hamanof31027c2011-01-06 13:50:06 -08001118 if (diff_unmodified_pair(rename_src[i].p))
1119 continue;
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001120 limited_sources++;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001121 }
Elijah Newrenad8a1be2020-12-11 09:08:42 +00001122 if (st_mult(num_destinations, limited_sources)
1123 <= st_mult(rename_limit, rename_limit))
Junio C Hamanof31027c2011-01-06 13:50:06 -08001124 return 2;
Junio C Hamano9d8a5a52011-01-06 13:50:04 -08001125 return 1;
1126}
1127
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001128static int find_renames(struct diff_score *mx,
1129 int dst_cnt,
1130 int minimum_score,
1131 int copies,
1132 struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001133 struct strintmap *dirs_removed)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001134{
1135 int count = 0, i;
1136
1137 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
1138 struct diff_rename_dst *dst;
1139
1140 if ((mx[i].dst < 0) ||
1141 (mx[i].score < minimum_score))
1142 break; /* there is no more usable pair. */
1143 dst = &rename_dst[mx[i].dst];
Elijah Newren9db2ac52020-12-11 09:08:47 +00001144 if (dst->is_rename)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001145 continue; /* already done, either exact or fuzzy. */
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001146 if (!copies && rename_src[mx[i].src].p->one->rename_used)
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001147 continue;
1148 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
1149 count++;
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001150 update_dir_rename_counts(info, dirs_removed,
1151 rename_src[mx[i].src].p->one->path,
1152 rename_dst[mx[i].dst].p->two->path);
Linus Torvalds0940e5f2011-02-18 20:10:32 -08001153 }
1154 return count;
1155}
1156
Elijah Newren97998892021-03-11 00:38:24 +00001157static void remove_unneeded_paths_from_src(int detecting_copies,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001158 struct strintmap *interesting)
Elijah Newren829514c2021-02-14 07:35:01 +00001159{
1160 int i, new_num_src;
1161
Elijah Newren97998892021-03-11 00:38:24 +00001162 if (detecting_copies && !interesting)
Elijah Newren829514c2021-02-14 07:35:01 +00001163 return; /* nothing to remove */
1164 if (break_idx)
1165 return; /* culling incompatible with break detection */
1166
1167 /*
1168 * Note on reasons why we cull unneeded sources but not destinations:
1169 * 1) Pairings are stored in rename_dst (not rename_src), which we
1170 * need to keep around. So, we just can't cull rename_dst even
1171 * if we wanted to. But doing so wouldn't help because...
1172 *
1173 * 2) There is a matrix pairwise comparison that follows the
1174 * "Performing inexact rename detection" progress message.
1175 * Iterating over the destinations is done in the outer loop,
1176 * hence we only iterate over each of those once and we can
1177 * easily skip the outer loop early if the destination isn't
1178 * relevant. That's only one check per destination path to
1179 * skip.
1180 *
1181 * By contrast, the sources are iterated in the inner loop; if
1182 * we check whether a source can be skipped, then we'll be
1183 * checking it N separate times, once for each destination.
1184 * We don't want to have to iterate over known-not-needed
1185 * sources N times each, so avoid that by removing the sources
1186 * from rename_src here.
1187 */
1188 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
Elijah Newren97998892021-03-11 00:38:24 +00001189 struct diff_filespec *one = rename_src[i].p->one;
1190
Elijah Newren829514c2021-02-14 07:35:01 +00001191 /*
1192 * renames are stored in rename_dst, so if a rename has
1193 * already been detected using this source, we can just
1194 * remove the source knowing rename_dst has its info.
1195 */
Elijah Newren97998892021-03-11 00:38:24 +00001196 if (!detecting_copies && one->rename_used)
1197 continue;
1198
1199 /* If we don't care about the source path, skip it */
Elijah Newrena49b55d2021-03-13 22:22:02 +00001200 if (interesting && !strintmap_contains(interesting, one->path))
Elijah Newren829514c2021-02-14 07:35:01 +00001201 continue;
1202
1203 if (new_num_src < i)
1204 memcpy(&rename_src[new_num_src], &rename_src[i],
1205 sizeof(struct diff_rename_src));
1206 new_num_src++;
1207 }
1208
1209 rename_src_nr = new_num_src;
1210}
1211
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001212static void handle_early_known_dir_renames(struct dir_rename_info *info,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001213 struct strintmap *relevant_sources,
1214 struct strintmap *dirs_removed)
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001215{
1216 /*
Elijah Newren0491d392021-03-13 22:22:05 +00001217 * Directory renames are determined via an aggregate of all renames
1218 * under them and using a "majority wins" rule. The fact that
1219 * "majority wins", though, means we don't need all the renames
1220 * under the given directory, we only need enough to ensure we have
1221 * a majority.
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001222 */
Elijah Newren0491d392021-03-13 22:22:05 +00001223
Elijah Newren9bd34212021-03-13 22:22:08 +00001224 int i, new_num_src;
Elijah Newren0491d392021-03-13 22:22:05 +00001225 struct hashmap_iter iter;
1226 struct strmap_entry *entry;
1227
1228 if (!dirs_removed || !relevant_sources)
1229 return; /* nothing to cull */
1230 if (break_idx)
1231 return; /* culling incompatbile with break detection */
1232
1233 /*
Elijah Newrenbf238b72021-03-13 22:22:06 +00001234 * Supplement dir_rename_count with number of potential renames,
1235 * marking all potential rename sources as mapping to UNKNOWN_DIR.
Elijah Newren0491d392021-03-13 22:22:05 +00001236 */
Elijah Newrenbf238b72021-03-13 22:22:06 +00001237 for (i = 0; i < rename_src_nr; i++) {
1238 char *old_dir;
1239 struct diff_filespec *one = rename_src[i].p->one;
1240
1241 /*
1242 * sources that are part of a rename will have already been
1243 * removed by a prior call to remove_unneeded_paths_from_src()
1244 */
1245 assert(!one->rename_used);
1246
1247 old_dir = get_dirname(one->path);
1248 while (*old_dir != '\0' &&
1249 NOT_RELEVANT != strintmap_get(dirs_removed, old_dir)) {
1250 char *freeme = old_dir;
1251
1252 increment_count(info, old_dir, UNKNOWN_DIR);
1253 old_dir = get_dirname(old_dir);
1254
1255 /* Free resources we don't need anymore */
1256 free(freeme);
1257 }
1258 /*
1259 * old_dir and new_dir free'd in increment_count, but
1260 * get_dirname() gives us a new pointer we need to free for
1261 * old_dir. Also, if the loop runs 0 times we need old_dir
1262 * to be freed.
1263 */
1264 free(old_dir);
1265 }
Elijah Newren0491d392021-03-13 22:22:05 +00001266
1267 /*
1268 * For any directory which we need a potential rename detected for
1269 * (i.e. those marked as RELEVANT_FOR_SELF in dirs_removed), check
1270 * whether we have enough renames to satisfy the "majority rules"
1271 * requirement such that detecting any more renames of files under
1272 * it won't change the result. For any such directory, mark that
1273 * we no longer need to detect a rename for it. However, since we
1274 * might need to still detect renames for an ancestor of that
1275 * directory, use RELEVANT_FOR_ANCESTOR.
1276 */
1277 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
1278 /* entry->key is source_dir */
1279 struct strintmap *counts = entry->value;
1280
1281 if (strintmap_get(dirs_removed, entry->key) ==
1282 RELEVANT_FOR_SELF &&
1283 dir_rename_already_determinable(counts)) {
1284 strintmap_set(dirs_removed, entry->key,
1285 RELEVANT_FOR_ANCESTOR);
1286 }
1287 }
Elijah Newren9bd34212021-03-13 22:22:08 +00001288
1289 for (i = 0, new_num_src = 0; i < rename_src_nr; i++) {
1290 struct diff_filespec *one = rename_src[i].p->one;
1291 int val;
1292
1293 val = strintmap_get(relevant_sources, one->path);
1294
1295 /*
1296 * sources that were not found in relevant_sources should
1297 * have already been removed by a prior call to
1298 * remove_unneeded_paths_from_src()
1299 */
1300 assert(val != -1);
1301
1302 if (val == RELEVANT_LOCATION) {
1303 int removable = 1;
1304 char *dir = get_dirname(one->path);
1305 while (1) {
1306 char *freeme = dir;
1307 int res = strintmap_get(dirs_removed, dir);
1308
1309 /* Quit if not found or irrelevant */
1310 if (res == NOT_RELEVANT)
1311 break;
1312 /* If RELEVANT_FOR_SELF, can't remove */
1313 if (res == RELEVANT_FOR_SELF) {
1314 removable = 0;
1315 break;
1316 }
1317 /* Else continue searching upwards */
1318 assert(res == RELEVANT_FOR_ANCESTOR);
1319 dir = get_dirname(dir);
1320 free(freeme);
1321 }
1322 free(dir);
1323 if (removable) {
1324 strintmap_set(relevant_sources, one->path,
1325 RELEVANT_NO_MORE);
1326 continue;
1327 }
1328 }
1329
1330 if (new_num_src < i)
1331 memcpy(&rename_src[new_num_src], &rename_src[i],
1332 sizeof(struct diff_rename_src));
1333 new_num_src++;
1334 }
1335
1336 rename_src_nr = new_num_src;
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001337}
1338
Elijah Newrena8791ef2021-07-30 11:47:41 +00001339static void free_filespec_data(struct diff_filespec *spec)
1340{
1341 if (!--spec->count)
1342 diff_free_filespec_data(spec);
1343}
1344
Elijah Newrena8791ef2021-07-30 11:47:41 +00001345static void pool_free_filespec(struct mem_pool *pool,
1346 struct diff_filespec *spec)
1347{
1348 if (!pool) {
1349 free_filespec(spec);
1350 return;
1351 }
1352
1353 /*
1354 * Similar to free_filespec(), but only frees the data. The spec
1355 * itself was allocated in the pool and should not be individually
1356 * freed.
1357 */
1358 free_filespec_data(spec);
1359}
1360
Elijah Newrena8791ef2021-07-30 11:47:41 +00001361void pool_diff_free_filepair(struct mem_pool *pool,
1362 struct diff_filepair *p)
1363{
1364 if (!pool) {
1365 diff_free_filepair(p);
1366 return;
1367 }
1368
1369 /*
1370 * Similar to diff_free_filepair() but only frees the data from the
1371 * filespecs; not the filespecs or the filepair which were
1372 * allocated from the pool.
1373 */
1374 free_filespec_data(p->one);
1375 free_filespec_data(p->two);
1376}
1377
Elijah Newren0c4fd732021-02-27 00:30:42 +00001378void diffcore_rename_extended(struct diff_options *options,
Elijah Newrenf239fff2021-07-30 11:47:42 +00001379 struct mem_pool *pool,
Elijah Newrena49b55d2021-03-13 22:22:02 +00001380 struct strintmap *relevant_sources,
1381 struct strintmap *dirs_removed,
Elijah Newren25e65b62021-05-20 06:09:41 +00001382 struct strmap *dir_rename_count,
1383 struct strmap *cached_pairs)
Junio C Hamano427dcb42005-05-21 02:39:09 -07001384{
Junio C Hamano8082d8d2005-09-21 00:18:27 -07001385 int detect_rename = options->detect_rename;
1386 int minimum_score = options->rename_score;
Junio C Hamano38c6f782005-05-21 19:40:36 -07001387 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano5098baf2005-09-15 16:13:43 -07001388 struct diff_queue_struct outq;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001389 struct diff_score *mx;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001390 int i, j, rename_count, skip_unmodified = 0;
Elijah Newren26a66a62020-12-11 09:08:40 +00001391 int num_destinations, dst_cnt;
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001392 int num_sources, want_copies;
Jeff King3ac942d2011-02-20 04:51:16 -05001393 struct progress *progress = NULL;
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001394 struct mem_pool local_pool;
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001395 struct dir_rename_info info;
Elijah Newrend331dd32021-06-22 08:04:39 +00001396 struct diff_populate_filespec_options dpf_options = {
1397 .check_binary = 0,
1398 .missing_object_cb = NULL,
1399 .missing_object_data = NULL
1400 };
Elijah Newren1aedd032021-06-22 08:04:40 +00001401 struct inexact_prefetch_options prefetch_options = {
Elijah Newrend331dd32021-06-22 08:04:39 +00001402 .repo = options->repo
1403 };
Junio C Hamano427dcb42005-05-21 02:39:09 -07001404
Elijah Newren557ac032021-01-23 22:01:12 -08001405 trace2_region_enter("diff", "setup", options->repo);
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001406 info.setup = 0;
Elijah Newren0c4fd732021-02-27 00:30:42 +00001407 assert(!dir_rename_count || strmap_empty(dir_rename_count));
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001408 want_copies = (detect_rename == DIFF_DETECT_COPY);
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001409 if (dirs_removed && (break_idx || want_copies))
1410 BUG("dirs_removed incompatible with break/copy detection");
Elijah Newren97998892021-03-11 00:38:24 +00001411 if (break_idx && relevant_sources)
1412 BUG("break detection incompatible with source specification");
Junio C Hamano26dee0a2005-05-21 23:33:32 -07001413 if (!minimum_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001414 minimum_score = DEFAULT_RENAME_SCORE;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001415
Junio C Hamano427dcb42005-05-21 02:39:09 -07001416 for (i = 0; i < q->nr; i++) {
Junio C Hamano52e95782005-05-21 02:40:01 -07001417 struct diff_filepair *p = q->queue[i];
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001418 if (!DIFF_FILE_VALID(p->one)) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -07001419 if (!DIFF_FILE_VALID(p->two))
Junio C Hamano60896c72005-05-22 21:24:49 -07001420 continue; /* unmerged */
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001421 else if (options->single_follow &&
1422 strcmp(options->single_follow, p->two->path))
1423 continue; /* not interested */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001424 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -07001425 is_empty_blob_oid(&p->two->oid))
Jeff King90d43b02012-03-22 18:52:13 -04001426 continue;
Elijah Newren9db2ac52020-12-11 09:08:47 +00001427 else if (add_rename_dst(p) < 0) {
Jeff King4d6be032015-02-26 20:42:27 -05001428 warning("skipping rename detection, detected"
1429 " duplicate destination '%s'",
1430 p->two->path);
1431 goto cleanup;
1432 }
Junio C Hamano2f3f8b22006-11-02 00:02:11 -08001433 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001434 else if (!options->flags.rename_empty &&
Brandon Williams02491b62017-05-30 10:31:08 -07001435 is_empty_blob_oid(&p->one->oid))
Jeff King90d43b02012-03-22 18:52:13 -04001436 continue;
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -04001437 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
Linus Torvalds64479712007-10-25 11:20:56 -07001438 /*
1439 * If the source is a broken "delete", and
Junio C Hamano22101002005-06-11 20:55:20 -07001440 * they did not really want to get broken,
1441 * that means the source actually stays.
Linus Torvalds64479712007-10-25 11:20:56 -07001442 * So we increment the "rename_used" score
1443 * by one, to indicate ourselves as a user
Junio C Hamano22101002005-06-11 20:55:20 -07001444 */
Linus Torvalds64479712007-10-25 11:20:56 -07001445 if (p->broken_pair && !p->score)
1446 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001447 register_rename_src(p);
Junio C Hamano22101002005-06-11 20:55:20 -07001448 }
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001449 else if (want_copies) {
Linus Torvalds64479712007-10-25 11:20:56 -07001450 /*
1451 * Increment the "rename_used" score by
1452 * one, to indicate ourselves as a user.
1453 */
1454 p->one->rename_used++;
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001455 register_rename_src(p);
Linus Torvalds64479712007-10-25 11:20:56 -07001456 }
Junio C Hamano427dcb42005-05-21 02:39:09 -07001457 }
Elijah Newren557ac032021-01-23 22:01:12 -08001458 trace2_region_leave("diff", "setup", options->repo);
Linus Torvalds0024a542007-09-14 10:39:48 -07001459 if (rename_dst_nr == 0 || rename_src_nr == 0)
Junio C Hamano427dcb42005-05-21 02:39:09 -07001460 goto cleanup; /* nothing to do */
1461
Elijah Newren557ac032021-01-23 22:01:12 -08001462 trace2_region_enter("diff", "exact renames", options->repo);
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001463 mem_pool_init(&local_pool, 32*1024);
Linus Torvalds0024a542007-09-14 10:39:48 -07001464 /*
Linus Torvalds17559a62007-10-25 11:24:47 -07001465 * We really want to cull the candidates list early
1466 * with cheap tests in order to avoid doing deltas.
1467 */
Elijah Newrenfa0e9362021-07-30 11:47:37 +00001468 rename_count = find_exact_renames(options, &local_pool);
1469 /*
1470 * Discard local_pool immediately instead of at "cleanup:" in order
1471 * to reduce maximum memory usage; inexact rename detection uses up
1472 * a fair amount of memory, and mem_pools can too.
1473 */
1474 mem_pool_discard(&local_pool, 0);
Elijah Newren557ac032021-01-23 22:01:12 -08001475 trace2_region_leave("diff", "exact renames", options->repo);
Linus Torvalds17559a62007-10-25 11:24:47 -07001476
Linus Torvalds42899ac2007-10-26 16:56:34 -07001477 /* Did we only want exact renames? */
1478 if (minimum_score == MAX_SCORE)
1479 goto cleanup;
1480
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001481 num_sources = rename_src_nr;
Linus Torvalds42899ac2007-10-26 16:56:34 -07001482
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001483 if (want_copies || break_idx) {
1484 /*
1485 * Cull sources:
1486 * - remove ones corresponding to exact renames
Elijah Newren97998892021-03-11 00:38:24 +00001487 * - remove ones not found in relevant_sources
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001488 */
1489 trace2_region_enter("diff", "cull after exact", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001490 remove_unneeded_paths_from_src(want_copies, relevant_sources);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001491 trace2_region_leave("diff", "cull after exact", options->repo);
1492 } else {
1493 /* Determine minimum score to match basenames */
1494 double factor = 0.5;
1495 char *basename_factor = getenv("GIT_BASENAME_FACTOR");
1496 int min_basename_score;
1497
1498 if (basename_factor)
1499 factor = strtol(basename_factor, NULL, 10)/100.0;
1500 assert(factor >= 0.0 && factor <= 1.0);
1501 min_basename_score = minimum_score +
1502 (int)(factor * (MAX_SCORE - minimum_score));
1503
1504 /*
1505 * Cull sources:
1506 * - remove ones involved in renames (found via exact match)
1507 */
1508 trace2_region_enter("diff", "cull after exact", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001509 remove_unneeded_paths_from_src(want_copies, NULL);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001510 trace2_region_leave("diff", "cull after exact", options->repo);
1511
Elijah Newrenae8cf742021-02-27 00:30:41 +00001512 /* Preparation for basename-driven matching. */
1513 trace2_region_enter("diff", "dir rename setup", options->repo);
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001514 initialize_dir_rename_info(&info, relevant_sources,
Elijah Newren25e65b62021-05-20 06:09:41 +00001515 dirs_removed, dir_rename_count,
1516 cached_pairs);
Elijah Newrenae8cf742021-02-27 00:30:41 +00001517 trace2_region_leave("diff", "dir rename setup", options->repo);
1518
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001519 /* Utilize file basenames to quickly find renames. */
1520 trace2_region_enter("diff", "basename matches", options->repo);
1521 rename_count += find_basename_matches(options,
Elijah Newrenbde8b9f2021-02-27 00:30:40 +00001522 min_basename_score,
Elijah Newrene4fd06e2021-03-11 00:38:31 +00001523 &info,
1524 relevant_sources,
1525 dirs_removed);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001526 trace2_region_leave("diff", "basename matches", options->repo);
1527
1528 /*
1529 * Cull sources, again:
1530 * - remove ones involved in renames (found via basenames)
Elijah Newren97998892021-03-11 00:38:24 +00001531 * - remove ones not found in relevant_sources
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001532 * and
1533 * - remove ones in relevant_sources which are needed only
1534 * for directory renames IF no ancestory directory
1535 * actually needs to know any more individual path
1536 * renames under them
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001537 */
1538 trace2_region_enter("diff", "cull basename", options->repo);
Elijah Newren97998892021-03-11 00:38:24 +00001539 remove_unneeded_paths_from_src(want_copies, relevant_sources);
Elijah Newrenae1db7b2021-03-13 22:22:01 +00001540 handle_early_known_dir_renames(&info, relevant_sources,
1541 dirs_removed);
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001542 trace2_region_leave("diff", "cull basename", options->repo);
1543 }
1544
1545 /* Calculate how many rename destinations are left */
1546 num_destinations = (rename_dst_nr - rename_count);
1547 num_sources = rename_src_nr; /* rename_src_nr reflects lower number */
1548
Linus Torvalds42899ac2007-10-26 16:56:34 -07001549 /* All done? */
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001550 if (!num_destinations || !num_sources)
Linus Torvalds42899ac2007-10-26 16:56:34 -07001551 goto cleanup;
1552
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001553 switch (too_many_rename_candidates(num_destinations, num_sources,
Elijah Newren00b8ccc2020-12-11 09:08:41 +00001554 options)) {
Junio C Hamanof31027c2011-01-06 13:50:06 -08001555 case 1:
Linus Torvalds0024a542007-09-14 10:39:48 -07001556 goto cleanup;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001557 case 2:
1558 options->degraded_cc_to_c = 1;
1559 skip_unmodified = 1;
1560 break;
1561 default:
1562 break;
1563 }
Linus Torvalds0024a542007-09-14 10:39:48 -07001564
Elijah Newren557ac032021-01-23 22:01:12 -08001565 trace2_region_enter("diff", "inexact renames", options->repo);
Jeff King3ac942d2011-02-20 04:51:16 -05001566 if (options->show_rename_progress) {
Junio C Hamano8aade102017-08-19 10:39:41 -07001567 progress = start_delayed_progress(
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +07001568 _("Performing inexact rename detection"),
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001569 (uint64_t)num_destinations * (uint64_t)num_sources);
Jeff King3ac942d2011-02-20 04:51:16 -05001570 }
1571
Elijah Newrend331dd32021-06-22 08:04:39 +00001572 /* Finish setting up dpf_options */
1573 prefetch_options.skip_unmodified = skip_unmodified;
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +02001574 if (options->repo == the_repository && repo_has_promisor_remote(the_repository)) {
Elijah Newren1aedd032021-06-22 08:04:40 +00001575 dpf_options.missing_object_cb = inexact_prefetch;
Elijah Newrend331dd32021-06-22 08:04:39 +00001576 dpf_options.missing_object_data = &prefetch_options;
1577 }
1578
René Scharfeca56dad2021-03-13 17:17:22 +01001579 CALLOC_ARRAY(mx, st_mult(NUM_CANDIDATE_PER_DST, num_destinations));
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001580 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
Elijah Newren9db2ac52020-12-11 09:08:47 +00001581 struct diff_filespec *two = rename_dst[i].p->two;
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001582 struct diff_score *m;
1583
Elijah Newren9db2ac52020-12-11 09:08:47 +00001584 if (rename_dst[i].is_rename)
Elijah Newrenbd24aa22021-02-14 07:51:49 +00001585 continue; /* exact or basename match already handled */
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001586
1587 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
1588 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
1589 m[j].dst = -1;
1590
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001591 for (j = 0; j < rename_src_nr; j++) {
Junio C Hamanoe88d6bc2011-01-06 13:50:05 -08001592 struct diff_filespec *one = rename_src[j].p->one;
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001593 struct diff_score this_src;
Junio C Hamanof31027c2011-01-06 13:50:06 -08001594
Elijah Newren829514c2021-02-14 07:35:01 +00001595 assert(!one->rename_used || want_copies || break_idx);
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001596
Junio C Hamanof31027c2011-01-06 13:50:06 -08001597 if (skip_unmodified &&
1598 diff_unmodified_pair(rename_src[j].p))
1599 continue;
1600
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02001601 this_src.score = estimate_similarity(options->repo,
1602 one, two,
Jonathan Tan95acf112020-04-07 15:11:43 -07001603 minimum_score,
Elijah Newrend331dd32021-06-22 08:04:39 +00001604 &dpf_options);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001605 this_src.name_score = basename_same(one, two);
1606 this_src.dst = i;
1607 this_src.src = j;
1608 record_if_better(m, &this_src);
Junio C Hamano809809b2009-11-20 22:13:47 -08001609 /*
1610 * Once we run estimate_similarity,
1611 * We do not need the text anymore.
1612 */
Junio C Hamano8ae92e62007-10-02 21:01:03 -07001613 diff_free_filespec_blob(one);
Junio C Hamano809809b2009-11-20 22:13:47 -08001614 diff_free_filespec_blob(two);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001615 }
1616 dst_cnt++;
Elijah Newren81c4bf02020-12-11 09:08:43 +00001617 display_progress(progress,
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001618 (uint64_t)dst_cnt * (uint64_t)num_sources);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001619 }
Jeff King3ac942d2011-02-20 04:51:16 -05001620 stop_progress(&progress);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001621
Junio C Hamano427dcb42005-05-21 02:39:09 -07001622 /* cost matrix sorted by most to least similar pair */
Johannes Schindelin2049b8d2019-09-30 10:21:55 -07001623 STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
Junio C Hamano6d24ad92008-01-29 20:54:56 -08001624
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001625 rename_count += find_renames(mx, dst_cnt, minimum_score, 0,
1626 &info, dirs_removed);
Elijah Newrenf15eb7c2021-02-03 20:03:46 +00001627 if (want_copies)
Elijah Newren1ad69eb2021-02-27 00:30:46 +00001628 rename_count += find_renames(mx, dst_cnt, minimum_score, 1,
1629 &info, dirs_removed);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001630 free(mx);
Elijah Newren557ac032021-01-23 22:01:12 -08001631 trace2_region_leave("diff", "inexact renames", options->repo);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001632
Junio C Hamano15d061b2005-05-27 15:55:55 -07001633 cleanup:
Junio C Hamano427dcb42005-05-21 02:39:09 -07001634 /* At this point, we have found some renames and copies and they
Junio C Hamano5098baf2005-09-15 16:13:43 -07001635 * are recorded in rename_dst. The original list is still in *q.
Junio C Hamano427dcb42005-05-21 02:39:09 -07001636 */
Elijah Newren557ac032021-01-23 22:01:12 -08001637 trace2_region_enter("diff", "write back to queue", options->repo);
Bo Yang9ca5df92010-05-06 21:52:27 -07001638 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001639 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001640 struct diff_filepair *p = q->queue[i];
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001641 struct diff_filepair *pair_to_free = NULL;
Junio C Hamano427dcb42005-05-21 02:39:09 -07001642
Martin von Zweigbergkd7c9bf22011-03-23 22:41:01 -04001643 if (DIFF_PAIR_UNMERGED(p)) {
1644 diff_q(&outq, p);
1645 }
1646 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Elijah Newren9db2ac52020-12-11 09:08:47 +00001647 /* Creation */
1648 diff_q(&outq, p);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001649 }
Junio C Hamano2cd68882005-05-30 00:08:07 -07001650 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
1651 /*
1652 * Deletion
1653 *
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001654 * We would output this delete record if:
1655 *
1656 * (1) this is a broken delete and the counterpart
1657 * broken create remains in the output; or
Junio C Hamano5098baf2005-09-15 16:13:43 -07001658 * (2) this is not a broken delete, and rename_dst
1659 * does not have a rename/copy to move p->one->path
1660 * out of existence.
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001661 *
1662 * Otherwise, the counterpart broken create
1663 * has been turned into a rename-edit; or
1664 * delete did not have a matching create to
1665 * begin with.
Junio C Hamano2cd68882005-05-30 00:08:07 -07001666 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001667 if (DIFF_PAIR_BROKEN(p)) {
1668 /* broken delete */
Elijah Newren9db2ac52020-12-11 09:08:47 +00001669 struct diff_rename_dst *dst = locate_rename_dst(p);
1670 if (!dst)
1671 BUG("tracking failed somehow; failed to find associated dst for broken pair");
1672 if (dst->is_rename)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001673 /* counterpart is now rename/copy */
1674 pair_to_free = p;
1675 }
1676 else {
Linus Torvalds64479712007-10-25 11:20:56 -07001677 if (p->one->rename_used)
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001678 /* this path remains */
1679 pair_to_free = p;
1680 }
Junio C Hamano2cd68882005-05-30 00:08:07 -07001681
Elijah Newren9db2ac52020-12-11 09:08:47 +00001682 if (!pair_to_free)
Junio C Hamano2cd68882005-05-30 00:08:07 -07001683 diff_q(&outq, p);
1684 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001685 else if (!diff_unmodified_pair(p))
Junio C Hamano15d061b2005-05-27 15:55:55 -07001686 /* all the usual ones need to be kept */
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001687 diff_q(&outq, p);
Junio C Hamano15d061b2005-05-27 15:55:55 -07001688 else
Elijah Newren356da0f2021-06-08 16:11:41 +00001689 /* no need to keep unmodified pairs */
Junio C Hamano15d061b2005-05-27 15:55:55 -07001690 pair_to_free = p;
1691
Junio C Hamano226406f2005-05-27 15:50:30 -07001692 if (pair_to_free)
Elijah Newrenf239fff2021-07-30 11:47:42 +00001693 pool_diff_free_filepair(pool, pair_to_free);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001694 }
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001695 diff_debug_queue("done copying original", &outq);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001696
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001697 free(q->queue);
1698 *q = outq;
1699 diff_debug_queue("done collapsing", q);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001700
Linus Torvalds9fb88412007-10-25 11:19:10 -07001701 for (i = 0; i < rename_dst_nr; i++)
Elijah Newren9db2ac52020-12-11 09:08:47 +00001702 if (rename_dst[i].filespec_to_free)
Elijah Newrenf239fff2021-07-30 11:47:42 +00001703 pool_free_filespec(pool, rename_dst[i].filespec_to_free);
Junio C Hamano5098baf2005-09-15 16:13:43 -07001704
Elijah Newrenb1473012021-02-27 00:30:45 +00001705 cleanup_dir_rename_info(&info, dirs_removed, dir_rename_count != NULL);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001706 FREE_AND_NULL(rename_dst);
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001707 rename_dst_nr = rename_dst_alloc = 0;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001708 FREE_AND_NULL(rename_src);
Junio C Hamano25d5ea42005-05-24 01:10:48 -07001709 rename_src_nr = rename_src_alloc = 0;
Elijah Newren9db2ac52020-12-11 09:08:47 +00001710 if (break_idx) {
1711 strintmap_clear(break_idx);
1712 FREE_AND_NULL(break_idx);
1713 }
Elijah Newren557ac032021-01-23 22:01:12 -08001714 trace2_region_leave("diff", "write back to queue", options->repo);
Junio C Hamano427dcb42005-05-21 02:39:09 -07001715 return;
1716}
Elijah Newren0c4fd732021-02-27 00:30:42 +00001717
1718void diffcore_rename(struct diff_options *options)
1719{
Elijah Newrenf239fff2021-07-30 11:47:42 +00001720 diffcore_rename_extended(options, NULL, NULL, NULL, NULL, NULL);
Elijah Newren0c4fd732021-02-27 00:30:42 +00001721}