]>
Commit | Line | Data |
---|---|---|
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 | ||
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; | |
427dcb4b | 17 | |
25d5ea41 JH |
18 | static 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 | ||
15d061b4 | 55 | /* Table of rename/copy src files */ |
25d5ea41 JH |
56 | static struct diff_rename_src { |
57 | struct diff_filespec *one; | |
15d061b4 | 58 | unsigned src_stays : 1; |
25d5ea41 JH |
59 | } *rename_src; |
60 | static int rename_src_nr, rename_src_alloc; | |
427dcb4b | 61 | |
15d061b4 JH |
62 | static struct diff_rename_src *register_rename_src(struct diff_filespec *one, |
63 | int src_stays) | |
25d5ea41 JH |
64 | { |
65 | int first, last; | |
66 | ||
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; | |
78 | } | |
79 | first = next+1; | |
80 | } | |
15d061b4 | 81 | |
25d5ea41 JH |
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)); | |
427dcb4b | 87 | } |
25d5ea41 JH |
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; | |
15d061b4 | 93 | rename_src[first].src_stays = src_stays; |
25d5ea41 | 94 | return &(rename_src[first]); |
427dcb4b JH |
95 | } |
96 | ||
97 | static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst) | |
98 | { | |
99 | if (src->sha1_valid && dst->sha1_valid && | |
100 | !memcmp(src->sha1, dst->sha1, 20)) | |
101 | return 1; | |
102 | if (diff_populate_filespec(src) || diff_populate_filespec(dst)) | |
103 | /* this is an error but will be caught downstream */ | |
104 | return 0; | |
105 | if (src->size == dst->size && | |
106 | !memcmp(src->data, dst->data, src->size)) | |
107 | return 1; | |
108 | return 0; | |
109 | } | |
110 | ||
111 | struct diff_score { | |
25d5ea41 JH |
112 | int src; /* index in rename_src */ |
113 | int dst; /* index in rename_dst */ | |
427dcb4b | 114 | int score; |
427dcb4b JH |
115 | }; |
116 | ||
117 | static int estimate_similarity(struct diff_filespec *src, | |
118 | struct diff_filespec *dst, | |
119 | int minimum_score) | |
120 | { | |
121 | /* src points at a file that existed in the original tree (or | |
122 | * optionally a file in the destination tree) and dst points | |
123 | * at a newly created file. They may be quite similar, in which | |
124 | * case we want to say src is renamed to dst or src is copied into | |
125 | * dst, and then some edit has been applied to dst. | |
126 | * | |
127 | * Compare them and return how similar they are, representing | |
128 | * the score as an integer between 0 and 10000, except | |
129 | * where they match exactly it is considered better than anything | |
130 | * else. | |
131 | */ | |
132 | void *delta; | |
58b103f5 | 133 | unsigned long delta_size, base_size; |
427dcb4b JH |
134 | int score; |
135 | ||
60896c7b JH |
136 | /* We deal only with regular files. Symlink renames are handled |
137 | * only when they are exact matches --- in other words, no edits | |
138 | * after renaming. | |
139 | */ | |
140 | if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) | |
141 | return 0; | |
142 | ||
427dcb4b JH |
143 | delta_size = ((src->size < dst->size) ? |
144 | (dst->size - src->size) : (src->size - dst->size)); | |
58b103f5 | 145 | base_size = ((src->size < dst->size) ? src->size : dst->size); |
427dcb4b | 146 | |
58b103f5 JH |
147 | /* We would not consider edits that change the file size so |
148 | * drastically. delta_size must be smaller than | |
cd1870ed | 149 | * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). |
58b103f5 JH |
150 | * Note that base_size == 0 case is handled here already |
151 | * and the final score computation below would not have a | |
152 | * divide-by-zero issue. | |
427dcb4b | 153 | */ |
cd1870ed | 154 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
427dcb4b JH |
155 | return 0; |
156 | ||
157 | delta = diff_delta(src->data, src->size, | |
158 | dst->data, dst->size, | |
159 | &delta_size); | |
85976974 JH |
160 | |
161 | /* A delta that has a lot of literal additions would have | |
162 | * big delta_size no matter what else it does. | |
58b103f5 | 163 | */ |
a00d7d10 | 164 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
85976974 JH |
165 | return 0; |
166 | ||
167 | /* Estimate the edit size by interpreting delta. */ | |
168 | delta_size = count_delta(delta, delta_size); | |
427dcb4b | 169 | free(delta); |
85976974 JH |
170 | if (delta_size == UINT_MAX) |
171 | return 0; | |
427dcb4b | 172 | |
58b103f5 JH |
173 | /* |
174 | * Now we will give some score to it. 100% edit gets 0 points | |
175 | * and 0% edit gets MAX_SCORE points. | |
427dcb4b | 176 | */ |
58b103f5 | 177 | score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); |
427dcb4b JH |
178 | if (score < 0) return 0; |
179 | if (MAX_SCORE < score) return MAX_SCORE; | |
180 | return score; | |
181 | } | |
182 | ||
25d5ea41 JH |
183 | static void record_rename_pair(struct diff_queue_struct *renq, |
184 | int dst_index, int src_index, int score) | |
427dcb4b | 185 | { |
25d5ea41 JH |
186 | struct diff_filespec *one, *two, *src, *dst; |
187 | struct diff_filepair *dp; | |
f7c1512a | 188 | |
25d5ea41 JH |
189 | if (rename_dst[dst_index].pair) |
190 | die("internal error: dst already matched."); | |
427dcb4b | 191 | |
25d5ea41 JH |
192 | src = rename_src[src_index].one; |
193 | one = alloc_filespec(src->path); | |
194 | fill_filespec(one, src->sha1, src->mode); | |
427dcb4b | 195 | |
25d5ea41 JH |
196 | dst = rename_dst[dst_index].two; |
197 | two = alloc_filespec(dst->path); | |
198 | fill_filespec(two, dst->sha1, dst->mode); | |
427dcb4b | 199 | |
25d5ea41 | 200 | dp = diff_queue(renq, one, two); |
15d061b4 JH |
201 | dp->score = score ? : 1; /* make sure it is at least 1 */ |
202 | dp->source_stays = rename_src[src_index].src_stays; | |
25d5ea41 | 203 | rename_dst[dst_index].pair = dp; |
427dcb4b JH |
204 | } |
205 | ||
206 | /* | |
207 | * We sort the rename similarity matrix with the score, in descending | |
15d061b4 | 208 | * order (the most similar first). |
427dcb4b JH |
209 | */ |
210 | static int score_compare(const void *a_, const void *b_) | |
211 | { | |
212 | const struct diff_score *a = a_, *b = b_; | |
213 | return b->score - a->score; | |
214 | } | |
215 | ||
6b14d7fa JH |
216 | int diff_scoreopt_parse(const char *opt) |
217 | { | |
218 | int diglen, num, scale, i; | |
219 | if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C')) | |
220 | return -1; /* that is not a -M nor -C option */ | |
221 | diglen = strspn(opt+2, "0123456789"); | |
222 | if (diglen == 0 || strlen(opt+2) != diglen) | |
223 | return 0; /* use default */ | |
224 | sscanf(opt+2, "%d", &num); | |
225 | for (i = 0, scale = 1; i < diglen; i++) | |
226 | scale *= 10; | |
227 | ||
228 | /* user says num divided by scale and we say internally that | |
229 | * is MAX_SCORE * num / scale. | |
230 | */ | |
231 | return MAX_SCORE * num / scale; | |
232 | } | |
233 | ||
234 | void diffcore_rename(int detect_rename, int minimum_score) | |
427dcb4b | 235 | { |
38c6f780 | 236 | struct diff_queue_struct *q = &diff_queued_diff; |
25d5ea41 | 237 | struct diff_queue_struct renq, outq; |
427dcb4b | 238 | struct diff_score *mx; |
25d5ea41 JH |
239 | int i, j; |
240 | int num_create, num_src, dst_cnt; | |
427dcb4b | 241 | |
26dee0ad JH |
242 | if (!minimum_score) |
243 | minimum_score = DEFAULT_MINIMUM_SCORE; | |
25d5ea41 JH |
244 | renq.queue = NULL; |
245 | renq.nr = renq.alloc = 0; | |
427dcb4b | 246 | |
427dcb4b | 247 | for (i = 0; i < q->nr; i++) { |
52e95789 | 248 | struct diff_filepair *p = q->queue[i]; |
81e50eab JH |
249 | if (!DIFF_FILE_VALID(p->one)) |
250 | if (!DIFF_FILE_VALID(p->two)) | |
60896c7b | 251 | continue; /* unmerged */ |
427dcb4b | 252 | else |
25d5ea41 | 253 | locate_rename_dst(p->two, 1); |
81e50eab | 254 | else if (!DIFF_FILE_VALID(p->two)) |
15d061b4 JH |
255 | register_rename_src(p->one, 0); |
256 | else if (detect_rename == DIFF_DETECT_COPY) | |
257 | register_rename_src(p->one, 1); | |
427dcb4b | 258 | } |
25d5ea41 | 259 | if (rename_dst_nr == 0) |
427dcb4b JH |
260 | goto cleanup; /* nothing to do */ |
261 | ||
262 | /* We really want to cull the candidates list early | |
263 | * with cheap tests in order to avoid doing deltas. | |
427dcb4b | 264 | */ |
25d5ea41 JH |
265 | for (i = 0; i < rename_dst_nr; i++) { |
266 | struct diff_filespec *two = rename_dst[i].two; | |
267 | for (j = 0; j < rename_src_nr; j++) { | |
268 | struct diff_filespec *one = rename_src[j].one; | |
269 | if (!is_exact_match(one, two)) | |
270 | continue; | |
271 | record_rename_pair(&renq, i, j, MAX_SCORE); | |
272 | break; /* we are done with this entry */ | |
427dcb4b JH |
273 | } |
274 | } | |
25d5ea41 | 275 | diff_debug_queue("done detecting exact", &renq); |
427dcb4b JH |
276 | |
277 | /* Have we run out the created file pool? If so we can avoid | |
278 | * doing the delta matrix altogether. | |
279 | */ | |
25d5ea41 | 280 | if (renq.nr == rename_dst_nr) |
15d061b4 | 281 | goto cleanup; |
427dcb4b | 282 | |
25d5ea41 JH |
283 | num_create = (rename_dst_nr - renq.nr); |
284 | num_src = rename_src_nr; | |
427dcb4b | 285 | mx = xmalloc(sizeof(*mx) * num_create * num_src); |
25d5ea41 | 286 | for (dst_cnt = i = 0; i < rename_dst_nr; i++) { |
427dcb4b | 287 | int base = dst_cnt * num_src; |
25d5ea41 JH |
288 | struct diff_filespec *two = rename_dst[i].two; |
289 | if (rename_dst[i].pair) | |
427dcb4b | 290 | continue; /* dealt with exact match already. */ |
25d5ea41 JH |
291 | for (j = 0; j < rename_src_nr; j++) { |
292 | struct diff_filespec *one = rename_src[j].one; | |
293 | struct diff_score *m = &mx[base+j]; | |
294 | m->src = j; | |
295 | m->dst = i; | |
296 | m->score = estimate_similarity(one, two, | |
297 | minimum_score); | |
427dcb4b JH |
298 | } |
299 | dst_cnt++; | |
300 | } | |
301 | /* cost matrix sorted by most to least similar pair */ | |
302 | qsort(mx, num_create * num_src, sizeof(*mx), score_compare); | |
303 | for (i = 0; i < num_create * num_src; i++) { | |
25d5ea41 JH |
304 | struct diff_rename_dst *dst = &rename_dst[mx[i].dst]; |
305 | if (dst->pair) | |
306 | continue; /* already done, either exact or fuzzy. */ | |
427dcb4b | 307 | if (mx[i].score < minimum_score) |
15d061b4 | 308 | break; /* there is no more usable pair. */ |
25d5ea41 | 309 | record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score); |
427dcb4b JH |
310 | } |
311 | free(mx); | |
25d5ea41 | 312 | diff_debug_queue("done detecting fuzzy", &renq); |
427dcb4b | 313 | |
15d061b4 | 314 | cleanup: |
427dcb4b | 315 | /* At this point, we have found some renames and copies and they |
25d5ea41 | 316 | * are kept in renq. The original list is still in *q. |
427dcb4b | 317 | */ |
25d5ea41 JH |
318 | outq.queue = NULL; |
319 | outq.nr = outq.alloc = 0; | |
427dcb4b | 320 | for (i = 0; i < q->nr; i++) { |
25d5ea41 | 321 | struct diff_filepair *p = q->queue[i]; |
25d5ea41 JH |
322 | struct diff_rename_dst *dst = locate_rename_dst(p->two, 0); |
323 | struct diff_filepair *pair_to_free = NULL; | |
324 | ||
325 | if (dst) { | |
326 | /* creation */ | |
327 | if (dst->pair) { | |
15d061b4 JH |
328 | /* renq has rename/copy to produce |
329 | * this file already, so we do not | |
330 | * emit the creation record in the | |
331 | * output. | |
25d5ea41 JH |
332 | */ |
333 | diff_q(&outq, dst->pair); | |
334 | pair_to_free = p; | |
335 | } | |
336 | else | |
337 | /* no matching rename/copy source, so record | |
338 | * this as a creation. | |
339 | */ | |
340 | diff_q(&outq, p); | |
427dcb4b | 341 | } |
25d5ea41 | 342 | else if (!diff_unmodified_pair(p)) |
15d061b4 | 343 | /* all the usual ones need to be kept */ |
25d5ea41 | 344 | diff_q(&outq, p); |
15d061b4 JH |
345 | else |
346 | /* no need to keep unmodified pairs */ | |
347 | pair_to_free = p; | |
348 | ||
226406f6 JH |
349 | if (pair_to_free) |
350 | diff_free_filepair(pair_to_free); | |
427dcb4b | 351 | } |
25d5ea41 | 352 | diff_debug_queue("done copying original", &outq); |
427dcb4b | 353 | |
25d5ea41 JH |
354 | free(renq.queue); |
355 | free(q->queue); | |
356 | *q = outq; | |
357 | diff_debug_queue("done collapsing", q); | |
427dcb4b | 358 | |
25d5ea41 JH |
359 | free(rename_dst); |
360 | rename_dst = NULL; | |
361 | rename_dst_nr = rename_dst_alloc = 0; | |
362 | free(rename_src); | |
363 | rename_src = NULL; | |
364 | rename_src_nr = rename_src_alloc = 0; | |
427dcb4b JH |
365 | return; |
366 | } |