4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
16 #include "xdiff-interface.h"
18 static char blame_usage
[] =
19 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
20 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
21 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
22 " -l, --long Show long commit SHA1 (Default: off)\n"
23 " --root Do not treat root commits as boundaries (Default: off)\n"
24 " -t, --time Show raw timestamp (Default: off)\n"
25 " -f, --show-name Show original filename (Default: auto)\n"
26 " -n, --show-number Show original linenumber (Default: off)\n"
27 " -p, --porcelain Show in a format designed for machine consumption\n"
28 " -L n,m Process only line range n,m, counting from 1\n"
29 " -M, -C Find line movements within and across files\n"
30 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
32 static int longest_file
;
33 static int longest_author
;
34 static int max_orig_digits
;
35 static int max_digits
;
36 static int max_score_digits
;
38 static int blank_boundary
;
45 static int num_read_blob
;
46 static int num_get_patch
;
47 static int num_commits
;
49 #define PICKAXE_BLAME_MOVE 01
50 #define PICKAXE_BLAME_COPY 02
51 #define PICKAXE_BLAME_COPY_HARDER 04
54 * blame for a blame_entry with score lower than these thresholds
55 * is not passed to the parent using move/copy logic.
57 static unsigned blame_move_score
;
58 static unsigned blame_copy_score
;
59 #define BLAME_DEFAULT_MOVE_SCORE 20
60 #define BLAME_DEFAULT_COPY_SCORE 40
62 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
63 #define METAINFO_SHOWN (1u<<12)
64 #define MORE_THAN_ONE_PATH (1u<<13)
67 * One blob in a commit that is being suspected
71 struct commit
*commit
;
73 unsigned char blob_sha1
[20];
74 char path
[FLEX_ARRAY
];
77 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
82 file
->ptr
= read_sha1_file(o
->blob_sha1
, type
,
83 (unsigned long *)(&(file
->size
)));
91 static inline struct origin
*origin_incref(struct origin
*o
)
98 static void origin_decref(struct origin
*o
)
100 if (o
&& --o
->refcnt
<= 0) {
103 memset(o
, 0, sizeof(*o
));
109 struct blame_entry
*prev
;
110 struct blame_entry
*next
;
112 /* the first line of this group in the final image;
113 * internally all line numbers are 0 based.
117 /* how many lines this group has */
120 /* the commit that introduced this group into the final image */
121 struct origin
*suspect
;
123 /* true if the suspect is truly guilty; false while we have not
124 * checked if the group came from one of its parents.
128 /* the line number of the first line of this group in the
129 * suspect's file; internally all line numbers are 0 based.
133 /* how significant this entry is -- cached to avoid
134 * scanning the lines over and over
140 /* the final commit (i.e. where we started digging from) */
141 struct commit
*final
;
145 /* the contents in the final; pointed into by buf pointers of
148 const char *final_buf
;
149 unsigned long final_buf_size
;
151 /* linked list of blames */
152 struct blame_entry
*ent
;
154 /* look-up a line in the final buffer */
159 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
161 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
164 return strcmp(a
->path
, b
->path
);
167 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
169 static void sanity_check_refcnt(struct scoreboard
*);
171 static void coalesce(struct scoreboard
*sb
)
173 struct blame_entry
*ent
, *next
;
175 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
176 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
177 ent
->guilty
== next
->guilty
&&
178 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
179 ent
->num_lines
+= next
->num_lines
;
180 ent
->next
= next
->next
;
182 ent
->next
->prev
= ent
;
183 origin_decref(next
->suspect
);
186 next
= ent
; /* again */
190 if (DEBUG
) /* sanity */
191 sanity_check_refcnt(sb
);
194 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
197 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
200 strcpy(o
->path
, path
);
204 static struct origin
*get_origin(struct scoreboard
*sb
,
205 struct commit
*commit
,
208 struct blame_entry
*e
;
210 for (e
= sb
->ent
; e
; e
= e
->next
) {
211 if (e
->suspect
->commit
== commit
&&
212 !strcmp(e
->suspect
->path
, path
))
213 return origin_incref(e
->suspect
);
215 return make_origin(commit
, path
);
218 static int fill_blob_sha1(struct origin
*origin
)
223 if (!is_null_sha1(origin
->blob_sha1
))
225 if (get_tree_entry(origin
->commit
->object
.sha1
,
227 origin
->blob_sha1
, &mode
))
229 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
230 strcmp(type
, blob_type
))
234 hashclr(origin
->blob_sha1
);
238 static struct origin
*find_origin(struct scoreboard
*sb
,
239 struct commit
*parent
,
240 struct origin
*origin
)
242 struct origin
*porigin
= NULL
;
243 struct diff_options diff_opts
;
244 const char *paths
[2];
247 /* This is a freestanding copy of origin and not
250 struct origin
*cached
= parent
->util
;
251 if (!strcmp(cached
->path
, origin
->path
)) {
252 porigin
= get_origin(sb
, parent
, cached
->path
);
253 if (porigin
->refcnt
== 1)
254 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
257 /* otherwise it was not very useful; free it */
262 /* See if the origin->path is different between parent
263 * and origin first. Most of the time they are the
264 * same and diff-tree is fairly efficient about this.
266 diff_setup(&diff_opts
);
267 diff_opts
.recursive
= 1;
268 diff_opts
.detect_rename
= 0;
269 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
270 paths
[0] = origin
->path
;
273 diff_tree_setup_paths(paths
, &diff_opts
);
274 if (diff_setup_done(&diff_opts
) < 0)
276 diff_tree_sha1(parent
->tree
->object
.sha1
,
277 origin
->commit
->tree
->object
.sha1
,
279 diffcore_std(&diff_opts
);
281 /* It is either one entry that says "modified", or "created",
284 if (!diff_queued_diff
.nr
) {
285 /* The path is the same as parent */
286 porigin
= get_origin(sb
, parent
, origin
->path
);
287 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
289 else if (diff_queued_diff
.nr
!= 1)
290 die("internal error in blame::find_origin");
292 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
295 die("internal error in blame::find_origin (%c)",
298 porigin
= get_origin(sb
, parent
, origin
->path
);
299 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
303 /* Did not exist in parent, or type changed */
307 diff_flush(&diff_opts
);
309 struct origin
*cached
;
310 cached
= make_origin(porigin
->commit
, porigin
->path
);
311 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
312 parent
->util
= cached
;
317 static struct origin
*find_rename(struct scoreboard
*sb
,
318 struct commit
*parent
,
319 struct origin
*origin
)
321 struct origin
*porigin
= NULL
;
322 struct diff_options diff_opts
;
324 const char *paths
[2];
326 diff_setup(&diff_opts
);
327 diff_opts
.recursive
= 1;
328 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
329 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
330 diff_opts
.single_follow
= origin
->path
;
332 diff_tree_setup_paths(paths
, &diff_opts
);
333 if (diff_setup_done(&diff_opts
) < 0)
335 diff_tree_sha1(parent
->tree
->object
.sha1
,
336 origin
->commit
->tree
->object
.sha1
,
338 diffcore_std(&diff_opts
);
340 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
341 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
342 if ((p
->status
== 'R' || p
->status
== 'C') &&
343 !strcmp(p
->two
->path
, origin
->path
)) {
344 porigin
= get_origin(sb
, parent
, p
->one
->path
);
345 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
349 diff_flush(&diff_opts
);
354 /* line number in postimage; up to but not including this
355 * line is the same as preimage
359 /* preimage line number after this chunk */
362 /* postimage line number after this chunk */
367 struct chunk
*chunks
;
371 struct blame_diff_state
{
372 struct xdiff_emit_state xm
;
374 unsigned hunk_post_context
;
375 unsigned hunk_in_pre_context
: 1;
378 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
380 struct blame_diff_state
*state
= state_
;
382 int off1
, off2
, len1
, len2
, num
;
384 num
= state
->ret
->num
;
385 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
386 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
387 state
->ret
->chunks
[num
- 1].same
++;
389 state
->hunk_in_pre_context
= 0;
391 state
->hunk_post_context
++;
393 state
->hunk_post_context
= 0;
398 if (num
&& state
->hunk_post_context
) {
399 chunk
= &state
->ret
->chunks
[num
- 1];
400 chunk
->p_next
-= state
->hunk_post_context
;
401 chunk
->t_next
-= state
->hunk_post_context
;
403 state
->ret
->num
= ++num
;
404 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
405 sizeof(struct chunk
) * num
);
406 chunk
= &state
->ret
->chunks
[num
- 1];
407 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
412 /* Line numbers in patch output are one based. */
416 chunk
->same
= len2
? off2
: (off2
+ 1);
418 chunk
->p_next
= off1
+ (len1
? len1
: 1);
419 chunk
->t_next
= chunk
->same
+ len2
;
420 state
->hunk_in_pre_context
= 1;
421 state
->hunk_post_context
= 0;
424 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
427 struct blame_diff_state state
;
432 xpp
.flags
= XDF_NEED_MINIMAL
;
433 xecfg
.ctxlen
= context
;
435 ecb
.outf
= xdiff_outf
;
437 memset(&state
, 0, sizeof(state
));
438 state
.xm
.consume
= process_u_diff
;
439 state
.ret
= xmalloc(sizeof(struct patch
));
440 state
.ret
->chunks
= NULL
;
443 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
445 if (state
.ret
->num
) {
447 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
448 chunk
->p_next
-= state
.hunk_post_context
;
449 chunk
->t_next
-= state
.hunk_post_context
;
454 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
456 mmfile_t file_p
, file_o
;
459 fill_origin_blob(parent
, &file_p
);
460 fill_origin_blob(origin
, &file_o
);
461 if (!file_p
.ptr
|| !file_o
.ptr
)
463 patch
= compare_buffer(&file_p
, &file_o
, 0);
468 static void free_patch(struct patch
*p
)
474 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
476 struct blame_entry
*ent
, *prev
= NULL
;
478 origin_incref(e
->suspect
);
480 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
483 /* prev, if not NULL, is the last one that is below e */
486 e
->next
= prev
->next
;
497 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
499 struct blame_entry
*p
, *n
;
503 origin_incref(src
->suspect
);
504 origin_decref(dst
->suspect
);
505 memcpy(dst
, src
, sizeof(*src
));
511 static const char *nth_line(struct scoreboard
*sb
, int lno
)
513 return sb
->final_buf
+ sb
->lineno
[lno
];
516 static void split_overlap(struct blame_entry
*split
,
517 struct blame_entry
*e
,
518 int tlno
, int plno
, int same
,
519 struct origin
*parent
)
521 /* it is known that lines between tlno to same came from
522 * parent, and e has an overlap with that range. it also is
523 * known that parent's line plno corresponds to e's line tlno.
529 * <------------------>
531 * Potentially we need to split e into three parts; before
532 * this chunk, the chunk to be blamed for parent, and after
536 memset(split
, 0, sizeof(struct blame_entry
[3]));
538 if (e
->s_lno
< tlno
) {
539 /* there is a pre-chunk part not blamed on parent */
540 split
[0].suspect
= origin_incref(e
->suspect
);
541 split
[0].lno
= e
->lno
;
542 split
[0].s_lno
= e
->s_lno
;
543 split
[0].num_lines
= tlno
- e
->s_lno
;
544 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
545 split
[1].s_lno
= plno
;
548 split
[1].lno
= e
->lno
;
549 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
552 if (same
< e
->s_lno
+ e
->num_lines
) {
553 /* there is a post-chunk part not blamed on parent */
554 split
[2].suspect
= origin_incref(e
->suspect
);
555 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
556 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
557 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
558 chunk_end_lno
= split
[2].lno
;
561 chunk_end_lno
= e
->lno
+ e
->num_lines
;
562 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
564 if (split
[1].num_lines
< 1)
566 split
[1].suspect
= origin_incref(parent
);
569 static void split_blame(struct scoreboard
*sb
,
570 struct blame_entry
*split
,
571 struct blame_entry
*e
)
573 struct blame_entry
*new_entry
;
575 if (split
[0].suspect
&& split
[2].suspect
) {
576 /* we need to split e into two and add another for parent */
577 dup_entry(e
, &split
[0]);
579 new_entry
= xmalloc(sizeof(*new_entry
));
580 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
581 add_blame_entry(sb
, new_entry
);
583 new_entry
= xmalloc(sizeof(*new_entry
));
584 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
585 add_blame_entry(sb
, new_entry
);
587 else if (!split
[0].suspect
&& !split
[2].suspect
)
588 /* parent covers the entire area */
589 dup_entry(e
, &split
[1]);
590 else if (split
[0].suspect
) {
591 dup_entry(e
, &split
[0]);
593 new_entry
= xmalloc(sizeof(*new_entry
));
594 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
595 add_blame_entry(sb
, new_entry
);
598 dup_entry(e
, &split
[1]);
600 new_entry
= xmalloc(sizeof(*new_entry
));
601 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
602 add_blame_entry(sb
, new_entry
);
605 if (DEBUG
) { /* sanity */
606 struct blame_entry
*ent
;
607 int lno
= sb
->ent
->lno
, corrupt
= 0;
609 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
614 lno
+= ent
->num_lines
;
618 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
619 printf("L %8d l %8d n %8d\n",
620 lno
, ent
->lno
, ent
->num_lines
);
621 lno
= ent
->lno
+ ent
->num_lines
;
628 static void decref_split(struct blame_entry
*split
)
632 for (i
= 0; i
< 3; i
++)
633 origin_decref(split
[i
].suspect
);
636 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
637 int tlno
, int plno
, int same
,
638 struct origin
*parent
)
640 struct blame_entry split
[3];
642 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
643 if (split
[1].suspect
)
644 split_blame(sb
, split
, e
);
648 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
650 struct blame_entry
*e
;
651 int last_in_target
= -1;
653 for (e
= sb
->ent
; e
; e
= e
->next
) {
654 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
656 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
657 last_in_target
= e
->s_lno
+ e
->num_lines
;
659 return last_in_target
;
662 static void blame_chunk(struct scoreboard
*sb
,
663 int tlno
, int plno
, int same
,
664 struct origin
*target
, struct origin
*parent
)
666 struct blame_entry
*e
;
668 for (e
= sb
->ent
; e
; e
= e
->next
) {
669 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
671 if (same
<= e
->s_lno
)
673 if (tlno
< e
->s_lno
+ e
->num_lines
)
674 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
678 static int pass_blame_to_parent(struct scoreboard
*sb
,
679 struct origin
*target
,
680 struct origin
*parent
)
682 int i
, last_in_target
, plno
, tlno
;
685 last_in_target
= find_last_in_target(sb
, target
);
686 if (last_in_target
< 0)
687 return 1; /* nothing remains for this target */
689 patch
= get_patch(parent
, target
);
691 for (i
= 0; i
< patch
->num
; i
++) {
692 struct chunk
*chunk
= &patch
->chunks
[i
];
694 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
695 plno
= chunk
->p_next
;
696 tlno
= chunk
->t_next
;
698 /* rest (i.e. anything above tlno) are the same as parent */
699 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
705 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
714 cp
= nth_line(sb
, e
->lno
);
715 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
717 unsigned ch
= *((unsigned char *)cp
);
726 static void copy_split_if_better(struct scoreboard
*sb
,
727 struct blame_entry
*best_so_far
,
728 struct blame_entry
*this)
732 if (!this[1].suspect
)
734 if (best_so_far
[1].suspect
) {
735 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
739 for (i
= 0; i
< 3; i
++)
740 origin_incref(this[i
].suspect
);
741 decref_split(best_so_far
);
742 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
745 static void find_copy_in_blob(struct scoreboard
*sb
,
746 struct blame_entry
*ent
,
747 struct origin
*parent
,
748 struct blame_entry
*split
,
757 cp
= nth_line(sb
, ent
->lno
);
758 file_o
.ptr
= (char*) cp
;
759 cnt
= ent
->num_lines
;
761 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
765 file_o
.size
= cp
- file_o
.ptr
;
767 patch
= compare_buffer(file_p
, &file_o
, 1);
769 memset(split
, 0, sizeof(struct blame_entry
[3]));
771 for (i
= 0; i
< patch
->num
; i
++) {
772 struct chunk
*chunk
= &patch
->chunks
[i
];
774 /* tlno to chunk->same are the same as ent */
775 if (ent
->num_lines
<= tlno
)
777 if (tlno
< chunk
->same
) {
778 struct blame_entry
this[3];
779 split_overlap(this, ent
,
780 tlno
+ ent
->s_lno
, plno
,
781 chunk
->same
+ ent
->s_lno
,
783 copy_split_if_better(sb
, split
, this);
786 plno
= chunk
->p_next
;
787 tlno
= chunk
->t_next
;
792 static int find_move_in_parent(struct scoreboard
*sb
,
793 struct origin
*target
,
794 struct origin
*parent
)
796 int last_in_target
, made_progress
;
797 struct blame_entry
*e
, split
[3];
800 last_in_target
= find_last_in_target(sb
, target
);
801 if (last_in_target
< 0)
802 return 1; /* nothing remains for this target */
804 fill_origin_blob(parent
, &file_p
);
809 while (made_progress
) {
811 for (e
= sb
->ent
; e
; e
= e
->next
) {
812 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
814 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
815 if (split
[1].suspect
&&
816 blame_move_score
< ent_score(sb
, &split
[1])) {
817 split_blame(sb
, split
, e
);
828 struct blame_entry
*ent
;
829 struct blame_entry split
[3];
832 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
833 struct origin
*target
,
836 struct blame_entry
*e
;
838 struct blame_list
*blame_list
= NULL
;
840 /* Count the number of entries the target is suspected for,
841 * and prepare a list of entry and the best split.
843 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
844 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
847 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
848 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
849 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
850 blame_list
[i
++].ent
= e
;
852 *num_ents_p
= num_ents
;
856 static int find_copy_in_parent(struct scoreboard
*sb
,
857 struct origin
*target
,
858 struct commit
*parent
,
859 struct origin
*porigin
,
862 struct diff_options diff_opts
;
863 const char *paths
[1];
866 struct blame_list
*blame_list
;
869 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
871 return 1; /* nothing remains for this target */
873 diff_setup(&diff_opts
);
874 diff_opts
.recursive
= 1;
875 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
878 diff_tree_setup_paths(paths
, &diff_opts
);
879 if (diff_setup_done(&diff_opts
) < 0)
882 /* Try "find copies harder" on new path if requested;
883 * we do not want to use diffcore_rename() actually to
884 * match things up; find_copies_harder is set only to
885 * force diff_tree_sha1() to feed all filepairs to diff_queue,
886 * and this code needs to be after diff_setup_done(), which
887 * usually makes find-copies-harder imply copy detection.
889 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
890 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
891 diff_opts
.find_copies_harder
= 1;
893 diff_tree_sha1(parent
->tree
->object
.sha1
,
894 target
->commit
->tree
->object
.sha1
,
897 if (!diff_opts
.find_copies_harder
)
898 diffcore_std(&diff_opts
);
902 int made_progress
= 0;
904 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
905 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
906 struct origin
*norigin
;
908 struct blame_entry
this[3];
910 if (!DIFF_FILE_VALID(p
->one
))
911 continue; /* does not exist in parent */
912 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
913 /* find_move already dealt with this path */
916 norigin
= get_origin(sb
, parent
, p
->one
->path
);
917 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
918 fill_origin_blob(norigin
, &file_p
);
922 for (j
= 0; j
< num_ents
; j
++) {
923 find_copy_in_blob(sb
, blame_list
[j
].ent
,
924 norigin
, this, &file_p
);
925 copy_split_if_better(sb
, blame_list
[j
].split
,
929 origin_decref(norigin
);
932 for (j
= 0; j
< num_ents
; j
++) {
933 struct blame_entry
*split
= blame_list
[j
].split
;
934 if (split
[1].suspect
&&
935 blame_copy_score
< ent_score(sb
, &split
[1])) {
936 split_blame(sb
, split
, blame_list
[j
].ent
);
945 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
951 diff_flush(&diff_opts
);
956 /* The blobs of origin and porigin exactly match, so everything
957 * origin is suspected for can be blamed on the parent.
959 static void pass_whole_blame(struct scoreboard
*sb
,
960 struct origin
*origin
, struct origin
*porigin
)
962 struct blame_entry
*e
;
964 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
966 porigin
->file
= origin
->file
;
967 origin
->file
.ptr
= NULL
;
969 for (e
= sb
->ent
; e
; e
= e
->next
) {
970 if (cmp_suspect(e
->suspect
, origin
))
972 origin_incref(porigin
);
973 origin_decref(e
->suspect
);
974 e
->suspect
= porigin
;
980 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
983 struct commit
*commit
= origin
->commit
;
984 struct commit_list
*parent
;
985 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
987 memset(parent_origin
, 0, sizeof(parent_origin
));
989 /* The first pass looks for unrenamed path to optimize for
990 * common cases, then we look for renames in the second pass.
992 for (pass
= 0; pass
< 2; pass
++) {
993 struct origin
*(*find
)(struct scoreboard
*,
994 struct commit
*, struct origin
*);
995 find
= pass
? find_rename
: find_origin
;
997 for (i
= 0, parent
= commit
->parents
;
998 i
< MAXPARENT
&& parent
;
999 parent
= parent
->next
, i
++) {
1000 struct commit
*p
= parent
->item
;
1003 if (parent_origin
[i
])
1005 if (parse_commit(p
))
1007 porigin
= find(sb
, p
, origin
);
1010 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1011 pass_whole_blame(sb
, origin
, porigin
);
1012 origin_decref(porigin
);
1015 for (j
= same
= 0; j
< i
; j
++)
1016 if (parent_origin
[j
] &&
1017 !hashcmp(parent_origin
[j
]->blob_sha1
,
1018 porigin
->blob_sha1
)) {
1023 parent_origin
[i
] = porigin
;
1025 origin_decref(porigin
);
1030 for (i
= 0, parent
= commit
->parents
;
1031 i
< MAXPARENT
&& parent
;
1032 parent
= parent
->next
, i
++) {
1033 struct origin
*porigin
= parent_origin
[i
];
1036 if (pass_blame_to_parent(sb
, origin
, porigin
))
1041 * Optionally run "miff" to find moves in parents' files here.
1043 if (opt
& PICKAXE_BLAME_MOVE
)
1044 for (i
= 0, parent
= commit
->parents
;
1045 i
< MAXPARENT
&& parent
;
1046 parent
= parent
->next
, i
++) {
1047 struct origin
*porigin
= parent_origin
[i
];
1050 if (find_move_in_parent(sb
, origin
, porigin
))
1055 * Optionally run "ciff" to find copies from parents' files here.
1057 if (opt
& PICKAXE_BLAME_COPY
)
1058 for (i
= 0, parent
= commit
->parents
;
1059 i
< MAXPARENT
&& parent
;
1060 parent
= parent
->next
, i
++) {
1061 struct origin
*porigin
= parent_origin
[i
];
1062 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1068 for (i
= 0; i
< MAXPARENT
; i
++)
1069 origin_decref(parent_origin
[i
]);
1072 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1075 struct blame_entry
*ent
;
1076 struct commit
*commit
;
1077 struct origin
*suspect
= NULL
;
1079 /* find one suspect to break down */
1080 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1082 suspect
= ent
->suspect
;
1084 return; /* all done */
1086 origin_incref(suspect
);
1087 commit
= suspect
->commit
;
1088 if (!commit
->object
.parsed
)
1089 parse_commit(commit
);
1090 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1091 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1092 pass_blame(sb
, suspect
, opt
);
1094 commit
->object
.flags
|= UNINTERESTING
;
1095 if (commit
->object
.parsed
)
1096 mark_parents_uninteresting(commit
);
1098 /* treat root commit as boundary */
1099 if (!commit
->parents
&& !show_root
)
1100 commit
->object
.flags
|= UNINTERESTING
;
1102 /* Take responsibility for the remaining entries */
1103 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1104 if (!cmp_suspect(ent
->suspect
, suspect
))
1106 origin_decref(suspect
);
1108 if (DEBUG
) /* sanity */
1109 sanity_check_refcnt(sb
);
1113 static const char *format_time(unsigned long time
, const char *tz_str
,
1116 static char time_buf
[128];
1121 if (show_raw_time
) {
1122 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1127 minutes
= tz
< 0 ? -tz
: tz
;
1128 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1129 minutes
= tz
< 0 ? -minutes
: minutes
;
1130 t
= time
+ minutes
* 60;
1133 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1134 strcat(time_buf
, tz_str
);
1142 unsigned long author_time
;
1145 /* filled only when asked for details */
1147 char *committer_mail
;
1148 unsigned long committer_time
;
1154 static void get_ac_line(const char *inbuf
, const char *what
,
1155 int bufsz
, char *person
, char **mail
,
1156 unsigned long *time
, char **tz
)
1161 tmp
= strstr(inbuf
, what
);
1164 tmp
+= strlen(what
);
1165 endp
= strchr(tmp
, '\n');
1173 person
= *mail
= *tz
= "(unknown)";
1177 memcpy(person
, tmp
, len
);
1189 *time
= strtoul(tmp
, NULL
, 10);
1198 static void get_commit_info(struct commit
*commit
,
1199 struct commit_info
*ret
,
1204 static char author_buf
[1024];
1205 static char committer_buf
[1024];
1206 static char summary_buf
[1024];
1208 /* We've operated without save_commit_buffer, so
1209 * we now need to populate them for output.
1211 if (!commit
->buffer
) {
1215 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1217 ret
->author
= author_buf
;
1218 get_ac_line(commit
->buffer
, "\nauthor ",
1219 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1220 &ret
->author_time
, &ret
->author_tz
);
1225 ret
->committer
= committer_buf
;
1226 get_ac_line(commit
->buffer
, "\ncommitter ",
1227 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1228 &ret
->committer_time
, &ret
->committer_tz
);
1230 ret
->summary
= summary_buf
;
1231 tmp
= strstr(commit
->buffer
, "\n\n");
1234 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1238 endp
= strchr(tmp
, '\n');
1242 if (len
>= sizeof(summary_buf
))
1244 memcpy(summary_buf
, tmp
, len
);
1245 summary_buf
[len
] = 0;
1248 #define OUTPUT_ANNOTATE_COMPAT 001
1249 #define OUTPUT_LONG_OBJECT_NAME 002
1250 #define OUTPUT_RAW_TIMESTAMP 004
1251 #define OUTPUT_PORCELAIN 010
1252 #define OUTPUT_SHOW_NAME 020
1253 #define OUTPUT_SHOW_NUMBER 040
1254 #define OUTPUT_SHOW_SCORE 0100
1256 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1260 struct origin
*suspect
= ent
->suspect
;
1263 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1264 printf("%s%c%d %d %d\n",
1266 ent
->guilty
? ' ' : '*', // purely for debugging
1270 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1271 struct commit_info ci
;
1272 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1273 get_commit_info(suspect
->commit
, &ci
, 1);
1274 printf("author %s\n", ci
.author
);
1275 printf("author-mail %s\n", ci
.author_mail
);
1276 printf("author-time %lu\n", ci
.author_time
);
1277 printf("author-tz %s\n", ci
.author_tz
);
1278 printf("committer %s\n", ci
.committer
);
1279 printf("committer-mail %s\n", ci
.committer_mail
);
1280 printf("committer-time %lu\n", ci
.committer_time
);
1281 printf("committer-tz %s\n", ci
.committer_tz
);
1282 printf("filename %s\n", suspect
->path
);
1283 printf("summary %s\n", ci
.summary
);
1284 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1285 printf("boundary\n");
1287 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1288 printf("filename %s\n", suspect
->path
);
1290 cp
= nth_line(sb
, ent
->lno
);
1291 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1294 printf("%s %d %d\n", hex
,
1295 ent
->s_lno
+ 1 + cnt
,
1296 ent
->lno
+ 1 + cnt
);
1301 } while (ch
!= '\n' &&
1302 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1306 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1310 struct origin
*suspect
= ent
->suspect
;
1311 struct commit_info ci
;
1313 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1315 get_commit_info(suspect
->commit
, &ci
, 1);
1316 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1318 cp
= nth_line(sb
, ent
->lno
);
1319 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1321 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1323 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1324 if (!blank_boundary
) {
1329 memset(hex
, ' ', length
);
1332 printf("%.*s", length
, hex
);
1333 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1334 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1335 format_time(ci
.author_time
, ci
.author_tz
,
1337 ent
->lno
+ 1 + cnt
);
1339 if (opt
& OUTPUT_SHOW_SCORE
)
1341 max_score_digits
, ent
->score
,
1342 ent
->suspect
->refcnt
);
1343 if (opt
& OUTPUT_SHOW_NAME
)
1344 printf(" %-*.*s", longest_file
, longest_file
,
1346 if (opt
& OUTPUT_SHOW_NUMBER
)
1347 printf(" %*d", max_orig_digits
,
1348 ent
->s_lno
+ 1 + cnt
);
1349 printf(" (%-*.*s %10s %*d) ",
1350 longest_author
, longest_author
, ci
.author
,
1351 format_time(ci
.author_time
, ci
.author_tz
,
1353 max_digits
, ent
->lno
+ 1 + cnt
);
1358 } while (ch
!= '\n' &&
1359 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1363 static void output(struct scoreboard
*sb
, int option
)
1365 struct blame_entry
*ent
;
1367 if (option
& OUTPUT_PORCELAIN
) {
1368 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1369 struct blame_entry
*oth
;
1370 struct origin
*suspect
= ent
->suspect
;
1371 struct commit
*commit
= suspect
->commit
;
1372 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1374 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1375 if ((oth
->suspect
->commit
!= commit
) ||
1376 !strcmp(oth
->suspect
->path
, suspect
->path
))
1378 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1384 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1385 if (option
& OUTPUT_PORCELAIN
)
1386 emit_porcelain(sb
, ent
);
1388 emit_other(sb
, ent
, option
);
1393 static int prepare_lines(struct scoreboard
*sb
)
1395 const char *buf
= sb
->final_buf
;
1396 unsigned long len
= sb
->final_buf_size
;
1397 int num
= 0, incomplete
= 0, bol
= 1;
1399 if (len
&& buf
[len
-1] != '\n')
1400 incomplete
++; /* incomplete line at the end */
1403 sb
->lineno
= xrealloc(sb
->lineno
,
1404 sizeof(int* ) * (num
+ 1));
1405 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1408 if (*buf
++ == '\n') {
1413 sb
->lineno
= xrealloc(sb
->lineno
,
1414 sizeof(int* ) * (num
+ incomplete
+ 1));
1415 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1416 sb
->num_lines
= num
+ incomplete
;
1417 return sb
->num_lines
;
1420 static int read_ancestry(const char *graft_file
)
1422 FILE *fp
= fopen(graft_file
, "r");
1426 while (fgets(buf
, sizeof(buf
), fp
)) {
1427 /* The format is just "Commit Parent1 Parent2 ...\n" */
1428 int len
= strlen(buf
);
1429 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1431 register_commit_graft(graft
, 0);
1437 static int lineno_width(int lines
)
1441 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1446 static void find_alignment(struct scoreboard
*sb
, int *option
)
1448 int longest_src_lines
= 0;
1449 int longest_dst_lines
= 0;
1450 unsigned largest_score
= 0;
1451 struct blame_entry
*e
;
1453 for (e
= sb
->ent
; e
; e
= e
->next
) {
1454 struct origin
*suspect
= e
->suspect
;
1455 struct commit_info ci
;
1458 if (strcmp(suspect
->path
, sb
->path
))
1459 *option
|= OUTPUT_SHOW_NAME
;
1460 num
= strlen(suspect
->path
);
1461 if (longest_file
< num
)
1463 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1464 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1465 get_commit_info(suspect
->commit
, &ci
, 1);
1466 num
= strlen(ci
.author
);
1467 if (longest_author
< num
)
1468 longest_author
= num
;
1470 num
= e
->s_lno
+ e
->num_lines
;
1471 if (longest_src_lines
< num
)
1472 longest_src_lines
= num
;
1473 num
= e
->lno
+ e
->num_lines
;
1474 if (longest_dst_lines
< num
)
1475 longest_dst_lines
= num
;
1476 if (largest_score
< ent_score(sb
, e
))
1477 largest_score
= ent_score(sb
, e
);
1479 max_orig_digits
= lineno_width(longest_src_lines
);
1480 max_digits
= lineno_width(longest_dst_lines
);
1481 max_score_digits
= lineno_width(largest_score
);
1484 static void sanity_check_refcnt(struct scoreboard
*sb
)
1487 struct blame_entry
*ent
;
1489 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1490 /* Nobody should have zero or negative refcnt */
1491 if (ent
->suspect
->refcnt
<= 0) {
1492 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1494 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1495 ent
->suspect
->refcnt
);
1499 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1500 /* Mark the ones that haven't been checked */
1501 if (0 < ent
->suspect
->refcnt
)
1502 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1504 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1505 /* then pick each and see if they have the the correct
1509 struct blame_entry
*e
;
1510 struct origin
*suspect
= ent
->suspect
;
1512 if (0 < suspect
->refcnt
)
1514 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1515 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1516 if (e
->suspect
!= suspect
)
1520 if (suspect
->refcnt
!= found
) {
1521 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1523 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1524 ent
->suspect
->refcnt
, found
);
1530 find_alignment(sb
, &opt
);
1532 die("Baa %d!", baa
);
1536 static int has_path_in_work_tree(const char *path
)
1539 return !lstat(path
, &st
);
1542 static unsigned parse_score(const char *arg
)
1545 unsigned long score
= strtoul(arg
, &end
, 10);
1551 static const char *add_prefix(const char *prefix
, const char *path
)
1553 if (!prefix
|| !prefix
[0])
1555 return prefix_path(prefix
, strlen(prefix
), path
);
1558 static const char *parse_loc(const char *spec
,
1559 struct scoreboard
*sb
, long lno
,
1560 long begin
, long *ret
)
1567 regmatch_t match
[1];
1569 /* Allow "-L <something>,+20" to mean starting at <something>
1570 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1573 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1574 num
= strtol(spec
+ 1, &term
, 10);
1575 if (term
!= spec
+ 1) {
1579 *ret
= begin
+ num
- 2;
1588 num
= strtol(spec
, &term
, 10);
1596 /* it could be a regexp of form /.../ */
1597 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1604 /* try [spec+1 .. term-1] as regexp */
1606 begin
--; /* input is in human terms */
1607 line
= nth_line(sb
, begin
);
1609 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1610 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1611 const char *cp
= line
+ match
[0].rm_so
;
1614 while (begin
++ < lno
) {
1615 nline
= nth_line(sb
, begin
);
1616 if (line
<= cp
&& cp
< nline
)
1627 regerror(reg_error
, ®exp
, errbuf
, 1024);
1628 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1632 static void prepare_blame_range(struct scoreboard
*sb
,
1633 const char *bottomtop
,
1635 long *bottom
, long *top
)
1639 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1641 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1649 static int git_blame_config(const char *var
, const char *value
)
1651 if (!strcmp(var
, "blame.showroot")) {
1652 show_root
= git_config_bool(var
, value
);
1655 if (!strcmp(var
, "blame.blankboundary")) {
1656 blank_boundary
= git_config_bool(var
, value
);
1659 return git_default_config(var
, value
);
1662 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
1664 struct rev_info revs
;
1666 struct scoreboard sb
;
1668 struct blame_entry
*ent
;
1669 int i
, seen_dashdash
, unk
, opt
;
1670 long bottom
, top
, lno
;
1671 int output_option
= 0;
1672 const char *revs_file
= NULL
;
1673 const char *final_commit_name
= NULL
;
1675 const char *bottomtop
= NULL
;
1677 git_config(git_blame_config
);
1678 save_commit_buffer
= 0;
1682 for (unk
= i
= 1; i
< argc
; i
++) {
1683 const char *arg
= argv
[i
];
1686 else if (!strcmp("-b", arg
))
1688 else if (!strcmp("--root", arg
))
1690 else if (!strcmp("-c", arg
))
1691 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1692 else if (!strcmp("-t", arg
))
1693 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1694 else if (!strcmp("-l", arg
))
1695 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1696 else if (!strcmp("-S", arg
) && ++i
< argc
)
1697 revs_file
= argv
[i
];
1698 else if (!strncmp("-M", arg
, 2)) {
1699 opt
|= PICKAXE_BLAME_MOVE
;
1700 blame_move_score
= parse_score(arg
+2);
1702 else if (!strncmp("-C", arg
, 2)) {
1703 if (opt
& PICKAXE_BLAME_COPY
)
1704 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1705 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1706 blame_copy_score
= parse_score(arg
+2);
1708 else if (!strncmp("-L", arg
, 2)) {
1717 die("More than one '-L n,m' option given");
1720 else if (!strcmp("--score-debug", arg
))
1721 output_option
|= OUTPUT_SHOW_SCORE
;
1722 else if (!strcmp("-f", arg
) ||
1723 !strcmp("--show-name", arg
))
1724 output_option
|= OUTPUT_SHOW_NAME
;
1725 else if (!strcmp("-n", arg
) ||
1726 !strcmp("--show-number", arg
))
1727 output_option
|= OUTPUT_SHOW_NUMBER
;
1728 else if (!strcmp("-p", arg
) ||
1729 !strcmp("--porcelain", arg
))
1730 output_option
|= OUTPUT_PORCELAIN
;
1731 else if (!strcmp("--", arg
)) {
1740 if (!blame_move_score
)
1741 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1742 if (!blame_copy_score
)
1743 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1745 /* We have collected options unknown to us in argv[1..unk]
1746 * which are to be passed to revision machinery if we are
1747 * going to do the "bottom" procesing.
1749 * The remaining are:
1751 * (1) if seen_dashdash, its either
1752 * "-options -- <path>" or
1753 * "-options -- <path> <rev>".
1754 * but the latter is allowed only if there is no
1755 * options that we passed to revision machinery.
1757 * (2) otherwise, we may have "--" somewhere later and
1758 * might be looking at the first one of multiple 'rev'
1759 * parameters (e.g. " master ^next ^maint -- path").
1760 * See if there is a dashdash first, and give the
1761 * arguments before that to revision machinery.
1762 * After that there must be one 'path'.
1764 * (3) otherwise, its one of the three:
1765 * "-options <path> <rev>"
1766 * "-options <rev> <path>"
1768 * but again the first one is allowed only if
1769 * there is no options that we passed to revision
1773 if (seen_dashdash
) {
1777 path
= add_prefix(prefix
, argv
[i
]);
1778 if (i
+ 1 == argc
- 1) {
1781 argv
[unk
++] = argv
[i
+ 1];
1783 else if (i
+ 1 != argc
)
1784 /* garbage at end */
1789 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1790 if (!strcmp(argv
[j
], "--"))
1792 if (seen_dashdash
) {
1793 if (seen_dashdash
+ 1 != argc
- 1)
1795 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
1796 for (j
= i
; j
< seen_dashdash
; j
++)
1797 argv
[unk
++] = argv
[j
];
1801 path
= add_prefix(prefix
, argv
[i
]);
1802 if (i
+ 1 == argc
- 1) {
1803 final_commit_name
= argv
[i
+ 1];
1805 /* if (unk == 1) we could be getting
1808 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1809 path
= add_prefix(prefix
, argv
[i
+ 1]);
1810 final_commit_name
= argv
[i
];
1813 else if (i
!= argc
- 1)
1814 usage(blame_usage
); /* garbage at end */
1816 if (!has_path_in_work_tree(path
))
1817 die("cannot stat path %s: %s",
1818 path
, strerror(errno
));
1822 if (final_commit_name
)
1823 argv
[unk
++] = final_commit_name
;
1825 /* Now we got rev and path. We do not want the path pruning
1826 * but we may want "bottom" processing.
1828 argv
[unk
++] = "--"; /* terminate the rev name */
1831 init_revisions(&revs
, NULL
);
1832 setup_revisions(unk
, argv
, &revs
, "HEAD");
1833 memset(&sb
, 0, sizeof(sb
));
1835 /* There must be one and only one positive commit in the
1836 * revs->pending array.
1838 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1839 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1840 if (obj
->flags
& UNINTERESTING
)
1842 while (obj
->type
== OBJ_TAG
)
1843 obj
= deref_tag(obj
, NULL
, 0);
1844 if (obj
->type
!= OBJ_COMMIT
)
1845 die("Non commit %s?",
1846 revs
.pending
.objects
[i
].name
);
1848 die("More than one commit to dig from %s and %s?",
1849 revs
.pending
.objects
[i
].name
,
1851 sb
.final
= (struct commit
*) obj
;
1852 final_commit_name
= revs
.pending
.objects
[i
].name
;
1856 /* "--not A B -- path" without anything positive */
1857 unsigned char head_sha1
[20];
1859 final_commit_name
= "HEAD";
1860 if (get_sha1(final_commit_name
, head_sha1
))
1861 die("No such ref: HEAD");
1862 sb
.final
= lookup_commit_reference(head_sha1
);
1863 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1866 /* If we have bottom, this will mark the ancestors of the
1867 * bottom commits we would reach while traversing as
1870 prepare_revision_walk(&revs
);
1872 o
= get_origin(&sb
, sb
.final
, path
);
1873 if (fill_blob_sha1(o
))
1874 die("no such path %s in %s", path
, final_commit_name
);
1876 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1878 lno
= prepare_lines(&sb
);
1882 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
1883 if (bottom
&& top
&& top
< bottom
) {
1885 tmp
= top
; top
= bottom
; bottom
= tmp
;
1893 die("file %s has only %lu lines", path
, lno
);
1895 ent
= xcalloc(1, sizeof(*ent
));
1897 ent
->num_lines
= top
- bottom
;
1899 ent
->s_lno
= bottom
;
1904 if (revs_file
&& read_ancestry(revs_file
))
1905 die("reading graft file %s failed: %s",
1906 revs_file
, strerror(errno
));
1908 assign_blame(&sb
, &revs
, opt
);
1912 if (!(output_option
& OUTPUT_PORCELAIN
))
1913 find_alignment(&sb
, &output_option
);
1915 output(&sb
, output_option
);
1916 free((void *)sb
.final_buf
);
1917 for (ent
= sb
.ent
; ent
; ) {
1918 struct blame_entry
*e
= ent
->next
;
1924 printf("num read blob: %d\n", num_read_blob
);
1925 printf("num get patch: %d\n", num_get_patch
);
1926 printf("num commits: %d\n", num_commits
);