blame.con commit Merge branch 'nd/commit-util-to-slab' (b3b2aaf)
   1#include "cache.h"
   2#include "refs.h"
   3#include "cache-tree.h"
   4#include "mergesort.h"
   5#include "diff.h"
   6#include "diffcore.h"
   7#include "tag.h"
   8#include "blame.h"
   9#include "commit-slab.h"
  10
  11define_commit_slab(blame_suspects, struct blame_origin *);
  12static struct blame_suspects blame_suspects;
  13
  14struct blame_origin *get_blame_suspects(struct commit *commit)
  15{
  16        struct blame_origin **result;
  17
  18        result = blame_suspects_peek(&blame_suspects, commit);
  19
  20        return result ? *result : NULL;
  21}
  22
  23static void set_blame_suspects(struct commit *commit, struct blame_origin *origin)
  24{
  25        *blame_suspects_at(&blame_suspects, commit) = origin;
  26}
  27
  28void blame_origin_decref(struct blame_origin *o)
  29{
  30        if (o && --o->refcnt <= 0) {
  31                struct blame_origin *p, *l = NULL;
  32                if (o->previous)
  33                        blame_origin_decref(o->previous);
  34                free(o->file.ptr);
  35                /* Should be present exactly once in commit chain */
  36                for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) {
  37                        if (p == o) {
  38                                if (l)
  39                                        l->next = p->next;
  40                                else
  41                                        set_blame_suspects(o->commit, p->next);
  42                                free(o);
  43                                return;
  44                        }
  45                }
  46                die("internal error in blame_origin_decref");
  47        }
  48}
  49
  50/*
  51 * Given a commit and a path in it, create a new origin structure.
  52 * The callers that add blame to the scoreboard should use
  53 * get_origin() to obtain shared, refcounted copy instead of calling
  54 * this function directly.
  55 */
  56static struct blame_origin *make_origin(struct commit *commit, const char *path)
  57{
  58        struct blame_origin *o;
  59        FLEX_ALLOC_STR(o, path, path);
  60        o->commit = commit;
  61        o->refcnt = 1;
  62        o->next = get_blame_suspects(commit);
  63        set_blame_suspects(commit, o);
  64        return o;
  65}
  66
  67/*
  68 * Locate an existing origin or create a new one.
  69 * This moves the origin to front position in the commit util list.
  70 */
  71static struct blame_origin *get_origin(struct commit *commit, const char *path)
  72{
  73        struct blame_origin *o, *l;
  74
  75        for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) {
  76                if (!strcmp(o->path, path)) {
  77                        /* bump to front */
  78                        if (l) {
  79                                l->next = o->next;
  80                                o->next = get_blame_suspects(commit);
  81                                set_blame_suspects(commit, o);
  82                        }
  83                        return blame_origin_incref(o);
  84                }
  85        }
  86        return make_origin(commit, path);
  87}
  88
  89
  90
  91static void verify_working_tree_path(struct commit *work_tree, const char *path)
  92{
  93        struct commit_list *parents;
  94        int pos;
  95
  96        for (parents = work_tree->parents; parents; parents = parents->next) {
  97                const struct object_id *commit_oid = &parents->item->object.oid;
  98                struct object_id blob_oid;
  99                unsigned mode;
 100
 101                if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
 102                    oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
 103                        return;
 104        }
 105
 106        pos = cache_name_pos(path, strlen(path));
 107        if (pos >= 0)
 108                ; /* path is in the index */
 109        else if (-1 - pos < active_nr &&
 110                 !strcmp(active_cache[-1 - pos]->name, path))
 111                ; /* path is in the index, unmerged */
 112        else
 113                die("no such path '%s' in HEAD", path);
 114}
 115
 116static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid)
 117{
 118        struct commit *parent;
 119
 120        parent = lookup_commit_reference(oid);
 121        if (!parent)
 122                die("no such commit %s", oid_to_hex(oid));
 123        return &commit_list_insert(parent, tail)->next;
 124}
 125
 126static void append_merge_parents(struct commit_list **tail)
 127{
 128        int merge_head;
 129        struct strbuf line = STRBUF_INIT;
 130
 131        merge_head = open(git_path_merge_head(), O_RDONLY);
 132        if (merge_head < 0) {
 133                if (errno == ENOENT)
 134                        return;
 135                die("cannot open '%s' for reading", git_path_merge_head());
 136        }
 137
 138        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
 139                struct object_id oid;
 140                if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
 141                        die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
 142                tail = append_parent(tail, &oid);
 143        }
 144        close(merge_head);
 145        strbuf_release(&line);
 146}
 147
 148/*
 149 * This isn't as simple as passing sb->buf and sb->len, because we
 150 * want to transfer ownership of the buffer to the commit (so we
 151 * must use detach).
 152 */
 153static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
 154{
 155        size_t len;
 156        void *buf = strbuf_detach(sb, &len);
 157        set_commit_buffer(c, buf, len);
 158}
 159
 160/*
 161 * Prepare a dummy commit that represents the work tree (or staged) item.
 162 * Note that annotating work tree item never works in the reverse.
 163 */
 164static struct commit *fake_working_tree_commit(struct diff_options *opt,
 165                                               const char *path,
 166                                               const char *contents_from)
 167{
 168        struct commit *commit;
 169        struct blame_origin *origin;
 170        struct commit_list **parent_tail, *parent;
 171        struct object_id head_oid;
 172        struct strbuf buf = STRBUF_INIT;
 173        const char *ident;
 174        time_t now;
 175        int size, len;
 176        struct cache_entry *ce;
 177        unsigned mode;
 178        struct strbuf msg = STRBUF_INIT;
 179
 180        read_cache();
 181        time(&now);
 182        commit = alloc_commit_node();
 183        commit->object.parsed = 1;
 184        commit->date = now;
 185        parent_tail = &commit->parents;
 186
 187        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
 188                die("no such ref: HEAD");
 189
 190        parent_tail = append_parent(parent_tail, &head_oid);
 191        append_merge_parents(parent_tail);
 192        verify_working_tree_path(commit, path);
 193
 194        origin = make_origin(commit, path);
 195
 196        ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
 197        strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
 198        for (parent = commit->parents; parent; parent = parent->next)
 199                strbuf_addf(&msg, "parent %s\n",
 200                            oid_to_hex(&parent->item->object.oid));
 201        strbuf_addf(&msg,
 202                    "author %s\n"
 203                    "committer %s\n\n"
 204                    "Version of %s from %s\n",
 205                    ident, ident, path,
 206                    (!contents_from ? path :
 207                     (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
 208        set_commit_buffer_from_strbuf(commit, &msg);
 209
 210        if (!contents_from || strcmp("-", contents_from)) {
 211                struct stat st;
 212                const char *read_from;
 213                char *buf_ptr;
 214                unsigned long buf_len;
 215
 216                if (contents_from) {
 217                        if (stat(contents_from, &st) < 0)
 218                                die_errno("Cannot stat '%s'", contents_from);
 219                        read_from = contents_from;
 220                }
 221                else {
 222                        if (lstat(path, &st) < 0)
 223                                die_errno("Cannot lstat '%s'", path);
 224                        read_from = path;
 225                }
 226                mode = canon_mode(st.st_mode);
 227
 228                switch (st.st_mode & S_IFMT) {
 229                case S_IFREG:
 230                        if (opt->flags.allow_textconv &&
 231                            textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
 232                                strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
 233                        else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
 234                                die_errno("cannot open or read '%s'", read_from);
 235                        break;
 236                case S_IFLNK:
 237                        if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
 238                                die_errno("cannot readlink '%s'", read_from);
 239                        break;
 240                default:
 241                        die("unsupported file type %s", read_from);
 242                }
 243        }
 244        else {
 245                /* Reading from stdin */
 246                mode = 0;
 247                if (strbuf_read(&buf, 0, 0) < 0)
 248                        die_errno("failed to read from stdin");
 249        }
 250        convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
 251        origin->file.ptr = buf.buf;
 252        origin->file.size = buf.len;
 253        pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
 254
 255        /*
 256         * Read the current index, replace the path entry with
 257         * origin->blob_sha1 without mucking with its mode or type
 258         * bits; we are not going to write this index out -- we just
 259         * want to run "diff-index --cached".
 260         */
 261        discard_cache();
 262        read_cache();
 263
 264        len = strlen(path);
 265        if (!mode) {
 266                int pos = cache_name_pos(path, len);
 267                if (0 <= pos)
 268                        mode = active_cache[pos]->ce_mode;
 269                else
 270                        /* Let's not bother reading from HEAD tree */
 271                        mode = S_IFREG | 0644;
 272        }
 273        size = cache_entry_size(len);
 274        ce = xcalloc(1, size);
 275        oidcpy(&ce->oid, &origin->blob_oid);
 276        memcpy(ce->name, path, len);
 277        ce->ce_flags = create_ce_flags(0);
 278        ce->ce_namelen = len;
 279        ce->ce_mode = create_ce_mode(mode);
 280        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 281
 282        cache_tree_invalidate_path(&the_index, path);
 283
 284        return commit;
 285}
 286
 287
 288
 289static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
 290                      xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
 291{
 292        xpparam_t xpp = {0};
 293        xdemitconf_t xecfg = {0};
 294        xdemitcb_t ecb = {NULL};
 295
 296        xpp.flags = xdl_opts;
 297        xecfg.hunk_func = hunk_func;
 298        ecb.priv = cb_data;
 299        return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
 300}
 301
 302/*
 303 * Given an origin, prepare mmfile_t structure to be used by the
 304 * diff machinery
 305 */
 306static void fill_origin_blob(struct diff_options *opt,
 307                             struct blame_origin *o, mmfile_t *file, int *num_read_blob)
 308{
 309        if (!o->file.ptr) {
 310                enum object_type type;
 311                unsigned long file_size;
 312
 313                (*num_read_blob)++;
 314                if (opt->flags.allow_textconv &&
 315                    textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size))
 316                        ;
 317                else
 318                        file->ptr = read_object_file(&o->blob_oid, &type,
 319                                                     &file_size);
 320                file->size = file_size;
 321
 322                if (!file->ptr)
 323                        die("Cannot read blob %s for path %s",
 324                            oid_to_hex(&o->blob_oid),
 325                            o->path);
 326                o->file = *file;
 327        }
 328        else
 329                *file = o->file;
 330}
 331
 332static void drop_origin_blob(struct blame_origin *o)
 333{
 334        if (o->file.ptr) {
 335                FREE_AND_NULL(o->file.ptr);
 336        }
 337}
 338
 339/*
 340 * Any merge of blames happens on lists of blames that arrived via
 341 * different parents in a single suspect.  In this case, we want to
 342 * sort according to the suspect line numbers as opposed to the final
 343 * image line numbers.  The function body is somewhat longish because
 344 * it avoids unnecessary writes.
 345 */
 346
 347static struct blame_entry *blame_merge(struct blame_entry *list1,
 348                                       struct blame_entry *list2)
 349{
 350        struct blame_entry *p1 = list1, *p2 = list2,
 351                **tail = &list1;
 352
 353        if (!p1)
 354                return p2;
 355        if (!p2)
 356                return p1;
 357
 358        if (p1->s_lno <= p2->s_lno) {
 359                do {
 360                        tail = &p1->next;
 361                        if ((p1 = *tail) == NULL) {
 362                                *tail = p2;
 363                                return list1;
 364                        }
 365                } while (p1->s_lno <= p2->s_lno);
 366        }
 367        for (;;) {
 368                *tail = p2;
 369                do {
 370                        tail = &p2->next;
 371                        if ((p2 = *tail) == NULL)  {
 372                                *tail = p1;
 373                                return list1;
 374                        }
 375                } while (p1->s_lno > p2->s_lno);
 376                *tail = p1;
 377                do {
 378                        tail = &p1->next;
 379                        if ((p1 = *tail) == NULL) {
 380                                *tail = p2;
 381                                return list1;
 382                        }
 383                } while (p1->s_lno <= p2->s_lno);
 384        }
 385}
 386
 387static void *get_next_blame(const void *p)
 388{
 389        return ((struct blame_entry *)p)->next;
 390}
 391
 392static void set_next_blame(void *p1, void *p2)
 393{
 394        ((struct blame_entry *)p1)->next = p2;
 395}
 396
 397/*
 398 * Final image line numbers are all different, so we don't need a
 399 * three-way comparison here.
 400 */
 401
 402static int compare_blame_final(const void *p1, const void *p2)
 403{
 404        return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
 405                ? 1 : -1;
 406}
 407
 408static int compare_blame_suspect(const void *p1, const void *p2)
 409{
 410        const struct blame_entry *s1 = p1, *s2 = p2;
 411        /*
 412         * to allow for collating suspects, we sort according to the
 413         * respective pointer value as the primary sorting criterion.
 414         * The actual relation is pretty unimportant as long as it
 415         * establishes a total order.  Comparing as integers gives us
 416         * that.
 417         */
 418        if (s1->suspect != s2->suspect)
 419                return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
 420        if (s1->s_lno == s2->s_lno)
 421                return 0;
 422        return s1->s_lno > s2->s_lno ? 1 : -1;
 423}
 424
 425void blame_sort_final(struct blame_scoreboard *sb)
 426{
 427        sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,
 428                                  compare_blame_final);
 429}
 430
 431static int compare_commits_by_reverse_commit_date(const void *a,
 432                                                  const void *b,
 433                                                  void *c)
 434{
 435        return -compare_commits_by_commit_date(a, b, c);
 436}
 437
 438/*
 439 * For debugging -- origin is refcounted, and this asserts that
 440 * we do not underflow.
 441 */
 442static void sanity_check_refcnt(struct blame_scoreboard *sb)
 443{
 444        int baa = 0;
 445        struct blame_entry *ent;
 446
 447        for (ent = sb->ent; ent; ent = ent->next) {
 448                /* Nobody should have zero or negative refcnt */
 449                if (ent->suspect->refcnt <= 0) {
 450                        fprintf(stderr, "%s in %s has negative refcnt %d\n",
 451                                ent->suspect->path,
 452                                oid_to_hex(&ent->suspect->commit->object.oid),
 453                                ent->suspect->refcnt);
 454                        baa = 1;
 455                }
 456        }
 457        if (baa)
 458                sb->on_sanity_fail(sb, baa);
 459}
 460
 461/*
 462 * If two blame entries that are next to each other came from
 463 * contiguous lines in the same origin (i.e. <commit, path> pair),
 464 * merge them together.
 465 */
 466void blame_coalesce(struct blame_scoreboard *sb)
 467{
 468        struct blame_entry *ent, *next;
 469
 470        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
 471                if (ent->suspect == next->suspect &&
 472                    ent->s_lno + ent->num_lines == next->s_lno) {
 473                        ent->num_lines += next->num_lines;
 474                        ent->next = next->next;
 475                        blame_origin_decref(next->suspect);
 476                        free(next);
 477                        ent->score = 0;
 478                        next = ent; /* again */
 479                }
 480        }
 481
 482        if (sb->debug) /* sanity */
 483                sanity_check_refcnt(sb);
 484}
 485
 486/*
 487 * Merge the given sorted list of blames into a preexisting origin.
 488 * If there were no previous blames to that commit, it is entered into
 489 * the commit priority queue of the score board.
 490 */
 491
 492static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
 493                         struct blame_entry *sorted)
 494{
 495        if (porigin->suspects)
 496                porigin->suspects = blame_merge(porigin->suspects, sorted);
 497        else {
 498                struct blame_origin *o;
 499                for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
 500                        if (o->suspects) {
 501                                porigin->suspects = sorted;
 502                                return;
 503                        }
 504                }
 505                porigin->suspects = sorted;
 506                prio_queue_put(&sb->commits, porigin->commit);
 507        }
 508}
 509
 510/*
 511 * Fill the blob_sha1 field of an origin if it hasn't, so that later
 512 * call to fill_origin_blob() can use it to locate the data.  blob_sha1
 513 * for an origin is also used to pass the blame for the entire file to
 514 * the parent to detect the case where a child's blob is identical to
 515 * that of its parent's.
 516 *
 517 * This also fills origin->mode for corresponding tree path.
 518 */
 519static int fill_blob_sha1_and_mode(struct blame_origin *origin)
 520{
 521        if (!is_null_oid(&origin->blob_oid))
 522                return 0;
 523        if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
 524                goto error_out;
 525        if (oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB)
 526                goto error_out;
 527        return 0;
 528 error_out:
 529        oidclr(&origin->blob_oid);
 530        origin->mode = S_IFINVALID;
 531        return -1;
 532}
 533
 534/*
 535 * We have an origin -- check if the same path exists in the
 536 * parent and return an origin structure to represent it.
 537 */
 538static struct blame_origin *find_origin(struct commit *parent,
 539                                  struct blame_origin *origin)
 540{
 541        struct blame_origin *porigin;
 542        struct diff_options diff_opts;
 543        const char *paths[2];
 544
 545        /* First check any existing origins */
 546        for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
 547                if (!strcmp(porigin->path, origin->path)) {
 548                        /*
 549                         * The same path between origin and its parent
 550                         * without renaming -- the most common case.
 551                         */
 552                        return blame_origin_incref (porigin);
 553                }
 554
 555        /* See if the origin->path is different between parent
 556         * and origin first.  Most of the time they are the
 557         * same and diff-tree is fairly efficient about this.
 558         */
 559        diff_setup(&diff_opts);
 560        diff_opts.flags.recursive = 1;
 561        diff_opts.detect_rename = 0;
 562        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 563        paths[0] = origin->path;
 564        paths[1] = NULL;
 565
 566        parse_pathspec(&diff_opts.pathspec,
 567                       PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
 568                       PATHSPEC_LITERAL_PATH, "", paths);
 569        diff_setup_done(&diff_opts);
 570
 571        if (is_null_oid(&origin->commit->object.oid))
 572                do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
 573        else
 574                diff_tree_oid(get_commit_tree_oid(parent),
 575                              get_commit_tree_oid(origin->commit),
 576                              "", &diff_opts);
 577        diffcore_std(&diff_opts);
 578
 579        if (!diff_queued_diff.nr) {
 580                /* The path is the same as parent */
 581                porigin = get_origin(parent, origin->path);
 582                oidcpy(&porigin->blob_oid, &origin->blob_oid);
 583                porigin->mode = origin->mode;
 584        } else {
 585                /*
 586                 * Since origin->path is a pathspec, if the parent
 587                 * commit had it as a directory, we will see a whole
 588                 * bunch of deletion of files in the directory that we
 589                 * do not care about.
 590                 */
 591                int i;
 592                struct diff_filepair *p = NULL;
 593                for (i = 0; i < diff_queued_diff.nr; i++) {
 594                        const char *name;
 595                        p = diff_queued_diff.queue[i];
 596                        name = p->one->path ? p->one->path : p->two->path;
 597                        if (!strcmp(name, origin->path))
 598                                break;
 599                }
 600                if (!p)
 601                        die("internal error in blame::find_origin");
 602                switch (p->status) {
 603                default:
 604                        die("internal error in blame::find_origin (%c)",
 605                            p->status);
 606                case 'M':
 607                        porigin = get_origin(parent, origin->path);
 608                        oidcpy(&porigin->blob_oid, &p->one->oid);
 609                        porigin->mode = p->one->mode;
 610                        break;
 611                case 'A':
 612                case 'T':
 613                        /* Did not exist in parent, or type changed */
 614                        break;
 615                }
 616        }
 617        diff_flush(&diff_opts);
 618        clear_pathspec(&diff_opts.pathspec);
 619        return porigin;
 620}
 621
 622/*
 623 * We have an origin -- find the path that corresponds to it in its
 624 * parent and return an origin structure to represent it.
 625 */
 626static struct blame_origin *find_rename(struct commit *parent,
 627                                  struct blame_origin *origin)
 628{
 629        struct blame_origin *porigin = NULL;
 630        struct diff_options diff_opts;
 631        int i;
 632
 633        diff_setup(&diff_opts);
 634        diff_opts.flags.recursive = 1;
 635        diff_opts.detect_rename = DIFF_DETECT_RENAME;
 636        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 637        diff_opts.single_follow = origin->path;
 638        diff_setup_done(&diff_opts);
 639
 640        if (is_null_oid(&origin->commit->object.oid))
 641                do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
 642        else
 643                diff_tree_oid(get_commit_tree_oid(parent),
 644                              get_commit_tree_oid(origin->commit),
 645                              "", &diff_opts);
 646        diffcore_std(&diff_opts);
 647
 648        for (i = 0; i < diff_queued_diff.nr; i++) {
 649                struct diff_filepair *p = diff_queued_diff.queue[i];
 650                if ((p->status == 'R' || p->status == 'C') &&
 651                    !strcmp(p->two->path, origin->path)) {
 652                        porigin = get_origin(parent, p->one->path);
 653                        oidcpy(&porigin->blob_oid, &p->one->oid);
 654                        porigin->mode = p->one->mode;
 655                        break;
 656                }
 657        }
 658        diff_flush(&diff_opts);
 659        clear_pathspec(&diff_opts.pathspec);
 660        return porigin;
 661}
 662
 663/*
 664 * Append a new blame entry to a given output queue.
 665 */
 666static void add_blame_entry(struct blame_entry ***queue,
 667                            const struct blame_entry *src)
 668{
 669        struct blame_entry *e = xmalloc(sizeof(*e));
 670        memcpy(e, src, sizeof(*e));
 671        blame_origin_incref(e->suspect);
 672
 673        e->next = **queue;
 674        **queue = e;
 675        *queue = &e->next;
 676}
 677
 678/*
 679 * src typically is on-stack; we want to copy the information in it to
 680 * a malloced blame_entry that gets added to the given queue.  The
 681 * origin of dst loses a refcnt.
 682 */
 683static void dup_entry(struct blame_entry ***queue,
 684                      struct blame_entry *dst, struct blame_entry *src)
 685{
 686        blame_origin_incref(src->suspect);
 687        blame_origin_decref(dst->suspect);
 688        memcpy(dst, src, sizeof(*src));
 689        dst->next = **queue;
 690        **queue = dst;
 691        *queue = &dst->next;
 692}
 693
 694const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
 695{
 696        return sb->final_buf + sb->lineno[lno];
 697}
 698
 699/*
 700 * It is known that lines between tlno to same came from parent, and e
 701 * has an overlap with that range.  it also is known that parent's
 702 * line plno corresponds to e's line tlno.
 703 *
 704 *                <---- e ----->
 705 *                   <------>
 706 *                   <------------>
 707 *             <------------>
 708 *             <------------------>
 709 *
 710 * Split e into potentially three parts; before this chunk, the chunk
 711 * to be blamed for the parent, and after that portion.
 712 */
 713static void split_overlap(struct blame_entry *split,
 714                          struct blame_entry *e,
 715                          int tlno, int plno, int same,
 716                          struct blame_origin *parent)
 717{
 718        int chunk_end_lno;
 719        memset(split, 0, sizeof(struct blame_entry [3]));
 720
 721        if (e->s_lno < tlno) {
 722                /* there is a pre-chunk part not blamed on parent */
 723                split[0].suspect = blame_origin_incref(e->suspect);
 724                split[0].lno = e->lno;
 725                split[0].s_lno = e->s_lno;
 726                split[0].num_lines = tlno - e->s_lno;
 727                split[1].lno = e->lno + tlno - e->s_lno;
 728                split[1].s_lno = plno;
 729        }
 730        else {
 731                split[1].lno = e->lno;
 732                split[1].s_lno = plno + (e->s_lno - tlno);
 733        }
 734
 735        if (same < e->s_lno + e->num_lines) {
 736                /* there is a post-chunk part not blamed on parent */
 737                split[2].suspect = blame_origin_incref(e->suspect);
 738                split[2].lno = e->lno + (same - e->s_lno);
 739                split[2].s_lno = e->s_lno + (same - e->s_lno);
 740                split[2].num_lines = e->s_lno + e->num_lines - same;
 741                chunk_end_lno = split[2].lno;
 742        }
 743        else
 744                chunk_end_lno = e->lno + e->num_lines;
 745        split[1].num_lines = chunk_end_lno - split[1].lno;
 746
 747        /*
 748         * if it turns out there is nothing to blame the parent for,
 749         * forget about the splitting.  !split[1].suspect signals this.
 750         */
 751        if (split[1].num_lines < 1)
 752                return;
 753        split[1].suspect = blame_origin_incref(parent);
 754}
 755
 756/*
 757 * split_overlap() divided an existing blame e into up to three parts
 758 * in split.  Any assigned blame is moved to queue to
 759 * reflect the split.
 760 */
 761static void split_blame(struct blame_entry ***blamed,
 762                        struct blame_entry ***unblamed,
 763                        struct blame_entry *split,
 764                        struct blame_entry *e)
 765{
 766        if (split[0].suspect && split[2].suspect) {
 767                /* The first part (reuse storage for the existing entry e) */
 768                dup_entry(unblamed, e, &split[0]);
 769
 770                /* The last part -- me */
 771                add_blame_entry(unblamed, &split[2]);
 772
 773                /* ... and the middle part -- parent */
 774                add_blame_entry(blamed, &split[1]);
 775        }
 776        else if (!split[0].suspect && !split[2].suspect)
 777                /*
 778                 * The parent covers the entire area; reuse storage for
 779                 * e and replace it with the parent.
 780                 */
 781                dup_entry(blamed, e, &split[1]);
 782        else if (split[0].suspect) {
 783                /* me and then parent */
 784                dup_entry(unblamed, e, &split[0]);
 785                add_blame_entry(blamed, &split[1]);
 786        }
 787        else {
 788                /* parent and then me */
 789                dup_entry(blamed, e, &split[1]);
 790                add_blame_entry(unblamed, &split[2]);
 791        }
 792}
 793
 794/*
 795 * After splitting the blame, the origins used by the
 796 * on-stack blame_entry should lose one refcnt each.
 797 */
 798static void decref_split(struct blame_entry *split)
 799{
 800        int i;
 801
 802        for (i = 0; i < 3; i++)
 803                blame_origin_decref(split[i].suspect);
 804}
 805
 806/*
 807 * reverse_blame reverses the list given in head, appending tail.
 808 * That allows us to build lists in reverse order, then reverse them
 809 * afterwards.  This can be faster than building the list in proper
 810 * order right away.  The reason is that building in proper order
 811 * requires writing a link in the _previous_ element, while building
 812 * in reverse order just requires placing the list head into the
 813 * _current_ element.
 814 */
 815
 816static struct blame_entry *reverse_blame(struct blame_entry *head,
 817                                         struct blame_entry *tail)
 818{
 819        while (head) {
 820                struct blame_entry *next = head->next;
 821                head->next = tail;
 822                tail = head;
 823                head = next;
 824        }
 825        return tail;
 826}
 827
 828/*
 829 * Process one hunk from the patch between the current suspect for
 830 * blame_entry e and its parent.  This first blames any unfinished
 831 * entries before the chunk (which is where target and parent start
 832 * differing) on the parent, and then splits blame entries at the
 833 * start and at the end of the difference region.  Since use of -M and
 834 * -C options may lead to overlapping/duplicate source line number
 835 * ranges, all we can rely on from sorting/merging is the order of the
 836 * first suspect line number.
 837 */
 838static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
 839                        int tlno, int offset, int same,
 840                        struct blame_origin *parent)
 841{
 842        struct blame_entry *e = **srcq;
 843        struct blame_entry *samep = NULL, *diffp = NULL;
 844
 845        while (e && e->s_lno < tlno) {
 846                struct blame_entry *next = e->next;
 847                /*
 848                 * current record starts before differing portion.  If
 849                 * it reaches into it, we need to split it up and
 850                 * examine the second part separately.
 851                 */
 852                if (e->s_lno + e->num_lines > tlno) {
 853                        /* Move second half to a new record */
 854                        int len = tlno - e->s_lno;
 855                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
 856                        n->suspect = e->suspect;
 857                        n->lno = e->lno + len;
 858                        n->s_lno = e->s_lno + len;
 859                        n->num_lines = e->num_lines - len;
 860                        e->num_lines = len;
 861                        e->score = 0;
 862                        /* Push new record to diffp */
 863                        n->next = diffp;
 864                        diffp = n;
 865                } else
 866                        blame_origin_decref(e->suspect);
 867                /* Pass blame for everything before the differing
 868                 * chunk to the parent */
 869                e->suspect = blame_origin_incref(parent);
 870                e->s_lno += offset;
 871                e->next = samep;
 872                samep = e;
 873                e = next;
 874        }
 875        /*
 876         * As we don't know how much of a common stretch after this
 877         * diff will occur, the currently blamed parts are all that we
 878         * can assign to the parent for now.
 879         */
 880
 881        if (samep) {
 882                **dstq = reverse_blame(samep, **dstq);
 883                *dstq = &samep->next;
 884        }
 885        /*
 886         * Prepend the split off portions: everything after e starts
 887         * after the blameable portion.
 888         */
 889        e = reverse_blame(diffp, e);
 890
 891        /*
 892         * Now retain records on the target while parts are different
 893         * from the parent.
 894         */
 895        samep = NULL;
 896        diffp = NULL;
 897        while (e && e->s_lno < same) {
 898                struct blame_entry *next = e->next;
 899
 900                /*
 901                 * If current record extends into sameness, need to split.
 902                 */
 903                if (e->s_lno + e->num_lines > same) {
 904                        /*
 905                         * Move second half to a new record to be
 906                         * processed by later chunks
 907                         */
 908                        int len = same - e->s_lno;
 909                        struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
 910                        n->suspect = blame_origin_incref(e->suspect);
 911                        n->lno = e->lno + len;
 912                        n->s_lno = e->s_lno + len;
 913                        n->num_lines = e->num_lines - len;
 914                        e->num_lines = len;
 915                        e->score = 0;
 916                        /* Push new record to samep */
 917                        n->next = samep;
 918                        samep = n;
 919                }
 920                e->next = diffp;
 921                diffp = e;
 922                e = next;
 923        }
 924        **srcq = reverse_blame(diffp, reverse_blame(samep, e));
 925        /* Move across elements that are in the unblamable portion */
 926        if (diffp)
 927                *srcq = &diffp->next;
 928}
 929
 930struct blame_chunk_cb_data {
 931        struct blame_origin *parent;
 932        long offset;
 933        struct blame_entry **dstq;
 934        struct blame_entry **srcq;
 935};
 936
 937/* diff chunks are from parent to target */
 938static int blame_chunk_cb(long start_a, long count_a,
 939                          long start_b, long count_b, void *data)
 940{
 941        struct blame_chunk_cb_data *d = data;
 942        if (start_a - start_b != d->offset)
 943                die("internal error in blame::blame_chunk_cb");
 944        blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
 945                    start_b + count_b, d->parent);
 946        d->offset = start_a + count_a - (start_b + count_b);
 947        return 0;
 948}
 949
 950/*
 951 * We are looking at the origin 'target' and aiming to pass blame
 952 * for the lines it is suspected to its parent.  Run diff to find
 953 * which lines came from parent and pass blame for them.
 954 */
 955static void pass_blame_to_parent(struct blame_scoreboard *sb,
 956                                 struct blame_origin *target,
 957                                 struct blame_origin *parent)
 958{
 959        mmfile_t file_p, file_o;
 960        struct blame_chunk_cb_data d;
 961        struct blame_entry *newdest = NULL;
 962
 963        if (!target->suspects)
 964                return; /* nothing remains for this target */
 965
 966        d.parent = parent;
 967        d.offset = 0;
 968        d.dstq = &newdest; d.srcq = &target->suspects;
 969
 970        fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
 971        fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
 972        sb->num_get_patch++;
 973
 974        if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
 975                die("unable to generate diff (%s -> %s)",
 976                    oid_to_hex(&parent->commit->object.oid),
 977                    oid_to_hex(&target->commit->object.oid));
 978        /* The rest are the same as the parent */
 979        blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
 980        *d.dstq = NULL;
 981        queue_blames(sb, parent, newdest);
 982
 983        return;
 984}
 985
 986/*
 987 * The lines in blame_entry after splitting blames many times can become
 988 * very small and trivial, and at some point it becomes pointless to
 989 * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
 990 * ordinary C program, and it is not worth to say it was copied from
 991 * totally unrelated file in the parent.
 992 *
 993 * Compute how trivial the lines in the blame_entry are.
 994 */
 995unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
 996{
 997        unsigned score;
 998        const char *cp, *ep;
 999
1000        if (e->score)
1001                return e->score;
1002
1003        score = 1;
1004        cp = blame_nth_line(sb, e->lno);
1005        ep = blame_nth_line(sb, e->lno + e->num_lines);
1006        while (cp < ep) {
1007                unsigned ch = *((unsigned char *)cp);
1008                if (isalnum(ch))
1009                        score++;
1010                cp++;
1011        }
1012        e->score = score;
1013        return score;
1014}
1015
1016/*
1017 * best_so_far[] and potential[] are both a split of an existing blame_entry
1018 * that passes blame to the parent.  Maintain best_so_far the best split so
1019 * far, by comparing potential and best_so_far and copying potential into
1020 * bst_so_far as needed.
1021 */
1022static void copy_split_if_better(struct blame_scoreboard *sb,
1023                                 struct blame_entry *best_so_far,
1024                                 struct blame_entry *potential)
1025{
1026        int i;
1027
1028        if (!potential[1].suspect)
1029                return;
1030        if (best_so_far[1].suspect) {
1031                if (blame_entry_score(sb, &potential[1]) <
1032                    blame_entry_score(sb, &best_so_far[1]))
1033                        return;
1034        }
1035
1036        for (i = 0; i < 3; i++)
1037                blame_origin_incref(potential[i].suspect);
1038        decref_split(best_so_far);
1039        memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
1040}
1041
1042/*
1043 * We are looking at a part of the final image represented by
1044 * ent (tlno and same are offset by ent->s_lno).
1045 * tlno is where we are looking at in the final image.
1046 * up to (but not including) same match preimage.
1047 * plno is where we are looking at in the preimage.
1048 *
1049 * <-------------- final image ---------------------->
1050 *       <------ent------>
1051 *         ^tlno ^same
1052 *    <---------preimage----->
1053 *         ^plno
1054 *
1055 * All line numbers are 0-based.
1056 */
1057static void handle_split(struct blame_scoreboard *sb,
1058                         struct blame_entry *ent,
1059                         int tlno, int plno, int same,
1060                         struct blame_origin *parent,
1061                         struct blame_entry *split)
1062{
1063        if (ent->num_lines <= tlno)
1064                return;
1065        if (tlno < same) {
1066                struct blame_entry potential[3];
1067                tlno += ent->s_lno;
1068                same += ent->s_lno;
1069                split_overlap(potential, ent, tlno, plno, same, parent);
1070                copy_split_if_better(sb, split, potential);
1071                decref_split(potential);
1072        }
1073}
1074
1075struct handle_split_cb_data {
1076        struct blame_scoreboard *sb;
1077        struct blame_entry *ent;
1078        struct blame_origin *parent;
1079        struct blame_entry *split;
1080        long plno;
1081        long tlno;
1082};
1083
1084static int handle_split_cb(long start_a, long count_a,
1085                           long start_b, long count_b, void *data)
1086{
1087        struct handle_split_cb_data *d = data;
1088        handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1089                     d->split);
1090        d->plno = start_a + count_a;
1091        d->tlno = start_b + count_b;
1092        return 0;
1093}
1094
1095/*
1096 * Find the lines from parent that are the same as ent so that
1097 * we can pass blames to it.  file_p has the blob contents for
1098 * the parent.
1099 */
1100static void find_copy_in_blob(struct blame_scoreboard *sb,
1101                              struct blame_entry *ent,
1102                              struct blame_origin *parent,
1103                              struct blame_entry *split,
1104                              mmfile_t *file_p)
1105{
1106        const char *cp;
1107        mmfile_t file_o;
1108        struct handle_split_cb_data d;
1109
1110        memset(&d, 0, sizeof(d));
1111        d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1112        /*
1113         * Prepare mmfile that contains only the lines in ent.
1114         */
1115        cp = blame_nth_line(sb, ent->lno);
1116        file_o.ptr = (char *) cp;
1117        file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
1118
1119        /*
1120         * file_o is a part of final image we are annotating.
1121         * file_p partially may match that image.
1122         */
1123        memset(split, 0, sizeof(struct blame_entry [3]));
1124        if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
1125                die("unable to generate diff (%s)",
1126                    oid_to_hex(&parent->commit->object.oid));
1127        /* remainder, if any, all match the preimage */
1128        handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1129}
1130
1131/* Move all blame entries from list *source that have a score smaller
1132 * than score_min to the front of list *small.
1133 * Returns a pointer to the link pointing to the old head of the small list.
1134 */
1135
1136static struct blame_entry **filter_small(struct blame_scoreboard *sb,
1137                                         struct blame_entry **small,
1138                                         struct blame_entry **source,
1139                                         unsigned score_min)
1140{
1141        struct blame_entry *p = *source;
1142        struct blame_entry *oldsmall = *small;
1143        while (p) {
1144                if (blame_entry_score(sb, p) <= score_min) {
1145                        *small = p;
1146                        small = &p->next;
1147                        p = *small;
1148                } else {
1149                        *source = p;
1150                        source = &p->next;
1151                        p = *source;
1152                }
1153        }
1154        *small = oldsmall;
1155        *source = NULL;
1156        return small;
1157}
1158
1159/*
1160 * See if lines currently target is suspected for can be attributed to
1161 * parent.
1162 */
1163static void find_move_in_parent(struct blame_scoreboard *sb,
1164                                struct blame_entry ***blamed,
1165                                struct blame_entry **toosmall,
1166                                struct blame_origin *target,
1167                                struct blame_origin *parent)
1168{
1169        struct blame_entry *e, split[3];
1170        struct blame_entry *unblamed = target->suspects;
1171        struct blame_entry *leftover = NULL;
1172        mmfile_t file_p;
1173
1174        if (!unblamed)
1175                return; /* nothing remains for this target */
1176
1177        fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1178        if (!file_p.ptr)
1179                return;
1180
1181        /* At each iteration, unblamed has a NULL-terminated list of
1182         * entries that have not yet been tested for blame.  leftover
1183         * contains the reversed list of entries that have been tested
1184         * without being assignable to the parent.
1185         */
1186        do {
1187                struct blame_entry **unblamedtail = &unblamed;
1188                struct blame_entry *next;
1189                for (e = unblamed; e; e = next) {
1190                        next = e->next;
1191                        find_copy_in_blob(sb, e, parent, split, &file_p);
1192                        if (split[1].suspect &&
1193                            sb->move_score < blame_entry_score(sb, &split[1])) {
1194                                split_blame(blamed, &unblamedtail, split, e);
1195                        } else {
1196                                e->next = leftover;
1197                                leftover = e;
1198                        }
1199                        decref_split(split);
1200                }
1201                *unblamedtail = NULL;
1202                toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
1203        } while (unblamed);
1204        target->suspects = reverse_blame(leftover, NULL);
1205}
1206
1207struct blame_list {
1208        struct blame_entry *ent;
1209        struct blame_entry split[3];
1210};
1211
1212/*
1213 * Count the number of entries the target is suspected for,
1214 * and prepare a list of entry and the best split.
1215 */
1216static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1217                                           int *num_ents_p)
1218{
1219        struct blame_entry *e;
1220        int num_ents, i;
1221        struct blame_list *blame_list = NULL;
1222
1223        for (e = unblamed, num_ents = 0; e; e = e->next)
1224                num_ents++;
1225        if (num_ents) {
1226                blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1227                for (e = unblamed, i = 0; e; e = e->next)
1228                        blame_list[i++].ent = e;
1229        }
1230        *num_ents_p = num_ents;
1231        return blame_list;
1232}
1233
1234/*
1235 * For lines target is suspected for, see if we can find code movement
1236 * across file boundary from the parent commit.  porigin is the path
1237 * in the parent we already tried.
1238 */
1239static void find_copy_in_parent(struct blame_scoreboard *sb,
1240                                struct blame_entry ***blamed,
1241                                struct blame_entry **toosmall,
1242                                struct blame_origin *target,
1243                                struct commit *parent,
1244                                struct blame_origin *porigin,
1245                                int opt)
1246{
1247        struct diff_options diff_opts;
1248        int i, j;
1249        struct blame_list *blame_list;
1250        int num_ents;
1251        struct blame_entry *unblamed = target->suspects;
1252        struct blame_entry *leftover = NULL;
1253
1254        if (!unblamed)
1255                return; /* nothing remains for this target */
1256
1257        diff_setup(&diff_opts);
1258        diff_opts.flags.recursive = 1;
1259        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1260
1261        diff_setup_done(&diff_opts);
1262
1263        /* Try "find copies harder" on new path if requested;
1264         * we do not want to use diffcore_rename() actually to
1265         * match things up; find_copies_harder is set only to
1266         * force diff_tree_oid() to feed all filepairs to diff_queue,
1267         * and this code needs to be after diff_setup_done(), which
1268         * usually makes find-copies-harder imply copy detection.
1269         */
1270        if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1271            || ((opt & PICKAXE_BLAME_COPY_HARDER)
1272                && (!porigin || strcmp(target->path, porigin->path))))
1273                diff_opts.flags.find_copies_harder = 1;
1274
1275        if (is_null_oid(&target->commit->object.oid))
1276                do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1277        else
1278                diff_tree_oid(get_commit_tree_oid(parent),
1279                              get_commit_tree_oid(target->commit),
1280                              "", &diff_opts);
1281
1282        if (!diff_opts.flags.find_copies_harder)
1283                diffcore_std(&diff_opts);
1284
1285        do {
1286                struct blame_entry **unblamedtail = &unblamed;
1287                blame_list = setup_blame_list(unblamed, &num_ents);
1288
1289                for (i = 0; i < diff_queued_diff.nr; i++) {
1290                        struct diff_filepair *p = diff_queued_diff.queue[i];
1291                        struct blame_origin *norigin;
1292                        mmfile_t file_p;
1293                        struct blame_entry potential[3];
1294
1295                        if (!DIFF_FILE_VALID(p->one))
1296                                continue; /* does not exist in parent */
1297                        if (S_ISGITLINK(p->one->mode))
1298                                continue; /* ignore git links */
1299                        if (porigin && !strcmp(p->one->path, porigin->path))
1300                                /* find_move already dealt with this path */
1301                                continue;
1302
1303                        norigin = get_origin(parent, p->one->path);
1304                        oidcpy(&norigin->blob_oid, &p->one->oid);
1305                        norigin->mode = p->one->mode;
1306                        fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1307                        if (!file_p.ptr)
1308                                continue;
1309
1310                        for (j = 0; j < num_ents; j++) {
1311                                find_copy_in_blob(sb, blame_list[j].ent,
1312                                                  norigin, potential, &file_p);
1313                                copy_split_if_better(sb, blame_list[j].split,
1314                                                     potential);
1315                                decref_split(potential);
1316                        }
1317                        blame_origin_decref(norigin);
1318                }
1319
1320                for (j = 0; j < num_ents; j++) {
1321                        struct blame_entry *split = blame_list[j].split;
1322                        if (split[1].suspect &&
1323                            sb->copy_score < blame_entry_score(sb, &split[1])) {
1324                                split_blame(blamed, &unblamedtail, split,
1325                                            blame_list[j].ent);
1326                        } else {
1327                                blame_list[j].ent->next = leftover;
1328                                leftover = blame_list[j].ent;
1329                        }
1330                        decref_split(split);
1331                }
1332                free(blame_list);
1333                *unblamedtail = NULL;
1334                toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
1335        } while (unblamed);
1336        target->suspects = reverse_blame(leftover, NULL);
1337        diff_flush(&diff_opts);
1338        clear_pathspec(&diff_opts.pathspec);
1339}
1340
1341/*
1342 * The blobs of origin and porigin exactly match, so everything
1343 * origin is suspected for can be blamed on the parent.
1344 */
1345static void pass_whole_blame(struct blame_scoreboard *sb,
1346                             struct blame_origin *origin, struct blame_origin *porigin)
1347{
1348        struct blame_entry *e, *suspects;
1349
1350        if (!porigin->file.ptr && origin->file.ptr) {
1351                /* Steal its file */
1352                porigin->file = origin->file;
1353                origin->file.ptr = NULL;
1354        }
1355        suspects = origin->suspects;
1356        origin->suspects = NULL;
1357        for (e = suspects; e; e = e->next) {
1358                blame_origin_incref(porigin);
1359                blame_origin_decref(e->suspect);
1360                e->suspect = porigin;
1361        }
1362        queue_blames(sb, porigin, suspects);
1363}
1364
1365/*
1366 * We pass blame from the current commit to its parents.  We keep saying
1367 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1368 * exonerate ourselves.
1369 */
1370static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
1371                                        int reverse)
1372{
1373        if (!reverse) {
1374                if (revs->first_parent_only &&
1375                    commit->parents &&
1376                    commit->parents->next) {
1377                        free_commit_list(commit->parents->next);
1378                        commit->parents->next = NULL;
1379                }
1380                return commit->parents;
1381        }
1382        return lookup_decoration(&revs->children, &commit->object);
1383}
1384
1385static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
1386{
1387        struct commit_list *l = first_scapegoat(revs, commit, reverse);
1388        return commit_list_count(l);
1389}
1390
1391/* Distribute collected unsorted blames to the respected sorted lists
1392 * in the various origins.
1393 */
1394static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
1395{
1396        blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,
1397                                 compare_blame_suspect);
1398        while (blamed)
1399        {
1400                struct blame_origin *porigin = blamed->suspect;
1401                struct blame_entry *suspects = NULL;
1402                do {
1403                        struct blame_entry *next = blamed->next;
1404                        blamed->next = suspects;
1405                        suspects = blamed;
1406                        blamed = next;
1407                } while (blamed && blamed->suspect == porigin);
1408                suspects = reverse_blame(suspects, NULL);
1409                queue_blames(sb, porigin, suspects);
1410        }
1411}
1412
1413#define MAXSG 16
1414
1415static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
1416{
1417        struct rev_info *revs = sb->revs;
1418        int i, pass, num_sg;
1419        struct commit *commit = origin->commit;
1420        struct commit_list *sg;
1421        struct blame_origin *sg_buf[MAXSG];
1422        struct blame_origin *porigin, **sg_origin = sg_buf;
1423        struct blame_entry *toosmall = NULL;
1424        struct blame_entry *blames, **blametail = &blames;
1425
1426        num_sg = num_scapegoats(revs, commit, sb->reverse);
1427        if (!num_sg)
1428                goto finish;
1429        else if (num_sg < ARRAY_SIZE(sg_buf))
1430                memset(sg_buf, 0, sizeof(sg_buf));
1431        else
1432                sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1433
1434        /*
1435         * The first pass looks for unrenamed path to optimize for
1436         * common cases, then we look for renames in the second pass.
1437         */
1438        for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
1439                struct blame_origin *(*find)(struct commit *, struct blame_origin *);
1440                find = pass ? find_rename : find_origin;
1441
1442                for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1443                     i < num_sg && sg;
1444                     sg = sg->next, i++) {
1445                        struct commit *p = sg->item;
1446                        int j, same;
1447
1448                        if (sg_origin[i])
1449                                continue;
1450                        if (parse_commit(p))
1451                                continue;
1452                        porigin = find(p, origin);
1453                        if (!porigin)
1454                                continue;
1455                        if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
1456                                pass_whole_blame(sb, origin, porigin);
1457                                blame_origin_decref(porigin);
1458                                goto finish;
1459                        }
1460                        for (j = same = 0; j < i; j++)
1461                                if (sg_origin[j] &&
1462                                    !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
1463                                        same = 1;
1464                                        break;
1465                                }
1466                        if (!same)
1467                                sg_origin[i] = porigin;
1468                        else
1469                                blame_origin_decref(porigin);
1470                }
1471        }
1472
1473        sb->num_commits++;
1474        for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1475             i < num_sg && sg;
1476             sg = sg->next, i++) {
1477                struct blame_origin *porigin = sg_origin[i];
1478                if (!porigin)
1479                        continue;
1480                if (!origin->previous) {
1481                        blame_origin_incref(porigin);
1482                        origin->previous = porigin;
1483                }
1484                pass_blame_to_parent(sb, origin, porigin);
1485                if (!origin->suspects)
1486                        goto finish;
1487        }
1488
1489        /*
1490         * Optionally find moves in parents' files.
1491         */
1492        if (opt & PICKAXE_BLAME_MOVE) {
1493                filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
1494                if (origin->suspects) {
1495                        for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1496                             i < num_sg && sg;
1497                             sg = sg->next, i++) {
1498                                struct blame_origin *porigin = sg_origin[i];
1499                                if (!porigin)
1500                                        continue;
1501                                find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1502                                if (!origin->suspects)
1503                                        break;
1504                        }
1505                }
1506        }
1507
1508        /*
1509         * Optionally find copies from parents' files.
1510         */
1511        if (opt & PICKAXE_BLAME_COPY) {
1512                if (sb->copy_score > sb->move_score)
1513                        filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1514                else if (sb->copy_score < sb->move_score) {
1515                        origin->suspects = blame_merge(origin->suspects, toosmall);
1516                        toosmall = NULL;
1517                        filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1518                }
1519                if (!origin->suspects)
1520                        goto finish;
1521
1522                for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1523                     i < num_sg && sg;
1524                     sg = sg->next, i++) {
1525                        struct blame_origin *porigin = sg_origin[i];
1526                        find_copy_in_parent(sb, &blametail, &toosmall,
1527                                            origin, sg->item, porigin, opt);
1528                        if (!origin->suspects)
1529                                goto finish;
1530                }
1531        }
1532
1533finish:
1534        *blametail = NULL;
1535        distribute_blame(sb, blames);
1536        /*
1537         * prepend toosmall to origin->suspects
1538         *
1539         * There is no point in sorting: this ends up on a big
1540         * unsorted list in the caller anyway.
1541         */
1542        if (toosmall) {
1543                struct blame_entry **tail = &toosmall;
1544                while (*tail)
1545                        tail = &(*tail)->next;
1546                *tail = origin->suspects;
1547                origin->suspects = toosmall;
1548        }
1549        for (i = 0; i < num_sg; i++) {
1550                if (sg_origin[i]) {
1551                        drop_origin_blob(sg_origin[i]);
1552                        blame_origin_decref(sg_origin[i]);
1553                }
1554        }
1555        drop_origin_blob(origin);
1556        if (sg_buf != sg_origin)
1557                free(sg_origin);
1558}
1559
1560/*
1561 * The main loop -- while we have blobs with lines whose true origin
1562 * is still unknown, pick one blob, and allow its lines to pass blames
1563 * to its parents. */
1564void assign_blame(struct blame_scoreboard *sb, int opt)
1565{
1566        struct rev_info *revs = sb->revs;
1567        struct commit *commit = prio_queue_get(&sb->commits);
1568
1569        while (commit) {
1570                struct blame_entry *ent;
1571                struct blame_origin *suspect = get_blame_suspects(commit);
1572
1573                /* find one suspect to break down */
1574                while (suspect && !suspect->suspects)
1575                        suspect = suspect->next;
1576
1577                if (!suspect) {
1578                        commit = prio_queue_get(&sb->commits);
1579                        continue;
1580                }
1581
1582                assert(commit == suspect->commit);
1583
1584                /*
1585                 * We will use this suspect later in the loop,
1586                 * so hold onto it in the meantime.
1587                 */
1588                blame_origin_incref(suspect);
1589                parse_commit(commit);
1590                if (sb->reverse ||
1591                    (!(commit->object.flags & UNINTERESTING) &&
1592                     !(revs->max_age != -1 && commit->date < revs->max_age)))
1593                        pass_blame(sb, suspect, opt);
1594                else {
1595                        commit->object.flags |= UNINTERESTING;
1596                        if (commit->object.parsed)
1597                                mark_parents_uninteresting(commit);
1598                }
1599                /* treat root commit as boundary */
1600                if (!commit->parents && !sb->show_root)
1601                        commit->object.flags |= UNINTERESTING;
1602
1603                /* Take responsibility for the remaining entries */
1604                ent = suspect->suspects;
1605                if (ent) {
1606                        suspect->guilty = 1;
1607                        for (;;) {
1608                                struct blame_entry *next = ent->next;
1609                                if (sb->found_guilty_entry)
1610                                        sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
1611                                if (next) {
1612                                        ent = next;
1613                                        continue;
1614                                }
1615                                ent->next = sb->ent;
1616                                sb->ent = suspect->suspects;
1617                                suspect->suspects = NULL;
1618                                break;
1619                        }
1620                }
1621                blame_origin_decref(suspect);
1622
1623                if (sb->debug) /* sanity */
1624                        sanity_check_refcnt(sb);
1625        }
1626}
1627
1628static const char *get_next_line(const char *start, const char *end)
1629{
1630        const char *nl = memchr(start, '\n', end - start);
1631        return nl ? nl + 1 : end;
1632}
1633
1634/*
1635 * To allow quick access to the contents of nth line in the
1636 * final image, prepare an index in the scoreboard.
1637 */
1638static int prepare_lines(struct blame_scoreboard *sb)
1639{
1640        const char *buf = sb->final_buf;
1641        unsigned long len = sb->final_buf_size;
1642        const char *end = buf + len;
1643        const char *p;
1644        int *lineno;
1645        int num = 0;
1646
1647        for (p = buf; p < end; p = get_next_line(p, end))
1648                num++;
1649
1650        ALLOC_ARRAY(sb->lineno, num + 1);
1651        lineno = sb->lineno;
1652
1653        for (p = buf; p < end; p = get_next_line(p, end))
1654                *lineno++ = p - buf;
1655
1656        *lineno = len;
1657
1658        sb->num_lines = num;
1659        return sb->num_lines;
1660}
1661
1662static struct commit *find_single_final(struct rev_info *revs,
1663                                        const char **name_p)
1664{
1665        int i;
1666        struct commit *found = NULL;
1667        const char *name = NULL;
1668
1669        for (i = 0; i < revs->pending.nr; i++) {
1670                struct object *obj = revs->pending.objects[i].item;
1671                if (obj->flags & UNINTERESTING)
1672                        continue;
1673                obj = deref_tag(obj, NULL, 0);
1674                if (obj->type != OBJ_COMMIT)
1675                        die("Non commit %s?", revs->pending.objects[i].name);
1676                if (found)
1677                        die("More than one commit to dig from %s and %s?",
1678                            revs->pending.objects[i].name, name);
1679                found = (struct commit *)obj;
1680                name = revs->pending.objects[i].name;
1681        }
1682        if (name_p)
1683                *name_p = xstrdup_or_null(name);
1684        return found;
1685}
1686
1687static struct commit *dwim_reverse_initial(struct rev_info *revs,
1688                                           const char **name_p)
1689{
1690        /*
1691         * DWIM "git blame --reverse ONE -- PATH" as
1692         * "git blame --reverse ONE..HEAD -- PATH" but only do so
1693         * when it makes sense.
1694         */
1695        struct object *obj;
1696        struct commit *head_commit;
1697        struct object_id head_oid;
1698
1699        if (revs->pending.nr != 1)
1700                return NULL;
1701
1702        /* Is that sole rev a committish? */
1703        obj = revs->pending.objects[0].item;
1704        obj = deref_tag(obj, NULL, 0);
1705        if (obj->type != OBJ_COMMIT)
1706                return NULL;
1707
1708        /* Do we have HEAD? */
1709        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
1710                return NULL;
1711        head_commit = lookup_commit_reference_gently(&head_oid, 1);
1712        if (!head_commit)
1713                return NULL;
1714
1715        /* Turn "ONE" into "ONE..HEAD" then */
1716        obj->flags |= UNINTERESTING;
1717        add_pending_object(revs, &head_commit->object, "HEAD");
1718
1719        if (name_p)
1720                *name_p = revs->pending.objects[0].name;
1721        return (struct commit *)obj;
1722}
1723
1724static struct commit *find_single_initial(struct rev_info *revs,
1725                                          const char **name_p)
1726{
1727        int i;
1728        struct commit *found = NULL;
1729        const char *name = NULL;
1730
1731        /*
1732         * There must be one and only one negative commit, and it must be
1733         * the boundary.
1734         */
1735        for (i = 0; i < revs->pending.nr; i++) {
1736                struct object *obj = revs->pending.objects[i].item;
1737                if (!(obj->flags & UNINTERESTING))
1738                        continue;
1739                obj = deref_tag(obj, NULL, 0);
1740                if (obj->type != OBJ_COMMIT)
1741                        die("Non commit %s?", revs->pending.objects[i].name);
1742                if (found)
1743                        die("More than one commit to dig up from, %s and %s?",
1744                            revs->pending.objects[i].name, name);
1745                found = (struct commit *) obj;
1746                name = revs->pending.objects[i].name;
1747        }
1748
1749        if (!name)
1750                found = dwim_reverse_initial(revs, &name);
1751        if (!name)
1752                die("No commit to dig up from?");
1753
1754        if (name_p)
1755                *name_p = xstrdup(name);
1756        return found;
1757}
1758
1759void init_scoreboard(struct blame_scoreboard *sb)
1760{
1761        memset(sb, 0, sizeof(struct blame_scoreboard));
1762        sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
1763        sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
1764}
1765
1766void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)
1767{
1768        const char *final_commit_name = NULL;
1769        struct blame_origin *o;
1770        struct commit *final_commit = NULL;
1771        enum object_type type;
1772
1773        init_blame_suspects(&blame_suspects);
1774
1775        if (sb->reverse && sb->contents_from)
1776                die(_("--contents and --reverse do not blend well."));
1777
1778        if (!sb->reverse) {
1779                sb->final = find_single_final(sb->revs, &final_commit_name);
1780                sb->commits.compare = compare_commits_by_commit_date;
1781        } else {
1782                sb->final = find_single_initial(sb->revs, &final_commit_name);
1783                sb->commits.compare = compare_commits_by_reverse_commit_date;
1784        }
1785
1786        if (sb->final && sb->contents_from)
1787                die(_("cannot use --contents with final commit object name"));
1788
1789        if (sb->reverse && sb->revs->first_parent_only)
1790                sb->revs->children.name = NULL;
1791
1792        if (!sb->final) {
1793                /*
1794                 * "--not A B -- path" without anything positive;
1795                 * do not default to HEAD, but use the working tree
1796                 * or "--contents".
1797                 */
1798                setup_work_tree();
1799                sb->final = fake_working_tree_commit(&sb->revs->diffopt,
1800                                                     path, sb->contents_from);
1801                add_pending_object(sb->revs, &(sb->final->object), ":");
1802        }
1803
1804        if (sb->reverse && sb->revs->first_parent_only) {
1805                final_commit = find_single_final(sb->revs, NULL);
1806                if (!final_commit)
1807                        die(_("--reverse and --first-parent together require specified latest commit"));
1808        }
1809
1810        /*
1811         * If we have bottom, this will mark the ancestors of the
1812         * bottom commits we would reach while traversing as
1813         * uninteresting.
1814         */
1815        if (prepare_revision_walk(sb->revs))
1816                die(_("revision walk setup failed"));
1817
1818        if (sb->reverse && sb->revs->first_parent_only) {
1819                struct commit *c = final_commit;
1820
1821                sb->revs->children.name = "children";
1822                while (c->parents &&
1823                       oidcmp(&c->object.oid, &sb->final->object.oid)) {
1824                        struct commit_list *l = xcalloc(1, sizeof(*l));
1825
1826                        l->item = c;
1827                        if (add_decoration(&sb->revs->children,
1828                                           &c->parents->item->object, l))
1829                                BUG("not unique item in first-parent chain");
1830                        c = c->parents->item;
1831                }
1832
1833                if (oidcmp(&c->object.oid, &sb->final->object.oid))
1834                        die(_("--reverse --first-parent together require range along first-parent chain"));
1835        }
1836
1837        if (is_null_oid(&sb->final->object.oid)) {
1838                o = get_blame_suspects(sb->final);
1839                sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
1840                sb->final_buf_size = o->file.size;
1841        }
1842        else {
1843                o = get_origin(sb->final, path);
1844                if (fill_blob_sha1_and_mode(o))
1845                        die(_("no such path %s in %s"), path, final_commit_name);
1846
1847                if (sb->revs->diffopt.flags.allow_textconv &&
1848                    textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
1849                                    &sb->final_buf_size))
1850                        ;
1851                else
1852                        sb->final_buf = read_object_file(&o->blob_oid, &type,
1853                                                         &sb->final_buf_size);
1854
1855                if (!sb->final_buf)
1856                        die(_("cannot read blob %s for path %s"),
1857                            oid_to_hex(&o->blob_oid),
1858                            path);
1859        }
1860        sb->num_read_blob++;
1861        prepare_lines(sb);
1862
1863        if (orig)
1864                *orig = o;
1865
1866        free((char *)final_commit_name);
1867}
1868
1869
1870
1871struct blame_entry *blame_entry_prepend(struct blame_entry *head,
1872                                        long start, long end,
1873                                        struct blame_origin *o)
1874{
1875        struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
1876        new_head->lno = start;
1877        new_head->num_lines = end - start;
1878        new_head->suspect = o;
1879        new_head->s_lno = start;
1880        new_head->next = head;
1881        blame_origin_incref(o);
1882        return new_head;
1883}