blame: move no_whole_file_rename flag to scoreboard
[gitweb.git] / builtin / blame.c
index 717868e7e38a6197a916bd129fc04d88ac553caa..90c643c7da671235ad51b563d93db7e5fa56b431 100644 (file)
@@ -61,20 +61,11 @@ static struct string_list mailmap = STRING_LIST_INIT_NODUP;
 #define DEBUG 0
 #endif
 
-/* stats */
-static int num_read_blob;
-static int num_get_patch;
-static int num_commits;
-
 #define PICKAXE_BLAME_MOVE             01
 #define PICKAXE_BLAME_COPY             02
 #define PICKAXE_BLAME_COPY_HARDER      04
 #define PICKAXE_BLAME_COPY_HARDEST     010
 
-/*
- * blame for a blame_entry with score lower than these thresholds
- * is not passed to the parent using move/copy logic.
- */
 static unsigned blame_move_score;
 static unsigned blame_copy_score;
 #define BLAME_DEFAULT_MOVE_SCORE       20
@@ -134,7 +125,7 @@ struct progress_info {
 };
 
 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
-                     xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
+                     xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
 {
        xpparam_t xpp = {0};
        xdemitconf_t xecfg = {0};
@@ -151,13 +142,13 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
  * diff machinery
  */
 static void fill_origin_blob(struct diff_options *opt,
-                            struct blame_origin *o, mmfile_t *file)
+                            struct blame_origin *o, mmfile_t *file, int *num_read_blob)
 {
        if (!o->file.ptr) {
                enum object_type type;
                unsigned long file_size;
 
-               num_read_blob++;
+               (*num_read_blob)++;
                if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
                    textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size))
                        ;
@@ -180,19 +171,19 @@ static void fill_origin_blob(struct diff_options *opt,
  * Origin is refcounted and usually we keep the blob contents to be
  * reused.
  */
-static inline struct blame_origin *origin_incref(struct blame_origin *o)
+static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
 {
        if (o)
                o->refcnt++;
        return o;
 }
 
-static void origin_decref(struct blame_origin *o)
+static void blame_origin_decref(struct blame_origin *o)
 {
        if (o && --o->refcnt <= 0) {
                struct blame_origin *p, *l = NULL;
                if (o->previous)
-                       origin_decref(o->previous);
+                       blame_origin_decref(o->previous);
                free(o->file.ptr);
                /* Should be present exactly once in commit chain */
                for (p = o->commit->util; p; l = p, p = p->next) {
@@ -205,7 +196,7 @@ static void origin_decref(struct blame_origin *o)
                                return;
                        }
                }
-               die("internal error in blame::origin_decref");
+               die("internal error in blame_origin_decref");
        }
 }
 
@@ -375,6 +366,27 @@ struct blame_scoreboard {
        /* look-up a line in the final buffer */
        int num_lines;
        int *lineno;
+
+       /* stats */
+       int num_read_blob;
+       int num_get_patch;
+       int num_commits;
+
+       /*
+        * blame for a blame_entry with score lower than these thresholds
+        * is not passed to the parent using move/copy logic.
+        */
+       unsigned move_score;
+       unsigned copy_score;
+
+       /* use this file's contents as the final image */
+       const char *contents_from;
+
+       /* flags */
+       int reverse;
+       int show_root;
+       int xdl_opts;
+       int no_whole_file_rename;
 };
 
 static void sanity_check_refcnt(struct blame_scoreboard *);
@@ -384,7 +396,7 @@ static void sanity_check_refcnt(struct blame_scoreboard *);
  * contiguous lines in the same origin (i.e. <commit, path> pair),
  * merge them together.
  */
-static void coalesce(struct blame_scoreboard *sb)
+static void blame_coalesce(struct blame_scoreboard *sb)
 {
        struct blame_entry *ent, *next;
 
@@ -393,7 +405,7 @@ static void coalesce(struct blame_scoreboard *sb)
                    ent->s_lno + ent->num_lines == next->s_lno) {
                        ent->num_lines += next->num_lines;
                        ent->next = next->next;
-                       origin_decref(next->suspect);
+                       blame_origin_decref(next->suspect);
                        free(next);
                        ent->score = 0;
                        next = ent; /* again */
@@ -461,7 +473,7 @@ static struct blame_origin *get_origin(struct commit *commit, const char *path)
                                o->next = commit->util;
                                commit->util = o;
                        }
-                       return origin_incref(o);
+                       return blame_origin_incref(o);
                }
        }
        return make_origin(commit, path);
@@ -511,7 +523,7 @@ static struct blame_origin *find_origin(struct commit *parent,
                         * The same path between origin and its parent
                         * without renaming -- the most common case.
                         */
-                       return origin_incref (porigin);
+                       return blame_origin_incref (porigin);
                }
 
        /* See if the origin->path is different between parent
@@ -630,7 +642,7 @@ static void add_blame_entry(struct blame_entry ***queue,
 {
        struct blame_entry *e = xmalloc(sizeof(*e));
        memcpy(e, src, sizeof(*e));
-       origin_incref(e->suspect);
+       blame_origin_incref(e->suspect);
 
        e->next = **queue;
        **queue = e;
@@ -645,22 +657,22 @@ static void add_blame_entry(struct blame_entry ***queue,
 static void dup_entry(struct blame_entry ***queue,
                      struct blame_entry *dst, struct blame_entry *src)
 {
-       origin_incref(src->suspect);
-       origin_decref(dst->suspect);
+       blame_origin_incref(src->suspect);
+       blame_origin_decref(dst->suspect);
        memcpy(dst, src, sizeof(*src));
        dst->next = **queue;
        **queue = dst;
        *queue = &dst->next;
 }
 
-static const char *nth_line(struct blame_scoreboard *sb, long lno)
+static const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
 {
        return sb->final_buf + sb->lineno[lno];
 }
 
 static const char *nth_line_cb(void *data, long lno)
 {
-       return nth_line((struct blame_scoreboard *)data, lno);
+       return blame_nth_line((struct blame_scoreboard *)data, lno);
 }
 
 /*
@@ -687,7 +699,7 @@ static void split_overlap(struct blame_entry *split,
 
        if (e->s_lno < tlno) {
                /* there is a pre-chunk part not blamed on parent */
-               split[0].suspect = origin_incref(e->suspect);
+               split[0].suspect = blame_origin_incref(e->suspect);
                split[0].lno = e->lno;
                split[0].s_lno = e->s_lno;
                split[0].num_lines = tlno - e->s_lno;
@@ -701,7 +713,7 @@ static void split_overlap(struct blame_entry *split,
 
        if (same < e->s_lno + e->num_lines) {
                /* there is a post-chunk part not blamed on parent */
-               split[2].suspect = origin_incref(e->suspect);
+               split[2].suspect = blame_origin_incref(e->suspect);
                split[2].lno = e->lno + (same - e->s_lno);
                split[2].s_lno = e->s_lno + (same - e->s_lno);
                split[2].num_lines = e->s_lno + e->num_lines - same;
@@ -717,7 +729,7 @@ static void split_overlap(struct blame_entry *split,
         */
        if (split[1].num_lines < 1)
                return;
-       split[1].suspect = origin_incref(parent);
+       split[1].suspect = blame_origin_incref(parent);
 }
 
 /*
@@ -767,7 +779,7 @@ static void decref_split(struct blame_entry *split)
        int i;
 
        for (i = 0; i < 3; i++)
-               origin_decref(split[i].suspect);
+               blame_origin_decref(split[i].suspect);
 }
 
 /*
@@ -830,10 +842,10 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
                        n->next = diffp;
                        diffp = n;
                } else
-                       origin_decref(e->suspect);
+                       blame_origin_decref(e->suspect);
                /* Pass blame for everything before the differing
                 * chunk to the parent */
-               e->suspect = origin_incref(parent);
+               e->suspect = blame_origin_incref(parent);
                e->s_lno += offset;
                e->next = samep;
                samep = e;
@@ -874,7 +886,7 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
                         */
                        int len = same - e->s_lno;
                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
-                       n->suspect = origin_incref(e->suspect);
+                       n->suspect = blame_origin_incref(e->suspect);
                        n->lno = e->lno + len;
                        n->s_lno = e->s_lno + len;
                        n->num_lines = e->num_lines - len;
@@ -934,11 +946,11 @@ static void pass_blame_to_parent(struct blame_scoreboard *sb,
        d.offset = 0;
        d.dstq = &newdest; d.srcq = &target->suspects;
 
-       fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
-       fill_origin_blob(&sb->revs->diffopt, target, &file_o);
-       num_get_patch++;
+       fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
+       fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
+       sb->num_get_patch++;
 
-       if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d))
+       if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
                die("unable to generate diff (%s -> %s)",
                    oid_to_hex(&parent->commit->object.oid),
                    oid_to_hex(&target->commit->object.oid));
@@ -959,7 +971,7 @@ static void pass_blame_to_parent(struct blame_scoreboard *sb,
  *
  * Compute how trivial the lines in the blame_entry are.
  */
-static unsigned ent_score(struct blame_scoreboard *sb, struct blame_entry *e)
+static unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
 {
        unsigned score;
        const char *cp, *ep;
@@ -968,8 +980,8 @@ static unsigned ent_score(struct blame_scoreboard *sb, struct blame_entry *e)
                return e->score;
 
        score = 1;
-       cp = nth_line(sb, e->lno);
-       ep = nth_line(sb, e->lno + e->num_lines);
+       cp = blame_nth_line(sb, e->lno);
+       ep = blame_nth_line(sb, e->lno + e->num_lines);
        while (cp < ep) {
                unsigned ch = *((unsigned char *)cp);
                if (isalnum(ch))
@@ -995,12 +1007,12 @@ static void copy_split_if_better(struct blame_scoreboard *sb,
        if (!this[1].suspect)
                return;
        if (best_so_far[1].suspect) {
-               if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
+               if (blame_entry_score(sb, &this[1]) < blame_entry_score(sb, &best_so_far[1]))
                        return;
        }
 
        for (i = 0; i < 3; i++)
-               origin_incref(this[i].suspect);
+               blame_origin_incref(this[i].suspect);
        decref_split(best_so_far);
        memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
 }
@@ -1078,16 +1090,16 @@ static void find_copy_in_blob(struct blame_scoreboard *sb,
        /*
         * Prepare mmfile that contains only the lines in ent.
         */
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        file_o.ptr = (char *) cp;
-       file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
+       file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
 
        /*
         * file_o is a part of final image we are annotating.
         * file_p partially may match that image.
         */
        memset(split, 0, sizeof(struct blame_entry [3]));
-       if (diff_hunks(file_p, &file_o, handle_split_cb, &d))
+       if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
                die("unable to generate diff (%s)",
                    oid_to_hex(&parent->commit->object.oid));
        /* remainder, if any, all match the preimage */
@@ -1107,7 +1119,7 @@ static struct blame_entry **filter_small(struct blame_scoreboard *sb,
        struct blame_entry *p = *source;
        struct blame_entry *oldsmall = *small;
        while (p) {
-               if (ent_score(sb, p) <= score_min) {
+               if (blame_entry_score(sb, p) <= score_min) {
                        *small = p;
                        small = &p->next;
                        p = *small;
@@ -1140,7 +1152,7 @@ static void find_move_in_parent(struct blame_scoreboard *sb,
        if (!unblamed)
                return; /* nothing remains for this target */
 
-       fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
+       fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
        if (!file_p.ptr)
                return;
 
@@ -1156,7 +1168,7 @@ static void find_move_in_parent(struct blame_scoreboard *sb,
                        next = e->next;
                        find_copy_in_blob(sb, e, parent, split, &file_p);
                        if (split[1].suspect &&
-                           blame_move_score < ent_score(sb, &split[1])) {
+                           sb->move_score < blame_entry_score(sb, &split[1])) {
                                split_blame(blamed, &unblamedtail, split, e);
                        } else {
                                e->next = leftover;
@@ -1165,7 +1177,7 @@ static void find_move_in_parent(struct blame_scoreboard *sb,
                        decref_split(split);
                }
                *unblamedtail = NULL;
-               toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
+               toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
        } while (unblamed);
        target->suspects = reverse_blame(leftover, NULL);
 }
@@ -1269,7 +1281,7 @@ static void find_copy_in_parent(struct blame_scoreboard *sb,
                        norigin = get_origin(parent, p->one->path);
                        oidcpy(&norigin->blob_oid, &p->one->oid);
                        norigin->mode = p->one->mode;
-                       fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
+                       fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
                        if (!file_p.ptr)
                                continue;
 
@@ -1280,13 +1292,13 @@ static void find_copy_in_parent(struct blame_scoreboard *sb,
                                                     this);
                                decref_split(this);
                        }
-                       origin_decref(norigin);
+                       blame_origin_decref(norigin);
                }
 
                for (j = 0; j < num_ents; j++) {
                        struct blame_entry *split = blame_list[j].split;
                        if (split[1].suspect &&
-                           blame_copy_score < ent_score(sb, &split[1])) {
+                           sb->copy_score < blame_entry_score(sb, &split[1])) {
                                split_blame(blamed, &unblamedtail, split,
                                            blame_list[j].ent);
                        } else {
@@ -1297,7 +1309,7 @@ static void find_copy_in_parent(struct blame_scoreboard *sb,
                }
                free(blame_list);
                *unblamedtail = NULL;
-               toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
+               toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
        } while (unblamed);
        target->suspects = reverse_blame(leftover, NULL);
        diff_flush(&diff_opts);
@@ -1321,8 +1333,8 @@ static void pass_whole_blame(struct blame_scoreboard *sb,
        suspects = origin->suspects;
        origin->suspects = NULL;
        for (e = suspects; e; e = e->next) {
-               origin_incref(porigin);
-               origin_decref(e->suspect);
+               blame_origin_incref(porigin);
+               blame_origin_decref(e->suspect);
                e->suspect = porigin;
        }
        queue_blames(sb, porigin, suspects);
@@ -1333,7 +1345,8 @@ static void pass_whole_blame(struct blame_scoreboard *sb,
  * "parent" (and "porigin"), but what we mean is to find scapegoat to
  * exonerate ourselves.
  */
-static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit)
+static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
+                                          int reverse)
 {
        if (!reverse) {
                if (revs->first_parent_only &&
@@ -1347,9 +1360,9 @@ static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit
        return lookup_decoration(&revs->children, &commit->object);
 }
 
-static int num_scapegoats(struct rev_info *revs, struct commit *commit)
+static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
 {
-       struct commit_list *l = first_scapegoat(revs, commit);
+       struct commit_list *l = first_scapegoat(revs, commit, reverse);
        return commit_list_count(l);
 }
 
@@ -1387,7 +1400,7 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
        struct blame_entry *toosmall = NULL;
        struct blame_entry *blames, **blametail = &blames;
 
-       num_sg = num_scapegoats(revs, commit);
+       num_sg = num_scapegoats(revs, commit, sb->reverse);
        if (!num_sg)
                goto finish;
        else if (num_sg < ARRAY_SIZE(sg_buf))
@@ -1399,11 +1412,11 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
         * The first pass looks for unrenamed path to optimize for
         * common cases, then we look for renames in the second pass.
         */
-       for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
+       for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
                struct blame_origin *(*find)(struct commit *, struct blame_origin *);
                find = pass ? find_rename : find_origin;
 
-               for (i = 0, sg = first_scapegoat(revs, commit);
+               for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
                     i < num_sg && sg;
                     sg = sg->next, i++) {
                        struct commit *p = sg->item;
@@ -1418,7 +1431,7 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
                                continue;
                        if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
                                pass_whole_blame(sb, origin, porigin);
-                               origin_decref(porigin);
+                               blame_origin_decref(porigin);
                                goto finish;
                        }
                        for (j = same = 0; j < i; j++)
@@ -1430,19 +1443,19 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
                        if (!same)
                                sg_origin[i] = porigin;
                        else
-                               origin_decref(porigin);
+                               blame_origin_decref(porigin);
                }
        }
 
-       num_commits++;
-       for (i = 0, sg = first_scapegoat(revs, commit);
+       sb->num_commits++;
+       for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
             i < num_sg && sg;
             sg = sg->next, i++) {
                struct blame_origin *porigin = sg_origin[i];
                if (!porigin)
                        continue;
                if (!origin->previous) {
-                       origin_incref(porigin);
+                       blame_origin_incref(porigin);
                        origin->previous = porigin;
                }
                pass_blame_to_parent(sb, origin, porigin);
@@ -1454,9 +1467,9 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
         * Optionally find moves in parents' files.
         */
        if (opt & PICKAXE_BLAME_MOVE) {
-               filter_small(sb, &toosmall, &origin->suspects, blame_move_score);
+               filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
                if (origin->suspects) {
-                       for (i = 0, sg = first_scapegoat(revs, commit);
+                       for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
                             i < num_sg && sg;
                             sg = sg->next, i++) {
                                struct blame_origin *porigin = sg_origin[i];
@@ -1473,17 +1486,17 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
         * Optionally find copies from parents' files.
         */
        if (opt & PICKAXE_BLAME_COPY) {
-               if (blame_copy_score > blame_move_score)
-                       filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
-               else if (blame_copy_score < blame_move_score) {
+               if (sb->copy_score > sb->move_score)
+                       filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
+               else if (sb->copy_score < sb->move_score) {
                        origin->suspects = blame_merge(origin->suspects, toosmall);
                        toosmall = NULL;
-                       filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
+                       filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
                }
                if (!origin->suspects)
                        goto finish;
 
-               for (i = 0, sg = first_scapegoat(revs, commit);
+               for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
                     i < num_sg && sg;
                     sg = sg->next, i++) {
                        struct blame_origin *porigin = sg_origin[i];
@@ -1513,7 +1526,7 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
        for (i = 0; i < num_sg; i++) {
                if (sg_origin[i]) {
                        drop_origin_blob(sg_origin[i]);
-                       origin_decref(sg_origin[i]);
+                       blame_origin_decref(sg_origin[i]);
                }
        }
        drop_origin_blob(origin);
@@ -1762,9 +1775,9 @@ static void assign_blame(struct blame_scoreboard *sb, int opt)
                 * We will use this suspect later in the loop,
                 * so hold onto it in the meantime.
                 */
-               origin_incref(suspect);
+               blame_origin_incref(suspect);
                parse_commit(commit);
-               if (reverse ||
+               if (sb->reverse ||
                    (!(commit->object.flags & UNINTERESTING) &&
                     !(revs->max_age != -1 && commit->date < revs->max_age)))
                        pass_blame(sb, suspect, opt);
@@ -1774,7 +1787,7 @@ static void assign_blame(struct blame_scoreboard *sb, int opt)
                                mark_parents_uninteresting(commit);
                }
                /* treat root commit as boundary */
-               if (!commit->parents && !show_root)
+               if (!commit->parents && !sb->show_root)
                        commit->object.flags |= UNINTERESTING;
 
                /* Take responsibility for the remaining entries */
@@ -1794,7 +1807,7 @@ static void assign_blame(struct blame_scoreboard *sb, int opt)
                                break;
                        }
                }
-               origin_decref(suspect);
+               blame_origin_decref(suspect);
 
                if (DEBUG) /* sanity */
                        sanity_check_refcnt(sb);
@@ -1866,7 +1879,7 @@ static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
               ent->num_lines);
        emit_porcelain_details(suspect, repeat);
 
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
                if (cnt) {
@@ -1900,7 +1913,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
        get_commit_info(suspect->commit, &ci, 1);
        oid_to_hex_r(hex, &suspect->commit->object.oid);
 
-       cp = nth_line(sb, ent->lno);
+       cp = blame_nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
@@ -2104,8 +2117,8 @@ static void find_alignment(struct blame_scoreboard *sb, int *option)
                num = e->lno + e->num_lines;
                if (longest_dst_lines < num)
                        longest_dst_lines = num;
-               if (largest_score < ent_score(sb, e))
-                       largest_score = ent_score(sb, e);
+               if (largest_score < blame_entry_score(sb, e))
+                       largest_score = blame_entry_score(sb, e);
        }
        max_orig_digits = decimal_width(longest_src_lines);
        max_digits = decimal_width(longest_dst_lines);
@@ -2675,11 +2688,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
                        PICKAXE_BLAME_COPY_HARDER);
 
-       if (!blame_move_score)
-               blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
-       if (!blame_copy_score)
-               blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
-
        /*
         * We have collected options unknown to us in argv[1..unk]
         * which are to be passed to revision machinery if we are
@@ -2733,8 +2741,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        revs.disable_stdin = 1;
        setup_revisions(argc, argv, &revs, NULL);
        memset(&sb, 0, sizeof(sb));
+       sb.move_score = BLAME_DEFAULT_MOVE_SCORE;
+       sb.copy_score = BLAME_DEFAULT_COPY_SCORE;
 
        sb.revs = &revs;
+       sb.contents_from = contents_from;
+       sb.reverse = reverse;
        if (!reverse) {
                final_commit_name = prepare_final(&sb);
                sb.commits.compare = compare_commits_by_commit_date;
@@ -2818,7 +2830,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                            oid_to_hex(&o->blob_oid),
                            path);
        }
-       num_read_blob++;
+       sb.num_read_blob++;
        lno = prepare_lines(&sb);
 
        if (lno && !range_list.nr)
@@ -2857,13 +2869,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                ent->suspect = o;
                ent->s_lno = bottom;
                ent->next = next;
-               origin_incref(o);
+               blame_origin_incref(o);
        }
 
        o->suspects = ent;
        prio_queue_put(&sb.commits, o->commit);
 
-       origin_decref(o);
+       blame_origin_decref(o);
 
        range_set_release(&ranges);
        string_list_clear(&range_list, 0);
@@ -2871,6 +2883,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        sb.ent = NULL;
        sb.path = path;
 
+       if (blame_move_score)
+               sb.move_score = blame_move_score;
+       if (blame_copy_score)
+               sb.copy_score = blame_copy_score;
+
+       sb.show_root = show_root;
+       sb.xdl_opts = xdl_opts;
+       sb.no_whole_file_rename = no_whole_file_rename;
+
        read_mailmap(&mailmap, NULL);
 
        assign_blame(&sb, opt);
@@ -2885,7 +2906,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
        sb.ent = blame_sort(sb.ent, compare_blame_final);
 
-       coalesce(&sb);
+       blame_coalesce(&sb);
 
        if (!(output_option & OUTPUT_PORCELAIN))
                find_alignment(&sb, &output_option);
@@ -2899,9 +2920,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        }
 
        if (show_stats) {
-               printf("num read blob: %d\n", num_read_blob);
-               printf("num get patch: %d\n", num_get_patch);
-               printf("num commits: %d\n", num_commits);
+               printf("num read blob: %d\n", sb.num_read_blob);
+               printf("num get patch: %d\n", sb.num_get_patch);
+               printf("num commits: %d\n", sb.num_commits);
        }
        return 0;
 }