461506c3cb298915395bfa8c81e9b49f8bffe48f
   1/*
   2 * Blame
   3 *
   4 * Copyright (c) 2006, 2014 by its authors
   5 * See COPYING for licensing conditions
   6 */
   7
   8#include "cache.h"
   9#include "refs.h"
  10#include "builtin.h"
  11#include "commit.h"
  12#include "tag.h"
  13#include "tree-walk.h"
  14#include "diff.h"
  15#include "revision.h"
  16#include "quote.h"
  17#include "string-list.h"
  18#include "mailmap.h"
  19#include "parse-options.h"
  20#include "prio-queue.h"
  21#include "utf8.h"
  22#include "userdiff.h"
  23#include "line-range.h"
  24#include "line-log.h"
  25#include "dir.h"
  26#include "progress.h"
  27#include "blame.h"
  28
  29static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
  30
  31static const char *blame_opt_usage[] = {
  32        blame_usage,
  33        "",
  34        N_("<rev-opts> are documented in git-rev-list(1)"),
  35        NULL
  36};
  37
  38static int longest_file;
  39static int longest_author;
  40static int max_orig_digits;
  41static int max_digits;
  42static int max_score_digits;
  43static int show_root;
  44static int reverse;
  45static int blank_boundary;
  46static int incremental;
  47static int xdl_opts;
  48static int abbrev = -1;
  49static int no_whole_file_rename;
  50static int show_progress;
  51
  52static struct date_mode blame_date_mode = { DATE_ISO8601 };
  53static size_t blame_date_width;
  54
  55static struct string_list mailmap = STRING_LIST_INIT_NODUP;
  56
  57#ifndef DEBUG
  58#define DEBUG 0
  59#endif
  60
  61static unsigned blame_move_score;
  62static unsigned blame_copy_score;
  63#define BLAME_DEFAULT_MOVE_SCORE        20
  64#define BLAME_DEFAULT_COPY_SCORE        40
  65
  66/* Remember to update object flag allocation in object.h */
  67#define METAINFO_SHOWN          (1u<<12)
  68#define MORE_THAN_ONE_PATH      (1u<<13)
  69
  70struct progress_info {
  71        struct progress *progress;
  72        int blamed_lines;
  73};
  74
  75static int compare_commits_by_reverse_commit_date(const void *a,
  76                                                  const void *b,
  77                                                  void *c)
  78{
  79        return -compare_commits_by_commit_date(a, b, c);
  80}
  81
  82/*
  83 * Fill the blob_sha1 field of an origin if it hasn't, so that later
  84 * call to fill_origin_blob() can use it to locate the data.  blob_sha1
  85 * for an origin is also used to pass the blame for the entire file to
  86 * the parent to detect the case where a child's blob is identical to
  87 * that of its parent's.
  88 *
  89 * This also fills origin->mode for corresponding tree path.
  90 */
  91static int fill_blob_sha1_and_mode(struct blame_origin *origin)
  92{
  93        if (!is_null_oid(&origin->blob_oid))
  94                return 0;
  95        if (get_tree_entry(origin->commit->object.oid.hash,
  96                           origin->path,
  97                           origin->blob_oid.hash, &origin->mode))
  98                goto error_out;
  99        if (sha1_object_info(origin->blob_oid.hash, NULL) != OBJ_BLOB)
 100                goto error_out;
 101        return 0;
 102 error_out:
 103        oidclr(&origin->blob_oid);
 104        origin->mode = S_IFINVALID;
 105        return -1;
 106}
 107
 108static const char *nth_line_cb(void *data, long lno)
 109{
 110        return blame_nth_line((struct blame_scoreboard *)data, lno);
 111}
 112
 113/*
 114 * Information on commits, used for output.
 115 */
 116struct commit_info {
 117        struct strbuf author;
 118        struct strbuf author_mail;
 119        timestamp_t author_time;
 120        struct strbuf author_tz;
 121
 122        /* filled only when asked for details */
 123        struct strbuf committer;
 124        struct strbuf committer_mail;
 125        timestamp_t committer_time;
 126        struct strbuf committer_tz;
 127
 128        struct strbuf summary;
 129};
 130
 131/*
 132 * Parse author/committer line in the commit object buffer
 133 */
 134static void get_ac_line(const char *inbuf, const char *what,
 135        struct strbuf *name, struct strbuf *mail,
 136        timestamp_t *time, struct strbuf *tz)
 137{
 138        struct ident_split ident;
 139        size_t len, maillen, namelen;
 140        char *tmp, *endp;
 141        const char *namebuf, *mailbuf;
 142
 143        tmp = strstr(inbuf, what);
 144        if (!tmp)
 145                goto error_out;
 146        tmp += strlen(what);
 147        endp = strchr(tmp, '\n');
 148        if (!endp)
 149                len = strlen(tmp);
 150        else
 151                len = endp - tmp;
 152
 153        if (split_ident_line(&ident, tmp, len)) {
 154        error_out:
 155                /* Ugh */
 156                tmp = "(unknown)";
 157                strbuf_addstr(name, tmp);
 158                strbuf_addstr(mail, tmp);
 159                strbuf_addstr(tz, tmp);
 160                *time = 0;
 161                return;
 162        }
 163
 164        namelen = ident.name_end - ident.name_begin;
 165        namebuf = ident.name_begin;
 166
 167        maillen = ident.mail_end - ident.mail_begin;
 168        mailbuf = ident.mail_begin;
 169
 170        if (ident.date_begin && ident.date_end)
 171                *time = strtoul(ident.date_begin, NULL, 10);
 172        else
 173                *time = 0;
 174
 175        if (ident.tz_begin && ident.tz_end)
 176                strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
 177        else
 178                strbuf_addstr(tz, "(unknown)");
 179
 180        /*
 181         * Now, convert both name and e-mail using mailmap
 182         */
 183        map_user(&mailmap, &mailbuf, &maillen,
 184                 &namebuf, &namelen);
 185
 186        strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
 187        strbuf_add(name, namebuf, namelen);
 188}
 189
 190static void commit_info_init(struct commit_info *ci)
 191{
 192
 193        strbuf_init(&ci->author, 0);
 194        strbuf_init(&ci->author_mail, 0);
 195        strbuf_init(&ci->author_tz, 0);
 196        strbuf_init(&ci->committer, 0);
 197        strbuf_init(&ci->committer_mail, 0);
 198        strbuf_init(&ci->committer_tz, 0);
 199        strbuf_init(&ci->summary, 0);
 200}
 201
 202static void commit_info_destroy(struct commit_info *ci)
 203{
 204
 205        strbuf_release(&ci->author);
 206        strbuf_release(&ci->author_mail);
 207        strbuf_release(&ci->author_tz);
 208        strbuf_release(&ci->committer);
 209        strbuf_release(&ci->committer_mail);
 210        strbuf_release(&ci->committer_tz);
 211        strbuf_release(&ci->summary);
 212}
 213
 214static void get_commit_info(struct commit *commit,
 215                            struct commit_info *ret,
 216                            int detailed)
 217{
 218        int len;
 219        const char *subject, *encoding;
 220        const char *message;
 221
 222        commit_info_init(ret);
 223
 224        encoding = get_log_output_encoding();
 225        message = logmsg_reencode(commit, NULL, encoding);
 226        get_ac_line(message, "\nauthor ",
 227                    &ret->author, &ret->author_mail,
 228                    &ret->author_time, &ret->author_tz);
 229
 230        if (!detailed) {
 231                unuse_commit_buffer(commit, message);
 232                return;
 233        }
 234
 235        get_ac_line(message, "\ncommitter ",
 236                    &ret->committer, &ret->committer_mail,
 237                    &ret->committer_time, &ret->committer_tz);
 238
 239        len = find_commit_subject(message, &subject);
 240        if (len)
 241                strbuf_add(&ret->summary, subject, len);
 242        else
 243                strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
 244
 245        unuse_commit_buffer(commit, message);
 246}
 247
 248/*
 249 * Write out any suspect information which depends on the path. This must be
 250 * handled separately from emit_one_suspect_detail(), because a given commit
 251 * may have changes in multiple paths. So this needs to appear each time
 252 * we mention a new group.
 253 *
 254 * To allow LF and other nonportable characters in pathnames,
 255 * they are c-style quoted as needed.
 256 */
 257static void write_filename_info(struct blame_origin *suspect)
 258{
 259        if (suspect->previous) {
 260                struct blame_origin *prev = suspect->previous;
 261                printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
 262                write_name_quoted(prev->path, stdout, '\n');
 263        }
 264        printf("filename ");
 265        write_name_quoted(suspect->path, stdout, '\n');
 266}
 267
 268/*
 269 * Porcelain/Incremental format wants to show a lot of details per
 270 * commit.  Instead of repeating this every line, emit it only once,
 271 * the first time each commit appears in the output (unless the
 272 * user has specifically asked for us to repeat).
 273 */
 274static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
 275{
 276        struct commit_info ci;
 277
 278        if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
 279                return 0;
 280
 281        suspect->commit->object.flags |= METAINFO_SHOWN;
 282        get_commit_info(suspect->commit, &ci, 1);
 283        printf("author %s\n", ci.author.buf);
 284        printf("author-mail %s\n", ci.author_mail.buf);
 285        printf("author-time %"PRItime"\n", ci.author_time);
 286        printf("author-tz %s\n", ci.author_tz.buf);
 287        printf("committer %s\n", ci.committer.buf);
 288        printf("committer-mail %s\n", ci.committer_mail.buf);
 289        printf("committer-time %"PRItime"\n", ci.committer_time);
 290        printf("committer-tz %s\n", ci.committer_tz.buf);
 291        printf("summary %s\n", ci.summary.buf);
 292        if (suspect->commit->object.flags & UNINTERESTING)
 293                printf("boundary\n");
 294
 295        commit_info_destroy(&ci);
 296
 297        return 1;
 298}
 299
 300/*
 301 * The blame_entry is found to be guilty for the range.
 302 * Show it in incremental output.
 303 */
 304static void found_guilty_entry(struct blame_entry *ent, void *data)
 305{
 306        struct progress_info *pi = (struct progress_info *)data;
 307
 308        if (incremental) {
 309                struct blame_origin *suspect = ent->suspect;
 310
 311                printf("%s %d %d %d\n",
 312                       oid_to_hex(&suspect->commit->object.oid),
 313                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
 314                emit_one_suspect_detail(suspect, 0);
 315                write_filename_info(suspect);
 316                maybe_flush_or_die(stdout, "stdout");
 317        }
 318        pi->blamed_lines += ent->num_lines;
 319        display_progress(pi->progress, pi->blamed_lines);
 320}
 321
 322static const char *format_time(timestamp_t time, const char *tz_str,
 323                               int show_raw_time)
 324{
 325        static struct strbuf time_buf = STRBUF_INIT;
 326
 327        strbuf_reset(&time_buf);
 328        if (show_raw_time) {
 329                strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
 330        }
 331        else {
 332                const char *time_str;
 333                size_t time_width;
 334                int tz;
 335                tz = atoi(tz_str);
 336                time_str = show_date(time, tz, &blame_date_mode);
 337                strbuf_addstr(&time_buf, time_str);
 338                /*
 339                 * Add space paddings to time_buf to display a fixed width
 340                 * string, and use time_width for display width calibration.
 341                 */
 342                for (time_width = utf8_strwidth(time_str);
 343                     time_width < blame_date_width;
 344                     time_width++)
 345                        strbuf_addch(&time_buf, ' ');
 346        }
 347        return time_buf.buf;
 348}
 349
 350#define OUTPUT_ANNOTATE_COMPAT  001
 351#define OUTPUT_LONG_OBJECT_NAME 002
 352#define OUTPUT_RAW_TIMESTAMP    004
 353#define OUTPUT_PORCELAIN        010
 354#define OUTPUT_SHOW_NAME        020
 355#define OUTPUT_SHOW_NUMBER      040
 356#define OUTPUT_SHOW_SCORE      0100
 357#define OUTPUT_NO_AUTHOR       0200
 358#define OUTPUT_SHOW_EMAIL       0400
 359#define OUTPUT_LINE_PORCELAIN 01000
 360
 361static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
 362{
 363        if (emit_one_suspect_detail(suspect, repeat) ||
 364            (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
 365                write_filename_info(suspect);
 366}
 367
 368static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
 369                           int opt)
 370{
 371        int repeat = opt & OUTPUT_LINE_PORCELAIN;
 372        int cnt;
 373        const char *cp;
 374        struct blame_origin *suspect = ent->suspect;
 375        char hex[GIT_MAX_HEXSZ + 1];
 376
 377        oid_to_hex_r(hex, &suspect->commit->object.oid);
 378        printf("%s %d %d %d\n",
 379               hex,
 380               ent->s_lno + 1,
 381               ent->lno + 1,
 382               ent->num_lines);
 383        emit_porcelain_details(suspect, repeat);
 384
 385        cp = blame_nth_line(sb, ent->lno);
 386        for (cnt = 0; cnt < ent->num_lines; cnt++) {
 387                char ch;
 388                if (cnt) {
 389                        printf("%s %d %d\n", hex,
 390                               ent->s_lno + 1 + cnt,
 391                               ent->lno + 1 + cnt);
 392                        if (repeat)
 393                                emit_porcelain_details(suspect, 1);
 394                }
 395                putchar('\t');
 396                do {
 397                        ch = *cp++;
 398                        putchar(ch);
 399                } while (ch != '\n' &&
 400                         cp < sb->final_buf + sb->final_buf_size);
 401        }
 402
 403        if (sb->final_buf_size && cp[-1] != '\n')
 404                putchar('\n');
 405}
 406
 407static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
 408{
 409        int cnt;
 410        const char *cp;
 411        struct blame_origin *suspect = ent->suspect;
 412        struct commit_info ci;
 413        char hex[GIT_MAX_HEXSZ + 1];
 414        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
 415
 416        get_commit_info(suspect->commit, &ci, 1);
 417        oid_to_hex_r(hex, &suspect->commit->object.oid);
 418
 419        cp = blame_nth_line(sb, ent->lno);
 420        for (cnt = 0; cnt < ent->num_lines; cnt++) {
 421                char ch;
 422                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 423
 424                if (suspect->commit->object.flags & UNINTERESTING) {
 425                        if (blank_boundary)
 426                                memset(hex, ' ', length);
 427                        else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
 428                                length--;
 429                                putchar('^');
 430                        }
 431                }
 432
 433                printf("%.*s", length, hex);
 434                if (opt & OUTPUT_ANNOTATE_COMPAT) {
 435                        const char *name;
 436                        if (opt & OUTPUT_SHOW_EMAIL)
 437                                name = ci.author_mail.buf;
 438                        else
 439                                name = ci.author.buf;
 440                        printf("\t(%10s\t%10s\t%d)", name,
 441                               format_time(ci.author_time, ci.author_tz.buf,
 442                                           show_raw_time),
 443                               ent->lno + 1 + cnt);
 444                } else {
 445                        if (opt & OUTPUT_SHOW_SCORE)
 446                                printf(" %*d %02d",
 447                                       max_score_digits, ent->score,
 448                                       ent->suspect->refcnt);
 449                        if (opt & OUTPUT_SHOW_NAME)
 450                                printf(" %-*.*s", longest_file, longest_file,
 451                                       suspect->path);
 452                        if (opt & OUTPUT_SHOW_NUMBER)
 453                                printf(" %*d", max_orig_digits,
 454                                       ent->s_lno + 1 + cnt);
 455
 456                        if (!(opt & OUTPUT_NO_AUTHOR)) {
 457                                const char *name;
 458                                int pad;
 459                                if (opt & OUTPUT_SHOW_EMAIL)
 460                                        name = ci.author_mail.buf;
 461                                else
 462                                        name = ci.author.buf;
 463                                pad = longest_author - utf8_strwidth(name);
 464                                printf(" (%s%*s %10s",
 465                                       name, pad, "",
 466                                       format_time(ci.author_time,
 467                                                   ci.author_tz.buf,
 468                                                   show_raw_time));
 469                        }
 470                        printf(" %*d) ",
 471                               max_digits, ent->lno + 1 + cnt);
 472                }
 473                do {
 474                        ch = *cp++;
 475                        putchar(ch);
 476                } while (ch != '\n' &&
 477                         cp < sb->final_buf + sb->final_buf_size);
 478        }
 479
 480        if (sb->final_buf_size && cp[-1] != '\n')
 481                putchar('\n');
 482
 483        commit_info_destroy(&ci);
 484}
 485
 486static void output(struct blame_scoreboard *sb, int option)
 487{
 488        struct blame_entry *ent;
 489
 490        if (option & OUTPUT_PORCELAIN) {
 491                for (ent = sb->ent; ent; ent = ent->next) {
 492                        int count = 0;
 493                        struct blame_origin *suspect;
 494                        struct commit *commit = ent->suspect->commit;
 495                        if (commit->object.flags & MORE_THAN_ONE_PATH)
 496                                continue;
 497                        for (suspect = commit->util; suspect; suspect = suspect->next) {
 498                                if (suspect->guilty && count++) {
 499                                        commit->object.flags |= MORE_THAN_ONE_PATH;
 500                                        break;
 501                                }
 502                        }
 503                }
 504        }
 505
 506        for (ent = sb->ent; ent; ent = ent->next) {
 507                if (option & OUTPUT_PORCELAIN)
 508                        emit_porcelain(sb, ent, option);
 509                else {
 510                        emit_other(sb, ent, option);
 511                }
 512        }
 513}
 514
 515static const char *get_next_line(const char *start, const char *end)
 516{
 517        const char *nl = memchr(start, '\n', end - start);
 518        return nl ? nl + 1 : end;
 519}
 520
 521/*
 522 * To allow quick access to the contents of nth line in the
 523 * final image, prepare an index in the scoreboard.
 524 */
 525static int prepare_lines(struct blame_scoreboard *sb)
 526{
 527        const char *buf = sb->final_buf;
 528        unsigned long len = sb->final_buf_size;
 529        const char *end = buf + len;
 530        const char *p;
 531        int *lineno;
 532        int num = 0;
 533
 534        for (p = buf; p < end; p = get_next_line(p, end))
 535                num++;
 536
 537        ALLOC_ARRAY(sb->lineno, num + 1);
 538        lineno = sb->lineno;
 539
 540        for (p = buf; p < end; p = get_next_line(p, end))
 541                *lineno++ = p - buf;
 542
 543        *lineno = len;
 544
 545        sb->num_lines = num;
 546        return sb->num_lines;
 547}
 548
 549/*
 550 * Add phony grafts for use with -S; this is primarily to
 551 * support git's cvsserver that wants to give a linear history
 552 * to its clients.
 553 */
 554static int read_ancestry(const char *graft_file)
 555{
 556        FILE *fp = fopen(graft_file, "r");
 557        struct strbuf buf = STRBUF_INIT;
 558        if (!fp)
 559                return -1;
 560        while (!strbuf_getwholeline(&buf, fp, '\n')) {
 561                /* The format is just "Commit Parent1 Parent2 ...\n" */
 562                struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
 563                if (graft)
 564                        register_commit_graft(graft, 0);
 565        }
 566        fclose(fp);
 567        strbuf_release(&buf);
 568        return 0;
 569}
 570
 571static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
 572{
 573        const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
 574                                              auto_abbrev);
 575        int len = strlen(uniq);
 576        if (auto_abbrev < len)
 577                return len;
 578        return auto_abbrev;
 579}
 580
 581/*
 582 * How many columns do we need to show line numbers, authors,
 583 * and filenames?
 584 */
 585static void find_alignment(struct blame_scoreboard *sb, int *option)
 586{
 587        int longest_src_lines = 0;
 588        int longest_dst_lines = 0;
 589        unsigned largest_score = 0;
 590        struct blame_entry *e;
 591        int compute_auto_abbrev = (abbrev < 0);
 592        int auto_abbrev = DEFAULT_ABBREV;
 593
 594        for (e = sb->ent; e; e = e->next) {
 595                struct blame_origin *suspect = e->suspect;
 596                int num;
 597
 598                if (compute_auto_abbrev)
 599                        auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
 600                if (strcmp(suspect->path, sb->path))
 601                        *option |= OUTPUT_SHOW_NAME;
 602                num = strlen(suspect->path);
 603                if (longest_file < num)
 604                        longest_file = num;
 605                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
 606                        struct commit_info ci;
 607                        suspect->commit->object.flags |= METAINFO_SHOWN;
 608                        get_commit_info(suspect->commit, &ci, 1);
 609                        if (*option & OUTPUT_SHOW_EMAIL)
 610                                num = utf8_strwidth(ci.author_mail.buf);
 611                        else
 612                                num = utf8_strwidth(ci.author.buf);
 613                        if (longest_author < num)
 614                                longest_author = num;
 615                        commit_info_destroy(&ci);
 616                }
 617                num = e->s_lno + e->num_lines;
 618                if (longest_src_lines < num)
 619                        longest_src_lines = num;
 620                num = e->lno + e->num_lines;
 621                if (longest_dst_lines < num)
 622                        longest_dst_lines = num;
 623                if (largest_score < blame_entry_score(sb, e))
 624                        largest_score = blame_entry_score(sb, e);
 625        }
 626        max_orig_digits = decimal_width(longest_src_lines);
 627        max_digits = decimal_width(longest_dst_lines);
 628        max_score_digits = decimal_width(largest_score);
 629
 630        if (compute_auto_abbrev)
 631                /* one more abbrev length is needed for the boundary commit */
 632                abbrev = auto_abbrev + 1;
 633}
 634
 635static void sanity_check_on_fail(struct blame_scoreboard *sb, int baa)
 636{
 637        int opt = OUTPUT_SHOW_SCORE | OUTPUT_SHOW_NUMBER | OUTPUT_SHOW_NAME;
 638        find_alignment(sb, &opt);
 639        output(sb, opt);
 640        die("Baa %d!", baa);
 641}
 642
 643static unsigned parse_score(const char *arg)
 644{
 645        char *end;
 646        unsigned long score = strtoul(arg, &end, 10);
 647        if (*end)
 648                return 0;
 649        return score;
 650}
 651
 652static const char *add_prefix(const char *prefix, const char *path)
 653{
 654        return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 655}
 656
 657static int git_blame_config(const char *var, const char *value, void *cb)
 658{
 659        if (!strcmp(var, "blame.showroot")) {
 660                show_root = git_config_bool(var, value);
 661                return 0;
 662        }
 663        if (!strcmp(var, "blame.blankboundary")) {
 664                blank_boundary = git_config_bool(var, value);
 665                return 0;
 666        }
 667        if (!strcmp(var, "blame.showemail")) {
 668                int *output_option = cb;
 669                if (git_config_bool(var, value))
 670                        *output_option |= OUTPUT_SHOW_EMAIL;
 671                else
 672                        *output_option &= ~OUTPUT_SHOW_EMAIL;
 673                return 0;
 674        }
 675        if (!strcmp(var, "blame.date")) {
 676                if (!value)
 677                        return config_error_nonbool(var);
 678                parse_date_format(value, &blame_date_mode);
 679                return 0;
 680        }
 681
 682        if (git_diff_heuristic_config(var, value, cb) < 0)
 683                return -1;
 684        if (userdiff_config(var, value) < 0)
 685                return -1;
 686
 687        return git_default_config(var, value, cb);
 688}
 689
 690static struct commit *find_single_final(struct rev_info *revs,
 691                                        const char **name_p)
 692{
 693        int i;
 694        struct commit *found = NULL;
 695        const char *name = NULL;
 696
 697        for (i = 0; i < revs->pending.nr; i++) {
 698                struct object *obj = revs->pending.objects[i].item;
 699                if (obj->flags & UNINTERESTING)
 700                        continue;
 701                obj = deref_tag(obj, NULL, 0);
 702                if (obj->type != OBJ_COMMIT)
 703                        die("Non commit %s?", revs->pending.objects[i].name);
 704                if (found)
 705                        die("More than one commit to dig from %s and %s?",
 706                            revs->pending.objects[i].name, name);
 707                found = (struct commit *)obj;
 708                name = revs->pending.objects[i].name;
 709        }
 710        if (name_p)
 711                *name_p = name;
 712        return found;
 713}
 714
 715static struct commit *dwim_reverse_initial(struct rev_info *revs,
 716                                           const char **name_p)
 717{
 718        /*
 719         * DWIM "git blame --reverse ONE -- PATH" as
 720         * "git blame --reverse ONE..HEAD -- PATH" but only do so
 721         * when it makes sense.
 722         */
 723        struct object *obj;
 724        struct commit *head_commit;
 725        unsigned char head_sha1[20];
 726
 727        if (revs->pending.nr != 1)
 728                return NULL;
 729
 730        /* Is that sole rev a committish? */
 731        obj = revs->pending.objects[0].item;
 732        obj = deref_tag(obj, NULL, 0);
 733        if (obj->type != OBJ_COMMIT)
 734                return NULL;
 735
 736        /* Do we have HEAD? */
 737        if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
 738                return NULL;
 739        head_commit = lookup_commit_reference_gently(head_sha1, 1);
 740        if (!head_commit)
 741                return NULL;
 742
 743        /* Turn "ONE" into "ONE..HEAD" then */
 744        obj->flags |= UNINTERESTING;
 745        add_pending_object(revs, &head_commit->object, "HEAD");
 746
 747        if (name_p)
 748                *name_p = revs->pending.objects[0].name;
 749        return (struct commit *)obj;
 750}
 751
 752static struct commit *find_single_initial(struct rev_info *revs,
 753                                          const char **name_p)
 754{
 755        int i;
 756        const char *final_commit_name = NULL;
 757        struct commit *found = NULL;
 758
 759        /*
 760         * There must be one and only one negative commit, and it must be
 761         * the boundary.
 762         */
 763        for (i = 0; i < revs->pending.nr; i++) {
 764                struct object *obj = revs->pending.objects[i].item;
 765                if (!(obj->flags & UNINTERESTING))
 766                        continue;
 767                obj = deref_tag(obj, NULL, 0);
 768                if (obj->type != OBJ_COMMIT)
 769                        die("Non commit %s?", revs->pending.objects[i].name);
 770                if (found)
 771                        die("More than one commit to dig up from, %s and %s?",
 772                            revs->pending.objects[i].name,
 773                            final_commit_name);
 774                found = (struct commit *) obj;
 775                final_commit_name = revs->pending.objects[i].name;
 776        }
 777
 778        if (!final_commit_name)
 779                found = dwim_reverse_initial(revs, &final_commit_name);
 780        if (!final_commit_name)
 781                die("No commit to dig up from?");
 782
 783        if (name_p)
 784                *name_p = final_commit_name;
 785        return found;
 786}
 787
 788static int blame_copy_callback(const struct option *option, const char *arg, int unset)
 789{
 790        int *opt = option->value;
 791
 792        /*
 793         * -C enables copy from removed files;
 794         * -C -C enables copy from existing files, but only
 795         *       when blaming a new file;
 796         * -C -C -C enables copy from existing files for
 797         *          everybody
 798         */
 799        if (*opt & PICKAXE_BLAME_COPY_HARDER)
 800                *opt |= PICKAXE_BLAME_COPY_HARDEST;
 801        if (*opt & PICKAXE_BLAME_COPY)
 802                *opt |= PICKAXE_BLAME_COPY_HARDER;
 803        *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
 804
 805        if (arg)
 806                blame_copy_score = parse_score(arg);
 807        return 0;
 808}
 809
 810static int blame_move_callback(const struct option *option, const char *arg, int unset)
 811{
 812        int *opt = option->value;
 813
 814        *opt |= PICKAXE_BLAME_MOVE;
 815
 816        if (arg)
 817                blame_move_score = parse_score(arg);
 818        return 0;
 819}
 820
 821void init_scoreboard(struct blame_scoreboard *sb)
 822{
 823        memset(sb, 0, sizeof(struct blame_scoreboard));
 824        sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
 825        sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
 826}
 827
 828void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)
 829{
 830        const char *final_commit_name = NULL;
 831        struct blame_origin *o;
 832        struct commit *final_commit = NULL;
 833        enum object_type type;
 834
 835        if (sb->reverse && sb->contents_from)
 836                die(_("--contents and --reverse do not blend well."));
 837
 838        if (!sb->reverse) {
 839                sb->final = find_single_final(sb->revs, &final_commit_name);
 840                sb->commits.compare = compare_commits_by_commit_date;
 841        } else {
 842                sb->final = find_single_initial(sb->revs, &final_commit_name);
 843                sb->commits.compare = compare_commits_by_reverse_commit_date;
 844        }
 845
 846        if (sb->final && sb->contents_from)
 847                die(_("cannot use --contents with final commit object name"));
 848
 849        if (sb->reverse && sb->revs->first_parent_only)
 850                sb->revs->children.name = NULL;
 851
 852        if (!sb->final) {
 853                /*
 854                 * "--not A B -- path" without anything positive;
 855                 * do not default to HEAD, but use the working tree
 856                 * or "--contents".
 857                 */
 858                setup_work_tree();
 859                sb->final = fake_working_tree_commit(&sb->revs->diffopt,
 860                                                     path, sb->contents_from);
 861                add_pending_object(sb->revs, &(sb->final->object), ":");
 862        }
 863
 864        if (sb->reverse && sb->revs->first_parent_only) {
 865                final_commit = find_single_final(sb->revs, NULL);
 866                if (!final_commit)
 867                        die(_("--reverse and --first-parent together require specified latest commit"));
 868        }
 869
 870        /*
 871         * If we have bottom, this will mark the ancestors of the
 872         * bottom commits we would reach while traversing as
 873         * uninteresting.
 874         */
 875        if (prepare_revision_walk(sb->revs))
 876                die(_("revision walk setup failed"));
 877
 878        if (sb->reverse && sb->revs->first_parent_only) {
 879                struct commit *c = final_commit;
 880
 881                sb->revs->children.name = "children";
 882                while (c->parents &&
 883                       oidcmp(&c->object.oid, &sb->final->object.oid)) {
 884                        struct commit_list *l = xcalloc(1, sizeof(*l));
 885
 886                        l->item = c;
 887                        if (add_decoration(&sb->revs->children,
 888                                           &c->parents->item->object, l))
 889                                die("BUG: not unique item in first-parent chain");
 890                        c = c->parents->item;
 891                }
 892
 893                if (oidcmp(&c->object.oid, &sb->final->object.oid))
 894                        die(_("--reverse --first-parent together require range along first-parent chain"));
 895        }
 896
 897        if (is_null_oid(&sb->final->object.oid)) {
 898                o = sb->final->util;
 899                sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
 900                sb->final_buf_size = o->file.size;
 901        }
 902        else {
 903                o = get_origin(sb->final, path);
 904                if (fill_blob_sha1_and_mode(o))
 905                        die(_("no such path %s in %s"), path, final_commit_name);
 906
 907                if (DIFF_OPT_TST(&sb->revs->diffopt, ALLOW_TEXTCONV) &&
 908                    textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
 909                                    &sb->final_buf_size))
 910                        ;
 911                else
 912                        sb->final_buf = read_sha1_file(o->blob_oid.hash, &type,
 913                                                       &sb->final_buf_size);
 914
 915                if (!sb->final_buf)
 916                        die(_("cannot read blob %s for path %s"),
 917                            oid_to_hex(&o->blob_oid),
 918                            path);
 919        }
 920        sb->num_read_blob++;
 921        prepare_lines(sb);
 922
 923        if (orig)
 924                *orig = o;
 925}
 926
 927struct blame_entry *blame_entry_prepend(struct blame_entry *head,
 928                                        long start, long end,
 929                                        struct blame_origin *o)
 930{
 931        struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
 932        new_head->lno = start;
 933        new_head->num_lines = end - start;
 934        new_head->suspect = o;
 935        new_head->s_lno = start;
 936        new_head->next = head;
 937        blame_origin_incref(o);
 938        return new_head;
 939}
 940
 941int cmd_blame(int argc, const char **argv, const char *prefix)
 942{
 943        struct rev_info revs;
 944        const char *path;
 945        struct blame_scoreboard sb;
 946        struct blame_origin *o;
 947        struct blame_entry *ent = NULL;
 948        long dashdash_pos, lno;
 949        struct progress_info pi = { NULL, 0 };
 950
 951        struct string_list range_list = STRING_LIST_INIT_NODUP;
 952        int output_option = 0, opt = 0;
 953        int show_stats = 0;
 954        const char *revs_file = NULL;
 955        const char *contents_from = NULL;
 956        const struct option options[] = {
 957                OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
 958                OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
 959                OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
 960                OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
 961                OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
 962                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
 963                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
 964                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
 965                OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
 966                OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
 967                OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
 968                OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
 969                OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
 970                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
 971                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
 972                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
 973
 974                /*
 975                 * The following two options are parsed by parse_revision_opt()
 976                 * and are only included here to get included in the "-h"
 977                 * output:
 978                 */
 979                { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
 980
 981                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
 982                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
 983                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
 984                { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
 985                { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
 986                OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
 987                OPT__ABBREV(&abbrev),
 988                OPT_END()
 989        };
 990
 991        struct parse_opt_ctx_t ctx;
 992        int cmd_is_annotate = !strcmp(argv[0], "annotate");
 993        struct range_set ranges;
 994        unsigned int range_i;
 995        long anchor;
 996
 997        git_config(git_blame_config, &output_option);
 998        init_revisions(&revs, NULL);
 999        revs.date_mode = blame_date_mode;
1000        DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
1001        DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
1002
1003        save_commit_buffer = 0;
1004        dashdash_pos = 0;
1005        show_progress = -1;
1006
1007        parse_options_start(&ctx, argc, argv, prefix, options,
1008                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
1009        for (;;) {
1010                switch (parse_options_step(&ctx, options, blame_opt_usage)) {
1011                case PARSE_OPT_HELP:
1012                        exit(129);
1013                case PARSE_OPT_DONE:
1014                        if (ctx.argv[0])
1015                                dashdash_pos = ctx.cpidx;
1016                        goto parse_done;
1017                }
1018
1019                if (!strcmp(ctx.argv[0], "--reverse")) {
1020                        ctx.argv[0] = "--children";
1021                        reverse = 1;
1022                }
1023                parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
1024        }
1025parse_done:
1026        no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
1027        xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
1028        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
1029        argc = parse_options_end(&ctx);
1030
1031        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
1032                if (show_progress > 0)
1033                        die(_("--progress can't be used with --incremental or porcelain formats"));
1034                show_progress = 0;
1035        } else if (show_progress < 0)
1036                show_progress = isatty(2);
1037
1038        if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
1039                /* one more abbrev length is needed for the boundary commit */
1040                abbrev++;
1041        else if (!abbrev)
1042                abbrev = GIT_SHA1_HEXSZ;
1043
1044        if (revs_file && read_ancestry(revs_file))
1045                die_errno("reading graft file '%s' failed", revs_file);
1046
1047        if (cmd_is_annotate) {
1048                output_option |= OUTPUT_ANNOTATE_COMPAT;
1049                blame_date_mode.type = DATE_ISO8601;
1050        } else {
1051                blame_date_mode = revs.date_mode;
1052        }
1053
1054        /* The maximum width used to show the dates */
1055        switch (blame_date_mode.type) {
1056        case DATE_RFC2822:
1057                blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
1058                break;
1059        case DATE_ISO8601_STRICT:
1060                blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
1061                break;
1062        case DATE_ISO8601:
1063                blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
1064                break;
1065        case DATE_RAW:
1066                blame_date_width = sizeof("1161298804 -0700");
1067                break;
1068        case DATE_UNIX:
1069                blame_date_width = sizeof("1161298804");
1070                break;
1071        case DATE_SHORT:
1072                blame_date_width = sizeof("2006-10-19");
1073                break;
1074        case DATE_RELATIVE:
1075                /* TRANSLATORS: This string is used to tell us the maximum
1076                   display width for a relative timestamp in "git blame"
1077                   output.  For C locale, "4 years, 11 months ago", which
1078                   takes 22 places, is the longest among various forms of
1079                   relative timestamps, but your language may need more or
1080                   fewer display columns. */
1081                blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
1082                break;
1083        case DATE_NORMAL:
1084                blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
1085                break;
1086        case DATE_STRFTIME:
1087                blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
1088                break;
1089        }
1090        blame_date_width -= 1; /* strip the null */
1091
1092        if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
1093                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
1094                        PICKAXE_BLAME_COPY_HARDER);
1095
1096        /*
1097         * We have collected options unknown to us in argv[1..unk]
1098         * which are to be passed to revision machinery if we are
1099         * going to do the "bottom" processing.
1100         *
1101         * The remaining are:
1102         *
1103         * (1) if dashdash_pos != 0, it is either
1104         *     "blame [revisions] -- <path>" or
1105         *     "blame -- <path> <rev>"
1106         *
1107         * (2) otherwise, it is one of the two:
1108         *     "blame [revisions] <path>"
1109         *     "blame <path> <rev>"
1110         *
1111         * Note that we must strip out <path> from the arguments: we do not
1112         * want the path pruning but we may want "bottom" processing.
1113         */
1114        if (dashdash_pos) {
1115                switch (argc - dashdash_pos - 1) {
1116                case 2: /* (1b) */
1117                        if (argc != 4)
1118                                usage_with_options(blame_opt_usage, options);
1119                        /* reorder for the new way: <rev> -- <path> */
1120                        argv[1] = argv[3];
1121                        argv[3] = argv[2];
1122                        argv[2] = "--";
1123                        /* FALLTHROUGH */
1124                case 1: /* (1a) */
1125                        path = add_prefix(prefix, argv[--argc]);
1126                        argv[argc] = NULL;
1127                        break;
1128                default:
1129                        usage_with_options(blame_opt_usage, options);
1130                }
1131        } else {
1132                if (argc < 2)
1133                        usage_with_options(blame_opt_usage, options);
1134                path = add_prefix(prefix, argv[argc - 1]);
1135                if (argc == 3 && !file_exists(path)) { /* (2b) */
1136                        path = add_prefix(prefix, argv[1]);
1137                        argv[1] = argv[2];
1138                }
1139                argv[argc - 1] = "--";
1140
1141                setup_work_tree();
1142                if (!file_exists(path))
1143                        die_errno("cannot stat path '%s'", path);
1144        }
1145
1146        revs.disable_stdin = 1;
1147        setup_revisions(argc, argv, &revs, NULL);
1148
1149        init_scoreboard(&sb);
1150        sb.revs = &revs;
1151        sb.contents_from = contents_from;
1152        sb.reverse = reverse;
1153        setup_scoreboard(&sb, path, &o);
1154        lno = sb.num_lines;
1155
1156        if (lno && !range_list.nr)
1157                string_list_append(&range_list, "1");
1158
1159        anchor = 1;
1160        range_set_init(&ranges, range_list.nr);
1161        for (range_i = 0; range_i < range_list.nr; ++range_i) {
1162                long bottom, top;
1163                if (parse_range_arg(range_list.items[range_i].string,
1164                                    nth_line_cb, &sb, lno, anchor,
1165                                    &bottom, &top, sb.path))
1166                        usage(blame_usage);
1167                if (lno < top || ((lno || bottom) && lno < bottom))
1168                        die(Q_("file %s has only %lu line",
1169                               "file %s has only %lu lines",
1170                               lno), path, lno);
1171                if (bottom < 1)
1172                        bottom = 1;
1173                if (top < 1)
1174                        top = lno;
1175                bottom--;
1176                range_set_append_unsafe(&ranges, bottom, top);
1177                anchor = top + 1;
1178        }
1179        sort_and_merge_range_set(&ranges);
1180
1181        for (range_i = ranges.nr; range_i > 0; --range_i) {
1182                const struct range *r = &ranges.ranges[range_i - 1];
1183                ent = blame_entry_prepend(ent, r->start, r->end, o);
1184        }
1185
1186        o->suspects = ent;
1187        prio_queue_put(&sb.commits, o->commit);
1188
1189        blame_origin_decref(o);
1190
1191        range_set_release(&ranges);
1192        string_list_clear(&range_list, 0);
1193
1194        sb.ent = NULL;
1195        sb.path = path;
1196
1197        if (blame_move_score)
1198                sb.move_score = blame_move_score;
1199        if (blame_copy_score)
1200                sb.copy_score = blame_copy_score;
1201
1202        sb.debug = DEBUG;
1203        sb.on_sanity_fail = &sanity_check_on_fail;
1204
1205        sb.show_root = show_root;
1206        sb.xdl_opts = xdl_opts;
1207        sb.no_whole_file_rename = no_whole_file_rename;
1208
1209        read_mailmap(&mailmap, NULL);
1210
1211        sb.found_guilty_entry = &found_guilty_entry;
1212        sb.found_guilty_entry_data = &pi;
1213        if (show_progress)
1214                pi.progress = start_progress_delay(_("Blaming lines"),
1215                                                   sb.num_lines, 50, 1);
1216
1217        assign_blame(&sb, opt);
1218
1219        stop_progress(&pi.progress);
1220
1221        if (!incremental)
1222                setup_pager();
1223        else
1224                return 0;
1225
1226        blame_sort_final(&sb);
1227
1228        blame_coalesce(&sb);
1229
1230        if (!(output_option & OUTPUT_PORCELAIN))
1231                find_alignment(&sb, &output_option);
1232
1233        output(&sb, output_option);
1234        free((void *)sb.final_buf);
1235        for (ent = sb.ent; ent; ) {
1236                struct blame_entry *e = ent->next;
1237                free(ent);
1238                ent = e;
1239        }
1240
1241        if (show_stats) {
1242                printf("num read blob: %d\n", sb.num_read_blob);
1243                printf("num get patch: %d\n", sb.num_get_patch);
1244                printf("num commits: %d\n", sb.num_commits);
1245        }
1246        return 0;
1247}