]> git.proxmox.com Git - libgit2.git/blame - src/diff_tform.c
Fix rename detection for tree-to-tree diffs
[libgit2.git] / src / diff_tform.c
CommitLineData
db106d01 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
db106d01
RB
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7#include "common.h"
114f5a6c 8
db106d01 9#include "git2/config.h"
960a04dd 10#include "git2/blob.h"
114f5a6c
RB
11
12#include "diff.h"
5e5848eb 13#include "hashsig.h"
114f5a6c
RB
14#include "path.h"
15#include "fileops.h"
db106d01
RB
16
17static git_diff_delta *diff_delta__dup(
18 const git_diff_delta *d, git_pool *pool)
19{
20 git_diff_delta *delta = git__malloc(sizeof(git_diff_delta));
21 if (!delta)
22 return NULL;
23
24 memcpy(delta, d, sizeof(git_diff_delta));
c68b09dc 25 GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags);
db106d01 26
d958e37a
RB
27 if (d->old_file.path != NULL) {
28 delta->old_file.path = git_pool_strdup(pool, d->old_file.path);
29 if (delta->old_file.path == NULL)
30 goto fail;
31 }
db106d01 32
d958e37a 33 if (d->new_file.path != d->old_file.path && d->new_file.path != NULL) {
db106d01
RB
34 delta->new_file.path = git_pool_strdup(pool, d->new_file.path);
35 if (delta->new_file.path == NULL)
36 goto fail;
37 } else {
38 delta->new_file.path = delta->old_file.path;
39 }
40
41 return delta;
42
43fail:
44 git__free(delta);
45 return NULL;
46}
47
48static git_diff_delta *diff_delta__merge_like_cgit(
49 const git_diff_delta *a, const git_diff_delta *b, git_pool *pool)
50{
51 git_diff_delta *dup;
52
53 /* Emulate C git for merging two diffs (a la 'git diff <sha>').
54 *
55 * When C git does a diff between the work dir and a tree, it actually
56 * diffs with the index but uses the workdir contents. This emulates
57 * those choices so we can emulate the type of diff.
58 *
59 * We have three file descriptions here, let's call them:
60 * f1 = a->old_file
61 * f2 = a->new_file AND b->old_file
62 * f3 = b->new_file
63 */
64
65 /* if f2 == f3 or f2 is deleted, then just dup the 'a' diff */
66 if (b->status == GIT_DELTA_UNMODIFIED || a->status == GIT_DELTA_DELETED)
67 return diff_delta__dup(a, pool);
68
69 /* otherwise, base this diff on the 'b' diff */
70 if ((dup = diff_delta__dup(b, pool)) == NULL)
71 return NULL;
72
73 /* If 'a' status is uninteresting, then we're done */
74 if (a->status == GIT_DELTA_UNMODIFIED)
75 return dup;
76
77 assert(a->status != GIT_DELTA_UNMODIFIED);
78 assert(b->status != GIT_DELTA_UNMODIFIED);
79
80 /* A cgit exception is that the diff of a file that is only in the
81 * index (i.e. not in HEAD nor workdir) is given as empty.
82 */
83 if (dup->status == GIT_DELTA_DELETED) {
84 if (a->status == GIT_DELTA_ADDED)
85 dup->status = GIT_DELTA_UNMODIFIED;
86 /* else don't overwrite DELETE status */
87 } else {
88 dup->status = a->status;
89 }
90
91 git_oid_cpy(&dup->old_file.oid, &a->old_file.oid);
92 dup->old_file.mode = a->old_file.mode;
93 dup->old_file.size = a->old_file.size;
94 dup->old_file.flags = a->old_file.flags;
95
96 return dup;
97}
98
99int git_diff_merge(
100 git_diff_list *onto,
101 const git_diff_list *from)
102{
103 int error = 0;
104 git_pool onto_pool;
105 git_vector onto_new;
106 git_diff_delta *delta;
107 bool ignore_case = false;
108 unsigned int i, j;
109
110 assert(onto && from);
111
112 if (!from->deltas.length)
113 return 0;
114
115 if (git_vector_init(
116 &onto_new, onto->deltas.length, git_diff_delta__cmp) < 0 ||
117 git_pool_init(&onto_pool, 1, 0) < 0)
118 return -1;
119
120 if ((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 ||
121 (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0)
122 {
123 ignore_case = true;
124
125 /* This function currently only supports merging diff lists that
126 * are sorted identically. */
127 assert((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 &&
128 (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0);
129 }
130
131 for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) {
132 git_diff_delta *o = GIT_VECTOR_GET(&onto->deltas, i);
133 const git_diff_delta *f = GIT_VECTOR_GET(&from->deltas, j);
134 int cmp = !f ? -1 : !o ? 1 : STRCMP_CASESELECT(ignore_case, o->old_file.path, f->old_file.path);
135
136 if (cmp < 0) {
137 delta = diff_delta__dup(o, &onto_pool);
138 i++;
139 } else if (cmp > 0) {
140 delta = diff_delta__dup(f, &onto_pool);
141 j++;
142 } else {
143 delta = diff_delta__merge_like_cgit(o, f, &onto_pool);
144 i++;
145 j++;
146 }
147
148 /* the ignore rules for the target may not match the source
149 * or the result of a merged delta could be skippable...
150 */
151 if (git_diff_delta__should_skip(&onto->opts, delta)) {
152 git__free(delta);
153 continue;
154 }
155
156 if ((error = !delta ? -1 : git_vector_insert(&onto_new, delta)) < 0)
157 break;
158 }
159
160 if (!error) {
161 git_vector_swap(&onto->deltas, &onto_new);
162 git_pool_swap(&onto->pool, &onto_pool);
163 onto->new_src = from->new_src;
164
165 /* prefix strings also come from old pool, so recreate those.*/
166 onto->opts.old_prefix =
167 git_pool_strdup_safe(&onto->pool, onto->opts.old_prefix);
168 onto->opts.new_prefix =
169 git_pool_strdup_safe(&onto->pool, onto->opts.new_prefix);
170 }
171
172 git_vector_foreach(&onto_new, i, delta)
173 git__free(delta);
174 git_vector_free(&onto_new);
175 git_pool_clear(&onto_pool);
176
177 return error;
178}
179
0462fba5 180int git_diff_find_similar__hashsig_for_file(
f8275890
RB
181 void **out, const git_diff_file *f, const char *path, void *p)
182{
183 git_hashsig_option_t opt = (git_hashsig_option_t)p;
aa408cbf
ET
184 int error = 0;
185
f8275890 186 GIT_UNUSED(f);
aa408cbf 187 error = git_hashsig_create_fromfile((git_hashsig **)out, path, opt);
1fed6b07 188
aa408cbf
ET
189 if (error == GIT_EBUFS) {
190 error = 0;
191 giterr_clear();
192 }
193
194 return error;
f8275890 195}
9bc8be3d 196
0462fba5 197int git_diff_find_similar__hashsig_for_buf(
f8275890
RB
198 void **out, const git_diff_file *f, const char *buf, size_t len, void *p)
199{
200 git_hashsig_option_t opt = (git_hashsig_option_t)p;
aa408cbf 201 int error = 0;
0462fba5 202
f8275890 203 GIT_UNUSED(f);
aa408cbf 204 error = git_hashsig_create((git_hashsig **)out, buf, len, opt);
1fed6b07 205
aa408cbf
ET
206 if (error == GIT_EBUFS) {
207 error = 0;
208 giterr_clear();
209 }
210
211 return error;
f8275890 212}
9bc8be3d 213
0462fba5 214void git_diff_find_similar__hashsig_free(void *sig, void *payload)
9bc8be3d
RB
215{
216 GIT_UNUSED(payload);
217 git_hashsig_free(sig);
218}
219
0462fba5 220int git_diff_find_similar__calc_similarity(
9bc8be3d
RB
221 int *score, void *siga, void *sigb, void *payload)
222{
223 GIT_UNUSED(payload);
224 *score = git_hashsig_compare(siga, sigb);
225 return 0;
226}
227
db106d01
RB
228#define DEFAULT_THRESHOLD 50
229#define DEFAULT_BREAK_REWRITE_THRESHOLD 60
a21cbb12 230#define DEFAULT_RENAME_LIMIT 200
db106d01
RB
231
232static int normalize_find_opts(
233 git_diff_list *diff,
234 git_diff_find_options *opts,
235 git_diff_find_options *given)
236{
237 git_config *cfg = NULL;
db106d01
RB
238
239 if (diff->repo != NULL &&
240 git_repository_config__weakptr(&cfg, diff->repo) < 0)
241 return -1;
242
243 if (given != NULL)
244 memcpy(opts, given, sizeof(*opts));
245 else {
0a008913
RB
246 const char *val = NULL;
247
248 GIT_INIT_STRUCTURE(opts, GIT_DIFF_FIND_OPTIONS_VERSION);
db106d01
RB
249
250 opts->flags = GIT_DIFF_FIND_RENAMES;
251
252 if (git_config_get_string(&val, cfg, "diff.renames") < 0)
253 giterr_clear();
254 else if (val &&
255 (!strcasecmp(val, "copies") || !strcasecmp(val, "copy")))
256 opts->flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
257 }
258
c7231c45 259 GITERR_CHECK_VERSION(opts, GIT_DIFF_FIND_OPTIONS_VERSION, "git_diff_find_options");
ca901e7b 260
db106d01
RB
261 /* some flags imply others */
262
9be5be47
RB
263 if (opts->flags & GIT_DIFF_FIND_EXACT_MATCH_ONLY) {
264 /* if we are only looking for exact matches, then don't turn
265 * MODIFIED items into ADD/DELETE pairs because it's too picky
266 */
267 opts->flags &= ~(GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES);
268
269 /* similarly, don't look for self-rewrites to split */
270 opts->flags &= ~GIT_DIFF_FIND_RENAMES_FROM_REWRITES;
271 }
272
db106d01
RB
273 if (opts->flags & GIT_DIFF_FIND_RENAMES_FROM_REWRITES)
274 opts->flags |= GIT_DIFF_FIND_RENAMES;
275
276 if (opts->flags & GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED)
277 opts->flags |= GIT_DIFF_FIND_COPIES;
278
d958e37a
RB
279 if (opts->flags & GIT_DIFF_BREAK_REWRITES)
280 opts->flags |= GIT_DIFF_FIND_REWRITES;
281
db106d01
RB
282#define USE_DEFAULT(X) ((X) == 0 || (X) > 100)
283
284 if (USE_DEFAULT(opts->rename_threshold))
285 opts->rename_threshold = DEFAULT_THRESHOLD;
286
287 if (USE_DEFAULT(opts->rename_from_rewrite_threshold))
288 opts->rename_from_rewrite_threshold = DEFAULT_THRESHOLD;
289
290 if (USE_DEFAULT(opts->copy_threshold))
291 opts->copy_threshold = DEFAULT_THRESHOLD;
292
293 if (USE_DEFAULT(opts->break_rewrite_threshold))
294 opts->break_rewrite_threshold = DEFAULT_BREAK_REWRITE_THRESHOLD;
295
296#undef USE_DEFAULT
297
a21cbb12 298 if (!opts->rename_limit) {
db106d01
RB
299 int32_t limit = 0;
300
a21cbb12 301 opts->rename_limit = DEFAULT_RENAME_LIMIT;
db106d01
RB
302
303 if (git_config_get_int32(&limit, cfg, "diff.renameLimit") < 0)
304 giterr_clear();
305 else if (limit > 0)
a21cbb12 306 opts->rename_limit = limit;
db106d01
RB
307 }
308
f8275890 309 /* assign the internal metric with whitespace flag as payload */
9bc8be3d 310 if (!opts->metric) {
f8275890
RB
311 opts->metric = git__malloc(sizeof(git_diff_similarity_metric));
312 GITERR_CHECK_ALLOC(opts->metric);
313
0462fba5
ET
314 opts->metric->file_signature = git_diff_find_similar__hashsig_for_file;
315 opts->metric->buffer_signature = git_diff_find_similar__hashsig_for_buf;
316 opts->metric->free_signature = git_diff_find_similar__hashsig_free;
317 opts->metric->similarity = git_diff_find_similar__calc_similarity;
f8275890 318
9bc8be3d 319 if (opts->flags & GIT_DIFF_FIND_IGNORE_WHITESPACE)
f8275890 320 opts->metric->payload = (void *)GIT_HASHSIG_IGNORE_WHITESPACE;
9bc8be3d 321 else if (opts->flags & GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE)
f8275890 322 opts->metric->payload = (void *)GIT_HASHSIG_NORMAL;
9bc8be3d 323 else
f8275890 324 opts->metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE;
9bc8be3d
RB
325 }
326
db106d01
RB
327 return 0;
328}
329
d958e37a
RB
330static int apply_splits_and_deletes(
331 git_diff_list *diff, size_t expected_size, bool actually_split)
db106d01
RB
332{
333 git_vector onto = GIT_VECTOR_INIT;
334 size_t i;
d958e37a 335 git_diff_delta *delta, *deleted;
db106d01
RB
336
337 if (git_vector_init(&onto, expected_size, git_diff_delta__cmp) < 0)
338 return -1;
339
340 /* build new delta list without TO_DELETE and splitting TO_SPLIT */
341 git_vector_foreach(&diff->deltas, i, delta) {
71a3d27e 342 if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
db106d01 343 continue;
db106d01 344
a21cbb12 345 if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0 && actually_split) {
d958e37a
RB
346 delta->similarity = 0;
347
348 /* make new record for DELETED side of split */
349 if (!(deleted = diff_delta__dup(delta, &diff->pool)))
11d9f6b3 350 goto on_error;
db106d01
RB
351
352 deleted->status = GIT_DELTA_DELETED;
353 memset(&deleted->new_file, 0, sizeof(deleted->new_file));
354 deleted->new_file.path = deleted->old_file.path;
71a3d27e 355 deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
db106d01 356
11d9f6b3
PK
357 if (git_vector_insert(&onto, deleted) < 0)
358 goto on_error;
db106d01 359
9be5be47
RB
360 if (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR)
361 delta->status = GIT_DELTA_UNTRACKED;
362 else
363 delta->status = GIT_DELTA_ADDED;
db106d01
RB
364 memset(&delta->old_file, 0, sizeof(delta->old_file));
365 delta->old_file.path = delta->new_file.path;
71a3d27e 366 delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
db106d01
RB
367 }
368
c68b09dc
RB
369 /* clean up delta before inserting into new list */
370 GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags);
371
372 if (delta->status != GIT_DELTA_COPIED &&
373 delta->status != GIT_DELTA_RENAMED &&
374 (delta->status != GIT_DELTA_MODIFIED || actually_split))
375 delta->similarity = 0;
376
377 /* insert into new list */
11d9f6b3
PK
378 if (git_vector_insert(&onto, delta) < 0)
379 goto on_error;
db106d01
RB
380 }
381
11d9f6b3 382 /* cannot return an error past this point */
c68b09dc
RB
383
384 /* free deltas from old list that didn't make it to the new one */
a21cbb12 385 git_vector_foreach(&diff->deltas, i, delta) {
71a3d27e 386 if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
11d9f6b3 387 git__free(delta);
a21cbb12
RB
388 }
389
db106d01 390 /* swap new delta list into place */
db106d01
RB
391 git_vector_swap(&diff->deltas, &onto);
392 git_vector_free(&onto);
a21cbb12 393 git_vector_sort(&diff->deltas);
db106d01
RB
394
395 return 0;
11d9f6b3
PK
396
397on_error:
398 git_vector_foreach(&onto, i, delta)
399 git__free(delta);
11d9f6b3
PK
400 git_vector_free(&onto);
401
402 return -1;
db106d01
RB
403}
404
960a04dd
RB
405GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx)
406{
407 git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2);
408 return (idx & 1) ? &delta->new_file : &delta->old_file;
409}
99ba8f23 410
a5140f4d
RB
411typedef struct {
412 size_t idx;
413 git_iterator_type_t src;
414 git_repository *repo;
415 git_diff_file *file;
416 git_buf data;
417 git_blob *blob;
418 int loaded;
419} similarity_info;
420
421static void similarity_init(
422 similarity_info *info, git_diff_list *diff, size_t file_idx)
423{
424 info->idx = file_idx;
425 info->src = (file_idx & 1) ? diff->new_src : diff->old_src;
426 info->repo = diff->repo;
427 info->file = similarity_get_file(diff, file_idx);
428 info->blob = NULL;
429 info->loaded = 0;
430 git_buf_init(&info->data, 0);
431}
432
433static int similarity_load(similarity_info *info)
db106d01 434{
960a04dd 435 int error = 0;
a5140f4d 436 git_diff_file *file = info->file;
960a04dd 437
a5140f4d
RB
438 if (info->src == GIT_ITERATOR_TYPE_WORKDIR) {
439 error = git_buf_joinpath(
440 &info->data, git_repository_workdir(info->repo), file->path);
960a04dd 441
a5140f4d
RB
442 /* if path is not a regular file, just skip this item */
443 if (!error && !git_path_isfile(info->data.ptr))
444 git_buf_free(&info->data);
445 } else if (git_blob_lookup(&info->blob, info->repo, &file->oid) < 0) {
446 /* if lookup fails, just skip this item in similarity calc */
447 giterr_clear();
448 } else {
449 if (!file->size)
450 file->size = git_blob_rawsize(info->blob);
451 assert(file->size == git_blob_rawsize(info->blob));
960a04dd 452
a5140f4d
RB
453 info->data.size = (size_t)(git__is_sizet(file->size) ? file->size : -1);
454 info->data.ptr = (char *)git_blob_rawcontent(info->blob);
455 }
09fae31d 456
a5140f4d 457 info->loaded = 1;
960a04dd 458
a5140f4d
RB
459 return error;
460}
db106d01 461
a5140f4d
RB
462static void similarity_unload(similarity_info *info)
463{
464 if (info->blob)
465 git_blob_free(info->blob);
466 else
467 git_buf_free(&info->data);
5e5848eb 468
a5140f4d
RB
469 info->loaded = 0;
470}
960a04dd 471
a5140f4d
RB
472static int similarity_calc(
473 similarity_info *info,
474 const git_diff_find_options *opts,
475 void **cache)
476{
477 int error = 0;
8cfd54f0 478
a5140f4d
RB
479 if (!info->loaded && (error = similarity_load(info)) < 0)
480 return error;
960a04dd 481
a5140f4d
RB
482 if (!info->data.size)
483 return 0;
484
485 if (info->src == GIT_ITERATOR_TYPE_WORKDIR) {
486 /* TODO: apply wd-to-odb filters to file data if necessary */
487
488 error = opts->metric->file_signature(
489 &cache[info->idx], info->file,
490 info->data.ptr, opts->metric->payload);
491 } else {
492 error = opts->metric->buffer_signature(
493 &cache[info->idx], info->file,
494 info->data.ptr, info->data.size, opts->metric->payload);
960a04dd
RB
495 }
496
497 return error;
498}
499
a21cbb12 500#define FLAG_SET(opts,flag_name) (((opts)->flags & flag_name) != 0)
9be5be47
RB
501
502/* - score < 0 means files cannot be compared
503 * - score >= 100 means files are exact match
504 * - score == 0 means files are completely different
505 */
960a04dd 506static int similarity_measure(
9be5be47 507 int *score,
960a04dd 508 git_diff_list *diff,
a21cbb12 509 const git_diff_find_options *opts,
960a04dd
RB
510 void **cache,
511 size_t a_idx,
512 size_t b_idx)
513{
960a04dd
RB
514 git_diff_file *a_file = similarity_get_file(diff, a_idx);
515 git_diff_file *b_file = similarity_get_file(diff, b_idx);
a21cbb12 516 bool exact_match = FLAG_SET(opts, GIT_DIFF_FIND_EXACT_MATCH_ONLY);
a5140f4d
RB
517 int error = 0;
518 similarity_info a_info, b_info;
9be5be47
RB
519
520 *score = -1;
960a04dd 521
9be5be47 522 /* don't try to compare files of different types */
960a04dd
RB
523 if (GIT_MODE_TYPE(a_file->mode) != GIT_MODE_TYPE(b_file->mode))
524 return 0;
525
a1683f28 526 /* if exact match is requested, force calculation of missing OIDs now */
9be5be47
RB
527 if (exact_match) {
528 if (git_oid_iszero(&a_file->oid) &&
529 diff->old_src == GIT_ITERATOR_TYPE_WORKDIR &&
530 !git_diff__oid_for_file(diff->repo, a_file->path,
531 a_file->mode, a_file->size, &a_file->oid))
532 a_file->flags |= GIT_DIFF_FLAG_VALID_OID;
533
534 if (git_oid_iszero(&b_file->oid) &&
535 diff->new_src == GIT_ITERATOR_TYPE_WORKDIR &&
536 !git_diff__oid_for_file(diff->repo, b_file->path,
537 b_file->mode, b_file->size, &b_file->oid))
538 b_file->flags |= GIT_DIFF_FLAG_VALID_OID;
539 }
540
541 /* check OID match as a quick test */
542 if (git_oid__cmp(&a_file->oid, &b_file->oid) == 0) {
543 *score = 100;
544 return 0;
545 }
546
547 /* don't calculate signatures if we are doing exact match */
548 if (exact_match) {
549 *score = 0;
550 return 0;
551 }
db106d01 552
a5140f4d
RB
553 similarity_init(&a_info, diff, a_idx);
554 similarity_init(&b_info, diff, b_idx);
555
556 if (!a_file->size && (error = similarity_load(&a_info)) < 0)
557 goto done;
558 if (!b_file->size && (error = similarity_load(&b_info)) < 0)
559 goto done;
560
f5c4d022 561 /* check if file sizes are nowhere near each other */
18e9efc4
RB
562 if (a_file->size > 127 &&
563 b_file->size > 127 &&
564 (a_file->size > (b_file->size << 4) ||
565 b_file->size > (a_file->size << 4)))
a5140f4d 566 goto done;
18e9efc4 567
960a04dd 568 /* update signature cache if needed */
a5140f4d
RB
569 if (!cache[a_idx] && (error = similarity_calc(&a_info, opts, cache)) < 0)
570 goto done;
571 if (!cache[b_idx] && (error = similarity_calc(&b_info, opts, cache)) < 0)
572 goto done;
1fed6b07 573
a5140f4d
RB
574 /* calculate similarity provided that the metric choose to process
575 * both the a and b files (some may not if file is too big, etc).
576 */
577 if (cache[a_idx] && cache[b_idx])
578 error = opts->metric->similarity(
579 score, cache[a_idx], cache[b_idx], opts->metric->payload);
db106d01 580
a5140f4d
RB
581done:
582 similarity_unload(&a_info);
583 similarity_unload(&b_info);
584
585 return error;
db106d01
RB
586}
587
a21cbb12 588static int calc_self_similarity(
9be5be47 589 git_diff_list *diff,
a21cbb12
RB
590 const git_diff_find_options *opts,
591 size_t delta_idx,
592 void **cache)
9be5be47 593{
a21cbb12
RB
594 int error, similarity = -1;
595 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
596
597 if ((delta->flags & GIT_DIFF_FLAG__HAS_SELF_SIMILARITY) != 0)
598 return 0;
599
600 error = similarity_measure(
601 &similarity, diff, opts, cache, 2 * delta_idx, 2 * delta_idx + 1);
602 if (error < 0)
603 return error;
604
605 if (similarity >= 0) {
606 delta->similarity = (uint32_t)similarity;
607 delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY;
608 }
609
610 return 0;
611}
612
613static bool is_rename_target(
614 git_diff_list *diff,
615 const git_diff_find_options *opts,
616 size_t delta_idx,
617 void **cache)
618{
619 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
620
621 /* skip things that aren't plain blobs */
622 if (!GIT_MODE_ISBLOB(delta->new_file.mode))
623 return false;
624
625 /* only consider ADDED, RENAMED, COPIED, and split MODIFIED as
626 * targets; maybe include UNTRACKED and IGNORED if requested.
627 */
628 switch (delta->status) {
629 case GIT_DELTA_UNMODIFIED:
630 case GIT_DELTA_DELETED:
631 return false;
632
633 case GIT_DELTA_MODIFIED:
634 if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) &&
635 !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES))
636 return false;
637
638 if (calc_self_similarity(diff, opts, delta_idx, cache) < 0)
639 return false;
640
641 if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) &&
642 delta->similarity < opts->break_rewrite_threshold) {
643 delta->flags |= GIT_DIFF_FLAG__TO_SPLIT;
644 break;
645 }
646 if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
647 delta->similarity < opts->rename_from_rewrite_threshold)
648 break;
649
650 return false;
651
652 case GIT_DELTA_UNTRACKED:
a21cbb12
RB
653 if (!FLAG_SET(opts, GIT_DIFF_FIND_FOR_UNTRACKED))
654 return false;
655 break;
656
d55bed1a
ET
657 case GIT_DELTA_IGNORED:
658 return false;
659
a21cbb12
RB
660 default: /* all other status values should be checked */
661 break;
662 }
663
664 delta->flags |= GIT_DIFF_FLAG__IS_RENAME_TARGET;
665 return true;
666}
667
668static bool is_rename_source(
669 git_diff_list *diff,
670 const git_diff_find_options *opts,
671 size_t delta_idx,
672 void **cache)
673{
674 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
675
676 /* skip things that aren't blobs */
677 if (!GIT_MODE_ISBLOB(delta->old_file.mode))
678 return false;
679
680 switch (delta->status) {
681 case GIT_DELTA_ADDED:
682 case GIT_DELTA_UNTRACKED:
683 case GIT_DELTA_IGNORED:
684 return false;
685
686 case GIT_DELTA_DELETED:
687 case GIT_DELTA_TYPECHANGE:
688 break;
689
690 case GIT_DELTA_UNMODIFIED:
691 if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
692 return false;
693 break;
694
695 default: /* MODIFIED, RENAMED, COPIED */
696 /* if we're finding copies, this could be a source */
697 if (FLAG_SET(opts, GIT_DIFF_FIND_COPIES))
698 break;
699
700 /* otherwise, this is only a source if we can split it */
701 if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) &&
702 !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES))
703 return false;
704
705 if (calc_self_similarity(diff, opts, delta_idx, cache) < 0)
706 return false;
707
708 if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) &&
709 delta->similarity < opts->break_rewrite_threshold) {
710 delta->flags |= GIT_DIFF_FLAG__TO_SPLIT;
711 break;
712 }
713
714 if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
715 delta->similarity < opts->rename_from_rewrite_threshold)
716 break;
717
718 return false;
719 }
720
721 delta->flags |= GIT_DIFF_FLAG__IS_RENAME_SOURCE;
722 return true;
723}
724
725GIT_INLINE(bool) delta_is_split(git_diff_delta *delta)
726{
727 return (delta->status == GIT_DELTA_TYPECHANGE ||
728 (delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0);
729}
730
731GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta)
732{
733 return (delta->status == GIT_DELTA_ADDED ||
734 delta->status == GIT_DELTA_UNTRACKED ||
735 delta->status == GIT_DELTA_IGNORED);
9be5be47 736}
db106d01 737
e4acc3ba
RB
738GIT_INLINE(void) delta_make_rename(
739 git_diff_delta *to, const git_diff_delta *from, uint32_t similarity)
740{
741 to->status = GIT_DELTA_RENAMED;
742 to->similarity = similarity;
743 memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
744 to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
745}
746
d958e37a
RB
747typedef struct {
748 uint32_t idx;
749 uint32_t similarity;
750} diff_find_match;
751
db106d01
RB
752int git_diff_find_similar(
753 git_diff_list *diff,
754 git_diff_find_options *given_opts)
755{
e4acc3ba 756 size_t i, j, sigcache_size;
960a04dd 757 int error = 0, similarity;
db106d01
RB
758 git_diff_delta *from, *to;
759 git_diff_find_options opts;
e4acc3ba
RB
760 size_t num_srcs = 0, num_tgts = 0, tried_srcs = 0, tried_tgts = 0;
761 size_t num_rewrites = 0, num_updates = 0, num_bumped = 0;
762 void **sigcache; /* cache of similarity metric file signatures */
763 diff_find_match *match_srcs = NULL, *match_tgts = NULL, *best_match;
764 git_diff_file swap;
db106d01 765
960a04dd
RB
766 if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
767 return error;
db106d01 768
a21cbb12 769 /* TODO: maybe abort if deltas.length > rename_limit ??? */
d958e37a
RB
770 if (!git__is_uint32(diff->deltas.length))
771 return 0;
960a04dd 772
e4acc3ba
RB
773 sigcache_size = diff->deltas.length * 2; /* keep size b/c diff may change */
774 sigcache = git__calloc(sigcache_size, sizeof(void *));
775 GITERR_CHECK_ALLOC(sigcache);
776
777 /* Label rename sources and targets
778 *
779 * This will also set self-similarity scores for MODIFIED files and
780 * mark them for splitting if break-rewrites is enabled
781 */
782 git_vector_foreach(&diff->deltas, i, to) {
783 if (is_rename_source(diff, &opts, i, sigcache))
784 ++num_srcs;
785
786 if (is_rename_target(diff, &opts, i, sigcache))
787 ++num_tgts;
788 }
960a04dd 789
e4acc3ba
RB
790 /* if there are no candidate srcs or tgts, we're done */
791 if (!num_srcs || !num_tgts)
792 goto cleanup;
960a04dd 793
e4acc3ba
RB
794 match_tgts = git__calloc(diff->deltas.length, sizeof(diff_find_match));
795 GITERR_CHECK_ALLOC(match_tgts);
796 match_srcs = git__calloc(diff->deltas.length, sizeof(diff_find_match));
797 GITERR_CHECK_ALLOC(match_srcs);
db106d01 798
e4acc3ba
RB
799 /*
800 * Find best-fit matches for rename / copy candidates
801 */
d958e37a 802
e4acc3ba
RB
803find_best_matches:
804 tried_tgts = num_bumped = 0;
db106d01 805
e4acc3ba 806 git_vector_foreach(&diff->deltas, i, to) {
a21cbb12 807 /* skip things that are not rename targets */
e4acc3ba 808 if ((to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0)
d958e37a
RB
809 continue;
810
e4acc3ba 811 tried_srcs = 0;
db106d01 812
e4acc3ba 813 git_vector_foreach(&diff->deltas, j, from) {
a21cbb12 814 /* skip things that are not rename sources */
e4acc3ba 815 if ((from->flags & GIT_DIFF_FLAG__IS_RENAME_SOURCE) == 0)
960a04dd
RB
816 continue;
817
d958e37a 818 /* calculate similarity for this pair and find best match */
e4acc3ba
RB
819 if (i == j)
820 similarity = -1; /* don't measure self-similarity here */
821 else if ((error = similarity_measure(
822 &similarity, diff, &opts, sigcache, 2 * j, 2 * i + 1)) < 0)
960a04dd 823 goto cleanup;
a21cbb12 824
e4acc3ba
RB
825 /* if this pairing is better for the src and the tgt, keep it */
826 if (similarity > 0 &&
827 match_tgts[i].similarity < (uint32_t)similarity &&
828 match_srcs[j].similarity < (uint32_t)similarity)
829 {
830 if (match_tgts[i].similarity > 0) {
831 match_tgts[match_srcs[j].idx].similarity = 0;
832 match_srcs[match_tgts[i].idx].similarity = 0;
833 ++num_bumped;
834 }
835
836 match_tgts[i].similarity = (uint32_t)similarity;
837 match_tgts[i].idx = (uint32_t)j;
a21cbb12 838
e4acc3ba
RB
839 match_srcs[j].similarity = (uint32_t)similarity;
840 match_srcs[j].idx = (uint32_t)i;
db106d01 841 }
e4acc3ba
RB
842
843 if (++tried_srcs >= num_srcs)
844 break;
845
846 /* cap on maximum targets we'll examine (per "to" file) */
847 if (tried_srcs > opts.rename_limit)
848 break;
db106d01 849 }
e4acc3ba
RB
850
851 if (++tried_tgts >= num_tgts)
852 break;
db106d01
RB
853 }
854
e4acc3ba
RB
855 if (num_bumped > 0) /* try again if we bumped some items */
856 goto find_best_matches;
857
858 /*
859 * Rewrite the diffs with renames / copies
860 */
861
862 tried_tgts = 0;
db106d01 863
a21cbb12 864 git_vector_foreach(&diff->deltas, i, to) {
e4acc3ba
RB
865 /* skip things that are not rename targets */
866 if ((to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0)
a21cbb12 867 continue;
690bf41c 868
e4acc3ba
RB
869 /* check if this delta was the target of a similarity */
870 best_match = &match_tgts[i];
871 if (!best_match->similarity)
872 continue;
d958e37a 873
e4acc3ba
RB
874 j = best_match->idx;
875 from = GIT_VECTOR_GET(&diff->deltas, j);
d958e37a 876
a21cbb12
RB
877 /* possible scenarios:
878 * 1. from DELETE to ADD/UNTRACK/IGNORE = RENAME
879 * 2. from DELETE to SPLIT/TYPECHANGE = RENAME + DELETE
880 * 3. from SPLIT/TYPECHANGE to ADD/UNTRACK/IGNORE = ADD + RENAME
881 * 4. from SPLIT/TYPECHANGE to SPLIT/TYPECHANGE = RENAME + SPLIT
882 * 5. from OTHER to ADD/UNTRACK/IGNORE = OTHER + COPY
db106d01
RB
883 */
884
885 if (from->status == GIT_DELTA_DELETED) {
db106d01 886
a21cbb12 887 if (delta_is_new_only(to)) {
db106d01 888
e4acc3ba 889 if (best_match->similarity < opts.rename_threshold)
a21cbb12 890 continue;
960a04dd 891
e4acc3ba 892 delta_make_rename(to, from, best_match->similarity);
d958e37a 893
e4acc3ba 894 from->flags |= GIT_DIFF_FLAG__TO_DELETE;
a21cbb12
RB
895 num_rewrites++;
896 } else {
49f70f2c 897 assert(delta_is_split(to));
960a04dd 898
e4acc3ba 899 if (best_match->similarity < opts.rename_from_rewrite_threshold)
a21cbb12 900 continue;
db106d01 901
e4acc3ba 902 memcpy(&swap, &to->old_file, sizeof(swap));
db106d01 903
e4acc3ba
RB
904 delta_make_rename(to, from, best_match->similarity);
905 num_rewrites--;
906
907 from->status = GIT_DELTA_DELETED;
908 memcpy(&from->old_file, &swap, sizeof(from->old_file));
909 memset(&from->new_file, 0, sizeof(from->new_file));
910 from->new_file.path = from->old_file.path;
911 from->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
db106d01 912
d958e37a 913 num_updates++;
db106d01
RB
914 }
915 }
916
a21cbb12 917 else if (delta_is_split(from)) {
a21cbb12
RB
918
919 if (delta_is_new_only(to)) {
db106d01 920
e4acc3ba 921 if (best_match->similarity < opts.rename_threshold)
a21cbb12 922 continue;
d958e37a 923
e4acc3ba 924 delta_make_rename(to, from, best_match->similarity);
a21cbb12 925
e4acc3ba 926 from->status = (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ?
a21cbb12 927 GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED;
e4acc3ba
RB
928 memset(&from->old_file, 0, sizeof(from->old_file));
929 from->old_file.path = from->new_file.path;
930 from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
931
932 from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
933 num_rewrites--;
a21cbb12
RB
934
935 num_updates++;
936 } else {
937 assert(delta_is_split(from));
938
e4acc3ba 939 if (best_match->similarity < opts.rename_from_rewrite_threshold)
a21cbb12
RB
940 continue;
941
e4acc3ba 942 memcpy(&swap, &to->old_file, sizeof(swap));
a21cbb12 943
e4acc3ba
RB
944 delta_make_rename(to, from, best_match->similarity);
945 num_rewrites--;
946 num_updates++;
a21cbb12 947
e4acc3ba 948 memcpy(&from->old_file, &swap, sizeof(from->old_file));
a21cbb12 949
e4acc3ba
RB
950 /* if we've just swapped the new element into the correct
951 * place, clear the SPLIT flag
67db583d 952 */
e4acc3ba
RB
953 if (match_tgts[j].idx == i &&
954 match_tgts[j].similarity >
67db583d
RB
955 opts.rename_from_rewrite_threshold) {
956
e4acc3ba
RB
957 from->status = GIT_DELTA_RENAMED;
958 from->similarity = match_tgts[j].similarity;
959 match_tgts[j].similarity = 0;
67db583d
RB
960 from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
961 num_rewrites--;
962 }
e4acc3ba
RB
963 /* otherwise, if we just overwrote a source, update mapping */
964 else if (j > i && match_srcs[i].similarity > 0) {
c4ac556e 965 match_tgts[match_srcs[i].idx].idx = (uint32_t)j;
e4acc3ba 966 }
67db583d 967
a21cbb12
RB
968 num_updates++;
969 }
970 }
971
972 else if (delta_is_new_only(to)) {
973 if (!FLAG_SET(&opts, GIT_DIFF_FIND_COPIES) ||
e4acc3ba 974 best_match->similarity < opts.copy_threshold)
a21cbb12
RB
975 continue;
976
e4acc3ba
RB
977 to->status = GIT_DELTA_COPIED;
978 to->similarity = best_match->similarity;
a21cbb12
RB
979 memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
980
981 num_updates++;
982 }
db106d01
RB
983 }
984
e4acc3ba
RB
985 /*
986 * Actually split and delete entries as needed
987 */
988
a21cbb12 989 if (num_rewrites > 0 || num_updates > 0)
960a04dd 990 error = apply_splits_and_deletes(
d958e37a 991 diff, diff->deltas.length - num_rewrites,
a21cbb12 992 FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES));
d958e37a 993
960a04dd 994cleanup:
e4acc3ba
RB
995 git__free(match_srcs);
996 git__free(match_tgts);
db106d01 997
e4acc3ba
RB
998 for (i = 0; i < sigcache_size; ++i) {
999 if (sigcache[i] != NULL)
1000 opts.metric->free_signature(sigcache[i], opts.metric->payload);
db106d01 1001 }
e4acc3ba 1002 git__free(sigcache);
db106d01 1003
f8275890
RB
1004 if (!given_opts || !given_opts->metric)
1005 git__free(opts.metric);
1006
960a04dd 1007 return error;
db106d01
RB
1008}
1009
1010#undef FLAG_SET