4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
13 // --check turns on checking that the working tree matches the
14 // files that are being modified, but doesn't apply the patch
15 // --stat does just a diffstat, and doesn't actually apply
16 // --show-files shows the directory changes
17 // --show-index-info shows the old and new index info for paths if available.
19 static int check_index
= 0;
20 static int write_index
= 0;
21 static int diffstat
= 0;
22 static int summary
= 0;
25 static int show_files
= 0;
26 static int show_index_info
= 0;
27 static const char apply_usage
[] =
28 "git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] <patch>...";
31 * For "diff-stat" like behaviour, we keep track of the biggest change
32 * we've seen, and the longest filename. That allows us to do simple
35 static int max_change
, max_len
;
38 * Various "current state", notably line numbers and what
39 * file (and how) we're patching right now.. The "is_xxxx"
40 * things are flags, where -1 means "don't know yet".
42 static int linenr
= 1;
45 unsigned long oldpos
, oldlines
;
46 unsigned long newpos
, newlines
;
49 struct fragment
*next
;
53 char *new_name
, *old_name
, *def_name
;
54 unsigned int old_mode
, new_mode
;
55 int is_rename
, is_copy
, is_new
, is_delete
;
56 int lines_added
, lines_deleted
;
58 struct fragment
*fragments
;
60 unsigned long resultsize
;
61 char old_sha1_prefix
[41];
62 char new_sha1_prefix
[41];
66 #define CHUNKSIZE (8192)
69 static void *read_patch_file(int fd
, unsigned long *sizep
)
71 unsigned long size
= 0, alloc
= CHUNKSIZE
;
72 void *buffer
= xmalloc(alloc
);
75 int nr
= alloc
- size
;
78 buffer
= xrealloc(buffer
, alloc
);
81 nr
= read(fd
, buffer
+ size
, nr
);
87 die("git-apply: read returned %s", strerror(errno
));
94 * Make sure that we have some slop in the buffer
95 * so that we can do speculative "memcmp" etc, and
96 * see to it that it is NUL-filled.
98 if (alloc
< size
+ SLOP
)
99 buffer
= xrealloc(buffer
, size
+ SLOP
);
100 memset(buffer
+ size
, 0, SLOP
);
104 static unsigned long linelen(const char *buffer
, unsigned long size
)
106 unsigned long len
= 0;
109 if (*buffer
++ == '\n')
115 static int is_dev_null(const char *str
)
117 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
123 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
125 if (c
== ' ' && !(terminate
& TERM_SPACE
))
127 if (c
== '\t' && !(terminate
& TERM_TAB
))
133 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
136 const char *start
= line
;
145 if (name_terminate(start
, line
-start
, c
, terminate
))
149 if (c
== '/' && !--p_value
)
159 * Generally we prefer the shorter name, especially
160 * if the other one is just a variation of that with
161 * something else tacked on to the end (ie "file.orig"
165 int deflen
= strlen(def
);
166 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
170 name
= xmalloc(len
+ 1);
171 memcpy(name
, start
, len
);
178 * Get the name etc info from the --/+++ lines of a traditional patch header
180 * NOTE! This hardcodes "-p1" behaviour in filename detection.
182 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
183 * files, we can happily check the index for a match, but for creating a
184 * new file we should try to match whatever "patch" does. I have no idea.
186 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
191 first
+= 4; // skip "--- "
192 second
+= 4; // skip "+++ "
193 if (is_dev_null(first
)) {
195 patch
->is_delete
= 0;
196 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
197 patch
->new_name
= name
;
198 } else if (is_dev_null(second
)) {
200 patch
->is_delete
= 1;
201 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
202 patch
->old_name
= name
;
204 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
205 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
206 patch
->old_name
= patch
->new_name
= name
;
209 die("unable to find filename in patch at line %d", linenr
);
212 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
218 * We're anal about diff header consistency, to make
219 * sure that we don't end up having strange ambiguous
220 * patches floating around.
222 * As a result, gitdiff_{old|new}name() will check
223 * their names against any previous information, just
226 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
231 if (!orig_name
&& !isnull
)
232 return find_name(line
, NULL
, 1, 0);
240 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
253 if (memcmp(line
, name
, len
) || line
[len
] != '\n')
257 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
261 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
263 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
267 static int gitdiff_newname(const char *line
, struct patch
*patch
)
269 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
273 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
275 patch
->old_mode
= strtoul(line
, NULL
, 8);
279 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
281 patch
->new_mode
= strtoul(line
, NULL
, 8);
285 static int gitdiff_delete(const char *line
, struct patch
*patch
)
287 patch
->is_delete
= 1;
288 patch
->old_name
= patch
->def_name
;
289 return gitdiff_oldmode(line
, patch
);
292 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
295 patch
->new_name
= patch
->def_name
;
296 return gitdiff_newmode(line
, patch
);
299 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
302 patch
->old_name
= find_name(line
, NULL
, 0, 0);
306 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
309 patch
->new_name
= find_name(line
, NULL
, 0, 0);
313 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
315 patch
->is_rename
= 1;
316 patch
->old_name
= find_name(line
, NULL
, 0, 0);
320 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
322 patch
->is_rename
= 1;
323 patch
->new_name
= find_name(line
, NULL
, 0, 0);
327 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
329 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
334 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
336 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
341 static int gitdiff_index(const char *line
, struct patch
*patch
)
343 /* index line is N hexadecimal, "..", N hexadecimal,
344 * and optional space with octal mode.
346 const char *ptr
, *eol
;
349 ptr
= strchr(line
, '.');
350 if (!ptr
|| ptr
[1] != '.' || 40 <= ptr
- line
)
353 memcpy(patch
->old_sha1_prefix
, line
, len
);
354 patch
->old_sha1_prefix
[len
] = 0;
357 ptr
= strchr(line
, ' ');
358 eol
= strchr(line
, '\n');
360 if (!ptr
|| eol
< ptr
)
366 memcpy(patch
->new_sha1_prefix
, line
, len
);
367 patch
->new_sha1_prefix
[len
] = 0;
369 patch
->new_mode
= patch
->old_mode
= strtoul(ptr
+1, NULL
, 8);
374 * This is normal for a diff that doesn't change anything: we'll fall through
375 * into the next diff. Tell the parser to break out.
377 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
382 static char *git_header_name(char *line
)
400 * We don't accept absolute paths (/dev/null) as possibly valid
406 * Accept a name only if it shows up twice, exactly the same
409 for (len
= 0 ; ; len
++) {
426 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
427 char *ret
= xmalloc(len
+ 1);
428 memcpy(ret
, name
, len
);
437 /* Verify that we recognize the lines following a git header */
438 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
440 unsigned long offset
;
442 /* A git diff has explicit new/delete information, so we don't guess */
444 patch
->is_delete
= 0;
447 * Some things may not have the old name in the
448 * rest of the headers anywhere (pure mode changes,
449 * or removing or adding empty files), so we get
450 * the default name from the header.
452 patch
->def_name
= git_header_name(line
+ strlen("diff --git "));
457 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
458 static const struct opentry
{
460 int (*fn
)(const char *, struct patch
*);
462 { "@@ -", gitdiff_hdrend
},
463 { "--- ", gitdiff_oldname
},
464 { "+++ ", gitdiff_newname
},
465 { "old mode ", gitdiff_oldmode
},
466 { "new mode ", gitdiff_newmode
},
467 { "deleted file mode ", gitdiff_delete
},
468 { "new file mode ", gitdiff_newfile
},
469 { "copy from ", gitdiff_copysrc
},
470 { "copy to ", gitdiff_copydst
},
471 { "rename old ", gitdiff_renamesrc
},
472 { "rename new ", gitdiff_renamedst
},
473 { "rename from ", gitdiff_renamesrc
},
474 { "rename to ", gitdiff_renamedst
},
475 { "similarity index ", gitdiff_similarity
},
476 { "dissimilarity index ", gitdiff_dissimilarity
},
477 { "index ", gitdiff_index
},
478 { "", gitdiff_unrecognized
},
482 len
= linelen(line
, size
);
483 if (!len
|| line
[len
-1] != '\n')
485 for (i
= 0; i
< sizeof(optable
) / sizeof(optable
[0]); i
++) {
486 const struct opentry
*p
= optable
+ i
;
487 int oplen
= strlen(p
->str
);
488 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
490 if (p
->fn(line
+ oplen
, patch
) < 0)
499 static int parse_num(const char *line
, unsigned long *p
)
505 *p
= strtoul(line
, &ptr
, 10);
509 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
510 unsigned long *p1
, unsigned long *p2
)
514 if (offset
< 0 || offset
>= len
)
519 digits
= parse_num(line
, p1
);
529 digits
= parse_num(line
+1, p2
);
541 if (memcmp(line
, expect
, ex
))
548 * Parse a unified diff fragment header of the
549 * form "@@ -a,b +c,d @@"
551 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
555 if (!len
|| line
[len
-1] != '\n')
558 /* Figure out the number of lines in a fragment */
559 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
560 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
565 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
567 unsigned long offset
, len
;
569 patch
->is_rename
= patch
->is_copy
= 0;
570 patch
->is_new
= patch
->is_delete
= -1;
571 patch
->old_mode
= patch
->new_mode
= 0;
572 patch
->old_name
= patch
->new_name
= NULL
;
573 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
574 unsigned long nextlen
;
576 len
= linelen(line
, size
);
580 /* Testing this early allows us to take a few shortcuts.. */
585 * Make sure we don't find any unconnected patch fragmants.
586 * That's a sign that we didn't find a header, and that a
587 * patch has become corrupted/broken up.
589 if (!memcmp("@@ -", line
, 4)) {
590 struct fragment dummy
;
591 if (parse_fragment_header(line
, len
, &dummy
) < 0)
593 error("patch fragment without header at line %d: %.*s", linenr
, (int)len
-1, line
);
600 * Git patch? It might not have a real patch, just a rename
601 * or mode change, so we handle that specially
603 if (!memcmp("diff --git ", line
, 11)) {
604 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
605 if (git_hdr_len
<= len
)
607 if (!patch
->old_name
&& !patch
->new_name
) {
608 if (!patch
->def_name
)
609 die("git diff header lacks filename information (line %d)", linenr
);
610 patch
->old_name
= patch
->new_name
= patch
->def_name
;
612 *hdrsize
= git_hdr_len
;
616 /** --- followed by +++ ? */
617 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
621 * We only accept unified patches, so we want it to
622 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
625 nextlen
= linelen(line
+ len
, size
- len
);
626 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
629 /* Ok, we'll consider it a patch */
630 parse_traditional_patch(line
, line
+len
, patch
);
631 *hdrsize
= len
+ nextlen
;
639 * Parse a unified diff. Note that this really needs
640 * to parse each fragment separately, since the only
641 * way to know the difference between a "---" that is
642 * part of a patch, and a "---" that starts the next
643 * patch is to look at the line counts..
645 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
648 int len
= linelen(line
, size
), offset
;
649 unsigned long oldlines
, newlines
;
651 offset
= parse_fragment_header(line
, len
, fragment
);
654 oldlines
= fragment
->oldlines
;
655 newlines
= fragment
->newlines
;
657 if (patch
->is_new
< 0) {
658 patch
->is_new
= !oldlines
;
660 patch
->old_name
= NULL
;
662 if (patch
->is_delete
< 0) {
663 patch
->is_delete
= !newlines
;
665 patch
->new_name
= NULL
;
668 if (patch
->is_new
!= !oldlines
)
669 return error("new file depends on old contents");
670 if (patch
->is_delete
!= !newlines
) {
672 return error("deleted file still has contents");
673 fprintf(stderr
, "** warning: file %s becomes empty but is not deleted\n", patch
->new_name
);
676 /* Parse the thing.. */
681 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
682 if (!oldlines
&& !newlines
)
684 len
= linelen(line
, size
);
685 if (!len
|| line
[len
-1] != '\n')
703 /* We allow "\ No newline at end of file". Depending
704 * on locale settings when the patch was produced we
705 * don't know what this line looks like. The only
706 * thing we do know is that it begins with "\ ".
707 * Checking for 12 is just for sanity check -- any
708 * l10n of "\ No newline..." is at least that long.
711 if (len
< 12 || memcmp(line
, "\\ ", 2))
716 /* If a fragment ends with an incomplete line, we failed to include
717 * it in the above loop because we hit oldlines == newlines == 0
720 if (12 < size
&& !memcmp(line
, "\\ ", 2))
721 offset
+= linelen(line
, size
);
723 patch
->lines_added
+= added
;
724 patch
->lines_deleted
+= deleted
;
728 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
730 unsigned long offset
= 0;
731 struct fragment
**fragp
= &patch
->fragments
;
733 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
734 struct fragment
*fragment
;
737 fragment
= xmalloc(sizeof(*fragment
));
738 memset(fragment
, 0, sizeof(*fragment
));
739 len
= parse_fragment(line
, size
, patch
, fragment
);
741 die("corrupt patch at line %d", linenr
);
743 fragment
->patch
= line
;
744 fragment
->size
= len
;
747 fragp
= &fragment
->next
;
756 static inline int metadata_changes(struct patch
*patch
)
758 return patch
->is_rename
> 0 ||
759 patch
->is_copy
> 0 ||
762 (patch
->old_mode
&& patch
->new_mode
&&
763 patch
->old_mode
!= patch
->new_mode
);
766 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
768 int hdrsize
, patchsize
;
769 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
774 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
776 if (!patchsize
&& !metadata_changes(patch
))
777 die("patch with only garbage at line %d", linenr
);
779 return offset
+ hdrsize
+ patchsize
;
782 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
783 static const char minuses
[]= "----------------------------------------------------------------------";
785 static void show_stats(struct patch
*patch
)
787 const char *prefix
= "";
788 char *name
= patch
->new_name
;
789 int len
, max
, add
, del
, total
;
792 name
= patch
->old_name
;
795 * "scale" the filename
806 slash
= strchr(name
, '/');
813 * scale the add/delete
819 add
= patch
->lines_added
;
820 del
= patch
->lines_deleted
;
823 if (max_change
> 0) {
824 total
= (total
* max
+ max_change
/ 2) / max_change
;
825 add
= (add
* max
+ max_change
/ 2) / max_change
;
828 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
829 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
830 add
, pluses
, del
, minuses
);
833 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
838 switch (st
->st_mode
& S_IFMT
) {
840 return readlink(path
, buf
, size
);
842 fd
= open(path
, O_RDONLY
);
844 return error("unable to open %s", path
);
847 int ret
= read(fd
, buf
+ got
, size
- got
);
865 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
)
868 unsigned long start
, backwards
, forwards
;
875 unsigned long offset
= 0;
877 while (offset
+ fragsize
<= size
) {
878 if (buf
[offset
++] == '\n') {
886 /* Exact line number? */
887 if (!memcmp(buf
+ start
, fragment
, fragsize
))
891 * There's probably some smart way to do this, but I'll leave
892 * that to the smart and beautiful people. I'm simple and stupid.
903 if (forwards
+ fragsize
> size
)
909 } while (backwards
&& buf
[backwards
-1] != '\n');
912 while (forwards
+ fragsize
<= size
) {
913 if (buf
[forwards
++] == '\n')
919 if (try + fragsize
> size
)
921 if (memcmp(buf
+ try, fragment
, fragsize
))
930 * We should start searching forward and backward.
941 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
943 char *buf
= desc
->buffer
;
944 const char *patch
= frag
->patch
;
945 int offset
, size
= frag
->size
;
946 char *old
= xmalloc(size
);
947 char *new = xmalloc(size
);
948 int oldsize
= 0, newsize
= 0;
951 int len
= linelen(patch
, size
);
958 * "plen" is how much of the line we should use for
959 * the actual patch data. Normally we just remove the
960 * first character on the line, but if the line is
961 * followed by "\ No newline", then we also remove the
962 * last one (which is the newline, of course).
965 if (len
< size
&& patch
[len
] == '\\')
970 memcpy(old
+ oldsize
, patch
+ 1, plen
);
974 /* Fall-through for ' ' */
976 memcpy(new + newsize
, patch
+ 1, plen
);
980 /* Ignore it, we already handled it */
989 offset
= find_offset(buf
, desc
->size
, old
, oldsize
, frag
->newpos
);
991 int diff
= newsize
- oldsize
;
992 unsigned long size
= desc
->size
+ diff
;
993 unsigned long alloc
= desc
->alloc
;
998 buf
= xrealloc(buf
, alloc
);
1002 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
1003 memcpy(buf
+ offset
, new, newsize
);
1012 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
1014 struct fragment
*frag
= patch
->fragments
;
1017 if (apply_one_fragment(desc
, frag
) < 0)
1018 return error("patch failed: %s:%ld", patch
->old_name
, frag
->oldpos
);
1024 static int apply_data(struct patch
*patch
, struct stat
*st
)
1027 unsigned long size
, alloc
;
1028 struct buffer_desc desc
;
1033 if (patch
->old_name
) {
1035 alloc
= size
+ 8192;
1036 buf
= xmalloc(alloc
);
1037 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
1038 return error("read of %s failed", patch
->old_name
);
1044 if (apply_fragments(&desc
, patch
) < 0)
1046 patch
->result
= desc
.buffer
;
1047 patch
->resultsize
= desc
.size
;
1049 if (patch
->is_delete
&& patch
->resultsize
)
1050 return error("removal patch leaves file contents");
1055 static int check_patch(struct patch
*patch
)
1058 const char *old_name
= patch
->old_name
;
1059 const char *new_name
= patch
->new_name
;
1063 int stat_ret
= lstat(old_name
, &st
);
1066 int pos
= cache_name_pos(old_name
, strlen(old_name
));
1068 return error("%s: does not exist in index",
1071 struct checkout costate
;
1072 if (errno
!= ENOENT
)
1073 return error("%s: %s", old_name
,
1076 costate
.base_dir
= "";
1077 costate
.base_dir_len
= 0;
1080 costate
.not_new
= 0;
1081 costate
.refresh_cache
= 1;
1082 if (checkout_entry(active_cache
[pos
],
1084 lstat(old_name
, &st
))
1088 changed
= ce_match_stat(active_cache
[pos
], &st
);
1090 return error("%s: does not match index",
1093 else if (stat_ret
< 0)
1094 return error("%s: %s", old_name
, strerror(errno
));
1096 if (patch
->is_new
< 0)
1098 st
.st_mode
= ntohl(create_ce_mode(st
.st_mode
));
1099 if (!patch
->old_mode
)
1100 patch
->old_mode
= st
.st_mode
;
1101 if ((st
.st_mode
^ patch
->old_mode
) & S_IFMT
)
1102 return error("%s: wrong type", old_name
);
1103 if (st
.st_mode
!= patch
->old_mode
)
1104 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
1105 old_name
, st
.st_mode
, patch
->old_mode
);
1108 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
1109 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
1110 return error("%s: already exists in index", new_name
);
1111 if (!lstat(new_name
, &st
))
1112 return error("%s: already exists in working directory", new_name
);
1113 if (errno
!= ENOENT
)
1114 return error("%s: %s", new_name
, strerror(errno
));
1115 if (!patch
->new_mode
) {
1117 patch
->new_mode
= S_IFREG
| 0644;
1119 patch
->new_mode
= patch
->old_mode
;
1123 if (new_name
&& old_name
) {
1124 int same
= !strcmp(old_name
, new_name
);
1125 if (!patch
->new_mode
)
1126 patch
->new_mode
= patch
->old_mode
;
1127 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1128 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1129 patch
->new_mode
, new_name
, patch
->old_mode
,
1130 same
? "" : " of ", same
? "" : old_name
);
1133 if (apply_data(patch
, &st
) < 0)
1134 return error("%s: patch does not apply", old_name
);
1138 static int check_patch_list(struct patch
*patch
)
1142 for (;patch
; patch
= patch
->next
)
1143 error
|= check_patch(patch
);
1147 static void show_file(int c
, unsigned int mode
, const char *name
)
1149 printf("%c %o %s\n", c
, mode
, name
);
1152 static void show_file_list(struct patch
*patch
)
1154 for (;patch
; patch
= patch
->next
) {
1155 if (patch
->is_rename
) {
1156 show_file('-', patch
->old_mode
, patch
->old_name
);
1157 show_file('+', patch
->new_mode
, patch
->new_name
);
1160 if (patch
->is_copy
|| patch
->is_new
) {
1161 show_file('+', patch
->new_mode
, patch
->new_name
);
1164 if (patch
->is_delete
) {
1165 show_file('-', patch
->old_mode
, patch
->old_name
);
1168 if (patch
->old_mode
&& patch
->new_mode
&& patch
->old_mode
!= patch
->new_mode
) {
1169 printf("M %o:%o %s\n", patch
->old_mode
, patch
->new_mode
, patch
->old_name
);
1172 printf("M %o %s\n", patch
->old_mode
, patch
->old_name
);
1176 static inline int is_null_sha1(const unsigned char *sha1
)
1178 return !memcmp(sha1
, null_sha1
, 20);
1181 static void show_index_list(struct patch
*list
)
1183 struct patch
*patch
;
1185 /* Once we start supporting the reverse patch, it may be
1186 * worth showing the new sha1 prefix, but until then...
1188 for (patch
= list
; patch
; patch
= patch
->next
) {
1189 const unsigned char *sha1_ptr
;
1190 unsigned char sha1
[20];
1193 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1195 sha1_ptr
= null_sha1
;
1196 else if (get_sha1(patch
->old_sha1_prefix
, sha1
))
1197 die("sha1 information is lacking or useless (%s).",
1201 printf("%06o %s %s\n",patch
->old_mode
,
1202 sha1_to_hex(sha1_ptr
), name
);
1206 static void stat_patch_list(struct patch
*patch
)
1208 int files
, adds
, dels
;
1210 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1212 adds
+= patch
->lines_added
;
1213 dels
+= patch
->lines_deleted
;
1217 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1220 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
1223 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
1225 printf(" %s %s\n", newdelete
, name
);
1228 static void show_mode_change(struct patch
*p
, int show_name
)
1230 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
1232 printf(" mode change %06o => %06o %s\n",
1233 p
->old_mode
, p
->new_mode
, p
->new_name
);
1235 printf(" mode change %06o => %06o\n",
1236 p
->old_mode
, p
->new_mode
);
1240 static void show_rename_copy(struct patch
*p
)
1242 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
1243 const char *old
, *new;
1245 /* Find common prefix */
1249 const char *slash_old
, *slash_new
;
1250 slash_old
= strchr(old
, '/');
1251 slash_new
= strchr(new, '/');
1254 slash_old
- old
!= slash_new
- new ||
1255 memcmp(old
, new, slash_new
- new))
1257 old
= slash_old
+ 1;
1258 new = slash_new
+ 1;
1260 /* p->old_name thru old is the common prefix, and old and new
1261 * through the end of names are renames
1263 if (old
!= p
->old_name
)
1264 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
1265 (int)(old
- p
->old_name
), p
->old_name
,
1266 old
, new, p
->score
);
1268 printf(" %s %s => %s (%d%%)\n", renamecopy
,
1269 p
->old_name
, p
->new_name
, p
->score
);
1270 show_mode_change(p
, 0);
1273 static void summary_patch_list(struct patch
*patch
)
1277 for (p
= patch
; p
; p
= p
->next
) {
1279 show_file_mode_name("create", p
->new_mode
, p
->new_name
);
1280 else if (p
->is_delete
)
1281 show_file_mode_name("delete", p
->old_mode
, p
->old_name
);
1283 if (p
->is_rename
|| p
->is_copy
)
1284 show_rename_copy(p
);
1287 printf(" rewrite %s (%d%%)\n",
1288 p
->new_name
, p
->score
);
1289 show_mode_change(p
, 0);
1292 show_mode_change(p
, 1);
1298 static void patch_stats(struct patch
*patch
)
1300 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1302 if (lines
> max_change
)
1304 if (patch
->old_name
) {
1305 int len
= strlen(patch
->old_name
);
1309 if (patch
->new_name
) {
1310 int len
= strlen(patch
->new_name
);
1316 static void remove_file(struct patch
*patch
)
1319 if (remove_file_from_cache(patch
->old_name
) < 0)
1320 die("unable to remove %s from index", patch
->old_name
);
1322 unlink(patch
->old_name
);
1325 static void add_index_file(const char *path
, unsigned mode
, void *buf
, unsigned long size
)
1328 struct cache_entry
*ce
;
1329 int namelen
= strlen(path
);
1330 unsigned ce_size
= cache_entry_size(namelen
);
1335 ce
= xmalloc(ce_size
);
1336 memset(ce
, 0, ce_size
);
1337 memcpy(ce
->name
, path
, namelen
);
1338 ce
->ce_mode
= create_ce_mode(mode
);
1339 ce
->ce_flags
= htons(namelen
);
1340 if (lstat(path
, &st
) < 0)
1341 die("unable to stat newly created file %s", path
);
1342 fill_stat_cache_info(ce
, &st
);
1343 if (write_sha1_file(buf
, size
, "blob", ce
->sha1
) < 0)
1344 die("unable to create backing store for newly created file %s", path
);
1345 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
1346 die("unable to add cache entry for %s", path
);
1349 static void create_subdirectories(const char *path
)
1351 int len
= strlen(path
);
1352 char *buf
= xmalloc(len
+ 1);
1353 const char *slash
= path
;
1355 while ((slash
= strchr(slash
+1, '/')) != NULL
) {
1357 memcpy(buf
, path
, len
);
1359 if (mkdir(buf
, 0777) < 0) {
1360 if (errno
!= EEXIST
)
1367 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
1372 return symlink(buf
, path
);
1373 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
| O_TRUNC
, (mode
& 0100) ? 0777 : 0666);
1377 int written
= write(fd
, buf
, size
);
1379 if (errno
== EINTR
|| errno
== EAGAIN
)
1381 die("writing file %s: %s", path
, strerror(errno
));
1384 die("out of space writing file %s", path
);
1389 die("closing file %s: %s", path
, strerror(errno
));
1394 * We optimistically assume that the directories exist,
1395 * which is true 99% of the time anyway. If they don't,
1396 * we create them and try again.
1398 static void create_one_file(const char *path
, unsigned mode
, const char *buf
, unsigned long size
)
1400 if (!try_create_file(path
, mode
, buf
, size
))
1403 if (errno
== ENOENT
) {
1404 create_subdirectories(path
);
1405 if (!try_create_file(path
, mode
, buf
, size
))
1409 if (errno
== EEXIST
) {
1410 unsigned int nr
= getpid();
1413 const char *newpath
;
1414 newpath
= mkpath("%s~%u", path
, nr
);
1415 if (!try_create_file(newpath
, mode
, buf
, size
)) {
1416 if (!rename(newpath
, path
))
1421 if (errno
!= EEXIST
)
1425 die("unable to write file %s mode %o", path
, mode
);
1428 static void create_file(struct patch
*patch
)
1430 const char *path
= patch
->new_name
;
1431 unsigned mode
= patch
->new_mode
;
1432 unsigned long size
= patch
->resultsize
;
1433 char *buf
= patch
->result
;
1436 mode
= S_IFREG
| 0644;
1437 create_one_file(path
, mode
, buf
, size
);
1438 add_index_file(path
, mode
, buf
, size
);
1441 static void write_out_one_result(struct patch
*patch
)
1443 if (patch
->is_delete
> 0) {
1447 if (patch
->is_new
> 0 || patch
->is_copy
) {
1452 * Rename or modification boils down to the same
1453 * thing: remove the old, write the new
1459 static void write_out_results(struct patch
*list
, int skipped_patch
)
1461 if (!list
&& !skipped_patch
)
1465 write_out_one_result(list
);
1470 static struct cache_file cache_file
;
1472 static struct excludes
{
1473 struct excludes
*next
;
1477 static int use_patch(struct patch
*p
)
1479 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
1480 struct excludes
*x
= excludes
;
1482 if (fnmatch(x
->path
, pathname
, 0) == 0)
1489 static int apply_patch(int fd
)
1492 unsigned long offset
, size
;
1493 char *buffer
= read_patch_file(fd
, &size
);
1494 struct patch
*list
= NULL
, **listp
= &list
;
1495 int skipped_patch
= 0;
1501 struct patch
*patch
;
1504 patch
= xmalloc(sizeof(*patch
));
1505 memset(patch
, 0, sizeof(*patch
));
1506 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
1509 if (use_patch(patch
)) {
1512 listp
= &patch
->next
;
1514 /* perhaps free it a bit better? */
1523 write_index
= check_index
&& apply
;
1525 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
1527 if (read_cache() < 0)
1528 die("unable to read index file");
1531 if ((check
|| apply
) && check_patch_list(list
) < 0)
1535 write_out_results(list
, skipped_patch
);
1538 if (write_cache(newfd
, active_cache
, active_nr
) ||
1539 commit_index_file(&cache_file
))
1540 die("Unable to write new cachefile");
1544 show_file_list(list
);
1546 if (show_index_info
)
1547 show_index_list(list
);
1550 stat_patch_list(list
);
1553 summary_patch_list(list
);
1559 int main(int argc
, char **argv
)
1564 for (i
= 1; i
< argc
; i
++) {
1565 const char *arg
= argv
[i
];
1568 if (!strcmp(arg
, "-")) {
1573 if (!strncmp(arg
, "--exclude=", 10)) {
1574 struct excludes
*x
= xmalloc(sizeof(*x
));
1580 if (!strcmp(arg
, "--stat")) {
1585 if (!strcmp(arg
, "--summary")) {
1590 if (!strcmp(arg
, "--check")) {
1595 if (!strcmp(arg
, "--index")) {
1599 if (!strcmp(arg
, "--apply")) {
1603 if (!strcmp(arg
, "--show-files")) {
1607 if (!strcmp(arg
, "--show-index-info")) {
1609 show_index_info
= 1;
1612 fd
= open(arg
, O_RDONLY
);