82868328ac1fb6b951e2124db7a8563365fe34e3
   1/*
   2 * Pickaxe
   3 *
   4 * Copyright (c) 2006, Junio C Hamano
   5 */
   6
   7#include "cache.h"
   8#include "builtin.h"
   9#include "blob.h"
  10#include "commit.h"
  11#include "tag.h"
  12#include "tree-walk.h"
  13#include "diff.h"
  14#include "diffcore.h"
  15#include "revision.h"
  16#include "xdiff-interface.h"
  17
  18#include <time.h>
  19#include <sys/time.h>
  20
  21static char pickaxe_usage[] =
  22"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
  23"  -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
  24"  -l, --long          Show long commit SHA1 (Default: off)\n"
  25"  -t, --time          Show raw timestamp (Default: off)\n"
  26"  -f, --show-name     Show original filename (Default: auto)\n"
  27"  -n, --show-number   Show original linenumber (Default: off)\n"
  28"  -p, --porcelain     Show in a format designed for machine consumption\n"
  29"  -L n,m              Process only line range n,m, counting from 1\n"
  30"  -M, -C              Find line movements within and across files\n"
  31"  -S revs-file        Use revisions from revs-file instead of calling git-rev-list\n";
  32
  33static int longest_file;
  34static int longest_author;
  35static int max_orig_digits;
  36static int max_digits;
  37static int max_score_digits;
  38
  39#ifndef DEBUG
  40#define DEBUG 0
  41#endif
  42
  43#define PICKAXE_BLAME_MOVE              01
  44#define PICKAXE_BLAME_COPY              02
  45#define PICKAXE_BLAME_COPY_HARDER       04
  46
  47/*
  48 * blame for a blame_entry with score lower than these thresholds
  49 * is not passed to the parent using move/copy logic.
  50 */
  51static unsigned blame_move_score;
  52static unsigned blame_copy_score;
  53#define BLAME_DEFAULT_MOVE_SCORE        20
  54#define BLAME_DEFAULT_COPY_SCORE        40
  55
  56/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
  57#define METAINFO_SHOWN          (1u<<12)
  58#define MORE_THAN_ONE_PATH      (1u<<13)
  59
  60/*
  61 * One blob in a commit that is being suspected
  62 */
  63struct origin {
  64        int refcnt;
  65        struct commit *commit;
  66        unsigned char blob_sha1[20];
  67        char path[FLEX_ARRAY];
  68};
  69
  70static inline struct origin *origin_incref(struct origin *o)
  71{
  72        if (o)
  73                o->refcnt++;
  74        return o;
  75}
  76
  77static void origin_decref(struct origin *o)
  78{
  79        if (o && --o->refcnt <= 0) {
  80                memset(o, 0, sizeof(*o));
  81                free(o);
  82        }
  83}
  84
  85struct blame_entry {
  86        struct blame_entry *prev;
  87        struct blame_entry *next;
  88
  89        /* the first line of this group in the final image;
  90         * internally all line numbers are 0 based.
  91         */
  92        int lno;
  93
  94        /* how many lines this group has */
  95        int num_lines;
  96
  97        /* the commit that introduced this group into the final image */
  98        struct origin *suspect;
  99
 100        /* true if the suspect is truly guilty; false while we have not
 101         * checked if the group came from one of its parents.
 102         */
 103        char guilty;
 104
 105        /* the line number of the first line of this group in the
 106         * suspect's file; internally all line numbers are 0 based.
 107         */
 108        int s_lno;
 109
 110        /* how significant this entry is -- cached to avoid
 111         * scanning the lines over and over
 112         */
 113        unsigned score;
 114};
 115
 116struct scoreboard {
 117        /* the final commit (i.e. where we started digging from) */
 118        struct commit *final;
 119
 120        const char *path;
 121
 122        /* the contents in the final; pointed into by buf pointers of
 123         * blame_entries
 124         */
 125        const char *final_buf;
 126        unsigned long final_buf_size;
 127
 128        /* linked list of blames */
 129        struct blame_entry *ent;
 130
 131        /* look-up a line in the final buffer */
 132        int num_lines;
 133        int *lineno;
 134};
 135
 136static int cmp_suspect(struct origin *a, struct origin *b)
 137{
 138        int cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);
 139        if (cmp)
 140                return cmp;
 141        return strcmp(a->path, b->path);
 142}
 143
 144static void sanity_check_refcnt(struct scoreboard *);
 145
 146static void coalesce(struct scoreboard *sb)
 147{
 148        struct blame_entry *ent, *next;
 149
 150        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
 151                if (!cmp_suspect(ent->suspect, next->suspect) &&
 152                    ent->guilty == next->guilty &&
 153                    ent->s_lno + ent->num_lines == next->s_lno) {
 154                        ent->num_lines += next->num_lines;
 155                        ent->next = next->next;
 156                        if (ent->next)
 157                                ent->next->prev = ent;
 158                        origin_decref(next->suspect);
 159                        free(next);
 160                        ent->score = 0;
 161                        next = ent; /* again */
 162                }
 163        }
 164
 165        if (DEBUG) /* sanity */
 166                sanity_check_refcnt(sb);
 167}
 168
 169static struct origin *get_origin(struct scoreboard *sb,
 170                                 struct commit *commit,
 171                                 const char *path)
 172{
 173        struct blame_entry *e;
 174        struct origin *o;
 175
 176        for (e = sb->ent; e; e = e->next) {
 177                if (e->suspect->commit == commit &&
 178                    !strcmp(e->suspect->path, path))
 179                        return origin_incref(e->suspect);
 180        }
 181        o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
 182        o->commit = commit;
 183        o->refcnt = 1;
 184        strcpy(o->path, path);
 185        return o;
 186}
 187
 188static int fill_blob_sha1(struct origin *origin)
 189{
 190        unsigned mode;
 191        char type[10];
 192
 193        if (!is_null_sha1(origin->blob_sha1))
 194                return 0;
 195        if (get_tree_entry(origin->commit->object.sha1,
 196                           origin->path,
 197                           origin->blob_sha1, &mode))
 198                goto error_out;
 199        if (sha1_object_info(origin->blob_sha1, type, NULL) ||
 200            strcmp(type, blob_type))
 201                goto error_out;
 202        return 0;
 203 error_out:
 204        hashclr(origin->blob_sha1);
 205        return -1;
 206}
 207
 208static struct origin *find_origin(struct scoreboard *sb,
 209                                  struct commit *parent,
 210                                  struct origin *origin)
 211{
 212        struct origin *porigin = NULL;
 213        struct diff_options diff_opts;
 214        int i;
 215        const char *paths[2];
 216
 217        /* See if the origin->path is different between parent
 218         * and origin first.  Most of the time they are the
 219         * same and diff-tree is fairly efficient about this.
 220         */
 221        diff_setup(&diff_opts);
 222        diff_opts.recursive = 1;
 223        diff_opts.detect_rename = 0;
 224        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 225        paths[0] = origin->path;
 226        paths[1] = NULL;
 227
 228        diff_tree_setup_paths(paths, &diff_opts);
 229        if (diff_setup_done(&diff_opts) < 0)
 230                die("diff-setup");
 231        diff_tree_sha1(parent->tree->object.sha1,
 232                       origin->commit->tree->object.sha1,
 233                       "", &diff_opts);
 234        diffcore_std(&diff_opts);
 235
 236        /* It is either one entry that says "modified", or "created",
 237         * or nothing.
 238         */
 239        if (!diff_queued_diff.nr) {
 240                /* The path is the same as parent */
 241                porigin = get_origin(sb, parent, origin->path);
 242                hashcpy(porigin->blob_sha1, origin->blob_sha1);
 243        }
 244        else if (diff_queued_diff.nr != 1)
 245                die("internal error in pickaxe::find_origin");
 246        else {
 247                struct diff_filepair *p = diff_queued_diff.queue[0];
 248                switch (p->status) {
 249                default:
 250                        die("internal error in pickaxe::find_origin (%c)",
 251                            p->status);
 252                case 'M':
 253                        porigin = get_origin(sb, parent, origin->path);
 254                        hashcpy(porigin->blob_sha1, p->one->sha1);
 255                        break;
 256                case 'A':
 257                case 'T':
 258                        /* Did not exist in parent, or type changed */
 259                        break;
 260                }
 261        }
 262        diff_flush(&diff_opts);
 263        if (porigin)
 264                return porigin;
 265
 266        /* Otherwise we would look for a rename */
 267
 268        diff_setup(&diff_opts);
 269        diff_opts.recursive = 1;
 270        diff_opts.detect_rename = DIFF_DETECT_RENAME;
 271        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 272        paths[0] = NULL;
 273        diff_tree_setup_paths(paths, &diff_opts);
 274        if (diff_setup_done(&diff_opts) < 0)
 275                die("diff-setup");
 276        diff_tree_sha1(parent->tree->object.sha1,
 277                       origin->commit->tree->object.sha1,
 278                       "", &diff_opts);
 279        diffcore_std(&diff_opts);
 280
 281        for (i = 0; i < diff_queued_diff.nr; i++) {
 282                struct diff_filepair *p = diff_queued_diff.queue[i];
 283                if ((p->status == 'R' || p->status == 'C') &&
 284                    !strcmp(p->two->path, origin->path)) {
 285                        porigin = get_origin(sb, parent, p->one->path);
 286                        hashcpy(porigin->blob_sha1, p->one->sha1);
 287                        break;
 288                }
 289        }
 290        diff_flush(&diff_opts);
 291        return porigin;
 292}
 293
 294struct chunk {
 295        /* line number in postimage; up to but not including this
 296         * line is the same as preimage
 297         */
 298        int same;
 299
 300        /* preimage line number after this chunk */
 301        int p_next;
 302
 303        /* postimage line number after this chunk */
 304        int t_next;
 305};
 306
 307struct patch {
 308        struct chunk *chunks;
 309        int num;
 310};
 311
 312struct blame_diff_state {
 313        struct xdiff_emit_state xm;
 314        struct patch *ret;
 315        unsigned hunk_post_context;
 316        unsigned hunk_in_pre_context : 1;
 317};
 318
 319static void process_u_diff(void *state_, char *line, unsigned long len)
 320{
 321        struct blame_diff_state *state = state_;
 322        struct chunk *chunk;
 323        int off1, off2, len1, len2, num;
 324
 325        num = state->ret->num;
 326        if (len < 4 || line[0] != '@' || line[1] != '@') {
 327                if (state->hunk_in_pre_context && line[0] == ' ')
 328                        state->ret->chunks[num - 1].same++;
 329                else {
 330                        state->hunk_in_pre_context = 0;
 331                        if (line[0] == ' ')
 332                                state->hunk_post_context++;
 333                        else
 334                                state->hunk_post_context = 0;
 335                }
 336                return;
 337        }
 338
 339        if (num && state->hunk_post_context) {
 340                chunk = &state->ret->chunks[num - 1];
 341                chunk->p_next -= state->hunk_post_context;
 342                chunk->t_next -= state->hunk_post_context;
 343        }
 344        state->ret->num = ++num;
 345        state->ret->chunks = xrealloc(state->ret->chunks,
 346                                      sizeof(struct chunk) * num);
 347        chunk = &state->ret->chunks[num - 1];
 348        if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) {
 349                state->ret->num--;
 350                return;
 351        }
 352
 353        /* Line numbers in patch output are one based. */
 354        off1--;
 355        off2--;
 356
 357        chunk->same = len2 ? off2 : (off2 + 1);
 358
 359        chunk->p_next = off1 + (len1 ? len1 : 1);
 360        chunk->t_next = chunk->same + len2;
 361        state->hunk_in_pre_context = 1;
 362        state->hunk_post_context = 0;
 363}
 364
 365static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
 366                                    int context)
 367{
 368        struct blame_diff_state state;
 369        xpparam_t xpp;
 370        xdemitconf_t xecfg;
 371        xdemitcb_t ecb;
 372
 373        xpp.flags = XDF_NEED_MINIMAL;
 374        xecfg.ctxlen = context;
 375        xecfg.flags = 0;
 376        ecb.outf = xdiff_outf;
 377        ecb.priv = &state;
 378        memset(&state, 0, sizeof(state));
 379        state.xm.consume = process_u_diff;
 380        state.ret = xmalloc(sizeof(struct patch));
 381        state.ret->chunks = NULL;
 382        state.ret->num = 0;
 383
 384        xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
 385
 386        if (state.ret->num) {
 387                struct chunk *chunk;
 388                chunk = &state.ret->chunks[state.ret->num - 1];
 389                chunk->p_next -= state.hunk_post_context;
 390                chunk->t_next -= state.hunk_post_context;
 391        }
 392        return state.ret;
 393}
 394
 395static struct patch *get_patch(struct origin *parent, struct origin *origin)
 396{
 397        mmfile_t file_p, file_o;
 398        char type[10];
 399        char *blob_p, *blob_o;
 400        struct patch *patch;
 401
 402        blob_p = read_sha1_file(parent->blob_sha1, type,
 403                                (unsigned long *) &file_p.size);
 404        blob_o = read_sha1_file(origin->blob_sha1, type,
 405                                (unsigned long *) &file_o.size);
 406        file_p.ptr = blob_p;
 407        file_o.ptr = blob_o;
 408        if (!file_p.ptr || !file_o.ptr) {
 409                free(blob_p);
 410                free(blob_o);
 411                return NULL;
 412        }
 413
 414        patch = compare_buffer(&file_p, &file_o, 0);
 415        free(blob_p);
 416        free(blob_o);
 417        return patch;
 418}
 419
 420static void free_patch(struct patch *p)
 421{
 422        free(p->chunks);
 423        free(p);
 424}
 425
 426static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
 427{
 428        struct blame_entry *ent, *prev = NULL;
 429
 430        origin_incref(e->suspect);
 431
 432        for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
 433                prev = ent;
 434
 435        /* prev, if not NULL, is the last one that is below e */
 436        e->prev = prev;
 437        if (prev) {
 438                e->next = prev->next;
 439                prev->next = e;
 440        }
 441        else {
 442                e->next = sb->ent;
 443                sb->ent = e;
 444        }
 445        if (e->next)
 446                e->next->prev = e;
 447}
 448
 449static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
 450{
 451        struct blame_entry *p, *n;
 452
 453        p = dst->prev;
 454        n = dst->next;
 455        origin_incref(src->suspect);
 456        origin_decref(dst->suspect);
 457        memcpy(dst, src, sizeof(*src));
 458        dst->prev = p;
 459        dst->next = n;
 460        dst->score = 0;
 461}
 462
 463static const char *nth_line(struct scoreboard *sb, int lno)
 464{
 465        return sb->final_buf + sb->lineno[lno];
 466}
 467
 468static void split_overlap(struct blame_entry *split,
 469                          struct blame_entry *e,
 470                          int tlno, int plno, int same,
 471                          struct origin *parent)
 472{
 473        /* it is known that lines between tlno to same came from
 474         * parent, and e has an overlap with that range.  it also is
 475         * known that parent's line plno corresponds to e's line tlno.
 476         *
 477         *                <---- e ----->
 478         *                   <------>
 479         *                   <------------>
 480         *             <------------>
 481         *             <------------------>
 482         *
 483         * Potentially we need to split e into three parts; before
 484         * this chunk, the chunk to be blamed for parent, and after
 485         * that portion.
 486         */
 487        int chunk_end_lno;
 488        memset(split, 0, sizeof(struct blame_entry [3]));
 489
 490        if (e->s_lno < tlno) {
 491                /* there is a pre-chunk part not blamed on parent */
 492                split[0].suspect = origin_incref(e->suspect);
 493                split[0].lno = e->lno;
 494                split[0].s_lno = e->s_lno;
 495                split[0].num_lines = tlno - e->s_lno;
 496                split[1].lno = e->lno + tlno - e->s_lno;
 497                split[1].s_lno = plno;
 498        }
 499        else {
 500                split[1].lno = e->lno;
 501                split[1].s_lno = plno + (e->s_lno - tlno);
 502        }
 503
 504        if (same < e->s_lno + e->num_lines) {
 505                /* there is a post-chunk part not blamed on parent */
 506                split[2].suspect = origin_incref(e->suspect);
 507                split[2].lno = e->lno + (same - e->s_lno);
 508                split[2].s_lno = e->s_lno + (same - e->s_lno);
 509                split[2].num_lines = e->s_lno + e->num_lines - same;
 510                chunk_end_lno = split[2].lno;
 511        }
 512        else
 513                chunk_end_lno = e->lno + e->num_lines;
 514        split[1].num_lines = chunk_end_lno - split[1].lno;
 515
 516        if (split[1].num_lines < 1)
 517                return;
 518        split[1].suspect = origin_incref(parent);
 519}
 520
 521static void split_blame(struct scoreboard *sb,
 522                        struct blame_entry *split,
 523                        struct blame_entry *e)
 524{
 525        struct blame_entry *new_entry;
 526
 527        if (split[0].suspect && split[2].suspect) {
 528                /* we need to split e into two and add another for parent */
 529                dup_entry(e, &split[0]);
 530
 531                new_entry = xmalloc(sizeof(*new_entry));
 532                memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
 533                add_blame_entry(sb, new_entry);
 534
 535                new_entry = xmalloc(sizeof(*new_entry));
 536                memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
 537                add_blame_entry(sb, new_entry);
 538        }
 539        else if (!split[0].suspect && !split[2].suspect)
 540                /* parent covers the entire area */
 541                dup_entry(e, &split[1]);
 542        else if (split[0].suspect) {
 543                dup_entry(e, &split[0]);
 544
 545                new_entry = xmalloc(sizeof(*new_entry));
 546                memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
 547                add_blame_entry(sb, new_entry);
 548        }
 549        else {
 550                dup_entry(e, &split[1]);
 551
 552                new_entry = xmalloc(sizeof(*new_entry));
 553                memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
 554                add_blame_entry(sb, new_entry);
 555        }
 556
 557        if (DEBUG) { /* sanity */
 558                struct blame_entry *ent;
 559                int lno = sb->ent->lno, corrupt = 0;
 560
 561                for (ent = sb->ent; ent; ent = ent->next) {
 562                        if (lno != ent->lno)
 563                                corrupt = 1;
 564                        if (ent->s_lno < 0)
 565                                corrupt = 1;
 566                        lno += ent->num_lines;
 567                }
 568                if (corrupt) {
 569                        lno = sb->ent->lno;
 570                        for (ent = sb->ent; ent; ent = ent->next) {
 571                                printf("L %8d l %8d n %8d\n",
 572                                       lno, ent->lno, ent->num_lines);
 573                                lno = ent->lno + ent->num_lines;
 574                        }
 575                        die("oops");
 576                }
 577        }
 578}
 579
 580static void decref_split(struct blame_entry *split)
 581{
 582        int i;
 583
 584        for (i = 0; i < 3; i++)
 585                origin_decref(split[i].suspect);
 586}
 587
 588static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
 589                          int tlno, int plno, int same,
 590                          struct origin *parent)
 591{
 592        struct blame_entry split[3];
 593
 594        split_overlap(split, e, tlno, plno, same, parent);
 595        if (split[1].suspect)
 596                split_blame(sb, split, e);
 597        decref_split(split);
 598}
 599
 600static int find_last_in_target(struct scoreboard *sb, struct origin *target)
 601{
 602        struct blame_entry *e;
 603        int last_in_target = -1;
 604
 605        for (e = sb->ent; e; e = e->next) {
 606                if (e->guilty || cmp_suspect(e->suspect, target))
 607                        continue;
 608                if (last_in_target < e->s_lno + e->num_lines)
 609                        last_in_target = e->s_lno + e->num_lines;
 610        }
 611        return last_in_target;
 612}
 613
 614static void blame_chunk(struct scoreboard *sb,
 615                        int tlno, int plno, int same,
 616                        struct origin *target, struct origin *parent)
 617{
 618        struct blame_entry *e;
 619
 620        for (e = sb->ent; e; e = e->next) {
 621                if (e->guilty || cmp_suspect(e->suspect, target))
 622                        continue;
 623                if (same <= e->s_lno)
 624                        continue;
 625                if (tlno < e->s_lno + e->num_lines)
 626                        blame_overlap(sb, e, tlno, plno, same, parent);
 627        }
 628}
 629
 630static int pass_blame_to_parent(struct scoreboard *sb,
 631                                struct origin *target,
 632                                struct origin *parent)
 633{
 634        int i, last_in_target, plno, tlno;
 635        struct patch *patch;
 636
 637        last_in_target = find_last_in_target(sb, target);
 638        if (last_in_target < 0)
 639                return 1; /* nothing remains for this target */
 640
 641        patch = get_patch(parent, target);
 642        plno = tlno = 0;
 643        for (i = 0; i < patch->num; i++) {
 644                struct chunk *chunk = &patch->chunks[i];
 645
 646                blame_chunk(sb, tlno, plno, chunk->same, target, parent);
 647                plno = chunk->p_next;
 648                tlno = chunk->t_next;
 649        }
 650        /* rest (i.e. anything above tlno) are the same as parent */
 651        blame_chunk(sb, tlno, plno, last_in_target, target, parent);
 652
 653        free_patch(patch);
 654        return 0;
 655}
 656
 657static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
 658{
 659        unsigned score;
 660        const char *cp, *ep;
 661
 662        if (e->score)
 663                return e->score;
 664
 665        score = 1;
 666        cp = nth_line(sb, e->lno);
 667        ep = nth_line(sb, e->lno + e->num_lines);
 668        while (cp < ep) {
 669                unsigned ch = *((unsigned char *)cp);
 670                if (isalnum(ch))
 671                        score++;
 672                cp++;
 673        }
 674        e->score = score;
 675        return score;
 676}
 677
 678static void copy_split_if_better(struct scoreboard *sb,
 679                                 struct blame_entry *best_so_far,
 680                                 struct blame_entry *this)
 681{
 682        int i;
 683
 684        if (!this[1].suspect)
 685                return;
 686        if (best_so_far[1].suspect) {
 687                if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
 688                        return;
 689        }
 690
 691        for (i = 0; i < 3; i++)
 692                origin_incref(this[i].suspect);
 693        decref_split(best_so_far);
 694        memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
 695}
 696
 697static void find_copy_in_blob(struct scoreboard *sb,
 698                              struct blame_entry *ent,
 699                              struct origin *parent,
 700                              struct blame_entry *split,
 701                              mmfile_t *file_p)
 702{
 703        const char *cp;
 704        int cnt;
 705        mmfile_t file_o;
 706        struct patch *patch;
 707        int i, plno, tlno;
 708
 709        cp = nth_line(sb, ent->lno);
 710        file_o.ptr = (char*) cp;
 711        cnt = ent->num_lines;
 712
 713        while (cnt && cp < sb->final_buf + sb->final_buf_size) {
 714                if (*cp++ == '\n')
 715                        cnt--;
 716        }
 717        file_o.size = cp - file_o.ptr;
 718
 719        patch = compare_buffer(file_p, &file_o, 1);
 720
 721        memset(split, 0, sizeof(struct blame_entry [3]));
 722        plno = tlno = 0;
 723        for (i = 0; i < patch->num; i++) {
 724                struct chunk *chunk = &patch->chunks[i];
 725
 726                /* tlno to chunk->same are the same as ent */
 727                if (ent->num_lines <= tlno)
 728                        break;
 729                if (tlno < chunk->same) {
 730                        struct blame_entry this[3];
 731                        split_overlap(this, ent,
 732                                      tlno + ent->s_lno, plno,
 733                                      chunk->same + ent->s_lno,
 734                                      parent);
 735                        copy_split_if_better(sb, split, this);
 736                        decref_split(this);
 737                }
 738                plno = chunk->p_next;
 739                tlno = chunk->t_next;
 740        }
 741        free_patch(patch);
 742}
 743
 744static int find_move_in_parent(struct scoreboard *sb,
 745                               struct origin *target,
 746                               struct origin *parent)
 747{
 748        int last_in_target;
 749        struct blame_entry *e, split[3];
 750        mmfile_t file_p;
 751        char type[10];
 752        char *blob_p;
 753
 754        last_in_target = find_last_in_target(sb, target);
 755        if (last_in_target < 0)
 756                return 1; /* nothing remains for this target */
 757
 758        blob_p = read_sha1_file(parent->blob_sha1, type,
 759                                (unsigned long *) &file_p.size);
 760        file_p.ptr = blob_p;
 761        if (!file_p.ptr) {
 762                free(blob_p);
 763                return 0;
 764        }
 765
 766        for (e = sb->ent; e; e = e->next) {
 767                if (e->guilty || cmp_suspect(e->suspect, target))
 768                        continue;
 769                find_copy_in_blob(sb, e, parent, split, &file_p);
 770                if (split[1].suspect &&
 771                    blame_move_score < ent_score(sb, &split[1]))
 772                        split_blame(sb, split, e);
 773                decref_split(split);
 774        }
 775        free(blob_p);
 776        return 0;
 777}
 778
 779static int find_copy_in_parent(struct scoreboard *sb,
 780                               struct origin *target,
 781                               struct commit *parent,
 782                               struct origin *porigin,
 783                               int opt)
 784{
 785        struct diff_options diff_opts;
 786        const char *paths[1];
 787        struct blame_entry *e;
 788        int i, j;
 789        struct blame_list {
 790                struct blame_entry *ent;
 791                struct blame_entry split[3];
 792        } *blame_list;
 793        int num_ents;
 794
 795        /* Count the number of entries the target is suspected for,
 796         * and prepare a list of entry and the best split.
 797         */
 798        for (e = sb->ent, num_ents = 0; e; e = e->next)
 799                if (!e->guilty && !cmp_suspect(e->suspect, target))
 800                        num_ents++;
 801        if (!num_ents)
 802                return 1; /* nothing remains for this target */
 803
 804        blame_list = xcalloc(num_ents, sizeof(struct blame_list));
 805        for (e = sb->ent, i = 0; e; e = e->next)
 806                if (!e->guilty && !cmp_suspect(e->suspect, target))
 807                        blame_list[i++].ent = e;
 808
 809        diff_setup(&diff_opts);
 810        diff_opts.recursive = 1;
 811        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 812
 813        /* Try "find copies harder" on new path */
 814        if ((opt & PICKAXE_BLAME_COPY_HARDER) &&
 815            (!porigin || strcmp(target->path, porigin->path))) {
 816                diff_opts.detect_rename = DIFF_DETECT_COPY;
 817                diff_opts.find_copies_harder = 1;
 818        }
 819        paths[0] = NULL;
 820        diff_tree_setup_paths(paths, &diff_opts);
 821        if (diff_setup_done(&diff_opts) < 0)
 822                die("diff-setup");
 823        diff_tree_sha1(parent->tree->object.sha1,
 824                       target->commit->tree->object.sha1,
 825                       "", &diff_opts);
 826        diffcore_std(&diff_opts);
 827
 828        for (i = 0; i < diff_queued_diff.nr; i++) {
 829                struct diff_filepair *p = diff_queued_diff.queue[i];
 830                struct origin *norigin;
 831                mmfile_t file_p;
 832                char type[10];
 833                char *blob;
 834                struct blame_entry this[3];
 835
 836                if (!DIFF_FILE_VALID(p->one))
 837                        continue; /* does not exist in parent */
 838                if (porigin && !strcmp(p->one->path, porigin->path))
 839                        /* find_move already dealt with this path */
 840                        continue;
 841
 842                norigin = get_origin(sb, parent, p->one->path);
 843                hashcpy(norigin->blob_sha1, p->one->sha1);
 844                blob = read_sha1_file(norigin->blob_sha1, type,
 845                                      (unsigned long *) &file_p.size);
 846                file_p.ptr = blob;
 847                if (!file_p.ptr)
 848                        continue;
 849
 850                for (j = 0; j < num_ents; j++) {
 851                        find_copy_in_blob(sb, blame_list[j].ent, norigin,
 852                                          this, &file_p);
 853                        copy_split_if_better(sb, blame_list[j].split,
 854                                             this);
 855                        decref_split(this);
 856                }
 857                free(blob);
 858                origin_decref(norigin);
 859        }
 860        diff_flush(&diff_opts);
 861
 862        for (j = 0; j < num_ents; j++) {
 863                struct blame_entry *split = blame_list[j].split;
 864                if (split[1].suspect &&
 865                    blame_copy_score < ent_score(sb, &split[1]))
 866                        split_blame(sb, split, blame_list[j].ent);
 867                decref_split(split);
 868        }
 869        free(blame_list);
 870
 871        return 0;
 872}
 873
 874#define MAXPARENT 16
 875
 876static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
 877{
 878        int i;
 879        struct commit *commit = origin->commit;
 880        struct commit_list *parent;
 881        struct origin *parent_origin[MAXPARENT], *porigin;
 882
 883        memset(parent_origin, 0, sizeof(parent_origin));
 884        for (i = 0, parent = commit->parents;
 885             i < MAXPARENT && parent;
 886             parent = parent->next, i++) {
 887                struct commit *p = parent->item;
 888
 889                if (parse_commit(p))
 890                        continue;
 891                porigin = find_origin(sb, parent->item, origin);
 892                if (!porigin)
 893                        continue;
 894                if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
 895                        struct blame_entry *e;
 896                        for (e = sb->ent; e; e = e->next)
 897                                if (e->suspect == origin) {
 898                                        origin_incref(porigin);
 899                                        origin_decref(e->suspect);
 900                                        e->suspect = porigin;
 901                                }
 902                        origin_decref(porigin);
 903                        goto finish;
 904                }
 905                parent_origin[i] = porigin;
 906        }
 907
 908        for (i = 0, parent = commit->parents;
 909             i < MAXPARENT && parent;
 910             parent = parent->next, i++) {
 911                struct origin *porigin = parent_origin[i];
 912                if (!porigin)
 913                        continue;
 914                if (pass_blame_to_parent(sb, origin, porigin))
 915                        goto finish;
 916        }
 917
 918        /*
 919         * Optionally run "miff" to find moves in parents' files here.
 920         */
 921        if (opt & PICKAXE_BLAME_MOVE)
 922                for (i = 0, parent = commit->parents;
 923                     i < MAXPARENT && parent;
 924                     parent = parent->next, i++) {
 925                        struct origin *porigin = parent_origin[i];
 926                        if (!porigin)
 927                                continue;
 928                        if (find_move_in_parent(sb, origin, porigin))
 929                                goto finish;
 930                }
 931
 932        /*
 933         * Optionally run "ciff" to find copies from parents' files here.
 934         */
 935        if (opt & PICKAXE_BLAME_COPY)
 936                for (i = 0, parent = commit->parents;
 937                     i < MAXPARENT && parent;
 938                     parent = parent->next, i++) {
 939                        struct origin *porigin = parent_origin[i];
 940                        if (find_copy_in_parent(sb, origin, parent->item,
 941                                                porigin, opt))
 942                                goto finish;
 943                }
 944
 945 finish:
 946        for (i = 0; i < MAXPARENT; i++)
 947                origin_decref(parent_origin[i]);
 948}
 949
 950static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
 951{
 952        while (1) {
 953                struct blame_entry *ent;
 954                struct commit *commit;
 955                struct origin *suspect = NULL;
 956
 957                /* find one suspect to break down */
 958                for (ent = sb->ent; !suspect && ent; ent = ent->next)
 959                        if (!ent->guilty)
 960                                suspect = ent->suspect;
 961                if (!suspect)
 962                        return; /* all done */
 963
 964                origin_incref(suspect);
 965                commit = suspect->commit;
 966                parse_commit(commit);
 967                if (!(commit->object.flags & UNINTERESTING) &&
 968                    !(revs->max_age != -1 && commit->date  < revs->max_age))
 969                        pass_blame(sb, suspect, opt);
 970
 971                /* Take responsibility for the remaining entries */
 972                for (ent = sb->ent; ent; ent = ent->next)
 973                        if (!cmp_suspect(ent->suspect, suspect))
 974                                ent->guilty = 1;
 975                origin_decref(suspect);
 976
 977                if (DEBUG) /* sanity */
 978                        sanity_check_refcnt(sb);
 979        }
 980}
 981
 982static const char *format_time(unsigned long time, const char *tz_str,
 983                               int show_raw_time)
 984{
 985        static char time_buf[128];
 986        time_t t = time;
 987        int minutes, tz;
 988        struct tm *tm;
 989
 990        if (show_raw_time) {
 991                sprintf(time_buf, "%lu %s", time, tz_str);
 992                return time_buf;
 993        }
 994
 995        tz = atoi(tz_str);
 996        minutes = tz < 0 ? -tz : tz;
 997        minutes = (minutes / 100)*60 + (minutes % 100);
 998        minutes = tz < 0 ? -minutes : minutes;
 999        t = time + minutes * 60;
1000        tm = gmtime(&t);
1001
1002        strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1003        strcat(time_buf, tz_str);
1004        return time_buf;
1005}
1006
1007struct commit_info
1008{
1009        char *author;
1010        char *author_mail;
1011        unsigned long author_time;
1012        char *author_tz;
1013
1014        /* filled only when asked for details */
1015        char *committer;
1016        char *committer_mail;
1017        unsigned long committer_time;
1018        char *committer_tz;
1019
1020        char *summary;
1021};
1022
1023static void get_ac_line(const char *inbuf, const char *what,
1024                        int bufsz, char *person, char **mail,
1025                        unsigned long *time, char **tz)
1026{
1027        int len;
1028        char *tmp, *endp;
1029
1030        tmp = strstr(inbuf, what);
1031        if (!tmp)
1032                goto error_out;
1033        tmp += strlen(what);
1034        endp = strchr(tmp, '\n');
1035        if (!endp)
1036                len = strlen(tmp);
1037        else
1038                len = endp - tmp;
1039        if (bufsz <= len) {
1040        error_out:
1041                /* Ugh */
1042                person = *mail = *tz = "(unknown)";
1043                *time = 0;
1044                return;
1045        }
1046        memcpy(person, tmp, len);
1047
1048        tmp = person;
1049        tmp += len;
1050        *tmp = 0;
1051        while (*tmp != ' ')
1052                tmp--;
1053        *tz = tmp+1;
1054
1055        *tmp = 0;
1056        while (*tmp != ' ')
1057                tmp--;
1058        *time = strtoul(tmp, NULL, 10);
1059
1060        *tmp = 0;
1061        while (*tmp != ' ')
1062                tmp--;
1063        *mail = tmp + 1;
1064        *tmp = 0;
1065}
1066
1067static void get_commit_info(struct commit *commit,
1068                            struct commit_info *ret,
1069                            int detailed)
1070{
1071        int len;
1072        char *tmp, *endp;
1073        static char author_buf[1024];
1074        static char committer_buf[1024];
1075        static char summary_buf[1024];
1076
1077        /* We've operated without save_commit_buffer, so
1078         * we now need to populate them for output.
1079         */
1080        if (!commit->buffer) {
1081                char type[20];
1082                unsigned long size;
1083                commit->buffer =
1084                        read_sha1_file(commit->object.sha1, type, &size);
1085        }
1086        ret->author = author_buf;
1087        get_ac_line(commit->buffer, "\nauthor ",
1088                    sizeof(author_buf), author_buf, &ret->author_mail,
1089                    &ret->author_time, &ret->author_tz);
1090
1091        if (!detailed)
1092                return;
1093
1094        ret->committer = committer_buf;
1095        get_ac_line(commit->buffer, "\ncommitter ",
1096                    sizeof(committer_buf), committer_buf, &ret->committer_mail,
1097                    &ret->committer_time, &ret->committer_tz);
1098
1099        ret->summary = summary_buf;
1100        tmp = strstr(commit->buffer, "\n\n");
1101        if (!tmp) {
1102        error_out:
1103                sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1104                return;
1105        }
1106        tmp += 2;
1107        endp = strchr(tmp, '\n');
1108        if (!endp)
1109                goto error_out;
1110        len = endp - tmp;
1111        if (len >= sizeof(summary_buf))
1112                goto error_out;
1113        memcpy(summary_buf, tmp, len);
1114        summary_buf[len] = 0;
1115}
1116
1117#define OUTPUT_ANNOTATE_COMPAT  001
1118#define OUTPUT_LONG_OBJECT_NAME 002
1119#define OUTPUT_RAW_TIMESTAMP    004
1120#define OUTPUT_PORCELAIN        010
1121#define OUTPUT_SHOW_NAME        020
1122#define OUTPUT_SHOW_NUMBER      040
1123#define OUTPUT_SHOW_SCORE      0100
1124
1125static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
1126{
1127        int cnt;
1128        const char *cp;
1129        struct origin *suspect = ent->suspect;
1130        char hex[41];
1131
1132        strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1133        printf("%s%c%d %d %d\n",
1134               hex,
1135               ent->guilty ? ' ' : '*', // purely for debugging
1136               ent->s_lno + 1,
1137               ent->lno + 1,
1138               ent->num_lines);
1139        if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1140                struct commit_info ci;
1141                suspect->commit->object.flags |= METAINFO_SHOWN;
1142                get_commit_info(suspect->commit, &ci, 1);
1143                printf("author %s\n", ci.author);
1144                printf("author-mail %s\n", ci.author_mail);
1145                printf("author-time %lu\n", ci.author_time);
1146                printf("author-tz %s\n", ci.author_tz);
1147                printf("committer %s\n", ci.committer);
1148                printf("committer-mail %s\n", ci.committer_mail);
1149                printf("committer-time %lu\n", ci.committer_time);
1150                printf("committer-tz %s\n", ci.committer_tz);
1151                printf("filename %s\n", suspect->path);
1152                printf("summary %s\n", ci.summary);
1153        }
1154        else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
1155                printf("filename %s\n", suspect->path);
1156
1157        cp = nth_line(sb, ent->lno);
1158        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1159                char ch;
1160                if (cnt)
1161                        printf("%s %d %d\n", hex,
1162                               ent->s_lno + 1 + cnt,
1163                               ent->lno + 1 + cnt);
1164                putchar('\t');
1165                do {
1166                        ch = *cp++;
1167                        putchar(ch);
1168                } while (ch != '\n' &&
1169                         cp < sb->final_buf + sb->final_buf_size);
1170        }
1171}
1172
1173static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1174{
1175        int cnt;
1176        const char *cp;
1177        struct origin *suspect = ent->suspect;
1178        struct commit_info ci;
1179        char hex[41];
1180        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1181
1182        get_commit_info(suspect->commit, &ci, 1);
1183        strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1184
1185        cp = nth_line(sb, ent->lno);
1186        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1187                char ch;
1188
1189                printf("%.*s", (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8, hex);
1190                if (opt & OUTPUT_ANNOTATE_COMPAT)
1191                        printf("\t(%10s\t%10s\t%d)", ci.author,
1192                               format_time(ci.author_time, ci.author_tz,
1193                                           show_raw_time),
1194                               ent->lno + 1 + cnt);
1195                else {
1196                        if (opt & OUTPUT_SHOW_SCORE)
1197                                printf(" %*d %02d",
1198                                       max_score_digits, ent->score,
1199                                       ent->suspect->refcnt);
1200                        if (opt & OUTPUT_SHOW_NAME)
1201                                printf(" %-*.*s", longest_file, longest_file,
1202                                       suspect->path);
1203                        if (opt & OUTPUT_SHOW_NUMBER)
1204                                printf(" %*d", max_orig_digits,
1205                                       ent->s_lno + 1 + cnt);
1206                        printf(" (%-*.*s %10s %*d) ",
1207                               longest_author, longest_author, ci.author,
1208                               format_time(ci.author_time, ci.author_tz,
1209                                           show_raw_time),
1210                               max_digits, ent->lno + 1 + cnt);
1211                }
1212                do {
1213                        ch = *cp++;
1214                        putchar(ch);
1215                } while (ch != '\n' &&
1216                         cp < sb->final_buf + sb->final_buf_size);
1217        }
1218}
1219
1220static void output(struct scoreboard *sb, int option)
1221{
1222        struct blame_entry *ent;
1223
1224        if (option & OUTPUT_PORCELAIN) {
1225                for (ent = sb->ent; ent; ent = ent->next) {
1226                        struct blame_entry *oth;
1227                        struct origin *suspect = ent->suspect;
1228                        struct commit *commit = suspect->commit;
1229                        if (commit->object.flags & MORE_THAN_ONE_PATH)
1230                                continue;
1231                        for (oth = ent->next; oth; oth = oth->next) {
1232                                if ((oth->suspect->commit != commit) ||
1233                                    !strcmp(oth->suspect->path, suspect->path))
1234                                        continue;
1235                                commit->object.flags |= MORE_THAN_ONE_PATH;
1236                                break;
1237                        }
1238                }
1239        }
1240
1241        for (ent = sb->ent; ent; ent = ent->next) {
1242                if (option & OUTPUT_PORCELAIN)
1243                        emit_porcelain(sb, ent);
1244                else {
1245                        emit_other(sb, ent, option);
1246                }
1247        }
1248}
1249
1250static int prepare_lines(struct scoreboard *sb)
1251{
1252        const char *buf = sb->final_buf;
1253        unsigned long len = sb->final_buf_size;
1254        int num = 0, incomplete = 0, bol = 1;
1255
1256        if (len && buf[len-1] != '\n')
1257                incomplete++; /* incomplete line at the end */
1258        while (len--) {
1259                if (bol) {
1260                        sb->lineno = xrealloc(sb->lineno,
1261                                              sizeof(int* ) * (num + 1));
1262                        sb->lineno[num] = buf - sb->final_buf;
1263                        bol = 0;
1264                }
1265                if (*buf++ == '\n') {
1266                        num++;
1267                        bol = 1;
1268                }
1269        }
1270        sb->lineno = xrealloc(sb->lineno,
1271                              sizeof(int* ) * (num + incomplete + 1));
1272        sb->lineno[num + incomplete] = buf - sb->final_buf;
1273        sb->num_lines = num + incomplete;
1274        return sb->num_lines;
1275}
1276
1277static int read_ancestry(const char *graft_file)
1278{
1279        FILE *fp = fopen(graft_file, "r");
1280        char buf[1024];
1281        if (!fp)
1282                return -1;
1283        while (fgets(buf, sizeof(buf), fp)) {
1284                /* The format is just "Commit Parent1 Parent2 ...\n" */
1285                int len = strlen(buf);
1286                struct commit_graft *graft = read_graft_line(buf, len);
1287                register_commit_graft(graft, 0);
1288        }
1289        fclose(fp);
1290        return 0;
1291}
1292
1293static int lineno_width(int lines)
1294{
1295        int i, width;
1296
1297        for (width = 1, i = 10; i <= lines + 1; width++)
1298                i *= 10;
1299        return width;
1300}
1301
1302static void find_alignment(struct scoreboard *sb, int *option)
1303{
1304        int longest_src_lines = 0;
1305        int longest_dst_lines = 0;
1306        unsigned largest_score = 0;
1307        struct blame_entry *e;
1308
1309        for (e = sb->ent; e; e = e->next) {
1310                struct origin *suspect = e->suspect;
1311                struct commit_info ci;
1312                int num;
1313
1314                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1315                        suspect->commit->object.flags |= METAINFO_SHOWN;
1316                        get_commit_info(suspect->commit, &ci, 1);
1317                        if (strcmp(suspect->path, sb->path))
1318                                *option |= OUTPUT_SHOW_NAME;
1319                        num = strlen(suspect->path);
1320                        if (longest_file < num)
1321                                longest_file = num;
1322                        num = strlen(ci.author);
1323                        if (longest_author < num)
1324                                longest_author = num;
1325                }
1326                num = e->s_lno + e->num_lines;
1327                if (longest_src_lines < num)
1328                        longest_src_lines = num;
1329                num = e->lno + e->num_lines;
1330                if (longest_dst_lines < num)
1331                        longest_dst_lines = num;
1332                if (largest_score < ent_score(sb, e))
1333                        largest_score = ent_score(sb, e);
1334        }
1335        max_orig_digits = lineno_width(longest_src_lines);
1336        max_digits = lineno_width(longest_dst_lines);
1337        max_score_digits = lineno_width(largest_score);
1338}
1339
1340static void sanity_check_refcnt(struct scoreboard *sb)
1341{
1342        int baa = 0;
1343        struct blame_entry *ent;
1344
1345        for (ent = sb->ent; ent; ent = ent->next) {
1346                /* Nobody should have zero or negative refcnt */
1347                if (ent->suspect->refcnt <= 0)
1348                        baa = 1;
1349        }
1350        for (ent = sb->ent; ent; ent = ent->next) {
1351                /* Mark the ones that haven't been checked */
1352                if (0 < ent->suspect->refcnt)
1353                        ent->suspect->refcnt = -ent->suspect->refcnt;
1354        }
1355        for (ent = sb->ent; ent; ent = ent->next) {
1356                /* then pick each and see if they have the the
1357                 * correct refcnt
1358                 */
1359                int found;
1360                struct blame_entry *e;
1361                struct origin *suspect = ent->suspect;
1362
1363                if (0 < suspect->refcnt)
1364                        continue;
1365                suspect->refcnt = -suspect->refcnt; /* Unmark */
1366                for (found = 0, e = sb->ent; e; e = e->next) {
1367                        if (e->suspect != suspect)
1368                                continue;
1369                        found++;
1370                }
1371                if (suspect->refcnt != found)
1372                        baa = 1;
1373        }
1374        if (baa) {
1375                int opt = 0160;
1376                find_alignment(sb, &opt);
1377                output(sb, opt);
1378                die("Baa!");
1379        }
1380}
1381
1382static int has_path_in_work_tree(const char *path)
1383{
1384        struct stat st;
1385        return !lstat(path, &st);
1386}
1387
1388static unsigned parse_score(const char *arg)
1389{
1390        char *end;
1391        unsigned long score = strtoul(arg, &end, 10);
1392        if (*end)
1393                return 0;
1394        return score;
1395}
1396
1397int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1398{
1399        struct rev_info revs;
1400        const char *path;
1401        struct scoreboard sb;
1402        struct origin *o;
1403        struct blame_entry *ent;
1404        int i, seen_dashdash, unk, opt;
1405        long bottom, top, lno;
1406        int output_option = 0;
1407        const char *revs_file = NULL;
1408        const char *final_commit_name = NULL;
1409        char type[10];
1410
1411        save_commit_buffer = 0;
1412
1413        opt = 0;
1414        bottom = top = 0;
1415        seen_dashdash = 0;
1416        for (unk = i = 1; i < argc; i++) {
1417                const char *arg = argv[i];
1418                if (*arg != '-')
1419                        break;
1420                else if (!strcmp("-c", arg))
1421                        output_option |= OUTPUT_ANNOTATE_COMPAT;
1422                else if (!strcmp("-t", arg))
1423                        output_option |= OUTPUT_RAW_TIMESTAMP;
1424                else if (!strcmp("-l", arg))
1425                        output_option |= OUTPUT_LONG_OBJECT_NAME;
1426                else if (!strcmp("-S", arg) && ++i < argc)
1427                        revs_file = argv[i];
1428                else if (!strncmp("-M", arg, 2)) {
1429                        opt |= PICKAXE_BLAME_MOVE;
1430                        blame_move_score = parse_score(arg+2);
1431                }
1432                else if (!strncmp("-C", arg, 2)) {
1433                        if (opt & PICKAXE_BLAME_COPY)
1434                                opt |= PICKAXE_BLAME_COPY_HARDER;
1435                        opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
1436                        blame_copy_score = parse_score(arg+2);
1437                }
1438                else if (!strncmp("-L", arg, 2)) {
1439                        char *term;
1440                        if (!arg[2]) {
1441                                if (++i >= argc)
1442                                        usage(pickaxe_usage);
1443                                arg = argv[i];
1444                        }
1445                        else
1446                                arg += 2;
1447                        if (bottom || top)
1448                                die("More than one '-L n,m' option given");
1449                        bottom = strtol(arg, &term, 10);
1450                        if (*term == ',') {
1451                                top = strtol(term + 1, &term, 10);
1452                                if (*term)
1453                                        usage(pickaxe_usage);
1454                        }
1455                        if (bottom && top && top < bottom) {
1456                                unsigned long tmp;
1457                                tmp = top; top = bottom; bottom = tmp;
1458                        }
1459                }
1460                else if (!strcmp("--score-debug", arg))
1461                        output_option |= OUTPUT_SHOW_SCORE;
1462                else if (!strcmp("-f", arg) ||
1463                         !strcmp("--show-name", arg))
1464                        output_option |= OUTPUT_SHOW_NAME;
1465                else if (!strcmp("-n", arg) ||
1466                         !strcmp("--show-number", arg))
1467                        output_option |= OUTPUT_SHOW_NUMBER;
1468                else if (!strcmp("-p", arg) ||
1469                         !strcmp("--porcelain", arg))
1470                        output_option |= OUTPUT_PORCELAIN;
1471                else if (!strcmp("--", arg)) {
1472                        seen_dashdash = 1;
1473                        i++;
1474                        break;
1475                }
1476                else
1477                        argv[unk++] = arg;
1478        }
1479
1480        if (!blame_move_score)
1481                blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
1482        if (!blame_copy_score)
1483                blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
1484
1485        /* We have collected options unknown to us in argv[1..unk]
1486         * which are to be passed to revision machinery if we are
1487         * going to do the "bottom" procesing.
1488         *
1489         * The remaining are:
1490         *
1491         * (1) if seen_dashdash, its either
1492         *     "-options -- <path>" or
1493         *     "-options -- <path> <rev>".
1494         *     but the latter is allowed only if there is no
1495         *     options that we passed to revision machinery.
1496         *
1497         * (2) otherwise, we may have "--" somewhere later and
1498         *     might be looking at the first one of multiple 'rev'
1499         *     parameters (e.g. " master ^next ^maint -- path").
1500         *     See if there is a dashdash first, and give the
1501         *     arguments before that to revision machinery.
1502         *     After that there must be one 'path'.
1503         *
1504         * (3) otherwise, its one of the three:
1505         *     "-options <path> <rev>"
1506         *     "-options <rev> <path>"
1507         *     "-options <path>"
1508         *     but again the first one is allowed only if
1509         *     there is no options that we passed to revision
1510         *     machinery.
1511         */
1512
1513        if (seen_dashdash) {
1514                /* (1) */
1515                if (argc <= i)
1516                        usage(pickaxe_usage);
1517                path = argv[i];
1518                if (i + 1 == argc - 1) {
1519                        if (unk != 1)
1520                                usage(pickaxe_usage);
1521                        argv[unk++] = argv[i + 1];
1522                }
1523                else if (i + 1 != argc)
1524                        /* garbage at end */
1525                        usage(pickaxe_usage);
1526        }
1527        else {
1528                int j;
1529                for (j = i; !seen_dashdash && j < argc; j++)
1530                        if (!strcmp(argv[j], "--"))
1531                                seen_dashdash = j;
1532                if (seen_dashdash) {
1533                        if (seen_dashdash + 1 != argc - 1)
1534                                usage(pickaxe_usage);
1535                        path = argv[seen_dashdash + 1];
1536                        for (j = i; j < seen_dashdash; j++)
1537                                argv[unk++] = argv[j];
1538                }
1539                else {
1540                        /* (3) */
1541                        path = argv[i];
1542                        if (i + 1 == argc - 1) {
1543                                final_commit_name = argv[i + 1];
1544
1545                                /* if (unk == 1) we could be getting
1546                                 * old-style
1547                                 */
1548                                if (unk == 1 && !has_path_in_work_tree(path)) {
1549                                        path = argv[i + 1];
1550                                        final_commit_name = argv[i];
1551                                }
1552                        }
1553                        else if (i != argc - 1)
1554                                usage(pickaxe_usage); /* garbage at end */
1555
1556                        if (!has_path_in_work_tree(path))
1557                                die("cannot stat path %s: %s",
1558                                    path, strerror(errno));
1559                }
1560        }
1561
1562        if (final_commit_name)
1563                argv[unk++] = final_commit_name;
1564
1565        /* Now we got rev and path.  We do not want the path pruning
1566         * but we may want "bottom" processing.
1567         */
1568        argv[unk] = NULL;
1569
1570        init_revisions(&revs, NULL);
1571        setup_revisions(unk, argv, &revs, "HEAD");
1572        memset(&sb, 0, sizeof(sb));
1573
1574        /* There must be one and only one positive commit in the
1575         * revs->pending array.
1576         */
1577        for (i = 0; i < revs.pending.nr; i++) {
1578                struct object *obj = revs.pending.objects[i].item;
1579                if (obj->flags & UNINTERESTING)
1580                        continue;
1581                while (obj->type == OBJ_TAG)
1582                        obj = deref_tag(obj, NULL, 0);
1583                if (obj->type != OBJ_COMMIT)
1584                        die("Non commit %s?",
1585                            revs.pending.objects[i].name);
1586                if (sb.final)
1587                        die("More than one commit to dig from %s and %s?",
1588                            revs.pending.objects[i].name,
1589                            final_commit_name);
1590                sb.final = (struct commit *) obj;
1591                final_commit_name = revs.pending.objects[i].name;
1592        }
1593
1594        if (!sb.final) {
1595                /* "--not A B -- path" without anything positive */
1596                unsigned char head_sha1[20];
1597
1598                final_commit_name = "HEAD";
1599                if (get_sha1(final_commit_name, head_sha1))
1600                        die("No such ref: HEAD");
1601                sb.final = lookup_commit_reference(head_sha1);
1602                add_pending_object(&revs, &(sb.final->object), "HEAD");
1603        }
1604
1605        /* If we have bottom, this will mark the ancestors of the
1606         * bottom commits we would reach while traversing as
1607         * uninteresting.
1608         */
1609        prepare_revision_walk(&revs);
1610
1611        o = get_origin(&sb, sb.final, path);
1612        if (fill_blob_sha1(o))
1613                die("no such path %s in %s", path, final_commit_name);
1614
1615        sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);
1616        lno = prepare_lines(&sb);
1617
1618        if (bottom < 1)
1619                bottom = 1;
1620        if (top < 1)
1621                top = lno;
1622        bottom--;
1623        if (lno < top)
1624                die("file %s has only %lu lines", path, lno);
1625
1626        ent = xcalloc(1, sizeof(*ent));
1627        ent->lno = bottom;
1628        ent->num_lines = top - bottom;
1629        ent->suspect = o;
1630        ent->s_lno = bottom;
1631
1632        sb.ent = ent;
1633        sb.path = path;
1634
1635        if (revs_file && read_ancestry(revs_file))
1636                die("reading graft file %s failed: %s",
1637                    revs_file, strerror(errno));
1638
1639        assign_blame(&sb, &revs, opt);
1640
1641        coalesce(&sb);
1642
1643        if (!(output_option & OUTPUT_PORCELAIN))
1644                find_alignment(&sb, &output_option);
1645
1646        output(&sb, output_option);
1647        free((void *)sb.final_buf);
1648        for (ent = sb.ent; ent; ) {
1649                struct blame_entry *e = ent->next;
1650                free(ent);
1651                ent = e;
1652        }
1653        return 0;
1654}