Skip to content

Commit 30997bb

Browse files
johnkeepinggitster
authored andcommitted
diff.c: use diff_line_prefix() where applicable
Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f192223 commit 30997bb

File tree

1 file changed

+20
-95
lines changed

1 file changed

+20
-95
lines changed

diff.c

Lines changed: 20 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,7 @@ static void emit_line_0(struct diff_options *o, const char *set, const char *res
402402
int nofirst;
403403
FILE *file = o->file;
404404

405-
if (o->output_prefix) {
406-
struct strbuf *msg = NULL;
407-
msg = o->output_prefix(o, o->output_prefix_data);
408-
assert(msg);
409-
fwrite(msg->buf, msg->len, 1, file);
410-
}
405+
fputs(diff_line_prefix(o), file);
411406

412407
if (len == 0) {
413408
has_trailing_newline = (first == '\n');
@@ -625,13 +620,7 @@ static void emit_rewrite_diff(const char *name_a,
625620
char *data_one, *data_two;
626621
size_t size_one, size_two;
627622
struct emit_callback ecbdata;
628-
char *line_prefix = "";
629-
struct strbuf *msgbuf;
630-
631-
if (o && o->output_prefix) {
632-
msgbuf = o->output_prefix(o, o->output_prefix_data);
633-
line_prefix = msgbuf->buf;
634-
}
623+
const char *line_prefix = diff_line_prefix(o);
635624

636625
if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
637626
a_prefix = o->b_prefix;
@@ -827,18 +816,14 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
827816
int minus_first, minus_len, plus_first, plus_len;
828817
const char *minus_begin, *minus_end, *plus_begin, *plus_end;
829818
struct diff_options *opt = diff_words->opt;
830-
struct strbuf *msgbuf;
831-
char *line_prefix = "";
819+
const char *line_prefix;
832820

833821
if (line[0] != '@' || parse_hunk_header(line, len,
834822
&minus_first, &minus_len, &plus_first, &plus_len))
835823
return;
836824

837825
assert(opt);
838-
if (opt->output_prefix) {
839-
msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
840-
line_prefix = msgbuf->buf;
841-
}
826+
line_prefix = diff_line_prefix(opt);
842827

843828
/* POSIX requires that first be decremented by one if len == 0... */
844829
if (minus_len) {
@@ -962,14 +947,10 @@ static void diff_words_show(struct diff_words_data *diff_words)
962947
struct diff_words_style *style = diff_words->style;
963948

964949
struct diff_options *opt = diff_words->opt;
965-
struct strbuf *msgbuf;
966-
char *line_prefix = "";
950+
const char *line_prefix;
967951

968952
assert(opt);
969-
if (opt->output_prefix) {
970-
msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
971-
line_prefix = msgbuf->buf;
972-
}
953+
line_prefix = diff_line_prefix(opt);
973954

974955
/* special case: only removal */
975956
if (!diff_words->plus.text.size) {
@@ -1155,13 +1136,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
11551136
const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
11561137
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
11571138
struct diff_options *o = ecbdata->opt;
1158-
char *line_prefix = "";
1159-
struct strbuf *msgbuf;
1160-
1161-
if (o && o->output_prefix) {
1162-
msgbuf = o->output_prefix(o, o->output_prefix_data);
1163-
line_prefix = msgbuf->buf;
1164-
}
1139+
const char *line_prefix = diff_line_prefix(o);
11651140

11661141
if (ecbdata->header) {
11671142
fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
@@ -1485,16 +1460,11 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14851460
const char *reset, *add_c, *del_c;
14861461
const char *line_prefix = "";
14871462
int extra_shown = 0;
1488-
struct strbuf *msg = NULL;
14891463

14901464
if (data->nr == 0)
14911465
return;
14921466

1493-
if (options->output_prefix) {
1494-
msg = options->output_prefix(options, options->output_prefix_data);
1495-
line_prefix = msg->buf;
1496-
}
1497-
1467+
line_prefix = diff_line_prefix(options);
14981468
count = options->stat_count ? options->stat_count : data->nr;
14991469

15001470
reset = diff_get_color_opt(options, DIFF_RESET);
@@ -1746,12 +1716,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
17461716
dels += deleted;
17471717
}
17481718
}
1749-
if (options->output_prefix) {
1750-
struct strbuf *msg = NULL;
1751-
msg = options->output_prefix(options,
1752-
options->output_prefix_data);
1753-
fprintf(options->file, "%s", msg->buf);
1754-
}
1719+
fprintf(options->file, "%s", diff_line_prefix(options));
17551720
print_stat_summary(options->file, total_files, adds, dels);
17561721
}
17571722

@@ -1765,12 +1730,7 @@ static void show_numstat(struct diffstat_t *data, struct diff_options *options)
17651730
for (i = 0; i < data->nr; i++) {
17661731
struct diffstat_file *file = data->files[i];
17671732

1768-
if (options->output_prefix) {
1769-
struct strbuf *msg = NULL;
1770-
msg = options->output_prefix(options,
1771-
options->output_prefix_data);
1772-
fprintf(options->file, "%s", msg->buf);
1773-
}
1733+
fprintf(options->file, "%s", diff_line_prefix(options));
17741734

17751735
if (file->is_binary)
17761736
fprintf(options->file, "-\t-\t");
@@ -1812,13 +1772,7 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
18121772
{
18131773
unsigned long this_dir = 0;
18141774
unsigned int sources = 0;
1815-
const char *line_prefix = "";
1816-
struct strbuf *msg = NULL;
1817-
1818-
if (opt->output_prefix) {
1819-
msg = opt->output_prefix(opt, opt->output_prefix_data);
1820-
line_prefix = msg->buf;
1821-
}
1775+
const char *line_prefix = diff_line_prefix(opt);
18221776

18231777
while (dir->nr) {
18241778
struct dirstat_file *f = dir->files;
@@ -2068,15 +2022,10 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
20682022
const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
20692023
const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
20702024
char *err;
2071-
char *line_prefix = "";
2072-
struct strbuf *msgbuf;
2025+
const char *line_prefix;
20732026

20742027
assert(data->o);
2075-
if (data->o->output_prefix) {
2076-
msgbuf = data->o->output_prefix(data->o,
2077-
data->o->output_prefix_data);
2078-
line_prefix = msgbuf->buf;
2079-
}
2028+
line_prefix = diff_line_prefix(data->o);
20802029

20812030
if (line[0] == '+') {
20822031
unsigned bad;
@@ -2263,13 +2212,7 @@ static void builtin_diff(const char *name_a,
22632212
struct userdiff_driver *textconv_one = NULL;
22642213
struct userdiff_driver *textconv_two = NULL;
22652214
struct strbuf header = STRBUF_INIT;
2266-
struct strbuf *msgbuf;
2267-
char *line_prefix = "";
2268-
2269-
if (o->output_prefix) {
2270-
msgbuf = o->output_prefix(o, o->output_prefix_data);
2271-
line_prefix = msgbuf->buf;
2272-
}
2215+
const char *line_prefix = diff_line_prefix(o);
22732216

22742217
if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
22752218
(!one->mode || S_ISGITLINK(one->mode)) &&
@@ -2968,14 +2911,9 @@ static void fill_metainfo(struct strbuf *msg,
29682911
{
29692912
const char *set = diff_get_color(use_color, DIFF_METAINFO);
29702913
const char *reset = diff_get_color(use_color, DIFF_RESET);
2971-
struct strbuf *msgbuf;
2972-
char *line_prefix = "";
2914+
const char *line_prefix = diff_line_prefix(o);
29732915

29742916
*must_show_header = 1;
2975-
if (o->output_prefix) {
2976-
msgbuf = o->output_prefix(o, o->output_prefix_data);
2977-
line_prefix = msgbuf->buf;
2978-
}
29792917
strbuf_init(msg, PATH_MAX * 2 + 300);
29802918
switch (p->status) {
29812919
case DIFF_STATUS_COPIED:
@@ -3910,12 +3848,8 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
39103848
{
39113849
int line_termination = opt->line_termination;
39123850
int inter_name_termination = line_termination ? '\t' : '\0';
3913-
if (opt->output_prefix) {
3914-
struct strbuf *msg = NULL;
3915-
msg = opt->output_prefix(opt, opt->output_prefix_data);
3916-
fprintf(opt->file, "%s", msg->buf);
3917-
}
39183851

3852+
fprintf(opt->file, "%s", diff_line_prefix(opt));
39193853
if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
39203854
fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
39213855
diff_unique_abbrev(p->one->sha1, opt->abbrev));
@@ -4185,12 +4119,7 @@ static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_fil
41854119
static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
41864120
{
41874121
FILE *file = opt->file;
4188-
char *line_prefix = "";
4189-
4190-
if (opt->output_prefix) {
4191-
struct strbuf *buf = opt->output_prefix(opt, opt->output_prefix_data);
4192-
line_prefix = buf->buf;
4193-
}
4122+
const char *line_prefix = diff_line_prefix(opt);
41944123

41954124
switch(p->status) {
41964125
case DIFF_STATUS_DELETED:
@@ -4491,13 +4420,9 @@ void diff_flush(struct diff_options *options)
44914420

44924421
if (output_format & DIFF_FORMAT_PATCH) {
44934422
if (separator) {
4494-
if (options->output_prefix) {
4495-
struct strbuf *msg = NULL;
4496-
msg = options->output_prefix(options,
4497-
options->output_prefix_data);
4498-
fwrite(msg->buf, msg->len, 1, options->file);
4499-
}
4500-
putc(options->line_termination, options->file);
4423+
fprintf(options->file, "%s%c",
4424+
diff_line_prefix(options),
4425+
options->line_termination);
45014426
if (options->stat_sep) {
45024427
/* attach patch instead of inline */
45034428
fputs(options->stat_sep, options->file);

0 commit comments

Comments
 (0)