4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "string-list.h"
21 #include "parse-options.h"
23 static char blame_usage
[] = "git blame [options] [rev-opts] [rev] [--] file";
25 static const char *blame_opt_usage
[] = {
28 "[rev-opts] are documented in git-rev-list(1)",
32 static int longest_file
;
33 static int longest_author
;
34 static int max_orig_digits
;
35 static int max_digits
;
36 static int max_score_digits
;
39 static int blank_boundary
;
40 static int incremental
;
41 static int xdl_opts
= XDF_NEED_MINIMAL
;
42 static struct string_list mailmap
;
49 static int num_read_blob
;
50 static int num_get_patch
;
51 static int num_commits
;
53 #define PICKAXE_BLAME_MOVE 01
54 #define PICKAXE_BLAME_COPY 02
55 #define PICKAXE_BLAME_COPY_HARDER 04
56 #define PICKAXE_BLAME_COPY_HARDEST 010
59 * blame for a blame_entry with score lower than these thresholds
60 * is not passed to the parent using move/copy logic.
62 static unsigned blame_move_score
;
63 static unsigned blame_copy_score
;
64 #define BLAME_DEFAULT_MOVE_SCORE 20
65 #define BLAME_DEFAULT_COPY_SCORE 40
67 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
68 #define METAINFO_SHOWN (1u<<12)
69 #define MORE_THAN_ONE_PATH (1u<<13)
72 * One blob in a commit that is being suspected
76 struct commit
*commit
;
78 unsigned char blob_sha1
[20];
79 char path
[FLEX_ARRAY
];
83 * Given an origin, prepare mmfile_t structure to be used by the
86 static void fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
89 enum object_type type
;
91 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
92 (unsigned long *)(&(file
->size
)));
94 die("Cannot read blob %s for path %s",
95 sha1_to_hex(o
->blob_sha1
),
104 * Origin is refcounted and usually we keep the blob contents to be
107 static inline struct origin
*origin_incref(struct origin
*o
)
114 static void origin_decref(struct origin
*o
)
116 if (o
&& --o
->refcnt
<= 0) {
122 static void drop_origin_blob(struct origin
*o
)
131 * Each group of lines is described by a blame_entry; it can be split
132 * as we pass blame to the parents. They form a linked list in the
133 * scoreboard structure, sorted by the target line number.
136 struct blame_entry
*prev
;
137 struct blame_entry
*next
;
139 /* the first line of this group in the final image;
140 * internally all line numbers are 0 based.
144 /* how many lines this group has */
147 /* the commit that introduced this group into the final image */
148 struct origin
*suspect
;
150 /* true if the suspect is truly guilty; false while we have not
151 * checked if the group came from one of its parents.
155 /* true if the entry has been scanned for copies in the current parent
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
;
176 struct rev_info
*revs
;
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_OPT_SET(&diff_opts
, RECURSIVE
);
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
);
384 diff_tree_release_paths(&diff_opts
);
387 * Create a freestanding copy that is not part of
388 * the refcounted origin found in the scoreboard, and
389 * cache it in the commit.
391 struct origin
*cached
;
393 cached
= make_origin(porigin
->commit
, porigin
->path
);
394 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
395 parent
->util
= cached
;
401 * We have an origin -- find the path that corresponds to it in its
402 * parent and return an origin structure to represent it.
404 static struct origin
*find_rename(struct scoreboard
*sb
,
405 struct commit
*parent
,
406 struct origin
*origin
)
408 struct origin
*porigin
= NULL
;
409 struct diff_options diff_opts
;
411 const char *paths
[2];
413 diff_setup(&diff_opts
);
414 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
415 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
416 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
417 diff_opts
.single_follow
= origin
->path
;
419 diff_tree_setup_paths(paths
, &diff_opts
);
420 if (diff_setup_done(&diff_opts
) < 0)
423 if (is_null_sha1(origin
->commit
->object
.sha1
))
424 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
426 diff_tree_sha1(parent
->tree
->object
.sha1
,
427 origin
->commit
->tree
->object
.sha1
,
429 diffcore_std(&diff_opts
);
431 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
432 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
433 if ((p
->status
== 'R' || p
->status
== 'C') &&
434 !strcmp(p
->two
->path
, origin
->path
)) {
435 porigin
= get_origin(sb
, parent
, p
->one
->path
);
436 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
440 diff_flush(&diff_opts
);
441 diff_tree_release_paths(&diff_opts
);
446 * Parsing of patch chunks...
449 /* line number in postimage; up to but not including this
450 * line is the same as preimage
454 /* preimage line number after this chunk */
457 /* postimage line number after this chunk */
462 struct chunk
*chunks
;
466 struct blame_diff_state
{
468 unsigned hunk_post_context
;
469 unsigned hunk_in_pre_context
: 1;
472 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
474 struct blame_diff_state
*state
= state_
;
476 int off1
, off2
, len1
, len2
, num
;
478 num
= state
->ret
->num
;
479 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
480 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
481 state
->ret
->chunks
[num
- 1].same
++;
483 state
->hunk_in_pre_context
= 0;
485 state
->hunk_post_context
++;
487 state
->hunk_post_context
= 0;
492 if (num
&& state
->hunk_post_context
) {
493 chunk
= &state
->ret
->chunks
[num
- 1];
494 chunk
->p_next
-= state
->hunk_post_context
;
495 chunk
->t_next
-= state
->hunk_post_context
;
497 state
->ret
->num
= ++num
;
498 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
499 sizeof(struct chunk
) * num
);
500 chunk
= &state
->ret
->chunks
[num
- 1];
501 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
506 /* Line numbers in patch output are one based. */
510 chunk
->same
= len2
? off2
: (off2
+ 1);
512 chunk
->p_next
= off1
+ (len1
? len1
: 1);
513 chunk
->t_next
= chunk
->same
+ len2
;
514 state
->hunk_in_pre_context
= 1;
515 state
->hunk_post_context
= 0;
518 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
521 struct blame_diff_state state
;
526 xpp
.flags
= xdl_opts
;
527 memset(&xecfg
, 0, sizeof(xecfg
));
528 xecfg
.ctxlen
= context
;
529 memset(&state
, 0, sizeof(state
));
530 state
.ret
= xmalloc(sizeof(struct patch
));
531 state
.ret
->chunks
= NULL
;
534 xdi_diff_outf(file_p
, file_o
, process_u_diff
, &state
, &xpp
, &xecfg
, &ecb
);
536 if (state
.ret
->num
) {
538 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
539 chunk
->p_next
-= state
.hunk_post_context
;
540 chunk
->t_next
-= state
.hunk_post_context
;
546 * Run diff between two origins and grab the patch output, so that
547 * we can pass blame for lines origin is currently suspected for
550 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
552 mmfile_t file_p
, file_o
;
555 fill_origin_blob(parent
, &file_p
);
556 fill_origin_blob(origin
, &file_o
);
557 if (!file_p
.ptr
|| !file_o
.ptr
)
559 patch
= compare_buffer(&file_p
, &file_o
, 0);
564 static void free_patch(struct patch
*p
)
571 * Link in a new blame entry to the scoreboard. Entries that cover the
572 * same line range have been removed from the scoreboard previously.
574 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
576 struct blame_entry
*ent
, *prev
= NULL
;
578 origin_incref(e
->suspect
);
580 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
583 /* prev, if not NULL, is the last one that is below e */
586 e
->next
= prev
->next
;
598 * src typically is on-stack; we want to copy the information in it to
599 * a malloced blame_entry that is already on the linked list of the
600 * scoreboard. The origin of dst loses a refcnt while the origin of src
603 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
605 struct blame_entry
*p
, *n
;
609 origin_incref(src
->suspect
);
610 origin_decref(dst
->suspect
);
611 memcpy(dst
, src
, sizeof(*src
));
617 static const char *nth_line(struct scoreboard
*sb
, int lno
)
619 return sb
->final_buf
+ sb
->lineno
[lno
];
623 * It is known that lines between tlno to same came from parent, and e
624 * has an overlap with that range. it also is known that parent's
625 * line plno corresponds to e's line tlno.
631 * <------------------>
633 * Split e into potentially three parts; before this chunk, the chunk
634 * to be blamed for the parent, and after that portion.
636 static void split_overlap(struct blame_entry
*split
,
637 struct blame_entry
*e
,
638 int tlno
, int plno
, int same
,
639 struct origin
*parent
)
642 memset(split
, 0, sizeof(struct blame_entry
[3]));
644 if (e
->s_lno
< tlno
) {
645 /* there is a pre-chunk part not blamed on parent */
646 split
[0].suspect
= origin_incref(e
->suspect
);
647 split
[0].lno
= e
->lno
;
648 split
[0].s_lno
= e
->s_lno
;
649 split
[0].num_lines
= tlno
- e
->s_lno
;
650 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
651 split
[1].s_lno
= plno
;
654 split
[1].lno
= e
->lno
;
655 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
658 if (same
< e
->s_lno
+ e
->num_lines
) {
659 /* there is a post-chunk part not blamed on parent */
660 split
[2].suspect
= origin_incref(e
->suspect
);
661 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
662 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
663 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
664 chunk_end_lno
= split
[2].lno
;
667 chunk_end_lno
= e
->lno
+ e
->num_lines
;
668 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
671 * if it turns out there is nothing to blame the parent for,
672 * forget about the splitting. !split[1].suspect signals this.
674 if (split
[1].num_lines
< 1)
676 split
[1].suspect
= origin_incref(parent
);
680 * split_overlap() divided an existing blame e into up to three parts
681 * in split. Adjust the linked list of blames in the scoreboard to
684 static void split_blame(struct scoreboard
*sb
,
685 struct blame_entry
*split
,
686 struct blame_entry
*e
)
688 struct blame_entry
*new_entry
;
690 if (split
[0].suspect
&& split
[2].suspect
) {
691 /* The first part (reuse storage for the existing entry e) */
692 dup_entry(e
, &split
[0]);
694 /* The last part -- me */
695 new_entry
= xmalloc(sizeof(*new_entry
));
696 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
697 add_blame_entry(sb
, new_entry
);
699 /* ... and the middle part -- parent */
700 new_entry
= xmalloc(sizeof(*new_entry
));
701 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
702 add_blame_entry(sb
, new_entry
);
704 else if (!split
[0].suspect
&& !split
[2].suspect
)
706 * The parent covers the entire area; reuse storage for
707 * e and replace it with the parent.
709 dup_entry(e
, &split
[1]);
710 else if (split
[0].suspect
) {
711 /* me and then parent */
712 dup_entry(e
, &split
[0]);
714 new_entry
= xmalloc(sizeof(*new_entry
));
715 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
716 add_blame_entry(sb
, new_entry
);
719 /* parent and then me */
720 dup_entry(e
, &split
[1]);
722 new_entry
= xmalloc(sizeof(*new_entry
));
723 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
724 add_blame_entry(sb
, new_entry
);
727 if (DEBUG
) { /* sanity */
728 struct blame_entry
*ent
;
729 int lno
= sb
->ent
->lno
, corrupt
= 0;
731 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
736 lno
+= ent
->num_lines
;
740 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
741 printf("L %8d l %8d n %8d\n",
742 lno
, ent
->lno
, ent
->num_lines
);
743 lno
= ent
->lno
+ ent
->num_lines
;
751 * After splitting the blame, the origins used by the
752 * on-stack blame_entry should lose one refcnt each.
754 static void decref_split(struct blame_entry
*split
)
758 for (i
= 0; i
< 3; i
++)
759 origin_decref(split
[i
].suspect
);
763 * Helper for blame_chunk(). blame_entry e is known to overlap with
764 * the patch hunk; split it and pass blame to the parent.
766 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
767 int tlno
, int plno
, int same
,
768 struct origin
*parent
)
770 struct blame_entry split
[3];
772 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
773 if (split
[1].suspect
)
774 split_blame(sb
, split
, e
);
779 * Find the line number of the last line the target is suspected for.
781 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
783 struct blame_entry
*e
;
784 int last_in_target
= -1;
786 for (e
= sb
->ent
; e
; e
= e
->next
) {
787 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
789 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
790 last_in_target
= e
->s_lno
+ e
->num_lines
;
792 return last_in_target
;
796 * Process one hunk from the patch between the current suspect for
797 * blame_entry e and its parent. Find and split the overlap, and
798 * pass blame to the overlapping part to the parent.
800 static void blame_chunk(struct scoreboard
*sb
,
801 int tlno
, int plno
, int same
,
802 struct origin
*target
, struct origin
*parent
)
804 struct blame_entry
*e
;
806 for (e
= sb
->ent
; e
; e
= e
->next
) {
807 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
809 if (same
<= e
->s_lno
)
811 if (tlno
< e
->s_lno
+ e
->num_lines
)
812 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
817 * We are looking at the origin 'target' and aiming to pass blame
818 * for the lines it is suspected to its parent. Run diff to find
819 * which lines came from parent and pass blame for them.
821 static int pass_blame_to_parent(struct scoreboard
*sb
,
822 struct origin
*target
,
823 struct origin
*parent
)
825 int i
, last_in_target
, plno
, tlno
;
828 last_in_target
= find_last_in_target(sb
, target
);
829 if (last_in_target
< 0)
830 return 1; /* nothing remains for this target */
832 patch
= get_patch(parent
, target
);
834 for (i
= 0; i
< patch
->num
; i
++) {
835 struct chunk
*chunk
= &patch
->chunks
[i
];
837 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
838 plno
= chunk
->p_next
;
839 tlno
= chunk
->t_next
;
841 /* The rest (i.e. anything after tlno) are the same as the parent */
842 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
849 * The lines in blame_entry after splitting blames many times can become
850 * very small and trivial, and at some point it becomes pointless to
851 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
852 * ordinary C program, and it is not worth to say it was copied from
853 * totally unrelated file in the parent.
855 * Compute how trivial the lines in the blame_entry are.
857 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
866 cp
= nth_line(sb
, e
->lno
);
867 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
869 unsigned ch
= *((unsigned char *)cp
);
879 * best_so_far[] and this[] are both a split of an existing blame_entry
880 * that passes blame to the parent. Maintain best_so_far the best split
881 * so far, by comparing this and best_so_far and copying this into
882 * bst_so_far as needed.
884 static void copy_split_if_better(struct scoreboard
*sb
,
885 struct blame_entry
*best_so_far
,
886 struct blame_entry
*this)
890 if (!this[1].suspect
)
892 if (best_so_far
[1].suspect
) {
893 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
897 for (i
= 0; i
< 3; i
++)
898 origin_incref(this[i
].suspect
);
899 decref_split(best_so_far
);
900 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
904 * We are looking at a part of the final image represented by
905 * ent (tlno and same are offset by ent->s_lno).
906 * tlno is where we are looking at in the final image.
907 * up to (but not including) same match preimage.
908 * plno is where we are looking at in the preimage.
910 * <-------------- final image ---------------------->
913 * <---------preimage----->
916 * All line numbers are 0-based.
918 static void handle_split(struct scoreboard
*sb
,
919 struct blame_entry
*ent
,
920 int tlno
, int plno
, int same
,
921 struct origin
*parent
,
922 struct blame_entry
*split
)
924 if (ent
->num_lines
<= tlno
)
927 struct blame_entry
this[3];
930 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
931 copy_split_if_better(sb
, split
, this);
937 * Find the lines from parent that are the same as ent so that
938 * we can pass blames to it. file_p has the blob contents for
941 static void find_copy_in_blob(struct scoreboard
*sb
,
942 struct blame_entry
*ent
,
943 struct origin
*parent
,
944 struct blame_entry
*split
,
954 * Prepare mmfile that contains only the lines in ent.
956 cp
= nth_line(sb
, ent
->lno
);
957 file_o
.ptr
= (char*) cp
;
958 cnt
= ent
->num_lines
;
960 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
964 file_o
.size
= cp
- file_o
.ptr
;
966 patch
= compare_buffer(file_p
, &file_o
, 1);
969 * file_o is a part of final image we are annotating.
970 * file_p partially may match that image.
972 memset(split
, 0, sizeof(struct blame_entry
[3]));
974 for (i
= 0; i
< patch
->num
; i
++) {
975 struct chunk
*chunk
= &patch
->chunks
[i
];
977 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
978 plno
= chunk
->p_next
;
979 tlno
= chunk
->t_next
;
981 /* remainder, if any, all match the preimage */
982 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
987 * See if lines currently target is suspected for can be attributed to
990 static int find_move_in_parent(struct scoreboard
*sb
,
991 struct origin
*target
,
992 struct origin
*parent
)
994 int last_in_target
, made_progress
;
995 struct blame_entry
*e
, split
[3];
998 last_in_target
= find_last_in_target(sb
, target
);
999 if (last_in_target
< 0)
1000 return 1; /* nothing remains for this target */
1002 fill_origin_blob(parent
, &file_p
);
1007 while (made_progress
) {
1009 for (e
= sb
->ent
; e
; e
= e
->next
) {
1010 if (e
->guilty
|| !same_suspect(e
->suspect
, target
) ||
1011 ent_score(sb
, e
) < blame_move_score
)
1013 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1014 if (split
[1].suspect
&&
1015 blame_move_score
< ent_score(sb
, &split
[1])) {
1016 split_blame(sb
, split
, e
);
1019 decref_split(split
);
1026 struct blame_entry
*ent
;
1027 struct blame_entry split
[3];
1031 * Count the number of entries the target is suspected for,
1032 * and prepare a list of entry and the best split.
1034 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1035 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
->scanned
&& !e
->guilty
&&
1045 same_suspect(e
->suspect
, target
) &&
1046 min_score
< ent_score(sb
, e
))
1049 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1050 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1051 if (!e
->scanned
&& !e
->guilty
&&
1052 same_suspect(e
->suspect
, target
) &&
1053 min_score
< ent_score(sb
, e
))
1054 blame_list
[i
++].ent
= e
;
1056 *num_ents_p
= num_ents
;
1061 * Reset the scanned status on all entries.
1063 static void reset_scanned_flag(struct scoreboard
*sb
)
1065 struct blame_entry
*e
;
1066 for (e
= sb
->ent
; e
; e
= e
->next
)
1071 * For lines target is suspected for, see if we can find code movement
1072 * across file boundary from the parent commit. porigin is the path
1073 * in the parent we already tried.
1075 static int find_copy_in_parent(struct scoreboard
*sb
,
1076 struct origin
*target
,
1077 struct commit
*parent
,
1078 struct origin
*porigin
,
1081 struct diff_options diff_opts
;
1082 const char *paths
[1];
1085 struct blame_list
*blame_list
;
1088 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
1090 return 1; /* nothing remains for this target */
1092 diff_setup(&diff_opts
);
1093 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1094 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1097 diff_tree_setup_paths(paths
, &diff_opts
);
1098 if (diff_setup_done(&diff_opts
) < 0)
1101 /* Try "find copies harder" on new path if requested;
1102 * we do not want to use diffcore_rename() actually to
1103 * match things up; find_copies_harder is set only to
1104 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1105 * and this code needs to be after diff_setup_done(), which
1106 * usually makes find-copies-harder imply copy detection.
1108 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1109 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1110 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1111 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1113 if (is_null_sha1(target
->commit
->object
.sha1
))
1114 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1116 diff_tree_sha1(parent
->tree
->object
.sha1
,
1117 target
->commit
->tree
->object
.sha1
,
1120 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1121 diffcore_std(&diff_opts
);
1125 int made_progress
= 0;
1127 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1128 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1129 struct origin
*norigin
;
1131 struct blame_entry
this[3];
1133 if (!DIFF_FILE_VALID(p
->one
))
1134 continue; /* does not exist in parent */
1135 if (S_ISGITLINK(p
->one
->mode
))
1136 continue; /* ignore git links */
1137 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1138 /* find_move already dealt with this path */
1141 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1142 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1143 fill_origin_blob(norigin
, &file_p
);
1147 for (j
= 0; j
< num_ents
; j
++) {
1148 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1149 norigin
, this, &file_p
);
1150 copy_split_if_better(sb
, blame_list
[j
].split
,
1154 origin_decref(norigin
);
1157 for (j
= 0; j
< num_ents
; j
++) {
1158 struct blame_entry
*split
= blame_list
[j
].split
;
1159 if (split
[1].suspect
&&
1160 blame_copy_score
< ent_score(sb
, &split
[1])) {
1161 split_blame(sb
, split
, blame_list
[j
].ent
);
1165 blame_list
[j
].ent
->scanned
= 1;
1166 decref_split(split
);
1172 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
1178 reset_scanned_flag(sb
);
1179 diff_flush(&diff_opts
);
1180 diff_tree_release_paths(&diff_opts
);
1185 * The blobs of origin and porigin exactly match, so everything
1186 * origin is suspected for can be blamed on the parent.
1188 static void pass_whole_blame(struct scoreboard
*sb
,
1189 struct origin
*origin
, struct origin
*porigin
)
1191 struct blame_entry
*e
;
1193 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1194 /* Steal its file */
1195 porigin
->file
= origin
->file
;
1196 origin
->file
.ptr
= NULL
;
1198 for (e
= sb
->ent
; e
; e
= e
->next
) {
1199 if (!same_suspect(e
->suspect
, origin
))
1201 origin_incref(porigin
);
1202 origin_decref(e
->suspect
);
1203 e
->suspect
= porigin
;
1208 * We pass blame from the current commit to its parents. We keep saying
1209 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1210 * exonerate ourselves.
1212 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1215 return commit
->parents
;
1216 return lookup_decoration(&revs
->children
, &commit
->object
);
1219 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1222 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1223 for (cnt
= 0; l
; l
= l
->next
)
1230 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1232 struct rev_info
*revs
= sb
->revs
;
1233 int i
, pass
, num_sg
;
1234 struct commit
*commit
= origin
->commit
;
1235 struct commit_list
*sg
;
1236 struct origin
*sg_buf
[MAXSG
];
1237 struct origin
*porigin
, **sg_origin
= sg_buf
;
1239 num_sg
= num_scapegoats(revs
, commit
);
1242 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1243 memset(sg_buf
, 0, sizeof(sg_buf
));
1245 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1248 * The first pass looks for unrenamed path to optimize for
1249 * common cases, then we look for renames in the second pass.
1251 for (pass
= 0; pass
< 2; pass
++) {
1252 struct origin
*(*find
)(struct scoreboard
*,
1253 struct commit
*, struct origin
*);
1254 find
= pass
? find_rename
: find_origin
;
1256 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1258 sg
= sg
->next
, i
++) {
1259 struct commit
*p
= sg
->item
;
1264 if (parse_commit(p
))
1266 porigin
= find(sb
, p
, origin
);
1269 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1270 pass_whole_blame(sb
, origin
, porigin
);
1271 origin_decref(porigin
);
1274 for (j
= same
= 0; j
< i
; j
++)
1276 !hashcmp(sg_origin
[j
]->blob_sha1
,
1277 porigin
->blob_sha1
)) {
1282 sg_origin
[i
] = porigin
;
1284 origin_decref(porigin
);
1289 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1291 sg
= sg
->next
, i
++) {
1292 struct origin
*porigin
= sg_origin
[i
];
1295 if (pass_blame_to_parent(sb
, origin
, porigin
))
1300 * Optionally find moves in parents' files.
1302 if (opt
& PICKAXE_BLAME_MOVE
)
1303 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1305 sg
= sg
->next
, i
++) {
1306 struct origin
*porigin
= sg_origin
[i
];
1309 if (find_move_in_parent(sb
, origin
, porigin
))
1314 * Optionally find copies from parents' files.
1316 if (opt
& PICKAXE_BLAME_COPY
)
1317 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1319 sg
= sg
->next
, i
++) {
1320 struct origin
*porigin
= sg_origin
[i
];
1321 if (find_copy_in_parent(sb
, origin
, sg
->item
,
1327 for (i
= 0; i
< num_sg
; i
++) {
1329 drop_origin_blob(sg_origin
[i
]);
1330 origin_decref(sg_origin
[i
]);
1333 drop_origin_blob(origin
);
1334 if (sg_buf
!= sg_origin
)
1339 * Information on commits, used for output.
1344 const char *author_mail
;
1345 unsigned long author_time
;
1346 const char *author_tz
;
1348 /* filled only when asked for details */
1349 const char *committer
;
1350 const char *committer_mail
;
1351 unsigned long committer_time
;
1352 const char *committer_tz
;
1354 const char *summary
;
1358 * Parse author/committer line in the commit object buffer
1360 static void get_ac_line(const char *inbuf
, const char *what
,
1361 int bufsz
, char *person
, const char **mail
,
1362 unsigned long *time
, const char **tz
)
1364 int len
, tzlen
, maillen
;
1365 char *tmp
, *endp
, *timepos
;
1367 tmp
= strstr(inbuf
, what
);
1370 tmp
+= strlen(what
);
1371 endp
= strchr(tmp
, '\n');
1379 *mail
= *tz
= "(unknown)";
1383 memcpy(person
, tmp
, len
);
1391 tzlen
= (person
+len
)-(tmp
+1);
1396 *time
= strtoul(tmp
, NULL
, 10);
1404 maillen
= timepos
- tmp
;
1410 * mailmap expansion may make the name longer.
1411 * make room by pushing stuff down.
1413 tmp
= person
+ bufsz
- (tzlen
+ 1);
1414 memmove(tmp
, *tz
, tzlen
);
1418 tmp
= tmp
- (maillen
+ 1);
1419 memmove(tmp
, *mail
, maillen
);
1424 * Now, convert e-mail using mailmap
1426 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1429 static void get_commit_info(struct commit
*commit
,
1430 struct commit_info
*ret
,
1435 static char author_buf
[1024];
1436 static char committer_buf
[1024];
1437 static char summary_buf
[1024];
1440 * We've operated without save_commit_buffer, so
1441 * we now need to populate them for output.
1443 if (!commit
->buffer
) {
1444 enum object_type type
;
1447 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1448 if (!commit
->buffer
)
1449 die("Cannot read commit %s",
1450 sha1_to_hex(commit
->object
.sha1
));
1452 ret
->author
= author_buf
;
1453 get_ac_line(commit
->buffer
, "\nauthor ",
1454 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1455 &ret
->author_time
, &ret
->author_tz
);
1460 ret
->committer
= committer_buf
;
1461 get_ac_line(commit
->buffer
, "\ncommitter ",
1462 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1463 &ret
->committer_time
, &ret
->committer_tz
);
1465 ret
->summary
= summary_buf
;
1466 tmp
= strstr(commit
->buffer
, "\n\n");
1469 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1473 endp
= strchr(tmp
, '\n');
1475 endp
= tmp
+ strlen(tmp
);
1477 if (len
>= sizeof(summary_buf
) || len
== 0)
1479 memcpy(summary_buf
, tmp
, len
);
1480 summary_buf
[len
] = 0;
1484 * To allow LF and other nonportable characters in pathnames,
1485 * they are c-style quoted as needed.
1487 static void write_filename_info(const char *path
)
1489 printf("filename ");
1490 write_name_quoted(path
, stdout
, '\n');
1494 * Porcelain/Incremental format wants to show a lot of details per
1495 * commit. Instead of repeating this every line, emit it only once,
1496 * the first time each commit appears in the output.
1498 static int emit_one_suspect_detail(struct origin
*suspect
)
1500 struct commit_info ci
;
1502 if (suspect
->commit
->object
.flags
& METAINFO_SHOWN
)
1505 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1506 get_commit_info(suspect
->commit
, &ci
, 1);
1507 printf("author %s\n", ci
.author
);
1508 printf("author-mail %s\n", ci
.author_mail
);
1509 printf("author-time %lu\n", ci
.author_time
);
1510 printf("author-tz %s\n", ci
.author_tz
);
1511 printf("committer %s\n", ci
.committer
);
1512 printf("committer-mail %s\n", ci
.committer_mail
);
1513 printf("committer-time %lu\n", ci
.committer_time
);
1514 printf("committer-tz %s\n", ci
.committer_tz
);
1515 printf("summary %s\n", ci
.summary
);
1516 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1517 printf("boundary\n");
1522 * The blame_entry is found to be guilty for the range. Mark it
1523 * as such, and show it in incremental output.
1525 static void found_guilty_entry(struct blame_entry
*ent
)
1531 struct origin
*suspect
= ent
->suspect
;
1533 printf("%s %d %d %d\n",
1534 sha1_to_hex(suspect
->commit
->object
.sha1
),
1535 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1536 emit_one_suspect_detail(suspect
);
1537 write_filename_info(suspect
->path
);
1538 maybe_flush_or_die(stdout
, "stdout");
1543 * The main loop -- while the scoreboard has lines whose true origin
1544 * is still unknown, pick one blame_entry, and allow its current
1545 * suspect to pass blames to its parents.
1547 static void assign_blame(struct scoreboard
*sb
, int opt
)
1549 struct rev_info
*revs
= sb
->revs
;
1552 struct blame_entry
*ent
;
1553 struct commit
*commit
;
1554 struct origin
*suspect
= NULL
;
1556 /* find one suspect to break down */
1557 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1559 suspect
= ent
->suspect
;
1561 return; /* all done */
1564 * We will use this suspect later in the loop,
1565 * so hold onto it in the meantime.
1567 origin_incref(suspect
);
1568 commit
= suspect
->commit
;
1569 if (!commit
->object
.parsed
)
1570 parse_commit(commit
);
1572 (!(commit
->object
.flags
& UNINTERESTING
) &&
1573 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1574 pass_blame(sb
, suspect
, opt
);
1576 commit
->object
.flags
|= UNINTERESTING
;
1577 if (commit
->object
.parsed
)
1578 mark_parents_uninteresting(commit
);
1580 /* treat root commit as boundary */
1581 if (!commit
->parents
&& !show_root
)
1582 commit
->object
.flags
|= UNINTERESTING
;
1584 /* Take responsibility for the remaining entries */
1585 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1586 if (same_suspect(ent
->suspect
, suspect
))
1587 found_guilty_entry(ent
);
1588 origin_decref(suspect
);
1590 if (DEBUG
) /* sanity */
1591 sanity_check_refcnt(sb
);
1595 static const char *format_time(unsigned long time
, const char *tz_str
,
1598 static char time_buf
[128];
1603 if (show_raw_time
) {
1604 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1609 minutes
= tz
< 0 ? -tz
: tz
;
1610 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1611 minutes
= tz
< 0 ? -minutes
: minutes
;
1612 t
= time
+ minutes
* 60;
1615 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1616 strcat(time_buf
, tz_str
);
1620 #define OUTPUT_ANNOTATE_COMPAT 001
1621 #define OUTPUT_LONG_OBJECT_NAME 002
1622 #define OUTPUT_RAW_TIMESTAMP 004
1623 #define OUTPUT_PORCELAIN 010
1624 #define OUTPUT_SHOW_NAME 020
1625 #define OUTPUT_SHOW_NUMBER 040
1626 #define OUTPUT_SHOW_SCORE 0100
1627 #define OUTPUT_NO_AUTHOR 0200
1629 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1633 struct origin
*suspect
= ent
->suspect
;
1636 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1637 printf("%s%c%d %d %d\n",
1639 ent
->guilty
? ' ' : '*', // purely for debugging
1643 if (emit_one_suspect_detail(suspect
) ||
1644 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
1645 write_filename_info(suspect
->path
);
1647 cp
= nth_line(sb
, ent
->lno
);
1648 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1651 printf("%s %d %d\n", hex
,
1652 ent
->s_lno
+ 1 + cnt
,
1653 ent
->lno
+ 1 + cnt
);
1658 } while (ch
!= '\n' &&
1659 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1663 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1667 struct origin
*suspect
= ent
->suspect
;
1668 struct commit_info ci
;
1670 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1672 get_commit_info(suspect
->commit
, &ci
, 1);
1673 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1675 cp
= nth_line(sb
, ent
->lno
);
1676 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1678 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1680 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1682 memset(hex
, ' ', length
);
1683 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
1689 printf("%.*s", length
, hex
);
1690 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1691 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1692 format_time(ci
.author_time
, ci
.author_tz
,
1694 ent
->lno
+ 1 + cnt
);
1696 if (opt
& OUTPUT_SHOW_SCORE
)
1698 max_score_digits
, ent
->score
,
1699 ent
->suspect
->refcnt
);
1700 if (opt
& OUTPUT_SHOW_NAME
)
1701 printf(" %-*.*s", longest_file
, longest_file
,
1703 if (opt
& OUTPUT_SHOW_NUMBER
)
1704 printf(" %*d", max_orig_digits
,
1705 ent
->s_lno
+ 1 + cnt
);
1707 if (!(opt
& OUTPUT_NO_AUTHOR
))
1708 printf(" (%-*.*s %10s",
1709 longest_author
, longest_author
,
1711 format_time(ci
.author_time
,
1715 max_digits
, ent
->lno
+ 1 + cnt
);
1720 } while (ch
!= '\n' &&
1721 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1725 static void output(struct scoreboard
*sb
, int option
)
1727 struct blame_entry
*ent
;
1729 if (option
& OUTPUT_PORCELAIN
) {
1730 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1731 struct blame_entry
*oth
;
1732 struct origin
*suspect
= ent
->suspect
;
1733 struct commit
*commit
= suspect
->commit
;
1734 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1736 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1737 if ((oth
->suspect
->commit
!= commit
) ||
1738 !strcmp(oth
->suspect
->path
, suspect
->path
))
1740 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1746 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1747 if (option
& OUTPUT_PORCELAIN
)
1748 emit_porcelain(sb
, ent
);
1750 emit_other(sb
, ent
, option
);
1756 * To allow quick access to the contents of nth line in the
1757 * final image, prepare an index in the scoreboard.
1759 static int prepare_lines(struct scoreboard
*sb
)
1761 const char *buf
= sb
->final_buf
;
1762 unsigned long len
= sb
->final_buf_size
;
1763 int num
= 0, incomplete
= 0, bol
= 1;
1765 if (len
&& buf
[len
-1] != '\n')
1766 incomplete
++; /* incomplete line at the end */
1769 sb
->lineno
= xrealloc(sb
->lineno
,
1770 sizeof(int* ) * (num
+ 1));
1771 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1774 if (*buf
++ == '\n') {
1779 sb
->lineno
= xrealloc(sb
->lineno
,
1780 sizeof(int* ) * (num
+ incomplete
+ 1));
1781 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1782 sb
->num_lines
= num
+ incomplete
;
1783 return sb
->num_lines
;
1787 * Add phony grafts for use with -S; this is primarily to
1788 * support git's cvsserver that wants to give a linear history
1791 static int read_ancestry(const char *graft_file
)
1793 FILE *fp
= fopen(graft_file
, "r");
1797 while (fgets(buf
, sizeof(buf
), fp
)) {
1798 /* The format is just "Commit Parent1 Parent2 ...\n" */
1799 int len
= strlen(buf
);
1800 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1802 register_commit_graft(graft
, 0);
1809 * How many columns do we need to show line numbers in decimal?
1811 static int lineno_width(int lines
)
1815 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1821 * How many columns do we need to show line numbers, authors,
1824 static void find_alignment(struct scoreboard
*sb
, int *option
)
1826 int longest_src_lines
= 0;
1827 int longest_dst_lines
= 0;
1828 unsigned largest_score
= 0;
1829 struct blame_entry
*e
;
1831 for (e
= sb
->ent
; e
; e
= e
->next
) {
1832 struct origin
*suspect
= e
->suspect
;
1833 struct commit_info ci
;
1836 if (strcmp(suspect
->path
, sb
->path
))
1837 *option
|= OUTPUT_SHOW_NAME
;
1838 num
= strlen(suspect
->path
);
1839 if (longest_file
< num
)
1841 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1842 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1843 get_commit_info(suspect
->commit
, &ci
, 1);
1844 num
= strlen(ci
.author
);
1845 if (longest_author
< num
)
1846 longest_author
= num
;
1848 num
= e
->s_lno
+ e
->num_lines
;
1849 if (longest_src_lines
< num
)
1850 longest_src_lines
= num
;
1851 num
= e
->lno
+ e
->num_lines
;
1852 if (longest_dst_lines
< num
)
1853 longest_dst_lines
= num
;
1854 if (largest_score
< ent_score(sb
, e
))
1855 largest_score
= ent_score(sb
, e
);
1857 max_orig_digits
= lineno_width(longest_src_lines
);
1858 max_digits
= lineno_width(longest_dst_lines
);
1859 max_score_digits
= lineno_width(largest_score
);
1863 * For debugging -- origin is refcounted, and this asserts that
1864 * we do not underflow.
1866 static void sanity_check_refcnt(struct scoreboard
*sb
)
1869 struct blame_entry
*ent
;
1871 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1872 /* Nobody should have zero or negative refcnt */
1873 if (ent
->suspect
->refcnt
<= 0) {
1874 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1876 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1877 ent
->suspect
->refcnt
);
1881 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1882 /* Mark the ones that haven't been checked */
1883 if (0 < ent
->suspect
->refcnt
)
1884 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1886 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1888 * ... then pick each and see if they have the the
1892 struct blame_entry
*e
;
1893 struct origin
*suspect
= ent
->suspect
;
1895 if (0 < suspect
->refcnt
)
1897 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1898 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1899 if (e
->suspect
!= suspect
)
1903 if (suspect
->refcnt
!= found
) {
1904 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1906 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1907 ent
->suspect
->refcnt
, found
);
1913 find_alignment(sb
, &opt
);
1915 die("Baa %d!", baa
);
1920 * Used for the command line parsing; check if the path exists
1921 * in the working tree.
1923 static int has_string_in_work_tree(const char *path
)
1926 return !lstat(path
, &st
);
1929 static unsigned parse_score(const char *arg
)
1932 unsigned long score
= strtoul(arg
, &end
, 10);
1938 static const char *add_prefix(const char *prefix
, const char *path
)
1940 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
1944 * Parsing of (comma separated) one item in the -L option
1946 static const char *parse_loc(const char *spec
,
1947 struct scoreboard
*sb
, long lno
,
1948 long begin
, long *ret
)
1955 regmatch_t match
[1];
1957 /* Allow "-L <something>,+20" to mean starting at <something>
1958 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1961 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1962 num
= strtol(spec
+ 1, &term
, 10);
1963 if (term
!= spec
+ 1) {
1967 *ret
= begin
+ num
- 2;
1976 num
= strtol(spec
, &term
, 10);
1984 /* it could be a regexp of form /.../ */
1985 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1992 /* try [spec+1 .. term-1] as regexp */
1994 begin
--; /* input is in human terms */
1995 line
= nth_line(sb
, begin
);
1997 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1998 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1999 const char *cp
= line
+ match
[0].rm_so
;
2002 while (begin
++ < lno
) {
2003 nline
= nth_line(sb
, begin
);
2004 if (line
<= cp
&& cp
< nline
)
2015 regerror(reg_error
, ®exp
, errbuf
, 1024);
2016 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
2021 * Parsing of -L option
2023 static void prepare_blame_range(struct scoreboard
*sb
,
2024 const char *bottomtop
,
2026 long *bottom
, long *top
)
2030 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
2032 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
2040 static int git_blame_config(const char *var
, const char *value
, void *cb
)
2042 if (!strcmp(var
, "blame.showroot")) {
2043 show_root
= git_config_bool(var
, value
);
2046 if (!strcmp(var
, "blame.blankboundary")) {
2047 blank_boundary
= git_config_bool(var
, value
);
2050 return git_default_config(var
, value
, cb
);
2054 * Prepare a dummy commit that represents the work tree (or staged) item.
2055 * Note that annotating work tree item never works in the reverse.
2057 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2059 struct commit
*commit
;
2060 struct origin
*origin
;
2061 unsigned char head_sha1
[20];
2062 struct strbuf buf
= STRBUF_INIT
;
2066 struct cache_entry
*ce
;
2069 if (get_sha1("HEAD", head_sha1
))
2070 die("No such ref: HEAD");
2073 commit
= xcalloc(1, sizeof(*commit
));
2074 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2075 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2076 commit
->object
.parsed
= 1;
2078 commit
->object
.type
= OBJ_COMMIT
;
2080 origin
= make_origin(commit
, path
);
2082 if (!contents_from
|| strcmp("-", contents_from
)) {
2084 const char *read_from
;
2085 unsigned long fin_size
;
2087 if (contents_from
) {
2088 if (stat(contents_from
, &st
) < 0)
2089 die("Cannot stat %s", contents_from
);
2090 read_from
= contents_from
;
2093 if (lstat(path
, &st
) < 0)
2094 die("Cannot lstat %s", path
);
2097 fin_size
= xsize_t(st
.st_size
);
2098 mode
= canon_mode(st
.st_mode
);
2099 switch (st
.st_mode
& S_IFMT
) {
2101 if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2102 die("cannot open or read %s", read_from
);
2105 if (readlink(read_from
, buf
.buf
, buf
.alloc
) != fin_size
)
2106 die("cannot readlink %s", read_from
);
2110 die("unsupported file type %s", read_from
);
2114 /* Reading from stdin */
2115 contents_from
= "standard input";
2117 if (strbuf_read(&buf
, 0, 0) < 0)
2118 die("read error %s from stdin", strerror(errno
));
2120 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2121 origin
->file
.ptr
= buf
.buf
;
2122 origin
->file
.size
= buf
.len
;
2123 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2124 commit
->util
= origin
;
2127 * Read the current index, replace the path entry with
2128 * origin->blob_sha1 without mucking with its mode or type
2129 * bits; we are not going to write this index out -- we just
2130 * want to run "diff-index --cached".
2137 int pos
= cache_name_pos(path
, len
);
2139 mode
= active_cache
[pos
]->ce_mode
;
2141 /* Let's not bother reading from HEAD tree */
2142 mode
= S_IFREG
| 0644;
2144 size
= cache_entry_size(len
);
2145 ce
= xcalloc(1, size
);
2146 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2147 memcpy(ce
->name
, path
, len
);
2148 ce
->ce_flags
= create_ce_flags(len
, 0);
2149 ce
->ce_mode
= create_ce_mode(mode
);
2150 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2153 * We are not going to write this out, so this does not matter
2154 * right now, but someday we might optimize diff-index --cached
2155 * with cache-tree information.
2157 cache_tree_invalidate_path(active_cache_tree
, path
);
2159 commit
->buffer
= xmalloc(400);
2160 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2161 snprintf(commit
->buffer
, 400,
2162 "tree 0000000000000000000000000000000000000000\n"
2166 "Version of %s from %s\n",
2167 sha1_to_hex(head_sha1
),
2168 ident
, ident
, path
, contents_from
? contents_from
: path
);
2172 static const char *prepare_final(struct scoreboard
*sb
)
2175 const char *final_commit_name
= NULL
;
2176 struct rev_info
*revs
= sb
->revs
;
2179 * There must be one and only one positive commit in the
2180 * revs->pending array.
2182 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2183 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2184 if (obj
->flags
& UNINTERESTING
)
2186 while (obj
->type
== OBJ_TAG
)
2187 obj
= deref_tag(obj
, NULL
, 0);
2188 if (obj
->type
!= OBJ_COMMIT
)
2189 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2191 die("More than one commit to dig from %s and %s?",
2192 revs
->pending
.objects
[i
].name
,
2194 sb
->final
= (struct commit
*) obj
;
2195 final_commit_name
= revs
->pending
.objects
[i
].name
;
2197 return final_commit_name
;
2200 static const char *prepare_initial(struct scoreboard
*sb
)
2203 const char *final_commit_name
= NULL
;
2204 struct rev_info
*revs
= sb
->revs
;
2207 * There must be one and only one negative commit, and it must be
2210 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2211 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2212 if (!(obj
->flags
& UNINTERESTING
))
2214 while (obj
->type
== OBJ_TAG
)
2215 obj
= deref_tag(obj
, NULL
, 0);
2216 if (obj
->type
!= OBJ_COMMIT
)
2217 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2219 die("More than one commit to dig down to %s and %s?",
2220 revs
->pending
.objects
[i
].name
,
2222 sb
->final
= (struct commit
*) obj
;
2223 final_commit_name
= revs
->pending
.objects
[i
].name
;
2225 if (!final_commit_name
)
2226 die("No commit to dig down to?");
2227 return final_commit_name
;
2230 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2232 int *opt
= option
->value
;
2235 * -C enables copy from removed files;
2236 * -C -C enables copy from existing files, but only
2237 * when blaming a new file;
2238 * -C -C -C enables copy from existing files for
2241 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2242 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2243 if (*opt
& PICKAXE_BLAME_COPY
)
2244 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2245 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2248 blame_copy_score
= parse_score(arg
);
2252 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2254 int *opt
= option
->value
;
2256 *opt
|= PICKAXE_BLAME_MOVE
;
2259 blame_move_score
= parse_score(arg
);
2263 static int blame_bottomtop_callback(const struct option
*option
, const char *arg
, int unset
)
2265 const char **bottomtop
= option
->value
;
2269 die("More than one '-L n,m' option given");
2274 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2276 struct rev_info revs
;
2278 struct scoreboard sb
;
2280 struct blame_entry
*ent
;
2281 long dashdash_pos
, bottom
, top
, lno
;
2282 const char *final_commit_name
= NULL
;
2283 enum object_type type
;
2285 static const char *bottomtop
= NULL
;
2286 static int output_option
= 0, opt
= 0;
2287 static int show_stats
= 0;
2288 static const char *revs_file
= NULL
;
2289 static const char *contents_from
= NULL
;
2290 static const struct option options
[] = {
2291 OPT_BOOLEAN(0, "incremental", &incremental
, "Show blame entries as we find them, incrementally"),
2292 OPT_BOOLEAN('b', NULL
, &blank_boundary
, "Show blank SHA-1 for boundary commits (Default: off)"),
2293 OPT_BOOLEAN(0, "root", &show_root
, "Do not treat root commits as boundaries (Default: off)"),
2294 OPT_BOOLEAN(0, "show-stats", &show_stats
, "Show work cost statistics"),
2295 OPT_BIT(0, "score-debug", &output_option
, "Show output score for blame entries", OUTPUT_SHOW_SCORE
),
2296 OPT_BIT('f', "show-name", &output_option
, "Show original filename (Default: auto)", OUTPUT_SHOW_NAME
),
2297 OPT_BIT('n', "show-number", &output_option
, "Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER
),
2298 OPT_BIT('p', "porcelain", &output_option
, "Show in a format designed for machine consumption", OUTPUT_PORCELAIN
),
2299 OPT_BIT('c', NULL
, &output_option
, "Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT
),
2300 OPT_BIT('t', NULL
, &output_option
, "Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP
),
2301 OPT_BIT('l', NULL
, &output_option
, "Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME
),
2302 OPT_BIT('s', NULL
, &output_option
, "Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR
),
2303 OPT_BIT('w', NULL
, &xdl_opts
, "Ignore whitespace differences", XDF_IGNORE_WHITESPACE
),
2304 OPT_STRING('S', NULL
, &revs_file
, "file", "Use revisions from <file> instead of calling git-rev-list"),
2305 OPT_STRING(0, "contents", &contents_from
, "file", "Use <file>'s contents as the final image"),
2306 { OPTION_CALLBACK
, 'C', NULL
, &opt
, "score", "Find line copies within and across files", PARSE_OPT_OPTARG
, blame_copy_callback
},
2307 { OPTION_CALLBACK
, 'M', NULL
, &opt
, "score", "Find line movements within and across files", PARSE_OPT_OPTARG
, blame_move_callback
},
2308 OPT_CALLBACK('L', NULL
, &bottomtop
, "n,m", "Process only line range n,m, counting from 1", blame_bottomtop_callback
),
2312 struct parse_opt_ctx_t ctx
;
2313 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2315 git_config(git_blame_config
, NULL
);
2316 init_revisions(&revs
, NULL
);
2317 save_commit_buffer
= 0;
2320 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
2321 PARSE_OPT_KEEP_ARGV0
);
2323 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2324 case PARSE_OPT_HELP
:
2326 case PARSE_OPT_DONE
:
2328 dashdash_pos
= ctx
.cpidx
;
2332 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2333 ctx
.argv
[0] = "--children";
2336 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2339 argc
= parse_options_end(&ctx
);
2341 if (cmd_is_annotate
)
2342 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2344 if (DIFF_OPT_TST(&revs
.diffopt
, FIND_COPIES_HARDER
))
2345 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
2346 PICKAXE_BLAME_COPY_HARDER
);
2348 if (!blame_move_score
)
2349 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2350 if (!blame_copy_score
)
2351 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2354 * We have collected options unknown to us in argv[1..unk]
2355 * which are to be passed to revision machinery if we are
2356 * going to do the "bottom" processing.
2358 * The remaining are:
2360 * (1) if dashdash_pos != 0, its either
2361 * "blame [revisions] -- <path>" or
2362 * "blame -- <path> <rev>"
2364 * (2) otherwise, its one of the two:
2365 * "blame [revisions] <path>"
2366 * "blame <path> <rev>"
2368 * Note that we must strip out <path> from the arguments: we do not
2369 * want the path pruning but we may want "bottom" processing.
2372 switch (argc
- dashdash_pos
- 1) {
2375 usage_with_options(blame_opt_usage
, options
);
2376 /* reorder for the new way: <rev> -- <path> */
2382 path
= add_prefix(prefix
, argv
[--argc
]);
2386 usage_with_options(blame_opt_usage
, options
);
2390 usage_with_options(blame_opt_usage
, options
);
2391 path
= add_prefix(prefix
, argv
[argc
- 1]);
2392 if (argc
== 3 && !has_string_in_work_tree(path
)) { /* (2b) */
2393 path
= add_prefix(prefix
, argv
[1]);
2396 argv
[argc
- 1] = "--";
2399 if (!has_string_in_work_tree(path
))
2400 die("cannot stat path %s: %s", path
, strerror(errno
));
2403 setup_revisions(argc
, argv
, &revs
, NULL
);
2404 memset(&sb
, 0, sizeof(sb
));
2408 final_commit_name
= prepare_final(&sb
);
2409 else if (contents_from
)
2410 die("--contents and --children do not blend well.");
2412 final_commit_name
= prepare_initial(&sb
);
2416 * "--not A B -- path" without anything positive;
2417 * do not default to HEAD, but use the working tree
2421 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2422 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2424 else if (contents_from
)
2425 die("Cannot use --contents with final commit object name");
2428 * If we have bottom, this will mark the ancestors of the
2429 * bottom commits we would reach while traversing as
2432 if (prepare_revision_walk(&revs
))
2433 die("revision walk setup failed");
2435 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2438 buf
= xmalloc(o
->file
.size
+ 1);
2439 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2441 sb
.final_buf_size
= o
->file
.size
;
2444 o
= get_origin(&sb
, sb
.final
, path
);
2445 if (fill_blob_sha1(o
))
2446 die("no such path %s in %s", path
, final_commit_name
);
2448 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2449 &sb
.final_buf_size
);
2451 die("Cannot read blob %s for path %s",
2452 sha1_to_hex(o
->blob_sha1
),
2456 lno
= prepare_lines(&sb
);
2460 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2461 if (bottom
&& top
&& top
< bottom
) {
2463 tmp
= top
; top
= bottom
; bottom
= tmp
;
2471 die("file %s has only %lu lines", path
, lno
);
2473 ent
= xcalloc(1, sizeof(*ent
));
2475 ent
->num_lines
= top
- bottom
;
2477 ent
->s_lno
= bottom
;
2482 if (revs_file
&& read_ancestry(revs_file
))
2483 die("reading graft file %s failed: %s",
2484 revs_file
, strerror(errno
));
2486 read_mailmap(&mailmap
, ".mailmap", NULL
);
2491 assign_blame(&sb
, opt
);
2498 if (!(output_option
& OUTPUT_PORCELAIN
))
2499 find_alignment(&sb
, &output_option
);
2501 output(&sb
, output_option
);
2502 free((void *)sb
.final_buf
);
2503 for (ent
= sb
.ent
; ent
; ) {
2504 struct blame_entry
*e
= ent
->next
;
2510 printf("num read blob: %d\n", num_read_blob
);
2511 printf("num get patch: %d\n", num_get_patch
);
2512 printf("num commits: %d\n", num_commits
);