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 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 Show long commit SHA1 (Default: off)\n"
25 " --root Do not treat root commits as boundaries (Default: off)\n"
26 " -t 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 inline int same_suspect(struct origin
*a
, struct origin
*b
)
187 if (a
->commit
!= b
->commit
)
189 return !strcmp(a
->path
, b
->path
);
192 static void sanity_check_refcnt(struct scoreboard
*);
195 * If two blame entries that are next to each other came from
196 * contiguous lines in the same origin (i.e. <commit, path> pair),
197 * merge them together.
199 static void coalesce(struct scoreboard
*sb
)
201 struct blame_entry
*ent
, *next
;
203 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
204 if (same_suspect(ent
->suspect
, next
->suspect
) &&
205 ent
->guilty
== next
->guilty
&&
206 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
207 ent
->num_lines
+= next
->num_lines
;
208 ent
->next
= next
->next
;
210 ent
->next
->prev
= ent
;
211 origin_decref(next
->suspect
);
214 next
= ent
; /* again */
218 if (DEBUG
) /* sanity */
219 sanity_check_refcnt(sb
);
223 * Given a commit and a path in it, create a new origin structure.
224 * The callers that add blame to the scoreboard should use
225 * get_origin() to obtain shared, refcounted copy instead of calling
226 * this function directly.
228 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
231 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
234 strcpy(o
->path
, path
);
239 * Locate an existing origin or create a new one.
241 static struct origin
*get_origin(struct scoreboard
*sb
,
242 struct commit
*commit
,
245 struct blame_entry
*e
;
247 for (e
= sb
->ent
; e
; e
= e
->next
) {
248 if (e
->suspect
->commit
== commit
&&
249 !strcmp(e
->suspect
->path
, path
))
250 return origin_incref(e
->suspect
);
252 return make_origin(commit
, path
);
256 * Fill the blob_sha1 field of an origin if it hasn't, so that later
257 * call to fill_origin_blob() can use it to locate the data. blob_sha1
258 * for an origin is also used to pass the blame for the entire file to
259 * the parent to detect the case where a child's blob is identical to
260 * that of its parent's.
262 static int fill_blob_sha1(struct origin
*origin
)
266 if (!is_null_sha1(origin
->blob_sha1
))
268 if (get_tree_entry(origin
->commit
->object
.sha1
,
270 origin
->blob_sha1
, &mode
))
272 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
276 hashclr(origin
->blob_sha1
);
281 * We have an origin -- check if the same path exists in the
282 * parent and return an origin structure to represent it.
284 static struct origin
*find_origin(struct scoreboard
*sb
,
285 struct commit
*parent
,
286 struct origin
*origin
)
288 struct origin
*porigin
= NULL
;
289 struct diff_options diff_opts
;
290 const char *paths
[2];
294 * Each commit object can cache one origin in that
295 * commit. This is a freestanding copy of origin and
298 struct origin
*cached
= parent
->util
;
299 if (!strcmp(cached
->path
, origin
->path
)) {
301 * The same path between origin and its parent
302 * without renaming -- the most common case.
304 porigin
= get_origin(sb
, parent
, cached
->path
);
307 * If the origin was newly created (i.e. get_origin
308 * would call make_origin if none is found in the
309 * scoreboard), it does not know the blob_sha1,
310 * so copy it. Otherwise porigin was in the
311 * scoreboard and already knows blob_sha1.
313 if (porigin
->refcnt
== 1)
314 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
317 /* otherwise it was not very useful; free it */
322 /* See if the origin->path is different between parent
323 * and origin first. Most of the time they are the
324 * same and diff-tree is fairly efficient about this.
326 diff_setup(&diff_opts
);
327 diff_opts
.recursive
= 1;
328 diff_opts
.detect_rename
= 0;
329 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
330 paths
[0] = origin
->path
;
333 diff_tree_setup_paths(paths
, &diff_opts
);
334 if (diff_setup_done(&diff_opts
) < 0)
337 if (is_null_sha1(origin
->commit
->object
.sha1
))
338 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
340 diff_tree_sha1(parent
->tree
->object
.sha1
,
341 origin
->commit
->tree
->object
.sha1
,
343 diffcore_std(&diff_opts
);
345 /* It is either one entry that says "modified", or "created",
348 if (!diff_queued_diff
.nr
) {
349 /* The path is the same as parent */
350 porigin
= get_origin(sb
, parent
, origin
->path
);
351 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
353 else if (diff_queued_diff
.nr
!= 1)
354 die("internal error in blame::find_origin");
356 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
359 die("internal error in blame::find_origin (%c)",
362 porigin
= get_origin(sb
, parent
, origin
->path
);
363 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
367 /* Did not exist in parent, or type changed */
371 diff_flush(&diff_opts
);
374 * Create a freestanding copy that is not part of
375 * the refcounted origin found in the scoreboard, and
376 * cache it in the commit.
378 struct origin
*cached
;
380 cached
= make_origin(porigin
->commit
, porigin
->path
);
381 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
382 parent
->util
= cached
;
388 * We have an origin -- find the path that corresponds to it in its
389 * parent and return an origin structure to represent it.
391 static struct origin
*find_rename(struct scoreboard
*sb
,
392 struct commit
*parent
,
393 struct origin
*origin
)
395 struct origin
*porigin
= NULL
;
396 struct diff_options diff_opts
;
398 const char *paths
[2];
400 diff_setup(&diff_opts
);
401 diff_opts
.recursive
= 1;
402 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
403 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
404 diff_opts
.single_follow
= origin
->path
;
406 diff_tree_setup_paths(paths
, &diff_opts
);
407 if (diff_setup_done(&diff_opts
) < 0)
410 if (is_null_sha1(origin
->commit
->object
.sha1
))
411 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
413 diff_tree_sha1(parent
->tree
->object
.sha1
,
414 origin
->commit
->tree
->object
.sha1
,
416 diffcore_std(&diff_opts
);
418 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
419 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
420 if ((p
->status
== 'R' || p
->status
== 'C') &&
421 !strcmp(p
->two
->path
, origin
->path
)) {
422 porigin
= get_origin(sb
, parent
, p
->one
->path
);
423 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
427 diff_flush(&diff_opts
);
432 * Parsing of patch chunks...
435 /* line number in postimage; up to but not including this
436 * line is the same as preimage
440 /* preimage line number after this chunk */
443 /* postimage line number after this chunk */
448 struct chunk
*chunks
;
452 struct blame_diff_state
{
453 struct xdiff_emit_state xm
;
455 unsigned hunk_post_context
;
456 unsigned hunk_in_pre_context
: 1;
459 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
461 struct blame_diff_state
*state
= state_
;
463 int off1
, off2
, len1
, len2
, num
;
465 num
= state
->ret
->num
;
466 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
467 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
468 state
->ret
->chunks
[num
- 1].same
++;
470 state
->hunk_in_pre_context
= 0;
472 state
->hunk_post_context
++;
474 state
->hunk_post_context
= 0;
479 if (num
&& state
->hunk_post_context
) {
480 chunk
= &state
->ret
->chunks
[num
- 1];
481 chunk
->p_next
-= state
->hunk_post_context
;
482 chunk
->t_next
-= state
->hunk_post_context
;
484 state
->ret
->num
= ++num
;
485 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
486 sizeof(struct chunk
) * num
);
487 chunk
= &state
->ret
->chunks
[num
- 1];
488 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
493 /* Line numbers in patch output are one based. */
497 chunk
->same
= len2
? off2
: (off2
+ 1);
499 chunk
->p_next
= off1
+ (len1
? len1
: 1);
500 chunk
->t_next
= chunk
->same
+ len2
;
501 state
->hunk_in_pre_context
= 1;
502 state
->hunk_post_context
= 0;
505 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
508 struct blame_diff_state state
;
513 xpp
.flags
= XDF_NEED_MINIMAL
;
514 xecfg
.ctxlen
= context
;
516 ecb
.outf
= xdiff_outf
;
518 memset(&state
, 0, sizeof(state
));
519 state
.xm
.consume
= process_u_diff
;
520 state
.ret
= xmalloc(sizeof(struct patch
));
521 state
.ret
->chunks
= NULL
;
524 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
526 if (state
.ret
->num
) {
528 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
529 chunk
->p_next
-= state
.hunk_post_context
;
530 chunk
->t_next
-= state
.hunk_post_context
;
536 * Run diff between two origins and grab the patch output, so that
537 * we can pass blame for lines origin is currently suspected for
540 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
542 mmfile_t file_p
, file_o
;
545 fill_origin_blob(parent
, &file_p
);
546 fill_origin_blob(origin
, &file_o
);
547 if (!file_p
.ptr
|| !file_o
.ptr
)
549 patch
= compare_buffer(&file_p
, &file_o
, 0);
554 static void free_patch(struct patch
*p
)
561 * Link in a new blame entry to the scoreboard. Entries that cover the
562 * same line range have been removed from the scoreboard previously.
564 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
566 struct blame_entry
*ent
, *prev
= NULL
;
568 origin_incref(e
->suspect
);
570 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
573 /* prev, if not NULL, is the last one that is below e */
576 e
->next
= prev
->next
;
588 * src typically is on-stack; we want to copy the information in it to
589 * an malloced blame_entry that is already on the linked list of the
590 * scoreboard. The origin of dst loses a refcnt while the origin of src
593 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
595 struct blame_entry
*p
, *n
;
599 origin_incref(src
->suspect
);
600 origin_decref(dst
->suspect
);
601 memcpy(dst
, src
, sizeof(*src
));
607 static const char *nth_line(struct scoreboard
*sb
, int lno
)
609 return sb
->final_buf
+ sb
->lineno
[lno
];
613 * It is known that lines between tlno to same came from parent, and e
614 * has an overlap with that range. it also is known that parent's
615 * line plno corresponds to e's line tlno.
621 * <------------------>
623 * Split e into potentially three parts; before this chunk, the chunk
624 * to be blamed for the parent, and after that portion.
626 static void split_overlap(struct blame_entry
*split
,
627 struct blame_entry
*e
,
628 int tlno
, int plno
, int same
,
629 struct origin
*parent
)
632 memset(split
, 0, sizeof(struct blame_entry
[3]));
634 if (e
->s_lno
< tlno
) {
635 /* there is a pre-chunk part not blamed on parent */
636 split
[0].suspect
= origin_incref(e
->suspect
);
637 split
[0].lno
= e
->lno
;
638 split
[0].s_lno
= e
->s_lno
;
639 split
[0].num_lines
= tlno
- e
->s_lno
;
640 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
641 split
[1].s_lno
= plno
;
644 split
[1].lno
= e
->lno
;
645 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
648 if (same
< e
->s_lno
+ e
->num_lines
) {
649 /* there is a post-chunk part not blamed on parent */
650 split
[2].suspect
= origin_incref(e
->suspect
);
651 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
652 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
653 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
654 chunk_end_lno
= split
[2].lno
;
657 chunk_end_lno
= e
->lno
+ e
->num_lines
;
658 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
661 * if it turns out there is nothing to blame the parent for,
662 * forget about the splitting. !split[1].suspect signals this.
664 if (split
[1].num_lines
< 1)
666 split
[1].suspect
= origin_incref(parent
);
670 * split_overlap() divided an existing blame e into up to three parts
671 * in split. Adjust the linked list of blames in the scoreboard to
674 static void split_blame(struct scoreboard
*sb
,
675 struct blame_entry
*split
,
676 struct blame_entry
*e
)
678 struct blame_entry
*new_entry
;
680 if (split
[0].suspect
&& split
[2].suspect
) {
681 /* The first part (reuse storage for the existing entry e) */
682 dup_entry(e
, &split
[0]);
684 /* The last part -- me */
685 new_entry
= xmalloc(sizeof(*new_entry
));
686 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
687 add_blame_entry(sb
, new_entry
);
689 /* ... and the middle part -- parent */
690 new_entry
= xmalloc(sizeof(*new_entry
));
691 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
692 add_blame_entry(sb
, new_entry
);
694 else if (!split
[0].suspect
&& !split
[2].suspect
)
696 * The parent covers the entire area; reuse storage for
697 * e and replace it with the parent.
699 dup_entry(e
, &split
[1]);
700 else if (split
[0].suspect
) {
701 /* me and then parent */
702 dup_entry(e
, &split
[0]);
704 new_entry
= xmalloc(sizeof(*new_entry
));
705 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
706 add_blame_entry(sb
, new_entry
);
709 /* parent and then me */
710 dup_entry(e
, &split
[1]);
712 new_entry
= xmalloc(sizeof(*new_entry
));
713 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
714 add_blame_entry(sb
, new_entry
);
717 if (DEBUG
) { /* sanity */
718 struct blame_entry
*ent
;
719 int lno
= sb
->ent
->lno
, corrupt
= 0;
721 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
726 lno
+= ent
->num_lines
;
730 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
731 printf("L %8d l %8d n %8d\n",
732 lno
, ent
->lno
, ent
->num_lines
);
733 lno
= ent
->lno
+ ent
->num_lines
;
741 * After splitting the blame, the origins used by the
742 * on-stack blame_entry should lose one refcnt each.
744 static void decref_split(struct blame_entry
*split
)
748 for (i
= 0; i
< 3; i
++)
749 origin_decref(split
[i
].suspect
);
753 * Helper for blame_chunk(). blame_entry e is known to overlap with
754 * the patch hunk; split it and pass blame to the parent.
756 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
757 int tlno
, int plno
, int same
,
758 struct origin
*parent
)
760 struct blame_entry split
[3];
762 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
763 if (split
[1].suspect
)
764 split_blame(sb
, split
, e
);
769 * Find the line number of the last line the target is suspected for.
771 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
773 struct blame_entry
*e
;
774 int last_in_target
= -1;
776 for (e
= sb
->ent
; e
; e
= e
->next
) {
777 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
779 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
780 last_in_target
= e
->s_lno
+ e
->num_lines
;
782 return last_in_target
;
786 * Process one hunk from the patch between the current suspect for
787 * blame_entry e and its parent. Find and split the overlap, and
788 * pass blame to the overlapping part to the parent.
790 static void blame_chunk(struct scoreboard
*sb
,
791 int tlno
, int plno
, int same
,
792 struct origin
*target
, struct origin
*parent
)
794 struct blame_entry
*e
;
796 for (e
= sb
->ent
; e
; e
= e
->next
) {
797 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
799 if (same
<= e
->s_lno
)
801 if (tlno
< e
->s_lno
+ e
->num_lines
)
802 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
807 * We are looking at the origin 'target' and aiming to pass blame
808 * for the lines it is suspected to its parent. Run diff to find
809 * which lines came from parent and pass blame for them.
811 static int pass_blame_to_parent(struct scoreboard
*sb
,
812 struct origin
*target
,
813 struct origin
*parent
)
815 int i
, last_in_target
, plno
, tlno
;
818 last_in_target
= find_last_in_target(sb
, target
);
819 if (last_in_target
< 0)
820 return 1; /* nothing remains for this target */
822 patch
= get_patch(parent
, target
);
824 for (i
= 0; i
< patch
->num
; i
++) {
825 struct chunk
*chunk
= &patch
->chunks
[i
];
827 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
828 plno
= chunk
->p_next
;
829 tlno
= chunk
->t_next
;
831 /* The rest (i.e. anything after tlno) are the same as the parent */
832 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
839 * The lines in blame_entry after splitting blames many times can become
840 * very small and trivial, and at some point it becomes pointless to
841 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
842 * ordinary C program, and it is not worth to say it was copied from
843 * totally unrelated file in the parent.
845 * Compute how trivial the lines in the blame_entry are.
847 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
856 cp
= nth_line(sb
, e
->lno
);
857 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
859 unsigned ch
= *((unsigned char *)cp
);
869 * best_so_far[] and this[] are both a split of an existing blame_entry
870 * that passes blame to the parent. Maintain best_so_far the best split
871 * so far, by comparing this and best_so_far and copying this into
872 * bst_so_far as needed.
874 static void copy_split_if_better(struct scoreboard
*sb
,
875 struct blame_entry
*best_so_far
,
876 struct blame_entry
*this)
880 if (!this[1].suspect
)
882 if (best_so_far
[1].suspect
) {
883 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
887 for (i
= 0; i
< 3; i
++)
888 origin_incref(this[i
].suspect
);
889 decref_split(best_so_far
);
890 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
894 * We are looking at a part of the final image represented by
895 * ent (tlno and same are offset by ent->s_lno).
896 * tlno is where we are looking at in the final image.
897 * up to (but not including) same match preimage.
898 * plno is where we are looking at in the preimage.
900 * <-------------- final image ---------------------->
903 * <---------preimage----->
906 * All line numbers are 0-based.
908 static void handle_split(struct scoreboard
*sb
,
909 struct blame_entry
*ent
,
910 int tlno
, int plno
, int same
,
911 struct origin
*parent
,
912 struct blame_entry
*split
)
914 if (ent
->num_lines
<= tlno
)
917 struct blame_entry
this[3];
920 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
921 copy_split_if_better(sb
, split
, this);
927 * Find the lines from parent that are the same as ent so that
928 * we can pass blames to it. file_p has the blob contents for
931 static void find_copy_in_blob(struct scoreboard
*sb
,
932 struct blame_entry
*ent
,
933 struct origin
*parent
,
934 struct blame_entry
*split
,
944 * Prepare mmfile that contains only the lines in ent.
946 cp
= nth_line(sb
, ent
->lno
);
947 file_o
.ptr
= (char*) cp
;
948 cnt
= ent
->num_lines
;
950 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
954 file_o
.size
= cp
- file_o
.ptr
;
956 patch
= compare_buffer(file_p
, &file_o
, 1);
959 * file_o is a part of final image we are annotating.
960 * file_p partially may match that image.
962 memset(split
, 0, sizeof(struct blame_entry
[3]));
964 for (i
= 0; i
< patch
->num
; i
++) {
965 struct chunk
*chunk
= &patch
->chunks
[i
];
967 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
968 plno
= chunk
->p_next
;
969 tlno
= chunk
->t_next
;
971 /* remainder, if any, all match the preimage */
972 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
977 * See if lines currently target is suspected for can be attributed to
980 static int find_move_in_parent(struct scoreboard
*sb
,
981 struct origin
*target
,
982 struct origin
*parent
)
984 int last_in_target
, made_progress
;
985 struct blame_entry
*e
, split
[3];
988 last_in_target
= find_last_in_target(sb
, target
);
989 if (last_in_target
< 0)
990 return 1; /* nothing remains for this target */
992 fill_origin_blob(parent
, &file_p
);
997 while (made_progress
) {
999 for (e
= sb
->ent
; e
; e
= e
->next
) {
1000 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1002 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1003 if (split
[1].suspect
&&
1004 blame_move_score
< ent_score(sb
, &split
[1])) {
1005 split_blame(sb
, split
, e
);
1008 decref_split(split
);
1015 struct blame_entry
*ent
;
1016 struct blame_entry split
[3];
1020 * Count the number of entries the target is suspected for,
1021 * and prepare a list of entry and the best split.
1023 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1024 struct origin
*target
,
1027 struct blame_entry
*e
;
1029 struct blame_list
*blame_list
= NULL
;
1031 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1032 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1035 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1036 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1037 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1038 blame_list
[i
++].ent
= e
;
1040 *num_ents_p
= num_ents
;
1045 * For lines target is suspected for, see if we can find code movement
1046 * across file boundary from the parent commit. porigin is the path
1047 * in the parent we already tried.
1049 static int find_copy_in_parent(struct scoreboard
*sb
,
1050 struct origin
*target
,
1051 struct commit
*parent
,
1052 struct origin
*porigin
,
1055 struct diff_options diff_opts
;
1056 const char *paths
[1];
1059 struct blame_list
*blame_list
;
1062 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1064 return 1; /* nothing remains for this target */
1066 diff_setup(&diff_opts
);
1067 diff_opts
.recursive
= 1;
1068 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1071 diff_tree_setup_paths(paths
, &diff_opts
);
1072 if (diff_setup_done(&diff_opts
) < 0)
1075 /* Try "find copies harder" on new path if requested;
1076 * we do not want to use diffcore_rename() actually to
1077 * match things up; find_copies_harder is set only to
1078 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1079 * and this code needs to be after diff_setup_done(), which
1080 * usually makes find-copies-harder imply copy detection.
1082 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
1083 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
1084 diff_opts
.find_copies_harder
= 1;
1086 if (is_null_sha1(target
->commit
->object
.sha1
))
1087 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1089 diff_tree_sha1(parent
->tree
->object
.sha1
,
1090 target
->commit
->tree
->object
.sha1
,
1093 if (!diff_opts
.find_copies_harder
)
1094 diffcore_std(&diff_opts
);
1098 int made_progress
= 0;
1100 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1101 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1102 struct origin
*norigin
;
1104 struct blame_entry
this[3];
1106 if (!DIFF_FILE_VALID(p
->one
))
1107 continue; /* does not exist in parent */
1108 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1109 /* find_move already dealt with this path */
1112 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1113 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1114 fill_origin_blob(norigin
, &file_p
);
1118 for (j
= 0; j
< num_ents
; j
++) {
1119 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1120 norigin
, this, &file_p
);
1121 copy_split_if_better(sb
, blame_list
[j
].split
,
1125 origin_decref(norigin
);
1128 for (j
= 0; j
< num_ents
; j
++) {
1129 struct blame_entry
*split
= blame_list
[j
].split
;
1130 if (split
[1].suspect
&&
1131 blame_copy_score
< ent_score(sb
, &split
[1])) {
1132 split_blame(sb
, split
, blame_list
[j
].ent
);
1135 decref_split(split
);
1141 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1147 diff_flush(&diff_opts
);
1153 * The blobs of origin and porigin exactly match, so everything
1154 * origin is suspected for can be blamed on the parent.
1156 static void pass_whole_blame(struct scoreboard
*sb
,
1157 struct origin
*origin
, struct origin
*porigin
)
1159 struct blame_entry
*e
;
1161 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1162 /* Steal its file */
1163 porigin
->file
= origin
->file
;
1164 origin
->file
.ptr
= NULL
;
1166 for (e
= sb
->ent
; e
; e
= e
->next
) {
1167 if (!same_suspect(e
->suspect
, origin
))
1169 origin_incref(porigin
);
1170 origin_decref(e
->suspect
);
1171 e
->suspect
= porigin
;
1175 #define MAXPARENT 16
1177 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1180 struct commit
*commit
= origin
->commit
;
1181 struct commit_list
*parent
;
1182 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1184 memset(parent_origin
, 0, sizeof(parent_origin
));
1186 /* The first pass looks for unrenamed path to optimize for
1187 * common cases, then we look for renames in the second pass.
1189 for (pass
= 0; pass
< 2; pass
++) {
1190 struct origin
*(*find
)(struct scoreboard
*,
1191 struct commit
*, struct origin
*);
1192 find
= pass
? find_rename
: find_origin
;
1194 for (i
= 0, parent
= commit
->parents
;
1195 i
< MAXPARENT
&& parent
;
1196 parent
= parent
->next
, i
++) {
1197 struct commit
*p
= parent
->item
;
1200 if (parent_origin
[i
])
1202 if (parse_commit(p
))
1204 porigin
= find(sb
, p
, origin
);
1207 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1208 pass_whole_blame(sb
, origin
, porigin
);
1209 origin_decref(porigin
);
1212 for (j
= same
= 0; j
< i
; j
++)
1213 if (parent_origin
[j
] &&
1214 !hashcmp(parent_origin
[j
]->blob_sha1
,
1215 porigin
->blob_sha1
)) {
1220 parent_origin
[i
] = porigin
;
1222 origin_decref(porigin
);
1227 for (i
= 0, parent
= commit
->parents
;
1228 i
< MAXPARENT
&& parent
;
1229 parent
= parent
->next
, i
++) {
1230 struct origin
*porigin
= parent_origin
[i
];
1233 if (pass_blame_to_parent(sb
, origin
, porigin
))
1238 * Optionally find moves in parents' files.
1240 if (opt
& PICKAXE_BLAME_MOVE
)
1241 for (i
= 0, parent
= commit
->parents
;
1242 i
< MAXPARENT
&& parent
;
1243 parent
= parent
->next
, i
++) {
1244 struct origin
*porigin
= parent_origin
[i
];
1247 if (find_move_in_parent(sb
, origin
, porigin
))
1252 * Optionally find copies from parents' files.
1254 if (opt
& PICKAXE_BLAME_COPY
)
1255 for (i
= 0, parent
= commit
->parents
;
1256 i
< MAXPARENT
&& parent
;
1257 parent
= parent
->next
, i
++) {
1258 struct origin
*porigin
= parent_origin
[i
];
1259 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1265 for (i
= 0; i
< MAXPARENT
; i
++)
1266 origin_decref(parent_origin
[i
]);
1270 * Information on commits, used for output.
1275 const char *author_mail
;
1276 unsigned long author_time
;
1277 const char *author_tz
;
1279 /* filled only when asked for details */
1280 const char *committer
;
1281 const char *committer_mail
;
1282 unsigned long committer_time
;
1283 const char *committer_tz
;
1285 const char *summary
;
1289 * Parse author/committer line in the commit object buffer
1291 static void get_ac_line(const char *inbuf
, const char *what
,
1292 int bufsz
, char *person
, const char **mail
,
1293 unsigned long *time
, const char **tz
)
1298 tmp
= strstr(inbuf
, what
);
1301 tmp
+= strlen(what
);
1302 endp
= strchr(tmp
, '\n');
1310 *mail
= *tz
= "(unknown)";
1314 memcpy(person
, tmp
, len
);
1326 *time
= strtoul(tmp
, NULL
, 10);
1335 static void get_commit_info(struct commit
*commit
,
1336 struct commit_info
*ret
,
1341 static char author_buf
[1024];
1342 static char committer_buf
[1024];
1343 static char summary_buf
[1024];
1346 * We've operated without save_commit_buffer, so
1347 * we now need to populate them for output.
1349 if (!commit
->buffer
) {
1350 enum object_type type
;
1353 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1355 ret
->author
= author_buf
;
1356 get_ac_line(commit
->buffer
, "\nauthor ",
1357 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1358 &ret
->author_time
, &ret
->author_tz
);
1363 ret
->committer
= committer_buf
;
1364 get_ac_line(commit
->buffer
, "\ncommitter ",
1365 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1366 &ret
->committer_time
, &ret
->committer_tz
);
1368 ret
->summary
= summary_buf
;
1369 tmp
= strstr(commit
->buffer
, "\n\n");
1372 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1376 endp
= strchr(tmp
, '\n');
1378 endp
= tmp
+ strlen(tmp
);
1380 if (len
>= sizeof(summary_buf
) || len
== 0)
1382 memcpy(summary_buf
, tmp
, len
);
1383 summary_buf
[len
] = 0;
1387 * To allow LF and other nonportable characters in pathnames,
1388 * they are c-style quoted as needed.
1390 static void write_filename_info(const char *path
)
1392 printf("filename ");
1393 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1398 * The blame_entry is found to be guilty for the range. Mark it
1399 * as such, and show it in incremental output.
1401 static void found_guilty_entry(struct blame_entry
*ent
)
1407 struct origin
*suspect
= ent
->suspect
;
1409 printf("%s %d %d %d\n",
1410 sha1_to_hex(suspect
->commit
->object
.sha1
),
1411 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1412 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1413 struct commit_info ci
;
1414 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1415 get_commit_info(suspect
->commit
, &ci
, 1);
1416 printf("author %s\n", ci
.author
);
1417 printf("author-mail %s\n", ci
.author_mail
);
1418 printf("author-time %lu\n", ci
.author_time
);
1419 printf("author-tz %s\n", ci
.author_tz
);
1420 printf("committer %s\n", ci
.committer
);
1421 printf("committer-mail %s\n", ci
.committer_mail
);
1422 printf("committer-time %lu\n", ci
.committer_time
);
1423 printf("committer-tz %s\n", ci
.committer_tz
);
1424 printf("summary %s\n", ci
.summary
);
1425 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1426 printf("boundary\n");
1428 write_filename_info(suspect
->path
);
1433 * The main loop -- while the scoreboard has lines whose true origin
1434 * is still unknown, pick one blame_entry, and allow its current
1435 * suspect to pass blames to its parents.
1437 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1440 struct blame_entry
*ent
;
1441 struct commit
*commit
;
1442 struct origin
*suspect
= NULL
;
1444 /* find one suspect to break down */
1445 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1447 suspect
= ent
->suspect
;
1449 return; /* all done */
1452 * We will use this suspect later in the loop,
1453 * so hold onto it in the meantime.
1455 origin_incref(suspect
);
1456 commit
= suspect
->commit
;
1457 if (!commit
->object
.parsed
)
1458 parse_commit(commit
);
1459 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1460 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1461 pass_blame(sb
, suspect
, opt
);
1463 commit
->object
.flags
|= UNINTERESTING
;
1464 if (commit
->object
.parsed
)
1465 mark_parents_uninteresting(commit
);
1467 /* treat root commit as boundary */
1468 if (!commit
->parents
&& !show_root
)
1469 commit
->object
.flags
|= UNINTERESTING
;
1471 /* Take responsibility for the remaining entries */
1472 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1473 if (same_suspect(ent
->suspect
, suspect
))
1474 found_guilty_entry(ent
);
1475 origin_decref(suspect
);
1477 if (DEBUG
) /* sanity */
1478 sanity_check_refcnt(sb
);
1482 static const char *format_time(unsigned long time
, const char *tz_str
,
1485 static char time_buf
[128];
1490 if (show_raw_time
) {
1491 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1496 minutes
= tz
< 0 ? -tz
: tz
;
1497 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1498 minutes
= tz
< 0 ? -minutes
: minutes
;
1499 t
= time
+ minutes
* 60;
1502 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1503 strcat(time_buf
, tz_str
);
1507 #define OUTPUT_ANNOTATE_COMPAT 001
1508 #define OUTPUT_LONG_OBJECT_NAME 002
1509 #define OUTPUT_RAW_TIMESTAMP 004
1510 #define OUTPUT_PORCELAIN 010
1511 #define OUTPUT_SHOW_NAME 020
1512 #define OUTPUT_SHOW_NUMBER 040
1513 #define OUTPUT_SHOW_SCORE 0100
1515 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1519 struct origin
*suspect
= ent
->suspect
;
1522 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1523 printf("%s%c%d %d %d\n",
1525 ent
->guilty
? ' ' : '*', // purely for debugging
1529 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1530 struct commit_info ci
;
1531 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1532 get_commit_info(suspect
->commit
, &ci
, 1);
1533 printf("author %s\n", ci
.author
);
1534 printf("author-mail %s\n", ci
.author_mail
);
1535 printf("author-time %lu\n", ci
.author_time
);
1536 printf("author-tz %s\n", ci
.author_tz
);
1537 printf("committer %s\n", ci
.committer
);
1538 printf("committer-mail %s\n", ci
.committer_mail
);
1539 printf("committer-time %lu\n", ci
.committer_time
);
1540 printf("committer-tz %s\n", ci
.committer_tz
);
1541 write_filename_info(suspect
->path
);
1542 printf("summary %s\n", ci
.summary
);
1543 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1544 printf("boundary\n");
1546 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1547 write_filename_info(suspect
->path
);
1549 cp
= nth_line(sb
, ent
->lno
);
1550 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1553 printf("%s %d %d\n", hex
,
1554 ent
->s_lno
+ 1 + cnt
,
1555 ent
->lno
+ 1 + cnt
);
1560 } while (ch
!= '\n' &&
1561 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1565 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1569 struct origin
*suspect
= ent
->suspect
;
1570 struct commit_info ci
;
1572 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1574 get_commit_info(suspect
->commit
, &ci
, 1);
1575 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1577 cp
= nth_line(sb
, ent
->lno
);
1578 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1580 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1582 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1584 memset(hex
, ' ', length
);
1585 else if (!cmd_is_annotate
) {
1591 printf("%.*s", length
, hex
);
1592 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1593 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1594 format_time(ci
.author_time
, ci
.author_tz
,
1596 ent
->lno
+ 1 + cnt
);
1598 if (opt
& OUTPUT_SHOW_SCORE
)
1600 max_score_digits
, ent
->score
,
1601 ent
->suspect
->refcnt
);
1602 if (opt
& OUTPUT_SHOW_NAME
)
1603 printf(" %-*.*s", longest_file
, longest_file
,
1605 if (opt
& OUTPUT_SHOW_NUMBER
)
1606 printf(" %*d", max_orig_digits
,
1607 ent
->s_lno
+ 1 + cnt
);
1608 printf(" (%-*.*s %10s %*d) ",
1609 longest_author
, longest_author
, ci
.author
,
1610 format_time(ci
.author_time
, ci
.author_tz
,
1612 max_digits
, ent
->lno
+ 1 + cnt
);
1617 } while (ch
!= '\n' &&
1618 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1622 static void output(struct scoreboard
*sb
, int option
)
1624 struct blame_entry
*ent
;
1626 if (option
& OUTPUT_PORCELAIN
) {
1627 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1628 struct blame_entry
*oth
;
1629 struct origin
*suspect
= ent
->suspect
;
1630 struct commit
*commit
= suspect
->commit
;
1631 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1633 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1634 if ((oth
->suspect
->commit
!= commit
) ||
1635 !strcmp(oth
->suspect
->path
, suspect
->path
))
1637 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1643 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1644 if (option
& OUTPUT_PORCELAIN
)
1645 emit_porcelain(sb
, ent
);
1647 emit_other(sb
, ent
, option
);
1653 * To allow quick access to the contents of nth line in the
1654 * final image, prepare an index in the scoreboard.
1656 static int prepare_lines(struct scoreboard
*sb
)
1658 const char *buf
= sb
->final_buf
;
1659 unsigned long len
= sb
->final_buf_size
;
1660 int num
= 0, incomplete
= 0, bol
= 1;
1662 if (len
&& buf
[len
-1] != '\n')
1663 incomplete
++; /* incomplete line at the end */
1666 sb
->lineno
= xrealloc(sb
->lineno
,
1667 sizeof(int* ) * (num
+ 1));
1668 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1671 if (*buf
++ == '\n') {
1676 sb
->lineno
= xrealloc(sb
->lineno
,
1677 sizeof(int* ) * (num
+ incomplete
+ 1));
1678 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1679 sb
->num_lines
= num
+ incomplete
;
1680 return sb
->num_lines
;
1684 * Add phony grafts for use with -S; this is primarily to
1685 * support git-cvsserver that wants to give a linear history
1688 static int read_ancestry(const char *graft_file
)
1690 FILE *fp
= fopen(graft_file
, "r");
1694 while (fgets(buf
, sizeof(buf
), fp
)) {
1695 /* The format is just "Commit Parent1 Parent2 ...\n" */
1696 int len
= strlen(buf
);
1697 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1699 register_commit_graft(graft
, 0);
1706 * How many columns do we need to show line numbers in decimal?
1708 static int lineno_width(int lines
)
1712 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1718 * How many columns do we need to show line numbers, authors,
1721 static void find_alignment(struct scoreboard
*sb
, int *option
)
1723 int longest_src_lines
= 0;
1724 int longest_dst_lines
= 0;
1725 unsigned largest_score
= 0;
1726 struct blame_entry
*e
;
1728 for (e
= sb
->ent
; e
; e
= e
->next
) {
1729 struct origin
*suspect
= e
->suspect
;
1730 struct commit_info ci
;
1733 if (strcmp(suspect
->path
, sb
->path
))
1734 *option
|= OUTPUT_SHOW_NAME
;
1735 num
= strlen(suspect
->path
);
1736 if (longest_file
< num
)
1738 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1739 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1740 get_commit_info(suspect
->commit
, &ci
, 1);
1741 num
= strlen(ci
.author
);
1742 if (longest_author
< num
)
1743 longest_author
= num
;
1745 num
= e
->s_lno
+ e
->num_lines
;
1746 if (longest_src_lines
< num
)
1747 longest_src_lines
= num
;
1748 num
= e
->lno
+ e
->num_lines
;
1749 if (longest_dst_lines
< num
)
1750 longest_dst_lines
= num
;
1751 if (largest_score
< ent_score(sb
, e
))
1752 largest_score
= ent_score(sb
, e
);
1754 max_orig_digits
= lineno_width(longest_src_lines
);
1755 max_digits
= lineno_width(longest_dst_lines
);
1756 max_score_digits
= lineno_width(largest_score
);
1760 * For debugging -- origin is refcounted, and this asserts that
1761 * we do not underflow.
1763 static void sanity_check_refcnt(struct scoreboard
*sb
)
1766 struct blame_entry
*ent
;
1768 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1769 /* Nobody should have zero or negative refcnt */
1770 if (ent
->suspect
->refcnt
<= 0) {
1771 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1773 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1774 ent
->suspect
->refcnt
);
1778 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1779 /* Mark the ones that haven't been checked */
1780 if (0 < ent
->suspect
->refcnt
)
1781 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1783 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1785 * ... then pick each and see if they have the the
1789 struct blame_entry
*e
;
1790 struct origin
*suspect
= ent
->suspect
;
1792 if (0 < suspect
->refcnt
)
1794 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1795 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1796 if (e
->suspect
!= suspect
)
1800 if (suspect
->refcnt
!= found
) {
1801 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1803 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1804 ent
->suspect
->refcnt
, found
);
1810 find_alignment(sb
, &opt
);
1812 die("Baa %d!", baa
);
1817 * Used for the command line parsing; check if the path exists
1818 * in the working tree.
1820 static int has_path_in_work_tree(const char *path
)
1823 return !lstat(path
, &st
);
1826 static unsigned parse_score(const char *arg
)
1829 unsigned long score
= strtoul(arg
, &end
, 10);
1835 static const char *add_prefix(const char *prefix
, const char *path
)
1837 if (!prefix
|| !prefix
[0])
1839 return prefix_path(prefix
, strlen(prefix
), path
);
1843 * Parsing of (comma separated) one item in the -L option
1845 static const char *parse_loc(const char *spec
,
1846 struct scoreboard
*sb
, long lno
,
1847 long begin
, long *ret
)
1854 regmatch_t match
[1];
1856 /* Allow "-L <something>,+20" to mean starting at <something>
1857 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1860 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1861 num
= strtol(spec
+ 1, &term
, 10);
1862 if (term
!= spec
+ 1) {
1866 *ret
= begin
+ num
- 2;
1875 num
= strtol(spec
, &term
, 10);
1883 /* it could be a regexp of form /.../ */
1884 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1891 /* try [spec+1 .. term-1] as regexp */
1893 begin
--; /* input is in human terms */
1894 line
= nth_line(sb
, begin
);
1896 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1897 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1898 const char *cp
= line
+ match
[0].rm_so
;
1901 while (begin
++ < lno
) {
1902 nline
= nth_line(sb
, begin
);
1903 if (line
<= cp
&& cp
< nline
)
1914 regerror(reg_error
, ®exp
, errbuf
, 1024);
1915 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1920 * Parsing of -L option
1922 static void prepare_blame_range(struct scoreboard
*sb
,
1923 const char *bottomtop
,
1925 long *bottom
, long *top
)
1929 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1931 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1939 static int git_blame_config(const char *var
, const char *value
)
1941 if (!strcmp(var
, "blame.showroot")) {
1942 show_root
= git_config_bool(var
, value
);
1945 if (!strcmp(var
, "blame.blankboundary")) {
1946 blank_boundary
= git_config_bool(var
, value
);
1949 return git_default_config(var
, value
);
1952 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1954 struct commit
*commit
;
1955 struct origin
*origin
;
1956 unsigned char head_sha1
[20];
1961 unsigned long fin_size
;
1963 struct cache_entry
*ce
;
1966 if (get_sha1("HEAD", head_sha1
))
1967 die("No such ref: HEAD");
1970 commit
= xcalloc(1, sizeof(*commit
));
1971 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
1972 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
1973 commit
->object
.parsed
= 1;
1975 commit
->object
.type
= OBJ_COMMIT
;
1977 origin
= make_origin(commit
, path
);
1979 if (!contents_from
|| strcmp("-", contents_from
)) {
1981 const char *read_from
;
1983 if (contents_from
) {
1984 if (stat(contents_from
, &st
) < 0)
1985 die("Cannot stat %s", contents_from
);
1986 read_from
= contents_from
;
1989 if (lstat(path
, &st
) < 0)
1990 die("Cannot lstat %s", path
);
1993 fin_size
= xsize_t(st
.st_size
);
1994 buf
= xmalloc(fin_size
+1);
1995 mode
= canon_mode(st
.st_mode
);
1996 switch (st
.st_mode
& S_IFMT
) {
1998 fd
= open(read_from
, O_RDONLY
);
2000 die("cannot open %s", read_from
);
2001 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
2002 die("cannot read %s", read_from
);
2005 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
2006 die("cannot readlink %s", read_from
);
2009 die("unsupported file type %s", read_from
);
2013 /* Reading from stdin */
2014 contents_from
= "standard input";
2020 buf
= xrealloc(buf
, fin_size
+ cnt
);
2021 cnt
= xread(0, buf
+ fin_size
, cnt
);
2023 die("read error %s from stdin",
2029 buf
= xrealloc(buf
, fin_size
+ 1);
2032 origin
->file
.ptr
= buf
;
2033 origin
->file
.size
= fin_size
;
2034 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2035 commit
->util
= origin
;
2038 * Read the current index, replace the path entry with
2039 * origin->blob_sha1 without mucking with its mode or type
2040 * bits; we are not going to write this index out -- we just
2041 * want to run "diff-index --cached".
2048 int pos
= cache_name_pos(path
, len
);
2050 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2052 /* Let's not bother reading from HEAD tree */
2053 mode
= S_IFREG
| 0644;
2055 size
= cache_entry_size(len
);
2056 ce
= xcalloc(1, size
);
2057 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2058 memcpy(ce
->name
, path
, len
);
2059 ce
->ce_flags
= create_ce_flags(len
, 0);
2060 ce
->ce_mode
= create_ce_mode(mode
);
2061 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2064 * We are not going to write this out, so this does not matter
2065 * right now, but someday we might optimize diff-index --cached
2066 * with cache-tree information.
2068 cache_tree_invalidate_path(active_cache_tree
, path
);
2070 commit
->buffer
= xmalloc(400);
2071 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2072 snprintf(commit
->buffer
, 400,
2073 "tree 0000000000000000000000000000000000000000\n"
2077 "Version of %s from %s\n",
2078 sha1_to_hex(head_sha1
),
2079 ident
, ident
, path
, contents_from
? contents_from
: path
);
2083 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2085 struct rev_info revs
;
2087 struct scoreboard sb
;
2089 struct blame_entry
*ent
;
2090 int i
, seen_dashdash
, unk
, opt
;
2091 long bottom
, top
, lno
;
2092 int output_option
= 0;
2094 const char *revs_file
= NULL
;
2095 const char *final_commit_name
= NULL
;
2096 enum object_type type
;
2097 const char *bottomtop
= NULL
;
2098 const char *contents_from
= NULL
;
2100 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2102 git_config(git_blame_config
);
2103 save_commit_buffer
= 0;
2107 for (unk
= i
= 1; i
< argc
; i
++) {
2108 const char *arg
= argv
[i
];
2111 else if (!strcmp("-b", arg
))
2113 else if (!strcmp("--root", arg
))
2115 else if (!strcmp(arg
, "--show-stats"))
2117 else if (!strcmp("-c", arg
))
2118 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2119 else if (!strcmp("-t", arg
))
2120 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2121 else if (!strcmp("-l", arg
))
2122 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2123 else if (!strcmp("-S", arg
) && ++i
< argc
)
2124 revs_file
= argv
[i
];
2125 else if (!prefixcmp(arg
, "-M")) {
2126 opt
|= PICKAXE_BLAME_MOVE
;
2127 blame_move_score
= parse_score(arg
+2);
2129 else if (!prefixcmp(arg
, "-C")) {
2130 if (opt
& PICKAXE_BLAME_COPY
)
2131 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2132 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2133 blame_copy_score
= parse_score(arg
+2);
2135 else if (!prefixcmp(arg
, "-L")) {
2144 die("More than one '-L n,m' option given");
2147 else if (!strcmp("--contents", arg
)) {
2150 contents_from
= argv
[i
];
2152 else if (!strcmp("--incremental", arg
))
2154 else if (!strcmp("--score-debug", arg
))
2155 output_option
|= OUTPUT_SHOW_SCORE
;
2156 else if (!strcmp("-f", arg
) ||
2157 !strcmp("--show-name", arg
))
2158 output_option
|= OUTPUT_SHOW_NAME
;
2159 else if (!strcmp("-n", arg
) ||
2160 !strcmp("--show-number", arg
))
2161 output_option
|= OUTPUT_SHOW_NUMBER
;
2162 else if (!strcmp("-p", arg
) ||
2163 !strcmp("--porcelain", arg
))
2164 output_option
|= OUTPUT_PORCELAIN
;
2165 else if (!strcmp("--", arg
)) {
2177 if (!blame_move_score
)
2178 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2179 if (!blame_copy_score
)
2180 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2183 * We have collected options unknown to us in argv[1..unk]
2184 * which are to be passed to revision machinery if we are
2185 * going to do the "bottom" processing.
2187 * The remaining are:
2189 * (1) if seen_dashdash, its either
2190 * "-options -- <path>" or
2191 * "-options -- <path> <rev>".
2192 * but the latter is allowed only if there is no
2193 * options that we passed to revision machinery.
2195 * (2) otherwise, we may have "--" somewhere later and
2196 * might be looking at the first one of multiple 'rev'
2197 * parameters (e.g. " master ^next ^maint -- path").
2198 * See if there is a dashdash first, and give the
2199 * arguments before that to revision machinery.
2200 * After that there must be one 'path'.
2202 * (3) otherwise, its one of the three:
2203 * "-options <path> <rev>"
2204 * "-options <rev> <path>"
2206 * but again the first one is allowed only if
2207 * there is no options that we passed to revision
2211 if (seen_dashdash
) {
2215 path
= add_prefix(prefix
, argv
[i
]);
2216 if (i
+ 1 == argc
- 1) {
2219 argv
[unk
++] = argv
[i
+ 1];
2221 else if (i
+ 1 != argc
)
2222 /* garbage at end */
2227 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2228 if (!strcmp(argv
[j
], "--"))
2230 if (seen_dashdash
) {
2232 if (seen_dashdash
+ 1 != argc
- 1)
2234 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2235 for (j
= i
; j
< seen_dashdash
; j
++)
2236 argv
[unk
++] = argv
[j
];
2242 path
= add_prefix(prefix
, argv
[i
]);
2243 if (i
+ 1 == argc
- 1) {
2244 final_commit_name
= argv
[i
+ 1];
2246 /* if (unk == 1) we could be getting
2249 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2250 path
= add_prefix(prefix
, argv
[i
+ 1]);
2251 final_commit_name
= argv
[i
];
2254 else if (i
!= argc
- 1)
2255 usage(blame_usage
); /* garbage at end */
2257 if (!has_path_in_work_tree(path
))
2258 die("cannot stat path %s: %s",
2259 path
, strerror(errno
));
2263 if (final_commit_name
)
2264 argv
[unk
++] = final_commit_name
;
2267 * Now we got rev and path. We do not want the path pruning
2268 * but we may want "bottom" processing.
2270 argv
[unk
++] = "--"; /* terminate the rev name */
2273 init_revisions(&revs
, NULL
);
2274 setup_revisions(unk
, argv
, &revs
, NULL
);
2275 memset(&sb
, 0, sizeof(sb
));
2278 * There must be one and only one positive commit in the
2279 * revs->pending array.
2281 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2282 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2283 if (obj
->flags
& UNINTERESTING
)
2285 while (obj
->type
== OBJ_TAG
)
2286 obj
= deref_tag(obj
, NULL
, 0);
2287 if (obj
->type
!= OBJ_COMMIT
)
2288 die("Non commit %s?",
2289 revs
.pending
.objects
[i
].name
);
2291 die("More than one commit to dig from %s and %s?",
2292 revs
.pending
.objects
[i
].name
,
2294 sb
.final
= (struct commit
*) obj
;
2295 final_commit_name
= revs
.pending
.objects
[i
].name
;
2300 * "--not A B -- path" without anything positive;
2301 * do not default to HEAD, but use the working tree
2304 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2305 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2307 else if (contents_from
)
2308 die("Cannot use --contents with final commit object name");
2311 * If we have bottom, this will mark the ancestors of the
2312 * bottom commits we would reach while traversing as
2315 prepare_revision_walk(&revs
);
2317 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2320 buf
= xmalloc(o
->file
.size
+ 1);
2321 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2323 sb
.final_buf_size
= o
->file
.size
;
2326 o
= get_origin(&sb
, sb
.final
, path
);
2327 if (fill_blob_sha1(o
))
2328 die("no such path %s in %s", path
, final_commit_name
);
2330 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2331 &sb
.final_buf_size
);
2334 lno
= prepare_lines(&sb
);
2338 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2339 if (bottom
&& top
&& top
< bottom
) {
2341 tmp
= top
; top
= bottom
; bottom
= tmp
;
2349 die("file %s has only %lu lines", path
, lno
);
2351 ent
= xcalloc(1, sizeof(*ent
));
2353 ent
->num_lines
= top
- bottom
;
2355 ent
->s_lno
= bottom
;
2360 if (revs_file
&& read_ancestry(revs_file
))
2361 die("reading graft file %s failed: %s",
2362 revs_file
, strerror(errno
));
2364 assign_blame(&sb
, &revs
, opt
);
2371 if (!(output_option
& OUTPUT_PORCELAIN
))
2372 find_alignment(&sb
, &output_option
);
2374 output(&sb
, output_option
);
2375 free((void *)sb
.final_buf
);
2376 for (ent
= sb
.ent
; ent
; ) {
2377 struct blame_entry
*e
= ent
->next
;
2383 printf("num read blob: %d\n", num_read_blob
);
2384 printf("num get patch: %d\n", num_get_patch
);
2385 printf("num commits: %d\n", num_commits
);