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