2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
19 #include "xdiff-interface.h"
24 static const char blame_usage[] =
25 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-S <revs-file>] [--] file [commit]\n"
26 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
27 " -l, --long Show long commit SHA1 (Default: off)\n"
28 " -t, --time Show raw timestamp (Default: off)\n"
29 " -f, --show-name Show original filename (Default: auto)\n"
30 " -n, --show-number Show original linenumber (Default: off)\n"
31 " -p, --porcelain Show in a format designed for machine consumption\n"
32 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"
33 " -h, --help This message";
35 static struct commit **blame_lines;
36 static int num_blame_lines;
37 static char *blame_contents;
42 unsigned char sha1[20]; /* blob sha, not commit! */
47 unsigned meta_given:1;
53 int off1, len1; /* --- */
54 int off2, len2; /* +++ */
62 static void get_blob(struct commit *commit);
64 /* Only used for statistics */
65 static int num_get_patch;
66 static int num_commits;
67 static int patch_time;
69 struct blame_diff_state {
70 struct xdiff_emit_state xm;
74 static void process_u0_diff(void *state_, char *line, unsigned long len)
76 struct blame_diff_state *state = state_;
79 if (len < 4 || line[0] != '@' || line[1] != '@')
83 printf("chunk line: %.*s", (int)len, line);
85 state->ret->chunks = xrealloc(state->ret->chunks,
86 sizeof(struct chunk) * state->ret->num);
87 chunk = &state->ret->chunks[state->ret->num - 1];
89 assert(!strncmp(line, "@@ -", 4));
91 if (parse_hunk_header(line, len,
92 &chunk->off1, &chunk->len1,
93 &chunk->off2, &chunk->len2)) {
100 if (chunk->len2 == 0)
108 assert(chunk->off1 >= 0);
109 assert(chunk->off2 >= 0);
112 static struct patch *get_patch(struct commit *commit, struct commit *other)
114 struct blame_diff_state state;
117 mmfile_t file_c, file_o;
119 struct util_info *info_c = (struct util_info *)commit->util;
120 struct util_info *info_o = (struct util_info *)other->util;
121 struct timeval tv_start, tv_end;
124 file_c.ptr = info_c->buf;
125 file_c.size = info_c->size;
128 file_o.ptr = info_o->buf;
129 file_o.size = info_o->size;
131 gettimeofday(&tv_start, NULL);
133 xpp.flags = XDF_NEED_MINIMAL;
136 ecb.outf = xdiff_outf;
138 memset(&state, 0, sizeof(state));
139 state.xm.consume = process_u0_diff;
140 state.ret = xmalloc(sizeof(struct patch));
141 state.ret->chunks = NULL;
144 xdl_diff(&file_c, &file_o, &xpp, &xecfg, &ecb);
146 gettimeofday(&tv_end, NULL);
147 patch_time += 1000000 * (tv_end.tv_sec - tv_start.tv_sec) +
148 tv_end.tv_usec - tv_start.tv_usec;
154 static void free_patch(struct patch *p)
160 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
161 int baselen, const char *pathname,
162 unsigned mode, int stage);
164 static unsigned char blob_sha1[20];
165 static const char *blame_file;
166 static int get_blob_sha1(struct tree *t, const char *pathname,
169 const char *pathspec[2];
170 blame_file = pathname;
171 pathspec[0] = pathname;
174 read_tree_recursive(t, "", 0, 0, pathspec, get_blob_sha1_internal);
176 if (is_null_sha1(blob_sha1))
179 hashcpy(sha1, blob_sha1);
183 static int get_blob_sha1_internal(const unsigned char *sha1, const char *base,
184 int baselen, const char *pathname,
185 unsigned mode, int stage)
188 return READ_TREE_RECURSIVE;
190 if (strncmp(blame_file, base, baselen) ||
191 strcmp(blame_file + baselen, pathname))
194 hashcpy(blob_sha1, sha1);
198 static void get_blob(struct commit *commit)
200 struct util_info *info = commit->util;
206 info->buf = read_sha1_file(info->sha1, type, &info->size);
208 assert(!strcmp(type, blob_type));
211 /* For debugging only */
212 static void print_patch(struct patch *p)
215 printf("Num chunks: %d\n", p->num);
216 for (i = 0; i < p->num; i++) {
217 printf("%d,%d %d,%d\n", p->chunks[i].off1, p->chunks[i].len1,
218 p->chunks[i].off2, p->chunks[i].len2);
223 /* For debugging only */
224 static void print_map(struct commit *cmit, struct commit *other)
226 struct util_info *util = cmit->util;
227 struct util_info *util2 = other->util;
232 util2->num_lines ? util->num_lines : util2->num_lines;
235 for (i = 0; i < max; i++) {
239 if (i < util->num_lines) {
240 num = util->line_map[i];
246 if (i < util2->num_lines) {
247 int num2 = util2->line_map[i];
248 printf("%d\t", num2);
249 if (num != -1 && num2 != num)
260 /* p is a patch from commit to other. */
261 static void fill_line_map(struct commit *commit, struct commit *other,
264 struct util_info *util = commit->util;
265 struct util_info *util2 = other->util;
266 int *map = util->line_map;
267 int *map2 = util2->line_map;
274 printf("num lines 1: %d num lines 2: %d\n", util->num_lines,
278 for (i1 = 0, i2 = 0; i1 < util->num_lines; i1++, i2++) {
279 struct chunk *chunk = NULL;
280 if (cur_chunk < p->num)
281 chunk = &p->chunks[cur_chunk];
283 if (chunk && chunk->off1 == i1) {
284 if (DEBUG && i2 != chunk->off2)
285 printf("i2: %d off2: %d\n", i2, chunk->off2);
287 assert(i2 == chunk->off2);
300 if (i2 >= util2->num_lines)
303 if (map[i1] != map2[i2] && map[i1] != -1) {
305 printf("map: i1: %d %d %p i2: %d %d %p\n",
307 (void *) (i1 != -1 ? blame_lines[map[i1]] : NULL),
309 (void *) (i2 != -1 ? blame_lines[map2[i2]] : NULL));
310 if (map2[i2] != -1 &&
311 blame_lines[map[i1]] &&
312 !blame_lines[map2[i2]])
316 if (map[i1] == -1 && map2[i2] != -1)
321 printf("l1: %d l2: %d i1: %d i2: %d\n",
322 map[i1], map2[i2], i1, i2);
326 static int map_line(struct commit *commit, int line)
328 struct util_info *info = commit->util;
329 assert(line >= 0 && line < info->num_lines);
330 return info->line_map[line];
333 static struct util_info *get_util(struct commit *commit)
335 struct util_info *util = commit->util;
340 util = xcalloc(1, sizeof(struct util_info));
341 util->num_lines = -1;
346 static int fill_util_info(struct commit *commit)
348 struct util_info *util = commit->util;
351 assert(util->pathname);
353 return !!get_blob_sha1(commit->tree, util->pathname, util->sha1);
356 static void alloc_line_map(struct commit *commit)
358 struct util_info *util = commit->util;
367 for (i = 0; i < util->size; i++) {
368 if (util->buf[i] == '\n')
371 if (util->buf[util->size - 1] != '\n')
374 util->line_map = xmalloc(sizeof(int) * util->num_lines);
376 for (i = 0; i < util->num_lines; i++)
377 util->line_map[i] = -1;
380 static void init_first_commit(struct commit *commit, const char *filename)
382 struct util_info *util = commit->util;
385 util->pathname = filename;
386 if (fill_util_info(commit))
387 die("fill_util_info failed");
389 alloc_line_map(commit);
393 for (i = 0; i < util->num_lines; i++)
394 util->line_map[i] = i;
397 static void process_commits(struct rev_info *rev, const char *path,
398 struct commit **initial)
401 struct util_info *util;
407 struct commit *commit = get_revision(rev);
409 init_first_commit(commit, path);
412 num_blame_lines = util->num_lines;
413 blame_lines = xmalloc(sizeof(struct commit *) * num_blame_lines);
414 blame_contents = util->buf;
415 blame_len = util->size;
417 for (i = 0; i < num_blame_lines; i++)
418 blame_lines[i] = NULL;
420 lines_left = num_blame_lines;
421 blame_p = xmalloc(sizeof(int) * num_blame_lines);
422 new_lines = xmalloc(sizeof(int) * num_blame_lines);
424 struct commit_list *parents;
426 struct util_info *util;
429 printf("\nProcessing commit: %d %s\n", num_commits,
430 sha1_to_hex(commit->object.sha1));
436 memset(blame_p, 0, sizeof(int) * num_blame_lines);
439 for (parents = commit->parents;
440 parents != NULL; parents = parents->next)
443 if (num_parents == 0)
446 if (fill_util_info(commit))
449 alloc_line_map(commit);
452 for (parents = commit->parents;
453 parents != NULL; parents = parents->next) {
454 struct commit *parent = parents->item;
457 if (parse_commit(parent) < 0)
458 die("parse_commit error");
461 printf("parent: %s\n",
462 sha1_to_hex(parent->object.sha1));
464 if (fill_util_info(parent)) {
469 patch = get_patch(parent, commit);
470 alloc_line_map(parent);
471 fill_line_map(parent, commit, patch);
473 for (i = 0; i < patch->num; i++) {
475 for (l = 0; l < patch->chunks[i].len2; l++) {
477 map_line(commit, patch->chunks[i].off2 + l);
478 if (mapped_line != -1) {
479 blame_p[mapped_line]++;
480 if (blame_p[mapped_line] == num_parents)
481 new_lines[new_lines_len++] = mapped_line;
489 printf("parents: %d\n", num_parents);
491 for (i = 0; i < new_lines_len; i++) {
492 int mapped_line = new_lines[i];
493 if (blame_lines[mapped_line] == NULL) {
494 blame_lines[mapped_line] = commit;
497 printf("blame: mapped: %d i: %d\n",
501 } while ((commit = get_revision(rev)) != NULL);
504 static int compare_tree_path(struct rev_info *revs,
505 struct commit *c1, struct commit *c2)
508 const char *paths[2];
509 struct util_info *util = c2->util;
510 paths[0] = util->pathname;
513 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
515 ret = rev_compare_tree(revs, c1->tree, c2->tree);
516 diff_tree_release_paths(&revs->pruning);
520 static int same_tree_as_empty_path(struct rev_info *revs, struct tree *t1,
524 const char *paths[2];
528 diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
530 ret = rev_same_tree_as_empty(revs, t1);
531 diff_tree_release_paths(&revs->pruning);
535 static const char *find_rename(struct commit *commit, struct commit *parent)
537 struct util_info *cutil = commit->util;
538 struct diff_options diff_opts;
539 const char *paths[1];
543 printf("find_rename commit: %s ",
544 sha1_to_hex(commit->object.sha1));
545 puts(sha1_to_hex(parent->object.sha1));
548 diff_setup(&diff_opts);
549 diff_opts.recursive = 1;
550 diff_opts.detect_rename = DIFF_DETECT_RENAME;
552 diff_tree_setup_paths(paths, &diff_opts);
553 if (diff_setup_done(&diff_opts) < 0)
554 die("diff_setup_done failed");
556 diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
558 diffcore_std(&diff_opts);
560 for (i = 0; i < diff_queued_diff.nr; i++) {
561 struct diff_filepair *p = diff_queued_diff.queue[i];
563 if (p->status == 'R' &&
564 !strcmp(p->one->path, cutil->pathname)) {
566 printf("rename %s -> %s\n",
567 p->one->path, p->two->path);
575 static void simplify_commit(struct rev_info *revs, struct commit *commit)
577 struct commit_list **pp, *parent;
582 if (!commit->parents) {
583 struct util_info *util = commit->util;
584 if (!same_tree_as_empty_path(revs, commit->tree,
586 commit->object.flags |= TREECHANGE;
590 pp = &commit->parents;
591 while ((parent = *pp) != NULL) {
592 struct commit *p = parent->item;
594 if (p->object.flags & UNINTERESTING) {
600 switch (compare_tree_path(revs, p, commit)) {
603 commit->parents = parent;
604 get_util(p)->pathname = get_util(commit)->pathname;
609 struct util_info *util = commit->util;
610 if (revs->remove_empty_trees &&
611 same_tree_as_empty_path(revs, p->tree,
613 const char *new_name = find_rename(commit, p);
615 struct util_info *putil = get_util(p);
616 if (!putil->pathname)
617 putil->pathname = xstrdup(new_name);
627 case REV_TREE_DIFFERENT:
629 if (!get_util(p)->pathname)
630 get_util(p)->pathname =
631 get_util(commit)->pathname;
634 die("bad tree compare for commit %s",
635 sha1_to_hex(commit->object.sha1));
637 commit->object.flags |= TREECHANGE;
644 unsigned long author_time;
647 /* filled only when asked for details */
649 char *committer_mail;
650 unsigned long committer_time;
656 static void get_ac_line(const char *inbuf, const char *what,
657 int bufsz, char *person, char **mail,
658 unsigned long *time, char **tz)
663 tmp = strstr(inbuf, what);
667 endp = strchr(tmp, '\n');
675 person = *mail = *tz = "(unknown)";
679 memcpy(person, tmp, len);
691 *time = strtoul(tmp, NULL, 10);
700 static void get_commit_info(struct commit *commit, struct commit_info *ret, int detailed)
704 static char author_buf[1024];
705 static char committer_buf[1024];
706 static char summary_buf[1024];
708 ret->author = author_buf;
709 get_ac_line(commit->buffer, "\nauthor ",
710 sizeof(author_buf), author_buf, &ret->author_mail,
711 &ret->author_time, &ret->author_tz);
716 ret->committer = committer_buf;
717 get_ac_line(commit->buffer, "\ncommitter ",
718 sizeof(committer_buf), committer_buf, &ret->committer_mail,
719 &ret->committer_time, &ret->committer_tz);
721 ret->summary = summary_buf;
722 tmp = strstr(commit->buffer, "\n\n");
725 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
729 endp = strchr(tmp, '\n');
733 if (len >= sizeof(summary_buf))
735 memcpy(summary_buf, tmp, len);
736 summary_buf[len] = 0;
739 static const char *format_time(unsigned long time, const char *tz_str,
742 static char time_buf[128];
748 sprintf(time_buf, "%lu %s", time, tz_str);
753 minutes = tz < 0 ? -tz : tz;
754 minutes = (minutes / 100)*60 + (minutes % 100);
755 minutes = tz < 0 ? -minutes : minutes;
756 t = time + minutes * 60;
759 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
760 strcat(time_buf, tz_str);
764 static void topo_setter(struct commit *c, void *data)
766 struct util_info *util = c->util;
767 util->topo_data = data;
770 static void *topo_getter(struct commit *c)
772 struct util_info *util = c->util;
773 return util->topo_data;
776 static int read_ancestry(const char *graft_file,
777 unsigned char **start_sha1)
779 FILE *fp = fopen(graft_file, "r");
783 while (fgets(buf, sizeof(buf), fp)) {
784 /* The format is just "Commit Parent1 Parent2 ...\n" */
785 int len = strlen(buf);
786 struct commit_graft *graft = read_graft_line(buf, len);
787 register_commit_graft(graft, 0);
789 *start_sha1 = graft->sha1;
795 static int lineno_width(int lines)
799 for (width = 1, i = 10; i <= lines + 1; width++)
804 static int find_orig_linenum(struct util_info *u, int lineno)
808 for (i = 0; i < u->num_lines; i++)
809 if (lineno == u->line_map[i])
814 static void emit_meta(struct commit *c, int lno,
815 int sha1_len, int compatibility, int porcelain,
816 int show_name, int show_number, int show_raw_time,
817 int longest_file, int longest_author,
818 int max_digits, int max_orig_digits)
822 struct commit_info ci;
825 lineno = find_orig_linenum(u, lno);
829 struct commit *cc = (lno == 0) ? NULL : blame_lines[lno-1];
831 /* This is the beginning of this group */
833 for (i = lno + 1; i < num_blame_lines; i++)
834 if (blame_lines[i] != c)
836 group_size = i - lno;
839 printf("%s %d %d %d\n", sha1_to_hex(c->object.sha1),
840 lineno, lno + 1, group_size);
842 printf("%s %d %d\n", sha1_to_hex(c->object.sha1),
844 if (!u->meta_given) {
845 get_commit_info(c, &ci, 1);
846 printf("author %s\n", ci.author);
847 printf("author-mail %s\n", ci.author_mail);
848 printf("author-time %lu\n", ci.author_time);
849 printf("author-tz %s\n", ci.author_tz);
850 printf("committer %s\n", ci.committer);
851 printf("committer-mail %s\n", ci.committer_mail);
852 printf("committer-time %lu\n", ci.committer_time);
853 printf("committer-tz %s\n", ci.committer_tz);
855 if (quote_c_style(u->pathname, NULL, NULL, 0))
856 quote_c_style(u->pathname, NULL, stdout, 0);
858 fputs(u->pathname, stdout);
859 printf("\nsummary %s\n", ci.summary);
867 get_commit_info(c, &ci, 0);
868 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
870 printf("\t(%10s\t%10s\t%d)", ci.author,
871 format_time(ci.author_time, ci.author_tz,
877 printf(" %-*.*s", longest_file, longest_file,
880 printf(" %*d", max_orig_digits,
882 printf(" (%-*.*s %10s %*d) ",
883 longest_author, longest_author, ci.author,
884 format_time(ci.author_time, ci.author_tz,
886 max_digits, lno + 1);
890 int main(int argc, const char **argv)
893 struct commit *initial = NULL;
894 unsigned char sha1[20], *sha1_p = NULL;
896 const char *filename = NULL, *commit = NULL;
897 char filename_buf[256];
899 int compatibility = 0;
900 int show_raw_time = 0;
902 struct commit *start_commit;
904 const char *args[10];
907 struct commit_info ci;
909 int max_digits, max_orig_digits;
910 int longest_file, longest_author, longest_file_lines;
915 const char *prefix = setup_git_directory();
916 git_config(git_default_config);
918 for (i = 1; i < argc; i++) {
920 if (!strcmp(argv[i], "-h") ||
921 !strcmp(argv[i], "--help"))
923 if (!strcmp(argv[i], "-l") ||
924 !strcmp(argv[i], "--long")) {
928 if (!strcmp(argv[i], "-c") ||
929 !strcmp(argv[i], "--compatibility")) {
933 if (!strcmp(argv[i], "-t") ||
934 !strcmp(argv[i], "--time")) {
938 if (!strcmp(argv[i], "-S")) {
940 !read_ancestry(argv[i + 1], &sha1_p)) {
947 if (!strcmp(argv[i], "-f") ||
948 !strcmp(argv[i], "--show-name")) {
952 if (!strcmp(argv[i], "-n") ||
953 !strcmp(argv[i], "--show-number")) {
957 if (!strcmp(argv[i], "-p") ||
958 !strcmp(argv[i], "--porcelain")) {
964 if (!strcmp(argv[i], "--")) {
968 if (argv[i][0] == '-')
985 if (commit && sha1_p)
991 sprintf(filename_buf, "%s%s", prefix, filename);
993 strcpy(filename_buf, filename);
994 filename = filename_buf;
997 if (get_sha1(commit, sha1))
998 die("get_sha1 failed, commit '%s' not found", commit);
1001 start_commit = lookup_commit_reference(sha1_p);
1002 get_util(start_commit)->pathname = filename;
1003 if (fill_util_info(start_commit)) {
1004 printf("%s not found in %s\n", filename, commit);
1008 init_revisions(&rev, setup_git_directory());
1009 rev.remove_empty_trees = 1;
1011 rev.prune_fn = simplify_commit;
1012 rev.topo_setter = topo_setter;
1013 rev.topo_getter = topo_getter;
1017 commit_list_insert(start_commit, &rev.commits);
1021 diff_tree_setup_paths(args, &rev.pruning);
1022 prepare_revision_walk(&rev);
1023 process_commits(&rev, filename, &initial);
1025 for (i = 0; i < num_blame_lines; i++)
1026 if (!blame_lines[i])
1027 blame_lines[i] = initial;
1029 buf = blame_contents;
1030 max_digits = lineno_width(num_blame_lines);
1034 longest_file_lines = 0;
1035 for (i = 0; i < num_blame_lines; i++) {
1036 struct commit *c = blame_lines[i];
1037 struct util_info *u;
1040 if (!show_name && strcmp(filename, u->pathname))
1042 if (longest_file < strlen(u->pathname))
1043 longest_file = strlen(u->pathname);
1044 if (longest_file_lines < u->num_lines)
1045 longest_file_lines = u->num_lines;
1046 get_commit_info(c, &ci, 0);
1047 if (longest_author < strlen(ci.author))
1048 longest_author = strlen(ci.author);
1051 max_orig_digits = lineno_width(longest_file_lines);
1053 for (i = 0; i < num_blame_lines; i++) {
1054 emit_meta(blame_lines[i], i,
1055 sha1_len, compatibility, porcelain,
1056 show_name, show_number, show_raw_time,
1057 longest_file, longest_author,
1058 max_digits, max_orig_digits);
1060 if (i == num_blame_lines - 1) {
1061 fwrite(buf, blame_len - (buf - blame_contents),
1063 if (blame_contents[blame_len-1] != '\n')
1067 char *next_buf = strchr(buf, '\n') + 1;
1068 fwrite(buf, next_buf - buf, 1, stdout);
1074 printf("num get patch: %d\n", num_get_patch);
1075 printf("num commits: %d\n", num_commits);
1076 printf("patch time: %f\n", patch_time / 1000000.0);
1077 printf("initial: %s\n", sha1_to_hex(initial->object.sha1));