4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "path-list.h"
22 static char blame_usage
[] =
23 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
24 " -c Use the same output mode as git-annotate (Default: off)\n"
25 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
26 " -l Show long commit SHA1 (Default: off)\n"
27 " --root Do not treat root commits as boundaries (Default: off)\n"
28 " -t Show raw timestamp (Default: off)\n"
29 " -f, --show-name Show original filename (Default: auto)\n"
30 " -n, --show-number Show original linenumber (Default: off)\n"
31 " -s Suppress author name and timestamp (Default: off)\n"
32 " -p, --porcelain Show in a format designed for machine consumption\n"
33 " -w Ignore whitespace differences\n"
34 " -L n,m Process only line range n,m, counting from 1\n"
35 " -M, -C Find line movements within and across files\n"
36 " --incremental Show blame entries as we find them, incrementally\n"
37 " --contents file Use <file>'s contents as the final image\n"
38 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
40 static int longest_file
;
41 static int longest_author
;
42 static int max_orig_digits
;
43 static int max_digits
;
44 static int max_score_digits
;
46 static int blank_boundary
;
47 static int incremental
;
48 static int cmd_is_annotate
;
49 static int xdl_opts
= XDF_NEED_MINIMAL
;
50 static struct path_list mailmap
;
57 static int num_read_blob
;
58 static int num_get_patch
;
59 static int num_commits
;
61 #define PICKAXE_BLAME_MOVE 01
62 #define PICKAXE_BLAME_COPY 02
63 #define PICKAXE_BLAME_COPY_HARDER 04
64 #define PICKAXE_BLAME_COPY_HARDEST 010
67 * blame for a blame_entry with score lower than these thresholds
68 * is not passed to the parent using move/copy logic.
70 static unsigned blame_move_score
;
71 static unsigned blame_copy_score
;
72 #define BLAME_DEFAULT_MOVE_SCORE 20
73 #define BLAME_DEFAULT_COPY_SCORE 40
75 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76 #define METAINFO_SHOWN (1u<<12)
77 #define MORE_THAN_ONE_PATH (1u<<13)
80 * One blob in a commit that is being suspected
84 struct commit
*commit
;
86 unsigned char blob_sha1
[20];
87 char path
[FLEX_ARRAY
];
91 * Given an origin, prepare mmfile_t structure to be used by the
94 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
97 enum object_type type
;
99 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
100 (unsigned long *)(&(file
->size
)));
102 die("Cannot read blob %s for path %s",
103 sha1_to_hex(o
->blob_sha1
),
113 * Origin is refcounted and usually we keep the blob contents to be
116 static inline struct origin
*origin_incref(struct origin
*o
)
123 static void origin_decref(struct origin
*o
)
125 if (o
&& --o
->refcnt
<= 0) {
131 static void drop_origin_blob(struct origin
*o
)
140 * Each group of lines is described by a blame_entry; it can be split
141 * as we pass blame to the parents. They form a linked list in the
142 * scoreboard structure, sorted by the target line number.
145 struct blame_entry
*prev
;
146 struct blame_entry
*next
;
148 /* the first line of this group in the final image;
149 * internally all line numbers are 0 based.
153 /* how many lines this group has */
156 /* the commit that introduced this group into the final image */
157 struct origin
*suspect
;
159 /* true if the suspect is truly guilty; false while we have not
160 * checked if the group came from one of its parents.
164 /* the line number of the first line of this group in the
165 * suspect's file; internally all line numbers are 0 based.
169 /* how significant this entry is -- cached to avoid
170 * scanning the lines over and over.
176 * The current state of the blame assignment.
179 /* the final commit (i.e. where we started digging from) */
180 struct commit
*final
;
185 * The contents in the final image.
186 * Used by many functions to obtain contents of the nth line,
187 * indexed with scoreboard.lineno[blame_entry.lno].
189 const char *final_buf
;
190 unsigned long final_buf_size
;
192 /* linked list of blames */
193 struct blame_entry
*ent
;
195 /* look-up a line in the final buffer */
200 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
204 if (a
->commit
!= b
->commit
)
206 return !strcmp(a
->path
, b
->path
);
209 static void sanity_check_refcnt(struct scoreboard
*);
212 * If two blame entries that are next to each other came from
213 * contiguous lines in the same origin (i.e. <commit, path> pair),
214 * merge them together.
216 static void coalesce(struct scoreboard
*sb
)
218 struct blame_entry
*ent
, *next
;
220 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
221 if (same_suspect(ent
->suspect
, next
->suspect
) &&
222 ent
->guilty
== next
->guilty
&&
223 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
224 ent
->num_lines
+= next
->num_lines
;
225 ent
->next
= next
->next
;
227 ent
->next
->prev
= ent
;
228 origin_decref(next
->suspect
);
231 next
= ent
; /* again */
235 if (DEBUG
) /* sanity */
236 sanity_check_refcnt(sb
);
240 * Given a commit and a path in it, create a new origin structure.
241 * The callers that add blame to the scoreboard should use
242 * get_origin() to obtain shared, refcounted copy instead of calling
243 * this function directly.
245 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
248 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
251 strcpy(o
->path
, path
);
256 * Locate an existing origin or create a new one.
258 static struct origin
*get_origin(struct scoreboard
*sb
,
259 struct commit
*commit
,
262 struct blame_entry
*e
;
264 for (e
= sb
->ent
; e
; e
= e
->next
) {
265 if (e
->suspect
->commit
== commit
&&
266 !strcmp(e
->suspect
->path
, path
))
267 return origin_incref(e
->suspect
);
269 return make_origin(commit
, path
);
273 * Fill the blob_sha1 field of an origin if it hasn't, so that later
274 * call to fill_origin_blob() can use it to locate the data. blob_sha1
275 * for an origin is also used to pass the blame for the entire file to
276 * the parent to detect the case where a child's blob is identical to
277 * that of its parent's.
279 static int fill_blob_sha1(struct origin
*origin
)
283 if (!is_null_sha1(origin
->blob_sha1
))
285 if (get_tree_entry(origin
->commit
->object
.sha1
,
287 origin
->blob_sha1
, &mode
))
289 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
293 hashclr(origin
->blob_sha1
);
298 * We have an origin -- check if the same path exists in the
299 * parent and return an origin structure to represent it.
301 static struct origin
*find_origin(struct scoreboard
*sb
,
302 struct commit
*parent
,
303 struct origin
*origin
)
305 struct origin
*porigin
= NULL
;
306 struct diff_options diff_opts
;
307 const char *paths
[2];
311 * Each commit object can cache one origin in that
312 * commit. This is a freestanding copy of origin and
315 struct origin
*cached
= parent
->util
;
316 if (!strcmp(cached
->path
, origin
->path
)) {
318 * The same path between origin and its parent
319 * without renaming -- the most common case.
321 porigin
= get_origin(sb
, parent
, cached
->path
);
324 * If the origin was newly created (i.e. get_origin
325 * would call make_origin if none is found in the
326 * scoreboard), it does not know the blob_sha1,
327 * so copy it. Otherwise porigin was in the
328 * scoreboard and already knows blob_sha1.
330 if (porigin
->refcnt
== 1)
331 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
334 /* otherwise it was not very useful; free it */
339 /* See if the origin->path is different between parent
340 * and origin first. Most of the time they are the
341 * same and diff-tree is fairly efficient about this.
343 diff_setup(&diff_opts
);
344 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
345 diff_opts
.detect_rename
= 0;
346 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
347 paths
[0] = origin
->path
;
350 diff_tree_setup_paths(paths
, &diff_opts
);
351 if (diff_setup_done(&diff_opts
) < 0)
354 if (is_null_sha1(origin
->commit
->object
.sha1
))
355 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
357 diff_tree_sha1(parent
->tree
->object
.sha1
,
358 origin
->commit
->tree
->object
.sha1
,
360 diffcore_std(&diff_opts
);
362 /* It is either one entry that says "modified", or "created",
365 if (!diff_queued_diff
.nr
) {
366 /* The path is the same as parent */
367 porigin
= get_origin(sb
, parent
, origin
->path
);
368 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
370 else if (diff_queued_diff
.nr
!= 1)
371 die("internal error in blame::find_origin");
373 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
376 die("internal error in blame::find_origin (%c)",
379 porigin
= get_origin(sb
, parent
, origin
->path
);
380 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
384 /* Did not exist in parent, or type changed */
388 diff_flush(&diff_opts
);
389 diff_tree_release_paths(&diff_opts
);
392 * Create a freestanding copy that is not part of
393 * the refcounted origin found in the scoreboard, and
394 * cache it in the commit.
396 struct origin
*cached
;
398 cached
= make_origin(porigin
->commit
, porigin
->path
);
399 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
400 parent
->util
= cached
;
406 * We have an origin -- find the path that corresponds to it in its
407 * parent and return an origin structure to represent it.
409 static struct origin
*find_rename(struct scoreboard
*sb
,
410 struct commit
*parent
,
411 struct origin
*origin
)
413 struct origin
*porigin
= NULL
;
414 struct diff_options diff_opts
;
416 const char *paths
[2];
418 diff_setup(&diff_opts
);
419 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
420 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
421 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
422 diff_opts
.single_follow
= origin
->path
;
424 diff_tree_setup_paths(paths
, &diff_opts
);
425 if (diff_setup_done(&diff_opts
) < 0)
428 if (is_null_sha1(origin
->commit
->object
.sha1
))
429 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
431 diff_tree_sha1(parent
->tree
->object
.sha1
,
432 origin
->commit
->tree
->object
.sha1
,
434 diffcore_std(&diff_opts
);
436 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
437 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
438 if ((p
->status
== 'R' || p
->status
== 'C') &&
439 !strcmp(p
->two
->path
, origin
->path
)) {
440 porigin
= get_origin(sb
, parent
, p
->one
->path
);
441 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
445 diff_flush(&diff_opts
);
446 diff_tree_release_paths(&diff_opts
);
451 * Parsing of patch chunks...
454 /* line number in postimage; up to but not including this
455 * line is the same as preimage
459 /* preimage line number after this chunk */
462 /* postimage line number after this chunk */
467 struct chunk
*chunks
;
471 struct blame_diff_state
{
472 struct xdiff_emit_state xm
;
474 unsigned hunk_post_context
;
475 unsigned hunk_in_pre_context
: 1;
478 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
480 struct blame_diff_state
*state
= state_
;
482 int off1
, off2
, len1
, len2
, num
;
484 num
= state
->ret
->num
;
485 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
486 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
487 state
->ret
->chunks
[num
- 1].same
++;
489 state
->hunk_in_pre_context
= 0;
491 state
->hunk_post_context
++;
493 state
->hunk_post_context
= 0;
498 if (num
&& state
->hunk_post_context
) {
499 chunk
= &state
->ret
->chunks
[num
- 1];
500 chunk
->p_next
-= state
->hunk_post_context
;
501 chunk
->t_next
-= state
->hunk_post_context
;
503 state
->ret
->num
= ++num
;
504 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
505 sizeof(struct chunk
) * num
);
506 chunk
= &state
->ret
->chunks
[num
- 1];
507 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
512 /* Line numbers in patch output are one based. */
516 chunk
->same
= len2
? off2
: (off2
+ 1);
518 chunk
->p_next
= off1
+ (len1
? len1
: 1);
519 chunk
->t_next
= chunk
->same
+ len2
;
520 state
->hunk_in_pre_context
= 1;
521 state
->hunk_post_context
= 0;
524 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
527 struct blame_diff_state state
;
532 xpp
.flags
= xdl_opts
;
533 memset(&xecfg
, 0, sizeof(xecfg
));
534 xecfg
.ctxlen
= context
;
535 ecb
.outf
= xdiff_outf
;
537 memset(&state
, 0, sizeof(state
));
538 state
.xm
.consume
= process_u_diff
;
539 state
.ret
= xmalloc(sizeof(struct patch
));
540 state
.ret
->chunks
= NULL
;
543 xdi_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
545 if (state
.ret
->num
) {
547 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
548 chunk
->p_next
-= state
.hunk_post_context
;
549 chunk
->t_next
-= state
.hunk_post_context
;
555 * Run diff between two origins and grab the patch output, so that
556 * we can pass blame for lines origin is currently suspected for
559 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
561 mmfile_t file_p
, file_o
;
564 fill_origin_blob(parent
, &file_p
);
565 fill_origin_blob(origin
, &file_o
);
566 if (!file_p
.ptr
|| !file_o
.ptr
)
568 patch
= compare_buffer(&file_p
, &file_o
, 0);
573 static void free_patch(struct patch
*p
)
580 * Link in a new blame entry to the scoreboard. Entries that cover the
581 * same line range have been removed from the scoreboard previously.
583 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
585 struct blame_entry
*ent
, *prev
= NULL
;
587 origin_incref(e
->suspect
);
589 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
592 /* prev, if not NULL, is the last one that is below e */
595 e
->next
= prev
->next
;
607 * src typically is on-stack; we want to copy the information in it to
608 * a malloced blame_entry that is already on the linked list of the
609 * scoreboard. The origin of dst loses a refcnt while the origin of src
612 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
614 struct blame_entry
*p
, *n
;
618 origin_incref(src
->suspect
);
619 origin_decref(dst
->suspect
);
620 memcpy(dst
, src
, sizeof(*src
));
626 static const char *nth_line(struct scoreboard
*sb
, int lno
)
628 return sb
->final_buf
+ sb
->lineno
[lno
];
632 * It is known that lines between tlno to same came from parent, and e
633 * has an overlap with that range. it also is known that parent's
634 * line plno corresponds to e's line tlno.
640 * <------------------>
642 * Split e into potentially three parts; before this chunk, the chunk
643 * to be blamed for the parent, and after that portion.
645 static void split_overlap(struct blame_entry
*split
,
646 struct blame_entry
*e
,
647 int tlno
, int plno
, int same
,
648 struct origin
*parent
)
651 memset(split
, 0, sizeof(struct blame_entry
[3]));
653 if (e
->s_lno
< tlno
) {
654 /* there is a pre-chunk part not blamed on parent */
655 split
[0].suspect
= origin_incref(e
->suspect
);
656 split
[0].lno
= e
->lno
;
657 split
[0].s_lno
= e
->s_lno
;
658 split
[0].num_lines
= tlno
- e
->s_lno
;
659 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
660 split
[1].s_lno
= plno
;
663 split
[1].lno
= e
->lno
;
664 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
667 if (same
< e
->s_lno
+ e
->num_lines
) {
668 /* there is a post-chunk part not blamed on parent */
669 split
[2].suspect
= origin_incref(e
->suspect
);
670 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
671 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
672 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
673 chunk_end_lno
= split
[2].lno
;
676 chunk_end_lno
= e
->lno
+ e
->num_lines
;
677 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
680 * if it turns out there is nothing to blame the parent for,
681 * forget about the splitting. !split[1].suspect signals this.
683 if (split
[1].num_lines
< 1)
685 split
[1].suspect
= origin_incref(parent
);
689 * split_overlap() divided an existing blame e into up to three parts
690 * in split. Adjust the linked list of blames in the scoreboard to
693 static void split_blame(struct scoreboard
*sb
,
694 struct blame_entry
*split
,
695 struct blame_entry
*e
)
697 struct blame_entry
*new_entry
;
699 if (split
[0].suspect
&& split
[2].suspect
) {
700 /* The first part (reuse storage for the existing entry e) */
701 dup_entry(e
, &split
[0]);
703 /* The last part -- me */
704 new_entry
= xmalloc(sizeof(*new_entry
));
705 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
706 add_blame_entry(sb
, new_entry
);
708 /* ... and the middle part -- parent */
709 new_entry
= xmalloc(sizeof(*new_entry
));
710 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
711 add_blame_entry(sb
, new_entry
);
713 else if (!split
[0].suspect
&& !split
[2].suspect
)
715 * The parent covers the entire area; reuse storage for
716 * e and replace it with the parent.
718 dup_entry(e
, &split
[1]);
719 else if (split
[0].suspect
) {
720 /* me and then parent */
721 dup_entry(e
, &split
[0]);
723 new_entry
= xmalloc(sizeof(*new_entry
));
724 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
725 add_blame_entry(sb
, new_entry
);
728 /* parent and then me */
729 dup_entry(e
, &split
[1]);
731 new_entry
= xmalloc(sizeof(*new_entry
));
732 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
733 add_blame_entry(sb
, new_entry
);
736 if (DEBUG
) { /* sanity */
737 struct blame_entry
*ent
;
738 int lno
= sb
->ent
->lno
, corrupt
= 0;
740 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
745 lno
+= ent
->num_lines
;
749 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
750 printf("L %8d l %8d n %8d\n",
751 lno
, ent
->lno
, ent
->num_lines
);
752 lno
= ent
->lno
+ ent
->num_lines
;
760 * After splitting the blame, the origins used by the
761 * on-stack blame_entry should lose one refcnt each.
763 static void decref_split(struct blame_entry
*split
)
767 for (i
= 0; i
< 3; i
++)
768 origin_decref(split
[i
].suspect
);
772 * Helper for blame_chunk(). blame_entry e is known to overlap with
773 * the patch hunk; split it and pass blame to the parent.
775 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
776 int tlno
, int plno
, int same
,
777 struct origin
*parent
)
779 struct blame_entry split
[3];
781 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
782 if (split
[1].suspect
)
783 split_blame(sb
, split
, e
);
788 * Find the line number of the last line the target is suspected for.
790 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
792 struct blame_entry
*e
;
793 int last_in_target
= -1;
795 for (e
= sb
->ent
; e
; e
= e
->next
) {
796 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
798 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
799 last_in_target
= e
->s_lno
+ e
->num_lines
;
801 return last_in_target
;
805 * Process one hunk from the patch between the current suspect for
806 * blame_entry e and its parent. Find and split the overlap, and
807 * pass blame to the overlapping part to the parent.
809 static void blame_chunk(struct scoreboard
*sb
,
810 int tlno
, int plno
, int same
,
811 struct origin
*target
, struct origin
*parent
)
813 struct blame_entry
*e
;
815 for (e
= sb
->ent
; e
; e
= e
->next
) {
816 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
818 if (same
<= e
->s_lno
)
820 if (tlno
< e
->s_lno
+ e
->num_lines
)
821 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
826 * We are looking at the origin 'target' and aiming to pass blame
827 * for the lines it is suspected to its parent. Run diff to find
828 * which lines came from parent and pass blame for them.
830 static int pass_blame_to_parent(struct scoreboard
*sb
,
831 struct origin
*target
,
832 struct origin
*parent
)
834 int i
, last_in_target
, plno
, tlno
;
837 last_in_target
= find_last_in_target(sb
, target
);
838 if (last_in_target
< 0)
839 return 1; /* nothing remains for this target */
841 patch
= get_patch(parent
, target
);
843 for (i
= 0; i
< patch
->num
; i
++) {
844 struct chunk
*chunk
= &patch
->chunks
[i
];
846 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
847 plno
= chunk
->p_next
;
848 tlno
= chunk
->t_next
;
850 /* The rest (i.e. anything after tlno) are the same as the parent */
851 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
858 * The lines in blame_entry after splitting blames many times can become
859 * very small and trivial, and at some point it becomes pointless to
860 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
861 * ordinary C program, and it is not worth to say it was copied from
862 * totally unrelated file in the parent.
864 * Compute how trivial the lines in the blame_entry are.
866 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
875 cp
= nth_line(sb
, e
->lno
);
876 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
878 unsigned ch
= *((unsigned char *)cp
);
888 * best_so_far[] and this[] are both a split of an existing blame_entry
889 * that passes blame to the parent. Maintain best_so_far the best split
890 * so far, by comparing this and best_so_far and copying this into
891 * bst_so_far as needed.
893 static void copy_split_if_better(struct scoreboard
*sb
,
894 struct blame_entry
*best_so_far
,
895 struct blame_entry
*this)
899 if (!this[1].suspect
)
901 if (best_so_far
[1].suspect
) {
902 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
906 for (i
= 0; i
< 3; i
++)
907 origin_incref(this[i
].suspect
);
908 decref_split(best_so_far
);
909 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
913 * We are looking at a part of the final image represented by
914 * ent (tlno and same are offset by ent->s_lno).
915 * tlno is where we are looking at in the final image.
916 * up to (but not including) same match preimage.
917 * plno is where we are looking at in the preimage.
919 * <-------------- final image ---------------------->
922 * <---------preimage----->
925 * All line numbers are 0-based.
927 static void handle_split(struct scoreboard
*sb
,
928 struct blame_entry
*ent
,
929 int tlno
, int plno
, int same
,
930 struct origin
*parent
,
931 struct blame_entry
*split
)
933 if (ent
->num_lines
<= tlno
)
936 struct blame_entry
this[3];
939 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
940 copy_split_if_better(sb
, split
, this);
946 * Find the lines from parent that are the same as ent so that
947 * we can pass blames to it. file_p has the blob contents for
950 static void find_copy_in_blob(struct scoreboard
*sb
,
951 struct blame_entry
*ent
,
952 struct origin
*parent
,
953 struct blame_entry
*split
,
963 * Prepare mmfile that contains only the lines in ent.
965 cp
= nth_line(sb
, ent
->lno
);
966 file_o
.ptr
= (char*) cp
;
967 cnt
= ent
->num_lines
;
969 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
973 file_o
.size
= cp
- file_o
.ptr
;
975 patch
= compare_buffer(file_p
, &file_o
, 1);
978 * file_o is a part of final image we are annotating.
979 * file_p partially may match that image.
981 memset(split
, 0, sizeof(struct blame_entry
[3]));
983 for (i
= 0; i
< patch
->num
; i
++) {
984 struct chunk
*chunk
= &patch
->chunks
[i
];
986 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
987 plno
= chunk
->p_next
;
988 tlno
= chunk
->t_next
;
990 /* remainder, if any, all match the preimage */
991 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
996 * See if lines currently target is suspected for can be attributed to
999 static int find_move_in_parent(struct scoreboard
*sb
,
1000 struct origin
*target
,
1001 struct origin
*parent
)
1003 int last_in_target
, made_progress
;
1004 struct blame_entry
*e
, split
[3];
1007 last_in_target
= find_last_in_target(sb
, target
);
1008 if (last_in_target
< 0)
1009 return 1; /* nothing remains for this target */
1011 fill_origin_blob(parent
, &file_p
);
1016 while (made_progress
) {
1018 for (e
= sb
->ent
; e
; e
= e
->next
) {
1019 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1021 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1022 if (split
[1].suspect
&&
1023 blame_move_score
< ent_score(sb
, &split
[1])) {
1024 split_blame(sb
, split
, e
);
1027 decref_split(split
);
1034 struct blame_entry
*ent
;
1035 struct blame_entry split
[3];
1039 * Count the number of entries the target is suspected for,
1040 * and prepare a list of entry and the best split.
1042 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1043 struct origin
*target
,
1046 struct blame_entry
*e
;
1048 struct blame_list
*blame_list
= NULL
;
1050 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1051 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1054 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1055 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1056 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1057 blame_list
[i
++].ent
= e
;
1059 *num_ents_p
= num_ents
;
1064 * For lines target is suspected for, see if we can find code movement
1065 * across file boundary from the parent commit. porigin is the path
1066 * in the parent we already tried.
1068 static int find_copy_in_parent(struct scoreboard
*sb
,
1069 struct origin
*target
,
1070 struct commit
*parent
,
1071 struct origin
*porigin
,
1074 struct diff_options diff_opts
;
1075 const char *paths
[1];
1078 struct blame_list
*blame_list
;
1081 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1083 return 1; /* nothing remains for this target */
1085 diff_setup(&diff_opts
);
1086 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1087 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1090 diff_tree_setup_paths(paths
, &diff_opts
);
1091 if (diff_setup_done(&diff_opts
) < 0)
1094 /* Try "find copies harder" on new path if requested;
1095 * we do not want to use diffcore_rename() actually to
1096 * match things up; find_copies_harder is set only to
1097 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1098 * and this code needs to be after diff_setup_done(), which
1099 * usually makes find-copies-harder imply copy detection.
1101 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1102 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1103 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1104 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1106 if (is_null_sha1(target
->commit
->object
.sha1
))
1107 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1109 diff_tree_sha1(parent
->tree
->object
.sha1
,
1110 target
->commit
->tree
->object
.sha1
,
1113 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1114 diffcore_std(&diff_opts
);
1118 int made_progress
= 0;
1120 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1121 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1122 struct origin
*norigin
;
1124 struct blame_entry
this[3];
1126 if (!DIFF_FILE_VALID(p
->one
))
1127 continue; /* does not exist in parent */
1128 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1129 /* find_move already dealt with this path */
1132 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1133 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1134 fill_origin_blob(norigin
, &file_p
);
1138 for (j
= 0; j
< num_ents
; j
++) {
1139 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1140 norigin
, this, &file_p
);
1141 copy_split_if_better(sb
, blame_list
[j
].split
,
1145 origin_decref(norigin
);
1148 for (j
= 0; j
< num_ents
; j
++) {
1149 struct blame_entry
*split
= blame_list
[j
].split
;
1150 if (split
[1].suspect
&&
1151 blame_copy_score
< ent_score(sb
, &split
[1])) {
1152 split_blame(sb
, split
, blame_list
[j
].ent
);
1155 decref_split(split
);
1161 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1167 diff_flush(&diff_opts
);
1168 diff_tree_release_paths(&diff_opts
);
1173 * The blobs of origin and porigin exactly match, so everything
1174 * origin is suspected for can be blamed on the parent.
1176 static void pass_whole_blame(struct scoreboard
*sb
,
1177 struct origin
*origin
, struct origin
*porigin
)
1179 struct blame_entry
*e
;
1181 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1182 /* Steal its file */
1183 porigin
->file
= origin
->file
;
1184 origin
->file
.ptr
= NULL
;
1186 for (e
= sb
->ent
; e
; e
= e
->next
) {
1187 if (!same_suspect(e
->suspect
, origin
))
1189 origin_incref(porigin
);
1190 origin_decref(e
->suspect
);
1191 e
->suspect
= porigin
;
1195 #define MAXPARENT 16
1197 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1200 struct commit
*commit
= origin
->commit
;
1201 struct commit_list
*parent
;
1202 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1204 memset(parent_origin
, 0, sizeof(parent_origin
));
1206 /* The first pass looks for unrenamed path to optimize for
1207 * common cases, then we look for renames in the second pass.
1209 for (pass
= 0; pass
< 2; pass
++) {
1210 struct origin
*(*find
)(struct scoreboard
*,
1211 struct commit
*, struct origin
*);
1212 find
= pass
? find_rename
: find_origin
;
1214 for (i
= 0, parent
= commit
->parents
;
1215 i
< MAXPARENT
&& parent
;
1216 parent
= parent
->next
, i
++) {
1217 struct commit
*p
= parent
->item
;
1220 if (parent_origin
[i
])
1222 if (parse_commit(p
))
1224 porigin
= find(sb
, p
, origin
);
1227 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1228 pass_whole_blame(sb
, origin
, porigin
);
1229 origin_decref(porigin
);
1232 for (j
= same
= 0; j
< i
; j
++)
1233 if (parent_origin
[j
] &&
1234 !hashcmp(parent_origin
[j
]->blob_sha1
,
1235 porigin
->blob_sha1
)) {
1240 parent_origin
[i
] = porigin
;
1242 origin_decref(porigin
);
1247 for (i
= 0, parent
= commit
->parents
;
1248 i
< MAXPARENT
&& parent
;
1249 parent
= parent
->next
, i
++) {
1250 struct origin
*porigin
= parent_origin
[i
];
1253 if (pass_blame_to_parent(sb
, origin
, porigin
))
1258 * Optionally find moves in parents' files.
1260 if (opt
& PICKAXE_BLAME_MOVE
)
1261 for (i
= 0, parent
= commit
->parents
;
1262 i
< MAXPARENT
&& parent
;
1263 parent
= parent
->next
, i
++) {
1264 struct origin
*porigin
= parent_origin
[i
];
1267 if (find_move_in_parent(sb
, origin
, porigin
))
1272 * Optionally find copies from parents' files.
1274 if (opt
& PICKAXE_BLAME_COPY
)
1275 for (i
= 0, parent
= commit
->parents
;
1276 i
< MAXPARENT
&& parent
;
1277 parent
= parent
->next
, i
++) {
1278 struct origin
*porigin
= parent_origin
[i
];
1279 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1285 for (i
= 0; i
< MAXPARENT
; i
++) {
1286 if (parent_origin
[i
]) {
1287 drop_origin_blob(parent_origin
[i
]);
1288 origin_decref(parent_origin
[i
]);
1291 drop_origin_blob(origin
);
1295 * Information on commits, used for output.
1300 const char *author_mail
;
1301 unsigned long author_time
;
1302 const char *author_tz
;
1304 /* filled only when asked for details */
1305 const char *committer
;
1306 const char *committer_mail
;
1307 unsigned long committer_time
;
1308 const char *committer_tz
;
1310 const char *summary
;
1314 * Parse author/committer line in the commit object buffer
1316 static void get_ac_line(const char *inbuf
, const char *what
,
1317 int bufsz
, char *person
, const char **mail
,
1318 unsigned long *time
, const char **tz
)
1320 int len
, tzlen
, maillen
;
1321 char *tmp
, *endp
, *timepos
;
1323 tmp
= strstr(inbuf
, what
);
1326 tmp
+= strlen(what
);
1327 endp
= strchr(tmp
, '\n');
1335 *mail
= *tz
= "(unknown)";
1339 memcpy(person
, tmp
, len
);
1347 tzlen
= (person
+len
)-(tmp
+1);
1352 *time
= strtoul(tmp
, NULL
, 10);
1360 maillen
= timepos
- tmp
;
1366 * mailmap expansion may make the name longer.
1367 * make room by pushing stuff down.
1369 tmp
= person
+ bufsz
- (tzlen
+ 1);
1370 memmove(tmp
, *tz
, tzlen
);
1374 tmp
= tmp
- (maillen
+ 1);
1375 memmove(tmp
, *mail
, maillen
);
1380 * Now, convert e-mail using mailmap
1382 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1385 static void get_commit_info(struct commit
*commit
,
1386 struct commit_info
*ret
,
1391 static char author_buf
[1024];
1392 static char committer_buf
[1024];
1393 static char summary_buf
[1024];
1396 * We've operated without save_commit_buffer, so
1397 * we now need to populate them for output.
1399 if (!commit
->buffer
) {
1400 enum object_type type
;
1403 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1404 if (!commit
->buffer
)
1405 die("Cannot read commit %s",
1406 sha1_to_hex(commit
->object
.sha1
));
1408 ret
->author
= author_buf
;
1409 get_ac_line(commit
->buffer
, "\nauthor ",
1410 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1411 &ret
->author_time
, &ret
->author_tz
);
1416 ret
->committer
= committer_buf
;
1417 get_ac_line(commit
->buffer
, "\ncommitter ",
1418 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1419 &ret
->committer_time
, &ret
->committer_tz
);
1421 ret
->summary
= summary_buf
;
1422 tmp
= strstr(commit
->buffer
, "\n\n");
1425 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1429 endp
= strchr(tmp
, '\n');
1431 endp
= tmp
+ strlen(tmp
);
1433 if (len
>= sizeof(summary_buf
) || len
== 0)
1435 memcpy(summary_buf
, tmp
, len
);
1436 summary_buf
[len
] = 0;
1440 * To allow LF and other nonportable characters in pathnames,
1441 * they are c-style quoted as needed.
1443 static void write_filename_info(const char *path
)
1445 printf("filename ");
1446 write_name_quoted(path
, stdout
, '\n');
1450 * The blame_entry is found to be guilty for the range. Mark it
1451 * as such, and show it in incremental output.
1453 static void found_guilty_entry(struct blame_entry
*ent
)
1459 struct origin
*suspect
= ent
->suspect
;
1461 printf("%s %d %d %d\n",
1462 sha1_to_hex(suspect
->commit
->object
.sha1
),
1463 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1464 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1465 struct commit_info ci
;
1466 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1467 get_commit_info(suspect
->commit
, &ci
, 1);
1468 printf("author %s\n", ci
.author
);
1469 printf("author-mail %s\n", ci
.author_mail
);
1470 printf("author-time %lu\n", ci
.author_time
);
1471 printf("author-tz %s\n", ci
.author_tz
);
1472 printf("committer %s\n", ci
.committer
);
1473 printf("committer-mail %s\n", ci
.committer_mail
);
1474 printf("committer-time %lu\n", ci
.committer_time
);
1475 printf("committer-tz %s\n", ci
.committer_tz
);
1476 printf("summary %s\n", ci
.summary
);
1477 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1478 printf("boundary\n");
1480 write_filename_info(suspect
->path
);
1481 maybe_flush_or_die(stdout
, "stdout");
1486 * The main loop -- while the scoreboard has lines whose true origin
1487 * is still unknown, pick one blame_entry, and allow its current
1488 * suspect to pass blames to its parents.
1490 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1493 struct blame_entry
*ent
;
1494 struct commit
*commit
;
1495 struct origin
*suspect
= NULL
;
1497 /* find one suspect to break down */
1498 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1500 suspect
= ent
->suspect
;
1502 return; /* all done */
1505 * We will use this suspect later in the loop,
1506 * so hold onto it in the meantime.
1508 origin_incref(suspect
);
1509 commit
= suspect
->commit
;
1510 if (!commit
->object
.parsed
)
1511 parse_commit(commit
);
1512 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1513 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1514 pass_blame(sb
, suspect
, opt
);
1516 commit
->object
.flags
|= UNINTERESTING
;
1517 if (commit
->object
.parsed
)
1518 mark_parents_uninteresting(commit
);
1520 /* treat root commit as boundary */
1521 if (!commit
->parents
&& !show_root
)
1522 commit
->object
.flags
|= UNINTERESTING
;
1524 /* Take responsibility for the remaining entries */
1525 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1526 if (same_suspect(ent
->suspect
, suspect
))
1527 found_guilty_entry(ent
);
1528 origin_decref(suspect
);
1530 if (DEBUG
) /* sanity */
1531 sanity_check_refcnt(sb
);
1535 static const char *format_time(unsigned long time
, const char *tz_str
,
1538 static char time_buf
[128];
1543 if (show_raw_time
) {
1544 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1549 minutes
= tz
< 0 ? -tz
: tz
;
1550 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1551 minutes
= tz
< 0 ? -minutes
: minutes
;
1552 t
= time
+ minutes
* 60;
1555 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1556 strcat(time_buf
, tz_str
);
1560 #define OUTPUT_ANNOTATE_COMPAT 001
1561 #define OUTPUT_LONG_OBJECT_NAME 002
1562 #define OUTPUT_RAW_TIMESTAMP 004
1563 #define OUTPUT_PORCELAIN 010
1564 #define OUTPUT_SHOW_NAME 020
1565 #define OUTPUT_SHOW_NUMBER 040
1566 #define OUTPUT_SHOW_SCORE 0100
1567 #define OUTPUT_NO_AUTHOR 0200
1569 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1573 struct origin
*suspect
= ent
->suspect
;
1576 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1577 printf("%s%c%d %d %d\n",
1579 ent
->guilty
? ' ' : '*', // purely for debugging
1583 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1584 struct commit_info ci
;
1585 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1586 get_commit_info(suspect
->commit
, &ci
, 1);
1587 printf("author %s\n", ci
.author
);
1588 printf("author-mail %s\n", ci
.author_mail
);
1589 printf("author-time %lu\n", ci
.author_time
);
1590 printf("author-tz %s\n", ci
.author_tz
);
1591 printf("committer %s\n", ci
.committer
);
1592 printf("committer-mail %s\n", ci
.committer_mail
);
1593 printf("committer-time %lu\n", ci
.committer_time
);
1594 printf("committer-tz %s\n", ci
.committer_tz
);
1595 write_filename_info(suspect
->path
);
1596 printf("summary %s\n", ci
.summary
);
1597 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1598 printf("boundary\n");
1600 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1601 write_filename_info(suspect
->path
);
1603 cp
= nth_line(sb
, ent
->lno
);
1604 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1607 printf("%s %d %d\n", hex
,
1608 ent
->s_lno
+ 1 + cnt
,
1609 ent
->lno
+ 1 + cnt
);
1614 } while (ch
!= '\n' &&
1615 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1619 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1623 struct origin
*suspect
= ent
->suspect
;
1624 struct commit_info ci
;
1626 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1628 get_commit_info(suspect
->commit
, &ci
, 1);
1629 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1631 cp
= nth_line(sb
, ent
->lno
);
1632 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1634 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1636 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1638 memset(hex
, ' ', length
);
1639 else if (!cmd_is_annotate
) {
1645 printf("%.*s", length
, hex
);
1646 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1647 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1648 format_time(ci
.author_time
, ci
.author_tz
,
1650 ent
->lno
+ 1 + cnt
);
1652 if (opt
& OUTPUT_SHOW_SCORE
)
1654 max_score_digits
, ent
->score
,
1655 ent
->suspect
->refcnt
);
1656 if (opt
& OUTPUT_SHOW_NAME
)
1657 printf(" %-*.*s", longest_file
, longest_file
,
1659 if (opt
& OUTPUT_SHOW_NUMBER
)
1660 printf(" %*d", max_orig_digits
,
1661 ent
->s_lno
+ 1 + cnt
);
1663 if (!(opt
& OUTPUT_NO_AUTHOR
))
1664 printf(" (%-*.*s %10s",
1665 longest_author
, longest_author
,
1667 format_time(ci
.author_time
,
1671 max_digits
, ent
->lno
+ 1 + cnt
);
1676 } while (ch
!= '\n' &&
1677 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1681 static void output(struct scoreboard
*sb
, int option
)
1683 struct blame_entry
*ent
;
1685 if (option
& OUTPUT_PORCELAIN
) {
1686 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1687 struct blame_entry
*oth
;
1688 struct origin
*suspect
= ent
->suspect
;
1689 struct commit
*commit
= suspect
->commit
;
1690 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1692 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1693 if ((oth
->suspect
->commit
!= commit
) ||
1694 !strcmp(oth
->suspect
->path
, suspect
->path
))
1696 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1702 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1703 if (option
& OUTPUT_PORCELAIN
)
1704 emit_porcelain(sb
, ent
);
1706 emit_other(sb
, ent
, option
);
1712 * To allow quick access to the contents of nth line in the
1713 * final image, prepare an index in the scoreboard.
1715 static int prepare_lines(struct scoreboard
*sb
)
1717 const char *buf
= sb
->final_buf
;
1718 unsigned long len
= sb
->final_buf_size
;
1719 int num
= 0, incomplete
= 0, bol
= 1;
1721 if (len
&& buf
[len
-1] != '\n')
1722 incomplete
++; /* incomplete line at the end */
1725 sb
->lineno
= xrealloc(sb
->lineno
,
1726 sizeof(int* ) * (num
+ 1));
1727 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1730 if (*buf
++ == '\n') {
1735 sb
->lineno
= xrealloc(sb
->lineno
,
1736 sizeof(int* ) * (num
+ incomplete
+ 1));
1737 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1738 sb
->num_lines
= num
+ incomplete
;
1739 return sb
->num_lines
;
1743 * Add phony grafts for use with -S; this is primarily to
1744 * support git-cvsserver that wants to give a linear history
1747 static int read_ancestry(const char *graft_file
)
1749 FILE *fp
= fopen(graft_file
, "r");
1753 while (fgets(buf
, sizeof(buf
), fp
)) {
1754 /* The format is just "Commit Parent1 Parent2 ...\n" */
1755 int len
= strlen(buf
);
1756 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1758 register_commit_graft(graft
, 0);
1765 * How many columns do we need to show line numbers in decimal?
1767 static int lineno_width(int lines
)
1771 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1777 * How many columns do we need to show line numbers, authors,
1780 static void find_alignment(struct scoreboard
*sb
, int *option
)
1782 int longest_src_lines
= 0;
1783 int longest_dst_lines
= 0;
1784 unsigned largest_score
= 0;
1785 struct blame_entry
*e
;
1787 for (e
= sb
->ent
; e
; e
= e
->next
) {
1788 struct origin
*suspect
= e
->suspect
;
1789 struct commit_info ci
;
1792 if (strcmp(suspect
->path
, sb
->path
))
1793 *option
|= OUTPUT_SHOW_NAME
;
1794 num
= strlen(suspect
->path
);
1795 if (longest_file
< num
)
1797 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1798 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1799 get_commit_info(suspect
->commit
, &ci
, 1);
1800 num
= strlen(ci
.author
);
1801 if (longest_author
< num
)
1802 longest_author
= num
;
1804 num
= e
->s_lno
+ e
->num_lines
;
1805 if (longest_src_lines
< num
)
1806 longest_src_lines
= num
;
1807 num
= e
->lno
+ e
->num_lines
;
1808 if (longest_dst_lines
< num
)
1809 longest_dst_lines
= num
;
1810 if (largest_score
< ent_score(sb
, e
))
1811 largest_score
= ent_score(sb
, e
);
1813 max_orig_digits
= lineno_width(longest_src_lines
);
1814 max_digits
= lineno_width(longest_dst_lines
);
1815 max_score_digits
= lineno_width(largest_score
);
1819 * For debugging -- origin is refcounted, and this asserts that
1820 * we do not underflow.
1822 static void sanity_check_refcnt(struct scoreboard
*sb
)
1825 struct blame_entry
*ent
;
1827 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1828 /* Nobody should have zero or negative refcnt */
1829 if (ent
->suspect
->refcnt
<= 0) {
1830 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1832 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1833 ent
->suspect
->refcnt
);
1837 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1838 /* Mark the ones that haven't been checked */
1839 if (0 < ent
->suspect
->refcnt
)
1840 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1842 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1844 * ... then pick each and see if they have the the
1848 struct blame_entry
*e
;
1849 struct origin
*suspect
= ent
->suspect
;
1851 if (0 < suspect
->refcnt
)
1853 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1854 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1855 if (e
->suspect
!= suspect
)
1859 if (suspect
->refcnt
!= found
) {
1860 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1862 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1863 ent
->suspect
->refcnt
, found
);
1869 find_alignment(sb
, &opt
);
1871 die("Baa %d!", baa
);
1876 * Used for the command line parsing; check if the path exists
1877 * in the working tree.
1879 static int has_path_in_work_tree(const char *path
)
1882 return !lstat(path
, &st
);
1885 static unsigned parse_score(const char *arg
)
1888 unsigned long score
= strtoul(arg
, &end
, 10);
1894 static const char *add_prefix(const char *prefix
, const char *path
)
1896 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
1900 * Parsing of (comma separated) one item in the -L option
1902 static const char *parse_loc(const char *spec
,
1903 struct scoreboard
*sb
, long lno
,
1904 long begin
, long *ret
)
1911 regmatch_t match
[1];
1913 /* Allow "-L <something>,+20" to mean starting at <something>
1914 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1917 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1918 num
= strtol(spec
+ 1, &term
, 10);
1919 if (term
!= spec
+ 1) {
1923 *ret
= begin
+ num
- 2;
1932 num
= strtol(spec
, &term
, 10);
1940 /* it could be a regexp of form /.../ */
1941 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1948 /* try [spec+1 .. term-1] as regexp */
1950 begin
--; /* input is in human terms */
1951 line
= nth_line(sb
, begin
);
1953 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1954 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1955 const char *cp
= line
+ match
[0].rm_so
;
1958 while (begin
++ < lno
) {
1959 nline
= nth_line(sb
, begin
);
1960 if (line
<= cp
&& cp
< nline
)
1971 regerror(reg_error
, ®exp
, errbuf
, 1024);
1972 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1977 * Parsing of -L option
1979 static void prepare_blame_range(struct scoreboard
*sb
,
1980 const char *bottomtop
,
1982 long *bottom
, long *top
)
1986 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1988 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1996 static int git_blame_config(const char *var
, const char *value
, void *cb
)
1998 if (!strcmp(var
, "blame.showroot")) {
1999 show_root
= git_config_bool(var
, value
);
2002 if (!strcmp(var
, "blame.blankboundary")) {
2003 blank_boundary
= git_config_bool(var
, value
);
2006 return git_default_config(var
, value
, cb
);
2009 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2011 struct commit
*commit
;
2012 struct origin
*origin
;
2013 unsigned char head_sha1
[20];
2018 struct cache_entry
*ce
;
2021 if (get_sha1("HEAD", head_sha1
))
2022 die("No such ref: HEAD");
2025 commit
= xcalloc(1, sizeof(*commit
));
2026 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2027 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2028 commit
->object
.parsed
= 1;
2030 commit
->object
.type
= OBJ_COMMIT
;
2032 origin
= make_origin(commit
, path
);
2034 strbuf_init(&buf
, 0);
2035 if (!contents_from
|| strcmp("-", contents_from
)) {
2037 const char *read_from
;
2038 unsigned long fin_size
;
2040 if (contents_from
) {
2041 if (stat(contents_from
, &st
) < 0)
2042 die("Cannot stat %s", contents_from
);
2043 read_from
= contents_from
;
2046 if (lstat(path
, &st
) < 0)
2047 die("Cannot lstat %s", path
);
2050 fin_size
= xsize_t(st
.st_size
);
2051 mode
= canon_mode(st
.st_mode
);
2052 switch (st
.st_mode
& S_IFMT
) {
2054 if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2055 die("cannot open or read %s", read_from
);
2058 if (readlink(read_from
, buf
.buf
, buf
.alloc
) != fin_size
)
2059 die("cannot readlink %s", read_from
);
2063 die("unsupported file type %s", read_from
);
2067 /* Reading from stdin */
2068 contents_from
= "standard input";
2070 if (strbuf_read(&buf
, 0, 0) < 0)
2071 die("read error %s from stdin", strerror(errno
));
2073 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2074 origin
->file
.ptr
= buf
.buf
;
2075 origin
->file
.size
= buf
.len
;
2076 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2077 commit
->util
= origin
;
2080 * Read the current index, replace the path entry with
2081 * origin->blob_sha1 without mucking with its mode or type
2082 * bits; we are not going to write this index out -- we just
2083 * want to run "diff-index --cached".
2090 int pos
= cache_name_pos(path
, len
);
2092 mode
= active_cache
[pos
]->ce_mode
;
2094 /* Let's not bother reading from HEAD tree */
2095 mode
= S_IFREG
| 0644;
2097 size
= cache_entry_size(len
);
2098 ce
= xcalloc(1, size
);
2099 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2100 memcpy(ce
->name
, path
, len
);
2101 ce
->ce_flags
= create_ce_flags(len
, 0);
2102 ce
->ce_mode
= create_ce_mode(mode
);
2103 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2106 * We are not going to write this out, so this does not matter
2107 * right now, but someday we might optimize diff-index --cached
2108 * with cache-tree information.
2110 cache_tree_invalidate_path(active_cache_tree
, path
);
2112 commit
->buffer
= xmalloc(400);
2113 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2114 snprintf(commit
->buffer
, 400,
2115 "tree 0000000000000000000000000000000000000000\n"
2119 "Version of %s from %s\n",
2120 sha1_to_hex(head_sha1
),
2121 ident
, ident
, path
, contents_from
? contents_from
: path
);
2125 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2127 struct rev_info revs
;
2129 struct scoreboard sb
;
2131 struct blame_entry
*ent
;
2132 int i
, seen_dashdash
, unk
, opt
;
2133 long bottom
, top
, lno
;
2134 int output_option
= 0;
2136 const char *revs_file
= NULL
;
2137 const char *final_commit_name
= NULL
;
2138 enum object_type type
;
2139 const char *bottomtop
= NULL
;
2140 const char *contents_from
= NULL
;
2142 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2144 git_config(git_blame_config
, NULL
);
2145 save_commit_buffer
= 0;
2149 for (unk
= i
= 1; i
< argc
; i
++) {
2150 const char *arg
= argv
[i
];
2153 else if (!strcmp("-b", arg
))
2155 else if (!strcmp("--root", arg
))
2157 else if (!strcmp(arg
, "--show-stats"))
2159 else if (!strcmp("-c", arg
))
2160 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2161 else if (!strcmp("-t", arg
))
2162 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2163 else if (!strcmp("-l", arg
))
2164 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2165 else if (!strcmp("-s", arg
))
2166 output_option
|= OUTPUT_NO_AUTHOR
;
2167 else if (!strcmp("-w", arg
))
2168 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2169 else if (!strcmp("-S", arg
) && ++i
< argc
)
2170 revs_file
= argv
[i
];
2171 else if (!prefixcmp(arg
, "-M")) {
2172 opt
|= PICKAXE_BLAME_MOVE
;
2173 blame_move_score
= parse_score(arg
+2);
2175 else if (!prefixcmp(arg
, "-C")) {
2177 * -C enables copy from removed files;
2178 * -C -C enables copy from existing files, but only
2179 * when blaming a new file;
2180 * -C -C -C enables copy from existing files for
2183 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2184 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2185 if (opt
& PICKAXE_BLAME_COPY
)
2186 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2187 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2188 blame_copy_score
= parse_score(arg
+2);
2190 else if (!prefixcmp(arg
, "-L")) {
2199 die("More than one '-L n,m' option given");
2202 else if (!strcmp("--contents", arg
)) {
2205 contents_from
= argv
[i
];
2207 else if (!strcmp("--incremental", arg
))
2209 else if (!strcmp("--score-debug", arg
))
2210 output_option
|= OUTPUT_SHOW_SCORE
;
2211 else if (!strcmp("-f", arg
) ||
2212 !strcmp("--show-name", arg
))
2213 output_option
|= OUTPUT_SHOW_NAME
;
2214 else if (!strcmp("-n", arg
) ||
2215 !strcmp("--show-number", arg
))
2216 output_option
|= OUTPUT_SHOW_NUMBER
;
2217 else if (!strcmp("-p", arg
) ||
2218 !strcmp("--porcelain", arg
))
2219 output_option
|= OUTPUT_PORCELAIN
;
2220 else if (!strcmp("--", arg
)) {
2229 if (!blame_move_score
)
2230 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2231 if (!blame_copy_score
)
2232 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2235 * We have collected options unknown to us in argv[1..unk]
2236 * which are to be passed to revision machinery if we are
2237 * going to do the "bottom" processing.
2239 * The remaining are:
2241 * (1) if seen_dashdash, its either
2242 * "-options -- <path>" or
2243 * "-options -- <path> <rev>".
2244 * but the latter is allowed only if there is no
2245 * options that we passed to revision machinery.
2247 * (2) otherwise, we may have "--" somewhere later and
2248 * might be looking at the first one of multiple 'rev'
2249 * parameters (e.g. " master ^next ^maint -- path").
2250 * See if there is a dashdash first, and give the
2251 * arguments before that to revision machinery.
2252 * After that there must be one 'path'.
2254 * (3) otherwise, its one of the three:
2255 * "-options <path> <rev>"
2256 * "-options <rev> <path>"
2258 * but again the first one is allowed only if
2259 * there is no options that we passed to revision
2263 if (seen_dashdash
) {
2267 path
= add_prefix(prefix
, argv
[i
]);
2268 if (i
+ 1 == argc
- 1) {
2271 argv
[unk
++] = argv
[i
+ 1];
2273 else if (i
+ 1 != argc
)
2274 /* garbage at end */
2279 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2280 if (!strcmp(argv
[j
], "--"))
2282 if (seen_dashdash
) {
2284 if (seen_dashdash
+ 1 != argc
- 1)
2286 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2287 for (j
= i
; j
< seen_dashdash
; j
++)
2288 argv
[unk
++] = argv
[j
];
2294 path
= add_prefix(prefix
, argv
[i
]);
2295 if (i
+ 1 == argc
- 1) {
2296 final_commit_name
= argv
[i
+ 1];
2298 /* if (unk == 1) we could be getting
2301 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2302 path
= add_prefix(prefix
, argv
[i
+ 1]);
2303 final_commit_name
= argv
[i
];
2306 else if (i
!= argc
- 1)
2307 usage(blame_usage
); /* garbage at end */
2310 if (!has_path_in_work_tree(path
))
2311 die("cannot stat path %s: %s",
2312 path
, strerror(errno
));
2316 if (final_commit_name
)
2317 argv
[unk
++] = final_commit_name
;
2320 * Now we got rev and path. We do not want the path pruning
2321 * but we may want "bottom" processing.
2323 argv
[unk
++] = "--"; /* terminate the rev name */
2326 init_revisions(&revs
, NULL
);
2327 setup_revisions(unk
, argv
, &revs
, NULL
);
2328 memset(&sb
, 0, sizeof(sb
));
2331 * There must be one and only one positive commit in the
2332 * revs->pending array.
2334 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2335 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2336 if (obj
->flags
& UNINTERESTING
)
2338 while (obj
->type
== OBJ_TAG
)
2339 obj
= deref_tag(obj
, NULL
, 0);
2340 if (obj
->type
!= OBJ_COMMIT
)
2341 die("Non commit %s?",
2342 revs
.pending
.objects
[i
].name
);
2344 die("More than one commit to dig from %s and %s?",
2345 revs
.pending
.objects
[i
].name
,
2347 sb
.final
= (struct commit
*) obj
;
2348 final_commit_name
= revs
.pending
.objects
[i
].name
;
2353 * "--not A B -- path" without anything positive;
2354 * do not default to HEAD, but use the working tree
2358 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2359 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2361 else if (contents_from
)
2362 die("Cannot use --contents with final commit object name");
2365 * If we have bottom, this will mark the ancestors of the
2366 * bottom commits we would reach while traversing as
2369 if (prepare_revision_walk(&revs
))
2370 die("revision walk setup failed");
2372 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2375 buf
= xmalloc(o
->file
.size
+ 1);
2376 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2378 sb
.final_buf_size
= o
->file
.size
;
2381 o
= get_origin(&sb
, sb
.final
, path
);
2382 if (fill_blob_sha1(o
))
2383 die("no such path %s in %s", path
, final_commit_name
);
2385 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2386 &sb
.final_buf_size
);
2388 die("Cannot read blob %s for path %s",
2389 sha1_to_hex(o
->blob_sha1
),
2393 lno
= prepare_lines(&sb
);
2397 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2398 if (bottom
&& top
&& top
< bottom
) {
2400 tmp
= top
; top
= bottom
; bottom
= tmp
;
2408 die("file %s has only %lu lines", path
, lno
);
2410 ent
= xcalloc(1, sizeof(*ent
));
2412 ent
->num_lines
= top
- bottom
;
2414 ent
->s_lno
= bottom
;
2419 if (revs_file
&& read_ancestry(revs_file
))
2420 die("reading graft file %s failed: %s",
2421 revs_file
, strerror(errno
));
2423 read_mailmap(&mailmap
, ".mailmap", NULL
);
2428 assign_blame(&sb
, &revs
, opt
);
2435 if (!(output_option
& OUTPUT_PORCELAIN
))
2436 find_alignment(&sb
, &output_option
);
2438 output(&sb
, output_option
);
2439 free((void *)sb
.final_buf
);
2440 for (ent
= sb
.ent
; ent
; ) {
2441 struct blame_entry
*e
= ent
->next
;
2447 printf("num read blob: %d\n", num_read_blob
);
2448 printf("num get patch: %d\n", num_get_patch
);
2449 printf("num commits: %d\n", num_commits
);