4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
16 #include "xdiff-interface.h"
21 static char pickaxe_usage
[] =
22 "git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
23 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
24 " -l, --long Show long commit SHA1 (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 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
33 static int longest_file
;
34 static int longest_author
;
35 static int max_orig_digits
;
36 static int max_digits
;
37 static int max_score_digits
;
44 static int num_read_blob
;
45 static int num_get_patch
;
46 static int num_commits
;
48 #define PICKAXE_BLAME_MOVE 01
49 #define PICKAXE_BLAME_COPY 02
50 #define PICKAXE_BLAME_COPY_HARDER 04
53 * blame for a blame_entry with score lower than these thresholds
54 * is not passed to the parent using move/copy logic.
56 static unsigned blame_move_score
;
57 static unsigned blame_copy_score
;
58 #define BLAME_DEFAULT_MOVE_SCORE 20
59 #define BLAME_DEFAULT_COPY_SCORE 40
61 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
62 #define METAINFO_SHOWN (1u<<12)
63 #define MORE_THAN_ONE_PATH (1u<<13)
66 * One blob in a commit that is being suspected
70 struct commit
*commit
;
72 unsigned char blob_sha1
[20];
73 char path
[FLEX_ARRAY
];
76 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
81 file
->ptr
= read_sha1_file(o
->blob_sha1
, type
,
82 (unsigned long *)(&(file
->size
)));
90 static inline struct origin
*origin_incref(struct origin
*o
)
97 static void origin_decref(struct origin
*o
)
99 if (o
&& --o
->refcnt
<= 0) {
102 memset(o
, 0, sizeof(*o
));
108 struct blame_entry
*prev
;
109 struct blame_entry
*next
;
111 /* the first line of this group in the final image;
112 * internally all line numbers are 0 based.
116 /* how many lines this group has */
119 /* the commit that introduced this group into the final image */
120 struct origin
*suspect
;
122 /* true if the suspect is truly guilty; false while we have not
123 * checked if the group came from one of its parents.
127 /* the line number of the first line of this group in the
128 * suspect's file; internally all line numbers are 0 based.
132 /* how significant this entry is -- cached to avoid
133 * scanning the lines over and over
139 /* the final commit (i.e. where we started digging from) */
140 struct commit
*final
;
144 /* the contents in the final; pointed into by buf pointers of
147 const char *final_buf
;
148 unsigned long final_buf_size
;
150 /* linked list of blames */
151 struct blame_entry
*ent
;
153 /* look-up a line in the final buffer */
158 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
160 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
163 return strcmp(a
->path
, b
->path
);
166 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
168 static void sanity_check_refcnt(struct scoreboard
*);
170 static void coalesce(struct scoreboard
*sb
)
172 struct blame_entry
*ent
, *next
;
174 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
175 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
176 ent
->guilty
== next
->guilty
&&
177 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
178 ent
->num_lines
+= next
->num_lines
;
179 ent
->next
= next
->next
;
181 ent
->next
->prev
= ent
;
182 origin_decref(next
->suspect
);
185 next
= ent
; /* again */
189 if (DEBUG
) /* sanity */
190 sanity_check_refcnt(sb
);
193 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
196 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
199 strcpy(o
->path
, path
);
203 static struct origin
*get_origin(struct scoreboard
*sb
,
204 struct commit
*commit
,
207 struct blame_entry
*e
;
209 for (e
= sb
->ent
; e
; e
= e
->next
) {
210 if (e
->suspect
->commit
== commit
&&
211 !strcmp(e
->suspect
->path
, path
))
212 return origin_incref(e
->suspect
);
214 return make_origin(commit
, path
);
217 static int fill_blob_sha1(struct origin
*origin
)
222 if (!is_null_sha1(origin
->blob_sha1
))
224 if (get_tree_entry(origin
->commit
->object
.sha1
,
226 origin
->blob_sha1
, &mode
))
228 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
229 strcmp(type
, blob_type
))
233 hashclr(origin
->blob_sha1
);
237 static struct origin
*find_origin(struct scoreboard
*sb
,
238 struct commit
*parent
,
239 struct origin
*origin
)
241 struct origin
*porigin
= NULL
;
242 struct diff_options diff_opts
;
243 const char *paths
[2];
246 /* This is a freestanding copy of origin and not
249 struct origin
*cached
= parent
->util
;
250 if (!strcmp(cached
->path
, origin
->path
)) {
251 porigin
= get_origin(sb
, parent
, cached
->path
);
252 if (porigin
->refcnt
== 1)
253 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
256 /* otherwise it was not very useful; free it */
261 /* See if the origin->path is different between parent
262 * and origin first. Most of the time they are the
263 * same and diff-tree is fairly efficient about this.
265 diff_setup(&diff_opts
);
266 diff_opts
.recursive
= 1;
267 diff_opts
.detect_rename
= 0;
268 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
269 paths
[0] = origin
->path
;
272 diff_tree_setup_paths(paths
, &diff_opts
);
273 if (diff_setup_done(&diff_opts
) < 0)
275 diff_tree_sha1(parent
->tree
->object
.sha1
,
276 origin
->commit
->tree
->object
.sha1
,
278 diffcore_std(&diff_opts
);
280 /* It is either one entry that says "modified", or "created",
283 if (!diff_queued_diff
.nr
) {
284 /* The path is the same as parent */
285 porigin
= get_origin(sb
, parent
, origin
->path
);
286 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
288 else if (diff_queued_diff
.nr
!= 1)
289 die("internal error in pickaxe::find_origin");
291 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
294 die("internal error in pickaxe::find_origin (%c)",
297 porigin
= get_origin(sb
, parent
, origin
->path
);
298 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
302 /* Did not exist in parent, or type changed */
306 diff_flush(&diff_opts
);
308 struct origin
*cached
;
309 cached
= make_origin(porigin
->commit
, porigin
->path
);
310 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
311 parent
->util
= cached
;
316 static struct origin
*find_rename(struct scoreboard
*sb
,
317 struct commit
*parent
,
318 struct origin
*origin
)
320 struct origin
*porigin
= NULL
;
321 struct diff_options diff_opts
;
323 const char *paths
[2];
325 diff_setup(&diff_opts
);
326 diff_opts
.recursive
= 1;
327 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
328 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
329 diff_opts
.single_follow
= origin
->path
;
331 diff_tree_setup_paths(paths
, &diff_opts
);
332 if (diff_setup_done(&diff_opts
) < 0)
334 diff_tree_sha1(parent
->tree
->object
.sha1
,
335 origin
->commit
->tree
->object
.sha1
,
337 diffcore_std(&diff_opts
);
339 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
340 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
341 if ((p
->status
== 'R' || p
->status
== 'C') &&
342 !strcmp(p
->two
->path
, origin
->path
)) {
343 porigin
= get_origin(sb
, parent
, p
->one
->path
);
344 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
348 diff_flush(&diff_opts
);
353 /* line number in postimage; up to but not including this
354 * line is the same as preimage
358 /* preimage line number after this chunk */
361 /* postimage line number after this chunk */
366 struct chunk
*chunks
;
370 struct blame_diff_state
{
371 struct xdiff_emit_state xm
;
373 unsigned hunk_post_context
;
374 unsigned hunk_in_pre_context
: 1;
377 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
379 struct blame_diff_state
*state
= state_
;
381 int off1
, off2
, len1
, len2
, num
;
383 num
= state
->ret
->num
;
384 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
385 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
386 state
->ret
->chunks
[num
- 1].same
++;
388 state
->hunk_in_pre_context
= 0;
390 state
->hunk_post_context
++;
392 state
->hunk_post_context
= 0;
397 if (num
&& state
->hunk_post_context
) {
398 chunk
= &state
->ret
->chunks
[num
- 1];
399 chunk
->p_next
-= state
->hunk_post_context
;
400 chunk
->t_next
-= state
->hunk_post_context
;
402 state
->ret
->num
= ++num
;
403 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
404 sizeof(struct chunk
) * num
);
405 chunk
= &state
->ret
->chunks
[num
- 1];
406 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
411 /* Line numbers in patch output are one based. */
415 chunk
->same
= len2
? off2
: (off2
+ 1);
417 chunk
->p_next
= off1
+ (len1
? len1
: 1);
418 chunk
->t_next
= chunk
->same
+ len2
;
419 state
->hunk_in_pre_context
= 1;
420 state
->hunk_post_context
= 0;
423 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
426 struct blame_diff_state state
;
431 xpp
.flags
= XDF_NEED_MINIMAL
;
432 xecfg
.ctxlen
= context
;
434 ecb
.outf
= xdiff_outf
;
436 memset(&state
, 0, sizeof(state
));
437 state
.xm
.consume
= process_u_diff
;
438 state
.ret
= xmalloc(sizeof(struct patch
));
439 state
.ret
->chunks
= NULL
;
442 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
444 if (state
.ret
->num
) {
446 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
447 chunk
->p_next
-= state
.hunk_post_context
;
448 chunk
->t_next
-= state
.hunk_post_context
;
453 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
455 mmfile_t file_p
, file_o
;
458 fill_origin_blob(parent
, &file_p
);
459 fill_origin_blob(origin
, &file_o
);
460 if (!file_p
.ptr
|| !file_o
.ptr
)
462 patch
= compare_buffer(&file_p
, &file_o
, 0);
467 static void free_patch(struct patch
*p
)
473 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
475 struct blame_entry
*ent
, *prev
= NULL
;
477 origin_incref(e
->suspect
);
479 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
482 /* prev, if not NULL, is the last one that is below e */
485 e
->next
= prev
->next
;
496 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
498 struct blame_entry
*p
, *n
;
502 origin_incref(src
->suspect
);
503 origin_decref(dst
->suspect
);
504 memcpy(dst
, src
, sizeof(*src
));
510 static const char *nth_line(struct scoreboard
*sb
, int lno
)
512 return sb
->final_buf
+ sb
->lineno
[lno
];
515 static void split_overlap(struct blame_entry
*split
,
516 struct blame_entry
*e
,
517 int tlno
, int plno
, int same
,
518 struct origin
*parent
)
520 /* it is known that lines between tlno to same came from
521 * parent, and e has an overlap with that range. it also is
522 * known that parent's line plno corresponds to e's line tlno.
528 * <------------------>
530 * Potentially we need to split e into three parts; before
531 * this chunk, the chunk to be blamed for parent, and after
535 memset(split
, 0, sizeof(struct blame_entry
[3]));
537 if (e
->s_lno
< tlno
) {
538 /* there is a pre-chunk part not blamed on parent */
539 split
[0].suspect
= origin_incref(e
->suspect
);
540 split
[0].lno
= e
->lno
;
541 split
[0].s_lno
= e
->s_lno
;
542 split
[0].num_lines
= tlno
- e
->s_lno
;
543 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
544 split
[1].s_lno
= plno
;
547 split
[1].lno
= e
->lno
;
548 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
551 if (same
< e
->s_lno
+ e
->num_lines
) {
552 /* there is a post-chunk part not blamed on parent */
553 split
[2].suspect
= origin_incref(e
->suspect
);
554 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
555 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
556 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
557 chunk_end_lno
= split
[2].lno
;
560 chunk_end_lno
= e
->lno
+ e
->num_lines
;
561 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
563 if (split
[1].num_lines
< 1)
565 split
[1].suspect
= origin_incref(parent
);
568 static void split_blame(struct scoreboard
*sb
,
569 struct blame_entry
*split
,
570 struct blame_entry
*e
)
572 struct blame_entry
*new_entry
;
574 if (split
[0].suspect
&& split
[2].suspect
) {
575 /* we need to split e into two and add another for parent */
576 dup_entry(e
, &split
[0]);
578 new_entry
= xmalloc(sizeof(*new_entry
));
579 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
580 add_blame_entry(sb
, new_entry
);
582 new_entry
= xmalloc(sizeof(*new_entry
));
583 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
584 add_blame_entry(sb
, new_entry
);
586 else if (!split
[0].suspect
&& !split
[2].suspect
)
587 /* parent covers the entire area */
588 dup_entry(e
, &split
[1]);
589 else if (split
[0].suspect
) {
590 dup_entry(e
, &split
[0]);
592 new_entry
= xmalloc(sizeof(*new_entry
));
593 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
594 add_blame_entry(sb
, new_entry
);
597 dup_entry(e
, &split
[1]);
599 new_entry
= xmalloc(sizeof(*new_entry
));
600 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
601 add_blame_entry(sb
, new_entry
);
604 if (DEBUG
) { /* sanity */
605 struct blame_entry
*ent
;
606 int lno
= sb
->ent
->lno
, corrupt
= 0;
608 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
613 lno
+= ent
->num_lines
;
617 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
618 printf("L %8d l %8d n %8d\n",
619 lno
, ent
->lno
, ent
->num_lines
);
620 lno
= ent
->lno
+ ent
->num_lines
;
627 static void decref_split(struct blame_entry
*split
)
631 for (i
= 0; i
< 3; i
++)
632 origin_decref(split
[i
].suspect
);
635 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
636 int tlno
, int plno
, int same
,
637 struct origin
*parent
)
639 struct blame_entry split
[3];
641 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
642 if (split
[1].suspect
)
643 split_blame(sb
, split
, e
);
647 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
649 struct blame_entry
*e
;
650 int last_in_target
= -1;
652 for (e
= sb
->ent
; e
; e
= e
->next
) {
653 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
655 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
656 last_in_target
= e
->s_lno
+ e
->num_lines
;
658 return last_in_target
;
661 static void blame_chunk(struct scoreboard
*sb
,
662 int tlno
, int plno
, int same
,
663 struct origin
*target
, struct origin
*parent
)
665 struct blame_entry
*e
;
667 for (e
= sb
->ent
; e
; e
= e
->next
) {
668 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
670 if (same
<= e
->s_lno
)
672 if (tlno
< e
->s_lno
+ e
->num_lines
)
673 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
677 static int pass_blame_to_parent(struct scoreboard
*sb
,
678 struct origin
*target
,
679 struct origin
*parent
)
681 int i
, last_in_target
, plno
, tlno
;
684 last_in_target
= find_last_in_target(sb
, target
);
685 if (last_in_target
< 0)
686 return 1; /* nothing remains for this target */
688 patch
= get_patch(parent
, target
);
690 for (i
= 0; i
< patch
->num
; i
++) {
691 struct chunk
*chunk
= &patch
->chunks
[i
];
693 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
694 plno
= chunk
->p_next
;
695 tlno
= chunk
->t_next
;
697 /* rest (i.e. anything above tlno) are the same as parent */
698 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
704 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
713 cp
= nth_line(sb
, e
->lno
);
714 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
716 unsigned ch
= *((unsigned char *)cp
);
725 static void copy_split_if_better(struct scoreboard
*sb
,
726 struct blame_entry
*best_so_far
,
727 struct blame_entry
*this)
731 if (!this[1].suspect
)
733 if (best_so_far
[1].suspect
) {
734 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
738 for (i
= 0; i
< 3; i
++)
739 origin_incref(this[i
].suspect
);
740 decref_split(best_so_far
);
741 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
744 static void find_copy_in_blob(struct scoreboard
*sb
,
745 struct blame_entry
*ent
,
746 struct origin
*parent
,
747 struct blame_entry
*split
,
756 cp
= nth_line(sb
, ent
->lno
);
757 file_o
.ptr
= (char*) cp
;
758 cnt
= ent
->num_lines
;
760 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
764 file_o
.size
= cp
- file_o
.ptr
;
766 patch
= compare_buffer(file_p
, &file_o
, 1);
768 memset(split
, 0, sizeof(struct blame_entry
[3]));
770 for (i
= 0; i
< patch
->num
; i
++) {
771 struct chunk
*chunk
= &patch
->chunks
[i
];
773 /* tlno to chunk->same are the same as ent */
774 if (ent
->num_lines
<= tlno
)
776 if (tlno
< chunk
->same
) {
777 struct blame_entry
this[3];
778 split_overlap(this, ent
,
779 tlno
+ ent
->s_lno
, plno
,
780 chunk
->same
+ ent
->s_lno
,
782 copy_split_if_better(sb
, split
, this);
785 plno
= chunk
->p_next
;
786 tlno
= chunk
->t_next
;
791 static int find_move_in_parent(struct scoreboard
*sb
,
792 struct origin
*target
,
793 struct origin
*parent
)
795 int last_in_target
, made_progress
;
796 struct blame_entry
*e
, split
[3];
799 last_in_target
= find_last_in_target(sb
, target
);
800 if (last_in_target
< 0)
801 return 1; /* nothing remains for this target */
803 fill_origin_blob(parent
, &file_p
);
808 while (made_progress
) {
810 for (e
= sb
->ent
; e
; e
= e
->next
) {
811 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
813 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
814 if (split
[1].suspect
&&
815 blame_move_score
< ent_score(sb
, &split
[1])) {
816 split_blame(sb
, split
, e
);
827 struct blame_entry
*ent
;
828 struct blame_entry split
[3];
831 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
832 struct origin
*target
,
835 struct blame_entry
*e
;
837 struct blame_list
*blame_list
= NULL
;
839 /* Count the number of entries the target is suspected for,
840 * and prepare a list of entry and the best split.
842 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
843 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
846 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
847 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
848 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
849 blame_list
[i
++].ent
= e
;
851 *num_ents_p
= num_ents
;
855 static int find_copy_in_parent(struct scoreboard
*sb
,
856 struct origin
*target
,
857 struct commit
*parent
,
858 struct origin
*porigin
,
861 struct diff_options diff_opts
;
862 const char *paths
[1];
865 struct blame_list
*blame_list
;
868 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
870 return 1; /* nothing remains for this target */
872 diff_setup(&diff_opts
);
873 diff_opts
.recursive
= 1;
874 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
877 diff_tree_setup_paths(paths
, &diff_opts
);
878 if (diff_setup_done(&diff_opts
) < 0)
881 /* Try "find copies harder" on new path if requested;
882 * we do not want to use diffcore_rename() actually to
883 * match things up; find_copies_harder is set only to
884 * force diff_tree_sha1() to feed all filepairs to diff_queue,
885 * and this code needs to be after diff_setup_done(), which
886 * usually makes find-copies-harder imply copy detection.
888 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
889 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
890 diff_opts
.find_copies_harder
= 1;
892 diff_tree_sha1(parent
->tree
->object
.sha1
,
893 target
->commit
->tree
->object
.sha1
,
896 if (!diff_opts
.find_copies_harder
)
897 diffcore_std(&diff_opts
);
901 int made_progress
= 0;
903 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
904 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
905 struct origin
*norigin
;
907 struct blame_entry
this[3];
909 if (!DIFF_FILE_VALID(p
->one
))
910 continue; /* does not exist in parent */
911 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
912 /* find_move already dealt with this path */
915 norigin
= get_origin(sb
, parent
, p
->one
->path
);
916 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
917 fill_origin_blob(norigin
, &file_p
);
921 for (j
= 0; j
< num_ents
; j
++) {
922 find_copy_in_blob(sb
, blame_list
[j
].ent
,
923 norigin
, this, &file_p
);
924 copy_split_if_better(sb
, blame_list
[j
].split
,
928 origin_decref(norigin
);
931 for (j
= 0; j
< num_ents
; j
++) {
932 struct blame_entry
*split
= blame_list
[j
].split
;
933 if (split
[1].suspect
&&
934 blame_copy_score
< ent_score(sb
, &split
[1])) {
935 split_blame(sb
, split
, blame_list
[j
].ent
);
944 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
950 diff_flush(&diff_opts
);
955 /* The blobs of origin and porigin exactly match, so everything
956 * origin is suspected for can be blamed on the parent.
958 static void pass_whole_blame(struct scoreboard
*sb
,
959 struct origin
*origin
, struct origin
*porigin
)
961 struct blame_entry
*e
;
963 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
965 porigin
->file
= origin
->file
;
966 origin
->file
.ptr
= NULL
;
968 for (e
= sb
->ent
; e
; e
= e
->next
) {
969 if (cmp_suspect(e
->suspect
, origin
))
971 origin_incref(porigin
);
972 origin_decref(e
->suspect
);
973 e
->suspect
= porigin
;
979 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
982 struct commit
*commit
= origin
->commit
;
983 struct commit_list
*parent
;
984 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
986 memset(parent_origin
, 0, sizeof(parent_origin
));
988 /* The first pass looks for unrenamed path to optimize for
989 * common cases, then we look for renames in the second pass.
991 for (pass
= 0; pass
< 2; pass
++) {
992 struct origin
*(*find
)(struct scoreboard
*,
993 struct commit
*, struct origin
*);
994 find
= pass
? find_rename
: find_origin
;
996 for (i
= 0, parent
= commit
->parents
;
997 i
< MAXPARENT
&& parent
;
998 parent
= parent
->next
, i
++) {
999 struct commit
*p
= parent
->item
;
1002 if (parent_origin
[i
])
1004 if (parse_commit(p
))
1006 porigin
= find(sb
, p
, origin
);
1009 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1010 pass_whole_blame(sb
, origin
, porigin
);
1011 origin_decref(porigin
);
1014 for (j
= same
= 0; j
< i
; j
++)
1015 if (parent_origin
[j
] &&
1016 !hashcmp(parent_origin
[j
]->blob_sha1
,
1017 porigin
->blob_sha1
)) {
1022 parent_origin
[i
] = porigin
;
1024 origin_decref(porigin
);
1029 for (i
= 0, parent
= commit
->parents
;
1030 i
< MAXPARENT
&& parent
;
1031 parent
= parent
->next
, i
++) {
1032 struct origin
*porigin
= parent_origin
[i
];
1035 if (pass_blame_to_parent(sb
, origin
, porigin
))
1040 * Optionally run "miff" to find moves in parents' files here.
1042 if (opt
& PICKAXE_BLAME_MOVE
)
1043 for (i
= 0, parent
= commit
->parents
;
1044 i
< MAXPARENT
&& parent
;
1045 parent
= parent
->next
, i
++) {
1046 struct origin
*porigin
= parent_origin
[i
];
1049 if (find_move_in_parent(sb
, origin
, porigin
))
1054 * Optionally run "ciff" to find copies from parents' files here.
1056 if (opt
& PICKAXE_BLAME_COPY
)
1057 for (i
= 0, parent
= commit
->parents
;
1058 i
< MAXPARENT
&& parent
;
1059 parent
= parent
->next
, i
++) {
1060 struct origin
*porigin
= parent_origin
[i
];
1061 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1067 for (i
= 0; i
< MAXPARENT
; i
++)
1068 origin_decref(parent_origin
[i
]);
1071 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1074 struct blame_entry
*ent
;
1075 struct commit
*commit
;
1076 struct origin
*suspect
= NULL
;
1078 /* find one suspect to break down */
1079 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1081 suspect
= ent
->suspect
;
1083 return; /* all done */
1085 origin_incref(suspect
);
1086 commit
= suspect
->commit
;
1087 if (!commit
->object
.parsed
)
1088 parse_commit(commit
);
1089 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1090 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1091 pass_blame(sb
, suspect
, opt
);
1093 /* Take responsibility for the remaining entries */
1094 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1095 if (!cmp_suspect(ent
->suspect
, suspect
))
1097 origin_decref(suspect
);
1099 if (DEBUG
) /* sanity */
1100 sanity_check_refcnt(sb
);
1104 static const char *format_time(unsigned long time
, const char *tz_str
,
1107 static char time_buf
[128];
1112 if (show_raw_time
) {
1113 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1118 minutes
= tz
< 0 ? -tz
: tz
;
1119 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1120 minutes
= tz
< 0 ? -minutes
: minutes
;
1121 t
= time
+ minutes
* 60;
1124 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1125 strcat(time_buf
, tz_str
);
1133 unsigned long author_time
;
1136 /* filled only when asked for details */
1138 char *committer_mail
;
1139 unsigned long committer_time
;
1145 static void get_ac_line(const char *inbuf
, const char *what
,
1146 int bufsz
, char *person
, char **mail
,
1147 unsigned long *time
, char **tz
)
1152 tmp
= strstr(inbuf
, what
);
1155 tmp
+= strlen(what
);
1156 endp
= strchr(tmp
, '\n');
1164 person
= *mail
= *tz
= "(unknown)";
1168 memcpy(person
, tmp
, len
);
1180 *time
= strtoul(tmp
, NULL
, 10);
1189 static void get_commit_info(struct commit
*commit
,
1190 struct commit_info
*ret
,
1195 static char author_buf
[1024];
1196 static char committer_buf
[1024];
1197 static char summary_buf
[1024];
1199 /* We've operated without save_commit_buffer, so
1200 * we now need to populate them for output.
1202 if (!commit
->buffer
) {
1206 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1208 ret
->author
= author_buf
;
1209 get_ac_line(commit
->buffer
, "\nauthor ",
1210 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1211 &ret
->author_time
, &ret
->author_tz
);
1216 ret
->committer
= committer_buf
;
1217 get_ac_line(commit
->buffer
, "\ncommitter ",
1218 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1219 &ret
->committer_time
, &ret
->committer_tz
);
1221 ret
->summary
= summary_buf
;
1222 tmp
= strstr(commit
->buffer
, "\n\n");
1225 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1229 endp
= strchr(tmp
, '\n');
1233 if (len
>= sizeof(summary_buf
))
1235 memcpy(summary_buf
, tmp
, len
);
1236 summary_buf
[len
] = 0;
1239 #define OUTPUT_ANNOTATE_COMPAT 001
1240 #define OUTPUT_LONG_OBJECT_NAME 002
1241 #define OUTPUT_RAW_TIMESTAMP 004
1242 #define OUTPUT_PORCELAIN 010
1243 #define OUTPUT_SHOW_NAME 020
1244 #define OUTPUT_SHOW_NUMBER 040
1245 #define OUTPUT_SHOW_SCORE 0100
1247 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1251 struct origin
*suspect
= ent
->suspect
;
1254 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1255 printf("%s%c%d %d %d\n",
1257 ent
->guilty
? ' ' : '*', // purely for debugging
1261 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1262 struct commit_info ci
;
1263 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1264 get_commit_info(suspect
->commit
, &ci
, 1);
1265 printf("author %s\n", ci
.author
);
1266 printf("author-mail %s\n", ci
.author_mail
);
1267 printf("author-time %lu\n", ci
.author_time
);
1268 printf("author-tz %s\n", ci
.author_tz
);
1269 printf("committer %s\n", ci
.committer
);
1270 printf("committer-mail %s\n", ci
.committer_mail
);
1271 printf("committer-time %lu\n", ci
.committer_time
);
1272 printf("committer-tz %s\n", ci
.committer_tz
);
1273 printf("filename %s\n", suspect
->path
);
1274 printf("summary %s\n", ci
.summary
);
1276 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1277 printf("filename %s\n", suspect
->path
);
1279 cp
= nth_line(sb
, ent
->lno
);
1280 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1283 printf("%s %d %d\n", hex
,
1284 ent
->s_lno
+ 1 + cnt
,
1285 ent
->lno
+ 1 + cnt
);
1290 } while (ch
!= '\n' &&
1291 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1295 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1299 struct origin
*suspect
= ent
->suspect
;
1300 struct commit_info ci
;
1302 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1304 get_commit_info(suspect
->commit
, &ci
, 1);
1305 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1307 cp
= nth_line(sb
, ent
->lno
);
1308 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1311 printf("%.*s", (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8, hex
);
1312 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1313 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1314 format_time(ci
.author_time
, ci
.author_tz
,
1316 ent
->lno
+ 1 + cnt
);
1318 if (opt
& OUTPUT_SHOW_SCORE
)
1320 max_score_digits
, ent
->score
,
1321 ent
->suspect
->refcnt
);
1322 if (opt
& OUTPUT_SHOW_NAME
)
1323 printf(" %-*.*s", longest_file
, longest_file
,
1325 if (opt
& OUTPUT_SHOW_NUMBER
)
1326 printf(" %*d", max_orig_digits
,
1327 ent
->s_lno
+ 1 + cnt
);
1328 printf(" (%-*.*s %10s %*d) ",
1329 longest_author
, longest_author
, ci
.author
,
1330 format_time(ci
.author_time
, ci
.author_tz
,
1332 max_digits
, ent
->lno
+ 1 + cnt
);
1337 } while (ch
!= '\n' &&
1338 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1342 static void output(struct scoreboard
*sb
, int option
)
1344 struct blame_entry
*ent
;
1346 if (option
& OUTPUT_PORCELAIN
) {
1347 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1348 struct blame_entry
*oth
;
1349 struct origin
*suspect
= ent
->suspect
;
1350 struct commit
*commit
= suspect
->commit
;
1351 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1353 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1354 if ((oth
->suspect
->commit
!= commit
) ||
1355 !strcmp(oth
->suspect
->path
, suspect
->path
))
1357 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1363 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1364 if (option
& OUTPUT_PORCELAIN
)
1365 emit_porcelain(sb
, ent
);
1367 emit_other(sb
, ent
, option
);
1372 static int prepare_lines(struct scoreboard
*sb
)
1374 const char *buf
= sb
->final_buf
;
1375 unsigned long len
= sb
->final_buf_size
;
1376 int num
= 0, incomplete
= 0, bol
= 1;
1378 if (len
&& buf
[len
-1] != '\n')
1379 incomplete
++; /* incomplete line at the end */
1382 sb
->lineno
= xrealloc(sb
->lineno
,
1383 sizeof(int* ) * (num
+ 1));
1384 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1387 if (*buf
++ == '\n') {
1392 sb
->lineno
= xrealloc(sb
->lineno
,
1393 sizeof(int* ) * (num
+ incomplete
+ 1));
1394 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1395 sb
->num_lines
= num
+ incomplete
;
1396 return sb
->num_lines
;
1399 static int read_ancestry(const char *graft_file
)
1401 FILE *fp
= fopen(graft_file
, "r");
1405 while (fgets(buf
, sizeof(buf
), fp
)) {
1406 /* The format is just "Commit Parent1 Parent2 ...\n" */
1407 int len
= strlen(buf
);
1408 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1409 register_commit_graft(graft
, 0);
1415 static int lineno_width(int lines
)
1419 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1424 static void find_alignment(struct scoreboard
*sb
, int *option
)
1426 int longest_src_lines
= 0;
1427 int longest_dst_lines
= 0;
1428 unsigned largest_score
= 0;
1429 struct blame_entry
*e
;
1431 for (e
= sb
->ent
; e
; e
= e
->next
) {
1432 struct origin
*suspect
= e
->suspect
;
1433 struct commit_info ci
;
1436 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1437 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1438 get_commit_info(suspect
->commit
, &ci
, 1);
1439 if (strcmp(suspect
->path
, sb
->path
))
1440 *option
|= OUTPUT_SHOW_NAME
;
1441 num
= strlen(suspect
->path
);
1442 if (longest_file
< num
)
1444 num
= strlen(ci
.author
);
1445 if (longest_author
< num
)
1446 longest_author
= num
;
1448 num
= e
->s_lno
+ e
->num_lines
;
1449 if (longest_src_lines
< num
)
1450 longest_src_lines
= num
;
1451 num
= e
->lno
+ e
->num_lines
;
1452 if (longest_dst_lines
< num
)
1453 longest_dst_lines
= num
;
1454 if (largest_score
< ent_score(sb
, e
))
1455 largest_score
= ent_score(sb
, e
);
1457 max_orig_digits
= lineno_width(longest_src_lines
);
1458 max_digits
= lineno_width(longest_dst_lines
);
1459 max_score_digits
= lineno_width(largest_score
);
1462 static void sanity_check_refcnt(struct scoreboard
*sb
)
1465 struct blame_entry
*ent
;
1467 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1468 /* Nobody should have zero or negative refcnt */
1469 if (ent
->suspect
->refcnt
<= 0) {
1470 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1472 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1473 ent
->suspect
->refcnt
);
1477 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1478 /* Mark the ones that haven't been checked */
1479 if (0 < ent
->suspect
->refcnt
)
1480 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1482 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1483 /* then pick each and see if they have the the correct
1487 struct blame_entry
*e
;
1488 struct origin
*suspect
= ent
->suspect
;
1490 if (0 < suspect
->refcnt
)
1492 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1493 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1494 if (e
->suspect
!= suspect
)
1498 if (suspect
->refcnt
!= found
) {
1499 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1501 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1502 ent
->suspect
->refcnt
, found
);
1508 find_alignment(sb
, &opt
);
1510 die("Baa %d!", baa
);
1514 static int has_path_in_work_tree(const char *path
)
1517 return !lstat(path
, &st
);
1520 static unsigned parse_score(const char *arg
)
1523 unsigned long score
= strtoul(arg
, &end
, 10);
1529 static const char *add_prefix(const char *prefix
, const char *path
)
1531 if (!prefix
|| !prefix
[0])
1533 return prefix_path(prefix
, strlen(prefix
), path
);
1536 int cmd_pickaxe(int argc
, const char **argv
, const char *prefix
)
1538 struct rev_info revs
;
1540 struct scoreboard sb
;
1542 struct blame_entry
*ent
;
1543 int i
, seen_dashdash
, unk
, opt
;
1544 long bottom
, top
, lno
;
1545 int output_option
= 0;
1546 const char *revs_file
= NULL
;
1547 const char *final_commit_name
= NULL
;
1550 save_commit_buffer
= 0;
1555 for (unk
= i
= 1; i
< argc
; i
++) {
1556 const char *arg
= argv
[i
];
1559 else if (!strcmp("-c", arg
))
1560 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1561 else if (!strcmp("-t", arg
))
1562 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1563 else if (!strcmp("-l", arg
))
1564 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1565 else if (!strcmp("-S", arg
) && ++i
< argc
)
1566 revs_file
= argv
[i
];
1567 else if (!strncmp("-M", arg
, 2)) {
1568 opt
|= PICKAXE_BLAME_MOVE
;
1569 blame_move_score
= parse_score(arg
+2);
1571 else if (!strncmp("-C", arg
, 2)) {
1572 if (opt
& PICKAXE_BLAME_COPY
)
1573 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1574 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1575 blame_copy_score
= parse_score(arg
+2);
1577 else if (!strncmp("-L", arg
, 2)) {
1581 usage(pickaxe_usage
);
1587 die("More than one '-L n,m' option given");
1588 bottom
= strtol(arg
, &term
, 10);
1590 top
= strtol(term
+ 1, &term
, 10);
1592 usage(pickaxe_usage
);
1594 if (bottom
&& top
&& top
< bottom
) {
1596 tmp
= top
; top
= bottom
; bottom
= tmp
;
1599 else if (!strcmp("--score-debug", arg
))
1600 output_option
|= OUTPUT_SHOW_SCORE
;
1601 else if (!strcmp("-f", arg
) ||
1602 !strcmp("--show-name", arg
))
1603 output_option
|= OUTPUT_SHOW_NAME
;
1604 else if (!strcmp("-n", arg
) ||
1605 !strcmp("--show-number", arg
))
1606 output_option
|= OUTPUT_SHOW_NUMBER
;
1607 else if (!strcmp("-p", arg
) ||
1608 !strcmp("--porcelain", arg
))
1609 output_option
|= OUTPUT_PORCELAIN
;
1610 else if (!strcmp("--", arg
)) {
1619 if (!blame_move_score
)
1620 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1621 if (!blame_copy_score
)
1622 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1624 /* We have collected options unknown to us in argv[1..unk]
1625 * which are to be passed to revision machinery if we are
1626 * going to do the "bottom" procesing.
1628 * The remaining are:
1630 * (1) if seen_dashdash, its either
1631 * "-options -- <path>" or
1632 * "-options -- <path> <rev>".
1633 * but the latter is allowed only if there is no
1634 * options that we passed to revision machinery.
1636 * (2) otherwise, we may have "--" somewhere later and
1637 * might be looking at the first one of multiple 'rev'
1638 * parameters (e.g. " master ^next ^maint -- path").
1639 * See if there is a dashdash first, and give the
1640 * arguments before that to revision machinery.
1641 * After that there must be one 'path'.
1643 * (3) otherwise, its one of the three:
1644 * "-options <path> <rev>"
1645 * "-options <rev> <path>"
1647 * but again the first one is allowed only if
1648 * there is no options that we passed to revision
1652 if (seen_dashdash
) {
1655 usage(pickaxe_usage
);
1656 path
= add_prefix(prefix
, argv
[i
]);
1657 if (i
+ 1 == argc
- 1) {
1659 usage(pickaxe_usage
);
1660 argv
[unk
++] = argv
[i
+ 1];
1662 else if (i
+ 1 != argc
)
1663 /* garbage at end */
1664 usage(pickaxe_usage
);
1668 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1669 if (!strcmp(argv
[j
], "--"))
1671 if (seen_dashdash
) {
1672 if (seen_dashdash
+ 1 != argc
- 1)
1673 usage(pickaxe_usage
);
1674 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
1675 for (j
= i
; j
< seen_dashdash
; j
++)
1676 argv
[unk
++] = argv
[j
];
1680 path
= add_prefix(prefix
, argv
[i
]);
1681 if (i
+ 1 == argc
- 1) {
1682 final_commit_name
= argv
[i
+ 1];
1684 /* if (unk == 1) we could be getting
1687 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1688 path
= add_prefix(prefix
, argv
[i
+ 1]);
1689 final_commit_name
= argv
[i
];
1692 else if (i
!= argc
- 1)
1693 usage(pickaxe_usage
); /* garbage at end */
1695 if (!has_path_in_work_tree(path
))
1696 die("cannot stat path %s: %s",
1697 path
, strerror(errno
));
1701 if (final_commit_name
)
1702 argv
[unk
++] = final_commit_name
;
1704 /* Now we got rev and path. We do not want the path pruning
1705 * but we may want "bottom" processing.
1709 init_revisions(&revs
, NULL
);
1710 setup_revisions(unk
, argv
, &revs
, "HEAD");
1711 memset(&sb
, 0, sizeof(sb
));
1713 /* There must be one and only one positive commit in the
1714 * revs->pending array.
1716 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1717 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1718 if (obj
->flags
& UNINTERESTING
)
1720 while (obj
->type
== OBJ_TAG
)
1721 obj
= deref_tag(obj
, NULL
, 0);
1722 if (obj
->type
!= OBJ_COMMIT
)
1723 die("Non commit %s?",
1724 revs
.pending
.objects
[i
].name
);
1726 die("More than one commit to dig from %s and %s?",
1727 revs
.pending
.objects
[i
].name
,
1729 sb
.final
= (struct commit
*) obj
;
1730 final_commit_name
= revs
.pending
.objects
[i
].name
;
1734 /* "--not A B -- path" without anything positive */
1735 unsigned char head_sha1
[20];
1737 final_commit_name
= "HEAD";
1738 if (get_sha1(final_commit_name
, head_sha1
))
1739 die("No such ref: HEAD");
1740 sb
.final
= lookup_commit_reference(head_sha1
);
1741 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1744 /* If we have bottom, this will mark the ancestors of the
1745 * bottom commits we would reach while traversing as
1748 prepare_revision_walk(&revs
);
1750 o
= get_origin(&sb
, sb
.final
, path
);
1751 if (fill_blob_sha1(o
))
1752 die("no such path %s in %s", path
, final_commit_name
);
1754 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1756 lno
= prepare_lines(&sb
);
1764 die("file %s has only %lu lines", path
, lno
);
1766 ent
= xcalloc(1, sizeof(*ent
));
1768 ent
->num_lines
= top
- bottom
;
1770 ent
->s_lno
= bottom
;
1775 if (revs_file
&& read_ancestry(revs_file
))
1776 die("reading graft file %s failed: %s",
1777 revs_file
, strerror(errno
));
1779 assign_blame(&sb
, &revs
, opt
);
1783 if (!(output_option
& OUTPUT_PORCELAIN
))
1784 find_alignment(&sb
, &output_option
);
1786 output(&sb
, output_option
);
1787 free((void *)sb
.final_buf
);
1788 for (ent
= sb
.ent
; ent
; ) {
1789 struct blame_entry
*e
= ent
->next
;
1795 printf("num read blob: %d\n", num_read_blob
);
1796 printf("num get patch: %d\n", num_get_patch
);
1797 printf("num commits: %d\n", num_commits
);