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