4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "string-list.h"
21 #include "parse-options.h"
24 static char blame_usage
[] = "git blame [options] [rev-opts] [rev] [--] file";
26 static const char *blame_opt_usage
[] = {
29 "[rev-opts] are documented in git-rev-list(1)",
33 static int longest_file
;
34 static int longest_author
;
35 static int max_orig_digits
;
36 static int max_digits
;
37 static int max_score_digits
;
40 static int blank_boundary
;
41 static int incremental
;
42 static int xdl_opts
= XDF_NEED_MINIMAL
;
43 static struct string_list mailmap
;
50 static int num_read_blob
;
51 static int num_get_patch
;
52 static int num_commits
;
54 #define PICKAXE_BLAME_MOVE 01
55 #define PICKAXE_BLAME_COPY 02
56 #define PICKAXE_BLAME_COPY_HARDER 04
57 #define PICKAXE_BLAME_COPY_HARDEST 010
60 * blame for a blame_entry with score lower than these thresholds
61 * is not passed to the parent using move/copy logic.
63 static unsigned blame_move_score
;
64 static unsigned blame_copy_score
;
65 #define BLAME_DEFAULT_MOVE_SCORE 20
66 #define BLAME_DEFAULT_COPY_SCORE 40
68 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
69 #define METAINFO_SHOWN (1u<<12)
70 #define MORE_THAN_ONE_PATH (1u<<13)
73 * One blob in a commit that is being suspected
77 struct origin
*previous
;
78 struct commit
*commit
;
80 unsigned char blob_sha1
[20];
81 char path
[FLEX_ARRAY
];
85 * Given an origin, prepare mmfile_t structure to be used by the
88 static void fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
91 enum object_type type
;
93 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
94 (unsigned long *)(&(file
->size
)));
96 die("Cannot read blob %s for path %s",
97 sha1_to_hex(o
->blob_sha1
),
106 * Origin is refcounted and usually we keep the blob contents to be
109 static inline struct origin
*origin_incref(struct origin
*o
)
116 static void origin_decref(struct origin
*o
)
118 if (o
&& --o
->refcnt
<= 0) {
120 origin_decref(o
->previous
);
126 static void drop_origin_blob(struct origin
*o
)
135 * Each group of lines is described by a blame_entry; it can be split
136 * as we pass blame to the parents. They form a linked list in the
137 * scoreboard structure, sorted by the target line number.
140 struct blame_entry
*prev
;
141 struct blame_entry
*next
;
143 /* the first line of this group in the final image;
144 * internally all line numbers are 0 based.
148 /* how many lines this group has */
151 /* the commit that introduced this group into the final image */
152 struct origin
*suspect
;
154 /* true if the suspect is truly guilty; false while we have not
155 * checked if the group came from one of its parents.
159 /* true if the entry has been scanned for copies in the current parent
163 /* the line number of the first line of this group in the
164 * suspect's file; internally all line numbers are 0 based.
168 /* how significant this entry is -- cached to avoid
169 * scanning the lines over and over.
175 * The current state of the blame assignment.
178 /* the final commit (i.e. where we started digging from) */
179 struct commit
*final
;
180 struct rev_info
*revs
;
184 * The contents in the final image.
185 * Used by many functions to obtain contents of the nth line,
186 * indexed with scoreboard.lineno[blame_entry.lno].
188 const char *final_buf
;
189 unsigned long final_buf_size
;
191 /* linked list of blames */
192 struct blame_entry
*ent
;
194 /* look-up a line in the final buffer */
199 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
203 if (a
->commit
!= b
->commit
)
205 return !strcmp(a
->path
, b
->path
);
208 static void sanity_check_refcnt(struct scoreboard
*);
211 * If two blame entries that are next to each other came from
212 * contiguous lines in the same origin (i.e. <commit, path> pair),
213 * merge them together.
215 static void coalesce(struct scoreboard
*sb
)
217 struct blame_entry
*ent
, *next
;
219 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
220 if (same_suspect(ent
->suspect
, next
->suspect
) &&
221 ent
->guilty
== next
->guilty
&&
222 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
223 ent
->num_lines
+= next
->num_lines
;
224 ent
->next
= next
->next
;
226 ent
->next
->prev
= ent
;
227 origin_decref(next
->suspect
);
230 next
= ent
; /* again */
234 if (DEBUG
) /* sanity */
235 sanity_check_refcnt(sb
);
239 * Given a commit and a path in it, create a new origin structure.
240 * The callers that add blame to the scoreboard should use
241 * get_origin() to obtain shared, refcounted copy instead of calling
242 * this function directly.
244 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
247 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
250 strcpy(o
->path
, path
);
255 * Locate an existing origin or create a new one.
257 static struct origin
*get_origin(struct scoreboard
*sb
,
258 struct commit
*commit
,
261 struct blame_entry
*e
;
263 for (e
= sb
->ent
; e
; e
= e
->next
) {
264 if (e
->suspect
->commit
== commit
&&
265 !strcmp(e
->suspect
->path
, path
))
266 return origin_incref(e
->suspect
);
268 return make_origin(commit
, path
);
272 * Fill the blob_sha1 field of an origin if it hasn't, so that later
273 * call to fill_origin_blob() can use it to locate the data. blob_sha1
274 * for an origin is also used to pass the blame for the entire file to
275 * the parent to detect the case where a child's blob is identical to
276 * that of its parent's.
278 static int fill_blob_sha1(struct origin
*origin
)
282 if (!is_null_sha1(origin
->blob_sha1
))
284 if (get_tree_entry(origin
->commit
->object
.sha1
,
286 origin
->blob_sha1
, &mode
))
288 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
292 hashclr(origin
->blob_sha1
);
297 * We have an origin -- check if the same path exists in the
298 * parent and return an origin structure to represent it.
300 static struct origin
*find_origin(struct scoreboard
*sb
,
301 struct commit
*parent
,
302 struct origin
*origin
)
304 struct origin
*porigin
= NULL
;
305 struct diff_options diff_opts
;
306 const char *paths
[2];
310 * Each commit object can cache one origin in that
311 * commit. This is a freestanding copy of origin and
314 struct origin
*cached
= parent
->util
;
315 if (!strcmp(cached
->path
, origin
->path
)) {
317 * The same path between origin and its parent
318 * without renaming -- the most common case.
320 porigin
= get_origin(sb
, parent
, cached
->path
);
323 * If the origin was newly created (i.e. get_origin
324 * would call make_origin if none is found in the
325 * scoreboard), it does not know the blob_sha1,
326 * so copy it. Otherwise porigin was in the
327 * scoreboard and already knows blob_sha1.
329 if (porigin
->refcnt
== 1)
330 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
333 /* otherwise it was not very useful; free it */
338 /* See if the origin->path is different between parent
339 * and origin first. Most of the time they are the
340 * same and diff-tree is fairly efficient about this.
342 diff_setup(&diff_opts
);
343 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
344 diff_opts
.detect_rename
= 0;
345 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
346 paths
[0] = origin
->path
;
349 diff_tree_setup_paths(paths
, &diff_opts
);
350 if (diff_setup_done(&diff_opts
) < 0)
353 if (is_null_sha1(origin
->commit
->object
.sha1
))
354 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
356 diff_tree_sha1(parent
->tree
->object
.sha1
,
357 origin
->commit
->tree
->object
.sha1
,
359 diffcore_std(&diff_opts
);
361 /* It is either one entry that says "modified", or "created",
364 if (!diff_queued_diff
.nr
) {
365 /* The path is the same as parent */
366 porigin
= get_origin(sb
, parent
, origin
->path
);
367 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
369 else if (diff_queued_diff
.nr
!= 1)
370 die("internal error in blame::find_origin");
372 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
375 die("internal error in blame::find_origin (%c)",
378 porigin
= get_origin(sb
, parent
, origin
->path
);
379 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
383 /* Did not exist in parent, or type changed */
387 diff_flush(&diff_opts
);
388 diff_tree_release_paths(&diff_opts
);
391 * Create a freestanding copy that is not part of
392 * the refcounted origin found in the scoreboard, and
393 * cache it in the commit.
395 struct origin
*cached
;
397 cached
= make_origin(porigin
->commit
, porigin
->path
);
398 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
399 parent
->util
= cached
;
405 * We have an origin -- find the path that corresponds to it in its
406 * parent and return an origin structure to represent it.
408 static struct origin
*find_rename(struct scoreboard
*sb
,
409 struct commit
*parent
,
410 struct origin
*origin
)
412 struct origin
*porigin
= NULL
;
413 struct diff_options diff_opts
;
415 const char *paths
[2];
417 diff_setup(&diff_opts
);
418 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
419 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
420 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
421 diff_opts
.single_follow
= origin
->path
;
423 diff_tree_setup_paths(paths
, &diff_opts
);
424 if (diff_setup_done(&diff_opts
) < 0)
427 if (is_null_sha1(origin
->commit
->object
.sha1
))
428 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
430 diff_tree_sha1(parent
->tree
->object
.sha1
,
431 origin
->commit
->tree
->object
.sha1
,
433 diffcore_std(&diff_opts
);
435 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
436 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
437 if ((p
->status
== 'R' || p
->status
== 'C') &&
438 !strcmp(p
->two
->path
, origin
->path
)) {
439 porigin
= get_origin(sb
, parent
, p
->one
->path
);
440 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
444 diff_flush(&diff_opts
);
445 diff_tree_release_paths(&diff_opts
);
450 * Link in a new blame entry to the scoreboard. Entries that cover the
451 * same line range have been removed from the scoreboard previously.
453 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
455 struct blame_entry
*ent
, *prev
= NULL
;
457 origin_incref(e
->suspect
);
459 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
462 /* prev, if not NULL, is the last one that is below e */
465 e
->next
= prev
->next
;
477 * src typically is on-stack; we want to copy the information in it to
478 * a malloced blame_entry that is already on the linked list of the
479 * scoreboard. The origin of dst loses a refcnt while the origin of src
482 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
484 struct blame_entry
*p
, *n
;
488 origin_incref(src
->suspect
);
489 origin_decref(dst
->suspect
);
490 memcpy(dst
, src
, sizeof(*src
));
496 static const char *nth_line(struct scoreboard
*sb
, int lno
)
498 return sb
->final_buf
+ sb
->lineno
[lno
];
502 * It is known that lines between tlno to same came from parent, and e
503 * has an overlap with that range. it also is known that parent's
504 * line plno corresponds to e's line tlno.
510 * <------------------>
512 * Split e into potentially three parts; before this chunk, the chunk
513 * to be blamed for the parent, and after that portion.
515 static void split_overlap(struct blame_entry
*split
,
516 struct blame_entry
*e
,
517 int tlno
, int plno
, int same
,
518 struct origin
*parent
)
521 memset(split
, 0, sizeof(struct blame_entry
[3]));
523 if (e
->s_lno
< tlno
) {
524 /* there is a pre-chunk part not blamed on parent */
525 split
[0].suspect
= origin_incref(e
->suspect
);
526 split
[0].lno
= e
->lno
;
527 split
[0].s_lno
= e
->s_lno
;
528 split
[0].num_lines
= tlno
- e
->s_lno
;
529 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
530 split
[1].s_lno
= plno
;
533 split
[1].lno
= e
->lno
;
534 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
537 if (same
< e
->s_lno
+ e
->num_lines
) {
538 /* there is a post-chunk part not blamed on parent */
539 split
[2].suspect
= origin_incref(e
->suspect
);
540 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
541 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
542 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
543 chunk_end_lno
= split
[2].lno
;
546 chunk_end_lno
= e
->lno
+ e
->num_lines
;
547 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
550 * if it turns out there is nothing to blame the parent for,
551 * forget about the splitting. !split[1].suspect signals this.
553 if (split
[1].num_lines
< 1)
555 split
[1].suspect
= origin_incref(parent
);
559 * split_overlap() divided an existing blame e into up to three parts
560 * in split. Adjust the linked list of blames in the scoreboard to
563 static void split_blame(struct scoreboard
*sb
,
564 struct blame_entry
*split
,
565 struct blame_entry
*e
)
567 struct blame_entry
*new_entry
;
569 if (split
[0].suspect
&& split
[2].suspect
) {
570 /* The first part (reuse storage for the existing entry e) */
571 dup_entry(e
, &split
[0]);
573 /* The last part -- me */
574 new_entry
= xmalloc(sizeof(*new_entry
));
575 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
576 add_blame_entry(sb
, new_entry
);
578 /* ... and the middle part -- parent */
579 new_entry
= xmalloc(sizeof(*new_entry
));
580 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
581 add_blame_entry(sb
, new_entry
);
583 else if (!split
[0].suspect
&& !split
[2].suspect
)
585 * The parent covers the entire area; reuse storage for
586 * e and replace it with the parent.
588 dup_entry(e
, &split
[1]);
589 else if (split
[0].suspect
) {
590 /* me and then parent */
591 dup_entry(e
, &split
[0]);
593 new_entry
= xmalloc(sizeof(*new_entry
));
594 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
595 add_blame_entry(sb
, new_entry
);
598 /* parent and then me */
599 dup_entry(e
, &split
[1]);
601 new_entry
= xmalloc(sizeof(*new_entry
));
602 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
603 add_blame_entry(sb
, new_entry
);
606 if (DEBUG
) { /* sanity */
607 struct blame_entry
*ent
;
608 int lno
= sb
->ent
->lno
, corrupt
= 0;
610 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
615 lno
+= ent
->num_lines
;
619 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
620 printf("L %8d l %8d n %8d\n",
621 lno
, ent
->lno
, ent
->num_lines
);
622 lno
= ent
->lno
+ ent
->num_lines
;
630 * After splitting the blame, the origins used by the
631 * on-stack blame_entry should lose one refcnt each.
633 static void decref_split(struct blame_entry
*split
)
637 for (i
= 0; i
< 3; i
++)
638 origin_decref(split
[i
].suspect
);
642 * Helper for blame_chunk(). blame_entry e is known to overlap with
643 * the patch hunk; split it and pass blame to the parent.
645 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
646 int tlno
, int plno
, int same
,
647 struct origin
*parent
)
649 struct blame_entry split
[3];
651 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
652 if (split
[1].suspect
)
653 split_blame(sb
, split
, e
);
658 * Find the line number of the last line the target is suspected for.
660 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
662 struct blame_entry
*e
;
663 int last_in_target
= -1;
665 for (e
= sb
->ent
; e
; e
= e
->next
) {
666 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
668 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
669 last_in_target
= e
->s_lno
+ e
->num_lines
;
671 return last_in_target
;
675 * Process one hunk from the patch between the current suspect for
676 * blame_entry e and its parent. Find and split the overlap, and
677 * pass blame to the overlapping part to the parent.
679 static void blame_chunk(struct scoreboard
*sb
,
680 int tlno
, int plno
, int same
,
681 struct origin
*target
, struct origin
*parent
)
683 struct blame_entry
*e
;
685 for (e
= sb
->ent
; e
; e
= e
->next
) {
686 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
688 if (same
<= e
->s_lno
)
690 if (tlno
< e
->s_lno
+ e
->num_lines
)
691 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
695 struct blame_chunk_cb_data
{
696 struct scoreboard
*sb
;
697 struct origin
*target
;
698 struct origin
*parent
;
703 static void blame_chunk_cb(void *data
, long same
, long p_next
, long t_next
)
705 struct blame_chunk_cb_data
*d
= data
;
706 blame_chunk(d
->sb
, d
->tlno
, d
->plno
, same
, d
->target
, d
->parent
);
712 * We are looking at the origin 'target' and aiming to pass blame
713 * for the lines it is suspected to its parent. Run diff to find
714 * which lines came from parent and pass blame for them.
716 static int pass_blame_to_parent(struct scoreboard
*sb
,
717 struct origin
*target
,
718 struct origin
*parent
)
721 mmfile_t file_p
, file_o
;
722 struct blame_chunk_cb_data d
= { sb
, target
, parent
, 0, 0 };
726 last_in_target
= find_last_in_target(sb
, target
);
727 if (last_in_target
< 0)
728 return 1; /* nothing remains for this target */
730 fill_origin_blob(parent
, &file_p
);
731 fill_origin_blob(target
, &file_o
);
734 memset(&xpp
, 0, sizeof(xpp
));
735 xpp
.flags
= xdl_opts
;
736 memset(&xecfg
, 0, sizeof(xecfg
));
738 xdi_diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, &xpp
, &xecfg
);
739 /* The rest (i.e. anything after tlno) are the same as the parent */
740 blame_chunk(sb
, d
.tlno
, d
.plno
, last_in_target
, target
, parent
);
746 * The lines in blame_entry after splitting blames many times can become
747 * very small and trivial, and at some point it becomes pointless to
748 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
749 * ordinary C program, and it is not worth to say it was copied from
750 * totally unrelated file in the parent.
752 * Compute how trivial the lines in the blame_entry are.
754 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
763 cp
= nth_line(sb
, e
->lno
);
764 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
766 unsigned ch
= *((unsigned char *)cp
);
776 * best_so_far[] and this[] are both a split of an existing blame_entry
777 * that passes blame to the parent. Maintain best_so_far the best split
778 * so far, by comparing this and best_so_far and copying this into
779 * bst_so_far as needed.
781 static void copy_split_if_better(struct scoreboard
*sb
,
782 struct blame_entry
*best_so_far
,
783 struct blame_entry
*this)
787 if (!this[1].suspect
)
789 if (best_so_far
[1].suspect
) {
790 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
794 for (i
= 0; i
< 3; i
++)
795 origin_incref(this[i
].suspect
);
796 decref_split(best_so_far
);
797 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
801 * We are looking at a part of the final image represented by
802 * ent (tlno and same are offset by ent->s_lno).
803 * tlno is where we are looking at in the final image.
804 * up to (but not including) same match preimage.
805 * plno is where we are looking at in the preimage.
807 * <-------------- final image ---------------------->
810 * <---------preimage----->
813 * All line numbers are 0-based.
815 static void handle_split(struct scoreboard
*sb
,
816 struct blame_entry
*ent
,
817 int tlno
, int plno
, int same
,
818 struct origin
*parent
,
819 struct blame_entry
*split
)
821 if (ent
->num_lines
<= tlno
)
824 struct blame_entry
this[3];
827 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
828 copy_split_if_better(sb
, split
, this);
833 struct handle_split_cb_data
{
834 struct scoreboard
*sb
;
835 struct blame_entry
*ent
;
836 struct origin
*parent
;
837 struct blame_entry
*split
;
842 static void handle_split_cb(void *data
, long same
, long p_next
, long t_next
)
844 struct handle_split_cb_data
*d
= data
;
845 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, same
, d
->parent
, d
->split
);
851 * Find the lines from parent that are the same as ent so that
852 * we can pass blames to it. file_p has the blob contents for
855 static void find_copy_in_blob(struct scoreboard
*sb
,
856 struct blame_entry
*ent
,
857 struct origin
*parent
,
858 struct blame_entry
*split
,
864 struct handle_split_cb_data d
= { sb
, ent
, parent
, split
, 0, 0 };
869 * Prepare mmfile that contains only the lines in ent.
871 cp
= nth_line(sb
, ent
->lno
);
872 file_o
.ptr
= (char*) cp
;
873 cnt
= ent
->num_lines
;
875 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
879 file_o
.size
= cp
- file_o
.ptr
;
882 * file_o is a part of final image we are annotating.
883 * file_p partially may match that image.
885 memset(&xpp
, 0, sizeof(xpp
));
886 xpp
.flags
= xdl_opts
;
887 memset(&xecfg
, 0, sizeof(xecfg
));
889 memset(split
, 0, sizeof(struct blame_entry
[3]));
890 xdi_diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, &xpp
, &xecfg
);
891 /* remainder, if any, all match the preimage */
892 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
896 * See if lines currently target is suspected for can be attributed to
899 static int find_move_in_parent(struct scoreboard
*sb
,
900 struct origin
*target
,
901 struct origin
*parent
)
903 int last_in_target
, made_progress
;
904 struct blame_entry
*e
, split
[3];
907 last_in_target
= find_last_in_target(sb
, target
);
908 if (last_in_target
< 0)
909 return 1; /* nothing remains for this target */
911 fill_origin_blob(parent
, &file_p
);
916 while (made_progress
) {
918 for (e
= sb
->ent
; e
; e
= e
->next
) {
919 if (e
->guilty
|| !same_suspect(e
->suspect
, target
) ||
920 ent_score(sb
, e
) < blame_move_score
)
922 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
923 if (split
[1].suspect
&&
924 blame_move_score
< ent_score(sb
, &split
[1])) {
925 split_blame(sb
, split
, e
);
935 struct blame_entry
*ent
;
936 struct blame_entry split
[3];
940 * Count the number of entries the target is suspected for,
941 * and prepare a list of entry and the best split.
943 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
944 struct origin
*target
,
948 struct blame_entry
*e
;
950 struct blame_list
*blame_list
= NULL
;
952 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
953 if (!e
->scanned
&& !e
->guilty
&&
954 same_suspect(e
->suspect
, target
) &&
955 min_score
< ent_score(sb
, e
))
958 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
959 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
960 if (!e
->scanned
&& !e
->guilty
&&
961 same_suspect(e
->suspect
, target
) &&
962 min_score
< ent_score(sb
, e
))
963 blame_list
[i
++].ent
= e
;
965 *num_ents_p
= num_ents
;
970 * Reset the scanned status on all entries.
972 static void reset_scanned_flag(struct scoreboard
*sb
)
974 struct blame_entry
*e
;
975 for (e
= sb
->ent
; e
; e
= e
->next
)
980 * For lines target is suspected for, see if we can find code movement
981 * across file boundary from the parent commit. porigin is the path
982 * in the parent we already tried.
984 static int find_copy_in_parent(struct scoreboard
*sb
,
985 struct origin
*target
,
986 struct commit
*parent
,
987 struct origin
*porigin
,
990 struct diff_options diff_opts
;
991 const char *paths
[1];
994 struct blame_list
*blame_list
;
997 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
999 return 1; /* nothing remains for this target */
1001 diff_setup(&diff_opts
);
1002 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1003 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1006 diff_tree_setup_paths(paths
, &diff_opts
);
1007 if (diff_setup_done(&diff_opts
) < 0)
1010 /* Try "find copies harder" on new path if requested;
1011 * we do not want to use diffcore_rename() actually to
1012 * match things up; find_copies_harder is set only to
1013 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1014 * and this code needs to be after diff_setup_done(), which
1015 * usually makes find-copies-harder imply copy detection.
1017 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1018 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1019 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1020 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1022 if (is_null_sha1(target
->commit
->object
.sha1
))
1023 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1025 diff_tree_sha1(parent
->tree
->object
.sha1
,
1026 target
->commit
->tree
->object
.sha1
,
1029 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1030 diffcore_std(&diff_opts
);
1034 int made_progress
= 0;
1036 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1037 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1038 struct origin
*norigin
;
1040 struct blame_entry
this[3];
1042 if (!DIFF_FILE_VALID(p
->one
))
1043 continue; /* does not exist in parent */
1044 if (S_ISGITLINK(p
->one
->mode
))
1045 continue; /* ignore git links */
1046 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1047 /* find_move already dealt with this path */
1050 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1051 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1052 fill_origin_blob(norigin
, &file_p
);
1056 for (j
= 0; j
< num_ents
; j
++) {
1057 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1058 norigin
, this, &file_p
);
1059 copy_split_if_better(sb
, blame_list
[j
].split
,
1063 origin_decref(norigin
);
1066 for (j
= 0; j
< num_ents
; j
++) {
1067 struct blame_entry
*split
= blame_list
[j
].split
;
1068 if (split
[1].suspect
&&
1069 blame_copy_score
< ent_score(sb
, &split
[1])) {
1070 split_blame(sb
, split
, blame_list
[j
].ent
);
1074 blame_list
[j
].ent
->scanned
= 1;
1075 decref_split(split
);
1081 blame_list
= setup_blame_list(sb
, target
, blame_copy_score
, &num_ents
);
1087 reset_scanned_flag(sb
);
1088 diff_flush(&diff_opts
);
1089 diff_tree_release_paths(&diff_opts
);
1094 * The blobs of origin and porigin exactly match, so everything
1095 * origin is suspected for can be blamed on the parent.
1097 static void pass_whole_blame(struct scoreboard
*sb
,
1098 struct origin
*origin
, struct origin
*porigin
)
1100 struct blame_entry
*e
;
1102 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1103 /* Steal its file */
1104 porigin
->file
= origin
->file
;
1105 origin
->file
.ptr
= NULL
;
1107 for (e
= sb
->ent
; e
; e
= e
->next
) {
1108 if (!same_suspect(e
->suspect
, origin
))
1110 origin_incref(porigin
);
1111 origin_decref(e
->suspect
);
1112 e
->suspect
= porigin
;
1117 * We pass blame from the current commit to its parents. We keep saying
1118 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1119 * exonerate ourselves.
1121 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
)
1124 return commit
->parents
;
1125 return lookup_decoration(&revs
->children
, &commit
->object
);
1128 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
)
1131 struct commit_list
*l
= first_scapegoat(revs
, commit
);
1132 for (cnt
= 0; l
; l
= l
->next
)
1139 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1141 struct rev_info
*revs
= sb
->revs
;
1142 int i
, pass
, num_sg
;
1143 struct commit
*commit
= origin
->commit
;
1144 struct commit_list
*sg
;
1145 struct origin
*sg_buf
[MAXSG
];
1146 struct origin
*porigin
, **sg_origin
= sg_buf
;
1148 num_sg
= num_scapegoats(revs
, commit
);
1151 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1152 memset(sg_buf
, 0, sizeof(sg_buf
));
1154 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1157 * The first pass looks for unrenamed path to optimize for
1158 * common cases, then we look for renames in the second pass.
1160 for (pass
= 0; pass
< 2; pass
++) {
1161 struct origin
*(*find
)(struct scoreboard
*,
1162 struct commit
*, struct origin
*);
1163 find
= pass
? find_rename
: find_origin
;
1165 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1167 sg
= sg
->next
, i
++) {
1168 struct commit
*p
= sg
->item
;
1173 if (parse_commit(p
))
1175 porigin
= find(sb
, p
, origin
);
1178 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1179 pass_whole_blame(sb
, origin
, porigin
);
1180 origin_decref(porigin
);
1183 for (j
= same
= 0; j
< i
; j
++)
1185 !hashcmp(sg_origin
[j
]->blob_sha1
,
1186 porigin
->blob_sha1
)) {
1191 sg_origin
[i
] = porigin
;
1193 origin_decref(porigin
);
1198 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1200 sg
= sg
->next
, i
++) {
1201 struct origin
*porigin
= sg_origin
[i
];
1204 if (!origin
->previous
) {
1205 origin_incref(porigin
);
1206 origin
->previous
= porigin
;
1208 if (pass_blame_to_parent(sb
, origin
, porigin
))
1213 * Optionally find moves in parents' files.
1215 if (opt
& PICKAXE_BLAME_MOVE
)
1216 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1218 sg
= sg
->next
, i
++) {
1219 struct origin
*porigin
= sg_origin
[i
];
1222 if (find_move_in_parent(sb
, origin
, porigin
))
1227 * Optionally find copies from parents' files.
1229 if (opt
& PICKAXE_BLAME_COPY
)
1230 for (i
= 0, sg
= first_scapegoat(revs
, commit
);
1232 sg
= sg
->next
, i
++) {
1233 struct origin
*porigin
= sg_origin
[i
];
1234 if (find_copy_in_parent(sb
, origin
, sg
->item
,
1240 for (i
= 0; i
< num_sg
; i
++) {
1242 drop_origin_blob(sg_origin
[i
]);
1243 origin_decref(sg_origin
[i
]);
1246 drop_origin_blob(origin
);
1247 if (sg_buf
!= sg_origin
)
1252 * Information on commits, used for output.
1257 const char *author_mail
;
1258 unsigned long author_time
;
1259 const char *author_tz
;
1261 /* filled only when asked for details */
1262 const char *committer
;
1263 const char *committer_mail
;
1264 unsigned long committer_time
;
1265 const char *committer_tz
;
1267 const char *summary
;
1271 * Parse author/committer line in the commit object buffer
1273 static void get_ac_line(const char *inbuf
, const char *what
,
1274 int person_len
, char *person
,
1275 int mail_len
, char *mail
,
1276 unsigned long *time
, const char **tz
)
1278 int len
, tzlen
, maillen
;
1279 char *tmp
, *endp
, *timepos
, *mailpos
;
1281 tmp
= strstr(inbuf
, what
);
1284 tmp
+= strlen(what
);
1285 endp
= strchr(tmp
, '\n');
1290 if (person_len
<= len
) {
1298 memcpy(person
, tmp
, len
);
1306 tzlen
= (person
+len
)-(tmp
+1);
1311 *time
= strtoul(tmp
, NULL
, 10);
1319 maillen
= timepos
- tmp
;
1320 memcpy(mail
, mailpos
, maillen
);
1326 * mailmap expansion may make the name longer.
1327 * make room by pushing stuff down.
1329 tmp
= person
+ person_len
- (tzlen
+ 1);
1330 memmove(tmp
, *tz
, tzlen
);
1335 * Now, convert both name and e-mail using mailmap
1337 if(map_user(&mailmap
, mail
+1, mail_len
-1, person
, tmp
-person
-1)) {
1338 /* Add a trailing '>' to email, since map_user returns plain emails
1339 Note: It already has '<', since we replace from mail+1 */
1340 mailpos
= memchr(mail
, '\0', mail_len
);
1341 if (mailpos
&& mailpos
-mail
< mail_len
- 1) {
1343 *(mailpos
+1) = '\0';
1348 static void get_commit_info(struct commit
*commit
,
1349 struct commit_info
*ret
,
1353 char *tmp
, *endp
, *reencoded
, *message
;
1354 static char author_name
[1024];
1355 static char author_mail
[1024];
1356 static char committer_name
[1024];
1357 static char committer_mail
[1024];
1358 static char summary_buf
[1024];
1361 * We've operated without save_commit_buffer, so
1362 * we now need to populate them for output.
1364 if (!commit
->buffer
) {
1365 enum object_type type
;
1368 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1369 if (!commit
->buffer
)
1370 die("Cannot read commit %s",
1371 sha1_to_hex(commit
->object
.sha1
));
1373 reencoded
= reencode_commit_message(commit
, NULL
);
1374 message
= reencoded
? reencoded
: commit
->buffer
;
1375 ret
->author
= author_name
;
1376 ret
->author_mail
= author_mail
;
1377 get_ac_line(message
, "\nauthor ",
1378 sizeof(author_name
), author_name
,
1379 sizeof(author_mail
), author_mail
,
1380 &ret
->author_time
, &ret
->author_tz
);
1387 ret
->committer
= committer_name
;
1388 ret
->committer_mail
= committer_mail
;
1389 get_ac_line(message
, "\ncommitter ",
1390 sizeof(committer_name
), committer_name
,
1391 sizeof(committer_mail
), committer_mail
,
1392 &ret
->committer_time
, &ret
->committer_tz
);
1394 ret
->summary
= summary_buf
;
1395 tmp
= strstr(message
, "\n\n");
1398 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1403 endp
= strchr(tmp
, '\n');
1405 endp
= tmp
+ strlen(tmp
);
1407 if (len
>= sizeof(summary_buf
) || len
== 0)
1409 memcpy(summary_buf
, tmp
, len
);
1410 summary_buf
[len
] = 0;
1415 * To allow LF and other nonportable characters in pathnames,
1416 * they are c-style quoted as needed.
1418 static void write_filename_info(const char *path
)
1420 printf("filename ");
1421 write_name_quoted(path
, stdout
, '\n');
1425 * Porcelain/Incremental format wants to show a lot of details per
1426 * commit. Instead of repeating this every line, emit it only once,
1427 * the first time each commit appears in the output.
1429 static int emit_one_suspect_detail(struct origin
*suspect
)
1431 struct commit_info ci
;
1433 if (suspect
->commit
->object
.flags
& METAINFO_SHOWN
)
1436 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1437 get_commit_info(suspect
->commit
, &ci
, 1);
1438 printf("author %s\n", ci
.author
);
1439 printf("author-mail %s\n", ci
.author_mail
);
1440 printf("author-time %lu\n", ci
.author_time
);
1441 printf("author-tz %s\n", ci
.author_tz
);
1442 printf("committer %s\n", ci
.committer
);
1443 printf("committer-mail %s\n", ci
.committer_mail
);
1444 printf("committer-time %lu\n", ci
.committer_time
);
1445 printf("committer-tz %s\n", ci
.committer_tz
);
1446 printf("summary %s\n", ci
.summary
);
1447 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1448 printf("boundary\n");
1449 if (suspect
->previous
) {
1450 struct origin
*prev
= suspect
->previous
;
1451 printf("previous %s ", sha1_to_hex(prev
->commit
->object
.sha1
));
1452 write_name_quoted(prev
->path
, stdout
, '\n');
1458 * The blame_entry is found to be guilty for the range. Mark it
1459 * as such, and show it in incremental output.
1461 static void found_guilty_entry(struct blame_entry
*ent
)
1467 struct origin
*suspect
= ent
->suspect
;
1469 printf("%s %d %d %d\n",
1470 sha1_to_hex(suspect
->commit
->object
.sha1
),
1471 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1472 emit_one_suspect_detail(suspect
);
1473 write_filename_info(suspect
->path
);
1474 maybe_flush_or_die(stdout
, "stdout");
1479 * The main loop -- while the scoreboard has lines whose true origin
1480 * is still unknown, pick one blame_entry, and allow its current
1481 * suspect to pass blames to its parents.
1483 static void assign_blame(struct scoreboard
*sb
, int opt
)
1485 struct rev_info
*revs
= sb
->revs
;
1488 struct blame_entry
*ent
;
1489 struct commit
*commit
;
1490 struct origin
*suspect
= NULL
;
1492 /* find one suspect to break down */
1493 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1495 suspect
= ent
->suspect
;
1497 return; /* all done */
1500 * We will use this suspect later in the loop,
1501 * so hold onto it in the meantime.
1503 origin_incref(suspect
);
1504 commit
= suspect
->commit
;
1505 if (!commit
->object
.parsed
)
1506 parse_commit(commit
);
1508 (!(commit
->object
.flags
& UNINTERESTING
) &&
1509 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1510 pass_blame(sb
, suspect
, opt
);
1512 commit
->object
.flags
|= UNINTERESTING
;
1513 if (commit
->object
.parsed
)
1514 mark_parents_uninteresting(commit
);
1516 /* treat root commit as boundary */
1517 if (!commit
->parents
&& !show_root
)
1518 commit
->object
.flags
|= UNINTERESTING
;
1520 /* Take responsibility for the remaining entries */
1521 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1522 if (same_suspect(ent
->suspect
, suspect
))
1523 found_guilty_entry(ent
);
1524 origin_decref(suspect
);
1526 if (DEBUG
) /* sanity */
1527 sanity_check_refcnt(sb
);
1531 static const char *format_time(unsigned long time
, const char *tz_str
,
1534 static char time_buf
[128];
1539 if (show_raw_time
) {
1540 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1545 minutes
= tz
< 0 ? -tz
: tz
;
1546 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1547 minutes
= tz
< 0 ? -minutes
: minutes
;
1548 t
= time
+ minutes
* 60;
1551 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1552 strcat(time_buf
, tz_str
);
1556 #define OUTPUT_ANNOTATE_COMPAT 001
1557 #define OUTPUT_LONG_OBJECT_NAME 002
1558 #define OUTPUT_RAW_TIMESTAMP 004
1559 #define OUTPUT_PORCELAIN 010
1560 #define OUTPUT_SHOW_NAME 020
1561 #define OUTPUT_SHOW_NUMBER 040
1562 #define OUTPUT_SHOW_SCORE 0100
1563 #define OUTPUT_NO_AUTHOR 0200
1565 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1569 struct origin
*suspect
= ent
->suspect
;
1572 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1573 printf("%s%c%d %d %d\n",
1575 ent
->guilty
? ' ' : '*', // purely for debugging
1579 if (emit_one_suspect_detail(suspect
) ||
1580 (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
))
1581 write_filename_info(suspect
->path
);
1583 cp
= nth_line(sb
, ent
->lno
);
1584 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1587 printf("%s %d %d\n", hex
,
1588 ent
->s_lno
+ 1 + cnt
,
1589 ent
->lno
+ 1 + cnt
);
1594 } while (ch
!= '\n' &&
1595 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1599 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1603 struct origin
*suspect
= ent
->suspect
;
1604 struct commit_info ci
;
1606 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1608 get_commit_info(suspect
->commit
, &ci
, 1);
1609 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1611 cp
= nth_line(sb
, ent
->lno
);
1612 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1614 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1616 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1618 memset(hex
, ' ', length
);
1619 else if (!(opt
& OUTPUT_ANNOTATE_COMPAT
)) {
1625 printf("%.*s", length
, hex
);
1626 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1627 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1628 format_time(ci
.author_time
, ci
.author_tz
,
1630 ent
->lno
+ 1 + cnt
);
1632 if (opt
& OUTPUT_SHOW_SCORE
)
1634 max_score_digits
, ent
->score
,
1635 ent
->suspect
->refcnt
);
1636 if (opt
& OUTPUT_SHOW_NAME
)
1637 printf(" %-*.*s", longest_file
, longest_file
,
1639 if (opt
& OUTPUT_SHOW_NUMBER
)
1640 printf(" %*d", max_orig_digits
,
1641 ent
->s_lno
+ 1 + cnt
);
1643 if (!(opt
& OUTPUT_NO_AUTHOR
)) {
1644 int pad
= longest_author
- utf8_strwidth(ci
.author
);
1645 printf(" (%s%*s %10s",
1647 format_time(ci
.author_time
,
1652 max_digits
, ent
->lno
+ 1 + cnt
);
1657 } while (ch
!= '\n' &&
1658 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1662 static void output(struct scoreboard
*sb
, int option
)
1664 struct blame_entry
*ent
;
1666 if (option
& OUTPUT_PORCELAIN
) {
1667 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1668 struct blame_entry
*oth
;
1669 struct origin
*suspect
= ent
->suspect
;
1670 struct commit
*commit
= suspect
->commit
;
1671 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1673 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1674 if ((oth
->suspect
->commit
!= commit
) ||
1675 !strcmp(oth
->suspect
->path
, suspect
->path
))
1677 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1683 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1684 if (option
& OUTPUT_PORCELAIN
)
1685 emit_porcelain(sb
, ent
);
1687 emit_other(sb
, ent
, option
);
1693 * To allow quick access to the contents of nth line in the
1694 * final image, prepare an index in the scoreboard.
1696 static int prepare_lines(struct scoreboard
*sb
)
1698 const char *buf
= sb
->final_buf
;
1699 unsigned long len
= sb
->final_buf_size
;
1700 int num
= 0, incomplete
= 0, bol
= 1;
1702 if (len
&& buf
[len
-1] != '\n')
1703 incomplete
++; /* incomplete line at the end */
1706 sb
->lineno
= xrealloc(sb
->lineno
,
1707 sizeof(int* ) * (num
+ 1));
1708 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1711 if (*buf
++ == '\n') {
1716 sb
->lineno
= xrealloc(sb
->lineno
,
1717 sizeof(int* ) * (num
+ incomplete
+ 1));
1718 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1719 sb
->num_lines
= num
+ incomplete
;
1720 return sb
->num_lines
;
1724 * Add phony grafts for use with -S; this is primarily to
1725 * support git's cvsserver that wants to give a linear history
1728 static int read_ancestry(const char *graft_file
)
1730 FILE *fp
= fopen(graft_file
, "r");
1734 while (fgets(buf
, sizeof(buf
), fp
)) {
1735 /* The format is just "Commit Parent1 Parent2 ...\n" */
1736 int len
= strlen(buf
);
1737 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1739 register_commit_graft(graft
, 0);
1746 * How many columns do we need to show line numbers in decimal?
1748 static int lineno_width(int lines
)
1752 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1758 * How many columns do we need to show line numbers, authors,
1761 static void find_alignment(struct scoreboard
*sb
, int *option
)
1763 int longest_src_lines
= 0;
1764 int longest_dst_lines
= 0;
1765 unsigned largest_score
= 0;
1766 struct blame_entry
*e
;
1768 for (e
= sb
->ent
; e
; e
= e
->next
) {
1769 struct origin
*suspect
= e
->suspect
;
1770 struct commit_info ci
;
1773 if (strcmp(suspect
->path
, sb
->path
))
1774 *option
|= OUTPUT_SHOW_NAME
;
1775 num
= strlen(suspect
->path
);
1776 if (longest_file
< num
)
1778 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1779 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1780 get_commit_info(suspect
->commit
, &ci
, 1);
1781 num
= utf8_strwidth(ci
.author
);
1782 if (longest_author
< num
)
1783 longest_author
= num
;
1785 num
= e
->s_lno
+ e
->num_lines
;
1786 if (longest_src_lines
< num
)
1787 longest_src_lines
= num
;
1788 num
= e
->lno
+ e
->num_lines
;
1789 if (longest_dst_lines
< num
)
1790 longest_dst_lines
= num
;
1791 if (largest_score
< ent_score(sb
, e
))
1792 largest_score
= ent_score(sb
, e
);
1794 max_orig_digits
= lineno_width(longest_src_lines
);
1795 max_digits
= lineno_width(longest_dst_lines
);
1796 max_score_digits
= lineno_width(largest_score
);
1800 * For debugging -- origin is refcounted, and this asserts that
1801 * we do not underflow.
1803 static void sanity_check_refcnt(struct scoreboard
*sb
)
1806 struct blame_entry
*ent
;
1808 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1809 /* Nobody should have zero or negative refcnt */
1810 if (ent
->suspect
->refcnt
<= 0) {
1811 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1813 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1814 ent
->suspect
->refcnt
);
1820 find_alignment(sb
, &opt
);
1822 die("Baa %d!", baa
);
1827 * Used for the command line parsing; check if the path exists
1828 * in the working tree.
1830 static int has_string_in_work_tree(const char *path
)
1833 return !lstat(path
, &st
);
1836 static unsigned parse_score(const char *arg
)
1839 unsigned long score
= strtoul(arg
, &end
, 10);
1845 static const char *add_prefix(const char *prefix
, const char *path
)
1847 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
1851 * Parsing of (comma separated) one item in the -L option
1853 static const char *parse_loc(const char *spec
,
1854 struct scoreboard
*sb
, long lno
,
1855 long begin
, long *ret
)
1862 regmatch_t match
[1];
1864 /* Allow "-L <something>,+20" to mean starting at <something>
1865 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1868 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1869 num
= strtol(spec
+ 1, &term
, 10);
1870 if (term
!= spec
+ 1) {
1874 *ret
= begin
+ num
- 2;
1883 num
= strtol(spec
, &term
, 10);
1891 /* it could be a regexp of form /.../ */
1892 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1899 /* try [spec+1 .. term-1] as regexp */
1901 begin
--; /* input is in human terms */
1902 line
= nth_line(sb
, begin
);
1904 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1905 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1906 const char *cp
= line
+ match
[0].rm_so
;
1909 while (begin
++ < lno
) {
1910 nline
= nth_line(sb
, begin
);
1911 if (line
<= cp
&& cp
< nline
)
1922 regerror(reg_error
, ®exp
, errbuf
, 1024);
1923 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1928 * Parsing of -L option
1930 static void prepare_blame_range(struct scoreboard
*sb
,
1931 const char *bottomtop
,
1933 long *bottom
, long *top
)
1937 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1939 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1947 static int git_blame_config(const char *var
, const char *value
, void *cb
)
1949 if (!strcmp(var
, "blame.showroot")) {
1950 show_root
= git_config_bool(var
, value
);
1953 if (!strcmp(var
, "blame.blankboundary")) {
1954 blank_boundary
= git_config_bool(var
, value
);
1957 return git_default_config(var
, value
, cb
);
1961 * Prepare a dummy commit that represents the work tree (or staged) item.
1962 * Note that annotating work tree item never works in the reverse.
1964 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1966 struct commit
*commit
;
1967 struct origin
*origin
;
1968 unsigned char head_sha1
[20];
1969 struct strbuf buf
= STRBUF_INIT
;
1973 struct cache_entry
*ce
;
1976 if (get_sha1("HEAD", head_sha1
))
1977 die("No such ref: HEAD");
1980 commit
= xcalloc(1, sizeof(*commit
));
1981 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
1982 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
1983 commit
->object
.parsed
= 1;
1985 commit
->object
.type
= OBJ_COMMIT
;
1987 origin
= make_origin(commit
, path
);
1989 if (!contents_from
|| strcmp("-", contents_from
)) {
1991 const char *read_from
;
1993 if (contents_from
) {
1994 if (stat(contents_from
, &st
) < 0)
1995 die("Cannot stat %s", contents_from
);
1996 read_from
= contents_from
;
1999 if (lstat(path
, &st
) < 0)
2000 die("Cannot lstat %s", path
);
2003 mode
= canon_mode(st
.st_mode
);
2004 switch (st
.st_mode
& S_IFMT
) {
2006 if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2007 die("cannot open or read %s", read_from
);
2010 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
2011 die("cannot readlink %s", read_from
);
2014 die("unsupported file type %s", read_from
);
2018 /* Reading from stdin */
2019 contents_from
= "standard input";
2021 if (strbuf_read(&buf
, 0, 0) < 0)
2022 die("read error %s from stdin", strerror(errno
));
2024 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2025 origin
->file
.ptr
= buf
.buf
;
2026 origin
->file
.size
= buf
.len
;
2027 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2028 commit
->util
= origin
;
2031 * Read the current index, replace the path entry with
2032 * origin->blob_sha1 without mucking with its mode or type
2033 * bits; we are not going to write this index out -- we just
2034 * want to run "diff-index --cached".
2041 int pos
= cache_name_pos(path
, len
);
2043 mode
= active_cache
[pos
]->ce_mode
;
2045 /* Let's not bother reading from HEAD tree */
2046 mode
= S_IFREG
| 0644;
2048 size
= cache_entry_size(len
);
2049 ce
= xcalloc(1, size
);
2050 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2051 memcpy(ce
->name
, path
, len
);
2052 ce
->ce_flags
= create_ce_flags(len
, 0);
2053 ce
->ce_mode
= create_ce_mode(mode
);
2054 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2057 * We are not going to write this out, so this does not matter
2058 * right now, but someday we might optimize diff-index --cached
2059 * with cache-tree information.
2061 cache_tree_invalidate_path(active_cache_tree
, path
);
2063 commit
->buffer
= xmalloc(400);
2064 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2065 snprintf(commit
->buffer
, 400,
2066 "tree 0000000000000000000000000000000000000000\n"
2070 "Version of %s from %s\n",
2071 sha1_to_hex(head_sha1
),
2072 ident
, ident
, path
, contents_from
? contents_from
: path
);
2076 static const char *prepare_final(struct scoreboard
*sb
)
2079 const char *final_commit_name
= NULL
;
2080 struct rev_info
*revs
= sb
->revs
;
2083 * There must be one and only one positive commit in the
2084 * revs->pending array.
2086 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2087 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2088 if (obj
->flags
& UNINTERESTING
)
2090 while (obj
->type
== OBJ_TAG
)
2091 obj
= deref_tag(obj
, NULL
, 0);
2092 if (obj
->type
!= OBJ_COMMIT
)
2093 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2095 die("More than one commit to dig from %s and %s?",
2096 revs
->pending
.objects
[i
].name
,
2098 sb
->final
= (struct commit
*) obj
;
2099 final_commit_name
= revs
->pending
.objects
[i
].name
;
2101 return final_commit_name
;
2104 static const char *prepare_initial(struct scoreboard
*sb
)
2107 const char *final_commit_name
= NULL
;
2108 struct rev_info
*revs
= sb
->revs
;
2111 * There must be one and only one negative commit, and it must be
2114 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2115 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2116 if (!(obj
->flags
& UNINTERESTING
))
2118 while (obj
->type
== OBJ_TAG
)
2119 obj
= deref_tag(obj
, NULL
, 0);
2120 if (obj
->type
!= OBJ_COMMIT
)
2121 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2123 die("More than one commit to dig down to %s and %s?",
2124 revs
->pending
.objects
[i
].name
,
2126 sb
->final
= (struct commit
*) obj
;
2127 final_commit_name
= revs
->pending
.objects
[i
].name
;
2129 if (!final_commit_name
)
2130 die("No commit to dig down to?");
2131 return final_commit_name
;
2134 static int blame_copy_callback(const struct option
*option
, const char *arg
, int unset
)
2136 int *opt
= option
->value
;
2139 * -C enables copy from removed files;
2140 * -C -C enables copy from existing files, but only
2141 * when blaming a new file;
2142 * -C -C -C enables copy from existing files for
2145 if (*opt
& PICKAXE_BLAME_COPY_HARDER
)
2146 *opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2147 if (*opt
& PICKAXE_BLAME_COPY
)
2148 *opt
|= PICKAXE_BLAME_COPY_HARDER
;
2149 *opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2152 blame_copy_score
= parse_score(arg
);
2156 static int blame_move_callback(const struct option
*option
, const char *arg
, int unset
)
2158 int *opt
= option
->value
;
2160 *opt
|= PICKAXE_BLAME_MOVE
;
2163 blame_move_score
= parse_score(arg
);
2167 static int blame_bottomtop_callback(const struct option
*option
, const char *arg
, int unset
)
2169 const char **bottomtop
= option
->value
;
2173 die("More than one '-L n,m' option given");
2178 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2180 struct rev_info revs
;
2182 struct scoreboard sb
;
2184 struct blame_entry
*ent
;
2185 long dashdash_pos
, bottom
, top
, lno
;
2186 const char *final_commit_name
= NULL
;
2187 enum object_type type
;
2189 static const char *bottomtop
= NULL
;
2190 static int output_option
= 0, opt
= 0;
2191 static int show_stats
= 0;
2192 static const char *revs_file
= NULL
;
2193 static const char *contents_from
= NULL
;
2194 static const struct option options
[] = {
2195 OPT_BOOLEAN(0, "incremental", &incremental
, "Show blame entries as we find them, incrementally"),
2196 OPT_BOOLEAN('b', NULL
, &blank_boundary
, "Show blank SHA-1 for boundary commits (Default: off)"),
2197 OPT_BOOLEAN(0, "root", &show_root
, "Do not treat root commits as boundaries (Default: off)"),
2198 OPT_BOOLEAN(0, "show-stats", &show_stats
, "Show work cost statistics"),
2199 OPT_BIT(0, "score-debug", &output_option
, "Show output score for blame entries", OUTPUT_SHOW_SCORE
),
2200 OPT_BIT('f', "show-name", &output_option
, "Show original filename (Default: auto)", OUTPUT_SHOW_NAME
),
2201 OPT_BIT('n', "show-number", &output_option
, "Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER
),
2202 OPT_BIT('p', "porcelain", &output_option
, "Show in a format designed for machine consumption", OUTPUT_PORCELAIN
),
2203 OPT_BIT('c', NULL
, &output_option
, "Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT
),
2204 OPT_BIT('t', NULL
, &output_option
, "Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP
),
2205 OPT_BIT('l', NULL
, &output_option
, "Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME
),
2206 OPT_BIT('s', NULL
, &output_option
, "Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR
),
2207 OPT_BIT('w', NULL
, &xdl_opts
, "Ignore whitespace differences", XDF_IGNORE_WHITESPACE
),
2208 OPT_STRING('S', NULL
, &revs_file
, "file", "Use revisions from <file> instead of calling git-rev-list"),
2209 OPT_STRING(0, "contents", &contents_from
, "file", "Use <file>'s contents as the final image"),
2210 { OPTION_CALLBACK
, 'C', NULL
, &opt
, "score", "Find line copies within and across files", PARSE_OPT_OPTARG
, blame_copy_callback
},
2211 { OPTION_CALLBACK
, 'M', NULL
, &opt
, "score", "Find line movements within and across files", PARSE_OPT_OPTARG
, blame_move_callback
},
2212 OPT_CALLBACK('L', NULL
, &bottomtop
, "n,m", "Process only line range n,m, counting from 1", blame_bottomtop_callback
),
2216 struct parse_opt_ctx_t ctx
;
2217 int cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2219 git_config(git_blame_config
, NULL
);
2220 init_revisions(&revs
, NULL
);
2221 save_commit_buffer
= 0;
2224 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
2225 PARSE_OPT_KEEP_ARGV0
);
2227 switch (parse_options_step(&ctx
, options
, blame_opt_usage
)) {
2228 case PARSE_OPT_HELP
:
2230 case PARSE_OPT_DONE
:
2232 dashdash_pos
= ctx
.cpidx
;
2236 if (!strcmp(ctx
.argv
[0], "--reverse")) {
2237 ctx
.argv
[0] = "--children";
2240 parse_revision_opt(&revs
, &ctx
, options
, blame_opt_usage
);
2243 argc
= parse_options_end(&ctx
);
2245 if (cmd_is_annotate
)
2246 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2248 if (DIFF_OPT_TST(&revs
.diffopt
, FIND_COPIES_HARDER
))
2249 opt
|= (PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
|
2250 PICKAXE_BLAME_COPY_HARDER
);
2252 if (!blame_move_score
)
2253 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2254 if (!blame_copy_score
)
2255 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2258 * We have collected options unknown to us in argv[1..unk]
2259 * which are to be passed to revision machinery if we are
2260 * going to do the "bottom" processing.
2262 * The remaining are:
2264 * (1) if dashdash_pos != 0, its either
2265 * "blame [revisions] -- <path>" or
2266 * "blame -- <path> <rev>"
2268 * (2) otherwise, its one of the two:
2269 * "blame [revisions] <path>"
2270 * "blame <path> <rev>"
2272 * Note that we must strip out <path> from the arguments: we do not
2273 * want the path pruning but we may want "bottom" processing.
2276 switch (argc
- dashdash_pos
- 1) {
2279 usage_with_options(blame_opt_usage
, options
);
2280 /* reorder for the new way: <rev> -- <path> */
2286 path
= add_prefix(prefix
, argv
[--argc
]);
2290 usage_with_options(blame_opt_usage
, options
);
2294 usage_with_options(blame_opt_usage
, options
);
2295 path
= add_prefix(prefix
, argv
[argc
- 1]);
2296 if (argc
== 3 && !has_string_in_work_tree(path
)) { /* (2b) */
2297 path
= add_prefix(prefix
, argv
[1]);
2300 argv
[argc
- 1] = "--";
2303 if (!has_string_in_work_tree(path
))
2304 die("cannot stat path %s: %s", path
, strerror(errno
));
2307 setup_revisions(argc
, argv
, &revs
, NULL
);
2308 memset(&sb
, 0, sizeof(sb
));
2312 final_commit_name
= prepare_final(&sb
);
2313 else if (contents_from
)
2314 die("--contents and --children do not blend well.");
2316 final_commit_name
= prepare_initial(&sb
);
2320 * "--not A B -- path" without anything positive;
2321 * do not default to HEAD, but use the working tree
2325 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2326 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2328 else if (contents_from
)
2329 die("Cannot use --contents with final commit object name");
2332 * If we have bottom, this will mark the ancestors of the
2333 * bottom commits we would reach while traversing as
2336 if (prepare_revision_walk(&revs
))
2337 die("revision walk setup failed");
2339 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2342 buf
= xmalloc(o
->file
.size
+ 1);
2343 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2345 sb
.final_buf_size
= o
->file
.size
;
2348 o
= get_origin(&sb
, sb
.final
, path
);
2349 if (fill_blob_sha1(o
))
2350 die("no such path %s in %s", path
, final_commit_name
);
2352 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2353 &sb
.final_buf_size
);
2355 die("Cannot read blob %s for path %s",
2356 sha1_to_hex(o
->blob_sha1
),
2360 lno
= prepare_lines(&sb
);
2364 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2365 if (bottom
&& top
&& top
< bottom
) {
2367 tmp
= top
; top
= bottom
; bottom
= tmp
;
2375 die("file %s has only %lu lines", path
, lno
);
2377 ent
= xcalloc(1, sizeof(*ent
));
2379 ent
->num_lines
= top
- bottom
;
2381 ent
->s_lno
= bottom
;
2386 if (revs_file
&& read_ancestry(revs_file
))
2387 die("reading graft file %s failed: %s",
2388 revs_file
, strerror(errno
));
2390 read_mailmap(&mailmap
, NULL
);
2395 assign_blame(&sb
, opt
);
2402 if (!(output_option
& OUTPUT_PORCELAIN
))
2403 find_alignment(&sb
, &output_option
);
2405 output(&sb
, output_option
);
2406 free((void *)sb
.final_buf
);
2407 for (ent
= sb
.ent
; ent
; ) {
2408 struct blame_entry
*e
= ent
->next
;
2414 printf("num read blob: %d\n", num_read_blob
);
2415 printf("num get patch: %d\n", num_get_patch
);
2416 printf("num commits: %d\n", num_commits
);