1/*
2* Copyright (C) 2005 Junio C Hamano
3*/
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
7#include "delta.h"
8#include "count-delta.h"
910
/* Table of rename/copy destinations */
1112
static struct diff_rename_dst {
13struct diff_filespec *two;
14struct diff_filepair *pair;
15} *rename_dst;
16static int rename_dst_nr, rename_dst_alloc;
1718
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19int insert_ok)
20{
21int first, last;
2223
first = 0;
24last = rename_dst_nr;
25while (last > first) {
26int next = (last + first) >> 1;
27struct diff_rename_dst *dst = &(rename_dst[next]);
28int cmp = strcmp(two->path, dst->two->path);
29if (!cmp)
30return dst;
31if (cmp < 0) {
32last = next;
33continue;
34}
35first = next+1;
36}
37/* not found */
38if (!insert_ok)
39return NULL;
40/* insert to make it at "first" */
41if (rename_dst_alloc <= rename_dst_nr) {
42rename_dst_alloc = alloc_nr(rename_dst_alloc);
43rename_dst = xrealloc(rename_dst,
44rename_dst_alloc * sizeof(*rename_dst));
45}
46rename_dst_nr++;
47if (first < rename_dst_nr)
48memmove(rename_dst + first + 1, rename_dst + first,
49(rename_dst_nr - first - 1) * sizeof(*rename_dst));
50rename_dst[first].two = two;
51rename_dst[first].pair = NULL;
52return &(rename_dst[first]);
53}
5455
/* Table of rename/copy src files */
56static struct diff_rename_src {
57struct diff_filespec *one;
58unsigned src_stays : 1;
59} *rename_src;
60static int rename_src_nr, rename_src_alloc;
6162
static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
63int src_stays)
64{
65int first, last;
6667
first = 0;
68last = rename_src_nr;
69while (last > first) {
70int next = (last + first) >> 1;
71struct diff_rename_src *src = &(rename_src[next]);
72int cmp = strcmp(one->path, src->one->path);
73if (!cmp)
74return src;
75if (cmp < 0) {
76last = next;
77continue;
78}
79first = next+1;
80}
8182
/* insert to make it at "first" */
83if (rename_src_alloc <= rename_src_nr) {
84rename_src_alloc = alloc_nr(rename_src_alloc);
85rename_src = xrealloc(rename_src,
86rename_src_alloc * sizeof(*rename_src));
87}
88rename_src_nr++;
89if (first < rename_src_nr)
90memmove(rename_src + first + 1, rename_src + first,
91(rename_src_nr - first - 1) * sizeof(*rename_src));
92rename_src[first].one = one;
93rename_src[first].src_stays = src_stays;
94return &(rename_src[first]);
95}
9697
static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)
98{
99if (src->sha1_valid && dst->sha1_valid &&
100!memcmp(src->sha1, dst->sha1, 20))
101return 1;
102if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
103return 0;
104if (src->size != dst->size)
105return 0;
106if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
107return 0;
108if (src->size == dst->size &&
109!memcmp(src->data, dst->data, src->size))
110return 1;
111return 0;
112}
113114
struct diff_score {
115int src; /* index in rename_src */
116int dst; /* index in rename_dst */
117int score;
118};
119120
static int estimate_similarity(struct diff_filespec *src,
121struct diff_filespec *dst,
122int minimum_score)
123{
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.
129*
130* Compare them and return how similar they are, representing
131* the score as an integer between 0 and MAX_SCORE.
132*
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.
136*/
137void *delta;
138unsigned long delta_size, base_size;
139int score;
140141
/* We deal only with regular files. Symlink renames are handled
142* only when they are exact matches --- in other words, no edits
143* after renaming.
144*/
145if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
146return 0;
147148
delta_size = ((src->size < dst->size) ?
149(dst->size - src->size) : (src->size - dst->size));
150base_size = ((src->size < dst->size) ? src->size : dst->size);
151152
/* We would not consider edits that change the file size so
153* drastically. delta_size must be smaller than
154* (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
155*
156* Note that base_size == 0 case is handled here already
157* and the final score computation below would not have a
158* divide-by-zero issue.
159*/
160if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
161return 0;
162163
if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
164return 0; /* error but caught downstream */
165166
delta = diff_delta(src->data, src->size,
167dst->data, dst->size,
168&delta_size);
169170
/* A delta that has a lot of literal additions would have
171* big delta_size no matter what else it does.
172*/
173if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
174return 0;
175176
/* Estimate the edit size by interpreting delta. */
177delta_size = count_delta(delta, delta_size);
178free(delta);
179if (delta_size == UINT_MAX)
180return 0;
181182
/*
183* Now we will give some score to it. 100% edit gets 0 points
184* and 0% edit gets MAX_SCORE points.
185*/
186score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);
187if (score < 0) return 0;
188if (MAX_SCORE < score) return MAX_SCORE;
189return score;
190}
191192
static void record_rename_pair(struct diff_queue_struct *renq,
193int dst_index, int src_index, int score)
194{
195struct diff_filespec *one, *two, *src, *dst;
196struct diff_filepair *dp;
197198
if (rename_dst[dst_index].pair)
199die("internal error: dst already matched.");
200201
src = rename_src[src_index].one;
202one = alloc_filespec(src->path);
203fill_filespec(one, src->sha1, src->mode);
204205
dst = rename_dst[dst_index].two;
206two = alloc_filespec(dst->path);
207fill_filespec(two, dst->sha1, dst->mode);
208209
dp = diff_queue(renq, one, two);
210dp->score = score;
211dp->source_stays = rename_src[src_index].src_stays;
212rename_dst[dst_index].pair = dp;
213}
214215
/*
216* We sort the rename similarity matrix with the score, in descending
217* order (the most similar first).
218*/
219static int score_compare(const void *a_, const void *b_)
220{
221const struct diff_score *a = a_, *b = b_;
222return b->score - a->score;
223}
224225
int diff_scoreopt_parse(const char *opt)
226{
227int diglen, num, scale, i;
228if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C' && opt[1] != 'B'))
229return -1; /* that is not a -M, -C nor -B option */
230diglen = strspn(opt+2, "0123456789");
231if (diglen == 0 || strlen(opt+2) != diglen)
232return 0; /* use default */
233sscanf(opt+2, "%d", &num);
234for (i = 0, scale = 1; i < diglen; i++)
235scale *= 10;
236237
/* user says num divided by scale and we say internally that
238* is MAX_SCORE * num / scale.
239*/
240return MAX_SCORE * num / scale;
241}
242243
void diffcore_rename(int detect_rename, int minimum_score)
244{
245struct diff_queue_struct *q = &diff_queued_diff;
246struct diff_queue_struct renq, outq;
247struct diff_score *mx;
248int i, j;
249int num_create, num_src, dst_cnt;
250251
if (!minimum_score)
252minimum_score = DEFAULT_RENAME_SCORE;
253renq.queue = NULL;
254renq.nr = renq.alloc = 0;
255256
for (i = 0; i < q->nr; i++) {
257struct diff_filepair *p = q->queue[i];
258if (!DIFF_FILE_VALID(p->one))
259if (!DIFF_FILE_VALID(p->two))
260continue; /* unmerged */
261else
262locate_rename_dst(p->two, 1);
263else if (!DIFF_FILE_VALID(p->two))
264register_rename_src(p->one, 0);
265else if (detect_rename == DIFF_DETECT_COPY)
266register_rename_src(p->one, 1);
267}
268if (rename_dst_nr == 0)
269goto cleanup; /* nothing to do */
270271
/* We really want to cull the candidates list early
272* with cheap tests in order to avoid doing deltas.
273*/
274for (i = 0; i < rename_dst_nr; i++) {
275struct diff_filespec *two = rename_dst[i].two;
276for (j = 0; j < rename_src_nr; j++) {
277struct diff_filespec *one = rename_src[j].one;
278if (!is_exact_match(one, two))
279continue;
280record_rename_pair(&renq, i, j, MAX_SCORE);
281break; /* we are done with this entry */
282}
283}
284diff_debug_queue("done detecting exact", &renq);
285286
/* Have we run out the created file pool? If so we can avoid
287* doing the delta matrix altogether.
288*/
289if (renq.nr == rename_dst_nr)
290goto cleanup;
291292
num_create = (rename_dst_nr - renq.nr);
293num_src = rename_src_nr;
294mx = xmalloc(sizeof(*mx) * num_create * num_src);
295for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
296int base = dst_cnt * num_src;
297struct diff_filespec *two = rename_dst[i].two;
298if (rename_dst[i].pair)
299continue; /* dealt with exact match already. */
300for (j = 0; j < rename_src_nr; j++) {
301struct diff_filespec *one = rename_src[j].one;
302struct diff_score *m = &mx[base+j];
303m->src = j;
304m->dst = i;
305m->score = estimate_similarity(one, two,
306minimum_score);
307}
308dst_cnt++;
309}
310/* cost matrix sorted by most to least similar pair */
311qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
312for (i = 0; i < num_create * num_src; i++) {
313struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
314if (dst->pair)
315continue; /* already done, either exact or fuzzy. */
316if (mx[i].score < minimum_score)
317break; /* there is no more usable pair. */
318record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
319}
320free(mx);
321diff_debug_queue("done detecting fuzzy", &renq);
322323
cleanup:
324/* At this point, we have found some renames and copies and they
325* are kept in renq. The original list is still in *q.
326*/
327outq.queue = NULL;
328outq.nr = outq.alloc = 0;
329for (i = 0; i < q->nr; i++) {
330struct diff_filepair *p = q->queue[i];
331struct diff_filepair *pair_to_free = NULL;
332333
if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
334/*
335* Creation
336*
337* We would output this create record if it has
338* not been turned into a rename/copy already.
339*/
340struct diff_rename_dst *dst =
341locate_rename_dst(p->two, 0);
342if (dst && dst->pair) {
343diff_q(&outq, dst->pair);
344pair_to_free = p;
345}
346else
347/* no matching rename/copy source, so
348* record this as a creation.
349*/
350diff_q(&outq, p);
351}
352else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
353/*
354* Deletion
355*
356* We would output this delete record if:
357*
358* (1) this is a broken delete and the counterpart
359* broken create remains in the output; or
360* (2) this is not a broken delete, and renq does
361* not have a rename/copy to move p->one->path
362* out.
363*
364* Otherwise, the counterpart broken create
365* has been turned into a rename-edit; or
366* delete did not have a matching create to
367* begin with.
368*/
369if (DIFF_PAIR_BROKEN(p)) {
370/* broken delete */
371struct diff_rename_dst *dst =
372locate_rename_dst(p->one, 0);
373if (dst && dst->pair)
374/* counterpart is now rename/copy */
375pair_to_free = p;
376}
377else {
378for (j = 0; j < renq.nr; j++)
379if (!strcmp(renq.queue[j]->one->path,
380p->one->path))
381break;
382if (j < renq.nr)
383/* this path remains */
384pair_to_free = p;
385}
386387
if (pair_to_free)
388;
389else
390diff_q(&outq, p);
391}
392else if (!diff_unmodified_pair(p))
393/* all the usual ones need to be kept */
394diff_q(&outq, p);
395else
396/* no need to keep unmodified pairs */
397pair_to_free = p;
398399
if (pair_to_free)
400diff_free_filepair(pair_to_free);
401}
402diff_debug_queue("done copying original", &outq);
403404
free(renq.queue);
405free(q->queue);
406*q = outq;
407diff_debug_queue("done collapsing", q);
408409
free(rename_dst);
410rename_dst = NULL;
411rename_dst_nr = rename_dst_alloc = 0;
412free(rename_src);
413rename_src = NULL;
414rename_src_nr = rename_src_alloc = 0;
415return;
416}