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