Make read_one_header_line return a flag not a length.
[git.git] / apply.c
diff --git a/apply.c b/apply.c
index e37c4eb..0ed9d13 100644 (file)
--- a/apply.c
+++ b/apply.c
 //  --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.
+//  --index updates the cache as well.
+//  --cached updates only the cache without ever touching the working tree.
 //
 static const char *prefix;
 static int prefix_length = -1;
+static int newfd = -1;
 
 static int p_value = 1;
 static int allow_binary_replacement = 0;
 static int check_index = 0;
 static int write_index = 0;
+static int cached = 0;
 static int diffstat = 0;
 static int numstat = 0;
 static int summary = 0;
@@ -35,7 +39,7 @@ static int show_index_info = 0;
 static int line_termination = '\n';
 static unsigned long p_context = -1;
 static const char apply_usage[] =
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
 
 static enum whitespace_eol {
        nowarn_whitespace,
@@ -114,6 +118,9 @@ struct patch {
        char *new_name, *old_name, *def_name;
        unsigned int old_mode, new_mode;
        int is_rename, is_copy, is_new, is_delete, is_binary;
+#define BINARY_DELTA_DEFLATED 1
+#define BINARY_LITERAL_DEFLATED 2
+       unsigned long deflate_origlen;
        int lines_added, lines_deleted;
        int score;
        struct fragment *fragments;
@@ -969,9 +976,11 @@ static inline int metadata_changes(struct patch *patch)
 
 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 {
-       /* We have read "GIT binary patch\n"; what follows is a
-        * sequence of 'length-byte' followed by base-85 encoded
-        * delta data.
+       /* We have read "GIT binary patch\n"; what follows is a line
+        * that says the patch method (currently, either "deflated
+        * literal" or "deflated delta") and the length of data before
+        * deflating; a sequence of 'length-byte' followed by base-85
+        * encoded data follows.
         *
         * Each 5-byte sequence of base-85 encodes up to 4 bytes,
         * and we would limit the patch line to 66 characters,
@@ -982,11 +991,27 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
         */
        int llen, used;
        struct fragment *fragment;
-       char *delta = NULL;
+       char *data = NULL;
 
-       patch->is_binary = 1;
        patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
-       used = 0;
+
+       /* Grab the type of patch */
+       llen = linelen(buffer, size);
+       used = llen;
+       linenr++;
+
+       if (!strncmp(buffer, "delta ", 6)) {
+               patch->is_binary = BINARY_DELTA_DEFLATED;
+               patch->deflate_origlen = strtoul(buffer + 6, NULL, 10);
+       }
+       else if (!strncmp(buffer, "literal ", 8)) {
+               patch->is_binary = BINARY_LITERAL_DEFLATED;
+               patch->deflate_origlen = strtoul(buffer + 8, NULL, 10);
+       }
+       else
+               return error("unrecognized binary patch at line %d: %.*s",
+                            linenr-1, llen-1, buffer);
+       buffer += llen;
        while (1) {
                int byte_length, max_byte_length, newsize;
                llen = linelen(buffer, size);
@@ -1015,8 +1040,8 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
                    byte_length <= max_byte_length - 4)
                        goto corrupt;
                newsize = fragment->size + byte_length;
-               delta = xrealloc(delta, newsize);
-               if (decode_85(delta + fragment->size,
+               data = xrealloc(data, newsize);
+               if (decode_85(data + fragment->size,
                              buffer + 1,
                              byte_length))
                        goto corrupt;
@@ -1024,7 +1049,7 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
                buffer += llen;
                size -= llen;
        }
-       fragment->patch = delta;
+       fragment->patch = data;
        return used;
  corrupt:
        return error("corrupt binary patch at line %d: %.*s",
@@ -1425,6 +1450,61 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
        return offset;
 }
 
+static char *inflate_it(const void *data, unsigned long size,
+                       unsigned long inflated_size)
+{
+       z_stream stream;
+       void *out;
+       int st;
+
+       memset(&stream, 0, sizeof(stream));
+
+       stream.next_in = (unsigned char *)data;
+       stream.avail_in = size;
+       stream.next_out = out = xmalloc(inflated_size);
+       stream.avail_out = inflated_size;
+       inflateInit(&stream);
+       st = inflate(&stream, Z_FINISH);
+       if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
+               free(out);
+               return NULL;
+       }
+       return out;
+}
+
+static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
+{
+       unsigned long dst_size;
+       struct fragment *fragment = patch->fragments;
+       void *data;
+       void *result;
+
+       data = inflate_it(fragment->patch, fragment->size,
+                         patch->deflate_origlen);
+       if (!data)
+               return error("corrupt patch data");
+       switch (patch->is_binary) {
+       case BINARY_DELTA_DEFLATED:
+               result = patch_delta(desc->buffer, desc->size,
+                                    data,
+                                    patch->deflate_origlen,
+                                    &dst_size);
+               free(desc->buffer);
+               desc->buffer = result;
+               free(data);
+               break;
+       case BINARY_LITERAL_DEFLATED:
+               free(desc->buffer);
+               desc->buffer = data;
+               dst_size = patch->deflate_origlen;
+               break;
+       }
+       if (!desc->buffer)
+               return -1;
+       desc->size = desc->alloc = dst_size;
+       return 0;
+}
+
 static int apply_binary(struct buffer_desc *desc, struct patch *patch)
 {
        const char *name = patch->old_name ? patch->old_name : patch->new_name;
@@ -1466,18 +1546,20 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
                                     "'%s' but it is not empty", name);
        }
 
-       if (desc->buffer) {
+       get_sha1_hex(patch->new_sha1_prefix, sha1);
+       if (!memcmp(sha1, null_sha1, 20)) {
                free(desc->buffer);
                desc->alloc = desc->size = 0;
-       }
-       get_sha1_hex(patch->new_sha1_prefix, sha1);
-       if (!memcmp(sha1, null_sha1, 20))
+               desc->buffer = NULL;
                return 0; /* deletion patch */
+       }
 
        if (has_sha1_file(sha1)) {
+               /* We already have the postimage */
                char type[10];
                unsigned long size;
 
+               free(desc->buffer);
                desc->buffer = read_sha1_file(sha1, type, &size);
                if (!desc->buffer)
                        return error("the necessary postimage %s for "
@@ -1486,28 +1568,13 @@ static int apply_binary(struct buffer_desc *desc, struct patch *patch)
                desc->alloc = desc->size = size;
        }
        else {
-               char type[10];
-               unsigned long src_size, dst_size;
-               void *src;
-
-               get_sha1_hex(patch->old_sha1_prefix, sha1);
-               src = read_sha1_file(sha1, type, &src_size);
-               if (!src)
-                       return error("the necessary preimage %s for "
-                                    "'%s' cannot be read",
-                                    patch->old_sha1_prefix, name);
-
-               /* patch->fragment->patch has the delta data and
-                * we should apply it to the preimage.
+               /* We have verified desc matches the preimage;
+                * apply the patch data to it, which is stored
+                * in the patch->fragments->{patch,size}.
                 */
-               desc->buffer = patch_delta(src, src_size,
-                                          (void*) patch->fragments->patch,
-                                          patch->fragments->size,
-                                          &dst_size);
-               if (!desc->buffer)
+               if (apply_binary_fragment(desc, patch))
                        return error("binary patch does not apply to '%s'",
                                     name);
-               desc->size = desc->alloc = dst_size;
 
                /* verify that the result matches */
                write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
@@ -1536,7 +1603,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
        return 0;
 }
 
-static int apply_data(struct patch *patch, struct stat *st)
+static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
 {
        char *buf;
        unsigned long size, alloc;
@@ -1545,7 +1612,17 @@ static int apply_data(struct patch *patch, struct stat *st)
        size = 0;
        alloc = 0;
        buf = NULL;
-       if (patch->old_name) {
+       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);
@@ -1573,16 +1650,21 @@ static int check_patch(struct patch *patch)
        const char *old_name = patch->old_name;
        const char *new_name = patch->new_name;
        const char *name = old_name ? old_name : new_name;
+       struct cache_entry *ce = NULL;
 
        if (old_name) {
-               int changed;
-               int stat_ret = lstat(old_name, &st);
+               int changed = 0;
+               int stat_ret = 0;
+               unsigned st_mode = 0;
 
+               if (!cached)
+                       stat_ret = lstat(old_name, &st);
                if (check_index) {
                        int pos = cache_name_pos(old_name, strlen(old_name));
                        if (pos < 0)
                                return error("%s: does not exist in index",
                                             old_name);
+                       ce = active_cache[pos];
                        if (stat_ret < 0) {
                                struct checkout costate;
                                if (errno != ENOENT)
@@ -1595,40 +1677,46 @@ static int check_patch(struct patch *patch)
                                costate.quiet = 0;
                                costate.not_new = 0;
                                costate.refresh_cache = 1;
-                               if (checkout_entry(active_cache[pos],
+                               if (checkout_entry(ce,
                                                   &costate,
                                                   NULL) ||
                                    lstat(old_name, &st))
                                        return -1;
                        }
-
-                       changed = ce_match_stat(active_cache[pos], &st, 1);
+                       if (!cached)
+                               changed = ce_match_stat(ce, &st, 1);
                        if (changed)
                                return error("%s: does not match index",
                                             old_name);
+                       if (cached)
+                               st_mode = ntohl(ce->ce_mode);
                }
                else if (stat_ret < 0)
                        return error("%s: %s", old_name, strerror(errno));
 
+               if (!cached)
+                       st_mode = ntohl(create_ce_mode(st.st_mode));
+
                if (patch->is_new < 0)
                        patch->is_new = 0;
-               st.st_mode = ntohl(create_ce_mode(st.st_mode));
                if (!patch->old_mode)
-                       patch->old_mode = st.st_mode;
-               if ((st.st_mode ^ patch->old_mode) & S_IFMT)
+                       patch->old_mode = st_mode;
+               if ((st_mode ^ patch->old_mode) & S_IFMT)
                        return error("%s: wrong type", old_name);
-               if (st.st_mode != patch->old_mode)
+               if (st_mode != patch->old_mode)
                        fprintf(stderr, "warning: %s has type %o, expected %o\n",
-                               old_name, st.st_mode, patch->old_mode);
+                               old_name, st_mode, patch->old_mode);
        }
 
        if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
                if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
                        return error("%s: already exists in index", new_name);
-               if (!lstat(new_name, &st))
-                       return error("%s: already exists in working directory", new_name);
-               if (errno != ENOENT)
-                       return error("%s: %s", new_name, strerror(errno));
+               if (!cached) {
+                       if (!lstat(new_name, &st))
+                               return error("%s: already exists in working directory", new_name);
+                       if (errno != ENOENT)
+                               return error("%s: %s", new_name, strerror(errno));
+               }
                if (!patch->new_mode) {
                        if (patch->is_new)
                                patch->new_mode = S_IFREG | 0644;
@@ -1645,9 +1733,9 @@ static int check_patch(struct patch *patch)
                        return error("new mode (%o) of %s does not match old mode (%o)%s%s",
                                patch->new_mode, new_name, patch->old_mode,
                                same ? "" : " of ", same ? "" : old_name);
-       }       
+       }
 
-       if (apply_data(patch, &st) < 0)
+       if (apply_data(patch, &st, ce) < 0)
                return error("%s: patch does not apply", name);
        return 0;
 }
@@ -1714,7 +1802,7 @@ 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;
+               name = patch->new_name ? patch->new_name : patch->old_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);
@@ -1830,7 +1918,8 @@ static void remove_file(struct patch *patch)
                if (remove_file_from_cache(patch->old_name) < 0)
                        die("unable to remove %s from index", patch->old_name);
        }
-       unlink(patch->old_name);
+       if (!cached)
+               unlink(patch->old_name);
 }
 
 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
@@ -1847,9 +1936,11 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
        memcpy(ce->name, path, namelen);
        ce->ce_mode = create_ce_mode(mode);
        ce->ce_flags = htons(namelen);
-       if (lstat(path, &st) < 0)
-               die("unable to stat newly created file %s", path);
-       fill_stat_cache_info(ce, &st);
+       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 (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
@@ -1886,6 +1977,8 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
  */
 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
 {
+       if (cached)
+               return;
        if (!try_create_file(path, mode, buf, size))
                return;
 
@@ -1985,7 +2078,6 @@ static int use_patch(struct patch *p)
 
 static int apply_patch(int fd, const char *filename)
 {
-       int newfd;
        unsigned long offset, size;
        char *buffer = read_patch_file(fd, &size);
        struct patch *list = NULL, **listp = &list;
@@ -2016,12 +2108,11 @@ static int apply_patch(int fd, const char *filename)
                size -= nr;
        }
 
-       newfd = -1;
        if (whitespace_error && (new_whitespace == error_on_whitespace))
                apply = 0;
 
        write_index = check_index && apply;
-       if (write_index)
+       if (write_index && newfd < 0)
                newfd = hold_index_file_for_update(&cache_file, get_index_file());
        if (check_index) {
                if (read_cache() < 0)
@@ -2034,12 +2125,6 @@ static int apply_patch(int fd, const char *filename)
        if (apply)
                write_out_results(list, skipped_patch);
 
-       if (write_index) {
-               if (write_cache(newfd, active_cache, active_nr) ||
-                   commit_index_file(&cache_file))
-                       die("Unable to write new cachefile");
-       }
-
        if (show_index_info)
                show_index_list(list);
 
@@ -2102,7 +2187,8 @@ int main(int argc, char **argv)
                        diffstat = 1;
                        continue;
                }
-               if (!strcmp(arg, "--allow-binary-replacement")) {
+               if (!strcmp(arg, "--allow-binary-replacement") ||
+                   !strcmp(arg, "--binary")) {
                        allow_binary_replacement = 1;
                        continue;
                }
@@ -2125,6 +2211,11 @@ int main(int argc, char **argv)
                        check_index = 1;
                        continue;
                }
+               if (!strcmp(arg, "--cached")) {
+                       check_index = 1;
+                       cached = 1;
+                       continue;
+               }
                if (!strcmp(arg, "--apply")) {
                        apply = 1;
                        continue;
@@ -2197,5 +2288,12 @@ int main(int argc, char **argv)
                                whitespace_error == 1 ? "" : "s",
                                whitespace_error == 1 ? "s" : "");
        }
+
+       if (write_index) {
+               if (write_cache(newfd, active_cache, active_nr) ||
+                   commit_index_file(&cache_file))
+                       die("Unable to write new cachefile");
+       }
+
        return 0;
 }