blame: move contents_from to scoreboard
[gitweb.git] / builtin / blame.c
index 5e5d30ecbc83cbbef4f1feadfd77163e3afbbf96..0955fc125cf251f16d6fc2f734fa6c9ceb62b176 100644 (file)
@@ -8,7 +8,6 @@
 #include "cache.h"
 #include "refs.h"
 #include "builtin.h"
-#include "blob.h"
 #include "commit.h"
 #include "tag.h"
 #include "tree-walk.h"
@@ -56,26 +55,17 @@ static int show_progress;
 static struct date_mode blame_date_mode = { DATE_ISO8601 };
 static size_t blame_date_width;
 
-static struct string_list mailmap;
+static struct string_list mailmap = STRING_LIST_INIT_NODUP;
 
 #ifndef DEBUG
 #define DEBUG 0
 #endif
 
-/* stats */
-static int num_read_blob;
-static int num_get_patch;
-static int num_commits;
-
 #define PICKAXE_BLAME_MOVE             01
 #define PICKAXE_BLAME_COPY             02
 #define PICKAXE_BLAME_COPY_HARDER      04
 #define PICKAXE_BLAME_COPY_HARDEST     010
 
-/*
- * blame for a blame_entry with score lower than these thresholds
- * is not passed to the parent using move/copy logic.
- */
 static unsigned blame_move_score;
 static unsigned blame_copy_score;
 #define BLAME_DEFAULT_MOVE_SCORE       20
@@ -88,10 +78,10 @@ static unsigned blame_copy_score;
 /*
  * One blob in a commit that is being suspected
  */
-struct origin {
+struct blame_origin {
        int refcnt;
        /* Record preceding blame record for this blob */
-       struct origin *previous;
+       struct blame_origin *previous;
        /* origins are put in a list linked via `next' hanging off the
         * corresponding commit's util field in order to make finding
         * them fast.  The presence in this chain does not count
@@ -109,7 +99,7 @@ struct origin {
         * us get tripped up by this case, it certainly does not seem
         * worth optimizing for.
         */
-       struct origin *next;
+       struct blame_origin *next;
        struct commit *commit;
        /* `suspects' contains blame entries that may be attributed to
         * this origin's commit or to parent commits.  When a commit
@@ -120,7 +110,7 @@ struct origin {
         */
        struct blame_entry *suspects;
        mmfile_t file;
-       unsigned char blob_sha1[20];
+       struct object_id blob_oid;
        unsigned mode;
        /* guilty gets set when shipping any suspects to the final
         * blame list instead of other commits
@@ -147,56 +137,29 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
        return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
 }
 
-/*
- * Prepare diff_filespec and convert it using diff textconv API
- * if the textconv driver exists.
- * Return 1 if the conversion succeeds, 0 otherwise.
- */
-int textconv_object(const char *path,
-                   unsigned mode,
-                   const unsigned char *sha1,
-                   int sha1_valid,
-                   char **buf,
-                   unsigned long *buf_size)
-{
-       struct diff_filespec *df;
-       struct userdiff_driver *textconv;
-
-       df = alloc_filespec(path);
-       fill_filespec(df, sha1, sha1_valid, mode);
-       textconv = get_textconv(df);
-       if (!textconv) {
-               free_filespec(df);
-               return 0;
-       }
-
-       *buf_size = fill_textconv(textconv, df, buf);
-       free_filespec(df);
-       return 1;
-}
-
 /*
  * Given an origin, prepare mmfile_t structure to be used by the
  * diff machinery
  */
 static void fill_origin_blob(struct diff_options *opt,
-                            struct origin *o, mmfile_t *file)
+                            struct blame_origin *o, mmfile_t *file, int *num_read_blob)
 {
        if (!o->file.ptr) {
                enum object_type type;
                unsigned long file_size;
 
-               num_read_blob++;
+               (*num_read_blob)++;
                if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
-                   textconv_object(o->path, o->mode, o->blob_sha1, 1, &file->ptr, &file_size))
+                   textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size))
                        ;
                else
-                       file->ptr = read_sha1_file(o->blob_sha1, &type, &file_size);
+                       file->ptr = read_sha1_file(o->blob_oid.hash, &type,
+                                                  &file_size);
                file->size = file_size;
 
                if (!file->ptr)
                        die("Cannot read blob %s for path %s",
-                           sha1_to_hex(o->blob_sha1),
+                           oid_to_hex(&o->blob_oid),
                            o->path);
                o->file = *file;
        }
@@ -208,19 +171,19 @@ static void fill_origin_blob(struct diff_options *opt,
  * Origin is refcounted and usually we keep the blob contents to be
  * reused.
  */
-static inline struct origin *origin_incref(struct origin *o)
+static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
 {
        if (o)
                o->refcnt++;
        return o;
 }
 
-static void origin_decref(struct origin *o)
+static void blame_origin_decref(struct blame_origin *o)
 {
        if (o && --o->refcnt <= 0) {
-               struct origin *p, *l = NULL;
+               struct blame_origin *p, *l = NULL;
                if (o->previous)
-                       origin_decref(o->previous);
+                       blame_origin_decref(o->previous);
                free(o->file.ptr);
                /* Should be present exactly once in commit chain */
                for (p = o->commit->util; p; l = p, p = p->next) {
@@ -233,11 +196,11 @@ static void origin_decref(struct origin *o)
                                return;
                        }
                }
-               die("internal error in blame::origin_decref");
+               die("internal error in blame_origin_decref");
        }
 }
 
-static void drop_origin_blob(struct origin *o)
+static void drop_origin_blob(struct blame_origin *o)
 {
        if (o->file.ptr) {
                free(o->file.ptr);
@@ -266,7 +229,7 @@ struct blame_entry {
        int num_lines;
 
        /* the commit that introduced this group into the final image */
-       struct origin *suspect;
+       struct blame_origin *suspect;
 
        /* the line number of the first line of this group in the
         * suspect's file; internally all line numbers are 0 based.
@@ -381,7 +344,7 @@ static int compare_commits_by_reverse_commit_date(const void *a,
 /*
  * The current state of the blame assignment.
  */
-struct scoreboard {
+struct blame_scoreboard {
        /* the final commit (i.e. where we started digging from) */
        struct commit *final;
        /* Priority queue for commits with unassigned blame records */
@@ -403,16 +366,31 @@ struct scoreboard {
        /* look-up a line in the final buffer */
        int num_lines;
        int *lineno;
+
+       /* stats */
+       int num_read_blob;
+       int num_get_patch;
+       int num_commits;
+
+       /*
+        * blame for a blame_entry with score lower than these thresholds
+        * is not passed to the parent using move/copy logic.
+        */
+       unsigned move_score;
+       unsigned copy_score;
+
+       /* use this file's contents as the final image */
+       const char *contents_from;
 };
 
-static void sanity_check_refcnt(struct scoreboard *);
+static void sanity_check_refcnt(struct blame_scoreboard *);
 
 /*
  * If two blame entries that are next to each other came from
  * contiguous lines in the same origin (i.e. <commit, path> pair),
  * merge them together.
  */
-static void coalesce(struct scoreboard *sb)
+static void blame_coalesce(struct blame_scoreboard *sb)
 {
        struct blame_entry *ent, *next;
 
@@ -421,7 +399,7 @@ static void coalesce(struct scoreboard *sb)
                    ent->s_lno + ent->num_lines == next->s_lno) {
                        ent->num_lines += next->num_lines;
                        ent->next = next->next;
-                       origin_decref(next->suspect);
+                       blame_origin_decref(next->suspect);
                        free(next);
                        ent->score = 0;
                        next = ent; /* again */
@@ -438,13 +416,13 @@ static void coalesce(struct scoreboard *sb)
  * the commit priority queue of the score board.
  */
 
-static void queue_blames(struct scoreboard *sb, struct origin *porigin,
+static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
                         struct blame_entry *sorted)
 {
        if (porigin->suspects)
                porigin->suspects = blame_merge(porigin->suspects, sorted);
        else {
-               struct origin *o;
+               struct blame_origin *o;
                for (o = porigin->commit->util; o; o = o->next) {
                        if (o->suspects) {
                                porigin->suspects = sorted;
@@ -462,9 +440,9 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
  * get_origin() to obtain shared, refcounted copy instead of calling
  * this function directly.
  */
-static struct origin *make_origin(struct commit *commit, const char *path)
+static struct blame_origin *make_origin(struct commit *commit, const char *path)
 {
-       struct origin *o;
+       struct blame_origin *o;
        FLEX_ALLOC_STR(o, path, path);
        o->commit = commit;
        o->refcnt = 1;
@@ -477,11 +455,9 @@ static struct origin *make_origin(struct commit *commit, const char *path)
  * Locate an existing origin or create a new one.
  * This moves the origin to front position in the commit util list.
  */
-static struct origin *get_origin(struct scoreboard *sb,
-                                struct commit *commit,
-                                const char *path)
+static struct blame_origin *get_origin(struct commit *commit, const char *path)
 {
-       struct origin *o, *l;
+       struct blame_origin *o, *l;
 
        for (o = commit->util, l = NULL; o; l = o, o = o->next) {
                if (!strcmp(o->path, path)) {
@@ -491,7 +467,7 @@ static struct origin *get_origin(struct scoreboard *sb,
                                o->next = commit->util;
                                commit->util = o;
                        }
-                       return origin_incref(o);
+                       return blame_origin_incref(o);
                }
        }
        return make_origin(commit, path);
@@ -506,19 +482,19 @@ static struct origin *get_origin(struct scoreboard *sb,
  *
  * This also fills origin->mode for corresponding tree path.
  */
-static int fill_blob_sha1_and_mode(struct origin *origin)
+static int fill_blob_sha1_and_mode(struct blame_origin *origin)
 {
-       if (!is_null_sha1(origin->blob_sha1))
+       if (!is_null_oid(&origin->blob_oid))
                return 0;
        if (get_tree_entry(origin->commit->object.oid.hash,
                           origin->path,
-                          origin->blob_sha1, &origin->mode))
+                          origin->blob_oid.hash, &origin->mode))
                goto error_out;
-       if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
+       if (sha1_object_info(origin->blob_oid.hash, NULL) != OBJ_BLOB)
                goto error_out;
        return 0;
  error_out:
-       hashclr(origin->blob_sha1);
+       oidclr(&origin->blob_oid);
        origin->mode = S_IFINVALID;
        return -1;
 }
@@ -527,11 +503,10 @@ static int fill_blob_sha1_and_mode(struct origin *origin)
  * We have an origin -- check if the same path exists in the
  * parent and return an origin structure to represent it.
  */
-static struct origin *find_origin(struct scoreboard *sb,
-                                 struct commit *parent,
-                                 struct origin *origin)
+static struct blame_origin *find_origin(struct commit *parent,
+                                 struct blame_origin *origin)
 {
-       struct origin *porigin;
+       struct blame_origin *porigin;
        struct diff_options diff_opts;
        const char *paths[2];
 
@@ -542,7 +517,7 @@ static struct origin *find_origin(struct scoreboard *sb,
                         * The same path between origin and its parent
                         * without renaming -- the most common case.
                         */
-                       return origin_incref (porigin);
+                       return blame_origin_incref (porigin);
                }
 
        /* See if the origin->path is different between parent
@@ -571,8 +546,8 @@ static struct origin *find_origin(struct scoreboard *sb,
 
        if (!diff_queued_diff.nr) {
                /* The path is the same as parent */
-               porigin = get_origin(sb, parent, origin->path);
-               hashcpy(porigin->blob_sha1, origin->blob_sha1);
+               porigin = get_origin(parent, origin->path);
+               oidcpy(&porigin->blob_oid, &origin->blob_oid);
                porigin->mode = origin->mode;
        } else {
                /*
@@ -597,8 +572,8 @@ static struct origin *find_origin(struct scoreboard *sb,
                        die("internal error in blame::find_origin (%c)",
                            p->status);
                case 'M':
-                       porigin = get_origin(sb, parent, origin->path);
-                       hashcpy(porigin->blob_sha1, p->one->sha1);
+                       porigin = get_origin(parent, origin->path);
+                       oidcpy(&porigin->blob_oid, &p->one->oid);
                        porigin->mode = p->one->mode;
                        break;
                case 'A':
@@ -608,7 +583,7 @@ static struct origin *find_origin(struct scoreboard *sb,
                }
        }
        diff_flush(&diff_opts);
-       free_pathspec(&diff_opts.pathspec);
+       clear_pathspec(&diff_opts.pathspec);
        return porigin;
 }
 
@@ -616,11 +591,10 @@ static struct origin *find_origin(struct scoreboard *sb,
  * We have an origin -- find the path that corresponds to it in its
  * parent and return an origin structure to represent it.
  */
-static struct origin *find_rename(struct scoreboard *sb,
-                                 struct commit *parent,
-                                 struct origin *origin)
+static struct blame_origin *find_rename(struct commit *parent,
+                                 struct blame_origin *origin)
 {
-       struct origin *porigin = NULL;
+       struct blame_origin *porigin = NULL;
        struct diff_options diff_opts;
        int i;
 
@@ -643,23 +617,26 @@ static struct origin *find_rename(struct scoreboard *sb,
                struct diff_filepair *p = diff_queued_diff.queue[i];
                if ((p->status == 'R' || p->status == 'C') &&
                    !strcmp(p->two->path, origin->path)) {
-                       porigin = get_origin(sb, parent, p->one->path);
-                       hashcpy(porigin->blob_sha1, p->one->sha1);
+                       porigin = get_origin(parent, p->one->path);
+                       oidcpy(&porigin->blob_oid, &p->one->oid);
                        porigin->mode = p->one->mode;
                        break;
                }
        }
        diff_flush(&diff_opts);
-       free_pathspec(&diff_opts.pathspec);
+       clear_pathspec(&diff_opts.pathspec);
        return porigin;
 }
 
 /*
  * Append a new blame entry to a given output queue.
  */
-static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e)
+static void add_blame_entry(struct blame_entry ***queue,
+                           const struct blame_entry *src)
 {
-       origin_incref(e->suspect);
+       struct blame_entry *e = xmalloc(sizeof(*e));
+       memcpy(e, src, sizeof(*e));
+       blame_origin_incref(e->suspect);
 
        e->next = **queue;
        **queue = e;
@@ -674,22 +651,22 @@ static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e)
 static void dup_entry(struct blame_entry ***queue,
                      struct blame_entry *dst, struct blame_entry *src)
 {
-       origin_incref(src->suspect);
-       origin_decref(dst->suspect);
+       blame_origin_incref(src->suspect);
+       blame_origin_decref(dst->suspect);
        memcpy(dst, src, sizeof(*src));
        dst->next = **queue;
        **queue = dst;
        *queue = &dst->next;
 }
 
-static const char *nth_line(struct scoreboard *sb, long lno)
+static const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
 {
        return sb->final_buf + sb->lineno[lno];
 }
 
 static const char *nth_line_cb(void *data, long lno)
 {
-       return nth_line((struct scoreboard *)data, lno);
+       return blame_nth_line((struct blame_scoreboard *)data, lno);
 }
 
 /*
@@ -709,14 +686,14 @@ static const char *nth_line_cb(void *data, long lno)
 static void split_overlap(struct blame_entry *split,
                          struct blame_entry *e,
                          int tlno, int plno, int same,
-                         struct origin *parent)
+                         struct blame_origin *parent)
 {
        int chunk_end_lno;
        memset(split, 0, sizeof(struct blame_entry [3]));
 
        if (e->s_lno < tlno) {
                /* there is a pre-chunk part not blamed on parent */
-               split[0].suspect = origin_incref(e->suspect);
+               split[0].suspect = blame_origin_incref(e->suspect);
                split[0].lno = e->lno;
                split[0].s_lno = e->s_lno;
                split[0].num_lines = tlno - e->s_lno;
@@ -730,7 +707,7 @@ static void split_overlap(struct blame_entry *split,
 
        if (same < e->s_lno + e->num_lines) {
                /* there is a post-chunk part not blamed on parent */
-               split[2].suspect = origin_incref(e->suspect);
+               split[2].suspect = blame_origin_incref(e->suspect);
                split[2].lno = e->lno + (same - e->s_lno);
                split[2].s_lno = e->s_lno + (same - e->s_lno);
                split[2].num_lines = e->s_lno + e->num_lines - same;
@@ -746,7 +723,7 @@ static void split_overlap(struct blame_entry *split,
         */
        if (split[1].num_lines < 1)
                return;
-       split[1].suspect = origin_incref(parent);
+       split[1].suspect = blame_origin_incref(parent);
 }
 
 /*
@@ -759,21 +736,15 @@ static void split_blame(struct blame_entry ***blamed,
                        struct blame_entry *split,
                        struct blame_entry *e)
 {
-       struct blame_entry *new_entry;
-
        if (split[0].suspect && split[2].suspect) {
                /* The first part (reuse storage for the existing entry e) */
                dup_entry(unblamed, e, &split[0]);
 
                /* The last part -- me */
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
-               add_blame_entry(unblamed, new_entry);
+               add_blame_entry(unblamed, &split[2]);
 
                /* ... and the middle part -- parent */
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
-               add_blame_entry(blamed, new_entry);
+               add_blame_entry(blamed, &split[1]);
        }
        else if (!split[0].suspect && !split[2].suspect)
                /*
@@ -784,18 +755,12 @@ static void split_blame(struct blame_entry ***blamed,
        else if (split[0].suspect) {
                /* me and then parent */
                dup_entry(unblamed, e, &split[0]);
-
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
-               add_blame_entry(blamed, new_entry);
+               add_blame_entry(blamed, &split[1]);
        }
        else {
                /* parent and then me */
                dup_entry(blamed, e, &split[1]);
-
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
-               add_blame_entry(unblamed, new_entry);
+               add_blame_entry(unblamed, &split[2]);
        }
 }
 
@@ -808,7 +773,7 @@ static void decref_split(struct blame_entry *split)
        int i;
 
        for (i = 0; i < 3; i++)
-               origin_decref(split[i].suspect);
+               blame_origin_decref(split[i].suspect);
 }
 
 /*
@@ -845,7 +810,7 @@ static struct blame_entry *reverse_blame(struct blame_entry *head,
  */
 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
                        int tlno, int offset, int same,
-                       struct origin *parent)
+                       struct blame_origin *parent)
 {
        struct blame_entry *e = **srcq;
        struct blame_entry *samep = NULL, *diffp = NULL;
@@ -871,10 +836,10 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
                        n->next = diffp;
                        diffp = n;
                } else
-                       origin_decref(e->suspect);
+                       blame_origin_decref(e->suspect);
                /* Pass blame for everything before the differing
                 * chunk to the parent */
-               e->suspect = origin_incref(parent);
+               e->suspect = blame_origin_incref(parent);
                e->s_lno += offset;
                e->next = samep;
                samep = e;
@@ -915,7 +880,7 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
                         */
                        int len = same - e->s_lno;
                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
-                       n->suspect = origin_incref(e->suspect);
+                       n->suspect = blame_origin_incref(e->suspect);
                        n->lno = e->lno + len;
                        n->s_lno = e->s_lno + len;
                        n->num_lines = e->num_lines - len;
@@ -936,7 +901,7 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
 }
 
 struct blame_chunk_cb_data {
-       struct origin *parent;
+       struct blame_origin *parent;
        long offset;
        struct blame_entry **dstq;
        struct blame_entry **srcq;
@@ -960,9 +925,9 @@ static int blame_chunk_cb(long start_a, long count_a,
  * for the lines it is suspected to its parent.  Run diff to find
  * which lines came from parent and pass blame for them.
  */
-static void pass_blame_to_parent(struct scoreboard *sb,
-                                struct origin *target,
-                                struct origin *parent)
+static void pass_blame_to_parent(struct blame_scoreboard *sb,
+                                struct blame_origin *target,
+                                struct blame_origin *parent)
 {
        mmfile_t file_p, file_o;
        struct blame_chunk_cb_data d;
@@ -975,9 +940,9 @@ static void pass_blame_to_parent(struct scoreboard *sb,
        d.offset = 0;
        d.dstq = &newdest; d.srcq = &target->suspects;
 
-       fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
-       fill_origin_blob(&sb->revs->diffopt, target, &file_o);
-       num_get_patch++;
+       fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
+       fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
+       sb->num_get_patch++;
 
        if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d))
                die("unable to generate diff (%s -> %s)",
@@ -1000,7 +965,7 @@ static void pass_blame_to_parent(struct scoreboard *sb,
  *
  * Compute how trivial the lines in the blame_entry are.
  */
-static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
+static unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
 {
        unsigned score;
        const char *cp, *ep;
@@ -1009,8 +974,8 @@ static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
                return e->score;
 
        score = 1;
-       cp = nth_line(sb, e->lno);
-       ep = nth_line(sb, e->lno + e->num_lines);
+       cp = blame_nth_line(sb, e->lno);
+       ep = blame_nth_line(sb, e->lno + e->num_lines);
        while (cp < ep) {
                unsigned ch = *((unsigned char *)cp);
                if (isalnum(ch))
@@ -1027,7 +992,7 @@ static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
  * so far, by comparing this and best_so_far and copying this into
  * bst_so_far as needed.
  */
-static void copy_split_if_better(struct scoreboard *sb,
+static void copy_split_if_better(struct blame_scoreboard *sb,
                                 struct blame_entry *best_so_far,
                                 struct blame_entry *this)
 {
@@ -1036,12 +1001,12 @@ static void copy_split_if_better(struct scoreboard *sb,
        if (!this[1].suspect)
                return;
        if (best_so_far[1].suspect) {
-               if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
+               if (blame_entry_score(sb, &this[1]) < blame_entry_score(sb, &best_so_far[1]))
                        return;
        }
 
        for (i = 0; i < 3; i++)
-               origin_incref(this[i].suspect);
+               blame_origin_incref(this[i].suspect);
        decref_split(best_so_far);
        memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
 }
@@ -1061,10 +1026,10 @@ static void copy_split_if_better(struct scoreboard *sb,
  *
  * All line numbers are 0-based.
  */
-static void handle_split(struct scoreboard *sb,
+static void handle_split(struct blame_scoreboard *sb,
                         struct blame_entry *ent,
                         int tlno, int plno, int same,
-                        struct origin *parent,
+                        struct blame_origin *parent,
                         struct blame_entry *split)
 {
        if (ent->num_lines <= tlno)
@@ -1080,9 +1045,9 @@ static void handle_split(struct scoreboard *sb,
 }
 
 struct handle_split_cb_data {
-       struct scoreboard *sb;
+       struct blame_scoreboard *sb;
        struct blame_entry *ent;
-       struct origin *parent;
+       struct blame_origin *parent;
        struct blame_entry *split;
        long plno;
        long tlno;
@@ -1104,9 +1069,9 @@ static int handle_split_cb(long start_a, long count_a,
  * we can pass blames to it.  file_p has the blob contents for
  * the parent.
  */
-static void find_copy_in_blob(struct scoreboard *sb,
+static void find_copy_in_blob(struct blame_scoreboard *sb,
                              struct blame_entry *ent,
-                             struct origin *parent,
+                             struct blame_origin *parent,
                              struct blame_entry *split,
                              mmfile_t *file_p)
 {
@@ -1119,9 +1084,9 @@ static void find_copy_in_blob(struct scoreboard *sb,
        /*
         * Prepare mmfile that contains only the lines in ent.
         */
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        file_o.ptr = (char *) cp;
-       file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
+       file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
 
        /*
         * file_o is a part of final image we are annotating.
@@ -1140,7 +1105,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
  * Returns a pointer to the link pointing to the old head of the small list.
  */
 
-static struct blame_entry **filter_small(struct scoreboard *sb,
+static struct blame_entry **filter_small(struct blame_scoreboard *sb,
                                         struct blame_entry **small,
                                         struct blame_entry **source,
                                         unsigned score_min)
@@ -1148,7 +1113,7 @@ static struct blame_entry **filter_small(struct scoreboard *sb,
        struct blame_entry *p = *source;
        struct blame_entry *oldsmall = *small;
        while (p) {
-               if (ent_score(sb, p) <= score_min) {
+               if (blame_entry_score(sb, p) <= score_min) {
                        *small = p;
                        small = &p->next;
                        p = *small;
@@ -1167,11 +1132,11 @@ static struct blame_entry **filter_small(struct scoreboard *sb,
  * See if lines currently target is suspected for can be attributed to
  * parent.
  */
-static void find_move_in_parent(struct scoreboard *sb,
+static void find_move_in_parent(struct blame_scoreboard *sb,
                                struct blame_entry ***blamed,
                                struct blame_entry **toosmall,
-                               struct origin *target,
-                               struct origin *parent)
+                               struct blame_origin *target,
+                               struct blame_origin *parent)
 {
        struct blame_entry *e, split[3];
        struct blame_entry *unblamed = target->suspects;
@@ -1181,7 +1146,7 @@ static void find_move_in_parent(struct scoreboard *sb,
        if (!unblamed)
                return; /* nothing remains for this target */
 
-       fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
+       fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
        if (!file_p.ptr)
                return;
 
@@ -1197,7 +1162,7 @@ static void find_move_in_parent(struct scoreboard *sb,
                        next = e->next;
                        find_copy_in_blob(sb, e, parent, split, &file_p);
                        if (split[1].suspect &&
-                           blame_move_score < ent_score(sb, &split[1])) {
+                           sb->move_score < blame_entry_score(sb, &split[1])) {
                                split_blame(blamed, &unblamedtail, split, e);
                        } else {
                                e->next = leftover;
@@ -1206,7 +1171,7 @@ static void find_move_in_parent(struct scoreboard *sb,
                        decref_split(split);
                }
                *unblamedtail = NULL;
-               toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
+               toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
        } while (unblamed);
        target->suspects = reverse_blame(leftover, NULL);
 }
@@ -1243,12 +1208,12 @@ static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
  * across file boundary from the parent commit.  porigin is the path
  * in the parent we already tried.
  */
-static void find_copy_in_parent(struct scoreboard *sb,
+static void find_copy_in_parent(struct blame_scoreboard *sb,
                                struct blame_entry ***blamed,
                                struct blame_entry **toosmall,
-                               struct origin *target,
+                               struct blame_origin *target,
                                struct commit *parent,
-                               struct origin *porigin,
+                               struct blame_origin *porigin,
                                int opt)
 {
        struct diff_options diff_opts;
@@ -1295,7 +1260,7 @@ static void find_copy_in_parent(struct scoreboard *sb,
 
                for (i = 0; i < diff_queued_diff.nr; i++) {
                        struct diff_filepair *p = diff_queued_diff.queue[i];
-                       struct origin *norigin;
+                       struct blame_origin *norigin;
                        mmfile_t file_p;
                        struct blame_entry this[3];
 
@@ -1307,10 +1272,10 @@ static void find_copy_in_parent(struct scoreboard *sb,
                                /* find_move already dealt with this path */
                                continue;
 
-                       norigin = get_origin(sb, parent, p->one->path);
-                       hashcpy(norigin->blob_sha1, p->one->sha1);
+                       norigin = get_origin(parent, p->one->path);
+                       oidcpy(&norigin->blob_oid, &p->one->oid);
                        norigin->mode = p->one->mode;
-                       fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
+                       fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
                        if (!file_p.ptr)
                                continue;
 
@@ -1321,13 +1286,13 @@ static void find_copy_in_parent(struct scoreboard *sb,
                                                     this);
                                decref_split(this);
                        }
-                       origin_decref(norigin);
+                       blame_origin_decref(norigin);
                }
 
                for (j = 0; j < num_ents; j++) {
                        struct blame_entry *split = blame_list[j].split;
                        if (split[1].suspect &&
-                           blame_copy_score < ent_score(sb, &split[1])) {
+                           sb->copy_score < blame_entry_score(sb, &split[1])) {
                                split_blame(blamed, &unblamedtail, split,
                                            blame_list[j].ent);
                        } else {
@@ -1338,19 +1303,19 @@ static void find_copy_in_parent(struct scoreboard *sb,
                }
                free(blame_list);
                *unblamedtail = NULL;
-               toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
+               toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
        } while (unblamed);
        target->suspects = reverse_blame(leftover, NULL);
        diff_flush(&diff_opts);
-       free_pathspec(&diff_opts.pathspec);
+       clear_pathspec(&diff_opts.pathspec);
 }
 
 /*
  * The blobs of origin and porigin exactly match, so everything
  * origin is suspected for can be blamed on the parent.
  */
-static void pass_whole_blame(struct scoreboard *sb,
-                            struct origin *origin, struct origin *porigin)
+static void pass_whole_blame(struct blame_scoreboard *sb,
+                            struct blame_origin *origin, struct blame_origin *porigin)
 {
        struct blame_entry *e, *suspects;
 
@@ -1362,8 +1327,8 @@ static void pass_whole_blame(struct scoreboard *sb,
        suspects = origin->suspects;
        origin->suspects = NULL;
        for (e = suspects; e; e = e->next) {
-               origin_incref(porigin);
-               origin_decref(e->suspect);
+               blame_origin_incref(porigin);
+               blame_origin_decref(e->suspect);
                e->suspect = porigin;
        }
        queue_blames(sb, porigin, suspects);
@@ -1397,12 +1362,12 @@ static int num_scapegoats(struct rev_info *revs, struct commit *commit)
 /* Distribute collected unsorted blames to the respected sorted lists
  * in the various origins.
  */
-static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
+static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
 {
        blamed = blame_sort(blamed, compare_blame_suspect);
        while (blamed)
        {
-               struct origin *porigin = blamed->suspect;
+               struct blame_origin *porigin = blamed->suspect;
                struct blame_entry *suspects = NULL;
                do {
                        struct blame_entry *next = blamed->next;
@@ -1417,14 +1382,14 @@ static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
 
 #define MAXSG 16
 
-static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
+static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
 {
        struct rev_info *revs = sb->revs;
        int i, pass, num_sg;
        struct commit *commit = origin->commit;
        struct commit_list *sg;
-       struct origin *sg_buf[MAXSG];
-       struct origin *porigin, **sg_origin = sg_buf;
+       struct blame_origin *sg_buf[MAXSG];
+       struct blame_origin *porigin, **sg_origin = sg_buf;
        struct blame_entry *toosmall = NULL;
        struct blame_entry *blames, **blametail = &blames;
 
@@ -1441,8 +1406,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
         * common cases, then we look for renames in the second pass.
         */
        for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
-               struct origin *(*find)(struct scoreboard *,
-                                      struct commit *, struct origin *);
+               struct blame_origin *(*find)(struct commit *, struct blame_origin *);
                find = pass ? find_rename : find_origin;
 
                for (i = 0, sg = first_scapegoat(revs, commit);
@@ -1455,37 +1419,36 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
                                continue;
                        if (parse_commit(p))
                                continue;
-                       porigin = find(sb, p, origin);
+                       porigin = find(p, origin);
                        if (!porigin)
                                continue;
-                       if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
+                       if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
                                pass_whole_blame(sb, origin, porigin);
-                               origin_decref(porigin);
+                               blame_origin_decref(porigin);
                                goto finish;
                        }
                        for (j = same = 0; j < i; j++)
                                if (sg_origin[j] &&
-                                   !hashcmp(sg_origin[j]->blob_sha1,
-                                            porigin->blob_sha1)) {
+                                   !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
                                        same = 1;
                                        break;
                                }
                        if (!same)
                                sg_origin[i] = porigin;
                        else
-                               origin_decref(porigin);
+                               blame_origin_decref(porigin);
                }
        }
 
-       num_commits++;
+       sb->num_commits++;
        for (i = 0, sg = first_scapegoat(revs, commit);
             i < num_sg && sg;
             sg = sg->next, i++) {
-               struct origin *porigin = sg_origin[i];
+               struct blame_origin *porigin = sg_origin[i];
                if (!porigin)
                        continue;
                if (!origin->previous) {
-                       origin_incref(porigin);
+                       blame_origin_incref(porigin);
                        origin->previous = porigin;
                }
                pass_blame_to_parent(sb, origin, porigin);
@@ -1497,12 +1460,12 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
         * Optionally find moves in parents' files.
         */
        if (opt & PICKAXE_BLAME_MOVE) {
-               filter_small(sb, &toosmall, &origin->suspects, blame_move_score);
+               filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
                if (origin->suspects) {
                        for (i = 0, sg = first_scapegoat(revs, commit);
                             i < num_sg && sg;
                             sg = sg->next, i++) {
-                               struct origin *porigin = sg_origin[i];
+                               struct blame_origin *porigin = sg_origin[i];
                                if (!porigin)
                                        continue;
                                find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
@@ -1516,12 +1479,12 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
         * Optionally find copies from parents' files.
         */
        if (opt & PICKAXE_BLAME_COPY) {
-               if (blame_copy_score > blame_move_score)
-                       filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
-               else if (blame_copy_score < blame_move_score) {
+               if (sb->copy_score > sb->move_score)
+                       filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
+               else if (sb->copy_score < sb->move_score) {
                        origin->suspects = blame_merge(origin->suspects, toosmall);
                        toosmall = NULL;
-                       filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
+                       filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
                }
                if (!origin->suspects)
                        goto finish;
@@ -1529,7 +1492,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
                for (i = 0, sg = first_scapegoat(revs, commit);
                     i < num_sg && sg;
                     sg = sg->next, i++) {
-                       struct origin *porigin = sg_origin[i];
+                       struct blame_origin *porigin = sg_origin[i];
                        find_copy_in_parent(sb, &blametail, &toosmall,
                                            origin, sg->item, porigin, opt);
                        if (!origin->suspects)
@@ -1556,7 +1519,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
        for (i = 0; i < num_sg; i++) {
                if (sg_origin[i]) {
                        drop_origin_blob(sg_origin[i]);
-                       origin_decref(sg_origin[i]);
+                       blame_origin_decref(sg_origin[i]);
                }
        }
        drop_origin_blob(origin);
@@ -1570,13 +1533,13 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
 struct commit_info {
        struct strbuf author;
        struct strbuf author_mail;
-       unsigned long author_time;
+       timestamp_t author_time;
        struct strbuf author_tz;
 
        /* filled only when asked for details */
        struct strbuf committer;
        struct strbuf committer_mail;
-       unsigned long committer_time;
+       timestamp_t committer_time;
        struct strbuf committer_tz;
 
        struct strbuf summary;
@@ -1587,7 +1550,7 @@ struct commit_info {
  */
 static void get_ac_line(const char *inbuf, const char *what,
        struct strbuf *name, struct strbuf *mail,
-       unsigned long *time, struct strbuf *tz)
+       timestamp_t *time, struct strbuf *tz)
 {
        struct ident_split ident;
        size_t len, maillen, namelen;
@@ -1700,13 +1663,23 @@ static void get_commit_info(struct commit *commit,
 }
 
 /*
+ * Write out any suspect information which depends on the path. This must be
+ * handled separately from emit_one_suspect_detail(), because a given commit
+ * may have changes in multiple paths. So this needs to appear each time
+ * we mention a new group.
+ *
  * To allow LF and other nonportable characters in pathnames,
  * they are c-style quoted as needed.
  */
-static void write_filename_info(const char *path)
+static void write_filename_info(struct blame_origin *suspect)
 {
+       if (suspect->previous) {
+               struct blame_origin *prev = suspect->previous;
+               printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
+               write_name_quoted(prev->path, stdout, '\n');
+       }
        printf("filename ");
-       write_name_quoted(path, stdout, '\n');
+       write_name_quoted(suspect->path, stdout, '\n');
 }
 
 /*
@@ -1715,7 +1688,7 @@ static void write_filename_info(const char *path)
  * the first time each commit appears in the output (unless the
  * user has specifically asked for us to repeat).
  */
-static int emit_one_suspect_detail(struct origin *suspect, int repeat)
+static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
 {
        struct commit_info ci;
 
@@ -1726,20 +1699,15 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
        get_commit_info(suspect->commit, &ci, 1);
        printf("author %s\n", ci.author.buf);
        printf("author-mail %s\n", ci.author_mail.buf);
-       printf("author-time %lu\n", ci.author_time);
+       printf("author-time %"PRItime"\n", ci.author_time);
        printf("author-tz %s\n", ci.author_tz.buf);
        printf("committer %s\n", ci.committer.buf);
        printf("committer-mail %s\n", ci.committer_mail.buf);
-       printf("committer-time %lu\n", ci.committer_time);
+       printf("committer-time %"PRItime"\n", ci.committer_time);
        printf("committer-tz %s\n", ci.committer_tz.buf);
        printf("summary %s\n", ci.summary.buf);
        if (suspect->commit->object.flags & UNINTERESTING)
                printf("boundary\n");
-       if (suspect->previous) {
-               struct origin *prev = suspect->previous;
-               printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
-               write_name_quoted(prev->path, stdout, '\n');
-       }
 
        commit_info_destroy(&ci);
 
@@ -1754,13 +1722,13 @@ static void found_guilty_entry(struct blame_entry *ent,
                           struct progress_info *pi)
 {
        if (incremental) {
-               struct origin *suspect = ent->suspect;
+               struct blame_origin *suspect = ent->suspect;
 
                printf("%s %d %d %d\n",
                       oid_to_hex(&suspect->commit->object.oid),
                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
                emit_one_suspect_detail(suspect, 0);
-               write_filename_info(suspect->path);
+               write_filename_info(suspect);
                maybe_flush_or_die(stdout, "stdout");
        }
        pi->blamed_lines += ent->num_lines;
@@ -1771,7 +1739,7 @@ static void found_guilty_entry(struct blame_entry *ent,
  * The main loop -- while we have blobs with lines whose true origin
  * is still unknown, pick one blob, and allow its lines to pass blames
  * to its parents. */
-static void assign_blame(struct scoreboard *sb, int opt)
+static void assign_blame(struct blame_scoreboard *sb, int opt)
 {
        struct rev_info *revs = sb->revs;
        struct commit *commit = prio_queue_get(&sb->commits);
@@ -1783,7 +1751,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
 
        while (commit) {
                struct blame_entry *ent;
-               struct origin *suspect = commit->util;
+               struct blame_origin *suspect = commit->util;
 
                /* find one suspect to break down */
                while (suspect && !suspect->suspects)
@@ -1800,7 +1768,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
                 * We will use this suspect later in the loop,
                 * so hold onto it in the meantime.
                 */
-               origin_incref(suspect);
+               blame_origin_incref(suspect);
                parse_commit(commit);
                if (reverse ||
                    (!(commit->object.flags & UNINTERESTING) &&
@@ -1832,7 +1800,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
                                break;
                        }
                }
-               origin_decref(suspect);
+               blame_origin_decref(suspect);
 
                if (DEBUG) /* sanity */
                        sanity_check_refcnt(sb);
@@ -1841,14 +1809,14 @@ static void assign_blame(struct scoreboard *sb, int opt)
        stop_progress(&pi.progress);
 }
 
-static const char *format_time(unsigned long time, const char *tz_str,
+static const char *format_time(timestamp_t time, const char *tz_str,
                               int show_raw_time)
 {
        static struct strbuf time_buf = STRBUF_INIT;
 
        strbuf_reset(&time_buf);
        if (show_raw_time) {
-               strbuf_addf(&time_buf, "%lu %s", time, tz_str);
+               strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
        }
        else {
                const char *time_str;
@@ -1880,23 +1848,23 @@ static const char *format_time(unsigned long time, const char *tz_str,
 #define OUTPUT_SHOW_EMAIL      0400
 #define OUTPUT_LINE_PORCELAIN 01000
 
-static void emit_porcelain_details(struct origin *suspect, int repeat)
+static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
 {
        if (emit_one_suspect_detail(suspect, repeat) ||
            (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
-               write_filename_info(suspect->path);
+               write_filename_info(suspect);
 }
 
-static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
+static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
                           int opt)
 {
        int repeat = opt & OUTPUT_LINE_PORCELAIN;
        int cnt;
        const char *cp;
-       struct origin *suspect = ent->suspect;
-       char hex[GIT_SHA1_HEXSZ + 1];
+       struct blame_origin *suspect = ent->suspect;
+       char hex[GIT_MAX_HEXSZ + 1];
 
-       sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
+       oid_to_hex_r(hex, &suspect->commit->object.oid);
        printf("%s %d %d %d\n",
               hex,
               ent->s_lno + 1,
@@ -1904,7 +1872,7 @@ static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
               ent->num_lines);
        emit_porcelain_details(suspect, repeat);
 
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
                if (cnt) {
@@ -1926,22 +1894,22 @@ static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
                putchar('\n');
 }
 
-static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
+static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
 {
        int cnt;
        const char *cp;
-       struct origin *suspect = ent->suspect;
+       struct blame_origin *suspect = ent->suspect;
        struct commit_info ci;
-       char hex[GIT_SHA1_HEXSZ + 1];
+       char hex[GIT_MAX_HEXSZ + 1];
        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
 
        get_commit_info(suspect->commit, &ci, 1);
-       sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
+       oid_to_hex_r(hex, &suspect->commit->object.oid);
 
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
-               int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev;
+               int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
                if (suspect->commit->object.flags & UNINTERESTING) {
                        if (blank_boundary)
@@ -2005,14 +1973,14 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
        commit_info_destroy(&ci);
 }
 
-static void output(struct scoreboard *sb, int option)
+static void output(struct blame_scoreboard *sb, int option)
 {
        struct blame_entry *ent;
 
        if (option & OUTPUT_PORCELAIN) {
                for (ent = sb->ent; ent; ent = ent->next) {
                        int count = 0;
-                       struct origin *suspect;
+                       struct blame_origin *suspect;
                        struct commit *commit = ent->suspect->commit;
                        if (commit->object.flags & MORE_THAN_ONE_PATH)
                                continue;
@@ -2044,7 +2012,7 @@ static const char *get_next_line(const char *start, const char *end)
  * To allow quick access to the contents of nth line in the
  * final image, prepare an index in the scoreboard.
  */
-static int prepare_lines(struct scoreboard *sb)
+static int prepare_lines(struct blame_scoreboard *sb)
 {
        const char *buf = sb->final_buf;
        unsigned long len = sb->final_buf_size;
@@ -2090,7 +2058,7 @@ static int read_ancestry(const char *graft_file)
        return 0;
 }
 
-static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
+static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
 {
        const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
                                              auto_abbrev);
@@ -2104,17 +2072,17 @@ static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
  * How many columns do we need to show line numbers, authors,
  * and filenames?
  */
-static void find_alignment(struct scoreboard *sb, int *option)
+static void find_alignment(struct blame_scoreboard *sb, int *option)
 {
        int longest_src_lines = 0;
        int longest_dst_lines = 0;
        unsigned largest_score = 0;
        struct blame_entry *e;
        int compute_auto_abbrev = (abbrev < 0);
-       int auto_abbrev = default_abbrev;
+       int auto_abbrev = DEFAULT_ABBREV;
 
        for (e = sb->ent; e; e = e->next) {
-               struct origin *suspect = e->suspect;
+               struct blame_origin *suspect = e->suspect;
                int num;
 
                if (compute_auto_abbrev)
@@ -2142,8 +2110,8 @@ static void find_alignment(struct scoreboard *sb, int *option)
                num = e->lno + e->num_lines;
                if (longest_dst_lines < num)
                        longest_dst_lines = num;
-               if (largest_score < ent_score(sb, e))
-                       largest_score = ent_score(sb, e);
+               if (largest_score < blame_entry_score(sb, e))
+                       largest_score = blame_entry_score(sb, e);
        }
        max_orig_digits = decimal_width(longest_src_lines);
        max_digits = decimal_width(longest_dst_lines);
@@ -2158,7 +2126,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
  * For debugging -- origin is refcounted, and this asserts that
  * we do not underflow.
  */
-static void sanity_check_refcnt(struct scoreboard *sb)
+static void sanity_check_refcnt(struct blame_scoreboard *sb)
 {
        int baa = 0;
        struct blame_entry *ent;
@@ -2220,6 +2188,8 @@ static int git_blame_config(const char *var, const char *value, void *cb)
                return 0;
        }
 
+       if (git_diff_heuristic_config(var, value, cb) < 0)
+               return -1;
        if (userdiff_config(var, value) < 0)
                return -1;
 
@@ -2232,31 +2202,32 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
        int pos;
 
        for (parents = work_tree->parents; parents; parents = parents->next) {
-               const unsigned char *commit_sha1 = parents->item->object.oid.hash;
-               unsigned char blob_sha1[20];
+               const struct object_id *commit_oid = &parents->item->object.oid;
+               struct object_id blob_oid;
                unsigned mode;
 
-               if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) &&
-                   sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
+               if (!get_tree_entry(commit_oid->hash, path, blob_oid.hash, &mode) &&
+                   sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
                        return;
        }
 
        pos = cache_name_pos(path, strlen(path));
        if (pos >= 0)
                ; /* path is in the index */
-       else if (!strcmp(active_cache[-1 - pos]->name, path))
+       else if (-1 - pos < active_nr &&
+                !strcmp(active_cache[-1 - pos]->name, path))
                ; /* path is in the index, unmerged */
        else
                die("no such path '%s' in HEAD", path);
 }
 
-static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
+static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid)
 {
        struct commit *parent;
 
-       parent = lookup_commit_reference(sha1);
+       parent = lookup_commit_reference(oid->hash);
        if (!parent)
-               die("no such commit %s", sha1_to_hex(sha1));
+               die("no such commit %s", oid_to_hex(oid));
        return &commit_list_insert(parent, tail)->next;
 }
 
@@ -2273,10 +2244,10 @@ static void append_merge_parents(struct commit_list **tail)
        }
 
        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
-               unsigned char sha1[20];
-               if (line.len < 40 || get_sha1_hex(line.buf, sha1))
+               struct object_id oid;
+               if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
                        die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
-               tail = append_parent(tail, sha1);
+               tail = append_parent(tail, &oid);
        }
        close(merge_head);
        strbuf_release(&line);
@@ -2303,9 +2274,9 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
                                               const char *contents_from)
 {
        struct commit *commit;
-       struct origin *origin;
+       struct blame_origin *origin;
        struct commit_list **parent_tail, *parent;
-       unsigned char head_sha1[20];
+       struct object_id head_oid;
        struct strbuf buf = STRBUF_INIT;
        const char *ident;
        time_t now;
@@ -2321,10 +2292,10 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        commit->date = now;
        parent_tail = &commit->parents;
 
-       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
+       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL))
                die("no such ref: HEAD");
 
-       parent_tail = append_parent(parent_tail, head_sha1);
+       parent_tail = append_parent(parent_tail, &head_oid);
        append_merge_parents(parent_tail);
        verify_working_tree_path(commit, path);
 
@@ -2365,7 +2336,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
                switch (st.st_mode & S_IFMT) {
                case S_IFREG:
                        if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
-                           textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len))
+                           textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
                                strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
                        else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
                                die_errno("cannot open or read '%s'", read_from);
@@ -2387,7 +2358,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        convert_to_git(path, buf.buf, buf.len, &buf, 0);
        origin->file.ptr = buf.buf;
        origin->file.size = buf.len;
-       pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
+       pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);
 
        /*
         * Read the current index, replace the path entry with
@@ -2409,7 +2380,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        }
        size = cache_entry_size(len);
        ce = xcalloc(1, size);
-       hashcpy(ce->sha1, origin->blob_sha1);
+       oidcpy(&ce->oid, &origin->blob_oid);
        memcpy(ce->name, path, len);
        ce->ce_flags = create_ce_flags(0);
        ce->ce_namelen = len;
@@ -2446,14 +2417,49 @@ static struct commit *find_single_final(struct rev_info *revs,
        return found;
 }
 
-static char *prepare_final(struct scoreboard *sb)
+static char *prepare_final(struct blame_scoreboard *sb)
 {
        const char *name;
        sb->final = find_single_final(sb->revs, &name);
        return xstrdup_or_null(name);
 }
 
-static char *prepare_initial(struct scoreboard *sb)
+static const char *dwim_reverse_initial(struct blame_scoreboard *sb)
+{
+       /*
+        * DWIM "git blame --reverse ONE -- PATH" as
+        * "git blame --reverse ONE..HEAD -- PATH" but only do so
+        * when it makes sense.
+        */
+       struct object *obj;
+       struct commit *head_commit;
+       unsigned char head_sha1[20];
+
+       if (sb->revs->pending.nr != 1)
+               return NULL;
+
+       /* Is that sole rev a committish? */
+       obj = sb->revs->pending.objects[0].item;
+       obj = deref_tag(obj, NULL, 0);
+       if (obj->type != OBJ_COMMIT)
+               return NULL;
+
+       /* Do we have HEAD? */
+       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
+               return NULL;
+       head_commit = lookup_commit_reference_gently(head_sha1, 1);
+       if (!head_commit)
+               return NULL;
+
+       /* Turn "ONE" into "ONE..HEAD" then */
+       obj->flags |= UNINTERESTING;
+       add_pending_object(sb->revs, &head_commit->object, "HEAD");
+
+       sb->final = (struct commit *)obj;
+       return sb->revs->pending.objects[0].name;
+}
+
+static char *prepare_initial(struct blame_scoreboard *sb)
 {
        int i;
        const char *final_commit_name = NULL;
@@ -2471,14 +2477,17 @@ static char *prepare_initial(struct scoreboard *sb)
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (sb->final)
-                       die("More than one commit to dig down to %s and %s?",
+                       die("More than one commit to dig up from, %s and %s?",
                            revs->pending.objects[i].name,
                            final_commit_name);
                sb->final = (struct commit *) obj;
                final_commit_name = revs->pending.objects[i].name;
        }
+
+       if (!final_commit_name)
+               final_commit_name = dwim_reverse_initial(sb);
        if (!final_commit_name)
-               die("No commit to dig down to?");
+               die("No commit to dig up from?");
        return xstrdup(final_commit_name);
 }
 
@@ -2519,20 +2528,20 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 {
        struct rev_info revs;
        const char *path;
-       struct scoreboard sb;
-       struct origin *o;
+       struct blame_scoreboard sb;
+       struct blame_origin *o;
        struct blame_entry *ent = NULL;
        long dashdash_pos, lno;
        char *final_commit_name = NULL;
        enum object_type type;
        struct commit *final_commit = NULL;
 
-       static struct string_list range_list;
-       static int output_option = 0, opt = 0;
-       static int show_stats = 0;
-       static const char *revs_file = NULL;
-       static const char *contents_from = NULL;
-       static const struct option options[] = {
+       struct string_list range_list = STRING_LIST_INIT_NODUP;
+       int output_option = 0, opt = 0;
+       int show_stats = 0;
+       const char *revs_file = NULL;
+       const char *contents_from = NULL;
+       const struct option options[] = {
                OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
                OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
                OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
@@ -2549,6 +2558,14 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
+
+               /*
+                * The following two options are parsed by parse_revision_opt()
+                * and are only included here to get included in the "-h"
+                * output:
+                */
+               { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
+
                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
@@ -2595,19 +2612,22 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        }
 parse_done:
        no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
+       xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
        argc = parse_options_end(&ctx);
 
        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
                if (show_progress > 0)
-                       die("--progress can't be used with --incremental or porcelain formats");
+                       die(_("--progress can't be used with --incremental or porcelain formats"));
                show_progress = 0;
        } else if (show_progress < 0)
                show_progress = isatty(2);
 
-       if (0 < abbrev)
+       if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
                /* one more abbrev length is needed for the boundary commit */
                abbrev++;
+       else if (!abbrev)
+               abbrev = GIT_SHA1_HEXSZ;
 
        if (revs_file && read_ancestry(revs_file))
                die_errno("reading graft file '%s' failed", revs_file);
@@ -2661,11 +2681,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
                        PICKAXE_BLAME_COPY_HARDER);
 
-       if (!blame_move_score)
-               blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
-       if (!blame_copy_score)
-               blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
-
        /*
         * We have collected options unknown to us in argv[1..unk]
         * which are to be passed to revision machinery if we are
@@ -2719,14 +2734,17 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        revs.disable_stdin = 1;
        setup_revisions(argc, argv, &revs, NULL);
        memset(&sb, 0, sizeof(sb));
+       sb.move_score = BLAME_DEFAULT_MOVE_SCORE;
+       sb.copy_score = BLAME_DEFAULT_COPY_SCORE;
 
        sb.revs = &revs;
+       sb.contents_from = contents_from;
        if (!reverse) {
                final_commit_name = prepare_final(&sb);
                sb.commits.compare = compare_commits_by_commit_date;
        }
        else if (contents_from)
-               die("--contents and --reverse do not blend well.");
+               die(_("--contents and --reverse do not blend well."));
        else {
                final_commit_name = prepare_initial(&sb);
                sb.commits.compare = compare_commits_by_reverse_commit_date;
@@ -2746,12 +2764,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                add_pending_object(&revs, &(sb.final->object), ":");
        }
        else if (contents_from)
-               die("Cannot use --contents with final commit object name");
+               die(_("cannot use --contents with final commit object name"));
 
        if (reverse && revs.first_parent_only) {
                final_commit = find_single_final(sb.revs, NULL);
                if (!final_commit)
-                       die("--reverse and --first-parent together require specified latest commit");
+                       die(_("--reverse and --first-parent together require specified latest commit"));
        }
 
        /*
@@ -2778,7 +2796,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                }
 
                if (oidcmp(&c->object.oid, &sb.final->object.oid))
-                       die("--reverse --first-parent together require range along first-parent chain");
+                       die(_("--reverse --first-parent together require range along first-parent chain"));
        }
 
        if (is_null_oid(&sb.final->object.oid)) {
@@ -2787,28 +2805,28 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                sb.final_buf_size = o->file.size;
        }
        else {
-               o = get_origin(&sb, sb.final, path);
+               o = get_origin(sb.final, path);
                if (fill_blob_sha1_and_mode(o))
-                       die("no such path %s in %s", path, final_commit_name);
+                       die(_("no such path %s in %s"), path, final_commit_name);
 
                if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
-                   textconv_object(path, o->mode, o->blob_sha1, 1, (char **) &sb.final_buf,
+                   textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb.final_buf,
                                    &sb.final_buf_size))
                        ;
                else
-                       sb.final_buf = read_sha1_file(o->blob_sha1, &type,
+                       sb.final_buf = read_sha1_file(o->blob_oid.hash, &type,
                                                      &sb.final_buf_size);
 
                if (!sb.final_buf)
-                       die("Cannot read blob %s for path %s",
-                           sha1_to_hex(o->blob_sha1),
+                       die(_("cannot read blob %s for path %s"),
+                           oid_to_hex(&o->blob_oid),
                            path);
        }
-       num_read_blob++;
+       sb.num_read_blob++;
        lno = prepare_lines(&sb);
 
        if (lno && !range_list.nr)
-               string_list_append(&range_list, xstrdup("1"));
+               string_list_append(&range_list, "1");
 
        anchor = 1;
        range_set_init(&ranges, range_list.nr);
@@ -2819,7 +2837,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                                    &bottom, &top, sb.path))
                        usage(blame_usage);
                if (lno < top || ((lno || bottom) && lno < bottom))
-                       die("file %s has only %lu lines", path, lno);
+                       die(Q_("file %s has only %lu line",
+                              "file %s has only %lu lines",
+                              lno), path, lno);
                if (bottom < 1)
                        bottom = 1;
                if (top < 1)
@@ -2841,13 +2861,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                ent->suspect = o;
                ent->s_lno = bottom;
                ent->next = next;
-               origin_incref(o);
+               blame_origin_incref(o);
        }
 
        o->suspects = ent;
        prio_queue_put(&sb.commits, o->commit);
 
-       origin_decref(o);
+       blame_origin_decref(o);
 
        range_set_release(&ranges);
        string_list_clear(&range_list, 0);
@@ -2855,6 +2875,11 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        sb.ent = NULL;
        sb.path = path;
 
+       if (blame_move_score)
+               sb.move_score = blame_move_score;
+       if (blame_copy_score)
+               sb.copy_score = blame_copy_score;
+
        read_mailmap(&mailmap, NULL);
 
        assign_blame(&sb, opt);
@@ -2869,7 +2894,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
        sb.ent = blame_sort(sb.ent, compare_blame_final);
 
-       coalesce(&sb);
+       blame_coalesce(&sb);
 
        if (!(output_option & OUTPUT_PORCELAIN))
                find_alignment(&sb, &output_option);
@@ -2883,9 +2908,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        }
 
        if (show_stats) {
-               printf("num read blob: %d\n", num_read_blob);
-               printf("num get patch: %d\n", num_get_patch);
-               printf("num commits: %d\n", num_commits);
+               printf("num read blob: %d\n", sb.num_read_blob);
+               printf("num get patch: %d\n", sb.num_get_patch);
+               printf("num commits: %d\n", sb.num_commits);
        }
        return 0;
 }