builtin / blame.con commit Merge branch 'ps/stash-push-pathspec-fix' (49a8bf2)
   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 "builtin.h"
  10#include "commit.h"
  11#include "diff.h"
  12#include "revision.h"
  13#include "quote.h"
  14#include "string-list.h"
  15#include "mailmap.h"
  16#include "parse-options.h"
  17#include "prio-queue.h"
  18#include "utf8.h"
  19#include "userdiff.h"
  20#include "line-range.h"
  21#include "line-log.h"
  22#include "dir.h"
  23#include "progress.h"
  24#include "blame.h"
  25
  26static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
  27
  28static const char *blame_opt_usage[] = {
  29        blame_usage,
  30        "",
  31        N_("<rev-opts> are documented in git-rev-list(1)"),
  32        NULL
  33};
  34
  35static int longest_file;
  36static int longest_author;
  37static int max_orig_digits;
  38static int max_digits;
  39static int max_score_digits;
  40static int show_root;
  41static int reverse;
  42static int blank_boundary;
  43static int incremental;
  44static int xdl_opts;
  45static int abbrev = -1;
  46static int no_whole_file_rename;
  47static int show_progress;
  48
  49static struct date_mode blame_date_mode = { DATE_ISO8601 };
  50static size_t blame_date_width;
  51
  52static struct string_list mailmap = STRING_LIST_INIT_NODUP;
  53
  54#ifndef DEBUG
  55#define DEBUG 0
  56#endif
  57
  58static unsigned blame_move_score;
  59static unsigned blame_copy_score;
  60
  61/* Remember to update object flag allocation in object.h */
  62#define METAINFO_SHOWN          (1u<<12)
  63#define MORE_THAN_ONE_PATH      (1u<<13)
  64
  65struct progress_info {
  66        struct progress *progress;
  67        int blamed_lines;
  68};
  69
  70static const char *nth_line_cb(void *data, long lno)
  71{
  72        return blame_nth_line((struct blame_scoreboard *)data, lno);
  73}
  74
  75/*
  76 * Information on commits, used for output.
  77 */
  78struct commit_info {
  79        struct strbuf author;
  80        struct strbuf author_mail;
  81        timestamp_t author_time;
  82        struct strbuf author_tz;
  83
  84        /* filled only when asked for details */
  85        struct strbuf committer;
  86        struct strbuf committer_mail;
  87        timestamp_t committer_time;
  88        struct strbuf committer_tz;
  89
  90        struct strbuf summary;
  91};
  92
  93/*
  94 * Parse author/committer line in the commit object buffer
  95 */
  96static void get_ac_line(const char *inbuf, const char *what,
  97        struct strbuf *name, struct strbuf *mail,
  98        timestamp_t *time, struct strbuf *tz)
  99{
 100        struct ident_split ident;
 101        size_t len, maillen, namelen;
 102        char *tmp, *endp;
 103        const char *namebuf, *mailbuf;
 104
 105        tmp = strstr(inbuf, what);
 106        if (!tmp)
 107                goto error_out;
 108        tmp += strlen(what);
 109        endp = strchr(tmp, '\n');
 110        if (!endp)
 111                len = strlen(tmp);
 112        else
 113                len = endp - tmp;
 114
 115        if (split_ident_line(&ident, tmp, len)) {
 116        error_out:
 117                /* Ugh */
 118                tmp = "(unknown)";
 119                strbuf_addstr(name, tmp);
 120                strbuf_addstr(mail, tmp);
 121                strbuf_addstr(tz, tmp);
 122                *time = 0;
 123                return;
 124        }
 125
 126        namelen = ident.name_end - ident.name_begin;
 127        namebuf = ident.name_begin;
 128
 129        maillen = ident.mail_end - ident.mail_begin;
 130        mailbuf = ident.mail_begin;
 131
 132        if (ident.date_begin && ident.date_end)
 133                *time = strtoul(ident.date_begin, NULL, 10);
 134        else
 135                *time = 0;
 136
 137        if (ident.tz_begin && ident.tz_end)
 138                strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
 139        else
 140                strbuf_addstr(tz, "(unknown)");
 141
 142        /*
 143         * Now, convert both name and e-mail using mailmap
 144         */
 145        map_user(&mailmap, &mailbuf, &maillen,
 146                 &namebuf, &namelen);
 147
 148        strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
 149        strbuf_add(name, namebuf, namelen);
 150}
 151
 152static void commit_info_init(struct commit_info *ci)
 153{
 154
 155        strbuf_init(&ci->author, 0);
 156        strbuf_init(&ci->author_mail, 0);
 157        strbuf_init(&ci->author_tz, 0);
 158        strbuf_init(&ci->committer, 0);
 159        strbuf_init(&ci->committer_mail, 0);
 160        strbuf_init(&ci->committer_tz, 0);
 161        strbuf_init(&ci->summary, 0);
 162}
 163
 164static void commit_info_destroy(struct commit_info *ci)
 165{
 166
 167        strbuf_release(&ci->author);
 168        strbuf_release(&ci->author_mail);
 169        strbuf_release(&ci->author_tz);
 170        strbuf_release(&ci->committer);
 171        strbuf_release(&ci->committer_mail);
 172        strbuf_release(&ci->committer_tz);
 173        strbuf_release(&ci->summary);
 174}
 175
 176static void get_commit_info(struct commit *commit,
 177                            struct commit_info *ret,
 178                            int detailed)
 179{
 180        int len;
 181        const char *subject, *encoding;
 182        const char *message;
 183
 184        commit_info_init(ret);
 185
 186        encoding = get_log_output_encoding();
 187        message = logmsg_reencode(commit, NULL, encoding);
 188        get_ac_line(message, "\nauthor ",
 189                    &ret->author, &ret->author_mail,
 190                    &ret->author_time, &ret->author_tz);
 191
 192        if (!detailed) {
 193                unuse_commit_buffer(commit, message);
 194                return;
 195        }
 196
 197        get_ac_line(message, "\ncommitter ",
 198                    &ret->committer, &ret->committer_mail,
 199                    &ret->committer_time, &ret->committer_tz);
 200
 201        len = find_commit_subject(message, &subject);
 202        if (len)
 203                strbuf_add(&ret->summary, subject, len);
 204        else
 205                strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
 206
 207        unuse_commit_buffer(commit, message);
 208}
 209
 210/*
 211 * Write out any suspect information which depends on the path. This must be
 212 * handled separately from emit_one_suspect_detail(), because a given commit
 213 * may have changes in multiple paths. So this needs to appear each time
 214 * we mention a new group.
 215 *
 216 * To allow LF and other nonportable characters in pathnames,
 217 * they are c-style quoted as needed.
 218 */
 219static void write_filename_info(struct blame_origin *suspect)
 220{
 221        if (suspect->previous) {
 222                struct blame_origin *prev = suspect->previous;
 223                printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
 224                write_name_quoted(prev->path, stdout, '\n');
 225        }
 226        printf("filename ");
 227        write_name_quoted(suspect->path, stdout, '\n');
 228}
 229
 230/*
 231 * Porcelain/Incremental format wants to show a lot of details per
 232 * commit.  Instead of repeating this every line, emit it only once,
 233 * the first time each commit appears in the output (unless the
 234 * user has specifically asked for us to repeat).
 235 */
 236static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
 237{
 238        struct commit_info ci;
 239
 240        if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
 241                return 0;
 242
 243        suspect->commit->object.flags |= METAINFO_SHOWN;
 244        get_commit_info(suspect->commit, &ci, 1);
 245        printf("author %s\n", ci.author.buf);
 246        printf("author-mail %s\n", ci.author_mail.buf);
 247        printf("author-time %"PRItime"\n", ci.author_time);
 248        printf("author-tz %s\n", ci.author_tz.buf);
 249        printf("committer %s\n", ci.committer.buf);
 250        printf("committer-mail %s\n", ci.committer_mail.buf);
 251        printf("committer-time %"PRItime"\n", ci.committer_time);
 252        printf("committer-tz %s\n", ci.committer_tz.buf);
 253        printf("summary %s\n", ci.summary.buf);
 254        if (suspect->commit->object.flags & UNINTERESTING)
 255                printf("boundary\n");
 256
 257        commit_info_destroy(&ci);
 258
 259        return 1;
 260}
 261
 262/*
 263 * The blame_entry is found to be guilty for the range.
 264 * Show it in incremental output.
 265 */
 266static void found_guilty_entry(struct blame_entry *ent, void *data)
 267{
 268        struct progress_info *pi = (struct progress_info *)data;
 269
 270        if (incremental) {
 271                struct blame_origin *suspect = ent->suspect;
 272
 273                printf("%s %d %d %d\n",
 274                       oid_to_hex(&suspect->commit->object.oid),
 275                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
 276                emit_one_suspect_detail(suspect, 0);
 277                write_filename_info(suspect);
 278                maybe_flush_or_die(stdout, "stdout");
 279        }
 280        pi->blamed_lines += ent->num_lines;
 281        display_progress(pi->progress, pi->blamed_lines);
 282}
 283
 284static const char *format_time(timestamp_t time, const char *tz_str,
 285                               int show_raw_time)
 286{
 287        static struct strbuf time_buf = STRBUF_INIT;
 288
 289        strbuf_reset(&time_buf);
 290        if (show_raw_time) {
 291                strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
 292        }
 293        else {
 294                const char *time_str;
 295                size_t time_width;
 296                int tz;
 297                tz = atoi(tz_str);
 298                time_str = show_date(time, tz, &blame_date_mode);
 299                strbuf_addstr(&time_buf, time_str);
 300                /*
 301                 * Add space paddings to time_buf to display a fixed width
 302                 * string, and use time_width for display width calibration.
 303                 */
 304                for (time_width = utf8_strwidth(time_str);
 305                     time_width < blame_date_width;
 306                     time_width++)
 307                        strbuf_addch(&time_buf, ' ');
 308        }
 309        return time_buf.buf;
 310}
 311
 312#define OUTPUT_ANNOTATE_COMPAT  001
 313#define OUTPUT_LONG_OBJECT_NAME 002
 314#define OUTPUT_RAW_TIMESTAMP    004
 315#define OUTPUT_PORCELAIN        010
 316#define OUTPUT_SHOW_NAME        020
 317#define OUTPUT_SHOW_NUMBER      040
 318#define OUTPUT_SHOW_SCORE      0100
 319#define OUTPUT_NO_AUTHOR       0200
 320#define OUTPUT_SHOW_EMAIL       0400
 321#define OUTPUT_LINE_PORCELAIN 01000
 322
 323static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
 324{
 325        if (emit_one_suspect_detail(suspect, repeat) ||
 326            (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
 327                write_filename_info(suspect);
 328}
 329
 330static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
 331                           int opt)
 332{
 333        int repeat = opt & OUTPUT_LINE_PORCELAIN;
 334        int cnt;
 335        const char *cp;
 336        struct blame_origin *suspect = ent->suspect;
 337        char hex[GIT_MAX_HEXSZ + 1];
 338
 339        oid_to_hex_r(hex, &suspect->commit->object.oid);
 340        printf("%s %d %d %d\n",
 341               hex,
 342               ent->s_lno + 1,
 343               ent->lno + 1,
 344               ent->num_lines);
 345        emit_porcelain_details(suspect, repeat);
 346
 347        cp = blame_nth_line(sb, ent->lno);
 348        for (cnt = 0; cnt < ent->num_lines; cnt++) {
 349                char ch;
 350                if (cnt) {
 351                        printf("%s %d %d\n", hex,
 352                               ent->s_lno + 1 + cnt,
 353                               ent->lno + 1 + cnt);
 354                        if (repeat)
 355                                emit_porcelain_details(suspect, 1);
 356                }
 357                putchar('\t');
 358                do {
 359                        ch = *cp++;
 360                        putchar(ch);
 361                } while (ch != '\n' &&
 362                         cp < sb->final_buf + sb->final_buf_size);
 363        }
 364
 365        if (sb->final_buf_size && cp[-1] != '\n')
 366                putchar('\n');
 367}
 368
 369static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
 370{
 371        int cnt;
 372        const char *cp;
 373        struct blame_origin *suspect = ent->suspect;
 374        struct commit_info ci;
 375        char hex[GIT_MAX_HEXSZ + 1];
 376        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
 377
 378        get_commit_info(suspect->commit, &ci, 1);
 379        oid_to_hex_r(hex, &suspect->commit->object.oid);
 380
 381        cp = blame_nth_line(sb, ent->lno);
 382        for (cnt = 0; cnt < ent->num_lines; cnt++) {
 383                char ch;
 384                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 385
 386                if (suspect->commit->object.flags & UNINTERESTING) {
 387                        if (blank_boundary)
 388                                memset(hex, ' ', length);
 389                        else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
 390                                length--;
 391                                putchar('^');
 392                        }
 393                }
 394
 395                printf("%.*s", length, hex);
 396                if (opt & OUTPUT_ANNOTATE_COMPAT) {
 397                        const char *name;
 398                        if (opt & OUTPUT_SHOW_EMAIL)
 399                                name = ci.author_mail.buf;
 400                        else
 401                                name = ci.author.buf;
 402                        printf("\t(%10s\t%10s\t%d)", name,
 403                               format_time(ci.author_time, ci.author_tz.buf,
 404                                           show_raw_time),
 405                               ent->lno + 1 + cnt);
 406                } else {
 407                        if (opt & OUTPUT_SHOW_SCORE)
 408                                printf(" %*d %02d",
 409                                       max_score_digits, ent->score,
 410                                       ent->suspect->refcnt);
 411                        if (opt & OUTPUT_SHOW_NAME)
 412                                printf(" %-*.*s", longest_file, longest_file,
 413                                       suspect->path);
 414                        if (opt & OUTPUT_SHOW_NUMBER)
 415                                printf(" %*d", max_orig_digits,
 416                                       ent->s_lno + 1 + cnt);
 417
 418                        if (!(opt & OUTPUT_NO_AUTHOR)) {
 419                                const char *name;
 420                                int pad;
 421                                if (opt & OUTPUT_SHOW_EMAIL)
 422                                        name = ci.author_mail.buf;
 423                                else
 424                                        name = ci.author.buf;
 425                                pad = longest_author - utf8_strwidth(name);
 426                                printf(" (%s%*s %10s",
 427                                       name, pad, "",
 428                                       format_time(ci.author_time,
 429                                                   ci.author_tz.buf,
 430                                                   show_raw_time));
 431                        }
 432                        printf(" %*d) ",
 433                               max_digits, ent->lno + 1 + cnt);
 434                }
 435                do {
 436                        ch = *cp++;
 437                        putchar(ch);
 438                } while (ch != '\n' &&
 439                         cp < sb->final_buf + sb->final_buf_size);
 440        }
 441
 442        if (sb->final_buf_size && cp[-1] != '\n')
 443                putchar('\n');
 444
 445        commit_info_destroy(&ci);
 446}
 447
 448static void output(struct blame_scoreboard *sb, int option)
 449{
 450        struct blame_entry *ent;
 451
 452        if (option & OUTPUT_PORCELAIN) {
 453                for (ent = sb->ent; ent; ent = ent->next) {
 454                        int count = 0;
 455                        struct blame_origin *suspect;
 456                        struct commit *commit = ent->suspect->commit;
 457                        if (commit->object.flags & MORE_THAN_ONE_PATH)
 458                                continue;
 459                        for (suspect = commit->util; suspect; suspect = suspect->next) {
 460                                if (suspect->guilty && count++) {
 461                                        commit->object.flags |= MORE_THAN_ONE_PATH;
 462                                        break;
 463                                }
 464                        }
 465                }
 466        }
 467
 468        for (ent = sb->ent; ent; ent = ent->next) {
 469                if (option & OUTPUT_PORCELAIN)
 470                        emit_porcelain(sb, ent, option);
 471                else {
 472                        emit_other(sb, ent, option);
 473                }
 474        }
 475}
 476
 477/*
 478 * Add phony grafts for use with -S; this is primarily to
 479 * support git's cvsserver that wants to give a linear history
 480 * to its clients.
 481 */
 482static int read_ancestry(const char *graft_file)
 483{
 484        FILE *fp = fopen_or_warn(graft_file, "r");
 485        struct strbuf buf = STRBUF_INIT;
 486        if (!fp)
 487                return -1;
 488        while (!strbuf_getwholeline(&buf, fp, '\n')) {
 489                /* The format is just "Commit Parent1 Parent2 ...\n" */
 490                struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
 491                if (graft)
 492                        register_commit_graft(graft, 0);
 493        }
 494        fclose(fp);
 495        strbuf_release(&buf);
 496        return 0;
 497}
 498
 499static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
 500{
 501        const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
 502                                              auto_abbrev);
 503        int len = strlen(uniq);
 504        if (auto_abbrev < len)
 505                return len;
 506        return auto_abbrev;
 507}
 508
 509/*
 510 * How many columns do we need to show line numbers, authors,
 511 * and filenames?
 512 */
 513static void find_alignment(struct blame_scoreboard *sb, int *option)
 514{
 515        int longest_src_lines = 0;
 516        int longest_dst_lines = 0;
 517        unsigned largest_score = 0;
 518        struct blame_entry *e;
 519        int compute_auto_abbrev = (abbrev < 0);
 520        int auto_abbrev = DEFAULT_ABBREV;
 521
 522        for (e = sb->ent; e; e = e->next) {
 523                struct blame_origin *suspect = e->suspect;
 524                int num;
 525
 526                if (compute_auto_abbrev)
 527                        auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
 528                if (strcmp(suspect->path, sb->path))
 529                        *option |= OUTPUT_SHOW_NAME;
 530                num = strlen(suspect->path);
 531                if (longest_file < num)
 532                        longest_file = num;
 533                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
 534                        struct commit_info ci;
 535                        suspect->commit->object.flags |= METAINFO_SHOWN;
 536                        get_commit_info(suspect->commit, &ci, 1);
 537                        if (*option & OUTPUT_SHOW_EMAIL)
 538                                num = utf8_strwidth(ci.author_mail.buf);
 539                        else
 540                                num = utf8_strwidth(ci.author.buf);
 541                        if (longest_author < num)
 542                                longest_author = num;
 543                        commit_info_destroy(&ci);
 544                }
 545                num = e->s_lno + e->num_lines;
 546                if (longest_src_lines < num)
 547                        longest_src_lines = num;
 548                num = e->lno + e->num_lines;
 549                if (longest_dst_lines < num)
 550                        longest_dst_lines = num;
 551                if (largest_score < blame_entry_score(sb, e))
 552                        largest_score = blame_entry_score(sb, e);
 553        }
 554        max_orig_digits = decimal_width(longest_src_lines);
 555        max_digits = decimal_width(longest_dst_lines);
 556        max_score_digits = decimal_width(largest_score);
 557
 558        if (compute_auto_abbrev)
 559                /* one more abbrev length is needed for the boundary commit */
 560                abbrev = auto_abbrev + 1;
 561}
 562
 563static void sanity_check_on_fail(struct blame_scoreboard *sb, int baa)
 564{
 565        int opt = OUTPUT_SHOW_SCORE | OUTPUT_SHOW_NUMBER | OUTPUT_SHOW_NAME;
 566        find_alignment(sb, &opt);
 567        output(sb, opt);
 568        die("Baa %d!", baa);
 569}
 570
 571static unsigned parse_score(const char *arg)
 572{
 573        char *end;
 574        unsigned long score = strtoul(arg, &end, 10);
 575        if (*end)
 576                return 0;
 577        return score;
 578}
 579
 580static const char *add_prefix(const char *prefix, const char *path)
 581{
 582        return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 583}
 584
 585static int git_blame_config(const char *var, const char *value, void *cb)
 586{
 587        if (!strcmp(var, "blame.showroot")) {
 588                show_root = git_config_bool(var, value);
 589                return 0;
 590        }
 591        if (!strcmp(var, "blame.blankboundary")) {
 592                blank_boundary = git_config_bool(var, value);
 593                return 0;
 594        }
 595        if (!strcmp(var, "blame.showemail")) {
 596                int *output_option = cb;
 597                if (git_config_bool(var, value))
 598                        *output_option |= OUTPUT_SHOW_EMAIL;
 599                else
 600                        *output_option &= ~OUTPUT_SHOW_EMAIL;
 601                return 0;
 602        }
 603        if (!strcmp(var, "blame.date")) {
 604                if (!value)
 605                        return config_error_nonbool(var);
 606                parse_date_format(value, &blame_date_mode);
 607                return 0;
 608        }
 609
 610        if (git_diff_heuristic_config(var, value, cb) < 0)
 611                return -1;
 612        if (userdiff_config(var, value) < 0)
 613                return -1;
 614
 615        return git_default_config(var, value, cb);
 616}
 617
 618static int blame_copy_callback(const struct option *option, const char *arg, int unset)
 619{
 620        int *opt = option->value;
 621
 622        /*
 623         * -C enables copy from removed files;
 624         * -C -C enables copy from existing files, but only
 625         *       when blaming a new file;
 626         * -C -C -C enables copy from existing files for
 627         *          everybody
 628         */
 629        if (*opt & PICKAXE_BLAME_COPY_HARDER)
 630                *opt |= PICKAXE_BLAME_COPY_HARDEST;
 631        if (*opt & PICKAXE_BLAME_COPY)
 632                *opt |= PICKAXE_BLAME_COPY_HARDER;
 633        *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
 634
 635        if (arg)
 636                blame_copy_score = parse_score(arg);
 637        return 0;
 638}
 639
 640static int blame_move_callback(const struct option *option, const char *arg, int unset)
 641{
 642        int *opt = option->value;
 643
 644        *opt |= PICKAXE_BLAME_MOVE;
 645
 646        if (arg)
 647                blame_move_score = parse_score(arg);
 648        return 0;
 649}
 650
 651int cmd_blame(int argc, const char **argv, const char *prefix)
 652{
 653        struct rev_info revs;
 654        const char *path;
 655        struct blame_scoreboard sb;
 656        struct blame_origin *o;
 657        struct blame_entry *ent = NULL;
 658        long dashdash_pos, lno;
 659        struct progress_info pi = { NULL, 0 };
 660
 661        struct string_list range_list = STRING_LIST_INIT_NODUP;
 662        int output_option = 0, opt = 0;
 663        int show_stats = 0;
 664        const char *revs_file = NULL;
 665        const char *contents_from = NULL;
 666        const struct option options[] = {
 667                OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
 668                OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
 669                OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
 670                OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
 671                OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
 672                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
 673                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
 674                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
 675                OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
 676                OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
 677                OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
 678                OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
 679                OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
 680                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
 681                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
 682                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
 683
 684                /*
 685                 * The following two options are parsed by parse_revision_opt()
 686                 * and are only included here to get included in the "-h"
 687                 * output:
 688                 */
 689                { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
 690
 691                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
 692                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
 693                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
 694                { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
 695                { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
 696                OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
 697                OPT__ABBREV(&abbrev),
 698                OPT_END()
 699        };
 700
 701        struct parse_opt_ctx_t ctx;
 702        int cmd_is_annotate = !strcmp(argv[0], "annotate");
 703        struct range_set ranges;
 704        unsigned int range_i;
 705        long anchor;
 706
 707        git_config(git_blame_config, &output_option);
 708        init_revisions(&revs, NULL);
 709        revs.date_mode = blame_date_mode;
 710        DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
 711        DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
 712
 713        save_commit_buffer = 0;
 714        dashdash_pos = 0;
 715        show_progress = -1;
 716
 717        parse_options_start(&ctx, argc, argv, prefix, options,
 718                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
 719        for (;;) {
 720                switch (parse_options_step(&ctx, options, blame_opt_usage)) {
 721                case PARSE_OPT_HELP:
 722                        exit(129);
 723                case PARSE_OPT_DONE:
 724                        if (ctx.argv[0])
 725                                dashdash_pos = ctx.cpidx;
 726                        goto parse_done;
 727                }
 728
 729                if (!strcmp(ctx.argv[0], "--reverse")) {
 730                        ctx.argv[0] = "--children";
 731                        reverse = 1;
 732                }
 733                parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
 734        }
 735parse_done:
 736        no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
 737        xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
 738        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
 739        argc = parse_options_end(&ctx);
 740
 741        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
 742                if (show_progress > 0)
 743                        die(_("--progress can't be used with --incremental or porcelain formats"));
 744                show_progress = 0;
 745        } else if (show_progress < 0)
 746                show_progress = isatty(2);
 747
 748        if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
 749                /* one more abbrev length is needed for the boundary commit */
 750                abbrev++;
 751        else if (!abbrev)
 752                abbrev = GIT_SHA1_HEXSZ;
 753
 754        if (revs_file && read_ancestry(revs_file))
 755                die_errno("reading graft file '%s' failed", revs_file);
 756
 757        if (cmd_is_annotate) {
 758                output_option |= OUTPUT_ANNOTATE_COMPAT;
 759                blame_date_mode.type = DATE_ISO8601;
 760        } else {
 761                blame_date_mode = revs.date_mode;
 762        }
 763
 764        /* The maximum width used to show the dates */
 765        switch (blame_date_mode.type) {
 766        case DATE_RFC2822:
 767                blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
 768                break;
 769        case DATE_ISO8601_STRICT:
 770                blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
 771                break;
 772        case DATE_ISO8601:
 773                blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
 774                break;
 775        case DATE_RAW:
 776                blame_date_width = sizeof("1161298804 -0700");
 777                break;
 778        case DATE_UNIX:
 779                blame_date_width = sizeof("1161298804");
 780                break;
 781        case DATE_SHORT:
 782                blame_date_width = sizeof("2006-10-19");
 783                break;
 784        case DATE_RELATIVE:
 785                /*
 786                 * TRANSLATORS: This string is used to tell us the
 787                 * maximum display width for a relative timestamp in
 788                 * "git blame" output.  For C locale, "4 years, 11
 789                 * months ago", which takes 22 places, is the longest
 790                 * among various forms of relative timestamps, but
 791                 * your language may need more or fewer display
 792                 * columns.
 793                 */
 794                blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 795                break;
 796        case DATE_NORMAL:
 797                blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 798                break;
 799        case DATE_STRFTIME:
 800                blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
 801                break;
 802        }
 803        blame_date_width -= 1; /* strip the null */
 804
 805        if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
 806                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
 807                        PICKAXE_BLAME_COPY_HARDER);
 808
 809        /*
 810         * We have collected options unknown to us in argv[1..unk]
 811         * which are to be passed to revision machinery if we are
 812         * going to do the "bottom" processing.
 813         *
 814         * The remaining are:
 815         *
 816         * (1) if dashdash_pos != 0, it is either
 817         *     "blame [revisions] -- <path>" or
 818         *     "blame -- <path> <rev>"
 819         *
 820         * (2) otherwise, it is one of the two:
 821         *     "blame [revisions] <path>"
 822         *     "blame <path> <rev>"
 823         *
 824         * Note that we must strip out <path> from the arguments: we do not
 825         * want the path pruning but we may want "bottom" processing.
 826         */
 827        if (dashdash_pos) {
 828                switch (argc - dashdash_pos - 1) {
 829                case 2: /* (1b) */
 830                        if (argc != 4)
 831                                usage_with_options(blame_opt_usage, options);
 832                        /* reorder for the new way: <rev> -- <path> */
 833                        argv[1] = argv[3];
 834                        argv[3] = argv[2];
 835                        argv[2] = "--";
 836                        /* FALLTHROUGH */
 837                case 1: /* (1a) */
 838                        path = add_prefix(prefix, argv[--argc]);
 839                        argv[argc] = NULL;
 840                        break;
 841                default:
 842                        usage_with_options(blame_opt_usage, options);
 843                }
 844        } else {
 845                if (argc < 2)
 846                        usage_with_options(blame_opt_usage, options);
 847                path = add_prefix(prefix, argv[argc - 1]);
 848                if (argc == 3 && !file_exists(path)) { /* (2b) */
 849                        path = add_prefix(prefix, argv[1]);
 850                        argv[1] = argv[2];
 851                }
 852                argv[argc - 1] = "--";
 853
 854                setup_work_tree();
 855                if (!file_exists(path))
 856                        die_errno("cannot stat path '%s'", path);
 857        }
 858
 859        revs.disable_stdin = 1;
 860        setup_revisions(argc, argv, &revs, NULL);
 861
 862        init_scoreboard(&sb);
 863        sb.revs = &revs;
 864        sb.contents_from = contents_from;
 865        sb.reverse = reverse;
 866        setup_scoreboard(&sb, path, &o);
 867        lno = sb.num_lines;
 868
 869        if (lno && !range_list.nr)
 870                string_list_append(&range_list, "1");
 871
 872        anchor = 1;
 873        range_set_init(&ranges, range_list.nr);
 874        for (range_i = 0; range_i < range_list.nr; ++range_i) {
 875                long bottom, top;
 876                if (parse_range_arg(range_list.items[range_i].string,
 877                                    nth_line_cb, &sb, lno, anchor,
 878                                    &bottom, &top, sb.path))
 879                        usage(blame_usage);
 880                if (lno < top || ((lno || bottom) && lno < bottom))
 881                        die(Q_("file %s has only %lu line",
 882                               "file %s has only %lu lines",
 883                               lno), path, lno);
 884                if (bottom < 1)
 885                        bottom = 1;
 886                if (top < 1)
 887                        top = lno;
 888                bottom--;
 889                range_set_append_unsafe(&ranges, bottom, top);
 890                anchor = top + 1;
 891        }
 892        sort_and_merge_range_set(&ranges);
 893
 894        for (range_i = ranges.nr; range_i > 0; --range_i) {
 895                const struct range *r = &ranges.ranges[range_i - 1];
 896                ent = blame_entry_prepend(ent, r->start, r->end, o);
 897        }
 898
 899        o->suspects = ent;
 900        prio_queue_put(&sb.commits, o->commit);
 901
 902        blame_origin_decref(o);
 903
 904        range_set_release(&ranges);
 905        string_list_clear(&range_list, 0);
 906
 907        sb.ent = NULL;
 908        sb.path = path;
 909
 910        if (blame_move_score)
 911                sb.move_score = blame_move_score;
 912        if (blame_copy_score)
 913                sb.copy_score = blame_copy_score;
 914
 915        sb.debug = DEBUG;
 916        sb.on_sanity_fail = &sanity_check_on_fail;
 917
 918        sb.show_root = show_root;
 919        sb.xdl_opts = xdl_opts;
 920        sb.no_whole_file_rename = no_whole_file_rename;
 921
 922        read_mailmap(&mailmap, NULL);
 923
 924        sb.found_guilty_entry = &found_guilty_entry;
 925        sb.found_guilty_entry_data = &pi;
 926        if (show_progress)
 927                pi.progress = start_progress_delay(_("Blaming lines"),
 928                                                   sb.num_lines, 50, 1);
 929
 930        assign_blame(&sb, opt);
 931
 932        stop_progress(&pi.progress);
 933
 934        if (!incremental)
 935                setup_pager();
 936        else
 937                return 0;
 938
 939        blame_sort_final(&sb);
 940
 941        blame_coalesce(&sb);
 942
 943        if (!(output_option & OUTPUT_PORCELAIN))
 944                find_alignment(&sb, &output_option);
 945
 946        output(&sb, output_option);
 947        free((void *)sb.final_buf);
 948        for (ent = sb.ent; ent; ) {
 949                struct blame_entry *e = ent->next;
 950                free(ent);
 951                ent = e;
 952        }
 953
 954        if (show_stats) {
 955                printf("num read blob: %d\n", sb.num_read_blob);
 956                printf("num get patch: %d\n", sb.num_get_patch);
 957                printf("num commits: %d\n", sb.num_commits);
 958        }
 959        return 0;
 960}