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"
30 static char blame_usage
[] = N_("git blame [options] [rev-opts] [rev] [--] file");
32 static const char *blame_opt_usage
[] = {
35 N_("[rev-opts] are documented in git-rev-list(1)"),
39 static int longest_file
;
40 static int longest_author
;
41 static int max_orig_digits
;
42 static int max_digits
;
43 static int max_score_digits
;
46 static int blank_boundary
;
47 static int incremental
;
49 static int abbrev
= -1;
50 static int no_whole_file_rename
;
52 static enum date_mode blame_date_mode
= DATE_ISO8601
;
53 static size_t blame_date_width
;
55 static struct string_list mailmap
;
62 static int num_read_blob
;
63 static int num_get_patch
;
64 static int num_commits
;
66 #define PICKAXE_BLAME_MOVE 01
67 #define PICKAXE_BLAME_COPY 02
68 #define PICKAXE_BLAME_COPY_HARDER 04
69 #define PICKAXE_BLAME_COPY_HARDEST 010
72 * blame for a blame_entry with score lower than these thresholds
73 * is not passed to the parent using move/copy logic.
75 static unsigned blame_move_score
;
76 static unsigned blame_copy_score
;
77 #define BLAME_DEFAULT_MOVE_SCORE 20
78 #define BLAME_DEFAULT_COPY_SCORE 40
80 /* Remember to update object flag allocation in object.h */
81 #define METAINFO_SHOWN (1u<<12)
82 #define MORE_THAN_ONE_PATH (1u<<13)
85 * One blob in a commit that is being suspected
89 /* Record preceding blame record for this blob */
90 struct origin
*previous
;
91 /* origins are put in a list linked via `next' hanging off the
92 * corresponding commit's util field in order to make finding
93 * them fast. The presence in this chain does not count
94 * towards the origin's reference count. It is tempting to
95 * let it count as long as the commit is pending examination,
96 * but even under circumstances where the commit will be
97 * present multiple times in the priority queue of unexamined
98 * commits, processing the first instance will not leave any
99 * work requiring the origin data for the second instance. An
100 * interspersed commit changing that would have to be
101 * preexisting with a different ancestry and with the same
102 * commit date in order to wedge itself between two instances
103 * of the same commit in the priority queue _and_ produce
104 * blame entries relevant for it. While we don't want to let
105 * us get tripped up by this case, it certainly does not seem
106 * worth optimizing for.
109 struct commit
*commit
;
110 /* `suspects' contains blame entries that may be attributed to
111 * this origin's commit or to parent commits. When a commit
112 * is being processed, all suspects will be moved, either by
113 * assigning them to an origin in a different commit, or by
114 * shipping them to the scoreboard's ent list because they
115 * cannot be attributed to a different commit.
117 struct blame_entry
*suspects
;
119 unsigned char blob_sha1
[20];
121 /* guilty gets set when shipping any suspects to the final
122 * blame list instead of other commits
125 char path
[FLEX_ARRAY
];
128 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
, long ctxlen
,
129 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
)
132 xdemitconf_t xecfg
= {0};
133 xdemitcb_t ecb
= {NULL
};
135 xpp
.flags
= xdl_opts
;
136 xecfg
.ctxlen
= ctxlen
;
137 xecfg
.hunk_func
= hunk_func
;
139 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
143 * Prepare diff_filespec and convert it using diff textconv API
144 * if the textconv driver exists.
145 * Return 1 if the conversion succeeds, 0 otherwise.
147 int textconv_object(const char *path
,
149 const unsigned char *sha1
,
152 unsigned long *buf_size
)
154 struct diff_filespec
*df
;
155 struct userdiff_driver
*textconv
;
157 df
= alloc_filespec(path
);
158 fill_filespec(df
, sha1
, sha1_valid
, mode
);
159 textconv
= get_textconv(df
);
165 *buf_size
= fill_textconv(textconv
, df
, buf
);
171 * Given an origin, prepare mmfile_t structure to be used by the
174 static void fill_origin_blob(struct diff_options
*opt
,
175 struct origin
*o
, mmfile_t
*file
)
178 enum object_type type
;
179 unsigned long file_size
;
182 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
183 textconv_object(o
->path
, o
->mode
, o
->blob_sha1
, 1, &file
->ptr
, &file_size
))
186 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
, &file_size
);
187 file
->size
= file_size
;
190 die("Cannot read blob %s for path %s",
191 sha1_to_hex(o
->blob_sha1
),
200 * Origin is refcounted and usually we keep the blob contents to be
203 static inline struct origin
*origin_incref(struct origin
*o
)
210 static void origin_decref(struct origin
*o
)
212 if (o
&& --o
->refcnt
<= 0) {
213 struct origin
*p
, *l
= NULL
;
215 origin_decref(o
->previous
);
217 /* Should be present exactly once in commit chain */
218 for (p
= o
->commit
->util
; p
; l
= p
, p
= p
->next
) {
223 o
->commit
->util
= p
->next
;
228 die("internal error in blame::origin_decref");
232 static void drop_origin_blob(struct origin
*o
)
241 * Each group of lines is described by a blame_entry; it can be split
242 * as we pass blame to the parents. They are arranged in linked lists
243 * kept as `suspects' of some unprocessed origin, or entered (when the
244 * blame origin has been finalized) into the scoreboard structure.
245 * While the scoreboard structure is only sorted at the end of
246 * processing (according to final image line number), the lists
247 * attached to an origin are sorted by the target line number.
250 struct blame_entry
*next
;
252 /* the first line of this group in the final image;
253 * internally all line numbers are 0 based.
257 /* how many lines this group has */
260 /* the commit that introduced this group into the final image */
261 struct origin
*suspect
;
263 /* the line number of the first line of this group in the
264 * suspect's file; internally all line numbers are 0 based.
268 /* how significant this entry is -- cached to avoid
269 * scanning the lines over and over.
275 * Any merge of blames happens on lists of blames that arrived via
276 * different parents in a single suspect. In this case, we want to
277 * sort according to the suspect line numbers as opposed to the final
278 * image line numbers. The function body is somewhat longish because
279 * it avoids unnecessary writes.
282 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
283 struct blame_entry
*list2
)
285 struct blame_entry
*p1
= list1
, *p2
= list2
,
293 if (p1
->s_lno
<= p2
->s_lno
) {
296 if ((p1
= *tail
) == NULL
) {
300 } while (p1
->s_lno
<= p2
->s_lno
);
306 if ((p2
= *tail
) == NULL
) {
310 } while (p1
->s_lno
> p2
->s_lno
);
314 if ((p1
= *tail
) == NULL
) {
318 } while (p1
->s_lno
<= p2
->s_lno
);
322 static void *get_next_blame(const void *p
)
324 return ((struct blame_entry
*)p
)->next
;
327 static void set_next_blame(void *p1
, void *p2
)
329 ((struct blame_entry
*)p1
)->next
= p2
;
333 * Final image line numbers are all different, so we don't need a
334 * three-way comparison here.
337 static int compare_blame_final(const void *p1
, const void *p2
)
339 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
343 static int compare_blame_suspect(const void *p1
, const void *p2
)
345 const struct blame_entry
*s1
= p1
, *s2
= p2
;
347 * to allow for collating suspects, we sort according to the
348 * respective pointer value as the primary sorting criterion.
349 * The actual relation is pretty unimportant as long as it
350 * establishes a total order. Comparing as integers gives us
353 if (s1
->suspect
!= s2
->suspect
)
354 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
355 if (s1
->s_lno
== s2
->s_lno
)
357 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
360 static struct blame_entry
*blame_sort(struct blame_entry
*head
,
361 int (*compare_fn
)(const void *, const void *))
363 return llist_mergesort (head
, get_next_blame
, set_next_blame
, compare_fn
);
366 static int compare_commits_by_reverse_commit_date(const void *a
,
370 return -compare_commits_by_commit_date(a
, b
, c
);
374 * The current state of the blame assignment.
377 /* the final commit (i.e. where we started digging from) */
378 struct commit
*final
;
379 /* Priority queue for commits with unassigned blame records */
380 struct prio_queue commits
;
381 struct rev_info
*revs
;
385 * The contents in the final image.
386 * Used by many functions to obtain contents of the nth line,
387 * indexed with scoreboard.lineno[blame_entry.lno].
389 const char *final_buf
;
390 unsigned long final_buf_size
;
392 /* linked list of blames */
393 struct blame_entry
*ent
;
395 /* look-up a line in the final buffer */
400 static void sanity_check_refcnt(struct scoreboard
*);
403 * If two blame entries that are next to each other came from
404 * contiguous lines in the same origin (i.e. <commit, path> pair),
405 * merge them together.
407 static void coalesce(struct scoreboard
*sb
)
409 struct blame_entry
*ent
, *next
;
411 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
412 if (ent
->suspect
== next
->suspect
&&
413 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
414 ent
->num_lines
+= next
->num_lines
;
415 ent
->next
= next
->next
;
416 origin_decref(next
->suspect
);
419 next
= ent
; /* again */
423 if (DEBUG
) /* sanity */
424 sanity_check_refcnt(sb
);
428 * Merge the given sorted list of blames into a preexisting origin.
429 * If there were no previous blames to that commit, it is entered into
430 * the commit priority queue of the score board.
433 static void queue_blames(struct scoreboard
*sb
, struct origin
*porigin
,
434 struct blame_entry
*sorted
)
436 if (porigin
->suspects
)
437 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
440 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
442 porigin
->suspects
= sorted
;
446 porigin
->suspects
= sorted
;
447 prio_queue_put(&sb
->commits
, porigin
->commit
);
452 * Given a commit and a path in it, create a new origin structure.
453 * The callers that add blame to the scoreboard should use
454 * get_origin() to obtain shared, refcounted copy instead of calling
455 * this function directly.
457 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
460 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
463 o
->next
= commit
->util
;
465 strcpy(o
->path
, path
);
470 * Locate an existing origin or create a new one.
471 * This moves the origin to front position in the commit util list.
473 static struct origin
*get_origin(struct scoreboard
*sb
,
474 struct commit
*commit
,
477 struct origin
*o
, *l
;
479 for (o
= commit
->util
, l
= NULL
; o
; l
= o
, o
= o
->next
) {
480 if (!strcmp(o
->path
, path
)) {
484 o
->next
= commit
->util
;
487 return origin_incref(o
);
490 return make_origin(commit
, path
);
494 * Fill the blob_sha1 field of an origin if it hasn't, so that later
495 * call to fill_origin_blob() can use it to locate the data. blob_sha1
496 * for an origin is also used to pass the blame for the entire file to
497 * the parent to detect the case where a child's blob is identical to
498 * that of its parent's.
500 * This also fills origin->mode for corresponding tree path.
502 static int fill_blob_sha1_and_mode(struct origin
*origin
)
504 if (!is_null_sha1(origin
->blob_sha1
))
506 if (get_tree_entry(origin
->commit
->object
.sha1
,
508 origin
->blob_sha1
, &origin
->mode
))
510 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
514 hashclr(origin
->blob_sha1
);
515 origin
->mode
= S_IFINVALID
;
520 * We have an origin -- check if the same path exists in the
521 * parent and return an origin structure to represent it.
523 static struct origin
*find_origin(struct scoreboard
*sb
,
524 struct commit
*parent
,
525 struct origin
*origin
)
527 struct origin
*porigin
;
528 struct diff_options diff_opts
;
529 const char *paths
[2];
531 /* First check any existing origins */
532 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
533 if (!strcmp(porigin
->path
, origin
->path
)) {
535 * The same path between origin and its parent
536 * without renaming -- the most common case.
538 return origin_incref (porigin
);
541 /* See if the origin->path is different between parent
542 * and origin first. Most of the time they are the
543 * same and diff-tree is fairly efficient about this.
545 diff_setup(&diff_opts
);
546 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
547 diff_opts
.detect_rename
= 0;
548 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
549 paths
[0] = origin
->path
;
552 parse_pathspec(&diff_opts
.pathspec
,
553 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
554 PATHSPEC_LITERAL_PATH
, "", paths
);
555 diff_setup_done(&diff_opts
);
557 if (is_null_sha1(origin
->commit
->object
.sha1
))
558 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
560 diff_tree_sha1(parent
->tree
->object
.sha1
,
561 origin
->commit
->tree
->object
.sha1
,
563 diffcore_std(&diff_opts
);
565 if (!diff_queued_diff
.nr
) {
566 /* The path is the same as parent */
567 porigin
= get_origin(sb
, parent
, origin
->path
);
568 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
569 porigin
->mode
= origin
->mode
;
572 * Since origin->path is a pathspec, if the parent
573 * commit had it as a directory, we will see a whole
574 * bunch of deletion of files in the directory that we
578 struct diff_filepair
*p
= NULL
;
579 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
581 p
= diff_queued_diff
.queue
[i
];
582 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
583 if (!strcmp(name
, origin
->path
))
587 die("internal error in blame::find_origin");
590 die("internal error in blame::find_origin (%c)",
593 porigin
= get_origin(sb
, parent
, origin
->path
);
594 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
595 porigin
->mode
= p
->one
->mode
;
599 /* Did not exist in parent, or type changed */
603 diff_flush(&diff_opts
);
604 free_pathspec(&diff_opts
.pathspec
);
609 * We have an origin -- find the path that corresponds to it in its
610 * parent and return an origin structure to represent it.
612 static struct origin
*find_rename(struct scoreboard
*sb
,
613 struct commit
*parent
,
614 struct origin
*origin
)
616 struct origin
*porigin
= NULL
;
617 struct diff_options diff_opts
;
620 diff_setup(&diff_opts
);
621 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
622 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
623 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
624 diff_opts
.single_follow
= origin
->path
;
625 diff_setup_done(&diff_opts
);
627 if (is_null_sha1(origin
->commit
->object
.sha1
))
628 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
630 diff_tree_sha1(parent
->tree
->object
.sha1
,
631 origin
->commit
->tree
->object
.sha1
,
633 diffcore_std(&diff_opts
);
635 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
636 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
637 if ((p
->status
== 'R' || p
->status
== 'C') &&
638 !strcmp(p
->two
->path
, origin
->path
)) {
639 porigin
= get_origin(sb
, parent
, p
->one
->path
);
640 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
641 porigin
->mode
= p
->one
->mode
;
645 diff_flush(&diff_opts
);
646 free_pathspec(&diff_opts
.pathspec
);
651 * Append a new blame entry to a given output queue.
653 static void add_blame_entry(struct blame_entry
***queue
, struct blame_entry
*e
)
655 origin_incref(e
->suspect
);
663 * src typically is on-stack; we want to copy the information in it to
664 * a malloced blame_entry that gets added to the given queue. The
665 * origin of dst loses a refcnt.
667 static void dup_entry(struct blame_entry
***queue
,
668 struct blame_entry
*dst
, struct blame_entry
*src
)
670 origin_incref(src
->suspect
);
671 origin_decref(dst
->suspect
);
672 memcpy(dst
, src
, sizeof(*src
));
678 static const char *nth_line(struct scoreboard
*sb
, long lno
)
680 return sb
->final_buf
+ sb
->lineno
[lno
];
683 static const char *nth_line_cb(void *data
, long lno
)
685 return nth_line((struct scoreboard
*)data
, lno
);
689 * It is known that lines between tlno to same came from parent, and e
690 * has an overlap with that range. it also is known that parent's
691 * line plno corresponds to e's line tlno.
697 * <------------------>
699 * Split e into potentially three parts; before this chunk, the chunk
700 * to be blamed for the parent, and after that portion.
702 static void split_overlap(struct blame_entry
*split
,
703 struct blame_entry
*e
,
704 int tlno
, int plno
, int same
,
705 struct origin
*parent
)
708 memset(split
, 0, sizeof(struct blame_entry
[3]));
710 if (e
->s_lno
< tlno
) {
711 /* there is a pre-chunk part not blamed on parent */
712 split
[0].suspect
= origin_incref(e
->suspect
);
713 split
[0].lno
= e
->lno
;
714 split
[0].s_lno
= e
->s_lno
;
715 split
[0].num_lines
= tlno
- e
->s_lno
;
716 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
717 split
[1].s_lno
= plno
;
720 split
[1].lno
= e
->lno
;
721 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
724 if (same
< e
->s_lno
+ e
->num_lines
) {
725 /* there is a post-chunk part not blamed on parent */
726 split
[2].suspect
= origin_incref(e
->suspect
);
727 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
728 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
729 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
730 chunk_end_lno
= split
[2].lno
;
733 chunk_end_lno
= e
->lno
+ e
->num_lines
;
734 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
737 * if it turns out there is nothing to blame the parent for,
738 * forget about the splitting. !split[1].suspect signals this.
740 if (split
[1].num_lines
< 1)
742 split
[1].suspect
= origin_incref(parent
);
746 * split_overlap() divided an existing blame e into up to three parts
747 * in split. Any assigned blame is moved to queue to
750 static void split_blame(struct blame_entry
***blamed
,
751 struct blame_entry
***unblamed
,
752 struct blame_entry
*split
,
753 struct blame_entry
*e
)
755 struct blame_entry
*new_entry
;
757 if (split
[0].suspect
&& split
[2].suspect
) {
758 /* The first part (reuse storage for the existing entry e) */
759 dup_entry(unblamed
, e
, &split
[0]);
761 /* The last part -- me */
762 new_entry
= xmalloc(sizeof(*new_entry
));
763 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
764 add_blame_entry(unblamed
, new_entry
);
766 /* ... and the middle part -- parent */
767 new_entry
= xmalloc(sizeof(*new_entry
));
768 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
769 add_blame_entry(blamed
, new_entry
);
771 else if (!split
[0].suspect
&& !split
[2].suspect
)
773 * The parent covers the entire area; reuse storage for
774 * e and replace it with the parent.
776 dup_entry(blamed
, e
, &split
[1]);
777 else if (split
[0].suspect
) {
778 /* me and then parent */
779 dup_entry(unblamed
, e
, &split
[0]);
781 new_entry
= xmalloc(sizeof(*new_entry
));
782 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
783 add_blame_entry(blamed
, new_entry
);
786 /* parent and then me */
787 dup_entry(blamed
, e
, &split
[1]);
789 new_entry
= xmalloc(sizeof(*new_entry
));
790 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
791 add_blame_entry(unblamed
, new_entry
);
796 * After splitting the blame, the origins used by the
797 * on-stack blame_entry should lose one refcnt each.
799 static void decref_split(struct blame_entry
*split
)
803 for (i
= 0; i
< 3; i
++)
804 origin_decref(split
[i
].suspect
);
808 * reverse_blame reverses the list given in head, appending tail.
809 * That allows us to build lists in reverse order, then reverse them
810 * afterwards. This can be faster than building the list in proper
811 * order right away. The reason is that building in proper order
812 * requires writing a link in the _previous_ element, while building
813 * in reverse order just requires placing the list head into the
817 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
818 struct blame_entry
*tail
)
821 struct blame_entry
*next
= head
->next
;
830 * Process one hunk from the patch between the current suspect for
831 * blame_entry e and its parent. This first blames any unfinished
832 * entries before the chunk (which is where target and parent start
833 * differing) on the parent, and then splits blame entries at the
834 * start and at the end of the difference region. Since use of -M and
835 * -C options may lead to overlapping/duplicate source line number
836 * ranges, all we can rely on from sorting/merging is the order of the
837 * first suspect line number.
839 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
840 int tlno
, int offset
, int same
,
841 struct origin
*parent
)
843 struct blame_entry
*e
= **srcq
;
844 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
846 while (e
&& e
->s_lno
< tlno
) {
847 struct blame_entry
*next
= e
->next
;
849 * current record starts before differing portion. If
850 * it reaches into it, we need to split it up and
851 * examine the second part separately.
853 if (e
->s_lno
+ e
->num_lines
> tlno
) {
854 /* Move second half to a new record */
855 int len
= tlno
- e
->s_lno
;
856 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
857 n
->suspect
= e
->suspect
;
858 n
->lno
= e
->lno
+ len
;
859 n
->s_lno
= e
->s_lno
+ len
;
860 n
->num_lines
= e
->num_lines
- len
;
863 /* Push new record to diffp */
867 origin_decref(e
->suspect
);
868 /* Pass blame for everything before the differing
869 * chunk to the parent */
870 e
->suspect
= origin_incref(parent
);
877 * As we don't know how much of a common stretch after this
878 * diff will occur, the currently blamed parts are all that we
879 * can assign to the parent for now.
883 **dstq
= reverse_blame(samep
, **dstq
);
884 *dstq
= &samep
->next
;
887 * Prepend the split off portions: everything after e starts
888 * after the blameable portion.
890 e
= reverse_blame(diffp
, e
);
893 * Now retain records on the target while parts are different
898 while (e
&& e
->s_lno
< same
) {
899 struct blame_entry
*next
= e
->next
;
902 * If current record extends into sameness, need to split.
904 if (e
->s_lno
+ e
->num_lines
> same
) {
906 * Move second half to a new record to be
907 * processed by later chunks
909 int len
= same
- e
->s_lno
;
910 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
911 n
->suspect
= origin_incref(e
->suspect
);
912 n
->lno
= e
->lno
+ len
;
913 n
->s_lno
= e
->s_lno
+ len
;
914 n
->num_lines
= e
->num_lines
- len
;
917 /* Push new record to samep */
925 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
926 /* Move across elements that are in the unblamable portion */
928 *srcq
= &diffp
->next
;
931 struct blame_chunk_cb_data
{
932 struct origin
*parent
;
934 struct blame_entry
**dstq
;
935 struct blame_entry
**srcq
;
938 /* diff chunks are from parent to target */
939 static int blame_chunk_cb(long start_a
, long count_a
,
940 long start_b
, long count_b
, void *data
)
942 struct blame_chunk_cb_data
*d
= data
;
943 if (start_a
- start_b
!= d
->offset
)
944 die("internal error in blame::blame_chunk_cb");
945 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
946 start_b
+ count_b
, d
->parent
);
947 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
952 * We are looking at the origin 'target' and aiming to pass blame
953 * for the lines it is suspected to its parent. Run diff to find
954 * which lines came from parent and pass blame for them.
956 static void pass_blame_to_parent(struct scoreboard
*sb
,
957 struct origin
*target
,
958 struct origin
*parent
)
960 mmfile_t file_p
, file_o
;
961 struct blame_chunk_cb_data d
;
962 struct blame_entry
*newdest
= NULL
;
964 if (!target
->suspects
)
965 return; /* nothing remains for this target */
969 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
971 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
972 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
);
975 diff_hunks(&file_p
, &file_o
, 0, blame_chunk_cb
, &d
);
976 /* The rest are the same as the parent */
977 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
979 queue_blames(sb
, parent
, newdest
);
985 * The lines in blame_entry after splitting blames many times can become
986 * very small and trivial, and at some point it becomes pointless to
987 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
988 * ordinary C program, and it is not worth to say it was copied from
989 * totally unrelated file in the parent.
991 * Compute how trivial the lines in the blame_entry are.
993 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
1002 cp
= nth_line(sb
, e
->lno
);
1003 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
1005 unsigned ch
= *((unsigned char *)cp
);
1015 * best_so_far[] and this[] are both a split of an existing blame_entry
1016 * that passes blame to the parent. Maintain best_so_far the best split
1017 * so far, by comparing this and best_so_far and copying this into
1018 * bst_so_far as needed.
1020 static void copy_split_if_better(struct scoreboard
*sb
,
1021 struct blame_entry
*best_so_far
,
1022 struct blame_entry
*this)
1026 if (!this[1].suspect
)
1028 if (best_so_far
[1].suspect
) {
1029 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
1033 for (i
= 0; i
< 3; i
++)
1034 origin_incref(this[i
].suspect
);
1035 decref_split(best_so_far
);
1036 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
1040 * We are looking at a part of the final image represented by
1041 * ent (tlno and same are offset by ent->s_lno).
1042 * tlno is where we are looking at in the final image.
1043 * up to (but not including) same match preimage.
1044 * plno is where we are looking at in the preimage.
1046 * <-------------- final image ---------------------->
1049 * <---------preimage----->
1052 * All line numbers are 0-based.
1054 static void handle_split(struct scoreboard
*sb
,
1055 struct blame_entry
*ent
,
1056 int tlno
, int plno
, int same
,
1057 struct origin
*parent
,
1058 struct blame_entry
*split
)
1060 if (ent
->num_lines
<= tlno
)
1063 struct blame_entry
this[3];
1066 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
1067 copy_split_if_better(sb
, split
, this);
1072 struct handle_split_cb_data
{
1073 struct scoreboard
*sb
;
1074 struct blame_entry
*ent
;
1075 struct origin
*parent
;
1076 struct blame_entry
*split
;
1081 static int handle_split_cb(long start_a
, long count_a
,
1082 long start_b
, long count_b
, void *data
)
1084 struct handle_split_cb_data
*d
= data
;
1085 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1087 d
->plno
= start_a
+ count_a
;
1088 d
->tlno
= start_b
+ count_b
;
1093 * Find the lines from parent that are the same as ent so that
1094 * we can pass blames to it. file_p has the blob contents for
1097 static void find_copy_in_blob(struct scoreboard
*sb
,
1098 struct blame_entry
*ent
,
1099 struct origin
*parent
,
1100 struct blame_entry
*split
,
1105 struct handle_split_cb_data d
;
1107 memset(&d
, 0, sizeof(d
));
1108 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1110 * Prepare mmfile that contains only the lines in ent.
1112 cp
= nth_line(sb
, ent
->lno
);
1113 file_o
.ptr
= (char *) cp
;
1114 file_o
.size
= nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1117 * file_o is a part of final image we are annotating.
1118 * file_p partially may match that image.
1120 memset(split
, 0, sizeof(struct blame_entry
[3]));
1121 diff_hunks(file_p
, &file_o
, 1, handle_split_cb
, &d
);
1122 /* remainder, if any, all match the preimage */
1123 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1126 /* Move all blame entries from list *source that have a score smaller
1127 * than score_min to the front of list *small.
1128 * Returns a pointer to the link pointing to the old head of the small list.
1131 static struct blame_entry
**filter_small(struct scoreboard
*sb
,
1132 struct blame_entry
**small
,
1133 struct blame_entry
**source
,
1136 struct blame_entry
*p
= *source
;
1137 struct blame_entry
*oldsmall
= *small
;
1139 if (ent_score(sb
, p
) <= score_min
) {
1155 * See if lines currently target is suspected for can be attributed to
1158 static void find_move_in_parent(struct scoreboard
*sb
,
1159 struct blame_entry
***blamed
,
1160 struct blame_entry
**toosmall
,
1161 struct origin
*target
,
1162 struct origin
*parent
)
1164 struct blame_entry
*e
, split
[3];
1165 struct blame_entry
*unblamed
= target
->suspects
;
1166 struct blame_entry
*leftover
= NULL
;
1170 return; /* nothing remains for this target */
1172 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
1176 /* At each iteration, unblamed has a NULL-terminated list of
1177 * entries that have not yet been tested for blame. leftover
1178 * contains the reversed list of entries that have been tested
1179 * without being assignable to the parent.
1182 struct blame_entry
**unblamedtail
= &unblamed
;
1183 struct blame_entry
*next
;
1184 for (e
= unblamed
; e
; e
= next
) {
1186 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1187 if (split
[1].suspect
&&
1188 blame_move_score
< ent_score(sb
, &split
[1])) {
1189 split_blame(blamed
, &unblamedtail
, split
, e
);
1194 decref_split(split
);
1196 *unblamedtail
= NULL
;
1197 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_move_score
);
1199 target
->suspects
= reverse_blame(leftover
, NULL
);
1203 struct blame_entry
*ent
;
1204 struct blame_entry split
[3];
1208 * Count the number of entries the target is suspected for,
1209 * and prepare a list of entry and the best split.
1211 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1214 struct blame_entry
*e
;
1216 struct blame_list
*blame_list
= NULL
;
1218 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1221 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1222 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1223 blame_list
[i
++].ent
= e
;
1225 *num_ents_p
= num_ents
;
1230 * For lines target is suspected for, see if we can find code movement
1231 * across file boundary from the parent commit. porigin is the path
1232 * in the parent we already tried.
1234 static void find_copy_in_parent(struct scoreboard
*sb
,
1235 struct blame_entry
***blamed
,
1236 struct blame_entry
**toosmall
,
1237 struct origin
*target
,
1238 struct commit
*parent
,
1239 struct origin
*porigin
,
1242 struct diff_options diff_opts
;
1244 struct blame_list
*blame_list
;
1246 struct blame_entry
*unblamed
= target
->suspects
;
1247 struct blame_entry
*leftover
= NULL
;
1250 return; /* nothing remains for this target */
1252 diff_setup(&diff_opts
);
1253 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1254 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1256 diff_setup_done(&diff_opts
);
1258 /* Try "find copies harder" on new path if requested;
1259 * we do not want to use diffcore_rename() actually to
1260 * match things up; find_copies_harder is set only to
1261 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1262 * and this code needs to be after diff_setup_done(), which
1263 * usually makes find-copies-harder imply copy detection.
1265 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1266 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1267 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1268 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1270 if (is_null_sha1(target
->commit
->object
.sha1
))
1271 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1273 diff_tree_sha1(parent
->tree
->object
.sha1
,
1274 target
->commit
->tree
->object
.sha1
,
1277 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1278 diffcore_std(&diff_opts
);
1281 struct blame_entry
**unblamedtail
= &unblamed
;
1282 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1284 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1285 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1286 struct origin
*norigin
;
1288 struct blame_entry
this[3];
1290 if (!DIFF_FILE_VALID(p
->one
))
1291 continue; /* does not exist in parent */
1292 if (S_ISGITLINK(p
->one
->mode
))
1293 continue; /* ignore git links */
1294 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1295 /* find_move already dealt with this path */
1298 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1299 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1300 norigin
->mode
= p
->one
->mode
;
1301 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
);
1305 for (j
= 0; j
< num_ents
; j
++) {
1306 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1307 norigin
, this, &file_p
);
1308 copy_split_if_better(sb
, blame_list
[j
].split
,
1312 origin_decref(norigin
);
1315 for (j
= 0; j
< num_ents
; j
++) {
1316 struct blame_entry
*split
= blame_list
[j
].split
;
1317 if (split
[1].suspect
&&
1318 blame_copy_score
< ent_score(sb
, &split
[1])) {
1319 split_blame(blamed
, &unblamedtail
, split
,
1322 blame_list
[j
].ent
->next
= leftover
;
1323 leftover
= blame_list
[j
].ent
;
1325 decref_split(split
);
1328 *unblamedtail
= NULL
;
1329 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_copy_score
);
1331 target
->suspects
= reverse_blame(leftover
, NULL
);
1332 diff_flush(&diff_opts
);
1333 free_pathspec(&diff_opts
.pathspec
);
1337 * The blobs of origin and porigin exactly match, so everything
1338 * origin is suspected for can be blamed on the parent.
1340 static void pass_whole_blame(struct scoreboard
*sb
,
1341 struct origin
*origin
, struct origin
*porigin
)
1343 struct blame_entry
*e
, *suspects
;
1345 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1346 /* Steal its file */
1347 porigin
->file
= origin
->file
;
1348 origin
->file
.ptr
= NULL
;
1350 suspects
= origin
->suspects
;
1351 origin
->suspects
= NULL
;
1352 for (e
= suspects
; e
; e
= e
->next
) {
1353 origin_incref(porigin
);
1354 origin_decref(e
->suspect
);
1355 e
->suspect
= porigin
;
1357 queue_blames(sb
, porigin
, suspects
);
1361 * We pass blame from the current commit to its parents. We keep saying
1362 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1363 * exonerate ourselves.
1365 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1368 return commit
->parents
;
1369 return lookup_decoration(&revs
->children
, &commit
->object
);
1372 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1375 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1376 for (cnt
= 0; l
; l
= l
->next
)
1381 /* Distribute collected unsorted blames to the respected sorted lists
1382 * in the various origins.
1384 static void distribute_blame(struct scoreboard
*sb
, struct blame_entry
*blamed
)
1386 blamed
= blame_sort(blamed
, compare_blame_suspect
);
1389 struct origin
*porigin
= blamed
->suspect
;
1390 struct blame_entry
*suspects
= NULL
;
1392 struct blame_entry
*next
= blamed
->next
;
1393 blamed
->next
= suspects
;
1396 } while (blamed
&& blamed
->suspect
== porigin
);
1397 suspects
= reverse_blame(suspects
, NULL
);
1398 queue_blames(sb
, porigin
, suspects
);
1404 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1406 struct rev_info
*revs
= sb
->revs
;
1407 int i
, pass
, num_sg
;
1408 struct commit
*commit
= origin
->commit
;
1409 struct commit_list
*sg
;
1410 struct origin
*sg_buf
[MAXSG
];
1411 struct origin
*porigin
, **sg_origin
= sg_buf
;
1412 struct blame_entry
*toosmall
= NULL
;
1413 struct blame_entry
*blames
, **blametail
= &blames
;
1415 num_sg
= num_scapegoats(revs
, commit
);
1418 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1419 memset(sg_buf
, 0, sizeof(sg_buf
));
1421 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1424 * The first pass looks for unrenamed path to optimize for
1425 * common cases, then we look for renames in the second pass.
1427 for (pass
= 0; pass
< 2 - no_whole_file_rename
; pass
++) {
1428 struct origin
*(*find
)(struct scoreboard
*,
1429 struct commit
*, struct origin
*);
1430 find
= pass
? find_rename
: find_origin
;
1432 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1434 sg
= sg
->next
, i
++) {
1435 struct commit
*p
= sg
->item
;
1440 if (parse_commit(p
))
1442 porigin
= find(sb
, p
, origin
);
1445 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1446 pass_whole_blame(sb
, origin
, porigin
);
1447 origin_decref(porigin
);
1450 for (j
= same
= 0; j
< i
; j
++)
1452 !hashcmp(sg_origin
[j
]->blob_sha1
,
1453 porigin
->blob_sha1
)) {
1458 sg_origin
[i
] = porigin
;
1460 origin_decref(porigin
);
1465 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1467 sg
= sg
->next
, i
++) {
1468 struct origin
*porigin
= sg_origin
[i
];
1471 if (!origin
->previous
) {
1472 origin_incref(porigin
);
1473 origin
->previous
= porigin
;
1475 pass_blame_to_parent(sb
, origin
, porigin
);
1476 if (!origin
->suspects
)
1481 * Optionally find moves in parents' files.
1483 if (opt
& PICKAXE_BLAME_MOVE
) {
1484 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_move_score
);
1485 if (origin
->suspects
) {
1486 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1488 sg
= sg
->next
, i
++) {
1489 struct origin
*porigin
= sg_origin
[i
];
1492 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1493 if (!origin
->suspects
)
1500 * Optionally find copies from parents' files.
1502 if (opt
& PICKAXE_BLAME_COPY
) {
1503 if (blame_copy_score
> blame_move_score
)
1504 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1505 else if (blame_copy_score
< blame_move_score
) {
1506 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1508 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1510 if (!origin
->suspects
)
1513 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1515 sg
= sg
->next
, i
++) {
1516 struct origin
*porigin
= sg_origin
[i
];
1517 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1518 origin
, sg
->item
, porigin
, opt
);
1519 if (!origin
->suspects
)
1526 distribute_blame(sb
, blames
);
1528 * prepend toosmall to origin->suspects
1530 * There is no point in sorting: this ends up on a big
1531 * unsorted list in the caller anyway.
1534 struct blame_entry
**tail
= &toosmall
;
1536 tail
= &(*tail
)->next
;
1537 *tail
= origin
->suspects
;
1538 origin
->suspects
= toosmall
;
1540 for (i
= 0; i
< num_sg
; i
++) {
1542 drop_origin_blob(sg_origin
[i
]);
1543 origin_decref(sg_origin
[i
]);
1546 drop_origin_blob(origin
);
1547 if (sg_buf
!= sg_origin
)
1552 * Information on commits, used for output.
1554 struct commit_info
{
1555 struct strbuf author
;
1556 struct strbuf author_mail
;
1557 unsigned long author_time
;
1558 struct strbuf author_tz
;
1560 /* filled only when asked for details */
1561 struct strbuf committer
;
1562 struct strbuf committer_mail
;
1563 unsigned long committer_time
;
1564 struct strbuf committer_tz
;
1566 struct strbuf summary
;
1570 * Parse author/committer line in the commit object buffer
1572 static void get_ac_line(const char *inbuf
, const char *what
,
1573 struct strbuf
*name
, struct strbuf
*mail
,
1574 unsigned long *time
, struct strbuf
*tz
)
1576 struct ident_split ident
;
1577 size_t len
, maillen
, namelen
;
1579 const char *namebuf
, *mailbuf
;
1581 tmp
= strstr(inbuf
, what
);
1584 tmp
+= strlen(what
);
1585 endp
= strchr(tmp
, '\n');
1591 if (split_ident_line(&ident
, tmp
, len
)) {
1595 strbuf_addstr(name
, tmp
);
1596 strbuf_addstr(mail
, tmp
);
1597 strbuf_addstr(tz
, tmp
);
1602 namelen
= ident
.name_end
- ident
.name_begin
;
1603 namebuf
= ident
.name_begin
;
1605 maillen
= ident
.mail_end
- ident
.mail_begin
;
1606 mailbuf
= ident
.mail_begin
;
1608 if (ident
.date_begin
&& ident
.date_end
)
1609 *time
= strtoul(ident
.date_begin
, NULL
, 10);
1613 if (ident
.tz_begin
&& ident
.tz_end
)
1614 strbuf_add(tz
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
1616 strbuf_addstr(tz
, "(unknown)");
1619 * Now, convert both name and e-mail using mailmap
1621 map_user(&mailmap
, &mailbuf
, &maillen
,
1622 &namebuf
, &namelen
);
1624 strbuf_addf(mail
, "<%.*s>", (int)maillen
, mailbuf
);
1625 strbuf_add(name
, namebuf
, namelen
);
1628 static void commit_info_init(struct commit_info
*ci
)
1631 strbuf_init(&ci
->author
, 0);
1632 strbuf_init(&ci
->author_mail
, 0);
1633 strbuf_init(&ci
->author_tz
, 0);
1634 strbuf_init(&ci
->committer
, 0);
1635 strbuf_init(&ci
->committer_mail
, 0);
1636 strbuf_init(&ci
->committer_tz
, 0);
1637 strbuf_init(&ci
->summary
, 0);
1640 static void commit_info_destroy(struct commit_info
*ci
)
1643 strbuf_release(&ci
->author
);
1644 strbuf_release(&ci
->author_mail
);
1645 strbuf_release(&ci
->author_tz
);
1646 strbuf_release(&ci
->committer
);
1647 strbuf_release(&ci
->committer_mail
);
1648 strbuf_release(&ci
->committer_tz
);
1649 strbuf_release(&ci
->summary
);
1652 static void get_commit_info(struct commit
*commit
,
1653 struct commit_info
*ret
,
1657 const char *subject
, *encoding
;
1658 const char *message
;
1660 commit_info_init(ret
);
1662 encoding
= get_log_output_encoding();
1663 message
= logmsg_reencode(commit
, NULL
, encoding
);
1664 get_ac_line(message
, "\nauthor ",
1665 &ret
->author
, &ret
->author_mail
,
1666 &ret
->author_time
, &ret
->author_tz
);
1669 unuse_commit_buffer(commit
, message
);
1673 get_ac_line(message
, "\ncommitter ",
1674 &ret
->committer
, &ret
->committer_mail
,
1675 &ret
->committer_time
, &ret
->committer_tz
);
1677 len
= find_commit_subject(message
, &subject
);
1679 strbuf_add(&ret
->summary
, subject
, len
);
1681 strbuf_addf(&ret
->summary
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1683 unuse_commit_buffer(commit
, message
);
1687 * To allow LF and other nonportable characters in pathnames,
1688 * they are c-style quoted as needed.
1690 static void write_filename_info(const char *path
)
1692 printf("filename ");
1693 write_name_quoted(path
, stdout
, '\n');
1697 * Porcelain/Incremental format wants to show a lot of details per
1698 * commit. Instead of repeating this every line, emit it only once,
1699 * the first time each commit appears in the output (unless the
1700 * user has specifically asked for us to repeat).
1702 static int emit_one_suspect_detail(struct origin
*suspect
, int repeat
)
1704 struct commit_info ci
;
1706 if (!repeat
&& (suspect
->commit
->object
.flags
& METAINFO_SHOWN
))
1709 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1710 get_commit_info(suspect
->commit
, &ci
, 1);
1711 printf("author %s\n", ci
.author
.buf
);
1712 printf("author-mail %s\n", ci
.author_mail
.buf
);
1713 printf("author-time %lu\n", ci
.author_time
);
1714 printf("author-tz %s\n", ci
.author_tz
.buf
);
1715 printf("committer %s\n", ci
.committer
.buf
);
1716 printf("committer-mail %s\n", ci
.committer_mail
.buf
);
1717 printf("committer-time %lu\n", ci
.committer_time
);
1718 printf("committer-tz %s\n", ci
.committer_tz
.buf
);
1719 printf("summary %s\n", ci
.summary
.buf
);
1720 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1721 printf("boundary\n");
1722 if (suspect
->previous
) {
1723 struct origin
*prev
= suspect
->previous
;
1724 printf("previous %s ", sha1_to_hex(prev
->commit
->object
.sha1
));
1725 write_name_quoted(prev
->path
, stdout
, '\n');
1728 commit_info_destroy(&ci
);
1734 * The blame_entry is found to be guilty for the range.
1735 * Show it in incremental output.
1737 static void found_guilty_entry(struct blame_entry
*ent
)
1740 struct origin
*suspect
= ent
->suspect
;
1742 printf("%s %d %d %d\n",
1743 sha1_to_hex(suspect
->commit
->object
.sha1
),
1744 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1745 emit_one_suspect_detail(suspect
, 0);
1746 write_filename_info(suspect
->path
);
1747 maybe_flush_or_die(stdout
, "stdout");
1752 * The main loop -- while we have blobs with lines whose true origin
1753 * is still unknown, pick one blob, and allow its lines to pass blames
1754 * to its parents. */
1755 static void assign_blame(struct scoreboard
*sb
, int opt
)
1757 struct rev_info
*revs
= sb
->revs
;
1758 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1761 struct blame_entry
*ent
;
1762 struct origin
*suspect
= commit
->util
;
1764 /* find one suspect to break down */
1765 while (suspect
&& !suspect
->suspects
)
1766 suspect
= suspect
->next
;
1769 commit
= prio_queue_get(&sb
->commits
);
1773 assert(commit
== suspect
->commit
);
1776 * We will use this suspect later in the loop,
1777 * so hold onto it in the meantime.
1779 origin_incref(suspect
);
1780 parse_commit(commit
);
1782 (!(commit
->object
.flags
& UNINTERESTING
) &&
1783 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1784 pass_blame(sb
, suspect
, opt
);
1786 commit
->object
.flags
|= UNINTERESTING
;
1787 if (commit
->object
.parsed
)
1788 mark_parents_uninteresting(commit
);
1790 /* treat root commit as boundary */
1791 if (!commit
->parents
&& !show_root
)
1792 commit
->object
.flags
|= UNINTERESTING
;
1794 /* Take responsibility for the remaining entries */
1795 ent
= suspect
->suspects
;
1797 suspect
->guilty
= 1;
1799 struct blame_entry
*next
= ent
->next
;
1800 found_guilty_entry(ent
);
1805 ent
->next
= sb
->ent
;
1806 sb
->ent
= suspect
->suspects
;
1807 suspect
->suspects
= NULL
;
1811 origin_decref(suspect
);
1813 if (DEBUG
) /* sanity */
1814 sanity_check_refcnt(sb
);
1818 static const char *format_time(unsigned long time
, const char *tz_str
,
1821 static struct strbuf time_buf
= STRBUF_INIT
;
1823 strbuf_reset(&time_buf
);
1824 if (show_raw_time
) {
1825 strbuf_addf(&time_buf
, "%lu %s", time
, tz_str
);
1828 const char *time_str
;
1832 time_str
= show_date(time
, tz
, blame_date_mode
);
1833 strbuf_addstr(&time_buf
, time_str
);
1835 * Add space paddings to time_buf to display a fixed width
1836 * string, and use time_width for display width calibration.
1838 for (time_width
= utf8_strwidth(time_str
);
1839 time_width
< blame_date_width
;
1841 strbuf_addch(&time_buf
, ' ');
1843 return time_buf
.buf
;
1846 #define OUTPUT_ANNOTATE_COMPAT 001
1847 #define OUTPUT_LONG_OBJECT_NAME 002
1848 #define OUTPUT_RAW_TIMESTAMP 004
1849 #define OUTPUT_PORCELAIN 010
1850 #define OUTPUT_SHOW_NAME 020
1851 #define OUTPUT_SHOW_NUMBER 040
1852 #define OUTPUT_SHOW_SCORE 0100
1853 #define OUTPUT_NO_AUTHOR 0200
1854 #define OUTPUT_SHOW_EMAIL 0400
1855 #define OUTPUT_LINE_PORCELAIN 01000
1857 static void emit_porcelain_details(struct origin
*suspect
, int repeat
)
1859 if (emit_one_suspect_detail(suspect
, repeat
) ||
1860 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
1861 write_filename_info(suspect
->path
);
1864 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
,
1867 int repeat
= opt
& OUTPUT_LINE_PORCELAIN
;
1870 struct origin
*suspect
= ent
->suspect
;
1873 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1874 printf("%s %d %d %d\n",
1879 emit_porcelain_details(suspect
, repeat
);
1881 cp
= nth_line(sb
, ent
->lno
);
1882 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1885 printf("%s %d %d\n", hex
,
1886 ent
->s_lno
+ 1 + cnt
,
1887 ent
->lno
+ 1 + cnt
);
1889 emit_porcelain_details(suspect
, 1);
1895 } while (ch
!= '\n' &&
1896 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1899 if (sb
->final_buf_size
&& cp
[-1] != '\n')
1903 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1907 struct origin
*suspect
= ent
->suspect
;
1908 struct commit_info ci
;
1910 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1912 get_commit_info(suspect
->commit
, &ci
, 1);
1913 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1915 cp
= nth_line(sb
, ent
->lno
);
1916 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1918 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : abbrev
;
1920 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1922 memset(hex
, ' ', length
);
1923 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
1929 printf("%.*s", length
, hex
);
1930 if (opt
& OUTPUT_ANNOTATE_COMPAT
) {
1932 if (opt
& OUTPUT_SHOW_EMAIL
)
1933 name
= ci
.author_mail
.buf
;
1935 name
= ci
.author
.buf
;
1936 printf("\t(%10s\t%10s\t%d)", name
,
1937 format_time(ci
.author_time
, ci
.author_tz
.buf
,
1939 ent
->lno
+ 1 + cnt
);
1941 if (opt
& OUTPUT_SHOW_SCORE
)
1943 max_score_digits
, ent
->score
,
1944 ent
->suspect
->refcnt
);
1945 if (opt
& OUTPUT_SHOW_NAME
)
1946 printf(" %-*.*s", longest_file
, longest_file
,
1948 if (opt
& OUTPUT_SHOW_NUMBER
)
1949 printf(" %*d", max_orig_digits
,
1950 ent
->s_lno
+ 1 + cnt
);
1952 if (!(opt
& OUTPUT_NO_AUTHOR
)) {
1955 if (opt
& OUTPUT_SHOW_EMAIL
)
1956 name
= ci
.author_mail
.buf
;
1958 name
= ci
.author
.buf
;
1959 pad
= longest_author
- utf8_strwidth(name
);
1960 printf(" (%s%*s %10s",
1962 format_time(ci
.author_time
,
1967 max_digits
, ent
->lno
+ 1 + cnt
);
1972 } while (ch
!= '\n' &&
1973 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1976 if (sb
->final_buf_size
&& cp
[-1] != '\n')
1979 commit_info_destroy(&ci
);
1982 static void output(struct scoreboard
*sb
, int option
)
1984 struct blame_entry
*ent
;
1986 if (option
& OUTPUT_PORCELAIN
) {
1987 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1989 struct origin
*suspect
;
1990 struct commit
*commit
= ent
->suspect
->commit
;
1991 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1993 for (suspect
= commit
->util
; suspect
; suspect
= suspect
->next
) {
1994 if (suspect
->guilty
&& count
++) {
1995 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
2002 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2003 if (option
& OUTPUT_PORCELAIN
)
2004 emit_porcelain(sb
, ent
, option
);
2006 emit_other(sb
, ent
, option
);
2011 static const char *get_next_line(const char *start
, const char *end
)
2013 const char *nl
= memchr(start
, '\n', end
- start
);
2014 return nl
? nl
+ 1 : end
;
2018 * To allow quick access to the contents of nth line in the
2019 * final image, prepare an index in the scoreboard.
2021 static int prepare_lines(struct scoreboard
*sb
)
2023 const char *buf
= sb
->final_buf
;
2024 unsigned long len
= sb
->final_buf_size
;
2025 const char *end
= buf
+ len
;
2030 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2033 sb
->lineno
= lineno
= xmalloc(sizeof(*sb
->lineno
) * (num
+ 1));
2035 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2036 *lineno
++ = p
- buf
;
2040 sb
->num_lines
= num
;
2041 return sb
->num_lines
;
2045 * Add phony grafts for use with -S; this is primarily to
2046 * support git's cvsserver that wants to give a linear history
2049 static int read_ancestry(const char *graft_file
)
2051 FILE *fp
= fopen(graft_file
, "r");
2052 struct strbuf buf
= STRBUF_INIT
;
2055 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
2056 /* The format is just "Commit Parent1 Parent2 ...\n" */
2057 struct commit_graft
*graft
= read_graft_line(buf
.buf
, buf
.len
);
2059 register_commit_graft(graft
, 0);
2062 strbuf_release(&buf
);
2066 static int update_auto_abbrev(int auto_abbrev
, struct origin
*suspect
)
2068 const char *uniq
= find_unique_abbrev(suspect
->commit
->object
.sha1
,
2070 int len
= strlen(uniq
);
2071 if (auto_abbrev
< len
)
2077 * How many columns do we need to show line numbers, authors,
2080 static void find_alignment(struct scoreboard
*sb
, int *option
)
2082 int longest_src_lines
= 0;
2083 int longest_dst_lines
= 0;
2084 unsigned largest_score
= 0;
2085 struct blame_entry
*e
;
2086 int compute_auto_abbrev
= (abbrev
< 0);
2087 int auto_abbrev
= default_abbrev
;
2089 for (e
= sb
->ent
; e
; e
= e
->next
) {
2090 struct origin
*suspect
= e
->suspect
;
2091 struct commit_info ci
;
2094 if (compute_auto_abbrev
)
2095 auto_abbrev
= update_auto_abbrev(auto_abbrev
, suspect
);
2096 if (strcmp(suspect
->path
, sb
->path
))
2097 *option
|= OUTPUT_SHOW_NAME
;
2098 num
= strlen(suspect
->path
);
2099 if (longest_file
< num
)
2101 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
2102 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
2103 get_commit_info(suspect
->commit
, &ci
, 1);
2104 if (*option
& OUTPUT_SHOW_EMAIL
)
2105 num
= utf8_strwidth(ci
.author_mail
.buf
);
2107 num
= utf8_strwidth(ci
.author
.buf
);
2108 if (longest_author
< num
)
2109 longest_author
= num
;
2111 num
= e
->s_lno
+ e
->num_lines
;
2112 if (longest_src_lines
< num
)
2113 longest_src_lines
= num
;
2114 num
= e
->lno
+ e
->num_lines
;
2115 if (longest_dst_lines
< num
)
2116 longest_dst_lines
= num
;
2117 if (largest_score
< ent_score(sb
, e
))
2118 largest_score
= ent_score(sb
, e
);
2120 commit_info_destroy(&ci
);
2122 max_orig_digits
= decimal_width(longest_src_lines
);
2123 max_digits
= decimal_width(longest_dst_lines
);
2124 max_score_digits
= decimal_width(largest_score
);
2126 if (compute_auto_abbrev
)
2127 /* one more abbrev length is needed for the boundary commit */
2128 abbrev
= auto_abbrev
+ 1;
2132 * For debugging -- origin is refcounted, and this asserts that
2133 * we do not underflow.
2135 static void sanity_check_refcnt(struct scoreboard
*sb
)
2138 struct blame_entry
*ent
;
2140 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2141 /* Nobody should have zero or negative refcnt */
2142 if (ent
->suspect
->refcnt
<= 0) {
2143 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
2145 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
2146 ent
->suspect
->refcnt
);
2152 find_alignment(sb
, &opt
);
2154 die("Baa %d!", baa
);
2159 * Used for the command line parsing; check if the path exists
2160 * in the working tree.
2162 static int has_string_in_work_tree(const char *path
)
2165 return !lstat(path
, &st
);
2168 static unsigned parse_score(const char *arg
)
2171 unsigned long score
= strtoul(arg
, &end
, 10);
2177 static const char *add_prefix(const char *prefix
, const char *path
)
2179 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
2182 static int git_blame_config(const char *var
, const char *value
, void *cb
)
2184 if (!strcmp(var
, "blame.showroot")) {
2185 show_root
= git_config_bool(var
, value
);
2188 if (!strcmp(var
, "blame.blankboundary")) {
2189 blank_boundary
= git_config_bool(var
, value
);
2192 if (!strcmp(var
, "blame.date")) {
2194 return config_error_nonbool(var
);
2195 blame_date_mode
= parse_date_format(value
);
2199 if (userdiff_config(var
, value
) < 0)
2202 return git_default_config(var
, value
, cb
);
2205 static void verify_working_tree_path(struct commit
*work_tree
, const char *path
)
2207 struct commit_list
*parents
;
2209 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
2210 const unsigned char *commit_sha1
= parents
->item
->object
.sha1
;
2211 unsigned char blob_sha1
[20];
2214 if (!get_tree_entry(commit_sha1
, path
, blob_sha1
, &mode
) &&
2215 sha1_object_info(blob_sha1
, NULL
) == OBJ_BLOB
)
2218 die("no such path '%s' in HEAD", path
);
2221 static struct commit_list
**append_parent(struct commit_list
**tail
, const unsigned char *sha1
)
2223 struct commit
*parent
;
2225 parent
= lookup_commit_reference(sha1
);
2227 die("no such commit %s", sha1_to_hex(sha1
));
2228 return &commit_list_insert(parent
, tail
)->next
;
2231 static void append_merge_parents(struct commit_list
**tail
)
2234 const char *merge_head_file
= git_path("MERGE_HEAD");
2235 struct strbuf line
= STRBUF_INIT
;
2237 merge_head
= open(merge_head_file
, O_RDONLY
);
2238 if (merge_head
< 0) {
2239 if (errno
== ENOENT
)
2241 die("cannot open '%s' for reading", merge_head_file
);
2244 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
2245 unsigned char sha1
[20];
2246 if (line
.len
< 40 || get_sha1_hex(line
.buf
, sha1
))
2247 die("unknown line in '%s': %s", merge_head_file
, line
.buf
);
2248 tail
= append_parent(tail
, sha1
);
2251 strbuf_release(&line
);
2255 * This isn't as simple as passing sb->buf and sb->len, because we
2256 * want to transfer ownership of the buffer to the commit (so we
2259 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
2262 void *buf
= strbuf_detach(sb
, &len
);
2263 set_commit_buffer(c
, buf
, len
);
2267 * Prepare a dummy commit that represents the work tree (or staged) item.
2268 * Note that annotating work tree item never works in the reverse.
2270 static struct commit
*fake_working_tree_commit(struct diff_options
*opt
,
2272 const char *contents_from
)
2274 struct commit
*commit
;
2275 struct origin
*origin
;
2276 struct commit_list
**parent_tail
, *parent
;
2277 unsigned char head_sha1
[20];
2278 struct strbuf buf
= STRBUF_INIT
;
2282 struct cache_entry
*ce
;
2284 struct strbuf msg
= STRBUF_INIT
;
2287 commit
= alloc_commit_node();
2288 commit
->object
.parsed
= 1;
2290 commit
->object
.type
= OBJ_COMMIT
;
2291 parent_tail
= &commit
->parents
;
2293 if (!resolve_ref_unsafe("HEAD", head_sha1
, 1, NULL
))
2294 die("no such ref: HEAD");
2296 parent_tail
= append_parent(parent_tail
, head_sha1
);
2297 append_merge_parents(parent_tail
);
2298 verify_working_tree_path(commit
, path
);
2300 origin
= make_origin(commit
, path
);
2302 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2303 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
2304 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
2305 strbuf_addf(&msg
, "parent %s\n",
2306 sha1_to_hex(parent
->item
->object
.sha1
));
2310 "Version of %s from %s\n",
2312 (!contents_from
? path
:
2313 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
2314 set_commit_buffer_from_strbuf(commit
, &msg
);
2316 if (!contents_from
|| strcmp("-", contents_from
)) {
2318 const char *read_from
;
2320 unsigned long buf_len
;
2322 if (contents_from
) {
2323 if (stat(contents_from
, &st
) < 0)
2324 die_errno("Cannot stat '%s'", contents_from
);
2325 read_from
= contents_from
;
2328 if (lstat(path
, &st
) < 0)
2329 die_errno("Cannot lstat '%s'", path
);
2332 mode
= canon_mode(st
.st_mode
);
2334 switch (st
.st_mode
& S_IFMT
) {
2336 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
2337 textconv_object(read_from
, mode
, null_sha1
, 0, &buf_ptr
, &buf_len
))
2338 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
2339 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2340 die_errno("cannot open or read '%s'", read_from
);
2343 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
2344 die_errno("cannot readlink '%s'", read_from
);
2347 die("unsupported file type %s", read_from
);
2351 /* Reading from stdin */
2353 if (strbuf_read(&buf
, 0, 0) < 0)
2354 die_errno("failed to read from stdin");
2356 origin
->file
.ptr
= buf
.buf
;
2357 origin
->file
.size
= buf
.len
;
2358 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2361 * Read the current index, replace the path entry with
2362 * origin->blob_sha1 without mucking with its mode or type
2363 * bits; we are not going to write this index out -- we just
2364 * want to run "diff-index --cached".
2371 int pos
= cache_name_pos(path
, len
);
2373 mode
= active_cache
[pos
]->ce_mode
;
2375 /* Let's not bother reading from HEAD tree */
2376 mode
= S_IFREG
| 0644;
2378 size
= cache_entry_size(len
);
2379 ce
= xcalloc(1, size
);
2380 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2381 memcpy(ce
->name
, path
, len
);
2382 ce
->ce_flags
= create_ce_flags(0);
2383 ce
->ce_namelen
= len
;
2384 ce
->ce_mode
= create_ce_mode(mode
);
2385 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2388 * We are not going to write this out, so this does not matter
2389 * right now, but someday we might optimize diff-index --cached
2390 * with cache-tree information.
2392 cache_tree_invalidate_path(&the_index
, path
);
2397 static const char *prepare_final(struct scoreboard
*sb
)
2400 const char *final_commit_name
= NULL
;
2401 struct rev_info
*revs
= sb
->revs
;
2404 * There must be one and only one positive commit in the
2405 * revs->pending array.
2407 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2408 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2409 if (obj
->flags
& UNINTERESTING
)
2411 while (obj
->type
== OBJ_TAG
)
2412 obj
= deref_tag(obj
, NULL
, 0);
2413 if (obj
->type
!= OBJ_COMMIT
)
2414 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2416 die("More than one commit to dig from %s and %s?",
2417 revs
->pending
.objects
[i
].name
,
2419 sb
->final
= (struct commit
*) obj
;
2420 final_commit_name
= revs
->pending
.objects
[i
].name
;
2422 return final_commit_name
;
2425 static const char *prepare_initial(struct scoreboard
*sb
)
2428 const char *final_commit_name
= NULL
;
2429 struct rev_info
*revs
= sb
->revs
;
2432 * There must be one and only one negative commit, and it must be
2435 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2436 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2437 if (!(obj
->flags
& UNINTERESTING
))
2439 while (obj
->type
== OBJ_TAG
)
2440 obj
= deref_tag(obj
, NULL
, 0);
2441 if (obj
->type
!= OBJ_COMMIT
)
2442 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2444 die("More than one commit to dig down to %s and %s?",
2445 revs
->pending
.objects
[i
].name
,
2447 sb
->final
= (struct commit
*) obj
;
2448 final_commit_name
= revs
->pending
.objects
[i
].name
;
2450 if (!final_commit_name
)
2451 die("No commit to dig down to?");
2452 return final_commit_name
;
2455 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2457 int *opt
= option
->value
;
2460 * -C enables copy from removed files;
2461 * -C -C enables copy from existing files, but only
2462 * when blaming a new file;
2463 * -C -C -C enables copy from existing files for
2466 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2467 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2468 if (*opt
& PICKAXE_BLAME_COPY
)
2469 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2470 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2473 blame_copy_score
= parse_score(arg
);
2477 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2479 int *opt
= option
->value
;
2481 *opt
|= PICKAXE_BLAME_MOVE
;
2484 blame_move_score
= parse_score(arg
);
2488 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2490 struct rev_info revs
;
2492 struct scoreboard sb
;
2494 struct blame_entry
*ent
= NULL
;
2495 long dashdash_pos
, lno
;
2496 const char *final_commit_name
= NULL
;
2497 enum object_type type
;
2499 static struct string_list range_list
;
2500 static int output_option
= 0, opt
= 0;
2501 static int show_stats
= 0;
2502 static const char *revs_file
= NULL
;
2503 static const char *contents_from
= NULL
;
2504 static const struct option options
[] = {
2505 OPT_BOOL(0, "incremental", &incremental
, N_("Show blame entries as we find them, incrementally")),
2506 OPT_BOOL('b', NULL
, &blank_boundary
, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2507 OPT_BOOL(0, "root", &show_root
, N_("Do not treat root commits as boundaries (Default: off)")),
2508 OPT_BOOL(0, "show-stats", &show_stats
, N_("Show work cost statistics")),
2509 OPT_BIT(0, "score-debug", &output_option
, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE
),
2510 OPT_BIT('f', "show-name", &output_option
, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME
),
2511 OPT_BIT('n', "show-number", &output_option
, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER
),
2512 OPT_BIT('p', "porcelain", &output_option
, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN
),
2513 OPT_BIT(0, "line-porcelain", &output_option
, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN
|OUTPUT_LINE_PORCELAIN
),
2514 OPT_BIT('c', NULL
, &output_option
, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT
),
2515 OPT_BIT('t', NULL
, &output_option
, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP
),
2516 OPT_BIT('l', NULL
, &output_option
, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME
),
2517 OPT_BIT('s', NULL
, &output_option
, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR
),
2518 OPT_BIT('e', "show-email", &output_option
, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL
),
2519 OPT_BIT('w', NULL
, &xdl_opts
, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE
),
2520 OPT_BIT(0, "minimal", &xdl_opts
, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL
),
2521 OPT_STRING('S', NULL
, &revs_file
, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2522 OPT_STRING(0, "contents", &contents_from
, N_("file"), N_("Use <file>'s contents as the final image")),
2523 { OPTION_CALLBACK
, 'C', NULL
, &opt
, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG
, blame_copy_callback
},
2524 { OPTION_CALLBACK
, 'M', NULL
, &opt
, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG
, blame_move_callback
},
2525 OPT_STRING_LIST('L', NULL
, &range_list
, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2526 OPT__ABBREV(&abbrev
),
2530 struct parse_opt_ctx_t ctx
;
2531 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2532 struct range_set ranges
;
2533 unsigned int range_i
;
2536 git_config(git_blame_config
, NULL
);
2537 init_revisions(&revs
, NULL
);
2538 revs
.date_mode
= blame_date_mode
;
2539 DIFF_OPT_SET(&revs
.diffopt
, ALLOW_TEXTCONV
);
2540 DIFF_OPT_SET(&revs
.diffopt
, FOLLOW_RENAMES
);
2542 save_commit_buffer
= 0;
2545 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
2546 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
2548 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2549 case PARSE_OPT_HELP
:
2551 case PARSE_OPT_DONE
:
2553 dashdash_pos
= ctx
.cpidx
;
2557 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2558 ctx
.argv
[0] = "--children";
2561 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2564 no_whole_file_rename
= !DIFF_OPT_TST(&revs
.diffopt
, FOLLOW_RENAMES
);
2565 DIFF_OPT_CLR(&revs
.diffopt
, FOLLOW_RENAMES
);
2566 argc
= parse_options_end(&ctx
);
2569 /* one more abbrev length is needed for the boundary commit */
2572 if (revs_file
&& read_ancestry(revs_file
))
2573 die_errno("reading graft file '%s' failed", revs_file
);
2575 if (cmd_is_annotate
) {
2576 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2577 blame_date_mode
= DATE_ISO8601
;
2579 blame_date_mode
= revs
.date_mode
;
2582 /* The maximum width used to show the dates */
2583 switch (blame_date_mode
) {
2585 blame_date_width
= sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2588 blame_date_width
= sizeof("2006-10-19 16:00:04 -0700");
2591 blame_date_width
= sizeof("1161298804 -0700");
2594 blame_date_width
= sizeof("2006-10-19");
2597 /* TRANSLATORS: This string is used to tell us the maximum
2598 display width for a relative timestamp in "git blame"
2599 output. For C locale, "4 years, 11 months ago", which
2600 takes 22 places, is the longest among various forms of
2601 relative timestamps, but your language may need more or
2602 fewer display columns. */
2603 blame_date_width
= utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2607 blame_date_width
= sizeof("Thu Oct 19 16:00:04 2006 -0700");
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 && !has_string_in_work_tree(path
)) { /* (2b) */
2661 path
= add_prefix(prefix
, argv
[1]);
2664 argv
[argc
- 1] = "--";
2667 if (!has_string_in_work_tree(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
)) {
2712 buf
= xmalloc(o
->file
.size
+ 1);
2713 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2715 sb
.final_buf_size
= o
->file
.size
;
2718 o
= get_origin(&sb
, sb
.final
, path
);
2719 if (fill_blob_sha1_and_mode(o
))
2720 die("no such path %s in %s", path
, final_commit_name
);
2722 if (DIFF_OPT_TST(&sb
.revs
->diffopt
, ALLOW_TEXTCONV
) &&
2723 textconv_object(path
, o
->mode
, o
->blob_sha1
, 1, (char **) &sb
.final_buf
,
2724 &sb
.final_buf_size
))
2727 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2728 &sb
.final_buf_size
);
2731 die("Cannot read blob %s for path %s",
2732 sha1_to_hex(o
->blob_sha1
),
2736 lno
= prepare_lines(&sb
);
2738 if (lno
&& !range_list
.nr
)
2739 string_list_append(&range_list
, xstrdup("1"));
2742 range_set_init(&ranges
, range_list
.nr
);
2743 for (range_i
= 0; range_i
< range_list
.nr
; ++range_i
) {
2745 if (parse_range_arg(range_list
.items
[range_i
].string
,
2746 nth_line_cb
, &sb
, lno
, anchor
,
2747 &bottom
, &top
, sb
.path
))
2749 if (lno
< top
|| ((lno
|| bottom
) && lno
< bottom
))
2750 die("file %s has only %lu lines", path
, lno
);
2756 range_set_append_unsafe(&ranges
, bottom
, top
);
2759 sort_and_merge_range_set(&ranges
);
2761 for (range_i
= ranges
.nr
; range_i
> 0; --range_i
) {
2762 const struct range
*r
= &ranges
.ranges
[range_i
- 1];
2763 long bottom
= r
->start
;
2765 struct blame_entry
*next
= ent
;
2766 ent
= xcalloc(1, sizeof(*ent
));
2768 ent
->num_lines
= top
- bottom
;
2770 ent
->s_lno
= bottom
;
2776 prio_queue_put(&sb
.commits
, o
->commit
);
2780 range_set_release(&ranges
);
2781 string_list_clear(&range_list
, 0);
2786 read_mailmap(&mailmap
, NULL
);
2791 assign_blame(&sb
, opt
);
2796 sb
.ent
= blame_sort(sb
.ent
, compare_blame_final
);
2800 if (!(output_option
& OUTPUT_PORCELAIN
))
2801 find_alignment(&sb
, &output_option
);
2803 output(&sb
, output_option
);
2804 free((void *)sb
.final_buf
);
2805 for (ent
= sb
.ent
; ent
; ) {
2806 struct blame_entry
*e
= ent
->next
;
2812 printf("num read blob: %d\n", num_read_blob
);
2813 printf("num get patch: %d\n", num_get_patch
);
2814 printf("num commits: %d\n", num_commits
);