@@ -385,26 +385,36 @@ static void print_line_count(FILE *file, int count)
385385 }
386386}
387387
388- static void copy_file_with_prefix (FILE * file ,
389- int prefix , const char * data , int size ,
390- const char * set , const char * reset )
391- {
392- int ch , nl_just_seen = 1 ;
393- while (0 < size -- ) {
394- ch = * data ++ ;
395- if (nl_just_seen ) {
396- fputs (set , file );
397- putc (prefix , file );
388+ static void emit_rewrite_lines (struct emit_callback * ecb ,
389+ int prefix , const char * data , int size )
390+ {
391+ const char * endp = NULL ;
392+ static const char * nneof = " No newline at end of file\n" ;
393+ const char * old = diff_get_color (ecb -> color_diff , DIFF_FILE_OLD );
394+ const char * reset = diff_get_color (ecb -> color_diff , DIFF_RESET );
395+
396+ while (0 < size ) {
397+ int len ;
398+
399+ endp = memchr (data , '\n' , size );
400+ len = endp ? (endp - data + 1 ) : size ;
401+ if (prefix != '+' ) {
402+ ecb -> lno_in_preimage ++ ;
403+ emit_line_0 (ecb -> file , old , reset , '-' ,
404+ data , len );
405+ } else {
406+ ecb -> lno_in_postimage ++ ;
407+ emit_add_line (reset , ecb , data , len );
398408 }
399- if (ch == '\n' ) {
400- nl_just_seen = 1 ;
401- fputs (reset , file );
402- } else
403- nl_just_seen = 0 ;
404- putc (ch , file );
409+ size -= len ;
410+ data += len ;
411+ }
412+ if (!endp ) {
413+ const char * plain = diff_get_color (ecb -> color_diff ,
414+ DIFF_PLAIN );
415+ emit_line_0 (ecb -> file , plain , reset , '\\' ,
416+ nneof , strlen (nneof ));
405417 }
406- if (!nl_just_seen )
407- fprintf (file , "%s\n\\ No newline at end of file\n" , reset );
408418}
409419
410420static void emit_rewrite_diff (const char * name_a ,
@@ -420,13 +430,12 @@ static void emit_rewrite_diff(const char *name_a,
420430 const char * name_a_tab , * name_b_tab ;
421431 const char * metainfo = diff_get_color (color_diff , DIFF_METAINFO );
422432 const char * fraginfo = diff_get_color (color_diff , DIFF_FRAGINFO );
423- const char * old = diff_get_color (color_diff , DIFF_FILE_OLD );
424- const char * new = diff_get_color (color_diff , DIFF_FILE_NEW );
425433 const char * reset = diff_get_color (color_diff , DIFF_RESET );
426434 static struct strbuf a_name = STRBUF_INIT , b_name = STRBUF_INIT ;
427435 const char * a_prefix , * b_prefix ;
428436 const char * data_one , * data_two ;
429437 size_t size_one , size_two ;
438+ struct emit_callback ecbdata ;
430439
431440 if (diff_mnemonic_prefix && DIFF_OPT_TST (o , REVERSE_DIFF )) {
432441 a_prefix = o -> b_prefix ;
@@ -467,6 +476,22 @@ static void emit_rewrite_diff(const char *name_a,
467476 size_two = two -> size ;
468477 }
469478
479+ memset (& ecbdata , 0 , sizeof (ecbdata ));
480+ ecbdata .color_diff = color_diff ;
481+ ecbdata .found_changesp = & o -> found_changes ;
482+ ecbdata .ws_rule = whitespace_rule (name_b ? name_b : name_a );
483+ ecbdata .file = o -> file ;
484+ if (ecbdata .ws_rule & WS_BLANK_AT_EOF ) {
485+ mmfile_t mf1 , mf2 ;
486+ mf1 .ptr = (char * )data_one ;
487+ mf2 .ptr = (char * )data_two ;
488+ mf1 .size = size_one ;
489+ mf2 .size = size_two ;
490+ check_blank_at_eof (& mf1 , & mf2 , & ecbdata );
491+ }
492+ ecbdata .lno_in_preimage = 1 ;
493+ ecbdata .lno_in_postimage = 1 ;
494+
470495 lc_a = count_lines (data_one , size_one );
471496 lc_b = count_lines (data_two , size_two );
472497 fprintf (o -> file ,
@@ -478,9 +503,9 @@ static void emit_rewrite_diff(const char *name_a,
478503 print_line_count (o -> file , lc_b );
479504 fprintf (o -> file , " @@%s\n" , reset );
480505 if (lc_a )
481- copy_file_with_prefix ( o -> file , '-' , data_one , size_one , old , reset );
506+ emit_rewrite_lines ( & ecbdata , '-' , data_one , size_one );
482507 if (lc_b )
483- copy_file_with_prefix ( o -> file , '+' , data_two , size_two , new , reset );
508+ emit_rewrite_lines ( & ecbdata , '+' , data_two , size_two );
484509}
485510
486511struct diff_words_buffer {
0 commit comments