git-pickaxe: pagenate output by default.
[git.git] / builtin-pickaxe.c
blob74c7c9a33b1a3fe6c2a49dd2ba426431451c9fed
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>] [-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;
38 #define DEBUG 0
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
51 struct origin {
52 struct commit *commit;
53 unsigned char blob_sha1[20];
54 char path[FLEX_ARRAY];
57 struct blame_entry {
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.
64 int lno;
66 /* how many lines this group has */
67 int num_lines;
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.
75 char guilty;
77 /* the line number of the first line of this group in the
78 * suspect's file; internally all line numbers are 0 based.
80 int s_lno;
83 struct scoreboard {
84 /* the final commit (i.e. where we started digging from) */
85 struct commit *final;
87 const char *path;
89 /* the contents in the final; pointed into by buf pointers of
90 * blame_entries
92 const char *final_buf;
93 unsigned long final_buf_size;
95 /* linked list of blames */
96 struct blame_entry *ent;
98 int num_lines;
99 int *lineno;
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;
112 if (ent->next)
113 ent->next->prev = ent;
114 free(next);
115 next = ent; /* again */
120 static void free_origin(struct origin *o)
122 free(o);
125 static struct origin *find_origin(struct scoreboard *sb,
126 struct commit *commit,
127 const char *path)
129 struct blame_entry *ent;
130 struct origin *o;
131 unsigned mode;
132 char type[10];
134 for (ent = sb->ent; ent; ent = ent->next) {
135 if (ent->suspect->commit == commit &&
136 !strcmp(ent->suspect->path, path))
137 return ent->suspect;
140 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
141 o->commit = commit;
142 strcpy(o->path, path);
143 if (get_tree_entry(commit->object.sha1, path, o->blob_sha1, &mode))
144 goto err_out;
145 if (sha1_object_info(o->blob_sha1, type, NULL) ||
146 strcmp(type, blob_type))
147 goto err_out;
148 return o;
149 err_out:
150 free_origin(o);
151 return NULL;
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;
160 int i;
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;
167 paths[0] = NULL;
168 diff_tree_setup_paths(paths, &diff_opts);
169 if (diff_setup_done(&diff_opts) < 0)
170 die("diff-setup");
171 diff_tree_sha1(origin->commit->tree->object.sha1,
172 parent->tree->object.sha1,
173 "", &diff_opts);
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);
180 break;
183 diff_flush(&diff_opts);
184 return porigin;
187 struct chunk {
188 /* line number in postimage; up to but not including this
189 * line is the same as preimage
191 int same;
193 /* preimage line number after this chunk */
194 int p_next;
196 /* postimage line number after this chunk */
197 int t_next;
200 struct patch {
201 struct chunk *chunks;
202 int num;
205 struct blame_diff_state {
206 struct xdiff_emit_state xm;
207 struct patch *ret;
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_;
215 struct chunk *chunk;
216 int off1, off2, len1, len2, num;
218 if (DEBUG)
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++;
225 else {
226 state->hunk_in_pre_context = 0;
227 if (line[0] == ' ')
228 state->hunk_post_context++;
229 else
230 state->hunk_post_context = 0;
232 return;
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)) {
245 state->ret->num--;
246 return;
249 /* Line numbers in patch output are one based. */
250 off1--;
251 off2--;
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,
262 int context)
264 struct blame_diff_state state;
265 xpparam_t xpp;
266 xdemitconf_t xecfg;
267 xdemitcb_t ecb;
269 xpp.flags = XDF_NEED_MINIMAL;
270 xecfg.ctxlen = context;
271 xecfg.flags = 0;
272 ecb.outf = xdiff_outf;
273 ecb.priv = &state;
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;
278 state.ret->num = 0;
280 xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
282 if (state.ret->num) {
283 struct chunk *chunk;
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;
288 return state.ret;
291 static struct patch *get_patch(struct origin *parent, struct origin *origin)
293 mmfile_t file_p, file_o;
294 char type[10];
295 char *blob_p, *blob_o;
296 struct patch *patch;
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);
306 file_p.ptr = blob_p;
307 file_o.ptr = blob_o;
308 if (!file_p.ptr || !file_o.ptr) {
309 free(blob_p);
310 free(blob_o);
311 return NULL;
314 patch = compare_buffer(&file_p, &file_o, 0);
315 free(blob_p);
316 free(blob_o);
317 return patch;
320 static void free_patch(struct patch *p)
322 free(p->chunks);
323 free(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)
331 prev = ent;
333 /* prev, if not NULL, is the last one that is below e */
334 e->prev = prev;
335 if (prev) {
336 e->next = prev->next;
337 prev->next = e;
339 else {
340 e->next = sb->ent;
341 sb->ent = e;
343 if (e->next)
344 e->next->prev = e;
347 static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
349 struct blame_entry *p, *n;
350 p = dst->prev;
351 n = dst->next;
352 memcpy(dst, src, sizeof(*src));
353 dst->prev = p;
354 dst->next = n;
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.
371 * <---- e ----->
372 * <------>
373 * <------------>
374 * <------------>
375 * <------------------>
377 * Potentially we need to split e into three parts; before
378 * this chunk, the chunk to be blamed for parent, and after
379 * that portion.
381 int chunk_end_lno;
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;
393 else {
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;
406 else
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)
411 return;
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);
443 else {
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);
451 if (DEBUG) {
452 struct blame_entry *ent;
453 int lno = 0, corrupt = 0;
455 for (ent = sb->ent; ent; ent = ent->next) {
456 if (lno != ent->lno)
457 corrupt = 1;
458 if (ent->s_lno < 0)
459 corrupt = 1;
460 lno += ent->num_lines;
462 if (corrupt) {
463 lno = 0;
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;
469 die("oops");
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)
482 return;
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)
493 continue;
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) {
507 n = e->next;
508 if (e->guilty || e->suspect != target)
509 continue;
510 if (same <= e->s_lno)
511 continue;
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;
522 struct patch *patch;
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);
529 plno = tlno = 0;
530 for (i = 0; i < patch->num; i++) {
531 struct chunk *chunk = &patch->chunks[i];
533 if (DEBUG)
534 fprintf(stderr,
535 "plno = %d, tlno = %d, "
536 "same as parent up to %d, resync %d and %d\n",
537 plno, tlno,
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);
546 free_patch(patch);
547 return 0;
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)
554 return;
555 if (best_so_far[1].suspect &&
556 (this[1].num_lines < best_so_far[1].num_lines))
557 return;
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],
565 mmfile_t *file_p)
567 const char *cp;
568 int cnt;
569 mmfile_t file_o;
570 struct patch *patch;
571 int i, plno, tlno;
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) {
578 if (*cp++ == '\n')
579 cnt--;
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]));
586 plno = tlno = 0;
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)
592 break;
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,
598 parent);
599 copy_split_if_better(split, this);
601 plno = chunk->p_next;
602 tlno = chunk->t_next;
604 free_patch(patch);
607 static int find_move_in_parent(struct scoreboard *sb,
608 struct origin *target,
609 struct origin *parent)
611 int last_in_target;
612 struct blame_entry *ent, split[3];
613 mmfile_t file_p;
614 char type[10];
615 char *blob_p;
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);
623 file_p.ptr = blob_p;
624 if (!file_p.ptr) {
625 free(blob_p);
626 return 0;
629 for (ent = sb->ent; ent; ent = ent->next) {
630 if (ent->suspect != target || ent->guilty)
631 continue;
632 find_copy_in_blob(sb, ent, parent, split, &file_p);
633 if (split[1].suspect)
634 split_blame(sb, split, ent);
636 free(blob_p);
637 return 0;
640 static int find_copy_in_parent(struct scoreboard *sb,
641 struct origin *target,
642 struct commit *parent,
643 struct origin *porigin,
644 int opt)
646 struct diff_options diff_opts;
647 const char *paths[1];
648 struct blame_entry *ent;
649 int i;
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;
664 paths[0] = NULL;
665 diff_tree_setup_paths(paths, &diff_opts);
666 if (diff_setup_done(&diff_opts) < 0)
667 die("diff-setup");
668 diff_tree_sha1(parent->tree->object.sha1,
669 target->commit->tree->object.sha1,
670 "", &diff_opts);
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)
676 continue;
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;
682 mmfile_t file_p;
683 char type[10];
684 char *blob;
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 */
691 continue;
692 norigin = find_origin(sb, parent, p->one->path);
694 blob = read_sha1_file(norigin->blob_sha1, type,
695 (unsigned long *) &file_p.size);
696 file_p.ptr = blob;
697 if (!file_p.ptr) {
698 free(blob);
699 continue;
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);
709 return 0;
712 #define MAXPARENT 16
714 static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
716 int i;
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;
727 if (parse_commit(p))
728 continue;
729 porigin = find_origin(sb, parent->item, origin->path);
730 if (!porigin)
731 porigin = find_rename(sb, parent->item, origin);
732 if (!porigin)
733 continue;
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.
744 free_origin(origin);
745 return;
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];
754 if (!porigin)
755 continue;
756 if (pass_blame_to_parent(sb, origin, porigin))
757 return;
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];
768 if (!porigin)
769 continue;
770 if (find_move_in_parent(sb, origin, porigin))
771 return;
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,
783 porigin, opt))
784 return;
788 static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
790 while (1) {
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)
797 if (!ent->guilty)
798 suspect = ent->suspect;
799 if (!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)
811 ent->guilty = 1;
815 static const char *format_time(unsigned long time, const char *tz_str,
816 int show_raw_time)
818 static char time_buf[128];
819 time_t t = time;
820 int minutes, tz;
821 struct tm *tm;
823 if (show_raw_time) {
824 sprintf(time_buf, "%lu %s", time, tz_str);
825 return time_buf;
828 tz = atoi(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;
833 tm = gmtime(&t);
835 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
836 strcat(time_buf, tz_str);
837 return time_buf;
840 struct commit_info
842 char *author;
843 char *author_mail;
844 unsigned long author_time;
845 char *author_tz;
847 /* filled only when asked for details */
848 char *committer;
849 char *committer_mail;
850 unsigned long committer_time;
851 char *committer_tz;
853 char *summary;
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)
860 int len;
861 char *tmp, *endp;
863 tmp = strstr(inbuf, what);
864 if (!tmp)
865 goto error_out;
866 tmp += strlen(what);
867 endp = strchr(tmp, '\n');
868 if (!endp)
869 len = strlen(tmp);
870 else
871 len = endp - tmp;
872 if (bufsz <= len) {
873 error_out:
874 /* Ugh */
875 person = *mail = *tz = "(unknown)";
876 *time = 0;
877 return;
879 memcpy(person, tmp, len);
881 tmp = person;
882 tmp += len;
883 *tmp = 0;
884 while (*tmp != ' ')
885 tmp--;
886 *tz = tmp+1;
888 *tmp = 0;
889 while (*tmp != ' ')
890 tmp--;
891 *time = strtoul(tmp, NULL, 10);
893 *tmp = 0;
894 while (*tmp != ' ')
895 tmp--;
896 *mail = tmp + 1;
897 *tmp = 0;
900 static void get_commit_info(struct commit *commit,
901 struct commit_info *ret,
902 int detailed)
904 int len;
905 char *tmp, *endp;
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);
915 if (!detailed)
916 return;
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");
925 if (!tmp) {
926 error_out:
927 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
928 return;
930 tmp += 2;
931 endp = strchr(tmp, '\n');
932 if (!endp)
933 goto error_out;
934 len = endp - tmp;
935 if (len >= sizeof(summary_buf))
936 goto error_out;
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)
950 int cnt;
951 const char *cp;
952 struct origin *suspect = ent->suspect;
953 char hex[41];
955 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
956 printf("%s%c%d %d %d\n",
957 hex,
958 ent->guilty ? ' ' : '*', // purely for debugging
959 ent->s_lno + 1,
960 ent->lno + 1,
961 ent->num_lines);
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++) {
982 char ch;
983 if (cnt)
984 printf("%s %d %d\n", hex,
985 ent->s_lno + 1 + cnt,
986 ent->lno + 1 + cnt);
987 putchar('\t');
988 do {
989 ch = *cp++;
990 putchar(ch);
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)
998 int cnt;
999 const char *cp;
1000 struct origin *suspect = ent->suspect;
1001 struct commit_info ci;
1002 char hex[41];
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++) {
1010 char ch;
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,
1016 show_raw_time),
1017 ent->lno + 1 + cnt);
1018 else {
1019 if (opt & OUTPUT_SHOW_NAME)
1020 printf(" %-*.*s", longest_file, longest_file,
1021 suspect->path);
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,
1028 show_raw_time),
1029 max_digits, ent->lno + 1 + cnt);
1031 do {
1032 ch = *cp++;
1033 putchar(ch);
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)
1049 continue;
1050 for (oth = ent->next; oth; oth = oth->next) {
1051 if ((oth->suspect->commit != commit) ||
1052 !strcmp(oth->suspect->path, suspect->path))
1053 continue;
1054 commit->object.flags |= MORE_THAN_ONE_PATH;
1055 break;
1060 for (ent = sb->ent; ent; ent = ent->next) {
1061 if (option & OUTPUT_PORCELAIN)
1062 emit_porcelain(sb, ent);
1063 else
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 */
1076 while (len--) {
1077 if (bol) {
1078 sb->lineno = xrealloc(sb->lineno,
1079 sizeof(int* ) * (num + 1));
1080 sb->lineno[num] = buf - sb->final_buf;
1081 bol = 0;
1083 if (*buf++ == '\n') {
1084 num++;
1085 bol = 1;
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");
1095 char buf[1024];
1096 if (!fp)
1097 return -1;
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);
1104 fclose(fp);
1105 return 0;
1108 static int lineno_width(int lines)
1110 int i, width;
1112 for (width = 1, i = 10; i <= lines + 1; width++)
1113 i *= 10;
1114 return 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;
1126 int num;
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)
1135 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)
1153 struct stat st;
1154 return !lstat(path, &st);
1157 int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1159 struct rev_info revs;
1160 const char *path;
1161 struct scoreboard sb;
1162 struct origin *o;
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;
1169 char type[10];
1171 opt = 0;
1172 bottom = top = 0;
1173 seen_dashdash = 0;
1174 for (unk = i = 1; i < argc; i++) {
1175 const char *arg = argv[i];
1176 if (*arg != '-')
1177 break;
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) {
1194 char *term;
1195 arg = argv[i];
1196 if (bottom || top)
1197 die("More than one '-L n,m' option given");
1198 bottom = strtol(arg, &term, 10);
1199 if (*term == ',') {
1200 top = strtol(term + 1, &term, 10);
1201 if (*term)
1202 usage(pickaxe_usage);
1204 if (bottom && top && top < bottom) {
1205 unsigned long tmp;
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)) {
1219 seen_dashdash = 1;
1220 i++;
1221 break;
1223 else
1224 argv[unk++] = 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>"
1249 * "-options <path>"
1250 * but again the first one is allowed only if
1251 * there is no options that we passed to revision
1252 * machinery.
1255 if (seen_dashdash) {
1256 /* (1) */
1257 if (argc <= i)
1258 usage(pickaxe_usage);
1259 path = argv[i];
1260 if (i + 1 == argc - 1) {
1261 if (unk != 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);
1269 else {
1270 int j;
1271 for (j = i; !seen_dashdash && j < argc; j++)
1272 if (!strcmp(argv[j], "--"))
1273 seen_dashdash = 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];
1281 else {
1282 /* (3) */
1283 path = argv[i];
1284 if (i + 1 == argc - 1) {
1285 final_commit_name = argv[i + 1];
1287 /* if (unk == 1) we could be getting
1288 * old-style
1290 if (unk == 1 && !has_path_in_work_tree(path)) {
1291 path = argv[i + 1];
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.
1310 argv[unk] = NULL;
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)
1322 continue;
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);
1328 if (sb.final)
1329 die("More than one commit to dig from %s and %s?",
1330 revs.pending.objects[i].name,
1331 final_commit_name);
1332 sb.final = (struct commit *) obj;
1333 final_commit_name = revs.pending.objects[i].name;
1336 if (!sb.final) {
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
1349 * uninteresting.
1351 prepare_revision_walk(&revs);
1353 o = find_origin(&sb, sb.final, path);
1354 if (!o)
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);
1360 if (bottom < 1)
1361 bottom = 1;
1362 if (top < 1)
1363 top = lno;
1364 bottom--;
1365 if (lno < top)
1366 die("file %s has only %lu lines", path, lno);
1368 ent = xcalloc(1, sizeof(*ent));
1369 ent->lno = bottom;
1370 ent->num_lines = top - bottom;
1371 ent->suspect = o;
1372 ent->s_lno = bottom;
1374 sb.ent = ent;
1375 sb.path = path;
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);
1383 coalesce(&sb);
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;
1392 free(ent);
1393 ent = e;
1395 return 0;