2 * Copyright (C) 2005 Junio C Hamano
10 /* Table of rename/copy destinations */
12 static struct diff_rename_dst
{
13 struct diff_filespec
*two
;
14 struct diff_filepair
*pair
;
16 static int rename_dst_nr
, rename_dst_alloc
;
18 static struct diff_rename_dst
*locate_rename_dst(struct diff_filespec
*two
,
25 while (last
> first
) {
26 int next
= (last
+ first
) >> 1;
27 struct diff_rename_dst
*dst
= &(rename_dst
[next
]);
28 int cmp
= strcmp(two
->path
, dst
->two
->path
);
40 /* insert to make it at "first" */
41 if (rename_dst_alloc
<= rename_dst_nr
) {
42 rename_dst_alloc
= alloc_nr(rename_dst_alloc
);
43 rename_dst
= xrealloc(rename_dst
,
44 rename_dst_alloc
* sizeof(*rename_dst
));
47 if (first
< rename_dst_nr
)
48 memmove(rename_dst
+ first
+ 1, rename_dst
+ first
,
49 (rename_dst_nr
- first
- 1) * sizeof(*rename_dst
));
50 rename_dst
[first
].two
= alloc_filespec(two
->path
);
51 fill_filespec(rename_dst
[first
].two
, two
->sha1
, two
->sha1_valid
, two
->mode
);
52 rename_dst
[first
].pair
= NULL
;
53 return &(rename_dst
[first
]);
56 /* Table of rename/copy src files */
57 static struct diff_rename_src
{
58 struct diff_filepair
*p
;
59 unsigned short score
; /* to remember the break score */
61 static int rename_src_nr
, rename_src_alloc
;
63 static struct diff_rename_src
*register_rename_src(struct diff_filepair
*p
)
66 struct diff_filespec
*one
= p
->one
;
67 unsigned short score
= p
->score
;
71 while (last
> first
) {
72 int next
= (last
+ first
) >> 1;
73 struct diff_rename_src
*src
= &(rename_src
[next
]);
74 int cmp
= strcmp(one
->path
, src
->p
->one
->path
);
84 /* insert to make it at "first" */
85 if (rename_src_alloc
<= rename_src_nr
) {
86 rename_src_alloc
= alloc_nr(rename_src_alloc
);
87 rename_src
= xrealloc(rename_src
,
88 rename_src_alloc
* sizeof(*rename_src
));
91 if (first
< rename_src_nr
)
92 memmove(rename_src
+ first
+ 1, rename_src
+ first
,
93 (rename_src_nr
- first
- 1) * sizeof(*rename_src
));
94 rename_src
[first
].p
= p
;
95 rename_src
[first
].score
= score
;
96 return &(rename_src
[first
]);
99 static int basename_same(struct diff_filespec
*src
, struct diff_filespec
*dst
)
101 int src_len
= strlen(src
->path
), dst_len
= strlen(dst
->path
);
102 while (src_len
&& dst_len
) {
103 char c1
= src
->path
[--src_len
];
104 char c2
= dst
->path
[--dst_len
];
110 return (!src_len
|| src
->path
[src_len
- 1] == '/') &&
111 (!dst_len
|| dst
->path
[dst_len
- 1] == '/');
115 int src
; /* index in rename_src */
116 int dst
; /* index in rename_dst */
117 unsigned short score
;
121 static int estimate_similarity(struct diff_filespec
*src
,
122 struct diff_filespec
*dst
,
125 /* src points at a file that existed in the original tree (or
126 * optionally a file in the destination tree) and dst points
127 * at a newly created file. They may be quite similar, in which
128 * case we want to say src is renamed to dst or src is copied into
129 * dst, and then some edit has been applied to dst.
131 * Compare them and return how similar they are, representing
132 * the score as an integer between 0 and MAX_SCORE.
134 * When there is an exact match, it is considered a better
135 * match than anything else; the destination does not even
136 * call into this function in that case.
138 unsigned long max_size
, delta_size
, base_size
, src_copied
, literal_added
;
139 unsigned long delta_limit
;
142 /* We deal only with regular files. Symlink renames are handled
143 * only when they are exact matches --- in other words, no edits
146 if (!S_ISREG(src
->mode
) || !S_ISREG(dst
->mode
))
150 * Need to check that source and destination sizes are
151 * filled in before comparing them.
153 * If we already have "cnt_data" filled in, we know it's
154 * all good (avoid checking the size for zero, as that
155 * is a possible size - we really should have a flag to
156 * say whether the size is valid or not!)
158 if (!src
->cnt_data
&& diff_populate_filespec(src
, 1))
160 if (!dst
->cnt_data
&& diff_populate_filespec(dst
, 1))
163 max_size
= ((src
->size
> dst
->size
) ? src
->size
: dst
->size
);
164 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
165 delta_size
= max_size
- base_size
;
167 /* We would not consider edits that change the file size so
168 * drastically. delta_size must be smaller than
169 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
171 * Note that base_size == 0 case is handled here already
172 * and the final score computation below would not have a
173 * divide-by-zero issue.
175 if (max_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
178 if (!src
->cnt_data
&& diff_populate_filespec(src
, 0))
180 if (!dst
->cnt_data
&& diff_populate_filespec(dst
, 0))
183 delta_limit
= (unsigned long)
184 (base_size
* (MAX_SCORE
-minimum_score
) / MAX_SCORE
);
185 if (diffcore_count_changes(src
, dst
,
186 &src
->cnt_data
, &dst
->cnt_data
,
188 &src_copied
, &literal_added
))
191 /* How similar are they?
192 * what percentage of material in dst are from source?
195 score
= 0; /* should not happen */
197 score
= (int)(src_copied
* MAX_SCORE
/ max_size
);
201 static void record_rename_pair(int dst_index
, int src_index
, int score
)
203 struct diff_filespec
*src
, *dst
;
204 struct diff_filepair
*dp
;
206 if (rename_dst
[dst_index
].pair
)
207 die("internal error: dst already matched.");
209 src
= rename_src
[src_index
].p
->one
;
213 dst
= rename_dst
[dst_index
].two
;
216 dp
= diff_queue(NULL
, src
, dst
);
217 dp
->renamed_pair
= 1;
218 if (!strcmp(src
->path
, dst
->path
))
219 dp
->score
= rename_src
[src_index
].score
;
222 rename_dst
[dst_index
].pair
= dp
;
226 * We sort the rename similarity matrix with the score, in descending
227 * order (the most similar first).
229 static int score_compare(const void *a_
, const void *b_
)
231 const struct diff_score
*a
= a_
, *b
= b_
;
233 /* sink the unused ones to the bottom */
235 return (0 <= b
->dst
);
239 if (a
->score
== b
->score
)
240 return b
->name_score
- a
->name_score
;
242 return b
->score
- a
->score
;
245 struct file_similarity
{
247 struct diff_filespec
*filespec
;
248 struct file_similarity
*next
;
251 static int find_identical_files(struct file_similarity
*src
,
252 struct file_similarity
*dst
,
253 struct diff_options
*options
)
258 * Walk over all the destinations ...
261 struct diff_filespec
*target
= dst
->filespec
;
262 struct file_similarity
*p
, *best
;
263 int i
= 100, best_score
= -1;
266 * .. to find the best source match
269 for (p
= src
; p
; p
= p
->next
) {
271 struct diff_filespec
*source
= p
->filespec
;
273 /* False hash collision? */
274 if (hashcmp(source
->sha1
, target
->sha1
))
276 /* Non-regular files? If so, the modes must match! */
277 if (!S_ISREG(source
->mode
) || !S_ISREG(target
->mode
)) {
278 if (source
->mode
!= target
->mode
)
281 /* Give higher scores to sources that haven't been used already */
282 score
= !source
->rename_used
;
283 if (source
->rename_used
&& options
->detect_rename
!= DIFF_DETECT_COPY
)
285 score
+= basename_same(source
, target
);
286 if (score
> best_score
) {
293 /* Too many identical alternatives? Pick one */
298 record_rename_pair(dst
->index
, best
->index
, MAX_SCORE
);
301 } while ((dst
= dst
->next
) != NULL
);
305 static void free_similarity_list(struct file_similarity
*p
)
308 struct file_similarity
*entry
= p
;
314 static int find_same_files(void *ptr
, void *data
)
317 struct file_similarity
*p
= ptr
;
318 struct file_similarity
*src
= NULL
, *dst
= NULL
;
319 struct diff_options
*options
= data
;
321 /* Split the hash list up into sources and destinations */
323 struct file_similarity
*entry
= p
;
325 if (entry
->src_dst
< 0) {
335 * If we have both sources *and* destinations, see if
336 * we can match them up
338 ret
= (src
&& dst
) ? find_identical_files(src
, dst
, options
) : 0;
340 /* Free the hashes and return the number of renames found */
341 free_similarity_list(src
);
342 free_similarity_list(dst
);
346 static unsigned int hash_filespec(struct diff_filespec
*filespec
)
349 if (!filespec
->sha1_valid
) {
350 if (diff_populate_filespec(filespec
, 0))
352 hash_sha1_file(filespec
->data
, filespec
->size
, "blob", filespec
->sha1
);
354 memcpy(&hash
, filespec
->sha1
, sizeof(hash
));
358 static void insert_file_table(struct hash_table
*table
, int src_dst
, int index
, struct diff_filespec
*filespec
)
362 struct file_similarity
*entry
= xmalloc(sizeof(*entry
));
364 entry
->src_dst
= src_dst
;
365 entry
->index
= index
;
366 entry
->filespec
= filespec
;
369 hash
= hash_filespec(filespec
);
370 pos
= insert_hash(hash
, entry
, table
);
372 /* We already had an entry there? */
380 * Find exact renames first.
382 * The first round matches up the up-to-date entries,
383 * and then during the second round we try to match
384 * cache-dirty entries as well.
386 static int find_exact_renames(struct diff_options
*options
)
389 struct hash_table file_table
;
391 init_hash(&file_table
);
392 preallocate_hash(&file_table
, rename_src_nr
+ rename_dst_nr
);
393 for (i
= 0; i
< rename_src_nr
; i
++)
394 insert_file_table(&file_table
, -1, i
, rename_src
[i
].p
->one
);
396 for (i
= 0; i
< rename_dst_nr
; i
++)
397 insert_file_table(&file_table
, 1, i
, rename_dst
[i
].two
);
399 /* Find the renames */
400 i
= for_each_hash(&file_table
, find_same_files
, options
);
402 /* .. and free the hash data structure */
403 free_hash(&file_table
);
408 #define NUM_CANDIDATE_PER_DST 4
409 static void record_if_better(struct diff_score m
[], struct diff_score
*o
)
413 /* find the worst one */
415 for (i
= 1; i
< NUM_CANDIDATE_PER_DST
; i
++)
416 if (score_compare(&m
[i
], &m
[worst
]) > 0)
419 /* is it better than the worst one? */
420 if (score_compare(&m
[worst
], o
) > 0)
426 * 0 if we are under the limit;
427 * 1 if we need to disable inexact rename detection;
428 * 2 if we would be under the limit if we were given -C instead of -C -C.
430 static int too_many_rename_candidates(int num_create
,
431 struct diff_options
*options
)
433 int rename_limit
= options
->rename_limit
;
434 int num_src
= rename_src_nr
;
437 options
->needed_rename_limit
= 0;
440 * This basically does a test for the rename matrix not
441 * growing larger than a "rename_limit" square matrix, ie:
443 * num_create * num_src > rename_limit * rename_limit
445 * but handles the potential overflow case specially (and we
446 * assume at least 32-bit integers)
448 if (rename_limit
<= 0 || rename_limit
> 32767)
449 rename_limit
= 32767;
450 if ((num_create
<= rename_limit
|| num_src
<= rename_limit
) &&
451 (num_create
* num_src
<= rename_limit
* rename_limit
))
454 options
->needed_rename_limit
=
455 num_src
> num_create
? num_src
: num_create
;
457 /* Are we running under -C -C? */
458 if (!DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
461 /* Would we bust the limit if we were running under -C? */
462 for (num_src
= i
= 0; i
< rename_src_nr
; i
++) {
463 if (diff_unmodified_pair(rename_src
[i
].p
))
467 if ((num_create
<= rename_limit
|| num_src
<= rename_limit
) &&
468 (num_create
* num_src
<= rename_limit
* rename_limit
))
473 static int find_renames(struct diff_score
*mx
, int dst_cnt
, int minimum_score
, int copies
)
477 for (i
= 0; i
< dst_cnt
* NUM_CANDIDATE_PER_DST
; i
++) {
478 struct diff_rename_dst
*dst
;
480 if ((mx
[i
].dst
< 0) ||
481 (mx
[i
].score
< minimum_score
))
482 break; /* there is no more usable pair. */
483 dst
= &rename_dst
[mx
[i
].dst
];
485 continue; /* already done, either exact or fuzzy. */
486 if (!copies
&& rename_src
[mx
[i
].src
].p
->one
->rename_used
)
488 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
494 void diffcore_rename(struct diff_options
*options
)
496 int detect_rename
= options
->detect_rename
;
497 int minimum_score
= options
->rename_score
;
498 struct diff_queue_struct
*q
= &diff_queued_diff
;
499 struct diff_queue_struct outq
;
500 struct diff_score
*mx
;
501 int i
, j
, rename_count
, skip_unmodified
= 0;
502 int num_create
, dst_cnt
;
503 struct progress
*progress
= NULL
;
506 minimum_score
= DEFAULT_RENAME_SCORE
;
508 for (i
= 0; i
< q
->nr
; i
++) {
509 struct diff_filepair
*p
= q
->queue
[i
];
510 if (!DIFF_FILE_VALID(p
->one
)) {
511 if (!DIFF_FILE_VALID(p
->two
))
512 continue; /* unmerged */
513 else if (options
->single_follow
&&
514 strcmp(options
->single_follow
, p
->two
->path
))
515 continue; /* not interested */
516 else if (!DIFF_OPT_TST(options
, RENAME_EMPTY
) &&
517 is_empty_blob_sha1(p
->two
->sha1
))
520 locate_rename_dst(p
->two
, 1);
522 else if (!DIFF_OPT_TST(options
, RENAME_EMPTY
) &&
523 is_empty_blob_sha1(p
->one
->sha1
))
525 else if (!DIFF_PAIR_UNMERGED(p
) && !DIFF_FILE_VALID(p
->two
)) {
527 * If the source is a broken "delete", and
528 * they did not really want to get broken,
529 * that means the source actually stays.
530 * So we increment the "rename_used" score
531 * by one, to indicate ourselves as a user
533 if (p
->broken_pair
&& !p
->score
)
534 p
->one
->rename_used
++;
535 register_rename_src(p
);
537 else if (detect_rename
== DIFF_DETECT_COPY
) {
539 * Increment the "rename_used" score by
540 * one, to indicate ourselves as a user.
542 p
->one
->rename_used
++;
543 register_rename_src(p
);
546 if (rename_dst_nr
== 0 || rename_src_nr
== 0)
547 goto cleanup
; /* nothing to do */
550 * We really want to cull the candidates list early
551 * with cheap tests in order to avoid doing deltas.
553 rename_count
= find_exact_renames(options
);
555 /* Did we only want exact renames? */
556 if (minimum_score
== MAX_SCORE
)
560 * Calculate how many renames are left (but all the source
561 * files still remain as options for rename/copies!)
563 num_create
= (rename_dst_nr
- rename_count
);
569 switch (too_many_rename_candidates(num_create
, options
)) {
573 options
->degraded_cc_to_c
= 1;
580 if (options
->show_rename_progress
) {
581 progress
= start_progress_delay(
582 _("Performing inexact rename detection"),
583 rename_dst_nr
* rename_src_nr
, 50, 1);
586 mx
= xcalloc(num_create
* NUM_CANDIDATE_PER_DST
, sizeof(*mx
));
587 for (dst_cnt
= i
= 0; i
< rename_dst_nr
; i
++) {
588 struct diff_filespec
*two
= rename_dst
[i
].two
;
589 struct diff_score
*m
;
591 if (rename_dst
[i
].pair
)
592 continue; /* dealt with exact match already. */
594 m
= &mx
[dst_cnt
* NUM_CANDIDATE_PER_DST
];
595 for (j
= 0; j
< NUM_CANDIDATE_PER_DST
; j
++)
598 for (j
= 0; j
< rename_src_nr
; j
++) {
599 struct diff_filespec
*one
= rename_src
[j
].p
->one
;
600 struct diff_score this_src
;
602 if (skip_unmodified
&&
603 diff_unmodified_pair(rename_src
[j
].p
))
606 this_src
.score
= estimate_similarity(one
, two
,
608 this_src
.name_score
= basename_same(one
, two
);
611 record_if_better(m
, &this_src
);
613 * Once we run estimate_similarity,
614 * We do not need the text anymore.
616 diff_free_filespec_blob(one
);
617 diff_free_filespec_blob(two
);
620 display_progress(progress
, (i
+1)*rename_src_nr
);
622 stop_progress(&progress
);
624 /* cost matrix sorted by most to least similar pair */
625 qsort(mx
, dst_cnt
* NUM_CANDIDATE_PER_DST
, sizeof(*mx
), score_compare
);
627 rename_count
+= find_renames(mx
, dst_cnt
, minimum_score
, 0);
628 if (detect_rename
== DIFF_DETECT_COPY
)
629 rename_count
+= find_renames(mx
, dst_cnt
, minimum_score
, 1);
633 /* At this point, we have found some renames and copies and they
634 * are recorded in rename_dst. The original list is still in *q.
636 DIFF_QUEUE_CLEAR(&outq
);
637 for (i
= 0; i
< q
->nr
; i
++) {
638 struct diff_filepair
*p
= q
->queue
[i
];
639 struct diff_filepair
*pair_to_free
= NULL
;
641 if (DIFF_PAIR_UNMERGED(p
)) {
644 else if (!DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
648 * We would output this create record if it has
649 * not been turned into a rename/copy already.
651 struct diff_rename_dst
*dst
=
652 locate_rename_dst(p
->two
, 0);
653 if (dst
&& dst
->pair
) {
654 diff_q(&outq
, dst
->pair
);
658 /* no matching rename/copy source, so
659 * record this as a creation.
663 else if (DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
)) {
667 * We would output this delete record if:
669 * (1) this is a broken delete and the counterpart
670 * broken create remains in the output; or
671 * (2) this is not a broken delete, and rename_dst
672 * does not have a rename/copy to move p->one->path
675 * Otherwise, the counterpart broken create
676 * has been turned into a rename-edit; or
677 * delete did not have a matching create to
680 if (DIFF_PAIR_BROKEN(p
)) {
682 struct diff_rename_dst
*dst
=
683 locate_rename_dst(p
->one
, 0);
684 if (dst
&& dst
->pair
)
685 /* counterpart is now rename/copy */
689 if (p
->one
->rename_used
)
690 /* this path remains */
699 else if (!diff_unmodified_pair(p
))
700 /* all the usual ones need to be kept */
703 /* no need to keep unmodified pairs */
707 diff_free_filepair(pair_to_free
);
709 diff_debug_queue("done copying original", &outq
);
713 diff_debug_queue("done collapsing", q
);
715 for (i
= 0; i
< rename_dst_nr
; i
++)
716 free_filespec(rename_dst
[i
].two
);
720 rename_dst_nr
= rename_dst_alloc
= 0;
723 rename_src_nr
= rename_src_alloc
= 0;