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