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"
23 static char blame_usage
[] =
24 "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"
25 " -c Use the same output mode as git-annotate (Default: off)\n"
26 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
27 " -l Show long commit SHA1 (Default: off)\n"
28 " --root Do not treat root commits as boundaries (Default: off)\n"
29 " -t Show raw timestamp (Default: off)\n"
30 " -f, --show-name Show original filename (Default: auto)\n"
31 " -n, --show-number Show original linenumber (Default: off)\n"
32 " -s Suppress author name and timestamp (Default: off)\n"
33 " -p, --porcelain Show in a format designed for machine consumption\n"
34 " -w Ignore whitespace differences\n"
35 " -L n,m Process only line range n,m, counting from 1\n"
36 " -M, -C Find line movements within and across files\n"
37 " --incremental Show blame entries as we find them, incrementally\n"
38 " --contents file Use <file>'s contents as the final image\n"
39 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
41 static int longest_file
;
42 static int longest_author
;
43 static int max_orig_digits
;
44 static int max_digits
;
45 static int max_score_digits
;
47 static int blank_boundary
;
48 static int incremental
;
49 static int cmd_is_annotate
;
50 static int xdl_opts
= XDF_NEED_MINIMAL
;
51 static struct path_list mailmap
;
58 static int num_read_blob
;
59 static int num_get_patch
;
60 static int num_commits
;
62 #define PICKAXE_BLAME_MOVE 01
63 #define PICKAXE_BLAME_COPY 02
64 #define PICKAXE_BLAME_COPY_HARDER 04
65 #define PICKAXE_BLAME_COPY_HARDEST 010
68 * blame for a blame_entry with score lower than these thresholds
69 * is not passed to the parent using move/copy logic.
71 static unsigned blame_move_score
;
72 static unsigned blame_copy_score
;
73 #define BLAME_DEFAULT_MOVE_SCORE 20
74 #define BLAME_DEFAULT_COPY_SCORE 40
76 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
77 #define METAINFO_SHOWN (1u<<12)
78 #define MORE_THAN_ONE_PATH (1u<<13)
81 * One blob in a commit that is being suspected
85 struct commit
*commit
;
87 unsigned char blob_sha1
[20];
88 char path
[FLEX_ARRAY
];
92 * Given an origin, prepare mmfile_t structure to be used by the
95 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
98 enum object_type type
;
100 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
101 (unsigned long *)(&(file
->size
)));
103 die("Cannot read blob %s for path %s",
104 sha1_to_hex(o
->blob_sha1
),
114 * Origin is refcounted and usually we keep the blob contents to be
117 static inline struct origin
*origin_incref(struct origin
*o
)
124 static void origin_decref(struct origin
*o
)
126 if (o
&& --o
->refcnt
<= 0) {
129 memset(o
, 0, sizeof(*o
));
135 * Each group of lines is described by a blame_entry; it can be split
136 * as we pass blame to the parents. They form a linked list in the
137 * scoreboard structure, sorted by the target line number.
140 struct blame_entry
*prev
;
141 struct blame_entry
*next
;
143 /* the first line of this group in the final image;
144 * internally all line numbers are 0 based.
148 /* how many lines this group has */
151 /* the commit that introduced this group into the final image */
152 struct origin
*suspect
;
154 /* true if the suspect is truly guilty; false while we have not
155 * checked if the group came from one of its parents.
159 /* the line number of the first line of this group in the
160 * suspect's file; internally all line numbers are 0 based.
164 /* how significant this entry is -- cached to avoid
165 * scanning the lines over and over.
171 * The current state of the blame assignment.
174 /* the final commit (i.e. where we started digging from) */
175 struct commit
*final
;
180 * The contents in the final image.
181 * Used by many functions to obtain contents of the nth line,
182 * indexed with scoreboard.lineno[blame_entry.lno].
184 const char *final_buf
;
185 unsigned long final_buf_size
;
187 /* linked list of blames */
188 struct blame_entry
*ent
;
190 /* look-up a line in the final buffer */
195 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
199 if (a
->commit
!= b
->commit
)
201 return !strcmp(a
->path
, b
->path
);
204 static void sanity_check_refcnt(struct scoreboard
*);
207 * If two blame entries that are next to each other came from
208 * contiguous lines in the same origin (i.e. <commit, path> pair),
209 * merge them together.
211 static void coalesce(struct scoreboard
*sb
)
213 struct blame_entry
*ent
, *next
;
215 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
216 if (same_suspect(ent
->suspect
, next
->suspect
) &&
217 ent
->guilty
== next
->guilty
&&
218 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
219 ent
->num_lines
+= next
->num_lines
;
220 ent
->next
= next
->next
;
222 ent
->next
->prev
= ent
;
223 origin_decref(next
->suspect
);
226 next
= ent
; /* again */
230 if (DEBUG
) /* sanity */
231 sanity_check_refcnt(sb
);
235 * Given a commit and a path in it, create a new origin structure.
236 * The callers that add blame to the scoreboard should use
237 * get_origin() to obtain shared, refcounted copy instead of calling
238 * this function directly.
240 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
243 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
246 strcpy(o
->path
, path
);
251 * Locate an existing origin or create a new one.
253 static struct origin
*get_origin(struct scoreboard
*sb
,
254 struct commit
*commit
,
257 struct blame_entry
*e
;
259 for (e
= sb
->ent
; e
; e
= e
->next
) {
260 if (e
->suspect
->commit
== commit
&&
261 !strcmp(e
->suspect
->path
, path
))
262 return origin_incref(e
->suspect
);
264 return make_origin(commit
, path
);
268 * Fill the blob_sha1 field of an origin if it hasn't, so that later
269 * call to fill_origin_blob() can use it to locate the data. blob_sha1
270 * for an origin is also used to pass the blame for the entire file to
271 * the parent to detect the case where a child's blob is identical to
272 * that of its parent's.
274 static int fill_blob_sha1(struct origin
*origin
)
278 if (!is_null_sha1(origin
->blob_sha1
))
280 if (get_tree_entry(origin
->commit
->object
.sha1
,
282 origin
->blob_sha1
, &mode
))
284 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
288 hashclr(origin
->blob_sha1
);
293 * We have an origin -- check if the same path exists in the
294 * parent and return an origin structure to represent it.
296 static struct origin
*find_origin(struct scoreboard
*sb
,
297 struct commit
*parent
,
298 struct origin
*origin
)
300 struct origin
*porigin
= NULL
;
301 struct diff_options diff_opts
;
302 const char *paths
[2];
306 * Each commit object can cache one origin in that
307 * commit. This is a freestanding copy of origin and
310 struct origin
*cached
= parent
->util
;
311 if (!strcmp(cached
->path
, origin
->path
)) {
313 * The same path between origin and its parent
314 * without renaming -- the most common case.
316 porigin
= get_origin(sb
, parent
, cached
->path
);
319 * If the origin was newly created (i.e. get_origin
320 * would call make_origin if none is found in the
321 * scoreboard), it does not know the blob_sha1,
322 * so copy it. Otherwise porigin was in the
323 * scoreboard and already knows blob_sha1.
325 if (porigin
->refcnt
== 1)
326 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
329 /* otherwise it was not very useful; free it */
334 /* See if the origin->path is different between parent
335 * and origin first. Most of the time they are the
336 * same and diff-tree is fairly efficient about this.
338 diff_setup(&diff_opts
);
339 diff_opts
.recursive
= 1;
340 diff_opts
.detect_rename
= 0;
341 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
342 paths
[0] = origin
->path
;
345 diff_tree_setup_paths(paths
, &diff_opts
);
346 if (diff_setup_done(&diff_opts
) < 0)
349 if (is_null_sha1(origin
->commit
->object
.sha1
))
350 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
352 diff_tree_sha1(parent
->tree
->object
.sha1
,
353 origin
->commit
->tree
->object
.sha1
,
355 diffcore_std(&diff_opts
);
357 /* It is either one entry that says "modified", or "created",
360 if (!diff_queued_diff
.nr
) {
361 /* The path is the same as parent */
362 porigin
= get_origin(sb
, parent
, origin
->path
);
363 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
365 else if (diff_queued_diff
.nr
!= 1)
366 die("internal error in blame::find_origin");
368 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
371 die("internal error in blame::find_origin (%c)",
374 porigin
= get_origin(sb
, parent
, origin
->path
);
375 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
379 /* Did not exist in parent, or type changed */
383 diff_flush(&diff_opts
);
386 * Create a freestanding copy that is not part of
387 * the refcounted origin found in the scoreboard, and
388 * cache it in the commit.
390 struct origin
*cached
;
392 cached
= make_origin(porigin
->commit
, porigin
->path
);
393 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
394 parent
->util
= cached
;
400 * We have an origin -- find the path that corresponds to it in its
401 * parent and return an origin structure to represent it.
403 static struct origin
*find_rename(struct scoreboard
*sb
,
404 struct commit
*parent
,
405 struct origin
*origin
)
407 struct origin
*porigin
= NULL
;
408 struct diff_options diff_opts
;
410 const char *paths
[2];
412 diff_setup(&diff_opts
);
413 diff_opts
.recursive
= 1;
414 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
415 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
416 diff_opts
.single_follow
= origin
->path
;
418 diff_tree_setup_paths(paths
, &diff_opts
);
419 if (diff_setup_done(&diff_opts
) < 0)
422 if (is_null_sha1(origin
->commit
->object
.sha1
))
423 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
425 diff_tree_sha1(parent
->tree
->object
.sha1
,
426 origin
->commit
->tree
->object
.sha1
,
428 diffcore_std(&diff_opts
);
430 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
431 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
432 if ((p
->status
== 'R' || p
->status
== 'C') &&
433 !strcmp(p
->two
->path
, origin
->path
)) {
434 porigin
= get_origin(sb
, parent
, p
->one
->path
);
435 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
439 diff_flush(&diff_opts
);
444 * Parsing of patch chunks...
447 /* line number in postimage; up to but not including this
448 * line is the same as preimage
452 /* preimage line number after this chunk */
455 /* postimage line number after this chunk */
460 struct chunk
*chunks
;
464 struct blame_diff_state
{
465 struct xdiff_emit_state xm
;
467 unsigned hunk_post_context
;
468 unsigned hunk_in_pre_context
: 1;
471 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
473 struct blame_diff_state
*state
= state_
;
475 int off1
, off2
, len1
, len2
, num
;
477 num
= state
->ret
->num
;
478 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
479 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
480 state
->ret
->chunks
[num
- 1].same
++;
482 state
->hunk_in_pre_context
= 0;
484 state
->hunk_post_context
++;
486 state
->hunk_post_context
= 0;
491 if (num
&& state
->hunk_post_context
) {
492 chunk
= &state
->ret
->chunks
[num
- 1];
493 chunk
->p_next
-= state
->hunk_post_context
;
494 chunk
->t_next
-= state
->hunk_post_context
;
496 state
->ret
->num
= ++num
;
497 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
498 sizeof(struct chunk
) * num
);
499 chunk
= &state
->ret
->chunks
[num
- 1];
500 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
505 /* Line numbers in patch output are one based. */
509 chunk
->same
= len2
? off2
: (off2
+ 1);
511 chunk
->p_next
= off1
+ (len1
? len1
: 1);
512 chunk
->t_next
= chunk
->same
+ len2
;
513 state
->hunk_in_pre_context
= 1;
514 state
->hunk_post_context
= 0;
517 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
520 struct blame_diff_state state
;
525 xpp
.flags
= xdl_opts
;
526 memset(&xecfg
, 0, sizeof(xecfg
));
527 xecfg
.ctxlen
= context
;
528 ecb
.outf
= xdiff_outf
;
530 memset(&state
, 0, sizeof(state
));
531 state
.xm
.consume
= process_u_diff
;
532 state
.ret
= xmalloc(sizeof(struct patch
));
533 state
.ret
->chunks
= NULL
;
536 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
538 if (state
.ret
->num
) {
540 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
541 chunk
->p_next
-= state
.hunk_post_context
;
542 chunk
->t_next
-= state
.hunk_post_context
;
548 * Run diff between two origins and grab the patch output, so that
549 * we can pass blame for lines origin is currently suspected for
552 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
554 mmfile_t file_p
, file_o
;
557 fill_origin_blob(parent
, &file_p
);
558 fill_origin_blob(origin
, &file_o
);
559 if (!file_p
.ptr
|| !file_o
.ptr
)
561 patch
= compare_buffer(&file_p
, &file_o
, 0);
566 static void free_patch(struct patch
*p
)
573 * Link in a new blame entry to the scoreboard. Entries that cover the
574 * same line range have been removed from the scoreboard previously.
576 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
578 struct blame_entry
*ent
, *prev
= NULL
;
580 origin_incref(e
->suspect
);
582 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
585 /* prev, if not NULL, is the last one that is below e */
588 e
->next
= prev
->next
;
600 * src typically is on-stack; we want to copy the information in it to
601 * an malloced blame_entry that is already on the linked list of the
602 * scoreboard. The origin of dst loses a refcnt while the origin of src
605 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
607 struct blame_entry
*p
, *n
;
611 origin_incref(src
->suspect
);
612 origin_decref(dst
->suspect
);
613 memcpy(dst
, src
, sizeof(*src
));
619 static const char *nth_line(struct scoreboard
*sb
, int lno
)
621 return sb
->final_buf
+ sb
->lineno
[lno
];
625 * It is known that lines between tlno to same came from parent, and e
626 * has an overlap with that range. it also is known that parent's
627 * line plno corresponds to e's line tlno.
633 * <------------------>
635 * Split e into potentially three parts; before this chunk, the chunk
636 * to be blamed for the parent, and after that portion.
638 static void split_overlap(struct blame_entry
*split
,
639 struct blame_entry
*e
,
640 int tlno
, int plno
, int same
,
641 struct origin
*parent
)
644 memset(split
, 0, sizeof(struct blame_entry
[3]));
646 if (e
->s_lno
< tlno
) {
647 /* there is a pre-chunk part not blamed on parent */
648 split
[0].suspect
= origin_incref(e
->suspect
);
649 split
[0].lno
= e
->lno
;
650 split
[0].s_lno
= e
->s_lno
;
651 split
[0].num_lines
= tlno
- e
->s_lno
;
652 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
653 split
[1].s_lno
= plno
;
656 split
[1].lno
= e
->lno
;
657 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
660 if (same
< e
->s_lno
+ e
->num_lines
) {
661 /* there is a post-chunk part not blamed on parent */
662 split
[2].suspect
= origin_incref(e
->suspect
);
663 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
664 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
665 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
666 chunk_end_lno
= split
[2].lno
;
669 chunk_end_lno
= e
->lno
+ e
->num_lines
;
670 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
673 * if it turns out there is nothing to blame the parent for,
674 * forget about the splitting. !split[1].suspect signals this.
676 if (split
[1].num_lines
< 1)
678 split
[1].suspect
= origin_incref(parent
);
682 * split_overlap() divided an existing blame e into up to three parts
683 * in split. Adjust the linked list of blames in the scoreboard to
686 static void split_blame(struct scoreboard
*sb
,
687 struct blame_entry
*split
,
688 struct blame_entry
*e
)
690 struct blame_entry
*new_entry
;
692 if (split
[0].suspect
&& split
[2].suspect
) {
693 /* The first part (reuse storage for the existing entry e) */
694 dup_entry(e
, &split
[0]);
696 /* The last part -- me */
697 new_entry
= xmalloc(sizeof(*new_entry
));
698 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
699 add_blame_entry(sb
, new_entry
);
701 /* ... and the middle part -- parent */
702 new_entry
= xmalloc(sizeof(*new_entry
));
703 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
704 add_blame_entry(sb
, new_entry
);
706 else if (!split
[0].suspect
&& !split
[2].suspect
)
708 * The parent covers the entire area; reuse storage for
709 * e and replace it with the parent.
711 dup_entry(e
, &split
[1]);
712 else if (split
[0].suspect
) {
713 /* me and then parent */
714 dup_entry(e
, &split
[0]);
716 new_entry
= xmalloc(sizeof(*new_entry
));
717 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
718 add_blame_entry(sb
, new_entry
);
721 /* parent and then me */
722 dup_entry(e
, &split
[1]);
724 new_entry
= xmalloc(sizeof(*new_entry
));
725 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
726 add_blame_entry(sb
, new_entry
);
729 if (DEBUG
) { /* sanity */
730 struct blame_entry
*ent
;
731 int lno
= sb
->ent
->lno
, corrupt
= 0;
733 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
738 lno
+= ent
->num_lines
;
742 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
743 printf("L %8d l %8d n %8d\n",
744 lno
, ent
->lno
, ent
->num_lines
);
745 lno
= ent
->lno
+ ent
->num_lines
;
753 * After splitting the blame, the origins used by the
754 * on-stack blame_entry should lose one refcnt each.
756 static void decref_split(struct blame_entry
*split
)
760 for (i
= 0; i
< 3; i
++)
761 origin_decref(split
[i
].suspect
);
765 * Helper for blame_chunk(). blame_entry e is known to overlap with
766 * the patch hunk; split it and pass blame to the parent.
768 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
769 int tlno
, int plno
, int same
,
770 struct origin
*parent
)
772 struct blame_entry split
[3];
774 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
775 if (split
[1].suspect
)
776 split_blame(sb
, split
, e
);
781 * Find the line number of the last line the target is suspected for.
783 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
785 struct blame_entry
*e
;
786 int last_in_target
= -1;
788 for (e
= sb
->ent
; e
; e
= e
->next
) {
789 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
791 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
792 last_in_target
= e
->s_lno
+ e
->num_lines
;
794 return last_in_target
;
798 * Process one hunk from the patch between the current suspect for
799 * blame_entry e and its parent. Find and split the overlap, and
800 * pass blame to the overlapping part to the parent.
802 static void blame_chunk(struct scoreboard
*sb
,
803 int tlno
, int plno
, int same
,
804 struct origin
*target
, struct origin
*parent
)
806 struct blame_entry
*e
;
808 for (e
= sb
->ent
; e
; e
= e
->next
) {
809 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
811 if (same
<= e
->s_lno
)
813 if (tlno
< e
->s_lno
+ e
->num_lines
)
814 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
819 * We are looking at the origin 'target' and aiming to pass blame
820 * for the lines it is suspected to its parent. Run diff to find
821 * which lines came from parent and pass blame for them.
823 static int pass_blame_to_parent(struct scoreboard
*sb
,
824 struct origin
*target
,
825 struct origin
*parent
)
827 int i
, last_in_target
, plno
, tlno
;
830 last_in_target
= find_last_in_target(sb
, target
);
831 if (last_in_target
< 0)
832 return 1; /* nothing remains for this target */
834 patch
= get_patch(parent
, target
);
836 for (i
= 0; i
< patch
->num
; i
++) {
837 struct chunk
*chunk
= &patch
->chunks
[i
];
839 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
840 plno
= chunk
->p_next
;
841 tlno
= chunk
->t_next
;
843 /* The rest (i.e. anything after tlno) are the same as the parent */
844 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
851 * The lines in blame_entry after splitting blames many times can become
852 * very small and trivial, and at some point it becomes pointless to
853 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
854 * ordinary C program, and it is not worth to say it was copied from
855 * totally unrelated file in the parent.
857 * Compute how trivial the lines in the blame_entry are.
859 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
868 cp
= nth_line(sb
, e
->lno
);
869 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
871 unsigned ch
= *((unsigned char *)cp
);
881 * best_so_far[] and this[] are both a split of an existing blame_entry
882 * that passes blame to the parent. Maintain best_so_far the best split
883 * so far, by comparing this and best_so_far and copying this into
884 * bst_so_far as needed.
886 static void copy_split_if_better(struct scoreboard
*sb
,
887 struct blame_entry
*best_so_far
,
888 struct blame_entry
*this)
892 if (!this[1].suspect
)
894 if (best_so_far
[1].suspect
) {
895 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
899 for (i
= 0; i
< 3; i
++)
900 origin_incref(this[i
].suspect
);
901 decref_split(best_so_far
);
902 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
906 * We are looking at a part of the final image represented by
907 * ent (tlno and same are offset by ent->s_lno).
908 * tlno is where we are looking at in the final image.
909 * up to (but not including) same match preimage.
910 * plno is where we are looking at in the preimage.
912 * <-------------- final image ---------------------->
915 * <---------preimage----->
918 * All line numbers are 0-based.
920 static void handle_split(struct scoreboard
*sb
,
921 struct blame_entry
*ent
,
922 int tlno
, int plno
, int same
,
923 struct origin
*parent
,
924 struct blame_entry
*split
)
926 if (ent
->num_lines
<= tlno
)
929 struct blame_entry
this[3];
932 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
933 copy_split_if_better(sb
, split
, this);
939 * Find the lines from parent that are the same as ent so that
940 * we can pass blames to it. file_p has the blob contents for
943 static void find_copy_in_blob(struct scoreboard
*sb
,
944 struct blame_entry
*ent
,
945 struct origin
*parent
,
946 struct blame_entry
*split
,
956 * Prepare mmfile that contains only the lines in ent.
958 cp
= nth_line(sb
, ent
->lno
);
959 file_o
.ptr
= (char*) cp
;
960 cnt
= ent
->num_lines
;
962 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
966 file_o
.size
= cp
- file_o
.ptr
;
968 patch
= compare_buffer(file_p
, &file_o
, 1);
971 * file_o is a part of final image we are annotating.
972 * file_p partially may match that image.
974 memset(split
, 0, sizeof(struct blame_entry
[3]));
976 for (i
= 0; i
< patch
->num
; i
++) {
977 struct chunk
*chunk
= &patch
->chunks
[i
];
979 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
980 plno
= chunk
->p_next
;
981 tlno
= chunk
->t_next
;
983 /* remainder, if any, all match the preimage */
984 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
989 * See if lines currently target is suspected for can be attributed to
992 static int find_move_in_parent(struct scoreboard
*sb
,
993 struct origin
*target
,
994 struct origin
*parent
)
996 int last_in_target
, made_progress
;
997 struct blame_entry
*e
, split
[3];
1000 last_in_target
= find_last_in_target(sb
, target
);
1001 if (last_in_target
< 0)
1002 return 1; /* nothing remains for this target */
1004 fill_origin_blob(parent
, &file_p
);
1009 while (made_progress
) {
1011 for (e
= sb
->ent
; e
; e
= e
->next
) {
1012 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1014 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1015 if (split
[1].suspect
&&
1016 blame_move_score
< ent_score(sb
, &split
[1])) {
1017 split_blame(sb
, split
, e
);
1020 decref_split(split
);
1027 struct blame_entry
*ent
;
1028 struct blame_entry split
[3];
1032 * Count the number of entries the target is suspected for,
1033 * and prepare a list of entry and the best split.
1035 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1036 struct origin
*target
,
1039 struct blame_entry
*e
;
1041 struct blame_list
*blame_list
= NULL
;
1043 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1044 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1047 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1048 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1049 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1050 blame_list
[i
++].ent
= e
;
1052 *num_ents_p
= num_ents
;
1057 * For lines target is suspected for, see if we can find code movement
1058 * across file boundary from the parent commit. porigin is the path
1059 * in the parent we already tried.
1061 static int find_copy_in_parent(struct scoreboard
*sb
,
1062 struct origin
*target
,
1063 struct commit
*parent
,
1064 struct origin
*porigin
,
1067 struct diff_options diff_opts
;
1068 const char *paths
[1];
1071 struct blame_list
*blame_list
;
1074 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1076 return 1; /* nothing remains for this target */
1078 diff_setup(&diff_opts
);
1079 diff_opts
.recursive
= 1;
1080 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1083 diff_tree_setup_paths(paths
, &diff_opts
);
1084 if (diff_setup_done(&diff_opts
) < 0)
1087 /* Try "find copies harder" on new path if requested;
1088 * we do not want to use diffcore_rename() actually to
1089 * match things up; find_copies_harder is set only to
1090 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1091 * and this code needs to be after diff_setup_done(), which
1092 * usually makes find-copies-harder imply copy detection.
1094 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1095 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1096 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1097 diff_opts
.find_copies_harder
= 1;
1099 if (is_null_sha1(target
->commit
->object
.sha1
))
1100 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1102 diff_tree_sha1(parent
->tree
->object
.sha1
,
1103 target
->commit
->tree
->object
.sha1
,
1106 if (!diff_opts
.find_copies_harder
)
1107 diffcore_std(&diff_opts
);
1111 int made_progress
= 0;
1113 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1114 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1115 struct origin
*norigin
;
1117 struct blame_entry
this[3];
1119 if (!DIFF_FILE_VALID(p
->one
))
1120 continue; /* does not exist in parent */
1121 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1122 /* find_move already dealt with this path */
1125 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1126 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1127 fill_origin_blob(norigin
, &file_p
);
1131 for (j
= 0; j
< num_ents
; j
++) {
1132 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1133 norigin
, this, &file_p
);
1134 copy_split_if_better(sb
, blame_list
[j
].split
,
1138 origin_decref(norigin
);
1141 for (j
= 0; j
< num_ents
; j
++) {
1142 struct blame_entry
*split
= blame_list
[j
].split
;
1143 if (split
[1].suspect
&&
1144 blame_copy_score
< ent_score(sb
, &split
[1])) {
1145 split_blame(sb
, split
, blame_list
[j
].ent
);
1148 decref_split(split
);
1154 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1160 diff_flush(&diff_opts
);
1166 * The blobs of origin and porigin exactly match, so everything
1167 * origin is suspected for can be blamed on the parent.
1169 static void pass_whole_blame(struct scoreboard
*sb
,
1170 struct origin
*origin
, struct origin
*porigin
)
1172 struct blame_entry
*e
;
1174 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1175 /* Steal its file */
1176 porigin
->file
= origin
->file
;
1177 origin
->file
.ptr
= NULL
;
1179 for (e
= sb
->ent
; e
; e
= e
->next
) {
1180 if (!same_suspect(e
->suspect
, origin
))
1182 origin_incref(porigin
);
1183 origin_decref(e
->suspect
);
1184 e
->suspect
= porigin
;
1188 #define MAXPARENT 16
1190 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1193 struct commit
*commit
= origin
->commit
;
1194 struct commit_list
*parent
;
1195 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1197 memset(parent_origin
, 0, sizeof(parent_origin
));
1199 /* The first pass looks for unrenamed path to optimize for
1200 * common cases, then we look for renames in the second pass.
1202 for (pass
= 0; pass
< 2; pass
++) {
1203 struct origin
*(*find
)(struct scoreboard
*,
1204 struct commit
*, struct origin
*);
1205 find
= pass
? find_rename
: find_origin
;
1207 for (i
= 0, parent
= commit
->parents
;
1208 i
< MAXPARENT
&& parent
;
1209 parent
= parent
->next
, i
++) {
1210 struct commit
*p
= parent
->item
;
1213 if (parent_origin
[i
])
1215 if (parse_commit(p
))
1217 porigin
= find(sb
, p
, origin
);
1220 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1221 pass_whole_blame(sb
, origin
, porigin
);
1222 origin_decref(porigin
);
1225 for (j
= same
= 0; j
< i
; j
++)
1226 if (parent_origin
[j
] &&
1227 !hashcmp(parent_origin
[j
]->blob_sha1
,
1228 porigin
->blob_sha1
)) {
1233 parent_origin
[i
] = porigin
;
1235 origin_decref(porigin
);
1240 for (i
= 0, parent
= commit
->parents
;
1241 i
< MAXPARENT
&& parent
;
1242 parent
= parent
->next
, i
++) {
1243 struct origin
*porigin
= parent_origin
[i
];
1246 if (pass_blame_to_parent(sb
, origin
, porigin
))
1251 * Optionally find moves in parents' files.
1253 if (opt
& PICKAXE_BLAME_MOVE
)
1254 for (i
= 0, parent
= commit
->parents
;
1255 i
< MAXPARENT
&& parent
;
1256 parent
= parent
->next
, i
++) {
1257 struct origin
*porigin
= parent_origin
[i
];
1260 if (find_move_in_parent(sb
, origin
, porigin
))
1265 * Optionally find copies from parents' files.
1267 if (opt
& PICKAXE_BLAME_COPY
)
1268 for (i
= 0, parent
= commit
->parents
;
1269 i
< MAXPARENT
&& parent
;
1270 parent
= parent
->next
, i
++) {
1271 struct origin
*porigin
= parent_origin
[i
];
1272 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1278 for (i
= 0; i
< MAXPARENT
; i
++)
1279 origin_decref(parent_origin
[i
]);
1283 * Information on commits, used for output.
1288 const char *author_mail
;
1289 unsigned long author_time
;
1290 const char *author_tz
;
1292 /* filled only when asked for details */
1293 const char *committer
;
1294 const char *committer_mail
;
1295 unsigned long committer_time
;
1296 const char *committer_tz
;
1298 const char *summary
;
1302 * Parse author/committer line in the commit object buffer
1304 static void get_ac_line(const char *inbuf
, const char *what
,
1305 int bufsz
, char *person
, const char **mail
,
1306 unsigned long *time
, const char **tz
)
1308 int len
, tzlen
, maillen
;
1309 char *tmp
, *endp
, *timepos
;
1311 tmp
= strstr(inbuf
, what
);
1314 tmp
+= strlen(what
);
1315 endp
= strchr(tmp
, '\n');
1323 *mail
= *tz
= "(unknown)";
1327 memcpy(person
, tmp
, len
);
1335 tzlen
= (person
+len
)-(tmp
+1);
1340 *time
= strtoul(tmp
, NULL
, 10);
1348 maillen
= timepos
- tmp
;
1354 * mailmap expansion may make the name longer.
1355 * make room by pushing stuff down.
1357 tmp
= person
+ bufsz
- (tzlen
+ 1);
1358 memmove(tmp
, *tz
, tzlen
);
1362 tmp
= tmp
- (maillen
+ 1);
1363 memmove(tmp
, *mail
, maillen
);
1368 * Now, convert e-mail using mailmap
1370 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1373 static void get_commit_info(struct commit
*commit
,
1374 struct commit_info
*ret
,
1379 static char author_buf
[1024];
1380 static char committer_buf
[1024];
1381 static char summary_buf
[1024];
1384 * We've operated without save_commit_buffer, so
1385 * we now need to populate them for output.
1387 if (!commit
->buffer
) {
1388 enum object_type type
;
1391 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1392 if (!commit
->buffer
)
1393 die("Cannot read commit %s",
1394 sha1_to_hex(commit
->object
.sha1
));
1396 ret
->author
= author_buf
;
1397 get_ac_line(commit
->buffer
, "\nauthor ",
1398 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1399 &ret
->author_time
, &ret
->author_tz
);
1404 ret
->committer
= committer_buf
;
1405 get_ac_line(commit
->buffer
, "\ncommitter ",
1406 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1407 &ret
->committer_time
, &ret
->committer_tz
);
1409 ret
->summary
= summary_buf
;
1410 tmp
= strstr(commit
->buffer
, "\n\n");
1413 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1417 endp
= strchr(tmp
, '\n');
1419 endp
= tmp
+ strlen(tmp
);
1421 if (len
>= sizeof(summary_buf
) || len
== 0)
1423 memcpy(summary_buf
, tmp
, len
);
1424 summary_buf
[len
] = 0;
1428 * To allow LF and other nonportable characters in pathnames,
1429 * they are c-style quoted as needed.
1431 static void write_filename_info(const char *path
)
1433 printf("filename ");
1434 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1439 * The blame_entry is found to be guilty for the range. Mark it
1440 * as such, and show it in incremental output.
1442 static void found_guilty_entry(struct blame_entry
*ent
)
1448 struct origin
*suspect
= ent
->suspect
;
1450 printf("%s %d %d %d\n",
1451 sha1_to_hex(suspect
->commit
->object
.sha1
),
1452 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1453 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1454 struct commit_info ci
;
1455 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1456 get_commit_info(suspect
->commit
, &ci
, 1);
1457 printf("author %s\n", ci
.author
);
1458 printf("author-mail %s\n", ci
.author_mail
);
1459 printf("author-time %lu\n", ci
.author_time
);
1460 printf("author-tz %s\n", ci
.author_tz
);
1461 printf("committer %s\n", ci
.committer
);
1462 printf("committer-mail %s\n", ci
.committer_mail
);
1463 printf("committer-time %lu\n", ci
.committer_time
);
1464 printf("committer-tz %s\n", ci
.committer_tz
);
1465 printf("summary %s\n", ci
.summary
);
1466 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1467 printf("boundary\n");
1469 write_filename_info(suspect
->path
);
1470 maybe_flush_or_die(stdout
, "stdout");
1475 * The main loop -- while the scoreboard has lines whose true origin
1476 * is still unknown, pick one blame_entry, and allow its current
1477 * suspect to pass blames to its parents.
1479 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1482 struct blame_entry
*ent
;
1483 struct commit
*commit
;
1484 struct origin
*suspect
= NULL
;
1486 /* find one suspect to break down */
1487 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1489 suspect
= ent
->suspect
;
1491 return; /* all done */
1494 * We will use this suspect later in the loop,
1495 * so hold onto it in the meantime.
1497 origin_incref(suspect
);
1498 commit
= suspect
->commit
;
1499 if (!commit
->object
.parsed
)
1500 parse_commit(commit
);
1501 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1502 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1503 pass_blame(sb
, suspect
, opt
);
1505 commit
->object
.flags
|= UNINTERESTING
;
1506 if (commit
->object
.parsed
)
1507 mark_parents_uninteresting(commit
);
1509 /* treat root commit as boundary */
1510 if (!commit
->parents
&& !show_root
)
1511 commit
->object
.flags
|= UNINTERESTING
;
1513 /* Take responsibility for the remaining entries */
1514 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1515 if (same_suspect(ent
->suspect
, suspect
))
1516 found_guilty_entry(ent
);
1517 origin_decref(suspect
);
1519 if (DEBUG
) /* sanity */
1520 sanity_check_refcnt(sb
);
1524 static const char *format_time(unsigned long time
, const char *tz_str
,
1527 static char time_buf
[128];
1532 if (show_raw_time
) {
1533 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1538 minutes
= tz
< 0 ? -tz
: tz
;
1539 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1540 minutes
= tz
< 0 ? -minutes
: minutes
;
1541 t
= time
+ minutes
* 60;
1544 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1545 strcat(time_buf
, tz_str
);
1549 #define OUTPUT_ANNOTATE_COMPAT 001
1550 #define OUTPUT_LONG_OBJECT_NAME 002
1551 #define OUTPUT_RAW_TIMESTAMP 004
1552 #define OUTPUT_PORCELAIN 010
1553 #define OUTPUT_SHOW_NAME 020
1554 #define OUTPUT_SHOW_NUMBER 040
1555 #define OUTPUT_SHOW_SCORE 0100
1556 #define OUTPUT_NO_AUTHOR 0200
1558 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1562 struct origin
*suspect
= ent
->suspect
;
1565 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1566 printf("%s%c%d %d %d\n",
1568 ent
->guilty
? ' ' : '*', // purely for debugging
1572 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1573 struct commit_info ci
;
1574 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1575 get_commit_info(suspect
->commit
, &ci
, 1);
1576 printf("author %s\n", ci
.author
);
1577 printf("author-mail %s\n", ci
.author_mail
);
1578 printf("author-time %lu\n", ci
.author_time
);
1579 printf("author-tz %s\n", ci
.author_tz
);
1580 printf("committer %s\n", ci
.committer
);
1581 printf("committer-mail %s\n", ci
.committer_mail
);
1582 printf("committer-time %lu\n", ci
.committer_time
);
1583 printf("committer-tz %s\n", ci
.committer_tz
);
1584 write_filename_info(suspect
->path
);
1585 printf("summary %s\n", ci
.summary
);
1586 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1587 printf("boundary\n");
1589 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1590 write_filename_info(suspect
->path
);
1592 cp
= nth_line(sb
, ent
->lno
);
1593 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1596 printf("%s %d %d\n", hex
,
1597 ent
->s_lno
+ 1 + cnt
,
1598 ent
->lno
+ 1 + cnt
);
1603 } while (ch
!= '\n' &&
1604 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1608 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1612 struct origin
*suspect
= ent
->suspect
;
1613 struct commit_info ci
;
1615 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1617 get_commit_info(suspect
->commit
, &ci
, 1);
1618 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1620 cp
= nth_line(sb
, ent
->lno
);
1621 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1623 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1625 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1627 memset(hex
, ' ', length
);
1628 else if (!cmd_is_annotate
) {
1634 printf("%.*s", length
, hex
);
1635 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1636 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1637 format_time(ci
.author_time
, ci
.author_tz
,
1639 ent
->lno
+ 1 + cnt
);
1641 if (opt
& OUTPUT_SHOW_SCORE
)
1643 max_score_digits
, ent
->score
,
1644 ent
->suspect
->refcnt
);
1645 if (opt
& OUTPUT_SHOW_NAME
)
1646 printf(" %-*.*s", longest_file
, longest_file
,
1648 if (opt
& OUTPUT_SHOW_NUMBER
)
1649 printf(" %*d", max_orig_digits
,
1650 ent
->s_lno
+ 1 + cnt
);
1652 if (!(opt
& OUTPUT_NO_AUTHOR
))
1653 printf(" (%-*.*s %10s",
1654 longest_author
, longest_author
,
1656 format_time(ci
.author_time
,
1660 max_digits
, ent
->lno
+ 1 + cnt
);
1665 } while (ch
!= '\n' &&
1666 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1670 static void output(struct scoreboard
*sb
, int option
)
1672 struct blame_entry
*ent
;
1674 if (option
& OUTPUT_PORCELAIN
) {
1675 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1676 struct blame_entry
*oth
;
1677 struct origin
*suspect
= ent
->suspect
;
1678 struct commit
*commit
= suspect
->commit
;
1679 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1681 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1682 if ((oth
->suspect
->commit
!= commit
) ||
1683 !strcmp(oth
->suspect
->path
, suspect
->path
))
1685 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1691 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1692 if (option
& OUTPUT_PORCELAIN
)
1693 emit_porcelain(sb
, ent
);
1695 emit_other(sb
, ent
, option
);
1701 * To allow quick access to the contents of nth line in the
1702 * final image, prepare an index in the scoreboard.
1704 static int prepare_lines(struct scoreboard
*sb
)
1706 const char *buf
= sb
->final_buf
;
1707 unsigned long len
= sb
->final_buf_size
;
1708 int num
= 0, incomplete
= 0, bol
= 1;
1710 if (len
&& buf
[len
-1] != '\n')
1711 incomplete
++; /* incomplete line at the end */
1714 sb
->lineno
= xrealloc(sb
->lineno
,
1715 sizeof(int* ) * (num
+ 1));
1716 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1719 if (*buf
++ == '\n') {
1724 sb
->lineno
= xrealloc(sb
->lineno
,
1725 sizeof(int* ) * (num
+ incomplete
+ 1));
1726 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1727 sb
->num_lines
= num
+ incomplete
;
1728 return sb
->num_lines
;
1732 * Add phony grafts for use with -S; this is primarily to
1733 * support git-cvsserver that wants to give a linear history
1736 static int read_ancestry(const char *graft_file
)
1738 FILE *fp
= fopen(graft_file
, "r");
1742 while (fgets(buf
, sizeof(buf
), fp
)) {
1743 /* The format is just "Commit Parent1 Parent2 ...\n" */
1744 int len
= strlen(buf
);
1745 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1747 register_commit_graft(graft
, 0);
1754 * How many columns do we need to show line numbers in decimal?
1756 static int lineno_width(int lines
)
1760 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1766 * How many columns do we need to show line numbers, authors,
1769 static void find_alignment(struct scoreboard
*sb
, int *option
)
1771 int longest_src_lines
= 0;
1772 int longest_dst_lines
= 0;
1773 unsigned largest_score
= 0;
1774 struct blame_entry
*e
;
1776 for (e
= sb
->ent
; e
; e
= e
->next
) {
1777 struct origin
*suspect
= e
->suspect
;
1778 struct commit_info ci
;
1781 if (strcmp(suspect
->path
, sb
->path
))
1782 *option
|= OUTPUT_SHOW_NAME
;
1783 num
= strlen(suspect
->path
);
1784 if (longest_file
< num
)
1786 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1787 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1788 get_commit_info(suspect
->commit
, &ci
, 1);
1789 num
= strlen(ci
.author
);
1790 if (longest_author
< num
)
1791 longest_author
= num
;
1793 num
= e
->s_lno
+ e
->num_lines
;
1794 if (longest_src_lines
< num
)
1795 longest_src_lines
= num
;
1796 num
= e
->lno
+ e
->num_lines
;
1797 if (longest_dst_lines
< num
)
1798 longest_dst_lines
= num
;
1799 if (largest_score
< ent_score(sb
, e
))
1800 largest_score
= ent_score(sb
, e
);
1802 max_orig_digits
= lineno_width(longest_src_lines
);
1803 max_digits
= lineno_width(longest_dst_lines
);
1804 max_score_digits
= lineno_width(largest_score
);
1808 * For debugging -- origin is refcounted, and this asserts that
1809 * we do not underflow.
1811 static void sanity_check_refcnt(struct scoreboard
*sb
)
1814 struct blame_entry
*ent
;
1816 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1817 /* Nobody should have zero or negative refcnt */
1818 if (ent
->suspect
->refcnt
<= 0) {
1819 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1821 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1822 ent
->suspect
->refcnt
);
1826 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1827 /* Mark the ones that haven't been checked */
1828 if (0 < ent
->suspect
->refcnt
)
1829 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1831 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1833 * ... then pick each and see if they have the the
1837 struct blame_entry
*e
;
1838 struct origin
*suspect
= ent
->suspect
;
1840 if (0 < suspect
->refcnt
)
1842 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1843 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1844 if (e
->suspect
!= suspect
)
1848 if (suspect
->refcnt
!= found
) {
1849 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1851 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1852 ent
->suspect
->refcnt
, found
);
1858 find_alignment(sb
, &opt
);
1860 die("Baa %d!", baa
);
1865 * Used for the command line parsing; check if the path exists
1866 * in the working tree.
1868 static int has_path_in_work_tree(const char *path
)
1871 return !lstat(path
, &st
);
1874 static unsigned parse_score(const char *arg
)
1877 unsigned long score
= strtoul(arg
, &end
, 10);
1883 static const char *add_prefix(const char *prefix
, const char *path
)
1885 if (!prefix
|| !prefix
[0])
1887 return prefix_path(prefix
, strlen(prefix
), path
);
1891 * Parsing of (comma separated) one item in the -L option
1893 static const char *parse_loc(const char *spec
,
1894 struct scoreboard
*sb
, long lno
,
1895 long begin
, long *ret
)
1902 regmatch_t match
[1];
1904 /* Allow "-L <something>,+20" to mean starting at <something>
1905 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1908 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1909 num
= strtol(spec
+ 1, &term
, 10);
1910 if (term
!= spec
+ 1) {
1914 *ret
= begin
+ num
- 2;
1923 num
= strtol(spec
, &term
, 10);
1931 /* it could be a regexp of form /.../ */
1932 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1939 /* try [spec+1 .. term-1] as regexp */
1941 begin
--; /* input is in human terms */
1942 line
= nth_line(sb
, begin
);
1944 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1945 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1946 const char *cp
= line
+ match
[0].rm_so
;
1949 while (begin
++ < lno
) {
1950 nline
= nth_line(sb
, begin
);
1951 if (line
<= cp
&& cp
< nline
)
1962 regerror(reg_error
, ®exp
, errbuf
, 1024);
1963 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1968 * Parsing of -L option
1970 static void prepare_blame_range(struct scoreboard
*sb
,
1971 const char *bottomtop
,
1973 long *bottom
, long *top
)
1977 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1979 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1987 static int git_blame_config(const char *var
, const char *value
)
1989 if (!strcmp(var
, "blame.showroot")) {
1990 show_root
= git_config_bool(var
, value
);
1993 if (!strcmp(var
, "blame.blankboundary")) {
1994 blank_boundary
= git_config_bool(var
, value
);
1997 return git_default_config(var
, value
);
2000 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2002 struct commit
*commit
;
2003 struct origin
*origin
;
2004 unsigned char head_sha1
[20];
2010 struct cache_entry
*ce
;
2013 if (get_sha1("HEAD", head_sha1
))
2014 die("No such ref: HEAD");
2017 commit
= xcalloc(1, sizeof(*commit
));
2018 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2019 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2020 commit
->object
.parsed
= 1;
2022 commit
->object
.type
= OBJ_COMMIT
;
2024 origin
= make_origin(commit
, path
);
2026 strbuf_init(&buf
, 0);
2027 if (!contents_from
|| strcmp("-", contents_from
)) {
2029 const char *read_from
;
2030 unsigned long fin_size
;
2032 if (contents_from
) {
2033 if (stat(contents_from
, &st
) < 0)
2034 die("Cannot stat %s", contents_from
);
2035 read_from
= contents_from
;
2038 if (lstat(path
, &st
) < 0)
2039 die("Cannot lstat %s", path
);
2042 fin_size
= xsize_t(st
.st_size
);
2043 mode
= canon_mode(st
.st_mode
);
2044 switch (st
.st_mode
& S_IFMT
) {
2046 fd
= open(read_from
, O_RDONLY
);
2048 die("cannot open %s", read_from
);
2049 if (strbuf_read(&buf
, fd
, 0) != xsize_t(st
.st_size
))
2050 die("cannot read %s", read_from
);
2053 if (readlink(read_from
, buf
.buf
, buf
.alloc
) != fin_size
)
2054 die("cannot readlink %s", read_from
);
2058 die("unsupported file type %s", read_from
);
2062 /* Reading from stdin */
2063 contents_from
= "standard input";
2065 if (strbuf_read(&buf
, 0, 0) < 0)
2066 die("read error %s from stdin", strerror(errno
));
2068 origin
->file
.ptr
= buf
.buf
;
2069 origin
->file
.size
= buf
.len
;
2070 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2071 commit
->util
= origin
;
2074 * Read the current index, replace the path entry with
2075 * origin->blob_sha1 without mucking with its mode or type
2076 * bits; we are not going to write this index out -- we just
2077 * want to run "diff-index --cached".
2084 int pos
= cache_name_pos(path
, len
);
2086 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2088 /* Let's not bother reading from HEAD tree */
2089 mode
= S_IFREG
| 0644;
2091 size
= cache_entry_size(len
);
2092 ce
= xcalloc(1, size
);
2093 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2094 memcpy(ce
->name
, path
, len
);
2095 ce
->ce_flags
= create_ce_flags(len
, 0);
2096 ce
->ce_mode
= create_ce_mode(mode
);
2097 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2100 * We are not going to write this out, so this does not matter
2101 * right now, but someday we might optimize diff-index --cached
2102 * with cache-tree information.
2104 cache_tree_invalidate_path(active_cache_tree
, path
);
2106 commit
->buffer
= xmalloc(400);
2107 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2108 snprintf(commit
->buffer
, 400,
2109 "tree 0000000000000000000000000000000000000000\n"
2113 "Version of %s from %s\n",
2114 sha1_to_hex(head_sha1
),
2115 ident
, ident
, path
, contents_from
? contents_from
: path
);
2119 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2121 struct rev_info revs
;
2123 struct scoreboard sb
;
2125 struct blame_entry
*ent
;
2126 int i
, seen_dashdash
, unk
, opt
;
2127 long bottom
, top
, lno
;
2128 int output_option
= 0;
2130 const char *revs_file
= NULL
;
2131 const char *final_commit_name
= NULL
;
2132 enum object_type type
;
2133 const char *bottomtop
= NULL
;
2134 const char *contents_from
= NULL
;
2136 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2138 git_config(git_blame_config
);
2139 save_commit_buffer
= 0;
2143 for (unk
= i
= 1; i
< argc
; i
++) {
2144 const char *arg
= argv
[i
];
2147 else if (!strcmp("-b", arg
))
2149 else if (!strcmp("--root", arg
))
2151 else if (!strcmp(arg
, "--show-stats"))
2153 else if (!strcmp("-c", arg
))
2154 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2155 else if (!strcmp("-t", arg
))
2156 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2157 else if (!strcmp("-l", arg
))
2158 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2159 else if (!strcmp("-s", arg
))
2160 output_option
|= OUTPUT_NO_AUTHOR
;
2161 else if (!strcmp("-w", arg
))
2162 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2163 else if (!strcmp("-S", arg
) && ++i
< argc
)
2164 revs_file
= argv
[i
];
2165 else if (!prefixcmp(arg
, "-M")) {
2166 opt
|= PICKAXE_BLAME_MOVE
;
2167 blame_move_score
= parse_score(arg
+2);
2169 else if (!prefixcmp(arg
, "-C")) {
2171 * -C enables copy from removed files;
2172 * -C -C enables copy from existing files, but only
2173 * when blaming a new file;
2174 * -C -C -C enables copy from existing files for
2177 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2178 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2179 if (opt
& PICKAXE_BLAME_COPY
)
2180 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2181 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2182 blame_copy_score
= parse_score(arg
+2);
2184 else if (!prefixcmp(arg
, "-L")) {
2193 die("More than one '-L n,m' option given");
2196 else if (!strcmp("--contents", arg
)) {
2199 contents_from
= argv
[i
];
2201 else if (!strcmp("--incremental", arg
))
2203 else if (!strcmp("--score-debug", arg
))
2204 output_option
|= OUTPUT_SHOW_SCORE
;
2205 else if (!strcmp("-f", arg
) ||
2206 !strcmp("--show-name", arg
))
2207 output_option
|= OUTPUT_SHOW_NAME
;
2208 else if (!strcmp("-n", arg
) ||
2209 !strcmp("--show-number", arg
))
2210 output_option
|= OUTPUT_SHOW_NUMBER
;
2211 else if (!strcmp("-p", arg
) ||
2212 !strcmp("--porcelain", arg
))
2213 output_option
|= OUTPUT_PORCELAIN
;
2214 else if (!strcmp("--", arg
)) {
2226 if (!blame_move_score
)
2227 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2228 if (!blame_copy_score
)
2229 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2232 * We have collected options unknown to us in argv[1..unk]
2233 * which are to be passed to revision machinery if we are
2234 * going to do the "bottom" processing.
2236 * The remaining are:
2238 * (1) if seen_dashdash, its either
2239 * "-options -- <path>" or
2240 * "-options -- <path> <rev>".
2241 * but the latter is allowed only if there is no
2242 * options that we passed to revision machinery.
2244 * (2) otherwise, we may have "--" somewhere later and
2245 * might be looking at the first one of multiple 'rev'
2246 * parameters (e.g. " master ^next ^maint -- path").
2247 * See if there is a dashdash first, and give the
2248 * arguments before that to revision machinery.
2249 * After that there must be one 'path'.
2251 * (3) otherwise, its one of the three:
2252 * "-options <path> <rev>"
2253 * "-options <rev> <path>"
2255 * but again the first one is allowed only if
2256 * there is no options that we passed to revision
2260 if (seen_dashdash
) {
2264 path
= add_prefix(prefix
, argv
[i
]);
2265 if (i
+ 1 == argc
- 1) {
2268 argv
[unk
++] = argv
[i
+ 1];
2270 else if (i
+ 1 != argc
)
2271 /* garbage at end */
2276 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2277 if (!strcmp(argv
[j
], "--"))
2279 if (seen_dashdash
) {
2281 if (seen_dashdash
+ 1 != argc
- 1)
2283 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2284 for (j
= i
; j
< seen_dashdash
; j
++)
2285 argv
[unk
++] = argv
[j
];
2291 path
= add_prefix(prefix
, argv
[i
]);
2292 if (i
+ 1 == argc
- 1) {
2293 final_commit_name
= argv
[i
+ 1];
2295 /* if (unk == 1) we could be getting
2298 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2299 path
= add_prefix(prefix
, argv
[i
+ 1]);
2300 final_commit_name
= argv
[i
];
2303 else if (i
!= argc
- 1)
2304 usage(blame_usage
); /* garbage at end */
2306 if (!has_path_in_work_tree(path
))
2307 die("cannot stat path %s: %s",
2308 path
, strerror(errno
));
2312 if (final_commit_name
)
2313 argv
[unk
++] = final_commit_name
;
2316 * Now we got rev and path. We do not want the path pruning
2317 * but we may want "bottom" processing.
2319 argv
[unk
++] = "--"; /* terminate the rev name */
2322 init_revisions(&revs
, NULL
);
2323 setup_revisions(unk
, argv
, &revs
, NULL
);
2324 memset(&sb
, 0, sizeof(sb
));
2327 * There must be one and only one positive commit in the
2328 * revs->pending array.
2330 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2331 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2332 if (obj
->flags
& UNINTERESTING
)
2334 while (obj
->type
== OBJ_TAG
)
2335 obj
= deref_tag(obj
, NULL
, 0);
2336 if (obj
->type
!= OBJ_COMMIT
)
2337 die("Non commit %s?",
2338 revs
.pending
.objects
[i
].name
);
2340 die("More than one commit to dig from %s and %s?",
2341 revs
.pending
.objects
[i
].name
,
2343 sb
.final
= (struct commit
*) obj
;
2344 final_commit_name
= revs
.pending
.objects
[i
].name
;
2349 * "--not A B -- path" without anything positive;
2350 * do not default to HEAD, but use the working tree
2353 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2354 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2356 else if (contents_from
)
2357 die("Cannot use --contents with final commit object name");
2360 * If we have bottom, this will mark the ancestors of the
2361 * bottom commits we would reach while traversing as
2364 prepare_revision_walk(&revs
);
2366 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2369 buf
= xmalloc(o
->file
.size
+ 1);
2370 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2372 sb
.final_buf_size
= o
->file
.size
;
2375 o
= get_origin(&sb
, sb
.final
, path
);
2376 if (fill_blob_sha1(o
))
2377 die("no such path %s in %s", path
, final_commit_name
);
2379 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2380 &sb
.final_buf_size
);
2382 die("Cannot read blob %s for path %s",
2383 sha1_to_hex(o
->blob_sha1
),
2387 lno
= prepare_lines(&sb
);
2391 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2392 if (bottom
&& top
&& top
< bottom
) {
2394 tmp
= top
; top
= bottom
; bottom
= tmp
;
2402 die("file %s has only %lu lines", path
, lno
);
2404 ent
= xcalloc(1, sizeof(*ent
));
2406 ent
->num_lines
= top
- bottom
;
2408 ent
->s_lno
= bottom
;
2413 if (revs_file
&& read_ancestry(revs_file
))
2414 die("reading graft file %s failed: %s",
2415 revs_file
, strerror(errno
));
2417 read_mailmap(&mailmap
, ".mailmap", NULL
);
2419 assign_blame(&sb
, &revs
, opt
);
2426 if (!(output_option
& OUTPUT_PORCELAIN
))
2427 find_alignment(&sb
, &output_option
);
2429 output(&sb
, output_option
);
2430 free((void *)sb
.final_buf
);
2431 for (ent
= sb
.ent
; ent
; ) {
2432 struct blame_entry
*e
= ent
->next
;
2438 printf("num read blob: %d\n", num_read_blob
);
2439 printf("num get patch: %d\n", num_get_patch
);
2440 printf("num commits: %d\n", num_commits
);