2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
19 #include "xdiff-interface.h"
25 static const char blame_usage
[] = "git-blame [-c] [-l] [-t] [-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 " -S, --revs-file Use revisions from revs-file instead of calling git-rev-list\n"
30 " -h, --help This message";
32 static struct commit
**blame_lines
;
33 static int num_blame_lines
;
34 static char* blame_contents
;
39 unsigned char sha1
[20]; /* blob sha, not commit! */
49 int off1
, len1
; /* --- */
50 int off2
, len2
; /* +++ */
58 static void get_blob(struct commit
*commit
);
60 /* Only used for statistics */
61 static int num_get_patch
;
62 static int num_commits
;
63 static int patch_time
;
65 struct blame_diff_state
{
66 struct xdiff_emit_state xm
;
70 static void process_u0_diff(void *state_
, char *line
, unsigned long len
)
72 struct blame_diff_state
*state
= state_
;
75 if (len
< 4 || line
[0] != '@' || line
[1] != '@')
79 printf("chunk line: %.*s", (int)len
, line
);
81 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
82 sizeof(struct chunk
) * state
->ret
->num
);
83 chunk
= &state
->ret
->chunks
[state
->ret
->num
- 1];
85 assert(!strncmp(line
, "@@ -", 4));
87 if (parse_hunk_header(line
, len
,
88 &chunk
->off1
, &chunk
->len1
,
89 &chunk
->off2
, &chunk
->len2
)) {
104 assert(chunk
->off1
>= 0);
105 assert(chunk
->off2
>= 0);
108 static struct patch
*get_patch(struct commit
*commit
, struct commit
*other
)
110 struct blame_diff_state state
;
113 mmfile_t file_c
, file_o
;
115 struct util_info
*info_c
= (struct util_info
*)commit
->util
;
116 struct util_info
*info_o
= (struct util_info
*)other
->util
;
117 struct timeval tv_start
, tv_end
;
120 file_c
.ptr
= info_c
->buf
;
121 file_c
.size
= info_c
->size
;
124 file_o
.ptr
= info_o
->buf
;
125 file_o
.size
= info_o
->size
;
127 gettimeofday(&tv_start
, NULL
);
129 xpp
.flags
= XDF_NEED_MINIMAL
;
132 ecb
.outf
= xdiff_outf
;
134 memset(&state
, 0, sizeof(state
));
135 state
.xm
.consume
= process_u0_diff
;
136 state
.ret
= xmalloc(sizeof(struct patch
));
137 state
.ret
->chunks
= NULL
;
140 xdl_diff(&file_c
, &file_o
, &xpp
, &xecfg
, &ecb
);
142 gettimeofday(&tv_end
, NULL
);
143 patch_time
+= 1000000 * (tv_end
.tv_sec
- tv_start
.tv_sec
) +
144 tv_end
.tv_usec
- tv_start
.tv_usec
;
150 static void free_patch(struct patch
*p
)
156 static int get_blob_sha1_internal(const unsigned char *sha1
, const char *base
,
157 int baselen
, const char *pathname
,
158 unsigned mode
, int stage
);
160 static unsigned char blob_sha1
[20];
161 static const char* blame_file
;
162 static int get_blob_sha1(struct tree
*t
, const char *pathname
,
166 const char *pathspec
[2];
167 blame_file
= pathname
;
168 pathspec
[0] = pathname
;
171 read_tree_recursive(t
, "", 0, 0, pathspec
, get_blob_sha1_internal
);
173 for (i
= 0; i
< 20; i
++) {
174 if (blob_sha1
[i
] != 0)
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
];
250 if (i
< util2
->num_lines
) {
251 int num2
= util2
->line_map
[i
];
252 printf("%d\t", num2
);
253 if (num
!= -1 && num2
!= num
)
263 /* p is a patch from commit to other. */
264 static void fill_line_map(struct commit
*commit
, struct commit
*other
,
267 struct util_info
*util
= commit
->util
;
268 struct util_info
*util2
= other
->util
;
269 int *map
= util
->line_map
;
270 int *map2
= util2
->line_map
;
278 printf("num lines 1: %d num lines 2: %d\n", util
->num_lines
,
281 for (i1
= 0, i2
= 0; i1
< util
->num_lines
; i1
++, i2
++) {
282 struct chunk
*chunk
= NULL
;
283 if (cur_chunk
< p
->num
)
284 chunk
= &p
->chunks
[cur_chunk
];
286 if (chunk
&& chunk
->off1
== i1
) {
287 if (DEBUG
&& i2
!= chunk
->off2
)
288 printf("i2: %d off2: %d\n", i2
, chunk
->off2
);
290 assert(i2
== chunk
->off2
);
302 if (i2
>= util2
->num_lines
)
305 if (map
[i1
] != map2
[i2
] && map
[i1
] != -1) {
307 printf("map: i1: %d %d %p i2: %d %d %p\n",
309 (void *) (i1
!= -1 ? blame_lines
[map
[i1
]] : NULL
),
311 (void *) (i2
!= -1 ? blame_lines
[map2
[i2
]] : NULL
));
312 if (map2
[i2
] != -1 &&
313 blame_lines
[map
[i1
]] &&
314 !blame_lines
[map2
[i2
]])
318 if (map
[i1
] == -1 && map2
[i2
] != -1)
323 printf("l1: %d l2: %d i1: %d i2: %d\n",
324 map
[i1
], map2
[i2
], i1
, i2
);
328 static int map_line(struct commit
*commit
, int line
)
330 struct util_info
*info
= commit
->util
;
331 assert(line
>= 0 && line
< info
->num_lines
);
332 return info
->line_map
[line
];
335 static struct util_info
* get_util(struct commit
*commit
)
337 struct util_info
*util
= commit
->util
;
342 util
= xmalloc(sizeof(struct util_info
));
345 util
->line_map
= NULL
;
346 util
->num_lines
= -1;
347 util
->pathname
= NULL
;
352 static int fill_util_info(struct commit
*commit
)
354 struct util_info
*util
= commit
->util
;
357 assert(util
->pathname
);
359 return !!get_blob_sha1(commit
->tree
, util
->pathname
, util
->sha1
);
362 static void alloc_line_map(struct commit
*commit
)
364 struct util_info
*util
= commit
->util
;
373 for (i
= 0; i
< util
->size
; i
++) {
374 if (util
->buf
[i
] == '\n')
377 if(util
->buf
[util
->size
- 1] != '\n')
380 util
->line_map
= xmalloc(sizeof(int) * util
->num_lines
);
382 for (i
= 0; i
< util
->num_lines
; i
++)
383 util
->line_map
[i
] = -1;
386 static void init_first_commit(struct commit
* commit
, const char* filename
)
388 struct util_info
* util
= commit
->util
;
391 util
->pathname
= filename
;
392 if (fill_util_info(commit
))
393 die("fill_util_info failed");
395 alloc_line_map(commit
);
399 for (i
= 0; i
< util
->num_lines
; i
++)
400 util
->line_map
[i
] = i
;
404 static void process_commits(struct rev_info
*rev
, const char *path
,
405 struct commit
** initial
)
408 struct util_info
* util
;
414 struct commit
* commit
= get_revision(rev
);
416 init_first_commit(commit
, path
);
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
);
431 struct commit_list
*parents
;
433 struct util_info
*util
;
436 printf("\nProcessing commit: %d %s\n", num_commits
,
437 sha1_to_hex(commit
->object
.sha1
));
443 memset(blame_p
, 0, sizeof(int) * num_blame_lines
);
446 for (parents
= commit
->parents
;
447 parents
!= NULL
; parents
= parents
->next
)
453 if (fill_util_info(commit
))
456 alloc_line_map(commit
);
459 for (parents
= commit
->parents
;
460 parents
!= NULL
; parents
= parents
->next
) {
461 struct commit
*parent
= parents
->item
;
464 if (parse_commit(parent
) < 0)
465 die("parse_commit error");
468 printf("parent: %s\n",
469 sha1_to_hex(parent
->object
.sha1
));
471 if (fill_util_info(parent
)) {
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
++) {
482 for (l
= 0; l
< patch
->chunks
[i
].len2
; l
++) {
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
;
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
;
504 printf("blame: mapped: %d i: %d\n",
508 } while ((commit
= get_revision(rev
)) != NULL
);
512 static int compare_tree_path(struct rev_info
* revs
,
513 struct commit
* c1
, struct commit
* c2
)
516 const char* paths
[2];
517 struct util_info
* util
= c2
->util
;
518 paths
[0] = util
->pathname
;
521 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
523 ret
= rev_compare_tree(revs
, c1
->tree
, c2
->tree
);
524 diff_tree_release_paths(&revs
->pruning
);
529 static int same_tree_as_empty_path(struct rev_info
*revs
, struct tree
* t1
,
533 const char* paths
[2];
537 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
539 ret
= rev_same_tree_as_empty(revs
, t1
);
540 diff_tree_release_paths(&revs
->pruning
);
544 static const char* find_rename(struct commit
* commit
, struct commit
* parent
)
546 struct util_info
* cutil
= commit
->util
;
547 struct diff_options diff_opts
;
548 const char *paths
[1];
552 printf("find_rename commit: %s ",
553 sha1_to_hex(commit
->object
.sha1
));
554 puts(sha1_to_hex(parent
->object
.sha1
));
557 diff_setup(&diff_opts
);
558 diff_opts
.recursive
= 1;
559 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
561 diff_tree_setup_paths(paths
, &diff_opts
);
562 if (diff_setup_done(&diff_opts
) < 0)
563 die("diff_setup_done failed");
565 diff_tree_sha1(commit
->tree
->object
.sha1
, parent
->tree
->object
.sha1
,
567 diffcore_std(&diff_opts
);
569 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
570 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
572 if (p
->status
== 'R' && !strcmp(p
->one
->path
, cutil
->pathname
)) {
574 printf("rename %s -> %s\n", p
->one
->path
, p
->two
->path
);
582 static void simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
584 struct commit_list
**pp
, *parent
;
589 if (!commit
->parents
) {
590 struct util_info
* util
= commit
->util
;
591 if (!same_tree_as_empty_path(revs
, commit
->tree
,
593 commit
->object
.flags
|= TREECHANGE
;
597 pp
= &commit
->parents
;
598 while ((parent
= *pp
) != NULL
) {
599 struct commit
*p
= parent
->item
;
601 if (p
->object
.flags
& UNINTERESTING
) {
607 switch (compare_tree_path(revs
, p
, commit
)) {
610 commit
->parents
= parent
;
611 get_util(p
)->pathname
= get_util(commit
)->pathname
;
617 struct util_info
* util
= commit
->util
;
618 if (revs
->remove_empty_trees
&&
619 same_tree_as_empty_path(revs
, p
->tree
,
621 const char* new_name
= find_rename(commit
, p
);
623 struct util_info
* putil
= get_util(p
);
624 if (!putil
->pathname
)
625 putil
->pathname
= xstrdup(new_name
);
634 case REV_TREE_DIFFERENT
:
636 if (!get_util(p
)->pathname
)
637 get_util(p
)->pathname
=
638 get_util(commit
)->pathname
;
641 die("bad tree compare for commit %s",
642 sha1_to_hex(commit
->object
.sha1
));
644 commit
->object
.flags
|= TREECHANGE
;
652 unsigned long author_time
;
656 static void get_commit_info(struct commit
* commit
, struct commit_info
* ret
)
660 static char author_buf
[1024];
662 tmp
= strstr(commit
->buffer
, "\nauthor ") + 8;
663 len
= strchr(tmp
, '\n') - tmp
;
664 ret
->author
= author_buf
;
665 memcpy(ret
->author
, tmp
, len
);
672 ret
->author_tz
= tmp
+1;
677 ret
->author_time
= strtoul(tmp
, NULL
, 10);
682 ret
->author_mail
= tmp
+ 1;
687 static const char* format_time(unsigned long time
, const char* tz_str
,
690 static char time_buf
[128];
696 sprintf(time_buf
, "%lu %s", time
, tz_str
);
701 minutes
= tz
< 0 ? -tz
: tz
;
702 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
703 minutes
= tz
< 0 ? -minutes
: minutes
;
704 t
= time
+ minutes
* 60;
707 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
708 strcat(time_buf
, tz_str
);
712 static void topo_setter(struct commit
* c
, void* data
)
714 struct util_info
* util
= c
->util
;
715 util
->topo_data
= data
;
718 static void* topo_getter(struct commit
* c
)
720 struct util_info
* util
= c
->util
;
721 return util
->topo_data
;
724 static int read_ancestry(const char *graft_file
,
725 unsigned char **start_sha1
)
727 FILE *fp
= fopen(graft_file
, "r");
731 while (fgets(buf
, sizeof(buf
), fp
)) {
732 /* The format is just "Commit Parent1 Parent2 ...\n" */
733 int len
= strlen(buf
);
734 struct commit_graft
*graft
= read_graft_line(buf
, len
);
735 register_commit_graft(graft
, 0);
737 *start_sha1
= graft
->sha1
;
743 int main(int argc
, const char **argv
)
746 struct commit
*initial
= NULL
;
747 unsigned char sha1
[20], *sha1_p
= NULL
;
749 const char *filename
= NULL
, *commit
= NULL
;
750 char filename_buf
[256];
752 int compatibility
= 0;
753 int show_raw_time
= 0;
755 struct commit
* start_commit
;
757 const char* args
[10];
760 struct commit_info ci
;
763 int longest_file
, longest_author
;
766 const char* prefix
= setup_git_directory();
767 git_config(git_default_config
);
769 for(i
= 1; i
< argc
; i
++) {
771 if(!strcmp(argv
[i
], "-h") ||
772 !strcmp(argv
[i
], "--help"))
774 else if(!strcmp(argv
[i
], "-l") ||
775 !strcmp(argv
[i
], "--long")) {
778 } else if(!strcmp(argv
[i
], "-c") ||
779 !strcmp(argv
[i
], "--compatibility")) {
782 } else if(!strcmp(argv
[i
], "-t") ||
783 !strcmp(argv
[i
], "--time")) {
786 } else if(!strcmp(argv
[i
], "-S")) {
788 !read_ancestry(argv
[i
+ 1], &sha1_p
)) {
794 } else if(!strcmp(argv
[i
], "--")) {
797 } else if(argv
[i
][0] == '-')
815 if (commit
&& sha1_p
)
821 sprintf(filename_buf
, "%s%s", prefix
, filename
);
823 strcpy(filename_buf
, filename
);
824 filename
= filename_buf
;
827 if (get_sha1(commit
, sha1
))
828 die("get_sha1 failed, commit '%s' not found", commit
);
831 start_commit
= lookup_commit_reference(sha1_p
);
832 get_util(start_commit
)->pathname
= filename
;
833 if (fill_util_info(start_commit
)) {
834 printf("%s not found in %s\n", filename
, commit
);
839 init_revisions(&rev
, setup_git_directory());
840 rev
.remove_empty_trees
= 1;
842 rev
.prune_fn
= simplify_commit
;
843 rev
.topo_setter
= topo_setter
;
844 rev
.topo_getter
= topo_getter
;
848 commit_list_insert(start_commit
, &rev
.commits
);
852 diff_tree_setup_paths(args
, &rev
.pruning
);
853 prepare_revision_walk(&rev
);
854 process_commits(&rev
, filename
, &initial
);
856 buf
= blame_contents
;
857 for (max_digits
= 1, i
= 10; i
<= num_blame_lines
+ 1; max_digits
++)
863 for (i
= 0; i
< num_blame_lines
; i
++) {
864 struct commit
*c
= blame_lines
[i
];
870 if (!found_rename
&& strcmp(filename
, u
->pathname
))
872 if (longest_file
< strlen(u
->pathname
))
873 longest_file
= strlen(u
->pathname
);
874 get_commit_info(c
, &ci
);
875 if (longest_author
< strlen(ci
.author
))
876 longest_author
= strlen(ci
.author
);
879 for (i
= 0; i
< num_blame_lines
; i
++) {
880 struct commit
*c
= blame_lines
[i
];
887 get_commit_info(c
, &ci
);
888 fwrite(sha1_to_hex(c
->object
.sha1
), sha1_len
, 1, stdout
);
890 printf("\t(%10s\t%10s\t%d)", ci
.author
,
891 format_time(ci
.author_time
, ci
.author_tz
,
896 printf(" %-*.*s", longest_file
, longest_file
,
898 printf(" (%-*.*s %10s %*d) ",
899 longest_author
, longest_author
, ci
.author
,
900 format_time(ci
.author_time
, ci
.author_tz
,
905 if(i
== num_blame_lines
- 1) {
906 fwrite(buf
, blame_len
- (buf
- blame_contents
),
908 if(blame_contents
[blame_len
-1] != '\n')
911 char* next_buf
= strchr(buf
, '\n') + 1;
912 fwrite(buf
, next_buf
- buf
, 1, stdout
);
918 printf("num get patch: %d\n", num_get_patch
);
919 printf("num commits: %d\n", num_commits
);
920 printf("patch time: %f\n", patch_time
/ 1000000.0);
921 printf("initial: %s\n", sha1_to_hex(initial
->object
.sha1
));