@@ -624,42 +624,56 @@ static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
624
624
}
625
625
626
626
static void emit_line_0 (struct diff_options * o ,
627
- const char * set , unsigned reverse , const char * reset ,
627
+ const char * set_sign , const char * set , const char * reset ,
628
628
int first , const char * line , int len )
629
629
{
630
630
int has_trailing_newline , has_trailing_carriage_return ;
631
- int nofirst ;
631
+ int reverse = !!set && !!set_sign ;
632
+ int needs_reset = 0 ;
633
+
632
634
FILE * file = o -> file ;
633
635
634
- if (first )
635
- fputs (diff_line_prefix (o ), file );
636
- else if (!len )
637
- return ;
636
+ fputs (diff_line_prefix (o ), file );
638
637
639
- if (len == 0 ) {
640
- has_trailing_newline = (first == '\n' );
641
- has_trailing_carriage_return = (!has_trailing_newline &&
642
- (first == '\r' ));
643
- nofirst = has_trailing_newline || has_trailing_carriage_return ;
644
- } else {
645
- has_trailing_newline = (len > 0 && line [len - 1 ] == '\n' );
646
- if (has_trailing_newline )
647
- len -- ;
648
- has_trailing_carriage_return = (len > 0 && line [len - 1 ] == '\r' );
649
- if (has_trailing_carriage_return )
650
- len -- ;
651
- nofirst = 0 ;
638
+ has_trailing_newline = (len > 0 && line [len - 1 ] == '\n' );
639
+ if (has_trailing_newline )
640
+ len -- ;
641
+
642
+ has_trailing_carriage_return = (len > 0 && line [len - 1 ] == '\r' );
643
+ if (has_trailing_carriage_return )
644
+ len -- ;
645
+
646
+ if (!len && !first )
647
+ goto end_of_line ;
648
+
649
+ if (reverse && want_color (o -> use_color )) {
650
+ fputs (GIT_COLOR_REVERSE , file );
651
+ needs_reset = 1 ;
652
+ }
653
+
654
+ if (set_sign || set ) {
655
+ fputs (set_sign ? set_sign : set , file );
656
+ needs_reset = 1 ;
652
657
}
653
658
654
- if (len || !nofirst ) {
655
- if (reverse && want_color (o -> use_color ))
656
- fputs (GIT_COLOR_REVERSE , file );
659
+ if (first )
660
+ fputc (first , file );
661
+
662
+ if (!len )
663
+ goto end_of_line ;
664
+
665
+ if (set ) {
666
+ if (set_sign && set != set_sign )
667
+ fputs (reset , file );
657
668
fputs (set , file );
658
- if (first && !nofirst )
659
- fputc (first , file );
660
- fwrite (line , len , 1 , file );
661
- fputs (reset , file );
669
+ needs_reset = 1 ;
662
670
}
671
+ fwrite (line , len , 1 , file );
672
+ needs_reset |= len > 0 ;
673
+
674
+ end_of_line :
675
+ if (needs_reset )
676
+ fputs (reset , file );
663
677
if (has_trailing_carriage_return )
664
678
fputc ('\r' , file );
665
679
if (has_trailing_newline )
@@ -669,7 +683,7 @@ static void emit_line_0(struct diff_options *o,
669
683
static void emit_line (struct diff_options * o , const char * set , const char * reset ,
670
684
const char * line , int len )
671
685
{
672
- emit_line_0 (o , set , 0 , reset , line [ 0 ] , line + 1 , len - 1 );
686
+ emit_line_0 (o , set , NULL , reset , 0 , line , len );
673
687
}
674
688
675
689
enum diff_symbol {
@@ -1187,9 +1201,9 @@ static void dim_moved_lines(struct diff_options *o)
1187
1201
}
1188
1202
1189
1203
static void emit_line_ws_markup (struct diff_options * o ,
1190
- const char * set , const char * reset ,
1191
- const char * line , int len ,
1192
- const char * set_sign , char sign ,
1204
+ const char * set_sign , const char * set ,
1205
+ const char * reset ,
1206
+ char sign , const char * line , int len ,
1193
1207
unsigned ws_rule , int blank_at_eof )
1194
1208
{
1195
1209
const char * ws = NULL ;
@@ -1201,19 +1215,15 @@ static void emit_line_ws_markup(struct diff_options *o,
1201
1215
}
1202
1216
1203
1217
if (!ws && !set_sign )
1204
- emit_line_0 (o , set , 0 , reset , sign , line , len );
1218
+ emit_line_0 (o , set , NULL , reset , sign , line , len );
1205
1219
else if (!ws ) {
1206
- /* Emit just the prefix, then the rest. */
1207
- emit_line_0 (o , set_sign ? set_sign : set , !!set_sign , reset ,
1208
- sign , "" , 0 );
1209
- emit_line_0 (o , set , 0 , reset , 0 , line , len );
1220
+ emit_line_0 (o , set_sign , set , reset , sign , line , len );
1210
1221
} else if (blank_at_eof )
1211
1222
/* Blank line at EOF - paint '+' as well */
1212
- emit_line_0 (o , ws , 0 , reset , sign , line , len );
1223
+ emit_line_0 (o , ws , NULL , reset , sign , line , len );
1213
1224
else {
1214
1225
/* Emit just the prefix, then the rest. */
1215
- emit_line_0 (o , set_sign ? set_sign : set , !!set_sign , reset ,
1216
- sign , "" , 0 );
1226
+ emit_line_0 (o , set_sign , set , reset , sign , "" , 0 );
1217
1227
ws_check_emit (line , len , ws_rule ,
1218
1228
o -> file , set , reset , ws );
1219
1229
}
@@ -1236,7 +1246,7 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
1236
1246
context = diff_get_color_opt (o , DIFF_CONTEXT );
1237
1247
reset = diff_get_color_opt (o , DIFF_RESET );
1238
1248
putc ('\n' , o -> file );
1239
- emit_line_0 (o , context , 0 , reset , '\\' ,
1249
+ emit_line_0 (o , context , NULL , reset , '\\' ,
1240
1250
nneof , strlen (nneof ));
1241
1251
break ;
1242
1252
case DIFF_SYMBOL_SUBMODULE_HEADER :
@@ -1274,7 +1284,7 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
1274
1284
else if (c == '-' )
1275
1285
set = diff_get_color_opt (o , DIFF_FILE_OLD );
1276
1286
}
1277
- emit_line_ws_markup (o , set , reset , line , len , set_sign , ' ' ,
1287
+ emit_line_ws_markup (o , set_sign , set , reset , ' ' , line , len ,
1278
1288
flags & (DIFF_SYMBOL_CONTENT_WS_MASK ), 0 );
1279
1289
break ;
1280
1290
case DIFF_SYMBOL_PLUS :
@@ -1317,7 +1327,7 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
1317
1327
set = diff_get_color_opt (o , DIFF_CONTEXT_BOLD );
1318
1328
flags |= WS_IGNORE_FIRST_SPACE ;
1319
1329
}
1320
- emit_line_ws_markup (o , set , reset , line , len , set_sign , '+' ,
1330
+ emit_line_ws_markup (o , set_sign , set , reset , '+' , line , len ,
1321
1331
flags & DIFF_SYMBOL_CONTENT_WS_MASK ,
1322
1332
flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF );
1323
1333
break ;
@@ -1360,7 +1370,7 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
1360
1370
else
1361
1371
set = diff_get_color_opt (o , DIFF_CONTEXT_DIM );
1362
1372
}
1363
- emit_line_ws_markup (o , set , reset , line , len , set_sign , '-' ,
1373
+ emit_line_ws_markup (o , set_sign , set , reset , '-' , line , len ,
1364
1374
flags & DIFF_SYMBOL_CONTENT_WS_MASK , 0 );
1365
1375
break ;
1366
1376
case DIFF_SYMBOL_WORDS_PORCELAIN :
0 commit comments