@@ -144,6 +144,7 @@ struct patch {
144144 unsigned int old_mode , new_mode ;
145145 int is_new , is_delete ; /* -1 = unknown, 0 = false, 1 = true */
146146 int rejected ;
147+ unsigned ws_rule ;
147148 unsigned long deflate_origlen ;
148149 int lines_added , lines_deleted ;
149150 int score ;
@@ -898,7 +899,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
898899 return -1 ;
899900}
900901
901- static void check_whitespace (const char * line , int len )
902+ static void check_whitespace (const char * line , int len , unsigned ws_rule )
902903{
903904 const char * err = "Adds trailing whitespace" ;
904905 int seen_space = 0 ;
@@ -910,14 +911,14 @@ static void check_whitespace(const char *line, int len)
910911 * this function. That is, an addition of an empty line would
911912 * check the '+' here. Sneaky...
912913 */
913- if ((whitespace_rule & WS_TRAILING_SPACE ) && isspace (line [len - 2 ]))
914+ if ((ws_rule & WS_TRAILING_SPACE ) && isspace (line [len - 2 ]))
914915 goto error ;
915916
916917 /*
917918 * Make sure that there is no space followed by a tab in
918919 * indentation.
919920 */
920- if (whitespace_rule & WS_SPACE_BEFORE_TAB ) {
921+ if (ws_rule & WS_SPACE_BEFORE_TAB ) {
921922 err = "Space in indent is followed by a tab" ;
922923 for (i = 1 ; i < len ; i ++ ) {
923924 if (line [i ] == '\t' ) {
@@ -935,7 +936,7 @@ static void check_whitespace(const char *line, int len)
935936 * Make sure that the indentation does not contain more than
936937 * 8 spaces.
937938 */
938- if ((whitespace_rule & WS_INDENT_WITH_NON_TAB ) &&
939+ if ((ws_rule & WS_INDENT_WITH_NON_TAB ) &&
939940 (8 < len ) && !strncmp ("+ " , line , 9 )) {
940941 err = "Indent more than 8 places with spaces" ;
941942 goto error ;
@@ -1001,15 +1002,15 @@ static int parse_fragment(char *line, unsigned long size,
10011002 case '-' :
10021003 if (apply_in_reverse &&
10031004 ws_error_action != nowarn_ws_error )
1004- check_whitespace (line , len );
1005+ check_whitespace (line , len , patch -> ws_rule );
10051006 deleted ++ ;
10061007 oldlines -- ;
10071008 trailing = 0 ;
10081009 break ;
10091010 case '+' :
10101011 if (!apply_in_reverse &&
10111012 ws_error_action != nowarn_ws_error )
1012- check_whitespace (line , len );
1013+ check_whitespace (line , len , patch -> ws_rule );
10131014 added ++ ;
10141015 newlines -- ;
10151016 trailing = 0 ;
@@ -1318,6 +1319,10 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
13181319 if (offset < 0 )
13191320 return offset ;
13201321
1322+ patch -> ws_rule = whitespace_rule (patch -> new_name
1323+ ? patch -> new_name
1324+ : patch -> old_name );
1325+
13211326 patchsize = parse_single_patch (buffer + offset + hdrsize ,
13221327 size - offset - hdrsize , patch );
13231328
@@ -1568,7 +1573,8 @@ static void remove_last_line(const char **rbuf, int *rsize)
15681573 * rsize = offset + 1 ;
15691574}
15701575
1571- static int apply_line (char * output , const char * patch , int plen )
1576+ static int apply_line (char * output , const char * patch , int plen ,
1577+ unsigned ws_rule )
15721578{
15731579 /*
15741580 * plen is number of bytes to be copied from patch,
@@ -1593,7 +1599,7 @@ static int apply_line(char *output, const char *patch, int plen)
15931599 /*
15941600 * Strip trailing whitespace
15951601 */
1596- if ((whitespace_rule & WS_TRAILING_SPACE ) &&
1602+ if ((ws_rule & WS_TRAILING_SPACE ) &&
15971603 (1 < plen && isspace (patch [plen - 1 ]))) {
15981604 if (patch [plen ] == '\n' )
15991605 add_nl_to_tail = 1 ;
@@ -1610,12 +1616,12 @@ static int apply_line(char *output, const char *patch, int plen)
16101616 char ch = patch [i ];
16111617 if (ch == '\t' ) {
16121618 last_tab_in_indent = i ;
1613- if ((whitespace_rule & WS_SPACE_BEFORE_TAB ) &&
1619+ if ((ws_rule & WS_SPACE_BEFORE_TAB ) &&
16141620 0 <= last_space_in_indent )
16151621 need_fix_leading_space = 1 ;
16161622 } else if (ch == ' ' ) {
16171623 last_space_in_indent = i ;
1618- if ((whitespace_rule & WS_INDENT_WITH_NON_TAB ) &&
1624+ if ((ws_rule & WS_INDENT_WITH_NON_TAB ) &&
16191625 last_tab_in_indent < 0 &&
16201626 8 <= i )
16211627 need_fix_leading_space = 1 ;
@@ -1629,7 +1635,7 @@ static int apply_line(char *output, const char *patch, int plen)
16291635 int consecutive_spaces = 0 ;
16301636 int last = last_tab_in_indent + 1 ;
16311637
1632- if (whitespace_rule & WS_INDENT_WITH_NON_TAB ) {
1638+ if (ws_rule & WS_INDENT_WITH_NON_TAB ) {
16331639 /* have "last" point at one past the indent */
16341640 if (last_tab_in_indent < last_space_in_indent )
16351641 last = last_space_in_indent + 1 ;
@@ -1671,7 +1677,7 @@ static int apply_line(char *output, const char *patch, int plen)
16711677}
16721678
16731679static int apply_one_fragment (struct strbuf * buf , struct fragment * frag ,
1674- int inaccurate_eof )
1680+ int inaccurate_eof , unsigned ws_rule )
16751681{
16761682 int match_beginning , match_end ;
16771683 const char * patch = frag -> patch ;
@@ -1730,7 +1736,7 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
17301736 case '+' :
17311737 if (first != '+' || !no_add ) {
17321738 int added = apply_line (new + newsize , patch ,
1733- plen );
1739+ plen , ws_rule );
17341740 newsize += added ;
17351741 if (first == '+' &&
17361742 added == 1 && new [newsize - 1 ] == '\n' )
@@ -1953,12 +1959,14 @@ static int apply_fragments(struct strbuf *buf, struct patch *patch)
19531959{
19541960 struct fragment * frag = patch -> fragments ;
19551961 const char * name = patch -> old_name ? patch -> old_name : patch -> new_name ;
1962+ unsigned ws_rule = patch -> ws_rule ;
1963+ unsigned inaccurate_eof = patch -> inaccurate_eof ;
19561964
19571965 if (patch -> is_binary )
19581966 return apply_binary (buf , patch );
19591967
19601968 while (frag ) {
1961- if (apply_one_fragment (buf , frag , patch -> inaccurate_eof )) {
1969+ if (apply_one_fragment (buf , frag , inaccurate_eof , ws_rule )) {
19621970 error ("patch failed: %s:%ld" , name , frag -> oldpos );
19631971 if (!apply_with_reject )
19641972 return -1 ;
0 commit comments