git-clone: exit early if repo isn't specified
[gitweb.git] / apply.c
diff --git a/apply.c b/apply.c
index e5c0b7d074936507159353cb5699d17897c8b1e4..c50b3a60464ea05307af8e8d1be576cf96415ff0 100644 (file)
--- a/apply.c
+++ b/apply.c
 //  --check turns on checking that the working tree matches the
 //    files that are being modified, but doesn't apply the patch
 //  --stat does just a diffstat, and doesn't actually apply
+//  --numstat does numeric diffstat, and doesn't actually apply
 //  --index-info shows the old and new index info for paths if available.
 //
+static const char *prefix;
+static int prefix_length = -1;
+
+static int p_value = 1;
+static int allow_binary_replacement = 0;
 static int check_index = 0;
 static int write_index = 0;
 static int diffstat = 0;
+static int numstat = 0;
 static int summary = 0;
 static int check = 0;
 static int apply = 1;
+static int no_add = 0;
 static int show_index_info = 0;
 static int line_termination = '\n';
 static const char apply_usage[] =
-"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] <patch>...";
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [--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;
+static int whitespace_error = 0;
+static int squelch_whitespace_errors = 5;
+static int applied_after_stripping = 0;
+static const char *patch_input_file = NULL;
+
+static void parse_whitespace_option(const char *option)
+{
+       if (!option) {
+               new_whitespace = warn_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "warn")) {
+               new_whitespace = warn_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "nowarn")) {
+               new_whitespace = nowarn_whitespace;
+               return;
+       }
+       if (!strcmp(option, "error")) {
+               new_whitespace = error_on_whitespace;
+               return;
+       }
+       if (!strcmp(option, "error-all")) {
+               new_whitespace = error_on_whitespace;
+               squelch_whitespace_errors = 0;
+               return;
+       }
+       if (!strcmp(option, "strip")) {
+               new_whitespace = strip_whitespace;
+               return;
+       }
+       die("unrecognized whitespace option '%s'", 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);
+       }
+}
 
 /*
  * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -51,7 +109,7 @@ struct fragment {
 struct patch {
        char *new_name, *old_name, *def_name;
        unsigned int old_mode, new_mode;
-       int is_rename, is_copy, is_new, is_delete;
+       int is_rename, is_copy, is_new, is_delete, is_binary;
        int lines_added, lines_deleted;
        int score;
        struct fragment *fragments;
@@ -77,14 +135,11 @@ static void *read_patch_file(int fd, unsigned long *sizep)
                        buffer = xrealloc(buffer, alloc);
                        nr = alloc - size;
                }
-               nr = read(fd, buffer + size, nr);
+               nr = xread(fd, buffer + size, nr);
                if (!nr)
                        break;
-               if (nr < 0) {
-                       if (errno == EAGAIN)
-                               continue;
+               if (nr < 0)
                        die("git-apply: read returned %s", strerror(errno));
-               }
                size += nr;
        }
        *sizep = size;
@@ -213,7 +268,6 @@ static char * find_name(const char *line, char *def, int p_value, int terminate)
  */
 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
 {
-       int p_value = 1;
        char *name;
 
        first += 4;     // skip "--- "
@@ -367,7 +421,7 @@ static int gitdiff_index(const char *line, struct patch *patch)
        int len;
 
        ptr = strchr(line, '.');
-       if (!ptr || ptr[1] != '.' || 40 <= ptr - line)
+       if (!ptr || ptr[1] != '.' || 40 < ptr - line)
                return 0;
        len = ptr - line;
        memcpy(patch->old_sha1_prefix, line, len);
@@ -381,7 +435,7 @@ static int gitdiff_index(const char *line, struct patch *patch)
                ptr = eol;
        len = ptr - line;
 
-       if (40 <= len)
+       if (40 < len)
                return 0;
        memcpy(patch->new_sha1_prefix, line, len);
        patch->new_sha1_prefix[len] = 0;
@@ -597,7 +651,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
                len = linelen(line, size);
                if (!len || line[len-1] != '\n')
                        break;
-               for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
+               for (i = 0; i < ARRAY_SIZE(optable); i++) {
                        const struct opentry *p = optable + i;
                        int oplen = strlen(p->str);
                        if (len < oplen || memcmp(p->str, line, oplen))
@@ -639,7 +693,7 @@ static int parse_range(const char *line, int len, int offset, const char *expect
        line += digits;
        len -= digits;
 
-       *p2 = *p1;
+       *p2 = 1;
        if (*line == ',') {
                digits = parse_num(line+1, p2);
                if (!digits)
@@ -780,7 +834,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
                        patch->new_name = NULL;
        }
 
-       if (patch->is_new != !oldlines)
+       if (patch->is_new && oldlines)
                return error("new file depends on old contents");
        if (patch->is_delete != !newlines) {
                if (newlines)
@@ -811,6 +865,25 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
                        oldlines--;
                        break;
                case '+':
+                       /*
+                        * We know len is at least two, since we have a '+' and
+                        * we checked that the last character was a '\n' above.
+                        * That is, an addition of an empty line would check
+                        * the '+' here.  Sneaky...
+                        */
+                       if ((new_whitespace != nowarn_whitespace) &&
+                           isspace(line[len-2])) {
+                               whitespace_error++;
+                               if (squelch_whitespace_errors &&
+                                   squelch_whitespace_errors <
+                                   whitespace_error)
+                                       ;
+                               else {
+                                       fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
+                                               patch_input_file,
+                                               linenr, len-2, line+1);
+                               }
+                       }
                        added++;
                        newlines--;
                        break;
@@ -828,6 +901,8 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
                        break;
                }
        }
+       if (oldlines || newlines)
+               return -1;
        /* 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.
@@ -888,8 +963,36 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 
        patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
 
-       if (!patchsize && !metadata_changes(patch))
-               die("patch with only garbage at line %d", linenr);
+       if (!patchsize) {
+               static const char *binhdr[] = {
+                       "Binary files ",
+                       "Files ",
+                       NULL,
+               };
+               int i;
+               int hd = hdrsize + offset;
+               unsigned long llen = linelen(buffer + hd, size - hd);
+
+               if (!memcmp(" differ\n", buffer + hd + llen - 8, 8))
+                       for (i = 0; binhdr[i]; i++) {
+                               int len = strlen(binhdr[i]);
+                               if (len < size - hd &&
+                                   !memcmp(binhdr[i], buffer + hd, len)) {
+                                       patch->is_binary = 1;
+                                       break;
+                               }
+                       }
+
+               /* Empty patch cannot be applied if:
+                * - it is a binary patch and we do not do binary_replace, or
+                * - text patch without metadata change
+                */
+               if ((apply || check) &&
+                   (patch->is_binary
+                    ? !allow_binary_replacement
+                    : !metadata_changes(patch)))
+                       die("patch with only garbage at line %d", linenr);
+       }
 
        return offset + hdrsize + patchsize;
 }
@@ -947,9 +1050,12 @@ static void show_stats(struct patch *patch)
                add = (add * max + max_change / 2) / max_change;
                del = total - add;
        }
-       printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
-               len, name, patch->lines_added + patch->lines_deleted,
-               add, pluses, del, minuses);
+       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);
        if (qname)
                free(qname);
 }
@@ -968,13 +1074,8 @@ static int read_old_data(struct stat *st, const char *path, void *buf, unsigned
                        return error("unable to open %s", path);
                got = 0;
                for (;;) {
-                       int ret = read(fd, buf + got, size - got);
-                       if (ret < 0) {
-                               if (errno == EAGAIN)
-                                       continue;
-                               break;
-                       }
-                       if (!ret)
+                       int ret = xread(fd, buf + got, size - got);
+                       if (ret <= 0)
                                break;
                        got += ret;
                }
@@ -1062,6 +1163,28 @@ struct buffer_desc {
        unsigned long alloc;
 };
 
+static int apply_line(char *output, const char *patch, int plen)
+{
+       /* plen is number of bytes to be copied from patch,
+        * starting at patch+1 (patch[0] is '+').  Typically
+        * patch[plen] is '\n'.
+        */
+       int add_nl_to_tail = 0;
+       if ((new_whitespace == strip_whitespace) &&
+           1 < plen && isspace(patch[plen-1])) {
+               if (patch[plen] == '\n')
+                       add_nl_to_tail = 1;
+               plen--;
+               while (0 < plen && isspace(patch[plen]))
+                       plen--;
+               applied_after_stripping++;
+       }
+       memcpy(output, patch + 1, plen);
+       if (add_nl_to_tail)
+               output[plen++] = '\n';
+       return plen;
+}
+
 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
 {
        char *buf = desc->buffer;
@@ -1097,8 +1220,9 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
                                break;
                /* Fall-through for ' ' */
                case '+':
-                       memcpy(new + newsize, patch + 1, plen);
-                       newsize += plen;
+                       if (*patch != '+' || !no_add)
+                               newsize += apply_line(new + newsize, patch,
+                                                     plen);
                        break;
                case '@': case '\\':
                        /* Ignore it, we already handled it */
@@ -1110,6 +1234,14 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
                size -= len;
        }
 
+#ifdef NO_ACCURATE_DIFF
+       if (oldsize > 0 && old[oldsize - 1] == '\n' &&
+                       newsize > 0 && new[newsize - 1] == '\n') {
+               oldsize--;
+               newsize--;
+       }
+#endif
+                       
        offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
        if (offset >= 0) {
                int diff = newsize - oldsize;
@@ -1136,10 +1268,77 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
 {
        struct fragment *frag = patch->fragments;
+       const char *name = patch->old_name ? patch->old_name : patch->new_name;
+
+       if (patch->is_binary) {
+               unsigned char sha1[20];
+
+               if (!allow_binary_replacement)
+                       return error("cannot apply binary patch to '%s' "
+                                    "without --allow-binary-replacement",
+                                    name);
+
+               /* 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 ||
+                   strlen(patch->new_sha1_prefix) != 40 ||
+                   get_sha1_hex(patch->old_sha1_prefix, sha1) ||
+                   get_sha1_hex(patch->new_sha1_prefix, sha1))
+                       return error("cannot apply binary patch to '%s' "
+                                    "without full index line", name);
+
+               if (patch->old_name) {
+                       unsigned char hdr[50];
+                       int hdrlen;
+
+                       /* See if the old one matches what the patch
+                        * applies to.
+                        */
+                       write_sha1_file_prepare(desc->buffer, desc->size,
+                                               "blob", sha1, hdr, &hdrlen);
+                       if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
+                               return error("the patch applies to '%s' (%s), "
+                                            "which does not match the "
+                                            "current contents.",
+                                            name, sha1_to_hex(sha1));
+               }
+               else {
+                       /* Otherwise, the old one must be empty. */
+                       if (desc->size)
+                               return error("the patch applies to an empty "
+                                            "'%s' but it is not empty", name);
+               }
+
+               /* For now, we do not record post-image data in the patch,
+                * and require the object already present in the recipient's
+                * object database.
+                */
+               if (desc->buffer) {
+                       free(desc->buffer);
+                       desc->alloc = desc->size = 0;
+               }
+               get_sha1_hex(patch->new_sha1_prefix, sha1);
+
+               if (memcmp(sha1, null_sha1, 20)) {
+                       char type[10];
+                       unsigned long size;
+
+                       desc->buffer = read_sha1_file(sha1, type, &size);
+                       if (!desc->buffer)
+                               return error("the necessary postimage %s for "
+                                            "'%s' does not exist",
+                                            patch->new_sha1_prefix, name);
+                       desc->alloc = desc->size = size;
+               }
+
+               return 0;
+       }
 
        while (frag) {
                if (apply_one_fragment(desc, frag) < 0)
-                       return error("patch failed: %s:%ld", patch->old_name, frag->oldpos);
+                       return error("patch failed: %s:%ld",
+                                    name, frag->oldpos);
                frag = frag->next;
        }
        return 0;
@@ -1181,6 +1380,7 @@ static int check_patch(struct patch *patch)
        struct stat st;
        const char *old_name = patch->old_name;
        const char *new_name = patch->new_name;
+       const char *name = old_name ? old_name : new_name;
 
        if (old_name) {
                int changed;
@@ -1204,12 +1404,13 @@ static int check_patch(struct patch *patch)
                                costate.not_new = 0;
                                costate.refresh_cache = 1;
                                if (checkout_entry(active_cache[pos],
-                                                  &costate) ||
+                                                  &costate,
+                                                  NULL) ||
                                    lstat(old_name, &st))
                                        return -1;
                        }
 
-                       changed = ce_match_stat(active_cache[pos], &st);
+                       changed = ce_match_stat(active_cache[pos], &st, 1);
                        if (changed)
                                return error("%s: does not match index",
                                             old_name);
@@ -1255,7 +1456,7 @@ static int check_patch(struct patch *patch)
        }       
 
        if (apply_data(patch, &st) < 0)
-               return error("%s: patch does not apply", old_name);
+               return error("%s: patch does not apply", name);
        return 0;
 }
 
@@ -1317,6 +1518,20 @@ static void stat_patch_list(struct patch *patch)
        printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
 }
 
+static void numstat_patch_list(struct patch *patch)
+{
+       for ( ; patch; patch = patch->next) {
+               const char *name;
+               name = patch->old_name ? patch->old_name : patch->new_name;
+               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('\n');
+       }
+}
+
 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
 {
        if (mode)
@@ -1450,40 +1665,19 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
                die("unable to add cache entry for %s", path);
 }
 
-static void create_subdirectories(const char *path)
-{
-       int len = strlen(path);
-       char *buf = xmalloc(len + 1);
-       const char *slash = path;
-
-       while ((slash = strchr(slash+1, '/')) != NULL) {
-               len = slash - path;
-               memcpy(buf, path, len);
-               buf[len] = 0;
-               if (mkdir(buf, 0777) < 0) {
-                       if (errno != EEXIST)
-                               break;
-               }
-       }
-       free(buf);
-}
-
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
 {
        int fd;
 
        if (S_ISLNK(mode))
                return symlink(buf, path);
-       fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, (mode & 0100) ? 0777 : 0666);
+       fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
        if (fd < 0)
                return -1;
        while (size) {
-               int written = write(fd, buf, size);
-               if (written < 0) {
-                       if (errno == EINTR || errno == EAGAIN)
-                               continue;
+               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;
@@ -1499,13 +1693,14 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
  * which is true 99% of the time anyway. If they don't,
  * we create them and try again.
  */
-static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size)
+static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
 {
        if (!try_create_file(path, mode, buf, size))
                return;
 
        if (errno == ENOENT) {
-               create_subdirectories(path);
+               if (safe_create_leading_directories(path))
+                       return;
                if (!try_create_file(path, mode, buf, size))
                        return;
        }
@@ -1524,14 +1719,15 @@ static void create_one_file(const char *path, unsigned mode, const char *buf, un
                        }
                        if (errno != EEXIST)
                                break;
-               }                       
+                       ++nr;
+               }
        }
        die("unable to write file %s mode %o", path, mode);
 }
 
 static void create_file(struct patch *patch)
 {
-       const char *path = patch->new_name;
+       char *path = patch->new_name;
        unsigned mode = patch->new_mode;
        unsigned long size = patch->resultsize;
        char *buf = patch->result;
@@ -1587,10 +1783,16 @@ static int use_patch(struct patch *p)
                        return 0;
                x = x->next;
        }
+       if (0 < prefix_length) {
+               int pathlen = strlen(pathname);
+               if (pathlen <= prefix_length ||
+                   memcmp(prefix, pathname, prefix_length))
+                       return 0;
+       }
        return 1;
 }
 
-static int apply_patch(int fd)
+static int apply_patch(int fd, const char *filename)
 {
        int newfd;
        unsigned long offset, size;
@@ -1598,6 +1800,7 @@ static int apply_patch(int fd)
        struct patch *list = NULL, **listp = &list;
        int skipped_patch = 0;
 
+       patch_input_file = filename;
        if (!buffer)
                return -1;
        offset = 0;
@@ -1624,6 +1827,9 @@ static int apply_patch(int fd)
        }
 
        newfd = -1;
+       if (whitespace_error && (new_whitespace == error_on_whitespace))
+               apply = 0;
+
        write_index = check_index && apply;
        if (write_index)
                newfd = hold_index_file_for_update(&cache_file, get_index_file());
@@ -1650,6 +1856,9 @@ static int apply_patch(int fd)
        if (diffstat)
                stat_patch_list(list);
 
+       if (numstat)
+               numstat_patch_list(list);
+
        if (summary)
                summary_patch_list(list);
 
@@ -1657,17 +1866,28 @@ static int apply_patch(int fd)
        return 0;
 }
 
+static int git_apply_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "apply.whitespace")) {
+               apply_default_whitespace = strdup(value);
+               return 0;
+       }
+       return git_default_config(var, value);
+}
+
+
 int main(int argc, char **argv)
 {
        int i;
        int read_stdin = 1;
+       const char *whitespace_option = NULL;
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
                int fd;
 
                if (!strcmp(arg, "-")) {
-                       apply_patch(0);
+                       apply_patch(0, "<stdin>");
                        read_stdin = 0;
                        continue;
                }
@@ -1678,11 +1898,28 @@ int main(int argc, char **argv)
                        excludes = x;
                        continue;
                }
+               if (!strncmp(arg, "-p", 2)) {
+                       p_value = atoi(arg + 2);
+                       continue;
+               }
+               if (!strcmp(arg, "--no-add")) {
+                       no_add = 1;
+                       continue;
+               }
                if (!strcmp(arg, "--stat")) {
                        apply = 0;
                        diffstat = 1;
                        continue;
                }
+               if (!strcmp(arg, "--allow-binary-replacement")) {
+                       allow_binary_replacement = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "--numstat")) {
+                       apply = 0;
+                       numstat = 1;
+                       continue;
+               }
                if (!strcmp(arg, "--summary")) {
                        apply = 0;
                        summary = 1;
@@ -1710,14 +1947,58 @@ int main(int argc, char **argv)
                        line_termination = 0;
                        continue;
                }
+               if (!strncmp(arg, "--whitespace=", 13)) {
+                       whitespace_option = arg + 13;
+                       parse_whitespace_option(arg + 13);
+                       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);
+
                fd = open(arg, O_RDONLY);
                if (fd < 0)
                        usage(apply_usage);
                read_stdin = 0;
-               apply_patch(fd);
+               set_default_whitespace_mode(whitespace_option);
+               apply_patch(fd, arg);
                close(fd);
        }
+       set_default_whitespace_mode(whitespace_option);
        if (read_stdin)
-               apply_patch(0);
+               apply_patch(0, "<stdin>");
+       if (whitespace_error) {
+               if (squelch_whitespace_errors &&
+                   squelch_whitespace_errors < whitespace_error) {
+                       int squelched =
+                               whitespace_error - squelch_whitespace_errors;
+                       fprintf(stderr, "warning: squelched %d whitespace error%s\n",
+                               squelched,
+                               squelched == 1 ? "" : "s");
+               }
+               if (new_whitespace == error_on_whitespace)
+                       die("%d line%s add%s trailing whitespaces.",
+                           whitespace_error,
+                           whitespace_error == 1 ? "" : "s",
+                           whitespace_error == 1 ? "s" : "");
+               if (applied_after_stripping)
+                       fprintf(stderr, "warning: %d line%s applied after"
+                               " stripping trailing whitespaces.\n",
+                               applied_after_stripping,
+                               applied_after_stripping == 1 ? "" : "s");
+               else if (whitespace_error)
+                       fprintf(stderr, "warning: %d line%s add%s trailing"
+                               " whitespaces.\n",
+                               whitespace_error,
+                               whitespace_error == 1 ? "" : "s",
+                               whitespace_error == 1 ? "s" : "");
+       }
        return 0;
 }