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