X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=apply.c;h=155fbe84da8b6d8dfb231fe508b68a36d52ffc60;hb=d402d5566fdf226697a386dfb9858e5d954e9b91;hp=31f134c9b325263fc10e09bb92570010241ca449;hpb=5569bf9bbedd63a00780fc5c110e0cfab3aa97b9;p=git.git diff --git a/apply.c b/apply.c index 31f134c9..155fbe84 100644 --- a/apply.c +++ b/apply.c @@ -5,26 +5,17 @@ * * This applies patches on top of some (arbitrary) version of the SCM. * - * NOTE! It does all its work in the index file, and only cares about - * the files in the working directory if you tell it to "merge" the - * patch apply. - * - * Even when merging it always takes the source from the index, and - * uses the working tree as a "branch" for a 3-way merge. */ #include - +#include #include "cache.h" -// We default to the merge behaviour, since that's what most people would -// expect. -// // --check turns on checking that the working tree matches the // files that are being modified, but doesn't apply the patch // --stat does just a diffstat, and doesn't actually apply // --show-files shows the directory changes +// --show-index-info shows the old and new index info for paths if available. // -static int merge_patch = 1; static int check_index = 0; static int write_index = 0; static int diffstat = 0; @@ -32,7 +23,9 @@ static int summary = 0; static int check = 0; static int apply = 1; static int show_files = 0; -static const char apply_usage[] = "git-apply [--stat] [--summary] [--check] [--show-files] "; +static int show_index_info = 0; +static const char apply_usage[] = +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] ..."; /* * For "diff-stat" like behaviour, we keep track of the biggest change @@ -65,6 +58,8 @@ struct patch { struct fragment *fragments; char *result; unsigned long resultsize; + char old_sha1_prefix[41]; + char new_sha1_prefix[41]; struct patch *next; }; @@ -343,6 +338,38 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch) return 0; } +static int gitdiff_index(const char *line, struct patch *patch) +{ + /* index line is N hexadecimal, "..", N hexadecimal, + * and optional space with octal mode. + */ + const char *ptr, *eol; + int len; + + ptr = strchr(line, '.'); + if (!ptr || ptr[1] != '.' || 40 <= ptr - line) + return 0; + len = ptr - line; + memcpy(patch->old_sha1_prefix, line, len); + patch->old_sha1_prefix[len] = 0; + + line = ptr + 2; + ptr = strchr(line, ' '); + eol = strchr(line, '\n'); + + if (!ptr || eol < ptr) + ptr = eol; + len = ptr - line; + + if (40 <= len) + return 0; + memcpy(patch->new_sha1_prefix, line, len); + patch->new_sha1_prefix[len] = 0; + if (*ptr == ' ') + patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8); + return 0; +} + /* * This is normal for a diff that doesn't change anything: we'll fall through * into the next diff. Tell the parser to break out. @@ -386,7 +413,7 @@ static char *git_header_name(char *line) default: continue; case '\n': - break; + return NULL; case '\t': case ' ': second = name+len; for (;;) { @@ -447,6 +474,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch { "rename to ", gitdiff_renamedst }, { "similarity index ", gitdiff_similarity }, { "dissimilarity index ", gitdiff_dissimilarity }, + { "index ", gitdiff_index }, { "", gitdiff_unrecognized }, }; int i; @@ -562,7 +590,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) @@ -671,13 +699,27 @@ 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 "\ ". + * Checking for 12 is just for sanity check -- any + * l10n of "\ No newline..." is at least that long. + */ case '\\': - if (len < 12 || memcmp(line, "\\ No newline", 12)) + if (len < 12 || memcmp(line, "\\ ", 2)) return -1; break; } } + /* 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. + */ + if (12 < size && !memcmp(line, "\\ ", 2)) + offset += linelen(line, size); + patch->lines_added += added; patch->lines_deleted += deleted; return offset; @@ -711,6 +753,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; @@ -721,14 +773,18 @@ 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; } -const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; -const char minuses[]= "----------------------------------------------------------------------"; +static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; +static const char minuses[]= "----------------------------------------------------------------------"; static void show_stats(struct patch *patch) { + const char *prefix = ""; char *name = patch->new_name; int len, max, add, del, total; @@ -742,8 +798,15 @@ static void show_stats(struct patch *patch) max = max_len; if (max > 50) max = 50; - if (len > max) + if (len > max) { + char *slash; + prefix = "..."; + max -= 3; name += len - max; + slash = strchr(name, '/'); + if (slash) + name = slash; + } len = max; /* @@ -762,7 +825,7 @@ static void show_stats(struct patch *patch) add = (add * max + max_change / 2) / max_change; del = total - add; } - printf(" %-*s |%5d %.*s%.*s\n", + printf(" %s%-*s |%5d %.*s%.*s\n", prefix, len, name, patch->lines_added + patch->lines_deleted, add, pluses, del, minuses); } @@ -860,7 +923,6 @@ static int find_offset(const char *buf, unsigned long size, const char *fragment n = (i >> 1)+1; if (i & 1) n = -n; - fprintf(stderr, "Fragment applied at offset %d\n", n); return try; } @@ -900,7 +962,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) * last one (which is the newline, of course). */ plen = len-1; - if (len > size && patch[len] == '\\') + if (len < size && patch[len] == '\\') plen--; switch (*patch) { case ' ': @@ -953,7 +1015,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; @@ -998,17 +1060,39 @@ static int check_patch(struct patch *patch) if (old_name) { int changed; + int stat_ret = lstat(old_name, &st); - if (lstat(old_name, &st) < 0) - return error("%s: %s", old_name, strerror(errno)); 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); + return error("%s: does not exist in index", + old_name); + if (stat_ret < 0) { + struct checkout costate; + if (errno != ENOENT) + return error("%s: %s", old_name, + strerror(errno)); + /* checkout */ + costate.base_dir = ""; + costate.base_dir_len = 0; + costate.force = 0; + costate.quiet = 0; + costate.not_new = 0; + costate.refresh_cache = 1; + if (checkout_entry(active_cache[pos], + &costate) || + lstat(old_name, &st)) + return -1; + } + changed = ce_match_stat(active_cache[pos], &st); if (changed) - return error("%s: does not match index", old_name); + return error("%s: does not match index", + old_name); } + else if (stat_ret < 0) + return error("%s: %s", old_name, strerror(errno)); + if (patch->is_new < 0) patch->is_new = 0; st.st_mode = ntohl(create_ce_mode(st.st_mode)); @@ -1028,8 +1112,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) { @@ -1085,6 +1173,36 @@ static void show_file_list(struct patch *patch) } } +static inline int is_null_sha1(const unsigned char *sha1) +{ + return !memcmp(sha1, null_sha1, 20); +} + +static void show_index_list(struct patch *list) +{ + struct patch *patch; + + /* Once we start supporting the reverse patch, it may be + * worth showing the new sha1 prefix, but until then... + */ + for (patch = list; patch; patch = patch->next) { + const unsigned char *sha1_ptr; + unsigned char sha1[20]; + const char *name; + + name = patch->old_name ? patch->old_name : patch->new_name; + if (patch->is_new) + sha1_ptr = null_sha1; + else if (get_sha1(patch->old_sha1_prefix, sha1)) + die("sha1 information is lacking or useless (%s).", + name); + else + sha1_ptr = sha1; + printf("%06o %s %s\n",patch->old_mode, + sha1_to_hex(sha1_ptr), name); + } +} + static void stat_patch_list(struct patch *patch) { int files, adds, dels; @@ -1144,7 +1262,7 @@ static void show_rename_copy(struct patch *p) */ if (old != p->old_name) printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, - old - p->old_name, p->old_name, + (int)(old - p->old_name), p->old_name, old, new, p->score); else printf(" %s %s => %s (%d%%)\n", renamecopy, @@ -1238,7 +1356,7 @@ static void create_subdirectories(const char *path) len = slash - path; memcpy(buf, path, len); buf[len] = 0; - if (mkdir(buf, 0755) < 0) { + if (mkdir(buf, 0777) < 0) { if (errno != EEXIST) break; } @@ -1246,31 +1364,65 @@ static void create_subdirectories(const char *path) free(buf); } +static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) +{ + int fd; + + if (S_ISLNK(mode)) + return symlink(buf, path); + fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, (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; + die("writing file %s: %s", path, strerror(errno)); + } + if (!written) + die("out of space writing file %s", path); + buf += written; + size -= written; + } + if (close(fd) < 0) + die("closing file %s: %s", path, strerror(errno)); + return 0; +} + /* * We optimistically assume that the directories exist, * which is true 99% of the time anyway. If they don't, * we create them and try again. */ -static int create_regular_file(const char *path, unsigned int mode) +static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size) { - int ret = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); + if (!try_create_file(path, mode, buf, size)) + return; - if (ret < 0 && errno == ENOENT) { + if (errno == ENOENT) { create_subdirectories(path); - ret = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); + if (!try_create_file(path, mode, buf, size)) + return; } - return ret; -} -static int create_symlink(const char *buf, const char *path) -{ - int ret = symlink(buf, path); + if (errno == EEXIST) { + unsigned int nr = getpid(); - if (ret < 0 && errno == ENOENT) { - create_subdirectories(path); - ret = symlink(buf, path); + for (;;) { + const char *newpath; + newpath = mkpath("%s~%u", path, nr); + if (!try_create_file(newpath, mode, buf, size)) { + if (!rename(newpath, path)) + return; + unlink(newpath); + break; + } + if (errno != EEXIST) + break; + } } - return ret; + die("unable to write file %s mode %o", path, mode); } static void create_file(struct patch *patch) @@ -1282,28 +1434,8 @@ static void create_file(struct patch *patch) if (!mode) mode = S_IFREG | 0644; - if (S_ISREG(mode)) { - int fd; - mode = (mode & 0100) ? 0777 : 0666; - fd = create_regular_file(path, mode); - if (fd < 0) - die("unable to create file %s (%s)", path, strerror(errno)); - if (write(fd, buf, size) != size) - die("unable to write file %s", path); - close(fd); - add_index_file(path, mode, buf, size); - return; - } - if (S_ISLNK(mode)) { - if (size && buf[size-1] == '\n') - size--; - buf[size] = 0; - if (create_symlink(buf, path) < 0) - die("unable to write symlink %s", path); - add_index_file(path, mode, buf, size); - return; - } - die("unable to write file mode %o", mode); + create_one_file(path, mode, buf, size); + add_index_file(path, mode, buf, size); } static void write_out_one_result(struct patch *patch) @@ -1324,9 +1456,9 @@ static void write_out_one_result(struct patch *patch) create_file(patch); } -static void write_out_results(struct patch *list) +static void write_out_results(struct patch *list, int skipped_patch) { - if (!list) + if (!list && !skipped_patch) die("No changes"); while (list) { @@ -1337,12 +1469,30 @@ static void write_out_results(struct patch *list) static struct cache_file cache_file; +static struct excludes { + struct excludes *next; + const char *path; +} *excludes; + +static int use_patch(struct patch *p) +{ + 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) + return 0; + x = x->next; + } + return 1; +} + static int apply_patch(int fd) { int newfd; unsigned long offset, size; char *buffer = read_patch_file(fd, &size); struct patch *list = NULL, **listp = &list; + int skipped_patch = 0; if (!buffer) return -1; @@ -1356,9 +1506,15 @@ static int apply_patch(int fd) nr = parse_chunk(buffer + offset, size, patch); if (nr < 0) break; - patch_stats(patch); - *listp = patch; - listp = &patch->next; + if (use_patch(patch)) { + patch_stats(patch); + *listp = patch; + listp = &patch->next; + } else { + /* perhaps free it a bit better? */ + free(patch); + skipped_patch++; + } offset += nr; size -= nr; } @@ -1376,7 +1532,7 @@ static int apply_patch(int fd) exit(1); if (apply) - write_out_results(list); + write_out_results(list, skipped_patch); if (write_index) { if (write_cache(newfd, active_cache, active_nr) || @@ -1387,6 +1543,9 @@ static int apply_patch(int fd) if (show_files) show_file_list(list); + if (show_index_info) + show_index_list(list); + if (diffstat) stat_patch_list(list); @@ -1411,8 +1570,11 @@ int main(int argc, char **argv) read_stdin = 0; continue; } - if (!strcmp(arg, "--no-merge")) { - merge_patch = 0; + if (!strncmp(arg, "--exclude=", 10)) { + struct excludes *x = xmalloc(sizeof(*x)); + x->path = arg + 10; + x->next = excludes; + excludes = x; continue; } if (!strcmp(arg, "--stat")) { @@ -1434,10 +1596,19 @@ int main(int argc, char **argv) check_index = 1; continue; } + if (!strcmp(arg, "--apply")) { + apply = 1; + continue; + } if (!strcmp(arg, "--show-files")) { show_files = 1; continue; } + if (!strcmp(arg, "--show-index-info")) { + apply = 0; + show_index_info = 1; + continue; + } fd = open(arg, O_RDONLY); if (fd < 0) usage(apply_usage);