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