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] [-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! */
44 unsigned meta_given
:1;
50 int off1
, len1
; /* --- */
51 int off2
, len2
; /* +++ */
59 static void get_blob(struct commit
*commit
);
61 /* Only used for statistics */
62 static int num_get_patch
;
63 static int num_commits
;
64 static int patch_time
;
66 struct blame_diff_state
{
67 struct xdiff_emit_state xm
;
71 static void process_u0_diff(void *state_
, char *line
, unsigned long len
)
73 struct blame_diff_state
*state
= state_
;
76 if (len
< 4 || line
[0] != '@' || line
[1] != '@')
80 printf("chunk line: %.*s", (int)len
, line
);
82 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
83 sizeof(struct chunk
) * state
->ret
->num
);
84 chunk
= &state
->ret
->chunks
[state
->ret
->num
- 1];
86 assert(!strncmp(line
, "@@ -", 4));
88 if (parse_hunk_header(line
, len
,
89 &chunk
->off1
, &chunk
->len1
,
90 &chunk
->off2
, &chunk
->len2
)) {
105 assert(chunk
->off1
>= 0);
106 assert(chunk
->off2
>= 0);
109 static struct patch
*get_patch(struct commit
*commit
, struct commit
*other
)
111 struct blame_diff_state state
;
114 mmfile_t file_c
, file_o
;
116 struct util_info
*info_c
= (struct util_info
*)commit
->util
;
117 struct util_info
*info_o
= (struct util_info
*)other
->util
;
118 struct timeval tv_start
, tv_end
;
121 file_c
.ptr
= info_c
->buf
;
122 file_c
.size
= info_c
->size
;
125 file_o
.ptr
= info_o
->buf
;
126 file_o
.size
= info_o
->size
;
128 gettimeofday(&tv_start
, NULL
);
130 xpp
.flags
= XDF_NEED_MINIMAL
;
133 ecb
.outf
= xdiff_outf
;
135 memset(&state
, 0, sizeof(state
));
136 state
.xm
.consume
= process_u0_diff
;
137 state
.ret
= xmalloc(sizeof(struct patch
));
138 state
.ret
->chunks
= NULL
;
141 xdl_diff(&file_c
, &file_o
, &xpp
, &xecfg
, &ecb
);
143 gettimeofday(&tv_end
, NULL
);
144 patch_time
+= 1000000 * (tv_end
.tv_sec
- tv_start
.tv_sec
) +
145 tv_end
.tv_usec
- tv_start
.tv_usec
;
151 static void free_patch(struct patch
*p
)
157 static int get_blob_sha1_internal(const unsigned char *sha1
, const char *base
,
158 int baselen
, const char *pathname
,
159 unsigned mode
, int stage
);
161 static unsigned char blob_sha1
[20];
162 static const char *blame_file
;
163 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 if (is_null_sha1(blob_sha1
))
176 hashcpy(sha1
, blob_sha1
);
180 static int get_blob_sha1_internal(const unsigned char *sha1
, const char *base
,
181 int baselen
, const char *pathname
,
182 unsigned mode
, int stage
)
185 return READ_TREE_RECURSIVE
;
187 if (strncmp(blame_file
, base
, baselen
) ||
188 strcmp(blame_file
+ baselen
, pathname
))
191 hashcpy(blob_sha1
, sha1
);
195 static void get_blob(struct commit
*commit
)
197 struct util_info
*info
= commit
->util
;
203 info
->buf
= read_sha1_file(info
->sha1
, type
, &info
->size
);
205 assert(!strcmp(type
, blob_type
));
208 /* For debugging only */
209 static void print_patch(struct patch
*p
)
212 printf("Num chunks: %d\n", p
->num
);
213 for (i
= 0; i
< p
->num
; i
++) {
214 printf("%d,%d %d,%d\n", p
->chunks
[i
].off1
, p
->chunks
[i
].len1
,
215 p
->chunks
[i
].off2
, p
->chunks
[i
].len2
);
220 /* For debugging only */
221 static void print_map(struct commit
*cmit
, struct commit
*other
)
223 struct util_info
*util
= cmit
->util
;
224 struct util_info
*util2
= other
->util
;
229 util2
->num_lines
? util
->num_lines
: util2
->num_lines
;
232 for (i
= 0; i
< max
; i
++) {
236 if (i
< util
->num_lines
) {
237 num
= util
->line_map
[i
];
243 if (i
< util2
->num_lines
) {
244 int num2
= util2
->line_map
[i
];
245 printf("%d\t", num2
);
246 if (num
!= -1 && num2
!= num
)
257 /* p is a patch from commit to other. */
258 static void fill_line_map(struct commit
*commit
, struct commit
*other
,
261 struct util_info
*util
= commit
->util
;
262 struct util_info
*util2
= other
->util
;
263 int *map
= util
->line_map
;
264 int *map2
= util2
->line_map
;
271 printf("num lines 1: %d num lines 2: %d\n", util
->num_lines
,
275 for (i1
= 0, i2
= 0; i1
< util
->num_lines
; i1
++, i2
++) {
276 struct chunk
*chunk
= NULL
;
277 if (cur_chunk
< p
->num
)
278 chunk
= &p
->chunks
[cur_chunk
];
280 if (chunk
&& chunk
->off1
== i1
) {
281 if (DEBUG
&& i2
!= chunk
->off2
)
282 printf("i2: %d off2: %d\n", i2
, chunk
->off2
);
284 assert(i2
== chunk
->off2
);
297 if (i2
>= util2
->num_lines
)
300 if (map
[i1
] != map2
[i2
] && map
[i1
] != -1) {
302 printf("map: i1: %d %d %p i2: %d %d %p\n",
304 (void *) (i1
!= -1 ? blame_lines
[map
[i1
]] : NULL
),
306 (void *) (i2
!= -1 ? blame_lines
[map2
[i2
]] : NULL
));
307 if (map2
[i2
] != -1 &&
308 blame_lines
[map
[i1
]] &&
309 !blame_lines
[map2
[i2
]])
313 if (map
[i1
] == -1 && map2
[i2
] != -1)
318 printf("l1: %d l2: %d i1: %d i2: %d\n",
319 map
[i1
], map2
[i2
], i1
, i2
);
323 static int map_line(struct commit
*commit
, int line
)
325 struct util_info
*info
= commit
->util
;
326 assert(line
>= 0 && line
< info
->num_lines
);
327 return info
->line_map
[line
];
330 static struct util_info
*get_util(struct commit
*commit
)
332 struct util_info
*util
= commit
->util
;
337 util
= xcalloc(1, sizeof(struct util_info
));
338 util
->num_lines
= -1;
343 static int fill_util_info(struct commit
*commit
)
345 struct util_info
*util
= commit
->util
;
348 assert(util
->pathname
);
350 return !!get_blob_sha1(commit
->tree
, util
->pathname
, util
->sha1
);
353 static void alloc_line_map(struct commit
*commit
)
355 struct util_info
*util
= commit
->util
;
364 for (i
= 0; i
< util
->size
; i
++) {
365 if (util
->buf
[i
] == '\n')
368 if (util
->buf
[util
->size
- 1] != '\n')
371 util
->line_map
= xmalloc(sizeof(int) * util
->num_lines
);
373 for (i
= 0; i
< util
->num_lines
; i
++)
374 util
->line_map
[i
] = -1;
377 static void init_first_commit(struct commit
*commit
, const char *filename
)
379 struct util_info
*util
= commit
->util
;
382 util
->pathname
= filename
;
383 if (fill_util_info(commit
))
384 die("fill_util_info failed");
386 alloc_line_map(commit
);
390 for (i
= 0; i
< util
->num_lines
; i
++)
391 util
->line_map
[i
] = i
;
394 static void process_commits(struct rev_info
*rev
, const char *path
,
395 struct commit
**initial
)
398 struct util_info
*util
;
404 struct commit
*commit
= get_revision(rev
);
406 init_first_commit(commit
, path
);
409 num_blame_lines
= util
->num_lines
;
410 blame_lines
= xmalloc(sizeof(struct commit
*) * num_blame_lines
);
411 blame_contents
= util
->buf
;
412 blame_len
= util
->size
;
414 for (i
= 0; i
< num_blame_lines
; i
++)
415 blame_lines
[i
] = NULL
;
417 lines_left
= num_blame_lines
;
418 blame_p
= xmalloc(sizeof(int) * num_blame_lines
);
419 new_lines
= xmalloc(sizeof(int) * num_blame_lines
);
421 struct commit_list
*parents
;
423 struct util_info
*util
;
426 printf("\nProcessing commit: %d %s\n", num_commits
,
427 sha1_to_hex(commit
->object
.sha1
));
433 memset(blame_p
, 0, sizeof(int) * num_blame_lines
);
436 for (parents
= commit
->parents
;
437 parents
!= NULL
; parents
= parents
->next
)
440 if (num_parents
== 0)
443 if (fill_util_info(commit
))
446 alloc_line_map(commit
);
449 for (parents
= commit
->parents
;
450 parents
!= NULL
; parents
= parents
->next
) {
451 struct commit
*parent
= parents
->item
;
454 if (parse_commit(parent
) < 0)
455 die("parse_commit error");
458 printf("parent: %s\n",
459 sha1_to_hex(parent
->object
.sha1
));
461 if (fill_util_info(parent
)) {
466 patch
= get_patch(parent
, commit
);
467 alloc_line_map(parent
);
468 fill_line_map(parent
, commit
, patch
);
470 for (i
= 0; i
< patch
->num
; i
++) {
472 for (l
= 0; l
< patch
->chunks
[i
].len2
; l
++) {
474 map_line(commit
, patch
->chunks
[i
].off2
+ l
);
475 if (mapped_line
!= -1) {
476 blame_p
[mapped_line
]++;
477 if (blame_p
[mapped_line
] == num_parents
)
478 new_lines
[new_lines_len
++] = mapped_line
;
486 printf("parents: %d\n", num_parents
);
488 for (i
= 0; i
< new_lines_len
; i
++) {
489 int mapped_line
= new_lines
[i
];
490 if (blame_lines
[mapped_line
] == NULL
) {
491 blame_lines
[mapped_line
] = commit
;
494 printf("blame: mapped: %d i: %d\n",
498 } while ((commit
= get_revision(rev
)) != NULL
);
501 static int compare_tree_path(struct rev_info
*revs
,
502 struct commit
*c1
, struct commit
*c2
)
505 const char *paths
[2];
506 struct util_info
*util
= c2
->util
;
507 paths
[0] = util
->pathname
;
510 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
512 ret
= rev_compare_tree(revs
, c1
->tree
, c2
->tree
);
513 diff_tree_release_paths(&revs
->pruning
);
517 static int same_tree_as_empty_path(struct rev_info
*revs
, struct tree
*t1
,
521 const char *paths
[2];
525 diff_tree_setup_paths(get_pathspec(revs
->prefix
, paths
),
527 ret
= rev_same_tree_as_empty(revs
, t1
);
528 diff_tree_release_paths(&revs
->pruning
);
532 static const char *find_rename(struct commit
*commit
, struct commit
*parent
)
534 struct util_info
*cutil
= commit
->util
;
535 struct diff_options diff_opts
;
536 const char *paths
[1];
540 printf("find_rename commit: %s ",
541 sha1_to_hex(commit
->object
.sha1
));
542 puts(sha1_to_hex(parent
->object
.sha1
));
545 diff_setup(&diff_opts
);
546 diff_opts
.recursive
= 1;
547 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
549 diff_tree_setup_paths(paths
, &diff_opts
);
550 if (diff_setup_done(&diff_opts
) < 0)
551 die("diff_setup_done failed");
553 diff_tree_sha1(commit
->tree
->object
.sha1
, parent
->tree
->object
.sha1
,
555 diffcore_std(&diff_opts
);
557 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
558 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
560 if (p
->status
== 'R' &&
561 !strcmp(p
->one
->path
, cutil
->pathname
)) {
563 printf("rename %s -> %s\n",
564 p
->one
->path
, p
->two
->path
);
572 static void simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
574 struct commit_list
**pp
, *parent
;
579 if (!commit
->parents
) {
580 struct util_info
*util
= commit
->util
;
581 if (!same_tree_as_empty_path(revs
, commit
->tree
,
583 commit
->object
.flags
|= TREECHANGE
;
587 pp
= &commit
->parents
;
588 while ((parent
= *pp
) != NULL
) {
589 struct commit
*p
= parent
->item
;
591 if (p
->object
.flags
& UNINTERESTING
) {
597 switch (compare_tree_path(revs
, p
, commit
)) {
600 commit
->parents
= parent
;
601 get_util(p
)->pathname
= get_util(commit
)->pathname
;
606 struct util_info
*util
= commit
->util
;
607 if (revs
->remove_empty_trees
&&
608 same_tree_as_empty_path(revs
, p
->tree
,
610 const char *new_name
= find_rename(commit
, p
);
612 struct util_info
*putil
= get_util(p
);
613 if (!putil
->pathname
)
614 putil
->pathname
= xstrdup(new_name
);
624 case REV_TREE_DIFFERENT
:
626 if (!get_util(p
)->pathname
)
627 get_util(p
)->pathname
=
628 get_util(commit
)->pathname
;
631 die("bad tree compare for commit %s",
632 sha1_to_hex(commit
->object
.sha1
));
634 commit
->object
.flags
|= TREECHANGE
;
641 unsigned long author_time
;
644 /* filled only when asked for details */
646 char *committer_mail
;
647 unsigned long committer_time
;
653 static void get_ac_line(const char *inbuf
, const char *what
,
654 int bufsz
, char *person
, char **mail
,
655 unsigned long *time
, char **tz
)
660 tmp
= strstr(inbuf
, what
);
664 endp
= strchr(tmp
, '\n');
672 person
= *mail
= *tz
= "(unknown)";
676 memcpy(person
, tmp
, len
);
688 *time
= strtoul(tmp
, NULL
, 10);
697 static void get_commit_info(struct commit
*commit
, struct commit_info
*ret
, int detailed
)
701 static char author_buf
[1024];
702 static char committer_buf
[1024];
703 static char summary_buf
[1024];
705 ret
->author
= author_buf
;
706 get_ac_line(commit
->buffer
, "\nauthor ",
707 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
708 &ret
->author_time
, &ret
->author_tz
);
713 ret
->committer
= committer_buf
;
714 get_ac_line(commit
->buffer
, "\ncommitter ",
715 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
716 &ret
->committer_time
, &ret
->committer_tz
);
718 ret
->summary
= summary_buf
;
719 tmp
= strstr(commit
->buffer
, "\n\n");
722 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
726 endp
= strchr(tmp
, '\n');
730 if (len
>= sizeof(summary_buf
))
732 memcpy(summary_buf
, tmp
, len
);
733 summary_buf
[len
] = 0;
736 static const char *format_time(unsigned long time
, const char *tz_str
,
739 static char time_buf
[128];
745 sprintf(time_buf
, "%lu %s", time
, tz_str
);
750 minutes
= tz
< 0 ? -tz
: tz
;
751 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
752 minutes
= tz
< 0 ? -minutes
: minutes
;
753 t
= time
+ minutes
* 60;
756 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
757 strcat(time_buf
, tz_str
);
761 static void topo_setter(struct commit
*c
, void *data
)
763 struct util_info
*util
= c
->util
;
764 util
->topo_data
= data
;
767 static void *topo_getter(struct commit
*c
)
769 struct util_info
*util
= c
->util
;
770 return util
->topo_data
;
773 static int read_ancestry(const char *graft_file
,
774 unsigned char **start_sha1
)
776 FILE *fp
= fopen(graft_file
, "r");
780 while (fgets(buf
, sizeof(buf
), fp
)) {
781 /* The format is just "Commit Parent1 Parent2 ...\n" */
782 int len
= strlen(buf
);
783 struct commit_graft
*graft
= read_graft_line(buf
, len
);
784 register_commit_graft(graft
, 0);
786 *start_sha1
= graft
->sha1
;
792 static int lineno_width(int lines
)
796 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
801 static int find_orig_linenum(struct util_info
*u
, int lineno
)
805 for (i
= 0; i
< u
->num_lines
; i
++)
806 if (lineno
== u
->line_map
[i
])
811 static void emit_meta(struct commit
*c
, int lno
,
812 int sha1_len
, int compatibility
, int porcelain
,
813 int show_name
, int show_number
, int show_raw_time
,
814 int longest_file
, int longest_author
,
815 int max_digits
, int max_orig_digits
)
819 struct commit_info ci
;
822 lineno
= find_orig_linenum(u
, lno
);
826 struct commit
*cc
= (lno
== 0) ? NULL
: blame_lines
[lno
-1];
828 /* This is the beginning of this group */
830 for (i
= lno
+ 1; i
< num_blame_lines
; i
++)
831 if (blame_lines
[i
] != c
)
833 group_size
= i
- lno
;
836 printf("%s %d %d %d\n", sha1_to_hex(c
->object
.sha1
),
837 lineno
, lno
+ 1, group_size
);
839 printf("%s %d %d\n", sha1_to_hex(c
->object
.sha1
),
841 if (!u
->meta_given
) {
842 get_commit_info(c
, &ci
, 1);
843 printf("author %s\n", ci
.author
);
844 printf("author-mail %s\n", ci
.author_mail
);
845 printf("author-time %lu\n", ci
.author_time
);
846 printf("author-tz %s\n", ci
.author_tz
);
847 printf("committer %s\n", ci
.committer
);
848 printf("committer-mail %s\n", ci
.committer_mail
);
849 printf("committer-time %lu\n", ci
.committer_time
);
850 printf("committer-tz %s\n", ci
.committer_tz
);
852 if (quote_c_style(u
->pathname
, NULL
, NULL
, 0))
853 quote_c_style(u
->pathname
, NULL
, stdout
, 0);
855 fputs(u
->pathname
, stdout
);
856 printf("\nsummary %s\n", ci
.summary
);
864 get_commit_info(c
, &ci
, 0);
865 fwrite(sha1_to_hex(c
->object
.sha1
), sha1_len
, 1, stdout
);
867 printf("\t(%10s\t%10s\t%d)", ci
.author
,
868 format_time(ci
.author_time
, ci
.author_tz
,
874 printf(" %-*.*s", longest_file
, longest_file
,
877 printf(" %*d", max_orig_digits
,
879 printf(" (%-*.*s %10s %*d) ",
880 longest_author
, longest_author
, ci
.author
,
881 format_time(ci
.author_time
, ci
.author_tz
,
883 max_digits
, lno
+ 1);
887 int main(int argc
, const char **argv
)
890 struct commit
*initial
= NULL
;
891 unsigned char sha1
[20], *sha1_p
= NULL
;
893 const char *filename
= NULL
, *commit
= NULL
;
894 char filename_buf
[256];
896 int compatibility
= 0;
897 int show_raw_time
= 0;
899 struct commit
*start_commit
;
901 const char *args
[10];
904 struct commit_info ci
;
906 int max_digits
, max_orig_digits
;
907 int longest_file
, longest_author
, longest_file_lines
;
912 const char *prefix
= setup_git_directory();
913 git_config(git_default_config
);
915 for (i
= 1; i
< argc
; i
++) {
917 if (!strcmp(argv
[i
], "-h") ||
918 !strcmp(argv
[i
], "--help"))
920 if (!strcmp(argv
[i
], "-l") ||
921 !strcmp(argv
[i
], "--long")) {
925 if (!strcmp(argv
[i
], "-c") ||
926 !strcmp(argv
[i
], "--compatibility")) {
930 if (!strcmp(argv
[i
], "-t") ||
931 !strcmp(argv
[i
], "--time")) {
935 if (!strcmp(argv
[i
], "-S")) {
937 !read_ancestry(argv
[i
+ 1], &sha1_p
)) {
944 if (!strcmp(argv
[i
], "-f") ||
945 !strcmp(argv
[i
], "--show-name")) {
949 if (!strcmp(argv
[i
], "-n") ||
950 !strcmp(argv
[i
], "--show-number")) {
954 if (!strcmp(argv
[i
], "--porcelain")) {
960 if (!strcmp(argv
[i
], "--")) {
964 if (argv
[i
][0] == '-')
981 if (commit
&& sha1_p
)
987 sprintf(filename_buf
, "%s%s", prefix
, filename
);
989 strcpy(filename_buf
, filename
);
990 filename
= filename_buf
;
993 if (get_sha1(commit
, sha1
))
994 die("get_sha1 failed, commit '%s' not found", commit
);
997 start_commit
= lookup_commit_reference(sha1_p
);
998 get_util(start_commit
)->pathname
= filename
;
999 if (fill_util_info(start_commit
)) {
1000 printf("%s not found in %s\n", filename
, commit
);
1004 init_revisions(&rev
, setup_git_directory());
1005 rev
.remove_empty_trees
= 1;
1007 rev
.prune_fn
= simplify_commit
;
1008 rev
.topo_setter
= topo_setter
;
1009 rev
.topo_getter
= topo_getter
;
1013 commit_list_insert(start_commit
, &rev
.commits
);
1017 diff_tree_setup_paths(args
, &rev
.pruning
);
1018 prepare_revision_walk(&rev
);
1019 process_commits(&rev
, filename
, &initial
);
1021 for (i
= 0; i
< num_blame_lines
; i
++)
1022 if (!blame_lines
[i
])
1023 blame_lines
[i
] = initial
;
1025 buf
= blame_contents
;
1026 max_digits
= lineno_width(num_blame_lines
);
1030 longest_file_lines
= 0;
1031 for (i
= 0; i
< num_blame_lines
; i
++) {
1032 struct commit
*c
= blame_lines
[i
];
1033 struct util_info
*u
;
1036 if (!show_name
&& strcmp(filename
, u
->pathname
))
1038 if (longest_file
< strlen(u
->pathname
))
1039 longest_file
= strlen(u
->pathname
);
1040 if (longest_file_lines
< u
->num_lines
)
1041 longest_file_lines
= u
->num_lines
;
1042 get_commit_info(c
, &ci
, 0);
1043 if (longest_author
< strlen(ci
.author
))
1044 longest_author
= strlen(ci
.author
);
1047 max_orig_digits
= lineno_width(longest_file_lines
);
1049 for (i
= 0; i
< num_blame_lines
; i
++) {
1050 emit_meta(blame_lines
[i
], i
,
1051 sha1_len
, compatibility
, porcelain
,
1052 show_name
, show_number
, show_raw_time
,
1053 longest_file
, longest_author
,
1054 max_digits
, max_orig_digits
);
1056 if (i
== num_blame_lines
- 1) {
1057 fwrite(buf
, blame_len
- (buf
- blame_contents
),
1059 if (blame_contents
[blame_len
-1] != '\n')
1063 char *next_buf
= strchr(buf
, '\n') + 1;
1064 fwrite(buf
, next_buf
- buf
, 1, stdout
);
1070 printf("num get patch: %d\n", num_get_patch
);
1071 printf("num commits: %d\n", num_commits
);
1072 printf("patch time: %f\n", patch_time
/ 1000000.0);
1073 printf("initial: %s\n", sha1_to_hex(initial
->object
.sha1
));