4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
20 static char blame_usage
[] =
21 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
22 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
23 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
24 " -l, --long Show long commit SHA1 (Default: off)\n"
25 " --root Do not treat root commits as boundaries (Default: off)\n"
26 " -t, --time Show raw timestamp (Default: off)\n"
27 " -f, --show-name Show original filename (Default: auto)\n"
28 " -n, --show-number Show original linenumber (Default: off)\n"
29 " -p, --porcelain Show in a format designed for machine consumption\n"
30 " -L n,m Process only line range n,m, counting from 1\n"
31 " -M, -C Find line movements within and across files\n"
32 " --incremental Show blame entries as we find them, incrementally\n"
33 " --contents file Use <file>'s contents as the final image\n"
34 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
36 static int longest_file
;
37 static int longest_author
;
38 static int max_orig_digits
;
39 static int max_digits
;
40 static int max_score_digits
;
42 static int blank_boundary
;
43 static int incremental
;
44 static int cmd_is_annotate
;
51 static int num_read_blob
;
52 static int num_get_patch
;
53 static int num_commits
;
55 #define PICKAXE_BLAME_MOVE 01
56 #define PICKAXE_BLAME_COPY 02
57 #define PICKAXE_BLAME_COPY_HARDER 04
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 commit
*commit
;
79 unsigned char blob_sha1
[20];
80 char path
[FLEX_ARRAY
];
84 * Given an origin, prepare mmfile_t structure to be used by the
87 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
90 enum object_type type
;
92 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
93 (unsigned long *)(&(file
->size
)));
102 * Origin is refcounted and usually we keep the blob contents to be
105 static inline struct origin
*origin_incref(struct origin
*o
)
112 static void origin_decref(struct origin
*o
)
114 if (o
&& --o
->refcnt
<= 0) {
117 memset(o
, 0, sizeof(*o
));
123 * Each group of lines is described by a blame_entry; it can be split
124 * as we pass blame to the parents. They form a linked list in the
125 * scoreboard structure, sorted by the target line number.
128 struct blame_entry
*prev
;
129 struct blame_entry
*next
;
131 /* the first line of this group in the final image;
132 * internally all line numbers are 0 based.
136 /* how many lines this group has */
139 /* the commit that introduced this group into the final image */
140 struct origin
*suspect
;
142 /* true if the suspect is truly guilty; false while we have not
143 * checked if the group came from one of its parents.
147 /* the line number of the first line of this group in the
148 * suspect's file; internally all line numbers are 0 based.
152 /* how significant this entry is -- cached to avoid
153 * scanning the lines over and over.
159 * The current state of the blame assignment.
162 /* the final commit (i.e. where we started digging from) */
163 struct commit
*final
;
168 * The contents in the final image.
169 * Used by many functions to obtain contents of the nth line,
170 * indexed with scoreboard.lineno[blame_entry.lno].
172 const char *final_buf
;
173 unsigned long final_buf_size
;
175 /* linked list of blames */
176 struct blame_entry
*ent
;
178 /* look-up a line in the final buffer */
183 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
185 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
188 return strcmp(a
->path
, b
->path
);
191 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
193 static void sanity_check_refcnt(struct scoreboard
*);
196 * If two blame entries that are next to each other came from
197 * contiguous lines in the same origin (i.e. <commit, path> pair),
198 * merge them together.
200 static void coalesce(struct scoreboard
*sb
)
202 struct blame_entry
*ent
, *next
;
204 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
205 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
206 ent
->guilty
== next
->guilty
&&
207 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
208 ent
->num_lines
+= next
->num_lines
;
209 ent
->next
= next
->next
;
211 ent
->next
->prev
= ent
;
212 origin_decref(next
->suspect
);
215 next
= ent
; /* again */
219 if (DEBUG
) /* sanity */
220 sanity_check_refcnt(sb
);
224 * Given a commit and a path in it, create a new origin structure.
225 * The callers that add blame to the scoreboard should use
226 * get_origin() to obtain shared, refcounted copy instead of calling
227 * this function directly.
229 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
232 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
235 strcpy(o
->path
, path
);
240 * Locate an existing origin or create a new one.
242 static struct origin
*get_origin(struct scoreboard
*sb
,
243 struct commit
*commit
,
246 struct blame_entry
*e
;
248 for (e
= sb
->ent
; e
; e
= e
->next
) {
249 if (e
->suspect
->commit
== commit
&&
250 !strcmp(e
->suspect
->path
, path
))
251 return origin_incref(e
->suspect
);
253 return make_origin(commit
, path
);
257 * Fill the blob_sha1 field of an origin if it hasn't, so that later
258 * call to fill_origin_blob() can use it to locate the data. blob_sha1
259 * for an origin is also used to pass the blame for the entire file to
260 * the parent to detect the case where a child's blob is identical to
261 * that of its parent's.
263 static int fill_blob_sha1(struct origin
*origin
)
267 if (!is_null_sha1(origin
->blob_sha1
))
269 if (get_tree_entry(origin
->commit
->object
.sha1
,
271 origin
->blob_sha1
, &mode
))
273 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
277 hashclr(origin
->blob_sha1
);
282 * We have an origin -- check if the same path exists in the
283 * parent and return an origin structure to represent it.
285 static struct origin
*find_origin(struct scoreboard
*sb
,
286 struct commit
*parent
,
287 struct origin
*origin
)
289 struct origin
*porigin
= NULL
;
290 struct diff_options diff_opts
;
291 const char *paths
[2];
295 * Each commit object can cache one origin in that
296 * commit. This is a freestanding copy of origin and
299 struct origin
*cached
= parent
->util
;
300 if (!strcmp(cached
->path
, origin
->path
)) {
302 * The same path between origin and its parent
303 * without renaming -- the most common case.
305 porigin
= get_origin(sb
, parent
, cached
->path
);
308 * If the origin was newly created (i.e. get_origin
309 * would call make_origin if none is found in the
310 * scoreboard), it does not know the blob_sha1,
311 * so copy it. Otherwise porigin was in the
312 * scoreboard and already knows blob_sha1.
314 if (porigin
->refcnt
== 1)
315 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
318 /* otherwise it was not very useful; free it */
323 /* See if the origin->path is different between parent
324 * and origin first. Most of the time they are the
325 * same and diff-tree is fairly efficient about this.
327 diff_setup(&diff_opts
);
328 diff_opts
.recursive
= 1;
329 diff_opts
.detect_rename
= 0;
330 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
331 paths
[0] = origin
->path
;
334 diff_tree_setup_paths(paths
, &diff_opts
);
335 if (diff_setup_done(&diff_opts
) < 0)
338 if (is_null_sha1(origin
->commit
->object
.sha1
))
339 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
341 diff_tree_sha1(parent
->tree
->object
.sha1
,
342 origin
->commit
->tree
->object
.sha1
,
344 diffcore_std(&diff_opts
);
346 /* It is either one entry that says "modified", or "created",
349 if (!diff_queued_diff
.nr
) {
350 /* The path is the same as parent */
351 porigin
= get_origin(sb
, parent
, origin
->path
);
352 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
354 else if (diff_queued_diff
.nr
!= 1)
355 die("internal error in blame::find_origin");
357 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
360 die("internal error in blame::find_origin (%c)",
363 porigin
= get_origin(sb
, parent
, origin
->path
);
364 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
368 /* Did not exist in parent, or type changed */
372 diff_flush(&diff_opts
);
375 * Create a freestanding copy that is not part of
376 * the refcounted origin found in the scoreboard, and
377 * cache it in the commit.
379 struct origin
*cached
;
381 cached
= make_origin(porigin
->commit
, porigin
->path
);
382 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
383 parent
->util
= cached
;
389 * We have an origin -- find the path that corresponds to it in its
390 * parent and return an origin structure to represent it.
392 static struct origin
*find_rename(struct scoreboard
*sb
,
393 struct commit
*parent
,
394 struct origin
*origin
)
396 struct origin
*porigin
= NULL
;
397 struct diff_options diff_opts
;
399 const char *paths
[2];
401 diff_setup(&diff_opts
);
402 diff_opts
.recursive
= 1;
403 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
404 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
405 diff_opts
.single_follow
= origin
->path
;
407 diff_tree_setup_paths(paths
, &diff_opts
);
408 if (diff_setup_done(&diff_opts
) < 0)
411 if (is_null_sha1(origin
->commit
->object
.sha1
))
412 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
414 diff_tree_sha1(parent
->tree
->object
.sha1
,
415 origin
->commit
->tree
->object
.sha1
,
417 diffcore_std(&diff_opts
);
419 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
420 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
421 if ((p
->status
== 'R' || p
->status
== 'C') &&
422 !strcmp(p
->two
->path
, origin
->path
)) {
423 porigin
= get_origin(sb
, parent
, p
->one
->path
);
424 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
428 diff_flush(&diff_opts
);
433 * Parsing of patch chunks...
436 /* line number in postimage; up to but not including this
437 * line is the same as preimage
441 /* preimage line number after this chunk */
444 /* postimage line number after this chunk */
449 struct chunk
*chunks
;
453 struct blame_diff_state
{
454 struct xdiff_emit_state xm
;
456 unsigned hunk_post_context
;
457 unsigned hunk_in_pre_context
: 1;
460 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
462 struct blame_diff_state
*state
= state_
;
464 int off1
, off2
, len1
, len2
, num
;
466 num
= state
->ret
->num
;
467 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
468 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
469 state
->ret
->chunks
[num
- 1].same
++;
471 state
->hunk_in_pre_context
= 0;
473 state
->hunk_post_context
++;
475 state
->hunk_post_context
= 0;
480 if (num
&& state
->hunk_post_context
) {
481 chunk
= &state
->ret
->chunks
[num
- 1];
482 chunk
->p_next
-= state
->hunk_post_context
;
483 chunk
->t_next
-= state
->hunk_post_context
;
485 state
->ret
->num
= ++num
;
486 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
487 sizeof(struct chunk
) * num
);
488 chunk
= &state
->ret
->chunks
[num
- 1];
489 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
494 /* Line numbers in patch output are one based. */
498 chunk
->same
= len2
? off2
: (off2
+ 1);
500 chunk
->p_next
= off1
+ (len1
? len1
: 1);
501 chunk
->t_next
= chunk
->same
+ len2
;
502 state
->hunk_in_pre_context
= 1;
503 state
->hunk_post_context
= 0;
506 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
509 struct blame_diff_state state
;
514 xpp
.flags
= XDF_NEED_MINIMAL
;
515 xecfg
.ctxlen
= context
;
517 ecb
.outf
= xdiff_outf
;
519 memset(&state
, 0, sizeof(state
));
520 state
.xm
.consume
= process_u_diff
;
521 state
.ret
= xmalloc(sizeof(struct patch
));
522 state
.ret
->chunks
= NULL
;
525 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
527 if (state
.ret
->num
) {
529 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
530 chunk
->p_next
-= state
.hunk_post_context
;
531 chunk
->t_next
-= state
.hunk_post_context
;
537 * Run diff between two origins and grab the patch output, so that
538 * we can pass blame for lines origin is currently suspected for
541 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
543 mmfile_t file_p
, file_o
;
546 fill_origin_blob(parent
, &file_p
);
547 fill_origin_blob(origin
, &file_o
);
548 if (!file_p
.ptr
|| !file_o
.ptr
)
550 patch
= compare_buffer(&file_p
, &file_o
, 0);
555 static void free_patch(struct patch
*p
)
562 * Link in a new blame entry to the scoreboard. Entries that cover the
563 * same line range have been removed from the scoreboard previously.
565 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
567 struct blame_entry
*ent
, *prev
= NULL
;
569 origin_incref(e
->suspect
);
571 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
574 /* prev, if not NULL, is the last one that is below e */
577 e
->next
= prev
->next
;
589 * src typically is on-stack; we want to copy the information in it to
590 * an malloced blame_entry that is already on the linked list of the
591 * scoreboard. The origin of dst loses a refcnt while the origin of src
594 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
596 struct blame_entry
*p
, *n
;
600 origin_incref(src
->suspect
);
601 origin_decref(dst
->suspect
);
602 memcpy(dst
, src
, sizeof(*src
));
608 static const char *nth_line(struct scoreboard
*sb
, int lno
)
610 return sb
->final_buf
+ sb
->lineno
[lno
];
614 * It is known that lines between tlno to same came from parent, and e
615 * has an overlap with that range. it also is known that parent's
616 * line plno corresponds to e's line tlno.
622 * <------------------>
624 * Split e into potentially three parts; before this chunk, the chunk
625 * to be blamed for the parent, and after that portion.
627 static void split_overlap(struct blame_entry
*split
,
628 struct blame_entry
*e
,
629 int tlno
, int plno
, int same
,
630 struct origin
*parent
)
633 memset(split
, 0, sizeof(struct blame_entry
[3]));
635 if (e
->s_lno
< tlno
) {
636 /* there is a pre-chunk part not blamed on parent */
637 split
[0].suspect
= origin_incref(e
->suspect
);
638 split
[0].lno
= e
->lno
;
639 split
[0].s_lno
= e
->s_lno
;
640 split
[0].num_lines
= tlno
- e
->s_lno
;
641 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
642 split
[1].s_lno
= plno
;
645 split
[1].lno
= e
->lno
;
646 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
649 if (same
< e
->s_lno
+ e
->num_lines
) {
650 /* there is a post-chunk part not blamed on parent */
651 split
[2].suspect
= origin_incref(e
->suspect
);
652 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
653 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
654 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
655 chunk_end_lno
= split
[2].lno
;
658 chunk_end_lno
= e
->lno
+ e
->num_lines
;
659 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
662 * if it turns out there is nothing to blame the parent for,
663 * forget about the splitting. !split[1].suspect signals this.
665 if (split
[1].num_lines
< 1)
667 split
[1].suspect
= origin_incref(parent
);
671 * split_overlap() divided an existing blame e into up to three parts
672 * in split. Adjust the linked list of blames in the scoreboard to
675 static void split_blame(struct scoreboard
*sb
,
676 struct blame_entry
*split
,
677 struct blame_entry
*e
)
679 struct blame_entry
*new_entry
;
681 if (split
[0].suspect
&& split
[2].suspect
) {
682 /* The first part (reuse storage for the existing entry e) */
683 dup_entry(e
, &split
[0]);
685 /* The last part -- me */
686 new_entry
= xmalloc(sizeof(*new_entry
));
687 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
688 add_blame_entry(sb
, new_entry
);
690 /* ... and the middle part -- parent */
691 new_entry
= xmalloc(sizeof(*new_entry
));
692 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
693 add_blame_entry(sb
, new_entry
);
695 else if (!split
[0].suspect
&& !split
[2].suspect
)
697 * The parent covers the entire area; reuse storage for
698 * e and replace it with the parent.
700 dup_entry(e
, &split
[1]);
701 else if (split
[0].suspect
) {
702 /* me and then parent */
703 dup_entry(e
, &split
[0]);
705 new_entry
= xmalloc(sizeof(*new_entry
));
706 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
707 add_blame_entry(sb
, new_entry
);
710 /* parent and then me */
711 dup_entry(e
, &split
[1]);
713 new_entry
= xmalloc(sizeof(*new_entry
));
714 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
715 add_blame_entry(sb
, new_entry
);
718 if (DEBUG
) { /* sanity */
719 struct blame_entry
*ent
;
720 int lno
= sb
->ent
->lno
, corrupt
= 0;
722 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
727 lno
+= ent
->num_lines
;
731 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
732 printf("L %8d l %8d n %8d\n",
733 lno
, ent
->lno
, ent
->num_lines
);
734 lno
= ent
->lno
+ ent
->num_lines
;
742 * After splitting the blame, the origins used by the
743 * on-stack blame_entry should lose one refcnt each.
745 static void decref_split(struct blame_entry
*split
)
749 for (i
= 0; i
< 3; i
++)
750 origin_decref(split
[i
].suspect
);
754 * Helper for blame_chunk(). blame_entry e is known to overlap with
755 * the patch hunk; split it and pass blame to the parent.
757 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
758 int tlno
, int plno
, int same
,
759 struct origin
*parent
)
761 struct blame_entry split
[3];
763 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
764 if (split
[1].suspect
)
765 split_blame(sb
, split
, e
);
770 * Find the line number of the last line the target is suspected for.
772 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
774 struct blame_entry
*e
;
775 int last_in_target
= -1;
777 for (e
= sb
->ent
; e
; e
= e
->next
) {
778 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
780 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
781 last_in_target
= e
->s_lno
+ e
->num_lines
;
783 return last_in_target
;
787 * Process one hunk from the patch between the current suspect for
788 * blame_entry e and its parent. Find and split the overlap, and
789 * pass blame to the overlapping part to the parent.
791 static void blame_chunk(struct scoreboard
*sb
,
792 int tlno
, int plno
, int same
,
793 struct origin
*target
, struct origin
*parent
)
795 struct blame_entry
*e
;
797 for (e
= sb
->ent
; e
; e
= e
->next
) {
798 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
800 if (same
<= e
->s_lno
)
802 if (tlno
< e
->s_lno
+ e
->num_lines
)
803 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
808 * We are looking at the origin 'target' and aiming to pass blame
809 * for the lines it is suspected to its parent. Run diff to find
810 * which lines came from parent and pass blame for them.
812 static int pass_blame_to_parent(struct scoreboard
*sb
,
813 struct origin
*target
,
814 struct origin
*parent
)
816 int i
, last_in_target
, plno
, tlno
;
819 last_in_target
= find_last_in_target(sb
, target
);
820 if (last_in_target
< 0)
821 return 1; /* nothing remains for this target */
823 patch
= get_patch(parent
, target
);
825 for (i
= 0; i
< patch
->num
; i
++) {
826 struct chunk
*chunk
= &patch
->chunks
[i
];
828 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
829 plno
= chunk
->p_next
;
830 tlno
= chunk
->t_next
;
832 /* The rest (i.e. anything after tlno) are the same as the parent */
833 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
840 * The lines in blame_entry after splitting blames many times can become
841 * very small and trivial, and at some point it becomes pointless to
842 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
843 * ordinary C program, and it is not worth to say it was copied from
844 * totally unrelated file in the parent.
846 * Compute how trivial the lines in the blame_entry are.
848 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
857 cp
= nth_line(sb
, e
->lno
);
858 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
860 unsigned ch
= *((unsigned char *)cp
);
870 * best_so_far[] and this[] are both a split of an existing blame_entry
871 * that passes blame to the parent. Maintain best_so_far the best split
872 * so far, by comparing this and best_so_far and copying this into
873 * bst_so_far as needed.
875 static void copy_split_if_better(struct scoreboard
*sb
,
876 struct blame_entry
*best_so_far
,
877 struct blame_entry
*this)
881 if (!this[1].suspect
)
883 if (best_so_far
[1].suspect
) {
884 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
888 for (i
= 0; i
< 3; i
++)
889 origin_incref(this[i
].suspect
);
890 decref_split(best_so_far
);
891 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
895 * Find the lines from parent that are the same as ent so that
896 * we can pass blames to it. file_p has the blob contents for
899 static void find_copy_in_blob(struct scoreboard
*sb
,
900 struct blame_entry
*ent
,
901 struct origin
*parent
,
902 struct blame_entry
*split
,
912 * Prepare mmfile that contains only the lines in ent.
914 cp
= nth_line(sb
, ent
->lno
);
915 file_o
.ptr
= (char*) cp
;
916 cnt
= ent
->num_lines
;
918 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
922 file_o
.size
= cp
- file_o
.ptr
;
924 patch
= compare_buffer(file_p
, &file_o
, 1);
926 memset(split
, 0, sizeof(struct blame_entry
[3]));
928 for (i
= 0; i
< patch
->num
; i
++) {
929 struct chunk
*chunk
= &patch
->chunks
[i
];
931 /* tlno to chunk->same are the same as ent */
932 if (ent
->num_lines
<= tlno
)
934 if (tlno
< chunk
->same
) {
935 struct blame_entry
this[3];
936 split_overlap(this, ent
,
937 tlno
+ ent
->s_lno
, plno
,
938 chunk
->same
+ ent
->s_lno
,
940 copy_split_if_better(sb
, split
, this);
943 plno
= chunk
->p_next
;
944 tlno
= chunk
->t_next
;
950 * See if lines currently target is suspected for can be attributed to
953 static int find_move_in_parent(struct scoreboard
*sb
,
954 struct origin
*target
,
955 struct origin
*parent
)
957 int last_in_target
, made_progress
;
958 struct blame_entry
*e
, split
[3];
961 last_in_target
= find_last_in_target(sb
, target
);
962 if (last_in_target
< 0)
963 return 1; /* nothing remains for this target */
965 fill_origin_blob(parent
, &file_p
);
970 while (made_progress
) {
972 for (e
= sb
->ent
; e
; e
= e
->next
) {
973 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
975 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
976 if (split
[1].suspect
&&
977 blame_move_score
< ent_score(sb
, &split
[1])) {
978 split_blame(sb
, split
, e
);
988 struct blame_entry
*ent
;
989 struct blame_entry split
[3];
993 * Count the number of entries the target is suspected for,
994 * and prepare a list of entry and the best split.
996 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
997 struct origin
*target
,
1000 struct blame_entry
*e
;
1002 struct blame_list
*blame_list
= NULL
;
1004 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1005 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
1008 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1009 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1010 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
1011 blame_list
[i
++].ent
= e
;
1013 *num_ents_p
= num_ents
;
1018 * For lines target is suspected for, see if we can find code movement
1019 * across file boundary from the parent commit. porigin is the path
1020 * in the parent we already tried.
1022 static int find_copy_in_parent(struct scoreboard
*sb
,
1023 struct origin
*target
,
1024 struct commit
*parent
,
1025 struct origin
*porigin
,
1028 struct diff_options diff_opts
;
1029 const char *paths
[1];
1032 struct blame_list
*blame_list
;
1035 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1037 return 1; /* nothing remains for this target */
1039 diff_setup(&diff_opts
);
1040 diff_opts
.recursive
= 1;
1041 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1044 diff_tree_setup_paths(paths
, &diff_opts
);
1045 if (diff_setup_done(&diff_opts
) < 0)
1048 /* Try "find copies harder" on new path if requested;
1049 * we do not want to use diffcore_rename() actually to
1050 * match things up; find_copies_harder is set only to
1051 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1052 * and this code needs to be after diff_setup_done(), which
1053 * usually makes find-copies-harder imply copy detection.
1055 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
1056 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
1057 diff_opts
.find_copies_harder
= 1;
1059 if (is_null_sha1(target
->commit
->object
.sha1
))
1060 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1062 diff_tree_sha1(parent
->tree
->object
.sha1
,
1063 target
->commit
->tree
->object
.sha1
,
1066 if (!diff_opts
.find_copies_harder
)
1067 diffcore_std(&diff_opts
);
1071 int made_progress
= 0;
1073 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1074 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1075 struct origin
*norigin
;
1077 struct blame_entry
this[3];
1079 if (!DIFF_FILE_VALID(p
->one
))
1080 continue; /* does not exist in parent */
1081 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1082 /* find_move already dealt with this path */
1085 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1086 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1087 fill_origin_blob(norigin
, &file_p
);
1091 for (j
= 0; j
< num_ents
; j
++) {
1092 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1093 norigin
, this, &file_p
);
1094 copy_split_if_better(sb
, blame_list
[j
].split
,
1098 origin_decref(norigin
);
1101 for (j
= 0; j
< num_ents
; j
++) {
1102 struct blame_entry
*split
= blame_list
[j
].split
;
1103 if (split
[1].suspect
&&
1104 blame_copy_score
< ent_score(sb
, &split
[1])) {
1105 split_blame(sb
, split
, blame_list
[j
].ent
);
1108 decref_split(split
);
1114 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1120 diff_flush(&diff_opts
);
1126 * The blobs of origin and porigin exactly match, so everything
1127 * origin is suspected for can be blamed on the parent.
1129 static void pass_whole_blame(struct scoreboard
*sb
,
1130 struct origin
*origin
, struct origin
*porigin
)
1132 struct blame_entry
*e
;
1134 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1135 /* Steal its file */
1136 porigin
->file
= origin
->file
;
1137 origin
->file
.ptr
= NULL
;
1139 for (e
= sb
->ent
; e
; e
= e
->next
) {
1140 if (cmp_suspect(e
->suspect
, origin
))
1142 origin_incref(porigin
);
1143 origin_decref(e
->suspect
);
1144 e
->suspect
= porigin
;
1148 #define MAXPARENT 16
1150 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1153 struct commit
*commit
= origin
->commit
;
1154 struct commit_list
*parent
;
1155 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1157 memset(parent_origin
, 0, sizeof(parent_origin
));
1159 /* The first pass looks for unrenamed path to optimize for
1160 * common cases, then we look for renames in the second pass.
1162 for (pass
= 0; pass
< 2; pass
++) {
1163 struct origin
*(*find
)(struct scoreboard
*,
1164 struct commit
*, struct origin
*);
1165 find
= pass
? find_rename
: find_origin
;
1167 for (i
= 0, parent
= commit
->parents
;
1168 i
< MAXPARENT
&& parent
;
1169 parent
= parent
->next
, i
++) {
1170 struct commit
*p
= parent
->item
;
1173 if (parent_origin
[i
])
1175 if (parse_commit(p
))
1177 porigin
= find(sb
, p
, origin
);
1180 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1181 pass_whole_blame(sb
, origin
, porigin
);
1182 origin_decref(porigin
);
1185 for (j
= same
= 0; j
< i
; j
++)
1186 if (parent_origin
[j
] &&
1187 !hashcmp(parent_origin
[j
]->blob_sha1
,
1188 porigin
->blob_sha1
)) {
1193 parent_origin
[i
] = porigin
;
1195 origin_decref(porigin
);
1200 for (i
= 0, parent
= commit
->parents
;
1201 i
< MAXPARENT
&& parent
;
1202 parent
= parent
->next
, i
++) {
1203 struct origin
*porigin
= parent_origin
[i
];
1206 if (pass_blame_to_parent(sb
, origin
, porigin
))
1211 * Optionally find moves in parents' files.
1213 if (opt
& PICKAXE_BLAME_MOVE
)
1214 for (i
= 0, parent
= commit
->parents
;
1215 i
< MAXPARENT
&& parent
;
1216 parent
= parent
->next
, i
++) {
1217 struct origin
*porigin
= parent_origin
[i
];
1220 if (find_move_in_parent(sb
, origin
, porigin
))
1225 * Optionally find copies from parents' files.
1227 if (opt
& PICKAXE_BLAME_COPY
)
1228 for (i
= 0, parent
= commit
->parents
;
1229 i
< MAXPARENT
&& parent
;
1230 parent
= parent
->next
, i
++) {
1231 struct origin
*porigin
= parent_origin
[i
];
1232 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1238 for (i
= 0; i
< MAXPARENT
; i
++)
1239 origin_decref(parent_origin
[i
]);
1243 * Information on commits, used for output.
1248 const char *author_mail
;
1249 unsigned long author_time
;
1250 const char *author_tz
;
1252 /* filled only when asked for details */
1253 const char *committer
;
1254 const char *committer_mail
;
1255 unsigned long committer_time
;
1256 const char *committer_tz
;
1258 const char *summary
;
1262 * Parse author/committer line in the commit object buffer
1264 static void get_ac_line(const char *inbuf
, const char *what
,
1265 int bufsz
, char *person
, const char **mail
,
1266 unsigned long *time
, const char **tz
)
1271 tmp
= strstr(inbuf
, what
);
1274 tmp
+= strlen(what
);
1275 endp
= strchr(tmp
, '\n');
1283 *mail
= *tz
= "(unknown)";
1287 memcpy(person
, tmp
, len
);
1299 *time
= strtoul(tmp
, NULL
, 10);
1308 static void get_commit_info(struct commit
*commit
,
1309 struct commit_info
*ret
,
1314 static char author_buf
[1024];
1315 static char committer_buf
[1024];
1316 static char summary_buf
[1024];
1319 * We've operated without save_commit_buffer, so
1320 * we now need to populate them for output.
1322 if (!commit
->buffer
) {
1323 enum object_type type
;
1326 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1328 ret
->author
= author_buf
;
1329 get_ac_line(commit
->buffer
, "\nauthor ",
1330 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1331 &ret
->author_time
, &ret
->author_tz
);
1336 ret
->committer
= committer_buf
;
1337 get_ac_line(commit
->buffer
, "\ncommitter ",
1338 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1339 &ret
->committer_time
, &ret
->committer_tz
);
1341 ret
->summary
= summary_buf
;
1342 tmp
= strstr(commit
->buffer
, "\n\n");
1345 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1349 endp
= strchr(tmp
, '\n');
1351 endp
= tmp
+ strlen(tmp
);
1353 if (len
>= sizeof(summary_buf
) || len
== 0)
1355 memcpy(summary_buf
, tmp
, len
);
1356 summary_buf
[len
] = 0;
1360 * To allow LF and other nonportable characters in pathnames,
1361 * they are c-style quoted as needed.
1363 static void write_filename_info(const char *path
)
1365 printf("filename ");
1366 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1371 * The blame_entry is found to be guilty for the range. Mark it
1372 * as such, and show it in incremental output.
1374 static void found_guilty_entry(struct blame_entry
*ent
)
1380 struct origin
*suspect
= ent
->suspect
;
1382 printf("%s %d %d %d\n",
1383 sha1_to_hex(suspect
->commit
->object
.sha1
),
1384 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1385 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1386 struct commit_info ci
;
1387 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1388 get_commit_info(suspect
->commit
, &ci
, 1);
1389 printf("author %s\n", ci
.author
);
1390 printf("author-mail %s\n", ci
.author_mail
);
1391 printf("author-time %lu\n", ci
.author_time
);
1392 printf("author-tz %s\n", ci
.author_tz
);
1393 printf("committer %s\n", ci
.committer
);
1394 printf("committer-mail %s\n", ci
.committer_mail
);
1395 printf("committer-time %lu\n", ci
.committer_time
);
1396 printf("committer-tz %s\n", ci
.committer_tz
);
1397 printf("summary %s\n", ci
.summary
);
1398 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1399 printf("boundary\n");
1401 write_filename_info(suspect
->path
);
1406 * The main loop -- while the scoreboard has lines whose true origin
1407 * is still unknown, pick one blame_entry, and allow its current
1408 * suspect to pass blames to its parents.
1410 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1413 struct blame_entry
*ent
;
1414 struct commit
*commit
;
1415 struct origin
*suspect
= NULL
;
1417 /* find one suspect to break down */
1418 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1420 suspect
= ent
->suspect
;
1422 return; /* all done */
1425 * We will use this suspect later in the loop,
1426 * so hold onto it in the meantime.
1428 origin_incref(suspect
);
1429 commit
= suspect
->commit
;
1430 if (!commit
->object
.parsed
)
1431 parse_commit(commit
);
1432 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1433 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1434 pass_blame(sb
, suspect
, opt
);
1436 commit
->object
.flags
|= UNINTERESTING
;
1437 if (commit
->object
.parsed
)
1438 mark_parents_uninteresting(commit
);
1440 /* treat root commit as boundary */
1441 if (!commit
->parents
&& !show_root
)
1442 commit
->object
.flags
|= UNINTERESTING
;
1444 /* Take responsibility for the remaining entries */
1445 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1446 if (!cmp_suspect(ent
->suspect
, suspect
))
1447 found_guilty_entry(ent
);
1448 origin_decref(suspect
);
1450 if (DEBUG
) /* sanity */
1451 sanity_check_refcnt(sb
);
1455 static const char *format_time(unsigned long time
, const char *tz_str
,
1458 static char time_buf
[128];
1463 if (show_raw_time
) {
1464 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1469 minutes
= tz
< 0 ? -tz
: tz
;
1470 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1471 minutes
= tz
< 0 ? -minutes
: minutes
;
1472 t
= time
+ minutes
* 60;
1475 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1476 strcat(time_buf
, tz_str
);
1480 #define OUTPUT_ANNOTATE_COMPAT 001
1481 #define OUTPUT_LONG_OBJECT_NAME 002
1482 #define OUTPUT_RAW_TIMESTAMP 004
1483 #define OUTPUT_PORCELAIN 010
1484 #define OUTPUT_SHOW_NAME 020
1485 #define OUTPUT_SHOW_NUMBER 040
1486 #define OUTPUT_SHOW_SCORE 0100
1488 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1492 struct origin
*suspect
= ent
->suspect
;
1495 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1496 printf("%s%c%d %d %d\n",
1498 ent
->guilty
? ' ' : '*', // purely for debugging
1502 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1503 struct commit_info ci
;
1504 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1505 get_commit_info(suspect
->commit
, &ci
, 1);
1506 printf("author %s\n", ci
.author
);
1507 printf("author-mail %s\n", ci
.author_mail
);
1508 printf("author-time %lu\n", ci
.author_time
);
1509 printf("author-tz %s\n", ci
.author_tz
);
1510 printf("committer %s\n", ci
.committer
);
1511 printf("committer-mail %s\n", ci
.committer_mail
);
1512 printf("committer-time %lu\n", ci
.committer_time
);
1513 printf("committer-tz %s\n", ci
.committer_tz
);
1514 write_filename_info(suspect
->path
);
1515 printf("summary %s\n", ci
.summary
);
1516 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1517 printf("boundary\n");
1519 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1520 write_filename_info(suspect
->path
);
1522 cp
= nth_line(sb
, ent
->lno
);
1523 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1526 printf("%s %d %d\n", hex
,
1527 ent
->s_lno
+ 1 + cnt
,
1528 ent
->lno
+ 1 + cnt
);
1533 } while (ch
!= '\n' &&
1534 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1538 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1542 struct origin
*suspect
= ent
->suspect
;
1543 struct commit_info ci
;
1545 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1547 get_commit_info(suspect
->commit
, &ci
, 1);
1548 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1550 cp
= nth_line(sb
, ent
->lno
);
1551 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1553 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1555 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1557 memset(hex
, ' ', length
);
1558 else if (!cmd_is_annotate
) {
1564 printf("%.*s", length
, hex
);
1565 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1566 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1567 format_time(ci
.author_time
, ci
.author_tz
,
1569 ent
->lno
+ 1 + cnt
);
1571 if (opt
& OUTPUT_SHOW_SCORE
)
1573 max_score_digits
, ent
->score
,
1574 ent
->suspect
->refcnt
);
1575 if (opt
& OUTPUT_SHOW_NAME
)
1576 printf(" %-*.*s", longest_file
, longest_file
,
1578 if (opt
& OUTPUT_SHOW_NUMBER
)
1579 printf(" %*d", max_orig_digits
,
1580 ent
->s_lno
+ 1 + cnt
);
1581 printf(" (%-*.*s %10s %*d) ",
1582 longest_author
, longest_author
, ci
.author
,
1583 format_time(ci
.author_time
, ci
.author_tz
,
1585 max_digits
, ent
->lno
+ 1 + cnt
);
1590 } while (ch
!= '\n' &&
1591 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1595 static void output(struct scoreboard
*sb
, int option
)
1597 struct blame_entry
*ent
;
1599 if (option
& OUTPUT_PORCELAIN
) {
1600 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1601 struct blame_entry
*oth
;
1602 struct origin
*suspect
= ent
->suspect
;
1603 struct commit
*commit
= suspect
->commit
;
1604 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1606 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1607 if ((oth
->suspect
->commit
!= commit
) ||
1608 !strcmp(oth
->suspect
->path
, suspect
->path
))
1610 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1616 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1617 if (option
& OUTPUT_PORCELAIN
)
1618 emit_porcelain(sb
, ent
);
1620 emit_other(sb
, ent
, option
);
1626 * To allow quick access to the contents of nth line in the
1627 * final image, prepare an index in the scoreboard.
1629 static int prepare_lines(struct scoreboard
*sb
)
1631 const char *buf
= sb
->final_buf
;
1632 unsigned long len
= sb
->final_buf_size
;
1633 int num
= 0, incomplete
= 0, bol
= 1;
1635 if (len
&& buf
[len
-1] != '\n')
1636 incomplete
++; /* incomplete line at the end */
1639 sb
->lineno
= xrealloc(sb
->lineno
,
1640 sizeof(int* ) * (num
+ 1));
1641 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1644 if (*buf
++ == '\n') {
1649 sb
->lineno
= xrealloc(sb
->lineno
,
1650 sizeof(int* ) * (num
+ incomplete
+ 1));
1651 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1652 sb
->num_lines
= num
+ incomplete
;
1653 return sb
->num_lines
;
1657 * Add phony grafts for use with -S; this is primarily to
1658 * support git-cvsserver that wants to give a linear history
1661 static int read_ancestry(const char *graft_file
)
1663 FILE *fp
= fopen(graft_file
, "r");
1667 while (fgets(buf
, sizeof(buf
), fp
)) {
1668 /* The format is just "Commit Parent1 Parent2 ...\n" */
1669 int len
= strlen(buf
);
1670 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1672 register_commit_graft(graft
, 0);
1679 * How many columns do we need to show line numbers in decimal?
1681 static int lineno_width(int lines
)
1685 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1691 * How many columns do we need to show line numbers, authors,
1694 static void find_alignment(struct scoreboard
*sb
, int *option
)
1696 int longest_src_lines
= 0;
1697 int longest_dst_lines
= 0;
1698 unsigned largest_score
= 0;
1699 struct blame_entry
*e
;
1701 for (e
= sb
->ent
; e
; e
= e
->next
) {
1702 struct origin
*suspect
= e
->suspect
;
1703 struct commit_info ci
;
1706 if (strcmp(suspect
->path
, sb
->path
))
1707 *option
|= OUTPUT_SHOW_NAME
;
1708 num
= strlen(suspect
->path
);
1709 if (longest_file
< num
)
1711 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1712 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1713 get_commit_info(suspect
->commit
, &ci
, 1);
1714 num
= strlen(ci
.author
);
1715 if (longest_author
< num
)
1716 longest_author
= num
;
1718 num
= e
->s_lno
+ e
->num_lines
;
1719 if (longest_src_lines
< num
)
1720 longest_src_lines
= num
;
1721 num
= e
->lno
+ e
->num_lines
;
1722 if (longest_dst_lines
< num
)
1723 longest_dst_lines
= num
;
1724 if (largest_score
< ent_score(sb
, e
))
1725 largest_score
= ent_score(sb
, e
);
1727 max_orig_digits
= lineno_width(longest_src_lines
);
1728 max_digits
= lineno_width(longest_dst_lines
);
1729 max_score_digits
= lineno_width(largest_score
);
1733 * For debugging -- origin is refcounted, and this asserts that
1734 * we do not underflow.
1736 static void sanity_check_refcnt(struct scoreboard
*sb
)
1739 struct blame_entry
*ent
;
1741 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1742 /* Nobody should have zero or negative refcnt */
1743 if (ent
->suspect
->refcnt
<= 0) {
1744 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1746 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1747 ent
->suspect
->refcnt
);
1751 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1752 /* Mark the ones that haven't been checked */
1753 if (0 < ent
->suspect
->refcnt
)
1754 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1756 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1758 * ... then pick each and see if they have the the
1762 struct blame_entry
*e
;
1763 struct origin
*suspect
= ent
->suspect
;
1765 if (0 < suspect
->refcnt
)
1767 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1768 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1769 if (e
->suspect
!= suspect
)
1773 if (suspect
->refcnt
!= found
) {
1774 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1776 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1777 ent
->suspect
->refcnt
, found
);
1783 find_alignment(sb
, &opt
);
1785 die("Baa %d!", baa
);
1790 * Used for the command line parsing; check if the path exists
1791 * in the working tree.
1793 static int has_path_in_work_tree(const char *path
)
1796 return !lstat(path
, &st
);
1799 static unsigned parse_score(const char *arg
)
1802 unsigned long score
= strtoul(arg
, &end
, 10);
1808 static const char *add_prefix(const char *prefix
, const char *path
)
1810 if (!prefix
|| !prefix
[0])
1812 return prefix_path(prefix
, strlen(prefix
), path
);
1816 * Parsing of (comma separated) one item in the -L option
1818 static const char *parse_loc(const char *spec
,
1819 struct scoreboard
*sb
, long lno
,
1820 long begin
, long *ret
)
1827 regmatch_t match
[1];
1829 /* Allow "-L <something>,+20" to mean starting at <something>
1830 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1833 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1834 num
= strtol(spec
+ 1, &term
, 10);
1835 if (term
!= spec
+ 1) {
1839 *ret
= begin
+ num
- 2;
1848 num
= strtol(spec
, &term
, 10);
1856 /* it could be a regexp of form /.../ */
1857 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1864 /* try [spec+1 .. term-1] as regexp */
1866 begin
--; /* input is in human terms */
1867 line
= nth_line(sb
, begin
);
1869 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1870 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1871 const char *cp
= line
+ match
[0].rm_so
;
1874 while (begin
++ < lno
) {
1875 nline
= nth_line(sb
, begin
);
1876 if (line
<= cp
&& cp
< nline
)
1887 regerror(reg_error
, ®exp
, errbuf
, 1024);
1888 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1893 * Parsing of -L option
1895 static void prepare_blame_range(struct scoreboard
*sb
,
1896 const char *bottomtop
,
1898 long *bottom
, long *top
)
1902 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1904 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1912 static int git_blame_config(const char *var
, const char *value
)
1914 if (!strcmp(var
, "blame.showroot")) {
1915 show_root
= git_config_bool(var
, value
);
1918 if (!strcmp(var
, "blame.blankboundary")) {
1919 blank_boundary
= git_config_bool(var
, value
);
1922 return git_default_config(var
, value
);
1925 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1927 struct commit
*commit
;
1928 struct origin
*origin
;
1929 unsigned char head_sha1
[20];
1934 unsigned long fin_size
;
1936 struct cache_entry
*ce
;
1939 if (get_sha1("HEAD", head_sha1
))
1940 die("No such ref: HEAD");
1943 commit
= xcalloc(1, sizeof(*commit
));
1944 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
1945 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
1946 commit
->object
.parsed
= 1;
1948 commit
->object
.type
= OBJ_COMMIT
;
1950 origin
= make_origin(commit
, path
);
1952 if (!contents_from
|| strcmp("-", contents_from
)) {
1954 const char *read_from
;
1956 if (contents_from
) {
1957 if (stat(contents_from
, &st
) < 0)
1958 die("Cannot stat %s", contents_from
);
1959 read_from
= contents_from
;
1962 if (lstat(path
, &st
) < 0)
1963 die("Cannot lstat %s", path
);
1966 fin_size
= st
.st_size
;
1967 buf
= xmalloc(fin_size
+1);
1968 mode
= canon_mode(st
.st_mode
);
1969 switch (st
.st_mode
& S_IFMT
) {
1971 fd
= open(read_from
, O_RDONLY
);
1973 die("cannot open %s", read_from
);
1974 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
1975 die("cannot read %s", read_from
);
1978 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
1979 die("cannot readlink %s", read_from
);
1982 die("unsupported file type %s", read_from
);
1986 /* Reading from stdin */
1987 contents_from
= "standard input";
1993 buf
= xrealloc(buf
, fin_size
+ cnt
);
1994 cnt
= xread(0, buf
+ fin_size
, cnt
);
1996 die("read error %s from stdin",
2002 buf
= xrealloc(buf
, fin_size
+ 1);
2005 origin
->file
.ptr
= buf
;
2006 origin
->file
.size
= fin_size
;
2007 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2008 commit
->util
= origin
;
2011 * Read the current index, replace the path entry with
2012 * origin->blob_sha1 without mucking with its mode or type
2013 * bits; we are not going to write this index out -- we just
2014 * want to run "diff-index --cached".
2021 int pos
= cache_name_pos(path
, len
);
2023 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2025 /* Let's not bother reading from HEAD tree */
2026 mode
= S_IFREG
| 0644;
2028 size
= cache_entry_size(len
);
2029 ce
= xcalloc(1, size
);
2030 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2031 memcpy(ce
->name
, path
, len
);
2032 ce
->ce_flags
= create_ce_flags(len
, 0);
2033 ce
->ce_mode
= create_ce_mode(mode
);
2034 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2037 * We are not going to write this out, so this does not matter
2038 * right now, but someday we might optimize diff-index --cached
2039 * with cache-tree information.
2041 cache_tree_invalidate_path(active_cache_tree
, path
);
2043 commit
->buffer
= xmalloc(400);
2044 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2045 sprintf(commit
->buffer
,
2046 "tree 0000000000000000000000000000000000000000\n"
2050 "Version of %s from %s\n",
2051 sha1_to_hex(head_sha1
),
2052 ident
, ident
, path
, contents_from
? contents_from
: path
);
2056 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2058 struct rev_info revs
;
2060 struct scoreboard sb
;
2062 struct blame_entry
*ent
;
2063 int i
, seen_dashdash
, unk
, opt
;
2064 long bottom
, top
, lno
;
2065 int output_option
= 0;
2067 const char *revs_file
= NULL
;
2068 const char *final_commit_name
= NULL
;
2069 enum object_type type
;
2070 const char *bottomtop
= NULL
;
2071 const char *contents_from
= NULL
;
2073 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2075 git_config(git_blame_config
);
2076 save_commit_buffer
= 0;
2080 for (unk
= i
= 1; i
< argc
; i
++) {
2081 const char *arg
= argv
[i
];
2084 else if (!strcmp("-b", arg
))
2086 else if (!strcmp("--root", arg
))
2088 else if (!strcmp(arg
, "--show-stats"))
2090 else if (!strcmp("-c", arg
))
2091 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2092 else if (!strcmp("-t", arg
))
2093 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2094 else if (!strcmp("-l", arg
))
2095 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2096 else if (!strcmp("-S", arg
) && ++i
< argc
)
2097 revs_file
= argv
[i
];
2098 else if (!prefixcmp(arg
, "-M")) {
2099 opt
|= PICKAXE_BLAME_MOVE
;
2100 blame_move_score
= parse_score(arg
+2);
2102 else if (!prefixcmp(arg
, "-C")) {
2103 if (opt
& PICKAXE_BLAME_COPY
)
2104 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2105 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2106 blame_copy_score
= parse_score(arg
+2);
2108 else if (!prefixcmp(arg
, "-L")) {
2117 die("More than one '-L n,m' option given");
2120 else if (!strcmp("--contents", arg
)) {
2123 contents_from
= argv
[i
];
2125 else if (!strcmp("--incremental", arg
))
2127 else if (!strcmp("--score-debug", arg
))
2128 output_option
|= OUTPUT_SHOW_SCORE
;
2129 else if (!strcmp("-f", arg
) ||
2130 !strcmp("--show-name", arg
))
2131 output_option
|= OUTPUT_SHOW_NAME
;
2132 else if (!strcmp("-n", arg
) ||
2133 !strcmp("--show-number", arg
))
2134 output_option
|= OUTPUT_SHOW_NUMBER
;
2135 else if (!strcmp("-p", arg
) ||
2136 !strcmp("--porcelain", arg
))
2137 output_option
|= OUTPUT_PORCELAIN
;
2138 else if (!strcmp("--", arg
)) {
2150 if (!blame_move_score
)
2151 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2152 if (!blame_copy_score
)
2153 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2156 * We have collected options unknown to us in argv[1..unk]
2157 * which are to be passed to revision machinery if we are
2158 * going to do the "bottom" processing.
2160 * The remaining are:
2162 * (1) if seen_dashdash, its either
2163 * "-options -- <path>" or
2164 * "-options -- <path> <rev>".
2165 * but the latter is allowed only if there is no
2166 * options that we passed to revision machinery.
2168 * (2) otherwise, we may have "--" somewhere later and
2169 * might be looking at the first one of multiple 'rev'
2170 * parameters (e.g. " master ^next ^maint -- path").
2171 * See if there is a dashdash first, and give the
2172 * arguments before that to revision machinery.
2173 * After that there must be one 'path'.
2175 * (3) otherwise, its one of the three:
2176 * "-options <path> <rev>"
2177 * "-options <rev> <path>"
2179 * but again the first one is allowed only if
2180 * there is no options that we passed to revision
2184 if (seen_dashdash
) {
2188 path
= add_prefix(prefix
, argv
[i
]);
2189 if (i
+ 1 == argc
- 1) {
2192 argv
[unk
++] = argv
[i
+ 1];
2194 else if (i
+ 1 != argc
)
2195 /* garbage at end */
2200 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2201 if (!strcmp(argv
[j
], "--"))
2203 if (seen_dashdash
) {
2205 if (seen_dashdash
+ 1 != argc
- 1)
2207 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2208 for (j
= i
; j
< seen_dashdash
; j
++)
2209 argv
[unk
++] = argv
[j
];
2215 path
= add_prefix(prefix
, argv
[i
]);
2216 if (i
+ 1 == argc
- 1) {
2217 final_commit_name
= argv
[i
+ 1];
2219 /* if (unk == 1) we could be getting
2222 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2223 path
= add_prefix(prefix
, argv
[i
+ 1]);
2224 final_commit_name
= argv
[i
];
2227 else if (i
!= argc
- 1)
2228 usage(blame_usage
); /* garbage at end */
2230 if (!has_path_in_work_tree(path
))
2231 die("cannot stat path %s: %s",
2232 path
, strerror(errno
));
2236 if (final_commit_name
)
2237 argv
[unk
++] = final_commit_name
;
2240 * Now we got rev and path. We do not want the path pruning
2241 * but we may want "bottom" processing.
2243 argv
[unk
++] = "--"; /* terminate the rev name */
2246 init_revisions(&revs
, NULL
);
2247 setup_revisions(unk
, argv
, &revs
, NULL
);
2248 memset(&sb
, 0, sizeof(sb
));
2251 * There must be one and only one positive commit in the
2252 * revs->pending array.
2254 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2255 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2256 if (obj
->flags
& UNINTERESTING
)
2258 while (obj
->type
== OBJ_TAG
)
2259 obj
= deref_tag(obj
, NULL
, 0);
2260 if (obj
->type
!= OBJ_COMMIT
)
2261 die("Non commit %s?",
2262 revs
.pending
.objects
[i
].name
);
2264 die("More than one commit to dig from %s and %s?",
2265 revs
.pending
.objects
[i
].name
,
2267 sb
.final
= (struct commit
*) obj
;
2268 final_commit_name
= revs
.pending
.objects
[i
].name
;
2273 * "--not A B -- path" without anything positive;
2274 * do not default to HEAD, but use the working tree
2277 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2278 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2280 else if (contents_from
)
2281 die("Cannot use --contents with final commit object name");
2284 * If we have bottom, this will mark the ancestors of the
2285 * bottom commits we would reach while traversing as
2288 prepare_revision_walk(&revs
);
2290 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2293 buf
= xmalloc(o
->file
.size
+ 1);
2294 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2296 sb
.final_buf_size
= o
->file
.size
;
2299 o
= get_origin(&sb
, sb
.final
, path
);
2300 if (fill_blob_sha1(o
))
2301 die("no such path %s in %s", path
, final_commit_name
);
2303 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2304 &sb
.final_buf_size
);
2307 lno
= prepare_lines(&sb
);
2311 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2312 if (bottom
&& top
&& top
< bottom
) {
2314 tmp
= top
; top
= bottom
; bottom
= tmp
;
2322 die("file %s has only %lu lines", path
, lno
);
2324 ent
= xcalloc(1, sizeof(*ent
));
2326 ent
->num_lines
= top
- bottom
;
2328 ent
->s_lno
= bottom
;
2333 if (revs_file
&& read_ancestry(revs_file
))
2334 die("reading graft file %s failed: %s",
2335 revs_file
, strerror(errno
));
2337 assign_blame(&sb
, &revs
, opt
);
2344 if (!(output_option
& OUTPUT_PORCELAIN
))
2345 find_alignment(&sb
, &output_option
);
2347 output(&sb
, output_option
);
2348 free((void *)sb
.final_buf
);
2349 for (ent
= sb
.ent
; ent
; ) {
2350 struct blame_entry
*e
= ent
->next
;
2356 printf("num read blob: %d\n", num_read_blob
);
2357 printf("num get patch: %d\n", num_get_patch
);
2358 printf("num commits: %d\n", num_commits
);