4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
13 #include "tree-walk.h"
18 #include "xdiff-interface.h"
19 #include "cache-tree.h"
20 #include "string-list.h"
22 #include "mergesort.h"
23 #include "parse-options.h"
24 #include "prio-queue.h"
27 #include "line-range.h"
31 static char blame_usage
[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
33 static const char *blame_opt_usage
[] = {
36 N_("<rev-opts> are documented in git-rev-list(1)"),
40 static int longest_file
;
41 static int longest_author
;
42 static int max_orig_digits
;
43 static int max_digits
;
44 static int max_score_digits
;
47 static int blank_boundary
;
48 static int incremental
;
50 static int abbrev
= -1;
51 static int no_whole_file_rename
;
53 static struct date_mode blame_date_mode
= { DATE_ISO8601
};
54 static size_t blame_date_width
;
56 static struct string_list mailmap
;
63 static int num_read_blob
;
64 static int num_get_patch
;
65 static int num_commits
;
67 #define PICKAXE_BLAME_MOVE 01
68 #define PICKAXE_BLAME_COPY 02
69 #define PICKAXE_BLAME_COPY_HARDER 04
70 #define PICKAXE_BLAME_COPY_HARDEST 010
73 * blame for a blame_entry with score lower than these thresholds
74 * is not passed to the parent using move/copy logic.
76 static unsigned blame_move_score
;
77 static unsigned blame_copy_score
;
78 #define BLAME_DEFAULT_MOVE_SCORE 20
79 #define BLAME_DEFAULT_COPY_SCORE 40
81 /* Remember to update object flag allocation in object.h */
82 #define METAINFO_SHOWN (1u<<12)
83 #define MORE_THAN_ONE_PATH (1u<<13)
86 * One blob in a commit that is being suspected
90 /* Record preceding blame record for this blob */
91 struct origin
*previous
;
92 /* origins are put in a list linked via `next' hanging off the
93 * corresponding commit's util field in order to make finding
94 * them fast. The presence in this chain does not count
95 * towards the origin's reference count. It is tempting to
96 * let it count as long as the commit is pending examination,
97 * but even under circumstances where the commit will be
98 * present multiple times in the priority queue of unexamined
99 * commits, processing the first instance will not leave any
100 * work requiring the origin data for the second instance. An
101 * interspersed commit changing that would have to be
102 * preexisting with a different ancestry and with the same
103 * commit date in order to wedge itself between two instances
104 * of the same commit in the priority queue _and_ produce
105 * blame entries relevant for it. While we don't want to let
106 * us get tripped up by this case, it certainly does not seem
107 * worth optimizing for.
110 struct commit
*commit
;
111 /* `suspects' contains blame entries that may be attributed to
112 * this origin's commit or to parent commits. When a commit
113 * is being processed, all suspects will be moved, either by
114 * assigning them to an origin in a different commit, or by
115 * shipping them to the scoreboard's ent list because they
116 * cannot be attributed to a different commit.
118 struct blame_entry
*suspects
;
120 unsigned char blob_sha1
[20];
122 /* guilty gets set when shipping any suspects to the final
123 * blame list instead of other commits
126 char path
[FLEX_ARRAY
];
129 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
, long ctxlen
,
130 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
)
133 xdemitconf_t xecfg
= {0};
134 xdemitcb_t ecb
= {NULL
};
136 xpp
.flags
= xdl_opts
;
137 xecfg
.ctxlen
= ctxlen
;
138 xecfg
.hunk_func
= hunk_func
;
140 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
144 * Prepare diff_filespec and convert it using diff textconv API
145 * if the textconv driver exists.
146 * Return 1 if the conversion succeeds, 0 otherwise.
148 int textconv_object(const char *path
,
150 const unsigned char *sha1
,
153 unsigned long *buf_size
)
155 struct diff_filespec
*df
;
156 struct userdiff_driver
*textconv
;
158 df
= alloc_filespec(path
);
159 fill_filespec(df
, sha1
, sha1_valid
, mode
);
160 textconv
= get_textconv(df
);
166 *buf_size
= fill_textconv(textconv
, df
, buf
);
172 * Given an origin, prepare mmfile_t structure to be used by the
175 static void fill_origin_blob(struct diff_options
*opt
,
176 struct origin
*o
, mmfile_t
*file
)
179 enum object_type type
;
180 unsigned long file_size
;
183 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
184 textconv_object(o
->path
, o
->mode
, o
->blob_sha1
, 1, &file
->ptr
, &file_size
))
187 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
, &file_size
);
188 file
->size
= file_size
;
191 die("Cannot read blob %s for path %s",
192 sha1_to_hex(o
->blob_sha1
),
201 * Origin is refcounted and usually we keep the blob contents to be
204 static inline struct origin
*origin_incref(struct origin
*o
)
211 static void origin_decref(struct origin
*o
)
213 if (o
&& --o
->refcnt
<= 0) {
214 struct origin
*p
, *l
= NULL
;
216 origin_decref(o
->previous
);
218 /* Should be present exactly once in commit chain */
219 for (p
= o
->commit
->util
; p
; l
= p
, p
= p
->next
) {
224 o
->commit
->util
= p
->next
;
229 die("internal error in blame::origin_decref");
233 static void drop_origin_blob(struct origin
*o
)
242 * Each group of lines is described by a blame_entry; it can be split
243 * as we pass blame to the parents. They are arranged in linked lists
244 * kept as `suspects' of some unprocessed origin, or entered (when the
245 * blame origin has been finalized) into the scoreboard structure.
246 * While the scoreboard structure is only sorted at the end of
247 * processing (according to final image line number), the lists
248 * attached to an origin are sorted by the target line number.
251 struct blame_entry
*next
;
253 /* the first line of this group in the final image;
254 * internally all line numbers are 0 based.
258 /* how many lines this group has */
261 /* the commit that introduced this group into the final image */
262 struct origin
*suspect
;
264 /* the line number of the first line of this group in the
265 * suspect's file; internally all line numbers are 0 based.
269 /* how significant this entry is -- cached to avoid
270 * scanning the lines over and over.
276 * Any merge of blames happens on lists of blames that arrived via
277 * different parents in a single suspect. In this case, we want to
278 * sort according to the suspect line numbers as opposed to the final
279 * image line numbers. The function body is somewhat longish because
280 * it avoids unnecessary writes.
283 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
284 struct blame_entry
*list2
)
286 struct blame_entry
*p1
= list1
, *p2
= list2
,
294 if (p1
->s_lno
<= p2
->s_lno
) {
297 if ((p1
= *tail
) == NULL
) {
301 } while (p1
->s_lno
<= p2
->s_lno
);
307 if ((p2
= *tail
) == NULL
) {
311 } while (p1
->s_lno
> p2
->s_lno
);
315 if ((p1
= *tail
) == NULL
) {
319 } while (p1
->s_lno
<= p2
->s_lno
);
323 static void *get_next_blame(const void *p
)
325 return ((struct blame_entry
*)p
)->next
;
328 static void set_next_blame(void *p1
, void *p2
)
330 ((struct blame_entry
*)p1
)->next
= p2
;
334 * Final image line numbers are all different, so we don't need a
335 * three-way comparison here.
338 static int compare_blame_final(const void *p1
, const void *p2
)
340 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
344 static int compare_blame_suspect(const void *p1
, const void *p2
)
346 const struct blame_entry
*s1
= p1
, *s2
= p2
;
348 * to allow for collating suspects, we sort according to the
349 * respective pointer value as the primary sorting criterion.
350 * The actual relation is pretty unimportant as long as it
351 * establishes a total order. Comparing as integers gives us
354 if (s1
->suspect
!= s2
->suspect
)
355 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
356 if (s1
->s_lno
== s2
->s_lno
)
358 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
361 static struct blame_entry
*blame_sort(struct blame_entry
*head
,
362 int (*compare_fn
)(const void *, const void *))
364 return llist_mergesort (head
, get_next_blame
, set_next_blame
, compare_fn
);
367 static int compare_commits_by_reverse_commit_date(const void *a
,
371 return -compare_commits_by_commit_date(a
, b
, c
);
375 * The current state of the blame assignment.
378 /* the final commit (i.e. where we started digging from) */
379 struct commit
*final
;
380 /* Priority queue for commits with unassigned blame records */
381 struct prio_queue commits
;
382 struct rev_info
*revs
;
386 * The contents in the final image.
387 * Used by many functions to obtain contents of the nth line,
388 * indexed with scoreboard.lineno[blame_entry.lno].
390 const char *final_buf
;
391 unsigned long final_buf_size
;
393 /* linked list of blames */
394 struct blame_entry
*ent
;
396 /* look-up a line in the final buffer */
401 static void sanity_check_refcnt(struct scoreboard
*);
404 * If two blame entries that are next to each other came from
405 * contiguous lines in the same origin (i.e. <commit, path> pair),
406 * merge them together.
408 static void coalesce(struct scoreboard
*sb
)
410 struct blame_entry
*ent
, *next
;
412 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
413 if (ent
->suspect
== next
->suspect
&&
414 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
415 ent
->num_lines
+= next
->num_lines
;
416 ent
->next
= next
->next
;
417 origin_decref(next
->suspect
);
420 next
= ent
; /* again */
424 if (DEBUG
) /* sanity */
425 sanity_check_refcnt(sb
);
429 * Merge the given sorted list of blames into a preexisting origin.
430 * If there were no previous blames to that commit, it is entered into
431 * the commit priority queue of the score board.
434 static void queue_blames(struct scoreboard
*sb
, struct origin
*porigin
,
435 struct blame_entry
*sorted
)
437 if (porigin
->suspects
)
438 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
441 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
443 porigin
->suspects
= sorted
;
447 porigin
->suspects
= sorted
;
448 prio_queue_put(&sb
->commits
, porigin
->commit
);
453 * Given a commit and a path in it, create a new origin structure.
454 * The callers that add blame to the scoreboard should use
455 * get_origin() to obtain shared, refcounted copy instead of calling
456 * this function directly.
458 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
461 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
464 o
->next
= commit
->util
;
466 strcpy(o
->path
, path
);
471 * Locate an existing origin or create a new one.
472 * This moves the origin to front position in the commit util list.
474 static struct origin
*get_origin(struct scoreboard
*sb
,
475 struct commit
*commit
,
478 struct origin
*o
, *l
;
480 for (o
= commit
->util
, l
= NULL
; o
; l
= o
, o
= o
->next
) {
481 if (!strcmp(o
->path
, path
)) {
485 o
->next
= commit
->util
;
488 return origin_incref(o
);
491 return make_origin(commit
, path
);
495 * Fill the blob_sha1 field of an origin if it hasn't, so that later
496 * call to fill_origin_blob() can use it to locate the data. blob_sha1
497 * for an origin is also used to pass the blame for the entire file to
498 * the parent to detect the case where a child's blob is identical to
499 * that of its parent's.
501 * This also fills origin->mode for corresponding tree path.
503 static int fill_blob_sha1_and_mode(struct origin
*origin
)
505 if (!is_null_sha1(origin
->blob_sha1
))
507 if (get_tree_entry(origin
->commit
->object
.sha1
,
509 origin
->blob_sha1
, &origin
->mode
))
511 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
515 hashclr(origin
->blob_sha1
);
516 origin
->mode
= S_IFINVALID
;
521 * We have an origin -- check if the same path exists in the
522 * parent and return an origin structure to represent it.
524 static struct origin
*find_origin(struct scoreboard
*sb
,
525 struct commit
*parent
,
526 struct origin
*origin
)
528 struct origin
*porigin
;
529 struct diff_options diff_opts
;
530 const char *paths
[2];
532 /* First check any existing origins */
533 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
534 if (!strcmp(porigin
->path
, origin
->path
)) {
536 * The same path between origin and its parent
537 * without renaming -- the most common case.
539 return origin_incref (porigin
);
542 /* See if the origin->path is different between parent
543 * and origin first. Most of the time they are the
544 * same and diff-tree is fairly efficient about this.
546 diff_setup(&diff_opts
);
547 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
548 diff_opts
.detect_rename
= 0;
549 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
550 paths
[0] = origin
->path
;
553 parse_pathspec(&diff_opts
.pathspec
,
554 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
555 PATHSPEC_LITERAL_PATH
, "", paths
);
556 diff_setup_done(&diff_opts
);
558 if (is_null_sha1(origin
->commit
->object
.sha1
))
559 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
561 diff_tree_sha1(parent
->tree
->object
.sha1
,
562 origin
->commit
->tree
->object
.sha1
,
564 diffcore_std(&diff_opts
);
566 if (!diff_queued_diff
.nr
) {
567 /* The path is the same as parent */
568 porigin
= get_origin(sb
, parent
, origin
->path
);
569 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
570 porigin
->mode
= origin
->mode
;
573 * Since origin->path is a pathspec, if the parent
574 * commit had it as a directory, we will see a whole
575 * bunch of deletion of files in the directory that we
579 struct diff_filepair
*p
= NULL
;
580 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
582 p
= diff_queued_diff
.queue
[i
];
583 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
584 if (!strcmp(name
, origin
->path
))
588 die("internal error in blame::find_origin");
591 die("internal error in blame::find_origin (%c)",
594 porigin
= get_origin(sb
, parent
, origin
->path
);
595 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
596 porigin
->mode
= p
->one
->mode
;
600 /* Did not exist in parent, or type changed */
604 diff_flush(&diff_opts
);
605 free_pathspec(&diff_opts
.pathspec
);
610 * We have an origin -- find the path that corresponds to it in its
611 * parent and return an origin structure to represent it.
613 static struct origin
*find_rename(struct scoreboard
*sb
,
614 struct commit
*parent
,
615 struct origin
*origin
)
617 struct origin
*porigin
= NULL
;
618 struct diff_options diff_opts
;
621 diff_setup(&diff_opts
);
622 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
623 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
624 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
625 diff_opts
.single_follow
= origin
->path
;
626 diff_setup_done(&diff_opts
);
628 if (is_null_sha1(origin
->commit
->object
.sha1
))
629 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
631 diff_tree_sha1(parent
->tree
->object
.sha1
,
632 origin
->commit
->tree
->object
.sha1
,
634 diffcore_std(&diff_opts
);
636 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
637 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
638 if ((p
->status
== 'R' || p
->status
== 'C') &&
639 !strcmp(p
->two
->path
, origin
->path
)) {
640 porigin
= get_origin(sb
, parent
, p
->one
->path
);
641 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
642 porigin
->mode
= p
->one
->mode
;
646 diff_flush(&diff_opts
);
647 free_pathspec(&diff_opts
.pathspec
);
652 * Append a new blame entry to a given output queue.
654 static void add_blame_entry(struct blame_entry
***queue
, struct blame_entry
*e
)
656 origin_incref(e
->suspect
);
664 * src typically is on-stack; we want to copy the information in it to
665 * a malloced blame_entry that gets added to the given queue. The
666 * origin of dst loses a refcnt.
668 static void dup_entry(struct blame_entry
***queue
,
669 struct blame_entry
*dst
, struct blame_entry
*src
)
671 origin_incref(src
->suspect
);
672 origin_decref(dst
->suspect
);
673 memcpy(dst
, src
, sizeof(*src
));
679 static const char *nth_line(struct scoreboard
*sb
, long lno
)
681 return sb
->final_buf
+ sb
->lineno
[lno
];
684 static const char *nth_line_cb(void *data
, long lno
)
686 return nth_line((struct scoreboard
*)data
, lno
);
690 * It is known that lines between tlno to same came from parent, and e
691 * has an overlap with that range. it also is known that parent's
692 * line plno corresponds to e's line tlno.
698 * <------------------>
700 * Split e into potentially three parts; before this chunk, the chunk
701 * to be blamed for the parent, and after that portion.
703 static void split_overlap(struct blame_entry
*split
,
704 struct blame_entry
*e
,
705 int tlno
, int plno
, int same
,
706 struct origin
*parent
)
709 memset(split
, 0, sizeof(struct blame_entry
[3]));
711 if (e
->s_lno
< tlno
) {
712 /* there is a pre-chunk part not blamed on parent */
713 split
[0].suspect
= origin_incref(e
->suspect
);
714 split
[0].lno
= e
->lno
;
715 split
[0].s_lno
= e
->s_lno
;
716 split
[0].num_lines
= tlno
- e
->s_lno
;
717 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
718 split
[1].s_lno
= plno
;
721 split
[1].lno
= e
->lno
;
722 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
725 if (same
< e
->s_lno
+ e
->num_lines
) {
726 /* there is a post-chunk part not blamed on parent */
727 split
[2].suspect
= origin_incref(e
->suspect
);
728 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
729 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
730 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
731 chunk_end_lno
= split
[2].lno
;
734 chunk_end_lno
= e
->lno
+ e
->num_lines
;
735 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
738 * if it turns out there is nothing to blame the parent for,
739 * forget about the splitting. !split[1].suspect signals this.
741 if (split
[1].num_lines
< 1)
743 split
[1].suspect
= origin_incref(parent
);
747 * split_overlap() divided an existing blame e into up to three parts
748 * in split. Any assigned blame is moved to queue to
751 static void split_blame(struct blame_entry
***blamed
,
752 struct blame_entry
***unblamed
,
753 struct blame_entry
*split
,
754 struct blame_entry
*e
)
756 struct blame_entry
*new_entry
;
758 if (split
[0].suspect
&& split
[2].suspect
) {
759 /* The first part (reuse storage for the existing entry e) */
760 dup_entry(unblamed
, e
, &split
[0]);
762 /* The last part -- me */
763 new_entry
= xmalloc(sizeof(*new_entry
));
764 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
765 add_blame_entry(unblamed
, new_entry
);
767 /* ... and the middle part -- parent */
768 new_entry
= xmalloc(sizeof(*new_entry
));
769 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
770 add_blame_entry(blamed
, new_entry
);
772 else if (!split
[0].suspect
&& !split
[2].suspect
)
774 * The parent covers the entire area; reuse storage for
775 * e and replace it with the parent.
777 dup_entry(blamed
, e
, &split
[1]);
778 else if (split
[0].suspect
) {
779 /* me and then parent */
780 dup_entry(unblamed
, e
, &split
[0]);
782 new_entry
= xmalloc(sizeof(*new_entry
));
783 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
784 add_blame_entry(blamed
, new_entry
);
787 /* parent and then me */
788 dup_entry(blamed
, e
, &split
[1]);
790 new_entry
= xmalloc(sizeof(*new_entry
));
791 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
792 add_blame_entry(unblamed
, new_entry
);
797 * After splitting the blame, the origins used by the
798 * on-stack blame_entry should lose one refcnt each.
800 static void decref_split(struct blame_entry
*split
)
804 for (i
= 0; i
< 3; i
++)
805 origin_decref(split
[i
].suspect
);
809 * reverse_blame reverses the list given in head, appending tail.
810 * That allows us to build lists in reverse order, then reverse them
811 * afterwards. This can be faster than building the list in proper
812 * order right away. The reason is that building in proper order
813 * requires writing a link in the _previous_ element, while building
814 * in reverse order just requires placing the list head into the
818 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
819 struct blame_entry
*tail
)
822 struct blame_entry
*next
= head
->next
;
831 * Process one hunk from the patch between the current suspect for
832 * blame_entry e and its parent. This first blames any unfinished
833 * entries before the chunk (which is where target and parent start
834 * differing) on the parent, and then splits blame entries at the
835 * start and at the end of the difference region. Since use of -M and
836 * -C options may lead to overlapping/duplicate source line number
837 * ranges, all we can rely on from sorting/merging is the order of the
838 * first suspect line number.
840 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
841 int tlno
, int offset
, int same
,
842 struct origin
*parent
)
844 struct blame_entry
*e
= **srcq
;
845 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
847 while (e
&& e
->s_lno
< tlno
) {
848 struct blame_entry
*next
= e
->next
;
850 * current record starts before differing portion. If
851 * it reaches into it, we need to split it up and
852 * examine the second part separately.
854 if (e
->s_lno
+ e
->num_lines
> tlno
) {
855 /* Move second half to a new record */
856 int len
= tlno
- e
->s_lno
;
857 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
858 n
->suspect
= e
->suspect
;
859 n
->lno
= e
->lno
+ len
;
860 n
->s_lno
= e
->s_lno
+ len
;
861 n
->num_lines
= e
->num_lines
- len
;
864 /* Push new record to diffp */
868 origin_decref(e
->suspect
);
869 /* Pass blame for everything before the differing
870 * chunk to the parent */
871 e
->suspect
= origin_incref(parent
);
878 * As we don't know how much of a common stretch after this
879 * diff will occur, the currently blamed parts are all that we
880 * can assign to the parent for now.
884 **dstq
= reverse_blame(samep
, **dstq
);
885 *dstq
= &samep
->next
;
888 * Prepend the split off portions: everything after e starts
889 * after the blameable portion.
891 e
= reverse_blame(diffp
, e
);
894 * Now retain records on the target while parts are different
899 while (e
&& e
->s_lno
< same
) {
900 struct blame_entry
*next
= e
->next
;
903 * If current record extends into sameness, need to split.
905 if (e
->s_lno
+ e
->num_lines
> same
) {
907 * Move second half to a new record to be
908 * processed by later chunks
910 int len
= same
- e
->s_lno
;
911 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
912 n
->suspect
= origin_incref(e
->suspect
);
913 n
->lno
= e
->lno
+ len
;
914 n
->s_lno
= e
->s_lno
+ len
;
915 n
->num_lines
= e
->num_lines
- len
;
918 /* Push new record to samep */
926 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
927 /* Move across elements that are in the unblamable portion */
929 *srcq
= &diffp
->next
;
932 struct blame_chunk_cb_data
{
933 struct origin
*parent
;
935 struct blame_entry
**dstq
;
936 struct blame_entry
**srcq
;
939 /* diff chunks are from parent to target */
940 static int blame_chunk_cb(long start_a
, long count_a
,
941 long start_b
, long count_b
, void *data
)
943 struct blame_chunk_cb_data
*d
= data
;
944 if (start_a
- start_b
!= d
->offset
)
945 die("internal error in blame::blame_chunk_cb");
946 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
947 start_b
+ count_b
, d
->parent
);
948 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
953 * We are looking at the origin 'target' and aiming to pass blame
954 * for the lines it is suspected to its parent. Run diff to find
955 * which lines came from parent and pass blame for them.
957 static void pass_blame_to_parent(struct scoreboard
*sb
,
958 struct origin
*target
,
959 struct origin
*parent
)
961 mmfile_t file_p
, file_o
;
962 struct blame_chunk_cb_data d
;
963 struct blame_entry
*newdest
= NULL
;
965 if (!target
->suspects
)
966 return; /* nothing remains for this target */
970 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
972 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
973 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
);
976 diff_hunks(&file_p
, &file_o
, 0, blame_chunk_cb
, &d
);
977 /* The rest are the same as the parent */
978 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
980 queue_blames(sb
, parent
, newdest
);
986 * The lines in blame_entry after splitting blames many times can become
987 * very small and trivial, and at some point it becomes pointless to
988 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
989 * ordinary C program, and it is not worth to say it was copied from
990 * totally unrelated file in the parent.
992 * Compute how trivial the lines in the blame_entry are.
994 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
1003 cp
= nth_line(sb
, e
->lno
);
1004 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
1006 unsigned ch
= *((unsigned char *)cp
);
1016 * best_so_far[] and this[] are both a split of an existing blame_entry
1017 * that passes blame to the parent. Maintain best_so_far the best split
1018 * so far, by comparing this and best_so_far and copying this into
1019 * bst_so_far as needed.
1021 static void copy_split_if_better(struct scoreboard
*sb
,
1022 struct blame_entry
*best_so_far
,
1023 struct blame_entry
*this)
1027 if (!this[1].suspect
)
1029 if (best_so_far
[1].suspect
) {
1030 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
1034 for (i
= 0; i
< 3; i
++)
1035 origin_incref(this[i
].suspect
);
1036 decref_split(best_so_far
);
1037 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
1041 * We are looking at a part of the final image represented by
1042 * ent (tlno and same are offset by ent->s_lno).
1043 * tlno is where we are looking at in the final image.
1044 * up to (but not including) same match preimage.
1045 * plno is where we are looking at in the preimage.
1047 * <-------------- final image ---------------------->
1050 * <---------preimage----->
1053 * All line numbers are 0-based.
1055 static void handle_split(struct scoreboard
*sb
,
1056 struct blame_entry
*ent
,
1057 int tlno
, int plno
, int same
,
1058 struct origin
*parent
,
1059 struct blame_entry
*split
)
1061 if (ent
->num_lines
<= tlno
)
1064 struct blame_entry
this[3];
1067 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
1068 copy_split_if_better(sb
, split
, this);
1073 struct handle_split_cb_data
{
1074 struct scoreboard
*sb
;
1075 struct blame_entry
*ent
;
1076 struct origin
*parent
;
1077 struct blame_entry
*split
;
1082 static int handle_split_cb(long start_a
, long count_a
,
1083 long start_b
, long count_b
, void *data
)
1085 struct handle_split_cb_data
*d
= data
;
1086 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1088 d
->plno
= start_a
+ count_a
;
1089 d
->tlno
= start_b
+ count_b
;
1094 * Find the lines from parent that are the same as ent so that
1095 * we can pass blames to it. file_p has the blob contents for
1098 static void find_copy_in_blob(struct scoreboard
*sb
,
1099 struct blame_entry
*ent
,
1100 struct origin
*parent
,
1101 struct blame_entry
*split
,
1106 struct handle_split_cb_data d
;
1108 memset(&d
, 0, sizeof(d
));
1109 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1111 * Prepare mmfile that contains only the lines in ent.
1113 cp
= nth_line(sb
, ent
->lno
);
1114 file_o
.ptr
= (char *) cp
;
1115 file_o
.size
= nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1118 * file_o is a part of final image we are annotating.
1119 * file_p partially may match that image.
1121 memset(split
, 0, sizeof(struct blame_entry
[3]));
1122 diff_hunks(file_p
, &file_o
, 1, handle_split_cb
, &d
);
1123 /* remainder, if any, all match the preimage */
1124 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1127 /* Move all blame entries from list *source that have a score smaller
1128 * than score_min to the front of list *small.
1129 * Returns a pointer to the link pointing to the old head of the small list.
1132 static struct blame_entry
**filter_small(struct scoreboard
*sb
,
1133 struct blame_entry
**small
,
1134 struct blame_entry
**source
,
1137 struct blame_entry
*p
= *source
;
1138 struct blame_entry
*oldsmall
= *small
;
1140 if (ent_score(sb
, p
) <= score_min
) {
1156 * See if lines currently target is suspected for can be attributed to
1159 static void find_move_in_parent(struct scoreboard
*sb
,
1160 struct blame_entry
***blamed
,
1161 struct blame_entry
**toosmall
,
1162 struct origin
*target
,
1163 struct origin
*parent
)
1165 struct blame_entry
*e
, split
[3];
1166 struct blame_entry
*unblamed
= target
->suspects
;
1167 struct blame_entry
*leftover
= NULL
;
1171 return; /* nothing remains for this target */
1173 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
1177 /* At each iteration, unblamed has a NULL-terminated list of
1178 * entries that have not yet been tested for blame. leftover
1179 * contains the reversed list of entries that have been tested
1180 * without being assignable to the parent.
1183 struct blame_entry
**unblamedtail
= &unblamed
;
1184 struct blame_entry
*next
;
1185 for (e
= unblamed
; e
; e
= next
) {
1187 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1188 if (split
[1].suspect
&&
1189 blame_move_score
< ent_score(sb
, &split
[1])) {
1190 split_blame(blamed
, &unblamedtail
, split
, e
);
1195 decref_split(split
);
1197 *unblamedtail
= NULL
;
1198 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_move_score
);
1200 target
->suspects
= reverse_blame(leftover
, NULL
);
1204 struct blame_entry
*ent
;
1205 struct blame_entry split
[3];
1209 * Count the number of entries the target is suspected for,
1210 * and prepare a list of entry and the best split.
1212 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1215 struct blame_entry
*e
;
1217 struct blame_list
*blame_list
= NULL
;
1219 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1222 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1223 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1224 blame_list
[i
++].ent
= e
;
1226 *num_ents_p
= num_ents
;
1231 * For lines target is suspected for, see if we can find code movement
1232 * across file boundary from the parent commit. porigin is the path
1233 * in the parent we already tried.
1235 static void find_copy_in_parent(struct scoreboard
*sb
,
1236 struct blame_entry
***blamed
,
1237 struct blame_entry
**toosmall
,
1238 struct origin
*target
,
1239 struct commit
*parent
,
1240 struct origin
*porigin
,
1243 struct diff_options diff_opts
;
1245 struct blame_list
*blame_list
;
1247 struct blame_entry
*unblamed
= target
->suspects
;
1248 struct blame_entry
*leftover
= NULL
;
1251 return; /* nothing remains for this target */
1253 diff_setup(&diff_opts
);
1254 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1255 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1257 diff_setup_done(&diff_opts
);
1259 /* Try "find copies harder" on new path if requested;
1260 * we do not want to use diffcore_rename() actually to
1261 * match things up; find_copies_harder is set only to
1262 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1263 * and this code needs to be after diff_setup_done(), which
1264 * usually makes find-copies-harder imply copy detection.
1266 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1267 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1268 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1269 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1271 if (is_null_sha1(target
->commit
->object
.sha1
))
1272 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1274 diff_tree_sha1(parent
->tree
->object
.sha1
,
1275 target
->commit
->tree
->object
.sha1
,
1278 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1279 diffcore_std(&diff_opts
);
1282 struct blame_entry
**unblamedtail
= &unblamed
;
1283 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1285 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1286 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1287 struct origin
*norigin
;
1289 struct blame_entry
this[3];
1291 if (!DIFF_FILE_VALID(p
->one
))
1292 continue; /* does not exist in parent */
1293 if (S_ISGITLINK(p
->one
->mode
))
1294 continue; /* ignore git links */
1295 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1296 /* find_move already dealt with this path */
1299 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1300 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1301 norigin
->mode
= p
->one
->mode
;
1302 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
);
1306 for (j
= 0; j
< num_ents
; j
++) {
1307 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1308 norigin
, this, &file_p
);
1309 copy_split_if_better(sb
, blame_list
[j
].split
,
1313 origin_decref(norigin
);
1316 for (j
= 0; j
< num_ents
; j
++) {
1317 struct blame_entry
*split
= blame_list
[j
].split
;
1318 if (split
[1].suspect
&&
1319 blame_copy_score
< ent_score(sb
, &split
[1])) {
1320 split_blame(blamed
, &unblamedtail
, split
,
1323 blame_list
[j
].ent
->next
= leftover
;
1324 leftover
= blame_list
[j
].ent
;
1326 decref_split(split
);
1329 *unblamedtail
= NULL
;
1330 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_copy_score
);
1332 target
->suspects
= reverse_blame(leftover
, NULL
);
1333 diff_flush(&diff_opts
);
1334 free_pathspec(&diff_opts
.pathspec
);
1338 * The blobs of origin and porigin exactly match, so everything
1339 * origin is suspected for can be blamed on the parent.
1341 static void pass_whole_blame(struct scoreboard
*sb
,
1342 struct origin
*origin
, struct origin
*porigin
)
1344 struct blame_entry
*e
, *suspects
;
1346 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1347 /* Steal its file */
1348 porigin
->file
= origin
->file
;
1349 origin
->file
.ptr
= NULL
;
1351 suspects
= origin
->suspects
;
1352 origin
->suspects
= NULL
;
1353 for (e
= suspects
; e
; e
= e
->next
) {
1354 origin_incref(porigin
);
1355 origin_decref(e
->suspect
);
1356 e
->suspect
= porigin
;
1358 queue_blames(sb
, porigin
, suspects
);
1362 * We pass blame from the current commit to its parents. We keep saying
1363 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1364 * exonerate ourselves.
1366 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1369 return commit
->parents
;
1370 return lookup_decoration(&revs
->children
, &commit
->object
);
1373 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1375 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1376 return commit_list_count(l
);
1379 /* Distribute collected unsorted blames to the respected sorted lists
1380 * in the various origins.
1382 static void distribute_blame(struct scoreboard
*sb
, struct blame_entry
*blamed
)
1384 blamed
= blame_sort(blamed
, compare_blame_suspect
);
1387 struct origin
*porigin
= blamed
->suspect
;
1388 struct blame_entry
*suspects
= NULL
;
1390 struct blame_entry
*next
= blamed
->next
;
1391 blamed
->next
= suspects
;
1394 } while (blamed
&& blamed
->suspect
== porigin
);
1395 suspects
= reverse_blame(suspects
, NULL
);
1396 queue_blames(sb
, porigin
, suspects
);
1402 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1404 struct rev_info
*revs
= sb
->revs
;
1405 int i
, pass
, num_sg
;
1406 struct commit
*commit
= origin
->commit
;
1407 struct commit_list
*sg
;
1408 struct origin
*sg_buf
[MAXSG
];
1409 struct origin
*porigin
, **sg_origin
= sg_buf
;
1410 struct blame_entry
*toosmall
= NULL
;
1411 struct blame_entry
*blames
, **blametail
= &blames
;
1413 num_sg
= num_scapegoats(revs
, commit
);
1416 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1417 memset(sg_buf
, 0, sizeof(sg_buf
));
1419 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1422 * The first pass looks for unrenamed path to optimize for
1423 * common cases, then we look for renames in the second pass.
1425 for (pass
= 0; pass
< 2 - no_whole_file_rename
; pass
++) {
1426 struct origin
*(*find
)(struct scoreboard
*,
1427 struct commit
*, struct origin
*);
1428 find
= pass
? find_rename
: find_origin
;
1430 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1432 sg
= sg
->next
, i
++) {
1433 struct commit
*p
= sg
->item
;
1438 if (parse_commit(p
))
1440 porigin
= find(sb
, p
, origin
);
1443 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1444 pass_whole_blame(sb
, origin
, porigin
);
1445 origin_decref(porigin
);
1448 for (j
= same
= 0; j
< i
; j
++)
1450 !hashcmp(sg_origin
[j
]->blob_sha1
,
1451 porigin
->blob_sha1
)) {
1456 sg_origin
[i
] = porigin
;
1458 origin_decref(porigin
);
1463 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1465 sg
= sg
->next
, i
++) {
1466 struct origin
*porigin
= sg_origin
[i
];
1469 if (!origin
->previous
) {
1470 origin_incref(porigin
);
1471 origin
->previous
= porigin
;
1473 pass_blame_to_parent(sb
, origin
, porigin
);
1474 if (!origin
->suspects
)
1479 * Optionally find moves in parents' files.
1481 if (opt
& PICKAXE_BLAME_MOVE
) {
1482 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_move_score
);
1483 if (origin
->suspects
) {
1484 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1486 sg
= sg
->next
, i
++) {
1487 struct origin
*porigin
= sg_origin
[i
];
1490 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1491 if (!origin
->suspects
)
1498 * Optionally find copies from parents' files.
1500 if (opt
& PICKAXE_BLAME_COPY
) {
1501 if (blame_copy_score
> blame_move_score
)
1502 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1503 else if (blame_copy_score
< blame_move_score
) {
1504 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1506 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1508 if (!origin
->suspects
)
1511 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1513 sg
= sg
->next
, i
++) {
1514 struct origin
*porigin
= sg_origin
[i
];
1515 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1516 origin
, sg
->item
, porigin
, opt
);
1517 if (!origin
->suspects
)
1524 distribute_blame(sb
, blames
);
1526 * prepend toosmall to origin->suspects
1528 * There is no point in sorting: this ends up on a big
1529 * unsorted list in the caller anyway.
1532 struct blame_entry
**tail
= &toosmall
;
1534 tail
= &(*tail
)->next
;
1535 *tail
= origin
->suspects
;
1536 origin
->suspects
= toosmall
;
1538 for (i
= 0; i
< num_sg
; i
++) {
1540 drop_origin_blob(sg_origin
[i
]);
1541 origin_decref(sg_origin
[i
]);
1544 drop_origin_blob(origin
);
1545 if (sg_buf
!= sg_origin
)
1550 * Information on commits, used for output.
1552 struct commit_info
{
1553 struct strbuf author
;
1554 struct strbuf author_mail
;
1555 unsigned long author_time
;
1556 struct strbuf author_tz
;
1558 /* filled only when asked for details */
1559 struct strbuf committer
;
1560 struct strbuf committer_mail
;
1561 unsigned long committer_time
;
1562 struct strbuf committer_tz
;
1564 struct strbuf summary
;
1568 * Parse author/committer line in the commit object buffer
1570 static void get_ac_line(const char *inbuf
, const char *what
,
1571 struct strbuf
*name
, struct strbuf
*mail
,
1572 unsigned long *time
, struct strbuf
*tz
)
1574 struct ident_split ident
;
1575 size_t len
, maillen
, namelen
;
1577 const char *namebuf
, *mailbuf
;
1579 tmp
= strstr(inbuf
, what
);
1582 tmp
+= strlen(what
);
1583 endp
= strchr(tmp
, '\n');
1589 if (split_ident_line(&ident
, tmp
, len
)) {
1593 strbuf_addstr(name
, tmp
);
1594 strbuf_addstr(mail
, tmp
);
1595 strbuf_addstr(tz
, tmp
);
1600 namelen
= ident
.name_end
- ident
.name_begin
;
1601 namebuf
= ident
.name_begin
;
1603 maillen
= ident
.mail_end
- ident
.mail_begin
;
1604 mailbuf
= ident
.mail_begin
;
1606 if (ident
.date_begin
&& ident
.date_end
)
1607 *time
= strtoul(ident
.date_begin
, NULL
, 10);
1611 if (ident
.tz_begin
&& ident
.tz_end
)
1612 strbuf_add(tz
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
1614 strbuf_addstr(tz
, "(unknown)");
1617 * Now, convert both name and e-mail using mailmap
1619 map_user(&mailmap
, &mailbuf
, &maillen
,
1620 &namebuf
, &namelen
);
1622 strbuf_addf(mail
, "<%.*s>", (int)maillen
, mailbuf
);
1623 strbuf_add(name
, namebuf
, namelen
);
1626 static void commit_info_init(struct commit_info
*ci
)
1629 strbuf_init(&ci
->author
, 0);
1630 strbuf_init(&ci
->author_mail
, 0);
1631 strbuf_init(&ci
->author_tz
, 0);
1632 strbuf_init(&ci
->committer
, 0);
1633 strbuf_init(&ci
->committer_mail
, 0);
1634 strbuf_init(&ci
->committer_tz
, 0);
1635 strbuf_init(&ci
->summary
, 0);
1638 static void commit_info_destroy(struct commit_info
*ci
)
1641 strbuf_release(&ci
->author
);
1642 strbuf_release(&ci
->author_mail
);
1643 strbuf_release(&ci
->author_tz
);
1644 strbuf_release(&ci
->committer
);
1645 strbuf_release(&ci
->committer_mail
);
1646 strbuf_release(&ci
->committer_tz
);
1647 strbuf_release(&ci
->summary
);
1650 static void get_commit_info(struct commit
*commit
,
1651 struct commit_info
*ret
,
1655 const char *subject
, *encoding
;
1656 const char *message
;
1658 commit_info_init(ret
);
1660 encoding
= get_log_output_encoding();
1661 message
= logmsg_reencode(commit
, NULL
, encoding
);
1662 get_ac_line(message
, "\nauthor ",
1663 &ret
->author
, &ret
->author_mail
,
1664 &ret
->author_time
, &ret
->author_tz
);
1667 unuse_commit_buffer(commit
, message
);
1671 get_ac_line(message
, "\ncommitter ",
1672 &ret
->committer
, &ret
->committer_mail
,
1673 &ret
->committer_time
, &ret
->committer_tz
);
1675 len
= find_commit_subject(message
, &subject
);
1677 strbuf_add(&ret
->summary
, subject
, len
);
1679 strbuf_addf(&ret
->summary
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1681 unuse_commit_buffer(commit
, message
);
1685 * To allow LF and other nonportable characters in pathnames,
1686 * they are c-style quoted as needed.
1688 static void write_filename_info(const char *path
)
1690 printf("filename ");
1691 write_name_quoted(path
, stdout
, '\n');
1695 * Porcelain/Incremental format wants to show a lot of details per
1696 * commit. Instead of repeating this every line, emit it only once,
1697 * the first time each commit appears in the output (unless the
1698 * user has specifically asked for us to repeat).
1700 static int emit_one_suspect_detail(struct origin
*suspect
, int repeat
)
1702 struct commit_info ci
;
1704 if (!repeat
&& (suspect
->commit
->object
.flags
& METAINFO_SHOWN
))
1707 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1708 get_commit_info(suspect
->commit
, &ci
, 1);
1709 printf("author %s\n", ci
.author
.buf
);
1710 printf("author-mail %s\n", ci
.author_mail
.buf
);
1711 printf("author-time %lu\n", ci
.author_time
);
1712 printf("author-tz %s\n", ci
.author_tz
.buf
);
1713 printf("committer %s\n", ci
.committer
.buf
);
1714 printf("committer-mail %s\n", ci
.committer_mail
.buf
);
1715 printf("committer-time %lu\n", ci
.committer_time
);
1716 printf("committer-tz %s\n", ci
.committer_tz
.buf
);
1717 printf("summary %s\n", ci
.summary
.buf
);
1718 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1719 printf("boundary\n");
1720 if (suspect
->previous
) {
1721 struct origin
*prev
= suspect
->previous
;
1722 printf("previous %s ", sha1_to_hex(prev
->commit
->object
.sha1
));
1723 write_name_quoted(prev
->path
, stdout
, '\n');
1726 commit_info_destroy(&ci
);
1732 * The blame_entry is found to be guilty for the range.
1733 * Show it in incremental output.
1735 static void found_guilty_entry(struct blame_entry
*ent
)
1738 struct origin
*suspect
= ent
->suspect
;
1740 printf("%s %d %d %d\n",
1741 sha1_to_hex(suspect
->commit
->object
.sha1
),
1742 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1743 emit_one_suspect_detail(suspect
, 0);
1744 write_filename_info(suspect
->path
);
1745 maybe_flush_or_die(stdout
, "stdout");
1750 * The main loop -- while we have blobs with lines whose true origin
1751 * is still unknown, pick one blob, and allow its lines to pass blames
1752 * to its parents. */
1753 static void assign_blame(struct scoreboard
*sb
, int opt
)
1755 struct rev_info
*revs
= sb
->revs
;
1756 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1759 struct blame_entry
*ent
;
1760 struct origin
*suspect
= commit
->util
;
1762 /* find one suspect to break down */
1763 while (suspect
&& !suspect
->suspects
)
1764 suspect
= suspect
->next
;
1767 commit
= prio_queue_get(&sb
->commits
);
1771 assert(commit
== suspect
->commit
);
1774 * We will use this suspect later in the loop,
1775 * so hold onto it in the meantime.
1777 origin_incref(suspect
);
1778 parse_commit(commit
);
1780 (!(commit
->object
.flags
& UNINTERESTING
) &&
1781 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1782 pass_blame(sb
, suspect
, opt
);
1784 commit
->object
.flags
|= UNINTERESTING
;
1785 if (commit
->object
.parsed
)
1786 mark_parents_uninteresting(commit
);
1788 /* treat root commit as boundary */
1789 if (!commit
->parents
&& !show_root
)
1790 commit
->object
.flags
|= UNINTERESTING
;
1792 /* Take responsibility for the remaining entries */
1793 ent
= suspect
->suspects
;
1795 suspect
->guilty
= 1;
1797 struct blame_entry
*next
= ent
->next
;
1798 found_guilty_entry(ent
);
1803 ent
->next
= sb
->ent
;
1804 sb
->ent
= suspect
->suspects
;
1805 suspect
->suspects
= NULL
;
1809 origin_decref(suspect
);
1811 if (DEBUG
) /* sanity */
1812 sanity_check_refcnt(sb
);
1816 static const char *format_time(unsigned long time
, const char *tz_str
,
1819 static struct strbuf time_buf
= STRBUF_INIT
;
1821 strbuf_reset(&time_buf
);
1822 if (show_raw_time
) {
1823 strbuf_addf(&time_buf
, "%lu %s", time
, tz_str
);
1826 const char *time_str
;
1830 time_str
= show_date(time
, tz
, &blame_date_mode
);
1831 strbuf_addstr(&time_buf
, time_str
);
1833 * Add space paddings to time_buf to display a fixed width
1834 * string, and use time_width for display width calibration.
1836 for (time_width
= utf8_strwidth(time_str
);
1837 time_width
< blame_date_width
;
1839 strbuf_addch(&time_buf
, ' ');
1841 return time_buf
.buf
;
1844 #define OUTPUT_ANNOTATE_COMPAT 001
1845 #define OUTPUT_LONG_OBJECT_NAME 002
1846 #define OUTPUT_RAW_TIMESTAMP 004
1847 #define OUTPUT_PORCELAIN 010
1848 #define OUTPUT_SHOW_NAME 020
1849 #define OUTPUT_SHOW_NUMBER 040
1850 #define OUTPUT_SHOW_SCORE 0100
1851 #define OUTPUT_NO_AUTHOR 0200
1852 #define OUTPUT_SHOW_EMAIL 0400
1853 #define OUTPUT_LINE_PORCELAIN 01000
1855 static void emit_porcelain_details(struct origin
*suspect
, int repeat
)
1857 if (emit_one_suspect_detail(suspect
, repeat
) ||
1858 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
1859 write_filename_info(suspect
->path
);
1862 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
,
1865 int repeat
= opt
& OUTPUT_LINE_PORCELAIN
;
1868 struct origin
*suspect
= ent
->suspect
;
1871 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1872 printf("%s %d %d %d\n",
1877 emit_porcelain_details(suspect
, repeat
);
1879 cp
= nth_line(sb
, ent
->lno
);
1880 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1883 printf("%s %d %d\n", hex
,
1884 ent
->s_lno
+ 1 + cnt
,
1885 ent
->lno
+ 1 + cnt
);
1887 emit_porcelain_details(suspect
, 1);
1893 } while (ch
!= '\n' &&
1894 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1897 if (sb
->final_buf_size
&& cp
[-1] != '\n')
1901 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1905 struct origin
*suspect
= ent
->suspect
;
1906 struct commit_info ci
;
1908 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1910 get_commit_info(suspect
->commit
, &ci
, 1);
1911 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1913 cp
= nth_line(sb
, ent
->lno
);
1914 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1916 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : abbrev
;
1918 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1920 memset(hex
, ' ', length
);
1921 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
1927 printf("%.*s", length
, hex
);
1928 if (opt
& OUTPUT_ANNOTATE_COMPAT
) {
1930 if (opt
& OUTPUT_SHOW_EMAIL
)
1931 name
= ci
.author_mail
.buf
;
1933 name
= ci
.author
.buf
;
1934 printf("\t(%10s\t%10s\t%d)", name
,
1935 format_time(ci
.author_time
, ci
.author_tz
.buf
,
1937 ent
->lno
+ 1 + cnt
);
1939 if (opt
& OUTPUT_SHOW_SCORE
)
1941 max_score_digits
, ent
->score
,
1942 ent
->suspect
->refcnt
);
1943 if (opt
& OUTPUT_SHOW_NAME
)
1944 printf(" %-*.*s", longest_file
, longest_file
,
1946 if (opt
& OUTPUT_SHOW_NUMBER
)
1947 printf(" %*d", max_orig_digits
,
1948 ent
->s_lno
+ 1 + cnt
);
1950 if (!(opt
& OUTPUT_NO_AUTHOR
)) {
1953 if (opt
& OUTPUT_SHOW_EMAIL
)
1954 name
= ci
.author_mail
.buf
;
1956 name
= ci
.author
.buf
;
1957 pad
= longest_author
- utf8_strwidth(name
);
1958 printf(" (%s%*s %10s",
1960 format_time(ci
.author_time
,
1965 max_digits
, ent
->lno
+ 1 + cnt
);
1970 } while (ch
!= '\n' &&
1971 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1974 if (sb
->final_buf_size
&& cp
[-1] != '\n')
1977 commit_info_destroy(&ci
);
1980 static void output(struct scoreboard
*sb
, int option
)
1982 struct blame_entry
*ent
;
1984 if (option
& OUTPUT_PORCELAIN
) {
1985 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1987 struct origin
*suspect
;
1988 struct commit
*commit
= ent
->suspect
->commit
;
1989 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1991 for (suspect
= commit
->util
; suspect
; suspect
= suspect
->next
) {
1992 if (suspect
->guilty
&& count
++) {
1993 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
2000 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2001 if (option
& OUTPUT_PORCELAIN
)
2002 emit_porcelain(sb
, ent
, option
);
2004 emit_other(sb
, ent
, option
);
2009 static const char *get_next_line(const char *start
, const char *end
)
2011 const char *nl
= memchr(start
, '\n', end
- start
);
2012 return nl
? nl
+ 1 : end
;
2016 * To allow quick access to the contents of nth line in the
2017 * final image, prepare an index in the scoreboard.
2019 static int prepare_lines(struct scoreboard
*sb
)
2021 const char *buf
= sb
->final_buf
;
2022 unsigned long len
= sb
->final_buf_size
;
2023 const char *end
= buf
+ len
;
2028 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2031 sb
->lineno
= lineno
= xmalloc(sizeof(*sb
->lineno
) * (num
+ 1));
2033 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2034 *lineno
++ = p
- buf
;
2038 sb
->num_lines
= num
;
2039 return sb
->num_lines
;
2043 * Add phony grafts for use with -S; this is primarily to
2044 * support git's cvsserver that wants to give a linear history
2047 static int read_ancestry(const char *graft_file
)
2049 FILE *fp
= fopen(graft_file
, "r");
2050 struct strbuf buf
= STRBUF_INIT
;
2053 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
2054 /* The format is just "Commit Parent1 Parent2 ...\n" */
2055 struct commit_graft
*graft
= read_graft_line(buf
.buf
, buf
.len
);
2057 register_commit_graft(graft
, 0);
2060 strbuf_release(&buf
);
2064 static int update_auto_abbrev(int auto_abbrev
, struct origin
*suspect
)
2066 const char *uniq
= find_unique_abbrev(suspect
->commit
->object
.sha1
,
2068 int len
= strlen(uniq
);
2069 if (auto_abbrev
< len
)
2075 * How many columns do we need to show line numbers, authors,
2078 static void find_alignment(struct scoreboard
*sb
, int *option
)
2080 int longest_src_lines
= 0;
2081 int longest_dst_lines
= 0;
2082 unsigned largest_score
= 0;
2083 struct blame_entry
*e
;
2084 int compute_auto_abbrev
= (abbrev
< 0);
2085 int auto_abbrev
= default_abbrev
;
2087 for (e
= sb
->ent
; e
; e
= e
->next
) {
2088 struct origin
*suspect
= e
->suspect
;
2091 if (compute_auto_abbrev
)
2092 auto_abbrev
= update_auto_abbrev(auto_abbrev
, suspect
);
2093 if (strcmp(suspect
->path
, sb
->path
))
2094 *option
|= OUTPUT_SHOW_NAME
;
2095 num
= strlen(suspect
->path
);
2096 if (longest_file
< num
)
2098 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
2099 struct commit_info ci
;
2100 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
2101 get_commit_info(suspect
->commit
, &ci
, 1);
2102 if (*option
& OUTPUT_SHOW_EMAIL
)
2103 num
= utf8_strwidth(ci
.author_mail
.buf
);
2105 num
= utf8_strwidth(ci
.author
.buf
);
2106 if (longest_author
< num
)
2107 longest_author
= num
;
2108 commit_info_destroy(&ci
);
2110 num
= e
->s_lno
+ e
->num_lines
;
2111 if (longest_src_lines
< num
)
2112 longest_src_lines
= num
;
2113 num
= e
->lno
+ e
->num_lines
;
2114 if (longest_dst_lines
< num
)
2115 longest_dst_lines
= num
;
2116 if (largest_score
< ent_score(sb
, e
))
2117 largest_score
= ent_score(sb
, e
);
2119 max_orig_digits
= decimal_width(longest_src_lines
);
2120 max_digits
= decimal_width(longest_dst_lines
);
2121 max_score_digits
= decimal_width(largest_score
);
2123 if (compute_auto_abbrev
)
2124 /* one more abbrev length is needed for the boundary commit */
2125 abbrev
= auto_abbrev
+ 1;
2129 * For debugging -- origin is refcounted, and this asserts that
2130 * we do not underflow.
2132 static void sanity_check_refcnt(struct scoreboard
*sb
)
2135 struct blame_entry
*ent
;
2137 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2138 /* Nobody should have zero or negative refcnt */
2139 if (ent
->suspect
->refcnt
<= 0) {
2140 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
2142 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
2143 ent
->suspect
->refcnt
);
2149 find_alignment(sb
, &opt
);
2151 die("Baa %d!", baa
);
2155 static unsigned parse_score(const char *arg
)
2158 unsigned long score
= strtoul(arg
, &end
, 10);
2164 static const char *add_prefix(const char *prefix
, const char *path
)
2166 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
2169 static int git_blame_config(const char *var
, const char *value
, void *cb
)
2171 if (!strcmp(var
, "blame.showroot")) {
2172 show_root
= git_config_bool(var
, value
);
2175 if (!strcmp(var
, "blame.blankboundary")) {
2176 blank_boundary
= git_config_bool(var
, value
);
2179 if (!strcmp(var
, "blame.showemail")) {
2180 int *output_option
= cb
;
2181 if (git_config_bool(var
, value
))
2182 *output_option
|= OUTPUT_SHOW_EMAIL
;
2184 *output_option
&= ~OUTPUT_SHOW_EMAIL
;
2187 if (!strcmp(var
, "blame.date")) {
2189 return config_error_nonbool(var
);
2190 parse_date_format(value
, &blame_date_mode
);
2194 if (userdiff_config(var
, value
) < 0)
2197 return git_default_config(var
, value
, cb
);
2200 static void verify_working_tree_path(struct commit
*work_tree
, const char *path
)
2202 struct commit_list
*parents
;
2204 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
2205 const unsigned char *commit_sha1
= parents
->item
->object
.sha1
;
2206 unsigned char blob_sha1
[20];
2209 if (!get_tree_entry(commit_sha1
, path
, blob_sha1
, &mode
) &&
2210 sha1_object_info(blob_sha1
, NULL
) == OBJ_BLOB
)
2213 die("no such path '%s' in HEAD", path
);
2216 static struct commit_list
**append_parent(struct commit_list
**tail
, const unsigned char *sha1
)
2218 struct commit
*parent
;
2220 parent
= lookup_commit_reference(sha1
);
2222 die("no such commit %s", sha1_to_hex(sha1
));
2223 return &commit_list_insert(parent
, tail
)->next
;
2226 static void append_merge_parents(struct commit_list
**tail
)
2229 const char *merge_head_file
= git_path("MERGE_HEAD");
2230 struct strbuf line
= STRBUF_INIT
;
2232 merge_head
= open(merge_head_file
, O_RDONLY
);
2233 if (merge_head
< 0) {
2234 if (errno
== ENOENT
)
2236 die("cannot open '%s' for reading", merge_head_file
);
2239 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
2240 unsigned char sha1
[20];
2241 if (line
.len
< 40 || get_sha1_hex(line
.buf
, sha1
))
2242 die("unknown line in '%s': %s", merge_head_file
, line
.buf
);
2243 tail
= append_parent(tail
, sha1
);
2246 strbuf_release(&line
);
2250 * This isn't as simple as passing sb->buf and sb->len, because we
2251 * want to transfer ownership of the buffer to the commit (so we
2254 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
2257 void *buf
= strbuf_detach(sb
, &len
);
2258 set_commit_buffer(c
, buf
, len
);
2262 * Prepare a dummy commit that represents the work tree (or staged) item.
2263 * Note that annotating work tree item never works in the reverse.
2265 static struct commit
*fake_working_tree_commit(struct diff_options
*opt
,
2267 const char *contents_from
)
2269 struct commit
*commit
;
2270 struct origin
*origin
;
2271 struct commit_list
**parent_tail
, *parent
;
2272 unsigned char head_sha1
[20];
2273 struct strbuf buf
= STRBUF_INIT
;
2277 struct cache_entry
*ce
;
2279 struct strbuf msg
= STRBUF_INIT
;
2282 commit
= alloc_commit_node();
2283 commit
->object
.parsed
= 1;
2285 parent_tail
= &commit
->parents
;
2287 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
2288 die("no such ref: HEAD");
2290 parent_tail
= append_parent(parent_tail
, head_sha1
);
2291 append_merge_parents(parent_tail
);
2292 verify_working_tree_path(commit
, path
);
2294 origin
= make_origin(commit
, path
);
2296 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2297 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
2298 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
2299 strbuf_addf(&msg
, "parent %s\n",
2300 sha1_to_hex(parent
->item
->object
.sha1
));
2304 "Version of %s from %s\n",
2306 (!contents_from
? path
:
2307 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
2308 set_commit_buffer_from_strbuf(commit
, &msg
);
2310 if (!contents_from
|| strcmp("-", contents_from
)) {
2312 const char *read_from
;
2314 unsigned long buf_len
;
2316 if (contents_from
) {
2317 if (stat(contents_from
, &st
) < 0)
2318 die_errno("Cannot stat '%s'", contents_from
);
2319 read_from
= contents_from
;
2322 if (lstat(path
, &st
) < 0)
2323 die_errno("Cannot lstat '%s'", path
);
2326 mode
= canon_mode(st
.st_mode
);
2328 switch (st
.st_mode
& S_IFMT
) {
2330 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
2331 textconv_object(read_from
, mode
, null_sha1
, 0, &buf_ptr
, &buf_len
))
2332 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
2333 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2334 die_errno("cannot open or read '%s'", read_from
);
2337 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
2338 die_errno("cannot readlink '%s'", read_from
);
2341 die("unsupported file type %s", read_from
);
2345 /* Reading from stdin */
2347 if (strbuf_read(&buf
, 0, 0) < 0)
2348 die_errno("failed to read from stdin");
2350 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2351 origin
->file
.ptr
= buf
.buf
;
2352 origin
->file
.size
= buf
.len
;
2353 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2356 * Read the current index, replace the path entry with
2357 * origin->blob_sha1 without mucking with its mode or type
2358 * bits; we are not going to write this index out -- we just
2359 * want to run "diff-index --cached".
2366 int pos
= cache_name_pos(path
, len
);
2368 mode
= active_cache
[pos
]->ce_mode
;
2370 /* Let's not bother reading from HEAD tree */
2371 mode
= S_IFREG
| 0644;
2373 size
= cache_entry_size(len
);
2374 ce
= xcalloc(1, size
);
2375 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2376 memcpy(ce
->name
, path
, len
);
2377 ce
->ce_flags
= create_ce_flags(0);
2378 ce
->ce_namelen
= len
;
2379 ce
->ce_mode
= create_ce_mode(mode
);
2380 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2383 * We are not going to write this out, so this does not matter
2384 * right now, but someday we might optimize diff-index --cached
2385 * with cache-tree information.
2387 cache_tree_invalidate_path(&the_index
, path
);
2392 static char *prepare_final(struct scoreboard
*sb
)
2395 const char *final_commit_name
= NULL
;
2396 struct rev_info
*revs
= sb
->revs
;
2399 * There must be one and only one positive commit in the
2400 * revs->pending array.
2402 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2403 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2404 if (obj
->flags
& UNINTERESTING
)
2406 while (obj
->type
== OBJ_TAG
)
2407 obj
= deref_tag(obj
, NULL
, 0);
2408 if (obj
->type
!= OBJ_COMMIT
)
2409 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2411 die("More than one commit to dig from %s and %s?",
2412 revs
->pending
.objects
[i
].name
,
2414 sb
->final
= (struct commit
*) obj
;
2415 final_commit_name
= revs
->pending
.objects
[i
].name
;
2417 return xstrdup_or_null(final_commit_name
);
2420 static char *prepare_initial(struct scoreboard
*sb
)
2423 const char *final_commit_name
= NULL
;
2424 struct rev_info
*revs
= sb
->revs
;
2427 * There must be one and only one negative commit, and it must be
2430 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2431 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2432 if (!(obj
->flags
& UNINTERESTING
))
2434 while (obj
->type
== OBJ_TAG
)
2435 obj
= deref_tag(obj
, NULL
, 0);
2436 if (obj
->type
!= OBJ_COMMIT
)
2437 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2439 die("More than one commit to dig down to %s and %s?",
2440 revs
->pending
.objects
[i
].name
,
2442 sb
->final
= (struct commit
*) obj
;
2443 final_commit_name
= revs
->pending
.objects
[i
].name
;
2445 if (!final_commit_name
)
2446 die("No commit to dig down to?");
2447 return xstrdup(final_commit_name
);
2450 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2452 int *opt
= option
->value
;
2455 * -C enables copy from removed files;
2456 * -C -C enables copy from existing files, but only
2457 * when blaming a new file;
2458 * -C -C -C enables copy from existing files for
2461 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2462 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2463 if (*opt
& PICKAXE_BLAME_COPY
)
2464 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2465 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2468 blame_copy_score
= parse_score(arg
);
2472 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2474 int *opt
= option
->value
;
2476 *opt
|= PICKAXE_BLAME_MOVE
;
2479 blame_move_score
= parse_score(arg
);
2483 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2485 struct rev_info revs
;
2487 struct scoreboard sb
;
2489 struct blame_entry
*ent
= NULL
;
2490 long dashdash_pos
, lno
;
2491 char *final_commit_name
= NULL
;
2492 enum object_type type
;
2494 static struct string_list range_list
;
2495 static int output_option
= 0, opt
= 0;
2496 static int show_stats
= 0;
2497 static const char *revs_file
= NULL
;
2498 static const char *contents_from
= NULL
;
2499 static const struct option options
[] = {
2500 OPT_BOOL(0, "incremental", &incremental
, N_("Show blame entries as we find them, incrementally")),
2501 OPT_BOOL('b', NULL
, &blank_boundary
, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2502 OPT_BOOL(0, "root", &show_root
, N_("Do not treat root commits as boundaries (Default: off)")),
2503 OPT_BOOL(0, "show-stats", &show_stats
, N_("Show work cost statistics")),
2504 OPT_BIT(0, "score-debug", &output_option
, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE
),
2505 OPT_BIT('f', "show-name", &output_option
, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME
),
2506 OPT_BIT('n', "show-number", &output_option
, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER
),
2507 OPT_BIT('p', "porcelain", &output_option
, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN
),
2508 OPT_BIT(0, "line-porcelain", &output_option
, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN
|OUTPUT_LINE_PORCELAIN
),
2509 OPT_BIT('c', NULL
, &output_option
, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT
),
2510 OPT_BIT('t', NULL
, &output_option
, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP
),
2511 OPT_BIT('l', NULL
, &output_option
, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME
),
2512 OPT_BIT('s', NULL
, &output_option
, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR
),
2513 OPT_BIT('e', "show-email", &output_option
, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL
),
2514 OPT_BIT('w', NULL
, &xdl_opts
, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE
),
2515 OPT_BIT(0, "minimal", &xdl_opts
, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL
),
2516 OPT_STRING('S', NULL
, &revs_file
, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2517 OPT_STRING(0, "contents", &contents_from
, N_("file"), N_("Use <file>'s contents as the final image")),
2518 { OPTION_CALLBACK
, 'C', NULL
, &opt
, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG
, blame_copy_callback
},
2519 { OPTION_CALLBACK
, 'M', NULL
, &opt
, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG
, blame_move_callback
},
2520 OPT_STRING_LIST('L', NULL
, &range_list
, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2521 OPT__ABBREV(&abbrev
),
2525 struct parse_opt_ctx_t ctx
;
2526 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2527 struct range_set ranges
;
2528 unsigned int range_i
;
2531 git_config(git_blame_config
, &output_option
);
2532 init_revisions(&revs
, NULL
);
2533 revs
.date_mode
= blame_date_mode
;
2534 DIFF_OPT_SET(&revs
.diffopt
, ALLOW_TEXTCONV
);
2535 DIFF_OPT_SET(&revs
.diffopt
, FOLLOW_RENAMES
);
2537 save_commit_buffer
= 0;
2540 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
2541 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
2543 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2544 case PARSE_OPT_HELP
:
2546 case PARSE_OPT_DONE
:
2548 dashdash_pos
= ctx
.cpidx
;
2552 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2553 ctx
.argv
[0] = "--children";
2556 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2559 no_whole_file_rename
= !DIFF_OPT_TST(&revs
.diffopt
, FOLLOW_RENAMES
);
2560 DIFF_OPT_CLR(&revs
.diffopt
, FOLLOW_RENAMES
);
2561 argc
= parse_options_end(&ctx
);
2564 /* one more abbrev length is needed for the boundary commit */
2567 if (revs_file
&& read_ancestry(revs_file
))
2568 die_errno("reading graft file '%s' failed", revs_file
);
2570 if (cmd_is_annotate
) {
2571 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2572 blame_date_mode
.type
= DATE_ISO8601
;
2574 blame_date_mode
= revs
.date_mode
;
2577 /* The maximum width used to show the dates */
2578 switch (blame_date_mode
.type
) {
2580 blame_date_width
= sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2582 case DATE_ISO8601_STRICT
:
2583 blame_date_width
= sizeof("2006-10-19T16:00:04-07:00");
2586 blame_date_width
= sizeof("2006-10-19 16:00:04 -0700");
2589 blame_date_width
= sizeof("1161298804 -0700");
2592 blame_date_width
= sizeof("2006-10-19");
2595 /* TRANSLATORS: This string is used to tell us the maximum
2596 display width for a relative timestamp in "git blame"
2597 output. For C locale, "4 years, 11 months ago", which
2598 takes 22 places, is the longest among various forms of
2599 relative timestamps, but your language may need more or
2600 fewer display columns. */
2601 blame_date_width
= utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2604 blame_date_width
= sizeof("Thu Oct 19 16:00:04 2006 -0700");
2607 blame_date_width
= strlen(show_date(0, 0, &blame_date_mode
)) + 1; /* add the null */
2610 blame_date_width
-= 1; /* strip the null */
2612 if (DIFF_OPT_TST(&revs
.diffopt
, FIND_COPIES_HARDER
))
2613 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
2614 PICKAXE_BLAME_COPY_HARDER
);
2616 if (!blame_move_score
)
2617 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2618 if (!blame_copy_score
)
2619 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2622 * We have collected options unknown to us in argv[1..unk]
2623 * which are to be passed to revision machinery if we are
2624 * going to do the "bottom" processing.
2626 * The remaining are:
2628 * (1) if dashdash_pos != 0, it is either
2629 * "blame [revisions] -- <path>" or
2630 * "blame -- <path> <rev>"
2632 * (2) otherwise, it is one of the two:
2633 * "blame [revisions] <path>"
2634 * "blame <path> <rev>"
2636 * Note that we must strip out <path> from the arguments: we do not
2637 * want the path pruning but we may want "bottom" processing.
2640 switch (argc
- dashdash_pos
- 1) {
2643 usage_with_options(blame_opt_usage
, options
);
2644 /* reorder for the new way: <rev> -- <path> */
2650 path
= add_prefix(prefix
, argv
[--argc
]);
2654 usage_with_options(blame_opt_usage
, options
);
2658 usage_with_options(blame_opt_usage
, options
);
2659 path
= add_prefix(prefix
, argv
[argc
- 1]);
2660 if (argc
== 3 && !file_exists(path
)) { /* (2b) */
2661 path
= add_prefix(prefix
, argv
[1]);
2664 argv
[argc
- 1] = "--";
2667 if (!file_exists(path
))
2668 die_errno("cannot stat path '%s'", path
);
2671 revs
.disable_stdin
= 1;
2672 setup_revisions(argc
, argv
, &revs
, NULL
);
2673 memset(&sb
, 0, sizeof(sb
));
2677 final_commit_name
= prepare_final(&sb
);
2678 sb
.commits
.compare
= compare_commits_by_commit_date
;
2680 else if (contents_from
)
2681 die("--contents and --children do not blend well.");
2683 final_commit_name
= prepare_initial(&sb
);
2684 sb
.commits
.compare
= compare_commits_by_reverse_commit_date
;
2689 * "--not A B -- path" without anything positive;
2690 * do not default to HEAD, but use the working tree
2694 sb
.final
= fake_working_tree_commit(&sb
.revs
->diffopt
,
2695 path
, contents_from
);
2696 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2698 else if (contents_from
)
2699 die("Cannot use --contents with final commit object name");
2702 * If we have bottom, this will mark the ancestors of the
2703 * bottom commits we would reach while traversing as
2706 if (prepare_revision_walk(&revs
))
2707 die(_("revision walk setup failed"));
2709 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2711 sb
.final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
2712 sb
.final_buf_size
= o
->file
.size
;
2715 o
= get_origin(&sb
, sb
.final
, path
);
2716 if (fill_blob_sha1_and_mode(o
))
2717 die("no such path %s in %s", path
, final_commit_name
);
2719 if (DIFF_OPT_TST(&sb
.revs
->diffopt
, ALLOW_TEXTCONV
) &&
2720 textconv_object(path
, o
->mode
, o
->blob_sha1
, 1, (char **) &sb
.final_buf
,
2721 &sb
.final_buf_size
))
2724 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2725 &sb
.final_buf_size
);
2728 die("Cannot read blob %s for path %s",
2729 sha1_to_hex(o
->blob_sha1
),
2733 lno
= prepare_lines(&sb
);
2735 if (lno
&& !range_list
.nr
)
2736 string_list_append(&range_list
, xstrdup("1"));
2739 range_set_init(&ranges
, range_list
.nr
);
2740 for (range_i
= 0; range_i
< range_list
.nr
; ++range_i
) {
2742 if (parse_range_arg(range_list
.items
[range_i
].string
,
2743 nth_line_cb
, &sb
, lno
, anchor
,
2744 &bottom
, &top
, sb
.path
))
2746 if (lno
< top
|| ((lno
|| bottom
) && lno
< bottom
))
2747 die("file %s has only %lu lines", path
, lno
);
2753 range_set_append_unsafe(&ranges
, bottom
, top
);
2756 sort_and_merge_range_set(&ranges
);
2758 for (range_i
= ranges
.nr
; range_i
> 0; --range_i
) {
2759 const struct range
*r
= &ranges
.ranges
[range_i
- 1];
2760 long bottom
= r
->start
;
2762 struct blame_entry
*next
= ent
;
2763 ent
= xcalloc(1, sizeof(*ent
));
2765 ent
->num_lines
= top
- bottom
;
2767 ent
->s_lno
= bottom
;
2773 prio_queue_put(&sb
.commits
, o
->commit
);
2777 range_set_release(&ranges
);
2778 string_list_clear(&range_list
, 0);
2783 read_mailmap(&mailmap
, NULL
);
2788 assign_blame(&sb
, opt
);
2790 free(final_commit_name
);
2795 sb
.ent
= blame_sort(sb
.ent
, compare_blame_final
);
2799 if (!(output_option
& OUTPUT_PORCELAIN
))
2800 find_alignment(&sb
, &output_option
);
2802 output(&sb
, output_option
);
2803 free((void *)sb
.final_buf
);
2804 for (ent
= sb
.ent
; ent
; ) {
2805 struct blame_entry
*e
= ent
->next
;
2811 printf("num read blob: %d\n", num_read_blob
);
2812 printf("num get patch: %d\n", num_get_patch
);
2813 printf("num commits: %d\n", num_commits
);