4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "path-list.h"
22 static char blame_usage
[] =
23 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
24 " -c Use the same output mode as git-annotate (Default: off)\n"
25 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
26 " -l Show long commit SHA1 (Default: off)\n"
27 " --root Do not treat root commits as boundaries (Default: off)\n"
28 " -t Show raw timestamp (Default: off)\n"
29 " -f, --show-name Show original filename (Default: auto)\n"
30 " -n, --show-number Show original linenumber (Default: off)\n"
31 " -s Suppress author name and timestamp (Default: off)\n"
32 " -p, --porcelain Show in a format designed for machine consumption\n"
33 " -w Ignore whitespace differences\n"
34 " -L n,m Process only line range n,m, counting from 1\n"
35 " -M, -C Find line movements within and across files\n"
36 " --incremental Show blame entries as we find them, incrementally\n"
37 " --contents file Use <file>'s contents as the final image\n"
38 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
40 static int longest_file
;
41 static int longest_author
;
42 static int max_orig_digits
;
43 static int max_digits
;
44 static int max_score_digits
;
46 static int blank_boundary
;
47 static int incremental
;
48 static int cmd_is_annotate
;
49 static int xdl_opts
= XDF_NEED_MINIMAL
;
50 static struct path_list mailmap
;
57 static int num_read_blob
;
58 static int num_get_patch
;
59 static int num_commits
;
61 #define PICKAXE_BLAME_MOVE 01
62 #define PICKAXE_BLAME_COPY 02
63 #define PICKAXE_BLAME_COPY_HARDER 04
64 #define PICKAXE_BLAME_COPY_HARDEST 010
67 * blame for a blame_entry with score lower than these thresholds
68 * is not passed to the parent using move/copy logic.
70 static unsigned blame_move_score
;
71 static unsigned blame_copy_score
;
72 #define BLAME_DEFAULT_MOVE_SCORE 20
73 #define BLAME_DEFAULT_COPY_SCORE 40
75 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76 #define METAINFO_SHOWN (1u<<12)
77 #define MORE_THAN_ONE_PATH (1u<<13)
80 * One blob in a commit that is being suspected
84 struct commit
*commit
;
86 unsigned char blob_sha1
[20];
87 char path
[FLEX_ARRAY
];
91 * Given an origin, prepare mmfile_t structure to be used by the
94 static void fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
97 enum object_type type
;
99 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
100 (unsigned long *)(&(file
->size
)));
102 die("Cannot read blob %s for path %s",
103 sha1_to_hex(o
->blob_sha1
),
112 * Origin is refcounted and usually we keep the blob contents to be
115 static inline struct origin
*origin_incref(struct origin
*o
)
122 static void origin_decref(struct origin
*o
)
124 if (o
&& --o
->refcnt
<= 0) {
130 static void drop_origin_blob(struct origin
*o
)
139 * Each group of lines is described by a blame_entry; it can be split
140 * as we pass blame to the parents. They form a linked list in the
141 * scoreboard structure, sorted by the target line number.
144 struct blame_entry
*prev
;
145 struct blame_entry
*next
;
147 /* the first line of this group in the final image;
148 * internally all line numbers are 0 based.
152 /* how many lines this group has */
155 /* the commit that introduced this group into the final image */
156 struct origin
*suspect
;
158 /* true if the suspect is truly guilty; false while we have not
159 * checked if the group came from one of its parents.
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
;
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 * Parsing of patch chunks...
453 /* line number in postimage; up to but not including this
454 * line is the same as preimage
458 /* preimage line number after this chunk */
461 /* postimage line number after this chunk */
466 struct chunk
*chunks
;
470 struct blame_diff_state
{
471 struct xdiff_emit_state xm
;
473 unsigned hunk_post_context
;
474 unsigned hunk_in_pre_context
: 1;
477 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
479 struct blame_diff_state
*state
= state_
;
481 int off1
, off2
, len1
, len2
, num
;
483 num
= state
->ret
->num
;
484 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
485 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
486 state
->ret
->chunks
[num
- 1].same
++;
488 state
->hunk_in_pre_context
= 0;
490 state
->hunk_post_context
++;
492 state
->hunk_post_context
= 0;
497 if (num
&& state
->hunk_post_context
) {
498 chunk
= &state
->ret
->chunks
[num
- 1];
499 chunk
->p_next
-= state
->hunk_post_context
;
500 chunk
->t_next
-= state
->hunk_post_context
;
502 state
->ret
->num
= ++num
;
503 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
504 sizeof(struct chunk
) * num
);
505 chunk
= &state
->ret
->chunks
[num
- 1];
506 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
511 /* Line numbers in patch output are one based. */
515 chunk
->same
= len2
? off2
: (off2
+ 1);
517 chunk
->p_next
= off1
+ (len1
? len1
: 1);
518 chunk
->t_next
= chunk
->same
+ len2
;
519 state
->hunk_in_pre_context
= 1;
520 state
->hunk_post_context
= 0;
523 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
526 struct blame_diff_state state
;
531 xpp
.flags
= xdl_opts
;
532 memset(&xecfg
, 0, sizeof(xecfg
));
533 xecfg
.ctxlen
= context
;
534 ecb
.outf
= xdiff_outf
;
536 memset(&state
, 0, sizeof(state
));
537 state
.xm
.consume
= process_u_diff
;
538 state
.ret
= xmalloc(sizeof(struct patch
));
539 state
.ret
->chunks
= NULL
;
542 xdi_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
544 if (state
.ret
->num
) {
546 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
547 chunk
->p_next
-= state
.hunk_post_context
;
548 chunk
->t_next
-= state
.hunk_post_context
;
554 * Run diff between two origins and grab the patch output, so that
555 * we can pass blame for lines origin is currently suspected for
558 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
560 mmfile_t file_p
, file_o
;
563 fill_origin_blob(parent
, &file_p
);
564 fill_origin_blob(origin
, &file_o
);
565 if (!file_p
.ptr
|| !file_o
.ptr
)
567 patch
= compare_buffer(&file_p
, &file_o
, 0);
572 static void free_patch(struct patch
*p
)
579 * Link in a new blame entry to the scoreboard. Entries that cover the
580 * same line range have been removed from the scoreboard previously.
582 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
584 struct blame_entry
*ent
, *prev
= NULL
;
586 origin_incref(e
->suspect
);
588 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
591 /* prev, if not NULL, is the last one that is below e */
594 e
->next
= prev
->next
;
606 * src typically is on-stack; we want to copy the information in it to
607 * a malloced blame_entry that is already on the linked list of the
608 * scoreboard. The origin of dst loses a refcnt while the origin of src
611 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
613 struct blame_entry
*p
, *n
;
617 origin_incref(src
->suspect
);
618 origin_decref(dst
->suspect
);
619 memcpy(dst
, src
, sizeof(*src
));
625 static const char *nth_line(struct scoreboard
*sb
, int lno
)
627 return sb
->final_buf
+ sb
->lineno
[lno
];
631 * It is known that lines between tlno to same came from parent, and e
632 * has an overlap with that range. it also is known that parent's
633 * line plno corresponds to e's line tlno.
639 * <------------------>
641 * Split e into potentially three parts; before this chunk, the chunk
642 * to be blamed for the parent, and after that portion.
644 static void split_overlap(struct blame_entry
*split
,
645 struct blame_entry
*e
,
646 int tlno
, int plno
, int same
,
647 struct origin
*parent
)
650 memset(split
, 0, sizeof(struct blame_entry
[3]));
652 if (e
->s_lno
< tlno
) {
653 /* there is a pre-chunk part not blamed on parent */
654 split
[0].suspect
= origin_incref(e
->suspect
);
655 split
[0].lno
= e
->lno
;
656 split
[0].s_lno
= e
->s_lno
;
657 split
[0].num_lines
= tlno
- e
->s_lno
;
658 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
659 split
[1].s_lno
= plno
;
662 split
[1].lno
= e
->lno
;
663 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
666 if (same
< e
->s_lno
+ e
->num_lines
) {
667 /* there is a post-chunk part not blamed on parent */
668 split
[2].suspect
= origin_incref(e
->suspect
);
669 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
670 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
671 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
672 chunk_end_lno
= split
[2].lno
;
675 chunk_end_lno
= e
->lno
+ e
->num_lines
;
676 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
679 * if it turns out there is nothing to blame the parent for,
680 * forget about the splitting. !split[1].suspect signals this.
682 if (split
[1].num_lines
< 1)
684 split
[1].suspect
= origin_incref(parent
);
688 * split_overlap() divided an existing blame e into up to three parts
689 * in split. Adjust the linked list of blames in the scoreboard to
692 static void split_blame(struct scoreboard
*sb
,
693 struct blame_entry
*split
,
694 struct blame_entry
*e
)
696 struct blame_entry
*new_entry
;
698 if (split
[0].suspect
&& split
[2].suspect
) {
699 /* The first part (reuse storage for the existing entry e) */
700 dup_entry(e
, &split
[0]);
702 /* The last part -- me */
703 new_entry
= xmalloc(sizeof(*new_entry
));
704 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
705 add_blame_entry(sb
, new_entry
);
707 /* ... and the middle part -- parent */
708 new_entry
= xmalloc(sizeof(*new_entry
));
709 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
710 add_blame_entry(sb
, new_entry
);
712 else if (!split
[0].suspect
&& !split
[2].suspect
)
714 * The parent covers the entire area; reuse storage for
715 * e and replace it with the parent.
717 dup_entry(e
, &split
[1]);
718 else if (split
[0].suspect
) {
719 /* me and then parent */
720 dup_entry(e
, &split
[0]);
722 new_entry
= xmalloc(sizeof(*new_entry
));
723 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
724 add_blame_entry(sb
, new_entry
);
727 /* parent and then me */
728 dup_entry(e
, &split
[1]);
730 new_entry
= xmalloc(sizeof(*new_entry
));
731 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
732 add_blame_entry(sb
, new_entry
);
735 if (DEBUG
) { /* sanity */
736 struct blame_entry
*ent
;
737 int lno
= sb
->ent
->lno
, corrupt
= 0;
739 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
744 lno
+= ent
->num_lines
;
748 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
749 printf("L %8d l %8d n %8d\n",
750 lno
, ent
->lno
, ent
->num_lines
);
751 lno
= ent
->lno
+ ent
->num_lines
;
759 * After splitting the blame, the origins used by the
760 * on-stack blame_entry should lose one refcnt each.
762 static void decref_split(struct blame_entry
*split
)
766 for (i
= 0; i
< 3; i
++)
767 origin_decref(split
[i
].suspect
);
771 * Helper for blame_chunk(). blame_entry e is known to overlap with
772 * the patch hunk; split it and pass blame to the parent.
774 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
775 int tlno
, int plno
, int same
,
776 struct origin
*parent
)
778 struct blame_entry split
[3];
780 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
781 if (split
[1].suspect
)
782 split_blame(sb
, split
, e
);
787 * Find the line number of the last line the target is suspected for.
789 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
791 struct blame_entry
*e
;
792 int last_in_target
= -1;
794 for (e
= sb
->ent
; e
; e
= e
->next
) {
795 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
797 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
798 last_in_target
= e
->s_lno
+ e
->num_lines
;
800 return last_in_target
;
804 * Process one hunk from the patch between the current suspect for
805 * blame_entry e and its parent. Find and split the overlap, and
806 * pass blame to the overlapping part to the parent.
808 static void blame_chunk(struct scoreboard
*sb
,
809 int tlno
, int plno
, int same
,
810 struct origin
*target
, struct origin
*parent
)
812 struct blame_entry
*e
;
814 for (e
= sb
->ent
; e
; e
= e
->next
) {
815 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
817 if (same
<= e
->s_lno
)
819 if (tlno
< e
->s_lno
+ e
->num_lines
)
820 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
825 * We are looking at the origin 'target' and aiming to pass blame
826 * for the lines it is suspected to its parent. Run diff to find
827 * which lines came from parent and pass blame for them.
829 static int pass_blame_to_parent(struct scoreboard
*sb
,
830 struct origin
*target
,
831 struct origin
*parent
)
833 int i
, last_in_target
, plno
, tlno
;
836 last_in_target
= find_last_in_target(sb
, target
);
837 if (last_in_target
< 0)
838 return 1; /* nothing remains for this target */
840 patch
= get_patch(parent
, target
);
842 for (i
= 0; i
< patch
->num
; i
++) {
843 struct chunk
*chunk
= &patch
->chunks
[i
];
845 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
846 plno
= chunk
->p_next
;
847 tlno
= chunk
->t_next
;
849 /* The rest (i.e. anything after tlno) are the same as the parent */
850 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
857 * The lines in blame_entry after splitting blames many times can become
858 * very small and trivial, and at some point it becomes pointless to
859 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
860 * ordinary C program, and it is not worth to say it was copied from
861 * totally unrelated file in the parent.
863 * Compute how trivial the lines in the blame_entry are.
865 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
874 cp
= nth_line(sb
, e
->lno
);
875 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
877 unsigned ch
= *((unsigned char *)cp
);
887 * best_so_far[] and this[] are both a split of an existing blame_entry
888 * that passes blame to the parent. Maintain best_so_far the best split
889 * so far, by comparing this and best_so_far and copying this into
890 * bst_so_far as needed.
892 static void copy_split_if_better(struct scoreboard
*sb
,
893 struct blame_entry
*best_so_far
,
894 struct blame_entry
*this)
898 if (!this[1].suspect
)
900 if (best_so_far
[1].suspect
) {
901 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
905 for (i
= 0; i
< 3; i
++)
906 origin_incref(this[i
].suspect
);
907 decref_split(best_so_far
);
908 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
912 * We are looking at a part of the final image represented by
913 * ent (tlno and same are offset by ent->s_lno).
914 * tlno is where we are looking at in the final image.
915 * up to (but not including) same match preimage.
916 * plno is where we are looking at in the preimage.
918 * <-------------- final image ---------------------->
921 * <---------preimage----->
924 * All line numbers are 0-based.
926 static void handle_split(struct scoreboard
*sb
,
927 struct blame_entry
*ent
,
928 int tlno
, int plno
, int same
,
929 struct origin
*parent
,
930 struct blame_entry
*split
)
932 if (ent
->num_lines
<= tlno
)
935 struct blame_entry
this[3];
938 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
939 copy_split_if_better(sb
, split
, this);
945 * Find the lines from parent that are the same as ent so that
946 * we can pass blames to it. file_p has the blob contents for
949 static void find_copy_in_blob(struct scoreboard
*sb
,
950 struct blame_entry
*ent
,
951 struct origin
*parent
,
952 struct blame_entry
*split
,
962 * Prepare mmfile that contains only the lines in ent.
964 cp
= nth_line(sb
, ent
->lno
);
965 file_o
.ptr
= (char*) cp
;
966 cnt
= ent
->num_lines
;
968 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
972 file_o
.size
= cp
- file_o
.ptr
;
974 patch
= compare_buffer(file_p
, &file_o
, 1);
977 * file_o is a part of final image we are annotating.
978 * file_p partially may match that image.
980 memset(split
, 0, sizeof(struct blame_entry
[3]));
982 for (i
= 0; i
< patch
->num
; i
++) {
983 struct chunk
*chunk
= &patch
->chunks
[i
];
985 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
986 plno
= chunk
->p_next
;
987 tlno
= chunk
->t_next
;
989 /* remainder, if any, all match the preimage */
990 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
995 * See if lines currently target is suspected for can be attributed to
998 static int find_move_in_parent(struct scoreboard
*sb
,
999 struct origin
*target
,
1000 struct origin
*parent
)
1002 int last_in_target
, made_progress
;
1003 struct blame_entry
*e
, split
[3];
1006 last_in_target
= find_last_in_target(sb
, target
);
1007 if (last_in_target
< 0)
1008 return 1; /* nothing remains for this target */
1010 fill_origin_blob(parent
, &file_p
);
1015 while (made_progress
) {
1017 for (e
= sb
->ent
; e
; e
= e
->next
) {
1018 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1020 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1021 if (split
[1].suspect
&&
1022 blame_move_score
< ent_score(sb
, &split
[1])) {
1023 split_blame(sb
, split
, e
);
1026 decref_split(split
);
1033 struct blame_entry
*ent
;
1034 struct blame_entry split
[3];
1038 * Count the number of entries the target is suspected for,
1039 * and prepare a list of entry and the best split.
1041 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1042 struct origin
*target
,
1045 struct blame_entry
*e
;
1047 struct blame_list
*blame_list
= NULL
;
1049 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1050 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1053 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1054 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1055 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1056 blame_list
[i
++].ent
= e
;
1058 *num_ents_p
= num_ents
;
1063 * For lines target is suspected for, see if we can find code movement
1064 * across file boundary from the parent commit. porigin is the path
1065 * in the parent we already tried.
1067 static int find_copy_in_parent(struct scoreboard
*sb
,
1068 struct origin
*target
,
1069 struct commit
*parent
,
1070 struct origin
*porigin
,
1073 struct diff_options diff_opts
;
1074 const char *paths
[1];
1077 struct blame_list
*blame_list
;
1080 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1082 return 1; /* nothing remains for this target */
1084 diff_setup(&diff_opts
);
1085 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1086 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1089 diff_tree_setup_paths(paths
, &diff_opts
);
1090 if (diff_setup_done(&diff_opts
) < 0)
1093 /* Try "find copies harder" on new path if requested;
1094 * we do not want to use diffcore_rename() actually to
1095 * match things up; find_copies_harder is set only to
1096 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1097 * and this code needs to be after diff_setup_done(), which
1098 * usually makes find-copies-harder imply copy detection.
1100 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1101 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1102 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1103 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1105 if (is_null_sha1(target
->commit
->object
.sha1
))
1106 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1108 diff_tree_sha1(parent
->tree
->object
.sha1
,
1109 target
->commit
->tree
->object
.sha1
,
1112 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1113 diffcore_std(&diff_opts
);
1117 int made_progress
= 0;
1119 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1120 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1121 struct origin
*norigin
;
1123 struct blame_entry
this[3];
1125 if (!DIFF_FILE_VALID(p
->one
))
1126 continue; /* does not exist in parent */
1127 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1128 /* find_move already dealt with this path */
1131 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1132 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1133 fill_origin_blob(norigin
, &file_p
);
1137 for (j
= 0; j
< num_ents
; j
++) {
1138 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1139 norigin
, this, &file_p
);
1140 copy_split_if_better(sb
, blame_list
[j
].split
,
1144 origin_decref(norigin
);
1147 for (j
= 0; j
< num_ents
; j
++) {
1148 struct blame_entry
*split
= blame_list
[j
].split
;
1149 if (split
[1].suspect
&&
1150 blame_copy_score
< ent_score(sb
, &split
[1])) {
1151 split_blame(sb
, split
, blame_list
[j
].ent
);
1154 decref_split(split
);
1160 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1166 diff_flush(&diff_opts
);
1167 diff_tree_release_paths(&diff_opts
);
1172 * The blobs of origin and porigin exactly match, so everything
1173 * origin is suspected for can be blamed on the parent.
1175 static void pass_whole_blame(struct scoreboard
*sb
,
1176 struct origin
*origin
, struct origin
*porigin
)
1178 struct blame_entry
*e
;
1180 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1181 /* Steal its file */
1182 porigin
->file
= origin
->file
;
1183 origin
->file
.ptr
= NULL
;
1185 for (e
= sb
->ent
; e
; e
= e
->next
) {
1186 if (!same_suspect(e
->suspect
, origin
))
1188 origin_incref(porigin
);
1189 origin_decref(e
->suspect
);
1190 e
->suspect
= porigin
;
1195 * We pass blame from the current commit to its parents. We keep saying
1196 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1197 * exonerate ourselves.
1199 static struct commit_list
*first_scapegoat(struct commit
*commit
)
1201 return commit
->parents
;
1204 static int num_scapegoats(struct commit
*commit
)
1207 struct commit_list
*l
= first_scapegoat(commit
);
1208 for (cnt
= 0; l
; l
= l
->next
)
1215 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1217 int i
, pass
, num_sg
;
1218 struct commit
*commit
= origin
->commit
;
1219 struct commit_list
*sg
;
1220 struct origin
*sg_buf
[MAXSG
];
1221 struct origin
*porigin
, **sg_origin
= sg_buf
;
1223 num_sg
= num_scapegoats(commit
);
1226 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1227 memset(sg_buf
, 0, sizeof(sg_buf
));
1229 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1232 * The first pass looks for unrenamed path to optimize for
1233 * common cases, then we look for renames in the second pass.
1235 for (pass
= 0; pass
< 2; pass
++) {
1236 struct origin
*(*find
)(struct scoreboard
*,
1237 struct commit
*, struct origin
*);
1238 find
= pass
? find_rename
: find_origin
;
1240 for (i
= 0, sg
= first_scapegoat(commit
);
1242 sg
= sg
->next
, i
++) {
1243 struct commit
*p
= sg
->item
;
1248 if (parse_commit(p
))
1250 porigin
= find(sb
, p
, origin
);
1253 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1254 pass_whole_blame(sb
, origin
, porigin
);
1255 origin_decref(porigin
);
1258 for (j
= same
= 0; j
< i
; j
++)
1260 !hashcmp(sg_origin
[j
]->blob_sha1
,
1261 porigin
->blob_sha1
)) {
1266 sg_origin
[i
] = porigin
;
1268 origin_decref(porigin
);
1273 for (i
= 0, sg
= first_scapegoat(commit
);
1275 sg
= sg
->next
, i
++) {
1276 struct origin
*porigin
= sg_origin
[i
];
1279 if (pass_blame_to_parent(sb
, origin
, porigin
))
1284 * Optionally find moves in parents' files.
1286 if (opt
& PICKAXE_BLAME_MOVE
)
1287 for (i
= 0, sg
= first_scapegoat(commit
);
1289 sg
= sg
->next
, i
++) {
1290 struct origin
*porigin
= sg_origin
[i
];
1293 if (find_move_in_parent(sb
, origin
, porigin
))
1298 * Optionally find copies from parents' files.
1300 if (opt
& PICKAXE_BLAME_COPY
)
1301 for (i
= 0, sg
= first_scapegoat(commit
);
1303 sg
= sg
->next
, i
++) {
1304 struct origin
*porigin
= sg_origin
[i
];
1305 if (find_copy_in_parent(sb
, origin
, sg
->item
,
1311 for (i
= 0; i
< num_sg
; i
++) {
1313 drop_origin_blob(sg_origin
[i
]);
1314 origin_decref(sg_origin
[i
]);
1317 drop_origin_blob(origin
);
1318 if (sg_buf
!= sg_origin
)
1323 * Information on commits, used for output.
1328 const char *author_mail
;
1329 unsigned long author_time
;
1330 const char *author_tz
;
1332 /* filled only when asked for details */
1333 const char *committer
;
1334 const char *committer_mail
;
1335 unsigned long committer_time
;
1336 const char *committer_tz
;
1338 const char *summary
;
1342 * Parse author/committer line in the commit object buffer
1344 static void get_ac_line(const char *inbuf
, const char *what
,
1345 int bufsz
, char *person
, const char **mail
,
1346 unsigned long *time
, const char **tz
)
1348 int len
, tzlen
, maillen
;
1349 char *tmp
, *endp
, *timepos
;
1351 tmp
= strstr(inbuf
, what
);
1354 tmp
+= strlen(what
);
1355 endp
= strchr(tmp
, '\n');
1363 *mail
= *tz
= "(unknown)";
1367 memcpy(person
, tmp
, len
);
1375 tzlen
= (person
+len
)-(tmp
+1);
1380 *time
= strtoul(tmp
, NULL
, 10);
1388 maillen
= timepos
- tmp
;
1394 * mailmap expansion may make the name longer.
1395 * make room by pushing stuff down.
1397 tmp
= person
+ bufsz
- (tzlen
+ 1);
1398 memmove(tmp
, *tz
, tzlen
);
1402 tmp
= tmp
- (maillen
+ 1);
1403 memmove(tmp
, *mail
, maillen
);
1408 * Now, convert e-mail using mailmap
1410 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1413 static void get_commit_info(struct commit
*commit
,
1414 struct commit_info
*ret
,
1419 static char author_buf
[1024];
1420 static char committer_buf
[1024];
1421 static char summary_buf
[1024];
1424 * We've operated without save_commit_buffer, so
1425 * we now need to populate them for output.
1427 if (!commit
->buffer
) {
1428 enum object_type type
;
1431 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1432 if (!commit
->buffer
)
1433 die("Cannot read commit %s",
1434 sha1_to_hex(commit
->object
.sha1
));
1436 ret
->author
= author_buf
;
1437 get_ac_line(commit
->buffer
, "\nauthor ",
1438 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1439 &ret
->author_time
, &ret
->author_tz
);
1444 ret
->committer
= committer_buf
;
1445 get_ac_line(commit
->buffer
, "\ncommitter ",
1446 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1447 &ret
->committer_time
, &ret
->committer_tz
);
1449 ret
->summary
= summary_buf
;
1450 tmp
= strstr(commit
->buffer
, "\n\n");
1453 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1457 endp
= strchr(tmp
, '\n');
1459 endp
= tmp
+ strlen(tmp
);
1461 if (len
>= sizeof(summary_buf
) || len
== 0)
1463 memcpy(summary_buf
, tmp
, len
);
1464 summary_buf
[len
] = 0;
1468 * To allow LF and other nonportable characters in pathnames,
1469 * they are c-style quoted as needed.
1471 static void write_filename_info(const char *path
)
1473 printf("filename ");
1474 write_name_quoted(path
, stdout
, '\n');
1478 * The blame_entry is found to be guilty for the range. Mark it
1479 * as such, and show it in incremental output.
1481 static void found_guilty_entry(struct blame_entry
*ent
)
1487 struct origin
*suspect
= ent
->suspect
;
1489 printf("%s %d %d %d\n",
1490 sha1_to_hex(suspect
->commit
->object
.sha1
),
1491 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1492 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1493 struct commit_info ci
;
1494 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1495 get_commit_info(suspect
->commit
, &ci
, 1);
1496 printf("author %s\n", ci
.author
);
1497 printf("author-mail %s\n", ci
.author_mail
);
1498 printf("author-time %lu\n", ci
.author_time
);
1499 printf("author-tz %s\n", ci
.author_tz
);
1500 printf("committer %s\n", ci
.committer
);
1501 printf("committer-mail %s\n", ci
.committer_mail
);
1502 printf("committer-time %lu\n", ci
.committer_time
);
1503 printf("committer-tz %s\n", ci
.committer_tz
);
1504 printf("summary %s\n", ci
.summary
);
1505 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1506 printf("boundary\n");
1508 write_filename_info(suspect
->path
);
1509 maybe_flush_or_die(stdout
, "stdout");
1514 * The main loop -- while the scoreboard has lines whose true origin
1515 * is still unknown, pick one blame_entry, and allow its current
1516 * suspect to pass blames to its parents.
1518 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1521 struct blame_entry
*ent
;
1522 struct commit
*commit
;
1523 struct origin
*suspect
= NULL
;
1525 /* find one suspect to break down */
1526 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1528 suspect
= ent
->suspect
;
1530 return; /* all done */
1533 * We will use this suspect later in the loop,
1534 * so hold onto it in the meantime.
1536 origin_incref(suspect
);
1537 commit
= suspect
->commit
;
1538 if (!commit
->object
.parsed
)
1539 parse_commit(commit
);
1540 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1541 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1542 pass_blame(sb
, suspect
, opt
);
1544 commit
->object
.flags
|= UNINTERESTING
;
1545 if (commit
->object
.parsed
)
1546 mark_parents_uninteresting(commit
);
1548 /* treat root commit as boundary */
1549 if (!commit
->parents
&& !show_root
)
1550 commit
->object
.flags
|= UNINTERESTING
;
1552 /* Take responsibility for the remaining entries */
1553 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1554 if (same_suspect(ent
->suspect
, suspect
))
1555 found_guilty_entry(ent
);
1556 origin_decref(suspect
);
1558 if (DEBUG
) /* sanity */
1559 sanity_check_refcnt(sb
);
1563 static const char *format_time(unsigned long time
, const char *tz_str
,
1566 static char time_buf
[128];
1571 if (show_raw_time
) {
1572 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1577 minutes
= tz
< 0 ? -tz
: tz
;
1578 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1579 minutes
= tz
< 0 ? -minutes
: minutes
;
1580 t
= time
+ minutes
* 60;
1583 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1584 strcat(time_buf
, tz_str
);
1588 #define OUTPUT_ANNOTATE_COMPAT 001
1589 #define OUTPUT_LONG_OBJECT_NAME 002
1590 #define OUTPUT_RAW_TIMESTAMP 004
1591 #define OUTPUT_PORCELAIN 010
1592 #define OUTPUT_SHOW_NAME 020
1593 #define OUTPUT_SHOW_NUMBER 040
1594 #define OUTPUT_SHOW_SCORE 0100
1595 #define OUTPUT_NO_AUTHOR 0200
1597 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1601 struct origin
*suspect
= ent
->suspect
;
1604 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1605 printf("%s%c%d %d %d\n",
1607 ent
->guilty
? ' ' : '*', // purely for debugging
1611 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1612 struct commit_info ci
;
1613 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1614 get_commit_info(suspect
->commit
, &ci
, 1);
1615 printf("author %s\n", ci
.author
);
1616 printf("author-mail %s\n", ci
.author_mail
);
1617 printf("author-time %lu\n", ci
.author_time
);
1618 printf("author-tz %s\n", ci
.author_tz
);
1619 printf("committer %s\n", ci
.committer
);
1620 printf("committer-mail %s\n", ci
.committer_mail
);
1621 printf("committer-time %lu\n", ci
.committer_time
);
1622 printf("committer-tz %s\n", ci
.committer_tz
);
1623 write_filename_info(suspect
->path
);
1624 printf("summary %s\n", ci
.summary
);
1625 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1626 printf("boundary\n");
1628 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1629 write_filename_info(suspect
->path
);
1631 cp
= nth_line(sb
, ent
->lno
);
1632 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1635 printf("%s %d %d\n", hex
,
1636 ent
->s_lno
+ 1 + cnt
,
1637 ent
->lno
+ 1 + cnt
);
1642 } while (ch
!= '\n' &&
1643 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1647 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1651 struct origin
*suspect
= ent
->suspect
;
1652 struct commit_info ci
;
1654 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1656 get_commit_info(suspect
->commit
, &ci
, 1);
1657 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1659 cp
= nth_line(sb
, ent
->lno
);
1660 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1662 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1664 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1666 memset(hex
, ' ', length
);
1667 else if (!cmd_is_annotate
) {
1673 printf("%.*s", length
, hex
);
1674 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1675 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1676 format_time(ci
.author_time
, ci
.author_tz
,
1678 ent
->lno
+ 1 + cnt
);
1680 if (opt
& OUTPUT_SHOW_SCORE
)
1682 max_score_digits
, ent
->score
,
1683 ent
->suspect
->refcnt
);
1684 if (opt
& OUTPUT_SHOW_NAME
)
1685 printf(" %-*.*s", longest_file
, longest_file
,
1687 if (opt
& OUTPUT_SHOW_NUMBER
)
1688 printf(" %*d", max_orig_digits
,
1689 ent
->s_lno
+ 1 + cnt
);
1691 if (!(opt
& OUTPUT_NO_AUTHOR
))
1692 printf(" (%-*.*s %10s",
1693 longest_author
, longest_author
,
1695 format_time(ci
.author_time
,
1699 max_digits
, ent
->lno
+ 1 + cnt
);
1704 } while (ch
!= '\n' &&
1705 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1709 static void output(struct scoreboard
*sb
, int option
)
1711 struct blame_entry
*ent
;
1713 if (option
& OUTPUT_PORCELAIN
) {
1714 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1715 struct blame_entry
*oth
;
1716 struct origin
*suspect
= ent
->suspect
;
1717 struct commit
*commit
= suspect
->commit
;
1718 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1720 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1721 if ((oth
->suspect
->commit
!= commit
) ||
1722 !strcmp(oth
->suspect
->path
, suspect
->path
))
1724 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1730 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1731 if (option
& OUTPUT_PORCELAIN
)
1732 emit_porcelain(sb
, ent
);
1734 emit_other(sb
, ent
, option
);
1740 * To allow quick access to the contents of nth line in the
1741 * final image, prepare an index in the scoreboard.
1743 static int prepare_lines(struct scoreboard
*sb
)
1745 const char *buf
= sb
->final_buf
;
1746 unsigned long len
= sb
->final_buf_size
;
1747 int num
= 0, incomplete
= 0, bol
= 1;
1749 if (len
&& buf
[len
-1] != '\n')
1750 incomplete
++; /* incomplete line at the end */
1753 sb
->lineno
= xrealloc(sb
->lineno
,
1754 sizeof(int* ) * (num
+ 1));
1755 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1758 if (*buf
++ == '\n') {
1763 sb
->lineno
= xrealloc(sb
->lineno
,
1764 sizeof(int* ) * (num
+ incomplete
+ 1));
1765 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1766 sb
->num_lines
= num
+ incomplete
;
1767 return sb
->num_lines
;
1771 * Add phony grafts for use with -S; this is primarily to
1772 * support git-cvsserver that wants to give a linear history
1775 static int read_ancestry(const char *graft_file
)
1777 FILE *fp
= fopen(graft_file
, "r");
1781 while (fgets(buf
, sizeof(buf
), fp
)) {
1782 /* The format is just "Commit Parent1 Parent2 ...\n" */
1783 int len
= strlen(buf
);
1784 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1786 register_commit_graft(graft
, 0);
1793 * How many columns do we need to show line numbers in decimal?
1795 static int lineno_width(int lines
)
1799 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1805 * How many columns do we need to show line numbers, authors,
1808 static void find_alignment(struct scoreboard
*sb
, int *option
)
1810 int longest_src_lines
= 0;
1811 int longest_dst_lines
= 0;
1812 unsigned largest_score
= 0;
1813 struct blame_entry
*e
;
1815 for (e
= sb
->ent
; e
; e
= e
->next
) {
1816 struct origin
*suspect
= e
->suspect
;
1817 struct commit_info ci
;
1820 if (strcmp(suspect
->path
, sb
->path
))
1821 *option
|= OUTPUT_SHOW_NAME
;
1822 num
= strlen(suspect
->path
);
1823 if (longest_file
< num
)
1825 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1826 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1827 get_commit_info(suspect
->commit
, &ci
, 1);
1828 num
= strlen(ci
.author
);
1829 if (longest_author
< num
)
1830 longest_author
= num
;
1832 num
= e
->s_lno
+ e
->num_lines
;
1833 if (longest_src_lines
< num
)
1834 longest_src_lines
= num
;
1835 num
= e
->lno
+ e
->num_lines
;
1836 if (longest_dst_lines
< num
)
1837 longest_dst_lines
= num
;
1838 if (largest_score
< ent_score(sb
, e
))
1839 largest_score
= ent_score(sb
, e
);
1841 max_orig_digits
= lineno_width(longest_src_lines
);
1842 max_digits
= lineno_width(longest_dst_lines
);
1843 max_score_digits
= lineno_width(largest_score
);
1847 * For debugging -- origin is refcounted, and this asserts that
1848 * we do not underflow.
1850 static void sanity_check_refcnt(struct scoreboard
*sb
)
1853 struct blame_entry
*ent
;
1855 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1856 /* Nobody should have zero or negative refcnt */
1857 if (ent
->suspect
->refcnt
<= 0) {
1858 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1860 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1861 ent
->suspect
->refcnt
);
1865 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1866 /* Mark the ones that haven't been checked */
1867 if (0 < ent
->suspect
->refcnt
)
1868 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1870 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1872 * ... then pick each and see if they have the the
1876 struct blame_entry
*e
;
1877 struct origin
*suspect
= ent
->suspect
;
1879 if (0 < suspect
->refcnt
)
1881 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1882 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1883 if (e
->suspect
!= suspect
)
1887 if (suspect
->refcnt
!= found
) {
1888 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1890 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1891 ent
->suspect
->refcnt
, found
);
1897 find_alignment(sb
, &opt
);
1899 die("Baa %d!", baa
);
1904 * Used for the command line parsing; check if the path exists
1905 * in the working tree.
1907 static int has_path_in_work_tree(const char *path
)
1910 return !lstat(path
, &st
);
1913 static unsigned parse_score(const char *arg
)
1916 unsigned long score
= strtoul(arg
, &end
, 10);
1922 static const char *add_prefix(const char *prefix
, const char *path
)
1924 return prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
1928 * Parsing of (comma separated) one item in the -L option
1930 static const char *parse_loc(const char *spec
,
1931 struct scoreboard
*sb
, long lno
,
1932 long begin
, long *ret
)
1939 regmatch_t match
[1];
1941 /* Allow "-L <something>,+20" to mean starting at <something>
1942 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1945 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1946 num
= strtol(spec
+ 1, &term
, 10);
1947 if (term
!= spec
+ 1) {
1951 *ret
= begin
+ num
- 2;
1960 num
= strtol(spec
, &term
, 10);
1968 /* it could be a regexp of form /.../ */
1969 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1976 /* try [spec+1 .. term-1] as regexp */
1978 begin
--; /* input is in human terms */
1979 line
= nth_line(sb
, begin
);
1981 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1982 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1983 const char *cp
= line
+ match
[0].rm_so
;
1986 while (begin
++ < lno
) {
1987 nline
= nth_line(sb
, begin
);
1988 if (line
<= cp
&& cp
< nline
)
1999 regerror(reg_error
, ®exp
, errbuf
, 1024);
2000 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
2005 * Parsing of -L option
2007 static void prepare_blame_range(struct scoreboard
*sb
,
2008 const char *bottomtop
,
2010 long *bottom
, long *top
)
2014 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
2016 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
2024 static int git_blame_config(const char *var
, const char *value
)
2026 if (!strcmp(var
, "blame.showroot")) {
2027 show_root
= git_config_bool(var
, value
);
2030 if (!strcmp(var
, "blame.blankboundary")) {
2031 blank_boundary
= git_config_bool(var
, value
);
2034 return git_default_config(var
, value
);
2038 * Prepare a dummy commit that represents the work tree (or staged) item.
2039 * Note that annotating work tree item never works in the reverse.
2041 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2043 struct commit
*commit
;
2044 struct origin
*origin
;
2045 unsigned char head_sha1
[20];
2050 struct cache_entry
*ce
;
2053 if (get_sha1("HEAD", head_sha1
))
2054 die("No such ref: HEAD");
2057 commit
= xcalloc(1, sizeof(*commit
));
2058 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2059 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2060 commit
->object
.parsed
= 1;
2062 commit
->object
.type
= OBJ_COMMIT
;
2064 origin
= make_origin(commit
, path
);
2066 strbuf_init(&buf
, 0);
2067 if (!contents_from
|| strcmp("-", contents_from
)) {
2069 const char *read_from
;
2070 unsigned long fin_size
;
2072 if (contents_from
) {
2073 if (stat(contents_from
, &st
) < 0)
2074 die("Cannot stat %s", contents_from
);
2075 read_from
= contents_from
;
2078 if (lstat(path
, &st
) < 0)
2079 die("Cannot lstat %s", path
);
2082 fin_size
= xsize_t(st
.st_size
);
2083 mode
= canon_mode(st
.st_mode
);
2084 switch (st
.st_mode
& S_IFMT
) {
2086 if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
2087 die("cannot open or read %s", read_from
);
2090 if (readlink(read_from
, buf
.buf
, buf
.alloc
) != fin_size
)
2091 die("cannot readlink %s", read_from
);
2095 die("unsupported file type %s", read_from
);
2099 /* Reading from stdin */
2100 contents_from
= "standard input";
2102 if (strbuf_read(&buf
, 0, 0) < 0)
2103 die("read error %s from stdin", strerror(errno
));
2105 convert_to_git(path
, buf
.buf
, buf
.len
, &buf
, 0);
2106 origin
->file
.ptr
= buf
.buf
;
2107 origin
->file
.size
= buf
.len
;
2108 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_sha1
);
2109 commit
->util
= origin
;
2112 * Read the current index, replace the path entry with
2113 * origin->blob_sha1 without mucking with its mode or type
2114 * bits; we are not going to write this index out -- we just
2115 * want to run "diff-index --cached".
2122 int pos
= cache_name_pos(path
, len
);
2124 mode
= active_cache
[pos
]->ce_mode
;
2126 /* Let's not bother reading from HEAD tree */
2127 mode
= S_IFREG
| 0644;
2129 size
= cache_entry_size(len
);
2130 ce
= xcalloc(1, size
);
2131 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2132 memcpy(ce
->name
, path
, len
);
2133 ce
->ce_flags
= create_ce_flags(len
, 0);
2134 ce
->ce_mode
= create_ce_mode(mode
);
2135 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2138 * We are not going to write this out, so this does not matter
2139 * right now, but someday we might optimize diff-index --cached
2140 * with cache-tree information.
2142 cache_tree_invalidate_path(active_cache_tree
, path
);
2144 commit
->buffer
= xmalloc(400);
2145 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2146 snprintf(commit
->buffer
, 400,
2147 "tree 0000000000000000000000000000000000000000\n"
2151 "Version of %s from %s\n",
2152 sha1_to_hex(head_sha1
),
2153 ident
, ident
, path
, contents_from
? contents_from
: path
);
2157 static const char *prepare_final(struct scoreboard
*sb
, struct rev_info
*revs
)
2160 const char *final_commit_name
= NULL
;
2163 * There must be one and only one positive commit in the
2164 * revs->pending array.
2166 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2167 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2168 if (obj
->flags
& UNINTERESTING
)
2170 while (obj
->type
== OBJ_TAG
)
2171 obj
= deref_tag(obj
, NULL
, 0);
2172 if (obj
->type
!= OBJ_COMMIT
)
2173 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2175 die("More than one commit to dig from %s and %s?",
2176 revs
->pending
.objects
[i
].name
,
2178 sb
->final
= (struct commit
*) obj
;
2179 final_commit_name
= revs
->pending
.objects
[i
].name
;
2181 return final_commit_name
;
2184 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2186 struct rev_info revs
;
2188 struct scoreboard sb
;
2190 struct blame_entry
*ent
;
2191 int i
, seen_dashdash
, unk
, opt
;
2192 long bottom
, top
, lno
;
2193 int output_option
= 0;
2195 const char *revs_file
= NULL
;
2196 const char *final_commit_name
= NULL
;
2197 enum object_type type
;
2198 const char *bottomtop
= NULL
;
2199 const char *contents_from
= NULL
;
2201 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2203 git_config(git_blame_config
);
2204 save_commit_buffer
= 0;
2208 for (unk
= i
= 1; i
< argc
; i
++) {
2209 const char *arg
= argv
[i
];
2212 else if (!strcmp("-b", arg
))
2214 else if (!strcmp("--root", arg
))
2216 else if (!strcmp(arg
, "--show-stats"))
2218 else if (!strcmp("-c", arg
))
2219 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2220 else if (!strcmp("-t", arg
))
2221 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2222 else if (!strcmp("-l", arg
))
2223 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2224 else if (!strcmp("-s", arg
))
2225 output_option
|= OUTPUT_NO_AUTHOR
;
2226 else if (!strcmp("-w", arg
))
2227 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2228 else if (!strcmp("-S", arg
) && ++i
< argc
)
2229 revs_file
= argv
[i
];
2230 else if (!prefixcmp(arg
, "-M")) {
2231 opt
|= PICKAXE_BLAME_MOVE
;
2232 blame_move_score
= parse_score(arg
+2);
2234 else if (!prefixcmp(arg
, "-C")) {
2236 * -C enables copy from removed files;
2237 * -C -C enables copy from existing files, but only
2238 * when blaming a new file;
2239 * -C -C -C enables copy from existing files for
2242 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2243 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2244 if (opt
& PICKAXE_BLAME_COPY
)
2245 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2246 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2247 blame_copy_score
= parse_score(arg
+2);
2249 else if (!prefixcmp(arg
, "-L")) {
2258 die("More than one '-L n,m' option given");
2261 else if (!strcmp("--contents", arg
)) {
2264 contents_from
= argv
[i
];
2266 else if (!strcmp("--incremental", arg
))
2268 else if (!strcmp("--score-debug", arg
))
2269 output_option
|= OUTPUT_SHOW_SCORE
;
2270 else if (!strcmp("-f", arg
) ||
2271 !strcmp("--show-name", arg
))
2272 output_option
|= OUTPUT_SHOW_NAME
;
2273 else if (!strcmp("-n", arg
) ||
2274 !strcmp("--show-number", arg
))
2275 output_option
|= OUTPUT_SHOW_NUMBER
;
2276 else if (!strcmp("-p", arg
) ||
2277 !strcmp("--porcelain", arg
))
2278 output_option
|= OUTPUT_PORCELAIN
;
2279 else if (!strcmp("--", arg
)) {
2288 if (!blame_move_score
)
2289 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2290 if (!blame_copy_score
)
2291 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2294 * We have collected options unknown to us in argv[1..unk]
2295 * which are to be passed to revision machinery if we are
2296 * going to do the "bottom" processing.
2298 * The remaining are:
2300 * (1) if seen_dashdash, its either
2301 * "-options -- <path>" or
2302 * "-options -- <path> <rev>".
2303 * but the latter is allowed only if there is no
2304 * options that we passed to revision machinery.
2306 * (2) otherwise, we may have "--" somewhere later and
2307 * might be looking at the first one of multiple 'rev'
2308 * parameters (e.g. " master ^next ^maint -- path").
2309 * See if there is a dashdash first, and give the
2310 * arguments before that to revision machinery.
2311 * After that there must be one 'path'.
2313 * (3) otherwise, its one of the three:
2314 * "-options <path> <rev>"
2315 * "-options <rev> <path>"
2317 * but again the first one is allowed only if
2318 * there is no options that we passed to revision
2322 if (seen_dashdash
) {
2326 path
= add_prefix(prefix
, argv
[i
]);
2327 if (i
+ 1 == argc
- 1) {
2330 argv
[unk
++] = argv
[i
+ 1];
2332 else if (i
+ 1 != argc
)
2333 /* garbage at end */
2338 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2339 if (!strcmp(argv
[j
], "--"))
2341 if (seen_dashdash
) {
2343 if (seen_dashdash
+ 1 != argc
- 1)
2345 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2346 for (j
= i
; j
< seen_dashdash
; j
++)
2347 argv
[unk
++] = argv
[j
];
2353 path
= add_prefix(prefix
, argv
[i
]);
2354 if (i
+ 1 == argc
- 1) {
2355 final_commit_name
= argv
[i
+ 1];
2357 /* if (unk == 1) we could be getting
2360 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2361 path
= add_prefix(prefix
, argv
[i
+ 1]);
2362 final_commit_name
= argv
[i
];
2365 else if (i
!= argc
- 1)
2366 usage(blame_usage
); /* garbage at end */
2369 if (!has_path_in_work_tree(path
))
2370 die("cannot stat path %s: %s",
2371 path
, strerror(errno
));
2375 if (final_commit_name
)
2376 argv
[unk
++] = final_commit_name
;
2379 * Now we got rev and path. We do not want the path pruning
2380 * but we may want "bottom" processing.
2382 argv
[unk
++] = "--"; /* terminate the rev name */
2385 init_revisions(&revs
, NULL
);
2386 setup_revisions(unk
, argv
, &revs
, NULL
);
2387 memset(&sb
, 0, sizeof(sb
));
2389 final_commit_name
= prepare_final(&sb
, &revs
);
2392 * "--not A B -- path" without anything positive;
2393 * do not default to HEAD, but use the working tree
2397 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2398 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2400 else if (contents_from
)
2401 die("Cannot use --contents with final commit object name");
2404 * If we have bottom, this will mark the ancestors of the
2405 * bottom commits we would reach while traversing as
2408 if (prepare_revision_walk(&revs
))
2409 die("revision walk setup failed");
2411 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2414 buf
= xmalloc(o
->file
.size
+ 1);
2415 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2417 sb
.final_buf_size
= o
->file
.size
;
2420 o
= get_origin(&sb
, sb
.final
, path
);
2421 if (fill_blob_sha1(o
))
2422 die("no such path %s in %s", path
, final_commit_name
);
2424 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2425 &sb
.final_buf_size
);
2427 die("Cannot read blob %s for path %s",
2428 sha1_to_hex(o
->blob_sha1
),
2432 lno
= prepare_lines(&sb
);
2436 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2437 if (bottom
&& top
&& top
< bottom
) {
2439 tmp
= top
; top
= bottom
; bottom
= tmp
;
2447 die("file %s has only %lu lines", path
, lno
);
2449 ent
= xcalloc(1, sizeof(*ent
));
2451 ent
->num_lines
= top
- bottom
;
2453 ent
->s_lno
= bottom
;
2458 if (revs_file
&& read_ancestry(revs_file
))
2459 die("reading graft file %s failed: %s",
2460 revs_file
, strerror(errno
));
2462 read_mailmap(&mailmap
, ".mailmap", NULL
);
2467 assign_blame(&sb
, &revs
, opt
);
2474 if (!(output_option
& OUTPUT_PORCELAIN
))
2475 find_alignment(&sb
, &output_option
);
2477 output(&sb
, output_option
);
2478 free((void *)sb
.final_buf
);
2479 for (ent
= sb
.ent
; ent
; ) {
2480 struct blame_entry
*e
= ent
->next
;
2486 printf("num read blob: %d\n", num_read_blob
);
2487 printf("num get patch: %d\n", num_get_patch
);
2488 printf("num commits: %d\n", num_commits
);