builtin-apply.con commit Merge branch 'jc/maint-1.6.0-blank-at-eof' into jc/maint-blank-at-eof (d91ba8f)
   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 "cache.h"
  10#include "cache-tree.h"
  11#include "quote.h"
  12#include "blob.h"
  13#include "delta.h"
  14#include "builtin.h"
  15#include "string-list.h"
  16#include "dir.h"
  17#include "parse-options.h"
  18
  19/*
  20 *  --check turns on checking that the working tree matches the
  21 *    files that are being modified, but doesn't apply the patch
  22 *  --stat does just a diffstat, and doesn't actually apply
  23 *  --numstat does numeric diffstat, and doesn't actually apply
  24 *  --index-info shows the old and new index info for paths if available.
  25 *  --index updates the cache as well.
  26 *  --cached updates only the cache without ever touching the working tree.
  27 */
  28static const char *prefix;
  29static int prefix_length = -1;
  30static int newfd = -1;
  31
  32static int unidiff_zero;
  33static int p_value = 1;
  34static int p_value_known;
  35static int check_index;
  36static int update_index;
  37static int cached;
  38static int diffstat;
  39static int numstat;
  40static int summary;
  41static int check;
  42static int apply = 1;
  43static int apply_in_reverse;
  44static int apply_with_reject;
  45static int apply_verbosely;
  46static int no_add;
  47static const char *fake_ancestor;
  48static int line_termination = '\n';
  49static unsigned int p_context = UINT_MAX;
  50static const char * const apply_usage[] = {
  51        "git apply [options] [<patch>...]",
  52        NULL
  53};
  54
  55static enum ws_error_action {
  56        nowarn_ws_error,
  57        warn_on_ws_error,
  58        die_on_ws_error,
  59        correct_ws_error,
  60} ws_error_action = warn_on_ws_error;
  61static int whitespace_error;
  62static int squelch_whitespace_errors = 5;
  63static int applied_after_fixing_ws;
  64static const char *patch_input_file;
  65static const char *root;
  66static int root_len;
  67static int read_stdin = 1;
  68static int options;
  69
  70static void parse_whitespace_option(const char *option)
  71{
  72        if (!option) {
  73                ws_error_action = warn_on_ws_error;
  74                return;
  75        }
  76        if (!strcmp(option, "warn")) {
  77                ws_error_action = warn_on_ws_error;
  78                return;
  79        }
  80        if (!strcmp(option, "nowarn")) {
  81                ws_error_action = nowarn_ws_error;
  82                return;
  83        }
  84        if (!strcmp(option, "error")) {
  85                ws_error_action = die_on_ws_error;
  86                return;
  87        }
  88        if (!strcmp(option, "error-all")) {
  89                ws_error_action = die_on_ws_error;
  90                squelch_whitespace_errors = 0;
  91                return;
  92        }
  93        if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
  94                ws_error_action = correct_ws_error;
  95                return;
  96        }
  97        die("unrecognized whitespace option '%s'", option);
  98}
  99
 100static void set_default_whitespace_mode(const char *whitespace_option)
 101{
 102        if (!whitespace_option && !apply_default_whitespace)
 103                ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
 104}
 105
 106/*
 107 * For "diff-stat" like behaviour, we keep track of the biggest change
 108 * we've seen, and the longest filename. That allows us to do simple
 109 * scaling.
 110 */
 111static int max_change, max_len;
 112
 113/*
 114 * Various "current state", notably line numbers and what
 115 * file (and how) we're patching right now.. The "is_xxxx"
 116 * things are flags, where -1 means "don't know yet".
 117 */
 118static int linenr = 1;
 119
 120/*
 121 * This represents one "hunk" from a patch, starting with
 122 * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
 123 * patch text is pointed at by patch, and its byte length
 124 * is stored in size.  leading and trailing are the number
 125 * of context lines.
 126 */
 127struct fragment {
 128        unsigned long leading, trailing;
 129        unsigned long oldpos, oldlines;
 130        unsigned long newpos, newlines;
 131        const char *patch;
 132        int size;
 133        int rejected;
 134        int linenr;
 135        struct fragment *next;
 136};
 137
 138/*
 139 * When dealing with a binary patch, we reuse "leading" field
 140 * to store the type of the binary hunk, either deflated "delta"
 141 * or deflated "literal".
 142 */
 143#define binary_patch_method leading
 144#define BINARY_DELTA_DEFLATED   1
 145#define BINARY_LITERAL_DEFLATED 2
 146
 147/*
 148 * This represents a "patch" to a file, both metainfo changes
 149 * such as creation/deletion, filemode and content changes represented
 150 * as a series of fragments.
 151 */
 152struct patch {
 153        char *new_name, *old_name, *def_name;
 154        unsigned int old_mode, new_mode;
 155        int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
 156        int rejected;
 157        unsigned ws_rule;
 158        unsigned long deflate_origlen;
 159        int lines_added, lines_deleted;
 160        int score;
 161        unsigned int is_toplevel_relative:1;
 162        unsigned int inaccurate_eof:1;
 163        unsigned int is_binary:1;
 164        unsigned int is_copy:1;
 165        unsigned int is_rename:1;
 166        unsigned int recount:1;
 167        struct fragment *fragments;
 168        char *result;
 169        size_t resultsize;
 170        char old_sha1_prefix[41];
 171        char new_sha1_prefix[41];
 172        struct patch *next;
 173};
 174
 175/*
 176 * A line in a file, len-bytes long (includes the terminating LF,
 177 * except for an incomplete line at the end if the file ends with
 178 * one), and its contents hashes to 'hash'.
 179 */
 180struct line {
 181        size_t len;
 182        unsigned hash : 24;
 183        unsigned flag : 8;
 184#define LINE_COMMON     1
 185};
 186
 187/*
 188 * This represents a "file", which is an array of "lines".
 189 */
 190struct image {
 191        char *buf;
 192        size_t len;
 193        size_t nr;
 194        size_t alloc;
 195        struct line *line_allocated;
 196        struct line *line;
 197};
 198
 199/*
 200 * Records filenames that have been touched, in order to handle
 201 * the case where more than one patches touch the same file.
 202 */
 203
 204static struct string_list fn_table;
 205
 206static uint32_t hash_line(const char *cp, size_t len)
 207{
 208        size_t i;
 209        uint32_t h;
 210        for (i = 0, h = 0; i < len; i++) {
 211                if (!isspace(cp[i])) {
 212                        h = h * 3 + (cp[i] & 0xff);
 213                }
 214        }
 215        return h;
 216}
 217
 218static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
 219{
 220        ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
 221        img->line_allocated[img->nr].len = len;
 222        img->line_allocated[img->nr].hash = hash_line(bol, len);
 223        img->line_allocated[img->nr].flag = flag;
 224        img->nr++;
 225}
 226
 227static void prepare_image(struct image *image, char *buf, size_t len,
 228                          int prepare_linetable)
 229{
 230        const char *cp, *ep;
 231
 232        memset(image, 0, sizeof(*image));
 233        image->buf = buf;
 234        image->len = len;
 235
 236        if (!prepare_linetable)
 237                return;
 238
 239        ep = image->buf + image->len;
 240        cp = image->buf;
 241        while (cp < ep) {
 242                const char *next;
 243                for (next = cp; next < ep && *next != '\n'; next++)
 244                        ;
 245                if (next < ep)
 246                        next++;
 247                add_line_info(image, cp, next - cp, 0);
 248                cp = next;
 249        }
 250        image->line = image->line_allocated;
 251}
 252
 253static void clear_image(struct image *image)
 254{
 255        free(image->buf);
 256        image->buf = NULL;
 257        image->len = 0;
 258}
 259
 260static void say_patch_name(FILE *output, const char *pre,
 261                           struct patch *patch, const char *post)
 262{
 263        fputs(pre, output);
 264        if (patch->old_name && patch->new_name &&
 265            strcmp(patch->old_name, patch->new_name)) {
 266                quote_c_style(patch->old_name, NULL, output, 0);
 267                fputs(" => ", output);
 268                quote_c_style(patch->new_name, NULL, output, 0);
 269        } else {
 270                const char *n = patch->new_name;
 271                if (!n)
 272                        n = patch->old_name;
 273                quote_c_style(n, NULL, output, 0);
 274        }
 275        fputs(post, output);
 276}
 277
 278#define CHUNKSIZE (8192)
 279#define SLOP (16)
 280
 281static void read_patch_file(struct strbuf *sb, int fd)
 282{
 283        if (strbuf_read(sb, fd, 0) < 0)
 284                die_errno("git apply: failed to read");
 285
 286        /*
 287         * Make sure that we have some slop in the buffer
 288         * so that we can do speculative "memcmp" etc, and
 289         * see to it that it is NUL-filled.
 290         */
 291        strbuf_grow(sb, SLOP);
 292        memset(sb->buf + sb->len, 0, SLOP);
 293}
 294
 295static unsigned long linelen(const char *buffer, unsigned long size)
 296{
 297        unsigned long len = 0;
 298        while (size--) {
 299                len++;
 300                if (*buffer++ == '\n')
 301                        break;
 302        }
 303        return len;
 304}
 305
 306static int is_dev_null(const char *str)
 307{
 308        return !memcmp("/dev/null", str, 9) && isspace(str[9]);
 309}
 310
 311#define TERM_SPACE      1
 312#define TERM_TAB        2
 313
 314static int name_terminate(const char *name, int namelen, int c, int terminate)
 315{
 316        if (c == ' ' && !(terminate & TERM_SPACE))
 317                return 0;
 318        if (c == '\t' && !(terminate & TERM_TAB))
 319                return 0;
 320
 321        return 1;
 322}
 323
 324/* remove double slashes to make --index work with such filenames */
 325static char *squash_slash(char *name)
 326{
 327        int i = 0, j = 0;
 328
 329        while (name[i]) {
 330                if ((name[j++] = name[i++]) == '/')
 331                        while (name[i] == '/')
 332                                i++;
 333        }
 334        name[j] = '\0';
 335        return name;
 336}
 337
 338static char *find_name(const char *line, char *def, int p_value, int terminate)
 339{
 340        int len;
 341        const char *start = line;
 342
 343        if (*line == '"') {
 344                struct strbuf name = STRBUF_INIT;
 345
 346                /*
 347                 * Proposed "new-style" GNU patch/diff format; see
 348                 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
 349                 */
 350                if (!unquote_c_style(&name, line, NULL)) {
 351                        char *cp;
 352
 353                        for (cp = name.buf; p_value; p_value--) {
 354                                cp = strchr(cp, '/');
 355                                if (!cp)
 356                                        break;
 357                                cp++;
 358                        }
 359                        if (cp) {
 360                                /* name can later be freed, so we need
 361                                 * to memmove, not just return cp
 362                                 */
 363                                strbuf_remove(&name, 0, cp - name.buf);
 364                                free(def);
 365                                if (root)
 366                                        strbuf_insert(&name, 0, root, root_len);
 367                                return squash_slash(strbuf_detach(&name, NULL));
 368                        }
 369                }
 370                strbuf_release(&name);
 371        }
 372
 373        for (;;) {
 374                char c = *line;
 375
 376                if (isspace(c)) {
 377                        if (c == '\n')
 378                                break;
 379                        if (name_terminate(start, line-start, c, terminate))
 380                                break;
 381                }
 382                line++;
 383                if (c == '/' && !--p_value)
 384                        start = line;
 385        }
 386        if (!start)
 387                return squash_slash(def);
 388        len = line - start;
 389        if (!len)
 390                return squash_slash(def);
 391
 392        /*
 393         * Generally we prefer the shorter name, especially
 394         * if the other one is just a variation of that with
 395         * something else tacked on to the end (ie "file.orig"
 396         * or "file~").
 397         */
 398        if (def) {
 399                int deflen = strlen(def);
 400                if (deflen < len && !strncmp(start, def, deflen))
 401                        return squash_slash(def);
 402                free(def);
 403        }
 404
 405        if (root) {
 406                char *ret = xmalloc(root_len + len + 1);
 407                strcpy(ret, root);
 408                memcpy(ret + root_len, start, len);
 409                ret[root_len + len] = '\0';
 410                return squash_slash(ret);
 411        }
 412
 413        return squash_slash(xmemdupz(start, len));
 414}
 415
 416static int count_slashes(const char *cp)
 417{
 418        int cnt = 0;
 419        char ch;
 420
 421        while ((ch = *cp++))
 422                if (ch == '/')
 423                        cnt++;
 424        return cnt;
 425}
 426
 427/*
 428 * Given the string after "--- " or "+++ ", guess the appropriate
 429 * p_value for the given patch.
 430 */
 431static int guess_p_value(const char *nameline)
 432{
 433        char *name, *cp;
 434        int val = -1;
 435
 436        if (is_dev_null(nameline))
 437                return -1;
 438        name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB);
 439        if (!name)
 440                return -1;
 441        cp = strchr(name, '/');
 442        if (!cp)
 443                val = 0;
 444        else if (prefix) {
 445                /*
 446                 * Does it begin with "a/$our-prefix" and such?  Then this is
 447                 * very likely to apply to our directory.
 448                 */
 449                if (!strncmp(name, prefix, prefix_length))
 450                        val = count_slashes(prefix);
 451                else {
 452                        cp++;
 453                        if (!strncmp(cp, prefix, prefix_length))
 454                                val = count_slashes(prefix) + 1;
 455                }
 456        }
 457        free(name);
 458        return val;
 459}
 460
 461/*
 462 * Does the ---/+++ line has the POSIX timestamp after the last HT?
 463 * GNU diff puts epoch there to signal a creation/deletion event.  Is
 464 * this such a timestamp?
 465 */
 466static int has_epoch_timestamp(const char *nameline)
 467{
 468        /*
 469         * We are only interested in epoch timestamp; any non-zero
 470         * fraction cannot be one, hence "(\.0+)?" in the regexp below.
 471         * For the same reason, the date must be either 1969-12-31 or
 472         * 1970-01-01, and the seconds part must be "00".
 473         */
 474        const char stamp_regexp[] =
 475                "^(1969-12-31|1970-01-01)"
 476                " "
 477                "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
 478                " "
 479                "([-+][0-2][0-9][0-5][0-9])\n";
 480        const char *timestamp = NULL, *cp;
 481        static regex_t *stamp;
 482        regmatch_t m[10];
 483        int zoneoffset;
 484        int hourminute;
 485        int status;
 486
 487        for (cp = nameline; *cp != '\n'; cp++) {
 488                if (*cp == '\t')
 489                        timestamp = cp + 1;
 490        }
 491        if (!timestamp)
 492                return 0;
 493        if (!stamp) {
 494                stamp = xmalloc(sizeof(*stamp));
 495                if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
 496                        warning("Cannot prepare timestamp regexp %s",
 497                                stamp_regexp);
 498                        return 0;
 499                }
 500        }
 501
 502        status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
 503        if (status) {
 504                if (status != REG_NOMATCH)
 505                        warning("regexec returned %d for input: %s",
 506                                status, timestamp);
 507                return 0;
 508        }
 509
 510        zoneoffset = strtol(timestamp + m[3].rm_so + 1, NULL, 10);
 511        zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
 512        if (timestamp[m[3].rm_so] == '-')
 513                zoneoffset = -zoneoffset;
 514
 515        /*
 516         * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
 517         * (west of GMT) or 1970-01-01 (east of GMT)
 518         */
 519        if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
 520            (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
 521                return 0;
 522
 523        hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
 524                      strtol(timestamp + 14, NULL, 10) -
 525                      zoneoffset);
 526
 527        return ((zoneoffset < 0 && hourminute == 1440) ||
 528                (0 <= zoneoffset && !hourminute));
 529}
 530
 531/*
 532 * Get the name etc info from the ---/+++ lines of a traditional patch header
 533 *
 534 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
 535 * files, we can happily check the index for a match, but for creating a
 536 * new file we should try to match whatever "patch" does. I have no idea.
 537 */
 538static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
 539{
 540        char *name;
 541
 542        first += 4;     /* skip "--- " */
 543        second += 4;    /* skip "+++ " */
 544        if (!p_value_known) {
 545                int p, q;
 546                p = guess_p_value(first);
 547                q = guess_p_value(second);
 548                if (p < 0) p = q;
 549                if (0 <= p && p == q) {
 550                        p_value = p;
 551                        p_value_known = 1;
 552                }
 553        }
 554        if (is_dev_null(first)) {
 555                patch->is_new = 1;
 556                patch->is_delete = 0;
 557                name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
 558                patch->new_name = name;
 559        } else if (is_dev_null(second)) {
 560                patch->is_new = 0;
 561                patch->is_delete = 1;
 562                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 563                patch->old_name = name;
 564        } else {
 565                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 566                name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
 567                if (has_epoch_timestamp(first)) {
 568                        patch->is_new = 1;
 569                        patch->is_delete = 0;
 570                        patch->new_name = name;
 571                } else if (has_epoch_timestamp(second)) {
 572                        patch->is_new = 0;
 573                        patch->is_delete = 1;
 574                        patch->old_name = name;
 575                } else {
 576                        patch->old_name = patch->new_name = name;
 577                }
 578        }
 579        if (!name)
 580                die("unable to find filename in patch at line %d", linenr);
 581}
 582
 583static int gitdiff_hdrend(const char *line, struct patch *patch)
 584{
 585        return -1;
 586}
 587
 588/*
 589 * We're anal about diff header consistency, to make
 590 * sure that we don't end up having strange ambiguous
 591 * patches floating around.
 592 *
 593 * As a result, gitdiff_{old|new}name() will check
 594 * their names against any previous information, just
 595 * to make sure..
 596 */
 597static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 598{
 599        if (!orig_name && !isnull)
 600                return find_name(line, NULL, p_value, TERM_TAB);
 601
 602        if (orig_name) {
 603                int len;
 604                const char *name;
 605                char *another;
 606                name = orig_name;
 607                len = strlen(name);
 608                if (isnull)
 609                        die("git apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
 610                another = find_name(line, NULL, p_value, TERM_TAB);
 611                if (!another || memcmp(another, name, len))
 612                        die("git apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
 613                free(another);
 614                return orig_name;
 615        }
 616        else {
 617                /* expect "/dev/null" */
 618                if (memcmp("/dev/null", line, 9) || line[9] != '\n')
 619                        die("git apply: bad git-diff - expected /dev/null on line %d", linenr);
 620                return NULL;
 621        }
 622}
 623
 624static int gitdiff_oldname(const char *line, struct patch *patch)
 625{
 626        patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
 627        return 0;
 628}
 629
 630static int gitdiff_newname(const char *line, struct patch *patch)
 631{
 632        patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
 633        return 0;
 634}
 635
 636static int gitdiff_oldmode(const char *line, struct patch *patch)
 637{
 638        patch->old_mode = strtoul(line, NULL, 8);
 639        return 0;
 640}
 641
 642static int gitdiff_newmode(const char *line, struct patch *patch)
 643{
 644        patch->new_mode = strtoul(line, NULL, 8);
 645        return 0;
 646}
 647
 648static int gitdiff_delete(const char *line, struct patch *patch)
 649{
 650        patch->is_delete = 1;
 651        patch->old_name = patch->def_name;
 652        return gitdiff_oldmode(line, patch);
 653}
 654
 655static int gitdiff_newfile(const char *line, struct patch *patch)
 656{
 657        patch->is_new = 1;
 658        patch->new_name = patch->def_name;
 659        return gitdiff_newmode(line, patch);
 660}
 661
 662static int gitdiff_copysrc(const char *line, struct patch *patch)
 663{
 664        patch->is_copy = 1;
 665        patch->old_name = find_name(line, NULL, 0, 0);
 666        return 0;
 667}
 668
 669static int gitdiff_copydst(const char *line, struct patch *patch)
 670{
 671        patch->is_copy = 1;
 672        patch->new_name = find_name(line, NULL, 0, 0);
 673        return 0;
 674}
 675
 676static int gitdiff_renamesrc(const char *line, struct patch *patch)
 677{
 678        patch->is_rename = 1;
 679        patch->old_name = find_name(line, NULL, 0, 0);
 680        return 0;
 681}
 682
 683static int gitdiff_renamedst(const char *line, struct patch *patch)
 684{
 685        patch->is_rename = 1;
 686        patch->new_name = find_name(line, NULL, 0, 0);
 687        return 0;
 688}
 689
 690static int gitdiff_similarity(const char *line, struct patch *patch)
 691{
 692        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 693                patch->score = 0;
 694        return 0;
 695}
 696
 697static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 698{
 699        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 700                patch->score = 0;
 701        return 0;
 702}
 703
 704static int gitdiff_index(const char *line, struct patch *patch)
 705{
 706        /*
 707         * index line is N hexadecimal, "..", N hexadecimal,
 708         * and optional space with octal mode.
 709         */
 710        const char *ptr, *eol;
 711        int len;
 712
 713        ptr = strchr(line, '.');
 714        if (!ptr || ptr[1] != '.' || 40 < ptr - line)
 715                return 0;
 716        len = ptr - line;
 717        memcpy(patch->old_sha1_prefix, line, len);
 718        patch->old_sha1_prefix[len] = 0;
 719
 720        line = ptr + 2;
 721        ptr = strchr(line, ' ');
 722        eol = strchr(line, '\n');
 723
 724        if (!ptr || eol < ptr)
 725                ptr = eol;
 726        len = ptr - line;
 727
 728        if (40 < len)
 729                return 0;
 730        memcpy(patch->new_sha1_prefix, line, len);
 731        patch->new_sha1_prefix[len] = 0;
 732        if (*ptr == ' ')
 733                patch->old_mode = strtoul(ptr+1, NULL, 8);
 734        return 0;
 735}
 736
 737/*
 738 * This is normal for a diff that doesn't change anything: we'll fall through
 739 * into the next diff. Tell the parser to break out.
 740 */
 741static int gitdiff_unrecognized(const char *line, struct patch *patch)
 742{
 743        return -1;
 744}
 745
 746static const char *stop_at_slash(const char *line, int llen)
 747{
 748        int i;
 749
 750        for (i = 0; i < llen; i++) {
 751                int ch = line[i];
 752                if (ch == '/')
 753                        return line + i;
 754        }
 755        return NULL;
 756}
 757
 758/*
 759 * This is to extract the same name that appears on "diff --git"
 760 * line.  We do not find and return anything if it is a rename
 761 * patch, and it is OK because we will find the name elsewhere.
 762 * We need to reliably find name only when it is mode-change only,
 763 * creation or deletion of an empty file.  In any of these cases,
 764 * both sides are the same name under a/ and b/ respectively.
 765 */
 766static char *git_header_name(char *line, int llen)
 767{
 768        const char *name;
 769        const char *second = NULL;
 770        size_t len;
 771
 772        line += strlen("diff --git ");
 773        llen -= strlen("diff --git ");
 774
 775        if (*line == '"') {
 776                const char *cp;
 777                struct strbuf first = STRBUF_INIT;
 778                struct strbuf sp = STRBUF_INIT;
 779
 780                if (unquote_c_style(&first, line, &second))
 781                        goto free_and_fail1;
 782
 783                /* advance to the first slash */
 784                cp = stop_at_slash(first.buf, first.len);
 785                /* we do not accept absolute paths */
 786                if (!cp || cp == first.buf)
 787                        goto free_and_fail1;
 788                strbuf_remove(&first, 0, cp + 1 - first.buf);
 789
 790                /*
 791                 * second points at one past closing dq of name.
 792                 * find the second name.
 793                 */
 794                while ((second < line + llen) && isspace(*second))
 795                        second++;
 796
 797                if (line + llen <= second)
 798                        goto free_and_fail1;
 799                if (*second == '"') {
 800                        if (unquote_c_style(&sp, second, NULL))
 801                                goto free_and_fail1;
 802                        cp = stop_at_slash(sp.buf, sp.len);
 803                        if (!cp || cp == sp.buf)
 804                                goto free_and_fail1;
 805                        /* They must match, otherwise ignore */
 806                        if (strcmp(cp + 1, first.buf))
 807                                goto free_and_fail1;
 808                        strbuf_release(&sp);
 809                        return strbuf_detach(&first, NULL);
 810                }
 811
 812                /* unquoted second */
 813                cp = stop_at_slash(second, line + llen - second);
 814                if (!cp || cp == second)
 815                        goto free_and_fail1;
 816                cp++;
 817                if (line + llen - cp != first.len + 1 ||
 818                    memcmp(first.buf, cp, first.len))
 819                        goto free_and_fail1;
 820                return strbuf_detach(&first, NULL);
 821
 822        free_and_fail1:
 823                strbuf_release(&first);
 824                strbuf_release(&sp);
 825                return NULL;
 826        }
 827
 828        /* unquoted first name */
 829        name = stop_at_slash(line, llen);
 830        if (!name || name == line)
 831                return NULL;
 832        name++;
 833
 834        /*
 835         * since the first name is unquoted, a dq if exists must be
 836         * the beginning of the second name.
 837         */
 838        for (second = name; second < line + llen; second++) {
 839                if (*second == '"') {
 840                        struct strbuf sp = STRBUF_INIT;
 841                        const char *np;
 842
 843                        if (unquote_c_style(&sp, second, NULL))
 844                                goto free_and_fail2;
 845
 846                        np = stop_at_slash(sp.buf, sp.len);
 847                        if (!np || np == sp.buf)
 848                                goto free_and_fail2;
 849                        np++;
 850
 851                        len = sp.buf + sp.len - np;
 852                        if (len < second - name &&
 853                            !strncmp(np, name, len) &&
 854                            isspace(name[len])) {
 855                                /* Good */
 856                                strbuf_remove(&sp, 0, np - sp.buf);
 857                                return strbuf_detach(&sp, NULL);
 858                        }
 859
 860                free_and_fail2:
 861                        strbuf_release(&sp);
 862                        return NULL;
 863                }
 864        }
 865
 866        /*
 867         * Accept a name only if it shows up twice, exactly the same
 868         * form.
 869         */
 870        for (len = 0 ; ; len++) {
 871                switch (name[len]) {
 872                default:
 873                        continue;
 874                case '\n':
 875                        return NULL;
 876                case '\t': case ' ':
 877                        second = name+len;
 878                        for (;;) {
 879                                char c = *second++;
 880                                if (c == '\n')
 881                                        return NULL;
 882                                if (c == '/')
 883                                        break;
 884                        }
 885                        if (second[len] == '\n' && !memcmp(name, second, len)) {
 886                                return xmemdupz(name, len);
 887                        }
 888                }
 889        }
 890}
 891
 892/* Verify that we recognize the lines following a git header */
 893static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
 894{
 895        unsigned long offset;
 896
 897        /* A git diff has explicit new/delete information, so we don't guess */
 898        patch->is_new = 0;
 899        patch->is_delete = 0;
 900
 901        /*
 902         * Some things may not have the old name in the
 903         * rest of the headers anywhere (pure mode changes,
 904         * or removing or adding empty files), so we get
 905         * the default name from the header.
 906         */
 907        patch->def_name = git_header_name(line, len);
 908        if (patch->def_name && root) {
 909                char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
 910                strcpy(s, root);
 911                strcpy(s + root_len, patch->def_name);
 912                free(patch->def_name);
 913                patch->def_name = s;
 914        }
 915
 916        line += len;
 917        size -= len;
 918        linenr++;
 919        for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 920                static const struct opentry {
 921                        const char *str;
 922                        int (*fn)(const char *, struct patch *);
 923                } optable[] = {
 924                        { "@@ -", gitdiff_hdrend },
 925                        { "--- ", gitdiff_oldname },
 926                        { "+++ ", gitdiff_newname },
 927                        { "old mode ", gitdiff_oldmode },
 928                        { "new mode ", gitdiff_newmode },
 929                        { "deleted file mode ", gitdiff_delete },
 930                        { "new file mode ", gitdiff_newfile },
 931                        { "copy from ", gitdiff_copysrc },
 932                        { "copy to ", gitdiff_copydst },
 933                        { "rename old ", gitdiff_renamesrc },
 934                        { "rename new ", gitdiff_renamedst },
 935                        { "rename from ", gitdiff_renamesrc },
 936                        { "rename to ", gitdiff_renamedst },
 937                        { "similarity index ", gitdiff_similarity },
 938                        { "dissimilarity index ", gitdiff_dissimilarity },
 939                        { "index ", gitdiff_index },
 940                        { "", gitdiff_unrecognized },
 941                };
 942                int i;
 943
 944                len = linelen(line, size);
 945                if (!len || line[len-1] != '\n')
 946                        break;
 947                for (i = 0; i < ARRAY_SIZE(optable); i++) {
 948                        const struct opentry *p = optable + i;
 949                        int oplen = strlen(p->str);
 950                        if (len < oplen || memcmp(p->str, line, oplen))
 951                                continue;
 952                        if (p->fn(line + oplen, patch) < 0)
 953                                return offset;
 954                        break;
 955                }
 956        }
 957
 958        return offset;
 959}
 960
 961static int parse_num(const char *line, unsigned long *p)
 962{
 963        char *ptr;
 964
 965        if (!isdigit(*line))
 966                return 0;
 967        *p = strtoul(line, &ptr, 10);
 968        return ptr - line;
 969}
 970
 971static int parse_range(const char *line, int len, int offset, const char *expect,
 972                       unsigned long *p1, unsigned long *p2)
 973{
 974        int digits, ex;
 975
 976        if (offset < 0 || offset >= len)
 977                return -1;
 978        line += offset;
 979        len -= offset;
 980
 981        digits = parse_num(line, p1);
 982        if (!digits)
 983                return -1;
 984
 985        offset += digits;
 986        line += digits;
 987        len -= digits;
 988
 989        *p2 = 1;
 990        if (*line == ',') {
 991                digits = parse_num(line+1, p2);
 992                if (!digits)
 993                        return -1;
 994
 995                offset += digits+1;
 996                line += digits+1;
 997                len -= digits+1;
 998        }
 999
1000        ex = strlen(expect);
1001        if (ex > len)
1002                return -1;
1003        if (memcmp(line, expect, ex))
1004                return -1;
1005
1006        return offset + ex;
1007}
1008
1009static void recount_diff(char *line, int size, struct fragment *fragment)
1010{
1011        int oldlines = 0, newlines = 0, ret = 0;
1012
1013        if (size < 1) {
1014                warning("recount: ignore empty hunk");
1015                return;
1016        }
1017
1018        for (;;) {
1019                int len = linelen(line, size);
1020                size -= len;
1021                line += len;
1022
1023                if (size < 1)
1024                        break;
1025
1026                switch (*line) {
1027                case ' ': case '\n':
1028                        newlines++;
1029                        /* fall through */
1030                case '-':
1031                        oldlines++;
1032                        continue;
1033                case '+':
1034                        newlines++;
1035                        continue;
1036                case '\\':
1037                        continue;
1038                case '@':
1039                        ret = size < 3 || prefixcmp(line, "@@ ");
1040                        break;
1041                case 'd':
1042                        ret = size < 5 || prefixcmp(line, "diff ");
1043                        break;
1044                default:
1045                        ret = -1;
1046                        break;
1047                }
1048                if (ret) {
1049                        warning("recount: unexpected line: %.*s",
1050                                (int)linelen(line, size), line);
1051                        return;
1052                }
1053                break;
1054        }
1055        fragment->oldlines = oldlines;
1056        fragment->newlines = newlines;
1057}
1058
1059/*
1060 * Parse a unified diff fragment header of the
1061 * form "@@ -a,b +c,d @@"
1062 */
1063static int parse_fragment_header(char *line, int len, struct fragment *fragment)
1064{
1065        int offset;
1066
1067        if (!len || line[len-1] != '\n')
1068                return -1;
1069
1070        /* Figure out the number of lines in a fragment */
1071        offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1072        offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1073
1074        return offset;
1075}
1076
1077static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
1078{
1079        unsigned long offset, len;
1080
1081        patch->is_toplevel_relative = 0;
1082        patch->is_rename = patch->is_copy = 0;
1083        patch->is_new = patch->is_delete = -1;
1084        patch->old_mode = patch->new_mode = 0;
1085        patch->old_name = patch->new_name = NULL;
1086        for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
1087                unsigned long nextlen;
1088
1089                len = linelen(line, size);
1090                if (!len)
1091                        break;
1092
1093                /* Testing this early allows us to take a few shortcuts.. */
1094                if (len < 6)
1095                        continue;
1096
1097                /*
1098                 * Make sure we don't find any unconnected patch fragments.
1099                 * That's a sign that we didn't find a header, and that a
1100                 * patch has become corrupted/broken up.
1101                 */
1102                if (!memcmp("@@ -", line, 4)) {
1103                        struct fragment dummy;
1104                        if (parse_fragment_header(line, len, &dummy) < 0)
1105                                continue;
1106                        die("patch fragment without header at line %d: %.*s",
1107                            linenr, (int)len-1, line);
1108                }
1109
1110                if (size < len + 6)
1111                        break;
1112
1113                /*
1114                 * Git patch? It might not have a real patch, just a rename
1115                 * or mode change, so we handle that specially
1116                 */
1117                if (!memcmp("diff --git ", line, 11)) {
1118                        int git_hdr_len = parse_git_header(line, len, size, patch);
1119                        if (git_hdr_len <= len)
1120                                continue;
1121                        if (!patch->old_name && !patch->new_name) {
1122                                if (!patch->def_name)
1123                                        die("git diff header lacks filename information (line %d)", linenr);
1124                                patch->old_name = patch->new_name = patch->def_name;
1125                        }
1126                        patch->is_toplevel_relative = 1;
1127                        *hdrsize = git_hdr_len;
1128                        return offset;
1129                }
1130
1131                /* --- followed by +++ ? */
1132                if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
1133                        continue;
1134
1135                /*
1136                 * We only accept unified patches, so we want it to
1137                 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1138                 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1139                 */
1140                nextlen = linelen(line + len, size - len);
1141                if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1142                        continue;
1143
1144                /* Ok, we'll consider it a patch */
1145                parse_traditional_patch(line, line+len, patch);
1146                *hdrsize = len + nextlen;
1147                linenr += 2;
1148                return offset;
1149        }
1150        return -1;
1151}
1152
1153static void record_ws_error(unsigned result, const char *line, int len, int linenr)
1154{
1155        char *err;
1156
1157        if (!result)
1158                return;
1159
1160        whitespace_error++;
1161        if (squelch_whitespace_errors &&
1162            squelch_whitespace_errors < whitespace_error)
1163                return;
1164
1165        err = whitespace_error_string(result);
1166        fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1167                patch_input_file, linenr, err, len, line);
1168        free(err);
1169}
1170
1171static void check_whitespace(const char *line, int len, unsigned ws_rule)
1172{
1173        unsigned result = ws_check(line + 1, len - 1, ws_rule);
1174
1175        record_ws_error(result, line + 1, len - 2, linenr);
1176}
1177
1178/*
1179 * Parse a unified diff. Note that this really needs to parse each
1180 * fragment separately, since the only way to know the difference
1181 * between a "---" that is part of a patch, and a "---" that starts
1182 * the next patch is to look at the line counts..
1183 */
1184static int parse_fragment(char *line, unsigned long size,
1185                          struct patch *patch, struct fragment *fragment)
1186{
1187        int added, deleted;
1188        int len = linelen(line, size), offset;
1189        unsigned long oldlines, newlines;
1190        unsigned long leading, trailing;
1191
1192        offset = parse_fragment_header(line, len, fragment);
1193        if (offset < 0)
1194                return -1;
1195        if (offset > 0 && patch->recount)
1196                recount_diff(line + offset, size - offset, fragment);
1197        oldlines = fragment->oldlines;
1198        newlines = fragment->newlines;
1199        leading = 0;
1200        trailing = 0;
1201
1202        /* Parse the thing.. */
1203        line += len;
1204        size -= len;
1205        linenr++;
1206        added = deleted = 0;
1207        for (offset = len;
1208             0 < size;
1209             offset += len, size -= len, line += len, linenr++) {
1210                if (!oldlines && !newlines)
1211                        break;
1212                len = linelen(line, size);
1213                if (!len || line[len-1] != '\n')
1214                        return -1;
1215                switch (*line) {
1216                default:
1217                        return -1;
1218                case '\n': /* newer GNU diff, an empty context line */
1219                case ' ':
1220                        oldlines--;
1221                        newlines--;
1222                        if (!deleted && !added)
1223                                leading++;
1224                        trailing++;
1225                        break;
1226                case '-':
1227                        if (apply_in_reverse &&
1228                            ws_error_action != nowarn_ws_error)
1229                                check_whitespace(line, len, patch->ws_rule);
1230                        deleted++;
1231                        oldlines--;
1232                        trailing = 0;
1233                        break;
1234                case '+':
1235                        if (!apply_in_reverse &&
1236                            ws_error_action != nowarn_ws_error)
1237                                check_whitespace(line, len, patch->ws_rule);
1238                        added++;
1239                        newlines--;
1240                        trailing = 0;
1241                        break;
1242
1243                /*
1244                 * We allow "\ No newline at end of file". Depending
1245                 * on locale settings when the patch was produced we
1246                 * don't know what this line looks like. The only
1247                 * thing we do know is that it begins with "\ ".
1248                 * Checking for 12 is just for sanity check -- any
1249                 * l10n of "\ No newline..." is at least that long.
1250                 */
1251                case '\\':
1252                        if (len < 12 || memcmp(line, "\\ ", 2))
1253                                return -1;
1254                        break;
1255                }
1256        }
1257        if (oldlines || newlines)
1258                return -1;
1259        fragment->leading = leading;
1260        fragment->trailing = trailing;
1261
1262        /*
1263         * If a fragment ends with an incomplete line, we failed to include
1264         * it in the above loop because we hit oldlines == newlines == 0
1265         * before seeing it.
1266         */
1267        if (12 < size && !memcmp(line, "\\ ", 2))
1268                offset += linelen(line, size);
1269
1270        patch->lines_added += added;
1271        patch->lines_deleted += deleted;
1272
1273        if (0 < patch->is_new && oldlines)
1274                return error("new file depends on old contents");
1275        if (0 < patch->is_delete && newlines)
1276                return error("deleted file still has contents");
1277        return offset;
1278}
1279
1280static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
1281{
1282        unsigned long offset = 0;
1283        unsigned long oldlines = 0, newlines = 0, context = 0;
1284        struct fragment **fragp = &patch->fragments;
1285
1286        while (size > 4 && !memcmp(line, "@@ -", 4)) {
1287                struct fragment *fragment;
1288                int len;
1289
1290                fragment = xcalloc(1, sizeof(*fragment));
1291                fragment->linenr = linenr;
1292                len = parse_fragment(line, size, patch, fragment);
1293                if (len <= 0)
1294                        die("corrupt patch at line %d", linenr);
1295                fragment->patch = line;
1296                fragment->size = len;
1297                oldlines += fragment->oldlines;
1298                newlines += fragment->newlines;
1299                context += fragment->leading + fragment->trailing;
1300
1301                *fragp = fragment;
1302                fragp = &fragment->next;
1303
1304                offset += len;
1305                line += len;
1306                size -= len;
1307        }
1308
1309        /*
1310         * If something was removed (i.e. we have old-lines) it cannot
1311         * be creation, and if something was added it cannot be
1312         * deletion.  However, the reverse is not true; --unified=0
1313         * patches that only add are not necessarily creation even
1314         * though they do not have any old lines, and ones that only
1315         * delete are not necessarily deletion.
1316         *
1317         * Unfortunately, a real creation/deletion patch do _not_ have
1318         * any context line by definition, so we cannot safely tell it
1319         * apart with --unified=0 insanity.  At least if the patch has
1320         * more than one hunk it is not creation or deletion.
1321         */
1322        if (patch->is_new < 0 &&
1323            (oldlines || (patch->fragments && patch->fragments->next)))
1324                patch->is_new = 0;
1325        if (patch->is_delete < 0 &&
1326            (newlines || (patch->fragments && patch->fragments->next)))
1327                patch->is_delete = 0;
1328
1329        if (0 < patch->is_new && oldlines)
1330                die("new file %s depends on old contents", patch->new_name);
1331        if (0 < patch->is_delete && newlines)
1332                die("deleted file %s still has contents", patch->old_name);
1333        if (!patch->is_delete && !newlines && context)
1334                fprintf(stderr, "** warning: file %s becomes empty but "
1335                        "is not deleted\n", patch->new_name);
1336
1337        return offset;
1338}
1339
1340static inline int metadata_changes(struct patch *patch)
1341{
1342        return  patch->is_rename > 0 ||
1343                patch->is_copy > 0 ||
1344                patch->is_new > 0 ||
1345                patch->is_delete ||
1346                (patch->old_mode && patch->new_mode &&
1347                 patch->old_mode != patch->new_mode);
1348}
1349
1350static char *inflate_it(const void *data, unsigned long size,
1351                        unsigned long inflated_size)
1352{
1353        z_stream stream;
1354        void *out;
1355        int st;
1356
1357        memset(&stream, 0, sizeof(stream));
1358
1359        stream.next_in = (unsigned char *)data;
1360        stream.avail_in = size;
1361        stream.next_out = out = xmalloc(inflated_size);
1362        stream.avail_out = inflated_size;
1363        git_inflate_init(&stream);
1364        st = git_inflate(&stream, Z_FINISH);
1365        git_inflate_end(&stream);
1366        if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1367                free(out);
1368                return NULL;
1369        }
1370        return out;
1371}
1372
1373static struct fragment *parse_binary_hunk(char **buf_p,
1374                                          unsigned long *sz_p,
1375                                          int *status_p,
1376                                          int *used_p)
1377{
1378        /*
1379         * Expect a line that begins with binary patch method ("literal"
1380         * or "delta"), followed by the length of data before deflating.
1381         * a sequence of 'length-byte' followed by base-85 encoded data
1382         * should follow, terminated by a newline.
1383         *
1384         * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1385         * and we would limit the patch line to 66 characters,
1386         * so one line can fit up to 13 groups that would decode
1387         * to 52 bytes max.  The length byte 'A'-'Z' corresponds
1388         * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1389         */
1390        int llen, used;
1391        unsigned long size = *sz_p;
1392        char *buffer = *buf_p;
1393        int patch_method;
1394        unsigned long origlen;
1395        char *data = NULL;
1396        int hunk_size = 0;
1397        struct fragment *frag;
1398
1399        llen = linelen(buffer, size);
1400        used = llen;
1401
1402        *status_p = 0;
1403
1404        if (!prefixcmp(buffer, "delta ")) {
1405                patch_method = BINARY_DELTA_DEFLATED;
1406                origlen = strtoul(buffer + 6, NULL, 10);
1407        }
1408        else if (!prefixcmp(buffer, "literal ")) {
1409                patch_method = BINARY_LITERAL_DEFLATED;
1410                origlen = strtoul(buffer + 8, NULL, 10);
1411        }
1412        else
1413                return NULL;
1414
1415        linenr++;
1416        buffer += llen;
1417        while (1) {
1418                int byte_length, max_byte_length, newsize;
1419                llen = linelen(buffer, size);
1420                used += llen;
1421                linenr++;
1422                if (llen == 1) {
1423                        /* consume the blank line */
1424                        buffer++;
1425                        size--;
1426                        break;
1427                }
1428                /*
1429                 * Minimum line is "A00000\n" which is 7-byte long,
1430                 * and the line length must be multiple of 5 plus 2.
1431                 */
1432                if ((llen < 7) || (llen-2) % 5)
1433                        goto corrupt;
1434                max_byte_length = (llen - 2) / 5 * 4;
1435                byte_length = *buffer;
1436                if ('A' <= byte_length && byte_length <= 'Z')
1437                        byte_length = byte_length - 'A' + 1;
1438                else if ('a' <= byte_length && byte_length <= 'z')
1439                        byte_length = byte_length - 'a' + 27;
1440                else
1441                        goto corrupt;
1442                /* if the input length was not multiple of 4, we would
1443                 * have filler at the end but the filler should never
1444                 * exceed 3 bytes
1445                 */
1446                if (max_byte_length < byte_length ||
1447                    byte_length <= max_byte_length - 4)
1448                        goto corrupt;
1449                newsize = hunk_size + byte_length;
1450                data = xrealloc(data, newsize);
1451                if (decode_85(data + hunk_size, buffer + 1, byte_length))
1452                        goto corrupt;
1453                hunk_size = newsize;
1454                buffer += llen;
1455                size -= llen;
1456        }
1457
1458        frag = xcalloc(1, sizeof(*frag));
1459        frag->patch = inflate_it(data, hunk_size, origlen);
1460        if (!frag->patch)
1461                goto corrupt;
1462        free(data);
1463        frag->size = origlen;
1464        *buf_p = buffer;
1465        *sz_p = size;
1466        *used_p = used;
1467        frag->binary_patch_method = patch_method;
1468        return frag;
1469
1470 corrupt:
1471        free(data);
1472        *status_p = -1;
1473        error("corrupt binary patch at line %d: %.*s",
1474              linenr-1, llen-1, buffer);
1475        return NULL;
1476}
1477
1478static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1479{
1480        /*
1481         * We have read "GIT binary patch\n"; what follows is a line
1482         * that says the patch method (currently, either "literal" or
1483         * "delta") and the length of data before deflating; a
1484         * sequence of 'length-byte' followed by base-85 encoded data
1485         * follows.
1486         *
1487         * When a binary patch is reversible, there is another binary
1488         * hunk in the same format, starting with patch method (either
1489         * "literal" or "delta") with the length of data, and a sequence
1490         * of length-byte + base-85 encoded data, terminated with another
1491         * empty line.  This data, when applied to the postimage, produces
1492         * the preimage.
1493         */
1494        struct fragment *forward;
1495        struct fragment *reverse;
1496        int status;
1497        int used, used_1;
1498
1499        forward = parse_binary_hunk(&buffer, &size, &status, &used);
1500        if (!forward && !status)
1501                /* there has to be one hunk (forward hunk) */
1502                return error("unrecognized binary patch at line %d", linenr-1);
1503        if (status)
1504                /* otherwise we already gave an error message */
1505                return status;
1506
1507        reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1508        if (reverse)
1509                used += used_1;
1510        else if (status) {
1511                /*
1512                 * Not having reverse hunk is not an error, but having
1513                 * a corrupt reverse hunk is.
1514                 */
1515                free((void*) forward->patch);
1516                free(forward);
1517                return status;
1518        }
1519        forward->next = reverse;
1520        patch->fragments = forward;
1521        patch->is_binary = 1;
1522        return used;
1523}
1524
1525static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1526{
1527        int hdrsize, patchsize;
1528        int offset = find_header(buffer, size, &hdrsize, patch);
1529
1530        if (offset < 0)
1531                return offset;
1532
1533        patch->ws_rule = whitespace_rule(patch->new_name
1534                                         ? patch->new_name
1535                                         : patch->old_name);
1536
1537        patchsize = parse_single_patch(buffer + offset + hdrsize,
1538                                       size - offset - hdrsize, patch);
1539
1540        if (!patchsize) {
1541                static const char *binhdr[] = {
1542                        "Binary files ",
1543                        "Files ",
1544                        NULL,
1545                };
1546                static const char git_binary[] = "GIT binary patch\n";
1547                int i;
1548                int hd = hdrsize + offset;
1549                unsigned long llen = linelen(buffer + hd, size - hd);
1550
1551                if (llen == sizeof(git_binary) - 1 &&
1552                    !memcmp(git_binary, buffer + hd, llen)) {
1553                        int used;
1554                        linenr++;
1555                        used = parse_binary(buffer + hd + llen,
1556                                            size - hd - llen, patch);
1557                        if (used)
1558                                patchsize = used + llen;
1559                        else
1560                                patchsize = 0;
1561                }
1562                else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1563                        for (i = 0; binhdr[i]; i++) {
1564                                int len = strlen(binhdr[i]);
1565                                if (len < size - hd &&
1566                                    !memcmp(binhdr[i], buffer + hd, len)) {
1567                                        linenr++;
1568                                        patch->is_binary = 1;
1569                                        patchsize = llen;
1570                                        break;
1571                                }
1572                        }
1573                }
1574
1575                /* Empty patch cannot be applied if it is a text patch
1576                 * without metadata change.  A binary patch appears
1577                 * empty to us here.
1578                 */
1579                if ((apply || check) &&
1580                    (!patch->is_binary && !metadata_changes(patch)))
1581                        die("patch with only garbage at line %d", linenr);
1582        }
1583
1584        return offset + hdrsize + patchsize;
1585}
1586
1587#define swap(a,b) myswap((a),(b),sizeof(a))
1588
1589#define myswap(a, b, size) do {         \
1590        unsigned char mytmp[size];      \
1591        memcpy(mytmp, &a, size);                \
1592        memcpy(&a, &b, size);           \
1593        memcpy(&b, mytmp, size);                \
1594} while (0)
1595
1596static void reverse_patches(struct patch *p)
1597{
1598        for (; p; p = p->next) {
1599                struct fragment *frag = p->fragments;
1600
1601                swap(p->new_name, p->old_name);
1602                swap(p->new_mode, p->old_mode);
1603                swap(p->is_new, p->is_delete);
1604                swap(p->lines_added, p->lines_deleted);
1605                swap(p->old_sha1_prefix, p->new_sha1_prefix);
1606
1607                for (; frag; frag = frag->next) {
1608                        swap(frag->newpos, frag->oldpos);
1609                        swap(frag->newlines, frag->oldlines);
1610                }
1611        }
1612}
1613
1614static const char pluses[] =
1615"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1616static const char minuses[]=
1617"----------------------------------------------------------------------";
1618
1619static void show_stats(struct patch *patch)
1620{
1621        struct strbuf qname = STRBUF_INIT;
1622        char *cp = patch->new_name ? patch->new_name : patch->old_name;
1623        int max, add, del;
1624
1625        quote_c_style(cp, &qname, NULL, 0);
1626
1627        /*
1628         * "scale" the filename
1629         */
1630        max = max_len;
1631        if (max > 50)
1632                max = 50;
1633
1634        if (qname.len > max) {
1635                cp = strchr(qname.buf + qname.len + 3 - max, '/');
1636                if (!cp)
1637                        cp = qname.buf + qname.len + 3 - max;
1638                strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
1639        }
1640
1641        if (patch->is_binary) {
1642                printf(" %-*s |  Bin\n", max, qname.buf);
1643                strbuf_release(&qname);
1644                return;
1645        }
1646
1647        printf(" %-*s |", max, qname.buf);
1648        strbuf_release(&qname);
1649
1650        /*
1651         * scale the add/delete
1652         */
1653        max = max + max_change > 70 ? 70 - max : max_change;
1654        add = patch->lines_added;
1655        del = patch->lines_deleted;
1656
1657        if (max_change > 0) {
1658                int total = ((add + del) * max + max_change / 2) / max_change;
1659                add = (add * max + max_change / 2) / max_change;
1660                del = total - add;
1661        }
1662        printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
1663                add, pluses, del, minuses);
1664}
1665
1666static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1667{
1668        switch (st->st_mode & S_IFMT) {
1669        case S_IFLNK:
1670                if (strbuf_readlink(buf, path, st->st_size) < 0)
1671                        return error("unable to read symlink %s", path);
1672                return 0;
1673        case S_IFREG:
1674                if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
1675                        return error("unable to open or read %s", path);
1676                convert_to_git(path, buf->buf, buf->len, buf, 0);
1677                return 0;
1678        default:
1679                return -1;
1680        }
1681}
1682
1683static void update_pre_post_images(struct image *preimage,
1684                                   struct image *postimage,
1685                                   char *buf,
1686                                   size_t len)
1687{
1688        int i, ctx;
1689        char *new, *old, *fixed;
1690        struct image fixed_preimage;
1691
1692        /*
1693         * Update the preimage with whitespace fixes.  Note that we
1694         * are not losing preimage->buf -- apply_one_fragment() will
1695         * free "oldlines".
1696         */
1697        prepare_image(&fixed_preimage, buf, len, 1);
1698        assert(fixed_preimage.nr == preimage->nr);
1699        for (i = 0; i < preimage->nr; i++)
1700                fixed_preimage.line[i].flag = preimage->line[i].flag;
1701        free(preimage->line_allocated);
1702        *preimage = fixed_preimage;
1703
1704        /*
1705         * Adjust the common context lines in postimage, in place.
1706         * This is possible because whitespace fixing does not make
1707         * the string grow.
1708         */
1709        new = old = postimage->buf;
1710        fixed = preimage->buf;
1711        for (i = ctx = 0; i < postimage->nr; i++) {
1712                size_t len = postimage->line[i].len;
1713                if (!(postimage->line[i].flag & LINE_COMMON)) {
1714                        /* an added line -- no counterparts in preimage */
1715                        memmove(new, old, len);
1716                        old += len;
1717                        new += len;
1718                        continue;
1719                }
1720
1721                /* a common context -- skip it in the original postimage */
1722                old += len;
1723
1724                /* and find the corresponding one in the fixed preimage */
1725                while (ctx < preimage->nr &&
1726                       !(preimage->line[ctx].flag & LINE_COMMON)) {
1727                        fixed += preimage->line[ctx].len;
1728                        ctx++;
1729                }
1730                if (preimage->nr <= ctx)
1731                        die("oops");
1732
1733                /* and copy it in, while fixing the line length */
1734                len = preimage->line[ctx].len;
1735                memcpy(new, fixed, len);
1736                new += len;
1737                fixed += len;
1738                postimage->line[i].len = len;
1739                ctx++;
1740        }
1741
1742        /* Fix the length of the whole thing */
1743        postimage->len = new - postimage->buf;
1744}
1745
1746static int match_fragment(struct image *img,
1747                          struct image *preimage,
1748                          struct image *postimage,
1749                          unsigned long try,
1750                          int try_lno,
1751                          unsigned ws_rule,
1752                          int match_beginning, int match_end)
1753{
1754        int i;
1755        char *fixed_buf, *buf, *orig, *target;
1756
1757        if (preimage->nr + try_lno > img->nr)
1758                return 0;
1759
1760        if (match_beginning && try_lno)
1761                return 0;
1762
1763        if (match_end && preimage->nr + try_lno != img->nr)
1764                return 0;
1765
1766        /* Quick hash check */
1767        for (i = 0; i < preimage->nr; i++)
1768                if (preimage->line[i].hash != img->line[try_lno + i].hash)
1769                        return 0;
1770
1771        /*
1772         * Do we have an exact match?  If we were told to match
1773         * at the end, size must be exactly at try+fragsize,
1774         * otherwise try+fragsize must be still within the preimage,
1775         * and either case, the old piece should match the preimage
1776         * exactly.
1777         */
1778        if ((match_end
1779             ? (try + preimage->len == img->len)
1780             : (try + preimage->len <= img->len)) &&
1781            !memcmp(img->buf + try, preimage->buf, preimage->len))
1782                return 1;
1783
1784        if (ws_error_action != correct_ws_error)
1785                return 0;
1786
1787        /*
1788         * The hunk does not apply byte-by-byte, but the hash says
1789         * it might with whitespace fuzz.
1790         */
1791        fixed_buf = xmalloc(preimage->len + 1);
1792        buf = fixed_buf;
1793        orig = preimage->buf;
1794        target = img->buf + try;
1795        for (i = 0; i < preimage->nr; i++) {
1796                size_t fixlen; /* length after fixing the preimage */
1797                size_t oldlen = preimage->line[i].len;
1798                size_t tgtlen = img->line[try_lno + i].len;
1799                size_t tgtfixlen; /* length after fixing the target line */
1800                char tgtfixbuf[1024], *tgtfix;
1801                int match;
1802
1803                /* Try fixing the line in the preimage */
1804                fixlen = ws_fix_copy(buf, orig, oldlen, ws_rule, NULL);
1805
1806                /* Try fixing the line in the target */
1807                if (sizeof(tgtfixbuf) > tgtlen)
1808                        tgtfix = tgtfixbuf;
1809                else
1810                        tgtfix = xmalloc(tgtlen);
1811                tgtfixlen = ws_fix_copy(tgtfix, target, tgtlen, ws_rule, NULL);
1812
1813                /*
1814                 * If they match, either the preimage was based on
1815                 * a version before our tree fixed whitespace breakage,
1816                 * or we are lacking a whitespace-fix patch the tree
1817                 * the preimage was based on already had (i.e. target
1818                 * has whitespace breakage, the preimage doesn't).
1819                 * In either case, we are fixing the whitespace breakages
1820                 * so we might as well take the fix together with their
1821                 * real change.
1822                 */
1823                match = (tgtfixlen == fixlen && !memcmp(tgtfix, buf, fixlen));
1824
1825                if (tgtfix != tgtfixbuf)
1826                        free(tgtfix);
1827                if (!match)
1828                        goto unmatch_exit;
1829
1830                orig += oldlen;
1831                buf += fixlen;
1832                target += tgtlen;
1833        }
1834
1835        /*
1836         * Yes, the preimage is based on an older version that still
1837         * has whitespace breakages unfixed, and fixing them makes the
1838         * hunk match.  Update the context lines in the postimage.
1839         */
1840        update_pre_post_images(preimage, postimage,
1841                               fixed_buf, buf - fixed_buf);
1842        return 1;
1843
1844 unmatch_exit:
1845        free(fixed_buf);
1846        return 0;
1847}
1848
1849static int find_pos(struct image *img,
1850                    struct image *preimage,
1851                    struct image *postimage,
1852                    int line,
1853                    unsigned ws_rule,
1854                    int match_beginning, int match_end)
1855{
1856        int i;
1857        unsigned long backwards, forwards, try;
1858        int backwards_lno, forwards_lno, try_lno;
1859
1860        if (preimage->nr > img->nr)
1861                return -1;
1862
1863        /*
1864         * If match_begining or match_end is specified, there is no
1865         * point starting from a wrong line that will never match and
1866         * wander around and wait for a match at the specified end.
1867         */
1868        if (match_beginning)
1869                line = 0;
1870        else if (match_end)
1871                line = img->nr - preimage->nr;
1872
1873        if (line > img->nr)
1874                line = img->nr;
1875
1876        try = 0;
1877        for (i = 0; i < line; i++)
1878                try += img->line[i].len;
1879
1880        /*
1881         * There's probably some smart way to do this, but I'll leave
1882         * that to the smart and beautiful people. I'm simple and stupid.
1883         */
1884        backwards = try;
1885        backwards_lno = line;
1886        forwards = try;
1887        forwards_lno = line;
1888        try_lno = line;
1889
1890        for (i = 0; ; i++) {
1891                if (match_fragment(img, preimage, postimage,
1892                                   try, try_lno, ws_rule,
1893                                   match_beginning, match_end))
1894                        return try_lno;
1895
1896        again:
1897                if (backwards_lno == 0 && forwards_lno == img->nr)
1898                        break;
1899
1900                if (i & 1) {
1901                        if (backwards_lno == 0) {
1902                                i++;
1903                                goto again;
1904                        }
1905                        backwards_lno--;
1906                        backwards -= img->line[backwards_lno].len;
1907                        try = backwards;
1908                        try_lno = backwards_lno;
1909                } else {
1910                        if (forwards_lno == img->nr) {
1911                                i++;
1912                                goto again;
1913                        }
1914                        forwards += img->line[forwards_lno].len;
1915                        forwards_lno++;
1916                        try = forwards;
1917                        try_lno = forwards_lno;
1918                }
1919
1920        }
1921        return -1;
1922}
1923
1924static void remove_first_line(struct image *img)
1925{
1926        img->buf += img->line[0].len;
1927        img->len -= img->line[0].len;
1928        img->line++;
1929        img->nr--;
1930}
1931
1932static void remove_last_line(struct image *img)
1933{
1934        img->len -= img->line[--img->nr].len;
1935}
1936
1937static void update_image(struct image *img,
1938                         int applied_pos,
1939                         struct image *preimage,
1940                         struct image *postimage)
1941{
1942        /*
1943         * remove the copy of preimage at offset in img
1944         * and replace it with postimage
1945         */
1946        int i, nr;
1947        size_t remove_count, insert_count, applied_at = 0;
1948        char *result;
1949
1950        for (i = 0; i < applied_pos; i++)
1951                applied_at += img->line[i].len;
1952
1953        remove_count = 0;
1954        for (i = 0; i < preimage->nr; i++)
1955                remove_count += img->line[applied_pos + i].len;
1956        insert_count = postimage->len;
1957
1958        /* Adjust the contents */
1959        result = xmalloc(img->len + insert_count - remove_count + 1);
1960        memcpy(result, img->buf, applied_at);
1961        memcpy(result + applied_at, postimage->buf, postimage->len);
1962        memcpy(result + applied_at + postimage->len,
1963               img->buf + (applied_at + remove_count),
1964               img->len - (applied_at + remove_count));
1965        free(img->buf);
1966        img->buf = result;
1967        img->len += insert_count - remove_count;
1968        result[img->len] = '\0';
1969
1970        /* Adjust the line table */
1971        nr = img->nr + postimage->nr - preimage->nr;
1972        if (preimage->nr < postimage->nr) {
1973                /*
1974                 * NOTE: this knows that we never call remove_first_line()
1975                 * on anything other than pre/post image.
1976                 */
1977                img->line = xrealloc(img->line, nr * sizeof(*img->line));
1978                img->line_allocated = img->line;
1979        }
1980        if (preimage->nr != postimage->nr)
1981                memmove(img->line + applied_pos + postimage->nr,
1982                        img->line + applied_pos + preimage->nr,
1983                        (img->nr - (applied_pos + preimage->nr)) *
1984                        sizeof(*img->line));
1985        memcpy(img->line + applied_pos,
1986               postimage->line,
1987               postimage->nr * sizeof(*img->line));
1988        img->nr = nr;
1989}
1990
1991static int apply_one_fragment(struct image *img, struct fragment *frag,
1992                              int inaccurate_eof, unsigned ws_rule)
1993{
1994        int match_beginning, match_end;
1995        const char *patch = frag->patch;
1996        int size = frag->size;
1997        char *old, *new, *oldlines, *newlines;
1998        int new_blank_lines_at_end = 0;
1999        unsigned long leading, trailing;
2000        int pos, applied_pos;
2001        struct image preimage;
2002        struct image postimage;
2003
2004        memset(&preimage, 0, sizeof(preimage));
2005        memset(&postimage, 0, sizeof(postimage));
2006        oldlines = xmalloc(size);
2007        newlines = xmalloc(size);
2008
2009        old = oldlines;
2010        new = newlines;
2011        while (size > 0) {
2012                char first;
2013                int len = linelen(patch, size);
2014                int plen, added;
2015                int added_blank_line = 0;
2016                int is_blank_context = 0;
2017
2018                if (!len)
2019                        break;
2020
2021                /*
2022                 * "plen" is how much of the line we should use for
2023                 * the actual patch data. Normally we just remove the
2024                 * first character on the line, but if the line is
2025                 * followed by "\ No newline", then we also remove the
2026                 * last one (which is the newline, of course).
2027                 */
2028                plen = len - 1;
2029                if (len < size && patch[len] == '\\')
2030                        plen--;
2031                first = *patch;
2032                if (apply_in_reverse) {
2033                        if (first == '-')
2034                                first = '+';
2035                        else if (first == '+')
2036                                first = '-';
2037                }
2038
2039                switch (first) {
2040                case '\n':
2041                        /* Newer GNU diff, empty context line */
2042                        if (plen < 0)
2043                                /* ... followed by '\No newline'; nothing */
2044                                break;
2045                        *old++ = '\n';
2046                        *new++ = '\n';
2047                        add_line_info(&preimage, "\n", 1, LINE_COMMON);
2048                        add_line_info(&postimage, "\n", 1, LINE_COMMON);
2049                        is_blank_context = 1;
2050                        break;
2051                case ' ':
2052                        if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2053                            ws_blank_line(patch + 1, plen, ws_rule))
2054                                is_blank_context = 1;
2055                case '-':
2056                        memcpy(old, patch + 1, plen);
2057                        add_line_info(&preimage, old, plen,
2058                                      (first == ' ' ? LINE_COMMON : 0));
2059                        old += plen;
2060                        if (first == '-')
2061                                break;
2062                /* Fall-through for ' ' */
2063                case '+':
2064                        /* --no-add does not add new lines */
2065                        if (first == '+' && no_add)
2066                                break;
2067
2068                        if (first != '+' ||
2069                            !whitespace_error ||
2070                            ws_error_action != correct_ws_error) {
2071                                memcpy(new, patch + 1, plen);
2072                                added = plen;
2073                        }
2074                        else {
2075                                added = ws_fix_copy(new, patch + 1, plen, ws_rule, &applied_after_fixing_ws);
2076                        }
2077                        add_line_info(&postimage, new, added,
2078                                      (first == '+' ? 0 : LINE_COMMON));
2079                        new += added;
2080                        if (first == '+' &&
2081                            (ws_rule & WS_BLANK_AT_EOF) &&
2082                            ws_blank_line(patch + 1, plen, ws_rule))
2083                                added_blank_line = 1;
2084                        break;
2085                case '@': case '\\':
2086                        /* Ignore it, we already handled it */
2087                        break;
2088                default:
2089                        if (apply_verbosely)
2090                                error("invalid start of line: '%c'", first);
2091                        return -1;
2092                }
2093                if (added_blank_line)
2094                        new_blank_lines_at_end++;
2095                else if (is_blank_context)
2096                        ;
2097                else
2098                        new_blank_lines_at_end = 0;
2099                patch += len;
2100                size -= len;
2101        }
2102        if (inaccurate_eof &&
2103            old > oldlines && old[-1] == '\n' &&
2104            new > newlines && new[-1] == '\n') {
2105                old--;
2106                new--;
2107        }
2108
2109        leading = frag->leading;
2110        trailing = frag->trailing;
2111
2112        /*
2113         * A hunk to change lines at the beginning would begin with
2114         * @@ -1,L +N,M @@
2115         * but we need to be careful.  -U0 that inserts before the second
2116         * line also has this pattern.
2117         *
2118         * And a hunk to add to an empty file would begin with
2119         * @@ -0,0 +N,M @@
2120         *
2121         * In other words, a hunk that is (frag->oldpos <= 1) with or
2122         * without leading context must match at the beginning.
2123         */
2124        match_beginning = (!frag->oldpos ||
2125                           (frag->oldpos == 1 && !unidiff_zero));
2126
2127        /*
2128         * A hunk without trailing lines must match at the end.
2129         * However, we simply cannot tell if a hunk must match end
2130         * from the lack of trailing lines if the patch was generated
2131         * with unidiff without any context.
2132         */
2133        match_end = !unidiff_zero && !trailing;
2134
2135        pos = frag->newpos ? (frag->newpos - 1) : 0;
2136        preimage.buf = oldlines;
2137        preimage.len = old - oldlines;
2138        postimage.buf = newlines;
2139        postimage.len = new - newlines;
2140        preimage.line = preimage.line_allocated;
2141        postimage.line = postimage.line_allocated;
2142
2143        for (;;) {
2144
2145                applied_pos = find_pos(img, &preimage, &postimage, pos,
2146                                       ws_rule, match_beginning, match_end);
2147
2148                if (applied_pos >= 0)
2149                        break;
2150
2151                /* Am I at my context limits? */
2152                if ((leading <= p_context) && (trailing <= p_context))
2153                        break;
2154                if (match_beginning || match_end) {
2155                        match_beginning = match_end = 0;
2156                        continue;
2157                }
2158
2159                /*
2160                 * Reduce the number of context lines; reduce both
2161                 * leading and trailing if they are equal otherwise
2162                 * just reduce the larger context.
2163                 */
2164                if (leading >= trailing) {
2165                        remove_first_line(&preimage);
2166                        remove_first_line(&postimage);
2167                        pos--;
2168                        leading--;
2169                }
2170                if (trailing > leading) {
2171                        remove_last_line(&preimage);
2172                        remove_last_line(&postimage);
2173                        trailing--;
2174                }
2175        }
2176
2177        if (applied_pos >= 0) {
2178                if (new_blank_lines_at_end &&
2179                    preimage.nr + applied_pos == img->nr &&
2180                    (ws_rule & WS_BLANK_AT_EOF) &&
2181                    ws_error_action != nowarn_ws_error) {
2182                        record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);
2183                        if (ws_error_action == correct_ws_error) {
2184                                while (new_blank_lines_at_end--)
2185                                        remove_last_line(&postimage);
2186                        }
2187                        /*
2188                         * We would want to prevent write_out_results()
2189                         * from taking place in apply_patch() that follows
2190                         * the callchain led us here, which is:
2191                         * apply_patch->check_patch_list->check_patch->
2192                         * apply_data->apply_fragments->apply_one_fragment
2193                         */
2194                        if (ws_error_action == die_on_ws_error)
2195                                apply = 0;
2196                }
2197
2198                /*
2199                 * Warn if it was necessary to reduce the number
2200                 * of context lines.
2201                 */
2202                if ((leading != frag->leading) ||
2203                    (trailing != frag->trailing))
2204                        fprintf(stderr, "Context reduced to (%ld/%ld)"
2205                                " to apply fragment at %d\n",
2206                                leading, trailing, applied_pos+1);
2207                update_image(img, applied_pos, &preimage, &postimage);
2208        } else {
2209                if (apply_verbosely)
2210                        error("while searching for:\n%.*s",
2211                              (int)(old - oldlines), oldlines);
2212        }
2213
2214        free(oldlines);
2215        free(newlines);
2216        free(preimage.line_allocated);
2217        free(postimage.line_allocated);
2218
2219        return (applied_pos < 0);
2220}
2221
2222static int apply_binary_fragment(struct image *img, struct patch *patch)
2223{
2224        struct fragment *fragment = patch->fragments;
2225        unsigned long len;
2226        void *dst;
2227
2228        /* Binary patch is irreversible without the optional second hunk */
2229        if (apply_in_reverse) {
2230                if (!fragment->next)
2231                        return error("cannot reverse-apply a binary patch "
2232                                     "without the reverse hunk to '%s'",
2233                                     patch->new_name
2234                                     ? patch->new_name : patch->old_name);
2235                fragment = fragment->next;
2236        }
2237        switch (fragment->binary_patch_method) {
2238        case BINARY_DELTA_DEFLATED:
2239                dst = patch_delta(img->buf, img->len, fragment->patch,
2240                                  fragment->size, &len);
2241                if (!dst)
2242                        return -1;
2243                clear_image(img);
2244                img->buf = dst;
2245                img->len = len;
2246                return 0;
2247        case BINARY_LITERAL_DEFLATED:
2248                clear_image(img);
2249                img->len = fragment->size;
2250                img->buf = xmalloc(img->len+1);
2251                memcpy(img->buf, fragment->patch, img->len);
2252                img->buf[img->len] = '\0';
2253                return 0;
2254        }
2255        return -1;
2256}
2257
2258static int apply_binary(struct image *img, struct patch *patch)
2259{
2260        const char *name = patch->old_name ? patch->old_name : patch->new_name;
2261        unsigned char sha1[20];
2262
2263        /*
2264         * For safety, we require patch index line to contain
2265         * full 40-byte textual SHA1 for old and new, at least for now.
2266         */
2267        if (strlen(patch->old_sha1_prefix) != 40 ||
2268            strlen(patch->new_sha1_prefix) != 40 ||
2269            get_sha1_hex(patch->old_sha1_prefix, sha1) ||
2270            get_sha1_hex(patch->new_sha1_prefix, sha1))
2271                return error("cannot apply binary patch to '%s' "
2272                             "without full index line", name);
2273
2274        if (patch->old_name) {
2275                /*
2276                 * See if the old one matches what the patch
2277                 * applies to.
2278                 */
2279                hash_sha1_file(img->buf, img->len, blob_type, sha1);
2280                if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
2281                        return error("the patch applies to '%s' (%s), "
2282                                     "which does not match the "
2283                                     "current contents.",
2284                                     name, sha1_to_hex(sha1));
2285        }
2286        else {
2287                /* Otherwise, the old one must be empty. */
2288                if (img->len)
2289                        return error("the patch applies to an empty "
2290                                     "'%s' but it is not empty", name);
2291        }
2292
2293        get_sha1_hex(patch->new_sha1_prefix, sha1);
2294        if (is_null_sha1(sha1)) {
2295                clear_image(img);
2296                return 0; /* deletion patch */
2297        }
2298
2299        if (has_sha1_file(sha1)) {
2300                /* We already have the postimage */
2301                enum object_type type;
2302                unsigned long size;
2303                char *result;
2304
2305                result = read_sha1_file(sha1, &type, &size);
2306                if (!result)
2307                        return error("the necessary postimage %s for "
2308                                     "'%s' cannot be read",
2309                                     patch->new_sha1_prefix, name);
2310                clear_image(img);
2311                img->buf = result;
2312                img->len = size;
2313        } else {
2314                /*
2315                 * We have verified buf matches the preimage;
2316                 * apply the patch data to it, which is stored
2317                 * in the patch->fragments->{patch,size}.
2318                 */
2319                if (apply_binary_fragment(img, patch))
2320                        return error("binary patch does not apply to '%s'",
2321                                     name);
2322
2323                /* verify that the result matches */
2324                hash_sha1_file(img->buf, img->len, blob_type, sha1);
2325                if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
2326                        return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",
2327                                name, patch->new_sha1_prefix, sha1_to_hex(sha1));
2328        }
2329
2330        return 0;
2331}
2332
2333static int apply_fragments(struct image *img, struct patch *patch)
2334{
2335        struct fragment *frag = patch->fragments;
2336        const char *name = patch->old_name ? patch->old_name : patch->new_name;
2337        unsigned ws_rule = patch->ws_rule;
2338        unsigned inaccurate_eof = patch->inaccurate_eof;
2339
2340        if (patch->is_binary)
2341                return apply_binary(img, patch);
2342
2343        while (frag) {
2344                if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule)) {
2345                        error("patch failed: %s:%ld", name, frag->oldpos);
2346                        if (!apply_with_reject)
2347                                return -1;
2348                        frag->rejected = 1;
2349                }
2350                frag = frag->next;
2351        }
2352        return 0;
2353}
2354
2355static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2356{
2357        if (!ce)
2358                return 0;
2359
2360        if (S_ISGITLINK(ce->ce_mode)) {
2361                strbuf_grow(buf, 100);
2362                strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
2363        } else {
2364                enum object_type type;
2365                unsigned long sz;
2366                char *result;
2367
2368                result = read_sha1_file(ce->sha1, &type, &sz);
2369                if (!result)
2370                        return -1;
2371                /* XXX read_sha1_file NUL-terminates */
2372                strbuf_attach(buf, result, sz, sz + 1);
2373        }
2374        return 0;
2375}
2376
2377static struct patch *in_fn_table(const char *name)
2378{
2379        struct string_list_item *item;
2380
2381        if (name == NULL)
2382                return NULL;
2383
2384        item = string_list_lookup(name, &fn_table);
2385        if (item != NULL)
2386                return (struct patch *)item->util;
2387
2388        return NULL;
2389}
2390
2391/*
2392 * item->util in the filename table records the status of the path.
2393 * Usually it points at a patch (whose result records the contents
2394 * of it after applying it), but it could be PATH_WAS_DELETED for a
2395 * path that a previously applied patch has already removed.
2396 */
2397 #define PATH_TO_BE_DELETED ((struct patch *) -2)
2398#define PATH_WAS_DELETED ((struct patch *) -1)
2399
2400static int to_be_deleted(struct patch *patch)
2401{
2402        return patch == PATH_TO_BE_DELETED;
2403}
2404
2405static int was_deleted(struct patch *patch)
2406{
2407        return patch == PATH_WAS_DELETED;
2408}
2409
2410static void add_to_fn_table(struct patch *patch)
2411{
2412        struct string_list_item *item;
2413
2414        /*
2415         * Always add new_name unless patch is a deletion
2416         * This should cover the cases for normal diffs,
2417         * file creations and copies
2418         */
2419        if (patch->new_name != NULL) {
2420                item = string_list_insert(patch->new_name, &fn_table);
2421                item->util = patch;
2422        }
2423
2424        /*
2425         * store a failure on rename/deletion cases because
2426         * later chunks shouldn't patch old names
2427         */
2428        if ((patch->new_name == NULL) || (patch->is_rename)) {
2429                item = string_list_insert(patch->old_name, &fn_table);
2430                item->util = PATH_WAS_DELETED;
2431        }
2432}
2433
2434static void prepare_fn_table(struct patch *patch)
2435{
2436        /*
2437         * store information about incoming file deletion
2438         */
2439        while (patch) {
2440                if ((patch->new_name == NULL) || (patch->is_rename)) {
2441                        struct string_list_item *item;
2442                        item = string_list_insert(patch->old_name, &fn_table);
2443                        item->util = PATH_TO_BE_DELETED;
2444                }
2445                patch = patch->next;
2446        }
2447}
2448
2449static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
2450{
2451        struct strbuf buf = STRBUF_INIT;
2452        struct image image;
2453        size_t len;
2454        char *img;
2455        struct patch *tpatch;
2456
2457        if (!(patch->is_copy || patch->is_rename) &&
2458            (tpatch = in_fn_table(patch->old_name)) != NULL && !to_be_deleted(tpatch)) {
2459                if (was_deleted(tpatch)) {
2460                        return error("patch %s has been renamed/deleted",
2461                                patch->old_name);
2462                }
2463                /* We have a patched copy in memory use that */
2464                strbuf_add(&buf, tpatch->result, tpatch->resultsize);
2465        } else if (cached) {
2466                if (read_file_or_gitlink(ce, &buf))
2467                        return error("read of %s failed", patch->old_name);
2468        } else if (patch->old_name) {
2469                if (S_ISGITLINK(patch->old_mode)) {
2470                        if (ce) {
2471                                read_file_or_gitlink(ce, &buf);
2472                        } else {
2473                                /*
2474                                 * There is no way to apply subproject
2475                                 * patch without looking at the index.
2476                                 */
2477                                patch->fragments = NULL;
2478                        }
2479                } else {
2480                        if (read_old_data(st, patch->old_name, &buf))
2481                                return error("read of %s failed", patch->old_name);
2482                }
2483        }
2484
2485        img = strbuf_detach(&buf, &len);
2486        prepare_image(&image, img, len, !patch->is_binary);
2487
2488        if (apply_fragments(&image, patch) < 0)
2489                return -1; /* note with --reject this succeeds. */
2490        patch->result = image.buf;
2491        patch->resultsize = image.len;
2492        add_to_fn_table(patch);
2493        free(image.line_allocated);
2494
2495        if (0 < patch->is_delete && patch->resultsize)
2496                return error("removal patch leaves file contents");
2497
2498        return 0;
2499}
2500
2501static int check_to_create_blob(const char *new_name, int ok_if_exists)
2502{
2503        struct stat nst;
2504        if (!lstat(new_name, &nst)) {
2505                if (S_ISDIR(nst.st_mode) || ok_if_exists)
2506                        return 0;
2507                /*
2508                 * A leading component of new_name might be a symlink
2509                 * that is going to be removed with this patch, but
2510                 * still pointing at somewhere that has the path.
2511                 * In such a case, path "new_name" does not exist as
2512                 * far as git is concerned.
2513                 */
2514                if (has_symlink_leading_path(new_name, strlen(new_name)))
2515                        return 0;
2516
2517                return error("%s: already exists in working directory", new_name);
2518        }
2519        else if ((errno != ENOENT) && (errno != ENOTDIR))
2520                return error("%s: %s", new_name, strerror(errno));
2521        return 0;
2522}
2523
2524static int verify_index_match(struct cache_entry *ce, struct stat *st)
2525{
2526        if (S_ISGITLINK(ce->ce_mode)) {
2527                if (!S_ISDIR(st->st_mode))
2528                        return -1;
2529                return 0;
2530        }
2531        return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
2532}
2533
2534static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
2535{
2536        const char *old_name = patch->old_name;
2537        struct patch *tpatch = NULL;
2538        int stat_ret = 0;
2539        unsigned st_mode = 0;
2540
2541        /*
2542         * Make sure that we do not have local modifications from the
2543         * index when we are looking at the index.  Also make sure
2544         * we have the preimage file to be patched in the work tree,
2545         * unless --cached, which tells git to apply only in the index.
2546         */
2547        if (!old_name)
2548                return 0;
2549
2550        assert(patch->is_new <= 0);
2551
2552        if (!(patch->is_copy || patch->is_rename) &&
2553            (tpatch = in_fn_table(old_name)) != NULL && !to_be_deleted(tpatch)) {
2554                if (was_deleted(tpatch))
2555                        return error("%s: has been deleted/renamed", old_name);
2556                st_mode = tpatch->new_mode;
2557        } else if (!cached) {
2558                stat_ret = lstat(old_name, st);
2559                if (stat_ret && errno != ENOENT)
2560                        return error("%s: %s", old_name, strerror(errno));
2561        }
2562
2563        if (to_be_deleted(tpatch))
2564                tpatch = NULL;
2565
2566        if (check_index && !tpatch) {
2567                int pos = cache_name_pos(old_name, strlen(old_name));
2568                if (pos < 0) {
2569                        if (patch->is_new < 0)
2570                                goto is_new;
2571                        return error("%s: does not exist in index", old_name);
2572                }
2573                *ce = active_cache[pos];
2574                if (stat_ret < 0) {
2575                        struct checkout costate;
2576                        /* checkout */
2577                        costate.base_dir = "";
2578                        costate.base_dir_len = 0;
2579                        costate.force = 0;
2580                        costate.quiet = 0;
2581                        costate.not_new = 0;
2582                        costate.refresh_cache = 1;
2583                        if (checkout_entry(*ce, &costate, NULL) ||
2584                            lstat(old_name, st))
2585                                return -1;
2586                }
2587                if (!cached && verify_index_match(*ce, st))
2588                        return error("%s: does not match index", old_name);
2589                if (cached)
2590                        st_mode = (*ce)->ce_mode;
2591        } else if (stat_ret < 0) {
2592                if (patch->is_new < 0)
2593                        goto is_new;
2594                return error("%s: %s", old_name, strerror(errno));
2595        }
2596
2597        if (!cached && !tpatch)
2598                st_mode = ce_mode_from_stat(*ce, st->st_mode);
2599
2600        if (patch->is_new < 0)
2601                patch->is_new = 0;
2602        if (!patch->old_mode)
2603                patch->old_mode = st_mode;
2604        if ((st_mode ^ patch->old_mode) & S_IFMT)
2605                return error("%s: wrong type", old_name);
2606        if (st_mode != patch->old_mode)
2607                warning("%s has type %o, expected %o",
2608                        old_name, st_mode, patch->old_mode);
2609        if (!patch->new_mode && !patch->is_delete)
2610                patch->new_mode = st_mode;
2611        return 0;
2612
2613 is_new:
2614        patch->is_new = 1;
2615        patch->is_delete = 0;
2616        patch->old_name = NULL;
2617        return 0;
2618}
2619
2620static int check_patch(struct patch *patch)
2621{
2622        struct stat st;
2623        const char *old_name = patch->old_name;
2624        const char *new_name = patch->new_name;
2625        const char *name = old_name ? old_name : new_name;
2626        struct cache_entry *ce = NULL;
2627        struct patch *tpatch;
2628        int ok_if_exists;
2629        int status;
2630
2631        patch->rejected = 1; /* we will drop this after we succeed */
2632
2633        status = check_preimage(patch, &ce, &st);
2634        if (status)
2635                return status;
2636        old_name = patch->old_name;
2637
2638        if ((tpatch = in_fn_table(new_name)) &&
2639                        (was_deleted(tpatch) || to_be_deleted(tpatch)))
2640                /*
2641                 * A type-change diff is always split into a patch to
2642                 * delete old, immediately followed by a patch to
2643                 * create new (see diff.c::run_diff()); in such a case
2644                 * it is Ok that the entry to be deleted by the
2645                 * previous patch is still in the working tree and in
2646                 * the index.
2647                 */
2648                ok_if_exists = 1;
2649        else
2650                ok_if_exists = 0;
2651
2652        if (new_name &&
2653            ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
2654                if (check_index &&
2655                    cache_name_pos(new_name, strlen(new_name)) >= 0 &&
2656                    !ok_if_exists)
2657                        return error("%s: already exists in index", new_name);
2658                if (!cached) {
2659                        int err = check_to_create_blob(new_name, ok_if_exists);
2660                        if (err)
2661                                return err;
2662                }
2663                if (!patch->new_mode) {
2664                        if (0 < patch->is_new)
2665                                patch->new_mode = S_IFREG | 0644;
2666                        else
2667                                patch->new_mode = patch->old_mode;
2668                }
2669        }
2670
2671        if (new_name && old_name) {
2672                int same = !strcmp(old_name, new_name);
2673                if (!patch->new_mode)
2674                        patch->new_mode = patch->old_mode;
2675                if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
2676                        return error("new mode (%o) of %s does not match old mode (%o)%s%s",
2677                                patch->new_mode, new_name, patch->old_mode,
2678                                same ? "" : " of ", same ? "" : old_name);
2679        }
2680
2681        if (apply_data(patch, &st, ce) < 0)
2682                return error("%s: patch does not apply", name);
2683        patch->rejected = 0;
2684        return 0;
2685}
2686
2687static int check_patch_list(struct patch *patch)
2688{
2689        int err = 0;
2690
2691        prepare_fn_table(patch);
2692        while (patch) {
2693                if (apply_verbosely)
2694                        say_patch_name(stderr,
2695                                       "Checking patch ", patch, "...\n");
2696                err |= check_patch(patch);
2697                patch = patch->next;
2698        }
2699        return err;
2700}
2701
2702/* This function tries to read the sha1 from the current index */
2703static int get_current_sha1(const char *path, unsigned char *sha1)
2704{
2705        int pos;
2706
2707        if (read_cache() < 0)
2708                return -1;
2709        pos = cache_name_pos(path, strlen(path));
2710        if (pos < 0)
2711                return -1;
2712        hashcpy(sha1, active_cache[pos]->sha1);
2713        return 0;
2714}
2715
2716/* Build an index that contains the just the files needed for a 3way merge */
2717static void build_fake_ancestor(struct patch *list, const char *filename)
2718{
2719        struct patch *patch;
2720        struct index_state result = { NULL };
2721        int fd;
2722
2723        /* Once we start supporting the reverse patch, it may be
2724         * worth showing the new sha1 prefix, but until then...
2725         */
2726        for (patch = list; patch; patch = patch->next) {
2727                const unsigned char *sha1_ptr;
2728                unsigned char sha1[20];
2729                struct cache_entry *ce;
2730                const char *name;
2731
2732                name = patch->old_name ? patch->old_name : patch->new_name;
2733                if (0 < patch->is_new)
2734                        continue;
2735                else if (get_sha1(patch->old_sha1_prefix, sha1))
2736                        /* git diff has no index line for mode/type changes */
2737                        if (!patch->lines_added && !patch->lines_deleted) {
2738                                if (get_current_sha1(patch->new_name, sha1) ||
2739                                    get_current_sha1(patch->old_name, sha1))
2740                                        die("mode change for %s, which is not "
2741                                                "in current HEAD", name);
2742                                sha1_ptr = sha1;
2743                        } else
2744                                die("sha1 information is lacking or useless "
2745                                        "(%s).", name);
2746                else
2747                        sha1_ptr = sha1;
2748
2749                ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
2750                if (!ce)
2751                        die("make_cache_entry failed for path '%s'", name);
2752                if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
2753                        die ("Could not add %s to temporary index", name);
2754        }
2755
2756        fd = open(filename, O_WRONLY | O_CREAT, 0666);
2757        if (fd < 0 || write_index(&result, fd) || close(fd))
2758                die ("Could not write temporary index to %s", filename);
2759
2760        discard_index(&result);
2761}
2762
2763static void stat_patch_list(struct patch *patch)
2764{
2765        int files, adds, dels;
2766
2767        for (files = adds = dels = 0 ; patch ; patch = patch->next) {
2768                files++;
2769                adds += patch->lines_added;
2770                dels += patch->lines_deleted;
2771                show_stats(patch);
2772        }
2773
2774        printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
2775}
2776
2777static void numstat_patch_list(struct patch *patch)
2778{
2779        for ( ; patch; patch = patch->next) {
2780                const char *name;
2781                name = patch->new_name ? patch->new_name : patch->old_name;
2782                if (patch->is_binary)
2783                        printf("-\t-\t");
2784                else
2785                        printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
2786                write_name_quoted(name, stdout, line_termination);
2787        }
2788}
2789
2790static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
2791{
2792        if (mode)
2793                printf(" %s mode %06o %s\n", newdelete, mode, name);
2794        else
2795                printf(" %s %s\n", newdelete, name);
2796}
2797
2798static void show_mode_change(struct patch *p, int show_name)
2799{
2800        if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
2801                if (show_name)
2802                        printf(" mode change %06o => %06o %s\n",
2803                               p->old_mode, p->new_mode, p->new_name);
2804                else
2805                        printf(" mode change %06o => %06o\n",
2806                               p->old_mode, p->new_mode);
2807        }
2808}
2809
2810static void show_rename_copy(struct patch *p)
2811{
2812        const char *renamecopy = p->is_rename ? "rename" : "copy";
2813        const char *old, *new;
2814
2815        /* Find common prefix */
2816        old = p->old_name;
2817        new = p->new_name;
2818        while (1) {
2819                const char *slash_old, *slash_new;
2820                slash_old = strchr(old, '/');
2821                slash_new = strchr(new, '/');
2822                if (!slash_old ||
2823                    !slash_new ||
2824                    slash_old - old != slash_new - new ||
2825                    memcmp(old, new, slash_new - new))
2826                        break;
2827                old = slash_old + 1;
2828                new = slash_new + 1;
2829        }
2830        /* p->old_name thru old is the common prefix, and old and new
2831         * through the end of names are renames
2832         */
2833        if (old != p->old_name)
2834                printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2835                       (int)(old - p->old_name), p->old_name,
2836                       old, new, p->score);
2837        else
2838                printf(" %s %s => %s (%d%%)\n", renamecopy,
2839                       p->old_name, p->new_name, p->score);
2840        show_mode_change(p, 0);
2841}
2842
2843static void summary_patch_list(struct patch *patch)
2844{
2845        struct patch *p;
2846
2847        for (p = patch; p; p = p->next) {
2848                if (p->is_new)
2849                        show_file_mode_name("create", p->new_mode, p->new_name);
2850                else if (p->is_delete)
2851                        show_file_mode_name("delete", p->old_mode, p->old_name);
2852                else {
2853                        if (p->is_rename || p->is_copy)
2854                                show_rename_copy(p);
2855                        else {
2856                                if (p->score) {
2857                                        printf(" rewrite %s (%d%%)\n",
2858                                               p->new_name, p->score);
2859                                        show_mode_change(p, 0);
2860                                }
2861                                else
2862                                        show_mode_change(p, 1);
2863                        }
2864                }
2865        }
2866}
2867
2868static void patch_stats(struct patch *patch)
2869{
2870        int lines = patch->lines_added + patch->lines_deleted;
2871
2872        if (lines > max_change)
2873                max_change = lines;
2874        if (patch->old_name) {
2875                int len = quote_c_style(patch->old_name, NULL, NULL, 0);
2876                if (!len)
2877                        len = strlen(patch->old_name);
2878                if (len > max_len)
2879                        max_len = len;
2880        }
2881        if (patch->new_name) {
2882                int len = quote_c_style(patch->new_name, NULL, NULL, 0);
2883                if (!len)
2884                        len = strlen(patch->new_name);
2885                if (len > max_len)
2886                        max_len = len;
2887        }
2888}
2889
2890static void remove_file(struct patch *patch, int rmdir_empty)
2891{
2892        if (update_index) {
2893                if (remove_file_from_cache(patch->old_name) < 0)
2894                        die("unable to remove %s from index", patch->old_name);
2895        }
2896        if (!cached) {
2897                if (S_ISGITLINK(patch->old_mode)) {
2898                        if (rmdir(patch->old_name))
2899                                warning("unable to remove submodule %s",
2900                                        patch->old_name);
2901                } else if (!unlink_or_warn(patch->old_name) && rmdir_empty) {
2902                        remove_path(patch->old_name);
2903                }
2904        }
2905}
2906
2907static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
2908{
2909        struct stat st;
2910        struct cache_entry *ce;
2911        int namelen = strlen(path);
2912        unsigned ce_size = cache_entry_size(namelen);
2913
2914        if (!update_index)
2915                return;
2916
2917        ce = xcalloc(1, ce_size);
2918        memcpy(ce->name, path, namelen);
2919        ce->ce_mode = create_ce_mode(mode);
2920        ce->ce_flags = namelen;
2921        if (S_ISGITLINK(mode)) {
2922                const char *s = buf;
2923
2924                if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
2925                        die("corrupt patch for subproject %s", path);
2926        } else {
2927                if (!cached) {
2928                        if (lstat(path, &st) < 0)
2929                                die_errno("unable to stat newly created file '%s'",
2930                                          path);
2931                        fill_stat_cache_info(ce, &st);
2932                }
2933                if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2934                        die("unable to create backing store for newly created file %s", path);
2935        }
2936        if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
2937                die("unable to add cache entry for %s", path);
2938}
2939
2940static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2941{
2942        int fd;
2943        struct strbuf nbuf = STRBUF_INIT;
2944
2945        if (S_ISGITLINK(mode)) {
2946                struct stat st;
2947                if (!lstat(path, &st) && S_ISDIR(st.st_mode))
2948                        return 0;
2949                return mkdir(path, 0777);
2950        }
2951
2952        if (has_symlinks && S_ISLNK(mode))
2953                /* Although buf:size is counted string, it also is NUL
2954                 * terminated.
2955                 */
2956                return symlink(buf, path);
2957
2958        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2959        if (fd < 0)
2960                return -1;
2961
2962        if (convert_to_working_tree(path, buf, size, &nbuf)) {
2963                size = nbuf.len;
2964                buf  = nbuf.buf;
2965        }
2966        write_or_die(fd, buf, size);
2967        strbuf_release(&nbuf);
2968
2969        if (close(fd) < 0)
2970                die_errno("closing file '%s'", path);
2971        return 0;
2972}
2973
2974/*
2975 * We optimistically assume that the directories exist,
2976 * which is true 99% of the time anyway. If they don't,
2977 * we create them and try again.
2978 */
2979static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
2980{
2981        if (cached)
2982                return;
2983        if (!try_create_file(path, mode, buf, size))
2984                return;
2985
2986        if (errno == ENOENT) {
2987                if (safe_create_leading_directories(path))
2988                        return;
2989                if (!try_create_file(path, mode, buf, size))
2990                        return;
2991        }
2992
2993        if (errno == EEXIST || errno == EACCES) {
2994                /* We may be trying to create a file where a directory
2995                 * used to be.
2996                 */
2997                struct stat st;
2998                if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
2999                        errno = EEXIST;
3000        }
3001
3002        if (errno == EEXIST) {
3003                unsigned int nr = getpid();
3004
3005                for (;;) {
3006                        char newpath[PATH_MAX];
3007                        mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
3008                        if (!try_create_file(newpath, mode, buf, size)) {
3009                                if (!rename(newpath, path))
3010                                        return;
3011                                unlink_or_warn(newpath);
3012                                break;
3013                        }
3014                        if (errno != EEXIST)
3015                                break;
3016                        ++nr;
3017                }
3018        }
3019        die_errno("unable to write file '%s' mode %o", path, mode);
3020}
3021
3022static void create_file(struct patch *patch)
3023{
3024        char *path = patch->new_name;
3025        unsigned mode = patch->new_mode;
3026        unsigned long size = patch->resultsize;
3027        char *buf = patch->result;
3028
3029        if (!mode)
3030                mode = S_IFREG | 0644;
3031        create_one_file(path, mode, buf, size);
3032        add_index_file(path, mode, buf, size);
3033}
3034
3035/* phase zero is to remove, phase one is to create */
3036static void write_out_one_result(struct patch *patch, int phase)
3037{
3038        if (patch->is_delete > 0) {
3039                if (phase == 0)
3040                        remove_file(patch, 1);
3041                return;
3042        }
3043        if (patch->is_new > 0 || patch->is_copy) {
3044                if (phase == 1)
3045                        create_file(patch);
3046                return;
3047        }
3048        /*
3049         * Rename or modification boils down to the same
3050         * thing: remove the old, write the new
3051         */
3052        if (phase == 0)
3053                remove_file(patch, patch->is_rename);
3054        if (phase == 1)
3055                create_file(patch);
3056}
3057
3058static int write_out_one_reject(struct patch *patch)
3059{
3060        FILE *rej;
3061        char namebuf[PATH_MAX];
3062        struct fragment *frag;
3063        int cnt = 0;
3064
3065        for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
3066                if (!frag->rejected)
3067                        continue;
3068                cnt++;
3069        }
3070
3071        if (!cnt) {
3072                if (apply_verbosely)
3073                        say_patch_name(stderr,
3074                                       "Applied patch ", patch, " cleanly.\n");
3075                return 0;
3076        }
3077
3078        /* This should not happen, because a removal patch that leaves
3079         * contents are marked "rejected" at the patch level.
3080         */
3081        if (!patch->new_name)
3082                die("internal error");
3083
3084        /* Say this even without --verbose */
3085        say_patch_name(stderr, "Applying patch ", patch, " with");
3086        fprintf(stderr, " %d rejects...\n", cnt);
3087
3088        cnt = strlen(patch->new_name);
3089        if (ARRAY_SIZE(namebuf) <= cnt + 5) {
3090                cnt = ARRAY_SIZE(namebuf) - 5;
3091                warning("truncating .rej filename to %.*s.rej",
3092                        cnt - 1, patch->new_name);
3093        }
3094        memcpy(namebuf, patch->new_name, cnt);
3095        memcpy(namebuf + cnt, ".rej", 5);
3096
3097        rej = fopen(namebuf, "w");
3098        if (!rej)
3099                return error("cannot open %s: %s", namebuf, strerror(errno));
3100
3101        /* Normal git tools never deal with .rej, so do not pretend
3102         * this is a git patch by saying --git nor give extended
3103         * headers.  While at it, maybe please "kompare" that wants
3104         * the trailing TAB and some garbage at the end of line ;-).
3105         */
3106        fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
3107                patch->new_name, patch->new_name);
3108        for (cnt = 1, frag = patch->fragments;
3109             frag;
3110             cnt++, frag = frag->next) {
3111                if (!frag->rejected) {
3112                        fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
3113                        continue;
3114                }
3115                fprintf(stderr, "Rejected hunk #%d.\n", cnt);
3116                fprintf(rej, "%.*s", frag->size, frag->patch);
3117                if (frag->patch[frag->size-1] != '\n')
3118                        fputc('\n', rej);
3119        }
3120        fclose(rej);
3121        return -1;
3122}
3123
3124static int write_out_results(struct patch *list, int skipped_patch)
3125{
3126        int phase;
3127        int errs = 0;
3128        struct patch *l;
3129
3130        if (!list && !skipped_patch)
3131                return error("No changes");
3132
3133        for (phase = 0; phase < 2; phase++) {
3134                l = list;
3135                while (l) {
3136                        if (l->rejected)
3137                                errs = 1;
3138                        else {
3139                                write_out_one_result(l, phase);
3140                                if (phase == 1 && write_out_one_reject(l))
3141                                        errs = 1;
3142                        }
3143                        l = l->next;
3144                }
3145        }
3146        return errs;
3147}
3148
3149static struct lock_file lock_file;
3150
3151static struct string_list limit_by_name;
3152static int has_include;
3153static void add_name_limit(const char *name, int exclude)
3154{
3155        struct string_list_item *it;
3156
3157        it = string_list_append(name, &limit_by_name);
3158        it->util = exclude ? NULL : (void *) 1;
3159}
3160
3161static int use_patch(struct patch *p)
3162{
3163        const char *pathname = p->new_name ? p->new_name : p->old_name;
3164        int i;
3165
3166        /* Paths outside are not touched regardless of "--include" */
3167        if (0 < prefix_length) {
3168                int pathlen = strlen(pathname);
3169                if (pathlen <= prefix_length ||
3170                    memcmp(prefix, pathname, prefix_length))
3171                        return 0;
3172        }
3173
3174        /* See if it matches any of exclude/include rule */
3175        for (i = 0; i < limit_by_name.nr; i++) {
3176                struct string_list_item *it = &limit_by_name.items[i];
3177                if (!fnmatch(it->string, pathname, 0))
3178                        return (it->util != NULL);
3179        }
3180
3181        /*
3182         * If we had any include, a path that does not match any rule is
3183         * not used.  Otherwise, we saw bunch of exclude rules (or none)
3184         * and such a path is used.
3185         */
3186        return !has_include;
3187}
3188
3189
3190static void prefix_one(char **name)
3191{
3192        char *old_name = *name;
3193        if (!old_name)
3194                return;
3195        *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
3196        free(old_name);
3197}
3198
3199static void prefix_patches(struct patch *p)
3200{
3201        if (!prefix || p->is_toplevel_relative)
3202                return;
3203        for ( ; p; p = p->next) {
3204                if (p->new_name == p->old_name) {
3205                        char *prefixed = p->new_name;
3206                        prefix_one(&prefixed);
3207                        p->new_name = p->old_name = prefixed;
3208                }
3209                else {
3210                        prefix_one(&p->new_name);
3211                        prefix_one(&p->old_name);
3212                }
3213        }
3214}
3215
3216#define INACCURATE_EOF  (1<<0)
3217#define RECOUNT         (1<<1)
3218
3219static int apply_patch(int fd, const char *filename, int options)
3220{
3221        size_t offset;
3222        struct strbuf buf = STRBUF_INIT;
3223        struct patch *list = NULL, **listp = &list;
3224        int skipped_patch = 0;
3225
3226        /* FIXME - memory leak when using multiple patch files as inputs */
3227        memset(&fn_table, 0, sizeof(struct string_list));
3228        patch_input_file = filename;
3229        read_patch_file(&buf, fd);
3230        offset = 0;
3231        while (offset < buf.len) {
3232                struct patch *patch;
3233                int nr;
3234
3235                patch = xcalloc(1, sizeof(*patch));
3236                patch->inaccurate_eof = !!(options & INACCURATE_EOF);
3237                patch->recount =  !!(options & RECOUNT);
3238                nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
3239                if (nr < 0)
3240                        break;
3241                if (apply_in_reverse)
3242                        reverse_patches(patch);
3243                if (prefix)
3244                        prefix_patches(patch);
3245                if (use_patch(patch)) {
3246                        patch_stats(patch);
3247                        *listp = patch;
3248                        listp = &patch->next;
3249                }
3250                else {
3251                        /* perhaps free it a bit better? */
3252                        free(patch);
3253                        skipped_patch++;
3254                }
3255                offset += nr;
3256        }
3257
3258        if (whitespace_error && (ws_error_action == die_on_ws_error))
3259                apply = 0;
3260
3261        update_index = check_index && apply;
3262        if (update_index && newfd < 0)
3263                newfd = hold_locked_index(&lock_file, 1);
3264
3265        if (check_index) {
3266                if (read_cache() < 0)
3267                        die("unable to read index file");
3268        }
3269
3270        if ((check || apply) &&
3271            check_patch_list(list) < 0 &&
3272            !apply_with_reject)
3273                exit(1);
3274
3275        if (apply && write_out_results(list, skipped_patch))
3276                exit(1);
3277
3278        if (fake_ancestor)
3279                build_fake_ancestor(list, fake_ancestor);
3280
3281        if (diffstat)
3282                stat_patch_list(list);
3283
3284        if (numstat)
3285                numstat_patch_list(list);
3286
3287        if (summary)
3288                summary_patch_list(list);
3289
3290        strbuf_release(&buf);
3291        return 0;
3292}
3293
3294static int git_apply_config(const char *var, const char *value, void *cb)
3295{
3296        if (!strcmp(var, "apply.whitespace"))
3297                return git_config_string(&apply_default_whitespace, var, value);
3298        return git_default_config(var, value, cb);
3299}
3300
3301static int option_parse_exclude(const struct option *opt,
3302                                const char *arg, int unset)
3303{
3304        add_name_limit(arg, 1);
3305        return 0;
3306}
3307
3308static int option_parse_include(const struct option *opt,
3309                                const char *arg, int unset)
3310{
3311        add_name_limit(arg, 0);
3312        has_include = 1;
3313        return 0;
3314}
3315
3316static int option_parse_p(const struct option *opt,
3317                          const char *arg, int unset)
3318{
3319        p_value = atoi(arg);
3320        p_value_known = 1;
3321        return 0;
3322}
3323
3324static int option_parse_z(const struct option *opt,
3325                          const char *arg, int unset)
3326{
3327        if (unset)
3328                line_termination = '\n';
3329        else
3330                line_termination = 0;
3331        return 0;
3332}
3333
3334static int option_parse_whitespace(const struct option *opt,
3335                                   const char *arg, int unset)
3336{
3337        const char **whitespace_option = opt->value;
3338
3339        *whitespace_option = arg;
3340        parse_whitespace_option(arg);
3341        return 0;
3342}
3343
3344static int option_parse_directory(const struct option *opt,
3345                                  const char *arg, int unset)
3346{
3347        root_len = strlen(arg);
3348        if (root_len && arg[root_len - 1] != '/') {
3349                char *new_root;
3350                root = new_root = xmalloc(root_len + 2);
3351                strcpy(new_root, arg);
3352                strcpy(new_root + root_len++, "/");
3353        } else
3354                root = arg;
3355        return 0;
3356}
3357
3358int cmd_apply(int argc, const char **argv, const char *unused_prefix)
3359{
3360        int i;
3361        int errs = 0;
3362        int is_not_gitdir;
3363        int binary;
3364        int force_apply = 0;
3365
3366        const char *whitespace_option = NULL;
3367
3368        struct option builtin_apply_options[] = {
3369                { OPTION_CALLBACK, 0, "exclude", NULL, "path",
3370                        "don't apply changes matching the given path",
3371                        0, option_parse_exclude },
3372                { OPTION_CALLBACK, 0, "include", NULL, "path",
3373                        "apply changes matching the given path",
3374                        0, option_parse_include },
3375                { OPTION_CALLBACK, 'p', NULL, NULL, "num",
3376                        "remove <num> leading slashes from traditional diff paths",
3377                        0, option_parse_p },
3378                OPT_BOOLEAN(0, "no-add", &no_add,
3379                        "ignore additions made by the patch"),
3380                OPT_BOOLEAN(0, "stat", &diffstat,
3381                        "instead of applying the patch, output diffstat for the input"),
3382                { OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
3383                  NULL, "old option, now no-op",
3384                  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3385                { OPTION_BOOLEAN, 0, "binary", &binary,
3386                  NULL, "old option, now no-op",
3387                  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3388                OPT_BOOLEAN(0, "numstat", &numstat,
3389                        "shows number of added and deleted lines in decimal notation"),
3390                OPT_BOOLEAN(0, "summary", &summary,
3391                        "instead of applying the patch, output a summary for the input"),
3392                OPT_BOOLEAN(0, "check", &check,
3393                        "instead of applying the patch, see if the patch is applicable"),
3394                OPT_BOOLEAN(0, "index", &check_index,
3395                        "make sure the patch is applicable to the current index"),
3396                OPT_BOOLEAN(0, "cached", &cached,
3397                        "apply a patch without touching the working tree"),
3398                OPT_BOOLEAN(0, "apply", &force_apply,
3399                        "also apply the patch (use with --stat/--summary/--check)"),
3400                OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
3401                        "build a temporary index based on embedded index information"),
3402                { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
3403                        "paths are separated with NUL character",
3404                        PARSE_OPT_NOARG, option_parse_z },
3405                OPT_INTEGER('C', NULL, &p_context,
3406                                "ensure at least <n> lines of context match"),
3407                { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action",
3408                        "detect new or modified lines that have whitespace errors",
3409                        0, option_parse_whitespace },
3410                OPT_BOOLEAN('R', "reverse", &apply_in_reverse,
3411                        "apply the patch in reverse"),
3412                OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero,
3413                        "don't expect at least one line of context"),
3414                OPT_BOOLEAN(0, "reject", &apply_with_reject,
3415                        "leave the rejected hunks in corresponding *.rej files"),
3416                OPT__VERBOSE(&apply_verbosely),
3417                OPT_BIT(0, "inaccurate-eof", &options,
3418                        "tolerate incorrectly detected missing new-line at the end of file",
3419                        INACCURATE_EOF),
3420                OPT_BIT(0, "recount", &options,
3421                        "do not trust the line counts in the hunk headers",
3422                        RECOUNT),
3423                { OPTION_CALLBACK, 0, "directory", NULL, "root",
3424                        "prepend <root> to all filenames",
3425                        0, option_parse_directory },
3426                OPT_END()
3427        };
3428
3429        prefix = setup_git_directory_gently(&is_not_gitdir);
3430        prefix_length = prefix ? strlen(prefix) : 0;
3431        git_config(git_apply_config, NULL);
3432        if (apply_default_whitespace)
3433                parse_whitespace_option(apply_default_whitespace);
3434
3435        argc = parse_options(argc, argv, prefix, builtin_apply_options,
3436                        apply_usage, 0);
3437
3438        if (apply_with_reject)
3439                apply = apply_verbosely = 1;
3440        if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
3441                apply = 0;
3442        if (check_index && is_not_gitdir)
3443                die("--index outside a repository");
3444        if (cached) {
3445                if (is_not_gitdir)
3446                        die("--cached outside a repository");
3447                check_index = 1;
3448        }
3449        for (i = 0; i < argc; i++) {
3450                const char *arg = argv[i];
3451                int fd;
3452
3453                if (!strcmp(arg, "-")) {
3454                        errs |= apply_patch(0, "<stdin>", options);
3455                        read_stdin = 0;
3456                        continue;
3457                } else if (0 < prefix_length)
3458                        arg = prefix_filename(prefix, prefix_length, arg);
3459
3460                fd = open(arg, O_RDONLY);
3461                if (fd < 0)
3462                        die_errno("can't open patch '%s'", arg);
3463                read_stdin = 0;
3464                set_default_whitespace_mode(whitespace_option);
3465                errs |= apply_patch(fd, arg, options);
3466                close(fd);
3467        }
3468        set_default_whitespace_mode(whitespace_option);
3469        if (read_stdin)
3470                errs |= apply_patch(0, "<stdin>", options);
3471        if (whitespace_error) {
3472                if (squelch_whitespace_errors &&
3473                    squelch_whitespace_errors < whitespace_error) {
3474                        int squelched =
3475                                whitespace_error - squelch_whitespace_errors;
3476                        warning("squelched %d "
3477                                "whitespace error%s",
3478                                squelched,
3479                                squelched == 1 ? "" : "s");
3480                }
3481                if (ws_error_action == die_on_ws_error)
3482                        die("%d line%s add%s whitespace errors.",
3483                            whitespace_error,
3484                            whitespace_error == 1 ? "" : "s",
3485                            whitespace_error == 1 ? "s" : "");
3486                if (applied_after_fixing_ws && apply)
3487                        warning("%d line%s applied after"
3488                                " fixing whitespace errors.",
3489                                applied_after_fixing_ws,
3490                                applied_after_fixing_ws == 1 ? "" : "s");
3491                else if (whitespace_error)
3492                        warning("%d line%s add%s whitespace errors.",
3493                                whitespace_error,
3494                                whitespace_error == 1 ? "" : "s",
3495                                whitespace_error == 1 ? "s" : "");
3496        }
3497
3498        if (update_index) {
3499                if (write_cache(newfd, active_cache, active_nr) ||
3500                    commit_locked_index(&lock_file))
3501                        die("Unable to write new index file");
3502        }
3503
3504        return !!errs;
3505}