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
->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_filespec
*one
;
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_filespec
*one
,
70 while (last
> first
) {
71 int next
= (last
+ first
) >> 1;
72 struct diff_rename_src
*src
= &(rename_src
[next
]);
73 int cmp
= strcmp(one
->path
, src
->one
->path
);
83 /* insert to make it at "first" */
84 if (rename_src_alloc
<= rename_src_nr
) {
85 rename_src_alloc
= alloc_nr(rename_src_alloc
);
86 rename_src
= xrealloc(rename_src
,
87 rename_src_alloc
* sizeof(*rename_src
));
90 if (first
< rename_src_nr
)
91 memmove(rename_src
+ first
+ 1, rename_src
+ first
,
92 (rename_src_nr
- first
- 1) * sizeof(*rename_src
));
93 rename_src
[first
].one
= one
;
94 rename_src
[first
].score
= score
;
95 return &(rename_src
[first
]);
98 static int basename_same(struct diff_filespec
*src
, struct diff_filespec
*dst
)
100 int src_len
= strlen(src
->path
), dst_len
= strlen(dst
->path
);
101 while (src_len
&& dst_len
) {
102 char c1
= src
->path
[--src_len
];
103 char c2
= dst
->path
[--dst_len
];
109 return (!src_len
|| src
->path
[src_len
- 1] == '/') &&
110 (!dst_len
|| dst
->path
[dst_len
- 1] == '/');
114 int src
; /* index in rename_src */
115 int dst
; /* index in rename_dst */
116 unsigned short score
;
120 static int estimate_similarity(struct diff_filespec
*src
,
121 struct diff_filespec
*dst
,
124 /* src points at a file that existed in the original tree (or
125 * optionally a file in the destination tree) and dst points
126 * at a newly created file. They may be quite similar, in which
127 * case we want to say src is renamed to dst or src is copied into
128 * dst, and then some edit has been applied to dst.
130 * Compare them and return how similar they are, representing
131 * the score as an integer between 0 and MAX_SCORE.
133 * When there is an exact match, it is considered a better
134 * match than anything else; the destination does not even
135 * call into this function in that case.
137 unsigned long max_size
, delta_size
, base_size
, src_copied
, literal_added
;
138 unsigned long delta_limit
;
141 /* We deal only with regular files. Symlink renames are handled
142 * only when they are exact matches --- in other words, no edits
145 if (!S_ISREG(src
->mode
) || !S_ISREG(dst
->mode
))
149 * Need to check that source and destination sizes are
150 * filled in before comparing them.
152 * If we already have "cnt_data" filled in, we know it's
153 * all good (avoid checking the size for zero, as that
154 * is a possible size - we really should have a flag to
155 * say whether the size is valid or not!)
157 if (!src
->cnt_data
&& diff_populate_filespec(src
, 1))
159 if (!dst
->cnt_data
&& diff_populate_filespec(dst
, 1))
162 max_size
= ((src
->size
> dst
->size
) ? src
->size
: dst
->size
);
163 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
164 delta_size
= max_size
- base_size
;
166 /* We would not consider edits that change the file size so
167 * drastically. delta_size must be smaller than
168 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
170 * Note that base_size == 0 case is handled here already
171 * and the final score computation below would not have a
172 * divide-by-zero issue.
174 if (base_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
177 if (!src
->cnt_data
&& diff_populate_filespec(src
, 0))
179 if (!dst
->cnt_data
&& diff_populate_filespec(dst
, 0))
182 delta_limit
= (unsigned long)
183 (base_size
* (MAX_SCORE
-minimum_score
) / MAX_SCORE
);
184 if (diffcore_count_changes(src
, dst
,
185 &src
->cnt_data
, &dst
->cnt_data
,
187 &src_copied
, &literal_added
))
190 /* How similar are they?
191 * what percentage of material in dst are from source?
194 score
= 0; /* should not happen */
196 score
= (int)(src_copied
* MAX_SCORE
/ max_size
);
200 static void record_rename_pair(int dst_index
, int src_index
, int score
)
202 struct diff_filespec
*src
, *dst
;
203 struct diff_filepair
*dp
;
205 if (rename_dst
[dst_index
].pair
)
206 die("internal error: dst already matched.");
208 src
= rename_src
[src_index
].one
;
212 dst
= rename_dst
[dst_index
].two
;
215 dp
= diff_queue(NULL
, src
, dst
);
216 dp
->renamed_pair
= 1;
217 if (!strcmp(src
->path
, dst
->path
))
218 dp
->score
= rename_src
[src_index
].score
;
221 rename_dst
[dst_index
].pair
= dp
;
225 * We sort the rename similarity matrix with the score, in descending
226 * order (the most similar first).
228 static int score_compare(const void *a_
, const void *b_
)
230 const struct diff_score
*a
= a_
, *b
= b_
;
232 /* sink the unused ones to the bottom */
234 return (0 <= b
->dst
);
238 if (a
->score
== b
->score
)
239 return b
->name_score
- a
->name_score
;
241 return b
->score
- a
->score
;
244 struct file_similarity
{
246 struct diff_filespec
*filespec
;
247 struct file_similarity
*next
;
250 static int find_identical_files(struct file_similarity
*src
,
251 struct file_similarity
*dst
)
256 * Walk over all the destinations ...
259 struct diff_filespec
*target
= dst
->filespec
;
260 struct file_similarity
*p
, *best
;
261 int i
= 100, best_score
= -1;
264 * .. to find the best source match
267 for (p
= src
; p
; p
= p
->next
) {
269 struct diff_filespec
*source
= p
->filespec
;
271 /* False hash collision? */
272 if (hashcmp(source
->sha1
, target
->sha1
))
274 /* Non-regular files? If so, the modes must match! */
275 if (!S_ISREG(source
->mode
) || !S_ISREG(target
->mode
)) {
276 if (source
->mode
!= target
->mode
)
279 /* Give higher scores to sources that haven't been used already */
280 score
= !source
->rename_used
;
281 score
+= basename_same(source
, target
);
282 if (score
> best_score
) {
289 /* Too many identical alternatives? Pick one */
294 record_rename_pair(dst
->index
, best
->index
, MAX_SCORE
);
297 } while ((dst
= dst
->next
) != NULL
);
301 static void free_similarity_list(struct file_similarity
*p
)
304 struct file_similarity
*entry
= p
;
310 static int find_same_files(void *ptr
)
313 struct file_similarity
*p
= ptr
;
314 struct file_similarity
*src
= NULL
, *dst
= NULL
;
316 /* Split the hash list up into sources and destinations */
318 struct file_similarity
*entry
= p
;
320 if (entry
->src_dst
< 0) {
330 * If we have both sources *and* destinations, see if
331 * we can match them up
333 ret
= (src
&& dst
) ? find_identical_files(src
, dst
) : 0;
335 /* Free the hashes and return the number of renames found */
336 free_similarity_list(src
);
337 free_similarity_list(dst
);
341 static unsigned int hash_filespec(struct diff_filespec
*filespec
)
344 if (!filespec
->sha1_valid
) {
345 if (diff_populate_filespec(filespec
, 0))
347 hash_sha1_file(filespec
->data
, filespec
->size
, "blob", filespec
->sha1
);
349 memcpy(&hash
, filespec
->sha1
, sizeof(hash
));
353 static void insert_file_table(struct hash_table
*table
, int src_dst
, int index
, struct diff_filespec
*filespec
)
357 struct file_similarity
*entry
= xmalloc(sizeof(*entry
));
359 entry
->src_dst
= src_dst
;
360 entry
->index
= index
;
361 entry
->filespec
= filespec
;
364 hash
= hash_filespec(filespec
);
365 pos
= insert_hash(hash
, entry
, table
);
367 /* We already had an entry there? */
375 * Find exact renames first.
377 * The first round matches up the up-to-date entries,
378 * and then during the second round we try to match
379 * cache-dirty entries as well.
381 static int find_exact_renames(void)
384 struct hash_table file_table
;
386 init_hash(&file_table
);
387 for (i
= 0; i
< rename_src_nr
; i
++)
388 insert_file_table(&file_table
, -1, i
, rename_src
[i
].one
);
390 for (i
= 0; i
< rename_dst_nr
; i
++)
391 insert_file_table(&file_table
, 1, i
, rename_dst
[i
].two
);
393 /* Find the renames */
394 i
= for_each_hash(&file_table
, find_same_files
);
396 /* .. and free the hash data structure */
397 free_hash(&file_table
);
402 #define NUM_CANDIDATE_PER_DST 4
403 static void record_if_better(struct diff_score m
[], struct diff_score
*o
)
407 /* find the worst one */
409 for (i
= 1; i
< NUM_CANDIDATE_PER_DST
; i
++)
410 if (score_compare(&m
[i
], &m
[worst
]) > 0)
413 /* is it better than the worst one? */
414 if (score_compare(&m
[worst
], o
) > 0)
418 void diffcore_rename(struct diff_options
*options
)
420 int detect_rename
= options
->detect_rename
;
421 int minimum_score
= options
->rename_score
;
422 int rename_limit
= options
->rename_limit
;
423 struct diff_queue_struct
*q
= &diff_queued_diff
;
424 struct diff_queue_struct outq
;
425 struct diff_score
*mx
;
426 int i
, j
, rename_count
;
427 int num_create
, num_src
, dst_cnt
;
428 struct progress
*progress
= NULL
;
431 minimum_score
= DEFAULT_RENAME_SCORE
;
433 for (i
= 0; i
< q
->nr
; i
++) {
434 struct diff_filepair
*p
= q
->queue
[i
];
435 if (!DIFF_FILE_VALID(p
->one
)) {
436 if (!DIFF_FILE_VALID(p
->two
))
437 continue; /* unmerged */
438 else if (options
->single_follow
&&
439 strcmp(options
->single_follow
, p
->two
->path
))
440 continue; /* not interested */
442 locate_rename_dst(p
->two
, 1);
444 else if (!DIFF_FILE_VALID(p
->two
)) {
446 * If the source is a broken "delete", and
447 * they did not really want to get broken,
448 * that means the source actually stays.
449 * So we increment the "rename_used" score
450 * by one, to indicate ourselves as a user
452 if (p
->broken_pair
&& !p
->score
)
453 p
->one
->rename_used
++;
454 register_rename_src(p
->one
, p
->score
);
456 else if (detect_rename
== DIFF_DETECT_COPY
) {
458 * Increment the "rename_used" score by
459 * one, to indicate ourselves as a user.
461 p
->one
->rename_used
++;
462 register_rename_src(p
->one
, p
->score
);
465 if (rename_dst_nr
== 0 || rename_src_nr
== 0)
466 goto cleanup
; /* nothing to do */
469 * We really want to cull the candidates list early
470 * with cheap tests in order to avoid doing deltas.
472 rename_count
= find_exact_renames();
474 /* Did we only want exact renames? */
475 if (minimum_score
== MAX_SCORE
)
479 * Calculate how many renames are left (but all the source
480 * files still remain as options for rename/copies!)
482 num_create
= (rename_dst_nr
- rename_count
);
483 num_src
= rename_src_nr
;
490 * This basically does a test for the rename matrix not
491 * growing larger than a "rename_limit" square matrix, ie:
493 * num_create * num_src > rename_limit * rename_limit
495 * but handles the potential overflow case specially (and we
496 * assume at least 32-bit integers)
498 options
->needed_rename_limit
= 0;
499 if (rename_limit
<= 0 || rename_limit
> 32767)
500 rename_limit
= 32767;
501 if ((num_create
> rename_limit
&& num_src
> rename_limit
) ||
502 (num_create
* num_src
> rename_limit
* rename_limit
)) {
503 options
->needed_rename_limit
=
504 num_src
> num_create
? num_src
: num_create
;
508 if (options
->show_rename_progress
) {
509 progress
= start_progress_delay(
510 "Performing inexact rename detection",
511 rename_dst_nr
* rename_src_nr
, 50, 1);
514 mx
= xcalloc(num_create
* NUM_CANDIDATE_PER_DST
, sizeof(*mx
));
515 for (dst_cnt
= i
= 0; i
< rename_dst_nr
; i
++) {
516 struct diff_filespec
*two
= rename_dst
[i
].two
;
517 struct diff_score
*m
;
519 if (rename_dst
[i
].pair
)
520 continue; /* dealt with exact match already. */
522 m
= &mx
[dst_cnt
* NUM_CANDIDATE_PER_DST
];
523 for (j
= 0; j
< NUM_CANDIDATE_PER_DST
; j
++)
526 for (j
= 0; j
< rename_src_nr
; j
++) {
527 struct diff_filespec
*one
= rename_src
[j
].one
;
528 struct diff_score this_src
;
529 this_src
.score
= estimate_similarity(one
, two
,
531 this_src
.name_score
= basename_same(one
, two
);
534 record_if_better(m
, &this_src
);
536 * Once we run estimate_similarity,
537 * We do not need the text anymore.
539 diff_free_filespec_blob(one
);
540 diff_free_filespec_blob(two
);
543 display_progress(progress
, (i
+1)*rename_src_nr
);
545 stop_progress(&progress
);
547 /* cost matrix sorted by most to least similar pair */
548 qsort(mx
, dst_cnt
* NUM_CANDIDATE_PER_DST
, sizeof(*mx
), score_compare
);
550 for (i
= 0; i
< dst_cnt
* NUM_CANDIDATE_PER_DST
; i
++) {
551 struct diff_rename_dst
*dst
;
553 if ((mx
[i
].dst
< 0) ||
554 (mx
[i
].score
< minimum_score
))
555 break; /* there is no more usable pair. */
556 dst
= &rename_dst
[mx
[i
].dst
];
558 continue; /* already done, either exact or fuzzy. */
559 if (rename_src
[mx
[i
].src
].one
->rename_used
)
561 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
565 for (i
= 0; i
< dst_cnt
* NUM_CANDIDATE_PER_DST
; i
++) {
566 struct diff_rename_dst
*dst
;
568 if ((mx
[i
].dst
< 0) ||
569 (mx
[i
].score
< minimum_score
))
570 break; /* there is no more usable pair. */
571 dst
= &rename_dst
[mx
[i
].dst
];
573 continue; /* already done, either exact or fuzzy. */
574 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
580 /* At this point, we have found some renames and copies and they
581 * are recorded in rename_dst. The original list is still in *q.
583 DIFF_QUEUE_CLEAR(&outq
);
584 for (i
= 0; i
< q
->nr
; i
++) {
585 struct diff_filepair
*p
= q
->queue
[i
];
586 struct diff_filepair
*pair_to_free
= NULL
;
588 if (!DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
592 * We would output this create record if it has
593 * not been turned into a rename/copy already.
595 struct diff_rename_dst
*dst
=
596 locate_rename_dst(p
->two
, 0);
597 if (dst
&& dst
->pair
) {
598 diff_q(&outq
, dst
->pair
);
602 /* no matching rename/copy source, so
603 * record this as a creation.
607 else if (DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
)) {
611 * We would output this delete record if:
613 * (1) this is a broken delete and the counterpart
614 * broken create remains in the output; or
615 * (2) this is not a broken delete, and rename_dst
616 * does not have a rename/copy to move p->one->path
619 * Otherwise, the counterpart broken create
620 * has been turned into a rename-edit; or
621 * delete did not have a matching create to
624 if (DIFF_PAIR_BROKEN(p
)) {
626 struct diff_rename_dst
*dst
=
627 locate_rename_dst(p
->one
, 0);
628 if (dst
&& dst
->pair
)
629 /* counterpart is now rename/copy */
633 if (p
->one
->rename_used
)
634 /* this path remains */
643 else if (!diff_unmodified_pair(p
))
644 /* all the usual ones need to be kept */
647 /* no need to keep unmodified pairs */
651 diff_free_filepair(pair_to_free
);
653 diff_debug_queue("done copying original", &outq
);
657 diff_debug_queue("done collapsing", q
);
659 for (i
= 0; i
< rename_dst_nr
; i
++)
660 free_filespec(rename_dst
[i
].two
);
664 rename_dst_nr
= rename_dst_alloc
= 0;
667 rename_src_nr
= rename_src_alloc
= 0;