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