@@ -131,6 +131,7 @@ struct fragment {
131
131
const char * patch ;
132
132
int size ;
133
133
int rejected ;
134
+ int linenr ;
134
135
struct fragment * next ;
135
136
};
136
137
@@ -1149,23 +1150,29 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
1149
1150
return -1 ;
1150
1151
}
1151
1152
1152
- static void check_whitespace ( const char * line , int len , unsigned ws_rule )
1153
+ static void record_ws_error ( unsigned result , const char * line , int len , int linenr )
1153
1154
{
1154
1155
char * err ;
1155
- unsigned result = ws_check ( line + 1 , len - 1 , ws_rule );
1156
+
1156
1157
if (!result )
1157
1158
return ;
1158
1159
1159
1160
whitespace_error ++ ;
1160
1161
if (squelch_whitespace_errors &&
1161
1162
squelch_whitespace_errors < whitespace_error )
1162
- ;
1163
- else {
1164
- err = whitespace_error_string (result );
1165
- fprintf (stderr , "%s:%d: %s.\n%.*s\n" ,
1166
- patch_input_file , linenr , err , len - 2 , line + 1 );
1167
- free (err );
1168
- }
1163
+ return ;
1164
+
1165
+ err = whitespace_error_string (result );
1166
+ fprintf (stderr , "%s:%d: %s.\n%.*s\n" ,
1167
+ patch_input_file , linenr , err , len , line );
1168
+ free (err );
1169
+ }
1170
+
1171
+ static void check_whitespace (const char * line , int len , unsigned ws_rule )
1172
+ {
1173
+ unsigned result = ws_check (line + 1 , len - 1 , ws_rule );
1174
+
1175
+ record_ws_error (result , line + 1 , len - 2 , linenr );
1169
1176
}
1170
1177
1171
1178
/*
@@ -1281,6 +1288,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
1281
1288
int len ;
1282
1289
1283
1290
fragment = xcalloc (1 , sizeof (* fragment ));
1291
+ fragment -> linenr = linenr ;
1284
1292
len = parse_fragment (line , size , patch , fragment );
1285
1293
if (len <= 0 )
1286
1294
die ("corrupt patch at line %d" , linenr );
@@ -2005,6 +2013,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2005
2013
int len = linelen (patch , size );
2006
2014
int plen , added ;
2007
2015
int added_blank_line = 0 ;
2016
+ int is_blank_context = 0 ;
2008
2017
2009
2018
if (!len )
2010
2019
break ;
@@ -2037,8 +2046,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2037
2046
* new ++ = '\n' ;
2038
2047
add_line_info (& preimage , "\n" , 1 , LINE_COMMON );
2039
2048
add_line_info (& postimage , "\n" , 1 , LINE_COMMON );
2049
+ is_blank_context = 1 ;
2040
2050
break ;
2041
2051
case ' ' :
2052
+ if (plen && (ws_rule & WS_BLANK_AT_EOF ) &&
2053
+ ws_blank_line (patch + 1 , plen , ws_rule ))
2054
+ is_blank_context = 1 ;
2042
2055
case '-' :
2043
2056
memcpy (old , patch + 1 , plen );
2044
2057
add_line_info (& preimage , old , plen ,
@@ -2065,7 +2078,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2065
2078
(first == '+' ? 0 : LINE_COMMON ));
2066
2079
new += added ;
2067
2080
if (first == '+' &&
2068
- added == 1 && new [-1 ] == '\n' )
2081
+ (ws_rule & WS_BLANK_AT_EOF ) &&
2082
+ ws_blank_line (patch + 1 , plen , ws_rule ))
2069
2083
added_blank_line = 1 ;
2070
2084
break ;
2071
2085
case '@' : case '\\' :
@@ -2078,6 +2092,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2078
2092
}
2079
2093
if (added_blank_line )
2080
2094
new_blank_lines_at_end ++ ;
2095
+ else if (is_blank_context )
2096
+ ;
2081
2097
else
2082
2098
new_blank_lines_at_end = 0 ;
2083
2099
patch += len ;
@@ -2159,17 +2175,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2159
2175
}
2160
2176
2161
2177
if (applied_pos >= 0 ) {
2162
- if (ws_error_action == correct_ws_error &&
2163
- new_blank_lines_at_end &&
2164
- postimage .nr + applied_pos == img -> nr ) {
2178
+ if (new_blank_lines_at_end &&
2179
+ preimage .nr + applied_pos == img -> nr &&
2180
+ (ws_rule & WS_BLANK_AT_EOF ) &&
2181
+ ws_error_action != nowarn_ws_error ) {
2182
+ record_ws_error (WS_BLANK_AT_EOF , "+" , 1 , frag -> linenr );
2183
+ if (ws_error_action == correct_ws_error ) {
2184
+ while (new_blank_lines_at_end -- )
2185
+ remove_last_line (& postimage );
2186
+ }
2165
2187
/*
2166
- * If the patch application adds blank lines
2167
- * at the end, and if the patch applies at the
2168
- * end of the image, remove those added blank
2169
- * lines.
2188
+ * We would want to prevent write_out_results()
2189
+ * from taking place in apply_patch() that follows
2190
+ * the callchain led us here, which is:
2191
+ * apply_patch->check_patch_list->check_patch->
2192
+ * apply_data->apply_fragments->apply_one_fragment
2170
2193
*/
2171
- while ( new_blank_lines_at_end -- )
2172
- remove_last_line ( & postimage ) ;
2194
+ if ( ws_error_action == die_on_ws_error )
2195
+ apply = 0 ;
2173
2196
}
2174
2197
2175
2198
/*
0 commit comments