1/*2* Copyright (C) 2005 Junio C Hamano3*/4#include "cache.h"5#include "diff.h"6#include "diffcore.h"7#include "delta.h"8#include "count-delta.h"910/* Table of rename/copy destinations */1112static struct diff_rename_dst {13struct diff_filespec *two;14struct diff_filepair *pair;15} *rename_dst;16static int rename_dst_nr, rename_dst_alloc;1718static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,19int insert_ok)20{21int first, last;2223first = 0;24last = rename_dst_nr;25while (last > first) {26int next = (last + first) >> 1;27struct diff_rename_dst *dst = &(rename_dst[next]);28int cmp = strcmp(two->path, dst->two->path);29if (!cmp)30return dst;31if (cmp < 0) {32last = next;33continue;34}35first = next+1;36}37/* not found */38if (!insert_ok)39return NULL;40/* insert to make it at "first" */41if (rename_dst_alloc <= rename_dst_nr) {42rename_dst_alloc = alloc_nr(rename_dst_alloc);43rename_dst = xrealloc(rename_dst,44rename_dst_alloc * sizeof(*rename_dst));45}46rename_dst_nr++;47if (first < rename_dst_nr)48memmove(rename_dst + first + 1, rename_dst + first,49(rename_dst_nr - first - 1) * sizeof(*rename_dst));50rename_dst[first].two = two;51rename_dst[first].pair = NULL;52return &(rename_dst[first]);53}5455/* Table of rename/copy src files */56static struct diff_rename_src {57struct diff_filespec *one;58unsigned src_path_left : 1;59} *rename_src;60static int rename_src_nr, rename_src_alloc;6162static struct diff_rename_src *register_rename_src(struct diff_filespec *one,63int src_path_left)64{65int first, last;6667first = 0;68last = rename_src_nr;69while (last > first) {70int next = (last + first) >> 1;71struct diff_rename_src *src = &(rename_src[next]);72int cmp = strcmp(one->path, src->one->path);73if (!cmp)74return src;75if (cmp < 0) {76last = next;77continue;78}79first = next+1;80}8182/* insert to make it at "first" */83if (rename_src_alloc <= rename_src_nr) {84rename_src_alloc = alloc_nr(rename_src_alloc);85rename_src = xrealloc(rename_src,86rename_src_alloc * sizeof(*rename_src));87}88rename_src_nr++;89if (first < rename_src_nr)90memmove(rename_src + first + 1, rename_src + first,91(rename_src_nr - first - 1) * sizeof(*rename_src));92rename_src[first].one = one;93rename_src[first].src_path_left = src_path_left;94return &(rename_src[first]);95}9697static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)98{99if (src->sha1_valid && dst->sha1_valid &&100!memcmp(src->sha1, dst->sha1, 20))101return 1;102if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))103return 0;104if (src->size != dst->size)105return 0;106if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))107return 0;108if (src->size == dst->size &&109!memcmp(src->data, dst->data, src->size))110return 1;111return 0;112}113114struct diff_score {115int src; /* index in rename_src */116int dst; /* index in rename_dst */117int score;118};119120static int estimate_similarity(struct diff_filespec *src,121struct diff_filespec *dst,122int minimum_score)123{124/* src points at a file that existed in the original tree (or125* optionally a file in the destination tree) and dst points126* at a newly created file. They may be quite similar, in which127* case we want to say src is renamed to dst or src is copied into128* dst, and then some edit has been applied to dst.129*130* Compare them and return how similar they are, representing131* the score as an integer between 0 and MAX_SCORE.132*133* When there is an exact match, it is considered a better134* match than anything else; the destination does not even135* call into this function in that case.136*/137void *delta;138unsigned long delta_size, base_size, src_copied, literal_added;139unsigned long delta_limit;140int score;141142/* We deal only with regular files. Symlink renames are handled143* only when they are exact matches --- in other words, no edits144* after renaming.145*/146if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))147return 0;148149delta_size = ((src->size < dst->size) ?150(dst->size - src->size) : (src->size - dst->size));151base_size = ((src->size < dst->size) ? src->size : dst->size);152153/* We would not consider edits that change the file size so154* drastically. delta_size must be smaller than155* (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).156*157* Note that base_size == 0 case is handled here already158* and the final score computation below would not have a159* divide-by-zero issue.160*/161if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)162return 0;163164if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))165return 0; /* error but caught downstream */166167delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE;168delta = diff_delta(src->data, src->size,169dst->data, dst->size,170&delta_size, delta_limit);171if (!delta)172/* If delta_limit is exceeded, we have too much differences */173return 0;174175/* A delta that has a lot of literal additions would have176* big delta_size no matter what else it does.177*/178if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)179return 0;180181/* Estimate the edit size by interpreting delta. */182if (count_delta(delta, delta_size, &src_copied, &literal_added)) {183free(delta);184return 0;185}186free(delta);187188/* Extent of damage */189if (src->size + literal_added < src_copied)190delta_size = 0;191else192delta_size = (src->size - src_copied) + literal_added;193194/*195* Now we will give some score to it. 100% edit gets 0 points196* and 0% edit gets MAX_SCORE points.197*/198score = MAX_SCORE - (MAX_SCORE * delta_size / base_size);199if (score < 0) return 0;200if (MAX_SCORE < score) return MAX_SCORE;201return score;202}203204static void record_rename_pair(struct diff_queue_struct *renq,205int dst_index, int src_index, int score)206{207struct diff_filespec *one, *two, *src, *dst;208struct diff_filepair *dp;209210if (rename_dst[dst_index].pair)211die("internal error: dst already matched.");212213src = rename_src[src_index].one;214one = alloc_filespec(src->path);215fill_filespec(one, src->sha1, src->mode);216217dst = rename_dst[dst_index].two;218two = alloc_filespec(dst->path);219fill_filespec(two, dst->sha1, dst->mode);220221dp = diff_queue(renq, one, two);222dp->score = score;223dp->source_stays = rename_src[src_index].src_path_left;224rename_dst[dst_index].pair = dp;225}226227/*228* We sort the rename similarity matrix with the score, in descending229* order (the most similar first).230*/231static int score_compare(const void *a_, const void *b_)232{233const struct diff_score *a = a_, *b = b_;234return b->score - a->score;235}236237static int compute_stays(struct diff_queue_struct *q,238struct diff_filespec *one)239{240int i;241for (i = 0; i < q->nr; i++) {242struct diff_filepair *p = q->queue[i];243if (strcmp(one->path, p->two->path))244continue;245if (DIFF_PAIR_RENAME(p)) {246return 0; /* something else is renamed into this */247}248}249return 1;250}251252void diffcore_rename(int detect_rename, int minimum_score)253{254struct diff_queue_struct *q = &diff_queued_diff;255struct diff_queue_struct renq, outq;256struct diff_score *mx;257int i, j;258int num_create, num_src, dst_cnt;259260if (!minimum_score)261minimum_score = DEFAULT_RENAME_SCORE;262renq.queue = NULL;263renq.nr = renq.alloc = 0;264265for (i = 0; i < q->nr; i++) {266struct diff_filepair *p = q->queue[i];267if (!DIFF_FILE_VALID(p->one))268if (!DIFF_FILE_VALID(p->two))269continue; /* unmerged */270else271locate_rename_dst(p->two, 1);272else if (!DIFF_FILE_VALID(p->two)) {273/* If the source is a broken "delete", and274* they did not really want to get broken,275* that means the source actually stays.276*/277int stays = (p->broken_pair && !p->score);278register_rename_src(p->one, stays);279}280else if (detect_rename == DIFF_DETECT_COPY)281register_rename_src(p->one, 1);282}283if (rename_dst_nr == 0)284goto cleanup; /* nothing to do */285286/* We really want to cull the candidates list early287* with cheap tests in order to avoid doing deltas.288*/289for (i = 0; i < rename_dst_nr; i++) {290struct diff_filespec *two = rename_dst[i].two;291for (j = 0; j < rename_src_nr; j++) {292struct diff_filespec *one = rename_src[j].one;293if (!is_exact_match(one, two))294continue;295record_rename_pair(&renq, i, j, MAX_SCORE);296break; /* we are done with this entry */297}298}299diff_debug_queue("done detecting exact", &renq);300301/* Have we run out the created file pool? If so we can avoid302* doing the delta matrix altogether.303*/304if (renq.nr == rename_dst_nr)305goto cleanup;306307num_create = (rename_dst_nr - renq.nr);308num_src = rename_src_nr;309mx = xmalloc(sizeof(*mx) * num_create * num_src);310for (dst_cnt = i = 0; i < rename_dst_nr; i++) {311int base = dst_cnt * num_src;312struct diff_filespec *two = rename_dst[i].two;313if (rename_dst[i].pair)314continue; /* dealt with exact match already. */315for (j = 0; j < rename_src_nr; j++) {316struct diff_filespec *one = rename_src[j].one;317struct diff_score *m = &mx[base+j];318m->src = j;319m->dst = i;320m->score = estimate_similarity(one, two,321minimum_score);322}323dst_cnt++;324}325/* cost matrix sorted by most to least similar pair */326qsort(mx, num_create * num_src, sizeof(*mx), score_compare);327for (i = 0; i < num_create * num_src; i++) {328struct diff_rename_dst *dst = &rename_dst[mx[i].dst];329if (dst->pair)330continue; /* already done, either exact or fuzzy. */331if (mx[i].score < minimum_score)332break; /* there is no more usable pair. */333record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);334}335free(mx);336diff_debug_queue("done detecting fuzzy", &renq);337338cleanup:339/* At this point, we have found some renames and copies and they340* are kept in renq. The original list is still in *q.341*/342outq.queue = NULL;343outq.nr = outq.alloc = 0;344for (i = 0; i < q->nr; i++) {345struct diff_filepair *p = q->queue[i];346struct diff_filepair *pair_to_free = NULL;347348if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {349/*350* Creation351*352* We would output this create record if it has353* not been turned into a rename/copy already.354*/355struct diff_rename_dst *dst =356locate_rename_dst(p->two, 0);357if (dst && dst->pair) {358diff_q(&outq, dst->pair);359pair_to_free = p;360}361else362/* no matching rename/copy source, so363* record this as a creation.364*/365diff_q(&outq, p);366}367else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {368/*369* Deletion370*371* We would output this delete record if:372*373* (1) this is a broken delete and the counterpart374* broken create remains in the output; or375* (2) this is not a broken delete, and renq does376* not have a rename/copy to move p->one->path377* out.378*379* Otherwise, the counterpart broken create380* has been turned into a rename-edit; or381* delete did not have a matching create to382* begin with.383*/384if (DIFF_PAIR_BROKEN(p)) {385/* broken delete */386struct diff_rename_dst *dst =387locate_rename_dst(p->one, 0);388if (dst && dst->pair)389/* counterpart is now rename/copy */390pair_to_free = p;391}392else {393for (j = 0; j < renq.nr; j++)394if (!strcmp(renq.queue[j]->one->path,395p->one->path))396break;397if (j < renq.nr)398/* this path remains */399pair_to_free = p;400}401402if (pair_to_free)403;404else405diff_q(&outq, p);406}407else if (!diff_unmodified_pair(p))408/* all the usual ones need to be kept */409diff_q(&outq, p);410else411/* no need to keep unmodified pairs */412pair_to_free = p;413414if (pair_to_free)415diff_free_filepair(pair_to_free);416}417diff_debug_queue("done copying original", &outq);418419free(renq.queue);420free(q->queue);421*q = outq;422diff_debug_queue("done collapsing", q);423424/* We need to see which rename source really stays here;425* earlier we only checked if the path is left in the result,426* but even if a path remains in the result, if that is coming427* from copying something else on top of it, then the original428* source is lost and does not stay.429*/430for (i = 0; i < q->nr; i++) {431struct diff_filepair *p = q->queue[i];432if (DIFF_PAIR_RENAME(p) && p->source_stays) {433/* If one appears as the target of a rename-copy,434* then mark p->source_stays = 0; otherwise435* leave it as is.436*/437p->source_stays = compute_stays(q, p->one);438}439}440441free(rename_dst);442rename_dst = NULL;443rename_dst_nr = rename_dst_alloc = 0;444free(rename_src);445rename_src = NULL;446rename_src_nr = rename_src_alloc = 0;447return;448}