4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
14 #include "tree-walk.h"
19 #include "xdiff-interface.h"
20 #include "cache-tree.h"
21 #include "string-list.h"
23 #include "mergesort.h"
24 #include "parse-options.h"
25 #include "prio-queue.h"
28 #include "line-range.h"
33 static char blame_usage
[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
35 static const char *blame_opt_usage
[] = {
38 N_("<rev-opts> are documented in git-rev-list(1)"),
42 static int longest_file
;
43 static int longest_author
;
44 static int max_orig_digits
;
45 static int max_digits
;
46 static int max_score_digits
;
49 static int blank_boundary
;
50 static int incremental
;
52 static int abbrev
= -1;
53 static int no_whole_file_rename
;
54 static int show_progress
;
56 static struct date_mode blame_date_mode
= { DATE_ISO8601
};
57 static size_t blame_date_width
;
59 static struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
66 static int num_read_blob
;
67 static int num_get_patch
;
68 static int num_commits
;
70 #define PICKAXE_BLAME_MOVE 01
71 #define PICKAXE_BLAME_COPY 02
72 #define PICKAXE_BLAME_COPY_HARDER 04
73 #define PICKAXE_BLAME_COPY_HARDEST 010
76 * blame for a blame_entry with score lower than these thresholds
77 * is not passed to the parent using move/copy logic.
79 static unsigned blame_move_score
;
80 static unsigned blame_copy_score
;
81 #define BLAME_DEFAULT_MOVE_SCORE 20
82 #define BLAME_DEFAULT_COPY_SCORE 40
84 /* Remember to update object flag allocation in object.h */
85 #define METAINFO_SHOWN (1u<<12)
86 #define MORE_THAN_ONE_PATH (1u<<13)
89 * One blob in a commit that is being suspected
93 /* Record preceding blame record for this blob */
94 struct origin
*previous
;
95 /* origins are put in a list linked via `next' hanging off the
96 * corresponding commit's util field in order to make finding
97 * them fast. The presence in this chain does not count
98 * towards the origin's reference count. It is tempting to
99 * let it count as long as the commit is pending examination,
100 * but even under circumstances where the commit will be
101 * present multiple times in the priority queue of unexamined
102 * commits, processing the first instance will not leave any
103 * work requiring the origin data for the second instance. An
104 * interspersed commit changing that would have to be
105 * preexisting with a different ancestry and with the same
106 * commit date in order to wedge itself between two instances
107 * of the same commit in the priority queue _and_ produce
108 * blame entries relevant for it. While we don't want to let
109 * us get tripped up by this case, it certainly does not seem
110 * worth optimizing for.
113 struct commit
*commit
;
114 /* `suspects' contains blame entries that may be attributed to
115 * this origin's commit or to parent commits. When a commit
116 * is being processed, all suspects will be moved, either by
117 * assigning them to an origin in a different commit, or by
118 * shipping them to the scoreboard's ent list because they
119 * cannot be attributed to a different commit.
121 struct blame_entry
*suspects
;
123 struct object_id blob_oid
;
125 /* guilty gets set when shipping any suspects to the final
126 * blame list instead of other commits
129 char path
[FLEX_ARRAY
];
132 struct progress_info
{
133 struct progress
*progress
;
137 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
138 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
)
141 xdemitconf_t xecfg
= {0};
142 xdemitcb_t ecb
= {NULL
};
144 xpp
.flags
= xdl_opts
;
145 xecfg
.hunk_func
= hunk_func
;
147 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
151 * Prepare diff_filespec and convert it using diff textconv API
152 * if the textconv driver exists.
153 * Return 1 if the conversion succeeds, 0 otherwise.
155 int textconv_object(const char *path
,
157 const struct object_id
*oid
,
160 unsigned long *buf_size
)
162 struct diff_filespec
*df
;
163 struct userdiff_driver
*textconv
;
165 df
= alloc_filespec(path
);
166 fill_filespec(df
, oid
->hash
, oid_valid
, mode
);
167 textconv
= get_textconv(df
);
173 *buf_size
= fill_textconv(textconv
, df
, buf
);
179 * Given an origin, prepare mmfile_t structure to be used by the
182 static void fill_origin_blob(struct diff_options
*opt
,
183 struct origin
*o
, mmfile_t
*file
)
186 enum object_type type
;
187 unsigned long file_size
;
190 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
191 textconv_object(o
->path
, o
->mode
, &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
194 file
->ptr
= read_sha1_file(o
->blob_oid
.hash
, &type
,
196 file
->size
= file_size
;
199 die("Cannot read blob %s for path %s",
200 oid_to_hex(&o
->blob_oid
),
209 * Origin is refcounted and usually we keep the blob contents to be
212 static inline struct origin
*origin_incref(struct origin
*o
)
219 static void origin_decref(struct origin
*o
)
221 if (o
&& --o
->refcnt
<= 0) {
222 struct origin
*p
, *l
= NULL
;
224 origin_decref(o
->previous
);
226 /* Should be present exactly once in commit chain */
227 for (p
= o
->commit
->util
; p
; l
= p
, p
= p
->next
) {
232 o
->commit
->util
= p
->next
;
237 die("internal error in blame::origin_decref");
241 static void drop_origin_blob(struct origin
*o
)
250 * Each group of lines is described by a blame_entry; it can be split
251 * as we pass blame to the parents. They are arranged in linked lists
252 * kept as `suspects' of some unprocessed origin, or entered (when the
253 * blame origin has been finalized) into the scoreboard structure.
254 * While the scoreboard structure is only sorted at the end of
255 * processing (according to final image line number), the lists
256 * attached to an origin are sorted by the target line number.
259 struct blame_entry
*next
;
261 /* the first line of this group in the final image;
262 * internally all line numbers are 0 based.
266 /* how many lines this group has */
269 /* the commit that introduced this group into the final image */
270 struct origin
*suspect
;
272 /* the line number of the first line of this group in the
273 * suspect's file; internally all line numbers are 0 based.
277 /* how significant this entry is -- cached to avoid
278 * scanning the lines over and over.
284 * Any merge of blames happens on lists of blames that arrived via
285 * different parents in a single suspect. In this case, we want to
286 * sort according to the suspect line numbers as opposed to the final
287 * image line numbers. The function body is somewhat longish because
288 * it avoids unnecessary writes.
291 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
292 struct blame_entry
*list2
)
294 struct blame_entry
*p1
= list1
, *p2
= list2
,
302 if (p1
->s_lno
<= p2
->s_lno
) {
305 if ((p1
= *tail
) == NULL
) {
309 } while (p1
->s_lno
<= p2
->s_lno
);
315 if ((p2
= *tail
) == NULL
) {
319 } while (p1
->s_lno
> p2
->s_lno
);
323 if ((p1
= *tail
) == NULL
) {
327 } while (p1
->s_lno
<= p2
->s_lno
);
331 static void *get_next_blame(const void *p
)
333 return ((struct blame_entry
*)p
)->next
;
336 static void set_next_blame(void *p1
, void *p2
)
338 ((struct blame_entry
*)p1
)->next
= p2
;
342 * Final image line numbers are all different, so we don't need a
343 * three-way comparison here.
346 static int compare_blame_final(const void *p1
, const void *p2
)
348 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
352 static int compare_blame_suspect(const void *p1
, const void *p2
)
354 const struct blame_entry
*s1
= p1
, *s2
= p2
;
356 * to allow for collating suspects, we sort according to the
357 * respective pointer value as the primary sorting criterion.
358 * The actual relation is pretty unimportant as long as it
359 * establishes a total order. Comparing as integers gives us
362 if (s1
->suspect
!= s2
->suspect
)
363 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
364 if (s1
->s_lno
== s2
->s_lno
)
366 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
369 static struct blame_entry
*blame_sort(struct blame_entry
*head
,
370 int (*compare_fn
)(const void *, const void *))
372 return llist_mergesort (head
, get_next_blame
, set_next_blame
, compare_fn
);
375 static int compare_commits_by_reverse_commit_date(const void *a
,
379 return -compare_commits_by_commit_date(a
, b
, c
);
383 * The current state of the blame assignment.
386 /* the final commit (i.e. where we started digging from) */
387 struct commit
*final
;
388 /* Priority queue for commits with unassigned blame records */
389 struct prio_queue commits
;
390 struct rev_info
*revs
;
394 * The contents in the final image.
395 * Used by many functions to obtain contents of the nth line,
396 * indexed with scoreboard.lineno[blame_entry.lno].
398 const char *final_buf
;
399 unsigned long final_buf_size
;
401 /* linked list of blames */
402 struct blame_entry
*ent
;
404 /* look-up a line in the final buffer */
409 static void sanity_check_refcnt(struct scoreboard
*);
412 * If two blame entries that are next to each other came from
413 * contiguous lines in the same origin (i.e. <commit, path> pair),
414 * merge them together.
416 static void coalesce(struct scoreboard
*sb
)
418 struct blame_entry
*ent
, *next
;
420 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
421 if (ent
->suspect
== next
->suspect
&&
422 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
423 ent
->num_lines
+= next
->num_lines
;
424 ent
->next
= next
->next
;
425 origin_decref(next
->suspect
);
428 next
= ent
; /* again */
432 if (DEBUG
) /* sanity */
433 sanity_check_refcnt(sb
);
437 * Merge the given sorted list of blames into a preexisting origin.
438 * If there were no previous blames to that commit, it is entered into
439 * the commit priority queue of the score board.
442 static void queue_blames(struct scoreboard
*sb
, struct origin
*porigin
,
443 struct blame_entry
*sorted
)
445 if (porigin
->suspects
)
446 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
449 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
451 porigin
->suspects
= sorted
;
455 porigin
->suspects
= sorted
;
456 prio_queue_put(&sb
->commits
, porigin
->commit
);
461 * Given a commit and a path in it, create a new origin structure.
462 * The callers that add blame to the scoreboard should use
463 * get_origin() to obtain shared, refcounted copy instead of calling
464 * this function directly.
466 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
469 FLEX_ALLOC_STR(o
, path
, path
);
472 o
->next
= commit
->util
;
478 * Locate an existing origin or create a new one.
479 * This moves the origin to front position in the commit util list.
481 static struct origin
*get_origin(struct scoreboard
*sb
,
482 struct commit
*commit
,
485 struct origin
*o
, *l
;
487 for (o
= commit
->util
, l
= NULL
; o
; l
= o
, o
= o
->next
) {
488 if (!strcmp(o
->path
, path
)) {
492 o
->next
= commit
->util
;
495 return origin_incref(o
);
498 return make_origin(commit
, path
);
502 * Fill the blob_sha1 field of an origin if it hasn't, so that later
503 * call to fill_origin_blob() can use it to locate the data. blob_sha1
504 * for an origin is also used to pass the blame for the entire file to
505 * the parent to detect the case where a child's blob is identical to
506 * that of its parent's.
508 * This also fills origin->mode for corresponding tree path.
510 static int fill_blob_sha1_and_mode(struct origin
*origin
)
512 if (!is_null_oid(&origin
->blob_oid
))
514 if (get_tree_entry(origin
->commit
->object
.oid
.hash
,
516 origin
->blob_oid
.hash
, &origin
->mode
))
518 if (sha1_object_info(origin
->blob_oid
.hash
, NULL
) != OBJ_BLOB
)
522 oidclr(&origin
->blob_oid
);
523 origin
->mode
= S_IFINVALID
;
528 * We have an origin -- check if the same path exists in the
529 * parent and return an origin structure to represent it.
531 static struct origin
*find_origin(struct scoreboard
*sb
,
532 struct commit
*parent
,
533 struct origin
*origin
)
535 struct origin
*porigin
;
536 struct diff_options diff_opts
;
537 const char *paths
[2];
539 /* First check any existing origins */
540 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
541 if (!strcmp(porigin
->path
, origin
->path
)) {
543 * The same path between origin and its parent
544 * without renaming -- the most common case.
546 return origin_incref (porigin
);
549 /* See if the origin->path is different between parent
550 * and origin first. Most of the time they are the
551 * same and diff-tree is fairly efficient about this.
553 diff_setup(&diff_opts
);
554 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
555 diff_opts
.detect_rename
= 0;
556 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
557 paths
[0] = origin
->path
;
560 parse_pathspec(&diff_opts
.pathspec
,
561 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
562 PATHSPEC_LITERAL_PATH
, "", paths
);
563 diff_setup_done(&diff_opts
);
565 if (is_null_oid(&origin
->commit
->object
.oid
))
566 do_diff_cache(parent
->tree
->object
.oid
.hash
, &diff_opts
);
568 diff_tree_sha1(parent
->tree
->object
.oid
.hash
,
569 origin
->commit
->tree
->object
.oid
.hash
,
571 diffcore_std(&diff_opts
);
573 if (!diff_queued_diff
.nr
) {
574 /* The path is the same as parent */
575 porigin
= get_origin(sb
, parent
, origin
->path
);
576 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
577 porigin
->mode
= origin
->mode
;
580 * Since origin->path is a pathspec, if the parent
581 * commit had it as a directory, we will see a whole
582 * bunch of deletion of files in the directory that we
586 struct diff_filepair
*p
= NULL
;
587 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
589 p
= diff_queued_diff
.queue
[i
];
590 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
591 if (!strcmp(name
, origin
->path
))
595 die("internal error in blame::find_origin");
598 die("internal error in blame::find_origin (%c)",
601 porigin
= get_origin(sb
, parent
, origin
->path
);
602 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
603 porigin
->mode
= p
->one
->mode
;
607 /* Did not exist in parent, or type changed */
611 diff_flush(&diff_opts
);
612 clear_pathspec(&diff_opts
.pathspec
);
617 * We have an origin -- find the path that corresponds to it in its
618 * parent and return an origin structure to represent it.
620 static struct origin
*find_rename(struct scoreboard
*sb
,
621 struct commit
*parent
,
622 struct origin
*origin
)
624 struct origin
*porigin
= NULL
;
625 struct diff_options diff_opts
;
628 diff_setup(&diff_opts
);
629 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
630 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
631 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
632 diff_opts
.single_follow
= origin
->path
;
633 diff_setup_done(&diff_opts
);
635 if (is_null_oid(&origin
->commit
->object
.oid
))
636 do_diff_cache(parent
->tree
->object
.oid
.hash
, &diff_opts
);
638 diff_tree_sha1(parent
->tree
->object
.oid
.hash
,
639 origin
->commit
->tree
->object
.oid
.hash
,
641 diffcore_std(&diff_opts
);
643 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
644 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
645 if ((p
->status
== 'R' || p
->status
== 'C') &&
646 !strcmp(p
->two
->path
, origin
->path
)) {
647 porigin
= get_origin(sb
, parent
, p
->one
->path
);
648 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
649 porigin
->mode
= p
->one
->mode
;
653 diff_flush(&diff_opts
);
654 clear_pathspec(&diff_opts
.pathspec
);
659 * Append a new blame entry to a given output queue.
661 static void add_blame_entry(struct blame_entry
***queue
, struct blame_entry
*e
)
663 origin_incref(e
->suspect
);
671 * src typically is on-stack; we want to copy the information in it to
672 * a malloced blame_entry that gets added to the given queue. The
673 * origin of dst loses a refcnt.
675 static void dup_entry(struct blame_entry
***queue
,
676 struct blame_entry
*dst
, struct blame_entry
*src
)
678 origin_incref(src
->suspect
);
679 origin_decref(dst
->suspect
);
680 memcpy(dst
, src
, sizeof(*src
));
686 static const char *nth_line(struct scoreboard
*sb
, long lno
)
688 return sb
->final_buf
+ sb
->lineno
[lno
];
691 static const char *nth_line_cb(void *data
, long lno
)
693 return nth_line((struct scoreboard
*)data
, lno
);
697 * It is known that lines between tlno to same came from parent, and e
698 * has an overlap with that range. it also is known that parent's
699 * line plno corresponds to e's line tlno.
705 * <------------------>
707 * Split e into potentially three parts; before this chunk, the chunk
708 * to be blamed for the parent, and after that portion.
710 static void split_overlap(struct blame_entry
*split
,
711 struct blame_entry
*e
,
712 int tlno
, int plno
, int same
,
713 struct origin
*parent
)
716 memset(split
, 0, sizeof(struct blame_entry
[3]));
718 if (e
->s_lno
< tlno
) {
719 /* there is a pre-chunk part not blamed on parent */
720 split
[0].suspect
= origin_incref(e
->suspect
);
721 split
[0].lno
= e
->lno
;
722 split
[0].s_lno
= e
->s_lno
;
723 split
[0].num_lines
= tlno
- e
->s_lno
;
724 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
725 split
[1].s_lno
= plno
;
728 split
[1].lno
= e
->lno
;
729 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
732 if (same
< e
->s_lno
+ e
->num_lines
) {
733 /* there is a post-chunk part not blamed on parent */
734 split
[2].suspect
= origin_incref(e
->suspect
);
735 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
736 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
737 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
738 chunk_end_lno
= split
[2].lno
;
741 chunk_end_lno
= e
->lno
+ e
->num_lines
;
742 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
745 * if it turns out there is nothing to blame the parent for,
746 * forget about the splitting. !split[1].suspect signals this.
748 if (split
[1].num_lines
< 1)
750 split
[1].suspect
= origin_incref(parent
);
754 * split_overlap() divided an existing blame e into up to three parts
755 * in split. Any assigned blame is moved to queue to
758 static void split_blame(struct blame_entry
***blamed
,
759 struct blame_entry
***unblamed
,
760 struct blame_entry
*split
,
761 struct blame_entry
*e
)
763 struct blame_entry
*new_entry
;
765 if (split
[0].suspect
&& split
[2].suspect
) {
766 /* The first part (reuse storage for the existing entry e) */
767 dup_entry(unblamed
, e
, &split
[0]);
769 /* The last part -- me */
770 new_entry
= xmalloc(sizeof(*new_entry
));
771 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
772 add_blame_entry(unblamed
, new_entry
);
774 /* ... and the middle part -- parent */
775 new_entry
= xmalloc(sizeof(*new_entry
));
776 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
777 add_blame_entry(blamed
, new_entry
);
779 else if (!split
[0].suspect
&& !split
[2].suspect
)
781 * The parent covers the entire area; reuse storage for
782 * e and replace it with the parent.
784 dup_entry(blamed
, e
, &split
[1]);
785 else if (split
[0].suspect
) {
786 /* me and then parent */
787 dup_entry(unblamed
, e
, &split
[0]);
789 new_entry
= xmalloc(sizeof(*new_entry
));
790 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
791 add_blame_entry(blamed
, new_entry
);
794 /* parent and then me */
795 dup_entry(blamed
, e
, &split
[1]);
797 new_entry
= xmalloc(sizeof(*new_entry
));
798 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
799 add_blame_entry(unblamed
, new_entry
);
804 * After splitting the blame, the origins used by the
805 * on-stack blame_entry should lose one refcnt each.
807 static void decref_split(struct blame_entry
*split
)
811 for (i
= 0; i
< 3; i
++)
812 origin_decref(split
[i
].suspect
);
816 * reverse_blame reverses the list given in head, appending tail.
817 * That allows us to build lists in reverse order, then reverse them
818 * afterwards. This can be faster than building the list in proper
819 * order right away. The reason is that building in proper order
820 * requires writing a link in the _previous_ element, while building
821 * in reverse order just requires placing the list head into the
825 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
826 struct blame_entry
*tail
)
829 struct blame_entry
*next
= head
->next
;
838 * Process one hunk from the patch between the current suspect for
839 * blame_entry e and its parent. This first blames any unfinished
840 * entries before the chunk (which is where target and parent start
841 * differing) on the parent, and then splits blame entries at the
842 * start and at the end of the difference region. Since use of -M and
843 * -C options may lead to overlapping/duplicate source line number
844 * ranges, all we can rely on from sorting/merging is the order of the
845 * first suspect line number.
847 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
848 int tlno
, int offset
, int same
,
849 struct origin
*parent
)
851 struct blame_entry
*e
= **srcq
;
852 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
854 while (e
&& e
->s_lno
< tlno
) {
855 struct blame_entry
*next
= e
->next
;
857 * current record starts before differing portion. If
858 * it reaches into it, we need to split it up and
859 * examine the second part separately.
861 if (e
->s_lno
+ e
->num_lines
> tlno
) {
862 /* Move second half to a new record */
863 int len
= tlno
- e
->s_lno
;
864 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
865 n
->suspect
= e
->suspect
;
866 n
->lno
= e
->lno
+ len
;
867 n
->s_lno
= e
->s_lno
+ len
;
868 n
->num_lines
= e
->num_lines
- len
;
871 /* Push new record to diffp */
875 origin_decref(e
->suspect
);
876 /* Pass blame for everything before the differing
877 * chunk to the parent */
878 e
->suspect
= origin_incref(parent
);
885 * As we don't know how much of a common stretch after this
886 * diff will occur, the currently blamed parts are all that we
887 * can assign to the parent for now.
891 **dstq
= reverse_blame(samep
, **dstq
);
892 *dstq
= &samep
->next
;
895 * Prepend the split off portions: everything after e starts
896 * after the blameable portion.
898 e
= reverse_blame(diffp
, e
);
901 * Now retain records on the target while parts are different
906 while (e
&& e
->s_lno
< same
) {
907 struct blame_entry
*next
= e
->next
;
910 * If current record extends into sameness, need to split.
912 if (e
->s_lno
+ e
->num_lines
> same
) {
914 * Move second half to a new record to be
915 * processed by later chunks
917 int len
= same
- e
->s_lno
;
918 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
919 n
->suspect
= origin_incref(e
->suspect
);
920 n
->lno
= e
->lno
+ len
;
921 n
->s_lno
= e
->s_lno
+ len
;
922 n
->num_lines
= e
->num_lines
- len
;
925 /* Push new record to samep */
933 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
934 /* Move across elements that are in the unblamable portion */
936 *srcq
= &diffp
->next
;
939 struct blame_chunk_cb_data
{
940 struct origin
*parent
;
942 struct blame_entry
**dstq
;
943 struct blame_entry
**srcq
;
946 /* diff chunks are from parent to target */
947 static int blame_chunk_cb(long start_a
, long count_a
,
948 long start_b
, long count_b
, void *data
)
950 struct blame_chunk_cb_data
*d
= data
;
951 if (start_a
- start_b
!= d
->offset
)
952 die("internal error in blame::blame_chunk_cb");
953 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
954 start_b
+ count_b
, d
->parent
);
955 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
960 * We are looking at the origin 'target' and aiming to pass blame
961 * for the lines it is suspected to its parent. Run diff to find
962 * which lines came from parent and pass blame for them.
964 static void pass_blame_to_parent(struct scoreboard
*sb
,
965 struct origin
*target
,
966 struct origin
*parent
)
968 mmfile_t file_p
, file_o
;
969 struct blame_chunk_cb_data d
;
970 struct blame_entry
*newdest
= NULL
;
972 if (!target
->suspects
)
973 return; /* nothing remains for this target */
977 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
979 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
980 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
);
983 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
))
984 die("unable to generate diff (%s -> %s)",
985 oid_to_hex(&parent
->commit
->object
.oid
),
986 oid_to_hex(&target
->commit
->object
.oid
));
987 /* The rest are the same as the parent */
988 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
990 queue_blames(sb
, parent
, newdest
);
996 * The lines in blame_entry after splitting blames many times can become
997 * very small and trivial, and at some point it becomes pointless to
998 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
999 * ordinary C program, and it is not worth to say it was copied from
1000 * totally unrelated file in the parent.
1002 * Compute how trivial the lines in the blame_entry are.
1004 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
1007 const char *cp
, *ep
;
1013 cp
= nth_line(sb
, e
->lno
);
1014 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
1016 unsigned ch
= *((unsigned char *)cp
);
1026 * best_so_far[] and this[] are both a split of an existing blame_entry
1027 * that passes blame to the parent. Maintain best_so_far the best split
1028 * so far, by comparing this and best_so_far and copying this into
1029 * bst_so_far as needed.
1031 static void copy_split_if_better(struct scoreboard
*sb
,
1032 struct blame_entry
*best_so_far
,
1033 struct blame_entry
*this)
1037 if (!this[1].suspect
)
1039 if (best_so_far
[1].suspect
) {
1040 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
1044 for (i
= 0; i
< 3; i
++)
1045 origin_incref(this[i
].suspect
);
1046 decref_split(best_so_far
);
1047 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
1051 * We are looking at a part of the final image represented by
1052 * ent (tlno and same are offset by ent->s_lno).
1053 * tlno is where we are looking at in the final image.
1054 * up to (but not including) same match preimage.
1055 * plno is where we are looking at in the preimage.
1057 * <-------------- final image ---------------------->
1060 * <---------preimage----->
1063 * All line numbers are 0-based.
1065 static void handle_split(struct scoreboard
*sb
,
1066 struct blame_entry
*ent
,
1067 int tlno
, int plno
, int same
,
1068 struct origin
*parent
,
1069 struct blame_entry
*split
)
1071 if (ent
->num_lines
<= tlno
)
1074 struct blame_entry
this[3];
1077 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
1078 copy_split_if_better(sb
, split
, this);
1083 struct handle_split_cb_data
{
1084 struct scoreboard
*sb
;
1085 struct blame_entry
*ent
;
1086 struct origin
*parent
;
1087 struct blame_entry
*split
;
1092 static int handle_split_cb(long start_a
, long count_a
,
1093 long start_b
, long count_b
, void *data
)
1095 struct handle_split_cb_data
*d
= data
;
1096 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1098 d
->plno
= start_a
+ count_a
;
1099 d
->tlno
= start_b
+ count_b
;
1104 * Find the lines from parent that are the same as ent so that
1105 * we can pass blames to it. file_p has the blob contents for
1108 static void find_copy_in_blob(struct scoreboard
*sb
,
1109 struct blame_entry
*ent
,
1110 struct origin
*parent
,
1111 struct blame_entry
*split
,
1116 struct handle_split_cb_data d
;
1118 memset(&d
, 0, sizeof(d
));
1119 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1121 * Prepare mmfile that contains only the lines in ent.
1123 cp
= nth_line(sb
, ent
->lno
);
1124 file_o
.ptr
= (char *) cp
;
1125 file_o
.size
= nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1128 * file_o is a part of final image we are annotating.
1129 * file_p partially may match that image.
1131 memset(split
, 0, sizeof(struct blame_entry
[3]));
1132 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
))
1133 die("unable to generate diff (%s)",
1134 oid_to_hex(&parent
->commit
->object
.oid
));
1135 /* remainder, if any, all match the preimage */
1136 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1139 /* Move all blame entries from list *source that have a score smaller
1140 * than score_min to the front of list *small.
1141 * Returns a pointer to the link pointing to the old head of the small list.
1144 static struct blame_entry
**filter_small(struct scoreboard
*sb
,
1145 struct blame_entry
**small
,
1146 struct blame_entry
**source
,
1149 struct blame_entry
*p
= *source
;
1150 struct blame_entry
*oldsmall
= *small
;
1152 if (ent_score(sb
, p
) <= score_min
) {
1168 * See if lines currently target is suspected for can be attributed to
1171 static void find_move_in_parent(struct scoreboard
*sb
,
1172 struct blame_entry
***blamed
,
1173 struct blame_entry
**toosmall
,
1174 struct origin
*target
,
1175 struct origin
*parent
)
1177 struct blame_entry
*e
, split
[3];
1178 struct blame_entry
*unblamed
= target
->suspects
;
1179 struct blame_entry
*leftover
= NULL
;
1183 return; /* nothing remains for this target */
1185 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
);
1189 /* At each iteration, unblamed has a NULL-terminated list of
1190 * entries that have not yet been tested for blame. leftover
1191 * contains the reversed list of entries that have been tested
1192 * without being assignable to the parent.
1195 struct blame_entry
**unblamedtail
= &unblamed
;
1196 struct blame_entry
*next
;
1197 for (e
= unblamed
; e
; e
= next
) {
1199 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1200 if (split
[1].suspect
&&
1201 blame_move_score
< ent_score(sb
, &split
[1])) {
1202 split_blame(blamed
, &unblamedtail
, split
, e
);
1207 decref_split(split
);
1209 *unblamedtail
= NULL
;
1210 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_move_score
);
1212 target
->suspects
= reverse_blame(leftover
, NULL
);
1216 struct blame_entry
*ent
;
1217 struct blame_entry split
[3];
1221 * Count the number of entries the target is suspected for,
1222 * and prepare a list of entry and the best split.
1224 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1227 struct blame_entry
*e
;
1229 struct blame_list
*blame_list
= NULL
;
1231 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1234 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1235 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1236 blame_list
[i
++].ent
= e
;
1238 *num_ents_p
= num_ents
;
1243 * For lines target is suspected for, see if we can find code movement
1244 * across file boundary from the parent commit. porigin is the path
1245 * in the parent we already tried.
1247 static void find_copy_in_parent(struct scoreboard
*sb
,
1248 struct blame_entry
***blamed
,
1249 struct blame_entry
**toosmall
,
1250 struct origin
*target
,
1251 struct commit
*parent
,
1252 struct origin
*porigin
,
1255 struct diff_options diff_opts
;
1257 struct blame_list
*blame_list
;
1259 struct blame_entry
*unblamed
= target
->suspects
;
1260 struct blame_entry
*leftover
= NULL
;
1263 return; /* nothing remains for this target */
1265 diff_setup(&diff_opts
);
1266 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1267 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1269 diff_setup_done(&diff_opts
);
1271 /* Try "find copies harder" on new path if requested;
1272 * we do not want to use diffcore_rename() actually to
1273 * match things up; find_copies_harder is set only to
1274 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1275 * and this code needs to be after diff_setup_done(), which
1276 * usually makes find-copies-harder imply copy detection.
1278 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1279 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1280 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1281 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1283 if (is_null_oid(&target
->commit
->object
.oid
))
1284 do_diff_cache(parent
->tree
->object
.oid
.hash
, &diff_opts
);
1286 diff_tree_sha1(parent
->tree
->object
.oid
.hash
,
1287 target
->commit
->tree
->object
.oid
.hash
,
1290 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1291 diffcore_std(&diff_opts
);
1294 struct blame_entry
**unblamedtail
= &unblamed
;
1295 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1297 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1298 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1299 struct origin
*norigin
;
1301 struct blame_entry
this[3];
1303 if (!DIFF_FILE_VALID(p
->one
))
1304 continue; /* does not exist in parent */
1305 if (S_ISGITLINK(p
->one
->mode
))
1306 continue; /* ignore git links */
1307 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1308 /* find_move already dealt with this path */
1311 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1312 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
1313 norigin
->mode
= p
->one
->mode
;
1314 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
);
1318 for (j
= 0; j
< num_ents
; j
++) {
1319 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1320 norigin
, this, &file_p
);
1321 copy_split_if_better(sb
, blame_list
[j
].split
,
1325 origin_decref(norigin
);
1328 for (j
= 0; j
< num_ents
; j
++) {
1329 struct blame_entry
*split
= blame_list
[j
].split
;
1330 if (split
[1].suspect
&&
1331 blame_copy_score
< ent_score(sb
, &split
[1])) {
1332 split_blame(blamed
, &unblamedtail
, split
,
1335 blame_list
[j
].ent
->next
= leftover
;
1336 leftover
= blame_list
[j
].ent
;
1338 decref_split(split
);
1341 *unblamedtail
= NULL
;
1342 toosmall
= filter_small(sb
, toosmall
, &unblamed
, blame_copy_score
);
1344 target
->suspects
= reverse_blame(leftover
, NULL
);
1345 diff_flush(&diff_opts
);
1346 clear_pathspec(&diff_opts
.pathspec
);
1350 * The blobs of origin and porigin exactly match, so everything
1351 * origin is suspected for can be blamed on the parent.
1353 static void pass_whole_blame(struct scoreboard
*sb
,
1354 struct origin
*origin
, struct origin
*porigin
)
1356 struct blame_entry
*e
, *suspects
;
1358 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1359 /* Steal its file */
1360 porigin
->file
= origin
->file
;
1361 origin
->file
.ptr
= NULL
;
1363 suspects
= origin
->suspects
;
1364 origin
->suspects
= NULL
;
1365 for (e
= suspects
; e
; e
= e
->next
) {
1366 origin_incref(porigin
);
1367 origin_decref(e
->suspect
);
1368 e
->suspect
= porigin
;
1370 queue_blames(sb
, porigin
, suspects
);
1374 * We pass blame from the current commit to its parents. We keep saying
1375 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1376 * exonerate ourselves.
1378 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1381 if (revs
->first_parent_only
&&
1383 commit
->parents
->next
) {
1384 free_commit_list(commit
->parents
->next
);
1385 commit
->parents
->next
= NULL
;
1387 return commit
->parents
;
1389 return lookup_decoration(&revs
->children
, &commit
->object
);
1392 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1394 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1395 return commit_list_count(l
);
1398 /* Distribute collected unsorted blames to the respected sorted lists
1399 * in the various origins.
1401 static void distribute_blame(struct scoreboard
*sb
, struct blame_entry
*blamed
)
1403 blamed
= blame_sort(blamed
, compare_blame_suspect
);
1406 struct origin
*porigin
= blamed
->suspect
;
1407 struct blame_entry
*suspects
= NULL
;
1409 struct blame_entry
*next
= blamed
->next
;
1410 blamed
->next
= suspects
;
1413 } while (blamed
&& blamed
->suspect
== porigin
);
1414 suspects
= reverse_blame(suspects
, NULL
);
1415 queue_blames(sb
, porigin
, suspects
);
1421 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1423 struct rev_info
*revs
= sb
->revs
;
1424 int i
, pass
, num_sg
;
1425 struct commit
*commit
= origin
->commit
;
1426 struct commit_list
*sg
;
1427 struct origin
*sg_buf
[MAXSG
];
1428 struct origin
*porigin
, **sg_origin
= sg_buf
;
1429 struct blame_entry
*toosmall
= NULL
;
1430 struct blame_entry
*blames
, **blametail
= &blames
;
1432 num_sg
= num_scapegoats(revs
, commit
);
1435 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1436 memset(sg_buf
, 0, sizeof(sg_buf
));
1438 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1441 * The first pass looks for unrenamed path to optimize for
1442 * common cases, then we look for renames in the second pass.
1444 for (pass
= 0; pass
< 2 - no_whole_file_rename
; pass
++) {
1445 struct origin
*(*find
)(struct scoreboard
*,
1446 struct commit
*, struct origin
*);
1447 find
= pass
? find_rename
: find_origin
;
1449 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1451 sg
= sg
->next
, i
++) {
1452 struct commit
*p
= sg
->item
;
1457 if (parse_commit(p
))
1459 porigin
= find(sb
, p
, origin
);
1462 if (!oidcmp(&porigin
->blob_oid
, &origin
->blob_oid
)) {
1463 pass_whole_blame(sb
, origin
, porigin
);
1464 origin_decref(porigin
);
1467 for (j
= same
= 0; j
< i
; j
++)
1469 !oidcmp(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
1474 sg_origin
[i
] = porigin
;
1476 origin_decref(porigin
);
1481 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1483 sg
= sg
->next
, i
++) {
1484 struct origin
*porigin
= sg_origin
[i
];
1487 if (!origin
->previous
) {
1488 origin_incref(porigin
);
1489 origin
->previous
= porigin
;
1491 pass_blame_to_parent(sb
, origin
, porigin
);
1492 if (!origin
->suspects
)
1497 * Optionally find moves in parents' files.
1499 if (opt
& PICKAXE_BLAME_MOVE
) {
1500 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_move_score
);
1501 if (origin
->suspects
) {
1502 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1504 sg
= sg
->next
, i
++) {
1505 struct origin
*porigin
= sg_origin
[i
];
1508 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1509 if (!origin
->suspects
)
1516 * Optionally find copies from parents' files.
1518 if (opt
& PICKAXE_BLAME_COPY
) {
1519 if (blame_copy_score
> blame_move_score
)
1520 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1521 else if (blame_copy_score
< blame_move_score
) {
1522 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1524 filter_small(sb
, &toosmall
, &origin
->suspects
, blame_copy_score
);
1526 if (!origin
->suspects
)
1529 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1531 sg
= sg
->next
, i
++) {
1532 struct origin
*porigin
= sg_origin
[i
];
1533 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1534 origin
, sg
->item
, porigin
, opt
);
1535 if (!origin
->suspects
)
1542 distribute_blame(sb
, blames
);
1544 * prepend toosmall to origin->suspects
1546 * There is no point in sorting: this ends up on a big
1547 * unsorted list in the caller anyway.
1550 struct blame_entry
**tail
= &toosmall
;
1552 tail
= &(*tail
)->next
;
1553 *tail
= origin
->suspects
;
1554 origin
->suspects
= toosmall
;
1556 for (i
= 0; i
< num_sg
; i
++) {
1558 drop_origin_blob(sg_origin
[i
]);
1559 origin_decref(sg_origin
[i
]);
1562 drop_origin_blob(origin
);
1563 if (sg_buf
!= sg_origin
)
1568 * Information on commits, used for output.
1570 struct commit_info
{
1571 struct strbuf author
;
1572 struct strbuf author_mail
;
1573 unsigned long author_time
;
1574 struct strbuf author_tz
;
1576 /* filled only when asked for details */
1577 struct strbuf committer
;
1578 struct strbuf committer_mail
;
1579 unsigned long committer_time
;
1580 struct strbuf committer_tz
;
1582 struct strbuf summary
;
1586 * Parse author/committer line in the commit object buffer
1588 static void get_ac_line(const char *inbuf
, const char *what
,
1589 struct strbuf
*name
, struct strbuf
*mail
,
1590 unsigned long *time
, struct strbuf
*tz
)
1592 struct ident_split ident
;
1593 size_t len
, maillen
, namelen
;
1595 const char *namebuf
, *mailbuf
;
1597 tmp
= strstr(inbuf
, what
);
1600 tmp
+= strlen(what
);
1601 endp
= strchr(tmp
, '\n');
1607 if (split_ident_line(&ident
, tmp
, len
)) {
1611 strbuf_addstr(name
, tmp
);
1612 strbuf_addstr(mail
, tmp
);
1613 strbuf_addstr(tz
, tmp
);
1618 namelen
= ident
.name_end
- ident
.name_begin
;
1619 namebuf
= ident
.name_begin
;
1621 maillen
= ident
.mail_end
- ident
.mail_begin
;
1622 mailbuf
= ident
.mail_begin
;
1624 if (ident
.date_begin
&& ident
.date_end
)
1625 *time
= strtoul(ident
.date_begin
, NULL
, 10);
1629 if (ident
.tz_begin
&& ident
.tz_end
)
1630 strbuf_add(tz
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
1632 strbuf_addstr(tz
, "(unknown)");
1635 * Now, convert both name and e-mail using mailmap
1637 map_user(&mailmap
, &mailbuf
, &maillen
,
1638 &namebuf
, &namelen
);
1640 strbuf_addf(mail
, "<%.*s>", (int)maillen
, mailbuf
);
1641 strbuf_add(name
, namebuf
, namelen
);
1644 static void commit_info_init(struct commit_info
*ci
)
1647 strbuf_init(&ci
->author
, 0);
1648 strbuf_init(&ci
->author_mail
, 0);
1649 strbuf_init(&ci
->author_tz
, 0);
1650 strbuf_init(&ci
->committer
, 0);
1651 strbuf_init(&ci
->committer_mail
, 0);
1652 strbuf_init(&ci
->committer_tz
, 0);
1653 strbuf_init(&ci
->summary
, 0);
1656 static void commit_info_destroy(struct commit_info
*ci
)
1659 strbuf_release(&ci
->author
);
1660 strbuf_release(&ci
->author_mail
);
1661 strbuf_release(&ci
->author_tz
);
1662 strbuf_release(&ci
->committer
);
1663 strbuf_release(&ci
->committer_mail
);
1664 strbuf_release(&ci
->committer_tz
);
1665 strbuf_release(&ci
->summary
);
1668 static void get_commit_info(struct commit
*commit
,
1669 struct commit_info
*ret
,
1673 const char *subject
, *encoding
;
1674 const char *message
;
1676 commit_info_init(ret
);
1678 encoding
= get_log_output_encoding();
1679 message
= logmsg_reencode(commit
, NULL
, encoding
);
1680 get_ac_line(message
, "\nauthor ",
1681 &ret
->author
, &ret
->author_mail
,
1682 &ret
->author_time
, &ret
->author_tz
);
1685 unuse_commit_buffer(commit
, message
);
1689 get_ac_line(message
, "\ncommitter ",
1690 &ret
->committer
, &ret
->committer_mail
,
1691 &ret
->committer_time
, &ret
->committer_tz
);
1693 len
= find_commit_subject(message
, &subject
);
1695 strbuf_add(&ret
->summary
, subject
, len
);
1697 strbuf_addf(&ret
->summary
, "(%s)", oid_to_hex(&commit
->object
.oid
));
1699 unuse_commit_buffer(commit
, message
);
1703 * To allow LF and other nonportable characters in pathnames,
1704 * they are c-style quoted as needed.
1706 static void write_filename_info(const char *path
)
1708 printf("filename ");
1709 write_name_quoted(path
, stdout
, '\n');
1713 * Porcelain/Incremental format wants to show a lot of details per
1714 * commit. Instead of repeating this every line, emit it only once,
1715 * the first time each commit appears in the output (unless the
1716 * user has specifically asked for us to repeat).
1718 static int emit_one_suspect_detail(struct origin
*suspect
, int repeat
)
1720 struct commit_info ci
;
1722 if (!repeat
&& (suspect
->commit
->object
.flags
& METAINFO_SHOWN
))
1725 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1726 get_commit_info(suspect
->commit
, &ci
, 1);
1727 printf("author %s\n", ci
.author
.buf
);
1728 printf("author-mail %s\n", ci
.author_mail
.buf
);
1729 printf("author-time %lu\n", ci
.author_time
);
1730 printf("author-tz %s\n", ci
.author_tz
.buf
);
1731 printf("committer %s\n", ci
.committer
.buf
);
1732 printf("committer-mail %s\n", ci
.committer_mail
.buf
);
1733 printf("committer-time %lu\n", ci
.committer_time
);
1734 printf("committer-tz %s\n", ci
.committer_tz
.buf
);
1735 printf("summary %s\n", ci
.summary
.buf
);
1736 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1737 printf("boundary\n");
1738 if (suspect
->previous
) {
1739 struct origin
*prev
= suspect
->previous
;
1740 printf("previous %s ", oid_to_hex(&prev
->commit
->object
.oid
));
1741 write_name_quoted(prev
->path
, stdout
, '\n');
1744 commit_info_destroy(&ci
);
1750 * The blame_entry is found to be guilty for the range.
1751 * Show it in incremental output.
1753 static void found_guilty_entry(struct blame_entry
*ent
,
1754 struct progress_info
*pi
)
1757 struct origin
*suspect
= ent
->suspect
;
1759 printf("%s %d %d %d\n",
1760 oid_to_hex(&suspect
->commit
->object
.oid
),
1761 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1762 emit_one_suspect_detail(suspect
, 0);
1763 write_filename_info(suspect
->path
);
1764 maybe_flush_or_die(stdout
, "stdout");
1766 pi
->blamed_lines
+= ent
->num_lines
;
1767 display_progress(pi
->progress
, pi
->blamed_lines
);
1771 * The main loop -- while we have blobs with lines whose true origin
1772 * is still unknown, pick one blob, and allow its lines to pass blames
1773 * to its parents. */
1774 static void assign_blame(struct scoreboard
*sb
, int opt
)
1776 struct rev_info
*revs
= sb
->revs
;
1777 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1778 struct progress_info pi
= { NULL
, 0 };
1781 pi
.progress
= start_progress_delay(_("Blaming lines"),
1782 sb
->num_lines
, 50, 1);
1785 struct blame_entry
*ent
;
1786 struct origin
*suspect
= commit
->util
;
1788 /* find one suspect to break down */
1789 while (suspect
&& !suspect
->suspects
)
1790 suspect
= suspect
->next
;
1793 commit
= prio_queue_get(&sb
->commits
);
1797 assert(commit
== suspect
->commit
);
1800 * We will use this suspect later in the loop,
1801 * so hold onto it in the meantime.
1803 origin_incref(suspect
);
1804 parse_commit(commit
);
1806 (!(commit
->object
.flags
& UNINTERESTING
) &&
1807 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1808 pass_blame(sb
, suspect
, opt
);
1810 commit
->object
.flags
|= UNINTERESTING
;
1811 if (commit
->object
.parsed
)
1812 mark_parents_uninteresting(commit
);
1814 /* treat root commit as boundary */
1815 if (!commit
->parents
&& !show_root
)
1816 commit
->object
.flags
|= UNINTERESTING
;
1818 /* Take responsibility for the remaining entries */
1819 ent
= suspect
->suspects
;
1821 suspect
->guilty
= 1;
1823 struct blame_entry
*next
= ent
->next
;
1824 found_guilty_entry(ent
, &pi
);
1829 ent
->next
= sb
->ent
;
1830 sb
->ent
= suspect
->suspects
;
1831 suspect
->suspects
= NULL
;
1835 origin_decref(suspect
);
1837 if (DEBUG
) /* sanity */
1838 sanity_check_refcnt(sb
);
1841 stop_progress(&pi
.progress
);
1844 static const char *format_time(unsigned long time
, const char *tz_str
,
1847 static struct strbuf time_buf
= STRBUF_INIT
;
1849 strbuf_reset(&time_buf
);
1850 if (show_raw_time
) {
1851 strbuf_addf(&time_buf
, "%lu %s", time
, tz_str
);
1854 const char *time_str
;
1858 time_str
= show_date(time
, tz
, &blame_date_mode
);
1859 strbuf_addstr(&time_buf
, time_str
);
1861 * Add space paddings to time_buf to display a fixed width
1862 * string, and use time_width for display width calibration.
1864 for (time_width
= utf8_strwidth(time_str
);
1865 time_width
< blame_date_width
;
1867 strbuf_addch(&time_buf
, ' ');
1869 return time_buf
.buf
;
1872 #define OUTPUT_ANNOTATE_COMPAT 001
1873 #define OUTPUT_LONG_OBJECT_NAME 002
1874 #define OUTPUT_RAW_TIMESTAMP 004
1875 #define OUTPUT_PORCELAIN 010
1876 #define OUTPUT_SHOW_NAME 020
1877 #define OUTPUT_SHOW_NUMBER 040
1878 #define OUTPUT_SHOW_SCORE 0100
1879 #define OUTPUT_NO_AUTHOR 0200
1880 #define OUTPUT_SHOW_EMAIL 0400
1881 #define OUTPUT_LINE_PORCELAIN 01000
1883 static void emit_porcelain_details(struct origin
*suspect
, int repeat
)
1885 if (emit_one_suspect_detail(suspect
, repeat
) ||
1886 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
1887 write_filename_info(suspect
->path
);
1890 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
,
1893 int repeat
= opt
& OUTPUT_LINE_PORCELAIN
;
1896 struct origin
*suspect
= ent
->suspect
;
1897 char hex
[GIT_SHA1_HEXSZ
+ 1];
1899 sha1_to_hex_r(hex
, suspect
->commit
->object
.oid
.hash
);
1900 printf("%s %d %d %d\n",
1905 emit_porcelain_details(suspect
, repeat
);
1907 cp
= nth_line(sb
, ent
->lno
);
1908 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1911 printf("%s %d %d\n", hex
,
1912 ent
->s_lno
+ 1 + cnt
,
1913 ent
->lno
+ 1 + cnt
);
1915 emit_porcelain_details(suspect
, 1);
1921 } while (ch
!= '\n' &&
1922 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1925 if (sb
->final_buf_size
&& cp
[-1] != '\n')
1929 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1933 struct origin
*suspect
= ent
->suspect
;
1934 struct commit_info ci
;
1935 char hex
[GIT_SHA1_HEXSZ
+ 1];
1936 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1938 get_commit_info(suspect
->commit
, &ci
, 1);
1939 sha1_to_hex_r(hex
, suspect
->commit
->object
.oid
.hash
);
1941 cp
= nth_line(sb
, ent
->lno
);
1942 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1944 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? GIT_SHA1_HEXSZ
: abbrev
;
1946 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1948 memset(hex
, ' ', length
);
1949 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
1955 printf("%.*s", length
, hex
);
1956 if (opt
& OUTPUT_ANNOTATE_COMPAT
) {
1958 if (opt
& OUTPUT_SHOW_EMAIL
)
1959 name
= ci
.author_mail
.buf
;
1961 name
= ci
.author
.buf
;
1962 printf("\t(%10s\t%10s\t%d)", name
,
1963 format_time(ci
.author_time
, ci
.author_tz
.buf
,
1965 ent
->lno
+ 1 + cnt
);
1967 if (opt
& OUTPUT_SHOW_SCORE
)
1969 max_score_digits
, ent
->score
,
1970 ent
->suspect
->refcnt
);
1971 if (opt
& OUTPUT_SHOW_NAME
)
1972 printf(" %-*.*s", longest_file
, longest_file
,
1974 if (opt
& OUTPUT_SHOW_NUMBER
)
1975 printf(" %*d", max_orig_digits
,
1976 ent
->s_lno
+ 1 + cnt
);
1978 if (!(opt
& OUTPUT_NO_AUTHOR
)) {
1981 if (opt
& OUTPUT_SHOW_EMAIL
)
1982 name
= ci
.author_mail
.buf
;
1984 name
= ci
.author
.buf
;
1985 pad
= longest_author
- utf8_strwidth(name
);
1986 printf(" (%s%*s %10s",
1988 format_time(ci
.author_time
,
1993 max_digits
, ent
->lno
+ 1 + cnt
);
1998 } while (ch
!= '\n' &&
1999 cp
< sb
->final_buf
+ sb
->final_buf_size
);
2002 if (sb
->final_buf_size
&& cp
[-1] != '\n')
2005 commit_info_destroy(&ci
);
2008 static void output(struct scoreboard
*sb
, int option
)
2010 struct blame_entry
*ent
;
2012 if (option
& OUTPUT_PORCELAIN
) {
2013 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2015 struct origin
*suspect
;
2016 struct commit
*commit
= ent
->suspect
->commit
;
2017 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
2019 for (suspect
= commit
->util
; suspect
; suspect
= suspect
->next
) {
2020 if (suspect
->guilty
&& count
++) {
2021 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
2028 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2029 if (option
& OUTPUT_PORCELAIN
)
2030 emit_porcelain(sb
, ent
, option
);
2032 emit_other(sb
, ent
, option
);
2037 static const char *get_next_line(const char *start
, const char *end
)
2039 const char *nl
= memchr(start
, '\n', end
- start
);
2040 return nl
? nl
+ 1 : end
;
2044 * To allow quick access to the contents of nth line in the
2045 * final image, prepare an index in the scoreboard.
2047 static int prepare_lines(struct scoreboard
*sb
)
2049 const char *buf
= sb
->final_buf
;
2050 unsigned long len
= sb
->final_buf_size
;
2051 const char *end
= buf
+ len
;
2056 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2059 ALLOC_ARRAY(sb
->lineno
, num
+ 1);
2060 lineno
= sb
->lineno
;
2062 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
2063 *lineno
++ = p
- buf
;
2067 sb
->num_lines
= num
;
2068 return sb
->num_lines
;
2072 * Add phony grafts for use with -S; this is primarily to
2073 * support git's cvsserver that wants to give a linear history
2076 static int read_ancestry(const char *graft_file
)
2078 FILE *fp
= fopen(graft_file
, "r");
2079 struct strbuf buf
= STRBUF_INIT
;
2082 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
2083 /* The format is just "Commit Parent1 Parent2 ...\n" */
2084 struct commit_graft
*graft
= read_graft_line(buf
.buf
, buf
.len
);
2086 register_commit_graft(graft
, 0);
2089 strbuf_release(&buf
);
2093 static int update_auto_abbrev(int auto_abbrev
, struct origin
*suspect
)
2095 const char *uniq
= find_unique_abbrev(suspect
->commit
->object
.oid
.hash
,
2097 int len
= strlen(uniq
);
2098 if (auto_abbrev
< len
)
2104 * How many columns do we need to show line numbers, authors,
2107 static void find_alignment(struct scoreboard
*sb
, int *option
)
2109 int longest_src_lines
= 0;
2110 int longest_dst_lines
= 0;
2111 unsigned largest_score
= 0;
2112 struct blame_entry
*e
;
2113 int compute_auto_abbrev
= (abbrev
< 0);
2114 int auto_abbrev
= default_abbrev
;
2116 for (e
= sb
->ent
; e
; e
= e
->next
) {
2117 struct origin
*suspect
= e
->suspect
;
2120 if (compute_auto_abbrev
)
2121 auto_abbrev
= update_auto_abbrev(auto_abbrev
, suspect
);
2122 if (strcmp(suspect
->path
, sb
->path
))
2123 *option
|= OUTPUT_SHOW_NAME
;
2124 num
= strlen(suspect
->path
);
2125 if (longest_file
< num
)
2127 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
2128 struct commit_info ci
;
2129 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
2130 get_commit_info(suspect
->commit
, &ci
, 1);
2131 if (*option
& OUTPUT_SHOW_EMAIL
)
2132 num
= utf8_strwidth(ci
.author_mail
.buf
);
2134 num
= utf8_strwidth(ci
.author
.buf
);
2135 if (longest_author
< num
)
2136 longest_author
= num
;
2137 commit_info_destroy(&ci
);
2139 num
= e
->s_lno
+ e
->num_lines
;
2140 if (longest_src_lines
< num
)
2141 longest_src_lines
= num
;
2142 num
= e
->lno
+ e
->num_lines
;
2143 if (longest_dst_lines
< num
)
2144 longest_dst_lines
= num
;
2145 if (largest_score
< ent_score(sb
, e
))
2146 largest_score
= ent_score(sb
, e
);
2148 max_orig_digits
= decimal_width(longest_src_lines
);
2149 max_digits
= decimal_width(longest_dst_lines
);
2150 max_score_digits
= decimal_width(largest_score
);
2152 if (compute_auto_abbrev
)
2153 /* one more abbrev length is needed for the boundary commit */
2154 abbrev
= auto_abbrev
+ 1;
2158 * For debugging -- origin is refcounted, and this asserts that
2159 * we do not underflow.
2161 static void sanity_check_refcnt(struct scoreboard
*sb
)
2164 struct blame_entry
*ent
;
2166 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
2167 /* Nobody should have zero or negative refcnt */
2168 if (ent
->suspect
->refcnt
<= 0) {
2169 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
2171 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
2172 ent
->suspect
->refcnt
);
2178 find_alignment(sb
, &opt
);
2180 die("Baa %d!", baa
);
2184 static unsigned parse_score(const char *arg
)
2187 unsigned long score
= strtoul(arg
, &end
, 10);
2193 static const char *add_prefix(const char *prefix
, const char *path
)
2195 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
2198 static int git_blame_config(const char *var
, const char *value
, void *cb
)
2200 if (!strcmp(var
, "blame.showroot")) {
2201 show_root
= git_config_bool(var
, value
);
2204 if (!strcmp(var
, "blame.blankboundary")) {
2205 blank_boundary
= git_config_bool(var
, value
);
2208 if (!strcmp(var
, "blame.showemail")) {
2209 int *output_option
= cb
;
2210 if (git_config_bool(var
, value
))
2211 *output_option
|= OUTPUT_SHOW_EMAIL
;
2213 *output_option
&= ~OUTPUT_SHOW_EMAIL
;
2216 if (!strcmp(var
, "blame.date")) {
2218 return config_error_nonbool(var
);
2219 parse_date_format(value
, &blame_date_mode
);
2223 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
2225 if (userdiff_config(var
, value
) < 0)
2228 return git_default_config(var
, value
, cb
);
2231 static void verify_working_tree_path(struct commit
*work_tree
, const char *path
)
2233 struct commit_list
*parents
;
2236 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
2237 const struct object_id
*commit_oid
= &parents
->item
->object
.oid
;
2238 struct object_id blob_oid
;
2241 if (!get_tree_entry(commit_oid
->hash
, path
, blob_oid
.hash
, &mode
) &&
2242 sha1_object_info(blob_oid
.hash
, NULL
) == OBJ_BLOB
)
2246 pos
= cache_name_pos(path
, strlen(path
));
2248 ; /* path is in the index */
2249 else if (-1 - pos
< active_nr
&&
2250 !strcmp(active_cache
[-1 - pos
]->name
, path
))
2251 ; /* path is in the index, unmerged */
2253 die("no such path '%s' in HEAD", path
);
2256 static struct commit_list
**append_parent(struct commit_list
**tail
, const struct object_id
*oid
)
2258 struct commit
*parent
;
2260 parent
= lookup_commit_reference(oid
->hash
);
2262 die("no such commit %s", oid_to_hex(oid
));
2263 return &commit_list_insert(parent
, tail
)->next
;
2266 static void append_merge_parents(struct commit_list
**tail
)
2269 struct strbuf line
= STRBUF_INIT
;
2271 merge_head
= open(git_path_merge_head(), O_RDONLY
);
2272 if (merge_head
< 0) {
2273 if (errno
== ENOENT
)
2275 die("cannot open '%s' for reading", git_path_merge_head());
2278 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
2279 struct object_id oid
;
2280 if (line
.len
< GIT_SHA1_HEXSZ
|| get_oid_hex(line
.buf
, &oid
))
2281 die("unknown line in '%s': %s", git_path_merge_head(), line
.buf
);
2282 tail
= append_parent(tail
, &oid
);
2285 strbuf_release(&line
);
2289 * This isn't as simple as passing sb->buf and sb->len, because we
2290 * want to transfer ownership of the buffer to the commit (so we
2293 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
2296 void *buf
= strbuf_detach(sb
, &len
);
2297 set_commit_buffer(c
, buf
, len
);
2301 * Prepare a dummy commit that represents the work tree (or staged) item.
2302 * Note that annotating work tree item never works in the reverse.
2304 static struct commit
*fake_working_tree_commit(struct diff_options
*opt
,
2306 const char *contents_from
)
2308 struct commit
*commit
;
2309 struct origin
*origin
;
2310 struct commit_list
**parent_tail
, *parent
;
2311 struct object_id head_oid
;
2312 struct strbuf buf
= STRBUF_INIT
;
2316 struct cache_entry
*ce
;
2318 struct strbuf msg
= STRBUF_INIT
;
2322 commit
= alloc_commit_node();
2323 commit
->object
.parsed
= 1;
2325 parent_tail
= &commit
->parents
;
2327 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_oid
.hash
, NULL
))
2328 die("no such ref: HEAD");
2330 parent_tail
= append_parent(parent_tail
, &head_oid
);
2331 append_merge_parents(parent_tail
);
2332 verify_working_tree_path(commit
, path
);
2334 origin
= make_origin(commit
, path
);
2336 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2337 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
2338 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
2339 strbuf_addf(&msg
, "parent %s\n",
2340 oid_to_hex(&parent
->item
->object
.oid
));
2344 "Version of %s from %s\n",
2346 (!contents_from
? path
:
2347 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
2348 set_commit_buffer_from_strbuf(commit
, &msg
);
2350 if (!contents_from
|| strcmp("-", contents_from
)) {
2352 const char *read_from
;
2354 unsigned long buf_len
;
2356 if (contents_from
) {
2357 if (stat(contents_from
, &st
) < 0)
2358 die_errno("Cannot stat '%s'", contents_from
);
2359 read_from
= contents_from
;
2362 if (lstat(path
, &st
) < 0)
2363 die_errno("Cannot lstat '%s'", path
);
2366 mode
= canon_mode(st
.st_mode
);
2368 switch (st
.st_mode
& S_IFMT
) {
2370 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
2371 textconv_object(read_from
, mode
, &null_oid
, 0, &buf_ptr
, &buf_len
))
2372 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
2373 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2374 die_errno("cannot open or read '%s'", read_from
);
2377 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
2378 die_errno("cannot readlink '%s'", read_from
);
2381 die("unsupported file type %s", read_from
);
2385 /* Reading from stdin */
2387 if (strbuf_read(&buf
, 0, 0) < 0)
2388 die_errno("failed to read from stdin");
2390 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2391 origin
->file
.ptr
= buf
.buf
;
2392 origin
->file
.size
= buf
.len
;
2393 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_oid
.hash
);
2396 * Read the current index, replace the path entry with
2397 * origin->blob_sha1 without mucking with its mode or type
2398 * bits; we are not going to write this index out -- we just
2399 * want to run "diff-index --cached".
2406 int pos
= cache_name_pos(path
, len
);
2408 mode
= active_cache
[pos
]->ce_mode
;
2410 /* Let's not bother reading from HEAD tree */
2411 mode
= S_IFREG
| 0644;
2413 size
= cache_entry_size(len
);
2414 ce
= xcalloc(1, size
);
2415 oidcpy(&ce
->oid
, &origin
->blob_oid
);
2416 memcpy(ce
->name
, path
, len
);
2417 ce
->ce_flags
= create_ce_flags(0);
2418 ce
->ce_namelen
= len
;
2419 ce
->ce_mode
= create_ce_mode(mode
);
2420 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2422 cache_tree_invalidate_path(&the_index
, path
);
2427 static struct commit
*find_single_final(struct rev_info
*revs
,
2428 const char **name_p
)
2431 struct commit
*found
= NULL
;
2432 const char *name
= NULL
;
2434 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2435 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2436 if (obj
->flags
& UNINTERESTING
)
2438 obj
= deref_tag(obj
, NULL
, 0);
2439 if (obj
->type
!= OBJ_COMMIT
)
2440 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2442 die("More than one commit to dig from %s and %s?",
2443 revs
->pending
.objects
[i
].name
, name
);
2444 found
= (struct commit
*)obj
;
2445 name
= revs
->pending
.objects
[i
].name
;
2452 static char *prepare_final(struct scoreboard
*sb
)
2455 sb
->final
= find_single_final(sb
->revs
, &name
);
2456 return xstrdup_or_null(name
);
2459 static char *prepare_initial(struct scoreboard
*sb
)
2462 const char *final_commit_name
= NULL
;
2463 struct rev_info
*revs
= sb
->revs
;
2466 * There must be one and only one negative commit, and it must be
2469 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2470 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2471 if (!(obj
->flags
& UNINTERESTING
))
2473 obj
= deref_tag(obj
, NULL
, 0);
2474 if (obj
->type
!= OBJ_COMMIT
)
2475 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2477 die("More than one commit to dig down to %s and %s?",
2478 revs
->pending
.objects
[i
].name
,
2480 sb
->final
= (struct commit
*) obj
;
2481 final_commit_name
= revs
->pending
.objects
[i
].name
;
2483 if (!final_commit_name
)
2484 die("No commit to dig down to?");
2485 return xstrdup(final_commit_name
);
2488 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2490 int *opt
= option
->value
;
2493 * -C enables copy from removed files;
2494 * -C -C enables copy from existing files, but only
2495 * when blaming a new file;
2496 * -C -C -C enables copy from existing files for
2499 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2500 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2501 if (*opt
& PICKAXE_BLAME_COPY
)
2502 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2503 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2506 blame_copy_score
= parse_score(arg
);
2510 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2512 int *opt
= option
->value
;
2514 *opt
|= PICKAXE_BLAME_MOVE
;
2517 blame_move_score
= parse_score(arg
);
2521 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2523 struct rev_info revs
;
2525 struct scoreboard sb
;
2527 struct blame_entry
*ent
= NULL
;
2528 long dashdash_pos
, lno
;
2529 char *final_commit_name
= NULL
;
2530 enum object_type type
;
2531 struct commit
*final_commit
= NULL
;
2533 struct string_list range_list
= STRING_LIST_INIT_NODUP
;
2534 int output_option
= 0, opt
= 0;
2536 const char *revs_file
= NULL
;
2537 const char *contents_from
= NULL
;
2538 const struct option options
[] = {
2539 OPT_BOOL(0, "incremental", &incremental
, N_("Show blame entries as we find them, incrementally")),
2540 OPT_BOOL('b', NULL
, &blank_boundary
, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2541 OPT_BOOL(0, "root", &show_root
, N_("Do not treat root commits as boundaries (Default: off)")),
2542 OPT_BOOL(0, "show-stats", &show_stats
, N_("Show work cost statistics")),
2543 OPT_BOOL(0, "progress", &show_progress
, N_("Force progress reporting")),
2544 OPT_BIT(0, "score-debug", &output_option
, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE
),
2545 OPT_BIT('f', "show-name", &output_option
, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME
),
2546 OPT_BIT('n', "show-number", &output_option
, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER
),
2547 OPT_BIT('p', "porcelain", &output_option
, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN
),
2548 OPT_BIT(0, "line-porcelain", &output_option
, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN
|OUTPUT_LINE_PORCELAIN
),
2549 OPT_BIT('c', NULL
, &output_option
, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT
),
2550 OPT_BIT('t', NULL
, &output_option
, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP
),
2551 OPT_BIT('l', NULL
, &output_option
, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME
),
2552 OPT_BIT('s', NULL
, &output_option
, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR
),
2553 OPT_BIT('e', "show-email", &output_option
, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL
),
2554 OPT_BIT('w', NULL
, &xdl_opts
, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE
),
2557 * The following two options are parsed by parse_revision_opt()
2558 * and are only included here to get included in the "-h"
2561 { OPTION_LOWLEVEL_CALLBACK
, 0, "indent-heuristic", NULL
, NULL
, N_("Use an experimental indent-based heuristic to improve diffs"), PARSE_OPT_NOARG
, parse_opt_unknown_cb
},
2562 { OPTION_LOWLEVEL_CALLBACK
, 0, "compaction-heuristic", NULL
, NULL
, N_("Use an experimental blank-line-based heuristic to improve diffs"), PARSE_OPT_NOARG
, parse_opt_unknown_cb
},
2564 OPT_BIT(0, "minimal", &xdl_opts
, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL
),
2565 OPT_STRING('S', NULL
, &revs_file
, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2566 OPT_STRING(0, "contents", &contents_from
, N_("file"), N_("Use <file>'s contents as the final image")),
2567 { OPTION_CALLBACK
, 'C', NULL
, &opt
, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG
, blame_copy_callback
},
2568 { OPTION_CALLBACK
, 'M', NULL
, &opt
, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG
, blame_move_callback
},
2569 OPT_STRING_LIST('L', NULL
, &range_list
, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2570 OPT__ABBREV(&abbrev
),
2574 struct parse_opt_ctx_t ctx
;
2575 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2576 struct range_set ranges
;
2577 unsigned int range_i
;
2580 git_config(git_blame_config
, &output_option
);
2581 init_revisions(&revs
, NULL
);
2582 revs
.date_mode
= blame_date_mode
;
2583 DIFF_OPT_SET(&revs
.diffopt
, ALLOW_TEXTCONV
);
2584 DIFF_OPT_SET(&revs
.diffopt
, FOLLOW_RENAMES
);
2586 save_commit_buffer
= 0;
2590 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
2591 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
2593 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2594 case PARSE_OPT_HELP
:
2596 case PARSE_OPT_DONE
:
2598 dashdash_pos
= ctx
.cpidx
;
2602 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2603 ctx
.argv
[0] = "--children";
2606 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2609 no_whole_file_rename
= !DIFF_OPT_TST(&revs
.diffopt
, FOLLOW_RENAMES
);
2610 xdl_opts
|= revs
.diffopt
.xdl_opts
& (XDF_COMPACTION_HEURISTIC
| XDF_INDENT_HEURISTIC
);
2611 DIFF_OPT_CLR(&revs
.diffopt
, FOLLOW_RENAMES
);
2612 argc
= parse_options_end(&ctx
);
2614 if (incremental
|| (output_option
& OUTPUT_PORCELAIN
)) {
2615 if (show_progress
> 0)
2616 die(_("--progress can't be used with --incremental or porcelain formats"));
2618 } else if (show_progress
< 0)
2619 show_progress
= isatty(2);
2622 /* one more abbrev length is needed for the boundary commit */
2625 if (revs_file
&& read_ancestry(revs_file
))
2626 die_errno("reading graft file '%s' failed", revs_file
);
2628 if (cmd_is_annotate
) {
2629 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2630 blame_date_mode
.type
= DATE_ISO8601
;
2632 blame_date_mode
= revs
.date_mode
;
2635 /* The maximum width used to show the dates */
2636 switch (blame_date_mode
.type
) {
2638 blame_date_width
= sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2640 case DATE_ISO8601_STRICT
:
2641 blame_date_width
= sizeof("2006-10-19T16:00:04-07:00");
2644 blame_date_width
= sizeof("2006-10-19 16:00:04 -0700");
2647 blame_date_width
= sizeof("1161298804 -0700");
2650 blame_date_width
= sizeof("1161298804");
2653 blame_date_width
= sizeof("2006-10-19");
2656 /* TRANSLATORS: This string is used to tell us the maximum
2657 display width for a relative timestamp in "git blame"
2658 output. For C locale, "4 years, 11 months ago", which
2659 takes 22 places, is the longest among various forms of
2660 relative timestamps, but your language may need more or
2661 fewer display columns. */
2662 blame_date_width
= utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2665 blame_date_width
= sizeof("Thu Oct 19 16:00:04 2006 -0700");
2668 blame_date_width
= strlen(show_date(0, 0, &blame_date_mode
)) + 1; /* add the null */
2671 blame_date_width
-= 1; /* strip the null */
2673 if (DIFF_OPT_TST(&revs
.diffopt
, FIND_COPIES_HARDER
))
2674 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
2675 PICKAXE_BLAME_COPY_HARDER
);
2677 if (!blame_move_score
)
2678 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2679 if (!blame_copy_score
)
2680 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2683 * We have collected options unknown to us in argv[1..unk]
2684 * which are to be passed to revision machinery if we are
2685 * going to do the "bottom" processing.
2687 * The remaining are:
2689 * (1) if dashdash_pos != 0, it is either
2690 * "blame [revisions] -- <path>" or
2691 * "blame -- <path> <rev>"
2693 * (2) otherwise, it is one of the two:
2694 * "blame [revisions] <path>"
2695 * "blame <path> <rev>"
2697 * Note that we must strip out <path> from the arguments: we do not
2698 * want the path pruning but we may want "bottom" processing.
2701 switch (argc
- dashdash_pos
- 1) {
2704 usage_with_options(blame_opt_usage
, options
);
2705 /* reorder for the new way: <rev> -- <path> */
2711 path
= add_prefix(prefix
, argv
[--argc
]);
2715 usage_with_options(blame_opt_usage
, options
);
2719 usage_with_options(blame_opt_usage
, options
);
2720 path
= add_prefix(prefix
, argv
[argc
- 1]);
2721 if (argc
== 3 && !file_exists(path
)) { /* (2b) */
2722 path
= add_prefix(prefix
, argv
[1]);
2725 argv
[argc
- 1] = "--";
2728 if (!file_exists(path
))
2729 die_errno("cannot stat path '%s'", path
);
2732 revs
.disable_stdin
= 1;
2733 setup_revisions(argc
, argv
, &revs
, NULL
);
2734 memset(&sb
, 0, sizeof(sb
));
2738 final_commit_name
= prepare_final(&sb
);
2739 sb
.commits
.compare
= compare_commits_by_commit_date
;
2741 else if (contents_from
)
2742 die(_("--contents and --reverse do not blend well."));
2744 final_commit_name
= prepare_initial(&sb
);
2745 sb
.commits
.compare
= compare_commits_by_reverse_commit_date
;
2746 if (revs
.first_parent_only
)
2747 revs
.children
.name
= NULL
;
2752 * "--not A B -- path" without anything positive;
2753 * do not default to HEAD, but use the working tree
2757 sb
.final
= fake_working_tree_commit(&sb
.revs
->diffopt
,
2758 path
, contents_from
);
2759 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2761 else if (contents_from
)
2762 die(_("cannot use --contents with final commit object name"));
2764 if (reverse
&& revs
.first_parent_only
) {
2765 final_commit
= find_single_final(sb
.revs
, NULL
);
2767 die(_("--reverse and --first-parent together require specified latest commit"));
2771 * If we have bottom, this will mark the ancestors of the
2772 * bottom commits we would reach while traversing as
2775 if (prepare_revision_walk(&revs
))
2776 die(_("revision walk setup failed"));
2778 if (reverse
&& revs
.first_parent_only
) {
2779 struct commit
*c
= final_commit
;
2781 sb
.revs
->children
.name
= "children";
2782 while (c
->parents
&&
2783 oidcmp(&c
->object
.oid
, &sb
.final
->object
.oid
)) {
2784 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2787 if (add_decoration(&sb
.revs
->children
,
2788 &c
->parents
->item
->object
, l
))
2789 die("BUG: not unique item in first-parent chain");
2790 c
= c
->parents
->item
;
2793 if (oidcmp(&c
->object
.oid
, &sb
.final
->object
.oid
))
2794 die(_("--reverse --first-parent together require range along first-parent chain"));
2797 if (is_null_oid(&sb
.final
->object
.oid
)) {
2799 sb
.final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
2800 sb
.final_buf_size
= o
->file
.size
;
2803 o
= get_origin(&sb
, sb
.final
, path
);
2804 if (fill_blob_sha1_and_mode(o
))
2805 die(_("no such path %s in %s"), path
, final_commit_name
);
2807 if (DIFF_OPT_TST(&sb
.revs
->diffopt
, ALLOW_TEXTCONV
) &&
2808 textconv_object(path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
.final_buf
,
2809 &sb
.final_buf_size
))
2812 sb
.final_buf
= read_sha1_file(o
->blob_oid
.hash
, &type
,
2813 &sb
.final_buf_size
);
2816 die(_("cannot read blob %s for path %s"),
2817 oid_to_hex(&o
->blob_oid
),
2821 lno
= prepare_lines(&sb
);
2823 if (lno
&& !range_list
.nr
)
2824 string_list_append(&range_list
, "1");
2827 range_set_init(&ranges
, range_list
.nr
);
2828 for (range_i
= 0; range_i
< range_list
.nr
; ++range_i
) {
2830 if (parse_range_arg(range_list
.items
[range_i
].string
,
2831 nth_line_cb
, &sb
, lno
, anchor
,
2832 &bottom
, &top
, sb
.path
))
2834 if (lno
< top
|| ((lno
|| bottom
) && lno
< bottom
))
2835 die(Q_("file %s has only %lu line",
2836 "file %s has only %lu lines",
2843 range_set_append_unsafe(&ranges
, bottom
, top
);
2846 sort_and_merge_range_set(&ranges
);
2848 for (range_i
= ranges
.nr
; range_i
> 0; --range_i
) {
2849 const struct range
*r
= &ranges
.ranges
[range_i
- 1];
2850 long bottom
= r
->start
;
2852 struct blame_entry
*next
= ent
;
2853 ent
= xcalloc(1, sizeof(*ent
));
2855 ent
->num_lines
= top
- bottom
;
2857 ent
->s_lno
= bottom
;
2863 prio_queue_put(&sb
.commits
, o
->commit
);
2867 range_set_release(&ranges
);
2868 string_list_clear(&range_list
, 0);
2873 read_mailmap(&mailmap
, NULL
);
2875 assign_blame(&sb
, opt
);
2880 free(final_commit_name
);
2885 sb
.ent
= blame_sort(sb
.ent
, compare_blame_final
);
2889 if (!(output_option
& OUTPUT_PORCELAIN
))
2890 find_alignment(&sb
, &output_option
);
2892 output(&sb
, output_option
);
2893 free((void *)sb
.final_buf
);
2894 for (ent
= sb
.ent
; ent
; ) {
2895 struct blame_entry
*e
= ent
->next
;
2901 printf("num read blob: %d\n", num_read_blob
);
2902 printf("num get patch: %d\n", num_get_patch
);
2903 printf("num commits: %d\n", num_commits
);