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