X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=apply.c;h=79e23a7fb930ba7b322a0c30dc87c5b394622e49;hb=cb754fdf5a742a5479458da84d8f92bce2434ed5;hp=50be8f3e22d0906d09410b89ebc8d06f928ba631;hpb=8fc66df237afce0b4318657f166b3583831949f3;p=git.git diff --git a/apply.c b/apply.c index 50be8f3e..79e23a7f 100644 --- a/apply.c +++ b/apply.c @@ -16,6 +16,10 @@ // --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; @@ -28,7 +32,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] ..."; +"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -81,14 +85,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; @@ -217,7 +218,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 "--- " @@ -1003,13 +1003,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; } @@ -1593,16 +1588,13 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, 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; @@ -1643,7 +1635,8 @@ 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); } @@ -1706,6 +1699,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; } @@ -1800,6 +1799,10 @@ 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; @@ -1845,6 +1848,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);