xdiff / xdiffi.con commit Merge branch 'nd/worktree-name-sanitization' (0d107b1)
   1/*
   2 *  LibXDiff by Davide Libenzi ( File Differential Library )
   3 *  Copyright (C) 2003  Davide Libenzi
   4 *
   5 *  This library is free software; you can redistribute it and/or
   6 *  modify it under the terms of the GNU Lesser General Public
   7 *  License as published by the Free Software Foundation; either
   8 *  version 2.1 of the License, or (at your option) any later version.
   9 *
  10 *  This library is distributed in the hope that it will be useful,
  11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 *  Lesser General Public License for more details.
  14 *
  15 *  You should have received a copy of the GNU Lesser General Public
  16 *  License along with this library; if not, see
  17 *  <http://www.gnu.org/licenses/>.
  18 *
  19 *  Davide Libenzi <davidel@xmailserver.org>
  20 *
  21 */
  22
  23#include "xinclude.h"
  24
  25#define XDL_MAX_COST_MIN 256
  26#define XDL_HEUR_MIN_COST 256
  27#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1)
  28#define XDL_SNAKE_CNT 20
  29#define XDL_K_HEUR 4
  30
  31typedef struct s_xdpsplit {
  32        long i1, i2;
  33        int min_lo, min_hi;
  34} xdpsplit_t;
  35
  36/*
  37 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
  38 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
  39 * the forward diagonal starting from (off1, off2) and the backward diagonal
  40 * starting from (lim1, lim2). If the K values on the same diagonal crosses
  41 * returns the furthest point of reach. We might end up having to expensive
  42 * cases using this algorithm is full, so a little bit of heuristic is needed
  43 * to cut the search and to return a suboptimal point.
  44 */
  45static long xdl_split(unsigned long const *ha1, long off1, long lim1,
  46                      unsigned long const *ha2, long off2, long lim2,
  47                      long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
  48                      xdalgoenv_t *xenv) {
  49        long dmin = off1 - lim2, dmax = lim1 - off2;
  50        long fmid = off1 - off2, bmid = lim1 - lim2;
  51        long odd = (fmid - bmid) & 1;
  52        long fmin = fmid, fmax = fmid;
  53        long bmin = bmid, bmax = bmid;
  54        long ec, d, i1, i2, prev1, best, dd, v, k;
  55
  56        /*
  57         * Set initial diagonal values for both forward and backward path.
  58         */
  59        kvdf[fmid] = off1;
  60        kvdb[bmid] = lim1;
  61
  62        for (ec = 1;; ec++) {
  63                int got_snake = 0;
  64
  65                /*
  66                 * We need to extent the diagonal "domain" by one. If the next
  67                 * values exits the box boundaries we need to change it in the
  68                 * opposite direction because (max - min) must be a power of two.
  69                 * Also we initialize the external K value to -1 so that we can
  70                 * avoid extra conditions check inside the core loop.
  71                 */
  72                if (fmin > dmin)
  73                        kvdf[--fmin - 1] = -1;
  74                else
  75                        ++fmin;
  76                if (fmax < dmax)
  77                        kvdf[++fmax + 1] = -1;
  78                else
  79                        --fmax;
  80
  81                for (d = fmax; d >= fmin; d -= 2) {
  82                        if (kvdf[d - 1] >= kvdf[d + 1])
  83                                i1 = kvdf[d - 1] + 1;
  84                        else
  85                                i1 = kvdf[d + 1];
  86                        prev1 = i1;
  87                        i2 = i1 - d;
  88                        for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
  89                        if (i1 - prev1 > xenv->snake_cnt)
  90                                got_snake = 1;
  91                        kvdf[d] = i1;
  92                        if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
  93                                spl->i1 = i1;
  94                                spl->i2 = i2;
  95                                spl->min_lo = spl->min_hi = 1;
  96                                return ec;
  97                        }
  98                }
  99
 100                /*
 101                 * We need to extent the diagonal "domain" by one. If the next
 102                 * values exits the box boundaries we need to change it in the
 103                 * opposite direction because (max - min) must be a power of two.
 104                 * Also we initialize the external K value to -1 so that we can
 105                 * avoid extra conditions check inside the core loop.
 106                 */
 107                if (bmin > dmin)
 108                        kvdb[--bmin - 1] = XDL_LINE_MAX;
 109                else
 110                        ++bmin;
 111                if (bmax < dmax)
 112                        kvdb[++bmax + 1] = XDL_LINE_MAX;
 113                else
 114                        --bmax;
 115
 116                for (d = bmax; d >= bmin; d -= 2) {
 117                        if (kvdb[d - 1] < kvdb[d + 1])
 118                                i1 = kvdb[d - 1];
 119                        else
 120                                i1 = kvdb[d + 1] - 1;
 121                        prev1 = i1;
 122                        i2 = i1 - d;
 123                        for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
 124                        if (prev1 - i1 > xenv->snake_cnt)
 125                                got_snake = 1;
 126                        kvdb[d] = i1;
 127                        if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
 128                                spl->i1 = i1;
 129                                spl->i2 = i2;
 130                                spl->min_lo = spl->min_hi = 1;
 131                                return ec;
 132                        }
 133                }
 134
 135                if (need_min)
 136                        continue;
 137
 138                /*
 139                 * If the edit cost is above the heuristic trigger and if
 140                 * we got a good snake, we sample current diagonals to see
 141                 * if some of the, have reached an "interesting" path. Our
 142                 * measure is a function of the distance from the diagonal
 143                 * corner (i1 + i2) penalized with the distance from the
 144                 * mid diagonal itself. If this value is above the current
 145                 * edit cost times a magic factor (XDL_K_HEUR) we consider
 146                 * it interesting.
 147                 */
 148                if (got_snake && ec > xenv->heur_min) {
 149                        for (best = 0, d = fmax; d >= fmin; d -= 2) {
 150                                dd = d > fmid ? d - fmid: fmid - d;
 151                                i1 = kvdf[d];
 152                                i2 = i1 - d;
 153                                v = (i1 - off1) + (i2 - off2) - dd;
 154
 155                                if (v > XDL_K_HEUR * ec && v > best &&
 156                                    off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
 157                                    off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
 158                                        for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
 159                                                if (k == xenv->snake_cnt) {
 160                                                        best = v;
 161                                                        spl->i1 = i1;
 162                                                        spl->i2 = i2;
 163                                                        break;
 164                                                }
 165                                }
 166                        }
 167                        if (best > 0) {
 168                                spl->min_lo = 1;
 169                                spl->min_hi = 0;
 170                                return ec;
 171                        }
 172
 173                        for (best = 0, d = bmax; d >= bmin; d -= 2) {
 174                                dd = d > bmid ? d - bmid: bmid - d;
 175                                i1 = kvdb[d];
 176                                i2 = i1 - d;
 177                                v = (lim1 - i1) + (lim2 - i2) - dd;
 178
 179                                if (v > XDL_K_HEUR * ec && v > best &&
 180                                    off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
 181                                    off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
 182                                        for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
 183                                                if (k == xenv->snake_cnt - 1) {
 184                                                        best = v;
 185                                                        spl->i1 = i1;
 186                                                        spl->i2 = i2;
 187                                                        break;
 188                                                }
 189                                }
 190                        }
 191                        if (best > 0) {
 192                                spl->min_lo = 0;
 193                                spl->min_hi = 1;
 194                                return ec;
 195                        }
 196                }
 197
 198                /*
 199                 * Enough is enough. We spent too much time here and now we collect
 200                 * the furthest reaching path using the (i1 + i2) measure.
 201                 */
 202                if (ec >= xenv->mxcost) {
 203                        long fbest, fbest1, bbest, bbest1;
 204
 205                        fbest = fbest1 = -1;
 206                        for (d = fmax; d >= fmin; d -= 2) {
 207                                i1 = XDL_MIN(kvdf[d], lim1);
 208                                i2 = i1 - d;
 209                                if (lim2 < i2)
 210                                        i1 = lim2 + d, i2 = lim2;
 211                                if (fbest < i1 + i2) {
 212                                        fbest = i1 + i2;
 213                                        fbest1 = i1;
 214                                }
 215                        }
 216
 217                        bbest = bbest1 = XDL_LINE_MAX;
 218                        for (d = bmax; d >= bmin; d -= 2) {
 219                                i1 = XDL_MAX(off1, kvdb[d]);
 220                                i2 = i1 - d;
 221                                if (i2 < off2)
 222                                        i1 = off2 + d, i2 = off2;
 223                                if (i1 + i2 < bbest) {
 224                                        bbest = i1 + i2;
 225                                        bbest1 = i1;
 226                                }
 227                        }
 228
 229                        if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
 230                                spl->i1 = fbest1;
 231                                spl->i2 = fbest - fbest1;
 232                                spl->min_lo = 1;
 233                                spl->min_hi = 0;
 234                        } else {
 235                                spl->i1 = bbest1;
 236                                spl->i2 = bbest - bbest1;
 237                                spl->min_lo = 0;
 238                                spl->min_hi = 1;
 239                        }
 240                        return ec;
 241                }
 242        }
 243}
 244
 245
 246/*
 247 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
 248 * the box splitting function. Note that the real job (marking changed lines)
 249 * is done in the two boundary reaching checks.
 250 */
 251int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
 252                 diffdata_t *dd2, long off2, long lim2,
 253                 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
 254        unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
 255
 256        /*
 257         * Shrink the box by walking through each diagonal snake (SW and NE).
 258         */
 259        for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
 260        for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
 261
 262        /*
 263         * If one dimension is empty, then all records on the other one must
 264         * be obviously changed.
 265         */
 266        if (off1 == lim1) {
 267                char *rchg2 = dd2->rchg;
 268                long *rindex2 = dd2->rindex;
 269
 270                for (; off2 < lim2; off2++)
 271                        rchg2[rindex2[off2]] = 1;
 272        } else if (off2 == lim2) {
 273                char *rchg1 = dd1->rchg;
 274                long *rindex1 = dd1->rindex;
 275
 276                for (; off1 < lim1; off1++)
 277                        rchg1[rindex1[off1]] = 1;
 278        } else {
 279                xdpsplit_t spl;
 280                spl.i1 = spl.i2 = 0;
 281
 282                /*
 283                 * Divide ...
 284                 */
 285                if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
 286                              need_min, &spl, xenv) < 0) {
 287
 288                        return -1;
 289                }
 290
 291                /*
 292                 * ... et Impera.
 293                 */
 294                if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
 295                                 kvdf, kvdb, spl.min_lo, xenv) < 0 ||
 296                    xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
 297                                 kvdf, kvdb, spl.min_hi, xenv) < 0) {
 298
 299                        return -1;
 300                }
 301        }
 302
 303        return 0;
 304}
 305
 306
 307int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 308                xdfenv_t *xe) {
 309        long ndiags;
 310        long *kvd, *kvdf, *kvdb;
 311        xdalgoenv_t xenv;
 312        diffdata_t dd1, dd2;
 313
 314        if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF)
 315                return xdl_do_patience_diff(mf1, mf2, xpp, xe);
 316
 317        if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
 318                return xdl_do_histogram_diff(mf1, mf2, xpp, xe);
 319
 320        if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
 321
 322                return -1;
 323        }
 324
 325        /*
 326         * Allocate and setup K vectors to be used by the differential algorithm.
 327         * One is to store the forward path and one to store the backward path.
 328         */
 329        ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
 330        if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
 331
 332                xdl_free_env(xe);
 333                return -1;
 334        }
 335        kvdf = kvd;
 336        kvdb = kvdf + ndiags;
 337        kvdf += xe->xdf2.nreff + 1;
 338        kvdb += xe->xdf2.nreff + 1;
 339
 340        xenv.mxcost = xdl_bogosqrt(ndiags);
 341        if (xenv.mxcost < XDL_MAX_COST_MIN)
 342                xenv.mxcost = XDL_MAX_COST_MIN;
 343        xenv.snake_cnt = XDL_SNAKE_CNT;
 344        xenv.heur_min = XDL_HEUR_MIN_COST;
 345
 346        dd1.nrec = xe->xdf1.nreff;
 347        dd1.ha = xe->xdf1.ha;
 348        dd1.rchg = xe->xdf1.rchg;
 349        dd1.rindex = xe->xdf1.rindex;
 350        dd2.nrec = xe->xdf2.nreff;
 351        dd2.ha = xe->xdf2.ha;
 352        dd2.rchg = xe->xdf2.rchg;
 353        dd2.rindex = xe->xdf2.rindex;
 354
 355        if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
 356                         kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
 357
 358                xdl_free(kvd);
 359                xdl_free_env(xe);
 360                return -1;
 361        }
 362
 363        xdl_free(kvd);
 364
 365        return 0;
 366}
 367
 368
 369static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
 370        xdchange_t *xch;
 371
 372        if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
 373                return NULL;
 374
 375        xch->next = xscr;
 376        xch->i1 = i1;
 377        xch->i2 = i2;
 378        xch->chg1 = chg1;
 379        xch->chg2 = chg2;
 380        xch->ignore = 0;
 381
 382        return xch;
 383}
 384
 385
 386static int recs_match(xrecord_t *rec1, xrecord_t *rec2, long flags)
 387{
 388        return (rec1->ha == rec2->ha &&
 389                xdl_recmatch(rec1->ptr, rec1->size,
 390                             rec2->ptr, rec2->size,
 391                             flags));
 392}
 393
 394/*
 395 * If a line is indented more than this, get_indent() just returns this value.
 396 * This avoids having to do absurd amounts of work for data that are not
 397 * human-readable text, and also ensures that the output of get_indent fits within
 398 * an int.
 399 */
 400#define MAX_INDENT 200
 401
 402/*
 403 * Return the amount of indentation of the specified line, treating TAB as 8
 404 * columns. Return -1 if line is empty or contains only whitespace. Clamp the
 405 * output value at MAX_INDENT.
 406 */
 407static int get_indent(xrecord_t *rec)
 408{
 409        long i;
 410        int ret = 0;
 411
 412        for (i = 0; i < rec->size; i++) {
 413                char c = rec->ptr[i];
 414
 415                if (!XDL_ISSPACE(c))
 416                        return ret;
 417                else if (c == ' ')
 418                        ret += 1;
 419                else if (c == '\t')
 420                        ret += 8 - ret % 8;
 421                /* ignore other whitespace characters */
 422
 423                if (ret >= MAX_INDENT)
 424                        return MAX_INDENT;
 425        }
 426
 427        /* The line contains only whitespace. */
 428        return -1;
 429}
 430
 431/*
 432 * If more than this number of consecutive blank rows are found, just return this
 433 * value. This avoids requiring O(N^2) work for pathological cases, and also
 434 * ensures that the output of score_split fits in an int.
 435 */
 436#define MAX_BLANKS 20
 437
 438/* Characteristics measured about a hypothetical split position. */
 439struct split_measurement {
 440        /*
 441         * Is the split at the end of the file (aside from any blank lines)?
 442         */
 443        int end_of_file;
 444
 445        /*
 446         * How much is the line immediately following the split indented (or -1 if
 447         * the line is blank):
 448         */
 449        int indent;
 450
 451        /*
 452         * How many consecutive lines above the split are blank?
 453         */
 454        int pre_blank;
 455
 456        /*
 457         * How much is the nearest non-blank line above the split indented (or -1
 458         * if there is no such line)?
 459         */
 460        int pre_indent;
 461
 462        /*
 463         * How many lines after the line following the split are blank?
 464         */
 465        int post_blank;
 466
 467        /*
 468         * How much is the nearest non-blank line after the line following the
 469         * split indented (or -1 if there is no such line)?
 470         */
 471        int post_indent;
 472};
 473
 474struct split_score {
 475        /* The effective indent of this split (smaller is preferred). */
 476        int effective_indent;
 477
 478        /* Penalty for this split (smaller is preferred). */
 479        int penalty;
 480};
 481
 482/*
 483 * Fill m with information about a hypothetical split of xdf above line split.
 484 */
 485static void measure_split(const xdfile_t *xdf, long split,
 486                          struct split_measurement *m)
 487{
 488        long i;
 489
 490        if (split >= xdf->nrec) {
 491                m->end_of_file = 1;
 492                m->indent = -1;
 493        } else {
 494                m->end_of_file = 0;
 495                m->indent = get_indent(xdf->recs[split]);
 496        }
 497
 498        m->pre_blank = 0;
 499        m->pre_indent = -1;
 500        for (i = split - 1; i >= 0; i--) {
 501                m->pre_indent = get_indent(xdf->recs[i]);
 502                if (m->pre_indent != -1)
 503                        break;
 504                m->pre_blank += 1;
 505                if (m->pre_blank == MAX_BLANKS) {
 506                        m->pre_indent = 0;
 507                        break;
 508                }
 509        }
 510
 511        m->post_blank = 0;
 512        m->post_indent = -1;
 513        for (i = split + 1; i < xdf->nrec; i++) {
 514                m->post_indent = get_indent(xdf->recs[i]);
 515                if (m->post_indent != -1)
 516                        break;
 517                m->post_blank += 1;
 518                if (m->post_blank == MAX_BLANKS) {
 519                        m->post_indent = 0;
 520                        break;
 521                }
 522        }
 523}
 524
 525/*
 526 * The empirically-determined weight factors used by score_split() below.
 527 * Larger values means that the position is a less favorable place to split.
 528 *
 529 * Note that scores are only ever compared against each other, so multiplying
 530 * all of these weight/penalty values by the same factor wouldn't change the
 531 * heuristic's behavior. Still, we need to set that arbitrary scale *somehow*.
 532 * In practice, these numbers are chosen to be large enough that they can be
 533 * adjusted relative to each other with sufficient precision despite using
 534 * integer math.
 535 */
 536
 537/* Penalty if there are no non-blank lines before the split */
 538#define START_OF_FILE_PENALTY 1
 539
 540/* Penalty if there are no non-blank lines after the split */
 541#define END_OF_FILE_PENALTY 21
 542
 543/* Multiplier for the number of blank lines around the split */
 544#define TOTAL_BLANK_WEIGHT (-30)
 545
 546/* Multiplier for the number of blank lines after the split */
 547#define POST_BLANK_WEIGHT 6
 548
 549/*
 550 * Penalties applied if the line is indented more than its predecessor
 551 */
 552#define RELATIVE_INDENT_PENALTY (-4)
 553#define RELATIVE_INDENT_WITH_BLANK_PENALTY 10
 554
 555/*
 556 * Penalties applied if the line is indented less than both its predecessor and
 557 * its successor
 558 */
 559#define RELATIVE_OUTDENT_PENALTY 24
 560#define RELATIVE_OUTDENT_WITH_BLANK_PENALTY 17
 561
 562/*
 563 * Penalties applied if the line is indented less than its predecessor but not
 564 * less than its successor
 565 */
 566#define RELATIVE_DEDENT_PENALTY 23
 567#define RELATIVE_DEDENT_WITH_BLANK_PENALTY 17
 568
 569/*
 570 * We only consider whether the sum of the effective indents for splits are
 571 * less than (-1), equal to (0), or greater than (+1) each other. The resulting
 572 * value is multiplied by the following weight and combined with the penalty to
 573 * determine the better of two scores.
 574 */
 575#define INDENT_WEIGHT 60
 576
 577/*
 578 * How far do we slide a hunk at most?
 579 */
 580#define INDENT_HEURISTIC_MAX_SLIDING 100
 581
 582/*
 583 * Compute a badness score for the hypothetical split whose measurements are
 584 * stored in m. The weight factors were determined empirically using the tools and
 585 * corpus described in
 586 *
 587 *     https://github.com/mhagger/diff-slider-tools
 588 *
 589 * Also see that project if you want to improve the weights based on, for example,
 590 * a larger or more diverse corpus.
 591 */
 592static void score_add_split(const struct split_measurement *m, struct split_score *s)
 593{
 594        /*
 595         * A place to accumulate penalty factors (positive makes this index more
 596         * favored):
 597         */
 598        int post_blank, total_blank, indent, any_blanks;
 599
 600        if (m->pre_indent == -1 && m->pre_blank == 0)
 601                s->penalty += START_OF_FILE_PENALTY;
 602
 603        if (m->end_of_file)
 604                s->penalty += END_OF_FILE_PENALTY;
 605
 606        /*
 607         * Set post_blank to the number of blank lines following the split,
 608         * including the line immediately after the split:
 609         */
 610        post_blank = (m->indent == -1) ? 1 + m->post_blank : 0;
 611        total_blank = m->pre_blank + post_blank;
 612
 613        /* Penalties based on nearby blank lines: */
 614        s->penalty += TOTAL_BLANK_WEIGHT * total_blank;
 615        s->penalty += POST_BLANK_WEIGHT * post_blank;
 616
 617        if (m->indent != -1)
 618                indent = m->indent;
 619        else
 620                indent = m->post_indent;
 621
 622        any_blanks = (total_blank != 0);
 623
 624        /* Note that the effective indent is -1 at the end of the file: */
 625        s->effective_indent += indent;
 626
 627        if (indent == -1) {
 628                /* No additional adjustments needed. */
 629        } else if (m->pre_indent == -1) {
 630                /* No additional adjustments needed. */
 631        } else if (indent > m->pre_indent) {
 632                /*
 633                 * The line is indented more than its predecessor.
 634                 */
 635                s->penalty += any_blanks ?
 636                        RELATIVE_INDENT_WITH_BLANK_PENALTY :
 637                        RELATIVE_INDENT_PENALTY;
 638        } else if (indent == m->pre_indent) {
 639                /*
 640                 * The line has the same indentation level as its predecessor.
 641                 * No additional adjustments needed.
 642                 */
 643        } else {
 644                /*
 645                 * The line is indented less than its predecessor. It could be
 646                 * the block terminator of the previous block, but it could
 647                 * also be the start of a new block (e.g., an "else" block, or
 648                 * maybe the previous block didn't have a block terminator).
 649                 * Try to distinguish those cases based on what comes next:
 650                 */
 651                if (m->post_indent != -1 && m->post_indent > indent) {
 652                        /*
 653                         * The following line is indented more. So it is likely
 654                         * that this line is the start of a block.
 655                         */
 656                        s->penalty += any_blanks ?
 657                                RELATIVE_OUTDENT_WITH_BLANK_PENALTY :
 658                                RELATIVE_OUTDENT_PENALTY;
 659                } else {
 660                        /*
 661                         * That was probably the end of a block.
 662                         */
 663                        s->penalty += any_blanks ?
 664                                RELATIVE_DEDENT_WITH_BLANK_PENALTY :
 665                                RELATIVE_DEDENT_PENALTY;
 666                }
 667        }
 668}
 669
 670static int score_cmp(struct split_score *s1, struct split_score *s2)
 671{
 672        /* -1 if s1.effective_indent < s2->effective_indent, etc. */
 673        int cmp_indents = ((s1->effective_indent > s2->effective_indent) -
 674                           (s1->effective_indent < s2->effective_indent));
 675
 676        return INDENT_WEIGHT * cmp_indents + (s1->penalty - s2->penalty);
 677}
 678
 679/*
 680 * Represent a group of changed lines in an xdfile_t (i.e., a contiguous group
 681 * of lines that was inserted or deleted from the corresponding version of the
 682 * file). We consider there to be such a group at the beginning of the file, at
 683 * the end of the file, and between any two unchanged lines, though most such
 684 * groups will usually be empty.
 685 *
 686 * If the first line in a group is equal to the line following the group, then
 687 * the group can be slid down. Similarly, if the last line in a group is equal
 688 * to the line preceding the group, then the group can be slid up. See
 689 * group_slide_down() and group_slide_up().
 690 *
 691 * Note that loops that are testing for changed lines in xdf->rchg do not need
 692 * index bounding since the array is prepared with a zero at position -1 and N.
 693 */
 694struct xdlgroup {
 695        /*
 696         * The index of the first changed line in the group, or the index of
 697         * the unchanged line above which the (empty) group is located.
 698         */
 699        long start;
 700
 701        /*
 702         * The index of the first unchanged line after the group. For an empty
 703         * group, end is equal to start.
 704         */
 705        long end;
 706};
 707
 708/*
 709 * Initialize g to point at the first group in xdf.
 710 */
 711static void group_init(xdfile_t *xdf, struct xdlgroup *g)
 712{
 713        g->start = g->end = 0;
 714        while (xdf->rchg[g->end])
 715                g->end++;
 716}
 717
 718/*
 719 * Move g to describe the next (possibly empty) group in xdf and return 0. If g
 720 * is already at the end of the file, do nothing and return -1.
 721 */
 722static inline int group_next(xdfile_t *xdf, struct xdlgroup *g)
 723{
 724        if (g->end == xdf->nrec)
 725                return -1;
 726
 727        g->start = g->end + 1;
 728        for (g->end = g->start; xdf->rchg[g->end]; g->end++)
 729                ;
 730
 731        return 0;
 732}
 733
 734/*
 735 * Move g to describe the previous (possibly empty) group in xdf and return 0.
 736 * If g is already at the beginning of the file, do nothing and return -1.
 737 */
 738static inline int group_previous(xdfile_t *xdf, struct xdlgroup *g)
 739{
 740        if (g->start == 0)
 741                return -1;
 742
 743        g->end = g->start - 1;
 744        for (g->start = g->end; xdf->rchg[g->start - 1]; g->start--)
 745                ;
 746
 747        return 0;
 748}
 749
 750/*
 751 * If g can be slid toward the end of the file, do so, and if it bumps into a
 752 * following group, expand this group to include it. Return 0 on success or -1
 753 * if g cannot be slid down.
 754 */
 755static int group_slide_down(xdfile_t *xdf, struct xdlgroup *g, long flags)
 756{
 757        if (g->end < xdf->nrec &&
 758            recs_match(xdf->recs[g->start], xdf->recs[g->end], flags)) {
 759                xdf->rchg[g->start++] = 0;
 760                xdf->rchg[g->end++] = 1;
 761
 762                while (xdf->rchg[g->end])
 763                        g->end++;
 764
 765                return 0;
 766        } else {
 767                return -1;
 768        }
 769}
 770
 771/*
 772 * If g can be slid toward the beginning of the file, do so, and if it bumps
 773 * into a previous group, expand this group to include it. Return 0 on success
 774 * or -1 if g cannot be slid up.
 775 */
 776static int group_slide_up(xdfile_t *xdf, struct xdlgroup *g, long flags)
 777{
 778        if (g->start > 0 &&
 779            recs_match(xdf->recs[g->start - 1], xdf->recs[g->end - 1], flags)) {
 780                xdf->rchg[--g->start] = 1;
 781                xdf->rchg[--g->end] = 0;
 782
 783                while (xdf->rchg[g->start - 1])
 784                        g->start--;
 785
 786                return 0;
 787        } else {
 788                return -1;
 789        }
 790}
 791
 792static void xdl_bug(const char *msg)
 793{
 794        fprintf(stderr, "BUG: %s\n", msg);
 795        exit(1);
 796}
 797
 798/*
 799 * Move back and forward change groups for a consistent and pretty diff output.
 800 * This also helps in finding joinable change groups and reducing the diff
 801 * size.
 802 */
 803int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
 804        struct xdlgroup g, go;
 805        long earliest_end, end_matching_other;
 806        long groupsize;
 807
 808        group_init(xdf, &g);
 809        group_init(xdfo, &go);
 810
 811        while (1) {
 812                /* If the group is empty in the to-be-compacted file, skip it: */
 813                if (g.end == g.start)
 814                        goto next;
 815
 816                /*
 817                 * Now shift the change up and then down as far as possible in
 818                 * each direction. If it bumps into any other changes, merge them.
 819                 */
 820                do {
 821                        groupsize = g.end - g.start;
 822
 823                        /*
 824                         * Keep track of the last "end" index that causes this
 825                         * group to align with a group of changed lines in the
 826                         * other file. -1 indicates that we haven't found such
 827                         * a match yet:
 828                         */
 829                        end_matching_other = -1;
 830
 831                        /* Shift the group backward as much as possible: */
 832                        while (!group_slide_up(xdf, &g, flags))
 833                                if (group_previous(xdfo, &go))
 834                                        xdl_bug("group sync broken sliding up");
 835
 836                        /*
 837                         * This is this highest that this group can be shifted.
 838                         * Record its end index:
 839                         */
 840                        earliest_end = g.end;
 841
 842                        if (go.end > go.start)
 843                                end_matching_other = g.end;
 844
 845                        /* Now shift the group forward as far as possible: */
 846                        while (1) {
 847                                if (group_slide_down(xdf, &g, flags))
 848                                        break;
 849                                if (group_next(xdfo, &go))
 850                                        xdl_bug("group sync broken sliding down");
 851
 852                                if (go.end > go.start)
 853                                        end_matching_other = g.end;
 854                        }
 855                } while (groupsize != g.end - g.start);
 856
 857                /*
 858                 * If the group can be shifted, then we can possibly use this
 859                 * freedom to produce a more intuitive diff.
 860                 *
 861                 * The group is currently shifted as far down as possible, so the
 862                 * heuristics below only have to handle upwards shifts.
 863                 */
 864
 865                if (g.end == earliest_end) {
 866                        /* no shifting was possible */
 867                } else if (end_matching_other != -1) {
 868                        /*
 869                         * Move the possibly merged group of changes back to line
 870                         * up with the last group of changes from the other file
 871                         * that it can align with.
 872                         */
 873                        while (go.end == go.start) {
 874                                if (group_slide_up(xdf, &g, flags))
 875                                        xdl_bug("match disappeared");
 876                                if (group_previous(xdfo, &go))
 877                                        xdl_bug("group sync broken sliding to match");
 878                        }
 879                } else if (flags & XDF_INDENT_HEURISTIC) {
 880                        /*
 881                         * Indent heuristic: a group of pure add/delete lines
 882                         * implies two splits, one between the end of the "before"
 883                         * context and the start of the group, and another between
 884                         * the end of the group and the beginning of the "after"
 885                         * context. Some splits are aesthetically better and some
 886                         * are worse. We compute a badness "score" for each split,
 887                         * and add the scores for the two splits to define a
 888                         * "score" for each position that the group can be shifted
 889                         * to. Then we pick the shift with the lowest score.
 890                         */
 891                        long shift, best_shift = -1;
 892                        struct split_score best_score;
 893
 894                        shift = earliest_end;
 895                        if (g.end - groupsize - 1 > shift)
 896                                shift = g.end - groupsize - 1;
 897                        if (g.end - INDENT_HEURISTIC_MAX_SLIDING > shift)
 898                                shift = g.end - INDENT_HEURISTIC_MAX_SLIDING;
 899                        for (; shift <= g.end; shift++) {
 900                                struct split_measurement m;
 901                                struct split_score score = {0, 0};
 902
 903                                measure_split(xdf, shift, &m);
 904                                score_add_split(&m, &score);
 905                                measure_split(xdf, shift - groupsize, &m);
 906                                score_add_split(&m, &score);
 907                                if (best_shift == -1 ||
 908                                    score_cmp(&score, &best_score) <= 0) {
 909                                        best_score.effective_indent = score.effective_indent;
 910                                        best_score.penalty = score.penalty;
 911                                        best_shift = shift;
 912                                }
 913                        }
 914
 915                        while (g.end > best_shift) {
 916                                if (group_slide_up(xdf, &g, flags))
 917                                        xdl_bug("best shift unreached");
 918                                if (group_previous(xdfo, &go))
 919                                        xdl_bug("group sync broken sliding to blank line");
 920                        }
 921                }
 922
 923        next:
 924                /* Move past the just-processed group: */
 925                if (group_next(xdf, &g))
 926                        break;
 927                if (group_next(xdfo, &go))
 928                        xdl_bug("group sync broken moving to next group");
 929        }
 930
 931        if (!group_next(xdfo, &go))
 932                xdl_bug("group sync broken at end of file");
 933
 934        return 0;
 935}
 936
 937
 938int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
 939        xdchange_t *cscr = NULL, *xch;
 940        char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
 941        long i1, i2, l1, l2;
 942
 943        /*
 944         * Trivial. Collects "groups" of changes and creates an edit script.
 945         */
 946        for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
 947                if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
 948                        for (l1 = i1; rchg1[i1 - 1]; i1--);
 949                        for (l2 = i2; rchg2[i2 - 1]; i2--);
 950
 951                        if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
 952                                xdl_free_script(cscr);
 953                                return -1;
 954                        }
 955                        cscr = xch;
 956                }
 957
 958        *xscr = cscr;
 959
 960        return 0;
 961}
 962
 963
 964void xdl_free_script(xdchange_t *xscr) {
 965        xdchange_t *xch;
 966
 967        while ((xch = xscr) != NULL) {
 968                xscr = xscr->next;
 969                xdl_free(xch);
 970        }
 971}
 972
 973static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 974                              xdemitconf_t const *xecfg)
 975{
 976        xdchange_t *xch, *xche;
 977
 978        for (xch = xscr; xch; xch = xche->next) {
 979                xche = xdl_get_hunk(&xch, xecfg);
 980                if (!xch)
 981                        break;
 982                if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
 983                                     xch->i2, xche->i2 + xche->chg2 - xch->i2,
 984                                     ecb->priv) < 0)
 985                        return -1;
 986        }
 987        return 0;
 988}
 989
 990static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
 991{
 992        xdchange_t *xch;
 993
 994        for (xch = xscr; xch; xch = xch->next) {
 995                int ignore = 1;
 996                xrecord_t **rec;
 997                long i;
 998
 999                rec = &xe->xdf1.recs[xch->i1];
1000                for (i = 0; i < xch->chg1 && ignore; i++)
1001                        ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
1002
1003                rec = &xe->xdf2.recs[xch->i2];
1004                for (i = 0; i < xch->chg2 && ignore; i++)
1005                        ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
1006
1007                xch->ignore = ignore;
1008        }
1009}
1010
1011int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
1012             xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
1013        xdchange_t *xscr;
1014        xdfenv_t xe;
1015        emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;
1016
1017        if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
1018
1019                return -1;
1020        }
1021        if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
1022            xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
1023            xdl_build_script(&xe, &xscr) < 0) {
1024
1025                xdl_free_env(&xe);
1026                return -1;
1027        }
1028        if (xscr) {
1029                if (xpp->flags & XDF_IGNORE_BLANK_LINES)
1030                        xdl_mark_ignorable(xscr, &xe, xpp->flags);
1031
1032                if (ef(&xe, xscr, ecb, xecfg) < 0) {
1033
1034                        xdl_free_script(xscr);
1035                        xdl_free_env(&xe);
1036                        return -1;
1037                }
1038                xdl_free_script(xscr);
1039        }
1040        xdl_free_env(&xe);
1041
1042        return 0;
1043}