Merge branch 'jc/pickaxe'
[git/jnareb-git/bp-gitweb.git] / blame.c
blob1144c8533724b06534caad7f693bdb65df496682
1 /*
2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
3 */
5 #include <assert.h>
6 #include <time.h>
7 #include <sys/time.h>
8 #include <math.h>
10 #include "cache.h"
11 #include "refs.h"
12 #include "tag.h"
13 #include "commit.h"
14 #include "tree.h"
15 #include "blob.h"
16 #include "diff.h"
17 #include "diffcore.h"
18 #include "revision.h"
19 #include "xdiff-interface.h"
20 #include "quote.h"
22 #ifndef DEBUG
23 #define DEBUG 0
24 #endif
26 static const char blame_usage[] =
27 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-S <revs-file>] [--] file [commit]\n"
28 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
29 " -l, --long Show long commit SHA1 (Default: off)\n"
30 " -t, --time Show raw timestamp (Default: off)\n"
31 " -f, --show-name Show original filename (Default: auto)\n"
32 " -n, --show-number Show original linenumber (Default: off)\n"
33 " -p, --porcelain Show in a format designed for machine consumption\n"
34 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"
35 " -h, --help This message";
37 static struct commit **blame_lines;
38 static int num_blame_lines;
39 static char *blame_contents;
40 static int blame_len;
42 struct util_info {
43 int *line_map;
44 unsigned char sha1[20]; /* blob sha, not commit! */
45 char *buf;
46 unsigned long size;
47 int num_lines;
48 const char *pathname;
49 unsigned meta_given:1;
51 void *topo_data;
54 struct chunk {
55 int off1, len1; /* --- */
56 int off2, len2; /* +++ */
59 struct patch {
60 struct chunk *chunks;
61 int num;
64 static void get_blob(struct commit *commit);
66 /* Only used for statistics */
67 static int num_get_patch;
68 static int num_commits;
69 static int patch_time;
70 static int num_read_blob;
72 struct blame_diff_state {
73 struct xdiff_emit_state xm;
74 struct patch *ret;
77 static void process_u0_diff(void *state_, char *line, unsigned long len)
79 struct blame_diff_state *state = state_;
80 struct chunk *chunk;
82 if (len < 4 || line[0] != '@' || line[1] != '@')
83 return;
85 if (DEBUG)
86 printf("chunk line: %.*s", (int)len, line);
87 state->ret->num++;
88 state->ret->chunks = xrealloc(state->ret->chunks,
89 sizeof(struct chunk) * state->ret->num);
90 chunk = &state->ret->chunks[state->ret->num - 1];
92 assert(!strncmp(line, "@@ -", 4));
94 if (parse_hunk_header(line, len,
95 &chunk->off1, &chunk->len1,
96 &chunk->off2, &chunk->len2)) {
97 state->ret->num--;
98 return;
101 if (chunk->len1 == 0)
102 chunk->off1++;
103 if (chunk->len2 == 0)
104 chunk->off2++;
106 if (chunk->off1 > 0)
107 chunk->off1--;
108 if (chunk->off2 > 0)
109 chunk->off2--;
111 assert(chunk->off1 >= 0);
112 assert(chunk->off2 >= 0);
115 static struct patch *get_patch(struct commit *commit, struct commit *other)
117 struct blame_diff_state state;
118 xpparam_t xpp;
119 xdemitconf_t xecfg;
120 mmfile_t file_c, file_o;
121 xdemitcb_t ecb;
122 struct util_info *info_c = (struct util_info *)commit->util;
123 struct util_info *info_o = (struct util_info *)other->util;
124 struct timeval tv_start, tv_end;
126 get_blob(commit);
127 file_c.ptr = info_c->buf;
128 file_c.size = info_c->size;
130 get_blob(other);
131 file_o.ptr = info_o->buf;
132 file_o.size = info_o->size;
134 gettimeofday(&tv_start, NULL);
136 xpp.flags = XDF_NEED_MINIMAL;
137 xecfg.ctxlen = 0;
138 xecfg.flags = 0;
139 ecb.outf = xdiff_outf;
140 ecb.priv = &state;
141 memset(&state, 0, sizeof(state));
142 state.xm.consume = process_u0_diff;
143 state.ret = xmalloc(sizeof(struct patch));
144 state.ret->chunks = NULL;
145 state.ret->num = 0;
147 xdl_diff(&file_c, &file_o, &xpp, &xecfg, &ecb);
149 gettimeofday(&tv_end, NULL);
150 patch_time += 1000000 * (tv_end.tv_sec - tv_start.tv_sec) +
151 tv_end.tv_usec - tv_start.tv_usec;
153 num_get_patch++;
154 return state.ret;
157 static void free_patch(struct patch *p)
159 free(p->chunks);
160 free(p);
163 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
164 int baselen, const char *pathname,
165 unsigned mode, int stage);
167 static unsigned char blob_sha1[20];
168 static const char *blame_file;
169 static int get_blob_sha1(struct tree *t, const char *pathname,
170 unsigned char *sha1)
172 const char *pathspec[2];
173 blame_file = pathname;
174 pathspec[0] = pathname;
175 pathspec[1] = NULL;
176 hashclr(blob_sha1);
177 read_tree_recursive(t, "", 0, 0, pathspec, get_blob_sha1_internal);
179 if (is_null_sha1(blob_sha1))
180 return -1;
182 hashcpy(sha1, blob_sha1);
183 return 0;
186 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
187 int baselen, const char *pathname,
188 unsigned mode, int stage)
190 if (S_ISDIR(mode))
191 return READ_TREE_RECURSIVE;
193 if (strncmp(blame_file, base, baselen) ||
194 strcmp(blame_file + baselen, pathname))
195 return -1;
197 hashcpy(blob_sha1, sha1);
198 return -1;
201 static void get_blob(struct commit *commit)
203 struct util_info *info = commit->util;
204 char type[20];
206 if (info->buf)
207 return;
209 info->buf = read_sha1_file(info->sha1, type, &info->size);
210 num_read_blob++;
212 assert(!strcmp(type, blob_type));
215 /* For debugging only */
216 static void print_patch(struct patch *p)
218 int i;
219 printf("Num chunks: %d\n", p->num);
220 for (i = 0; i < p->num; i++) {
221 printf("%d,%d %d,%d\n", p->chunks[i].off1, p->chunks[i].len1,
222 p->chunks[i].off2, p->chunks[i].len2);
226 #if DEBUG
227 /* For debugging only */
228 static void print_map(struct commit *cmit, struct commit *other)
230 struct util_info *util = cmit->util;
231 struct util_info *util2 = other->util;
233 int i;
234 int max =
235 util->num_lines >
236 util2->num_lines ? util->num_lines : util2->num_lines;
237 int num;
239 if (print_map == NULL)
240 ; /* to avoid "unused function" warning */
242 for (i = 0; i < max; i++) {
243 printf("i: %d ", i);
244 num = -1;
246 if (i < util->num_lines) {
247 num = util->line_map[i];
248 printf("%d\t", num);
250 else
251 printf("\t");
253 if (i < util2->num_lines) {
254 int num2 = util2->line_map[i];
255 printf("%d\t", num2);
256 if (num != -1 && num2 != num)
257 printf("---");
259 else
260 printf("\t");
262 printf("\n");
265 #endif
267 /* p is a patch from commit to other. */
268 static void fill_line_map(struct commit *commit, struct commit *other,
269 struct patch *p)
271 struct util_info *util = commit->util;
272 struct util_info *util2 = other->util;
273 int *map = util->line_map;
274 int *map2 = util2->line_map;
275 int cur_chunk = 0;
276 int i1, i2;
278 if (DEBUG) {
279 if (p->num)
280 print_patch(p);
281 printf("num lines 1: %d num lines 2: %d\n", util->num_lines,
282 util2->num_lines);
285 for (i1 = 0, i2 = 0; i1 < util->num_lines; i1++, i2++) {
286 struct chunk *chunk = NULL;
287 if (cur_chunk < p->num)
288 chunk = &p->chunks[cur_chunk];
290 if (chunk && chunk->off1 == i1) {
291 if (DEBUG && i2 != chunk->off2)
292 printf("i2: %d off2: %d\n", i2, chunk->off2);
294 assert(i2 == chunk->off2);
296 i1--;
297 i2--;
298 if (chunk->len1 > 0)
299 i1 += chunk->len1;
301 if (chunk->len2 > 0)
302 i2 += chunk->len2;
304 cur_chunk++;
306 else {
307 if (i2 >= util2->num_lines)
308 break;
310 if (map[i1] != map2[i2] && map[i1] != -1) {
311 if (DEBUG)
312 printf("map: i1: %d %d %p i2: %d %d %p\n",
313 i1, map[i1],
314 (void *) (i1 != -1 ? blame_lines[map[i1]] : NULL),
315 i2, map2[i2],
316 (void *) (i2 != -1 ? blame_lines[map2[i2]] : NULL));
317 if (map2[i2] != -1 &&
318 blame_lines[map[i1]] &&
319 !blame_lines[map2[i2]])
320 map[i1] = map2[i2];
323 if (map[i1] == -1 && map2[i2] != -1)
324 map[i1] = map2[i2];
327 if (DEBUG > 1)
328 printf("l1: %d l2: %d i1: %d i2: %d\n",
329 map[i1], map2[i2], i1, i2);
333 static int map_line(struct commit *commit, int line)
335 struct util_info *info = commit->util;
336 assert(line >= 0 && line < info->num_lines);
337 return info->line_map[line];
340 static struct util_info *get_util(struct commit *commit)
342 struct util_info *util = commit->util;
344 if (util)
345 return util;
347 util = xcalloc(1, sizeof(struct util_info));
348 util->num_lines = -1;
349 commit->util = util;
350 return util;
353 static int fill_util_info(struct commit *commit)
355 struct util_info *util = commit->util;
357 assert(util);
358 assert(util->pathname);
360 return !!get_blob_sha1(commit->tree, util->pathname, util->sha1);
363 static void alloc_line_map(struct commit *commit)
365 struct util_info *util = commit->util;
366 int i;
368 if (util->line_map)
369 return;
371 get_blob(commit);
373 util->num_lines = 0;
374 for (i = 0; i < util->size; i++) {
375 if (util->buf[i] == '\n')
376 util->num_lines++;
378 if (util->buf[util->size - 1] != '\n')
379 util->num_lines++;
381 util->line_map = xmalloc(sizeof(int) * util->num_lines);
383 for (i = 0; i < util->num_lines; i++)
384 util->line_map[i] = -1;
387 static void init_first_commit(struct commit *commit, const char *filename)
389 struct util_info *util = commit->util;
390 int i;
392 util->pathname = filename;
393 if (fill_util_info(commit))
394 die("fill_util_info failed");
396 alloc_line_map(commit);
398 util = commit->util;
400 for (i = 0; i < util->num_lines; i++)
401 util->line_map[i] = i;
404 static void process_commits(struct rev_info *rev, const char *path,
405 struct commit **initial)
407 int i;
408 struct util_info *util;
409 int lines_left;
410 int *blame_p;
411 int *new_lines;
412 int new_lines_len;
414 struct commit *commit = get_revision(rev);
415 assert(commit);
416 init_first_commit(commit, path);
418 util = commit->util;
419 num_blame_lines = util->num_lines;
420 blame_lines = xmalloc(sizeof(struct commit *) * num_blame_lines);
421 blame_contents = util->buf;
422 blame_len = util->size;
424 for (i = 0; i < num_blame_lines; i++)
425 blame_lines[i] = NULL;
427 lines_left = num_blame_lines;
428 blame_p = xmalloc(sizeof(int) * num_blame_lines);
429 new_lines = xmalloc(sizeof(int) * num_blame_lines);
430 do {
431 struct commit_list *parents;
432 int num_parents;
433 struct util_info *util;
435 if (DEBUG)
436 printf("\nProcessing commit: %d %s\n", num_commits,
437 sha1_to_hex(commit->object.sha1));
439 if (lines_left == 0)
440 return;
442 num_commits++;
443 memset(blame_p, 0, sizeof(int) * num_blame_lines);
444 new_lines_len = 0;
445 num_parents = 0;
446 for (parents = commit->parents;
447 parents != NULL; parents = parents->next)
448 num_parents++;
450 if (num_parents == 0)
451 *initial = commit;
453 if (fill_util_info(commit))
454 continue;
456 alloc_line_map(commit);
457 util = commit->util;
459 for (parents = commit->parents;
460 parents != NULL; parents = parents->next) {
461 struct commit *parent = parents->item;
462 struct patch *patch;
464 if (parse_commit(parent) < 0)
465 die("parse_commit error");
467 if (DEBUG)
468 printf("parent: %s\n",
469 sha1_to_hex(parent->object.sha1));
471 if (fill_util_info(parent)) {
472 num_parents--;
473 continue;
476 patch = get_patch(parent, commit);
477 alloc_line_map(parent);
478 fill_line_map(parent, commit, patch);
480 for (i = 0; i < patch->num; i++) {
481 int l;
482 for (l = 0; l < patch->chunks[i].len2; l++) {
483 int mapped_line =
484 map_line(commit, patch->chunks[i].off2 + l);
485 if (mapped_line != -1) {
486 blame_p[mapped_line]++;
487 if (blame_p[mapped_line] == num_parents)
488 new_lines[new_lines_len++] = mapped_line;
492 free_patch(patch);
495 if (DEBUG)
496 printf("parents: %d\n", num_parents);
498 for (i = 0; i < new_lines_len; i++) {
499 int mapped_line = new_lines[i];
500 if (blame_lines[mapped_line] == NULL) {
501 blame_lines[mapped_line] = commit;
502 lines_left--;
503 if (DEBUG)
504 printf("blame: mapped: %d i: %d\n",
505 mapped_line, i);
508 } while ((commit = get_revision(rev)) != NULL);
511 static int compare_tree_path(struct rev_info *revs,
512 struct commit *c1, struct commit *c2)
514 int ret;
515 const char *paths[2];
516 struct util_info *util = c2->util;
517 paths[0] = util->pathname;
518 paths[1] = NULL;
520 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
521 &revs->pruning);
522 ret = rev_compare_tree(revs, c1->tree, c2->tree);
523 diff_tree_release_paths(&revs->pruning);
524 return ret;
527 static int same_tree_as_empty_path(struct rev_info *revs, struct tree *t1,
528 const char *path)
530 int ret;
531 const char *paths[2];
532 paths[0] = path;
533 paths[1] = NULL;
535 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
536 &revs->pruning);
537 ret = rev_same_tree_as_empty(revs, t1);
538 diff_tree_release_paths(&revs->pruning);
539 return ret;
542 static const char *find_rename(struct commit *commit, struct commit *parent)
544 struct util_info *cutil = commit->util;
545 struct diff_options diff_opts;
546 const char *paths[1];
547 int i;
549 if (DEBUG) {
550 printf("find_rename commit: %s ",
551 sha1_to_hex(commit->object.sha1));
552 puts(sha1_to_hex(parent->object.sha1));
555 diff_setup(&diff_opts);
556 diff_opts.recursive = 1;
557 diff_opts.detect_rename = DIFF_DETECT_RENAME;
558 paths[0] = NULL;
559 diff_tree_setup_paths(paths, &diff_opts);
560 if (diff_setup_done(&diff_opts) < 0)
561 die("diff_setup_done failed");
563 diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
564 "", &diff_opts);
565 diffcore_std(&diff_opts);
567 for (i = 0; i < diff_queued_diff.nr; i++) {
568 struct diff_filepair *p = diff_queued_diff.queue[i];
570 if (p->status == 'R' &&
571 !strcmp(p->one->path, cutil->pathname)) {
572 if (DEBUG)
573 printf("rename %s -> %s\n",
574 p->one->path, p->two->path);
575 return p->two->path;
579 return 0;
582 static void simplify_commit(struct rev_info *revs, struct commit *commit)
584 struct commit_list **pp, *parent;
586 if (!commit->tree)
587 return;
589 if (!commit->parents) {
590 struct util_info *util = commit->util;
591 if (!same_tree_as_empty_path(revs, commit->tree,
592 util->pathname))
593 commit->object.flags |= TREECHANGE;
594 return;
597 pp = &commit->parents;
598 while ((parent = *pp) != NULL) {
599 struct commit *p = parent->item;
601 if (p->object.flags & UNINTERESTING) {
602 pp = &parent->next;
603 continue;
606 parse_commit(p);
607 switch (compare_tree_path(revs, p, commit)) {
608 case REV_TREE_SAME:
609 parent->next = NULL;
610 commit->parents = parent;
611 get_util(p)->pathname = get_util(commit)->pathname;
612 return;
614 case REV_TREE_NEW:
616 struct util_info *util = commit->util;
617 if (revs->remove_empty_trees &&
618 same_tree_as_empty_path(revs, p->tree,
619 util->pathname)) {
620 const char *new_name = find_rename(commit, p);
621 if (new_name) {
622 struct util_info *putil = get_util(p);
623 if (!putil->pathname)
624 putil->pathname = xstrdup(new_name);
626 else {
627 *pp = parent->next;
628 continue;
633 /* fallthrough */
634 case REV_TREE_DIFFERENT:
635 pp = &parent->next;
636 if (!get_util(p)->pathname)
637 get_util(p)->pathname =
638 get_util(commit)->pathname;
639 continue;
641 die("bad tree compare for commit %s",
642 sha1_to_hex(commit->object.sha1));
644 commit->object.flags |= TREECHANGE;
647 struct commit_info
649 char *author;
650 char *author_mail;
651 unsigned long author_time;
652 char *author_tz;
654 /* filled only when asked for details */
655 char *committer;
656 char *committer_mail;
657 unsigned long committer_time;
658 char *committer_tz;
660 char *summary;
663 static void get_ac_line(const char *inbuf, const char *what,
664 int bufsz, char *person, char **mail,
665 unsigned long *time, char **tz)
667 int len;
668 char *tmp, *endp;
670 tmp = strstr(inbuf, what);
671 if (!tmp)
672 goto error_out;
673 tmp += strlen(what);
674 endp = strchr(tmp, '\n');
675 if (!endp)
676 len = strlen(tmp);
677 else
678 len = endp - tmp;
679 if (bufsz <= len) {
680 error_out:
681 /* Ugh */
682 person = *mail = *tz = "(unknown)";
683 *time = 0;
684 return;
686 memcpy(person, tmp, len);
688 tmp = person;
689 tmp += len;
690 *tmp = 0;
691 while (*tmp != ' ')
692 tmp--;
693 *tz = tmp+1;
695 *tmp = 0;
696 while (*tmp != ' ')
697 tmp--;
698 *time = strtoul(tmp, NULL, 10);
700 *tmp = 0;
701 while (*tmp != ' ')
702 tmp--;
703 *mail = tmp + 1;
704 *tmp = 0;
707 static void get_commit_info(struct commit *commit, struct commit_info *ret, int detailed)
709 int len;
710 char *tmp, *endp;
711 static char author_buf[1024];
712 static char committer_buf[1024];
713 static char summary_buf[1024];
715 ret->author = author_buf;
716 get_ac_line(commit->buffer, "\nauthor ",
717 sizeof(author_buf), author_buf, &ret->author_mail,
718 &ret->author_time, &ret->author_tz);
720 if (!detailed)
721 return;
723 ret->committer = committer_buf;
724 get_ac_line(commit->buffer, "\ncommitter ",
725 sizeof(committer_buf), committer_buf, &ret->committer_mail,
726 &ret->committer_time, &ret->committer_tz);
728 ret->summary = summary_buf;
729 tmp = strstr(commit->buffer, "\n\n");
730 if (!tmp) {
731 error_out:
732 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
733 return;
735 tmp += 2;
736 endp = strchr(tmp, '\n');
737 if (!endp)
738 goto error_out;
739 len = endp - tmp;
740 if (len >= sizeof(summary_buf))
741 goto error_out;
742 memcpy(summary_buf, tmp, len);
743 summary_buf[len] = 0;
746 static const char *format_time(unsigned long time, const char *tz_str,
747 int show_raw_time)
749 static char time_buf[128];
750 time_t t = time;
751 int minutes, tz;
752 struct tm *tm;
754 if (show_raw_time) {
755 sprintf(time_buf, "%lu %s", time, tz_str);
756 return time_buf;
759 tz = atoi(tz_str);
760 minutes = tz < 0 ? -tz : tz;
761 minutes = (minutes / 100)*60 + (minutes % 100);
762 minutes = tz < 0 ? -minutes : minutes;
763 t = time + minutes * 60;
764 tm = gmtime(&t);
766 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
767 strcat(time_buf, tz_str);
768 return time_buf;
771 static void topo_setter(struct commit *c, void *data)
773 struct util_info *util = c->util;
774 util->topo_data = data;
777 static void *topo_getter(struct commit *c)
779 struct util_info *util = c->util;
780 return util->topo_data;
783 static int read_ancestry(const char *graft_file,
784 unsigned char **start_sha1)
786 FILE *fp = fopen(graft_file, "r");
787 char buf[1024];
788 if (!fp)
789 return -1;
790 while (fgets(buf, sizeof(buf), fp)) {
791 /* The format is just "Commit Parent1 Parent2 ...\n" */
792 int len = strlen(buf);
793 struct commit_graft *graft = read_graft_line(buf, len);
794 register_commit_graft(graft, 0);
795 if (!*start_sha1)
796 *start_sha1 = graft->sha1;
798 fclose(fp);
799 return 0;
802 static int lineno_width(int lines)
804 int i, width;
806 for (width = 1, i = 10; i <= lines + 1; width++)
807 i *= 10;
808 return width;
811 static int find_orig_linenum(struct util_info *u, int lineno)
813 int i;
815 for (i = 0; i < u->num_lines; i++)
816 if (lineno == u->line_map[i])
817 return i + 1;
818 return 0;
821 static void emit_meta(struct commit *c, int lno,
822 int sha1_len, int compatibility, int porcelain,
823 int show_name, int show_number, int show_raw_time,
824 int longest_file, int longest_author,
825 int max_digits, int max_orig_digits)
827 struct util_info *u;
828 int lineno;
829 struct commit_info ci;
831 u = c->util;
832 lineno = find_orig_linenum(u, lno);
834 if (porcelain) {
835 int group_size = -1;
836 struct commit *cc = (lno == 0) ? NULL : blame_lines[lno-1];
837 if (cc != c) {
838 /* This is the beginning of this group */
839 int i;
840 for (i = lno + 1; i < num_blame_lines; i++)
841 if (blame_lines[i] != c)
842 break;
843 group_size = i - lno;
845 if (0 < group_size)
846 printf("%s %d %d %d\n", sha1_to_hex(c->object.sha1),
847 lineno, lno + 1, group_size);
848 else
849 printf("%s %d %d\n", sha1_to_hex(c->object.sha1),
850 lineno, lno + 1);
851 if (!u->meta_given) {
852 get_commit_info(c, &ci, 1);
853 printf("author %s\n", ci.author);
854 printf("author-mail %s\n", ci.author_mail);
855 printf("author-time %lu\n", ci.author_time);
856 printf("author-tz %s\n", ci.author_tz);
857 printf("committer %s\n", ci.committer);
858 printf("committer-mail %s\n", ci.committer_mail);
859 printf("committer-time %lu\n", ci.committer_time);
860 printf("committer-tz %s\n", ci.committer_tz);
861 printf("filename ");
862 if (quote_c_style(u->pathname, NULL, NULL, 0))
863 quote_c_style(u->pathname, NULL, stdout, 0);
864 else
865 fputs(u->pathname, stdout);
866 printf("\nsummary %s\n", ci.summary);
868 u->meta_given = 1;
870 putchar('\t');
871 return;
874 get_commit_info(c, &ci, 0);
875 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
876 if (compatibility) {
877 printf("\t(%10s\t%10s\t%d)", ci.author,
878 format_time(ci.author_time, ci.author_tz,
879 show_raw_time),
880 lno + 1);
882 else {
883 if (show_name)
884 printf(" %-*.*s", longest_file, longest_file,
885 u->pathname);
886 if (show_number)
887 printf(" %*d", max_orig_digits,
888 lineno);
889 printf(" (%-*.*s %10s %*d) ",
890 longest_author, longest_author, ci.author,
891 format_time(ci.author_time, ci.author_tz,
892 show_raw_time),
893 max_digits, lno + 1);
897 int main(int argc, const char **argv)
899 int i;
900 struct commit *initial = NULL;
901 unsigned char sha1[20], *sha1_p = NULL;
903 const char *filename = NULL, *commit = NULL;
904 char filename_buf[256];
905 int sha1_len = 8;
906 int compatibility = 0;
907 int show_raw_time = 0;
908 int options = 1;
909 struct commit *start_commit;
911 const char *args[10];
912 struct rev_info rev;
914 struct commit_info ci;
915 const char *buf;
916 int max_digits, max_orig_digits;
917 int longest_file, longest_author, longest_file_lines;
918 int show_name = 0;
919 int show_number = 0;
920 int porcelain = 0;
922 const char *prefix = setup_git_directory();
923 git_config(git_default_config);
925 for (i = 1; i < argc; i++) {
926 if (options) {
927 if (!strcmp(argv[i], "-h") ||
928 !strcmp(argv[i], "--help"))
929 usage(blame_usage);
930 if (!strcmp(argv[i], "-l") ||
931 !strcmp(argv[i], "--long")) {
932 sha1_len = 40;
933 continue;
935 if (!strcmp(argv[i], "-c") ||
936 !strcmp(argv[i], "--compatibility")) {
937 compatibility = 1;
938 continue;
940 if (!strcmp(argv[i], "-t") ||
941 !strcmp(argv[i], "--time")) {
942 show_raw_time = 1;
943 continue;
945 if (!strcmp(argv[i], "-S")) {
946 if (i + 1 < argc &&
947 !read_ancestry(argv[i + 1], &sha1_p)) {
948 compatibility = 1;
949 i++;
950 continue;
952 usage(blame_usage);
954 if (!strcmp(argv[i], "-f") ||
955 !strcmp(argv[i], "--show-name")) {
956 show_name = 1;
957 continue;
959 if (!strcmp(argv[i], "-n") ||
960 !strcmp(argv[i], "--show-number")) {
961 show_number = 1;
962 continue;
964 if (!strcmp(argv[i], "-p") ||
965 !strcmp(argv[i], "--porcelain")) {
966 porcelain = 1;
967 sha1_len = 40;
968 show_raw_time = 1;
969 continue;
971 if (!strcmp(argv[i], "--")) {
972 options = 0;
973 continue;
975 if (argv[i][0] == '-')
976 usage(blame_usage);
977 options = 0;
980 if (!options) {
981 if (!filename)
982 filename = argv[i];
983 else if (!commit)
984 commit = argv[i];
985 else
986 usage(blame_usage);
990 if (!filename)
991 usage(blame_usage);
992 if (commit && sha1_p)
993 usage(blame_usage);
994 else if (!commit)
995 commit = "HEAD";
997 if (prefix)
998 sprintf(filename_buf, "%s%s", prefix, filename);
999 else
1000 strcpy(filename_buf, filename);
1001 filename = filename_buf;
1003 if (!sha1_p) {
1004 if (get_sha1(commit, sha1))
1005 die("get_sha1 failed, commit '%s' not found", commit);
1006 sha1_p = sha1;
1008 start_commit = lookup_commit_reference(sha1_p);
1009 get_util(start_commit)->pathname = filename;
1010 if (fill_util_info(start_commit)) {
1011 printf("%s not found in %s\n", filename, commit);
1012 return 1;
1015 init_revisions(&rev, setup_git_directory());
1016 rev.remove_empty_trees = 1;
1017 rev.topo_order = 1;
1018 rev.prune_fn = simplify_commit;
1019 rev.topo_setter = topo_setter;
1020 rev.topo_getter = topo_getter;
1021 rev.parents = 1;
1022 rev.limited = 1;
1024 commit_list_insert(start_commit, &rev.commits);
1026 args[0] = filename;
1027 args[1] = NULL;
1028 diff_tree_setup_paths(args, &rev.pruning);
1029 prepare_revision_walk(&rev);
1030 process_commits(&rev, filename, &initial);
1032 for (i = 0; i < num_blame_lines; i++)
1033 if (!blame_lines[i])
1034 blame_lines[i] = initial;
1036 buf = blame_contents;
1037 max_digits = lineno_width(num_blame_lines);
1039 longest_file = 0;
1040 longest_author = 0;
1041 longest_file_lines = 0;
1042 for (i = 0; i < num_blame_lines; i++) {
1043 struct commit *c = blame_lines[i];
1044 struct util_info *u;
1045 u = c->util;
1047 if (!show_name && strcmp(filename, u->pathname))
1048 show_name = 1;
1049 if (longest_file < strlen(u->pathname))
1050 longest_file = strlen(u->pathname);
1051 if (longest_file_lines < u->num_lines)
1052 longest_file_lines = u->num_lines;
1053 get_commit_info(c, &ci, 0);
1054 if (longest_author < strlen(ci.author))
1055 longest_author = strlen(ci.author);
1058 max_orig_digits = lineno_width(longest_file_lines);
1060 for (i = 0; i < num_blame_lines; i++) {
1061 emit_meta(blame_lines[i], i,
1062 sha1_len, compatibility, porcelain,
1063 show_name, show_number, show_raw_time,
1064 longest_file, longest_author,
1065 max_digits, max_orig_digits);
1067 if (i == num_blame_lines - 1) {
1068 fwrite(buf, blame_len - (buf - blame_contents),
1069 1, stdout);
1070 if (blame_contents[blame_len-1] != '\n')
1071 putc('\n', stdout);
1073 else {
1074 char *next_buf = strchr(buf, '\n') + 1;
1075 fwrite(buf, next_buf - buf, 1, stdout);
1076 buf = next_buf;
1080 if (DEBUG) {
1081 printf("num read blob: %d\n", num_read_blob);
1082 printf("num get patch: %d\n", num_get_patch);
1083 printf("num commits: %d\n", num_commits);
1084 printf("patch time: %f\n", patch_time / 1000000.0);
1085 printf("initial: %s\n", sha1_to_hex(initial->object.sha1));
1088 return 0;