X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=apply.c;h=f8862722fdba204d6e15e4d012f8891bcc36475c;hb=df8baa42fe4eeb5a021ac262caf601f44d2a5746;hp=c671d9e86cc04e2c6756212e3496c4ef5db8207a;hpb=f10e0e0b18c8e2e69535e7380fb3c1f9b097cfda;p=git.git diff --git a/apply.c b/apply.c index c671d9e8..f8862722 100644 --- a/apply.c +++ b/apply.c @@ -387,7 +387,7 @@ static char *git_header_name(char *line) default: continue; case '\n': - break; + return NULL; case '\t': case ' ': second = name+len; for (;;) { @@ -563,7 +563,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc struct fragment dummy; if (parse_fragment_header(line, len, &dummy) < 0) continue; - error("patch fragment without header at line %d: %.*s", linenr, len-1, line); + error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line); } if (size < len + 6) @@ -672,9 +672,13 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s added++; newlines--; break; - /* We allow "\ No newline at end of file" */ + + /* 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 "\ ". */ case '\\': - if (len < 12 || memcmp(line, "\\ No newline", 12)) + if (len < 12 || memcmp(line, "\\ ", 2)) return -1; break; } @@ -683,7 +687,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s * it in the above loop because we hit oldlines == newlines == 0 * before seeing it. */ - if (12 < size && !memcmp(line, "\\ No newline", 12)) + if (12 < size && !memcmp(line, "\\ ", 2)) offset += linelen(line, size); patch->lines_added += added; @@ -719,6 +723,16 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc return offset; } +static inline int metadata_changes(struct patch *patch) +{ + return patch->is_rename > 0 || + patch->is_copy > 0 || + patch->is_new > 0 || + patch->is_delete || + (patch->old_mode && patch->new_mode && + patch->old_mode != patch->new_mode); +} + static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) { int hdrsize, patchsize; @@ -729,6 +743,9 @@ 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); + return offset + hdrsize + patchsize; } @@ -968,7 +985,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch) while (frag) { if (apply_one_fragment(desc, frag) < 0) - return error("patch failed: %s:%d", patch->old_name, frag->oldpos); + return error("patch failed: %s:%ld", patch->old_name, frag->oldpos); frag = frag->next; } return 0; @@ -1043,8 +1060,12 @@ static int check_patch(struct patch *patch) 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) - patch->new_mode = S_IFREG | 0644; + if (!patch->new_mode) { + if (patch->is_new) + patch->new_mode = S_IFREG | 0644; + else + patch->new_mode = patch->old_mode; + } } if (new_name && old_name) { @@ -1373,7 +1394,7 @@ static struct excludes { static int use_patch(struct patch *p) { - const char *pathname = p->new_name ? : p->old_name; + const char *pathname = p->new_name ? p->new_name : p->old_name; struct excludes *x = excludes; while (x) { if (fnmatch(x->path, pathname, 0) == 0)