]> git.ipfire.org Git - thirdparty/git.git/blobdiff - diff.c
diff.c: convert show_stats to use emit_diff_symbol
[thirdparty/git.git] / diff.c
diff --git a/diff.c b/diff.c
index acedf86aecc151112898ff54575151f157776281..76d4b8ebf97c0688ffa64176fc8d31fa544cadf9 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -2,6 +2,7 @@
  * Copyright (C) 2005 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "quote.h"
 #include "diff.h"
@@ -559,68 +560,289 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
        emit_line_0(o, set, reset, line[0], line+1, len-1);
 }
 
-static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
-{
-       if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
-             ecbdata->blank_at_eof_in_preimage &&
-             ecbdata->blank_at_eof_in_postimage &&
-             ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
-             ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
-               return 0;
-       return ws_blank_line(line, len, ecbdata->ws_rule);
-}
+enum diff_symbol {
+       DIFF_SYMBOL_BINARY_DIFF_HEADER,
+       DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+       DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+       DIFF_SYMBOL_BINARY_DIFF_BODY,
+       DIFF_SYMBOL_BINARY_DIFF_FOOTER,
+       DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+       DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+       DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+       DIFF_SYMBOL_STATS_LINE,
+       DIFF_SYMBOL_SUBMODULE_ADD,
+       DIFF_SYMBOL_SUBMODULE_DEL,
+       DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+       DIFF_SYMBOL_SUBMODULE_MODIFIED,
+       DIFF_SYMBOL_SUBMODULE_HEADER,
+       DIFF_SYMBOL_SUBMODULE_ERROR,
+       DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
+       DIFF_SYMBOL_REWRITE_DIFF,
+       DIFF_SYMBOL_BINARY_FILES,
+       DIFF_SYMBOL_HEADER,
+       DIFF_SYMBOL_FILEPAIR_PLUS,
+       DIFF_SYMBOL_FILEPAIR_MINUS,
+       DIFF_SYMBOL_WORDS_PORCELAIN,
+       DIFF_SYMBOL_WORDS,
+       DIFF_SYMBOL_CONTEXT,
+       DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+       DIFF_SYMBOL_PLUS,
+       DIFF_SYMBOL_MINUS,
+       DIFF_SYMBOL_NO_LF_EOF,
+       DIFF_SYMBOL_CONTEXT_FRAGINFO,
+       DIFF_SYMBOL_CONTEXT_MARKER,
+       DIFF_SYMBOL_SEPARATOR
+};
+/*
+ * Flags for content lines:
+ * 0..12 are whitespace rules
+ * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
+ * 16 is marking if the line is blank at EOF
+ */
+#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
+#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
 
-static void emit_line_checked(const char *reset,
-                             struct emit_callback *ecbdata,
-                             const char *line, int len,
-                             enum color_diff color,
-                             unsigned ws_error_highlight,
-                             char sign)
+static void emit_line_ws_markup(struct diff_options *o,
+                               const char *set, const char *reset,
+                               const char *line, int len, char sign,
+                               unsigned ws_rule, int blank_at_eof)
 {
-       const char *set = diff_get_color(ecbdata->color_diff, color);
        const char *ws = NULL;
 
-       if (ecbdata->opt->ws_error_highlight & ws_error_highlight) {
-               ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+       if (o->ws_error_highlight & ws_rule) {
+               ws = diff_get_color_opt(o, DIFF_WHITESPACE);
                if (!*ws)
                        ws = NULL;
        }
 
        if (!ws)
-               emit_line_0(ecbdata->opt, set, reset, sign, line, len);
-       else if (sign == '+' && new_blank_line_at_eof(ecbdata, line, len))
+               emit_line_0(o, set, reset, sign, line, len);
+       else if (blank_at_eof)
                /* Blank line at EOF - paint '+' as well */
-               emit_line_0(ecbdata->opt, ws, reset, sign, line, len);
+               emit_line_0(o, ws, reset, sign, line, len);
        else {
                /* Emit just the prefix, then the rest. */
-               emit_line_0(ecbdata->opt, set, reset, sign, "", 0);
-               ws_check_emit(line, len, ecbdata->ws_rule,
-                             ecbdata->opt->file, set, reset, ws);
+               emit_line_0(o, set, reset, sign, "", 0);
+               ws_check_emit(line, len, ws_rule,
+                             o->file, set, reset, ws);
        }
 }
 
+static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
+                            const char *line, int len, unsigned flags)
+{
+       static const char *nneof = " No newline at end of file\n";
+       const char *context, *reset, *set, *meta, *fraginfo;
+       struct strbuf sb = STRBUF_INIT;
+       switch (s) {
+       case DIFF_SYMBOL_NO_LF_EOF:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               putc('\n', o->file);
+               emit_line_0(o, context, reset, '\\',
+                           nneof, strlen(nneof));
+               break;
+       case DIFF_SYMBOL_SUBMODULE_HEADER:
+       case DIFF_SYMBOL_SUBMODULE_ERROR:
+       case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
+       case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
+       case DIFF_SYMBOL_STATS_LINE:
+       case DIFF_SYMBOL_BINARY_DIFF_BODY:
+       case DIFF_SYMBOL_CONTEXT_FRAGINFO:
+               emit_line(o, "", "", line, len);
+               break;
+       case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
+       case DIFF_SYMBOL_CONTEXT_MARKER:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, context, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SEPARATOR:
+               fprintf(o->file, "%s%c",
+                       diff_line_prefix(o),
+                       o->line_termination);
+               break;
+       case DIFF_SYMBOL_CONTEXT:
+               set = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, ' ',
+                                   flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
+               break;
+       case DIFF_SYMBOL_PLUS:
+               set = diff_get_color_opt(o, DIFF_FILE_NEW);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, '+',
+                                   flags & DIFF_SYMBOL_CONTENT_WS_MASK,
+                                   flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
+               break;
+       case DIFF_SYMBOL_MINUS:
+               set = diff_get_color_opt(o, DIFF_FILE_OLD);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line_ws_markup(o, set, reset, line, len, '-',
+                                   flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
+               break;
+       case DIFF_SYMBOL_WORDS_PORCELAIN:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, context, reset, line, len);
+               fputs("~\n", o->file);
+               break;
+       case DIFF_SYMBOL_WORDS:
+               context = diff_get_color_opt(o, DIFF_CONTEXT);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               /*
+                * Skip the prefix character, if any.  With
+                * diff_suppress_blank_empty, there may be
+                * none.
+                */
+               if (line[0] != '\n') {
+                       line++;
+                       len--;
+               }
+               emit_line(o, context, reset, line, len);
+               break;
+       case DIFF_SYMBOL_FILEPAIR_PLUS:
+               meta = diff_get_color_opt(o, DIFF_METAINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
+                       line, reset,
+                       strchr(line, ' ') ? "\t" : "");
+               break;
+       case DIFF_SYMBOL_FILEPAIR_MINUS:
+               meta = diff_get_color_opt(o, DIFF_METAINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
+                       line, reset,
+                       strchr(line, ' ') ? "\t" : "");
+               break;
+       case DIFF_SYMBOL_BINARY_FILES:
+       case DIFF_SYMBOL_HEADER:
+               fprintf(o->file, "%s", line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER:
+               fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
+               fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
+               fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
+               fputs(diff_line_prefix(o), o->file);
+               fputc('\n', o->file);
+               break;
+       case DIFF_SYMBOL_REWRITE_DIFF:
+               fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, fraginfo, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_ADD:
+               set = diff_get_color_opt(o, DIFF_FILE_NEW);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, set, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_DEL:
+               set = diff_get_color_opt(o, DIFF_FILE_OLD);
+               reset = diff_get_color_opt(o, DIFF_RESET);
+               emit_line(o, set, reset, line, len);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
+               fprintf(o->file, "%sSubmodule %s contains untracked content\n",
+                       diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_SUBMODULE_MODIFIED:
+               fprintf(o->file, "%sSubmodule %s contains modified content\n",
+                       diff_line_prefix(o), line);
+               break;
+       case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
+               emit_line(o, "", "", " 0 files changed\n",
+                         strlen(" 0 files changed\n"));
+               break;
+       case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
+               emit_line(o, "", "", " ...\n", strlen(" ...\n"));
+               break;
+       default:
+               die("BUG: unknown diff symbol");
+       }
+       strbuf_release(&sb);
+}
+
+void diff_emit_submodule_del(struct diff_options *o, const char *line)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_add(struct diff_options *o, const char *line)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
+}
+
+void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
+                        path, strlen(path), 0);
+}
+
+void diff_emit_submodule_modified(struct diff_options *o, const char *path)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
+                        path, strlen(path), 0);
+}
+
+void diff_emit_submodule_header(struct diff_options *o, const char *header)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
+                        header, strlen(header), 0);
+}
+
+void diff_emit_submodule_error(struct diff_options *o, const char *err)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
+}
+
+void diff_emit_submodule_pipethrough(struct diff_options *o,
+                                    const char *line, int len)
+{
+       emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
+}
+
+static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
+{
+       if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
+             ecbdata->blank_at_eof_in_preimage &&
+             ecbdata->blank_at_eof_in_postimage &&
+             ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
+             ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
+               return 0;
+       return ws_blank_line(line, len, ecbdata->ws_rule);
+}
+
 static void emit_add_line(const char *reset,
                          struct emit_callback *ecbdata,
                          const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_FILE_NEW, WSEH_NEW, '+');
+       unsigned flags = WSEH_NEW | ecbdata->ws_rule;
+       if (new_blank_line_at_eof(ecbdata, line, len))
+               flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
+
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
 }
 
 static void emit_del_line(const char *reset,
                          struct emit_callback *ecbdata,
                          const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_FILE_OLD, WSEH_OLD, '-');
+       unsigned flags = WSEH_OLD | ecbdata->ws_rule;
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
 }
 
 static void emit_context_line(const char *reset,
                              struct emit_callback *ecbdata,
                              const char *line, int len)
 {
-       emit_line_checked(reset, ecbdata, line, len,
-                         DIFF_CONTEXT, WSEH_CONTEXT, ' ');
+       unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
+       emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
 }
 
 static void emit_hunk_header(struct emit_callback *ecbdata,
@@ -643,7 +865,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
        if (len < 10 ||
            memcmp(line, atat, 2) ||
            !(ep = memmem(line + 2, len - 2, atat, 2))) {
-               emit_line(ecbdata->opt, context, reset, line, len);
+               emit_diff_symbol(ecbdata->opt,
+                                DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
                return;
        }
        ep += 2; /* skip over @@ */
@@ -677,7 +900,9 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
        }
 
        strbuf_add(&msgbuf, line + len, org_len - len);
-       emit_line(ecbdata->opt, "", "", msgbuf.buf, msgbuf.len);
+       strbuf_complete_line(&msgbuf);
+       emit_diff_symbol(ecbdata->opt,
+                        DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
        strbuf_release(&msgbuf);
 }
 
@@ -699,17 +924,17 @@ static void remove_tempfile(void)
        }
 }
 
-static void print_line_count(FILE *file, int count)
+static void add_line_count(struct strbuf *out, int count)
 {
        switch (count) {
        case 0:
-               fprintf(file, "0,0");
+               strbuf_addstr(out, "0,0");
                break;
        case 1:
-               fprintf(file, "1");
+               strbuf_addstr(out, "1");
                break;
        default:
-               fprintf(file, "1,%d", count);
+               strbuf_addf(out, "1,%d", count);
                break;
        }
 }
@@ -718,7 +943,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
                               int prefix, const char *data, int size)
 {
        const char *endp = NULL;
-       static const char *nneof = " No newline at end of file\n";
        const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
 
        while (0 < size) {
@@ -736,13 +960,8 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
                size -= len;
                data += len;
        }
-       if (!endp) {
-               const char *context = diff_get_color(ecb->color_diff,
-                                                    DIFF_CONTEXT);
-               putc('\n', ecb->opt->file);
-               emit_line_0(ecb->opt, context, reset, '\\',
-                           nneof, strlen(nneof));
-       }
+       if (!endp)
+               emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
 }
 
 static void emit_rewrite_diff(const char *name_a,
@@ -754,16 +973,12 @@ static void emit_rewrite_diff(const char *name_a,
                              struct diff_options *o)
 {
        int lc_a, lc_b;
-       const char *name_a_tab, *name_b_tab;
-       const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
-       const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
-       const char *reset = diff_get_color(o->use_color, DIFF_RESET);
        static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
        const char *a_prefix, *b_prefix;
        char *data_one, *data_two;
        size_t size_one, size_two;
        struct emit_callback ecbdata;
-       const char *line_prefix = diff_line_prefix(o);
+       struct strbuf out = STRBUF_INIT;
 
        if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
                a_prefix = o->b_prefix;
@@ -775,8 +990,6 @@ static void emit_rewrite_diff(const char *name_a,
 
        name_a += (*name_a == '/');
        name_b += (*name_b == '/');
-       name_a_tab = strchr(name_a, ' ') ? "\t" : "";
-       name_b_tab = strchr(name_b, ' ') ? "\t" : "";
 
        strbuf_reset(&a_name);
        strbuf_reset(&b_name);
@@ -803,18 +1016,23 @@ static void emit_rewrite_diff(const char *name_a,
 
        lc_a = count_lines(data_one, size_one);
        lc_b = count_lines(data_two, size_two);
-       fprintf(o->file,
-               "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
-               line_prefix, metainfo, a_name.buf, name_a_tab, reset,
-               line_prefix, metainfo, b_name.buf, name_b_tab, reset,
-               line_prefix, fraginfo);
+
+       emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+                        a_name.buf, a_name.len, 0);
+       emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+                        b_name.buf, b_name.len, 0);
+
+       strbuf_addstr(&out, "@@ -");
        if (!o->irreversible_delete)
-               print_line_count(o->file, lc_a);
+               add_line_count(&out, lc_a);
        else
-               fprintf(o->file, "?,?");
-       fprintf(o->file, " +");
-       print_line_count(o->file, lc_b);
-       fprintf(o->file, " @@%s\n", reset);
+               strbuf_addstr(&out, "?,?");
+       strbuf_addstr(&out, " +");
+       add_line_count(&out, lc_b);
+       strbuf_addstr(&out, " @@\n");
+       emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
+       strbuf_release(&out);
+
        if (lc_a && !o->irreversible_delete)
                emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
        if (lc_b)
@@ -1218,8 +1436,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
                        regfree(ecbdata->diff_words->word_regex);
                        free(ecbdata->diff_words->word_regex);
                }
-               free(ecbdata->diff_words);
-               ecbdata->diff_words = NULL;
+               FREE_AND_NULL(ecbdata->diff_words);
        }
 }
 
@@ -1276,30 +1493,25 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
 static void fn_out_consume(void *priv, char *line, unsigned long len)
 {
        struct emit_callback *ecbdata = priv;
-       const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
-       const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
        struct diff_options *o = ecbdata->opt;
-       const char *line_prefix = diff_line_prefix(o);
 
        o->found_changes = 1;
 
        if (ecbdata->header) {
-               fprintf(o->file, "%s", ecbdata->header->buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                ecbdata->header->buf, ecbdata->header->len, 0);
                strbuf_reset(ecbdata->header);
                ecbdata->header = NULL;
        }
 
        if (ecbdata->label_path[0]) {
-               const char *name_a_tab, *name_b_tab;
-
-               name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
-               name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
-
-               fprintf(o->file, "%s%s--- %s%s%s\n",
-                       line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
-               fprintf(o->file, "%s%s+++ %s%s%s\n",
-                       line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
+               emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
+                                ecbdata->label_path[0],
+                                strlen(ecbdata->label_path[0]), 0);
+               emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
+                                ecbdata->label_path[1],
+                                strlen(ecbdata->label_path[1]), 0);
                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
        }
 
@@ -1315,12 +1527,13 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
                len = sane_truncate_line(ecbdata, line, len);
                find_lno(line, ecbdata);
                emit_hunk_header(ecbdata, line, len);
-               if (line[len-1] != '\n')
-                       putc('\n', o->file);
                return;
        }
 
        if (ecbdata->diff_words) {
+               enum diff_symbol s =
+                       ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
+                       DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
                if (line[0] == '-') {
                        diff_words_append(line, len,
                                          &ecbdata->diff_words->minus);
@@ -1340,21 +1553,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
                        return;
                }
                diff_words_flush(ecbdata);
-               if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
-                       emit_line(o, context, reset, line, len);
-                       fputs("~\n", o->file);
-               } else {
-                       /*
-                        * Skip the prefix character, if any.  With
-                        * diff_suppress_blank_empty, there may be
-                        * none.
-                        */
-                       if (line[0] != '\n') {
-                             line++;
-                             len--;
-                       }
-                       emit_line(o, context, reset, line, len);
-               }
+               emit_diff_symbol(o, s, line, len, 0);
                return;
        }
 
@@ -1375,8 +1574,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
        default:
                /* incomplete line at the end */
                ecbdata->lno_in_preimage++;
-               emit_line(o, diff_get_color(ecbdata->color_diff, DIFF_CONTEXT),
-                         reset, line, len);
+               emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
+                                line, len, 0);
                break;
        }
 }
@@ -1521,20 +1720,14 @@ static int scale_linear(int it, int width, int max_change)
        return 1 + (it * (width - 1) / max_change);
 }
 
-static void show_name(FILE *file,
-                     const char *prefix, const char *name, int len)
-{
-       fprintf(file, " %s%-*s |", prefix, len, name);
-}
-
-static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
+static void show_graph(struct strbuf *out, char ch, int cnt,
+                      const char *set, const char *reset)
 {
        if (cnt <= 0)
                return;
-       fprintf(file, "%s", set);
-       while (cnt--)
-               putc(ch, file);
-       fprintf(file, "%s", reset);
+       strbuf_addstr(out, set);
+       strbuf_addchars(out, ch, cnt);
+       strbuf_addstr(out, reset);
 }
 
 static void fill_print_name(struct diffstat_file *file)
@@ -1558,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file)
        file->print_name = pname;
 }
 
-int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
+static void print_stat_summary_inserts_deletes(struct diff_options *options,
+               int files, int insertions, int deletions)
 {
        struct strbuf sb = STRBUF_INIT;
-       int ret;
 
        if (!files) {
                assert(insertions == 0 && deletions == 0);
-               return fprintf(fp, "%s\n", " 0 files changed");
+               emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
+                                NULL, 0, 0);
+               return;
        }
 
        strbuf_addf(&sb,
@@ -1592,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions)
                            deletions);
        }
        strbuf_addch(&sb, '\n');
-       ret = fputs(sb.buf, fp);
+       emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
+                        sb.buf, sb.len, 0);
        strbuf_release(&sb);
-       return ret;
+}
+
+void print_stat_summary(FILE *fp, int files,
+                       int insertions, int deletions)
+{
+       struct diff_options o;
+       memset(&o, 0, sizeof(o));
+       o.file = fp;
+
+       print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
 }
 
 static void show_stats(struct diffstat_t *data, struct diff_options *options)
@@ -1604,13 +1809,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
        int total_files = data->nr, count;
        int width, name_width, graph_width, number_width = 0, bin_width = 0;
        const char *reset, *add_c, *del_c;
-       const char *line_prefix = "";
        int extra_shown = 0;
+       const char *line_prefix = diff_line_prefix(options);
+       struct strbuf out = STRBUF_INIT;
 
        if (data->nr == 0)
                return;
 
-       line_prefix = diff_line_prefix(options);
        count = options->stat_count ? options->stat_count : data->nr;
 
        reset = diff_get_color_opt(options, DIFF_RESET);
@@ -1764,26 +1969,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                }
 
                if (file->is_binary) {
-                       fprintf(options->file, "%s", line_prefix);
-                       show_name(options->file, prefix, name, len);
-                       fprintf(options->file, " %*s", number_width, "Bin");
+                       strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+                       strbuf_addf(&out, " %*s", number_width, "Bin");
                        if (!added && !deleted) {
-                               putc('\n', options->file);
+                               strbuf_addch(&out, '\n');
+                               emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                                out.buf, out.len, 0);
+                               strbuf_reset(&out);
                                continue;
                        }
-                       fprintf(options->file, " %s%"PRIuMAX"%s",
+                       strbuf_addf(&out, " %s%"PRIuMAX"%s",
                                del_c, deleted, reset);
-                       fprintf(options->file, " -> ");
-                       fprintf(options->file, "%s%"PRIuMAX"%s",
+                       strbuf_addstr(&out, " -> ");
+                       strbuf_addf(&out, "%s%"PRIuMAX"%s",
                                add_c, added, reset);
-                       fprintf(options->file, " bytes");
-                       fprintf(options->file, "\n");
+                       strbuf_addstr(&out, " bytes\n");
+                       emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                        out.buf, out.len, 0);
+                       strbuf_reset(&out);
                        continue;
                }
                else if (file->is_unmerged) {
-                       fprintf(options->file, "%s", line_prefix);
-                       show_name(options->file, prefix, name, len);
-                       fprintf(options->file, " Unmerged\n");
+                       strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+                       strbuf_addstr(&out, " Unmerged\n");
+                       emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                        out.buf, out.len, 0);
+                       strbuf_reset(&out);
                        continue;
                }
 
@@ -1806,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                                add = total - del;
                        }
                }
-               fprintf(options->file, "%s", line_prefix);
-               show_name(options->file, prefix, name, len);
-               fprintf(options->file, " %*"PRIuMAX"%s",
+               strbuf_addf(&out, " %s%-*s |", prefix, len, name);
+               strbuf_addf(&out, " %*"PRIuMAX"%s",
                        number_width, added + deleted,
                        added + deleted ? " " : "");
-               show_graph(options->file, '+', add, add_c, reset);
-               show_graph(options->file, '-', del, del_c, reset);
-               fprintf(options->file, "\n");
+               show_graph(&out, '+', add, add_c, reset);
+               show_graph(&out, '-', del, del_c, reset);
+               strbuf_addch(&out, '\n');
+               emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
+                                out.buf, out.len, 0);
+               strbuf_reset(&out);
        }
 
        for (i = 0; i < data->nr; i++) {
@@ -1834,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
                if (i < count)
                        continue;
                if (!extra_shown)
-                       fprintf(options->file, "%s ...\n", line_prefix);
+                       emit_diff_symbol(options,
+                                        DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
+                                        NULL, 0, 0);
                extra_shown = 1;
        }
-       fprintf(options->file, "%s", line_prefix);
-       print_stat_summary(options->file, total_files, adds, dels);
+
+       print_stat_summary_inserts_deletes(options, total_files, adds, dels);
 }
 
 static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
@@ -1850,7 +2065,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
 
        for (i = 0; i < data->nr; i++) {
                int added = data->files[i]->added;
-               int deleted= data->files[i]->deleted;
+               int deleted = data->files[i]->deleted;
 
                if (data->files[i]->is_unmerged ||
                    (!data->files[i]->is_interesting && (added + deleted == 0))) {
@@ -1860,8 +2075,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option
                        dels += deleted;
                }
        }
-       fprintf(options->file, "%s", diff_line_prefix(options));
-       print_stat_summary(options->file, total_files, adds, dels);
+       print_stat_summary_inserts_deletes(options, total_files, adds, dels);
 }
 
 static void show_numstat(struct diffstat_t *data, struct diff_options *options)
@@ -2225,8 +2439,8 @@ static unsigned char *deflate_it(char *data,
        return deflated;
 }
 
-static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
-                                 const char *prefix)
+static void emit_binary_diff_body(struct diff_options *o,
+                                 mmfile_t *one, mmfile_t *two)
 {
        void *cp;
        void *delta;
@@ -2255,13 +2469,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
        }
 
        if (delta && delta_size < deflate_size) {
-               fprintf(file, "%sdelta %lu\n", prefix, orig_size);
+               char *s = xstrfmt("%lu", orig_size);
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
+                                s, strlen(s), 0);
+               free(s);
                free(deflated);
                data = delta;
                data_size = delta_size;
-       }
-       else {
-               fprintf(file, "%sliteral %lu\n", prefix, two->size);
+       } else {
+               char *s = xstrfmt("%lu", two->size);
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
+                                s, strlen(s), 0);
+               free(s);
                free(delta);
                data = deflated;
                data_size = deflate_size;
@@ -2270,8 +2489,9 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
        /* emit data encoded in base85 */
        cp = data;
        while (data_size) {
+               int len;
                int bytes = (52 < data_size) ? 52 : data_size;
-               char line[70];
+               char line[71];
                data_size -= bytes;
                if (bytes <= 26)
                        line[0] = bytes + 'A' - 1;
@@ -2279,20 +2499,24 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,
                        line[0] = bytes - 26 + 'a' - 1;
                encode_85(line + 1, cp, bytes);
                cp = (char *) cp + bytes;
-               fprintf(file, "%s", prefix);
-               fputs(line, file);
-               fputc('\n', file);
+
+               len = strlen(line);
+               line[len++] = '\n';
+               line[len] = '\0';
+
+               emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
+                                line, len, 0);
        }
-       fprintf(file, "%s\n", prefix);
+       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
        free(data);
 }
 
-static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two,
-                            const char *prefix)
+static void emit_binary_diff(struct diff_options *o,
+                            mmfile_t *one, mmfile_t *two)
 {
-       fprintf(file, "%sGIT binary patch\n", prefix);
-       emit_binary_diff_body(file, one, two, prefix);
-       emit_binary_diff_body(file, two, one, prefix);
+       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
+       emit_binary_diff_body(o, one, two);
+       emit_binary_diff_body(o, two, one);
 }
 
 int diff_filespec_is_binary(struct diff_filespec *one)
@@ -2369,24 +2593,16 @@ static void builtin_diff(const char *name_a,
        if (o->submodule_format == DIFF_SUBMODULE_LOG &&
            (!one->mode || S_ISGITLINK(one->mode)) &&
            (!two->mode || S_ISGITLINK(two->mode))) {
-               const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
-               const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
-               show_submodule_summary(o->file, one->path ? one->path : two->path,
-                               line_prefix,
+               show_submodule_summary(o, one->path ? one->path : two->path,
                                &one->oid, &two->oid,
-                               two->dirty_submodule,
-                               meta, del, add, reset);
+                               two->dirty_submodule);
                return;
        } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
                   (!one->mode || S_ISGITLINK(one->mode)) &&
                   (!two->mode || S_ISGITLINK(two->mode))) {
-               const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
-               const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
-               show_submodule_inline_diff(o->file, one->path ? one->path : two->path,
-                               line_prefix,
+               show_submodule_inline_diff(o, one->path ? one->path : two->path,
                                &one->oid, &two->oid,
-                               two->dirty_submodule,
-                               meta, del, add, reset, o);
+                               two->dirty_submodule);
                return;
        }
 
@@ -2435,7 +2651,8 @@ static void builtin_diff(const char *name_a,
                if (complete_rewrite &&
                    (textconv_one || !diff_filespec_is_binary(one)) &&
                    (textconv_two || !diff_filespec_is_binary(two))) {
-                       fprintf(o->file, "%s", header.buf);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
                        strbuf_reset(&header);
                        emit_rewrite_diff(name_a, name_b, one, two,
                                                textconv_one, textconv_two, o);
@@ -2445,23 +2662,31 @@ static void builtin_diff(const char *name_a,
        }
 
        if (o->irreversible_delete && lbl[1][0] == '/') {
-               fprintf(o->file, "%s", header.buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
+                                header.len, 0);
                strbuf_reset(&header);
                goto free_ab_and_return;
        } else if (!DIFF_OPT_TST(o, TEXT) &&
            ( (!textconv_one && diff_filespec_is_binary(one)) ||
              (!textconv_two && diff_filespec_is_binary(two)) )) {
+               struct strbuf sb = STRBUF_INIT;
                if (!one->data && !two->data &&
                    S_ISREG(one->mode) && S_ISREG(two->mode) &&
                    !DIFF_OPT_TST(o, BINARY)) {
                        if (!oidcmp(&one->oid, &two->oid)) {
                                if (must_show_header)
-                                       fprintf(o->file, "%s", header.buf);
+                                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                                        header.buf, header.len,
+                                                        0);
                                goto free_ab_and_return;
                        }
-                       fprintf(o->file, "%s", header.buf);
-                       fprintf(o->file, "%sBinary files %s and %s differ\n",
-                               line_prefix, lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
+                       strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+                                   diff_line_prefix(o), lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+                                        sb.buf, sb.len, 0);
+                       strbuf_release(&sb);
                        goto free_ab_and_return;
                }
                if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
@@ -2470,16 +2695,21 @@ static void builtin_diff(const char *name_a,
                if (mf1.size == mf2.size &&
                    !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
                        if (must_show_header)
-                               fprintf(o->file, "%s", header.buf);
+                               emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                                header.buf, header.len, 0);
                        goto free_ab_and_return;
                }
-               fprintf(o->file, "%s", header.buf);
+               emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
                strbuf_reset(&header);
                if (DIFF_OPT_TST(o, BINARY))
-                       emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
-               else
-                       fprintf(o->file, "%sBinary files %s and %s differ\n",
-                               line_prefix, lbl[0], lbl[1]);
+                       emit_binary_diff(o, &mf1, &mf2);
+               else {
+                       strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
+                                   diff_line_prefix(o), lbl[0], lbl[1]);
+                       emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
+                                        sb.buf, sb.len, 0);
+                       strbuf_release(&sb);
+               }
                o->found_changes = 1;
        } else {
                /* Crazy xdl interfaces.. */
@@ -2491,7 +2721,8 @@ static void builtin_diff(const char *name_a,
                const struct userdiff_funcname *pe;
 
                if (must_show_header) {
-                       fprintf(o->file, "%s", header.buf);
+                       emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
+                                        header.buf, header.len, 0);
                        strbuf_reset(&header);
                }
 
@@ -2755,7 +2986,7 @@ static int reuse_worktree_file(const char *name, const struct object_id *oid, in
         * Similarly, if we'd have to convert the file contents anyway, that
         * makes the optimization not worthwhile.
         */
-       if (!want_file && would_convert_to_git(name))
+       if (!want_file && would_convert_to_git(&the_index, name))
                return 0;
 
        len = strlen(name);
@@ -2877,7 +3108,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
                 * point if the path requires us to run the content
                 * conversion.
                 */
-               if (size_only && !would_convert_to_git(s->path))
+               if (size_only && !would_convert_to_git(&the_index, s->path))
                        return 0;
 
                /*
@@ -2904,7 +3135,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
                /*
                 * Convert from working tree format to canonical git format
                 */
-               if (convert_to_git(s->path, s->data, s->size, &buf, crlf_warn)) {
+               if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
                        size_t size = 0;
                        munmap(s->data, s->size);
                        s->should_munmap = 0;
@@ -2951,8 +3182,7 @@ void diff_free_filespec_blob(struct diff_filespec *s)
 void diff_free_filespec_data(struct diff_filespec *s)
 {
        diff_free_filespec_blob(s);
-       free(s->cnt_data);
-       s->cnt_data = NULL;
+       FREE_AND_NULL(s->cnt_data);
 }
 
 static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
@@ -3283,8 +3513,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
        const char *other;
        const char *attr_path;
 
-       name  = p->one->path;
-       other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+       name  = one->path;
+       other = (strcmp(name, two->path) ? two->path : NULL);
        attr_path = name;
        if (o->prefix_length)
                strip_prefix(o->prefix_length, &name, &other);
@@ -4735,6 +4965,21 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
                warning(_(rename_limit_advice), varname, needed);
 }
 
+static void diff_flush_patch_all_file_pairs(struct diff_options *o)
+{
+       int i;
+       struct diff_queue_struct *q = &diff_queued_diff;
+
+       if (WSEH_NEW & WS_RULE_MASK)
+               die("BUG: WS rules bit mask overlaps with diff symbol flags");
+
+       for (i = 0; i < q->nr; i++) {
+               struct diff_filepair *p = q->queue[i];
+               if (check_pair_status(p))
+                       diff_flush_patch(p, o);
+       }
+}
+
 void diff_flush(struct diff_options *options)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -4818,20 +5063,14 @@ void diff_flush(struct diff_options *options)
 
        if (output_format & DIFF_FORMAT_PATCH) {
                if (separator) {
-                       fprintf(options->file, "%s%c",
-                               diff_line_prefix(options),
-                               options->line_termination);
+                       emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
                        if (options->stat_sep) {
                                /* attach patch instead of inline */
                                fputs(options->stat_sep, options->file);
                        }
                }
 
-               for (i = 0; i < q->nr; i++) {
-                       struct diff_filepair *p = q->queue[i];
-                       if (check_pair_status(p))
-                               diff_flush_patch(p, options);
-               }
+               diff_flush_patch_all_file_pairs(options);
        }
 
        if (output_format & DIFF_FORMAT_CALLBACK)