@@ -153,6 +153,7 @@ struct fragment {
153
153
const char * patch ;
154
154
int size ;
155
155
int rejected ;
156
+ int linenr ;
156
157
struct fragment * next ;
157
158
};
158
159
@@ -1227,23 +1228,29 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
1227
1228
return -1 ;
1228
1229
}
1229
1230
1230
- static void check_whitespace ( const char * line , int len , unsigned ws_rule )
1231
+ static void record_ws_error ( unsigned result , const char * line , int len , int linenr )
1231
1232
{
1232
1233
char * err ;
1233
- unsigned result = ws_check ( line + 1 , len - 1 , ws_rule );
1234
+
1234
1235
if (!result )
1235
1236
return ;
1236
1237
1237
1238
whitespace_error ++ ;
1238
1239
if (squelch_whitespace_errors &&
1239
1240
squelch_whitespace_errors < whitespace_error )
1240
- ;
1241
- else {
1242
- err = whitespace_error_string (result );
1243
- fprintf (stderr , "%s:%d: %s.\n%.*s\n" ,
1244
- patch_input_file , linenr , err , len - 2 , line + 1 );
1245
- free (err );
1246
- }
1241
+ return ;
1242
+
1243
+ err = whitespace_error_string (result );
1244
+ fprintf (stderr , "%s:%d: %s.\n%.*s\n" ,
1245
+ patch_input_file , linenr , err , len , line );
1246
+ free (err );
1247
+ }
1248
+
1249
+ static void check_whitespace (const char * line , int len , unsigned ws_rule )
1250
+ {
1251
+ unsigned result = ws_check (line + 1 , len - 1 , ws_rule );
1252
+
1253
+ record_ws_error (result , line + 1 , len - 2 , linenr );
1247
1254
}
1248
1255
1249
1256
/*
@@ -1359,6 +1366,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
1359
1366
int len ;
1360
1367
1361
1368
fragment = xcalloc (1 , sizeof (* fragment ));
1369
+ fragment -> linenr = linenr ;
1362
1370
len = parse_fragment (line , size , patch , fragment );
1363
1371
if (len <= 0 )
1364
1372
die ("corrupt patch at line %d" , linenr );
@@ -2142,6 +2150,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2142
2150
int len = linelen (patch , size );
2143
2151
int plen , added ;
2144
2152
int added_blank_line = 0 ;
2153
+ int is_blank_context = 0 ;
2145
2154
2146
2155
if (!len )
2147
2156
break ;
@@ -2174,8 +2183,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2174
2183
* new ++ = '\n' ;
2175
2184
add_line_info (& preimage , "\n" , 1 , LINE_COMMON );
2176
2185
add_line_info (& postimage , "\n" , 1 , LINE_COMMON );
2186
+ is_blank_context = 1 ;
2177
2187
break ;
2178
2188
case ' ' :
2189
+ if (plen && (ws_rule & WS_BLANK_AT_EOF ) &&
2190
+ ws_blank_line (patch + 1 , plen , ws_rule ))
2191
+ is_blank_context = 1 ;
2179
2192
case '-' :
2180
2193
memcpy (old , patch + 1 , plen );
2181
2194
add_line_info (& preimage , old , plen ,
@@ -2202,7 +2215,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2202
2215
(first == '+' ? 0 : LINE_COMMON ));
2203
2216
new += added ;
2204
2217
if (first == '+' &&
2205
- added == 1 && new [-1 ] == '\n' )
2218
+ (ws_rule & WS_BLANK_AT_EOF ) &&
2219
+ ws_blank_line (patch + 1 , plen , ws_rule ))
2206
2220
added_blank_line = 1 ;
2207
2221
break ;
2208
2222
case '@' : case '\\' :
@@ -2215,6 +2229,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2215
2229
}
2216
2230
if (added_blank_line )
2217
2231
new_blank_lines_at_end ++ ;
2232
+ else if (is_blank_context )
2233
+ ;
2218
2234
else
2219
2235
new_blank_lines_at_end = 0 ;
2220
2236
patch += len ;
@@ -2296,17 +2312,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2296
2312
}
2297
2313
2298
2314
if (applied_pos >= 0 ) {
2299
- if (ws_error_action == correct_ws_error &&
2300
- new_blank_lines_at_end &&
2301
- postimage .nr + applied_pos == img -> nr ) {
2315
+ if (new_blank_lines_at_end &&
2316
+ preimage .nr + applied_pos == img -> nr &&
2317
+ (ws_rule & WS_BLANK_AT_EOF ) &&
2318
+ ws_error_action != nowarn_ws_error ) {
2319
+ record_ws_error (WS_BLANK_AT_EOF , "+" , 1 , frag -> linenr );
2320
+ if (ws_error_action == correct_ws_error ) {
2321
+ while (new_blank_lines_at_end -- )
2322
+ remove_last_line (& postimage );
2323
+ }
2302
2324
/*
2303
- * If the patch application adds blank lines
2304
- * at the end, and if the patch applies at the
2305
- * end of the image, remove those added blank
2306
- * lines.
2325
+ * We would want to prevent write_out_results()
2326
+ * from taking place in apply_patch() that follows
2327
+ * the callchain led us here, which is:
2328
+ * apply_patch->check_patch_list->check_patch->
2329
+ * apply_data->apply_fragments->apply_one_fragment
2307
2330
*/
2308
- while ( new_blank_lines_at_end -- )
2309
- remove_last_line ( & postimage ) ;
2331
+ if ( ws_error_action == die_on_ws_error )
2332
+ apply = 0 ;
2310
2333
}
2311
2334
2312
2335
/*
0 commit comments