4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
19 static char blame_usage
[] =
20 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
21 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
22 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
23 " -l, --long Show long commit SHA1 (Default: off)\n"
24 " --root Do not treat root commits as boundaries (Default: off)\n"
25 " -t, --time Show raw timestamp (Default: off)\n"
26 " -f, --show-name Show original filename (Default: auto)\n"
27 " -n, --show-number Show original linenumber (Default: off)\n"
28 " -p, --porcelain Show in a format designed for machine consumption\n"
29 " -L n,m Process only line range n,m, counting from 1\n"
30 " -M, -C Find line movements within and across files\n"
31 " --incremental Show blame entries as we find them, incrementally\n"
32 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
34 static int longest_file
;
35 static int longest_author
;
36 static int max_orig_digits
;
37 static int max_digits
;
38 static int max_score_digits
;
40 static int blank_boundary
;
41 static int incremental
;
48 static int num_read_blob
;
49 static int num_get_patch
;
50 static int num_commits
;
52 #define PICKAXE_BLAME_MOVE 01
53 #define PICKAXE_BLAME_COPY 02
54 #define PICKAXE_BLAME_COPY_HARDER 04
57 * blame for a blame_entry with score lower than these thresholds
58 * is not passed to the parent using move/copy logic.
60 static unsigned blame_move_score
;
61 static unsigned blame_copy_score
;
62 #define BLAME_DEFAULT_MOVE_SCORE 20
63 #define BLAME_DEFAULT_COPY_SCORE 40
65 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
66 #define METAINFO_SHOWN (1u<<12)
67 #define MORE_THAN_ONE_PATH (1u<<13)
70 * One blob in a commit that is being suspected
74 struct commit
*commit
;
76 unsigned char blob_sha1
[20];
77 char path
[FLEX_ARRAY
];
80 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
85 file
->ptr
= read_sha1_file(o
->blob_sha1
, type
,
86 (unsigned long *)(&(file
->size
)));
94 static inline struct origin
*origin_incref(struct origin
*o
)
101 static void origin_decref(struct origin
*o
)
103 if (o
&& --o
->refcnt
<= 0) {
106 memset(o
, 0, sizeof(*o
));
112 struct blame_entry
*prev
;
113 struct blame_entry
*next
;
115 /* the first line of this group in the final image;
116 * internally all line numbers are 0 based.
120 /* how many lines this group has */
123 /* the commit that introduced this group into the final image */
124 struct origin
*suspect
;
126 /* true if the suspect is truly guilty; false while we have not
127 * checked if the group came from one of its parents.
131 /* the line number of the first line of this group in the
132 * suspect's file; internally all line numbers are 0 based.
136 /* how significant this entry is -- cached to avoid
137 * scanning the lines over and over
143 /* the final commit (i.e. where we started digging from) */
144 struct commit
*final
;
148 /* the contents in the final; pointed into by buf pointers of
151 const char *final_buf
;
152 unsigned long final_buf_size
;
154 /* linked list of blames */
155 struct blame_entry
*ent
;
157 /* look-up a line in the final buffer */
162 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
164 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
167 return strcmp(a
->path
, b
->path
);
170 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
172 static void sanity_check_refcnt(struct scoreboard
*);
174 static void coalesce(struct scoreboard
*sb
)
176 struct blame_entry
*ent
, *next
;
178 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
179 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
180 ent
->guilty
== next
->guilty
&&
181 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
182 ent
->num_lines
+= next
->num_lines
;
183 ent
->next
= next
->next
;
185 ent
->next
->prev
= ent
;
186 origin_decref(next
->suspect
);
189 next
= ent
; /* again */
193 if (DEBUG
) /* sanity */
194 sanity_check_refcnt(sb
);
197 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
200 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
203 strcpy(o
->path
, path
);
207 static struct origin
*get_origin(struct scoreboard
*sb
,
208 struct commit
*commit
,
211 struct blame_entry
*e
;
213 for (e
= sb
->ent
; e
; e
= e
->next
) {
214 if (e
->suspect
->commit
== commit
&&
215 !strcmp(e
->suspect
->path
, path
))
216 return origin_incref(e
->suspect
);
218 return make_origin(commit
, path
);
221 static int fill_blob_sha1(struct origin
*origin
)
226 if (!is_null_sha1(origin
->blob_sha1
))
228 if (get_tree_entry(origin
->commit
->object
.sha1
,
230 origin
->blob_sha1
, &mode
))
232 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
233 strcmp(type
, blob_type
))
237 hashclr(origin
->blob_sha1
);
241 static struct origin
*find_origin(struct scoreboard
*sb
,
242 struct commit
*parent
,
243 struct origin
*origin
)
245 struct origin
*porigin
= NULL
;
246 struct diff_options diff_opts
;
247 const char *paths
[2];
250 /* This is a freestanding copy of origin and not
253 struct origin
*cached
= parent
->util
;
254 if (!strcmp(cached
->path
, origin
->path
)) {
255 porigin
= get_origin(sb
, parent
, cached
->path
);
256 if (porigin
->refcnt
== 1)
257 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
260 /* otherwise it was not very useful; free it */
265 /* See if the origin->path is different between parent
266 * and origin first. Most of the time they are the
267 * same and diff-tree is fairly efficient about this.
269 diff_setup(&diff_opts
);
270 diff_opts
.recursive
= 1;
271 diff_opts
.detect_rename
= 0;
272 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
273 paths
[0] = origin
->path
;
276 diff_tree_setup_paths(paths
, &diff_opts
);
277 if (diff_setup_done(&diff_opts
) < 0)
279 diff_tree_sha1(parent
->tree
->object
.sha1
,
280 origin
->commit
->tree
->object
.sha1
,
282 diffcore_std(&diff_opts
);
284 /* It is either one entry that says "modified", or "created",
287 if (!diff_queued_diff
.nr
) {
288 /* The path is the same as parent */
289 porigin
= get_origin(sb
, parent
, origin
->path
);
290 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
292 else if (diff_queued_diff
.nr
!= 1)
293 die("internal error in blame::find_origin");
295 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
298 die("internal error in blame::find_origin (%c)",
301 porigin
= get_origin(sb
, parent
, origin
->path
);
302 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
306 /* Did not exist in parent, or type changed */
310 diff_flush(&diff_opts
);
312 struct origin
*cached
;
313 cached
= make_origin(porigin
->commit
, porigin
->path
);
314 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
315 parent
->util
= cached
;
320 static struct origin
*find_rename(struct scoreboard
*sb
,
321 struct commit
*parent
,
322 struct origin
*origin
)
324 struct origin
*porigin
= NULL
;
325 struct diff_options diff_opts
;
327 const char *paths
[2];
329 diff_setup(&diff_opts
);
330 diff_opts
.recursive
= 1;
331 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
332 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
333 diff_opts
.single_follow
= origin
->path
;
335 diff_tree_setup_paths(paths
, &diff_opts
);
336 if (diff_setup_done(&diff_opts
) < 0)
338 diff_tree_sha1(parent
->tree
->object
.sha1
,
339 origin
->commit
->tree
->object
.sha1
,
341 diffcore_std(&diff_opts
);
343 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
344 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
345 if ((p
->status
== 'R' || p
->status
== 'C') &&
346 !strcmp(p
->two
->path
, origin
->path
)) {
347 porigin
= get_origin(sb
, parent
, p
->one
->path
);
348 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
352 diff_flush(&diff_opts
);
357 /* line number in postimage; up to but not including this
358 * line is the same as preimage
362 /* preimage line number after this chunk */
365 /* postimage line number after this chunk */
370 struct chunk
*chunks
;
374 struct blame_diff_state
{
375 struct xdiff_emit_state xm
;
377 unsigned hunk_post_context
;
378 unsigned hunk_in_pre_context
: 1;
381 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
383 struct blame_diff_state
*state
= state_
;
385 int off1
, off2
, len1
, len2
, num
;
387 num
= state
->ret
->num
;
388 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
389 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
390 state
->ret
->chunks
[num
- 1].same
++;
392 state
->hunk_in_pre_context
= 0;
394 state
->hunk_post_context
++;
396 state
->hunk_post_context
= 0;
401 if (num
&& state
->hunk_post_context
) {
402 chunk
= &state
->ret
->chunks
[num
- 1];
403 chunk
->p_next
-= state
->hunk_post_context
;
404 chunk
->t_next
-= state
->hunk_post_context
;
406 state
->ret
->num
= ++num
;
407 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
408 sizeof(struct chunk
) * num
);
409 chunk
= &state
->ret
->chunks
[num
- 1];
410 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
415 /* Line numbers in patch output are one based. */
419 chunk
->same
= len2
? off2
: (off2
+ 1);
421 chunk
->p_next
= off1
+ (len1
? len1
: 1);
422 chunk
->t_next
= chunk
->same
+ len2
;
423 state
->hunk_in_pre_context
= 1;
424 state
->hunk_post_context
= 0;
427 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
430 struct blame_diff_state state
;
435 xpp
.flags
= XDF_NEED_MINIMAL
;
436 xecfg
.ctxlen
= context
;
438 ecb
.outf
= xdiff_outf
;
440 memset(&state
, 0, sizeof(state
));
441 state
.xm
.consume
= process_u_diff
;
442 state
.ret
= xmalloc(sizeof(struct patch
));
443 state
.ret
->chunks
= NULL
;
446 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
448 if (state
.ret
->num
) {
450 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
451 chunk
->p_next
-= state
.hunk_post_context
;
452 chunk
->t_next
-= state
.hunk_post_context
;
457 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
459 mmfile_t file_p
, file_o
;
462 fill_origin_blob(parent
, &file_p
);
463 fill_origin_blob(origin
, &file_o
);
464 if (!file_p
.ptr
|| !file_o
.ptr
)
466 patch
= compare_buffer(&file_p
, &file_o
, 0);
471 static void free_patch(struct patch
*p
)
477 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
479 struct blame_entry
*ent
, *prev
= NULL
;
481 origin_incref(e
->suspect
);
483 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
486 /* prev, if not NULL, is the last one that is below e */
489 e
->next
= prev
->next
;
500 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
502 struct blame_entry
*p
, *n
;
506 origin_incref(src
->suspect
);
507 origin_decref(dst
->suspect
);
508 memcpy(dst
, src
, sizeof(*src
));
514 static const char *nth_line(struct scoreboard
*sb
, int lno
)
516 return sb
->final_buf
+ sb
->lineno
[lno
];
519 static void split_overlap(struct blame_entry
*split
,
520 struct blame_entry
*e
,
521 int tlno
, int plno
, int same
,
522 struct origin
*parent
)
524 /* it is known that lines between tlno to same came from
525 * parent, and e has an overlap with that range. it also is
526 * known that parent's line plno corresponds to e's line tlno.
532 * <------------------>
534 * Potentially we need to split e into three parts; before
535 * this chunk, the chunk to be blamed for parent, and after
539 memset(split
, 0, sizeof(struct blame_entry
[3]));
541 if (e
->s_lno
< tlno
) {
542 /* there is a pre-chunk part not blamed on parent */
543 split
[0].suspect
= origin_incref(e
->suspect
);
544 split
[0].lno
= e
->lno
;
545 split
[0].s_lno
= e
->s_lno
;
546 split
[0].num_lines
= tlno
- e
->s_lno
;
547 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
548 split
[1].s_lno
= plno
;
551 split
[1].lno
= e
->lno
;
552 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
555 if (same
< e
->s_lno
+ e
->num_lines
) {
556 /* there is a post-chunk part not blamed on parent */
557 split
[2].suspect
= origin_incref(e
->suspect
);
558 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
559 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
560 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
561 chunk_end_lno
= split
[2].lno
;
564 chunk_end_lno
= e
->lno
+ e
->num_lines
;
565 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
567 if (split
[1].num_lines
< 1)
569 split
[1].suspect
= origin_incref(parent
);
572 static void split_blame(struct scoreboard
*sb
,
573 struct blame_entry
*split
,
574 struct blame_entry
*e
)
576 struct blame_entry
*new_entry
;
578 if (split
[0].suspect
&& split
[2].suspect
) {
579 /* we need to split e into two and add another for parent */
580 dup_entry(e
, &split
[0]);
582 new_entry
= xmalloc(sizeof(*new_entry
));
583 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
584 add_blame_entry(sb
, new_entry
);
586 new_entry
= xmalloc(sizeof(*new_entry
));
587 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
588 add_blame_entry(sb
, new_entry
);
590 else if (!split
[0].suspect
&& !split
[2].suspect
)
591 /* parent covers the entire area */
592 dup_entry(e
, &split
[1]);
593 else if (split
[0].suspect
) {
594 dup_entry(e
, &split
[0]);
596 new_entry
= xmalloc(sizeof(*new_entry
));
597 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
598 add_blame_entry(sb
, new_entry
);
601 dup_entry(e
, &split
[1]);
603 new_entry
= xmalloc(sizeof(*new_entry
));
604 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
605 add_blame_entry(sb
, new_entry
);
608 if (DEBUG
) { /* sanity */
609 struct blame_entry
*ent
;
610 int lno
= sb
->ent
->lno
, corrupt
= 0;
612 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
617 lno
+= ent
->num_lines
;
621 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
622 printf("L %8d l %8d n %8d\n",
623 lno
, ent
->lno
, ent
->num_lines
);
624 lno
= ent
->lno
+ ent
->num_lines
;
631 static void decref_split(struct blame_entry
*split
)
635 for (i
= 0; i
< 3; i
++)
636 origin_decref(split
[i
].suspect
);
639 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
640 int tlno
, int plno
, int same
,
641 struct origin
*parent
)
643 struct blame_entry split
[3];
645 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
646 if (split
[1].suspect
)
647 split_blame(sb
, split
, e
);
651 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
653 struct blame_entry
*e
;
654 int last_in_target
= -1;
656 for (e
= sb
->ent
; e
; e
= e
->next
) {
657 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
659 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
660 last_in_target
= e
->s_lno
+ e
->num_lines
;
662 return last_in_target
;
665 static void blame_chunk(struct scoreboard
*sb
,
666 int tlno
, int plno
, int same
,
667 struct origin
*target
, struct origin
*parent
)
669 struct blame_entry
*e
;
671 for (e
= sb
->ent
; e
; e
= e
->next
) {
672 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
674 if (same
<= e
->s_lno
)
676 if (tlno
< e
->s_lno
+ e
->num_lines
)
677 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
681 static int pass_blame_to_parent(struct scoreboard
*sb
,
682 struct origin
*target
,
683 struct origin
*parent
)
685 int i
, last_in_target
, plno
, tlno
;
688 last_in_target
= find_last_in_target(sb
, target
);
689 if (last_in_target
< 0)
690 return 1; /* nothing remains for this target */
692 patch
= get_patch(parent
, target
);
694 for (i
= 0; i
< patch
->num
; i
++) {
695 struct chunk
*chunk
= &patch
->chunks
[i
];
697 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
698 plno
= chunk
->p_next
;
699 tlno
= chunk
->t_next
;
701 /* rest (i.e. anything above tlno) are the same as parent */
702 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
708 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
717 cp
= nth_line(sb
, e
->lno
);
718 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
720 unsigned ch
= *((unsigned char *)cp
);
729 static void copy_split_if_better(struct scoreboard
*sb
,
730 struct blame_entry
*best_so_far
,
731 struct blame_entry
*this)
735 if (!this[1].suspect
)
737 if (best_so_far
[1].suspect
) {
738 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
742 for (i
= 0; i
< 3; i
++)
743 origin_incref(this[i
].suspect
);
744 decref_split(best_so_far
);
745 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
748 static void find_copy_in_blob(struct scoreboard
*sb
,
749 struct blame_entry
*ent
,
750 struct origin
*parent
,
751 struct blame_entry
*split
,
760 cp
= nth_line(sb
, ent
->lno
);
761 file_o
.ptr
= (char*) cp
;
762 cnt
= ent
->num_lines
;
764 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
768 file_o
.size
= cp
- file_o
.ptr
;
770 patch
= compare_buffer(file_p
, &file_o
, 1);
772 memset(split
, 0, sizeof(struct blame_entry
[3]));
774 for (i
= 0; i
< patch
->num
; i
++) {
775 struct chunk
*chunk
= &patch
->chunks
[i
];
777 /* tlno to chunk->same are the same as ent */
778 if (ent
->num_lines
<= tlno
)
780 if (tlno
< chunk
->same
) {
781 struct blame_entry
this[3];
782 split_overlap(this, ent
,
783 tlno
+ ent
->s_lno
, plno
,
784 chunk
->same
+ ent
->s_lno
,
786 copy_split_if_better(sb
, split
, this);
789 plno
= chunk
->p_next
;
790 tlno
= chunk
->t_next
;
795 static int find_move_in_parent(struct scoreboard
*sb
,
796 struct origin
*target
,
797 struct origin
*parent
)
799 int last_in_target
, made_progress
;
800 struct blame_entry
*e
, split
[3];
803 last_in_target
= find_last_in_target(sb
, target
);
804 if (last_in_target
< 0)
805 return 1; /* nothing remains for this target */
807 fill_origin_blob(parent
, &file_p
);
812 while (made_progress
) {
814 for (e
= sb
->ent
; e
; e
= e
->next
) {
815 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
817 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
818 if (split
[1].suspect
&&
819 blame_move_score
< ent_score(sb
, &split
[1])) {
820 split_blame(sb
, split
, e
);
831 struct blame_entry
*ent
;
832 struct blame_entry split
[3];
835 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
836 struct origin
*target
,
839 struct blame_entry
*e
;
841 struct blame_list
*blame_list
= NULL
;
843 /* Count the number of entries the target is suspected for,
844 * and prepare a list of entry and the best split.
846 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
847 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
850 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
851 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
852 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
853 blame_list
[i
++].ent
= e
;
855 *num_ents_p
= num_ents
;
859 static int find_copy_in_parent(struct scoreboard
*sb
,
860 struct origin
*target
,
861 struct commit
*parent
,
862 struct origin
*porigin
,
865 struct diff_options diff_opts
;
866 const char *paths
[1];
869 struct blame_list
*blame_list
;
872 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
874 return 1; /* nothing remains for this target */
876 diff_setup(&diff_opts
);
877 diff_opts
.recursive
= 1;
878 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
881 diff_tree_setup_paths(paths
, &diff_opts
);
882 if (diff_setup_done(&diff_opts
) < 0)
885 /* Try "find copies harder" on new path if requested;
886 * we do not want to use diffcore_rename() actually to
887 * match things up; find_copies_harder is set only to
888 * force diff_tree_sha1() to feed all filepairs to diff_queue,
889 * and this code needs to be after diff_setup_done(), which
890 * usually makes find-copies-harder imply copy detection.
892 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
893 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
894 diff_opts
.find_copies_harder
= 1;
896 diff_tree_sha1(parent
->tree
->object
.sha1
,
897 target
->commit
->tree
->object
.sha1
,
900 if (!diff_opts
.find_copies_harder
)
901 diffcore_std(&diff_opts
);
905 int made_progress
= 0;
907 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
908 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
909 struct origin
*norigin
;
911 struct blame_entry
this[3];
913 if (!DIFF_FILE_VALID(p
->one
))
914 continue; /* does not exist in parent */
915 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
916 /* find_move already dealt with this path */
919 norigin
= get_origin(sb
, parent
, p
->one
->path
);
920 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
921 fill_origin_blob(norigin
, &file_p
);
925 for (j
= 0; j
< num_ents
; j
++) {
926 find_copy_in_blob(sb
, blame_list
[j
].ent
,
927 norigin
, this, &file_p
);
928 copy_split_if_better(sb
, blame_list
[j
].split
,
932 origin_decref(norigin
);
935 for (j
= 0; j
< num_ents
; j
++) {
936 struct blame_entry
*split
= blame_list
[j
].split
;
937 if (split
[1].suspect
&&
938 blame_copy_score
< ent_score(sb
, &split
[1])) {
939 split_blame(sb
, split
, blame_list
[j
].ent
);
948 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
954 diff_flush(&diff_opts
);
959 /* The blobs of origin and porigin exactly match, so everything
960 * origin is suspected for can be blamed on the parent.
962 static void pass_whole_blame(struct scoreboard
*sb
,
963 struct origin
*origin
, struct origin
*porigin
)
965 struct blame_entry
*e
;
967 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
969 porigin
->file
= origin
->file
;
970 origin
->file
.ptr
= NULL
;
972 for (e
= sb
->ent
; e
; e
= e
->next
) {
973 if (cmp_suspect(e
->suspect
, origin
))
975 origin_incref(porigin
);
976 origin_decref(e
->suspect
);
977 e
->suspect
= porigin
;
983 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
986 struct commit
*commit
= origin
->commit
;
987 struct commit_list
*parent
;
988 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
990 memset(parent_origin
, 0, sizeof(parent_origin
));
992 /* The first pass looks for unrenamed path to optimize for
993 * common cases, then we look for renames in the second pass.
995 for (pass
= 0; pass
< 2; pass
++) {
996 struct origin
*(*find
)(struct scoreboard
*,
997 struct commit
*, struct origin
*);
998 find
= pass
? find_rename
: find_origin
;
1000 for (i
= 0, parent
= commit
->parents
;
1001 i
< MAXPARENT
&& parent
;
1002 parent
= parent
->next
, i
++) {
1003 struct commit
*p
= parent
->item
;
1006 if (parent_origin
[i
])
1008 if (parse_commit(p
))
1010 porigin
= find(sb
, p
, origin
);
1013 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1014 pass_whole_blame(sb
, origin
, porigin
);
1015 origin_decref(porigin
);
1018 for (j
= same
= 0; j
< i
; j
++)
1019 if (parent_origin
[j
] &&
1020 !hashcmp(parent_origin
[j
]->blob_sha1
,
1021 porigin
->blob_sha1
)) {
1026 parent_origin
[i
] = porigin
;
1028 origin_decref(porigin
);
1033 for (i
= 0, parent
= commit
->parents
;
1034 i
< MAXPARENT
&& parent
;
1035 parent
= parent
->next
, i
++) {
1036 struct origin
*porigin
= parent_origin
[i
];
1039 if (pass_blame_to_parent(sb
, origin
, porigin
))
1044 * Optionally run "miff" to find moves in parents' files here.
1046 if (opt
& PICKAXE_BLAME_MOVE
)
1047 for (i
= 0, parent
= commit
->parents
;
1048 i
< MAXPARENT
&& parent
;
1049 parent
= parent
->next
, i
++) {
1050 struct origin
*porigin
= parent_origin
[i
];
1053 if (find_move_in_parent(sb
, origin
, porigin
))
1058 * Optionally run "ciff" to find copies from parents' files here.
1060 if (opt
& PICKAXE_BLAME_COPY
)
1061 for (i
= 0, parent
= commit
->parents
;
1062 i
< MAXPARENT
&& parent
;
1063 parent
= parent
->next
, i
++) {
1064 struct origin
*porigin
= parent_origin
[i
];
1065 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1071 for (i
= 0; i
< MAXPARENT
; i
++)
1072 origin_decref(parent_origin
[i
]);
1079 unsigned long author_time
;
1082 /* filled only when asked for details */
1084 char *committer_mail
;
1085 unsigned long committer_time
;
1091 static void get_ac_line(const char *inbuf
, const char *what
,
1092 int bufsz
, char *person
, char **mail
,
1093 unsigned long *time
, char **tz
)
1098 tmp
= strstr(inbuf
, what
);
1101 tmp
+= strlen(what
);
1102 endp
= strchr(tmp
, '\n');
1110 person
= *mail
= *tz
= "(unknown)";
1114 memcpy(person
, tmp
, len
);
1126 *time
= strtoul(tmp
, NULL
, 10);
1135 static void get_commit_info(struct commit
*commit
,
1136 struct commit_info
*ret
,
1141 static char author_buf
[1024];
1142 static char committer_buf
[1024];
1143 static char summary_buf
[1024];
1145 /* We've operated without save_commit_buffer, so
1146 * we now need to populate them for output.
1148 if (!commit
->buffer
) {
1152 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1154 ret
->author
= author_buf
;
1155 get_ac_line(commit
->buffer
, "\nauthor ",
1156 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1157 &ret
->author_time
, &ret
->author_tz
);
1162 ret
->committer
= committer_buf
;
1163 get_ac_line(commit
->buffer
, "\ncommitter ",
1164 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1165 &ret
->committer_time
, &ret
->committer_tz
);
1167 ret
->summary
= summary_buf
;
1168 tmp
= strstr(commit
->buffer
, "\n\n");
1171 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1175 endp
= strchr(tmp
, '\n');
1179 if (len
>= sizeof(summary_buf
))
1181 memcpy(summary_buf
, tmp
, len
);
1182 summary_buf
[len
] = 0;
1185 static void write_filename_info(const char *path
)
1187 printf("filename ");
1188 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1192 static void found_guilty_entry(struct blame_entry
*ent
)
1198 struct origin
*suspect
= ent
->suspect
;
1200 printf("%s %d %d %d\n",
1201 sha1_to_hex(suspect
->commit
->object
.sha1
),
1202 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1203 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1204 struct commit_info ci
;
1205 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1206 get_commit_info(suspect
->commit
, &ci
, 1);
1207 printf("author %s\n", ci
.author
);
1208 printf("author-mail %s\n", ci
.author_mail
);
1209 printf("author-time %lu\n", ci
.author_time
);
1210 printf("author-tz %s\n", ci
.author_tz
);
1211 printf("committer %s\n", ci
.committer
);
1212 printf("committer-mail %s\n", ci
.committer_mail
);
1213 printf("committer-time %lu\n", ci
.committer_time
);
1214 printf("committer-tz %s\n", ci
.committer_tz
);
1215 printf("summary %s\n", ci
.summary
);
1216 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1217 printf("boundary\n");
1219 write_filename_info(suspect
->path
);
1223 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1226 struct blame_entry
*ent
;
1227 struct commit
*commit
;
1228 struct origin
*suspect
= NULL
;
1230 /* find one suspect to break down */
1231 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1233 suspect
= ent
->suspect
;
1235 return; /* all done */
1237 origin_incref(suspect
);
1238 commit
= suspect
->commit
;
1239 if (!commit
->object
.parsed
)
1240 parse_commit(commit
);
1241 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1242 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1243 pass_blame(sb
, suspect
, opt
);
1245 commit
->object
.flags
|= UNINTERESTING
;
1246 if (commit
->object
.parsed
)
1247 mark_parents_uninteresting(commit
);
1249 /* treat root commit as boundary */
1250 if (!commit
->parents
&& !show_root
)
1251 commit
->object
.flags
|= UNINTERESTING
;
1253 /* Take responsibility for the remaining entries */
1254 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1255 if (!cmp_suspect(ent
->suspect
, suspect
))
1256 found_guilty_entry(ent
);
1257 origin_decref(suspect
);
1259 if (DEBUG
) /* sanity */
1260 sanity_check_refcnt(sb
);
1264 static const char *format_time(unsigned long time
, const char *tz_str
,
1267 static char time_buf
[128];
1272 if (show_raw_time
) {
1273 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1278 minutes
= tz
< 0 ? -tz
: tz
;
1279 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1280 minutes
= tz
< 0 ? -minutes
: minutes
;
1281 t
= time
+ minutes
* 60;
1284 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1285 strcat(time_buf
, tz_str
);
1289 #define OUTPUT_ANNOTATE_COMPAT 001
1290 #define OUTPUT_LONG_OBJECT_NAME 002
1291 #define OUTPUT_RAW_TIMESTAMP 004
1292 #define OUTPUT_PORCELAIN 010
1293 #define OUTPUT_SHOW_NAME 020
1294 #define OUTPUT_SHOW_NUMBER 040
1295 #define OUTPUT_SHOW_SCORE 0100
1297 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1301 struct origin
*suspect
= ent
->suspect
;
1304 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1305 printf("%s%c%d %d %d\n",
1307 ent
->guilty
? ' ' : '*', // purely for debugging
1311 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1312 struct commit_info ci
;
1313 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1314 get_commit_info(suspect
->commit
, &ci
, 1);
1315 printf("author %s\n", ci
.author
);
1316 printf("author-mail %s\n", ci
.author_mail
);
1317 printf("author-time %lu\n", ci
.author_time
);
1318 printf("author-tz %s\n", ci
.author_tz
);
1319 printf("committer %s\n", ci
.committer
);
1320 printf("committer-mail %s\n", ci
.committer_mail
);
1321 printf("committer-time %lu\n", ci
.committer_time
);
1322 printf("committer-tz %s\n", ci
.committer_tz
);
1323 write_filename_info(suspect
->path
);
1324 printf("summary %s\n", ci
.summary
);
1325 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1326 printf("boundary\n");
1328 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1329 write_filename_info(suspect
->path
);
1331 cp
= nth_line(sb
, ent
->lno
);
1332 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1335 printf("%s %d %d\n", hex
,
1336 ent
->s_lno
+ 1 + cnt
,
1337 ent
->lno
+ 1 + cnt
);
1342 } while (ch
!= '\n' &&
1343 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1347 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1351 struct origin
*suspect
= ent
->suspect
;
1352 struct commit_info ci
;
1354 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1356 get_commit_info(suspect
->commit
, &ci
, 1);
1357 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1359 cp
= nth_line(sb
, ent
->lno
);
1360 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1362 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1364 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1365 if (!blank_boundary
) {
1370 memset(hex
, ' ', length
);
1373 printf("%.*s", length
, hex
);
1374 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1375 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1376 format_time(ci
.author_time
, ci
.author_tz
,
1378 ent
->lno
+ 1 + cnt
);
1380 if (opt
& OUTPUT_SHOW_SCORE
)
1382 max_score_digits
, ent
->score
,
1383 ent
->suspect
->refcnt
);
1384 if (opt
& OUTPUT_SHOW_NAME
)
1385 printf(" %-*.*s", longest_file
, longest_file
,
1387 if (opt
& OUTPUT_SHOW_NUMBER
)
1388 printf(" %*d", max_orig_digits
,
1389 ent
->s_lno
+ 1 + cnt
);
1390 printf(" (%-*.*s %10s %*d) ",
1391 longest_author
, longest_author
, ci
.author
,
1392 format_time(ci
.author_time
, ci
.author_tz
,
1394 max_digits
, ent
->lno
+ 1 + cnt
);
1399 } while (ch
!= '\n' &&
1400 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1404 static void output(struct scoreboard
*sb
, int option
)
1406 struct blame_entry
*ent
;
1408 if (option
& OUTPUT_PORCELAIN
) {
1409 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1410 struct blame_entry
*oth
;
1411 struct origin
*suspect
= ent
->suspect
;
1412 struct commit
*commit
= suspect
->commit
;
1413 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1415 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1416 if ((oth
->suspect
->commit
!= commit
) ||
1417 !strcmp(oth
->suspect
->path
, suspect
->path
))
1419 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1425 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1426 if (option
& OUTPUT_PORCELAIN
)
1427 emit_porcelain(sb
, ent
);
1429 emit_other(sb
, ent
, option
);
1434 static int prepare_lines(struct scoreboard
*sb
)
1436 const char *buf
= sb
->final_buf
;
1437 unsigned long len
= sb
->final_buf_size
;
1438 int num
= 0, incomplete
= 0, bol
= 1;
1440 if (len
&& buf
[len
-1] != '\n')
1441 incomplete
++; /* incomplete line at the end */
1444 sb
->lineno
= xrealloc(sb
->lineno
,
1445 sizeof(int* ) * (num
+ 1));
1446 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1449 if (*buf
++ == '\n') {
1454 sb
->lineno
= xrealloc(sb
->lineno
,
1455 sizeof(int* ) * (num
+ incomplete
+ 1));
1456 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1457 sb
->num_lines
= num
+ incomplete
;
1458 return sb
->num_lines
;
1461 static int read_ancestry(const char *graft_file
)
1463 FILE *fp
= fopen(graft_file
, "r");
1467 while (fgets(buf
, sizeof(buf
), fp
)) {
1468 /* The format is just "Commit Parent1 Parent2 ...\n" */
1469 int len
= strlen(buf
);
1470 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1472 register_commit_graft(graft
, 0);
1478 static int lineno_width(int lines
)
1482 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1487 static void find_alignment(struct scoreboard
*sb
, int *option
)
1489 int longest_src_lines
= 0;
1490 int longest_dst_lines
= 0;
1491 unsigned largest_score
= 0;
1492 struct blame_entry
*e
;
1494 for (e
= sb
->ent
; e
; e
= e
->next
) {
1495 struct origin
*suspect
= e
->suspect
;
1496 struct commit_info ci
;
1499 if (strcmp(suspect
->path
, sb
->path
))
1500 *option
|= OUTPUT_SHOW_NAME
;
1501 num
= strlen(suspect
->path
);
1502 if (longest_file
< num
)
1504 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1505 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1506 get_commit_info(suspect
->commit
, &ci
, 1);
1507 num
= strlen(ci
.author
);
1508 if (longest_author
< num
)
1509 longest_author
= num
;
1511 num
= e
->s_lno
+ e
->num_lines
;
1512 if (longest_src_lines
< num
)
1513 longest_src_lines
= num
;
1514 num
= e
->lno
+ e
->num_lines
;
1515 if (longest_dst_lines
< num
)
1516 longest_dst_lines
= num
;
1517 if (largest_score
< ent_score(sb
, e
))
1518 largest_score
= ent_score(sb
, e
);
1520 max_orig_digits
= lineno_width(longest_src_lines
);
1521 max_digits
= lineno_width(longest_dst_lines
);
1522 max_score_digits
= lineno_width(largest_score
);
1525 static void sanity_check_refcnt(struct scoreboard
*sb
)
1528 struct blame_entry
*ent
;
1530 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1531 /* Nobody should have zero or negative refcnt */
1532 if (ent
->suspect
->refcnt
<= 0) {
1533 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1535 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1536 ent
->suspect
->refcnt
);
1540 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1541 /* Mark the ones that haven't been checked */
1542 if (0 < ent
->suspect
->refcnt
)
1543 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1545 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1546 /* then pick each and see if they have the the correct
1550 struct blame_entry
*e
;
1551 struct origin
*suspect
= ent
->suspect
;
1553 if (0 < suspect
->refcnt
)
1555 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1556 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1557 if (e
->suspect
!= suspect
)
1561 if (suspect
->refcnt
!= found
) {
1562 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1564 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1565 ent
->suspect
->refcnt
, found
);
1571 find_alignment(sb
, &opt
);
1573 die("Baa %d!", baa
);
1577 static int has_path_in_work_tree(const char *path
)
1580 return !lstat(path
, &st
);
1583 static unsigned parse_score(const char *arg
)
1586 unsigned long score
= strtoul(arg
, &end
, 10);
1592 static const char *add_prefix(const char *prefix
, const char *path
)
1594 if (!prefix
|| !prefix
[0])
1596 return prefix_path(prefix
, strlen(prefix
), path
);
1599 static const char *parse_loc(const char *spec
,
1600 struct scoreboard
*sb
, long lno
,
1601 long begin
, long *ret
)
1608 regmatch_t match
[1];
1610 /* Allow "-L <something>,+20" to mean starting at <something>
1611 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1614 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1615 num
= strtol(spec
+ 1, &term
, 10);
1616 if (term
!= spec
+ 1) {
1620 *ret
= begin
+ num
- 2;
1629 num
= strtol(spec
, &term
, 10);
1637 /* it could be a regexp of form /.../ */
1638 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1645 /* try [spec+1 .. term-1] as regexp */
1647 begin
--; /* input is in human terms */
1648 line
= nth_line(sb
, begin
);
1650 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1651 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1652 const char *cp
= line
+ match
[0].rm_so
;
1655 while (begin
++ < lno
) {
1656 nline
= nth_line(sb
, begin
);
1657 if (line
<= cp
&& cp
< nline
)
1668 regerror(reg_error
, ®exp
, errbuf
, 1024);
1669 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1673 static void prepare_blame_range(struct scoreboard
*sb
,
1674 const char *bottomtop
,
1676 long *bottom
, long *top
)
1680 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1682 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1690 static int git_blame_config(const char *var
, const char *value
)
1692 if (!strcmp(var
, "blame.showroot")) {
1693 show_root
= git_config_bool(var
, value
);
1696 if (!strcmp(var
, "blame.blankboundary")) {
1697 blank_boundary
= git_config_bool(var
, value
);
1700 return git_default_config(var
, value
);
1703 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
1705 struct rev_info revs
;
1707 struct scoreboard sb
;
1709 struct blame_entry
*ent
;
1710 int i
, seen_dashdash
, unk
, opt
;
1711 long bottom
, top
, lno
;
1712 int output_option
= 0;
1713 const char *revs_file
= NULL
;
1714 const char *final_commit_name
= NULL
;
1716 const char *bottomtop
= NULL
;
1718 git_config(git_blame_config
);
1719 save_commit_buffer
= 0;
1723 for (unk
= i
= 1; i
< argc
; i
++) {
1724 const char *arg
= argv
[i
];
1727 else if (!strcmp("-b", arg
))
1729 else if (!strcmp("--root", arg
))
1731 else if (!strcmp("-c", arg
))
1732 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1733 else if (!strcmp("-t", arg
))
1734 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1735 else if (!strcmp("-l", arg
))
1736 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1737 else if (!strcmp("-S", arg
) && ++i
< argc
)
1738 revs_file
= argv
[i
];
1739 else if (!strncmp("-M", arg
, 2)) {
1740 opt
|= PICKAXE_BLAME_MOVE
;
1741 blame_move_score
= parse_score(arg
+2);
1743 else if (!strncmp("-C", arg
, 2)) {
1744 if (opt
& PICKAXE_BLAME_COPY
)
1745 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1746 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1747 blame_copy_score
= parse_score(arg
+2);
1749 else if (!strncmp("-L", arg
, 2)) {
1758 die("More than one '-L n,m' option given");
1761 else if (!strcmp("--incremental", arg
))
1763 else if (!strcmp("--score-debug", arg
))
1764 output_option
|= OUTPUT_SHOW_SCORE
;
1765 else if (!strcmp("-f", arg
) ||
1766 !strcmp("--show-name", arg
))
1767 output_option
|= OUTPUT_SHOW_NAME
;
1768 else if (!strcmp("-n", arg
) ||
1769 !strcmp("--show-number", arg
))
1770 output_option
|= OUTPUT_SHOW_NUMBER
;
1771 else if (!strcmp("-p", arg
) ||
1772 !strcmp("--porcelain", arg
))
1773 output_option
|= OUTPUT_PORCELAIN
;
1774 else if (!strcmp("--", arg
)) {
1786 if (!blame_move_score
)
1787 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1788 if (!blame_copy_score
)
1789 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1791 /* We have collected options unknown to us in argv[1..unk]
1792 * which are to be passed to revision machinery if we are
1793 * going to do the "bottom" procesing.
1795 * The remaining are:
1797 * (1) if seen_dashdash, its either
1798 * "-options -- <path>" or
1799 * "-options -- <path> <rev>".
1800 * but the latter is allowed only if there is no
1801 * options that we passed to revision machinery.
1803 * (2) otherwise, we may have "--" somewhere later and
1804 * might be looking at the first one of multiple 'rev'
1805 * parameters (e.g. " master ^next ^maint -- path").
1806 * See if there is a dashdash first, and give the
1807 * arguments before that to revision machinery.
1808 * After that there must be one 'path'.
1810 * (3) otherwise, its one of the three:
1811 * "-options <path> <rev>"
1812 * "-options <rev> <path>"
1814 * but again the first one is allowed only if
1815 * there is no options that we passed to revision
1819 if (seen_dashdash
) {
1823 path
= add_prefix(prefix
, argv
[i
]);
1824 if (i
+ 1 == argc
- 1) {
1827 argv
[unk
++] = argv
[i
+ 1];
1829 else if (i
+ 1 != argc
)
1830 /* garbage at end */
1835 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1836 if (!strcmp(argv
[j
], "--"))
1838 if (seen_dashdash
) {
1839 if (seen_dashdash
+ 1 != argc
- 1)
1841 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
1842 for (j
= i
; j
< seen_dashdash
; j
++)
1843 argv
[unk
++] = argv
[j
];
1847 path
= add_prefix(prefix
, argv
[i
]);
1848 if (i
+ 1 == argc
- 1) {
1849 final_commit_name
= argv
[i
+ 1];
1851 /* if (unk == 1) we could be getting
1854 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1855 path
= add_prefix(prefix
, argv
[i
+ 1]);
1856 final_commit_name
= argv
[i
];
1859 else if (i
!= argc
- 1)
1860 usage(blame_usage
); /* garbage at end */
1862 if (!has_path_in_work_tree(path
))
1863 die("cannot stat path %s: %s",
1864 path
, strerror(errno
));
1868 if (final_commit_name
)
1869 argv
[unk
++] = final_commit_name
;
1871 /* Now we got rev and path. We do not want the path pruning
1872 * but we may want "bottom" processing.
1874 argv
[unk
++] = "--"; /* terminate the rev name */
1877 init_revisions(&revs
, NULL
);
1878 setup_revisions(unk
, argv
, &revs
, "HEAD");
1879 memset(&sb
, 0, sizeof(sb
));
1881 /* There must be one and only one positive commit in the
1882 * revs->pending array.
1884 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1885 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1886 if (obj
->flags
& UNINTERESTING
)
1888 while (obj
->type
== OBJ_TAG
)
1889 obj
= deref_tag(obj
, NULL
, 0);
1890 if (obj
->type
!= OBJ_COMMIT
)
1891 die("Non commit %s?",
1892 revs
.pending
.objects
[i
].name
);
1894 die("More than one commit to dig from %s and %s?",
1895 revs
.pending
.objects
[i
].name
,
1897 sb
.final
= (struct commit
*) obj
;
1898 final_commit_name
= revs
.pending
.objects
[i
].name
;
1902 /* "--not A B -- path" without anything positive */
1903 unsigned char head_sha1
[20];
1905 final_commit_name
= "HEAD";
1906 if (get_sha1(final_commit_name
, head_sha1
))
1907 die("No such ref: HEAD");
1908 sb
.final
= lookup_commit_reference(head_sha1
);
1909 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1912 /* If we have bottom, this will mark the ancestors of the
1913 * bottom commits we would reach while traversing as
1916 prepare_revision_walk(&revs
);
1918 o
= get_origin(&sb
, sb
.final
, path
);
1919 if (fill_blob_sha1(o
))
1920 die("no such path %s in %s", path
, final_commit_name
);
1922 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1924 lno
= prepare_lines(&sb
);
1928 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
1929 if (bottom
&& top
&& top
< bottom
) {
1931 tmp
= top
; top
= bottom
; bottom
= tmp
;
1939 die("file %s has only %lu lines", path
, lno
);
1941 ent
= xcalloc(1, sizeof(*ent
));
1943 ent
->num_lines
= top
- bottom
;
1945 ent
->s_lno
= bottom
;
1950 if (revs_file
&& read_ancestry(revs_file
))
1951 die("reading graft file %s failed: %s",
1952 revs_file
, strerror(errno
));
1954 assign_blame(&sb
, &revs
, opt
);
1961 if (!(output_option
& OUTPUT_PORCELAIN
))
1962 find_alignment(&sb
, &output_option
);
1964 output(&sb
, output_option
);
1965 free((void *)sb
.final_buf
);
1966 for (ent
= sb
.ent
; ent
; ) {
1967 struct blame_entry
*e
= ent
->next
;
1973 printf("num read blob: %d\n", num_read_blob
);
1974 printf("num get patch: %d\n", num_get_patch
);
1975 printf("num commits: %d\n", num_commits
);