3 * Copyright (C) 2005 Junio C Hamano
8 #include "object-store.h"
11 #include "promisor-remote.h"
14 /* Table of rename/copy destinations */
16 static struct diff_rename_dst
{
17 struct diff_filepair
*p
;
18 struct diff_filespec
*filespec_to_free
;
19 int is_rename
; /* false -> just a create; true -> rename or copy */
21 static int rename_dst_nr
, rename_dst_alloc
;
22 /* Mapping from break source pathname to break destination index */
23 static struct strintmap
*break_idx
= NULL
;
25 static struct diff_rename_dst
*locate_rename_dst(struct diff_filepair
*p
)
27 /* Lookup by p->ONE->path */
28 int idx
= break_idx
? strintmap_get(break_idx
, p
->one
->path
) : -1;
29 return (idx
== -1) ? NULL
: &rename_dst
[idx
];
33 * Returns 0 on success, -1 if we found a duplicate.
35 static int add_rename_dst(struct diff_filepair
*p
)
37 ALLOC_GROW(rename_dst
, rename_dst_nr
+ 1, rename_dst_alloc
);
38 rename_dst
[rename_dst_nr
].p
= p
;
39 rename_dst
[rename_dst_nr
].filespec_to_free
= NULL
;
40 rename_dst
[rename_dst_nr
].is_rename
= 0;
45 /* Table of rename/copy src files */
46 static struct diff_rename_src
{
47 struct diff_filepair
*p
;
48 unsigned short score
; /* to remember the break score */
50 static int rename_src_nr
, rename_src_alloc
;
52 static void register_rename_src(struct diff_filepair
*p
)
56 break_idx
= xmalloc(sizeof(*break_idx
));
57 strintmap_init(break_idx
, -1);
59 strintmap_set(break_idx
, p
->one
->path
, rename_dst_nr
);
62 ALLOC_GROW(rename_src
, rename_src_nr
+ 1, rename_src_alloc
);
63 rename_src
[rename_src_nr
].p
= p
;
64 rename_src
[rename_src_nr
].score
= p
->score
;
68 static int basename_same(struct diff_filespec
*src
, struct diff_filespec
*dst
)
70 int src_len
= strlen(src
->path
), dst_len
= strlen(dst
->path
);
71 while (src_len
&& dst_len
) {
72 char c1
= src
->path
[--src_len
];
73 char c2
= dst
->path
[--dst_len
];
79 return (!src_len
|| src
->path
[src_len
- 1] == '/') &&
80 (!dst_len
|| dst
->path
[dst_len
- 1] == '/');
84 int src
; /* index in rename_src */
85 int dst
; /* index in rename_dst */
90 struct prefetch_options
{
91 struct repository
*repo
;
94 static void prefetch(void *prefetch_options
)
96 struct prefetch_options
*options
= prefetch_options
;
98 struct oid_array to_fetch
= OID_ARRAY_INIT
;
100 for (i
= 0; i
< rename_dst_nr
; i
++) {
101 if (rename_dst
[i
].p
->renamed_pair
)
103 * The loop in diffcore_rename() will not need these
104 * blobs, so skip prefetching.
106 continue; /* already found exact match */
107 diff_add_if_missing(options
->repo
, &to_fetch
,
108 rename_dst
[i
].p
->two
);
110 for (i
= 0; i
< rename_src_nr
; i
++) {
111 if (options
->skip_unmodified
&&
112 diff_unmodified_pair(rename_src
[i
].p
))
114 * The loop in diffcore_rename() will not need these
115 * blobs, so skip prefetching.
118 diff_add_if_missing(options
->repo
, &to_fetch
,
119 rename_src
[i
].p
->one
);
121 promisor_remote_get_direct(options
->repo
, to_fetch
.oid
, to_fetch
.nr
);
122 oid_array_clear(&to_fetch
);
125 static int estimate_similarity(struct repository
*r
,
126 struct diff_filespec
*src
,
127 struct diff_filespec
*dst
,
131 /* src points at a file that existed in the original tree (or
132 * optionally a file in the destination tree) and dst points
133 * at a newly created file. They may be quite similar, in which
134 * case we want to say src is renamed to dst or src is copied into
135 * dst, and then some edit has been applied to dst.
137 * Compare them and return how similar they are, representing
138 * the score as an integer between 0 and MAX_SCORE.
140 * When there is an exact match, it is considered a better
141 * match than anything else; the destination does not even
142 * call into this function in that case.
144 unsigned long max_size
, delta_size
, base_size
, src_copied
, literal_added
;
146 struct diff_populate_filespec_options dpf_options
= {
149 struct prefetch_options prefetch_options
= {r
, skip_unmodified
};
151 if (r
== the_repository
&& has_promisor_remote()) {
152 dpf_options
.missing_object_cb
= prefetch
;
153 dpf_options
.missing_object_data
= &prefetch_options
;
156 /* We deal only with regular files. Symlink renames are handled
157 * only when they are exact matches --- in other words, no edits
160 if (!S_ISREG(src
->mode
) || !S_ISREG(dst
->mode
))
164 * Need to check that source and destination sizes are
165 * filled in before comparing them.
167 * If we already have "cnt_data" filled in, we know it's
168 * all good (avoid checking the size for zero, as that
169 * is a possible size - we really should have a flag to
170 * say whether the size is valid or not!)
172 if (!src
->cnt_data
&&
173 diff_populate_filespec(r
, src
, &dpf_options
))
175 if (!dst
->cnt_data
&&
176 diff_populate_filespec(r
, dst
, &dpf_options
))
179 max_size
= ((src
->size
> dst
->size
) ? src
->size
: dst
->size
);
180 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
181 delta_size
= max_size
- base_size
;
183 /* We would not consider edits that change the file size so
184 * drastically. delta_size must be smaller than
185 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
187 * Note that base_size == 0 case is handled here already
188 * and the final score computation below would not have a
189 * divide-by-zero issue.
191 if (max_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
194 dpf_options
.check_size_only
= 0;
196 if (!src
->cnt_data
&& diff_populate_filespec(r
, src
, &dpf_options
))
198 if (!dst
->cnt_data
&& diff_populate_filespec(r
, dst
, &dpf_options
))
201 if (diffcore_count_changes(r
, src
, dst
,
202 &src
->cnt_data
, &dst
->cnt_data
,
203 &src_copied
, &literal_added
))
206 /* How similar are they?
207 * what percentage of material in dst are from source?
210 score
= 0; /* should not happen */
212 score
= (int)(src_copied
* MAX_SCORE
/ max_size
);
216 static void record_rename_pair(int dst_index
, int src_index
, int score
)
218 struct diff_filepair
*src
= rename_src
[src_index
].p
;
219 struct diff_filepair
*dst
= rename_dst
[dst_index
].p
;
221 if (dst
->renamed_pair
)
222 die("internal error: dst already matched.");
224 src
->one
->rename_used
++;
227 rename_dst
[dst_index
].filespec_to_free
= dst
->one
;
228 rename_dst
[dst_index
].is_rename
= 1;
231 dst
->renamed_pair
= 1;
232 if (!strcmp(dst
->one
->path
, dst
->two
->path
))
233 dst
->score
= rename_src
[src_index
].score
;
239 * We sort the rename similarity matrix with the score, in descending
240 * order (the most similar first).
242 static int score_compare(const void *a_
, const void *b_
)
244 const struct diff_score
*a
= a_
, *b
= b_
;
246 /* sink the unused ones to the bottom */
248 return (0 <= b
->dst
);
252 if (a
->score
== b
->score
)
253 return b
->name_score
- a
->name_score
;
255 return b
->score
- a
->score
;
258 struct file_similarity
{
259 struct hashmap_entry entry
;
261 struct diff_filespec
*filespec
;
264 static unsigned int hash_filespec(struct repository
*r
,
265 struct diff_filespec
*filespec
)
267 if (!filespec
->oid_valid
) {
268 if (diff_populate_filespec(r
, filespec
, NULL
))
270 hash_object_file(r
->hash_algo
, filespec
->data
, filespec
->size
,
271 "blob", &filespec
->oid
);
273 return oidhash(&filespec
->oid
);
276 static int find_identical_files(struct hashmap
*srcs
,
278 struct diff_options
*options
)
281 struct diff_filespec
*target
= rename_dst
[dst_index
].p
->two
;
282 struct file_similarity
*p
, *best
= NULL
;
283 int i
= 100, best_score
= -1;
284 unsigned int hash
= hash_filespec(options
->repo
, target
);
287 * Find the best source match for specified destination.
289 p
= hashmap_get_entry_from_hash(srcs
, hash
, NULL
,
290 struct file_similarity
, entry
);
291 hashmap_for_each_entry_from(srcs
, p
, entry
) {
293 struct diff_filespec
*source
= p
->filespec
;
295 /* False hash collision? */
296 if (!oideq(&source
->oid
, &target
->oid
))
298 /* Non-regular files? If so, the modes must match! */
299 if (!S_ISREG(source
->mode
) || !S_ISREG(target
->mode
)) {
300 if (source
->mode
!= target
->mode
)
303 /* Give higher scores to sources that haven't been used already */
304 score
= !source
->rename_used
;
305 if (source
->rename_used
&& options
->detect_rename
!= DIFF_DETECT_COPY
)
307 score
+= basename_same(source
, target
);
308 if (score
> best_score
) {
315 /* Too many identical alternatives? Pick one */
320 record_rename_pair(dst_index
, best
->index
, MAX_SCORE
);
326 static void insert_file_table(struct repository
*r
,
327 struct hashmap
*table
, int index
,
328 struct diff_filespec
*filespec
)
330 struct file_similarity
*entry
= xmalloc(sizeof(*entry
));
332 entry
->index
= index
;
333 entry
->filespec
= filespec
;
335 hashmap_entry_init(&entry
->entry
, hash_filespec(r
, filespec
));
336 hashmap_add(table
, &entry
->entry
);
340 * Find exact renames first.
342 * The first round matches up the up-to-date entries,
343 * and then during the second round we try to match
344 * cache-dirty entries as well.
346 static int find_exact_renames(struct diff_options
*options
)
349 struct hashmap file_table
;
351 /* Add all sources to the hash table in reverse order, because
352 * later on they will be retrieved in LIFO order.
354 hashmap_init(&file_table
, NULL
, NULL
, rename_src_nr
);
355 for (i
= rename_src_nr
-1; i
>= 0; i
--)
356 insert_file_table(options
->repo
,
358 rename_src
[i
].p
->one
);
360 /* Walk the destinations and find best source match */
361 for (i
= 0; i
< rename_dst_nr
; i
++)
362 renames
+= find_identical_files(&file_table
, i
, options
);
364 /* Free the hash data structure and entries */
365 hashmap_clear_and_free(&file_table
, struct file_similarity
, entry
);
370 struct dir_rename_info
{
371 struct strintmap idx_map
;
372 struct strmap dir_rename_guess
;
373 struct strmap
*dir_rename_count
;
377 static char *get_dirname(const char *filename
)
379 char *slash
= strrchr(filename
, '/');
380 return slash
? xstrndup(filename
, slash
- filename
) : xstrdup("");
383 static const char *get_basename(const char *filename
)
386 * gitbasename() has to worry about special drives, multiple
387 * directory separator characters, trailing slashes, NULL or
388 * empty strings, etc. We only work on filenames as stored in
389 * git, and thus get to ignore all those complications.
391 const char *base
= strrchr(filename
, '/');
392 return base
? base
+ 1 : filename
;
395 static int idx_possible_rename(char *filename
, struct dir_rename_info
*info
)
398 * Our comparison of files with the same basename (see
399 * find_basename_matches() below), is only helpful when after exact
400 * rename detection we have exactly one file with a given basename
401 * among the rename sources and also only exactly one file with
402 * that basename among the rename destinations. When we have
403 * multiple files with the same basename in either set, we do not
404 * know which to compare against. However, there are some
405 * filenames that occur in large numbers (particularly
406 * build-related filenames such as 'Makefile', '.gitignore', or
407 * 'build.gradle' that potentially exist within every single
408 * subdirectory), and for performance we want to be able to quickly
409 * find renames for these files too.
411 * The reason basename comparisons are a useful heuristic was that it
412 * is common for people to move files across directories while keeping
413 * their filename the same. If we had a way of determining or even
414 * making a good educated guess about which directory these non-unique
415 * basename files had moved the file to, we could check it.
418 * When an entire directory is in fact renamed, we have two factors
420 * (a) the original directory disappeared giving us a hint
421 * about when we can apply an extra heuristic.
422 * (a) we often have several files within that directory and
423 * subdirectories that are renamed without changes
424 * So, rules for a heuristic:
425 * (0) If there basename matches are non-unique (the condition under
426 * which this function is called) AND
427 * (1) the directory in which the file was found has disappeared
428 * (i.e. dirs_removed is non-NULL and has a relevant entry) THEN
429 * (2) use exact renames of files within the directory to determine
430 * where the directory is likely to have been renamed to. IF
431 * there is at least one exact rename from within that
432 * directory, we can proceed.
433 * (3) If there are multiple places the directory could have been
434 * renamed to based on exact renames, ignore all but one of them.
435 * Just use the destination with the most renames going to it.
436 * (4) Check if applying that directory rename to the original file
437 * would result in a destination filename that is in the
438 * potential rename set. If so, return the index of the
439 * destination file (the index within rename_dst).
440 * (5) Compare the original file and returned destination for
441 * similarity, and if they are sufficiently similar, record the
444 * This function, idx_possible_rename(), is only responsible for (4).
445 * The conditions/steps in (1)-(3) will be handled via setting up
446 * dir_rename_count and dir_rename_guess in a future
447 * initialize_dir_rename_info() function. Steps (0) and (5) are
448 * handled by the caller of this function.
450 char *old_dir
, *new_dir
;
451 struct strbuf new_path
= STRBUF_INIT
;
457 old_dir
= get_dirname(filename
);
458 new_dir
= strmap_get(&info
->dir_rename_guess
, old_dir
);
463 strbuf_addstr(&new_path
, new_dir
);
464 strbuf_addch(&new_path
, '/');
465 strbuf_addstr(&new_path
, get_basename(filename
));
467 idx
= strintmap_get(&info
->idx_map
, new_path
.buf
);
468 strbuf_release(&new_path
);
472 static int find_basename_matches(struct diff_options
*options
,
474 struct dir_rename_info
*info
)
477 * When I checked in early 2020, over 76% of file renames in linux
478 * just moved files to a different directory but kept the same
479 * basename. gcc did that with over 64% of renames, gecko did it
480 * with over 79%, and WebKit did it with over 89%.
482 * Therefore we can bypass the normal exhaustive NxM matrix
483 * comparison of similarities between all potential rename sources
484 * and destinations by instead using file basename as a hint (i.e.
485 * the portion of the filename after the last '/'), checking for
486 * similarity between files with the same basename, and if we find
487 * a pair that are sufficiently similar, record the rename pair and
488 * exclude those two from the NxM matrix.
490 * This *might* cause us to find a less than optimal pairing (if
491 * there is another file that we are even more similar to but has a
492 * different basename). Given the huge performance advantage
493 * basename matching provides, and given the frequency with which
494 * people use the same basename in real world projects, that's a
495 * trade-off we are willing to accept when doing just rename
498 * If someone wants copy detection that implies they are willing to
499 * spend more cycles to find similarities between files, so it may
500 * be less likely that this heuristic is wanted. If someone is
501 * doing break detection, that means they do not want filename
502 * similarity to imply any form of content similiarity, and thus
503 * this heuristic would definitely be incompatible.
507 struct strintmap sources
;
508 struct strintmap dests
;
511 * The prefeteching stuff wants to know if it can skip prefetching
512 * blobs that are unmodified...and will then do a little extra work
513 * to verify that the oids are indeed different before prefetching.
514 * Unmodified blobs are only relevant when doing copy detection;
515 * when limiting to rename detection, diffcore_rename[_extended]()
516 * will never be called with unmodified source paths fed to us, so
517 * the extra work necessary to check if rename_src entries are
518 * unmodified would be a small waste.
520 int skip_unmodified
= 0;
523 * Create maps of basename -> fullname(s) for remaining sources and
526 strintmap_init_with_options(&sources
, -1, NULL
, 0);
527 strintmap_init_with_options(&dests
, -1, NULL
, 0);
528 for (i
= 0; i
< rename_src_nr
; ++i
) {
529 char *filename
= rename_src
[i
].p
->one
->path
;
532 /* exact renames removed in remove_unneeded_paths_from_src() */
533 assert(!rename_src
[i
].p
->one
->rename_used
);
535 /* Record index within rename_src (i) if basename is unique */
536 base
= get_basename(filename
);
537 if (strintmap_contains(&sources
, base
))
538 strintmap_set(&sources
, base
, -1);
540 strintmap_set(&sources
, base
, i
);
542 for (i
= 0; i
< rename_dst_nr
; ++i
) {
543 char *filename
= rename_dst
[i
].p
->two
->path
;
546 if (rename_dst
[i
].is_rename
)
547 continue; /* involved in exact match already. */
549 /* Record index within rename_dst (i) if basename is unique */
550 base
= get_basename(filename
);
551 if (strintmap_contains(&dests
, base
))
552 strintmap_set(&dests
, base
, -1);
554 strintmap_set(&dests
, base
, i
);
557 /* Now look for basename matchups and do similarity estimation */
558 for (i
= 0; i
< rename_src_nr
; ++i
) {
559 char *filename
= rename_src
[i
].p
->one
->path
;
560 const char *base
= NULL
;
565 * If the basename is unique among remaining sources, then
566 * src_index will equal 'i' and we can attempt to match it
567 * to a unique basename in the destinations. Otherwise,
568 * use directory rename heuristics, if possible.
570 base
= get_basename(filename
);
571 src_index
= strintmap_get(&sources
, base
);
572 assert(src_index
== -1 || src_index
== i
);
574 if (strintmap_contains(&dests
, base
)) {
575 struct diff_filespec
*one
, *two
;
578 /* Find a matching destination, if possible */
579 dst_index
= strintmap_get(&dests
, base
);
580 if (src_index
== -1 || dst_index
== -1) {
582 dst_index
= idx_possible_rename(filename
, info
);
587 /* Ignore this dest if already used in a rename */
588 if (rename_dst
[dst_index
].is_rename
)
589 continue; /* already used previously */
591 /* Estimate the similarity */
592 one
= rename_src
[src_index
].p
->one
;
593 two
= rename_dst
[dst_index
].p
->two
;
594 score
= estimate_similarity(options
->repo
, one
, two
,
595 minimum_score
, skip_unmodified
);
597 /* If sufficiently similar, record as rename pair */
598 if (score
< minimum_score
)
600 record_rename_pair(dst_index
, src_index
, score
);
604 * Found a rename so don't need text anymore; if we
605 * didn't find a rename, the filespec_blob would get
606 * re-used when doing the matrix of comparisons.
608 diff_free_filespec_blob(one
);
609 diff_free_filespec_blob(two
);
613 strintmap_clear(&sources
);
614 strintmap_clear(&dests
);
619 #define NUM_CANDIDATE_PER_DST 4
620 static void record_if_better(struct diff_score m
[], struct diff_score
*o
)
624 /* find the worst one */
626 for (i
= 1; i
< NUM_CANDIDATE_PER_DST
; i
++)
627 if (score_compare(&m
[i
], &m
[worst
]) > 0)
630 /* is it better than the worst one? */
631 if (score_compare(&m
[worst
], o
) > 0)
637 * 0 if we are under the limit;
638 * 1 if we need to disable inexact rename detection;
639 * 2 if we would be under the limit if we were given -C instead of -C -C.
641 static int too_many_rename_candidates(int num_destinations
, int num_sources
,
642 struct diff_options
*options
)
644 int rename_limit
= options
->rename_limit
;
645 int i
, limited_sources
;
647 options
->needed_rename_limit
= 0;
650 * This basically does a test for the rename matrix not
651 * growing larger than a "rename_limit" square matrix, ie:
653 * num_destinations * num_sources > rename_limit * rename_limit
655 * We use st_mult() to check overflow conditions; in the
656 * exceptional circumstance that size_t isn't large enough to hold
657 * the multiplication, the system won't be able to allocate enough
658 * memory for the matrix anyway.
660 if (rename_limit
<= 0)
661 rename_limit
= 32767;
662 if (st_mult(num_destinations
, num_sources
)
663 <= st_mult(rename_limit
, rename_limit
))
666 options
->needed_rename_limit
=
667 num_sources
> num_destinations
? num_sources
: num_destinations
;
669 /* Are we running under -C -C? */
670 if (!options
->flags
.find_copies_harder
)
673 /* Would we bust the limit if we were running under -C? */
674 for (limited_sources
= i
= 0; i
< num_sources
; i
++) {
675 if (diff_unmodified_pair(rename_src
[i
].p
))
679 if (st_mult(num_destinations
, limited_sources
)
680 <= st_mult(rename_limit
, rename_limit
))
685 static int find_renames(struct diff_score
*mx
, int dst_cnt
, int minimum_score
, int copies
)
689 for (i
= 0; i
< dst_cnt
* NUM_CANDIDATE_PER_DST
; i
++) {
690 struct diff_rename_dst
*dst
;
692 if ((mx
[i
].dst
< 0) ||
693 (mx
[i
].score
< minimum_score
))
694 break; /* there is no more usable pair. */
695 dst
= &rename_dst
[mx
[i
].dst
];
697 continue; /* already done, either exact or fuzzy. */
698 if (!copies
&& rename_src
[mx
[i
].src
].p
->one
->rename_used
)
700 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
706 static void remove_unneeded_paths_from_src(int detecting_copies
)
710 if (detecting_copies
)
711 return; /* nothing to remove */
713 return; /* culling incompatible with break detection */
716 * Note on reasons why we cull unneeded sources but not destinations:
717 * 1) Pairings are stored in rename_dst (not rename_src), which we
718 * need to keep around. So, we just can't cull rename_dst even
719 * if we wanted to. But doing so wouldn't help because...
721 * 2) There is a matrix pairwise comparison that follows the
722 * "Performing inexact rename detection" progress message.
723 * Iterating over the destinations is done in the outer loop,
724 * hence we only iterate over each of those once and we can
725 * easily skip the outer loop early if the destination isn't
726 * relevant. That's only one check per destination path to
729 * By contrast, the sources are iterated in the inner loop; if
730 * we check whether a source can be skipped, then we'll be
731 * checking it N separate times, once for each destination.
732 * We don't want to have to iterate over known-not-needed
733 * sources N times each, so avoid that by removing the sources
734 * from rename_src here.
736 for (i
= 0, new_num_src
= 0; i
< rename_src_nr
; i
++) {
738 * renames are stored in rename_dst, so if a rename has
739 * already been detected using this source, we can just
740 * remove the source knowing rename_dst has its info.
742 if (rename_src
[i
].p
->one
->rename_used
)
746 memcpy(&rename_src
[new_num_src
], &rename_src
[i
],
747 sizeof(struct diff_rename_src
));
751 rename_src_nr
= new_num_src
;
754 void diffcore_rename(struct diff_options
*options
)
756 int detect_rename
= options
->detect_rename
;
757 int minimum_score
= options
->rename_score
;
758 struct diff_queue_struct
*q
= &diff_queued_diff
;
759 struct diff_queue_struct outq
;
760 struct diff_score
*mx
;
761 int i
, j
, rename_count
, skip_unmodified
= 0;
762 int num_destinations
, dst_cnt
;
763 int num_sources
, want_copies
;
764 struct progress
*progress
= NULL
;
765 struct dir_rename_info info
;
767 trace2_region_enter("diff", "setup", options
->repo
);
769 want_copies
= (detect_rename
== DIFF_DETECT_COPY
);
771 minimum_score
= DEFAULT_RENAME_SCORE
;
773 for (i
= 0; i
< q
->nr
; i
++) {
774 struct diff_filepair
*p
= q
->queue
[i
];
775 if (!DIFF_FILE_VALID(p
->one
)) {
776 if (!DIFF_FILE_VALID(p
->two
))
777 continue; /* unmerged */
778 else if (options
->single_follow
&&
779 strcmp(options
->single_follow
, p
->two
->path
))
780 continue; /* not interested */
781 else if (!options
->flags
.rename_empty
&&
782 is_empty_blob_oid(&p
->two
->oid
))
784 else if (add_rename_dst(p
) < 0) {
785 warning("skipping rename detection, detected"
786 " duplicate destination '%s'",
791 else if (!options
->flags
.rename_empty
&&
792 is_empty_blob_oid(&p
->one
->oid
))
794 else if (!DIFF_PAIR_UNMERGED(p
) && !DIFF_FILE_VALID(p
->two
)) {
796 * If the source is a broken "delete", and
797 * they did not really want to get broken,
798 * that means the source actually stays.
799 * So we increment the "rename_used" score
800 * by one, to indicate ourselves as a user
802 if (p
->broken_pair
&& !p
->score
)
803 p
->one
->rename_used
++;
804 register_rename_src(p
);
806 else if (want_copies
) {
808 * Increment the "rename_used" score by
809 * one, to indicate ourselves as a user.
811 p
->one
->rename_used
++;
812 register_rename_src(p
);
815 trace2_region_leave("diff", "setup", options
->repo
);
816 if (rename_dst_nr
== 0 || rename_src_nr
== 0)
817 goto cleanup
; /* nothing to do */
819 trace2_region_enter("diff", "exact renames", options
->repo
);
821 * We really want to cull the candidates list early
822 * with cheap tests in order to avoid doing deltas.
824 rename_count
= find_exact_renames(options
);
825 trace2_region_leave("diff", "exact renames", options
->repo
);
827 /* Did we only want exact renames? */
828 if (minimum_score
== MAX_SCORE
)
831 num_sources
= rename_src_nr
;
833 if (want_copies
|| break_idx
) {
836 * - remove ones corresponding to exact renames
838 trace2_region_enter("diff", "cull after exact", options
->repo
);
839 remove_unneeded_paths_from_src(want_copies
);
840 trace2_region_leave("diff", "cull after exact", options
->repo
);
842 /* Determine minimum score to match basenames */
844 char *basename_factor
= getenv("GIT_BASENAME_FACTOR");
845 int min_basename_score
;
848 factor
= strtol(basename_factor
, NULL
, 10)/100.0;
849 assert(factor
>= 0.0 && factor
<= 1.0);
850 min_basename_score
= minimum_score
+
851 (int)(factor
* (MAX_SCORE
- minimum_score
));
855 * - remove ones involved in renames (found via exact match)
857 trace2_region_enter("diff", "cull after exact", options
->repo
);
858 remove_unneeded_paths_from_src(want_copies
);
859 trace2_region_leave("diff", "cull after exact", options
->repo
);
861 /* Utilize file basenames to quickly find renames. */
862 trace2_region_enter("diff", "basename matches", options
->repo
);
863 rename_count
+= find_basename_matches(options
,
866 trace2_region_leave("diff", "basename matches", options
->repo
);
869 * Cull sources, again:
870 * - remove ones involved in renames (found via basenames)
872 trace2_region_enter("diff", "cull basename", options
->repo
);
873 remove_unneeded_paths_from_src(want_copies
);
874 trace2_region_leave("diff", "cull basename", options
->repo
);
877 /* Calculate how many rename destinations are left */
878 num_destinations
= (rename_dst_nr
- rename_count
);
879 num_sources
= rename_src_nr
; /* rename_src_nr reflects lower number */
882 if (!num_destinations
|| !num_sources
)
885 switch (too_many_rename_candidates(num_destinations
, num_sources
,
890 options
->degraded_cc_to_c
= 1;
897 trace2_region_enter("diff", "inexact renames", options
->repo
);
898 if (options
->show_rename_progress
) {
899 progress
= start_delayed_progress(
900 _("Performing inexact rename detection"),
901 (uint64_t)num_destinations
* (uint64_t)num_sources
);
904 mx
= xcalloc(st_mult(NUM_CANDIDATE_PER_DST
, num_destinations
),
906 for (dst_cnt
= i
= 0; i
< rename_dst_nr
; i
++) {
907 struct diff_filespec
*two
= rename_dst
[i
].p
->two
;
908 struct diff_score
*m
;
910 if (rename_dst
[i
].is_rename
)
911 continue; /* exact or basename match already handled */
913 m
= &mx
[dst_cnt
* NUM_CANDIDATE_PER_DST
];
914 for (j
= 0; j
< NUM_CANDIDATE_PER_DST
; j
++)
917 for (j
= 0; j
< rename_src_nr
; j
++) {
918 struct diff_filespec
*one
= rename_src
[j
].p
->one
;
919 struct diff_score this_src
;
921 assert(!one
->rename_used
|| want_copies
|| break_idx
);
923 if (skip_unmodified
&&
924 diff_unmodified_pair(rename_src
[j
].p
))
927 this_src
.score
= estimate_similarity(options
->repo
,
931 this_src
.name_score
= basename_same(one
, two
);
934 record_if_better(m
, &this_src
);
936 * Once we run estimate_similarity,
937 * We do not need the text anymore.
939 diff_free_filespec_blob(one
);
940 diff_free_filespec_blob(two
);
943 display_progress(progress
,
944 (uint64_t)dst_cnt
* (uint64_t)num_sources
);
946 stop_progress(&progress
);
948 /* cost matrix sorted by most to least similar pair */
949 STABLE_QSORT(mx
, dst_cnt
* NUM_CANDIDATE_PER_DST
, score_compare
);
951 rename_count
+= find_renames(mx
, dst_cnt
, minimum_score
, 0);
953 rename_count
+= find_renames(mx
, dst_cnt
, minimum_score
, 1);
955 trace2_region_leave("diff", "inexact renames", options
->repo
);
958 /* At this point, we have found some renames and copies and they
959 * are recorded in rename_dst. The original list is still in *q.
961 trace2_region_enter("diff", "write back to queue", options
->repo
);
962 DIFF_QUEUE_CLEAR(&outq
);
963 for (i
= 0; i
< q
->nr
; i
++) {
964 struct diff_filepair
*p
= q
->queue
[i
];
965 struct diff_filepair
*pair_to_free
= NULL
;
967 if (DIFF_PAIR_UNMERGED(p
)) {
970 else if (!DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
974 else if (DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
)) {
978 * We would output this delete record if:
980 * (1) this is a broken delete and the counterpart
981 * broken create remains in the output; or
982 * (2) this is not a broken delete, and rename_dst
983 * does not have a rename/copy to move p->one->path
986 * Otherwise, the counterpart broken create
987 * has been turned into a rename-edit; or
988 * delete did not have a matching create to
991 if (DIFF_PAIR_BROKEN(p
)) {
993 struct diff_rename_dst
*dst
= locate_rename_dst(p
);
995 BUG("tracking failed somehow; failed to find associated dst for broken pair");
997 /* counterpart is now rename/copy */
1001 if (p
->one
->rename_used
)
1002 /* this path remains */
1009 else if (!diff_unmodified_pair(p
))
1010 /* all the usual ones need to be kept */
1013 /* no need to keep unmodified pairs; FIXME: remove earlier? */
1017 diff_free_filepair(pair_to_free
);
1019 diff_debug_queue("done copying original", &outq
);
1023 diff_debug_queue("done collapsing", q
);
1025 for (i
= 0; i
< rename_dst_nr
; i
++)
1026 if (rename_dst
[i
].filespec_to_free
)
1027 free_filespec(rename_dst
[i
].filespec_to_free
);
1029 FREE_AND_NULL(rename_dst
);
1030 rename_dst_nr
= rename_dst_alloc
= 0;
1031 FREE_AND_NULL(rename_src
);
1032 rename_src_nr
= rename_src_alloc
= 0;
1034 strintmap_clear(break_idx
);
1035 FREE_AND_NULL(break_idx
);
1037 trace2_region_leave("diff", "write back to queue", options
->repo
);