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