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>";
24 static int linenr = 1;
26 #define CHUNKSIZE (8192)
28 static void *read_patch_file(int fd, unsigned long *sizep)
30 unsigned long size = 0, alloc = CHUNKSIZE;
31 void *buffer = xmalloc(alloc);
34 int nr = alloc - size;
37 buffer = xrealloc(buffer, alloc);
40 nr = read(fd, buffer + size, nr);
46 die("git-apply: read returned %s", strerror(errno));
54 static unsigned long linelen(char *buffer, unsigned long size)
56 unsigned long len = 0;
59 if (*buffer++ == '\n')
65 static int match_word(const char *line, const char *match)
77 /* Verify that we recognize the lines following a git header */
78 static int parse_git_header(char *line, unsigned int size)
80 unsigned long offset, len;
82 for (offset = 0 ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
83 len = linelen(line, size);
86 if (line[len-1] != '\n')
90 if (!memcmp(line, "@@ -", 4))
92 if (match_word(line, "new file mode"))
94 if (match_word(line, "deleted file mode"))
96 if (match_word(line, "copy"))
98 if (match_word(line, "rename"))
100 if (match_word(line, "similarity index"))
105 /* We want either a patch _or_ something real */
109 static int parse_num(const char *line, int len, int offset, const char *expect, unsigned long *p)
114 if (offset < 0 || offset >= len)
121 *p = strtoul(line, &ptr, 10);
132 if (memcmp(line, expect, ex))
139 * Parse a unified diff fragment header of the
140 * form "@@ -a,b +c,d @@"
142 static int parse_fragment_header(char *line, int len, unsigned long *pos)
146 if (!len || line[len-1] != '\n')
149 /* Figure out the number of lines in a fragment */
150 offset = parse_num(line, len, 4, ",", pos);
151 offset = parse_num(line, len, offset, " +", pos+1);
152 offset = parse_num(line, len, offset, ",", pos+2);
153 offset = parse_num(line, len, offset, " @@", pos+3);
158 static int find_header(char *line, unsigned long size, int *hdrsize)
160 unsigned long offset, len;
162 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
163 unsigned long nextlen;
165 len = linelen(line, size);
169 /* Testing this early allows us to take a few shortcuts.. */
174 * Make sure we don't find any unconnected patch fragmants.
175 * That's a sign that we didn't find a header, and that a
176 * patch has become corrupted/broken up.
178 if (!memcmp("@@ -", line, 4)) {
179 unsigned long pos[4];
180 if (parse_fragment_header(line, len, pos) < 0)
182 error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
189 * Git patch? It might not have a real patch, just a rename
190 * or mode change, so we handle that specially
192 if (!memcmp("diff --git ", line, 11)) {
193 int git_hdr_len = parse_git_header(line + len, size - len);
197 *hdrsize = len + git_hdr_len;
201 /** --- followed by +++ ? */
202 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
206 * We only accept unified patches, so we want it to
207 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
210 nextlen = linelen(line + len, size - len);
211 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
214 /* Ok, we'll consider it a patch */
215 *hdrsize = len + nextlen;
223 * Parse a unified diff. Note that this really needs
224 * to parse each fragment separately, since the only
225 * way to know the difference between a "---" that is
226 * part of a patch, and a "---" that starts the next
227 * patch is to look at the line counts..
229 static int apply_fragment(char *line, unsigned long size)
231 int len = linelen(line, size), offset;
232 unsigned long pos[4], oldlines, newlines;
234 offset = parse_fragment_header(line, len, pos);
240 /* Parse the thing.. */
244 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
245 if (!oldlines && !newlines)
247 len = linelen(line, size);
248 if (!len || line[len-1] != '\n')
268 static int apply_single_patch(char *line, unsigned long size)
270 unsigned long offset = 0;
272 while (size > 4 && !memcmp(line, "@@ -", 4)) {
273 int len = apply_fragment(line, size);
275 die("corrupt patch at line %d", linenr);
277 printf("applying fragment:\n%.*s\n\n", len, line);
286 static int apply_chunk(char *buffer, unsigned long size)
288 int hdrsize, patchsize;
289 int offset = find_header(buffer, size, &hdrsize);
290 char *header, *patch;
294 header = buffer + offset;
296 printf("Found header:\n%.*s\n\n", hdrsize, header);
298 patch = header + hdrsize;
299 patchsize = apply_single_patch(patch, size - offset - hdrsize);
301 return offset + hdrsize + patchsize;
304 static int apply_patch(int fd)
306 unsigned long offset, size;
307 char *buffer = read_patch_file(fd, &size);
313 int nr = apply_chunk(buffer + offset, size);
323 int main(int argc, char **argv)
327 if (read_cache() < 0)
328 die("unable to read index file");
330 for (i = 1; i < argc; i++) {
331 const char *arg = argv[i];
334 if (!strcmp(arg, "-")) {
338 if (!strcmp(arg, "--no-merge")) {
342 fd = open(arg, O_RDONLY);