e37c4ebf524672fc738a74537b7e9fef6fa508b1
   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 */
   9#include <fnmatch.h>
  10#include "cache.h"
  11#include "quote.h"
  12#include "blob.h"
  13#include "delta.h"
  14
  15//  --check turns on checking that the working tree matches the
  16//    files that are being modified, but doesn't apply the patch
  17//  --stat does just a diffstat, and doesn't actually apply
  18//  --numstat does numeric diffstat, and doesn't actually apply
  19//  --index-info shows the old and new index info for paths if available.
  20//
  21static const char *prefix;
  22static int prefix_length = -1;
  23
  24static int p_value = 1;
  25static int allow_binary_replacement = 0;
  26static int check_index = 0;
  27static int write_index = 0;
  28static int diffstat = 0;
  29static int numstat = 0;
  30static int summary = 0;
  31static int check = 0;
  32static int apply = 1;
  33static int no_add = 0;
  34static int show_index_info = 0;
  35static int line_termination = '\n';
  36static unsigned long p_context = -1;
  37static const char apply_usage[] =
  38"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
  39
  40static enum whitespace_eol {
  41        nowarn_whitespace,
  42        warn_on_whitespace,
  43        error_on_whitespace,
  44        strip_whitespace,
  45} new_whitespace = warn_on_whitespace;
  46static int whitespace_error = 0;
  47static int squelch_whitespace_errors = 5;
  48static int applied_after_stripping = 0;
  49static const char *patch_input_file = NULL;
  50
  51static void parse_whitespace_option(const char *option)
  52{
  53        if (!option) {
  54                new_whitespace = warn_on_whitespace;
  55                return;
  56        }
  57        if (!strcmp(option, "warn")) {
  58                new_whitespace = warn_on_whitespace;
  59                return;
  60        }
  61        if (!strcmp(option, "nowarn")) {
  62                new_whitespace = nowarn_whitespace;
  63                return;
  64        }
  65        if (!strcmp(option, "error")) {
  66                new_whitespace = error_on_whitespace;
  67                return;
  68        }
  69        if (!strcmp(option, "error-all")) {
  70                new_whitespace = error_on_whitespace;
  71                squelch_whitespace_errors = 0;
  72                return;
  73        }
  74        if (!strcmp(option, "strip")) {
  75                new_whitespace = strip_whitespace;
  76                return;
  77        }
  78        die("unrecognized whitespace option '%s'", option);
  79}
  80
  81static void set_default_whitespace_mode(const char *whitespace_option)
  82{
  83        if (!whitespace_option && !apply_default_whitespace) {
  84                new_whitespace = (apply
  85                                  ? warn_on_whitespace
  86                                  : nowarn_whitespace);
  87        }
  88}
  89
  90/*
  91 * For "diff-stat" like behaviour, we keep track of the biggest change
  92 * we've seen, and the longest filename. That allows us to do simple
  93 * scaling.
  94 */
  95static int max_change, max_len;
  96
  97/*
  98 * Various "current state", notably line numbers and what
  99 * file (and how) we're patching right now.. The "is_xxxx"
 100 * things are flags, where -1 means "don't know yet".
 101 */
 102static int linenr = 1;
 103
 104struct fragment {
 105        unsigned long leading, trailing;
 106        unsigned long oldpos, oldlines;
 107        unsigned long newpos, newlines;
 108        const char *patch;
 109        int size;
 110        struct fragment *next;
 111};
 112
 113struct patch {
 114        char *new_name, *old_name, *def_name;
 115        unsigned int old_mode, new_mode;
 116        int is_rename, is_copy, is_new, is_delete, is_binary;
 117        int lines_added, lines_deleted;
 118        int score;
 119        struct fragment *fragments;
 120        char *result;
 121        unsigned long resultsize;
 122        char old_sha1_prefix[41];
 123        char new_sha1_prefix[41];
 124        struct patch *next;
 125};
 126
 127#define CHUNKSIZE (8192)
 128#define SLOP (16)
 129
 130static void *read_patch_file(int fd, unsigned long *sizep)
 131{
 132        unsigned long size = 0, alloc = CHUNKSIZE;
 133        void *buffer = xmalloc(alloc);
 134
 135        for (;;) {
 136                int nr = alloc - size;
 137                if (nr < 1024) {
 138                        alloc += CHUNKSIZE;
 139                        buffer = xrealloc(buffer, alloc);
 140                        nr = alloc - size;
 141                }
 142                nr = xread(fd, buffer + size, nr);
 143                if (!nr)
 144                        break;
 145                if (nr < 0)
 146                        die("git-apply: read returned %s", strerror(errno));
 147                size += nr;
 148        }
 149        *sizep = size;
 150
 151        /*
 152         * Make sure that we have some slop in the buffer
 153         * so that we can do speculative "memcmp" etc, and
 154         * see to it that it is NUL-filled.
 155         */
 156        if (alloc < size + SLOP)
 157                buffer = xrealloc(buffer, size + SLOP);
 158        memset(buffer + size, 0, SLOP);
 159        return buffer;
 160}
 161
 162static unsigned long linelen(const char *buffer, unsigned long size)
 163{
 164        unsigned long len = 0;
 165        while (size--) {
 166                len++;
 167                if (*buffer++ == '\n')
 168                        break;
 169        }
 170        return len;
 171}
 172
 173static int is_dev_null(const char *str)
 174{
 175        return !memcmp("/dev/null", str, 9) && isspace(str[9]);
 176}
 177
 178#define TERM_SPACE      1
 179#define TERM_TAB        2
 180
 181static int name_terminate(const char *name, int namelen, int c, int terminate)
 182{
 183        if (c == ' ' && !(terminate & TERM_SPACE))
 184                return 0;
 185        if (c == '\t' && !(terminate & TERM_TAB))
 186                return 0;
 187
 188        return 1;
 189}
 190
 191static char * find_name(const char *line, char *def, int p_value, int terminate)
 192{
 193        int len;
 194        const char *start = line;
 195        char *name;
 196
 197        if (*line == '"') {
 198                /* Proposed "new-style" GNU patch/diff format; see
 199                 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
 200                 */
 201                name = unquote_c_style(line, NULL);
 202                if (name) {
 203                        char *cp = name;
 204                        while (p_value) {
 205                                cp = strchr(name, '/');
 206                                if (!cp)
 207                                        break;
 208                                cp++;
 209                                p_value--;
 210                        }
 211                        if (cp) {
 212                                /* name can later be freed, so we need
 213                                 * to memmove, not just return cp
 214                                 */
 215                                memmove(name, cp, strlen(cp) + 1);
 216                                free(def);
 217                                return name;
 218                        }
 219                        else {
 220                                free(name);
 221                                name = NULL;
 222                        }
 223                }
 224        }
 225
 226        for (;;) {
 227                char c = *line;
 228
 229                if (isspace(c)) {
 230                        if (c == '\n')
 231                                break;
 232                        if (name_terminate(start, line-start, c, terminate))
 233                                break;
 234                }
 235                line++;
 236                if (c == '/' && !--p_value)
 237                        start = line;
 238        }
 239        if (!start)
 240                return def;
 241        len = line - start;
 242        if (!len)
 243                return def;
 244
 245        /*
 246         * Generally we prefer the shorter name, especially
 247         * if the other one is just a variation of that with
 248         * something else tacked on to the end (ie "file.orig"
 249         * or "file~").
 250         */
 251        if (def) {
 252                int deflen = strlen(def);
 253                if (deflen < len && !strncmp(start, def, deflen))
 254                        return def;
 255        }
 256
 257        name = xmalloc(len + 1);
 258        memcpy(name, start, len);
 259        name[len] = 0;
 260        free(def);
 261        return name;
 262}
 263
 264/*
 265 * Get the name etc info from the --/+++ lines of a traditional patch header
 266 *
 267 * NOTE! This hardcodes "-p1" behaviour in filename detection.
 268 *
 269 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
 270 * files, we can happily check the index for a match, but for creating a
 271 * new file we should try to match whatever "patch" does. I have no idea.
 272 */
 273static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
 274{
 275        char *name;
 276
 277        first += 4;     // skip "--- "
 278        second += 4;    // skip "+++ "
 279        if (is_dev_null(first)) {
 280                patch->is_new = 1;
 281                patch->is_delete = 0;
 282                name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
 283                patch->new_name = name;
 284        } else if (is_dev_null(second)) {
 285                patch->is_new = 0;
 286                patch->is_delete = 1;
 287                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 288                patch->old_name = name;
 289        } else {
 290                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 291                name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
 292                patch->old_name = patch->new_name = name;
 293        }
 294        if (!name)
 295                die("unable to find filename in patch at line %d", linenr);
 296}
 297
 298static int gitdiff_hdrend(const char *line, struct patch *patch)
 299{
 300        return -1;
 301}
 302
 303/*
 304 * We're anal about diff header consistency, to make
 305 * sure that we don't end up having strange ambiguous
 306 * patches floating around.
 307 *
 308 * As a result, gitdiff_{old|new}name() will check
 309 * their names against any previous information, just
 310 * to make sure..
 311 */
 312static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 313{
 314        if (!orig_name && !isnull)
 315                return find_name(line, NULL, 1, 0);
 316
 317        if (orig_name) {
 318                int len;
 319                const char *name;
 320                char *another;
 321                name = orig_name;
 322                len = strlen(name);
 323                if (isnull)
 324                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
 325                another = find_name(line, NULL, 1, 0);
 326                if (!another || memcmp(another, name, len))
 327                        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
 328                free(another);
 329                return orig_name;
 330        }
 331        else {
 332                /* expect "/dev/null" */
 333                if (memcmp("/dev/null", line, 9) || line[9] != '\n')
 334                        die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
 335                return NULL;
 336        }
 337}
 338
 339static int gitdiff_oldname(const char *line, struct patch *patch)
 340{
 341        patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
 342        return 0;
 343}
 344
 345static int gitdiff_newname(const char *line, struct patch *patch)
 346{
 347        patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
 348        return 0;
 349}
 350
 351static int gitdiff_oldmode(const char *line, struct patch *patch)
 352{
 353        patch->old_mode = strtoul(line, NULL, 8);
 354        return 0;
 355}
 356
 357static int gitdiff_newmode(const char *line, struct patch *patch)
 358{
 359        patch->new_mode = strtoul(line, NULL, 8);
 360        return 0;
 361}
 362
 363static int gitdiff_delete(const char *line, struct patch *patch)
 364{
 365        patch->is_delete = 1;
 366        patch->old_name = patch->def_name;
 367        return gitdiff_oldmode(line, patch);
 368}
 369
 370static int gitdiff_newfile(const char *line, struct patch *patch)
 371{
 372        patch->is_new = 1;
 373        patch->new_name = patch->def_name;
 374        return gitdiff_newmode(line, patch);
 375}
 376
 377static int gitdiff_copysrc(const char *line, struct patch *patch)
 378{
 379        patch->is_copy = 1;
 380        patch->old_name = find_name(line, NULL, 0, 0);
 381        return 0;
 382}
 383
 384static int gitdiff_copydst(const char *line, struct patch *patch)
 385{
 386        patch->is_copy = 1;
 387        patch->new_name = find_name(line, NULL, 0, 0);
 388        return 0;
 389}
 390
 391static int gitdiff_renamesrc(const char *line, struct patch *patch)
 392{
 393        patch->is_rename = 1;
 394        patch->old_name = find_name(line, NULL, 0, 0);
 395        return 0;
 396}
 397
 398static int gitdiff_renamedst(const char *line, struct patch *patch)
 399{
 400        patch->is_rename = 1;
 401        patch->new_name = find_name(line, NULL, 0, 0);
 402        return 0;
 403}
 404
 405static int gitdiff_similarity(const char *line, struct patch *patch)
 406{
 407        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 408                patch->score = 0;
 409        return 0;
 410}
 411
 412static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 413{
 414        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 415                patch->score = 0;
 416        return 0;
 417}
 418
 419static int gitdiff_index(const char *line, struct patch *patch)
 420{
 421        /* index line is N hexadecimal, "..", N hexadecimal,
 422         * and optional space with octal mode.
 423         */
 424        const char *ptr, *eol;
 425        int len;
 426
 427        ptr = strchr(line, '.');
 428        if (!ptr || ptr[1] != '.' || 40 < ptr - line)
 429                return 0;
 430        len = ptr - line;
 431        memcpy(patch->old_sha1_prefix, line, len);
 432        patch->old_sha1_prefix[len] = 0;
 433
 434        line = ptr + 2;
 435        ptr = strchr(line, ' ');
 436        eol = strchr(line, '\n');
 437
 438        if (!ptr || eol < ptr)
 439                ptr = eol;
 440        len = ptr - line;
 441
 442        if (40 < len)
 443                return 0;
 444        memcpy(patch->new_sha1_prefix, line, len);
 445        patch->new_sha1_prefix[len] = 0;
 446        if (*ptr == ' ')
 447                patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
 448        return 0;
 449}
 450
 451/*
 452 * This is normal for a diff that doesn't change anything: we'll fall through
 453 * into the next diff. Tell the parser to break out.
 454 */
 455static int gitdiff_unrecognized(const char *line, struct patch *patch)
 456{
 457        return -1;
 458}
 459
 460static const char *stop_at_slash(const char *line, int llen)
 461{
 462        int i;
 463
 464        for (i = 0; i < llen; i++) {
 465                int ch = line[i];
 466                if (ch == '/')
 467                        return line + i;
 468        }
 469        return NULL;
 470}
 471
 472/* This is to extract the same name that appears on "diff --git"
 473 * line.  We do not find and return anything if it is a rename
 474 * patch, and it is OK because we will find the name elsewhere.
 475 * We need to reliably find name only when it is mode-change only,
 476 * creation or deletion of an empty file.  In any of these cases,
 477 * both sides are the same name under a/ and b/ respectively.
 478 */
 479static char *git_header_name(char *line, int llen)
 480{
 481        int len;
 482        const char *name;
 483        const char *second = NULL;
 484
 485        line += strlen("diff --git ");
 486        llen -= strlen("diff --git ");
 487
 488        if (*line == '"') {
 489                const char *cp;
 490                char *first = unquote_c_style(line, &second);
 491                if (!first)
 492                        return NULL;
 493
 494                /* advance to the first slash */
 495                cp = stop_at_slash(first, strlen(first));
 496                if (!cp || cp == first) {
 497                        /* we do not accept absolute paths */
 498                free_first_and_fail:
 499                        free(first);
 500                        return NULL;
 501                }
 502                len = strlen(cp+1);
 503                memmove(first, cp+1, len+1); /* including NUL */
 504
 505                /* second points at one past closing dq of name.
 506                 * find the second name.
 507                 */
 508                while ((second < line + llen) && isspace(*second))
 509                        second++;
 510
 511                if (line + llen <= second)
 512                        goto free_first_and_fail;
 513                if (*second == '"') {
 514                        char *sp = unquote_c_style(second, NULL);
 515                        if (!sp)
 516                                goto free_first_and_fail;
 517                        cp = stop_at_slash(sp, strlen(sp));
 518                        if (!cp || cp == sp) {
 519                        free_both_and_fail:
 520                                free(sp);
 521                                goto free_first_and_fail;
 522                        }
 523                        /* They must match, otherwise ignore */
 524                        if (strcmp(cp+1, first))
 525                                goto free_both_and_fail;
 526                        free(sp);
 527                        return first;
 528                }
 529
 530                /* unquoted second */
 531                cp = stop_at_slash(second, line + llen - second);
 532                if (!cp || cp == second)
 533                        goto free_first_and_fail;
 534                cp++;
 535                if (line + llen - cp != len + 1 ||
 536                    memcmp(first, cp, len))
 537                        goto free_first_and_fail;
 538                return first;
 539        }
 540
 541        /* unquoted first name */
 542        name = stop_at_slash(line, llen);
 543        if (!name || name == line)
 544                return NULL;
 545
 546        name++;
 547
 548        /* since the first name is unquoted, a dq if exists must be
 549         * the beginning of the second name.
 550         */
 551        for (second = name; second < line + llen; second++) {
 552                if (*second == '"') {
 553                        const char *cp = second;
 554                        const char *np;
 555                        char *sp = unquote_c_style(second, NULL);
 556
 557                        if (!sp)
 558                                return NULL;
 559                        np = stop_at_slash(sp, strlen(sp));
 560                        if (!np || np == sp) {
 561                        free_second_and_fail:
 562                                free(sp);
 563                                return NULL;
 564                        }
 565                        np++;
 566                        len = strlen(np);
 567                        if (len < cp - name &&
 568                            !strncmp(np, name, len) &&
 569                            isspace(name[len])) {
 570                                /* Good */
 571                                memmove(sp, np, len + 1);
 572                                return sp;
 573                        }
 574                        goto free_second_and_fail;
 575                }
 576        }
 577
 578        /*
 579         * Accept a name only if it shows up twice, exactly the same
 580         * form.
 581         */
 582        for (len = 0 ; ; len++) {
 583                char c = name[len];
 584
 585                switch (c) {
 586                default:
 587                        continue;
 588                case '\n':
 589                        return NULL;
 590                case '\t': case ' ':
 591                        second = name+len;
 592                        for (;;) {
 593                                char c = *second++;
 594                                if (c == '\n')
 595                                        return NULL;
 596                                if (c == '/')
 597                                        break;
 598                        }
 599                        if (second[len] == '\n' && !memcmp(name, second, len)) {
 600                                char *ret = xmalloc(len + 1);
 601                                memcpy(ret, name, len);
 602                                ret[len] = 0;
 603                                return ret;
 604                        }
 605                }
 606        }
 607        return NULL;
 608}
 609
 610/* Verify that we recognize the lines following a git header */
 611static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
 612{
 613        unsigned long offset;
 614
 615        /* A git diff has explicit new/delete information, so we don't guess */
 616        patch->is_new = 0;
 617        patch->is_delete = 0;
 618
 619        /*
 620         * Some things may not have the old name in the
 621         * rest of the headers anywhere (pure mode changes,
 622         * or removing or adding empty files), so we get
 623         * the default name from the header.
 624         */
 625        patch->def_name = git_header_name(line, len);
 626
 627        line += len;
 628        size -= len;
 629        linenr++;
 630        for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 631                static const struct opentry {
 632                        const char *str;
 633                        int (*fn)(const char *, struct patch *);
 634                } optable[] = {
 635                        { "@@ -", gitdiff_hdrend },
 636                        { "--- ", gitdiff_oldname },
 637                        { "+++ ", gitdiff_newname },
 638                        { "old mode ", gitdiff_oldmode },
 639                        { "new mode ", gitdiff_newmode },
 640                        { "deleted file mode ", gitdiff_delete },
 641                        { "new file mode ", gitdiff_newfile },
 642                        { "copy from ", gitdiff_copysrc },
 643                        { "copy to ", gitdiff_copydst },
 644                        { "rename old ", gitdiff_renamesrc },
 645                        { "rename new ", gitdiff_renamedst },
 646                        { "rename from ", gitdiff_renamesrc },
 647                        { "rename to ", gitdiff_renamedst },
 648                        { "similarity index ", gitdiff_similarity },
 649                        { "dissimilarity index ", gitdiff_dissimilarity },
 650                        { "index ", gitdiff_index },
 651                        { "", gitdiff_unrecognized },
 652                };
 653                int i;
 654
 655                len = linelen(line, size);
 656                if (!len || line[len-1] != '\n')
 657                        break;
 658                for (i = 0; i < ARRAY_SIZE(optable); i++) {
 659                        const struct opentry *p = optable + i;
 660                        int oplen = strlen(p->str);
 661                        if (len < oplen || memcmp(p->str, line, oplen))
 662                                continue;
 663                        if (p->fn(line + oplen, patch) < 0)
 664                                return offset;
 665                        break;
 666                }
 667        }
 668
 669        return offset;
 670}
 671
 672static int parse_num(const char *line, unsigned long *p)
 673{
 674        char *ptr;
 675
 676        if (!isdigit(*line))
 677                return 0;
 678        *p = strtoul(line, &ptr, 10);
 679        return ptr - line;
 680}
 681
 682static int parse_range(const char *line, int len, int offset, const char *expect,
 683                        unsigned long *p1, unsigned long *p2)
 684{
 685        int digits, ex;
 686
 687        if (offset < 0 || offset >= len)
 688                return -1;
 689        line += offset;
 690        len -= offset;
 691
 692        digits = parse_num(line, p1);
 693        if (!digits)
 694                return -1;
 695
 696        offset += digits;
 697        line += digits;
 698        len -= digits;
 699
 700        *p2 = 1;
 701        if (*line == ',') {
 702                digits = parse_num(line+1, p2);
 703                if (!digits)
 704                        return -1;
 705
 706                offset += digits+1;
 707                line += digits+1;
 708                len -= digits+1;
 709        }
 710
 711        ex = strlen(expect);
 712        if (ex > len)
 713                return -1;
 714        if (memcmp(line, expect, ex))
 715                return -1;
 716
 717        return offset + ex;
 718}
 719
 720/*
 721 * Parse a unified diff fragment header of the
 722 * form "@@ -a,b +c,d @@"
 723 */
 724static int parse_fragment_header(char *line, int len, struct fragment *fragment)
 725{
 726        int offset;
 727
 728        if (!len || line[len-1] != '\n')
 729                return -1;
 730
 731        /* Figure out the number of lines in a fragment */
 732        offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
 733        offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
 734
 735        return offset;
 736}
 737
 738static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
 739{
 740        unsigned long offset, len;
 741
 742        patch->is_rename = patch->is_copy = 0;
 743        patch->is_new = patch->is_delete = -1;
 744        patch->old_mode = patch->new_mode = 0;
 745        patch->old_name = patch->new_name = NULL;
 746        for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
 747                unsigned long nextlen;
 748
 749                len = linelen(line, size);
 750                if (!len)
 751                        break;
 752
 753                /* Testing this early allows us to take a few shortcuts.. */
 754                if (len < 6)
 755                        continue;
 756
 757                /*
 758                 * Make sure we don't find any unconnected patch fragmants.
 759                 * That's a sign that we didn't find a header, and that a
 760                 * patch has become corrupted/broken up.
 761                 */
 762                if (!memcmp("@@ -", line, 4)) {
 763                        struct fragment dummy;
 764                        if (parse_fragment_header(line, len, &dummy) < 0)
 765                                continue;
 766                        error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
 767                }
 768
 769                if (size < len + 6)
 770                        break;
 771
 772                /*
 773                 * Git patch? It might not have a real patch, just a rename
 774                 * or mode change, so we handle that specially
 775                 */
 776                if (!memcmp("diff --git ", line, 11)) {
 777                        int git_hdr_len = parse_git_header(line, len, size, patch);
 778                        if (git_hdr_len <= len)
 779                                continue;
 780                        if (!patch->old_name && !patch->new_name) {
 781                                if (!patch->def_name)
 782                                        die("git diff header lacks filename information (line %d)", linenr);
 783                                patch->old_name = patch->new_name = patch->def_name;
 784                        }
 785                        *hdrsize = git_hdr_len;
 786                        return offset;
 787                }
 788
 789                /** --- followed by +++ ? */
 790                if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
 791                        continue;
 792
 793                /*
 794                 * We only accept unified patches, so we want it to
 795                 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
 796                 * minimum
 797                 */
 798                nextlen = linelen(line + len, size - len);
 799                if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
 800                        continue;
 801
 802                /* Ok, we'll consider it a patch */
 803                parse_traditional_patch(line, line+len, patch);
 804                *hdrsize = len + nextlen;
 805                linenr += 2;
 806                return offset;
 807        }
 808        return -1;
 809}
 810
 811/*
 812 * Parse a unified diff. Note that this really needs
 813 * to parse each fragment separately, since the only
 814 * way to know the difference between a "---" that is
 815 * part of a patch, and a "---" that starts the next
 816 * patch is to look at the line counts..
 817 */
 818static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
 819{
 820        int added, deleted;
 821        int len = linelen(line, size), offset;
 822        unsigned long oldlines, newlines;
 823        unsigned long leading, trailing;
 824
 825        offset = parse_fragment_header(line, len, fragment);
 826        if (offset < 0)
 827                return -1;
 828        oldlines = fragment->oldlines;
 829        newlines = fragment->newlines;
 830        leading = 0;
 831        trailing = 0;
 832
 833        if (patch->is_new < 0) {
 834                patch->is_new =  !oldlines;
 835                if (!oldlines)
 836                        patch->old_name = NULL;
 837        }
 838        if (patch->is_delete < 0) {
 839                patch->is_delete = !newlines;
 840                if (!newlines)
 841                        patch->new_name = NULL;
 842        }
 843
 844        if (patch->is_new && oldlines)
 845                return error("new file depends on old contents");
 846        if (patch->is_delete != !newlines) {
 847                if (newlines)
 848                        return error("deleted file still has contents");
 849                fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
 850        }
 851
 852        /* Parse the thing.. */
 853        line += len;
 854        size -= len;
 855        linenr++;
 856        added = deleted = 0;
 857        for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
 858                if (!oldlines && !newlines)
 859                        break;
 860                len = linelen(line, size);
 861                if (!len || line[len-1] != '\n')
 862                        return -1;
 863                switch (*line) {
 864                default:
 865                        return -1;
 866                case ' ':
 867                        oldlines--;
 868                        newlines--;
 869                        if (!deleted && !added)
 870                                leading++;
 871                        trailing++;
 872                        break;
 873                case '-':
 874                        deleted++;
 875                        oldlines--;
 876                        trailing = 0;
 877                        break;
 878                case '+':
 879                        /*
 880                         * We know len is at least two, since we have a '+' and
 881                         * we checked that the last character was a '\n' above.
 882                         * That is, an addition of an empty line would check
 883                         * the '+' here.  Sneaky...
 884                         */
 885                        if ((new_whitespace != nowarn_whitespace) &&
 886                            isspace(line[len-2])) {
 887                                whitespace_error++;
 888                                if (squelch_whitespace_errors &&
 889                                    squelch_whitespace_errors <
 890                                    whitespace_error)
 891                                        ;
 892                                else {
 893                                        fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
 894                                                patch_input_file,
 895                                                linenr, len-2, line+1);
 896                                }
 897                        }
 898                        added++;
 899                        newlines--;
 900                        trailing = 0;
 901                        break;
 902
 903                /* We allow "\ No newline at end of file". Depending
 904                 * on locale settings when the patch was produced we
 905                 * don't know what this line looks like. The only
 906                 * thing we do know is that it begins with "\ ".
 907                 * Checking for 12 is just for sanity check -- any
 908                 * l10n of "\ No newline..." is at least that long.
 909                 */
 910                case '\\':
 911                        if (len < 12 || memcmp(line, "\\ ", 2))
 912                                return -1;
 913                        break;
 914                }
 915        }
 916        if (oldlines || newlines)
 917                return -1;
 918        fragment->leading = leading;
 919        fragment->trailing = trailing;
 920
 921        /* If a fragment ends with an incomplete line, we failed to include
 922         * it in the above loop because we hit oldlines == newlines == 0
 923         * before seeing it.
 924         */
 925        if (12 < size && !memcmp(line, "\\ ", 2))
 926                offset += linelen(line, size);
 927
 928        patch->lines_added += added;
 929        patch->lines_deleted += deleted;
 930        return offset;
 931}
 932
 933static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
 934{
 935        unsigned long offset = 0;
 936        struct fragment **fragp = &patch->fragments;
 937
 938        while (size > 4 && !memcmp(line, "@@ -", 4)) {
 939                struct fragment *fragment;
 940                int len;
 941
 942                fragment = xcalloc(1, sizeof(*fragment));
 943                len = parse_fragment(line, size, patch, fragment);
 944                if (len <= 0)
 945                        die("corrupt patch at line %d", linenr);
 946
 947                fragment->patch = line;
 948                fragment->size = len;
 949
 950                *fragp = fragment;
 951                fragp = &fragment->next;
 952
 953                offset += len;
 954                line += len;
 955                size -= len;
 956        }
 957        return offset;
 958}
 959
 960static inline int metadata_changes(struct patch *patch)
 961{
 962        return  patch->is_rename > 0 ||
 963                patch->is_copy > 0 ||
 964                patch->is_new > 0 ||
 965                patch->is_delete ||
 966                (patch->old_mode && patch->new_mode &&
 967                 patch->old_mode != patch->new_mode);
 968}
 969
 970static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 971{
 972        /* We have read "GIT binary patch\n"; what follows is a
 973         * sequence of 'length-byte' followed by base-85 encoded
 974         * delta data.
 975         *
 976         * Each 5-byte sequence of base-85 encodes up to 4 bytes,
 977         * and we would limit the patch line to 66 characters,
 978         * so one line can fit up to 13 groups that would decode
 979         * to 52 bytes max.  The length byte 'A'-'Z' corresponds
 980         * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
 981         * The end of binary is signalled with an empty line.
 982         */
 983        int llen, used;
 984        struct fragment *fragment;
 985        char *delta = NULL;
 986
 987        patch->is_binary = 1;
 988        patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
 989        used = 0;
 990        while (1) {
 991                int byte_length, max_byte_length, newsize;
 992                llen = linelen(buffer, size);
 993                used += llen;
 994                linenr++;
 995                if (llen == 1)
 996                        break;
 997                /* Minimum line is "A00000\n" which is 7-byte long,
 998                 * and the line length must be multiple of 5 plus 2.
 999                 */
1000                if ((llen < 7) || (llen-2) % 5)
1001                        goto corrupt;
1002                max_byte_length = (llen - 2) / 5 * 4;
1003                byte_length = *buffer;
1004                if ('A' <= byte_length && byte_length <= 'Z')
1005                        byte_length = byte_length - 'A' + 1;
1006                else if ('a' <= byte_length && byte_length <= 'z')
1007                        byte_length = byte_length - 'a' + 27;
1008                else
1009                        goto corrupt;
1010                /* if the input length was not multiple of 4, we would
1011                 * have filler at the end but the filler should never
1012                 * exceed 3 bytes
1013                 */
1014                if (max_byte_length < byte_length ||
1015                    byte_length <= max_byte_length - 4)
1016                        goto corrupt;
1017                newsize = fragment->size + byte_length;
1018                delta = xrealloc(delta, newsize);
1019                if (decode_85(delta + fragment->size,
1020                              buffer + 1,
1021                              byte_length))
1022                        goto corrupt;
1023                fragment->size = newsize;
1024                buffer += llen;
1025                size -= llen;
1026        }
1027        fragment->patch = delta;
1028        return used;
1029 corrupt:
1030        return error("corrupt binary patch at line %d: %.*s",
1031                     linenr-1, llen-1, buffer);
1032}
1033
1034static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1035{
1036        int hdrsize, patchsize;
1037        int offset = find_header(buffer, size, &hdrsize, patch);
1038
1039        if (offset < 0)
1040                return offset;
1041
1042        patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
1043
1044        if (!patchsize) {
1045                static const char *binhdr[] = {
1046                        "Binary files ",
1047                        "Files ",
1048                        NULL,
1049                };
1050                static const char git_binary[] = "GIT binary patch\n";
1051                int i;
1052                int hd = hdrsize + offset;
1053                unsigned long llen = linelen(buffer + hd, size - hd);
1054
1055                if (llen == sizeof(git_binary) - 1 &&
1056                    !memcmp(git_binary, buffer + hd, llen)) {
1057                        int used;
1058                        linenr++;
1059                        used = parse_binary(buffer + hd + llen,
1060                                            size - hd - llen, patch);
1061                        if (used)
1062                                patchsize = used + llen;
1063                        else
1064                                patchsize = 0;
1065                }
1066                else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1067                        for (i = 0; binhdr[i]; i++) {
1068                                int len = strlen(binhdr[i]);
1069                                if (len < size - hd &&
1070                                    !memcmp(binhdr[i], buffer + hd, len)) {
1071                                        linenr++;
1072                                        patch->is_binary = 1;
1073                                        patchsize = llen;
1074                                        break;
1075                                }
1076                        }
1077                }
1078
1079                /* Empty patch cannot be applied if:
1080                 * - it is a binary patch and we do not do binary_replace, or
1081                 * - text patch without metadata change
1082                 */
1083                if ((apply || check) &&
1084                    (patch->is_binary
1085                     ? !allow_binary_replacement
1086                     : !metadata_changes(patch)))
1087                        die("patch with only garbage at line %d", linenr);
1088        }
1089
1090        return offset + hdrsize + patchsize;
1091}
1092
1093static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1094static const char minuses[]= "----------------------------------------------------------------------";
1095
1096static void show_stats(struct patch *patch)
1097{
1098        const char *prefix = "";
1099        char *name = patch->new_name;
1100        char *qname = NULL;
1101        int len, max, add, del, total;
1102
1103        if (!name)
1104                name = patch->old_name;
1105
1106        if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
1107                qname = xmalloc(len + 1);
1108                quote_c_style(name, qname, NULL, 0);
1109                name = qname;
1110        }
1111
1112        /*
1113         * "scale" the filename
1114         */
1115        len = strlen(name);
1116        max = max_len;
1117        if (max > 50)
1118                max = 50;
1119        if (len > max) {
1120                char *slash;
1121                prefix = "...";
1122                max -= 3;
1123                name += len - max;
1124                slash = strchr(name, '/');
1125                if (slash)
1126                        name = slash;
1127        }
1128        len = max;
1129
1130        /*
1131         * scale the add/delete
1132         */
1133        max = max_change;
1134        if (max + len > 70)
1135                max = 70 - len;
1136
1137        add = patch->lines_added;
1138        del = patch->lines_deleted;
1139        total = add + del;
1140
1141        if (max_change > 0) {
1142                total = (total * max + max_change / 2) / max_change;
1143                add = (add * max + max_change / 2) / max_change;
1144                del = total - add;
1145        }
1146        if (patch->is_binary)
1147                printf(" %s%-*s |  Bin\n", prefix, len, name);
1148        else
1149                printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
1150                       len, name, patch->lines_added + patch->lines_deleted,
1151                       add, pluses, del, minuses);
1152        if (qname)
1153                free(qname);
1154}
1155
1156static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
1157{
1158        int fd;
1159        unsigned long got;
1160
1161        switch (st->st_mode & S_IFMT) {
1162        case S_IFLNK:
1163                return readlink(path, buf, size);
1164        case S_IFREG:
1165                fd = open(path, O_RDONLY);
1166                if (fd < 0)
1167                        return error("unable to open %s", path);
1168                got = 0;
1169                for (;;) {
1170                        int ret = xread(fd, buf + got, size - got);
1171                        if (ret <= 0)
1172                                break;
1173                        got += ret;
1174                }
1175                close(fd);
1176                return got;
1177
1178        default:
1179                return -1;
1180        }
1181}
1182
1183static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1184{
1185        int i;
1186        unsigned long start, backwards, forwards;
1187
1188        if (fragsize > size)
1189                return -1;
1190
1191        start = 0;
1192        if (line > 1) {
1193                unsigned long offset = 0;
1194                i = line-1;
1195                while (offset + fragsize <= size) {
1196                        if (buf[offset++] == '\n') {
1197                                start = offset;
1198                                if (!--i)
1199                                        break;
1200                        }
1201                }
1202        }
1203
1204        /* Exact line number? */
1205        if (!memcmp(buf + start, fragment, fragsize))
1206                return start;
1207
1208        /*
1209         * There's probably some smart way to do this, but I'll leave
1210         * that to the smart and beautiful people. I'm simple and stupid.
1211         */
1212        backwards = start;
1213        forwards = start;
1214        for (i = 0; ; i++) {
1215                unsigned long try;
1216                int n;
1217
1218                /* "backward" */
1219                if (i & 1) {
1220                        if (!backwards) {
1221                                if (forwards + fragsize > size)
1222                                        break;
1223                                continue;
1224                        }
1225                        do {
1226                                --backwards;
1227                        } while (backwards && buf[backwards-1] != '\n');
1228                        try = backwards;
1229                } else {
1230                        while (forwards + fragsize <= size) {
1231                                if (buf[forwards++] == '\n')
1232                                        break;
1233                        }
1234                        try = forwards;
1235                }
1236
1237                if (try + fragsize > size)
1238                        continue;
1239                if (memcmp(buf + try, fragment, fragsize))
1240                        continue;
1241                n = (i >> 1)+1;
1242                if (i & 1)
1243                        n = -n;
1244                *lines = n;
1245                return try;
1246        }
1247
1248        /*
1249         * We should start searching forward and backward.
1250         */
1251        return -1;
1252}
1253
1254static void remove_first_line(const char **rbuf, int *rsize)
1255{
1256        const char *buf = *rbuf;
1257        int size = *rsize;
1258        unsigned long offset;
1259        offset = 0;
1260        while (offset <= size) {
1261                if (buf[offset++] == '\n')
1262                        break;
1263        }
1264        *rsize = size - offset;
1265        *rbuf = buf + offset;
1266}
1267
1268static void remove_last_line(const char **rbuf, int *rsize)
1269{
1270        const char *buf = *rbuf;
1271        int size = *rsize;
1272        unsigned long offset;
1273        offset = size - 1;
1274        while (offset > 0) {
1275                if (buf[--offset] == '\n')
1276                        break;
1277        }
1278        *rsize = offset + 1;
1279}
1280
1281struct buffer_desc {
1282        char *buffer;
1283        unsigned long size;
1284        unsigned long alloc;
1285};
1286
1287static int apply_line(char *output, const char *patch, int plen)
1288{
1289        /* plen is number of bytes to be copied from patch,
1290         * starting at patch+1 (patch[0] is '+').  Typically
1291         * patch[plen] is '\n'.
1292         */
1293        int add_nl_to_tail = 0;
1294        if ((new_whitespace == strip_whitespace) &&
1295            1 < plen && isspace(patch[plen-1])) {
1296                if (patch[plen] == '\n')
1297                        add_nl_to_tail = 1;
1298                plen--;
1299                while (0 < plen && isspace(patch[plen]))
1300                        plen--;
1301                applied_after_stripping++;
1302        }
1303        memcpy(output, patch + 1, plen);
1304        if (add_nl_to_tail)
1305                output[plen++] = '\n';
1306        return plen;
1307}
1308
1309static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1310{
1311        char *buf = desc->buffer;
1312        const char *patch = frag->patch;
1313        int offset, size = frag->size;
1314        char *old = xmalloc(size);
1315        char *new = xmalloc(size);
1316        const char *oldlines, *newlines;
1317        int oldsize = 0, newsize = 0;
1318        unsigned long leading, trailing;
1319        int pos, lines;
1320
1321        while (size > 0) {
1322                int len = linelen(patch, size);
1323                int plen;
1324
1325                if (!len)
1326                        break;
1327
1328                /*
1329                 * "plen" is how much of the line we should use for
1330                 * the actual patch data. Normally we just remove the
1331                 * first character on the line, but if the line is
1332                 * followed by "\ No newline", then we also remove the
1333                 * last one (which is the newline, of course).
1334                 */
1335                plen = len-1;
1336                if (len < size && patch[len] == '\\')
1337                        plen--;
1338                switch (*patch) {
1339                case ' ':
1340                case '-':
1341                        memcpy(old + oldsize, patch + 1, plen);
1342                        oldsize += plen;
1343                        if (*patch == '-')
1344                                break;
1345                /* Fall-through for ' ' */
1346                case '+':
1347                        if (*patch != '+' || !no_add)
1348                                newsize += apply_line(new + newsize, patch,
1349                                                      plen);
1350                        break;
1351                case '@': case '\\':
1352                        /* Ignore it, we already handled it */
1353                        break;
1354                default:
1355                        return -1;
1356                }
1357                patch += len;
1358                size -= len;
1359        }
1360
1361#ifdef NO_ACCURATE_DIFF
1362        if (oldsize > 0 && old[oldsize - 1] == '\n' &&
1363                        newsize > 0 && new[newsize - 1] == '\n') {
1364                oldsize--;
1365                newsize--;
1366        }
1367#endif
1368
1369        oldlines = old;
1370        newlines = new;
1371        leading = frag->leading;
1372        trailing = frag->trailing;
1373        lines = 0;
1374        pos = frag->newpos;
1375        for (;;) {
1376                offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1377                if (offset >= 0) {
1378                        int diff = newsize - oldsize;
1379                        unsigned long size = desc->size + diff;
1380                        unsigned long alloc = desc->alloc;
1381
1382                        /* Warn if it was necessary to reduce the number
1383                         * of context lines.
1384                         */
1385                        if ((leading != frag->leading) || (trailing != frag->trailing))
1386                                fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1387                                        leading, trailing, pos + lines);
1388
1389                        if (size > alloc) {
1390                                alloc = size + 8192;
1391                                desc->alloc = alloc;
1392                                buf = xrealloc(buf, alloc);
1393                                desc->buffer = buf;
1394                        }
1395                        desc->size = size;
1396                        memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1397                        memcpy(buf + offset, newlines, newsize);
1398                        offset = 0;
1399
1400                        break;
1401                }
1402
1403                /* Am I at my context limits? */
1404                if ((leading <= p_context) && (trailing <= p_context))
1405                        break;
1406                /* Reduce the number of context lines
1407                 * Reduce both leading and trailing if they are equal
1408                 * otherwise just reduce the larger context.
1409                 */
1410                if (leading >= trailing) {
1411                        remove_first_line(&oldlines, &oldsize);
1412                        remove_first_line(&newlines, &newsize);
1413                        pos--;
1414                        leading--;
1415                }
1416                if (trailing > leading) {
1417                        remove_last_line(&oldlines, &oldsize);
1418                        remove_last_line(&newlines, &newsize);
1419                        trailing--;
1420                }
1421        }
1422
1423        free(old);
1424        free(new);
1425        return offset;
1426}
1427
1428static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1429{
1430        const char *name = patch->old_name ? patch->old_name : patch->new_name;
1431        unsigned char sha1[20];
1432        unsigned char hdr[50];
1433        int hdrlen;
1434
1435        if (!allow_binary_replacement)
1436                return error("cannot apply binary patch to '%s' "
1437                             "without --allow-binary-replacement",
1438                             name);
1439
1440        /* For safety, we require patch index line to contain
1441         * full 40-byte textual SHA1 for old and new, at least for now.
1442         */
1443        if (strlen(patch->old_sha1_prefix) != 40 ||
1444            strlen(patch->new_sha1_prefix) != 40 ||
1445            get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1446            get_sha1_hex(patch->new_sha1_prefix, sha1))
1447                return error("cannot apply binary patch to '%s' "
1448                             "without full index line", name);
1449
1450        if (patch->old_name) {
1451                /* See if the old one matches what the patch
1452                 * applies to.
1453                 */
1454                write_sha1_file_prepare(desc->buffer, desc->size,
1455                                        blob_type, sha1, hdr, &hdrlen);
1456                if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1457                        return error("the patch applies to '%s' (%s), "
1458                                     "which does not match the "
1459                                     "current contents.",
1460                                     name, sha1_to_hex(sha1));
1461        }
1462        else {
1463                /* Otherwise, the old one must be empty. */
1464                if (desc->size)
1465                        return error("the patch applies to an empty "
1466                                     "'%s' but it is not empty", name);
1467        }
1468
1469        if (desc->buffer) {
1470                free(desc->buffer);
1471                desc->alloc = desc->size = 0;
1472        }
1473        get_sha1_hex(patch->new_sha1_prefix, sha1);
1474        if (!memcmp(sha1, null_sha1, 20))
1475                return 0; /* deletion patch */
1476
1477        if (has_sha1_file(sha1)) {
1478                char type[10];
1479                unsigned long size;
1480
1481                desc->buffer = read_sha1_file(sha1, type, &size);
1482                if (!desc->buffer)
1483                        return error("the necessary postimage %s for "
1484                                     "'%s' cannot be read",
1485                                     patch->new_sha1_prefix, name);
1486                desc->alloc = desc->size = size;
1487        }
1488        else {
1489                char type[10];
1490                unsigned long src_size, dst_size;
1491                void *src;
1492
1493                get_sha1_hex(patch->old_sha1_prefix, sha1);
1494                src = read_sha1_file(sha1, type, &src_size);
1495                if (!src)
1496                        return error("the necessary preimage %s for "
1497                                     "'%s' cannot be read",
1498                                     patch->old_sha1_prefix, name);
1499
1500                /* patch->fragment->patch has the delta data and
1501                 * we should apply it to the preimage.
1502                 */
1503                desc->buffer = patch_delta(src, src_size,
1504                                           (void*) patch->fragments->patch,
1505                                           patch->fragments->size,
1506                                           &dst_size);
1507                if (!desc->buffer)
1508                        return error("binary patch does not apply to '%s'",
1509                                     name);
1510                desc->size = desc->alloc = dst_size;
1511
1512                /* verify that the result matches */
1513                write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
1514                                        sha1, hdr, &hdrlen);
1515                if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1516                        return error("binary patch to '%s' creates incorrect result", name);
1517        }
1518
1519        return 0;
1520}
1521
1522static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1523{
1524        struct fragment *frag = patch->fragments;
1525        const char *name = patch->old_name ? patch->old_name : patch->new_name;
1526
1527        if (patch->is_binary)
1528                return apply_binary(desc, patch);
1529
1530        while (frag) {
1531                if (apply_one_fragment(desc, frag) < 0)
1532                        return error("patch failed: %s:%ld",
1533                                     name, frag->oldpos);
1534                frag = frag->next;
1535        }
1536        return 0;
1537}
1538
1539static int apply_data(struct patch *patch, struct stat *st)
1540{
1541        char *buf;
1542        unsigned long size, alloc;
1543        struct buffer_desc desc;
1544
1545        size = 0;
1546        alloc = 0;
1547        buf = NULL;
1548        if (patch->old_name) {
1549                size = st->st_size;
1550                alloc = size + 8192;
1551                buf = xmalloc(alloc);
1552                if (read_old_data(st, patch->old_name, buf, alloc) != size)
1553                        return error("read of %s failed", patch->old_name);
1554        }
1555
1556        desc.size = size;
1557        desc.alloc = alloc;
1558        desc.buffer = buf;
1559        if (apply_fragments(&desc, patch) < 0)
1560                return -1;
1561        patch->result = desc.buffer;
1562        patch->resultsize = desc.size;
1563
1564        if (patch->is_delete && patch->resultsize)
1565                return error("removal patch leaves file contents");
1566
1567        return 0;
1568}
1569
1570static int check_patch(struct patch *patch)
1571{
1572        struct stat st;
1573        const char *old_name = patch->old_name;
1574        const char *new_name = patch->new_name;
1575        const char *name = old_name ? old_name : new_name;
1576
1577        if (old_name) {
1578                int changed;
1579                int stat_ret = lstat(old_name, &st);
1580
1581                if (check_index) {
1582                        int pos = cache_name_pos(old_name, strlen(old_name));
1583                        if (pos < 0)
1584                                return error("%s: does not exist in index",
1585                                             old_name);
1586                        if (stat_ret < 0) {
1587                                struct checkout costate;
1588                                if (errno != ENOENT)
1589                                        return error("%s: %s", old_name,
1590                                                     strerror(errno));
1591                                /* checkout */
1592                                costate.base_dir = "";
1593                                costate.base_dir_len = 0;
1594                                costate.force = 0;
1595                                costate.quiet = 0;
1596                                costate.not_new = 0;
1597                                costate.refresh_cache = 1;
1598                                if (checkout_entry(active_cache[pos],
1599                                                   &costate,
1600                                                   NULL) ||
1601                                    lstat(old_name, &st))
1602                                        return -1;
1603                        }
1604
1605                        changed = ce_match_stat(active_cache[pos], &st, 1);
1606                        if (changed)
1607                                return error("%s: does not match index",
1608                                             old_name);
1609                }
1610                else if (stat_ret < 0)
1611                        return error("%s: %s", old_name, strerror(errno));
1612
1613                if (patch->is_new < 0)
1614                        patch->is_new = 0;
1615                st.st_mode = ntohl(create_ce_mode(st.st_mode));
1616                if (!patch->old_mode)
1617                        patch->old_mode = st.st_mode;
1618                if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1619                        return error("%s: wrong type", old_name);
1620                if (st.st_mode != patch->old_mode)
1621                        fprintf(stderr, "warning: %s has type %o, expected %o\n",
1622                                old_name, st.st_mode, patch->old_mode);
1623        }
1624
1625        if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1626                if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1627                        return error("%s: already exists in index", new_name);
1628                if (!lstat(new_name, &st))
1629                        return error("%s: already exists in working directory", new_name);
1630                if (errno != ENOENT)
1631                        return error("%s: %s", new_name, strerror(errno));
1632                if (!patch->new_mode) {
1633                        if (patch->is_new)
1634                                patch->new_mode = S_IFREG | 0644;
1635                        else
1636                                patch->new_mode = patch->old_mode;
1637                }
1638        }
1639
1640        if (new_name && old_name) {
1641                int same = !strcmp(old_name, new_name);
1642                if (!patch->new_mode)
1643                        patch->new_mode = patch->old_mode;
1644                if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1645                        return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1646                                patch->new_mode, new_name, patch->old_mode,
1647                                same ? "" : " of ", same ? "" : old_name);
1648        }       
1649
1650        if (apply_data(patch, &st) < 0)
1651                return error("%s: patch does not apply", name);
1652        return 0;
1653}
1654
1655static int check_patch_list(struct patch *patch)
1656{
1657        int error = 0;
1658
1659        for (;patch ; patch = patch->next)
1660                error |= check_patch(patch);
1661        return error;
1662}
1663
1664static inline int is_null_sha1(const unsigned char *sha1)
1665{
1666        return !memcmp(sha1, null_sha1, 20);
1667}
1668
1669static void show_index_list(struct patch *list)
1670{
1671        struct patch *patch;
1672
1673        /* Once we start supporting the reverse patch, it may be
1674         * worth showing the new sha1 prefix, but until then...
1675         */
1676        for (patch = list; patch; patch = patch->next) {
1677                const unsigned char *sha1_ptr;
1678                unsigned char sha1[20];
1679                const char *name;
1680
1681                name = patch->old_name ? patch->old_name : patch->new_name;
1682                if (patch->is_new)
1683                        sha1_ptr = null_sha1;
1684                else if (get_sha1(patch->old_sha1_prefix, sha1))
1685                        die("sha1 information is lacking or useless (%s).",
1686                            name);
1687                else
1688                        sha1_ptr = sha1;
1689
1690                printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
1691                if (line_termination && quote_c_style(name, NULL, NULL, 0))
1692                        quote_c_style(name, NULL, stdout, 0);
1693                else
1694                        fputs(name, stdout);
1695                putchar(line_termination);
1696        }
1697}
1698
1699static void stat_patch_list(struct patch *patch)
1700{
1701        int files, adds, dels;
1702
1703        for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1704                files++;
1705                adds += patch->lines_added;
1706                dels += patch->lines_deleted;
1707                show_stats(patch);
1708        }
1709
1710        printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
1711}
1712
1713static void numstat_patch_list(struct patch *patch)
1714{
1715        for ( ; patch; patch = patch->next) {
1716                const char *name;
1717                name = patch->old_name ? patch->old_name : patch->new_name;
1718                printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
1719                if (line_termination && quote_c_style(name, NULL, NULL, 0))
1720                        quote_c_style(name, NULL, stdout, 0);
1721                else
1722                        fputs(name, stdout);
1723                putchar('\n');
1724        }
1725}
1726
1727static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
1728{
1729        if (mode)
1730                printf(" %s mode %06o %s\n", newdelete, mode, name);
1731        else
1732                printf(" %s %s\n", newdelete, name);
1733}
1734
1735static void show_mode_change(struct patch *p, int show_name)
1736{
1737        if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
1738                if (show_name)
1739                        printf(" mode change %06o => %06o %s\n",
1740                               p->old_mode, p->new_mode, p->new_name);
1741                else
1742                        printf(" mode change %06o => %06o\n",
1743                               p->old_mode, p->new_mode);
1744        }
1745}
1746
1747static void show_rename_copy(struct patch *p)
1748{
1749        const char *renamecopy = p->is_rename ? "rename" : "copy";
1750        const char *old, *new;
1751
1752        /* Find common prefix */
1753        old = p->old_name;
1754        new = p->new_name;
1755        while (1) {
1756                const char *slash_old, *slash_new;
1757                slash_old = strchr(old, '/');
1758                slash_new = strchr(new, '/');
1759                if (!slash_old ||
1760                    !slash_new ||
1761                    slash_old - old != slash_new - new ||
1762                    memcmp(old, new, slash_new - new))
1763                        break;
1764                old = slash_old + 1;
1765                new = slash_new + 1;
1766        }
1767        /* p->old_name thru old is the common prefix, and old and new
1768         * through the end of names are renames
1769         */
1770        if (old != p->old_name)
1771                printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1772                       (int)(old - p->old_name), p->old_name,
1773                       old, new, p->score);
1774        else
1775                printf(" %s %s => %s (%d%%)\n", renamecopy,
1776                       p->old_name, p->new_name, p->score);
1777        show_mode_change(p, 0);
1778}
1779
1780static void summary_patch_list(struct patch *patch)
1781{
1782        struct patch *p;
1783
1784        for (p = patch; p; p = p->next) {
1785                if (p->is_new)
1786                        show_file_mode_name("create", p->new_mode, p->new_name);
1787                else if (p->is_delete)
1788                        show_file_mode_name("delete", p->old_mode, p->old_name);
1789                else {
1790                        if (p->is_rename || p->is_copy)
1791                                show_rename_copy(p);
1792                        else {
1793                                if (p->score) {
1794                                        printf(" rewrite %s (%d%%)\n",
1795                                               p->new_name, p->score);
1796                                        show_mode_change(p, 0);
1797                                }
1798                                else
1799                                        show_mode_change(p, 1);
1800                        }
1801                }
1802        }
1803}
1804
1805static void patch_stats(struct patch *patch)
1806{
1807        int lines = patch->lines_added + patch->lines_deleted;
1808
1809        if (lines > max_change)
1810                max_change = lines;
1811        if (patch->old_name) {
1812                int len = quote_c_style(patch->old_name, NULL, NULL, 0);
1813                if (!len)
1814                        len = strlen(patch->old_name);
1815                if (len > max_len)
1816                        max_len = len;
1817        }
1818        if (patch->new_name) {
1819                int len = quote_c_style(patch->new_name, NULL, NULL, 0);
1820                if (!len)
1821                        len = strlen(patch->new_name);
1822                if (len > max_len)
1823                        max_len = len;
1824        }
1825}
1826
1827static void remove_file(struct patch *patch)
1828{
1829        if (write_index) {
1830                if (remove_file_from_cache(patch->old_name) < 0)
1831                        die("unable to remove %s from index", patch->old_name);
1832        }
1833        unlink(patch->old_name);
1834}
1835
1836static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
1837{
1838        struct stat st;
1839        struct cache_entry *ce;
1840        int namelen = strlen(path);
1841        unsigned ce_size = cache_entry_size(namelen);
1842
1843        if (!write_index)
1844                return;
1845
1846        ce = xcalloc(1, ce_size);
1847        memcpy(ce->name, path, namelen);
1848        ce->ce_mode = create_ce_mode(mode);
1849        ce->ce_flags = htons(namelen);
1850        if (lstat(path, &st) < 0)
1851                die("unable to stat newly created file %s", path);
1852        fill_stat_cache_info(ce, &st);
1853        if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
1854                die("unable to create backing store for newly created file %s", path);
1855        if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
1856                die("unable to add cache entry for %s", path);
1857}
1858
1859static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
1860{
1861        int fd;
1862
1863        if (S_ISLNK(mode))
1864                return symlink(buf, path);
1865        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
1866        if (fd < 0)
1867                return -1;
1868        while (size) {
1869                int written = xwrite(fd, buf, size);
1870                if (written < 0)
1871                        die("writing file %s: %s", path, strerror(errno));
1872                if (!written)
1873                        die("out of space writing file %s", path);
1874                buf += written;
1875                size -= written;
1876        }
1877        if (close(fd) < 0)
1878                die("closing file %s: %s", path, strerror(errno));
1879        return 0;
1880}
1881
1882/*
1883 * We optimistically assume that the directories exist,
1884 * which is true 99% of the time anyway. If they don't,
1885 * we create them and try again.
1886 */
1887static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
1888{
1889        if (!try_create_file(path, mode, buf, size))
1890                return;
1891
1892        if (errno == ENOENT) {
1893                if (safe_create_leading_directories(path))
1894                        return;
1895                if (!try_create_file(path, mode, buf, size))
1896                        return;
1897        }
1898
1899        if (errno == EEXIST) {
1900                unsigned int nr = getpid();
1901
1902                for (;;) {
1903                        const char *newpath;
1904                        newpath = mkpath("%s~%u", path, nr);
1905                        if (!try_create_file(newpath, mode, buf, size)) {
1906                                if (!rename(newpath, path))
1907                                        return;
1908                                unlink(newpath);
1909                                break;
1910                        }
1911                        if (errno != EEXIST)
1912                                break;
1913                        ++nr;
1914                }
1915        }
1916        die("unable to write file %s mode %o", path, mode);
1917}
1918
1919static void create_file(struct patch *patch)
1920{
1921        char *path = patch->new_name;
1922        unsigned mode = patch->new_mode;
1923        unsigned long size = patch->resultsize;
1924        char *buf = patch->result;
1925
1926        if (!mode)
1927                mode = S_IFREG | 0644;
1928        create_one_file(path, mode, buf, size); 
1929        add_index_file(path, mode, buf, size);
1930}
1931
1932static void write_out_one_result(struct patch *patch)
1933{
1934        if (patch->is_delete > 0) {
1935                remove_file(patch);
1936                return;
1937        }
1938        if (patch->is_new > 0 || patch->is_copy) {
1939                create_file(patch);
1940                return;
1941        }
1942        /*
1943         * Rename or modification boils down to the same
1944         * thing: remove the old, write the new
1945         */
1946        remove_file(patch);
1947        create_file(patch);
1948}
1949
1950static void write_out_results(struct patch *list, int skipped_patch)
1951{
1952        if (!list && !skipped_patch)
1953                die("No changes");
1954
1955        while (list) {
1956                write_out_one_result(list);
1957                list = list->next;
1958        }
1959}
1960
1961static struct cache_file cache_file;
1962
1963static struct excludes {
1964        struct excludes *next;
1965        const char *path;
1966} *excludes;
1967
1968static int use_patch(struct patch *p)
1969{
1970        const char *pathname = p->new_name ? p->new_name : p->old_name;
1971        struct excludes *x = excludes;
1972        while (x) {
1973                if (fnmatch(x->path, pathname, 0) == 0)
1974                        return 0;
1975                x = x->next;
1976        }
1977        if (0 < prefix_length) {
1978                int pathlen = strlen(pathname);
1979                if (pathlen <= prefix_length ||
1980                    memcmp(prefix, pathname, prefix_length))
1981                        return 0;
1982        }
1983        return 1;
1984}
1985
1986static int apply_patch(int fd, const char *filename)
1987{
1988        int newfd;
1989        unsigned long offset, size;
1990        char *buffer = read_patch_file(fd, &size);
1991        struct patch *list = NULL, **listp = &list;
1992        int skipped_patch = 0;
1993
1994        patch_input_file = filename;
1995        if (!buffer)
1996                return -1;
1997        offset = 0;
1998        while (size > 0) {
1999                struct patch *patch;
2000                int nr;
2001
2002                patch = xcalloc(1, sizeof(*patch));
2003                nr = parse_chunk(buffer + offset, size, patch);
2004                if (nr < 0)
2005                        break;
2006                if (use_patch(patch)) {
2007                        patch_stats(patch);
2008                        *listp = patch;
2009                        listp = &patch->next;
2010                } else {
2011                        /* perhaps free it a bit better? */
2012                        free(patch);
2013                        skipped_patch++;
2014                }
2015                offset += nr;
2016                size -= nr;
2017        }
2018
2019        newfd = -1;
2020        if (whitespace_error && (new_whitespace == error_on_whitespace))
2021                apply = 0;
2022
2023        write_index = check_index && apply;
2024        if (write_index)
2025                newfd = hold_index_file_for_update(&cache_file, get_index_file());
2026        if (check_index) {
2027                if (read_cache() < 0)
2028                        die("unable to read index file");
2029        }
2030
2031        if ((check || apply) && check_patch_list(list) < 0)
2032                exit(1);
2033
2034        if (apply)
2035                write_out_results(list, skipped_patch);
2036
2037        if (write_index) {
2038                if (write_cache(newfd, active_cache, active_nr) ||
2039                    commit_index_file(&cache_file))
2040                        die("Unable to write new cachefile");
2041        }
2042
2043        if (show_index_info)
2044                show_index_list(list);
2045
2046        if (diffstat)
2047                stat_patch_list(list);
2048
2049        if (numstat)
2050                numstat_patch_list(list);
2051
2052        if (summary)
2053                summary_patch_list(list);
2054
2055        free(buffer);
2056        return 0;
2057}
2058
2059static int git_apply_config(const char *var, const char *value)
2060{
2061        if (!strcmp(var, "apply.whitespace")) {
2062                apply_default_whitespace = strdup(value);
2063                return 0;
2064        }
2065        return git_default_config(var, value);
2066}
2067
2068
2069int main(int argc, char **argv)
2070{
2071        int i;
2072        int read_stdin = 1;
2073        const char *whitespace_option = NULL;
2074
2075        for (i = 1; i < argc; i++) {
2076                const char *arg = argv[i];
2077                char *end;
2078                int fd;
2079
2080                if (!strcmp(arg, "-")) {
2081                        apply_patch(0, "<stdin>");
2082                        read_stdin = 0;
2083                        continue;
2084                }
2085                if (!strncmp(arg, "--exclude=", 10)) {
2086                        struct excludes *x = xmalloc(sizeof(*x));
2087                        x->path = arg + 10;
2088                        x->next = excludes;
2089                        excludes = x;
2090                        continue;
2091                }
2092                if (!strncmp(arg, "-p", 2)) {
2093                        p_value = atoi(arg + 2);
2094                        continue;
2095                }
2096                if (!strcmp(arg, "--no-add")) {
2097                        no_add = 1;
2098                        continue;
2099                }
2100                if (!strcmp(arg, "--stat")) {
2101                        apply = 0;
2102                        diffstat = 1;
2103                        continue;
2104                }
2105                if (!strcmp(arg, "--allow-binary-replacement")) {
2106                        allow_binary_replacement = 1;
2107                        continue;
2108                }
2109                if (!strcmp(arg, "--numstat")) {
2110                        apply = 0;
2111                        numstat = 1;
2112                        continue;
2113                }
2114                if (!strcmp(arg, "--summary")) {
2115                        apply = 0;
2116                        summary = 1;
2117                        continue;
2118                }
2119                if (!strcmp(arg, "--check")) {
2120                        apply = 0;
2121                        check = 1;
2122                        continue;
2123                }
2124                if (!strcmp(arg, "--index")) {
2125                        check_index = 1;
2126                        continue;
2127                }
2128                if (!strcmp(arg, "--apply")) {
2129                        apply = 1;
2130                        continue;
2131                }
2132                if (!strcmp(arg, "--index-info")) {
2133                        apply = 0;
2134                        show_index_info = 1;
2135                        continue;
2136                }
2137                if (!strcmp(arg, "-z")) {
2138                        line_termination = 0;
2139                        continue;
2140                }
2141                if (!strncmp(arg, "-C", 2)) {
2142                        p_context = strtoul(arg + 2, &end, 0);
2143                        if (*end != '\0')
2144                                die("unrecognized context count '%s'", arg + 2);
2145                        continue;
2146                }
2147                if (!strncmp(arg, "--whitespace=", 13)) {
2148                        whitespace_option = arg + 13;
2149                        parse_whitespace_option(arg + 13);
2150                        continue;
2151                }
2152
2153                if (check_index && prefix_length < 0) {
2154                        prefix = setup_git_directory();
2155                        prefix_length = prefix ? strlen(prefix) : 0;
2156                        git_config(git_apply_config);
2157                        if (!whitespace_option && apply_default_whitespace)
2158                                parse_whitespace_option(apply_default_whitespace);
2159                }
2160                if (0 < prefix_length)
2161                        arg = prefix_filename(prefix, prefix_length, arg);
2162
2163                fd = open(arg, O_RDONLY);
2164                if (fd < 0)
2165                        usage(apply_usage);
2166                read_stdin = 0;
2167                set_default_whitespace_mode(whitespace_option);
2168                apply_patch(fd, arg);
2169                close(fd);
2170        }
2171        set_default_whitespace_mode(whitespace_option);
2172        if (read_stdin)
2173                apply_patch(0, "<stdin>");
2174        if (whitespace_error) {
2175                if (squelch_whitespace_errors &&
2176                    squelch_whitespace_errors < whitespace_error) {
2177                        int squelched =
2178                                whitespace_error - squelch_whitespace_errors;
2179                        fprintf(stderr, "warning: squelched %d whitespace error%s\n",
2180                                squelched,
2181                                squelched == 1 ? "" : "s");
2182                }
2183                if (new_whitespace == error_on_whitespace)
2184                        die("%d line%s add%s trailing whitespaces.",
2185                            whitespace_error,
2186                            whitespace_error == 1 ? "" : "s",
2187                            whitespace_error == 1 ? "s" : "");
2188                if (applied_after_stripping)
2189                        fprintf(stderr, "warning: %d line%s applied after"
2190                                " stripping trailing whitespaces.\n",
2191                                applied_after_stripping,
2192                                applied_after_stripping == 1 ? "" : "s");
2193                else if (whitespace_error)
2194                        fprintf(stderr, "warning: %d line%s add%s trailing"
2195                                " whitespaces.\n",
2196                                whitespace_error,
2197                                whitespace_error == 1 ? "" : "s",
2198                                whitespace_error == 1 ? "s" : "");
2199        }
2200        return 0;
2201}