4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
12 #include "repository.h"
17 #include "string-list.h"
19 #include "parse-options.h"
20 #include "prio-queue.h"
23 #include "line-range.h"
27 #include "object-store.h"
32 static char blame_usage
[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
34 static const char *blame_opt_usage
[] = {
37 N_("<rev-opts> are documented in git-rev-list(1)"),
41 static int longest_file
;
42 static int longest_author
;
43 static int max_orig_digits
;
44 static int max_digits
;
45 static int max_score_digits
;
48 static int blank_boundary
;
49 static int incremental
;
51 static int abbrev
= -1;
52 static int no_whole_file_rename
;
53 static int show_progress
;
54 static char repeated_meta_color
[COLOR_MAXLEN
];
55 static int coloring_mode
;
56 static struct string_list ignore_revs_file_list
= STRING_LIST_INIT_NODUP
;
57 static int mark_unblamable_lines
;
58 static int mark_ignored_lines
;
60 static struct date_mode blame_date_mode
= { DATE_ISO8601
};
61 static size_t blame_date_width
;
63 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
69 static unsigned blame_move_score
;
70 static unsigned blame_copy_score
;
72 /* Remember to update object flag allocation in object.h */
73 #define METAINFO_SHOWN (1u<<12)
74 #define MORE_THAN_ONE_PATH (1u<<13)
76 struct progress_info
{
77 struct progress
*progress
;
81 static const char *nth_line_cb(void *data
, long lno
)
83 return blame_nth_line((struct blame_scoreboard
*)data
, lno
);
87 * Information on commits, used for output.
91 struct strbuf author_mail
;
92 timestamp_t author_time
;
93 struct strbuf author_tz
;
95 /* filled only when asked for details */
96 struct strbuf committer
;
97 struct strbuf committer_mail
;
98 timestamp_t committer_time
;
99 struct strbuf committer_tz
;
101 struct strbuf summary
;
104 #define COMMIT_INFO_INIT { \
105 .author = STRBUF_INIT, \
106 .author_mail = STRBUF_INIT, \
107 .author_tz = STRBUF_INIT, \
108 .committer = STRBUF_INIT, \
109 .committer_mail = STRBUF_INIT, \
110 .committer_tz = STRBUF_INIT, \
111 .summary = STRBUF_INIT, \
115 * Parse author/committer line in the commit object buffer
117 static void get_ac_line(const char *inbuf
, const char *what
,
118 struct strbuf
*name
, struct strbuf
*mail
,
119 timestamp_t
*time
, struct strbuf
*tz
)
121 struct ident_split ident
;
122 size_t len
, maillen
, namelen
;
124 const char *namebuf
, *mailbuf
;
126 tmp
= strstr(inbuf
, what
);
130 endp
= strchr(tmp
, '\n');
136 if (split_ident_line(&ident
, tmp
, len
)) {
140 strbuf_addstr(name
, tmp
);
141 strbuf_addstr(mail
, tmp
);
142 strbuf_addstr(tz
, tmp
);
147 namelen
= ident
.name_end
- ident
.name_begin
;
148 namebuf
= ident
.name_begin
;
150 maillen
= ident
.mail_end
- ident
.mail_begin
;
151 mailbuf
= ident
.mail_begin
;
153 if (ident
.date_begin
&& ident
.date_end
)
154 *time
= strtoul(ident
.date_begin
, NULL
, 10);
158 if (ident
.tz_begin
&& ident
.tz_end
)
159 strbuf_add(tz
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
161 strbuf_addstr(tz
, "(unknown)");
164 * Now, convert both name and e-mail using mailmap
166 map_user(&mailmap
, &mailbuf
, &maillen
,
169 strbuf_addf(mail
, "<%.*s>", (int)maillen
, mailbuf
);
170 strbuf_add(name
, namebuf
, namelen
);
173 static void commit_info_destroy(struct commit_info
*ci
)
176 strbuf_release(&ci
->author
);
177 strbuf_release(&ci
->author_mail
);
178 strbuf_release(&ci
->author_tz
);
179 strbuf_release(&ci
->committer
);
180 strbuf_release(&ci
->committer_mail
);
181 strbuf_release(&ci
->committer_tz
);
182 strbuf_release(&ci
->summary
);
185 static void get_commit_info(struct commit
*commit
,
186 struct commit_info
*ret
,
190 const char *subject
, *encoding
;
193 encoding
= get_log_output_encoding();
194 message
= logmsg_reencode(commit
, NULL
, encoding
);
195 get_ac_line(message
, "\nauthor ",
196 &ret
->author
, &ret
->author_mail
,
197 &ret
->author_time
, &ret
->author_tz
);
200 unuse_commit_buffer(commit
, message
);
204 get_ac_line(message
, "\ncommitter ",
205 &ret
->committer
, &ret
->committer_mail
,
206 &ret
->committer_time
, &ret
->committer_tz
);
208 len
= find_commit_subject(message
, &subject
);
210 strbuf_add(&ret
->summary
, subject
, len
);
212 strbuf_addf(&ret
->summary
, "(%s)", oid_to_hex(&commit
->object
.oid
));
214 unuse_commit_buffer(commit
, message
);
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.
223 * To allow LF and other nonportable characters in pathnames,
224 * they are c-style quoted as needed.
226 static void write_filename_info(struct blame_origin
*suspect
)
228 if (suspect
->previous
) {
229 struct blame_origin
*prev
= suspect
->previous
;
230 printf("previous %s ", oid_to_hex(&prev
->commit
->object
.oid
));
231 write_name_quoted(prev
->path
, stdout
, '\n');
234 write_name_quoted(suspect
->path
, stdout
, '\n');
238 * Porcelain/Incremental format wants to show a lot of details per
239 * commit. Instead of repeating this every line, emit it only once,
240 * the first time each commit appears in the output (unless the
241 * user has specifically asked for us to repeat).
243 static int emit_one_suspect_detail(struct blame_origin
*suspect
, int repeat
)
245 struct commit_info ci
= COMMIT_INFO_INIT
;
247 if (!repeat
&& (suspect
->commit
->object
.flags
& METAINFO_SHOWN
))
250 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
251 get_commit_info(suspect
->commit
, &ci
, 1);
252 printf("author %s\n", ci
.author
.buf
);
253 printf("author-mail %s\n", ci
.author_mail
.buf
);
254 printf("author-time %"PRItime
"\n", ci
.author_time
);
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
);
258 printf("committer-time %"PRItime
"\n", ci
.committer_time
);
259 printf("committer-tz %s\n", ci
.committer_tz
.buf
);
260 printf("summary %s\n", ci
.summary
.buf
);
261 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
262 printf("boundary\n");
264 commit_info_destroy(&ci
);
270 * The blame_entry is found to be guilty for the range.
271 * Show it in incremental output.
273 static void found_guilty_entry(struct blame_entry
*ent
, void *data
)
275 struct progress_info
*pi
= (struct progress_info
*)data
;
278 struct blame_origin
*suspect
= ent
->suspect
;
280 printf("%s %d %d %d\n",
281 oid_to_hex(&suspect
->commit
->object
.oid
),
282 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
283 emit_one_suspect_detail(suspect
, 0);
284 write_filename_info(suspect
);
285 maybe_flush_or_die(stdout
, "stdout");
287 pi
->blamed_lines
+= ent
->num_lines
;
288 display_progress(pi
->progress
, pi
->blamed_lines
);
291 static const char *format_time(timestamp_t time
, const char *tz_str
,
294 static struct strbuf time_buf
= STRBUF_INIT
;
296 strbuf_reset(&time_buf
);
298 strbuf_addf(&time_buf
, "%"PRItime
" %s", time
, tz_str
);
301 const char *time_str
;
305 time_str
= show_date(time
, tz
, &blame_date_mode
);
306 strbuf_addstr(&time_buf
, time_str
);
308 * Add space paddings to time_buf to display a fixed width
309 * string, and use time_width for display width calibration.
311 for (time_width
= utf8_strwidth(time_str
);
312 time_width
< blame_date_width
;
314 strbuf_addch(&time_buf
, ' ');
319 #define OUTPUT_ANNOTATE_COMPAT (1U<<0)
320 #define OUTPUT_LONG_OBJECT_NAME (1U<<1)
321 #define OUTPUT_RAW_TIMESTAMP (1U<<2)
322 #define OUTPUT_PORCELAIN (1U<<3)
323 #define OUTPUT_SHOW_NAME (1U<<4)
324 #define OUTPUT_SHOW_NUMBER (1U<<5)
325 #define OUTPUT_SHOW_SCORE (1U<<6)
326 #define OUTPUT_NO_AUTHOR (1U<<7)
327 #define OUTPUT_SHOW_EMAIL (1U<<8)
328 #define OUTPUT_LINE_PORCELAIN (1U<<9)
329 #define OUTPUT_COLOR_LINE (1U<<10)
330 #define OUTPUT_SHOW_AGE_WITH_COLOR (1U<<11)
332 static void emit_porcelain_details(struct blame_origin
*suspect
, int repeat
)
334 if (emit_one_suspect_detail(suspect
, repeat
) ||
335 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
336 write_filename_info(suspect
);
339 static void emit_porcelain(struct blame_scoreboard
*sb
, struct blame_entry
*ent
,
342 int repeat
= opt
& OUTPUT_LINE_PORCELAIN
;
345 struct blame_origin
*suspect
= ent
->suspect
;
346 char hex
[GIT_MAX_HEXSZ
+ 1];
348 oid_to_hex_r(hex
, &suspect
->commit
->object
.oid
);
349 printf("%s %d %d %d\n",
354 emit_porcelain_details(suspect
, repeat
);
356 cp
= blame_nth_line(sb
, ent
->lno
);
357 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
360 printf("%s %d %d\n", hex
,
361 ent
->s_lno
+ 1 + cnt
,
364 emit_porcelain_details(suspect
, 1);
370 } while (ch
!= '\n' &&
371 cp
< sb
->final_buf
+ sb
->final_buf_size
);
374 if (sb
->final_buf_size
&& cp
[-1] != '\n')
378 static struct color_field
{
380 char col
[COLOR_MAXLEN
];
382 static int colorfield_nr
, colorfield_alloc
;
384 static void parse_color_fields(const char *s
)
386 struct string_list l
= STRING_LIST_INIT_DUP
;
387 struct string_list_item
*item
;
388 enum { EXPECT_DATE
, EXPECT_COLOR
} next
= EXPECT_COLOR
;
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
);
396 for_each_string_list_item(item
, &l
) {
399 colorfield
[colorfield_nr
].hop
= approxidate(item
->string
);
402 ALLOC_GROW(colorfield
, colorfield_nr
+ 1, colorfield_alloc
);
405 if (color_parse(item
->string
, colorfield
[colorfield_nr
].col
))
406 die(_("expecting a color: %s"), item
->string
);
412 if (next
== EXPECT_COLOR
)
413 die(_("must end with a color"));
415 colorfield
[colorfield_nr
].hop
= TIME_MAX
;
416 string_list_clear(&l
, 0);
419 static void setup_default_color_by_age(void)
421 parse_color_fields("blue,12 month ago,white,1 month ago,red");
424 static void determine_line_heat(struct commit_info
*ci
, const char **dest_color
)
428 while (i
< colorfield_nr
&& ci
->author_time
> colorfield
[i
].hop
)
431 *dest_color
= colorfield
[i
].col
;
434 static void emit_other(struct blame_scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
438 struct blame_origin
*suspect
= ent
->suspect
;
439 struct commit_info ci
= COMMIT_INFO_INIT
;
440 char hex
[GIT_MAX_HEXSZ
+ 1];
441 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
442 const char *default_color
= NULL
, *color
= NULL
, *reset
= NULL
;
444 get_commit_info(suspect
->commit
, &ci
, 1);
445 oid_to_hex_r(hex
, &suspect
->commit
->object
.oid
);
447 cp
= blame_nth_line(sb
, ent
->lno
);
449 if (opt
& OUTPUT_SHOW_AGE_WITH_COLOR
) {
450 determine_line_heat(&ci
, &default_color
);
451 color
= default_color
;
452 reset
= GIT_COLOR_RESET
;
455 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
457 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? the_hash_algo
->hexsz
: abbrev
;
459 if (opt
& OUTPUT_COLOR_LINE
) {
461 color
= repeated_meta_color
;
462 reset
= GIT_COLOR_RESET
;
464 color
= default_color
? default_color
: NULL
;
465 reset
= default_color
? GIT_COLOR_RESET
: NULL
;
469 fputs(color
, stdout
);
471 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
473 memset(hex
, ' ', length
);
474 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
480 if (mark_unblamable_lines
&& ent
->unblamable
) {
484 if (mark_ignored_lines
&& ent
->ignored
) {
488 printf("%.*s", length
, hex
);
489 if (opt
& OUTPUT_ANNOTATE_COMPAT
) {
491 if (opt
& OUTPUT_SHOW_EMAIL
)
492 name
= ci
.author_mail
.buf
;
494 name
= ci
.author
.buf
;
495 printf("\t(%10s\t%10s\t%d)", name
,
496 format_time(ci
.author_time
, ci
.author_tz
.buf
,
500 if (opt
& OUTPUT_SHOW_SCORE
)
502 max_score_digits
, ent
->score
,
503 ent
->suspect
->refcnt
);
504 if (opt
& OUTPUT_SHOW_NAME
)
505 printf(" %-*.*s", longest_file
, longest_file
,
507 if (opt
& OUTPUT_SHOW_NUMBER
)
508 printf(" %*d", max_orig_digits
,
509 ent
->s_lno
+ 1 + cnt
);
511 if (!(opt
& OUTPUT_NO_AUTHOR
)) {
514 if (opt
& OUTPUT_SHOW_EMAIL
)
515 name
= ci
.author_mail
.buf
;
517 name
= ci
.author
.buf
;
518 pad
= longest_author
- utf8_strwidth(name
);
519 printf(" (%s%*s %10s",
521 format_time(ci
.author_time
,
526 max_digits
, ent
->lno
+ 1 + cnt
);
529 fputs(reset
, stdout
);
533 } while (ch
!= '\n' &&
534 cp
< sb
->final_buf
+ sb
->final_buf_size
);
537 if (sb
->final_buf_size
&& cp
[-1] != '\n')
540 commit_info_destroy(&ci
);
543 static void output(struct blame_scoreboard
*sb
, int option
)
545 struct blame_entry
*ent
;
547 if (option
& OUTPUT_PORCELAIN
) {
548 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
550 struct blame_origin
*suspect
;
551 struct commit
*commit
= ent
->suspect
->commit
;
552 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
554 for (suspect
= get_blame_suspects(commit
); suspect
; suspect
= suspect
->next
) {
555 if (suspect
->guilty
&& count
++) {
556 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
563 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
564 if (option
& OUTPUT_PORCELAIN
)
565 emit_porcelain(sb
, ent
, option
);
567 emit_other(sb
, ent
, option
);
573 * Add phony grafts for use with -S; this is primarily to
574 * support git's cvsserver that wants to give a linear history
577 static int read_ancestry(const char *graft_file
)
579 FILE *fp
= fopen_or_warn(graft_file
, "r");
580 struct strbuf buf
= STRBUF_INIT
;
583 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
584 /* The format is just "Commit Parent1 Parent2 ...\n" */
585 struct commit_graft
*graft
= read_graft_line(&buf
);
587 register_commit_graft(the_repository
, graft
, 0);
590 strbuf_release(&buf
);
594 static int update_auto_abbrev(int auto_abbrev
, struct blame_origin
*suspect
)
596 const char *uniq
= find_unique_abbrev(&suspect
->commit
->object
.oid
,
598 int len
= strlen(uniq
);
599 if (auto_abbrev
< len
)
605 * How many columns do we need to show line numbers, authors,
608 static void find_alignment(struct blame_scoreboard
*sb
, int *option
)
610 int longest_src_lines
= 0;
611 int longest_dst_lines
= 0;
612 unsigned largest_score
= 0;
613 struct blame_entry
*e
;
614 int compute_auto_abbrev
= (abbrev
< 0);
615 int auto_abbrev
= DEFAULT_ABBREV
;
617 for (e
= sb
->ent
; e
; e
= e
->next
) {
618 struct blame_origin
*suspect
= e
->suspect
;
621 if (compute_auto_abbrev
)
622 auto_abbrev
= update_auto_abbrev(auto_abbrev
, suspect
);
623 if (strcmp(suspect
->path
, sb
->path
))
624 *option
|= OUTPUT_SHOW_NAME
;
625 num
= strlen(suspect
->path
);
626 if (longest_file
< num
)
628 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
629 struct commit_info ci
= COMMIT_INFO_INIT
;
630 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
631 get_commit_info(suspect
->commit
, &ci
, 1);
632 if (*option
& OUTPUT_SHOW_EMAIL
)
633 num
= utf8_strwidth(ci
.author_mail
.buf
);
635 num
= utf8_strwidth(ci
.author
.buf
);
636 if (longest_author
< num
)
637 longest_author
= num
;
638 commit_info_destroy(&ci
);
640 num
= e
->s_lno
+ e
->num_lines
;
641 if (longest_src_lines
< num
)
642 longest_src_lines
= num
;
643 num
= e
->lno
+ e
->num_lines
;
644 if (longest_dst_lines
< num
)
645 longest_dst_lines
= num
;
646 if (largest_score
< blame_entry_score(sb
, e
))
647 largest_score
= blame_entry_score(sb
, e
);
649 max_orig_digits
= decimal_width(longest_src_lines
);
650 max_digits
= decimal_width(longest_dst_lines
);
651 max_score_digits
= decimal_width(largest_score
);
653 if (compute_auto_abbrev
)
654 /* one more abbrev length is needed for the boundary commit */
655 abbrev
= auto_abbrev
+ 1;
658 static void sanity_check_on_fail(struct blame_scoreboard
*sb
, int baa
)
660 int opt
= OUTPUT_SHOW_SCORE
| OUTPUT_SHOW_NUMBER
| OUTPUT_SHOW_NAME
;
661 find_alignment(sb
, &opt
);
666 static unsigned parse_score(const char *arg
)
669 unsigned long score
= strtoul(arg
, &end
, 10);
675 static const char *add_prefix(const char *prefix
, const char *path
)
677 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
680 static int git_blame_config(const char *var
, const char *value
, void *cb
)
682 if (!strcmp(var
, "blame.showroot")) {
683 show_root
= git_config_bool(var
, value
);
686 if (!strcmp(var
, "blame.blankboundary")) {
687 blank_boundary
= git_config_bool(var
, value
);
690 if (!strcmp(var
, "blame.showemail")) {
691 int *output_option
= cb
;
692 if (git_config_bool(var
, value
))
693 *output_option
|= OUTPUT_SHOW_EMAIL
;
695 *output_option
&= ~OUTPUT_SHOW_EMAIL
;
698 if (!strcmp(var
, "blame.date")) {
700 return config_error_nonbool(var
);
701 parse_date_format(value
, &blame_date_mode
);
704 if (!strcmp(var
, "blame.ignorerevsfile")) {
708 ret
= git_config_pathname(&str
, var
, value
);
711 string_list_insert(&ignore_revs_file_list
, str
);
714 if (!strcmp(var
, "blame.markunblamablelines")) {
715 mark_unblamable_lines
= git_config_bool(var
, value
);
718 if (!strcmp(var
, "blame.markignoredlines")) {
719 mark_ignored_lines
= git_config_bool(var
, value
);
722 if (!strcmp(var
, "color.blame.repeatedlines")) {
723 if (color_parse_mem(value
, strlen(value
), repeated_meta_color
))
724 warning(_("invalid value for '%s': '%s'"),
725 "color.blame.repeatedLines", value
);
728 if (!strcmp(var
, "color.blame.highlightrecent")) {
729 parse_color_fields(value
);
733 if (!strcmp(var
, "blame.coloring")) {
734 if (!strcmp(value
, "repeatedLines")) {
735 coloring_mode
|= OUTPUT_COLOR_LINE
;
736 } else if (!strcmp(value
, "highlightRecent")) {
737 coloring_mode
|= OUTPUT_SHOW_AGE_WITH_COLOR
;
738 } else if (!strcmp(value
, "none")) {
739 coloring_mode
&= ~(OUTPUT_COLOR_LINE
|
740 OUTPUT_SHOW_AGE_WITH_COLOR
);
742 warning(_("invalid value for '%s': '%s'"),
743 "blame.coloring", value
);
748 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
750 if (userdiff_config(var
, value
) < 0)
753 return git_default_config(var
, value
, cb
);
756 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
758 int *opt
= option
->value
;
760 BUG_ON_OPT_NEG(unset
);
763 * -C enables copy from removed files;
764 * -C -C enables copy from existing files, but only
765 * when blaming a new file;
766 * -C -C -C enables copy from existing files for
769 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
770 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
771 if (*opt
& PICKAXE_BLAME_COPY
)
772 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
773 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
776 blame_copy_score
= parse_score(arg
);
780 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
782 int *opt
= option
->value
;
784 BUG_ON_OPT_NEG(unset
);
786 *opt
|= PICKAXE_BLAME_MOVE
;
789 blame_move_score
= parse_score(arg
);
793 static int is_a_rev(const char *name
)
795 struct object_id oid
;
797 if (get_oid(name
, &oid
))
799 return OBJ_NONE
< oid_object_info(the_repository
, &oid
, NULL
);
802 static int peel_to_commit_oid(struct object_id
*oid_ret
, void *cbdata
)
804 struct repository
*r
= ((struct blame_scoreboard
*)cbdata
)->repo
;
805 struct object_id oid
;
807 oidcpy(&oid
, oid_ret
);
810 int kind
= oid_object_info(r
, &oid
, NULL
);
811 if (kind
== OBJ_COMMIT
) {
812 oidcpy(oid_ret
, &oid
);
817 obj
= deref_tag(r
, parse_object(r
, &oid
), NULL
, 0);
820 oidcpy(&oid
, &obj
->oid
);
824 static void build_ignorelist(struct blame_scoreboard
*sb
,
825 struct string_list
*ignore_revs_file_list
,
826 struct string_list
*ignore_rev_list
)
828 struct string_list_item
*i
;
829 struct object_id oid
;
831 oidset_init(&sb
->ignore_list
, 0);
832 for_each_string_list_item(i
, ignore_revs_file_list
) {
833 if (!strcmp(i
->string
, ""))
834 oidset_clear(&sb
->ignore_list
);
836 oidset_parse_file_carefully(&sb
->ignore_list
, i
->string
,
837 peel_to_commit_oid
, sb
);
839 for_each_string_list_item(i
, ignore_rev_list
) {
840 if (get_oid_committish(i
->string
, &oid
) ||
841 peel_to_commit_oid(&oid
, sb
))
842 die(_("cannot find revision %s to ignore"), i
->string
);
843 oidset_insert(&sb
->ignore_list
, &oid
);
847 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
849 struct rev_info revs
;
851 struct blame_scoreboard sb
;
852 struct blame_origin
*o
;
853 struct blame_entry
*ent
= NULL
;
854 long dashdash_pos
, lno
;
855 struct progress_info pi
= { NULL
, 0 };
857 struct string_list range_list
= STRING_LIST_INIT_NODUP
;
858 struct string_list ignore_rev_list
= STRING_LIST_INIT_NODUP
;
859 int output_option
= 0, opt
= 0;
861 const char *revs_file
= NULL
;
862 const char *contents_from
= NULL
;
863 const struct option options
[] = {
864 OPT_BOOL(0, "incremental", &incremental
, N_("show blame entries as we find them, incrementally")),
865 OPT_BOOL('b', NULL
, &blank_boundary
, N_("do not show object names of boundary commits (Default: off)")),
866 OPT_BOOL(0, "root", &show_root
, N_("do not treat root commits as boundaries (Default: off)")),
867 OPT_BOOL(0, "show-stats", &show_stats
, N_("show work cost statistics")),
868 OPT_BOOL(0, "progress", &show_progress
, N_("force progress reporting")),
869 OPT_BIT(0, "score-debug", &output_option
, N_("show output score for blame entries"), OUTPUT_SHOW_SCORE
),
870 OPT_BIT('f', "show-name", &output_option
, N_("show original filename (Default: auto)"), OUTPUT_SHOW_NAME
),
871 OPT_BIT('n', "show-number", &output_option
, N_("show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER
),
872 OPT_BIT('p', "porcelain", &output_option
, N_("show in a format designed for machine consumption"), OUTPUT_PORCELAIN
),
873 OPT_BIT(0, "line-porcelain", &output_option
, N_("show porcelain format with per-line commit information"), OUTPUT_PORCELAIN
|OUTPUT_LINE_PORCELAIN
),
874 OPT_BIT('c', NULL
, &output_option
, N_("use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT
),
875 OPT_BIT('t', NULL
, &output_option
, N_("show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP
),
876 OPT_BIT('l', NULL
, &output_option
, N_("show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME
),
877 OPT_BIT('s', NULL
, &output_option
, N_("suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR
),
878 OPT_BIT('e', "show-email", &output_option
, N_("show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL
),
879 OPT_BIT('w', NULL
, &xdl_opts
, N_("ignore whitespace differences"), XDF_IGNORE_WHITESPACE
),
880 OPT_STRING_LIST(0, "ignore-rev", &ignore_rev_list
, N_("rev"), N_("ignore <rev> when blaming")),
881 OPT_STRING_LIST(0, "ignore-revs-file", &ignore_revs_file_list
, N_("file"), N_("ignore revisions from <file>")),
882 OPT_BIT(0, "color-lines", &output_option
, N_("color redundant metadata from previous line differently"), OUTPUT_COLOR_LINE
),
883 OPT_BIT(0, "color-by-age", &output_option
, N_("color lines by age"), OUTPUT_SHOW_AGE_WITH_COLOR
),
884 OPT_BIT(0, "minimal", &xdl_opts
, N_("spend extra cycles to find better match"), XDF_NEED_MINIMAL
),
885 OPT_STRING('S', NULL
, &revs_file
, N_("file"), N_("use revisions from <file> instead of calling git-rev-list")),
886 OPT_STRING(0, "contents", &contents_from
, N_("file"), N_("use <file>'s contents as the final image")),
887 OPT_CALLBACK_F('C', NULL
, &opt
, N_("score"), N_("find line copies within and across files"), PARSE_OPT_OPTARG
, blame_copy_callback
),
888 OPT_CALLBACK_F('M', NULL
, &opt
, N_("score"), N_("find line movements within and across files"), PARSE_OPT_OPTARG
, blame_move_callback
),
889 OPT_STRING_LIST('L', NULL
, &range_list
, N_("range"),
890 N_("process only line range <start>,<end> or function :<funcname>")),
891 OPT__ABBREV(&abbrev
),
895 struct parse_opt_ctx_t ctx
;
896 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
897 struct range_set ranges
;
898 unsigned int range_i
;
900 const int hexsz
= the_hash_algo
->hexsz
;
903 setup_default_color_by_age();
904 git_config(git_blame_config
, &output_option
);
905 repo_init_revisions(the_repository
, &revs
, NULL
);
906 revs
.date_mode
= blame_date_mode
;
907 revs
.diffopt
.flags
.allow_textconv
= 1;
908 revs
.diffopt
.flags
.follow_renames
= 1;
910 save_commit_buffer
= 0;
914 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
915 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
917 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
918 case PARSE_OPT_NON_OPTION
:
919 case PARSE_OPT_UNKNOWN
:
922 case PARSE_OPT_ERROR
:
924 case PARSE_OPT_COMPLETE
:
928 dashdash_pos
= ctx
.cpidx
;
932 if (!strcmp(ctx
.argv
[0], "--reverse")) {
933 ctx
.argv
[0] = "--children";
936 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
939 revision_opts_finish(&revs
);
940 no_whole_file_rename
= !revs
.diffopt
.flags
.follow_renames
;
941 xdl_opts
|= revs
.diffopt
.xdl_opts
& XDF_INDENT_HEURISTIC
;
942 revs
.diffopt
.flags
.follow_renames
= 0;
943 argc
= parse_options_end(&ctx
);
945 prepare_repo_settings(the_repository
);
946 the_repository
->settings
.command_requires_full_index
= 0;
948 if (incremental
|| (output_option
& OUTPUT_PORCELAIN
)) {
949 if (show_progress
> 0)
950 die(_("--progress can't be used with --incremental or porcelain formats"));
952 } else if (show_progress
< 0)
953 show_progress
= isatty(2);
955 if (0 < abbrev
&& abbrev
< hexsz
)
956 /* one more abbrev length is needed for the boundary commit */
961 if (revs_file
&& read_ancestry(revs_file
))
962 die_errno("reading graft file '%s' failed", revs_file
);
964 if (cmd_is_annotate
) {
965 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
966 blame_date_mode
.type
= DATE_ISO8601
;
968 blame_date_mode
= revs
.date_mode
;
971 /* The maximum width used to show the dates */
972 switch (blame_date_mode
.type
) {
974 blame_date_width
= sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
976 case DATE_ISO8601_STRICT
:
977 blame_date_width
= sizeof("2006-10-19T16:00:04-07:00");
980 blame_date_width
= sizeof("2006-10-19 16:00:04 -0700");
983 blame_date_width
= sizeof("1161298804 -0700");
986 blame_date_width
= sizeof("1161298804");
989 blame_date_width
= sizeof("2006-10-19");
993 * TRANSLATORS: This string is used to tell us the
994 * maximum display width for a relative timestamp in
995 * "git blame" output. For C locale, "4 years, 11
996 * months ago", which takes 22 places, is the longest
997 * among various forms of relative timestamps, but
998 * your language may need more or fewer display
1001 blame_date_width
= utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
1004 /* If the year is shown, no time is shown */
1005 blame_date_width
= sizeof("Thu Oct 19 16:00");
1008 blame_date_width
= sizeof("Thu Oct 19 16:00:04 2006 -0700");
1011 blame_date_width
= strlen(show_date(0, 0, &blame_date_mode
)) + 1; /* add the null */
1014 blame_date_width
-= 1; /* strip the null */
1016 if (revs
.diffopt
.flags
.find_copies_harder
)
1017 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
1018 PICKAXE_BLAME_COPY_HARDER
);
1021 * We have collected options unknown to us in argv[1..unk]
1022 * which are to be passed to revision machinery if we are
1023 * going to do the "bottom" processing.
1025 * The remaining are:
1027 * (1) if dashdash_pos != 0, it is either
1028 * "blame [revisions] -- <path>" or
1029 * "blame -- <path> <rev>"
1031 * (2) otherwise, it is one of the two:
1032 * "blame [revisions] <path>"
1033 * "blame <path> <rev>"
1035 * Note that we must strip out <path> from the arguments: we do not
1036 * want the path pruning but we may want "bottom" processing.
1039 switch (argc
- dashdash_pos
- 1) {
1042 usage_with_options(blame_opt_usage
, options
);
1043 /* reorder for the new way: <rev> -- <path> */
1049 path
= add_prefix(prefix
, argv
[--argc
]);
1053 usage_with_options(blame_opt_usage
, options
);
1057 usage_with_options(blame_opt_usage
, options
);
1058 if (argc
== 3 && is_a_rev(argv
[argc
- 1])) { /* (2b) */
1059 path
= add_prefix(prefix
, argv
[1]);
1062 if (argc
== 2 && is_a_rev(argv
[1]) && !get_git_work_tree())
1063 die("missing <path> to blame");
1064 path
= add_prefix(prefix
, argv
[argc
- 1]);
1066 argv
[argc
- 1] = "--";
1069 revs
.disable_stdin
= 1;
1070 setup_revisions(argc
, argv
, &revs
, NULL
);
1071 if (!revs
.pending
.nr
&& is_bare_repository()) {
1072 struct commit
*head_commit
;
1073 struct object_id head_oid
;
1075 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1077 !(head_commit
= lookup_commit_reference_gently(revs
.repo
,
1079 die("no such ref: HEAD");
1081 add_pending_object(&revs
, &head_commit
->object
, "HEAD");
1084 init_scoreboard(&sb
);
1086 sb
.contents_from
= contents_from
;
1087 sb
.reverse
= reverse
;
1088 sb
.repo
= the_repository
;
1090 build_ignorelist(&sb
, &ignore_revs_file_list
, &ignore_rev_list
);
1091 string_list_clear(&ignore_revs_file_list
, 0);
1092 string_list_clear(&ignore_rev_list
, 0);
1093 setup_scoreboard(&sb
, &o
);
1096 * Changed-path Bloom filters are disabled when looking
1099 if (!(opt
& PICKAXE_BLAME_COPY
))
1100 setup_blame_bloom_data(&sb
);
1104 if (lno
&& !range_list
.nr
)
1105 string_list_append(&range_list
, "1");
1108 range_set_init(&ranges
, range_list
.nr
);
1109 for (range_i
= 0; range_i
< range_list
.nr
; ++range_i
) {
1111 if (parse_range_arg(range_list
.items
[range_i
].string
,
1112 nth_line_cb
, &sb
, lno
, anchor
,
1113 &bottom
, &top
, sb
.path
,
1114 the_repository
->index
))
1116 if ((!lno
&& (top
|| bottom
)) || lno
< bottom
)
1117 die(Q_("file %s has only %lu line",
1118 "file %s has only %lu lines",
1119 lno
), sb
.path
, lno
);
1122 if (top
< 1 || lno
< top
)
1125 range_set_append_unsafe(&ranges
, bottom
, top
);
1128 sort_and_merge_range_set(&ranges
);
1130 for (range_i
= ranges
.nr
; range_i
> 0; --range_i
) {
1131 const struct range
*r
= &ranges
.ranges
[range_i
- 1];
1132 ent
= blame_entry_prepend(ent
, r
->start
, r
->end
, o
);
1133 num_lines
+= (r
->end
- r
->start
);
1136 num_lines
= sb
.num_lines
;
1139 prio_queue_put(&sb
.commits
, o
->commit
);
1141 blame_origin_decref(o
);
1143 range_set_release(&ranges
);
1144 string_list_clear(&range_list
, 0);
1148 if (blame_move_score
)
1149 sb
.move_score
= blame_move_score
;
1150 if (blame_copy_score
)
1151 sb
.copy_score
= blame_copy_score
;
1153 sb
.debug
= DEBUG_BLAME
;
1154 sb
.on_sanity_fail
= &sanity_check_on_fail
;
1156 sb
.show_root
= show_root
;
1157 sb
.xdl_opts
= xdl_opts
;
1158 sb
.no_whole_file_rename
= no_whole_file_rename
;
1160 read_mailmap(&mailmap
);
1162 sb
.found_guilty_entry
= &found_guilty_entry
;
1163 sb
.found_guilty_entry_data
= &pi
;
1165 pi
.progress
= start_delayed_progress(_("Blaming lines"), num_lines
);
1167 assign_blame(&sb
, opt
);
1169 stop_progress(&pi
.progress
);
1176 blame_sort_final(&sb
);
1178 blame_coalesce(&sb
);
1180 if (!(output_option
& (OUTPUT_COLOR_LINE
| OUTPUT_SHOW_AGE_WITH_COLOR
)))
1181 output_option
|= coloring_mode
;
1183 if (!(output_option
& OUTPUT_PORCELAIN
)) {
1184 find_alignment(&sb
, &output_option
);
1185 if (!*repeated_meta_color
&&
1186 (output_option
& OUTPUT_COLOR_LINE
))
1187 xsnprintf(repeated_meta_color
,
1188 sizeof(repeated_meta_color
),
1189 "%s", GIT_COLOR_CYAN
);
1191 if (output_option
& OUTPUT_ANNOTATE_COMPAT
)
1192 output_option
&= ~(OUTPUT_COLOR_LINE
| OUTPUT_SHOW_AGE_WITH_COLOR
);
1194 output(&sb
, output_option
);
1195 free((void *)sb
.final_buf
);
1196 for (ent
= sb
.ent
; ent
; ) {
1197 struct blame_entry
*e
= ent
->next
;
1203 printf("num read blob: %d\n", sb
.num_read_blob
);
1204 printf("num get patch: %d\n", sb
.num_get_patch
);
1205 printf("num commits: %d\n", sb
.num_commits
);
1209 cleanup_scoreboard(&sb
);
1210 release_revisions(&revs
);