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