@@ -35,14 +35,42 @@ static const char apply_usage[] =
3535"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] <patch>..." ;
3636
3737static enum whitespace_eol {
38- nowarn ,
38+ nowarn_whitespace ,
3939 warn_on_whitespace ,
4040 error_on_whitespace ,
41- strip_and_apply ,
42- } new_whitespace = nowarn ;
41+ strip_whitespace ,
42+ } new_whitespace = nowarn_whitespace ;
4343static int whitespace_error = 0 ;
44+ static int squelch_whitespace_errors = 5 ;
45+ static int applied_after_stripping = 0 ;
4446static const char * patch_input_file = NULL ;
4547
48+ static void parse_whitespace_option (const char * option )
49+ {
50+ if (!option ) {
51+ new_whitespace = nowarn_whitespace ;
52+ return ;
53+ }
54+ if (!strcmp (option , "warn" )) {
55+ new_whitespace = warn_on_whitespace ;
56+ return ;
57+ }
58+ if (!strcmp (option , "error" )) {
59+ new_whitespace = error_on_whitespace ;
60+ return ;
61+ }
62+ if (!strcmp (option , "error-all" )) {
63+ new_whitespace = error_on_whitespace ;
64+ squelch_whitespace_errors = 0 ;
65+ return ;
66+ }
67+ if (!strcmp (option , "strip" )) {
68+ new_whitespace = strip_whitespace ;
69+ return ;
70+ }
71+ die ("unrecognized whitespace option '%s'" , option );
72+ }
73+
4674/*
4775 * For "diff-stat" like behaviour, we keep track of the biggest change
4876 * we've seen, and the longest filename. That allows us to do simple
@@ -830,13 +858,18 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
830858 * That is, an addition of an empty line would check
831859 * the '+' here. Sneaky...
832860 */
833- if ((new_whitespace != nowarn ) &&
861+ if ((new_whitespace != nowarn_whitespace ) &&
834862 isspace (line [len - 2 ])) {
835- fprintf (stderr , "Added whitespace\n" );
836- fprintf (stderr , "%s:%d:%.*s\n" ,
837- patch_input_file ,
838- linenr , len - 2 , line + 1 );
839- whitespace_error = 1 ;
863+ whitespace_error ++ ;
864+ if (squelch_whitespace_errors &&
865+ squelch_whitespace_errors <
866+ whitespace_error )
867+ ;
868+ else {
869+ fprintf (stderr , "Adds trailing whitespace.\n%s:%d:%.*s\n" ,
870+ patch_input_file ,
871+ linenr , len - 2 , line + 1 );
872+ }
840873 }
841874 added ++ ;
842875 newlines -- ;
@@ -1122,13 +1155,14 @@ static int apply_line(char *output, const char *patch, int plen)
11221155 * patch[plen] is '\n'.
11231156 */
11241157 int add_nl_to_tail = 0 ;
1125- if ((new_whitespace == strip_and_apply ) &&
1158+ if ((new_whitespace == strip_whitespace ) &&
11261159 1 < plen && isspace (patch [plen - 1 ])) {
11271160 if (patch [plen ] == '\n' )
11281161 add_nl_to_tail = 1 ;
11291162 plen -- ;
11301163 while (0 < plen && isspace (patch [plen ]))
11311164 plen -- ;
1165+ applied_after_stripping ++ ;
11321166 }
11331167 memcpy (output , patch + 1 , plen );
11341168 if (add_nl_to_tail )
@@ -1816,10 +1850,21 @@ static int apply_patch(int fd, const char *filename)
18161850 return 0 ;
18171851}
18181852
1853+ static int git_apply_config (const char * var , const char * value )
1854+ {
1855+ if (!strcmp (var , "apply.whitespace" )) {
1856+ apply_default_whitespace = strdup (value );
1857+ return 0 ;
1858+ }
1859+ return git_default_config (var , value );
1860+ }
1861+
1862+
18191863int main (int argc , char * * argv )
18201864{
18211865 int i ;
18221866 int read_stdin = 1 ;
1867+ const char * whitespace_option = NULL ;
18231868
18241869 for (i = 1 ; i < argc ; i ++ ) {
18251870 const char * arg = argv [i ];
@@ -1887,25 +1932,17 @@ int main(int argc, char **argv)
18871932 continue ;
18881933 }
18891934 if (!strncmp (arg , "--whitespace=" , 13 )) {
1890- if (!strcmp (arg + 13 , "warn" )) {
1891- new_whitespace = warn_on_whitespace ;
1892- continue ;
1893- }
1894- if (!strcmp (arg + 13 , "error" )) {
1895- new_whitespace = error_on_whitespace ;
1896- continue ;
1897- }
1898- if (!strcmp (arg + 13 , "strip" )) {
1899- new_whitespace = strip_and_apply ;
1900- continue ;
1901- }
1902- die ("unrecognixed whitespace option '%s'" , arg + 13 );
1935+ whitespace_option = arg + 13 ;
1936+ parse_whitespace_option (arg + 13 );
1937+ continue ;
19031938 }
19041939
19051940 if (check_index && prefix_length < 0 ) {
19061941 prefix = setup_git_directory ();
19071942 prefix_length = prefix ? strlen (prefix ) : 0 ;
1908- git_config (git_default_config );
1943+ git_config (git_apply_config );
1944+ if (!whitespace_option && apply_default_whitespace )
1945+ parse_whitespace_option (apply_default_whitespace );
19091946 }
19101947 if (0 < prefix_length )
19111948 arg = prefix_filename (prefix , prefix_length , arg );
@@ -1919,7 +1956,31 @@ int main(int argc, char **argv)
19191956 }
19201957 if (read_stdin )
19211958 apply_patch (0 , "<stdin>" );
1922- if (whitespace_error && new_whitespace == error_on_whitespace )
1923- return 1 ;
1959+ if (whitespace_error ) {
1960+ if (squelch_whitespace_errors &&
1961+ squelch_whitespace_errors < whitespace_error ) {
1962+ int squelched =
1963+ whitespace_error - squelch_whitespace_errors ;
1964+ fprintf (stderr , "warning: squelched %d whitespace error%s\n" ,
1965+ squelched ,
1966+ squelched == 1 ? "" : "s" );
1967+ }
1968+ if (new_whitespace == error_on_whitespace )
1969+ die ("%d line%s add%s trailing whitespaces." ,
1970+ whitespace_error ,
1971+ whitespace_error == 1 ? "" : "s" ,
1972+ whitespace_error == 1 ? "s" : "" );
1973+ if (applied_after_stripping )
1974+ fprintf (stderr , "warning: %d line%s applied after"
1975+ " stripping trailing whitespaces.\n" ,
1976+ applied_after_stripping ,
1977+ applied_after_stripping == 1 ? "" : "s" );
1978+ else if (whitespace_error )
1979+ fprintf (stderr , "warning: %d line%s add%s trailing"
1980+ " whitespaces.\n" ,
1981+ whitespace_error ,
1982+ whitespace_error == 1 ? "" : "s" ,
1983+ whitespace_error == 1 ? "s" : "" );
1984+ }
19241985 return 0 ;
19251986}
0 commit comments