diffcore-break.con commit git-svn: allow 'init' to act as multi-init (dadc6d2)
   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                src_removed;
  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            !hashcmp(src->sha1, dst->sha1))
  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                                   NULL, NULL,
  72                                   0,
  73                                   &src_copied, &literal_added))
  74                return 0;
  75
  76        /* sanity */
  77        if (src->size < src_copied)
  78                src_copied = src->size;
  79        if (dst->size < literal_added + src_copied) {
  80                if (src_copied < dst->size)
  81                        literal_added = dst->size - src_copied;
  82                else
  83                        literal_added = 0;
  84        }
  85        src_removed = src->size - src_copied;
  86
  87        /* Compute merge-score, which is "how much is removed
  88         * from the source material".  The clean-up stage will
  89         * merge the surviving pair together if the score is
  90         * less than the minimum, after rename/copy runs.
  91         */
  92        *merge_score_p = src_removed * MAX_SCORE / src->size;
  93
  94        /* Extent of damage, which counts both inserts and
  95         * deletes.
  96         */
  97        delta_size = src_removed + literal_added;
  98        if (delta_size * MAX_SCORE / base_size < break_score)
  99                return 0;
 100
 101        /* If you removed a lot without adding new material, that is
 102         * not really a rewrite.
 103         */
 104        if ((src->size * break_score < src_removed * MAX_SCORE) &&
 105            (literal_added * 20 < src_removed) &&
 106            (literal_added * 20 < src_copied))
 107                return 0;
 108
 109        return 1;
 110}
 111
 112void diffcore_break(int break_score)
 113{
 114        struct diff_queue_struct *q = &diff_queued_diff;
 115        struct diff_queue_struct outq;
 116
 117        /* When the filepair has this much edit (insert and delete),
 118         * it is first considered to be a rewrite and broken into a
 119         * create and delete filepair.  This is to help breaking a
 120         * file that had too much new stuff added, possibly from
 121         * moving contents from another file, so that rename/copy can
 122         * match it with the other file.
 123         *
 124         * int break_score; we reuse incoming parameter for this.
 125         */
 126
 127        /* After a pair is broken according to break_score and
 128         * subjected to rename/copy, both of them may survive intact,
 129         * due to lack of suitable rename/copy peer.  Or, the caller
 130         * may be calling us without using rename/copy.  When that
 131         * happens, we merge the broken pieces back into one
 132         * modification together if the pair did not have more than
 133         * this much delete.  For this computation, we do not take
 134         * insert into account at all.  If you start from a 100-line
 135         * file and delete 97 lines of it, it does not matter if you
 136         * add 27 lines to it to make a new 30-line file or if you add
 137         * 997 lines to it to make a 1000-line file.  Either way what
 138         * you did was a rewrite of 97%.  On the other hand, if you
 139         * delete 3 lines, keeping 97 lines intact, it does not matter
 140         * if you add 3 lines to it to make a new 100-line file or if
 141         * you add 903 lines to it to make a new 1000-line file.
 142         * Either way you did a lot of additions and not a rewrite.
 143         * This merge happens to catch the latter case.  A merge_score
 144         * of 80% would be a good default value (a broken pair that
 145         * has score lower than merge_score will be merged back
 146         * together).
 147         */
 148        int merge_score;
 149        int i;
 150
 151        /* See comment on DEFAULT_BREAK_SCORE and
 152         * DEFAULT_MERGE_SCORE in diffcore.h
 153         */
 154        merge_score = (break_score >> 16) & 0xFFFF;
 155        break_score = (break_score & 0xFFFF);
 156
 157        if (!break_score)
 158                break_score = DEFAULT_BREAK_SCORE;
 159        if (!merge_score)
 160                merge_score = DEFAULT_MERGE_SCORE;
 161
 162        outq.nr = outq.alloc = 0;
 163        outq.queue = NULL;
 164
 165        for (i = 0; i < q->nr; i++) {
 166                struct diff_filepair *p = q->queue[i];
 167                int score;
 168
 169                /* We deal only with in-place edit of non directory.
 170                 * We do not break anything else.
 171                 */
 172                if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
 173                    !S_ISDIR(p->one->mode) && !S_ISDIR(p->two->mode) &&
 174                    !strcmp(p->one->path, p->two->path)) {
 175                        if (should_break(p->one, p->two,
 176                                         break_score, &score)) {
 177                                /* Split this into delete and create */
 178                                struct diff_filespec *null_one, *null_two;
 179                                struct diff_filepair *dp;
 180
 181                                /* Set score to 0 for the pair that
 182                                 * needs to be merged back together
 183                                 * should they survive rename/copy.
 184                                 * Also we do not want to break very
 185                                 * small files.
 186                                 */
 187                                if (score < merge_score)
 188                                        score = 0;
 189
 190                                /* deletion of one */
 191                                null_one = alloc_filespec(p->one->path);
 192                                dp = diff_queue(&outq, p->one, null_one);
 193                                dp->score = score;
 194                                dp->broken_pair = 1;
 195
 196                                /* creation of two */
 197                                null_two = alloc_filespec(p->two->path);
 198                                dp = diff_queue(&outq, null_two, p->two);
 199                                dp->score = score;
 200                                dp->broken_pair = 1;
 201
 202                                free(p); /* not diff_free_filepair(), we are
 203                                          * reusing one and two here.
 204                                          */
 205                                continue;
 206                        }
 207                }
 208                diff_q(&outq, p);
 209        }
 210        free(q->queue);
 211        *q = outq;
 212
 213        return;
 214}
 215
 216static void merge_broken(struct diff_filepair *p,
 217                         struct diff_filepair *pp,
 218                         struct diff_queue_struct *outq)
 219{
 220        /* p and pp are broken pairs we want to merge */
 221        struct diff_filepair *c = p, *d = pp, *dp;
 222        if (DIFF_FILE_VALID(p->one)) {
 223                /* this must be a delete half */
 224                d = p; c = pp;
 225        }
 226        /* Sanity check */
 227        if (!DIFF_FILE_VALID(d->one))
 228                die("internal error in merge #1");
 229        if (DIFF_FILE_VALID(d->two))
 230                die("internal error in merge #2");
 231        if (DIFF_FILE_VALID(c->one))
 232                die("internal error in merge #3");
 233        if (!DIFF_FILE_VALID(c->two))
 234                die("internal error in merge #4");
 235
 236        dp = diff_queue(outq, d->one, c->two);
 237        dp->score = p->score;
 238        diff_free_filespec_data(d->two);
 239        diff_free_filespec_data(c->one);
 240        free(d);
 241        free(c);
 242}
 243
 244void diffcore_merge_broken(void)
 245{
 246        struct diff_queue_struct *q = &diff_queued_diff;
 247        struct diff_queue_struct outq;
 248        int i, j;
 249
 250        outq.nr = outq.alloc = 0;
 251        outq.queue = NULL;
 252
 253        for (i = 0; i < q->nr; i++) {
 254                struct diff_filepair *p = q->queue[i];
 255                if (!p)
 256                        /* we already merged this with its peer */
 257                        continue;
 258                else if (p->broken_pair &&
 259                         !strcmp(p->one->path, p->two->path)) {
 260                        /* If the peer also survived rename/copy, then
 261                         * we merge them back together.
 262                         */
 263                        for (j = i + 1; j < q->nr; j++) {
 264                                struct diff_filepair *pp = q->queue[j];
 265                                if (pp->broken_pair &&
 266                                    !strcmp(pp->one->path, pp->two->path) &&
 267                                    !strcmp(p->one->path, pp->two->path)) {
 268                                        /* Peer survived.  Merge them */
 269                                        merge_broken(p, pp, &outq);
 270                                        q->queue[j] = NULL;
 271                                        break;
 272                                }
 273                        }
 274                        if (q->nr <= j)
 275                                /* The peer did not survive, so we keep
 276                                 * it in the output.
 277                                 */
 278                                diff_q(&outq, p);
 279                }
 280                else
 281                        diff_q(&outq, p);
 282        }
 283        free(q->queue);
 284        *q = outq;
 285
 286        return;
 287}