git-pickaxe: blame rewritten.
[git.git] / builtin-pickaxe.c
blobcb69fcc16bc5584f1983fa4933e99721ef9f8dfc
1 /*
2 * Pickaxe
4 * Copyright (c) 2006, Junio C Hamano
5 */
7 #include "cache.h"
8 #include "builtin.h"
9 #include "blob.h"
10 #include "commit.h"
11 #include "tag.h"
12 #include "tree-walk.h"
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "revision.h"
16 #include "xdiff-interface.h"
18 #include <time.h>
19 #include <sys/time.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;
37 #define DEBUG 0
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
46 struct origin {
47 struct commit *commit;
48 unsigned char blob_sha1[20];
49 char path[FLEX_ARRAY];
52 struct blame_entry {
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.
59 int lno;
61 /* how many lines this group has */
62 int num_lines;
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.
70 char guilty;
72 /* the line number of the first line of this group in the
73 * suspect's file; internally all line numbers are 0 based.
75 int s_lno;
78 struct scoreboard {
79 /* the final commit (i.e. where we started digging from) */
80 struct commit *final;
82 const char *path;
84 /* the contents in the final; pointed into by buf pointers of
85 * blame_entries
87 const char *final_buf;
88 unsigned long final_buf_size;
90 /* linked list of blames */
91 struct blame_entry *ent;
93 int num_lines;
94 int *lineno;
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;
107 if (ent->next)
108 ent->next->prev = ent;
109 free(next);
110 next = ent; /* again */
115 static void free_origin(struct origin *o)
117 free(o);
120 static struct origin *find_origin(struct scoreboard *sb,
121 struct commit *commit,
122 const char *path)
124 struct blame_entry *ent;
125 struct origin *o;
126 unsigned mode;
127 char type[10];
129 for (ent = sb->ent; ent; ent = ent->next) {
130 if (ent->suspect->commit == commit &&
131 !strcmp(ent->suspect->path, path))
132 return ent->suspect;
135 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
136 o->commit = commit;
137 strcpy(o->path, path);
138 if (get_tree_entry(commit->object.sha1, path, o->blob_sha1, &mode))
139 goto err_out;
140 if (sha1_object_info(o->blob_sha1, type, NULL) ||
141 strcmp(type, blob_type))
142 goto err_out;
143 return o;
144 err_out:
145 free_origin(o);
146 return NULL;
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;
155 int i;
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;
162 paths[0] = NULL;
163 diff_tree_setup_paths(paths, &diff_opts);
164 if (diff_setup_done(&diff_opts) < 0)
165 die("diff-setup");
166 diff_tree_sha1(origin->commit->tree->object.sha1,
167 parent->tree->object.sha1,
168 "", &diff_opts);
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);
175 break;
178 diff_flush(&diff_opts);
179 return porigin;
182 struct chunk {
183 /* line number in postimage; up to but not including this
184 * line is the same as preimage
186 int same;
188 /* preimage line number after this chunk */
189 int p_next;
191 /* postimage line number after this chunk */
192 int t_next;
195 struct patch {
196 struct chunk *chunks;
197 int num;
200 struct blame_diff_state {
201 struct xdiff_emit_state xm;
202 struct patch *ret;
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_;
210 struct chunk *chunk;
211 int off1, off2, len1, len2, num;
213 if (DEBUG)
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++;
220 else {
221 state->hunk_in_pre_context = 0;
222 if (line[0] == ' ')
223 state->hunk_post_context++;
224 else
225 state->hunk_post_context = 0;
227 return;
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)) {
240 state->ret->num--;
241 return;
244 /* Line numbers in patch output are one based. */
245 off1--;
246 off2--;
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,
257 int context)
259 struct blame_diff_state state;
260 xpparam_t xpp;
261 xdemitconf_t xecfg;
262 xdemitcb_t ecb;
264 xpp.flags = XDF_NEED_MINIMAL;
265 xecfg.ctxlen = context;
266 xecfg.flags = 0;
267 ecb.outf = xdiff_outf;
268 ecb.priv = &state;
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;
273 state.ret->num = 0;
275 xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
277 if (state.ret->num) {
278 struct chunk *chunk;
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;
283 return state.ret;
286 static struct patch *get_patch(struct origin *parent, struct origin *origin)
288 mmfile_t file_p, file_o;
289 char type[10];
290 char *blob_p, *blob_o;
291 struct patch *patch;
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);
301 file_p.ptr = blob_p;
302 file_o.ptr = blob_o;
303 if (!file_p.ptr || !file_o.ptr) {
304 free(blob_p);
305 free(blob_o);
306 return NULL;
309 patch = compare_buffer(&file_p, &file_o, 0);
310 free(blob_p);
311 free(blob_o);
312 return patch;
315 static void free_patch(struct patch *p)
317 free(p->chunks);
318 free(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)
326 prev = ent;
328 /* prev, if not NULL, is the last one that is below e */
329 e->prev = prev;
330 if (prev) {
331 e->next = prev->next;
332 prev->next = e;
334 else {
335 e->next = sb->ent;
336 sb->ent = e;
338 if (e->next)
339 e->next->prev = e;
342 static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
344 struct blame_entry *p, *n;
345 p = dst->prev;
346 n = dst->next;
347 memcpy(dst, src, sizeof(*src));
348 dst->prev = p;
349 dst->next = n;
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.
366 * <---- e ----->
367 * <------>
368 * <------------>
369 * <------------>
370 * <------------------>
372 * Potentially we need to split e into three parts; before
373 * this chunk, the chunk to be blamed for parent, and after
374 * that portion.
376 int chunk_end_lno;
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;
388 else {
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;
401 else
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)
406 return;
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);
438 else {
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);
446 if (DEBUG) {
447 struct blame_entry *ent;
448 int lno = 0, corrupt = 0;
450 for (ent = sb->ent; ent; ent = ent->next) {
451 if (lno != ent->lno)
452 corrupt = 1;
453 if (ent->s_lno < 0)
454 corrupt = 1;
455 lno += ent->num_lines;
457 if (corrupt) {
458 lno = 0;
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;
464 die("oops");
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)
477 return;
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)
488 continue;
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) {
502 n = e->next;
503 if (e->guilty || e->suspect != target)
504 continue;
505 if (same <= e->s_lno)
506 continue;
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;
517 struct patch *patch;
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);
524 plno = tlno = 0;
525 for (i = 0; i < patch->num; i++) {
526 struct chunk *chunk = &patch->chunks[i];
528 if (DEBUG)
529 fprintf(stderr,
530 "plno = %d, tlno = %d, "
531 "same as parent up to %d, resync %d and %d\n",
532 plno, tlno,
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);
541 free_patch(patch);
542 return 0;
545 #define MAXPARENT 16
547 static void pass_blame(struct scoreboard *sb, struct origin *origin)
549 int i;
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;
560 if (parse_commit(p))
561 continue;
562 porigin = find_origin(sb, parent->item, origin->path);
563 if (!porigin)
564 porigin = find_rename(sb, parent->item, origin);
565 if (!porigin)
566 continue;
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.
577 free_origin(origin);
578 return;
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];
587 if (!porigin)
588 continue;
589 if (pass_blame_to_parent(sb, origin, porigin))
590 return;
594 static void assign_blame(struct scoreboard *sb, struct rev_info *revs)
596 while (1) {
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)
603 if (!ent->guilty)
604 suspect = ent->suspect;
605 if (!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)
617 ent->guilty = 1;
621 static const char *format_time(unsigned long time, const char *tz_str,
622 int show_raw_time)
624 static char time_buf[128];
625 time_t t = time;
626 int minutes, tz;
627 struct tm *tm;
629 if (show_raw_time) {
630 sprintf(time_buf, "%lu %s", time, tz_str);
631 return time_buf;
634 tz = atoi(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;
639 tm = gmtime(&t);
641 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
642 strcat(time_buf, tz_str);
643 return time_buf;
646 struct commit_info
648 char *author;
649 char *author_mail;
650 unsigned long author_time;
651 char *author_tz;
653 /* filled only when asked for details */
654 char *committer;
655 char *committer_mail;
656 unsigned long committer_time;
657 char *committer_tz;
659 char *summary;
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)
666 int len;
667 char *tmp, *endp;
669 tmp = strstr(inbuf, what);
670 if (!tmp)
671 goto error_out;
672 tmp += strlen(what);
673 endp = strchr(tmp, '\n');
674 if (!endp)
675 len = strlen(tmp);
676 else
677 len = endp - tmp;
678 if (bufsz <= len) {
679 error_out:
680 /* Ugh */
681 person = *mail = *tz = "(unknown)";
682 *time = 0;
683 return;
685 memcpy(person, tmp, len);
687 tmp = person;
688 tmp += len;
689 *tmp = 0;
690 while (*tmp != ' ')
691 tmp--;
692 *tz = tmp+1;
694 *tmp = 0;
695 while (*tmp != ' ')
696 tmp--;
697 *time = strtoul(tmp, NULL, 10);
699 *tmp = 0;
700 while (*tmp != ' ')
701 tmp--;
702 *mail = tmp + 1;
703 *tmp = 0;
706 static void get_commit_info(struct commit *commit,
707 struct commit_info *ret,
708 int detailed)
710 int len;
711 char *tmp, *endp;
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);
721 if (!detailed)
722 return;
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");
731 if (!tmp) {
732 error_out:
733 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
734 return;
736 tmp += 2;
737 endp = strchr(tmp, '\n');
738 if (!endp)
739 goto error_out;
740 len = endp - tmp;
741 if (len >= sizeof(summary_buf))
742 goto error_out;
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)
756 int cnt;
757 const char *cp;
758 struct origin *suspect = ent->suspect;
759 char hex[41];
761 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
762 printf("%s%c%d %d %d\n",
763 hex,
764 ent->guilty ? ' ' : '*', // purely for debugging
765 ent->s_lno + 1,
766 ent->lno + 1,
767 ent->num_lines);
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++) {
788 char ch;
789 if (cnt)
790 printf("%s %d %d\n", hex,
791 ent->s_lno + 1 + cnt,
792 ent->lno + 1 + cnt);
793 putchar('\t');
794 do {
795 ch = *cp++;
796 putchar(ch);
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)
804 int cnt;
805 const char *cp;
806 struct origin *suspect = ent->suspect;
807 struct commit_info ci;
808 char hex[41];
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++) {
816 char ch;
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,
822 show_raw_time),
823 ent->lno + 1 + cnt);
824 else {
825 if (opt & OUTPUT_SHOW_NAME)
826 printf(" %-*.*s", longest_file, longest_file,
827 suspect->path);
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,
834 show_raw_time),
835 max_digits, ent->lno + 1 + cnt);
837 do {
838 ch = *cp++;
839 putchar(ch);
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)
855 continue;
856 for (oth = ent->next; oth; oth = oth->next) {
857 if ((oth->suspect->commit != commit) ||
858 !strcmp(oth->suspect->path, suspect->path))
859 continue;
860 commit->object.flags |= MORE_THAN_ONE_PATH;
861 break;
866 for (ent = sb->ent; ent; ent = ent->next) {
867 if (option & OUTPUT_PORCELAIN)
868 emit_porcelain(sb, ent);
869 else
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 */
882 while (len--) {
883 if (bol) {
884 sb->lineno = xrealloc(sb->lineno,
885 sizeof(int* ) * (num + 1));
886 sb->lineno[num] = buf - sb->final_buf;
887 bol = 0;
889 if (*buf++ == '\n') {
890 num++;
891 bol = 1;
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");
901 char buf[1024];
902 if (!fp)
903 return -1;
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);
910 fclose(fp);
911 return 0;
914 static int lineno_width(int lines)
916 int i, width;
918 for (width = 1, i = 10; i <= lines + 1; width++)
919 i *= 10;
920 return 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;
932 int num;
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)
941 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)
959 struct stat st;
960 return !lstat(path, &st);
963 int cmd_pickaxe(int argc, const char **argv, const char *prefix)
965 struct rev_info revs;
966 const char *path;
967 struct scoreboard sb;
968 struct origin *o;
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;
975 char type[10];
977 bottom = top = 0;
978 seen_dashdash = 0;
979 for (unk = i = 1; i < argc; i++) {
980 const char *arg = argv[i];
981 if (*arg != '-')
982 break;
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)
990 revs_file = argv[i];
991 else if (!strcmp("-L", arg) && ++i < argc) {
992 char *term;
993 arg = argv[i];
994 if (bottom || top)
995 die("More than one '-L n,m' option given");
996 bottom = strtol(arg, &term, 10);
997 if (*term == ',') {
998 top = strtol(term + 1, &term, 10);
999 if (*term)
1000 usage(pickaxe_usage);
1002 if (bottom && top && top < bottom) {
1003 unsigned long tmp;
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)) {
1017 seen_dashdash = 1;
1018 i++;
1019 break;
1021 else
1022 argv[unk++] = 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>"
1047 * "-options <path>"
1048 * but again the first one is allowed only if
1049 * there is no options that we passed to revision
1050 * machinery.
1053 if (seen_dashdash) {
1054 /* (1) */
1055 if (argc <= i)
1056 usage(pickaxe_usage);
1057 path = argv[i];
1058 if (i + 1 == argc - 1) {
1059 if (unk != 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);
1067 else {
1068 int j;
1069 for (j = i; !seen_dashdash && j < argc; j++)
1070 if (!strcmp(argv[j], "--"))
1071 seen_dashdash = 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];
1079 else {
1080 /* (3) */
1081 path = argv[i];
1082 if (i + 1 == argc - 1) {
1083 final_commit_name = argv[i + 1];
1085 /* if (unk == 1) we could be getting
1086 * old-style
1088 if (unk == 1 && !has_path_in_work_tree(path)) {
1089 path = argv[i + 1];
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.
1108 argv[unk] = NULL;
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)
1120 continue;
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);
1126 if (sb.final)
1127 die("More than one commit to dig from %s and %s?",
1128 revs.pending.objects[i].name,
1129 final_commit_name);
1130 sb.final = (struct commit *) obj;
1131 final_commit_name = revs.pending.objects[i].name;
1134 if (!sb.final) {
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
1147 * uninteresting.
1149 prepare_revision_walk(&revs);
1151 o = find_origin(&sb, sb.final, path);
1152 if (!o)
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);
1158 if (bottom < 1)
1159 bottom = 1;
1160 if (top < 1)
1161 top = lno;
1162 bottom--;
1163 if (lno < top)
1164 die("file %s has only %lu lines", path, lno);
1166 ent = xcalloc(1, sizeof(*ent));
1167 ent->lno = bottom;
1168 ent->num_lines = top - bottom;
1169 ent->suspect = o;
1170 ent->s_lno = bottom;
1172 sb.ent = ent;
1173 sb.path = path;
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);
1181 coalesce(&sb);
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;
1190 free(ent);
1191 ent = e;
1193 return 0;