Merge branch 'jl/maint-diff-ignore-submodules'
authorJunio C Hamano <gitster@pobox.com>
Wed, 30 Jun 2010 18:55:37 +0000 (11:55 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 Jun 2010 18:55:37 +0000 (11:55 -0700)
* jl/maint-diff-ignore-submodules:
  t4027,4041: Use test -s to test for an empty file
  Add optional parameters to the diff option "--ignore-submodules"
  git diff: rename test that had a conflicting name

1  2 
Documentation/diff-options.txt
diff.c

index e745a3ccdc923b068e791a778a7c73b664b230d2,2dd91e930bae16808108eb677246aa1163abbc82..2371262b10aad44391406e126a1a8027b543fd49
@@@ -21,7 -21,6 +21,7 @@@ endif::git-format-patch[
  ifndef::git-format-patch[]
  -p::
  -u::
 +--patch::
        Generate patch (see section on generating patches).
        {git-diff? This is the default.}
  endif::git-format-patch[]
@@@ -127,39 -126,11 +127,39 @@@ any of those replacements occurred
        gives the default to color output.
        Same as `--color=never`.
  
 ---color-words[=<regex>]::
 -      Show colored word diff, i.e., color words which have changed.
 -      By default, words are separated by whitespace.
 +--word-diff[=<mode>]::
 +      Show a word diff, using the <mode> to delimit changed words.
 +      By default, words are delimited by whitespace; see
 +      `--word-diff-regex` below.  The <mode> defaults to 'plain', and
 +      must be one of:
 ++
 +--
 +color::
 +      Highlight changed words using only colors.  Implies `--color`.
 +plain::
 +      Show words as `[-removed-]` and `{+added+}`.  Makes no
 +      attempts to escape the delimiters if they appear in the input,
 +      so the output may be ambiguous.
 +porcelain::
 +      Use a special line-based format intended for script
 +      consumption.  Added/removed/unchanged runs are printed in the
 +      usual unified diff format, starting with a `+`/`-`/` `
 +      character at the beginning of the line and extending to the
 +      end of the line.  Newlines in the input are represented by a
 +      tilde `~` on a line of its own.
 +none::
 +      Disable word diff again.
 +--
  +
 -When a <regex> is specified, every non-overlapping match of the
 +Note that despite the name of the first mode, color is used to
 +highlight the changed parts in all modes if enabled.
 +
 +--word-diff-regex=<regex>::
 +      Use <regex> to decide what a word is, instead of considering
 +      runs of non-whitespace to be a word.  Also implies
 +      `--word-diff` unless it was already enabled.
 ++
 +Every non-overlapping match of the
  <regex> is considered a word.  Anything between these matches is
  considered whitespace and ignored(!) for the purposes of finding
  differences.  You may want to append `|[^[:space:]]` to your regular
@@@ -171,10 -142,6 +171,10 @@@ The regex can also be set via a diff dr
  linkgit:gitattributes[1] or linkgit:git-config[1].  Giving it explicitly
  overrides any diff driver or configuration setting.  Diff drivers
  override configuration settings.
 +
 +--color-words[=<regex>]::
 +      Equivalent to `--word-diff=color` plus (if a regex was
 +      specified) `--word-diff-regex=<regex>`.
  endif::git-format-patch[]
  
  --no-renames::
@@@ -210,14 -177,7 +210,14 @@@ endif::git-format-patch[
        Break complete rewrite changes into pairs of delete and create.
  
  -M::
 +ifndef::git-log[]
        Detect renames.
 +endif::git-log[]
 +ifdef::git-log[]
 +      If generating diffs, detect and report renames for each commit.
 +      For following files across renames while traversing history, see
 +      `--follow`.
 +endif::git-log[]
  
  -C::
        Detect copies as well as renames.  See also `--find-copies-harder`.
@@@ -328,8 -288,14 +328,14 @@@ endif::git-format-patch[
  --no-ext-diff::
        Disallow external diff drivers.
  
- --ignore-submodules::
-       Ignore changes to submodules in the diff generation.
+ --ignore-submodules[=<when>]::
+       Ignore changes to submodules in the diff generation. <when> can be
+       either "untracked", "dirty" or "all", which is the default. When
+       "untracked" is used submodules are not considered dirty when they only
+       contain untracked content (but they are still scanned for modified
+       content). Using "dirty" ignores all changes to the work tree of submodules,
+       only changes to the commits stored in the superproject are shown (this was
+       the behavior until 1.7.0). Using "all" hides all changes to submodules.
  
  --src-prefix=<prefix>::
        Show the given source prefix instead of "a/".
diff --combined diff.c
index 2332fa6798d9e40e83a07a318675e70d9b3169aa,804acf0b21c5bc0f7b702c62e159a4296277538b..9d70f9d731325287dd7e9f83e5e3b44ad1dfb8d9
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -30,7 -30,6 +30,7 @@@ static const char *diff_word_regex_cfg
  static const char *external_diff_cmd_cfg;
  int diff_auto_refresh_index = 1;
  static int diff_mnemonic_prefix;
 +static int diff_no_prefix;
  
  static char diff_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
@@@ -44,6 -43,9 +44,6 @@@
        GIT_COLOR_NORMAL,       /* FUNCINFO */
  };
  
 -static void diff_filespec_load_driver(struct diff_filespec *one);
 -static char *run_textconv(const char *, struct diff_filespec *, size_t *);
 -
  static int parse_diff_color_slot(const char *var, int ofs)
  {
        if (!strcasecmp(var+ofs, "plain"))
@@@ -98,10 -100,6 +98,10 @@@ int git_diff_ui_config(const char *var
                diff_mnemonic_prefix = git_config_bool(var, value);
                return 0;
        }
 +      if (!strcmp(var, "diff.noprefix")) {
 +              diff_no_prefix = git_config_bool(var, value);
 +              return 0;
 +      }
        if (!strcmp(var, "diff.external"))
                return git_config_string(&external_diff_cmd_cfg, var, value);
        if (!strcmp(var, "diff.wordregex"))
@@@ -195,8 -193,8 +195,8 @@@ struct emit_callback 
        sane_truncate_fn truncate;
        const char **label_path;
        struct diff_words_data *diff_words;
 +      struct diff_options *opt;
        int *found_changesp;
 -      FILE *file;
        struct strbuf *header;
  };
  
@@@ -283,19 -281,11 +283,19 @@@ static void check_blank_at_eof(mmfile_
        ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
  }
  
 -static void emit_line_0(FILE *file, const char *set, const char *reset,
 +static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
                        int first, const char *line, int len)
  {
        int has_trailing_newline, has_trailing_carriage_return;
        int nofirst;
 +      FILE *file = o->file;
 +
 +      if (o->output_prefix) {
 +              struct strbuf *msg = NULL;
 +              msg = o->output_prefix(o, o->output_prefix_data);
 +              assert(msg);
 +              fwrite(msg->buf, msg->len, 1, file);
 +      }
  
        if (len == 0) {
                has_trailing_newline = (first == '\n');
                fputc('\n', file);
  }
  
 -static void emit_line(FILE *file, const char *set, const char *reset,
 +static void emit_line(struct diff_options *o, const char *set, const char *reset,
                      const char *line, int len)
  {
 -      emit_line_0(file, set, reset, line[0], line+1, len-1);
 +      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)
@@@ -350,15 -340,15 +350,15 @@@ static void emit_add_line(const char *r
        const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
  
        if (!*ws)
 -              emit_line_0(ecbdata->file, set, reset, '+', line, len);
 +              emit_line_0(ecbdata->opt, set, reset, '+', line, len);
        else if (new_blank_line_at_eof(ecbdata, line, len))
                /* Blank line at EOF - paint '+' as well */
 -              emit_line_0(ecbdata->file, ws, reset, '+', line, len);
 +              emit_line_0(ecbdata->opt, ws, reset, '+', line, len);
        else {
                /* Emit just the prefix, then the rest. */
 -              emit_line_0(ecbdata->file, set, reset, '+', "", 0);
 +              emit_line_0(ecbdata->opt, set, reset, '+', "", 0);
                ws_check_emit(line, len, ecbdata->ws_rule,
 -                            ecbdata->file, set, reset, ws);
 +                            ecbdata->opt->file, set, reset, ws);
        }
  }
  
@@@ -371,9 -361,6 +371,9 @@@ static void emit_hunk_header(struct emi
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
        static const char atat[2] = { '@', '@' };
        const char *cp, *ep;
 +      struct strbuf msgbuf = STRBUF_INIT;
 +      int org_len = len;
 +      int i = 1;
  
        /*
         * As a hunk header must begin with "@@ -<old>, +<new> @@",
        if (len < 10 ||
            memcmp(line, atat, 2) ||
            !(ep = memmem(line + 2, len - 2, atat, 2))) {
 -              emit_line(ecbdata->file, plain, reset, line, len);
 +              emit_line(ecbdata->opt, plain, reset, line, len);
                return;
        }
        ep += 2; /* skip over @@ */
  
        /* The hunk header in fraginfo color */
 -      emit_line(ecbdata->file, frag, reset, line, ep - line);
 +      strbuf_add(&msgbuf, frag, strlen(frag));
 +      strbuf_add(&msgbuf, line, ep - line);
 +      strbuf_add(&msgbuf, reset, strlen(reset));
 +
 +      /*
 +       * trailing "\r\n"
 +       */
 +      for ( ; i < 3; i++)
 +              if (line[len - i] == '\r' || line[len - i] == '\n')
 +                      len--;
  
        /* blank before the func header */
        for (cp = ep; ep - line < len; ep++)
                if (*ep != ' ' && *ep != '\t')
                        break;
 -      if (ep != cp)
 -              emit_line(ecbdata->file, plain, reset, cp, ep - cp);
 +      if (ep != cp) {
 +              strbuf_add(&msgbuf, plain, strlen(plain));
 +              strbuf_add(&msgbuf, cp, ep - cp);
 +              strbuf_add(&msgbuf, reset, strlen(reset));
 +      }
 +
 +      if (ep < line + len) {
 +              strbuf_add(&msgbuf, func, strlen(func));
 +              strbuf_add(&msgbuf, ep, line + len - ep);
 +              strbuf_add(&msgbuf, reset, strlen(reset));
 +      }
  
 -      if (ep < line + len)
 -              emit_line(ecbdata->file, func, reset, ep, line + len - ep);
 +      strbuf_add(&msgbuf, line + len, org_len - len);
 +      emit_line(ecbdata->opt, "", "", msgbuf.buf, msgbuf.len);
 +      strbuf_release(&msgbuf);
  }
  
  static struct diff_tempfile *claim_diff_tempfile(void) {
@@@ -477,7 -445,7 +477,7 @@@ static void emit_rewrite_lines(struct e
                len = endp ? (endp - data + 1) : size;
                if (prefix != '+') {
                        ecb->lno_in_preimage++;
 -                      emit_line_0(ecb->file, old, reset, '-',
 +                      emit_line_0(ecb->opt, old, reset, '-',
                                    data, len);
                } else {
                        ecb->lno_in_postimage++;
        if (!endp) {
                const char *plain = diff_get_color(ecb->color_diff,
                                                   DIFF_PLAIN);
 -              emit_line_0(ecb->file, plain, reset, '\\',
 +              emit_line_0(ecb->opt, plain, reset, '\\',
                            nneof, strlen(nneof));
        }
  }
@@@ -498,8 -466,8 +498,8 @@@ static void emit_rewrite_diff(const cha
                              const char *name_b,
                              struct diff_filespec *one,
                              struct diff_filespec *two,
 -                            const char *textconv_one,
 -                            const char *textconv_two,
 +                            struct userdiff_driver *textconv_one,
 +                            struct userdiff_driver *textconv_two,
                              struct diff_options *o)
  {
        int lc_a, lc_b;
        const char *reset = diff_get_color(color_diff, DIFF_RESET);
        static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
        const char *a_prefix, *b_prefix;
 -      const char *data_one, *data_two;
 +      char *data_one, *data_two;
        size_t size_one, size_two;
        struct emit_callback ecbdata;
 +      char *line_prefix = "";
 +      struct strbuf *msgbuf;
 +
 +      if (o && o->output_prefix) {
 +              msgbuf = o->output_prefix(o, o->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
  
        if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
                a_prefix = o->b_prefix;
        quote_two_c_style(&a_name, a_prefix, name_a, 0);
        quote_two_c_style(&b_name, b_prefix, name_b, 0);
  
 -      diff_populate_filespec(one, 0);
 -      diff_populate_filespec(two, 0);
 -      if (textconv_one) {
 -              data_one = run_textconv(textconv_one, one, &size_one);
 -              if (!data_one)
 -                      die("unable to read files to diff");
 -      }
 -      else {
 -              data_one = one->data;
 -              size_one = one->size;
 -      }
 -      if (textconv_two) {
 -              data_two = run_textconv(textconv_two, two, &size_two);
 -              if (!data_two)
 -                      die("unable to read files to diff");
 -      }
 -      else {
 -              data_two = two->data;
 -              size_two = two->size;
 -      }
 +      size_one = fill_textconv(textconv_one, one, &data_one);
 +      size_two = fill_textconv(textconv_two, two, &data_two);
  
        memset(&ecbdata, 0, sizeof(ecbdata));
        ecbdata.color_diff = color_diff;
        ecbdata.found_changesp = &o->found_changes;
        ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
 -      ecbdata.file = o->file;
 +      ecbdata.opt = o;
        if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
                mmfile_t mf1, mf2;
                mf1.ptr = (char *)data_one;
        lc_a = count_lines(data_one, size_one);
        lc_b = count_lines(data_two, size_two);
        fprintf(o->file,
 -              "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
 -              metainfo, a_name.buf, name_a_tab, reset,
 -              metainfo, b_name.buf, name_b_tab, reset, fraginfo);
 +              "%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);
        print_line_count(o->file, lc_a);
        fprintf(o->file, " +");
        print_line_count(o->file, lc_b);
@@@ -599,134 -577,23 +599,134 @@@ static void diff_words_append(char *lin
        buffer->text.ptr[buffer->text.size] = '\0';
  }
  
 +struct diff_words_style_elem
 +{
 +      const char *prefix;
 +      const char *suffix;
 +      const char *color; /* NULL; filled in by the setup code if
 +                          * color is enabled */
 +};
 +
 +struct diff_words_style
 +{
 +      enum diff_words_type type;
 +      struct diff_words_style_elem new, old, ctx;
 +      const char *newline;
 +};
 +
 +struct diff_words_style diff_words_styles[] = {
 +      { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
 +      { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
 +      { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
 +};
 +
  struct diff_words_data {
        struct diff_words_buffer minus, plus;
        const char *current_plus;
 -      FILE *file;
 +      int last_minus;
 +      struct diff_options *opt;
        regex_t *word_regex;
 +      enum diff_words_type type;
 +      struct diff_words_style *style;
  };
  
 +static int fn_out_diff_words_write_helper(FILE *fp,
 +                                        struct diff_words_style_elem *st_el,
 +                                        const char *newline,
 +                                        size_t count, const char *buf,
 +                                        const char *line_prefix)
 +{
 +      int print = 0;
 +
 +      while (count) {
 +              char *p = memchr(buf, '\n', count);
 +              if (print)
 +                      fputs(line_prefix, fp);
 +              if (p != buf) {
 +                      if (st_el->color && fputs(st_el->color, fp) < 0)
 +                              return -1;
 +                      if (fputs(st_el->prefix, fp) < 0 ||
 +                          fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
 +                          fputs(st_el->suffix, fp) < 0)
 +                              return -1;
 +                      if (st_el->color && *st_el->color
 +                          && fputs(GIT_COLOR_RESET, fp) < 0)
 +                              return -1;
 +              }
 +              if (!p)
 +                      return 0;
 +              if (fputs(newline, fp) < 0)
 +                      return -1;
 +              count -= p + 1 - buf;
 +              buf = p + 1;
 +              print = 1;
 +      }
 +      return 0;
 +}
 +
 +/*
 + * '--color-words' algorithm can be described as:
 + *
 + *   1. collect a the minus/plus lines of a diff hunk, divided into
 + *      minus-lines and plus-lines;
 + *
 + *   2. break both minus-lines and plus-lines into words and
 + *      place them into two mmfile_t with one word for each line;
 + *
 + *   3. use xdiff to run diff on the two mmfile_t to get the words level diff;
 + *
 + * And for the common parts of the both file, we output the plus side text.
 + * diff_words->current_plus is used to trace the current position of the plus file
 + * which printed. diff_words->last_minus is used to trace the last minus word
 + * printed.
 + *
 + * For '--graph' to work with '--color-words', we need to output the graph prefix
 + * on each line of color words output. Generally, there are two conditions on
 + * which we should output the prefix.
 + *
 + *   1. diff_words->last_minus == 0 &&
 + *      diff_words->current_plus == diff_words->plus.text.ptr
 + *
 + *      that is: the plus text must start as a new line, and if there is no minus
 + *      word printed, a graph prefix must be printed.
 + *
 + *   2. diff_words->current_plus > diff_words->plus.text.ptr &&
 + *      *(diff_words->current_plus - 1) == '\n'
 + *
 + *      that is: a graph prefix must be printed following a '\n'
 + */
 +static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
 +{
 +      if ((diff_words->last_minus == 0 &&
 +              diff_words->current_plus == diff_words->plus.text.ptr) ||
 +              (diff_words->current_plus > diff_words->plus.text.ptr &&
 +              *(diff_words->current_plus - 1) == '\n')) {
 +              return 1;
 +      } else {
 +              return 0;
 +      }
 +}
 +
  static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
  {
        struct diff_words_data *diff_words = priv;
 +      struct diff_words_style *style = diff_words->style;
        int minus_first, minus_len, plus_first, plus_len;
        const char *minus_begin, *minus_end, *plus_begin, *plus_end;
 +      struct diff_options *opt = diff_words->opt;
 +      struct strbuf *msgbuf;
 +      char *line_prefix = "";
  
        if (line[0] != '@' || parse_hunk_header(line, len,
                        &minus_first, &minus_len, &plus_first, &plus_len))
                return;
  
 +      assert(opt);
 +      if (opt->output_prefix) {
 +              msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
 +
        /* POSIX requires that first be decremented by one if len == 0... */
        if (minus_len) {
                minus_begin = diff_words->minus.orig[minus_first].begin;
        } else
                plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
  
 -      if (diff_words->current_plus != plus_begin)
 -              fwrite(diff_words->current_plus,
 -                              plus_begin - diff_words->current_plus, 1,
 -                              diff_words->file);
 -      if (minus_begin != minus_end)
 -              color_fwrite_lines(diff_words->file,
 -                              diff_get_color(1, DIFF_FILE_OLD),
 -                              minus_end - minus_begin, minus_begin);
 -      if (plus_begin != plus_end)
 -              color_fwrite_lines(diff_words->file,
 -                              diff_get_color(1, DIFF_FILE_NEW),
 -                              plus_end - plus_begin, plus_begin);
 +      if (color_words_output_graph_prefix(diff_words)) {
 +              fputs(line_prefix, diff_words->opt->file);
 +      }
 +      if (diff_words->current_plus != plus_begin) {
 +              fn_out_diff_words_write_helper(diff_words->opt->file,
 +                              &style->ctx, style->newline,
 +                              plus_begin - diff_words->current_plus,
 +                              diff_words->current_plus, line_prefix);
 +              if (*(plus_begin - 1) == '\n')
 +                      fputs(line_prefix, diff_words->opt->file);
 +      }
 +      if (minus_begin != minus_end) {
 +              fn_out_diff_words_write_helper(diff_words->opt->file,
 +                              &style->old, style->newline,
 +                              minus_end - minus_begin, minus_begin,
 +                              line_prefix);
 +      }
 +      if (plus_begin != plus_end) {
 +              fn_out_diff_words_write_helper(diff_words->opt->file,
 +                              &style->new, style->newline,
 +                              plus_end - plus_begin, plus_begin,
 +                              line_prefix);
 +      }
  
        diff_words->current_plus = plus_end;
 +      diff_words->last_minus = minus_first;
  }
  
  /* This function starts looking at *begin, and returns 0 iff a word was found. */
@@@ -846,37 -701,23 +846,37 @@@ static void diff_words_show(struct diff
        xpparam_t xpp;
        xdemitconf_t xecfg;
        mmfile_t minus, plus;
 +      struct diff_words_style *style = diff_words->style;
 +
 +      struct diff_options *opt = diff_words->opt;
 +      struct strbuf *msgbuf;
 +      char *line_prefix = "";
 +
 +      assert(opt);
 +      if (opt->output_prefix) {
 +              msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
  
        /* special case: only removal */
        if (!diff_words->plus.text.size) {
 -              color_fwrite_lines(diff_words->file,
 -                      diff_get_color(1, DIFF_FILE_OLD),
 -                      diff_words->minus.text.size, diff_words->minus.text.ptr);
 +              fputs(line_prefix, diff_words->opt->file);
 +              fn_out_diff_words_write_helper(diff_words->opt->file,
 +                      &style->old, style->newline,
 +                      diff_words->minus.text.size,
 +                      diff_words->minus.text.ptr, line_prefix);
                diff_words->minus.text.size = 0;
                return;
        }
  
        diff_words->current_plus = diff_words->plus.text.ptr;
 +      diff_words->last_minus = 0;
  
        memset(&xpp, 0, sizeof(xpp));
        memset(&xecfg, 0, sizeof(xecfg));
        diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
        diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
 -      xpp.flags = XDF_NEED_MINIMAL;
 +      xpp.flags = 0;
        /* as only the hunk header will be parsed, we need a 0-context */
        xecfg.ctxlen = 0;
        xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
        free(minus.ptr);
        free(plus.ptr);
        if (diff_words->current_plus != diff_words->plus.text.ptr +
 -                      diff_words->plus.text.size)
 -              fwrite(diff_words->current_plus,
 +                      diff_words->plus.text.size) {
 +              if (color_words_output_graph_prefix(diff_words))
 +                      fputs(line_prefix, diff_words->opt->file);
 +              fn_out_diff_words_write_helper(diff_words->opt->file,
 +                      &style->ctx, style->newline,
                        diff_words->plus.text.ptr + diff_words->plus.text.size
 -                      - diff_words->current_plus, 1,
 -                      diff_words->file);
 +                      - diff_words->current_plus, diff_words->current_plus,
 +                      line_prefix);
 +      }
        diff_words->minus.text.size = diff_words->plus.text.size = 0;
  }
  
@@@ -964,17 -801,9 +964,17 @@@ static void fn_out_consume(void *priv, 
        const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
        const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
 +      struct diff_options *o = ecbdata->opt;
 +      char *line_prefix = "";
 +      struct strbuf *msgbuf;
 +
 +      if (o && o->output_prefix) {
 +              msgbuf = o->output_prefix(o, o->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
  
        if (ecbdata->header) {
 -              fprintf(ecbdata->file, "%s", ecbdata->header->buf);
 +              fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
                strbuf_reset(ecbdata->header);
                ecbdata->header = NULL;
        }
                name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
                name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
  
 -              fprintf(ecbdata->file, "%s--- %s%s%s\n",
 -                      meta, ecbdata->label_path[0], reset, name_a_tab);
 -              fprintf(ecbdata->file, "%s+++ %s%s%s\n",
 -                      meta, ecbdata->label_path[1], reset, name_b_tab);
 +              fprintf(ecbdata->opt->file, "%s%s--- %s%s%s\n",
 +                      line_prefix, meta, ecbdata->label_path[0], reset, name_a_tab);
 +              fprintf(ecbdata->opt->file, "%s%s+++ %s%s%s\n",
 +                      line_prefix, meta, ecbdata->label_path[1], reset, name_b_tab);
                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
        }
  
                find_lno(line, ecbdata);
                emit_hunk_header(ecbdata, line, len);
                if (line[len-1] != '\n')
 -                      putc('\n', ecbdata->file);
 +                      putc('\n', ecbdata->opt->file);
                return;
        }
  
        if (len < 1) {
 -              emit_line(ecbdata->file, reset, reset, line, len);
 +              emit_line(ecbdata->opt, reset, reset, line, len);
 +              if (ecbdata->diff_words
 +                  && ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
 +                      fputs("~\n", ecbdata->opt->file);
                return;
        }
  
                        return;
                }
                diff_words_flush(ecbdata);
 -              line++;
 -              len--;
 -              emit_line(ecbdata->file, plain, reset, line, len);
 +              if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
 +                      emit_line(ecbdata->opt, plain, reset, line, len);
 +                      fputs("~\n", ecbdata->opt->file);
 +              } else {
 +                      /* don't print the prefix character */
 +                      emit_line(ecbdata->opt, plain, reset, line+1, len-1);
 +              }
                return;
        }
  
                ecbdata->lno_in_preimage++;
                if (line[0] == ' ')
                        ecbdata->lno_in_postimage++;
 -              emit_line(ecbdata->file, color, reset, line, len);
 +              emit_line(ecbdata->opt, color, reset, line, len);
        } else {
                ecbdata->lno_in_postimage++;
                emit_add_line(reset, ecbdata, line + 1, len - 1);
@@@ -1226,17 -1048,10 +1226,17 @@@ static void show_stats(struct diffstat_
        int total_files = data->nr;
        int width, name_width;
        const char *reset, *set, *add_c, *del_c;
 +      const char *line_prefix = "";
 +      struct strbuf *msg = NULL;
  
        if (data->nr == 0)
                return;
  
 +      if (options->output_prefix) {
 +              msg = options->output_prefix(options, options->output_prefix_data);
 +              line_prefix = msg->buf;
 +      }
 +
        width = options->stat_width ? options->stat_width : 80;
        name_width = options->stat_name_width ? options->stat_name_width : 50;
  
                }
  
                if (data->files[i]->is_binary) {
 +                      fprintf(options->file, "%s", line_prefix);
                        show_name(options->file, prefix, name, len);
                        fprintf(options->file, "  Bin ");
                        fprintf(options->file, "%s%"PRIuMAX"%s",
                        continue;
                }
                else if (data->files[i]->is_unmerged) {
 +                      fprintf(options->file, "%s", line_prefix);
                        show_name(options->file, prefix, name, len);
                        fprintf(options->file, "  Unmerged\n");
                        continue;
                        add = scale_linear(add, width, max_change);
                        del = scale_linear(del, width, max_change);
                }
 +              fprintf(options->file, "%s", line_prefix);
                show_name(options->file, prefix, name, len);
                fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
                                added + deleted ? " " : "");
                show_graph(options->file, '-', del, del_c, reset);
                fprintf(options->file, "\n");
        }
 +      fprintf(options->file, "%s", line_prefix);
        fprintf(options->file,
               " %d files changed, %d insertions(+), %d deletions(-)\n",
               total_files, adds, dels);
@@@ -1377,12 -1188,6 +1377,12 @@@ static void show_shortstats(struct diff
                        }
                }
        }
 +      if (options->output_prefix) {
 +              struct strbuf *msg = NULL;
 +              msg = options->output_prefix(options,
 +                              options->output_prefix_data);
 +              fprintf(options->file, "%s", msg->buf);
 +      }
        fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
               total_files, adds, dels);
  }
@@@ -1397,13 -1202,6 +1397,13 @@@ static void show_numstat(struct diffsta
        for (i = 0; i < data->nr; i++) {
                struct diffstat_file *file = data->files[i];
  
 +              if (options->output_prefix) {
 +                      struct strbuf *msg = NULL;
 +                      msg = options->output_prefix(options,
 +                                      options->output_prefix_data);
 +                      fprintf(options->file, "%s", msg->buf);
 +              }
 +
                if (file->is_binary)
                        fprintf(options->file, "-\t-\t");
                else
@@@ -1439,18 -1237,10 +1439,18 @@@ struct dirstat_dir 
        int alloc, nr, percent, cumulative;
  };
  
 -static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
 +static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
 +              unsigned long changed, const char *base, int baselen)
  {
        unsigned long this_dir = 0;
        unsigned int sources = 0;
 +      const char *line_prefix = "";
 +      struct strbuf *msg = NULL;
 +
 +      if (opt->output_prefix) {
 +              msg = opt->output_prefix(opt, opt->output_prefix_data);
 +              line_prefix = msg->buf;
 +      }
  
        while (dir->nr) {
                struct dirstat_file *f = dir->files;
                slash = strchr(f->name + baselen, '/');
                if (slash) {
                        int newbaselen = slash + 1 - f->name;
 -                      this = gather_dirstat(file, dir, changed, f->name, newbaselen);
 +                      this = gather_dirstat(opt, dir, changed, f->name, newbaselen);
                        sources++;
                } else {
                        this = f->changed;
                if (permille) {
                        int percent = permille / 10;
                        if (percent >= dir->percent) {
 -                              fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
 +                              fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
 +                                      percent, permille % 10, baselen, base);
                                if (!dir->cumulative)
                                        return 0;
                        }
@@@ -1568,7 -1357,7 +1568,7 @@@ static void show_dirstat(struct diff_op
  
        /* Show all directories with more than x% of the changes */
        qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
 -      gather_dirstat(options->file, &dir, changed, "", 0);
 +      gather_dirstat(options, &dir, changed, "", 0);
  }
  
  static void free_diffstat_info(struct diffstat_t *diffstat)
@@@ -1626,15 -1415,6 +1626,15 @@@ static void checkdiff_consume(void *pri
        const char *reset = diff_get_color(color_diff, DIFF_RESET);
        const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
        char *err;
 +      char *line_prefix = "";
 +      struct strbuf *msgbuf;
 +
 +      assert(data->o);
 +      if (data->o->output_prefix) {
 +              msgbuf = data->o->output_prefix(data->o,
 +                      data->o->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
  
        if (line[0] == '+') {
                unsigned bad;
                if (is_conflict_marker(line + 1, marker_size, len - 1)) {
                        data->status |= 1;
                        fprintf(data->o->file,
 -                              "%s:%d: leftover conflict marker\n",
 -                              data->filename, data->lineno);
 +                              "%s%s:%d: leftover conflict marker\n",
 +                              line_prefix, data->filename, data->lineno);
                }
                bad = ws_check(line + 1, len - 1, data->ws_rule);
                if (!bad)
                        return;
                data->status |= bad;
                err = whitespace_error_string(bad);
 -              fprintf(data->o->file, "%s:%d: %s.\n",
 -                      data->filename, data->lineno, err);
 +              fprintf(data->o->file, "%s%s:%d: %s.\n",
 +                      line_prefix, data->filename, data->lineno, err);
                free(err);
 -              emit_line(data->o->file, set, reset, line, 1);
 +              emit_line(data->o, set, reset, line, 1);
                ws_check_emit(line + 1, len - 1, data->ws_rule,
                              data->o->file, set, reset, ws);
        } else if (line[0] == ' ') {
@@@ -1691,7 -1471,7 +1691,7 @@@ static unsigned char *deflate_it(char *
        return deflated;
  }
  
 -static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
 +static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
  {
        void *cp;
        void *delta;
        }
  
        if (delta && delta_size < deflate_size) {
 -              fprintf(file, "delta %lu\n", orig_size);
 +              fprintf(file, "%sdelta %lu\n", prefix, orig_size);
                free(deflated);
                data = delta;
                data_size = delta_size;
        }
        else {
 -              fprintf(file, "literal %lu\n", two->size);
 +              fprintf(file, "%sliteral %lu\n", prefix, two->size);
                free(delta);
                data = deflated;
                data_size = deflate_size;
                        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);
        }
 -      fprintf(file, "\n");
 +      fprintf(file, "%s\n", prefix);
        free(data);
  }
  
 -static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
 +static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two, char *prefix)
  {
 -      fprintf(file, "GIT binary patch\n");
 -      emit_binary_diff_body(file, one, two);
 -      emit_binary_diff_body(file, two, one);
 +      fprintf(file, "%sGIT binary patch\n", prefix);
 +      emit_binary_diff_body(file, one, two, prefix);
 +      emit_binary_diff_body(file, two, one, prefix);
  }
  
  static void diff_filespec_load_driver(struct diff_filespec *one)
@@@ -1806,26 -1585,14 +1806,26 @@@ void diff_set_mnemonic_prefix(struct di
                options->b_prefix = b;
  }
  
 -static const char *get_textconv(struct diff_filespec *one)
 +struct userdiff_driver *get_textconv(struct diff_filespec *one)
  {
        if (!DIFF_FILE_VALID(one))
                return NULL;
        if (!S_ISREG(one->mode))
                return NULL;
        diff_filespec_load_driver(one);
 -      return one->driver->textconv;
 +      if (!one->driver->textconv)
 +              return NULL;
 +
 +      if (one->driver->textconv_want_cache && !one->driver->textconv_cache) {
 +              struct notes_cache *c = xmalloc(sizeof(*c));
 +              struct strbuf name = STRBUF_INIT;
 +
 +              strbuf_addf(&name, "textconv/%s", one->driver->name);
 +              notes_cache_init(c, name.buf, one->driver->textconv);
 +              one->driver->textconv_cache = c;
 +      }
 +
 +      return one->driver;
  }
  
  static void builtin_diff(const char *name_a,
                         struct diff_filespec *one,
                         struct diff_filespec *two,
                         const char *xfrm_msg,
 +                       int must_show_header,
                         struct diff_options *o,
                         int complete_rewrite)
  {
        const char *set = diff_get_color_opt(o, DIFF_METAINFO);
        const char *reset = diff_get_color_opt(o, DIFF_RESET);
        const char *a_prefix, *b_prefix;
 -      const char *textconv_one = NULL, *textconv_two = NULL;
 +      struct userdiff_driver *textconv_one = NULL;
 +      struct userdiff_driver *textconv_two = NULL;
        struct strbuf header = STRBUF_INIT;
 +      struct strbuf *msgbuf;
 +      char *line_prefix = "";
 +
 +      if (o->output_prefix) {
 +              msgbuf = o->output_prefix(o, o->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
  
        if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
                        (!one->mode || S_ISGITLINK(one->mode)) &&
        b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
        lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
        lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
 -      strbuf_addf(&header, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
 +      strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, set, a_one, b_two, reset);
        if (lbl[0][0] == '/') {
                /* /dev/null */
 -              strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
 -              if (xfrm_msg && xfrm_msg[0])
 -                      strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
 +              strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, set, two->mode, reset);
 +              if (xfrm_msg)
 +                      strbuf_addstr(&header, xfrm_msg);
 +              must_show_header = 1;
        }
        else if (lbl[1][0] == '/') {
 -              strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
 -              if (xfrm_msg && xfrm_msg[0])
 -                      strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
 +              strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, set, one->mode, reset);
 +              if (xfrm_msg)
 +                      strbuf_addstr(&header, xfrm_msg);
 +              must_show_header = 1;
        }
        else {
                if (one->mode != two->mode) {
 -                      strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
 -                      strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
 +                      strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, set, one->mode, reset);
 +                      strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, set, two->mode, reset);
 +                      must_show_header = 1;
                }
 -              if (xfrm_msg && xfrm_msg[0])
 -                      strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
 +              if (xfrm_msg)
 +                      strbuf_addstr(&header, xfrm_msg);
  
                /*
                 * we do not run diff between different kind
                }
        }
  
 -      if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 -              die("unable to read files to diff");
 -
        if (!DIFF_OPT_TST(o, TEXT) &&
 -          ( (diff_filespec_is_binary(one) && !textconv_one) ||
 -            (diff_filespec_is_binary(two) && !textconv_two) )) {
 +          ( (!textconv_one && diff_filespec_is_binary(one)) ||
 +            (!textconv_two && diff_filespec_is_binary(two)) )) {
 +              if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 +                      die("unable to read files to diff");
                /* Quite common confusing case */
                if (mf1.size == mf2.size &&
 -                  !memcmp(mf1.ptr, mf2.ptr, mf1.size))
 +                  !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
 +                      if (must_show_header)
 +                              fprintf(o->file, "%s", header.buf);
                        goto free_ab_and_return;
 +              }
                fprintf(o->file, "%s", header.buf);
                strbuf_reset(&header);
                if (DIFF_OPT_TST(o, BINARY))
 -                      emit_binary_diff(o->file, &mf1, &mf2);
 +                      emit_binary_diff(o->file, &mf1, &mf2, line_prefix);
                else
 -                      fprintf(o->file, "Binary files %s and %s differ\n",
 -                              lbl[0], lbl[1]);
 +                      fprintf(o->file, "%sBinary files %s and %s differ\n",
 +                              line_prefix, lbl[0], lbl[1]);
                o->found_changes = 1;
        }
        else {
                struct emit_callback ecbdata;
                const struct userdiff_funcname *pe;
  
 -              if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
 +              if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
                        fprintf(o->file, "%s", header.buf);
                        strbuf_reset(&header);
                }
  
 -              if (textconv_one) {
 -                      size_t size;
 -                      mf1.ptr = run_textconv(textconv_one, one, &size);
 -                      if (!mf1.ptr)
 -                              die("unable to read files to diff");
 -                      mf1.size = size;
 -              }
 -              if (textconv_two) {
 -                      size_t size;
 -                      mf2.ptr = run_textconv(textconv_two, two, &size);
 -                      if (!mf2.ptr)
 -                              die("unable to read files to diff");
 -                      mf2.size = size;
 -              }
 +              mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
 +              mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
  
                pe = diff_funcname_pattern(one);
                if (!pe)
                ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
                if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
                        check_blank_at_eof(&mf1, &mf2, &ecbdata);
 -              ecbdata.file = o->file;
 +              ecbdata.opt = o;
                ecbdata.header = header.len ? &header : NULL;
 -              xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
 +              xpp.flags = o->xdl_opts;
                xecfg.ctxlen = o->context;
                xecfg.interhunkctxlen = o->interhunkcontext;
                xecfg.flags = XDL_EMIT_FUNCNAMES;
                        xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
                else if (!prefixcmp(diffopts, "-u"))
                        xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
 -              if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
 +              if (o->word_diff) {
 +                      int i;
 +
                        ecbdata.diff_words =
                                xcalloc(1, sizeof(struct diff_words_data));
 -                      ecbdata.diff_words->file = o->file;
 +                      ecbdata.diff_words->type = o->word_diff;
 +                      ecbdata.diff_words->opt = o;
                        if (!o->word_regex)
                                o->word_regex = userdiff_word_regex(one);
                        if (!o->word_regex)
                                        die ("Invalid regular expression: %s",
                                                        o->word_regex);
                        }
 +                      for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
 +                              if (o->word_diff == diff_words_styles[i].type) {
 +                                      ecbdata.diff_words->style =
 +                                              &diff_words_styles[i];
 +                                      break;
 +                              }
 +                      }
 +                      if (DIFF_OPT_TST(o, COLOR_DIFF)) {
 +                              struct diff_words_style *st = ecbdata.diff_words->style;
 +                              st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
 +                              st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
 +                              st->ctx.color = diff_get_color_opt(o, DIFF_PLAIN);
 +                      }
                }
                xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
                              &xpp, &xecfg);
 -              if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
 +              if (o->word_diff)
                        free_diff_words_data(&ecbdata);
                if (textconv_one)
                        free(mf1.ptr);
@@@ -2085,7 -1834,7 +2085,7 @@@ static void builtin_diffstat(const cha
  
                memset(&xpp, 0, sizeof(xpp));
                memset(&xecfg, 0, sizeof(xecfg));
 -              xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
 +              xpp.flags = o->xdl_opts;
                xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
                              &xpp, &xecfg);
        }
@@@ -2133,7 -1882,7 +2133,7 @@@ static void builtin_checkdiff(const cha
                memset(&xpp, 0, sizeof(xpp));
                memset(&xecfg, 0, sizeof(xecfg));
                xecfg.ctxlen = 1; /* at least one context line */
 -              xpp.flags = XDF_NEED_MINIMAL;
 +              xpp.flags = 0;
                xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
                              &xpp, &xecfg);
  
@@@ -2570,53 -2319,36 +2570,53 @@@ static void fill_metainfo(struct strbu
                          struct diff_filespec *one,
                          struct diff_filespec *two,
                          struct diff_options *o,
 -                        struct diff_filepair *p)
 +                        struct diff_filepair *p,
 +                        int *must_show_header,
 +                        int use_color)
  {
 +      const char *set = diff_get_color(use_color, DIFF_METAINFO);
 +      const char *reset = diff_get_color(use_color, DIFF_RESET);
 +      struct strbuf *msgbuf;
 +      char *line_prefix = "";
 +
 +      *must_show_header = 1;
 +      if (o->output_prefix) {
 +              msgbuf = o->output_prefix(o, o->output_prefix_data);
 +              line_prefix = msgbuf->buf;
 +      }
        strbuf_init(msg, PATH_MAX * 2 + 300);
        switch (p->status) {
        case DIFF_STATUS_COPIED:
 -              strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
 -              strbuf_addstr(msg, "\ncopy from ");
 +              strbuf_addf(msg, "%s%ssimilarity index %d%%",
 +                          line_prefix, set, similarity_index(p));
 +              strbuf_addf(msg, "%s\n%s%scopy from ",
 +                          reset,  line_prefix, set);
                quote_c_style(name, msg, NULL, 0);
 -              strbuf_addstr(msg, "\ncopy to ");
 +              strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
                quote_c_style(other, msg, NULL, 0);
 -              strbuf_addch(msg, '\n');
 +              strbuf_addf(msg, "%s\n", reset);
                break;
        case DIFF_STATUS_RENAMED:
 -              strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
 -              strbuf_addstr(msg, "\nrename from ");
 +              strbuf_addf(msg, "%s%ssimilarity index %d%%",
 +                          line_prefix, set, similarity_index(p));
 +              strbuf_addf(msg, "%s\n%s%srename from ",
 +                          reset, line_prefix, set);
                quote_c_style(name, msg, NULL, 0);
 -              strbuf_addstr(msg, "\nrename to ");
 +              strbuf_addf(msg, "%s\n%s%srename to ",
 +                          reset, line_prefix, set);
                quote_c_style(other, msg, NULL, 0);
 -              strbuf_addch(msg, '\n');
 +              strbuf_addf(msg, "%s\n", reset);
                break;
        case DIFF_STATUS_MODIFIED:
                if (p->score) {
 -                      strbuf_addf(msg, "dissimilarity index %d%%\n",
 -                                  similarity_index(p));
 +                      strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
 +                                  line_prefix,
 +                                  set, similarity_index(p), reset);
                        break;
                }
                /* fallthru */
        default:
 -              /* nothing */
 -              ;
 +              *must_show_header = 0;
        }
        if (one && two && hashcmp(one->sha1, two->sha1)) {
                int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
                            (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
                                abbrev = 40;
                }
 -              strbuf_addf(msg, "index %.*s..%.*s",
 -                          abbrev, sha1_to_hex(one->sha1),
 -                          abbrev, sha1_to_hex(two->sha1));
 +              strbuf_addf(msg, "%s%sindex %s..", set,
 +                          line_prefix,
 +                          find_unique_abbrev(one->sha1, abbrev));
 +              strbuf_addstr(msg, find_unique_abbrev(two->sha1, abbrev));
                if (one->mode == two->mode)
                        strbuf_addf(msg, " %06o", one->mode);
 -              strbuf_addch(msg, '\n');
 +              strbuf_addf(msg, "%s\n", reset);
        }
 -      if (msg->len)
 -              strbuf_setlen(msg, msg->len - 1);
  }
  
  static void run_diff_cmd(const char *pgm,
  {
        const char *xfrm_msg = NULL;
        int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
 -
 -      if (msg) {
 -              fill_metainfo(msg, name, other, one, two, o, p);
 -              xfrm_msg = msg->len ? msg->buf : NULL;
 -      }
 +      int must_show_header = 0;
  
        if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
                pgm = NULL;
                        pgm = drv->external;
        }
  
 +      if (msg) {
 +              /*
 +               * don't use colors when the header is intended for an
 +               * external diff driver
 +               */
 +              fill_metainfo(msg, name, other, one, two, o, p,
 +                            &must_show_header,
 +                            DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
 +              xfrm_msg = msg->len ? msg->buf : NULL;
 +      }
 +
        if (pgm) {
                run_external_diff(pgm, name, other, one, two, xfrm_msg,
                                  complete_rewrite);
        }
        if (one && two)
                builtin_diff(name, other ? other : name,
 -                           one, two, xfrm_msg, o, complete_rewrite);
 +                           one, two, xfrm_msg, must_show_header,
 +                           o, complete_rewrite);
        else
                fprintf(o->file, "* Unmerged path %s\n", name);
  }
@@@ -2815,7 -2540,6 +2815,7 @@@ static void run_checkdiff(struct diff_f
  void diff_setup(struct diff_options *options)
  {
        memset(options, 0, sizeof(*options));
 +      memset(&diff_queued_diff, 0, sizeof(diff_queued_diff));
  
        options->file = stdout;
  
                DIFF_OPT_SET(options, COLOR_DIFF);
        options->detect_rename = diff_detect_rename_default;
  
 -      if (!diff_mnemonic_prefix) {
 +      if (diff_no_prefix) {
 +              options->a_prefix = options->b_prefix = "";
 +      } else if (!diff_mnemonic_prefix) {
                options->a_prefix = "a/";
                options->b_prefix = "b/";
        }
@@@ -2996,7 -2718,7 +2996,7 @@@ int diff_opt_parse(struct diff_options 
        const char *arg = av[0];
  
        /* Output format options */
 -      if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
 +      if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
                options->output_format |= DIFF_FORMAT_PATCH;
        else if (opt_arg(arg, 'U', "unified", &options->context))
                options->output_format |= DIFF_FORMAT_PATCH;
                DIFF_OPT_CLR(options, COLOR_DIFF);
        else if (!strcmp(arg, "--color-words")) {
                DIFF_OPT_SET(options, COLOR_DIFF);
 -              DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
 +              options->word_diff = DIFF_WORDS_COLOR;
        }
        else if (!prefixcmp(arg, "--color-words=")) {
                DIFF_OPT_SET(options, COLOR_DIFF);
 -              DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
 +              options->word_diff = DIFF_WORDS_COLOR;
                options->word_regex = arg + 14;
        }
 +      else if (!strcmp(arg, "--word-diff")) {
 +              if (options->word_diff == DIFF_WORDS_NONE)
 +                      options->word_diff = DIFF_WORDS_PLAIN;
 +      }
 +      else if (!prefixcmp(arg, "--word-diff=")) {
 +              const char *type = arg + 12;
 +              if (!strcmp(type, "plain"))
 +                      options->word_diff = DIFF_WORDS_PLAIN;
 +              else if (!strcmp(type, "color")) {
 +                      DIFF_OPT_SET(options, COLOR_DIFF);
 +                      options->word_diff = DIFF_WORDS_COLOR;
 +              }
 +              else if (!strcmp(type, "porcelain"))
 +                      options->word_diff = DIFF_WORDS_PORCELAIN;
 +              else if (!strcmp(type, "none"))
 +                      options->word_diff = DIFF_WORDS_NONE;
 +              else
 +                      die("bad --word-diff argument: %s", type);
 +      }
 +      else if (!prefixcmp(arg, "--word-diff-regex=")) {
 +              if (options->word_diff == DIFF_WORDS_NONE)
 +                      options->word_diff = DIFF_WORDS_PLAIN;
 +              options->word_regex = arg + 18;
 +      }
        else if (!strcmp(arg, "--exit-code"))
                DIFF_OPT_SET(options, EXIT_WITH_STATUS);
        else if (!strcmp(arg, "--quiet"))
                DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
        else if (!strcmp(arg, "--ignore-submodules"))
                DIFF_OPT_SET(options, IGNORE_SUBMODULES);
-       else if (!strcmp(arg, "--submodule"))
+       else if (!prefixcmp(arg, "--ignore-submodules=")) {
+               if (!strcmp(arg + 20, "all"))
+                       DIFF_OPT_SET(options, IGNORE_SUBMODULES);
+               else if (!strcmp(arg + 20, "untracked"))
+                       DIFF_OPT_SET(options, IGNORE_UNTRACKED_IN_SUBMODULES);
+               else if (!strcmp(arg + 20, "dirty"))
+                       DIFF_OPT_SET(options, IGNORE_DIRTY_SUBMODULES);
+               else
+                       die("bad --ignore-submodules argument: %s", arg + 20);
+       } else if (!strcmp(arg, "--submodule"))
                DIFF_OPT_SET(options, SUBMODULE_LOG);
        else if (!prefixcmp(arg, "--submodule=")) {
                if (!strcmp(arg + 12, "log"))
@@@ -3341,11 -3048,6 +3350,11 @@@ static void diff_flush_raw(struct diff_
  {
        int line_termination = opt->line_termination;
        int inter_name_termination = line_termination ? '\t' : '\0';
 +      if (opt->output_prefix) {
 +              struct strbuf *msg = NULL;
 +              msg = opt->output_prefix(opt, opt->output_prefix_data);
 +              fprintf(opt->file, "%s", msg->buf);
 +      }
  
        if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
                fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
@@@ -3591,62 -3293,48 +3600,62 @@@ static void show_file_mode_name(FILE *f
  }
  
  
 -static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
 +static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name,
 +              const char *line_prefix)
  {
        if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
 -              fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
 -                      show_name ? ' ' : '\n');
 +              fprintf(file, "%s mode change %06o => %06o%c", line_prefix, p->one->mode,
 +                      p->two->mode, show_name ? ' ' : '\n');
                if (show_name) {
                        write_name_quoted(p->two->path, file, '\n');
                }
        }
  }
  
 -static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
 +static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p,
 +                      const char *line_prefix)
  {
        char *names = pprint_rename(p->one->path, p->two->path);
  
        fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
        free(names);
 -      show_mode_change(file, p, 0);
 +      show_mode_change(file, p, 0, line_prefix);
  }
  
 -static void diff_summary(FILE *file, struct diff_filepair *p)
 +static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
  {
 +      FILE *file = opt->file;
 +      char *line_prefix = "";
 +
 +      if (opt->output_prefix) {
 +              struct strbuf *buf = opt->output_prefix(opt, opt->output_prefix_data);
 +              line_prefix = buf->buf;
 +      }
 +
        switch(p->status) {
        case DIFF_STATUS_DELETED:
 +              fputs(line_prefix, file);
                show_file_mode_name(file, "delete", p->one);
                break;
        case DIFF_STATUS_ADDED:
 +              fputs(line_prefix, file);
                show_file_mode_name(file, "create", p->two);
                break;
        case DIFF_STATUS_COPIED:
 -              show_rename_copy(file, "copy", p);
 +              fputs(line_prefix, file);
 +              show_rename_copy(file, "copy", p, line_prefix);
                break;
        case DIFF_STATUS_RENAMED:
 -              show_rename_copy(file, "rename", p);
 +              fputs(line_prefix, file);
 +              show_rename_copy(file, "rename", p, line_prefix);
                break;
        default:
                if (p->score) {
 -                      fputs(" rewrite ", file);
 +                      fprintf(file, "%s rewrite ", line_prefix);
                        write_name_quoted(p->two->path, file, ' ');
                        fprintf(file, "(%d%%)\n", similarity_index(p));
                }
 -              show_mode_change(file, p, !p->score);
 +              show_mode_change(file, p, !p->score, line_prefix);
                break;
        }
  }
@@@ -3757,7 -3445,7 +3766,7 @@@ static int diff_get_patch_id(struct dif
                                        len2, p->two->path);
                git_SHA1_Update(&ctx, buffer, len1);
  
 -              xpp.flags = XDF_NEED_MINIMAL;
 +              xpp.flags = 0;
                xecfg.ctxlen = 3;
                xecfg.flags = XDL_EMIT_FUNCNAMES;
                xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
@@@ -3778,7 -3466,8 +3787,7 @@@ int diff_flush_patch_id(struct diff_opt
                diff_free_filepair(q->queue[i]);
  
        free(q->queue);
 -      q->queue = NULL;
 -      q->nr = q->alloc = 0;
 +      DIFF_QUEUE_CLEAR(q);
  
        return result;
  }
@@@ -3855,9 -3544,8 +3864,9 @@@ void diff_flush(struct diff_options *op
                show_dirstat(options);
  
        if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
 -              for (i = 0; i < q->nr; i++)
 -                      diff_summary(options->file, q->queue[i]);
 +              for (i = 0; i < q->nr; i++) {
 +                      diff_summary(options, q->queue[i]);
 +              }
                separator++;
        }
  
                diff_free_filepair(q->queue[i]);
  free_queue:
        free(q->queue);
 -      q->queue = NULL;
 -      q->nr = q->alloc = 0;
 +      DIFF_QUEUE_CLEAR(q);
        if (options->close_file)
                fclose(options->file);
  
@@@ -3929,7 -3618,8 +3938,7 @@@ static void diffcore_apply_filter(cons
        int i;
        struct diff_queue_struct *q = &diff_queued_diff;
        struct diff_queue_struct outq;
 -      outq.queue = NULL;
 -      outq.nr = outq.alloc = 0;
 +      DIFF_QUEUE_CLEAR(&outq);
  
        if (!filter)
                return;
@@@ -3997,7 -3687,8 +4006,7 @@@ static void diffcore_skip_stat_unmatch(
        int i;
        struct diff_queue_struct *q = &diff_queued_diff;
        struct diff_queue_struct outq;
 -      outq.queue = NULL;
 -      outq.nr = outq.alloc = 0;
 +      DIFF_QUEUE_CLEAR(&outq);
  
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
@@@ -4058,12 -3749,6 +4067,12 @@@ void diffcore_fix_diff_index(struct dif
  
  void diffcore_std(struct diff_options *options)
  {
 +      /* We never run this function more than one time, because the
 +       * rename/copy detection logic can only run once.
 +       */
 +      if (diff_queued_diff.run)
 +              return;
 +
        if (options->skip_stat_unmatch)
                diffcore_skip_stat_unmatch(options);
        if (options->break_opt != -1)
                DIFF_OPT_SET(options, HAS_CHANGES);
        else
                DIFF_OPT_CLR(options, HAS_CHANGES);
 +
 +      diff_queued_diff.run = 1;
  }
  
  int diff_result_code(struct diff_options *opt, int status)
@@@ -4238,47 -3921,3 +4247,47 @@@ static char *run_textconv(const char *p
  
        return strbuf_detach(&buf, outsize);
  }
 +
 +size_t fill_textconv(struct userdiff_driver *driver,
 +                   struct diff_filespec *df,
 +                   char **outbuf)
 +{
 +      size_t size;
 +
 +      if (!driver || !driver->textconv) {
 +              if (!DIFF_FILE_VALID(df)) {
 +                      *outbuf = "";
 +                      return 0;
 +              }
 +              if (diff_populate_filespec(df, 0))
 +                      die("unable to read files to diff");
 +              *outbuf = df->data;
 +              return df->size;
 +      }
 +
 +      if (driver->textconv_cache) {
 +              *outbuf = notes_cache_get(driver->textconv_cache, df->sha1,
 +                                        &size);
 +              if (*outbuf)
 +                      return size;
 +      }
 +
 +      *outbuf = run_textconv(driver->textconv, df, &size);
 +      if (!*outbuf)
 +              die("unable to read files to diff");
 +
 +      if (driver->textconv_cache) {
 +              /* ignore errors, as we might be in a readonly repository */
 +              notes_cache_put(driver->textconv_cache, df->sha1, *outbuf,
 +                              size);
 +              /*
 +               * we could save up changes and flush them all at the end,
 +               * but we would need an extra call after all diffing is done.
 +               * Since generating a cache entry is the slow path anyway,
 +               * this extra overhead probably isn't a big deal.
 +               */
 +              notes_cache_write(driver->textconv_cache);
 +      }
 +
 +      return size;
 +}