161d15c173fb50276391636d7d867147b6a93465
   1/*
   2 * Blame
   3 *
   4 * Copyright (c) 2006, 2014 by its authors
   5 * See COPYING for licensing conditions
   6 */
   7
   8#include "cache.h"
   9#include "refs.h"
  10#include "builtin.h"
  11#include "commit.h"
  12#include "tag.h"
  13#include "tree-walk.h"
  14#include "diff.h"
  15#include "diffcore.h"
  16#include "revision.h"
  17#include "quote.h"
  18#include "xdiff-interface.h"
  19#include "cache-tree.h"
  20#include "string-list.h"
  21#include "mailmap.h"
  22#include "mergesort.h"
  23#include "parse-options.h"
  24#include "prio-queue.h"
  25#include "utf8.h"
  26#include "userdiff.h"
  27#include "line-range.h"
  28#include "line-log.h"
  29#include "dir.h"
  30#include "progress.h"
  31
  32static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
  33
  34static const char *blame_opt_usage[] = {
  35        blame_usage,
  36        "",
  37        N_("<rev-opts> are documented in git-rev-list(1)"),
  38        NULL
  39};
  40
  41static int longest_file;
  42static int longest_author;
  43static int max_orig_digits;
  44static int max_digits;
  45static int max_score_digits;
  46static int show_root;
  47static int reverse;
  48static int blank_boundary;
  49static int incremental;
  50static int xdl_opts;
  51static int abbrev = -1;
  52static int no_whole_file_rename;
  53static int show_progress;
  54
  55static struct date_mode blame_date_mode = { DATE_ISO8601 };
  56static size_t blame_date_width;
  57
  58static struct string_list mailmap = STRING_LIST_INIT_NODUP;
  59
  60#ifndef DEBUG
  61#define DEBUG 0
  62#endif
  63
  64#define PICKAXE_BLAME_MOVE              01
  65#define PICKAXE_BLAME_COPY              02
  66#define PICKAXE_BLAME_COPY_HARDER       04
  67#define PICKAXE_BLAME_COPY_HARDEST      010
  68
  69static unsigned blame_move_score;
  70static unsigned blame_copy_score;
  71#define BLAME_DEFAULT_MOVE_SCORE        20
  72#define BLAME_DEFAULT_COPY_SCORE        40
  73
  74/* Remember to update object flag allocation in object.h */
  75#define METAINFO_SHOWN          (1u<<12)
  76#define MORE_THAN_ONE_PATH      (1u<<13)
  77
  78/*
  79 * One blob in a commit that is being suspected
  80 */
  81struct blame_origin {
  82        int refcnt;
  83        /* Record preceding blame record for this blob */
  84        struct blame_origin *previous;
  85        /* origins are put in a list linked via `next' hanging off the
  86         * corresponding commit's util field in order to make finding
  87         * them fast.  The presence in this chain does not count
  88         * towards the origin's reference count.  It is tempting to
  89         * let it count as long as the commit is pending examination,
  90         * but even under circumstances where the commit will be
  91         * present multiple times in the priority queue of unexamined
  92         * commits, processing the first instance will not leave any
  93         * work requiring the origin data for the second instance.  An
  94         * interspersed commit changing that would have to be
  95         * preexisting with a different ancestry and with the same
  96         * commit date in order to wedge itself between two instances
  97         * of the same commit in the priority queue _and_ produce
  98         * blame entries relevant for it.  While we don't want to let
  99         * us get tripped up by this case, it certainly does not seem
 100         * worth optimizing for.
 101         */
 102        struct blame_origin *next;
 103        struct commit *commit;
 104        /* `suspects' contains blame entries that may be attributed to
 105         * this origin's commit or to parent commits.  When a commit
 106         * is being processed, all suspects will be moved, either by
 107         * assigning them to an origin in a different commit, or by
 108         * shipping them to the scoreboard's ent list because they
 109         * cannot be attributed to a different commit.
 110         */
 111        struct blame_entry *suspects;
 112        mmfile_t file;
 113        struct object_id blob_oid;
 114        unsigned mode;
 115        /* guilty gets set when shipping any suspects to the final
 116         * blame list instead of other commits
 117         */
 118        char guilty;
 119        char path[FLEX_ARRAY];
 120};
 121
 122struct progress_info {
 123        struct progress *progress;
 124        int blamed_lines;
 125};
 126
 127static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
 128                      xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
 129{
 130        xpparam_t xpp = {0};
 131        xdemitconf_t xecfg = {0};
 132        xdemitcb_t ecb = {NULL};
 133
 134        xpp.flags = xdl_opts;
 135        xecfg.hunk_func = hunk_func;
 136        ecb.priv = cb_data;
 137        return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
 138}
 139
 140/*
 141 * Given an origin, prepare mmfile_t structure to be used by the
 142 * diff machinery
 143 */
 144static void fill_origin_blob(struct diff_options *opt,
 145                             struct blame_origin *o, mmfile_t *file, int *num_read_blob)
 146{
 147        if (!o->file.ptr) {
 148                enum object_type type;
 149                unsigned long file_size;
 150
 151                (*num_read_blob)++;
 152                if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
 153                    textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size))
 154                        ;
 155                else
 156                        file->ptr = read_sha1_file(o->blob_oid.hash, &type,
 157                                                   &file_size);
 158                file->size = file_size;
 159
 160                if (!file->ptr)
 161                        die("Cannot read blob %s for path %s",
 162                            oid_to_hex(&o->blob_oid),
 163                            o->path);
 164                o->file = *file;
 165        }
 166        else
 167                *file = o->file;
 168}
 169
 170/*
 171 * Origin is refcounted and usually we keep the blob contents to be
 172 * reused.
 173 */
 174static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
 175{
 176        if (o)
 177                o->refcnt++;
 178        return o;
 179}
 180
 181static void blame_origin_decref(struct blame_origin *o)
 182{
 183        if (o && --o->refcnt <= 0) {
 184                struct blame_origin *p, *l = NULL;
 185                if (o->previous)
 186                        blame_origin_decref(o->previous);
 187                free(o->file.ptr);
 188                /* Should be present exactly once in commit chain */
 189                for (p = o->commit->util; p; l = p, p = p->next) {
 190                        if (p == o) {
 191                                if (l)
 192                                        l->next = p->next;
 193                                else
 194                                        o->commit->util = p->next;
 195                                free(o);
 196                                return;
 197                        }
 198                }
 199                die("internal error in blame_origin_decref");
 200        }
 201}
 202
 203static void drop_origin_blob(struct blame_origin *o)
 204{
 205        if (o->file.ptr) {
 206                free(o->file.ptr);
 207                o->file.ptr = NULL;
 208        }
 209}
 210
 211/*
 212 * Each group of lines is described by a blame_entry; it can be split
 213 * as we pass blame to the parents.  They are arranged in linked lists
 214 * kept as `suspects' of some unprocessed origin, or entered (when the
 215 * blame origin has been finalized) into the scoreboard structure.
 216 * While the scoreboard structure is only sorted at the end of
 217 * processing (according to final image line number), the lists
 218 * attached to an origin are sorted by the target line number.
 219 */
 220struct blame_entry {
 221        struct blame_entry *next;
 222
 223        /* the first line of this group in the final image;
 224         * internally all line numbers are 0 based.
 225         */
 226        int lno;
 227
 228        /* how many lines this group has */
 229        int num_lines;
 230
 231        /* the commit that introduced this group into the final image */
 232        struct blame_origin *suspect;
 233
 234        /* the line number of the first line of this group in the
 235         * suspect's file; internally all line numbers are 0 based.
 236         */
 237        int s_lno;
 238
 239        /* how significant this entry is -- cached to avoid
 240         * scanning the lines over and over.
 241         */
 242        unsigned score;
 243};
 244
 245/*
 246 * Any merge of blames happens on lists of blames that arrived via
 247 * different parents in a single suspect.  In this case, we want to
 248 * sort according to the suspect line numbers as opposed to the final
 249 * image line numbers.  The function body is somewhat longish because
 250 * it avoids unnecessary writes.
 251 */
 252
 253static struct blame_entry *blame_merge(struct blame_entry *list1,
 254                                       struct blame_entry *list2)
 255{
 256        struct blame_entry *p1 = list1, *p2 = list2,
 257                **tail = &list1;
 258
 259        if (!p1)
 260                return p2;
 261        if (!p2)
 262                return p1;
 263
 264        if (p1->s_lno <= p2->s_lno) {
 265                do {
 266                        tail = &p1->next;
 267                        if ((p1 = *tail) == NULL) {
 268                                *tail = p2;
 269                                return list1;
 270                        }
 271                } while (p1->s_lno <= p2->s_lno);
 272        }
 273        for (;;) {
 274                *tail = p2;
 275                do {
 276                        tail = &p2->next;
 277                        if ((p2 = *tail) == NULL)  {
 278                                *tail = p1;
 279                                return list1;
 280                        }
 281                } while (p1->s_lno > p2->s_lno);
 282                *tail = p1;
 283                do {
 284                        tail = &p1->next;
 285                        if ((p1 = *tail) == NULL) {
 286                                *tail = p2;
 287                                return list1;
 288                        }
 289                } while (p1->s_lno <= p2->s_lno);
 290        }
 291}
 292
 293static void *get_next_blame(const void *p)
 294{
 295        return ((struct blame_entry *)p)->next;
 296}
 297
 298static void set_next_blame(void *p1, void *p2)
 299{
 300        ((struct blame_entry *)p1)->next = p2;
 301}
 302
 303/*
 304 * Final image line numbers are all different, so we don't need a
 305 * three-way comparison here.
 306 */
 307
 308static int compare_blame_final(const void *p1, const void *p2)
 309{
 310        return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
 311                ? 1 : -1;
 312}
 313
 314static int compare_blame_suspect(const void *p1, const void *p2)
 315{
 316        const struct blame_entry *s1 = p1, *s2 = p2;
 317        /*
 318         * to allow for collating suspects, we sort according to the
 319         * respective pointer value as the primary sorting criterion.
 320         * The actual relation is pretty unimportant as long as it
 321         * establishes a total order.  Comparing as integers gives us
 322         * that.
 323         */
 324        if (s1->suspect != s2->suspect)
 325                return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
 326        if (s1->s_lno == s2->s_lno)
 327                return 0;
 328        return s1->s_lno > s2->s_lno ? 1 : -1;
 329}
 330
 331static struct blame_entry *blame_sort(struct blame_entry *head,
 332                                      int (*compare_fn)(const void *, const void *))
 333{
 334        return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn);
 335}
 336
 337static int compare_commits_by_reverse_commit_date(const void *a,
 338                                                  const void *b,
 339                                                  void *c)
 340{
 341        return -compare_commits_by_commit_date(a, b, c);
 342}
 343
 344/*
 345 * The current state of the blame assignment.
 346 */
 347struct blame_scoreboard {
 348        /* the final commit (i.e. where we started digging from) */
 349        struct commit *final;
 350        /* Priority queue for commits with unassigned blame records */
 351        struct prio_queue commits;
 352        struct rev_info *revs;
 353        const char *path;
 354
 355        /*
 356         * The contents in the final image.
 357         * Used by many functions to obtain contents of the nth line,
 358         * indexed with scoreboard.lineno[blame_entry.lno].
 359         */
 360        const char *final_buf;
 361        unsigned long final_buf_size;
 362
 363        /* linked list of blames */
 364        struct blame_entry *ent;
 365
 366        /* look-up a line in the final buffer */
 367        int num_lines;
 368        int *lineno;
 369
 370        /* stats */
 371        int num_read_blob;
 372        int num_get_patch;
 373        int num_commits;
 374
 375        /*
 376         * blame for a blame_entry with score lower than these thresholds
 377         * is not passed to the parent using move/copy logic.
 378         */
 379        unsigned move_score;
 380        unsigned copy_score;
 381
 382        /* use this file's contents as the final image */
 383        const char *contents_from;
 384
 385        /* flags */
 386        int reverse;
 387};
 388
 389static void sanity_check_refcnt(struct blame_scoreboard *);
 390
 391/*
 392 * If two blame entries that are next to each other came from
 393 * contiguous lines in the same origin (i.e. <commit, path> pair),
 394 * merge them together.
 395 */
 396static void blame_coalesce(struct blame_scoreboard *sb)
 397{
 398        struct blame_entry *ent, *next;
 399
 400        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
 401                if (ent->suspect == next->suspect &&
 402                    ent->s_lno + ent->num_lines == next->s_lno) {
 403                        ent->num_lines += next->num_lines;
 404                        ent->next = next->next;
 405                        blame_origin_decref(next->suspect);
 406                        free(next);
 407                        ent->score = 0;
 408                        next = ent; /* again */
 409                }
 410        }
 411
 412        if (DEBUG) /* sanity */
 413                sanity_check_refcnt(sb);
 414}
 415
 416/*
 417 * Merge the given sorted list of blames into a preexisting origin.
 418 * If there were no previous blames to that commit, it is entered into
 419 * the commit priority queue of the score board.
 420 */
 421
 422static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
 423                         struct blame_entry *sorted)
 424{
 425        if (porigin->suspects)
 426                porigin->suspects = blame_merge(porigin->suspects, sorted);
 427        else {
 428                struct blame_origin *o;
 429                for (o = porigin->commit->util; o; o = o->next) {
 430                        if (o->suspects) {
 431                                porigin->suspects = sorted;
 432                                return;
 433                        }
 434                }
 435                porigin->suspects = sorted;
 436                prio_queue_put(&sb->commits, porigin->commit);
 437        }
 438}
 439
 440/*
 441 * Given a commit and a path in it, create a new origin structure.
 442 * The callers that add blame to the scoreboard should use
 443 * get_origin() to obtain shared, refcounted copy instead of calling
 444 * this function directly.
 445 */
 446static struct blame_origin *make_origin(struct commit *commit, const char *path)
 447{
 448        struct blame_origin *o;
 449        FLEX_ALLOC_STR(o, path, path);
 450        o->commit = commit;
 451        o->refcnt = 1;
 452        o->next = commit->util;
 453        commit->util = o;
 454        return o;
 455}
 456
 457/*
 458 * Locate an existing origin or create a new one.
 459 * This moves the origin to front position in the commit util list.
 460 */
 461static struct blame_origin *get_origin(struct commit *commit, const char *path)
 462{
 463        struct blame_origin *o, *l;
 464
 465        for (o = commit->util, l = NULL; o; l = o, o = o->next) {
 466                if (!strcmp(o->path, path)) {
 467                        /* bump to front */
 468                        if (l) {
 469                                l->next = o->next;
 470                                o->next = commit->util;
 471                                commit->util = o;
 472                        }
 473                        return blame_origin_incref(o);
 474                }
 475        }
 476        return make_origin(commit, path);
 477}
 478
 479/*
 480 * Fill the blob_sha1 field of an origin if it hasn't, so that later
 481 * call to fill_origin_blob() can use it to locate the data.  blob_sha1
 482 * for an origin is also used to pass the blame for the entire file to
 483 * the parent to detect the case where a child's blob is identical to
 484 * that of its parent's.
 485 *
 486 * This also fills origin->mode for corresponding tree path.
 487 */
 488static int fill_blob_sha1_and_mode(struct blame_origin *origin)
 489{
 490        if (!is_null_oid(&origin->blob_oid))
 491                return 0;
 492        if (get_tree_entry(origin->commit->object.oid.hash,
 493                           origin->path,
 494                           origin->blob_oid.hash, &origin->mode))
 495                goto error_out;
 496        if (sha1_object_info(origin->blob_oid.hash, NULL) != OBJ_BLOB)
 497                goto error_out;
 498        return 0;
 499 error_out:
 500        oidclr(&origin->blob_oid);
 501        origin->mode = S_IFINVALID;
 502        return -1;
 503}
 504
 505/*
 506 * We have an origin -- check if the same path exists in the
 507 * parent and return an origin structure to represent it.
 508 */
 509static struct blame_origin *find_origin(struct commit *parent,
 510                                  struct blame_origin *origin)
 511{
 512        struct blame_origin *porigin;
 513        struct diff_options diff_opts;
 514        const char *paths[2];
 515
 516        /* First check any existing origins */
 517        for (porigin = parent->util; porigin; porigin = porigin->next)
 518                if (!strcmp(porigin->path, origin->path)) {
 519                        /*
 520                         * The same path between origin and its parent
 521                         * without renaming -- the most common case.
 522                         */
 523                        return blame_origin_incref (porigin);
 524                }
 525
 526        /* See if the origin->path is different between parent
 527         * and origin first.  Most of the time they are the
 528         * same and diff-tree is fairly efficient about this.
 529         */
 530        diff_setup(&diff_opts);
 531        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 532        diff_opts.detect_rename = 0;
 533        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 534        paths[0] = origin->path;
 535        paths[1] = NULL;
 536
 537        parse_pathspec(&diff_opts.pathspec,
 538                       PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
 539                       PATHSPEC_LITERAL_PATH, "", paths);
 540        diff_setup_done(&diff_opts);
 541
 542        if (is_null_oid(&origin->commit->object.oid))
 543                do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
 544        else
 545                diff_tree_sha1(parent->tree->object.oid.hash,
 546                               origin->commit->tree->object.oid.hash,
 547                               "", &diff_opts);
 548        diffcore_std(&diff_opts);
 549
 550        if (!diff_queued_diff.nr) {
 551                /* The path is the same as parent */
 552                porigin = get_origin(parent, origin->path);
 553                oidcpy(&porigin->blob_oid, &origin->blob_oid);
 554                porigin->mode = origin->mode;
 555        } else {
 556                /*
 557                 * Since origin->path is a pathspec, if the parent
 558                 * commit had it as a directory, we will see a whole
 559                 * bunch of deletion of files in the directory that we
 560                 * do not care about.
 561                 */
 562                int i;
 563                struct diff_filepair *p = NULL;
 564                for (i = 0; i < diff_queued_diff.nr; i++) {
 565                        const char *name;
 566                        p = diff_queued_diff.queue[i];
 567                        name = p->one->path ? p->one->path : p->two->path;
 568                        if (!strcmp(name, origin->path))
 569                                break;
 570                }
 571                if (!p)
 572                        die("internal error in blame::find_origin");
 573                switch (p->status) {
 574                default:
 575                        die("internal error in blame::find_origin (%c)",
 576                            p->status);
 577                case 'M':
 578                        porigin = get_origin(parent, origin->path);
 579                        oidcpy(&porigin->blob_oid, &p->one->oid);
 580                        porigin->mode = p->one->mode;
 581                        break;
 582                case 'A':
 583                case 'T':
 584                        /* Did not exist in parent, or type changed */
 585                        break;
 586                }
 587        }
 588        diff_flush(&diff_opts);
 589        clear_pathspec(&diff_opts.pathspec);
 590        return porigin;
 591}
 592
 593/*
 594 * We have an origin -- find the path that corresponds to it in its
 595 * parent and return an origin structure to represent it.
 596 */
 597static struct blame_origin *find_rename(struct commit *parent,
 598                                  struct blame_origin *origin)
 599{
 600        struct blame_origin *porigin = NULL;
 601        struct diff_options diff_opts;
 602        int i;
 603
 604        diff_setup(&diff_opts);
 605        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 606        diff_opts.detect_rename = DIFF_DETECT_RENAME;
 607        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 608        diff_opts.single_follow = origin->path;
 609        diff_setup_done(&diff_opts);
 610
 611        if (is_null_oid(&origin->commit->object.oid))
 612                do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
 613        else
 614                diff_tree_sha1(parent->tree->object.oid.hash,
 615                               origin->commit->tree->object.oid.hash,
 616                               "", &diff_opts);
 617        diffcore_std(&diff_opts);
 618
 619        for (i = 0; i < diff_queued_diff.nr; i++) {
 620                struct diff_filepair *p = diff_queued_diff.queue[i];
 621                if ((p->status == 'R' || p->status == 'C') &&
 622                    !strcmp(p->two->path, origin->path)) {
 623                        porigin = get_origin(parent, p->one->path);
 624                        oidcpy(&porigin->blob_oid, &p->one->oid);
 625                        porigin->mode = p->one->mode;
 626                        break;
 627                }
 628        }
 629        diff_flush(&diff_opts);
 630        clear_pathspec(&diff_opts.pathspec);
 631        return porigin;
 632}
 633
 634/*
 635 * Append a new blame entry to a given output queue.
 636 */
 637static void add_blame_entry(struct blame_entry ***queue,
 638                            const struct blame_entry *src)
 639{
 640        struct blame_entry *e = xmalloc(sizeof(*e));
 641        memcpy(e, src, sizeof(*e));
 642        blame_origin_incref(e->suspect);
 643
 644        e->next = **queue;
 645        **queue = e;
 646        *queue = &e->next;
 647}
 648
 649/*
 650 * src typically is on-stack; we want to copy the information in it to
 651 * a malloced blame_entry that gets added to the given queue.  The
 652 * origin of dst loses a refcnt.
 653 */
 654static void dup_entry(struct blame_entry ***queue,
 655                      struct blame_entry *dst, struct blame_entry *src)
 656{
 657        blame_origin_incref(src->suspect);
 658        blame_origin_decref(dst->suspect);
 659        memcpy(dst, src, sizeof(*src));
 660        dst->next = **queue;
 661        **queue = dst;
 662        *queue = &dst->next;
 663}
 664
 665static const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
 666{
 667        return sb->final_buf + sb->lineno[lno];
 668}
 669
 670static const char *nth_line_cb(void *data, long lno)
 671{
 672        return blame_nth_line((struct blame_scoreboard *)data, lno);
 673}
 674
 675/*
 676 * It is known that lines between tlno to same came from parent, and e
 677 * has an overlap with that range.  it also is known that parent's
 678 * line plno corresponds to e's line tlno.
 679 *
 680 *                <---- e ----->
 681 *                   <------>
 682 *                   <------------>
 683 *             <------------>
 684 *             <------------------>
 685 *
 686 * Split e into potentially three parts; before this chunk, the chunk
 687 * to be blamed for the parent, and after that portion.
 688 */
 689static void split_overlap(struct blame_entry *split,
 690                          struct blame_entry *e,
 691                          int tlno, int plno, int same,
 692                          struct blame_origin *parent)
 693{
 694        int chunk_end_lno;
 695        memset(split, 0, sizeof(struct blame_entry [3]));
 696
 697        if (e->s_lno < tlno) {
 698                /* there is a pre-chunk part not blamed on parent */
 699                split[0].suspect = blame_origin_incref(e->suspect);
 700                split[0].lno = e->lno;
 701                split[0].s_lno = e->s_lno;
 702                split[0].num_lines = tlno - e->s_lno;
 703                split[1].lno = e->lno + tlno - e->s_lno;
 704                split[1].s_lno = plno;
 705        }
 706        else {
 707                split[1].lno = e->lno;
 708                split[1].s_lno = plno + (e->s_lno - tlno);
 709        }
 710
 711        if (same < e->s_lno + e->num_lines) {
 712                /* there is a post-chunk part not blamed on parent */
 713                split[2].suspect = blame_origin_incref(e->suspect);
 714                split[2].lno = e->lno + (same - e->s_lno);
 715                split[2].s_lno = e->s_lno + (same - e->s_lno);
 716                split[2].num_lines = e->s_lno + e->num_lines - same;
 717                chunk_end_lno = split[2].lno;
 718        }
 719        else
 720                chunk_end_lno = e->lno + e->num_lines;
 721        split[1].num_lines = chunk_end_lno - split[1].lno;
 722
 723        /*
 724         * if it turns out there is nothing to blame the parent for,
 725         * forget about the splitting.  !split[1].suspect signals this.
 726         */
 727        if (split[1].num_lines < 1)
 728                return;
 729        split[1].suspect = blame_origin_incref(parent);
 730}
 731
 732/*
 733 * split_overlap() divided an existing blame e into up to three parts
 734 * in split.  Any assigned blame is moved to queue to
 735 * reflect the split.
 736 */
 737static void split_blame(struct blame_entry ***blamed,
 738                        struct blame_entry ***unblamed,
 739                        struct blame_entry *split,
 740                        struct blame_entry *e)
 741{
 742        if (split[0].suspect && split[2].suspect) {
 743                /* The first part (reuse storage for the existing entry e) */
 744                dup_entry(unblamed, e, &split[0]);
 745
 746                /* The last part -- me */
 747                add_blame_entry(unblamed, &split[2]);
 748
 749                /* ... and the middle part -- parent */
 750                add_blame_entry(blamed, &split[1]);
 751        }
 752        else if (!split[0].suspect && !split[2].suspect)
 753                /*
 754                 * The parent covers the entire area; reuse storage for
 755                 * e and replace it with the parent.
 756                 */
 757                dup_entry(blamed, e, &split[1]);
 758        else if (split[0].suspect) {
 759                /* me and then parent */
 760                dup_entry(unblamed, e, &split[0]);
 761                add_blame_entry(blamed, &split[1]);
 762        }
 763        else {
 764                /* parent and then me */
 765                dup_entry(blamed, e, &split[1]);
 766                add_blame_entry(unblamed, &split[2]);
 767        }
 768}
 769
 770/*
 771 * After splitting the blame, the origins used by the
 772 * on-stack blame_entry should lose one refcnt each.
 773 */
 774static void decref_split(struct blame_entry *split)
 775{
 776        int i;
 777
 778        for (i = 0; i < 3; i++)
 779                blame_origin_decref(split[i].suspect);
 780}
 781
 782/*
 783 * reverse_blame reverses the list given in head, appending tail.
 784 * That allows us to build lists in reverse order, then reverse them
 785 * afterwards.  This can be faster than building the list in proper
 786 * order right away.  The reason is that building in proper order
 787 * requires writing a link in the _previous_ element, while building
 788 * in reverse order just requires placing the list head into the
 789 * _current_ element.
 790 */
 791
 792static struct blame_entry *reverse_blame(struct blame_entry *head,
 793                                         struct blame_entry *tail)
 794{
 795        while (head) {
 796                struct blame_entry *next = head->next;
 797                head->next = tail;
 798                tail = head;
 799                head = next;
 800        }
 801        return tail;
 802}
 803
 804/*
 805 * Process one hunk from the patch between the current suspect for
 806 * blame_entry e and its parent.  This first blames any unfinished
 807 * entries before the chunk (which is where target and parent start
 808 * differing) on the parent, and then splits blame entries at the
 809 * start and at the end of the difference region.  Since use of -M and
 810 * -C options may lead to overlapping/duplicate source line number
 811 * ranges, all we can rely on from sorting/merging is the order of the
 812 * first suspect line number.
 813 */
 814static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
 815                        int tlno, int offset, int same,
 816                        struct blame_origin *parent)
 817{
 818        struct blame_entry *e = **srcq;
 819        struct blame_entry *samep = NULL, *diffp = NULL;
 820
 821        while (e && e->s_lno < tlno) {
 822                struct blame_entry *next = e->next;
 823                /*
 824                 * current record starts before differing portion.  If
 825                 * it reaches into it, we need to split it up and
 826                 * examine the second part separately.
 827                 */
 828                if (e->s_lno + e->num_lines > tlno) {
 829                        /* Move second half to a new record */
 830                        int len = tlno - e->s_lno;
 831                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
 832                        n->suspect = e->suspect;
 833                        n->lno = e->lno + len;
 834                        n->s_lno = e->s_lno + len;
 835                        n->num_lines = e->num_lines - len;
 836                        e->num_lines = len;
 837                        e->score = 0;
 838                        /* Push new record to diffp */
 839                        n->next = diffp;
 840                        diffp = n;
 841                } else
 842                        blame_origin_decref(e->suspect);
 843                /* Pass blame for everything before the differing
 844                 * chunk to the parent */
 845                e->suspect = blame_origin_incref(parent);
 846                e->s_lno += offset;
 847                e->next = samep;
 848                samep = e;
 849                e = next;
 850        }
 851        /*
 852         * As we don't know how much of a common stretch after this
 853         * diff will occur, the currently blamed parts are all that we
 854         * can assign to the parent for now.
 855         */
 856
 857        if (samep) {
 858                **dstq = reverse_blame(samep, **dstq);
 859                *dstq = &samep->next;
 860        }
 861        /*
 862         * Prepend the split off portions: everything after e starts
 863         * after the blameable portion.
 864         */
 865        e = reverse_blame(diffp, e);
 866
 867        /*
 868         * Now retain records on the target while parts are different
 869         * from the parent.
 870         */
 871        samep = NULL;
 872        diffp = NULL;
 873        while (e && e->s_lno < same) {
 874                struct blame_entry *next = e->next;
 875
 876                /*
 877                 * If current record extends into sameness, need to split.
 878                 */
 879                if (e->s_lno + e->num_lines > same) {
 880                        /*
 881                         * Move second half to a new record to be
 882                         * processed by later chunks
 883                         */
 884                        int len = same - e->s_lno;
 885                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
 886                        n->suspect = blame_origin_incref(e->suspect);
 887                        n->lno = e->lno + len;
 888                        n->s_lno = e->s_lno + len;
 889                        n->num_lines = e->num_lines - len;
 890                        e->num_lines = len;
 891                        e->score = 0;
 892                        /* Push new record to samep */
 893                        n->next = samep;
 894                        samep = n;
 895                }
 896                e->next = diffp;
 897                diffp = e;
 898                e = next;
 899        }
 900        **srcq = reverse_blame(diffp, reverse_blame(samep, e));
 901        /* Move across elements that are in the unblamable portion */
 902        if (diffp)
 903                *srcq = &diffp->next;
 904}
 905
 906struct blame_chunk_cb_data {
 907        struct blame_origin *parent;
 908        long offset;
 909        struct blame_entry **dstq;
 910        struct blame_entry **srcq;
 911};
 912
 913/* diff chunks are from parent to target */
 914static int blame_chunk_cb(long start_a, long count_a,
 915                          long start_b, long count_b, void *data)
 916{
 917        struct blame_chunk_cb_data *d = data;
 918        if (start_a - start_b != d->offset)
 919                die("internal error in blame::blame_chunk_cb");
 920        blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
 921                    start_b + count_b, d->parent);
 922        d->offset = start_a + count_a - (start_b + count_b);
 923        return 0;
 924}
 925
 926/*
 927 * We are looking at the origin 'target' and aiming to pass blame
 928 * for the lines it is suspected to its parent.  Run diff to find
 929 * which lines came from parent and pass blame for them.
 930 */
 931static void pass_blame_to_parent(struct blame_scoreboard *sb,
 932                                 struct blame_origin *target,
 933                                 struct blame_origin *parent)
 934{
 935        mmfile_t file_p, file_o;
 936        struct blame_chunk_cb_data d;
 937        struct blame_entry *newdest = NULL;
 938
 939        if (!target->suspects)
 940                return; /* nothing remains for this target */
 941
 942        d.parent = parent;
 943        d.offset = 0;
 944        d.dstq = &newdest; d.srcq = &target->suspects;
 945
 946        fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
 947        fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
 948        sb->num_get_patch++;
 949
 950        if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d))
 951                die("unable to generate diff (%s -> %s)",
 952                    oid_to_hex(&parent->commit->object.oid),
 953                    oid_to_hex(&target->commit->object.oid));
 954        /* The rest are the same as the parent */
 955        blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
 956        *d.dstq = NULL;
 957        queue_blames(sb, parent, newdest);
 958
 959        return;
 960}
 961
 962/*
 963 * The lines in blame_entry after splitting blames many times can become
 964 * very small and trivial, and at some point it becomes pointless to
 965 * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
 966 * ordinary C program, and it is not worth to say it was copied from
 967 * totally unrelated file in the parent.
 968 *
 969 * Compute how trivial the lines in the blame_entry are.
 970 */
 971static unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
 972{
 973        unsigned score;
 974        const char *cp, *ep;
 975
 976        if (e->score)
 977                return e->score;
 978
 979        score = 1;
 980        cp = blame_nth_line(sb, e->lno);
 981        ep = blame_nth_line(sb, e->lno + e->num_lines);
 982        while (cp < ep) {
 983                unsigned ch = *((unsigned char *)cp);
 984                if (isalnum(ch))
 985                        score++;
 986                cp++;
 987        }
 988        e->score = score;
 989        return score;
 990}
 991
 992/*
 993 * best_so_far[] and this[] are both a split of an existing blame_entry
 994 * that passes blame to the parent.  Maintain best_so_far the best split
 995 * so far, by comparing this and best_so_far and copying this into
 996 * bst_so_far as needed.
 997 */
 998static void copy_split_if_better(struct blame_scoreboard *sb,
 999                                 struct blame_entry *best_so_far,
1000                                 struct blame_entry *this)
1001{
1002        int i;
1003
1004        if (!this[1].suspect)
1005                return;
1006        if (best_so_far[1].suspect) {
1007                if (blame_entry_score(sb, &this[1]) < blame_entry_score(sb, &best_so_far[1]))
1008                        return;
1009        }
1010
1011        for (i = 0; i < 3; i++)
1012                blame_origin_incref(this[i].suspect);
1013        decref_split(best_so_far);
1014        memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
1015}
1016
1017/*
1018 * We are looking at a part of the final image represented by
1019 * ent (tlno and same are offset by ent->s_lno).
1020 * tlno is where we are looking at in the final image.
1021 * up to (but not including) same match preimage.
1022 * plno is where we are looking at in the preimage.
1023 *
1024 * <-------------- final image ---------------------->
1025 *       <------ent------>
1026 *         ^tlno ^same
1027 *    <---------preimage----->
1028 *         ^plno
1029 *
1030 * All line numbers are 0-based.
1031 */
1032static void handle_split(struct blame_scoreboard *sb,
1033                         struct blame_entry *ent,
1034                         int tlno, int plno, int same,
1035                         struct blame_origin *parent,
1036                         struct blame_entry *split)
1037{
1038        if (ent->num_lines <= tlno)
1039                return;
1040        if (tlno < same) {
1041                struct blame_entry this[3];
1042                tlno += ent->s_lno;
1043                same += ent->s_lno;
1044                split_overlap(this, ent, tlno, plno, same, parent);
1045                copy_split_if_better(sb, split, this);
1046                decref_split(this);
1047        }
1048}
1049
1050struct handle_split_cb_data {
1051        struct blame_scoreboard *sb;
1052        struct blame_entry *ent;
1053        struct blame_origin *parent;
1054        struct blame_entry *split;
1055        long plno;
1056        long tlno;
1057};
1058
1059static int handle_split_cb(long start_a, long count_a,
1060                           long start_b, long count_b, void *data)
1061{
1062        struct handle_split_cb_data *d = data;
1063        handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1064                     d->split);
1065        d->plno = start_a + count_a;
1066        d->tlno = start_b + count_b;
1067        return 0;
1068}
1069
1070/*
1071 * Find the lines from parent that are the same as ent so that
1072 * we can pass blames to it.  file_p has the blob contents for
1073 * the parent.
1074 */
1075static void find_copy_in_blob(struct blame_scoreboard *sb,
1076                              struct blame_entry *ent,
1077                              struct blame_origin *parent,
1078                              struct blame_entry *split,
1079                              mmfile_t *file_p)
1080{
1081        const char *cp;
1082        mmfile_t file_o;
1083        struct handle_split_cb_data d;
1084
1085        memset(&d, 0, sizeof(d));
1086        d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1087        /*
1088         * Prepare mmfile that contains only the lines in ent.
1089         */
1090        cp = blame_nth_line(sb, ent->lno);
1091        file_o.ptr = (char *) cp;
1092        file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
1093
1094        /*
1095         * file_o is a part of final image we are annotating.
1096         * file_p partially may match that image.
1097         */
1098        memset(split, 0, sizeof(struct blame_entry [3]));
1099        if (diff_hunks(file_p, &file_o, handle_split_cb, &d))
1100                die("unable to generate diff (%s)",
1101                    oid_to_hex(&parent->commit->object.oid));
1102        /* remainder, if any, all match the preimage */
1103        handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1104}
1105
1106/* Move all blame entries from list *source that have a score smaller
1107 * than score_min to the front of list *small.
1108 * Returns a pointer to the link pointing to the old head of the small list.
1109 */
1110
1111static struct blame_entry **filter_small(struct blame_scoreboard *sb,
1112                                         struct blame_entry **small,
1113                                         struct blame_entry **source,
1114                                         unsigned score_min)
1115{
1116        struct blame_entry *p = *source;
1117        struct blame_entry *oldsmall = *small;
1118        while (p) {
1119                if (blame_entry_score(sb, p) <= score_min) {
1120                        *small = p;
1121                        small = &p->next;
1122                        p = *small;
1123                } else {
1124                        *source = p;
1125                        source = &p->next;
1126                        p = *source;
1127                }
1128        }
1129        *small = oldsmall;
1130        *source = NULL;
1131        return small;
1132}
1133
1134/*
1135 * See if lines currently target is suspected for can be attributed to
1136 * parent.
1137 */
1138static void find_move_in_parent(struct blame_scoreboard *sb,
1139                                struct blame_entry ***blamed,
1140                                struct blame_entry **toosmall,
1141                                struct blame_origin *target,
1142                                struct blame_origin *parent)
1143{
1144        struct blame_entry *e, split[3];
1145        struct blame_entry *unblamed = target->suspects;
1146        struct blame_entry *leftover = NULL;
1147        mmfile_t file_p;
1148
1149        if (!unblamed)
1150                return; /* nothing remains for this target */
1151
1152        fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1153        if (!file_p.ptr)
1154                return;
1155
1156        /* At each iteration, unblamed has a NULL-terminated list of
1157         * entries that have not yet been tested for blame.  leftover
1158         * contains the reversed list of entries that have been tested
1159         * without being assignable to the parent.
1160         */
1161        do {
1162                struct blame_entry **unblamedtail = &unblamed;
1163                struct blame_entry *next;
1164                for (e = unblamed; e; e = next) {
1165                        next = e->next;
1166                        find_copy_in_blob(sb, e, parent, split, &file_p);
1167                        if (split[1].suspect &&
1168                            sb->move_score < blame_entry_score(sb, &split[1])) {
1169                                split_blame(blamed, &unblamedtail, split, e);
1170                        } else {
1171                                e->next = leftover;
1172                                leftover = e;
1173                        }
1174                        decref_split(split);
1175                }
1176                *unblamedtail = NULL;
1177                toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
1178        } while (unblamed);
1179        target->suspects = reverse_blame(leftover, NULL);
1180}
1181
1182struct blame_list {
1183        struct blame_entry *ent;
1184        struct blame_entry split[3];
1185};
1186
1187/*
1188 * Count the number of entries the target is suspected for,
1189 * and prepare a list of entry and the best split.
1190 */
1191static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1192                                           int *num_ents_p)
1193{
1194        struct blame_entry *e;
1195        int num_ents, i;
1196        struct blame_list *blame_list = NULL;
1197
1198        for (e = unblamed, num_ents = 0; e; e = e->next)
1199                num_ents++;
1200        if (num_ents) {
1201                blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1202                for (e = unblamed, i = 0; e; e = e->next)
1203                        blame_list[i++].ent = e;
1204        }
1205        *num_ents_p = num_ents;
1206        return blame_list;
1207}
1208
1209/*
1210 * For lines target is suspected for, see if we can find code movement
1211 * across file boundary from the parent commit.  porigin is the path
1212 * in the parent we already tried.
1213 */
1214static void find_copy_in_parent(struct blame_scoreboard *sb,
1215                                struct blame_entry ***blamed,
1216                                struct blame_entry **toosmall,
1217                                struct blame_origin *target,
1218                                struct commit *parent,
1219                                struct blame_origin *porigin,
1220                                int opt)
1221{
1222        struct diff_options diff_opts;
1223        int i, j;
1224        struct blame_list *blame_list;
1225        int num_ents;
1226        struct blame_entry *unblamed = target->suspects;
1227        struct blame_entry *leftover = NULL;
1228
1229        if (!unblamed)
1230                return; /* nothing remains for this target */
1231
1232        diff_setup(&diff_opts);
1233        DIFF_OPT_SET(&diff_opts, RECURSIVE);
1234        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1235
1236        diff_setup_done(&diff_opts);
1237
1238        /* Try "find copies harder" on new path if requested;
1239         * we do not want to use diffcore_rename() actually to
1240         * match things up; find_copies_harder is set only to
1241         * force diff_tree_sha1() to feed all filepairs to diff_queue,
1242         * and this code needs to be after diff_setup_done(), which
1243         * usually makes find-copies-harder imply copy detection.
1244         */
1245        if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1246            || ((opt & PICKAXE_BLAME_COPY_HARDER)
1247                && (!porigin || strcmp(target->path, porigin->path))))
1248                DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
1249
1250        if (is_null_oid(&target->commit->object.oid))
1251                do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
1252        else
1253                diff_tree_sha1(parent->tree->object.oid.hash,
1254                               target->commit->tree->object.oid.hash,
1255                               "", &diff_opts);
1256
1257        if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
1258                diffcore_std(&diff_opts);
1259
1260        do {
1261                struct blame_entry **unblamedtail = &unblamed;
1262                blame_list = setup_blame_list(unblamed, &num_ents);
1263
1264                for (i = 0; i < diff_queued_diff.nr; i++) {
1265                        struct diff_filepair *p = diff_queued_diff.queue[i];
1266                        struct blame_origin *norigin;
1267                        mmfile_t file_p;
1268                        struct blame_entry this[3];
1269
1270                        if (!DIFF_FILE_VALID(p->one))
1271                                continue; /* does not exist in parent */
1272                        if (S_ISGITLINK(p->one->mode))
1273                                continue; /* ignore git links */
1274                        if (porigin && !strcmp(p->one->path, porigin->path))
1275                                /* find_move already dealt with this path */
1276                                continue;
1277
1278                        norigin = get_origin(parent, p->one->path);
1279                        oidcpy(&norigin->blob_oid, &p->one->oid);
1280                        norigin->mode = p->one->mode;
1281                        fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1282                        if (!file_p.ptr)
1283                                continue;
1284
1285                        for (j = 0; j < num_ents; j++) {
1286                                find_copy_in_blob(sb, blame_list[j].ent,
1287                                                  norigin, this, &file_p);
1288                                copy_split_if_better(sb, blame_list[j].split,
1289                                                     this);
1290                                decref_split(this);
1291                        }
1292                        blame_origin_decref(norigin);
1293                }
1294
1295                for (j = 0; j < num_ents; j++) {
1296                        struct blame_entry *split = blame_list[j].split;
1297                        if (split[1].suspect &&
1298                            sb->copy_score < blame_entry_score(sb, &split[1])) {
1299                                split_blame(blamed, &unblamedtail, split,
1300                                            blame_list[j].ent);
1301                        } else {
1302                                blame_list[j].ent->next = leftover;
1303                                leftover = blame_list[j].ent;
1304                        }
1305                        decref_split(split);
1306                }
1307                free(blame_list);
1308                *unblamedtail = NULL;
1309                toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
1310        } while (unblamed);
1311        target->suspects = reverse_blame(leftover, NULL);
1312        diff_flush(&diff_opts);
1313        clear_pathspec(&diff_opts.pathspec);
1314}
1315
1316/*
1317 * The blobs of origin and porigin exactly match, so everything
1318 * origin is suspected for can be blamed on the parent.
1319 */
1320static void pass_whole_blame(struct blame_scoreboard *sb,
1321                             struct blame_origin *origin, struct blame_origin *porigin)
1322{
1323        struct blame_entry *e, *suspects;
1324
1325        if (!porigin->file.ptr && origin->file.ptr) {
1326                /* Steal its file */
1327                porigin->file = origin->file;
1328                origin->file.ptr = NULL;
1329        }
1330        suspects = origin->suspects;
1331        origin->suspects = NULL;
1332        for (e = suspects; e; e = e->next) {
1333                blame_origin_incref(porigin);
1334                blame_origin_decref(e->suspect);
1335                e->suspect = porigin;
1336        }
1337        queue_blames(sb, porigin, suspects);
1338}
1339
1340/*
1341 * We pass blame from the current commit to its parents.  We keep saying
1342 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1343 * exonerate ourselves.
1344 */
1345static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
1346                                           int reverse)
1347{
1348        if (!reverse) {
1349                if (revs->first_parent_only &&
1350                    commit->parents &&
1351                    commit->parents->next) {
1352                        free_commit_list(commit->parents->next);
1353                        commit->parents->next = NULL;
1354                }
1355                return commit->parents;
1356        }
1357        return lookup_decoration(&revs->children, &commit->object);
1358}
1359
1360static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
1361{
1362        struct commit_list *l = first_scapegoat(revs, commit, reverse);
1363        return commit_list_count(l);
1364}
1365
1366/* Distribute collected unsorted blames to the respected sorted lists
1367 * in the various origins.
1368 */
1369static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
1370{
1371        blamed = blame_sort(blamed, compare_blame_suspect);
1372        while (blamed)
1373        {
1374                struct blame_origin *porigin = blamed->suspect;
1375                struct blame_entry *suspects = NULL;
1376                do {
1377                        struct blame_entry *next = blamed->next;
1378                        blamed->next = suspects;
1379                        suspects = blamed;
1380                        blamed = next;
1381                } while (blamed && blamed->suspect == porigin);
1382                suspects = reverse_blame(suspects, NULL);
1383                queue_blames(sb, porigin, suspects);
1384        }
1385}
1386
1387#define MAXSG 16
1388
1389static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
1390{
1391        struct rev_info *revs = sb->revs;
1392        int i, pass, num_sg;
1393        struct commit *commit = origin->commit;
1394        struct commit_list *sg;
1395        struct blame_origin *sg_buf[MAXSG];
1396        struct blame_origin *porigin, **sg_origin = sg_buf;
1397        struct blame_entry *toosmall = NULL;
1398        struct blame_entry *blames, **blametail = &blames;
1399
1400        num_sg = num_scapegoats(revs, commit, sb->reverse);
1401        if (!num_sg)
1402                goto finish;
1403        else if (num_sg < ARRAY_SIZE(sg_buf))
1404                memset(sg_buf, 0, sizeof(sg_buf));
1405        else
1406                sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1407
1408        /*
1409         * The first pass looks for unrenamed path to optimize for
1410         * common cases, then we look for renames in the second pass.
1411         */
1412        for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
1413                struct blame_origin *(*find)(struct commit *, struct blame_origin *);
1414                find = pass ? find_rename : find_origin;
1415
1416                for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1417                     i < num_sg && sg;
1418                     sg = sg->next, i++) {
1419                        struct commit *p = sg->item;
1420                        int j, same;
1421
1422                        if (sg_origin[i])
1423                                continue;
1424                        if (parse_commit(p))
1425                                continue;
1426                        porigin = find(p, origin);
1427                        if (!porigin)
1428                                continue;
1429                        if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
1430                                pass_whole_blame(sb, origin, porigin);
1431                                blame_origin_decref(porigin);
1432                                goto finish;
1433                        }
1434                        for (j = same = 0; j < i; j++)
1435                                if (sg_origin[j] &&
1436                                    !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
1437                                        same = 1;
1438                                        break;
1439                                }
1440                        if (!same)
1441                                sg_origin[i] = porigin;
1442                        else
1443                                blame_origin_decref(porigin);
1444                }
1445        }
1446
1447        sb->num_commits++;
1448        for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1449             i < num_sg && sg;
1450             sg = sg->next, i++) {
1451                struct blame_origin *porigin = sg_origin[i];
1452                if (!porigin)
1453                        continue;
1454                if (!origin->previous) {
1455                        blame_origin_incref(porigin);
1456                        origin->previous = porigin;
1457                }
1458                pass_blame_to_parent(sb, origin, porigin);
1459                if (!origin->suspects)
1460                        goto finish;
1461        }
1462
1463        /*
1464         * Optionally find moves in parents' files.
1465         */
1466        if (opt & PICKAXE_BLAME_MOVE) {
1467                filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
1468                if (origin->suspects) {
1469                        for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1470                             i < num_sg && sg;
1471                             sg = sg->next, i++) {
1472                                struct blame_origin *porigin = sg_origin[i];
1473                                if (!porigin)
1474                                        continue;
1475                                find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1476                                if (!origin->suspects)
1477                                        break;
1478                        }
1479                }
1480        }
1481
1482        /*
1483         * Optionally find copies from parents' files.
1484         */
1485        if (opt & PICKAXE_BLAME_COPY) {
1486                if (sb->copy_score > sb->move_score)
1487                        filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1488                else if (sb->copy_score < sb->move_score) {
1489                        origin->suspects = blame_merge(origin->suspects, toosmall);
1490                        toosmall = NULL;
1491                        filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1492                }
1493                if (!origin->suspects)
1494                        goto finish;
1495
1496                for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1497                     i < num_sg && sg;
1498                     sg = sg->next, i++) {
1499                        struct blame_origin *porigin = sg_origin[i];
1500                        find_copy_in_parent(sb, &blametail, &toosmall,
1501                                            origin, sg->item, porigin, opt);
1502                        if (!origin->suspects)
1503                                goto finish;
1504                }
1505        }
1506
1507finish:
1508        *blametail = NULL;
1509        distribute_blame(sb, blames);
1510        /*
1511         * prepend toosmall to origin->suspects
1512         *
1513         * There is no point in sorting: this ends up on a big
1514         * unsorted list in the caller anyway.
1515         */
1516        if (toosmall) {
1517                struct blame_entry **tail = &toosmall;
1518                while (*tail)
1519                        tail = &(*tail)->next;
1520                *tail = origin->suspects;
1521                origin->suspects = toosmall;
1522        }
1523        for (i = 0; i < num_sg; i++) {
1524                if (sg_origin[i]) {
1525                        drop_origin_blob(sg_origin[i]);
1526                        blame_origin_decref(sg_origin[i]);
1527                }
1528        }
1529        drop_origin_blob(origin);
1530        if (sg_buf != sg_origin)
1531                free(sg_origin);
1532}
1533
1534/*
1535 * Information on commits, used for output.
1536 */
1537struct commit_info {
1538        struct strbuf author;
1539        struct strbuf author_mail;
1540        timestamp_t author_time;
1541        struct strbuf author_tz;
1542
1543        /* filled only when asked for details */
1544        struct strbuf committer;
1545        struct strbuf committer_mail;
1546        timestamp_t committer_time;
1547        struct strbuf committer_tz;
1548
1549        struct strbuf summary;
1550};
1551
1552/*
1553 * Parse author/committer line in the commit object buffer
1554 */
1555static void get_ac_line(const char *inbuf, const char *what,
1556        struct strbuf *name, struct strbuf *mail,
1557        timestamp_t *time, struct strbuf *tz)
1558{
1559        struct ident_split ident;
1560        size_t len, maillen, namelen;
1561        char *tmp, *endp;
1562        const char *namebuf, *mailbuf;
1563
1564        tmp = strstr(inbuf, what);
1565        if (!tmp)
1566                goto error_out;
1567        tmp += strlen(what);
1568        endp = strchr(tmp, '\n');
1569        if (!endp)
1570                len = strlen(tmp);
1571        else
1572                len = endp - tmp;
1573
1574        if (split_ident_line(&ident, tmp, len)) {
1575        error_out:
1576                /* Ugh */
1577                tmp = "(unknown)";
1578                strbuf_addstr(name, tmp);
1579                strbuf_addstr(mail, tmp);
1580                strbuf_addstr(tz, tmp);
1581                *time = 0;
1582                return;
1583        }
1584
1585        namelen = ident.name_end - ident.name_begin;
1586        namebuf = ident.name_begin;
1587
1588        maillen = ident.mail_end - ident.mail_begin;
1589        mailbuf = ident.mail_begin;
1590
1591        if (ident.date_begin && ident.date_end)
1592                *time = strtoul(ident.date_begin, NULL, 10);
1593        else
1594                *time = 0;
1595
1596        if (ident.tz_begin && ident.tz_end)
1597                strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
1598        else
1599                strbuf_addstr(tz, "(unknown)");
1600
1601        /*
1602         * Now, convert both name and e-mail using mailmap
1603         */
1604        map_user(&mailmap, &mailbuf, &maillen,
1605                 &namebuf, &namelen);
1606
1607        strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
1608        strbuf_add(name, namebuf, namelen);
1609}
1610
1611static void commit_info_init(struct commit_info *ci)
1612{
1613
1614        strbuf_init(&ci->author, 0);
1615        strbuf_init(&ci->author_mail, 0);
1616        strbuf_init(&ci->author_tz, 0);
1617        strbuf_init(&ci->committer, 0);
1618        strbuf_init(&ci->committer_mail, 0);
1619        strbuf_init(&ci->committer_tz, 0);
1620        strbuf_init(&ci->summary, 0);
1621}
1622
1623static void commit_info_destroy(struct commit_info *ci)
1624{
1625
1626        strbuf_release(&ci->author);
1627        strbuf_release(&ci->author_mail);
1628        strbuf_release(&ci->author_tz);
1629        strbuf_release(&ci->committer);
1630        strbuf_release(&ci->committer_mail);
1631        strbuf_release(&ci->committer_tz);
1632        strbuf_release(&ci->summary);
1633}
1634
1635static void get_commit_info(struct commit *commit,
1636                            struct commit_info *ret,
1637                            int detailed)
1638{
1639        int len;
1640        const char *subject, *encoding;
1641        const char *message;
1642
1643        commit_info_init(ret);
1644
1645        encoding = get_log_output_encoding();
1646        message = logmsg_reencode(commit, NULL, encoding);
1647        get_ac_line(message, "\nauthor ",
1648                    &ret->author, &ret->author_mail,
1649                    &ret->author_time, &ret->author_tz);
1650
1651        if (!detailed) {
1652                unuse_commit_buffer(commit, message);
1653                return;
1654        }
1655
1656        get_ac_line(message, "\ncommitter ",
1657                    &ret->committer, &ret->committer_mail,
1658                    &ret->committer_time, &ret->committer_tz);
1659
1660        len = find_commit_subject(message, &subject);
1661        if (len)
1662                strbuf_add(&ret->summary, subject, len);
1663        else
1664                strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
1665
1666        unuse_commit_buffer(commit, message);
1667}
1668
1669/*
1670 * Write out any suspect information which depends on the path. This must be
1671 * handled separately from emit_one_suspect_detail(), because a given commit
1672 * may have changes in multiple paths. So this needs to appear each time
1673 * we mention a new group.
1674 *
1675 * To allow LF and other nonportable characters in pathnames,
1676 * they are c-style quoted as needed.
1677 */
1678static void write_filename_info(struct blame_origin *suspect)
1679{
1680        if (suspect->previous) {
1681                struct blame_origin *prev = suspect->previous;
1682                printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
1683                write_name_quoted(prev->path, stdout, '\n');
1684        }
1685        printf("filename ");
1686        write_name_quoted(suspect->path, stdout, '\n');
1687}
1688
1689/*
1690 * Porcelain/Incremental format wants to show a lot of details per
1691 * commit.  Instead of repeating this every line, emit it only once,
1692 * the first time each commit appears in the output (unless the
1693 * user has specifically asked for us to repeat).
1694 */
1695static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
1696{
1697        struct commit_info ci;
1698
1699        if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
1700                return 0;
1701
1702        suspect->commit->object.flags |= METAINFO_SHOWN;
1703        get_commit_info(suspect->commit, &ci, 1);
1704        printf("author %s\n", ci.author.buf);
1705        printf("author-mail %s\n", ci.author_mail.buf);
1706        printf("author-time %"PRItime"\n", ci.author_time);
1707        printf("author-tz %s\n", ci.author_tz.buf);
1708        printf("committer %s\n", ci.committer.buf);
1709        printf("committer-mail %s\n", ci.committer_mail.buf);
1710        printf("committer-time %"PRItime"\n", ci.committer_time);
1711        printf("committer-tz %s\n", ci.committer_tz.buf);
1712        printf("summary %s\n", ci.summary.buf);
1713        if (suspect->commit->object.flags & UNINTERESTING)
1714                printf("boundary\n");
1715
1716        commit_info_destroy(&ci);
1717
1718        return 1;
1719}
1720
1721/*
1722 * The blame_entry is found to be guilty for the range.
1723 * Show it in incremental output.
1724 */
1725static void found_guilty_entry(struct blame_entry *ent,
1726                           struct progress_info *pi)
1727{
1728        if (incremental) {
1729                struct blame_origin *suspect = ent->suspect;
1730
1731                printf("%s %d %d %d\n",
1732                       oid_to_hex(&suspect->commit->object.oid),
1733                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1734                emit_one_suspect_detail(suspect, 0);
1735                write_filename_info(suspect);
1736                maybe_flush_or_die(stdout, "stdout");
1737        }
1738        pi->blamed_lines += ent->num_lines;
1739        display_progress(pi->progress, pi->blamed_lines);
1740}
1741
1742/*
1743 * The main loop -- while we have blobs with lines whose true origin
1744 * is still unknown, pick one blob, and allow its lines to pass blames
1745 * to its parents. */
1746static void assign_blame(struct blame_scoreboard *sb, int opt)
1747{
1748        struct rev_info *revs = sb->revs;
1749        struct commit *commit = prio_queue_get(&sb->commits);
1750        struct progress_info pi = { NULL, 0 };
1751
1752        if (show_progress)
1753                pi.progress = start_progress_delay(_("Blaming lines"),
1754                                                   sb->num_lines, 50, 1);
1755
1756        while (commit) {
1757                struct blame_entry *ent;
1758                struct blame_origin *suspect = commit->util;
1759
1760                /* find one suspect to break down */
1761                while (suspect && !suspect->suspects)
1762                        suspect = suspect->next;
1763
1764                if (!suspect) {
1765                        commit = prio_queue_get(&sb->commits);
1766                        continue;
1767                }
1768
1769                assert(commit == suspect->commit);
1770
1771                /*
1772                 * We will use this suspect later in the loop,
1773                 * so hold onto it in the meantime.
1774                 */
1775                blame_origin_incref(suspect);
1776                parse_commit(commit);
1777                if (sb->reverse ||
1778                    (!(commit->object.flags & UNINTERESTING) &&
1779                     !(revs->max_age != -1 && commit->date < revs->max_age)))
1780                        pass_blame(sb, suspect, opt);
1781                else {
1782                        commit->object.flags |= UNINTERESTING;
1783                        if (commit->object.parsed)
1784                                mark_parents_uninteresting(commit);
1785                }
1786                /* treat root commit as boundary */
1787                if (!commit->parents && !show_root)
1788                        commit->object.flags |= UNINTERESTING;
1789
1790                /* Take responsibility for the remaining entries */
1791                ent = suspect->suspects;
1792                if (ent) {
1793                        suspect->guilty = 1;
1794                        for (;;) {
1795                                struct blame_entry *next = ent->next;
1796                                found_guilty_entry(ent, &pi);
1797                                if (next) {
1798                                        ent = next;
1799                                        continue;
1800                                }
1801                                ent->next = sb->ent;
1802                                sb->ent = suspect->suspects;
1803                                suspect->suspects = NULL;
1804                                break;
1805                        }
1806                }
1807                blame_origin_decref(suspect);
1808
1809                if (DEBUG) /* sanity */
1810                        sanity_check_refcnt(sb);
1811        }
1812
1813        stop_progress(&pi.progress);
1814}
1815
1816static const char *format_time(timestamp_t time, const char *tz_str,
1817                               int show_raw_time)
1818{
1819        static struct strbuf time_buf = STRBUF_INIT;
1820
1821        strbuf_reset(&time_buf);
1822        if (show_raw_time) {
1823                strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
1824        }
1825        else {
1826                const char *time_str;
1827                size_t time_width;
1828                int tz;
1829                tz = atoi(tz_str);
1830                time_str = show_date(time, tz, &blame_date_mode);
1831                strbuf_addstr(&time_buf, time_str);
1832                /*
1833                 * Add space paddings to time_buf to display a fixed width
1834                 * string, and use time_width for display width calibration.
1835                 */
1836                for (time_width = utf8_strwidth(time_str);
1837                     time_width < blame_date_width;
1838                     time_width++)
1839                        strbuf_addch(&time_buf, ' ');
1840        }
1841        return time_buf.buf;
1842}
1843
1844#define OUTPUT_ANNOTATE_COMPAT  001
1845#define OUTPUT_LONG_OBJECT_NAME 002
1846#define OUTPUT_RAW_TIMESTAMP    004
1847#define OUTPUT_PORCELAIN        010
1848#define OUTPUT_SHOW_NAME        020
1849#define OUTPUT_SHOW_NUMBER      040
1850#define OUTPUT_SHOW_SCORE      0100
1851#define OUTPUT_NO_AUTHOR       0200
1852#define OUTPUT_SHOW_EMAIL       0400
1853#define OUTPUT_LINE_PORCELAIN 01000
1854
1855static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
1856{
1857        if (emit_one_suspect_detail(suspect, repeat) ||
1858            (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
1859                write_filename_info(suspect);
1860}
1861
1862static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
1863                           int opt)
1864{
1865        int repeat = opt & OUTPUT_LINE_PORCELAIN;
1866        int cnt;
1867        const char *cp;
1868        struct blame_origin *suspect = ent->suspect;
1869        char hex[GIT_MAX_HEXSZ + 1];
1870
1871        oid_to_hex_r(hex, &suspect->commit->object.oid);
1872        printf("%s %d %d %d\n",
1873               hex,
1874               ent->s_lno + 1,
1875               ent->lno + 1,
1876               ent->num_lines);
1877        emit_porcelain_details(suspect, repeat);
1878
1879        cp = blame_nth_line(sb, ent->lno);
1880        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1881                char ch;
1882                if (cnt) {
1883                        printf("%s %d %d\n", hex,
1884                               ent->s_lno + 1 + cnt,
1885                               ent->lno + 1 + cnt);
1886                        if (repeat)
1887                                emit_porcelain_details(suspect, 1);
1888                }
1889                putchar('\t');
1890                do {
1891                        ch = *cp++;
1892                        putchar(ch);
1893                } while (ch != '\n' &&
1894                         cp < sb->final_buf + sb->final_buf_size);
1895        }
1896
1897        if (sb->final_buf_size && cp[-1] != '\n')
1898                putchar('\n');
1899}
1900
1901static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
1902{
1903        int cnt;
1904        const char *cp;
1905        struct blame_origin *suspect = ent->suspect;
1906        struct commit_info ci;
1907        char hex[GIT_MAX_HEXSZ + 1];
1908        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1909
1910        get_commit_info(suspect->commit, &ci, 1);
1911        oid_to_hex_r(hex, &suspect->commit->object.oid);
1912
1913        cp = blame_nth_line(sb, ent->lno);
1914        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1915                char ch;
1916                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
1917
1918                if (suspect->commit->object.flags & UNINTERESTING) {
1919                        if (blank_boundary)
1920                                memset(hex, ' ', length);
1921                        else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
1922                                length--;
1923                                putchar('^');
1924                        }
1925                }
1926
1927                printf("%.*s", length, hex);
1928                if (opt & OUTPUT_ANNOTATE_COMPAT) {
1929                        const char *name;
1930                        if (opt & OUTPUT_SHOW_EMAIL)
1931                                name = ci.author_mail.buf;
1932                        else
1933                                name = ci.author.buf;
1934                        printf("\t(%10s\t%10s\t%d)", name,
1935                               format_time(ci.author_time, ci.author_tz.buf,
1936                                           show_raw_time),
1937                               ent->lno + 1 + cnt);
1938                } else {
1939                        if (opt & OUTPUT_SHOW_SCORE)
1940                                printf(" %*d %02d",
1941                                       max_score_digits, ent->score,
1942                                       ent->suspect->refcnt);
1943                        if (opt & OUTPUT_SHOW_NAME)
1944                                printf(" %-*.*s", longest_file, longest_file,
1945                                       suspect->path);
1946                        if (opt & OUTPUT_SHOW_NUMBER)
1947                                printf(" %*d", max_orig_digits,
1948                                       ent->s_lno + 1 + cnt);
1949
1950                        if (!(opt & OUTPUT_NO_AUTHOR)) {
1951                                const char *name;
1952                                int pad;
1953                                if (opt & OUTPUT_SHOW_EMAIL)
1954                                        name = ci.author_mail.buf;
1955                                else
1956                                        name = ci.author.buf;
1957                                pad = longest_author - utf8_strwidth(name);
1958                                printf(" (%s%*s %10s",
1959                                       name, pad, "",
1960                                       format_time(ci.author_time,
1961                                                   ci.author_tz.buf,
1962                                                   show_raw_time));
1963                        }
1964                        printf(" %*d) ",
1965                               max_digits, ent->lno + 1 + cnt);
1966                }
1967                do {
1968                        ch = *cp++;
1969                        putchar(ch);
1970                } while (ch != '\n' &&
1971                         cp < sb->final_buf + sb->final_buf_size);
1972        }
1973
1974        if (sb->final_buf_size && cp[-1] != '\n')
1975                putchar('\n');
1976
1977        commit_info_destroy(&ci);
1978}
1979
1980static void output(struct blame_scoreboard *sb, int option)
1981{
1982        struct blame_entry *ent;
1983
1984        if (option & OUTPUT_PORCELAIN) {
1985                for (ent = sb->ent; ent; ent = ent->next) {
1986                        int count = 0;
1987                        struct blame_origin *suspect;
1988                        struct commit *commit = ent->suspect->commit;
1989                        if (commit->object.flags & MORE_THAN_ONE_PATH)
1990                                continue;
1991                        for (suspect = commit->util; suspect; suspect = suspect->next) {
1992                                if (suspect->guilty && count++) {
1993                                        commit->object.flags |= MORE_THAN_ONE_PATH;
1994                                        break;
1995                                }
1996                        }
1997                }
1998        }
1999
2000        for (ent = sb->ent; ent; ent = ent->next) {
2001                if (option & OUTPUT_PORCELAIN)
2002                        emit_porcelain(sb, ent, option);
2003                else {
2004                        emit_other(sb, ent, option);
2005                }
2006        }
2007}
2008
2009static const char *get_next_line(const char *start, const char *end)
2010{
2011        const char *nl = memchr(start, '\n', end - start);
2012        return nl ? nl + 1 : end;
2013}
2014
2015/*
2016 * To allow quick access to the contents of nth line in the
2017 * final image, prepare an index in the scoreboard.
2018 */
2019static int prepare_lines(struct blame_scoreboard *sb)
2020{
2021        const char *buf = sb->final_buf;
2022        unsigned long len = sb->final_buf_size;
2023        const char *end = buf + len;
2024        const char *p;
2025        int *lineno;
2026        int num = 0;
2027
2028        for (p = buf; p < end; p = get_next_line(p, end))
2029                num++;
2030
2031        ALLOC_ARRAY(sb->lineno, num + 1);
2032        lineno = sb->lineno;
2033
2034        for (p = buf; p < end; p = get_next_line(p, end))
2035                *lineno++ = p - buf;
2036
2037        *lineno = len;
2038
2039        sb->num_lines = num;
2040        return sb->num_lines;
2041}
2042
2043/*
2044 * Add phony grafts for use with -S; this is primarily to
2045 * support git's cvsserver that wants to give a linear history
2046 * to its clients.
2047 */
2048static int read_ancestry(const char *graft_file)
2049{
2050        FILE *fp = fopen(graft_file, "r");
2051        struct strbuf buf = STRBUF_INIT;
2052        if (!fp)
2053                return -1;
2054        while (!strbuf_getwholeline(&buf, fp, '\n')) {
2055                /* The format is just "Commit Parent1 Parent2 ...\n" */
2056                struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
2057                if (graft)
2058                        register_commit_graft(graft, 0);
2059        }
2060        fclose(fp);
2061        strbuf_release(&buf);
2062        return 0;
2063}
2064
2065static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
2066{
2067        const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
2068                                              auto_abbrev);
2069        int len = strlen(uniq);
2070        if (auto_abbrev < len)
2071                return len;
2072        return auto_abbrev;
2073}
2074
2075/*
2076 * How many columns do we need to show line numbers, authors,
2077 * and filenames?
2078 */
2079static void find_alignment(struct blame_scoreboard *sb, int *option)
2080{
2081        int longest_src_lines = 0;
2082        int longest_dst_lines = 0;
2083        unsigned largest_score = 0;
2084        struct blame_entry *e;
2085        int compute_auto_abbrev = (abbrev < 0);
2086        int auto_abbrev = DEFAULT_ABBREV;
2087
2088        for (e = sb->ent; e; e = e->next) {
2089                struct blame_origin *suspect = e->suspect;
2090                int num;
2091
2092                if (compute_auto_abbrev)
2093                        auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
2094                if (strcmp(suspect->path, sb->path))
2095                        *option |= OUTPUT_SHOW_NAME;
2096                num = strlen(suspect->path);
2097                if (longest_file < num)
2098                        longest_file = num;
2099                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
2100                        struct commit_info ci;
2101                        suspect->commit->object.flags |= METAINFO_SHOWN;
2102                        get_commit_info(suspect->commit, &ci, 1);
2103                        if (*option & OUTPUT_SHOW_EMAIL)
2104                                num = utf8_strwidth(ci.author_mail.buf);
2105                        else
2106                                num = utf8_strwidth(ci.author.buf);
2107                        if (longest_author < num)
2108                                longest_author = num;
2109                        commit_info_destroy(&ci);
2110                }
2111                num = e->s_lno + e->num_lines;
2112                if (longest_src_lines < num)
2113                        longest_src_lines = num;
2114                num = e->lno + e->num_lines;
2115                if (longest_dst_lines < num)
2116                        longest_dst_lines = num;
2117                if (largest_score < blame_entry_score(sb, e))
2118                        largest_score = blame_entry_score(sb, e);
2119        }
2120        max_orig_digits = decimal_width(longest_src_lines);
2121        max_digits = decimal_width(longest_dst_lines);
2122        max_score_digits = decimal_width(largest_score);
2123
2124        if (compute_auto_abbrev)
2125                /* one more abbrev length is needed for the boundary commit */
2126                abbrev = auto_abbrev + 1;
2127}
2128
2129/*
2130 * For debugging -- origin is refcounted, and this asserts that
2131 * we do not underflow.
2132 */
2133static void sanity_check_refcnt(struct blame_scoreboard *sb)
2134{
2135        int baa = 0;
2136        struct blame_entry *ent;
2137
2138        for (ent = sb->ent; ent; ent = ent->next) {
2139                /* Nobody should have zero or negative refcnt */
2140                if (ent->suspect->refcnt <= 0) {
2141                        fprintf(stderr, "%s in %s has negative refcnt %d\n",
2142                                ent->suspect->path,
2143                                oid_to_hex(&ent->suspect->commit->object.oid),
2144                                ent->suspect->refcnt);
2145                        baa = 1;
2146                }
2147        }
2148        if (baa) {
2149                int opt = 0160;
2150                find_alignment(sb, &opt);
2151                output(sb, opt);
2152                die("Baa %d!", baa);
2153        }
2154}
2155
2156static unsigned parse_score(const char *arg)
2157{
2158        char *end;
2159        unsigned long score = strtoul(arg, &end, 10);
2160        if (*end)
2161                return 0;
2162        return score;
2163}
2164
2165static const char *add_prefix(const char *prefix, const char *path)
2166{
2167        return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
2168}
2169
2170static int git_blame_config(const char *var, const char *value, void *cb)
2171{
2172        if (!strcmp(var, "blame.showroot")) {
2173                show_root = git_config_bool(var, value);
2174                return 0;
2175        }
2176        if (!strcmp(var, "blame.blankboundary")) {
2177                blank_boundary = git_config_bool(var, value);
2178                return 0;
2179        }
2180        if (!strcmp(var, "blame.showemail")) {
2181                int *output_option = cb;
2182                if (git_config_bool(var, value))
2183                        *output_option |= OUTPUT_SHOW_EMAIL;
2184                else
2185                        *output_option &= ~OUTPUT_SHOW_EMAIL;
2186                return 0;
2187        }
2188        if (!strcmp(var, "blame.date")) {
2189                if (!value)
2190                        return config_error_nonbool(var);
2191                parse_date_format(value, &blame_date_mode);
2192                return 0;
2193        }
2194
2195        if (git_diff_heuristic_config(var, value, cb) < 0)
2196                return -1;
2197        if (userdiff_config(var, value) < 0)
2198                return -1;
2199
2200        return git_default_config(var, value, cb);
2201}
2202
2203static void verify_working_tree_path(struct commit *work_tree, const char *path)
2204{
2205        struct commit_list *parents;
2206        int pos;
2207
2208        for (parents = work_tree->parents; parents; parents = parents->next) {
2209                const struct object_id *commit_oid = &parents->item->object.oid;
2210                struct object_id blob_oid;
2211                unsigned mode;
2212
2213                if (!get_tree_entry(commit_oid->hash, path, blob_oid.hash, &mode) &&
2214                    sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
2215                        return;
2216        }
2217
2218        pos = cache_name_pos(path, strlen(path));
2219        if (pos >= 0)
2220                ; /* path is in the index */
2221        else if (-1 - pos < active_nr &&
2222                 !strcmp(active_cache[-1 - pos]->name, path))
2223                ; /* path is in the index, unmerged */
2224        else
2225                die("no such path '%s' in HEAD", path);
2226}
2227
2228static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid)
2229{
2230        struct commit *parent;
2231
2232        parent = lookup_commit_reference(oid->hash);
2233        if (!parent)
2234                die("no such commit %s", oid_to_hex(oid));
2235        return &commit_list_insert(parent, tail)->next;
2236}
2237
2238static void append_merge_parents(struct commit_list **tail)
2239{
2240        int merge_head;
2241        struct strbuf line = STRBUF_INIT;
2242
2243        merge_head = open(git_path_merge_head(), O_RDONLY);
2244        if (merge_head < 0) {
2245                if (errno == ENOENT)
2246                        return;
2247                die("cannot open '%s' for reading", git_path_merge_head());
2248        }
2249
2250        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
2251                struct object_id oid;
2252                if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
2253                        die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
2254                tail = append_parent(tail, &oid);
2255        }
2256        close(merge_head);
2257        strbuf_release(&line);
2258}
2259
2260/*
2261 * This isn't as simple as passing sb->buf and sb->len, because we
2262 * want to transfer ownership of the buffer to the commit (so we
2263 * must use detach).
2264 */
2265static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
2266{
2267        size_t len;
2268        void *buf = strbuf_detach(sb, &len);
2269        set_commit_buffer(c, buf, len);
2270}
2271
2272/*
2273 * Prepare a dummy commit that represents the work tree (or staged) item.
2274 * Note that annotating work tree item never works in the reverse.
2275 */
2276static struct commit *fake_working_tree_commit(struct diff_options *opt,
2277                                               const char *path,
2278                                               const char *contents_from)
2279{
2280        struct commit *commit;
2281        struct blame_origin *origin;
2282        struct commit_list **parent_tail, *parent;
2283        struct object_id head_oid;
2284        struct strbuf buf = STRBUF_INIT;
2285        const char *ident;
2286        time_t now;
2287        int size, len;
2288        struct cache_entry *ce;
2289        unsigned mode;
2290        struct strbuf msg = STRBUF_INIT;
2291
2292        read_cache();
2293        time(&now);
2294        commit = alloc_commit_node();
2295        commit->object.parsed = 1;
2296        commit->date = now;
2297        parent_tail = &commit->parents;
2298
2299        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL))
2300                die("no such ref: HEAD");
2301
2302        parent_tail = append_parent(parent_tail, &head_oid);
2303        append_merge_parents(parent_tail);
2304        verify_working_tree_path(commit, path);
2305
2306        origin = make_origin(commit, path);
2307
2308        ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2309        strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
2310        for (parent = commit->parents; parent; parent = parent->next)
2311                strbuf_addf(&msg, "parent %s\n",
2312                            oid_to_hex(&parent->item->object.oid));
2313        strbuf_addf(&msg,
2314                    "author %s\n"
2315                    "committer %s\n\n"
2316                    "Version of %s from %s\n",
2317                    ident, ident, path,
2318                    (!contents_from ? path :
2319                     (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
2320        set_commit_buffer_from_strbuf(commit, &msg);
2321
2322        if (!contents_from || strcmp("-", contents_from)) {
2323                struct stat st;
2324                const char *read_from;
2325                char *buf_ptr;
2326                unsigned long buf_len;
2327
2328                if (contents_from) {
2329                        if (stat(contents_from, &st) < 0)
2330                                die_errno("Cannot stat '%s'", contents_from);
2331                        read_from = contents_from;
2332                }
2333                else {
2334                        if (lstat(path, &st) < 0)
2335                                die_errno("Cannot lstat '%s'", path);
2336                        read_from = path;
2337                }
2338                mode = canon_mode(st.st_mode);
2339
2340                switch (st.st_mode & S_IFMT) {
2341                case S_IFREG:
2342                        if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
2343                            textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
2344                                strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
2345                        else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
2346                                die_errno("cannot open or read '%s'", read_from);
2347                        break;
2348                case S_IFLNK:
2349                        if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
2350                                die_errno("cannot readlink '%s'", read_from);
2351                        break;
2352                default:
2353                        die("unsupported file type %s", read_from);
2354                }
2355        }
2356        else {
2357                /* Reading from stdin */
2358                mode = 0;
2359                if (strbuf_read(&buf, 0, 0) < 0)
2360                        die_errno("failed to read from stdin");
2361        }
2362        convert_to_git(path, buf.buf, buf.len, &buf, 0);
2363        origin->file.ptr = buf.buf;
2364        origin->file.size = buf.len;
2365        pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);
2366
2367        /*
2368         * Read the current index, replace the path entry with
2369         * origin->blob_sha1 without mucking with its mode or type
2370         * bits; we are not going to write this index out -- we just
2371         * want to run "diff-index --cached".
2372         */
2373        discard_cache();
2374        read_cache();
2375
2376        len = strlen(path);
2377        if (!mode) {
2378                int pos = cache_name_pos(path, len);
2379                if (0 <= pos)
2380                        mode = active_cache[pos]->ce_mode;
2381                else
2382                        /* Let's not bother reading from HEAD tree */
2383                        mode = S_IFREG | 0644;
2384        }
2385        size = cache_entry_size(len);
2386        ce = xcalloc(1, size);
2387        oidcpy(&ce->oid, &origin->blob_oid);
2388        memcpy(ce->name, path, len);
2389        ce->ce_flags = create_ce_flags(0);
2390        ce->ce_namelen = len;
2391        ce->ce_mode = create_ce_mode(mode);
2392        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2393
2394        cache_tree_invalidate_path(&the_index, path);
2395
2396        return commit;
2397}
2398
2399static struct commit *find_single_final(struct rev_info *revs,
2400                                        const char **name_p)
2401{
2402        int i;
2403        struct commit *found = NULL;
2404        const char *name = NULL;
2405
2406        for (i = 0; i < revs->pending.nr; i++) {
2407                struct object *obj = revs->pending.objects[i].item;
2408                if (obj->flags & UNINTERESTING)
2409                        continue;
2410                obj = deref_tag(obj, NULL, 0);
2411                if (obj->type != OBJ_COMMIT)
2412                        die("Non commit %s?", revs->pending.objects[i].name);
2413                if (found)
2414                        die("More than one commit to dig from %s and %s?",
2415                            revs->pending.objects[i].name, name);
2416                found = (struct commit *)obj;
2417                name = revs->pending.objects[i].name;
2418        }
2419        if (name_p)
2420                *name_p = name;
2421        return found;
2422}
2423
2424static char *prepare_final(struct blame_scoreboard *sb)
2425{
2426        const char *name;
2427        sb->final = find_single_final(sb->revs, &name);
2428        return xstrdup_or_null(name);
2429}
2430
2431static const char *dwim_reverse_initial(struct blame_scoreboard *sb)
2432{
2433        /*
2434         * DWIM "git blame --reverse ONE -- PATH" as
2435         * "git blame --reverse ONE..HEAD -- PATH" but only do so
2436         * when it makes sense.
2437         */
2438        struct object *obj;
2439        struct commit *head_commit;
2440        unsigned char head_sha1[20];
2441
2442        if (sb->revs->pending.nr != 1)
2443                return NULL;
2444
2445        /* Is that sole rev a committish? */
2446        obj = sb->revs->pending.objects[0].item;
2447        obj = deref_tag(obj, NULL, 0);
2448        if (obj->type != OBJ_COMMIT)
2449                return NULL;
2450
2451        /* Do we have HEAD? */
2452        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
2453                return NULL;
2454        head_commit = lookup_commit_reference_gently(head_sha1, 1);
2455        if (!head_commit)
2456                return NULL;
2457
2458        /* Turn "ONE" into "ONE..HEAD" then */
2459        obj->flags |= UNINTERESTING;
2460        add_pending_object(sb->revs, &head_commit->object, "HEAD");
2461
2462        sb->final = (struct commit *)obj;
2463        return sb->revs->pending.objects[0].name;
2464}
2465
2466static char *prepare_initial(struct blame_scoreboard *sb)
2467{
2468        int i;
2469        const char *final_commit_name = NULL;
2470        struct rev_info *revs = sb->revs;
2471
2472        /*
2473         * There must be one and only one negative commit, and it must be
2474         * the boundary.
2475         */
2476        for (i = 0; i < revs->pending.nr; i++) {
2477                struct object *obj = revs->pending.objects[i].item;
2478                if (!(obj->flags & UNINTERESTING))
2479                        continue;
2480                obj = deref_tag(obj, NULL, 0);
2481                if (obj->type != OBJ_COMMIT)
2482                        die("Non commit %s?", revs->pending.objects[i].name);
2483                if (sb->final)
2484                        die("More than one commit to dig up from, %s and %s?",
2485                            revs->pending.objects[i].name,
2486                            final_commit_name);
2487                sb->final = (struct commit *) obj;
2488                final_commit_name = revs->pending.objects[i].name;
2489        }
2490
2491        if (!final_commit_name)
2492                final_commit_name = dwim_reverse_initial(sb);
2493        if (!final_commit_name)
2494                die("No commit to dig up from?");
2495        return xstrdup(final_commit_name);
2496}
2497
2498static int blame_copy_callback(const struct option *option, const char *arg, int unset)
2499{
2500        int *opt = option->value;
2501
2502        /*
2503         * -C enables copy from removed files;
2504         * -C -C enables copy from existing files, but only
2505         *       when blaming a new file;
2506         * -C -C -C enables copy from existing files for
2507         *          everybody
2508         */
2509        if (*opt & PICKAXE_BLAME_COPY_HARDER)
2510                *opt |= PICKAXE_BLAME_COPY_HARDEST;
2511        if (*opt & PICKAXE_BLAME_COPY)
2512                *opt |= PICKAXE_BLAME_COPY_HARDER;
2513        *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2514
2515        if (arg)
2516                blame_copy_score = parse_score(arg);
2517        return 0;
2518}
2519
2520static int blame_move_callback(const struct option *option, const char *arg, int unset)
2521{
2522        int *opt = option->value;
2523
2524        *opt |= PICKAXE_BLAME_MOVE;
2525
2526        if (arg)
2527                blame_move_score = parse_score(arg);
2528        return 0;
2529}
2530
2531int cmd_blame(int argc, const char **argv, const char *prefix)
2532{
2533        struct rev_info revs;
2534        const char *path;
2535        struct blame_scoreboard sb;
2536        struct blame_origin *o;
2537        struct blame_entry *ent = NULL;
2538        long dashdash_pos, lno;
2539        char *final_commit_name = NULL;
2540        enum object_type type;
2541        struct commit *final_commit = NULL;
2542
2543        struct string_list range_list = STRING_LIST_INIT_NODUP;
2544        int output_option = 0, opt = 0;
2545        int show_stats = 0;
2546        const char *revs_file = NULL;
2547        const char *contents_from = NULL;
2548        const struct option options[] = {
2549                OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
2550                OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2551                OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
2552                OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
2553                OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
2554                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
2555                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
2556                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
2557                OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
2558                OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
2559                OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
2560                OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
2561                OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
2562                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
2563                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
2564                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
2565
2566                /*
2567                 * The following two options are parsed by parse_revision_opt()
2568                 * and are only included here to get included in the "-h"
2569                 * output:
2570                 */
2571                { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
2572
2573                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
2574                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2575                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
2576                { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
2577                { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
2578                OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2579                OPT__ABBREV(&abbrev),
2580                OPT_END()
2581        };
2582
2583        struct parse_opt_ctx_t ctx;
2584        int cmd_is_annotate = !strcmp(argv[0], "annotate");
2585        struct range_set ranges;
2586        unsigned int range_i;
2587        long anchor;
2588
2589        git_config(git_blame_config, &output_option);
2590        init_revisions(&revs, NULL);
2591        revs.date_mode = blame_date_mode;
2592        DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
2593        DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
2594
2595        save_commit_buffer = 0;
2596        dashdash_pos = 0;
2597        show_progress = -1;
2598
2599        parse_options_start(&ctx, argc, argv, prefix, options,
2600                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
2601        for (;;) {
2602                switch (parse_options_step(&ctx, options, blame_opt_usage)) {
2603                case PARSE_OPT_HELP:
2604                        exit(129);
2605                case PARSE_OPT_DONE:
2606                        if (ctx.argv[0])
2607                                dashdash_pos = ctx.cpidx;
2608                        goto parse_done;
2609                }
2610
2611                if (!strcmp(ctx.argv[0], "--reverse")) {
2612                        ctx.argv[0] = "--children";
2613                        reverse = 1;
2614                }
2615                parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
2616        }
2617parse_done:
2618        no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
2619        xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
2620        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
2621        argc = parse_options_end(&ctx);
2622
2623        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
2624                if (show_progress > 0)
2625                        die(_("--progress can't be used with --incremental or porcelain formats"));
2626                show_progress = 0;
2627        } else if (show_progress < 0)
2628                show_progress = isatty(2);
2629
2630        if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
2631                /* one more abbrev length is needed for the boundary commit */
2632                abbrev++;
2633        else if (!abbrev)
2634                abbrev = GIT_SHA1_HEXSZ;
2635
2636        if (revs_file && read_ancestry(revs_file))
2637                die_errno("reading graft file '%s' failed", revs_file);
2638
2639        if (cmd_is_annotate) {
2640                output_option |= OUTPUT_ANNOTATE_COMPAT;
2641                blame_date_mode.type = DATE_ISO8601;
2642        } else {
2643                blame_date_mode = revs.date_mode;
2644        }
2645
2646        /* The maximum width used to show the dates */
2647        switch (blame_date_mode.type) {
2648        case DATE_RFC2822:
2649                blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2650                break;
2651        case DATE_ISO8601_STRICT:
2652                blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
2653                break;
2654        case DATE_ISO8601:
2655                blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2656                break;
2657        case DATE_RAW:
2658                blame_date_width = sizeof("1161298804 -0700");
2659                break;
2660        case DATE_UNIX:
2661                blame_date_width = sizeof("1161298804");
2662                break;
2663        case DATE_SHORT:
2664                blame_date_width = sizeof("2006-10-19");
2665                break;
2666        case DATE_RELATIVE:
2667                /* TRANSLATORS: This string is used to tell us the maximum
2668                   display width for a relative timestamp in "git blame"
2669                   output.  For C locale, "4 years, 11 months ago", which
2670                   takes 22 places, is the longest among various forms of
2671                   relative timestamps, but your language may need more or
2672                   fewer display columns. */
2673                blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2674                break;
2675        case DATE_NORMAL:
2676                blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2677                break;
2678        case DATE_STRFTIME:
2679                blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2680                break;
2681        }
2682        blame_date_width -= 1; /* strip the null */
2683
2684        if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
2685                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
2686                        PICKAXE_BLAME_COPY_HARDER);
2687
2688        /*
2689         * We have collected options unknown to us in argv[1..unk]
2690         * which are to be passed to revision machinery if we are
2691         * going to do the "bottom" processing.
2692         *
2693         * The remaining are:
2694         *
2695         * (1) if dashdash_pos != 0, it is either
2696         *     "blame [revisions] -- <path>" or
2697         *     "blame -- <path> <rev>"
2698         *
2699         * (2) otherwise, it is one of the two:
2700         *     "blame [revisions] <path>"
2701         *     "blame <path> <rev>"
2702         *
2703         * Note that we must strip out <path> from the arguments: we do not
2704         * want the path pruning but we may want "bottom" processing.
2705         */
2706        if (dashdash_pos) {
2707                switch (argc - dashdash_pos - 1) {
2708                case 2: /* (1b) */
2709                        if (argc != 4)
2710                                usage_with_options(blame_opt_usage, options);
2711                        /* reorder for the new way: <rev> -- <path> */
2712                        argv[1] = argv[3];
2713                        argv[3] = argv[2];
2714                        argv[2] = "--";
2715                        /* FALLTHROUGH */
2716                case 1: /* (1a) */
2717                        path = add_prefix(prefix, argv[--argc]);
2718                        argv[argc] = NULL;
2719                        break;
2720                default:
2721                        usage_with_options(blame_opt_usage, options);
2722                }
2723        } else {
2724                if (argc < 2)
2725                        usage_with_options(blame_opt_usage, options);
2726                path = add_prefix(prefix, argv[argc - 1]);
2727                if (argc == 3 && !file_exists(path)) { /* (2b) */
2728                        path = add_prefix(prefix, argv[1]);
2729                        argv[1] = argv[2];
2730                }
2731                argv[argc - 1] = "--";
2732
2733                setup_work_tree();
2734                if (!file_exists(path))
2735                        die_errno("cannot stat path '%s'", path);
2736        }
2737
2738        revs.disable_stdin = 1;
2739        setup_revisions(argc, argv, &revs, NULL);
2740        memset(&sb, 0, sizeof(sb));
2741        sb.move_score = BLAME_DEFAULT_MOVE_SCORE;
2742        sb.copy_score = BLAME_DEFAULT_COPY_SCORE;
2743
2744        sb.revs = &revs;
2745        sb.contents_from = contents_from;
2746        sb.reverse = reverse;
2747        if (!reverse) {
2748                final_commit_name = prepare_final(&sb);
2749                sb.commits.compare = compare_commits_by_commit_date;
2750        }
2751        else if (contents_from)
2752                die(_("--contents and --reverse do not blend well."));
2753        else {
2754                final_commit_name = prepare_initial(&sb);
2755                sb.commits.compare = compare_commits_by_reverse_commit_date;
2756                if (revs.first_parent_only)
2757                        revs.children.name = NULL;
2758        }
2759
2760        if (!sb.final) {
2761                /*
2762                 * "--not A B -- path" without anything positive;
2763                 * do not default to HEAD, but use the working tree
2764                 * or "--contents".
2765                 */
2766                setup_work_tree();
2767                sb.final = fake_working_tree_commit(&sb.revs->diffopt,
2768                                                    path, contents_from);
2769                add_pending_object(&revs, &(sb.final->object), ":");
2770        }
2771        else if (contents_from)
2772                die(_("cannot use --contents with final commit object name"));
2773
2774        if (reverse && revs.first_parent_only) {
2775                final_commit = find_single_final(sb.revs, NULL);
2776                if (!final_commit)
2777                        die(_("--reverse and --first-parent together require specified latest commit"));
2778        }
2779
2780        /*
2781         * If we have bottom, this will mark the ancestors of the
2782         * bottom commits we would reach while traversing as
2783         * uninteresting.
2784         */
2785        if (prepare_revision_walk(&revs))
2786                die(_("revision walk setup failed"));
2787
2788        if (reverse && revs.first_parent_only) {
2789                struct commit *c = final_commit;
2790
2791                sb.revs->children.name = "children";
2792                while (c->parents &&
2793                       oidcmp(&c->object.oid, &sb.final->object.oid)) {
2794                        struct commit_list *l = xcalloc(1, sizeof(*l));
2795
2796                        l->item = c;
2797                        if (add_decoration(&sb.revs->children,
2798                                           &c->parents->item->object, l))
2799                                die("BUG: not unique item in first-parent chain");
2800                        c = c->parents->item;
2801                }
2802
2803                if (oidcmp(&c->object.oid, &sb.final->object.oid))
2804                        die(_("--reverse --first-parent together require range along first-parent chain"));
2805        }
2806
2807        if (is_null_oid(&sb.final->object.oid)) {
2808                o = sb.final->util;
2809                sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
2810                sb.final_buf_size = o->file.size;
2811        }
2812        else {
2813                o = get_origin(sb.final, path);
2814                if (fill_blob_sha1_and_mode(o))
2815                        die(_("no such path %s in %s"), path, final_commit_name);
2816
2817                if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
2818                    textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb.final_buf,
2819                                    &sb.final_buf_size))
2820                        ;
2821                else
2822                        sb.final_buf = read_sha1_file(o->blob_oid.hash, &type,
2823                                                      &sb.final_buf_size);
2824
2825                if (!sb.final_buf)
2826                        die(_("cannot read blob %s for path %s"),
2827                            oid_to_hex(&o->blob_oid),
2828                            path);
2829        }
2830        sb.num_read_blob++;
2831        lno = prepare_lines(&sb);
2832
2833        if (lno && !range_list.nr)
2834                string_list_append(&range_list, "1");
2835
2836        anchor = 1;
2837        range_set_init(&ranges, range_list.nr);
2838        for (range_i = 0; range_i < range_list.nr; ++range_i) {
2839                long bottom, top;
2840                if (parse_range_arg(range_list.items[range_i].string,
2841                                    nth_line_cb, &sb, lno, anchor,
2842                                    &bottom, &top, sb.path))
2843                        usage(blame_usage);
2844                if (lno < top || ((lno || bottom) && lno < bottom))
2845                        die(Q_("file %s has only %lu line",
2846                               "file %s has only %lu lines",
2847                               lno), path, lno);
2848                if (bottom < 1)
2849                        bottom = 1;
2850                if (top < 1)
2851                        top = lno;
2852                bottom--;
2853                range_set_append_unsafe(&ranges, bottom, top);
2854                anchor = top + 1;
2855        }
2856        sort_and_merge_range_set(&ranges);
2857
2858        for (range_i = ranges.nr; range_i > 0; --range_i) {
2859                const struct range *r = &ranges.ranges[range_i - 1];
2860                long bottom = r->start;
2861                long top = r->end;
2862                struct blame_entry *next = ent;
2863                ent = xcalloc(1, sizeof(*ent));
2864                ent->lno = bottom;
2865                ent->num_lines = top - bottom;
2866                ent->suspect = o;
2867                ent->s_lno = bottom;
2868                ent->next = next;
2869                blame_origin_incref(o);
2870        }
2871
2872        o->suspects = ent;
2873        prio_queue_put(&sb.commits, o->commit);
2874
2875        blame_origin_decref(o);
2876
2877        range_set_release(&ranges);
2878        string_list_clear(&range_list, 0);
2879
2880        sb.ent = NULL;
2881        sb.path = path;
2882
2883        if (blame_move_score)
2884                sb.move_score = blame_move_score;
2885        if (blame_copy_score)
2886                sb.copy_score = blame_copy_score;
2887
2888        read_mailmap(&mailmap, NULL);
2889
2890        assign_blame(&sb, opt);
2891
2892        if (!incremental)
2893                setup_pager();
2894
2895        free(final_commit_name);
2896
2897        if (incremental)
2898                return 0;
2899
2900        sb.ent = blame_sort(sb.ent, compare_blame_final);
2901
2902        blame_coalesce(&sb);
2903
2904        if (!(output_option & OUTPUT_PORCELAIN))
2905                find_alignment(&sb, &output_option);
2906
2907        output(&sb, output_option);
2908        free((void *)sb.final_buf);
2909        for (ent = sb.ent; ent; ) {
2910                struct blame_entry *e = ent->next;
2911                free(ent);
2912                ent = e;
2913        }
2914
2915        if (show_stats) {
2916                printf("num read blob: %d\n", sb.num_read_blob);
2917                printf("num get patch: %d\n", sb.num_get_patch);
2918                printf("num commits: %d\n", sb.num_commits);
2919        }
2920        return 0;
2921}