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>

John Keeping committed Feb 7, 2013 at 20:15 UTC 30997bb8f11bd6e63a582c3b4e029ca47868ad8d
1 file changed +20 -95
diff.c
+20 -95
@@ -402,12 +402,7 @@ static void emit_line_0(struct diff_options *o, const char *set, const char *res
402 int nofirst;
403 FILE *file = o->file;
404
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);
406
407 if (len == 0) {
408 has_trailing_newline = (first == '\n');
@@ -625,13 +620,7 @@ static void emit_rewrite_diff(const char *name_a,
620 char *data_one, *data_two;
621 size_t size_one, size_two;
622 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);
624
625 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
626 a_prefix = o->b_prefix;
@@ -827,18 +816,14 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
816 int minus_first, minus_len, plus_first, plus_len;
817 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
818 struct diff_options *opt = diff_words->opt;
830 - struct strbuf *msgbuf;
831 - char *line_prefix = "";
819 + const char *line_prefix;
820
821 if (line[0] != '@' || parse_hunk_header(line, len,
822 &minus_first, &minus_len, &plus_first, &plus_len))
823 return;
824
825 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);
827
828 /* POSIX requires that first be decremented by one if len == 0... */
829 if (minus_len) {
@@ -962,14 +947,10 @@ static void diff_words_show(struct diff_words_data *diff_words)
947 struct diff_words_style *style = diff_words->style;
948
949 struct diff_options *opt = diff_words->opt;
965 - struct strbuf *msgbuf;
966 - char *line_prefix = "";
950 + const char *line_prefix;
951
952 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);
954
955 /* special case: only removal */
956 if (!diff_words->plus.text.size) {
@@ -1155,13 +1136,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
1136 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
1137 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1138 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);
1140
1141 if (ecbdata->header) {
1142 fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
@@ -1485,16 +1460,11 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1460 const char *reset, *add_c, *del_c;
1461 const char *line_prefix = "";
1462 int extra_shown = 0;
1488 - struct strbuf *msg = NULL;
1463
1464 if (data->nr == 0)
1465 return;
1466
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);
1468 count = options->stat_count ? options->stat_count : data->nr;
1469
1470 reset = diff_get_color_opt(options, DIFF_RESET);
@@ -1746,12 +1716,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
1716 dels += deleted;
1717 }
1718 }
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));
1720 print_stat_summary(options->file, total_files, adds, dels);
1721 }
1722
@@ -1765,12 +1730,7 @@ static void show_numstat(struct diffstat_t *data, struct diff_options *options)
1730 for (i = 0; i < data->nr; i++) {
1731 struct diffstat_file *file = data->files[i];
1732
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));
1734
1735 if (file->is_binary)
1736 fprintf(options->file, "-\t-\t");
@@ -1812,13 +1772,7 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
1772 {
1773 unsigned long this_dir = 0;
1774 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);
1776
1777 while (dir->nr) {
1778 struct dirstat_file *f = dir->files;
@@ -2068,15 +2022,10 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
2022 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
2023 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
2024 char *err;
2071 - char *line_prefix = "";
2072 - struct strbuf *msgbuf;
2025 + const char *line_prefix;
2026
2027 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);
2029
2030 if (line[0] == '+') {
2031 unsigned bad;
@@ -2263,13 +2212,7 @@ static void builtin_diff(const char *name_a,
2212 struct userdiff_driver *textconv_one = NULL;
2213 struct userdiff_driver *textconv_two = NULL;
2214 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);
2216
2217 if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
2218 (!one->mode || S_ISGITLINK(one->mode)) &&
@@ -2968,14 +2911,9 @@ static void fill_metainfo(struct strbuf *msg,
2911 {
2912 const char *set = diff_get_color(use_color, DIFF_METAINFO);
2913 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);
2915
2916 *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 - }
2917 strbuf_init(msg, PATH_MAX * 2 + 300);
2918 switch (p->status) {
2919 case DIFF_STATUS_COPIED:
@@ -3910,12 +3848,8 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
3848 {
3849 int line_termination = opt->line_termination;
3850 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 - }
3851
3852 + fprintf(opt->file, "%s", diff_line_prefix(opt));
3853 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
3854 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
3855 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
4119 static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
4120 {
4121 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);
4123
4124 switch(p->status) {
4125 case DIFF_STATUS_DELETED:
@@ -4491,13 +4420,9 @@ void diff_flush(struct diff_options *options)
4420
4421 if (output_format & DIFF_FORMAT_PATCH) {
4422 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);
4426 if (options->stat_sep) {
4427 /* attach patch instead of inline */
4428 fputs(options->stat_sep, options->file);