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
)));
109 * Origin is refcounted and usually we keep the blob contents to be
112 static inline struct origin
*origin_incref(struct origin
*o
)
119 static void origin_decref(struct origin
*o
)
121 if (o
&& --o
->refcnt
<= 0) {
124 memset(o
, 0, sizeof(*o
));
130 * Each group of lines is described by a blame_entry; it can be split
131 * as we pass blame to the parents. They form a linked list in the
132 * scoreboard structure, sorted by the target line number.
135 struct blame_entry
*prev
;
136 struct blame_entry
*next
;
138 /* the first line of this group in the final image;
139 * internally all line numbers are 0 based.
143 /* how many lines this group has */
146 /* the commit that introduced this group into the final image */
147 struct origin
*suspect
;
149 /* true if the suspect is truly guilty; false while we have not
150 * checked if the group came from one of its parents.
154 /* the line number of the first line of this group in the
155 * suspect's file; internally all line numbers are 0 based.
159 /* how significant this entry is -- cached to avoid
160 * scanning the lines over and over.
166 * The current state of the blame assignment.
169 /* the final commit (i.e. where we started digging from) */
170 struct commit
*final
;
175 * The contents in the final image.
176 * Used by many functions to obtain contents of the nth line,
177 * indexed with scoreboard.lineno[blame_entry.lno].
179 const char *final_buf
;
180 unsigned long final_buf_size
;
182 /* linked list of blames */
183 struct blame_entry
*ent
;
185 /* look-up a line in the final buffer */
190 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
194 if (a
->commit
!= b
->commit
)
196 return !strcmp(a
->path
, b
->path
);
199 static void sanity_check_refcnt(struct scoreboard
*);
202 * If two blame entries that are next to each other came from
203 * contiguous lines in the same origin (i.e. <commit, path> pair),
204 * merge them together.
206 static void coalesce(struct scoreboard
*sb
)
208 struct blame_entry
*ent
, *next
;
210 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
211 if (same_suspect(ent
->suspect
, next
->suspect
) &&
212 ent
->guilty
== next
->guilty
&&
213 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
214 ent
->num_lines
+= next
->num_lines
;
215 ent
->next
= next
->next
;
217 ent
->next
->prev
= ent
;
218 origin_decref(next
->suspect
);
221 next
= ent
; /* again */
225 if (DEBUG
) /* sanity */
226 sanity_check_refcnt(sb
);
230 * Given a commit and a path in it, create a new origin structure.
231 * The callers that add blame to the scoreboard should use
232 * get_origin() to obtain shared, refcounted copy instead of calling
233 * this function directly.
235 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
238 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
241 strcpy(o
->path
, path
);
246 * Locate an existing origin or create a new one.
248 static struct origin
*get_origin(struct scoreboard
*sb
,
249 struct commit
*commit
,
252 struct blame_entry
*e
;
254 for (e
= sb
->ent
; e
; e
= e
->next
) {
255 if (e
->suspect
->commit
== commit
&&
256 !strcmp(e
->suspect
->path
, path
))
257 return origin_incref(e
->suspect
);
259 return make_origin(commit
, path
);
263 * Fill the blob_sha1 field of an origin if it hasn't, so that later
264 * call to fill_origin_blob() can use it to locate the data. blob_sha1
265 * for an origin is also used to pass the blame for the entire file to
266 * the parent to detect the case where a child's blob is identical to
267 * that of its parent's.
269 static int fill_blob_sha1(struct origin
*origin
)
273 if (!is_null_sha1(origin
->blob_sha1
))
275 if (get_tree_entry(origin
->commit
->object
.sha1
,
277 origin
->blob_sha1
, &mode
))
279 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
283 hashclr(origin
->blob_sha1
);
288 * We have an origin -- check if the same path exists in the
289 * parent and return an origin structure to represent it.
291 static struct origin
*find_origin(struct scoreboard
*sb
,
292 struct commit
*parent
,
293 struct origin
*origin
)
295 struct origin
*porigin
= NULL
;
296 struct diff_options diff_opts
;
297 const char *paths
[2];
301 * Each commit object can cache one origin in that
302 * commit. This is a freestanding copy of origin and
305 struct origin
*cached
= parent
->util
;
306 if (!strcmp(cached
->path
, origin
->path
)) {
308 * The same path between origin and its parent
309 * without renaming -- the most common case.
311 porigin
= get_origin(sb
, parent
, cached
->path
);
314 * If the origin was newly created (i.e. get_origin
315 * would call make_origin if none is found in the
316 * scoreboard), it does not know the blob_sha1,
317 * so copy it. Otherwise porigin was in the
318 * scoreboard and already knows blob_sha1.
320 if (porigin
->refcnt
== 1)
321 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
324 /* otherwise it was not very useful; free it */
329 /* See if the origin->path is different between parent
330 * and origin first. Most of the time they are the
331 * same and diff-tree is fairly efficient about this.
333 diff_setup(&diff_opts
);
334 diff_opts
.recursive
= 1;
335 diff_opts
.detect_rename
= 0;
336 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
337 paths
[0] = origin
->path
;
340 diff_tree_setup_paths(paths
, &diff_opts
);
341 if (diff_setup_done(&diff_opts
) < 0)
344 if (is_null_sha1(origin
->commit
->object
.sha1
))
345 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
347 diff_tree_sha1(parent
->tree
->object
.sha1
,
348 origin
->commit
->tree
->object
.sha1
,
350 diffcore_std(&diff_opts
);
352 /* It is either one entry that says "modified", or "created",
355 if (!diff_queued_diff
.nr
) {
356 /* The path is the same as parent */
357 porigin
= get_origin(sb
, parent
, origin
->path
);
358 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
360 else if (diff_queued_diff
.nr
!= 1)
361 die("internal error in blame::find_origin");
363 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
366 die("internal error in blame::find_origin (%c)",
369 porigin
= get_origin(sb
, parent
, origin
->path
);
370 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
374 /* Did not exist in parent, or type changed */
378 diff_flush(&diff_opts
);
381 * Create a freestanding copy that is not part of
382 * the refcounted origin found in the scoreboard, and
383 * cache it in the commit.
385 struct origin
*cached
;
387 cached
= make_origin(porigin
->commit
, porigin
->path
);
388 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
389 parent
->util
= cached
;
395 * We have an origin -- find the path that corresponds to it in its
396 * parent and return an origin structure to represent it.
398 static struct origin
*find_rename(struct scoreboard
*sb
,
399 struct commit
*parent
,
400 struct origin
*origin
)
402 struct origin
*porigin
= NULL
;
403 struct diff_options diff_opts
;
405 const char *paths
[2];
407 diff_setup(&diff_opts
);
408 diff_opts
.recursive
= 1;
409 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
410 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
411 diff_opts
.single_follow
= origin
->path
;
413 diff_tree_setup_paths(paths
, &diff_opts
);
414 if (diff_setup_done(&diff_opts
) < 0)
417 if (is_null_sha1(origin
->commit
->object
.sha1
))
418 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
420 diff_tree_sha1(parent
->tree
->object
.sha1
,
421 origin
->commit
->tree
->object
.sha1
,
423 diffcore_std(&diff_opts
);
425 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
426 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
427 if ((p
->status
== 'R' || p
->status
== 'C') &&
428 !strcmp(p
->two
->path
, origin
->path
)) {
429 porigin
= get_origin(sb
, parent
, p
->one
->path
);
430 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
434 diff_flush(&diff_opts
);
439 * Parsing of patch chunks...
442 /* line number in postimage; up to but not including this
443 * line is the same as preimage
447 /* preimage line number after this chunk */
450 /* postimage line number after this chunk */
455 struct chunk
*chunks
;
459 struct blame_diff_state
{
460 struct xdiff_emit_state xm
;
462 unsigned hunk_post_context
;
463 unsigned hunk_in_pre_context
: 1;
466 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
468 struct blame_diff_state
*state
= state_
;
470 int off1
, off2
, len1
, len2
, num
;
472 num
= state
->ret
->num
;
473 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
474 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
475 state
->ret
->chunks
[num
- 1].same
++;
477 state
->hunk_in_pre_context
= 0;
479 state
->hunk_post_context
++;
481 state
->hunk_post_context
= 0;
486 if (num
&& state
->hunk_post_context
) {
487 chunk
= &state
->ret
->chunks
[num
- 1];
488 chunk
->p_next
-= state
->hunk_post_context
;
489 chunk
->t_next
-= state
->hunk_post_context
;
491 state
->ret
->num
= ++num
;
492 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
493 sizeof(struct chunk
) * num
);
494 chunk
= &state
->ret
->chunks
[num
- 1];
495 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
500 /* Line numbers in patch output are one based. */
504 chunk
->same
= len2
? off2
: (off2
+ 1);
506 chunk
->p_next
= off1
+ (len1
? len1
: 1);
507 chunk
->t_next
= chunk
->same
+ len2
;
508 state
->hunk_in_pre_context
= 1;
509 state
->hunk_post_context
= 0;
512 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
515 struct blame_diff_state state
;
520 xpp
.flags
= xdl_opts
;
521 memset(&xecfg
, 0, sizeof(xecfg
));
522 xecfg
.ctxlen
= context
;
523 ecb
.outf
= xdiff_outf
;
525 memset(&state
, 0, sizeof(state
));
526 state
.xm
.consume
= process_u_diff
;
527 state
.ret
= xmalloc(sizeof(struct patch
));
528 state
.ret
->chunks
= NULL
;
531 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
533 if (state
.ret
->num
) {
535 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
536 chunk
->p_next
-= state
.hunk_post_context
;
537 chunk
->t_next
-= state
.hunk_post_context
;
543 * Run diff between two origins and grab the patch output, so that
544 * we can pass blame for lines origin is currently suspected for
547 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
549 mmfile_t file_p
, file_o
;
552 fill_origin_blob(parent
, &file_p
);
553 fill_origin_blob(origin
, &file_o
);
554 if (!file_p
.ptr
|| !file_o
.ptr
)
556 patch
= compare_buffer(&file_p
, &file_o
, 0);
561 static void free_patch(struct patch
*p
)
568 * Link in a new blame entry to the scoreboard. Entries that cover the
569 * same line range have been removed from the scoreboard previously.
571 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
573 struct blame_entry
*ent
, *prev
= NULL
;
575 origin_incref(e
->suspect
);
577 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
580 /* prev, if not NULL, is the last one that is below e */
583 e
->next
= prev
->next
;
595 * src typically is on-stack; we want to copy the information in it to
596 * an malloced blame_entry that is already on the linked list of the
597 * scoreboard. The origin of dst loses a refcnt while the origin of src
600 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
602 struct blame_entry
*p
, *n
;
606 origin_incref(src
->suspect
);
607 origin_decref(dst
->suspect
);
608 memcpy(dst
, src
, sizeof(*src
));
614 static const char *nth_line(struct scoreboard
*sb
, int lno
)
616 return sb
->final_buf
+ sb
->lineno
[lno
];
620 * It is known that lines between tlno to same came from parent, and e
621 * has an overlap with that range. it also is known that parent's
622 * line plno corresponds to e's line tlno.
628 * <------------------>
630 * Split e into potentially three parts; before this chunk, the chunk
631 * to be blamed for the parent, and after that portion.
633 static void split_overlap(struct blame_entry
*split
,
634 struct blame_entry
*e
,
635 int tlno
, int plno
, int same
,
636 struct origin
*parent
)
639 memset(split
, 0, sizeof(struct blame_entry
[3]));
641 if (e
->s_lno
< tlno
) {
642 /* there is a pre-chunk part not blamed on parent */
643 split
[0].suspect
= origin_incref(e
->suspect
);
644 split
[0].lno
= e
->lno
;
645 split
[0].s_lno
= e
->s_lno
;
646 split
[0].num_lines
= tlno
- e
->s_lno
;
647 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
648 split
[1].s_lno
= plno
;
651 split
[1].lno
= e
->lno
;
652 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
655 if (same
< e
->s_lno
+ e
->num_lines
) {
656 /* there is a post-chunk part not blamed on parent */
657 split
[2].suspect
= origin_incref(e
->suspect
);
658 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
659 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
660 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
661 chunk_end_lno
= split
[2].lno
;
664 chunk_end_lno
= e
->lno
+ e
->num_lines
;
665 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
668 * if it turns out there is nothing to blame the parent for,
669 * forget about the splitting. !split[1].suspect signals this.
671 if (split
[1].num_lines
< 1)
673 split
[1].suspect
= origin_incref(parent
);
677 * split_overlap() divided an existing blame e into up to three parts
678 * in split. Adjust the linked list of blames in the scoreboard to
681 static void split_blame(struct scoreboard
*sb
,
682 struct blame_entry
*split
,
683 struct blame_entry
*e
)
685 struct blame_entry
*new_entry
;
687 if (split
[0].suspect
&& split
[2].suspect
) {
688 /* The first part (reuse storage for the existing entry e) */
689 dup_entry(e
, &split
[0]);
691 /* The last part -- me */
692 new_entry
= xmalloc(sizeof(*new_entry
));
693 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
694 add_blame_entry(sb
, new_entry
);
696 /* ... and the middle part -- parent */
697 new_entry
= xmalloc(sizeof(*new_entry
));
698 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
699 add_blame_entry(sb
, new_entry
);
701 else if (!split
[0].suspect
&& !split
[2].suspect
)
703 * The parent covers the entire area; reuse storage for
704 * e and replace it with the parent.
706 dup_entry(e
, &split
[1]);
707 else if (split
[0].suspect
) {
708 /* me and then parent */
709 dup_entry(e
, &split
[0]);
711 new_entry
= xmalloc(sizeof(*new_entry
));
712 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
713 add_blame_entry(sb
, new_entry
);
716 /* parent and then me */
717 dup_entry(e
, &split
[1]);
719 new_entry
= xmalloc(sizeof(*new_entry
));
720 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
721 add_blame_entry(sb
, new_entry
);
724 if (DEBUG
) { /* sanity */
725 struct blame_entry
*ent
;
726 int lno
= sb
->ent
->lno
, corrupt
= 0;
728 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
733 lno
+= ent
->num_lines
;
737 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
738 printf("L %8d l %8d n %8d\n",
739 lno
, ent
->lno
, ent
->num_lines
);
740 lno
= ent
->lno
+ ent
->num_lines
;
748 * After splitting the blame, the origins used by the
749 * on-stack blame_entry should lose one refcnt each.
751 static void decref_split(struct blame_entry
*split
)
755 for (i
= 0; i
< 3; i
++)
756 origin_decref(split
[i
].suspect
);
760 * Helper for blame_chunk(). blame_entry e is known to overlap with
761 * the patch hunk; split it and pass blame to the parent.
763 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
764 int tlno
, int plno
, int same
,
765 struct origin
*parent
)
767 struct blame_entry split
[3];
769 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
770 if (split
[1].suspect
)
771 split_blame(sb
, split
, e
);
776 * Find the line number of the last line the target is suspected for.
778 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
780 struct blame_entry
*e
;
781 int last_in_target
= -1;
783 for (e
= sb
->ent
; e
; e
= e
->next
) {
784 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
786 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
787 last_in_target
= e
->s_lno
+ e
->num_lines
;
789 return last_in_target
;
793 * Process one hunk from the patch between the current suspect for
794 * blame_entry e and its parent. Find and split the overlap, and
795 * pass blame to the overlapping part to the parent.
797 static void blame_chunk(struct scoreboard
*sb
,
798 int tlno
, int plno
, int same
,
799 struct origin
*target
, struct origin
*parent
)
801 struct blame_entry
*e
;
803 for (e
= sb
->ent
; e
; e
= e
->next
) {
804 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
806 if (same
<= e
->s_lno
)
808 if (tlno
< e
->s_lno
+ e
->num_lines
)
809 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
814 * We are looking at the origin 'target' and aiming to pass blame
815 * for the lines it is suspected to its parent. Run diff to find
816 * which lines came from parent and pass blame for them.
818 static int pass_blame_to_parent(struct scoreboard
*sb
,
819 struct origin
*target
,
820 struct origin
*parent
)
822 int i
, last_in_target
, plno
, tlno
;
825 last_in_target
= find_last_in_target(sb
, target
);
826 if (last_in_target
< 0)
827 return 1; /* nothing remains for this target */
829 patch
= get_patch(parent
, target
);
831 for (i
= 0; i
< patch
->num
; i
++) {
832 struct chunk
*chunk
= &patch
->chunks
[i
];
834 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
835 plno
= chunk
->p_next
;
836 tlno
= chunk
->t_next
;
838 /* The rest (i.e. anything after tlno) are the same as the parent */
839 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
846 * The lines in blame_entry after splitting blames many times can become
847 * very small and trivial, and at some point it becomes pointless to
848 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
849 * ordinary C program, and it is not worth to say it was copied from
850 * totally unrelated file in the parent.
852 * Compute how trivial the lines in the blame_entry are.
854 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
863 cp
= nth_line(sb
, e
->lno
);
864 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
866 unsigned ch
= *((unsigned char *)cp
);
876 * best_so_far[] and this[] are both a split of an existing blame_entry
877 * that passes blame to the parent. Maintain best_so_far the best split
878 * so far, by comparing this and best_so_far and copying this into
879 * bst_so_far as needed.
881 static void copy_split_if_better(struct scoreboard
*sb
,
882 struct blame_entry
*best_so_far
,
883 struct blame_entry
*this)
887 if (!this[1].suspect
)
889 if (best_so_far
[1].suspect
) {
890 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
894 for (i
= 0; i
< 3; i
++)
895 origin_incref(this[i
].suspect
);
896 decref_split(best_so_far
);
897 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
901 * We are looking at a part of the final image represented by
902 * ent (tlno and same are offset by ent->s_lno).
903 * tlno is where we are looking at in the final image.
904 * up to (but not including) same match preimage.
905 * plno is where we are looking at in the preimage.
907 * <-------------- final image ---------------------->
910 * <---------preimage----->
913 * All line numbers are 0-based.
915 static void handle_split(struct scoreboard
*sb
,
916 struct blame_entry
*ent
,
917 int tlno
, int plno
, int same
,
918 struct origin
*parent
,
919 struct blame_entry
*split
)
921 if (ent
->num_lines
<= tlno
)
924 struct blame_entry
this[3];
927 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
928 copy_split_if_better(sb
, split
, this);
934 * Find the lines from parent that are the same as ent so that
935 * we can pass blames to it. file_p has the blob contents for
938 static void find_copy_in_blob(struct scoreboard
*sb
,
939 struct blame_entry
*ent
,
940 struct origin
*parent
,
941 struct blame_entry
*split
,
951 * Prepare mmfile that contains only the lines in ent.
953 cp
= nth_line(sb
, ent
->lno
);
954 file_o
.ptr
= (char*) cp
;
955 cnt
= ent
->num_lines
;
957 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
961 file_o
.size
= cp
- file_o
.ptr
;
963 patch
= compare_buffer(file_p
, &file_o
, 1);
966 * file_o is a part of final image we are annotating.
967 * file_p partially may match that image.
969 memset(split
, 0, sizeof(struct blame_entry
[3]));
971 for (i
= 0; i
< patch
->num
; i
++) {
972 struct chunk
*chunk
= &patch
->chunks
[i
];
974 handle_split(sb
, ent
, tlno
, plno
, chunk
->same
, parent
, split
);
975 plno
= chunk
->p_next
;
976 tlno
= chunk
->t_next
;
978 /* remainder, if any, all match the preimage */
979 handle_split(sb
, ent
, tlno
, plno
, ent
->num_lines
, parent
, split
);
984 * See if lines currently target is suspected for can be attributed to
987 static int find_move_in_parent(struct scoreboard
*sb
,
988 struct origin
*target
,
989 struct origin
*parent
)
991 int last_in_target
, made_progress
;
992 struct blame_entry
*e
, split
[3];
995 last_in_target
= find_last_in_target(sb
, target
);
996 if (last_in_target
< 0)
997 return 1; /* nothing remains for this target */
999 fill_origin_blob(parent
, &file_p
);
1004 while (made_progress
) {
1006 for (e
= sb
->ent
; e
; e
= e
->next
) {
1007 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
1009 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1010 if (split
[1].suspect
&&
1011 blame_move_score
< ent_score(sb
, &split
[1])) {
1012 split_blame(sb
, split
, e
);
1015 decref_split(split
);
1022 struct blame_entry
*ent
;
1023 struct blame_entry split
[3];
1027 * Count the number of entries the target is suspected for,
1028 * and prepare a list of entry and the best split.
1030 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
1031 struct origin
*target
,
1034 struct blame_entry
*e
;
1036 struct blame_list
*blame_list
= NULL
;
1038 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1039 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1042 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1043 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1044 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1045 blame_list
[i
++].ent
= e
;
1047 *num_ents_p
= num_ents
;
1052 * For lines target is suspected for, see if we can find code movement
1053 * across file boundary from the parent commit. porigin is the path
1054 * in the parent we already tried.
1056 static int find_copy_in_parent(struct scoreboard
*sb
,
1057 struct origin
*target
,
1058 struct commit
*parent
,
1059 struct origin
*porigin
,
1062 struct diff_options diff_opts
;
1063 const char *paths
[1];
1066 struct blame_list
*blame_list
;
1069 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1071 return 1; /* nothing remains for this target */
1073 diff_setup(&diff_opts
);
1074 diff_opts
.recursive
= 1;
1075 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1078 diff_tree_setup_paths(paths
, &diff_opts
);
1079 if (diff_setup_done(&diff_opts
) < 0)
1082 /* Try "find copies harder" on new path if requested;
1083 * we do not want to use diffcore_rename() actually to
1084 * match things up; find_copies_harder is set only to
1085 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1086 * and this code needs to be after diff_setup_done(), which
1087 * usually makes find-copies-harder imply copy detection.
1089 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1090 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1091 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1092 diff_opts
.find_copies_harder
= 1;
1094 if (is_null_sha1(target
->commit
->object
.sha1
))
1095 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1097 diff_tree_sha1(parent
->tree
->object
.sha1
,
1098 target
->commit
->tree
->object
.sha1
,
1101 if (!diff_opts
.find_copies_harder
)
1102 diffcore_std(&diff_opts
);
1106 int made_progress
= 0;
1108 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1109 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1110 struct origin
*norigin
;
1112 struct blame_entry
this[3];
1114 if (!DIFF_FILE_VALID(p
->one
))
1115 continue; /* does not exist in parent */
1116 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1117 /* find_move already dealt with this path */
1120 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1121 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1122 fill_origin_blob(norigin
, &file_p
);
1126 for (j
= 0; j
< num_ents
; j
++) {
1127 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1128 norigin
, this, &file_p
);
1129 copy_split_if_better(sb
, blame_list
[j
].split
,
1133 origin_decref(norigin
);
1136 for (j
= 0; j
< num_ents
; j
++) {
1137 struct blame_entry
*split
= blame_list
[j
].split
;
1138 if (split
[1].suspect
&&
1139 blame_copy_score
< ent_score(sb
, &split
[1])) {
1140 split_blame(sb
, split
, blame_list
[j
].ent
);
1143 decref_split(split
);
1149 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1155 diff_flush(&diff_opts
);
1161 * The blobs of origin and porigin exactly match, so everything
1162 * origin is suspected for can be blamed on the parent.
1164 static void pass_whole_blame(struct scoreboard
*sb
,
1165 struct origin
*origin
, struct origin
*porigin
)
1167 struct blame_entry
*e
;
1169 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1170 /* Steal its file */
1171 porigin
->file
= origin
->file
;
1172 origin
->file
.ptr
= NULL
;
1174 for (e
= sb
->ent
; e
; e
= e
->next
) {
1175 if (!same_suspect(e
->suspect
, origin
))
1177 origin_incref(porigin
);
1178 origin_decref(e
->suspect
);
1179 e
->suspect
= porigin
;
1183 #define MAXPARENT 16
1185 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1188 struct commit
*commit
= origin
->commit
;
1189 struct commit_list
*parent
;
1190 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1192 memset(parent_origin
, 0, sizeof(parent_origin
));
1194 /* The first pass looks for unrenamed path to optimize for
1195 * common cases, then we look for renames in the second pass.
1197 for (pass
= 0; pass
< 2; pass
++) {
1198 struct origin
*(*find
)(struct scoreboard
*,
1199 struct commit
*, struct origin
*);
1200 find
= pass
? find_rename
: find_origin
;
1202 for (i
= 0, parent
= commit
->parents
;
1203 i
< MAXPARENT
&& parent
;
1204 parent
= parent
->next
, i
++) {
1205 struct commit
*p
= parent
->item
;
1208 if (parent_origin
[i
])
1210 if (parse_commit(p
))
1212 porigin
= find(sb
, p
, origin
);
1215 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1216 pass_whole_blame(sb
, origin
, porigin
);
1217 origin_decref(porigin
);
1220 for (j
= same
= 0; j
< i
; j
++)
1221 if (parent_origin
[j
] &&
1222 !hashcmp(parent_origin
[j
]->blob_sha1
,
1223 porigin
->blob_sha1
)) {
1228 parent_origin
[i
] = porigin
;
1230 origin_decref(porigin
);
1235 for (i
= 0, parent
= commit
->parents
;
1236 i
< MAXPARENT
&& parent
;
1237 parent
= parent
->next
, i
++) {
1238 struct origin
*porigin
= parent_origin
[i
];
1241 if (pass_blame_to_parent(sb
, origin
, porigin
))
1246 * Optionally find moves in parents' files.
1248 if (opt
& PICKAXE_BLAME_MOVE
)
1249 for (i
= 0, parent
= commit
->parents
;
1250 i
< MAXPARENT
&& parent
;
1251 parent
= parent
->next
, i
++) {
1252 struct origin
*porigin
= parent_origin
[i
];
1255 if (find_move_in_parent(sb
, origin
, porigin
))
1260 * Optionally find copies from parents' files.
1262 if (opt
& PICKAXE_BLAME_COPY
)
1263 for (i
= 0, parent
= commit
->parents
;
1264 i
< MAXPARENT
&& parent
;
1265 parent
= parent
->next
, i
++) {
1266 struct origin
*porigin
= parent_origin
[i
];
1267 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1273 for (i
= 0; i
< MAXPARENT
; i
++)
1274 origin_decref(parent_origin
[i
]);
1278 * Information on commits, used for output.
1283 const char *author_mail
;
1284 unsigned long author_time
;
1285 const char *author_tz
;
1287 /* filled only when asked for details */
1288 const char *committer
;
1289 const char *committer_mail
;
1290 unsigned long committer_time
;
1291 const char *committer_tz
;
1293 const char *summary
;
1297 * Parse author/committer line in the commit object buffer
1299 static void get_ac_line(const char *inbuf
, const char *what
,
1300 int bufsz
, char *person
, const char **mail
,
1301 unsigned long *time
, const char **tz
)
1303 int len
, tzlen
, maillen
;
1304 char *tmp
, *endp
, *timepos
;
1306 tmp
= strstr(inbuf
, what
);
1309 tmp
+= strlen(what
);
1310 endp
= strchr(tmp
, '\n');
1318 *mail
= *tz
= "(unknown)";
1322 memcpy(person
, tmp
, len
);
1330 tzlen
= (person
+len
)-(tmp
+1);
1335 *time
= strtoul(tmp
, NULL
, 10);
1343 maillen
= timepos
- tmp
;
1349 * mailmap expansion may make the name longer.
1350 * make room by pushing stuff down.
1352 tmp
= person
+ bufsz
- (tzlen
+ 1);
1353 memmove(tmp
, *tz
, tzlen
);
1357 tmp
= tmp
- (maillen
+ 1);
1358 memmove(tmp
, *mail
, maillen
);
1363 * Now, convert e-mail using mailmap
1365 map_email(&mailmap
, tmp
+ 1, person
, tmp
-person
-1);
1368 static void get_commit_info(struct commit
*commit
,
1369 struct commit_info
*ret
,
1374 static char author_buf
[1024];
1375 static char committer_buf
[1024];
1376 static char summary_buf
[1024];
1379 * We've operated without save_commit_buffer, so
1380 * we now need to populate them for output.
1382 if (!commit
->buffer
) {
1383 enum object_type type
;
1386 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1388 ret
->author
= author_buf
;
1389 get_ac_line(commit
->buffer
, "\nauthor ",
1390 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1391 &ret
->author_time
, &ret
->author_tz
);
1396 ret
->committer
= committer_buf
;
1397 get_ac_line(commit
->buffer
, "\ncommitter ",
1398 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1399 &ret
->committer_time
, &ret
->committer_tz
);
1401 ret
->summary
= summary_buf
;
1402 tmp
= strstr(commit
->buffer
, "\n\n");
1405 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1409 endp
= strchr(tmp
, '\n');
1411 endp
= tmp
+ strlen(tmp
);
1413 if (len
>= sizeof(summary_buf
) || len
== 0)
1415 memcpy(summary_buf
, tmp
, len
);
1416 summary_buf
[len
] = 0;
1420 * To allow LF and other nonportable characters in pathnames,
1421 * they are c-style quoted as needed.
1423 static void write_filename_info(const char *path
)
1425 printf("filename ");
1426 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1431 * The blame_entry is found to be guilty for the range. Mark it
1432 * as such, and show it in incremental output.
1434 static void found_guilty_entry(struct blame_entry
*ent
)
1440 struct origin
*suspect
= ent
->suspect
;
1442 printf("%s %d %d %d\n",
1443 sha1_to_hex(suspect
->commit
->object
.sha1
),
1444 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1445 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1446 struct commit_info ci
;
1447 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1448 get_commit_info(suspect
->commit
, &ci
, 1);
1449 printf("author %s\n", ci
.author
);
1450 printf("author-mail %s\n", ci
.author_mail
);
1451 printf("author-time %lu\n", ci
.author_time
);
1452 printf("author-tz %s\n", ci
.author_tz
);
1453 printf("committer %s\n", ci
.committer
);
1454 printf("committer-mail %s\n", ci
.committer_mail
);
1455 printf("committer-time %lu\n", ci
.committer_time
);
1456 printf("committer-tz %s\n", ci
.committer_tz
);
1457 printf("summary %s\n", ci
.summary
);
1458 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1459 printf("boundary\n");
1461 write_filename_info(suspect
->path
);
1462 maybe_flush_or_die(stdout
, "stdout");
1467 * The main loop -- while the scoreboard has lines whose true origin
1468 * is still unknown, pick one blame_entry, and allow its current
1469 * suspect to pass blames to its parents.
1471 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1474 struct blame_entry
*ent
;
1475 struct commit
*commit
;
1476 struct origin
*suspect
= NULL
;
1478 /* find one suspect to break down */
1479 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1481 suspect
= ent
->suspect
;
1483 return; /* all done */
1486 * We will use this suspect later in the loop,
1487 * so hold onto it in the meantime.
1489 origin_incref(suspect
);
1490 commit
= suspect
->commit
;
1491 if (!commit
->object
.parsed
)
1492 parse_commit(commit
);
1493 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1494 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1495 pass_blame(sb
, suspect
, opt
);
1497 commit
->object
.flags
|= UNINTERESTING
;
1498 if (commit
->object
.parsed
)
1499 mark_parents_uninteresting(commit
);
1501 /* treat root commit as boundary */
1502 if (!commit
->parents
&& !show_root
)
1503 commit
->object
.flags
|= UNINTERESTING
;
1505 /* Take responsibility for the remaining entries */
1506 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1507 if (same_suspect(ent
->suspect
, suspect
))
1508 found_guilty_entry(ent
);
1509 origin_decref(suspect
);
1511 if (DEBUG
) /* sanity */
1512 sanity_check_refcnt(sb
);
1516 static const char *format_time(unsigned long time
, const char *tz_str
,
1519 static char time_buf
[128];
1524 if (show_raw_time
) {
1525 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1530 minutes
= tz
< 0 ? -tz
: tz
;
1531 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1532 minutes
= tz
< 0 ? -minutes
: minutes
;
1533 t
= time
+ minutes
* 60;
1536 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1537 strcat(time_buf
, tz_str
);
1541 #define OUTPUT_ANNOTATE_COMPAT 001
1542 #define OUTPUT_LONG_OBJECT_NAME 002
1543 #define OUTPUT_RAW_TIMESTAMP 004
1544 #define OUTPUT_PORCELAIN 010
1545 #define OUTPUT_SHOW_NAME 020
1546 #define OUTPUT_SHOW_NUMBER 040
1547 #define OUTPUT_SHOW_SCORE 0100
1548 #define OUTPUT_NO_AUTHOR 0200
1550 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1554 struct origin
*suspect
= ent
->suspect
;
1557 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1558 printf("%s%c%d %d %d\n",
1560 ent
->guilty
? ' ' : '*', // purely for debugging
1564 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1565 struct commit_info ci
;
1566 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1567 get_commit_info(suspect
->commit
, &ci
, 1);
1568 printf("author %s\n", ci
.author
);
1569 printf("author-mail %s\n", ci
.author_mail
);
1570 printf("author-time %lu\n", ci
.author_time
);
1571 printf("author-tz %s\n", ci
.author_tz
);
1572 printf("committer %s\n", ci
.committer
);
1573 printf("committer-mail %s\n", ci
.committer_mail
);
1574 printf("committer-time %lu\n", ci
.committer_time
);
1575 printf("committer-tz %s\n", ci
.committer_tz
);
1576 write_filename_info(suspect
->path
);
1577 printf("summary %s\n", ci
.summary
);
1578 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1579 printf("boundary\n");
1581 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1582 write_filename_info(suspect
->path
);
1584 cp
= nth_line(sb
, ent
->lno
);
1585 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1588 printf("%s %d %d\n", hex
,
1589 ent
->s_lno
+ 1 + cnt
,
1590 ent
->lno
+ 1 + cnt
);
1595 } while (ch
!= '\n' &&
1596 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1600 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1604 struct origin
*suspect
= ent
->suspect
;
1605 struct commit_info ci
;
1607 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1609 get_commit_info(suspect
->commit
, &ci
, 1);
1610 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1612 cp
= nth_line(sb
, ent
->lno
);
1613 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1615 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1617 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1619 memset(hex
, ' ', length
);
1620 else if (!cmd_is_annotate
) {
1626 printf("%.*s", length
, hex
);
1627 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1628 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1629 format_time(ci
.author_time
, ci
.author_tz
,
1631 ent
->lno
+ 1 + cnt
);
1633 if (opt
& OUTPUT_SHOW_SCORE
)
1635 max_score_digits
, ent
->score
,
1636 ent
->suspect
->refcnt
);
1637 if (opt
& OUTPUT_SHOW_NAME
)
1638 printf(" %-*.*s", longest_file
, longest_file
,
1640 if (opt
& OUTPUT_SHOW_NUMBER
)
1641 printf(" %*d", max_orig_digits
,
1642 ent
->s_lno
+ 1 + cnt
);
1644 if (!(opt
& OUTPUT_NO_AUTHOR
))
1645 printf(" (%-*.*s %10s",
1646 longest_author
, longest_author
,
1648 format_time(ci
.author_time
,
1652 max_digits
, ent
->lno
+ 1 + cnt
);
1657 } while (ch
!= '\n' &&
1658 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1662 static void output(struct scoreboard
*sb
, int option
)
1664 struct blame_entry
*ent
;
1666 if (option
& OUTPUT_PORCELAIN
) {
1667 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1668 struct blame_entry
*oth
;
1669 struct origin
*suspect
= ent
->suspect
;
1670 struct commit
*commit
= suspect
->commit
;
1671 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1673 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1674 if ((oth
->suspect
->commit
!= commit
) ||
1675 !strcmp(oth
->suspect
->path
, suspect
->path
))
1677 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1683 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1684 if (option
& OUTPUT_PORCELAIN
)
1685 emit_porcelain(sb
, ent
);
1687 emit_other(sb
, ent
, option
);
1693 * To allow quick access to the contents of nth line in the
1694 * final image, prepare an index in the scoreboard.
1696 static int prepare_lines(struct scoreboard
*sb
)
1698 const char *buf
= sb
->final_buf
;
1699 unsigned long len
= sb
->final_buf_size
;
1700 int num
= 0, incomplete
= 0, bol
= 1;
1702 if (len
&& buf
[len
-1] != '\n')
1703 incomplete
++; /* incomplete line at the end */
1706 sb
->lineno
= xrealloc(sb
->lineno
,
1707 sizeof(int* ) * (num
+ 1));
1708 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1711 if (*buf
++ == '\n') {
1716 sb
->lineno
= xrealloc(sb
->lineno
,
1717 sizeof(int* ) * (num
+ incomplete
+ 1));
1718 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1719 sb
->num_lines
= num
+ incomplete
;
1720 return sb
->num_lines
;
1724 * Add phony grafts for use with -S; this is primarily to
1725 * support git-cvsserver that wants to give a linear history
1728 static int read_ancestry(const char *graft_file
)
1730 FILE *fp
= fopen(graft_file
, "r");
1734 while (fgets(buf
, sizeof(buf
), fp
)) {
1735 /* The format is just "Commit Parent1 Parent2 ...\n" */
1736 int len
= strlen(buf
);
1737 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1739 register_commit_graft(graft
, 0);
1746 * How many columns do we need to show line numbers in decimal?
1748 static int lineno_width(int lines
)
1752 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1758 * How many columns do we need to show line numbers, authors,
1761 static void find_alignment(struct scoreboard
*sb
, int *option
)
1763 int longest_src_lines
= 0;
1764 int longest_dst_lines
= 0;
1765 unsigned largest_score
= 0;
1766 struct blame_entry
*e
;
1768 for (e
= sb
->ent
; e
; e
= e
->next
) {
1769 struct origin
*suspect
= e
->suspect
;
1770 struct commit_info ci
;
1773 if (strcmp(suspect
->path
, sb
->path
))
1774 *option
|= OUTPUT_SHOW_NAME
;
1775 num
= strlen(suspect
->path
);
1776 if (longest_file
< num
)
1778 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1779 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1780 get_commit_info(suspect
->commit
, &ci
, 1);
1781 num
= strlen(ci
.author
);
1782 if (longest_author
< num
)
1783 longest_author
= num
;
1785 num
= e
->s_lno
+ e
->num_lines
;
1786 if (longest_src_lines
< num
)
1787 longest_src_lines
= num
;
1788 num
= e
->lno
+ e
->num_lines
;
1789 if (longest_dst_lines
< num
)
1790 longest_dst_lines
= num
;
1791 if (largest_score
< ent_score(sb
, e
))
1792 largest_score
= ent_score(sb
, e
);
1794 max_orig_digits
= lineno_width(longest_src_lines
);
1795 max_digits
= lineno_width(longest_dst_lines
);
1796 max_score_digits
= lineno_width(largest_score
);
1800 * For debugging -- origin is refcounted, and this asserts that
1801 * we do not underflow.
1803 static void sanity_check_refcnt(struct scoreboard
*sb
)
1806 struct blame_entry
*ent
;
1808 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1809 /* Nobody should have zero or negative refcnt */
1810 if (ent
->suspect
->refcnt
<= 0) {
1811 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1813 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1814 ent
->suspect
->refcnt
);
1818 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1819 /* Mark the ones that haven't been checked */
1820 if (0 < ent
->suspect
->refcnt
)
1821 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1823 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1825 * ... then pick each and see if they have the the
1829 struct blame_entry
*e
;
1830 struct origin
*suspect
= ent
->suspect
;
1832 if (0 < suspect
->refcnt
)
1834 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1835 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1836 if (e
->suspect
!= suspect
)
1840 if (suspect
->refcnt
!= found
) {
1841 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1843 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1844 ent
->suspect
->refcnt
, found
);
1850 find_alignment(sb
, &opt
);
1852 die("Baa %d!", baa
);
1857 * Used for the command line parsing; check if the path exists
1858 * in the working tree.
1860 static int has_path_in_work_tree(const char *path
)
1863 return !lstat(path
, &st
);
1866 static unsigned parse_score(const char *arg
)
1869 unsigned long score
= strtoul(arg
, &end
, 10);
1875 static const char *add_prefix(const char *prefix
, const char *path
)
1877 if (!prefix
|| !prefix
[0])
1879 return prefix_path(prefix
, strlen(prefix
), path
);
1883 * Parsing of (comma separated) one item in the -L option
1885 static const char *parse_loc(const char *spec
,
1886 struct scoreboard
*sb
, long lno
,
1887 long begin
, long *ret
)
1894 regmatch_t match
[1];
1896 /* Allow "-L <something>,+20" to mean starting at <something>
1897 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1900 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1901 num
= strtol(spec
+ 1, &term
, 10);
1902 if (term
!= spec
+ 1) {
1906 *ret
= begin
+ num
- 2;
1915 num
= strtol(spec
, &term
, 10);
1923 /* it could be a regexp of form /.../ */
1924 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1931 /* try [spec+1 .. term-1] as regexp */
1933 begin
--; /* input is in human terms */
1934 line
= nth_line(sb
, begin
);
1936 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1937 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1938 const char *cp
= line
+ match
[0].rm_so
;
1941 while (begin
++ < lno
) {
1942 nline
= nth_line(sb
, begin
);
1943 if (line
<= cp
&& cp
< nline
)
1954 regerror(reg_error
, ®exp
, errbuf
, 1024);
1955 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1960 * Parsing of -L option
1962 static void prepare_blame_range(struct scoreboard
*sb
,
1963 const char *bottomtop
,
1965 long *bottom
, long *top
)
1969 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1971 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1979 static int git_blame_config(const char *var
, const char *value
)
1981 if (!strcmp(var
, "blame.showroot")) {
1982 show_root
= git_config_bool(var
, value
);
1985 if (!strcmp(var
, "blame.blankboundary")) {
1986 blank_boundary
= git_config_bool(var
, value
);
1989 return git_default_config(var
, value
);
1992 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1994 struct commit
*commit
;
1995 struct origin
*origin
;
1996 unsigned char head_sha1
[20];
2001 unsigned long fin_size
;
2003 struct cache_entry
*ce
;
2006 if (get_sha1("HEAD", head_sha1
))
2007 die("No such ref: HEAD");
2010 commit
= xcalloc(1, sizeof(*commit
));
2011 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
2012 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
2013 commit
->object
.parsed
= 1;
2015 commit
->object
.type
= OBJ_COMMIT
;
2017 origin
= make_origin(commit
, path
);
2019 if (!contents_from
|| strcmp("-", contents_from
)) {
2021 const char *read_from
;
2023 if (contents_from
) {
2024 if (stat(contents_from
, &st
) < 0)
2025 die("Cannot stat %s", contents_from
);
2026 read_from
= contents_from
;
2029 if (lstat(path
, &st
) < 0)
2030 die("Cannot lstat %s", path
);
2033 fin_size
= xsize_t(st
.st_size
);
2034 buf
= xmalloc(fin_size
+1);
2035 mode
= canon_mode(st
.st_mode
);
2036 switch (st
.st_mode
& S_IFMT
) {
2038 fd
= open(read_from
, O_RDONLY
);
2040 die("cannot open %s", read_from
);
2041 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
2042 die("cannot read %s", read_from
);
2045 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
2046 die("cannot readlink %s", read_from
);
2049 die("unsupported file type %s", read_from
);
2053 /* Reading from stdin */
2054 contents_from
= "standard input";
2060 buf
= xrealloc(buf
, fin_size
+ cnt
);
2061 cnt
= xread(0, buf
+ fin_size
, cnt
);
2063 die("read error %s from stdin",
2069 buf
= xrealloc(buf
, fin_size
+ 1);
2072 origin
->file
.ptr
= buf
;
2073 origin
->file
.size
= fin_size
;
2074 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2075 commit
->util
= origin
;
2078 * Read the current index, replace the path entry with
2079 * origin->blob_sha1 without mucking with its mode or type
2080 * bits; we are not going to write this index out -- we just
2081 * want to run "diff-index --cached".
2088 int pos
= cache_name_pos(path
, len
);
2090 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2092 /* Let's not bother reading from HEAD tree */
2093 mode
= S_IFREG
| 0644;
2095 size
= cache_entry_size(len
);
2096 ce
= xcalloc(1, size
);
2097 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2098 memcpy(ce
->name
, path
, len
);
2099 ce
->ce_flags
= create_ce_flags(len
, 0);
2100 ce
->ce_mode
= create_ce_mode(mode
);
2101 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2104 * We are not going to write this out, so this does not matter
2105 * right now, but someday we might optimize diff-index --cached
2106 * with cache-tree information.
2108 cache_tree_invalidate_path(active_cache_tree
, path
);
2110 commit
->buffer
= xmalloc(400);
2111 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2112 snprintf(commit
->buffer
, 400,
2113 "tree 0000000000000000000000000000000000000000\n"
2117 "Version of %s from %s\n",
2118 sha1_to_hex(head_sha1
),
2119 ident
, ident
, path
, contents_from
? contents_from
: path
);
2123 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2125 struct rev_info revs
;
2127 struct scoreboard sb
;
2129 struct blame_entry
*ent
;
2130 int i
, seen_dashdash
, unk
, opt
;
2131 long bottom
, top
, lno
;
2132 int output_option
= 0;
2134 const char *revs_file
= NULL
;
2135 const char *final_commit_name
= NULL
;
2136 enum object_type type
;
2137 const char *bottomtop
= NULL
;
2138 const char *contents_from
= NULL
;
2140 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2142 git_config(git_blame_config
);
2143 save_commit_buffer
= 0;
2147 for (unk
= i
= 1; i
< argc
; i
++) {
2148 const char *arg
= argv
[i
];
2151 else if (!strcmp("-b", arg
))
2153 else if (!strcmp("--root", arg
))
2155 else if (!strcmp(arg
, "--show-stats"))
2157 else if (!strcmp("-c", arg
))
2158 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2159 else if (!strcmp("-t", arg
))
2160 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2161 else if (!strcmp("-l", arg
))
2162 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2163 else if (!strcmp("-s", arg
))
2164 output_option
|= OUTPUT_NO_AUTHOR
;
2165 else if (!strcmp("-w", arg
))
2166 xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2167 else if (!strcmp("-S", arg
) && ++i
< argc
)
2168 revs_file
= argv
[i
];
2169 else if (!prefixcmp(arg
, "-M")) {
2170 opt
|= PICKAXE_BLAME_MOVE
;
2171 blame_move_score
= parse_score(arg
+2);
2173 else if (!prefixcmp(arg
, "-C")) {
2175 * -C enables copy from removed files;
2176 * -C -C enables copy from existing files, but only
2177 * when blaming a new file;
2178 * -C -C -C enables copy from existing files for
2181 if (opt
& PICKAXE_BLAME_COPY_HARDER
)
2182 opt
|= PICKAXE_BLAME_COPY_HARDEST
;
2183 if (opt
& PICKAXE_BLAME_COPY
)
2184 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2185 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2186 blame_copy_score
= parse_score(arg
+2);
2188 else if (!prefixcmp(arg
, "-L")) {
2197 die("More than one '-L n,m' option given");
2200 else if (!strcmp("--contents", arg
)) {
2203 contents_from
= argv
[i
];
2205 else if (!strcmp("--incremental", arg
))
2207 else if (!strcmp("--score-debug", arg
))
2208 output_option
|= OUTPUT_SHOW_SCORE
;
2209 else if (!strcmp("-f", arg
) ||
2210 !strcmp("--show-name", arg
))
2211 output_option
|= OUTPUT_SHOW_NAME
;
2212 else if (!strcmp("-n", arg
) ||
2213 !strcmp("--show-number", arg
))
2214 output_option
|= OUTPUT_SHOW_NUMBER
;
2215 else if (!strcmp("-p", arg
) ||
2216 !strcmp("--porcelain", arg
))
2217 output_option
|= OUTPUT_PORCELAIN
;
2218 else if (!strcmp("--", arg
)) {
2230 if (!blame_move_score
)
2231 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2232 if (!blame_copy_score
)
2233 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2236 * We have collected options unknown to us in argv[1..unk]
2237 * which are to be passed to revision machinery if we are
2238 * going to do the "bottom" processing.
2240 * The remaining are:
2242 * (1) if seen_dashdash, its either
2243 * "-options -- <path>" or
2244 * "-options -- <path> <rev>".
2245 * but the latter is allowed only if there is no
2246 * options that we passed to revision machinery.
2248 * (2) otherwise, we may have "--" somewhere later and
2249 * might be looking at the first one of multiple 'rev'
2250 * parameters (e.g. " master ^next ^maint -- path").
2251 * See if there is a dashdash first, and give the
2252 * arguments before that to revision machinery.
2253 * After that there must be one 'path'.
2255 * (3) otherwise, its one of the three:
2256 * "-options <path> <rev>"
2257 * "-options <rev> <path>"
2259 * but again the first one is allowed only if
2260 * there is no options that we passed to revision
2264 if (seen_dashdash
) {
2268 path
= add_prefix(prefix
, argv
[i
]);
2269 if (i
+ 1 == argc
- 1) {
2272 argv
[unk
++] = argv
[i
+ 1];
2274 else if (i
+ 1 != argc
)
2275 /* garbage at end */
2280 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2281 if (!strcmp(argv
[j
], "--"))
2283 if (seen_dashdash
) {
2285 if (seen_dashdash
+ 1 != argc
- 1)
2287 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2288 for (j
= i
; j
< seen_dashdash
; j
++)
2289 argv
[unk
++] = argv
[j
];
2295 path
= add_prefix(prefix
, argv
[i
]);
2296 if (i
+ 1 == argc
- 1) {
2297 final_commit_name
= argv
[i
+ 1];
2299 /* if (unk == 1) we could be getting
2302 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2303 path
= add_prefix(prefix
, argv
[i
+ 1]);
2304 final_commit_name
= argv
[i
];
2307 else if (i
!= argc
- 1)
2308 usage(blame_usage
); /* garbage at end */
2310 if (!has_path_in_work_tree(path
))
2311 die("cannot stat path %s: %s",
2312 path
, strerror(errno
));
2316 if (final_commit_name
)
2317 argv
[unk
++] = final_commit_name
;
2320 * Now we got rev and path. We do not want the path pruning
2321 * but we may want "bottom" processing.
2323 argv
[unk
++] = "--"; /* terminate the rev name */
2326 init_revisions(&revs
, NULL
);
2327 setup_revisions(unk
, argv
, &revs
, NULL
);
2328 memset(&sb
, 0, sizeof(sb
));
2331 * There must be one and only one positive commit in the
2332 * revs->pending array.
2334 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2335 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2336 if (obj
->flags
& UNINTERESTING
)
2338 while (obj
->type
== OBJ_TAG
)
2339 obj
= deref_tag(obj
, NULL
, 0);
2340 if (obj
->type
!= OBJ_COMMIT
)
2341 die("Non commit %s?",
2342 revs
.pending
.objects
[i
].name
);
2344 die("More than one commit to dig from %s and %s?",
2345 revs
.pending
.objects
[i
].name
,
2347 sb
.final
= (struct commit
*) obj
;
2348 final_commit_name
= revs
.pending
.objects
[i
].name
;
2353 * "--not A B -- path" without anything positive;
2354 * do not default to HEAD, but use the working tree
2357 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2358 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2360 else if (contents_from
)
2361 die("Cannot use --contents with final commit object name");
2364 * If we have bottom, this will mark the ancestors of the
2365 * bottom commits we would reach while traversing as
2368 prepare_revision_walk(&revs
);
2370 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2373 buf
= xmalloc(o
->file
.size
+ 1);
2374 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2376 sb
.final_buf_size
= o
->file
.size
;
2379 o
= get_origin(&sb
, sb
.final
, path
);
2380 if (fill_blob_sha1(o
))
2381 die("no such path %s in %s", path
, final_commit_name
);
2383 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2384 &sb
.final_buf_size
);
2387 lno
= prepare_lines(&sb
);
2391 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2392 if (bottom
&& top
&& top
< bottom
) {
2394 tmp
= top
; top
= bottom
; bottom
= tmp
;
2402 die("file %s has only %lu lines", path
, lno
);
2404 ent
= xcalloc(1, sizeof(*ent
));
2406 ent
->num_lines
= top
- bottom
;
2408 ent
->s_lno
= bottom
;
2413 if (revs_file
&& read_ancestry(revs_file
))
2414 die("reading graft file %s failed: %s",
2415 revs_file
, strerror(errno
));
2417 read_mailmap(&mailmap
, ".mailmap", NULL
);
2419 assign_blame(&sb
, &revs
, opt
);
2426 if (!(output_option
& OUTPUT_PORCELAIN
))
2427 find_alignment(&sb
, &output_option
);
2429 output(&sb
, output_option
);
2430 free((void *)sb
.final_buf
);
2431 for (ent
= sb
.ent
; ent
; ) {
2432 struct blame_entry
*e
= ent
->next
;
2438 printf("num read blob: %d\n", num_read_blob
);
2439 printf("num get patch: %d\n", num_get_patch
);
2440 printf("num commits: %d\n", num_commits
);