4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "path-list.h"
22 static char blame_usage
[] =
23 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
24 " -c Use the same output mode as git-annotate (Default: off)\n"
25 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
26 " -l Show long commit SHA1 (Default: off)\n"
27 " --root Do not treat root commits as boundaries (Default: off)\n"
28 " -t Show raw timestamp (Default: off)\n"
29 " -f, --show-name Show original filename (Default: auto)\n"
30 " -n, --show-number Show original linenumber (Default: off)\n"
31 " -s Suppress author name and timestamp (Default: off)\n"
32 " -p, --porcelain Show in a format designed for machine consumption\n"
33 " -w Ignore whitespace differences\n"
34 " -L n,m Process only line range n,m, counting from 1\n"
35 " -M, -C Find line movements within and across files\n"
36 " --incremental Show blame entries as we find them, incrementally\n"
37 " --contents file Use <file>'s contents as the final image\n"
38 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
40 static int longest_file
;
41 static int longest_author
;
42 static int max_orig_digits
;
43 static int max_digits
;
44 static int max_score_digits
;
46 static int blank_boundary
;
47 static int incremental
;
48 static int cmd_is_annotate
;
49 static int xdl_opts
= XDF_NEED_MINIMAL
;
50 static struct path_list mailmap
;
57 static int num_read_blob
;
58 static int num_get_patch
;
59 static int num_commits
;
61 #define PICKAXE_BLAME_MOVE 01
62 #define PICKAXE_BLAME_COPY 02
63 #define PICKAXE_BLAME_COPY_HARDER 04
64 #define PICKAXE_BLAME_COPY_HARDEST 010
67 * blame for a blame_entry with score lower than these thresholds
68 * is not passed to the parent using move/copy logic.
70 static unsigned blame_move_score
;
71 static unsigned blame_copy_score
;
72 #define BLAME_DEFAULT_MOVE_SCORE 20
73 #define BLAME_DEFAULT_COPY_SCORE 40
75 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76 #define METAINFO_SHOWN (1u<<12)
77 #define MORE_THAN_ONE_PATH (1u<<13)
80 * One blob in a commit that is being suspected
84 struct commit
*commit
;
86 unsigned char blob_sha1
[20];
87 char path
[FLEX_ARRAY
];
91 * Given an origin, prepare mmfile_t structure to be used by the
94 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
97 enum object_type type
;
99 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
100 (unsigned long *)(&(file
->size
)));
102 die("Cannot read blob %s for path %s",
103 sha1_to_hex(o
->blob_sha1
),
113 * Origin is refcounted and usually we keep the blob contents to be
116 static inline struct origin
*origin_incref(struct origin
*o
)
123 static void origin_decref(struct origin
*o
)
125 if (o
&& --o
->refcnt
<= 0) {
128 memset(o
, 0, sizeof(*o
));
134 * Each group of lines is described by a blame_entry; it can be split
135 * as we pass blame to the parents. They form a linked list in the
136 * scoreboard structure, sorted by the target line number.
139 struct blame_entry
*prev
;
140 struct blame_entry
*next
;
142 /* the first line of this group in the final image;
143 * internally all line numbers are 0 based.
147 /* how many lines this group has */
150 /* the commit that introduced this group into the final image */
151 struct origin
*suspect
;
153 /* true if the suspect is truly guilty; false while we have not
154 * checked if the group came from one of its parents.
158 /* the line number of the first line of this group in the
159 * suspect's file; internally all line numbers are 0 based.
163 /* how significant this entry is -- cached to avoid
164 * scanning the lines over and over.
170 * The current state of the blame assignment.
173 /* the final commit (i.e. where we started digging from) */
174 struct commit
*final
;
179 * The contents in the final image.
180 * Used by many functions to obtain contents of the nth line,
181 * indexed with scoreboard.lineno[blame_entry.lno].
183 const char *final_buf
;
184 unsigned long final_buf_size
;
186 /* linked list of blames */
187 struct blame_entry
*ent
;
189 /* look-up a line in the final buffer */
194 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
198 if (a
->commit
!= b
->commit
)
200 return !strcmp(a
->path
, b
->path
);
203 static void sanity_check_refcnt(struct scoreboard
*);
206 * If two blame entries that are next to each other came from
207 * contiguous lines in the same origin (i.e. <commit, path> pair),
208 * merge them together.
210 static void coalesce(struct scoreboard
*sb
)
212 struct blame_entry
*ent
, *next
;
214 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
215 if (same_suspect(ent
->suspect
, next
->suspect
) &&
216 ent
->guilty
== next
->guilty
&&
217 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
218 ent
->num_lines
+= next
->num_lines
;
219 ent
->next
= next
->next
;
221 ent
->next
->prev
= ent
;
222 origin_decref(next
->suspect
);
225 next
= ent
; /* again */
229 if (DEBUG
) /* sanity */
230 sanity_check_refcnt(sb
);
234 * Given a commit and a path in it, create a new origin structure.
235 * The callers that add blame to the scoreboard should use
236 * get_origin() to obtain shared, refcounted copy instead of calling
237 * this function directly.
239 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
242 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
245 strcpy(o
->path
, path
);
250 * Locate an existing origin or create a new one.
252 static struct origin
*get_origin(struct scoreboard
*sb
,
253 struct commit
*commit
,
256 struct blame_entry
*e
;
258 for (e
= sb
->ent
; e
; e
= e
->next
) {
259 if (e
->suspect
->commit
== commit
&&
260 !strcmp(e
->suspect
->path
, path
))
261 return origin_incref(e
->suspect
);
263 return make_origin(commit
, path
);
267 * Fill the blob_sha1 field of an origin if it hasn't, so that later
268 * call to fill_origin_blob() can use it to locate the data. blob_sha1
269 * for an origin is also used to pass the blame for the entire file to
270 * the parent to detect the case where a child's blob is identical to
271 * that of its parent's.
273 static int fill_blob_sha1(struct origin
*origin
)
277 if (!is_null_sha1(origin
->blob_sha1
))
279 if (get_tree_entry(origin
->commit
->object
.sha1
,
281 origin
->blob_sha1
, &mode
))
283 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
287 hashclr(origin
->blob_sha1
);
292 * We have an origin -- check if the same path exists in the
293 * parent and return an origin structure to represent it.
295 static struct origin
*find_origin(struct scoreboard
*sb
,
296 struct commit
*parent
,
297 struct origin
*origin
)
299 struct origin
*porigin
= NULL
;
300 struct diff_options diff_opts
;
301 const char *paths
[2];
305 * Each commit object can cache one origin in that
306 * commit. This is a freestanding copy of origin and
309 struct origin
*cached
= parent
->util
;
310 if (!strcmp(cached
->path
, origin
->path
)) {
312 * The same path between origin and its parent
313 * without renaming -- the most common case.
315 porigin
= get_origin(sb
, parent
, cached
->path
);
318 * If the origin was newly created (i.e. get_origin
319 * would call make_origin if none is found in the
320 * scoreboard), it does not know the blob_sha1,
321 * so copy it. Otherwise porigin was in the
322 * scoreboard and already knows blob_sha1.
324 if (porigin
->refcnt
== 1)
325 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
328 /* otherwise it was not very useful; free it */
333 /* See if the origin->path is different between parent
334 * and origin first. Most of the time they are the
335 * same and diff-tree is fairly efficient about this.
337 diff_setup(&diff_opts
);
338 diff_opts
.recursive
= 1;
339 diff_opts
.detect_rename
= 0;
340 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
341 paths
[0] = origin
->path
;
344 diff_tree_setup_paths(paths
, &diff_opts
);
345 if (diff_setup_done(&diff_opts
) < 0)
348 if (is_null_sha1(origin
->commit
->object
.sha1
))
349 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
351 diff_tree_sha1(parent
->tree
->object
.sha1
,
352 origin
->commit
->tree
->object
.sha1
,
354 diffcore_std(&diff_opts
);
356 /* It is either one entry that says "modified", or "created",
359 if (!diff_queued_diff
.nr
) {
360 /* The path is the same as parent */
361 porigin
= get_origin(sb
, parent
, origin
->path
);
362 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
364 else if (diff_queued_diff
.nr
!= 1)
365 die("internal error in blame::find_origin");
367 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
370 die("internal error in blame::find_origin (%c)",
373 porigin
= get_origin(sb
, parent
, origin
->path
);
374 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
378 /* Did not exist in parent, or type changed */
382 diff_flush(&diff_opts
);
385 * Create a freestanding copy that is not part of
386 * the refcounted origin found in the scoreboard, and
387 * cache it in the commit.
389 struct origin
*cached
;
391 cached
= make_origin(porigin
->commit
, porigin
->path
);
392 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
393 parent
->util
= cached
;
399 * We have an origin -- find the path that corresponds to it in its
400 * parent and return an origin structure to represent it.
402 static struct origin
*find_rename(struct scoreboard
*sb
,
403 struct commit
*parent
,
404 struct origin
*origin
)
406 struct origin
*porigin
= NULL
;
407 struct diff_options diff_opts
;
409 const char *paths
[2];
411 diff_setup(&diff_opts
);
412 diff_opts
.recursive
= 1;
413 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
414 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
415 diff_opts
.single_follow
= origin
->path
;
417 diff_tree_setup_paths(paths
, &diff_opts
);
418 if (diff_setup_done(&diff_opts
) < 0)
421 if (is_null_sha1(origin
->commit
->object
.sha1
))
422 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
424 diff_tree_sha1(parent
->tree
->object
.sha1
,
425 origin
->commit
->tree
->object
.sha1
,
427 diffcore_std(&diff_opts
);
429 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
430 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
431 if ((p
->status
== 'R' || p
->status
== 'C') &&
432 !strcmp(p
->two
->path
, origin
->path
)) {
433 porigin
= get_origin(sb
, parent
, p
->one
->path
);
434 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
438 diff_flush(&diff_opts
);
443 * Parsing of patch chunks...
446 /* line number in postimage; up to but not including this
447 * line is the same as preimage
451 /* preimage line number after this chunk */
454 /* postimage line number after this chunk */
459 struct chunk
*chunks
;
463 struct blame_diff_state
{
464 struct xdiff_emit_state xm
;
466 unsigned hunk_post_context
;
467 unsigned hunk_in_pre_context
: 1;
470 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
472 struct blame_diff_state
*state
= state_
;
474 int off1
, off2
, len1
, len2
, num
;
476 num
= state
->ret
->num
;
477 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
478 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
479 state
->ret
->chunks
[num
- 1].same
++;
481 state
->hunk_in_pre_context
= 0;
483 state
->hunk_post_context
++;
485 state
->hunk_post_context
= 0;
490 if (num
&& state
->hunk_post_context
) {
491 chunk
= &state
->ret
->chunks
[num
- 1];
492 chunk
->p_next
-= state
->hunk_post_context
;
493 chunk
->t_next
-= state
->hunk_post_context
;
495 state
->ret
->num
= ++num
;
496 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
497 sizeof(struct chunk
) * num
);
498 chunk
= &state
->ret
->chunks
[num
- 1];
499 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
504 /* Line numbers in patch output are one based. */
508 chunk
->same
= len2
? off2
: (off2
+ 1);
510 chunk
->p_next
= off1
+ (len1
? len1
: 1);
511 chunk
->t_next
= chunk
->same
+ len2
;
512 state
->hunk_in_pre_context
= 1;
513 state
->hunk_post_context
= 0;
516 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
519 struct blame_diff_state state
;
524 xpp
.flags
= xdl_opts
;
525 memset(&xecfg
, 0, sizeof(xecfg
));
526 xecfg
.ctxlen
= context
;
527 ecb
.outf
= xdiff_outf
;
529 memset(&state
, 0, sizeof(state
));
530 state
.xm
.consume
= process_u_diff
;
531 state
.ret
= xmalloc(sizeof(struct patch
));
532 state
.ret
->chunks
= NULL
;
535 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
537 if (state
.ret
->num
) {
539 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
540 chunk
->p_next
-= state
.hunk_post_context
;
541 chunk
->t_next
-= state
.hunk_post_context
;
547 * Run diff between two origins and grab the patch output, so that
548 * we can pass blame for lines origin is currently suspected for
551 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
553 mmfile_t file_p
, file_o
;
556 fill_origin_blob(parent
, &file_p
);
557 fill_origin_blob(origin
, &file_o
);
558 if (!file_p
.ptr
|| !file_o
.ptr
)
560 patch
= compare_buffer(&file_p
, &file_o
, 0);
565 static void free_patch(struct patch
*p
)
572 * Link in a new blame entry to the scoreboard. Entries that cover the
573 * same line range have been removed from the scoreboard previously.
575 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
577 struct blame_entry
*ent
, *prev
= NULL
;
579 origin_incref(e
->suspect
);
581 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
584 /* prev, if not NULL, is the last one that is below e */
587 e
->next
= prev
->next
;
599 * src typically is on-stack; we want to copy the information in it to
600 * an malloced blame_entry that is already on the linked list of the
601 * scoreboard. The origin of dst loses a refcnt while the origin of src
604 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
606 struct blame_entry
*p
, *n
;
610 origin_incref(src
->suspect
);
611 origin_decref(dst
->suspect
);
612 memcpy(dst
, src
, sizeof(*src
));
618 static const char *nth_line(struct scoreboard
*sb
, int lno
)
620 return sb
->final_buf
+ sb
->lineno
[lno
];
624 * It is known that lines between tlno to same came from parent, and e
625 * has an overlap with that range. it also is known that parent's
626 * line plno corresponds to e's line tlno.
632 * <------------------>
634 * Split e into potentially three parts; before this chunk, the chunk
635 * to be blamed for the parent, and after that portion.
637 static void split_overlap(struct blame_entry
*split
,
638 struct blame_entry
*e
,
639 int tlno
, int plno
, int same
,
640 struct origin
*parent
)
643 memset(split
, 0, sizeof(struct blame_entry
[3]));
645 if (e
->s_lno
< tlno
) {
646 /* there is a pre-chunk part not blamed on parent */
647 split
[0].suspect
= origin_incref(e
->suspect
);
648 split
[0].lno
= e
->lno
;
649 split
[0].s_lno
= e
->s_lno
;
650 split
[0].num_lines
= tlno
- e
->s_lno
;
651 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
652 split
[1].s_lno
= plno
;
655 split
[1].lno
= e
->lno
;
656 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
659 if (same
< e
->s_lno
+ e
->num_lines
) {
660 /* there is a post-chunk part not blamed on parent */
661 split
[2].suspect
= origin_incref(e
->suspect
);
662 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
663 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
664 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
665 chunk_end_lno
= split
[2].lno
;
668 chunk_end_lno
= e
->lno
+ e
->num_lines
;
669 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
672 * if it turns out there is nothing to blame the parent for,
673 * forget about the splitting. !split[1].suspect signals this.
675 if (split
[1].num_lines
< 1)
677 split
[1].suspect
= origin_incref(parent
);
681 * split_overlap() divided an existing blame e into up to three parts
682 * in split. Adjust the linked list of blames in the scoreboard to
685 static void split_blame(struct scoreboard
*sb
,
686 struct blame_entry
*split
,
687 struct blame_entry
*e
)
689 struct blame_entry
*new_entry
;
691 if (split
[0].suspect
&& split
[2].suspect
) {
692 /* The first part (reuse storage for the existing entry e) */
693 dup_entry(e
, &split
[0]);
695 /* The last part -- me */
696 new_entry
= xmalloc(sizeof(*new_entry
));
697 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
698 add_blame_entry(sb
, new_entry
);
700 /* ... and the middle part -- parent */
701 new_entry
= xmalloc(sizeof(*new_entry
));
702 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
703 add_blame_entry(sb
, new_entry
);
705 else if (!split
[0].suspect
&& !split
[2].suspect
)
707 * The parent covers the entire area; reuse storage for
708 * e and replace it with the parent.
710 dup_entry(e
, &split
[1]);
711 else if (split
[0].suspect
) {
712 /* me and then parent */
713 dup_entry(e
, &split
[0]);
715 new_entry
= xmalloc(sizeof(*new_entry
));
716 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
717 add_blame_entry(sb
, new_entry
);
720 /* parent and then me */
721 dup_entry(e
, &split
[1]);
723 new_entry
= xmalloc(sizeof(*new_entry
));
724 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
725 add_blame_entry(sb
, new_entry
);
728 if (DEBUG
) { /* sanity */
729 struct blame_entry
*ent
;
730 int lno
= sb
->ent
->lno
, corrupt
= 0;
732 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
737 lno
+= ent
->num_lines
;
741 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
742 printf("L %8d l %8d n %8d\n",
743 lno
, ent
->lno
, ent
->num_lines
);
744 lno
= ent
->lno
+ ent
->num_lines
;
752 * After splitting the blame, the origins used by the
753 * on-stack blame_entry should lose one refcnt each.
755 static void decref_split(struct blame_entry
*split
)
759 for (i
= 0; i
< 3; i
++)
760 origin_decref(split
[i
].suspect
);
764 * Helper for blame_chunk(). blame_entry e is known to overlap with
765 * the patch hunk; split it and pass blame to the parent.
767 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
768 int tlno
, int plno
, int same
,
769 struct origin
*parent
)
771 struct blame_entry split
[3];
773 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
774 if (split
[1].suspect
)
775 split_blame(sb
, split
, e
);
780 * Find the line number of the last line the target is suspected for.
782 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
784 struct blame_entry
*e
;
785 int last_in_target
= -1;
787 for (e
= sb
->ent
; e
; e
= e
->next
) {
788 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
790 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
791 last_in_target
= e
->s_lno
+ e
->num_lines
;
793 return last_in_target
;
797 * Process one hunk from the patch between the current suspect for
798 * blame_entry e and its parent. Find and split the overlap, and
799 * pass blame to the overlapping part to the parent.
801 static void blame_chunk(struct scoreboard
*sb
,
802 int tlno
, int plno
, int same
,
803 struct origin
*target
, struct origin
*parent
)
805 struct blame_entry
*e
;
807 for (e
= sb
->ent
; e
; e
= e
->next
) {
808 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
810 if (same
<= e
->s_lno
)
812 if (tlno
< e
->s_lno
+ e
->num_lines
)
813 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
818 * We are looking at the origin 'target' and aiming to pass blame
819 * for the lines it is suspected to its parent. Run diff to find
820 * which lines came from parent and pass blame for them.
822 static int pass_blame_to_parent(struct scoreboard
*sb
,
823 struct origin
*target
,
824 struct origin
*parent
)
826 int i
, last_in_target
, plno
, tlno
;
829 last_in_target
= find_last_in_target(sb
, target
);
830 if (last_in_target
< 0)
831 return 1; /* nothing remains for this target */
833 patch
= get_patch(parent
, target
);
835 for (i
= 0; i
< patch
->num
; i
++) {
836 struct chunk
*chunk
= &patch
->chunks
[i
];
838 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
839 plno
= chunk
->p_next
;
840 tlno
= chunk
->t_next
;
842 /* The rest (i.e. anything after tlno) are the same as the parent */
843 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
850 * The lines in blame_entry after splitting blames many times can become
851 * very small and trivial, and at some point it becomes pointless to
852 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
853 * ordinary C program, and it is not worth to say it was copied from
854 * totally unrelated file in the parent.
856 * Compute how trivial the lines in the blame_entry are.
858 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
867 cp
= nth_line(sb
, e
->lno
);
868 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
870 unsigned ch
= *((unsigned char *)cp
);
880 * best_so_far[] and this[] are both a split of an existing blame_entry
881 * that passes blame to the parent. Maintain best_so_far the best split
882 * so far, by comparing this and best_so_far and copying this into
883 * bst_so_far as needed.
885 static void copy_split_if_better(struct scoreboard
*sb
,
886 struct blame_entry
*best_so_far
,
887 struct blame_entry
*this)
891 if (!this[1].suspect
)
893 if (best_so_far
[1].suspect
) {
894 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
898 for (i
= 0; i
< 3; i
++)
899 origin_incref(this[i
].suspect
);
900 decref_split(best_so_far
);
901 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
905 * We are looking at a part of the final image represented by
906 * ent (tlno and same are offset by ent->s_lno).
907 * tlno is where we are looking at in the final image.
908 * up to (but not including) same match preimage.
909 * plno is where we are looking at in the preimage.
911 * <-------------- final image ---------------------->
914 * <---------preimage----->
917 * All line numbers are 0-based.
919 static void handle_split(struct scoreboard
*sb
,
920 struct blame_entry
*ent
,
921 int tlno
, int plno
, int same
,
922 struct origin
*parent
,
923 struct blame_entry
*split
)
925 if (ent
->num_lines
<= tlno
)
928 struct blame_entry
this[3];
931 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
932 copy_split_if_better(sb
, split
, this);
938 * Find the lines from parent that are the same as ent so that
939 * we can pass blames to it. file_p has the blob contents for
942 static void find_copy_in_blob(struct scoreboard
*sb
,
943 struct blame_entry
*ent
,
944 struct origin
*parent
,
945 struct blame_entry
*split
,
955 * Prepare mmfile that contains only the lines in ent.
957 cp
= nth_line(sb
, ent
->lno
);
958 file_o
.ptr
= (char*) cp
;
959 cnt
= ent
->num_lines
;
961 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
965 file_o
.size
= cp
- file_o
.ptr
;
967 patch
= compare_buffer(file_p
, &file_o
, 1);
970 * file_o is a part of final image we are annotating.
971 * file_p partially may match that image.
973 memset(split
, 0, sizeof(struct blame_entry
[3]));
975 for (i
= 0; i
< patch
->num
; i
++) {
976 struct chunk
*chunk
= &patch
->chunks
[i
];
978 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
979 plno
= chunk
->p_next
;
980 tlno
= chunk
->t_next
;
982 /* remainder, if any, all match the preimage */
983 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
988 * See if lines currently target is suspected for can be attributed to
991 static int find_move_in_parent(struct scoreboard
*sb
,
992 struct origin
*target
,
993 struct origin
*parent
)
995 int last_in_target
, made_progress
;
996 struct blame_entry
*e
, split
[3];
999 last_in_target
= find_last_in_target(sb
, target
);
1000 if (last_in_target
< 0)
1001 return 1; /* nothing remains for this target */
1003 fill_origin_blob(parent
, &file_p
);
1008 while (made_progress
) {
1010 for (e
= sb
->ent
; e
; e
= e
->next
) {
1011 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1013 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1014 if (split
[1].suspect
&&
1015 blame_move_score
< ent_score(sb
, &split
[1])) {
1016 split_blame(sb
, split
, e
);
1019 decref_split(split
);
1026 struct blame_entry
*ent
;
1027 struct blame_entry split
[3];
1031 * Count the number of entries the target is suspected for,
1032 * and prepare a list of entry and the best split.
1034 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1035 struct origin
*target
,
1038 struct blame_entry
*e
;
1040 struct blame_list
*blame_list
= NULL
;
1042 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1043 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1046 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1047 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1048 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1049 blame_list
[i
++].ent
= e
;
1051 *num_ents_p
= num_ents
;
1056 * For lines target is suspected for, see if we can find code movement
1057 * across file boundary from the parent commit. porigin is the path
1058 * in the parent we already tried.
1060 static int find_copy_in_parent(struct scoreboard
*sb
,
1061 struct origin
*target
,
1062 struct commit
*parent
,
1063 struct origin
*porigin
,
1066 struct diff_options diff_opts
;
1067 const char *paths
[1];
1070 struct blame_list
*blame_list
;
1073 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1075 return 1; /* nothing remains for this target */
1077 diff_setup(&diff_opts
);
1078 diff_opts
.recursive
= 1;
1079 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1082 diff_tree_setup_paths(paths
, &diff_opts
);
1083 if (diff_setup_done(&diff_opts
) < 0)
1086 /* Try "find copies harder" on new path if requested;
1087 * we do not want to use diffcore_rename() actually to
1088 * match things up; find_copies_harder is set only to
1089 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1090 * and this code needs to be after diff_setup_done(), which
1091 * usually makes find-copies-harder imply copy detection.
1093 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1094 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1095 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1096 diff_opts
.find_copies_harder
= 1;
1098 if (is_null_sha1(target
->commit
->object
.sha1
))
1099 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1101 diff_tree_sha1(parent
->tree
->object
.sha1
,
1102 target
->commit
->tree
->object
.sha1
,
1105 if (!diff_opts
.find_copies_harder
)
1106 diffcore_std(&diff_opts
);
1110 int made_progress
= 0;
1112 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1113 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1114 struct origin
*norigin
;
1116 struct blame_entry
this[3];
1118 if (!DIFF_FILE_VALID(p
->one
))
1119 continue; /* does not exist in parent */
1120 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1121 /* find_move already dealt with this path */
1124 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1125 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1126 fill_origin_blob(norigin
, &file_p
);
1130 for (j
= 0; j
< num_ents
; j
++) {
1131 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1132 norigin
, this, &file_p
);
1133 copy_split_if_better(sb
, blame_list
[j
].split
,
1137 origin_decref(norigin
);
1140 for (j
= 0; j
< num_ents
; j
++) {
1141 struct blame_entry
*split
= blame_list
[j
].split
;
1142 if (split
[1].suspect
&&
1143 blame_copy_score
< ent_score(sb
, &split
[1])) {
1144 split_blame(sb
, split
, blame_list
[j
].ent
);
1147 decref_split(split
);
1153 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1159 diff_flush(&diff_opts
);
1165 * The blobs of origin and porigin exactly match, so everything
1166 * origin is suspected for can be blamed on the parent.
1168 static void pass_whole_blame(struct scoreboard
*sb
,
1169 struct origin
*origin
, struct origin
*porigin
)
1171 struct blame_entry
*e
;
1173 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1174 /* Steal its file */
1175 porigin
->file
= origin
->file
;
1176 origin
->file
.ptr
= NULL
;
1178 for (e
= sb
->ent
; e
; e
= e
->next
) {
1179 if (!same_suspect(e
->suspect
, origin
))
1181 origin_incref(porigin
);
1182 origin_decref(e
->suspect
);
1183 e
->suspect
= porigin
;
1187 #define MAXPARENT 16
1189 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1192 struct commit
*commit
= origin
->commit
;
1193 struct commit_list
*parent
;
1194 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1196 memset(parent_origin
, 0, sizeof(parent_origin
));
1198 /* The first pass looks for unrenamed path to optimize for
1199 * common cases, then we look for renames in the second pass.
1201 for (pass
= 0; pass
< 2; pass
++) {
1202 struct origin
*(*find
)(struct scoreboard
*,
1203 struct commit
*, struct origin
*);
1204 find
= pass
? find_rename
: find_origin
;
1206 for (i
= 0, parent
= commit
->parents
;
1207 i
< MAXPARENT
&& parent
;
1208 parent
= parent
->next
, i
++) {
1209 struct commit
*p
= parent
->item
;
1212 if (parent_origin
[i
])
1214 if (parse_commit(p
))
1216 porigin
= find(sb
, p
, origin
);
1219 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1220 pass_whole_blame(sb
, origin
, porigin
);
1221 origin_decref(porigin
);
1224 for (j
= same
= 0; j
< i
; j
++)
1225 if (parent_origin
[j
] &&
1226 !hashcmp(parent_origin
[j
]->blob_sha1
,
1227 porigin
->blob_sha1
)) {
1232 parent_origin
[i
] = porigin
;
1234 origin_decref(porigin
);
1239 for (i
= 0, parent
= commit
->parents
;
1240 i
< MAXPARENT
&& parent
;
1241 parent
= parent
->next
, i
++) {
1242 struct origin
*porigin
= parent_origin
[i
];
1245 if (pass_blame_to_parent(sb
, origin
, porigin
))
1250 * Optionally find moves in parents' files.
1252 if (opt
& PICKAXE_BLAME_MOVE
)
1253 for (i
= 0, parent
= commit
->parents
;
1254 i
< MAXPARENT
&& parent
;
1255 parent
= parent
->next
, i
++) {
1256 struct origin
*porigin
= parent_origin
[i
];
1259 if (find_move_in_parent(sb
, origin
, porigin
))
1264 * Optionally find copies from parents' files.
1266 if (opt
& PICKAXE_BLAME_COPY
)
1267 for (i
= 0, parent
= commit
->parents
;
1268 i
< MAXPARENT
&& parent
;
1269 parent
= parent
->next
, i
++) {
1270 struct origin
*porigin
= parent_origin
[i
];
1271 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1277 for (i
= 0; i
< MAXPARENT
; i
++)
1278 origin_decref(parent_origin
[i
]);
1282 * Information on commits, used for output.
1287 const char *author_mail
;
1288 unsigned long author_time
;
1289 const char *author_tz
;
1291 /* filled only when asked for details */
1292 const char *committer
;
1293 const char *committer_mail
;
1294 unsigned long committer_time
;
1295 const char *committer_tz
;
1297 const char *summary
;
1301 * Parse author/committer line in the commit object buffer
1303 static void get_ac_line(const char *inbuf
, const char *what
,
1304 int bufsz
, char *person
, const char **mail
,
1305 unsigned long *time
, const char **tz
)
1307 int len
, tzlen
, maillen
;
1308 char *tmp
, *endp
, *timepos
;
1310 tmp
= strstr(inbuf
, what
);
1313 tmp
+= strlen(what
);
1314 endp
= strchr(tmp
, '\n');
1322 *mail
= *tz
= "(unknown)";
1326 memcpy(person
, tmp
, len
);
1334 tzlen
= (person
+len
)-(tmp
+1);
1339 *time
= strtoul(tmp
, NULL
, 10);
1347 maillen
= timepos
- tmp
;
1353 * mailmap expansion may make the name longer.
1354 * make room by pushing stuff down.
1356 tmp
= person
+ bufsz
- (tzlen
+ 1);
1357 memmove(tmp
, *tz
, tzlen
);
1361 tmp
= tmp
- (maillen
+ 1);
1362 memmove(tmp
, *mail
, maillen
);
1367 * Now, convert e-mail using mailmap
1369 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1372 static void get_commit_info(struct commit
*commit
,
1373 struct commit_info
*ret
,
1378 static char author_buf
[1024];
1379 static char committer_buf
[1024];
1380 static char summary_buf
[1024];
1383 * We've operated without save_commit_buffer, so
1384 * we now need to populate them for output.
1386 if (!commit
->buffer
) {
1387 enum object_type type
;
1390 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1391 if (!commit
->buffer
)
1392 die("Cannot read commit %s",
1393 sha1_to_hex(commit
->object
.sha1
));
1395 ret
->author
= author_buf
;
1396 get_ac_line(commit
->buffer
, "\nauthor ",
1397 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1398 &ret
->author_time
, &ret
->author_tz
);
1403 ret
->committer
= committer_buf
;
1404 get_ac_line(commit
->buffer
, "\ncommitter ",
1405 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1406 &ret
->committer_time
, &ret
->committer_tz
);
1408 ret
->summary
= summary_buf
;
1409 tmp
= strstr(commit
->buffer
, "\n\n");
1412 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1416 endp
= strchr(tmp
, '\n');
1418 endp
= tmp
+ strlen(tmp
);
1420 if (len
>= sizeof(summary_buf
) || len
== 0)
1422 memcpy(summary_buf
, tmp
, len
);
1423 summary_buf
[len
] = 0;
1427 * To allow LF and other nonportable characters in pathnames,
1428 * they are c-style quoted as needed.
1430 static void write_filename_info(const char *path
)
1432 printf("filename ");
1433 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1438 * The blame_entry is found to be guilty for the range. Mark it
1439 * as such, and show it in incremental output.
1441 static void found_guilty_entry(struct blame_entry
*ent
)
1447 struct origin
*suspect
= ent
->suspect
;
1449 printf("%s %d %d %d\n",
1450 sha1_to_hex(suspect
->commit
->object
.sha1
),
1451 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1452 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1453 struct commit_info ci
;
1454 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1455 get_commit_info(suspect
->commit
, &ci
, 1);
1456 printf("author %s\n", ci
.author
);
1457 printf("author-mail %s\n", ci
.author_mail
);
1458 printf("author-time %lu\n", ci
.author_time
);
1459 printf("author-tz %s\n", ci
.author_tz
);
1460 printf("committer %s\n", ci
.committer
);
1461 printf("committer-mail %s\n", ci
.committer_mail
);
1462 printf("committer-time %lu\n", ci
.committer_time
);
1463 printf("committer-tz %s\n", ci
.committer_tz
);
1464 printf("summary %s\n", ci
.summary
);
1465 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1466 printf("boundary\n");
1468 write_filename_info(suspect
->path
);
1469 maybe_flush_or_die(stdout
, "stdout");
1474 * The main loop -- while the scoreboard has lines whose true origin
1475 * is still unknown, pick one blame_entry, and allow its current
1476 * suspect to pass blames to its parents.
1478 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1481 struct blame_entry
*ent
;
1482 struct commit
*commit
;
1483 struct origin
*suspect
= NULL
;
1485 /* find one suspect to break down */
1486 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1488 suspect
= ent
->suspect
;
1490 return; /* all done */
1493 * We will use this suspect later in the loop,
1494 * so hold onto it in the meantime.
1496 origin_incref(suspect
);
1497 commit
= suspect
->commit
;
1498 if (!commit
->object
.parsed
)
1499 parse_commit(commit
);
1500 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1501 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1502 pass_blame(sb
, suspect
, opt
);
1504 commit
->object
.flags
|= UNINTERESTING
;
1505 if (commit
->object
.parsed
)
1506 mark_parents_uninteresting(commit
);
1508 /* treat root commit as boundary */
1509 if (!commit
->parents
&& !show_root
)
1510 commit
->object
.flags
|= UNINTERESTING
;
1512 /* Take responsibility for the remaining entries */
1513 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1514 if (same_suspect(ent
->suspect
, suspect
))
1515 found_guilty_entry(ent
);
1516 origin_decref(suspect
);
1518 if (DEBUG
) /* sanity */
1519 sanity_check_refcnt(sb
);
1523 static const char *format_time(unsigned long time
, const char *tz_str
,
1526 static char time_buf
[128];
1531 if (show_raw_time
) {
1532 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1537 minutes
= tz
< 0 ? -tz
: tz
;
1538 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1539 minutes
= tz
< 0 ? -minutes
: minutes
;
1540 t
= time
+ minutes
* 60;
1543 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1544 strcat(time_buf
, tz_str
);
1548 #define OUTPUT_ANNOTATE_COMPAT 001
1549 #define OUTPUT_LONG_OBJECT_NAME 002
1550 #define OUTPUT_RAW_TIMESTAMP 004
1551 #define OUTPUT_PORCELAIN 010
1552 #define OUTPUT_SHOW_NAME 020
1553 #define OUTPUT_SHOW_NUMBER 040
1554 #define OUTPUT_SHOW_SCORE 0100
1555 #define OUTPUT_NO_AUTHOR 0200
1557 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1561 struct origin
*suspect
= ent
->suspect
;
1564 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1565 printf("%s%c%d %d %d\n",
1567 ent
->guilty
? ' ' : '*', // purely for debugging
1571 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1572 struct commit_info ci
;
1573 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1574 get_commit_info(suspect
->commit
, &ci
, 1);
1575 printf("author %s\n", ci
.author
);
1576 printf("author-mail %s\n", ci
.author_mail
);
1577 printf("author-time %lu\n", ci
.author_time
);
1578 printf("author-tz %s\n", ci
.author_tz
);
1579 printf("committer %s\n", ci
.committer
);
1580 printf("committer-mail %s\n", ci
.committer_mail
);
1581 printf("committer-time %lu\n", ci
.committer_time
);
1582 printf("committer-tz %s\n", ci
.committer_tz
);
1583 write_filename_info(suspect
->path
);
1584 printf("summary %s\n", ci
.summary
);
1585 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1586 printf("boundary\n");
1588 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1589 write_filename_info(suspect
->path
);
1591 cp
= nth_line(sb
, ent
->lno
);
1592 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1595 printf("%s %d %d\n", hex
,
1596 ent
->s_lno
+ 1 + cnt
,
1597 ent
->lno
+ 1 + cnt
);
1602 } while (ch
!= '\n' &&
1603 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1607 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1611 struct origin
*suspect
= ent
->suspect
;
1612 struct commit_info ci
;
1614 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1616 get_commit_info(suspect
->commit
, &ci
, 1);
1617 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1619 cp
= nth_line(sb
, ent
->lno
);
1620 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1622 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1624 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1626 memset(hex
, ' ', length
);
1627 else if (!cmd_is_annotate
) {
1633 printf("%.*s", length
, hex
);
1634 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1635 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1636 format_time(ci
.author_time
, ci
.author_tz
,
1638 ent
->lno
+ 1 + cnt
);
1640 if (opt
& OUTPUT_SHOW_SCORE
)
1642 max_score_digits
, ent
->score
,
1643 ent
->suspect
->refcnt
);
1644 if (opt
& OUTPUT_SHOW_NAME
)
1645 printf(" %-*.*s", longest_file
, longest_file
,
1647 if (opt
& OUTPUT_SHOW_NUMBER
)
1648 printf(" %*d", max_orig_digits
,
1649 ent
->s_lno
+ 1 + cnt
);
1651 if (!(opt
& OUTPUT_NO_AUTHOR
))
1652 printf(" (%-*.*s %10s",
1653 longest_author
, longest_author
,
1655 format_time(ci
.author_time
,
1659 max_digits
, ent
->lno
+ 1 + cnt
);
1664 } while (ch
!= '\n' &&
1665 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1669 static void output(struct scoreboard
*sb
, int option
)
1671 struct blame_entry
*ent
;
1673 if (option
& OUTPUT_PORCELAIN
) {
1674 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1675 struct blame_entry
*oth
;
1676 struct origin
*suspect
= ent
->suspect
;
1677 struct commit
*commit
= suspect
->commit
;
1678 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1680 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1681 if ((oth
->suspect
->commit
!= commit
) ||
1682 !strcmp(oth
->suspect
->path
, suspect
->path
))
1684 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1690 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1691 if (option
& OUTPUT_PORCELAIN
)
1692 emit_porcelain(sb
, ent
);
1694 emit_other(sb
, ent
, option
);
1700 * To allow quick access to the contents of nth line in the
1701 * final image, prepare an index in the scoreboard.
1703 static int prepare_lines(struct scoreboard
*sb
)
1705 const char *buf
= sb
->final_buf
;
1706 unsigned long len
= sb
->final_buf_size
;
1707 int num
= 0, incomplete
= 0, bol
= 1;
1709 if (len
&& buf
[len
-1] != '\n')
1710 incomplete
++; /* incomplete line at the end */
1713 sb
->lineno
= xrealloc(sb
->lineno
,
1714 sizeof(int* ) * (num
+ 1));
1715 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1718 if (*buf
++ == '\n') {
1723 sb
->lineno
= xrealloc(sb
->lineno
,
1724 sizeof(int* ) * (num
+ incomplete
+ 1));
1725 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1726 sb
->num_lines
= num
+ incomplete
;
1727 return sb
->num_lines
;
1731 * Add phony grafts for use with -S; this is primarily to
1732 * support git-cvsserver that wants to give a linear history
1735 static int read_ancestry(const char *graft_file
)
1737 FILE *fp
= fopen(graft_file
, "r");
1741 while (fgets(buf
, sizeof(buf
), fp
)) {
1742 /* The format is just "Commit Parent1 Parent2 ...\n" */
1743 int len
= strlen(buf
);
1744 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1746 register_commit_graft(graft
, 0);
1753 * How many columns do we need to show line numbers in decimal?
1755 static int lineno_width(int lines
)
1759 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1765 * How many columns do we need to show line numbers, authors,
1768 static void find_alignment(struct scoreboard
*sb
, int *option
)
1770 int longest_src_lines
= 0;
1771 int longest_dst_lines
= 0;
1772 unsigned largest_score
= 0;
1773 struct blame_entry
*e
;
1775 for (e
= sb
->ent
; e
; e
= e
->next
) {
1776 struct origin
*suspect
= e
->suspect
;
1777 struct commit_info ci
;
1780 if (strcmp(suspect
->path
, sb
->path
))
1781 *option
|= OUTPUT_SHOW_NAME
;
1782 num
= strlen(suspect
->path
);
1783 if (longest_file
< num
)
1785 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1786 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1787 get_commit_info(suspect
->commit
, &ci
, 1);
1788 num
= strlen(ci
.author
);
1789 if (longest_author
< num
)
1790 longest_author
= num
;
1792 num
= e
->s_lno
+ e
->num_lines
;
1793 if (longest_src_lines
< num
)
1794 longest_src_lines
= num
;
1795 num
= e
->lno
+ e
->num_lines
;
1796 if (longest_dst_lines
< num
)
1797 longest_dst_lines
= num
;
1798 if (largest_score
< ent_score(sb
, e
))
1799 largest_score
= ent_score(sb
, e
);
1801 max_orig_digits
= lineno_width(longest_src_lines
);
1802 max_digits
= lineno_width(longest_dst_lines
);
1803 max_score_digits
= lineno_width(largest_score
);
1807 * For debugging -- origin is refcounted, and this asserts that
1808 * we do not underflow.
1810 static void sanity_check_refcnt(struct scoreboard
*sb
)
1813 struct blame_entry
*ent
;
1815 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1816 /* Nobody should have zero or negative refcnt */
1817 if (ent
->suspect
->refcnt
<= 0) {
1818 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1820 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1821 ent
->suspect
->refcnt
);
1825 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1826 /* Mark the ones that haven't been checked */
1827 if (0 < ent
->suspect
->refcnt
)
1828 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1830 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1832 * ... then pick each and see if they have the the
1836 struct blame_entry
*e
;
1837 struct origin
*suspect
= ent
->suspect
;
1839 if (0 < suspect
->refcnt
)
1841 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1842 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1843 if (e
->suspect
!= suspect
)
1847 if (suspect
->refcnt
!= found
) {
1848 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1850 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1851 ent
->suspect
->refcnt
, found
);
1857 find_alignment(sb
, &opt
);
1859 die("Baa %d!", baa
);
1864 * Used for the command line parsing; check if the path exists
1865 * in the working tree.
1867 static int has_path_in_work_tree(const char *path
)
1870 return !lstat(path
, &st
);
1873 static unsigned parse_score(const char *arg
)
1876 unsigned long score
= strtoul(arg
, &end
, 10);
1882 static const char *add_prefix(const char *prefix
, const char *path
)
1884 if (!prefix
|| !prefix
[0])
1886 return prefix_path(prefix
, strlen(prefix
), path
);
1890 * Parsing of (comma separated) one item in the -L option
1892 static const char *parse_loc(const char *spec
,
1893 struct scoreboard
*sb
, long lno
,
1894 long begin
, long *ret
)
1901 regmatch_t match
[1];
1903 /* Allow "-L <something>,+20" to mean starting at <something>
1904 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1907 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1908 num
= strtol(spec
+ 1, &term
, 10);
1909 if (term
!= spec
+ 1) {
1913 *ret
= begin
+ num
- 2;
1922 num
= strtol(spec
, &term
, 10);
1930 /* it could be a regexp of form /.../ */
1931 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1938 /* try [spec+1 .. term-1] as regexp */
1940 begin
--; /* input is in human terms */
1941 line
= nth_line(sb
, begin
);
1943 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1944 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1945 const char *cp
= line
+ match
[0].rm_so
;
1948 while (begin
++ < lno
) {
1949 nline
= nth_line(sb
, begin
);
1950 if (line
<= cp
&& cp
< nline
)
1961 regerror(reg_error
, ®exp
, errbuf
, 1024);
1962 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1967 * Parsing of -L option
1969 static void prepare_blame_range(struct scoreboard
*sb
,
1970 const char *bottomtop
,
1972 long *bottom
, long *top
)
1976 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1978 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1986 static int git_blame_config(const char *var
, const char *value
)
1988 if (!strcmp(var
, "blame.showroot")) {
1989 show_root
= git_config_bool(var
, value
);
1992 if (!strcmp(var
, "blame.blankboundary")) {
1993 blank_boundary
= git_config_bool(var
, value
);
1996 return git_default_config(var
, value
);
1999 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
2001 struct commit
*commit
;
2002 struct origin
*origin
;
2003 unsigned char head_sha1
[20];
2008 unsigned long fin_size
;
2010 struct cache_entry
*ce
;
2013 if (get_sha1("HEAD", head_sha1
))
2014 die("No such ref: HEAD");
2017 commit
= xcalloc(1, sizeof(*commit
));
2018 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2019 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2020 commit
->object
.parsed
= 1;
2022 commit
->object
.type
= OBJ_COMMIT
;
2024 origin
= make_origin(commit
, path
);
2026 if (!contents_from
|| strcmp("-", contents_from
)) {
2028 const char *read_from
;
2030 if (contents_from
) {
2031 if (stat(contents_from
, &st
) < 0)
2032 die("Cannot stat %s", contents_from
);
2033 read_from
= contents_from
;
2036 if (lstat(path
, &st
) < 0)
2037 die("Cannot lstat %s", path
);
2040 fin_size
= xsize_t(st
.st_size
);
2041 buf
= xmalloc(fin_size
+1);
2042 mode
= canon_mode(st
.st_mode
);
2043 switch (st
.st_mode
& S_IFMT
) {
2045 fd
= open(read_from
, O_RDONLY
);
2047 die("cannot open %s", read_from
);
2048 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
2049 die("cannot read %s", read_from
);
2052 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
2053 die("cannot readlink %s", read_from
);
2056 die("unsupported file type %s", read_from
);
2060 /* Reading from stdin */
2061 contents_from
= "standard input";
2067 buf
= xrealloc(buf
, fin_size
+ cnt
);
2068 cnt
= xread(0, buf
+ fin_size
, cnt
);
2070 die("read error %s from stdin",
2076 buf
= xrealloc(buf
, fin_size
+ 1);
2079 origin
->file
.ptr
= buf
;
2080 origin
->file
.size
= fin_size
;
2081 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2082 commit
->util
= origin
;
2085 * Read the current index, replace the path entry with
2086 * origin->blob_sha1 without mucking with its mode or type
2087 * bits; we are not going to write this index out -- we just
2088 * want to run "diff-index --cached".
2095 int pos
= cache_name_pos(path
, len
);
2097 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2099 /* Let's not bother reading from HEAD tree */
2100 mode
= S_IFREG
| 0644;
2102 size
= cache_entry_size(len
);
2103 ce
= xcalloc(1, size
);
2104 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2105 memcpy(ce
->name
, path
, len
);
2106 ce
->ce_flags
= create_ce_flags(len
, 0);
2107 ce
->ce_mode
= create_ce_mode(mode
);
2108 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2111 * We are not going to write this out, so this does not matter
2112 * right now, but someday we might optimize diff-index --cached
2113 * with cache-tree information.
2115 cache_tree_invalidate_path(active_cache_tree
, path
);
2117 commit
->buffer
= xmalloc(400);
2118 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2119 snprintf(commit
->buffer
, 400,
2120 "tree 0000000000000000000000000000000000000000\n"
2124 "Version of %s from %s\n",
2125 sha1_to_hex(head_sha1
),
2126 ident
, ident
, path
, contents_from
? contents_from
: path
);
2130 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2132 struct rev_info revs
;
2134 struct scoreboard sb
;
2136 struct blame_entry
*ent
;
2137 int i
, seen_dashdash
, unk
, opt
;
2138 long bottom
, top
, lno
;
2139 int output_option
= 0;
2141 const char *revs_file
= NULL
;
2142 const char *final_commit_name
= NULL
;
2143 enum object_type type
;
2144 const char *bottomtop
= NULL
;
2145 const char *contents_from
= NULL
;
2147 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2149 git_config(git_blame_config
);
2150 save_commit_buffer
= 0;
2154 for (unk
= i
= 1; i
< argc
; i
++) {
2155 const char *arg
= argv
[i
];
2158 else if (!strcmp("-b", arg
))
2160 else if (!strcmp("--root", arg
))
2162 else if (!strcmp(arg
, "--show-stats"))
2164 else if (!strcmp("-c", arg
))
2165 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2166 else if (!strcmp("-t", arg
))
2167 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2168 else if (!strcmp("-l", arg
))
2169 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2170 else if (!strcmp("-s", arg
))
2171 output_option
|= OUTPUT_NO_AUTHOR
;
2172 else if (!strcmp("-w", arg
))
2173 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2174 else if (!strcmp("-S", arg
) && ++i
< argc
)
2175 revs_file
= argv
[i
];
2176 else if (!prefixcmp(arg
, "-M")) {
2177 opt
|= PICKAXE_BLAME_MOVE
;
2178 blame_move_score
= parse_score(arg
+2);
2180 else if (!prefixcmp(arg
, "-C")) {
2182 * -C enables copy from removed files;
2183 * -C -C enables copy from existing files, but only
2184 * when blaming a new file;
2185 * -C -C -C enables copy from existing files for
2188 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2189 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2190 if (opt
& PICKAXE_BLAME_COPY
)
2191 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2192 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2193 blame_copy_score
= parse_score(arg
+2);
2195 else if (!prefixcmp(arg
, "-L")) {
2204 die("More than one '-L n,m' option given");
2207 else if (!strcmp("--contents", arg
)) {
2210 contents_from
= argv
[i
];
2212 else if (!strcmp("--incremental", arg
))
2214 else if (!strcmp("--score-debug", arg
))
2215 output_option
|= OUTPUT_SHOW_SCORE
;
2216 else if (!strcmp("-f", arg
) ||
2217 !strcmp("--show-name", arg
))
2218 output_option
|= OUTPUT_SHOW_NAME
;
2219 else if (!strcmp("-n", arg
) ||
2220 !strcmp("--show-number", arg
))
2221 output_option
|= OUTPUT_SHOW_NUMBER
;
2222 else if (!strcmp("-p", arg
) ||
2223 !strcmp("--porcelain", arg
))
2224 output_option
|= OUTPUT_PORCELAIN
;
2225 else if (!strcmp("--", arg
)) {
2234 if (!blame_move_score
)
2235 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2236 if (!blame_copy_score
)
2237 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2240 * We have collected options unknown to us in argv[1..unk]
2241 * which are to be passed to revision machinery if we are
2242 * going to do the "bottom" processing.
2244 * The remaining are:
2246 * (1) if seen_dashdash, its either
2247 * "-options -- <path>" or
2248 * "-options -- <path> <rev>".
2249 * but the latter is allowed only if there is no
2250 * options that we passed to revision machinery.
2252 * (2) otherwise, we may have "--" somewhere later and
2253 * might be looking at the first one of multiple 'rev'
2254 * parameters (e.g. " master ^next ^maint -- path").
2255 * See if there is a dashdash first, and give the
2256 * arguments before that to revision machinery.
2257 * After that there must be one 'path'.
2259 * (3) otherwise, its one of the three:
2260 * "-options <path> <rev>"
2261 * "-options <rev> <path>"
2263 * but again the first one is allowed only if
2264 * there is no options that we passed to revision
2268 if (seen_dashdash
) {
2272 path
= add_prefix(prefix
, argv
[i
]);
2273 if (i
+ 1 == argc
- 1) {
2276 argv
[unk
++] = argv
[i
+ 1];
2278 else if (i
+ 1 != argc
)
2279 /* garbage at end */
2284 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2285 if (!strcmp(argv
[j
], "--"))
2287 if (seen_dashdash
) {
2289 if (seen_dashdash
+ 1 != argc
- 1)
2291 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2292 for (j
= i
; j
< seen_dashdash
; j
++)
2293 argv
[unk
++] = argv
[j
];
2299 path
= add_prefix(prefix
, argv
[i
]);
2300 if (i
+ 1 == argc
- 1) {
2301 final_commit_name
= argv
[i
+ 1];
2303 /* if (unk == 1) we could be getting
2306 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2307 path
= add_prefix(prefix
, argv
[i
+ 1]);
2308 final_commit_name
= argv
[i
];
2311 else if (i
!= argc
- 1)
2312 usage(blame_usage
); /* garbage at end */
2314 if (!has_path_in_work_tree(path
))
2315 die("cannot stat path %s: %s",
2316 path
, strerror(errno
));
2320 if (final_commit_name
)
2321 argv
[unk
++] = final_commit_name
;
2324 * Now we got rev and path. We do not want the path pruning
2325 * but we may want "bottom" processing.
2327 argv
[unk
++] = "--"; /* terminate the rev name */
2330 init_revisions(&revs
, NULL
);
2331 setup_revisions(unk
, argv
, &revs
, NULL
);
2332 memset(&sb
, 0, sizeof(sb
));
2335 * There must be one and only one positive commit in the
2336 * revs->pending array.
2338 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2339 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2340 if (obj
->flags
& UNINTERESTING
)
2342 while (obj
->type
== OBJ_TAG
)
2343 obj
= deref_tag(obj
, NULL
, 0);
2344 if (obj
->type
!= OBJ_COMMIT
)
2345 die("Non commit %s?",
2346 revs
.pending
.objects
[i
].name
);
2348 die("More than one commit to dig from %s and %s?",
2349 revs
.pending
.objects
[i
].name
,
2351 sb
.final
= (struct commit
*) obj
;
2352 final_commit_name
= revs
.pending
.objects
[i
].name
;
2357 * "--not A B -- path" without anything positive;
2358 * do not default to HEAD, but use the working tree
2361 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2362 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2364 else if (contents_from
)
2365 die("Cannot use --contents with final commit object name");
2368 * If we have bottom, this will mark the ancestors of the
2369 * bottom commits we would reach while traversing as
2372 prepare_revision_walk(&revs
);
2374 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2377 buf
= xmalloc(o
->file
.size
+ 1);
2378 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2380 sb
.final_buf_size
= o
->file
.size
;
2383 o
= get_origin(&sb
, sb
.final
, path
);
2384 if (fill_blob_sha1(o
))
2385 die("no such path %s in %s", path
, final_commit_name
);
2387 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2388 &sb
.final_buf_size
);
2390 die("Cannot read blob %s for path %s",
2391 sha1_to_hex(o
->blob_sha1
),
2395 lno
= prepare_lines(&sb
);
2399 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2400 if (bottom
&& top
&& top
< bottom
) {
2402 tmp
= top
; top
= bottom
; bottom
= tmp
;
2410 die("file %s has only %lu lines", path
, lno
);
2412 ent
= xcalloc(1, sizeof(*ent
));
2414 ent
->num_lines
= top
- bottom
;
2416 ent
->s_lno
= bottom
;
2421 if (revs_file
&& read_ancestry(revs_file
))
2422 die("reading graft file %s failed: %s",
2423 revs_file
, strerror(errno
));
2425 read_mailmap(&mailmap
, ".mailmap", NULL
);
2430 assign_blame(&sb
, &revs
, opt
);
2437 if (!(output_option
& OUTPUT_PORCELAIN
))
2438 find_alignment(&sb
, &output_option
);
2440 output(&sb
, output_option
);
2441 free((void *)sb
.final_buf
);
2442 for (ent
= sb
.ent
; ent
; ) {
2443 struct blame_entry
*e
= ent
->next
;
2449 printf("num read blob: %d\n", num_read_blob
);
2450 printf("num get patch: %d\n", num_get_patch
);
2451 printf("num commits: %d\n", num_commits
);