X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=apply.c;h=1742ab28e95936eed52e910930a8a04869c5418b;hb=9cac9d368934f68e75810df95211a6522d4d139e;hp=129edb18890196b2947c1e3add85ce5ef9bb0ee7;hpb=011f4274bbb14e44035586f2ede695d584b8cfd2;p=git.git diff --git a/apply.c b/apply.c index 129edb18..1742ab28 100644 --- a/apply.c +++ b/apply.c @@ -16,6 +16,9 @@ // --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 allow_binary_replacement = 0; static int check_index = 0; static int write_index = 0; @@ -893,12 +896,24 @@ 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) { - static const char binhdr[] = "Binary files "; - - if (sizeof(binhdr) - 1 < size - offset - hdrsize && - !memcmp(binhdr, buffer + hdrsize + offset, - sizeof(binhdr)-1)) - patch->is_binary = 1; + 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 @@ -1694,6 +1709,12 @@ 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; } @@ -1833,6 +1854,15 @@ int main(int argc, char **argv) line_termination = 0; continue; } + + if (check_index && prefix_length < 0) { + prefix = setup_git_directory(); + prefix_length = prefix ? strlen(prefix) : 0; + git_config(git_default_config); + } + if (0 < prefix_length) + arg = prefix_filename(prefix, prefix_length, arg); + fd = open(arg, O_RDONLY); if (fd < 0) usage(apply_usage);