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