expose a helper function peel_to_type().
[gitweb.git] / builtin-apply.c
index bec95d6c8aa60ac40718f52da5a8f9603330dadb..6a88ff018df8890af8ef2e17340d5969dc1e396b 100644 (file)
@@ -28,8 +28,9 @@ static int newfd = -1;
 
 static int unidiff_zero;
 static int p_value = 1;
+static int p_value_known;
 static int check_index;
-static int write_index;
+static int update_index;
 static int cached;
 static int diffstat;
 static int numstat;
@@ -40,48 +41,48 @@ static int apply_in_reverse;
 static int apply_with_reject;
 static int apply_verbosely;
 static int no_add;
-static int show_index_info;
+static const char *fake_ancestor;
 static int line_termination = '\n';
 static unsigned long p_context = ULONG_MAX;
 static const char apply_usage[] =
-"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>...";
-
-static enum whitespace_eol {
-       nowarn_whitespace,
-       warn_on_whitespace,
-       error_on_whitespace,
-       strip_whitespace,
-} new_whitespace = warn_on_whitespace;
+"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|fix|error|error-all>] <patch>...";
+
+static enum ws_error_action {
+       nowarn_ws_error,
+       warn_on_ws_error,
+       die_on_ws_error,
+       correct_ws_error,
+} ws_error_action = warn_on_ws_error;
 static int whitespace_error;
 static int squelch_whitespace_errors = 5;
-static int applied_after_stripping;
+static int applied_after_fixing_ws;
 static const char *patch_input_file;
 
 static void parse_whitespace_option(const char *option)
 {
        if (!option) {
-               new_whitespace = warn_on_whitespace;
+               ws_error_action = warn_on_ws_error;
                return;
        }
        if (!strcmp(option, "warn")) {
-               new_whitespace = warn_on_whitespace;
+               ws_error_action = warn_on_ws_error;
                return;
        }
        if (!strcmp(option, "nowarn")) {
-               new_whitespace = nowarn_whitespace;
+               ws_error_action = nowarn_ws_error;
                return;
        }
        if (!strcmp(option, "error")) {
-               new_whitespace = error_on_whitespace;
+               ws_error_action = die_on_ws_error;
                return;
        }
        if (!strcmp(option, "error-all")) {
-               new_whitespace = error_on_whitespace;
+               ws_error_action = die_on_ws_error;
                squelch_whitespace_errors = 0;
                return;
        }
-       if (!strcmp(option, "strip")) {
-               new_whitespace = strip_whitespace;
+       if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
+               ws_error_action = correct_ws_error;
                return;
        }
        die("unrecognized whitespace option '%s'", option);
@@ -89,11 +90,8 @@ static void parse_whitespace_option(const char *option)
 
 static void set_default_whitespace_mode(const char *whitespace_option)
 {
-       if (!whitespace_option && !apply_default_whitespace) {
-               new_whitespace = (apply
-                                 ? warn_on_whitespace
-                                 : nowarn_whitespace);
-       }
+       if (!whitespace_option && !apply_default_whitespace)
+               ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
 /*
@@ -136,40 +134,47 @@ struct fragment {
 #define BINARY_DELTA_DEFLATED  1
 #define BINARY_LITERAL_DEFLATED 2
 
+/*
+ * This represents a "patch" to a file, both metainfo changes
+ * such as creation/deletion, filemode and content changes represented
+ * as a series of fragments.
+ */
 struct patch {
        char *new_name, *old_name, *def_name;
        unsigned int old_mode, new_mode;
        int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
        int rejected;
+       unsigned ws_rule;
        unsigned long deflate_origlen;
        int lines_added, lines_deleted;
        int score;
+       unsigned int is_toplevel_relative:1;
        unsigned int inaccurate_eof:1;
        unsigned int is_binary:1;
        unsigned int is_copy:1;
        unsigned int is_rename:1;
        struct fragment *fragments;
        char *result;
-       unsigned long resultsize;
+       size_t resultsize;
        char old_sha1_prefix[41];
        char new_sha1_prefix[41];
        struct patch *next;
 };
 
-static void say_patch_name(FILE *output, const char *pre, struct patch *patch, const char *post)
+static void say_patch_name(FILE *output, const char *pre,
+                          struct patch *patch, const char *post)
 {
        fputs(pre, output);
        if (patch->old_name && patch->new_name &&
            strcmp(patch->old_name, patch->new_name)) {
-               write_name_quoted(NULL, 0, patch->old_name, 1, output);
+               quote_c_style(patch->old_name, NULL, output, 0);
                fputs(" => ", output);
-               write_name_quoted(NULL, 0, patch->new_name, 1, output);
-       }
-       else {
+               quote_c_style(patch->new_name, NULL, output, 0);
+       } else {
                const char *n = patch->new_name;
                if (!n)
                        n = patch->old_name;
-               write_name_quoted(NULL, 0, n, 1, output);
+               quote_c_style(n, NULL, output, 0);
        }
        fputs(post, output);
 }
@@ -177,36 +182,18 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
 #define CHUNKSIZE (8192)
 #define SLOP (16)
 
-static void *read_patch_file(int fd, unsigned long *sizep)
+static void read_patch_file(struct strbuf *sb, int fd)
 {
-       unsigned long size = 0, alloc = CHUNKSIZE;
-       void *buffer = xmalloc(alloc);
-
-       for (;;) {
-               int nr = alloc - size;
-               if (nr < 1024) {
-                       alloc += CHUNKSIZE;
-                       buffer = xrealloc(buffer, alloc);
-                       nr = alloc - size;
-               }
-               nr = xread(fd, (char *) buffer + size, nr);
-               if (!nr)
-                       break;
-               if (nr < 0)
-                       die("git-apply: read returned %s", strerror(errno));
-               size += nr;
-       }
-       *sizep = size;
+       if (strbuf_read(sb, fd, 0) < 0)
+               die("git-apply: read returned %s", strerror(errno));
 
        /*
         * Make sure that we have some slop in the buffer
         * so that we can do speculative "memcmp" etc, and
         * see to it that it is NUL-filled.
         */
-       if (alloc < size + SLOP)
-               buffer = xrealloc(buffer, size + SLOP);
-       memset((char *) buffer + size, 0, SLOP);
-       return buffer;
+       strbuf_grow(sb, SLOP);
+       memset(sb->buf + sb->len, 0, SLOP);
 }
 
 static unsigned long linelen(const char *buffer, unsigned long size)
@@ -238,39 +225,38 @@ static int name_terminate(const char *name, int namelen, int c, int terminate)
        return 1;
 }
 
-static char * find_name(const char *line, char *def, int p_value, int terminate)
+static char *find_name(const char *line, char *def, int p_value, int terminate)
 {
        int len;
        const char *start = line;
-       char *name;
 
        if (*line == '"') {
-               /* Proposed "new-style" GNU patch/diff format; see
+               struct strbuf name;
+
+               /*
+                * Proposed "new-style" GNU patch/diff format; see
                 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
                 */
-               name = unquote_c_style(line, NULL);
-               if (name) {
-                       char *cp = name;
-                       while (p_value) {
-                               cp = strchr(name, '/');
+               strbuf_init(&name, 0);
+               if (!unquote_c_style(&name, line, NULL)) {
+                       char *cp;
+
+                       for (cp = name.buf; p_value; p_value--) {
+                               cp = strchr(cp, '/');
                                if (!cp)
                                        break;
                                cp++;
-                               p_value--;
                        }
                        if (cp) {
                                /* name can later be freed, so we need
                                 * to memmove, not just return cp
                                 */
-                               memmove(name, cp, strlen(cp) + 1);
+                               strbuf_remove(&name, 0, cp - name.buf);
                                free(def);
-                               return name;
-                       }
-                       else {
-                               free(name);
-                               name = NULL;
+                               return strbuf_detach(&name, NULL);
                        }
                }
+               strbuf_release(&name);
        }
 
        for (;;) {
@@ -302,20 +288,60 @@ static char * find_name(const char *line, char *def, int p_value, int terminate)
                int deflen = strlen(def);
                if (deflen < len && !strncmp(start, def, deflen))
                        return def;
+               free(def);
        }
 
-       name = xmalloc(len + 1);
-       memcpy(name, start, len);
-       name[len] = 0;
-       free(def);
-       return name;
+       return xmemdupz(start, len);
+}
+
+static int count_slashes(const char *cp)
+{
+       int cnt = 0;
+       char ch;
+
+       while ((ch = *cp++))
+               if (ch == '/')
+                       cnt++;
+       return cnt;
+}
+
+/*
+ * Given the string after "--- " or "+++ ", guess the appropriate
+ * p_value for the given patch.
+ */
+static int guess_p_value(const char *nameline)
+{
+       char *name, *cp;
+       int val = -1;
+
+       if (is_dev_null(nameline))
+               return -1;
+       name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB);
+       if (!name)
+               return -1;
+       cp = strchr(name, '/');
+       if (!cp)
+               val = 0;
+       else if (prefix) {
+               /*
+                * Does it begin with "a/$our-prefix" and such?  Then this is
+                * very likely to apply to our directory.
+                */
+               if (!strncmp(name, prefix, prefix_length))
+                       val = count_slashes(prefix);
+               else {
+                       cp++;
+                       if (!strncmp(cp, prefix, prefix_length))
+                               val = count_slashes(prefix) + 1;
+               }
+       }
+       free(name);
+       return val;
 }
 
 /*
  * Get the name etc info from the --/+++ lines of a traditional patch header
  *
- * NOTE! This hardcodes "-p1" behaviour in filename detection.
- *
  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
@@ -326,6 +352,16 @@ static void parse_traditional_patch(const char *first, const char *second, struc
 
        first += 4;     /* skip "--- " */
        second += 4;    /* skip "+++ " */
+       if (!p_value_known) {
+               int p, q;
+               p = guess_p_value(first);
+               q = guess_p_value(second);
+               if (p < 0) p = q;
+               if (0 <= p && p == q) {
+                       p_value = p;
+                       p_value_known = 1;
+               }
+       }
        if (is_dev_null(first)) {
                patch->is_new = 1;
                patch->is_delete = 0;
@@ -362,7 +398,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 {
        if (!orig_name && !isnull)
-               return find_name(line, NULL, 1, TERM_TAB);
+               return find_name(line, NULL, p_value, TERM_TAB);
 
        if (orig_name) {
                int len;
@@ -372,7 +408,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
                len = strlen(name);
                if (isnull)
                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
-               another = find_name(line, NULL, 1, TERM_TAB);
+               another = find_name(line, NULL, p_value, TERM_TAB);
                if (!another || memcmp(another, name, len))
                        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
                free(another);
@@ -468,7 +504,8 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 
 static int gitdiff_index(const char *line, struct patch *patch)
 {
-       /* index line is N hexadecimal, "..", N hexadecimal,
+       /*
+        * index line is N hexadecimal, "..", N hexadecimal,
         * and optional space with octal mode.
         */
        const char *ptr, *eol;
@@ -519,7 +556,8 @@ static const char *stop_at_slash(const char *line, int llen)
        return NULL;
 }
 
-/* This is to extract the same name that appears on "diff --git"
+/*
+ * This is to extract the same name that appears on "diff --git"
  * line.  We do not find and return anything if it is a rename
  * patch, and it is OK because we will find the name elsewhere.
  * We need to reliably find name only when it is mode-change only,
@@ -528,100 +566,105 @@ static const char *stop_at_slash(const char *line, int llen)
  */
 static char *git_header_name(char *line, int llen)
 {
-       int len;
        const char *name;
        const char *second = NULL;
+       size_t len;
 
        line += strlen("diff --git ");
        llen -= strlen("diff --git ");
 
        if (*line == '"') {
                const char *cp;
-               char *first = unquote_c_style(line, &second);
-               if (!first)
-                       return NULL;
+               struct strbuf first;
+               struct strbuf sp;
+
+               strbuf_init(&first, 0);
+               strbuf_init(&sp, 0);
+
+               if (unquote_c_style(&first, line, &second))
+                       goto free_and_fail1;
 
                /* advance to the first slash */
-               cp = stop_at_slash(first, strlen(first));
-               if (!cp || cp == first) {
-                       /* we do not accept absolute paths */
-               free_first_and_fail:
-                       free(first);
-                       return NULL;
-               }
-               len = strlen(cp+1);
-               memmove(first, cp+1, len+1); /* including NUL */
+               cp = stop_at_slash(first.buf, first.len);
+               /* we do not accept absolute paths */
+               if (!cp || cp == first.buf)
+                       goto free_and_fail1;
+               strbuf_remove(&first, 0, cp + 1 - first.buf);
 
-               /* second points at one past closing dq of name.
+               /*
+                * second points at one past closing dq of name.
                 * find the second name.
                 */
                while ((second < line + llen) && isspace(*second))
                        second++;
 
                if (line + llen <= second)
-                       goto free_first_and_fail;
+                       goto free_and_fail1;
                if (*second == '"') {
-                       char *sp = unquote_c_style(second, NULL);
-                       if (!sp)
-                               goto free_first_and_fail;
-                       cp = stop_at_slash(sp, strlen(sp));
-                       if (!cp || cp == sp) {
-                       free_both_and_fail:
-                               free(sp);
-                               goto free_first_and_fail;
-                       }
+                       if (unquote_c_style(&sp, second, NULL))
+                               goto free_and_fail1;
+                       cp = stop_at_slash(sp.buf, sp.len);
+                       if (!cp || cp == sp.buf)
+                               goto free_and_fail1;
                        /* They must match, otherwise ignore */
-                       if (strcmp(cp+1, first))
-                               goto free_both_and_fail;
-                       free(sp);
-                       return first;
+                       if (strcmp(cp + 1, first.buf))
+                               goto free_and_fail1;
+                       strbuf_release(&sp);
+                       return strbuf_detach(&first, NULL);
                }
 
                /* unquoted second */
                cp = stop_at_slash(second, line + llen - second);
                if (!cp || cp == second)
-                       goto free_first_and_fail;
+                       goto free_and_fail1;
                cp++;
-               if (line + llen - cp != len + 1 ||
-                   memcmp(first, cp, len))
-                       goto free_first_and_fail;
-               return first;
+               if (line + llen - cp != first.len + 1 ||
+                   memcmp(first.buf, cp, first.len))
+                       goto free_and_fail1;
+               return strbuf_detach(&first, NULL);
+
+       free_and_fail1:
+               strbuf_release(&first);
+               strbuf_release(&sp);
+               return NULL;
        }
 
        /* unquoted first name */
        name = stop_at_slash(line, llen);
        if (!name || name == line)
                return NULL;
-
        name++;
 
-       /* since the first name is unquoted, a dq if exists must be
+       /*
+        * since the first name is unquoted, a dq if exists must be
         * the beginning of the second name.
         */
        for (second = name; second < line + llen; second++) {
                if (*second == '"') {
-                       const char *cp = second;
+                       struct strbuf sp;
                        const char *np;
-                       char *sp = unquote_c_style(second, NULL);
-
-                       if (!sp)
-                               return NULL;
-                       np = stop_at_slash(sp, strlen(sp));
-                       if (!np || np == sp) {
-                       free_second_and_fail:
-                               free(sp);
-                               return NULL;
-                       }
+
+                       strbuf_init(&sp, 0);
+                       if (unquote_c_style(&sp, second, NULL))
+                               goto free_and_fail2;
+
+                       np = stop_at_slash(sp.buf, sp.len);
+                       if (!np || np == sp.buf)
+                               goto free_and_fail2;
                        np++;
-                       len = strlen(np);
-                       if (len < cp - name &&
+
+                       len = sp.buf + sp.len - np;
+                       if (len < second - name &&
                            !strncmp(np, name, len) &&
                            isspace(name[len])) {
                                /* Good */
-                               memmove(sp, np, len + 1);
-                               return sp;
+                               strbuf_remove(&sp, 0, np - sp.buf);
+                               return strbuf_detach(&sp, NULL);
                        }
-                       goto free_second_and_fail;
+
+               free_and_fail2:
+                       strbuf_release(&sp);
+                       return NULL;
                }
        }
 
@@ -645,14 +688,10 @@ static char *git_header_name(char *line, int llen)
                                        break;
                        }
                        if (second[len] == '\n' && !memcmp(name, second, len)) {
-                               char *ret = xmalloc(len + 1);
-                               memcpy(ret, name, len);
-                               ret[len] = 0;
-                               return ret;
+                               return xmemdupz(name, len);
                        }
                }
        }
-       return NULL;
 }
 
 /* Verify that we recognize the lines following a git header */
@@ -728,7 +767,7 @@ static int parse_num(const char *line, unsigned long *p)
 }
 
 static int parse_range(const char *line, int len, int offset, const char *expect,
-                       unsigned long *p1, unsigned long *p2)
+                      unsigned long *p1, unsigned long *p2)
 {
        int digits, ex;
 
@@ -787,6 +826,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
 {
        unsigned long offset, len;
 
+       patch->is_toplevel_relative = 0;
        patch->is_rename = patch->is_copy = 0;
        patch->is_new = patch->is_delete = -1;
        patch->old_mode = patch->new_mode = 0;
@@ -831,18 +871,19 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
                                        die("git diff header lacks filename information (line %d)", linenr);
                                patch->old_name = patch->new_name = patch->def_name;
                        }
+                       patch->is_toplevel_relative = 1;
                        *hdrsize = git_hdr_len;
                        return offset;
                }
 
-               /** --- followed by +++ ? */
+               /* --- followed by +++ ? */
                if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
                        continue;
 
                /*
                 * We only accept unified patches, so we want it to
                 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
-                * minimum
+                * minimum ("@@ -0,0 +1 @@\n" is the shortest).
                 */
                nextlen = linelen(line + len, size - len);
                if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
@@ -857,56 +898,34 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
        return -1;
 }
 
-static void check_whitespace(const char *line, int len)
+static void check_whitespace(const char *line, int len, unsigned ws_rule)
 {
-       const char *err = "Adds trailing whitespace";
-       int seen_space = 0;
-       int i;
-
-       /*
-        * We know len is at least two, since we have a '+' and we
-        * checked that the last character was a '\n' before calling
-        * this function.  That is, an addition of an empty line would
-        * check the '+' here.  Sneaky...
-        */
-       if (isspace(line[len-2]))
-               goto error;
-
-       /*
-        * Make sure that there is no space followed by a tab in
-        * indentation.
-        */
-       err = "Space in indent is followed by a tab";
-       for (i = 1; i < len; i++) {
-               if (line[i] == '\t') {
-                       if (seen_space)
-                               goto error;
-               }
-               else if (line[i] == ' ')
-                       seen_space = 1;
-               else
-                       break;
-       }
-       return;
+       char *err;
+       unsigned result = check_and_emit_line(line + 1, len - 1, ws_rule,
+           NULL, NULL, NULL, NULL);
+       if (!result)
+               return;
 
- error:
        whitespace_error++;
        if (squelch_whitespace_errors &&
            squelch_whitespace_errors < whitespace_error)
                ;
-       else
-               fprintf(stderr, "%s.\n%s:%d:%.*s\n",
-                       err, patch_input_file, linenr, len-2, line+1);
+       else {
+               err = whitespace_error_string(result);
+               fprintf(stderr, "%s:%d: %s.\n%.*s\n",
+                    patch_input_file, linenr, err, len - 2, line + 1);
+               free(err);
+       }
 }
 
-
 /*
  * Parse a unified diff. Note that this really needs to parse each
  * fragment separately, since the only way to know the difference
  * between a "---" that is part of a patch, and a "---" that starts
  * the next patch is to look at the line counts..
  */
-static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
+static int parse_fragment(char *line, unsigned long size,
+                         struct patch *patch, struct fragment *fragment)
 {
        int added, deleted;
        int len = linelen(line, size), offset;
@@ -946,19 +965,24 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
                        trailing++;
                        break;
                case '-':
+                       if (apply_in_reverse &&
+                           ws_error_action != nowarn_ws_error)
+                               check_whitespace(line, len, patch->ws_rule);
                        deleted++;
                        oldlines--;
                        trailing = 0;
                        break;
                case '+':
-                       if (new_whitespace != nowarn_whitespace)
-                               check_whitespace(line, len);
+                       if (!apply_in_reverse &&
+                           ws_error_action != nowarn_ws_error)
+                               check_whitespace(line, len, patch->ws_rule);
                        added++;
                        newlines--;
                        trailing = 0;
                        break;
 
-                /* We allow "\ No newline at end of file". Depending
+               /*
+                * We allow "\ No newline at end of file". Depending
                  * on locale settings when the patch was produced we
                  * don't know what this line looks like. The only
                  * thing we do know is that it begins with "\ ".
@@ -976,7 +1000,8 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
        fragment->leading = leading;
        fragment->trailing = trailing;
 
-       /* If a fragment ends with an incomplete line, we failed to include
+       /*
+        * If a fragment ends with an incomplete line, we failed to include
         * it in the above loop because we hit oldlines == newlines == 0
         * before seeing it.
         */
@@ -1104,7 +1129,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
                                          int *status_p,
                                          int *used_p)
 {
-       /* Expect a line that begins with binary patch method ("literal"
+       /*
+        * Expect a line that begins with binary patch method ("literal"
         * or "delta"), followed by the length of data before deflating.
         * a sequence of 'length-byte' followed by base-85 encoded data
         * should follow, terminated by a newline.
@@ -1129,11 +1155,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 
        *status_p = 0;
 
-       if (!strncmp(buffer, "delta ", 6)) {
+       if (!prefixcmp(buffer, "delta ")) {
                patch_method = BINARY_DELTA_DEFLATED;
                origlen = strtoul(buffer + 6, NULL, 10);
        }
-       else if (!strncmp(buffer, "literal ", 8)) {
+       else if (!prefixcmp(buffer, "literal ")) {
                patch_method = BINARY_LITERAL_DEFLATED;
                origlen = strtoul(buffer + 8, NULL, 10);
        }
@@ -1153,7 +1179,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
                        size--;
                        break;
                }
-               /* Minimum line is "A00000\n" which is 7-byte long,
+               /*
+                * Minimum line is "A00000\n" which is 7-byte long,
                 * and the line length must be multiple of 5 plus 2.
                 */
                if ((llen < 7) || (llen-2) % 5)
@@ -1204,7 +1231,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 
 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 {
-       /* We have read "GIT binary patch\n"; what follows is a line
+       /*
+        * We have read "GIT binary patch\n"; what follows is a line
         * that says the patch method (currently, either "literal" or
         * "delta") and the length of data before deflating; a
         * sequence of 'length-byte' followed by base-85 encoded data
@@ -1234,7 +1262,8 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
        if (reverse)
                used += used_1;
        else if (status) {
-               /* not having reverse hunk is not an error, but having
+               /*
+                * Not having reverse hunk is not an error, but having
                 * a corrupt reverse hunk is.
                 */
                free((void*) forward->patch);
@@ -1255,7 +1284,12 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
        if (offset < 0)
                return offset;
 
-       patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
+       patch->ws_rule = whitespace_rule(patch->new_name
+                                        ? patch->new_name
+                                        : patch->old_name);
+
+       patchsize = parse_single_patch(buffer + offset + hdrsize,
+                                      size - offset - hdrsize, patch);
 
        if (!patchsize) {
                static const char *binhdr[] = {
@@ -1331,96 +1365,81 @@ static void reverse_patches(struct patch *p)
        }
 }
 
-static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
-static const char minuses[]= "----------------------------------------------------------------------";
+static const char pluses[] =
+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
+static const char minuses[]=
+"----------------------------------------------------------------------";
 
 static void show_stats(struct patch *patch)
 {
-       const char *prefix = "";
-       char *name = patch->new_name;
-       char *qname = NULL;
-       int len, max, add, del, total;
+       struct strbuf qname;
+       char *cp = patch->new_name ? patch->new_name : patch->old_name;
+       int max, add, del;
 
-       if (!name)
-               name = patch->old_name;
-
-       if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
-               qname = xmalloc(len + 1);
-               quote_c_style(name, qname, NULL, 0);
-               name = qname;
-       }
+       strbuf_init(&qname, 0);
+       quote_c_style(cp, &qname, NULL, 0);
 
        /*
         * "scale" the filename
         */
-       len = strlen(name);
        max = max_len;
        if (max > 50)
                max = 50;
-       if (len > max) {
-               char *slash;
-               prefix = "...";
-               max -= 3;
-               name += len - max;
-               slash = strchr(name, '/');
-               if (slash)
-                       name = slash;
+
+       if (qname.len > max) {
+               cp = strchr(qname.buf + qname.len + 3 - max, '/');
+               if (!cp)
+                       cp = qname.buf + qname.len + 3 - max;
+               strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
+       }
+
+       if (patch->is_binary) {
+               printf(" %-*s |  Bin\n", max, qname.buf);
+               strbuf_release(&qname);
+               return;
        }
-       len = max;
+
+       printf(" %-*s |", max, qname.buf);
+       strbuf_release(&qname);
 
        /*
         * scale the add/delete
         */
-       max = max_change;
-       if (max + len > 70)
-               max = 70 - len;
-
+       max = max + max_change > 70 ? 70 - max : max_change;
        add = patch->lines_added;
        del = patch->lines_deleted;
-       total = add + del;
 
        if (max_change > 0) {
-               total = (total * max + max_change / 2) / max_change;
+               int total = ((add + del) * max + max_change / 2) / max_change;
                add = (add * max + max_change / 2) / max_change;
                del = total - add;
        }
-       if (patch->is_binary)
-               printf(" %s%-*s |  Bin\n", prefix, len, name);
-       else
-               printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
-                      len, name, patch->lines_added + patch->lines_deleted,
-                      add, pluses, del, minuses);
-       free(qname);
+       printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
+               add, pluses, del, minuses);
 }
 
-static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
+static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
 {
-       int fd;
-       unsigned long got;
-
        switch (st->st_mode & S_IFMT) {
        case S_IFLNK:
-               return readlink(path, buf, size);
+               strbuf_grow(buf, st->st_size);
+               if (readlink(path, buf->buf, st->st_size) != st->st_size)
+                       return -1;
+               strbuf_setlen(buf, st->st_size);
+               return 0;
        case S_IFREG:
-               fd = open(path, O_RDONLY);
-               if (fd < 0)
-                       return error("unable to open %s", path);
-               got = 0;
-               for (;;) {
-                       int ret = xread(fd, (char *) buf + got, size - got);
-                       if (ret <= 0)
-                               break;
-                       got += ret;
-               }
-               close(fd);
-               return got;
-
+               if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
+                       return error("unable to open or read %s", path);
+               convert_to_git(path, buf->buf, buf->len, buf, 0);
+               return 0;
        default:
                return -1;
        }
 }
 
-static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
+static int find_offset(const char *buf, unsigned long size,
+                      const char *fragment, unsigned long fragsize,
+                      int line, int *lines)
 {
        int i;
        unsigned long start, backwards, forwards;
@@ -1442,7 +1461,8 @@ static int find_offset(const char *buf, unsigned long size, const char *fragment
        }
 
        /* Exact line number? */
-       if (!memcmp(buf + start, fragment, fragsize))
+       if ((start + fragsize <= size) &&
+           !memcmp(buf + start, fragment, fragsize))
                return start;
 
        /*
@@ -1518,15 +1538,11 @@ static void remove_last_line(const char **rbuf, int *rsize)
        *rsize = offset + 1;
 }
 
-struct buffer_desc {
-       char *buffer;
-       unsigned long size;
-       unsigned long alloc;
-};
-
-static int apply_line(char *output, const char *patch, int plen)
+static int apply_line(char *output, const char *patch, int plen,
+                     unsigned ws_rule)
 {
-       /* plen is number of bytes to be copied from patch,
+       /*
+        * plen is number of bytes to be copied from patch,
         * starting at patch+1 (patch[0] is '+').  Typically
         * patch[plen] is '\n', unless this is the incomplete
         * last line.
@@ -1534,18 +1550,22 @@ static int apply_line(char *output, const char *patch, int plen)
        int i;
        int add_nl_to_tail = 0;
        int fixed = 0;
-       int last_tab_in_indent = -1;
-       int last_space_in_indent = -1;
+       int last_tab_in_indent = 0;
+       int last_space_in_indent = 0;
        int need_fix_leading_space = 0;
        char *buf;
 
-       if ((new_whitespace != strip_whitespace) || !whitespace_error ||
+       if ((ws_error_action != correct_ws_error) || !whitespace_error ||
            *patch != '+') {
                memcpy(output, patch + 1, plen);
                return plen;
        }
 
-       if (1 < plen && isspace(patch[plen-1])) {
+       /*
+        * Strip trailing whitespace
+        */
+       if ((ws_rule & WS_TRAILING_SPACE) &&
+           (1 < plen && isspace(patch[plen-1]))) {
                if (patch[plen] == '\n')
                        add_nl_to_tail = 1;
                plen--;
@@ -1554,33 +1574,60 @@ static int apply_line(char *output, const char *patch, int plen)
                fixed = 1;
        }
 
+       /*
+        * Check leading whitespaces (indent)
+        */
        for (i = 1; i < plen; i++) {
                char ch = patch[i];
                if (ch == '\t') {
                        last_tab_in_indent = i;
-                       if (0 <= last_space_in_indent)
+                       if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
+                           0 < last_space_in_indent)
+                           need_fix_leading_space = 1;
+               } else if (ch == ' ') {
+                       last_space_in_indent = i;
+                       if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
+                           8 <= i - last_tab_in_indent)
                                need_fix_leading_space = 1;
                }
-               else if (ch == ' ')
-                       last_space_in_indent = i;
                else
                        break;
        }
 
        buf = output;
        if (need_fix_leading_space) {
-               /* between patch[1..last_tab_in_indent] strip the
-                * funny spaces, updating them to tab as needed.
+               int consecutive_spaces = 0;
+               int last = last_tab_in_indent + 1;
+
+               if (ws_rule & WS_INDENT_WITH_NON_TAB) {
+                       /* have "last" point at one past the indent */
+                       if (last_tab_in_indent < last_space_in_indent)
+                               last = last_space_in_indent + 1;
+                       else
+                               last = last_tab_in_indent + 1;
+               }
+
+               /*
+                * between patch[1..last], strip the funny spaces,
+                * updating them to tab as needed.
                 */
-               for (i = 1; i < last_tab_in_indent; i++, plen--) {
+               for (i = 1; i < last; i++, plen--) {
                        char ch = patch[i];
-                       if (ch != ' ')
+                       if (ch != ' ') {
+                               consecutive_spaces = 0;
                                *output++ = ch;
-                       else if ((i % 8) == 0)
-                               *output++ = '\t';
+                       } else {
+                               consecutive_spaces++;
+                               if (consecutive_spaces == 8) {
+                                       *output++ = '\t';
+                                       consecutive_spaces = 0;
+                               }
+                       }
                }
+               while (0 < consecutive_spaces--)
+                       *output++ = ' ';
                fixed = 1;
-               i = last_tab_in_indent;
+               i = last;
        }
        else
                i = 1;
@@ -1589,20 +1636,21 @@ static int apply_line(char *output, const char *patch, int plen)
        if (add_nl_to_tail)
                output[plen++] = '\n';
        if (fixed)
-               applied_after_stripping++;
+               applied_after_fixing_ws++;
        return output + plen - buf;
 }
 
-static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)
+static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
+                             int inaccurate_eof, unsigned ws_rule)
 {
        int match_beginning, match_end;
-       char *buf = desc->buffer;
        const char *patch = frag->patch;
        int offset, size = frag->size;
        char *old = xmalloc(size);
        char *new = xmalloc(size);
        const char *oldlines, *newlines;
        int oldsize = 0, newsize = 0;
+       int new_blank_lines_at_end = 0;
        unsigned long leading, trailing;
        int pos, lines;
 
@@ -1610,6 +1658,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                char first;
                int len = linelen(patch, size);
                int plen;
+               int added_blank_line = 0;
 
                if (!len)
                        break;
@@ -1631,6 +1680,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                        else if (first == '+')
                                first = '-';
                }
+
                switch (first) {
                case '\n':
                        /* Newer GNU diff, empty context line */
@@ -1648,22 +1698,34 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                                break;
                /* Fall-through for ' ' */
                case '+':
-                       if (first != '+' || !no_add)
-                               newsize += apply_line(new + newsize, patch,
-                                                     plen);
+                       if (first != '+' || !no_add) {
+                               int added = apply_line(new + newsize, patch,
+                                                      plen, ws_rule);
+                               newsize += added;
+                               if (first == '+' &&
+                                   added == 1 && new[newsize-1] == '\n')
+                                       added_blank_line = 1;
+                       }
                        break;
                case '@': case '\\':
                        /* Ignore it, we already handled it */
                        break;
                default:
+                       if (apply_verbosely)
+                               error("invalid start of line: '%c'", first);
                        return -1;
                }
+               if (added_blank_line)
+                       new_blank_lines_at_end++;
+               else
+                       new_blank_lines_at_end = 0;
                patch += len;
                size -= len;
        }
 
-       if (inaccurate_eof && oldsize > 0 && old[oldsize - 1] == '\n' &&
-                       newsize > 0 && new[newsize - 1] == '\n') {
+       if (inaccurate_eof &&
+           oldsize > 0 && old[oldsize - 1] == '\n' &&
+           newsize > 0 && new[newsize - 1] == '\n') {
                oldsize--;
                newsize--;
        }
@@ -1693,16 +1755,16 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
        lines = 0;
        pos = frag->newpos;
        for (;;) {
-               offset = find_offset(buf, desc->size,
+               offset = find_offset(buf->buf, buf->len,
                                     oldlines, oldsize, pos, &lines);
-               if (match_end && offset + oldsize != desc->size)
+               if (match_end && offset + oldsize != buf->len)
                        offset = -1;
                if (match_beginning && offset)
                        offset = -1;
                if (offset >= 0) {
-                       int diff = newsize - oldsize;
-                       unsigned long size = desc->size + diff;
-                       unsigned long alloc = desc->alloc;
+                       if (ws_error_action == correct_ws_error &&
+                           (buf->len - oldsize - offset == 0)) /* end of file? */
+                               newsize -= new_blank_lines_at_end;
 
                        /* Warn if it was necessary to reduce the number
                         * of context lines.
@@ -1713,19 +1775,8 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                                        " to apply fragment at %d\n",
                                        leading, trailing, pos + lines);
 
-                       if (size > alloc) {
-                               alloc = size + 8192;
-                               desc->alloc = alloc;
-                               buf = xrealloc(buf, alloc);
-                               desc->buffer = buf;
-                       }
-                       desc->size = size;
-                       memmove(buf + offset + newsize,
-                               buf + offset + oldsize,
-                               size - offset - newsize);
-                       memcpy(buf + offset, newlines, newsize);
+                       strbuf_splice(buf, offset, oldsize, newlines, newsize);
                        offset = 0;
-
                        break;
                }
 
@@ -1736,9 +1787,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                        match_beginning = match_end = 0;
                        continue;
                }
-               /* Reduce the number of context lines
-                * Reduce both leading and trailing if they are equal
-                * otherwise just reduce the larger context.
+               /*
+                * Reduce the number of context lines; reduce both
+                * leading and trailing if they are equal otherwise
+                * just reduce the larger context.
                 */
                if (leading >= trailing) {
                        remove_first_line(&oldlines, &oldsize);
@@ -1753,17 +1805,19 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
                }
        }
 
+       if (offset && apply_verbosely)
+               error("while searching for:\n%.*s", oldsize, oldlines);
+
        free(old);
        free(new);
        return offset;
 }
 
-static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
+static int apply_binary_fragment(struct strbuf *buf, struct patch *patch)
 {
-       unsigned long dst_size;
        struct fragment *fragment = patch->fragments;
-       void *data;
-       void *result;
+       unsigned long len;
+       void *dst;
 
        /* Binary patch is irreversible without the optional second hunk */
        if (apply_in_reverse) {
@@ -1774,34 +1828,30 @@ static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
                                     ? patch->new_name : patch->old_name);
                fragment = fragment->next;
        }
-       data = (void*) fragment->patch;
        switch (fragment->binary_patch_method) {
        case BINARY_DELTA_DEFLATED:
-               result = patch_delta(desc->buffer, desc->size,
-                                    data,
-                                    fragment->size,
-                                    &dst_size);
-               free(desc->buffer);
-               desc->buffer = result;
-               break;
+               dst = patch_delta(buf->buf, buf->len, fragment->patch,
+                                 fragment->size, &len);
+               if (!dst)
+                       return -1;
+               /* XXX patch_delta NUL-terminates */
+               strbuf_attach(buf, dst, len, len + 1);
+               return 0;
        case BINARY_LITERAL_DEFLATED:
-               free(desc->buffer);
-               desc->buffer = data;
-               dst_size = fragment->size;
-               break;
+               strbuf_reset(buf);
+               strbuf_add(buf, fragment->patch, fragment->size);
+               return 0;
        }
-       if (!desc->buffer)
-               return -1;
-       desc->size = desc->alloc = dst_size;
-       return 0;
+       return -1;
 }
 
-static int apply_binary(struct buffer_desc *desc, struct patch *patch)
+static int apply_binary(struct strbuf *buf, struct patch *patch)
 {
        const char *name = patch->old_name ? patch->old_name : patch->new_name;
        unsigned char sha1[20];
 
-       /* For safety, we require patch index line to contain
+       /*
+        * For safety, we require patch index line to contain
         * full 40-byte textual SHA1 for old and new, at least for now.
         */
        if (strlen(patch->old_sha1_prefix) != 40 ||
@@ -1812,10 +1862,11 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
                             "without full index line", name);
 
        if (patch->old_name) {
-               /* See if the old one matches what the patch
+               /*
+                * See if the old one matches what the patch
                 * applies to.
                 */
-               hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
+               hash_sha1_file(buf->buf, buf->len, blob_type, sha1);
                if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
                        return error("the patch applies to '%s' (%s), "
                                     "which does not match the "
@@ -1824,60 +1875,62 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
        }
        else {
                /* Otherwise, the old one must be empty. */
-               if (desc->size)
+               if (buf->len)
                        return error("the patch applies to an empty "
                                     "'%s' but it is not empty", name);
        }
 
        get_sha1_hex(patch->new_sha1_prefix, sha1);
        if (is_null_sha1(sha1)) {
-               free(desc->buffer);
-               desc->alloc = desc->size = 0;
-               desc->buffer = NULL;
+               strbuf_release(buf);
                return 0; /* deletion patch */
        }
 
        if (has_sha1_file(sha1)) {
                /* We already have the postimage */
-               char type[10];
+               enum object_type type;
                unsigned long size;
+               char *result;
 
-               free(desc->buffer);
-               desc->buffer = read_sha1_file(sha1, type, &size);
-               if (!desc->buffer)
+               result = read_sha1_file(sha1, &type, &size);
+               if (!result)
                        return error("the necessary postimage %s for "
                                     "'%s' cannot be read",
                                     patch->new_sha1_prefix, name);
-               desc->alloc = desc->size = size;
-       }
-       else {
-               /* We have verified desc matches the preimage;
+               /* XXX read_sha1_file NUL-terminates */
+               strbuf_attach(buf, result, size, size + 1);
+       } else {
+               /*
+                * We have verified buf matches the preimage;
                 * apply the patch data to it, which is stored
                 * in the patch->fragments->{patch,size}.
                 */
-               if (apply_binary_fragment(desc, patch))
+               if (apply_binary_fragment(buf, patch))
                        return error("binary patch does not apply to '%s'",
                                     name);
 
                /* verify that the result matches */
-               hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
+               hash_sha1_file(buf->buf, buf->len, blob_type, sha1);
                if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
-                       return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
+                       return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",
+                               name, patch->new_sha1_prefix, sha1_to_hex(sha1));
        }
 
        return 0;
 }
 
-static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
+static int apply_fragments(struct strbuf *buf, struct patch *patch)
 {
        struct fragment *frag = patch->fragments;
        const char *name = patch->old_name ? patch->old_name : patch->new_name;
+       unsigned ws_rule = patch->ws_rule;
+       unsigned inaccurate_eof = patch->inaccurate_eof;
 
        if (patch->is_binary)
-               return apply_binary(desc, patch);
+               return apply_binary(buf, patch);
 
        while (frag) {
-               if (apply_one_fragment(desc, frag, patch->inaccurate_eof)) {
+               if (apply_one_fragment(buf, frag, inaccurate_eof, ws_rule)) {
                        error("patch failed: %s:%ld", name, frag->oldpos);
                        if (!apply_with_reject)
                                return -1;
@@ -1888,47 +1941,56 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
        return 0;
 }
 
+static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
+{
+       if (!ce)
+               return 0;
+
+       if (S_ISGITLINK(ce->ce_mode)) {
+               strbuf_grow(buf, 100);
+               strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
+       } else {
+               enum object_type type;
+               unsigned long sz;
+               char *result;
+
+               result = read_sha1_file(ce->sha1, &type, &sz);
+               if (!result)
+                       return -1;
+               /* XXX read_sha1_file NUL-terminates */
+               strbuf_attach(buf, result, sz, sz + 1);
+       }
+       return 0;
+}
+
 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
 {
-       char *buf;
-       unsigned long size, alloc;
-       struct buffer_desc desc;
+       struct strbuf buf;
 
-       size = 0;
-       alloc = 0;
-       buf = NULL;
+       strbuf_init(&buf, 0);
        if (cached) {
-               if (ce) {
-                       char type[20];
-                       buf = read_sha1_file(ce->sha1, type, &size);
-                       if (!buf)
-                               return error("read of %s failed",
-                                            patch->old_name);
-                       alloc = size;
-               }
-       }
-       else if (patch->old_name) {
-               size = st->st_size;
-               alloc = size + 8192;
-               buf = xmalloc(alloc);
-               if (read_old_data(st, patch->old_name, buf, alloc) != size)
+               if (read_file_or_gitlink(ce, &buf))
                        return error("read of %s failed", patch->old_name);
+       } else if (patch->old_name) {
+               if (S_ISGITLINK(patch->old_mode)) {
+                       if (ce) {
+                               read_file_or_gitlink(ce, &buf);
+                       } else {
+                               /*
+                                * There is no way to apply subproject
+                                * patch without looking at the index.
+                                */
+                               patch->fragments = NULL;
+                       }
+               } else {
+                       if (read_old_data(st, patch->old_name, &buf))
+                               return error("read of %s failed", patch->old_name);
+               }
        }
 
-       desc.size = size;
-       desc.alloc = alloc;
-       desc.buffer = buf;
-
-       if (apply_fragments(&desc, patch) < 0)
+       if (apply_fragments(&buf, patch) < 0)
                return -1; /* note with --reject this succeeds. */
-
-       /* NUL terminate the result */
-       if (desc.alloc <= desc.size)
-               desc.buffer = xrealloc(desc.buffer, desc.size + 1);
-       desc.buffer[desc.size] = 0;
-
-       patch->result = desc.buffer;
-       patch->resultsize = desc.size;
+       patch->result = strbuf_detach(&buf, &patch->resultsize);
 
        if (0 < patch->is_delete && patch->resultsize)
                return error("removal patch leaves file contents");
@@ -1936,6 +1998,39 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
        return 0;
 }
 
+static int check_to_create_blob(const char *new_name, int ok_if_exists)
+{
+       struct stat nst;
+       if (!lstat(new_name, &nst)) {
+               if (S_ISDIR(nst.st_mode) || ok_if_exists)
+                       return 0;
+               /*
+                * A leading component of new_name might be a symlink
+                * that is going to be removed with this patch, but
+                * still pointing at somewhere that has the path.
+                * In such a case, path "new_name" does not exist as
+                * far as git is concerned.
+                */
+               if (has_symlink_leading_path(new_name, NULL))
+                       return 0;
+
+               return error("%s: already exists in working directory", new_name);
+       }
+       else if ((errno != ENOENT) && (errno != ENOTDIR))
+               return error("%s: %s", new_name, strerror(errno));
+       return 0;
+}
+
+static int verify_index_match(struct cache_entry *ce, struct stat *st)
+{
+       if (S_ISGITLINK(ce->ce_mode)) {
+               if (!S_ISDIR(st->st_mode))
+                       return -1;
+               return 0;
+       }
+       return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
+}
+
 static int check_patch(struct patch *patch, struct patch *prev_patch)
 {
        struct stat st;
@@ -1946,8 +2041,14 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
        int ok_if_exists;
 
        patch->rejected = 1; /* we will drop this after we succeed */
+
+       /*
+        * Make sure that we do not have local modifications from the
+        * index when we are looking at the index.  Also make sure
+        * we have the preimage file to be patched in the work tree,
+        * unless --cached, which tells git to apply only in the index.
+        */
        if (old_name) {
-               int changed = 0;
                int stat_ret = 0;
                unsigned st_mode = 0;
 
@@ -1977,19 +2078,16 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
                                    lstat(old_name, &st))
                                        return -1;
                        }
-                       if (!cached)
-                               changed = ce_match_stat(ce, &st, 1);
-                       if (changed)
+                       if (!cached && verify_index_match(ce, &st))
                                return error("%s: does not match index",
                                             old_name);
                        if (cached)
-                               st_mode = ntohl(ce->ce_mode);
-               }
-               else if (stat_ret < 0)
+                               st_mode = ce->ce_mode;
+               } else if (stat_ret < 0)
                        return error("%s: %s", old_name, strerror(errno));
 
                if (!cached)
-                       st_mode = ntohl(ce_mode_from_stat(ce, st.st_mode));
+                       st_mode = ce_mode_from_stat(ce, st.st_mode);
 
                if (patch->is_new < 0)
                        patch->is_new = 0;
@@ -2004,7 +2102,8 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
 
        if (new_name && prev_patch && 0 < prev_patch->is_delete &&
            !strcmp(prev_patch->old_name, new_name))
-               /* A type-change diff is always split into a patch to
+               /*
+                * A type-change diff is always split into a patch to
                 * delete old, immediately followed by a patch to
                 * create new (see diff.c::run_diff()); in such a case
                 * it is Ok that the entry to be deleted by the
@@ -2022,15 +2121,9 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
                    !ok_if_exists)
                        return error("%s: already exists in index", new_name);
                if (!cached) {
-                       struct stat nst;
-                       if (!lstat(new_name, &nst)) {
-                               if (S_ISDIR(nst.st_mode) || ok_if_exists)
-                                       ; /* ok */
-                               else
-                                       return error("%s: already exists in working directory", new_name);
-                       }
-                       else if ((errno != ENOENT) && (errno != ENOTDIR))
-                               return error("%s: %s", new_name, strerror(errno));
+                       int err = check_to_create_blob(new_name, ok_if_exists);
+                       if (err)
+                               return err;
                }
                if (!patch->new_mode) {
                        if (0 < patch->is_new)
@@ -2071,9 +2164,26 @@ static int check_patch_list(struct patch *patch)
        return err;
 }
 
-static void show_index_list(struct patch *list)
+/* This function tries to read the sha1 from the current index */
+static int get_current_sha1(const char *path, unsigned char *sha1)
+{
+       int pos;
+
+       if (read_cache() < 0)
+               return -1;
+       pos = cache_name_pos(path, strlen(path));
+       if (pos < 0)
+               return -1;
+       hashcpy(sha1, active_cache[pos]->sha1);
+       return 0;
+}
+
+/* Build an index that contains the just the files needed for a 3way merge */
+static void build_fake_ancestor(struct patch *list, const char *filename)
 {
        struct patch *patch;
+       struct index_state result = { 0 };
+       int fd;
 
        /* Once we start supporting the reverse patch, it may be
         * worth showing the new sha1 prefix, but until then...
@@ -2081,24 +2191,36 @@ static void show_index_list(struct patch *list)
        for (patch = list; patch; patch = patch->next) {
                const unsigned char *sha1_ptr;
                unsigned char sha1[20];
+               struct cache_entry *ce;
                const char *name;
 
                name = patch->old_name ? patch->old_name : patch->new_name;
                if (0 < patch->is_new)
-                       sha1_ptr = null_sha1;
+                       continue;
                else if (get_sha1(patch->old_sha1_prefix, sha1))
-                       die("sha1 information is lacking or useless (%s).",
-                           name);
+                       /* git diff has no index line for mode/type changes */
+                       if (!patch->lines_added && !patch->lines_deleted) {
+                               if (get_current_sha1(patch->new_name, sha1) ||
+                                   get_current_sha1(patch->old_name, sha1))
+                                       die("mode change for %s, which is not "
+                                               "in current HEAD", name);
+                               sha1_ptr = sha1;
+                       } else
+                               die("sha1 information is lacking or useless "
+                                       "(%s).", name);
                else
                        sha1_ptr = sha1;
 
-               printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
-               if (line_termination && quote_c_style(name, NULL, NULL, 0))
-                       quote_c_style(name, NULL, stdout, 0);
-               else
-                       fputs(name, stdout);
-               putchar(line_termination);
+               ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
+               if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
+                       die ("Could not add %s to temporary index", name);
        }
+
+       fd = open(filename, O_WRONLY | O_CREAT, 0666);
+       if (fd < 0 || write_index(&result, fd) || close(fd))
+               die ("Could not write temporary index to %s", filename);
+
+       discard_index(&result);
 }
 
 static void stat_patch_list(struct patch *patch)
@@ -2123,13 +2245,8 @@ static void numstat_patch_list(struct patch *patch)
                if (patch->is_binary)
                        printf("-\t-\t");
                else
-                       printf("%d\t%d\t",
-                              patch->lines_added, patch->lines_deleted);
-               if (line_termination && quote_c_style(name, NULL, NULL, 0))
-                       quote_c_style(name, NULL, stdout, 0);
-               else
-                       fputs(name, stdout);
-               putchar(line_termination);
+                       printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
+               write_name_quoted(name, stdout, line_termination);
        }
 }
 
@@ -2233,15 +2350,18 @@ static void patch_stats(struct patch *patch)
        }
 }
 
-static void remove_file(struct patch *patch)
+static void remove_file(struct patch *patch, int rmdir_empty)
 {
-       if (write_index) {
+       if (update_index) {
                if (remove_file_from_cache(patch->old_name) < 0)
                        die("unable to remove %s from index", patch->old_name);
-               cache_tree_invalidate_path(active_cache_tree, patch->old_name);
        }
        if (!cached) {
-               if (!unlink(patch->old_name)) {
+               if (S_ISGITLINK(patch->old_mode)) {
+                       if (rmdir(patch->old_name))
+                               warning("unable to remove submodule %s",
+                                       patch->old_name);
+               } else if (!unlink(patch->old_name) && rmdir_empty) {
                        char *name = xstrdup(patch->old_name);
                        char *end = strrchr(name, '/');
                        while (end) {
@@ -2262,20 +2382,28 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
        int namelen = strlen(path);
        unsigned ce_size = cache_entry_size(namelen);
 
-       if (!write_index)
+       if (!update_index)
                return;
 
        ce = xcalloc(1, ce_size);
        memcpy(ce->name, path, namelen);
        ce->ce_mode = create_ce_mode(mode);
-       ce->ce_flags = htons(namelen);
-       if (!cached) {
-               if (lstat(path, &st) < 0)
-                       die("unable to stat newly created file %s", path);
-               fill_stat_cache_info(ce, &st);
+       ce->ce_flags = namelen;
+       if (S_ISGITLINK(mode)) {
+               const char *s = buf;
+
+               if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
+                       die("corrupt patch for subproject %s", path);
+       } else {
+               if (!cached) {
+                       if (lstat(path, &st) < 0)
+                               die("unable to stat newly created file %s",
+                                   path);
+                       fill_stat_cache_info(ce, &st);
+               }
+               if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
+                       die("unable to create backing store for newly created file %s", path);
        }
-       if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-               die("unable to create backing store for newly created file %s", path);
        if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
                die("unable to add cache entry for %s", path);
 }
@@ -2283,24 +2411,33 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
 {
        int fd;
+       struct strbuf nbuf;
 
-       if (S_ISLNK(mode))
+       if (S_ISGITLINK(mode)) {
+               struct stat st;
+               if (!lstat(path, &st) && S_ISDIR(st.st_mode))
+                       return 0;
+               return mkdir(path, 0777);
+       }
+
+       if (has_symlinks && S_ISLNK(mode))
                /* Although buf:size is counted string, it also is NUL
                 * terminated.
                 */
                return symlink(buf, path);
+
        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
        if (fd < 0)
                return -1;
-       while (size) {
-               int written = xwrite(fd, buf, size);
-               if (written < 0)
-                       die("writing file %s: %s", path, strerror(errno));
-               if (!written)
-                       die("out of space writing file %s", path);
-               buf += written;
-               size -= written;
+
+       strbuf_init(&nbuf, 0);
+       if (convert_to_working_tree(path, buf, size, &nbuf)) {
+               size = nbuf.len;
+               buf  = nbuf.buf;
        }
+       write_or_die(fd, buf, size);
+       strbuf_release(&nbuf);
+
        if (close(fd) < 0)
                die("closing file %s: %s", path, strerror(errno));
        return 0;
@@ -2330,8 +2467,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
                 * used to be.
                 */
                struct stat st;
-               errno = 0;
-               if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
+               if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
                        errno = EEXIST;
        }
 
@@ -2366,7 +2502,6 @@ static void create_file(struct patch *patch)
                mode = S_IFREG | 0644;
        create_one_file(path, mode, buf, size);
        add_index_file(path, mode, buf, size);
-       cache_tree_invalidate_path(active_cache_tree, path);
 }
 
 /* phase zero is to remove, phase one is to create */
@@ -2374,7 +2509,7 @@ static void write_out_one_result(struct patch *patch, int phase)
 {
        if (patch->is_delete > 0) {
                if (phase == 0)
-                       remove_file(patch);
+                       remove_file(patch, 1);
                return;
        }
        if (patch->is_new > 0 || patch->is_copy) {
@@ -2387,7 +2522,7 @@ static void write_out_one_result(struct patch *patch, int phase)
         * thing: remove the old, write the new
         */
        if (phase == 0)
-               remove_file(patch);
+               remove_file(patch, patch->is_rename);
        if (phase == 1)
                create_file(patch);
 }
@@ -2509,48 +2644,76 @@ static int use_patch(struct patch *p)
        return 1;
 }
 
+static void prefix_one(char **name)
+{
+       char *old_name = *name;
+       if (!old_name)
+               return;
+       *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
+       free(old_name);
+}
+
+static void prefix_patches(struct patch *p)
+{
+       if (!prefix || p->is_toplevel_relative)
+               return;
+       for ( ; p; p = p->next) {
+               if (p->new_name == p->old_name) {
+                       char *prefixed = p->new_name;
+                       prefix_one(&prefixed);
+                       p->new_name = p->old_name = prefixed;
+               }
+               else {
+                       prefix_one(&p->new_name);
+                       prefix_one(&p->old_name);
+               }
+       }
+}
+
 static int apply_patch(int fd, const char *filename, int inaccurate_eof)
 {
-       unsigned long offset, size;
-       char *buffer = read_patch_file(fd, &size);
+       size_t offset;
+       struct strbuf buf;
        struct patch *list = NULL, **listp = &list;
        int skipped_patch = 0;
 
+       strbuf_init(&buf, 0);
        patch_input_file = filename;
-       if (!buffer)
-               return -1;
+       read_patch_file(&buf, fd);
        offset = 0;
-       while (size > 0) {
+       while (offset < buf.len) {
                struct patch *patch;
                int nr;
 
                patch = xcalloc(1, sizeof(*patch));
                patch->inaccurate_eof = inaccurate_eof;
-               nr = parse_chunk(buffer + offset, size, patch);
+               nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
                if (nr < 0)
                        break;
                if (apply_in_reverse)
                        reverse_patches(patch);
+               if (prefix)
+                       prefix_patches(patch);
                if (use_patch(patch)) {
                        patch_stats(patch);
                        *listp = patch;
                        listp = &patch->next;
-               } else {
+               }
+               else {
                        /* perhaps free it a bit better? */
                        free(patch);
                        skipped_patch++;
                }
                offset += nr;
-               size -= nr;
        }
 
-       if (whitespace_error && (new_whitespace == error_on_whitespace))
+       if (whitespace_error && (ws_error_action == die_on_ws_error))
                apply = 0;
 
-       write_index = check_index && apply;
-       if (write_index && newfd < 0)
-               newfd = hold_lock_file_for_update(&lock_file,
-                                                 get_index_file(), 1);
+       update_index = check_index && apply;
+       if (update_index && newfd < 0)
+               newfd = hold_locked_index(&lock_file, 1);
+
        if (check_index) {
                if (read_cache() < 0)
                        die("unable to read index file");
@@ -2564,8 +2727,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
        if (apply && write_out_results(list, skipped_patch))
                exit(1);
 
-       if (show_index_info)
-               show_index_list(list);
+       if (fake_ancestor)
+               build_fake_ancestor(list, fake_ancestor);
 
        if (diffstat)
                stat_patch_list(list);
@@ -2576,13 +2739,15 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
        if (summary)
                summary_patch_list(list);
 
-       free(buffer);
+       strbuf_release(&buf);
        return 0;
 }
 
 static int git_apply_config(const char *var, const char *value)
 {
        if (!strcmp(var, "apply.whitespace")) {
+               if (!value)
+                       return config_error_nonbool(var);
                apply_default_whitespace = xstrdup(value);
                return 0;
        }
@@ -2596,9 +2761,16 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
        int read_stdin = 1;
        int inaccurate_eof = 0;
        int errs = 0;
+       int is_not_gitdir = 0;
 
        const char *whitespace_option = NULL;
 
+       prefix = setup_git_directory_gently(&is_not_gitdir);
+       prefix_length = prefix ? strlen(prefix) : 0;
+       git_config(git_apply_config);
+       if (apply_default_whitespace)
+               parse_whitespace_option(apply_default_whitespace);
+
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
                char *end;
@@ -2609,15 +2781,16 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        read_stdin = 0;
                        continue;
                }
-               if (!strncmp(arg, "--exclude=", 10)) {
+               if (!prefixcmp(arg, "--exclude=")) {
                        struct excludes *x = xmalloc(sizeof(*x));
                        x->path = arg + 10;
                        x->next = excludes;
                        excludes = x;
                        continue;
                }
-               if (!strncmp(arg, "-p", 2)) {
+               if (!prefixcmp(arg, "-p")) {
                        p_value = atoi(arg + 2);
+                       p_value_known = 1;
                        continue;
                }
                if (!strcmp(arg, "--no-add")) {
@@ -2649,10 +2822,14 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        continue;
                }
                if (!strcmp(arg, "--index")) {
+                       if (is_not_gitdir)
+                               die("--index outside a repository");
                        check_index = 1;
                        continue;
                }
                if (!strcmp(arg, "--cached")) {
+                       if (is_not_gitdir)
+                               die("--cached outside a repository");
                        check_index = 1;
                        cached = 1;
                        continue;
@@ -2661,22 +2838,24 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        apply = 1;
                        continue;
                }
-               if (!strcmp(arg, "--index-info")) {
+               if (!strcmp(arg, "--build-fake-ancestor")) {
                        apply = 0;
-                       show_index_info = 1;
+                       if (++i >= argc)
+                               die ("need a filename");
+                       fake_ancestor = argv[i];
                        continue;
                }
                if (!strcmp(arg, "-z")) {
                        line_termination = 0;
                        continue;
                }
-               if (!strncmp(arg, "-C", 2)) {
+               if (!prefixcmp(arg, "-C")) {
                        p_context = strtoul(arg + 2, &end, 0);
                        if (*end != '\0')
                                die("unrecognized context count '%s'", arg + 2);
                        continue;
                }
-               if (!strncmp(arg, "--whitespace=", 13)) {
+               if (!prefixcmp(arg, "--whitespace=")) {
                        whitespace_option = arg + 13;
                        parse_whitespace_option(arg + 13);
                        continue;
@@ -2693,7 +2872,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        apply = apply_with_reject = apply_verbosely = 1;
                        continue;
                }
-               if (!strcmp(arg, "--verbose")) {
+               if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose")) {
                        apply_verbosely = 1;
                        continue;
                }
@@ -2701,14 +2880,6 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                        inaccurate_eof = 1;
                        continue;
                }
-
-               if (check_index && prefix_length < 0) {
-                       prefix = setup_git_directory();
-                       prefix_length = prefix ? strlen(prefix) : 0;
-                       git_config(git_apply_config);
-                       if (!whitespace_option && apply_default_whitespace)
-                               parse_whitespace_option(apply_default_whitespace);
-               }
                if (0 < prefix_length)
                        arg = prefix_filename(prefix, prefix_length, arg);
 
@@ -2733,27 +2904,26 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
                                squelched,
                                squelched == 1 ? "" : "s");
                }
-               if (new_whitespace == error_on_whitespace)
-                       die("%d line%s add%s trailing whitespaces.",
+               if (ws_error_action == die_on_ws_error)
+                       die("%d line%s add%s whitespace errors.",
                            whitespace_error,
                            whitespace_error == 1 ? "" : "s",
                            whitespace_error == 1 ? "s" : "");
-               if (applied_after_stripping)
+               if (applied_after_fixing_ws && apply)
                        fprintf(stderr, "warning: %d line%s applied after"
-                               " stripping trailing whitespaces.\n",
-                               applied_after_stripping,
-                               applied_after_stripping == 1 ? "" : "s");
+                               " fixing whitespace errors.\n",
+                               applied_after_fixing_ws,
+                               applied_after_fixing_ws == 1 ? "" : "s");
                else if (whitespace_error)
-                       fprintf(stderr, "warning: %d line%s add%s trailing"
-                               " whitespaces.\n",
+                       fprintf(stderr, "warning: %d line%s add%s whitespace errors.\n",
                                whitespace_error,
                                whitespace_error == 1 ? "" : "s",
                                whitespace_error == 1 ? "s" : "");
        }
 
-       if (write_index) {
+       if (update_index) {
                if (write_cache(newfd, active_cache, active_nr) ||
-                   close(newfd) || commit_lock_file(&lock_file))
+                   commit_locked_index(&lock_file))
                        die("Unable to write new index file");
        }