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
;
43 #define PICKAXE_BLAME_MOVE 01
44 #define PICKAXE_BLAME_COPY 02
45 #define PICKAXE_BLAME_COPY_HARDER 04
48 * blame for a blame_entry with score lower than these thresholds
49 * is not passed to the parent using move/copy logic.
51 static unsigned blame_move_score
;
52 static unsigned blame_copy_score
;
53 #define BLAME_DEFAULT_MOVE_SCORE 20
54 #define BLAME_DEFAULT_COPY_SCORE 40
56 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
57 #define METAINFO_SHOWN (1u<<12)
58 #define MORE_THAN_ONE_PATH (1u<<13)
61 * One blob in a commit that is being suspected
65 struct commit
*commit
;
66 unsigned char blob_sha1
[20];
67 char path
[FLEX_ARRAY
];
70 static inline struct origin
*origin_incref(struct origin
*o
)
77 static void origin_decref(struct origin
*o
)
79 if (o
&& --o
->refcnt
<= 0) {
80 memset(o
, 0, sizeof(*o
));
86 struct blame_entry
*prev
;
87 struct blame_entry
*next
;
89 /* the first line of this group in the final image;
90 * internally all line numbers are 0 based.
94 /* how many lines this group has */
97 /* the commit that introduced this group into the final image */
98 struct origin
*suspect
;
100 /* true if the suspect is truly guilty; false while we have not
101 * checked if the group came from one of its parents.
105 /* the line number of the first line of this group in the
106 * suspect's file; internally all line numbers are 0 based.
110 /* how significant this entry is -- cached to avoid
111 * scanning the lines over and over
117 /* the final commit (i.e. where we started digging from) */
118 struct commit
*final
;
122 /* the contents in the final; pointed into by buf pointers of
125 const char *final_buf
;
126 unsigned long final_buf_size
;
128 /* linked list of blames */
129 struct blame_entry
*ent
;
131 /* look-up a line in the final buffer */
136 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
138 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
141 return strcmp(a
->path
, b
->path
);
144 static void sanity_check_refcnt(struct scoreboard
*);
146 static void coalesce(struct scoreboard
*sb
)
148 struct blame_entry
*ent
, *next
;
150 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
151 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
152 ent
->guilty
== next
->guilty
&&
153 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
154 ent
->num_lines
+= next
->num_lines
;
155 ent
->next
= next
->next
;
157 ent
->next
->prev
= ent
;
158 origin_decref(next
->suspect
);
161 next
= ent
; /* again */
165 if (DEBUG
) /* sanity */
166 sanity_check_refcnt(sb
);
169 static struct origin
*get_origin(struct scoreboard
*sb
,
170 struct commit
*commit
,
173 struct blame_entry
*e
;
176 for (e
= sb
->ent
; e
; e
= e
->next
) {
177 if (e
->suspect
->commit
== commit
&&
178 !strcmp(e
->suspect
->path
, path
))
179 return origin_incref(e
->suspect
);
181 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
184 strcpy(o
->path
, path
);
188 static int fill_blob_sha1(struct origin
*origin
)
193 if (!is_null_sha1(origin
->blob_sha1
))
195 if (get_tree_entry(origin
->commit
->object
.sha1
,
197 origin
->blob_sha1
, &mode
))
199 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
200 strcmp(type
, blob_type
))
204 hashclr(origin
->blob_sha1
);
208 static struct origin
*find_origin(struct scoreboard
*sb
,
209 struct commit
*parent
,
210 struct origin
*origin
)
212 struct origin
*porigin
= NULL
;
213 struct diff_options diff_opts
;
215 const char *paths
[2];
217 /* See if the origin->path is different between parent
218 * and origin first. Most of the time they are the
219 * same and diff-tree is fairly efficient about this.
221 diff_setup(&diff_opts
);
222 diff_opts
.recursive
= 1;
223 diff_opts
.detect_rename
= 0;
224 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
225 paths
[0] = origin
->path
;
228 diff_tree_setup_paths(paths
, &diff_opts
);
229 if (diff_setup_done(&diff_opts
) < 0)
231 diff_tree_sha1(parent
->tree
->object
.sha1
,
232 origin
->commit
->tree
->object
.sha1
,
234 diffcore_std(&diff_opts
);
236 /* It is either one entry that says "modified", or "created",
239 if (!diff_queued_diff
.nr
) {
240 /* The path is the same as parent */
241 porigin
= get_origin(sb
, parent
, origin
->path
);
242 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
244 else if (diff_queued_diff
.nr
!= 1)
245 die("internal error in pickaxe::find_origin");
247 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
250 die("internal error in pickaxe::find_origin (%c)",
253 porigin
= get_origin(sb
, parent
, origin
->path
);
254 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
258 /* Did not exist in parent, or type changed */
262 diff_flush(&diff_opts
);
266 /* Otherwise we would look for a rename */
268 diff_setup(&diff_opts
);
269 diff_opts
.recursive
= 1;
270 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
271 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
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 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
282 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
283 if ((p
->status
== 'R' || p
->status
== 'C') &&
284 !strcmp(p
->two
->path
, origin
->path
)) {
285 porigin
= get_origin(sb
, parent
, p
->one
->path
);
286 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
290 diff_flush(&diff_opts
);
295 /* line number in postimage; up to but not including this
296 * line is the same as preimage
300 /* preimage line number after this chunk */
303 /* postimage line number after this chunk */
308 struct chunk
*chunks
;
312 struct blame_diff_state
{
313 struct xdiff_emit_state xm
;
315 unsigned hunk_post_context
;
316 unsigned hunk_in_pre_context
: 1;
319 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
321 struct blame_diff_state
*state
= state_
;
323 int off1
, off2
, len1
, len2
, num
;
325 num
= state
->ret
->num
;
326 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
327 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
328 state
->ret
->chunks
[num
- 1].same
++;
330 state
->hunk_in_pre_context
= 0;
332 state
->hunk_post_context
++;
334 state
->hunk_post_context
= 0;
339 if (num
&& state
->hunk_post_context
) {
340 chunk
= &state
->ret
->chunks
[num
- 1];
341 chunk
->p_next
-= state
->hunk_post_context
;
342 chunk
->t_next
-= state
->hunk_post_context
;
344 state
->ret
->num
= ++num
;
345 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
346 sizeof(struct chunk
) * num
);
347 chunk
= &state
->ret
->chunks
[num
- 1];
348 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
353 /* Line numbers in patch output are one based. */
357 chunk
->same
= len2
? off2
: (off2
+ 1);
359 chunk
->p_next
= off1
+ (len1
? len1
: 1);
360 chunk
->t_next
= chunk
->same
+ len2
;
361 state
->hunk_in_pre_context
= 1;
362 state
->hunk_post_context
= 0;
365 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
368 struct blame_diff_state state
;
373 xpp
.flags
= XDF_NEED_MINIMAL
;
374 xecfg
.ctxlen
= context
;
376 ecb
.outf
= xdiff_outf
;
378 memset(&state
, 0, sizeof(state
));
379 state
.xm
.consume
= process_u_diff
;
380 state
.ret
= xmalloc(sizeof(struct patch
));
381 state
.ret
->chunks
= NULL
;
384 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
386 if (state
.ret
->num
) {
388 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
389 chunk
->p_next
-= state
.hunk_post_context
;
390 chunk
->t_next
-= state
.hunk_post_context
;
395 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
397 mmfile_t file_p
, file_o
;
399 char *blob_p
, *blob_o
;
402 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
403 (unsigned long *) &file_p
.size
);
404 blob_o
= read_sha1_file(origin
->blob_sha1
, type
,
405 (unsigned long *) &file_o
.size
);
408 if (!file_p
.ptr
|| !file_o
.ptr
) {
414 patch
= compare_buffer(&file_p
, &file_o
, 0);
420 static void free_patch(struct patch
*p
)
426 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
428 struct blame_entry
*ent
, *prev
= NULL
;
430 origin_incref(e
->suspect
);
432 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
435 /* prev, if not NULL, is the last one that is below e */
438 e
->next
= prev
->next
;
449 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
451 struct blame_entry
*p
, *n
;
455 origin_incref(src
->suspect
);
456 origin_decref(dst
->suspect
);
457 memcpy(dst
, src
, sizeof(*src
));
463 static const char *nth_line(struct scoreboard
*sb
, int lno
)
465 return sb
->final_buf
+ sb
->lineno
[lno
];
468 static void split_overlap(struct blame_entry
*split
,
469 struct blame_entry
*e
,
470 int tlno
, int plno
, int same
,
471 struct origin
*parent
)
473 /* it is known that lines between tlno to same came from
474 * parent, and e has an overlap with that range. it also is
475 * known that parent's line plno corresponds to e's line tlno.
481 * <------------------>
483 * Potentially we need to split e into three parts; before
484 * this chunk, the chunk to be blamed for parent, and after
488 memset(split
, 0, sizeof(struct blame_entry
[3]));
490 if (e
->s_lno
< tlno
) {
491 /* there is a pre-chunk part not blamed on parent */
492 split
[0].suspect
= origin_incref(e
->suspect
);
493 split
[0].lno
= e
->lno
;
494 split
[0].s_lno
= e
->s_lno
;
495 split
[0].num_lines
= tlno
- e
->s_lno
;
496 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
497 split
[1].s_lno
= plno
;
500 split
[1].lno
= e
->lno
;
501 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
504 if (same
< e
->s_lno
+ e
->num_lines
) {
505 /* there is a post-chunk part not blamed on parent */
506 split
[2].suspect
= origin_incref(e
->suspect
);
507 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
508 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
509 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
510 chunk_end_lno
= split
[2].lno
;
513 chunk_end_lno
= e
->lno
+ e
->num_lines
;
514 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
516 if (split
[1].num_lines
< 1)
518 split
[1].suspect
= origin_incref(parent
);
521 static void split_blame(struct scoreboard
*sb
,
522 struct blame_entry
*split
,
523 struct blame_entry
*e
)
525 struct blame_entry
*new_entry
;
527 if (split
[0].suspect
&& split
[2].suspect
) {
528 /* we need to split e into two and add another for parent */
529 dup_entry(e
, &split
[0]);
531 new_entry
= xmalloc(sizeof(*new_entry
));
532 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
533 add_blame_entry(sb
, new_entry
);
535 new_entry
= xmalloc(sizeof(*new_entry
));
536 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
537 add_blame_entry(sb
, new_entry
);
539 else if (!split
[0].suspect
&& !split
[2].suspect
)
540 /* parent covers the entire area */
541 dup_entry(e
, &split
[1]);
542 else if (split
[0].suspect
) {
543 dup_entry(e
, &split
[0]);
545 new_entry
= xmalloc(sizeof(*new_entry
));
546 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
547 add_blame_entry(sb
, new_entry
);
550 dup_entry(e
, &split
[1]);
552 new_entry
= xmalloc(sizeof(*new_entry
));
553 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
554 add_blame_entry(sb
, new_entry
);
557 if (DEBUG
) { /* sanity */
558 struct blame_entry
*ent
;
559 int lno
= sb
->ent
->lno
, corrupt
= 0;
561 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
566 lno
+= ent
->num_lines
;
570 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
571 printf("L %8d l %8d n %8d\n",
572 lno
, ent
->lno
, ent
->num_lines
);
573 lno
= ent
->lno
+ ent
->num_lines
;
580 static void decref_split(struct blame_entry
*split
)
584 for (i
= 0; i
< 3; i
++)
585 origin_decref(split
[i
].suspect
);
588 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
589 int tlno
, int plno
, int same
,
590 struct origin
*parent
)
592 struct blame_entry split
[3];
594 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
595 if (split
[1].suspect
)
596 split_blame(sb
, split
, e
);
600 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
602 struct blame_entry
*e
;
603 int last_in_target
= -1;
605 for (e
= sb
->ent
; e
; e
= e
->next
) {
606 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
608 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
609 last_in_target
= e
->s_lno
+ e
->num_lines
;
611 return last_in_target
;
614 static void blame_chunk(struct scoreboard
*sb
,
615 int tlno
, int plno
, int same
,
616 struct origin
*target
, struct origin
*parent
)
618 struct blame_entry
*e
;
620 for (e
= sb
->ent
; e
; e
= e
->next
) {
621 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
623 if (same
<= e
->s_lno
)
625 if (tlno
< e
->s_lno
+ e
->num_lines
)
626 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
630 static int pass_blame_to_parent(struct scoreboard
*sb
,
631 struct origin
*target
,
632 struct origin
*parent
)
634 int i
, last_in_target
, plno
, tlno
;
637 last_in_target
= find_last_in_target(sb
, target
);
638 if (last_in_target
< 0)
639 return 1; /* nothing remains for this target */
641 patch
= get_patch(parent
, target
);
643 for (i
= 0; i
< patch
->num
; i
++) {
644 struct chunk
*chunk
= &patch
->chunks
[i
];
646 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
647 plno
= chunk
->p_next
;
648 tlno
= chunk
->t_next
;
650 /* rest (i.e. anything above tlno) are the same as parent */
651 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
657 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
666 cp
= nth_line(sb
, e
->lno
);
667 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
669 unsigned ch
= *((unsigned char *)cp
);
678 static void copy_split_if_better(struct scoreboard
*sb
,
679 struct blame_entry
*best_so_far
,
680 struct blame_entry
*this)
684 if (!this[1].suspect
)
686 if (best_so_far
[1].suspect
) {
687 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
691 for (i
= 0; i
< 3; i
++)
692 origin_incref(this[i
].suspect
);
693 decref_split(best_so_far
);
694 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
697 static void find_copy_in_blob(struct scoreboard
*sb
,
698 struct blame_entry
*ent
,
699 struct origin
*parent
,
700 struct blame_entry
*split
,
709 cp
= nth_line(sb
, ent
->lno
);
710 file_o
.ptr
= (char*) cp
;
711 cnt
= ent
->num_lines
;
713 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
717 file_o
.size
= cp
- file_o
.ptr
;
719 patch
= compare_buffer(file_p
, &file_o
, 1);
721 memset(split
, 0, sizeof(struct blame_entry
[3]));
723 for (i
= 0; i
< patch
->num
; i
++) {
724 struct chunk
*chunk
= &patch
->chunks
[i
];
726 /* tlno to chunk->same are the same as ent */
727 if (ent
->num_lines
<= tlno
)
729 if (tlno
< chunk
->same
) {
730 struct blame_entry
this[3];
731 split_overlap(this, ent
,
732 tlno
+ ent
->s_lno
, plno
,
733 chunk
->same
+ ent
->s_lno
,
735 copy_split_if_better(sb
, split
, this);
738 plno
= chunk
->p_next
;
739 tlno
= chunk
->t_next
;
744 static int find_move_in_parent(struct scoreboard
*sb
,
745 struct origin
*target
,
746 struct origin
*parent
)
749 struct blame_entry
*e
, split
[3];
754 last_in_target
= find_last_in_target(sb
, target
);
755 if (last_in_target
< 0)
756 return 1; /* nothing remains for this target */
758 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
759 (unsigned long *) &file_p
.size
);
766 for (e
= sb
->ent
; e
; e
= e
->next
) {
767 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
769 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
770 if (split
[1].suspect
&&
771 blame_move_score
< ent_score(sb
, &split
[1]))
772 split_blame(sb
, split
, e
);
779 static int find_copy_in_parent(struct scoreboard
*sb
,
780 struct origin
*target
,
781 struct commit
*parent
,
782 struct origin
*porigin
,
785 struct diff_options diff_opts
;
786 const char *paths
[1];
787 struct blame_entry
*e
;
790 struct blame_entry
*ent
;
791 struct blame_entry split
[3];
795 /* Count the number of entries the target is suspected for,
796 * and prepare a list of entry and the best split.
798 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
799 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
802 return 1; /* nothing remains for this target */
804 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
805 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
806 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
807 blame_list
[i
++].ent
= e
;
809 diff_setup(&diff_opts
);
810 diff_opts
.recursive
= 1;
811 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
813 /* Try "find copies harder" on new path */
814 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
815 (!porigin
|| strcmp(target
->path
, porigin
->path
))) {
816 diff_opts
.detect_rename
= DIFF_DETECT_COPY
;
817 diff_opts
.find_copies_harder
= 1;
820 diff_tree_setup_paths(paths
, &diff_opts
);
821 if (diff_setup_done(&diff_opts
) < 0)
823 diff_tree_sha1(parent
->tree
->object
.sha1
,
824 target
->commit
->tree
->object
.sha1
,
826 diffcore_std(&diff_opts
);
828 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
829 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
830 struct origin
*norigin
;
834 struct blame_entry
this[3];
836 if (!DIFF_FILE_VALID(p
->one
))
837 continue; /* does not exist in parent */
838 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
839 /* find_move already dealt with this path */
842 norigin
= get_origin(sb
, parent
, p
->one
->path
);
843 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
844 blob
= read_sha1_file(norigin
->blob_sha1
, type
,
845 (unsigned long *) &file_p
.size
);
850 for (j
= 0; j
< num_ents
; j
++) {
851 find_copy_in_blob(sb
, blame_list
[j
].ent
, norigin
,
853 copy_split_if_better(sb
, blame_list
[j
].split
,
857 origin_decref(norigin
);
859 diff_flush(&diff_opts
);
861 for (j
= 0; j
< num_ents
; j
++) {
862 struct blame_entry
*split
= blame_list
[j
].split
;
863 if (split
[1].suspect
&&
864 blame_copy_score
< ent_score(sb
, &split
[1]))
865 split_blame(sb
, split
, blame_list
[j
].ent
);
875 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
878 struct commit
*commit
= origin
->commit
;
879 struct commit_list
*parent
;
880 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
882 memset(parent_origin
, 0, sizeof(parent_origin
));
883 for (i
= 0, parent
= commit
->parents
;
884 i
< MAXPARENT
&& parent
;
885 parent
= parent
->next
, i
++) {
886 struct commit
*p
= parent
->item
;
890 porigin
= find_origin(sb
, parent
->item
, origin
);
893 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
894 struct blame_entry
*e
;
895 for (e
= sb
->ent
; e
; e
= e
->next
)
896 if (e
->suspect
== origin
) {
897 origin_incref(porigin
);
898 origin_decref(e
->suspect
);
899 e
->suspect
= porigin
;
901 origin_decref(porigin
);
904 parent_origin
[i
] = porigin
;
907 for (i
= 0, parent
= commit
->parents
;
908 i
< MAXPARENT
&& parent
;
909 parent
= parent
->next
, i
++) {
910 struct origin
*porigin
= parent_origin
[i
];
913 if (pass_blame_to_parent(sb
, origin
, porigin
))
918 * Optionally run "miff" to find moves in parents' files here.
920 if (opt
& PICKAXE_BLAME_MOVE
)
921 for (i
= 0, parent
= commit
->parents
;
922 i
< MAXPARENT
&& parent
;
923 parent
= parent
->next
, i
++) {
924 struct origin
*porigin
= parent_origin
[i
];
927 if (find_move_in_parent(sb
, origin
, porigin
))
932 * Optionally run "ciff" to find copies from parents' files here.
934 if (opt
& PICKAXE_BLAME_COPY
)
935 for (i
= 0, parent
= commit
->parents
;
936 i
< MAXPARENT
&& parent
;
937 parent
= parent
->next
, i
++) {
938 struct origin
*porigin
= parent_origin
[i
];
939 if (find_copy_in_parent(sb
, origin
, parent
->item
,
945 for (i
= 0; i
< MAXPARENT
; i
++)
946 origin_decref(parent_origin
[i
]);
949 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
952 struct blame_entry
*ent
;
953 struct commit
*commit
;
954 struct origin
*suspect
= NULL
;
956 /* find one suspect to break down */
957 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
959 suspect
= ent
->suspect
;
961 return; /* all done */
963 origin_incref(suspect
);
964 commit
= suspect
->commit
;
965 parse_commit(commit
);
966 if (!(commit
->object
.flags
& UNINTERESTING
) &&
967 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
968 pass_blame(sb
, suspect
, opt
);
970 /* Take responsibility for the remaining entries */
971 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
972 if (!cmp_suspect(ent
->suspect
, suspect
))
974 origin_decref(suspect
);
979 static const char *format_time(unsigned long time
, const char *tz_str
,
982 static char time_buf
[128];
988 sprintf(time_buf
, "%lu %s", time
, tz_str
);
993 minutes
= tz
< 0 ? -tz
: tz
;
994 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
995 minutes
= tz
< 0 ? -minutes
: minutes
;
996 t
= time
+ minutes
* 60;
999 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1000 strcat(time_buf
, tz_str
);
1008 unsigned long author_time
;
1011 /* filled only when asked for details */
1013 char *committer_mail
;
1014 unsigned long committer_time
;
1020 static void get_ac_line(const char *inbuf
, const char *what
,
1021 int bufsz
, char *person
, char **mail
,
1022 unsigned long *time
, char **tz
)
1027 tmp
= strstr(inbuf
, what
);
1030 tmp
+= strlen(what
);
1031 endp
= strchr(tmp
, '\n');
1039 person
= *mail
= *tz
= "(unknown)";
1043 memcpy(person
, tmp
, len
);
1055 *time
= strtoul(tmp
, NULL
, 10);
1064 static void get_commit_info(struct commit
*commit
,
1065 struct commit_info
*ret
,
1070 static char author_buf
[1024];
1071 static char committer_buf
[1024];
1072 static char summary_buf
[1024];
1074 /* We've operated without save_commit_buffer, so
1075 * we now need to populate them for output.
1077 if (!commit
->buffer
) {
1081 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1083 ret
->author
= author_buf
;
1084 get_ac_line(commit
->buffer
, "\nauthor ",
1085 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1086 &ret
->author_time
, &ret
->author_tz
);
1091 ret
->committer
= committer_buf
;
1092 get_ac_line(commit
->buffer
, "\ncommitter ",
1093 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1094 &ret
->committer_time
, &ret
->committer_tz
);
1096 ret
->summary
= summary_buf
;
1097 tmp
= strstr(commit
->buffer
, "\n\n");
1100 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1104 endp
= strchr(tmp
, '\n');
1108 if (len
>= sizeof(summary_buf
))
1110 memcpy(summary_buf
, tmp
, len
);
1111 summary_buf
[len
] = 0;
1114 #define OUTPUT_ANNOTATE_COMPAT 001
1115 #define OUTPUT_LONG_OBJECT_NAME 002
1116 #define OUTPUT_RAW_TIMESTAMP 004
1117 #define OUTPUT_PORCELAIN 010
1118 #define OUTPUT_SHOW_NAME 020
1119 #define OUTPUT_SHOW_NUMBER 040
1120 #define OUTPUT_SHOW_SCORE 0100
1122 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1126 struct origin
*suspect
= ent
->suspect
;
1129 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1130 printf("%s%c%d %d %d\n",
1132 ent
->guilty
? ' ' : '*', // purely for debugging
1136 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1137 struct commit_info ci
;
1138 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1139 get_commit_info(suspect
->commit
, &ci
, 1);
1140 printf("author %s\n", ci
.author
);
1141 printf("author-mail %s\n", ci
.author_mail
);
1142 printf("author-time %lu\n", ci
.author_time
);
1143 printf("author-tz %s\n", ci
.author_tz
);
1144 printf("committer %s\n", ci
.committer
);
1145 printf("committer-mail %s\n", ci
.committer_mail
);
1146 printf("committer-time %lu\n", ci
.committer_time
);
1147 printf("committer-tz %s\n", ci
.committer_tz
);
1148 printf("filename %s\n", suspect
->path
);
1149 printf("summary %s\n", ci
.summary
);
1151 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1152 printf("filename %s\n", suspect
->path
);
1154 cp
= nth_line(sb
, ent
->lno
);
1155 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1158 printf("%s %d %d\n", hex
,
1159 ent
->s_lno
+ 1 + cnt
,
1160 ent
->lno
+ 1 + cnt
);
1165 } while (ch
!= '\n' &&
1166 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1170 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1174 struct origin
*suspect
= ent
->suspect
;
1175 struct commit_info ci
;
1177 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1179 get_commit_info(suspect
->commit
, &ci
, 1);
1180 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1182 cp
= nth_line(sb
, ent
->lno
);
1183 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1186 printf("%.*s", (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8, hex
);
1187 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1188 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1189 format_time(ci
.author_time
, ci
.author_tz
,
1191 ent
->lno
+ 1 + cnt
);
1193 if (opt
& OUTPUT_SHOW_SCORE
)
1195 max_score_digits
, ent
->score
,
1196 ent
->suspect
->refcnt
);
1197 if (opt
& OUTPUT_SHOW_NAME
)
1198 printf(" %-*.*s", longest_file
, longest_file
,
1200 if (opt
& OUTPUT_SHOW_NUMBER
)
1201 printf(" %*d", max_orig_digits
,
1202 ent
->s_lno
+ 1 + cnt
);
1203 printf(" (%-*.*s %10s %*d) ",
1204 longest_author
, longest_author
, ci
.author
,
1205 format_time(ci
.author_time
, ci
.author_tz
,
1207 max_digits
, ent
->lno
+ 1 + cnt
);
1212 } while (ch
!= '\n' &&
1213 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1217 static void output(struct scoreboard
*sb
, int option
)
1219 struct blame_entry
*ent
;
1221 if (option
& OUTPUT_PORCELAIN
) {
1222 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1223 struct blame_entry
*oth
;
1224 struct origin
*suspect
= ent
->suspect
;
1225 struct commit
*commit
= suspect
->commit
;
1226 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1228 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1229 if ((oth
->suspect
->commit
!= commit
) ||
1230 !strcmp(oth
->suspect
->path
, suspect
->path
))
1232 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1238 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1239 if (option
& OUTPUT_PORCELAIN
)
1240 emit_porcelain(sb
, ent
);
1242 emit_other(sb
, ent
, option
);
1247 static int prepare_lines(struct scoreboard
*sb
)
1249 const char *buf
= sb
->final_buf
;
1250 unsigned long len
= sb
->final_buf_size
;
1251 int num
= 0, incomplete
= 0, bol
= 1;
1253 if (len
&& buf
[len
-1] != '\n')
1254 incomplete
++; /* incomplete line at the end */
1257 sb
->lineno
= xrealloc(sb
->lineno
,
1258 sizeof(int* ) * (num
+ 1));
1259 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1262 if (*buf
++ == '\n') {
1267 sb
->lineno
= xrealloc(sb
->lineno
,
1268 sizeof(int* ) * (num
+ incomplete
+ 1));
1269 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1270 sb
->num_lines
= num
+ incomplete
;
1271 return sb
->num_lines
;
1274 static int read_ancestry(const char *graft_file
)
1276 FILE *fp
= fopen(graft_file
, "r");
1280 while (fgets(buf
, sizeof(buf
), fp
)) {
1281 /* The format is just "Commit Parent1 Parent2 ...\n" */
1282 int len
= strlen(buf
);
1283 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1284 register_commit_graft(graft
, 0);
1290 static int lineno_width(int lines
)
1294 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1299 static void find_alignment(struct scoreboard
*sb
, int *option
)
1301 int longest_src_lines
= 0;
1302 int longest_dst_lines
= 0;
1303 unsigned largest_score
= 0;
1304 struct blame_entry
*e
;
1306 for (e
= sb
->ent
; e
; e
= e
->next
) {
1307 struct origin
*suspect
= e
->suspect
;
1308 struct commit_info ci
;
1311 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1312 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1313 get_commit_info(suspect
->commit
, &ci
, 1);
1314 if (strcmp(suspect
->path
, sb
->path
))
1315 *option
|= OUTPUT_SHOW_NAME
;
1316 num
= strlen(suspect
->path
);
1317 if (longest_file
< num
)
1319 num
= strlen(ci
.author
);
1320 if (longest_author
< num
)
1321 longest_author
= num
;
1323 num
= e
->s_lno
+ e
->num_lines
;
1324 if (longest_src_lines
< num
)
1325 longest_src_lines
= num
;
1326 num
= e
->lno
+ e
->num_lines
;
1327 if (longest_dst_lines
< num
)
1328 longest_dst_lines
= num
;
1329 if (largest_score
< ent_score(sb
, e
))
1330 largest_score
= ent_score(sb
, e
);
1332 max_orig_digits
= lineno_width(longest_src_lines
);
1333 max_digits
= lineno_width(longest_dst_lines
);
1334 max_score_digits
= lineno_width(largest_score
);
1337 static void sanity_check_refcnt(struct scoreboard
*sb
)
1340 struct blame_entry
*ent
;
1342 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1343 /* first mark the ones that haven't been checked */
1344 if (0 < ent
->suspect
->refcnt
)
1345 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1346 else if (!ent
->suspect
->refcnt
)
1349 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1350 /* then pick each and see if they have the the
1354 struct blame_entry
*e
;
1355 struct origin
*suspect
= ent
->suspect
;
1357 if (0 < suspect
->refcnt
)
1359 suspect
->refcnt
= -suspect
->refcnt
;
1360 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1361 if (e
->suspect
!= suspect
)
1365 if (suspect
->refcnt
!= found
)
1370 find_alignment(sb
, &opt
);
1376 static int has_path_in_work_tree(const char *path
)
1379 return !lstat(path
, &st
);
1382 static unsigned parse_score(const char *arg
)
1385 unsigned long score
= strtoul(arg
, &end
, 10);
1391 int cmd_pickaxe(int argc
, const char **argv
, const char *prefix
)
1393 struct rev_info revs
;
1395 struct scoreboard sb
;
1397 struct blame_entry
*ent
;
1398 int i
, seen_dashdash
, unk
, opt
;
1399 long bottom
, top
, lno
;
1400 int output_option
= 0;
1401 const char *revs_file
= NULL
;
1402 const char *final_commit_name
= NULL
;
1405 save_commit_buffer
= 0;
1410 for (unk
= i
= 1; i
< argc
; i
++) {
1411 const char *arg
= argv
[i
];
1414 else if (!strcmp("-c", arg
))
1415 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1416 else if (!strcmp("-t", arg
))
1417 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1418 else if (!strcmp("-l", arg
))
1419 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1420 else if (!strcmp("-S", arg
) && ++i
< argc
)
1421 revs_file
= argv
[i
];
1422 else if (!strncmp("-M", arg
, 2)) {
1423 opt
|= PICKAXE_BLAME_MOVE
;
1424 blame_move_score
= parse_score(arg
+2);
1426 else if (!strncmp("-C", arg
, 2)) {
1427 if (opt
& PICKAXE_BLAME_COPY
)
1428 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1429 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1430 blame_copy_score
= parse_score(arg
+2);
1432 else if (!strncmp("-L", arg
, 2)) {
1436 usage(pickaxe_usage
);
1442 die("More than one '-L n,m' option given");
1443 bottom
= strtol(arg
, &term
, 10);
1445 top
= strtol(term
+ 1, &term
, 10);
1447 usage(pickaxe_usage
);
1449 if (bottom
&& top
&& top
< bottom
) {
1451 tmp
= top
; top
= bottom
; bottom
= tmp
;
1454 else if (!strcmp("--score-debug", arg
))
1455 output_option
|= OUTPUT_SHOW_SCORE
;
1456 else if (!strcmp("-f", arg
) ||
1457 !strcmp("--show-name", arg
))
1458 output_option
|= OUTPUT_SHOW_NAME
;
1459 else if (!strcmp("-n", arg
) ||
1460 !strcmp("--show-number", arg
))
1461 output_option
|= OUTPUT_SHOW_NUMBER
;
1462 else if (!strcmp("-p", arg
) ||
1463 !strcmp("--porcelain", arg
))
1464 output_option
|= OUTPUT_PORCELAIN
;
1465 else if (!strcmp("--", arg
)) {
1474 if (!blame_move_score
)
1475 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1476 if (!blame_copy_score
)
1477 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1479 /* We have collected options unknown to us in argv[1..unk]
1480 * which are to be passed to revision machinery if we are
1481 * going to do the "bottom" procesing.
1483 * The remaining are:
1485 * (1) if seen_dashdash, its either
1486 * "-options -- <path>" or
1487 * "-options -- <path> <rev>".
1488 * but the latter is allowed only if there is no
1489 * options that we passed to revision machinery.
1491 * (2) otherwise, we may have "--" somewhere later and
1492 * might be looking at the first one of multiple 'rev'
1493 * parameters (e.g. " master ^next ^maint -- path").
1494 * See if there is a dashdash first, and give the
1495 * arguments before that to revision machinery.
1496 * After that there must be one 'path'.
1498 * (3) otherwise, its one of the three:
1499 * "-options <path> <rev>"
1500 * "-options <rev> <path>"
1502 * but again the first one is allowed only if
1503 * there is no options that we passed to revision
1507 if (seen_dashdash
) {
1510 usage(pickaxe_usage
);
1512 if (i
+ 1 == argc
- 1) {
1514 usage(pickaxe_usage
);
1515 argv
[unk
++] = argv
[i
+ 1];
1517 else if (i
+ 1 != argc
)
1518 /* garbage at end */
1519 usage(pickaxe_usage
);
1523 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1524 if (!strcmp(argv
[j
], "--"))
1526 if (seen_dashdash
) {
1527 if (seen_dashdash
+ 1 != argc
- 1)
1528 usage(pickaxe_usage
);
1529 path
= argv
[seen_dashdash
+ 1];
1530 for (j
= i
; j
< seen_dashdash
; j
++)
1531 argv
[unk
++] = argv
[j
];
1536 if (i
+ 1 == argc
- 1) {
1537 final_commit_name
= argv
[i
+ 1];
1539 /* if (unk == 1) we could be getting
1542 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1544 final_commit_name
= argv
[i
];
1547 else if (i
!= argc
- 1)
1548 usage(pickaxe_usage
); /* garbage at end */
1550 if (!has_path_in_work_tree(path
))
1551 die("cannot stat path %s: %s",
1552 path
, strerror(errno
));
1556 if (final_commit_name
)
1557 argv
[unk
++] = final_commit_name
;
1559 /* Now we got rev and path. We do not want the path pruning
1560 * but we may want "bottom" processing.
1564 init_revisions(&revs
, NULL
);
1565 setup_revisions(unk
, argv
, &revs
, "HEAD");
1566 memset(&sb
, 0, sizeof(sb
));
1568 /* There must be one and only one positive commit in the
1569 * revs->pending array.
1571 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1572 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1573 if (obj
->flags
& UNINTERESTING
)
1575 while (obj
->type
== OBJ_TAG
)
1576 obj
= deref_tag(obj
, NULL
, 0);
1577 if (obj
->type
!= OBJ_COMMIT
)
1578 die("Non commit %s?",
1579 revs
.pending
.objects
[i
].name
);
1581 die("More than one commit to dig from %s and %s?",
1582 revs
.pending
.objects
[i
].name
,
1584 sb
.final
= (struct commit
*) obj
;
1585 final_commit_name
= revs
.pending
.objects
[i
].name
;
1589 /* "--not A B -- path" without anything positive */
1590 unsigned char head_sha1
[20];
1592 final_commit_name
= "HEAD";
1593 if (get_sha1(final_commit_name
, head_sha1
))
1594 die("No such ref: HEAD");
1595 sb
.final
= lookup_commit_reference(head_sha1
);
1596 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1599 /* If we have bottom, this will mark the ancestors of the
1600 * bottom commits we would reach while traversing as
1603 prepare_revision_walk(&revs
);
1605 o
= get_origin(&sb
, sb
.final
, path
);
1606 if (fill_blob_sha1(o
))
1607 die("no such path %s in %s", path
, final_commit_name
);
1609 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1610 lno
= prepare_lines(&sb
);
1618 die("file %s has only %lu lines", path
, lno
);
1620 ent
= xcalloc(1, sizeof(*ent
));
1622 ent
->num_lines
= top
- bottom
;
1624 ent
->s_lno
= bottom
;
1629 if (revs_file
&& read_ancestry(revs_file
))
1630 die("reading graft file %s failed: %s",
1631 revs_file
, strerror(errno
));
1633 assign_blame(&sb
, &revs
, opt
);
1637 if (!(output_option
& OUTPUT_PORCELAIN
))
1638 find_alignment(&sb
, &output_option
);
1640 output(&sb
, output_option
);
1641 free((void *)sb
.final_buf
);
1642 for (ent
= sb
.ent
; ent
; ) {
1643 struct blame_entry
*e
= ent
->next
;