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 #define CHUNKSIZE (8192)
26 static void *read_patch_file(int fd
, unsigned long *sizep
)
28 unsigned long size
= 0, alloc
= CHUNKSIZE
;
29 void *buffer
= xmalloc(alloc
);
32 int nr
= alloc
- size
;
35 buffer
= xrealloc(buffer
, alloc
);
38 nr
= read(fd
, buffer
+ size
, nr
);
44 die("git-apply: read returned %s", strerror(errno
));
52 static unsigned long linelen(char *buffer
, unsigned long size
)
54 unsigned long len
= 0;
57 if (*buffer
++ == '\n')
63 static int match_word(const char *line
, const char *match
)
75 /* Verify that we recognize the lines following a git header */
76 static int parse_git_header(char *line
, unsigned int size
)
78 unsigned long offset
, len
;
80 for (offset
= 0 ; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
) {
81 len
= linelen(line
, size
);
84 if (line
[len
-1] != '\n')
88 if (!memcmp(line
, "@@ -", 4))
90 if (match_word(line
, "new file mode"))
92 if (match_word(line
, "deleted file mode"))
94 if (match_word(line
, "copy"))
96 if (match_word(line
, "rename"))
98 if (match_word(line
, "similarity index"))
103 /* We want either a patch _or_ something real */
107 static int find_header(char *line
, unsigned long size
, int *hdrsize
)
109 unsigned long offset
, len
;
111 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
) {
112 unsigned long nextlen
;
114 len
= linelen(line
, size
);
118 /* Testing this early allows us to take a few shortcuts.. */
125 * Git patch? It might not have a real patch, just a rename
126 * or mode change, so we handle that specially
128 if (!memcmp("diff --git ", line
, 11)) {
129 int git_hdr_len
= parse_git_header(line
+ len
, size
- len
);
133 *hdrsize
= len
+ git_hdr_len
;
137 /** --- followed by +++ ? */
138 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
142 * We only accept unified patches, so we want it to
143 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
146 nextlen
= linelen(line
+ len
, size
- len
);
147 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
150 /* Ok, we'll consider it a patch */
151 *hdrsize
= len
+ nextlen
;
157 static int parse_num(const char *line
, int len
, int offset
, const char *expect
, unsigned long *p
)
162 if (offset
< 0 || offset
>= len
)
169 *p
= strtoul(line
, &ptr
, 10);
180 if (memcmp(line
, expect
, ex
))
187 * Parse a unified diff. Note that this really needs
188 * to parse each fragment separately, since the only
189 * way to know the difference between a "---" that is
190 * part of a patch, and a "---" that starts the next
191 * patch is to look at the line counts..
193 static int apply_fragment(char *line
, unsigned long size
)
195 int len
= linelen(line
, size
), offset
;
196 unsigned long oldpos
, oldlines
, newpos
, newlines
;
198 if (!len
|| line
[len
-1] != '\n')
201 /* Figure out the number of lines in a fragment */
202 offset
= parse_num(line
, len
, 4, ",", &oldpos
);
203 offset
= parse_num(line
, len
, offset
, " +", &oldlines
);
204 offset
= parse_num(line
, len
, offset
, ",", &newpos
);
205 offset
= parse_num(line
, len
, offset
, " @@", &newlines
);
209 /* Parse the thing.. */
212 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
) {
213 if (!oldlines
&& !newlines
)
215 len
= linelen(line
, size
);
216 if (!len
|| line
[len
-1] != '\n')
236 static int apply_single_patch(char *line
, unsigned long size
)
238 unsigned long offset
= 0;
240 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
241 int len
= apply_fragment(line
, size
);
245 printf("applying fragment:\n%.*s\n\n", len
, line
);
254 static int apply_chunk(char *buffer
, unsigned long size
)
256 int hdrsize
, patchsize
;
257 int offset
= find_header(buffer
, size
, &hdrsize
);
258 char *header
, *patch
;
262 header
= buffer
+ offset
;
264 printf("Found header:\n%.*s\n\n", hdrsize
, header
);
266 patch
= header
+ hdrsize
;
267 patchsize
= apply_single_patch(patch
, size
- offset
- hdrsize
);
269 return offset
+ hdrsize
+ patchsize
;
272 static int apply_patch(int fd
)
274 unsigned long offset
, size
;
275 char *buffer
= read_patch_file(fd
, &size
);
281 int nr
= apply_chunk(buffer
+ offset
, size
);
291 int main(int argc
, char **argv
)
295 if (read_cache() < 0)
296 die("unable to read index file");
298 for (i
= 1; i
< argc
; i
++) {
299 const char *arg
= argv
[i
];
302 if (!strcmp(arg
, "-")) {
306 if (!strcmp(arg
, "--no-merge")) {
310 fd
= open(arg
, O_RDONLY
);