2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
19 #include "xdiff-interface.h"
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;
44 unsigned char sha1[20]; /* blob sha, not commit! */
49 unsigned meta_given:1;
55 int off1, len1; /* --- */
56 int off2, len2; /* +++ */
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;
71 struct blame_diff_state {
72 struct xdiff_emit_state xm;
76 static void process_u0_diff(void *state_, char *line, unsigned long len)
78 struct blame_diff_state *state = state_;
81 if (len < 4 || line[0] != '@' || line[1] != '@')
85 printf("chunk line: %.*s", (int)len, line);
87 state->ret->chunks = xrealloc(state->ret->chunks,
88 sizeof(struct chunk) * state->ret->num);
89 chunk = &state->ret->chunks[state->ret->num - 1];
91 assert(!strncmp(line, "@@ -", 4));
93 if (parse_hunk_header(line, len,
94 &chunk->off1, &chunk->len1,
95 &chunk->off2, &chunk->len2)) {
100 if (chunk->len1 == 0)
102 if (chunk->len2 == 0)
110 assert(chunk->off1 >= 0);
111 assert(chunk->off2 >= 0);
114 static struct patch *get_patch(struct commit *commit, struct commit *other)
116 struct blame_diff_state state;
119 mmfile_t file_c, file_o;
121 struct util_info *info_c = (struct util_info *)commit->util;
122 struct util_info *info_o = (struct util_info *)other->util;
123 struct timeval tv_start, tv_end;
126 file_c.ptr = info_c->buf;
127 file_c.size = info_c->size;
130 file_o.ptr = info_o->buf;
131 file_o.size = info_o->size;
133 gettimeofday(&tv_start, NULL);
135 xpp.flags = XDF_NEED_MINIMAL;
138 ecb.outf = xdiff_outf;
140 memset(&state, 0, sizeof(state));
141 state.xm.consume = process_u0_diff;
142 state.ret = xmalloc(sizeof(struct patch));
143 state.ret->chunks = NULL;
146 xdl_diff(&file_c, &file_o, &xpp, &xecfg, &ecb);
148 gettimeofday(&tv_end, NULL);
149 patch_time += 1000000 * (tv_end.tv_sec - tv_start.tv_sec) +
150 tv_end.tv_usec - tv_start.tv_usec;
156 static void free_patch(struct patch *p)
162 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
163 int baselen, const char *pathname,
164 unsigned mode, int stage);
166 static unsigned char blob_sha1[20];
167 static const char *blame_file;
168 static int get_blob_sha1(struct tree *t, const char *pathname,
171 const char *pathspec[2];
172 blame_file = pathname;
173 pathspec[0] = pathname;
176 read_tree_recursive(t, "", 0, 0, pathspec, get_blob_sha1_internal);
178 if (is_null_sha1(blob_sha1))
181 hashcpy(sha1, blob_sha1);
185 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
186 int baselen, const char *pathname,
187 unsigned mode, int stage)
190 return READ_TREE_RECURSIVE;
192 if (strncmp(blame_file, base, baselen) ||
193 strcmp(blame_file + baselen, pathname))
196 hashcpy(blob_sha1, sha1);
200 static void get_blob(struct commit *commit)
202 struct util_info *info = commit->util;
208 info->buf = read_sha1_file(info->sha1, type, &info->size);
210 assert(!strcmp(type, blob_type));
213 /* For debugging only */
214 static void print_patch(struct patch *p)
217 printf("Num chunks: %d\n", p->num);
218 for (i = 0; i < p->num; i++) {
219 printf("%d,%d %d,%d\n", p->chunks[i].off1, p->chunks[i].len1,
220 p->chunks[i].off2, p->chunks[i].len2);
225 /* For debugging only */
226 static void print_map(struct commit *cmit, struct commit *other)
228 struct util_info *util = cmit->util;
229 struct util_info *util2 = other->util;
234 util2->num_lines ? util->num_lines : util2->num_lines;
237 if (print_map == NULL)
238 ; /* to avoid "unused function" warning */
240 for (i = 0; i < max; i++) {
244 if (i < util->num_lines) {
245 num = util->line_map[i];
251 if (i < util2->num_lines) {
252 int num2 = util2->line_map[i];
253 printf("%d\t", num2);
254 if (num != -1 && num2 != num)
265 /* p is a patch from commit to other. */
266 static void fill_line_map(struct commit *commit, struct commit *other,
269 struct util_info *util = commit->util;
270 struct util_info *util2 = other->util;
271 int *map = util->line_map;
272 int *map2 = util2->line_map;
279 printf("num lines 1: %d num lines 2: %d\n", util->num_lines,
283 for (i1 = 0, i2 = 0; i1 < util->num_lines; i1++, i2++) {
284 struct chunk *chunk = NULL;
285 if (cur_chunk < p->num)
286 chunk = &p->chunks[cur_chunk];
288 if (chunk && chunk->off1 == i1) {
289 if (DEBUG && i2 != chunk->off2)
290 printf("i2: %d off2: %d\n", i2, chunk->off2);
292 assert(i2 == chunk->off2);
305 if (i2 >= util2->num_lines)
308 if (map[i1] != map2[i2] && map[i1] != -1) {
310 printf("map: i1: %d %d %p i2: %d %d %p\n",
312 (void *) (i1 != -1 ? blame_lines[map[i1]] : NULL),
314 (void *) (i2 != -1 ? blame_lines[map2[i2]] : NULL));
315 if (map2[i2] != -1 &&
316 blame_lines[map[i1]] &&
317 !blame_lines[map2[i2]])
321 if (map[i1] == -1 && map2[i2] != -1)
326 printf("l1: %d l2: %d i1: %d i2: %d\n",
327 map[i1], map2[i2], i1, i2);
331 static int map_line(struct commit *commit, int line)
333 struct util_info *info = commit->util;
334 assert(line >= 0 && line < info->num_lines);
335 return info->line_map[line];
338 static struct util_info *get_util(struct commit *commit)
340 struct util_info *util = commit->util;
345 util = xcalloc(1, sizeof(struct util_info));
346 util->num_lines = -1;
351 static int fill_util_info(struct commit *commit)
353 struct util_info *util = commit->util;
356 assert(util->pathname);
358 return !!get_blob_sha1(commit->tree, util->pathname, util->sha1);
361 static void alloc_line_map(struct commit *commit)
363 struct util_info *util = commit->util;
372 for (i = 0; i < util->size; i++) {
373 if (util->buf[i] == '\n')
376 if (util->buf[util->size - 1] != '\n')
379 util->line_map = xmalloc(sizeof(int) * util->num_lines);
381 for (i = 0; i < util->num_lines; i++)
382 util->line_map[i] = -1;
385 static void init_first_commit(struct commit *commit, const char *filename)
387 struct util_info *util = commit->util;
390 util->pathname = filename;
391 if (fill_util_info(commit))
392 die("fill_util_info failed");
394 alloc_line_map(commit);
398 for (i = 0; i < util->num_lines; i++)
399 util->line_map[i] = i;
402 static void process_commits(struct rev_info *rev, const char *path,
403 struct commit **initial)
406 struct util_info *util;
412 struct commit *commit = get_revision(rev);
414 init_first_commit(commit, path);
417 num_blame_lines = util->num_lines;
418 blame_lines = xmalloc(sizeof(struct commit *) * num_blame_lines);
419 blame_contents = util->buf;
420 blame_len = util->size;
422 for (i = 0; i < num_blame_lines; i++)
423 blame_lines[i] = NULL;
425 lines_left = num_blame_lines;
426 blame_p = xmalloc(sizeof(int) * num_blame_lines);
427 new_lines = xmalloc(sizeof(int) * num_blame_lines);
429 struct commit_list *parents;
431 struct util_info *util;
434 printf("\nProcessing commit: %d %s\n", num_commits,
435 sha1_to_hex(commit->object.sha1));
441 memset(blame_p, 0, sizeof(int) * num_blame_lines);
444 for (parents = commit->parents;
445 parents != NULL; parents = parents->next)
448 if (num_parents == 0)
451 if (fill_util_info(commit))
454 alloc_line_map(commit);
457 for (parents = commit->parents;
458 parents != NULL; parents = parents->next) {
459 struct commit *parent = parents->item;
462 if (parse_commit(parent) < 0)
463 die("parse_commit error");
466 printf("parent: %s\n",
467 sha1_to_hex(parent->object.sha1));
469 if (fill_util_info(parent)) {
474 patch = get_patch(parent, commit);
475 alloc_line_map(parent);
476 fill_line_map(parent, commit, patch);
478 for (i = 0; i < patch->num; i++) {
480 for (l = 0; l < patch->chunks[i].len2; l++) {
482 map_line(commit, patch->chunks[i].off2 + l);
483 if (mapped_line != -1) {
484 blame_p[mapped_line]++;
485 if (blame_p[mapped_line] == num_parents)
486 new_lines[new_lines_len++] = mapped_line;
494 printf("parents: %d\n", num_parents);
496 for (i = 0; i < new_lines_len; i++) {
497 int mapped_line = new_lines[i];
498 if (blame_lines[mapped_line] == NULL) {
499 blame_lines[mapped_line] = commit;
502 printf("blame: mapped: %d i: %d\n",
506 } while ((commit = get_revision(rev)) != NULL);
509 static int compare_tree_path(struct rev_info *revs,
510 struct commit *c1, struct commit *c2)
513 const char *paths[2];
514 struct util_info *util = c2->util;
515 paths[0] = util->pathname;
518 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
520 ret = rev_compare_tree(revs, c1->tree, c2->tree);
521 diff_tree_release_paths(&revs->pruning);
525 static int same_tree_as_empty_path(struct rev_info *revs, struct tree *t1,
529 const char *paths[2];
533 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
535 ret = rev_same_tree_as_empty(revs, t1);
536 diff_tree_release_paths(&revs->pruning);
540 static const char *find_rename(struct commit *commit, struct commit *parent)
542 struct util_info *cutil = commit->util;
543 struct diff_options diff_opts;
544 const char *paths[1];
548 printf("find_rename commit: %s ",
549 sha1_to_hex(commit->object.sha1));
550 puts(sha1_to_hex(parent->object.sha1));
553 diff_setup(&diff_opts);
554 diff_opts.recursive = 1;
555 diff_opts.detect_rename = DIFF_DETECT_RENAME;
557 diff_tree_setup_paths(paths, &diff_opts);
558 if (diff_setup_done(&diff_opts) < 0)
559 die("diff_setup_done failed");
561 diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
563 diffcore_std(&diff_opts);
565 for (i = 0; i < diff_queued_diff.nr; i++) {
566 struct diff_filepair *p = diff_queued_diff.queue[i];
568 if (p->status == 'R' &&
569 !strcmp(p->one->path, cutil->pathname)) {
571 printf("rename %s -> %s\n",
572 p->one->path, p->two->path);
580 static void simplify_commit(struct rev_info *revs, struct commit *commit)
582 struct commit_list **pp, *parent;
587 if (!commit->parents) {
588 struct util_info *util = commit->util;
589 if (!same_tree_as_empty_path(revs, commit->tree,
591 commit->object.flags |= TREECHANGE;
595 pp = &commit->parents;
596 while ((parent = *pp) != NULL) {
597 struct commit *p = parent->item;
599 if (p->object.flags & UNINTERESTING) {
605 switch (compare_tree_path(revs, p, commit)) {
608 commit->parents = parent;
609 get_util(p)->pathname = get_util(commit)->pathname;
614 struct util_info *util = commit->util;
615 if (revs->remove_empty_trees &&
616 same_tree_as_empty_path(revs, p->tree,
618 const char *new_name = find_rename(commit, p);
620 struct util_info *putil = get_util(p);
621 if (!putil->pathname)
622 putil->pathname = xstrdup(new_name);
632 case REV_TREE_DIFFERENT:
634 if (!get_util(p)->pathname)
635 get_util(p)->pathname =
636 get_util(commit)->pathname;
639 die("bad tree compare for commit %s",
640 sha1_to_hex(commit->object.sha1));
642 commit->object.flags |= TREECHANGE;
649 unsigned long author_time;
652 /* filled only when asked for details */
654 char *committer_mail;
655 unsigned long committer_time;
661 static void get_ac_line(const char *inbuf, const char *what,
662 int bufsz, char *person, char **mail,
663 unsigned long *time, char **tz)
668 tmp = strstr(inbuf, what);
672 endp = strchr(tmp, '\n');
680 person = *mail = *tz = "(unknown)";
684 memcpy(person, tmp, len);
696 *time = strtoul(tmp, NULL, 10);
705 static void get_commit_info(struct commit *commit, struct commit_info *ret, int detailed)
709 static char author_buf[1024];
710 static char committer_buf[1024];
711 static char summary_buf[1024];
713 ret->author = author_buf;
714 get_ac_line(commit->buffer, "\nauthor ",
715 sizeof(author_buf), author_buf, &ret->author_mail,
716 &ret->author_time, &ret->author_tz);
721 ret->committer = committer_buf;
722 get_ac_line(commit->buffer, "\ncommitter ",
723 sizeof(committer_buf), committer_buf, &ret->committer_mail,
724 &ret->committer_time, &ret->committer_tz);
726 ret->summary = summary_buf;
727 tmp = strstr(commit->buffer, "\n\n");
730 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
734 endp = strchr(tmp, '\n');
738 if (len >= sizeof(summary_buf))
740 memcpy(summary_buf, tmp, len);
741 summary_buf[len] = 0;
744 static const char *format_time(unsigned long time, const char *tz_str,
747 static char time_buf[128];
753 sprintf(time_buf, "%lu %s", time, tz_str);
758 minutes = tz < 0 ? -tz : tz;
759 minutes = (minutes / 100)*60 + (minutes % 100);
760 minutes = tz < 0 ? -minutes : minutes;
761 t = time + minutes * 60;
764 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
765 strcat(time_buf, tz_str);
769 static void topo_setter(struct commit *c, void *data)
771 struct util_info *util = c->util;
772 util->topo_data = data;
775 static void *topo_getter(struct commit *c)
777 struct util_info *util = c->util;
778 return util->topo_data;
781 static int read_ancestry(const char *graft_file,
782 unsigned char **start_sha1)
784 FILE *fp = fopen(graft_file, "r");
788 while (fgets(buf, sizeof(buf), fp)) {
789 /* The format is just "Commit Parent1 Parent2 ...\n" */
790 int len = strlen(buf);
791 struct commit_graft *graft = read_graft_line(buf, len);
792 register_commit_graft(graft, 0);
794 *start_sha1 = graft->sha1;
800 static int lineno_width(int lines)
804 for (width = 1, i = 10; i <= lines + 1; width++)
809 static int find_orig_linenum(struct util_info *u, int lineno)
813 for (i = 0; i < u->num_lines; i++)
814 if (lineno == u->line_map[i])
819 static void emit_meta(struct commit *c, int lno,
820 int sha1_len, int compatibility, int porcelain,
821 int show_name, int show_number, int show_raw_time,
822 int longest_file, int longest_author,
823 int max_digits, int max_orig_digits)
827 struct commit_info ci;
830 lineno = find_orig_linenum(u, lno);
834 struct commit *cc = (lno == 0) ? NULL : blame_lines[lno-1];
836 /* This is the beginning of this group */
838 for (i = lno + 1; i < num_blame_lines; i++)
839 if (blame_lines[i] != c)
841 group_size = i - lno;
844 printf("%s %d %d %d\n", sha1_to_hex(c->object.sha1),
845 lineno, lno + 1, group_size);
847 printf("%s %d %d\n", sha1_to_hex(c->object.sha1),
849 if (!u->meta_given) {
850 get_commit_info(c, &ci, 1);
851 printf("author %s\n", ci.author);
852 printf("author-mail %s\n", ci.author_mail);
853 printf("author-time %lu\n", ci.author_time);
854 printf("author-tz %s\n", ci.author_tz);
855 printf("committer %s\n", ci.committer);
856 printf("committer-mail %s\n", ci.committer_mail);
857 printf("committer-time %lu\n", ci.committer_time);
858 printf("committer-tz %s\n", ci.committer_tz);
860 if (quote_c_style(u->pathname, NULL, NULL, 0))
861 quote_c_style(u->pathname, NULL, stdout, 0);
863 fputs(u->pathname, stdout);
864 printf("\nsummary %s\n", ci.summary);
872 get_commit_info(c, &ci, 0);
873 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
875 printf("\t(%10s\t%10s\t%d)", ci.author,
876 format_time(ci.author_time, ci.author_tz,
882 printf(" %-*.*s", longest_file, longest_file,
885 printf(" %*d", max_orig_digits,
887 printf(" (%-*.*s %10s %*d) ",
888 longest_author, longest_author, ci.author,
889 format_time(ci.author_time, ci.author_tz,
891 max_digits, lno + 1);
895 int main(int argc, const char **argv)
898 struct commit *initial = NULL;
899 unsigned char sha1[20], *sha1_p = NULL;
901 const char *filename = NULL, *commit = NULL;
902 char filename_buf[256];
904 int compatibility = 0;
905 int show_raw_time = 0;
907 struct commit *start_commit;
909 const char *args[10];
912 struct commit_info ci;
914 int max_digits, max_orig_digits;
915 int longest_file, longest_author, longest_file_lines;
920 const char *prefix = setup_git_directory();
921 git_config(git_default_config);
923 for (i = 1; i < argc; i++) {
925 if (!strcmp(argv[i], "-h") ||
926 !strcmp(argv[i], "--help"))
928 if (!strcmp(argv[i], "-l") ||
929 !strcmp(argv[i], "--long")) {
933 if (!strcmp(argv[i], "-c") ||
934 !strcmp(argv[i], "--compatibility")) {
938 if (!strcmp(argv[i], "-t") ||
939 !strcmp(argv[i], "--time")) {
943 if (!strcmp(argv[i], "-S")) {
945 !read_ancestry(argv[i + 1], &sha1_p)) {
952 if (!strcmp(argv[i], "-f") ||
953 !strcmp(argv[i], "--show-name")) {
957 if (!strcmp(argv[i], "-n") ||
958 !strcmp(argv[i], "--show-number")) {
962 if (!strcmp(argv[i], "-p") ||
963 !strcmp(argv[i], "--porcelain")) {
969 if (!strcmp(argv[i], "--")) {
973 if (argv[i][0] == '-')
990 if (commit && sha1_p)
996 sprintf(filename_buf, "%s%s", prefix, filename);
998 strcpy(filename_buf, filename);
999 filename = filename_buf;
1002 if (get_sha1(commit, sha1))
1003 die("get_sha1 failed, commit '%s' not found", commit);
1006 start_commit = lookup_commit_reference(sha1_p);
1007 get_util(start_commit)->pathname = filename;
1008 if (fill_util_info(start_commit)) {
1009 printf("%s not found in %s\n", filename, commit);
1013 init_revisions(&rev, setup_git_directory());
1014 rev.remove_empty_trees = 1;
1016 rev.prune_fn = simplify_commit;
1017 rev.topo_setter = topo_setter;
1018 rev.topo_getter = topo_getter;
1022 commit_list_insert(start_commit, &rev.commits);
1026 diff_tree_setup_paths(args, &rev.pruning);
1027 prepare_revision_walk(&rev);
1028 process_commits(&rev, filename, &initial);
1030 for (i = 0; i < num_blame_lines; i++)
1031 if (!blame_lines[i])
1032 blame_lines[i] = initial;
1034 buf = blame_contents;
1035 max_digits = lineno_width(num_blame_lines);
1039 longest_file_lines = 0;
1040 for (i = 0; i < num_blame_lines; i++) {
1041 struct commit *c = blame_lines[i];
1042 struct util_info *u;
1045 if (!show_name && strcmp(filename, u->pathname))
1047 if (longest_file < strlen(u->pathname))
1048 longest_file = strlen(u->pathname);
1049 if (longest_file_lines < u->num_lines)
1050 longest_file_lines = u->num_lines;
1051 get_commit_info(c, &ci, 0);
1052 if (longest_author < strlen(ci.author))
1053 longest_author = strlen(ci.author);
1056 max_orig_digits = lineno_width(longest_file_lines);
1058 for (i = 0; i < num_blame_lines; i++) {
1059 emit_meta(blame_lines[i], i,
1060 sha1_len, compatibility, porcelain,
1061 show_name, show_number, show_raw_time,
1062 longest_file, longest_author,
1063 max_digits, max_orig_digits);
1065 if (i == num_blame_lines - 1) {
1066 fwrite(buf, blame_len - (buf - blame_contents),
1068 if (blame_contents[blame_len-1] != '\n')
1072 char *next_buf = strchr(buf, '\n') + 1;
1073 fwrite(buf, next_buf - buf, 1, stdout);
1079 printf("num get patch: %d\n", num_get_patch);
1080 printf("num commits: %d\n", num_commits);
1081 printf("patch time: %f\n", patch_time / 1000000.0);
1082 printf("initial: %s\n", sha1_to_hex(initial->object.sha1));