@@ -566,6 +566,10 @@ enum diff_symbol {
566
566
DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL ,
567
567
DIFF_SYMBOL_BINARY_DIFF_BODY ,
568
568
DIFF_SYMBOL_BINARY_DIFF_FOOTER ,
569
+ DIFF_SYMBOL_STATS_SUMMARY_NO_FILES ,
570
+ DIFF_SYMBOL_STATS_SUMMARY_ABBREV ,
571
+ DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,
572
+ DIFF_SYMBOL_STATS_LINE ,
569
573
DIFF_SYMBOL_SUBMODULE_ADD ,
570
574
DIFF_SYMBOL_SUBMODULE_DEL ,
571
575
DIFF_SYMBOL_SUBMODULE_UNTRACKED ,
@@ -629,6 +633,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
629
633
{
630
634
static const char * nneof = " No newline at end of file\n" ;
631
635
const char * context , * reset , * set , * meta , * fraginfo ;
636
+ struct strbuf sb = STRBUF_INIT ;
632
637
switch (s ) {
633
638
case DIFF_SYMBOL_NO_LF_EOF :
634
639
context = diff_get_color_opt (o , DIFF_CONTEXT );
@@ -640,6 +645,8 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
640
645
case DIFF_SYMBOL_SUBMODULE_HEADER :
641
646
case DIFF_SYMBOL_SUBMODULE_ERROR :
642
647
case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :
648
+ case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES :
649
+ case DIFF_SYMBOL_STATS_LINE :
643
650
case DIFF_SYMBOL_BINARY_DIFF_BODY :
644
651
case DIFF_SYMBOL_CONTEXT_FRAGINFO :
645
652
emit_line (o , "" , "" , line , len );
@@ -748,9 +755,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
748
755
fprintf (o -> file , "%sSubmodule %s contains modified content\n" ,
749
756
diff_line_prefix (o ), line );
750
757
break ;
758
+ case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES :
759
+ emit_line (o , "" , "" , " 0 files changed\n" ,
760
+ strlen (" 0 files changed\n" ));
761
+ break ;
762
+ case DIFF_SYMBOL_STATS_SUMMARY_ABBREV :
763
+ emit_line (o , "" , "" , " ...\n" , strlen (" ...\n" ));
764
+ break ;
751
765
default :
752
766
die ("BUG: unknown diff symbol" );
753
767
}
768
+ strbuf_release (& sb );
754
769
}
755
770
756
771
void diff_emit_submodule_del (struct diff_options * o , const char * line )
@@ -1705,20 +1720,14 @@ static int scale_linear(int it, int width, int max_change)
1705
1720
return 1 + (it * (width - 1 ) / max_change );
1706
1721
}
1707
1722
1708
- static void show_name (FILE * file ,
1709
- const char * prefix , const char * name , int len )
1710
- {
1711
- fprintf (file , " %s%-*s |" , prefix , len , name );
1712
- }
1713
-
1714
- static void show_graph (FILE * file , char ch , int cnt , const char * set , const char * reset )
1723
+ static void show_graph (struct strbuf * out , char ch , int cnt ,
1724
+ const char * set , const char * reset )
1715
1725
{
1716
1726
if (cnt <= 0 )
1717
1727
return ;
1718
- fprintf (file , "%s" , set );
1719
- while (cnt -- )
1720
- putc (ch , file );
1721
- fprintf (file , "%s" , reset );
1728
+ strbuf_addstr (out , set );
1729
+ strbuf_addchars (out , ch , cnt );
1730
+ strbuf_addstr (out , reset );
1722
1731
}
1723
1732
1724
1733
static void fill_print_name (struct diffstat_file * file )
@@ -1742,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file)
1742
1751
file -> print_name = pname ;
1743
1752
}
1744
1753
1745
- int print_stat_summary (FILE * fp , int files , int insertions , int deletions )
1754
+ static void print_stat_summary_inserts_deletes (struct diff_options * options ,
1755
+ int files , int insertions , int deletions )
1746
1756
{
1747
1757
struct strbuf sb = STRBUF_INIT ;
1748
- int ret ;
1749
1758
1750
1759
if (!files ) {
1751
1760
assert (insertions == 0 && deletions == 0 );
1752
- return fprintf (fp , "%s\n" , " 0 files changed" );
1761
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_SUMMARY_NO_FILES ,
1762
+ NULL , 0 , 0 );
1763
+ return ;
1753
1764
}
1754
1765
1755
1766
strbuf_addf (& sb ,
@@ -1776,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
1776
1787
deletions );
1777
1788
}
1778
1789
strbuf_addch (& sb , '\n' );
1779
- ret = fputs (sb .buf , fp );
1790
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,
1791
+ sb .buf , sb .len , 0 );
1780
1792
strbuf_release (& sb );
1781
- return ret ;
1793
+ }
1794
+
1795
+ void print_stat_summary (FILE * fp , int files ,
1796
+ int insertions , int deletions )
1797
+ {
1798
+ struct diff_options o ;
1799
+ memset (& o , 0 , sizeof (o ));
1800
+ o .file = fp ;
1801
+
1802
+ print_stat_summary_inserts_deletes (& o , files , insertions , deletions );
1782
1803
}
1783
1804
1784
1805
static void show_stats (struct diffstat_t * data , struct diff_options * options )
@@ -1788,13 +1809,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1788
1809
int total_files = data -> nr , count ;
1789
1810
int width , name_width , graph_width , number_width = 0 , bin_width = 0 ;
1790
1811
const char * reset , * add_c , * del_c ;
1791
- const char * line_prefix = "" ;
1792
1812
int extra_shown = 0 ;
1813
+ const char * line_prefix = diff_line_prefix (options );
1814
+ struct strbuf out = STRBUF_INIT ;
1793
1815
1794
1816
if (data -> nr == 0 )
1795
1817
return ;
1796
1818
1797
- line_prefix = diff_line_prefix (options );
1798
1819
count = options -> stat_count ? options -> stat_count : data -> nr ;
1799
1820
1800
1821
reset = diff_get_color_opt (options , DIFF_RESET );
@@ -1948,26 +1969,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1948
1969
}
1949
1970
1950
1971
if (file -> is_binary ) {
1951
- fprintf (options -> file , "%s" , line_prefix );
1952
- show_name (options -> file , prefix , name , len );
1953
- fprintf (options -> file , " %*s" , number_width , "Bin" );
1972
+ strbuf_addf (& out , " %s%-*s |" , prefix , len , name );
1973
+ strbuf_addf (& out , " %*s" , number_width , "Bin" );
1954
1974
if (!added && !deleted ) {
1955
- putc ('\n' , options -> file );
1975
+ strbuf_addch (& out , '\n' );
1976
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_LINE ,
1977
+ out .buf , out .len , 0 );
1978
+ strbuf_reset (& out );
1956
1979
continue ;
1957
1980
}
1958
- fprintf ( options -> file , " %s%" PRIuMAX "%s" ,
1981
+ strbuf_addf ( & out , " %s%" PRIuMAX "%s" ,
1959
1982
del_c , deleted , reset );
1960
- fprintf ( options -> file , " -> " );
1961
- fprintf ( options -> file , "%s%" PRIuMAX "%s" ,
1983
+ strbuf_addstr ( & out , " -> " );
1984
+ strbuf_addf ( & out , "%s%" PRIuMAX "%s" ,
1962
1985
add_c , added , reset );
1963
- fprintf (options -> file , " bytes" );
1964
- fprintf (options -> file , "\n" );
1986
+ strbuf_addstr (& out , " bytes\n" );
1987
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_LINE ,
1988
+ out .buf , out .len , 0 );
1989
+ strbuf_reset (& out );
1965
1990
continue ;
1966
1991
}
1967
1992
else if (file -> is_unmerged ) {
1968
- fprintf (options -> file , "%s" , line_prefix );
1969
- show_name (options -> file , prefix , name , len );
1970
- fprintf (options -> file , " Unmerged\n" );
1993
+ strbuf_addf (& out , " %s%-*s |" , prefix , len , name );
1994
+ strbuf_addstr (& out , " Unmerged\n" );
1995
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_LINE ,
1996
+ out .buf , out .len , 0 );
1997
+ strbuf_reset (& out );
1971
1998
continue ;
1972
1999
}
1973
2000
@@ -1990,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1990
2017
add = total - del ;
1991
2018
}
1992
2019
}
1993
- fprintf (options -> file , "%s" , line_prefix );
1994
- show_name (options -> file , prefix , name , len );
1995
- fprintf (options -> file , " %*" PRIuMAX "%s" ,
2020
+ strbuf_addf (& out , " %s%-*s |" , prefix , len , name );
2021
+ strbuf_addf (& out , " %*" PRIuMAX "%s" ,
1996
2022
number_width , added + deleted ,
1997
2023
added + deleted ? " " : "" );
1998
- show_graph (options -> file , '+' , add , add_c , reset );
1999
- show_graph (options -> file , '-' , del , del_c , reset );
2000
- fprintf (options -> file , "\n" );
2024
+ show_graph (& out , '+' , add , add_c , reset );
2025
+ show_graph (& out , '-' , del , del_c , reset );
2026
+ strbuf_addch (& out , '\n' );
2027
+ emit_diff_symbol (options , DIFF_SYMBOL_STATS_LINE ,
2028
+ out .buf , out .len , 0 );
2029
+ strbuf_reset (& out );
2001
2030
}
2002
2031
2003
2032
for (i = 0 ; i < data -> nr ; i ++ ) {
@@ -2018,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
2018
2047
if (i < count )
2019
2048
continue ;
2020
2049
if (!extra_shown )
2021
- fprintf (options -> file , "%s ...\n" , line_prefix );
2050
+ emit_diff_symbol (options ,
2051
+ DIFF_SYMBOL_STATS_SUMMARY_ABBREV ,
2052
+ NULL , 0 , 0 );
2022
2053
extra_shown = 1 ;
2023
2054
}
2024
- fprintf ( options -> file , "%s" , line_prefix );
2025
- print_stat_summary (options -> file , total_files , adds , dels );
2055
+
2056
+ print_stat_summary_inserts_deletes (options , total_files , adds , dels );
2026
2057
}
2027
2058
2028
2059
static void show_shortstats (struct diffstat_t * data , struct diff_options * options )
@@ -2034,7 +2065,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
2034
2065
2035
2066
for (i = 0 ; i < data -> nr ; i ++ ) {
2036
2067
int added = data -> files [i ]-> added ;
2037
- int deleted = data -> files [i ]-> deleted ;
2068
+ int deleted = data -> files [i ]-> deleted ;
2038
2069
2039
2070
if (data -> files [i ]-> is_unmerged ||
2040
2071
(!data -> files [i ]-> is_interesting && (added + deleted == 0 ))) {
@@ -2044,8 +2075,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
2044
2075
dels += deleted ;
2045
2076
}
2046
2077
}
2047
- fprintf (options -> file , "%s" , diff_line_prefix (options ));
2048
- print_stat_summary (options -> file , total_files , adds , dels );
2078
+ print_stat_summary_inserts_deletes (options , total_files , adds , dels );
2049
2079
}
2050
2080
2051
2081
static void show_numstat (struct diffstat_t * data , struct diff_options * options )
0 commit comments