4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
20 static char blame_usage
[] =
21 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
22 " -c Use the same output mode as git-annotate (Default: off)\n"
23 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
24 " -l Show long commit SHA1 (Default: off)\n"
25 " --root Do not treat root commits as boundaries (Default: off)\n"
26 " -t Show raw timestamp (Default: off)\n"
27 " -f, --show-name Show original filename (Default: auto)\n"
28 " -n, --show-number Show original linenumber (Default: off)\n"
29 " -s Suppress author name and timestamp (Default: off)\n"
30 " -p, --porcelain Show in a format designed for machine consumption\n"
31 " -L n,m Process only line range n,m, counting from 1\n"
32 " -M, -C Find line movements within and across files\n"
33 " --incremental Show blame entries as we find them, incrementally\n"
34 " --contents file Use <file>'s contents as the final image\n"
35 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
37 static int longest_file
;
38 static int longest_author
;
39 static int max_orig_digits
;
40 static int max_digits
;
41 static int max_score_digits
;
43 static int blank_boundary
;
44 static int incremental
;
45 static int cmd_is_annotate
;
52 static int num_read_blob
;
53 static int num_get_patch
;
54 static int num_commits
;
56 #define PICKAXE_BLAME_MOVE 01
57 #define PICKAXE_BLAME_COPY 02
58 #define PICKAXE_BLAME_COPY_HARDER 04
61 * blame for a blame_entry with score lower than these thresholds
62 * is not passed to the parent using move/copy logic.
64 static unsigned blame_move_score
;
65 static unsigned blame_copy_score
;
66 #define BLAME_DEFAULT_MOVE_SCORE 20
67 #define BLAME_DEFAULT_COPY_SCORE 40
69 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
70 #define METAINFO_SHOWN (1u<<12)
71 #define MORE_THAN_ONE_PATH (1u<<13)
74 * One blob in a commit that is being suspected
78 struct commit
*commit
;
80 unsigned char blob_sha1
[20];
81 char path
[FLEX_ARRAY
];
85 * Given an origin, prepare mmfile_t structure to be used by the
88 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
91 enum object_type type
;
93 file
->ptr
= read_sha1_file(o
->blob_sha1
, &type
,
94 (unsigned long *)(&(file
->size
)));
103 * Origin is refcounted and usually we keep the blob contents to be
106 static inline struct origin
*origin_incref(struct origin
*o
)
113 static void origin_decref(struct origin
*o
)
115 if (o
&& --o
->refcnt
<= 0) {
118 memset(o
, 0, sizeof(*o
));
124 * Each group of lines is described by a blame_entry; it can be split
125 * as we pass blame to the parents. They form a linked list in the
126 * scoreboard structure, sorted by the target line number.
129 struct blame_entry
*prev
;
130 struct blame_entry
*next
;
132 /* the first line of this group in the final image;
133 * internally all line numbers are 0 based.
137 /* how many lines this group has */
140 /* the commit that introduced this group into the final image */
141 struct origin
*suspect
;
143 /* true if the suspect is truly guilty; false while we have not
144 * checked if the group came from one of its parents.
148 /* the line number of the first line of this group in the
149 * suspect's file; internally all line numbers are 0 based.
153 /* how significant this entry is -- cached to avoid
154 * scanning the lines over and over.
160 * The current state of the blame assignment.
163 /* the final commit (i.e. where we started digging from) */
164 struct commit
*final
;
169 * The contents in the final image.
170 * Used by many functions to obtain contents of the nth line,
171 * indexed with scoreboard.lineno[blame_entry.lno].
173 const char *final_buf
;
174 unsigned long final_buf_size
;
176 /* linked list of blames */
177 struct blame_entry
*ent
;
179 /* look-up a line in the final buffer */
184 static inline int same_suspect(struct origin
*a
, struct origin
*b
)
188 if (a
->commit
!= b
->commit
)
190 return !strcmp(a
->path
, b
->path
);
193 static void sanity_check_refcnt(struct scoreboard
*);
196 * If two blame entries that are next to each other came from
197 * contiguous lines in the same origin (i.e. <commit, path> pair),
198 * merge them together.
200 static void coalesce(struct scoreboard
*sb
)
202 struct blame_entry
*ent
, *next
;
204 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
205 if (same_suspect(ent
->suspect
, next
->suspect
) &&
206 ent
->guilty
== next
->guilty
&&
207 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
208 ent
->num_lines
+= next
->num_lines
;
209 ent
->next
= next
->next
;
211 ent
->next
->prev
= ent
;
212 origin_decref(next
->suspect
);
215 next
= ent
; /* again */
219 if (DEBUG
) /* sanity */
220 sanity_check_refcnt(sb
);
224 * Given a commit and a path in it, create a new origin structure.
225 * The callers that add blame to the scoreboard should use
226 * get_origin() to obtain shared, refcounted copy instead of calling
227 * this function directly.
229 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
232 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
235 strcpy(o
->path
, path
);
240 * Locate an existing origin or create a new one.
242 static struct origin
*get_origin(struct scoreboard
*sb
,
243 struct commit
*commit
,
246 struct blame_entry
*e
;
248 for (e
= sb
->ent
; e
; e
= e
->next
) {
249 if (e
->suspect
->commit
== commit
&&
250 !strcmp(e
->suspect
->path
, path
))
251 return origin_incref(e
->suspect
);
253 return make_origin(commit
, path
);
257 * Fill the blob_sha1 field of an origin if it hasn't, so that later
258 * call to fill_origin_blob() can use it to locate the data. blob_sha1
259 * for an origin is also used to pass the blame for the entire file to
260 * the parent to detect the case where a child's blob is identical to
261 * that of its parent's.
263 static int fill_blob_sha1(struct origin
*origin
)
267 if (!is_null_sha1(origin
->blob_sha1
))
269 if (get_tree_entry(origin
->commit
->object
.sha1
,
271 origin
->blob_sha1
, &mode
))
273 if (sha1_object_info(origin
->blob_sha1
, NULL
) != OBJ_BLOB
)
277 hashclr(origin
->blob_sha1
);
282 * We have an origin -- check if the same path exists in the
283 * parent and return an origin structure to represent it.
285 static struct origin
*find_origin(struct scoreboard
*sb
,
286 struct commit
*parent
,
287 struct origin
*origin
)
289 struct origin
*porigin
= NULL
;
290 struct diff_options diff_opts
;
291 const char *paths
[2];
295 * Each commit object can cache one origin in that
296 * commit. This is a freestanding copy of origin and
299 struct origin
*cached
= parent
->util
;
300 if (!strcmp(cached
->path
, origin
->path
)) {
302 * The same path between origin and its parent
303 * without renaming -- the most common case.
305 porigin
= get_origin(sb
, parent
, cached
->path
);
308 * If the origin was newly created (i.e. get_origin
309 * would call make_origin if none is found in the
310 * scoreboard), it does not know the blob_sha1,
311 * so copy it. Otherwise porigin was in the
312 * scoreboard and already knows blob_sha1.
314 if (porigin
->refcnt
== 1)
315 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
318 /* otherwise it was not very useful; free it */
323 /* See if the origin->path is different between parent
324 * and origin first. Most of the time they are the
325 * same and diff-tree is fairly efficient about this.
327 diff_setup(&diff_opts
);
328 diff_opts
.recursive
= 1;
329 diff_opts
.detect_rename
= 0;
330 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
331 paths
[0] = origin
->path
;
334 diff_tree_setup_paths(paths
, &diff_opts
);
335 if (diff_setup_done(&diff_opts
) < 0)
338 if (is_null_sha1(origin
->commit
->object
.sha1
))
339 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
341 diff_tree_sha1(parent
->tree
->object
.sha1
,
342 origin
->commit
->tree
->object
.sha1
,
344 diffcore_std(&diff_opts
);
346 /* It is either one entry that says "modified", or "created",
349 if (!diff_queued_diff
.nr
) {
350 /* The path is the same as parent */
351 porigin
= get_origin(sb
, parent
, origin
->path
);
352 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
354 else if (diff_queued_diff
.nr
!= 1)
355 die("internal error in blame::find_origin");
357 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
360 die("internal error in blame::find_origin (%c)",
363 porigin
= get_origin(sb
, parent
, origin
->path
);
364 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
368 /* Did not exist in parent, or type changed */
372 diff_flush(&diff_opts
);
375 * Create a freestanding copy that is not part of
376 * the refcounted origin found in the scoreboard, and
377 * cache it in the commit.
379 struct origin
*cached
;
381 cached
= make_origin(porigin
->commit
, porigin
->path
);
382 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
383 parent
->util
= cached
;
389 * We have an origin -- find the path that corresponds to it in its
390 * parent and return an origin structure to represent it.
392 static struct origin
*find_rename(struct scoreboard
*sb
,
393 struct commit
*parent
,
394 struct origin
*origin
)
396 struct origin
*porigin
= NULL
;
397 struct diff_options diff_opts
;
399 const char *paths
[2];
401 diff_setup(&diff_opts
);
402 diff_opts
.recursive
= 1;
403 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
404 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
405 diff_opts
.single_follow
= origin
->path
;
407 diff_tree_setup_paths(paths
, &diff_opts
);
408 if (diff_setup_done(&diff_opts
) < 0)
411 if (is_null_sha1(origin
->commit
->object
.sha1
))
412 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
414 diff_tree_sha1(parent
->tree
->object
.sha1
,
415 origin
->commit
->tree
->object
.sha1
,
417 diffcore_std(&diff_opts
);
419 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
420 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
421 if ((p
->status
== 'R' || p
->status
== 'C') &&
422 !strcmp(p
->two
->path
, origin
->path
)) {
423 porigin
= get_origin(sb
, parent
, p
->one
->path
);
424 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
428 diff_flush(&diff_opts
);
433 * Parsing of patch chunks...
436 /* line number in postimage; up to but not including this
437 * line is the same as preimage
441 /* preimage line number after this chunk */
444 /* postimage line number after this chunk */
449 struct chunk
*chunks
;
453 struct blame_diff_state
{
454 struct xdiff_emit_state xm
;
456 unsigned hunk_post_context
;
457 unsigned hunk_in_pre_context
: 1;
460 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
462 struct blame_diff_state
*state
= state_
;
464 int off1
, off2
, len1
, len2
, num
;
466 num
= state
->ret
->num
;
467 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
468 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
469 state
->ret
->chunks
[num
- 1].same
++;
471 state
->hunk_in_pre_context
= 0;
473 state
->hunk_post_context
++;
475 state
->hunk_post_context
= 0;
480 if (num
&& state
->hunk_post_context
) {
481 chunk
= &state
->ret
->chunks
[num
- 1];
482 chunk
->p_next
-= state
->hunk_post_context
;
483 chunk
->t_next
-= state
->hunk_post_context
;
485 state
->ret
->num
= ++num
;
486 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
487 sizeof(struct chunk
) * num
);
488 chunk
= &state
->ret
->chunks
[num
- 1];
489 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
494 /* Line numbers in patch output are one based. */
498 chunk
->same
= len2
? off2
: (off2
+ 1);
500 chunk
->p_next
= off1
+ (len1
? len1
: 1);
501 chunk
->t_next
= chunk
->same
+ len2
;
502 state
->hunk_in_pre_context
= 1;
503 state
->hunk_post_context
= 0;
506 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
509 struct blame_diff_state state
;
514 xpp
.flags
= XDF_NEED_MINIMAL
;
515 xecfg
.ctxlen
= context
;
517 ecb
.outf
= xdiff_outf
;
519 memset(&state
, 0, sizeof(state
));
520 state
.xm
.consume
= process_u_diff
;
521 state
.ret
= xmalloc(sizeof(struct patch
));
522 state
.ret
->chunks
= NULL
;
525 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
527 if (state
.ret
->num
) {
529 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
530 chunk
->p_next
-= state
.hunk_post_context
;
531 chunk
->t_next
-= state
.hunk_post_context
;
537 * Run diff between two origins and grab the patch output, so that
538 * we can pass blame for lines origin is currently suspected for
541 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
543 mmfile_t file_p
, file_o
;
546 fill_origin_blob(parent
, &file_p
);
547 fill_origin_blob(origin
, &file_o
);
548 if (!file_p
.ptr
|| !file_o
.ptr
)
550 patch
= compare_buffer(&file_p
, &file_o
, 0);
555 static void free_patch(struct patch
*p
)
562 * Link in a new blame entry to the scoreboard. Entries that cover the
563 * same line range have been removed from the scoreboard previously.
565 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
567 struct blame_entry
*ent
, *prev
= NULL
;
569 origin_incref(e
->suspect
);
571 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
574 /* prev, if not NULL, is the last one that is below e */
577 e
->next
= prev
->next
;
589 * src typically is on-stack; we want to copy the information in it to
590 * an malloced blame_entry that is already on the linked list of the
591 * scoreboard. The origin of dst loses a refcnt while the origin of src
594 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
596 struct blame_entry
*p
, *n
;
600 origin_incref(src
->suspect
);
601 origin_decref(dst
->suspect
);
602 memcpy(dst
, src
, sizeof(*src
));
608 static const char *nth_line(struct scoreboard
*sb
, int lno
)
610 return sb
->final_buf
+ sb
->lineno
[lno
];
614 * It is known that lines between tlno to same came from parent, and e
615 * has an overlap with that range. it also is known that parent's
616 * line plno corresponds to e's line tlno.
622 * <------------------>
624 * Split e into potentially three parts; before this chunk, the chunk
625 * to be blamed for the parent, and after that portion.
627 static void split_overlap(struct blame_entry
*split
,
628 struct blame_entry
*e
,
629 int tlno
, int plno
, int same
,
630 struct origin
*parent
)
633 memset(split
, 0, sizeof(struct blame_entry
[3]));
635 if (e
->s_lno
< tlno
) {
636 /* there is a pre-chunk part not blamed on parent */
637 split
[0].suspect
= origin_incref(e
->suspect
);
638 split
[0].lno
= e
->lno
;
639 split
[0].s_lno
= e
->s_lno
;
640 split
[0].num_lines
= tlno
- e
->s_lno
;
641 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
642 split
[1].s_lno
= plno
;
645 split
[1].lno
= e
->lno
;
646 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
649 if (same
< e
->s_lno
+ e
->num_lines
) {
650 /* there is a post-chunk part not blamed on parent */
651 split
[2].suspect
= origin_incref(e
->suspect
);
652 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
653 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
654 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
655 chunk_end_lno
= split
[2].lno
;
658 chunk_end_lno
= e
->lno
+ e
->num_lines
;
659 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
662 * if it turns out there is nothing to blame the parent for,
663 * forget about the splitting. !split[1].suspect signals this.
665 if (split
[1].num_lines
< 1)
667 split
[1].suspect
= origin_incref(parent
);
671 * split_overlap() divided an existing blame e into up to three parts
672 * in split. Adjust the linked list of blames in the scoreboard to
675 static void split_blame(struct scoreboard
*sb
,
676 struct blame_entry
*split
,
677 struct blame_entry
*e
)
679 struct blame_entry
*new_entry
;
681 if (split
[0].suspect
&& split
[2].suspect
) {
682 /* The first part (reuse storage for the existing entry e) */
683 dup_entry(e
, &split
[0]);
685 /* The last part -- me */
686 new_entry
= xmalloc(sizeof(*new_entry
));
687 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
688 add_blame_entry(sb
, new_entry
);
690 /* ... and the middle part -- parent */
691 new_entry
= xmalloc(sizeof(*new_entry
));
692 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
693 add_blame_entry(sb
, new_entry
);
695 else if (!split
[0].suspect
&& !split
[2].suspect
)
697 * The parent covers the entire area; reuse storage for
698 * e and replace it with the parent.
700 dup_entry(e
, &split
[1]);
701 else if (split
[0].suspect
) {
702 /* me and then parent */
703 dup_entry(e
, &split
[0]);
705 new_entry
= xmalloc(sizeof(*new_entry
));
706 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
707 add_blame_entry(sb
, new_entry
);
710 /* parent and then me */
711 dup_entry(e
, &split
[1]);
713 new_entry
= xmalloc(sizeof(*new_entry
));
714 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
715 add_blame_entry(sb
, new_entry
);
718 if (DEBUG
) { /* sanity */
719 struct blame_entry
*ent
;
720 int lno
= sb
->ent
->lno
, corrupt
= 0;
722 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
727 lno
+= ent
->num_lines
;
731 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
732 printf("L %8d l %8d n %8d\n",
733 lno
, ent
->lno
, ent
->num_lines
);
734 lno
= ent
->lno
+ ent
->num_lines
;
742 * After splitting the blame, the origins used by the
743 * on-stack blame_entry should lose one refcnt each.
745 static void decref_split(struct blame_entry
*split
)
749 for (i
= 0; i
< 3; i
++)
750 origin_decref(split
[i
].suspect
);
754 * Helper for blame_chunk(). blame_entry e is known to overlap with
755 * the patch hunk; split it and pass blame to the parent.
757 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
758 int tlno
, int plno
, int same
,
759 struct origin
*parent
)
761 struct blame_entry split
[3];
763 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
764 if (split
[1].suspect
)
765 split_blame(sb
, split
, e
);
770 * Find the line number of the last line the target is suspected for.
772 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
774 struct blame_entry
*e
;
775 int last_in_target
= -1;
777 for (e
= sb
->ent
; e
; e
= e
->next
) {
778 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
780 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
781 last_in_target
= e
->s_lno
+ e
->num_lines
;
783 return last_in_target
;
787 * Process one hunk from the patch between the current suspect for
788 * blame_entry e and its parent. Find and split the overlap, and
789 * pass blame to the overlapping part to the parent.
791 static void blame_chunk(struct scoreboard
*sb
,
792 int tlno
, int plno
, int same
,
793 struct origin
*target
, struct origin
*parent
)
795 struct blame_entry
*e
;
797 for (e
= sb
->ent
; e
; e
= e
->next
) {
798 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
800 if (same
<= e
->s_lno
)
802 if (tlno
< e
->s_lno
+ e
->num_lines
)
803 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
808 * We are looking at the origin 'target' and aiming to pass blame
809 * for the lines it is suspected to its parent. Run diff to find
810 * which lines came from parent and pass blame for them.
812 static int pass_blame_to_parent(struct scoreboard
*sb
,
813 struct origin
*target
,
814 struct origin
*parent
)
816 int i
, last_in_target
, plno
, tlno
;
819 last_in_target
= find_last_in_target(sb
, target
);
820 if (last_in_target
< 0)
821 return 1; /* nothing remains for this target */
823 patch
= get_patch(parent
, target
);
825 for (i
= 0; i
< patch
->num
; i
++) {
826 struct chunk
*chunk
= &patch
->chunks
[i
];
828 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
829 plno
= chunk
->p_next
;
830 tlno
= chunk
->t_next
;
832 /* The rest (i.e. anything after tlno) are the same as the parent */
833 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
840 * The lines in blame_entry after splitting blames many times can become
841 * very small and trivial, and at some point it becomes pointless to
842 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
843 * ordinary C program, and it is not worth to say it was copied from
844 * totally unrelated file in the parent.
846 * Compute how trivial the lines in the blame_entry are.
848 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
857 cp
= nth_line(sb
, e
->lno
);
858 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
860 unsigned ch
= *((unsigned char *)cp
);
870 * best_so_far[] and this[] are both a split of an existing blame_entry
871 * that passes blame to the parent. Maintain best_so_far the best split
872 * so far, by comparing this and best_so_far and copying this into
873 * bst_so_far as needed.
875 static void copy_split_if_better(struct scoreboard
*sb
,
876 struct blame_entry
*best_so_far
,
877 struct blame_entry
*this)
881 if (!this[1].suspect
)
883 if (best_so_far
[1].suspect
) {
884 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
888 for (i
= 0; i
< 3; i
++)
889 origin_incref(this[i
].suspect
);
890 decref_split(best_so_far
);
891 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
895 * Find the lines from parent that are the same as ent so that
896 * we can pass blames to it. file_p has the blob contents for
899 static void find_copy_in_blob(struct scoreboard
*sb
,
900 struct blame_entry
*ent
,
901 struct origin
*parent
,
902 struct blame_entry
*split
,
912 * Prepare mmfile that contains only the lines in ent.
914 cp
= nth_line(sb
, ent
->lno
);
915 file_o
.ptr
= (char*) cp
;
916 cnt
= ent
->num_lines
;
918 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
922 file_o
.size
= cp
- file_o
.ptr
;
924 patch
= compare_buffer(file_p
, &file_o
, 1);
926 memset(split
, 0, sizeof(struct blame_entry
[3]));
928 for (i
= 0; i
< patch
->num
; i
++) {
929 struct chunk
*chunk
= &patch
->chunks
[i
];
931 /* tlno to chunk->same are the same as ent */
932 if (ent
->num_lines
<= tlno
)
934 if (tlno
< chunk
->same
) {
935 struct blame_entry
this[3];
936 split_overlap(this, ent
,
937 tlno
+ ent
->s_lno
, plno
,
938 chunk
->same
+ ent
->s_lno
,
940 copy_split_if_better(sb
, split
, this);
943 plno
= chunk
->p_next
;
944 tlno
= chunk
->t_next
;
950 * See if lines currently target is suspected for can be attributed to
953 static int find_move_in_parent(struct scoreboard
*sb
,
954 struct origin
*target
,
955 struct origin
*parent
)
957 int last_in_target
, made_progress
;
958 struct blame_entry
*e
, split
[3];
961 last_in_target
= find_last_in_target(sb
, target
);
962 if (last_in_target
< 0)
963 return 1; /* nothing remains for this target */
965 fill_origin_blob(parent
, &file_p
);
970 while (made_progress
) {
972 for (e
= sb
->ent
; e
; e
= e
->next
) {
973 if (e
->guilty
|| !same_suspect(e
->suspect
, target
))
975 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
976 if (split
[1].suspect
&&
977 blame_move_score
< ent_score(sb
, &split
[1])) {
978 split_blame(sb
, split
, e
);
988 struct blame_entry
*ent
;
989 struct blame_entry split
[3];
993 * Count the number of entries the target is suspected for,
994 * and prepare a list of entry and the best split.
996 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
997 struct origin
*target
,
1000 struct blame_entry
*e
;
1002 struct blame_list
*blame_list
= NULL
;
1004 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
1005 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1008 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1009 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1010 if (!e
->guilty
&& same_suspect(e
->suspect
, target
))
1011 blame_list
[i
++].ent
= e
;
1013 *num_ents_p
= num_ents
;
1018 * For lines target is suspected for, see if we can find code movement
1019 * across file boundary from the parent commit. porigin is the path
1020 * in the parent we already tried.
1022 static int find_copy_in_parent(struct scoreboard
*sb
,
1023 struct origin
*target
,
1024 struct commit
*parent
,
1025 struct origin
*porigin
,
1028 struct diff_options diff_opts
;
1029 const char *paths
[1];
1032 struct blame_list
*blame_list
;
1035 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1037 return 1; /* nothing remains for this target */
1039 diff_setup(&diff_opts
);
1040 diff_opts
.recursive
= 1;
1041 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1044 diff_tree_setup_paths(paths
, &diff_opts
);
1045 if (diff_setup_done(&diff_opts
) < 0)
1048 /* Try "find copies harder" on new path if requested;
1049 * we do not want to use diffcore_rename() actually to
1050 * match things up; find_copies_harder is set only to
1051 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1052 * and this code needs to be after diff_setup_done(), which
1053 * usually makes find-copies-harder imply copy detection.
1055 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
1056 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
1057 diff_opts
.find_copies_harder
= 1;
1059 if (is_null_sha1(target
->commit
->object
.sha1
))
1060 do_diff_cache(parent
->tree
->object
.sha1
, &diff_opts
);
1062 diff_tree_sha1(parent
->tree
->object
.sha1
,
1063 target
->commit
->tree
->object
.sha1
,
1066 if (!diff_opts
.find_copies_harder
)
1067 diffcore_std(&diff_opts
);
1071 int made_progress
= 0;
1073 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1074 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1075 struct origin
*norigin
;
1077 struct blame_entry
this[3];
1079 if (!DIFF_FILE_VALID(p
->one
))
1080 continue; /* does not exist in parent */
1081 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1082 /* find_move already dealt with this path */
1085 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1086 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1087 fill_origin_blob(norigin
, &file_p
);
1091 for (j
= 0; j
< num_ents
; j
++) {
1092 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1093 norigin
, this, &file_p
);
1094 copy_split_if_better(sb
, blame_list
[j
].split
,
1098 origin_decref(norigin
);
1101 for (j
= 0; j
< num_ents
; j
++) {
1102 struct blame_entry
*split
= blame_list
[j
].split
;
1103 if (split
[1].suspect
&&
1104 blame_copy_score
< ent_score(sb
, &split
[1])) {
1105 split_blame(sb
, split
, blame_list
[j
].ent
);
1108 decref_split(split
);
1114 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1120 diff_flush(&diff_opts
);
1126 * The blobs of origin and porigin exactly match, so everything
1127 * origin is suspected for can be blamed on the parent.
1129 static void pass_whole_blame(struct scoreboard
*sb
,
1130 struct origin
*origin
, struct origin
*porigin
)
1132 struct blame_entry
*e
;
1134 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1135 /* Steal its file */
1136 porigin
->file
= origin
->file
;
1137 origin
->file
.ptr
= NULL
;
1139 for (e
= sb
->ent
; e
; e
= e
->next
) {
1140 if (!same_suspect(e
->suspect
, origin
))
1142 origin_incref(porigin
);
1143 origin_decref(e
->suspect
);
1144 e
->suspect
= porigin
;
1148 #define MAXPARENT 16
1150 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1153 struct commit
*commit
= origin
->commit
;
1154 struct commit_list
*parent
;
1155 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1157 memset(parent_origin
, 0, sizeof(parent_origin
));
1159 /* The first pass looks for unrenamed path to optimize for
1160 * common cases, then we look for renames in the second pass.
1162 for (pass
= 0; pass
< 2; pass
++) {
1163 struct origin
*(*find
)(struct scoreboard
*,
1164 struct commit
*, struct origin
*);
1165 find
= pass
? find_rename
: find_origin
;
1167 for (i
= 0, parent
= commit
->parents
;
1168 i
< MAXPARENT
&& parent
;
1169 parent
= parent
->next
, i
++) {
1170 struct commit
*p
= parent
->item
;
1173 if (parent_origin
[i
])
1175 if (parse_commit(p
))
1177 porigin
= find(sb
, p
, origin
);
1180 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1181 pass_whole_blame(sb
, origin
, porigin
);
1182 origin_decref(porigin
);
1185 for (j
= same
= 0; j
< i
; j
++)
1186 if (parent_origin
[j
] &&
1187 !hashcmp(parent_origin
[j
]->blob_sha1
,
1188 porigin
->blob_sha1
)) {
1193 parent_origin
[i
] = porigin
;
1195 origin_decref(porigin
);
1200 for (i
= 0, parent
= commit
->parents
;
1201 i
< MAXPARENT
&& parent
;
1202 parent
= parent
->next
, i
++) {
1203 struct origin
*porigin
= parent_origin
[i
];
1206 if (pass_blame_to_parent(sb
, origin
, porigin
))
1211 * Optionally find moves in parents' files.
1213 if (opt
& PICKAXE_BLAME_MOVE
)
1214 for (i
= 0, parent
= commit
->parents
;
1215 i
< MAXPARENT
&& parent
;
1216 parent
= parent
->next
, i
++) {
1217 struct origin
*porigin
= parent_origin
[i
];
1220 if (find_move_in_parent(sb
, origin
, porigin
))
1225 * Optionally find copies from parents' files.
1227 if (opt
& PICKAXE_BLAME_COPY
)
1228 for (i
= 0, parent
= commit
->parents
;
1229 i
< MAXPARENT
&& parent
;
1230 parent
= parent
->next
, i
++) {
1231 struct origin
*porigin
= parent_origin
[i
];
1232 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1238 for (i
= 0; i
< MAXPARENT
; i
++)
1239 origin_decref(parent_origin
[i
]);
1243 * Information on commits, used for output.
1248 const char *author_mail
;
1249 unsigned long author_time
;
1250 const char *author_tz
;
1252 /* filled only when asked for details */
1253 const char *committer
;
1254 const char *committer_mail
;
1255 unsigned long committer_time
;
1256 const char *committer_tz
;
1258 const char *summary
;
1262 * Parse author/committer line in the commit object buffer
1264 static void get_ac_line(const char *inbuf
, const char *what
,
1265 int bufsz
, char *person
, const char **mail
,
1266 unsigned long *time
, const char **tz
)
1271 tmp
= strstr(inbuf
, what
);
1274 tmp
+= strlen(what
);
1275 endp
= strchr(tmp
, '\n');
1283 *mail
= *tz
= "(unknown)";
1287 memcpy(person
, tmp
, len
);
1299 *time
= strtoul(tmp
, NULL
, 10);
1308 static void get_commit_info(struct commit
*commit
,
1309 struct commit_info
*ret
,
1314 static char author_buf
[1024];
1315 static char committer_buf
[1024];
1316 static char summary_buf
[1024];
1319 * We've operated without save_commit_buffer, so
1320 * we now need to populate them for output.
1322 if (!commit
->buffer
) {
1323 enum object_type type
;
1326 read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1328 ret
->author
= author_buf
;
1329 get_ac_line(commit
->buffer
, "\nauthor ",
1330 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1331 &ret
->author_time
, &ret
->author_tz
);
1336 ret
->committer
= committer_buf
;
1337 get_ac_line(commit
->buffer
, "\ncommitter ",
1338 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1339 &ret
->committer_time
, &ret
->committer_tz
);
1341 ret
->summary
= summary_buf
;
1342 tmp
= strstr(commit
->buffer
, "\n\n");
1345 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1349 endp
= strchr(tmp
, '\n');
1351 endp
= tmp
+ strlen(tmp
);
1353 if (len
>= sizeof(summary_buf
) || len
== 0)
1355 memcpy(summary_buf
, tmp
, len
);
1356 summary_buf
[len
] = 0;
1360 * To allow LF and other nonportable characters in pathnames,
1361 * they are c-style quoted as needed.
1363 static void write_filename_info(const char *path
)
1365 printf("filename ");
1366 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1371 * The blame_entry is found to be guilty for the range. Mark it
1372 * as such, and show it in incremental output.
1374 static void found_guilty_entry(struct blame_entry
*ent
)
1380 struct origin
*suspect
= ent
->suspect
;
1382 printf("%s %d %d %d\n",
1383 sha1_to_hex(suspect
->commit
->object
.sha1
),
1384 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1385 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1386 struct commit_info ci
;
1387 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1388 get_commit_info(suspect
->commit
, &ci
, 1);
1389 printf("author %s\n", ci
.author
);
1390 printf("author-mail %s\n", ci
.author_mail
);
1391 printf("author-time %lu\n", ci
.author_time
);
1392 printf("author-tz %s\n", ci
.author_tz
);
1393 printf("committer %s\n", ci
.committer
);
1394 printf("committer-mail %s\n", ci
.committer_mail
);
1395 printf("committer-time %lu\n", ci
.committer_time
);
1396 printf("committer-tz %s\n", ci
.committer_tz
);
1397 printf("summary %s\n", ci
.summary
);
1398 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1399 printf("boundary\n");
1401 write_filename_info(suspect
->path
);
1406 * The main loop -- while the scoreboard has lines whose true origin
1407 * is still unknown, pick one blame_entry, and allow its current
1408 * suspect to pass blames to its parents.
1410 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1413 struct blame_entry
*ent
;
1414 struct commit
*commit
;
1415 struct origin
*suspect
= NULL
;
1417 /* find one suspect to break down */
1418 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1420 suspect
= ent
->suspect
;
1422 return; /* all done */
1425 * We will use this suspect later in the loop,
1426 * so hold onto it in the meantime.
1428 origin_incref(suspect
);
1429 commit
= suspect
->commit
;
1430 if (!commit
->object
.parsed
)
1431 parse_commit(commit
);
1432 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1433 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1434 pass_blame(sb
, suspect
, opt
);
1436 commit
->object
.flags
|= UNINTERESTING
;
1437 if (commit
->object
.parsed
)
1438 mark_parents_uninteresting(commit
);
1440 /* treat root commit as boundary */
1441 if (!commit
->parents
&& !show_root
)
1442 commit
->object
.flags
|= UNINTERESTING
;
1444 /* Take responsibility for the remaining entries */
1445 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1446 if (same_suspect(ent
->suspect
, suspect
))
1447 found_guilty_entry(ent
);
1448 origin_decref(suspect
);
1450 if (DEBUG
) /* sanity */
1451 sanity_check_refcnt(sb
);
1455 static const char *format_time(unsigned long time
, const char *tz_str
,
1458 static char time_buf
[128];
1463 if (show_raw_time
) {
1464 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1469 minutes
= tz
< 0 ? -tz
: tz
;
1470 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1471 minutes
= tz
< 0 ? -minutes
: minutes
;
1472 t
= time
+ minutes
* 60;
1475 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1476 strcat(time_buf
, tz_str
);
1480 #define OUTPUT_ANNOTATE_COMPAT 001
1481 #define OUTPUT_LONG_OBJECT_NAME 002
1482 #define OUTPUT_RAW_TIMESTAMP 004
1483 #define OUTPUT_PORCELAIN 010
1484 #define OUTPUT_SHOW_NAME 020
1485 #define OUTPUT_SHOW_NUMBER 040
1486 #define OUTPUT_SHOW_SCORE 0100
1487 #define OUTPUT_NO_AUTHOR 0200
1489 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1493 struct origin
*suspect
= ent
->suspect
;
1496 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1497 printf("%s%c%d %d %d\n",
1499 ent
->guilty
? ' ' : '*', // purely for debugging
1503 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1504 struct commit_info ci
;
1505 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1506 get_commit_info(suspect
->commit
, &ci
, 1);
1507 printf("author %s\n", ci
.author
);
1508 printf("author-mail %s\n", ci
.author_mail
);
1509 printf("author-time %lu\n", ci
.author_time
);
1510 printf("author-tz %s\n", ci
.author_tz
);
1511 printf("committer %s\n", ci
.committer
);
1512 printf("committer-mail %s\n", ci
.committer_mail
);
1513 printf("committer-time %lu\n", ci
.committer_time
);
1514 printf("committer-tz %s\n", ci
.committer_tz
);
1515 write_filename_info(suspect
->path
);
1516 printf("summary %s\n", ci
.summary
);
1517 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1518 printf("boundary\n");
1520 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1521 write_filename_info(suspect
->path
);
1523 cp
= nth_line(sb
, ent
->lno
);
1524 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1527 printf("%s %d %d\n", hex
,
1528 ent
->s_lno
+ 1 + cnt
,
1529 ent
->lno
+ 1 + cnt
);
1534 } while (ch
!= '\n' &&
1535 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1539 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1543 struct origin
*suspect
= ent
->suspect
;
1544 struct commit_info ci
;
1546 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1548 get_commit_info(suspect
->commit
, &ci
, 1);
1549 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1551 cp
= nth_line(sb
, ent
->lno
);
1552 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1554 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1556 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1558 memset(hex
, ' ', length
);
1559 else if (!cmd_is_annotate
) {
1565 printf("%.*s", length
, hex
);
1566 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1567 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1568 format_time(ci
.author_time
, ci
.author_tz
,
1570 ent
->lno
+ 1 + cnt
);
1572 if (opt
& OUTPUT_SHOW_SCORE
)
1574 max_score_digits
, ent
->score
,
1575 ent
->suspect
->refcnt
);
1576 if (opt
& OUTPUT_SHOW_NAME
)
1577 printf(" %-*.*s", longest_file
, longest_file
,
1579 if (opt
& OUTPUT_SHOW_NUMBER
)
1580 printf(" %*d", max_orig_digits
,
1581 ent
->s_lno
+ 1 + cnt
);
1583 if (!(opt
& OUTPUT_NO_AUTHOR
))
1584 printf(" (%-*.*s %10s",
1585 longest_author
, longest_author
,
1587 format_time(ci
.author_time
,
1591 max_digits
, ent
->lno
+ 1 + cnt
);
1596 } while (ch
!= '\n' &&
1597 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1601 static void output(struct scoreboard
*sb
, int option
)
1603 struct blame_entry
*ent
;
1605 if (option
& OUTPUT_PORCELAIN
) {
1606 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1607 struct blame_entry
*oth
;
1608 struct origin
*suspect
= ent
->suspect
;
1609 struct commit
*commit
= suspect
->commit
;
1610 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1612 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1613 if ((oth
->suspect
->commit
!= commit
) ||
1614 !strcmp(oth
->suspect
->path
, suspect
->path
))
1616 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1622 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1623 if (option
& OUTPUT_PORCELAIN
)
1624 emit_porcelain(sb
, ent
);
1626 emit_other(sb
, ent
, option
);
1632 * To allow quick access to the contents of nth line in the
1633 * final image, prepare an index in the scoreboard.
1635 static int prepare_lines(struct scoreboard
*sb
)
1637 const char *buf
= sb
->final_buf
;
1638 unsigned long len
= sb
->final_buf_size
;
1639 int num
= 0, incomplete
= 0, bol
= 1;
1641 if (len
&& buf
[len
-1] != '\n')
1642 incomplete
++; /* incomplete line at the end */
1645 sb
->lineno
= xrealloc(sb
->lineno
,
1646 sizeof(int* ) * (num
+ 1));
1647 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1650 if (*buf
++ == '\n') {
1655 sb
->lineno
= xrealloc(sb
->lineno
,
1656 sizeof(int* ) * (num
+ incomplete
+ 1));
1657 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1658 sb
->num_lines
= num
+ incomplete
;
1659 return sb
->num_lines
;
1663 * Add phony grafts for use with -S; this is primarily to
1664 * support git-cvsserver that wants to give a linear history
1667 static int read_ancestry(const char *graft_file
)
1669 FILE *fp
= fopen(graft_file
, "r");
1673 while (fgets(buf
, sizeof(buf
), fp
)) {
1674 /* The format is just "Commit Parent1 Parent2 ...\n" */
1675 int len
= strlen(buf
);
1676 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1678 register_commit_graft(graft
, 0);
1685 * How many columns do we need to show line numbers in decimal?
1687 static int lineno_width(int lines
)
1691 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1697 * How many columns do we need to show line numbers, authors,
1700 static void find_alignment(struct scoreboard
*sb
, int *option
)
1702 int longest_src_lines
= 0;
1703 int longest_dst_lines
= 0;
1704 unsigned largest_score
= 0;
1705 struct blame_entry
*e
;
1707 for (e
= sb
->ent
; e
; e
= e
->next
) {
1708 struct origin
*suspect
= e
->suspect
;
1709 struct commit_info ci
;
1712 if (strcmp(suspect
->path
, sb
->path
))
1713 *option
|= OUTPUT_SHOW_NAME
;
1714 num
= strlen(suspect
->path
);
1715 if (longest_file
< num
)
1717 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1718 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1719 get_commit_info(suspect
->commit
, &ci
, 1);
1720 num
= strlen(ci
.author
);
1721 if (longest_author
< num
)
1722 longest_author
= num
;
1724 num
= e
->s_lno
+ e
->num_lines
;
1725 if (longest_src_lines
< num
)
1726 longest_src_lines
= num
;
1727 num
= e
->lno
+ e
->num_lines
;
1728 if (longest_dst_lines
< num
)
1729 longest_dst_lines
= num
;
1730 if (largest_score
< ent_score(sb
, e
))
1731 largest_score
= ent_score(sb
, e
);
1733 max_orig_digits
= lineno_width(longest_src_lines
);
1734 max_digits
= lineno_width(longest_dst_lines
);
1735 max_score_digits
= lineno_width(largest_score
);
1739 * For debugging -- origin is refcounted, and this asserts that
1740 * we do not underflow.
1742 static void sanity_check_refcnt(struct scoreboard
*sb
)
1745 struct blame_entry
*ent
;
1747 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1748 /* Nobody should have zero or negative refcnt */
1749 if (ent
->suspect
->refcnt
<= 0) {
1750 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1752 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1753 ent
->suspect
->refcnt
);
1757 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1758 /* Mark the ones that haven't been checked */
1759 if (0 < ent
->suspect
->refcnt
)
1760 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1762 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1764 * ... then pick each and see if they have the the
1768 struct blame_entry
*e
;
1769 struct origin
*suspect
= ent
->suspect
;
1771 if (0 < suspect
->refcnt
)
1773 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1774 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1775 if (e
->suspect
!= suspect
)
1779 if (suspect
->refcnt
!= found
) {
1780 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1782 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1783 ent
->suspect
->refcnt
, found
);
1789 find_alignment(sb
, &opt
);
1791 die("Baa %d!", baa
);
1796 * Used for the command line parsing; check if the path exists
1797 * in the working tree.
1799 static int has_path_in_work_tree(const char *path
)
1802 return !lstat(path
, &st
);
1805 static unsigned parse_score(const char *arg
)
1808 unsigned long score
= strtoul(arg
, &end
, 10);
1814 static const char *add_prefix(const char *prefix
, const char *path
)
1816 if (!prefix
|| !prefix
[0])
1818 return prefix_path(prefix
, strlen(prefix
), path
);
1822 * Parsing of (comma separated) one item in the -L option
1824 static const char *parse_loc(const char *spec
,
1825 struct scoreboard
*sb
, long lno
,
1826 long begin
, long *ret
)
1833 regmatch_t match
[1];
1835 /* Allow "-L <something>,+20" to mean starting at <something>
1836 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1839 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1840 num
= strtol(spec
+ 1, &term
, 10);
1841 if (term
!= spec
+ 1) {
1845 *ret
= begin
+ num
- 2;
1854 num
= strtol(spec
, &term
, 10);
1862 /* it could be a regexp of form /.../ */
1863 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1870 /* try [spec+1 .. term-1] as regexp */
1872 begin
--; /* input is in human terms */
1873 line
= nth_line(sb
, begin
);
1875 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1876 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1877 const char *cp
= line
+ match
[0].rm_so
;
1880 while (begin
++ < lno
) {
1881 nline
= nth_line(sb
, begin
);
1882 if (line
<= cp
&& cp
< nline
)
1893 regerror(reg_error
, ®exp
, errbuf
, 1024);
1894 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1899 * Parsing of -L option
1901 static void prepare_blame_range(struct scoreboard
*sb
,
1902 const char *bottomtop
,
1904 long *bottom
, long *top
)
1908 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1910 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1918 static int git_blame_config(const char *var
, const char *value
)
1920 if (!strcmp(var
, "blame.showroot")) {
1921 show_root
= git_config_bool(var
, value
);
1924 if (!strcmp(var
, "blame.blankboundary")) {
1925 blank_boundary
= git_config_bool(var
, value
);
1928 return git_default_config(var
, value
);
1931 static struct commit
*fake_working_tree_commit(const char *path
, const char *contents_from
)
1933 struct commit
*commit
;
1934 struct origin
*origin
;
1935 unsigned char head_sha1
[20];
1940 unsigned long fin_size
;
1942 struct cache_entry
*ce
;
1945 if (get_sha1("HEAD", head_sha1
))
1946 die("No such ref: HEAD");
1949 commit
= xcalloc(1, sizeof(*commit
));
1950 commit
->parents
= xcalloc(1, sizeof(*commit
->parents
));
1951 commit
->parents
->item
= lookup_commit_reference(head_sha1
);
1952 commit
->object
.parsed
= 1;
1954 commit
->object
.type
= OBJ_COMMIT
;
1956 origin
= make_origin(commit
, path
);
1958 if (!contents_from
|| strcmp("-", contents_from
)) {
1960 const char *read_from
;
1962 if (contents_from
) {
1963 if (stat(contents_from
, &st
) < 0)
1964 die("Cannot stat %s", contents_from
);
1965 read_from
= contents_from
;
1968 if (lstat(path
, &st
) < 0)
1969 die("Cannot lstat %s", path
);
1972 fin_size
= xsize_t(st
.st_size
);
1973 buf
= xmalloc(fin_size
+1);
1974 mode
= canon_mode(st
.st_mode
);
1975 switch (st
.st_mode
& S_IFMT
) {
1977 fd
= open(read_from
, O_RDONLY
);
1979 die("cannot open %s", read_from
);
1980 if (read_in_full(fd
, buf
, fin_size
) != fin_size
)
1981 die("cannot read %s", read_from
);
1984 if (readlink(read_from
, buf
, fin_size
+1) != fin_size
)
1985 die("cannot readlink %s", read_from
);
1988 die("unsupported file type %s", read_from
);
1992 /* Reading from stdin */
1993 contents_from
= "standard input";
1999 buf
= xrealloc(buf
, fin_size
+ cnt
);
2000 cnt
= xread(0, buf
+ fin_size
, cnt
);
2002 die("read error %s from stdin",
2008 buf
= xrealloc(buf
, fin_size
+ 1);
2011 origin
->file
.ptr
= buf
;
2012 origin
->file
.size
= fin_size
;
2013 pretend_sha1_file(buf
, fin_size
, OBJ_BLOB
, origin
->blob_sha1
);
2014 commit
->util
= origin
;
2017 * Read the current index, replace the path entry with
2018 * origin->blob_sha1 without mucking with its mode or type
2019 * bits; we are not going to write this index out -- we just
2020 * want to run "diff-index --cached".
2027 int pos
= cache_name_pos(path
, len
);
2029 mode
= ntohl(active_cache
[pos
]->ce_mode
);
2031 /* Let's not bother reading from HEAD tree */
2032 mode
= S_IFREG
| 0644;
2034 size
= cache_entry_size(len
);
2035 ce
= xcalloc(1, size
);
2036 hashcpy(ce
->sha1
, origin
->blob_sha1
);
2037 memcpy(ce
->name
, path
, len
);
2038 ce
->ce_flags
= create_ce_flags(len
, 0);
2039 ce
->ce_mode
= create_ce_mode(mode
);
2040 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
2043 * We are not going to write this out, so this does not matter
2044 * right now, but someday we might optimize diff-index --cached
2045 * with cache-tree information.
2047 cache_tree_invalidate_path(active_cache_tree
, path
);
2049 commit
->buffer
= xmalloc(400);
2050 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
2051 snprintf(commit
->buffer
, 400,
2052 "tree 0000000000000000000000000000000000000000\n"
2056 "Version of %s from %s\n",
2057 sha1_to_hex(head_sha1
),
2058 ident
, ident
, path
, contents_from
? contents_from
: path
);
2062 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
2064 struct rev_info revs
;
2066 struct scoreboard sb
;
2068 struct blame_entry
*ent
;
2069 int i
, seen_dashdash
, unk
, opt
;
2070 long bottom
, top
, lno
;
2071 int output_option
= 0;
2073 const char *revs_file
= NULL
;
2074 const char *final_commit_name
= NULL
;
2075 enum object_type type
;
2076 const char *bottomtop
= NULL
;
2077 const char *contents_from
= NULL
;
2079 cmd_is_annotate
= !strcmp(argv
[0], "annotate");
2081 git_config(git_blame_config
);
2082 save_commit_buffer
= 0;
2086 for (unk
= i
= 1; i
< argc
; i
++) {
2087 const char *arg
= argv
[i
];
2090 else if (!strcmp("-b", arg
))
2092 else if (!strcmp("--root", arg
))
2094 else if (!strcmp(arg
, "--show-stats"))
2096 else if (!strcmp("-c", arg
))
2097 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
2098 else if (!strcmp("-t", arg
))
2099 output_option
|= OUTPUT_RAW_TIMESTAMP
;
2100 else if (!strcmp("-l", arg
))
2101 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
2102 else if (!strcmp("-s", arg
))
2103 output_option
|= OUTPUT_NO_AUTHOR
;
2104 else if (!strcmp("-S", arg
) && ++i
< argc
)
2105 revs_file
= argv
[i
];
2106 else if (!prefixcmp(arg
, "-M")) {
2107 opt
|= PICKAXE_BLAME_MOVE
;
2108 blame_move_score
= parse_score(arg
+2);
2110 else if (!prefixcmp(arg
, "-C")) {
2111 if (opt
& PICKAXE_BLAME_COPY
)
2112 opt
|= PICKAXE_BLAME_COPY_HARDER
;
2113 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
2114 blame_copy_score
= parse_score(arg
+2);
2116 else if (!prefixcmp(arg
, "-L")) {
2125 die("More than one '-L n,m' option given");
2128 else if (!strcmp("--contents", arg
)) {
2131 contents_from
= argv
[i
];
2133 else if (!strcmp("--incremental", arg
))
2135 else if (!strcmp("--score-debug", arg
))
2136 output_option
|= OUTPUT_SHOW_SCORE
;
2137 else if (!strcmp("-f", arg
) ||
2138 !strcmp("--show-name", arg
))
2139 output_option
|= OUTPUT_SHOW_NAME
;
2140 else if (!strcmp("-n", arg
) ||
2141 !strcmp("--show-number", arg
))
2142 output_option
|= OUTPUT_SHOW_NUMBER
;
2143 else if (!strcmp("-p", arg
) ||
2144 !strcmp("--porcelain", arg
))
2145 output_option
|= OUTPUT_PORCELAIN
;
2146 else if (!strcmp("--", arg
)) {
2158 if (!blame_move_score
)
2159 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2160 if (!blame_copy_score
)
2161 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2164 * We have collected options unknown to us in argv[1..unk]
2165 * which are to be passed to revision machinery if we are
2166 * going to do the "bottom" processing.
2168 * The remaining are:
2170 * (1) if seen_dashdash, its either
2171 * "-options -- <path>" or
2172 * "-options -- <path> <rev>".
2173 * but the latter is allowed only if there is no
2174 * options that we passed to revision machinery.
2176 * (2) otherwise, we may have "--" somewhere later and
2177 * might be looking at the first one of multiple 'rev'
2178 * parameters (e.g. " master ^next ^maint -- path").
2179 * See if there is a dashdash first, and give the
2180 * arguments before that to revision machinery.
2181 * After that there must be one 'path'.
2183 * (3) otherwise, its one of the three:
2184 * "-options <path> <rev>"
2185 * "-options <rev> <path>"
2187 * but again the first one is allowed only if
2188 * there is no options that we passed to revision
2192 if (seen_dashdash
) {
2196 path
= add_prefix(prefix
, argv
[i
]);
2197 if (i
+ 1 == argc
- 1) {
2200 argv
[unk
++] = argv
[i
+ 1];
2202 else if (i
+ 1 != argc
)
2203 /* garbage at end */
2208 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2209 if (!strcmp(argv
[j
], "--"))
2211 if (seen_dashdash
) {
2213 if (seen_dashdash
+ 1 != argc
- 1)
2215 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2216 for (j
= i
; j
< seen_dashdash
; j
++)
2217 argv
[unk
++] = argv
[j
];
2223 path
= add_prefix(prefix
, argv
[i
]);
2224 if (i
+ 1 == argc
- 1) {
2225 final_commit_name
= argv
[i
+ 1];
2227 /* if (unk == 1) we could be getting
2230 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2231 path
= add_prefix(prefix
, argv
[i
+ 1]);
2232 final_commit_name
= argv
[i
];
2235 else if (i
!= argc
- 1)
2236 usage(blame_usage
); /* garbage at end */
2238 if (!has_path_in_work_tree(path
))
2239 die("cannot stat path %s: %s",
2240 path
, strerror(errno
));
2244 if (final_commit_name
)
2245 argv
[unk
++] = final_commit_name
;
2248 * Now we got rev and path. We do not want the path pruning
2249 * but we may want "bottom" processing.
2251 argv
[unk
++] = "--"; /* terminate the rev name */
2254 init_revisions(&revs
, NULL
);
2255 setup_revisions(unk
, argv
, &revs
, NULL
);
2256 memset(&sb
, 0, sizeof(sb
));
2259 * There must be one and only one positive commit in the
2260 * revs->pending array.
2262 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2263 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2264 if (obj
->flags
& UNINTERESTING
)
2266 while (obj
->type
== OBJ_TAG
)
2267 obj
= deref_tag(obj
, NULL
, 0);
2268 if (obj
->type
!= OBJ_COMMIT
)
2269 die("Non commit %s?",
2270 revs
.pending
.objects
[i
].name
);
2272 die("More than one commit to dig from %s and %s?",
2273 revs
.pending
.objects
[i
].name
,
2275 sb
.final
= (struct commit
*) obj
;
2276 final_commit_name
= revs
.pending
.objects
[i
].name
;
2281 * "--not A B -- path" without anything positive;
2282 * do not default to HEAD, but use the working tree
2285 sb
.final
= fake_working_tree_commit(path
, contents_from
);
2286 add_pending_object(&revs
, &(sb
.final
->object
), ":");
2288 else if (contents_from
)
2289 die("Cannot use --contents with final commit object name");
2292 * If we have bottom, this will mark the ancestors of the
2293 * bottom commits we would reach while traversing as
2296 prepare_revision_walk(&revs
);
2298 if (is_null_sha1(sb
.final
->object
.sha1
)) {
2301 buf
= xmalloc(o
->file
.size
+ 1);
2302 memcpy(buf
, o
->file
.ptr
, o
->file
.size
+ 1);
2304 sb
.final_buf_size
= o
->file
.size
;
2307 o
= get_origin(&sb
, sb
.final
, path
);
2308 if (fill_blob_sha1(o
))
2309 die("no such path %s in %s", path
, final_commit_name
);
2311 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, &type
,
2312 &sb
.final_buf_size
);
2315 lno
= prepare_lines(&sb
);
2319 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2320 if (bottom
&& top
&& top
< bottom
) {
2322 tmp
= top
; top
= bottom
; bottom
= tmp
;
2330 die("file %s has only %lu lines", path
, lno
);
2332 ent
= xcalloc(1, sizeof(*ent
));
2334 ent
->num_lines
= top
- bottom
;
2336 ent
->s_lno
= bottom
;
2341 if (revs_file
&& read_ancestry(revs_file
))
2342 die("reading graft file %s failed: %s",
2343 revs_file
, strerror(errno
));
2345 assign_blame(&sb
, &revs
, opt
);
2352 if (!(output_option
& OUTPUT_PORCELAIN
))
2353 find_alignment(&sb
, &output_option
);
2355 output(&sb
, output_option
);
2356 free((void *)sb
.final_buf
);
2357 for (ent
= sb
.ent
; ent
; ) {
2358 struct blame_entry
*e
= ent
->next
;
2364 printf("num read blob: %d\n", num_read_blob
);
2365 printf("num get patch: %d\n", num_get_patch
);
2366 printf("num commits: %d\n", num_commits
);