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 cmd_is_annotate
;
42 static int xdl_opts
= XDF_NEED_MINIMAL
;
43 static struct string_list mailmap
;
50 static int num_read_blob
;
51 static int num_get_patch
;
52 static int num_commits
;
54 #define PICKAXE_BLAME_MOVE 01
55 #define PICKAXE_BLAME_COPY 02
56 #define PICKAXE_BLAME_COPY_HARDER 04
57 #define PICKAXE_BLAME_COPY_HARDEST 010
60 * blame for a blame_entry with score lower than these thresholds
61 * is not passed to the parent using move/copy logic.
63 static unsigned blame_move_score
;
64 static unsigned blame_copy_score
;
65 #define BLAME_DEFAULT_MOVE_SCORE 20
66 #define BLAME_DEFAULT_COPY_SCORE 40
68 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
69 #define METAINFO_SHOWN (1u<<12)
70 #define MORE_THAN_ONE_PATH (1u<<13)
73 * One blob in a commit that is being suspected
77 struct commit
*commit
;
79 unsigned char blob_sha1
[20];
80 char path
[FLEX_ARRAY
];
84 * Given an origin, prepare mmfile_t structure to be used by the
87 static void fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
90 enum object_type type
;
92 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
93 (unsigned long *)(&(file
->size
)));
95 die("Cannot read blob %s for path %s",
96 sha1_to_hex(o
->blob_sha1
),
105 * Origin is refcounted and usually we keep the blob contents to be
108 static inline struct origin
*origin_incref(struct origin
*o
)
115 static void origin_decref(struct origin
*o
)
117 if (o
&& --o
->refcnt
<= 0) {
123 static void drop_origin_blob(struct origin
*o
)
132 * Each group of lines is described by a blame_entry; it can be split
133 * as we pass blame to the parents. They form a linked list in the
134 * scoreboard structure, sorted by the target line number.
137 struct blame_entry
*prev
;
138 struct blame_entry
*next
;
140 /* the first line of this group in the final image;
141 * internally all line numbers are 0 based.
145 /* how many lines this group has */
148 /* the commit that introduced this group into the final image */
149 struct origin
*suspect
;
151 /* true if the suspect is truly guilty; false while we have not
152 * checked if the group came from one of its parents.
156 /* true if the entry has been scanned for copies in the current parent
160 /* the line number of the first line of this group in the
161 * suspect's file; internally all line numbers are 0 based.
165 /* how significant this entry is -- cached to avoid
166 * scanning the lines over and over.
172 * The current state of the blame assignment.
175 /* the final commit (i.e. where we started digging from) */
176 struct commit
*final
;
177 struct rev_info
*revs
;
181 * The contents in the final image.
182 * Used by many functions to obtain contents of the nth line,
183 * indexed with scoreboard.lineno[blame_entry.lno].
185 const char *final_buf
;
186 unsigned long final_buf_size
;
188 /* linked list of blames */
189 struct blame_entry
*ent
;
191 /* look-up a line in the final buffer */
196 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
200 if (a
->commit
!= b
->commit
)
202 return !strcmp(a
->path
, b
->path
);
205 static void sanity_check_refcnt(struct scoreboard
*);
208 * If two blame entries that are next to each other came from
209 * contiguous lines in the same origin (i.e. <commit, path> pair),
210 * merge them together.
212 static void coalesce(struct scoreboard
*sb
)
214 struct blame_entry
*ent
, *next
;
216 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
217 if (same_suspect(ent
->suspect
, next
->suspect
) &&
218 ent
->guilty
== next
->guilty
&&
219 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
220 ent
->num_lines
+= next
->num_lines
;
221 ent
->next
= next
->next
;
223 ent
->next
->prev
= ent
;
224 origin_decref(next
->suspect
);
227 next
= ent
; /* again */
231 if (DEBUG
) /* sanity */
232 sanity_check_refcnt(sb
);
236 * Given a commit and a path in it, create a new origin structure.
237 * The callers that add blame to the scoreboard should use
238 * get_origin() to obtain shared, refcounted copy instead of calling
239 * this function directly.
241 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
244 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
247 strcpy(o
->path
, path
);
252 * Locate an existing origin or create a new one.
254 static struct origin
*get_origin(struct scoreboard
*sb
,
255 struct commit
*commit
,
258 struct blame_entry
*e
;
260 for (e
= sb
->ent
; e
; e
= e
->next
) {
261 if (e
->suspect
->commit
== commit
&&
262 !strcmp(e
->suspect
->path
, path
))
263 return origin_incref(e
->suspect
);
265 return make_origin(commit
, path
);
269 * Fill the blob_sha1 field of an origin if it hasn't, so that later
270 * call to fill_origin_blob() can use it to locate the data. blob_sha1
271 * for an origin is also used to pass the blame for the entire file to
272 * the parent to detect the case where a child's blob is identical to
273 * that of its parent's.
275 static int fill_blob_sha1(struct origin
*origin
)
279 if (!is_null_sha1(origin
->blob_sha1
))
281 if (get_tree_entry(origin
->commit
->object
.sha1
,
283 origin
->blob_sha1
, &mode
))
285 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
289 hashclr(origin
->blob_sha1
);
294 * We have an origin -- check if the same path exists in the
295 * parent and return an origin structure to represent it.
297 static struct origin
*find_origin(struct scoreboard
*sb
,
298 struct commit
*parent
,
299 struct origin
*origin
)
301 struct origin
*porigin
= NULL
;
302 struct diff_options diff_opts
;
303 const char *paths
[2];
307 * Each commit object can cache one origin in that
308 * commit. This is a freestanding copy of origin and
311 struct origin
*cached
= parent
->util
;
312 if (!strcmp(cached
->path
, origin
->path
)) {
314 * The same path between origin and its parent
315 * without renaming -- the most common case.
317 porigin
= get_origin(sb
, parent
, cached
->path
);
320 * If the origin was newly created (i.e. get_origin
321 * would call make_origin if none is found in the
322 * scoreboard), it does not know the blob_sha1,
323 * so copy it. Otherwise porigin was in the
324 * scoreboard and already knows blob_sha1.
326 if (porigin
->refcnt
== 1)
327 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
330 /* otherwise it was not very useful; free it */
335 /* See if the origin->path is different between parent
336 * and origin first. Most of the time they are the
337 * same and diff-tree is fairly efficient about this.
339 diff_setup(&diff_opts
);
340 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
341 diff_opts
.detect_rename
= 0;
342 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
343 paths
[0] = origin
->path
;
346 diff_tree_setup_paths(paths
, &diff_opts
);
347 if (diff_setup_done(&diff_opts
) < 0)
350 if (is_null_sha1(origin
->commit
->object
.sha1
))
351 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
353 diff_tree_sha1(parent
->tree
->object
.sha1
,
354 origin
->commit
->tree
->object
.sha1
,
356 diffcore_std(&diff_opts
);
358 /* It is either one entry that says "modified", or "created",
361 if (!diff_queued_diff
.nr
) {
362 /* The path is the same as parent */
363 porigin
= get_origin(sb
, parent
, origin
->path
);
364 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
366 else if (diff_queued_diff
.nr
!= 1)
367 die("internal error in blame::find_origin");
369 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
372 die("internal error in blame::find_origin (%c)",
375 porigin
= get_origin(sb
, parent
, origin
->path
);
376 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
380 /* Did not exist in parent, or type changed */
384 diff_flush(&diff_opts
);
385 diff_tree_release_paths(&diff_opts
);
388 * Create a freestanding copy that is not part of
389 * the refcounted origin found in the scoreboard, and
390 * cache it in the commit.
392 struct origin
*cached
;
394 cached
= make_origin(porigin
->commit
, porigin
->path
);
395 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
396 parent
->util
= cached
;
402 * We have an origin -- find the path that corresponds to it in its
403 * parent and return an origin structure to represent it.
405 static struct origin
*find_rename(struct scoreboard
*sb
,
406 struct commit
*parent
,
407 struct origin
*origin
)
409 struct origin
*porigin
= NULL
;
410 struct diff_options diff_opts
;
412 const char *paths
[2];
414 diff_setup(&diff_opts
);
415 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
416 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
417 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
418 diff_opts
.single_follow
= origin
->path
;
420 diff_tree_setup_paths(paths
, &diff_opts
);
421 if (diff_setup_done(&diff_opts
) < 0)
424 if (is_null_sha1(origin
->commit
->object
.sha1
))
425 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
427 diff_tree_sha1(parent
->tree
->object
.sha1
,
428 origin
->commit
->tree
->object
.sha1
,
430 diffcore_std(&diff_opts
);
432 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
433 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
434 if ((p
->status
== 'R' || p
->status
== 'C') &&
435 !strcmp(p
->two
->path
, origin
->path
)) {
436 porigin
= get_origin(sb
, parent
, p
->one
->path
);
437 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
441 diff_flush(&diff_opts
);
442 diff_tree_release_paths(&diff_opts
);
447 * Parsing of patch chunks...
450 /* line number in postimage; up to but not including this
451 * line is the same as preimage
455 /* preimage line number after this chunk */
458 /* postimage line number after this chunk */
463 struct chunk
*chunks
;
467 struct blame_diff_state
{
469 unsigned hunk_post_context
;
470 unsigned hunk_in_pre_context
: 1;
473 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
475 struct blame_diff_state
*state
= state_
;
477 int off1
, off2
, len1
, len2
, num
;
479 num
= state
->ret
->num
;
480 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
481 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
482 state
->ret
->chunks
[num
- 1].same
++;
484 state
->hunk_in_pre_context
= 0;
486 state
->hunk_post_context
++;
488 state
->hunk_post_context
= 0;
493 if (num
&& state
->hunk_post_context
) {
494 chunk
= &state
->ret
->chunks
[num
- 1];
495 chunk
->p_next
-= state
->hunk_post_context
;
496 chunk
->t_next
-= state
->hunk_post_context
;
498 state
->ret
->num
= ++num
;
499 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
500 sizeof(struct chunk
) * num
);
501 chunk
= &state
->ret
->chunks
[num
- 1];
502 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
507 /* Line numbers in patch output are one based. */
511 chunk
->same
= len2
? off2
: (off2
+ 1);
513 chunk
->p_next
= off1
+ (len1
? len1
: 1);
514 chunk
->t_next
= chunk
->same
+ len2
;
515 state
->hunk_in_pre_context
= 1;
516 state
->hunk_post_context
= 0;
519 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
522 struct blame_diff_state state
;
527 xpp
.flags
= xdl_opts
;
528 memset(&xecfg
, 0, sizeof(xecfg
));
529 xecfg
.ctxlen
= context
;
530 memset(&state
, 0, sizeof(state
));
531 state
.ret
= xmalloc(sizeof(struct patch
));
532 state
.ret
->chunks
= NULL
;
535 xdi_diff_outf(file_p
, file_o
, process_u_diff
, &state
, &xpp
, &xecfg
, &ecb
);
537 if (state
.ret
->num
) {
539 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
540 chunk
->p_next
-= state
.hunk_post_context
;
541 chunk
->t_next
-= state
.hunk_post_context
;
547 * Run diff between two origins and grab the patch output, so that
548 * we can pass blame for lines origin is currently suspected for
551 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
553 mmfile_t file_p
, file_o
;
556 fill_origin_blob(parent
, &file_p
);
557 fill_origin_blob(origin
, &file_o
);
558 if (!file_p
.ptr
|| !file_o
.ptr
)
560 patch
= compare_buffer(&file_p
, &file_o
, 0);
565 static void free_patch(struct patch
*p
)
572 * Link in a new blame entry to the scoreboard. Entries that cover the
573 * same line range have been removed from the scoreboard previously.
575 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
577 struct blame_entry
*ent
, *prev
= NULL
;
579 origin_incref(e
->suspect
);
581 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
584 /* prev, if not NULL, is the last one that is below e */
587 e
->next
= prev
->next
;
599 * src typically is on-stack; we want to copy the information in it to
600 * a malloced blame_entry that is already on the linked list of the
601 * scoreboard. The origin of dst loses a refcnt while the origin of src
604 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
606 struct blame_entry
*p
, *n
;
610 origin_incref(src
->suspect
);
611 origin_decref(dst
->suspect
);
612 memcpy(dst
, src
, sizeof(*src
));
618 static const char *nth_line(struct scoreboard
*sb
, int lno
)
620 return sb
->final_buf
+ sb
->lineno
[lno
];
624 * It is known that lines between tlno to same came from parent, and e
625 * has an overlap with that range. it also is known that parent's
626 * line plno corresponds to e's line tlno.
632 * <------------------>
634 * Split e into potentially three parts; before this chunk, the chunk
635 * to be blamed for the parent, and after that portion.
637 static void split_overlap(struct blame_entry
*split
,
638 struct blame_entry
*e
,
639 int tlno
, int plno
, int same
,
640 struct origin
*parent
)
643 memset(split
, 0, sizeof(struct blame_entry
[3]));
645 if (e
->s_lno
< tlno
) {
646 /* there is a pre-chunk part not blamed on parent */
647 split
[0].suspect
= origin_incref(e
->suspect
);
648 split
[0].lno
= e
->lno
;
649 split
[0].s_lno
= e
->s_lno
;
650 split
[0].num_lines
= tlno
- e
->s_lno
;
651 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
652 split
[1].s_lno
= plno
;
655 split
[1].lno
= e
->lno
;
656 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
659 if (same
< e
->s_lno
+ e
->num_lines
) {
660 /* there is a post-chunk part not blamed on parent */
661 split
[2].suspect
= origin_incref(e
->suspect
);
662 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
663 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
664 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
665 chunk_end_lno
= split
[2].lno
;
668 chunk_end_lno
= e
->lno
+ e
->num_lines
;
669 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
672 * if it turns out there is nothing to blame the parent for,
673 * forget about the splitting. !split[1].suspect signals this.
675 if (split
[1].num_lines
< 1)
677 split
[1].suspect
= origin_incref(parent
);
681 * split_overlap() divided an existing blame e into up to three parts
682 * in split. Adjust the linked list of blames in the scoreboard to
685 static void split_blame(struct scoreboard
*sb
,
686 struct blame_entry
*split
,
687 struct blame_entry
*e
)
689 struct blame_entry
*new_entry
;
691 if (split
[0].suspect
&& split
[2].suspect
) {
692 /* The first part (reuse storage for the existing entry e) */
693 dup_entry(e
, &split
[0]);
695 /* The last part -- me */
696 new_entry
= xmalloc(sizeof(*new_entry
));
697 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
698 add_blame_entry(sb
, new_entry
);
700 /* ... and the middle part -- parent */
701 new_entry
= xmalloc(sizeof(*new_entry
));
702 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
703 add_blame_entry(sb
, new_entry
);
705 else if (!split
[0].suspect
&& !split
[2].suspect
)
707 * The parent covers the entire area; reuse storage for
708 * e and replace it with the parent.
710 dup_entry(e
, &split
[1]);
711 else if (split
[0].suspect
) {
712 /* me and then parent */
713 dup_entry(e
, &split
[0]);
715 new_entry
= xmalloc(sizeof(*new_entry
));
716 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
717 add_blame_entry(sb
, new_entry
);
720 /* parent and then me */
721 dup_entry(e
, &split
[1]);
723 new_entry
= xmalloc(sizeof(*new_entry
));
724 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
725 add_blame_entry(sb
, new_entry
);
728 if (DEBUG
) { /* sanity */
729 struct blame_entry
*ent
;
730 int lno
= sb
->ent
->lno
, corrupt
= 0;
732 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
737 lno
+= ent
->num_lines
;
741 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
742 printf("L %8d l %8d n %8d\n",
743 lno
, ent
->lno
, ent
->num_lines
);
744 lno
= ent
->lno
+ ent
->num_lines
;
752 * After splitting the blame, the origins used by the
753 * on-stack blame_entry should lose one refcnt each.
755 static void decref_split(struct blame_entry
*split
)
759 for (i
= 0; i
< 3; i
++)
760 origin_decref(split
[i
].suspect
);
764 * Helper for blame_chunk(). blame_entry e is known to overlap with
765 * the patch hunk; split it and pass blame to the parent.
767 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
768 int tlno
, int plno
, int same
,
769 struct origin
*parent
)
771 struct blame_entry split
[3];
773 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
774 if (split
[1].suspect
)
775 split_blame(sb
, split
, e
);
780 * Find the line number of the last line the target is suspected for.
782 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
784 struct blame_entry
*e
;
785 int last_in_target
= -1;
787 for (e
= sb
->ent
; e
; e
= e
->next
) {
788 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
790 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
791 last_in_target
= e
->s_lno
+ e
->num_lines
;
793 return last_in_target
;
797 * Process one hunk from the patch between the current suspect for
798 * blame_entry e and its parent. Find and split the overlap, and
799 * pass blame to the overlapping part to the parent.
801 static void blame_chunk(struct scoreboard
*sb
,
802 int tlno
, int plno
, int same
,
803 struct origin
*target
, struct origin
*parent
)
805 struct blame_entry
*e
;
807 for (e
= sb
->ent
; e
; e
= e
->next
) {
808 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
810 if (same
<= e
->s_lno
)
812 if (tlno
< e
->s_lno
+ e
->num_lines
)
813 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
818 * We are looking at the origin 'target' and aiming to pass blame
819 * for the lines it is suspected to its parent. Run diff to find
820 * which lines came from parent and pass blame for them.
822 static int pass_blame_to_parent(struct scoreboard
*sb
,
823 struct origin
*target
,
824 struct origin
*parent
)
826 int i
, last_in_target
, plno
, tlno
;
829 last_in_target
= find_last_in_target(sb
, target
);
830 if (last_in_target
< 0)
831 return 1; /* nothing remains for this target */
833 patch
= get_patch(parent
, target
);
835 for (i
= 0; i
< patch
->num
; i
++) {
836 struct chunk
*chunk
= &patch
->chunks
[i
];
838 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
839 plno
= chunk
->p_next
;
840 tlno
= chunk
->t_next
;
842 /* The rest (i.e. anything after tlno) are the same as the parent */
843 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
850 * The lines in blame_entry after splitting blames many times can become
851 * very small and trivial, and at some point it becomes pointless to
852 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
853 * ordinary C program, and it is not worth to say it was copied from
854 * totally unrelated file in the parent.
856 * Compute how trivial the lines in the blame_entry are.
858 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
867 cp
= nth_line(sb
, e
->lno
);
868 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
870 unsigned ch
= *((unsigned char *)cp
);
880 * best_so_far[] and this[] are both a split of an existing blame_entry
881 * that passes blame to the parent. Maintain best_so_far the best split
882 * so far, by comparing this and best_so_far and copying this into
883 * bst_so_far as needed.
885 static void copy_split_if_better(struct scoreboard
*sb
,
886 struct blame_entry
*best_so_far
,
887 struct blame_entry
*this)
891 if (!this[1].suspect
)
893 if (best_so_far
[1].suspect
) {
894 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
898 for (i
= 0; i
< 3; i
++)
899 origin_incref(this[i
].suspect
);
900 decref_split(best_so_far
);
901 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
905 * We are looking at a part of the final image represented by
906 * ent (tlno and same are offset by ent->s_lno).
907 * tlno is where we are looking at in the final image.
908 * up to (but not including) same match preimage.
909 * plno is where we are looking at in the preimage.
911 * <-------------- final image ---------------------->
914 * <---------preimage----->
917 * All line numbers are 0-based.
919 static void handle_split(struct scoreboard
*sb
,
920 struct blame_entry
*ent
,
921 int tlno
, int plno
, int same
,
922 struct origin
*parent
,
923 struct blame_entry
*split
)
925 if (ent
->num_lines
<= tlno
)
928 struct blame_entry
this[3];
931 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
932 copy_split_if_better(sb
, split
, this);
938 * Find the lines from parent that are the same as ent so that
939 * we can pass blames to it. file_p has the blob contents for
942 static void find_copy_in_blob(struct scoreboard
*sb
,
943 struct blame_entry
*ent
,
944 struct origin
*parent
,
945 struct blame_entry
*split
,
955 * Prepare mmfile that contains only the lines in ent.
957 cp
= nth_line(sb
, ent
->lno
);
958 file_o
.ptr
= (char*) cp
;
959 cnt
= ent
->num_lines
;
961 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
965 file_o
.size
= cp
- file_o
.ptr
;
967 patch
= compare_buffer(file_p
, &file_o
, 1);
970 * file_o is a part of final image we are annotating.
971 * file_p partially may match that image.
973 memset(split
, 0, sizeof(struct blame_entry
[3]));
975 for (i
= 0; i
< patch
->num
; i
++) {
976 struct chunk
*chunk
= &patch
->chunks
[i
];
978 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
979 plno
= chunk
->p_next
;
980 tlno
= chunk
->t_next
;
982 /* remainder, if any, all match the preimage */
983 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
988 * See if lines currently target is suspected for can be attributed to
991 static int find_move_in_parent(struct scoreboard
*sb
,
992 struct origin
*target
,
993 struct origin
*parent
)
995 int last_in_target
, made_progress
;
996 struct blame_entry
*e
, split
[3];
999 last_in_target
= find_last_in_target(sb
, target
);
1000 if (last_in_target
< 0)
1001 return 1; /* nothing remains for this target */
1003 fill_origin_blob(parent
, &file_p
);
1008 while (made_progress
) {
1010 for (e
= sb
->ent
; e
; e
= e
->next
) {
1011 if (e
->guilty
|| !same_suspect(e
->suspect
, target
) ||
1012 ent_score(sb
, e
) < blame_move_score
)
1014 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1015 if (split
[1].suspect
&&
1016 blame_move_score
< ent_score(sb
, &split
[1])) {
1017 split_blame(sb
, split
, e
);
1020 decref_split(split
);
1027 struct blame_entry
*ent
;
1028 struct blame_entry split
[3];
1032 * Count the number of entries the target is suspected for,
1033 * and prepare a list of entry and the best split.
1035 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1036 struct origin
*target
,
1040 struct blame_entry
*e
;
1042 struct blame_list
*blame_list
= NULL
;
1044 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1045 if (!e
->scanned
&& !e
->guilty
&&
1046 same_suspect(e
->suspect
, target
) &&
1047 min_score
< ent_score(sb
, e
))
1050 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1051 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1052 if (!e
->scanned
&& !e
->guilty
&&
1053 same_suspect(e
->suspect
, target
) &&
1054 min_score
< ent_score(sb
, e
))
1055 blame_list
[i
++].ent
= e
;
1057 *num_ents_p
= num_ents
;
1062 * Reset the scanned status on all entries.
1064 static void reset_scanned_flag(struct scoreboard
*sb
)
1066 struct blame_entry
*e
;
1067 for (e
= sb
->ent
; e
; e
= e
->next
)
1072 * For lines target is suspected for, see if we can find code movement
1073 * across file boundary from the parent commit. porigin is the path
1074 * in the parent we already tried.
1076 static int find_copy_in_parent(struct scoreboard
*sb
,
1077 struct origin
*target
,
1078 struct commit
*parent
,
1079 struct origin
*porigin
,
1082 struct diff_options diff_opts
;
1083 const char *paths
[1];
1086 struct blame_list
*blame_list
;
1089 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
1091 return 1; /* nothing remains for this target */
1093 diff_setup(&diff_opts
);
1094 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1095 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1098 diff_tree_setup_paths(paths
, &diff_opts
);
1099 if (diff_setup_done(&diff_opts
) < 0)
1102 /* Try "find copies harder" on new path if requested;
1103 * we do not want to use diffcore_rename() actually to
1104 * match things up; find_copies_harder is set only to
1105 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1106 * and this code needs to be after diff_setup_done(), which
1107 * usually makes find-copies-harder imply copy detection.
1109 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1110 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1111 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1112 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1114 if (is_null_sha1(target
->commit
->object
.sha1
))
1115 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1117 diff_tree_sha1(parent
->tree
->object
.sha1
,
1118 target
->commit
->tree
->object
.sha1
,
1121 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1122 diffcore_std(&diff_opts
);
1126 int made_progress
= 0;
1128 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1129 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1130 struct origin
*norigin
;
1132 struct blame_entry
this[3];
1134 if (!DIFF_FILE_VALID(p
->one
))
1135 continue; /* does not exist in parent */
1136 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1137 /* find_move already dealt with this path */
1140 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1141 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1142 fill_origin_blob(norigin
, &file_p
);
1146 for (j
= 0; j
< num_ents
; j
++) {
1147 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1148 norigin
, this, &file_p
);
1149 copy_split_if_better(sb
, blame_list
[j
].split
,
1153 origin_decref(norigin
);
1156 for (j
= 0; j
< num_ents
; j
++) {
1157 struct blame_entry
*split
= blame_list
[j
].split
;
1158 if (split
[1].suspect
&&
1159 blame_copy_score
< ent_score(sb
, &split
[1])) {
1160 split_blame(sb
, split
, blame_list
[j
].ent
);
1164 blame_list
[j
].ent
->scanned
= 1;
1165 decref_split(split
);
1171 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
1177 reset_scanned_flag(sb
);
1178 diff_flush(&diff_opts
);
1179 diff_tree_release_paths(&diff_opts
);
1184 * The blobs of origin and porigin exactly match, so everything
1185 * origin is suspected for can be blamed on the parent.
1187 static void pass_whole_blame(struct scoreboard
*sb
,
1188 struct origin
*origin
, struct origin
*porigin
)
1190 struct blame_entry
*e
;
1192 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1193 /* Steal its file */
1194 porigin
->file
= origin
->file
;
1195 origin
->file
.ptr
= NULL
;
1197 for (e
= sb
->ent
; e
; e
= e
->next
) {
1198 if (!same_suspect(e
->suspect
, origin
))
1200 origin_incref(porigin
);
1201 origin_decref(e
->suspect
);
1202 e
->suspect
= porigin
;
1207 * We pass blame from the current commit to its parents. We keep saying
1208 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1209 * exonerate ourselves.
1211 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1214 return commit
->parents
;
1215 return lookup_decoration(&revs
->children
, &commit
->object
);
1218 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1221 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1222 for (cnt
= 0; l
; l
= l
->next
)
1229 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1231 struct rev_info
*revs
= sb
->revs
;
1232 int i
, pass
, num_sg
;
1233 struct commit
*commit
= origin
->commit
;
1234 struct commit_list
*sg
;
1235 struct origin
*sg_buf
[MAXSG
];
1236 struct origin
*porigin
, **sg_origin
= sg_buf
;
1238 num_sg
= num_scapegoats(revs
, commit
);
1241 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1242 memset(sg_buf
, 0, sizeof(sg_buf
));
1244 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1247 * The first pass looks for unrenamed path to optimize for
1248 * common cases, then we look for renames in the second pass.
1250 for (pass
= 0; pass
< 2; pass
++) {
1251 struct origin
*(*find
)(struct scoreboard
*,
1252 struct commit
*, struct origin
*);
1253 find
= pass
? find_rename
: find_origin
;
1255 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1257 sg
= sg
->next
, i
++) {
1258 struct commit
*p
= sg
->item
;
1263 if (parse_commit(p
))
1265 porigin
= find(sb
, p
, origin
);
1268 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1269 pass_whole_blame(sb
, origin
, porigin
);
1270 origin_decref(porigin
);
1273 for (j
= same
= 0; j
< i
; j
++)
1275 !hashcmp(sg_origin
[j
]->blob_sha1
,
1276 porigin
->blob_sha1
)) {
1281 sg_origin
[i
] = porigin
;
1283 origin_decref(porigin
);
1288 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1290 sg
= sg
->next
, i
++) {
1291 struct origin
*porigin
= sg_origin
[i
];
1294 if (pass_blame_to_parent(sb
, origin
, porigin
))
1299 * Optionally find moves in parents' files.
1301 if (opt
& PICKAXE_BLAME_MOVE
)
1302 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1304 sg
= sg
->next
, i
++) {
1305 struct origin
*porigin
= sg_origin
[i
];
1308 if (find_move_in_parent(sb
, origin
, porigin
))
1313 * Optionally find copies from parents' files.
1315 if (opt
& PICKAXE_BLAME_COPY
)
1316 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1318 sg
= sg
->next
, i
++) {
1319 struct origin
*porigin
= sg_origin
[i
];
1320 if (find_copy_in_parent(sb
, origin
, sg
->item
,
1326 for (i
= 0; i
< num_sg
; i
++) {
1328 drop_origin_blob(sg_origin
[i
]);
1329 origin_decref(sg_origin
[i
]);
1332 drop_origin_blob(origin
);
1333 if (sg_buf
!= sg_origin
)
1338 * Information on commits, used for output.
1343 const char *author_mail
;
1344 unsigned long author_time
;
1345 const char *author_tz
;
1347 /* filled only when asked for details */
1348 const char *committer
;
1349 const char *committer_mail
;
1350 unsigned long committer_time
;
1351 const char *committer_tz
;
1353 const char *summary
;
1357 * Parse author/committer line in the commit object buffer
1359 static void get_ac_line(const char *inbuf
, const char *what
,
1360 int bufsz
, char *person
, const char **mail
,
1361 unsigned long *time
, const char **tz
)
1363 int len
, tzlen
, maillen
;
1364 char *tmp
, *endp
, *timepos
;
1366 tmp
= strstr(inbuf
, what
);
1369 tmp
+= strlen(what
);
1370 endp
= strchr(tmp
, '\n');
1378 *mail
= *tz
= "(unknown)";
1382 memcpy(person
, tmp
, len
);
1390 tzlen
= (person
+len
)-(tmp
+1);
1395 *time
= strtoul(tmp
, NULL
, 10);
1403 maillen
= timepos
- tmp
;
1409 * mailmap expansion may make the name longer.
1410 * make room by pushing stuff down.
1412 tmp
= person
+ bufsz
- (tzlen
+ 1);
1413 memmove(tmp
, *tz
, tzlen
);
1417 tmp
= tmp
- (maillen
+ 1);
1418 memmove(tmp
, *mail
, maillen
);
1423 * Now, convert e-mail using mailmap
1425 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1428 static void get_commit_info(struct commit
*commit
,
1429 struct commit_info
*ret
,
1434 static char author_buf
[1024];
1435 static char committer_buf
[1024];
1436 static char summary_buf
[1024];
1439 * We've operated without save_commit_buffer, so
1440 * we now need to populate them for output.
1442 if (!commit
->buffer
) {
1443 enum object_type type
;
1446 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1447 if (!commit
->buffer
)
1448 die("Cannot read commit %s",
1449 sha1_to_hex(commit
->object
.sha1
));
1451 ret
->author
= author_buf
;
1452 get_ac_line(commit
->buffer
, "\nauthor ",
1453 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1454 &ret
->author_time
, &ret
->author_tz
);
1459 ret
->committer
= committer_buf
;
1460 get_ac_line(commit
->buffer
, "\ncommitter ",
1461 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1462 &ret
->committer_time
, &ret
->committer_tz
);
1464 ret
->summary
= summary_buf
;
1465 tmp
= strstr(commit
->buffer
, "\n\n");
1468 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1472 endp
= strchr(tmp
, '\n');
1474 endp
= tmp
+ strlen(tmp
);
1476 if (len
>= sizeof(summary_buf
) || len
== 0)
1478 memcpy(summary_buf
, tmp
, len
);
1479 summary_buf
[len
] = 0;
1483 * To allow LF and other nonportable characters in pathnames,
1484 * they are c-style quoted as needed.
1486 static void write_filename_info(const char *path
)
1488 printf("filename ");
1489 write_name_quoted(path
, stdout
, '\n');
1493 * The blame_entry is found to be guilty for the range. Mark it
1494 * as such, and show it in incremental output.
1496 static void found_guilty_entry(struct blame_entry
*ent
)
1502 struct origin
*suspect
= ent
->suspect
;
1504 printf("%s %d %d %d\n",
1505 sha1_to_hex(suspect
->commit
->object
.sha1
),
1506 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1507 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1508 struct commit_info ci
;
1509 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1510 get_commit_info(suspect
->commit
, &ci
, 1);
1511 printf("author %s\n", ci
.author
);
1512 printf("author-mail %s\n", ci
.author_mail
);
1513 printf("author-time %lu\n", ci
.author_time
);
1514 printf("author-tz %s\n", ci
.author_tz
);
1515 printf("committer %s\n", ci
.committer
);
1516 printf("committer-mail %s\n", ci
.committer_mail
);
1517 printf("committer-time %lu\n", ci
.committer_time
);
1518 printf("committer-tz %s\n", ci
.committer_tz
);
1519 printf("summary %s\n", ci
.summary
);
1520 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1521 printf("boundary\n");
1523 write_filename_info(suspect
->path
);
1524 maybe_flush_or_die(stdout
, "stdout");
1529 * The main loop -- while the scoreboard has lines whose true origin
1530 * is still unknown, pick one blame_entry, and allow its current
1531 * suspect to pass blames to its parents.
1533 static void assign_blame(struct scoreboard
*sb
, int opt
)
1535 struct rev_info
*revs
= sb
->revs
;
1538 struct blame_entry
*ent
;
1539 struct commit
*commit
;
1540 struct origin
*suspect
= NULL
;
1542 /* find one suspect to break down */
1543 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1545 suspect
= ent
->suspect
;
1547 return; /* all done */
1550 * We will use this suspect later in the loop,
1551 * so hold onto it in the meantime.
1553 origin_incref(suspect
);
1554 commit
= suspect
->commit
;
1555 if (!commit
->object
.parsed
)
1556 parse_commit(commit
);
1558 (!(commit
->object
.flags
& UNINTERESTING
) &&
1559 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1560 pass_blame(sb
, suspect
, opt
);
1562 commit
->object
.flags
|= UNINTERESTING
;
1563 if (commit
->object
.parsed
)
1564 mark_parents_uninteresting(commit
);
1566 /* treat root commit as boundary */
1567 if (!commit
->parents
&& !show_root
)
1568 commit
->object
.flags
|= UNINTERESTING
;
1570 /* Take responsibility for the remaining entries */
1571 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1572 if (same_suspect(ent
->suspect
, suspect
))
1573 found_guilty_entry(ent
);
1574 origin_decref(suspect
);
1576 if (DEBUG
) /* sanity */
1577 sanity_check_refcnt(sb
);
1581 static const char *format_time(unsigned long time
, const char *tz_str
,
1584 static char time_buf
[128];
1589 if (show_raw_time
) {
1590 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1595 minutes
= tz
< 0 ? -tz
: tz
;
1596 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1597 minutes
= tz
< 0 ? -minutes
: minutes
;
1598 t
= time
+ minutes
* 60;
1601 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1602 strcat(time_buf
, tz_str
);
1606 #define OUTPUT_ANNOTATE_COMPAT 001
1607 #define OUTPUT_LONG_OBJECT_NAME 002
1608 #define OUTPUT_RAW_TIMESTAMP 004
1609 #define OUTPUT_PORCELAIN 010
1610 #define OUTPUT_SHOW_NAME 020
1611 #define OUTPUT_SHOW_NUMBER 040
1612 #define OUTPUT_SHOW_SCORE 0100
1613 #define OUTPUT_NO_AUTHOR 0200
1615 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1619 struct origin
*suspect
= ent
->suspect
;
1622 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1623 printf("%s%c%d %d %d\n",
1625 ent
->guilty
? ' ' : '*', // purely for debugging
1629 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1630 struct commit_info ci
;
1631 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1632 get_commit_info(suspect
->commit
, &ci
, 1);
1633 printf("author %s\n", ci
.author
);
1634 printf("author-mail %s\n", ci
.author_mail
);
1635 printf("author-time %lu\n", ci
.author_time
);
1636 printf("author-tz %s\n", ci
.author_tz
);
1637 printf("committer %s\n", ci
.committer
);
1638 printf("committer-mail %s\n", ci
.committer_mail
);
1639 printf("committer-time %lu\n", ci
.committer_time
);
1640 printf("committer-tz %s\n", ci
.committer_tz
);
1641 write_filename_info(suspect
->path
);
1642 printf("summary %s\n", ci
.summary
);
1643 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1644 printf("boundary\n");
1646 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1647 write_filename_info(suspect
->path
);
1649 cp
= nth_line(sb
, ent
->lno
);
1650 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1653 printf("%s %d %d\n", hex
,
1654 ent
->s_lno
+ 1 + cnt
,
1655 ent
->lno
+ 1 + cnt
);
1660 } while (ch
!= '\n' &&
1661 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1665 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1669 struct origin
*suspect
= ent
->suspect
;
1670 struct commit_info ci
;
1672 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1674 get_commit_info(suspect
->commit
, &ci
, 1);
1675 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1677 cp
= nth_line(sb
, ent
->lno
);
1678 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1680 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1682 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1684 memset(hex
, ' ', length
);
1685 else if (!cmd_is_annotate
) {
1691 printf("%.*s", length
, hex
);
1692 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1693 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1694 format_time(ci
.author_time
, ci
.author_tz
,
1696 ent
->lno
+ 1 + cnt
);
1698 if (opt
& OUTPUT_SHOW_SCORE
)
1700 max_score_digits
, ent
->score
,
1701 ent
->suspect
->refcnt
);
1702 if (opt
& OUTPUT_SHOW_NAME
)
1703 printf(" %-*.*s", longest_file
, longest_file
,
1705 if (opt
& OUTPUT_SHOW_NUMBER
)
1706 printf(" %*d", max_orig_digits
,
1707 ent
->s_lno
+ 1 + cnt
);
1709 if (!(opt
& OUTPUT_NO_AUTHOR
))
1710 printf(" (%-*.*s %10s",
1711 longest_author
, longest_author
,
1713 format_time(ci
.author_time
,
1717 max_digits
, ent
->lno
+ 1 + cnt
);
1722 } while (ch
!= '\n' &&
1723 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1727 static void output(struct scoreboard
*sb
, int option
)
1729 struct blame_entry
*ent
;
1731 if (option
& OUTPUT_PORCELAIN
) {
1732 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1733 struct blame_entry
*oth
;
1734 struct origin
*suspect
= ent
->suspect
;
1735 struct commit
*commit
= suspect
->commit
;
1736 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1738 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1739 if ((oth
->suspect
->commit
!= commit
) ||
1740 !strcmp(oth
->suspect
->path
, suspect
->path
))
1742 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1748 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1749 if (option
& OUTPUT_PORCELAIN
)
1750 emit_porcelain(sb
, ent
);
1752 emit_other(sb
, ent
, option
);
1758 * To allow quick access to the contents of nth line in the
1759 * final image, prepare an index in the scoreboard.
1761 static int prepare_lines(struct scoreboard
*sb
)
1763 const char *buf
= sb
->final_buf
;
1764 unsigned long len
= sb
->final_buf_size
;
1765 int num
= 0, incomplete
= 0, bol
= 1;
1767 if (len
&& buf
[len
-1] != '\n')
1768 incomplete
++; /* incomplete line at the end */
1771 sb
->lineno
= xrealloc(sb
->lineno
,
1772 sizeof(int* ) * (num
+ 1));
1773 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1776 if (*buf
++ == '\n') {
1781 sb
->lineno
= xrealloc(sb
->lineno
,
1782 sizeof(int* ) * (num
+ incomplete
+ 1));
1783 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1784 sb
->num_lines
= num
+ incomplete
;
1785 return sb
->num_lines
;
1789 * Add phony grafts for use with -S; this is primarily to
1790 * support git's cvsserver that wants to give a linear history
1793 static int read_ancestry(const char *graft_file
)
1795 FILE *fp
= fopen(graft_file
, "r");
1799 while (fgets(buf
, sizeof(buf
), fp
)) {
1800 /* The format is just "Commit Parent1 Parent2 ...\n" */
1801 int len
= strlen(buf
);
1802 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1804 register_commit_graft(graft
, 0);
1811 * How many columns do we need to show line numbers in decimal?
1813 static int lineno_width(int lines
)
1817 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1823 * How many columns do we need to show line numbers, authors,
1826 static void find_alignment(struct scoreboard
*sb
, int *option
)
1828 int longest_src_lines
= 0;
1829 int longest_dst_lines
= 0;
1830 unsigned largest_score
= 0;
1831 struct blame_entry
*e
;
1833 for (e
= sb
->ent
; e
; e
= e
->next
) {
1834 struct origin
*suspect
= e
->suspect
;
1835 struct commit_info ci
;
1838 if (strcmp(suspect
->path
, sb
->path
))
1839 *option
|= OUTPUT_SHOW_NAME
;
1840 num
= strlen(suspect
->path
);
1841 if (longest_file
< num
)
1843 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1844 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1845 get_commit_info(suspect
->commit
, &ci
, 1);
1846 num
= strlen(ci
.author
);
1847 if (longest_author
< num
)
1848 longest_author
= num
;
1850 num
= e
->s_lno
+ e
->num_lines
;
1851 if (longest_src_lines
< num
)
1852 longest_src_lines
= num
;
1853 num
= e
->lno
+ e
->num_lines
;
1854 if (longest_dst_lines
< num
)
1855 longest_dst_lines
= num
;
1856 if (largest_score
< ent_score(sb
, e
))
1857 largest_score
= ent_score(sb
, e
);
1859 max_orig_digits
= lineno_width(longest_src_lines
);
1860 max_digits
= lineno_width(longest_dst_lines
);
1861 max_score_digits
= lineno_width(largest_score
);
1865 * For debugging -- origin is refcounted, and this asserts that
1866 * we do not underflow.
1868 static void sanity_check_refcnt(struct scoreboard
*sb
)
1871 struct blame_entry
*ent
;
1873 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1874 /* Nobody should have zero or negative refcnt */
1875 if (ent
->suspect
->refcnt
<= 0) {
1876 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1878 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1879 ent
->suspect
->refcnt
);
1883 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1884 /* Mark the ones that haven't been checked */
1885 if (0 < ent
->suspect
->refcnt
)
1886 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1888 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1890 * ... then pick each and see if they have the the
1894 struct blame_entry
*e
;
1895 struct origin
*suspect
= ent
->suspect
;
1897 if (0 < suspect
->refcnt
)
1899 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1900 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1901 if (e
->suspect
!= suspect
)
1905 if (suspect
->refcnt
!= found
) {
1906 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1908 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1909 ent
->suspect
->refcnt
, found
);
1915 find_alignment(sb
, &opt
);
1917 die("Baa %d!", baa
);
1922 * Used for the command line parsing; check if the path exists
1923 * in the working tree.
1925 static int has_string_in_work_tree(const char *path
)
1928 return !lstat(path
, &st
);
1931 static unsigned parse_score(const char *arg
)
1934 unsigned long score
= strtoul(arg
, &end
, 10);
1940 static const char *add_prefix(const char *prefix
, const char *path
)
1942 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
1946 * Parsing of (comma separated) one item in the -L option
1948 static const char *parse_loc(const char *spec
,
1949 struct scoreboard
*sb
, long lno
,
1950 long begin
, long *ret
)
1957 regmatch_t match
[1];
1959 /* Allow "-L <something>,+20" to mean starting at <something>
1960 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1963 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1964 num
= strtol(spec
+ 1, &term
, 10);
1965 if (term
!= spec
+ 1) {
1969 *ret
= begin
+ num
- 2;
1978 num
= strtol(spec
, &term
, 10);
1986 /* it could be a regexp of form /.../ */
1987 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1994 /* try [spec+1 .. term-1] as regexp */
1996 begin
--; /* input is in human terms */
1997 line
= nth_line(sb
, begin
);
1999 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
2000 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
2001 const char *cp
= line
+ match
[0].rm_so
;
2004 while (begin
++ < lno
) {
2005 nline
= nth_line(sb
, begin
);
2006 if (line
<= cp
&& cp
< nline
)
2017 regerror(reg_error
, ®exp
, errbuf
, 1024);
2018 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
2023 * Parsing of -L option
2025 static void prepare_blame_range(struct scoreboard
*sb
,
2026 const char *bottomtop
,
2028 long *bottom
, long *top
)
2032 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
2034 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
2042 static int git_blame_config(const char *var
, const char *value
, void *cb
)
2044 if (!strcmp(var
, "blame.showroot")) {
2045 show_root
= git_config_bool(var
, value
);
2048 if (!strcmp(var
, "blame.blankboundary")) {
2049 blank_boundary
= git_config_bool(var
, value
);
2052 return git_default_config(var
, value
, cb
);
2056 * Prepare a dummy commit that represents the work tree (or staged) item.
2057 * Note that annotating work tree item never works in the reverse.
2059 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2061 struct commit
*commit
;
2062 struct origin
*origin
;
2063 unsigned char head_sha1
[20];
2068 struct cache_entry
*ce
;
2071 if (get_sha1("HEAD", head_sha1
))
2072 die("No such ref: HEAD");
2075 commit
= xcalloc(1, sizeof(*commit
));
2076 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2077 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2078 commit
->object
.parsed
= 1;
2080 commit
->object
.type
= OBJ_COMMIT
;
2082 origin
= make_origin(commit
, path
);
2084 strbuf_init(&buf
, 0);
2085 if (!contents_from
|| strcmp("-", contents_from
)) {
2087 const char *read_from
;
2088 unsigned long fin_size
;
2090 if (contents_from
) {
2091 if (stat(contents_from
, &st
) < 0)
2092 die("Cannot stat %s", contents_from
);
2093 read_from
= contents_from
;
2096 if (lstat(path
, &st
) < 0)
2097 die("Cannot lstat %s", path
);
2100 fin_size
= xsize_t(st
.st_size
);
2101 mode
= canon_mode(st
.st_mode
);
2102 switch (st
.st_mode
& S_IFMT
) {
2104 if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2105 die("cannot open or read %s", read_from
);
2108 if (readlink(read_from
, buf
.buf
, buf
.alloc
) != fin_size
)
2109 die("cannot readlink %s", read_from
);
2113 die("unsupported file type %s", read_from
);
2117 /* Reading from stdin */
2118 contents_from
= "standard input";
2120 if (strbuf_read(&buf
, 0, 0) < 0)
2121 die("read error %s from stdin", strerror(errno
));
2123 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2124 origin
->file
.ptr
= buf
.buf
;
2125 origin
->file
.size
= buf
.len
;
2126 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2127 commit
->util
= origin
;
2130 * Read the current index, replace the path entry with
2131 * origin->blob_sha1 without mucking with its mode or type
2132 * bits; we are not going to write this index out -- we just
2133 * want to run "diff-index --cached".
2140 int pos
= cache_name_pos(path
, len
);
2142 mode
= active_cache
[pos
]->ce_mode
;
2144 /* Let's not bother reading from HEAD tree */
2145 mode
= S_IFREG
| 0644;
2147 size
= cache_entry_size(len
);
2148 ce
= xcalloc(1, size
);
2149 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2150 memcpy(ce
->name
, path
, len
);
2151 ce
->ce_flags
= create_ce_flags(len
, 0);
2152 ce
->ce_mode
= create_ce_mode(mode
);
2153 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2156 * We are not going to write this out, so this does not matter
2157 * right now, but someday we might optimize diff-index --cached
2158 * with cache-tree information.
2160 cache_tree_invalidate_path(active_cache_tree
, path
);
2162 commit
->buffer
= xmalloc(400);
2163 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2164 snprintf(commit
->buffer
, 400,
2165 "tree 0000000000000000000000000000000000000000\n"
2169 "Version of %s from %s\n",
2170 sha1_to_hex(head_sha1
),
2171 ident
, ident
, path
, contents_from
? contents_from
: path
);
2175 static const char *prepare_final(struct scoreboard
*sb
)
2178 const char *final_commit_name
= NULL
;
2179 struct rev_info
*revs
= sb
->revs
;
2182 * There must be one and only one positive commit in the
2183 * revs->pending array.
2185 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2186 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2187 if (obj
->flags
& UNINTERESTING
)
2189 while (obj
->type
== OBJ_TAG
)
2190 obj
= deref_tag(obj
, NULL
, 0);
2191 if (obj
->type
!= OBJ_COMMIT
)
2192 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2194 die("More than one commit to dig from %s and %s?",
2195 revs
->pending
.objects
[i
].name
,
2197 sb
->final
= (struct commit
*) obj
;
2198 final_commit_name
= revs
->pending
.objects
[i
].name
;
2200 return final_commit_name
;
2203 static const char *prepare_initial(struct scoreboard
*sb
)
2206 const char *final_commit_name
= NULL
;
2207 struct rev_info
*revs
= sb
->revs
;
2210 * There must be one and only one negative commit, and it must be
2213 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2214 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2215 if (!(obj
->flags
& UNINTERESTING
))
2217 while (obj
->type
== OBJ_TAG
)
2218 obj
= deref_tag(obj
, NULL
, 0);
2219 if (obj
->type
!= OBJ_COMMIT
)
2220 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2222 die("More than one commit to dig down to %s and %s?",
2223 revs
->pending
.objects
[i
].name
,
2225 sb
->final
= (struct commit
*) obj
;
2226 final_commit_name
= revs
->pending
.objects
[i
].name
;
2228 if (!final_commit_name
)
2229 die("No commit to dig down to?");
2230 return final_commit_name
;
2233 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2235 int *opt
= option
->value
;
2238 * -C enables copy from removed files;
2239 * -C -C enables copy from existing files, but only
2240 * when blaming a new file;
2241 * -C -C -C enables copy from existing files for
2244 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2245 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2246 if (*opt
& PICKAXE_BLAME_COPY
)
2247 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2248 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2251 blame_copy_score
= parse_score(arg
);
2255 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2257 int *opt
= option
->value
;
2259 *opt
|= PICKAXE_BLAME_MOVE
;
2262 blame_move_score
= parse_score(arg
);
2266 static int blame_bottomtop_callback(const struct option
*option
, const char *arg
, int unset
)
2268 const char **bottomtop
= option
->value
;
2272 die("More than one '-L n,m' option given");
2277 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2279 struct rev_info revs
;
2281 struct scoreboard sb
;
2283 struct blame_entry
*ent
;
2284 long dashdash_pos
, bottom
, top
, lno
;
2285 const char *final_commit_name
= NULL
;
2286 enum object_type type
;
2288 static const char *bottomtop
= NULL
;
2289 static int output_option
= 0, opt
= 0;
2290 static int show_stats
= 0;
2291 static const char *revs_file
= NULL
;
2292 static const char *contents_from
= NULL
;
2293 static const struct option options
[] = {
2294 OPT_BOOLEAN(0, "incremental", &incremental
, "Show blame entries as we find them, incrementally"),
2295 OPT_BOOLEAN('b', NULL
, &blank_boundary
, "Show blank SHA-1 for boundary commits (Default: off)"),
2296 OPT_BOOLEAN(0, "root", &show_root
, "Do not treat root commits as boundaries (Default: off)"),
2297 OPT_BOOLEAN(0, "show-stats", &show_stats
, "Show work cost statistics"),
2298 OPT_BIT(0, "score-debug", &output_option
, "Show output score for blame entries", OUTPUT_SHOW_SCORE
),
2299 OPT_BIT('f', "show-name", &output_option
, "Show original filename (Default: auto)", OUTPUT_SHOW_NAME
),
2300 OPT_BIT('n', "show-number", &output_option
, "Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER
),
2301 OPT_BIT('p', "porcelain", &output_option
, "Show in a format designed for machine consumption", OUTPUT_PORCELAIN
),
2302 OPT_BIT('c', NULL
, &output_option
, "Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT
),
2303 OPT_BIT('t', NULL
, &output_option
, "Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP
),
2304 OPT_BIT('l', NULL
, &output_option
, "Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME
),
2305 OPT_BIT('s', NULL
, &output_option
, "Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR
),
2306 OPT_BIT('w', NULL
, &xdl_opts
, "Ignore whitespace differences", XDF_IGNORE_WHITESPACE
),
2307 OPT_STRING('S', NULL
, &revs_file
, "file", "Use revisions from <file> instead of calling git-rev-list"),
2308 OPT_STRING(0, "contents", &contents_from
, "file", "Use <file>'s contents as the final image"),
2309 { OPTION_CALLBACK
, 'C', NULL
, &opt
, "score", "Find line copies within and across files", PARSE_OPT_OPTARG
, blame_copy_callback
},
2310 { OPTION_CALLBACK
, 'M', NULL
, &opt
, "score", "Find line movements within and across files", PARSE_OPT_OPTARG
, blame_move_callback
},
2311 OPT_CALLBACK('L', NULL
, &bottomtop
, "n,m", "Process only line range n,m, counting from 1", blame_bottomtop_callback
),
2315 struct parse_opt_ctx_t ctx
;
2317 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2319 git_config(git_blame_config
, NULL
);
2320 init_revisions(&revs
, NULL
);
2321 save_commit_buffer
= 0;
2324 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
2325 PARSE_OPT_KEEP_ARGV0
);
2327 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2328 case PARSE_OPT_HELP
:
2330 case PARSE_OPT_DONE
:
2332 dashdash_pos
= ctx
.cpidx
;
2336 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2337 ctx
.argv
[0] = "--children";
2340 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2343 argc
= parse_options_end(&ctx
);
2345 if (DIFF_OPT_TST(&revs
.diffopt
, FIND_COPIES_HARDER
))
2346 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
2347 PICKAXE_BLAME_COPY_HARDER
);
2349 if (!blame_move_score
)
2350 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2351 if (!blame_copy_score
)
2352 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2355 * We have collected options unknown to us in argv[1..unk]
2356 * which are to be passed to revision machinery if we are
2357 * going to do the "bottom" processing.
2359 * The remaining are:
2361 * (1) if dashdash_pos != 0, its either
2362 * "blame [revisions] -- <path>" or
2363 * "blame -- <path> <rev>"
2365 * (2) otherwise, its one of the two:
2366 * "blame [revisions] <path>"
2367 * "blame <path> <rev>"
2369 * Note that we must strip out <path> from the arguments: we do not
2370 * want the path pruning but we may want "bottom" processing.
2373 switch (argc
- dashdash_pos
- 1) {
2376 usage_with_options(blame_opt_usage
, options
);
2377 /* reorder for the new way: <rev> -- <path> */
2383 path
= add_prefix(prefix
, argv
[--argc
]);
2387 usage_with_options(blame_opt_usage
, options
);
2391 usage_with_options(blame_opt_usage
, options
);
2392 path
= add_prefix(prefix
, argv
[argc
- 1]);
2393 if (argc
== 3 && !has_string_in_work_tree(path
)) { /* (2b) */
2394 path
= add_prefix(prefix
, argv
[1]);
2397 argv
[argc
- 1] = "--";
2400 if (!has_string_in_work_tree(path
))
2401 die("cannot stat path %s: %s", path
, strerror(errno
));
2404 setup_revisions(argc
, argv
, &revs
, NULL
);
2405 memset(&sb
, 0, sizeof(sb
));
2409 final_commit_name
= prepare_final(&sb
);
2410 else if (contents_from
)
2411 die("--contents and --children do not blend well.");
2413 final_commit_name
= prepare_initial(&sb
);
2417 * "--not A B -- path" without anything positive;
2418 * do not default to HEAD, but use the working tree
2422 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2423 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2425 else if (contents_from
)
2426 die("Cannot use --contents with final commit object name");
2429 * If we have bottom, this will mark the ancestors of the
2430 * bottom commits we would reach while traversing as
2433 if (prepare_revision_walk(&revs
))
2434 die("revision walk setup failed");
2436 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2439 buf
= xmalloc(o
->file
.size
+ 1);
2440 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2442 sb
.final_buf_size
= o
->file
.size
;
2445 o
= get_origin(&sb
, sb
.final
, path
);
2446 if (fill_blob_sha1(o
))
2447 die("no such path %s in %s", path
, final_commit_name
);
2449 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2450 &sb
.final_buf_size
);
2452 die("Cannot read blob %s for path %s",
2453 sha1_to_hex(o
->blob_sha1
),
2457 lno
= prepare_lines(&sb
);
2461 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2462 if (bottom
&& top
&& top
< bottom
) {
2464 tmp
= top
; top
= bottom
; bottom
= tmp
;
2472 die("file %s has only %lu lines", path
, lno
);
2474 ent
= xcalloc(1, sizeof(*ent
));
2476 ent
->num_lines
= top
- bottom
;
2478 ent
->s_lno
= bottom
;
2483 if (revs_file
&& read_ancestry(revs_file
))
2484 die("reading graft file %s failed: %s",
2485 revs_file
, strerror(errno
));
2487 read_mailmap(&mailmap
, ".mailmap", NULL
);
2492 assign_blame(&sb
, opt
);
2499 if (!(output_option
& OUTPUT_PORCELAIN
))
2500 find_alignment(&sb
, &output_option
);
2502 output(&sb
, output_option
);
2503 free((void *)sb
.final_buf
);
2504 for (ent
= sb
.ent
; ent
; ) {
2505 struct blame_entry
*e
= ent
->next
;
2511 printf("num read blob: %d\n", num_read_blob
);
2512 printf("num get patch: %d\n", num_get_patch
);
2513 printf("num commits: %d\n", num_commits
);