apply.con commit git-apply: first cut at actually checking fragment data (3cca928)
   1/*
   2 * apply.c
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 *
   6 * This applies patches on top of some (arbitrary) version of the SCM.
   7 *
   8 * NOTE! It does all its work in the index file, and only cares about
   9 * the files in the working directory if you tell it to "merge" the
  10 * patch apply.
  11 *
  12 * Even when merging it always takes the source from the index, and
  13 * uses the working tree as a "branch" for a 3-way merge.
  14 */
  15#include <ctype.h>
  16
  17#include "cache.h"
  18
  19// We default to the merge behaviour, since that's what most people would
  20// expect.
  21//
  22//  --check turns on checking that the working tree matches the
  23//    files that are being modified, but doesn't apply the patch
  24//  --stat does just a diffstat, and doesn't actually apply
  25//  --show-files shows the directory changes
  26//
  27static int merge_patch = 1;
  28static int check_index = 0;
  29static int diffstat = 0;
  30static int check = 0;
  31static int apply = 1;
  32static int show_files = 0;
  33static const char apply_usage[] = "git-apply [--stat] [--check] [--show-files] <patch>";
  34
  35/*
  36 * For "diff-stat" like behaviour, we keep track of the biggest change
  37 * we've seen, and the longest filename. That allows us to do simple
  38 * scaling.
  39 */
  40static int max_change, max_len;
  41
  42/*
  43 * Various "current state", notably line numbers and what
  44 * file (and how) we're patching right now.. The "is_xxxx"
  45 * things are flags, where -1 means "don't know yet".
  46 */
  47static int linenr = 1;
  48
  49struct fragment {
  50        unsigned long oldpos, oldlines;
  51        unsigned long newpos, newlines;
  52        const char *patch;
  53        int size;
  54        struct fragment *next;
  55};
  56
  57struct patch {
  58        char *new_name, *old_name, *def_name;
  59        unsigned int old_mode, new_mode;
  60        int is_rename, is_copy, is_new, is_delete;
  61        int lines_added, lines_deleted;
  62        struct fragment *fragments;
  63        const char *result;
  64        unsigned long resultsize;
  65        struct patch *next;
  66};
  67
  68#define CHUNKSIZE (8192)
  69#define SLOP (16)
  70
  71static void *read_patch_file(int fd, unsigned long *sizep)
  72{
  73        unsigned long size = 0, alloc = CHUNKSIZE;
  74        void *buffer = xmalloc(alloc);
  75
  76        for (;;) {
  77                int nr = alloc - size;
  78                if (nr < 1024) {
  79                        alloc += CHUNKSIZE;
  80                        buffer = xrealloc(buffer, alloc);
  81                        nr = alloc - size;
  82                }
  83                nr = read(fd, buffer + size, nr);
  84                if (!nr)
  85                        break;
  86                if (nr < 0) {
  87                        if (errno == EAGAIN)
  88                                continue;
  89                        die("git-apply: read returned %s", strerror(errno));
  90                }
  91                size += nr;
  92        }
  93        *sizep = size;
  94
  95        /*
  96         * Make sure that we have some slop in the buffer
  97         * so that we can do speculative "memcmp" etc, and
  98         * see to it that it is NUL-filled.
  99         */
 100        if (alloc < size + SLOP)
 101                buffer = xrealloc(buffer, size + SLOP);
 102        memset(buffer + size, 0, SLOP);
 103        return buffer;
 104}
 105
 106static unsigned long linelen(const char *buffer, unsigned long size)
 107{
 108        unsigned long len = 0;
 109        while (size--) {
 110                len++;
 111                if (*buffer++ == '\n')
 112                        break;
 113        }
 114        return len;
 115}
 116
 117static int is_dev_null(const char *str)
 118{
 119        return !memcmp("/dev/null", str, 9) && isspace(str[9]);
 120}
 121
 122#define TERM_SPACE      1
 123#define TERM_TAB        2
 124
 125static int name_terminate(const char *name, int namelen, int c, int terminate)
 126{
 127        if (c == ' ' && !(terminate & TERM_SPACE))
 128                return 0;
 129        if (c == '\t' && !(terminate & TERM_TAB))
 130                return 0;
 131
 132        return 1;
 133}
 134
 135static char * find_name(const char *line, char *def, int p_value, int terminate)
 136{
 137        int len;
 138        const char *start = line;
 139        char *name;
 140
 141        for (;;) {
 142                char c = *line;
 143
 144                if (isspace(c)) {
 145                        if (c == '\n')
 146                                break;
 147                        if (name_terminate(start, line-start, c, terminate))
 148                                break;
 149                }
 150                line++;
 151                if (c == '/' && !--p_value)
 152                        start = line;
 153        }
 154        if (!start)
 155                return def;
 156        len = line - start;
 157        if (!len)
 158                return def;
 159
 160        /*
 161         * Generally we prefer the shorter name, especially
 162         * if the other one is just a variation of that with
 163         * something else tacked on to the end (ie "file.orig"
 164         * or "file~").
 165         */
 166        if (def) {
 167                int deflen = strlen(def);
 168                if (deflen < len && !strncmp(start, def, deflen))
 169                        return def;
 170        }
 171
 172        name = xmalloc(len + 1);
 173        memcpy(name, start, len);
 174        name[len] = 0;
 175        free(def);
 176        return name;
 177}
 178
 179/*
 180 * Get the name etc info from the --/+++ lines of a traditional patch header
 181 *
 182 * NOTE! This hardcodes "-p1" behaviour in filename detection.
 183 *
 184 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
 185 * files, we can happily check the index for a match, but for creating a
 186 * new file we should try to match whatever "patch" does. I have no idea.
 187 */
 188static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
 189{
 190        int p_value = 1;
 191        char *name;
 192
 193        first += 4;     // skip "--- "
 194        second += 4;    // skip "+++ "
 195        if (is_dev_null(first)) {
 196                patch->is_new = 1;
 197                patch->is_delete = 0;
 198                name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
 199                patch->new_name = name;
 200        } else if (is_dev_null(second)) {
 201                patch->is_new = 0;
 202                patch->is_delete = 1;
 203                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 204                patch->old_name = name;
 205        } else {
 206                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 207                name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
 208                patch->old_name = patch->new_name = name;
 209        }
 210        if (!name)
 211                die("unable to find filename in patch at line %d", linenr);
 212}
 213
 214static int gitdiff_hdrend(const char *line, struct patch *patch)
 215{
 216        return -1;
 217}
 218
 219/*
 220 * We're anal about diff header consistency, to make
 221 * sure that we don't end up having strange ambiguous
 222 * patches floating around.
 223 *
 224 * As a result, gitdiff_{old|new}name() will check
 225 * their names against any previous information, just
 226 * to make sure..
 227 */
 228static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 229{
 230        int len;
 231        const char *name;
 232
 233        if (!orig_name && !isnull)
 234                return find_name(line, NULL, 1, 0);
 235
 236        name = "/dev/null";
 237        len = 9;
 238        if (orig_name) {
 239                name = orig_name;
 240                len = strlen(name);
 241                if (isnull)
 242                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
 243        }
 244
 245        if (*name == '/')
 246                goto absolute_path;
 247
 248        for (;;) {
 249                char c = *line++;
 250                if (c == '\n')
 251                        break;
 252                if (c != '/')
 253                        continue;
 254absolute_path:
 255                if (memcmp(line, name, len) || line[len] != '\n')
 256                        break;
 257                return orig_name;
 258        }
 259        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
 260        return NULL;
 261}
 262
 263static int gitdiff_oldname(const char *line, struct patch *patch)
 264{
 265        patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
 266        return 0;
 267}
 268
 269static int gitdiff_newname(const char *line, struct patch *patch)
 270{
 271        patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
 272        return 0;
 273}
 274
 275static int gitdiff_oldmode(const char *line, struct patch *patch)
 276{
 277        patch->old_mode = strtoul(line, NULL, 8);
 278        return 0;
 279}
 280
 281static int gitdiff_newmode(const char *line, struct patch *patch)
 282{
 283        patch->new_mode = strtoul(line, NULL, 8);
 284        return 0;
 285}
 286
 287static int gitdiff_delete(const char *line, struct patch *patch)
 288{
 289        patch->is_delete = 1;
 290        patch->old_name = patch->def_name;
 291        return gitdiff_oldmode(line, patch);
 292}
 293
 294static int gitdiff_newfile(const char *line, struct patch *patch)
 295{
 296        patch->is_new = 1;
 297        patch->new_name = patch->def_name;
 298        return gitdiff_newmode(line, patch);
 299}
 300
 301static int gitdiff_copysrc(const char *line, struct patch *patch)
 302{
 303        patch->is_copy = 1;
 304        patch->old_name = find_name(line, NULL, 0, 0);
 305        return 0;
 306}
 307
 308static int gitdiff_copydst(const char *line, struct patch *patch)
 309{
 310        patch->is_copy = 1;
 311        patch->new_name = find_name(line, NULL, 0, 0);
 312        return 0;
 313}
 314
 315static int gitdiff_renamesrc(const char *line, struct patch *patch)
 316{
 317        patch->is_rename = 1;
 318        patch->old_name = find_name(line, NULL, 0, 0);
 319        return 0;
 320}
 321
 322static int gitdiff_renamedst(const char *line, struct patch *patch)
 323{
 324        patch->is_rename = 1;
 325        patch->new_name = find_name(line, NULL, 0, 0);
 326        return 0;
 327}
 328
 329static int gitdiff_similarity(const char *line, struct patch *patch)
 330{
 331        return 0;
 332}
 333
 334static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 335{
 336        return 0;
 337}
 338
 339/*
 340 * This is normal for a diff that doesn't change anything: we'll fall through
 341 * into the next diff. Tell the parser to break out.
 342 */
 343static int gitdiff_unrecognized(const char *line, struct patch *patch)
 344{
 345        return -1;
 346}
 347
 348static char *git_header_name(char *line)
 349{
 350        int len;
 351        char *name, *second;
 352
 353        /*
 354         * Find the first '/'
 355         */
 356        name = line;
 357        for (;;) {
 358                char c = *name++;
 359                if (c == '\n')
 360                        return NULL;
 361                if (c == '/')
 362                        break;
 363        }
 364
 365        /*
 366         * We don't accept absolute paths (/dev/null) as possibly valid
 367         */
 368        if (name == line+1)
 369                return NULL;
 370
 371        /*
 372         * Accept a name only if it shows up twice, exactly the same
 373         * form.
 374         */
 375        for (len = 0 ; ; len++) {
 376                char c = name[len];
 377
 378                switch (c) {
 379                default:
 380                        continue;
 381                case '\n':
 382                        break;
 383                case '\t': case ' ':
 384                        second = name+len;
 385                        for (;;) {
 386                                char c = *second++;
 387                                if (c == '\n')
 388                                        return NULL;
 389                                if (c == '/')
 390                                        break;
 391                        }
 392                        if (second[len] == '\n' && !memcmp(name, second, len)) {
 393                                char *ret = xmalloc(len + 1);
 394                                memcpy(ret, name, len);
 395                                ret[len] = 0;
 396                                return ret;
 397                        }
 398                }
 399        }
 400        return NULL;
 401}
 402
 403/* Verify that we recognize the lines following a git header */
 404static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
 405{
 406        unsigned long offset;
 407
 408        /* A git diff has explicit new/delete information, so we don't guess */
 409        patch->is_new = 0;
 410        patch->is_delete = 0;
 411
 412        /*
 413         * Some things may not have the old name in the
 414         * rest of the headers anywhere (pure mode changes,
 415         * or removing or adding empty files), so we get
 416         * the default name from the header.
 417         */
 418        patch->def_name = git_header_name(line + strlen("diff --git "));
 419
 420        line += len;
 421        size -= len;
 422        linenr++;
 423        for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 424                static const struct opentry {
 425                        const char *str;
 426                        int (*fn)(const char *, struct patch *);
 427                } optable[] = {
 428                        { "@@ -", gitdiff_hdrend },
 429                        { "--- ", gitdiff_oldname },
 430                        { "+++ ", gitdiff_newname },
 431                        { "old mode ", gitdiff_oldmode },
 432                        { "new mode ", gitdiff_newmode },
 433                        { "deleted file mode ", gitdiff_delete },
 434                        { "new file mode ", gitdiff_newfile },
 435                        { "copy from ", gitdiff_copysrc },
 436                        { "copy to ", gitdiff_copydst },
 437                        { "rename from ", gitdiff_renamesrc },
 438                        { "rename to ", gitdiff_renamedst },
 439                        { "similarity index ", gitdiff_similarity },
 440                        { "dissimilarity index ", gitdiff_dissimilarity },
 441                        { "", gitdiff_unrecognized },
 442                };
 443                int i;
 444
 445                len = linelen(line, size);
 446                if (!len || line[len-1] != '\n')
 447                        break;
 448                for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
 449                        const struct opentry *p = optable + i;
 450                        int oplen = strlen(p->str);
 451                        if (len < oplen || memcmp(p->str, line, oplen))
 452                                continue;
 453                        if (p->fn(line + oplen, patch) < 0)
 454                                return offset;
 455                        break;
 456                }
 457        }
 458
 459        return offset;
 460}
 461
 462static int parse_num(const char *line, unsigned long *p)
 463{
 464        char *ptr;
 465
 466        if (!isdigit(*line))
 467                return 0;
 468        *p = strtoul(line, &ptr, 10);
 469        return ptr - line;
 470}
 471
 472static int parse_range(const char *line, int len, int offset, const char *expect,
 473                        unsigned long *p1, unsigned long *p2)
 474{
 475        int digits, ex;
 476
 477        if (offset < 0 || offset >= len)
 478                return -1;
 479        line += offset;
 480        len -= offset;
 481
 482        digits = parse_num(line, p1);
 483        if (!digits)
 484                return -1;
 485
 486        offset += digits;
 487        line += digits;
 488        len -= digits;
 489
 490        *p2 = *p1;
 491        if (*line == ',') {
 492                digits = parse_num(line+1, p2);
 493                if (!digits)
 494                        return -1;
 495
 496                offset += digits+1;
 497                line += digits+1;
 498                len -= digits+1;
 499        }
 500
 501        ex = strlen(expect);
 502        if (ex > len)
 503                return -1;
 504        if (memcmp(line, expect, ex))
 505                return -1;
 506
 507        return offset + ex;
 508}
 509
 510/*
 511 * Parse a unified diff fragment header of the
 512 * form "@@ -a,b +c,d @@"
 513 */
 514static int parse_fragment_header(char *line, int len, struct fragment *fragment)
 515{
 516        int offset;
 517
 518        if (!len || line[len-1] != '\n')
 519                return -1;
 520
 521        /* Figure out the number of lines in a fragment */
 522        offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
 523        offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
 524
 525        return offset;
 526}
 527
 528static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
 529{
 530        unsigned long offset, len;
 531
 532        patch->is_rename = patch->is_copy = 0;
 533        patch->is_new = patch->is_delete = -1;
 534        patch->old_mode = patch->new_mode = 0;
 535        patch->old_name = patch->new_name = NULL;
 536        for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
 537                unsigned long nextlen;
 538
 539                len = linelen(line, size);
 540                if (!len)
 541                        break;
 542
 543                /* Testing this early allows us to take a few shortcuts.. */
 544                if (len < 6)
 545                        continue;
 546
 547                /*
 548                 * Make sure we don't find any unconnected patch fragmants.
 549                 * That's a sign that we didn't find a header, and that a
 550                 * patch has become corrupted/broken up.
 551                 */
 552                if (!memcmp("@@ -", line, 4)) {
 553                        struct fragment dummy;
 554                        if (parse_fragment_header(line, len, &dummy) < 0)
 555                                continue;
 556                        error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
 557                }
 558
 559                if (size < len + 6)
 560                        break;
 561
 562                /*
 563                 * Git patch? It might not have a real patch, just a rename
 564                 * or mode change, so we handle that specially
 565                 */
 566                if (!memcmp("diff --git ", line, 11)) {
 567                        int git_hdr_len = parse_git_header(line, len, size, patch);
 568                        if (git_hdr_len < 0)
 569                                continue;
 570                        if (!patch->old_name && !patch->new_name)
 571                                die("git diff header lacks filename information");
 572                        *hdrsize = git_hdr_len;
 573                        return offset;
 574                }
 575
 576                /** --- followed by +++ ? */
 577                if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
 578                        continue;
 579
 580                /*
 581                 * We only accept unified patches, so we want it to
 582                 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
 583                 * minimum
 584                 */
 585                nextlen = linelen(line + len, size - len);
 586                if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
 587                        continue;
 588
 589                /* Ok, we'll consider it a patch */
 590                parse_traditional_patch(line, line+len, patch);
 591                *hdrsize = len + nextlen;
 592                linenr += 2;
 593                return offset;
 594        }
 595        return -1;
 596}
 597
 598/*
 599 * Parse a unified diff. Note that this really needs
 600 * to parse each fragment separately, since the only
 601 * way to know the difference between a "---" that is
 602 * part of a patch, and a "---" that starts the next
 603 * patch is to look at the line counts..
 604 */
 605static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
 606{
 607        int added, deleted;
 608        int len = linelen(line, size), offset;
 609        unsigned long pos[4], oldlines, newlines;
 610
 611        offset = parse_fragment_header(line, len, fragment);
 612        if (offset < 0)
 613                return -1;
 614        oldlines = fragment->oldlines;
 615        newlines = fragment->newlines;
 616
 617        if (patch->is_new < 0 && (pos[0] || oldlines))
 618                patch->is_new = 0;
 619        if (patch->is_delete < 0 && (pos[1] || newlines))
 620                patch->is_delete = 0;
 621
 622        /* Parse the thing.. */
 623        line += len;
 624        size -= len;
 625        linenr++;
 626        added = deleted = 0;
 627        for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
 628                if (!oldlines && !newlines)
 629                        break;
 630                len = linelen(line, size);
 631                if (!len || line[len-1] != '\n')
 632                        return -1;
 633                switch (*line) {
 634                default:
 635                        return -1;
 636                case ' ':
 637                        oldlines--;
 638                        newlines--;
 639                        break;
 640                case '-':
 641                        deleted++;
 642                        oldlines--;
 643                        break;
 644                case '+':
 645                        added++;
 646                        newlines--;
 647                        break;
 648                /* We allow "\ No newline at end of file" */
 649                case '\\':
 650                        if (len < 12 || memcmp(line, "\\ No newline", 12))
 651                                return -1;
 652                        break;
 653                }
 654        }
 655        patch->lines_added += added;
 656        patch->lines_deleted += deleted;
 657        return offset;
 658}
 659
 660static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
 661{
 662        unsigned long offset = 0;
 663        struct fragment **fragp = &patch->fragments;
 664
 665        while (size > 4 && !memcmp(line, "@@ -", 4)) {
 666                struct fragment *fragment;
 667                int len;
 668
 669                fragment = xmalloc(sizeof(*fragment));
 670                memset(fragment, 0, sizeof(*fragment));
 671                len = parse_fragment(line, size, patch, fragment);
 672                if (len <= 0)
 673                        die("corrupt patch at line %d", linenr);
 674
 675                fragment->patch = line;
 676                fragment->size = len;
 677
 678                *fragp = fragment;
 679                fragp = &fragment->next;
 680
 681                offset += len;
 682                line += len;
 683                size -= len;
 684        }
 685        return offset;
 686}
 687
 688static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 689{
 690        int hdrsize, patchsize;
 691        int offset = find_header(buffer, size, &hdrsize, patch);
 692
 693        if (offset < 0)
 694                return offset;
 695
 696        patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
 697
 698        return offset + hdrsize + patchsize;
 699}
 700
 701const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
 702const char minuses[]= "----------------------------------------------------------------------";
 703
 704static void show_stats(struct patch *patch)
 705{
 706        char *name = patch->old_name;
 707        int len, max, add, del, total;
 708
 709        if (!name)
 710                name = patch->new_name;
 711
 712        /*
 713         * "scale" the filename
 714         */
 715        len = strlen(name);
 716        max = max_len;
 717        if (max > 50)
 718                max = 50;
 719        if (len > max)
 720                name += len - max;
 721        len = max;
 722
 723        /*
 724         * scale the add/delete
 725         */
 726        max = max_change;
 727        if (max + len > 70)
 728                max = 70 - len;
 729
 730        add = patch->lines_added;
 731        del = patch->lines_deleted;
 732        total = add + del;
 733
 734        total = (total * max + max_change / 2) / max_change;
 735        add = (add * max + max_change / 2) / max_change;
 736        del = total - add;
 737        printf(" %-*s |%5d %.*s%.*s\n",
 738                len, name, patch->lines_added + patch->lines_deleted,
 739                add, pluses, del, minuses);
 740}
 741
 742static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
 743{
 744        int fd;
 745        unsigned long got;
 746
 747        switch (st->st_mode & S_IFMT) {
 748        case S_IFLNK:
 749                return readlink(path, buf, size);
 750        case S_IFREG:
 751                fd = open(path, O_RDONLY);
 752                if (fd < 0)
 753                        return error("unable to open %s", path);
 754                got = 0;
 755                for (;;) {
 756                        int ret = read(fd, buf + got, size - got);
 757                        if (ret < 0) {
 758                                if (errno == EAGAIN)
 759                                        continue;
 760                                break;
 761                        }
 762                        if (!ret)
 763                                break;
 764                        got += ret;
 765                }
 766                close(fd);
 767                return got;
 768
 769        default:
 770                return -1;
 771        }
 772}
 773
 774static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
 775{
 776        unsigned long start;
 777
 778        if (fragsize > size)
 779                return -1;
 780
 781        start = 0;
 782        if (line > 1) {
 783                line--;
 784                unsigned long offset = 0;
 785                while (start + offset <= size) {
 786                        if (buf[offset++] == '\n') {
 787                                start = offset;
 788                                if (!--line)
 789                                        break;
 790                        }
 791                }
 792        }
 793
 794        /* Exact line number? */
 795        if (!memcmp(buf + start, fragment, fragsize))
 796                return start;
 797
 798        /*
 799         * We should start searching forward and backward.
 800         */
 801        return -1;
 802}
 803
 804static int apply_one_fragment(char *buf, unsigned long *sizep, unsigned long *bufsizep, struct fragment *frag)
 805{
 806        const char *patch = frag->patch;
 807        int offset, size = frag->size;
 808        char *old = xmalloc(size);
 809        char *new = xmalloc(size);
 810        int oldsize = 0, newsize = 0;
 811
 812        while (size > 0) {
 813                int len = linelen(patch, size);
 814                int plen;
 815
 816                if (!len)
 817                        break;
 818
 819                /*
 820                 * "plen" is how much of the line we should use for
 821                 * the actual patch data. Normally we just remove the
 822                 * first character on the line, but if the line is
 823                 * followed by "\ No newline", then we also remove the
 824                 * last one (which is the newline, of course).
 825                 */
 826                plen = len-1;
 827                if (len > size && patch[len] == '\\')
 828                        plen--;
 829                switch (*patch) {
 830                case ' ':
 831                case '-':
 832                        memcpy(old + oldsize, patch + 1, plen);
 833                        oldsize += plen;
 834                        if (*patch == '-')
 835                                break;
 836                /* Fall-through for ' ' */
 837                case '+':
 838                        memcpy(new + newsize, patch + 1, plen);
 839                        newsize += plen;
 840                        break;
 841                case '@': case '\\':
 842                        /* Ignore it, we already handled it */
 843                        break;
 844                default:
 845                        return -1;
 846                }
 847                patch += len;
 848                size -= len;
 849        }
 850
 851        offset = find_offset(buf, *sizep, old, oldsize, frag->newpos);
 852        if (offset >= 0) {
 853                printf("found at offset %d\n", offset);
 854                offset = 0;
 855        }
 856
 857        free(old);
 858        free(new);
 859        return offset;
 860}
 861
 862static int apply_fragments(char *buf, unsigned long *sizep, unsigned long *bufsizep, struct patch *patch)
 863{
 864        struct fragment *frag = patch->fragments;
 865
 866        while (frag) {
 867                if (apply_one_fragment(buf, sizep, bufsizep, frag) < 0)
 868                        return error("patch failed: %s:%d", patch->old_name, frag->oldpos);
 869                frag = frag->next;
 870        }
 871}
 872
 873static int apply_data(struct patch *patch, struct stat *st)
 874{
 875        unsigned long size, bufsize;
 876        void *buf;
 877
 878        if (!patch->old_name || !patch->fragments)
 879                return 0;
 880        size = st->st_size;
 881        bufsize = size + 16;
 882        buf = xmalloc(bufsize);
 883        if (read_old_data(st, patch->old_name, buf, bufsize) != size)
 884                return error("read of %s failed", patch->old_name);
 885        if (apply_fragments(buf, &size, &bufsize, patch) < 0)
 886                return -1;
 887        return 0;
 888}
 889
 890static int check_patch(struct patch *patch)
 891{
 892        struct stat st;
 893        const char *old_name = patch->old_name;
 894        const char *new_name = patch->new_name;
 895
 896        if (old_name) {
 897                int changed;
 898
 899                if (lstat(old_name, &st) < 0)
 900                        return error("%s: %s\n", strerror(errno));
 901                if (check_index) {
 902                        int pos = cache_name_pos(old_name, strlen(old_name));
 903                        if (pos < 0)
 904                                return error("%s: does not exist in index", old_name);
 905                        changed = ce_match_stat(active_cache[pos], &st);
 906                        if (changed)
 907                                return error("%s: does not match index", old_name);
 908                }
 909                if (patch->is_new < 0)
 910                        patch->is_new = 0;
 911                if (!patch->old_mode)
 912                        patch->old_mode = st.st_mode;
 913                if ((st.st_mode ^ patch->old_mode) & S_IFMT)
 914                        return error("%s: wrong type", old_name);
 915                if (st.st_mode != patch->old_mode)
 916                        fprintf(stderr, "warning: %s has type %o, expected %o\n",
 917                                old_name, st.st_mode, patch->old_mode);
 918        }
 919
 920        if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
 921                if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
 922                        return error("%s: already exists in index", new_name);
 923                if (!lstat(new_name, &st))
 924                        return error("%s: already exists in working directory", new_name);
 925                if (errno != ENOENT)
 926                        return error("%s: %s", new_name, strerror(errno));
 927        }
 928
 929        if (new_name && old_name) {
 930                int same = !strcmp(old_name, new_name);
 931                if (!patch->new_mode)
 932                        patch->new_mode = patch->old_mode;
 933                if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
 934                        return error("new mode (%o) of %s does not match old mode (%o)%s%s",
 935                                patch->new_mode, new_name, patch->old_mode,
 936                                same ? "" : " of ", same ? "" : old_name);
 937        }       
 938
 939        if (apply_data(patch, &st) < 0)
 940                return error("%s: patch does not apply", old_name);
 941        return 0;
 942}
 943
 944static int check_patch_list(struct patch *patch)
 945{
 946        int error = 0;
 947
 948        for (;patch ; patch = patch->next)
 949                error |= check_patch(patch);
 950        return error;
 951}
 952
 953static void show_file(int c, unsigned int mode, const char *name)
 954{
 955        printf("%c %o %s\n", c, mode, name);
 956}
 957
 958static void show_file_list(struct patch *patch)
 959{
 960        for (;patch ; patch = patch->next) {
 961                if (patch->is_rename) {
 962                        show_file('-', patch->old_mode, patch->old_name);
 963                        show_file('+', patch->new_mode, patch->new_name);
 964                        continue;
 965                }
 966                if (patch->is_copy || patch->is_new) {
 967                        show_file('+', patch->new_mode, patch->new_name);
 968                        continue;
 969                }
 970                if (patch->is_delete) {
 971                        show_file('-', patch->old_mode, patch->old_name);
 972                        continue;
 973                }
 974                if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) {
 975                        printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name);
 976                        continue;
 977                }
 978                printf("M %o %s\n", patch->old_mode, patch->old_name);
 979        }
 980}
 981
 982static void stat_patch_list(struct patch *patch)
 983{
 984        int files, adds, dels;
 985
 986        for (files = adds = dels = 0 ; patch ; patch = patch->next) {
 987                files++;
 988                adds += patch->lines_added;
 989                dels += patch->lines_deleted;
 990                show_stats(patch);
 991        }
 992
 993        printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
 994}
 995
 996static void patch_stats(struct patch *patch)
 997{
 998        int lines = patch->lines_added + patch->lines_deleted;
 999
1000        if (lines > max_change)
1001                max_change = lines;
1002        if (patch->old_name) {
1003                int len = strlen(patch->old_name);
1004                if (len > max_len)
1005                        max_len = len;
1006        }
1007        if (patch->new_name) {
1008                int len = strlen(patch->new_name);
1009                if (len > max_len)
1010                        max_len = len;
1011        }
1012}
1013
1014static int apply_patch(int fd)
1015{
1016        unsigned long offset, size;
1017        char *buffer = read_patch_file(fd, &size);
1018        struct patch *list = NULL, **listp = &list;
1019
1020        if (!buffer)
1021                return -1;
1022        offset = 0;
1023        while (size > 0) {
1024                struct patch *patch;
1025                int nr;
1026
1027                patch = xmalloc(sizeof(*patch));
1028                memset(patch, 0, sizeof(*patch));
1029                nr = parse_chunk(buffer + offset, size, patch);
1030                if (nr < 0)
1031                        break;
1032                patch_stats(patch);
1033                *listp = patch;
1034                listp = &patch->next;
1035                offset += nr;
1036                size -= nr;
1037        }
1038
1039        if ((check || apply) && check_patch_list(list) < 0)
1040                exit(1);
1041
1042        if (show_files)
1043                show_file_list(list);
1044
1045        if (diffstat)
1046                stat_patch_list(list);
1047
1048        free(buffer);
1049        return 0;
1050}
1051
1052int main(int argc, char **argv)
1053{
1054        int i;
1055        int read_stdin = 1;
1056
1057        if (read_cache() < 0)
1058                die("unable to read index file");
1059
1060        for (i = 1; i < argc; i++) {
1061                const char *arg = argv[i];
1062                int fd;
1063
1064                if (!strcmp(arg, "-")) {
1065                        apply_patch(0);
1066                        read_stdin = 0;
1067                        continue;
1068                }
1069                if (!strcmp(arg, "--no-merge")) {
1070                        merge_patch = 0;
1071                        continue;
1072                }
1073                if (!strcmp(arg, "--stat")) {
1074                        apply = 0;
1075                        diffstat = 1;
1076                        continue;
1077                }
1078                if (!strcmp(arg, "--check")) {
1079                        apply = 0;
1080                        check = 1;
1081                        continue;
1082                }
1083                if (!strcmp(arg, "--index")) {
1084                        check_index = 1;
1085                        continue;
1086                }
1087                if (!strcmp(arg, "--show-files")) {
1088                        show_files = 1;
1089                        continue;
1090                }
1091                fd = open(arg, O_RDONLY);
1092                if (fd < 0)
1093                        usage(apply_usage);
1094                read_stdin = 0;
1095                apply_patch(fd);
1096                close(fd);
1097        }
1098        if (read_stdin)
1099                apply_patch(0);
1100        return 0;
1101}