Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * apply.c |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | * |
| 6 | * This applies patches on top of some (arbitrary) version of the SCM. |
| 7 | * |
| 8 | * NOTE! It does all its work in the index file, and only cares about |
| 9 | * the files in the working directory if you tell it to "merge" the |
| 10 | * patch apply. |
| 11 | * |
| 12 | * Even when merging it always takes the source from the index, and |
| 13 | * uses the working tree as a "branch" for a 3-way merge. |
| 14 | */ |
| 15 | #include <ctype.h> |
| 16 | |
| 17 | #include "cache.h" |
| 18 | |
| 19 | // We default to the merge behaviour, since that's what most people would |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 20 | // expect. |
| 21 | // |
| 22 | // --check turns on checking that the working tree matches the |
| 23 | // files that are being modified, but doesn't apply the patch |
| 24 | // --stat does just a diffstat, and doesn't actually apply |
| 25 | // --show-files shows the directory changes |
| 26 | // |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 27 | static int merge_patch = 1; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 28 | static int check_index = 0; |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 29 | static int write_index = 0; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 30 | static int diffstat = 0; |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 31 | static int summary = 0; |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 32 | static int check = 0; |
| 33 | static int apply = 1; |
| 34 | static int show_files = 0; |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 35 | static const char apply_usage[] = "git-apply [--stat] [--summary] [--check] [--show-files] <patch>"; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 37 | /* |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 38 | * For "diff-stat" like behaviour, we keep track of the biggest change |
| 39 | * we've seen, and the longest filename. That allows us to do simple |
| 40 | * scaling. |
| 41 | */ |
| 42 | static int max_change, max_len; |
| 43 | |
| 44 | /* |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 45 | * Various "current state", notably line numbers and what |
| 46 | * file (and how) we're patching right now.. The "is_xxxx" |
| 47 | * things are flags, where -1 means "don't know yet". |
| 48 | */ |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 49 | static int linenr = 1; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 50 | |
| 51 | struct fragment { |
| 52 | unsigned long oldpos, oldlines; |
| 53 | unsigned long newpos, newlines; |
| 54 | const char *patch; |
| 55 | int size; |
| 56 | struct fragment *next; |
| 57 | }; |
| 58 | |
| 59 | struct patch { |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 60 | char *new_name, *old_name, *def_name; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 61 | unsigned int old_mode, new_mode; |
| 62 | int is_rename, is_copy, is_new, is_delete; |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 63 | int lines_added, lines_deleted; |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 64 | int score; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 65 | struct fragment *fragments; |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 66 | char *result; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 67 | unsigned long resultsize; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 68 | struct patch *next; |
| 69 | }; |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 70 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 71 | #define CHUNKSIZE (8192) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 72 | #define SLOP (16) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 73 | |
| 74 | static void *read_patch_file(int fd, unsigned long *sizep) |
| 75 | { |
| 76 | unsigned long size = 0, alloc = CHUNKSIZE; |
| 77 | void *buffer = xmalloc(alloc); |
| 78 | |
| 79 | for (;;) { |
| 80 | int nr = alloc - size; |
| 81 | if (nr < 1024) { |
| 82 | alloc += CHUNKSIZE; |
| 83 | buffer = xrealloc(buffer, alloc); |
| 84 | nr = alloc - size; |
| 85 | } |
| 86 | nr = read(fd, buffer + size, nr); |
| 87 | if (!nr) |
| 88 | break; |
| 89 | if (nr < 0) { |
| 90 | if (errno == EAGAIN) |
| 91 | continue; |
| 92 | die("git-apply: read returned %s", strerror(errno)); |
| 93 | } |
| 94 | size += nr; |
| 95 | } |
| 96 | *sizep = size; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Make sure that we have some slop in the buffer |
| 100 | * so that we can do speculative "memcmp" etc, and |
| 101 | * see to it that it is NUL-filled. |
| 102 | */ |
| 103 | if (alloc < size + SLOP) |
| 104 | buffer = xrealloc(buffer, size + SLOP); |
| 105 | memset(buffer + size, 0, SLOP); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 106 | return buffer; |
| 107 | } |
| 108 | |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 109 | static unsigned long linelen(const char *buffer, unsigned long size) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 110 | { |
| 111 | unsigned long len = 0; |
| 112 | while (size--) { |
| 113 | len++; |
| 114 | if (*buffer++ == '\n') |
| 115 | break; |
| 116 | } |
| 117 | return len; |
| 118 | } |
| 119 | |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 120 | static int is_dev_null(const char *str) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 121 | { |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 122 | return !memcmp("/dev/null", str, 9) && isspace(str[9]); |
| 123 | } |
| 124 | |
Linus Torvalds | 381ca9a | 2005-05-31 15:05:59 -0700 | [diff] [blame] | 125 | #define TERM_SPACE 1 |
| 126 | #define TERM_TAB 2 |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 127 | |
| 128 | static int name_terminate(const char *name, int namelen, int c, int terminate) |
| 129 | { |
| 130 | if (c == ' ' && !(terminate & TERM_SPACE)) |
| 131 | return 0; |
| 132 | if (c == '\t' && !(terminate & TERM_TAB)) |
| 133 | return 0; |
| 134 | |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | static char * find_name(const char *line, char *def, int p_value, int terminate) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 139 | { |
| 140 | int len; |
| 141 | const char *start = line; |
| 142 | char *name; |
| 143 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 144 | for (;;) { |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 145 | char c = *line; |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 146 | |
| 147 | if (isspace(c)) { |
| 148 | if (c == '\n') |
| 149 | break; |
| 150 | if (name_terminate(start, line-start, c, terminate)) |
| 151 | break; |
| 152 | } |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 153 | line++; |
| 154 | if (c == '/' && !--p_value) |
| 155 | start = line; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 156 | } |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 157 | if (!start) |
| 158 | return def; |
| 159 | len = line - start; |
| 160 | if (!len) |
| 161 | return def; |
| 162 | |
| 163 | /* |
| 164 | * Generally we prefer the shorter name, especially |
| 165 | * if the other one is just a variation of that with |
| 166 | * something else tacked on to the end (ie "file.orig" |
| 167 | * or "file~"). |
| 168 | */ |
| 169 | if (def) { |
| 170 | int deflen = strlen(def); |
| 171 | if (deflen < len && !strncmp(start, def, deflen)) |
| 172 | return def; |
| 173 | } |
| 174 | |
| 175 | name = xmalloc(len + 1); |
| 176 | memcpy(name, start, len); |
| 177 | name[len] = 0; |
| 178 | free(def); |
| 179 | return name; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Get the name etc info from the --/+++ lines of a traditional patch header |
| 184 | * |
| 185 | * NOTE! This hardcodes "-p1" behaviour in filename detection. |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 186 | * |
| 187 | * FIXME! The end-of-filename heuristics are kind of screwy. For existing |
| 188 | * files, we can happily check the index for a match, but for creating a |
| 189 | * new file we should try to match whatever "patch" does. I have no idea. |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 190 | */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 191 | static void parse_traditional_patch(const char *first, const char *second, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 192 | { |
| 193 | int p_value = 1; |
| 194 | char *name; |
| 195 | |
| 196 | first += 4; // skip "--- " |
| 197 | second += 4; // skip "+++ " |
| 198 | if (is_dev_null(first)) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 199 | patch->is_new = 1; |
| 200 | patch->is_delete = 0; |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 201 | name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 202 | patch->new_name = name; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 203 | } else if (is_dev_null(second)) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 204 | patch->is_new = 0; |
| 205 | patch->is_delete = 1; |
Linus Torvalds | 381ca9a | 2005-05-31 15:05:59 -0700 | [diff] [blame] | 206 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 207 | patch->old_name = name; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 208 | } else { |
Linus Torvalds | 381ca9a | 2005-05-31 15:05:59 -0700 | [diff] [blame] | 209 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
| 210 | name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 211 | patch->old_name = patch->new_name = name; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 212 | } |
| 213 | if (!name) |
| 214 | die("unable to find filename in patch at line %d", linenr); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 217 | static int gitdiff_hdrend(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 218 | { |
| 219 | return -1; |
| 220 | } |
| 221 | |
Linus Torvalds | 1e3f6b6 | 2005-05-23 19:54:55 -0700 | [diff] [blame] | 222 | /* |
| 223 | * We're anal about diff header consistency, to make |
| 224 | * sure that we don't end up having strange ambiguous |
| 225 | * patches floating around. |
| 226 | * |
| 227 | * As a result, gitdiff_{old|new}name() will check |
| 228 | * their names against any previous information, just |
| 229 | * to make sure.. |
| 230 | */ |
| 231 | static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew) |
| 232 | { |
| 233 | int len; |
| 234 | const char *name; |
| 235 | |
| 236 | if (!orig_name && !isnull) |
| 237 | return find_name(line, NULL, 1, 0); |
| 238 | |
| 239 | name = "/dev/null"; |
| 240 | len = 9; |
| 241 | if (orig_name) { |
| 242 | name = orig_name; |
| 243 | len = strlen(name); |
| 244 | if (isnull) |
| 245 | die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); |
| 246 | } |
| 247 | |
| 248 | if (*name == '/') |
| 249 | goto absolute_path; |
| 250 | |
| 251 | for (;;) { |
| 252 | char c = *line++; |
| 253 | if (c == '\n') |
| 254 | break; |
| 255 | if (c != '/') |
| 256 | continue; |
| 257 | absolute_path: |
| 258 | if (memcmp(line, name, len) || line[len] != '\n') |
| 259 | break; |
| 260 | return orig_name; |
| 261 | } |
| 262 | die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 266 | static int gitdiff_oldname(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 267 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 268 | patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old"); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 272 | static int gitdiff_newname(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 273 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 274 | patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new"); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 278 | static int gitdiff_oldmode(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 279 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 280 | patch->old_mode = strtoul(line, NULL, 8); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 284 | static int gitdiff_newmode(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 285 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 286 | patch->new_mode = strtoul(line, NULL, 8); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 290 | static int gitdiff_delete(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 291 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 292 | patch->is_delete = 1; |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 293 | patch->old_name = patch->def_name; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 294 | return gitdiff_oldmode(line, patch); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 297 | static int gitdiff_newfile(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 298 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 299 | patch->is_new = 1; |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 300 | patch->new_name = patch->def_name; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 301 | return gitdiff_newmode(line, patch); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 304 | static int gitdiff_copysrc(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 305 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 306 | patch->is_copy = 1; |
| 307 | patch->old_name = find_name(line, NULL, 0, 0); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 311 | static int gitdiff_copydst(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 312 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 313 | patch->is_copy = 1; |
| 314 | patch->new_name = find_name(line, NULL, 0, 0); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 318 | static int gitdiff_renamesrc(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 319 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 320 | patch->is_rename = 1; |
| 321 | patch->old_name = find_name(line, NULL, 0, 0); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 325 | static int gitdiff_renamedst(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 326 | { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 327 | patch->is_rename = 1; |
| 328 | patch->new_name = find_name(line, NULL, 0, 0); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 332 | static int gitdiff_similarity(const char *line, struct patch *patch) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 333 | { |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 334 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
| 335 | patch->score = 0; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 336 | return 0; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Junio C Hamano | 70aadac | 2005-05-30 16:40:16 -0700 | [diff] [blame] | 339 | static int gitdiff_dissimilarity(const char *line, struct patch *patch) |
| 340 | { |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 341 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
| 342 | patch->score = 0; |
Junio C Hamano | 70aadac | 2005-05-30 16:40:16 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 346 | /* |
| 347 | * This is normal for a diff that doesn't change anything: we'll fall through |
| 348 | * into the next diff. Tell the parser to break out. |
| 349 | */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 350 | static int gitdiff_unrecognized(const char *line, struct patch *patch) |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 351 | { |
| 352 | return -1; |
| 353 | } |
| 354 | |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 355 | static char *git_header_name(char *line) |
| 356 | { |
| 357 | int len; |
| 358 | char *name, *second; |
| 359 | |
| 360 | /* |
| 361 | * Find the first '/' |
| 362 | */ |
| 363 | name = line; |
| 364 | for (;;) { |
| 365 | char c = *name++; |
| 366 | if (c == '\n') |
| 367 | return NULL; |
| 368 | if (c == '/') |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * We don't accept absolute paths (/dev/null) as possibly valid |
| 374 | */ |
| 375 | if (name == line+1) |
| 376 | return NULL; |
| 377 | |
| 378 | /* |
| 379 | * Accept a name only if it shows up twice, exactly the same |
| 380 | * form. |
| 381 | */ |
| 382 | for (len = 0 ; ; len++) { |
| 383 | char c = name[len]; |
| 384 | |
| 385 | switch (c) { |
| 386 | default: |
| 387 | continue; |
| 388 | case '\n': |
| 389 | break; |
| 390 | case '\t': case ' ': |
| 391 | second = name+len; |
| 392 | for (;;) { |
| 393 | char c = *second++; |
| 394 | if (c == '\n') |
| 395 | return NULL; |
| 396 | if (c == '/') |
| 397 | break; |
| 398 | } |
Linus Torvalds | 0e87e04 | 2005-05-26 13:28:42 -0700 | [diff] [blame] | 399 | if (second[len] == '\n' && !memcmp(name, second, len)) { |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 400 | char *ret = xmalloc(len + 1); |
| 401 | memcpy(ret, name, len); |
| 402 | ret[len] = 0; |
| 403 | return ret; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 410 | /* Verify that we recognize the lines following a git header */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 411 | static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 412 | { |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 413 | unsigned long offset; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 414 | |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 415 | /* A git diff has explicit new/delete information, so we don't guess */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 416 | patch->is_new = 0; |
| 417 | patch->is_delete = 0; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 418 | |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 419 | /* |
| 420 | * Some things may not have the old name in the |
| 421 | * rest of the headers anywhere (pure mode changes, |
| 422 | * or removing or adding empty files), so we get |
| 423 | * the default name from the header. |
| 424 | */ |
| 425 | patch->def_name = git_header_name(line + strlen("diff --git ")); |
| 426 | |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 427 | line += len; |
| 428 | size -= len; |
| 429 | linenr++; |
| 430 | for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) { |
| 431 | static const struct opentry { |
| 432 | const char *str; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 433 | int (*fn)(const char *, struct patch *); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 434 | } optable[] = { |
| 435 | { "@@ -", gitdiff_hdrend }, |
| 436 | { "--- ", gitdiff_oldname }, |
| 437 | { "+++ ", gitdiff_newname }, |
| 438 | { "old mode ", gitdiff_oldmode }, |
| 439 | { "new mode ", gitdiff_newmode }, |
| 440 | { "deleted file mode ", gitdiff_delete }, |
| 441 | { "new file mode ", gitdiff_newfile }, |
| 442 | { "copy from ", gitdiff_copysrc }, |
| 443 | { "copy to ", gitdiff_copydst }, |
Linus Torvalds | 33f4d08 | 2005-06-05 14:26:50 -0700 | [diff] [blame] | 444 | { "rename old ", gitdiff_renamesrc }, |
| 445 | { "rename new ", gitdiff_renamedst }, |
Linus Torvalds | dc93841 | 2005-06-05 15:31:52 -0700 | [diff] [blame] | 446 | { "rename from ", gitdiff_renamesrc }, |
| 447 | { "rename to ", gitdiff_renamedst }, |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 448 | { "similarity index ", gitdiff_similarity }, |
Junio C Hamano | 70aadac | 2005-05-30 16:40:16 -0700 | [diff] [blame] | 449 | { "dissimilarity index ", gitdiff_dissimilarity }, |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 450 | { "", gitdiff_unrecognized }, |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 451 | }; |
| 452 | int i; |
| 453 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 454 | len = linelen(line, size); |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 455 | if (!len || line[len-1] != '\n') |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 456 | break; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 457 | for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) { |
| 458 | const struct opentry *p = optable + i; |
| 459 | int oplen = strlen(p->str); |
| 460 | if (len < oplen || memcmp(p->str, line, oplen)) |
| 461 | continue; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 462 | if (p->fn(line + oplen, patch) < 0) |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 463 | return offset; |
Linus Torvalds | 9a4a100 | 2005-05-23 19:13:55 -0700 | [diff] [blame] | 464 | break; |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 465 | } |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 468 | return offset; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 471 | static int parse_num(const char *line, unsigned long *p) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 472 | { |
| 473 | char *ptr; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 474 | |
| 475 | if (!isdigit(*line)) |
| 476 | return 0; |
| 477 | *p = strtoul(line, &ptr, 10); |
| 478 | return ptr - line; |
| 479 | } |
| 480 | |
| 481 | static int parse_range(const char *line, int len, int offset, const char *expect, |
| 482 | unsigned long *p1, unsigned long *p2) |
| 483 | { |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 484 | int digits, ex; |
| 485 | |
| 486 | if (offset < 0 || offset >= len) |
| 487 | return -1; |
| 488 | line += offset; |
| 489 | len -= offset; |
| 490 | |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 491 | digits = parse_num(line, p1); |
| 492 | if (!digits) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 493 | return -1; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 494 | |
| 495 | offset += digits; |
| 496 | line += digits; |
| 497 | len -= digits; |
| 498 | |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 499 | *p2 = *p1; |
| 500 | if (*line == ',') { |
| 501 | digits = parse_num(line+1, p2); |
| 502 | if (!digits) |
| 503 | return -1; |
| 504 | |
| 505 | offset += digits+1; |
| 506 | line += digits+1; |
| 507 | len -= digits+1; |
| 508 | } |
| 509 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 510 | ex = strlen(expect); |
| 511 | if (ex > len) |
| 512 | return -1; |
| 513 | if (memcmp(line, expect, ex)) |
| 514 | return -1; |
| 515 | |
| 516 | return offset + ex; |
| 517 | } |
| 518 | |
| 519 | /* |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 520 | * Parse a unified diff fragment header of the |
| 521 | * form "@@ -a,b +c,d @@" |
| 522 | */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 523 | static int parse_fragment_header(char *line, int len, struct fragment *fragment) |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 524 | { |
| 525 | int offset; |
| 526 | |
| 527 | if (!len || line[len-1] != '\n') |
| 528 | return -1; |
| 529 | |
| 530 | /* Figure out the number of lines in a fragment */ |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 531 | offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); |
| 532 | offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 533 | |
| 534 | return offset; |
| 535 | } |
| 536 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 537 | static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch) |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 538 | { |
| 539 | unsigned long offset, len; |
| 540 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 541 | patch->is_rename = patch->is_copy = 0; |
| 542 | patch->is_new = patch->is_delete = -1; |
| 543 | patch->old_mode = patch->new_mode = 0; |
| 544 | patch->old_name = patch->new_name = NULL; |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 545 | for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) { |
| 546 | unsigned long nextlen; |
| 547 | |
| 548 | len = linelen(line, size); |
| 549 | if (!len) |
| 550 | break; |
| 551 | |
| 552 | /* Testing this early allows us to take a few shortcuts.. */ |
| 553 | if (len < 6) |
| 554 | continue; |
| 555 | |
| 556 | /* |
| 557 | * Make sure we don't find any unconnected patch fragmants. |
| 558 | * That's a sign that we didn't find a header, and that a |
| 559 | * patch has become corrupted/broken up. |
| 560 | */ |
| 561 | if (!memcmp("@@ -", line, 4)) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 562 | struct fragment dummy; |
| 563 | if (parse_fragment_header(line, len, &dummy) < 0) |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 564 | continue; |
| 565 | error("patch fragment without header at line %d: %.*s", linenr, len-1, line); |
| 566 | } |
| 567 | |
| 568 | if (size < len + 6) |
| 569 | break; |
| 570 | |
| 571 | /* |
| 572 | * Git patch? It might not have a real patch, just a rename |
| 573 | * or mode change, so we handle that specially |
| 574 | */ |
| 575 | if (!memcmp("diff --git ", line, 11)) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 576 | int git_hdr_len = parse_git_header(line, len, size, patch); |
Linus Torvalds | 206de27 | 2005-06-12 09:37:49 -0700 | [diff] [blame] | 577 | if (git_hdr_len <= len) |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 578 | continue; |
Linus Torvalds | b7e8039 | 2005-06-17 15:23:40 -0700 | [diff] [blame] | 579 | if (!patch->old_name && !patch->new_name) { |
| 580 | if (!patch->def_name) |
| 581 | die("git diff header lacks filename information (line %d)", linenr); |
| 582 | patch->old_name = patch->new_name = patch->def_name; |
| 583 | } |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 584 | *hdrsize = git_hdr_len; |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 585 | return offset; |
| 586 | } |
| 587 | |
| 588 | /** --- followed by +++ ? */ |
| 589 | if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4)) |
| 590 | continue; |
| 591 | |
| 592 | /* |
| 593 | * We only accept unified patches, so we want it to |
| 594 | * at least have "@@ -a,b +c,d @@\n", which is 14 chars |
| 595 | * minimum |
| 596 | */ |
| 597 | nextlen = linelen(line + len, size - len); |
| 598 | if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4)) |
| 599 | continue; |
| 600 | |
| 601 | /* Ok, we'll consider it a patch */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 602 | parse_traditional_patch(line, line+len, patch); |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 603 | *hdrsize = len + nextlen; |
| 604 | linenr += 2; |
| 605 | return offset; |
| 606 | } |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | /* |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 611 | * Parse a unified diff. Note that this really needs |
| 612 | * to parse each fragment separately, since the only |
| 613 | * way to know the difference between a "---" that is |
| 614 | * part of a patch, and a "---" that starts the next |
| 615 | * patch is to look at the line counts.. |
| 616 | */ |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 617 | static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 618 | { |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 619 | int added, deleted; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 620 | int len = linelen(line, size), offset; |
Linus Torvalds | 3099665 | 2005-06-05 12:43:56 -0700 | [diff] [blame] | 621 | unsigned long oldlines, newlines; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 622 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 623 | offset = parse_fragment_header(line, len, fragment); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 624 | if (offset < 0) |
| 625 | return -1; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 626 | oldlines = fragment->oldlines; |
| 627 | newlines = fragment->newlines; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 628 | |
Linus Torvalds | 3099665 | 2005-06-05 12:43:56 -0700 | [diff] [blame] | 629 | if (patch->is_new < 0) { |
| 630 | patch->is_new = !oldlines; |
| 631 | if (!oldlines) |
| 632 | patch->old_name = NULL; |
| 633 | } |
| 634 | if (patch->is_delete < 0) { |
| 635 | patch->is_delete = !newlines; |
| 636 | if (!newlines) |
| 637 | patch->new_name = NULL; |
| 638 | } |
| 639 | |
| 640 | if (patch->is_new != !oldlines) |
| 641 | return error("new file depends on old contents"); |
Linus Torvalds | af3f929 | 2005-06-08 08:11:47 -0700 | [diff] [blame] | 642 | if (patch->is_delete != !newlines) { |
| 643 | if (newlines) |
| 644 | return error("deleted file still has contents"); |
| 645 | fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name); |
| 646 | } |
Linus Torvalds | a4acb0e | 2005-05-23 16:09:09 -0700 | [diff] [blame] | 647 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 648 | /* Parse the thing.. */ |
| 649 | line += len; |
| 650 | size -= len; |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 651 | linenr++; |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 652 | added = deleted = 0; |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 653 | for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) { |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 654 | if (!oldlines && !newlines) |
| 655 | break; |
| 656 | len = linelen(line, size); |
| 657 | if (!len || line[len-1] != '\n') |
| 658 | return -1; |
| 659 | switch (*line) { |
| 660 | default: |
| 661 | return -1; |
| 662 | case ' ': |
| 663 | oldlines--; |
| 664 | newlines--; |
| 665 | break; |
| 666 | case '-': |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 667 | deleted++; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 668 | oldlines--; |
| 669 | break; |
| 670 | case '+': |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 671 | added++; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 672 | newlines--; |
| 673 | break; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 674 | /* We allow "\ No newline at end of file" */ |
| 675 | case '\\': |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 676 | if (len < 12 || memcmp(line, "\\ No newline", 12)) |
| 677 | return -1; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 678 | break; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 681 | patch->lines_added += added; |
| 682 | patch->lines_deleted += deleted; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 683 | return offset; |
| 684 | } |
| 685 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 686 | static int parse_single_patch(char *line, unsigned long size, struct patch *patch) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 687 | { |
| 688 | unsigned long offset = 0; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 689 | struct fragment **fragp = &patch->fragments; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 690 | |
| 691 | while (size > 4 && !memcmp(line, "@@ -", 4)) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 692 | struct fragment *fragment; |
| 693 | int len; |
| 694 | |
| 695 | fragment = xmalloc(sizeof(*fragment)); |
| 696 | memset(fragment, 0, sizeof(*fragment)); |
| 697 | len = parse_fragment(line, size, patch, fragment); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 698 | if (len <= 0) |
Linus Torvalds | 46979f5 | 2005-05-23 14:38:49 -0700 | [diff] [blame] | 699 | die("corrupt patch at line %d", linenr); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 700 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 701 | fragment->patch = line; |
| 702 | fragment->size = len; |
| 703 | |
| 704 | *fragp = fragment; |
| 705 | fragp = &fragment->next; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 706 | |
| 707 | offset += len; |
| 708 | line += len; |
| 709 | size -= len; |
| 710 | } |
| 711 | return offset; |
| 712 | } |
| 713 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 714 | static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 715 | { |
| 716 | int hdrsize, patchsize; |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 717 | int offset = find_header(buffer, size, &hdrsize, patch); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 718 | |
| 719 | if (offset < 0) |
| 720 | return offset; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 721 | |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 722 | patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 723 | |
| 724 | return offset + hdrsize + patchsize; |
| 725 | } |
| 726 | |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 727 | const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; |
| 728 | const char minuses[]= "----------------------------------------------------------------------"; |
| 729 | |
| 730 | static void show_stats(struct patch *patch) |
| 731 | { |
Junio C Hamano | 03b4538 | 2005-06-22 02:29:16 -0700 | [diff] [blame] | 732 | char *name = patch->new_name; |
Linus Torvalds | 95bedc9 | 2005-05-31 20:50:49 -0700 | [diff] [blame] | 733 | int len, max, add, del, total; |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 734 | |
Linus Torvalds | 5041aa7 | 2005-05-26 13:11:24 -0700 | [diff] [blame] | 735 | if (!name) |
Junio C Hamano | 03b4538 | 2005-06-22 02:29:16 -0700 | [diff] [blame] | 736 | name = patch->old_name; |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 737 | |
| 738 | /* |
| 739 | * "scale" the filename |
| 740 | */ |
| 741 | len = strlen(name); |
| 742 | max = max_len; |
| 743 | if (max > 50) |
| 744 | max = 50; |
| 745 | if (len > max) |
| 746 | name += len - max; |
| 747 | len = max; |
| 748 | |
| 749 | /* |
| 750 | * scale the add/delete |
| 751 | */ |
| 752 | max = max_change; |
| 753 | if (max + len > 70) |
| 754 | max = 70 - len; |
Linus Torvalds | 95bedc9 | 2005-05-31 20:50:49 -0700 | [diff] [blame] | 755 | |
| 756 | add = patch->lines_added; |
| 757 | del = patch->lines_deleted; |
| 758 | total = add + del; |
| 759 | |
Sven Verdoolaege | 69f956e | 2005-06-21 17:14:30 +0200 | [diff] [blame] | 760 | if (max_change > 0) { |
| 761 | total = (total * max + max_change / 2) / max_change; |
| 762 | add = (add * max + max_change / 2) / max_change; |
| 763 | del = total - add; |
| 764 | } |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 765 | printf(" %-*s |%5d %.*s%.*s\n", |
| 766 | len, name, patch->lines_added + patch->lines_deleted, |
| 767 | add, pluses, del, minuses); |
| 768 | } |
| 769 | |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 770 | static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size) |
| 771 | { |
| 772 | int fd; |
| 773 | unsigned long got; |
| 774 | |
| 775 | switch (st->st_mode & S_IFMT) { |
| 776 | case S_IFLNK: |
| 777 | return readlink(path, buf, size); |
| 778 | case S_IFREG: |
| 779 | fd = open(path, O_RDONLY); |
| 780 | if (fd < 0) |
| 781 | return error("unable to open %s", path); |
| 782 | got = 0; |
| 783 | for (;;) { |
| 784 | int ret = read(fd, buf + got, size - got); |
| 785 | if (ret < 0) { |
| 786 | if (errno == EAGAIN) |
| 787 | continue; |
| 788 | break; |
| 789 | } |
| 790 | if (!ret) |
| 791 | break; |
| 792 | got += ret; |
| 793 | } |
| 794 | close(fd); |
| 795 | return got; |
| 796 | |
| 797 | default: |
| 798 | return -1; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line) |
| 803 | { |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 804 | int i; |
| 805 | unsigned long start, backwards, forwards; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 806 | |
| 807 | if (fragsize > size) |
| 808 | return -1; |
| 809 | |
| 810 | start = 0; |
| 811 | if (line > 1) { |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 812 | unsigned long offset = 0; |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 813 | i = line-1; |
| 814 | while (offset + fragsize <= size) { |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 815 | if (buf[offset++] == '\n') { |
| 816 | start = offset; |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 817 | if (!--i) |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* Exact line number? */ |
| 824 | if (!memcmp(buf + start, fragment, fragsize)) |
| 825 | return start; |
| 826 | |
| 827 | /* |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 828 | * There's probably some smart way to do this, but I'll leave |
| 829 | * that to the smart and beautiful people. I'm simple and stupid. |
| 830 | */ |
| 831 | backwards = start; |
| 832 | forwards = start; |
| 833 | for (i = 0; ; i++) { |
| 834 | unsigned long try; |
| 835 | int n; |
| 836 | |
| 837 | /* "backward" */ |
| 838 | if (i & 1) { |
| 839 | if (!backwards) { |
| 840 | if (forwards + fragsize > size) |
| 841 | break; |
| 842 | continue; |
| 843 | } |
| 844 | do { |
| 845 | --backwards; |
| 846 | } while (backwards && buf[backwards-1] != '\n'); |
| 847 | try = backwards; |
| 848 | } else { |
| 849 | while (forwards + fragsize <= size) { |
| 850 | if (buf[forwards++] == '\n') |
| 851 | break; |
| 852 | } |
| 853 | try = forwards; |
| 854 | } |
| 855 | |
| 856 | if (try + fragsize > size) |
| 857 | continue; |
| 858 | if (memcmp(buf + try, fragment, fragsize)) |
| 859 | continue; |
| 860 | n = (i >> 1)+1; |
| 861 | if (i & 1) |
| 862 | n = -n; |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 863 | return try; |
| 864 | } |
| 865 | |
| 866 | /* |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 867 | * We should start searching forward and backward. |
| 868 | */ |
| 869 | return -1; |
| 870 | } |
| 871 | |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 872 | struct buffer_desc { |
| 873 | char *buffer; |
| 874 | unsigned long size; |
| 875 | unsigned long alloc; |
| 876 | }; |
| 877 | |
| 878 | static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 879 | { |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 880 | char *buf = desc->buffer; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 881 | const char *patch = frag->patch; |
| 882 | int offset, size = frag->size; |
| 883 | char *old = xmalloc(size); |
| 884 | char *new = xmalloc(size); |
| 885 | int oldsize = 0, newsize = 0; |
| 886 | |
| 887 | while (size > 0) { |
| 888 | int len = linelen(patch, size); |
| 889 | int plen; |
| 890 | |
| 891 | if (!len) |
| 892 | break; |
| 893 | |
| 894 | /* |
| 895 | * "plen" is how much of the line we should use for |
| 896 | * the actual patch data. Normally we just remove the |
| 897 | * first character on the line, but if the line is |
| 898 | * followed by "\ No newline", then we also remove the |
| 899 | * last one (which is the newline, of course). |
| 900 | */ |
| 901 | plen = len-1; |
| 902 | if (len > size && patch[len] == '\\') |
| 903 | plen--; |
| 904 | switch (*patch) { |
| 905 | case ' ': |
| 906 | case '-': |
| 907 | memcpy(old + oldsize, patch + 1, plen); |
| 908 | oldsize += plen; |
| 909 | if (*patch == '-') |
| 910 | break; |
| 911 | /* Fall-through for ' ' */ |
| 912 | case '+': |
| 913 | memcpy(new + newsize, patch + 1, plen); |
| 914 | newsize += plen; |
| 915 | break; |
| 916 | case '@': case '\\': |
| 917 | /* Ignore it, we already handled it */ |
| 918 | break; |
| 919 | default: |
| 920 | return -1; |
| 921 | } |
| 922 | patch += len; |
| 923 | size -= len; |
| 924 | } |
| 925 | |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 926 | offset = find_offset(buf, desc->size, old, oldsize, frag->newpos); |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 927 | if (offset >= 0) { |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 928 | int diff = newsize - oldsize; |
| 929 | unsigned long size = desc->size + diff; |
| 930 | unsigned long alloc = desc->alloc; |
| 931 | |
| 932 | if (size > alloc) { |
| 933 | alloc = size + 8192; |
| 934 | desc->alloc = alloc; |
| 935 | buf = xrealloc(buf, alloc); |
| 936 | desc->buffer = buf; |
| 937 | } |
| 938 | desc->size = size; |
| 939 | memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize); |
| 940 | memcpy(buf + offset, new, newsize); |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 941 | offset = 0; |
| 942 | } |
| 943 | |
| 944 | free(old); |
| 945 | free(new); |
| 946 | return offset; |
| 947 | } |
| 948 | |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 949 | static int apply_fragments(struct buffer_desc *desc, struct patch *patch) |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 950 | { |
| 951 | struct fragment *frag = patch->fragments; |
| 952 | |
| 953 | while (frag) { |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 954 | if (apply_one_fragment(desc, frag) < 0) |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 955 | return error("patch failed: %s:%d", patch->old_name, frag->oldpos); |
| 956 | frag = frag->next; |
| 957 | } |
Linus Torvalds | 3099665 | 2005-06-05 12:43:56 -0700 | [diff] [blame] | 958 | return 0; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static int apply_data(struct patch *patch, struct stat *st) |
| 962 | { |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 963 | char *buf; |
| 964 | unsigned long size, alloc; |
| 965 | struct buffer_desc desc; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 966 | |
Linus Torvalds | 3099665 | 2005-06-05 12:43:56 -0700 | [diff] [blame] | 967 | size = 0; |
| 968 | alloc = 0; |
| 969 | buf = NULL; |
| 970 | if (patch->old_name) { |
| 971 | size = st->st_size; |
| 972 | alloc = size + 8192; |
| 973 | buf = xmalloc(alloc); |
| 974 | if (read_old_data(st, patch->old_name, buf, alloc) != size) |
| 975 | return error("read of %s failed", patch->old_name); |
| 976 | } |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 977 | |
| 978 | desc.size = size; |
| 979 | desc.alloc = alloc; |
| 980 | desc.buffer = buf; |
| 981 | if (apply_fragments(&desc, patch) < 0) |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 982 | return -1; |
Linus Torvalds | 6e7c92a | 2005-06-05 12:16:32 -0700 | [diff] [blame] | 983 | patch->result = desc.buffer; |
| 984 | patch->resultsize = desc.size; |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 985 | |
| 986 | if (patch->is_delete && patch->resultsize) |
| 987 | return error("removal patch leaves file contents"); |
| 988 | |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 992 | static int check_patch(struct patch *patch) |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 993 | { |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 994 | struct stat st; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 995 | const char *old_name = patch->old_name; |
| 996 | const char *new_name = patch->new_name; |
| 997 | |
| 998 | if (old_name) { |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 999 | int changed; |
| 1000 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1001 | if (lstat(old_name, &st) < 0) |
Linus Torvalds | 84fb9a4 | 2005-06-12 21:04:27 -0700 | [diff] [blame] | 1002 | return error("%s: %s", old_name, strerror(errno)); |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 1003 | if (check_index) { |
| 1004 | int pos = cache_name_pos(old_name, strlen(old_name)); |
| 1005 | if (pos < 0) |
| 1006 | return error("%s: does not exist in index", old_name); |
| 1007 | changed = ce_match_stat(active_cache[pos], &st); |
| 1008 | if (changed) |
| 1009 | return error("%s: does not match index", old_name); |
| 1010 | } |
| 1011 | if (patch->is_new < 0) |
| 1012 | patch->is_new = 0; |
Linus Torvalds | de4971b | 2005-06-13 20:41:38 -0700 | [diff] [blame] | 1013 | st.st_mode = ntohl(create_ce_mode(st.st_mode)); |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1014 | if (!patch->old_mode) |
| 1015 | patch->old_mode = st.st_mode; |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 1016 | if ((st.st_mode ^ patch->old_mode) & S_IFMT) |
| 1017 | return error("%s: wrong type", old_name); |
| 1018 | if (st.st_mode != patch->old_mode) |
| 1019 | fprintf(stderr, "warning: %s has type %o, expected %o\n", |
| 1020 | old_name, st.st_mode, patch->old_mode); |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 1021 | } |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1022 | |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 1023 | if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) { |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 1024 | if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0) |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1025 | return error("%s: already exists in index", new_name); |
| 1026 | if (!lstat(new_name, &st)) |
| 1027 | return error("%s: already exists in working directory", new_name); |
| 1028 | if (errno != ENOENT) |
| 1029 | return error("%s: %s", new_name, strerror(errno)); |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1030 | if (!patch->new_mode) |
| 1031 | patch->new_mode = S_IFREG | 0644; |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1032 | } |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (new_name && old_name) { |
| 1035 | int same = !strcmp(old_name, new_name); |
| 1036 | if (!patch->new_mode) |
| 1037 | patch->new_mode = patch->old_mode; |
| 1038 | if ((patch->old_mode ^ patch->new_mode) & S_IFMT) |
| 1039 | return error("new mode (%o) of %s does not match old mode (%o)%s%s", |
| 1040 | patch->new_mode, new_name, patch->old_mode, |
| 1041 | same ? "" : " of ", same ? "" : old_name); |
| 1042 | } |
| 1043 | |
| 1044 | if (apply_data(patch, &st) < 0) |
| 1045 | return error("%s: patch does not apply", old_name); |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static int check_patch_list(struct patch *patch) |
| 1050 | { |
| 1051 | int error = 0; |
| 1052 | |
| 1053 | for (;patch ; patch = patch->next) |
| 1054 | error |= check_patch(patch); |
| 1055 | return error; |
| 1056 | } |
| 1057 | |
| 1058 | static void show_file(int c, unsigned int mode, const char *name) |
| 1059 | { |
| 1060 | printf("%c %o %s\n", c, mode, name); |
| 1061 | } |
| 1062 | |
| 1063 | static void show_file_list(struct patch *patch) |
| 1064 | { |
| 1065 | for (;patch ; patch = patch->next) { |
| 1066 | if (patch->is_rename) { |
| 1067 | show_file('-', patch->old_mode, patch->old_name); |
| 1068 | show_file('+', patch->new_mode, patch->new_name); |
| 1069 | continue; |
| 1070 | } |
| 1071 | if (patch->is_copy || patch->is_new) { |
| 1072 | show_file('+', patch->new_mode, patch->new_name); |
| 1073 | continue; |
| 1074 | } |
| 1075 | if (patch->is_delete) { |
| 1076 | show_file('-', patch->old_mode, patch->old_name); |
| 1077 | continue; |
| 1078 | } |
| 1079 | if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) { |
| 1080 | printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name); |
| 1081 | continue; |
| 1082 | } |
| 1083 | printf("M %o %s\n", patch->old_mode, patch->old_name); |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1087 | static void stat_patch_list(struct patch *patch) |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1088 | { |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 1089 | int files, adds, dels; |
| 1090 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1091 | for (files = adds = dels = 0 ; patch ; patch = patch->next) { |
| 1092 | files++; |
| 1093 | adds += patch->lines_added; |
| 1094 | dels += patch->lines_deleted; |
| 1095 | show_stats(patch); |
| 1096 | } |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1097 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1098 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels); |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 1101 | static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) |
| 1102 | { |
| 1103 | if (mode) |
| 1104 | printf(" %s mode %06o %s\n", newdelete, mode, name); |
| 1105 | else |
| 1106 | printf(" %s %s\n", newdelete, name); |
| 1107 | } |
| 1108 | |
| 1109 | static void show_mode_change(struct patch *p, int show_name) |
| 1110 | { |
| 1111 | if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) { |
| 1112 | if (show_name) |
| 1113 | printf(" mode change %06o => %06o %s\n", |
| 1114 | p->old_mode, p->new_mode, p->new_name); |
| 1115 | else |
| 1116 | printf(" mode change %06o => %06o\n", |
| 1117 | p->old_mode, p->new_mode); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | static void show_rename_copy(struct patch *p) |
| 1122 | { |
| 1123 | const char *renamecopy = p->is_rename ? "rename" : "copy"; |
| 1124 | const char *old, *new; |
| 1125 | |
| 1126 | /* Find common prefix */ |
| 1127 | old = p->old_name; |
| 1128 | new = p->new_name; |
| 1129 | while (1) { |
| 1130 | const char *slash_old, *slash_new; |
| 1131 | slash_old = strchr(old, '/'); |
| 1132 | slash_new = strchr(new, '/'); |
| 1133 | if (!slash_old || |
| 1134 | !slash_new || |
| 1135 | slash_old - old != slash_new - new || |
| 1136 | memcmp(old, new, slash_new - new)) |
| 1137 | break; |
| 1138 | old = slash_old + 1; |
| 1139 | new = slash_new + 1; |
| 1140 | } |
| 1141 | /* p->old_name thru old is the common prefix, and old and new |
| 1142 | * through the end of names are renames |
| 1143 | */ |
| 1144 | if (old != p->old_name) |
| 1145 | printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, |
| 1146 | old - p->old_name, p->old_name, |
| 1147 | old, new, p->score); |
| 1148 | else |
| 1149 | printf(" %s %s => %s (%d%%)\n", renamecopy, |
| 1150 | p->old_name, p->new_name, p->score); |
| 1151 | show_mode_change(p, 0); |
| 1152 | } |
| 1153 | |
| 1154 | static void summary_patch_list(struct patch *patch) |
| 1155 | { |
| 1156 | struct patch *p; |
| 1157 | |
| 1158 | for (p = patch; p; p = p->next) { |
| 1159 | if (p->is_new) |
| 1160 | show_file_mode_name("create", p->new_mode, p->new_name); |
| 1161 | else if (p->is_delete) |
| 1162 | show_file_mode_name("delete", p->old_mode, p->old_name); |
| 1163 | else { |
| 1164 | if (p->is_rename || p->is_copy) |
| 1165 | show_rename_copy(p); |
| 1166 | else { |
| 1167 | if (p->score) { |
| 1168 | printf(" rewrite %s (%d%%)\n", |
| 1169 | p->new_name, p->score); |
| 1170 | show_mode_change(p, 0); |
| 1171 | } |
| 1172 | else |
| 1173 | show_mode_change(p, 1); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 1179 | static void patch_stats(struct patch *patch) |
| 1180 | { |
| 1181 | int lines = patch->lines_added + patch->lines_deleted; |
| 1182 | |
| 1183 | if (lines > max_change) |
| 1184 | max_change = lines; |
| 1185 | if (patch->old_name) { |
| 1186 | int len = strlen(patch->old_name); |
| 1187 | if (len > max_len) |
| 1188 | max_len = len; |
| 1189 | } |
| 1190 | if (patch->new_name) { |
| 1191 | int len = strlen(patch->new_name); |
| 1192 | if (len > max_len) |
| 1193 | max_len = len; |
| 1194 | } |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1197 | static void remove_file(struct patch *patch) |
| 1198 | { |
| 1199 | if (write_index) { |
| 1200 | if (remove_file_from_cache(patch->old_name) < 0) |
| 1201 | die("unable to remove %s from index", patch->old_name); |
| 1202 | } |
| 1203 | unlink(patch->old_name); |
| 1204 | } |
| 1205 | |
| 1206 | static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size) |
| 1207 | { |
| 1208 | struct stat st; |
| 1209 | struct cache_entry *ce; |
| 1210 | int namelen = strlen(path); |
| 1211 | unsigned ce_size = cache_entry_size(namelen); |
| 1212 | |
| 1213 | if (!write_index) |
| 1214 | return; |
| 1215 | |
| 1216 | ce = xmalloc(ce_size); |
| 1217 | memset(ce, 0, ce_size); |
| 1218 | memcpy(ce->name, path, namelen); |
| 1219 | ce->ce_mode = create_ce_mode(mode); |
| 1220 | ce->ce_flags = htons(namelen); |
| 1221 | if (lstat(path, &st) < 0) |
| 1222 | die("unable to stat newly created file %s", path); |
| 1223 | fill_stat_cache_info(ce, &st); |
| 1224 | if (write_sha1_file(buf, size, "blob", ce->sha1) < 0) |
| 1225 | die("unable to create backing store for newly created file %s", path); |
| 1226 | if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) |
| 1227 | die("unable to add cache entry for %s", path); |
| 1228 | } |
| 1229 | |
Linus Torvalds | 5c8af18 | 2005-06-21 19:10:21 -0700 | [diff] [blame] | 1230 | static void create_subdirectories(const char *path) |
| 1231 | { |
| 1232 | int len = strlen(path); |
| 1233 | char *buf = xmalloc(len + 1); |
| 1234 | const char *slash = path; |
| 1235 | |
| 1236 | while ((slash = strchr(slash+1, '/')) != NULL) { |
| 1237 | len = slash - path; |
| 1238 | memcpy(buf, path, len); |
| 1239 | buf[len] = 0; |
| 1240 | if (mkdir(buf, 0755) < 0) { |
| 1241 | if (errno != EEXIST) |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | free(buf); |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * We optimistically assume that the directories exist, |
| 1250 | * which is true 99% of the time anyway. If they don't, |
| 1251 | * we create them and try again. |
| 1252 | */ |
| 1253 | static int create_regular_file(const char *path, unsigned int mode) |
| 1254 | { |
| 1255 | int ret = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 1256 | |
| 1257 | if (ret < 0 && errno == ENOENT) { |
| 1258 | create_subdirectories(path); |
| 1259 | ret = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 1260 | } |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | static int create_symlink(const char *buf, const char *path) |
| 1265 | { |
| 1266 | int ret = symlink(buf, path); |
| 1267 | |
| 1268 | if (ret < 0 && errno == ENOENT) { |
| 1269 | create_subdirectories(path); |
| 1270 | ret = symlink(buf, path); |
| 1271 | } |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1275 | static void create_file(struct patch *patch) |
| 1276 | { |
| 1277 | const char *path = patch->new_name; |
| 1278 | unsigned mode = patch->new_mode; |
| 1279 | unsigned long size = patch->resultsize; |
| 1280 | char *buf = patch->result; |
| 1281 | |
| 1282 | if (!mode) |
| 1283 | mode = S_IFREG | 0644; |
| 1284 | if (S_ISREG(mode)) { |
| 1285 | int fd; |
| 1286 | mode = (mode & 0100) ? 0777 : 0666; |
Linus Torvalds | 5c8af18 | 2005-06-21 19:10:21 -0700 | [diff] [blame] | 1287 | fd = create_regular_file(path, mode); |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1288 | if (fd < 0) |
| 1289 | die("unable to create file %s (%s)", path, strerror(errno)); |
| 1290 | if (write(fd, buf, size) != size) |
| 1291 | die("unable to write file %s", path); |
| 1292 | close(fd); |
| 1293 | add_index_file(path, mode, buf, size); |
| 1294 | return; |
| 1295 | } |
| 1296 | if (S_ISLNK(mode)) { |
| 1297 | if (size && buf[size-1] == '\n') |
| 1298 | size--; |
| 1299 | buf[size] = 0; |
Linus Torvalds | 5c8af18 | 2005-06-21 19:10:21 -0700 | [diff] [blame] | 1300 | if (create_symlink(buf, path) < 0) |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1301 | die("unable to write symlink %s", path); |
| 1302 | add_index_file(path, mode, buf, size); |
| 1303 | return; |
| 1304 | } |
| 1305 | die("unable to write file mode %o", mode); |
| 1306 | } |
| 1307 | |
| 1308 | static void write_out_one_result(struct patch *patch) |
| 1309 | { |
| 1310 | if (patch->is_delete > 0) { |
| 1311 | remove_file(patch); |
| 1312 | return; |
| 1313 | } |
| 1314 | if (patch->is_new > 0 || patch->is_copy) { |
| 1315 | create_file(patch); |
| 1316 | return; |
| 1317 | } |
| 1318 | /* |
| 1319 | * Rename or modification boils down to the same |
| 1320 | * thing: remove the old, write the new |
| 1321 | */ |
| 1322 | remove_file(patch); |
| 1323 | create_file(patch); |
| 1324 | } |
| 1325 | |
| 1326 | static void write_out_results(struct patch *list) |
| 1327 | { |
Linus Torvalds | f7b7970 | 2005-06-05 15:25:28 -0700 | [diff] [blame] | 1328 | if (!list) |
| 1329 | die("No changes"); |
| 1330 | |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1331 | while (list) { |
| 1332 | write_out_one_result(list); |
| 1333 | list = list->next; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | static struct cache_file cache_file; |
| 1338 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1339 | static int apply_patch(int fd) |
| 1340 | { |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1341 | int newfd; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1342 | unsigned long offset, size; |
| 1343 | char *buffer = read_patch_file(fd, &size); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1344 | struct patch *list = NULL, **listp = &list; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1345 | |
| 1346 | if (!buffer) |
| 1347 | return -1; |
| 1348 | offset = 0; |
| 1349 | while (size > 0) { |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1350 | struct patch *patch; |
| 1351 | int nr; |
| 1352 | |
| 1353 | patch = xmalloc(sizeof(*patch)); |
| 1354 | memset(patch, 0, sizeof(*patch)); |
| 1355 | nr = parse_chunk(buffer + offset, size, patch); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1356 | if (nr < 0) |
| 1357 | break; |
Linus Torvalds | 3f40315 | 2005-05-26 11:40:43 -0700 | [diff] [blame] | 1358 | patch_stats(patch); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1359 | *listp = patch; |
| 1360 | listp = &patch->next; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1361 | offset += nr; |
| 1362 | size -= nr; |
| 1363 | } |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1364 | |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1365 | newfd = -1; |
| 1366 | write_index = check_index && apply; |
| 1367 | if (write_index) |
| 1368 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
| 1369 | if (check_index) { |
| 1370 | if (read_cache() < 0) |
| 1371 | die("unable to read index file"); |
| 1372 | } |
| 1373 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1374 | if ((check || apply) && check_patch_list(list) < 0) |
| 1375 | exit(1); |
| 1376 | |
Linus Torvalds | 5aa7d94 | 2005-06-05 14:05:43 -0700 | [diff] [blame] | 1377 | if (apply) |
| 1378 | write_out_results(list); |
| 1379 | |
| 1380 | if (write_index) { |
| 1381 | if (write_cache(newfd, active_cache, active_nr) || |
| 1382 | commit_index_file(&cache_file)) |
| 1383 | die("Unable to write new cachefile"); |
| 1384 | } |
| 1385 | |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1386 | if (show_files) |
| 1387 | show_file_list(list); |
| 1388 | |
| 1389 | if (diffstat) |
| 1390 | stat_patch_list(list); |
Linus Torvalds | 19c58fb | 2005-05-26 10:23:51 -0700 | [diff] [blame] | 1391 | |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 1392 | if (summary) |
| 1393 | summary_patch_list(list); |
| 1394 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1395 | free(buffer); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | int main(int argc, char **argv) |
| 1400 | { |
| 1401 | int i; |
Linus Torvalds | 4dfdbe1 | 2005-05-23 16:42:21 -0700 | [diff] [blame] | 1402 | int read_stdin = 1; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1403 | |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1404 | for (i = 1; i < argc; i++) { |
| 1405 | const char *arg = argv[i]; |
| 1406 | int fd; |
| 1407 | |
| 1408 | if (!strcmp(arg, "-")) { |
| 1409 | apply_patch(0); |
Linus Torvalds | 4dfdbe1 | 2005-05-23 16:42:21 -0700 | [diff] [blame] | 1410 | read_stdin = 0; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1411 | continue; |
| 1412 | } |
| 1413 | if (!strcmp(arg, "--no-merge")) { |
| 1414 | merge_patch = 0; |
| 1415 | continue; |
| 1416 | } |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 1417 | if (!strcmp(arg, "--stat")) { |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1418 | apply = 0; |
Linus Torvalds | fab2c25 | 2005-05-26 12:25:52 -0700 | [diff] [blame] | 1419 | diffstat = 1; |
| 1420 | continue; |
| 1421 | } |
Junio C Hamano | 96c912a | 2005-06-22 02:29:46 -0700 | [diff] [blame] | 1422 | if (!strcmp(arg, "--summary")) { |
| 1423 | apply = 0; |
| 1424 | summary = 1; |
| 1425 | continue; |
| 1426 | } |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1427 | if (!strcmp(arg, "--check")) { |
| 1428 | apply = 0; |
| 1429 | check = 1; |
| 1430 | continue; |
| 1431 | } |
Linus Torvalds | 3cca928 | 2005-06-05 11:03:13 -0700 | [diff] [blame] | 1432 | if (!strcmp(arg, "--index")) { |
| 1433 | check_index = 1; |
| 1434 | continue; |
| 1435 | } |
Linus Torvalds | aefa4a5 | 2005-06-23 09:00:01 -0700 | [diff] [blame] | 1436 | if (!strcmp(arg, "--apply")) { |
| 1437 | apply = 1; |
| 1438 | continue; |
| 1439 | } |
Linus Torvalds | a577284 | 2005-05-26 15:10:02 -0700 | [diff] [blame] | 1440 | if (!strcmp(arg, "--show-files")) { |
| 1441 | show_files = 1; |
| 1442 | continue; |
| 1443 | } |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1444 | fd = open(arg, O_RDONLY); |
| 1445 | if (fd < 0) |
| 1446 | usage(apply_usage); |
Linus Torvalds | 4dfdbe1 | 2005-05-23 16:42:21 -0700 | [diff] [blame] | 1447 | read_stdin = 0; |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1448 | apply_patch(fd); |
| 1449 | close(fd); |
| 1450 | } |
Linus Torvalds | 4dfdbe1 | 2005-05-23 16:42:21 -0700 | [diff] [blame] | 1451 | if (read_stdin) |
| 1452 | apply_patch(0); |
Linus Torvalds | c1bb935 | 2005-05-23 10:52:17 -0700 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |