2 * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
19 #include "xdiff-interface.h"
23 static const char blame_usage
[] =
24 "git-blame [-c] [-l] [-t] [-S <revs-file>] [--] file [commit]\n"
25 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
26 " -l, --long Show long commit SHA1 (Default: off)\n"
27 " -t, --time Show raw timestamp (Default: off)\n"
28 " -S, --revs-file Use revisions from revs-file instead of calling git-rev-list\n"
29 " -h, --help This message";
31 static struct commit
**blame_lines
;
32 static int num_blame_lines
;
33 static char *blame_contents
;
38 unsigned char sha1
[20]; /* blob sha, not commit! */
48 int off1
, len1
; /* --- */
49 int off2
, len2
; /* +++ */
57 static void get_blob(struct commit
*commit
);
59 /* Only used for statistics */
60 static int num_get_patch
;
61 static int num_commits
;
62 static int patch_time
;
64 struct blame_diff_state
{
65 struct xdiff_emit_state xm
;
69 static void process_u0_diff(void *state_
, char *line
, unsigned long len
)
71 struct blame_diff_state
*state
= state_
;
74 if (len
< 4 || line
[0] != '@' || line
[1] != '@')
78 printf("chunk line: %.*s", (int)len
, line
);
80 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
81 sizeof(struct chunk
) * state
->ret
->num
);
82 chunk
= &state
->ret
->chunks
[state
->ret
->num
- 1];
84 assert(!strncmp(line
, "@@ -", 4));
86 if (parse_hunk_header(line
, len
,
87 &chunk
->off1
, &chunk
->len1
,
88 &chunk
->off2
, &chunk
->len2
)) {
103 assert(chunk
->off1
>= 0);
104 assert(chunk
->off2
>= 0);
107 static struct patch
*get_patch(struct commit
*commit
, struct commit
*other
)
109 struct blame_diff_state state
;
112 mmfile_t file_c
, file_o
;
114 struct util_info
*info_c
= (struct util_info
*)commit
->util
;
115 struct util_info
*info_o
= (struct util_info
*)other
->util
;
116 struct timeval tv_start
, tv_end
;
119 file_c
.ptr
= info_c
->buf
;
120 file_c
.size
= info_c
->size
;
123 file_o
.ptr
= info_o
->buf
;
124 file_o
.size
= info_o
->size
;
126 gettimeofday(&tv_start
, NULL
);
128 xpp
.flags
= XDF_NEED_MINIMAL
;
131 ecb
.outf
= xdiff_outf
;
133 memset(&state
, 0, sizeof(state
));
134 state
.xm
.consume
= process_u0_diff
;
135 state
.ret
= xmalloc(sizeof(struct patch
));
136 state
.ret
->chunks
= NULL
;
139 xdl_diff(&file_c
, &file_o
, &xpp
, &xecfg
, &ecb
);
141 gettimeofday(&tv_end
, NULL
);
142 patch_time
+= 1000000 * (tv_end
.tv_sec
- tv_start
.tv_sec
) +
143 tv_end
.tv_usec
- tv_start
.tv_usec
;
149 static void free_patch(struct patch
*p
)
155 static int get_blob_sha1_internal(const unsigned char *sha1
, const char *base
,
156 int baselen
, const char *pathname
,
157 unsigned mode
, int stage
);
159 static unsigned char blob_sha1
[20];
160 static const char *blame_file
;
161 static int get_blob_sha1(struct tree
*t
, const char *pathname
,
164 const char *pathspec
[2];
165 blame_file
= pathname
;
166 pathspec
[0] = pathname
;
169 read_tree_recursive(t
, "", 0, 0, pathspec
, get_blob_sha1_internal
);
171 if (is_null_sha1(blob_sha1
))
174 hashcpy(sha1
, blob_sha1
);
178 static int get_blob_sha1_internal(const unsigned char *sha1
, const char *base
,
179 int baselen
, const char *pathname
,
180 unsigned mode
, int stage
)
183 return READ_TREE_RECURSIVE
;
185 if (strncmp(blame_file
, base
, baselen
) ||
186 strcmp(blame_file
+ baselen
, pathname
))
189 hashcpy(blob_sha1
, sha1
);
193 static void get_blob(struct commit
*commit
)
195 struct util_info
*info
= commit
->util
;
201 info
->buf
= read_sha1_file(info
->sha1
, type
, &info
->size
);
203 assert(!strcmp(type
, blob_type
));
206 /* For debugging only */
207 static void print_patch(struct patch
*p
)
210 printf("Num chunks: %d\n", p
->num
);
211 for (i
= 0; i
< p
->num
; i
++) {
212 printf("%d,%d %d,%d\n", p
->chunks
[i
].off1
, p
->chunks
[i
].len1
,
213 p
->chunks
[i
].off2
, p
->chunks
[i
].len2
);
218 /* For debugging only */
219 static void print_map(struct commit
*cmit
, struct commit
*other
)
221 struct util_info
*util
= cmit
->util
;
222 struct util_info
*util2
= other
->util
;
227 util2
->num_lines
? util
->num_lines
: util2
->num_lines
;
230 for (i
= 0; i
< max
; i
++) {
234 if (i
< util
->num_lines
) {
235 num
= util
->line_map
[i
];
241 if (i
< util2
->num_lines
) {
242 int num2
= util2
->line_map
[i
];
243 printf("%d\t", num2
);
244 if (num
!= -1 && num2
!= num
)
255 /* p is a patch from commit to other. */
256 static void fill_line_map(struct commit
*commit
, struct commit
*other
,
259 struct util_info
*util
= commit
->util
;
260 struct util_info
*util2
= other
->util
;
261 int *map
= util
->line_map
;
262 int *map2
= util2
->line_map
;
269 printf("num lines 1: %d num lines 2: %d\n", util
->num_lines
,
273 for (i1
= 0, i2
= 0; i1
< util
->num_lines
; i1
++, i2
++) {
274 struct chunk
*chunk
= NULL
;
275 if (cur_chunk
< p
->num
)
276 chunk
= &p
->chunks
[cur_chunk
];
278 if (chunk
&& chunk
->off1
== i1
) {
279 if (DEBUG
&& i2
!= chunk
->off2
)
280 printf("i2: %d off2: %d\n", i2
, chunk
->off2
);
282 assert(i2
== chunk
->off2
);
295 if (i2
>= util2
->num_lines
)
298 if (map
[i1
] != map2
[i2
] && map
[i1
] != -1) {
300 printf("map: i1: %d %d %p i2: %d %d %p\n",
302 (void *) (i1
!= -1 ? blame_lines
[map
[i1
]] : NULL
),
304 (void *) (i2
!= -1 ? blame_lines
[map2
[i2
]] : NULL
));
305 if (map2
[i2
] != -1 &&
306 blame_lines
[map
[i1
]] &&
307 !blame_lines
[map2
[i2
]])
311 if (map
[i1
] == -1 && map2
[i2
] != -1)
316 printf("l1: %d l2: %d i1: %d i2: %d\n",
317 map
[i1
], map2
[i2
], i1
, i2
);
321 static int map_line(struct commit
*commit
, int line
)
323 struct util_info
*info
= commit
->util
;
324 assert(line
>= 0 && line
< info
->num_lines
);
325 return info
->line_map
[line
];
328 static struct util_info
*get_util(struct commit
*commit
)
330 struct util_info
*util
= commit
->util
;
335 util
= xmalloc(sizeof(struct util_info
));
338 util
->line_map
= NULL
;
339 util
->num_lines
= -1;
340 util
->pathname
= NULL
;
345 static int fill_util_info(struct commit
*commit
)
347 struct util_info
*util
= commit
->util
;
350 assert(util
->pathname
);
352 return !!get_blob_sha1(commit
->tree
, util
->pathname
, util
->sha1
);
355 static void alloc_line_map(struct commit
*commit
)
357 struct util_info
*util
= commit
->util
;
366 for (i
= 0; i
< util
->size
; i
++) {
367 if (util
->buf
[i
] == '\n')
370 if (util
->buf
[util
->size
- 1] != '\n')
373 util
->line_map
= xmalloc(sizeof(int) * util
->num_lines
);
375 for (i
= 0; i
< util
->num_lines
; i
++)
376 util
->line_map
[i
] = -1;
379 static void init_first_commit(struct commit
*commit
, const char *filename
)
381 struct util_info
*util
= commit
->util
;
384 util
->pathname
= filename
;
385 if (fill_util_info(commit
))
386 die("fill_util_info failed");
388 alloc_line_map(commit
);
392 for (i
= 0; i
< util
->num_lines
; i
++)
393 util
->line_map
[i
] = i
;
396 static void process_commits(struct rev_info
*rev
, const char *path
,
397 struct commit
**initial
)
400 struct util_info
*util
;
406 struct commit
*commit
= get_revision(rev
);
408 init_first_commit(commit
, path
);
411 num_blame_lines
= util
->num_lines
;
412 blame_lines
= xmalloc(sizeof(struct commit
*) * num_blame_lines
);
413 blame_contents
= util
->buf
;
414 blame_len
= util
->size
;
416 for (i
= 0; i
< num_blame_lines
; i
++)
417 blame_lines
[i
] = NULL
;
419 lines_left
= num_blame_lines
;
420 blame_p
= xmalloc(sizeof(int) * num_blame_lines
);
421 new_lines
= xmalloc(sizeof(int) * num_blame_lines
);
423 struct commit_list
*parents
;
425 struct util_info
*util
;
428 printf("\nProcessing commit: %d %s\n", num_commits
,
429 sha1_to_hex(commit
->object
.sha1
));
435 memset(blame_p
, 0, sizeof(int) * num_blame_lines
);
438 for (parents
= commit
->parents
;
439 parents
!= NULL
; parents
= parents
->next
)
442 if (num_parents
== 0)
445 if (fill_util_info(commit
))
448 alloc_line_map(commit
);
451 for (parents
= commit
->parents
;
452 parents
!= NULL
; parents
= parents
->next
) {
453 struct commit
*parent
= parents
->item
;
456 if (parse_commit(parent
) < 0)
457 die("parse_commit error");
460 printf("parent: %s\n",
461 sha1_to_hex(parent
->object
.sha1
));
463 if (fill_util_info(parent
)) {
468 patch
= get_patch(parent
, commit
);
469 alloc_line_map(parent
);
470 fill_line_map(parent
, commit
, patch
);
472 for (i
= 0; i
< patch
->num
; i
++) {
474 for (l
= 0; l
< patch
->chunks
[i
].len2
; l
++) {
476 map_line(commit
, patch
->chunks
[i
].off2
+ l
);
477 if (mapped_line
!= -1) {
478 blame_p
[mapped_line
]++;
479 if (blame_p
[mapped_line
] == num_parents
)
480 new_lines
[new_lines_len
++] = mapped_line
;
488 printf("parents: %d\n", num_parents
);
490 for (i
= 0; i
< new_lines_len
; i
++) {
491 int mapped_line
= new_lines
[i
];
492 if (blame_lines
[mapped_line
] == NULL
) {
493 blame_lines
[mapped_line
] = commit
;
496 printf("blame: mapped: %d i: %d\n",
500 } while ((commit
= get_revision(rev
)) != NULL
);
503 static int compare_tree_path(struct rev_info
*revs
,
504 struct commit
*c1
, struct commit
*c2
)
507 const char *paths
[2];
508 struct util_info
*util
= c2
->util
;
509 paths
[0] = util
->pathname
;
512 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
514 ret
= rev_compare_tree(revs
, c1
->tree
, c2
->tree
);
515 diff_tree_release_paths(&revs
->pruning
);
519 static int same_tree_as_empty_path(struct rev_info
*revs
, struct tree
*t1
,
523 const char *paths
[2];
527 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
529 ret
= rev_same_tree_as_empty(revs
, t1
);
530 diff_tree_release_paths(&revs
->pruning
);
534 static const char *find_rename(struct commit
*commit
, struct commit
*parent
)
536 struct util_info
*cutil
= commit
->util
;
537 struct diff_options diff_opts
;
538 const char *paths
[1];
542 printf("find_rename commit: %s ",
543 sha1_to_hex(commit
->object
.sha1
));
544 puts(sha1_to_hex(parent
->object
.sha1
));
547 diff_setup(&diff_opts
);
548 diff_opts
.recursive
= 1;
549 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
551 diff_tree_setup_paths(paths
, &diff_opts
);
552 if (diff_setup_done(&diff_opts
) < 0)
553 die("diff_setup_done failed");
555 diff_tree_sha1(commit
->tree
->object
.sha1
, parent
->tree
->object
.sha1
,
557 diffcore_std(&diff_opts
);
559 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
560 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
562 if (p
->status
== 'R' &&
563 !strcmp(p
->one
->path
, cutil
->pathname
)) {
565 printf("rename %s -> %s\n",
566 p
->one
->path
, p
->two
->path
);
574 static void simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
576 struct commit_list
**pp
, *parent
;
581 if (!commit
->parents
) {
582 struct util_info
*util
= commit
->util
;
583 if (!same_tree_as_empty_path(revs
, commit
->tree
,
585 commit
->object
.flags
|= TREECHANGE
;
589 pp
= &commit
->parents
;
590 while ((parent
= *pp
) != NULL
) {
591 struct commit
*p
= parent
->item
;
593 if (p
->object
.flags
& UNINTERESTING
) {
599 switch (compare_tree_path(revs
, p
, commit
)) {
602 commit
->parents
= parent
;
603 get_util(p
)->pathname
= get_util(commit
)->pathname
;
608 struct util_info
*util
= commit
->util
;
609 if (revs
->remove_empty_trees
&&
610 same_tree_as_empty_path(revs
, p
->tree
,
612 const char *new_name
= find_rename(commit
, p
);
614 struct util_info
*putil
= get_util(p
);
615 if (!putil
->pathname
)
616 putil
->pathname
= xstrdup(new_name
);
626 case REV_TREE_DIFFERENT
:
628 if (!get_util(p
)->pathname
)
629 get_util(p
)->pathname
=
630 get_util(commit
)->pathname
;
633 die("bad tree compare for commit %s",
634 sha1_to_hex(commit
->object
.sha1
));
636 commit
->object
.flags
|= TREECHANGE
;
643 unsigned long author_time
;
647 static void get_commit_info(struct commit
*commit
, struct commit_info
*ret
)
651 static char author_buf
[1024];
653 tmp
= strstr(commit
->buffer
, "\nauthor ") + 8;
654 len
= strchr(tmp
, '\n') - tmp
;
655 ret
->author
= author_buf
;
656 memcpy(ret
->author
, tmp
, len
);
663 ret
->author_tz
= tmp
+1;
668 ret
->author_time
= strtoul(tmp
, NULL
, 10);
673 ret
->author_mail
= tmp
+ 1;
678 static const char *format_time(unsigned long time
, const char *tz_str
,
681 static char time_buf
[128];
687 sprintf(time_buf
, "%lu %s", time
, tz_str
);
692 minutes
= tz
< 0 ? -tz
: tz
;
693 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
694 minutes
= tz
< 0 ? -minutes
: minutes
;
695 t
= time
+ minutes
* 60;
698 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
699 strcat(time_buf
, tz_str
);
703 static void topo_setter(struct commit
*c
, void *data
)
705 struct util_info
*util
= c
->util
;
706 util
->topo_data
= data
;
709 static void *topo_getter(struct commit
*c
)
711 struct util_info
*util
= c
->util
;
712 return util
->topo_data
;
715 static int read_ancestry(const char *graft_file
,
716 unsigned char **start_sha1
)
718 FILE *fp
= fopen(graft_file
, "r");
722 while (fgets(buf
, sizeof(buf
), fp
)) {
723 /* The format is just "Commit Parent1 Parent2 ...\n" */
724 int len
= strlen(buf
);
725 struct commit_graft
*graft
= read_graft_line(buf
, len
);
726 register_commit_graft(graft
, 0);
728 *start_sha1
= graft
->sha1
;
734 static int lineno_width(int lines
)
738 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
743 static int find_orig_linenum(struct util_info
*u
, int lineno
)
747 for (i
= 0; i
< u
->num_lines
; i
++)
748 if (lineno
== u
->line_map
[i
])
753 int main(int argc
, const char **argv
)
756 struct commit
*initial
= NULL
;
757 unsigned char sha1
[20], *sha1_p
= NULL
;
759 const char *filename
= NULL
, *commit
= NULL
;
760 char filename_buf
[256];
762 int compatibility
= 0;
763 int show_raw_time
= 0;
765 struct commit
*start_commit
;
767 const char *args
[10];
770 struct commit_info ci
;
772 int max_digits
, max_orig_digits
;
773 int longest_file
, longest_author
, longest_file_lines
;
777 const char *prefix
= setup_git_directory();
778 git_config(git_default_config
);
780 for (i
= 1; i
< argc
; i
++) {
782 if (!strcmp(argv
[i
], "-h") ||
783 !strcmp(argv
[i
], "--help"))
785 if (!strcmp(argv
[i
], "-l") ||
786 !strcmp(argv
[i
], "--long")) {
790 if (!strcmp(argv
[i
], "-c") ||
791 !strcmp(argv
[i
], "--compatibility")) {
795 if (!strcmp(argv
[i
], "-t") ||
796 !strcmp(argv
[i
], "--time")) {
800 if (!strcmp(argv
[i
], "-S")) {
802 !read_ancestry(argv
[i
+ 1], &sha1_p
)) {
809 if (!strcmp(argv
[i
], "-f") ||
810 !strcmp(argv
[i
], "--show-name")) {
814 if (!strcmp(argv
[i
], "-n") ||
815 !strcmp(argv
[i
], "--show-number")) {
819 if (!strcmp(argv
[i
], "--")) {
823 if (argv
[i
][0] == '-')
840 if (commit
&& sha1_p
)
846 sprintf(filename_buf
, "%s%s", prefix
, filename
);
848 strcpy(filename_buf
, filename
);
849 filename
= filename_buf
;
852 if (get_sha1(commit
, sha1
))
853 die("get_sha1 failed, commit '%s' not found", commit
);
856 start_commit
= lookup_commit_reference(sha1_p
);
857 get_util(start_commit
)->pathname
= filename
;
858 if (fill_util_info(start_commit
)) {
859 printf("%s not found in %s\n", filename
, commit
);
863 init_revisions(&rev
, setup_git_directory());
864 rev
.remove_empty_trees
= 1;
866 rev
.prune_fn
= simplify_commit
;
867 rev
.topo_setter
= topo_setter
;
868 rev
.topo_getter
= topo_getter
;
872 commit_list_insert(start_commit
, &rev
.commits
);
876 diff_tree_setup_paths(args
, &rev
.pruning
);
877 prepare_revision_walk(&rev
);
878 process_commits(&rev
, filename
, &initial
);
880 buf
= blame_contents
;
881 max_digits
= lineno_width(num_blame_lines
);
885 longest_file_lines
= 0;
886 for (i
= 0; i
< num_blame_lines
; i
++) {
887 struct commit
*c
= blame_lines
[i
];
893 if (!show_name
&& strcmp(filename
, u
->pathname
))
895 if (longest_file
< strlen(u
->pathname
))
896 longest_file
= strlen(u
->pathname
);
897 if (longest_file_lines
< u
->num_lines
)
898 longest_file_lines
= u
->num_lines
;
899 get_commit_info(c
, &ci
);
900 if (longest_author
< strlen(ci
.author
))
901 longest_author
= strlen(ci
.author
);
904 max_orig_digits
= lineno_width(longest_file_lines
);
906 for (i
= 0; i
< num_blame_lines
; i
++) {
907 struct commit
*c
= blame_lines
[i
];
913 lineno
= find_orig_linenum(u
, i
);
915 get_commit_info(c
, &ci
);
916 fwrite(sha1_to_hex(c
->object
.sha1
), sha1_len
, 1, stdout
);
918 printf("\t(%10s\t%10s\t%d)", ci
.author
,
919 format_time(ci
.author_time
, ci
.author_tz
,
925 printf(" %-*.*s", longest_file
, longest_file
,
928 printf(" %*d", max_orig_digits
,
930 printf(" (%-*.*s %10s %*d) ",
931 longest_author
, longest_author
, ci
.author
,
932 format_time(ci
.author_time
, ci
.author_tz
,
937 if (i
== num_blame_lines
- 1) {
938 fwrite(buf
, blame_len
- (buf
- blame_contents
),
940 if (blame_contents
[blame_len
-1] != '\n')
944 char *next_buf
= strchr(buf
, '\n') + 1;
945 fwrite(buf
, next_buf
- buf
, 1, stdout
);
951 printf("num get patch: %d\n", num_get_patch
);
952 printf("num commits: %d\n", num_commits
);
953 printf("patch time: %f\n", patch_time
/ 1000000.0);
954 printf("initial: %s\n", sha1_to_hex(initial
->object
.sha1
));