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]);
92 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
94 if (c
== ' ' && !(terminate
& TERM_SPACE
))
96 if (c
== '\t' && !(terminate
& TERM_TAB
))
100 * Do we want an existing name? Return false and
101 * continue if it's not there.
103 if (terminate
& TERM_EXIST
)
104 return cache_name_pos(name
, namelen
) >= 0;
109 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
112 const char *start
= line
;
121 if (name_terminate(start
, line
-start
, c
, terminate
))
125 if (c
== '/' && !--p_value
)
135 * Generally we prefer the shorter name, especially
136 * if the other one is just a variation of that with
137 * something else tacked on to the end (ie "file.orig"
141 int deflen
= strlen(def
);
142 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
146 name
= xmalloc(len
+ 1);
147 memcpy(name
, start
, len
);
154 * Get the name etc info from the --/+++ lines of a traditional patch header
156 * NOTE! This hardcodes "-p1" behaviour in filename detection.
158 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
159 * files, we can happily check the index for a match, but for creating a
160 * new file we should try to match whatever "patch" does. I have no idea.
162 static void parse_traditional_patch(const char *first
, const char *second
)
167 first
+= 4; // skip "--- "
168 second
+= 4; // skip "+++ "
169 if (is_dev_null(first
)) {
171 name
= find_name(second
, def_name
, p_value
, TERM_SPACE
| TERM_TAB
);
173 } else if (is_dev_null(second
)) {
175 name
= find_name(first
, def_name
, p_value
, TERM_EXIST
| TERM_SPACE
| TERM_TAB
);
178 name
= find_name(first
, def_name
, p_value
, TERM_EXIST
| TERM_SPACE
| TERM_TAB
);
179 name
= find_name(second
, name
, p_value
, TERM_EXIST
| TERM_SPACE
| TERM_TAB
);
180 old_name
= new_name
= name
;
183 die("unable to find filename in patch at line %d", linenr
);
186 static int gitdiff_hdrend(const char *line
)
192 * We're anal about diff header consistency, to make
193 * sure that we don't end up having strange ambiguous
194 * patches floating around.
196 * As a result, gitdiff_{old|new}name() will check
197 * their names against any previous information, just
200 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
205 if (!orig_name
&& !isnull
)
206 return find_name(line
, NULL
, 1, 0);
214 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
227 if (memcmp(line
, name
, len
) || line
[len
] != '\n')
231 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
235 static int gitdiff_oldname(const char *line
)
237 old_name
= gitdiff_verify_name(line
, is_new
, old_name
, "old");
241 static int gitdiff_newname(const char *line
)
243 new_name
= gitdiff_verify_name(line
, is_delete
, new_name
, "new");
247 static int gitdiff_oldmode(const char *line
)
249 old_mode
= strtoul(line
, NULL
, 8);
253 static int gitdiff_newmode(const char *line
)
255 new_mode
= strtoul(line
, NULL
, 8);
259 static int gitdiff_delete(const char *line
)
262 return gitdiff_oldmode(line
);
265 static int gitdiff_newfile(const char *line
)
268 return gitdiff_newmode(line
);
271 static int gitdiff_copysrc(const char *line
)
274 old_name
= find_name(line
, NULL
, 0, 0);
278 static int gitdiff_copydst(const char *line
)
281 new_name
= find_name(line
, NULL
, 0, 0);
285 static int gitdiff_renamesrc(const char *line
)
288 old_name
= find_name(line
, NULL
, 0, 0);
292 static int gitdiff_renamedst(const char *line
)
295 new_name
= find_name(line
, NULL
, 0, 0);
299 static int gitdiff_similarity(const char *line
)
305 * This is normal for a diff that doesn't change anything: we'll fall through
306 * into the next diff. Tell the parser to break out.
308 static int gitdiff_unrecognized(const char *line
)
313 /* Verify that we recognize the lines following a git header */
314 static int parse_git_header(char *line
, int len
, unsigned int size
)
316 unsigned long offset
;
318 /* A git diff has explicit new/delete information, so we don't guess */
325 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
326 static const struct opentry
{
328 int (*fn
)(const char *);
330 { "@@ -", gitdiff_hdrend
},
331 { "--- ", gitdiff_oldname
},
332 { "+++ ", gitdiff_newname
},
333 { "old mode ", gitdiff_oldmode
},
334 { "new mode ", gitdiff_newmode
},
335 { "deleted file mode ", gitdiff_delete
},
336 { "new file mode ", gitdiff_newfile
},
337 { "copy from ", gitdiff_copysrc
},
338 { "copy to ", gitdiff_copydst
},
339 { "rename from ", gitdiff_renamesrc
},
340 { "rename to ", gitdiff_renamedst
},
341 { "similarity index ", gitdiff_similarity
},
342 { "", gitdiff_unrecognized
},
346 len
= linelen(line
, size
);
347 if (!len
|| line
[len
-1] != '\n')
349 for (i
= 0; i
< sizeof(optable
) / sizeof(optable
[0]); i
++) {
350 const struct opentry
*p
= optable
+ i
;
351 int oplen
= strlen(p
->str
);
352 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
354 if (p
->fn(line
+ oplen
) < 0)
363 static int parse_num(const char *line
, int len
, int offset
, const char *expect
, unsigned long *p
)
368 if (offset
< 0 || offset
>= len
)
375 *p
= strtoul(line
, &ptr
, 10);
386 if (memcmp(line
, expect
, ex
))
393 * Parse a unified diff fragment header of the
394 * form "@@ -a,b +c,d @@"
396 static int parse_fragment_header(char *line
, int len
, unsigned long *pos
)
400 if (!len
|| line
[len
-1] != '\n')
403 /* Figure out the number of lines in a fragment */
404 offset
= parse_num(line
, len
, 4, ",", pos
);
405 offset
= parse_num(line
, len
, offset
, " +", pos
+1);
406 offset
= parse_num(line
, len
, offset
, ",", pos
+2);
407 offset
= parse_num(line
, len
, offset
, " @@", pos
+3);
412 static int find_header(char *line
, unsigned long size
, int *hdrsize
)
414 unsigned long offset
, len
;
416 is_rename
= is_copy
= 0;
417 is_new
= is_delete
= -1;
418 old_mode
= new_mode
= 0;
419 def_name
= old_name
= new_name
= NULL
;
420 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
421 unsigned long nextlen
;
423 len
= linelen(line
, size
);
427 /* Testing this early allows us to take a few shortcuts.. */
432 * Make sure we don't find any unconnected patch fragmants.
433 * That's a sign that we didn't find a header, and that a
434 * patch has become corrupted/broken up.
436 if (!memcmp("@@ -", line
, 4)) {
437 unsigned long pos
[4];
438 if (parse_fragment_header(line
, len
, pos
) < 0)
440 error("patch fragment without header at line %d: %.*s", linenr
, len
-1, line
);
447 * Git patch? It might not have a real patch, just a rename
448 * or mode change, so we handle that specially
450 if (!memcmp("diff --git ", line
, 11)) {
451 int git_hdr_len
= parse_git_header(line
, len
, size
);
455 *hdrsize
= git_hdr_len
;
459 /** --- followed by +++ ? */
460 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
464 * We only accept unified patches, so we want it to
465 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
468 nextlen
= linelen(line
+ len
, size
- len
);
469 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
472 /* Ok, we'll consider it a patch */
473 parse_traditional_patch(line
, line
+len
);
474 *hdrsize
= len
+ nextlen
;
482 * Parse a unified diff. Note that this really needs
483 * to parse each fragment separately, since the only
484 * way to know the difference between a "---" that is
485 * part of a patch, and a "---" that starts the next
486 * patch is to look at the line counts..
488 static int apply_fragment(char *line
, unsigned long size
)
490 int len
= linelen(line
, size
), offset
;
491 unsigned long pos
[4], oldlines
, newlines
;
493 offset
= parse_fragment_header(line
, len
, pos
);
499 if (is_new
< 0 && (pos
[0] || oldlines
))
501 if (is_delete
< 0 && (pos
[1] || newlines
))
504 /* Parse the thing.. */
508 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
509 if (!oldlines
&& !newlines
)
511 len
= linelen(line
, size
);
512 if (!len
|| line
[len
-1] != '\n')
532 static int apply_single_patch(char *line
, unsigned long size
)
534 unsigned long offset
= 0;
536 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
537 int len
= apply_fragment(line
, size
);
539 die("corrupt patch at line %d", linenr
);
541 printf("applying fragment:\n%.*s\n\n", len
, line
);
550 static int apply_chunk(char *buffer
, unsigned long size
)
552 int hdrsize
, patchsize
;
553 int offset
= find_header(buffer
, size
, &hdrsize
);
554 char *header
, *patch
;
558 header
= buffer
+ offset
;
560 printf("Found header:\n%.*s\n\n", hdrsize
, header
);
561 printf("Rename: %d\n", is_rename
);
562 printf("Copy: %d\n", is_copy
);
563 printf("New: %d\n", is_new
);
564 printf("Delete: %d\n", is_delete
);
565 printf("Mode: %o:%o\n", old_mode
, new_mode
);
566 printf("Name: '%s':'%s'\n", old_name
, new_name
);
568 if (old_name
&& cache_name_pos(old_name
, strlen(old_name
)) < 0)
569 die("file %s does not exist", old_name
);
570 if (new_name
&& (is_new
| is_rename
| is_copy
)) {
571 if (cache_name_pos(new_name
, strlen(new_name
)) >= 0)
572 die("file %s already exists", new_name
);
575 patch
= header
+ hdrsize
;
576 patchsize
= apply_single_patch(patch
, size
- offset
- hdrsize
);
578 return offset
+ hdrsize
+ patchsize
;
581 static int apply_patch(int fd
)
583 unsigned long offset
, size
;
584 char *buffer
= read_patch_file(fd
, &size
);
590 int nr
= apply_chunk(buffer
+ offset
, size
);
600 int main(int argc
, char **argv
)
605 if (read_cache() < 0)
606 die("unable to read index file");
608 for (i
= 1; i
< argc
; i
++) {
609 const char *arg
= argv
[i
];
612 if (!strcmp(arg
, "-")) {
617 if (!strcmp(arg
, "--no-merge")) {
621 fd
= open(arg
, O_RDONLY
);