]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-rename.c
GIT 0.99.9i aka 1.0rc2
[thirdparty/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));
5098bafb
JH
50 rename_dst[first].two = alloc_filespec(two->path);
51 fill_filespec(rename_dst[first].two, two->sha1, two->mode);
25d5ea41
JH
52 rename_dst[first].pair = NULL;
53 return &(rename_dst[first]);
427dcb4b
JH
54}
55
15d061b4 56/* Table of rename/copy src files */
25d5ea41
JH
57static struct diff_rename_src {
58 struct diff_filespec *one;
6bac10d8 59 unsigned src_path_left : 1;
25d5ea41
JH
60} *rename_src;
61static int rename_src_nr, rename_src_alloc;
427dcb4b 62
15d061b4 63static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
6bac10d8 64 int src_path_left)
25d5ea41
JH
65{
66 int first, last;
67
68 first = 0;
69 last = rename_src_nr;
70 while (last > first) {
71 int next = (last + first) >> 1;
72 struct diff_rename_src *src = &(rename_src[next]);
73 int cmp = strcmp(one->path, src->one->path);
74 if (!cmp)
75 return src;
76 if (cmp < 0) {
77 last = next;
78 continue;
79 }
80 first = next+1;
81 }
15d061b4 82
25d5ea41
JH
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;
6bac10d8 94 rename_src[first].src_path_left = src_path_left;
25d5ea41 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;
f0c6b2a2
JH
103 if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
104 return 0;
105 if (src->size != dst->size)
106 return 0;
107 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
427dcb4b
JH
108 return 0;
109 if (src->size == dst->size &&
110 !memcmp(src->data, dst->data, src->size))
111 return 1;
112 return 0;
113}
114
115struct diff_score {
25d5ea41
JH
116 int src; /* index in rename_src */
117 int dst; /* index in rename_dst */
427dcb4b 118 int score;
427dcb4b
JH
119};
120
121static int estimate_similarity(struct diff_filespec *src,
122 struct diff_filespec *dst,
123 int minimum_score)
124{
125 /* src points at a file that existed in the original tree (or
126 * optionally a file in the destination tree) and dst points
127 * at a newly created file. They may be quite similar, in which
128 * case we want to say src is renamed to dst or src is copied into
129 * dst, and then some edit has been applied to dst.
130 *
131 * Compare them and return how similar they are, representing
f0c6b2a2
JH
132 * the score as an integer between 0 and MAX_SCORE.
133 *
134 * When there is an exact match, it is considered a better
135 * match than anything else; the destination does not even
136 * call into this function in that case.
427dcb4b
JH
137 */
138 void *delta;
355e76a4 139 unsigned long delta_size, base_size, src_copied, literal_added;
75c660ac 140 unsigned long delta_limit;
427dcb4b
JH
141 int score;
142
60896c7b
JH
143 /* We deal only with regular files. Symlink renames are handled
144 * only when they are exact matches --- in other words, no edits
145 * after renaming.
146 */
147 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
148 return 0;
149
427dcb4b
JH
150 delta_size = ((src->size < dst->size) ?
151 (dst->size - src->size) : (src->size - dst->size));
58b103f5 152 base_size = ((src->size < dst->size) ? src->size : dst->size);
427dcb4b 153
58b103f5
JH
154 /* We would not consider edits that change the file size so
155 * drastically. delta_size must be smaller than
cd1870ed 156 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
f0c6b2a2 157 *
58b103f5
JH
158 * Note that base_size == 0 case is handled here already
159 * and the final score computation below would not have a
160 * divide-by-zero issue.
427dcb4b 161 */
cd1870ed 162 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
427dcb4b
JH
163 return 0;
164
f0c6b2a2
JH
165 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
166 return 0; /* error but caught downstream */
167
75c660ac 168 delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE;
427dcb4b
JH
169 delta = diff_delta(src->data, src->size,
170 dst->data, dst->size,
75c660ac
JH
171 &delta_size, delta_limit);
172 if (!delta)
173 /* If delta_limit is exceeded, we have too much differences */
174 return 0;
85976974
JH
175
176 /* A delta that has a lot of literal additions would have
177 * big delta_size no matter what else it does.
58b103f5 178 */
a00d7d10 179 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
85976974
JH
180 return 0;
181
182 /* Estimate the edit size by interpreting delta. */
355e76a4
JH
183 if (count_delta(delta, delta_size, &src_copied, &literal_added)) {
184 free(delta);
85976974 185 return 0;
355e76a4
JH
186 }
187 free(delta);
188
189 /* Extent of damage */
190 if (src->size + literal_added < src_copied)
191 delta_size = 0;
192 else
193 delta_size = (src->size - src_copied) + literal_added;
427dcb4b 194
58b103f5
JH
195 /*
196 * Now we will give some score to it. 100% edit gets 0 points
197 * and 0% edit gets MAX_SCORE points.
427dcb4b 198 */
58b103f5 199 score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);
427dcb4b
JH
200 if (score < 0) return 0;
201 if (MAX_SCORE < score) return MAX_SCORE;
202 return score;
203}
204
5098bafb 205static void record_rename_pair(int dst_index, int src_index, int score)
427dcb4b 206{
25d5ea41
JH
207 struct diff_filespec *one, *two, *src, *dst;
208 struct diff_filepair *dp;
f7c1512a 209
25d5ea41
JH
210 if (rename_dst[dst_index].pair)
211 die("internal error: dst already matched.");
427dcb4b 212
25d5ea41
JH
213 src = rename_src[src_index].one;
214 one = alloc_filespec(src->path);
215 fill_filespec(one, src->sha1, src->mode);
427dcb4b 216
25d5ea41
JH
217 dst = rename_dst[dst_index].two;
218 two = alloc_filespec(dst->path);
219 fill_filespec(two, dst->sha1, dst->mode);
427dcb4b 220
5098bafb 221 dp = diff_queue(NULL, one, two);
01c4e70f 222 dp->score = score;
6bac10d8 223 dp->source_stays = rename_src[src_index].src_path_left;
25d5ea41 224 rename_dst[dst_index].pair = dp;
427dcb4b
JH
225}
226
227/*
228 * We sort the rename similarity matrix with the score, in descending
15d061b4 229 * order (the most similar first).
427dcb4b
JH
230 */
231static int score_compare(const void *a_, const void *b_)
232{
233 const struct diff_score *a = a_, *b = b_;
234 return b->score - a->score;
235}
236
6bac10d8
JH
237static int compute_stays(struct diff_queue_struct *q,
238 struct diff_filespec *one)
239{
240 int i;
241 for (i = 0; i < q->nr; i++) {
242 struct diff_filepair *p = q->queue[i];
243 if (strcmp(one->path, p->two->path))
244 continue;
245 if (DIFF_PAIR_RENAME(p)) {
246 return 0; /* something else is renamed into this */
247 }
248 }
249 return 1;
250}
251
8082d8d3 252void diffcore_rename(struct diff_options *options)
427dcb4b 253{
8082d8d3
JH
254 int detect_rename = options->detect_rename;
255 int minimum_score = options->rename_score;
256 int rename_limit = options->rename_limit;
38c6f780 257 struct diff_queue_struct *q = &diff_queued_diff;
5098bafb 258 struct diff_queue_struct outq;
427dcb4b 259 struct diff_score *mx;
5098bafb 260 int i, j, rename_count;
25d5ea41 261 int num_create, num_src, dst_cnt;
427dcb4b 262
26dee0ad 263 if (!minimum_score)
f345b0a0 264 minimum_score = DEFAULT_RENAME_SCORE;
5098bafb 265 rename_count = 0;
427dcb4b 266
427dcb4b 267 for (i = 0; i < q->nr; i++) {
52e95789 268 struct diff_filepair *p = q->queue[i];
81e50eab
JH
269 if (!DIFF_FILE_VALID(p->one))
270 if (!DIFF_FILE_VALID(p->two))
60896c7b 271 continue; /* unmerged */
427dcb4b 272 else
25d5ea41 273 locate_rename_dst(p->two, 1);
2210100a
JH
274 else if (!DIFF_FILE_VALID(p->two)) {
275 /* If the source is a broken "delete", and
276 * they did not really want to get broken,
277 * that means the source actually stays.
278 */
279 int stays = (p->broken_pair && !p->score);
280 register_rename_src(p->one, stays);
281 }
15d061b4
JH
282 else if (detect_rename == DIFF_DETECT_COPY)
283 register_rename_src(p->one, 1);
427dcb4b 284 }
8082d8d3
JH
285 if (rename_dst_nr == 0 ||
286 (0 <= rename_limit && rename_limit < rename_dst_nr))
427dcb4b
JH
287 goto cleanup; /* nothing to do */
288
289 /* We really want to cull the candidates list early
290 * with cheap tests in order to avoid doing deltas.
427dcb4b 291 */
25d5ea41
JH
292 for (i = 0; i < rename_dst_nr; i++) {
293 struct diff_filespec *two = rename_dst[i].two;
294 for (j = 0; j < rename_src_nr; j++) {
295 struct diff_filespec *one = rename_src[j].one;
296 if (!is_exact_match(one, two))
297 continue;
5098bafb
JH
298 record_rename_pair(i, j, MAX_SCORE);
299 rename_count++;
25d5ea41 300 break; /* we are done with this entry */
427dcb4b
JH
301 }
302 }
427dcb4b
JH
303
304 /* Have we run out the created file pool? If so we can avoid
305 * doing the delta matrix altogether.
306 */
5098bafb 307 if (rename_count == rename_dst_nr)
15d061b4 308 goto cleanup;
427dcb4b 309
5098bafb 310 num_create = (rename_dst_nr - rename_count);
25d5ea41 311 num_src = rename_src_nr;
427dcb4b 312 mx = xmalloc(sizeof(*mx) * num_create * num_src);
25d5ea41 313 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
427dcb4b 314 int base = dst_cnt * num_src;
25d5ea41
JH
315 struct diff_filespec *two = rename_dst[i].two;
316 if (rename_dst[i].pair)
427dcb4b 317 continue; /* dealt with exact match already. */
25d5ea41
JH
318 for (j = 0; j < rename_src_nr; j++) {
319 struct diff_filespec *one = rename_src[j].one;
320 struct diff_score *m = &mx[base+j];
321 m->src = j;
322 m->dst = i;
323 m->score = estimate_similarity(one, two,
324 minimum_score);
427dcb4b
JH
325 }
326 dst_cnt++;
327 }
328 /* cost matrix sorted by most to least similar pair */
329 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
330 for (i = 0; i < num_create * num_src; i++) {
25d5ea41
JH
331 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
332 if (dst->pair)
333 continue; /* already done, either exact or fuzzy. */
427dcb4b 334 if (mx[i].score < minimum_score)
15d061b4 335 break; /* there is no more usable pair. */
5098bafb
JH
336 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
337 rename_count++;
427dcb4b
JH
338 }
339 free(mx);
427dcb4b 340
15d061b4 341 cleanup:
427dcb4b 342 /* At this point, we have found some renames and copies and they
5098bafb 343 * are recorded in rename_dst. The original list is still in *q.
427dcb4b 344 */
25d5ea41
JH
345 outq.queue = NULL;
346 outq.nr = outq.alloc = 0;
427dcb4b 347 for (i = 0; i < q->nr; i++) {
25d5ea41 348 struct diff_filepair *p = q->queue[i];
25d5ea41
JH
349 struct diff_filepair *pair_to_free = NULL;
350
2cd68882
JH
351 if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
352 /*
353 * Creation
354 *
355 * We would output this create record if it has
356 * not been turned into a rename/copy already.
357 */
358 struct diff_rename_dst *dst =
359 locate_rename_dst(p->two, 0);
360 if (dst && dst->pair) {
25d5ea41
JH
361 diff_q(&outq, dst->pair);
362 pair_to_free = p;
363 }
364 else
2cd68882
JH
365 /* no matching rename/copy source, so
366 * record this as a creation.
25d5ea41
JH
367 */
368 diff_q(&outq, p);
427dcb4b 369 }
2cd68882
JH
370 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
371 /*
372 * Deletion
373 *
f345b0a0
JH
374 * We would output this delete record if:
375 *
376 * (1) this is a broken delete and the counterpart
377 * broken create remains in the output; or
5098bafb
JH
378 * (2) this is not a broken delete, and rename_dst
379 * does not have a rename/copy to move p->one->path
380 * out of existence.
f345b0a0
JH
381 *
382 * Otherwise, the counterpart broken create
383 * has been turned into a rename-edit; or
384 * delete did not have a matching create to
385 * begin with.
2cd68882 386 */
f345b0a0
JH
387 if (DIFF_PAIR_BROKEN(p)) {
388 /* broken delete */
389 struct diff_rename_dst *dst =
390 locate_rename_dst(p->one, 0);
391 if (dst && dst->pair)
392 /* counterpart is now rename/copy */
393 pair_to_free = p;
394 }
395 else {
5098bafb
JH
396 for (j = 0; j < rename_dst_nr; j++) {
397 if (!rename_dst[j].pair)
398 continue;
399 if (strcmp(rename_dst[j].pair->
400 one->path,
401 p->one->path))
402 continue;
403 break;
404 }
405 if (j < rename_dst_nr)
f345b0a0
JH
406 /* this path remains */
407 pair_to_free = p;
408 }
2cd68882
JH
409
410 if (pair_to_free)
411 ;
412 else
413 diff_q(&outq, p);
414 }
25d5ea41 415 else if (!diff_unmodified_pair(p))
15d061b4 416 /* all the usual ones need to be kept */
25d5ea41 417 diff_q(&outq, p);
15d061b4
JH
418 else
419 /* no need to keep unmodified pairs */
420 pair_to_free = p;
421
226406f6
JH
422 if (pair_to_free)
423 diff_free_filepair(pair_to_free);
427dcb4b 424 }
25d5ea41 425 diff_debug_queue("done copying original", &outq);
427dcb4b 426
25d5ea41
JH
427 free(q->queue);
428 *q = outq;
429 diff_debug_queue("done collapsing", q);
427dcb4b 430
6bac10d8
JH
431 /* We need to see which rename source really stays here;
432 * earlier we only checked if the path is left in the result,
433 * but even if a path remains in the result, if that is coming
434 * from copying something else on top of it, then the original
435 * source is lost and does not stay.
436 */
437 for (i = 0; i < q->nr; i++) {
438 struct diff_filepair *p = q->queue[i];
439 if (DIFF_PAIR_RENAME(p) && p->source_stays) {
440 /* If one appears as the target of a rename-copy,
441 * then mark p->source_stays = 0; otherwise
442 * leave it as is.
443 */
444 p->source_stays = compute_stays(q, p->one);
445 }
446 }
447
5098bafb
JH
448 for (i = 0; i < rename_dst_nr; i++) {
449 diff_free_filespec_data(rename_dst[i].two);
450 free(rename_dst[i].two);
451 }
452
25d5ea41
JH
453 free(rename_dst);
454 rename_dst = NULL;
455 rename_dst_nr = rename_dst_alloc = 0;
456 free(rename_src);
457 rename_src = NULL;
458 rename_src_nr = rename_src_alloc = 0;
427dcb4b
JH
459 return;
460}