2 * Copyright (C) 2005 Junio C Hamano
8 /* Table of rename/copy destinations */
10 static struct diff_rename_dst {
11 struct diff_filespec *two;
12 struct diff_filepair *pair;
14 static int rename_dst_nr, rename_dst_alloc;
16 static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
23 while (last > first) {
24 int next = (last + first) >> 1;
25 struct diff_rename_dst *dst = &(rename_dst[next]);
26 int cmp = strcmp(two->path, dst->two->path);
38 /* insert to make it at "first" */
39 if (rename_dst_alloc <= rename_dst_nr) {
40 rename_dst_alloc = alloc_nr(rename_dst_alloc);
41 rename_dst = xrealloc(rename_dst,
42 rename_dst_alloc * sizeof(*rename_dst));
45 if (first < rename_dst_nr)
46 memmove(rename_dst + first + 1, rename_dst + first,
47 (rename_dst_nr - first - 1) * sizeof(*rename_dst));
48 rename_dst[first].two = alloc_filespec(two->path);
49 fill_filespec(rename_dst[first].two, two->sha1, two->mode);
50 rename_dst[first].pair = NULL;
51 return &(rename_dst[first]);
54 /* Table of rename/copy src files */
55 static struct diff_rename_src {
56 struct diff_filespec *one;
57 unsigned short score; /* to remember the break score */
59 static int rename_src_nr, rename_src_alloc;
61 static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
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);
81 /* insert to make it at "first" */
82 if (rename_src_alloc <= rename_src_nr) {
83 rename_src_alloc = alloc_nr(rename_src_alloc);
84 rename_src = xrealloc(rename_src,
85 rename_src_alloc * sizeof(*rename_src));
88 if (first < rename_src_nr)
89 memmove(rename_src + first + 1, rename_src + first,
90 (rename_src_nr - first - 1) * sizeof(*rename_src));
91 rename_src[first].one = one;
92 rename_src[first].score = score;
93 return &(rename_src[first]);
96 static int is_exact_match(struct diff_filespec *src,
97 struct diff_filespec *dst,
100 if (src->sha1_valid && dst->sha1_valid &&
101 !hashcmp(src->sha1, dst->sha1))
105 if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
107 if (src->size != dst->size)
109 if (src->sha1_valid && dst->sha1_valid)
110 return !hashcmp(src->sha1, dst->sha1);
111 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
113 if (src->size == dst->size &&
114 !memcmp(src->data, dst->data, src->size))
119 static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
121 int src_len = strlen(src->path), dst_len = strlen(dst->path);
122 while (src_len && dst_len) {
123 char c1 = src->path[--src_len];
124 char c2 = dst->path[--dst_len];
130 return (!src_len || src->path[src_len - 1] == '/') &&
131 (!dst_len || dst->path[dst_len - 1] == '/');
135 int src; /* index in rename_src */
136 int dst; /* index in rename_dst */
141 static int estimate_similarity(struct diff_filespec *src,
142 struct diff_filespec *dst,
145 /* src points at a file that existed in the original tree (or
146 * optionally a file in the destination tree) and dst points
147 * at a newly created file. They may be quite similar, in which
148 * case we want to say src is renamed to dst or src is copied into
149 * dst, and then some edit has been applied to dst.
151 * Compare them and return how similar they are, representing
152 * the score as an integer between 0 and MAX_SCORE.
154 * When there is an exact match, it is considered a better
155 * match than anything else; the destination does not even
156 * call into this function in that case.
158 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
159 unsigned long delta_limit;
162 /* We deal only with regular files. Symlink renames are handled
163 * only when they are exact matches --- in other words, no edits
166 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
169 max_size = ((src->size > dst->size) ? src->size : dst->size);
170 base_size = ((src->size < dst->size) ? src->size : dst->size);
171 delta_size = max_size - base_size;
173 /* We would not consider edits that change the file size so
174 * drastically. delta_size must be smaller than
175 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
177 * Note that base_size == 0 case is handled here already
178 * and the final score computation below would not have a
179 * divide-by-zero issue.
181 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
184 if ((!src->cnt_data && diff_populate_filespec(src, 0))
185 || (!dst->cnt_data && diff_populate_filespec(dst, 0)))
186 return 0; /* error but caught downstream */
189 delta_limit = (unsigned long)
190 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
191 if (diffcore_count_changes(src, dst,
192 &src->cnt_data, &dst->cnt_data,
194 &src_copied, &literal_added))
197 /* How similar are they?
198 * what percentage of material in dst are from source?
201 score = 0; /* should not happen */
203 score = (int)(src_copied * MAX_SCORE / max_size);
207 static void record_rename_pair(int dst_index, int src_index, int score)
209 struct diff_filespec *src, *dst;
210 struct diff_filepair *dp;
212 if (rename_dst[dst_index].pair)
213 die("internal error: dst already matched.");
215 src = rename_src[src_index].one;
219 dst = rename_dst[dst_index].two;
222 dp = diff_queue(NULL, src, dst);
223 dp->renamed_pair = 1;
224 if (!strcmp(src->path, dst->path))
225 dp->score = rename_src[src_index].score;
228 rename_dst[dst_index].pair = dp;
232 * We sort the rename similarity matrix with the score, in descending
233 * order (the most similar first).
235 static int score_compare(const void *a_, const void *b_)
237 const struct diff_score *a = a_, *b = b_;
239 if (a->score == b->score)
240 return b->name_score - a->name_score;
242 return b->score - a->score;
246 * Find exact renames first.
248 * The first round matches up the up-to-date entries,
249 * and then during the second round we try to match
250 * cache-dirty entries as well.
252 * Note: the rest of the rename logic depends on this
253 * phase also populating all the filespecs for any
254 * entry that isn't matched up with an exact rename,
255 * see "is_exact_match()".
257 static int find_exact_renames(void)
259 int rename_count = 0;
262 for (contents_too = 0; contents_too < 2; contents_too++) {
265 for (i = 0; i < rename_dst_nr; i++) {
266 struct diff_filespec *two = rename_dst[i].two;
269 if (rename_dst[i].pair)
270 continue; /* dealt with an earlier round */
271 for (j = 0; j < rename_src_nr; j++) {
273 struct diff_filespec *one = rename_src[j].one;
274 if (!is_exact_match(one, two, contents_too))
277 /* see if there is a basename match, too */
278 for (k = j; k < rename_src_nr; k++) {
279 one = rename_src[k].one;
280 if (basename_same(one, two) &&
281 is_exact_match(one, two,
288 record_rename_pair(i, j, (int)MAX_SCORE);
290 break; /* we are done with this entry */
297 void diffcore_rename(struct diff_options *options)
299 int detect_rename = options->detect_rename;
300 int minimum_score = options->rename_score;
301 int rename_limit = options->rename_limit;
302 struct diff_queue_struct *q = &diff_queued_diff;
303 struct diff_queue_struct outq;
304 struct diff_score *mx;
305 int i, j, rename_count;
306 int num_create, num_src, dst_cnt;
309 minimum_score = DEFAULT_RENAME_SCORE;
311 for (i = 0; i < q->nr; i++) {
312 struct diff_filepair *p = q->queue[i];
313 if (!DIFF_FILE_VALID(p->one)) {
314 if (!DIFF_FILE_VALID(p->two))
315 continue; /* unmerged */
316 else if (options->single_follow &&
317 strcmp(options->single_follow, p->two->path))
318 continue; /* not interested */
320 locate_rename_dst(p->two, 1);
322 else if (!DIFF_FILE_VALID(p->two)) {
324 * If the source is a broken "delete", and
325 * they did not really want to get broken,
326 * that means the source actually stays.
327 * So we increment the "rename_used" score
328 * by one, to indicate ourselves as a user
330 if (p->broken_pair && !p->score)
331 p->one->rename_used++;
332 register_rename_src(p->one, p->score);
334 else if (detect_rename == DIFF_DETECT_COPY) {
336 * Increment the "rename_used" score by
337 * one, to indicate ourselves as a user.
339 p->one->rename_used++;
340 register_rename_src(p->one, p->score);
343 if (rename_dst_nr == 0 || rename_src_nr == 0)
344 goto cleanup; /* nothing to do */
347 * This basically does a test for the rename matrix not
348 * growing larger than a "rename_limit" square matrix, ie:
350 * rename_dst_nr * rename_src_nr > rename_limit * rename_limit
352 * but handles the potential overflow case specially (and we
353 * assume at least 32-bit integers)
355 if (rename_limit <= 0 || rename_limit > 32767)
356 rename_limit = 32767;
357 if (rename_dst_nr > rename_limit && rename_src_nr > rename_limit)
359 if (rename_dst_nr * rename_src_nr > rename_limit * rename_limit)
363 * We really want to cull the candidates list early
364 * with cheap tests in order to avoid doing deltas.
366 rename_count = find_exact_renames();
368 /* Have we run out the created file pool? If so we can avoid
369 * doing the delta matrix altogether.
371 if (rename_count == rename_dst_nr)
374 if (minimum_score == MAX_SCORE)
377 num_create = (rename_dst_nr - rename_count);
378 num_src = rename_src_nr;
379 mx = xmalloc(sizeof(*mx) * num_create * num_src);
380 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
381 int base = dst_cnt * num_src;
382 struct diff_filespec *two = rename_dst[i].two;
383 if (rename_dst[i].pair)
384 continue; /* dealt with exact match already. */
385 for (j = 0; j < rename_src_nr; j++) {
386 struct diff_filespec *one = rename_src[j].one;
387 struct diff_score *m = &mx[base+j];
390 m->score = estimate_similarity(one, two,
392 m->name_score = basename_same(one, two);
393 diff_free_filespec_blob(one);
395 /* We do not need the text anymore */
396 diff_free_filespec_blob(two);
399 /* cost matrix sorted by most to least similar pair */
400 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
401 for (i = 0; i < num_create * num_src; i++) {
402 struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
404 continue; /* already done, either exact or fuzzy. */
405 if (mx[i].score < minimum_score)
406 break; /* there is no more usable pair. */
407 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
413 /* At this point, we have found some renames and copies and they
414 * are recorded in rename_dst. The original list is still in *q.
417 outq.nr = outq.alloc = 0;
418 for (i = 0; i < q->nr; i++) {
419 struct diff_filepair *p = q->queue[i];
420 struct diff_filepair *pair_to_free = NULL;
422 if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
426 * We would output this create record if it has
427 * not been turned into a rename/copy already.
429 struct diff_rename_dst *dst =
430 locate_rename_dst(p->two, 0);
431 if (dst && dst->pair) {
432 diff_q(&outq, dst->pair);
436 /* no matching rename/copy source, so
437 * record this as a creation.
441 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
445 * We would output this delete record if:
447 * (1) this is a broken delete and the counterpart
448 * broken create remains in the output; or
449 * (2) this is not a broken delete, and rename_dst
450 * does not have a rename/copy to move p->one->path
453 * Otherwise, the counterpart broken create
454 * has been turned into a rename-edit; or
455 * delete did not have a matching create to
458 if (DIFF_PAIR_BROKEN(p)) {
460 struct diff_rename_dst *dst =
461 locate_rename_dst(p->one, 0);
462 if (dst && dst->pair)
463 /* counterpart is now rename/copy */
467 if (p->one->rename_used)
468 /* this path remains */
477 else if (!diff_unmodified_pair(p))
478 /* all the usual ones need to be kept */
481 /* no need to keep unmodified pairs */
485 diff_free_filepair(pair_to_free);
487 diff_debug_queue("done copying original", &outq);
491 diff_debug_queue("done collapsing", q);
493 for (i = 0; i < rename_dst_nr; i++)
494 free_filespec(rename_dst[i].two);
498 rename_dst_nr = rename_dst_alloc = 0;
501 rename_src_nr = rename_src_alloc = 0;