2 * Copyright (C) 2005 Junio C Hamano
9 /* Table of rename/copy destinations */
11 static struct diff_rename_dst
{
12 struct diff_filespec
*two
;
13 struct diff_filepair
*pair
;
15 static int rename_dst_nr
, rename_dst_alloc
;
17 static struct diff_rename_dst
*locate_rename_dst(struct diff_filespec
*two
,
24 while (last
> first
) {
25 int next
= (last
+ first
) >> 1;
26 struct diff_rename_dst
*dst
= &(rename_dst
[next
]);
27 int cmp
= strcmp(two
->path
, dst
->two
->path
);
39 /* insert to make it at "first" */
40 if (rename_dst_alloc
<= rename_dst_nr
) {
41 rename_dst_alloc
= alloc_nr(rename_dst_alloc
);
42 rename_dst
= xrealloc(rename_dst
,
43 rename_dst_alloc
* sizeof(*rename_dst
));
46 if (first
< rename_dst_nr
)
47 memmove(rename_dst
+ first
+ 1, rename_dst
+ first
,
48 (rename_dst_nr
- first
- 1) * sizeof(*rename_dst
));
49 rename_dst
[first
].two
= alloc_filespec(two
->path
);
50 fill_filespec(rename_dst
[first
].two
, two
->sha1
, two
->mode
);
51 rename_dst
[first
].pair
= NULL
;
52 return &(rename_dst
[first
]);
55 /* Table of rename/copy src files */
56 static struct diff_rename_src
{
57 struct diff_filespec
*one
;
58 unsigned short score
; /* to remember the break score */
60 static int rename_src_nr
, rename_src_alloc
;
62 static struct diff_rename_src
*register_rename_src(struct diff_filespec
*one
,
69 while (last
> first
) {
70 int next
= (last
+ first
) >> 1;
71 struct diff_rename_src
*src
= &(rename_src
[next
]);
72 int cmp
= strcmp(one
->path
, src
->one
->path
);
82 /* insert to make it at "first" */
83 if (rename_src_alloc
<= rename_src_nr
) {
84 rename_src_alloc
= alloc_nr(rename_src_alloc
);
85 rename_src
= xrealloc(rename_src
,
86 rename_src_alloc
* sizeof(*rename_src
));
89 if (first
< rename_src_nr
)
90 memmove(rename_src
+ first
+ 1, rename_src
+ first
,
91 (rename_src_nr
- first
- 1) * sizeof(*rename_src
));
92 rename_src
[first
].one
= one
;
93 rename_src
[first
].score
= score
;
94 return &(rename_src
[first
]);
97 static int basename_same(struct diff_filespec
*src
, struct diff_filespec
*dst
)
99 int src_len
= strlen(src
->path
), dst_len
= strlen(dst
->path
);
100 while (src_len
&& dst_len
) {
101 char c1
= src
->path
[--src_len
];
102 char c2
= dst
->path
[--dst_len
];
108 return (!src_len
|| src
->path
[src_len
- 1] == '/') &&
109 (!dst_len
|| dst
->path
[dst_len
- 1] == '/');
113 int src
; /* index in rename_src */
114 int dst
; /* index in rename_dst */
119 static int estimate_similarity(struct diff_filespec
*src
,
120 struct diff_filespec
*dst
,
123 /* src points at a file that existed in the original tree (or
124 * optionally a file in the destination tree) and dst points
125 * at a newly created file. They may be quite similar, in which
126 * case we want to say src is renamed to dst or src is copied into
127 * dst, and then some edit has been applied to dst.
129 * Compare them and return how similar they are, representing
130 * the score as an integer between 0 and MAX_SCORE.
132 * When there is an exact match, it is considered a better
133 * match than anything else; the destination does not even
134 * call into this function in that case.
136 unsigned long max_size
, delta_size
, base_size
, src_copied
, literal_added
;
137 unsigned long delta_limit
;
140 /* We deal only with regular files. Symlink renames are handled
141 * only when they are exact matches --- in other words, no edits
144 if (!S_ISREG(src
->mode
) || !S_ISREG(dst
->mode
))
148 * Need to check that source and destination sizes are
149 * filled in before comparing them.
151 * If we already have "cnt_data" filled in, we know it's
152 * all good (avoid checking the size for zero, as that
153 * is a possible size - we really should have a flag to
154 * say whether the size is valid or not!)
156 if (!src
->cnt_data
&& diff_populate_filespec(src
, 0))
158 if (!dst
->cnt_data
&& diff_populate_filespec(dst
, 0))
161 max_size
= ((src
->size
> dst
->size
) ? src
->size
: dst
->size
);
162 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
163 delta_size
= max_size
- base_size
;
165 /* We would not consider edits that change the file size so
166 * drastically. delta_size must be smaller than
167 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
169 * Note that base_size == 0 case is handled here already
170 * and the final score computation below would not have a
171 * divide-by-zero issue.
173 if (base_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
176 delta_limit
= (unsigned long)
177 (base_size
* (MAX_SCORE
-minimum_score
) / MAX_SCORE
);
178 if (diffcore_count_changes(src
, dst
,
179 &src
->cnt_data
, &dst
->cnt_data
,
181 &src_copied
, &literal_added
))
184 /* How similar are they?
185 * what percentage of material in dst are from source?
188 score
= 0; /* should not happen */
190 score
= (int)(src_copied
* MAX_SCORE
/ max_size
);
194 static void record_rename_pair(int dst_index
, int src_index
, int score
)
196 struct diff_filespec
*src
, *dst
;
197 struct diff_filepair
*dp
;
199 if (rename_dst
[dst_index
].pair
)
200 die("internal error: dst already matched.");
202 src
= rename_src
[src_index
].one
;
206 dst
= rename_dst
[dst_index
].two
;
209 dp
= diff_queue(NULL
, src
, dst
);
210 dp
->renamed_pair
= 1;
211 if (!strcmp(src
->path
, dst
->path
))
212 dp
->score
= rename_src
[src_index
].score
;
215 rename_dst
[dst_index
].pair
= dp
;
219 * We sort the rename similarity matrix with the score, in descending
220 * order (the most similar first).
222 static int score_compare(const void *a_
, const void *b_
)
224 const struct diff_score
*a
= a_
, *b
= b_
;
226 if (a
->score
== b
->score
)
227 return b
->name_score
- a
->name_score
;
229 return b
->score
- a
->score
;
232 struct file_similarity
{
234 struct diff_filespec
*filespec
;
235 struct file_similarity
*next
;
238 static int find_identical_files(struct file_similarity
*src
,
239 struct file_similarity
*dst
)
244 * Walk over all the destinations ...
247 struct diff_filespec
*target
= dst
->filespec
;
248 struct file_similarity
*p
, *best
;
249 int i
= 100, best_score
= -1;
252 * .. to find the best source match
255 for (p
= src
; p
; p
= p
->next
) {
257 struct diff_filespec
*source
= p
->filespec
;
259 /* False hash collission? */
260 if (hashcmp(source
->sha1
, target
->sha1
))
262 /* Non-regular files? If so, the modes must match! */
263 if (!S_ISREG(source
->mode
) || !S_ISREG(target
->mode
)) {
264 if (source
->mode
!= target
->mode
)
267 /* Give higher scores to sources that haven't been used already */
268 score
= !source
->rename_used
;
269 score
+= basename_same(source
, target
);
270 if (score
> best_score
) {
277 /* Too many identical alternatives? Pick one */
282 record_rename_pair(dst
->index
, best
->index
, MAX_SCORE
);
285 } while ((dst
= dst
->next
) != NULL
);
289 static void free_similarity_list(struct file_similarity
*p
)
292 struct file_similarity
*entry
= p
;
298 static int find_same_files(void *ptr
)
301 struct file_similarity
*p
= ptr
;
302 struct file_similarity
*src
= NULL
, *dst
= NULL
;
304 /* Split the hash list up into sources and destinations */
306 struct file_similarity
*entry
= p
;
308 if (entry
->src_dst
< 0) {
318 * If we have both sources *and* destinations, see if
319 * we can match them up
321 ret
= (src
&& dst
) ? find_identical_files(src
, dst
) : 0;
323 /* Free the hashes and return the number of renames found */
324 free_similarity_list(src
);
325 free_similarity_list(dst
);
329 static unsigned int hash_filespec(struct diff_filespec
*filespec
)
332 if (!filespec
->sha1_valid
) {
333 if (diff_populate_filespec(filespec
, 0))
335 hash_sha1_file(filespec
->data
, filespec
->size
, "blob", filespec
->sha1
);
337 memcpy(&hash
, filespec
->sha1
, sizeof(hash
));
341 static void insert_file_table(struct hash_table
*table
, int src_dst
, int index
, struct diff_filespec
*filespec
)
345 struct file_similarity
*entry
= xmalloc(sizeof(*entry
));
347 entry
->src_dst
= src_dst
;
348 entry
->index
= index
;
349 entry
->filespec
= filespec
;
352 hash
= hash_filespec(filespec
);
353 pos
= insert_hash(hash
, entry
, table
);
355 /* We already had an entry there? */
363 * Find exact renames first.
365 * The first round matches up the up-to-date entries,
366 * and then during the second round we try to match
367 * cache-dirty entries as well.
369 static int find_exact_renames(void)
372 struct hash_table file_table
;
374 init_hash(&file_table
);
375 for (i
= 0; i
< rename_src_nr
; i
++)
376 insert_file_table(&file_table
, -1, i
, rename_src
[i
].one
);
378 for (i
= 0; i
< rename_dst_nr
; i
++)
379 insert_file_table(&file_table
, 1, i
, rename_dst
[i
].two
);
381 /* Find the renames */
382 i
= for_each_hash(&file_table
, find_same_files
);
384 /* .. and free the hash data structure */
385 free_hash(&file_table
);
390 void diffcore_rename(struct diff_options
*options
)
392 int detect_rename
= options
->detect_rename
;
393 int minimum_score
= options
->rename_score
;
394 int rename_limit
= options
->rename_limit
;
395 struct diff_queue_struct
*q
= &diff_queued_diff
;
396 struct diff_queue_struct outq
;
397 struct diff_score
*mx
;
398 int i
, j
, rename_count
;
399 int num_create
, num_src
, dst_cnt
;
402 minimum_score
= DEFAULT_RENAME_SCORE
;
404 for (i
= 0; i
< q
->nr
; i
++) {
405 struct diff_filepair
*p
= q
->queue
[i
];
406 if (!DIFF_FILE_VALID(p
->one
)) {
407 if (!DIFF_FILE_VALID(p
->two
))
408 continue; /* unmerged */
409 else if (options
->single_follow
&&
410 strcmp(options
->single_follow
, p
->two
->path
))
411 continue; /* not interested */
413 locate_rename_dst(p
->two
, 1);
415 else if (!DIFF_FILE_VALID(p
->two
)) {
417 * If the source is a broken "delete", and
418 * they did not really want to get broken,
419 * that means the source actually stays.
420 * So we increment the "rename_used" score
421 * by one, to indicate ourselves as a user
423 if (p
->broken_pair
&& !p
->score
)
424 p
->one
->rename_used
++;
425 register_rename_src(p
->one
, p
->score
);
427 else if (detect_rename
== DIFF_DETECT_COPY
) {
429 * Increment the "rename_used" score by
430 * one, to indicate ourselves as a user.
432 p
->one
->rename_used
++;
433 register_rename_src(p
->one
, p
->score
);
436 if (rename_dst_nr
== 0 || rename_src_nr
== 0)
437 goto cleanup
; /* nothing to do */
440 * We really want to cull the candidates list early
441 * with cheap tests in order to avoid doing deltas.
443 rename_count
= find_exact_renames();
445 /* Did we only want exact renames? */
446 if (minimum_score
== MAX_SCORE
)
450 * Calculate how many renames are left (but all the source
451 * files still remain as options for rename/copies!)
453 num_create
= (rename_dst_nr
- rename_count
);
454 num_src
= rename_src_nr
;
461 * This basically does a test for the rename matrix not
462 * growing larger than a "rename_limit" square matrix, ie:
464 * num_create * num_src > rename_limit * rename_limit
466 * but handles the potential overflow case specially (and we
467 * assume at least 32-bit integers)
469 if (rename_limit
<= 0 || rename_limit
> 32767)
470 rename_limit
= 32767;
471 if ((num_create
> rename_limit
&& num_src
> rename_limit
) ||
472 (num_create
* num_src
> rename_limit
* rename_limit
)) {
473 warning("too many files, skipping inexact rename detection");
477 mx
= xmalloc(sizeof(*mx
) * num_create
* num_src
);
478 for (dst_cnt
= i
= 0; i
< rename_dst_nr
; i
++) {
479 int base
= dst_cnt
* num_src
;
480 struct diff_filespec
*two
= rename_dst
[i
].two
;
481 if (rename_dst
[i
].pair
)
482 continue; /* dealt with exact match already. */
483 for (j
= 0; j
< rename_src_nr
; j
++) {
484 struct diff_filespec
*one
= rename_src
[j
].one
;
485 struct diff_score
*m
= &mx
[base
+j
];
488 m
->score
= estimate_similarity(one
, two
,
490 m
->name_score
= basename_same(one
, two
);
491 diff_free_filespec_blob(one
);
493 /* We do not need the text anymore */
494 diff_free_filespec_blob(two
);
497 /* cost matrix sorted by most to least similar pair */
498 qsort(mx
, num_create
* num_src
, sizeof(*mx
), score_compare
);
499 for (i
= 0; i
< num_create
* num_src
; i
++) {
500 struct diff_rename_dst
*dst
= &rename_dst
[mx
[i
].dst
];
501 struct diff_filespec
*src
;
503 continue; /* already done, either exact or fuzzy. */
504 if (mx
[i
].score
< minimum_score
)
505 break; /* there is no more usable pair. */
506 src
= rename_src
[mx
[i
].src
].one
;
507 if (src
->rename_used
)
509 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
512 for (i
= 0; i
< num_create
* num_src
; i
++) {
513 struct diff_rename_dst
*dst
= &rename_dst
[mx
[i
].dst
];
515 continue; /* already done, either exact or fuzzy. */
516 if (mx
[i
].score
< minimum_score
)
517 break; /* there is no more usable pair. */
518 record_rename_pair(mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
524 /* At this point, we have found some renames and copies and they
525 * are recorded in rename_dst. The original list is still in *q.
528 outq
.nr
= outq
.alloc
= 0;
529 for (i
= 0; i
< q
->nr
; i
++) {
530 struct diff_filepair
*p
= q
->queue
[i
];
531 struct diff_filepair
*pair_to_free
= NULL
;
533 if (!DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
537 * We would output this create record if it has
538 * not been turned into a rename/copy already.
540 struct diff_rename_dst
*dst
=
541 locate_rename_dst(p
->two
, 0);
542 if (dst
&& dst
->pair
) {
543 diff_q(&outq
, dst
->pair
);
547 /* no matching rename/copy source, so
548 * record this as a creation.
552 else if (DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
)) {
556 * We would output this delete record if:
558 * (1) this is a broken delete and the counterpart
559 * broken create remains in the output; or
560 * (2) this is not a broken delete, and rename_dst
561 * does not have a rename/copy to move p->one->path
564 * Otherwise, the counterpart broken create
565 * has been turned into a rename-edit; or
566 * delete did not have a matching create to
569 if (DIFF_PAIR_BROKEN(p
)) {
571 struct diff_rename_dst
*dst
=
572 locate_rename_dst(p
->one
, 0);
573 if (dst
&& dst
->pair
)
574 /* counterpart is now rename/copy */
578 if (p
->one
->rename_used
)
579 /* this path remains */
588 else if (!diff_unmodified_pair(p
))
589 /* all the usual ones need to be kept */
592 /* no need to keep unmodified pairs */
596 diff_free_filepair(pair_to_free
);
598 diff_debug_queue("done copying original", &outq
);
602 diff_debug_queue("done collapsing", q
);
604 for (i
= 0; i
< rename_dst_nr
; i
++)
605 free_filespec(rename_dst
[i
].two
);
609 rename_dst_nr
= rename_dst_alloc
= 0;
612 rename_src_nr
= rename_src_alloc
= 0;