@@ -561,6 +561,11 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561
561
}
562
562
563
563
enum diff_symbol {
564
+ DIFF_SYMBOL_BINARY_DIFF_HEADER ,
565
+ DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA ,
566
+ DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL ,
567
+ DIFF_SYMBOL_BINARY_DIFF_BODY ,
568
+ DIFF_SYMBOL_BINARY_DIFF_FOOTER ,
564
569
DIFF_SYMBOL_SUBMODULE_ADD ,
565
570
DIFF_SYMBOL_SUBMODULE_DEL ,
566
571
DIFF_SYMBOL_SUBMODULE_UNTRACKED ,
@@ -635,6 +640,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
635
640
case DIFF_SYMBOL_SUBMODULE_HEADER :
636
641
case DIFF_SYMBOL_SUBMODULE_ERROR :
637
642
case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :
643
+ case DIFF_SYMBOL_BINARY_DIFF_BODY :
638
644
case DIFF_SYMBOL_CONTEXT_FRAGINFO :
639
645
emit_line (o , "" , "" , line , len );
640
646
break ;
@@ -706,6 +712,19 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
706
712
case DIFF_SYMBOL_HEADER :
707
713
fprintf (o -> file , "%s" , line );
708
714
break ;
715
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER :
716
+ fprintf (o -> file , "%sGIT binary patch\n" , diff_line_prefix (o ));
717
+ break ;
718
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA :
719
+ fprintf (o -> file , "%sdelta %s\n" , diff_line_prefix (o ), line );
720
+ break ;
721
+ case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL :
722
+ fprintf (o -> file , "%sliteral %s\n" , diff_line_prefix (o ), line );
723
+ break ;
724
+ case DIFF_SYMBOL_BINARY_DIFF_FOOTER :
725
+ fputs (diff_line_prefix (o ), o -> file );
726
+ fputc ('\n' , o -> file );
727
+ break ;
709
728
case DIFF_SYMBOL_REWRITE_DIFF :
710
729
fraginfo = diff_get_color (o -> use_color , DIFF_FRAGINFO );
711
730
reset = diff_get_color_opt (o , DIFF_RESET );
@@ -2390,8 +2409,8 @@ static unsigned char *deflate_it(char *data,
2390
2409
return deflated ;
2391
2410
}
2392
2411
2393
- static void emit_binary_diff_body (FILE * file , mmfile_t * one , mmfile_t * two ,
2394
- const char * prefix )
2412
+ static void emit_binary_diff_body (struct diff_options * o ,
2413
+ mmfile_t * one , mmfile_t * two )
2395
2414
{
2396
2415
void * cp ;
2397
2416
void * delta ;
@@ -2420,13 +2439,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
2420
2439
}
2421
2440
2422
2441
if (delta && delta_size < deflate_size ) {
2423
- fprintf (file , "%sdelta %lu\n" , prefix , orig_size );
2442
+ char * s = xstrfmt ("%lu" , orig_size );
2443
+ emit_diff_symbol (o , DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA ,
2444
+ s , strlen (s ), 0 );
2445
+ free (s );
2424
2446
free (deflated );
2425
2447
data = delta ;
2426
2448
data_size = delta_size ;
2427
- }
2428
- else {
2429
- fprintf (file , "%sliteral %lu\n" , prefix , two -> size );
2449
+ } else {
2450
+ char * s = xstrfmt ("%lu" , two -> size );
2451
+ emit_diff_symbol (o , DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL ,
2452
+ s , strlen (s ), 0 );
2453
+ free (s );
2430
2454
free (delta );
2431
2455
data = deflated ;
2432
2456
data_size = deflate_size ;
@@ -2435,29 +2459,34 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
2435
2459
/* emit data encoded in base85 */
2436
2460
cp = data ;
2437
2461
while (data_size ) {
2462
+ int len ;
2438
2463
int bytes = (52 < data_size ) ? 52 : data_size ;
2439
- char line [70 ];
2464
+ char line [71 ];
2440
2465
data_size -= bytes ;
2441
2466
if (bytes <= 26 )
2442
2467
line [0 ] = bytes + 'A' - 1 ;
2443
2468
else
2444
2469
line [0 ] = bytes - 26 + 'a' - 1 ;
2445
2470
encode_85 (line + 1 , cp , bytes );
2446
2471
cp = (char * ) cp + bytes ;
2447
- fprintf (file , "%s" , prefix );
2448
- fputs (line , file );
2449
- fputc ('\n' , file );
2472
+
2473
+ len = strlen (line );
2474
+ line [len ++ ] = '\n' ;
2475
+ line [len ] = '\0' ;
2476
+
2477
+ emit_diff_symbol (o , DIFF_SYMBOL_BINARY_DIFF_BODY ,
2478
+ line , len , 0 );
2450
2479
}
2451
- fprintf ( file , "%s\n" , prefix );
2480
+ emit_diff_symbol ( o , DIFF_SYMBOL_BINARY_DIFF_FOOTER , NULL , 0 , 0 );
2452
2481
free (data );
2453
2482
}
2454
2483
2455
- static void emit_binary_diff (FILE * file , mmfile_t * one , mmfile_t * two ,
2456
- const char * prefix )
2484
+ static void emit_binary_diff (struct diff_options * o ,
2485
+ mmfile_t * one , mmfile_t * two )
2457
2486
{
2458
- fprintf ( file , "%sGIT binary patch\n" , prefix );
2459
- emit_binary_diff_body (file , one , two , prefix );
2460
- emit_binary_diff_body (file , two , one , prefix );
2487
+ emit_diff_symbol ( o , DIFF_SYMBOL_BINARY_DIFF_HEADER , NULL , 0 , 0 );
2488
+ emit_binary_diff_body (o , one , two );
2489
+ emit_binary_diff_body (o , two , one );
2461
2490
}
2462
2491
2463
2492
int diff_filespec_is_binary (struct diff_filespec * one )
@@ -2643,7 +2672,7 @@ static void builtin_diff(const char *name_a,
2643
2672
emit_diff_symbol (o , DIFF_SYMBOL_HEADER , header .buf , header .len , 0 );
2644
2673
strbuf_reset (& header );
2645
2674
if (DIFF_OPT_TST (o , BINARY ))
2646
- emit_binary_diff (o -> file , & mf1 , & mf2 , line_prefix );
2675
+ emit_binary_diff (o , & mf1 , & mf2 );
2647
2676
else {
2648
2677
strbuf_addf (& sb , "%sBinary files %s and %s differ\n" ,
2649
2678
diff_line_prefix (o ), lbl [0 ], lbl [1 ]);
0 commit comments