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>] [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 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
32 static int longest_file
;
33 static int longest_author
;
34 static int max_orig_digits
;
35 static int max_digits
;
39 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
40 #define METAINFO_SHOWN (1u<<12)
41 #define MORE_THAN_ONE_PATH (1u<<13)
44 * One blob in a commit
47 struct commit
*commit
;
48 unsigned char blob_sha1
[20];
49 char path
[FLEX_ARRAY
];
53 struct blame_entry
*prev
;
54 struct blame_entry
*next
;
56 /* the first line of this group in the final image;
57 * internally all line numbers are 0 based.
61 /* how many lines this group has */
64 /* the commit that introduced this group into the final image */
65 struct origin
*suspect
;
67 /* true if the suspect is truly guilty; false while we have not
68 * checked if the group came from one of its parents.
72 /* the line number of the first line of this group in the
73 * suspect's file; internally all line numbers are 0 based.
79 /* the final commit (i.e. where we started digging from) */
84 /* the contents in the final; pointed into by buf pointers of
87 const char *final_buf
;
88 unsigned long final_buf_size
;
90 /* linked list of blames */
91 struct blame_entry
*ent
;
97 static void coalesce(struct scoreboard
*sb
)
99 struct blame_entry
*ent
, *next
;
101 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
102 if (ent
->suspect
== next
->suspect
&&
103 ent
->guilty
== next
->guilty
&&
104 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
105 ent
->num_lines
+= next
->num_lines
;
106 ent
->next
= next
->next
;
108 ent
->next
->prev
= ent
;
110 next
= ent
; /* again */
115 static void free_origin(struct origin
*o
)
120 static struct origin
*find_origin(struct scoreboard
*sb
,
121 struct commit
*commit
,
124 struct blame_entry
*ent
;
129 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
130 if (ent
->suspect
->commit
== commit
&&
131 !strcmp(ent
->suspect
->path
, path
))
135 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
137 strcpy(o
->path
, path
);
138 if (get_tree_entry(commit
->object
.sha1
, path
, o
->blob_sha1
, &mode
))
140 if (sha1_object_info(o
->blob_sha1
, type
, NULL
) ||
141 strcmp(type
, blob_type
))
149 static struct origin
*find_rename(struct scoreboard
*sb
,
150 struct commit
*parent
,
151 struct origin
*origin
)
153 struct origin
*porigin
= NULL
;
154 struct diff_options diff_opts
;
156 const char *paths
[1];
158 diff_setup(&diff_opts
);
159 diff_opts
.recursive
= 1;
160 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
161 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
163 diff_tree_setup_paths(paths
, &diff_opts
);
164 if (diff_setup_done(&diff_opts
) < 0)
166 diff_tree_sha1(origin
->commit
->tree
->object
.sha1
,
167 parent
->tree
->object
.sha1
,
169 diffcore_std(&diff_opts
);
171 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
172 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
173 if (p
->status
== 'R' && !strcmp(p
->one
->path
, origin
->path
)) {
174 porigin
= find_origin(sb
, parent
, p
->two
->path
);
178 diff_flush(&diff_opts
);
183 /* line number in postimage; up to but not including this
184 * line is the same as preimage
188 /* preimage line number after this chunk */
191 /* postimage line number after this chunk */
196 struct chunk
*chunks
;
200 struct blame_diff_state
{
201 struct xdiff_emit_state xm
;
203 unsigned hunk_post_context
;
204 unsigned hunk_in_pre_context
: 1;
207 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
209 struct blame_diff_state
*state
= state_
;
211 int off1
, off2
, len1
, len2
, num
;
214 fprintf(stderr
, "%.*s", (int) len
, line
);
216 num
= state
->ret
->num
;
217 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
218 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
219 state
->ret
->chunks
[num
- 1].same
++;
221 state
->hunk_in_pre_context
= 0;
223 state
->hunk_post_context
++;
225 state
->hunk_post_context
= 0;
230 if (num
&& state
->hunk_post_context
) {
231 chunk
= &state
->ret
->chunks
[num
- 1];
232 chunk
->p_next
-= state
->hunk_post_context
;
233 chunk
->t_next
-= state
->hunk_post_context
;
235 state
->ret
->num
= ++num
;
236 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
237 sizeof(struct chunk
) * num
);
238 chunk
= &state
->ret
->chunks
[num
- 1];
239 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
244 /* Line numbers in patch output are one based. */
248 chunk
->same
= len2
? off2
: (off2
+ 1);
250 chunk
->p_next
= off1
+ (len1
? len1
: 1);
251 chunk
->t_next
= chunk
->same
+ len2
;
252 state
->hunk_in_pre_context
= 1;
253 state
->hunk_post_context
= 0;
256 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
259 struct blame_diff_state state
;
264 xpp
.flags
= XDF_NEED_MINIMAL
;
265 xecfg
.ctxlen
= context
;
267 ecb
.outf
= xdiff_outf
;
269 memset(&state
, 0, sizeof(state
));
270 state
.xm
.consume
= process_u_diff
;
271 state
.ret
= xmalloc(sizeof(struct patch
));
272 state
.ret
->chunks
= NULL
;
275 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
277 if (state
.ret
->num
) {
279 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
280 chunk
->p_next
-= state
.hunk_post_context
;
281 chunk
->t_next
-= state
.hunk_post_context
;
286 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
288 mmfile_t file_p
, file_o
;
290 char *blob_p
, *blob_o
;
293 if (DEBUG
) fprintf(stderr
, "get patch %.8s %.8s\n",
294 sha1_to_hex(parent
->commit
->object
.sha1
),
295 sha1_to_hex(origin
->commit
->object
.sha1
));
297 blob_p
= read_sha1_file(parent
->blob_sha1
, type
,
298 (unsigned long *) &file_p
.size
);
299 blob_o
= read_sha1_file(origin
->blob_sha1
, type
,
300 (unsigned long *) &file_o
.size
);
303 if (!file_p
.ptr
|| !file_o
.ptr
) {
309 patch
= compare_buffer(&file_p
, &file_o
, 0);
315 static void free_patch(struct patch
*p
)
321 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
323 struct blame_entry
*ent
, *prev
= NULL
;
325 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
328 /* prev, if not NULL, is the last one that is below e */
331 e
->next
= prev
->next
;
342 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
344 struct blame_entry
*p
, *n
;
347 memcpy(dst
, src
, sizeof(*src
));
352 static const char *nth_line(struct scoreboard
*sb
, int lno
)
354 return sb
->final_buf
+ sb
->lineno
[lno
];
357 static void split_overlap(struct blame_entry split
[3],
358 struct blame_entry
*e
,
359 int tlno
, int plno
, int same
,
360 struct origin
*parent
)
362 /* it is known that lines between tlno to same came from
363 * parent, and e has an overlap with that range. it also is
364 * known that parent's line plno corresponds to e's line tlno.
370 * <------------------>
372 * Potentially we need to split e into three parts; before
373 * this chunk, the chunk to be blamed for parent, and after
377 memset(split
, 0, sizeof(struct blame_entry
[3]));
379 if (e
->s_lno
< tlno
) {
380 /* there is a pre-chunk part not blamed on parent */
381 split
[0].suspect
= e
->suspect
;
382 split
[0].lno
= e
->lno
;
383 split
[0].s_lno
= e
->s_lno
;
384 split
[0].num_lines
= tlno
- e
->s_lno
;
385 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
386 split
[1].s_lno
= plno
;
389 split
[1].lno
= e
->lno
;
390 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
393 if (same
< e
->s_lno
+ e
->num_lines
) {
394 /* there is a post-chunk part not blamed on parent */
395 split
[2].suspect
= e
->suspect
;
396 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
397 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
398 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
399 chunk_end_lno
= split
[2].lno
;
402 chunk_end_lno
= e
->lno
+ e
->num_lines
;
403 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
405 if (split
[1].num_lines
< 1)
407 split
[1].suspect
= parent
;
410 static void split_blame(struct scoreboard
*sb
,
411 struct blame_entry split
[3],
412 struct blame_entry
*e
)
414 struct blame_entry
*new_entry
;
416 if (split
[0].suspect
&& split
[2].suspect
) {
417 /* we need to split e into two and add another for parent */
418 dup_entry(e
, &split
[0]);
420 new_entry
= xmalloc(sizeof(*new_entry
));
421 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
422 add_blame_entry(sb
, new_entry
);
424 new_entry
= xmalloc(sizeof(*new_entry
));
425 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
426 add_blame_entry(sb
, new_entry
);
428 else if (!split
[0].suspect
&& !split
[2].suspect
)
429 /* parent covers the entire area */
430 dup_entry(e
, &split
[1]);
431 else if (split
[0].suspect
) {
432 dup_entry(e
, &split
[0]);
434 new_entry
= xmalloc(sizeof(*new_entry
));
435 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
436 add_blame_entry(sb
, new_entry
);
439 dup_entry(e
, &split
[1]);
441 new_entry
= xmalloc(sizeof(*new_entry
));
442 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
443 add_blame_entry(sb
, new_entry
);
447 struct blame_entry
*ent
;
448 int lno
= 0, corrupt
= 0;
450 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
455 lno
+= ent
->num_lines
;
459 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
460 printf("L %8d l %8d n %8d\n",
461 lno
, ent
->lno
, ent
->num_lines
);
462 lno
= ent
->lno
+ ent
->num_lines
;
469 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
470 int tlno
, int plno
, int same
,
471 struct origin
*parent
)
473 struct blame_entry split
[3];
475 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
476 if (!split
[1].suspect
)
478 split_blame(sb
, split
, e
);
481 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
483 struct blame_entry
*e
;
484 int last_in_target
= -1;
486 for (e
= sb
->ent
; e
; e
= e
->next
) {
487 if (e
->guilty
|| e
->suspect
!= target
)
489 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
490 last_in_target
= e
->s_lno
+ e
->num_lines
;
492 return last_in_target
;
495 static void blame_chunk(struct scoreboard
*sb
,
496 int tlno
, int plno
, int same
,
497 struct origin
*target
, struct origin
*parent
)
499 struct blame_entry
*e
, *n
;
501 for (e
= sb
->ent
; e
; e
= n
) {
503 if (e
->guilty
|| e
->suspect
!= target
)
505 if (same
<= e
->s_lno
)
507 if (tlno
< e
->s_lno
+ e
->num_lines
)
508 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
512 static int pass_blame_to_parent(struct scoreboard
*sb
,
513 struct origin
*target
,
514 struct origin
*parent
)
516 int i
, last_in_target
, plno
, tlno
;
519 last_in_target
= find_last_in_target(sb
, target
);
520 if (last_in_target
< 0)
521 return 1; /* nothing remains for this target */
523 patch
= get_patch(parent
, target
);
525 for (i
= 0; i
< patch
->num
; i
++) {
526 struct chunk
*chunk
= &patch
->chunks
[i
];
530 "plno = %d, tlno = %d, "
531 "same as parent up to %d, resync %d and %d\n",
533 chunk
->same
, chunk
->p_next
, chunk
->t_next
);
534 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
535 plno
= chunk
->p_next
;
536 tlno
= chunk
->t_next
;
538 /* rest (i.e. anything above tlno) are the same as parent */
539 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
547 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
)
550 struct commit
*commit
= origin
->commit
;
551 struct commit_list
*parent
;
552 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
554 memset(parent_origin
, 0, sizeof(parent_origin
));
555 for (i
= 0, parent
= commit
->parents
;
556 i
< MAXPARENT
&& parent
;
557 parent
= parent
->next
, i
++) {
558 struct commit
*p
= parent
->item
;
562 porigin
= find_origin(sb
, parent
->item
, origin
->path
);
564 porigin
= find_rename(sb
, parent
->item
, origin
);
567 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
568 struct blame_entry
*e
;
569 for (e
= sb
->ent
; e
; e
= e
->next
)
570 if (e
->suspect
== origin
)
571 e
->suspect
= porigin
;
572 /* now everything blamed for origin is blamed for
573 * porigin, we do not need to keep it anymore.
574 * Do not free porigin (or the ones we got from
575 * earlier round); they may still be used elsewhere.
580 parent_origin
[i
] = porigin
;
583 for (i
= 0, parent
= commit
->parents
;
584 i
< MAXPARENT
&& parent
;
585 parent
= parent
->next
, i
++) {
586 struct origin
*porigin
= parent_origin
[i
];
589 if (pass_blame_to_parent(sb
, origin
, porigin
))
594 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
)
597 struct blame_entry
*ent
;
598 struct commit
*commit
;
599 struct origin
*suspect
= NULL
;
601 /* find one suspect to break down */
602 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
604 suspect
= ent
->suspect
;
606 return; /* all done */
608 commit
= suspect
->commit
;
609 parse_commit(commit
);
610 if (!(commit
->object
.flags
& UNINTERESTING
) &&
611 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
612 pass_blame(sb
, suspect
);
614 /* Take responsibility for the remaining entries */
615 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
616 if (ent
->suspect
== suspect
)
621 static const char *format_time(unsigned long time
, const char *tz_str
,
624 static char time_buf
[128];
630 sprintf(time_buf
, "%lu %s", time
, tz_str
);
635 minutes
= tz
< 0 ? -tz
: tz
;
636 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
637 minutes
= tz
< 0 ? -minutes
: minutes
;
638 t
= time
+ minutes
* 60;
641 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
642 strcat(time_buf
, tz_str
);
650 unsigned long author_time
;
653 /* filled only when asked for details */
655 char *committer_mail
;
656 unsigned long committer_time
;
662 static void get_ac_line(const char *inbuf
, const char *what
,
663 int bufsz
, char *person
, char **mail
,
664 unsigned long *time
, char **tz
)
669 tmp
= strstr(inbuf
, what
);
673 endp
= strchr(tmp
, '\n');
681 person
= *mail
= *tz
= "(unknown)";
685 memcpy(person
, tmp
, len
);
697 *time
= strtoul(tmp
, NULL
, 10);
706 static void get_commit_info(struct commit
*commit
,
707 struct commit_info
*ret
,
712 static char author_buf
[1024];
713 static char committer_buf
[1024];
714 static char summary_buf
[1024];
716 ret
->author
= author_buf
;
717 get_ac_line(commit
->buffer
, "\nauthor ",
718 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
719 &ret
->author_time
, &ret
->author_tz
);
724 ret
->committer
= committer_buf
;
725 get_ac_line(commit
->buffer
, "\ncommitter ",
726 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
727 &ret
->committer_time
, &ret
->committer_tz
);
729 ret
->summary
= summary_buf
;
730 tmp
= strstr(commit
->buffer
, "\n\n");
733 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
737 endp
= strchr(tmp
, '\n');
741 if (len
>= sizeof(summary_buf
))
743 memcpy(summary_buf
, tmp
, len
);
744 summary_buf
[len
] = 0;
747 #define OUTPUT_ANNOTATE_COMPAT 001
748 #define OUTPUT_LONG_OBJECT_NAME 002
749 #define OUTPUT_RAW_TIMESTAMP 004
750 #define OUTPUT_PORCELAIN 010
751 #define OUTPUT_SHOW_NAME 020
752 #define OUTPUT_SHOW_NUMBER 040
754 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
758 struct origin
*suspect
= ent
->suspect
;
761 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
762 printf("%s%c%d %d %d\n",
764 ent
->guilty
? ' ' : '*', // purely for debugging
768 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
769 struct commit_info ci
;
770 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
771 get_commit_info(suspect
->commit
, &ci
, 1);
772 printf("author %s\n", ci
.author
);
773 printf("author-mail %s\n", ci
.author_mail
);
774 printf("author-time %lu\n", ci
.author_time
);
775 printf("author-tz %s\n", ci
.author_tz
);
776 printf("committer %s\n", ci
.committer
);
777 printf("committer-mail %s\n", ci
.committer_mail
);
778 printf("committer-time %lu\n", ci
.committer_time
);
779 printf("committer-tz %s\n", ci
.committer_tz
);
780 printf("filename %s\n", suspect
->path
);
781 printf("summary %s\n", ci
.summary
);
783 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
784 printf("filename %s\n", suspect
->path
);
786 cp
= nth_line(sb
, ent
->lno
);
787 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
790 printf("%s %d %d\n", hex
,
791 ent
->s_lno
+ 1 + cnt
,
797 } while (ch
!= '\n' &&
798 cp
< sb
->final_buf
+ sb
->final_buf_size
);
802 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
806 struct origin
*suspect
= ent
->suspect
;
807 struct commit_info ci
;
809 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
811 get_commit_info(suspect
->commit
, &ci
, 1);
812 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
814 cp
= nth_line(sb
, ent
->lno
);
815 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
818 printf("%.*s", (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8, hex
);
819 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
820 printf("\t(%10s\t%10s\t%d)", ci
.author
,
821 format_time(ci
.author_time
, ci
.author_tz
,
825 if (opt
& OUTPUT_SHOW_NAME
)
826 printf(" %-*.*s", longest_file
, longest_file
,
828 if (opt
& OUTPUT_SHOW_NUMBER
)
829 printf(" %*d", max_orig_digits
,
830 ent
->s_lno
+ 1 + cnt
);
831 printf(" (%-*.*s %10s %*d) ",
832 longest_author
, longest_author
, ci
.author
,
833 format_time(ci
.author_time
, ci
.author_tz
,
835 max_digits
, ent
->lno
+ 1 + cnt
);
840 } while (ch
!= '\n' &&
841 cp
< sb
->final_buf
+ sb
->final_buf_size
);
845 static void output(struct scoreboard
*sb
, int option
)
847 struct blame_entry
*ent
;
849 if (option
& OUTPUT_PORCELAIN
) {
850 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
851 struct blame_entry
*oth
;
852 struct origin
*suspect
= ent
->suspect
;
853 struct commit
*commit
= suspect
->commit
;
854 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
856 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
857 if ((oth
->suspect
->commit
!= commit
) ||
858 !strcmp(oth
->suspect
->path
, suspect
->path
))
860 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
866 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
867 if (option
& OUTPUT_PORCELAIN
)
868 emit_porcelain(sb
, ent
);
870 emit_other(sb
, ent
, option
);
874 static int prepare_lines(struct scoreboard
*sb
)
876 const char *buf
= sb
->final_buf
;
877 unsigned long len
= sb
->final_buf_size
;
878 int num
= 0, incomplete
= 0, bol
= 1;
880 if (len
&& buf
[len
-1] != '\n')
881 incomplete
++; /* incomplete line at the end */
884 sb
->lineno
= xrealloc(sb
->lineno
,
885 sizeof(int* ) * (num
+ 1));
886 sb
->lineno
[num
] = buf
- sb
->final_buf
;
889 if (*buf
++ == '\n') {
894 sb
->num_lines
= num
+ incomplete
;
895 return sb
->num_lines
;
898 static int read_ancestry(const char *graft_file
)
900 FILE *fp
= fopen(graft_file
, "r");
904 while (fgets(buf
, sizeof(buf
), fp
)) {
905 /* The format is just "Commit Parent1 Parent2 ...\n" */
906 int len
= strlen(buf
);
907 struct commit_graft
*graft
= read_graft_line(buf
, len
);
908 register_commit_graft(graft
, 0);
914 static int lineno_width(int lines
)
918 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
923 static void find_alignment(struct scoreboard
*sb
, int *option
)
925 int longest_src_lines
= 0;
926 int longest_dst_lines
= 0;
927 struct blame_entry
*e
;
929 for (e
= sb
->ent
; e
; e
= e
->next
) {
930 struct origin
*suspect
= e
->suspect
;
931 struct commit_info ci
;
934 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
935 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
936 get_commit_info(suspect
->commit
, &ci
, 1);
937 if (strcmp(suspect
->path
, sb
->path
))
938 *option
|= OUTPUT_SHOW_NAME
;
939 num
= strlen(suspect
->path
);
940 if (longest_file
< num
)
942 num
= strlen(ci
.author
);
943 if (longest_author
< num
)
944 longest_author
= num
;
946 num
= e
->s_lno
+ e
->num_lines
;
947 if (longest_src_lines
< num
)
948 longest_src_lines
= num
;
949 num
= e
->lno
+ e
->num_lines
;
950 if (longest_dst_lines
< num
)
951 longest_dst_lines
= num
;
953 max_orig_digits
= lineno_width(longest_src_lines
);
954 max_digits
= lineno_width(longest_dst_lines
);
957 static int has_path_in_work_tree(const char *path
)
960 return !lstat(path
, &st
);
963 int cmd_pickaxe(int argc
, const char **argv
, const char *prefix
)
965 struct rev_info revs
;
967 struct scoreboard sb
;
969 struct blame_entry
*ent
;
970 int i
, seen_dashdash
, unk
;
971 long bottom
, top
, lno
;
972 int output_option
= 0;
973 const char *revs_file
= NULL
;
974 const char *final_commit_name
= NULL
;
979 for (unk
= i
= 1; i
< argc
; i
++) {
980 const char *arg
= argv
[i
];
983 else if (!strcmp("-c", arg
))
984 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
985 else if (!strcmp("-t", arg
))
986 output_option
|= OUTPUT_RAW_TIMESTAMP
;
987 else if (!strcmp("-l", arg
))
988 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
989 else if (!strcmp("-S", arg
) && ++i
< argc
)
991 else if (!strcmp("-L", arg
) && ++i
< argc
) {
995 die("More than one '-L n,m' option given");
996 bottom
= strtol(arg
, &term
, 10);
998 top
= strtol(term
+ 1, &term
, 10);
1000 usage(pickaxe_usage
);
1002 if (bottom
&& top
&& top
< bottom
) {
1004 tmp
= top
; top
= bottom
; bottom
= tmp
;
1007 else if (!strcmp("-f", arg
) ||
1008 !strcmp("--show-name", arg
))
1009 output_option
|= OUTPUT_SHOW_NAME
;
1010 else if (!strcmp("-n", arg
) ||
1011 !strcmp("--show-number", arg
))
1012 output_option
|= OUTPUT_SHOW_NUMBER
;
1013 else if (!strcmp("-p", arg
) ||
1014 !strcmp("--porcelain", arg
))
1015 output_option
|= OUTPUT_PORCELAIN
;
1016 else if (!strcmp("--", arg
)) {
1025 /* We have collected options unknown to us in argv[1..unk]
1026 * which are to be passed to revision machinery if we are
1027 * going to do the "bottom" procesing.
1029 * The remaining are:
1031 * (1) if seen_dashdash, its either
1032 * "-options -- <path>" or
1033 * "-options -- <path> <rev>".
1034 * but the latter is allowed only if there is no
1035 * options that we passed to revision machinery.
1037 * (2) otherwise, we may have "--" somewhere later and
1038 * might be looking at the first one of multiple 'rev'
1039 * parameters (e.g. " master ^next ^maint -- path").
1040 * See if there is a dashdash first, and give the
1041 * arguments before that to revision machinery.
1042 * After that there must be one 'path'.
1044 * (3) otherwise, its one of the three:
1045 * "-options <path> <rev>"
1046 * "-options <rev> <path>"
1048 * but again the first one is allowed only if
1049 * there is no options that we passed to revision
1053 if (seen_dashdash
) {
1056 usage(pickaxe_usage
);
1058 if (i
+ 1 == argc
- 1) {
1060 usage(pickaxe_usage
);
1061 argv
[unk
++] = argv
[i
+ 1];
1063 else if (i
+ 1 != argc
)
1064 /* garbage at end */
1065 usage(pickaxe_usage
);
1069 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
1070 if (!strcmp(argv
[j
], "--"))
1072 if (seen_dashdash
) {
1073 if (seen_dashdash
+ 1 != argc
- 1)
1074 usage(pickaxe_usage
);
1075 path
= argv
[seen_dashdash
+ 1];
1076 for (j
= i
; j
< seen_dashdash
; j
++)
1077 argv
[unk
++] = argv
[j
];
1082 if (i
+ 1 == argc
- 1) {
1083 final_commit_name
= argv
[i
+ 1];
1085 /* if (unk == 1) we could be getting
1088 if (unk
== 1 && !has_path_in_work_tree(path
)) {
1090 final_commit_name
= argv
[i
];
1093 else if (i
!= argc
- 1)
1094 usage(pickaxe_usage
); /* garbage at end */
1096 if (!has_path_in_work_tree(path
))
1097 die("cannot stat path %s: %s",
1098 path
, strerror(errno
));
1102 if (final_commit_name
)
1103 argv
[unk
++] = final_commit_name
;
1105 /* Now we got rev and path. We do not want the path pruning
1106 * but we may want "bottom" processing.
1110 init_revisions(&revs
, NULL
);
1111 setup_revisions(unk
, argv
, &revs
, "HEAD");
1112 memset(&sb
, 0, sizeof(sb
));
1114 /* There must be one and only one positive commit in the
1115 * revs->pending array.
1117 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
1118 struct object
*obj
= revs
.pending
.objects
[i
].item
;
1119 if (obj
->flags
& UNINTERESTING
)
1121 while (obj
->type
== OBJ_TAG
)
1122 obj
= deref_tag(obj
, NULL
, 0);
1123 if (obj
->type
!= OBJ_COMMIT
)
1124 die("Non commit %s?",
1125 revs
.pending
.objects
[i
].name
);
1127 die("More than one commit to dig from %s and %s?",
1128 revs
.pending
.objects
[i
].name
,
1130 sb
.final
= (struct commit
*) obj
;
1131 final_commit_name
= revs
.pending
.objects
[i
].name
;
1135 /* "--not A B -- path" without anything positive */
1136 unsigned char head_sha1
[20];
1138 final_commit_name
= "HEAD";
1139 if (get_sha1(final_commit_name
, head_sha1
))
1140 die("No such ref: HEAD");
1141 sb
.final
= lookup_commit_reference(head_sha1
);
1142 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
1145 /* If we have bottom, this will mark the ancestors of the
1146 * bottom commits we would reach while traversing as
1149 prepare_revision_walk(&revs
);
1151 o
= find_origin(&sb
, sb
.final
, path
);
1153 die("no such path %s in %s", path
, final_commit_name
);
1155 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
1156 lno
= prepare_lines(&sb
);
1164 die("file %s has only %lu lines", path
, lno
);
1166 ent
= xcalloc(1, sizeof(*ent
));
1168 ent
->num_lines
= top
- bottom
;
1170 ent
->s_lno
= bottom
;
1175 if (revs_file
&& read_ancestry(revs_file
))
1176 die("reading graft file %s failed: %s",
1177 revs_file
, strerror(errno
));
1179 assign_blame(&sb
, &revs
);
1183 if (!(output_option
& OUTPUT_PORCELAIN
))
1184 find_alignment(&sb
, &output_option
);
1186 output(&sb
, output_option
);
1187 free((void *)sb
.final_buf
);
1188 for (ent
= sb
.ent
; ent
; ) {
1189 struct blame_entry
*e
= ent
->next
;