]> git.proxmox.com Git - libgit2.git/blame - src/diff_tform.c
Fix incorrect comment
[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
960a04dd
RB
411static int similarity_calc(
412 git_diff_list *diff,
a21cbb12 413 const git_diff_find_options *opts,
960a04dd
RB
414 size_t file_idx,
415 void **cache)
db106d01 416{
960a04dd
RB
417 int error = 0;
418 git_diff_file *file = similarity_get_file(diff, file_idx);
d958e37a 419 git_iterator_type_t src = (file_idx & 1) ? diff->new_src : diff->old_src;
960a04dd
RB
420
421 if (src == GIT_ITERATOR_TYPE_WORKDIR) { /* compute hashsig from file */
422 git_buf path = GIT_BUF_INIT;
423
424 /* TODO: apply wd-to-odb filters to file data if necessary */
425
09fae31d
RB
426 if ((error = git_buf_joinpath(
427 &path, git_repository_workdir(diff->repo), file->path)) < 0)
428 return error;
429
430 /* if path is not a regular file, just skip this item */
431 if (git_path_isfile(path.ptr))
960a04dd
RB
432 error = opts->metric->file_signature(
433 &cache[file_idx], file, path.ptr, opts->metric->payload);
434
435 git_buf_free(&path);
436 } else { /* compute hashsig from blob buffer */
437 git_blob *blob = NULL;
8cfd54f0 438 git_off_t blobsize;
db106d01 439
960a04dd 440 /* TODO: add max size threshold a la diff? */
5e5848eb 441
09fae31d
RB
442 if (git_blob_lookup(&blob, diff->repo, &file->oid) < 0) {
443 /* if lookup fails, just skip this item in similarity calc */
444 giterr_clear();
445 return 0;
446 }
960a04dd 447
8cfd54f0 448 blobsize = git_blob_rawsize(blob);
18e9efc4
RB
449 if (!file->size)
450 file->size = blobsize;
8cfd54f0
RB
451 if (!git__is_sizet(blobsize)) /* ? what to do ? */
452 blobsize = (size_t)-1;
453
960a04dd
RB
454 error = opts->metric->buffer_signature(
455 &cache[file_idx], file, git_blob_rawcontent(blob),
8cfd54f0 456 (size_t)blobsize, opts->metric->payload);
960a04dd
RB
457
458 git_blob_free(blob);
459 }
460
461 return error;
462}
463
a21cbb12 464#define FLAG_SET(opts,flag_name) (((opts)->flags & flag_name) != 0)
9be5be47
RB
465
466/* - score < 0 means files cannot be compared
467 * - score >= 100 means files are exact match
468 * - score == 0 means files are completely different
469 */
960a04dd 470static int similarity_measure(
9be5be47 471 int *score,
960a04dd 472 git_diff_list *diff,
a21cbb12 473 const git_diff_find_options *opts,
960a04dd
RB
474 void **cache,
475 size_t a_idx,
476 size_t b_idx)
477{
960a04dd
RB
478 git_diff_file *a_file = similarity_get_file(diff, a_idx);
479 git_diff_file *b_file = similarity_get_file(diff, b_idx);
a21cbb12 480 bool exact_match = FLAG_SET(opts, GIT_DIFF_FIND_EXACT_MATCH_ONLY);
9be5be47
RB
481
482 *score = -1;
960a04dd 483
9be5be47 484 /* don't try to compare files of different types */
960a04dd
RB
485 if (GIT_MODE_TYPE(a_file->mode) != GIT_MODE_TYPE(b_file->mode))
486 return 0;
487
a1683f28 488 /* if exact match is requested, force calculation of missing OIDs now */
9be5be47
RB
489 if (exact_match) {
490 if (git_oid_iszero(&a_file->oid) &&
491 diff->old_src == GIT_ITERATOR_TYPE_WORKDIR &&
492 !git_diff__oid_for_file(diff->repo, a_file->path,
493 a_file->mode, a_file->size, &a_file->oid))
494 a_file->flags |= GIT_DIFF_FLAG_VALID_OID;
495
496 if (git_oid_iszero(&b_file->oid) &&
497 diff->new_src == GIT_ITERATOR_TYPE_WORKDIR &&
498 !git_diff__oid_for_file(diff->repo, b_file->path,
499 b_file->mode, b_file->size, &b_file->oid))
500 b_file->flags |= GIT_DIFF_FLAG_VALID_OID;
501 }
502
503 /* check OID match as a quick test */
504 if (git_oid__cmp(&a_file->oid, &b_file->oid) == 0) {
505 *score = 100;
506 return 0;
507 }
508
509 /* don't calculate signatures if we are doing exact match */
510 if (exact_match) {
511 *score = 0;
512 return 0;
513 }
db106d01 514
f5c4d022 515 /* check if file sizes are nowhere near each other */
18e9efc4
RB
516 if (a_file->size > 127 &&
517 b_file->size > 127 &&
518 (a_file->size > (b_file->size << 4) ||
519 b_file->size > (a_file->size << 4)))
520 return 0;
521
960a04dd
RB
522 /* update signature cache if needed */
523 if (!cache[a_idx] && similarity_calc(diff, opts, a_idx, cache) < 0)
524 return -1;
525 if (!cache[b_idx] && similarity_calc(diff, opts, b_idx, cache) < 0)
526 return -1;
1fed6b07 527
aa408cbf
ET
528 /* some metrics may not wish to process this file (too big / too small) */
529 if (!cache[a_idx] || !cache[b_idx])
530 return 0;
db106d01 531
960a04dd 532 /* compare signatures */
9be5be47
RB
533 return opts->metric->similarity(
534 score, cache[a_idx], cache[b_idx], opts->metric->payload);
db106d01
RB
535}
536
a21cbb12 537static int calc_self_similarity(
9be5be47 538 git_diff_list *diff,
a21cbb12
RB
539 const git_diff_find_options *opts,
540 size_t delta_idx,
541 void **cache)
9be5be47 542{
a21cbb12
RB
543 int error, similarity = -1;
544 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
545
546 if ((delta->flags & GIT_DIFF_FLAG__HAS_SELF_SIMILARITY) != 0)
547 return 0;
548
549 error = similarity_measure(
550 &similarity, diff, opts, cache, 2 * delta_idx, 2 * delta_idx + 1);
551 if (error < 0)
552 return error;
553
554 if (similarity >= 0) {
555 delta->similarity = (uint32_t)similarity;
556 delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY;
557 }
558
559 return 0;
560}
561
562static bool is_rename_target(
563 git_diff_list *diff,
564 const git_diff_find_options *opts,
565 size_t delta_idx,
566 void **cache)
567{
568 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
569
570 /* skip things that aren't plain blobs */
571 if (!GIT_MODE_ISBLOB(delta->new_file.mode))
572 return false;
573
574 /* only consider ADDED, RENAMED, COPIED, and split MODIFIED as
575 * targets; maybe include UNTRACKED and IGNORED if requested.
576 */
577 switch (delta->status) {
578 case GIT_DELTA_UNMODIFIED:
579 case GIT_DELTA_DELETED:
580 return false;
581
582 case GIT_DELTA_MODIFIED:
583 if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) &&
584 !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES))
585 return false;
586
587 if (calc_self_similarity(diff, opts, delta_idx, cache) < 0)
588 return false;
589
590 if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) &&
591 delta->similarity < opts->break_rewrite_threshold) {
592 delta->flags |= GIT_DIFF_FLAG__TO_SPLIT;
593 break;
594 }
595 if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
596 delta->similarity < opts->rename_from_rewrite_threshold)
597 break;
598
599 return false;
600
601 case GIT_DELTA_UNTRACKED:
a21cbb12
RB
602 if (!FLAG_SET(opts, GIT_DIFF_FIND_FOR_UNTRACKED))
603 return false;
604 break;
605
d55bed1a
ET
606 case GIT_DELTA_IGNORED:
607 return false;
608
a21cbb12
RB
609 default: /* all other status values should be checked */
610 break;
611 }
612
613 delta->flags |= GIT_DIFF_FLAG__IS_RENAME_TARGET;
614 return true;
615}
616
617static bool is_rename_source(
618 git_diff_list *diff,
619 const git_diff_find_options *opts,
620 size_t delta_idx,
621 void **cache)
622{
623 git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx);
624
625 /* skip things that aren't blobs */
626 if (!GIT_MODE_ISBLOB(delta->old_file.mode))
627 return false;
628
629 switch (delta->status) {
630 case GIT_DELTA_ADDED:
631 case GIT_DELTA_UNTRACKED:
632 case GIT_DELTA_IGNORED:
633 return false;
634
635 case GIT_DELTA_DELETED:
636 case GIT_DELTA_TYPECHANGE:
637 break;
638
639 case GIT_DELTA_UNMODIFIED:
640 if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
641 return false;
642 break;
643
644 default: /* MODIFIED, RENAMED, COPIED */
645 /* if we're finding copies, this could be a source */
646 if (FLAG_SET(opts, GIT_DIFF_FIND_COPIES))
647 break;
648
649 /* otherwise, this is only a source if we can split it */
650 if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) &&
651 !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES))
652 return false;
653
654 if (calc_self_similarity(diff, opts, delta_idx, cache) < 0)
655 return false;
656
657 if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) &&
658 delta->similarity < opts->break_rewrite_threshold) {
659 delta->flags |= GIT_DIFF_FLAG__TO_SPLIT;
660 break;
661 }
662
663 if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
664 delta->similarity < opts->rename_from_rewrite_threshold)
665 break;
666
667 return false;
668 }
669
670 delta->flags |= GIT_DIFF_FLAG__IS_RENAME_SOURCE;
671 return true;
672}
673
674GIT_INLINE(bool) delta_is_split(git_diff_delta *delta)
675{
676 return (delta->status == GIT_DELTA_TYPECHANGE ||
677 (delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0);
678}
679
680GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta)
681{
682 return (delta->status == GIT_DELTA_ADDED ||
683 delta->status == GIT_DELTA_UNTRACKED ||
684 delta->status == GIT_DELTA_IGNORED);
9be5be47 685}
db106d01 686
e4acc3ba
RB
687GIT_INLINE(void) delta_make_rename(
688 git_diff_delta *to, const git_diff_delta *from, uint32_t similarity)
689{
690 to->status = GIT_DELTA_RENAMED;
691 to->similarity = similarity;
692 memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
693 to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
694}
695
d958e37a
RB
696typedef struct {
697 uint32_t idx;
698 uint32_t similarity;
699} diff_find_match;
700
db106d01
RB
701int git_diff_find_similar(
702 git_diff_list *diff,
703 git_diff_find_options *given_opts)
704{
e4acc3ba 705 size_t i, j, sigcache_size;
960a04dd 706 int error = 0, similarity;
db106d01
RB
707 git_diff_delta *from, *to;
708 git_diff_find_options opts;
e4acc3ba
RB
709 size_t num_srcs = 0, num_tgts = 0, tried_srcs = 0, tried_tgts = 0;
710 size_t num_rewrites = 0, num_updates = 0, num_bumped = 0;
711 void **sigcache; /* cache of similarity metric file signatures */
712 diff_find_match *match_srcs = NULL, *match_tgts = NULL, *best_match;
713 git_diff_file swap;
db106d01 714
960a04dd
RB
715 if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
716 return error;
db106d01 717
a21cbb12 718 /* TODO: maybe abort if deltas.length > rename_limit ??? */
d958e37a
RB
719 if (!git__is_uint32(diff->deltas.length))
720 return 0;
960a04dd 721
e4acc3ba
RB
722 sigcache_size = diff->deltas.length * 2; /* keep size b/c diff may change */
723 sigcache = git__calloc(sigcache_size, sizeof(void *));
724 GITERR_CHECK_ALLOC(sigcache);
725
726 /* Label rename sources and targets
727 *
728 * This will also set self-similarity scores for MODIFIED files and
729 * mark them for splitting if break-rewrites is enabled
730 */
731 git_vector_foreach(&diff->deltas, i, to) {
732 if (is_rename_source(diff, &opts, i, sigcache))
733 ++num_srcs;
734
735 if (is_rename_target(diff, &opts, i, sigcache))
736 ++num_tgts;
737 }
960a04dd 738
e4acc3ba
RB
739 /* if there are no candidate srcs or tgts, we're done */
740 if (!num_srcs || !num_tgts)
741 goto cleanup;
960a04dd 742
e4acc3ba
RB
743 match_tgts = git__calloc(diff->deltas.length, sizeof(diff_find_match));
744 GITERR_CHECK_ALLOC(match_tgts);
745 match_srcs = git__calloc(diff->deltas.length, sizeof(diff_find_match));
746 GITERR_CHECK_ALLOC(match_srcs);
db106d01 747
e4acc3ba
RB
748 /*
749 * Find best-fit matches for rename / copy candidates
750 */
d958e37a 751
e4acc3ba
RB
752find_best_matches:
753 tried_tgts = num_bumped = 0;
db106d01 754
e4acc3ba 755 git_vector_foreach(&diff->deltas, i, to) {
a21cbb12 756 /* skip things that are not rename targets */
e4acc3ba 757 if ((to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0)
d958e37a
RB
758 continue;
759
e4acc3ba 760 tried_srcs = 0;
db106d01 761
e4acc3ba 762 git_vector_foreach(&diff->deltas, j, from) {
a21cbb12 763 /* skip things that are not rename sources */
e4acc3ba 764 if ((from->flags & GIT_DIFF_FLAG__IS_RENAME_SOURCE) == 0)
960a04dd
RB
765 continue;
766
d958e37a 767 /* calculate similarity for this pair and find best match */
e4acc3ba
RB
768 if (i == j)
769 similarity = -1; /* don't measure self-similarity here */
770 else if ((error = similarity_measure(
771 &similarity, diff, &opts, sigcache, 2 * j, 2 * i + 1)) < 0)
960a04dd 772 goto cleanup;
a21cbb12 773
e4acc3ba
RB
774 /* if this pairing is better for the src and the tgt, keep it */
775 if (similarity > 0 &&
776 match_tgts[i].similarity < (uint32_t)similarity &&
777 match_srcs[j].similarity < (uint32_t)similarity)
778 {
779 if (match_tgts[i].similarity > 0) {
780 match_tgts[match_srcs[j].idx].similarity = 0;
781 match_srcs[match_tgts[i].idx].similarity = 0;
782 ++num_bumped;
783 }
784
785 match_tgts[i].similarity = (uint32_t)similarity;
786 match_tgts[i].idx = (uint32_t)j;
a21cbb12 787
e4acc3ba
RB
788 match_srcs[j].similarity = (uint32_t)similarity;
789 match_srcs[j].idx = (uint32_t)i;
db106d01 790 }
e4acc3ba
RB
791
792 if (++tried_srcs >= num_srcs)
793 break;
794
795 /* cap on maximum targets we'll examine (per "to" file) */
796 if (tried_srcs > opts.rename_limit)
797 break;
db106d01 798 }
e4acc3ba
RB
799
800 if (++tried_tgts >= num_tgts)
801 break;
db106d01
RB
802 }
803
e4acc3ba
RB
804 if (num_bumped > 0) /* try again if we bumped some items */
805 goto find_best_matches;
806
807 /*
808 * Rewrite the diffs with renames / copies
809 */
810
811 tried_tgts = 0;
db106d01 812
a21cbb12 813 git_vector_foreach(&diff->deltas, i, to) {
e4acc3ba
RB
814 /* skip things that are not rename targets */
815 if ((to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) == 0)
a21cbb12 816 continue;
690bf41c 817
e4acc3ba
RB
818 /* check if this delta was the target of a similarity */
819 best_match = &match_tgts[i];
820 if (!best_match->similarity)
821 continue;
d958e37a 822
e4acc3ba
RB
823 j = best_match->idx;
824 from = GIT_VECTOR_GET(&diff->deltas, j);
d958e37a 825
a21cbb12
RB
826 /* possible scenarios:
827 * 1. from DELETE to ADD/UNTRACK/IGNORE = RENAME
828 * 2. from DELETE to SPLIT/TYPECHANGE = RENAME + DELETE
829 * 3. from SPLIT/TYPECHANGE to ADD/UNTRACK/IGNORE = ADD + RENAME
830 * 4. from SPLIT/TYPECHANGE to SPLIT/TYPECHANGE = RENAME + SPLIT
831 * 5. from OTHER to ADD/UNTRACK/IGNORE = OTHER + COPY
db106d01
RB
832 */
833
834 if (from->status == GIT_DELTA_DELETED) {
db106d01 835
a21cbb12 836 if (delta_is_new_only(to)) {
db106d01 837
e4acc3ba 838 if (best_match->similarity < opts.rename_threshold)
a21cbb12 839 continue;
960a04dd 840
e4acc3ba 841 delta_make_rename(to, from, best_match->similarity);
d958e37a 842
e4acc3ba 843 from->flags |= GIT_DIFF_FLAG__TO_DELETE;
a21cbb12
RB
844 num_rewrites++;
845 } else {
49f70f2c 846 assert(delta_is_split(to));
960a04dd 847
e4acc3ba 848 if (best_match->similarity < opts.rename_from_rewrite_threshold)
a21cbb12 849 continue;
db106d01 850
e4acc3ba 851 memcpy(&swap, &to->old_file, sizeof(swap));
db106d01 852
e4acc3ba
RB
853 delta_make_rename(to, from, best_match->similarity);
854 num_rewrites--;
855
856 from->status = GIT_DELTA_DELETED;
857 memcpy(&from->old_file, &swap, sizeof(from->old_file));
858 memset(&from->new_file, 0, sizeof(from->new_file));
859 from->new_file.path = from->old_file.path;
860 from->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
db106d01 861
d958e37a 862 num_updates++;
db106d01
RB
863 }
864 }
865
a21cbb12 866 else if (delta_is_split(from)) {
a21cbb12
RB
867
868 if (delta_is_new_only(to)) {
db106d01 869
e4acc3ba 870 if (best_match->similarity < opts.rename_threshold)
a21cbb12 871 continue;
d958e37a 872
e4acc3ba 873 delta_make_rename(to, from, best_match->similarity);
a21cbb12 874
e4acc3ba 875 from->status = (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ?
a21cbb12 876 GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED;
e4acc3ba
RB
877 memset(&from->old_file, 0, sizeof(from->old_file));
878 from->old_file.path = from->new_file.path;
879 from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
880
881 from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
882 num_rewrites--;
a21cbb12
RB
883
884 num_updates++;
885 } else {
886 assert(delta_is_split(from));
887
e4acc3ba 888 if (best_match->similarity < opts.rename_from_rewrite_threshold)
a21cbb12
RB
889 continue;
890
e4acc3ba 891 memcpy(&swap, &to->old_file, sizeof(swap));
a21cbb12 892
e4acc3ba
RB
893 delta_make_rename(to, from, best_match->similarity);
894 num_rewrites--;
895 num_updates++;
a21cbb12 896
e4acc3ba 897 memcpy(&from->old_file, &swap, sizeof(from->old_file));
a21cbb12 898
e4acc3ba
RB
899 /* if we've just swapped the new element into the correct
900 * place, clear the SPLIT flag
67db583d 901 */
e4acc3ba
RB
902 if (match_tgts[j].idx == i &&
903 match_tgts[j].similarity >
67db583d
RB
904 opts.rename_from_rewrite_threshold) {
905
e4acc3ba
RB
906 from->status = GIT_DELTA_RENAMED;
907 from->similarity = match_tgts[j].similarity;
908 match_tgts[j].similarity = 0;
67db583d
RB
909 from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
910 num_rewrites--;
911 }
e4acc3ba
RB
912 /* otherwise, if we just overwrote a source, update mapping */
913 else if (j > i && match_srcs[i].similarity > 0) {
c4ac556e 914 match_tgts[match_srcs[i].idx].idx = (uint32_t)j;
e4acc3ba 915 }
67db583d 916
a21cbb12
RB
917 num_updates++;
918 }
919 }
920
921 else if (delta_is_new_only(to)) {
922 if (!FLAG_SET(&opts, GIT_DIFF_FIND_COPIES) ||
e4acc3ba 923 best_match->similarity < opts.copy_threshold)
a21cbb12
RB
924 continue;
925
e4acc3ba
RB
926 to->status = GIT_DELTA_COPIED;
927 to->similarity = best_match->similarity;
a21cbb12
RB
928 memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
929
930 num_updates++;
931 }
db106d01
RB
932 }
933
e4acc3ba
RB
934 /*
935 * Actually split and delete entries as needed
936 */
937
a21cbb12 938 if (num_rewrites > 0 || num_updates > 0)
960a04dd 939 error = apply_splits_and_deletes(
d958e37a 940 diff, diff->deltas.length - num_rewrites,
a21cbb12 941 FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES));
d958e37a 942
960a04dd 943cleanup:
e4acc3ba
RB
944 git__free(match_srcs);
945 git__free(match_tgts);
db106d01 946
e4acc3ba
RB
947 for (i = 0; i < sigcache_size; ++i) {
948 if (sigcache[i] != NULL)
949 opts.metric->free_signature(sigcache[i], opts.metric->payload);
db106d01 950 }
e4acc3ba 951 git__free(sigcache);
db106d01 952
f8275890
RB
953 if (!given_opts || !given_opts->metric)
954 git__free(opts.metric);
955
960a04dd 956 return error;
db106d01
RB
957}
958
959#undef FLAG_SET