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