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
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
27 static int merge_patch
= 1;
28 static int check_index
= 0;
29 static int diffstat
= 0;
32 static int show_files
= 0;
33 static const char apply_usage
[] = "git-apply [--stat] [--check] [--show-files] <patch>";
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
40 static int max_change
, max_len
;
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".
47 static int linenr
= 1;
50 unsigned long oldpos
, oldlines
;
51 unsigned long newpos
, newlines
;
54 struct fragment
*next
;
58 char *new_name
, *old_name
, *def_name
;
59 unsigned int old_mode
, new_mode
;
60 int is_rename
, is_copy
, is_new
, is_delete
;
61 int lines_added
, lines_deleted
;
62 struct fragment
*fragments
;
64 unsigned long resultsize
;
68 #define CHUNKSIZE (8192)
71 static void *read_patch_file(int fd
, unsigned long *sizep
)
73 unsigned long size
= 0, alloc
= CHUNKSIZE
;
74 void *buffer
= xmalloc(alloc
);
77 int nr
= alloc
- size
;
80 buffer
= xrealloc(buffer
, alloc
);
83 nr
= read(fd
, buffer
+ size
, nr
);
89 die("git-apply: read returned %s", strerror(errno
));
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.
100 if (alloc
< size
+ SLOP
)
101 buffer
= xrealloc(buffer
, size
+ SLOP
);
102 memset(buffer
+ size
, 0, SLOP
);
106 static unsigned long linelen(const char *buffer
, unsigned long size
)
108 unsigned long len
= 0;
111 if (*buffer
++ == '\n')
117 static int is_dev_null(const char *str
)
119 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
125 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
127 if (c
== ' ' && !(terminate
& TERM_SPACE
))
129 if (c
== '\t' && !(terminate
& TERM_TAB
))
135 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
138 const char *start
= line
;
147 if (name_terminate(start
, line
-start
, c
, terminate
))
151 if (c
== '/' && !--p_value
)
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"
167 int deflen
= strlen(def
);
168 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
172 name
= xmalloc(len
+ 1);
173 memcpy(name
, start
, len
);
180 * Get the name etc info from the --/+++ lines of a traditional patch header
182 * NOTE! This hardcodes "-p1" behaviour in filename detection.
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.
188 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
193 first
+= 4; // skip "--- "
194 second
+= 4; // skip "+++ "
195 if (is_dev_null(first
)) {
197 patch
->is_delete
= 0;
198 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
199 patch
->new_name
= name
;
200 } else if (is_dev_null(second
)) {
202 patch
->is_delete
= 1;
203 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
204 patch
->old_name
= name
;
206 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
207 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
208 patch
->old_name
= patch
->new_name
= name
;
211 die("unable to find filename in patch at line %d", linenr
);
214 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
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.
224 * As a result, gitdiff_{old|new}name() will check
225 * their names against any previous information, just
228 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
233 if (!orig_name
&& !isnull
)
234 return find_name(line
, NULL
, 1, 0);
242 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
255 if (memcmp(line
, name
, len
) || line
[len
] != '\n')
259 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
263 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
265 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
269 static int gitdiff_newname(const char *line
, struct patch
*patch
)
271 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
275 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
277 patch
->old_mode
= strtoul(line
, NULL
, 8);
281 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
283 patch
->new_mode
= strtoul(line
, NULL
, 8);
287 static int gitdiff_delete(const char *line
, struct patch
*patch
)
289 patch
->is_delete
= 1;
290 patch
->old_name
= patch
->def_name
;
291 return gitdiff_oldmode(line
, patch
);
294 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
297 patch
->new_name
= patch
->def_name
;
298 return gitdiff_newmode(line
, patch
);
301 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
304 patch
->old_name
= find_name(line
, NULL
, 0, 0);
308 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
311 patch
->new_name
= find_name(line
, NULL
, 0, 0);
315 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
317 patch
->is_rename
= 1;
318 patch
->old_name
= find_name(line
, NULL
, 0, 0);
322 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
324 patch
->is_rename
= 1;
325 patch
->new_name
= find_name(line
, NULL
, 0, 0);
329 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
334 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
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.
343 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
348 static char *git_header_name(char *line
)
366 * We don't accept absolute paths (/dev/null) as possibly valid
372 * Accept a name only if it shows up twice, exactly the same
375 for (len
= 0 ; ; len
++) {
392 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
393 char *ret
= xmalloc(len
+ 1);
394 memcpy(ret
, name
, len
);
403 /* Verify that we recognize the lines following a git header */
404 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
406 unsigned long offset
;
408 /* A git diff has explicit new/delete information, so we don't guess */
410 patch
->is_delete
= 0;
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.
418 patch
->def_name
= git_header_name(line
+ strlen("diff --git "));
423 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
424 static const struct opentry
{
426 int (*fn
)(const char *, struct patch
*);
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
},
440 { "dissimilarity index ", gitdiff_dissimilarity
},
441 { "", gitdiff_unrecognized
},
445 len
= linelen(line
, size
);
446 if (!len
|| line
[len
-1] != '\n')
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
))
453 if (p
->fn(line
+ oplen
, patch
) < 0)
462 static int parse_num(const char *line
, unsigned long *p
)
468 *p
= strtoul(line
, &ptr
, 10);
472 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
473 unsigned long *p1
, unsigned long *p2
)
477 if (offset
< 0 || offset
>= len
)
482 digits
= parse_num(line
, p1
);
492 digits
= parse_num(line
+1, p2
);
504 if (memcmp(line
, expect
, ex
))
511 * Parse a unified diff fragment header of the
512 * form "@@ -a,b +c,d @@"
514 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
518 if (!len
|| line
[len
-1] != '\n')
521 /* Figure out the number of lines in a fragment */
522 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
523 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
528 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
530 unsigned long offset
, len
;
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
;
536 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
537 unsigned long nextlen
;
539 len
= linelen(line
, size
);
543 /* Testing this early allows us to take a few shortcuts.. */
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.
552 if (!memcmp("@@ -", line
, 4)) {
553 struct fragment dummy
;
554 if (parse_fragment_header(line
, len
, &dummy
) < 0)
556 error("patch fragment without header at line %d: %.*s", linenr
, len
-1, line
);
563 * Git patch? It might not have a real patch, just a rename
564 * or mode change, so we handle that specially
566 if (!memcmp("diff --git ", line
, 11)) {
567 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
570 if (!patch
->old_name
&& !patch
->new_name
)
571 die("git diff header lacks filename information");
572 *hdrsize
= git_hdr_len
;
576 /** --- followed by +++ ? */
577 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
581 * We only accept unified patches, so we want it to
582 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
585 nextlen
= linelen(line
+ len
, size
- len
);
586 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
589 /* Ok, we'll consider it a patch */
590 parse_traditional_patch(line
, line
+len
, patch
);
591 *hdrsize
= len
+ nextlen
;
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..
605 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
608 int len
= linelen(line
, size
), offset
;
609 unsigned long pos
[4], oldlines
, newlines
;
611 offset
= parse_fragment_header(line
, len
, fragment
);
614 oldlines
= fragment
->oldlines
;
615 newlines
= fragment
->newlines
;
617 if (patch
->is_new
< 0 && (pos
[0] || oldlines
))
619 if (patch
->is_delete
< 0 && (pos
[1] || newlines
))
620 patch
->is_delete
= 0;
622 /* Parse the thing.. */
627 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
628 if (!oldlines
&& !newlines
)
630 len
= linelen(line
, size
);
631 if (!len
|| line
[len
-1] != '\n')
648 /* We allow "\ No newline at end of file" */
650 if (len
< 12 || memcmp(line
, "\\ No newline", 12))
655 patch
->lines_added
+= added
;
656 patch
->lines_deleted
+= deleted
;
660 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
662 unsigned long offset
= 0;
663 struct fragment
**fragp
= &patch
->fragments
;
665 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
666 struct fragment
*fragment
;
669 fragment
= xmalloc(sizeof(*fragment
));
670 memset(fragment
, 0, sizeof(*fragment
));
671 len
= parse_fragment(line
, size
, patch
, fragment
);
673 die("corrupt patch at line %d", linenr
);
675 fragment
->patch
= line
;
676 fragment
->size
= len
;
679 fragp
= &fragment
->next
;
688 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
690 int hdrsize
, patchsize
;
691 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
696 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
698 return offset
+ hdrsize
+ patchsize
;
701 const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
702 const char minuses
[]= "----------------------------------------------------------------------";
704 static void show_stats(struct patch
*patch
)
706 char *name
= patch
->old_name
;
707 int len
, max
, add
, del
, total
;
710 name
= patch
->new_name
;
713 * "scale" the filename
724 * scale the add/delete
730 add
= patch
->lines_added
;
731 del
= patch
->lines_deleted
;
734 total
= (total
* max
+ max_change
/ 2) / max_change
;
735 add
= (add
* max
+ max_change
/ 2) / max_change
;
737 printf(" %-*s |%5d %.*s%.*s\n",
738 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
739 add
, pluses
, del
, minuses
);
742 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
747 switch (st
->st_mode
& S_IFMT
) {
749 return readlink(path
, buf
, size
);
751 fd
= open(path
, O_RDONLY
);
753 return error("unable to open %s", path
);
756 int ret
= read(fd
, buf
+ got
, size
- got
);
774 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
)
777 unsigned long start
, backwards
, forwards
;
784 unsigned long offset
= 0;
786 while (offset
+ fragsize
<= size
) {
787 if (buf
[offset
++] == '\n') {
795 /* Exact line number? */
796 if (!memcmp(buf
+ start
, fragment
, fragsize
))
800 * There's probably some smart way to do this, but I'll leave
801 * that to the smart and beautiful people. I'm simple and stupid.
812 if (forwards
+ fragsize
> size
)
818 } while (backwards
&& buf
[backwards
-1] != '\n');
821 while (forwards
+ fragsize
<= size
) {
822 if (buf
[forwards
++] == '\n')
828 if (try + fragsize
> size
)
830 if (memcmp(buf
+ try, fragment
, fragsize
))
835 fprintf(stderr
, "Fragment applied at offset %d\n", n
);
840 * We should start searching forward and backward.
851 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
853 char *buf
= desc
->buffer
;
854 const char *patch
= frag
->patch
;
855 int offset
, size
= frag
->size
;
856 char *old
= xmalloc(size
);
857 char *new = xmalloc(size
);
858 int oldsize
= 0, newsize
= 0;
861 int len
= linelen(patch
, size
);
868 * "plen" is how much of the line we should use for
869 * the actual patch data. Normally we just remove the
870 * first character on the line, but if the line is
871 * followed by "\ No newline", then we also remove the
872 * last one (which is the newline, of course).
875 if (len
> size
&& patch
[len
] == '\\')
880 memcpy(old
+ oldsize
, patch
+ 1, plen
);
884 /* Fall-through for ' ' */
886 memcpy(new + newsize
, patch
+ 1, plen
);
890 /* Ignore it, we already handled it */
899 offset
= find_offset(buf
, desc
->size
, old
, oldsize
, frag
->newpos
);
901 int diff
= newsize
- oldsize
;
902 unsigned long size
= desc
->size
+ diff
;
903 unsigned long alloc
= desc
->alloc
;
908 buf
= xrealloc(buf
, alloc
);
912 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
913 memcpy(buf
+ offset
, new, newsize
);
922 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
924 struct fragment
*frag
= patch
->fragments
;
927 if (apply_one_fragment(desc
, frag
) < 0)
928 return error("patch failed: %s:%d", patch
->old_name
, frag
->oldpos
);
933 static int apply_data(struct patch
*patch
, struct stat
*st
)
936 unsigned long size
, alloc
;
937 struct buffer_desc desc
;
939 if (!patch
->old_name
|| !patch
->fragments
)
943 buf
= xmalloc(alloc
);
944 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
945 return error("read of %s failed", patch
->old_name
);
950 if (apply_fragments(&desc
, patch
) < 0)
952 patch
->result
= desc
.buffer
;
953 patch
->resultsize
= desc
.size
;
957 static int check_patch(struct patch
*patch
)
960 const char *old_name
= patch
->old_name
;
961 const char *new_name
= patch
->new_name
;
966 if (lstat(old_name
, &st
) < 0)
967 return error("%s: %s\n", strerror(errno
));
969 int pos
= cache_name_pos(old_name
, strlen(old_name
));
971 return error("%s: does not exist in index", old_name
);
972 changed
= ce_match_stat(active_cache
[pos
], &st
);
974 return error("%s: does not match index", old_name
);
976 if (patch
->is_new
< 0)
978 if (!patch
->old_mode
)
979 patch
->old_mode
= st
.st_mode
;
980 if ((st
.st_mode
^ patch
->old_mode
) & S_IFMT
)
981 return error("%s: wrong type", old_name
);
982 if (st
.st_mode
!= patch
->old_mode
)
983 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
984 old_name
, st
.st_mode
, patch
->old_mode
);
987 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
988 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
989 return error("%s: already exists in index", new_name
);
990 if (!lstat(new_name
, &st
))
991 return error("%s: already exists in working directory", new_name
);
993 return error("%s: %s", new_name
, strerror(errno
));
996 if (new_name
&& old_name
) {
997 int same
= !strcmp(old_name
, new_name
);
998 if (!patch
->new_mode
)
999 patch
->new_mode
= patch
->old_mode
;
1000 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1001 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1002 patch
->new_mode
, new_name
, patch
->old_mode
,
1003 same
? "" : " of ", same
? "" : old_name
);
1006 if (apply_data(patch
, &st
) < 0)
1007 return error("%s: patch does not apply", old_name
);
1011 static int check_patch_list(struct patch
*patch
)
1015 for (;patch
; patch
= patch
->next
)
1016 error
|= check_patch(patch
);
1020 static void show_file(int c
, unsigned int mode
, const char *name
)
1022 printf("%c %o %s\n", c
, mode
, name
);
1025 static void show_file_list(struct patch
*patch
)
1027 for (;patch
; patch
= patch
->next
) {
1028 if (patch
->is_rename
) {
1029 show_file('-', patch
->old_mode
, patch
->old_name
);
1030 show_file('+', patch
->new_mode
, patch
->new_name
);
1033 if (patch
->is_copy
|| patch
->is_new
) {
1034 show_file('+', patch
->new_mode
, patch
->new_name
);
1037 if (patch
->is_delete
) {
1038 show_file('-', patch
->old_mode
, patch
->old_name
);
1041 if (patch
->old_mode
&& patch
->new_mode
&& patch
->old_mode
!= patch
->new_mode
) {
1042 printf("M %o:%o %s\n", patch
->old_mode
, patch
->new_mode
, patch
->old_name
);
1045 printf("M %o %s\n", patch
->old_mode
, patch
->old_name
);
1049 static void stat_patch_list(struct patch
*patch
)
1051 int files
, adds
, dels
;
1053 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1055 adds
+= patch
->lines_added
;
1056 dels
+= patch
->lines_deleted
;
1060 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1063 static void patch_stats(struct patch
*patch
)
1065 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1067 if (lines
> max_change
)
1069 if (patch
->old_name
) {
1070 int len
= strlen(patch
->old_name
);
1074 if (patch
->new_name
) {
1075 int len
= strlen(patch
->new_name
);
1081 static int apply_patch(int fd
)
1083 unsigned long offset
, size
;
1084 char *buffer
= read_patch_file(fd
, &size
);
1085 struct patch
*list
= NULL
, **listp
= &list
;
1091 struct patch
*patch
;
1094 patch
= xmalloc(sizeof(*patch
));
1095 memset(patch
, 0, sizeof(*patch
));
1096 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
1101 listp
= &patch
->next
;
1106 if ((check
|| apply
) && check_patch_list(list
) < 0)
1110 show_file_list(list
);
1113 stat_patch_list(list
);
1119 int main(int argc
, char **argv
)
1124 if (read_cache() < 0)
1125 die("unable to read index file");
1127 for (i
= 1; i
< argc
; i
++) {
1128 const char *arg
= argv
[i
];
1131 if (!strcmp(arg
, "-")) {
1136 if (!strcmp(arg
, "--no-merge")) {
1140 if (!strcmp(arg
, "--stat")) {
1145 if (!strcmp(arg
, "--check")) {
1150 if (!strcmp(arg
, "--index")) {
1154 if (!strcmp(arg
, "--show-files")) {
1158 fd
= open(arg
, O_RDONLY
);