Code

blame.c: move code to output metainfo into a separate function.
[git.git] / blame.c
1 /*
2  * Copyright (C) 2006, Fredrik Kuivinen <freku045@student.liu.se>
3  */
5 #include <assert.h>
6 #include <time.h>
7 #include <sys/time.h>
8 #include <math.h>
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"
21 #define DEBUG 0
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;
34 static int blame_len;
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;
44         void *topo_data;
45 };
47 struct chunk {
48         int off1, len1; /* --- */
49         int off2, len2; /* +++ */
50 };
52 struct patch {
53         struct chunk *chunks;
54         int num;
55 };
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;
66         struct patch *ret;
67 };
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;
74         if (len < 4 || line[0] != '@' || line[1] != '@')
75                 return;
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];
84         assert(!strncmp(line, "@@ -", 4));
86         if (parse_hunk_header(line, len,
87                               &chunk->off1, &chunk->len1,
88                               &chunk->off2, &chunk->len2)) {
89                 state->ret->num--;
90                 return;
91         }
93         if (chunk->len1 == 0)
94                 chunk->off1++;
95         if (chunk->len2 == 0)
96                 chunk->off2++;
98         if (chunk->off1 > 0)
99                 chunk->off1--;
100         if (chunk->off2 > 0)
101                 chunk->off2--;
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;
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;
118         get_blob(commit);
119         file_c.ptr = info_c->buf;
120         file_c.size = info_c->size;
122         get_blob(other);
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;
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;
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;
145         num_get_patch++;
146         return state.ret;
149 static void free_patch(struct patch *p)
151         free(p->chunks);
152         free(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,
162                          unsigned char *sha1)
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);
171         if (is_null_sha1(blob_sha1))
172                 return -1;
174         hashcpy(sha1, blob_sha1);
175         return 0;
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)
182         if (S_ISDIR(mode))
183                 return READ_TREE_RECURSIVE;
185         if (strncmp(blame_file, base, baselen) ||
186             strcmp(blame_file + baselen, pathname))
187                 return -1;
189         hashcpy(blob_sha1, sha1);
190         return -1;
193 static void get_blob(struct commit *commit)
195         struct util_info *info = commit->util;
196         char type[20];
198         if (info->buf)
199                 return;
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)
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         }
217 #if DEBUG
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;
224         int i;
225         int max =
226             util->num_lines >
227             util2->num_lines ? util->num_lines : util2->num_lines;
228         int num;
230         for (i = 0; i < max; i++) {
231                 printf("i: %d ", i);
232                 num = -1;
234                 if (i < util->num_lines) {
235                         num = util->line_map[i];
236                         printf("%d\t", num);
237                 }
238                 else
239                         printf("\t");
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");
250                 printf("\n");
251         }
253 #endif
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)
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;
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         }
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);
284                         i1--;
285                         i2--;
286                         if (chunk->len1 > 0)
287                                 i1 += chunk->len1;
289                         if (chunk->len2 > 0)
290                                 i2 += chunk->len2;
292                         cur_chunk++;
293                 }
294                 else {
295                         if (i2 >= util2->num_lines)
296                                 break;
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                         }
311                         if (map[i1] == -1 && map2[i2] != -1)
312                                 map[i1] = map2[i2];
313                 }
315                 if (DEBUG > 1)
316                         printf("l1: %d l2: %d i1: %d i2: %d\n",
317                                map[i1], map2[i2], i1, i2);
318         }
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;
332         if (util)
333                 return util;
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;
345 static int fill_util_info(struct commit *commit)
347         struct util_info *util = commit->util;
349         assert(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;
358         int i;
360         if (util->line_map)
361                 return;
363         get_blob(commit);
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++;
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;
382         int i;
384         util->pathname = filename;
385         if (fill_util_info(commit))
386                 die("fill_util_info failed");
388         alloc_line_map(commit);
390         util = commit->util;
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)
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;
406         struct commit *commit = get_revision(rev);
407         assert(commit);
408         init_first_commit(commit, path);
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;
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);
422         do {
423                 struct commit_list *parents;
424                 int num_parents;
425                 struct util_info *util;
427                 if (DEBUG)
428                         printf("\nProcessing commit: %d %s\n", num_commits,
429                                sha1_to_hex(commit->object.sha1));
431                 if (lines_left == 0)
432                         return;
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++;
442                 if (num_parents == 0)
443                         *initial = commit;
445                 if (fill_util_info(commit))
446                         continue;
448                 alloc_line_map(commit);
449                 util = commit->util;
451                 for (parents = commit->parents;
452                      parents != NULL; parents = parents->next) {
453                         struct commit *parent = parents->item;
454                         struct patch *patch;
456                         if (parse_commit(parent) < 0)
457                                 die("parse_commit error");
459                         if (DEBUG)
460                                 printf("parent: %s\n",
461                                        sha1_to_hex(parent->object.sha1));
463                         if (fill_util_info(parent)) {
464                                 num_parents--;
465                                 continue;
466                         }
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++) {
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                 }
487                 if (DEBUG)
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;
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);
503 static int compare_tree_path(struct rev_info *revs,
504                              struct commit *c1, struct commit *c2)
506         int ret;
507         const char *paths[2];
508         struct util_info *util = c2->util;
509         paths[0] = util->pathname;
510         paths[1] = NULL;
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;
519 static int same_tree_as_empty_path(struct rev_info *revs, struct tree *t1,
520                                    const char *path)
522         int ret;
523         const char *paths[2];
524         paths[0] = path;
525         paths[1] = NULL;
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;
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];
539         int i;
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         }
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");
555         diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
556                        "", &diff_opts);
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)) {
564                         if (DEBUG)
565                                 printf("rename %s -> %s\n",
566                                        p->one->path, p->two->path);
567                         return p->two->path;
568                 }
569         }
571         return 0;
574 static void simplify_commit(struct rev_info *revs, struct commit *commit)
576         struct commit_list **pp, *parent;
578         if (!commit->tree)
579                 return;
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         }
589         pp = &commit->parents;
590         while ((parent = *pp) != NULL) {
591                 struct commit *p = parent->item;
593                 if (p->object.flags & UNINTERESTING) {
594                         pp = &parent->next;
595                         continue;
596                 }
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;
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                 }
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;
639 struct commit_info
641         char *author;
642         char *author_mail;
643         unsigned long author_time;
644         char *author_tz;
645 };
647 static void get_commit_info(struct commit *commit, struct commit_info *ret)
649         int len;
650         char *tmp;
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);
658         tmp = ret->author;
659         tmp += len;
660         *tmp = 0;
661         while (*tmp != ' ')
662                 tmp--;
663         ret->author_tz = tmp+1;
665         *tmp = 0;
666         while (*tmp != ' ')
667                 tmp--;
668         ret->author_time = strtoul(tmp, NULL, 10);
670         *tmp = 0;
671         while (*tmp != ' ')
672                 tmp--;
673         ret->author_mail = tmp + 1;
675         *tmp = 0;
678 static const char *format_time(unsigned long time, const char *tz_str,
679                                int show_raw_time)
681         static char time_buf[128];
682         time_t t = time;
683         int minutes, tz;
684         struct tm *tm;
686         if (show_raw_time) {
687                 sprintf(time_buf, "%lu %s", time, tz_str);
688                 return time_buf;
689         }
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);
698         strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
699         strcat(time_buf, tz_str);
700         return time_buf;
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");
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;
734 static int lineno_width(int lines)
736         int i, width;
738         for (width = 1, i = 10; i <= lines + 1; width++)
739                 i *= 10;
740         return width;
743 static int find_orig_linenum(struct util_info *u, int lineno)
745         int i;
747         for (i = 0; i < u->num_lines; i++)
748                 if (lineno == u->line_map[i])
749                         return i + 1;
750         return 0;
753 static void emit_meta(struct commit *c, int lno,
754                       int sha1_len, int compatibility,
755                       int show_name, int show_number, int show_raw_time,
756                       int longest_file, int longest_author,
757                       int max_digits, int max_orig_digits)
759         struct util_info *u;
760         int lineno;
761         struct commit_info ci;
763         u = c->util;
764         lineno = find_orig_linenum(u, lno);
766         get_commit_info(c, &ci);
767         fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
768         if (compatibility) {
769                 printf("\t(%10s\t%10s\t%d)", ci.author,
770                        format_time(ci.author_time, ci.author_tz,
771                                    show_raw_time),
772                        lno + 1);
773         }
774         else {
775                 if (show_name)
776                         printf(" %-*.*s", longest_file, longest_file,
777                                u->pathname);
778                 if (show_number)
779                         printf(" %*d", max_orig_digits,
780                                lineno);
781                 printf(" (%-*.*s %10s %*d) ",
782                        longest_author, longest_author, ci.author,
783                        format_time(ci.author_time, ci.author_tz,
784                                    show_raw_time),
785                        max_digits, lno + 1);
786         }
789 int main(int argc, const char **argv)
791         int i;
792         struct commit *initial = NULL;
793         unsigned char sha1[20], *sha1_p = NULL;
795         const char *filename = NULL, *commit = NULL;
796         char filename_buf[256];
797         int sha1_len = 8;
798         int compatibility = 0;
799         int show_raw_time = 0;
800         int options = 1;
801         struct commit *start_commit;
803         const char *args[10];
804         struct rev_info rev;
806         struct commit_info ci;
807         const char *buf;
808         int max_digits, max_orig_digits;
809         int longest_file, longest_author, longest_file_lines;
810         int show_name = 0;
811         int show_number = 0;
813         const char *prefix = setup_git_directory();
814         git_config(git_default_config);
816         for (i = 1; i < argc; i++) {
817                 if (options) {
818                         if (!strcmp(argv[i], "-h") ||
819                            !strcmp(argv[i], "--help"))
820                                 usage(blame_usage);
821                         if (!strcmp(argv[i], "-l") ||
822                             !strcmp(argv[i], "--long")) {
823                                 sha1_len = 40;
824                                 continue;
825                         }
826                         if (!strcmp(argv[i], "-c") ||
827                             !strcmp(argv[i], "--compatibility")) {
828                                 compatibility = 1;
829                                 continue;
830                         }
831                         if (!strcmp(argv[i], "-t") ||
832                             !strcmp(argv[i], "--time")) {
833                                 show_raw_time = 1;
834                                 continue;
835                         }
836                         if (!strcmp(argv[i], "-S")) {
837                                 if (i + 1 < argc &&
838                                     !read_ancestry(argv[i + 1], &sha1_p)) {
839                                         compatibility = 1;
840                                         i++;
841                                         continue;
842                                 }
843                                 usage(blame_usage);
844                         }
845                         if (!strcmp(argv[i], "-f") ||
846                             !strcmp(argv[i], "--show-name")) {
847                                 show_name = 1;
848                                 continue;
849                         }
850                         if (!strcmp(argv[i], "-n") ||
851                             !strcmp(argv[i], "--show-number")) {
852                                 show_number = 1;
853                                 continue;
854                         }
855                         if (!strcmp(argv[i], "--")) {
856                                 options = 0;
857                                 continue;
858                         }
859                         if (argv[i][0] == '-')
860                                 usage(blame_usage);
861                         options = 0;
862                 }
864                 if (!options) {
865                         if (!filename)
866                                 filename = argv[i];
867                         else if (!commit)
868                                 commit = argv[i];
869                         else
870                                 usage(blame_usage);
871                 }
872         }
874         if (!filename)
875                 usage(blame_usage);
876         if (commit && sha1_p)
877                 usage(blame_usage);
878         else if (!commit)
879                 commit = "HEAD";
881         if (prefix)
882                 sprintf(filename_buf, "%s%s", prefix, filename);
883         else
884                 strcpy(filename_buf, filename);
885         filename = filename_buf;
887         if (!sha1_p) {
888                 if (get_sha1(commit, sha1))
889                         die("get_sha1 failed, commit '%s' not found", commit);
890                 sha1_p = sha1;
891         }
892         start_commit = lookup_commit_reference(sha1_p);
893         get_util(start_commit)->pathname = filename;
894         if (fill_util_info(start_commit)) {
895                 printf("%s not found in %s\n", filename, commit);
896                 return 1;
897         }
899         init_revisions(&rev, setup_git_directory());
900         rev.remove_empty_trees = 1;
901         rev.topo_order = 1;
902         rev.prune_fn = simplify_commit;
903         rev.topo_setter = topo_setter;
904         rev.topo_getter = topo_getter;
905         rev.parents = 1;
906         rev.limited = 1;
908         commit_list_insert(start_commit, &rev.commits);
910         args[0] = filename;
911         args[1] = NULL;
912         diff_tree_setup_paths(args, &rev.pruning);
913         prepare_revision_walk(&rev);
914         process_commits(&rev, filename, &initial);
916         for (i = 0; i < num_blame_lines; i++)
917                 if (!blame_lines[i])
918                         blame_lines[i] = initial;
920         buf = blame_contents;
921         max_digits = lineno_width(num_blame_lines);
923         longest_file = 0;
924         longest_author = 0;
925         longest_file_lines = 0;
926         for (i = 0; i < num_blame_lines; i++) {
927                 struct commit *c = blame_lines[i];
928                 struct util_info *u;
929                 u = c->util;
931                 if (!show_name && strcmp(filename, u->pathname))
932                         show_name = 1;
933                 if (longest_file < strlen(u->pathname))
934                         longest_file = strlen(u->pathname);
935                 if (longest_file_lines < u->num_lines)
936                         longest_file_lines = u->num_lines;
937                 get_commit_info(c, &ci);
938                 if (longest_author < strlen(ci.author))
939                         longest_author = strlen(ci.author);
940         }
942         max_orig_digits = lineno_width(longest_file_lines);
944         for (i = 0; i < num_blame_lines; i++) {
945                 emit_meta(blame_lines[i], i,
946                           sha1_len, compatibility,
947                           show_name, show_number, show_raw_time,
948                           longest_file, longest_author,
949                           max_digits, max_orig_digits);
951                 if (i == num_blame_lines - 1) {
952                         fwrite(buf, blame_len - (buf - blame_contents),
953                                1, stdout);
954                         if (blame_contents[blame_len-1] != '\n')
955                                 putc('\n', stdout);
956                 }
957                 else {
958                         char *next_buf = strchr(buf, '\n') + 1;
959                         fwrite(buf, next_buf - buf, 1, stdout);
960                         buf = next_buf;
961                 }
962         }
964         if (DEBUG) {
965                 printf("num get patch: %d\n", num_get_patch);
966                 printf("num commits: %d\n", num_commits);
967                 printf("patch time: %f\n", patch_time / 1000000.0);
968                 printf("initial: %s\n", sha1_to_hex(initial->object.sha1));
969         }
971         return 0;