2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
22 static const char blame_usage[] = "[-c] [-l] [--] file [commit]\n"
23 " -c, --compability Use the same output mode as git-annotate (Default: off)\n"
24 " -l, --long Show long commit SHA1 (Default: off)\n"
25 " -h, --help This message";
27 static struct commit **blame_lines;
28 static int num_blame_lines;
29 static char* blame_contents;
34 unsigned char sha1[20]; /* blob sha, not commit! */
44 int off1, len1; // ---
45 int off2, len2; // +++
53 static void get_blob(struct commit *commit);
55 /* Only used for statistics */
56 static int num_get_patch = 0;
57 static int num_commits = 0;
58 static int patch_time = 0;
60 #define TEMPFILE_PATH_LEN 60
61 static struct patch *get_patch(struct commit *commit, struct commit *other)
64 struct util_info *info_c = (struct util_info *)commit->object.util;
65 struct util_info *info_o = (struct util_info *)other->object.util;
66 char tmp_path1[TEMPFILE_PATH_LEN], tmp_path2[TEMPFILE_PATH_LEN];
67 char diff_cmd[TEMPFILE_PATH_LEN*2 + 20];
68 struct timeval tv_start, tv_end;
73 ret = xmalloc(sizeof(struct patch));
80 gettimeofday(&tv_start, NULL);
82 fd = git_mkstemp(tmp_path1, TEMPFILE_PATH_LEN, "git-blame-XXXXXX");
84 die("unable to create temp-file: %s", strerror(errno));
86 if (xwrite(fd, info_c->buf, info_c->size) != info_c->size)
87 die("write failed: %s", strerror(errno));
90 fd = git_mkstemp(tmp_path2, TEMPFILE_PATH_LEN, "git-blame-XXXXXX");
92 die("unable to create temp-file: %s", strerror(errno));
94 if (xwrite(fd, info_o->buf, info_o->size) != info_o->size)
95 die("write failed: %s", strerror(errno));
98 sprintf(diff_cmd, "diff -U 0 %s %s", tmp_path1, tmp_path2);
99 fin = popen(diff_cmd, "r");
101 die("popen failed: %s", strerror(errno));
103 while (fgets(buf, sizeof(buf), fin)) {
107 if (buf[0] != '@' || buf[1] != '@')
111 printf("chunk line: %s", buf);
113 ret->chunks = xrealloc(ret->chunks,
114 sizeof(struct chunk) * ret->num);
115 chunk = &ret->chunks[ret->num - 1];
117 assert(!strncmp(buf, "@@ -", 4));
120 sp = index(start, ' ');
122 if (index(start, ',')) {
124 sscanf(start, "%d,%d", &chunk->off1, &chunk->len1);
127 int ret = sscanf(start, "%d", &chunk->off1);
134 sp = index(start, ' ');
136 if (index(start, ',')) {
138 sscanf(start, "%d,%d", &chunk->off2, &chunk->len2);
141 int ret = sscanf(start, "%d", &chunk->off2);
147 if (chunk->len1 == 0)
149 if (chunk->len2 == 0)
157 assert(chunk->off1 >= 0);
158 assert(chunk->off2 >= 0);
164 gettimeofday(&tv_end, NULL);
165 patch_time += 1000000 * (tv_end.tv_sec - tv_start.tv_sec) +
166 tv_end.tv_usec - tv_start.tv_usec;
172 static void free_patch(struct patch *p)
178 static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
179 int baselen, const char *pathname,
180 unsigned mode, int stage);
182 static unsigned char blob_sha1[20];
183 static int get_blob_sha1(struct tree *t, const char *pathname,
187 const char *pathspec[2];
188 pathspec[0] = pathname;
190 memset(blob_sha1, 0, sizeof(blob_sha1));
191 read_tree_recursive(t, "", 0, 0, pathspec, get_blob_sha1_internal);
193 for (i = 0; i < 20; i++) {
194 if (blob_sha1[i] != 0)
201 memcpy(sha1, blob_sha1, 20);
205 static int get_blob_sha1_internal(unsigned char *sha1, const char *base,
206 int baselen, const char *pathname,
207 unsigned mode, int stage)
210 return READ_TREE_RECURSIVE;
212 memcpy(blob_sha1, sha1, 20);
216 static void get_blob(struct commit *commit)
218 struct util_info *info = commit->object.util;
224 info->buf = read_sha1_file(info->sha1, type, &info->size);
226 assert(!strcmp(type, "blob"));
229 /* For debugging only */
230 static void print_patch(struct patch *p)
233 printf("Num chunks: %d\n", p->num);
234 for (i = 0; i < p->num; i++) {
235 printf("%d,%d %d,%d\n", p->chunks[i].off1, p->chunks[i].len1,
236 p->chunks[i].off2, p->chunks[i].len2);
241 /* For debugging only */
242 static void print_map(struct commit *cmit, struct commit *other)
244 struct util_info *util = cmit->object.util;
245 struct util_info *util2 = other->object.util;
250 util2->num_lines ? util->num_lines : util2->num_lines;
253 for (i = 0; i < max; i++) {
257 if (i < util->num_lines) {
258 num = util->line_map[i];
263 if (i < util2->num_lines) {
264 int num2 = util2->line_map[i];
265 printf("%d\t", num2);
266 if (num != -1 && num2 != num)
276 // p is a patch from commit to other.
277 static void fill_line_map(struct commit *commit, struct commit *other,
280 struct util_info *util = commit->object.util;
281 struct util_info *util2 = other->object.util;
282 int *map = util->line_map;
283 int *map2 = util2->line_map;
291 printf("num lines 1: %d num lines 2: %d\n", util->num_lines,
294 for (i1 = 0, i2 = 0; i1 < util->num_lines; i1++, i2++) {
295 struct chunk *chunk = NULL;
296 if (cur_chunk < p->num)
297 chunk = &p->chunks[cur_chunk];
299 if (chunk && chunk->off1 == i1) {
300 if (DEBUG && i2 != chunk->off2)
301 printf("i2: %d off2: %d\n", i2, chunk->off2);
303 assert(i2 == chunk->off2);
315 if (i2 >= util2->num_lines)
318 if (map[i1] != map2[i2] && map[i1] != -1) {
320 printf("map: i1: %d %d %p i2: %d %d %p\n",
322 i1 != -1 ? blame_lines[map[i1]] : NULL,
324 i2 != -1 ? blame_lines[map2[i2]] : NULL);
325 if (map2[i2] != -1 &&
326 blame_lines[map[i1]] &&
327 !blame_lines[map2[i2]])
331 if (map[i1] == -1 && map2[i2] != -1)
336 printf("l1: %d l2: %d i1: %d i2: %d\n",
337 map[i1], map2[i2], i1, i2);
341 static int map_line(struct commit *commit, int line)
343 struct util_info *info = commit->object.util;
344 assert(line >= 0 && line < info->num_lines);
345 return info->line_map[line];
348 static struct util_info* get_util(struct commit *commit)
350 struct util_info *util = commit->object.util;
355 util = xmalloc(sizeof(struct util_info));
358 util->line_map = NULL;
359 util->num_lines = -1;
360 util->pathname = NULL;
361 commit->object.util = util;
365 static int fill_util_info(struct commit *commit)
367 struct util_info *util = commit->object.util;
370 assert(util->pathname);
372 if (get_blob_sha1(commit->tree, util->pathname, util->sha1))
378 static void alloc_line_map(struct commit *commit)
380 struct util_info *util = commit->object.util;
389 for (i = 0; i < util->size; i++) {
390 if (util->buf[i] == '\n')
393 if(util->buf[util->size - 1] != '\n')
396 util->line_map = xmalloc(sizeof(int) * util->num_lines);
398 for (i = 0; i < util->num_lines; i++)
399 util->line_map[i] = -1;
402 static void init_first_commit(struct commit* commit, const char* filename)
404 struct util_info* util = commit->object.util;
407 util->pathname = filename;
408 if (fill_util_info(commit))
409 die("fill_util_info failed");
411 alloc_line_map(commit);
413 util = commit->object.util;
415 for (i = 0; i < util->num_lines; i++)
416 util->line_map[i] = i;
420 static void process_commits(struct rev_info *rev, const char *path,
421 struct commit** initial)
424 struct util_info* util;
430 struct commit* commit = get_revision(rev);
432 init_first_commit(commit, path);
434 util = commit->object.util;
435 num_blame_lines = util->num_lines;
436 blame_lines = xmalloc(sizeof(struct commit *) * num_blame_lines);
437 blame_contents = util->buf;
438 blame_len = util->size;
440 for (i = 0; i < num_blame_lines; i++)
441 blame_lines[i] = NULL;
443 lines_left = num_blame_lines;
444 blame_p = xmalloc(sizeof(int) * num_blame_lines);
445 new_lines = xmalloc(sizeof(int) * num_blame_lines);
447 struct commit_list *parents;
449 struct util_info *util;
452 printf("\nProcessing commit: %d %s\n", num_commits,
453 sha1_to_hex(commit->object.sha1));
459 memset(blame_p, 0, sizeof(int) * num_blame_lines);
462 for (parents = commit->parents;
463 parents != NULL; parents = parents->next)
469 if (fill_util_info(commit))
472 alloc_line_map(commit);
473 util = commit->object.util;
475 for (parents = commit->parents;
476 parents != NULL; parents = parents->next) {
477 struct commit *parent = parents->item;
480 if (parse_commit(parent) < 0)
481 die("parse_commit error");
484 printf("parent: %s\n",
485 sha1_to_hex(parent->object.sha1));
487 if (fill_util_info(parent)) {
492 patch = get_patch(parent, commit);
493 alloc_line_map(parent);
494 fill_line_map(parent, commit, patch);
496 for (i = 0; i < patch->num; i++) {
498 for (l = 0; l < patch->chunks[i].len2; l++) {
500 map_line(commit, patch->chunks[i].off2 + l);
501 if (mapped_line != -1) {
502 blame_p[mapped_line]++;
503 if (blame_p[mapped_line] == num_parents)
504 new_lines[new_lines_len++] = mapped_line;
512 printf("parents: %d\n", num_parents);
514 for (i = 0; i < new_lines_len; i++) {
515 int mapped_line = new_lines[i];
516 if (blame_lines[mapped_line] == NULL) {
517 blame_lines[mapped_line] = commit;
520 printf("blame: mapped: %d i: %d\n",
524 } while ((commit = get_revision(rev)) != NULL);
528 static int compare_tree_path(struct rev_info* revs,
529 struct commit* c1, struct commit* c2)
531 const char* paths[2];
532 struct util_info* util = c2->object.util;
533 paths[0] = util->pathname;
536 diff_tree_setup_paths(get_pathspec(revs->prefix, paths));
537 return rev_compare_tree(c1->tree, c2->tree);
541 static int same_tree_as_empty_path(struct rev_info *revs, struct tree* t1,
544 const char* paths[2];
548 diff_tree_setup_paths(get_pathspec(revs->prefix, paths));
549 return rev_same_tree_as_empty(t1);
552 static const char* find_rename(struct commit* commit, struct commit* parent)
554 struct util_info* cutil = commit->object.util;
555 struct diff_options diff_opts;
556 const char *paths[1];
560 printf("find_rename commit: %s ",
561 sha1_to_hex(commit->object.sha1));
562 puts(sha1_to_hex(parent->object.sha1));
565 diff_setup(&diff_opts);
566 diff_opts.recursive = 1;
567 diff_opts.detect_rename = DIFF_DETECT_RENAME;
569 diff_tree_setup_paths(paths);
570 if (diff_setup_done(&diff_opts) < 0)
571 die("diff_setup_done failed");
573 diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
575 diffcore_std(&diff_opts);
577 for (i = 0; i < diff_queued_diff.nr; i++) {
578 struct diff_filepair *p = diff_queued_diff.queue[i];
580 if (p->status == 'R' && !strcmp(p->one->path, cutil->pathname)) {
582 printf("rename %s -> %s\n", p->one->path, p->two->path);
590 static void simplify_commit(struct rev_info *revs, struct commit *commit)
592 struct commit_list **pp, *parent;
597 if (!commit->parents) {
598 struct util_info* util = commit->object.util;
599 if (!same_tree_as_empty_path(revs, commit->tree,
601 commit->object.flags |= TREECHANGE;
605 pp = &commit->parents;
606 while ((parent = *pp) != NULL) {
607 struct commit *p = parent->item;
609 if (p->object.flags & UNINTERESTING) {
615 switch (compare_tree_path(revs, p, commit)) {
618 commit->parents = parent;
619 get_util(p)->pathname = get_util(commit)->pathname;
625 struct util_info* util = commit->object.util;
626 if (revs->remove_empty_trees &&
627 same_tree_as_empty_path(revs, p->tree,
629 const char* new_name = find_rename(commit, p);
631 struct util_info* putil = get_util(p);
632 if (!putil->pathname)
633 putil->pathname = strdup(new_name);
642 case REV_TREE_DIFFERENT:
644 if (!get_util(p)->pathname)
645 get_util(p)->pathname =
646 get_util(commit)->pathname;
649 die("bad tree compare for commit %s",
650 sha1_to_hex(commit->object.sha1));
652 commit->object.flags |= TREECHANGE;
660 unsigned long author_time;
664 static void get_commit_info(struct commit* commit, struct commit_info* ret)
668 static char author_buf[1024];
670 tmp = strstr(commit->buffer, "\nauthor ") + 8;
671 len = index(tmp, '\n') - tmp;
672 ret->author = author_buf;
673 memcpy(ret->author, tmp, len);
680 ret->author_tz = tmp+1;
685 ret->author_time = strtoul(tmp, NULL, 10);
690 ret->author_mail = tmp + 1;
695 static const char* format_time(unsigned long time, const char* tz_str)
697 static char time_buf[128];
703 minutes = tz < 0 ? -tz : tz;
704 minutes = (minutes / 100)*60 + (minutes % 100);
705 minutes = tz < 0 ? -minutes : minutes;
706 t = time + minutes * 60;
709 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
710 strcat(time_buf, tz_str);
714 static void topo_setter(struct commit* c, void* data)
716 struct util_info* util = c->object.util;
717 util->topo_data = data;
720 static void* topo_getter(struct commit* c)
722 struct util_info* util = c->object.util;
723 return util->topo_data;
726 int main(int argc, const char **argv)
729 struct commit *initial = NULL;
730 unsigned char sha1[20];
732 const char *filename = NULL, *commit = NULL;
733 char filename_buf[256];
737 struct commit* start_commit;
739 const char* args[10];
742 struct commit_info ci;
746 const char* prefix = setup_git_directory();
748 for(i = 1; i < argc; i++) {
750 if(!strcmp(argv[i], "-h") ||
751 !strcmp(argv[i], "--help"))
753 else if(!strcmp(argv[i], "-l") ||
754 !strcmp(argv[i], "--long")) {
757 } else if(!strcmp(argv[i], "-c") ||
758 !strcmp(argv[i], "--compability")) {
761 } else if(!strcmp(argv[i], "--")) {
764 } else if(argv[i][0] == '-')
786 sprintf(filename_buf, "%s%s", prefix, filename);
788 strcpy(filename_buf, filename);
789 filename = filename_buf;
791 if (get_sha1(commit, sha1))
792 die("get_sha1 failed, commit '%s' not found", commit);
793 start_commit = lookup_commit_reference(sha1);
794 get_util(start_commit)->pathname = filename;
795 if (fill_util_info(start_commit)) {
796 printf("%s not found in %s\n", filename, commit);
801 init_revisions(&rev);
802 rev.remove_empty_trees = 1;
804 rev.prune_fn = simplify_commit;
805 rev.topo_setter = topo_setter;
806 rev.topo_getter = topo_getter;
809 commit_list_insert(start_commit, &rev.commits);
813 diff_tree_setup_paths(args);
814 prepare_revision_walk(&rev);
815 process_commits(&rev, filename, &initial);
817 buf = blame_contents;
818 for (max_digits = 1, i = 10; i <= num_blame_lines + 1; max_digits++)
821 for (i = 0; i < num_blame_lines; i++) {
822 struct commit *c = blame_lines[i];
829 get_commit_info(c, &ci);
830 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
832 printf("\t(%10s\t%10s\t%d)", ci.author,
833 format_time(ci.author_time, ci.author_tz), i+1);
835 printf(" %s (%-15.15s %10s %*d) ", u->pathname,
836 ci.author, format_time(ci.author_time,
840 if(i == num_blame_lines - 1) {
841 fwrite(buf, blame_len - (buf - blame_contents),
843 if(blame_contents[blame_len-1] != '\n')
846 char* next_buf = index(buf, '\n') + 1;
847 fwrite(buf, next_buf - buf, 1, stdout);
853 printf("num get patch: %d\n", num_get_patch);
854 printf("num commits: %d\n", num_commits);
855 printf("patch time: %f\n", patch_time / 1000000.0);
856 printf("initial: %s\n", sha1_to_hex(initial->object.sha1));