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