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