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] [-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 " -L n,m Process only line range n,m, counting from 1\n"
34 " -M, -C Find line movements within and across files\n"
35 " --incremental Show blame entries as we find them, incrementally\n"
36 " --contents file Use <file>'s contents as the final image\n"
37 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
39 static int longest_file
;
40 static int longest_author
;
41 static int max_orig_digits
;
42 static int max_digits
;
43 static int max_score_digits
;
45 static int blank_boundary
;
46 static int incremental
;
47 static int cmd_is_annotate
;
48 static struct path_list mailmap
;
55 static int num_read_blob
;
56 static int num_get_patch
;
57 static int num_commits
;
59 #define PICKAXE_BLAME_MOVE 01
60 #define PICKAXE_BLAME_COPY 02
61 #define PICKAXE_BLAME_COPY_HARDER 04
64 * blame for a blame_entry with score lower than these thresholds
65 * is not passed to the parent using move/copy logic.
67 static unsigned blame_move_score
;
68 static unsigned blame_copy_score
;
69 #define BLAME_DEFAULT_MOVE_SCORE 20
70 #define BLAME_DEFAULT_COPY_SCORE 40
72 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
73 #define METAINFO_SHOWN (1u<<12)
74 #define MORE_THAN_ONE_PATH (1u<<13)
77 * One blob in a commit that is being suspected
81 struct commit
*commit
;
83 unsigned char blob_sha1
[20];
84 char path
[FLEX_ARRAY
];
88 * Given an origin, prepare mmfile_t structure to be used by the
91 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
94 enum object_type type
;
96 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
97 (unsigned long *)(&(file
->size
)));
106 * Origin is refcounted and usually we keep the blob contents to be
109 static inline struct origin
*origin_incref(struct origin
*o
)
116 static void origin_decref(struct origin
*o
)
118 if (o
&& --o
->refcnt
<= 0) {
121 memset(o
, 0, sizeof(*o
));
127 * Each group of lines is described by a blame_entry; it can be split
128 * as we pass blame to the parents. They form a linked list in the
129 * scoreboard structure, sorted by the target line number.
132 struct blame_entry
*prev
;
133 struct blame_entry
*next
;
135 /* the first line of this group in the final image;
136 * internally all line numbers are 0 based.
140 /* how many lines this group has */
143 /* the commit that introduced this group into the final image */
144 struct origin
*suspect
;
146 /* true if the suspect is truly guilty; false while we have not
147 * checked if the group came from one of its parents.
151 /* the line number of the first line of this group in the
152 * suspect's file; internally all line numbers are 0 based.
156 /* how significant this entry is -- cached to avoid
157 * scanning the lines over and over.
163 * The current state of the blame assignment.
166 /* the final commit (i.e. where we started digging from) */
167 struct commit
*final
;
172 * The contents in the final image.
173 * Used by many functions to obtain contents of the nth line,
174 * indexed with scoreboard.lineno[blame_entry.lno].
176 const char *final_buf
;
177 unsigned long final_buf_size
;
179 /* linked list of blames */
180 struct blame_entry
*ent
;
182 /* look-up a line in the final buffer */
187 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
191 if (a
->commit
!= b
->commit
)
193 return !strcmp(a
->path
, b
->path
);
196 static void sanity_check_refcnt(struct scoreboard
*);
199 * If two blame entries that are next to each other came from
200 * contiguous lines in the same origin (i.e. <commit, path> pair),
201 * merge them together.
203 static void coalesce(struct scoreboard
*sb
)
205 struct blame_entry
*ent
, *next
;
207 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
208 if (same_suspect(ent
->suspect
, next
->suspect
) &&
209 ent
->guilty
== next
->guilty
&&
210 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
211 ent
->num_lines
+= next
->num_lines
;
212 ent
->next
= next
->next
;
214 ent
->next
->prev
= ent
;
215 origin_decref(next
->suspect
);
218 next
= ent
; /* again */
222 if (DEBUG
) /* sanity */
223 sanity_check_refcnt(sb
);
227 * Given a commit and a path in it, create a new origin structure.
228 * The callers that add blame to the scoreboard should use
229 * get_origin() to obtain shared, refcounted copy instead of calling
230 * this function directly.
232 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
235 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
238 strcpy(o
->path
, path
);
243 * Locate an existing origin or create a new one.
245 static struct origin
*get_origin(struct scoreboard
*sb
,
246 struct commit
*commit
,
249 struct blame_entry
*e
;
251 for (e
= sb
->ent
; e
; e
= e
->next
) {
252 if (e
->suspect
->commit
== commit
&&
253 !strcmp(e
->suspect
->path
, path
))
254 return origin_incref(e
->suspect
);
256 return make_origin(commit
, path
);
260 * Fill the blob_sha1 field of an origin if it hasn't, so that later
261 * call to fill_origin_blob() can use it to locate the data. blob_sha1
262 * for an origin is also used to pass the blame for the entire file to
263 * the parent to detect the case where a child's blob is identical to
264 * that of its parent's.
266 static int fill_blob_sha1(struct origin
*origin
)
270 if (!is_null_sha1(origin
->blob_sha1
))
272 if (get_tree_entry(origin
->commit
->object
.sha1
,
274 origin
->blob_sha1
, &mode
))
276 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
280 hashclr(origin
->blob_sha1
);
285 * We have an origin -- check if the same path exists in the
286 * parent and return an origin structure to represent it.
288 static struct origin
*find_origin(struct scoreboard
*sb
,
289 struct commit
*parent
,
290 struct origin
*origin
)
292 struct origin
*porigin
= NULL
;
293 struct diff_options diff_opts
;
294 const char *paths
[2];
298 * Each commit object can cache one origin in that
299 * commit. This is a freestanding copy of origin and
302 struct origin
*cached
= parent
->util
;
303 if (!strcmp(cached
->path
, origin
->path
)) {
305 * The same path between origin and its parent
306 * without renaming -- the most common case.
308 porigin
= get_origin(sb
, parent
, cached
->path
);
311 * If the origin was newly created (i.e. get_origin
312 * would call make_origin if none is found in the
313 * scoreboard), it does not know the blob_sha1,
314 * so copy it. Otherwise porigin was in the
315 * scoreboard and already knows blob_sha1.
317 if (porigin
->refcnt
== 1)
318 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
321 /* otherwise it was not very useful; free it */
326 /* See if the origin->path is different between parent
327 * and origin first. Most of the time they are the
328 * same and diff-tree is fairly efficient about this.
330 diff_setup(&diff_opts
);
331 diff_opts
.recursive
= 1;
332 diff_opts
.detect_rename
= 0;
333 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
334 paths
[0] = origin
->path
;
337 diff_tree_setup_paths(paths
, &diff_opts
);
338 if (diff_setup_done(&diff_opts
) < 0)
341 if (is_null_sha1(origin
->commit
->object
.sha1
))
342 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
344 diff_tree_sha1(parent
->tree
->object
.sha1
,
345 origin
->commit
->tree
->object
.sha1
,
347 diffcore_std(&diff_opts
);
349 /* It is either one entry that says "modified", or "created",
352 if (!diff_queued_diff
.nr
) {
353 /* The path is the same as parent */
354 porigin
= get_origin(sb
, parent
, origin
->path
);
355 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
357 else if (diff_queued_diff
.nr
!= 1)
358 die("internal error in blame::find_origin");
360 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
363 die("internal error in blame::find_origin (%c)",
366 porigin
= get_origin(sb
, parent
, origin
->path
);
367 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
371 /* Did not exist in parent, or type changed */
375 diff_flush(&diff_opts
);
378 * Create a freestanding copy that is not part of
379 * the refcounted origin found in the scoreboard, and
380 * cache it in the commit.
382 struct origin
*cached
;
384 cached
= make_origin(porigin
->commit
, porigin
->path
);
385 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
386 parent
->util
= cached
;
392 * We have an origin -- find the path that corresponds to it in its
393 * parent and return an origin structure to represent it.
395 static struct origin
*find_rename(struct scoreboard
*sb
,
396 struct commit
*parent
,
397 struct origin
*origin
)
399 struct origin
*porigin
= NULL
;
400 struct diff_options diff_opts
;
402 const char *paths
[2];
404 diff_setup(&diff_opts
);
405 diff_opts
.recursive
= 1;
406 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
407 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
408 diff_opts
.single_follow
= origin
->path
;
410 diff_tree_setup_paths(paths
, &diff_opts
);
411 if (diff_setup_done(&diff_opts
) < 0)
414 if (is_null_sha1(origin
->commit
->object
.sha1
))
415 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
417 diff_tree_sha1(parent
->tree
->object
.sha1
,
418 origin
->commit
->tree
->object
.sha1
,
420 diffcore_std(&diff_opts
);
422 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
423 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
424 if ((p
->status
== 'R' || p
->status
== 'C') &&
425 !strcmp(p
->two
->path
, origin
->path
)) {
426 porigin
= get_origin(sb
, parent
, p
->one
->path
);
427 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
431 diff_flush(&diff_opts
);
436 * Parsing of patch chunks...
439 /* line number in postimage; up to but not including this
440 * line is the same as preimage
444 /* preimage line number after this chunk */
447 /* postimage line number after this chunk */
452 struct chunk
*chunks
;
456 struct blame_diff_state
{
457 struct xdiff_emit_state xm
;
459 unsigned hunk_post_context
;
460 unsigned hunk_in_pre_context
: 1;
463 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
465 struct blame_diff_state
*state
= state_
;
467 int off1
, off2
, len1
, len2
, num
;
469 num
= state
->ret
->num
;
470 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
471 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
472 state
->ret
->chunks
[num
- 1].same
++;
474 state
->hunk_in_pre_context
= 0;
476 state
->hunk_post_context
++;
478 state
->hunk_post_context
= 0;
483 if (num
&& state
->hunk_post_context
) {
484 chunk
= &state
->ret
->chunks
[num
- 1];
485 chunk
->p_next
-= state
->hunk_post_context
;
486 chunk
->t_next
-= state
->hunk_post_context
;
488 state
->ret
->num
= ++num
;
489 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
490 sizeof(struct chunk
) * num
);
491 chunk
= &state
->ret
->chunks
[num
- 1];
492 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
497 /* Line numbers in patch output are one based. */
501 chunk
->same
= len2
? off2
: (off2
+ 1);
503 chunk
->p_next
= off1
+ (len1
? len1
: 1);
504 chunk
->t_next
= chunk
->same
+ len2
;
505 state
->hunk_in_pre_context
= 1;
506 state
->hunk_post_context
= 0;
509 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
512 struct blame_diff_state state
;
517 xpp
.flags
= XDF_NEED_MINIMAL
;
518 xecfg
.ctxlen
= context
;
520 ecb
.outf
= xdiff_outf
;
522 memset(&state
, 0, sizeof(state
));
523 state
.xm
.consume
= process_u_diff
;
524 state
.ret
= xmalloc(sizeof(struct patch
));
525 state
.ret
->chunks
= NULL
;
528 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
530 if (state
.ret
->num
) {
532 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
533 chunk
->p_next
-= state
.hunk_post_context
;
534 chunk
->t_next
-= state
.hunk_post_context
;
540 * Run diff between two origins and grab the patch output, so that
541 * we can pass blame for lines origin is currently suspected for
544 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
546 mmfile_t file_p
, file_o
;
549 fill_origin_blob(parent
, &file_p
);
550 fill_origin_blob(origin
, &file_o
);
551 if (!file_p
.ptr
|| !file_o
.ptr
)
553 patch
= compare_buffer(&file_p
, &file_o
, 0);
558 static void free_patch(struct patch
*p
)
565 * Link in a new blame entry to the scoreboard. Entries that cover the
566 * same line range have been removed from the scoreboard previously.
568 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
570 struct blame_entry
*ent
, *prev
= NULL
;
572 origin_incref(e
->suspect
);
574 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
577 /* prev, if not NULL, is the last one that is below e */
580 e
->next
= prev
->next
;
592 * src typically is on-stack; we want to copy the information in it to
593 * an malloced blame_entry that is already on the linked list of the
594 * scoreboard. The origin of dst loses a refcnt while the origin of src
597 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
599 struct blame_entry
*p
, *n
;
603 origin_incref(src
->suspect
);
604 origin_decref(dst
->suspect
);
605 memcpy(dst
, src
, sizeof(*src
));
611 static const char *nth_line(struct scoreboard
*sb
, int lno
)
613 return sb
->final_buf
+ sb
->lineno
[lno
];
617 * It is known that lines between tlno to same came from parent, and e
618 * has an overlap with that range. it also is known that parent's
619 * line plno corresponds to e's line tlno.
625 * <------------------>
627 * Split e into potentially three parts; before this chunk, the chunk
628 * to be blamed for the parent, and after that portion.
630 static void split_overlap(struct blame_entry
*split
,
631 struct blame_entry
*e
,
632 int tlno
, int plno
, int same
,
633 struct origin
*parent
)
636 memset(split
, 0, sizeof(struct blame_entry
[3]));
638 if (e
->s_lno
< tlno
) {
639 /* there is a pre-chunk part not blamed on parent */
640 split
[0].suspect
= origin_incref(e
->suspect
);
641 split
[0].lno
= e
->lno
;
642 split
[0].s_lno
= e
->s_lno
;
643 split
[0].num_lines
= tlno
- e
->s_lno
;
644 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
645 split
[1].s_lno
= plno
;
648 split
[1].lno
= e
->lno
;
649 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
652 if (same
< e
->s_lno
+ e
->num_lines
) {
653 /* there is a post-chunk part not blamed on parent */
654 split
[2].suspect
= origin_incref(e
->suspect
);
655 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
656 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
657 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
658 chunk_end_lno
= split
[2].lno
;
661 chunk_end_lno
= e
->lno
+ e
->num_lines
;
662 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
665 * if it turns out there is nothing to blame the parent for,
666 * forget about the splitting. !split[1].suspect signals this.
668 if (split
[1].num_lines
< 1)
670 split
[1].suspect
= origin_incref(parent
);
674 * split_overlap() divided an existing blame e into up to three parts
675 * in split. Adjust the linked list of blames in the scoreboard to
678 static void split_blame(struct scoreboard
*sb
,
679 struct blame_entry
*split
,
680 struct blame_entry
*e
)
682 struct blame_entry
*new_entry
;
684 if (split
[0].suspect
&& split
[2].suspect
) {
685 /* The first part (reuse storage for the existing entry e) */
686 dup_entry(e
, &split
[0]);
688 /* The last part -- me */
689 new_entry
= xmalloc(sizeof(*new_entry
));
690 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
691 add_blame_entry(sb
, new_entry
);
693 /* ... and the middle part -- parent */
694 new_entry
= xmalloc(sizeof(*new_entry
));
695 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
696 add_blame_entry(sb
, new_entry
);
698 else if (!split
[0].suspect
&& !split
[2].suspect
)
700 * The parent covers the entire area; reuse storage for
701 * e and replace it with the parent.
703 dup_entry(e
, &split
[1]);
704 else if (split
[0].suspect
) {
705 /* me and then parent */
706 dup_entry(e
, &split
[0]);
708 new_entry
= xmalloc(sizeof(*new_entry
));
709 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
710 add_blame_entry(sb
, new_entry
);
713 /* parent and then me */
714 dup_entry(e
, &split
[1]);
716 new_entry
= xmalloc(sizeof(*new_entry
));
717 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
718 add_blame_entry(sb
, new_entry
);
721 if (DEBUG
) { /* sanity */
722 struct blame_entry
*ent
;
723 int lno
= sb
->ent
->lno
, corrupt
= 0;
725 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
730 lno
+= ent
->num_lines
;
734 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
735 printf("L %8d l %8d n %8d\n",
736 lno
, ent
->lno
, ent
->num_lines
);
737 lno
= ent
->lno
+ ent
->num_lines
;
745 * After splitting the blame, the origins used by the
746 * on-stack blame_entry should lose one refcnt each.
748 static void decref_split(struct blame_entry
*split
)
752 for (i
= 0; i
< 3; i
++)
753 origin_decref(split
[i
].suspect
);
757 * Helper for blame_chunk(). blame_entry e is known to overlap with
758 * the patch hunk; split it and pass blame to the parent.
760 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
761 int tlno
, int plno
, int same
,
762 struct origin
*parent
)
764 struct blame_entry split
[3];
766 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
767 if (split
[1].suspect
)
768 split_blame(sb
, split
, e
);
773 * Find the line number of the last line the target is suspected for.
775 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
777 struct blame_entry
*e
;
778 int last_in_target
= -1;
780 for (e
= sb
->ent
; e
; e
= e
->next
) {
781 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
783 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
784 last_in_target
= e
->s_lno
+ e
->num_lines
;
786 return last_in_target
;
790 * Process one hunk from the patch between the current suspect for
791 * blame_entry e and its parent. Find and split the overlap, and
792 * pass blame to the overlapping part to the parent.
794 static void blame_chunk(struct scoreboard
*sb
,
795 int tlno
, int plno
, int same
,
796 struct origin
*target
, struct origin
*parent
)
798 struct blame_entry
*e
;
800 for (e
= sb
->ent
; e
; e
= e
->next
) {
801 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
803 if (same
<= e
->s_lno
)
805 if (tlno
< e
->s_lno
+ e
->num_lines
)
806 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
811 * We are looking at the origin 'target' and aiming to pass blame
812 * for the lines it is suspected to its parent. Run diff to find
813 * which lines came from parent and pass blame for them.
815 static int pass_blame_to_parent(struct scoreboard
*sb
,
816 struct origin
*target
,
817 struct origin
*parent
)
819 int i
, last_in_target
, plno
, tlno
;
822 last_in_target
= find_last_in_target(sb
, target
);
823 if (last_in_target
< 0)
824 return 1; /* nothing remains for this target */
826 patch
= get_patch(parent
, target
);
828 for (i
= 0; i
< patch
->num
; i
++) {
829 struct chunk
*chunk
= &patch
->chunks
[i
];
831 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
832 plno
= chunk
->p_next
;
833 tlno
= chunk
->t_next
;
835 /* The rest (i.e. anything after tlno) are the same as the parent */
836 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
843 * The lines in blame_entry after splitting blames many times can become
844 * very small and trivial, and at some point it becomes pointless to
845 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
846 * ordinary C program, and it is not worth to say it was copied from
847 * totally unrelated file in the parent.
849 * Compute how trivial the lines in the blame_entry are.
851 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
860 cp
= nth_line(sb
, e
->lno
);
861 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
863 unsigned ch
= *((unsigned char *)cp
);
873 * best_so_far[] and this[] are both a split of an existing blame_entry
874 * that passes blame to the parent. Maintain best_so_far the best split
875 * so far, by comparing this and best_so_far and copying this into
876 * bst_so_far as needed.
878 static void copy_split_if_better(struct scoreboard
*sb
,
879 struct blame_entry
*best_so_far
,
880 struct blame_entry
*this)
884 if (!this[1].suspect
)
886 if (best_so_far
[1].suspect
) {
887 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
891 for (i
= 0; i
< 3; i
++)
892 origin_incref(this[i
].suspect
);
893 decref_split(best_so_far
);
894 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
898 * Find the lines from parent that are the same as ent so that
899 * we can pass blames to it. file_p has the blob contents for
902 static void find_copy_in_blob(struct scoreboard
*sb
,
903 struct blame_entry
*ent
,
904 struct origin
*parent
,
905 struct blame_entry
*split
,
915 * Prepare mmfile that contains only the lines in ent.
917 cp
= nth_line(sb
, ent
->lno
);
918 file_o
.ptr
= (char*) cp
;
919 cnt
= ent
->num_lines
;
921 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
925 file_o
.size
= cp
- file_o
.ptr
;
927 patch
= compare_buffer(file_p
, &file_o
, 1);
929 memset(split
, 0, sizeof(struct blame_entry
[3]));
931 for (i
= 0; i
< patch
->num
; i
++) {
932 struct chunk
*chunk
= &patch
->chunks
[i
];
934 /* tlno to chunk->same are the same as ent */
935 if (ent
->num_lines
<= tlno
)
937 if (tlno
< chunk
->same
) {
938 struct blame_entry
this[3];
939 split_overlap(this, ent
,
940 tlno
+ ent
->s_lno
, plno
,
941 chunk
->same
+ ent
->s_lno
,
943 copy_split_if_better(sb
, split
, this);
946 plno
= chunk
->p_next
;
947 tlno
= chunk
->t_next
;
953 * See if lines currently target is suspected for can be attributed to
956 static int find_move_in_parent(struct scoreboard
*sb
,
957 struct origin
*target
,
958 struct origin
*parent
)
960 int last_in_target
, made_progress
;
961 struct blame_entry
*e
, split
[3];
964 last_in_target
= find_last_in_target(sb
, target
);
965 if (last_in_target
< 0)
966 return 1; /* nothing remains for this target */
968 fill_origin_blob(parent
, &file_p
);
973 while (made_progress
) {
975 for (e
= sb
->ent
; e
; e
= e
->next
) {
976 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
978 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
979 if (split
[1].suspect
&&
980 blame_move_score
< ent_score(sb
, &split
[1])) {
981 split_blame(sb
, split
, e
);
991 struct blame_entry
*ent
;
992 struct blame_entry split
[3];
996 * Count the number of entries the target is suspected for,
997 * and prepare a list of entry and the best split.
999 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1000 struct origin
*target
,
1003 struct blame_entry
*e
;
1005 struct blame_list
*blame_list
= NULL
;
1007 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1008 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1011 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1012 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1013 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1014 blame_list
[i
++].ent
= e
;
1016 *num_ents_p
= num_ents
;
1021 * For lines target is suspected for, see if we can find code movement
1022 * across file boundary from the parent commit. porigin is the path
1023 * in the parent we already tried.
1025 static int find_copy_in_parent(struct scoreboard
*sb
,
1026 struct origin
*target
,
1027 struct commit
*parent
,
1028 struct origin
*porigin
,
1031 struct diff_options diff_opts
;
1032 const char *paths
[1];
1035 struct blame_list
*blame_list
;
1038 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1040 return 1; /* nothing remains for this target */
1042 diff_setup(&diff_opts
);
1043 diff_opts
.recursive
= 1;
1044 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1047 diff_tree_setup_paths(paths
, &diff_opts
);
1048 if (diff_setup_done(&diff_opts
) < 0)
1051 /* Try "find copies harder" on new path if requested;
1052 * we do not want to use diffcore_rename() actually to
1053 * match things up; find_copies_harder is set only to
1054 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1055 * and this code needs to be after diff_setup_done(), which
1056 * usually makes find-copies-harder imply copy detection.
1058 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
1059 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
1060 diff_opts
.find_copies_harder
= 1;
1062 if (is_null_sha1(target
->commit
->object
.sha1
))
1063 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1065 diff_tree_sha1(parent
->tree
->object
.sha1
,
1066 target
->commit
->tree
->object
.sha1
,
1069 if (!diff_opts
.find_copies_harder
)
1070 diffcore_std(&diff_opts
);
1074 int made_progress
= 0;
1076 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1077 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1078 struct origin
*norigin
;
1080 struct blame_entry
this[3];
1082 if (!DIFF_FILE_VALID(p
->one
))
1083 continue; /* does not exist in parent */
1084 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1085 /* find_move already dealt with this path */
1088 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1089 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1090 fill_origin_blob(norigin
, &file_p
);
1094 for (j
= 0; j
< num_ents
; j
++) {
1095 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1096 norigin
, this, &file_p
);
1097 copy_split_if_better(sb
, blame_list
[j
].split
,
1101 origin_decref(norigin
);
1104 for (j
= 0; j
< num_ents
; j
++) {
1105 struct blame_entry
*split
= blame_list
[j
].split
;
1106 if (split
[1].suspect
&&
1107 blame_copy_score
< ent_score(sb
, &split
[1])) {
1108 split_blame(sb
, split
, blame_list
[j
].ent
);
1111 decref_split(split
);
1117 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1123 diff_flush(&diff_opts
);
1129 * The blobs of origin and porigin exactly match, so everything
1130 * origin is suspected for can be blamed on the parent.
1132 static void pass_whole_blame(struct scoreboard
*sb
,
1133 struct origin
*origin
, struct origin
*porigin
)
1135 struct blame_entry
*e
;
1137 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1138 /* Steal its file */
1139 porigin
->file
= origin
->file
;
1140 origin
->file
.ptr
= NULL
;
1142 for (e
= sb
->ent
; e
; e
= e
->next
) {
1143 if (!same_suspect(e
->suspect
, origin
))
1145 origin_incref(porigin
);
1146 origin_decref(e
->suspect
);
1147 e
->suspect
= porigin
;
1151 #define MAXPARENT 16
1153 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1156 struct commit
*commit
= origin
->commit
;
1157 struct commit_list
*parent
;
1158 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1160 memset(parent_origin
, 0, sizeof(parent_origin
));
1162 /* The first pass looks for unrenamed path to optimize for
1163 * common cases, then we look for renames in the second pass.
1165 for (pass
= 0; pass
< 2; pass
++) {
1166 struct origin
*(*find
)(struct scoreboard
*,
1167 struct commit
*, struct origin
*);
1168 find
= pass
? find_rename
: find_origin
;
1170 for (i
= 0, parent
= commit
->parents
;
1171 i
< MAXPARENT
&& parent
;
1172 parent
= parent
->next
, i
++) {
1173 struct commit
*p
= parent
->item
;
1176 if (parent_origin
[i
])
1178 if (parse_commit(p
))
1180 porigin
= find(sb
, p
, origin
);
1183 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1184 pass_whole_blame(sb
, origin
, porigin
);
1185 origin_decref(porigin
);
1188 for (j
= same
= 0; j
< i
; j
++)
1189 if (parent_origin
[j
] &&
1190 !hashcmp(parent_origin
[j
]->blob_sha1
,
1191 porigin
->blob_sha1
)) {
1196 parent_origin
[i
] = porigin
;
1198 origin_decref(porigin
);
1203 for (i
= 0, parent
= commit
->parents
;
1204 i
< MAXPARENT
&& parent
;
1205 parent
= parent
->next
, i
++) {
1206 struct origin
*porigin
= parent_origin
[i
];
1209 if (pass_blame_to_parent(sb
, origin
, porigin
))
1214 * Optionally find moves in parents' files.
1216 if (opt
& PICKAXE_BLAME_MOVE
)
1217 for (i
= 0, parent
= commit
->parents
;
1218 i
< MAXPARENT
&& parent
;
1219 parent
= parent
->next
, i
++) {
1220 struct origin
*porigin
= parent_origin
[i
];
1223 if (find_move_in_parent(sb
, origin
, porigin
))
1228 * Optionally find copies from parents' files.
1230 if (opt
& PICKAXE_BLAME_COPY
)
1231 for (i
= 0, parent
= commit
->parents
;
1232 i
< MAXPARENT
&& parent
;
1233 parent
= parent
->next
, i
++) {
1234 struct origin
*porigin
= parent_origin
[i
];
1235 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1241 for (i
= 0; i
< MAXPARENT
; i
++)
1242 origin_decref(parent_origin
[i
]);
1246 * Information on commits, used for output.
1251 const char *author_mail
;
1252 unsigned long author_time
;
1253 const char *author_tz
;
1255 /* filled only when asked for details */
1256 const char *committer
;
1257 const char *committer_mail
;
1258 unsigned long committer_time
;
1259 const char *committer_tz
;
1261 const char *summary
;
1265 * Parse author/committer line in the commit object buffer
1267 static void get_ac_line(const char *inbuf
, const char *what
,
1268 int bufsz
, char *person
, const char **mail
,
1269 unsigned long *time
, const char **tz
)
1271 int len
, tzlen
, maillen
;
1272 char *tmp
, *endp
, *timepos
;
1274 tmp
= strstr(inbuf
, what
);
1277 tmp
+= strlen(what
);
1278 endp
= strchr(tmp
, '\n');
1286 *mail
= *tz
= "(unknown)";
1290 memcpy(person
, tmp
, len
);
1298 tzlen
= (person
+len
)-(tmp
+1);
1303 *time
= strtoul(tmp
, NULL
, 10);
1311 maillen
= timepos
- tmp
;
1317 * mailmap expansion may make the name longer.
1318 * make room by pushing stuff down.
1320 tmp
= person
+ bufsz
- (tzlen
+ 1);
1321 memmove(tmp
, *tz
, tzlen
);
1325 tmp
= tmp
- (maillen
+ 1);
1326 memmove(tmp
, *mail
, maillen
);
1331 * Now, convert e-mail using mailmap
1333 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1336 static void get_commit_info(struct commit
*commit
,
1337 struct commit_info
*ret
,
1342 static char author_buf
[1024];
1343 static char committer_buf
[1024];
1344 static char summary_buf
[1024];
1347 * We've operated without save_commit_buffer, so
1348 * we now need to populate them for output.
1350 if (!commit
->buffer
) {
1351 enum object_type type
;
1354 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1356 ret
->author
= author_buf
;
1357 get_ac_line(commit
->buffer
, "\nauthor ",
1358 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1359 &ret
->author_time
, &ret
->author_tz
);
1364 ret
->committer
= committer_buf
;
1365 get_ac_line(commit
->buffer
, "\ncommitter ",
1366 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1367 &ret
->committer_time
, &ret
->committer_tz
);
1369 ret
->summary
= summary_buf
;
1370 tmp
= strstr(commit
->buffer
, "\n\n");
1373 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1377 endp
= strchr(tmp
, '\n');
1379 endp
= tmp
+ strlen(tmp
);
1381 if (len
>= sizeof(summary_buf
) || len
== 0)
1383 memcpy(summary_buf
, tmp
, len
);
1384 summary_buf
[len
] = 0;
1388 * To allow LF and other nonportable characters in pathnames,
1389 * they are c-style quoted as needed.
1391 static void write_filename_info(const char *path
)
1393 printf("filename ");
1394 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1399 * The blame_entry is found to be guilty for the range. Mark it
1400 * as such, and show it in incremental output.
1402 static void found_guilty_entry(struct blame_entry
*ent
)
1408 struct origin
*suspect
= ent
->suspect
;
1410 printf("%s %d %d %d\n",
1411 sha1_to_hex(suspect
->commit
->object
.sha1
),
1412 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1413 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1414 struct commit_info ci
;
1415 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1416 get_commit_info(suspect
->commit
, &ci
, 1);
1417 printf("author %s\n", ci
.author
);
1418 printf("author-mail %s\n", ci
.author_mail
);
1419 printf("author-time %lu\n", ci
.author_time
);
1420 printf("author-tz %s\n", ci
.author_tz
);
1421 printf("committer %s\n", ci
.committer
);
1422 printf("committer-mail %s\n", ci
.committer_mail
);
1423 printf("committer-time %lu\n", ci
.committer_time
);
1424 printf("committer-tz %s\n", ci
.committer_tz
);
1425 printf("summary %s\n", ci
.summary
);
1426 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1427 printf("boundary\n");
1429 write_filename_info(suspect
->path
);
1434 * The main loop -- while the scoreboard has lines whose true origin
1435 * is still unknown, pick one blame_entry, and allow its current
1436 * suspect to pass blames to its parents.
1438 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1441 struct blame_entry
*ent
;
1442 struct commit
*commit
;
1443 struct origin
*suspect
= NULL
;
1445 /* find one suspect to break down */
1446 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1448 suspect
= ent
->suspect
;
1450 return; /* all done */
1453 * We will use this suspect later in the loop,
1454 * so hold onto it in the meantime.
1456 origin_incref(suspect
);
1457 commit
= suspect
->commit
;
1458 if (!commit
->object
.parsed
)
1459 parse_commit(commit
);
1460 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1461 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1462 pass_blame(sb
, suspect
, opt
);
1464 commit
->object
.flags
|= UNINTERESTING
;
1465 if (commit
->object
.parsed
)
1466 mark_parents_uninteresting(commit
);
1468 /* treat root commit as boundary */
1469 if (!commit
->parents
&& !show_root
)
1470 commit
->object
.flags
|= UNINTERESTING
;
1472 /* Take responsibility for the remaining entries */
1473 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1474 if (same_suspect(ent
->suspect
, suspect
))
1475 found_guilty_entry(ent
);
1476 origin_decref(suspect
);
1478 if (DEBUG
) /* sanity */
1479 sanity_check_refcnt(sb
);
1483 static const char *format_time(unsigned long time
, const char *tz_str
,
1486 static char time_buf
[128];
1491 if (show_raw_time
) {
1492 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1497 minutes
= tz
< 0 ? -tz
: tz
;
1498 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1499 minutes
= tz
< 0 ? -minutes
: minutes
;
1500 t
= time
+ minutes
* 60;
1503 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1504 strcat(time_buf
, tz_str
);
1508 #define OUTPUT_ANNOTATE_COMPAT 001
1509 #define OUTPUT_LONG_OBJECT_NAME 002
1510 #define OUTPUT_RAW_TIMESTAMP 004
1511 #define OUTPUT_PORCELAIN 010
1512 #define OUTPUT_SHOW_NAME 020
1513 #define OUTPUT_SHOW_NUMBER 040
1514 #define OUTPUT_SHOW_SCORE 0100
1515 #define OUTPUT_NO_AUTHOR 0200
1517 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1521 struct origin
*suspect
= ent
->suspect
;
1524 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1525 printf("%s%c%d %d %d\n",
1527 ent
->guilty
? ' ' : '*', // purely for debugging
1531 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1532 struct commit_info ci
;
1533 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1534 get_commit_info(suspect
->commit
, &ci
, 1);
1535 printf("author %s\n", ci
.author
);
1536 printf("author-mail %s\n", ci
.author_mail
);
1537 printf("author-time %lu\n", ci
.author_time
);
1538 printf("author-tz %s\n", ci
.author_tz
);
1539 printf("committer %s\n", ci
.committer
);
1540 printf("committer-mail %s\n", ci
.committer_mail
);
1541 printf("committer-time %lu\n", ci
.committer_time
);
1542 printf("committer-tz %s\n", ci
.committer_tz
);
1543 write_filename_info(suspect
->path
);
1544 printf("summary %s\n", ci
.summary
);
1545 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1546 printf("boundary\n");
1548 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1549 write_filename_info(suspect
->path
);
1551 cp
= nth_line(sb
, ent
->lno
);
1552 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1555 printf("%s %d %d\n", hex
,
1556 ent
->s_lno
+ 1 + cnt
,
1557 ent
->lno
+ 1 + cnt
);
1562 } while (ch
!= '\n' &&
1563 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1567 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1571 struct origin
*suspect
= ent
->suspect
;
1572 struct commit_info ci
;
1574 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1576 get_commit_info(suspect
->commit
, &ci
, 1);
1577 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1579 cp
= nth_line(sb
, ent
->lno
);
1580 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1582 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1584 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1586 memset(hex
, ' ', length
);
1587 else if (!cmd_is_annotate
) {
1593 printf("%.*s", length
, hex
);
1594 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1595 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1596 format_time(ci
.author_time
, ci
.author_tz
,
1598 ent
->lno
+ 1 + cnt
);
1600 if (opt
& OUTPUT_SHOW_SCORE
)
1602 max_score_digits
, ent
->score
,
1603 ent
->suspect
->refcnt
);
1604 if (opt
& OUTPUT_SHOW_NAME
)
1605 printf(" %-*.*s", longest_file
, longest_file
,
1607 if (opt
& OUTPUT_SHOW_NUMBER
)
1608 printf(" %*d", max_orig_digits
,
1609 ent
->s_lno
+ 1 + cnt
);
1611 if (!(opt
& OUTPUT_NO_AUTHOR
))
1612 printf(" (%-*.*s %10s",
1613 longest_author
, longest_author
,
1615 format_time(ci
.author_time
,
1619 max_digits
, ent
->lno
+ 1 + cnt
);
1624 } while (ch
!= '\n' &&
1625 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1629 static void output(struct scoreboard
*sb
, int option
)
1631 struct blame_entry
*ent
;
1633 if (option
& OUTPUT_PORCELAIN
) {
1634 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1635 struct blame_entry
*oth
;
1636 struct origin
*suspect
= ent
->suspect
;
1637 struct commit
*commit
= suspect
->commit
;
1638 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1640 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1641 if ((oth
->suspect
->commit
!= commit
) ||
1642 !strcmp(oth
->suspect
->path
, suspect
->path
))
1644 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1650 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1651 if (option
& OUTPUT_PORCELAIN
)
1652 emit_porcelain(sb
, ent
);
1654 emit_other(sb
, ent
, option
);
1660 * To allow quick access to the contents of nth line in the
1661 * final image, prepare an index in the scoreboard.
1663 static int prepare_lines(struct scoreboard
*sb
)
1665 const char *buf
= sb
->final_buf
;
1666 unsigned long len
= sb
->final_buf_size
;
1667 int num
= 0, incomplete
= 0, bol
= 1;
1669 if (len
&& buf
[len
-1] != '\n')
1670 incomplete
++; /* incomplete line at the end */
1673 sb
->lineno
= xrealloc(sb
->lineno
,
1674 sizeof(int* ) * (num
+ 1));
1675 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1678 if (*buf
++ == '\n') {
1683 sb
->lineno
= xrealloc(sb
->lineno
,
1684 sizeof(int* ) * (num
+ incomplete
+ 1));
1685 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1686 sb
->num_lines
= num
+ incomplete
;
1687 return sb
->num_lines
;
1691 * Add phony grafts for use with -S; this is primarily to
1692 * support git-cvsserver that wants to give a linear history
1695 static int read_ancestry(const char *graft_file
)
1697 FILE *fp
= fopen(graft_file
, "r");
1701 while (fgets(buf
, sizeof(buf
), fp
)) {
1702 /* The format is just "Commit Parent1 Parent2 ...\n" */
1703 int len
= strlen(buf
);
1704 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1706 register_commit_graft(graft
, 0);
1713 * How many columns do we need to show line numbers in decimal?
1715 static int lineno_width(int lines
)
1719 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1725 * How many columns do we need to show line numbers, authors,
1728 static void find_alignment(struct scoreboard
*sb
, int *option
)
1730 int longest_src_lines
= 0;
1731 int longest_dst_lines
= 0;
1732 unsigned largest_score
= 0;
1733 struct blame_entry
*e
;
1735 for (e
= sb
->ent
; e
; e
= e
->next
) {
1736 struct origin
*suspect
= e
->suspect
;
1737 struct commit_info ci
;
1740 if (strcmp(suspect
->path
, sb
->path
))
1741 *option
|= OUTPUT_SHOW_NAME
;
1742 num
= strlen(suspect
->path
);
1743 if (longest_file
< num
)
1745 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1746 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1747 get_commit_info(suspect
->commit
, &ci
, 1);
1748 num
= strlen(ci
.author
);
1749 if (longest_author
< num
)
1750 longest_author
= num
;
1752 num
= e
->s_lno
+ e
->num_lines
;
1753 if (longest_src_lines
< num
)
1754 longest_src_lines
= num
;
1755 num
= e
->lno
+ e
->num_lines
;
1756 if (longest_dst_lines
< num
)
1757 longest_dst_lines
= num
;
1758 if (largest_score
< ent_score(sb
, e
))
1759 largest_score
= ent_score(sb
, e
);
1761 max_orig_digits
= lineno_width(longest_src_lines
);
1762 max_digits
= lineno_width(longest_dst_lines
);
1763 max_score_digits
= lineno_width(largest_score
);
1767 * For debugging -- origin is refcounted, and this asserts that
1768 * we do not underflow.
1770 static void sanity_check_refcnt(struct scoreboard
*sb
)
1773 struct blame_entry
*ent
;
1775 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1776 /* Nobody should have zero or negative refcnt */
1777 if (ent
->suspect
->refcnt
<= 0) {
1778 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1780 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1781 ent
->suspect
->refcnt
);
1785 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1786 /* Mark the ones that haven't been checked */
1787 if (0 < ent
->suspect
->refcnt
)
1788 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1790 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1792 * ... then pick each and see if they have the the
1796 struct blame_entry
*e
;
1797 struct origin
*suspect
= ent
->suspect
;
1799 if (0 < suspect
->refcnt
)
1801 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1802 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1803 if (e
->suspect
!= suspect
)
1807 if (suspect
->refcnt
!= found
) {
1808 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1810 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1811 ent
->suspect
->refcnt
, found
);
1817 find_alignment(sb
, &opt
);
1819 die("Baa %d!", baa
);
1824 * Used for the command line parsing; check if the path exists
1825 * in the working tree.
1827 static int has_path_in_work_tree(const char *path
)
1830 return !lstat(path
, &st
);
1833 static unsigned parse_score(const char *arg
)
1836 unsigned long score
= strtoul(arg
, &end
, 10);
1842 static const char *add_prefix(const char *prefix
, const char *path
)
1844 if (!prefix
|| !prefix
[0])
1846 return prefix_path(prefix
, strlen(prefix
), path
);
1850 * Parsing of (comma separated) one item in the -L option
1852 static const char *parse_loc(const char *spec
,
1853 struct scoreboard
*sb
, long lno
,
1854 long begin
, long *ret
)
1861 regmatch_t match
[1];
1863 /* Allow "-L <something>,+20" to mean starting at <something>
1864 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1867 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1868 num
= strtol(spec
+ 1, &term
, 10);
1869 if (term
!= spec
+ 1) {
1873 *ret
= begin
+ num
- 2;
1882 num
= strtol(spec
, &term
, 10);
1890 /* it could be a regexp of form /.../ */
1891 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1898 /* try [spec+1 .. term-1] as regexp */
1900 begin
--; /* input is in human terms */
1901 line
= nth_line(sb
, begin
);
1903 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1904 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1905 const char *cp
= line
+ match
[0].rm_so
;
1908 while (begin
++ < lno
) {
1909 nline
= nth_line(sb
, begin
);
1910 if (line
<= cp
&& cp
< nline
)
1921 regerror(reg_error
, ®exp
, errbuf
, 1024);
1922 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1927 * Parsing of -L option
1929 static void prepare_blame_range(struct scoreboard
*sb
,
1930 const char *bottomtop
,
1932 long *bottom
, long *top
)
1936 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1938 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1946 static int git_blame_config(const char *var
, const char *value
)
1948 if (!strcmp(var
, "blame.showroot")) {
1949 show_root
= git_config_bool(var
, value
);
1952 if (!strcmp(var
, "blame.blankboundary")) {
1953 blank_boundary
= git_config_bool(var
, value
);
1956 return git_default_config(var
, value
);
1959 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1961 struct commit
*commit
;
1962 struct origin
*origin
;
1963 unsigned char head_sha1
[20];
1968 unsigned long fin_size
;
1970 struct cache_entry
*ce
;
1973 if (get_sha1("HEAD", head_sha1
))
1974 die("No such ref: HEAD");
1977 commit
= xcalloc(1, sizeof(*commit
));
1978 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
1979 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
1980 commit
->object
.parsed
= 1;
1982 commit
->object
.type
= OBJ_COMMIT
;
1984 origin
= make_origin(commit
, path
);
1986 if (!contents_from
|| strcmp("-", contents_from
)) {
1988 const char *read_from
;
1990 if (contents_from
) {
1991 if (stat(contents_from
, &st
) < 0)
1992 die("Cannot stat %s", contents_from
);
1993 read_from
= contents_from
;
1996 if (lstat(path
, &st
) < 0)
1997 die("Cannot lstat %s", path
);
2000 fin_size
= xsize_t(st
.st_size
);
2001 buf
= xmalloc(fin_size
+1);
2002 mode
= canon_mode(st
.st_mode
);
2003 switch (st
.st_mode
& S_IFMT
) {
2005 fd
= open(read_from
, O_RDONLY
);
2007 die("cannot open %s", read_from
);
2008 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
2009 die("cannot read %s", read_from
);
2012 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
2013 die("cannot readlink %s", read_from
);
2016 die("unsupported file type %s", read_from
);
2020 /* Reading from stdin */
2021 contents_from
= "standard input";
2027 buf
= xrealloc(buf
, fin_size
+ cnt
);
2028 cnt
= xread(0, buf
+ fin_size
, cnt
);
2030 die("read error %s from stdin",
2036 buf
= xrealloc(buf
, fin_size
+ 1);
2039 origin
->file
.ptr
= buf
;
2040 origin
->file
.size
= fin_size
;
2041 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2042 commit
->util
= origin
;
2045 * Read the current index, replace the path entry with
2046 * origin->blob_sha1 without mucking with its mode or type
2047 * bits; we are not going to write this index out -- we just
2048 * want to run "diff-index --cached".
2055 int pos
= cache_name_pos(path
, len
);
2057 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2059 /* Let's not bother reading from HEAD tree */
2060 mode
= S_IFREG
| 0644;
2062 size
= cache_entry_size(len
);
2063 ce
= xcalloc(1, size
);
2064 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2065 memcpy(ce
->name
, path
, len
);
2066 ce
->ce_flags
= create_ce_flags(len
, 0);
2067 ce
->ce_mode
= create_ce_mode(mode
);
2068 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2071 * We are not going to write this out, so this does not matter
2072 * right now, but someday we might optimize diff-index --cached
2073 * with cache-tree information.
2075 cache_tree_invalidate_path(active_cache_tree
, path
);
2077 commit
->buffer
= xmalloc(400);
2078 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2079 snprintf(commit
->buffer
, 400,
2080 "tree 0000000000000000000000000000000000000000\n"
2084 "Version of %s from %s\n",
2085 sha1_to_hex(head_sha1
),
2086 ident
, ident
, path
, contents_from
? contents_from
: path
);
2090 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2092 struct rev_info revs
;
2094 struct scoreboard sb
;
2096 struct blame_entry
*ent
;
2097 int i
, seen_dashdash
, unk
, opt
;
2098 long bottom
, top
, lno
;
2099 int output_option
= 0;
2101 const char *revs_file
= NULL
;
2102 const char *final_commit_name
= NULL
;
2103 enum object_type type
;
2104 const char *bottomtop
= NULL
;
2105 const char *contents_from
= NULL
;
2107 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2109 git_config(git_blame_config
);
2110 save_commit_buffer
= 0;
2114 for (unk
= i
= 1; i
< argc
; i
++) {
2115 const char *arg
= argv
[i
];
2118 else if (!strcmp("-b", arg
))
2120 else if (!strcmp("--root", arg
))
2122 else if (!strcmp(arg
, "--show-stats"))
2124 else if (!strcmp("-c", arg
))
2125 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2126 else if (!strcmp("-t", arg
))
2127 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2128 else if (!strcmp("-l", arg
))
2129 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2130 else if (!strcmp("-s", arg
))
2131 output_option
|= OUTPUT_NO_AUTHOR
;
2132 else if (!strcmp("-S", arg
) && ++i
< argc
)
2133 revs_file
= argv
[i
];
2134 else if (!prefixcmp(arg
, "-M")) {
2135 opt
|= PICKAXE_BLAME_MOVE
;
2136 blame_move_score
= parse_score(arg
+2);
2138 else if (!prefixcmp(arg
, "-C")) {
2139 if (opt
& PICKAXE_BLAME_COPY
)
2140 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2141 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2142 blame_copy_score
= parse_score(arg
+2);
2144 else if (!prefixcmp(arg
, "-L")) {
2153 die("More than one '-L n,m' option given");
2156 else if (!strcmp("--contents", arg
)) {
2159 contents_from
= argv
[i
];
2161 else if (!strcmp("--incremental", arg
))
2163 else if (!strcmp("--score-debug", arg
))
2164 output_option
|= OUTPUT_SHOW_SCORE
;
2165 else if (!strcmp("-f", arg
) ||
2166 !strcmp("--show-name", arg
))
2167 output_option
|= OUTPUT_SHOW_NAME
;
2168 else if (!strcmp("-n", arg
) ||
2169 !strcmp("--show-number", arg
))
2170 output_option
|= OUTPUT_SHOW_NUMBER
;
2171 else if (!strcmp("-p", arg
) ||
2172 !strcmp("--porcelain", arg
))
2173 output_option
|= OUTPUT_PORCELAIN
;
2174 else if (!strcmp("--", arg
)) {
2186 if (!blame_move_score
)
2187 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2188 if (!blame_copy_score
)
2189 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2192 * We have collected options unknown to us in argv[1..unk]
2193 * which are to be passed to revision machinery if we are
2194 * going to do the "bottom" processing.
2196 * The remaining are:
2198 * (1) if seen_dashdash, its either
2199 * "-options -- <path>" or
2200 * "-options -- <path> <rev>".
2201 * but the latter is allowed only if there is no
2202 * options that we passed to revision machinery.
2204 * (2) otherwise, we may have "--" somewhere later and
2205 * might be looking at the first one of multiple 'rev'
2206 * parameters (e.g. " master ^next ^maint -- path").
2207 * See if there is a dashdash first, and give the
2208 * arguments before that to revision machinery.
2209 * After that there must be one 'path'.
2211 * (3) otherwise, its one of the three:
2212 * "-options <path> <rev>"
2213 * "-options <rev> <path>"
2215 * but again the first one is allowed only if
2216 * there is no options that we passed to revision
2220 if (seen_dashdash
) {
2224 path
= add_prefix(prefix
, argv
[i
]);
2225 if (i
+ 1 == argc
- 1) {
2228 argv
[unk
++] = argv
[i
+ 1];
2230 else if (i
+ 1 != argc
)
2231 /* garbage at end */
2236 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2237 if (!strcmp(argv
[j
], "--"))
2239 if (seen_dashdash
) {
2241 if (seen_dashdash
+ 1 != argc
- 1)
2243 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2244 for (j
= i
; j
< seen_dashdash
; j
++)
2245 argv
[unk
++] = argv
[j
];
2251 path
= add_prefix(prefix
, argv
[i
]);
2252 if (i
+ 1 == argc
- 1) {
2253 final_commit_name
= argv
[i
+ 1];
2255 /* if (unk == 1) we could be getting
2258 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2259 path
= add_prefix(prefix
, argv
[i
+ 1]);
2260 final_commit_name
= argv
[i
];
2263 else if (i
!= argc
- 1)
2264 usage(blame_usage
); /* garbage at end */
2266 if (!has_path_in_work_tree(path
))
2267 die("cannot stat path %s: %s",
2268 path
, strerror(errno
));
2272 if (final_commit_name
)
2273 argv
[unk
++] = final_commit_name
;
2276 * Now we got rev and path. We do not want the path pruning
2277 * but we may want "bottom" processing.
2279 argv
[unk
++] = "--"; /* terminate the rev name */
2282 init_revisions(&revs
, NULL
);
2283 setup_revisions(unk
, argv
, &revs
, NULL
);
2284 memset(&sb
, 0, sizeof(sb
));
2287 * There must be one and only one positive commit in the
2288 * revs->pending array.
2290 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2291 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2292 if (obj
->flags
& UNINTERESTING
)
2294 while (obj
->type
== OBJ_TAG
)
2295 obj
= deref_tag(obj
, NULL
, 0);
2296 if (obj
->type
!= OBJ_COMMIT
)
2297 die("Non commit %s?",
2298 revs
.pending
.objects
[i
].name
);
2300 die("More than one commit to dig from %s and %s?",
2301 revs
.pending
.objects
[i
].name
,
2303 sb
.final
= (struct commit
*) obj
;
2304 final_commit_name
= revs
.pending
.objects
[i
].name
;
2309 * "--not A B -- path" without anything positive;
2310 * do not default to HEAD, but use the working tree
2313 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2314 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2316 else if (contents_from
)
2317 die("Cannot use --contents with final commit object name");
2320 * If we have bottom, this will mark the ancestors of the
2321 * bottom commits we would reach while traversing as
2324 prepare_revision_walk(&revs
);
2326 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2329 buf
= xmalloc(o
->file
.size
+ 1);
2330 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2332 sb
.final_buf_size
= o
->file
.size
;
2335 o
= get_origin(&sb
, sb
.final
, path
);
2336 if (fill_blob_sha1(o
))
2337 die("no such path %s in %s", path
, final_commit_name
);
2339 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2340 &sb
.final_buf_size
);
2343 lno
= prepare_lines(&sb
);
2347 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2348 if (bottom
&& top
&& top
< bottom
) {
2350 tmp
= top
; top
= bottom
; bottom
= tmp
;
2358 die("file %s has only %lu lines", path
, lno
);
2360 ent
= xcalloc(1, sizeof(*ent
));
2362 ent
->num_lines
= top
- bottom
;
2364 ent
->s_lno
= bottom
;
2369 if (revs_file
&& read_ancestry(revs_file
))
2370 die("reading graft file %s failed: %s",
2371 revs_file
, strerror(errno
));
2373 read_mailmap(&mailmap
, ".mailmap", NULL
);
2375 assign_blame(&sb
, &revs
, opt
);
2382 if (!(output_option
& OUTPUT_PORCELAIN
))
2383 find_alignment(&sb
, &output_option
);
2385 output(&sb
, output_option
);
2386 free((void *)sb
.final_buf
);
2387 for (ent
= sb
.ent
; ent
; ) {
2388 struct blame_entry
*e
= ent
->next
;
2394 printf("num read blob: %d\n", num_read_blob
);
2395 printf("num get patch: %d\n", num_get_patch
);
2396 printf("num commits: %d\n", num_commits
);