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