@@ -35,16 +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 ;
4444static int squelch_whitespace_errors = 5 ;
4545static int applied_after_stripping = 0 ;
4646static const char * patch_input_file = NULL ;
4747
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+
4874/*
4975 * For "diff-stat" like behaviour, we keep track of the biggest change
5076 * we've seen, and the longest filename. That allows us to do simple
@@ -832,7 +858,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
832858 * That is, an addition of an empty line would check
833859 * the '+' here. Sneaky...
834860 */
835- if ((new_whitespace != nowarn ) &&
861+ if ((new_whitespace != nowarn_whitespace ) &&
836862 isspace (line [len - 2 ])) {
837863 whitespace_error ++ ;
838864 if (squelch_whitespace_errors &&
@@ -1129,7 +1155,7 @@ static int apply_line(char *output, const char *patch, int plen)
11291155 * patch[plen] is '\n'.
11301156 */
11311157 int add_nl_to_tail = 0 ;
1132- if ((new_whitespace == strip_and_apply ) &&
1158+ if ((new_whitespace == strip_whitespace ) &&
11331159 1 < plen && isspace (patch [plen - 1 ])) {
11341160 if (patch [plen ] == '\n' )
11351161 add_nl_to_tail = 1 ;
@@ -1824,10 +1850,21 @@ static int apply_patch(int fd, const char *filename)
18241850 return 0 ;
18251851}
18261852
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+
18271863int main (int argc , char * * argv )
18281864{
18291865 int i ;
18301866 int read_stdin = 1 ;
1867+ const char * whitespace_option = NULL ;
18311868
18321869 for (i = 1 ; i < argc ; i ++ ) {
18331870 const char * arg = argv [i ];
@@ -1895,30 +1932,17 @@ int main(int argc, char **argv)
18951932 continue ;
18961933 }
18971934 if (!strncmp (arg , "--whitespace=" , 13 )) {
1898- if (!strcmp (arg + 13 , "warn" )) {
1899- new_whitespace = warn_on_whitespace ;
1900- continue ;
1901- }
1902- if (!strcmp (arg + 13 , "error" )) {
1903- new_whitespace = error_on_whitespace ;
1904- continue ;
1905- }
1906- if (!strcmp (arg + 13 , "error-all" )) {
1907- new_whitespace = error_on_whitespace ;
1908- squelch_whitespace_errors = 0 ;
1909- continue ;
1910- }
1911- if (!strcmp (arg + 13 , "strip" )) {
1912- new_whitespace = strip_and_apply ;
1913- continue ;
1914- }
1915- die ("unrecognized whitespace option '%s'" , arg + 13 );
1935+ whitespace_option = arg + 13 ;
1936+ parse_whitespace_option (arg + 13 );
1937+ continue ;
19161938 }
19171939
19181940 if (check_index && prefix_length < 0 ) {
19191941 prefix = setup_git_directory ();
19201942 prefix_length = prefix ? strlen (prefix ) : 0 ;
1921- 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 );
19221946 }
19231947 if (0 < prefix_length )
19241948 arg = prefix_filename (prefix , prefix_length , arg );
0 commit comments