[PATCH] Add --pickaxe-all to diff-* brothers.
[git/git.git] / diffcore-rename.c
CommitLineData
427dcb4b
JH
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"
85976974 8#include "count-delta.h"
427dcb4b 9
25d5ea41
JH
10/* Table of rename/copy destinations */
11
12static struct diff_rename_dst {
13 struct diff_filespec *two;
14 struct diff_filepair *pair;
15} *rename_dst;
16static int rename_dst_nr, rename_dst_alloc;
427dcb4b 17
25d5ea41
JH
18static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19 int insert_ok)
427dcb4b 20{
25d5ea41
JH
21 int first, last;
22
23 first = 0;
24 last = rename_dst_nr;
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);
29 if (!cmp)
30 return dst;
31 if (cmp < 0) {
32 last = next;
33 continue;
34 }
35 first = next+1;
36 }
37 /* not found */
38 if (!insert_ok)
39 return NULL;
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));
45 }
46 rename_dst_nr++;
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]);
427dcb4b
JH
53}
54
25d5ea41
JH
55static struct diff_rename_src {
56 struct diff_filespec *one;
57 unsigned src_used : 1;
58} *rename_src;
59static int rename_src_nr, rename_src_alloc;
427dcb4b 60
25d5ea41
JH
61static struct diff_rename_src *locate_rename_src(struct diff_filespec *one,
62 int insert_ok)
63{
64 int first, last;
65
66 first = 0;
67 last = rename_src_nr;
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);
72 if (!cmp)
73 return src;
74 if (cmp < 0) {
75 last = next;
76 continue;
77 }
78 first = next+1;
79 }
80 /* not found */
81 if (!insert_ok)
82 return NULL;
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));
427dcb4b 88 }
25d5ea41
JH
89 rename_src_nr++;
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]);
427dcb4b
JH
96}
97
98static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)
99{
100 if (src->sha1_valid && dst->sha1_valid &&
101 !memcmp(src->sha1, dst->sha1, 20))
102 return 1;
103 if (diff_populate_filespec(src) || diff_populate_filespec(dst))
104 /* this is an error but will be caught downstream */
105 return 0;
106 if (src->size == dst->size &&
107 !memcmp(src->data, dst->data, src->size))
108 return 1;
109 return 0;
110}
111
112struct diff_score {
25d5ea41
JH
113 int src; /* index in rename_src */
114 int dst; /* index in rename_dst */
427dcb4b 115 int score;
427dcb4b
JH
116};
117
118static int estimate_similarity(struct diff_filespec *src,
119 struct diff_filespec *dst,
120 int minimum_score)
121{
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.
127 *
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
131 * else.
132 */
133 void *delta;
58b103f5 134 unsigned long delta_size, base_size;
427dcb4b
JH
135 int score;
136
60896c7b
JH
137 /* We deal only with regular files. Symlink renames are handled
138 * only when they are exact matches --- in other words, no edits
139 * after renaming.
140 */
141 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
142 return 0;
143
427dcb4b
JH
144 delta_size = ((src->size < dst->size) ?
145 (dst->size - src->size) : (src->size - dst->size));
58b103f5 146 base_size = ((src->size < dst->size) ? src->size : dst->size);
427dcb4b 147
58b103f5
JH
148 /* We would not consider edits that change the file size so
149 * drastically. delta_size must be smaller than
cd1870ed 150 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
58b103f5
JH
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.
427dcb4b 154 */
cd1870ed 155 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
427dcb4b
JH
156 return 0;
157
158 delta = diff_delta(src->data, src->size,
159 dst->data, dst->size,
160 &delta_size);
85976974
JH
161
162 /* A delta that has a lot of literal additions would have
163 * big delta_size no matter what else it does.
58b103f5 164 */
a00d7d10 165 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
85976974
JH
166 return 0;
167
168 /* Estimate the edit size by interpreting delta. */
169 delta_size = count_delta(delta, delta_size);
427dcb4b 170 free(delta);
85976974
JH
171 if (delta_size == UINT_MAX)
172 return 0;
427dcb4b 173
58b103f5
JH
174 /*
175 * Now we will give some score to it. 100% edit gets 0 points
176 * and 0% edit gets MAX_SCORE points.
427dcb4b 177 */
58b103f5 178 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);
427dcb4b
JH
179 if (score < 0) return 0;
180 if (MAX_SCORE < score) return MAX_SCORE;
181 return score;
182}
183
25d5ea41
JH
184static void record_rename_pair(struct diff_queue_struct *renq,
185 int dst_index, int src_index, int score)
427dcb4b 186{
25d5ea41
JH
187 struct diff_filespec *one, *two, *src, *dst;
188 struct diff_filepair *dp;
f7c1512a 189
25d5ea41
JH
190 if (rename_dst[dst_index].pair)
191 die("internal error: dst already matched.");
427dcb4b 192
25d5ea41
JH
193 src = rename_src[src_index].one;
194 one = alloc_filespec(src->path);
195 fill_filespec(one, src->sha1, src->mode);
427dcb4b 196
25d5ea41
JH
197 dst = rename_dst[dst_index].two;
198 two = alloc_filespec(dst->path);
199 fill_filespec(two, dst->sha1, dst->mode);
427dcb4b 200
25d5ea41
JH
201 dp = diff_queue(renq, one, two);
202 dp->score = score;
427dcb4b 203
25d5ea41
JH
204 rename_src[src_index].src_used = 1;
205 rename_dst[dst_index].pair = dp;
427dcb4b
JH
206}
207
208/*
209 * We sort the rename similarity matrix with the score, in descending
210 * order (more similar first).
211 */
212static int score_compare(const void *a_, const void *b_)
213{
214 const struct diff_score *a = a_, *b = b_;
215 return b->score - a->score;
216}
217
6b14d7fa
JH
218int diff_scoreopt_parse(const char *opt)
219{
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++)
228 scale *= 10;
229
230 /* user says num divided by scale and we say internally that
231 * is MAX_SCORE * num / scale.
232 */
233 return MAX_SCORE * num / scale;
234}
235
236void diffcore_rename(int detect_rename, int minimum_score)
427dcb4b 237{
38c6f780 238 struct diff_queue_struct *q = &diff_queued_diff;
25d5ea41 239 struct diff_queue_struct renq, outq;
427dcb4b 240 struct diff_score *mx;
25d5ea41
JH
241 int i, j;
242 int num_create, num_src, dst_cnt;
427dcb4b 243
26dee0ad
JH
244 if (!minimum_score)
245 minimum_score = DEFAULT_MINIMUM_SCORE;
25d5ea41
JH
246 renq.queue = NULL;
247 renq.nr = renq.alloc = 0;
427dcb4b 248
427dcb4b 249 for (i = 0; i < q->nr; i++) {
52e95789 250 struct diff_filepair *p = q->queue[i];
81e50eab
JH
251 if (!DIFF_FILE_VALID(p->one))
252 if (!DIFF_FILE_VALID(p->two))
60896c7b 253 continue; /* unmerged */
427dcb4b 254 else
25d5ea41 255 locate_rename_dst(p->two, 1);
81e50eab 256 else if (!DIFF_FILE_VALID(p->two))
25d5ea41 257 locate_rename_src(p->one, 1);
427dcb4b 258 else if (1 < detect_rename) /* find copy, too */
25d5ea41 259 locate_rename_src(p->one, 1);
427dcb4b 260 }
25d5ea41 261 if (rename_dst_nr == 0)
427dcb4b
JH
262 goto cleanup; /* nothing to do */
263
264 /* We really want to cull the candidates list early
265 * with cheap tests in order to avoid doing deltas.
427dcb4b 266 */
25d5ea41
JH
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))
272 continue;
273 record_rename_pair(&renq, i, j, MAX_SCORE);
274 break; /* we are done with this entry */
427dcb4b
JH
275 }
276 }
25d5ea41 277 diff_debug_queue("done detecting exact", &renq);
427dcb4b
JH
278
279 /* Have we run out the created file pool? If so we can avoid
280 * doing the delta matrix altogether.
281 */
25d5ea41 282 if (renq.nr == rename_dst_nr)
427dcb4b
JH
283 goto flush_rest;
284
25d5ea41
JH
285 num_create = (rename_dst_nr - renq.nr);
286 num_src = rename_src_nr;
427dcb4b 287 mx = xmalloc(sizeof(*mx) * num_create * num_src);
25d5ea41 288 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
427dcb4b 289 int base = dst_cnt * num_src;
25d5ea41
JH
290 struct diff_filespec *two = rename_dst[i].two;
291 if (rename_dst[i].pair)
427dcb4b 292 continue; /* dealt with exact match already. */
25d5ea41
JH
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];
296 m->src = j;
297 m->dst = i;
298 m->score = estimate_similarity(one, two,
299 minimum_score);
427dcb4b
JH
300 }
301 dst_cnt++;
302 }
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++) {
25d5ea41
JH
306 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
307 if (dst->pair)
308 continue; /* already done, either exact or fuzzy. */
427dcb4b 309 if (mx[i].score < minimum_score)
58b103f5 310 break; /* there is not any more diffs applicable. */
25d5ea41 311 record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
427dcb4b
JH
312 }
313 free(mx);
25d5ea41 314 diff_debug_queue("done detecting fuzzy", &renq);
427dcb4b
JH
315
316 flush_rest:
317 /* At this point, we have found some renames and copies and they
25d5ea41 318 * are kept in renq. The original list is still in *q.
427dcb4b
JH
319 *
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.
323 *
324 * See comments at the top of record_rename_pair for numbers used
f7c1512a 325 * to assign rename_rank.
427dcb4b 326 */
25d5ea41
JH
327 outq.queue = NULL;
328 outq.nr = outq.alloc = 0;
427dcb4b 329 for (i = 0; i < q->nr; i++) {
25d5ea41
JH
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;
334
335 if (dst) {
336 /* creation */
337 if (dst->pair) {
338 /* renq has rename/copy already to produce
339 * this file, so we do not emit the creation
340 * record in the output.
341 */
342 diff_q(&outq, dst->pair);
343 pair_to_free = p;
344 }
345 else
346 /* no matching rename/copy source, so record
347 * this as a creation.
348 */
349 diff_q(&outq, p);
427dcb4b 350 }
25d5ea41
JH
351 else if (!diff_unmodified_pair(p))
352 /* all the other cases need to be recorded as is */
353 diff_q(&outq, p);
427dcb4b 354 else {
25d5ea41
JH
355 /* unmodified pair needs to be recorded only if
356 * it is used as the source of rename/copy
357 */
358 if (src && src->src_used)
359 diff_q(&outq, p);
427dcb4b 360 else
25d5ea41 361 pair_to_free = p;
427dcb4b 362 }
226406f6
JH
363 if (pair_to_free)
364 diff_free_filepair(pair_to_free);
427dcb4b 365 }
25d5ea41 366 diff_debug_queue("done copying original", &outq);
427dcb4b 367
25d5ea41
JH
368 free(renq.queue);
369 free(q->queue);
370 *q = outq;
371 diff_debug_queue("done collapsing", q);
427dcb4b
JH
372
373 cleanup:
25d5ea41
JH
374 free(rename_dst);
375 rename_dst = NULL;
376 rename_dst_nr = rename_dst_alloc = 0;
377 free(rename_src);
378 rename_src = NULL;
379 rename_src_nr = rename_src_alloc = 0;
427dcb4b
JH
380 return;
381}