Skip to content

Commit 0911c47

Browse files
stefanbellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4eed0eb commit 0911c47

File tree

2 files changed

+74
-44
lines changed

2 files changed

+74
-44
lines changed

diff.c

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ enum diff_symbol {
566566
DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
567567
DIFF_SYMBOL_BINARY_DIFF_BODY,
568568
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,
569573
DIFF_SYMBOL_SUBMODULE_ADD,
570574
DIFF_SYMBOL_SUBMODULE_DEL,
571575
DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -629,6 +633,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
629633
{
630634
static const char *nneof = " No newline at end of file\n";
631635
const char *context, *reset, *set, *meta, *fraginfo;
636+
struct strbuf sb = STRBUF_INIT;
632637
switch (s) {
633638
case DIFF_SYMBOL_NO_LF_EOF:
634639
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,
640645
case DIFF_SYMBOL_SUBMODULE_HEADER:
641646
case DIFF_SYMBOL_SUBMODULE_ERROR:
642647
case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
648+
case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
649+
case DIFF_SYMBOL_STATS_LINE:
643650
case DIFF_SYMBOL_BINARY_DIFF_BODY:
644651
case DIFF_SYMBOL_CONTEXT_FRAGINFO:
645652
emit_line(o, "", "", line, len);
@@ -748,9 +755,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
748755
fprintf(o->file, "%sSubmodule %s contains modified content\n",
749756
diff_line_prefix(o), line);
750757
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;
751765
default:
752766
die("BUG: unknown diff symbol");
753767
}
768+
strbuf_release(&sb);
754769
}
755770

756771
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)
17051720
return 1 + (it * (width - 1) / max_change);
17061721
}
17071722

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)
17151725
{
17161726
if (cnt <= 0)
17171727
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);
17221731
}
17231732

17241733
static void fill_print_name(struct diffstat_file *file)
@@ -1742,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file)
17421751
file->print_name = pname;
17431752
}
17441753

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)
17461756
{
17471757
struct strbuf sb = STRBUF_INIT;
1748-
int ret;
17491758

17501759
if (!files) {
17511760
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;
17531764
}
17541765

17551766
strbuf_addf(&sb,
@@ -1776,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
17761787
deletions);
17771788
}
17781789
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);
17801792
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);
17821803
}
17831804

17841805
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)
17881809
int total_files = data->nr, count;
17891810
int width, name_width, graph_width, number_width = 0, bin_width = 0;
17901811
const char *reset, *add_c, *del_c;
1791-
const char *line_prefix = "";
17921812
int extra_shown = 0;
1813+
const char *line_prefix = diff_line_prefix(options);
1814+
struct strbuf out = STRBUF_INIT;
17931815

17941816
if (data->nr == 0)
17951817
return;
17961818

1797-
line_prefix = diff_line_prefix(options);
17981819
count = options->stat_count ? options->stat_count : data->nr;
17991820

18001821
reset = diff_get_color_opt(options, DIFF_RESET);
@@ -1948,26 +1969,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
19481969
}
19491970

19501971
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");
19541974
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);
19561979
continue;
19571980
}
1958-
fprintf(options->file, " %s%"PRIuMAX"%s",
1981+
strbuf_addf(&out, " %s%"PRIuMAX"%s",
19591982
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",
19621985
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);
19651990
continue;
19661991
}
19671992
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);
19711998
continue;
19721999
}
19732000

@@ -1990,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
19902017
add = total - del;
19912018
}
19922019
}
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",
19962022
number_width, added + deleted,
19972023
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);
20012030
}
20022031

20032032
for (i = 0; i < data->nr; i++) {
@@ -2018,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
20182047
if (i < count)
20192048
continue;
20202049
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);
20222053
extra_shown = 1;
20232054
}
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);
20262057
}
20272058

20282059
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
20342065

20352066
for (i = 0; i < data->nr; i++) {
20362067
int added = data->files[i]->added;
2037-
int deleted= data->files[i]->deleted;
2068+
int deleted = data->files[i]->deleted;
20382069

20392070
if (data->files[i]->is_unmerged ||
20402071
(!data->files[i]->is_interesting && (added + deleted == 0))) {
@@ -2044,8 +2075,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
20442075
dels += deleted;
20452076
}
20462077
}
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);
20492079
}
20502080

20512081
static void show_numstat(struct diffstat_t *data, struct diff_options *options)

diff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ extern int parse_rename_score(const char **cp_p);
405405

406406
extern long parse_algorithm_value(const char *value);
407407

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);
410410
extern void setup_diff_pager(struct diff_options *);
411411

412412
#endif /* DIFF_H */

0 commit comments

Comments
 (0)