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