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; | |
f0c6b2a2 JH |
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)) | |
427dcb4b JH |
107 | return 0; |
108 | if (src->size == dst->size && | |
109 | !memcmp(src->data, dst->data, src->size)) | |
110 | return 1; | |
111 | return 0; | |
112 | } | |
113 | ||
114 | struct diff_score { | |
25d5ea41 JH |
115 | int src; /* index in rename_src */ |
116 | int dst; /* index in rename_dst */ | |
427dcb4b | 117 | int score; |
427dcb4b JH |
118 | }; |
119 | ||
120 | static int estimate_similarity(struct diff_filespec *src, | |
121 | struct diff_filespec *dst, | |
122 | int 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 | |
f0c6b2a2 JH |
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. | |
427dcb4b JH |
136 | */ |
137 | void *delta; | |
355e76a4 | 138 | unsigned long delta_size, base_size, src_copied, literal_added; |
427dcb4b JH |
139 | int score; |
140 | ||
60896c7b JH |
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. | |
144 | */ | |
145 | if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) | |
146 | return 0; | |
147 | ||
427dcb4b JH |
148 | delta_size = ((src->size < dst->size) ? |
149 | (dst->size - src->size) : (src->size - dst->size)); | |
58b103f5 | 150 | base_size = ((src->size < dst->size) ? src->size : dst->size); |
427dcb4b | 151 | |
58b103f5 JH |
152 | /* We would not consider edits that change the file size so |
153 | * drastically. delta_size must be smaller than | |
cd1870ed | 154 | * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). |
f0c6b2a2 | 155 | * |
58b103f5 JH |
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. | |
427dcb4b | 159 | */ |
cd1870ed | 160 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
427dcb4b JH |
161 | return 0; |
162 | ||
f0c6b2a2 JH |
163 | if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0)) |
164 | return 0; /* error but caught downstream */ | |
165 | ||
427dcb4b JH |
166 | delta = diff_delta(src->data, src->size, |
167 | dst->data, dst->size, | |
75c42d8c | 168 | &delta_size, ~0UL); |
85976974 JH |
169 | |
170 | /* A delta that has a lot of literal additions would have | |
171 | * big delta_size no matter what else it does. | |
58b103f5 | 172 | */ |
a00d7d10 | 173 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
85976974 JH |
174 | return 0; |
175 | ||
176 | /* Estimate the edit size by interpreting delta. */ | |
355e76a4 JH |
177 | if (count_delta(delta, delta_size, &src_copied, &literal_added)) { |
178 | free(delta); | |
85976974 | 179 | return 0; |
355e76a4 JH |
180 | } |
181 | free(delta); | |
182 | ||
183 | /* Extent of damage */ | |
184 | if (src->size + literal_added < src_copied) | |
185 | delta_size = 0; | |
186 | else | |
187 | delta_size = (src->size - src_copied) + literal_added; | |
427dcb4b | 188 | |
58b103f5 JH |
189 | /* |
190 | * Now we will give some score to it. 100% edit gets 0 points | |
191 | * and 0% edit gets MAX_SCORE points. | |
427dcb4b | 192 | */ |
58b103f5 | 193 | score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); |
427dcb4b JH |
194 | if (score < 0) return 0; |
195 | if (MAX_SCORE < score) return MAX_SCORE; | |
196 | return score; | |
197 | } | |
198 | ||
25d5ea41 JH |
199 | static void record_rename_pair(struct diff_queue_struct *renq, |
200 | int dst_index, int src_index, int score) | |
427dcb4b | 201 | { |
25d5ea41 JH |
202 | struct diff_filespec *one, *two, *src, *dst; |
203 | struct diff_filepair *dp; | |
f7c1512a | 204 | |
25d5ea41 JH |
205 | if (rename_dst[dst_index].pair) |
206 | die("internal error: dst already matched."); | |
427dcb4b | 207 | |
25d5ea41 JH |
208 | src = rename_src[src_index].one; |
209 | one = alloc_filespec(src->path); | |
210 | fill_filespec(one, src->sha1, src->mode); | |
427dcb4b | 211 | |
25d5ea41 JH |
212 | dst = rename_dst[dst_index].two; |
213 | two = alloc_filespec(dst->path); | |
214 | fill_filespec(two, dst->sha1, dst->mode); | |
427dcb4b | 215 | |
25d5ea41 | 216 | dp = diff_queue(renq, one, two); |
01c4e70f | 217 | dp->score = score; |
15d061b4 | 218 | dp->source_stays = rename_src[src_index].src_stays; |
25d5ea41 | 219 | rename_dst[dst_index].pair = dp; |
427dcb4b JH |
220 | } |
221 | ||
222 | /* | |
223 | * We sort the rename similarity matrix with the score, in descending | |
15d061b4 | 224 | * order (the most similar first). |
427dcb4b JH |
225 | */ |
226 | static int score_compare(const void *a_, const void *b_) | |
227 | { | |
228 | const struct diff_score *a = a_, *b = b_; | |
229 | return b->score - a->score; | |
230 | } | |
231 | ||
6b14d7fa | 232 | void diffcore_rename(int detect_rename, int minimum_score) |
427dcb4b | 233 | { |
38c6f780 | 234 | struct diff_queue_struct *q = &diff_queued_diff; |
25d5ea41 | 235 | struct diff_queue_struct renq, outq; |
427dcb4b | 236 | struct diff_score *mx; |
25d5ea41 JH |
237 | int i, j; |
238 | int num_create, num_src, dst_cnt; | |
427dcb4b | 239 | |
26dee0ad | 240 | if (!minimum_score) |
f345b0a0 | 241 | minimum_score = DEFAULT_RENAME_SCORE; |
25d5ea41 JH |
242 | renq.queue = NULL; |
243 | renq.nr = renq.alloc = 0; | |
427dcb4b | 244 | |
427dcb4b | 245 | for (i = 0; i < q->nr; i++) { |
52e95789 | 246 | struct diff_filepair *p = q->queue[i]; |
81e50eab JH |
247 | if (!DIFF_FILE_VALID(p->one)) |
248 | if (!DIFF_FILE_VALID(p->two)) | |
60896c7b | 249 | continue; /* unmerged */ |
427dcb4b | 250 | else |
25d5ea41 | 251 | locate_rename_dst(p->two, 1); |
2210100a JH |
252 | else if (!DIFF_FILE_VALID(p->two)) { |
253 | /* If the source is a broken "delete", and | |
254 | * they did not really want to get broken, | |
255 | * that means the source actually stays. | |
256 | */ | |
257 | int stays = (p->broken_pair && !p->score); | |
258 | register_rename_src(p->one, stays); | |
259 | } | |
15d061b4 JH |
260 | else if (detect_rename == DIFF_DETECT_COPY) |
261 | register_rename_src(p->one, 1); | |
427dcb4b | 262 | } |
25d5ea41 | 263 | if (rename_dst_nr == 0) |
427dcb4b JH |
264 | goto cleanup; /* nothing to do */ |
265 | ||
266 | /* We really want to cull the candidates list early | |
267 | * with cheap tests in order to avoid doing deltas. | |
427dcb4b | 268 | */ |
25d5ea41 JH |
269 | for (i = 0; i < rename_dst_nr; i++) { |
270 | struct diff_filespec *two = rename_dst[i].two; | |
271 | for (j = 0; j < rename_src_nr; j++) { | |
272 | struct diff_filespec *one = rename_src[j].one; | |
273 | if (!is_exact_match(one, two)) | |
274 | continue; | |
275 | record_rename_pair(&renq, i, j, MAX_SCORE); | |
276 | break; /* we are done with this entry */ | |
427dcb4b JH |
277 | } |
278 | } | |
25d5ea41 | 279 | diff_debug_queue("done detecting exact", &renq); |
427dcb4b JH |
280 | |
281 | /* Have we run out the created file pool? If so we can avoid | |
282 | * doing the delta matrix altogether. | |
283 | */ | |
25d5ea41 | 284 | if (renq.nr == rename_dst_nr) |
15d061b4 | 285 | goto cleanup; |
427dcb4b | 286 | |
25d5ea41 JH |
287 | num_create = (rename_dst_nr - renq.nr); |
288 | num_src = rename_src_nr; | |
427dcb4b | 289 | mx = xmalloc(sizeof(*mx) * num_create * num_src); |
25d5ea41 | 290 | for (dst_cnt = i = 0; i < rename_dst_nr; i++) { |
427dcb4b | 291 | int base = dst_cnt * num_src; |
25d5ea41 JH |
292 | struct diff_filespec *two = rename_dst[i].two; |
293 | if (rename_dst[i].pair) | |
427dcb4b | 294 | continue; /* dealt with exact match already. */ |
25d5ea41 JH |
295 | for (j = 0; j < rename_src_nr; j++) { |
296 | struct diff_filespec *one = rename_src[j].one; | |
297 | struct diff_score *m = &mx[base+j]; | |
298 | m->src = j; | |
299 | m->dst = i; | |
300 | m->score = estimate_similarity(one, two, | |
301 | minimum_score); | |
427dcb4b JH |
302 | } |
303 | dst_cnt++; | |
304 | } | |
305 | /* cost matrix sorted by most to least similar pair */ | |
306 | qsort(mx, num_create * num_src, sizeof(*mx), score_compare); | |
307 | for (i = 0; i < num_create * num_src; i++) { | |
25d5ea41 JH |
308 | struct diff_rename_dst *dst = &rename_dst[mx[i].dst]; |
309 | if (dst->pair) | |
310 | continue; /* already done, either exact or fuzzy. */ | |
427dcb4b | 311 | if (mx[i].score < minimum_score) |
15d061b4 | 312 | break; /* there is no more usable pair. */ |
25d5ea41 | 313 | record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score); |
427dcb4b JH |
314 | } |
315 | free(mx); | |
25d5ea41 | 316 | diff_debug_queue("done detecting fuzzy", &renq); |
427dcb4b | 317 | |
15d061b4 | 318 | cleanup: |
427dcb4b | 319 | /* At this point, we have found some renames and copies and they |
25d5ea41 | 320 | * are kept in renq. The original list is still in *q. |
427dcb4b | 321 | */ |
25d5ea41 JH |
322 | outq.queue = NULL; |
323 | outq.nr = outq.alloc = 0; | |
427dcb4b | 324 | for (i = 0; i < q->nr; i++) { |
25d5ea41 | 325 | struct diff_filepair *p = q->queue[i]; |
25d5ea41 JH |
326 | struct diff_filepair *pair_to_free = NULL; |
327 | ||
2cd68882 JH |
328 | if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
329 | /* | |
330 | * Creation | |
331 | * | |
332 | * We would output this create record if it has | |
333 | * not been turned into a rename/copy already. | |
334 | */ | |
335 | struct diff_rename_dst *dst = | |
336 | locate_rename_dst(p->two, 0); | |
337 | if (dst && dst->pair) { | |
25d5ea41 JH |
338 | diff_q(&outq, dst->pair); |
339 | pair_to_free = p; | |
340 | } | |
341 | else | |
2cd68882 JH |
342 | /* no matching rename/copy source, so |
343 | * record this as a creation. | |
25d5ea41 JH |
344 | */ |
345 | diff_q(&outq, p); | |
427dcb4b | 346 | } |
2cd68882 JH |
347 | else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) { |
348 | /* | |
349 | * Deletion | |
350 | * | |
f345b0a0 JH |
351 | * We would output this delete record if: |
352 | * | |
353 | * (1) this is a broken delete and the counterpart | |
354 | * broken create remains in the output; or | |
355 | * (2) this is not a broken delete, and renq does | |
356 | * not have a rename/copy to move p->one->path | |
357 | * out. | |
358 | * | |
359 | * Otherwise, the counterpart broken create | |
360 | * has been turned into a rename-edit; or | |
361 | * delete did not have a matching create to | |
362 | * begin with. | |
2cd68882 | 363 | */ |
f345b0a0 JH |
364 | if (DIFF_PAIR_BROKEN(p)) { |
365 | /* broken delete */ | |
366 | struct diff_rename_dst *dst = | |
367 | locate_rename_dst(p->one, 0); | |
368 | if (dst && dst->pair) | |
369 | /* counterpart is now rename/copy */ | |
370 | pair_to_free = p; | |
371 | } | |
372 | else { | |
373 | for (j = 0; j < renq.nr; j++) | |
374 | if (!strcmp(renq.queue[j]->one->path, | |
375 | p->one->path)) | |
376 | break; | |
377 | if (j < renq.nr) | |
378 | /* this path remains */ | |
379 | pair_to_free = p; | |
380 | } | |
2cd68882 JH |
381 | |
382 | if (pair_to_free) | |
383 | ; | |
384 | else | |
385 | diff_q(&outq, p); | |
386 | } | |
25d5ea41 | 387 | else if (!diff_unmodified_pair(p)) |
15d061b4 | 388 | /* all the usual ones need to be kept */ |
25d5ea41 | 389 | diff_q(&outq, p); |
15d061b4 JH |
390 | else |
391 | /* no need to keep unmodified pairs */ | |
392 | pair_to_free = p; | |
393 | ||
226406f6 JH |
394 | if (pair_to_free) |
395 | diff_free_filepair(pair_to_free); | |
427dcb4b | 396 | } |
25d5ea41 | 397 | diff_debug_queue("done copying original", &outq); |
427dcb4b | 398 | |
25d5ea41 JH |
399 | free(renq.queue); |
400 | free(q->queue); | |
401 | *q = outq; | |
402 | diff_debug_queue("done collapsing", q); | |
427dcb4b | 403 | |
25d5ea41 JH |
404 | free(rename_dst); |
405 | rename_dst = NULL; | |
406 | rename_dst_nr = rename_dst_alloc = 0; | |
407 | free(rename_src); | |
408 | rename_src = NULL; | |
409 | rename_src_nr = rename_src_alloc = 0; | |
427dcb4b JH |
410 | return; |
411 | } |