]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/blame.c
alloc.h: move ALLOC_GROW() functions from cache.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
36bf1958
EN
8#include "git-compat-util.h"
9#include "alloc.h"
b2141fc1 10#include "config.h"
cdc2d5f1 11#include "color.h"
cee7f245 12#include "builtin.h"
3f5787f8 13#include "repository.h"
cee7f245 14#include "commit.h"
cee7f245 15#include "diff.h"
cee7f245 16#include "revision.h"
717d1462 17#include "quote.h"
c455c87c 18#include "string-list.h"
f9567384 19#include "mailmap.h"
5817da01 20#include "parse-options.h"
7e6ac6e4 21#include "prio-queue.h"
ffaf9cc0 22#include "utf8.h"
3b8a12e8 23#include "userdiff.h"
25ed3412 24#include "line-range.h"
58dbfa2e 25#include "line-log.h"
dbe44faa 26#include "dir.h"
aba37f49 27#include "progress.h"
cbd53a21 28#include "object-store.h"
dc076ae5 29#include "blame.h"
a544fb08 30#include "refs.h"
610e2b92 31#include "tag.h"
cee7f245 32
ce41720c 33static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
df873811 34static char annotate_usage[] = N_("git annotate [<options>] [<rev-opts>] [<rev>] [--] <file>");
5817da01
PH
35
36static const char *blame_opt_usage[] = {
37 blame_usage,
38 "",
9c9b4f2f 39 N_("<rev-opts> are documented in git-rev-list(1)"),
5817da01
PH
40 NULL
41};
cee7f245 42
df873811
ÆAB
43static const char *annotate_opt_usage[] = {
44 annotate_usage,
45 "",
46 N_("<rev-opts> are documented in git-rev-list(1)"),
47 NULL
48};
49
cee7f245
JH
50static int longest_file;
51static int longest_author;
52static int max_orig_digits;
53static int max_digits;
5ff62c30 54static int max_score_digits;
4c10a5ca 55static int show_root;
85af7929 56static int reverse;
4c10a5ca 57static int blank_boundary;
717d1462 58static int incremental;
582aa00b 59static int xdl_opts;
84393bfd 60static int abbrev = -1;
3d1aa566 61static int no_whole_file_rename;
aba37f49 62static int show_progress;
cdc2d5f1 63static char repeated_meta_color[COLOR_MAXLEN];
0dc95a4d 64static int coloring_mode;
ae3f36de 65static struct string_list ignore_revs_file_list = STRING_LIST_INIT_NODUP;
8934ac8c
BR
66static int mark_unblamable_lines;
67static int mark_ignored_lines;
31653c1a 68
a5481a6c 69static struct date_mode blame_date_mode = { DATE_ISO8601 };
31653c1a
EL
70static size_t blame_date_width;
71
2721ce21 72static struct string_list mailmap = STRING_LIST_INIT_NODUP;
cee7f245 73
e9b9cc56
JH
74#ifndef DEBUG_BLAME
75#define DEBUG_BLAME 0
54a4c617
JH
76#endif
77
4a0fc95f
JH
78static unsigned blame_move_score;
79static unsigned blame_copy_score;
d24bba80 80
208acbfb 81/* Remember to update object flag allocation in object.h */
cee7f245
JH
82#define METAINFO_SHOWN (1u<<12)
83#define MORE_THAN_ONE_PATH (1u<<13)
d24bba80 84
aba37f49
ECA
85struct progress_info {
86 struct progress *progress;
87 int blamed_lines;
33494784
JH
88};
89
25ed3412 90static const char *nth_line_cb(void *data, long lno)
cee7f245 91{
935202bd 92 return blame_nth_line((struct blame_scoreboard *)data, lno);
cee7f245
JH
93}
94
1732a1fd
JH
95/*
96 * Information on commits, used for output.
97 */
9cba13ca 98struct commit_info {
ea02ffa3
AP
99 struct strbuf author;
100 struct strbuf author_mail;
dddbad72 101 timestamp_t author_time;
ea02ffa3 102 struct strbuf author_tz;
cee7f245
JH
103
104 /* filled only when asked for details */
ea02ffa3
AP
105 struct strbuf committer;
106 struct strbuf committer_mail;
dddbad72 107 timestamp_t committer_time;
ea02ffa3 108 struct strbuf committer_tz;
cee7f245 109
ea02ffa3 110 struct strbuf summary;
cee7f245
JH
111};
112
4eb2bfdc
ÆAB
113#define COMMIT_INFO_INIT { \
114 .author = STRBUF_INIT, \
115 .author_mail = STRBUF_INIT, \
116 .author_tz = STRBUF_INIT, \
117 .committer = STRBUF_INIT, \
118 .committer_mail = STRBUF_INIT, \
119 .committer_tz = STRBUF_INIT, \
120 .summary = STRBUF_INIT, \
121}
122
1732a1fd
JH
123/*
124 * Parse author/committer line in the commit object buffer
125 */
cee7f245 126static void get_ac_line(const char *inbuf, const char *what,
ea02ffa3 127 struct strbuf *name, struct strbuf *mail,
dddbad72 128 timestamp_t *time, struct strbuf *tz)
cee7f245 129{
3c020bd5 130 struct ident_split ident;
ea02ffa3
AP
131 size_t len, maillen, namelen;
132 char *tmp, *endp;
133 const char *namebuf, *mailbuf;
cee7f245
JH
134
135 tmp = strstr(inbuf, what);
136 if (!tmp)
137 goto error_out;
138 tmp += strlen(what);
139 endp = strchr(tmp, '\n');
140 if (!endp)
141 len = strlen(tmp);
142 else
143 len = endp - tmp;
3c020bd5
AP
144
145 if (split_ident_line(&ident, tmp, len)) {
cee7f245
JH
146 error_out:
147 /* Ugh */
ea02ffa3
AP
148 tmp = "(unknown)";
149 strbuf_addstr(name, tmp);
150 strbuf_addstr(mail, tmp);
151 strbuf_addstr(tz, tmp);
cee7f245
JH
152 *time = 0;
153 return;
154 }
cee7f245 155
3c020bd5 156 namelen = ident.name_end - ident.name_begin;
ea02ffa3 157 namebuf = ident.name_begin;
cee7f245 158
ea02ffa3
AP
159 maillen = ident.mail_end - ident.mail_begin;
160 mailbuf = ident.mail_begin;
f9567384 161
de5abe9f
RS
162 if (ident.date_begin && ident.date_end)
163 *time = strtoul(ident.date_begin, NULL, 10);
164 else
165 *time = 0;
f9567384 166
de5abe9f
RS
167 if (ident.tz_begin && ident.tz_end)
168 strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
169 else
170 strbuf_addstr(tz, "(unknown)");
f9567384 171
f9567384 172 /*
d20d654f 173 * Now, convert both name and e-mail using mailmap
f9567384 174 */
ea02ffa3
AP
175 map_user(&mailmap, &mailbuf, &maillen,
176 &namebuf, &namelen);
177
178 strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
179 strbuf_add(name, namebuf, namelen);
180}
181
ea02ffa3
AP
182static void commit_info_destroy(struct commit_info *ci)
183{
184
185 strbuf_release(&ci->author);
186 strbuf_release(&ci->author_mail);
187 strbuf_release(&ci->author_tz);
188 strbuf_release(&ci->committer);
189 strbuf_release(&ci->committer_mail);
190 strbuf_release(&ci->committer_tz);
191 strbuf_release(&ci->summary);
cee7f245
JH
192}
193
194static void get_commit_info(struct commit *commit,
195 struct commit_info *ret,
196 int detailed)
197{
198 int len;
e297cf5a 199 const char *subject, *encoding;
b000c59b 200 const char *message;
ea02ffa3 201
e297cf5a 202 encoding = get_log_output_encoding();
5a10d236 203 message = logmsg_reencode(commit, NULL, encoding);
69cd8f63 204 get_ac_line(message, "\nauthor ",
ea02ffa3 205 &ret->author, &ret->author_mail,
cee7f245
JH
206 &ret->author_time, &ret->author_tz);
207
69cd8f63 208 if (!detailed) {
b66103c3 209 unuse_commit_buffer(commit, message);
cee7f245 210 return;
69cd8f63 211 }
cee7f245 212
69cd8f63 213 get_ac_line(message, "\ncommitter ",
ea02ffa3 214 &ret->committer, &ret->committer_mail,
cee7f245
JH
215 &ret->committer_time, &ret->committer_tz);
216
ad98a58b 217 len = find_commit_subject(message, &subject);
ea02ffa3
AP
218 if (len)
219 strbuf_add(&ret->summary, subject, len);
220 else
f2fd0760 221 strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
ea02ffa3 222
b66103c3 223 unuse_commit_buffer(commit, message);
cee7f245
JH
224}
225
1732a1fd 226/*
4e768329
JK
227 * Write out any suspect information which depends on the path. This must be
228 * handled separately from emit_one_suspect_detail(), because a given commit
229 * may have changes in multiple paths. So this needs to appear each time
230 * we mention a new group.
231 *
1732a1fd
JH
232 * To allow LF and other nonportable characters in pathnames,
233 * they are c-style quoted as needed.
234 */
f84afb9c 235static void write_filename_info(struct blame_origin *suspect)
46e5e69d 236{
4e768329 237 if (suspect->previous) {
f84afb9c 238 struct blame_origin *prev = suspect->previous;
4e768329
JK
239 printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
240 write_name_quoted(prev->path, stdout, '\n');
241 }
46e5e69d 242 printf("filename ");
4e768329 243 write_name_quoted(suspect->path, stdout, '\n');
46e5e69d
JH
244}
245
9991030c
JH
246/*
247 * Porcelain/Incremental format wants to show a lot of details per
248 * commit. Instead of repeating this every line, emit it only once,
e86226e3
JK
249 * the first time each commit appears in the output (unless the
250 * user has specifically asked for us to repeat).
9991030c 251 */
f84afb9c 252static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat)
9991030c 253{
4eb2bfdc 254 struct commit_info ci = COMMIT_INFO_INIT;
9991030c 255
e86226e3 256 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
9991030c
JH
257 return 0;
258
259 suspect->commit->object.flags |= METAINFO_SHOWN;
260 get_commit_info(suspect->commit, &ci, 1);
ea02ffa3
AP
261 printf("author %s\n", ci.author.buf);
262 printf("author-mail %s\n", ci.author_mail.buf);
cb71f8bd 263 printf("author-time %"PRItime"\n", ci.author_time);
ea02ffa3
AP
264 printf("author-tz %s\n", ci.author_tz.buf);
265 printf("committer %s\n", ci.committer.buf);
266 printf("committer-mail %s\n", ci.committer_mail.buf);
cb71f8bd 267 printf("committer-time %"PRItime"\n", ci.committer_time);
ea02ffa3
AP
268 printf("committer-tz %s\n", ci.committer_tz.buf);
269 printf("summary %s\n", ci.summary.buf);
9991030c
JH
270 if (suspect->commit->object.flags & UNINTERESTING)
271 printf("boundary\n");
ea02ffa3
AP
272
273 commit_info_destroy(&ci);
274
9991030c
JH
275 return 1;
276}
277
1732a1fd 278/*
7e6ac6e4
DK
279 * The blame_entry is found to be guilty for the range.
280 * Show it in incremental output.
1732a1fd 281 */
8c59921d 282static void found_guilty_entry(struct blame_entry *ent, void *data)
717d1462 283{
8c59921d
JS
284 struct progress_info *pi = (struct progress_info *)data;
285
717d1462 286 if (incremental) {
f84afb9c 287 struct blame_origin *suspect = ent->suspect;
717d1462
LT
288
289 printf("%s %d %d %d\n",
f2fd0760 290 oid_to_hex(&suspect->commit->object.oid),
717d1462 291 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
e86226e3 292 emit_one_suspect_detail(suspect, 0);
4e768329 293 write_filename_info(suspect);
06f59e9f 294 maybe_flush_or_die(stdout, "stdout");
717d1462 295 }
aba37f49
ECA
296 pi->blamed_lines += ent->num_lines;
297 display_progress(pi->progress, pi->blamed_lines);
717d1462
LT
298}
299
dddbad72 300static const char *format_time(timestamp_t time, const char *tz_str,
717d1462
LT
301 int show_raw_time)
302{
bccce0f8 303 static struct strbuf time_buf = STRBUF_INIT;
717d1462 304
bccce0f8 305 strbuf_reset(&time_buf);
717d1462 306 if (show_raw_time) {
cb71f8bd 307 strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
717d1462 308 }
31653c1a 309 else {
ac39b277 310 const char *time_str;
bccce0f8 311 size_t time_width;
ac39b277 312 int tz;
31653c1a 313 tz = atoi(tz_str);
a5481a6c 314 time_str = show_date(time, tz, &blame_date_mode);
bccce0f8
JX
315 strbuf_addstr(&time_buf, time_str);
316 /*
317 * Add space paddings to time_buf to display a fixed width
318 * string, and use time_width for display width calibration.
319 */
320 for (time_width = utf8_strwidth(time_str);
321 time_width < blame_date_width;
322 time_width++)
323 strbuf_addch(&time_buf, ' ');
31653c1a 324 }
bccce0f8 325 return time_buf.buf;
717d1462
LT
326}
327
86795774
HV
328#define OUTPUT_ANNOTATE_COMPAT (1U<<0)
329#define OUTPUT_LONG_OBJECT_NAME (1U<<1)
330#define OUTPUT_RAW_TIMESTAMP (1U<<2)
331#define OUTPUT_PORCELAIN (1U<<3)
332#define OUTPUT_SHOW_NAME (1U<<4)
333#define OUTPUT_SHOW_NUMBER (1U<<5)
334#define OUTPUT_SHOW_SCORE (1U<<6)
335#define OUTPUT_NO_AUTHOR (1U<<7)
336#define OUTPUT_SHOW_EMAIL (1U<<8)
337#define OUTPUT_LINE_PORCELAIN (1U<<9)
338#define OUTPUT_COLOR_LINE (1U<<10)
339#define OUTPUT_SHOW_AGE_WITH_COLOR (1U<<11)
cee7f245 340
f84afb9c 341static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
e86226e3
JK
342{
343 if (emit_one_suspect_detail(suspect, repeat) ||
344 (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
4e768329 345 write_filename_info(suspect);
e86226e3
JK
346}
347
9807b3d6 348static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
e86226e3 349 int opt)
cee7f245 350{
ed747dd5 351 int repeat = opt & OUTPUT_LINE_PORCELAIN;
cee7f245
JH
352 int cnt;
353 const char *cp;
f84afb9c 354 struct blame_origin *suspect = ent->suspect;
dc01505f 355 char hex[GIT_MAX_HEXSZ + 1];
cee7f245 356
2490574d 357 oid_to_hex_r(hex, &suspect->commit->object.oid);
7e6ac6e4 358 printf("%s %d %d %d\n",
cee7f245 359 hex,
cee7f245
JH
360 ent->s_lno + 1,
361 ent->lno + 1,
362 ent->num_lines);
ed747dd5 363 emit_porcelain_details(suspect, repeat);
cee7f245 364
935202bd 365 cp = blame_nth_line(sb, ent->lno);
cee7f245
JH
366 for (cnt = 0; cnt < ent->num_lines; cnt++) {
367 char ch;
ed747dd5 368 if (cnt) {
cee7f245
JH
369 printf("%s %d %d\n", hex,
370 ent->s_lno + 1 + cnt,
371 ent->lno + 1 + cnt);
ed747dd5
JK
372 if (repeat)
373 emit_porcelain_details(suspect, 1);
374 }
cee7f245
JH
375 putchar('\t');
376 do {
377 ch = *cp++;
378 putchar(ch);
379 } while (ch != '\n' &&
380 cp < sb->final_buf + sb->final_buf_size);
381 }
a5ca8367
JS
382
383 if (sb->final_buf_size && cp[-1] != '\n')
384 putchar('\n');
cee7f245
JH
385}
386
25d5f529
SB
387static struct color_field {
388 timestamp_t hop;
389 char col[COLOR_MAXLEN];
390} *colorfield;
391static int colorfield_nr, colorfield_alloc;
392
393static void parse_color_fields(const char *s)
394{
395 struct string_list l = STRING_LIST_INIT_DUP;
396 struct string_list_item *item;
397 enum { EXPECT_DATE, EXPECT_COLOR } next = EXPECT_COLOR;
398
399 colorfield_nr = 0;
400
401 /* Ideally this would be stripped and split at the same time? */
402 string_list_split(&l, s, ',', -1);
403 ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
404
405 for_each_string_list_item(item, &l) {
406 switch (next) {
407 case EXPECT_DATE:
408 colorfield[colorfield_nr].hop = approxidate(item->string);
409 next = EXPECT_COLOR;
410 colorfield_nr++;
411 ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
412 break;
413 case EXPECT_COLOR:
414 if (color_parse(item->string, colorfield[colorfield_nr].col))
415 die(_("expecting a color: %s"), item->string);
416 next = EXPECT_DATE;
417 break;
418 }
419 }
420
421 if (next == EXPECT_COLOR)
1a07e59c 422 die(_("must end with a color"));
25d5f529
SB
423
424 colorfield[colorfield_nr].hop = TIME_MAX;
297bdf07 425 string_list_clear(&l, 0);
25d5f529
SB
426}
427
428static void setup_default_color_by_age(void)
429{
430 parse_color_fields("blue,12 month ago,white,1 month ago,red");
431}
432
8e16effe 433static void determine_line_heat(struct commit_info *ci, const char **dest_color)
25d5f529
SB
434{
435 int i = 0;
25d5f529 436
8e16effe 437 while (i < colorfield_nr && ci->author_time > colorfield[i].hop)
25d5f529
SB
438 i++;
439
440 *dest_color = colorfield[i].col;
441}
442
9807b3d6 443static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
cee7f245
JH
444{
445 int cnt;
446 const char *cp;
f84afb9c 447 struct blame_origin *suspect = ent->suspect;
4eb2bfdc 448 struct commit_info ci = COMMIT_INFO_INIT;
dc01505f 449 char hex[GIT_MAX_HEXSZ + 1];
cee7f245 450 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
25d5f529 451 const char *default_color = NULL, *color = NULL, *reset = NULL;
cee7f245
JH
452
453 get_commit_info(suspect->commit, &ci, 1);
2490574d 454 oid_to_hex_r(hex, &suspect->commit->object.oid);
cee7f245 455
935202bd 456 cp = blame_nth_line(sb, ent->lno);
25d5f529
SB
457
458 if (opt & OUTPUT_SHOW_AGE_WITH_COLOR) {
8e16effe 459 determine_line_heat(&ci, &default_color);
25d5f529
SB
460 color = default_color;
461 reset = GIT_COLOR_RESET;
462 }
463
cee7f245
JH
464 for (cnt = 0; cnt < ent->num_lines; cnt++) {
465 char ch;
31900964 466 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? the_hash_algo->hexsz : abbrev;
b11121d9 467
cdc2d5f1
SB
468 if (opt & OUTPUT_COLOR_LINE) {
469 if (cnt > 0) {
470 color = repeated_meta_color;
471 reset = GIT_COLOR_RESET;
472 } else {
25d5f529
SB
473 color = default_color ? default_color : NULL;
474 reset = default_color ? GIT_COLOR_RESET : NULL;
cdc2d5f1
SB
475 }
476 }
477 if (color)
478 fputs(color, stdout);
479
b11121d9 480 if (suspect->commit->object.flags & UNINTERESTING) {
e68989a7
JH
481 if (blank_boundary)
482 memset(hex, ' ', length);
7ceacdff 483 else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
4c10a5ca
JH
484 length--;
485 putchar('^');
486 }
b11121d9 487 }
cee7f245 488
8934ac8c
BR
489 if (mark_unblamable_lines && ent->unblamable) {
490 length--;
491 putchar('*');
492 }
493 if (mark_ignored_lines && ent->ignored) {
494 length--;
495 putchar('?');
496 }
b11121d9 497 printf("%.*s", length, hex);
1b8cdce9
KB
498 if (opt & OUTPUT_ANNOTATE_COMPAT) {
499 const char *name;
500 if (opt & OUTPUT_SHOW_EMAIL)
ea02ffa3 501 name = ci.author_mail.buf;
1b8cdce9 502 else
ea02ffa3 503 name = ci.author.buf;
1b8cdce9 504 printf("\t(%10s\t%10s\t%d)", name,
ea02ffa3 505 format_time(ci.author_time, ci.author_tz.buf,
cee7f245
JH
506 show_raw_time),
507 ent->lno + 1 + cnt);
1b8cdce9 508 } else {
5ff62c30 509 if (opt & OUTPUT_SHOW_SCORE)
54a4c617
JH
510 printf(" %*d %02d",
511 max_score_digits, ent->score,
512 ent->suspect->refcnt);
cee7f245
JH
513 if (opt & OUTPUT_SHOW_NAME)
514 printf(" %-*.*s", longest_file, longest_file,
515 suspect->path);
516 if (opt & OUTPUT_SHOW_NUMBER)
517 printf(" %*d", max_orig_digits,
518 ent->s_lno + 1 + cnt);
093dc5be 519
ffaf9cc0 520 if (!(opt & OUTPUT_NO_AUTHOR)) {
1b8cdce9
KB
521 const char *name;
522 int pad;
523 if (opt & OUTPUT_SHOW_EMAIL)
ea02ffa3 524 name = ci.author_mail.buf;
1b8cdce9 525 else
ea02ffa3 526 name = ci.author.buf;
1b8cdce9 527 pad = longest_author - utf8_strwidth(name);
ffaf9cc0 528 printf(" (%s%*s %10s",
1b8cdce9 529 name, pad, "",
093dc5be 530 format_time(ci.author_time,
ea02ffa3 531 ci.author_tz.buf,
093dc5be 532 show_raw_time));
ffaf9cc0 533 }
093dc5be 534 printf(" %*d) ",
cee7f245
JH
535 max_digits, ent->lno + 1 + cnt);
536 }
cdc2d5f1
SB
537 if (reset)
538 fputs(reset, stdout);
cee7f245
JH
539 do {
540 ch = *cp++;
541 putchar(ch);
542 } while (ch != '\n' &&
543 cp < sb->final_buf + sb->final_buf_size);
544 }
a5ca8367
JS
545
546 if (sb->final_buf_size && cp[-1] != '\n')
547 putchar('\n');
ea02ffa3
AP
548
549 commit_info_destroy(&ci);
cee7f245
JH
550}
551
9807b3d6 552static void output(struct blame_scoreboard *sb, int option)
cee7f245
JH
553{
554 struct blame_entry *ent;
555
556 if (option & OUTPUT_PORCELAIN) {
557 for (ent = sb->ent; ent; ent = ent->next) {
7e6ac6e4 558 int count = 0;
f84afb9c 559 struct blame_origin *suspect;
7e6ac6e4 560 struct commit *commit = ent->suspect->commit;
cee7f245
JH
561 if (commit->object.flags & MORE_THAN_ONE_PATH)
562 continue;
4e0df4e6 563 for (suspect = get_blame_suspects(commit); suspect; suspect = suspect->next) {
7e6ac6e4
DK
564 if (suspect->guilty && count++) {
565 commit->object.flags |= MORE_THAN_ONE_PATH;
566 break;
567 }
cee7f245
JH
568 }
569 }
570 }
571
572 for (ent = sb->ent; ent; ent = ent->next) {
573 if (option & OUTPUT_PORCELAIN)
e86226e3 574 emit_porcelain(sb, ent, option);
5ff62c30 575 else {
cee7f245 576 emit_other(sb, ent, option);
5ff62c30 577 }
cee7f245
JH
578 }
579}
580
1732a1fd
JH
581/*
582 * Add phony grafts for use with -S; this is primarily to
34baebce 583 * support git's cvsserver that wants to give a linear history
1732a1fd
JH
584 * to its clients.
585 */
cee7f245
JH
586static int read_ancestry(const char *graft_file)
587{
e9d983f1 588 FILE *fp = fopen_or_warn(graft_file, "r");
e228c173 589 struct strbuf buf = STRBUF_INIT;
cee7f245
JH
590 if (!fp)
591 return -1;
e228c173 592 while (!strbuf_getwholeline(&buf, fp, '\n')) {
cee7f245 593 /* The format is just "Commit Parent1 Parent2 ...\n" */
9a934032 594 struct commit_graft *graft = read_graft_line(&buf);
8eaf7986 595 if (graft)
3f5787f8 596 register_commit_graft(the_repository, graft, 0);
cee7f245
JH
597 }
598 fclose(fp);
e228c173 599 strbuf_release(&buf);
cee7f245
JH
600 return 0;
601}
602
f84afb9c 603static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
b31272f7 604{
aab9583f 605 const char *uniq = find_unique_abbrev(&suspect->commit->object.oid,
b31272f7
JH
606 auto_abbrev);
607 int len = strlen(uniq);
608 if (auto_abbrev < len)
609 return len;
610 return auto_abbrev;
611}
612
1732a1fd
JH
613/*
614 * How many columns do we need to show line numbers, authors,
615 * and filenames?
616 */
9807b3d6 617static void find_alignment(struct blame_scoreboard *sb, int *option)
cee7f245
JH
618{
619 int longest_src_lines = 0;
620 int longest_dst_lines = 0;
5ff62c30 621 unsigned largest_score = 0;
cee7f245 622 struct blame_entry *e;
b31272f7 623 int compute_auto_abbrev = (abbrev < 0);
5293284b 624 int auto_abbrev = DEFAULT_ABBREV;
cee7f245
JH
625
626 for (e = sb->ent; e; e = e->next) {
f84afb9c 627 struct blame_origin *suspect = e->suspect;
cee7f245
JH
628 int num;
629
b31272f7
JH
630 if (compute_auto_abbrev)
631 auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
ab3bb800
JH
632 if (strcmp(suspect->path, sb->path))
633 *option |= OUTPUT_SHOW_NAME;
634 num = strlen(suspect->path);
635 if (longest_file < num)
636 longest_file = num;
cee7f245 637 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
4eb2bfdc 638 struct commit_info ci = COMMIT_INFO_INIT;
cee7f245
JH
639 suspect->commit->object.flags |= METAINFO_SHOWN;
640 get_commit_info(suspect->commit, &ci, 1);
1b8cdce9 641 if (*option & OUTPUT_SHOW_EMAIL)
ea02ffa3 642 num = utf8_strwidth(ci.author_mail.buf);
1b8cdce9 643 else
ea02ffa3 644 num = utf8_strwidth(ci.author.buf);
cee7f245
JH
645 if (longest_author < num)
646 longest_author = num;
e6005927 647 commit_info_destroy(&ci);
cee7f245
JH
648 }
649 num = e->s_lno + e->num_lines;
650 if (longest_src_lines < num)
651 longest_src_lines = num;
652 num = e->lno + e->num_lines;
653 if (longest_dst_lines < num)
654 longest_dst_lines = num;
1a31a2d9
JS
655 if (largest_score < blame_entry_score(sb, e))
656 largest_score = blame_entry_score(sb, e);
cee7f245 657 }
ec7ff5ba
ZJS
658 max_orig_digits = decimal_width(longest_src_lines);
659 max_digits = decimal_width(longest_dst_lines);
660 max_score_digits = decimal_width(largest_score);
b31272f7
JH
661
662 if (compute_auto_abbrev)
663 /* one more abbrev length is needed for the boundary commit */
664 abbrev = auto_abbrev + 1;
cee7f245
JH
665}
666
4149c186 667static void sanity_check_on_fail(struct blame_scoreboard *sb, int baa)
54a4c617 668{
4149c186
JS
669 int opt = OUTPUT_SHOW_SCORE | OUTPUT_SHOW_NUMBER | OUTPUT_SHOW_NAME;
670 find_alignment(sb, &opt);
671 output(sb, opt);
672 die("Baa %d!", baa);
54a4c617
JH
673}
674
4a0fc95f
JH
675static unsigned parse_score(const char *arg)
676{
677 char *end;
678 unsigned long score = strtoul(arg, &end, 10);
679 if (*end)
680 return 0;
681 return score;
682}
683
20239bae
JK
684static const char *add_prefix(const char *prefix, const char *path)
685{
097971f5 686 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
20239bae
JK
687}
688
ef90d6d4 689static int git_blame_config(const char *var, const char *value, void *cb)
4c10a5ca
JH
690{
691 if (!strcmp(var, "blame.showroot")) {
692 show_root = git_config_bool(var, value);
693 return 0;
694 }
695 if (!strcmp(var, "blame.blankboundary")) {
696 blank_boundary = git_config_bool(var, value);
697 return 0;
698 }
8b504db3
QN
699 if (!strcmp(var, "blame.showemail")) {
700 int *output_option = cb;
701 if (git_config_bool(var, value))
702 *output_option |= OUTPUT_SHOW_EMAIL;
703 else
704 *output_option &= ~OUTPUT_SHOW_EMAIL;
705 return 0;
706 }
31653c1a
EL
707 if (!strcmp(var, "blame.date")) {
708 if (!value)
709 return config_error_nonbool(var);
a5481a6c 710 parse_date_format(value, &blame_date_mode);
31653c1a
EL
711 return 0;
712 }
ae3f36de
BR
713 if (!strcmp(var, "blame.ignorerevsfile")) {
714 const char *str;
715 int ret;
716
717 ret = git_config_pathname(&str, var, value);
718 if (ret)
719 return ret;
720 string_list_insert(&ignore_revs_file_list, str);
721 return 0;
722 }
8934ac8c
BR
723 if (!strcmp(var, "blame.markunblamablelines")) {
724 mark_unblamable_lines = git_config_bool(var, value);
725 return 0;
726 }
727 if (!strcmp(var, "blame.markignoredlines")) {
728 mark_ignored_lines = git_config_bool(var, value);
729 return 0;
730 }
cdc2d5f1
SB
731 if (!strcmp(var, "color.blame.repeatedlines")) {
732 if (color_parse_mem(value, strlen(value), repeated_meta_color))
1a8aea85
JNA
733 warning(_("invalid value for '%s': '%s'"),
734 "color.blame.repeatedLines", value);
cdc2d5f1
SB
735 return 0;
736 }
25d5f529
SB
737 if (!strcmp(var, "color.blame.highlightrecent")) {
738 parse_color_fields(value);
739 return 0;
740 }
3b8a12e8 741
0dc95a4d
SB
742 if (!strcmp(var, "blame.coloring")) {
743 if (!strcmp(value, "repeatedLines")) {
744 coloring_mode |= OUTPUT_COLOR_LINE;
745 } else if (!strcmp(value, "highlightRecent")) {
746 coloring_mode |= OUTPUT_SHOW_AGE_WITH_COLOR;
747 } else if (!strcmp(value, "none")) {
748 coloring_mode &= ~(OUTPUT_COLOR_LINE |
749 OUTPUT_SHOW_AGE_WITH_COLOR);
750 } else {
1a8aea85
JNA
751 warning(_("invalid value for '%s': '%s'"),
752 "blame.coloring", value);
0dc95a4d
SB
753 return 0;
754 }
755 }
3b8a12e8 756
5b162879
MH
757 if (git_diff_heuristic_config(var, value, cb) < 0)
758 return -1;
6680a087 759 if (userdiff_config(var, value) < 0)
3b8a12e8 760 return -1;
3b8a12e8 761
ef90d6d4 762 return git_default_config(var, value, cb);
4c10a5ca
JH
763}
764
5817da01
PH
765static int blame_copy_callback(const struct option *option, const char *arg, int unset)
766{
767 int *opt = option->value;
768
517fe807
JK
769 BUG_ON_OPT_NEG(unset);
770
5817da01
PH
771 /*
772 * -C enables copy from removed files;
773 * -C -C enables copy from existing files, but only
774 * when blaming a new file;
775 * -C -C -C enables copy from existing files for
776 * everybody
777 */
778 if (*opt & PICKAXE_BLAME_COPY_HARDER)
779 *opt |= PICKAXE_BLAME_COPY_HARDEST;
780 if (*opt & PICKAXE_BLAME_COPY)
781 *opt |= PICKAXE_BLAME_COPY_HARDER;
782 *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
783
784 if (arg)
785 blame_copy_score = parse_score(arg);
786 return 0;
787}
788
789static int blame_move_callback(const struct option *option, const char *arg, int unset)
790{
791 int *opt = option->value;
792
517fe807
JK
793 BUG_ON_OPT_NEG(unset);
794
5817da01
PH
795 *opt |= PICKAXE_BLAME_MOVE;
796
797 if (arg)
798 blame_move_score = parse_score(arg);
799 return 0;
800}
801
0c668f55
JH
802static int is_a_rev(const char *name)
803{
804 struct object_id oid;
805
806 if (get_oid(name, &oid))
807 return 0;
0df8e965 808 return OBJ_NONE < oid_object_info(the_repository, &oid, NULL);
0c668f55
JH
809}
810
610e2b92
JH
811static int peel_to_commit_oid(struct object_id *oid_ret, void *cbdata)
812{
813 struct repository *r = ((struct blame_scoreboard *)cbdata)->repo;
814 struct object_id oid;
815
816 oidcpy(&oid, oid_ret);
817 while (1) {
818 struct object *obj;
819 int kind = oid_object_info(r, &oid, NULL);
820 if (kind == OBJ_COMMIT) {
821 oidcpy(oid_ret, &oid);
822 return 0;
823 }
824 if (kind != OBJ_TAG)
825 return -1;
826 obj = deref_tag(r, parse_object(r, &oid), NULL, 0);
db7d07f6
RS
827 if (!obj)
828 return -1;
610e2b92
JH
829 oidcpy(&oid, &obj->oid);
830 }
831}
832
ae3f36de
BR
833static void build_ignorelist(struct blame_scoreboard *sb,
834 struct string_list *ignore_revs_file_list,
835 struct string_list *ignore_rev_list)
836{
837 struct string_list_item *i;
838 struct object_id oid;
839
840 oidset_init(&sb->ignore_list, 0);
841 for_each_string_list_item(i, ignore_revs_file_list) {
842 if (!strcmp(i->string, ""))
843 oidset_clear(&sb->ignore_list);
844 else
610e2b92
JH
845 oidset_parse_file_carefully(&sb->ignore_list, i->string,
846 peel_to_commit_oid, sb);
ae3f36de
BR
847 }
848 for_each_string_list_item(i, ignore_rev_list) {
610e2b92
JH
849 if (get_oid_committish(i->string, &oid) ||
850 peel_to_commit_oid(&oid, sb))
ae3f36de
BR
851 die(_("cannot find revision %s to ignore"), i->string);
852 oidset_insert(&sb->ignore_list, &oid);
853 }
854}
855
acca687f 856int cmd_blame(int argc, const char **argv, const char *prefix)
cee7f245
JH
857{
858 struct rev_info revs;
859 const char *path;
9807b3d6 860 struct blame_scoreboard sb;
f84afb9c 861 struct blame_origin *o;
58dbfa2e
ES
862 struct blame_entry *ent = NULL;
863 long dashdash_pos, lno;
8c59921d 864 struct progress_info pi = { NULL, 0 };
5817da01 865
64093fc0 866 struct string_list range_list = STRING_LIST_INIT_NODUP;
ae3f36de 867 struct string_list ignore_rev_list = STRING_LIST_INIT_NODUP;
64093fc0
JK
868 int output_option = 0, opt = 0;
869 int show_stats = 0;
870 const char *revs_file = NULL;
871 const char *contents_from = NULL;
872 const struct option options[] = {
e73fe3dd
ZH
873 OPT_BOOL(0, "incremental", &incremental, N_("show blame entries as we find them, incrementally")),
874 OPT_BOOL('b', NULL, &blank_boundary, N_("do not show object names of boundary commits (Default: off)")),
875 OPT_BOOL(0, "root", &show_root, N_("do not treat root commits as boundaries (Default: off)")),
876 OPT_BOOL(0, "show-stats", &show_stats, N_("show work cost statistics")),
877 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
878 OPT_BIT(0, "score-debug", &output_option, N_("show output score for blame entries"), OUTPUT_SHOW_SCORE),
879 OPT_BIT('f', "show-name", &output_option, N_("show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
880 OPT_BIT('n', "show-number", &output_option, N_("show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
881 OPT_BIT('p', "porcelain", &output_option, N_("show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
882 OPT_BIT(0, "line-porcelain", &output_option, N_("show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
883 OPT_BIT('c', NULL, &output_option, N_("use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
884 OPT_BIT('t', NULL, &output_option, N_("show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
885 OPT_BIT('l', NULL, &output_option, N_("show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
886 OPT_BIT('s', NULL, &output_option, N_("suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
887 OPT_BIT('e', "show-email", &output_option, N_("show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
888 OPT_BIT('w', NULL, &xdl_opts, N_("ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
889 OPT_STRING_LIST(0, "ignore-rev", &ignore_rev_list, N_("rev"), N_("ignore <rev> when blaming")),
890 OPT_STRING_LIST(0, "ignore-revs-file", &ignore_revs_file_list, N_("file"), N_("ignore revisions from <file>")),
cdc2d5f1 891 OPT_BIT(0, "color-lines", &output_option, N_("color redundant metadata from previous line differently"), OUTPUT_COLOR_LINE),
25d5f529 892 OPT_BIT(0, "color-by-age", &output_option, N_("color lines by age"), OUTPUT_SHOW_AGE_WITH_COLOR),
e73fe3dd
ZH
893 OPT_BIT(0, "minimal", &xdl_opts, N_("spend extra cycles to find better match"), XDF_NEED_MINIMAL),
894 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("use revisions from <file> instead of calling git-rev-list")),
895 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("use <file>'s contents as the final image")),
896 OPT_CALLBACK_F('C', NULL, &opt, N_("score"), N_("find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback),
897 OPT_CALLBACK_F('M', NULL, &opt, N_("score"), N_("find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback),
180d641d 898 OPT_STRING_LIST('L', NULL, &range_list, N_("range"),
e73fe3dd 899 N_("process only line range <start>,<end> or function :<funcname>")),
84393bfd 900 OPT__ABBREV(&abbrev),
5817da01
PH
901 OPT_END()
902 };
903
904 struct parse_opt_ctx_t ctx;
7ceacdff 905 int cmd_is_annotate = !strcmp(argv[0], "annotate");
58dbfa2e
ES
906 struct range_set ranges;
907 unsigned int range_i;
52f4d126 908 long anchor;
31900964 909 const int hexsz = the_hash_algo->hexsz;
e5f5d7d4 910 long num_lines = 0;
df873811
ÆAB
911 const char *str_usage = cmd_is_annotate ? annotate_usage : blame_usage;
912 const char **opt_usage = cmd_is_annotate ? annotate_opt_usage : blame_opt_usage;
e68989a7 913
25d5f529 914 setup_default_color_by_age();
8b504db3 915 git_config(git_blame_config, &output_option);
2abf3503 916 repo_init_revisions(the_repository, &revs, NULL);
31653c1a 917 revs.date_mode = blame_date_mode;
0d1e0e78
BW
918 revs.diffopt.flags.allow_textconv = 1;
919 revs.diffopt.flags.follow_renames = 1;
31653c1a 920
612702e8 921 save_commit_buffer = 0;
3f8d5204 922 dashdash_pos = 0;
aba37f49 923 show_progress = -1;
612702e8 924
9ca1169f
SB
925 parse_options_start(&ctx, argc, argv, prefix, options,
926 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
5817da01 927 for (;;) {
df873811 928 switch (parse_options_step(&ctx, options, opt_usage)) {
352e7613
ÆAB
929 case PARSE_OPT_NON_OPTION:
930 case PARSE_OPT_UNKNOWN:
931 break;
5817da01 932 case PARSE_OPT_HELP:
3bb0923f 933 case PARSE_OPT_ERROR:
fa83cc83 934 case PARSE_OPT_SUBCOMMAND:
5817da01 935 exit(129);
a92ec7ef
NTND
936 case PARSE_OPT_COMPLETE:
937 exit(0);
5817da01 938 case PARSE_OPT_DONE:
3f8d5204
PH
939 if (ctx.argv[0])
940 dashdash_pos = ctx.cpidx;
5817da01
PH
941 goto parse_done;
942 }
943
944 if (!strcmp(ctx.argv[0], "--reverse")) {
945 ctx.argv[0] = "--children";
946 reverse = 1;
947 }
df873811 948 parse_revision_opt(&revs, &ctx, options, opt_usage);
5817da01
PH
949 }
950parse_done:
087c7458 951 revision_opts_finish(&revs);
0d1e0e78 952 no_whole_file_rename = !revs.diffopt.flags.follow_renames;
3cde4e02 953 xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
0d1e0e78 954 revs.diffopt.flags.follow_renames = 0;
5817da01
PH
955 argc = parse_options_end(&ctx);
956
add4c864
LD
957 prepare_repo_settings(the_repository);
958 the_repository->settings.command_requires_full_index = 0;
959
aba37f49
ECA
960 if (incremental || (output_option & OUTPUT_PORCELAIN)) {
961 if (show_progress > 0)
e3f54bff 962 die(_("--progress can't be used with --incremental or porcelain formats"));
aba37f49
ECA
963 show_progress = 0;
964 } else if (show_progress < 0)
965 show_progress = isatty(2);
966
31900964 967 if (0 < abbrev && abbrev < hexsz)
b31272f7
JH
968 /* one more abbrev length is needed for the boundary commit */
969 abbrev++;
ed58d808 970 else if (!abbrev)
31900964 971 abbrev = hexsz;
84393bfd 972
aa9ea77d 973 if (revs_file && read_ancestry(revs_file))
d824cbba 974 die_errno("reading graft file '%s' failed", revs_file);
aa9ea77d 975
31653c1a 976 if (cmd_is_annotate) {
7ceacdff 977 output_option |= OUTPUT_ANNOTATE_COMPAT;
a5481a6c 978 blame_date_mode.type = DATE_ISO8601;
31653c1a
EL
979 } else {
980 blame_date_mode = revs.date_mode;
981 }
982
983 /* The maximum width used to show the dates */
a5481a6c 984 switch (blame_date_mode.type) {
31653c1a
EL
985 case DATE_RFC2822:
986 blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
987 break;
466fb674
BB
988 case DATE_ISO8601_STRICT:
989 blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
990 break;
31653c1a
EL
991 case DATE_ISO8601:
992 blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
993 break;
994 case DATE_RAW:
995 blame_date_width = sizeof("1161298804 -0700");
996 break;
642833db
JK
997 case DATE_UNIX:
998 blame_date_width = sizeof("1161298804");
999 break;
31653c1a
EL
1000 case DATE_SHORT:
1001 blame_date_width = sizeof("2006-10-19");
1002 break;
1003 case DATE_RELATIVE:
66f5f6dc
ÆAB
1004 /*
1005 * TRANSLATORS: This string is used to tell us the
1006 * maximum display width for a relative timestamp in
1007 * "git blame" output. For C locale, "4 years, 11
1008 * months ago", which takes 22 places, is the longest
1009 * among various forms of relative timestamps, but
1010 * your language may need more or fewer display
1011 * columns.
1012 */
dd75553b
JX
1013 blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
1014 break;
acdd3776
LT
1015 case DATE_HUMAN:
1016 /* If the year is shown, no time is shown */
1017 blame_date_width = sizeof("Thu Oct 19 16:00");
1018 break;
31653c1a
EL
1019 case DATE_NORMAL:
1020 blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
1021 break;
aa1462cc
JK
1022 case DATE_STRFTIME:
1023 blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
1024 break;
31653c1a
EL
1025 }
1026 blame_date_width -= 1; /* strip the null */
7ceacdff 1027
0d1e0e78 1028 if (revs.diffopt.flags.find_copies_harder)
b3123f98
JH
1029 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
1030 PICKAXE_BLAME_COPY_HARDER);
1031
1732a1fd
JH
1032 /*
1033 * We have collected options unknown to us in argv[1..unk]
cee7f245 1034 * which are to be passed to revision machinery if we are
3dff5379 1035 * going to do the "bottom" processing.
cee7f245
JH
1036 *
1037 * The remaining are:
1038 *
22e5e58a 1039 * (1) if dashdash_pos != 0, it is either
3f8d5204
PH
1040 * "blame [revisions] -- <path>" or
1041 * "blame -- <path> <rev>"
cee7f245 1042 *
22e5e58a 1043 * (2) otherwise, it is one of the two:
3f8d5204
PH
1044 * "blame [revisions] <path>"
1045 * "blame <path> <rev>"
cee7f245 1046 *
3f8d5204
PH
1047 * Note that we must strip out <path> from the arguments: we do not
1048 * want the path pruning but we may want "bottom" processing.
cee7f245 1049 */
3f8d5204
PH
1050 if (dashdash_pos) {
1051 switch (argc - dashdash_pos - 1) {
1052 case 2: /* (1b) */
1053 if (argc != 4)
df873811 1054 usage_with_options(opt_usage, options);
3f8d5204
PH
1055 /* reorder for the new way: <rev> -- <path> */
1056 argv[1] = argv[3];
1057 argv[3] = argv[2];
1058 argv[2] = "--";
1059 /* FALLTHROUGH */
1060 case 1: /* (1a) */
1061 path = add_prefix(prefix, argv[--argc]);
1062 argv[argc] = NULL;
1063 break;
1064 default:
df873811 1065 usage_with_options(opt_usage, options);
cee7f245 1066 }
3f8d5204
PH
1067 } else {
1068 if (argc < 2)
df873811 1069 usage_with_options(opt_usage, options);
0c668f55 1070 if (argc == 3 && is_a_rev(argv[argc - 1])) { /* (2b) */
3f8d5204
PH
1071 path = add_prefix(prefix, argv[1]);
1072 argv[1] = argv[2];
0c668f55
JH
1073 } else { /* (2a) */
1074 if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree())
1075 die("missing <path> to blame");
1076 path = add_prefix(prefix, argv[argc - 1]);
cee7f245 1077 }
3f8d5204 1078 argv[argc - 1] = "--";
cee7f245
JH
1079 }
1080
8b3dce56 1081 revs.disable_stdin = 1;
3f8d5204 1082 setup_revisions(argc, argv, &revs, NULL);
a544fb08
SG
1083 if (!revs.pending.nr && is_bare_repository()) {
1084 struct commit *head_commit;
1085 struct object_id head_oid;
1086
1087 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1088 &head_oid, NULL) ||
1089 !(head_commit = lookup_commit_reference_gently(revs.repo,
1090 &head_oid, 1)))
1091 die("no such ref: HEAD");
1092
1093 add_pending_object(&revs, &head_commit->object, "HEAD");
1094 }
cee7f245 1095
6e4c9b5b 1096 init_scoreboard(&sb);
85af7929 1097 sb.revs = &revs;
84be875e 1098 sb.contents_from = contents_from;
f81d70e9 1099 sb.reverse = reverse;
ecbbc0a5 1100 sb.repo = the_repository;
9466e380 1101 sb.path = path;
ae3f36de
BR
1102 build_ignorelist(&sb, &ignore_revs_file_list, &ignore_rev_list);
1103 string_list_clear(&ignore_revs_file_list, 0);
1104 string_list_clear(&ignore_rev_list, 0);
88894aae 1105 setup_scoreboard(&sb, &o);
0906ac2b
DS
1106
1107 /*
1108 * Changed-path Bloom filters are disabled when looking
1109 * for copies.
1110 */
1111 if (!(opt & PICKAXE_BLAME_COPY))
3af31e87 1112 setup_blame_bloom_data(&sb);
0906ac2b 1113
d0d0ef1f 1114 lno = sb.num_lines;
cee7f245 1115
58dbfa2e 1116 if (lno && !range_list.nr)
aa59e14b 1117 string_list_append(&range_list, "1");
58dbfa2e 1118
52f4d126 1119 anchor = 1;
58dbfa2e
ES
1120 range_set_init(&ranges, range_list.nr);
1121 for (range_i = 0; range_i < range_list.nr; ++range_i) {
1122 long bottom, top;
1123 if (parse_range_arg(range_list.items[range_i].string,
52f4d126 1124 nth_line_cb, &sb, lno, anchor,
f8adbec9
NTND
1125 &bottom, &top, sb.path,
1126 the_repository->index))
df873811 1127 usage(str_usage);
96cfa94e 1128 if ((!lno && (top || bottom)) || lno < bottom)
e3f54bff
VA
1129 die(Q_("file %s has only %lu line",
1130 "file %s has only %lu lines",
9466e380 1131 lno), sb.path, lno);
58dbfa2e
ES
1132 if (bottom < 1)
1133 bottom = 1;
96cfa94e 1134 if (top < 1 || lno < top)
58dbfa2e
ES
1135 top = lno;
1136 bottom--;
1137 range_set_append_unsafe(&ranges, bottom, top);
52f4d126 1138 anchor = top + 1;
58dbfa2e
ES
1139 }
1140 sort_and_merge_range_set(&ranges);
1141
1142 for (range_i = ranges.nr; range_i > 0; --range_i) {
1143 const struct range *r = &ranges.ranges[range_i - 1];
e94f77f0 1144 ent = blame_entry_prepend(ent, r->start, r->end, o);
e5f5d7d4 1145 num_lines += (r->end - r->start);
58dbfa2e 1146 }
e5f5d7d4
ECA
1147 if (!num_lines)
1148 num_lines = sb.num_lines;
7e6ac6e4
DK
1149
1150 o->suspects = ent;
1151 prio_queue_put(&sb.commits, o->commit);
1152
006a0744 1153 blame_origin_decref(o);
58dbfa2e
ES
1154
1155 range_set_release(&ranges);
1156 string_list_clear(&range_list, 0);
cee7f245 1157
7e6ac6e4 1158 sb.ent = NULL;
cee7f245 1159
18ec0d62
JS
1160 if (blame_move_score)
1161 sb.move_score = blame_move_score;
1162 if (blame_copy_score)
1163 sb.copy_score = blame_copy_score;
1164
e9b9cc56 1165 sb.debug = DEBUG_BLAME;
4149c186
JS
1166 sb.on_sanity_fail = &sanity_check_on_fail;
1167
2cf83374 1168 sb.show_root = show_root;
73e1c299 1169 sb.xdl_opts = xdl_opts;
1f44129b 1170 sb.no_whole_file_rename = no_whole_file_rename;
2cf83374 1171
4e168333 1172 read_mailmap(&mailmap);
f9567384 1173
8c59921d
JS
1174 sb.found_guilty_entry = &found_guilty_entry;
1175 sb.found_guilty_entry_data = &pi;
1176 if (show_progress)
e5f5d7d4 1177 pi.progress = start_delayed_progress(_("Blaming lines"), num_lines);
8c59921d 1178
aba37f49
ECA
1179 assign_blame(&sb, opt);
1180
8c59921d
JS
1181 stop_progress(&pi.progress);
1182
b92565dc
MH
1183 if (!incremental)
1184 setup_pager();
835c49f7 1185 else
4b59b2db 1186 goto cleanup;
717d1462 1187
78b06e66 1188 blame_sort_final(&sb);
7e6ac6e4 1189
c6971362 1190 blame_coalesce(&sb);
cee7f245 1191
0dc95a4d
SB
1192 if (!(output_option & (OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR)))
1193 output_option |= coloring_mode;
1194
cdc2d5f1 1195 if (!(output_option & OUTPUT_PORCELAIN)) {
cee7f245 1196 find_alignment(&sb, &output_option);
cdc2d5f1
SB
1197 if (!*repeated_meta_color &&
1198 (output_option & OUTPUT_COLOR_LINE))
022d2ac1
JK
1199 xsnprintf(repeated_meta_color,
1200 sizeof(repeated_meta_color),
1201 "%s", GIT_COLOR_CYAN);
cdc2d5f1
SB
1202 }
1203 if (output_option & OUTPUT_ANNOTATE_COMPAT)
25d5f529 1204 output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);
cee7f245
JH
1205
1206 output(&sb, output_option);
1207 free((void *)sb.final_buf);
1208 for (ent = sb.ent; ent; ) {
1209 struct blame_entry *e = ent->next;
1210 free(ent);
1211 ent = e;
1212 }
c2e525d9 1213
870b39c1 1214 if (show_stats) {
8449528d
JS
1215 printf("num read blob: %d\n", sb.num_read_blob);
1216 printf("num get patch: %d\n", sb.num_get_patch);
1217 printf("num commits: %d\n", sb.num_commits);
c2e525d9 1218 }
0906ac2b 1219
4b59b2db 1220cleanup:
0906ac2b 1221 cleanup_scoreboard(&sb);
2108fe4a 1222 release_revisions(&revs);
cee7f245
JH
1223 return 0;
1224}