4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
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
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.
19 // We default to the merge behaviour, since that's what most people would
21 static int merge_patch = 1;
22 static const char apply_usage[] = "git-apply <patch>";
25 * Various "current state", notably line numbers and what
26 * file (and how) we're patching right now.. The "is_xxxx"
27 * things are flags, where -1 means "don't know yet".
29 static int linenr = 1;
30 static int old_mode, new_mode;
31 static char *old_name, *new_name, *def_name;
32 static int is_rename, is_copy, is_new, is_delete;
34 #define CHUNKSIZE (8192)
37 static void *read_patch_file(int fd, unsigned long *sizep)
39 unsigned long size = 0, alloc = CHUNKSIZE;
40 void *buffer = xmalloc(alloc);
43 int nr = alloc - size;
46 buffer = xrealloc(buffer, alloc);
49 nr = read(fd, buffer + size, nr);
55 die("git-apply: read returned %s", strerror(errno));
62 * Make sure that we have some slop in the buffer
63 * so that we can do speculative "memcmp" etc, and
64 * see to it that it is NUL-filled.
66 if (alloc < size + SLOP)
67 buffer = xrealloc(buffer, size + SLOP);
68 memset(buffer + size, 0, SLOP);
72 static unsigned long linelen(char *buffer, unsigned long size)
74 unsigned long len = 0;
77 if (*buffer++ == '\n')
83 static int is_dev_null(const char *str)
85 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
88 static char * find_name(const char *line, char *def, int p_value)
91 const char *start = line;
99 if (c == '/' && !--p_value)
109 * Generally we prefer the shorter name, especially
110 * if the other one is just a variation of that with
111 * something else tacked on to the end (ie "file.orig"
115 int deflen = strlen(def);
116 if (deflen < len && !strncmp(start, def, deflen))
120 name = xmalloc(len + 1);
121 memcpy(name, start, len);
128 * Get the name etc info from the --/+++ lines of a traditional patch header
130 * NOTE! This hardcodes "-p1" behaviour in filename detection.
132 static int parse_traditional_patch(const char *first, const char *second)
137 first += 4; // skip "--- "
138 second += 4; // skip "+++ "
139 if (is_dev_null(first)) {
141 name = find_name(second, def_name, p_value);
142 } else if (is_dev_null(second)) {
144 name = find_name(first, def_name, p_value);
146 name = find_name(first, def_name, p_value);
147 name = find_name(second, name, p_value);
150 die("unable to find filename in patch at line %d", linenr);
155 static int gitdiff_hdrend(const char *line)
160 static int gitdiff_oldname(const char *line)
163 old_name = find_name(line, NULL, 1);
167 static int gitdiff_newname(const char *line)
170 new_name = find_name(line, NULL, 1);
174 static int gitdiff_oldmode(const char *line)
176 old_mode = strtoul(line, NULL, 8);
180 static int gitdiff_newmode(const char *line)
182 new_mode = strtoul(line, NULL, 8);
186 static int gitdiff_delete(const char *line)
189 return gitdiff_oldmode(line);
192 static int gitdiff_newfile(const char *line)
195 return gitdiff_newmode(line);
198 static int gitdiff_copysrc(const char *line)
201 old_name = find_name(line, NULL, 0);
205 static int gitdiff_copydst(const char *line)
208 new_name = find_name(line, NULL, 0);
212 static int gitdiff_renamesrc(const char *line)
215 old_name = find_name(line, NULL, 0);
219 static int gitdiff_renamedst(const char *line)
222 new_name = find_name(line, NULL, 0);
226 static int gitdiff_similarity(const char *line)
231 /* Verify that we recognize the lines following a git header */
232 static int parse_git_header(char *line, int len, unsigned int size)
234 unsigned long offset;
236 /* A git diff has explicit new/delete information, so we don't guess */
243 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
244 static const struct opentry {
246 int (*fn)(const char *);
248 { "@@ -", gitdiff_hdrend },
249 { "--- ", gitdiff_oldname },
250 { "+++ ", gitdiff_newname },
251 { "old mode ", gitdiff_oldmode },
252 { "new mode ", gitdiff_newmode },
253 { "deleted file mode ", gitdiff_delete },
254 { "new file mode ", gitdiff_newfile },
255 { "copy from ", gitdiff_copysrc },
256 { "copy to ", gitdiff_copydst },
257 { "rename from ", gitdiff_renamesrc },
258 { "rename to ", gitdiff_renamedst },
259 { "similarity index ", gitdiff_similarity },
263 len = linelen(line, size);
264 if (!len || line[len-1] != '\n')
266 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
267 const struct opentry *p = optable + i;
268 int oplen = strlen(p->str);
269 if (len < oplen || memcmp(p->str, line, oplen))
271 if (p->fn(line + oplen) < 0)
279 static int parse_num(const char *line, int len, int offset, const char *expect, unsigned long *p)
284 if (offset < 0 || offset >= len)
291 *p = strtoul(line, &ptr, 10);
302 if (memcmp(line, expect, ex))
309 * Parse a unified diff fragment header of the
310 * form "@@ -a,b +c,d @@"
312 static int parse_fragment_header(char *line, int len, unsigned long *pos)
316 if (!len || line[len-1] != '\n')
319 /* Figure out the number of lines in a fragment */
320 offset = parse_num(line, len, 4, ",", pos);
321 offset = parse_num(line, len, offset, " +", pos+1);
322 offset = parse_num(line, len, offset, ",", pos+2);
323 offset = parse_num(line, len, offset, " @@", pos+3);
328 static int find_header(char *line, unsigned long size, int *hdrsize)
330 unsigned long offset, len;
332 is_rename = is_copy = 0;
333 is_new = is_delete = -1;
334 old_mode = new_mode = 0;
335 def_name = old_name = new_name = NULL;
336 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
337 unsigned long nextlen;
339 len = linelen(line, size);
343 /* Testing this early allows us to take a few shortcuts.. */
348 * Make sure we don't find any unconnected patch fragmants.
349 * That's a sign that we didn't find a header, and that a
350 * patch has become corrupted/broken up.
352 if (!memcmp("@@ -", line, 4)) {
353 unsigned long pos[4];
354 if (parse_fragment_header(line, len, pos) < 0)
356 error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
363 * Git patch? It might not have a real patch, just a rename
364 * or mode change, so we handle that specially
366 if (!memcmp("diff --git ", line, 11)) {
367 int git_hdr_len = parse_git_header(line, len, size);
371 *hdrsize = git_hdr_len;
375 /** --- followed by +++ ? */
376 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
380 * We only accept unified patches, so we want it to
381 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
384 nextlen = linelen(line + len, size - len);
385 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
388 /* Ok, we'll consider it a patch */
389 parse_traditional_patch(line, line+len);
390 *hdrsize = len + nextlen;
398 * Parse a unified diff. Note that this really needs
399 * to parse each fragment separately, since the only
400 * way to know the difference between a "---" that is
401 * part of a patch, and a "---" that starts the next
402 * patch is to look at the line counts..
404 static int apply_fragment(char *line, unsigned long size)
406 int len = linelen(line, size), offset;
407 unsigned long pos[4], oldlines, newlines;
409 offset = parse_fragment_header(line, len, pos);
415 if (is_new < 0 && (pos[0] || oldlines))
417 if (is_delete < 0 && (pos[1] || newlines))
420 /* Parse the thing.. */
424 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
425 if (!oldlines && !newlines)
427 len = linelen(line, size);
428 if (!len || line[len-1] != '\n')
448 static int apply_single_patch(char *line, unsigned long size)
450 unsigned long offset = 0;
452 while (size > 4 && !memcmp(line, "@@ -", 4)) {
453 int len = apply_fragment(line, size);
455 die("corrupt patch at line %d", linenr);
457 printf("applying fragment:\n%.*s\n\n", len, line);
466 static int apply_chunk(char *buffer, unsigned long size)
468 int hdrsize, patchsize;
469 int offset = find_header(buffer, size, &hdrsize);
470 char *header, *patch;
474 header = buffer + offset;
476 printf("Found header:\n%.*s\n\n", hdrsize, header);
477 printf("Rename: %d\n", is_rename);
478 printf("Copy: %d\n", is_copy);
479 printf("New: %d\n", is_new);
480 printf("Delete: %d\n", is_delete);
481 printf("Mode: %o->%o\n", old_mode, new_mode);
482 printf("Name: '%s'->'%s'\n", old_name, new_name);
484 patch = header + hdrsize;
485 patchsize = apply_single_patch(patch, size - offset - hdrsize);
487 return offset + hdrsize + patchsize;
490 static int apply_patch(int fd)
492 unsigned long offset, size;
493 char *buffer = read_patch_file(fd, &size);
499 int nr = apply_chunk(buffer + offset, size);
509 int main(int argc, char **argv)
514 if (read_cache() < 0)
515 die("unable to read index file");
517 for (i = 1; i < argc; i++) {
518 const char *arg = argv[i];
521 if (!strcmp(arg, "-")) {
526 if (!strcmp(arg, "--no-merge")) {
530 fd = open(arg, O_RDONLY);