[PATCH] Add -O<orderfile> option to diff-* brothers.
[git/dkf.git] / diffcore-rename.c
blob8ed37dafda92048ffe67a485981d329726e6f41c
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"
10 /* Table of rename/copy destinations */
12 static struct diff_rename_dst {
13 struct diff_filespec *two;
14 struct diff_filepair *pair;
15 } *rename_dst;
16 static int rename_dst_nr, rename_dst_alloc;
18 static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19 int insert_ok)
21 int first, last;
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;
35 first = next+1;
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));
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]);
55 /* Table of rename/copy src files */
56 static struct diff_rename_src {
57 struct diff_filespec *one;
58 unsigned src_stays : 1;
59 } *rename_src;
60 static int rename_src_nr, rename_src_alloc;
62 static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
63 int src_stays)
65 int first, last;
67 first = 0;
68 last = rename_src_nr;
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);
73 if (!cmp)
74 return src;
75 if (cmp < 0) {
76 last = next;
77 continue;
79 first = next+1;
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));
88 rename_src_nr++;
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_stays = src_stays;
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))
101 return 1;
102 if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
103 return 0;
104 if (src->size != dst->size)
105 return 0;
106 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
107 return 0;
108 if (src->size == dst->size &&
109 !memcmp(src->data, dst->data, src->size))
110 return 1;
111 return 0;
114 struct diff_score {
115 int src; /* index in rename_src */
116 int dst; /* index in rename_dst */
117 int score;
120 static int estimate_similarity(struct diff_filespec *src,
121 struct diff_filespec *dst,
122 int minimum_score)
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.
137 void *delta;
138 unsigned long delta_size, base_size;
139 int score;
141 /* 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.
145 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
146 return 0;
148 delta_size = ((src->size < dst->size) ?
149 (dst->size - src->size) : (src->size - dst->size));
150 base_size = ((src->size < dst->size) ? src->size : dst->size);
152 /* 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).
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.
160 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
161 return 0;
163 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
164 return 0; /* error but caught downstream */
166 delta = diff_delta(src->data, src->size,
167 dst->data, dst->size,
168 &delta_size);
170 /* A delta that has a lot of literal additions would have
171 * big delta_size no matter what else it does.
173 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
174 return 0;
176 /* Estimate the edit size by interpreting delta. */
177 delta_size = count_delta(delta, delta_size);
178 free(delta);
179 if (delta_size == UINT_MAX)
180 return 0;
183 * Now we will give some score to it. 100% edit gets 0 points
184 * and 0% edit gets MAX_SCORE points.
186 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);
187 if (score < 0) return 0;
188 if (MAX_SCORE < score) return MAX_SCORE;
189 return score;
192 static void record_rename_pair(struct diff_queue_struct *renq,
193 int dst_index, int src_index, int score)
195 struct diff_filespec *one, *two, *src, *dst;
196 struct diff_filepair *dp;
198 if (rename_dst[dst_index].pair)
199 die("internal error: dst already matched.");
201 src = rename_src[src_index].one;
202 one = alloc_filespec(src->path);
203 fill_filespec(one, src->sha1, src->mode);
205 dst = rename_dst[dst_index].two;
206 two = alloc_filespec(dst->path);
207 fill_filespec(two, dst->sha1, dst->mode);
209 dp = diff_queue(renq, one, two);
210 dp->score = score;
211 dp->source_stays = rename_src[src_index].src_stays;
212 rename_dst[dst_index].pair = dp;
216 * We sort the rename similarity matrix with the score, in descending
217 * order (the most similar first).
219 static int score_compare(const void *a_, const void *b_)
221 const struct diff_score *a = a_, *b = b_;
222 return b->score - a->score;
225 int diff_scoreopt_parse(const char *opt)
227 int diglen, num, scale, i;
228 if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C' && opt[1] != 'B'))
229 return -1; /* that is not a -M, -C nor -B option */
230 diglen = strspn(opt+2, "0123456789");
231 if (diglen == 0 || strlen(opt+2) != diglen)
232 return 0; /* use default */
233 sscanf(opt+2, "%d", &num);
234 for (i = 0, scale = 1; i < diglen; i++)
235 scale *= 10;
237 /* user says num divided by scale and we say internally that
238 * is MAX_SCORE * num / scale.
240 return MAX_SCORE * num / scale;
243 void diffcore_rename(int detect_rename, int minimum_score)
245 struct diff_queue_struct *q = &diff_queued_diff;
246 struct diff_queue_struct renq, outq;
247 struct diff_score *mx;
248 int i, j;
249 int num_create, num_src, dst_cnt;
251 if (!minimum_score)
252 minimum_score = DEFAULT_RENAME_SCORE;
253 renq.queue = NULL;
254 renq.nr = renq.alloc = 0;
256 for (i = 0; i < q->nr; i++) {
257 struct diff_filepair *p = q->queue[i];
258 if (!DIFF_FILE_VALID(p->one))
259 if (!DIFF_FILE_VALID(p->two))
260 continue; /* unmerged */
261 else
262 locate_rename_dst(p->two, 1);
263 else if (!DIFF_FILE_VALID(p->two))
264 register_rename_src(p->one, 0);
265 else if (detect_rename == DIFF_DETECT_COPY)
266 register_rename_src(p->one, 1);
268 if (rename_dst_nr == 0)
269 goto cleanup; /* nothing to do */
271 /* We really want to cull the candidates list early
272 * with cheap tests in order to avoid doing deltas.
274 for (i = 0; i < rename_dst_nr; i++) {
275 struct diff_filespec *two = rename_dst[i].two;
276 for (j = 0; j < rename_src_nr; j++) {
277 struct diff_filespec *one = rename_src[j].one;
278 if (!is_exact_match(one, two))
279 continue;
280 record_rename_pair(&renq, i, j, MAX_SCORE);
281 break; /* we are done with this entry */
284 diff_debug_queue("done detecting exact", &renq);
286 /* Have we run out the created file pool? If so we can avoid
287 * doing the delta matrix altogether.
289 if (renq.nr == rename_dst_nr)
290 goto cleanup;
292 num_create = (rename_dst_nr - renq.nr);
293 num_src = rename_src_nr;
294 mx = xmalloc(sizeof(*mx) * num_create * num_src);
295 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
296 int base = dst_cnt * num_src;
297 struct diff_filespec *two = rename_dst[i].two;
298 if (rename_dst[i].pair)
299 continue; /* dealt with exact match already. */
300 for (j = 0; j < rename_src_nr; j++) {
301 struct diff_filespec *one = rename_src[j].one;
302 struct diff_score *m = &mx[base+j];
303 m->src = j;
304 m->dst = i;
305 m->score = estimate_similarity(one, two,
306 minimum_score);
308 dst_cnt++;
310 /* cost matrix sorted by most to least similar pair */
311 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
312 for (i = 0; i < num_create * num_src; i++) {
313 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
314 if (dst->pair)
315 continue; /* already done, either exact or fuzzy. */
316 if (mx[i].score < minimum_score)
317 break; /* there is no more usable pair. */
318 record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
320 free(mx);
321 diff_debug_queue("done detecting fuzzy", &renq);
323 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.
327 outq.queue = NULL;
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_filepair *pair_to_free = NULL;
333 if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
335 * Creation
337 * We would output this create record if it has
338 * not been turned into a rename/copy already.
340 struct diff_rename_dst *dst =
341 locate_rename_dst(p->two, 0);
342 if (dst && dst->pair) {
343 diff_q(&outq, dst->pair);
344 pair_to_free = p;
346 else
347 /* no matching rename/copy source, so
348 * record this as a creation.
350 diff_q(&outq, p);
352 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
354 * Deletion
356 * We would output this delete record if:
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.
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.
369 if (DIFF_PAIR_BROKEN(p)) {
370 /* broken delete */
371 struct diff_rename_dst *dst =
372 locate_rename_dst(p->one, 0);
373 if (dst && dst->pair)
374 /* counterpart is now rename/copy */
375 pair_to_free = p;
377 else {
378 for (j = 0; j < renq.nr; j++)
379 if (!strcmp(renq.queue[j]->one->path,
380 p->one->path))
381 break;
382 if (j < renq.nr)
383 /* this path remains */
384 pair_to_free = p;
387 if (pair_to_free)
389 else
390 diff_q(&outq, p);
392 else if (!diff_unmodified_pair(p))
393 /* all the usual ones need to be kept */
394 diff_q(&outq, p);
395 else
396 /* no need to keep unmodified pairs */
397 pair_to_free = p;
399 if (pair_to_free)
400 diff_free_filepair(pair_to_free);
402 diff_debug_queue("done copying original", &outq);
404 free(renq.queue);
405 free(q->queue);
406 *q = outq;
407 diff_debug_queue("done collapsing", q);
409 free(rename_dst);
410 rename_dst = NULL;
411 rename_dst_nr = rename_dst_alloc = 0;
412 free(rename_src);
413 rename_src = NULL;
414 rename_src_nr = rename_src_alloc = 0;
415 return;