12f00e38db2ff443d8856e35ec5476b22cb8ec6a
   1/*
   2 * apply.c
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 *
   6 * This applies patches on top of some (arbitrary) version of the SCM.
   7 *
   8 */
   9#include "cache.h"
  10#include "cache-tree.h"
  11#include "quote.h"
  12#include "blob.h"
  13#include "delta.h"
  14#include "builtin.h"
  15
  16/*
  17 *  --check turns on checking that the working tree matches the
  18 *    files that are being modified, but doesn't apply the patch
  19 *  --stat does just a diffstat, and doesn't actually apply
  20 *  --numstat does numeric diffstat, and doesn't actually apply
  21 *  --index-info shows the old and new index info for paths if available.
  22 *  --index updates the cache as well.
  23 *  --cached updates only the cache without ever touching the working tree.
  24 */
  25static const char *prefix;
  26static int prefix_length = -1;
  27static int newfd = -1;
  28
  29static int unidiff_zero;
  30static int p_value = 1;
  31static int check_index;
  32static int write_index;
  33static int cached;
  34static int diffstat;
  35static int numstat;
  36static int summary;
  37static int check;
  38static int apply = 1;
  39static int apply_in_reverse;
  40static int apply_with_reject;
  41static int apply_verbosely;
  42static int no_add;
  43static int show_index_info;
  44static int line_termination = '\n';
  45static unsigned long p_context = ULONG_MAX;
  46static const char apply_usage[] =
  47"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [--verbose] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
  48
  49static enum whitespace_eol {
  50        nowarn_whitespace,
  51        warn_on_whitespace,
  52        error_on_whitespace,
  53        strip_whitespace,
  54} new_whitespace = warn_on_whitespace;
  55static int whitespace_error;
  56static int squelch_whitespace_errors = 5;
  57static int applied_after_stripping;
  58static const char *patch_input_file;
  59
  60static void parse_whitespace_option(const char *option)
  61{
  62        if (!option) {
  63                new_whitespace = warn_on_whitespace;
  64                return;
  65        }
  66        if (!strcmp(option, "warn")) {
  67                new_whitespace = warn_on_whitespace;
  68                return;
  69        }
  70        if (!strcmp(option, "nowarn")) {
  71                new_whitespace = nowarn_whitespace;
  72                return;
  73        }
  74        if (!strcmp(option, "error")) {
  75                new_whitespace = error_on_whitespace;
  76                return;
  77        }
  78        if (!strcmp(option, "error-all")) {
  79                new_whitespace = error_on_whitespace;
  80                squelch_whitespace_errors = 0;
  81                return;
  82        }
  83        if (!strcmp(option, "strip")) {
  84                new_whitespace = strip_whitespace;
  85                return;
  86        }
  87        die("unrecognized whitespace option '%s'", option);
  88}
  89
  90static void set_default_whitespace_mode(const char *whitespace_option)
  91{
  92        if (!whitespace_option && !apply_default_whitespace) {
  93                new_whitespace = (apply
  94                                  ? warn_on_whitespace
  95                                  : nowarn_whitespace);
  96        }
  97}
  98
  99/*
 100 * For "diff-stat" like behaviour, we keep track of the biggest change
 101 * we've seen, and the longest filename. That allows us to do simple
 102 * scaling.
 103 */
 104static int max_change, max_len;
 105
 106/*
 107 * Various "current state", notably line numbers and what
 108 * file (and how) we're patching right now.. The "is_xxxx"
 109 * things are flags, where -1 means "don't know yet".
 110 */
 111static int linenr = 1;
 112
 113/*
 114 * This represents one "hunk" from a patch, starting with
 115 * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
 116 * patch text is pointed at by patch, and its byte length
 117 * is stored in size.  leading and trailing are the number
 118 * of context lines.
 119 */
 120struct fragment {
 121        unsigned long leading, trailing;
 122        unsigned long oldpos, oldlines;
 123        unsigned long newpos, newlines;
 124        const char *patch;
 125        int size;
 126        int rejected;
 127        struct fragment *next;
 128};
 129
 130/*
 131 * When dealing with a binary patch, we reuse "leading" field
 132 * to store the type of the binary hunk, either deflated "delta"
 133 * or deflated "literal".
 134 */
 135#define binary_patch_method leading
 136#define BINARY_DELTA_DEFLATED   1
 137#define BINARY_LITERAL_DEFLATED 2
 138
 139struct patch {
 140        char *new_name, *old_name, *def_name;
 141        unsigned int old_mode, new_mode;
 142        int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
 143        int rejected;
 144        unsigned long deflate_origlen;
 145        int lines_added, lines_deleted;
 146        int score;
 147        unsigned int is_toplevel_relative:1;
 148        unsigned int inaccurate_eof:1;
 149        unsigned int is_binary:1;
 150        unsigned int is_copy:1;
 151        unsigned int is_rename:1;
 152        struct fragment *fragments;
 153        char *result;
 154        unsigned long resultsize;
 155        char old_sha1_prefix[41];
 156        char new_sha1_prefix[41];
 157        struct patch *next;
 158};
 159
 160static void say_patch_name(FILE *output, const char *pre, struct patch *patch, const char *post)
 161{
 162        fputs(pre, output);
 163        if (patch->old_name && patch->new_name &&
 164            strcmp(patch->old_name, patch->new_name)) {
 165                write_name_quoted(NULL, 0, patch->old_name, 1, output);
 166                fputs(" => ", output);
 167                write_name_quoted(NULL, 0, patch->new_name, 1, output);
 168        }
 169        else {
 170                const char *n = patch->new_name;
 171                if (!n)
 172                        n = patch->old_name;
 173                write_name_quoted(NULL, 0, n, 1, output);
 174        }
 175        fputs(post, output);
 176}
 177
 178#define CHUNKSIZE (8192)
 179#define SLOP (16)
 180
 181static void *read_patch_file(int fd, unsigned long *sizep)
 182{
 183        unsigned long size = 0, alloc = CHUNKSIZE;
 184        void *buffer = xmalloc(alloc);
 185
 186        for (;;) {
 187                int nr = alloc - size;
 188                if (nr < 1024) {
 189                        alloc += CHUNKSIZE;
 190                        buffer = xrealloc(buffer, alloc);
 191                        nr = alloc - size;
 192                }
 193                nr = xread(fd, (char *) buffer + size, nr);
 194                if (!nr)
 195                        break;
 196                if (nr < 0)
 197                        die("git-apply: read returned %s", strerror(errno));
 198                size += nr;
 199        }
 200        *sizep = size;
 201
 202        /*
 203         * Make sure that we have some slop in the buffer
 204         * so that we can do speculative "memcmp" etc, and
 205         * see to it that it is NUL-filled.
 206         */
 207        if (alloc < size + SLOP)
 208                buffer = xrealloc(buffer, size + SLOP);
 209        memset((char *) buffer + size, 0, SLOP);
 210        return buffer;
 211}
 212
 213static unsigned long linelen(const char *buffer, unsigned long size)
 214{
 215        unsigned long len = 0;
 216        while (size--) {
 217                len++;
 218                if (*buffer++ == '\n')
 219                        break;
 220        }
 221        return len;
 222}
 223
 224static int is_dev_null(const char *str)
 225{
 226        return !memcmp("/dev/null", str, 9) && isspace(str[9]);
 227}
 228
 229#define TERM_SPACE      1
 230#define TERM_TAB        2
 231
 232static int name_terminate(const char *name, int namelen, int c, int terminate)
 233{
 234        if (c == ' ' && !(terminate & TERM_SPACE))
 235                return 0;
 236        if (c == '\t' && !(terminate & TERM_TAB))
 237                return 0;
 238
 239        return 1;
 240}
 241
 242static char *find_name(const char *line, char *def, int p_value, int terminate)
 243{
 244        int len;
 245        const char *start = line;
 246        char *name;
 247
 248        if (*line == '"') {
 249                /* Proposed "new-style" GNU patch/diff format; see
 250                 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
 251                 */
 252                name = unquote_c_style(line, NULL);
 253                if (name) {
 254                        char *cp = name;
 255                        while (p_value) {
 256                                cp = strchr(name, '/');
 257                                if (!cp)
 258                                        break;
 259                                cp++;
 260                                p_value--;
 261                        }
 262                        if (cp) {
 263                                /* name can later be freed, so we need
 264                                 * to memmove, not just return cp
 265                                 */
 266                                memmove(name, cp, strlen(cp) + 1);
 267                                free(def);
 268                                return name;
 269                        }
 270                        else {
 271                                free(name);
 272                                name = NULL;
 273                        }
 274                }
 275        }
 276
 277        for (;;) {
 278                char c = *line;
 279
 280                if (isspace(c)) {
 281                        if (c == '\n')
 282                                break;
 283                        if (name_terminate(start, line-start, c, terminate))
 284                                break;
 285                }
 286                line++;
 287                if (c == '/' && !--p_value)
 288                        start = line;
 289        }
 290        if (!start)
 291                return def;
 292        len = line - start;
 293        if (!len)
 294                return def;
 295
 296        /*
 297         * Generally we prefer the shorter name, especially
 298         * if the other one is just a variation of that with
 299         * something else tacked on to the end (ie "file.orig"
 300         * or "file~").
 301         */
 302        if (def) {
 303                int deflen = strlen(def);
 304                if (deflen < len && !strncmp(start, def, deflen))
 305                        return def;
 306        }
 307
 308        name = xmalloc(len + 1);
 309        memcpy(name, start, len);
 310        name[len] = 0;
 311        free(def);
 312        return name;
 313}
 314
 315/*
 316 * Get the name etc info from the --/+++ lines of a traditional patch header
 317 *
 318 * NOTE! This hardcodes "-p1" behaviour in filename detection.
 319 *
 320 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
 321 * files, we can happily check the index for a match, but for creating a
 322 * new file we should try to match whatever "patch" does. I have no idea.
 323 */
 324static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
 325{
 326        char *name;
 327
 328        first += 4;     /* skip "--- " */
 329        second += 4;    /* skip "+++ " */
 330        if (is_dev_null(first)) {
 331                patch->is_new = 1;
 332                patch->is_delete = 0;
 333                name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
 334                patch->new_name = name;
 335        } else if (is_dev_null(second)) {
 336                patch->is_new = 0;
 337                patch->is_delete = 1;
 338                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 339                patch->old_name = name;
 340        } else {
 341                name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
 342                name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
 343                patch->old_name = patch->new_name = name;
 344        }
 345        if (!name)
 346                die("unable to find filename in patch at line %d", linenr);
 347}
 348
 349static int gitdiff_hdrend(const char *line, struct patch *patch)
 350{
 351        return -1;
 352}
 353
 354/*
 355 * We're anal about diff header consistency, to make
 356 * sure that we don't end up having strange ambiguous
 357 * patches floating around.
 358 *
 359 * As a result, gitdiff_{old|new}name() will check
 360 * their names against any previous information, just
 361 * to make sure..
 362 */
 363static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 364{
 365        if (!orig_name && !isnull)
 366                return find_name(line, NULL, 1, TERM_TAB);
 367
 368        if (orig_name) {
 369                int len;
 370                const char *name;
 371                char *another;
 372                name = orig_name;
 373                len = strlen(name);
 374                if (isnull)
 375                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
 376                another = find_name(line, NULL, 1, TERM_TAB);
 377                if (!another || memcmp(another, name, len))
 378                        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
 379                free(another);
 380                return orig_name;
 381        }
 382        else {
 383                /* expect "/dev/null" */
 384                if (memcmp("/dev/null", line, 9) || line[9] != '\n')
 385                        die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
 386                return NULL;
 387        }
 388}
 389
 390static int gitdiff_oldname(const char *line, struct patch *patch)
 391{
 392        patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
 393        return 0;
 394}
 395
 396static int gitdiff_newname(const char *line, struct patch *patch)
 397{
 398        patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
 399        return 0;
 400}
 401
 402static int gitdiff_oldmode(const char *line, struct patch *patch)
 403{
 404        patch->old_mode = strtoul(line, NULL, 8);
 405        return 0;
 406}
 407
 408static int gitdiff_newmode(const char *line, struct patch *patch)
 409{
 410        patch->new_mode = strtoul(line, NULL, 8);
 411        return 0;
 412}
 413
 414static int gitdiff_delete(const char *line, struct patch *patch)
 415{
 416        patch->is_delete = 1;
 417        patch->old_name = patch->def_name;
 418        return gitdiff_oldmode(line, patch);
 419}
 420
 421static int gitdiff_newfile(const char *line, struct patch *patch)
 422{
 423        patch->is_new = 1;
 424        patch->new_name = patch->def_name;
 425        return gitdiff_newmode(line, patch);
 426}
 427
 428static int gitdiff_copysrc(const char *line, struct patch *patch)
 429{
 430        patch->is_copy = 1;
 431        patch->old_name = find_name(line, NULL, 0, 0);
 432        return 0;
 433}
 434
 435static int gitdiff_copydst(const char *line, struct patch *patch)
 436{
 437        patch->is_copy = 1;
 438        patch->new_name = find_name(line, NULL, 0, 0);
 439        return 0;
 440}
 441
 442static int gitdiff_renamesrc(const char *line, struct patch *patch)
 443{
 444        patch->is_rename = 1;
 445        patch->old_name = find_name(line, NULL, 0, 0);
 446        return 0;
 447}
 448
 449static int gitdiff_renamedst(const char *line, struct patch *patch)
 450{
 451        patch->is_rename = 1;
 452        patch->new_name = find_name(line, NULL, 0, 0);
 453        return 0;
 454}
 455
 456static int gitdiff_similarity(const char *line, struct patch *patch)
 457{
 458        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 459                patch->score = 0;
 460        return 0;
 461}
 462
 463static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 464{
 465        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
 466                patch->score = 0;
 467        return 0;
 468}
 469
 470static int gitdiff_index(const char *line, struct patch *patch)
 471{
 472        /* index line is N hexadecimal, "..", N hexadecimal,
 473         * and optional space with octal mode.
 474         */
 475        const char *ptr, *eol;
 476        int len;
 477
 478        ptr = strchr(line, '.');
 479        if (!ptr || ptr[1] != '.' || 40 < ptr - line)
 480                return 0;
 481        len = ptr - line;
 482        memcpy(patch->old_sha1_prefix, line, len);
 483        patch->old_sha1_prefix[len] = 0;
 484
 485        line = ptr + 2;
 486        ptr = strchr(line, ' ');
 487        eol = strchr(line, '\n');
 488
 489        if (!ptr || eol < ptr)
 490                ptr = eol;
 491        len = ptr - line;
 492
 493        if (40 < len)
 494                return 0;
 495        memcpy(patch->new_sha1_prefix, line, len);
 496        patch->new_sha1_prefix[len] = 0;
 497        if (*ptr == ' ')
 498                patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
 499        return 0;
 500}
 501
 502/*
 503 * This is normal for a diff that doesn't change anything: we'll fall through
 504 * into the next diff. Tell the parser to break out.
 505 */
 506static int gitdiff_unrecognized(const char *line, struct patch *patch)
 507{
 508        return -1;
 509}
 510
 511static const char *stop_at_slash(const char *line, int llen)
 512{
 513        int i;
 514
 515        for (i = 0; i < llen; i++) {
 516                int ch = line[i];
 517                if (ch == '/')
 518                        return line + i;
 519        }
 520        return NULL;
 521}
 522
 523/* This is to extract the same name that appears on "diff --git"
 524 * line.  We do not find and return anything if it is a rename
 525 * patch, and it is OK because we will find the name elsewhere.
 526 * We need to reliably find name only when it is mode-change only,
 527 * creation or deletion of an empty file.  In any of these cases,
 528 * both sides are the same name under a/ and b/ respectively.
 529 */
 530static char *git_header_name(char *line, int llen)
 531{
 532        int len;
 533        const char *name;
 534        const char *second = NULL;
 535
 536        line += strlen("diff --git ");
 537        llen -= strlen("diff --git ");
 538
 539        if (*line == '"') {
 540                const char *cp;
 541                char *first = unquote_c_style(line, &second);
 542                if (!first)
 543                        return NULL;
 544
 545                /* advance to the first slash */
 546                cp = stop_at_slash(first, strlen(first));
 547                if (!cp || cp == first) {
 548                        /* we do not accept absolute paths */
 549                free_first_and_fail:
 550                        free(first);
 551                        return NULL;
 552                }
 553                len = strlen(cp+1);
 554                memmove(first, cp+1, len+1); /* including NUL */
 555
 556                /* second points at one past closing dq of name.
 557                 * find the second name.
 558                 */
 559                while ((second < line + llen) && isspace(*second))
 560                        second++;
 561
 562                if (line + llen <= second)
 563                        goto free_first_and_fail;
 564                if (*second == '"') {
 565                        char *sp = unquote_c_style(second, NULL);
 566                        if (!sp)
 567                                goto free_first_and_fail;
 568                        cp = stop_at_slash(sp, strlen(sp));
 569                        if (!cp || cp == sp) {
 570                        free_both_and_fail:
 571                                free(sp);
 572                                goto free_first_and_fail;
 573                        }
 574                        /* They must match, otherwise ignore */
 575                        if (strcmp(cp+1, first))
 576                                goto free_both_and_fail;
 577                        free(sp);
 578                        return first;
 579                }
 580
 581                /* unquoted second */
 582                cp = stop_at_slash(second, line + llen - second);
 583                if (!cp || cp == second)
 584                        goto free_first_and_fail;
 585                cp++;
 586                if (line + llen - cp != len + 1 ||
 587                    memcmp(first, cp, len))
 588                        goto free_first_and_fail;
 589                return first;
 590        }
 591
 592        /* unquoted first name */
 593        name = stop_at_slash(line, llen);
 594        if (!name || name == line)
 595                return NULL;
 596
 597        name++;
 598
 599        /* since the first name is unquoted, a dq if exists must be
 600         * the beginning of the second name.
 601         */
 602        for (second = name; second < line + llen; second++) {
 603                if (*second == '"') {
 604                        const char *cp = second;
 605                        const char *np;
 606                        char *sp = unquote_c_style(second, NULL);
 607
 608                        if (!sp)
 609                                return NULL;
 610                        np = stop_at_slash(sp, strlen(sp));
 611                        if (!np || np == sp) {
 612                        free_second_and_fail:
 613                                free(sp);
 614                                return NULL;
 615                        }
 616                        np++;
 617                        len = strlen(np);
 618                        if (len < cp - name &&
 619                            !strncmp(np, name, len) &&
 620                            isspace(name[len])) {
 621                                /* Good */
 622                                memmove(sp, np, len + 1);
 623                                return sp;
 624                        }
 625                        goto free_second_and_fail;
 626                }
 627        }
 628
 629        /*
 630         * Accept a name only if it shows up twice, exactly the same
 631         * form.
 632         */
 633        for (len = 0 ; ; len++) {
 634                switch (name[len]) {
 635                default:
 636                        continue;
 637                case '\n':
 638                        return NULL;
 639                case '\t': case ' ':
 640                        second = name+len;
 641                        for (;;) {
 642                                char c = *second++;
 643                                if (c == '\n')
 644                                        return NULL;
 645                                if (c == '/')
 646                                        break;
 647                        }
 648                        if (second[len] == '\n' && !memcmp(name, second, len)) {
 649                                char *ret = xmalloc(len + 1);
 650                                memcpy(ret, name, len);
 651                                ret[len] = 0;
 652                                return ret;
 653                        }
 654                }
 655        }
 656        return NULL;
 657}
 658
 659/* Verify that we recognize the lines following a git header */
 660static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
 661{
 662        unsigned long offset;
 663
 664        /* A git diff has explicit new/delete information, so we don't guess */
 665        patch->is_new = 0;
 666        patch->is_delete = 0;
 667
 668        /*
 669         * Some things may not have the old name in the
 670         * rest of the headers anywhere (pure mode changes,
 671         * or removing or adding empty files), so we get
 672         * the default name from the header.
 673         */
 674        patch->def_name = git_header_name(line, len);
 675
 676        line += len;
 677        size -= len;
 678        linenr++;
 679        for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 680                static const struct opentry {
 681                        const char *str;
 682                        int (*fn)(const char *, struct patch *);
 683                } optable[] = {
 684                        { "@@ -", gitdiff_hdrend },
 685                        { "--- ", gitdiff_oldname },
 686                        { "+++ ", gitdiff_newname },
 687                        { "old mode ", gitdiff_oldmode },
 688                        { "new mode ", gitdiff_newmode },
 689                        { "deleted file mode ", gitdiff_delete },
 690                        { "new file mode ", gitdiff_newfile },
 691                        { "copy from ", gitdiff_copysrc },
 692                        { "copy to ", gitdiff_copydst },
 693                        { "rename old ", gitdiff_renamesrc },
 694                        { "rename new ", gitdiff_renamedst },
 695                        { "rename from ", gitdiff_renamesrc },
 696                        { "rename to ", gitdiff_renamedst },
 697                        { "similarity index ", gitdiff_similarity },
 698                        { "dissimilarity index ", gitdiff_dissimilarity },
 699                        { "index ", gitdiff_index },
 700                        { "", gitdiff_unrecognized },
 701                };
 702                int i;
 703
 704                len = linelen(line, size);
 705                if (!len || line[len-1] != '\n')
 706                        break;
 707                for (i = 0; i < ARRAY_SIZE(optable); i++) {
 708                        const struct opentry *p = optable + i;
 709                        int oplen = strlen(p->str);
 710                        if (len < oplen || memcmp(p->str, line, oplen))
 711                                continue;
 712                        if (p->fn(line + oplen, patch) < 0)
 713                                return offset;
 714                        break;
 715                }
 716        }
 717
 718        return offset;
 719}
 720
 721static int parse_num(const char *line, unsigned long *p)
 722{
 723        char *ptr;
 724
 725        if (!isdigit(*line))
 726                return 0;
 727        *p = strtoul(line, &ptr, 10);
 728        return ptr - line;
 729}
 730
 731static int parse_range(const char *line, int len, int offset, const char *expect,
 732                        unsigned long *p1, unsigned long *p2)
 733{
 734        int digits, ex;
 735
 736        if (offset < 0 || offset >= len)
 737                return -1;
 738        line += offset;
 739        len -= offset;
 740
 741        digits = parse_num(line, p1);
 742        if (!digits)
 743                return -1;
 744
 745        offset += digits;
 746        line += digits;
 747        len -= digits;
 748
 749        *p2 = 1;
 750        if (*line == ',') {
 751                digits = parse_num(line+1, p2);
 752                if (!digits)
 753                        return -1;
 754
 755                offset += digits+1;
 756                line += digits+1;
 757                len -= digits+1;
 758        }
 759
 760        ex = strlen(expect);
 761        if (ex > len)
 762                return -1;
 763        if (memcmp(line, expect, ex))
 764                return -1;
 765
 766        return offset + ex;
 767}
 768
 769/*
 770 * Parse a unified diff fragment header of the
 771 * form "@@ -a,b +c,d @@"
 772 */
 773static int parse_fragment_header(char *line, int len, struct fragment *fragment)
 774{
 775        int offset;
 776
 777        if (!len || line[len-1] != '\n')
 778                return -1;
 779
 780        /* Figure out the number of lines in a fragment */
 781        offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
 782        offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
 783
 784        return offset;
 785}
 786
 787static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
 788{
 789        unsigned long offset, len;
 790
 791        patch->is_toplevel_relative = 0;
 792        patch->is_rename = patch->is_copy = 0;
 793        patch->is_new = patch->is_delete = -1;
 794        patch->old_mode = patch->new_mode = 0;
 795        patch->old_name = patch->new_name = NULL;
 796        for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
 797                unsigned long nextlen;
 798
 799                len = linelen(line, size);
 800                if (!len)
 801                        break;
 802
 803                /* Testing this early allows us to take a few shortcuts.. */
 804                if (len < 6)
 805                        continue;
 806
 807                /*
 808                 * Make sure we don't find any unconnected patch fragments.
 809                 * That's a sign that we didn't find a header, and that a
 810                 * patch has become corrupted/broken up.
 811                 */
 812                if (!memcmp("@@ -", line, 4)) {
 813                        struct fragment dummy;
 814                        if (parse_fragment_header(line, len, &dummy) < 0)
 815                                continue;
 816                        die("patch fragment without header at line %d: %.*s",
 817                            linenr, (int)len-1, line);
 818                }
 819
 820                if (size < len + 6)
 821                        break;
 822
 823                /*
 824                 * Git patch? It might not have a real patch, just a rename
 825                 * or mode change, so we handle that specially
 826                 */
 827                if (!memcmp("diff --git ", line, 11)) {
 828                        int git_hdr_len = parse_git_header(line, len, size, patch);
 829                        if (git_hdr_len <= len)
 830                                continue;
 831                        if (!patch->old_name && !patch->new_name) {
 832                                if (!patch->def_name)
 833                                        die("git diff header lacks filename information (line %d)", linenr);
 834                                patch->old_name = patch->new_name = patch->def_name;
 835                        }
 836                        patch->is_toplevel_relative = 1;
 837                        *hdrsize = git_hdr_len;
 838                        return offset;
 839                }
 840
 841                /** --- followed by +++ ? */
 842                if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
 843                        continue;
 844
 845                /*
 846                 * We only accept unified patches, so we want it to
 847                 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
 848                 * minimum
 849                 */
 850                nextlen = linelen(line + len, size - len);
 851                if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
 852                        continue;
 853
 854                /* Ok, we'll consider it a patch */
 855                parse_traditional_patch(line, line+len, patch);
 856                *hdrsize = len + nextlen;
 857                linenr += 2;
 858                return offset;
 859        }
 860        return -1;
 861}
 862
 863static void check_whitespace(const char *line, int len)
 864{
 865        const char *err = "Adds trailing whitespace";
 866        int seen_space = 0;
 867        int i;
 868
 869        /*
 870         * We know len is at least two, since we have a '+' and we
 871         * checked that the last character was a '\n' before calling
 872         * this function.  That is, an addition of an empty line would
 873         * check the '+' here.  Sneaky...
 874         */
 875        if (isspace(line[len-2]))
 876                goto error;
 877
 878        /*
 879         * Make sure that there is no space followed by a tab in
 880         * indentation.
 881         */
 882        err = "Space in indent is followed by a tab";
 883        for (i = 1; i < len; i++) {
 884                if (line[i] == '\t') {
 885                        if (seen_space)
 886                                goto error;
 887                }
 888                else if (line[i] == ' ')
 889                        seen_space = 1;
 890                else
 891                        break;
 892        }
 893        return;
 894
 895 error:
 896        whitespace_error++;
 897        if (squelch_whitespace_errors &&
 898            squelch_whitespace_errors < whitespace_error)
 899                ;
 900        else
 901                fprintf(stderr, "%s.\n%s:%d:%.*s\n",
 902                        err, patch_input_file, linenr, len-2, line+1);
 903}
 904
 905
 906/*
 907 * Parse a unified diff. Note that this really needs to parse each
 908 * fragment separately, since the only way to know the difference
 909 * between a "---" that is part of a patch, and a "---" that starts
 910 * the next patch is to look at the line counts..
 911 */
 912static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
 913{
 914        int added, deleted;
 915        int len = linelen(line, size), offset;
 916        unsigned long oldlines, newlines;
 917        unsigned long leading, trailing;
 918
 919        offset = parse_fragment_header(line, len, fragment);
 920        if (offset < 0)
 921                return -1;
 922        oldlines = fragment->oldlines;
 923        newlines = fragment->newlines;
 924        leading = 0;
 925        trailing = 0;
 926
 927        /* Parse the thing.. */
 928        line += len;
 929        size -= len;
 930        linenr++;
 931        added = deleted = 0;
 932        for (offset = len;
 933             0 < size;
 934             offset += len, size -= len, line += len, linenr++) {
 935                if (!oldlines && !newlines)
 936                        break;
 937                len = linelen(line, size);
 938                if (!len || line[len-1] != '\n')
 939                        return -1;
 940                switch (*line) {
 941                default:
 942                        return -1;
 943                case '\n': /* newer GNU diff, an empty context line */
 944                case ' ':
 945                        oldlines--;
 946                        newlines--;
 947                        if (!deleted && !added)
 948                                leading++;
 949                        trailing++;
 950                        break;
 951                case '-':
 952                        deleted++;
 953                        oldlines--;
 954                        trailing = 0;
 955                        break;
 956                case '+':
 957                        if (new_whitespace != nowarn_whitespace)
 958                                check_whitespace(line, len);
 959                        added++;
 960                        newlines--;
 961                        trailing = 0;
 962                        break;
 963
 964                /* We allow "\ No newline at end of file". Depending
 965                 * on locale settings when the patch was produced we
 966                 * don't know what this line looks like. The only
 967                 * thing we do know is that it begins with "\ ".
 968                 * Checking for 12 is just for sanity check -- any
 969                 * l10n of "\ No newline..." is at least that long.
 970                 */
 971                case '\\':
 972                        if (len < 12 || memcmp(line, "\\ ", 2))
 973                                return -1;
 974                        break;
 975                }
 976        }
 977        if (oldlines || newlines)
 978                return -1;
 979        fragment->leading = leading;
 980        fragment->trailing = trailing;
 981
 982        /* If a fragment ends with an incomplete line, we failed to include
 983         * it in the above loop because we hit oldlines == newlines == 0
 984         * before seeing it.
 985         */
 986        if (12 < size && !memcmp(line, "\\ ", 2))
 987                offset += linelen(line, size);
 988
 989        patch->lines_added += added;
 990        patch->lines_deleted += deleted;
 991
 992        if (0 < patch->is_new && oldlines)
 993                return error("new file depends on old contents");
 994        if (0 < patch->is_delete && newlines)
 995                return error("deleted file still has contents");
 996        return offset;
 997}
 998
 999static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
1000{
1001        unsigned long offset = 0;
1002        unsigned long oldlines = 0, newlines = 0, context = 0;
1003        struct fragment **fragp = &patch->fragments;
1004
1005        while (size > 4 && !memcmp(line, "@@ -", 4)) {
1006                struct fragment *fragment;
1007                int len;
1008
1009                fragment = xcalloc(1, sizeof(*fragment));
1010                len = parse_fragment(line, size, patch, fragment);
1011                if (len <= 0)
1012                        die("corrupt patch at line %d", linenr);
1013                fragment->patch = line;
1014                fragment->size = len;
1015                oldlines += fragment->oldlines;
1016                newlines += fragment->newlines;
1017                context += fragment->leading + fragment->trailing;
1018
1019                *fragp = fragment;
1020                fragp = &fragment->next;
1021
1022                offset += len;
1023                line += len;
1024                size -= len;
1025        }
1026
1027        /*
1028         * If something was removed (i.e. we have old-lines) it cannot
1029         * be creation, and if something was added it cannot be
1030         * deletion.  However, the reverse is not true; --unified=0
1031         * patches that only add are not necessarily creation even
1032         * though they do not have any old lines, and ones that only
1033         * delete are not necessarily deletion.
1034         *
1035         * Unfortunately, a real creation/deletion patch do _not_ have
1036         * any context line by definition, so we cannot safely tell it
1037         * apart with --unified=0 insanity.  At least if the patch has
1038         * more than one hunk it is not creation or deletion.
1039         */
1040        if (patch->is_new < 0 &&
1041            (oldlines || (patch->fragments && patch->fragments->next)))
1042                patch->is_new = 0;
1043        if (patch->is_delete < 0 &&
1044            (newlines || (patch->fragments && patch->fragments->next)))
1045                patch->is_delete = 0;
1046        if (!unidiff_zero || context) {
1047                /* If the user says the patch is not generated with
1048                 * --unified=0, or if we have seen context lines,
1049                 * then not having oldlines means the patch is creation,
1050                 * and not having newlines means the patch is deletion.
1051                 */
1052                if (patch->is_new < 0 && !oldlines) {
1053                        patch->is_new = 1;
1054                        patch->old_name = NULL;
1055                }
1056                if (patch->is_delete < 0 && !newlines) {
1057                        patch->is_delete = 1;
1058                        patch->new_name = NULL;
1059                }
1060        }
1061
1062        if (0 < patch->is_new && oldlines)
1063                die("new file %s depends on old contents", patch->new_name);
1064        if (0 < patch->is_delete && newlines)
1065                die("deleted file %s still has contents", patch->old_name);
1066        if (!patch->is_delete && !newlines && context)
1067                fprintf(stderr, "** warning: file %s becomes empty but "
1068                        "is not deleted\n", patch->new_name);
1069
1070        return offset;
1071}
1072
1073static inline int metadata_changes(struct patch *patch)
1074{
1075        return  patch->is_rename > 0 ||
1076                patch->is_copy > 0 ||
1077                patch->is_new > 0 ||
1078                patch->is_delete ||
1079                (patch->old_mode && patch->new_mode &&
1080                 patch->old_mode != patch->new_mode);
1081}
1082
1083static char *inflate_it(const void *data, unsigned long size,
1084                        unsigned long inflated_size)
1085{
1086        z_stream stream;
1087        void *out;
1088        int st;
1089
1090        memset(&stream, 0, sizeof(stream));
1091
1092        stream.next_in = (unsigned char *)data;
1093        stream.avail_in = size;
1094        stream.next_out = out = xmalloc(inflated_size);
1095        stream.avail_out = inflated_size;
1096        inflateInit(&stream);
1097        st = inflate(&stream, Z_FINISH);
1098        if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1099                free(out);
1100                return NULL;
1101        }
1102        return out;
1103}
1104
1105static struct fragment *parse_binary_hunk(char **buf_p,
1106                                          unsigned long *sz_p,
1107                                          int *status_p,
1108                                          int *used_p)
1109{
1110        /* Expect a line that begins with binary patch method ("literal"
1111         * or "delta"), followed by the length of data before deflating.
1112         * a sequence of 'length-byte' followed by base-85 encoded data
1113         * should follow, terminated by a newline.
1114         *
1115         * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1116         * and we would limit the patch line to 66 characters,
1117         * so one line can fit up to 13 groups that would decode
1118         * to 52 bytes max.  The length byte 'A'-'Z' corresponds
1119         * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1120         */
1121        int llen, used;
1122        unsigned long size = *sz_p;
1123        char *buffer = *buf_p;
1124        int patch_method;
1125        unsigned long origlen;
1126        char *data = NULL;
1127        int hunk_size = 0;
1128        struct fragment *frag;
1129
1130        llen = linelen(buffer, size);
1131        used = llen;
1132
1133        *status_p = 0;
1134
1135        if (!strncmp(buffer, "delta ", 6)) {
1136                patch_method = BINARY_DELTA_DEFLATED;
1137                origlen = strtoul(buffer + 6, NULL, 10);
1138        }
1139        else if (!strncmp(buffer, "literal ", 8)) {
1140                patch_method = BINARY_LITERAL_DEFLATED;
1141                origlen = strtoul(buffer + 8, NULL, 10);
1142        }
1143        else
1144                return NULL;
1145
1146        linenr++;
1147        buffer += llen;
1148        while (1) {
1149                int byte_length, max_byte_length, newsize;
1150                llen = linelen(buffer, size);
1151                used += llen;
1152                linenr++;
1153                if (llen == 1) {
1154                        /* consume the blank line */
1155                        buffer++;
1156                        size--;
1157                        break;
1158                }
1159                /* Minimum line is "A00000\n" which is 7-byte long,
1160                 * and the line length must be multiple of 5 plus 2.
1161                 */
1162                if ((llen < 7) || (llen-2) % 5)
1163                        goto corrupt;
1164                max_byte_length = (llen - 2) / 5 * 4;
1165                byte_length = *buffer;
1166                if ('A' <= byte_length && byte_length <= 'Z')
1167                        byte_length = byte_length - 'A' + 1;
1168                else if ('a' <= byte_length && byte_length <= 'z')
1169                        byte_length = byte_length - 'a' + 27;
1170                else
1171                        goto corrupt;
1172                /* if the input length was not multiple of 4, we would
1173                 * have filler at the end but the filler should never
1174                 * exceed 3 bytes
1175                 */
1176                if (max_byte_length < byte_length ||
1177                    byte_length <= max_byte_length - 4)
1178                        goto corrupt;
1179                newsize = hunk_size + byte_length;
1180                data = xrealloc(data, newsize);
1181                if (decode_85(data + hunk_size, buffer + 1, byte_length))
1182                        goto corrupt;
1183                hunk_size = newsize;
1184                buffer += llen;
1185                size -= llen;
1186        }
1187
1188        frag = xcalloc(1, sizeof(*frag));
1189        frag->patch = inflate_it(data, hunk_size, origlen);
1190        if (!frag->patch)
1191                goto corrupt;
1192        free(data);
1193        frag->size = origlen;
1194        *buf_p = buffer;
1195        *sz_p = size;
1196        *used_p = used;
1197        frag->binary_patch_method = patch_method;
1198        return frag;
1199
1200 corrupt:
1201        free(data);
1202        *status_p = -1;
1203        error("corrupt binary patch at line %d: %.*s",
1204              linenr-1, llen-1, buffer);
1205        return NULL;
1206}
1207
1208static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1209{
1210        /* We have read "GIT binary patch\n"; what follows is a line
1211         * that says the patch method (currently, either "literal" or
1212         * "delta") and the length of data before deflating; a
1213         * sequence of 'length-byte' followed by base-85 encoded data
1214         * follows.
1215         *
1216         * When a binary patch is reversible, there is another binary
1217         * hunk in the same format, starting with patch method (either
1218         * "literal" or "delta") with the length of data, and a sequence
1219         * of length-byte + base-85 encoded data, terminated with another
1220         * empty line.  This data, when applied to the postimage, produces
1221         * the preimage.
1222         */
1223        struct fragment *forward;
1224        struct fragment *reverse;
1225        int status;
1226        int used, used_1;
1227
1228        forward = parse_binary_hunk(&buffer, &size, &status, &used);
1229        if (!forward && !status)
1230                /* there has to be one hunk (forward hunk) */
1231                return error("unrecognized binary patch at line %d", linenr-1);
1232        if (status)
1233                /* otherwise we already gave an error message */
1234                return status;
1235
1236        reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1237        if (reverse)
1238                used += used_1;
1239        else if (status) {
1240                /* not having reverse hunk is not an error, but having
1241                 * a corrupt reverse hunk is.
1242                 */
1243                free((void*) forward->patch);
1244                free(forward);
1245                return status;
1246        }
1247        forward->next = reverse;
1248        patch->fragments = forward;
1249        patch->is_binary = 1;
1250        return used;
1251}
1252
1253static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1254{
1255        int hdrsize, patchsize;
1256        int offset = find_header(buffer, size, &hdrsize, patch);
1257
1258        if (offset < 0)
1259                return offset;
1260
1261        patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
1262
1263        if (!patchsize) {
1264                static const char *binhdr[] = {
1265                        "Binary files ",
1266                        "Files ",
1267                        NULL,
1268                };
1269                static const char git_binary[] = "GIT binary patch\n";
1270                int i;
1271                int hd = hdrsize + offset;
1272                unsigned long llen = linelen(buffer + hd, size - hd);
1273
1274                if (llen == sizeof(git_binary) - 1 &&
1275                    !memcmp(git_binary, buffer + hd, llen)) {
1276                        int used;
1277                        linenr++;
1278                        used = parse_binary(buffer + hd + llen,
1279                                            size - hd - llen, patch);
1280                        if (used)
1281                                patchsize = used + llen;
1282                        else
1283                                patchsize = 0;
1284                }
1285                else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1286                        for (i = 0; binhdr[i]; i++) {
1287                                int len = strlen(binhdr[i]);
1288                                if (len < size - hd &&
1289                                    !memcmp(binhdr[i], buffer + hd, len)) {
1290                                        linenr++;
1291                                        patch->is_binary = 1;
1292                                        patchsize = llen;
1293                                        break;
1294                                }
1295                        }
1296                }
1297
1298                /* Empty patch cannot be applied if it is a text patch
1299                 * without metadata change.  A binary patch appears
1300                 * empty to us here.
1301                 */
1302                if ((apply || check) &&
1303                    (!patch->is_binary && !metadata_changes(patch)))
1304                        die("patch with only garbage at line %d", linenr);
1305        }
1306
1307        return offset + hdrsize + patchsize;
1308}
1309
1310#define swap(a,b) myswap((a),(b),sizeof(a))
1311
1312#define myswap(a, b, size) do {         \
1313        unsigned char mytmp[size];      \
1314        memcpy(mytmp, &a, size);                \
1315        memcpy(&a, &b, size);           \
1316        memcpy(&b, mytmp, size);                \
1317} while (0)
1318
1319static void reverse_patches(struct patch *p)
1320{
1321        for (; p; p = p->next) {
1322                struct fragment *frag = p->fragments;
1323
1324                swap(p->new_name, p->old_name);
1325                swap(p->new_mode, p->old_mode);
1326                swap(p->is_new, p->is_delete);
1327                swap(p->lines_added, p->lines_deleted);
1328                swap(p->old_sha1_prefix, p->new_sha1_prefix);
1329
1330                for (; frag; frag = frag->next) {
1331                        swap(frag->newpos, frag->oldpos);
1332                        swap(frag->newlines, frag->oldlines);
1333                }
1334        }
1335}
1336
1337static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1338static const char minuses[]= "----------------------------------------------------------------------";
1339
1340static void show_stats(struct patch *patch)
1341{
1342        const char *prefix = "";
1343        char *name = patch->new_name;
1344        char *qname = NULL;
1345        int len, max, add, del, total;
1346
1347        if (!name)
1348                name = patch->old_name;
1349
1350        if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
1351                qname = xmalloc(len + 1);
1352                quote_c_style(name, qname, NULL, 0);
1353                name = qname;
1354        }
1355
1356        /*
1357         * "scale" the filename
1358         */
1359        len = strlen(name);
1360        max = max_len;
1361        if (max > 50)
1362                max = 50;
1363        if (len > max) {
1364                char *slash;
1365                prefix = "...";
1366                max -= 3;
1367                name += len - max;
1368                slash = strchr(name, '/');
1369                if (slash)
1370                        name = slash;
1371        }
1372        len = max;
1373
1374        /*
1375         * scale the add/delete
1376         */
1377        max = max_change;
1378        if (max + len > 70)
1379                max = 70 - len;
1380
1381        add = patch->lines_added;
1382        del = patch->lines_deleted;
1383        total = add + del;
1384
1385        if (max_change > 0) {
1386                total = (total * max + max_change / 2) / max_change;
1387                add = (add * max + max_change / 2) / max_change;
1388                del = total - add;
1389        }
1390        if (patch->is_binary)
1391                printf(" %s%-*s |  Bin\n", prefix, len, name);
1392        else
1393                printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
1394                       len, name, patch->lines_added + patch->lines_deleted,
1395                       add, pluses, del, minuses);
1396        free(qname);
1397}
1398
1399static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
1400{
1401        int fd;
1402        unsigned long got;
1403
1404        switch (st->st_mode & S_IFMT) {
1405        case S_IFLNK:
1406                return readlink(path, buf, size);
1407        case S_IFREG:
1408                fd = open(path, O_RDONLY);
1409                if (fd < 0)
1410                        return error("unable to open %s", path);
1411                got = 0;
1412                for (;;) {
1413                        int ret = xread(fd, (char *) buf + got, size - got);
1414                        if (ret <= 0)
1415                                break;
1416                        got += ret;
1417                }
1418                close(fd);
1419                return got;
1420
1421        default:
1422                return -1;
1423        }
1424}
1425
1426static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1427{
1428        int i;
1429        unsigned long start, backwards, forwards;
1430
1431        if (fragsize > size)
1432                return -1;
1433
1434        start = 0;
1435        if (line > 1) {
1436                unsigned long offset = 0;
1437                i = line-1;
1438                while (offset + fragsize <= size) {
1439                        if (buf[offset++] == '\n') {
1440                                start = offset;
1441                                if (!--i)
1442                                        break;
1443                        }
1444                }
1445        }
1446
1447        /* Exact line number? */
1448        if (!memcmp(buf + start, fragment, fragsize))
1449                return start;
1450
1451        /*
1452         * There's probably some smart way to do this, but I'll leave
1453         * that to the smart and beautiful people. I'm simple and stupid.
1454         */
1455        backwards = start;
1456        forwards = start;
1457        for (i = 0; ; i++) {
1458                unsigned long try;
1459                int n;
1460
1461                /* "backward" */
1462                if (i & 1) {
1463                        if (!backwards) {
1464                                if (forwards + fragsize > size)
1465                                        break;
1466                                continue;
1467                        }
1468                        do {
1469                                --backwards;
1470                        } while (backwards && buf[backwards-1] != '\n');
1471                        try = backwards;
1472                } else {
1473                        while (forwards + fragsize <= size) {
1474                                if (buf[forwards++] == '\n')
1475                                        break;
1476                        }
1477                        try = forwards;
1478                }
1479
1480                if (try + fragsize > size)
1481                        continue;
1482                if (memcmp(buf + try, fragment, fragsize))
1483                        continue;
1484                n = (i >> 1)+1;
1485                if (i & 1)
1486                        n = -n;
1487                *lines = n;
1488                return try;
1489        }
1490
1491        /*
1492         * We should start searching forward and backward.
1493         */
1494        return -1;
1495}
1496
1497static void remove_first_line(const char **rbuf, int *rsize)
1498{
1499        const char *buf = *rbuf;
1500        int size = *rsize;
1501        unsigned long offset;
1502        offset = 0;
1503        while (offset <= size) {
1504                if (buf[offset++] == '\n')
1505                        break;
1506        }
1507        *rsize = size - offset;
1508        *rbuf = buf + offset;
1509}
1510
1511static void remove_last_line(const char **rbuf, int *rsize)
1512{
1513        const char *buf = *rbuf;
1514        int size = *rsize;
1515        unsigned long offset;
1516        offset = size - 1;
1517        while (offset > 0) {
1518                if (buf[--offset] == '\n')
1519                        break;
1520        }
1521        *rsize = offset + 1;
1522}
1523
1524struct buffer_desc {
1525        char *buffer;
1526        unsigned long size;
1527        unsigned long alloc;
1528};
1529
1530static int apply_line(char *output, const char *patch, int plen)
1531{
1532        /* plen is number of bytes to be copied from patch,
1533         * starting at patch+1 (patch[0] is '+').  Typically
1534         * patch[plen] is '\n', unless this is the incomplete
1535         * last line.
1536         */
1537        int i;
1538        int add_nl_to_tail = 0;
1539        int fixed = 0;
1540        int last_tab_in_indent = -1;
1541        int last_space_in_indent = -1;
1542        int need_fix_leading_space = 0;
1543        char *buf;
1544
1545        if ((new_whitespace != strip_whitespace) || !whitespace_error) {
1546                memcpy(output, patch + 1, plen);
1547                return plen;
1548        }
1549
1550        if (1 < plen && isspace(patch[plen-1])) {
1551                if (patch[plen] == '\n')
1552                        add_nl_to_tail = 1;
1553                plen--;
1554                while (0 < plen && isspace(patch[plen]))
1555                        plen--;
1556                fixed = 1;
1557        }
1558
1559        for (i = 1; i < plen; i++) {
1560                char ch = patch[i];
1561                if (ch == '\t') {
1562                        last_tab_in_indent = i;
1563                        if (0 <= last_space_in_indent)
1564                                need_fix_leading_space = 1;
1565                }
1566                else if (ch == ' ')
1567                        last_space_in_indent = i;
1568                else
1569                        break;
1570        }
1571
1572        buf = output;
1573        if (need_fix_leading_space) {
1574                /* between patch[1..last_tab_in_indent] strip the
1575                 * funny spaces, updating them to tab as needed.
1576                 */
1577                for (i = 1; i < last_tab_in_indent; i++, plen--) {
1578                        char ch = patch[i];
1579                        if (ch != ' ')
1580                                *output++ = ch;
1581                        else if ((i % 8) == 0)
1582                                *output++ = '\t';
1583                }
1584                fixed = 1;
1585                i = last_tab_in_indent;
1586        }
1587        else
1588                i = 1;
1589
1590        memcpy(output, patch + i, plen);
1591        if (add_nl_to_tail)
1592                output[plen++] = '\n';
1593        if (fixed)
1594                applied_after_stripping++;
1595        return output + plen - buf;
1596}
1597
1598static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)
1599{
1600        int match_beginning, match_end;
1601        char *buf = desc->buffer;
1602        const char *patch = frag->patch;
1603        int offset, size = frag->size;
1604        char *old = xmalloc(size);
1605        char *new = xmalloc(size);
1606        const char *oldlines, *newlines;
1607        int oldsize = 0, newsize = 0;
1608        unsigned long leading, trailing;
1609        int pos, lines;
1610
1611        while (size > 0) {
1612                char first;
1613                int len = linelen(patch, size);
1614                int plen;
1615
1616                if (!len)
1617                        break;
1618
1619                /*
1620                 * "plen" is how much of the line we should use for
1621                 * the actual patch data. Normally we just remove the
1622                 * first character on the line, but if the line is
1623                 * followed by "\ No newline", then we also remove the
1624                 * last one (which is the newline, of course).
1625                 */
1626                plen = len-1;
1627                if (len < size && patch[len] == '\\')
1628                        plen--;
1629                first = *patch;
1630                if (apply_in_reverse) {
1631                        if (first == '-')
1632                                first = '+';
1633                        else if (first == '+')
1634                                first = '-';
1635                }
1636                switch (first) {
1637                case '\n':
1638                        /* Newer GNU diff, empty context line */
1639                        if (plen < 0)
1640                                /* ... followed by '\No newline'; nothing */
1641                                break;
1642                        old[oldsize++] = '\n';
1643                        new[newsize++] = '\n';
1644                        break;
1645                case ' ':
1646                case '-':
1647                        memcpy(old + oldsize, patch + 1, plen);
1648                        oldsize += plen;
1649                        if (first == '-')
1650                                break;
1651                /* Fall-through for ' ' */
1652                case '+':
1653                        if (first != '+' || !no_add)
1654                                newsize += apply_line(new + newsize, patch,
1655                                                      plen);
1656                        break;
1657                case '@': case '\\':
1658                        /* Ignore it, we already handled it */
1659                        break;
1660                default:
1661                        return -1;
1662                }
1663                patch += len;
1664                size -= len;
1665        }
1666
1667        if (inaccurate_eof && oldsize > 0 && old[oldsize - 1] == '\n' &&
1668                        newsize > 0 && new[newsize - 1] == '\n') {
1669                oldsize--;
1670                newsize--;
1671        }
1672
1673        oldlines = old;
1674        newlines = new;
1675        leading = frag->leading;
1676        trailing = frag->trailing;
1677
1678        /*
1679         * If we don't have any leading/trailing data in the patch,
1680         * we want it to match at the beginning/end of the file.
1681         *
1682         * But that would break if the patch is generated with
1683         * --unified=0; sane people wouldn't do that to cause us
1684         * trouble, but we try to please not so sane ones as well.
1685         */
1686        if (unidiff_zero) {
1687                match_beginning = (!leading && !frag->oldpos);
1688                match_end = 0;
1689        }
1690        else {
1691                match_beginning = !leading && (frag->oldpos == 1);
1692                match_end = !trailing;
1693        }
1694
1695        lines = 0;
1696        pos = frag->newpos;
1697        for (;;) {
1698                offset = find_offset(buf, desc->size,
1699                                     oldlines, oldsize, pos, &lines);
1700                if (match_end && offset + oldsize != desc->size)
1701                        offset = -1;
1702                if (match_beginning && offset)
1703                        offset = -1;
1704                if (offset >= 0) {
1705                        int diff = newsize - oldsize;
1706                        unsigned long size = desc->size + diff;
1707                        unsigned long alloc = desc->alloc;
1708
1709                        /* Warn if it was necessary to reduce the number
1710                         * of context lines.
1711                         */
1712                        if ((leading != frag->leading) ||
1713                            (trailing != frag->trailing))
1714                                fprintf(stderr, "Context reduced to (%ld/%ld)"
1715                                        " to apply fragment at %d\n",
1716                                        leading, trailing, pos + lines);
1717
1718                        if (size > alloc) {
1719                                alloc = size + 8192;
1720                                desc->alloc = alloc;
1721                                buf = xrealloc(buf, alloc);
1722                                desc->buffer = buf;
1723                        }
1724                        desc->size = size;
1725                        memmove(buf + offset + newsize,
1726                                buf + offset + oldsize,
1727                                size - offset - newsize);
1728                        memcpy(buf + offset, newlines, newsize);
1729                        offset = 0;
1730
1731                        break;
1732                }
1733
1734                /* Am I at my context limits? */
1735                if ((leading <= p_context) && (trailing <= p_context))
1736                        break;
1737                if (match_beginning || match_end) {
1738                        match_beginning = match_end = 0;
1739                        continue;
1740                }
1741                /* Reduce the number of context lines
1742                 * Reduce both leading and trailing if they are equal
1743                 * otherwise just reduce the larger context.
1744                 */
1745                if (leading >= trailing) {
1746                        remove_first_line(&oldlines, &oldsize);
1747                        remove_first_line(&newlines, &newsize);
1748                        pos--;
1749                        leading--;
1750                }
1751                if (trailing > leading) {
1752                        remove_last_line(&oldlines, &oldsize);
1753                        remove_last_line(&newlines, &newsize);
1754                        trailing--;
1755                }
1756        }
1757
1758        free(old);
1759        free(new);
1760        return offset;
1761}
1762
1763static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1764{
1765        unsigned long dst_size;
1766        struct fragment *fragment = patch->fragments;
1767        void *data;
1768        void *result;
1769
1770        /* Binary patch is irreversible without the optional second hunk */
1771        if (apply_in_reverse) {
1772                if (!fragment->next)
1773                        return error("cannot reverse-apply a binary patch "
1774                                     "without the reverse hunk to '%s'",
1775                                     patch->new_name
1776                                     ? patch->new_name : patch->old_name);
1777                fragment = fragment->next;
1778        }
1779        data = (void*) fragment->patch;
1780        switch (fragment->binary_patch_method) {
1781        case BINARY_DELTA_DEFLATED:
1782                result = patch_delta(desc->buffer, desc->size,
1783                                     data,
1784                                     fragment->size,
1785                                     &dst_size);
1786                free(desc->buffer);
1787                desc->buffer = result;
1788                break;
1789        case BINARY_LITERAL_DEFLATED:
1790                free(desc->buffer);
1791                desc->buffer = data;
1792                dst_size = fragment->size;
1793                break;
1794        }
1795        if (!desc->buffer)
1796                return -1;
1797        desc->size = desc->alloc = dst_size;
1798        return 0;
1799}
1800
1801static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1802{
1803        const char *name = patch->old_name ? patch->old_name : patch->new_name;
1804        unsigned char sha1[20];
1805
1806        /* For safety, we require patch index line to contain
1807         * full 40-byte textual SHA1 for old and new, at least for now.
1808         */
1809        if (strlen(patch->old_sha1_prefix) != 40 ||
1810            strlen(patch->new_sha1_prefix) != 40 ||
1811            get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1812            get_sha1_hex(patch->new_sha1_prefix, sha1))
1813                return error("cannot apply binary patch to '%s' "
1814                             "without full index line", name);
1815
1816        if (patch->old_name) {
1817                /* See if the old one matches what the patch
1818                 * applies to.
1819                 */
1820                hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1821                if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1822                        return error("the patch applies to '%s' (%s), "
1823                                     "which does not match the "
1824                                     "current contents.",
1825                                     name, sha1_to_hex(sha1));
1826        }
1827        else {
1828                /* Otherwise, the old one must be empty. */
1829                if (desc->size)
1830                        return error("the patch applies to an empty "
1831                                     "'%s' but it is not empty", name);
1832        }
1833
1834        get_sha1_hex(patch->new_sha1_prefix, sha1);
1835        if (is_null_sha1(sha1)) {
1836                free(desc->buffer);
1837                desc->alloc = desc->size = 0;
1838                desc->buffer = NULL;
1839                return 0; /* deletion patch */
1840        }
1841
1842        if (has_sha1_file(sha1)) {
1843                /* We already have the postimage */
1844                char type[10];
1845                unsigned long size;
1846
1847                free(desc->buffer);
1848                desc->buffer = read_sha1_file(sha1, type, &size);
1849                if (!desc->buffer)
1850                        return error("the necessary postimage %s for "
1851                                     "'%s' cannot be read",
1852                                     patch->new_sha1_prefix, name);
1853                desc->alloc = desc->size = size;
1854        }
1855        else {
1856                /* We have verified desc matches the preimage;
1857                 * apply the patch data to it, which is stored
1858                 * in the patch->fragments->{patch,size}.
1859                 */
1860                if (apply_binary_fragment(desc, patch))
1861                        return error("binary patch does not apply to '%s'",
1862                                     name);
1863
1864                /* verify that the result matches */
1865                hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1866                if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1867                        return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
1868        }
1869
1870        return 0;
1871}
1872
1873static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1874{
1875        struct fragment *frag = patch->fragments;
1876        const char *name = patch->old_name ? patch->old_name : patch->new_name;
1877
1878        if (patch->is_binary)
1879                return apply_binary(desc, patch);
1880
1881        while (frag) {
1882                if (apply_one_fragment(desc, frag, patch->inaccurate_eof)) {
1883                        error("patch failed: %s:%ld", name, frag->oldpos);
1884                        if (!apply_with_reject)
1885                                return -1;
1886                        frag->rejected = 1;
1887                }
1888                frag = frag->next;
1889        }
1890        return 0;
1891}
1892
1893static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
1894{
1895        char *buf;
1896        unsigned long size, alloc;
1897        struct buffer_desc desc;
1898
1899        size = 0;
1900        alloc = 0;
1901        buf = NULL;
1902        if (cached) {
1903                if (ce) {
1904                        char type[20];
1905                        buf = read_sha1_file(ce->sha1, type, &size);
1906                        if (!buf)
1907                                return error("read of %s failed",
1908                                             patch->old_name);
1909                        alloc = size;
1910                }
1911        }
1912        else if (patch->old_name) {
1913                size = st->st_size;
1914                alloc = size + 8192;
1915                buf = xmalloc(alloc);
1916                if (read_old_data(st, patch->old_name, buf, alloc) != size)
1917                        return error("read of %s failed", patch->old_name);
1918        }
1919
1920        desc.size = size;
1921        desc.alloc = alloc;
1922        desc.buffer = buf;
1923
1924        if (apply_fragments(&desc, patch) < 0)
1925                return -1; /* note with --reject this succeeds. */
1926
1927        /* NUL terminate the result */
1928        if (desc.alloc <= desc.size)
1929                desc.buffer = xrealloc(desc.buffer, desc.size + 1);
1930        desc.buffer[desc.size] = 0;
1931
1932        patch->result = desc.buffer;
1933        patch->resultsize = desc.size;
1934
1935        if (0 < patch->is_delete && patch->resultsize)
1936                return error("removal patch leaves file contents");
1937
1938        return 0;
1939}
1940
1941static int check_patch(struct patch *patch, struct patch *prev_patch)
1942{
1943        struct stat st;
1944        const char *old_name = patch->old_name;
1945        const char *new_name = patch->new_name;
1946        const char *name = old_name ? old_name : new_name;
1947        struct cache_entry *ce = NULL;
1948        int ok_if_exists;
1949
1950        patch->rejected = 1; /* we will drop this after we succeed */
1951        if (old_name) {
1952                int changed = 0;
1953                int stat_ret = 0;
1954                unsigned st_mode = 0;
1955
1956                if (!cached)
1957                        stat_ret = lstat(old_name, &st);
1958                if (check_index) {
1959                        int pos = cache_name_pos(old_name, strlen(old_name));
1960                        if (pos < 0)
1961                                return error("%s: does not exist in index",
1962                                             old_name);
1963                        ce = active_cache[pos];
1964                        if (stat_ret < 0) {
1965                                struct checkout costate;
1966                                if (errno != ENOENT)
1967                                        return error("%s: %s", old_name,
1968                                                     strerror(errno));
1969                                /* checkout */
1970                                costate.base_dir = "";
1971                                costate.base_dir_len = 0;
1972                                costate.force = 0;
1973                                costate.quiet = 0;
1974                                costate.not_new = 0;
1975                                costate.refresh_cache = 1;
1976                                if (checkout_entry(ce,
1977                                                   &costate,
1978                                                   NULL) ||
1979                                    lstat(old_name, &st))
1980                                        return -1;
1981                        }
1982                        if (!cached)
1983                                changed = ce_match_stat(ce, &st, 1);
1984                        if (changed)
1985                                return error("%s: does not match index",
1986                                             old_name);
1987                        if (cached)
1988                                st_mode = ntohl(ce->ce_mode);
1989                }
1990                else if (stat_ret < 0)
1991                        return error("%s: %s", old_name, strerror(errno));
1992
1993                if (!cached)
1994                        st_mode = ntohl(create_ce_mode(st.st_mode));
1995
1996                if (patch->is_new < 0)
1997                        patch->is_new = 0;
1998                if (!patch->old_mode)
1999                        patch->old_mode = st_mode;
2000                if ((st_mode ^ patch->old_mode) & S_IFMT)
2001                        return error("%s: wrong type", old_name);
2002                if (st_mode != patch->old_mode)
2003                        fprintf(stderr, "warning: %s has type %o, expected %o\n",
2004                                old_name, st_mode, patch->old_mode);
2005        }
2006
2007        if (new_name && prev_patch && 0 < prev_patch->is_delete &&
2008            !strcmp(prev_patch->old_name, new_name))
2009                /* A type-change diff is always split into a patch to
2010                 * delete old, immediately followed by a patch to
2011                 * create new (see diff.c::run_diff()); in such a case
2012                 * it is Ok that the entry to be deleted by the
2013                 * previous patch is still in the working tree and in
2014                 * the index.
2015                 */
2016                ok_if_exists = 1;
2017        else
2018                ok_if_exists = 0;
2019
2020        if (new_name &&
2021            ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
2022                if (check_index &&
2023                    cache_name_pos(new_name, strlen(new_name)) >= 0 &&
2024                    !ok_if_exists)
2025                        return error("%s: already exists in index", new_name);
2026                if (!cached) {
2027                        struct stat nst;
2028                        if (!lstat(new_name, &nst)) {
2029                                if (S_ISDIR(nst.st_mode) || ok_if_exists)
2030                                        ; /* ok */
2031                                else
2032                                        return error("%s: already exists in working directory", new_name);
2033                        }
2034                        else if ((errno != ENOENT) && (errno != ENOTDIR))
2035                                return error("%s: %s", new_name, strerror(errno));
2036                }
2037                if (!patch->new_mode) {
2038                        if (0 < patch->is_new)
2039                                patch->new_mode = S_IFREG | 0644;
2040                        else
2041                                patch->new_mode = patch->old_mode;
2042                }
2043        }
2044
2045        if (new_name && old_name) {
2046                int same = !strcmp(old_name, new_name);
2047                if (!patch->new_mode)
2048                        patch->new_mode = patch->old_mode;
2049                if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
2050                        return error("new mode (%o) of %s does not match old mode (%o)%s%s",
2051                                patch->new_mode, new_name, patch->old_mode,
2052                                same ? "" : " of ", same ? "" : old_name);
2053        }
2054
2055        if (apply_data(patch, &st, ce) < 0)
2056                return error("%s: patch does not apply", name);
2057        patch->rejected = 0;
2058        return 0;
2059}
2060
2061static int check_patch_list(struct patch *patch)
2062{
2063        struct patch *prev_patch = NULL;
2064        int err = 0;
2065
2066        for (prev_patch = NULL; patch ; patch = patch->next) {
2067                if (apply_verbosely)
2068                        say_patch_name(stderr,
2069                                       "Checking patch ", patch, "...\n");
2070                err |= check_patch(patch, prev_patch);
2071                prev_patch = patch;
2072        }
2073        return err;
2074}
2075
2076static void show_index_list(struct patch *list)
2077{
2078        struct patch *patch;
2079
2080        /* Once we start supporting the reverse patch, it may be
2081         * worth showing the new sha1 prefix, but until then...
2082         */
2083        for (patch = list; patch; patch = patch->next) {
2084                const unsigned char *sha1_ptr;
2085                unsigned char sha1[20];
2086                const char *name;
2087
2088                name = patch->old_name ? patch->old_name : patch->new_name;
2089                if (0 < patch->is_new)
2090                        sha1_ptr = null_sha1;
2091                else if (get_sha1(patch->old_sha1_prefix, sha1))
2092                        die("sha1 information is lacking or useless (%s).",
2093                            name);
2094                else
2095                        sha1_ptr = sha1;
2096
2097                printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
2098                if (line_termination && quote_c_style(name, NULL, NULL, 0))
2099                        quote_c_style(name, NULL, stdout, 0);
2100                else
2101                        fputs(name, stdout);
2102                putchar(line_termination);
2103        }
2104}
2105
2106static void stat_patch_list(struct patch *patch)
2107{
2108        int files, adds, dels;
2109
2110        for (files = adds = dels = 0 ; patch ; patch = patch->next) {
2111                files++;
2112                adds += patch->lines_added;
2113                dels += patch->lines_deleted;
2114                show_stats(patch);
2115        }
2116
2117        printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
2118}
2119
2120static void numstat_patch_list(struct patch *patch)
2121{
2122        for ( ; patch; patch = patch->next) {
2123                const char *name;
2124                name = patch->new_name ? patch->new_name : patch->old_name;
2125                if (patch->is_binary)
2126                        printf("-\t-\t");
2127                else
2128                        printf("%d\t%d\t",
2129                               patch->lines_added, patch->lines_deleted);
2130                if (line_termination && quote_c_style(name, NULL, NULL, 0))
2131                        quote_c_style(name, NULL, stdout, 0);
2132                else
2133                        fputs(name, stdout);
2134                putchar(line_termination);
2135        }
2136}
2137
2138static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
2139{
2140        if (mode)
2141                printf(" %s mode %06o %s\n", newdelete, mode, name);
2142        else
2143                printf(" %s %s\n", newdelete, name);
2144}
2145
2146static void show_mode_change(struct patch *p, int show_name)
2147{
2148        if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
2149                if (show_name)
2150                        printf(" mode change %06o => %06o %s\n",
2151                               p->old_mode, p->new_mode, p->new_name);
2152                else
2153                        printf(" mode change %06o => %06o\n",
2154                               p->old_mode, p->new_mode);
2155        }
2156}
2157
2158static void show_rename_copy(struct patch *p)
2159{
2160        const char *renamecopy = p->is_rename ? "rename" : "copy";
2161        const char *old, *new;
2162
2163        /* Find common prefix */
2164        old = p->old_name;
2165        new = p->new_name;
2166        while (1) {
2167                const char *slash_old, *slash_new;
2168                slash_old = strchr(old, '/');
2169                slash_new = strchr(new, '/');
2170                if (!slash_old ||
2171                    !slash_new ||
2172                    slash_old - old != slash_new - new ||
2173                    memcmp(old, new, slash_new - new))
2174                        break;
2175                old = slash_old + 1;
2176                new = slash_new + 1;
2177        }
2178        /* p->old_name thru old is the common prefix, and old and new
2179         * through the end of names are renames
2180         */
2181        if (old != p->old_name)
2182                printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2183                       (int)(old - p->old_name), p->old_name,
2184                       old, new, p->score);
2185        else
2186                printf(" %s %s => %s (%d%%)\n", renamecopy,
2187                       p->old_name, p->new_name, p->score);
2188        show_mode_change(p, 0);
2189}
2190
2191static void summary_patch_list(struct patch *patch)
2192{
2193        struct patch *p;
2194
2195        for (p = patch; p; p = p->next) {
2196                if (p->is_new)
2197                        show_file_mode_name("create", p->new_mode, p->new_name);
2198                else if (p->is_delete)
2199                        show_file_mode_name("delete", p->old_mode, p->old_name);
2200                else {
2201                        if (p->is_rename || p->is_copy)
2202                                show_rename_copy(p);
2203                        else {
2204                                if (p->score) {
2205                                        printf(" rewrite %s (%d%%)\n",
2206                                               p->new_name, p->score);
2207                                        show_mode_change(p, 0);
2208                                }
2209                                else
2210                                        show_mode_change(p, 1);
2211                        }
2212                }
2213        }
2214}
2215
2216static void patch_stats(struct patch *patch)
2217{
2218        int lines = patch->lines_added + patch->lines_deleted;
2219
2220        if (lines > max_change)
2221                max_change = lines;
2222        if (patch->old_name) {
2223                int len = quote_c_style(patch->old_name, NULL, NULL, 0);
2224                if (!len)
2225                        len = strlen(patch->old_name);
2226                if (len > max_len)
2227                        max_len = len;
2228        }
2229        if (patch->new_name) {
2230                int len = quote_c_style(patch->new_name, NULL, NULL, 0);
2231                if (!len)
2232                        len = strlen(patch->new_name);
2233                if (len > max_len)
2234                        max_len = len;
2235        }
2236}
2237
2238static void remove_file(struct patch *patch, int rmdir_empty)
2239{
2240        if (write_index) {
2241                if (remove_file_from_cache(patch->old_name) < 0)
2242                        die("unable to remove %s from index", patch->old_name);
2243                cache_tree_invalidate_path(active_cache_tree, patch->old_name);
2244        }
2245        if (!cached) {
2246                if (!unlink(patch->old_name) && rmdir_empty) {
2247                        char *name = xstrdup(patch->old_name);
2248                        char *end = strrchr(name, '/');
2249                        while (end) {
2250                                *end = 0;
2251                                if (rmdir(name))
2252                                        break;
2253                                end = strrchr(name, '/');
2254                        }
2255                        free(name);
2256                }
2257        }
2258}
2259
2260static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
2261{
2262        struct stat st;
2263        struct cache_entry *ce;
2264        int namelen = strlen(path);
2265        unsigned ce_size = cache_entry_size(namelen);
2266
2267        if (!write_index)
2268                return;
2269
2270        ce = xcalloc(1, ce_size);
2271        memcpy(ce->name, path, namelen);
2272        ce->ce_mode = create_ce_mode(mode);
2273        ce->ce_flags = htons(namelen);
2274        if (!cached) {
2275                if (lstat(path, &st) < 0)
2276                        die("unable to stat newly created file %s", path);
2277                fill_stat_cache_info(ce, &st);
2278        }
2279        if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2280                die("unable to create backing store for newly created file %s", path);
2281        if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
2282                die("unable to add cache entry for %s", path);
2283}
2284
2285static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2286{
2287        int fd;
2288
2289        if (S_ISLNK(mode))
2290                /* Although buf:size is counted string, it also is NUL
2291                 * terminated.
2292                 */
2293                return symlink(buf, path);
2294        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2295        if (fd < 0)
2296                return -1;
2297        while (size) {
2298                int written = xwrite(fd, buf, size);
2299                if (written < 0)
2300                        die("writing file %s: %s", path, strerror(errno));
2301                if (!written)
2302                        die("out of space writing file %s", path);
2303                buf += written;
2304                size -= written;
2305        }
2306        if (close(fd) < 0)
2307                die("closing file %s: %s", path, strerror(errno));
2308        return 0;
2309}
2310
2311/*
2312 * We optimistically assume that the directories exist,
2313 * which is true 99% of the time anyway. If they don't,
2314 * we create them and try again.
2315 */
2316static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
2317{
2318        if (cached)
2319                return;
2320        if (!try_create_file(path, mode, buf, size))
2321                return;
2322
2323        if (errno == ENOENT) {
2324                if (safe_create_leading_directories(path))
2325                        return;
2326                if (!try_create_file(path, mode, buf, size))
2327                        return;
2328        }
2329
2330        if (errno == EEXIST || errno == EACCES) {
2331                /* We may be trying to create a file where a directory
2332                 * used to be.
2333                 */
2334                struct stat st;
2335                errno = 0;
2336                if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
2337                        errno = EEXIST;
2338        }
2339
2340        if (errno == EEXIST) {
2341                unsigned int nr = getpid();
2342
2343                for (;;) {
2344                        const char *newpath;
2345                        newpath = mkpath("%s~%u", path, nr);
2346                        if (!try_create_file(newpath, mode, buf, size)) {
2347                                if (!rename(newpath, path))
2348                                        return;
2349                                unlink(newpath);
2350                                break;
2351                        }
2352                        if (errno != EEXIST)
2353                                break;
2354                        ++nr;
2355                }
2356        }
2357        die("unable to write file %s mode %o", path, mode);
2358}
2359
2360static void create_file(struct patch *patch)
2361{
2362        char *path = patch->new_name;
2363        unsigned mode = patch->new_mode;
2364        unsigned long size = patch->resultsize;
2365        char *buf = patch->result;
2366
2367        if (!mode)
2368                mode = S_IFREG | 0644;
2369        create_one_file(path, mode, buf, size);
2370        add_index_file(path, mode, buf, size);
2371        cache_tree_invalidate_path(active_cache_tree, path);
2372}
2373
2374/* phase zero is to remove, phase one is to create */
2375static void write_out_one_result(struct patch *patch, int phase)
2376{
2377        if (patch->is_delete > 0) {
2378                if (phase == 0)
2379                        remove_file(patch, 1);
2380                return;
2381        }
2382        if (patch->is_new > 0 || patch->is_copy) {
2383                if (phase == 1)
2384                        create_file(patch);
2385                return;
2386        }
2387        /*
2388         * Rename or modification boils down to the same
2389         * thing: remove the old, write the new
2390         */
2391        if (phase == 0)
2392                remove_file(patch, 0);
2393        if (phase == 1)
2394                create_file(patch);
2395}
2396
2397static int write_out_one_reject(struct patch *patch)
2398{
2399        FILE *rej;
2400        char namebuf[PATH_MAX];
2401        struct fragment *frag;
2402        int cnt = 0;
2403
2404        for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
2405                if (!frag->rejected)
2406                        continue;
2407                cnt++;
2408        }
2409
2410        if (!cnt) {
2411                if (apply_verbosely)
2412                        say_patch_name(stderr,
2413                                       "Applied patch ", patch, " cleanly.\n");
2414                return 0;
2415        }
2416
2417        /* This should not happen, because a removal patch that leaves
2418         * contents are marked "rejected" at the patch level.
2419         */
2420        if (!patch->new_name)
2421                die("internal error");
2422
2423        /* Say this even without --verbose */
2424        say_patch_name(stderr, "Applying patch ", patch, " with");
2425        fprintf(stderr, " %d rejects...\n", cnt);
2426
2427        cnt = strlen(patch->new_name);
2428        if (ARRAY_SIZE(namebuf) <= cnt + 5) {
2429                cnt = ARRAY_SIZE(namebuf) - 5;
2430                fprintf(stderr,
2431                        "warning: truncating .rej filename to %.*s.rej",
2432                        cnt - 1, patch->new_name);
2433        }
2434        memcpy(namebuf, patch->new_name, cnt);
2435        memcpy(namebuf + cnt, ".rej", 5);
2436
2437        rej = fopen(namebuf, "w");
2438        if (!rej)
2439                return error("cannot open %s: %s", namebuf, strerror(errno));
2440
2441        /* Normal git tools never deal with .rej, so do not pretend
2442         * this is a git patch by saying --git nor give extended
2443         * headers.  While at it, maybe please "kompare" that wants
2444         * the trailing TAB and some garbage at the end of line ;-).
2445         */
2446        fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
2447                patch->new_name, patch->new_name);
2448        for (cnt = 1, frag = patch->fragments;
2449             frag;
2450             cnt++, frag = frag->next) {
2451                if (!frag->rejected) {
2452                        fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
2453                        continue;
2454                }
2455                fprintf(stderr, "Rejected hunk #%d.\n", cnt);
2456                fprintf(rej, "%.*s", frag->size, frag->patch);
2457                if (frag->patch[frag->size-1] != '\n')
2458                        fputc('\n', rej);
2459        }
2460        fclose(rej);
2461        return -1;
2462}
2463
2464static int write_out_results(struct patch *list, int skipped_patch)
2465{
2466        int phase;
2467        int errs = 0;
2468        struct patch *l;
2469
2470        if (!list && !skipped_patch)
2471                return error("No changes");
2472
2473        for (phase = 0; phase < 2; phase++) {
2474                l = list;
2475                while (l) {
2476                        if (l->rejected)
2477                                errs = 1;
2478                        else {
2479                                write_out_one_result(l, phase);
2480                                if (phase == 1 && write_out_one_reject(l))
2481                                        errs = 1;
2482                        }
2483                        l = l->next;
2484                }
2485        }
2486        return errs;
2487}
2488
2489static struct lock_file lock_file;
2490
2491static struct excludes {
2492        struct excludes *next;
2493        const char *path;
2494} *excludes;
2495
2496static int use_patch(struct patch *p)
2497{
2498        const char *pathname = p->new_name ? p->new_name : p->old_name;
2499        struct excludes *x = excludes;
2500        while (x) {
2501                if (fnmatch(x->path, pathname, 0) == 0)
2502                        return 0;
2503                x = x->next;
2504        }
2505        if (0 < prefix_length) {
2506                int pathlen = strlen(pathname);
2507                if (pathlen <= prefix_length ||
2508                    memcmp(prefix, pathname, prefix_length))
2509                        return 0;
2510        }
2511        return 1;
2512}
2513
2514static void prefix_one(char **name)
2515{
2516        char *old_name = *name;
2517        if (!old_name)
2518                return;
2519        *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
2520        free(old_name);
2521}
2522
2523static void prefix_patches(struct patch *p)
2524{
2525        if (!prefix || p->is_toplevel_relative)
2526                return;
2527        for ( ; p; p = p->next) {
2528                if (p->new_name == p->old_name) {
2529                        char *prefixed = p->new_name;
2530                        prefix_one(&prefixed);
2531                        p->new_name = p->old_name = prefixed;
2532                }
2533                else {
2534                        prefix_one(&p->new_name);
2535                        prefix_one(&p->old_name);
2536                }
2537        }
2538}
2539
2540static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2541{
2542        unsigned long offset, size;
2543        char *buffer = read_patch_file(fd, &size);
2544        struct patch *list = NULL, **listp = &list;
2545        int skipped_patch = 0;
2546
2547        patch_input_file = filename;
2548        if (!buffer)
2549                return -1;
2550        offset = 0;
2551        while (size > 0) {
2552                struct patch *patch;
2553                int nr;
2554
2555                patch = xcalloc(1, sizeof(*patch));
2556                patch->inaccurate_eof = inaccurate_eof;
2557                nr = parse_chunk(buffer + offset, size, patch);
2558                if (nr < 0)
2559                        break;
2560                if (apply_in_reverse)
2561                        reverse_patches(patch);
2562                if (prefix)
2563                        prefix_patches(patch);
2564                if (use_patch(patch)) {
2565                        patch_stats(patch);
2566                        *listp = patch;
2567                        listp = &patch->next;
2568                }
2569                else {
2570                        /* perhaps free it a bit better? */
2571                        free(patch);
2572                        skipped_patch++;
2573                }
2574                offset += nr;
2575                size -= nr;
2576        }
2577
2578        if (whitespace_error && (new_whitespace == error_on_whitespace))
2579                apply = 0;
2580
2581        write_index = check_index && apply;
2582        if (write_index && newfd < 0)
2583                newfd = hold_lock_file_for_update(&lock_file,
2584                                                  get_index_file(), 1);
2585        if (check_index) {
2586                if (read_cache() < 0)
2587                        die("unable to read index file");
2588        }
2589
2590        if ((check || apply) &&
2591            check_patch_list(list) < 0 &&
2592            !apply_with_reject)
2593                exit(1);
2594
2595        if (apply && write_out_results(list, skipped_patch))
2596                exit(1);
2597
2598        if (show_index_info)
2599                show_index_list(list);
2600
2601        if (diffstat)
2602                stat_patch_list(list);
2603
2604        if (numstat)
2605                numstat_patch_list(list);
2606
2607        if (summary)
2608                summary_patch_list(list);
2609
2610        free(buffer);
2611        return 0;
2612}
2613
2614static int git_apply_config(const char *var, const char *value)
2615{
2616        if (!strcmp(var, "apply.whitespace")) {
2617                apply_default_whitespace = xstrdup(value);
2618                return 0;
2619        }
2620        return git_default_config(var, value);
2621}
2622
2623
2624int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2625{
2626        int i;
2627        int read_stdin = 1;
2628        int inaccurate_eof = 0;
2629        int errs = 0;
2630        int is_not_gitdir = 0;
2631
2632        const char *whitespace_option = NULL;
2633
2634        prefix = setup_git_directory_gently(&is_not_gitdir);
2635        prefix_length = prefix ? strlen(prefix) : 0;
2636        git_config(git_apply_config);
2637        if (apply_default_whitespace)
2638                parse_whitespace_option(apply_default_whitespace);
2639
2640        for (i = 1; i < argc; i++) {
2641                const char *arg = argv[i];
2642                char *end;
2643                int fd;
2644
2645                if (!strcmp(arg, "-")) {
2646                        errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2647                        read_stdin = 0;
2648                        continue;
2649                }
2650                if (!strncmp(arg, "--exclude=", 10)) {
2651                        struct excludes *x = xmalloc(sizeof(*x));
2652                        x->path = arg + 10;
2653                        x->next = excludes;
2654                        excludes = x;
2655                        continue;
2656                }
2657                if (!strncmp(arg, "-p", 2)) {
2658                        p_value = atoi(arg + 2);
2659                        continue;
2660                }
2661                if (!strcmp(arg, "--no-add")) {
2662                        no_add = 1;
2663                        continue;
2664                }
2665                if (!strcmp(arg, "--stat")) {
2666                        apply = 0;
2667                        diffstat = 1;
2668                        continue;
2669                }
2670                if (!strcmp(arg, "--allow-binary-replacement") ||
2671                    !strcmp(arg, "--binary")) {
2672                        continue; /* now no-op */
2673                }
2674                if (!strcmp(arg, "--numstat")) {
2675                        apply = 0;
2676                        numstat = 1;
2677                        continue;
2678                }
2679                if (!strcmp(arg, "--summary")) {
2680                        apply = 0;
2681                        summary = 1;
2682                        continue;
2683                }
2684                if (!strcmp(arg, "--check")) {
2685                        apply = 0;
2686                        check = 1;
2687                        continue;
2688                }
2689                if (!strcmp(arg, "--index")) {
2690                        if (is_not_gitdir)
2691                                die("--index outside a repository");
2692                        check_index = 1;
2693                        continue;
2694                }
2695                if (!strcmp(arg, "--cached")) {
2696                        if (is_not_gitdir)
2697                                die("--cached outside a repository");
2698                        check_index = 1;
2699                        cached = 1;
2700                        continue;
2701                }
2702                if (!strcmp(arg, "--apply")) {
2703                        apply = 1;
2704                        continue;
2705                }
2706                if (!strcmp(arg, "--index-info")) {
2707                        apply = 0;
2708                        show_index_info = 1;
2709                        continue;
2710                }
2711                if (!strcmp(arg, "-z")) {
2712                        line_termination = 0;
2713                        continue;
2714                }
2715                if (!strncmp(arg, "-C", 2)) {
2716                        p_context = strtoul(arg + 2, &end, 0);
2717                        if (*end != '\0')
2718                                die("unrecognized context count '%s'", arg + 2);
2719                        continue;
2720                }
2721                if (!strncmp(arg, "--whitespace=", 13)) {
2722                        whitespace_option = arg + 13;
2723                        parse_whitespace_option(arg + 13);
2724                        continue;
2725                }
2726                if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) {
2727                        apply_in_reverse = 1;
2728                        continue;
2729                }
2730                if (!strcmp(arg, "--unidiff-zero")) {
2731                        unidiff_zero = 1;
2732                        continue;
2733                }
2734                if (!strcmp(arg, "--reject")) {
2735                        apply = apply_with_reject = apply_verbosely = 1;
2736                        continue;
2737                }
2738                if (!strcmp(arg, "--verbose")) {
2739                        apply_verbosely = 1;
2740                        continue;
2741                }
2742                if (!strcmp(arg, "--inaccurate-eof")) {
2743                        inaccurate_eof = 1;
2744                        continue;
2745                }
2746                if (0 < prefix_length)
2747                        arg = prefix_filename(prefix, prefix_length, arg);
2748
2749                fd = open(arg, O_RDONLY);
2750                if (fd < 0)
2751                        usage(apply_usage);
2752                read_stdin = 0;
2753                set_default_whitespace_mode(whitespace_option);
2754                errs |= apply_patch(fd, arg, inaccurate_eof);
2755                close(fd);
2756        }
2757        set_default_whitespace_mode(whitespace_option);
2758        if (read_stdin)
2759                errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2760        if (whitespace_error) {
2761                if (squelch_whitespace_errors &&
2762                    squelch_whitespace_errors < whitespace_error) {
2763                        int squelched =
2764                                whitespace_error - squelch_whitespace_errors;
2765                        fprintf(stderr, "warning: squelched %d "
2766                                "whitespace error%s\n",
2767                                squelched,
2768                                squelched == 1 ? "" : "s");
2769                }
2770                if (new_whitespace == error_on_whitespace)
2771                        die("%d line%s add%s trailing whitespaces.",
2772                            whitespace_error,
2773                            whitespace_error == 1 ? "" : "s",
2774                            whitespace_error == 1 ? "s" : "");
2775                if (applied_after_stripping)
2776                        fprintf(stderr, "warning: %d line%s applied after"
2777                                " stripping trailing whitespaces.\n",
2778                                applied_after_stripping,
2779                                applied_after_stripping == 1 ? "" : "s");
2780                else if (whitespace_error)
2781                        fprintf(stderr, "warning: %d line%s add%s trailing"
2782                                " whitespaces.\n",
2783                                whitespace_error,
2784                                whitespace_error == 1 ? "" : "s",
2785                                whitespace_error == 1 ? "s" : "");
2786        }
2787
2788        if (write_index) {
2789                if (write_cache(newfd, active_cache, active_nr) ||
2790                    close(newfd) || commit_lock_file(&lock_file))
2791                        die("Unable to write new index file");
2792        }
2793
2794        return !!errs;
2795}