4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
11 #include "cache-tree.h"
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.
21 static const char *prefix
;
22 static int prefix_length
= -1;
24 static int p_value
= 1;
25 static int allow_binary_replacement
= 0;
26 static int check_index
= 0;
27 static int write_index
= 0;
28 static int diffstat
= 0;
29 static int numstat
= 0;
30 static int summary
= 0;
33 static int no_add
= 0;
34 static int show_index_info
= 0;
35 static int line_termination
= '\n';
36 static unsigned long p_context
= -1;
37 static const char apply_usage
[] =
38 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
40 static enum whitespace_eol
{
45 } new_whitespace
= warn_on_whitespace
;
46 static int whitespace_error
= 0;
47 static int squelch_whitespace_errors
= 5;
48 static int applied_after_stripping
= 0;
49 static const char *patch_input_file
= NULL
;
51 static void parse_whitespace_option(const char *option
)
54 new_whitespace
= warn_on_whitespace
;
57 if (!strcmp(option
, "warn")) {
58 new_whitespace
= warn_on_whitespace
;
61 if (!strcmp(option
, "nowarn")) {
62 new_whitespace
= nowarn_whitespace
;
65 if (!strcmp(option
, "error")) {
66 new_whitespace
= error_on_whitespace
;
69 if (!strcmp(option
, "error-all")) {
70 new_whitespace
= error_on_whitespace
;
71 squelch_whitespace_errors
= 0;
74 if (!strcmp(option
, "strip")) {
75 new_whitespace
= strip_whitespace
;
78 die("unrecognized whitespace option '%s'", option
);
81 static void set_default_whitespace_mode(const char *whitespace_option
)
83 if (!whitespace_option
&& !apply_default_whitespace
) {
84 new_whitespace
= (apply
91 * For "diff-stat" like behaviour, we keep track of the biggest change
92 * we've seen, and the longest filename. That allows us to do simple
95 static int max_change
, max_len
;
98 * Various "current state", notably line numbers and what
99 * file (and how) we're patching right now.. The "is_xxxx"
100 * things are flags, where -1 means "don't know yet".
102 static int linenr
= 1;
105 unsigned long leading
, trailing
;
106 unsigned long oldpos
, oldlines
;
107 unsigned long newpos
, newlines
;
110 struct fragment
*next
;
114 char *new_name
, *old_name
, *def_name
;
115 unsigned int old_mode
, new_mode
;
116 int is_rename
, is_copy
, is_new
, is_delete
, is_binary
;
117 int lines_added
, lines_deleted
;
119 struct fragment
*fragments
;
121 unsigned long resultsize
;
122 char old_sha1_prefix
[41];
123 char new_sha1_prefix
[41];
127 #define CHUNKSIZE (8192)
130 static void *read_patch_file(int fd
, unsigned long *sizep
)
132 unsigned long size
= 0, alloc
= CHUNKSIZE
;
133 void *buffer
= xmalloc(alloc
);
136 int nr
= alloc
- size
;
139 buffer
= xrealloc(buffer
, alloc
);
142 nr
= xread(fd
, buffer
+ size
, nr
);
146 die("git-apply: read returned %s", strerror(errno
));
152 * Make sure that we have some slop in the buffer
153 * so that we can do speculative "memcmp" etc, and
154 * see to it that it is NUL-filled.
156 if (alloc
< size
+ SLOP
)
157 buffer
= xrealloc(buffer
, size
+ SLOP
);
158 memset(buffer
+ size
, 0, SLOP
);
162 static unsigned long linelen(const char *buffer
, unsigned long size
)
164 unsigned long len
= 0;
167 if (*buffer
++ == '\n')
173 static int is_dev_null(const char *str
)
175 return !memcmp("/dev/null", str
, 9) && isspace(str
[9]);
181 static int name_terminate(const char *name
, int namelen
, int c
, int terminate
)
183 if (c
== ' ' && !(terminate
& TERM_SPACE
))
185 if (c
== '\t' && !(terminate
& TERM_TAB
))
191 static char * find_name(const char *line
, char *def
, int p_value
, int terminate
)
194 const char *start
= line
;
198 /* Proposed "new-style" GNU patch/diff format; see
199 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
201 name
= unquote_c_style(line
, NULL
);
205 cp
= strchr(name
, '/');
212 /* name can later be freed, so we need
213 * to memmove, not just return cp
215 memmove(name
, cp
, strlen(cp
) + 1);
232 if (name_terminate(start
, line
-start
, c
, terminate
))
236 if (c
== '/' && !--p_value
)
246 * Generally we prefer the shorter name, especially
247 * if the other one is just a variation of that with
248 * something else tacked on to the end (ie "file.orig"
252 int deflen
= strlen(def
);
253 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
257 name
= xmalloc(len
+ 1);
258 memcpy(name
, start
, len
);
265 * Get the name etc info from the --/+++ lines of a traditional patch header
267 * NOTE! This hardcodes "-p1" behaviour in filename detection.
269 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
270 * files, we can happily check the index for a match, but for creating a
271 * new file we should try to match whatever "patch" does. I have no idea.
273 static void parse_traditional_patch(const char *first
, const char *second
, struct patch
*patch
)
277 first
+= 4; // skip "--- "
278 second
+= 4; // skip "+++ "
279 if (is_dev_null(first
)) {
281 patch
->is_delete
= 0;
282 name
= find_name(second
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
283 patch
->new_name
= name
;
284 } else if (is_dev_null(second
)) {
286 patch
->is_delete
= 1;
287 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
288 patch
->old_name
= name
;
290 name
= find_name(first
, NULL
, p_value
, TERM_SPACE
| TERM_TAB
);
291 name
= find_name(second
, name
, p_value
, TERM_SPACE
| TERM_TAB
);
292 patch
->old_name
= patch
->new_name
= name
;
295 die("unable to find filename in patch at line %d", linenr
);
298 static int gitdiff_hdrend(const char *line
, struct patch
*patch
)
304 * We're anal about diff header consistency, to make
305 * sure that we don't end up having strange ambiguous
306 * patches floating around.
308 * As a result, gitdiff_{old|new}name() will check
309 * their names against any previous information, just
312 static char *gitdiff_verify_name(const char *line
, int isnull
, char *orig_name
, const char *oldnew
)
314 if (!orig_name
&& !isnull
)
315 return find_name(line
, NULL
, 1, 0);
324 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name
, linenr
);
325 another
= find_name(line
, NULL
, 1, 0);
326 if (!another
|| memcmp(another
, name
, len
))
327 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew
, linenr
);
332 /* expect "/dev/null" */
333 if (memcmp("/dev/null", line
, 9) || line
[9] != '\n')
334 die("git-apply: bad git-diff - expected /dev/null on line %d", linenr
);
339 static int gitdiff_oldname(const char *line
, struct patch
*patch
)
341 patch
->old_name
= gitdiff_verify_name(line
, patch
->is_new
, patch
->old_name
, "old");
345 static int gitdiff_newname(const char *line
, struct patch
*patch
)
347 patch
->new_name
= gitdiff_verify_name(line
, patch
->is_delete
, patch
->new_name
, "new");
351 static int gitdiff_oldmode(const char *line
, struct patch
*patch
)
353 patch
->old_mode
= strtoul(line
, NULL
, 8);
357 static int gitdiff_newmode(const char *line
, struct patch
*patch
)
359 patch
->new_mode
= strtoul(line
, NULL
, 8);
363 static int gitdiff_delete(const char *line
, struct patch
*patch
)
365 patch
->is_delete
= 1;
366 patch
->old_name
= patch
->def_name
;
367 return gitdiff_oldmode(line
, patch
);
370 static int gitdiff_newfile(const char *line
, struct patch
*patch
)
373 patch
->new_name
= patch
->def_name
;
374 return gitdiff_newmode(line
, patch
);
377 static int gitdiff_copysrc(const char *line
, struct patch
*patch
)
380 patch
->old_name
= find_name(line
, NULL
, 0, 0);
384 static int gitdiff_copydst(const char *line
, struct patch
*patch
)
387 patch
->new_name
= find_name(line
, NULL
, 0, 0);
391 static int gitdiff_renamesrc(const char *line
, struct patch
*patch
)
393 patch
->is_rename
= 1;
394 patch
->old_name
= find_name(line
, NULL
, 0, 0);
398 static int gitdiff_renamedst(const char *line
, struct patch
*patch
)
400 patch
->is_rename
= 1;
401 patch
->new_name
= find_name(line
, NULL
, 0, 0);
405 static int gitdiff_similarity(const char *line
, struct patch
*patch
)
407 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
412 static int gitdiff_dissimilarity(const char *line
, struct patch
*patch
)
414 if ((patch
->score
= strtoul(line
, NULL
, 10)) == ULONG_MAX
)
419 static int gitdiff_index(const char *line
, struct patch
*patch
)
421 /* index line is N hexadecimal, "..", N hexadecimal,
422 * and optional space with octal mode.
424 const char *ptr
, *eol
;
427 ptr
= strchr(line
, '.');
428 if (!ptr
|| ptr
[1] != '.' || 40 < ptr
- line
)
431 memcpy(patch
->old_sha1_prefix
, line
, len
);
432 patch
->old_sha1_prefix
[len
] = 0;
435 ptr
= strchr(line
, ' ');
436 eol
= strchr(line
, '\n');
438 if (!ptr
|| eol
< ptr
)
444 memcpy(patch
->new_sha1_prefix
, line
, len
);
445 patch
->new_sha1_prefix
[len
] = 0;
447 patch
->new_mode
= patch
->old_mode
= strtoul(ptr
+1, NULL
, 8);
452 * This is normal for a diff that doesn't change anything: we'll fall through
453 * into the next diff. Tell the parser to break out.
455 static int gitdiff_unrecognized(const char *line
, struct patch
*patch
)
460 static const char *stop_at_slash(const char *line
, int llen
)
464 for (i
= 0; i
< llen
; i
++) {
472 /* This is to extract the same name that appears on "diff --git"
473 * line. We do not find and return anything if it is a rename
474 * patch, and it is OK because we will find the name elsewhere.
475 * We need to reliably find name only when it is mode-change only,
476 * creation or deletion of an empty file. In any of these cases,
477 * both sides are the same name under a/ and b/ respectively.
479 static char *git_header_name(char *line
, int llen
)
483 const char *second
= NULL
;
485 line
+= strlen("diff --git ");
486 llen
-= strlen("diff --git ");
490 char *first
= unquote_c_style(line
, &second
);
494 /* advance to the first slash */
495 cp
= stop_at_slash(first
, strlen(first
));
496 if (!cp
|| cp
== first
) {
497 /* we do not accept absolute paths */
503 memmove(first
, cp
+1, len
+1); /* including NUL */
505 /* second points at one past closing dq of name.
506 * find the second name.
508 while ((second
< line
+ llen
) && isspace(*second
))
511 if (line
+ llen
<= second
)
512 goto free_first_and_fail
;
513 if (*second
== '"') {
514 char *sp
= unquote_c_style(second
, NULL
);
516 goto free_first_and_fail
;
517 cp
= stop_at_slash(sp
, strlen(sp
));
518 if (!cp
|| cp
== sp
) {
521 goto free_first_and_fail
;
523 /* They must match, otherwise ignore */
524 if (strcmp(cp
+1, first
))
525 goto free_both_and_fail
;
530 /* unquoted second */
531 cp
= stop_at_slash(second
, line
+ llen
- second
);
532 if (!cp
|| cp
== second
)
533 goto free_first_and_fail
;
535 if (line
+ llen
- cp
!= len
+ 1 ||
536 memcmp(first
, cp
, len
))
537 goto free_first_and_fail
;
541 /* unquoted first name */
542 name
= stop_at_slash(line
, llen
);
543 if (!name
|| name
== line
)
548 /* since the first name is unquoted, a dq if exists must be
549 * the beginning of the second name.
551 for (second
= name
; second
< line
+ llen
; second
++) {
552 if (*second
== '"') {
553 const char *cp
= second
;
555 char *sp
= unquote_c_style(second
, NULL
);
559 np
= stop_at_slash(sp
, strlen(sp
));
560 if (!np
|| np
== sp
) {
561 free_second_and_fail
:
567 if (len
< cp
- name
&&
568 !strncmp(np
, name
, len
) &&
569 isspace(name
[len
])) {
571 memmove(sp
, np
, len
+ 1);
574 goto free_second_and_fail
;
579 * Accept a name only if it shows up twice, exactly the same
582 for (len
= 0 ; ; len
++) {
599 if (second
[len
] == '\n' && !memcmp(name
, second
, len
)) {
600 char *ret
= xmalloc(len
+ 1);
601 memcpy(ret
, name
, len
);
610 /* Verify that we recognize the lines following a git header */
611 static int parse_git_header(char *line
, int len
, unsigned int size
, struct patch
*patch
)
613 unsigned long offset
;
615 /* A git diff has explicit new/delete information, so we don't guess */
617 patch
->is_delete
= 0;
620 * Some things may not have the old name in the
621 * rest of the headers anywhere (pure mode changes,
622 * or removing or adding empty files), so we get
623 * the default name from the header.
625 patch
->def_name
= git_header_name(line
, len
);
630 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
631 static const struct opentry
{
633 int (*fn
)(const char *, struct patch
*);
635 { "@@ -", gitdiff_hdrend
},
636 { "--- ", gitdiff_oldname
},
637 { "+++ ", gitdiff_newname
},
638 { "old mode ", gitdiff_oldmode
},
639 { "new mode ", gitdiff_newmode
},
640 { "deleted file mode ", gitdiff_delete
},
641 { "new file mode ", gitdiff_newfile
},
642 { "copy from ", gitdiff_copysrc
},
643 { "copy to ", gitdiff_copydst
},
644 { "rename old ", gitdiff_renamesrc
},
645 { "rename new ", gitdiff_renamedst
},
646 { "rename from ", gitdiff_renamesrc
},
647 { "rename to ", gitdiff_renamedst
},
648 { "similarity index ", gitdiff_similarity
},
649 { "dissimilarity index ", gitdiff_dissimilarity
},
650 { "index ", gitdiff_index
},
651 { "", gitdiff_unrecognized
},
655 len
= linelen(line
, size
);
656 if (!len
|| line
[len
-1] != '\n')
658 for (i
= 0; i
< ARRAY_SIZE(optable
); i
++) {
659 const struct opentry
*p
= optable
+ i
;
660 int oplen
= strlen(p
->str
);
661 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
663 if (p
->fn(line
+ oplen
, patch
) < 0)
672 static int parse_num(const char *line
, unsigned long *p
)
678 *p
= strtoul(line
, &ptr
, 10);
682 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
683 unsigned long *p1
, unsigned long *p2
)
687 if (offset
< 0 || offset
>= len
)
692 digits
= parse_num(line
, p1
);
702 digits
= parse_num(line
+1, p2
);
714 if (memcmp(line
, expect
, ex
))
721 * Parse a unified diff fragment header of the
722 * form "@@ -a,b +c,d @@"
724 static int parse_fragment_header(char *line
, int len
, struct fragment
*fragment
)
728 if (!len
|| line
[len
-1] != '\n')
731 /* Figure out the number of lines in a fragment */
732 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
733 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
738 static int find_header(char *line
, unsigned long size
, int *hdrsize
, struct patch
*patch
)
740 unsigned long offset
, len
;
742 patch
->is_rename
= patch
->is_copy
= 0;
743 patch
->is_new
= patch
->is_delete
= -1;
744 patch
->old_mode
= patch
->new_mode
= 0;
745 patch
->old_name
= patch
->new_name
= NULL
;
746 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
747 unsigned long nextlen
;
749 len
= linelen(line
, size
);
753 /* Testing this early allows us to take a few shortcuts.. */
758 * Make sure we don't find any unconnected patch fragmants.
759 * That's a sign that we didn't find a header, and that a
760 * patch has become corrupted/broken up.
762 if (!memcmp("@@ -", line
, 4)) {
763 struct fragment dummy
;
764 if (parse_fragment_header(line
, len
, &dummy
) < 0)
766 error("patch fragment without header at line %d: %.*s", linenr
, (int)len
-1, line
);
773 * Git patch? It might not have a real patch, just a rename
774 * or mode change, so we handle that specially
776 if (!memcmp("diff --git ", line
, 11)) {
777 int git_hdr_len
= parse_git_header(line
, len
, size
, patch
);
778 if (git_hdr_len
<= len
)
780 if (!patch
->old_name
&& !patch
->new_name
) {
781 if (!patch
->def_name
)
782 die("git diff header lacks filename information (line %d)", linenr
);
783 patch
->old_name
= patch
->new_name
= patch
->def_name
;
785 *hdrsize
= git_hdr_len
;
789 /** --- followed by +++ ? */
790 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
794 * We only accept unified patches, so we want it to
795 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
798 nextlen
= linelen(line
+ len
, size
- len
);
799 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
802 /* Ok, we'll consider it a patch */
803 parse_traditional_patch(line
, line
+len
, patch
);
804 *hdrsize
= len
+ nextlen
;
812 * Parse a unified diff. Note that this really needs
813 * to parse each fragment separately, since the only
814 * way to know the difference between a "---" that is
815 * part of a patch, and a "---" that starts the next
816 * patch is to look at the line counts..
818 static int parse_fragment(char *line
, unsigned long size
, struct patch
*patch
, struct fragment
*fragment
)
821 int len
= linelen(line
, size
), offset
;
822 unsigned long oldlines
, newlines
;
823 unsigned long leading
, trailing
;
825 offset
= parse_fragment_header(line
, len
, fragment
);
828 oldlines
= fragment
->oldlines
;
829 newlines
= fragment
->newlines
;
833 if (patch
->is_new
< 0) {
834 patch
->is_new
= !oldlines
;
836 patch
->old_name
= NULL
;
838 if (patch
->is_delete
< 0) {
839 patch
->is_delete
= !newlines
;
841 patch
->new_name
= NULL
;
844 if (patch
->is_new
&& oldlines
)
845 return error("new file depends on old contents");
846 if (patch
->is_delete
!= !newlines
) {
848 return error("deleted file still has contents");
849 fprintf(stderr
, "** warning: file %s becomes empty but is not deleted\n", patch
->new_name
);
852 /* Parse the thing.. */
857 for (offset
= len
; size
> 0; offset
+= len
, size
-= len
, line
+= len
, linenr
++) {
858 if (!oldlines
&& !newlines
)
860 len
= linelen(line
, size
);
861 if (!len
|| line
[len
-1] != '\n')
869 if (!deleted
&& !added
)
880 * We know len is at least two, since we have a '+' and
881 * we checked that the last character was a '\n' above.
882 * That is, an addition of an empty line would check
883 * the '+' here. Sneaky...
885 if ((new_whitespace
!= nowarn_whitespace
) &&
886 isspace(line
[len
-2])) {
888 if (squelch_whitespace_errors
&&
889 squelch_whitespace_errors
<
893 fprintf(stderr
, "Adds trailing whitespace.\n%s:%d:%.*s\n",
895 linenr
, len
-2, line
+1);
903 /* We allow "\ No newline at end of file". Depending
904 * on locale settings when the patch was produced we
905 * don't know what this line looks like. The only
906 * thing we do know is that it begins with "\ ".
907 * Checking for 12 is just for sanity check -- any
908 * l10n of "\ No newline..." is at least that long.
911 if (len
< 12 || memcmp(line
, "\\ ", 2))
916 if (oldlines
|| newlines
)
918 fragment
->leading
= leading
;
919 fragment
->trailing
= trailing
;
921 /* If a fragment ends with an incomplete line, we failed to include
922 * it in the above loop because we hit oldlines == newlines == 0
925 if (12 < size
&& !memcmp(line
, "\\ ", 2))
926 offset
+= linelen(line
, size
);
928 patch
->lines_added
+= added
;
929 patch
->lines_deleted
+= deleted
;
933 static int parse_single_patch(char *line
, unsigned long size
, struct patch
*patch
)
935 unsigned long offset
= 0;
936 struct fragment
**fragp
= &patch
->fragments
;
938 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
939 struct fragment
*fragment
;
942 fragment
= xcalloc(1, sizeof(*fragment
));
943 len
= parse_fragment(line
, size
, patch
, fragment
);
945 die("corrupt patch at line %d", linenr
);
947 fragment
->patch
= line
;
948 fragment
->size
= len
;
951 fragp
= &fragment
->next
;
960 static inline int metadata_changes(struct patch
*patch
)
962 return patch
->is_rename
> 0 ||
963 patch
->is_copy
> 0 ||
966 (patch
->old_mode
&& patch
->new_mode
&&
967 patch
->old_mode
!= patch
->new_mode
);
970 static int parse_chunk(char *buffer
, unsigned long size
, struct patch
*patch
)
972 int hdrsize
, patchsize
;
973 int offset
= find_header(buffer
, size
, &hdrsize
, patch
);
978 patchsize
= parse_single_patch(buffer
+ offset
+ hdrsize
, size
- offset
- hdrsize
, patch
);
981 static const char *binhdr
[] = {
987 int hd
= hdrsize
+ offset
;
988 unsigned long llen
= linelen(buffer
+ hd
, size
- hd
);
990 if (!memcmp(" differ\n", buffer
+ hd
+ llen
- 8, 8))
991 for (i
= 0; binhdr
[i
]; i
++) {
992 int len
= strlen(binhdr
[i
]);
993 if (len
< size
- hd
&&
994 !memcmp(binhdr
[i
], buffer
+ hd
, len
)) {
995 patch
->is_binary
= 1;
1000 /* Empty patch cannot be applied if:
1001 * - it is a binary patch and we do not do binary_replace, or
1002 * - text patch without metadata change
1004 if ((apply
|| check
) &&
1006 ? !allow_binary_replacement
1007 : !metadata_changes(patch
)))
1008 die("patch with only garbage at line %d", linenr
);
1011 return offset
+ hdrsize
+ patchsize
;
1014 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1015 static const char minuses
[]= "----------------------------------------------------------------------";
1017 static void show_stats(struct patch
*patch
)
1019 const char *prefix
= "";
1020 char *name
= patch
->new_name
;
1022 int len
, max
, add
, del
, total
;
1025 name
= patch
->old_name
;
1027 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
1028 qname
= xmalloc(len
+ 1);
1029 quote_c_style(name
, qname
, NULL
, 0);
1034 * "scale" the filename
1045 slash
= strchr(name
, '/');
1052 * scale the add/delete
1058 add
= patch
->lines_added
;
1059 del
= patch
->lines_deleted
;
1062 if (max_change
> 0) {
1063 total
= (total
* max
+ max_change
/ 2) / max_change
;
1064 add
= (add
* max
+ max_change
/ 2) / max_change
;
1067 if (patch
->is_binary
)
1068 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
1070 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
1071 len
, name
, patch
->lines_added
+ patch
->lines_deleted
,
1072 add
, pluses
, del
, minuses
);
1077 static int read_old_data(struct stat
*st
, const char *path
, void *buf
, unsigned long size
)
1082 switch (st
->st_mode
& S_IFMT
) {
1084 return readlink(path
, buf
, size
);
1086 fd
= open(path
, O_RDONLY
);
1088 return error("unable to open %s", path
);
1091 int ret
= xread(fd
, buf
+ got
, size
- got
);
1104 static int find_offset(const char *buf
, unsigned long size
, const char *fragment
, unsigned long fragsize
, int line
, int *lines
)
1107 unsigned long start
, backwards
, forwards
;
1109 if (fragsize
> size
)
1114 unsigned long offset
= 0;
1116 while (offset
+ fragsize
<= size
) {
1117 if (buf
[offset
++] == '\n') {
1125 /* Exact line number? */
1126 if (!memcmp(buf
+ start
, fragment
, fragsize
))
1130 * There's probably some smart way to do this, but I'll leave
1131 * that to the smart and beautiful people. I'm simple and stupid.
1135 for (i
= 0; ; i
++) {
1142 if (forwards
+ fragsize
> size
)
1148 } while (backwards
&& buf
[backwards
-1] != '\n');
1151 while (forwards
+ fragsize
<= size
) {
1152 if (buf
[forwards
++] == '\n')
1158 if (try + fragsize
> size
)
1160 if (memcmp(buf
+ try, fragment
, fragsize
))
1170 * We should start searching forward and backward.
1175 static void remove_first_line(const char **rbuf
, int *rsize
)
1177 const char *buf
= *rbuf
;
1179 unsigned long offset
;
1181 while (offset
<= size
) {
1182 if (buf
[offset
++] == '\n')
1185 *rsize
= size
- offset
;
1186 *rbuf
= buf
+ offset
;
1189 static void remove_last_line(const char **rbuf
, int *rsize
)
1191 const char *buf
= *rbuf
;
1193 unsigned long offset
;
1195 while (offset
> 0) {
1196 if (buf
[--offset
] == '\n')
1199 *rsize
= offset
+ 1;
1202 struct buffer_desc
{
1205 unsigned long alloc
;
1208 static int apply_line(char *output
, const char *patch
, int plen
)
1210 /* plen is number of bytes to be copied from patch,
1211 * starting at patch+1 (patch[0] is '+'). Typically
1212 * patch[plen] is '\n'.
1214 int add_nl_to_tail
= 0;
1215 if ((new_whitespace
== strip_whitespace
) &&
1216 1 < plen
&& isspace(patch
[plen
-1])) {
1217 if (patch
[plen
] == '\n')
1220 while (0 < plen
&& isspace(patch
[plen
]))
1222 applied_after_stripping
++;
1224 memcpy(output
, patch
+ 1, plen
);
1226 output
[plen
++] = '\n';
1230 static int apply_one_fragment(struct buffer_desc
*desc
, struct fragment
*frag
)
1232 char *buf
= desc
->buffer
;
1233 const char *patch
= frag
->patch
;
1234 int offset
, size
= frag
->size
;
1235 char *old
= xmalloc(size
);
1236 char *new = xmalloc(size
);
1237 const char *oldlines
, *newlines
;
1238 int oldsize
= 0, newsize
= 0;
1239 unsigned long leading
, trailing
;
1243 int len
= linelen(patch
, size
);
1250 * "plen" is how much of the line we should use for
1251 * the actual patch data. Normally we just remove the
1252 * first character on the line, but if the line is
1253 * followed by "\ No newline", then we also remove the
1254 * last one (which is the newline, of course).
1257 if (len
< size
&& patch
[len
] == '\\')
1262 memcpy(old
+ oldsize
, patch
+ 1, plen
);
1266 /* Fall-through for ' ' */
1268 if (*patch
!= '+' || !no_add
)
1269 newsize
+= apply_line(new + newsize
, patch
,
1272 case '@': case '\\':
1273 /* Ignore it, we already handled it */
1282 #ifdef NO_ACCURATE_DIFF
1283 if (oldsize
> 0 && old
[oldsize
- 1] == '\n' &&
1284 newsize
> 0 && new[newsize
- 1] == '\n') {
1292 leading
= frag
->leading
;
1293 trailing
= frag
->trailing
;
1297 offset
= find_offset(buf
, desc
->size
, oldlines
, oldsize
, pos
, &lines
);
1299 int diff
= newsize
- oldsize
;
1300 unsigned long size
= desc
->size
+ diff
;
1301 unsigned long alloc
= desc
->alloc
;
1303 /* Warn if it was necessary to reduce the number
1306 if ((leading
!= frag
->leading
) || (trailing
!= frag
->trailing
))
1307 fprintf(stderr
, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1308 leading
, trailing
, pos
+ lines
);
1311 alloc
= size
+ 8192;
1312 desc
->alloc
= alloc
;
1313 buf
= xrealloc(buf
, alloc
);
1317 memmove(buf
+ offset
+ newsize
, buf
+ offset
+ oldsize
, size
- offset
- newsize
);
1318 memcpy(buf
+ offset
, newlines
, newsize
);
1324 /* Am I at my context limits? */
1325 if ((leading
<= p_context
) && (trailing
<= p_context
))
1327 /* Reduce the number of context lines
1328 * Reduce both leading and trailing if they are equal
1329 * otherwise just reduce the larger context.
1331 if (leading
>= trailing
) {
1332 remove_first_line(&oldlines
, &oldsize
);
1333 remove_first_line(&newlines
, &newsize
);
1337 if (trailing
> leading
) {
1338 remove_last_line(&oldlines
, &oldsize
);
1339 remove_last_line(&newlines
, &newsize
);
1349 static int apply_fragments(struct buffer_desc
*desc
, struct patch
*patch
)
1351 struct fragment
*frag
= patch
->fragments
;
1352 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1354 if (patch
->is_binary
) {
1355 unsigned char sha1
[20];
1357 if (!allow_binary_replacement
)
1358 return error("cannot apply binary patch to '%s' "
1359 "without --allow-binary-replacement",
1362 /* For safety, we require patch index line to contain
1363 * full 40-byte textual SHA1 for old and new, at least for now.
1365 if (strlen(patch
->old_sha1_prefix
) != 40 ||
1366 strlen(patch
->new_sha1_prefix
) != 40 ||
1367 get_sha1_hex(patch
->old_sha1_prefix
, sha1
) ||
1368 get_sha1_hex(patch
->new_sha1_prefix
, sha1
))
1369 return error("cannot apply binary patch to '%s' "
1370 "without full index line", name
);
1372 if (patch
->old_name
) {
1373 unsigned char hdr
[50];
1376 /* See if the old one matches what the patch
1379 write_sha1_file_prepare(desc
->buffer
, desc
->size
,
1380 blob_type
, sha1
, hdr
, &hdrlen
);
1381 if (strcmp(sha1_to_hex(sha1
), patch
->old_sha1_prefix
))
1382 return error("the patch applies to '%s' (%s), "
1383 "which does not match the "
1384 "current contents.",
1385 name
, sha1_to_hex(sha1
));
1388 /* Otherwise, the old one must be empty. */
1390 return error("the patch applies to an empty "
1391 "'%s' but it is not empty", name
);
1394 /* For now, we do not record post-image data in the patch,
1395 * and require the object already present in the recipient's
1400 desc
->alloc
= desc
->size
= 0;
1402 get_sha1_hex(patch
->new_sha1_prefix
, sha1
);
1404 if (memcmp(sha1
, null_sha1
, 20)) {
1408 desc
->buffer
= read_sha1_file(sha1
, type
, &size
);
1410 return error("the necessary postimage %s for "
1411 "'%s' does not exist",
1412 patch
->new_sha1_prefix
, name
);
1413 desc
->alloc
= desc
->size
= size
;
1420 if (apply_one_fragment(desc
, frag
) < 0)
1421 return error("patch failed: %s:%ld",
1422 name
, frag
->oldpos
);
1428 static int apply_data(struct patch
*patch
, struct stat
*st
)
1431 unsigned long size
, alloc
;
1432 struct buffer_desc desc
;
1437 if (patch
->old_name
) {
1439 alloc
= size
+ 8192;
1440 buf
= xmalloc(alloc
);
1441 if (read_old_data(st
, patch
->old_name
, buf
, alloc
) != size
)
1442 return error("read of %s failed", patch
->old_name
);
1448 if (apply_fragments(&desc
, patch
) < 0)
1450 patch
->result
= desc
.buffer
;
1451 patch
->resultsize
= desc
.size
;
1453 if (patch
->is_delete
&& patch
->resultsize
)
1454 return error("removal patch leaves file contents");
1459 static int check_patch(struct patch
*patch
)
1462 const char *old_name
= patch
->old_name
;
1463 const char *new_name
= patch
->new_name
;
1464 const char *name
= old_name
? old_name
: new_name
;
1468 int stat_ret
= lstat(old_name
, &st
);
1471 int pos
= cache_name_pos(old_name
, strlen(old_name
));
1473 return error("%s: does not exist in index",
1476 struct checkout costate
;
1477 if (errno
!= ENOENT
)
1478 return error("%s: %s", old_name
,
1481 costate
.base_dir
= "";
1482 costate
.base_dir_len
= 0;
1485 costate
.not_new
= 0;
1486 costate
.refresh_cache
= 1;
1487 if (checkout_entry(active_cache
[pos
],
1490 lstat(old_name
, &st
))
1494 changed
= ce_match_stat(active_cache
[pos
], &st
, 1);
1496 return error("%s: does not match index",
1499 else if (stat_ret
< 0)
1500 return error("%s: %s", old_name
, strerror(errno
));
1502 if (patch
->is_new
< 0)
1504 st
.st_mode
= ntohl(create_ce_mode(st
.st_mode
));
1505 if (!patch
->old_mode
)
1506 patch
->old_mode
= st
.st_mode
;
1507 if ((st
.st_mode
^ patch
->old_mode
) & S_IFMT
)
1508 return error("%s: wrong type", old_name
);
1509 if (st
.st_mode
!= patch
->old_mode
)
1510 fprintf(stderr
, "warning: %s has type %o, expected %o\n",
1511 old_name
, st
.st_mode
, patch
->old_mode
);
1514 if (new_name
&& (patch
->is_new
| patch
->is_rename
| patch
->is_copy
)) {
1515 if (check_index
&& cache_name_pos(new_name
, strlen(new_name
)) >= 0)
1516 return error("%s: already exists in index", new_name
);
1517 if (!lstat(new_name
, &st
))
1518 return error("%s: already exists in working directory", new_name
);
1519 if (errno
!= ENOENT
)
1520 return error("%s: %s", new_name
, strerror(errno
));
1521 if (!patch
->new_mode
) {
1523 patch
->new_mode
= S_IFREG
| 0644;
1525 patch
->new_mode
= patch
->old_mode
;
1529 if (new_name
&& old_name
) {
1530 int same
= !strcmp(old_name
, new_name
);
1531 if (!patch
->new_mode
)
1532 patch
->new_mode
= patch
->old_mode
;
1533 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
)
1534 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1535 patch
->new_mode
, new_name
, patch
->old_mode
,
1536 same
? "" : " of ", same
? "" : old_name
);
1539 if (apply_data(patch
, &st
) < 0)
1540 return error("%s: patch does not apply", name
);
1544 static int check_patch_list(struct patch
*patch
)
1548 for (;patch
; patch
= patch
->next
)
1549 error
|= check_patch(patch
);
1553 static inline int is_null_sha1(const unsigned char *sha1
)
1555 return !memcmp(sha1
, null_sha1
, 20);
1558 static void show_index_list(struct patch
*list
)
1560 struct patch
*patch
;
1562 /* Once we start supporting the reverse patch, it may be
1563 * worth showing the new sha1 prefix, but until then...
1565 for (patch
= list
; patch
; patch
= patch
->next
) {
1566 const unsigned char *sha1_ptr
;
1567 unsigned char sha1
[20];
1570 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1572 sha1_ptr
= null_sha1
;
1573 else if (get_sha1(patch
->old_sha1_prefix
, sha1
))
1574 die("sha1 information is lacking or useless (%s).",
1579 printf("%06o %s ",patch
->old_mode
, sha1_to_hex(sha1_ptr
));
1580 if (line_termination
&& quote_c_style(name
, NULL
, NULL
, 0))
1581 quote_c_style(name
, NULL
, stdout
, 0);
1583 fputs(name
, stdout
);
1584 putchar(line_termination
);
1588 static void stat_patch_list(struct patch
*patch
)
1590 int files
, adds
, dels
;
1592 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
1594 adds
+= patch
->lines_added
;
1595 dels
+= patch
->lines_deleted
;
1599 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files
, adds
, dels
);
1602 static void numstat_patch_list(struct patch
*patch
)
1604 for ( ; patch
; patch
= patch
->next
) {
1606 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
1607 printf("%d\t%d\t", patch
->lines_added
, patch
->lines_deleted
);
1608 if (line_termination
&& quote_c_style(name
, NULL
, NULL
, 0))
1609 quote_c_style(name
, NULL
, stdout
, 0);
1611 fputs(name
, stdout
);
1616 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
1619 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
1621 printf(" %s %s\n", newdelete
, name
);
1624 static void show_mode_change(struct patch
*p
, int show_name
)
1626 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
1628 printf(" mode change %06o => %06o %s\n",
1629 p
->old_mode
, p
->new_mode
, p
->new_name
);
1631 printf(" mode change %06o => %06o\n",
1632 p
->old_mode
, p
->new_mode
);
1636 static void show_rename_copy(struct patch
*p
)
1638 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
1639 const char *old
, *new;
1641 /* Find common prefix */
1645 const char *slash_old
, *slash_new
;
1646 slash_old
= strchr(old
, '/');
1647 slash_new
= strchr(new, '/');
1650 slash_old
- old
!= slash_new
- new ||
1651 memcmp(old
, new, slash_new
- new))
1653 old
= slash_old
+ 1;
1654 new = slash_new
+ 1;
1656 /* p->old_name thru old is the common prefix, and old and new
1657 * through the end of names are renames
1659 if (old
!= p
->old_name
)
1660 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
1661 (int)(old
- p
->old_name
), p
->old_name
,
1662 old
, new, p
->score
);
1664 printf(" %s %s => %s (%d%%)\n", renamecopy
,
1665 p
->old_name
, p
->new_name
, p
->score
);
1666 show_mode_change(p
, 0);
1669 static void summary_patch_list(struct patch
*patch
)
1673 for (p
= patch
; p
; p
= p
->next
) {
1675 show_file_mode_name("create", p
->new_mode
, p
->new_name
);
1676 else if (p
->is_delete
)
1677 show_file_mode_name("delete", p
->old_mode
, p
->old_name
);
1679 if (p
->is_rename
|| p
->is_copy
)
1680 show_rename_copy(p
);
1683 printf(" rewrite %s (%d%%)\n",
1684 p
->new_name
, p
->score
);
1685 show_mode_change(p
, 0);
1688 show_mode_change(p
, 1);
1694 static void patch_stats(struct patch
*patch
)
1696 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
1698 if (lines
> max_change
)
1700 if (patch
->old_name
) {
1701 int len
= quote_c_style(patch
->old_name
, NULL
, NULL
, 0);
1703 len
= strlen(patch
->old_name
);
1707 if (patch
->new_name
) {
1708 int len
= quote_c_style(patch
->new_name
, NULL
, NULL
, 0);
1710 len
= strlen(patch
->new_name
);
1716 static void remove_file(struct patch
*patch
)
1719 if (remove_file_from_cache(patch
->old_name
) < 0)
1720 die("unable to remove %s from index", patch
->old_name
);
1721 cache_tree_invalidate_path(active_cache_tree
, patch
->old_name
);
1723 unlink(patch
->old_name
);
1726 static void add_index_file(const char *path
, unsigned mode
, void *buf
, unsigned long size
)
1729 struct cache_entry
*ce
;
1730 int namelen
= strlen(path
);
1731 unsigned ce_size
= cache_entry_size(namelen
);
1736 ce
= xcalloc(1, ce_size
);
1737 memcpy(ce
->name
, path
, namelen
);
1738 ce
->ce_mode
= create_ce_mode(mode
);
1739 ce
->ce_flags
= htons(namelen
);
1740 if (lstat(path
, &st
) < 0)
1741 die("unable to stat newly created file %s", path
);
1742 fill_stat_cache_info(ce
, &st
);
1743 if (write_sha1_file(buf
, size
, blob_type
, ce
->sha1
) < 0)
1744 die("unable to create backing store for newly created file %s", path
);
1745 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
1746 die("unable to add cache entry for %s", path
);
1749 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
1754 return symlink(buf
, path
);
1755 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, (mode
& 0100) ? 0777 : 0666);
1759 int written
= xwrite(fd
, buf
, size
);
1761 die("writing file %s: %s", path
, strerror(errno
));
1763 die("out of space writing file %s", path
);
1768 die("closing file %s: %s", path
, strerror(errno
));
1773 * We optimistically assume that the directories exist,
1774 * which is true 99% of the time anyway. If they don't,
1775 * we create them and try again.
1777 static void create_one_file(char *path
, unsigned mode
, const char *buf
, unsigned long size
)
1779 if (!try_create_file(path
, mode
, buf
, size
))
1782 if (errno
== ENOENT
) {
1783 if (safe_create_leading_directories(path
))
1785 if (!try_create_file(path
, mode
, buf
, size
))
1789 if (errno
== EEXIST
) {
1790 unsigned int nr
= getpid();
1793 const char *newpath
;
1794 newpath
= mkpath("%s~%u", path
, nr
);
1795 if (!try_create_file(newpath
, mode
, buf
, size
)) {
1796 if (!rename(newpath
, path
))
1801 if (errno
!= EEXIST
)
1806 die("unable to write file %s mode %o", path
, mode
);
1809 static void create_file(struct patch
*patch
)
1811 char *path
= patch
->new_name
;
1812 unsigned mode
= patch
->new_mode
;
1813 unsigned long size
= patch
->resultsize
;
1814 char *buf
= patch
->result
;
1817 mode
= S_IFREG
| 0644;
1818 create_one_file(path
, mode
, buf
, size
);
1819 add_index_file(path
, mode
, buf
, size
);
1820 cache_tree_invalidate_path(active_cache_tree
, path
);
1823 static void write_out_one_result(struct patch
*patch
)
1825 if (patch
->is_delete
> 0) {
1829 if (patch
->is_new
> 0 || patch
->is_copy
) {
1834 * Rename or modification boils down to the same
1835 * thing: remove the old, write the new
1841 static void write_out_results(struct patch
*list
, int skipped_patch
)
1843 if (!list
&& !skipped_patch
)
1847 write_out_one_result(list
);
1852 static struct cache_file cache_file
;
1854 static struct excludes
{
1855 struct excludes
*next
;
1859 static int use_patch(struct patch
*p
)
1861 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
1862 struct excludes
*x
= excludes
;
1864 if (fnmatch(x
->path
, pathname
, 0) == 0)
1868 if (0 < prefix_length
) {
1869 int pathlen
= strlen(pathname
);
1870 if (pathlen
<= prefix_length
||
1871 memcmp(prefix
, pathname
, prefix_length
))
1877 static int apply_patch(int fd
, const char *filename
)
1880 unsigned long offset
, size
;
1881 char *buffer
= read_patch_file(fd
, &size
);
1882 struct patch
*list
= NULL
, **listp
= &list
;
1883 int skipped_patch
= 0;
1885 patch_input_file
= filename
;
1890 struct patch
*patch
;
1893 patch
= xcalloc(1, sizeof(*patch
));
1894 nr
= parse_chunk(buffer
+ offset
, size
, patch
);
1897 if (use_patch(patch
)) {
1900 listp
= &patch
->next
;
1902 /* perhaps free it a bit better? */
1911 if (whitespace_error
&& (new_whitespace
== error_on_whitespace
))
1914 write_index
= check_index
&& apply
;
1916 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
1918 if (read_cache() < 0)
1919 die("unable to read index file");
1922 if ((check
|| apply
) && check_patch_list(list
) < 0)
1926 write_out_results(list
, skipped_patch
);
1929 if (write_cache(newfd
, active_cache
, active_nr
) ||
1930 commit_index_file(&cache_file
))
1931 die("Unable to write new cachefile");
1934 if (show_index_info
)
1935 show_index_list(list
);
1938 stat_patch_list(list
);
1941 numstat_patch_list(list
);
1944 summary_patch_list(list
);
1950 static int git_apply_config(const char *var
, const char *value
)
1952 if (!strcmp(var
, "apply.whitespace")) {
1953 apply_default_whitespace
= strdup(value
);
1956 return git_default_config(var
, value
);
1960 int main(int argc
, char **argv
)
1964 const char *whitespace_option
= NULL
;
1966 for (i
= 1; i
< argc
; i
++) {
1967 const char *arg
= argv
[i
];
1971 if (!strcmp(arg
, "-")) {
1972 apply_patch(0, "<stdin>");
1976 if (!strncmp(arg
, "--exclude=", 10)) {
1977 struct excludes
*x
= xmalloc(sizeof(*x
));
1983 if (!strncmp(arg
, "-p", 2)) {
1984 p_value
= atoi(arg
+ 2);
1987 if (!strcmp(arg
, "--no-add")) {
1991 if (!strcmp(arg
, "--stat")) {
1996 if (!strcmp(arg
, "--allow-binary-replacement")) {
1997 allow_binary_replacement
= 1;
2000 if (!strcmp(arg
, "--numstat")) {
2005 if (!strcmp(arg
, "--summary")) {
2010 if (!strcmp(arg
, "--check")) {
2015 if (!strcmp(arg
, "--index")) {
2019 if (!strcmp(arg
, "--apply")) {
2023 if (!strcmp(arg
, "--index-info")) {
2025 show_index_info
= 1;
2028 if (!strcmp(arg
, "-z")) {
2029 line_termination
= 0;
2032 if (!strncmp(arg
, "-C", 2)) {
2033 p_context
= strtoul(arg
+ 2, &end
, 0);
2035 die("unrecognized context count '%s'", arg
+ 2);
2038 if (!strncmp(arg
, "--whitespace=", 13)) {
2039 whitespace_option
= arg
+ 13;
2040 parse_whitespace_option(arg
+ 13);
2044 if (check_index
&& prefix_length
< 0) {
2045 prefix
= setup_git_directory();
2046 prefix_length
= prefix
? strlen(prefix
) : 0;
2047 git_config(git_apply_config
);
2048 if (!whitespace_option
&& apply_default_whitespace
)
2049 parse_whitespace_option(apply_default_whitespace
);
2051 if (0 < prefix_length
)
2052 arg
= prefix_filename(prefix
, prefix_length
, arg
);
2054 fd
= open(arg
, O_RDONLY
);
2058 set_default_whitespace_mode(whitespace_option
);
2059 apply_patch(fd
, arg
);
2062 set_default_whitespace_mode(whitespace_option
);
2064 apply_patch(0, "<stdin>");
2065 if (whitespace_error
) {
2066 if (squelch_whitespace_errors
&&
2067 squelch_whitespace_errors
< whitespace_error
) {
2069 whitespace_error
- squelch_whitespace_errors
;
2070 fprintf(stderr
, "warning: squelched %d whitespace error%s\n",
2072 squelched
== 1 ? "" : "s");
2074 if (new_whitespace
== error_on_whitespace
)
2075 die("%d line%s add%s trailing whitespaces.",
2077 whitespace_error
== 1 ? "" : "s",
2078 whitespace_error
== 1 ? "s" : "");
2079 if (applied_after_stripping
)
2080 fprintf(stderr
, "warning: %d line%s applied after"
2081 " stripping trailing whitespaces.\n",
2082 applied_after_stripping
,
2083 applied_after_stripping
== 1 ? "" : "s");
2084 else if (whitespace_error
)
2085 fprintf(stderr
, "warning: %d line%s add%s trailing"
2088 whitespace_error
== 1 ? "" : "s",
2089 whitespace_error
== 1 ? "s" : "");