2 * Copyright (C) 2005 Junio C Hamano
8 #include "count-delta.h"
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
= two
;
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 src_path_left
: 1;
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
].src_path_left
= src_path_left
;
94 return &(rename_src
[first
]);
97 static int is_exact_match(struct diff_filespec
*src
, struct diff_filespec
*dst
)
99 if (src
->sha1_valid
&& dst
->sha1_valid
&&
100 !memcmp(src
->sha1
, dst
->sha1
, 20))
102 if (diff_populate_filespec(src
, 1) || diff_populate_filespec(dst
, 1))
104 if (src
->size
!= dst
->size
)
106 if (diff_populate_filespec(src
, 0) || diff_populate_filespec(dst
, 0))
108 if (src
->size
== dst
->size
&&
109 !memcmp(src
->data
, dst
->data
, src
->size
))
115 int src
; /* index in rename_src */
116 int dst
; /* index in rename_dst */
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.
138 unsigned long 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
))
149 delta_size
= ((src
->size
< dst
->size
) ?
150 (dst
->size
- src
->size
) : (src
->size
- dst
->size
));
151 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
153 /* We would not consider edits that change the file size so
154 * drastically. delta_size must be smaller than
155 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
157 * Note that base_size == 0 case is handled here already
158 * and the final score computation below would not have a
159 * divide-by-zero issue.
161 if (base_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
164 if (diff_populate_filespec(src
, 0) || diff_populate_filespec(dst
, 0))
165 return 0; /* error but caught downstream */
167 delta_limit
= base_size
* (MAX_SCORE
-minimum_score
) / MAX_SCORE
;
168 delta
= diff_delta(src
->data
, src
->size
,
169 dst
->data
, dst
->size
,
170 &delta_size
, delta_limit
);
172 /* If delta_limit is exceeded, we have too much differences */
175 /* A delta that has a lot of literal additions would have
176 * big delta_size no matter what else it does.
178 if (base_size
* (MAX_SCORE
-minimum_score
) < delta_size
* MAX_SCORE
)
181 /* Estimate the edit size by interpreting delta. */
182 if (count_delta(delta
, delta_size
, &src_copied
, &literal_added
)) {
188 /* Extent of damage */
189 if (src
->size
+ literal_added
< src_copied
)
192 delta_size
= (src
->size
- src_copied
) + literal_added
;
195 * Now we will give some score to it. 100% edit gets 0 points
196 * and 0% edit gets MAX_SCORE points.
198 score
= MAX_SCORE
- (MAX_SCORE
* delta_size
/ base_size
);
199 if (score
< 0) return 0;
200 if (MAX_SCORE
< score
) return MAX_SCORE
;
204 static void record_rename_pair(struct diff_queue_struct
*renq
,
205 int dst_index
, int src_index
, int score
)
207 struct diff_filespec
*one
, *two
, *src
, *dst
;
208 struct diff_filepair
*dp
;
210 if (rename_dst
[dst_index
].pair
)
211 die("internal error: dst already matched.");
213 src
= rename_src
[src_index
].one
;
214 one
= alloc_filespec(src
->path
);
215 fill_filespec(one
, src
->sha1
, src
->mode
);
217 dst
= rename_dst
[dst_index
].two
;
218 two
= alloc_filespec(dst
->path
);
219 fill_filespec(two
, dst
->sha1
, dst
->mode
);
221 dp
= diff_queue(renq
, one
, two
);
223 dp
->source_stays
= rename_src
[src_index
].src_path_left
;
224 rename_dst
[dst_index
].pair
= dp
;
228 * We sort the rename similarity matrix with the score, in descending
229 * order (the most similar first).
231 static int score_compare(const void *a_
, const void *b_
)
233 const struct diff_score
*a
= a_
, *b
= b_
;
234 return b
->score
- a
->score
;
237 static int compute_stays(struct diff_queue_struct
*q
,
238 struct diff_filespec
*one
)
241 for (i
= 0; i
< q
->nr
; i
++) {
242 struct diff_filepair
*p
= q
->queue
[i
];
243 if (strcmp(one
->path
, p
->two
->path
))
245 if (DIFF_PAIR_RENAME(p
)) {
246 return 0; /* something else is renamed into this */
252 void diffcore_rename(int detect_rename
, int minimum_score
)
254 struct diff_queue_struct
*q
= &diff_queued_diff
;
255 struct diff_queue_struct renq
, outq
;
256 struct diff_score
*mx
;
258 int num_create
, num_src
, dst_cnt
;
261 minimum_score
= DEFAULT_RENAME_SCORE
;
263 renq
.nr
= renq
.alloc
= 0;
265 for (i
= 0; i
< q
->nr
; i
++) {
266 struct diff_filepair
*p
= q
->queue
[i
];
267 if (!DIFF_FILE_VALID(p
->one
))
268 if (!DIFF_FILE_VALID(p
->two
))
269 continue; /* unmerged */
271 locate_rename_dst(p
->two
, 1);
272 else if (!DIFF_FILE_VALID(p
->two
)) {
273 /* If the source is a broken "delete", and
274 * they did not really want to get broken,
275 * that means the source actually stays.
277 int stays
= (p
->broken_pair
&& !p
->score
);
278 register_rename_src(p
->one
, stays
);
280 else if (detect_rename
== DIFF_DETECT_COPY
)
281 register_rename_src(p
->one
, 1);
283 if (rename_dst_nr
== 0)
284 goto cleanup
; /* nothing to do */
286 /* We really want to cull the candidates list early
287 * with cheap tests in order to avoid doing deltas.
289 for (i
= 0; i
< rename_dst_nr
; i
++) {
290 struct diff_filespec
*two
= rename_dst
[i
].two
;
291 for (j
= 0; j
< rename_src_nr
; j
++) {
292 struct diff_filespec
*one
= rename_src
[j
].one
;
293 if (!is_exact_match(one
, two
))
295 record_rename_pair(&renq
, i
, j
, MAX_SCORE
);
296 break; /* we are done with this entry */
299 diff_debug_queue("done detecting exact", &renq
);
301 /* Have we run out the created file pool? If so we can avoid
302 * doing the delta matrix altogether.
304 if (renq
.nr
== rename_dst_nr
)
307 num_create
= (rename_dst_nr
- renq
.nr
);
308 num_src
= rename_src_nr
;
309 mx
= xmalloc(sizeof(*mx
) * num_create
* num_src
);
310 for (dst_cnt
= i
= 0; i
< rename_dst_nr
; i
++) {
311 int base
= dst_cnt
* num_src
;
312 struct diff_filespec
*two
= rename_dst
[i
].two
;
313 if (rename_dst
[i
].pair
)
314 continue; /* dealt with exact match already. */
315 for (j
= 0; j
< rename_src_nr
; j
++) {
316 struct diff_filespec
*one
= rename_src
[j
].one
;
317 struct diff_score
*m
= &mx
[base
+j
];
320 m
->score
= estimate_similarity(one
, two
,
325 /* cost matrix sorted by most to least similar pair */
326 qsort(mx
, num_create
* num_src
, sizeof(*mx
), score_compare
);
327 for (i
= 0; i
< num_create
* num_src
; i
++) {
328 struct diff_rename_dst
*dst
= &rename_dst
[mx
[i
].dst
];
330 continue; /* already done, either exact or fuzzy. */
331 if (mx
[i
].score
< minimum_score
)
332 break; /* there is no more usable pair. */
333 record_rename_pair(&renq
, mx
[i
].dst
, mx
[i
].src
, mx
[i
].score
);
336 diff_debug_queue("done detecting fuzzy", &renq
);
339 /* At this point, we have found some renames and copies and they
340 * are kept in renq. The original list is still in *q.
343 outq
.nr
= outq
.alloc
= 0;
344 for (i
= 0; i
< q
->nr
; i
++) {
345 struct diff_filepair
*p
= q
->queue
[i
];
346 struct diff_filepair
*pair_to_free
= NULL
;
348 if (!DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
352 * We would output this create record if it has
353 * not been turned into a rename/copy already.
355 struct diff_rename_dst
*dst
=
356 locate_rename_dst(p
->two
, 0);
357 if (dst
&& dst
->pair
) {
358 diff_q(&outq
, dst
->pair
);
362 /* no matching rename/copy source, so
363 * record this as a creation.
367 else if (DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
)) {
371 * We would output this delete record if:
373 * (1) this is a broken delete and the counterpart
374 * broken create remains in the output; or
375 * (2) this is not a broken delete, and renq does
376 * not have a rename/copy to move p->one->path
379 * Otherwise, the counterpart broken create
380 * has been turned into a rename-edit; or
381 * delete did not have a matching create to
384 if (DIFF_PAIR_BROKEN(p
)) {
386 struct diff_rename_dst
*dst
=
387 locate_rename_dst(p
->one
, 0);
388 if (dst
&& dst
->pair
)
389 /* counterpart is now rename/copy */
393 for (j
= 0; j
< renq
.nr
; j
++)
394 if (!strcmp(renq
.queue
[j
]->one
->path
,
398 /* this path remains */
407 else if (!diff_unmodified_pair(p
))
408 /* all the usual ones need to be kept */
411 /* no need to keep unmodified pairs */
415 diff_free_filepair(pair_to_free
);
417 diff_debug_queue("done copying original", &outq
);
422 diff_debug_queue("done collapsing", q
);
424 /* We need to see which rename source really stays here;
425 * earlier we only checked if the path is left in the result,
426 * but even if a path remains in the result, if that is coming
427 * from copying something else on top of it, then the original
428 * source is lost and does not stay.
430 for (i
= 0; i
< q
->nr
; i
++) {
431 struct diff_filepair
*p
= q
->queue
[i
];
432 if (DIFF_PAIR_RENAME(p
) && p
->source_stays
) {
433 /* If one appears as the target of a rename-copy,
434 * then mark p->source_stays = 0; otherwise
437 p
->source_stays
= compute_stays(q
, p
->one
);
443 rename_dst_nr
= rename_dst_alloc
= 0;
446 rename_src_nr
= rename_src_alloc
= 0;