4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
15 // --check turns on checking that the working tree matches the
16 // files that are being modified, but doesn't apply the patch
17 // --stat does just a diffstat, and doesn't actually apply
18 // --numstat does numeric diffstat, and doesn't actually apply
19 // --index-info shows the old and new index info for paths if available.
20 // --index updates the cache as well.
21 // --cached updates only the cache without ever touching the working tree.
23 static const char *prefix
;
24 static int prefix_length
= -1;
25 static int newfd
= -1;
27 static int p_value
= 1;
28 static int allow_binary_replacement
= 0;
29 static int check_index
= 0;
30 static int write_index
= 0;
31 static int cached
= 0;
32 static int diffstat
= 0;
33 static int numstat
= 0;
34 static int summary
= 0;
37 static int no_add
= 0;
38 static int show_index_info
= 0;
39 static int line_termination
= '\n';
40 static unsigned long p_context
= -1;
41 static const char apply_usage
[] =
42 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
44 static enum whitespace_eol
{
49 } new_whitespace
= warn_on_whitespace
;
50 static int whitespace_error
= 0;
51 static int squelch_whitespace_errors
= 5;
52 static int applied_after_stripping
= 0;
53 static const char *patch_input_file
= NULL
;
55 static void parse_whitespace_option(const char *option
)
58 new_whitespace
= warn_on_whitespace
;
61 if (!strcmp(option
, "warn")) {
62 new_whitespace
= warn_on_whitespace
;
65 if (!strcmp(option
, "nowarn")) {
66 new_whitespace
= nowarn_whitespace
;
69 if (!strcmp(option
, "error")) {
70 new_whitespace
= error_on_whitespace
;
73 if (!strcmp(option
, "error-all")) {
74 new_whitespace
= error_on_whitespace
;
75 squelch_whitespace_errors
= 0;
78 if (!strcmp(option
, "strip")) {
79 new_whitespace
= strip_whitespace
;
82 die("unrecognized whitespace option '%s'", option
);
85 static void set_default_whitespace_mode(const char *whitespace_option
)
87 if (!whitespace_option
&& !apply_default_whitespace
) {
88 new_whitespace
= (apply
95 * For "diff-stat" like behaviour, we keep track of the biggest change
96 * we've seen, and the longest filename. That allows us to do simple
99 static int max_change
, max_len
;
102 * Various "current state", notably line numbers and what
103 * file (and how) we're patching right now.. The "is_xxxx"
104 * things are flags, where -1 means "don't know yet".
106 static int linenr
= 1;
109 unsigned long leading
, trailing
;
110 unsigned long oldpos
, oldlines
;
111 unsigned long newpos
, newlines
;
114 struct fragment
*next
;
118 char *new_name
, *old_name
, *def_name
;
119 unsigned int old_mode
, new_mode
;
120 int is_rename
, is_copy
, is_new
, is_delete
, is_binary
;
121 #define BINARY_DELTA_DEFLATED 1
122 #define BINARY_LITERAL_DEFLATED 2
123 unsigned long deflate_origlen
;
124 int lines_added
, lines_deleted
;
126 struct fragment
*fragments
;
128 unsigned long resultsize
;
129 char old_sha1_prefix
[41];
130 char new_sha1_prefix
[41];
134 #define CHUNKSIZE (8192)
137 static void *read_patch_file(int fd
, unsigned long *sizep
)
139 unsigned long size
= 0, alloc
= CHUNKSIZE
;
140 void *buffer
= xmalloc(alloc
);
143 int nr
= alloc
- size
;
146 buffer
= xrealloc(buffer
, alloc
);
149 nr
= xread(fd
, buffer
+ size
, nr
);
153 die("git-apply: read returned %s", strerror(errno
));
159 * Make sure that we have some slop in the buffer
160 * so that we can do speculative "memcmp" etc, and
161 * see to it that it is NUL-filled.
163 if (alloc
< size
+ SLOP
)
164 buffer
= xrealloc(buffer
, size
+ SLOP
);
165 memset(buffer
+ size
, 0, SLOP
);
169 static unsigned long linelen(const char *buffer
, unsigned long size
)
171 unsigned long len
= 0;
174 if (*buffer
++ == '\n')
180 static int is_dev_null(const char *str
)
182 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
188 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
190 if (c
== ' ' && !(terminate
& TERM_SPACE
))
192 if (c
== '\t' && !(terminate
& TERM_TAB
))
198 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
201 const char *start
= line
;
205 /* Proposed "new-style" GNU patch/diff format; see
206 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
208 name
= unquote_c_style(line
, NULL
);
212 cp
= strchr(name
, '/');
219 /* name can later be freed, so we need
220 * to memmove, not just return cp
222 memmove(name
, cp
, strlen(cp
) + 1);
239 if (name_terminate(start
, line
-start
, c
, terminate
))
243 if (c
== '/' && !--p_value
)
253 * Generally we prefer the shorter name, especially
254 * if the other one is just a variation of that with
255 * something else tacked on to the end (ie "file.orig"
259 int deflen
= strlen(def
);
260 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
264 name
= xmalloc(len
+ 1);
265 memcpy(name
, start
, len
);
272 * Get the name etc info from the --/+++ lines of a traditional patch header
274 * NOTE! This hardcodes "-p1" behaviour in filename detection.
276 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
277 * files, we can happily check the index for a match, but for creating a
278 * new file we should try to match whatever "patch" does. I have no idea.
280 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
284 first
+= 4; // skip "--- "
285 second
+= 4; // skip "+++ "
286 if (is_dev_null(first
)) {
288 patch
->is_delete
= 0;
289 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
290 patch
->new_name
= name
;
291 } else if (is_dev_null(second
)) {
293 patch
->is_delete
= 1;
294 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
295 patch
->old_name
= name
;
297 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
298 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
299 patch
->old_name
= patch
->new_name
= name
;
302 die("unable to find filename in patch at line %d", linenr
);
305 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
311 * We're anal about diff header consistency, to make
312 * sure that we don't end up having strange ambiguous
313 * patches floating around.
315 * As a result, gitdiff_{old|new}name() will check
316 * their names against any previous information, just
319 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
321 if (!orig_name
&& !isnull
)
322 return find_name(line
, NULL
, 1, 0);
331 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
332 another
= find_name(line
, NULL
, 1, 0);
333 if (!another
|| memcmp(another
, name
, len
))
334 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
339 /* expect "/dev/null" */
340 if (memcmp("/dev/null", line
, 9) || line
[9] != '\n')
341 die("git-apply: bad git-diff - expected /dev/null on line %d", linenr
);
346 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
348 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
352 static int gitdiff_newname(const char *line
, struct patch
*patch
)
354 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
358 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
360 patch
->old_mode
= strtoul(line
, NULL
, 8);
364 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
366 patch
->new_mode
= strtoul(line
, NULL
, 8);
370 static int gitdiff_delete(const char *line
, struct patch
*patch
)
372 patch
->is_delete
= 1;
373 patch
->old_name
= patch
->def_name
;
374 return gitdiff_oldmode(line
, patch
);
377 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
380 patch
->new_name
= patch
->def_name
;
381 return gitdiff_newmode(line
, patch
);
384 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
387 patch
->old_name
= find_name(line
, NULL
, 0, 0);
391 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
394 patch
->new_name
= find_name(line
, NULL
, 0, 0);
398 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
400 patch
->is_rename
= 1;
401 patch
->old_name
= find_name(line
, NULL
, 0, 0);
405 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
407 patch
->is_rename
= 1;
408 patch
->new_name
= find_name(line
, NULL
, 0, 0);
412 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
414 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
419 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
421 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
426 static int gitdiff_index(const char *line
, struct patch
*patch
)
428 /* index line is N hexadecimal, "..", N hexadecimal,
429 * and optional space with octal mode.
431 const char *ptr
, *eol
;
434 ptr
= strchr(line
, '.');
435 if (!ptr
|| ptr
[1] != '.' || 40 < ptr
- line
)
438 memcpy(patch
->old_sha1_prefix
, line
, len
);
439 patch
->old_sha1_prefix
[len
] = 0;
442 ptr
= strchr(line
, ' ');
443 eol
= strchr(line
, '\n');
445 if (!ptr
|| eol
< ptr
)
451 memcpy(patch
->new_sha1_prefix
, line
, len
);
452 patch
->new_sha1_prefix
[len
] = 0;
454 patch
->new_mode
= patch
->old_mode
= strtoul(ptr
+1, NULL
, 8);
459 * This is normal for a diff that doesn't change anything: we'll fall through
460 * into the next diff. Tell the parser to break out.
462 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
467 static const char *stop_at_slash(const char *line
, int llen
)
471 for (i
= 0; i
< llen
; i
++) {
479 /* This is to extract the same name that appears on "diff --git"
480 * line. We do not find and return anything if it is a rename
481 * patch, and it is OK because we will find the name elsewhere.
482 * We need to reliably find name only when it is mode-change only,
483 * creation or deletion of an empty file. In any of these cases,
484 * both sides are the same name under a/ and b/ respectively.
486 static char *git_header_name(char *line
, int llen
)
490 const char *second
= NULL
;
492 line
+= strlen("diff --git ");
493 llen
-= strlen("diff --git ");
497 char *first
= unquote_c_style(line
, &second
);
501 /* advance to the first slash */
502 cp
= stop_at_slash(first
, strlen(first
));
503 if (!cp
|| cp
== first
) {
504 /* we do not accept absolute paths */
510 memmove(first
, cp
+1, len
+1); /* including NUL */
512 /* second points at one past closing dq of name.
513 * find the second name.
515 while ((second
< line
+ llen
) && isspace(*second
))
518 if (line
+ llen
<= second
)
519 goto free_first_and_fail
;
520 if (*second
== '"') {
521 char *sp
= unquote_c_style(second
, NULL
);
523 goto free_first_and_fail
;
524 cp
= stop_at_slash(sp
, strlen(sp
));
525 if (!cp
|| cp
== sp
) {
528 goto free_first_and_fail
;
530 /* They must match, otherwise ignore */
531 if (strcmp(cp
+1, first
))
532 goto free_both_and_fail
;
537 /* unquoted second */
538 cp
= stop_at_slash(second
, line
+ llen
- second
);
539 if (!cp
|| cp
== second
)
540 goto free_first_and_fail
;
542 if (line
+ llen
- cp
!= len
+ 1 ||
543 memcmp(first
, cp
, len
))
544 goto free_first_and_fail
;
548 /* unquoted first name */
549 name
= stop_at_slash(line
, llen
);
550 if (!name
|| name
== line
)
555 /* since the first name is unquoted, a dq if exists must be
556 * the beginning of the second name.
558 for (second
= name
; second
< line
+ llen
; second
++) {
559 if (*second
== '"') {
560 const char *cp
= second
;
562 char *sp
= unquote_c_style(second
, NULL
);
566 np
= stop_at_slash(sp
, strlen(sp
));
567 if (!np
|| np
== sp
) {
568 free_second_and_fail
:
574 if (len
< cp
- name
&&
575 !strncmp(np
, name
, len
) &&
576 isspace(name
[len
])) {
578 memmove(sp
, np
, len
+ 1);
581 goto free_second_and_fail
;
586 * Accept a name only if it shows up twice, exactly the same
589 for (len
= 0 ; ; len
++) {
606 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
607 char *ret
= xmalloc(len
+ 1);
608 memcpy(ret
, name
, len
);
617 /* Verify that we recognize the lines following a git header */
618 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
620 unsigned long offset
;
622 /* A git diff has explicit new/delete information, so we don't guess */
624 patch
->is_delete
= 0;
627 * Some things may not have the old name in the
628 * rest of the headers anywhere (pure mode changes,
629 * or removing or adding empty files), so we get
630 * the default name from the header.
632 patch
->def_name
= git_header_name(line
, len
);
637 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
638 static const struct opentry
{
640 int (*fn
)(const char *, struct patch
*);
642 { "@@ -", gitdiff_hdrend
},
643 { "--- ", gitdiff_oldname
},
644 { "+++ ", gitdiff_newname
},
645 { "old mode ", gitdiff_oldmode
},
646 { "new mode ", gitdiff_newmode
},
647 { "deleted file mode ", gitdiff_delete
},
648 { "new file mode ", gitdiff_newfile
},
649 { "copy from ", gitdiff_copysrc
},
650 { "copy to ", gitdiff_copydst
},
651 { "rename old ", gitdiff_renamesrc
},
652 { "rename new ", gitdiff_renamedst
},
653 { "rename from ", gitdiff_renamesrc
},
654 { "rename to ", gitdiff_renamedst
},
655 { "similarity index ", gitdiff_similarity
},
656 { "dissimilarity index ", gitdiff_dissimilarity
},
657 { "index ", gitdiff_index
},
658 { "", gitdiff_unrecognized
},
662 len
= linelen(line
, size
);
663 if (!len
|| line
[len
-1] != '\n')
665 for (i
= 0; i
< ARRAY_SIZE(optable
); i
++) {
666 const struct opentry
*p
= optable
+ i
;
667 int oplen
= strlen(p
->str
);
668 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
670 if (p
->fn(line
+ oplen
, patch
) < 0)
679 static int parse_num(const char *line
, unsigned long *p
)
685 *p
= strtoul(line
, &ptr
, 10);
689 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
690 unsigned long *p1
, unsigned long *p2
)
694 if (offset
< 0 || offset
>= len
)
699 digits
= parse_num(line
, p1
);
709 digits
= parse_num(line
+1, p2
);
721 if (memcmp(line
, expect
, ex
))
728 * Parse a unified diff fragment header of the
729 * form "@@ -a,b +c,d @@"
731 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
735 if (!len
|| line
[len
-1] != '\n')
738 /* Figure out the number of lines in a fragment */
739 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
740 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
745 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
747 unsigned long offset
, len
;
749 patch
->is_rename
= patch
->is_copy
= 0;
750 patch
->is_new
= patch
->is_delete
= -1;
751 patch
->old_mode
= patch
->new_mode
= 0;
752 patch
->old_name
= patch
->new_name
= NULL
;
753 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
754 unsigned long nextlen
;
756 len
= linelen(line
, size
);
760 /* Testing this early allows us to take a few shortcuts.. */
765 * Make sure we don't find any unconnected patch fragmants.
766 * That's a sign that we didn't find a header, and that a
767 * patch has become corrupted/broken up.
769 if (!memcmp("@@ -", line
, 4)) {
770 struct fragment dummy
;
771 if (parse_fragment_header(line
, len
, &dummy
) < 0)
773 error("patch fragment without header at line %d: %.*s", linenr
, (int)len
-1, line
);
780 * Git patch? It might not have a real patch, just a rename
781 * or mode change, so we handle that specially
783 if (!memcmp("diff --git ", line
, 11)) {
784 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
785 if (git_hdr_len
<= len
)
787 if (!patch
->old_name
&& !patch
->new_name
) {
788 if (!patch
->def_name
)
789 die("git diff header lacks filename information (line %d)", linenr
);
790 patch
->old_name
= patch
->new_name
= patch
->def_name
;
792 *hdrsize
= git_hdr_len
;
796 /** --- followed by +++ ? */
797 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
801 * We only accept unified patches, so we want it to
802 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
805 nextlen
= linelen(line
+ len
, size
- len
);
806 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
809 /* Ok, we'll consider it a patch */
810 parse_traditional_patch(line
, line
+len
, patch
);
811 *hdrsize
= len
+ nextlen
;
819 * Parse a unified diff. Note that this really needs
820 * to parse each fragment separately, since the only
821 * way to know the difference between a "---" that is
822 * part of a patch, and a "---" that starts the next
823 * patch is to look at the line counts..
825 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
828 int len
= linelen(line
, size
), offset
;
829 unsigned long oldlines
, newlines
;
830 unsigned long leading
, trailing
;
832 offset
= parse_fragment_header(line
, len
, fragment
);
835 oldlines
= fragment
->oldlines
;
836 newlines
= fragment
->newlines
;
840 if (patch
->is_new
< 0) {
841 patch
->is_new
= !oldlines
;
843 patch
->old_name
= NULL
;
845 if (patch
->is_delete
< 0) {
846 patch
->is_delete
= !newlines
;
848 patch
->new_name
= NULL
;
851 if (patch
->is_new
&& oldlines
)
852 return error("new file depends on old contents");
853 if (patch
->is_delete
!= !newlines
) {
855 return error("deleted file still has contents");
856 fprintf(stderr
, "** warning: file %s becomes empty but is not deleted\n", patch
->new_name
);
859 /* Parse the thing.. */
864 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
865 if (!oldlines
&& !newlines
)
867 len
= linelen(line
, size
);
868 if (!len
|| line
[len
-1] != '\n')
876 if (!deleted
&& !added
)
887 * We know len is at least two, since we have a '+' and
888 * we checked that the last character was a '\n' above.
889 * That is, an addition of an empty line would check
890 * the '+' here. Sneaky...
892 if ((new_whitespace
!= nowarn_whitespace
) &&
893 isspace(line
[len
-2])) {
895 if (squelch_whitespace_errors
&&
896 squelch_whitespace_errors
<
900 fprintf(stderr
, "Adds trailing whitespace.\n%s:%d:%.*s\n",
902 linenr
, len
-2, line
+1);
910 /* We allow "\ No newline at end of file". Depending
911 * on locale settings when the patch was produced we
912 * don't know what this line looks like. The only
913 * thing we do know is that it begins with "\ ".
914 * Checking for 12 is just for sanity check -- any
915 * l10n of "\ No newline..." is at least that long.
918 if (len
< 12 || memcmp(line
, "\\ ", 2))
923 if (oldlines
|| newlines
)
925 fragment
->leading
= leading
;
926 fragment
->trailing
= trailing
;
928 /* If a fragment ends with an incomplete line, we failed to include
929 * it in the above loop because we hit oldlines == newlines == 0
932 if (12 < size
&& !memcmp(line
, "\\ ", 2))
933 offset
+= linelen(line
, size
);
935 patch
->lines_added
+= added
;
936 patch
->lines_deleted
+= deleted
;
940 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
942 unsigned long offset
= 0;
943 struct fragment
**fragp
= &patch
->fragments
;
945 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
946 struct fragment
*fragment
;
949 fragment
= xcalloc(1, sizeof(*fragment
));
950 len
= parse_fragment(line
, size
, patch
, fragment
);
952 die("corrupt patch at line %d", linenr
);
954 fragment
->patch
= line
;
955 fragment
->size
= len
;
958 fragp
= &fragment
->next
;
967 static inline int metadata_changes(struct patch
*patch
)
969 return patch
->is_rename
> 0 ||
970 patch
->is_copy
> 0 ||
973 (patch
->old_mode
&& patch
->new_mode
&&
974 patch
->old_mode
!= patch
->new_mode
);
977 static int parse_binary(char *buffer
, unsigned long size
, struct patch
*patch
)
979 /* We have read "GIT binary patch\n"; what follows is a line
980 * that says the patch method (currently, either "deflated
981 * literal" or "deflated delta") and the length of data before
982 * deflating; a sequence of 'length-byte' followed by base-85
983 * encoded data follows.
985 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
986 * and we would limit the patch line to 66 characters,
987 * so one line can fit up to 13 groups that would decode
988 * to 52 bytes max. The length byte 'A'-'Z' corresponds
989 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
990 * The end of binary is signalled with an empty line.
993 struct fragment
*fragment
;
996 patch
->fragments
= fragment
= xcalloc(1, sizeof(*fragment
));
998 /* Grab the type of patch */
999 llen
= linelen(buffer
, size
);
1003 if (!strncmp(buffer
, "delta ", 6)) {
1004 patch
->is_binary
= BINARY_DELTA_DEFLATED
;
1005 patch
->deflate_origlen
= strtoul(buffer
+ 6, NULL
, 10);
1007 else if (!strncmp(buffer
, "literal ", 8)) {
1008 patch
->is_binary
= BINARY_LITERAL_DEFLATED
;
1009 patch
->deflate_origlen
= strtoul(buffer
+ 8, NULL
, 10);
1012 return error("unrecognized binary patch at line %d: %.*s",
1013 linenr
-1, llen
-1, buffer
);
1016 int byte_length
, max_byte_length
, newsize
;
1017 llen
= linelen(buffer
, size
);
1022 /* Minimum line is "A00000\n" which is 7-byte long,
1023 * and the line length must be multiple of 5 plus 2.
1025 if ((llen
< 7) || (llen
-2) % 5)
1027 max_byte_length
= (llen
- 2) / 5 * 4;
1028 byte_length
= *buffer
;
1029 if ('A' <= byte_length
&& byte_length
<= 'Z')
1030 byte_length
= byte_length
- 'A' + 1;
1031 else if ('a' <= byte_length
&& byte_length
<= 'z')
1032 byte_length
= byte_length
- 'a' + 27;
1035 /* if the input length was not multiple of 4, we would
1036 * have filler at the end but the filler should never
1039 if (max_byte_length
< byte_length
||
1040 byte_length
<= max_byte_length
- 4)
1042 newsize
= fragment
->size
+ byte_length
;
1043 data
= xrealloc(data
, newsize
);
1044 if (decode_85(data
+ fragment
->size
,
1048 fragment
->size
= newsize
;
1052 fragment
->patch
= data
;
1055 return error("corrupt binary patch at line %d: %.*s",
1056 linenr
-1, llen
-1, buffer
);
1059 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
1061 int hdrsize
, patchsize
;
1062 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
1067 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
1070 static const char *binhdr
[] = {
1075 static const char git_binary
[] = "GIT binary patch\n";
1077 int hd
= hdrsize
+ offset
;
1078 unsigned long llen
= linelen(buffer
+ hd
, size
- hd
);
1080 if (llen
== sizeof(git_binary
) - 1 &&
1081 !memcmp(git_binary
, buffer
+ hd
, llen
)) {
1084 used
= parse_binary(buffer
+ hd
+ llen
,
1085 size
- hd
- llen
, patch
);
1087 patchsize
= used
+ llen
;
1091 else if (!memcmp(" differ\n", buffer
+ hd
+ llen
- 8, 8)) {
1092 for (i
= 0; binhdr
[i
]; i
++) {
1093 int len
= strlen(binhdr
[i
]);
1094 if (len
< size
- hd
&&
1095 !memcmp(binhdr
[i
], buffer
+ hd
, len
)) {
1097 patch
->is_binary
= 1;
1104 /* Empty patch cannot be applied if:
1105 * - it is a binary patch and we do not do binary_replace, or
1106 * - text patch without metadata change
1108 if ((apply
|| check
) &&
1110 ? !allow_binary_replacement
1111 : !metadata_changes(patch
)))
1112 die("patch with only garbage at line %d", linenr
);
1115 return offset
+ hdrsize
+ patchsize
;
1118 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1119 static const char minuses
[]= "----------------------------------------------------------------------";
1121 static void show_stats(struct patch
*patch
)
1123 const char *prefix
= "";
1124 char *name
= patch
->new_name
;
1126 int len
, max
, add
, del
, total
;
1129 name
= patch
->old_name
;
1131 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
1132 qname
= xmalloc(len
+ 1);
1133 quote_c_style(name
, qname
, NULL
, 0);
1138 * "scale" the filename
1149 slash
= strchr(name
, '/');
1156 * scale the add/delete
1162 add
= patch
->lines_added
;
1163 del
= patch
->lines_deleted
;
1166 if (max_change
> 0) {
1167 total
= (total
* max
+ max_change
/ 2) / max_change
;
1168 add
= (add
* max
+ max_change
/ 2) / max_change
;
1171 if (patch
->is_binary
)
1172 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
1174 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
1175 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
1176 add
, pluses
, del
, minuses
);
1181 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
1186 switch (st
->st_mode
& S_IFMT
) {
1188 return readlink(path
, buf
, size
);
1190 fd
= open(path
, O_RDONLY
);
1192 return error("unable to open %s", path
);
1195 int ret
= xread(fd
, buf
+ got
, size
- got
);
1208 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
, int *lines
)
1211 unsigned long start
, backwards
, forwards
;
1213 if (fragsize
> size
)
1218 unsigned long offset
= 0;
1220 while (offset
+ fragsize
<= size
) {
1221 if (buf
[offset
++] == '\n') {
1229 /* Exact line number? */
1230 if (!memcmp(buf
+ start
, fragment
, fragsize
))
1234 * There's probably some smart way to do this, but I'll leave
1235 * that to the smart and beautiful people. I'm simple and stupid.
1239 for (i
= 0; ; i
++) {
1246 if (forwards
+ fragsize
> size
)
1252 } while (backwards
&& buf
[backwards
-1] != '\n');
1255 while (forwards
+ fragsize
<= size
) {
1256 if (buf
[forwards
++] == '\n')
1262 if (try + fragsize
> size
)
1264 if (memcmp(buf
+ try, fragment
, fragsize
))
1274 * We should start searching forward and backward.
1279 static void remove_first_line(const char **rbuf
, int *rsize
)
1281 const char *buf
= *rbuf
;
1283 unsigned long offset
;
1285 while (offset
<= size
) {
1286 if (buf
[offset
++] == '\n')
1289 *rsize
= size
- offset
;
1290 *rbuf
= buf
+ offset
;
1293 static void remove_last_line(const char **rbuf
, int *rsize
)
1295 const char *buf
= *rbuf
;
1297 unsigned long offset
;
1299 while (offset
> 0) {
1300 if (buf
[--offset
] == '\n')
1303 *rsize
= offset
+ 1;
1306 struct buffer_desc
{
1309 unsigned long alloc
;
1312 static int apply_line(char *output
, const char *patch
, int plen
)
1314 /* plen is number of bytes to be copied from patch,
1315 * starting at patch+1 (patch[0] is '+'). Typically
1316 * patch[plen] is '\n'.
1318 int add_nl_to_tail
= 0;
1319 if ((new_whitespace
== strip_whitespace
) &&
1320 1 < plen
&& isspace(patch
[plen
-1])) {
1321 if (patch
[plen
] == '\n')
1324 while (0 < plen
&& isspace(patch
[plen
]))
1326 applied_after_stripping
++;
1328 memcpy(output
, patch
+ 1, plen
);
1330 output
[plen
++] = '\n';
1334 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
1336 char *buf
= desc
->buffer
;
1337 const char *patch
= frag
->patch
;
1338 int offset
, size
= frag
->size
;
1339 char *old
= xmalloc(size
);
1340 char *new = xmalloc(size
);
1341 const char *oldlines
, *newlines
;
1342 int oldsize
= 0, newsize
= 0;
1343 unsigned long leading
, trailing
;
1347 int len
= linelen(patch
, size
);
1354 * "plen" is how much of the line we should use for
1355 * the actual patch data. Normally we just remove the
1356 * first character on the line, but if the line is
1357 * followed by "\ No newline", then we also remove the
1358 * last one (which is the newline, of course).
1361 if (len
< size
&& patch
[len
] == '\\')
1366 memcpy(old
+ oldsize
, patch
+ 1, plen
);
1370 /* Fall-through for ' ' */
1372 if (*patch
!= '+' || !no_add
)
1373 newsize
+= apply_line(new + newsize
, patch
,
1376 case '@': case '\\':
1377 /* Ignore it, we already handled it */
1386 #ifdef NO_ACCURATE_DIFF
1387 if (oldsize
> 0 && old
[oldsize
- 1] == '\n' &&
1388 newsize
> 0 && new[newsize
- 1] == '\n') {
1396 leading
= frag
->leading
;
1397 trailing
= frag
->trailing
;
1401 offset
= find_offset(buf
, desc
->size
, oldlines
, oldsize
, pos
, &lines
);
1403 int diff
= newsize
- oldsize
;
1404 unsigned long size
= desc
->size
+ diff
;
1405 unsigned long alloc
= desc
->alloc
;
1407 /* Warn if it was necessary to reduce the number
1410 if ((leading
!= frag
->leading
) || (trailing
!= frag
->trailing
))
1411 fprintf(stderr
, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1412 leading
, trailing
, pos
+ lines
);
1415 alloc
= size
+ 8192;
1416 desc
->alloc
= alloc
;
1417 buf
= xrealloc(buf
, alloc
);
1421 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
1422 memcpy(buf
+ offset
, newlines
, newsize
);
1428 /* Am I at my context limits? */
1429 if ((leading
<= p_context
) && (trailing
<= p_context
))
1431 /* Reduce the number of context lines
1432 * Reduce both leading and trailing if they are equal
1433 * otherwise just reduce the larger context.
1435 if (leading
>= trailing
) {
1436 remove_first_line(&oldlines
, &oldsize
);
1437 remove_first_line(&newlines
, &newsize
);
1441 if (trailing
> leading
) {
1442 remove_last_line(&oldlines
, &oldsize
);
1443 remove_last_line(&newlines
, &newsize
);
1453 static char *inflate_it(const void *data
, unsigned long size
,
1454 unsigned long inflated_size
)
1460 memset(&stream
, 0, sizeof(stream
));
1462 stream
.next_in
= (unsigned char *)data
;
1463 stream
.avail_in
= size
;
1464 stream
.next_out
= out
= xmalloc(inflated_size
);
1465 stream
.avail_out
= inflated_size
;
1466 inflateInit(&stream
);
1467 st
= inflate(&stream
, Z_FINISH
);
1468 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= inflated_size
) {
1475 static int apply_binary_fragment(struct buffer_desc
*desc
, struct patch
*patch
)
1477 unsigned long dst_size
;
1478 struct fragment
*fragment
= patch
->fragments
;
1482 data
= inflate_it(fragment
->patch
, fragment
->size
,
1483 patch
->deflate_origlen
);
1485 return error("corrupt patch data");
1486 switch (patch
->is_binary
) {
1487 case BINARY_DELTA_DEFLATED
:
1488 result
= patch_delta(desc
->buffer
, desc
->size
,
1490 patch
->deflate_origlen
,
1493 desc
->buffer
= result
;
1496 case BINARY_LITERAL_DEFLATED
:
1498 desc
->buffer
= data
;
1499 dst_size
= patch
->deflate_origlen
;
1504 desc
->size
= desc
->alloc
= dst_size
;
1508 static int apply_binary(struct buffer_desc
*desc
, struct patch
*patch
)
1510 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1511 unsigned char sha1
[20];
1512 unsigned char hdr
[50];
1515 if (!allow_binary_replacement
)
1516 return error("cannot apply binary patch to '%s' "
1517 "without --allow-binary-replacement",
1520 /* For safety, we require patch index line to contain
1521 * full 40-byte textual SHA1 for old and new, at least for now.
1523 if (strlen(patch
->old_sha1_prefix
) != 40 ||
1524 strlen(patch
->new_sha1_prefix
) != 40 ||
1525 get_sha1_hex(patch
->old_sha1_prefix
, sha1
) ||
1526 get_sha1_hex(patch
->new_sha1_prefix
, sha1
))
1527 return error("cannot apply binary patch to '%s' "
1528 "without full index line", name
);
1530 if (patch
->old_name
) {
1531 /* See if the old one matches what the patch
1534 write_sha1_file_prepare(desc
->buffer
, desc
->size
,
1535 blob_type
, sha1
, hdr
, &hdrlen
);
1536 if (strcmp(sha1_to_hex(sha1
), patch
->old_sha1_prefix
))
1537 return error("the patch applies to '%s' (%s), "
1538 "which does not match the "
1539 "current contents.",
1540 name
, sha1_to_hex(sha1
));
1543 /* Otherwise, the old one must be empty. */
1545 return error("the patch applies to an empty "
1546 "'%s' but it is not empty", name
);
1549 get_sha1_hex(patch
->new_sha1_prefix
, sha1
);
1550 if (!memcmp(sha1
, null_sha1
, 20)) {
1552 desc
->alloc
= desc
->size
= 0;
1553 desc
->buffer
= NULL
;
1554 return 0; /* deletion patch */
1557 if (has_sha1_file(sha1
)) {
1558 /* We already have the postimage */
1563 desc
->buffer
= read_sha1_file(sha1
, type
, &size
);
1565 return error("the necessary postimage %s for "
1566 "'%s' cannot be read",
1567 patch
->new_sha1_prefix
, name
);
1568 desc
->alloc
= desc
->size
= size
;
1571 /* We have verified desc matches the preimage;
1572 * apply the patch data to it, which is stored
1573 * in the patch->fragments->{patch,size}.
1575 if (apply_binary_fragment(desc
, patch
))
1576 return error("binary patch does not apply to '%s'",
1579 /* verify that the result matches */
1580 write_sha1_file_prepare(desc
->buffer
, desc
->size
, blob_type
,
1581 sha1
, hdr
, &hdrlen
);
1582 if (strcmp(sha1_to_hex(sha1
), patch
->new_sha1_prefix
))
1583 return error("binary patch to '%s' creates incorrect result", name
);
1589 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
1591 struct fragment
*frag
= patch
->fragments
;
1592 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1594 if (patch
->is_binary
)
1595 return apply_binary(desc
, patch
);
1598 if (apply_one_fragment(desc
, frag
) < 0)
1599 return error("patch failed: %s:%ld",
1600 name
, frag
->oldpos
);
1606 static int apply_data(struct patch
*patch
, struct stat
*st
, struct cache_entry
*ce
)
1609 unsigned long size
, alloc
;
1610 struct buffer_desc desc
;
1618 buf
= read_sha1_file(ce
->sha1
, type
, &size
);
1620 return error("read of %s failed",
1625 else if (patch
->old_name
) {
1627 alloc
= size
+ 8192;
1628 buf
= xmalloc(alloc
);
1629 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
1630 return error("read of %s failed", patch
->old_name
);
1636 if (apply_fragments(&desc
, patch
) < 0)
1638 patch
->result
= desc
.buffer
;
1639 patch
->resultsize
= desc
.size
;
1641 if (patch
->is_delete
&& patch
->resultsize
)
1642 return error("removal patch leaves file contents");
1647 static int check_patch(struct patch
*patch
)
1650 const char *old_name
= patch
->old_name
;
1651 const char *new_name
= patch
->new_name
;
1652 const char *name
= old_name
? old_name
: new_name
;
1653 struct cache_entry
*ce
= NULL
;
1658 unsigned st_mode
= 0;
1661 stat_ret
= lstat(old_name
, &st
);
1663 int pos
= cache_name_pos(old_name
, strlen(old_name
));
1665 return error("%s: does not exist in index",
1667 ce
= active_cache
[pos
];
1669 struct checkout costate
;
1670 if (errno
!= ENOENT
)
1671 return error("%s: %s", old_name
,
1674 costate
.base_dir
= "";
1675 costate
.base_dir_len
= 0;
1678 costate
.not_new
= 0;
1679 costate
.refresh_cache
= 1;
1680 if (checkout_entry(ce
,
1683 lstat(old_name
, &st
))
1687 changed
= ce_match_stat(ce
, &st
, 1);
1689 return error("%s: does not match index",
1692 st_mode
= ntohl(ce
->ce_mode
);
1694 else if (stat_ret
< 0)
1695 return error("%s: %s", old_name
, strerror(errno
));
1698 st_mode
= ntohl(create_ce_mode(st
.st_mode
));
1700 if (patch
->is_new
< 0)
1702 if (!patch
->old_mode
)
1703 patch
->old_mode
= st_mode
;
1704 if ((st_mode
^ patch
->old_mode
) & S_IFMT
)
1705 return error("%s: wrong type", old_name
);
1706 if (st_mode
!= patch
->old_mode
)
1707 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
1708 old_name
, st_mode
, patch
->old_mode
);
1711 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
1712 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
1713 return error("%s: already exists in index", new_name
);
1715 if (!lstat(new_name
, &st
))
1716 return error("%s: already exists in working directory", new_name
);
1717 if (errno
!= ENOENT
)
1718 return error("%s: %s", new_name
, strerror(errno
));
1720 if (!patch
->new_mode
) {
1722 patch
->new_mode
= S_IFREG
| 0644;
1724 patch
->new_mode
= patch
->old_mode
;
1728 if (new_name
&& old_name
) {
1729 int same
= !strcmp(old_name
, new_name
);
1730 if (!patch
->new_mode
)
1731 patch
->new_mode
= patch
->old_mode
;
1732 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1733 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1734 patch
->new_mode
, new_name
, patch
->old_mode
,
1735 same
? "" : " of ", same
? "" : old_name
);
1738 if (apply_data(patch
, &st
, ce
) < 0)
1739 return error("%s: patch does not apply", name
);
1743 static int check_patch_list(struct patch
*patch
)
1747 for (;patch
; patch
= patch
->next
)
1748 error
|= check_patch(patch
);
1752 static inline int is_null_sha1(const unsigned char *sha1
)
1754 return !memcmp(sha1
, null_sha1
, 20);
1757 static void show_index_list(struct patch
*list
)
1759 struct patch
*patch
;
1761 /* Once we start supporting the reverse patch, it may be
1762 * worth showing the new sha1 prefix, but until then...
1764 for (patch
= list
; patch
; patch
= patch
->next
) {
1765 const unsigned char *sha1_ptr
;
1766 unsigned char sha1
[20];
1769 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1771 sha1_ptr
= null_sha1
;
1772 else if (get_sha1(patch
->old_sha1_prefix
, sha1
))
1773 die("sha1 information is lacking or useless (%s).",
1778 printf("%06o %s ",patch
->old_mode
, sha1_to_hex(sha1_ptr
));
1779 if (line_termination
&& quote_c_style(name
, NULL
, NULL
, 0))
1780 quote_c_style(name
, NULL
, stdout
, 0);
1782 fputs(name
, stdout
);
1783 putchar(line_termination
);
1787 static void stat_patch_list(struct patch
*patch
)
1789 int files
, adds
, dels
;
1791 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1793 adds
+= patch
->lines_added
;
1794 dels
+= patch
->lines_deleted
;
1798 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1801 static void numstat_patch_list(struct patch
*patch
)
1803 for ( ; patch
; patch
= patch
->next
) {
1805 name
= patch
->new_name
? patch
->new_name
: patch
->old_name
;
1806 printf("%d\t%d\t", patch
->lines_added
, patch
->lines_deleted
);
1807 if (line_termination
&& quote_c_style(name
, NULL
, NULL
, 0))
1808 quote_c_style(name
, NULL
, stdout
, 0);
1810 fputs(name
, stdout
);
1815 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
1818 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
1820 printf(" %s %s\n", newdelete
, name
);
1823 static void show_mode_change(struct patch
*p
, int show_name
)
1825 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
1827 printf(" mode change %06o => %06o %s\n",
1828 p
->old_mode
, p
->new_mode
, p
->new_name
);
1830 printf(" mode change %06o => %06o\n",
1831 p
->old_mode
, p
->new_mode
);
1835 static void show_rename_copy(struct patch
*p
)
1837 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
1838 const char *old
, *new;
1840 /* Find common prefix */
1844 const char *slash_old
, *slash_new
;
1845 slash_old
= strchr(old
, '/');
1846 slash_new
= strchr(new, '/');
1849 slash_old
- old
!= slash_new
- new ||
1850 memcmp(old
, new, slash_new
- new))
1852 old
= slash_old
+ 1;
1853 new = slash_new
+ 1;
1855 /* p->old_name thru old is the common prefix, and old and new
1856 * through the end of names are renames
1858 if (old
!= p
->old_name
)
1859 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
1860 (int)(old
- p
->old_name
), p
->old_name
,
1861 old
, new, p
->score
);
1863 printf(" %s %s => %s (%d%%)\n", renamecopy
,
1864 p
->old_name
, p
->new_name
, p
->score
);
1865 show_mode_change(p
, 0);
1868 static void summary_patch_list(struct patch
*patch
)
1872 for (p
= patch
; p
; p
= p
->next
) {
1874 show_file_mode_name("create", p
->new_mode
, p
->new_name
);
1875 else if (p
->is_delete
)
1876 show_file_mode_name("delete", p
->old_mode
, p
->old_name
);
1878 if (p
->is_rename
|| p
->is_copy
)
1879 show_rename_copy(p
);
1882 printf(" rewrite %s (%d%%)\n",
1883 p
->new_name
, p
->score
);
1884 show_mode_change(p
, 0);
1887 show_mode_change(p
, 1);
1893 static void patch_stats(struct patch
*patch
)
1895 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1897 if (lines
> max_change
)
1899 if (patch
->old_name
) {
1900 int len
= quote_c_style(patch
->old_name
, NULL
, NULL
, 0);
1902 len
= strlen(patch
->old_name
);
1906 if (patch
->new_name
) {
1907 int len
= quote_c_style(patch
->new_name
, NULL
, NULL
, 0);
1909 len
= strlen(patch
->new_name
);
1915 static void remove_file(struct patch
*patch
)
1918 if (remove_file_from_cache(patch
->old_name
) < 0)
1919 die("unable to remove %s from index", patch
->old_name
);
1922 unlink(patch
->old_name
);
1925 static void add_index_file(const char *path
, unsigned mode
, void *buf
, unsigned long size
)
1928 struct cache_entry
*ce
;
1929 int namelen
= strlen(path
);
1930 unsigned ce_size
= cache_entry_size(namelen
);
1935 ce
= xcalloc(1, ce_size
);
1936 memcpy(ce
->name
, path
, namelen
);
1937 ce
->ce_mode
= create_ce_mode(mode
);
1938 ce
->ce_flags
= htons(namelen
);
1940 if (lstat(path
, &st
) < 0)
1941 die("unable to stat newly created file %s", path
);
1942 fill_stat_cache_info(ce
, &st
);
1944 if (write_sha1_file(buf
, size
, blob_type
, ce
->sha1
) < 0)
1945 die("unable to create backing store for newly created file %s", path
);
1946 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
1947 die("unable to add cache entry for %s", path
);
1950 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
1955 return symlink(buf
, path
);
1956 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, (mode
& 0100) ? 0777 : 0666);
1960 int written
= xwrite(fd
, buf
, size
);
1962 die("writing file %s: %s", path
, strerror(errno
));
1964 die("out of space writing file %s", path
);
1969 die("closing file %s: %s", path
, strerror(errno
));
1974 * We optimistically assume that the directories exist,
1975 * which is true 99% of the time anyway. If they don't,
1976 * we create them and try again.
1978 static void create_one_file(char *path
, unsigned mode
, const char *buf
, unsigned long size
)
1982 if (!try_create_file(path
, mode
, buf
, size
))
1985 if (errno
== ENOENT
) {
1986 if (safe_create_leading_directories(path
))
1988 if (!try_create_file(path
, mode
, buf
, size
))
1992 if (errno
== EEXIST
) {
1993 unsigned int nr
= getpid();
1996 const char *newpath
;
1997 newpath
= mkpath("%s~%u", path
, nr
);
1998 if (!try_create_file(newpath
, mode
, buf
, size
)) {
1999 if (!rename(newpath
, path
))
2004 if (errno
!= EEXIST
)
2009 die("unable to write file %s mode %o", path
, mode
);
2012 static void create_file(struct patch
*patch
)
2014 char *path
= patch
->new_name
;
2015 unsigned mode
= patch
->new_mode
;
2016 unsigned long size
= patch
->resultsize
;
2017 char *buf
= patch
->result
;
2020 mode
= S_IFREG
| 0644;
2021 create_one_file(path
, mode
, buf
, size
);
2022 add_index_file(path
, mode
, buf
, size
);
2025 static void write_out_one_result(struct patch
*patch
)
2027 if (patch
->is_delete
> 0) {
2031 if (patch
->is_new
> 0 || patch
->is_copy
) {
2036 * Rename or modification boils down to the same
2037 * thing: remove the old, write the new
2043 static void write_out_results(struct patch
*list
, int skipped_patch
)
2045 if (!list
&& !skipped_patch
)
2049 write_out_one_result(list
);
2054 static struct cache_file cache_file
;
2056 static struct excludes
{
2057 struct excludes
*next
;
2061 static int use_patch(struct patch
*p
)
2063 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
2064 struct excludes
*x
= excludes
;
2066 if (fnmatch(x
->path
, pathname
, 0) == 0)
2070 if (0 < prefix_length
) {
2071 int pathlen
= strlen(pathname
);
2072 if (pathlen
<= prefix_length
||
2073 memcmp(prefix
, pathname
, prefix_length
))
2079 static int apply_patch(int fd
, const char *filename
)
2081 unsigned long offset
, size
;
2082 char *buffer
= read_patch_file(fd
, &size
);
2083 struct patch
*list
= NULL
, **listp
= &list
;
2084 int skipped_patch
= 0;
2086 patch_input_file
= filename
;
2091 struct patch
*patch
;
2094 patch
= xcalloc(1, sizeof(*patch
));
2095 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
2098 if (use_patch(patch
)) {
2101 listp
= &patch
->next
;
2103 /* perhaps free it a bit better? */
2111 if (whitespace_error
&& (new_whitespace
== error_on_whitespace
))
2114 write_index
= check_index
&& apply
;
2115 if (write_index
&& newfd
< 0)
2116 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
2118 if (read_cache() < 0)
2119 die("unable to read index file");
2122 if ((check
|| apply
) && check_patch_list(list
) < 0)
2126 write_out_results(list
, skipped_patch
);
2128 if (show_index_info
)
2129 show_index_list(list
);
2132 stat_patch_list(list
);
2135 numstat_patch_list(list
);
2138 summary_patch_list(list
);
2144 static int git_apply_config(const char *var
, const char *value
)
2146 if (!strcmp(var
, "apply.whitespace")) {
2147 apply_default_whitespace
= strdup(value
);
2150 return git_default_config(var
, value
);
2154 int main(int argc
, char **argv
)
2158 const char *whitespace_option
= NULL
;
2160 for (i
= 1; i
< argc
; i
++) {
2161 const char *arg
= argv
[i
];
2165 if (!strcmp(arg
, "-")) {
2166 apply_patch(0, "<stdin>");
2170 if (!strncmp(arg
, "--exclude=", 10)) {
2171 struct excludes
*x
= xmalloc(sizeof(*x
));
2177 if (!strncmp(arg
, "-p", 2)) {
2178 p_value
= atoi(arg
+ 2);
2181 if (!strcmp(arg
, "--no-add")) {
2185 if (!strcmp(arg
, "--stat")) {
2190 if (!strcmp(arg
, "--allow-binary-replacement") ||
2191 !strcmp(arg
, "--binary")) {
2192 allow_binary_replacement
= 1;
2195 if (!strcmp(arg
, "--numstat")) {
2200 if (!strcmp(arg
, "--summary")) {
2205 if (!strcmp(arg
, "--check")) {
2210 if (!strcmp(arg
, "--index")) {
2214 if (!strcmp(arg
, "--cached")) {
2219 if (!strcmp(arg
, "--apply")) {
2223 if (!strcmp(arg
, "--index-info")) {
2225 show_index_info
= 1;
2228 if (!strcmp(arg
, "-z")) {
2229 line_termination
= 0;
2232 if (!strncmp(arg
, "-C", 2)) {
2233 p_context
= strtoul(arg
+ 2, &end
, 0);
2235 die("unrecognized context count '%s'", arg
+ 2);
2238 if (!strncmp(arg
, "--whitespace=", 13)) {
2239 whitespace_option
= arg
+ 13;
2240 parse_whitespace_option(arg
+ 13);
2244 if (check_index
&& prefix_length
< 0) {
2245 prefix
= setup_git_directory();
2246 prefix_length
= prefix
? strlen(prefix
) : 0;
2247 git_config(git_apply_config
);
2248 if (!whitespace_option
&& apply_default_whitespace
)
2249 parse_whitespace_option(apply_default_whitespace
);
2251 if (0 < prefix_length
)
2252 arg
= prefix_filename(prefix
, prefix_length
, arg
);
2254 fd
= open(arg
, O_RDONLY
);
2258 set_default_whitespace_mode(whitespace_option
);
2259 apply_patch(fd
, arg
);
2262 set_default_whitespace_mode(whitespace_option
);
2264 apply_patch(0, "<stdin>");
2265 if (whitespace_error
) {
2266 if (squelch_whitespace_errors
&&
2267 squelch_whitespace_errors
< whitespace_error
) {
2269 whitespace_error
- squelch_whitespace_errors
;
2270 fprintf(stderr
, "warning: squelched %d whitespace error%s\n",
2272 squelched
== 1 ? "" : "s");
2274 if (new_whitespace
== error_on_whitespace
)
2275 die("%d line%s add%s trailing whitespaces.",
2277 whitespace_error
== 1 ? "" : "s",
2278 whitespace_error
== 1 ? "s" : "");
2279 if (applied_after_stripping
)
2280 fprintf(stderr
, "warning: %d line%s applied after"
2281 " stripping trailing whitespaces.\n",
2282 applied_after_stripping
,
2283 applied_after_stripping
== 1 ? "" : "s");
2284 else if (whitespace_error
)
2285 fprintf(stderr
, "warning: %d line%s add%s trailing"
2288 whitespace_error
== 1 ? "" : "s",
2289 whitespace_error
== 1 ? "s" : "");
2293 if (write_cache(newfd
, active_cache
, active_nr
) ||
2294 commit_index_file(&cache_file
))
2295 die("Unable to write new cachefile");