Imported Upstream version 2.12.3
[platform/upstream/git.git] / builtin / blame.c
index d8b2760..f7aa95f 100644 (file)
@@ -1,10 +1,12 @@
 /*
  * Blame
  *
- * Copyright (c) 2006, Junio C Hamano
+ * Copyright (c) 2006, 2014 by its authors
+ * See COPYING for licensing conditions
  */
 
 #include "cache.h"
+#include "refs.h"
 #include "builtin.h"
 #include "blob.h"
 #include "commit.h"
 #include "cache-tree.h"
 #include "string-list.h"
 #include "mailmap.h"
+#include "mergesort.h"
 #include "parse-options.h"
+#include "prio-queue.h"
 #include "utf8.h"
 #include "userdiff.h"
 #include "line-range.h"
 #include "line-log.h"
+#include "dir.h"
+#include "progress.h"
 
-static char blame_usage[] = N_("git blame [options] [rev-opts] [rev] [--] file");
+static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
 
 static const char *blame_opt_usage[] = {
        blame_usage,
        "",
-       N_("[rev-opts] are documented in git-rev-list(1)"),
+       N_("<rev-opts> are documented in git-rev-list(1)"),
        NULL
 };
 
@@ -45,11 +51,12 @@ static int incremental;
 static int xdl_opts;
 static int abbrev = -1;
 static int no_whole_file_rename;
+static int show_progress;
 
-static enum date_mode blame_date_mode = DATE_ISO8601;
+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
@@ -83,15 +90,51 @@ static unsigned blame_copy_score;
  */
 struct origin {
        int refcnt;
+       /* Record preceding blame record for this blob */
        struct 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
+        * towards the origin's reference count.  It is tempting to
+        * let it count as long as the commit is pending examination,
+        * but even under circumstances where the commit will be
+        * present multiple times in the priority queue of unexamined
+        * commits, processing the first instance will not leave any
+        * work requiring the origin data for the second instance.  An
+        * interspersed commit changing that would have to be
+        * preexisting with a different ancestry and with the same
+        * commit date in order to wedge itself between two instances
+        * of the same commit in the priority queue _and_ produce
+        * blame entries relevant for it.  While we don't want to let
+        * us get tripped up by this case, it certainly does not seem
+        * worth optimizing for.
+        */
+       struct 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
+        * is being processed, all suspects will be moved, either by
+        * assigning them to an origin in a different commit, or by
+        * shipping them to the scoreboard's ent list because they
+        * cannot be attributed to a different commit.
+        */
+       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
+        */
+       char guilty;
        char path[FLEX_ARRAY];
 };
 
-static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
+struct progress_info {
+       struct progress *progress;
+       int blamed_lines;
+};
+
+static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
                      xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
 {
        xpparam_t xpp = {0};
@@ -99,7 +142,6 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
        xdemitcb_t ecb = {NULL};
 
        xpp.flags = xdl_opts;
-       xecfg.ctxlen = ctxlen;
        xecfg.hunk_func = hunk_func;
        ecb.priv = cb_data;
        return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
@@ -112,8 +154,8 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
  */
 int textconv_object(const char *path,
                    unsigned mode,
-                   const unsigned char *sha1,
-                   int sha1_valid,
+                   const struct object_id *oid,
+                   int oid_valid,
                    char **buf,
                    unsigned long *buf_size)
 {
@@ -121,7 +163,7 @@ int textconv_object(const char *path,
        struct userdiff_driver *textconv;
 
        df = alloc_filespec(path);
-       fill_filespec(df, sha1, sha1_valid, mode);
+       fill_filespec(df, oid->hash, oid_valid, mode);
        textconv = get_textconv(df);
        if (!textconv) {
                free_filespec(df);
@@ -146,15 +188,16 @@ static void fill_origin_blob(struct diff_options *opt,
 
                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;
        }
@@ -176,10 +219,22 @@ static inline struct origin *origin_incref(struct origin *o)
 static void origin_decref(struct origin *o)
 {
        if (o && --o->refcnt <= 0) {
+               struct origin *p, *l = NULL;
                if (o->previous)
                        origin_decref(o->previous);
                free(o->file.ptr);
-               free(o);
+               /* Should be present exactly once in commit chain */
+               for (p = o->commit->util; p; l = p, p = p->next) {
+                       if (p == o) {
+                               if (l)
+                                       l->next = p->next;
+                               else
+                                       o->commit->util = p->next;
+                               free(o);
+                               return;
+                       }
+               }
+               die("internal error in blame::origin_decref");
        }
 }
 
@@ -193,8 +248,12 @@ static void drop_origin_blob(struct origin *o)
 
 /*
  * Each group of lines is described by a blame_entry; it can be split
- * as we pass blame to the parents.  They form a linked list in the
- * scoreboard structure, sorted by the target line number.
+ * as we pass blame to the parents.  They are arranged in linked lists
+ * kept as `suspects' of some unprocessed origin, or entered (when the
+ * blame origin has been finalized) into the scoreboard structure.
+ * While the scoreboard structure is only sorted at the end of
+ * processing (according to final image line number), the lists
+ * attached to an origin are sorted by the target line number.
  */
 struct blame_entry {
        struct blame_entry *next;
@@ -210,15 +269,6 @@ struct blame_entry {
        /* the commit that introduced this group into the final image */
        struct origin *suspect;
 
-       /* true if the suspect is truly guilty; false while we have not
-        * checked if the group came from one of its parents.
-        */
-       char guilty;
-
-       /* true if the entry has been scanned for copies in the current parent
-        */
-       char scanned;
-
        /* the line number of the first line of this group in the
         * suspect's file; internally all line numbers are 0 based.
         */
@@ -231,11 +281,112 @@ struct blame_entry {
 };
 
 /*
+ * Any merge of blames happens on lists of blames that arrived via
+ * different parents in a single suspect.  In this case, we want to
+ * sort according to the suspect line numbers as opposed to the final
+ * image line numbers.  The function body is somewhat longish because
+ * it avoids unnecessary writes.
+ */
+
+static struct blame_entry *blame_merge(struct blame_entry *list1,
+                                      struct blame_entry *list2)
+{
+       struct blame_entry *p1 = list1, *p2 = list2,
+               **tail = &list1;
+
+       if (!p1)
+               return p2;
+       if (!p2)
+               return p1;
+
+       if (p1->s_lno <= p2->s_lno) {
+               do {
+                       tail = &p1->next;
+                       if ((p1 = *tail) == NULL) {
+                               *tail = p2;
+                               return list1;
+                       }
+               } while (p1->s_lno <= p2->s_lno);
+       }
+       for (;;) {
+               *tail = p2;
+               do {
+                       tail = &p2->next;
+                       if ((p2 = *tail) == NULL)  {
+                               *tail = p1;
+                               return list1;
+                       }
+               } while (p1->s_lno > p2->s_lno);
+               *tail = p1;
+               do {
+                       tail = &p1->next;
+                       if ((p1 = *tail) == NULL) {
+                               *tail = p2;
+                               return list1;
+                       }
+               } while (p1->s_lno <= p2->s_lno);
+       }
+}
+
+static void *get_next_blame(const void *p)
+{
+       return ((struct blame_entry *)p)->next;
+}
+
+static void set_next_blame(void *p1, void *p2)
+{
+       ((struct blame_entry *)p1)->next = p2;
+}
+
+/*
+ * Final image line numbers are all different, so we don't need a
+ * three-way comparison here.
+ */
+
+static int compare_blame_final(const void *p1, const void *p2)
+{
+       return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
+               ? 1 : -1;
+}
+
+static int compare_blame_suspect(const void *p1, const void *p2)
+{
+       const struct blame_entry *s1 = p1, *s2 = p2;
+       /*
+        * to allow for collating suspects, we sort according to the
+        * respective pointer value as the primary sorting criterion.
+        * The actual relation is pretty unimportant as long as it
+        * establishes a total order.  Comparing as integers gives us
+        * that.
+        */
+       if (s1->suspect != s2->suspect)
+               return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
+       if (s1->s_lno == s2->s_lno)
+               return 0;
+       return s1->s_lno > s2->s_lno ? 1 : -1;
+}
+
+static struct blame_entry *blame_sort(struct blame_entry *head,
+                                     int (*compare_fn)(const void *, const void *))
+{
+       return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn);
+}
+
+static int compare_commits_by_reverse_commit_date(const void *a,
+                                                 const void *b,
+                                                 void *c)
+{
+       return -compare_commits_by_commit_date(a, b, c);
+}
+
+/*
  * The current state of the blame assignment.
  */
 struct scoreboard {
        /* the final commit (i.e. where we started digging from) */
        struct commit *final;
+       /* Priority queue for commits with unassigned blame records */
+       struct prio_queue commits;
        struct rev_info *revs;
        const char *path;
 
@@ -268,7 +419,6 @@ static void coalesce(struct scoreboard *sb)
 
        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
                if (ent->suspect == next->suspect &&
-                   ent->guilty == next->guilty &&
                    ent->s_lno + ent->num_lines == next->s_lno) {
                        ent->num_lines += next->num_lines;
                        ent->next = next->next;
@@ -284,6 +434,30 @@ static void coalesce(struct scoreboard *sb)
 }
 
 /*
+ * Merge the given sorted list of blames into a preexisting origin.
+ * If there were no previous blames to that commit, it is entered into
+ * the commit priority queue of the score board.
+ */
+
+static void queue_blames(struct scoreboard *sb, struct origin *porigin,
+                        struct blame_entry *sorted)
+{
+       if (porigin->suspects)
+               porigin->suspects = blame_merge(porigin->suspects, sorted);
+       else {
+               struct origin *o;
+               for (o = porigin->commit->util; o; o = o->next) {
+                       if (o->suspects) {
+                               porigin->suspects = sorted;
+                               return;
+                       }
+               }
+               porigin->suspects = sorted;
+               prio_queue_put(&sb->commits, porigin->commit);
+       }
+}
+
+/*
  * Given a commit and a path in it, create a new origin structure.
  * The callers that add blame to the scoreboard should use
  * get_origin() to obtain shared, refcounted copy instead of calling
@@ -292,26 +466,34 @@ static void coalesce(struct scoreboard *sb)
 static struct origin *make_origin(struct commit *commit, const char *path)
 {
        struct origin *o;
-       o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
+       FLEX_ALLOC_STR(o, path, path);
        o->commit = commit;
        o->refcnt = 1;
-       strcpy(o->path, path);
+       o->next = commit->util;
+       commit->util = o;
        return o;
 }
 
 /*
  * 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)
 {
-       struct blame_entry *e;
+       struct origin *o, *l;
 
-       for (e = sb->ent; e; e = e->next) {
-               if (e->suspect->commit == commit &&
-                   !strcmp(e->suspect->path, path))
-                       return origin_incref(e->suspect);
+       for (o = commit->util, l = NULL; o; l = o, o = o->next) {
+               if (!strcmp(o->path, path)) {
+                       /* bump to front */
+                       if (l) {
+                               l->next = o->next;
+                               o->next = commit->util;
+                               commit->util = o;
+                       }
+                       return origin_incref(o);
+               }
        }
        return make_origin(commit, path);
 }
@@ -327,17 +509,17 @@ static struct origin *get_origin(struct scoreboard *sb,
  */
 static int fill_blob_sha1_and_mode(struct 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.sha1,
+       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;
 }
@@ -350,41 +532,19 @@ static struct origin *find_origin(struct scoreboard *sb,
                                  struct commit *parent,
                                  struct origin *origin)
 {
-       struct origin *porigin = NULL;
+       struct origin *porigin;
        struct diff_options diff_opts;
        const char *paths[2];
 
-       if (parent->util) {
-               /*
-                * Each commit object can cache one origin in that
-                * commit.  This is a freestanding copy of origin and
-                * not refcounted.
-                */
-               struct origin *cached = parent->util;
-               if (!strcmp(cached->path, origin->path)) {
+       /* First check any existing origins */
+       for (porigin = parent->util; porigin; porigin = porigin->next)
+               if (!strcmp(porigin->path, origin->path)) {
                        /*
                         * The same path between origin and its parent
                         * without renaming -- the most common case.
                         */
-                       porigin = get_origin(sb, parent, cached->path);
-
-                       /*
-                        * If the origin was newly created (i.e. get_origin
-                        * would call make_origin if none is found in the
-                        * scoreboard), it does not know the blob_sha1/mode,
-                        * so copy it.  Otherwise porigin was in the
-                        * scoreboard and already knows blob_sha1/mode.
-                        */
-                       if (porigin->refcnt == 1) {
-                               hashcpy(porigin->blob_sha1, cached->blob_sha1);
-                               porigin->mode = cached->mode;
-                       }
-                       return porigin;
+                       return origin_incref (porigin);
                }
-               /* otherwise it was not very useful; free it */
-               free(parent->util);
-               parent->util = NULL;
-       }
 
        /* See if the origin->path is different between parent
         * and origin first.  Most of the time they are the
@@ -402,18 +562,18 @@ static struct origin *find_origin(struct scoreboard *sb,
                       PATHSPEC_LITERAL_PATH, "", paths);
        diff_setup_done(&diff_opts);
 
-       if (is_null_sha1(origin->commit->object.sha1))
-               do_diff_cache(parent->tree->object.sha1, &diff_opts);
+       if (is_null_oid(&origin->commit->object.oid))
+               do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
-               diff_tree_sha1(parent->tree->object.sha1,
-                              origin->commit->tree->object.sha1,
+               diff_tree_sha1(parent->tree->object.oid.hash,
+                              origin->commit->tree->object.oid.hash,
                               "", &diff_opts);
        diffcore_std(&diff_opts);
 
        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);
+               oidcpy(&porigin->blob_oid, &origin->blob_oid);
                porigin->mode = origin->mode;
        } else {
                /*
@@ -439,7 +599,7 @@ static struct origin *find_origin(struct scoreboard *sb,
                            p->status);
                case 'M':
                        porigin = get_origin(sb, parent, origin->path);
-                       hashcpy(porigin->blob_sha1, p->one->sha1);
+                       oidcpy(&porigin->blob_oid, &p->one->oid);
                        porigin->mode = p->one->mode;
                        break;
                case 'A':
@@ -449,20 +609,7 @@ static struct origin *find_origin(struct scoreboard *sb,
                }
        }
        diff_flush(&diff_opts);
-       free_pathspec(&diff_opts.pathspec);
-       if (porigin) {
-               /*
-                * Create a freestanding copy that is not part of
-                * the refcounted origin found in the scoreboard, and
-                * cache it in the commit.
-                */
-               struct origin *cached;
-
-               cached = make_origin(porigin->commit, porigin->path);
-               hashcpy(cached->blob_sha1, porigin->blob_sha1);
-               cached->mode = porigin->mode;
-               parent->util = cached;
-       }
+       clear_pathspec(&diff_opts.pathspec);
        return porigin;
 }
 
@@ -485,11 +632,11 @@ static struct origin *find_rename(struct scoreboard *sb,
        diff_opts.single_follow = origin->path;
        diff_setup_done(&diff_opts);
 
-       if (is_null_sha1(origin->commit->object.sha1))
-               do_diff_cache(parent->tree->object.sha1, &diff_opts);
+       if (is_null_oid(&origin->commit->object.oid))
+               do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
-               diff_tree_sha1(parent->tree->object.sha1,
-                              origin->commit->tree->object.sha1,
+               diff_tree_sha1(parent->tree->object.oid.hash,
+                              origin->commit->tree->object.oid.hash,
                               "", &diff_opts);
        diffcore_std(&diff_opts);
 
@@ -498,57 +645,45 @@ static struct origin *find_rename(struct scoreboard *sb,
                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);
+                       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;
 }
 
 /*
- * Link in a new blame entry to the scoreboard.  Entries that cover the
- * same line range have been removed from the scoreboard previously.
+ * Append a new blame entry to a given output queue.
  */
-static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
+static void add_blame_entry(struct blame_entry ***queue,
+                           const struct blame_entry *src)
 {
-       struct blame_entry *ent, *prev = NULL;
-
+       struct blame_entry *e = xmalloc(sizeof(*e));
+       memcpy(e, src, sizeof(*e));
        origin_incref(e->suspect);
 
-       for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
-               prev = ent;
-
-       /* prev, if not NULL, is the last one that is below e */
-
-       if (prev) {
-               e->next = prev->next;
-               prev->next = e;
-       }
-       else {
-               e->next = sb->ent;
-               sb->ent = e;
-       }
+       e->next = **queue;
+       **queue = e;
+       *queue = &e->next;
 }
 
 /*
  * src typically is on-stack; we want to copy the information in it to
- * a malloced blame_entry that is already on the linked list of the
- * scoreboard.  The origin of dst loses a refcnt while the origin of src
- * gains one.
+ * a malloced blame_entry that gets added to the given queue.  The
+ * origin of dst loses a refcnt.
  */
-static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
+static void dup_entry(struct blame_entry ***queue,
+                     struct blame_entry *dst, struct blame_entry *src)
 {
-       struct blame_entry *n;
-
-       n = dst->next;
        origin_incref(src->suspect);
        origin_decref(dst->suspect);
        memcpy(dst, src, sizeof(*src));
-       dst->next = n;
-       dst->score = 0;
+       dst->next = **queue;
+       **queue = dst;
+       *queue = &dst->next;
 }
 
 static const char *nth_line(struct scoreboard *sb, long lno)
@@ -620,72 +755,39 @@ static void split_overlap(struct blame_entry *split,
 
 /*
  * split_overlap() divided an existing blame e into up to three parts
- * in split.  Adjust the linked list of blames in the scoreboard to
+ * in split.  Any assigned blame is moved to queue to
  * reflect the split.
  */
-static void split_blame(struct scoreboard *sb,
+static void split_blame(struct blame_entry ***blamed,
+                       struct blame_entry ***unblamed,
                        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(e, &split[0]);
+               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(sb, 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(sb, new_entry);
+               add_blame_entry(blamed, &split[1]);
        }
        else if (!split[0].suspect && !split[2].suspect)
                /*
                 * The parent covers the entire area; reuse storage for
                 * e and replace it with the parent.
                 */
-               dup_entry(e, &split[1]);
+               dup_entry(blamed, e, &split[1]);
        else if (split[0].suspect) {
                /* me and then parent */
-               dup_entry(e, &split[0]);
-
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
-               add_blame_entry(sb, new_entry);
+               dup_entry(unblamed, e, &split[0]);
+               add_blame_entry(blamed, &split[1]);
        }
        else {
                /* parent and then me */
-               dup_entry(e, &split[1]);
-
-               new_entry = xmalloc(sizeof(*new_entry));
-               memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
-               add_blame_entry(sb, new_entry);
-       }
-
-       if (DEBUG) { /* sanity */
-               struct blame_entry *ent;
-               int lno = sb->ent->lno, corrupt = 0;
-
-               for (ent = sb->ent; ent; ent = ent->next) {
-                       if (lno != ent->lno)
-                               corrupt = 1;
-                       if (ent->s_lno < 0)
-                               corrupt = 1;
-                       lno += ent->num_lines;
-               }
-               if (corrupt) {
-                       lno = sb->ent->lno;
-                       for (ent = sb->ent; ent; ent = ent->next) {
-                               printf("L %8d l %8d n %8d\n",
-                                      lno, ent->lno, ent->num_lines);
-                               lno = ent->lno + ent->num_lines;
-                       }
-                       die("oops");
-               }
+               dup_entry(blamed, e, &split[1]);
+               add_blame_entry(unblamed, &split[2]);
        }
 }
 
@@ -702,74 +804,146 @@ static void decref_split(struct blame_entry *split)
 }
 
 /*
- * Helper for blame_chunk().  blame_entry e is known to overlap with
- * the patch hunk; split it and pass blame to the parent.
+ * reverse_blame reverses the list given in head, appending tail.
+ * That allows us to build lists in reverse order, then reverse them
+ * afterwards.  This can be faster than building the list in proper
+ * order right away.  The reason is that building in proper order
+ * requires writing a link in the _previous_ element, while building
+ * in reverse order just requires placing the list head into the
+ * _current_ element.
  */
-static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
-                         int tlno, int plno, int same,
-                         struct origin *parent)
-{
-       struct blame_entry split[3];
-
-       split_overlap(split, e, tlno, plno, same, parent);
-       if (split[1].suspect)
-               split_blame(sb, split, e);
-       decref_split(split);
-}
 
-/*
- * Find the line number of the last line the target is suspected for.
- */
-static int find_last_in_target(struct scoreboard *sb, struct origin *target)
+static struct blame_entry *reverse_blame(struct blame_entry *head,
+                                        struct blame_entry *tail)
 {
-       struct blame_entry *e;
-       int last_in_target = -1;
-
-       for (e = sb->ent; e; e = e->next) {
-               if (e->guilty || e->suspect != target)
-                       continue;
-               if (last_in_target < e->s_lno + e->num_lines)
-                       last_in_target = e->s_lno + e->num_lines;
+       while (head) {
+               struct blame_entry *next = head->next;
+               head->next = tail;
+               tail = head;
+               head = next;
        }
-       return last_in_target;
+       return tail;
 }
 
 /*
  * Process one hunk from the patch between the current suspect for
- * blame_entry e and its parent.  Find and split the overlap, and
- * pass blame to the overlapping part to the parent.
+ * blame_entry e and its parent.  This first blames any unfinished
+ * entries before the chunk (which is where target and parent start
+ * differing) on the parent, and then splits blame entries at the
+ * start and at the end of the difference region.  Since use of -M and
+ * -C options may lead to overlapping/duplicate source line number
+ * ranges, all we can rely on from sorting/merging is the order of the
+ * first suspect line number.
  */
-static void blame_chunk(struct scoreboard *sb,
-                       int tlno, int plno, int same,
-                       struct origin *target, struct origin *parent)
+static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
+                       int tlno, int offset, int same,
+                       struct origin *parent)
 {
-       struct blame_entry *e;
+       struct blame_entry *e = **srcq;
+       struct blame_entry *samep = NULL, *diffp = NULL;
 
-       for (e = sb->ent; e; e = e->next) {
-               if (e->guilty || e->suspect != target)
-                       continue;
-               if (same <= e->s_lno)
-                       continue;
-               if (tlno < e->s_lno + e->num_lines)
-                       blame_overlap(sb, e, tlno, plno, same, parent);
+       while (e && e->s_lno < tlno) {
+               struct blame_entry *next = e->next;
+               /*
+                * current record starts before differing portion.  If
+                * it reaches into it, we need to split it up and
+                * examine the second part separately.
+                */
+               if (e->s_lno + e->num_lines > tlno) {
+                       /* Move second half to a new record */
+                       int len = tlno - e->s_lno;
+                       struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
+                       n->suspect = e->suspect;
+                       n->lno = e->lno + len;
+                       n->s_lno = e->s_lno + len;
+                       n->num_lines = e->num_lines - len;
+                       e->num_lines = len;
+                       e->score = 0;
+                       /* Push new record to diffp */
+                       n->next = diffp;
+                       diffp = n;
+               } else
+                       origin_decref(e->suspect);
+               /* Pass blame for everything before the differing
+                * chunk to the parent */
+               e->suspect = origin_incref(parent);
+               e->s_lno += offset;
+               e->next = samep;
+               samep = e;
+               e = next;
        }
+       /*
+        * As we don't know how much of a common stretch after this
+        * diff will occur, the currently blamed parts are all that we
+        * can assign to the parent for now.
+        */
+
+       if (samep) {
+               **dstq = reverse_blame(samep, **dstq);
+               *dstq = &samep->next;
+       }
+       /*
+        * Prepend the split off portions: everything after e starts
+        * after the blameable portion.
+        */
+       e = reverse_blame(diffp, e);
+
+       /*
+        * Now retain records on the target while parts are different
+        * from the parent.
+        */
+       samep = NULL;
+       diffp = NULL;
+       while (e && e->s_lno < same) {
+               struct blame_entry *next = e->next;
+
+               /*
+                * If current record extends into sameness, need to split.
+                */
+               if (e->s_lno + e->num_lines > same) {
+                       /*
+                        * Move second half to a new record to be
+                        * processed by later chunks
+                        */
+                       int len = same - e->s_lno;
+                       struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
+                       n->suspect = origin_incref(e->suspect);
+                       n->lno = e->lno + len;
+                       n->s_lno = e->s_lno + len;
+                       n->num_lines = e->num_lines - len;
+                       e->num_lines = len;
+                       e->score = 0;
+                       /* Push new record to samep */
+                       n->next = samep;
+                       samep = n;
+               }
+               e->next = diffp;
+               diffp = e;
+               e = next;
+       }
+       **srcq = reverse_blame(diffp, reverse_blame(samep, e));
+       /* Move across elements that are in the unblamable portion */
+       if (diffp)
+               *srcq = &diffp->next;
 }
 
 struct blame_chunk_cb_data {
-       struct scoreboard *sb;
-       struct origin *target;
        struct origin *parent;
-       long plno;
-       long tlno;
+       long offset;
+       struct blame_entry **dstq;
+       struct blame_entry **srcq;
 };
 
+/* diff chunks are from parent to target */
 static int blame_chunk_cb(long start_a, long count_a,
                          long start_b, long count_b, void *data)
 {
        struct blame_chunk_cb_data *d = data;
-       blame_chunk(d->sb, d->tlno, d->plno, start_b, d->target, d->parent);
-       d->plno = start_a + count_a;
-       d->tlno = start_b + count_b;
+       if (start_a - start_b != d->offset)
+               die("internal error in blame::blame_chunk_cb");
+       blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
+                   start_b + count_b, d->parent);
+       d->offset = start_a + count_a - (start_b + count_b);
        return 0;
 }
 
@@ -778,29 +952,35 @@ 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 int pass_blame_to_parent(struct scoreboard *sb,
-                               struct origin *target,
-                               struct origin *parent)
+static void pass_blame_to_parent(struct scoreboard *sb,
+                                struct origin *target,
+                                struct origin *parent)
 {
-       int last_in_target;
        mmfile_t file_p, file_o;
        struct blame_chunk_cb_data d;
+       struct blame_entry *newdest = NULL;
 
-       memset(&d, 0, sizeof(d));
-       d.sb = sb; d.target = target; d.parent = parent;
-       last_in_target = find_last_in_target(sb, target);
-       if (last_in_target < 0)
-               return 1; /* nothing remains for this target */
+       if (!target->suspects)
+               return; /* nothing remains for this target */
+
+       d.parent = parent;
+       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++;
 
-       diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d);
-       /* The rest (i.e. anything after tlno) are the same as the parent */
-       blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent);
+       if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d))
+               die("unable to generate diff (%s -> %s)",
+                   oid_to_hex(&parent->commit->object.oid),
+                   oid_to_hex(&target->commit->object.oid));
+       /* The rest are the same as the parent */
+       blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
+       *d.dstq = NULL;
+       queue_blames(sb, parent, newdest);
 
-       return 0;
+       return;
 }
 
 /*
@@ -940,48 +1120,87 @@ static void find_copy_in_blob(struct scoreboard *sb,
         * file_p partially may match that image.
         */
        memset(split, 0, sizeof(struct blame_entry [3]));
-       diff_hunks(file_p, &file_o, 1, handle_split_cb, &d);
+       if (diff_hunks(file_p, &file_o, handle_split_cb, &d))
+               die("unable to generate diff (%s)",
+                   oid_to_hex(&parent->commit->object.oid));
        /* remainder, if any, all match the preimage */
        handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
 }
 
+/* Move all blame entries from list *source that have a score smaller
+ * than score_min to the front of list *small.
+ * Returns a pointer to the link pointing to the old head of the small list.
+ */
+
+static struct blame_entry **filter_small(struct scoreboard *sb,
+                                        struct blame_entry **small,
+                                        struct blame_entry **source,
+                                        unsigned score_min)
+{
+       struct blame_entry *p = *source;
+       struct blame_entry *oldsmall = *small;
+       while (p) {
+               if (ent_score(sb, p) <= score_min) {
+                       *small = p;
+                       small = &p->next;
+                       p = *small;
+               } else {
+                       *source = p;
+                       source = &p->next;
+                       p = *source;
+               }
+       }
+       *small = oldsmall;
+       *source = NULL;
+       return small;
+}
+
 /*
  * See if lines currently target is suspected for can be attributed to
  * parent.
  */
-static int find_move_in_parent(struct scoreboard *sb,
-                              struct origin *target,
-                              struct origin *parent)
+static void find_move_in_parent(struct scoreboard *sb,
+                               struct blame_entry ***blamed,
+                               struct blame_entry **toosmall,
+                               struct origin *target,
+                               struct origin *parent)
 {
-       int last_in_target, made_progress;
        struct blame_entry *e, split[3];
+       struct blame_entry *unblamed = target->suspects;
+       struct blame_entry *leftover = NULL;
        mmfile_t file_p;
 
-       last_in_target = find_last_in_target(sb, target);
-       if (last_in_target < 0)
-               return 1; /* nothing remains for this target */
+       if (!unblamed)
+               return; /* nothing remains for this target */
 
        fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
        if (!file_p.ptr)
-               return 0;
+               return;
 
-       made_progress = 1;
-       while (made_progress) {
-               made_progress = 0;
-               for (e = sb->ent; e; e = e->next) {
-                       if (e->guilty || e->suspect != target ||
-                           ent_score(sb, e) < blame_move_score)
-                               continue;
+       /* At each iteration, unblamed has a NULL-terminated list of
+        * entries that have not yet been tested for blame.  leftover
+        * contains the reversed list of entries that have been tested
+        * without being assignable to the parent.
+        */
+       do {
+               struct blame_entry **unblamedtail = &unblamed;
+               struct blame_entry *next;
+               for (e = unblamed; e; e = next) {
+                       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])) {
-                               split_blame(sb, split, e);
-                               made_progress = 1;
+                               split_blame(blamed, &unblamedtail, split, e);
+                       } else {
+                               e->next = leftover;
+                               leftover = e;
                        }
                        decref_split(split);
                }
-       }
-       return 0;
+               *unblamedtail = NULL;
+               toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
+       } while (unblamed);
+       target->suspects = reverse_blame(leftover, NULL);
 }
 
 struct blame_list {
@@ -993,62 +1212,46 @@ struct blame_list {
  * Count the number of entries the target is suspected for,
  * and prepare a list of entry and the best split.
  */
-static struct blame_list *setup_blame_list(struct scoreboard *sb,
-                                          struct origin *target,
-                                          int min_score,
+static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
                                           int *num_ents_p)
 {
        struct blame_entry *e;
        int num_ents, i;
        struct blame_list *blame_list = NULL;
 
-       for (e = sb->ent, num_ents = 0; e; e = e->next)
-               if (!e->scanned && !e->guilty &&
-                   e->suspect == target &&
-                   min_score < ent_score(sb, e))
-                       num_ents++;
+       for (e = unblamed, num_ents = 0; e; e = e->next)
+               num_ents++;
        if (num_ents) {
                blame_list = xcalloc(num_ents, sizeof(struct blame_list));
-               for (e = sb->ent, i = 0; e; e = e->next)
-                       if (!e->scanned && !e->guilty &&
-                           e->suspect == target &&
-                           min_score < ent_score(sb, e))
-                               blame_list[i++].ent = e;
+               for (e = unblamed, i = 0; e; e = e->next)
+                       blame_list[i++].ent = e;
        }
        *num_ents_p = num_ents;
        return blame_list;
 }
 
 /*
- * Reset the scanned status on all entries.
- */
-static void reset_scanned_flag(struct scoreboard *sb)
-{
-       struct blame_entry *e;
-       for (e = sb->ent; e; e = e->next)
-               e->scanned = 0;
-}
-
-/*
  * For lines target is suspected for, see if we can find code movement
  * across file boundary from the parent commit.  porigin is the path
  * in the parent we already tried.
  */
-static int find_copy_in_parent(struct scoreboard *sb,
-                              struct origin *target,
-                              struct commit *parent,
-                              struct origin *porigin,
-                              int opt)
+static void find_copy_in_parent(struct scoreboard *sb,
+                               struct blame_entry ***blamed,
+                               struct blame_entry **toosmall,
+                               struct origin *target,
+                               struct commit *parent,
+                               struct origin *porigin,
+                               int opt)
 {
        struct diff_options diff_opts;
        int i, j;
-       int retval;
        struct blame_list *blame_list;
        int num_ents;
+       struct blame_entry *unblamed = target->suspects;
+       struct blame_entry *leftover = NULL;
 
-       blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents);
-       if (!blame_list)
-               return 1; /* nothing remains for this target */
+       if (!unblamed)
+               return; /* nothing remains for this target */
 
        diff_setup(&diff_opts);
        DIFF_OPT_SET(&diff_opts, RECURSIVE);
@@ -1068,19 +1271,19 @@ static int find_copy_in_parent(struct scoreboard *sb,
                && (!porigin || strcmp(target->path, porigin->path))))
                DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
 
-       if (is_null_sha1(target->commit->object.sha1))
-               do_diff_cache(parent->tree->object.sha1, &diff_opts);
+       if (is_null_oid(&target->commit->object.oid))
+               do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
        else
-               diff_tree_sha1(parent->tree->object.sha1,
-                              target->commit->tree->object.sha1,
+               diff_tree_sha1(parent->tree->object.oid.hash,
+                              target->commit->tree->object.oid.hash,
                               "", &diff_opts);
 
        if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
                diffcore_std(&diff_opts);
 
-       retval = 0;
-       while (1) {
-               int made_progress = 0;
+       do {
+               struct blame_entry **unblamedtail = &unblamed;
+               blame_list = setup_blame_list(unblamed, &num_ents);
 
                for (i = 0; i < diff_queued_diff.nr; i++) {
                        struct diff_filepair *p = diff_queued_diff.queue[i];
@@ -1097,7 +1300,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
                                continue;
 
                        norigin = get_origin(sb, parent, p->one->path);
-                       hashcpy(norigin->blob_sha1, p->one->sha1);
+                       oidcpy(&norigin->blob_oid, &p->one->oid);
                        norigin->mode = p->one->mode;
                        fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
                        if (!file_p.ptr)
@@ -1117,27 +1320,21 @@ static int find_copy_in_parent(struct scoreboard *sb,
                        struct blame_entry *split = blame_list[j].split;
                        if (split[1].suspect &&
                            blame_copy_score < ent_score(sb, &split[1])) {
-                               split_blame(sb, split, blame_list[j].ent);
-                               made_progress = 1;
+                               split_blame(blamed, &unblamedtail, split,
+                                           blame_list[j].ent);
+                       } else {
+                               blame_list[j].ent->next = leftover;
+                               leftover = blame_list[j].ent;
                        }
-                       else
-                               blame_list[j].ent->scanned = 1;
                        decref_split(split);
                }
                free(blame_list);
-
-               if (!made_progress)
-                       break;
-               blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents);
-               if (!blame_list) {
-                       retval = 1;
-                       break;
-               }
-       }
-       reset_scanned_flag(sb);
+               *unblamedtail = NULL;
+               toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
+       } while (unblamed);
+       target->suspects = reverse_blame(leftover, NULL);
        diff_flush(&diff_opts);
-       free_pathspec(&diff_opts.pathspec);
-       return retval;
+       clear_pathspec(&diff_opts.pathspec);
 }
 
 /*
@@ -1147,20 +1344,21 @@ static int find_copy_in_parent(struct scoreboard *sb,
 static void pass_whole_blame(struct scoreboard *sb,
                             struct origin *origin, struct origin *porigin)
 {
-       struct blame_entry *e;
+       struct blame_entry *e, *suspects;
 
        if (!porigin->file.ptr && origin->file.ptr) {
                /* Steal its file */
                porigin->file = origin->file;
                origin->file.ptr = NULL;
        }
-       for (e = sb->ent; e; e = e->next) {
-               if (e->suspect != origin)
-                       continue;
+       suspects = origin->suspects;
+       origin->suspects = NULL;
+       for (e = suspects; e; e = e->next) {
                origin_incref(porigin);
                origin_decref(e->suspect);
                e->suspect = porigin;
        }
+       queue_blames(sb, porigin, suspects);
 }
 
 /*
@@ -1170,18 +1368,43 @@ static void pass_whole_blame(struct scoreboard *sb,
  */
 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit)
 {
-       if (!reverse)
+       if (!reverse) {
+               if (revs->first_parent_only &&
+                   commit->parents &&
+                   commit->parents->next) {
+                       free_commit_list(commit->parents->next);
+                       commit->parents->next = NULL;
+               }
                return commit->parents;
+       }
        return lookup_decoration(&revs->children, &commit->object);
 }
 
 static int num_scapegoats(struct rev_info *revs, struct commit *commit)
 {
-       int cnt;
        struct commit_list *l = first_scapegoat(revs, commit);
-       for (cnt = 0; l; l = l->next)
-               cnt++;
-       return cnt;
+       return commit_list_count(l);
+}
+
+/* Distribute collected unsorted blames to the respected sorted lists
+ * in the various origins.
+ */
+static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
+{
+       blamed = blame_sort(blamed, compare_blame_suspect);
+       while (blamed)
+       {
+               struct origin *porigin = blamed->suspect;
+               struct blame_entry *suspects = NULL;
+               do {
+                       struct blame_entry *next = blamed->next;
+                       blamed->next = suspects;
+                       suspects = blamed;
+                       blamed = next;
+               } while (blamed && blamed->suspect == porigin);
+               suspects = reverse_blame(suspects, NULL);
+               queue_blames(sb, porigin, suspects);
+       }
 }
 
 #define MAXSG 16
@@ -1194,6 +1417,8 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
        struct commit_list *sg;
        struct origin *sg_buf[MAXSG];
        struct origin *porigin, **sg_origin = sg_buf;
+       struct blame_entry *toosmall = NULL;
+       struct blame_entry *blames, **blametail = &blames;
 
        num_sg = num_scapegoats(revs, commit);
        if (!num_sg)
@@ -1225,15 +1450,14 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
                        porigin = find(sb, 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);
                                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;
                                }
@@ -1255,38 +1479,71 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
                        origin_incref(porigin);
                        origin->previous = porigin;
                }
-               if (pass_blame_to_parent(sb, origin, porigin))
+               pass_blame_to_parent(sb, origin, porigin);
+               if (!origin->suspects)
                        goto finish;
        }
 
        /*
         * Optionally find moves in parents' files.
         */
-       if (opt & PICKAXE_BLAME_MOVE)
-               for (i = 0, sg = first_scapegoat(revs, commit);
-                    i < num_sg && sg;
-                    sg = sg->next, i++) {
-                       struct origin *porigin = sg_origin[i];
-                       if (!porigin)
-                               continue;
-                       if (find_move_in_parent(sb, origin, porigin))
-                               goto finish;
+       if (opt & PICKAXE_BLAME_MOVE) {
+               filter_small(sb, &toosmall, &origin->suspects, blame_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];
+                               if (!porigin)
+                                       continue;
+                               find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
+                               if (!origin->suspects)
+                                       break;
+                       }
                }
+       }
 
        /*
         * Optionally find copies from parents' files.
         */
-       if (opt & PICKAXE_BLAME_COPY)
+       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) {
+                       origin->suspects = blame_merge(origin->suspects, toosmall);
+                       toosmall = NULL;
+                       filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
+               }
+               if (!origin->suspects)
+                       goto finish;
+
                for (i = 0, sg = first_scapegoat(revs, commit);
                     i < num_sg && sg;
                     sg = sg->next, i++) {
                        struct origin *porigin = sg_origin[i];
-                       if (find_copy_in_parent(sb, origin, sg->item,
-                                               porigin, opt))
+                       find_copy_in_parent(sb, &blametail, &toosmall,
+                                           origin, sg->item, porigin, opt);
+                       if (!origin->suspects)
                                goto finish;
                }
+       }
 
- finish:
+finish:
+       *blametail = NULL;
+       distribute_blame(sb, blames);
+       /*
+        * prepend toosmall to origin->suspects
+        *
+        * There is no point in sorting: this ends up on a big
+        * unsorted list in the caller anyway.
+        */
+       if (toosmall) {
+               struct blame_entry **tail = &toosmall;
+               while (*tail)
+                       tail = &(*tail)->next;
+               *tail = origin->suspects;
+               origin->suspects = toosmall;
+       }
        for (i = 0; i < num_sg; i++) {
                if (sg_origin[i]) {
                        drop_origin_blob(sg_origin[i]);
@@ -1405,7 +1662,7 @@ static void get_commit_info(struct commit *commit,
 {
        int len;
        const char *subject, *encoding;
-       char *message;
+       const char *message;
 
        commit_info_init(ret);
 
@@ -1416,7 +1673,7 @@ static void get_commit_info(struct commit *commit,
                    &ret->author_time, &ret->author_tz);
 
        if (!detailed) {
-               logmsg_free(message, commit);
+               unuse_commit_buffer(commit, message);
                return;
        }
 
@@ -1428,19 +1685,29 @@ static void get_commit_info(struct commit *commit,
        if (len)
                strbuf_add(&ret->summary, subject, len);
        else
-               strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
+               strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
 
-       logmsg_free(message, commit);
+       unuse_commit_buffer(commit, message);
 }
 
 /*
+ * 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 origin *suspect)
 {
+       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');
+       }
        printf("filename ");
-       write_name_quoted(path, stdout, '\n');
+       write_name_quoted(suspect->path, stdout, '\n');
 }
 
 /*
@@ -1469,11 +1736,6 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
        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 ", sha1_to_hex(prev->commit->object.sha1));
-               write_name_quoted(prev->path, stdout, '\n');
-       }
 
        commit_info_destroy(&ci);
 
@@ -1481,53 +1743,60 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
 }
 
 /*
- * The blame_entry is found to be guilty for the range.  Mark it
- * as such, and show it in incremental output.
+ * The blame_entry is found to be guilty for the range.
+ * Show it in incremental output.
  */
-static void found_guilty_entry(struct blame_entry *ent)
+static void found_guilty_entry(struct blame_entry *ent,
+                          struct progress_info *pi)
 {
-       if (ent->guilty)
-               return;
-       ent->guilty = 1;
        if (incremental) {
                struct origin *suspect = ent->suspect;
 
                printf("%s %d %d %d\n",
-                      sha1_to_hex(suspect->commit->object.sha1),
+                      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;
+       display_progress(pi->progress, pi->blamed_lines);
 }
 
 /*
- * The main loop -- while the scoreboard has lines whose true origin
- * is still unknown, pick one blame_entry, and allow its current
- * suspect to pass blames to its parents.
- */
+ * 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)
 {
        struct rev_info *revs = sb->revs;
+       struct commit *commit = prio_queue_get(&sb->commits);
+       struct progress_info pi = { NULL, 0 };
 
-       while (1) {
+       if (show_progress)
+               pi.progress = start_progress_delay(_("Blaming lines"),
+                                                  sb->num_lines, 50, 1);
+
+       while (commit) {
                struct blame_entry *ent;
-               struct commit *commit;
-               struct origin *suspect = NULL;
+               struct origin *suspect = commit->util;
 
                /* find one suspect to break down */
-               for (ent = sb->ent; !suspect && ent; ent = ent->next)
-                       if (!ent->guilty)
-                               suspect = ent->suspect;
-               if (!suspect)
-                       return; /* all done */
+               while (suspect && !suspect->suspects)
+                       suspect = suspect->next;
+
+               if (!suspect) {
+                       commit = prio_queue_get(&sb->commits);
+                       continue;
+               }
+
+               assert(commit == suspect->commit);
 
                /*
                 * We will use this suspect later in the loop,
                 * so hold onto it in the meantime.
                 */
                origin_incref(suspect);
-               commit = suspect->commit;
                parse_commit(commit);
                if (reverse ||
                    (!(commit->object.flags & UNINTERESTING) &&
@@ -1543,14 +1812,29 @@ static void assign_blame(struct scoreboard *sb, int opt)
                        commit->object.flags |= UNINTERESTING;
 
                /* Take responsibility for the remaining entries */
-               for (ent = sb->ent; ent; ent = ent->next)
-                       if (ent->suspect == suspect)
-                               found_guilty_entry(ent);
+               ent = suspect->suspects;
+               if (ent) {
+                       suspect->guilty = 1;
+                       for (;;) {
+                               struct blame_entry *next = ent->next;
+                               found_guilty_entry(ent, &pi);
+                               if (next) {
+                                       ent = next;
+                                       continue;
+                               }
+                               ent->next = sb->ent;
+                               sb->ent = suspect->suspects;
+                               suspect->suspects = NULL;
+                               break;
+                       }
+               }
                origin_decref(suspect);
 
                if (DEBUG) /* sanity */
                        sanity_check_refcnt(sb);
        }
+
+       stop_progress(&pi.progress);
 }
 
 static const char *format_time(unsigned long time, const char *tz_str,
@@ -1567,7 +1851,7 @@ static const char *format_time(unsigned long time, const char *tz_str,
                size_t time_width;
                int tz;
                tz = atoi(tz_str);
-               time_str = show_date(time, tz, blame_date_mode);
+               time_str = show_date(time, tz, &blame_date_mode);
                strbuf_addstr(&time_buf, time_str);
                /*
                 * Add space paddings to time_buf to display a fixed width
@@ -1596,7 +1880,7 @@ static void emit_porcelain_details(struct 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,
@@ -1606,12 +1890,11 @@ static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
        int cnt;
        const char *cp;
        struct origin *suspect = ent->suspect;
-       char hex[41];
+       char hex[GIT_SHA1_HEXSZ + 1];
 
-       strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
-       printf("%s%c%d %d %d\n",
+       oid_to_hex_r(hex, &suspect->commit->object.oid);
+       printf("%s %d %d %d\n",
               hex,
-              ent->guilty ? ' ' : '*', /* purely for debugging */
               ent->s_lno + 1,
               ent->lno + 1,
               ent->num_lines);
@@ -1645,16 +1928,16 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
        const char *cp;
        struct origin *suspect = ent->suspect;
        struct commit_info ci;
-       char hex[41];
+       char hex[GIT_SHA1_HEXSZ + 1];
        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
 
        get_commit_info(suspect->commit, &ci, 1);
-       strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
+       oid_to_hex_r(hex, &suspect->commit->object.oid);
 
        cp = 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)
@@ -1724,17 +2007,16 @@ static void output(struct scoreboard *sb, int option)
 
        if (option & OUTPUT_PORCELAIN) {
                for (ent = sb->ent; ent; ent = ent->next) {
-                       struct blame_entry *oth;
-                       struct origin *suspect = ent->suspect;
-                       struct commit *commit = suspect->commit;
+                       int count = 0;
+                       struct origin *suspect;
+                       struct commit *commit = ent->suspect->commit;
                        if (commit->object.flags & MORE_THAN_ONE_PATH)
                                continue;
-                       for (oth = ent->next; oth; oth = oth->next) {
-                               if ((oth->suspect->commit != commit) ||
-                                   !strcmp(oth->suspect->path, suspect->path))
-                                       continue;
-                               commit->object.flags |= MORE_THAN_ONE_PATH;
-                               break;
+                       for (suspect = commit->util; suspect; suspect = suspect->next) {
+                               if (suspect->guilty && count++) {
+                                       commit->object.flags |= MORE_THAN_ONE_PATH;
+                                       break;
+                               }
                        }
                }
        }
@@ -1748,6 +2030,12 @@ static void output(struct scoreboard *sb, int option)
        }
 }
 
+static const char *get_next_line(const char *start, const char *end)
+{
+       const char *nl = memchr(start, '\n', end - start);
+       return nl ? nl + 1 : end;
+}
+
 /*
  * To allow quick access to the contents of nth line in the
  * final image, prepare an index in the scoreboard.
@@ -1759,39 +2047,20 @@ static int prepare_lines(struct scoreboard *sb)
        const char *end = buf + len;
        const char *p;
        int *lineno;
-       int num = 0, incomplete = 0;
+       int num = 0;
 
-       for (p = buf;;) {
-               p = memchr(p, '\n', end - p);
-               if (p) {
-                       p++;
-                       num++;
-                       continue;
-               }
-               break;
-       }
+       for (p = buf; p < end; p = get_next_line(p, end))
+               num++;
 
-       if (len && end[-1] != '\n')
-               incomplete++; /* incomplete line at the end */
-
-       sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
+       ALLOC_ARRAY(sb->lineno, num + 1);
        lineno = sb->lineno;
 
-       *lineno++ = 0;
-       for (p = buf;;) {
-               p = memchr(p, '\n', end - p);
-               if (p) {
-                       p++;
-                       *lineno++ = p - buf;
-                       continue;
-               }
-               break;
-       }
+       for (p = buf; p < end; p = get_next_line(p, end))
+               *lineno++ = p - buf;
 
-       if (incomplete)
-               *lineno++ = len;
+       *lineno = len;
 
-       sb->num_lines = num + incomplete;
+       sb->num_lines = num;
        return sb->num_lines;
 }
 
@@ -1819,7 +2088,7 @@ static int read_ancestry(const char *graft_file)
 
 static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
 {
-       const char *uniq = find_unique_abbrev(suspect->commit->object.sha1,
+       const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
                                              auto_abbrev);
        int len = strlen(uniq);
        if (auto_abbrev < len)
@@ -1838,11 +2107,10 @@ static void find_alignment(struct scoreboard *sb, int *option)
        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 commit_info ci;
                int num;
 
                if (compute_auto_abbrev)
@@ -1853,6 +2121,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
                if (longest_file < num)
                        longest_file = num;
                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
+                       struct commit_info ci;
                        suspect->commit->object.flags |= METAINFO_SHOWN;
                        get_commit_info(suspect->commit, &ci, 1);
                        if (*option & OUTPUT_SHOW_EMAIL)
@@ -1861,6 +2130,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
                                num = utf8_strwidth(ci.author.buf);
                        if (longest_author < num)
                                longest_author = num;
+                       commit_info_destroy(&ci);
                }
                num = e->s_lno + e->num_lines;
                if (longest_src_lines < num)
@@ -1870,8 +2140,6 @@ static void find_alignment(struct scoreboard *sb, int *option)
                        longest_dst_lines = num;
                if (largest_score < ent_score(sb, e))
                        largest_score = ent_score(sb, e);
-
-               commit_info_destroy(&ci);
        }
        max_orig_digits = decimal_width(longest_src_lines);
        max_digits = decimal_width(longest_dst_lines);
@@ -1896,7 +2164,7 @@ static void sanity_check_refcnt(struct scoreboard *sb)
                if (ent->suspect->refcnt <= 0) {
                        fprintf(stderr, "%s in %s has negative refcnt %d\n",
                                ent->suspect->path,
-                               sha1_to_hex(ent->suspect->commit->object.sha1),
+                               oid_to_hex(&ent->suspect->commit->object.oid),
                                ent->suspect->refcnt);
                        baa = 1;
                }
@@ -1909,16 +2177,6 @@ static void sanity_check_refcnt(struct scoreboard *sb)
        }
 }
 
-/*
- * Used for the command line parsing; check if the path exists
- * in the working tree.
- */
-static int has_string_in_work_tree(const char *path)
-{
-       struct stat st;
-       return !lstat(path, &st);
-}
-
 static unsigned parse_score(const char *arg)
 {
        char *end;
@@ -1943,13 +2201,23 @@ static int git_blame_config(const char *var, const char *value, void *cb)
                blank_boundary = git_config_bool(var, value);
                return 0;
        }
+       if (!strcmp(var, "blame.showemail")) {
+               int *output_option = cb;
+               if (git_config_bool(var, value))
+                       *output_option |= OUTPUT_SHOW_EMAIL;
+               else
+                       *output_option &= ~OUTPUT_SHOW_EMAIL;
+               return 0;
+       }
        if (!strcmp(var, "blame.date")) {
                if (!value)
                        return config_error_nonbool(var);
-               blame_date_mode = parse_date_format(value);
+               parse_date_format(value, &blame_date_mode);
                return 0;
        }
 
+       if (git_diff_heuristic_config(var, value, cb) < 0)
+               return -1;
        if (userdiff_config(var, value) < 0)
                return -1;
 
@@ -1959,53 +2227,73 @@ static int git_blame_config(const char *var, const char *value, void *cb)
 static void verify_working_tree_path(struct commit *work_tree, const char *path)
 {
        struct commit_list *parents;
+       int pos;
 
        for (parents = work_tree->parents; parents; parents = parents->next) {
-               const unsigned char *commit_sha1 = parents->item->object.sha1;
-               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;
        }
-       die("no such path '%s' in HEAD", path);
+
+       pos = cache_name_pos(path, strlen(path));
+       if (pos >= 0)
+               ; /* path is in the index */
+       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;
 }
 
 static void append_merge_parents(struct commit_list **tail)
 {
        int merge_head;
-       const char *merge_head_file = git_path("MERGE_HEAD");
        struct strbuf line = STRBUF_INIT;
 
-       merge_head = open(merge_head_file, O_RDONLY);
+       merge_head = open(git_path_merge_head(), O_RDONLY);
        if (merge_head < 0) {
                if (errno == ENOENT)
                        return;
-               die("cannot open '%s' for reading", merge_head_file);
+               die("cannot open '%s' for reading", git_path_merge_head());
        }
 
        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
-               unsigned char sha1[20];
-               if (line.len < 40 || get_sha1_hex(line.buf, sha1))
-                       die("unknown line in '%s': %s", merge_head_file, line.buf);
-               tail = append_parent(tail, 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, &oid);
        }
        close(merge_head);
        strbuf_release(&line);
 }
 
 /*
+ * This isn't as simple as passing sb->buf and sb->len, because we
+ * want to transfer ownership of the buffer to the commit (so we
+ * must use detach).
+ */
+static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
+{
+       size_t len;
+       void *buf = strbuf_detach(sb, &len);
+       set_commit_buffer(c, buf, len);
+}
+
+/*
  * Prepare a dummy commit that represents the work tree (or staged) item.
  * Note that annotating work tree item never works in the reverse.
  */
@@ -2016,7 +2304,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        struct commit *commit;
        struct 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;
@@ -2025,17 +2313,17 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        unsigned mode;
        struct strbuf msg = STRBUF_INIT;
 
+       read_cache();
        time(&now);
-       commit = xcalloc(1, sizeof(*commit));
+       commit = alloc_commit_node();
        commit->object.parsed = 1;
        commit->date = now;
-       commit->object.type = OBJ_COMMIT;
        parent_tail = &commit->parents;
 
-       if (!resolve_ref_unsafe("HEAD", head_sha1, 1, 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);
 
@@ -2045,7 +2333,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
        for (parent = commit->parents; parent; parent = parent->next)
                strbuf_addf(&msg, "parent %s\n",
-                           sha1_to_hex(parent->item->object.sha1));
+                           oid_to_hex(&parent->item->object.oid));
        strbuf_addf(&msg,
                    "author %s\n"
                    "committer %s\n\n"
@@ -2053,7 +2341,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
                    ident, ident, path,
                    (!contents_from ? path :
                     (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
-       commit->buffer = strbuf_detach(&msg, NULL);
+       set_commit_buffer_from_strbuf(commit, &msg);
 
        if (!contents_from || strcmp("-", contents_from)) {
                struct stat st;
@@ -2076,7 +2364,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);
@@ -2095,10 +2383,10 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
                if (strbuf_read(&buf, 0, 0) < 0)
                        die_errno("failed to read from stdin");
        }
+       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);
-       commit->util = origin;
+       pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);
 
        /*
         * Read the current index, replace the path entry with
@@ -2120,52 +2408,86 @@ 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;
        ce->ce_mode = create_ce_mode(mode);
        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 
-       /*
-        * We are not going to write this out, so this does not matter
-        * right now, but someday we might optimize diff-index --cached
-        * with cache-tree information.
-        */
-       cache_tree_invalidate_path(active_cache_tree, path);
+       cache_tree_invalidate_path(&the_index, path);
 
        return commit;
 }
 
-static const char *prepare_final(struct scoreboard *sb)
+static struct commit *find_single_final(struct rev_info *revs,
+                                       const char **name_p)
 {
        int i;
-       const char *final_commit_name = NULL;
-       struct rev_info *revs = sb->revs;
+       struct commit *found = NULL;
+       const char *name = NULL;
 
-       /*
-        * There must be one and only one positive commit in the
-        * revs->pending array.
-        */
        for (i = 0; i < revs->pending.nr; i++) {
                struct object *obj = revs->pending.objects[i].item;
                if (obj->flags & UNINTERESTING)
                        continue;
-               while (obj->type == OBJ_TAG)
-                       obj = deref_tag(obj, NULL, 0);
+               obj = deref_tag(obj, NULL, 0);
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
-               if (sb->final)
+               if (found)
                        die("More than one commit to dig 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;
+                           revs->pending.objects[i].name, name);
+               found = (struct commit *)obj;
+               name = revs->pending.objects[i].name;
        }
-       return final_commit_name;
+       if (name_p)
+               *name_p = name;
+       return found;
+}
+
+static char *prepare_final(struct scoreboard *sb)
+{
+       const char *name;
+       sb->final = find_single_final(sb->revs, &name);
+       return xstrdup_or_null(name);
+}
+
+static const char *dwim_reverse_initial(struct 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 const char *prepare_initial(struct scoreboard *sb)
+static char *prepare_initial(struct scoreboard *sb)
 {
        int i;
        const char *final_commit_name = NULL;
@@ -2179,20 +2501,22 @@ static const char *prepare_initial(struct scoreboard *sb)
                struct object *obj = revs->pending.objects[i].item;
                if (!(obj->flags & UNINTERESTING))
                        continue;
-               while (obj->type == OBJ_TAG)
-                       obj = deref_tag(obj, NULL, 0);
+               obj = deref_tag(obj, NULL, 0);
                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?");
-       return final_commit_name;
+               die("No commit to dig up from?");
+       return xstrdup(final_commit_name);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
@@ -2236,19 +2560,21 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        struct origin *o;
        struct blame_entry *ent = NULL;
        long dashdash_pos, lno;
-       const char *final_commit_name = NULL;
+       char *final_commit_name = NULL;
        enum object_type type;
-
-       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 commit *final_commit = NULL;
+
+       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)")),
                OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
+               OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
@@ -2260,6 +2586,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")),
@@ -2276,7 +2610,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        unsigned int range_i;
        long anchor;
 
-       git_config(git_blame_config, NULL);
+       git_config(git_blame_config, &output_option);
        init_revisions(&revs, NULL);
        revs.date_mode = blame_date_mode;
        DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
@@ -2284,6 +2618,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
        save_commit_buffer = 0;
        dashdash_pos = 0;
+       show_progress = -1;
 
        parse_options_start(&ctx, argc, argv, prefix, options,
                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
@@ -2305,34 +2640,50 @@ 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 (0 < abbrev)
+       if (incremental || (output_option & OUTPUT_PORCELAIN)) {
+               if (show_progress > 0)
+                       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 && 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);
 
        if (cmd_is_annotate) {
                output_option |= OUTPUT_ANNOTATE_COMPAT;
-               blame_date_mode = DATE_ISO8601;
+               blame_date_mode.type = DATE_ISO8601;
        } else {
                blame_date_mode = revs.date_mode;
        }
 
        /* The maximum width used to show the dates */
-       switch (blame_date_mode) {
+       switch (blame_date_mode.type) {
        case DATE_RFC2822:
                blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
                break;
+       case DATE_ISO8601_STRICT:
+               blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
+               break;
        case DATE_ISO8601:
                blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
                break;
        case DATE_RAW:
                blame_date_width = sizeof("1161298804 -0700");
                break;
+       case DATE_UNIX:
+               blame_date_width = sizeof("1161298804");
+               break;
        case DATE_SHORT:
                blame_date_width = sizeof("2006-10-19");
                break;
@@ -2345,10 +2696,12 @@ parse_done:
                   fewer display columns. */
                blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
                break;
-       case DATE_LOCAL:
        case DATE_NORMAL:
                blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
                break;
+       case DATE_STRFTIME:
+               blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
+               break;
        }
        blame_date_width -= 1; /* strip the null */
 
@@ -2400,14 +2753,14 @@ parse_done:
                if (argc < 2)
                        usage_with_options(blame_opt_usage, options);
                path = add_prefix(prefix, argv[argc - 1]);
-               if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */
+               if (argc == 3 && !file_exists(path)) { /* (2b) */
                        path = add_prefix(prefix, argv[1]);
                        argv[1] = argv[2];
                }
                argv[argc - 1] = "--";
 
                setup_work_tree();
-               if (!has_string_in_work_tree(path))
+               if (!file_exists(path))
                        die_errno("cannot stat path '%s'", path);
        }
 
@@ -2416,12 +2769,18 @@ parse_done:
        memset(&sb, 0, sizeof(sb));
 
        sb.revs = &revs;
-       if (!reverse)
+       if (!reverse) {
                final_commit_name = prepare_final(&sb);
+               sb.commits.compare = compare_commits_by_commit_date;
+       }
        else if (contents_from)
-               die("--contents and --children do not blend well.");
-       else
+               die(_("--contents and --reverse do not blend well."));
+       else {
                final_commit_name = prepare_initial(&sb);
+               sb.commits.compare = compare_commits_by_reverse_commit_date;
+               if (revs.first_parent_only)
+                       revs.children.name = NULL;
+       }
 
        if (!sb.final) {
                /*
@@ -2435,7 +2794,13 @@ parse_done:
                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"));
+       }
 
        /*
         * If we have bottom, this will mark the ancestors of the
@@ -2443,39 +2808,55 @@ parse_done:
         * uninteresting.
         */
        if (prepare_revision_walk(&revs))
-               die("revision walk setup failed");
+               die(_("revision walk setup failed"));
+
+       if (reverse && revs.first_parent_only) {
+               struct commit *c = final_commit;
+
+               sb.revs->children.name = "children";
+               while (c->parents &&
+                      oidcmp(&c->object.oid, &sb.final->object.oid)) {
+                       struct commit_list *l = xcalloc(1, sizeof(*l));
+
+                       l->item = c;
+                       if (add_decoration(&sb.revs->children,
+                                          &c->parents->item->object, l))
+                               die("BUG: not unique item in first-parent chain");
+                       c = c->parents->item;
+               }
 
-       if (is_null_sha1(sb.final->object.sha1)) {
-               char *buf;
+               if (oidcmp(&c->object.oid, &sb.final->object.oid))
+                       die(_("--reverse --first-parent together require range along first-parent chain"));
+       }
+
+       if (is_null_oid(&sb.final->object.oid)) {
                o = sb.final->util;
-               buf = xmalloc(o->file.size + 1);
-               memcpy(buf, o->file.ptr, o->file.size + 1);
-               sb.final_buf = buf;
+               sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
                sb.final_buf_size = o->file.size;
        }
        else {
                o = get_origin(&sb, 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++;
        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);
@@ -2486,7 +2867,9 @@ parse_done:
                                    &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)
@@ -2510,24 +2893,32 @@ parse_done:
                ent->next = next;
                origin_incref(o);
        }
+
+       o->suspects = ent;
+       prio_queue_put(&sb.commits, o->commit);
+
        origin_decref(o);
 
        range_set_release(&ranges);
        string_list_clear(&range_list, 0);
 
-       sb.ent = ent;
+       sb.ent = NULL;
        sb.path = path;
 
        read_mailmap(&mailmap, NULL);
 
+       assign_blame(&sb, opt);
+
        if (!incremental)
                setup_pager();
 
-       assign_blame(&sb, opt);
+       free(final_commit_name);
 
        if (incremental)
                return 0;
 
+       sb.ent = blame_sort(sb.ent, compare_blame_final);
+
        coalesce(&sb);
 
        if (!(output_option & OUTPUT_PORCELAIN))