4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "path-list.h"
22 static char blame_usage
[] =
23 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
24 " -c Use the same output mode as git-annotate (Default: off)\n"
25 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
26 " -l Show long commit SHA1 (Default: off)\n"
27 " --root Do not treat root commits as boundaries (Default: off)\n"
28 " -t Show raw timestamp (Default: off)\n"
29 " -f, --show-name Show original filename (Default: auto)\n"
30 " -n, --show-number Show original linenumber (Default: off)\n"
31 " -s Suppress author name and timestamp (Default: off)\n"
32 " -p, --porcelain Show in a format designed for machine consumption\n"
33 " -w Ignore whitespace differences\n"
34 " -L n,m Process only line range n,m, counting from 1\n"
35 " -M, -C Find line movements within and across files\n"
36 " --incremental Show blame entries as we find them, incrementally\n"
37 " --contents file Use <file>'s contents as the final image\n"
38 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
40 static int longest_file
;
41 static int longest_author
;
42 static int max_orig_digits
;
43 static int max_digits
;
44 static int max_score_digits
;
46 static int blank_boundary
;
47 static int incremental
;
48 static int cmd_is_annotate
;
49 static int xdl_opts
= XDF_NEED_MINIMAL
;
50 static struct path_list mailmap
;
57 static int num_read_blob
;
58 static int num_get_patch
;
59 static int num_commits
;
61 #define PICKAXE_BLAME_MOVE 01
62 #define PICKAXE_BLAME_COPY 02
63 #define PICKAXE_BLAME_COPY_HARDER 04
64 #define PICKAXE_BLAME_COPY_HARDEST 010
67 * blame for a blame_entry with score lower than these thresholds
68 * is not passed to the parent using move/copy logic.
70 static unsigned blame_move_score
;
71 static unsigned blame_copy_score
;
72 #define BLAME_DEFAULT_MOVE_SCORE 20
73 #define BLAME_DEFAULT_COPY_SCORE 40
75 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76 #define METAINFO_SHOWN (1u<<12)
77 #define MORE_THAN_ONE_PATH (1u<<13)
80 * One blob in a commit that is being suspected
84 struct commit
*commit
;
86 unsigned char blob_sha1
[20];
87 char path
[FLEX_ARRAY
];
91 * Given an origin, prepare mmfile_t structure to be used by the
94 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
97 enum object_type type
;
99 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
100 (unsigned long *)(&(file
->size
)));
109 * Origin is refcounted and usually we keep the blob contents to be
112 static inline struct origin
*origin_incref(struct origin
*o
)
119 static void origin_decref(struct origin
*o
)
121 if (o
&& --o
->refcnt
<= 0) {
124 memset(o
, 0, sizeof(*o
));
130 * Each group of lines is described by a blame_entry; it can be split
131 * as we pass blame to the parents. They form a linked list in the
132 * scoreboard structure, sorted by the target line number.
135 struct blame_entry
*prev
;
136 struct blame_entry
*next
;
138 /* the first line of this group in the final image;
139 * internally all line numbers are 0 based.
143 /* how many lines this group has */
146 /* the commit that introduced this group into the final image */
147 struct origin
*suspect
;
149 /* true if the suspect is truly guilty; false while we have not
150 * checked if the group came from one of its parents.
154 /* the line number of the first line of this group in the
155 * suspect's file; internally all line numbers are 0 based.
159 /* how significant this entry is -- cached to avoid
160 * scanning the lines over and over.
166 * The current state of the blame assignment.
169 /* the final commit (i.e. where we started digging from) */
170 struct commit
*final
;
175 * The contents in the final image.
176 * Used by many functions to obtain contents of the nth line,
177 * indexed with scoreboard.lineno[blame_entry.lno].
179 const char *final_buf
;
180 unsigned long final_buf_size
;
182 /* linked list of blames */
183 struct blame_entry
*ent
;
185 /* look-up a line in the final buffer */
190 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
194 if (a
->commit
!= b
->commit
)
196 return !strcmp(a
->path
, b
->path
);
199 static void sanity_check_refcnt(struct scoreboard
*);
202 * If two blame entries that are next to each other came from
203 * contiguous lines in the same origin (i.e. <commit, path> pair),
204 * merge them together.
206 static void coalesce(struct scoreboard
*sb
)
208 struct blame_entry
*ent
, *next
;
210 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
211 if (same_suspect(ent
->suspect
, next
->suspect
) &&
212 ent
->guilty
== next
->guilty
&&
213 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
214 ent
->num_lines
+= next
->num_lines
;
215 ent
->next
= next
->next
;
217 ent
->next
->prev
= ent
;
218 origin_decref(next
->suspect
);
221 next
= ent
; /* again */
225 if (DEBUG
) /* sanity */
226 sanity_check_refcnt(sb
);
230 * Given a commit and a path in it, create a new origin structure.
231 * The callers that add blame to the scoreboard should use
232 * get_origin() to obtain shared, refcounted copy instead of calling
233 * this function directly.
235 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
238 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
241 strcpy(o
->path
, path
);
246 * Locate an existing origin or create a new one.
248 static struct origin
*get_origin(struct scoreboard
*sb
,
249 struct commit
*commit
,
252 struct blame_entry
*e
;
254 for (e
= sb
->ent
; e
; e
= e
->next
) {
255 if (e
->suspect
->commit
== commit
&&
256 !strcmp(e
->suspect
->path
, path
))
257 return origin_incref(e
->suspect
);
259 return make_origin(commit
, path
);
263 * Fill the blob_sha1 field of an origin if it hasn't, so that later
264 * call to fill_origin_blob() can use it to locate the data. blob_sha1
265 * for an origin is also used to pass the blame for the entire file to
266 * the parent to detect the case where a child's blob is identical to
267 * that of its parent's.
269 static int fill_blob_sha1(struct origin
*origin
)
273 if (!is_null_sha1(origin
->blob_sha1
))
275 if (get_tree_entry(origin
->commit
->object
.sha1
,
277 origin
->blob_sha1
, &mode
))
279 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
283 hashclr(origin
->blob_sha1
);
288 * We have an origin -- check if the same path exists in the
289 * parent and return an origin structure to represent it.
291 static struct origin
*find_origin(struct scoreboard
*sb
,
292 struct commit
*parent
,
293 struct origin
*origin
)
295 struct origin
*porigin
= NULL
;
296 struct diff_options diff_opts
;
297 const char *paths
[2];
301 * Each commit object can cache one origin in that
302 * commit. This is a freestanding copy of origin and
305 struct origin
*cached
= parent
->util
;
306 if (!strcmp(cached
->path
, origin
->path
)) {
308 * The same path between origin and its parent
309 * without renaming -- the most common case.
311 porigin
= get_origin(sb
, parent
, cached
->path
);
314 * If the origin was newly created (i.e. get_origin
315 * would call make_origin if none is found in the
316 * scoreboard), it does not know the blob_sha1,
317 * so copy it. Otherwise porigin was in the
318 * scoreboard and already knows blob_sha1.
320 if (porigin
->refcnt
== 1)
321 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
324 /* otherwise it was not very useful; free it */
329 /* See if the origin->path is different between parent
330 * and origin first. Most of the time they are the
331 * same and diff-tree is fairly efficient about this.
333 diff_setup(&diff_opts
);
334 diff_opts
.recursive
= 1;
335 diff_opts
.detect_rename
= 0;
336 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
337 paths
[0] = origin
->path
;
340 diff_tree_setup_paths(paths
, &diff_opts
);
341 if (diff_setup_done(&diff_opts
) < 0)
344 if (is_null_sha1(origin
->commit
->object
.sha1
))
345 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
347 diff_tree_sha1(parent
->tree
->object
.sha1
,
348 origin
->commit
->tree
->object
.sha1
,
350 diffcore_std(&diff_opts
);
352 /* It is either one entry that says "modified", or "created",
355 if (!diff_queued_diff
.nr
) {
356 /* The path is the same as parent */
357 porigin
= get_origin(sb
, parent
, origin
->path
);
358 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
360 else if (diff_queued_diff
.nr
!= 1)
361 die("internal error in blame::find_origin");
363 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
366 die("internal error in blame::find_origin (%c)",
369 porigin
= get_origin(sb
, parent
, origin
->path
);
370 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
374 /* Did not exist in parent, or type changed */
378 diff_flush(&diff_opts
);
381 * Create a freestanding copy that is not part of
382 * the refcounted origin found in the scoreboard, and
383 * cache it in the commit.
385 struct origin
*cached
;
387 cached
= make_origin(porigin
->commit
, porigin
->path
);
388 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
389 parent
->util
= cached
;
395 * We have an origin -- find the path that corresponds to it in its
396 * parent and return an origin structure to represent it.
398 static struct origin
*find_rename(struct scoreboard
*sb
,
399 struct commit
*parent
,
400 struct origin
*origin
)
402 struct origin
*porigin
= NULL
;
403 struct diff_options diff_opts
;
405 const char *paths
[2];
407 diff_setup(&diff_opts
);
408 diff_opts
.recursive
= 1;
409 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
410 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
411 diff_opts
.single_follow
= origin
->path
;
413 diff_tree_setup_paths(paths
, &diff_opts
);
414 if (diff_setup_done(&diff_opts
) < 0)
417 if (is_null_sha1(origin
->commit
->object
.sha1
))
418 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
420 diff_tree_sha1(parent
->tree
->object
.sha1
,
421 origin
->commit
->tree
->object
.sha1
,
423 diffcore_std(&diff_opts
);
425 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
426 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
427 if ((p
->status
== 'R' || p
->status
== 'C') &&
428 !strcmp(p
->two
->path
, origin
->path
)) {
429 porigin
= get_origin(sb
, parent
, p
->one
->path
);
430 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
434 diff_flush(&diff_opts
);
439 * Parsing of patch chunks...
442 /* line number in postimage; up to but not including this
443 * line is the same as preimage
447 /* preimage line number after this chunk */
450 /* postimage line number after this chunk */
455 struct chunk
*chunks
;
459 struct blame_diff_state
{
460 struct xdiff_emit_state xm
;
462 unsigned hunk_post_context
;
463 unsigned hunk_in_pre_context
: 1;
466 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
468 struct blame_diff_state
*state
= state_
;
470 int off1
, off2
, len1
, len2
, num
;
472 num
= state
->ret
->num
;
473 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
474 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
475 state
->ret
->chunks
[num
- 1].same
++;
477 state
->hunk_in_pre_context
= 0;
479 state
->hunk_post_context
++;
481 state
->hunk_post_context
= 0;
486 if (num
&& state
->hunk_post_context
) {
487 chunk
= &state
->ret
->chunks
[num
- 1];
488 chunk
->p_next
-= state
->hunk_post_context
;
489 chunk
->t_next
-= state
->hunk_post_context
;
491 state
->ret
->num
= ++num
;
492 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
493 sizeof(struct chunk
) * num
);
494 chunk
= &state
->ret
->chunks
[num
- 1];
495 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
500 /* Line numbers in patch output are one based. */
504 chunk
->same
= len2
? off2
: (off2
+ 1);
506 chunk
->p_next
= off1
+ (len1
? len1
: 1);
507 chunk
->t_next
= chunk
->same
+ len2
;
508 state
->hunk_in_pre_context
= 1;
509 state
->hunk_post_context
= 0;
512 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
515 struct blame_diff_state state
;
520 xpp
.flags
= xdl_opts
;
521 xecfg
.ctxlen
= context
;
523 ecb
.outf
= xdiff_outf
;
525 memset(&state
, 0, sizeof(state
));
526 state
.xm
.consume
= process_u_diff
;
527 state
.ret
= xmalloc(sizeof(struct patch
));
528 state
.ret
->chunks
= NULL
;
531 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
533 if (state
.ret
->num
) {
535 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
536 chunk
->p_next
-= state
.hunk_post_context
;
537 chunk
->t_next
-= state
.hunk_post_context
;
543 * Run diff between two origins and grab the patch output, so that
544 * we can pass blame for lines origin is currently suspected for
547 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
549 mmfile_t file_p
, file_o
;
552 fill_origin_blob(parent
, &file_p
);
553 fill_origin_blob(origin
, &file_o
);
554 if (!file_p
.ptr
|| !file_o
.ptr
)
556 patch
= compare_buffer(&file_p
, &file_o
, 0);
561 static void free_patch(struct patch
*p
)
568 * Link in a new blame entry to the scoreboard. Entries that cover the
569 * same line range have been removed from the scoreboard previously.
571 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
573 struct blame_entry
*ent
, *prev
= NULL
;
575 origin_incref(e
->suspect
);
577 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
580 /* prev, if not NULL, is the last one that is below e */
583 e
->next
= prev
->next
;
595 * src typically is on-stack; we want to copy the information in it to
596 * an malloced blame_entry that is already on the linked list of the
597 * scoreboard. The origin of dst loses a refcnt while the origin of src
600 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
602 struct blame_entry
*p
, *n
;
606 origin_incref(src
->suspect
);
607 origin_decref(dst
->suspect
);
608 memcpy(dst
, src
, sizeof(*src
));
614 static const char *nth_line(struct scoreboard
*sb
, int lno
)
616 return sb
->final_buf
+ sb
->lineno
[lno
];
620 * It is known that lines between tlno to same came from parent, and e
621 * has an overlap with that range. it also is known that parent's
622 * line plno corresponds to e's line tlno.
628 * <------------------>
630 * Split e into potentially three parts; before this chunk, the chunk
631 * to be blamed for the parent, and after that portion.
633 static void split_overlap(struct blame_entry
*split
,
634 struct blame_entry
*e
,
635 int tlno
, int plno
, int same
,
636 struct origin
*parent
)
639 memset(split
, 0, sizeof(struct blame_entry
[3]));
641 if (e
->s_lno
< tlno
) {
642 /* there is a pre-chunk part not blamed on parent */
643 split
[0].suspect
= origin_incref(e
->suspect
);
644 split
[0].lno
= e
->lno
;
645 split
[0].s_lno
= e
->s_lno
;
646 split
[0].num_lines
= tlno
- e
->s_lno
;
647 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
648 split
[1].s_lno
= plno
;
651 split
[1].lno
= e
->lno
;
652 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
655 if (same
< e
->s_lno
+ e
->num_lines
) {
656 /* there is a post-chunk part not blamed on parent */
657 split
[2].suspect
= origin_incref(e
->suspect
);
658 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
659 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
660 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
661 chunk_end_lno
= split
[2].lno
;
664 chunk_end_lno
= e
->lno
+ e
->num_lines
;
665 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
668 * if it turns out there is nothing to blame the parent for,
669 * forget about the splitting. !split[1].suspect signals this.
671 if (split
[1].num_lines
< 1)
673 split
[1].suspect
= origin_incref(parent
);
677 * split_overlap() divided an existing blame e into up to three parts
678 * in split. Adjust the linked list of blames in the scoreboard to
681 static void split_blame(struct scoreboard
*sb
,
682 struct blame_entry
*split
,
683 struct blame_entry
*e
)
685 struct blame_entry
*new_entry
;
687 if (split
[0].suspect
&& split
[2].suspect
) {
688 /* The first part (reuse storage for the existing entry e) */
689 dup_entry(e
, &split
[0]);
691 /* The last part -- me */
692 new_entry
= xmalloc(sizeof(*new_entry
));
693 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
694 add_blame_entry(sb
, new_entry
);
696 /* ... and the middle part -- parent */
697 new_entry
= xmalloc(sizeof(*new_entry
));
698 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
699 add_blame_entry(sb
, new_entry
);
701 else if (!split
[0].suspect
&& !split
[2].suspect
)
703 * The parent covers the entire area; reuse storage for
704 * e and replace it with the parent.
706 dup_entry(e
, &split
[1]);
707 else if (split
[0].suspect
) {
708 /* me and then parent */
709 dup_entry(e
, &split
[0]);
711 new_entry
= xmalloc(sizeof(*new_entry
));
712 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
713 add_blame_entry(sb
, new_entry
);
716 /* parent and then me */
717 dup_entry(e
, &split
[1]);
719 new_entry
= xmalloc(sizeof(*new_entry
));
720 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
721 add_blame_entry(sb
, new_entry
);
724 if (DEBUG
) { /* sanity */
725 struct blame_entry
*ent
;
726 int lno
= sb
->ent
->lno
, corrupt
= 0;
728 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
733 lno
+= ent
->num_lines
;
737 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
738 printf("L %8d l %8d n %8d\n",
739 lno
, ent
->lno
, ent
->num_lines
);
740 lno
= ent
->lno
+ ent
->num_lines
;
748 * After splitting the blame, the origins used by the
749 * on-stack blame_entry should lose one refcnt each.
751 static void decref_split(struct blame_entry
*split
)
755 for (i
= 0; i
< 3; i
++)
756 origin_decref(split
[i
].suspect
);
760 * Helper for blame_chunk(). blame_entry e is known to overlap with
761 * the patch hunk; split it and pass blame to the parent.
763 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
764 int tlno
, int plno
, int same
,
765 struct origin
*parent
)
767 struct blame_entry split
[3];
769 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
770 if (split
[1].suspect
)
771 split_blame(sb
, split
, e
);
776 * Find the line number of the last line the target is suspected for.
778 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
780 struct blame_entry
*e
;
781 int last_in_target
= -1;
783 for (e
= sb
->ent
; e
; e
= e
->next
) {
784 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
786 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
787 last_in_target
= e
->s_lno
+ e
->num_lines
;
789 return last_in_target
;
793 * Process one hunk from the patch between the current suspect for
794 * blame_entry e and its parent. Find and split the overlap, and
795 * pass blame to the overlapping part to the parent.
797 static void blame_chunk(struct scoreboard
*sb
,
798 int tlno
, int plno
, int same
,
799 struct origin
*target
, struct origin
*parent
)
801 struct blame_entry
*e
;
803 for (e
= sb
->ent
; e
; e
= e
->next
) {
804 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
806 if (same
<= e
->s_lno
)
808 if (tlno
< e
->s_lno
+ e
->num_lines
)
809 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
814 * We are looking at the origin 'target' and aiming to pass blame
815 * for the lines it is suspected to its parent. Run diff to find
816 * which lines came from parent and pass blame for them.
818 static int pass_blame_to_parent(struct scoreboard
*sb
,
819 struct origin
*target
,
820 struct origin
*parent
)
822 int i
, last_in_target
, plno
, tlno
;
825 last_in_target
= find_last_in_target(sb
, target
);
826 if (last_in_target
< 0)
827 return 1; /* nothing remains for this target */
829 patch
= get_patch(parent
, target
);
831 for (i
= 0; i
< patch
->num
; i
++) {
832 struct chunk
*chunk
= &patch
->chunks
[i
];
834 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
835 plno
= chunk
->p_next
;
836 tlno
= chunk
->t_next
;
838 /* The rest (i.e. anything after tlno) are the same as the parent */
839 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
846 * The lines in blame_entry after splitting blames many times can become
847 * very small and trivial, and at some point it becomes pointless to
848 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
849 * ordinary C program, and it is not worth to say it was copied from
850 * totally unrelated file in the parent.
852 * Compute how trivial the lines in the blame_entry are.
854 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
863 cp
= nth_line(sb
, e
->lno
);
864 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
866 unsigned ch
= *((unsigned char *)cp
);
876 * best_so_far[] and this[] are both a split of an existing blame_entry
877 * that passes blame to the parent. Maintain best_so_far the best split
878 * so far, by comparing this and best_so_far and copying this into
879 * bst_so_far as needed.
881 static void copy_split_if_better(struct scoreboard
*sb
,
882 struct blame_entry
*best_so_far
,
883 struct blame_entry
*this)
887 if (!this[1].suspect
)
889 if (best_so_far
[1].suspect
) {
890 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
894 for (i
= 0; i
< 3; i
++)
895 origin_incref(this[i
].suspect
);
896 decref_split(best_so_far
);
897 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
901 * We are looking at a part of the final image represented by
902 * ent (tlno and same are offset by ent->s_lno).
903 * tlno is where we are looking at in the final image.
904 * up to (but not including) same match preimage.
905 * plno is where we are looking at in the preimage.
907 * <-------------- final image ---------------------->
910 * <---------preimage----->
913 * All line numbers are 0-based.
915 static void handle_split(struct scoreboard
*sb
,
916 struct blame_entry
*ent
,
917 int tlno
, int plno
, int same
,
918 struct origin
*parent
,
919 struct blame_entry
*split
)
921 if (ent
->num_lines
<= tlno
)
924 struct blame_entry
this[3];
927 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
928 copy_split_if_better(sb
, split
, this);
934 * Find the lines from parent that are the same as ent so that
935 * we can pass blames to it. file_p has the blob contents for
938 static void find_copy_in_blob(struct scoreboard
*sb
,
939 struct blame_entry
*ent
,
940 struct origin
*parent
,
941 struct blame_entry
*split
,
951 * Prepare mmfile that contains only the lines in ent.
953 cp
= nth_line(sb
, ent
->lno
);
954 file_o
.ptr
= (char*) cp
;
955 cnt
= ent
->num_lines
;
957 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
961 file_o
.size
= cp
- file_o
.ptr
;
963 patch
= compare_buffer(file_p
, &file_o
, 1);
966 * file_o is a part of final image we are annotating.
967 * file_p partially may match that image.
969 memset(split
, 0, sizeof(struct blame_entry
[3]));
971 for (i
= 0; i
< patch
->num
; i
++) {
972 struct chunk
*chunk
= &patch
->chunks
[i
];
974 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
975 plno
= chunk
->p_next
;
976 tlno
= chunk
->t_next
;
978 /* remainder, if any, all match the preimage */
979 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
984 * See if lines currently target is suspected for can be attributed to
987 static int find_move_in_parent(struct scoreboard
*sb
,
988 struct origin
*target
,
989 struct origin
*parent
)
991 int last_in_target
, made_progress
;
992 struct blame_entry
*e
, split
[3];
995 last_in_target
= find_last_in_target(sb
, target
);
996 if (last_in_target
< 0)
997 return 1; /* nothing remains for this target */
999 fill_origin_blob(parent
, &file_p
);
1004 while (made_progress
) {
1006 for (e
= sb
->ent
; e
; e
= e
->next
) {
1007 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1009 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1010 if (split
[1].suspect
&&
1011 blame_move_score
< ent_score(sb
, &split
[1])) {
1012 split_blame(sb
, split
, e
);
1015 decref_split(split
);
1022 struct blame_entry
*ent
;
1023 struct blame_entry split
[3];
1027 * Count the number of entries the target is suspected for,
1028 * and prepare a list of entry and the best split.
1030 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1031 struct origin
*target
,
1034 struct blame_entry
*e
;
1036 struct blame_list
*blame_list
= NULL
;
1038 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1039 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1042 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1043 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1044 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1045 blame_list
[i
++].ent
= e
;
1047 *num_ents_p
= num_ents
;
1052 * For lines target is suspected for, see if we can find code movement
1053 * across file boundary from the parent commit. porigin is the path
1054 * in the parent we already tried.
1056 static int find_copy_in_parent(struct scoreboard
*sb
,
1057 struct origin
*target
,
1058 struct commit
*parent
,
1059 struct origin
*porigin
,
1062 struct diff_options diff_opts
;
1063 const char *paths
[1];
1066 struct blame_list
*blame_list
;
1069 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1071 return 1; /* nothing remains for this target */
1073 diff_setup(&diff_opts
);
1074 diff_opts
.recursive
= 1;
1075 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1078 diff_tree_setup_paths(paths
, &diff_opts
);
1079 if (diff_setup_done(&diff_opts
) < 0)
1082 /* Try "find copies harder" on new path if requested;
1083 * we do not want to use diffcore_rename() actually to
1084 * match things up; find_copies_harder is set only to
1085 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1086 * and this code needs to be after diff_setup_done(), which
1087 * usually makes find-copies-harder imply copy detection.
1089 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1090 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1091 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1092 diff_opts
.find_copies_harder
= 1;
1094 if (is_null_sha1(target
->commit
->object
.sha1
))
1095 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1097 diff_tree_sha1(parent
->tree
->object
.sha1
,
1098 target
->commit
->tree
->object
.sha1
,
1101 if (!diff_opts
.find_copies_harder
)
1102 diffcore_std(&diff_opts
);
1106 int made_progress
= 0;
1108 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1109 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1110 struct origin
*norigin
;
1112 struct blame_entry
this[3];
1114 if (!DIFF_FILE_VALID(p
->one
))
1115 continue; /* does not exist in parent */
1116 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1117 /* find_move already dealt with this path */
1120 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1121 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1122 fill_origin_blob(norigin
, &file_p
);
1126 for (j
= 0; j
< num_ents
; j
++) {
1127 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1128 norigin
, this, &file_p
);
1129 copy_split_if_better(sb
, blame_list
[j
].split
,
1133 origin_decref(norigin
);
1136 for (j
= 0; j
< num_ents
; j
++) {
1137 struct blame_entry
*split
= blame_list
[j
].split
;
1138 if (split
[1].suspect
&&
1139 blame_copy_score
< ent_score(sb
, &split
[1])) {
1140 split_blame(sb
, split
, blame_list
[j
].ent
);
1143 decref_split(split
);
1149 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1155 diff_flush(&diff_opts
);
1161 * The blobs of origin and porigin exactly match, so everything
1162 * origin is suspected for can be blamed on the parent.
1164 static void pass_whole_blame(struct scoreboard
*sb
,
1165 struct origin
*origin
, struct origin
*porigin
)
1167 struct blame_entry
*e
;
1169 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1170 /* Steal its file */
1171 porigin
->file
= origin
->file
;
1172 origin
->file
.ptr
= NULL
;
1174 for (e
= sb
->ent
; e
; e
= e
->next
) {
1175 if (!same_suspect(e
->suspect
, origin
))
1177 origin_incref(porigin
);
1178 origin_decref(e
->suspect
);
1179 e
->suspect
= porigin
;
1183 #define MAXPARENT 16
1185 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1188 struct commit
*commit
= origin
->commit
;
1189 struct commit_list
*parent
;
1190 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1192 memset(parent_origin
, 0, sizeof(parent_origin
));
1194 /* The first pass looks for unrenamed path to optimize for
1195 * common cases, then we look for renames in the second pass.
1197 for (pass
= 0; pass
< 2; pass
++) {
1198 struct origin
*(*find
)(struct scoreboard
*,
1199 struct commit
*, struct origin
*);
1200 find
= pass
? find_rename
: find_origin
;
1202 for (i
= 0, parent
= commit
->parents
;
1203 i
< MAXPARENT
&& parent
;
1204 parent
= parent
->next
, i
++) {
1205 struct commit
*p
= parent
->item
;
1208 if (parent_origin
[i
])
1210 if (parse_commit(p
))
1212 porigin
= find(sb
, p
, origin
);
1215 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1216 pass_whole_blame(sb
, origin
, porigin
);
1217 origin_decref(porigin
);
1220 for (j
= same
= 0; j
< i
; j
++)
1221 if (parent_origin
[j
] &&
1222 !hashcmp(parent_origin
[j
]->blob_sha1
,
1223 porigin
->blob_sha1
)) {
1228 parent_origin
[i
] = porigin
;
1230 origin_decref(porigin
);
1235 for (i
= 0, parent
= commit
->parents
;
1236 i
< MAXPARENT
&& parent
;
1237 parent
= parent
->next
, i
++) {
1238 struct origin
*porigin
= parent_origin
[i
];
1241 if (pass_blame_to_parent(sb
, origin
, porigin
))
1246 * Optionally find moves in parents' files.
1248 if (opt
& PICKAXE_BLAME_MOVE
)
1249 for (i
= 0, parent
= commit
->parents
;
1250 i
< MAXPARENT
&& parent
;
1251 parent
= parent
->next
, i
++) {
1252 struct origin
*porigin
= parent_origin
[i
];
1255 if (find_move_in_parent(sb
, origin
, porigin
))
1260 * Optionally find copies from parents' files.
1262 if (opt
& PICKAXE_BLAME_COPY
)
1263 for (i
= 0, parent
= commit
->parents
;
1264 i
< MAXPARENT
&& parent
;
1265 parent
= parent
->next
, i
++) {
1266 struct origin
*porigin
= parent_origin
[i
];
1267 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1273 for (i
= 0; i
< MAXPARENT
; i
++)
1274 origin_decref(parent_origin
[i
]);
1278 * Information on commits, used for output.
1283 const char *author_mail
;
1284 unsigned long author_time
;
1285 const char *author_tz
;
1287 /* filled only when asked for details */
1288 const char *committer
;
1289 const char *committer_mail
;
1290 unsigned long committer_time
;
1291 const char *committer_tz
;
1293 const char *summary
;
1297 * Parse author/committer line in the commit object buffer
1299 static void get_ac_line(const char *inbuf
, const char *what
,
1300 int bufsz
, char *person
, const char **mail
,
1301 unsigned long *time
, const char **tz
)
1303 int len
, tzlen
, maillen
;
1304 char *tmp
, *endp
, *timepos
;
1306 tmp
= strstr(inbuf
, what
);
1309 tmp
+= strlen(what
);
1310 endp
= strchr(tmp
, '\n');
1318 *mail
= *tz
= "(unknown)";
1322 memcpy(person
, tmp
, len
);
1330 tzlen
= (person
+len
)-(tmp
+1);
1335 *time
= strtoul(tmp
, NULL
, 10);
1343 maillen
= timepos
- tmp
;
1349 * mailmap expansion may make the name longer.
1350 * make room by pushing stuff down.
1352 tmp
= person
+ bufsz
- (tzlen
+ 1);
1353 memmove(tmp
, *tz
, tzlen
);
1357 tmp
= tmp
- (maillen
+ 1);
1358 memmove(tmp
, *mail
, maillen
);
1363 * Now, convert e-mail using mailmap
1365 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1368 static void get_commit_info(struct commit
*commit
,
1369 struct commit_info
*ret
,
1374 static char author_buf
[1024];
1375 static char committer_buf
[1024];
1376 static char summary_buf
[1024];
1379 * We've operated without save_commit_buffer, so
1380 * we now need to populate them for output.
1382 if (!commit
->buffer
) {
1383 enum object_type type
;
1386 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1388 ret
->author
= author_buf
;
1389 get_ac_line(commit
->buffer
, "\nauthor ",
1390 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1391 &ret
->author_time
, &ret
->author_tz
);
1396 ret
->committer
= committer_buf
;
1397 get_ac_line(commit
->buffer
, "\ncommitter ",
1398 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1399 &ret
->committer_time
, &ret
->committer_tz
);
1401 ret
->summary
= summary_buf
;
1402 tmp
= strstr(commit
->buffer
, "\n\n");
1405 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1409 endp
= strchr(tmp
, '\n');
1411 endp
= tmp
+ strlen(tmp
);
1413 if (len
>= sizeof(summary_buf
) || len
== 0)
1415 memcpy(summary_buf
, tmp
, len
);
1416 summary_buf
[len
] = 0;
1420 * To allow LF and other nonportable characters in pathnames,
1421 * they are c-style quoted as needed.
1423 static void write_filename_info(const char *path
)
1425 printf("filename ");
1426 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1431 * The blame_entry is found to be guilty for the range. Mark it
1432 * as such, and show it in incremental output.
1434 static void found_guilty_entry(struct blame_entry
*ent
)
1440 struct origin
*suspect
= ent
->suspect
;
1442 printf("%s %d %d %d\n",
1443 sha1_to_hex(suspect
->commit
->object
.sha1
),
1444 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1445 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1446 struct commit_info ci
;
1447 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1448 get_commit_info(suspect
->commit
, &ci
, 1);
1449 printf("author %s\n", ci
.author
);
1450 printf("author-mail %s\n", ci
.author_mail
);
1451 printf("author-time %lu\n", ci
.author_time
);
1452 printf("author-tz %s\n", ci
.author_tz
);
1453 printf("committer %s\n", ci
.committer
);
1454 printf("committer-mail %s\n", ci
.committer_mail
);
1455 printf("committer-time %lu\n", ci
.committer_time
);
1456 printf("committer-tz %s\n", ci
.committer_tz
);
1457 printf("summary %s\n", ci
.summary
);
1458 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1459 printf("boundary\n");
1461 write_filename_info(suspect
->path
);
1466 * The main loop -- while the scoreboard has lines whose true origin
1467 * is still unknown, pick one blame_entry, and allow its current
1468 * suspect to pass blames to its parents.
1470 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1473 struct blame_entry
*ent
;
1474 struct commit
*commit
;
1475 struct origin
*suspect
= NULL
;
1477 /* find one suspect to break down */
1478 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1480 suspect
= ent
->suspect
;
1482 return; /* all done */
1485 * We will use this suspect later in the loop,
1486 * so hold onto it in the meantime.
1488 origin_incref(suspect
);
1489 commit
= suspect
->commit
;
1490 if (!commit
->object
.parsed
)
1491 parse_commit(commit
);
1492 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1493 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1494 pass_blame(sb
, suspect
, opt
);
1496 commit
->object
.flags
|= UNINTERESTING
;
1497 if (commit
->object
.parsed
)
1498 mark_parents_uninteresting(commit
);
1500 /* treat root commit as boundary */
1501 if (!commit
->parents
&& !show_root
)
1502 commit
->object
.flags
|= UNINTERESTING
;
1504 /* Take responsibility for the remaining entries */
1505 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1506 if (same_suspect(ent
->suspect
, suspect
))
1507 found_guilty_entry(ent
);
1508 origin_decref(suspect
);
1510 if (DEBUG
) /* sanity */
1511 sanity_check_refcnt(sb
);
1515 static const char *format_time(unsigned long time
, const char *tz_str
,
1518 static char time_buf
[128];
1523 if (show_raw_time
) {
1524 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1529 minutes
= tz
< 0 ? -tz
: tz
;
1530 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1531 minutes
= tz
< 0 ? -minutes
: minutes
;
1532 t
= time
+ minutes
* 60;
1535 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1536 strcat(time_buf
, tz_str
);
1540 #define OUTPUT_ANNOTATE_COMPAT 001
1541 #define OUTPUT_LONG_OBJECT_NAME 002
1542 #define OUTPUT_RAW_TIMESTAMP 004
1543 #define OUTPUT_PORCELAIN 010
1544 #define OUTPUT_SHOW_NAME 020
1545 #define OUTPUT_SHOW_NUMBER 040
1546 #define OUTPUT_SHOW_SCORE 0100
1547 #define OUTPUT_NO_AUTHOR 0200
1549 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1553 struct origin
*suspect
= ent
->suspect
;
1556 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1557 printf("%s%c%d %d %d\n",
1559 ent
->guilty
? ' ' : '*', // purely for debugging
1563 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1564 struct commit_info ci
;
1565 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1566 get_commit_info(suspect
->commit
, &ci
, 1);
1567 printf("author %s\n", ci
.author
);
1568 printf("author-mail %s\n", ci
.author_mail
);
1569 printf("author-time %lu\n", ci
.author_time
);
1570 printf("author-tz %s\n", ci
.author_tz
);
1571 printf("committer %s\n", ci
.committer
);
1572 printf("committer-mail %s\n", ci
.committer_mail
);
1573 printf("committer-time %lu\n", ci
.committer_time
);
1574 printf("committer-tz %s\n", ci
.committer_tz
);
1575 write_filename_info(suspect
->path
);
1576 printf("summary %s\n", ci
.summary
);
1577 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1578 printf("boundary\n");
1580 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1581 write_filename_info(suspect
->path
);
1583 cp
= nth_line(sb
, ent
->lno
);
1584 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1587 printf("%s %d %d\n", hex
,
1588 ent
->s_lno
+ 1 + cnt
,
1589 ent
->lno
+ 1 + cnt
);
1594 } while (ch
!= '\n' &&
1595 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1599 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1603 struct origin
*suspect
= ent
->suspect
;
1604 struct commit_info ci
;
1606 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1608 get_commit_info(suspect
->commit
, &ci
, 1);
1609 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1611 cp
= nth_line(sb
, ent
->lno
);
1612 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1614 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1616 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1618 memset(hex
, ' ', length
);
1619 else if (!cmd_is_annotate
) {
1625 printf("%.*s", length
, hex
);
1626 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1627 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1628 format_time(ci
.author_time
, ci
.author_tz
,
1630 ent
->lno
+ 1 + cnt
);
1632 if (opt
& OUTPUT_SHOW_SCORE
)
1634 max_score_digits
, ent
->score
,
1635 ent
->suspect
->refcnt
);
1636 if (opt
& OUTPUT_SHOW_NAME
)
1637 printf(" %-*.*s", longest_file
, longest_file
,
1639 if (opt
& OUTPUT_SHOW_NUMBER
)
1640 printf(" %*d", max_orig_digits
,
1641 ent
->s_lno
+ 1 + cnt
);
1643 if (!(opt
& OUTPUT_NO_AUTHOR
))
1644 printf(" (%-*.*s %10s",
1645 longest_author
, longest_author
,
1647 format_time(ci
.author_time
,
1651 max_digits
, ent
->lno
+ 1 + cnt
);
1656 } while (ch
!= '\n' &&
1657 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1661 static void output(struct scoreboard
*sb
, int option
)
1663 struct blame_entry
*ent
;
1665 if (option
& OUTPUT_PORCELAIN
) {
1666 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1667 struct blame_entry
*oth
;
1668 struct origin
*suspect
= ent
->suspect
;
1669 struct commit
*commit
= suspect
->commit
;
1670 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1672 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1673 if ((oth
->suspect
->commit
!= commit
) ||
1674 !strcmp(oth
->suspect
->path
, suspect
->path
))
1676 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1682 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1683 if (option
& OUTPUT_PORCELAIN
)
1684 emit_porcelain(sb
, ent
);
1686 emit_other(sb
, ent
, option
);
1692 * To allow quick access to the contents of nth line in the
1693 * final image, prepare an index in the scoreboard.
1695 static int prepare_lines(struct scoreboard
*sb
)
1697 const char *buf
= sb
->final_buf
;
1698 unsigned long len
= sb
->final_buf_size
;
1699 int num
= 0, incomplete
= 0, bol
= 1;
1701 if (len
&& buf
[len
-1] != '\n')
1702 incomplete
++; /* incomplete line at the end */
1705 sb
->lineno
= xrealloc(sb
->lineno
,
1706 sizeof(int* ) * (num
+ 1));
1707 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1710 if (*buf
++ == '\n') {
1715 sb
->lineno
= xrealloc(sb
->lineno
,
1716 sizeof(int* ) * (num
+ incomplete
+ 1));
1717 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1718 sb
->num_lines
= num
+ incomplete
;
1719 return sb
->num_lines
;
1723 * Add phony grafts for use with -S; this is primarily to
1724 * support git-cvsserver that wants to give a linear history
1727 static int read_ancestry(const char *graft_file
)
1729 FILE *fp
= fopen(graft_file
, "r");
1733 while (fgets(buf
, sizeof(buf
), fp
)) {
1734 /* The format is just "Commit Parent1 Parent2 ...\n" */
1735 int len
= strlen(buf
);
1736 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1738 register_commit_graft(graft
, 0);
1745 * How many columns do we need to show line numbers in decimal?
1747 static int lineno_width(int lines
)
1751 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1757 * How many columns do we need to show line numbers, authors,
1760 static void find_alignment(struct scoreboard
*sb
, int *option
)
1762 int longest_src_lines
= 0;
1763 int longest_dst_lines
= 0;
1764 unsigned largest_score
= 0;
1765 struct blame_entry
*e
;
1767 for (e
= sb
->ent
; e
; e
= e
->next
) {
1768 struct origin
*suspect
= e
->suspect
;
1769 struct commit_info ci
;
1772 if (strcmp(suspect
->path
, sb
->path
))
1773 *option
|= OUTPUT_SHOW_NAME
;
1774 num
= strlen(suspect
->path
);
1775 if (longest_file
< num
)
1777 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1778 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1779 get_commit_info(suspect
->commit
, &ci
, 1);
1780 num
= strlen(ci
.author
);
1781 if (longest_author
< num
)
1782 longest_author
= num
;
1784 num
= e
->s_lno
+ e
->num_lines
;
1785 if (longest_src_lines
< num
)
1786 longest_src_lines
= num
;
1787 num
= e
->lno
+ e
->num_lines
;
1788 if (longest_dst_lines
< num
)
1789 longest_dst_lines
= num
;
1790 if (largest_score
< ent_score(sb
, e
))
1791 largest_score
= ent_score(sb
, e
);
1793 max_orig_digits
= lineno_width(longest_src_lines
);
1794 max_digits
= lineno_width(longest_dst_lines
);
1795 max_score_digits
= lineno_width(largest_score
);
1799 * For debugging -- origin is refcounted, and this asserts that
1800 * we do not underflow.
1802 static void sanity_check_refcnt(struct scoreboard
*sb
)
1805 struct blame_entry
*ent
;
1807 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1808 /* Nobody should have zero or negative refcnt */
1809 if (ent
->suspect
->refcnt
<= 0) {
1810 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1812 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1813 ent
->suspect
->refcnt
);
1817 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1818 /* Mark the ones that haven't been checked */
1819 if (0 < ent
->suspect
->refcnt
)
1820 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1822 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1824 * ... then pick each and see if they have the the
1828 struct blame_entry
*e
;
1829 struct origin
*suspect
= ent
->suspect
;
1831 if (0 < suspect
->refcnt
)
1833 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1834 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1835 if (e
->suspect
!= suspect
)
1839 if (suspect
->refcnt
!= found
) {
1840 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1842 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1843 ent
->suspect
->refcnt
, found
);
1849 find_alignment(sb
, &opt
);
1851 die("Baa %d!", baa
);
1856 * Used for the command line parsing; check if the path exists
1857 * in the working tree.
1859 static int has_path_in_work_tree(const char *path
)
1862 return !lstat(path
, &st
);
1865 static unsigned parse_score(const char *arg
)
1868 unsigned long score
= strtoul(arg
, &end
, 10);
1874 static const char *add_prefix(const char *prefix
, const char *path
)
1876 if (!prefix
|| !prefix
[0])
1878 return prefix_path(prefix
, strlen(prefix
), path
);
1882 * Parsing of (comma separated) one item in the -L option
1884 static const char *parse_loc(const char *spec
,
1885 struct scoreboard
*sb
, long lno
,
1886 long begin
, long *ret
)
1893 regmatch_t match
[1];
1895 /* Allow "-L <something>,+20" to mean starting at <something>
1896 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1899 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1900 num
= strtol(spec
+ 1, &term
, 10);
1901 if (term
!= spec
+ 1) {
1905 *ret
= begin
+ num
- 2;
1914 num
= strtol(spec
, &term
, 10);
1922 /* it could be a regexp of form /.../ */
1923 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1930 /* try [spec+1 .. term-1] as regexp */
1932 begin
--; /* input is in human terms */
1933 line
= nth_line(sb
, begin
);
1935 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1936 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1937 const char *cp
= line
+ match
[0].rm_so
;
1940 while (begin
++ < lno
) {
1941 nline
= nth_line(sb
, begin
);
1942 if (line
<= cp
&& cp
< nline
)
1953 regerror(reg_error
, ®exp
, errbuf
, 1024);
1954 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1959 * Parsing of -L option
1961 static void prepare_blame_range(struct scoreboard
*sb
,
1962 const char *bottomtop
,
1964 long *bottom
, long *top
)
1968 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1970 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1978 static int git_blame_config(const char *var
, const char *value
)
1980 if (!strcmp(var
, "blame.showroot")) {
1981 show_root
= git_config_bool(var
, value
);
1984 if (!strcmp(var
, "blame.blankboundary")) {
1985 blank_boundary
= git_config_bool(var
, value
);
1988 return git_default_config(var
, value
);
1991 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1993 struct commit
*commit
;
1994 struct origin
*origin
;
1995 unsigned char head_sha1
[20];
2000 unsigned long fin_size
;
2002 struct cache_entry
*ce
;
2005 if (get_sha1("HEAD", head_sha1
))
2006 die("No such ref: HEAD");
2009 commit
= xcalloc(1, sizeof(*commit
));
2010 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2011 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2012 commit
->object
.parsed
= 1;
2014 commit
->object
.type
= OBJ_COMMIT
;
2016 origin
= make_origin(commit
, path
);
2018 if (!contents_from
|| strcmp("-", contents_from
)) {
2020 const char *read_from
;
2022 if (contents_from
) {
2023 if (stat(contents_from
, &st
) < 0)
2024 die("Cannot stat %s", contents_from
);
2025 read_from
= contents_from
;
2028 if (lstat(path
, &st
) < 0)
2029 die("Cannot lstat %s", path
);
2032 fin_size
= xsize_t(st
.st_size
);
2033 buf
= xmalloc(fin_size
+1);
2034 mode
= canon_mode(st
.st_mode
);
2035 switch (st
.st_mode
& S_IFMT
) {
2037 fd
= open(read_from
, O_RDONLY
);
2039 die("cannot open %s", read_from
);
2040 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
2041 die("cannot read %s", read_from
);
2044 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
2045 die("cannot readlink %s", read_from
);
2048 die("unsupported file type %s", read_from
);
2052 /* Reading from stdin */
2053 contents_from
= "standard input";
2059 buf
= xrealloc(buf
, fin_size
+ cnt
);
2060 cnt
= xread(0, buf
+ fin_size
, cnt
);
2062 die("read error %s from stdin",
2068 buf
= xrealloc(buf
, fin_size
+ 1);
2071 origin
->file
.ptr
= buf
;
2072 origin
->file
.size
= fin_size
;
2073 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2074 commit
->util
= origin
;
2077 * Read the current index, replace the path entry with
2078 * origin->blob_sha1 without mucking with its mode or type
2079 * bits; we are not going to write this index out -- we just
2080 * want to run "diff-index --cached".
2087 int pos
= cache_name_pos(path
, len
);
2089 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2091 /* Let's not bother reading from HEAD tree */
2092 mode
= S_IFREG
| 0644;
2094 size
= cache_entry_size(len
);
2095 ce
= xcalloc(1, size
);
2096 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2097 memcpy(ce
->name
, path
, len
);
2098 ce
->ce_flags
= create_ce_flags(len
, 0);
2099 ce
->ce_mode
= create_ce_mode(mode
);
2100 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2103 * We are not going to write this out, so this does not matter
2104 * right now, but someday we might optimize diff-index --cached
2105 * with cache-tree information.
2107 cache_tree_invalidate_path(active_cache_tree
, path
);
2109 commit
->buffer
= xmalloc(400);
2110 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2111 snprintf(commit
->buffer
, 400,
2112 "tree 0000000000000000000000000000000000000000\n"
2116 "Version of %s from %s\n",
2117 sha1_to_hex(head_sha1
),
2118 ident
, ident
, path
, contents_from
? contents_from
: path
);
2122 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2124 struct rev_info revs
;
2126 struct scoreboard sb
;
2128 struct blame_entry
*ent
;
2129 int i
, seen_dashdash
, unk
, opt
;
2130 long bottom
, top
, lno
;
2131 int output_option
= 0;
2133 const char *revs_file
= NULL
;
2134 const char *final_commit_name
= NULL
;
2135 enum object_type type
;
2136 const char *bottomtop
= NULL
;
2137 const char *contents_from
= NULL
;
2139 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2141 git_config(git_blame_config
);
2142 save_commit_buffer
= 0;
2146 for (unk
= i
= 1; i
< argc
; i
++) {
2147 const char *arg
= argv
[i
];
2150 else if (!strcmp("-b", arg
))
2152 else if (!strcmp("--root", arg
))
2154 else if (!strcmp(arg
, "--show-stats"))
2156 else if (!strcmp("-c", arg
))
2157 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2158 else if (!strcmp("-t", arg
))
2159 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2160 else if (!strcmp("-l", arg
))
2161 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2162 else if (!strcmp("-s", arg
))
2163 output_option
|= OUTPUT_NO_AUTHOR
;
2164 else if (!strcmp("-w", arg
))
2165 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2166 else if (!strcmp("-S", arg
) && ++i
< argc
)
2167 revs_file
= argv
[i
];
2168 else if (!prefixcmp(arg
, "-M")) {
2169 opt
|= PICKAXE_BLAME_MOVE
;
2170 blame_move_score
= parse_score(arg
+2);
2172 else if (!prefixcmp(arg
, "-C")) {
2174 * -C enables copy from removed files;
2175 * -C -C enables copy from existing files, but only
2176 * when blaming a new file;
2177 * -C -C -C enables copy from existing files for
2180 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2181 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2182 if (opt
& PICKAXE_BLAME_COPY
)
2183 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2184 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2185 blame_copy_score
= parse_score(arg
+2);
2187 else if (!prefixcmp(arg
, "-L")) {
2196 die("More than one '-L n,m' option given");
2199 else if (!strcmp("--contents", arg
)) {
2202 contents_from
= argv
[i
];
2204 else if (!strcmp("--incremental", arg
))
2206 else if (!strcmp("--score-debug", arg
))
2207 output_option
|= OUTPUT_SHOW_SCORE
;
2208 else if (!strcmp("-f", arg
) ||
2209 !strcmp("--show-name", arg
))
2210 output_option
|= OUTPUT_SHOW_NAME
;
2211 else if (!strcmp("-n", arg
) ||
2212 !strcmp("--show-number", arg
))
2213 output_option
|= OUTPUT_SHOW_NUMBER
;
2214 else if (!strcmp("-p", arg
) ||
2215 !strcmp("--porcelain", arg
))
2216 output_option
|= OUTPUT_PORCELAIN
;
2217 else if (!strcmp("--", arg
)) {
2229 if (!blame_move_score
)
2230 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2231 if (!blame_copy_score
)
2232 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2235 * We have collected options unknown to us in argv[1..unk]
2236 * which are to be passed to revision machinery if we are
2237 * going to do the "bottom" processing.
2239 * The remaining are:
2241 * (1) if seen_dashdash, its either
2242 * "-options -- <path>" or
2243 * "-options -- <path> <rev>".
2244 * but the latter is allowed only if there is no
2245 * options that we passed to revision machinery.
2247 * (2) otherwise, we may have "--" somewhere later and
2248 * might be looking at the first one of multiple 'rev'
2249 * parameters (e.g. " master ^next ^maint -- path").
2250 * See if there is a dashdash first, and give the
2251 * arguments before that to revision machinery.
2252 * After that there must be one 'path'.
2254 * (3) otherwise, its one of the three:
2255 * "-options <path> <rev>"
2256 * "-options <rev> <path>"
2258 * but again the first one is allowed only if
2259 * there is no options that we passed to revision
2263 if (seen_dashdash
) {
2267 path
= add_prefix(prefix
, argv
[i
]);
2268 if (i
+ 1 == argc
- 1) {
2271 argv
[unk
++] = argv
[i
+ 1];
2273 else if (i
+ 1 != argc
)
2274 /* garbage at end */
2279 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2280 if (!strcmp(argv
[j
], "--"))
2282 if (seen_dashdash
) {
2284 if (seen_dashdash
+ 1 != argc
- 1)
2286 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2287 for (j
= i
; j
< seen_dashdash
; j
++)
2288 argv
[unk
++] = argv
[j
];
2294 path
= add_prefix(prefix
, argv
[i
]);
2295 if (i
+ 1 == argc
- 1) {
2296 final_commit_name
= argv
[i
+ 1];
2298 /* if (unk == 1) we could be getting
2301 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2302 path
= add_prefix(prefix
, argv
[i
+ 1]);
2303 final_commit_name
= argv
[i
];
2306 else if (i
!= argc
- 1)
2307 usage(blame_usage
); /* garbage at end */
2309 if (!has_path_in_work_tree(path
))
2310 die("cannot stat path %s: %s",
2311 path
, strerror(errno
));
2315 if (final_commit_name
)
2316 argv
[unk
++] = final_commit_name
;
2319 * Now we got rev and path. We do not want the path pruning
2320 * but we may want "bottom" processing.
2322 argv
[unk
++] = "--"; /* terminate the rev name */
2325 init_revisions(&revs
, NULL
);
2326 setup_revisions(unk
, argv
, &revs
, NULL
);
2327 memset(&sb
, 0, sizeof(sb
));
2330 * There must be one and only one positive commit in the
2331 * revs->pending array.
2333 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2334 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2335 if (obj
->flags
& UNINTERESTING
)
2337 while (obj
->type
== OBJ_TAG
)
2338 obj
= deref_tag(obj
, NULL
, 0);
2339 if (obj
->type
!= OBJ_COMMIT
)
2340 die("Non commit %s?",
2341 revs
.pending
.objects
[i
].name
);
2343 die("More than one commit to dig from %s and %s?",
2344 revs
.pending
.objects
[i
].name
,
2346 sb
.final
= (struct commit
*) obj
;
2347 final_commit_name
= revs
.pending
.objects
[i
].name
;
2352 * "--not A B -- path" without anything positive;
2353 * do not default to HEAD, but use the working tree
2356 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2357 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2359 else if (contents_from
)
2360 die("Cannot use --contents with final commit object name");
2363 * If we have bottom, this will mark the ancestors of the
2364 * bottom commits we would reach while traversing as
2367 prepare_revision_walk(&revs
);
2369 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2372 buf
= xmalloc(o
->file
.size
+ 1);
2373 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2375 sb
.final_buf_size
= o
->file
.size
;
2378 o
= get_origin(&sb
, sb
.final
, path
);
2379 if (fill_blob_sha1(o
))
2380 die("no such path %s in %s", path
, final_commit_name
);
2382 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2383 &sb
.final_buf_size
);
2386 lno
= prepare_lines(&sb
);
2390 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2391 if (bottom
&& top
&& top
< bottom
) {
2393 tmp
= top
; top
= bottom
; bottom
= tmp
;
2401 die("file %s has only %lu lines", path
, lno
);
2403 ent
= xcalloc(1, sizeof(*ent
));
2405 ent
->num_lines
= top
- bottom
;
2407 ent
->s_lno
= bottom
;
2412 if (revs_file
&& read_ancestry(revs_file
))
2413 die("reading graft file %s failed: %s",
2414 revs_file
, strerror(errno
));
2416 read_mailmap(&mailmap
, ".mailmap", NULL
);
2418 assign_blame(&sb
, &revs
, opt
);
2425 if (!(output_option
& OUTPUT_PORCELAIN
))
2426 find_alignment(&sb
, &output_option
);
2428 output(&sb
, output_option
);
2429 free((void *)sb
.final_buf
);
2430 for (ent
= sb
.ent
; ent
; ) {
2431 struct blame_entry
*e
= ent
->next
;
2437 printf("num read blob: %d\n", num_read_blob
);
2438 printf("num get patch: %d\n", num_get_patch
);
2439 printf("num commits: %d\n", num_commits
);