diffcore-break.con commit Merge part of 'jc/diff' into next (213523f)
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include "cache.h"
   5#include "diff.h"
   6#include "diffcore.h"
   7
   8static int should_break(struct diff_filespec *src,
   9                        struct diff_filespec *dst,
  10                        int break_score,
  11                        int *merge_score_p)
  12{
  13        /* dst is recorded as a modification of src.  Are they so
  14         * different that we are better off recording this as a pair
  15         * of delete and create?
  16         *
  17         * There are two criteria used in this algorithm.  For the
  18         * purposes of helping later rename/copy, we take both delete
  19         * and insert into account and estimate the amount of "edit".
  20         * If the edit is very large, we break this pair so that
  21         * rename/copy can pick the pieces up to match with other
  22         * files.
  23         *
  24         * On the other hand, we would want to ignore inserts for the
  25         * pure "complete rewrite" detection.  As long as most of the
  26         * existing contents were removed from the file, it is a
  27         * complete rewrite, and if sizable chunk from the original
  28         * still remains in the result, it is not a rewrite.  It does
  29         * not matter how much or how little new material is added to
  30         * the file.
  31         *
  32         * The score we leave for such a broken filepair uses the
  33         * latter definition so that later clean-up stage can find the
  34         * pieces that should not have been broken according to the
  35         * latter definition after rename/copy runs, and merge the
  36         * broken pair that have a score lower than given criteria
  37         * back together.  The break operation itself happens
  38         * according to the former definition.
  39         *
  40         * The minimum_edit parameter tells us when to break (the
  41         * amount of "edit" required for us to consider breaking the
  42         * pair).  We leave the amount of deletion in *merge_score_p
  43         * when we return.
  44         *
  45         * The value we return is 1 if we want the pair to be broken,
  46         * or 0 if we do not.
  47         */
  48        unsigned long delta_size, base_size, src_copied, literal_added;
  49        int to_break = 0;
  50
  51        *merge_score_p = 0; /* assume no deletion --- "do not break"
  52                             * is the default.
  53                             */
  54
  55        if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
  56                return 0; /* leave symlink rename alone */
  57
  58        if (src->sha1_valid && dst->sha1_valid &&
  59            !memcmp(src->sha1, dst->sha1, 20))
  60                return 0; /* they are the same */
  61
  62        if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
  63                return 0; /* error but caught downstream */
  64
  65        base_size = ((src->size < dst->size) ? src->size : dst->size);
  66        if (base_size < MINIMUM_BREAK_SIZE)
  67                return 0; /* we do not break too small filepair */
  68
  69        if (diffcore_count_changes(src->data, src->size,
  70                                   dst->data, dst->size,
  71                                   0,
  72                                   &src_copied, &literal_added))
  73                return 0;
  74
  75        /* Compute merge-score, which is "how much is removed
  76         * from the source material".  The clean-up stage will
  77         * merge the surviving pair together if the score is
  78         * less than the minimum, after rename/copy runs.
  79         */
  80        if (src->size <= src_copied)
  81                ; /* all copied, nothing removed */
  82        else {
  83                delta_size = src->size - src_copied;
  84                *merge_score_p = delta_size * MAX_SCORE / src->size;
  85        }
  86        
  87        /* Extent of damage, which counts both inserts and
  88         * deletes.
  89         */
  90        if (src->size + literal_added <= src_copied)
  91                delta_size = 0; /* avoid wrapping around */
  92        else
  93                delta_size = (src->size - src_copied) + literal_added;
  94        
  95        /* We break if the edit exceeds the minimum.
  96         * i.e. (break_score / MAX_SCORE < delta_size / base_size)
  97         */
  98        if (break_score * base_size < delta_size * MAX_SCORE)
  99                to_break = 1;
 100
 101        return to_break;
 102}
 103
 104void diffcore_break(int break_score)
 105{
 106        struct diff_queue_struct *q = &diff_queued_diff;
 107        struct diff_queue_struct outq;
 108
 109        /* When the filepair has this much edit (insert and delete),
 110         * it is first considered to be a rewrite and broken into a
 111         * create and delete filepair.  This is to help breaking a
 112         * file that had too much new stuff added, possibly from
 113         * moving contents from another file, so that rename/copy can
 114         * match it with the other file.
 115         *
 116         * int break_score; we reuse incoming parameter for this.
 117         */
 118
 119        /* After a pair is broken according to break_score and
 120         * subjected to rename/copy, both of them may survive intact,
 121         * due to lack of suitable rename/copy peer.  Or, the caller
 122         * may be calling us without using rename/copy.  When that
 123         * happens, we merge the broken pieces back into one
 124         * modification together if the pair did not have more than
 125         * this much delete.  For this computation, we do not take
 126         * insert into account at all.  If you start from a 100-line
 127         * file and delete 97 lines of it, it does not matter if you
 128         * add 27 lines to it to make a new 30-line file or if you add
 129         * 997 lines to it to make a 1000-line file.  Either way what
 130         * you did was a rewrite of 97%.  On the other hand, if you
 131         * delete 3 lines, keeping 97 lines intact, it does not matter
 132         * if you add 3 lines to it to make a new 100-line file or if
 133         * you add 903 lines to it to make a new 1000-line file.
 134         * Either way you did a lot of additions and not a rewrite.
 135         * This merge happens to catch the latter case.  A merge_score
 136         * of 80% would be a good default value (a broken pair that
 137         * has score lower than merge_score will be merged back
 138         * together).
 139         */
 140        int merge_score;
 141        int i;
 142
 143        /* See comment on DEFAULT_BREAK_SCORE and
 144         * DEFAULT_MERGE_SCORE in diffcore.h
 145         */
 146        merge_score = (break_score >> 16) & 0xFFFF;
 147        break_score = (break_score & 0xFFFF);
 148
 149        if (!break_score)
 150                break_score = DEFAULT_BREAK_SCORE;
 151        if (!merge_score)
 152                merge_score = DEFAULT_MERGE_SCORE;
 153
 154        outq.nr = outq.alloc = 0;
 155        outq.queue = NULL;
 156
 157        for (i = 0; i < q->nr; i++) {
 158                struct diff_filepair *p = q->queue[i];
 159                int score;
 160
 161                /* We deal only with in-place edit of non directory.
 162                 * We do not break anything else.
 163                 */
 164                if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
 165                    !S_ISDIR(p->one->mode) && !S_ISDIR(p->two->mode) &&
 166                    !strcmp(p->one->path, p->two->path)) {
 167                        if (should_break(p->one, p->two,
 168                                         break_score, &score)) {
 169                                /* Split this into delete and create */
 170                                struct diff_filespec *null_one, *null_two;
 171                                struct diff_filepair *dp;
 172
 173                                /* Set score to 0 for the pair that
 174                                 * needs to be merged back together
 175                                 * should they survive rename/copy.
 176                                 * Also we do not want to break very
 177                                 * small files.
 178                                 */
 179                                if (score < merge_score)
 180                                        score = 0;
 181
 182                                /* deletion of one */
 183                                null_one = alloc_filespec(p->one->path);
 184                                dp = diff_queue(&outq, p->one, null_one);
 185                                dp->score = score;
 186                                dp->broken_pair = 1;
 187
 188                                /* creation of two */
 189                                null_two = alloc_filespec(p->two->path);
 190                                dp = diff_queue(&outq, null_two, p->two);
 191                                dp->score = score;
 192                                dp->broken_pair = 1;
 193
 194                                free(p); /* not diff_free_filepair(), we are
 195                                          * reusing one and two here.
 196                                          */
 197                                continue;
 198                        }
 199                }
 200                diff_q(&outq, p);
 201        }
 202        free(q->queue);
 203        *q = outq;
 204
 205        return;
 206}
 207
 208static void merge_broken(struct diff_filepair *p,
 209                         struct diff_filepair *pp,
 210                         struct diff_queue_struct *outq)
 211{
 212        /* p and pp are broken pairs we want to merge */
 213        struct diff_filepair *c = p, *d = pp, *dp;
 214        if (DIFF_FILE_VALID(p->one)) {
 215                /* this must be a delete half */
 216                d = p; c = pp;
 217        }
 218        /* Sanity check */
 219        if (!DIFF_FILE_VALID(d->one))
 220                die("internal error in merge #1");
 221        if (DIFF_FILE_VALID(d->two))
 222                die("internal error in merge #2");
 223        if (DIFF_FILE_VALID(c->one))
 224                die("internal error in merge #3");
 225        if (!DIFF_FILE_VALID(c->two))
 226                die("internal error in merge #4");
 227
 228        dp = diff_queue(outq, d->one, c->two);
 229        dp->score = p->score;
 230        diff_free_filespec_data(d->two);
 231        diff_free_filespec_data(c->one);
 232        free(d);
 233        free(c);
 234}
 235
 236void diffcore_merge_broken(void)
 237{
 238        struct diff_queue_struct *q = &diff_queued_diff;
 239        struct diff_queue_struct outq;
 240        int i, j;
 241
 242        outq.nr = outq.alloc = 0;
 243        outq.queue = NULL;
 244
 245        for (i = 0; i < q->nr; i++) {
 246                struct diff_filepair *p = q->queue[i];
 247                if (!p)
 248                        /* we already merged this with its peer */
 249                        continue;
 250                else if (p->broken_pair &&
 251                         !strcmp(p->one->path, p->two->path)) {
 252                        /* If the peer also survived rename/copy, then
 253                         * we merge them back together.
 254                         */
 255                        for (j = i + 1; j < q->nr; j++) {
 256                                struct diff_filepair *pp = q->queue[j];
 257                                if (pp->broken_pair &&
 258                                    !strcmp(pp->one->path, pp->two->path) &&
 259                                    !strcmp(p->one->path, pp->two->path)) {
 260                                        /* Peer survived.  Merge them */
 261                                        merge_broken(p, pp, &outq);
 262                                        q->queue[j] = NULL;
 263                                        break;
 264                                }
 265                        }
 266                        if (q->nr <= j)
 267                                /* The peer did not survive, so we keep
 268                                 * it in the output.
 269                                 */
 270                                diff_q(&outq, p);
 271                }
 272                else
 273                        diff_q(&outq, p);
 274        }
 275        free(q->queue);
 276        *q = outq;
 277
 278        return;
 279}