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 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
146 static void sanity_check_refcnt(struct scoreboard
*);
148 static void coalesce(struct scoreboard
*sb
)
150 struct blame_entry
*ent
, *next
;
152 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
153 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
154 ent
->guilty
== next
->guilty
&&
155 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
156 ent
->num_lines
+= next
->num_lines
;
157 ent
->next
= next
->next
;
159 ent
->next
->prev
= ent
;
160 origin_decref(next
->suspect
);
163 next
= ent
; /* again */
167 if (DEBUG
) /* sanity */
168 sanity_check_refcnt(sb
);
171 static struct origin
*get_origin(struct scoreboard
*sb
,
172 struct commit
*commit
,
175 struct blame_entry
*e
;
178 for (e
= sb
->ent
; e
; e
= e
->next
) {
179 if (e
->suspect
->commit
== commit
&&
180 !strcmp(e
->suspect
->path
, path
))
181 return origin_incref(e
->suspect
);
183 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
186 strcpy(o
->path
, path
);
190 static int fill_blob_sha1(struct origin
*origin
)
195 if (!is_null_sha1(origin
->blob_sha1
))
197 if (get_tree_entry(origin
->commit
->object
.sha1
,
199 origin
->blob_sha1
, &mode
))
201 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
202 strcmp(type
, blob_type
))
206 hashclr(origin
->blob_sha1
);
210 static struct origin
*find_origin(struct scoreboard
*sb
,
211 struct commit
*parent
,
212 struct origin
*origin
)
214 struct origin
*porigin
= NULL
;
215 struct diff_options diff_opts
;
216 const char *paths
[2];
219 struct origin
*cached
= parent
->util
;
220 if (!strcmp(cached
->path
, origin
->path
))
221 return origin_incref(cached
);
224 /* See if the origin->path is different between parent
225 * and origin first. Most of the time they are the
226 * same and diff-tree is fairly efficient about this.
228 diff_setup(&diff_opts
);
229 diff_opts
.recursive
= 1;
230 diff_opts
.detect_rename
= 0;
231 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
232 paths
[0] = origin
->path
;
235 diff_tree_setup_paths(paths
, &diff_opts
);
236 if (diff_setup_done(&diff_opts
) < 0)
238 diff_tree_sha1(parent
->tree
->object
.sha1
,
239 origin
->commit
->tree
->object
.sha1
,
241 diffcore_std(&diff_opts
);
243 /* It is either one entry that says "modified", or "created",
246 if (!diff_queued_diff
.nr
) {
247 /* The path is the same as parent */
248 porigin
= get_origin(sb
, parent
, origin
->path
);
249 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
251 else if (diff_queued_diff
.nr
!= 1)
252 die("internal error in pickaxe::find_origin");
254 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
257 die("internal error in pickaxe::find_origin (%c)",
260 porigin
= get_origin(sb
, parent
, origin
->path
);
261 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
265 /* Did not exist in parent, or type changed */
269 diff_flush(&diff_opts
);
271 origin_incref(porigin
);
273 origin_decref(parent
->util
);
274 parent
->util
= porigin
;
279 static struct origin
*find_rename(struct scoreboard
*sb
,
280 struct commit
*parent
,
281 struct origin
*origin
)
283 struct origin
*porigin
= NULL
;
284 struct diff_options diff_opts
;
286 const char *paths
[2];
288 diff_setup(&diff_opts
);
289 diff_opts
.recursive
= 1;
290 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
291 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
292 diff_opts
.single_follow
= origin
->path
;
294 diff_tree_setup_paths(paths
, &diff_opts
);
295 if (diff_setup_done(&diff_opts
) < 0)
297 diff_tree_sha1(parent
->tree
->object
.sha1
,
298 origin
->commit
->tree
->object
.sha1
,
300 diffcore_std(&diff_opts
);
302 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
303 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
304 if ((p
->status
== 'R' || p
->status
== 'C') &&
305 !strcmp(p
->two
->path
, origin
->path
)) {
306 porigin
= get_origin(sb
, parent
, p
->one
->path
);
307 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
311 diff_flush(&diff_opts
);
316 /* line number in postimage; up to but not including this
317 * line is the same as preimage
321 /* preimage line number after this chunk */
324 /* postimage line number after this chunk */
329 struct chunk
*chunks
;
333 struct blame_diff_state
{
334 struct xdiff_emit_state xm
;
336 unsigned hunk_post_context
;
337 unsigned hunk_in_pre_context
: 1;
340 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
342 struct blame_diff_state
*state
= state_
;
344 int off1
, off2
, len1
, len2
, num
;
346 num
= state
->ret
->num
;
347 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
348 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
349 state
->ret
->chunks
[num
- 1].same
++;
351 state
->hunk_in_pre_context
= 0;
353 state
->hunk_post_context
++;
355 state
->hunk_post_context
= 0;
360 if (num
&& state
->hunk_post_context
) {
361 chunk
= &state
->ret
->chunks
[num
- 1];
362 chunk
->p_next
-= state
->hunk_post_context
;
363 chunk
->t_next
-= state
->hunk_post_context
;
365 state
->ret
->num
= ++num
;
366 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
367 sizeof(struct chunk
) * num
);
368 chunk
= &state
->ret
->chunks
[num
- 1];
369 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
374 /* Line numbers in patch output are one based. */
378 chunk
->same
= len2
? off2
: (off2
+ 1);
380 chunk
->p_next
= off1
+ (len1
? len1
: 1);
381 chunk
->t_next
= chunk
->same
+ len2
;
382 state
->hunk_in_pre_context
= 1;
383 state
->hunk_post_context
= 0;
386 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
389 struct blame_diff_state state
;
394 xpp
.flags
= XDF_NEED_MINIMAL
;
395 xecfg
.ctxlen
= context
;
397 ecb
.outf
= xdiff_outf
;
399 memset(&state
, 0, sizeof(state
));
400 state
.xm
.consume
= process_u_diff
;
401 state
.ret
= xmalloc(sizeof(struct patch
));
402 state
.ret
->chunks
= NULL
;
405 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
407 if (state
.ret
->num
) {
409 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
410 chunk
->p_next
-= state
.hunk_post_context
;
411 chunk
->t_next
-= state
.hunk_post_context
;
416 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
418 mmfile_t file_p
, file_o
;
420 char *blob_p
, *blob_o
;
423 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
424 (unsigned long *) &file_p
.size
);
425 blob_o
= read_sha1_file(origin
->blob_sha1
, type
,
426 (unsigned long *) &file_o
.size
);
429 if (!file_p
.ptr
|| !file_o
.ptr
) {
435 patch
= compare_buffer(&file_p
, &file_o
, 0);
441 static void free_patch(struct patch
*p
)
447 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
449 struct blame_entry
*ent
, *prev
= NULL
;
451 origin_incref(e
->suspect
);
453 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
456 /* prev, if not NULL, is the last one that is below e */
459 e
->next
= prev
->next
;
470 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
472 struct blame_entry
*p
, *n
;
476 origin_incref(src
->suspect
);
477 origin_decref(dst
->suspect
);
478 memcpy(dst
, src
, sizeof(*src
));
484 static const char *nth_line(struct scoreboard
*sb
, int lno
)
486 return sb
->final_buf
+ sb
->lineno
[lno
];
489 static void split_overlap(struct blame_entry
*split
,
490 struct blame_entry
*e
,
491 int tlno
, int plno
, int same
,
492 struct origin
*parent
)
494 /* it is known that lines between tlno to same came from
495 * parent, and e has an overlap with that range. it also is
496 * known that parent's line plno corresponds to e's line tlno.
502 * <------------------>
504 * Potentially we need to split e into three parts; before
505 * this chunk, the chunk to be blamed for parent, and after
509 memset(split
, 0, sizeof(struct blame_entry
[3]));
511 if (e
->s_lno
< tlno
) {
512 /* there is a pre-chunk part not blamed on parent */
513 split
[0].suspect
= origin_incref(e
->suspect
);
514 split
[0].lno
= e
->lno
;
515 split
[0].s_lno
= e
->s_lno
;
516 split
[0].num_lines
= tlno
- e
->s_lno
;
517 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
518 split
[1].s_lno
= plno
;
521 split
[1].lno
= e
->lno
;
522 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
525 if (same
< e
->s_lno
+ e
->num_lines
) {
526 /* there is a post-chunk part not blamed on parent */
527 split
[2].suspect
= origin_incref(e
->suspect
);
528 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
529 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
530 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
531 chunk_end_lno
= split
[2].lno
;
534 chunk_end_lno
= e
->lno
+ e
->num_lines
;
535 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
537 if (split
[1].num_lines
< 1)
539 split
[1].suspect
= origin_incref(parent
);
542 static void split_blame(struct scoreboard
*sb
,
543 struct blame_entry
*split
,
544 struct blame_entry
*e
)
546 struct blame_entry
*new_entry
;
548 if (split
[0].suspect
&& split
[2].suspect
) {
549 /* we need to split e into two and add another for parent */
550 dup_entry(e
, &split
[0]);
552 new_entry
= xmalloc(sizeof(*new_entry
));
553 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
554 add_blame_entry(sb
, new_entry
);
556 new_entry
= xmalloc(sizeof(*new_entry
));
557 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
558 add_blame_entry(sb
, new_entry
);
560 else if (!split
[0].suspect
&& !split
[2].suspect
)
561 /* parent covers the entire area */
562 dup_entry(e
, &split
[1]);
563 else if (split
[0].suspect
) {
564 dup_entry(e
, &split
[0]);
566 new_entry
= xmalloc(sizeof(*new_entry
));
567 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
568 add_blame_entry(sb
, new_entry
);
571 dup_entry(e
, &split
[1]);
573 new_entry
= xmalloc(sizeof(*new_entry
));
574 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
575 add_blame_entry(sb
, new_entry
);
578 if (DEBUG
) { /* sanity */
579 struct blame_entry
*ent
;
580 int lno
= sb
->ent
->lno
, corrupt
= 0;
582 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
587 lno
+= ent
->num_lines
;
591 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
592 printf("L %8d l %8d n %8d\n",
593 lno
, ent
->lno
, ent
->num_lines
);
594 lno
= ent
->lno
+ ent
->num_lines
;
601 static void decref_split(struct blame_entry
*split
)
605 for (i
= 0; i
< 3; i
++)
606 origin_decref(split
[i
].suspect
);
609 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
610 int tlno
, int plno
, int same
,
611 struct origin
*parent
)
613 struct blame_entry split
[3];
615 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
616 if (split
[1].suspect
)
617 split_blame(sb
, split
, e
);
621 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
623 struct blame_entry
*e
;
624 int last_in_target
= -1;
626 for (e
= sb
->ent
; e
; e
= e
->next
) {
627 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
629 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
630 last_in_target
= e
->s_lno
+ e
->num_lines
;
632 return last_in_target
;
635 static void blame_chunk(struct scoreboard
*sb
,
636 int tlno
, int plno
, int same
,
637 struct origin
*target
, struct origin
*parent
)
639 struct blame_entry
*e
;
641 for (e
= sb
->ent
; e
; e
= e
->next
) {
642 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
644 if (same
<= e
->s_lno
)
646 if (tlno
< e
->s_lno
+ e
->num_lines
)
647 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
651 static int pass_blame_to_parent(struct scoreboard
*sb
,
652 struct origin
*target
,
653 struct origin
*parent
)
655 int i
, last_in_target
, plno
, tlno
;
658 last_in_target
= find_last_in_target(sb
, target
);
659 if (last_in_target
< 0)
660 return 1; /* nothing remains for this target */
662 patch
= get_patch(parent
, target
);
664 for (i
= 0; i
< patch
->num
; i
++) {
665 struct chunk
*chunk
= &patch
->chunks
[i
];
667 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
668 plno
= chunk
->p_next
;
669 tlno
= chunk
->t_next
;
671 /* rest (i.e. anything above tlno) are the same as parent */
672 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
678 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
687 cp
= nth_line(sb
, e
->lno
);
688 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
690 unsigned ch
= *((unsigned char *)cp
);
699 static void copy_split_if_better(struct scoreboard
*sb
,
700 struct blame_entry
*best_so_far
,
701 struct blame_entry
*this)
705 if (!this[1].suspect
)
707 if (best_so_far
[1].suspect
) {
708 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
712 for (i
= 0; i
< 3; i
++)
713 origin_incref(this[i
].suspect
);
714 decref_split(best_so_far
);
715 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
718 static void find_copy_in_blob(struct scoreboard
*sb
,
719 struct blame_entry
*ent
,
720 struct origin
*parent
,
721 struct blame_entry
*split
,
730 cp
= nth_line(sb
, ent
->lno
);
731 file_o
.ptr
= (char*) cp
;
732 cnt
= ent
->num_lines
;
734 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
738 file_o
.size
= cp
- file_o
.ptr
;
740 patch
= compare_buffer(file_p
, &file_o
, 1);
742 memset(split
, 0, sizeof(struct blame_entry
[3]));
744 for (i
= 0; i
< patch
->num
; i
++) {
745 struct chunk
*chunk
= &patch
->chunks
[i
];
747 /* tlno to chunk->same are the same as ent */
748 if (ent
->num_lines
<= tlno
)
750 if (tlno
< chunk
->same
) {
751 struct blame_entry
this[3];
752 split_overlap(this, ent
,
753 tlno
+ ent
->s_lno
, plno
,
754 chunk
->same
+ ent
->s_lno
,
756 copy_split_if_better(sb
, split
, this);
759 plno
= chunk
->p_next
;
760 tlno
= chunk
->t_next
;
765 static int find_move_in_parent(struct scoreboard
*sb
,
766 struct origin
*target
,
767 struct origin
*parent
)
770 struct blame_entry
*e
, split
[3];
775 last_in_target
= find_last_in_target(sb
, target
);
776 if (last_in_target
< 0)
777 return 1; /* nothing remains for this target */
779 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
780 (unsigned long *) &file_p
.size
);
787 for (e
= sb
->ent
; e
; e
= e
->next
) {
788 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
790 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
791 if (split
[1].suspect
&&
792 blame_move_score
< ent_score(sb
, &split
[1]))
793 split_blame(sb
, split
, e
);
800 static int find_copy_in_parent(struct scoreboard
*sb
,
801 struct origin
*target
,
802 struct commit
*parent
,
803 struct origin
*porigin
,
806 struct diff_options diff_opts
;
807 const char *paths
[1];
808 struct blame_entry
*e
;
811 struct blame_entry
*ent
;
812 struct blame_entry split
[3];
816 /* Count the number of entries the target is suspected for,
817 * and prepare a list of entry and the best split.
819 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
820 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
823 return 1; /* nothing remains for this target */
825 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
826 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
827 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
828 blame_list
[i
++].ent
= e
;
830 diff_setup(&diff_opts
);
831 diff_opts
.recursive
= 1;
832 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
834 /* Try "find copies harder" on new path */
835 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
836 (!porigin
|| strcmp(target
->path
, porigin
->path
))) {
837 diff_opts
.detect_rename
= DIFF_DETECT_COPY
;
838 diff_opts
.find_copies_harder
= 1;
841 diff_tree_setup_paths(paths
, &diff_opts
);
842 if (diff_setup_done(&diff_opts
) < 0)
844 diff_tree_sha1(parent
->tree
->object
.sha1
,
845 target
->commit
->tree
->object
.sha1
,
847 diffcore_std(&diff_opts
);
849 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
850 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
851 struct origin
*norigin
;
855 struct blame_entry
this[3];
857 if (!DIFF_FILE_VALID(p
->one
))
858 continue; /* does not exist in parent */
859 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
860 /* find_move already dealt with this path */
863 norigin
= get_origin(sb
, parent
, p
->one
->path
);
864 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
865 blob
= read_sha1_file(norigin
->blob_sha1
, type
,
866 (unsigned long *) &file_p
.size
);
871 for (j
= 0; j
< num_ents
; j
++) {
872 find_copy_in_blob(sb
, blame_list
[j
].ent
, norigin
,
874 copy_split_if_better(sb
, blame_list
[j
].split
,
879 origin_decref(norigin
);
881 diff_flush(&diff_opts
);
883 for (j
= 0; j
< num_ents
; j
++) {
884 struct blame_entry
*split
= blame_list
[j
].split
;
885 if (split
[1].suspect
&&
886 blame_copy_score
< ent_score(sb
, &split
[1]))
887 split_blame(sb
, split
, blame_list
[j
].ent
);
897 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
900 struct commit
*commit
= origin
->commit
;
901 struct commit_list
*parent
;
902 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
904 memset(parent_origin
, 0, sizeof(parent_origin
));
906 /* The first pass looks for unrenamed path to optimize for
907 * common cases, then we look for renames in the second pass.
909 for (pass
= 0; pass
< 2; pass
++) {
910 struct origin
*(*find
)(struct scoreboard
*,
911 struct commit
*, struct origin
*);
912 find
= pass
? find_rename
: find_origin
;
914 for (i
= 0, parent
= commit
->parents
;
915 i
< MAXPARENT
&& parent
;
916 parent
= parent
->next
, i
++) {
917 struct commit
*p
= parent
->item
;
919 if (parent_origin
[i
])
923 porigin
= find(sb
, p
, origin
);
926 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
927 struct blame_entry
*e
;
928 for (e
= sb
->ent
; e
; e
= e
->next
)
929 if (e
->suspect
== origin
) {
930 origin_incref(porigin
);
931 origin_decref(e
->suspect
);
932 e
->suspect
= porigin
;
934 origin_decref(porigin
);
937 parent_origin
[i
] = porigin
;
941 for (i
= 0, parent
= commit
->parents
;
942 i
< MAXPARENT
&& parent
;
943 parent
= parent
->next
, i
++) {
944 struct origin
*porigin
= parent_origin
[i
];
947 if (pass_blame_to_parent(sb
, origin
, porigin
))
952 * Optionally run "miff" to find moves in parents' files here.
954 if (opt
& PICKAXE_BLAME_MOVE
)
955 for (i
= 0, parent
= commit
->parents
;
956 i
< MAXPARENT
&& parent
;
957 parent
= parent
->next
, i
++) {
958 struct origin
*porigin
= parent_origin
[i
];
961 if (find_move_in_parent(sb
, origin
, porigin
))
966 * Optionally run "ciff" to find copies from parents' files here.
968 if (opt
& PICKAXE_BLAME_COPY
)
969 for (i
= 0, parent
= commit
->parents
;
970 i
< MAXPARENT
&& parent
;
971 parent
= parent
->next
, i
++) {
972 struct origin
*porigin
= parent_origin
[i
];
973 if (find_copy_in_parent(sb
, origin
, parent
->item
,
979 for (i
= 0; i
< MAXPARENT
; i
++)
980 origin_decref(parent_origin
[i
]);
983 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
986 struct blame_entry
*ent
;
987 struct commit
*commit
;
988 struct origin
*suspect
= NULL
;
990 /* find one suspect to break down */
991 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
993 suspect
= ent
->suspect
;
995 return; /* all done */
997 origin_incref(suspect
);
998 commit
= suspect
->commit
;
999 parse_commit(commit
);
1000 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1001 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1002 pass_blame(sb
, suspect
, opt
);
1004 /* Take responsibility for the remaining entries */
1005 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1006 if (!cmp_suspect(ent
->suspect
, suspect
))
1008 origin_decref(suspect
);
1010 if (DEBUG
) /* sanity */
1011 sanity_check_refcnt(sb
);
1015 static const char *format_time(unsigned long time
, const char *tz_str
,
1018 static char time_buf
[128];
1023 if (show_raw_time
) {
1024 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1029 minutes
= tz
< 0 ? -tz
: tz
;
1030 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1031 minutes
= tz
< 0 ? -minutes
: minutes
;
1032 t
= time
+ minutes
* 60;
1035 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1036 strcat(time_buf
, tz_str
);
1044 unsigned long author_time
;
1047 /* filled only when asked for details */
1049 char *committer_mail
;
1050 unsigned long committer_time
;
1056 static void get_ac_line(const char *inbuf
, const char *what
,
1057 int bufsz
, char *person
, char **mail
,
1058 unsigned long *time
, char **tz
)
1063 tmp
= strstr(inbuf
, what
);
1066 tmp
+= strlen(what
);
1067 endp
= strchr(tmp
, '\n');
1075 person
= *mail
= *tz
= "(unknown)";
1079 memcpy(person
, tmp
, len
);
1091 *time
= strtoul(tmp
, NULL
, 10);
1100 static void get_commit_info(struct commit
*commit
,
1101 struct commit_info
*ret
,
1106 static char author_buf
[1024];
1107 static char committer_buf
[1024];
1108 static char summary_buf
[1024];
1110 /* We've operated without save_commit_buffer, so
1111 * we now need to populate them for output.
1113 if (!commit
->buffer
) {
1117 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1119 ret
->author
= author_buf
;
1120 get_ac_line(commit
->buffer
, "\nauthor ",
1121 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1122 &ret
->author_time
, &ret
->author_tz
);
1127 ret
->committer
= committer_buf
;
1128 get_ac_line(commit
->buffer
, "\ncommitter ",
1129 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1130 &ret
->committer_time
, &ret
->committer_tz
);
1132 ret
->summary
= summary_buf
;
1133 tmp
= strstr(commit
->buffer
, "\n\n");
1136 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1140 endp
= strchr(tmp
, '\n');
1144 if (len
>= sizeof(summary_buf
))
1146 memcpy(summary_buf
, tmp
, len
);
1147 summary_buf
[len
] = 0;
1150 #define OUTPUT_ANNOTATE_COMPAT 001
1151 #define OUTPUT_LONG_OBJECT_NAME 002
1152 #define OUTPUT_RAW_TIMESTAMP 004
1153 #define OUTPUT_PORCELAIN 010
1154 #define OUTPUT_SHOW_NAME 020
1155 #define OUTPUT_SHOW_NUMBER 040
1156 #define OUTPUT_SHOW_SCORE 0100
1158 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1162 struct origin
*suspect
= ent
->suspect
;
1165 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1166 printf("%s%c%d %d %d\n",
1168 ent
->guilty
? ' ' : '*', // purely for debugging
1172 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1173 struct commit_info ci
;
1174 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1175 get_commit_info(suspect
->commit
, &ci
, 1);
1176 printf("author %s\n", ci
.author
);
1177 printf("author-mail %s\n", ci
.author_mail
);
1178 printf("author-time %lu\n", ci
.author_time
);
1179 printf("author-tz %s\n", ci
.author_tz
);
1180 printf("committer %s\n", ci
.committer
);
1181 printf("committer-mail %s\n", ci
.committer_mail
);
1182 printf("committer-time %lu\n", ci
.committer_time
);
1183 printf("committer-tz %s\n", ci
.committer_tz
);
1184 printf("filename %s\n", suspect
->path
);
1185 printf("summary %s\n", ci
.summary
);
1187 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1188 printf("filename %s\n", suspect
->path
);
1190 cp
= nth_line(sb
, ent
->lno
);
1191 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1194 printf("%s %d %d\n", hex
,
1195 ent
->s_lno
+ 1 + cnt
,
1196 ent
->lno
+ 1 + cnt
);
1201 } while (ch
!= '\n' &&
1202 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1206 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1210 struct origin
*suspect
= ent
->suspect
;
1211 struct commit_info ci
;
1213 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1215 get_commit_info(suspect
->commit
, &ci
, 1);
1216 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1218 cp
= nth_line(sb
, ent
->lno
);
1219 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1222 printf("%.*s", (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8, hex
);
1223 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1224 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1225 format_time(ci
.author_time
, ci
.author_tz
,
1227 ent
->lno
+ 1 + cnt
);
1229 if (opt
& OUTPUT_SHOW_SCORE
)
1231 max_score_digits
, ent
->score
,
1232 ent
->suspect
->refcnt
);
1233 if (opt
& OUTPUT_SHOW_NAME
)
1234 printf(" %-*.*s", longest_file
, longest_file
,
1236 if (opt
& OUTPUT_SHOW_NUMBER
)
1237 printf(" %*d", max_orig_digits
,
1238 ent
->s_lno
+ 1 + cnt
);
1239 printf(" (%-*.*s %10s %*d) ",
1240 longest_author
, longest_author
, ci
.author
,
1241 format_time(ci
.author_time
, ci
.author_tz
,
1243 max_digits
, ent
->lno
+ 1 + cnt
);
1248 } while (ch
!= '\n' &&
1249 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1253 static void output(struct scoreboard
*sb
, int option
)
1255 struct blame_entry
*ent
;
1257 if (option
& OUTPUT_PORCELAIN
) {
1258 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1259 struct blame_entry
*oth
;
1260 struct origin
*suspect
= ent
->suspect
;
1261 struct commit
*commit
= suspect
->commit
;
1262 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1264 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1265 if ((oth
->suspect
->commit
!= commit
) ||
1266 !strcmp(oth
->suspect
->path
, suspect
->path
))
1268 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1274 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1275 if (option
& OUTPUT_PORCELAIN
)
1276 emit_porcelain(sb
, ent
);
1278 emit_other(sb
, ent
, option
);
1283 static int prepare_lines(struct scoreboard
*sb
)
1285 const char *buf
= sb
->final_buf
;
1286 unsigned long len
= sb
->final_buf_size
;
1287 int num
= 0, incomplete
= 0, bol
= 1;
1289 if (len
&& buf
[len
-1] != '\n')
1290 incomplete
++; /* incomplete line at the end */
1293 sb
->lineno
= xrealloc(sb
->lineno
,
1294 sizeof(int* ) * (num
+ 1));
1295 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1298 if (*buf
++ == '\n') {
1303 sb
->lineno
= xrealloc(sb
->lineno
,
1304 sizeof(int* ) * (num
+ incomplete
+ 1));
1305 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1306 sb
->num_lines
= num
+ incomplete
;
1307 return sb
->num_lines
;
1310 static int read_ancestry(const char *graft_file
)
1312 FILE *fp
= fopen(graft_file
, "r");
1316 while (fgets(buf
, sizeof(buf
), fp
)) {
1317 /* The format is just "Commit Parent1 Parent2 ...\n" */
1318 int len
= strlen(buf
);
1319 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1320 register_commit_graft(graft
, 0);
1326 static int lineno_width(int lines
)
1330 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1335 static void find_alignment(struct scoreboard
*sb
, int *option
)
1337 int longest_src_lines
= 0;
1338 int longest_dst_lines
= 0;
1339 unsigned largest_score
= 0;
1340 struct blame_entry
*e
;
1342 for (e
= sb
->ent
; e
; e
= e
->next
) {
1343 struct origin
*suspect
= e
->suspect
;
1344 struct commit_info ci
;
1347 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1348 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1349 get_commit_info(suspect
->commit
, &ci
, 1);
1350 if (strcmp(suspect
->path
, sb
->path
))
1351 *option
|= OUTPUT_SHOW_NAME
;
1352 num
= strlen(suspect
->path
);
1353 if (longest_file
< num
)
1355 num
= strlen(ci
.author
);
1356 if (longest_author
< num
)
1357 longest_author
= num
;
1359 num
= e
->s_lno
+ e
->num_lines
;
1360 if (longest_src_lines
< num
)
1361 longest_src_lines
= num
;
1362 num
= e
->lno
+ e
->num_lines
;
1363 if (longest_dst_lines
< num
)
1364 longest_dst_lines
= num
;
1365 if (largest_score
< ent_score(sb
, e
))
1366 largest_score
= ent_score(sb
, e
);
1368 max_orig_digits
= lineno_width(longest_src_lines
);
1369 max_digits
= lineno_width(longest_dst_lines
);
1370 max_score_digits
= lineno_width(largest_score
);
1373 static void sanity_check_refcnt(struct scoreboard
*sb
)
1376 struct blame_entry
*ent
;
1378 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1379 /* Nobody should have zero or negative refcnt */
1380 if (ent
->suspect
->refcnt
<= 0)
1383 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1384 /* Mark the ones that haven't been checked */
1385 if (0 < ent
->suspect
->refcnt
)
1386 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1388 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1389 /* then pick each and see if they have the the correct
1390 * refcnt; note that ->util caching means origin's refcnt
1391 * may well be greater than the number of blame entries
1395 struct blame_entry
*e
;
1396 struct origin
*suspect
= ent
->suspect
;
1398 if (0 < suspect
->refcnt
)
1400 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1401 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1402 if (e
->suspect
!= suspect
)
1406 if (suspect
->refcnt
< found
)
1411 find_alignment(sb
, &opt
);
1417 static int has_path_in_work_tree(const char *path
)
1420 return !lstat(path
, &st
);
1423 static unsigned parse_score(const char *arg
)
1426 unsigned long score
= strtoul(arg
, &end
, 10);
1432 static const char *add_prefix(const char *prefix
, const char *path
)
1434 if (!prefix
|| !prefix
[0])
1436 return prefix_path(prefix
, strlen(prefix
), path
);
1439 int cmd_pickaxe(int argc
, const char **argv
, const char *prefix
)
1441 struct rev_info revs
;
1443 struct scoreboard sb
;
1445 struct blame_entry
*ent
;
1446 int i
, seen_dashdash
, unk
, opt
;
1447 long bottom
, top
, lno
;
1448 int output_option
= 0;
1449 const char *revs_file
= NULL
;
1450 const char *final_commit_name
= NULL
;
1453 save_commit_buffer
= 0;
1458 for (unk
= i
= 1; i
< argc
; i
++) {
1459 const char *arg
= argv
[i
];
1462 else if (!strcmp("-c", arg
))
1463 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1464 else if (!strcmp("-t", arg
))
1465 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1466 else if (!strcmp("-l", arg
))
1467 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1468 else if (!strcmp("-S", arg
) && ++i
< argc
)
1469 revs_file
= argv
[i
];
1470 else if (!strncmp("-M", arg
, 2)) {
1471 opt
|= PICKAXE_BLAME_MOVE
;
1472 blame_move_score
= parse_score(arg
+2);
1474 else if (!strncmp("-C", arg
, 2)) {
1475 if (opt
& PICKAXE_BLAME_COPY
)
1476 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1477 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1478 blame_copy_score
= parse_score(arg
+2);
1480 else if (!strncmp("-L", arg
, 2)) {
1484 usage(pickaxe_usage
);
1490 die("More than one '-L n,m' option given");
1491 bottom
= strtol(arg
, &term
, 10);
1493 top
= strtol(term
+ 1, &term
, 10);
1495 usage(pickaxe_usage
);
1497 if (bottom
&& top
&& top
< bottom
) {
1499 tmp
= top
; top
= bottom
; bottom
= tmp
;
1502 else if (!strcmp("--score-debug", arg
))
1503 output_option
|= OUTPUT_SHOW_SCORE
;
1504 else if (!strcmp("-f", arg
) ||
1505 !strcmp("--show-name", arg
))
1506 output_option
|= OUTPUT_SHOW_NAME
;
1507 else if (!strcmp("-n", arg
) ||
1508 !strcmp("--show-number", arg
))
1509 output_option
|= OUTPUT_SHOW_NUMBER
;
1510 else if (!strcmp("-p", arg
) ||
1511 !strcmp("--porcelain", arg
))
1512 output_option
|= OUTPUT_PORCELAIN
;
1513 else if (!strcmp("--", arg
)) {
1522 if (!blame_move_score
)
1523 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1524 if (!blame_copy_score
)
1525 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1527 /* We have collected options unknown to us in argv[1..unk]
1528 * which are to be passed to revision machinery if we are
1529 * going to do the "bottom" procesing.
1531 * The remaining are:
1533 * (1) if seen_dashdash, its either
1534 * "-options -- <path>" or
1535 * "-options -- <path> <rev>".
1536 * but the latter is allowed only if there is no
1537 * options that we passed to revision machinery.
1539 * (2) otherwise, we may have "--" somewhere later and
1540 * might be looking at the first one of multiple 'rev'
1541 * parameters (e.g. " master ^next ^maint -- path").
1542 * See if there is a dashdash first, and give the
1543 * arguments before that to revision machinery.
1544 * After that there must be one 'path'.
1546 * (3) otherwise, its one of the three:
1547 * "-options <path> <rev>"
1548 * "-options <rev> <path>"
1550 * but again the first one is allowed only if
1551 * there is no options that we passed to revision
1555 if (seen_dashdash
) {
1558 usage(pickaxe_usage
);
1559 path
= add_prefix(prefix
, argv
[i
]);
1560 if (i
+ 1 == argc
- 1) {
1562 usage(pickaxe_usage
);
1563 argv
[unk
++] = argv
[i
+ 1];
1565 else if (i
+ 1 != argc
)
1566 /* garbage at end */
1567 usage(pickaxe_usage
);
1571 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1572 if (!strcmp(argv
[j
], "--"))
1574 if (seen_dashdash
) {
1575 if (seen_dashdash
+ 1 != argc
- 1)
1576 usage(pickaxe_usage
);
1577 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
1578 for (j
= i
; j
< seen_dashdash
; j
++)
1579 argv
[unk
++] = argv
[j
];
1583 path
= add_prefix(prefix
, argv
[i
]);
1584 if (i
+ 1 == argc
- 1) {
1585 final_commit_name
= argv
[i
+ 1];
1587 /* if (unk == 1) we could be getting
1590 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1591 path
= add_prefix(prefix
, argv
[i
+ 1]);
1592 final_commit_name
= argv
[i
];
1595 else if (i
!= argc
- 1)
1596 usage(pickaxe_usage
); /* garbage at end */
1598 if (!has_path_in_work_tree(path
))
1599 die("cannot stat path %s: %s",
1600 path
, strerror(errno
));
1604 if (final_commit_name
)
1605 argv
[unk
++] = final_commit_name
;
1607 /* Now we got rev and path. We do not want the path pruning
1608 * but we may want "bottom" processing.
1612 init_revisions(&revs
, NULL
);
1613 setup_revisions(unk
, argv
, &revs
, "HEAD");
1614 memset(&sb
, 0, sizeof(sb
));
1616 /* There must be one and only one positive commit in the
1617 * revs->pending array.
1619 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1620 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1621 if (obj
->flags
& UNINTERESTING
)
1623 while (obj
->type
== OBJ_TAG
)
1624 obj
= deref_tag(obj
, NULL
, 0);
1625 if (obj
->type
!= OBJ_COMMIT
)
1626 die("Non commit %s?",
1627 revs
.pending
.objects
[i
].name
);
1629 die("More than one commit to dig from %s and %s?",
1630 revs
.pending
.objects
[i
].name
,
1632 sb
.final
= (struct commit
*) obj
;
1633 final_commit_name
= revs
.pending
.objects
[i
].name
;
1637 /* "--not A B -- path" without anything positive */
1638 unsigned char head_sha1
[20];
1640 final_commit_name
= "HEAD";
1641 if (get_sha1(final_commit_name
, head_sha1
))
1642 die("No such ref: HEAD");
1643 sb
.final
= lookup_commit_reference(head_sha1
);
1644 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1647 /* If we have bottom, this will mark the ancestors of the
1648 * bottom commits we would reach while traversing as
1651 prepare_revision_walk(&revs
);
1653 o
= get_origin(&sb
, sb
.final
, path
);
1654 if (fill_blob_sha1(o
))
1655 die("no such path %s in %s", path
, final_commit_name
);
1657 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1658 lno
= prepare_lines(&sb
);
1666 die("file %s has only %lu lines", path
, lno
);
1668 ent
= xcalloc(1, sizeof(*ent
));
1670 ent
->num_lines
= top
- bottom
;
1672 ent
->s_lno
= bottom
;
1677 if (revs_file
&& read_ancestry(revs_file
))
1678 die("reading graft file %s failed: %s",
1679 revs_file
, strerror(errno
));
1681 assign_blame(&sb
, &revs
, opt
);
1685 if (!(output_option
& OUTPUT_PORCELAIN
))
1686 find_alignment(&sb
, &output_option
);
1688 output(&sb
, output_option
);
1689 free((void *)sb
.final_buf
);
1690 for (ent
= sb
.ent
; ent
; ) {
1691 struct blame_entry
*e
= ent
->next
;