Add support for an info version of the user manual
[git] / builtin-blame.c
1 /*
2  * Pickaxe
3  *
4  * Copyright (c) 2006, Junio C Hamano
5  */
6
7 #include "cache.h"
8 #include "builtin.h"
9 #include "blob.h"
10 #include "commit.h"
11 #include "tag.h"
12 #include "tree-walk.h"
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "revision.h"
16 #include "quote.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
19 #include "path-list.h"
20 #include "mailmap.h"
21
22 static char blame_usage[] =
23 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
24 "  -c                  Use the same output mode as git-annotate (Default: off)\n"
25 "  -b                  Show blank SHA-1 for boundary commits (Default: off)\n"
26 "  -l                  Show long commit SHA1 (Default: off)\n"
27 "  --root              Do not treat root commits as boundaries (Default: off)\n"
28 "  -t                  Show raw timestamp (Default: off)\n"
29 "  -f, --show-name     Show original filename (Default: auto)\n"
30 "  -n, --show-number   Show original linenumber (Default: off)\n"
31 "  -s                  Suppress author name and timestamp (Default: off)\n"
32 "  -p, --porcelain     Show in a format designed for machine consumption\n"
33 "  -w                  Ignore whitespace differences\n"
34 "  -L n,m              Process only line range n,m, counting from 1\n"
35 "  -M, -C              Find line movements within and across files\n"
36 "  --incremental       Show blame entries as we find them, incrementally\n"
37 "  --contents file     Use <file>'s contents as the final image\n"
38 "  -S revs-file        Use revisions from revs-file instead of calling git-rev-list\n";
39
40 static int longest_file;
41 static int longest_author;
42 static int max_orig_digits;
43 static int max_digits;
44 static int max_score_digits;
45 static int show_root;
46 static int blank_boundary;
47 static int incremental;
48 static int cmd_is_annotate;
49 static int xdl_opts = XDF_NEED_MINIMAL;
50 static struct path_list mailmap;
51
52 #ifndef DEBUG
53 #define DEBUG 0
54 #endif
55
56 /* stats */
57 static int num_read_blob;
58 static int num_get_patch;
59 static int num_commits;
60
61 #define PICKAXE_BLAME_MOVE              01
62 #define PICKAXE_BLAME_COPY              02
63 #define PICKAXE_BLAME_COPY_HARDER       04
64 #define PICKAXE_BLAME_COPY_HARDEST      010
65
66 /*
67  * blame for a blame_entry with score lower than these thresholds
68  * is not passed to the parent using move/copy logic.
69  */
70 static unsigned blame_move_score;
71 static unsigned blame_copy_score;
72 #define BLAME_DEFAULT_MOVE_SCORE        20
73 #define BLAME_DEFAULT_COPY_SCORE        40
74
75 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76 #define METAINFO_SHOWN          (1u<<12)
77 #define MORE_THAN_ONE_PATH      (1u<<13)
78
79 /*
80  * One blob in a commit that is being suspected
81  */
82 struct origin {
83         int refcnt;
84         struct commit *commit;
85         mmfile_t file;
86         unsigned char blob_sha1[20];
87         char path[FLEX_ARRAY];
88 };
89
90 /*
91  * Given an origin, prepare mmfile_t structure to be used by the
92  * diff machinery
93  */
94 static char *fill_origin_blob(struct origin *o, mmfile_t *file)
95 {
96         if (!o->file.ptr) {
97                 enum object_type type;
98                 num_read_blob++;
99                 file->ptr = read_sha1_file(o->blob_sha1, &type,
100                                            (unsigned long *)(&(file->size)));
101                 o->file = *file;
102         }
103         else
104                 *file = o->file;
105         return file->ptr;
106 }
107
108 /*
109  * Origin is refcounted and usually we keep the blob contents to be
110  * reused.
111  */
112 static inline struct origin *origin_incref(struct origin *o)
113 {
114         if (o)
115                 o->refcnt++;
116         return o;
117 }
118
119 static void origin_decref(struct origin *o)
120 {
121         if (o && --o->refcnt <= 0) {
122                 if (o->file.ptr)
123                         free(o->file.ptr);
124                 memset(o, 0, sizeof(*o));
125                 free(o);
126         }
127 }
128
129 /*
130  * Each group of lines is described by a blame_entry; it can be split
131  * as we pass blame to the parents.  They form a linked list in the
132  * scoreboard structure, sorted by the target line number.
133  */
134 struct blame_entry {
135         struct blame_entry *prev;
136         struct blame_entry *next;
137
138         /* the first line of this group in the final image;
139          * internally all line numbers are 0 based.
140          */
141         int lno;
142
143         /* how many lines this group has */
144         int num_lines;
145
146         /* the commit that introduced this group into the final image */
147         struct origin *suspect;
148
149         /* true if the suspect is truly guilty; false while we have not
150          * checked if the group came from one of its parents.
151          */
152         char guilty;
153
154         /* the line number of the first line of this group in the
155          * suspect's file; internally all line numbers are 0 based.
156          */
157         int s_lno;
158
159         /* how significant this entry is -- cached to avoid
160          * scanning the lines over and over.
161          */
162         unsigned score;
163 };
164
165 /*
166  * The current state of the blame assignment.
167  */
168 struct scoreboard {
169         /* the final commit (i.e. where we started digging from) */
170         struct commit *final;
171
172         const char *path;
173
174         /*
175          * The contents in the final image.
176          * Used by many functions to obtain contents of the nth line,
177          * indexed with scoreboard.lineno[blame_entry.lno].
178          */
179         const char *final_buf;
180         unsigned long final_buf_size;
181
182         /* linked list of blames */
183         struct blame_entry *ent;
184
185         /* look-up a line in the final buffer */
186         int num_lines;
187         int *lineno;
188 };
189
190 static inline int same_suspect(struct origin *a, struct origin *b)
191 {
192         if (a == b)
193                 return 1;
194         if (a->commit != b->commit)
195                 return 0;
196         return !strcmp(a->path, b->path);
197 }
198
199 static void sanity_check_refcnt(struct scoreboard *);
200
201 /*
202  * If two blame entries that are next to each other came from
203  * contiguous lines in the same origin (i.e. <commit, path> pair),
204  * merge them together.
205  */
206 static void coalesce(struct scoreboard *sb)
207 {
208         struct blame_entry *ent, *next;
209
210         for (ent = sb->ent; ent && (next = ent->next); ent = next) {
211                 if (same_suspect(ent->suspect, next->suspect) &&
212                     ent->guilty == next->guilty &&
213                     ent->s_lno + ent->num_lines == next->s_lno) {
214                         ent->num_lines += next->num_lines;
215                         ent->next = next->next;
216                         if (ent->next)
217                                 ent->next->prev = ent;
218                         origin_decref(next->suspect);
219                         free(next);
220                         ent->score = 0;
221                         next = ent; /* again */
222                 }
223         }
224
225         if (DEBUG) /* sanity */
226                 sanity_check_refcnt(sb);
227 }
228
229 /*
230  * Given a commit and a path in it, create a new origin structure.
231  * The callers that add blame to the scoreboard should use
232  * get_origin() to obtain shared, refcounted copy instead of calling
233  * this function directly.
234  */
235 static struct origin *make_origin(struct commit *commit, const char *path)
236 {
237         struct origin *o;
238         o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
239         o->commit = commit;
240         o->refcnt = 1;
241         strcpy(o->path, path);
242         return o;
243 }
244
245 /*
246  * Locate an existing origin or create a new one.
247  */
248 static struct origin *get_origin(struct scoreboard *sb,
249                                  struct commit *commit,
250                                  const char *path)
251 {
252         struct blame_entry *e;
253
254         for (e = sb->ent; e; e = e->next) {
255                 if (e->suspect->commit == commit &&
256                     !strcmp(e->suspect->path, path))
257                         return origin_incref(e->suspect);
258         }
259         return make_origin(commit, path);
260 }
261
262 /*
263  * Fill the blob_sha1 field of an origin if it hasn't, so that later
264  * call to fill_origin_blob() can use it to locate the data.  blob_sha1
265  * for an origin is also used to pass the blame for the entire file to
266  * the parent to detect the case where a child's blob is identical to
267  * that of its parent's.
268  */
269 static int fill_blob_sha1(struct origin *origin)
270 {
271         unsigned mode;
272
273         if (!is_null_sha1(origin->blob_sha1))
274                 return 0;
275         if (get_tree_entry(origin->commit->object.sha1,
276                            origin->path,
277                            origin->blob_sha1, &mode))
278                 goto error_out;
279         if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
280                 goto error_out;
281         return 0;
282  error_out:
283         hashclr(origin->blob_sha1);
284         return -1;
285 }
286
287 /*
288  * We have an origin -- check if the same path exists in the
289  * parent and return an origin structure to represent it.
290  */
291 static struct origin *find_origin(struct scoreboard *sb,
292                                   struct commit *parent,
293                                   struct origin *origin)
294 {
295         struct origin *porigin = NULL;
296         struct diff_options diff_opts;
297         const char *paths[2];
298
299         if (parent->util) {
300                 /*
301                  * Each commit object can cache one origin in that
302                  * commit.  This is a freestanding copy of origin and
303                  * not refcounted.
304                  */
305                 struct origin *cached = parent->util;
306                 if (!strcmp(cached->path, origin->path)) {
307                         /*
308                          * The same path between origin and its parent
309                          * without renaming -- the most common case.
310                          */
311                         porigin = get_origin(sb, parent, cached->path);
312
313                         /*
314                          * If the origin was newly created (i.e. get_origin
315                          * would call make_origin if none is found in the
316                          * scoreboard), it does not know the blob_sha1,
317                          * so copy it.  Otherwise porigin was in the
318                          * scoreboard and already knows blob_sha1.
319                          */
320                         if (porigin->refcnt == 1)
321                                 hashcpy(porigin->blob_sha1, cached->blob_sha1);
322                         return porigin;
323                 }
324                 /* otherwise it was not very useful; free it */
325                 free(parent->util);
326                 parent->util = NULL;
327         }
328
329         /* See if the origin->path is different between parent
330          * and origin first.  Most of the time they are the
331          * same and diff-tree is fairly efficient about this.
332          */
333         diff_setup(&diff_opts);
334         diff_opts.recursive = 1;
335         diff_opts.detect_rename = 0;
336         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
337         paths[0] = origin->path;
338         paths[1] = NULL;
339
340         diff_tree_setup_paths(paths, &diff_opts);
341         if (diff_setup_done(&diff_opts) < 0)
342                 die("diff-setup");
343
344         if (is_null_sha1(origin->commit->object.sha1))
345                 do_diff_cache(parent->tree->object.sha1, &diff_opts);
346         else
347                 diff_tree_sha1(parent->tree->object.sha1,
348                                origin->commit->tree->object.sha1,
349                                "", &diff_opts);
350         diffcore_std(&diff_opts);
351
352         /* It is either one entry that says "modified", or "created",
353          * or nothing.
354          */
355         if (!diff_queued_diff.nr) {
356                 /* The path is the same as parent */
357                 porigin = get_origin(sb, parent, origin->path);
358                 hashcpy(porigin->blob_sha1, origin->blob_sha1);
359         }
360         else if (diff_queued_diff.nr != 1)
361                 die("internal error in blame::find_origin");
362         else {
363                 struct diff_filepair *p = diff_queued_diff.queue[0];
364                 switch (p->status) {
365                 default:
366                         die("internal error in blame::find_origin (%c)",
367                             p->status);
368                 case 'M':
369                         porigin = get_origin(sb, parent, origin->path);
370                         hashcpy(porigin->blob_sha1, p->one->sha1);
371                         break;
372                 case 'A':
373                 case 'T':
374                         /* Did not exist in parent, or type changed */
375                         break;
376                 }
377         }
378         diff_flush(&diff_opts);
379         if (porigin) {
380                 /*
381                  * Create a freestanding copy that is not part of
382                  * the refcounted origin found in the scoreboard, and
383                  * cache it in the commit.
384                  */
385                 struct origin *cached;
386
387                 cached = make_origin(porigin->commit, porigin->path);
388                 hashcpy(cached->blob_sha1, porigin->blob_sha1);
389                 parent->util = cached;
390         }
391         return porigin;
392 }
393
394 /*
395  * We have an origin -- find the path that corresponds to it in its
396  * parent and return an origin structure to represent it.
397  */
398 static struct origin *find_rename(struct scoreboard *sb,
399                                   struct commit *parent,
400                                   struct origin *origin)
401 {
402         struct origin *porigin = NULL;
403         struct diff_options diff_opts;
404         int i;
405         const char *paths[2];
406
407         diff_setup(&diff_opts);
408         diff_opts.recursive = 1;
409         diff_opts.detect_rename = DIFF_DETECT_RENAME;
410         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
411         diff_opts.single_follow = origin->path;
412         paths[0] = NULL;
413         diff_tree_setup_paths(paths, &diff_opts);
414         if (diff_setup_done(&diff_opts) < 0)
415                 die("diff-setup");
416
417         if (is_null_sha1(origin->commit->object.sha1))
418                 do_diff_cache(parent->tree->object.sha1, &diff_opts);
419         else
420                 diff_tree_sha1(parent->tree->object.sha1,
421                                origin->commit->tree->object.sha1,
422                                "", &diff_opts);
423         diffcore_std(&diff_opts);
424
425         for (i = 0; i < diff_queued_diff.nr; i++) {
426                 struct diff_filepair *p = diff_queued_diff.queue[i];
427                 if ((p->status == 'R' || p->status == 'C') &&
428                     !strcmp(p->two->path, origin->path)) {
429                         porigin = get_origin(sb, parent, p->one->path);
430                         hashcpy(porigin->blob_sha1, p->one->sha1);
431                         break;
432                 }
433         }
434         diff_flush(&diff_opts);
435         return porigin;
436 }
437
438 /*
439  * Parsing of patch chunks...
440  */
441 struct chunk {
442         /* line number in postimage; up to but not including this
443          * line is the same as preimage
444          */
445         int same;
446
447         /* preimage line number after this chunk */
448         int p_next;
449
450         /* postimage line number after this chunk */
451         int t_next;
452 };
453
454 struct patch {
455         struct chunk *chunks;
456         int num;
457 };
458
459 struct blame_diff_state {
460         struct xdiff_emit_state xm;
461         struct patch *ret;
462         unsigned hunk_post_context;
463         unsigned hunk_in_pre_context : 1;
464 };
465
466 static void process_u_diff(void *state_, char *line, unsigned long len)
467 {
468         struct blame_diff_state *state = state_;
469         struct chunk *chunk;
470         int off1, off2, len1, len2, num;
471
472         num = state->ret->num;
473         if (len < 4 || line[0] != '@' || line[1] != '@') {
474                 if (state->hunk_in_pre_context && line[0] == ' ')
475                         state->ret->chunks[num - 1].same++;
476                 else {
477                         state->hunk_in_pre_context = 0;
478                         if (line[0] == ' ')
479                                 state->hunk_post_context++;
480                         else
481                                 state->hunk_post_context = 0;
482                 }
483                 return;
484         }
485
486         if (num && state->hunk_post_context) {
487                 chunk = &state->ret->chunks[num - 1];
488                 chunk->p_next -= state->hunk_post_context;
489                 chunk->t_next -= state->hunk_post_context;
490         }
491         state->ret->num = ++num;
492         state->ret->chunks = xrealloc(state->ret->chunks,
493                                       sizeof(struct chunk) * num);
494         chunk = &state->ret->chunks[num - 1];
495         if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) {
496                 state->ret->num--;
497                 return;
498         }
499
500         /* Line numbers in patch output are one based. */
501         off1--;
502         off2--;
503
504         chunk->same = len2 ? off2 : (off2 + 1);
505
506         chunk->p_next = off1 + (len1 ? len1 : 1);
507         chunk->t_next = chunk->same + len2;
508         state->hunk_in_pre_context = 1;
509         state->hunk_post_context = 0;
510 }
511
512 static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
513                                     int context)
514 {
515         struct blame_diff_state state;
516         xpparam_t xpp;
517         xdemitconf_t xecfg;
518         xdemitcb_t ecb;
519
520         xpp.flags = xdl_opts;
521         memset(&xecfg, 0, sizeof(xecfg));
522         xecfg.ctxlen = context;
523         ecb.outf = xdiff_outf;
524         ecb.priv = &state;
525         memset(&state, 0, sizeof(state));
526         state.xm.consume = process_u_diff;
527         state.ret = xmalloc(sizeof(struct patch));
528         state.ret->chunks = NULL;
529         state.ret->num = 0;
530
531         xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
532
533         if (state.ret->num) {
534                 struct chunk *chunk;
535                 chunk = &state.ret->chunks[state.ret->num - 1];
536                 chunk->p_next -= state.hunk_post_context;
537                 chunk->t_next -= state.hunk_post_context;
538         }
539         return state.ret;
540 }
541
542 /*
543  * Run diff between two origins and grab the patch output, so that
544  * we can pass blame for lines origin is currently suspected for
545  * to its parent.
546  */
547 static struct patch *get_patch(struct origin *parent, struct origin *origin)
548 {
549         mmfile_t file_p, file_o;
550         struct patch *patch;
551
552         fill_origin_blob(parent, &file_p);
553         fill_origin_blob(origin, &file_o);
554         if (!file_p.ptr || !file_o.ptr)
555                 return NULL;
556         patch = compare_buffer(&file_p, &file_o, 0);
557         num_get_patch++;
558         return patch;
559 }
560
561 static void free_patch(struct patch *p)
562 {
563         free(p->chunks);
564         free(p);
565 }
566
567 /*
568  * Link in a new blame entry to the scoreboard.  Entries that cover the
569  * same line range have been removed from the scoreboard previously.
570  */
571 static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
572 {
573         struct blame_entry *ent, *prev = NULL;
574
575         origin_incref(e->suspect);
576
577         for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
578                 prev = ent;
579
580         /* prev, if not NULL, is the last one that is below e */
581         e->prev = prev;
582         if (prev) {
583                 e->next = prev->next;
584                 prev->next = e;
585         }
586         else {
587                 e->next = sb->ent;
588                 sb->ent = e;
589         }
590         if (e->next)
591                 e->next->prev = e;
592 }
593
594 /*
595  * src typically is on-stack; we want to copy the information in it to
596  * an malloced blame_entry that is already on the linked list of the
597  * scoreboard.  The origin of dst loses a refcnt while the origin of src
598  * gains one.
599  */
600 static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
601 {
602         struct blame_entry *p, *n;
603
604         p = dst->prev;
605         n = dst->next;
606         origin_incref(src->suspect);
607         origin_decref(dst->suspect);
608         memcpy(dst, src, sizeof(*src));
609         dst->prev = p;
610         dst->next = n;
611         dst->score = 0;
612 }
613
614 static const char *nth_line(struct scoreboard *sb, int lno)
615 {
616         return sb->final_buf + sb->lineno[lno];
617 }
618
619 /*
620  * It is known that lines between tlno to same came from parent, and e
621  * has an overlap with that range.  it also is known that parent's
622  * line plno corresponds to e's line tlno.
623  *
624  *                <---- e ----->
625  *                   <------>
626  *                   <------------>
627  *             <------------>
628  *             <------------------>
629  *
630  * Split e into potentially three parts; before this chunk, the chunk
631  * to be blamed for the parent, and after that portion.
632  */
633 static void split_overlap(struct blame_entry *split,
634                           struct blame_entry *e,
635                           int tlno, int plno, int same,
636                           struct origin *parent)
637 {
638         int chunk_end_lno;
639         memset(split, 0, sizeof(struct blame_entry [3]));
640
641         if (e->s_lno < tlno) {
642                 /* there is a pre-chunk part not blamed on parent */
643                 split[0].suspect = origin_incref(e->suspect);
644                 split[0].lno = e->lno;
645                 split[0].s_lno = e->s_lno;
646                 split[0].num_lines = tlno - e->s_lno;
647                 split[1].lno = e->lno + tlno - e->s_lno;
648                 split[1].s_lno = plno;
649         }
650         else {
651                 split[1].lno = e->lno;
652                 split[1].s_lno = plno + (e->s_lno - tlno);
653         }
654
655         if (same < e->s_lno + e->num_lines) {
656                 /* there is a post-chunk part not blamed on parent */
657                 split[2].suspect = origin_incref(e->suspect);
658                 split[2].lno = e->lno + (same - e->s_lno);
659                 split[2].s_lno = e->s_lno + (same - e->s_lno);
660                 split[2].num_lines = e->s_lno + e->num_lines - same;
661                 chunk_end_lno = split[2].lno;
662         }
663         else
664                 chunk_end_lno = e->lno + e->num_lines;
665         split[1].num_lines = chunk_end_lno - split[1].lno;
666
667         /*
668          * if it turns out there is nothing to blame the parent for,
669          * forget about the splitting.  !split[1].suspect signals this.
670          */
671         if (split[1].num_lines < 1)
672                 return;
673         split[1].suspect = origin_incref(parent);
674 }
675
676 /*
677  * split_overlap() divided an existing blame e into up to three parts
678  * in split.  Adjust the linked list of blames in the scoreboard to
679  * reflect the split.
680  */
681 static void split_blame(struct scoreboard *sb,
682                         struct blame_entry *split,
683                         struct blame_entry *e)
684 {
685         struct blame_entry *new_entry;
686
687         if (split[0].suspect && split[2].suspect) {
688                 /* The first part (reuse storage for the existing entry e) */
689                 dup_entry(e, &split[0]);
690
691                 /* The last part -- me */
692                 new_entry = xmalloc(sizeof(*new_entry));
693                 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
694                 add_blame_entry(sb, new_entry);
695
696                 /* ... and the middle part -- parent */
697                 new_entry = xmalloc(sizeof(*new_entry));
698                 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
699                 add_blame_entry(sb, new_entry);
700         }
701         else if (!split[0].suspect && !split[2].suspect)
702                 /*
703                  * The parent covers the entire area; reuse storage for
704                  * e and replace it with the parent.
705                  */
706                 dup_entry(e, &split[1]);
707         else if (split[0].suspect) {
708                 /* me and then parent */
709                 dup_entry(e, &split[0]);
710
711                 new_entry = xmalloc(sizeof(*new_entry));
712                 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
713                 add_blame_entry(sb, new_entry);
714         }
715         else {
716                 /* parent and then me */
717                 dup_entry(e, &split[1]);
718
719                 new_entry = xmalloc(sizeof(*new_entry));
720                 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
721                 add_blame_entry(sb, new_entry);
722         }
723
724         if (DEBUG) { /* sanity */
725                 struct blame_entry *ent;
726                 int lno = sb->ent->lno, corrupt = 0;
727
728                 for (ent = sb->ent; ent; ent = ent->next) {
729                         if (lno != ent->lno)
730                                 corrupt = 1;
731                         if (ent->s_lno < 0)
732                                 corrupt = 1;
733                         lno += ent->num_lines;
734                 }
735                 if (corrupt) {
736                         lno = sb->ent->lno;
737                         for (ent = sb->ent; ent; ent = ent->next) {
738                                 printf("L %8d l %8d n %8d\n",
739                                        lno, ent->lno, ent->num_lines);
740                                 lno = ent->lno + ent->num_lines;
741                         }
742                         die("oops");
743                 }
744         }
745 }
746
747 /*
748  * After splitting the blame, the origins used by the
749  * on-stack blame_entry should lose one refcnt each.
750  */
751 static void decref_split(struct blame_entry *split)
752 {
753         int i;
754
755         for (i = 0; i < 3; i++)
756                 origin_decref(split[i].suspect);
757 }
758
759 /*
760  * Helper for blame_chunk().  blame_entry e is known to overlap with
761  * the patch hunk; split it and pass blame to the parent.
762  */
763 static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
764                           int tlno, int plno, int same,
765                           struct origin *parent)
766 {
767         struct blame_entry split[3];
768
769         split_overlap(split, e, tlno, plno, same, parent);
770         if (split[1].suspect)
771                 split_blame(sb, split, e);
772         decref_split(split);
773 }
774
775 /*
776  * Find the line number of the last line the target is suspected for.
777  */
778 static int find_last_in_target(struct scoreboard *sb, struct origin *target)
779 {
780         struct blame_entry *e;
781         int last_in_target = -1;
782
783         for (e = sb->ent; e; e = e->next) {
784                 if (e->guilty || !same_suspect(e->suspect, target))
785                         continue;
786                 if (last_in_target < e->s_lno + e->num_lines)
787                         last_in_target = e->s_lno + e->num_lines;
788         }
789         return last_in_target;
790 }
791
792 /*
793  * Process one hunk from the patch between the current suspect for
794  * blame_entry e and its parent.  Find and split the overlap, and
795  * pass blame to the overlapping part to the parent.
796  */
797 static void blame_chunk(struct scoreboard *sb,
798                         int tlno, int plno, int same,
799                         struct origin *target, struct origin *parent)
800 {
801         struct blame_entry *e;
802
803         for (e = sb->ent; e; e = e->next) {
804                 if (e->guilty || !same_suspect(e->suspect, target))
805                         continue;
806                 if (same <= e->s_lno)
807                         continue;
808                 if (tlno < e->s_lno + e->num_lines)
809                         blame_overlap(sb, e, tlno, plno, same, parent);
810         }
811 }
812
813 /*
814  * We are looking at the origin 'target' and aiming to pass blame
815  * for the lines it is suspected to its parent.  Run diff to find
816  * which lines came from parent and pass blame for them.
817  */
818 static int pass_blame_to_parent(struct scoreboard *sb,
819                                 struct origin *target,
820                                 struct origin *parent)
821 {
822         int i, last_in_target, plno, tlno;
823         struct patch *patch;
824
825         last_in_target = find_last_in_target(sb, target);
826         if (last_in_target < 0)
827                 return 1; /* nothing remains for this target */
828
829         patch = get_patch(parent, target);
830         plno = tlno = 0;
831         for (i = 0; i < patch->num; i++) {
832                 struct chunk *chunk = &patch->chunks[i];
833
834                 blame_chunk(sb, tlno, plno, chunk->same, target, parent);
835                 plno = chunk->p_next;
836                 tlno = chunk->t_next;
837         }
838         /* The rest (i.e. anything after tlno) are the same as the parent */
839         blame_chunk(sb, tlno, plno, last_in_target, target, parent);
840
841         free_patch(patch);
842         return 0;
843 }
844
845 /*
846  * The lines in blame_entry after splitting blames many times can become
847  * very small and trivial, and at some point it becomes pointless to
848  * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
849  * ordinary C program, and it is not worth to say it was copied from
850  * totally unrelated file in the parent.
851  *
852  * Compute how trivial the lines in the blame_entry are.
853  */
854 static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
855 {
856         unsigned score;
857         const char *cp, *ep;
858
859         if (e->score)
860                 return e->score;
861
862         score = 1;
863         cp = nth_line(sb, e->lno);
864         ep = nth_line(sb, e->lno + e->num_lines);
865         while (cp < ep) {
866                 unsigned ch = *((unsigned char *)cp);
867                 if (isalnum(ch))
868                         score++;
869                 cp++;
870         }
871         e->score = score;
872         return score;
873 }
874
875 /*
876  * best_so_far[] and this[] are both a split of an existing blame_entry
877  * that passes blame to the parent.  Maintain best_so_far the best split
878  * so far, by comparing this and best_so_far and copying this into
879  * bst_so_far as needed.
880  */
881 static void copy_split_if_better(struct scoreboard *sb,
882                                  struct blame_entry *best_so_far,
883                                  struct blame_entry *this)
884 {
885         int i;
886
887         if (!this[1].suspect)
888                 return;
889         if (best_so_far[1].suspect) {
890                 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
891                         return;
892         }
893
894         for (i = 0; i < 3; i++)
895                 origin_incref(this[i].suspect);
896         decref_split(best_so_far);
897         memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
898 }
899
900 /*
901  * We are looking at a part of the final image represented by
902  * ent (tlno and same are offset by ent->s_lno).
903  * tlno is where we are looking at in the final image.
904  * up to (but not including) same match preimage.
905  * plno is where we are looking at in the preimage.
906  *
907  * <-------------- final image ---------------------->
908  *       <------ent------>
909  *         ^tlno ^same
910  *    <---------preimage----->
911  *         ^plno
912  *
913  * All line numbers are 0-based.
914  */
915 static void handle_split(struct scoreboard *sb,
916                          struct blame_entry *ent,
917                          int tlno, int plno, int same,
918                          struct origin *parent,
919                          struct blame_entry *split)
920 {
921         if (ent->num_lines <= tlno)
922                 return;
923         if (tlno < same) {
924                 struct blame_entry this[3];
925                 tlno += ent->s_lno;
926                 same += ent->s_lno;
927                 split_overlap(this, ent, tlno, plno, same, parent);
928                 copy_split_if_better(sb, split, this);
929                 decref_split(this);
930         }
931 }
932
933 /*
934  * Find the lines from parent that are the same as ent so that
935  * we can pass blames to it.  file_p has the blob contents for
936  * the parent.
937  */
938 static void find_copy_in_blob(struct scoreboard *sb,
939                               struct blame_entry *ent,
940                               struct origin *parent,
941                               struct blame_entry *split,
942                               mmfile_t *file_p)
943 {
944         const char *cp;
945         int cnt;
946         mmfile_t file_o;
947         struct patch *patch;
948         int i, plno, tlno;
949
950         /*
951          * Prepare mmfile that contains only the lines in ent.
952          */
953         cp = nth_line(sb, ent->lno);
954         file_o.ptr = (char*) cp;
955         cnt = ent->num_lines;
956
957         while (cnt && cp < sb->final_buf + sb->final_buf_size) {
958                 if (*cp++ == '\n')
959                         cnt--;
960         }
961         file_o.size = cp - file_o.ptr;
962
963         patch = compare_buffer(file_p, &file_o, 1);
964
965         /*
966          * file_o is a part of final image we are annotating.
967          * file_p partially may match that image.
968          */
969         memset(split, 0, sizeof(struct blame_entry [3]));
970         plno = tlno = 0;
971         for (i = 0; i < patch->num; i++) {
972                 struct chunk *chunk = &patch->chunks[i];
973
974                 handle_split(sb, ent, tlno, plno, chunk->same, parent, split);
975                 plno = chunk->p_next;
976                 tlno = chunk->t_next;
977         }
978         /* remainder, if any, all match the preimage */
979         handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split);
980         free_patch(patch);
981 }
982
983 /*
984  * See if lines currently target is suspected for can be attributed to
985  * parent.
986  */
987 static int find_move_in_parent(struct scoreboard *sb,
988                                struct origin *target,
989                                struct origin *parent)
990 {
991         int last_in_target, made_progress;
992         struct blame_entry *e, split[3];
993         mmfile_t file_p;
994
995         last_in_target = find_last_in_target(sb, target);
996         if (last_in_target < 0)
997                 return 1; /* nothing remains for this target */
998
999         fill_origin_blob(parent, &file_p);
1000         if (!file_p.ptr)
1001                 return 0;
1002
1003         made_progress = 1;
1004         while (made_progress) {
1005                 made_progress = 0;
1006                 for (e = sb->ent; e; e = e->next) {
1007                         if (e->guilty || !same_suspect(e->suspect, target))
1008                                 continue;
1009                         find_copy_in_blob(sb, e, parent, split, &file_p);
1010                         if (split[1].suspect &&
1011                             blame_move_score < ent_score(sb, &split[1])) {
1012                                 split_blame(sb, split, e);
1013                                 made_progress = 1;
1014                         }
1015                         decref_split(split);
1016                 }
1017         }
1018         return 0;
1019 }
1020
1021 struct blame_list {
1022         struct blame_entry *ent;
1023         struct blame_entry split[3];
1024 };
1025
1026 /*
1027  * Count the number of entries the target is suspected for,
1028  * and prepare a list of entry and the best split.
1029  */
1030 static struct blame_list *setup_blame_list(struct scoreboard *sb,
1031                                            struct origin *target,
1032                                            int *num_ents_p)
1033 {
1034         struct blame_entry *e;
1035         int num_ents, i;
1036         struct blame_list *blame_list = NULL;
1037
1038         for (e = sb->ent, num_ents = 0; e; e = e->next)
1039                 if (!e->guilty && same_suspect(e->suspect, target))
1040                         num_ents++;
1041         if (num_ents) {
1042                 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1043                 for (e = sb->ent, i = 0; e; e = e->next)
1044                         if (!e->guilty && same_suspect(e->suspect, target))
1045                                 blame_list[i++].ent = e;
1046         }
1047         *num_ents_p = num_ents;
1048         return blame_list;
1049 }
1050
1051 /*
1052  * For lines target is suspected for, see if we can find code movement
1053  * across file boundary from the parent commit.  porigin is the path
1054  * in the parent we already tried.
1055  */
1056 static int find_copy_in_parent(struct scoreboard *sb,
1057                                struct origin *target,
1058                                struct commit *parent,
1059                                struct origin *porigin,
1060                                int opt)
1061 {
1062         struct diff_options diff_opts;
1063         const char *paths[1];
1064         int i, j;
1065         int retval;
1066         struct blame_list *blame_list;
1067         int num_ents;
1068
1069         blame_list = setup_blame_list(sb, target, &num_ents);
1070         if (!blame_list)
1071                 return 1; /* nothing remains for this target */
1072
1073         diff_setup(&diff_opts);
1074         diff_opts.recursive = 1;
1075         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1076
1077         paths[0] = NULL;
1078         diff_tree_setup_paths(paths, &diff_opts);
1079         if (diff_setup_done(&diff_opts) < 0)
1080                 die("diff-setup");
1081
1082         /* Try "find copies harder" on new path if requested;
1083          * we do not want to use diffcore_rename() actually to
1084          * match things up; find_copies_harder is set only to
1085          * force diff_tree_sha1() to feed all filepairs to diff_queue,
1086          * and this code needs to be after diff_setup_done(), which
1087          * usually makes find-copies-harder imply copy detection.
1088          */
1089         if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1090             || ((opt & PICKAXE_BLAME_COPY_HARDER)
1091                 && (!porigin || strcmp(target->path, porigin->path))))
1092                 diff_opts.find_copies_harder = 1;
1093
1094         if (is_null_sha1(target->commit->object.sha1))
1095                 do_diff_cache(parent->tree->object.sha1, &diff_opts);
1096         else
1097                 diff_tree_sha1(parent->tree->object.sha1,
1098                                target->commit->tree->object.sha1,
1099                                "", &diff_opts);
1100
1101         if (!diff_opts.find_copies_harder)
1102                 diffcore_std(&diff_opts);
1103
1104         retval = 0;
1105         while (1) {
1106                 int made_progress = 0;
1107
1108                 for (i = 0; i < diff_queued_diff.nr; i++) {
1109                         struct diff_filepair *p = diff_queued_diff.queue[i];
1110                         struct origin *norigin;
1111                         mmfile_t file_p;
1112                         struct blame_entry this[3];
1113
1114                         if (!DIFF_FILE_VALID(p->one))
1115                                 continue; /* does not exist in parent */
1116                         if (porigin && !strcmp(p->one->path, porigin->path))
1117                                 /* find_move already dealt with this path */
1118                                 continue;
1119
1120                         norigin = get_origin(sb, parent, p->one->path);
1121                         hashcpy(norigin->blob_sha1, p->one->sha1);
1122                         fill_origin_blob(norigin, &file_p);
1123                         if (!file_p.ptr)
1124                                 continue;
1125
1126                         for (j = 0; j < num_ents; j++) {
1127                                 find_copy_in_blob(sb, blame_list[j].ent,
1128                                                   norigin, this, &file_p);
1129                                 copy_split_if_better(sb, blame_list[j].split,
1130                                                      this);
1131                                 decref_split(this);
1132                         }
1133                         origin_decref(norigin);
1134                 }
1135
1136                 for (j = 0; j < num_ents; j++) {
1137                         struct blame_entry *split = blame_list[j].split;
1138                         if (split[1].suspect &&
1139                             blame_copy_score < ent_score(sb, &split[1])) {
1140                                 split_blame(sb, split, blame_list[j].ent);
1141                                 made_progress = 1;
1142                         }
1143                         decref_split(split);
1144                 }
1145                 free(blame_list);
1146
1147                 if (!made_progress)
1148                         break;
1149                 blame_list = setup_blame_list(sb, target, &num_ents);
1150                 if (!blame_list) {
1151                         retval = 1;
1152                         break;
1153                 }
1154         }
1155         diff_flush(&diff_opts);
1156
1157         return retval;
1158 }
1159
1160 /*
1161  * The blobs of origin and porigin exactly match, so everything
1162  * origin is suspected for can be blamed on the parent.
1163  */
1164 static void pass_whole_blame(struct scoreboard *sb,
1165                              struct origin *origin, struct origin *porigin)
1166 {
1167         struct blame_entry *e;
1168
1169         if (!porigin->file.ptr && origin->file.ptr) {
1170                 /* Steal its file */
1171                 porigin->file = origin->file;
1172                 origin->file.ptr = NULL;
1173         }
1174         for (e = sb->ent; e; e = e->next) {
1175                 if (!same_suspect(e->suspect, origin))
1176                         continue;
1177                 origin_incref(porigin);
1178                 origin_decref(e->suspect);
1179                 e->suspect = porigin;
1180         }
1181 }
1182
1183 #define MAXPARENT 16
1184
1185 static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1186 {
1187         int i, pass;
1188         struct commit *commit = origin->commit;
1189         struct commit_list *parent;
1190         struct origin *parent_origin[MAXPARENT], *porigin;
1191
1192         memset(parent_origin, 0, sizeof(parent_origin));
1193
1194         /* The first pass looks for unrenamed path to optimize for
1195          * common cases, then we look for renames in the second pass.
1196          */
1197         for (pass = 0; pass < 2; pass++) {
1198                 struct origin *(*find)(struct scoreboard *,
1199                                        struct commit *, struct origin *);
1200                 find = pass ? find_rename : find_origin;
1201
1202                 for (i = 0, parent = commit->parents;
1203                      i < MAXPARENT && parent;
1204                      parent = parent->next, i++) {
1205                         struct commit *p = parent->item;
1206                         int j, same;
1207
1208                         if (parent_origin[i])
1209                                 continue;
1210                         if (parse_commit(p))
1211                                 continue;
1212                         porigin = find(sb, p, origin);
1213                         if (!porigin)
1214                                 continue;
1215                         if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
1216                                 pass_whole_blame(sb, origin, porigin);
1217                                 origin_decref(porigin);
1218                                 goto finish;
1219                         }
1220                         for (j = same = 0; j < i; j++)
1221                                 if (parent_origin[j] &&
1222                                     !hashcmp(parent_origin[j]->blob_sha1,
1223                                              porigin->blob_sha1)) {
1224                                         same = 1;
1225                                         break;
1226                                 }
1227                         if (!same)
1228                                 parent_origin[i] = porigin;
1229                         else
1230                                 origin_decref(porigin);
1231                 }
1232         }
1233
1234         num_commits++;
1235         for (i = 0, parent = commit->parents;
1236              i < MAXPARENT && parent;
1237              parent = parent->next, i++) {
1238                 struct origin *porigin = parent_origin[i];
1239                 if (!porigin)
1240                         continue;
1241                 if (pass_blame_to_parent(sb, origin, porigin))
1242                         goto finish;
1243         }
1244
1245         /*
1246          * Optionally find moves in parents' files.
1247          */
1248         if (opt & PICKAXE_BLAME_MOVE)
1249                 for (i = 0, parent = commit->parents;
1250                      i < MAXPARENT && parent;
1251                      parent = parent->next, i++) {
1252                         struct origin *porigin = parent_origin[i];
1253                         if (!porigin)
1254                                 continue;
1255                         if (find_move_in_parent(sb, origin, porigin))
1256                                 goto finish;
1257                 }
1258
1259         /*
1260          * Optionally find copies from parents' files.
1261          */
1262         if (opt & PICKAXE_BLAME_COPY)
1263                 for (i = 0, parent = commit->parents;
1264                      i < MAXPARENT && parent;
1265                      parent = parent->next, i++) {
1266                         struct origin *porigin = parent_origin[i];
1267                         if (find_copy_in_parent(sb, origin, parent->item,
1268                                                 porigin, opt))
1269                                 goto finish;
1270                 }
1271
1272  finish:
1273         for (i = 0; i < MAXPARENT; i++)
1274                 origin_decref(parent_origin[i]);
1275 }
1276
1277 /*
1278  * Information on commits, used for output.
1279  */
1280 struct commit_info
1281 {
1282         const char *author;
1283         const char *author_mail;
1284         unsigned long author_time;
1285         const char *author_tz;
1286
1287         /* filled only when asked for details */
1288         const char *committer;
1289         const char *committer_mail;
1290         unsigned long committer_time;
1291         const char *committer_tz;
1292
1293         const char *summary;
1294 };
1295
1296 /*
1297  * Parse author/committer line in the commit object buffer
1298  */
1299 static void get_ac_line(const char *inbuf, const char *what,
1300                         int bufsz, char *person, const char **mail,
1301                         unsigned long *time, const char **tz)
1302 {
1303         int len, tzlen, maillen;
1304         char *tmp, *endp, *timepos;
1305
1306         tmp = strstr(inbuf, what);
1307         if (!tmp)
1308                 goto error_out;
1309         tmp += strlen(what);
1310         endp = strchr(tmp, '\n');
1311         if (!endp)
1312                 len = strlen(tmp);
1313         else
1314                 len = endp - tmp;
1315         if (bufsz <= len) {
1316         error_out:
1317                 /* Ugh */
1318                 *mail = *tz = "(unknown)";
1319                 *time = 0;
1320                 return;
1321         }
1322         memcpy(person, tmp, len);
1323
1324         tmp = person;
1325         tmp += len;
1326         *tmp = 0;
1327         while (*tmp != ' ')
1328                 tmp--;
1329         *tz = tmp+1;
1330         tzlen = (person+len)-(tmp+1);
1331
1332         *tmp = 0;
1333         while (*tmp != ' ')
1334                 tmp--;
1335         *time = strtoul(tmp, NULL, 10);
1336         timepos = tmp;
1337
1338         *tmp = 0;
1339         while (*tmp != ' ')
1340                 tmp--;
1341         *mail = tmp + 1;
1342         *tmp = 0;
1343         maillen = timepos - tmp;
1344
1345         if (!mailmap.nr)
1346                 return;
1347
1348         /*
1349          * mailmap expansion may make the name longer.
1350          * make room by pushing stuff down.
1351          */
1352         tmp = person + bufsz - (tzlen + 1);
1353         memmove(tmp, *tz, tzlen);
1354         tmp[tzlen] = 0;
1355         *tz = tmp;
1356
1357         tmp = tmp - (maillen + 1);
1358         memmove(tmp, *mail, maillen);
1359         tmp[maillen] = 0;
1360         *mail = tmp;
1361
1362         /*
1363          * Now, convert e-mail using mailmap
1364          */
1365         map_email(&mailmap, tmp + 1, person, tmp-person-1);
1366 }
1367
1368 static void get_commit_info(struct commit *commit,
1369                             struct commit_info *ret,
1370                             int detailed)
1371 {
1372         int len;
1373         char *tmp, *endp;
1374         static char author_buf[1024];
1375         static char committer_buf[1024];
1376         static char summary_buf[1024];
1377
1378         /*
1379          * We've operated without save_commit_buffer, so
1380          * we now need to populate them for output.
1381          */
1382         if (!commit->buffer) {
1383                 enum object_type type;
1384                 unsigned long size;
1385                 commit->buffer =
1386                         read_sha1_file(commit->object.sha1, &type, &size);
1387         }
1388         ret->author = author_buf;
1389         get_ac_line(commit->buffer, "\nauthor ",
1390                     sizeof(author_buf), author_buf, &ret->author_mail,
1391                     &ret->author_time, &ret->author_tz);
1392
1393         if (!detailed)
1394                 return;
1395
1396         ret->committer = committer_buf;
1397         get_ac_line(commit->buffer, "\ncommitter ",
1398                     sizeof(committer_buf), committer_buf, &ret->committer_mail,
1399                     &ret->committer_time, &ret->committer_tz);
1400
1401         ret->summary = summary_buf;
1402         tmp = strstr(commit->buffer, "\n\n");
1403         if (!tmp) {
1404         error_out:
1405                 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1406                 return;
1407         }
1408         tmp += 2;
1409         endp = strchr(tmp, '\n');
1410         if (!endp)
1411                 endp = tmp + strlen(tmp);
1412         len = endp - tmp;
1413         if (len >= sizeof(summary_buf) || len == 0)
1414                 goto error_out;
1415         memcpy(summary_buf, tmp, len);
1416         summary_buf[len] = 0;
1417 }
1418
1419 /*
1420  * To allow LF and other nonportable characters in pathnames,
1421  * they are c-style quoted as needed.
1422  */
1423 static void write_filename_info(const char *path)
1424 {
1425         printf("filename ");
1426         write_name_quoted(NULL, 0, path, 1, stdout);
1427         putchar('\n');
1428 }
1429
1430 /*
1431  * The blame_entry is found to be guilty for the range.  Mark it
1432  * as such, and show it in incremental output.
1433  */
1434 static void found_guilty_entry(struct blame_entry *ent)
1435 {
1436         if (ent->guilty)
1437                 return;
1438         ent->guilty = 1;
1439         if (incremental) {
1440                 struct origin *suspect = ent->suspect;
1441
1442                 printf("%s %d %d %d\n",
1443                        sha1_to_hex(suspect->commit->object.sha1),
1444                        ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1445                 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1446                         struct commit_info ci;
1447                         suspect->commit->object.flags |= METAINFO_SHOWN;
1448                         get_commit_info(suspect->commit, &ci, 1);
1449                         printf("author %s\n", ci.author);
1450                         printf("author-mail %s\n", ci.author_mail);
1451                         printf("author-time %lu\n", ci.author_time);
1452                         printf("author-tz %s\n", ci.author_tz);
1453                         printf("committer %s\n", ci.committer);
1454                         printf("committer-mail %s\n", ci.committer_mail);
1455                         printf("committer-time %lu\n", ci.committer_time);
1456                         printf("committer-tz %s\n", ci.committer_tz);
1457                         printf("summary %s\n", ci.summary);
1458                         if (suspect->commit->object.flags & UNINTERESTING)
1459                                 printf("boundary\n");
1460                 }
1461                 write_filename_info(suspect->path);
1462                 maybe_flush_or_die(stdout, "stdout");
1463         }
1464 }
1465
1466 /*
1467  * The main loop -- while the scoreboard has lines whose true origin
1468  * is still unknown, pick one blame_entry, and allow its current
1469  * suspect to pass blames to its parents.
1470  */
1471 static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1472 {
1473         while (1) {
1474                 struct blame_entry *ent;
1475                 struct commit *commit;
1476                 struct origin *suspect = NULL;
1477
1478                 /* find one suspect to break down */
1479                 for (ent = sb->ent; !suspect && ent; ent = ent->next)
1480                         if (!ent->guilty)
1481                                 suspect = ent->suspect;
1482                 if (!suspect)
1483                         return; /* all done */
1484
1485                 /*
1486                  * We will use this suspect later in the loop,
1487                  * so hold onto it in the meantime.
1488                  */
1489                 origin_incref(suspect);
1490                 commit = suspect->commit;
1491                 if (!commit->object.parsed)
1492                         parse_commit(commit);
1493                 if (!(commit->object.flags & UNINTERESTING) &&
1494                     !(revs->max_age != -1 && commit->date < revs->max_age))
1495                         pass_blame(sb, suspect, opt);
1496                 else {
1497                         commit->object.flags |= UNINTERESTING;
1498                         if (commit->object.parsed)
1499                                 mark_parents_uninteresting(commit);
1500                 }
1501                 /* treat root commit as boundary */
1502                 if (!commit->parents && !show_root)
1503                         commit->object.flags |= UNINTERESTING;
1504
1505                 /* Take responsibility for the remaining entries */
1506                 for (ent = sb->ent; ent; ent = ent->next)
1507                         if (same_suspect(ent->suspect, suspect))
1508                                 found_guilty_entry(ent);
1509                 origin_decref(suspect);
1510
1511                 if (DEBUG) /* sanity */
1512                         sanity_check_refcnt(sb);
1513         }
1514 }
1515
1516 static const char *format_time(unsigned long time, const char *tz_str,
1517                                int show_raw_time)
1518 {
1519         static char time_buf[128];
1520         time_t t = time;
1521         int minutes, tz;
1522         struct tm *tm;
1523
1524         if (show_raw_time) {
1525                 sprintf(time_buf, "%lu %s", time, tz_str);
1526                 return time_buf;
1527         }
1528
1529         tz = atoi(tz_str);
1530         minutes = tz < 0 ? -tz : tz;
1531         minutes = (minutes / 100)*60 + (minutes % 100);
1532         minutes = tz < 0 ? -minutes : minutes;
1533         t = time + minutes * 60;
1534         tm = gmtime(&t);
1535
1536         strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1537         strcat(time_buf, tz_str);
1538         return time_buf;
1539 }
1540
1541 #define OUTPUT_ANNOTATE_COMPAT  001
1542 #define OUTPUT_LONG_OBJECT_NAME 002
1543 #define OUTPUT_RAW_TIMESTAMP    004
1544 #define OUTPUT_PORCELAIN        010
1545 #define OUTPUT_SHOW_NAME        020
1546 #define OUTPUT_SHOW_NUMBER      040
1547 #define OUTPUT_SHOW_SCORE      0100
1548 #define OUTPUT_NO_AUTHOR       0200
1549
1550 static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
1551 {
1552         int cnt;
1553         const char *cp;
1554         struct origin *suspect = ent->suspect;
1555         char hex[41];
1556
1557         strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1558         printf("%s%c%d %d %d\n",
1559                hex,
1560                ent->guilty ? ' ' : '*', // purely for debugging
1561                ent->s_lno + 1,
1562                ent->lno + 1,
1563                ent->num_lines);
1564         if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1565                 struct commit_info ci;
1566                 suspect->commit->object.flags |= METAINFO_SHOWN;
1567                 get_commit_info(suspect->commit, &ci, 1);
1568                 printf("author %s\n", ci.author);
1569                 printf("author-mail %s\n", ci.author_mail);
1570                 printf("author-time %lu\n", ci.author_time);
1571                 printf("author-tz %s\n", ci.author_tz);
1572                 printf("committer %s\n", ci.committer);
1573                 printf("committer-mail %s\n", ci.committer_mail);
1574                 printf("committer-time %lu\n", ci.committer_time);
1575                 printf("committer-tz %s\n", ci.committer_tz);
1576                 write_filename_info(suspect->path);
1577                 printf("summary %s\n", ci.summary);
1578                 if (suspect->commit->object.flags & UNINTERESTING)
1579                         printf("boundary\n");
1580         }
1581         else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
1582                 write_filename_info(suspect->path);
1583
1584         cp = nth_line(sb, ent->lno);
1585         for (cnt = 0; cnt < ent->num_lines; cnt++) {
1586                 char ch;
1587                 if (cnt)
1588                         printf("%s %d %d\n", hex,
1589                                ent->s_lno + 1 + cnt,
1590                                ent->lno + 1 + cnt);
1591                 putchar('\t');
1592                 do {
1593                         ch = *cp++;
1594                         putchar(ch);
1595                 } while (ch != '\n' &&
1596                          cp < sb->final_buf + sb->final_buf_size);
1597         }
1598 }
1599
1600 static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1601 {
1602         int cnt;
1603         const char *cp;
1604         struct origin *suspect = ent->suspect;
1605         struct commit_info ci;
1606         char hex[41];
1607         int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1608
1609         get_commit_info(suspect->commit, &ci, 1);
1610         strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1611
1612         cp = nth_line(sb, ent->lno);
1613         for (cnt = 0; cnt < ent->num_lines; cnt++) {
1614                 char ch;
1615                 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8;
1616
1617                 if (suspect->commit->object.flags & UNINTERESTING) {
1618                         if (blank_boundary)
1619                                 memset(hex, ' ', length);
1620                         else if (!cmd_is_annotate) {
1621                                 length--;
1622                                 putchar('^');
1623                         }
1624                 }
1625
1626                 printf("%.*s", length, hex);
1627                 if (opt & OUTPUT_ANNOTATE_COMPAT)
1628                         printf("\t(%10s\t%10s\t%d)", ci.author,
1629                                format_time(ci.author_time, ci.author_tz,
1630                                            show_raw_time),
1631                                ent->lno + 1 + cnt);
1632                 else {
1633                         if (opt & OUTPUT_SHOW_SCORE)
1634                                 printf(" %*d %02d",
1635                                        max_score_digits, ent->score,
1636                                        ent->suspect->refcnt);
1637                         if (opt & OUTPUT_SHOW_NAME)
1638                                 printf(" %-*.*s", longest_file, longest_file,
1639                                        suspect->path);
1640                         if (opt & OUTPUT_SHOW_NUMBER)
1641                                 printf(" %*d", max_orig_digits,
1642                                        ent->s_lno + 1 + cnt);
1643
1644                         if (!(opt & OUTPUT_NO_AUTHOR))
1645                                 printf(" (%-*.*s %10s",
1646                                        longest_author, longest_author,
1647                                        ci.author,
1648                                        format_time(ci.author_time,
1649                                                    ci.author_tz,
1650                                                    show_raw_time));
1651                         printf(" %*d) ",
1652                                max_digits, ent->lno + 1 + cnt);
1653                 }
1654                 do {
1655                         ch = *cp++;
1656                         putchar(ch);
1657                 } while (ch != '\n' &&
1658                          cp < sb->final_buf + sb->final_buf_size);
1659         }
1660 }
1661
1662 static void output(struct scoreboard *sb, int option)
1663 {
1664         struct blame_entry *ent;
1665
1666         if (option & OUTPUT_PORCELAIN) {
1667                 for (ent = sb->ent; ent; ent = ent->next) {
1668                         struct blame_entry *oth;
1669                         struct origin *suspect = ent->suspect;
1670                         struct commit *commit = suspect->commit;
1671                         if (commit->object.flags & MORE_THAN_ONE_PATH)
1672                                 continue;
1673                         for (oth = ent->next; oth; oth = oth->next) {
1674                                 if ((oth->suspect->commit != commit) ||
1675                                     !strcmp(oth->suspect->path, suspect->path))
1676                                         continue;
1677                                 commit->object.flags |= MORE_THAN_ONE_PATH;
1678                                 break;
1679                         }
1680                 }
1681         }
1682
1683         for (ent = sb->ent; ent; ent = ent->next) {
1684                 if (option & OUTPUT_PORCELAIN)
1685                         emit_porcelain(sb, ent);
1686                 else {
1687                         emit_other(sb, ent, option);
1688                 }
1689         }
1690 }
1691
1692 /*
1693  * To allow quick access to the contents of nth line in the
1694  * final image, prepare an index in the scoreboard.
1695  */
1696 static int prepare_lines(struct scoreboard *sb)
1697 {
1698         const char *buf = sb->final_buf;
1699         unsigned long len = sb->final_buf_size;
1700         int num = 0, incomplete = 0, bol = 1;
1701
1702         if (len && buf[len-1] != '\n')
1703                 incomplete++; /* incomplete line at the end */
1704         while (len--) {
1705                 if (bol) {
1706                         sb->lineno = xrealloc(sb->lineno,
1707                                               sizeof(int* ) * (num + 1));
1708                         sb->lineno[num] = buf - sb->final_buf;
1709                         bol = 0;
1710                 }
1711                 if (*buf++ == '\n') {
1712                         num++;
1713                         bol = 1;
1714                 }
1715         }
1716         sb->lineno = xrealloc(sb->lineno,
1717                               sizeof(int* ) * (num + incomplete + 1));
1718         sb->lineno[num + incomplete] = buf - sb->final_buf;
1719         sb->num_lines = num + incomplete;
1720         return sb->num_lines;
1721 }
1722
1723 /*
1724  * Add phony grafts for use with -S; this is primarily to
1725  * support git-cvsserver that wants to give a linear history
1726  * to its clients.
1727  */
1728 static int read_ancestry(const char *graft_file)
1729 {
1730         FILE *fp = fopen(graft_file, "r");
1731         char buf[1024];
1732         if (!fp)
1733                 return -1;
1734         while (fgets(buf, sizeof(buf), fp)) {
1735                 /* The format is just "Commit Parent1 Parent2 ...\n" */
1736                 int len = strlen(buf);
1737                 struct commit_graft *graft = read_graft_line(buf, len);
1738                 if (graft)
1739                         register_commit_graft(graft, 0);
1740         }
1741         fclose(fp);
1742         return 0;
1743 }
1744
1745 /*
1746  * How many columns do we need to show line numbers in decimal?
1747  */
1748 static int lineno_width(int lines)
1749 {
1750         int i, width;
1751
1752         for (width = 1, i = 10; i <= lines + 1; width++)
1753                 i *= 10;
1754         return width;
1755 }
1756
1757 /*
1758  * How many columns do we need to show line numbers, authors,
1759  * and filenames?
1760  */
1761 static void find_alignment(struct scoreboard *sb, int *option)
1762 {
1763         int longest_src_lines = 0;
1764         int longest_dst_lines = 0;
1765         unsigned largest_score = 0;
1766         struct blame_entry *e;
1767
1768         for (e = sb->ent; e; e = e->next) {
1769                 struct origin *suspect = e->suspect;
1770                 struct commit_info ci;
1771                 int num;
1772
1773                 if (strcmp(suspect->path, sb->path))
1774                         *option |= OUTPUT_SHOW_NAME;
1775                 num = strlen(suspect->path);
1776                 if (longest_file < num)
1777                         longest_file = num;
1778                 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1779                         suspect->commit->object.flags |= METAINFO_SHOWN;
1780                         get_commit_info(suspect->commit, &ci, 1);
1781                         num = strlen(ci.author);
1782                         if (longest_author < num)
1783                                 longest_author = num;
1784                 }
1785                 num = e->s_lno + e->num_lines;
1786                 if (longest_src_lines < num)
1787                         longest_src_lines = num;
1788                 num = e->lno + e->num_lines;
1789                 if (longest_dst_lines < num)
1790                         longest_dst_lines = num;
1791                 if (largest_score < ent_score(sb, e))
1792                         largest_score = ent_score(sb, e);
1793         }
1794         max_orig_digits = lineno_width(longest_src_lines);
1795         max_digits = lineno_width(longest_dst_lines);
1796         max_score_digits = lineno_width(largest_score);
1797 }
1798
1799 /*
1800  * For debugging -- origin is refcounted, and this asserts that
1801  * we do not underflow.
1802  */
1803 static void sanity_check_refcnt(struct scoreboard *sb)
1804 {
1805         int baa = 0;
1806         struct blame_entry *ent;
1807
1808         for (ent = sb->ent; ent; ent = ent->next) {
1809                 /* Nobody should have zero or negative refcnt */
1810                 if (ent->suspect->refcnt <= 0) {
1811                         fprintf(stderr, "%s in %s has negative refcnt %d\n",
1812                                 ent->suspect->path,
1813                                 sha1_to_hex(ent->suspect->commit->object.sha1),
1814                                 ent->suspect->refcnt);
1815                         baa = 1;
1816                 }
1817         }
1818         for (ent = sb->ent; ent; ent = ent->next) {
1819                 /* Mark the ones that haven't been checked */
1820                 if (0 < ent->suspect->refcnt)
1821                         ent->suspect->refcnt = -ent->suspect->refcnt;
1822         }
1823         for (ent = sb->ent; ent; ent = ent->next) {
1824                 /*
1825                  * ... then pick each and see if they have the the
1826                  * correct refcnt.
1827                  */
1828                 int found;
1829                 struct blame_entry *e;
1830                 struct origin *suspect = ent->suspect;
1831
1832                 if (0 < suspect->refcnt)
1833                         continue;
1834                 suspect->refcnt = -suspect->refcnt; /* Unmark */
1835                 for (found = 0, e = sb->ent; e; e = e->next) {
1836                         if (e->suspect != suspect)
1837                                 continue;
1838                         found++;
1839                 }
1840                 if (suspect->refcnt != found) {
1841                         fprintf(stderr, "%s in %s has refcnt %d, not %d\n",
1842                                 ent->suspect->path,
1843                                 sha1_to_hex(ent->suspect->commit->object.sha1),
1844                                 ent->suspect->refcnt, found);
1845                         baa = 2;
1846                 }
1847         }
1848         if (baa) {
1849                 int opt = 0160;
1850                 find_alignment(sb, &opt);
1851                 output(sb, opt);
1852                 die("Baa %d!", baa);
1853         }
1854 }
1855
1856 /*
1857  * Used for the command line parsing; check if the path exists
1858  * in the working tree.
1859  */
1860 static int has_path_in_work_tree(const char *path)
1861 {
1862         struct stat st;
1863         return !lstat(path, &st);
1864 }
1865
1866 static unsigned parse_score(const char *arg)
1867 {
1868         char *end;
1869         unsigned long score = strtoul(arg, &end, 10);
1870         if (*end)
1871                 return 0;
1872         return score;
1873 }
1874
1875 static const char *add_prefix(const char *prefix, const char *path)
1876 {
1877         if (!prefix || !prefix[0])
1878                 return path;
1879         return prefix_path(prefix, strlen(prefix), path);
1880 }
1881
1882 /*
1883  * Parsing of (comma separated) one item in the -L option
1884  */
1885 static const char *parse_loc(const char *spec,
1886                              struct scoreboard *sb, long lno,
1887                              long begin, long *ret)
1888 {
1889         char *term;
1890         const char *line;
1891         long num;
1892         int reg_error;
1893         regex_t regexp;
1894         regmatch_t match[1];
1895
1896         /* Allow "-L <something>,+20" to mean starting at <something>
1897          * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1898          * <something>.
1899          */
1900         if (1 < begin && (spec[0] == '+' || spec[0] == '-')) {
1901                 num = strtol(spec + 1, &term, 10);
1902                 if (term != spec + 1) {
1903                         if (spec[0] == '-')
1904                                 num = 0 - num;
1905                         if (0 < num)
1906                                 *ret = begin + num - 2;
1907                         else if (!num)
1908                                 *ret = begin;
1909                         else
1910                                 *ret = begin + num;
1911                         return term;
1912                 }
1913                 return spec;
1914         }
1915         num = strtol(spec, &term, 10);
1916         if (term != spec) {
1917                 *ret = num;
1918                 return term;
1919         }
1920         if (spec[0] != '/')
1921                 return spec;
1922
1923         /* it could be a regexp of form /.../ */
1924         for (term = (char*) spec + 1; *term && *term != '/'; term++) {
1925                 if (*term == '\\')
1926                         term++;
1927         }
1928         if (*term != '/')
1929                 return spec;
1930
1931         /* try [spec+1 .. term-1] as regexp */
1932         *term = 0;
1933         begin--; /* input is in human terms */
1934         line = nth_line(sb, begin);
1935
1936         if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
1937             !(reg_error = regexec(&regexp, line, 1, match, 0))) {
1938                 const char *cp = line + match[0].rm_so;
1939                 const char *nline;
1940
1941                 while (begin++ < lno) {
1942                         nline = nth_line(sb, begin);
1943                         if (line <= cp && cp < nline)
1944                                 break;
1945                         line = nline;
1946                 }
1947                 *ret = begin;
1948                 regfree(&regexp);
1949                 *term++ = '/';
1950                 return term;
1951         }
1952         else {
1953                 char errbuf[1024];
1954                 regerror(reg_error, &regexp, errbuf, 1024);
1955                 die("-L parameter '%s': %s", spec + 1, errbuf);
1956         }
1957 }
1958
1959 /*
1960  * Parsing of -L option
1961  */
1962 static void prepare_blame_range(struct scoreboard *sb,
1963                                 const char *bottomtop,
1964                                 long lno,
1965                                 long *bottom, long *top)
1966 {
1967         const char *term;
1968
1969         term = parse_loc(bottomtop, sb, lno, 1, bottom);
1970         if (*term == ',') {
1971                 term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
1972                 if (*term)
1973                         usage(blame_usage);
1974         }
1975         if (*term)
1976                 usage(blame_usage);
1977 }
1978
1979 static int git_blame_config(const char *var, const char *value)
1980 {
1981         if (!strcmp(var, "blame.showroot")) {
1982                 show_root = git_config_bool(var, value);
1983                 return 0;
1984         }
1985         if (!strcmp(var, "blame.blankboundary")) {
1986                 blank_boundary = git_config_bool(var, value);
1987                 return 0;
1988         }
1989         return git_default_config(var, value);
1990 }
1991
1992 static struct commit *fake_working_tree_commit(const char *path, const char *contents_from)
1993 {
1994         struct commit *commit;
1995         struct origin *origin;
1996         unsigned char head_sha1[20];
1997         char *buf;
1998         const char *ident;
1999         int fd;
2000         time_t now;
2001         unsigned long fin_size;
2002         int size, len;
2003         struct cache_entry *ce;
2004         unsigned mode;
2005
2006         if (get_sha1("HEAD", head_sha1))
2007                 die("No such ref: HEAD");
2008
2009         time(&now);
2010         commit = xcalloc(1, sizeof(*commit));
2011         commit->parents = xcalloc(1, sizeof(*commit->parents));
2012         commit->parents->item = lookup_commit_reference(head_sha1);
2013         commit->object.parsed = 1;
2014         commit->date = now;
2015         commit->object.type = OBJ_COMMIT;
2016
2017         origin = make_origin(commit, path);
2018
2019         if (!contents_from || strcmp("-", contents_from)) {
2020                 struct stat st;
2021                 const char *read_from;
2022
2023                 if (contents_from) {
2024                         if (stat(contents_from, &st) < 0)
2025                                 die("Cannot stat %s", contents_from);
2026                         read_from = contents_from;
2027                 }
2028                 else {
2029                         if (lstat(path, &st) < 0)
2030                                 die("Cannot lstat %s", path);
2031                         read_from = path;
2032                 }
2033                 fin_size = xsize_t(st.st_size);
2034                 buf = xmalloc(fin_size+1);
2035                 mode = canon_mode(st.st_mode);
2036                 switch (st.st_mode & S_IFMT) {
2037                 case S_IFREG:
2038                         fd = open(read_from, O_RDONLY);
2039                         if (fd < 0)
2040                                 die("cannot open %s", read_from);
2041                         if (read_in_full(fd, buf, fin_size) != fin_size)
2042                                 die("cannot read %s", read_from);
2043                         break;
2044                 case S_IFLNK:
2045                         if (readlink(read_from, buf, fin_size+1) != fin_size)
2046                                 die("cannot readlink %s", read_from);
2047                         break;
2048                 default:
2049                         die("unsupported file type %s", read_from);
2050                 }
2051         }
2052         else {
2053                 /* Reading from stdin */
2054                 contents_from = "standard input";
2055                 buf = NULL;
2056                 fin_size = 0;
2057                 mode = 0;
2058                 while (1) {
2059                         ssize_t cnt = 8192;
2060                         buf = xrealloc(buf, fin_size + cnt);
2061                         cnt = xread(0, buf + fin_size, cnt);
2062                         if (cnt < 0)
2063                                 die("read error %s from stdin",
2064                                     strerror(errno));
2065                         if (!cnt)
2066                                 break;
2067                         fin_size += cnt;
2068                 }
2069                 buf = xrealloc(buf, fin_size + 1);
2070         }
2071         buf[fin_size] = 0;
2072         origin->file.ptr = buf;
2073         origin->file.size = fin_size;
2074         pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
2075         commit->util = origin;
2076
2077         /*
2078          * Read the current index, replace the path entry with
2079          * origin->blob_sha1 without mucking with its mode or type
2080          * bits; we are not going to write this index out -- we just
2081          * want to run "diff-index --cached".
2082          */
2083         discard_cache();
2084         read_cache();
2085
2086         len = strlen(path);
2087         if (!mode) {
2088                 int pos = cache_name_pos(path, len);
2089                 if (0 <= pos)
2090                         mode = ntohl(active_cache[pos]->ce_mode);
2091                 else
2092                         /* Let's not bother reading from HEAD tree */
2093                         mode = S_IFREG | 0644;
2094         }
2095         size = cache_entry_size(len);
2096         ce = xcalloc(1, size);
2097         hashcpy(ce->sha1, origin->blob_sha1);
2098         memcpy(ce->name, path, len);
2099         ce->ce_flags = create_ce_flags(len, 0);
2100         ce->ce_mode = create_ce_mode(mode);
2101         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2102
2103         /*
2104          * We are not going to write this out, so this does not matter
2105          * right now, but someday we might optimize diff-index --cached
2106          * with cache-tree information.
2107          */
2108         cache_tree_invalidate_path(active_cache_tree, path);
2109
2110         commit->buffer = xmalloc(400);
2111         ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2112         snprintf(commit->buffer, 400,
2113                 "tree 0000000000000000000000000000000000000000\n"
2114                 "parent %s\n"
2115                 "author %s\n"
2116                 "committer %s\n\n"
2117                 "Version of %s from %s\n",
2118                 sha1_to_hex(head_sha1),
2119                 ident, ident, path, contents_from ? contents_from : path);
2120         return commit;
2121 }
2122
2123 int cmd_blame(int argc, const char **argv, const char *prefix)
2124 {
2125         struct rev_info revs;
2126         const char *path;
2127         struct scoreboard sb;
2128         struct origin *o;
2129         struct blame_entry *ent;
2130         int i, seen_dashdash, unk, opt;
2131         long bottom, top, lno;
2132         int output_option = 0;
2133         int show_stats = 0;
2134         const char *revs_file = NULL;
2135         const char *final_commit_name = NULL;
2136         enum object_type type;
2137         const char *bottomtop = NULL;
2138         const char *contents_from = NULL;
2139
2140         cmd_is_annotate = !strcmp(argv[0], "annotate");
2141
2142         git_config(git_blame_config);
2143         save_commit_buffer = 0;
2144
2145         opt = 0;
2146         seen_dashdash = 0;
2147         for (unk = i = 1; i < argc; i++) {
2148                 const char *arg = argv[i];
2149                 if (*arg != '-')
2150                         break;
2151                 else if (!strcmp("-b", arg))
2152                         blank_boundary = 1;
2153                 else if (!strcmp("--root", arg))
2154                         show_root = 1;
2155                 else if (!strcmp(arg, "--show-stats"))
2156                         show_stats = 1;
2157                 else if (!strcmp("-c", arg))
2158                         output_option |= OUTPUT_ANNOTATE_COMPAT;
2159                 else if (!strcmp("-t", arg))
2160                         output_option |= OUTPUT_RAW_TIMESTAMP;
2161                 else if (!strcmp("-l", arg))
2162                         output_option |= OUTPUT_LONG_OBJECT_NAME;
2163                 else if (!strcmp("-s", arg))
2164                         output_option |= OUTPUT_NO_AUTHOR;
2165                 else if (!strcmp("-w", arg))
2166                         xdl_opts |= XDF_IGNORE_WHITESPACE;
2167                 else if (!strcmp("-S", arg) && ++i < argc)
2168                         revs_file = argv[i];
2169                 else if (!prefixcmp(arg, "-M")) {
2170                         opt |= PICKAXE_BLAME_MOVE;
2171                         blame_move_score = parse_score(arg+2);
2172                 }
2173                 else if (!prefixcmp(arg, "-C")) {
2174                         /*
2175                          * -C enables copy from removed files;
2176                          * -C -C enables copy from existing files, but only
2177                          *       when blaming a new file;
2178                          * -C -C -C enables copy from existing files for
2179                          *          everybody
2180                          */
2181                         if (opt & PICKAXE_BLAME_COPY_HARDER)
2182                                 opt |= PICKAXE_BLAME_COPY_HARDEST;
2183                         if (opt & PICKAXE_BLAME_COPY)
2184                                 opt |= PICKAXE_BLAME_COPY_HARDER;
2185                         opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2186                         blame_copy_score = parse_score(arg+2);
2187                 }
2188                 else if (!prefixcmp(arg, "-L")) {
2189                         if (!arg[2]) {
2190                                 if (++i >= argc)
2191                                         usage(blame_usage);
2192                                 arg = argv[i];
2193                         }
2194                         else
2195                                 arg += 2;
2196                         if (bottomtop)
2197                                 die("More than one '-L n,m' option given");
2198                         bottomtop = arg;
2199                 }
2200                 else if (!strcmp("--contents", arg)) {
2201                         if (++i >= argc)
2202                                 usage(blame_usage);
2203                         contents_from = argv[i];
2204                 }
2205                 else if (!strcmp("--incremental", arg))
2206                         incremental = 1;
2207                 else if (!strcmp("--score-debug", arg))
2208                         output_option |= OUTPUT_SHOW_SCORE;
2209                 else if (!strcmp("-f", arg) ||
2210                          !strcmp("--show-name", arg))
2211                         output_option |= OUTPUT_SHOW_NAME;
2212                 else if (!strcmp("-n", arg) ||
2213                          !strcmp("--show-number", arg))
2214                         output_option |= OUTPUT_SHOW_NUMBER;
2215                 else if (!strcmp("-p", arg) ||
2216                          !strcmp("--porcelain", arg))
2217                         output_option |= OUTPUT_PORCELAIN;
2218                 else if (!strcmp("--", arg)) {
2219                         seen_dashdash = 1;
2220                         i++;
2221                         break;
2222                 }
2223                 else
2224                         argv[unk++] = arg;
2225         }
2226
2227         if (!incremental)
2228                 setup_pager();
2229
2230         if (!blame_move_score)
2231                 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2232         if (!blame_copy_score)
2233                 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2234
2235         /*
2236          * We have collected options unknown to us in argv[1..unk]
2237          * which are to be passed to revision machinery if we are
2238          * going to do the "bottom" processing.
2239          *
2240          * The remaining are:
2241          *
2242          * (1) if seen_dashdash, its either
2243          *     "-options -- <path>" or
2244          *     "-options -- <path> <rev>".
2245          *     but the latter is allowed only if there is no
2246          *     options that we passed to revision machinery.
2247          *
2248          * (2) otherwise, we may have "--" somewhere later and
2249          *     might be looking at the first one of multiple 'rev'
2250          *     parameters (e.g. " master ^next ^maint -- path").
2251          *     See if there is a dashdash first, and give the
2252          *     arguments before that to revision machinery.
2253          *     After that there must be one 'path'.
2254          *
2255          * (3) otherwise, its one of the three:
2256          *     "-options <path> <rev>"
2257          *     "-options <rev> <path>"
2258          *     "-options <path>"
2259          *     but again the first one is allowed only if
2260          *     there is no options that we passed to revision
2261          *     machinery.
2262          */
2263
2264         if (seen_dashdash) {
2265                 /* (1) */
2266                 if (argc <= i)
2267                         usage(blame_usage);
2268                 path = add_prefix(prefix, argv[i]);
2269                 if (i + 1 == argc - 1) {
2270                         if (unk != 1)
2271                                 usage(blame_usage);
2272                         argv[unk++] = argv[i + 1];
2273                 }
2274                 else if (i + 1 != argc)
2275                         /* garbage at end */
2276                         usage(blame_usage);
2277         }
2278         else {
2279                 int j;
2280                 for (j = i; !seen_dashdash && j < argc; j++)
2281                         if (!strcmp(argv[j], "--"))
2282                                 seen_dashdash = j;
2283                 if (seen_dashdash) {
2284                         /* (2) */
2285                         if (seen_dashdash + 1 != argc - 1)
2286                                 usage(blame_usage);
2287                         path = add_prefix(prefix, argv[seen_dashdash + 1]);
2288                         for (j = i; j < seen_dashdash; j++)
2289                                 argv[unk++] = argv[j];
2290                 }
2291                 else {
2292                         /* (3) */
2293                         if (argc <= i)
2294                                 usage(blame_usage);
2295                         path = add_prefix(prefix, argv[i]);
2296                         if (i + 1 == argc - 1) {
2297                                 final_commit_name = argv[i + 1];
2298
2299                                 /* if (unk == 1) we could be getting
2300                                  * old-style
2301                                  */
2302                                 if (unk == 1 && !has_path_in_work_tree(path)) {
2303                                         path = add_prefix(prefix, argv[i + 1]);
2304                                         final_commit_name = argv[i];
2305                                 }
2306                         }
2307                         else if (i != argc - 1)
2308                                 usage(blame_usage); /* garbage at end */
2309
2310                         if (!has_path_in_work_tree(path))
2311                                 die("cannot stat path %s: %s",
2312                                     path, strerror(errno));
2313                 }
2314         }
2315
2316         if (final_commit_name)
2317                 argv[unk++] = final_commit_name;
2318
2319         /*
2320          * Now we got rev and path.  We do not want the path pruning
2321          * but we may want "bottom" processing.
2322          */
2323         argv[unk++] = "--"; /* terminate the rev name */
2324         argv[unk] = NULL;
2325
2326         init_revisions(&revs, NULL);
2327         setup_revisions(unk, argv, &revs, NULL);
2328         memset(&sb, 0, sizeof(sb));
2329
2330         /*
2331          * There must be one and only one positive commit in the
2332          * revs->pending array.
2333          */
2334         for (i = 0; i < revs.pending.nr; i++) {
2335                 struct object *obj = revs.pending.objects[i].item;
2336                 if (obj->flags & UNINTERESTING)
2337                         continue;
2338                 while (obj->type == OBJ_TAG)
2339                         obj = deref_tag(obj, NULL, 0);
2340                 if (obj->type != OBJ_COMMIT)
2341                         die("Non commit %s?",
2342                             revs.pending.objects[i].name);
2343                 if (sb.final)
2344                         die("More than one commit to dig from %s and %s?",
2345                             revs.pending.objects[i].name,
2346                             final_commit_name);
2347                 sb.final = (struct commit *) obj;
2348                 final_commit_name = revs.pending.objects[i].name;
2349         }
2350
2351         if (!sb.final) {
2352                 /*
2353                  * "--not A B -- path" without anything positive;
2354                  * do not default to HEAD, but use the working tree
2355                  * or "--contents".
2356                  */
2357                 sb.final = fake_working_tree_commit(path, contents_from);
2358                 add_pending_object(&revs, &(sb.final->object), ":");
2359         }
2360         else if (contents_from)
2361                 die("Cannot use --contents with final commit object name");
2362
2363         /*
2364          * If we have bottom, this will mark the ancestors of the
2365          * bottom commits we would reach while traversing as
2366          * uninteresting.
2367          */
2368         prepare_revision_walk(&revs);
2369
2370         if (is_null_sha1(sb.final->object.sha1)) {
2371                 char *buf;
2372                 o = sb.final->util;
2373                 buf = xmalloc(o->file.size + 1);
2374                 memcpy(buf, o->file.ptr, o->file.size + 1);
2375                 sb.final_buf = buf;
2376                 sb.final_buf_size = o->file.size;
2377         }
2378         else {
2379                 o = get_origin(&sb, sb.final, path);
2380                 if (fill_blob_sha1(o))
2381                         die("no such path %s in %s", path, final_commit_name);
2382
2383                 sb.final_buf = read_sha1_file(o->blob_sha1, &type,
2384                                               &sb.final_buf_size);
2385         }
2386         num_read_blob++;
2387         lno = prepare_lines(&sb);
2388
2389         bottom = top = 0;
2390         if (bottomtop)
2391                 prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
2392         if (bottom && top && top < bottom) {
2393                 long tmp;
2394                 tmp = top; top = bottom; bottom = tmp;
2395         }
2396         if (bottom < 1)
2397                 bottom = 1;
2398         if (top < 1)
2399                 top = lno;
2400         bottom--;
2401         if (lno < top)
2402                 die("file %s has only %lu lines", path, lno);
2403
2404         ent = xcalloc(1, sizeof(*ent));
2405         ent->lno = bottom;
2406         ent->num_lines = top - bottom;
2407         ent->suspect = o;
2408         ent->s_lno = bottom;
2409
2410         sb.ent = ent;
2411         sb.path = path;
2412
2413         if (revs_file && read_ancestry(revs_file))
2414                 die("reading graft file %s failed: %s",
2415                     revs_file, strerror(errno));
2416
2417         read_mailmap(&mailmap, ".mailmap", NULL);
2418
2419         assign_blame(&sb, &revs, opt);
2420
2421         if (incremental)
2422                 return 0;
2423
2424         coalesce(&sb);
2425
2426         if (!(output_option & OUTPUT_PORCELAIN))
2427                 find_alignment(&sb, &output_option);
2428
2429         output(&sb, output_option);
2430         free((void *)sb.final_buf);
2431         for (ent = sb.ent; ent; ) {
2432                 struct blame_entry *e = ent->next;
2433                 free(ent);
2434                 ent = e;
2435         }
2436
2437         if (show_stats) {
2438                 printf("num read blob: %d\n", num_read_blob);
2439                 printf("num get patch: %d\n", num_get_patch);
2440                 printf("num commits: %d\n", num_commits);
2441         }
2442         return 0;
2443 }