git-blame: --show-number (and -n)
[git] / blame.c
1 /*
2  * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
3  */
4
5 #include <assert.h>
6 #include <time.h>
7 #include <sys/time.h>
8 #include <math.h>
9
10 #include "cache.h"
11 #include "refs.h"
12 #include "tag.h"
13 #include "commit.h"
14 #include "tree.h"
15 #include "blob.h"
16 #include "diff.h"
17 #include "diffcore.h"
18 #include "revision.h"
19 #include "xdiff-interface.h"
20
21 #define DEBUG 0
22
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";
30
31 static struct commit **blame_lines;
32 static int num_blame_lines;
33 static char *blame_contents;
34 static int blame_len;
35
36 struct util_info {
37         int *line_map;
38         unsigned char sha1[20]; /* blob sha, not commit! */
39         char *buf;
40         unsigned long size;
41         int num_lines;
42         const char *pathname;
43
44         void *topo_data;
45 };
46
47 struct chunk {
48         int off1, len1; /* --- */
49         int off2, len2; /* +++ */
50 };
51
52 struct patch {
53         struct chunk *chunks;
54         int num;
55 };
56
57 static void get_blob(struct commit *commit);
58
59 /* Only used for statistics */
60 static int num_get_patch;
61 static int num_commits;
62 static int patch_time;
63
64 struct blame_diff_state {
65         struct xdiff_emit_state xm;
66         struct patch *ret;
67 };
68
69 static void process_u0_diff(void *state_, char *line, unsigned long len)
70 {
71         struct blame_diff_state *state = state_;
72         struct chunk *chunk;
73
74         if (len < 4 || line[0] != '@' || line[1] != '@')
75                 return;
76
77         if (DEBUG)
78                 printf("chunk line: %.*s", (int)len, line);
79         state->ret->num++;
80         state->ret->chunks = xrealloc(state->ret->chunks,
81                                       sizeof(struct chunk) * state->ret->num);
82         chunk = &state->ret->chunks[state->ret->num - 1];
83
84         assert(!strncmp(line, "@@ -", 4));
85
86         if (parse_hunk_header(line, len,
87                               &chunk->off1, &chunk->len1,
88                               &chunk->off2, &chunk->len2)) {
89                 state->ret->num--;
90                 return;
91         }
92
93         if (chunk->len1 == 0)
94                 chunk->off1++;
95         if (chunk->len2 == 0)
96                 chunk->off2++;
97
98         if (chunk->off1 > 0)
99                 chunk->off1--;
100         if (chunk->off2 > 0)
101                 chunk->off2--;
102
103         assert(chunk->off1 >= 0);
104         assert(chunk->off2 >= 0);
105 }
106
107 static struct patch *get_patch(struct commit *commit, struct commit *other)
108 {
109         struct blame_diff_state state;
110         xpparam_t xpp;
111         xdemitconf_t xecfg;
112         mmfile_t file_c, file_o;
113         xdemitcb_t ecb;
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;
117
118         get_blob(commit);
119         file_c.ptr = info_c->buf;
120         file_c.size = info_c->size;
121
122         get_blob(other);
123         file_o.ptr = info_o->buf;
124         file_o.size = info_o->size;
125
126         gettimeofday(&tv_start, NULL);
127
128         xpp.flags = XDF_NEED_MINIMAL;
129         xecfg.ctxlen = 0;
130         xecfg.flags = 0;
131         ecb.outf = xdiff_outf;
132         ecb.priv = &state;
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;
137         state.ret->num = 0;
138
139         xdl_diff(&file_c, &file_o, &xpp, &xecfg, &ecb);
140
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;
144
145         num_get_patch++;
146         return state.ret;
147 }
148
149 static void free_patch(struct patch *p)
150 {
151         free(p->chunks);
152         free(p);
153 }
154
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);
158
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,
162                          unsigned char *sha1)
163 {
164         const char *pathspec[2];
165         blame_file = pathname;
166         pathspec[0] = pathname;
167         pathspec[1] = NULL;
168         hashclr(blob_sha1);
169         read_tree_recursive(t, "", 0, 0, pathspec, get_blob_sha1_internal);
170
171         if (is_null_sha1(blob_sha1))
172                 return -1;
173
174         hashcpy(sha1, blob_sha1);
175         return 0;
176 }
177
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)
181 {
182         if (S_ISDIR(mode))
183                 return READ_TREE_RECURSIVE;
184
185         if (strncmp(blame_file, base, baselen) ||
186             strcmp(blame_file + baselen, pathname))
187                 return -1;
188
189         hashcpy(blob_sha1, sha1);
190         return -1;
191 }
192
193 static void get_blob(struct commit *commit)
194 {
195         struct util_info *info = commit->util;
196         char type[20];
197
198         if (info->buf)
199                 return;
200
201         info->buf = read_sha1_file(info->sha1, type, &info->size);
202
203         assert(!strcmp(type, blob_type));
204 }
205
206 /* For debugging only */
207 static void print_patch(struct patch *p)
208 {
209         int i;
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);
214         }
215 }
216
217 #if DEBUG
218 /* For debugging only */
219 static void print_map(struct commit *cmit, struct commit *other)
220 {
221         struct util_info *util = cmit->util;
222         struct util_info *util2 = other->util;
223
224         int i;
225         int max =
226             util->num_lines >
227             util2->num_lines ? util->num_lines : util2->num_lines;
228         int num;
229
230         for (i = 0; i < max; i++) {
231                 printf("i: %d ", i);
232                 num = -1;
233
234                 if (i < util->num_lines) {
235                         num = util->line_map[i];
236                         printf("%d\t", num);
237                 }
238                 else
239                         printf("\t");
240
241                 if (i < util2->num_lines) {
242                         int num2 = util2->line_map[i];
243                         printf("%d\t", num2);
244                         if (num != -1 && num2 != num)
245                                 printf("---");
246                 }
247                 else
248                         printf("\t");
249
250                 printf("\n");
251         }
252 }
253 #endif
254
255 /* p is a patch from commit to other. */
256 static void fill_line_map(struct commit *commit, struct commit *other,
257                           struct patch *p)
258 {
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;
263         int cur_chunk = 0;
264         int i1, i2;
265
266         if (DEBUG) {
267                 if (p->num)
268                         print_patch(p);
269                 printf("num lines 1: %d num lines 2: %d\n", util->num_lines,
270                        util2->num_lines);
271         }
272
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];
277
278                 if (chunk && chunk->off1 == i1) {
279                         if (DEBUG && i2 != chunk->off2)
280                                 printf("i2: %d off2: %d\n", i2, chunk->off2);
281
282                         assert(i2 == chunk->off2);
283
284                         i1--;
285                         i2--;
286                         if (chunk->len1 > 0)
287                                 i1 += chunk->len1;
288
289                         if (chunk->len2 > 0)
290                                 i2 += chunk->len2;
291
292                         cur_chunk++;
293                 }
294                 else {
295                         if (i2 >= util2->num_lines)
296                                 break;
297
298                         if (map[i1] != map2[i2] && map[i1] != -1) {
299                                 if (DEBUG)
300                                         printf("map: i1: %d %d %p i2: %d %d %p\n",
301                                                i1, map[i1],
302                                                (void *) (i1 != -1 ? blame_lines[map[i1]] : NULL),
303                                                i2, map2[i2],
304                                                (void *) (i2 != -1 ? blame_lines[map2[i2]] : NULL));
305                                 if (map2[i2] != -1 &&
306                                     blame_lines[map[i1]] &&
307                                     !blame_lines[map2[i2]])
308                                         map[i1] = map2[i2];
309                         }
310
311                         if (map[i1] == -1 && map2[i2] != -1)
312                                 map[i1] = map2[i2];
313                 }
314
315                 if (DEBUG > 1)
316                         printf("l1: %d l2: %d i1: %d i2: %d\n",
317                                map[i1], map2[i2], i1, i2);
318         }
319 }
320
321 static int map_line(struct commit *commit, int line)
322 {
323         struct util_info *info = commit->util;
324         assert(line >= 0 && line < info->num_lines);
325         return info->line_map[line];
326 }
327
328 static struct util_info *get_util(struct commit *commit)
329 {
330         struct util_info *util = commit->util;
331
332         if (util)
333                 return util;
334
335         util = xmalloc(sizeof(struct util_info));
336         util->buf = NULL;
337         util->size = 0;
338         util->line_map = NULL;
339         util->num_lines = -1;
340         util->pathname = NULL;
341         commit->util = util;
342         return util;
343 }
344
345 static int fill_util_info(struct commit *commit)
346 {
347         struct util_info *util = commit->util;
348
349         assert(util);
350         assert(util->pathname);
351
352         return !!get_blob_sha1(commit->tree, util->pathname, util->sha1);
353 }
354
355 static void alloc_line_map(struct commit *commit)
356 {
357         struct util_info *util = commit->util;
358         int i;
359
360         if (util->line_map)
361                 return;
362
363         get_blob(commit);
364
365         util->num_lines = 0;
366         for (i = 0; i < util->size; i++) {
367                 if (util->buf[i] == '\n')
368                         util->num_lines++;
369         }
370         if (util->buf[util->size - 1] != '\n')
371                 util->num_lines++;
372
373         util->line_map = xmalloc(sizeof(int) * util->num_lines);
374
375         for (i = 0; i < util->num_lines; i++)
376                 util->line_map[i] = -1;
377 }
378
379 static void init_first_commit(struct commit *commit, const char *filename)
380 {
381         struct util_info *util = commit->util;
382         int i;
383
384         util->pathname = filename;
385         if (fill_util_info(commit))
386                 die("fill_util_info failed");
387
388         alloc_line_map(commit);
389
390         util = commit->util;
391
392         for (i = 0; i < util->num_lines; i++)
393                 util->line_map[i] = i;
394 }
395
396 static void process_commits(struct rev_info *rev, const char *path,
397                             struct commit **initial)
398 {
399         int i;
400         struct util_info *util;
401         int lines_left;
402         int *blame_p;
403         int *new_lines;
404         int new_lines_len;
405
406         struct commit *commit = get_revision(rev);
407         assert(commit);
408         init_first_commit(commit, path);
409
410         util = commit->util;
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;
415
416         for (i = 0; i < num_blame_lines; i++)
417                 blame_lines[i] = NULL;
418
419         lines_left = num_blame_lines;
420         blame_p = xmalloc(sizeof(int) * num_blame_lines);
421         new_lines = xmalloc(sizeof(int) * num_blame_lines);
422         do {
423                 struct commit_list *parents;
424                 int num_parents;
425                 struct util_info *util;
426
427                 if (DEBUG)
428                         printf("\nProcessing commit: %d %s\n", num_commits,
429                                sha1_to_hex(commit->object.sha1));
430
431                 if (lines_left == 0)
432                         return;
433
434                 num_commits++;
435                 memset(blame_p, 0, sizeof(int) * num_blame_lines);
436                 new_lines_len = 0;
437                 num_parents = 0;
438                 for (parents = commit->parents;
439                      parents != NULL; parents = parents->next)
440                         num_parents++;
441
442                 if (num_parents == 0)
443                         *initial = commit;
444
445                 if (fill_util_info(commit))
446                         continue;
447
448                 alloc_line_map(commit);
449                 util = commit->util;
450
451                 for (parents = commit->parents;
452                      parents != NULL; parents = parents->next) {
453                         struct commit *parent = parents->item;
454                         struct patch *patch;
455
456                         if (parse_commit(parent) < 0)
457                                 die("parse_commit error");
458
459                         if (DEBUG)
460                                 printf("parent: %s\n",
461                                        sha1_to_hex(parent->object.sha1));
462
463                         if (fill_util_info(parent)) {
464                                 num_parents--;
465                                 continue;
466                         }
467
468                         patch = get_patch(parent, commit);
469                         alloc_line_map(parent);
470                         fill_line_map(parent, commit, patch);
471
472                         for (i = 0; i < patch->num; i++) {
473                             int l;
474                             for (l = 0; l < patch->chunks[i].len2; l++) {
475                                 int mapped_line =
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;
481                                 }
482                             }
483                         }
484                         free_patch(patch);
485                 }
486
487                 if (DEBUG)
488                         printf("parents: %d\n", num_parents);
489
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;
494                                 lines_left--;
495                                 if (DEBUG)
496                                         printf("blame: mapped: %d i: %d\n",
497                                                mapped_line, i);
498                         }
499                 }
500         } while ((commit = get_revision(rev)) != NULL);
501 }
502
503 static int compare_tree_path(struct rev_info *revs,
504                              struct commit *c1, struct commit *c2)
505 {
506         int ret;
507         const char *paths[2];
508         struct util_info *util = c2->util;
509         paths[0] = util->pathname;
510         paths[1] = NULL;
511
512         diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
513                               &revs->pruning);
514         ret = rev_compare_tree(revs, c1->tree, c2->tree);
515         diff_tree_release_paths(&revs->pruning);
516         return ret;
517 }
518
519 static int same_tree_as_empty_path(struct rev_info *revs, struct tree *t1,
520                                    const char *path)
521 {
522         int ret;
523         const char *paths[2];
524         paths[0] = path;
525         paths[1] = NULL;
526
527         diff_tree_setup_paths(get_pathspec(revs->prefix, paths),
528                               &revs->pruning);
529         ret = rev_same_tree_as_empty(revs, t1);
530         diff_tree_release_paths(&revs->pruning);
531         return ret;
532 }
533
534 static const char *find_rename(struct commit *commit, struct commit *parent)
535 {
536         struct util_info *cutil = commit->util;
537         struct diff_options diff_opts;
538         const char *paths[1];
539         int i;
540
541         if (DEBUG) {
542                 printf("find_rename commit: %s ",
543                        sha1_to_hex(commit->object.sha1));
544                 puts(sha1_to_hex(parent->object.sha1));
545         }
546
547         diff_setup(&diff_opts);
548         diff_opts.recursive = 1;
549         diff_opts.detect_rename = DIFF_DETECT_RENAME;
550         paths[0] = NULL;
551         diff_tree_setup_paths(paths, &diff_opts);
552         if (diff_setup_done(&diff_opts) < 0)
553                 die("diff_setup_done failed");
554
555         diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
556                        "", &diff_opts);
557         diffcore_std(&diff_opts);
558
559         for (i = 0; i < diff_queued_diff.nr; i++) {
560                 struct diff_filepair *p = diff_queued_diff.queue[i];
561
562                 if (p->status == 'R' &&
563                     !strcmp(p->one->path, cutil->pathname)) {
564                         if (DEBUG)
565                                 printf("rename %s -> %s\n",
566                                        p->one->path, p->two->path);
567                         return p->two->path;
568                 }
569         }
570
571         return 0;
572 }
573
574 static void simplify_commit(struct rev_info *revs, struct commit *commit)
575 {
576         struct commit_list **pp, *parent;
577
578         if (!commit->tree)
579                 return;
580
581         if (!commit->parents) {
582                 struct util_info *util = commit->util;
583                 if (!same_tree_as_empty_path(revs, commit->tree,
584                                              util->pathname))
585                         commit->object.flags |= TREECHANGE;
586                 return;
587         }
588
589         pp = &commit->parents;
590         while ((parent = *pp) != NULL) {
591                 struct commit *p = parent->item;
592
593                 if (p->object.flags & UNINTERESTING) {
594                         pp = &parent->next;
595                         continue;
596                 }
597
598                 parse_commit(p);
599                 switch (compare_tree_path(revs, p, commit)) {
600                 case REV_TREE_SAME:
601                         parent->next = NULL;
602                         commit->parents = parent;
603                         get_util(p)->pathname = get_util(commit)->pathname;
604                         return;
605
606                 case REV_TREE_NEW:
607                 {
608                         struct util_info *util = commit->util;
609                         if (revs->remove_empty_trees &&
610                             same_tree_as_empty_path(revs, p->tree,
611                                                     util->pathname)) {
612                                 const char *new_name = find_rename(commit, p);
613                                 if (new_name) {
614                                         struct util_info *putil = get_util(p);
615                                         if (!putil->pathname)
616                                                 putil->pathname = xstrdup(new_name);
617                                 }
618                                 else {
619                                         *pp = parent->next;
620                                         continue;
621                                 }
622                         }
623                 }
624
625                 /* fallthrough */
626                 case REV_TREE_DIFFERENT:
627                         pp = &parent->next;
628                         if (!get_util(p)->pathname)
629                                 get_util(p)->pathname =
630                                         get_util(commit)->pathname;
631                         continue;
632                 }
633                 die("bad tree compare for commit %s",
634                     sha1_to_hex(commit->object.sha1));
635         }
636         commit->object.flags |= TREECHANGE;
637 }
638
639 struct commit_info
640 {
641         char *author;
642         char *author_mail;
643         unsigned long author_time;
644         char *author_tz;
645 };
646
647 static void get_commit_info(struct commit *commit, struct commit_info *ret)
648 {
649         int len;
650         char *tmp;
651         static char author_buf[1024];
652
653         tmp = strstr(commit->buffer, "\nauthor ") + 8;
654         len = strchr(tmp, '\n') - tmp;
655         ret->author = author_buf;
656         memcpy(ret->author, tmp, len);
657
658         tmp = ret->author;
659         tmp += len;
660         *tmp = 0;
661         while (*tmp != ' ')
662                 tmp--;
663         ret->author_tz = tmp+1;
664
665         *tmp = 0;
666         while (*tmp != ' ')
667                 tmp--;
668         ret->author_time = strtoul(tmp, NULL, 10);
669
670         *tmp = 0;
671         while (*tmp != ' ')
672                 tmp--;
673         ret->author_mail = tmp + 1;
674
675         *tmp = 0;
676 }
677
678 static const char *format_time(unsigned long time, const char *tz_str,
679                                int show_raw_time)
680 {
681         static char time_buf[128];
682         time_t t = time;
683         int minutes, tz;
684         struct tm *tm;
685
686         if (show_raw_time) {
687                 sprintf(time_buf, "%lu %s", time, tz_str);
688                 return time_buf;
689         }
690
691         tz = atoi(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;
696         tm = gmtime(&t);
697
698         strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
699         strcat(time_buf, tz_str);
700         return time_buf;
701 }
702
703 static void topo_setter(struct commit *c, void *data)
704 {
705         struct util_info *util = c->util;
706         util->topo_data = data;
707 }
708
709 static void *topo_getter(struct commit *c)
710 {
711         struct util_info *util = c->util;
712         return util->topo_data;
713 }
714
715 static int read_ancestry(const char *graft_file,
716                          unsigned char **start_sha1)
717 {
718         FILE *fp = fopen(graft_file, "r");
719         char buf[1024];
720         if (!fp)
721                 return -1;
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);
727                 if (!*start_sha1)
728                         *start_sha1 = graft->sha1;
729         }
730         fclose(fp);
731         return 0;
732 }
733
734 static int lineno_width(int lines)
735 {
736         int i, width;
737
738         for (width = 1, i = 10; i <= lines + 1; width++)
739                 i *= 10;
740         return width;
741 }
742
743 static int find_orig_linenum(struct util_info *u, int lineno)
744 {
745         int i;
746
747         for (i = 0; i < u->num_lines; i++)
748                 if (lineno == u->line_map[i])
749                         return i + 1;
750         return 0;
751 }
752
753 int main(int argc, const char **argv)
754 {
755         int i;
756         struct commit *initial = NULL;
757         unsigned char sha1[20], *sha1_p = NULL;
758
759         const char *filename = NULL, *commit = NULL;
760         char filename_buf[256];
761         int sha1_len = 8;
762         int compatibility = 0;
763         int show_raw_time = 0;
764         int options = 1;
765         struct commit *start_commit;
766
767         const char *args[10];
768         struct rev_info rev;
769
770         struct commit_info ci;
771         const char *buf;
772         int max_digits, max_orig_digits;
773         int longest_file, longest_author, longest_file_lines;
774         int show_name = 0;
775         int show_number = 0;
776
777         const char *prefix = setup_git_directory();
778         git_config(git_default_config);
779
780         for (i = 1; i < argc; i++) {
781                 if (options) {
782                         if (!strcmp(argv[i], "-h") ||
783                            !strcmp(argv[i], "--help"))
784                                 usage(blame_usage);
785                         if (!strcmp(argv[i], "-l") ||
786                             !strcmp(argv[i], "--long")) {
787                                 sha1_len = 40;
788                                 continue;
789                         }
790                         if (!strcmp(argv[i], "-c") ||
791                             !strcmp(argv[i], "--compatibility")) {
792                                 compatibility = 1;
793                                 continue;
794                         }
795                         if (!strcmp(argv[i], "-t") ||
796                             !strcmp(argv[i], "--time")) {
797                                 show_raw_time = 1;
798                                 continue;
799                         }
800                         if (!strcmp(argv[i], "-S")) {
801                                 if (i + 1 < argc &&
802                                     !read_ancestry(argv[i + 1], &sha1_p)) {
803                                         compatibility = 1;
804                                         i++;
805                                         continue;
806                                 }
807                                 usage(blame_usage);
808                         }
809                         if (!strcmp(argv[i], "-f") ||
810                             !strcmp(argv[i], "--show-name")) {
811                                 show_name = 1;
812                                 continue;
813                         }
814                         if (!strcmp(argv[i], "-n") ||
815                             !strcmp(argv[i], "--show-number")) {
816                                 show_number = 1;
817                                 continue;
818                         }
819                         if (!strcmp(argv[i], "--")) {
820                                 options = 0;
821                                 continue;
822                         }
823                         if (argv[i][0] == '-')
824                                 usage(blame_usage);
825                         options = 0;
826                 }
827
828                 if (!options) {
829                         if (!filename)
830                                 filename = argv[i];
831                         else if (!commit)
832                                 commit = argv[i];
833                         else
834                                 usage(blame_usage);
835                 }
836         }
837
838         if (!filename)
839                 usage(blame_usage);
840         if (commit && sha1_p)
841                 usage(blame_usage);
842         else if (!commit)
843                 commit = "HEAD";
844
845         if (prefix)
846                 sprintf(filename_buf, "%s%s", prefix, filename);
847         else
848                 strcpy(filename_buf, filename);
849         filename = filename_buf;
850
851         if (!sha1_p) {
852                 if (get_sha1(commit, sha1))
853                         die("get_sha1 failed, commit '%s' not found", commit);
854                 sha1_p = sha1;
855         }
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);
860                 return 1;
861         }
862
863         init_revisions(&rev, setup_git_directory());
864         rev.remove_empty_trees = 1;
865         rev.topo_order = 1;
866         rev.prune_fn = simplify_commit;
867         rev.topo_setter = topo_setter;
868         rev.topo_getter = topo_getter;
869         rev.parents = 1;
870         rev.limited = 1;
871
872         commit_list_insert(start_commit, &rev.commits);
873
874         args[0] = filename;
875         args[1] = NULL;
876         diff_tree_setup_paths(args, &rev.pruning);
877         prepare_revision_walk(&rev);
878         process_commits(&rev, filename, &initial);
879
880         buf = blame_contents;
881         max_digits = lineno_width(num_blame_lines);
882
883         longest_file = 0;
884         longest_author = 0;
885         longest_file_lines = 0;
886         for (i = 0; i < num_blame_lines; i++) {
887                 struct commit *c = blame_lines[i];
888                 struct util_info *u;
889                 if (!c)
890                         c = initial;
891                 u = c->util;
892
893                 if (!show_name && strcmp(filename, u->pathname))
894                         show_name = 1;
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);
902         }
903
904         max_orig_digits = lineno_width(longest_file_lines);
905
906         for (i = 0; i < num_blame_lines; i++) {
907                 struct commit *c = blame_lines[i];
908                 struct util_info *u;
909                 int lineno;
910                 if (!c)
911                         c = initial;
912                 u = c->util;
913                 lineno = find_orig_linenum(u, i);
914
915                 get_commit_info(c, &ci);
916                 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
917                 if (compatibility) {
918                         printf("\t(%10s\t%10s\t%d)", ci.author,
919                                format_time(ci.author_time, ci.author_tz,
920                                            show_raw_time),
921                                i+1);
922                 }
923                 else {
924                         if (show_name)
925                                 printf(" %-*.*s", longest_file, longest_file,
926                                        u->pathname);
927                         if (show_number)
928                                 printf(" %*d", max_orig_digits,
929                                        lineno);
930                         printf(" (%-*.*s %10s %*d) ",
931                                longest_author, longest_author, ci.author,
932                                format_time(ci.author_time, ci.author_tz,
933                                            show_raw_time),
934                                max_digits, i+1);
935                 }
936
937                 if (i == num_blame_lines - 1) {
938                         fwrite(buf, blame_len - (buf - blame_contents),
939                                1, stdout);
940                         if (blame_contents[blame_len-1] != '\n')
941                                 putc('\n', stdout);
942                 }
943                 else {
944                         char *next_buf = strchr(buf, '\n') + 1;
945                         fwrite(buf, next_buf - buf, 1, stdout);
946                         buf = next_buf;
947                 }
948         }
949
950         if (DEBUG) {
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));
955         }
956
957         return 0;
958 }