X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=apply.c;h=33b4271288d3ee6ac786d7fc6009df1bf354055e;hb=fc4c4cd21c783b6dc387002c6e018d26f7405e9f;hp=9deb206faa5259e4f4f0a458ddc14dd4762da10b;hpb=213523f46c51c5e5f1b2623dcb3af822add75caa;p=git.git diff --git a/apply.c b/apply.c index 9deb206f..33b42712 100644 --- a/apply.c +++ b/apply.c @@ -9,6 +9,7 @@ #include #include "cache.h" #include "quote.h" +#include "blob.h" // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch @@ -32,7 +33,7 @@ static int no_add = 0; static int show_index_info = 0; static int line_termination = '\n'; static const char apply_usage[] = -"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] ..."; +"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [--whitespace=] ..."; static enum whitespace_eol { nowarn_whitespace, @@ -651,7 +652,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)) @@ -693,7 +694,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) @@ -834,7 +835,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) @@ -901,6 +902,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. @@ -922,8 +925,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc struct fragment *fragment; int len; - fragment = xmalloc(sizeof(*fragment)); - memset(fragment, 0, sizeof(*fragment)); + fragment = xcalloc(1, sizeof(*fragment)); len = parse_fragment(line, size, patch, fragment); if (len <= 0) die("corrupt patch at line %d", linenr); @@ -1294,7 +1296,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch) * applies to. */ write_sha1_file_prepare(desc->buffer, desc->size, - "blob", sha1, hdr, &hdrlen); + blob_type, 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 " @@ -1402,7 +1404,8 @@ 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; } @@ -1648,15 +1651,14 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned if (!write_index) return; - ce = xmalloc(ce_size); - memset(ce, 0, ce_size); + ce = xcalloc(1, ce_size); 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 (write_sha1_file(buf, size, "blob", ce->sha1) < 0) + 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); @@ -1805,8 +1807,7 @@ static int apply_patch(int fd, const char *filename) struct patch *patch; int nr; - patch = xmalloc(sizeof(*patch)); - memset(patch, 0, sizeof(*patch)); + patch = xcalloc(1, sizeof(*patch)); nr = parse_chunk(buffer + offset, size, patch); if (nr < 0) break;