doc: describe git svn init --ignore-refs
[git/git-svn.git] / builtin / blame.c
blobd7a2df3b47439c5564a716402e815e0da4f0d8a9
1 /*
2 * Blame
4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
6 */
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"
26 static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
28 static const char *blame_opt_usage[] = {
29 blame_usage,
30 "",
31 N_("<rev-opts> are documented in git-rev-list(1)"),
32 NULL
35 static int longest_file;
36 static int longest_author;
37 static int max_orig_digits;
38 static int max_digits;
39 static int max_score_digits;
40 static int show_root;
41 static int reverse;
42 static int blank_boundary;
43 static int incremental;
44 static int xdl_opts;
45 static int abbrev = -1;
46 static int no_whole_file_rename;
47 static int show_progress;
49 static struct date_mode blame_date_mode = { DATE_ISO8601 };
50 static size_t blame_date_width;
52 static struct string_list mailmap = STRING_LIST_INIT_NODUP;
54 #ifndef DEBUG
55 #define DEBUG 0
56 #endif
58 static unsigned blame_move_score;
59 static unsigned blame_copy_score;
61 /* Remember to update object flag allocation in object.h */
62 #define METAINFO_SHOWN (1u<<12)
63 #define MORE_THAN_ONE_PATH (1u<<13)
65 struct progress_info {
66 struct progress *progress;
67 int blamed_lines;
70 static const char *nth_line_cb(void *data, long lno)
72 return blame_nth_line((struct blame_scoreboard *)data, lno);
76 * Information on commits, used for output.
78 struct commit_info {
79 struct strbuf author;
80 struct strbuf author_mail;
81 timestamp_t author_time;
82 struct strbuf author_tz;
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;
90 struct strbuf summary;
94 * Parse author/committer line in the commit object buffer
96 static void get_ac_line(const char *inbuf, const char *what,
97 struct strbuf *name, struct strbuf *mail,
98 timestamp_t *time, struct strbuf *tz)
100 struct ident_split ident;
101 size_t len, maillen, namelen;
102 char *tmp, *endp;
103 const char *namebuf, *mailbuf;
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;
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;
126 namelen = ident.name_end - ident.name_begin;
127 namebuf = ident.name_begin;
129 maillen = ident.mail_end - ident.mail_begin;
130 mailbuf = ident.mail_begin;
132 if (ident.date_begin && ident.date_end)
133 *time = strtoul(ident.date_begin, NULL, 10);
134 else
135 *time = 0;
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)");
143 * Now, convert both name and e-mail using mailmap
145 map_user(&mailmap, &mailbuf, &maillen,
146 &namebuf, &namelen);
148 strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
149 strbuf_add(name, namebuf, namelen);
152 static void commit_info_init(struct commit_info *ci)
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);
164 static void commit_info_destroy(struct commit_info *ci)
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);
176 static void get_commit_info(struct commit *commit,
177 struct commit_info *ret,
178 int detailed)
180 int len;
181 const char *subject, *encoding;
182 const char *message;
184 commit_info_init(ret);
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);
192 if (!detailed) {
193 unuse_commit_buffer(commit, message);
194 return;
197 get_ac_line(message, "\ncommitter ",
198 &ret->committer, &ret->committer_mail,
199 &ret->committer_time, &ret->committer_tz);
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));
207 unuse_commit_buffer(commit, message);
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.
216 * To allow LF and other nonportable characters in pathnames,
217 * they are c-style quoted as needed.
219 static void write_filename_info(struct blame_origin *suspect)
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');
226 printf("filename ");
227 write_name_quoted(suspect->path, stdout, '\n');
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).
236 static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
238 struct commit_info ci;
240 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
241 return 0;
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");
257 commit_info_destroy(&ci);
259 return 1;
263 * The blame_entry is found to be guilty for the range.
264 * Show it in incremental output.
266 static void found_guilty_entry(struct blame_entry *ent, void *data)
268 struct progress_info *pi = (struct progress_info *)data;
270 if (incremental) {
271 struct blame_origin *suspect = ent->suspect;
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");
280 pi->blamed_lines += ent->num_lines;
281 display_progress(pi->progress, pi->blamed_lines);
284 static const char *format_time(timestamp_t time, const char *tz_str,
285 int show_raw_time)
287 static struct strbuf time_buf = STRBUF_INIT;
289 strbuf_reset(&time_buf);
290 if (show_raw_time) {
291 strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
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);
301 * Add space paddings to time_buf to display a fixed width
302 * string, and use time_width for display width calibration.
304 for (time_width = utf8_strwidth(time_str);
305 time_width < blame_date_width;
306 time_width++)
307 strbuf_addch(&time_buf, ' ');
309 return time_buf.buf;
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
323 static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
325 if (emit_one_suspect_detail(suspect, repeat) ||
326 (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
327 write_filename_info(suspect);
330 static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
331 int opt)
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];
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);
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);
357 putchar('\t');
358 do {
359 ch = *cp++;
360 putchar(ch);
361 } while (ch != '\n' &&
362 cp < sb->final_buf + sb->final_buf_size);
365 if (sb->final_buf_size && cp[-1] != '\n')
366 putchar('\n');
369 static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
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);
378 get_commit_info(suspect->commit, &ci, 1);
379 oid_to_hex_r(hex, &suspect->commit->object.oid);
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;
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('^');
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);
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));
432 printf(" %*d) ",
433 max_digits, ent->lno + 1 + cnt);
435 do {
436 ch = *cp++;
437 putchar(ch);
438 } while (ch != '\n' &&
439 cp < sb->final_buf + sb->final_buf_size);
442 if (sb->final_buf_size && cp[-1] != '\n')
443 putchar('\n');
445 commit_info_destroy(&ci);
448 static void output(struct blame_scoreboard *sb, int option)
450 struct blame_entry *ent;
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;
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);
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.
482 static int read_ancestry(const char *graft_file)
484 FILE *fp = fopen(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);
494 fclose(fp);
495 strbuf_release(&buf);
496 return 0;
499 static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
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;
510 * How many columns do we need to show line numbers, authors,
511 * and filenames?
513 static void find_alignment(struct blame_scoreboard *sb, int *option)
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;
522 for (e = sb->ent; e; e = e->next) {
523 struct blame_origin *suspect = e->suspect;
524 int num;
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);
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);
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);
558 if (compute_auto_abbrev)
559 /* one more abbrev length is needed for the boundary commit */
560 abbrev = auto_abbrev + 1;
563 static void sanity_check_on_fail(struct blame_scoreboard *sb, int baa)
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);
571 static unsigned parse_score(const char *arg)
573 char *end;
574 unsigned long score = strtoul(arg, &end, 10);
575 if (*end)
576 return 0;
577 return score;
580 static const char *add_prefix(const char *prefix, const char *path)
582 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
585 static int git_blame_config(const char *var, const char *value, void *cb)
587 if (!strcmp(var, "blame.showroot")) {
588 show_root = git_config_bool(var, value);
589 return 0;
591 if (!strcmp(var, "blame.blankboundary")) {
592 blank_boundary = git_config_bool(var, value);
593 return 0;
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;
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;
610 if (git_diff_heuristic_config(var, value, cb) < 0)
611 return -1;
612 if (userdiff_config(var, value) < 0)
613 return -1;
615 return git_default_config(var, value, cb);
618 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
620 int *opt = option->value;
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
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;
635 if (arg)
636 blame_copy_score = parse_score(arg);
637 return 0;
640 static int blame_move_callback(const struct option *option, const char *arg, int unset)
642 int *opt = option->value;
644 *opt |= PICKAXE_BLAME_MOVE;
646 if (arg)
647 blame_move_score = parse_score(arg);
648 return 0;
651 int cmd_blame(int argc, const char **argv, const char *prefix)
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 };
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),
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:
689 { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
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()
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;
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);
713 save_commit_buffer = 0;
714 dashdash_pos = 0;
715 show_progress = -1;
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;
729 if (!strcmp(ctx.argv[0], "--reverse")) {
730 ctx.argv[0] = "--children";
731 reverse = 1;
733 parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
735 parse_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);
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);
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;
754 if (revs_file && read_ancestry(revs_file))
755 die_errno("reading graft file '%s' failed", revs_file);
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;
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:
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.
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;
803 blame_date_width -= 1; /* strip the null */
805 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
806 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
807 PICKAXE_BLAME_COPY_HARDER);
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.
814 * The remaining are:
816 * (1) if dashdash_pos != 0, it is either
817 * "blame [revisions] -- <path>" or
818 * "blame -- <path> <rev>"
820 * (2) otherwise, it is one of the two:
821 * "blame [revisions] <path>"
822 * "blame <path> <rev>"
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.
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);
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];
852 argv[argc - 1] = "--";
854 setup_work_tree();
855 if (!file_exists(path))
856 die_errno("cannot stat path '%s'", path);
859 revs.disable_stdin = 1;
860 setup_revisions(argc, argv, &revs, NULL);
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;
869 if (lno && !range_list.nr)
870 string_list_append(&range_list, "1");
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;
892 sort_and_merge_range_set(&ranges);
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);
899 o->suspects = ent;
900 prio_queue_put(&sb.commits, o->commit);
902 blame_origin_decref(o);
904 range_set_release(&ranges);
905 string_list_clear(&range_list, 0);
907 sb.ent = NULL;
908 sb.path = path;
910 if (blame_move_score)
911 sb.move_score = blame_move_score;
912 if (blame_copy_score)
913 sb.copy_score = blame_copy_score;
915 sb.debug = DEBUG;
916 sb.on_sanity_fail = &sanity_check_on_fail;
918 sb.show_root = show_root;
919 sb.xdl_opts = xdl_opts;
920 sb.no_whole_file_rename = no_whole_file_rename;
922 read_mailmap(&mailmap, NULL);
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);
930 assign_blame(&sb, opt);
932 stop_progress(&pi.progress);
934 if (!incremental)
935 setup_pager();
936 else
937 return 0;
939 blame_sort_final(&sb);
941 blame_coalesce(&sb);
943 if (!(output_option & OUTPUT_PORCELAIN))
944 find_alignment(&sb, &output_option);
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;
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);
959 return 0;