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