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 static struct diff_rename_src {
56 struct diff_filespec *one;
57 unsigned src_used : 1;
59 static int rename_src_nr, rename_src_alloc;
61 static struct diff_rename_src *locate_rename_src(struct diff_filespec *one,
68 while (last > first) {
69 int next = (last + first) >> 1;
70 struct diff_rename_src *src = &(rename_src[next]);
71 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].src_used = 0;
95 return &(rename_src[first]);
98 static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)
100 if (src->sha1_valid && dst->sha1_valid &&
101 !memcmp(src->sha1, dst->sha1, 20))
103 if (diff_populate_filespec(src) || diff_populate_filespec(dst))
104 /* this is an error but will be caught downstream */
106 if (src->size == dst->size &&
107 !memcmp(src->data, dst->data, src->size))
113 int src; /* index in rename_src */
114 int dst; /* index in rename_dst */
118 static int estimate_similarity(struct diff_filespec *src,
119 struct diff_filespec *dst,
122 /* src points at a file that existed in the original tree (or
123 * optionally a file in the destination tree) and dst points
124 * at a newly created file. They may be quite similar, in which
125 * case we want to say src is renamed to dst or src is copied into
126 * dst, and then some edit has been applied to dst.
128 * Compare them and return how similar they are, representing
129 * the score as an integer between 0 and 10000, except
130 * where they match exactly it is considered better than anything
134 unsigned long delta_size, base_size;
137 /* We deal only with regular files. Symlink renames are handled
138 * only when they are exact matches --- in other words, no edits
141 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
144 delta_size = ((src->size < dst->size) ?
145 (dst->size - src->size) : (src->size - dst->size));
146 base_size = ((src->size < dst->size) ? src->size : dst->size);
148 /* We would not consider edits that change the file size so
149 * drastically. delta_size must be smaller than
150 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
151 * Note that base_size == 0 case is handled here already
152 * and the final score computation below would not have a
153 * divide-by-zero issue.
155 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
158 delta = diff_delta(src->data, src->size,
159 dst->data, dst->size,
162 /* A delta that has a lot of literal additions would have
163 * big delta_size no matter what else it does.
165 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
168 /* Estimate the edit size by interpreting delta. */
169 delta_size = count_delta(delta, delta_size);
171 if (delta_size == UINT_MAX)
175 * Now we will give some score to it. 100% edit gets 0 points
176 * and 0% edit gets MAX_SCORE points.
178 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);
179 if (score < 0) return 0;
180 if (MAX_SCORE < score) return MAX_SCORE;
184 static void record_rename_pair(struct diff_queue_struct *renq,
185 int dst_index, int src_index, int score)
187 struct diff_filespec *one, *two, *src, *dst;
188 struct diff_filepair *dp;
190 if (rename_dst[dst_index].pair)
191 die("internal error: dst already matched.");
193 src = rename_src[src_index].one;
194 one = alloc_filespec(src->path);
195 fill_filespec(one, src->sha1, src->mode);
197 dst = rename_dst[dst_index].two;
198 two = alloc_filespec(dst->path);
199 fill_filespec(two, dst->sha1, dst->mode);
201 dp = diff_queue(renq, one, two);
204 rename_src[src_index].src_used = 1;
205 rename_dst[dst_index].pair = dp;
209 * We sort the rename similarity matrix with the score, in descending
210 * order (more similar first).
212 static int score_compare(const void *a_, const void *b_)
214 const struct diff_score *a = a_, *b = b_;
215 return b->score - a->score;
218 int diff_scoreopt_parse(const char *opt)
220 int diglen, num, scale, i;
221 if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
222 return -1; /* that is not a -M nor -C option */
223 diglen = strspn(opt+2, "0123456789");
224 if (diglen == 0 || strlen(opt+2) != diglen)
225 return 0; /* use default */
226 sscanf(opt+2, "%d", &num);
227 for (i = 0, scale = 1; i < diglen; i++)
230 /* user says num divided by scale and we say internally that
231 * is MAX_SCORE * num / scale.
233 return MAX_SCORE * num / scale;
236 void diffcore_rename(int detect_rename, int minimum_score)
238 struct diff_queue_struct *q = &diff_queued_diff;
239 struct diff_queue_struct renq, outq;
240 struct diff_score *mx;
242 int num_create, num_src, dst_cnt;
245 minimum_score = DEFAULT_MINIMUM_SCORE;
247 renq.nr = renq.alloc = 0;
249 for (i = 0; i < q->nr; i++) {
250 struct diff_filepair *p = q->queue[i];
251 if (!DIFF_FILE_VALID(p->one))
252 if (!DIFF_FILE_VALID(p->two))
253 continue; /* unmerged */
255 locate_rename_dst(p->two, 1);
256 else if (!DIFF_FILE_VALID(p->two))
257 locate_rename_src(p->one, 1);
258 else if (1 < detect_rename) /* find copy, too */
259 locate_rename_src(p->one, 1);
261 if (rename_dst_nr == 0)
262 goto cleanup; /* nothing to do */
264 /* We really want to cull the candidates list early
265 * with cheap tests in order to avoid doing deltas.
267 for (i = 0; i < rename_dst_nr; i++) {
268 struct diff_filespec *two = rename_dst[i].two;
269 for (j = 0; j < rename_src_nr; j++) {
270 struct diff_filespec *one = rename_src[j].one;
271 if (!is_exact_match(one, two))
273 record_rename_pair(&renq, i, j, MAX_SCORE);
274 break; /* we are done with this entry */
277 diff_debug_queue("done detecting exact", &renq);
279 /* Have we run out the created file pool? If so we can avoid
280 * doing the delta matrix altogether.
282 if (renq.nr == rename_dst_nr)
285 num_create = (rename_dst_nr - renq.nr);
286 num_src = rename_src_nr;
287 mx = xmalloc(sizeof(*mx) * num_create * num_src);
288 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
289 int base = dst_cnt * num_src;
290 struct diff_filespec *two = rename_dst[i].two;
291 if (rename_dst[i].pair)
292 continue; /* dealt with exact match already. */
293 for (j = 0; j < rename_src_nr; j++) {
294 struct diff_filespec *one = rename_src[j].one;
295 struct diff_score *m = &mx[base+j];
298 m->score = estimate_similarity(one, two,
303 /* cost matrix sorted by most to least similar pair */
304 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
305 for (i = 0; i < num_create * num_src; i++) {
306 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
308 continue; /* already done, either exact or fuzzy. */
309 if (mx[i].score < minimum_score)
310 break; /* there is not any more diffs applicable. */
311 record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
314 diff_debug_queue("done detecting fuzzy", &renq);
317 /* At this point, we have found some renames and copies and they
318 * are kept in renq. The original list is still in *q.
320 * Scan the original list and move them into the outq; we will sort
321 * outq and swap it into the queue supplied to pass that to
322 * downstream, so we assign the sort keys in this loop.
324 * See comments at the top of record_rename_pair for numbers used
325 * to assign rename_rank.
328 outq.nr = outq.alloc = 0;
329 for (i = 0; i < q->nr; i++) {
330 struct diff_filepair *p = q->queue[i];
331 struct diff_rename_src *src = locate_rename_src(p->one, 0);
332 struct diff_rename_dst *dst = locate_rename_dst(p->two, 0);
333 struct diff_filepair *pair_to_free = NULL;
338 /* renq has rename/copy already to produce
339 * this file, so we do not emit the creation
340 * record in the output.
342 diff_q(&outq, dst->pair);
346 /* no matching rename/copy source, so record
347 * this as a creation.
351 else if (!diff_unmodified_pair(p))
352 /* all the other cases need to be recorded as is */
355 /* unmodified pair needs to be recorded only if
356 * it is used as the source of rename/copy
358 if (src && src->src_used)
364 diff_free_filepair(pair_to_free);
366 diff_debug_queue("done copying original", &outq);
371 diff_debug_queue("done collapsing", q);
376 rename_dst_nr = rename_dst_alloc = 0;
379 rename_src_nr = rename_src_alloc = 0;