7d9e322d4571e9cbc5cac9289922e9584f7f6573
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(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
651struct blame_entry *blame_entry_prepend(struct blame_entry *head,
652 long start, long end,
653 struct blame_origin *o)
654{
655 struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
656 new_head->lno = start;
657 new_head->num_lines = end - start;
658 new_head->suspect = o;
659 new_head->s_lno = start;
660 new_head->next = head;
661 blame_origin_incref(o);
662 return new_head;
663}
664
665int cmd_blame(int argc, const char **argv, const char *prefix)
666{
667 struct rev_info revs;
668 const char *path;
669 struct blame_scoreboard sb;
670 struct blame_origin *o;
671 struct blame_entry *ent = NULL;
672 long dashdash_pos, lno;
673 struct progress_info pi = { NULL, 0 };
674
675 struct string_list range_list = STRING_LIST_INIT_NODUP;
676 int output_option = 0, opt = 0;
677 int show_stats = 0;
678 const char *revs_file = NULL;
679 const char *contents_from = NULL;
680 const struct option options[] = {
681 OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
682 OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
683 OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
684 OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
685 OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
686 OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
687 OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
688 OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
689 OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
690 OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
691 OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
692 OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
693 OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
694 OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
695 OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
696 OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
697
698 /*
699 * The following two options are parsed by parse_revision_opt()
700 * and are only included here to get included in the "-h"
701 * output:
702 */
703 { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
704
705 OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
706 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
707 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
708 { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
709 { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
710 OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
711 OPT__ABBREV(&abbrev),
712 OPT_END()
713 };
714
715 struct parse_opt_ctx_t ctx;
716 int cmd_is_annotate = !strcmp(argv[0], "annotate");
717 struct range_set ranges;
718 unsigned int range_i;
719 long anchor;
720
721 git_config(git_blame_config, &output_option);
722 init_revisions(&revs, NULL);
723 revs.date_mode = blame_date_mode;
724 DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
725 DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
726
727 save_commit_buffer = 0;
728 dashdash_pos = 0;
729 show_progress = -1;
730
731 parse_options_start(&ctx, argc, argv, prefix, options,
732 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
733 for (;;) {
734 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
735 case PARSE_OPT_HELP:
736 exit(129);
737 case PARSE_OPT_DONE:
738 if (ctx.argv[0])
739 dashdash_pos = ctx.cpidx;
740 goto parse_done;
741 }
742
743 if (!strcmp(ctx.argv[0], "--reverse")) {
744 ctx.argv[0] = "--children";
745 reverse = 1;
746 }
747 parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
748 }
749parse_done:
750 no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
751 xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
752 DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
753 argc = parse_options_end(&ctx);
754
755 if (incremental || (output_option & OUTPUT_PORCELAIN)) {
756 if (show_progress > 0)
757 die(_("--progress can't be used with --incremental or porcelain formats"));
758 show_progress = 0;
759 } else if (show_progress < 0)
760 show_progress = isatty(2);
761
762 if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ)
763 /* one more abbrev length is needed for the boundary commit */
764 abbrev++;
765 else if (!abbrev)
766 abbrev = GIT_SHA1_HEXSZ;
767
768 if (revs_file && read_ancestry(revs_file))
769 die_errno("reading graft file '%s' failed", revs_file);
770
771 if (cmd_is_annotate) {
772 output_option |= OUTPUT_ANNOTATE_COMPAT;
773 blame_date_mode.type = DATE_ISO8601;
774 } else {
775 blame_date_mode = revs.date_mode;
776 }
777
778 /* The maximum width used to show the dates */
779 switch (blame_date_mode.type) {
780 case DATE_RFC2822:
781 blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
782 break;
783 case DATE_ISO8601_STRICT:
784 blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
785 break;
786 case DATE_ISO8601:
787 blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
788 break;
789 case DATE_RAW:
790 blame_date_width = sizeof("1161298804 -0700");
791 break;
792 case DATE_UNIX:
793 blame_date_width = sizeof("1161298804");
794 break;
795 case DATE_SHORT:
796 blame_date_width = sizeof("2006-10-19");
797 break;
798 case DATE_RELATIVE:
799 /* TRANSLATORS: This string is used to tell us the maximum
800 display width for a relative timestamp in "git blame"
801 output. For C locale, "4 years, 11 months ago", which
802 takes 22 places, is the longest among various forms of
803 relative timestamps, but your language may need more or
804 fewer display 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;
813 }
814 blame_date_width -= 1; /* strip the null */
815
816 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
817 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
818 PICKAXE_BLAME_COPY_HARDER);
819
820 /*
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.
824 *
825 * The remaining are:
826 *
827 * (1) if dashdash_pos != 0, it is either
828 * "blame [revisions] -- <path>" or
829 * "blame -- <path> <rev>"
830 *
831 * (2) otherwise, it is one of the two:
832 * "blame [revisions] <path>"
833 * "blame <path> <rev>"
834 *
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.
837 */
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);
854 }
855 } else {
856 if (argc < 2)
857 usage_with_options(blame_opt_usage, options);
858 path = add_prefix(prefix, argv[argc - 1]);
859 if (argc == 3 && !file_exists(path)) { /* (2b) */
860 path = add_prefix(prefix, argv[1]);
861 argv[1] = argv[2];
862 }
863 argv[argc - 1] = "--";
864
865 setup_work_tree();
866 if (!file_exists(path))
867 die_errno("cannot stat path '%s'", path);
868 }
869
870 revs.disable_stdin = 1;
871 setup_revisions(argc, argv, &revs, NULL);
872
873 init_scoreboard(&sb);
874 sb.revs = &revs;
875 sb.contents_from = contents_from;
876 sb.reverse = reverse;
877 setup_scoreboard(&sb, path, &o);
878 lno = sb.num_lines;
879
880 if (lno && !range_list.nr)
881 string_list_append(&range_list, "1");
882
883 anchor = 1;
884 range_set_init(&ranges, range_list.nr);
885 for (range_i = 0; range_i < range_list.nr; ++range_i) {
886 long bottom, top;
887 if (parse_range_arg(range_list.items[range_i].string,
888 nth_line_cb, &sb, lno, anchor,
889 &bottom, &top, sb.path))
890 usage(blame_usage);
891 if (lno < top || ((lno || bottom) && lno < bottom))
892 die(Q_("file %s has only %lu line",
893 "file %s has only %lu lines",
894 lno), path, lno);
895 if (bottom < 1)
896 bottom = 1;
897 if (top < 1)
898 top = lno;
899 bottom--;
900 range_set_append_unsafe(&ranges, bottom, top);
901 anchor = top + 1;
902 }
903 sort_and_merge_range_set(&ranges);
904
905 for (range_i = ranges.nr; range_i > 0; --range_i) {
906 const struct range *r = &ranges.ranges[range_i - 1];
907 ent = blame_entry_prepend(ent, r->start, r->end, o);
908 }
909
910 o->suspects = ent;
911 prio_queue_put(&sb.commits, o->commit);
912
913 blame_origin_decref(o);
914
915 range_set_release(&ranges);
916 string_list_clear(&range_list, 0);
917
918 sb.ent = NULL;
919 sb.path = path;
920
921 if (blame_move_score)
922 sb.move_score = blame_move_score;
923 if (blame_copy_score)
924 sb.copy_score = blame_copy_score;
925
926 sb.debug = DEBUG;
927 sb.on_sanity_fail = &sanity_check_on_fail;
928
929 sb.show_root = show_root;
930 sb.xdl_opts = xdl_opts;
931 sb.no_whole_file_rename = no_whole_file_rename;
932
933 read_mailmap(&mailmap, NULL);
934
935 sb.found_guilty_entry = &found_guilty_entry;
936 sb.found_guilty_entry_data = π
937 if (show_progress)
938 pi.progress = start_progress_delay(_("Blaming lines"),
939 sb.num_lines, 50, 1);
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}