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