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
;
40 #define PICKAXE_BLAME_MOVE 01
41 #define PICKAXE_BLAME_COPY 02
42 #define PICKAXE_BLAME_COPY_HARDER 04
44 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
45 #define METAINFO_SHOWN (1u<<12)
46 #define MORE_THAN_ONE_PATH (1u<<13)
49 * One blob in a commit
52 struct commit
*commit
;
53 unsigned char blob_sha1
[20];
54 char path
[FLEX_ARRAY
];
58 struct blame_entry
*prev
;
59 struct blame_entry
*next
;
61 /* the first line of this group in the final image;
62 * internally all line numbers are 0 based.
66 /* how many lines this group has */
69 /* the commit that introduced this group into the final image */
70 struct origin
*suspect
;
72 /* true if the suspect is truly guilty; false while we have not
73 * checked if the group came from one of its parents.
77 /* the line number of the first line of this group in the
78 * suspect's file; internally all line numbers are 0 based.
84 /* the final commit (i.e. where we started digging from) */
89 /* the contents in the final; pointed into by buf pointers of
92 const char *final_buf
;
93 unsigned long final_buf_size
;
95 /* linked list of blames */
96 struct blame_entry
*ent
;
102 static void coalesce(struct scoreboard
*sb
)
104 struct blame_entry
*ent
, *next
;
106 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
107 if (ent
->suspect
== next
->suspect
&&
108 ent
->guilty
== next
->guilty
&&
109 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
110 ent
->num_lines
+= next
->num_lines
;
111 ent
->next
= next
->next
;
113 ent
->next
->prev
= ent
;
115 next
= ent
; /* again */
120 static void free_origin(struct origin
*o
)
125 static struct origin
*find_origin(struct scoreboard
*sb
,
126 struct commit
*commit
,
129 struct blame_entry
*ent
;
134 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
135 if (ent
->suspect
->commit
== commit
&&
136 !strcmp(ent
->suspect
->path
, path
))
140 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
142 strcpy(o
->path
, path
);
143 if (get_tree_entry(commit
->object
.sha1
, path
, o
->blob_sha1
, &mode
))
145 if (sha1_object_info(o
->blob_sha1
, type
, NULL
) ||
146 strcmp(type
, blob_type
))
154 static struct origin
*find_rename(struct scoreboard
*sb
,
155 struct commit
*parent
,
156 struct origin
*origin
)
158 struct origin
*porigin
= NULL
;
159 struct diff_options diff_opts
;
161 const char *paths
[1];
163 diff_setup(&diff_opts
);
164 diff_opts
.recursive
= 1;
165 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
166 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
168 diff_tree_setup_paths(paths
, &diff_opts
);
169 if (diff_setup_done(&diff_opts
) < 0)
171 diff_tree_sha1(origin
->commit
->tree
->object
.sha1
,
172 parent
->tree
->object
.sha1
,
174 diffcore_std(&diff_opts
);
176 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
177 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
178 if (p
->status
== 'R' && !strcmp(p
->one
->path
, origin
->path
)) {
179 porigin
= find_origin(sb
, parent
, p
->two
->path
);
183 diff_flush(&diff_opts
);
188 /* line number in postimage; up to but not including this
189 * line is the same as preimage
193 /* preimage line number after this chunk */
196 /* postimage line number after this chunk */
201 struct chunk
*chunks
;
205 struct blame_diff_state
{
206 struct xdiff_emit_state xm
;
208 unsigned hunk_post_context
;
209 unsigned hunk_in_pre_context
: 1;
212 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
214 struct blame_diff_state
*state
= state_
;
216 int off1
, off2
, len1
, len2
, num
;
219 fprintf(stderr
, "%.*s", (int) len
, line
);
221 num
= state
->ret
->num
;
222 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
223 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
224 state
->ret
->chunks
[num
- 1].same
++;
226 state
->hunk_in_pre_context
= 0;
228 state
->hunk_post_context
++;
230 state
->hunk_post_context
= 0;
235 if (num
&& state
->hunk_post_context
) {
236 chunk
= &state
->ret
->chunks
[num
- 1];
237 chunk
->p_next
-= state
->hunk_post_context
;
238 chunk
->t_next
-= state
->hunk_post_context
;
240 state
->ret
->num
= ++num
;
241 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
242 sizeof(struct chunk
) * num
);
243 chunk
= &state
->ret
->chunks
[num
- 1];
244 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
249 /* Line numbers in patch output are one based. */
253 chunk
->same
= len2
? off2
: (off2
+ 1);
255 chunk
->p_next
= off1
+ (len1
? len1
: 1);
256 chunk
->t_next
= chunk
->same
+ len2
;
257 state
->hunk_in_pre_context
= 1;
258 state
->hunk_post_context
= 0;
261 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
264 struct blame_diff_state state
;
269 xpp
.flags
= XDF_NEED_MINIMAL
;
270 xecfg
.ctxlen
= context
;
272 ecb
.outf
= xdiff_outf
;
274 memset(&state
, 0, sizeof(state
));
275 state
.xm
.consume
= process_u_diff
;
276 state
.ret
= xmalloc(sizeof(struct patch
));
277 state
.ret
->chunks
= NULL
;
280 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
282 if (state
.ret
->num
) {
284 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
285 chunk
->p_next
-= state
.hunk_post_context
;
286 chunk
->t_next
-= state
.hunk_post_context
;
291 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
293 mmfile_t file_p
, file_o
;
295 char *blob_p
, *blob_o
;
298 if (DEBUG
) fprintf(stderr
, "get patch %.8s %.8s\n",
299 sha1_to_hex(parent
->commit
->object
.sha1
),
300 sha1_to_hex(origin
->commit
->object
.sha1
));
302 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
303 (unsigned long *) &file_p
.size
);
304 blob_o
= read_sha1_file(origin
->blob_sha1
, type
,
305 (unsigned long *) &file_o
.size
);
308 if (!file_p
.ptr
|| !file_o
.ptr
) {
314 patch
= compare_buffer(&file_p
, &file_o
, 0);
320 static void free_patch(struct patch
*p
)
326 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
328 struct blame_entry
*ent
, *prev
= NULL
;
330 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
333 /* prev, if not NULL, is the last one that is below e */
336 e
->next
= prev
->next
;
347 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
349 struct blame_entry
*p
, *n
;
352 memcpy(dst
, src
, sizeof(*src
));
357 static const char *nth_line(struct scoreboard
*sb
, int lno
)
359 return sb
->final_buf
+ sb
->lineno
[lno
];
362 static void split_overlap(struct blame_entry split
[3],
363 struct blame_entry
*e
,
364 int tlno
, int plno
, int same
,
365 struct origin
*parent
)
367 /* it is known that lines between tlno to same came from
368 * parent, and e has an overlap with that range. it also is
369 * known that parent's line plno corresponds to e's line tlno.
375 * <------------------>
377 * Potentially we need to split e into three parts; before
378 * this chunk, the chunk to be blamed for parent, and after
382 memset(split
, 0, sizeof(struct blame_entry
[3]));
384 if (e
->s_lno
< tlno
) {
385 /* there is a pre-chunk part not blamed on parent */
386 split
[0].suspect
= e
->suspect
;
387 split
[0].lno
= e
->lno
;
388 split
[0].s_lno
= e
->s_lno
;
389 split
[0].num_lines
= tlno
- e
->s_lno
;
390 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
391 split
[1].s_lno
= plno
;
394 split
[1].lno
= e
->lno
;
395 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
398 if (same
< e
->s_lno
+ e
->num_lines
) {
399 /* there is a post-chunk part not blamed on parent */
400 split
[2].suspect
= e
->suspect
;
401 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
402 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
403 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
404 chunk_end_lno
= split
[2].lno
;
407 chunk_end_lno
= e
->lno
+ e
->num_lines
;
408 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
410 if (split
[1].num_lines
< 1)
412 split
[1].suspect
= parent
;
415 static void split_blame(struct scoreboard
*sb
,
416 struct blame_entry split
[3],
417 struct blame_entry
*e
)
419 struct blame_entry
*new_entry
;
421 if (split
[0].suspect
&& split
[2].suspect
) {
422 /* we need to split e into two and add another for parent */
423 dup_entry(e
, &split
[0]);
425 new_entry
= xmalloc(sizeof(*new_entry
));
426 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
427 add_blame_entry(sb
, new_entry
);
429 new_entry
= xmalloc(sizeof(*new_entry
));
430 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
431 add_blame_entry(sb
, new_entry
);
433 else if (!split
[0].suspect
&& !split
[2].suspect
)
434 /* parent covers the entire area */
435 dup_entry(e
, &split
[1]);
436 else if (split
[0].suspect
) {
437 dup_entry(e
, &split
[0]);
439 new_entry
= xmalloc(sizeof(*new_entry
));
440 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
441 add_blame_entry(sb
, new_entry
);
444 dup_entry(e
, &split
[1]);
446 new_entry
= xmalloc(sizeof(*new_entry
));
447 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
448 add_blame_entry(sb
, new_entry
);
452 struct blame_entry
*ent
;
453 int lno
= 0, corrupt
= 0;
455 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
460 lno
+= ent
->num_lines
;
464 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
465 printf("L %8d l %8d n %8d\n",
466 lno
, ent
->lno
, ent
->num_lines
);
467 lno
= ent
->lno
+ ent
->num_lines
;
474 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
475 int tlno
, int plno
, int same
,
476 struct origin
*parent
)
478 struct blame_entry split
[3];
480 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
481 if (!split
[1].suspect
)
483 split_blame(sb
, split
, e
);
486 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
488 struct blame_entry
*e
;
489 int last_in_target
= -1;
491 for (e
= sb
->ent
; e
; e
= e
->next
) {
492 if (e
->guilty
|| e
->suspect
!= target
)
494 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
495 last_in_target
= e
->s_lno
+ e
->num_lines
;
497 return last_in_target
;
500 static void blame_chunk(struct scoreboard
*sb
,
501 int tlno
, int plno
, int same
,
502 struct origin
*target
, struct origin
*parent
)
504 struct blame_entry
*e
, *n
;
506 for (e
= sb
->ent
; e
; e
= n
) {
508 if (e
->guilty
|| e
->suspect
!= target
)
510 if (same
<= e
->s_lno
)
512 if (tlno
< e
->s_lno
+ e
->num_lines
)
513 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
517 static int pass_blame_to_parent(struct scoreboard
*sb
,
518 struct origin
*target
,
519 struct origin
*parent
)
521 int i
, last_in_target
, plno
, tlno
;
524 last_in_target
= find_last_in_target(sb
, target
);
525 if (last_in_target
< 0)
526 return 1; /* nothing remains for this target */
528 patch
= get_patch(parent
, target
);
530 for (i
= 0; i
< patch
->num
; i
++) {
531 struct chunk
*chunk
= &patch
->chunks
[i
];
535 "plno = %d, tlno = %d, "
536 "same as parent up to %d, resync %d and %d\n",
538 chunk
->same
, chunk
->p_next
, chunk
->t_next
);
539 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
540 plno
= chunk
->p_next
;
541 tlno
= chunk
->t_next
;
543 /* rest (i.e. anything above tlno) are the same as parent */
544 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
550 static void copy_split_if_better(struct blame_entry best_so_far
[3],
551 struct blame_entry
this[3])
553 if (!this[1].suspect
)
555 if (best_so_far
[1].suspect
&&
556 (this[1].num_lines
< best_so_far
[1].num_lines
))
558 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
561 static void find_copy_in_blob(struct scoreboard
*sb
,
562 struct blame_entry
*ent
,
563 struct origin
*parent
,
564 struct blame_entry split
[3],
573 cp
= nth_line(sb
, ent
->lno
);
574 file_o
.ptr
= (char*) cp
;
575 cnt
= ent
->num_lines
;
577 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
581 file_o
.size
= cp
- file_o
.ptr
;
583 patch
= compare_buffer(file_p
, &file_o
, 1);
585 memset(split
, 0, sizeof(struct blame_entry
[3]));
587 for (i
= 0; i
< patch
->num
; i
++) {
588 struct chunk
*chunk
= &patch
->chunks
[i
];
590 /* tlno to chunk->same are the same as ent */
591 if (ent
->num_lines
<= tlno
)
593 if (tlno
< chunk
->same
) {
594 struct blame_entry
this[3];
595 split_overlap(this, ent
,
596 tlno
+ ent
->s_lno
, plno
,
597 chunk
->same
+ ent
->s_lno
,
599 copy_split_if_better(split
, this);
601 plno
= chunk
->p_next
;
602 tlno
= chunk
->t_next
;
607 static int find_move_in_parent(struct scoreboard
*sb
,
608 struct origin
*target
,
609 struct origin
*parent
)
612 struct blame_entry
*ent
, split
[3];
617 last_in_target
= find_last_in_target(sb
, target
);
618 if (last_in_target
< 0)
619 return 1; /* nothing remains for this target */
621 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
622 (unsigned long *) &file_p
.size
);
629 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
630 if (ent
->suspect
!= target
|| ent
->guilty
)
632 find_copy_in_blob(sb
, ent
, parent
, split
, &file_p
);
633 if (split
[1].suspect
)
634 split_blame(sb
, split
, ent
);
640 static int find_copy_in_parent(struct scoreboard
*sb
,
641 struct origin
*target
,
642 struct commit
*parent
,
643 struct origin
*porigin
,
646 struct diff_options diff_opts
;
647 const char *paths
[1];
648 struct blame_entry
*ent
;
651 if (find_last_in_target(sb
, target
) < 0)
652 return 1; /* nothing remains for this target */
654 diff_setup(&diff_opts
);
655 diff_opts
.recursive
= 1;
656 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
658 /* Try "find copies harder" on new path */
659 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
660 (!porigin
|| strcmp(target
->path
, porigin
->path
))) {
661 diff_opts
.detect_rename
= DIFF_DETECT_COPY
;
662 diff_opts
.find_copies_harder
= 1;
665 diff_tree_setup_paths(paths
, &diff_opts
);
666 if (diff_setup_done(&diff_opts
) < 0)
668 diff_tree_sha1(parent
->tree
->object
.sha1
,
669 target
->commit
->tree
->object
.sha1
,
671 diffcore_std(&diff_opts
);
673 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
674 struct blame_entry split
[3];
675 if (ent
->suspect
!= target
|| ent
->guilty
)
678 memset(split
, 0, sizeof(split
));
679 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
680 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
681 struct origin
*norigin
;
685 struct blame_entry
this[3];
687 if (!DIFF_FILE_VALID(p
->one
))
688 continue; /* does not exist in parent */
689 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
690 /* find_move already dealt with this path */
692 norigin
= find_origin(sb
, parent
, p
->one
->path
);
694 blob
= read_sha1_file(norigin
->blob_sha1
, type
,
695 (unsigned long *) &file_p
.size
);
701 find_copy_in_blob(sb
, ent
, norigin
, this, &file_p
);
702 copy_split_if_better(split
, this);
704 if (split
[1].suspect
)
705 split_blame(sb
, split
, ent
);
707 diff_flush(&diff_opts
);
714 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
717 struct commit
*commit
= origin
->commit
;
718 struct commit_list
*parent
;
719 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
721 memset(parent_origin
, 0, sizeof(parent_origin
));
722 for (i
= 0, parent
= commit
->parents
;
723 i
< MAXPARENT
&& parent
;
724 parent
= parent
->next
, i
++) {
725 struct commit
*p
= parent
->item
;
729 porigin
= find_origin(sb
, parent
->item
, origin
->path
);
731 porigin
= find_rename(sb
, parent
->item
, origin
);
734 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
735 struct blame_entry
*e
;
736 for (e
= sb
->ent
; e
; e
= e
->next
)
737 if (e
->suspect
== origin
)
738 e
->suspect
= porigin
;
739 /* now everything blamed for origin is blamed for
740 * porigin, we do not need to keep it anymore.
741 * Do not free porigin (or the ones we got from
742 * earlier round); they may still be used elsewhere.
747 parent_origin
[i
] = porigin
;
750 for (i
= 0, parent
= commit
->parents
;
751 i
< MAXPARENT
&& parent
;
752 parent
= parent
->next
, i
++) {
753 struct origin
*porigin
= parent_origin
[i
];
756 if (pass_blame_to_parent(sb
, origin
, porigin
))
761 * Optionally run "miff" to find moves in parents' files here.
763 if (opt
& PICKAXE_BLAME_MOVE
)
764 for (i
= 0, parent
= commit
->parents
;
765 i
< MAXPARENT
&& parent
;
766 parent
= parent
->next
, i
++) {
767 struct origin
*porigin
= parent_origin
[i
];
770 if (find_move_in_parent(sb
, origin
, porigin
))
775 * Optionally run "ciff" to find copies from parents' files here.
777 if (opt
& PICKAXE_BLAME_COPY
)
778 for (i
= 0, parent
= commit
->parents
;
779 i
< MAXPARENT
&& parent
;
780 parent
= parent
->next
, i
++) {
781 struct origin
*porigin
= parent_origin
[i
];
782 if (find_copy_in_parent(sb
, origin
, parent
->item
,
788 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
791 struct blame_entry
*ent
;
792 struct commit
*commit
;
793 struct origin
*suspect
= NULL
;
795 /* find one suspect to break down */
796 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
798 suspect
= ent
->suspect
;
800 return; /* all done */
802 commit
= suspect
->commit
;
803 parse_commit(commit
);
804 if (!(commit
->object
.flags
& UNINTERESTING
) &&
805 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
806 pass_blame(sb
, suspect
, opt
);
808 /* Take responsibility for the remaining entries */
809 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
810 if (ent
->suspect
== suspect
)
815 static const char *format_time(unsigned long time
, const char *tz_str
,
818 static char time_buf
[128];
824 sprintf(time_buf
, "%lu %s", time
, tz_str
);
829 minutes
= tz
< 0 ? -tz
: tz
;
830 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
831 minutes
= tz
< 0 ? -minutes
: minutes
;
832 t
= time
+ minutes
* 60;
835 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
836 strcat(time_buf
, tz_str
);
844 unsigned long author_time
;
847 /* filled only when asked for details */
849 char *committer_mail
;
850 unsigned long committer_time
;
856 static void get_ac_line(const char *inbuf
, const char *what
,
857 int bufsz
, char *person
, char **mail
,
858 unsigned long *time
, char **tz
)
863 tmp
= strstr(inbuf
, what
);
867 endp
= strchr(tmp
, '\n');
875 person
= *mail
= *tz
= "(unknown)";
879 memcpy(person
, tmp
, len
);
891 *time
= strtoul(tmp
, NULL
, 10);
900 static void get_commit_info(struct commit
*commit
,
901 struct commit_info
*ret
,
906 static char author_buf
[1024];
907 static char committer_buf
[1024];
908 static char summary_buf
[1024];
910 ret
->author
= author_buf
;
911 get_ac_line(commit
->buffer
, "\nauthor ",
912 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
913 &ret
->author_time
, &ret
->author_tz
);
918 ret
->committer
= committer_buf
;
919 get_ac_line(commit
->buffer
, "\ncommitter ",
920 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
921 &ret
->committer_time
, &ret
->committer_tz
);
923 ret
->summary
= summary_buf
;
924 tmp
= strstr(commit
->buffer
, "\n\n");
927 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
931 endp
= strchr(tmp
, '\n');
935 if (len
>= sizeof(summary_buf
))
937 memcpy(summary_buf
, tmp
, len
);
938 summary_buf
[len
] = 0;
941 #define OUTPUT_ANNOTATE_COMPAT 001
942 #define OUTPUT_LONG_OBJECT_NAME 002
943 #define OUTPUT_RAW_TIMESTAMP 004
944 #define OUTPUT_PORCELAIN 010
945 #define OUTPUT_SHOW_NAME 020
946 #define OUTPUT_SHOW_NUMBER 040
948 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
952 struct origin
*suspect
= ent
->suspect
;
955 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
956 printf("%s%c%d %d %d\n",
958 ent
->guilty
? ' ' : '*', // purely for debugging
962 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
963 struct commit_info ci
;
964 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
965 get_commit_info(suspect
->commit
, &ci
, 1);
966 printf("author %s\n", ci
.author
);
967 printf("author-mail %s\n", ci
.author_mail
);
968 printf("author-time %lu\n", ci
.author_time
);
969 printf("author-tz %s\n", ci
.author_tz
);
970 printf("committer %s\n", ci
.committer
);
971 printf("committer-mail %s\n", ci
.committer_mail
);
972 printf("committer-time %lu\n", ci
.committer_time
);
973 printf("committer-tz %s\n", ci
.committer_tz
);
974 printf("filename %s\n", suspect
->path
);
975 printf("summary %s\n", ci
.summary
);
977 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
978 printf("filename %s\n", suspect
->path
);
980 cp
= nth_line(sb
, ent
->lno
);
981 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
984 printf("%s %d %d\n", hex
,
985 ent
->s_lno
+ 1 + cnt
,
991 } while (ch
!= '\n' &&
992 cp
< sb
->final_buf
+ sb
->final_buf_size
);
996 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1000 struct origin
*suspect
= ent
->suspect
;
1001 struct commit_info ci
;
1003 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1005 get_commit_info(suspect
->commit
, &ci
, 1);
1006 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1008 cp
= nth_line(sb
, ent
->lno
);
1009 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1012 printf("%.*s", (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8, hex
);
1013 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1014 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1015 format_time(ci
.author_time
, ci
.author_tz
,
1017 ent
->lno
+ 1 + cnt
);
1019 if (opt
& OUTPUT_SHOW_NAME
)
1020 printf(" %-*.*s", longest_file
, longest_file
,
1022 if (opt
& OUTPUT_SHOW_NUMBER
)
1023 printf(" %*d", max_orig_digits
,
1024 ent
->s_lno
+ 1 + cnt
);
1025 printf(" (%-*.*s %10s %*d) ",
1026 longest_author
, longest_author
, ci
.author
,
1027 format_time(ci
.author_time
, ci
.author_tz
,
1029 max_digits
, ent
->lno
+ 1 + cnt
);
1034 } while (ch
!= '\n' &&
1035 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1039 static void output(struct scoreboard
*sb
, int option
)
1041 struct blame_entry
*ent
;
1043 if (option
& OUTPUT_PORCELAIN
) {
1044 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1045 struct blame_entry
*oth
;
1046 struct origin
*suspect
= ent
->suspect
;
1047 struct commit
*commit
= suspect
->commit
;
1048 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1050 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1051 if ((oth
->suspect
->commit
!= commit
) ||
1052 !strcmp(oth
->suspect
->path
, suspect
->path
))
1054 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1060 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1061 if (option
& OUTPUT_PORCELAIN
)
1062 emit_porcelain(sb
, ent
);
1064 emit_other(sb
, ent
, option
);
1068 static int prepare_lines(struct scoreboard
*sb
)
1070 const char *buf
= sb
->final_buf
;
1071 unsigned long len
= sb
->final_buf_size
;
1072 int num
= 0, incomplete
= 0, bol
= 1;
1074 if (len
&& buf
[len
-1] != '\n')
1075 incomplete
++; /* incomplete line at the end */
1078 sb
->lineno
= xrealloc(sb
->lineno
,
1079 sizeof(int* ) * (num
+ 1));
1080 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1083 if (*buf
++ == '\n') {
1088 sb
->num_lines
= num
+ incomplete
;
1089 return sb
->num_lines
;
1092 static int read_ancestry(const char *graft_file
)
1094 FILE *fp
= fopen(graft_file
, "r");
1098 while (fgets(buf
, sizeof(buf
), fp
)) {
1099 /* The format is just "Commit Parent1 Parent2 ...\n" */
1100 int len
= strlen(buf
);
1101 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1102 register_commit_graft(graft
, 0);
1108 static int lineno_width(int lines
)
1112 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1117 static void find_alignment(struct scoreboard
*sb
, int *option
)
1119 int longest_src_lines
= 0;
1120 int longest_dst_lines
= 0;
1121 struct blame_entry
*e
;
1123 for (e
= sb
->ent
; e
; e
= e
->next
) {
1124 struct origin
*suspect
= e
->suspect
;
1125 struct commit_info ci
;
1128 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1129 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1130 get_commit_info(suspect
->commit
, &ci
, 1);
1131 if (strcmp(suspect
->path
, sb
->path
))
1132 *option
|= OUTPUT_SHOW_NAME
;
1133 num
= strlen(suspect
->path
);
1134 if (longest_file
< num
)
1136 num
= strlen(ci
.author
);
1137 if (longest_author
< num
)
1138 longest_author
= num
;
1140 num
= e
->s_lno
+ e
->num_lines
;
1141 if (longest_src_lines
< num
)
1142 longest_src_lines
= num
;
1143 num
= e
->lno
+ e
->num_lines
;
1144 if (longest_dst_lines
< num
)
1145 longest_dst_lines
= num
;
1147 max_orig_digits
= lineno_width(longest_src_lines
);
1148 max_digits
= lineno_width(longest_dst_lines
);
1151 static int has_path_in_work_tree(const char *path
)
1154 return !lstat(path
, &st
);
1157 int cmd_pickaxe(int argc
, const char **argv
, const char *prefix
)
1159 struct rev_info revs
;
1161 struct scoreboard sb
;
1163 struct blame_entry
*ent
;
1164 int i
, seen_dashdash
, unk
, opt
;
1165 long bottom
, top
, lno
;
1166 int output_option
= 0;
1167 const char *revs_file
= NULL
;
1168 const char *final_commit_name
= NULL
;
1174 for (unk
= i
= 1; i
< argc
; i
++) {
1175 const char *arg
= argv
[i
];
1178 else if (!strcmp("-c", arg
))
1179 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1180 else if (!strcmp("-t", arg
))
1181 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1182 else if (!strcmp("-l", arg
))
1183 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1184 else if (!strcmp("-S", arg
) && ++i
< argc
)
1185 revs_file
= argv
[i
];
1186 else if (!strcmp("-M", arg
))
1187 opt
|= PICKAXE_BLAME_MOVE
;
1188 else if (!strcmp("-C", arg
)) {
1189 if (opt
& PICKAXE_BLAME_COPY
)
1190 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1191 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1193 else if (!strcmp("-L", arg
) && ++i
< argc
) {
1197 die("More than one '-L n,m' option given");
1198 bottom
= strtol(arg
, &term
, 10);
1200 top
= strtol(term
+ 1, &term
, 10);
1202 usage(pickaxe_usage
);
1204 if (bottom
&& top
&& top
< bottom
) {
1206 tmp
= top
; top
= bottom
; bottom
= tmp
;
1209 else if (!strcmp("-f", arg
) ||
1210 !strcmp("--show-name", arg
))
1211 output_option
|= OUTPUT_SHOW_NAME
;
1212 else if (!strcmp("-n", arg
) ||
1213 !strcmp("--show-number", arg
))
1214 output_option
|= OUTPUT_SHOW_NUMBER
;
1215 else if (!strcmp("-p", arg
) ||
1216 !strcmp("--porcelain", arg
))
1217 output_option
|= OUTPUT_PORCELAIN
;
1218 else if (!strcmp("--", arg
)) {
1227 /* We have collected options unknown to us in argv[1..unk]
1228 * which are to be passed to revision machinery if we are
1229 * going to do the "bottom" procesing.
1231 * The remaining are:
1233 * (1) if seen_dashdash, its either
1234 * "-options -- <path>" or
1235 * "-options -- <path> <rev>".
1236 * but the latter is allowed only if there is no
1237 * options that we passed to revision machinery.
1239 * (2) otherwise, we may have "--" somewhere later and
1240 * might be looking at the first one of multiple 'rev'
1241 * parameters (e.g. " master ^next ^maint -- path").
1242 * See if there is a dashdash first, and give the
1243 * arguments before that to revision machinery.
1244 * After that there must be one 'path'.
1246 * (3) otherwise, its one of the three:
1247 * "-options <path> <rev>"
1248 * "-options <rev> <path>"
1250 * but again the first one is allowed only if
1251 * there is no options that we passed to revision
1255 if (seen_dashdash
) {
1258 usage(pickaxe_usage
);
1260 if (i
+ 1 == argc
- 1) {
1262 usage(pickaxe_usage
);
1263 argv
[unk
++] = argv
[i
+ 1];
1265 else if (i
+ 1 != argc
)
1266 /* garbage at end */
1267 usage(pickaxe_usage
);
1271 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1272 if (!strcmp(argv
[j
], "--"))
1274 if (seen_dashdash
) {
1275 if (seen_dashdash
+ 1 != argc
- 1)
1276 usage(pickaxe_usage
);
1277 path
= argv
[seen_dashdash
+ 1];
1278 for (j
= i
; j
< seen_dashdash
; j
++)
1279 argv
[unk
++] = argv
[j
];
1284 if (i
+ 1 == argc
- 1) {
1285 final_commit_name
= argv
[i
+ 1];
1287 /* if (unk == 1) we could be getting
1290 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1292 final_commit_name
= argv
[i
];
1295 else if (i
!= argc
- 1)
1296 usage(pickaxe_usage
); /* garbage at end */
1298 if (!has_path_in_work_tree(path
))
1299 die("cannot stat path %s: %s",
1300 path
, strerror(errno
));
1304 if (final_commit_name
)
1305 argv
[unk
++] = final_commit_name
;
1307 /* Now we got rev and path. We do not want the path pruning
1308 * but we may want "bottom" processing.
1312 init_revisions(&revs
, NULL
);
1313 setup_revisions(unk
, argv
, &revs
, "HEAD");
1314 memset(&sb
, 0, sizeof(sb
));
1316 /* There must be one and only one positive commit in the
1317 * revs->pending array.
1319 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1320 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1321 if (obj
->flags
& UNINTERESTING
)
1323 while (obj
->type
== OBJ_TAG
)
1324 obj
= deref_tag(obj
, NULL
, 0);
1325 if (obj
->type
!= OBJ_COMMIT
)
1326 die("Non commit %s?",
1327 revs
.pending
.objects
[i
].name
);
1329 die("More than one commit to dig from %s and %s?",
1330 revs
.pending
.objects
[i
].name
,
1332 sb
.final
= (struct commit
*) obj
;
1333 final_commit_name
= revs
.pending
.objects
[i
].name
;
1337 /* "--not A B -- path" without anything positive */
1338 unsigned char head_sha1
[20];
1340 final_commit_name
= "HEAD";
1341 if (get_sha1(final_commit_name
, head_sha1
))
1342 die("No such ref: HEAD");
1343 sb
.final
= lookup_commit_reference(head_sha1
);
1344 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1347 /* If we have bottom, this will mark the ancestors of the
1348 * bottom commits we would reach while traversing as
1351 prepare_revision_walk(&revs
);
1353 o
= find_origin(&sb
, sb
.final
, path
);
1355 die("no such path %s in %s", path
, final_commit_name
);
1357 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1358 lno
= prepare_lines(&sb
);
1366 die("file %s has only %lu lines", path
, lno
);
1368 ent
= xcalloc(1, sizeof(*ent
));
1370 ent
->num_lines
= top
- bottom
;
1372 ent
->s_lno
= bottom
;
1377 if (revs_file
&& read_ancestry(revs_file
))
1378 die("reading graft file %s failed: %s",
1379 revs_file
, strerror(errno
));
1381 assign_blame(&sb
, &revs
, opt
);
1385 if (!(output_option
& OUTPUT_PORCELAIN
))
1386 find_alignment(&sb
, &output_option
);
1388 output(&sb
, output_option
);
1389 free((void *)sb
.final_buf
);
1390 for (ent
= sb
.ent
; ent
; ) {
1391 struct blame_entry
*e
= ent
->next
;