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