Merge branch 'po/object-id' into next
authorJunio C Hamano <gitster@pobox.com>
Wed, 23 Aug 2017 21:37:58 +0000 (14:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 23 Aug 2017 21:37:59 +0000 (14:37 -0700)
* po/object-id:
sha1_file: convert index_stream to struct object_id
sha1_file: convert hash_sha1_file_literally to struct object_id
sha1_file: convert index_fd to struct object_id
sha1_file: convert index_path to struct object_id
read-cache: convert to struct object_id
builtin/hash-object: convert to struct object_id

1  2 
cache.h
diff.c
sha1_file.c
diff --combined cache.h
index 6ef99e7486c3bc1e3f5bd7e7510c9d201a9a30d6,237adb59d48112d7518c7f26686527da9a1a6642..bd8802af0e0c0e05afc8e3770bb123fc346636a3
+++ b/cache.h
@@@ -417,6 -417,7 +417,6 @@@ static inline enum object_type object_t
  #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
  #define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
  #define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
 -#define GIT_TOPLEVEL_PREFIX_ENVIRONMENT "GIT_INTERNAL_TOPLEVEL_PREFIX"
  #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
  #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
  #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
  #define GITATTRIBUTES_FILE ".gitattributes"
  #define INFOATTRIBUTES_FILE "info/attributes"
  #define ATTRIBUTE_MACRO_PREFIX "[attr]"
 +#define GITMODULES_FILE ".gitmodules"
  #define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
  #define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
  #define GIT_NOTES_DISPLAY_REF_ENVIRONMENT "GIT_NOTES_DISPLAY_REF"
@@@ -684,8 -684,8 +684,8 @@@ extern int ie_modified(const struct ind
  
  #define HASH_WRITE_OBJECT 1
  #define HASH_FORMAT_CHECK 2
- extern int index_fd(unsigned char *sha1, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
- extern int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags);
+ extern int index_fd(struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
+ extern int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags);
  
  /*
   * Record to sd the data from st that we use to check whether a file
@@@ -939,7 -939,14 +939,7 @@@ extern const struct object_id null_oid
  
  static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
  {
 -      int i;
 -
 -      for (i = 0; i < GIT_SHA1_RAWSZ; i++, sha1++, sha2++) {
 -              if (*sha1 != *sha2)
 -                      return *sha1 - *sha2;
 -      }
 -
 -      return 0;
 +      return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
  }
  
  static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
@@@ -1192,7 -1199,7 +1192,7 @@@ static inline const unsigned char *look
  extern int sha1_object_info(const unsigned char *, unsigned long *);
  extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
  extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
- extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
+ extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, struct object_id *oid, unsigned flags);
  extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
  extern int force_object_loose(const unsigned char *sha1, time_t mtime);
  extern int git_open_cloexec(const char *name, int flags);
@@@ -1544,6 -1551,7 +1544,6 @@@ extern struct alternate_object_databas
        char path[FLEX_ARRAY];
  } *alt_odb_list;
  extern void prepare_alt_odb(void);
 -extern void read_info_alternates(const char * relative_base, int depth);
  extern char *compute_alternate_path(const char *path, struct strbuf *err);
  typedef int alt_odb_fn(struct alternate_object_database *, void *);
  extern int foreach_alt_odb(alt_odb_fn, void*);
@@@ -1955,8 -1963,6 +1955,8 @@@ void shift_tree_by(const struct object_
  #define WS_TRAILING_SPACE      (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
  #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
  #define WS_TAB_WIDTH_MASK        077
 +/* All WS_* -- when extended, adapt diff.c emit_symbol */
 +#define WS_RULE_MASK           07777
  extern unsigned whitespace_rule_cfg;
  extern unsigned whitespace_rule(const char *);
  extern unsigned parse_whitespace_rule(const char *);
diff --combined diff.c
index ac4023d30b81cc316393b46572d6808b68728127,65f8d1367044f039833c9611b7b2c25a9f3158de..24bec41114de0364c7b1ea6e2c692dd94e17eea2
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -16,7 -16,6 +16,7 @@@
  #include "userdiff.h"
  #include "submodule-config.h"
  #include "submodule.h"
 +#include "hashmap.h"
  #include "ll-merge.h"
  #include "string-list.h"
  #include "argv-array.h"
@@@ -33,7 -32,6 +33,7 @@@ static int diff_indent_heuristic = 1
  static int diff_rename_limit_default = 400;
  static int diff_suppress_blank_empty;
  static int diff_use_color_default = -1;
 +static int diff_color_moved_default;
  static int diff_context_default = 3;
  static int diff_interhunk_context_default;
  static const char *diff_word_regex_cfg;
@@@ -58,14 -56,6 +58,14 @@@ static char diff_colors[][COLOR_MAXLEN
        GIT_COLOR_YELLOW,       /* COMMIT */
        GIT_COLOR_BG_RED,       /* WHITESPACE */
        GIT_COLOR_NORMAL,       /* FUNCINFO */
 +      GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
 +      GIT_COLOR_BOLD_BLUE,    /* OLD_MOVED ALTERNATIVE */
 +      GIT_COLOR_FAINT,        /* OLD_MOVED_DIM */
 +      GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
 +      GIT_COLOR_BOLD_CYAN,    /* NEW_MOVED */
 +      GIT_COLOR_BOLD_YELLOW,  /* NEW_MOVED ALTERNATIVE */
 +      GIT_COLOR_FAINT,        /* NEW_MOVED_DIM */
 +      GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
  };
  
  static NORETURN void die_want_option(const char *option_name)
@@@ -91,22 -81,6 +91,22 @@@ static int parse_diff_color_slot(const 
                return DIFF_WHITESPACE;
        if (!strcasecmp(var, "func"))
                return DIFF_FUNCINFO;
 +      if (!strcasecmp(var, "oldmoved"))
 +              return DIFF_FILE_OLD_MOVED;
 +      if (!strcasecmp(var, "oldmovedalternative"))
 +              return DIFF_FILE_OLD_MOVED_ALT;
 +      if (!strcasecmp(var, "oldmoveddimmed"))
 +              return DIFF_FILE_OLD_MOVED_DIM;
 +      if (!strcasecmp(var, "oldmovedalternativedimmed"))
 +              return DIFF_FILE_OLD_MOVED_ALT_DIM;
 +      if (!strcasecmp(var, "newmoved"))
 +              return DIFF_FILE_NEW_MOVED;
 +      if (!strcasecmp(var, "newmovedalternative"))
 +              return DIFF_FILE_NEW_MOVED_ALT;
 +      if (!strcasecmp(var, "newmoveddimmed"))
 +              return DIFF_FILE_NEW_MOVED_DIM;
 +      if (!strcasecmp(var, "newmovedalternativedimmed"))
 +              return DIFF_FILE_NEW_MOVED_ALT_DIM;
        return -1;
  }
  
@@@ -255,44 -229,12 +255,44 @@@ int git_diff_heuristic_config(const cha
        return 0;
  }
  
 +static int parse_color_moved(const char *arg)
 +{
 +      switch (git_parse_maybe_bool(arg)) {
 +      case 0:
 +              return COLOR_MOVED_NO;
 +      case 1:
 +              return COLOR_MOVED_DEFAULT;
 +      default:
 +              break;
 +      }
 +
 +      if (!strcmp(arg, "no"))
 +              return COLOR_MOVED_NO;
 +      else if (!strcmp(arg, "plain"))
 +              return COLOR_MOVED_PLAIN;
 +      else if (!strcmp(arg, "zebra"))
 +              return COLOR_MOVED_ZEBRA;
 +      else if (!strcmp(arg, "default"))
 +              return COLOR_MOVED_DEFAULT;
 +      else if (!strcmp(arg, "dimmed_zebra"))
 +              return COLOR_MOVED_ZEBRA_DIM;
 +      else
 +              return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
 +}
 +
  int git_diff_ui_config(const char *var, const char *value, void *cb)
  {
        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
                diff_use_color_default = git_config_colorbool(var, value);
                return 0;
        }
 +      if (!strcmp(var, "diff.colormoved")) {
 +              int cm = parse_color_moved(value);
 +              if (cm < 0)
 +                      return -1;
 +              diff_color_moved_default = cm;
 +              return 0;
 +      }
        if (!strcmp(var, "diff.context")) {
                diff_context_default = git_config_int(var, value);
                if (diff_context_default < 0)
@@@ -464,6 -406,8 +464,6 @@@ static struct diff_tempfile 
        struct tempfile tempfile;
  } diff_temp[2];
  
 -typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
 -
  struct emit_callback {
        int color_diff;
        unsigned ws_rule;
        int blank_at_eof_in_postimage;
        int lno_in_preimage;
        int lno_in_postimage;
 -      sane_truncate_fn truncate;
        const char **label_path;
        struct diff_words_data *diff_words;
        struct diff_options *opt;
@@@ -612,735 -557,68 +612,735 @@@ static void emit_line(struct diff_optio
        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)
 +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_WORD_DIFF,
 +      DIFF_SYMBOL_STAT_SEP,
 +      DIFF_SYMBOL_SUMMARY,
 +      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_MOVED_LINE                        (1<<17)
 +#define DIFF_SYMBOL_MOVED_LINE_ALT            (1<<18)
 +#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING  (1<<19)
 +#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
 +
 +/*
 + * This struct is used when we need to buffer the output of the diff output.
 + *
 + * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
 + * into the pre/post image file. This pointer could be a union with the
 + * line pointer. By storing an offset into the file instead of the literal line,
 + * we can decrease the memory footprint for the buffered output. At first we
 + * may want to only have indirection for the content lines, but we could also
 + * enhance the state for emitting prefabricated lines, e.g. the similarity
 + * score line or hunk/file headers would only need to store a number or path
 + * and then the output can be constructed later on depending on state.
 + */
 +struct emitted_diff_symbol {
 +      const char *line;
 +      int len;
 +      int flags;
 +      enum diff_symbol s;
 +};
 +#define EMITTED_DIFF_SYMBOL_INIT {NULL}
 +
 +struct emitted_diff_symbols {
 +      struct emitted_diff_symbol *buf;
 +      int nr, alloc;
 +};
 +#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
 +
 +static void append_emitted_diff_symbol(struct diff_options *o,
 +                                     struct emitted_diff_symbol *e)
  {
 -      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);
 +      struct emitted_diff_symbol *f;
 +
 +      ALLOC_GROW(o->emitted_symbols->buf,
 +                 o->emitted_symbols->nr + 1,
 +                 o->emitted_symbols->alloc);
 +      f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
 +
 +      memcpy(f, e, sizeof(struct emitted_diff_symbol));
 +      f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
  }
  
 -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)
 +struct moved_entry {
 +      struct hashmap_entry ent;
 +      const struct emitted_diff_symbol *es;
 +      struct moved_entry *next_line;
 +};
 +
 +static int next_byte(const char **cp, const char **endp,
 +                   const struct diff_options *diffopt)
 +{
 +      int retval;
 +
 +      if (*cp > *endp)
 +              return -1;
 +
 +      if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
 +              while (*cp < *endp && isspace(**cp))
 +                      (*cp)++;
 +              /*
 +               * After skipping a couple of whitespaces, we still have to
 +               * account for one space.
 +               */
 +              return (int)' ';
 +      }
 +
 +      if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
 +              while (*cp < *endp && isspace(**cp))
 +                      (*cp)++;
 +              /* return the first non-ws character via the usual below */
 +      }
 +
 +      retval = (unsigned char)(**cp);
 +      (*cp)++;
 +      return retval;
 +}
 +
 +static int moved_entry_cmp(const struct diff_options *diffopt,
 +                         const struct moved_entry *a,
 +                         const struct moved_entry *b,
 +                         const void *keydata)
 +{
 +      const char *ap = a->es->line, *ae = a->es->line + a->es->len;
 +      const char *bp = b->es->line, *be = b->es->line + b->es->len;
 +
 +      if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
 +              return a->es->len != b->es->len  || memcmp(ap, bp, a->es->len);
 +
 +      if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
 +              while (ae > ap && isspace(*ae))
 +                      ae--;
 +              while (be > bp && isspace(*be))
 +                      be--;
 +      }
 +
 +      while (1) {
 +              int ca, cb;
 +              ca = next_byte(&ap, &ae, diffopt);
 +              cb = next_byte(&bp, &be, diffopt);
 +              if (ca != cb)
 +                      return 1;
 +              if (ca < 0)
 +                      return 0;
 +      }
 +}
 +
 +static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
 +{
 +      if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
 +              static struct strbuf sb = STRBUF_INIT;
 +              const char *ap = es->line, *ae = es->line + es->len;
 +              int c;
 +
 +              strbuf_reset(&sb);
 +              while (ae > ap && isspace(*ae))
 +                      ae--;
 +              while ((c = next_byte(&ap, &ae, o)) > 0)
 +                      strbuf_addch(&sb, c);
 +
 +              return memhash(sb.buf, sb.len);
 +      } else {
 +              return memhash(es->line, es->len);
 +      }
 +}
 +
 +static struct moved_entry *prepare_entry(struct diff_options *o,
 +                                       int line_no)
 +{
 +      struct moved_entry *ret = xmalloc(sizeof(*ret));
 +      struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
 +
 +      ret->ent.hash = get_string_hash(l, o);
 +      ret->es = l;
 +      ret->next_line = NULL;
 +
 +      return ret;
 +}
 +
 +static void add_lines_to_move_detection(struct diff_options *o,
 +                                      struct hashmap *add_lines,
 +                                      struct hashmap *del_lines)
 +{
 +      struct moved_entry *prev_line = NULL;
 +
 +      int n;
 +      for (n = 0; n < o->emitted_symbols->nr; n++) {
 +              struct hashmap *hm;
 +              struct moved_entry *key;
 +
 +              switch (o->emitted_symbols->buf[n].s) {
 +              case DIFF_SYMBOL_PLUS:
 +                      hm = add_lines;
 +                      break;
 +              case DIFF_SYMBOL_MINUS:
 +                      hm = del_lines;
 +                      break;
 +              default:
 +                      prev_line = NULL;
 +                      continue;
 +              }
 +
 +              key = prepare_entry(o, n);
 +              if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
 +                      prev_line->next_line = key;
 +
 +              hashmap_add(hm, key);
 +              prev_line = key;
 +      }
 +}
 +
 +static int shrink_potential_moved_blocks(struct moved_entry **pmb,
 +                                       int pmb_nr)
 +{
 +      int lp, rp;
 +
 +      /* Shrink the set of potential block to the remaining running */
 +      for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
 +              while (lp < pmb_nr && pmb[lp])
 +                      lp++;
 +              /* lp points at the first NULL now */
 +
 +              while (rp > -1 && !pmb[rp])
 +                      rp--;
 +              /* rp points at the last non-NULL */
 +
 +              if (lp < pmb_nr && rp > -1 && lp < rp) {
 +                      pmb[lp] = pmb[rp];
 +                      pmb[rp] = NULL;
 +                      rp--;
 +                      lp++;
 +              }
 +      }
 +
 +      /* Remember the number of running sets */
 +      return rp + 1;
 +}
 +
 +/*
 + * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
 + *
 + * Otherwise, if the last block has fewer alphanumeric characters than
 + * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
 + * that block.
 + *
 + * The last block consists of the (n - block_length)'th line up to but not
 + * including the nth line.
 + *
 + * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
 + * Think of a way to unify them.
 + */
 +static void adjust_last_block(struct diff_options *o, int n, int block_length)
 +{
 +      int i, alnum_count = 0;
 +      if (o->color_moved == COLOR_MOVED_PLAIN)
 +              return;
 +      for (i = 1; i < block_length + 1; i++) {
 +              const char *c = o->emitted_symbols->buf[n - i].line;
 +              for (; *c; c++) {
 +                      if (!isalnum(*c))
 +                              continue;
 +                      alnum_count++;
 +                      if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
 +                              return;
 +              }
 +      }
 +      for (i = 1; i < block_length + 1; i++)
 +              o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
 +}
 +
 +/* Find blocks of moved code, delegate actual coloring decision to helper */
 +static void mark_color_as_moved(struct diff_options *o,
 +                              struct hashmap *add_lines,
 +                              struct hashmap *del_lines)
 +{
 +      struct moved_entry **pmb = NULL; /* potentially moved blocks */
 +      int pmb_nr = 0, pmb_alloc = 0;
 +      int n, flipped_block = 1, block_length = 0;
 +
 +
 +      for (n = 0; n < o->emitted_symbols->nr; n++) {
 +              struct hashmap *hm = NULL;
 +              struct moved_entry *key;
 +              struct moved_entry *match = NULL;
 +              struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
 +              int i;
 +
 +              switch (l->s) {
 +              case DIFF_SYMBOL_PLUS:
 +                      hm = del_lines;
 +                      key = prepare_entry(o, n);
 +                      match = hashmap_get(hm, key, o);
 +                      free(key);
 +                      break;
 +              case DIFF_SYMBOL_MINUS:
 +                      hm = add_lines;
 +                      key = prepare_entry(o, n);
 +                      match = hashmap_get(hm, key, o);
 +                      free(key);
 +                      break;
 +              default:
 +                      flipped_block = 1;
 +              }
 +
 +              if (!match) {
 +                      adjust_last_block(o, n, block_length);
 +                      pmb_nr = 0;
 +                      block_length = 0;
 +                      continue;
 +              }
 +
 +              l->flags |= DIFF_SYMBOL_MOVED_LINE;
 +
 +              if (o->color_moved == COLOR_MOVED_PLAIN)
 +                      continue;
 +
 +              /* Check any potential block runs, advance each or nullify */
 +              for (i = 0; i < pmb_nr; i++) {
 +                      struct moved_entry *p = pmb[i];
 +                      struct moved_entry *pnext = (p && p->next_line) ?
 +                                      p->next_line : NULL;
 +                      if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
 +                              pmb[i] = p->next_line;
 +                      } else {
 +                              pmb[i] = NULL;
 +                      }
 +              }
 +
 +              pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
 +
 +              if (pmb_nr == 0) {
 +                      /*
 +                       * The current line is the start of a new block.
 +                       * Setup the set of potential blocks.
 +                       */
 +                      for (; match; match = hashmap_get_next(hm, match)) {
 +                              ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
 +                              pmb[pmb_nr++] = match;
 +                      }
 +
 +                      flipped_block = (flipped_block + 1) % 2;
 +
 +                      adjust_last_block(o, n, block_length);
 +                      block_length = 0;
 +              }
 +
 +              block_length++;
 +
 +              if (flipped_block)
 +                      l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
 +      }
 +      adjust_last_block(o, n, block_length);
 +
 +      free(pmb);
 +}
 +
 +#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
 +  (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
 +static void dim_moved_lines(struct diff_options *o)
 +{
 +      int n;
 +      for (n = 0; n < o->emitted_symbols->nr; n++) {
 +              struct emitted_diff_symbol *prev = (n != 0) ?
 +                              &o->emitted_symbols->buf[n - 1] : NULL;
 +              struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
 +              struct emitted_diff_symbol *next =
 +                              (n < o->emitted_symbols->nr - 1) ?
 +                              &o->emitted_symbols->buf[n + 1] : NULL;
 +
 +              /* Not a plus or minus line? */
 +              if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
 +                      continue;
 +
 +              /* Not a moved line? */
 +              if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
 +                      continue;
 +
 +              /*
 +               * If prev or next are not a plus or minus line,
 +               * pretend they don't exist
 +               */
 +              if (prev && prev->s != DIFF_SYMBOL_PLUS &&
 +                          prev->s != DIFF_SYMBOL_MINUS)
 +                      prev = NULL;
 +              if (next && next->s != DIFF_SYMBOL_PLUS &&
 +                          next->s != DIFF_SYMBOL_MINUS)
 +                      next = NULL;
 +
 +              /* Inside a block? */
 +              if ((prev &&
 +                  (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
 +                  (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
 +                  (next &&
 +                  (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
 +                  (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
 +                      l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
 +                      continue;
 +              }
 +
 +              /* Check if we are at an interesting bound: */
 +              if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
 +                  (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
 +                     (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
 +                      continue;
 +              if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
 +                  (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
 +                     (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
 +                      continue;
 +
 +              /*
 +               * The boundary to prev and next are not interesting,
 +               * so this line is not interesting as a whole
 +               */
 +              l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
 +      }
 +}
 +
 +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_from_struct(struct diff_options *o,
 +                                       struct emitted_diff_symbol *eds)
 +{
 +      static const char *nneof = " No newline at end of file\n";
 +      const char *context, *reset, *set, *meta, *fraginfo;
 +      struct strbuf sb = STRBUF_INIT;
 +
 +      enum diff_symbol s = eds->s;
 +      const char *line = eds->line;
 +      int len = eds->len;
 +      unsigned flags = eds->flags;
 +
 +      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_SUMMARY:
 +      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:
 +              switch (flags & (DIFF_SYMBOL_MOVED_LINE |
 +                               DIFF_SYMBOL_MOVED_LINE_ALT |
 +                               DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_ALT |
 +                   DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
 +                      set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_ALT:
 +                      set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
 +                      set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE:
 +                      set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
 +                      break;
 +              default:
 +                      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:
 +              switch (flags & (DIFF_SYMBOL_MOVED_LINE |
 +                               DIFF_SYMBOL_MOVED_LINE_ALT |
 +                               DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_ALT |
 +                   DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
 +                      set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_ALT:
 +                      set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE |
 +                   DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
 +                      set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
 +                      break;
 +              case DIFF_SYMBOL_MOVED_LINE:
 +                      set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
 +                      break;
 +              default:
 +                      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;
 +      case DIFF_SYMBOL_WORD_DIFF:
 +              fprintf(o->file, "%.*s", len, line);
 +              break;
 +      case DIFF_SYMBOL_STAT_SEP:
 +              fputs(o->stat_sep, o->file);
 +              break;
 +      default:
 +              die("BUG: unknown diff symbol");
        }
 +      strbuf_release(&sb);
 +}
 +
 +static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
 +                           const char *line, int len, unsigned flags)
 +{
 +      struct emitted_diff_symbol e = {line, len, flags, s};
 +
 +      if (o->emitted_symbols)
 +              append_emitted_diff_symbol(o, &e);
 +      else
 +              emit_diff_symbol_from_struct(o, &e);
 +}
 +
 +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,
        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 @@ */
        }
  
        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);
  }
  
@@@ -1422,17 -697,17 +1422,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;
        }
  }
@@@ -1441,6 -716,7 +1441,6 @@@ static void emit_rewrite_lines(struct e
                               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) {
                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,
                              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;
  
        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);
  
        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)
@@@ -1590,49 -872,37 +1590,49 @@@ struct diff_words_data 
        struct diff_words_style *style;
  };
  
 -static int fn_out_diff_words_write_helper(FILE *fp,
 +static int fn_out_diff_words_write_helper(struct diff_options *o,
                                          struct diff_words_style_elem *st_el,
                                          const char *newline,
 -                                        size_t count, const char *buf,
 -                                        const char *line_prefix)
 +                                        size_t count, const char *buf)
  {
        int print = 0;
 +      struct strbuf sb = STRBUF_INIT;
  
        while (count) {
                char *p = memchr(buf, '\n', count);
                if (print)
 -                      fputs(line_prefix, fp);
 +                      strbuf_addstr(&sb, diff_line_prefix(o));
 +
                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;
 +                      const char *reset = st_el->color && *st_el->color ?
 +                                          GIT_COLOR_RESET : NULL;
 +                      if (st_el->color && *st_el->color)
 +                              strbuf_addstr(&sb, st_el->color);
 +                      strbuf_addstr(&sb, st_el->prefix);
 +                      strbuf_add(&sb, buf, p ? p - buf : count);
 +                      strbuf_addstr(&sb, st_el->suffix);
 +                      if (reset)
 +                              strbuf_addstr(&sb, reset);
                }
                if (!p)
 -                      return 0;
 -              if (fputs(newline, fp) < 0)
 -                      return -1;
 +                      goto out;
 +
 +              strbuf_addstr(&sb, newline);
                count -= p + 1 - buf;
                buf = p + 1;
                print = 1;
 +              if (count) {
 +                      emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
 +                                       sb.buf, sb.len, 0);
 +                      strbuf_reset(&sb);
 +              }
        }
 +
 +out:
 +      if (sb.len)
 +              emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
 +                               sb.buf, sb.len, 0);
 +      strbuf_release(&sb);
        return 0;
  }
  
@@@ -1714,20 -984,24 +1714,20 @@@ static void fn_out_diff_words_aux(void 
                fputs(line_prefix, diff_words->opt->file);
        }
        if (diff_words->current_plus != plus_begin) {
 -              fn_out_diff_words_write_helper(diff_words->opt->file,
 +              fn_out_diff_words_write_helper(diff_words->opt,
                                &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);
 +                              diff_words->current_plus);
        }
        if (minus_begin != minus_end) {
 -              fn_out_diff_words_write_helper(diff_words->opt->file,
 +              fn_out_diff_words_write_helper(diff_words->opt,
                                &style->old, style->newline,
 -                              minus_end - minus_begin, minus_begin,
 -                              line_prefix);
 +                              minus_end - minus_begin, minus_begin);
        }
        if (plus_begin != plus_end) {
 -              fn_out_diff_words_write_helper(diff_words->opt->file,
 +              fn_out_diff_words_write_helper(diff_words->opt,
                                &style->new, style->newline,
 -                              plus_end - plus_begin, plus_begin,
 -                              line_prefix);
 +                              plus_end - plus_begin, plus_begin);
        }
  
        diff_words->current_plus = plus_end;
@@@ -1821,12 -1095,11 +1821,12 @@@ static void diff_words_show(struct diff
  
        /* special case: only removal */
        if (!diff_words->plus.text.size) {
 -              fputs(line_prefix, diff_words->opt->file);
 -              fn_out_diff_words_write_helper(diff_words->opt->file,
 +              emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
 +                               line_prefix, strlen(line_prefix), 0);
 +              fn_out_diff_words_write_helper(diff_words->opt,
                        &style->old, style->newline,
                        diff_words->minus.text.size,
 -                      diff_words->minus.text.ptr, line_prefix);
 +                      diff_words->minus.text.ptr);
                diff_words->minus.text.size = 0;
                return;
        }
        if (diff_words->current_plus != diff_words->plus.text.ptr +
                        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,
 +                      emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
 +                                       line_prefix, strlen(line_prefix), 0);
 +              fn_out_diff_words_write_helper(diff_words->opt,
                        &style->ctx, style->newline,
                        diff_words->plus.text.ptr + diff_words->plus.text.size
 -                      - diff_words->current_plus, diff_words->current_plus,
 -                      line_prefix);
 +                      - diff_words->current_plus, diff_words->current_plus);
        }
        diff_words->minus.text.size = diff_words->plus.text.size = 0;
  }
  /* In "color-words" mode, show word-diff of words accumulated in the buffer */
  static void diff_words_flush(struct emit_callback *ecbdata)
  {
 +      struct diff_options *wo = ecbdata->diff_words->opt;
 +
        if (ecbdata->diff_words->minus.text.size ||
            ecbdata->diff_words->plus.text.size)
                diff_words_show(ecbdata->diff_words);
 +
 +      if (wo->emitted_symbols) {
 +              struct diff_options *o = ecbdata->opt;
 +              struct emitted_diff_symbols *wol = wo->emitted_symbols;
 +              int i;
 +
 +              /*
 +               * NEEDSWORK:
 +               * Instead of appending each, concat all words to a line?
 +               */
 +              for (i = 0; i < wol->nr; i++)
 +                      append_emitted_diff_symbol(o, &wol->buf[i]);
 +
 +              for (i = 0; i < wol->nr; i++)
 +                      free((void *)wol->buf[i].line);
 +
 +              wol->nr = 0;
 +      }
  }
  
  static void diff_filespec_load_driver(struct diff_filespec *one)
@@@ -1920,11 -1173,6 +1920,11 @@@ static void init_diff_words_data(struc
                xcalloc(1, sizeof(struct diff_words_data));
        ecbdata->diff_words->type = o->word_diff;
        ecbdata->diff_words->opt = o;
 +
 +      if (orig_opts->emitted_symbols)
 +              o->emitted_symbols =
 +                      xcalloc(1, sizeof(struct emitted_diff_symbols));
 +
        if (!o->word_regex)
                o->word_regex = userdiff_word_regex(one);
        if (!o->word_regex)
@@@ -1959,7 -1207,6 +1959,7 @@@ static void free_diff_words_data(struc
  {
        if (ecbdata->diff_words) {
                diff_words_flush(ecbdata);
 +              free (ecbdata->diff_words->opt->emitted_symbols);
                free (ecbdata->diff_words->opt);
                free (ecbdata->diff_words->minus.text.ptr);
                free (ecbdata->diff_words->minus.orig);
@@@ -1996,6 -1243,8 +1996,6 @@@ static unsigned long sane_truncate_line
        unsigned long allot;
        size_t l = len;
  
 -      if (ecb->truncate)
 -              return ecb->truncate(line, len);
        cp = line;
        allot = l;
        while (0 < l) {
@@@ -2024,25 -1273,30 +2024,25 @@@ static void find_lno(const char *line, 
  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;
        }
  
                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);
                        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;
        }
  
        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;
        }
  }
@@@ -2251,14 -1518,20 +2251,14 @@@ static int scale_linear(int it, int wid
        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)
        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,
                            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)
        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);
                }
  
                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;
                }
  
                                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++) {
                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)
  
        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))) {
                        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)
@@@ -2970,8 -2222,8 +2970,8 @@@ static unsigned char *deflate_it(char *
        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;
        }
  
        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;
        /* 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;
                        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)
@@@ -3124,16 -2366,24 +3124,16 @@@ static void builtin_diff(const char *na
        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;
        }
  
                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);
        }
  
        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)
                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.. */
                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);
                }
  
@@@ -4011,7 -3246,7 +4011,7 @@@ static void diff_fill_oid_info(struct d
                        }
                        if (lstat(one->path, &st) < 0)
                                die_errno("stat '%s'", one->path);
-                       if (index_path(one->oid.hash, one->path, &st, 0))
+                       if (index_path(&one->oid, one->path, &st, 0))
                                die("cannot hash %s", one->path);
                }
        }
@@@ -4044,8 -3279,8 +4044,8 @@@ static void run_diff(struct diff_filepa
        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);
@@@ -4168,8 -3403,6 +4168,8 @@@ void diff_setup(struct diff_options *op
                options->a_prefix = "a/";
                options->b_prefix = "b/";
        }
 +
 +      options->color_moved = diff_color_moved_default;
  }
  
  void diff_setup_done(struct diff_options *options)
  
        if (DIFF_OPT_TST(options, FOLLOW_RENAMES) && options->pathspec.nr != 1)
                die(_("--follow requires exactly one pathspec"));
 +
 +      if (!options->use_color || external_diff())
 +              options->color_moved = 0;
  }
  
  static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
@@@ -4706,19 -3936,7 +4706,19 @@@ int diff_opt_parse(struct diff_options 
        }
        else if (!strcmp(arg, "--no-color"))
                options->use_color = 0;
 -      else if (!strcmp(arg, "--color-words")) {
 +      else if (!strcmp(arg, "--color-moved")) {
 +              if (diff_color_moved_default)
 +                      options->color_moved = diff_color_moved_default;
 +              if (options->color_moved == COLOR_MOVED_NO)
 +                      options->color_moved = COLOR_MOVED_DEFAULT;
 +      } else if (!strcmp(arg, "--no-color-moved"))
 +              options->color_moved = COLOR_MOVED_NO;
 +      else if (skip_prefix(arg, "--color-moved=", &arg)) {
 +              int cm = parse_color_moved(arg);
 +              if (cm < 0)
 +                      die("bad --color-moved argument: %s", arg);
 +              options->color_moved = cm;
 +      } else if (!strcmp(arg, "--color-words")) {
                options->use_color = 1;
                options->word_diff = DIFF_WORDS_COLOR;
        }
@@@ -5248,76 -4466,67 +5248,76 @@@ static void flush_one_pair(struct diff_
        }
  }
  
 -static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
 +static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
  {
 +      struct strbuf sb = STRBUF_INIT;
        if (fs->mode)
 -              fprintf(file, " %s mode %06o ", newdelete, fs->mode);
 +              strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
        else
 -              fprintf(file, " %s ", newdelete);
 -      write_name_quoted(fs->path, file, '\n');
 -}
 +              strbuf_addf(&sb, " %s ", newdelete);
  
 +      quote_c_style(fs->path, &sb, NULL, 0);
 +      strbuf_addch(&sb, '\n');
 +      emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
 +                       sb.buf, sb.len, 0);
 +      strbuf_release(&sb);
 +}
  
 -static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name,
 -              const char *line_prefix)
 +static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
 +              int show_name)
  {
        if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
 -              fprintf(file, "%s mode change %06o => %06o%c", line_prefix, p->one->mode,
 -                      p->two->mode, show_name ? ' ' : '\n');
 +              struct strbuf sb = STRBUF_INIT;
 +              strbuf_addf(&sb, " mode change %06o => %06o",
 +                          p->one->mode, p->two->mode);
                if (show_name) {
 -                      write_name_quoted(p->two->path, file, '\n');
 +                      strbuf_addch(&sb, ' ');
 +                      quote_c_style(p->two->path, &sb, NULL, 0);
                }
 +              emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
 +                               sb.buf, sb.len, 0);
 +              strbuf_release(&sb);
        }
  }
  
 -static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p,
 -                      const char *line_prefix)
 +static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
 +              struct diff_filepair *p)
  {
 +      struct strbuf sb = STRBUF_INIT;
        char *names = pprint_rename(p->one->path, p->two->path);
 -
 -      fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
 +      strbuf_addf(&sb, " %s %s (%d%%)\n",
 +                      renamecopy, names, similarity_index(p));
        free(names);
 -      show_mode_change(file, p, 0, line_prefix);
 +      emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
 +                               sb.buf, sb.len, 0);
 +      show_mode_change(opt, p, 0);
  }
  
  static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
  {
 -      FILE *file = opt->file;
 -      const char *line_prefix = diff_line_prefix(opt);
 -
        switch(p->status) {
        case DIFF_STATUS_DELETED:
 -              fputs(line_prefix, file);
 -              show_file_mode_name(file, "delete", p->one);
 +              show_file_mode_name(opt, "delete", p->one);
                break;
        case DIFF_STATUS_ADDED:
 -              fputs(line_prefix, file);
 -              show_file_mode_name(file, "create", p->two);
 +              show_file_mode_name(opt, "create", p->two);
                break;
        case DIFF_STATUS_COPIED:
 -              fputs(line_prefix, file);
 -              show_rename_copy(file, "copy", p, line_prefix);
 +              show_rename_copy(opt, "copy", p);
                break;
        case DIFF_STATUS_RENAMED:
 -              fputs(line_prefix, file);
 -              show_rename_copy(file, "rename", p, line_prefix);
 +              show_rename_copy(opt, "rename", p);
                break;
        default:
                if (p->score) {
 -                      fprintf(file, "%s rewrite ", line_prefix);
 -                      write_name_quoted(p->two->path, file, ' ');
 -                      fprintf(file, "(%d%%)\n", similarity_index(p));
 +                      struct strbuf sb = STRBUF_INIT;
 +                      strbuf_addstr(&sb, " rewrite ");
 +                      quote_c_style(p->two->path, &sb, NULL, 0);
 +                      strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
 +                      emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
 +                                       sb.buf, sb.len, 0);
                }
 -              show_mode_change(file, p, !p->score, line_prefix);
 +              show_mode_change(opt, p, !p->score);
                break;
        }
  }
@@@ -5522,51 -4731,6 +5522,51 @@@ void diff_warn_rename_limit(const char 
                warning(_(rename_limit_advice), varname, needed);
  }
  
 +static void diff_flush_patch_all_file_pairs(struct diff_options *o)
 +{
 +      int i;
 +      static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
 +      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");
 +
 +      if (o->color_moved)
 +              o->emitted_symbols = &esm;
 +
 +      for (i = 0; i < q->nr; i++) {
 +              struct diff_filepair *p = q->queue[i];
 +              if (check_pair_status(p))
 +                      diff_flush_patch(p, o);
 +      }
 +
 +      if (o->emitted_symbols) {
 +              if (o->color_moved) {
 +                      struct hashmap add_lines, del_lines;
 +
 +                      hashmap_init(&del_lines,
 +                                   (hashmap_cmp_fn)moved_entry_cmp, o, 0);
 +                      hashmap_init(&add_lines,
 +                                   (hashmap_cmp_fn)moved_entry_cmp, o, 0);
 +
 +                      add_lines_to_move_detection(o, &add_lines, &del_lines);
 +                      mark_color_as_moved(o, &add_lines, &del_lines);
 +                      if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
 +                              dim_moved_lines(o);
 +
 +                      hashmap_free(&add_lines, 0);
 +                      hashmap_free(&del_lines, 0);
 +              }
 +
 +              for (i = 0; i < esm.nr; i++)
 +                      emit_diff_symbol_from_struct(o, &esm.buf[i]);
 +
 +              for (i = 0; i < esm.nr; i++)
 +                      free((void *)esm.buf[i].line);
 +      }
 +      esm.nr = 0;
 +}
 +
  void diff_flush(struct diff_options *options)
  {
        struct diff_queue_struct *q = &diff_queued_diff;
                        fclose(options->file);
                options->file = xfopen("/dev/null", "w");
                options->close_file = 1;
 +              options->color_moved = 0;
                for (i = 0; i < q->nr; i++) {
                        struct diff_filepair *p = q->queue[i];
                        if (check_pair_status(p))
  
        if (output_format & DIFF_FORMAT_PATCH) {
                if (separator) {
 -                      fprintf(options->file, "%s%c",
 -                              diff_line_prefix(options),
 -                              options->line_termination);
 -                      if (options->stat_sep) {
 +                      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);
 -                      }
 +                              emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
 +                                               NULL, 0, 0);
                }
  
 -              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)
diff --combined sha1_file.c
index 6da17a9c8762c7e79c1c26714722e482713a07aa,103acfcb7744419b582626554c49e12a0ab54b21..081820ac293a8fa9f0861ce1159ee73b0b45f5a1
@@@ -32,7 -32,7 +32,7 @@@
  #define SZ_FMT PRIuMAX
  static inline uintmax_t sz_fmt(size_t s) { return s; }
  
 -const unsigned char null_sha1[20];
 +const unsigned char null_sha1[GIT_MAX_RAWSZ];
  const struct object_id null_oid;
  const struct object_id empty_tree_oid = {
        EMPTY_TREE_SHA1_BIN_LITERAL
@@@ -347,7 -347,6 +347,7 @@@ static int alt_odb_usable(struct strbu
   * SHA1, an extra slash for the first level indirection, and the
   * terminating NUL.
   */
 +static void read_info_alternates(const char * relative_base, int depth);
  static int link_alt_odb_entry(const char *entry, const char *relative_base,
        int depth, const char *normalized_objdir)
  {
@@@ -449,7 -448,7 +449,7 @@@ static void link_alt_odb_entries(const 
        strbuf_release(&objdirbuf);
  }
  
 -void read_info_alternates(const char * relative_base, int depth)
 +static void read_info_alternates(const char * relative_base, int depth)
  {
        char *map;
        size_t mapsz;
@@@ -2445,9 -2444,6 +2445,9 @@@ int packed_object_info(struct packed_gi
                        hashclr(oi->delta_base_sha1);
        }
  
 +      oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
 +                                                        OI_PACKED;
 +
  out:
        unuse_pack(&w_curs);
        return type;
@@@ -2546,8 -2542,8 +2546,8 @@@ void *unpack_entry(struct packed_git *p
                                error("bad packed object CRC for %s",
                                      sha1_to_hex(sha1));
                                mark_bad_packed_object(p, sha1);
 -                              unuse_pack(&w_curs);
 -                              return NULL;
 +                              data = NULL;
 +                              goto out;
                        }
                }
  
        if (final_size)
                *final_size = size;
  
 +out:
        unuse_pack(&w_curs);
  
        if (delta_stack != small_delta_stack)
@@@ -2764,6 -2759,7 +2764,6 @@@ off_t find_pack_entry_one(const unsigne
        const uint32_t *level1_ofs = p->index_data;
        const unsigned char *index = p->index_data;
        unsigned hi, lo, stride;
 -      static int use_lookup = -1;
        static int debug_lookup = -1;
  
        if (debug_lookup < 0)
                printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
                       sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
  
 -      if (use_lookup < 0)
 -              use_lookup = !!getenv("GIT_USE_LOOKUP");
 -      if (use_lookup) {
 -              int pos = sha1_entry_pos(index, stride, 0,
 -                                       lo, hi, p->num_objects, sha1);
 -              if (pos < 0)
 -                      return 0;
 -              return nth_packed_object_offset(p, pos);
 -      }
 -
 -      do {
 +      while (lo < hi) {
                unsigned mi = (lo + hi) / 2;
                int cmp = hashcmp(index + mi * stride, sha1);
  
                        hi = mi;
                else
                        lo = mi+1;
 -      } while (lo < hi);
 +      }
        return 0;
  }
  
@@@ -2967,7 -2973,6 +2967,7 @@@ static int sha1_loose_object_info(cons
        if (oi->sizep == &size_scratch)
                oi->sizep = NULL;
        strbuf_release(&hdrbuf);
 +      oi->whence = OI_LOOSE;
        return (status < 0) ? status : 0;
  }
  
@@@ -3005,8 -3010,10 +3005,8 @@@ int sha1_object_info_extended(const uns
  
        if (!find_pack_entry(real, &e)) {
                /* Most likely it's a loose object. */
 -              if (!sha1_loose_object_info(real, oi, flags)) {
 -                      oi->whence = OI_LOOSE;
 +              if (!sha1_loose_object_info(real, oi, flags))
                        return 0;
 -              }
  
                /* Not a loose object; someone else may have just packed it. */
                if (flags & OBJECT_INFO_QUICK) {
        if (rtype < 0) {
                mark_bad_packed_object(e.p, real);
                return sha1_object_info_extended(real, oi, 0);
 -      } else if (in_delta_base_cache(e.p, e.offset)) {
 -              oi->whence = OI_DBCACHED;
 -      } else {
 -              oi->whence = OI_PACKED;
 +      } else if (oi->whence == OI_PACKED) {
                oi->u.packed.offset = e.offset;
                oi->u.packed.pack = e.p;
                oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
@@@ -3053,6 -3063,30 +3053,6 @@@ int sha1_object_info(const unsigned cha
        return type;
  }
  
 -static void *read_packed_sha1(const unsigned char *sha1,
 -                            enum object_type *type, unsigned long *size)
 -{
 -      struct pack_entry e;
 -      void *data;
 -
 -      if (!find_pack_entry(sha1, &e))
 -              return NULL;
 -      data = cache_or_unpack_entry(e.p, e.offset, size, type);
 -      if (!data) {
 -              /*
 -               * We're probably in deep shit, but let's try to fetch
 -               * the required object anyway from another pack or loose.
 -               * This should happen only in the presence of a corrupted
 -               * pack, and is better than failing outright.
 -               */
 -              error("failed to read object %s at offset %"PRIuMAX" from %s",
 -                    sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
 -              mark_bad_packed_object(e.p, sha1);
 -              data = read_object(sha1, type, size);
 -      }
 -      return data;
 -}
 -
  int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
                      unsigned char *sha1)
  {
@@@ -3403,7 -3437,7 +3403,7 @@@ int write_sha1_file(const void *buf, un
  }
  
  int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
-                            unsigned char *sha1, unsigned flags)
+                            struct object_id *oid, unsigned flags)
  {
        char *header;
        int hdrlen, status = 0;
        /* type string, SP, %lu of the length plus NUL must fit this */
        hdrlen = strlen(type) + 32;
        header = xmalloc(hdrlen);
-       write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
+       write_sha1_file_prepare(buf, len, type, oid->hash, header, &hdrlen);
  
        if (!(flags & HASH_WRITE_OBJECT))
                goto cleanup;
-       if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
+       if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
                goto cleanup;
-       status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
+       status = write_loose_object(oid->hash, header, hdrlen, buf, len, 0);
  
  cleanup:
        free(header);
@@@ -3435,7 -3469,7 +3435,7 @@@ int force_object_loose(const unsigned c
  
        if (has_loose_object(sha1))
                return 0;
 -      buf = read_packed_sha1(sha1, &type, &len);
 +      buf = read_object(sha1, &type, &len);
        if (!buf)
                return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
        hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
@@@ -3621,14 -3655,14 +3621,14 @@@ static int index_core(unsigned char *sh
   * binary blobs, they generally do not want to get any conversion, and
   * callers should avoid this code path when filters are requested.
   */
- static int index_stream(unsigned char *sha1, int fd, size_t size,
+ static int index_stream(struct object_id *oid, int fd, size_t size,
                        enum object_type type, const char *path,
                        unsigned flags)
  {
-       return index_bulk_checkin(sha1, fd, size, type, path, flags);
+       return index_bulk_checkin(oid->hash, fd, size, type, path, flags);
  }
  
- int index_fd(unsigned char *sha1, int fd, struct stat *st,
+ int index_fd(struct object_id *oid, int fd, struct stat *st,
             enum object_type type, const char *path, unsigned flags)
  {
        int ret;
         * die() for large files.
         */
        if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
-               ret = index_stream_convert_blob(sha1, fd, path, flags);
+               ret = index_stream_convert_blob(oid->hash, fd, path, flags);
        else if (!S_ISREG(st->st_mode))
-               ret = index_pipe(sha1, fd, type, path, flags);
+               ret = index_pipe(oid->hash, fd, type, path, flags);
        else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
                 (path && would_convert_to_git(&the_index, path)))
-               ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_core(oid->hash, fd, xsize_t(st->st_size), type, path,
                                 flags);
        else
-               ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path,
+               ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
                                   flags);
        close(fd);
        return ret;
  }
  
- int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
+ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
  {
        int fd;
        struct strbuf sb = STRBUF_INIT;
                fd = open(path, O_RDONLY);
                if (fd < 0)
                        return error_errno("open(\"%s\")", path);
-               if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
+               if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
                        return error("%s: failed to insert into database",
                                     path);
                break;
                if (strbuf_readlink(&sb, path, st->st_size))
                        return error_errno("readlink(\"%s\")", path);
                if (!(flags & HASH_WRITE_OBJECT))
-                       hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
-               else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
+                       hash_sha1_file(sb.buf, sb.len, blob_type, oid->hash);
+               else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash))
                        return error("%s: failed to insert into database",
                                     path);
                strbuf_release(&sb);
                break;
        case S_IFDIR:
-               return resolve_gitlink_ref(path, "HEAD", sha1);
+               return resolve_gitlink_ref(path, "HEAD", oid->hash);
        default:
                return error("%s: unsupported file type", path);
        }