diff.c: convert show_stats to use emit_diff_symbol
We call print_stat_summary from builtin/apply, so we still need the version with a file pointer, so introduce print_stat_summary_0 that uses emit_string machinery and keep print_stat_summary with the same arguments around. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Jun 29, 2017 at 17:07 UTC
0911c475c86862735caab43099ddb787d2e72f65
2 files changed
+74
-44
diff.c
+72
-42
@@ -566,6 +566,10 @@ enum diff_symbol {
566
DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
567
DIFF_SYMBOL_BINARY_DIFF_BODY,
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,
573
DIFF_SYMBOL_SUBMODULE_ADD,
574
DIFF_SYMBOL_SUBMODULE_DEL,
575
DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -629,6 +633,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
633
{
634
static const char *nneof = " No newline at end of file\n";
635
const char *context, *reset, *set, *meta, *fraginfo;
636
+ struct strbuf sb = STRBUF_INIT;
637
switch (s) {
638
case DIFF_SYMBOL_NO_LF_EOF:
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,
645
case DIFF_SYMBOL_SUBMODULE_HEADER:
646
case DIFF_SYMBOL_SUBMODULE_ERROR:
647
case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
648
+ case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
649
+ case DIFF_SYMBOL_STATS_LINE:
650
case DIFF_SYMBOL_BINARY_DIFF_BODY:
651
case DIFF_SYMBOL_CONTEXT_FRAGINFO:
652
emit_line(o, "", "", line, len);
@@ -748,9 +755,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
755
fprintf(o->file, "%sSubmodule %s contains modified content\n",
756
diff_line_prefix(o), line);
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;
765
default:
766
die("BUG: unknown diff symbol");
767
}
768
+ strbuf_release(&sb);
769
}
770
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)
1720
return 1 + (it * (width - 1) / max_change);
1721
}
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)
1725
{
1726
if (cnt <= 0)
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);
1731
}
1732
1733
static void fill_print_name(struct diffstat_file *file)
@@ -1742,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file)
1751
file->print_name = pname;
1752
}
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)
1756
{
1757
struct strbuf sb = STRBUF_INIT;
1748
- int ret;
1758
1759
if (!files) {
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;
1764
}
1765
1766
strbuf_addf(&sb,
@@ -1776,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
1787
deletions);
1788
}
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);
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);
1803
}
1804
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)
1809
int total_files = data->nr, count;
1810
int width, name_width, graph_width, number_width = 0, bin_width = 0;
1811
const char *reset, *add_c, *del_c;
1791
- const char *line_prefix = "";
1812
int extra_shown = 0;
1813
+ const char *line_prefix = diff_line_prefix(options);
1814
+ struct strbuf out = STRBUF_INIT;
1815
1816
if (data->nr == 0)
1817
return;
1818
1797
- line_prefix = diff_line_prefix(options);
1819
count = options->stat_count ? options->stat_count : data->nr;
1820
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)
1969
}
1970
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");
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);
1979
continue;
1980
}
1958
- fprintf(options->file, " %s%"PRIuMAX"%s",
1981
+ strbuf_addf(&out, " %s%"PRIuMAX"%s",
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",
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);
1990
continue;
1991
}
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);
1998
continue;
1999
}
2000
@@ -1990,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
2017
add = total - del;
2018
}
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",
2022
number_width, added + deleted,
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);
2030
}
2031
2032
for (i = 0; i < data->nr; i++) {
@@ -2018,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
2047
if (i < count)
2048
continue;
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);
2053
extra_shown = 1;
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);
2057
}
2058
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
2065
2066
for (i = 0; i < data->nr; i++) {
2067
int added = data->files[i]->added;
2037
- int deleted= data->files[i]->deleted;
2068
+ int deleted = data->files[i]->deleted;
2069
2070
if (data->files[i]->is_unmerged ||
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
2075
dels += deleted;
2076
}
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);
2079
}
2080
2081
static void show_numstat(struct diffstat_t *data, struct diff_options *options)
diff.h
+2
-2
@@ -405,8 +405,8 @@ extern int parse_rename_score(const char **cp_p);
405
406
extern long parse_algorithm_value(const char *value);
407
408
-extern int print_stat_summary(FILE *fp, int files,
409
- int insertions, int deletions);
408
+extern void print_stat_summary(FILE *fp, int files,
409
+ int insertions, int deletions);
410
extern void setup_diff_pager(struct diff_options *);
411
412
#endif /* DIFF_H */