blame: add the ability to ignore commits and their changes
[git] / blame.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "object-store.h"
4 #include "cache-tree.h"
5 #include "mergesort.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "tag.h"
9 #include "blame.h"
10 #include "alloc.h"
11 #include "commit-slab.h"
12
13 define_commit_slab(blame_suspects, struct blame_origin *);
14 static struct blame_suspects blame_suspects;
15
16 struct blame_origin *get_blame_suspects(struct commit *commit)
17 {
18         struct blame_origin **result;
19
20         result = blame_suspects_peek(&blame_suspects, commit);
21
22         return result ? *result : NULL;
23 }
24
25 static void set_blame_suspects(struct commit *commit, struct blame_origin *origin)
26 {
27         *blame_suspects_at(&blame_suspects, commit) = origin;
28 }
29
30 void blame_origin_decref(struct blame_origin *o)
31 {
32         if (o && --o->refcnt <= 0) {
33                 struct blame_origin *p, *l = NULL;
34                 if (o->previous)
35                         blame_origin_decref(o->previous);
36                 free(o->file.ptr);
37                 /* Should be present exactly once in commit chain */
38                 for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) {
39                         if (p == o) {
40                                 if (l)
41                                         l->next = p->next;
42                                 else
43                                         set_blame_suspects(o->commit, p->next);
44                                 free(o);
45                                 return;
46                         }
47                 }
48                 die("internal error in blame_origin_decref");
49         }
50 }
51
52 /*
53  * Given a commit and a path in it, create a new origin structure.
54  * The callers that add blame to the scoreboard should use
55  * get_origin() to obtain shared, refcounted copy instead of calling
56  * this function directly.
57  */
58 static struct blame_origin *make_origin(struct commit *commit, const char *path)
59 {
60         struct blame_origin *o;
61         FLEX_ALLOC_STR(o, path, path);
62         o->commit = commit;
63         o->refcnt = 1;
64         o->next = get_blame_suspects(commit);
65         set_blame_suspects(commit, o);
66         return o;
67 }
68
69 /*
70  * Locate an existing origin or create a new one.
71  * This moves the origin to front position in the commit util list.
72  */
73 static struct blame_origin *get_origin(struct commit *commit, const char *path)
74 {
75         struct blame_origin *o, *l;
76
77         for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) {
78                 if (!strcmp(o->path, path)) {
79                         /* bump to front */
80                         if (l) {
81                                 l->next = o->next;
82                                 o->next = get_blame_suspects(commit);
83                                 set_blame_suspects(commit, o);
84                         }
85                         return blame_origin_incref(o);
86                 }
87         }
88         return make_origin(commit, path);
89 }
90
91
92
93 static void verify_working_tree_path(struct repository *r,
94                                      struct commit *work_tree, const char *path)
95 {
96         struct commit_list *parents;
97         int pos;
98
99         for (parents = work_tree->parents; parents; parents = parents->next) {
100                 const struct object_id *commit_oid = &parents->item->object.oid;
101                 struct object_id blob_oid;
102                 unsigned mode;
103
104                 if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
105                     oid_object_info(r, &blob_oid, NULL) == OBJ_BLOB)
106                         return;
107         }
108
109         pos = index_name_pos(r->index, path, strlen(path));
110         if (pos >= 0)
111                 ; /* path is in the index */
112         else if (-1 - pos < r->index->cache_nr &&
113                  !strcmp(r->index->cache[-1 - pos]->name, path))
114                 ; /* path is in the index, unmerged */
115         else
116                 die("no such path '%s' in HEAD", path);
117 }
118
119 static struct commit_list **append_parent(struct repository *r,
120                                           struct commit_list **tail,
121                                           const struct object_id *oid)
122 {
123         struct commit *parent;
124
125         parent = lookup_commit_reference(r, oid);
126         if (!parent)
127                 die("no such commit %s", oid_to_hex(oid));
128         return &commit_list_insert(parent, tail)->next;
129 }
130
131 static void append_merge_parents(struct repository *r,
132                                  struct commit_list **tail)
133 {
134         int merge_head;
135         struct strbuf line = STRBUF_INIT;
136
137         merge_head = open(git_path_merge_head(r), O_RDONLY);
138         if (merge_head < 0) {
139                 if (errno == ENOENT)
140                         return;
141                 die("cannot open '%s' for reading",
142                     git_path_merge_head(r));
143         }
144
145         while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
146                 struct object_id oid;
147                 if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
148                         die("unknown line in '%s': %s",
149                             git_path_merge_head(r), line.buf);
150                 tail = append_parent(r, tail, &oid);
151         }
152         close(merge_head);
153         strbuf_release(&line);
154 }
155
156 /*
157  * This isn't as simple as passing sb->buf and sb->len, because we
158  * want to transfer ownership of the buffer to the commit (so we
159  * must use detach).
160  */
161 static void set_commit_buffer_from_strbuf(struct repository *r,
162                                           struct commit *c,
163                                           struct strbuf *sb)
164 {
165         size_t len;
166         void *buf = strbuf_detach(sb, &len);
167         set_commit_buffer(r, c, buf, len);
168 }
169
170 /*
171  * Prepare a dummy commit that represents the work tree (or staged) item.
172  * Note that annotating work tree item never works in the reverse.
173  */
174 static struct commit *fake_working_tree_commit(struct repository *r,
175                                                struct diff_options *opt,
176                                                const char *path,
177                                                const char *contents_from)
178 {
179         struct commit *commit;
180         struct blame_origin *origin;
181         struct commit_list **parent_tail, *parent;
182         struct object_id head_oid;
183         struct strbuf buf = STRBUF_INIT;
184         const char *ident;
185         time_t now;
186         int len;
187         struct cache_entry *ce;
188         unsigned mode;
189         struct strbuf msg = STRBUF_INIT;
190
191         read_index(r->index);
192         time(&now);
193         commit = alloc_commit_node(r);
194         commit->object.parsed = 1;
195         commit->date = now;
196         parent_tail = &commit->parents;
197
198         if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
199                 die("no such ref: HEAD");
200
201         parent_tail = append_parent(r, parent_tail, &head_oid);
202         append_merge_parents(r, parent_tail);
203         verify_working_tree_path(r, commit, path);
204
205         origin = make_origin(commit, path);
206
207         ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
208         strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
209         for (parent = commit->parents; parent; parent = parent->next)
210                 strbuf_addf(&msg, "parent %s\n",
211                             oid_to_hex(&parent->item->object.oid));
212         strbuf_addf(&msg,
213                     "author %s\n"
214                     "committer %s\n\n"
215                     "Version of %s from %s\n",
216                     ident, ident, path,
217                     (!contents_from ? path :
218                      (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
219         set_commit_buffer_from_strbuf(r, commit, &msg);
220
221         if (!contents_from || strcmp("-", contents_from)) {
222                 struct stat st;
223                 const char *read_from;
224                 char *buf_ptr;
225                 unsigned long buf_len;
226
227                 if (contents_from) {
228                         if (stat(contents_from, &st) < 0)
229                                 die_errno("Cannot stat '%s'", contents_from);
230                         read_from = contents_from;
231                 }
232                 else {
233                         if (lstat(path, &st) < 0)
234                                 die_errno("Cannot lstat '%s'", path);
235                         read_from = path;
236                 }
237                 mode = canon_mode(st.st_mode);
238
239                 switch (st.st_mode & S_IFMT) {
240                 case S_IFREG:
241                         if (opt->flags.allow_textconv &&
242                             textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
243                                 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
244                         else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
245                                 die_errno("cannot open or read '%s'", read_from);
246                         break;
247                 case S_IFLNK:
248                         if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
249                                 die_errno("cannot readlink '%s'", read_from);
250                         break;
251                 default:
252                         die("unsupported file type %s", read_from);
253                 }
254         }
255         else {
256                 /* Reading from stdin */
257                 mode = 0;
258                 if (strbuf_read(&buf, 0, 0) < 0)
259                         die_errno("failed to read from stdin");
260         }
261         convert_to_git(r->index, path, buf.buf, buf.len, &buf, 0);
262         origin->file.ptr = buf.buf;
263         origin->file.size = buf.len;
264         pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
265
266         /*
267          * Read the current index, replace the path entry with
268          * origin->blob_sha1 without mucking with its mode or type
269          * bits; we are not going to write this index out -- we just
270          * want to run "diff-index --cached".
271          */
272         discard_index(r->index);
273         read_index(r->index);
274
275         len = strlen(path);
276         if (!mode) {
277                 int pos = index_name_pos(r->index, path, len);
278                 if (0 <= pos)
279                         mode = r->index->cache[pos]->ce_mode;
280                 else
281                         /* Let's not bother reading from HEAD tree */
282                         mode = S_IFREG | 0644;
283         }
284         ce = make_empty_cache_entry(r->index, len);
285         oidcpy(&ce->oid, &origin->blob_oid);
286         memcpy(ce->name, path, len);
287         ce->ce_flags = create_ce_flags(0);
288         ce->ce_namelen = len;
289         ce->ce_mode = create_ce_mode(mode);
290         add_index_entry(r->index, ce,
291                         ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
292
293         cache_tree_invalidate_path(r->index, path);
294
295         return commit;
296 }
297
298
299
300 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
301                       xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
302 {
303         xpparam_t xpp = {0};
304         xdemitconf_t xecfg = {0};
305         xdemitcb_t ecb = {NULL};
306
307         xpp.flags = xdl_opts;
308         xecfg.hunk_func = hunk_func;
309         ecb.priv = cb_data;
310         return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
311 }
312
313 /*
314  * Given an origin, prepare mmfile_t structure to be used by the
315  * diff machinery
316  */
317 static void fill_origin_blob(struct diff_options *opt,
318                              struct blame_origin *o, mmfile_t *file, int *num_read_blob)
319 {
320         if (!o->file.ptr) {
321                 enum object_type type;
322                 unsigned long file_size;
323
324                 (*num_read_blob)++;
325                 if (opt->flags.allow_textconv &&
326                     textconv_object(opt->repo, o->path, o->mode,
327                                     &o->blob_oid, 1, &file->ptr, &file_size))
328                         ;
329                 else
330                         file->ptr = read_object_file(&o->blob_oid, &type,
331                                                      &file_size);
332                 file->size = file_size;
333
334                 if (!file->ptr)
335                         die("Cannot read blob %s for path %s",
336                             oid_to_hex(&o->blob_oid),
337                             o->path);
338                 o->file = *file;
339         }
340         else
341                 *file = o->file;
342 }
343
344 static void drop_origin_blob(struct blame_origin *o)
345 {
346         FREE_AND_NULL(o->file.ptr);
347 }
348
349 /*
350  * Any merge of blames happens on lists of blames that arrived via
351  * different parents in a single suspect.  In this case, we want to
352  * sort according to the suspect line numbers as opposed to the final
353  * image line numbers.  The function body is somewhat longish because
354  * it avoids unnecessary writes.
355  */
356
357 static struct blame_entry *blame_merge(struct blame_entry *list1,
358                                        struct blame_entry *list2)
359 {
360         struct blame_entry *p1 = list1, *p2 = list2,
361                 **tail = &list1;
362
363         if (!p1)
364                 return p2;
365         if (!p2)
366                 return p1;
367
368         if (p1->s_lno <= p2->s_lno) {
369                 do {
370                         tail = &p1->next;
371                         if ((p1 = *tail) == NULL) {
372                                 *tail = p2;
373                                 return list1;
374                         }
375                 } while (p1->s_lno <= p2->s_lno);
376         }
377         for (;;) {
378                 *tail = p2;
379                 do {
380                         tail = &p2->next;
381                         if ((p2 = *tail) == NULL)  {
382                                 *tail = p1;
383                                 return list1;
384                         }
385                 } while (p1->s_lno > p2->s_lno);
386                 *tail = p1;
387                 do {
388                         tail = &p1->next;
389                         if ((p1 = *tail) == NULL) {
390                                 *tail = p2;
391                                 return list1;
392                         }
393                 } while (p1->s_lno <= p2->s_lno);
394         }
395 }
396
397 static void *get_next_blame(const void *p)
398 {
399         return ((struct blame_entry *)p)->next;
400 }
401
402 static void set_next_blame(void *p1, void *p2)
403 {
404         ((struct blame_entry *)p1)->next = p2;
405 }
406
407 /*
408  * Final image line numbers are all different, so we don't need a
409  * three-way comparison here.
410  */
411
412 static int compare_blame_final(const void *p1, const void *p2)
413 {
414         return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
415                 ? 1 : -1;
416 }
417
418 static int compare_blame_suspect(const void *p1, const void *p2)
419 {
420         const struct blame_entry *s1 = p1, *s2 = p2;
421         /*
422          * to allow for collating suspects, we sort according to the
423          * respective pointer value as the primary sorting criterion.
424          * The actual relation is pretty unimportant as long as it
425          * establishes a total order.  Comparing as integers gives us
426          * that.
427          */
428         if (s1->suspect != s2->suspect)
429                 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
430         if (s1->s_lno == s2->s_lno)
431                 return 0;
432         return s1->s_lno > s2->s_lno ? 1 : -1;
433 }
434
435 void blame_sort_final(struct blame_scoreboard *sb)
436 {
437         sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,
438                                   compare_blame_final);
439 }
440
441 static int compare_commits_by_reverse_commit_date(const void *a,
442                                                   const void *b,
443                                                   void *c)
444 {
445         return -compare_commits_by_commit_date(a, b, c);
446 }
447
448 /*
449  * For debugging -- origin is refcounted, and this asserts that
450  * we do not underflow.
451  */
452 static void sanity_check_refcnt(struct blame_scoreboard *sb)
453 {
454         int baa = 0;
455         struct blame_entry *ent;
456
457         for (ent = sb->ent; ent; ent = ent->next) {
458                 /* Nobody should have zero or negative refcnt */
459                 if (ent->suspect->refcnt <= 0) {
460                         fprintf(stderr, "%s in %s has negative refcnt %d\n",
461                                 ent->suspect->path,
462                                 oid_to_hex(&ent->suspect->commit->object.oid),
463                                 ent->suspect->refcnt);
464                         baa = 1;
465                 }
466         }
467         if (baa)
468                 sb->on_sanity_fail(sb, baa);
469 }
470
471 /*
472  * If two blame entries that are next to each other came from
473  * contiguous lines in the same origin (i.e. <commit, path> pair),
474  * merge them together.
475  */
476 void blame_coalesce(struct blame_scoreboard *sb)
477 {
478         struct blame_entry *ent, *next;
479
480         for (ent = sb->ent; ent && (next = ent->next); ent = next) {
481                 if (ent->suspect == next->suspect &&
482                     ent->s_lno + ent->num_lines == next->s_lno) {
483                         ent->num_lines += next->num_lines;
484                         ent->next = next->next;
485                         blame_origin_decref(next->suspect);
486                         free(next);
487                         ent->score = 0;
488                         next = ent; /* again */
489                 }
490         }
491
492         if (sb->debug) /* sanity */
493                 sanity_check_refcnt(sb);
494 }
495
496 /*
497  * Merge the given sorted list of blames into a preexisting origin.
498  * If there were no previous blames to that commit, it is entered into
499  * the commit priority queue of the score board.
500  */
501
502 static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
503                          struct blame_entry *sorted)
504 {
505         if (porigin->suspects)
506                 porigin->suspects = blame_merge(porigin->suspects, sorted);
507         else {
508                 struct blame_origin *o;
509                 for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
510                         if (o->suspects) {
511                                 porigin->suspects = sorted;
512                                 return;
513                         }
514                 }
515                 porigin->suspects = sorted;
516                 prio_queue_put(&sb->commits, porigin->commit);
517         }
518 }
519
520 /*
521  * Fill the blob_sha1 field of an origin if it hasn't, so that later
522  * call to fill_origin_blob() can use it to locate the data.  blob_sha1
523  * for an origin is also used to pass the blame for the entire file to
524  * the parent to detect the case where a child's blob is identical to
525  * that of its parent's.
526  *
527  * This also fills origin->mode for corresponding tree path.
528  */
529 static int fill_blob_sha1_and_mode(struct repository *r,
530                                    struct blame_origin *origin)
531 {
532         if (!is_null_oid(&origin->blob_oid))
533                 return 0;
534         if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
535                 goto error_out;
536         if (oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB)
537                 goto error_out;
538         return 0;
539  error_out:
540         oidclr(&origin->blob_oid);
541         origin->mode = S_IFINVALID;
542         return -1;
543 }
544
545 /*
546  * We have an origin -- check if the same path exists in the
547  * parent and return an origin structure to represent it.
548  */
549 static struct blame_origin *find_origin(struct repository *r,
550                                         struct commit *parent,
551                                         struct blame_origin *origin)
552 {
553         struct blame_origin *porigin;
554         struct diff_options diff_opts;
555         const char *paths[2];
556
557         /* First check any existing origins */
558         for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
559                 if (!strcmp(porigin->path, origin->path)) {
560                         /*
561                          * The same path between origin and its parent
562                          * without renaming -- the most common case.
563                          */
564                         return blame_origin_incref (porigin);
565                 }
566
567         /* See if the origin->path is different between parent
568          * and origin first.  Most of the time they are the
569          * same and diff-tree is fairly efficient about this.
570          */
571         repo_diff_setup(r, &diff_opts);
572         diff_opts.flags.recursive = 1;
573         diff_opts.detect_rename = 0;
574         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
575         paths[0] = origin->path;
576         paths[1] = NULL;
577
578         parse_pathspec(&diff_opts.pathspec,
579                        PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
580                        PATHSPEC_LITERAL_PATH, "", paths);
581         diff_setup_done(&diff_opts);
582
583         if (is_null_oid(&origin->commit->object.oid))
584                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
585         else
586                 diff_tree_oid(get_commit_tree_oid(parent),
587                               get_commit_tree_oid(origin->commit),
588                               "", &diff_opts);
589         diffcore_std(&diff_opts);
590
591         if (!diff_queued_diff.nr) {
592                 /* The path is the same as parent */
593                 porigin = get_origin(parent, origin->path);
594                 oidcpy(&porigin->blob_oid, &origin->blob_oid);
595                 porigin->mode = origin->mode;
596         } else {
597                 /*
598                  * Since origin->path is a pathspec, if the parent
599                  * commit had it as a directory, we will see a whole
600                  * bunch of deletion of files in the directory that we
601                  * do not care about.
602                  */
603                 int i;
604                 struct diff_filepair *p = NULL;
605                 for (i = 0; i < diff_queued_diff.nr; i++) {
606                         const char *name;
607                         p = diff_queued_diff.queue[i];
608                         name = p->one->path ? p->one->path : p->two->path;
609                         if (!strcmp(name, origin->path))
610                                 break;
611                 }
612                 if (!p)
613                         die("internal error in blame::find_origin");
614                 switch (p->status) {
615                 default:
616                         die("internal error in blame::find_origin (%c)",
617                             p->status);
618                 case 'M':
619                         porigin = get_origin(parent, origin->path);
620                         oidcpy(&porigin->blob_oid, &p->one->oid);
621                         porigin->mode = p->one->mode;
622                         break;
623                 case 'A':
624                 case 'T':
625                         /* Did not exist in parent, or type changed */
626                         break;
627                 }
628         }
629         diff_flush(&diff_opts);
630         clear_pathspec(&diff_opts.pathspec);
631         return porigin;
632 }
633
634 /*
635  * We have an origin -- find the path that corresponds to it in its
636  * parent and return an origin structure to represent it.
637  */
638 static struct blame_origin *find_rename(struct repository *r,
639                                         struct commit *parent,
640                                         struct blame_origin *origin)
641 {
642         struct blame_origin *porigin = NULL;
643         struct diff_options diff_opts;
644         int i;
645
646         repo_diff_setup(r, &diff_opts);
647         diff_opts.flags.recursive = 1;
648         diff_opts.detect_rename = DIFF_DETECT_RENAME;
649         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
650         diff_opts.single_follow = origin->path;
651         diff_setup_done(&diff_opts);
652
653         if (is_null_oid(&origin->commit->object.oid))
654                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
655         else
656                 diff_tree_oid(get_commit_tree_oid(parent),
657                               get_commit_tree_oid(origin->commit),
658                               "", &diff_opts);
659         diffcore_std(&diff_opts);
660
661         for (i = 0; i < diff_queued_diff.nr; i++) {
662                 struct diff_filepair *p = diff_queued_diff.queue[i];
663                 if ((p->status == 'R' || p->status == 'C') &&
664                     !strcmp(p->two->path, origin->path)) {
665                         porigin = get_origin(parent, p->one->path);
666                         oidcpy(&porigin->blob_oid, &p->one->oid);
667                         porigin->mode = p->one->mode;
668                         break;
669                 }
670         }
671         diff_flush(&diff_opts);
672         clear_pathspec(&diff_opts.pathspec);
673         return porigin;
674 }
675
676 /*
677  * Append a new blame entry to a given output queue.
678  */
679 static void add_blame_entry(struct blame_entry ***queue,
680                             const struct blame_entry *src)
681 {
682         struct blame_entry *e = xmalloc(sizeof(*e));
683         memcpy(e, src, sizeof(*e));
684         blame_origin_incref(e->suspect);
685
686         e->next = **queue;
687         **queue = e;
688         *queue = &e->next;
689 }
690
691 /*
692  * src typically is on-stack; we want to copy the information in it to
693  * a malloced blame_entry that gets added to the given queue.  The
694  * origin of dst loses a refcnt.
695  */
696 static void dup_entry(struct blame_entry ***queue,
697                       struct blame_entry *dst, struct blame_entry *src)
698 {
699         blame_origin_incref(src->suspect);
700         blame_origin_decref(dst->suspect);
701         memcpy(dst, src, sizeof(*src));
702         dst->next = **queue;
703         **queue = dst;
704         *queue = &dst->next;
705 }
706
707 const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
708 {
709         return sb->final_buf + sb->lineno[lno];
710 }
711
712 /*
713  * It is known that lines between tlno to same came from parent, and e
714  * has an overlap with that range.  it also is known that parent's
715  * line plno corresponds to e's line tlno.
716  *
717  *                <---- e ----->
718  *                   <------>
719  *                   <------------>
720  *             <------------>
721  *             <------------------>
722  *
723  * Split e into potentially three parts; before this chunk, the chunk
724  * to be blamed for the parent, and after that portion.
725  */
726 static void split_overlap(struct blame_entry *split,
727                           struct blame_entry *e,
728                           int tlno, int plno, int same,
729                           struct blame_origin *parent)
730 {
731         int chunk_end_lno;
732         memset(split, 0, sizeof(struct blame_entry [3]));
733
734         if (e->s_lno < tlno) {
735                 /* there is a pre-chunk part not blamed on parent */
736                 split[0].suspect = blame_origin_incref(e->suspect);
737                 split[0].lno = e->lno;
738                 split[0].s_lno = e->s_lno;
739                 split[0].num_lines = tlno - e->s_lno;
740                 split[1].lno = e->lno + tlno - e->s_lno;
741                 split[1].s_lno = plno;
742         }
743         else {
744                 split[1].lno = e->lno;
745                 split[1].s_lno = plno + (e->s_lno - tlno);
746         }
747
748         if (same < e->s_lno + e->num_lines) {
749                 /* there is a post-chunk part not blamed on parent */
750                 split[2].suspect = blame_origin_incref(e->suspect);
751                 split[2].lno = e->lno + (same - e->s_lno);
752                 split[2].s_lno = e->s_lno + (same - e->s_lno);
753                 split[2].num_lines = e->s_lno + e->num_lines - same;
754                 chunk_end_lno = split[2].lno;
755         }
756         else
757                 chunk_end_lno = e->lno + e->num_lines;
758         split[1].num_lines = chunk_end_lno - split[1].lno;
759
760         /*
761          * if it turns out there is nothing to blame the parent for,
762          * forget about the splitting.  !split[1].suspect signals this.
763          */
764         if (split[1].num_lines < 1)
765                 return;
766         split[1].suspect = blame_origin_incref(parent);
767 }
768
769 /*
770  * split_overlap() divided an existing blame e into up to three parts
771  * in split.  Any assigned blame is moved to queue to
772  * reflect the split.
773  */
774 static void split_blame(struct blame_entry ***blamed,
775                         struct blame_entry ***unblamed,
776                         struct blame_entry *split,
777                         struct blame_entry *e)
778 {
779         if (split[0].suspect && split[2].suspect) {
780                 /* The first part (reuse storage for the existing entry e) */
781                 dup_entry(unblamed, e, &split[0]);
782
783                 /* The last part -- me */
784                 add_blame_entry(unblamed, &split[2]);
785
786                 /* ... and the middle part -- parent */
787                 add_blame_entry(blamed, &split[1]);
788         }
789         else if (!split[0].suspect && !split[2].suspect)
790                 /*
791                  * The parent covers the entire area; reuse storage for
792                  * e and replace it with the parent.
793                  */
794                 dup_entry(blamed, e, &split[1]);
795         else if (split[0].suspect) {
796                 /* me and then parent */
797                 dup_entry(unblamed, e, &split[0]);
798                 add_blame_entry(blamed, &split[1]);
799         }
800         else {
801                 /* parent and then me */
802                 dup_entry(blamed, e, &split[1]);
803                 add_blame_entry(unblamed, &split[2]);
804         }
805 }
806
807 /*
808  * After splitting the blame, the origins used by the
809  * on-stack blame_entry should lose one refcnt each.
810  */
811 static void decref_split(struct blame_entry *split)
812 {
813         int i;
814
815         for (i = 0; i < 3; i++)
816                 blame_origin_decref(split[i].suspect);
817 }
818
819 /*
820  * reverse_blame reverses the list given in head, appending tail.
821  * That allows us to build lists in reverse order, then reverse them
822  * afterwards.  This can be faster than building the list in proper
823  * order right away.  The reason is that building in proper order
824  * requires writing a link in the _previous_ element, while building
825  * in reverse order just requires placing the list head into the
826  * _current_ element.
827  */
828
829 static struct blame_entry *reverse_blame(struct blame_entry *head,
830                                          struct blame_entry *tail)
831 {
832         while (head) {
833                 struct blame_entry *next = head->next;
834                 head->next = tail;
835                 tail = head;
836                 head = next;
837         }
838         return tail;
839 }
840
841 /*
842  * Splits a blame entry into two entries at 'len' lines.  The original 'e'
843  * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
844  * which is returned, consists of the remainder: [e->lno + len, e->lno +
845  * e->num_lines).  The caller needs to sort out the reference counting for the
846  * new entry's suspect.
847  */
848 static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
849                                           struct blame_origin *new_suspect)
850 {
851         struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
852
853         n->suspect = new_suspect;
854         n->lno = e->lno + len;
855         n->s_lno = e->s_lno + len;
856         n->num_lines = e->num_lines - len;
857         e->num_lines = len;
858         e->score = 0;
859         return n;
860 }
861
862 struct blame_line_tracker {
863         int is_parent;
864         int s_lno;
865 };
866
867 static int are_lines_adjacent(struct blame_line_tracker *first,
868                               struct blame_line_tracker *second)
869 {
870         return first->is_parent == second->is_parent &&
871                first->s_lno + 1 == second->s_lno;
872 }
873
874 /*
875  * This cheap heuristic assigns lines in the chunk to their relative location in
876  * the parent's chunk.  Any additional lines are left with the target.
877  */
878 static void guess_line_blames(struct blame_origin *parent,
879                               struct blame_origin *target,
880                               int tlno, int offset, int same, int parent_len,
881                               struct blame_line_tracker *line_blames)
882 {
883         int i, best_idx, target_idx;
884         int parent_slno = tlno + offset;
885
886         for (i = 0; i < same - tlno; i++) {
887                 target_idx = tlno + i;
888                 best_idx = target_idx + offset;
889                 if (best_idx < parent_slno + parent_len) {
890                         line_blames[i].is_parent = 1;
891                         line_blames[i].s_lno = best_idx;
892                 } else {
893                         line_blames[i].is_parent = 0;
894                         line_blames[i].s_lno = target_idx;
895                 }
896         }
897 }
898
899 /*
900  * This decides which parts of a blame entry go to the parent (added to the
901  * ignoredp list) and which stay with the target (added to the diffp list).  The
902  * actual decision was made in a separate heuristic function, and those answers
903  * for the lines in 'e' are in line_blames.  This consumes e, essentially
904  * putting it on a list.
905  *
906  * Note that the blame entries on the ignoredp list are not necessarily sorted
907  * with respect to the parent's line numbers yet.
908  */
909 static void ignore_blame_entry(struct blame_entry *e,
910                                struct blame_origin *parent,
911                                struct blame_origin *target,
912                                struct blame_entry **diffp,
913                                struct blame_entry **ignoredp,
914                                struct blame_line_tracker *line_blames)
915 {
916         int entry_len, nr_lines, i;
917
918         /*
919          * We carve new entries off the front of e.  Each entry comes from a
920          * contiguous chunk of lines: adjacent lines from the same origin
921          * (either the parent or the target).
922          */
923         entry_len = 1;
924         nr_lines = e->num_lines;        /* e changes in the loop */
925         for (i = 0; i < nr_lines; i++) {
926                 struct blame_entry *next = NULL;
927
928                 /*
929                  * We are often adjacent to the next line - only split the blame
930                  * entry when we have to.
931                  */
932                 if (i + 1 < nr_lines) {
933                         if (are_lines_adjacent(&line_blames[i],
934                                                &line_blames[i + 1])) {
935                                 entry_len++;
936                                 continue;
937                         }
938                         next = split_blame_at(e, entry_len,
939                                               blame_origin_incref(e->suspect));
940                 }
941                 if (line_blames[i].is_parent) {
942                         blame_origin_decref(e->suspect);
943                         e->suspect = blame_origin_incref(parent);
944                         e->s_lno = line_blames[i - entry_len + 1].s_lno;
945                         e->next = *ignoredp;
946                         *ignoredp = e;
947                 } else {
948                         /* e->s_lno is already in the target's address space. */
949                         e->next = *diffp;
950                         *diffp = e;
951                 }
952                 assert(e->num_lines == entry_len);
953                 e = next;
954                 entry_len = 1;
955         }
956         assert(!e);
957 }
958
959 /*
960  * Process one hunk from the patch between the current suspect for
961  * blame_entry e and its parent.  This first blames any unfinished
962  * entries before the chunk (which is where target and parent start
963  * differing) on the parent, and then splits blame entries at the
964  * start and at the end of the difference region.  Since use of -M and
965  * -C options may lead to overlapping/duplicate source line number
966  * ranges, all we can rely on from sorting/merging is the order of the
967  * first suspect line number.
968  *
969  * tlno: line number in the target where this chunk begins
970  * same: line number in the target where this chunk ends
971  * offset: add to tlno to get the chunk starting point in the parent
972  * parent_len: number of lines in the parent chunk
973  */
974 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
975                         int tlno, int offset, int same, int parent_len,
976                         struct blame_origin *parent,
977                         struct blame_origin *target, int ignore_diffs)
978 {
979         struct blame_entry *e = **srcq;
980         struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL;
981         struct blame_line_tracker *line_blames = NULL;
982
983         while (e && e->s_lno < tlno) {
984                 struct blame_entry *next = e->next;
985                 /*
986                  * current record starts before differing portion.  If
987                  * it reaches into it, we need to split it up and
988                  * examine the second part separately.
989                  */
990                 if (e->s_lno + e->num_lines > tlno) {
991                         /* Move second half to a new record */
992                         struct blame_entry *n;
993
994                         n = split_blame_at(e, tlno - e->s_lno, e->suspect);
995                         /* Push new record to diffp */
996                         n->next = diffp;
997                         diffp = n;
998                 } else
999                         blame_origin_decref(e->suspect);
1000                 /* Pass blame for everything before the differing
1001                  * chunk to the parent */
1002                 e->suspect = blame_origin_incref(parent);
1003                 e->s_lno += offset;
1004                 e->next = samep;
1005                 samep = e;
1006                 e = next;
1007         }
1008         /*
1009          * As we don't know how much of a common stretch after this
1010          * diff will occur, the currently blamed parts are all that we
1011          * can assign to the parent for now.
1012          */
1013
1014         if (samep) {
1015                 **dstq = reverse_blame(samep, **dstq);
1016                 *dstq = &samep->next;
1017         }
1018         /*
1019          * Prepend the split off portions: everything after e starts
1020          * after the blameable portion.
1021          */
1022         e = reverse_blame(diffp, e);
1023
1024         /*
1025          * Now retain records on the target while parts are different
1026          * from the parent.
1027          */
1028         samep = NULL;
1029         diffp = NULL;
1030
1031         if (ignore_diffs && same - tlno > 0) {
1032                 line_blames = xcalloc(sizeof(struct blame_line_tracker),
1033                                       same - tlno);
1034                 guess_line_blames(parent, target, tlno, offset, same,
1035                                   parent_len, line_blames);
1036         }
1037
1038         while (e && e->s_lno < same) {
1039                 struct blame_entry *next = e->next;
1040
1041                 /*
1042                  * If current record extends into sameness, need to split.
1043                  */
1044                 if (e->s_lno + e->num_lines > same) {
1045                         /*
1046                          * Move second half to a new record to be
1047                          * processed by later chunks
1048                          */
1049                         struct blame_entry *n;
1050
1051                         n = split_blame_at(e, same - e->s_lno,
1052                                            blame_origin_incref(e->suspect));
1053                         /* Push new record to samep */
1054                         n->next = samep;
1055                         samep = n;
1056                 }
1057                 if (ignore_diffs) {
1058                         ignore_blame_entry(e, parent, target, &diffp, &ignoredp,
1059                                            line_blames + e->s_lno - tlno);
1060                 } else {
1061                         e->next = diffp;
1062                         diffp = e;
1063                 }
1064                 e = next;
1065         }
1066         free(line_blames);
1067         if (ignoredp) {
1068                 /*
1069                  * Note ignoredp is not sorted yet, and thus neither is dstq.
1070                  * That list must be sorted before we queue_blames().  We defer
1071                  * sorting until after all diff hunks are processed, so that
1072                  * guess_line_blames() can pick *any* line in the parent.  The
1073                  * slight drawback is that we end up sorting all blame entries
1074                  * passed to the parent, including those that are unrelated to
1075                  * changes made by the ignored commit.
1076                  */
1077                 **dstq = reverse_blame(ignoredp, **dstq);
1078                 *dstq = &ignoredp->next;
1079         }
1080         **srcq = reverse_blame(diffp, reverse_blame(samep, e));
1081         /* Move across elements that are in the unblamable portion */
1082         if (diffp)
1083                 *srcq = &diffp->next;
1084 }
1085
1086 struct blame_chunk_cb_data {
1087         struct blame_origin *parent;
1088         struct blame_origin *target;
1089         long offset;
1090         int ignore_diffs;
1091         struct blame_entry **dstq;
1092         struct blame_entry **srcq;
1093 };
1094
1095 /* diff chunks are from parent to target */
1096 static int blame_chunk_cb(long start_a, long count_a,
1097                           long start_b, long count_b, void *data)
1098 {
1099         struct blame_chunk_cb_data *d = data;
1100         if (start_a - start_b != d->offset)
1101                 die("internal error in blame::blame_chunk_cb");
1102         blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
1103                     start_b + count_b, count_a, d->parent, d->target,
1104                     d->ignore_diffs);
1105         d->offset = start_a + count_a - (start_b + count_b);
1106         return 0;
1107 }
1108
1109 /*
1110  * We are looking at the origin 'target' and aiming to pass blame
1111  * for the lines it is suspected to its parent.  Run diff to find
1112  * which lines came from parent and pass blame for them.
1113  */
1114 static void pass_blame_to_parent(struct blame_scoreboard *sb,
1115                                  struct blame_origin *target,
1116                                  struct blame_origin *parent, int ignore_diffs)
1117 {
1118         mmfile_t file_p, file_o;
1119         struct blame_chunk_cb_data d;
1120         struct blame_entry *newdest = NULL;
1121
1122         if (!target->suspects)
1123                 return; /* nothing remains for this target */
1124
1125         d.parent = parent;
1126         d.target = target;
1127         d.offset = 0;
1128         d.ignore_diffs = ignore_diffs;
1129         d.dstq = &newdest; d.srcq = &target->suspects;
1130
1131         fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1132         fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
1133         sb->num_get_patch++;
1134
1135         if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
1136                 die("unable to generate diff (%s -> %s)",
1137                     oid_to_hex(&parent->commit->object.oid),
1138                     oid_to_hex(&target->commit->object.oid));
1139         /* The rest are the same as the parent */
1140         blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
1141                     parent, target, 0);
1142         *d.dstq = NULL;
1143         if (ignore_diffs)
1144                 newdest = llist_mergesort(newdest, get_next_blame,
1145                                           set_next_blame,
1146                                           compare_blame_suspect);
1147         queue_blames(sb, parent, newdest);
1148
1149         return;
1150 }
1151
1152 /*
1153  * The lines in blame_entry after splitting blames many times can become
1154  * very small and trivial, and at some point it becomes pointless to
1155  * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
1156  * ordinary C program, and it is not worth to say it was copied from
1157  * totally unrelated file in the parent.
1158  *
1159  * Compute how trivial the lines in the blame_entry are.
1160  */
1161 unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
1162 {
1163         unsigned score;
1164         const char *cp, *ep;
1165
1166         if (e->score)
1167                 return e->score;
1168
1169         score = 1;
1170         cp = blame_nth_line(sb, e->lno);
1171         ep = blame_nth_line(sb, e->lno + e->num_lines);
1172         while (cp < ep) {
1173                 unsigned ch = *((unsigned char *)cp);
1174                 if (isalnum(ch))
1175                         score++;
1176                 cp++;
1177         }
1178         e->score = score;
1179         return score;
1180 }
1181
1182 /*
1183  * best_so_far[] and potential[] are both a split of an existing blame_entry
1184  * that passes blame to the parent.  Maintain best_so_far the best split so
1185  * far, by comparing potential and best_so_far and copying potential into
1186  * bst_so_far as needed.
1187  */
1188 static void copy_split_if_better(struct blame_scoreboard *sb,
1189                                  struct blame_entry *best_so_far,
1190                                  struct blame_entry *potential)
1191 {
1192         int i;
1193
1194         if (!potential[1].suspect)
1195                 return;
1196         if (best_so_far[1].suspect) {
1197                 if (blame_entry_score(sb, &potential[1]) <
1198                     blame_entry_score(sb, &best_so_far[1]))
1199                         return;
1200         }
1201
1202         for (i = 0; i < 3; i++)
1203                 blame_origin_incref(potential[i].suspect);
1204         decref_split(best_so_far);
1205         memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
1206 }
1207
1208 /*
1209  * We are looking at a part of the final image represented by
1210  * ent (tlno and same are offset by ent->s_lno).
1211  * tlno is where we are looking at in the final image.
1212  * up to (but not including) same match preimage.
1213  * plno is where we are looking at in the preimage.
1214  *
1215  * <-------------- final image ---------------------->
1216  *       <------ent------>
1217  *         ^tlno ^same
1218  *    <---------preimage----->
1219  *         ^plno
1220  *
1221  * All line numbers are 0-based.
1222  */
1223 static void handle_split(struct blame_scoreboard *sb,
1224                          struct blame_entry *ent,
1225                          int tlno, int plno, int same,
1226                          struct blame_origin *parent,
1227                          struct blame_entry *split)
1228 {
1229         if (ent->num_lines <= tlno)
1230                 return;
1231         if (tlno < same) {
1232                 struct blame_entry potential[3];
1233                 tlno += ent->s_lno;
1234                 same += ent->s_lno;
1235                 split_overlap(potential, ent, tlno, plno, same, parent);
1236                 copy_split_if_better(sb, split, potential);
1237                 decref_split(potential);
1238         }
1239 }
1240
1241 struct handle_split_cb_data {
1242         struct blame_scoreboard *sb;
1243         struct blame_entry *ent;
1244         struct blame_origin *parent;
1245         struct blame_entry *split;
1246         long plno;
1247         long tlno;
1248 };
1249
1250 static int handle_split_cb(long start_a, long count_a,
1251                            long start_b, long count_b, void *data)
1252 {
1253         struct handle_split_cb_data *d = data;
1254         handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1255                      d->split);
1256         d->plno = start_a + count_a;
1257         d->tlno = start_b + count_b;
1258         return 0;
1259 }
1260
1261 /*
1262  * Find the lines from parent that are the same as ent so that
1263  * we can pass blames to it.  file_p has the blob contents for
1264  * the parent.
1265  */
1266 static void find_copy_in_blob(struct blame_scoreboard *sb,
1267                               struct blame_entry *ent,
1268                               struct blame_origin *parent,
1269                               struct blame_entry *split,
1270                               mmfile_t *file_p)
1271 {
1272         const char *cp;
1273         mmfile_t file_o;
1274         struct handle_split_cb_data d;
1275
1276         memset(&d, 0, sizeof(d));
1277         d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1278         /*
1279          * Prepare mmfile that contains only the lines in ent.
1280          */
1281         cp = blame_nth_line(sb, ent->lno);
1282         file_o.ptr = (char *) cp;
1283         file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
1284
1285         /*
1286          * file_o is a part of final image we are annotating.
1287          * file_p partially may match that image.
1288          */
1289         memset(split, 0, sizeof(struct blame_entry [3]));
1290         if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
1291                 die("unable to generate diff (%s)",
1292                     oid_to_hex(&parent->commit->object.oid));
1293         /* remainder, if any, all match the preimage */
1294         handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1295 }
1296
1297 /* Move all blame entries from list *source that have a score smaller
1298  * than score_min to the front of list *small.
1299  * Returns a pointer to the link pointing to the old head of the small list.
1300  */
1301
1302 static struct blame_entry **filter_small(struct blame_scoreboard *sb,
1303                                          struct blame_entry **small,
1304                                          struct blame_entry **source,
1305                                          unsigned score_min)
1306 {
1307         struct blame_entry *p = *source;
1308         struct blame_entry *oldsmall = *small;
1309         while (p) {
1310                 if (blame_entry_score(sb, p) <= score_min) {
1311                         *small = p;
1312                         small = &p->next;
1313                         p = *small;
1314                 } else {
1315                         *source = p;
1316                         source = &p->next;
1317                         p = *source;
1318                 }
1319         }
1320         *small = oldsmall;
1321         *source = NULL;
1322         return small;
1323 }
1324
1325 /*
1326  * See if lines currently target is suspected for can be attributed to
1327  * parent.
1328  */
1329 static void find_move_in_parent(struct blame_scoreboard *sb,
1330                                 struct blame_entry ***blamed,
1331                                 struct blame_entry **toosmall,
1332                                 struct blame_origin *target,
1333                                 struct blame_origin *parent)
1334 {
1335         struct blame_entry *e, split[3];
1336         struct blame_entry *unblamed = target->suspects;
1337         struct blame_entry *leftover = NULL;
1338         mmfile_t file_p;
1339
1340         if (!unblamed)
1341                 return; /* nothing remains for this target */
1342
1343         fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1344         if (!file_p.ptr)
1345                 return;
1346
1347         /* At each iteration, unblamed has a NULL-terminated list of
1348          * entries that have not yet been tested for blame.  leftover
1349          * contains the reversed list of entries that have been tested
1350          * without being assignable to the parent.
1351          */
1352         do {
1353                 struct blame_entry **unblamedtail = &unblamed;
1354                 struct blame_entry *next;
1355                 for (e = unblamed; e; e = next) {
1356                         next = e->next;
1357                         find_copy_in_blob(sb, e, parent, split, &file_p);
1358                         if (split[1].suspect &&
1359                             sb->move_score < blame_entry_score(sb, &split[1])) {
1360                                 split_blame(blamed, &unblamedtail, split, e);
1361                         } else {
1362                                 e->next = leftover;
1363                                 leftover = e;
1364                         }
1365                         decref_split(split);
1366                 }
1367                 *unblamedtail = NULL;
1368                 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
1369         } while (unblamed);
1370         target->suspects = reverse_blame(leftover, NULL);
1371 }
1372
1373 struct blame_list {
1374         struct blame_entry *ent;
1375         struct blame_entry split[3];
1376 };
1377
1378 /*
1379  * Count the number of entries the target is suspected for,
1380  * and prepare a list of entry and the best split.
1381  */
1382 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1383                                            int *num_ents_p)
1384 {
1385         struct blame_entry *e;
1386         int num_ents, i;
1387         struct blame_list *blame_list = NULL;
1388
1389         for (e = unblamed, num_ents = 0; e; e = e->next)
1390                 num_ents++;
1391         if (num_ents) {
1392                 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1393                 for (e = unblamed, i = 0; e; e = e->next)
1394                         blame_list[i++].ent = e;
1395         }
1396         *num_ents_p = num_ents;
1397         return blame_list;
1398 }
1399
1400 /*
1401  * For lines target is suspected for, see if we can find code movement
1402  * across file boundary from the parent commit.  porigin is the path
1403  * in the parent we already tried.
1404  */
1405 static void find_copy_in_parent(struct blame_scoreboard *sb,
1406                                 struct blame_entry ***blamed,
1407                                 struct blame_entry **toosmall,
1408                                 struct blame_origin *target,
1409                                 struct commit *parent,
1410                                 struct blame_origin *porigin,
1411                                 int opt)
1412 {
1413         struct diff_options diff_opts;
1414         int i, j;
1415         struct blame_list *blame_list;
1416         int num_ents;
1417         struct blame_entry *unblamed = target->suspects;
1418         struct blame_entry *leftover = NULL;
1419
1420         if (!unblamed)
1421                 return; /* nothing remains for this target */
1422
1423         repo_diff_setup(sb->repo, &diff_opts);
1424         diff_opts.flags.recursive = 1;
1425         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1426
1427         diff_setup_done(&diff_opts);
1428
1429         /* Try "find copies harder" on new path if requested;
1430          * we do not want to use diffcore_rename() actually to
1431          * match things up; find_copies_harder is set only to
1432          * force diff_tree_oid() to feed all filepairs to diff_queue,
1433          * and this code needs to be after diff_setup_done(), which
1434          * usually makes find-copies-harder imply copy detection.
1435          */
1436         if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1437             || ((opt & PICKAXE_BLAME_COPY_HARDER)
1438                 && (!porigin || strcmp(target->path, porigin->path))))
1439                 diff_opts.flags.find_copies_harder = 1;
1440
1441         if (is_null_oid(&target->commit->object.oid))
1442                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1443         else
1444                 diff_tree_oid(get_commit_tree_oid(parent),
1445                               get_commit_tree_oid(target->commit),
1446                               "", &diff_opts);
1447
1448         if (!diff_opts.flags.find_copies_harder)
1449                 diffcore_std(&diff_opts);
1450
1451         do {
1452                 struct blame_entry **unblamedtail = &unblamed;
1453                 blame_list = setup_blame_list(unblamed, &num_ents);
1454
1455                 for (i = 0; i < diff_queued_diff.nr; i++) {
1456                         struct diff_filepair *p = diff_queued_diff.queue[i];
1457                         struct blame_origin *norigin;
1458                         mmfile_t file_p;
1459                         struct blame_entry potential[3];
1460
1461                         if (!DIFF_FILE_VALID(p->one))
1462                                 continue; /* does not exist in parent */
1463                         if (S_ISGITLINK(p->one->mode))
1464                                 continue; /* ignore git links */
1465                         if (porigin && !strcmp(p->one->path, porigin->path))
1466                                 /* find_move already dealt with this path */
1467                                 continue;
1468
1469                         norigin = get_origin(parent, p->one->path);
1470                         oidcpy(&norigin->blob_oid, &p->one->oid);
1471                         norigin->mode = p->one->mode;
1472                         fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1473                         if (!file_p.ptr)
1474                                 continue;
1475
1476                         for (j = 0; j < num_ents; j++) {
1477                                 find_copy_in_blob(sb, blame_list[j].ent,
1478                                                   norigin, potential, &file_p);
1479                                 copy_split_if_better(sb, blame_list[j].split,
1480                                                      potential);
1481                                 decref_split(potential);
1482                         }
1483                         blame_origin_decref(norigin);
1484                 }
1485
1486                 for (j = 0; j < num_ents; j++) {
1487                         struct blame_entry *split = blame_list[j].split;
1488                         if (split[1].suspect &&
1489                             sb->copy_score < blame_entry_score(sb, &split[1])) {
1490                                 split_blame(blamed, &unblamedtail, split,
1491                                             blame_list[j].ent);
1492                         } else {
1493                                 blame_list[j].ent->next = leftover;
1494                                 leftover = blame_list[j].ent;
1495                         }
1496                         decref_split(split);
1497                 }
1498                 free(blame_list);
1499                 *unblamedtail = NULL;
1500                 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
1501         } while (unblamed);
1502         target->suspects = reverse_blame(leftover, NULL);
1503         diff_flush(&diff_opts);
1504         clear_pathspec(&diff_opts.pathspec);
1505 }
1506
1507 /*
1508  * The blobs of origin and porigin exactly match, so everything
1509  * origin is suspected for can be blamed on the parent.
1510  */
1511 static void pass_whole_blame(struct blame_scoreboard *sb,
1512                              struct blame_origin *origin, struct blame_origin *porigin)
1513 {
1514         struct blame_entry *e, *suspects;
1515
1516         if (!porigin->file.ptr && origin->file.ptr) {
1517                 /* Steal its file */
1518                 porigin->file = origin->file;
1519                 origin->file.ptr = NULL;
1520         }
1521         suspects = origin->suspects;
1522         origin->suspects = NULL;
1523         for (e = suspects; e; e = e->next) {
1524                 blame_origin_incref(porigin);
1525                 blame_origin_decref(e->suspect);
1526                 e->suspect = porigin;
1527         }
1528         queue_blames(sb, porigin, suspects);
1529 }
1530
1531 /*
1532  * We pass blame from the current commit to its parents.  We keep saying
1533  * "parent" (and "porigin"), but what we mean is to find scapegoat to
1534  * exonerate ourselves.
1535  */
1536 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
1537                                         int reverse)
1538 {
1539         if (!reverse) {
1540                 if (revs->first_parent_only &&
1541                     commit->parents &&
1542                     commit->parents->next) {
1543                         free_commit_list(commit->parents->next);
1544                         commit->parents->next = NULL;
1545                 }
1546                 return commit->parents;
1547         }
1548         return lookup_decoration(&revs->children, &commit->object);
1549 }
1550
1551 static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
1552 {
1553         struct commit_list *l = first_scapegoat(revs, commit, reverse);
1554         return commit_list_count(l);
1555 }
1556
1557 /* Distribute collected unsorted blames to the respected sorted lists
1558  * in the various origins.
1559  */
1560 static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
1561 {
1562         blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,
1563                                  compare_blame_suspect);
1564         while (blamed)
1565         {
1566                 struct blame_origin *porigin = blamed->suspect;
1567                 struct blame_entry *suspects = NULL;
1568                 do {
1569                         struct blame_entry *next = blamed->next;
1570                         blamed->next = suspects;
1571                         suspects = blamed;
1572                         blamed = next;
1573                 } while (blamed && blamed->suspect == porigin);
1574                 suspects = reverse_blame(suspects, NULL);
1575                 queue_blames(sb, porigin, suspects);
1576         }
1577 }
1578
1579 #define MAXSG 16
1580
1581 static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
1582 {
1583         struct rev_info *revs = sb->revs;
1584         int i, pass, num_sg;
1585         struct commit *commit = origin->commit;
1586         struct commit_list *sg;
1587         struct blame_origin *sg_buf[MAXSG];
1588         struct blame_origin *porigin, **sg_origin = sg_buf;
1589         struct blame_entry *toosmall = NULL;
1590         struct blame_entry *blames, **blametail = &blames;
1591
1592         num_sg = num_scapegoats(revs, commit, sb->reverse);
1593         if (!num_sg)
1594                 goto finish;
1595         else if (num_sg < ARRAY_SIZE(sg_buf))
1596                 memset(sg_buf, 0, sizeof(sg_buf));
1597         else
1598                 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1599
1600         /*
1601          * The first pass looks for unrenamed path to optimize for
1602          * common cases, then we look for renames in the second pass.
1603          */
1604         for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
1605                 struct blame_origin *(*find)(struct repository *, struct commit *, struct blame_origin *);
1606                 find = pass ? find_rename : find_origin;
1607
1608                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1609                      i < num_sg && sg;
1610                      sg = sg->next, i++) {
1611                         struct commit *p = sg->item;
1612                         int j, same;
1613
1614                         if (sg_origin[i])
1615                                 continue;
1616                         if (parse_commit(p))
1617                                 continue;
1618                         porigin = find(sb->repo, p, origin);
1619                         if (!porigin)
1620                                 continue;
1621                         if (oideq(&porigin->blob_oid, &origin->blob_oid)) {
1622                                 pass_whole_blame(sb, origin, porigin);
1623                                 blame_origin_decref(porigin);
1624                                 goto finish;
1625                         }
1626                         for (j = same = 0; j < i; j++)
1627                                 if (sg_origin[j] &&
1628                                     oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
1629                                         same = 1;
1630                                         break;
1631                                 }
1632                         if (!same)
1633                                 sg_origin[i] = porigin;
1634                         else
1635                                 blame_origin_decref(porigin);
1636                 }
1637         }
1638
1639         sb->num_commits++;
1640         for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1641              i < num_sg && sg;
1642              sg = sg->next, i++) {
1643                 struct blame_origin *porigin = sg_origin[i];
1644                 if (!porigin)
1645                         continue;
1646                 if (!origin->previous) {
1647                         blame_origin_incref(porigin);
1648                         origin->previous = porigin;
1649                 }
1650                 pass_blame_to_parent(sb, origin, porigin, 0);
1651                 if (!origin->suspects)
1652                         goto finish;
1653         }
1654
1655         /*
1656          * Pass remaining suspects for ignored commits to their parents.
1657          */
1658         if (oidset_contains(&sb->ignore_list, &commit->object.oid)) {
1659                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1660                      i < num_sg && sg;
1661                      sg = sg->next, i++) {
1662                         struct blame_origin *porigin = sg_origin[i];
1663
1664                         if (!porigin)
1665                                 continue;
1666                         pass_blame_to_parent(sb, origin, porigin, 1);
1667                         if (!origin->suspects)
1668                                 goto finish;
1669                 }
1670         }
1671
1672         /*
1673          * Optionally find moves in parents' files.
1674          */
1675         if (opt & PICKAXE_BLAME_MOVE) {
1676                 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
1677                 if (origin->suspects) {
1678                         for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1679                              i < num_sg && sg;
1680                              sg = sg->next, i++) {
1681                                 struct blame_origin *porigin = sg_origin[i];
1682                                 if (!porigin)
1683                                         continue;
1684                                 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1685                                 if (!origin->suspects)
1686                                         break;
1687                         }
1688                 }
1689         }
1690
1691         /*
1692          * Optionally find copies from parents' files.
1693          */
1694         if (opt & PICKAXE_BLAME_COPY) {
1695                 if (sb->copy_score > sb->move_score)
1696                         filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1697                 else if (sb->copy_score < sb->move_score) {
1698                         origin->suspects = blame_merge(origin->suspects, toosmall);
1699                         toosmall = NULL;
1700                         filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1701                 }
1702                 if (!origin->suspects)
1703                         goto finish;
1704
1705                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1706                      i < num_sg && sg;
1707                      sg = sg->next, i++) {
1708                         struct blame_origin *porigin = sg_origin[i];
1709                         find_copy_in_parent(sb, &blametail, &toosmall,
1710                                             origin, sg->item, porigin, opt);
1711                         if (!origin->suspects)
1712                                 goto finish;
1713                 }
1714         }
1715
1716 finish:
1717         *blametail = NULL;
1718         distribute_blame(sb, blames);
1719         /*
1720          * prepend toosmall to origin->suspects
1721          *
1722          * There is no point in sorting: this ends up on a big
1723          * unsorted list in the caller anyway.
1724          */
1725         if (toosmall) {
1726                 struct blame_entry **tail = &toosmall;
1727                 while (*tail)
1728                         tail = &(*tail)->next;
1729                 *tail = origin->suspects;
1730                 origin->suspects = toosmall;
1731         }
1732         for (i = 0; i < num_sg; i++) {
1733                 if (sg_origin[i]) {
1734                         drop_origin_blob(sg_origin[i]);
1735                         blame_origin_decref(sg_origin[i]);
1736                 }
1737         }
1738         drop_origin_blob(origin);
1739         if (sg_buf != sg_origin)
1740                 free(sg_origin);
1741 }
1742
1743 /*
1744  * The main loop -- while we have blobs with lines whose true origin
1745  * is still unknown, pick one blob, and allow its lines to pass blames
1746  * to its parents. */
1747 void assign_blame(struct blame_scoreboard *sb, int opt)
1748 {
1749         struct rev_info *revs = sb->revs;
1750         struct commit *commit = prio_queue_get(&sb->commits);
1751
1752         while (commit) {
1753                 struct blame_entry *ent;
1754                 struct blame_origin *suspect = get_blame_suspects(commit);
1755
1756                 /* find one suspect to break down */
1757                 while (suspect && !suspect->suspects)
1758                         suspect = suspect->next;
1759
1760                 if (!suspect) {
1761                         commit = prio_queue_get(&sb->commits);
1762                         continue;
1763                 }
1764
1765                 assert(commit == suspect->commit);
1766
1767                 /*
1768                  * We will use this suspect later in the loop,
1769                  * so hold onto it in the meantime.
1770                  */
1771                 blame_origin_incref(suspect);
1772                 parse_commit(commit);
1773                 if (sb->reverse ||
1774                     (!(commit->object.flags & UNINTERESTING) &&
1775                      !(revs->max_age != -1 && commit->date < revs->max_age)))
1776                         pass_blame(sb, suspect, opt);
1777                 else {
1778                         commit->object.flags |= UNINTERESTING;
1779                         if (commit->object.parsed)
1780                                 mark_parents_uninteresting(commit);
1781                 }
1782                 /* treat root commit as boundary */
1783                 if (!commit->parents && !sb->show_root)
1784                         commit->object.flags |= UNINTERESTING;
1785
1786                 /* Take responsibility for the remaining entries */
1787                 ent = suspect->suspects;
1788                 if (ent) {
1789                         suspect->guilty = 1;
1790                         for (;;) {
1791                                 struct blame_entry *next = ent->next;
1792                                 if (sb->found_guilty_entry)
1793                                         sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
1794                                 if (next) {
1795                                         ent = next;
1796                                         continue;
1797                                 }
1798                                 ent->next = sb->ent;
1799                                 sb->ent = suspect->suspects;
1800                                 suspect->suspects = NULL;
1801                                 break;
1802                         }
1803                 }
1804                 blame_origin_decref(suspect);
1805
1806                 if (sb->debug) /* sanity */
1807                         sanity_check_refcnt(sb);
1808         }
1809 }
1810
1811 static const char *get_next_line(const char *start, const char *end)
1812 {
1813         const char *nl = memchr(start, '\n', end - start);
1814         return nl ? nl + 1 : end;
1815 }
1816
1817 /*
1818  * To allow quick access to the contents of nth line in the
1819  * final image, prepare an index in the scoreboard.
1820  */
1821 static int prepare_lines(struct blame_scoreboard *sb)
1822 {
1823         const char *buf = sb->final_buf;
1824         unsigned long len = sb->final_buf_size;
1825         const char *end = buf + len;
1826         const char *p;
1827         int *lineno;
1828         int num = 0;
1829
1830         for (p = buf; p < end; p = get_next_line(p, end))
1831                 num++;
1832
1833         ALLOC_ARRAY(sb->lineno, num + 1);
1834         lineno = sb->lineno;
1835
1836         for (p = buf; p < end; p = get_next_line(p, end))
1837                 *lineno++ = p - buf;
1838
1839         *lineno = len;
1840
1841         sb->num_lines = num;
1842         return sb->num_lines;
1843 }
1844
1845 static struct commit *find_single_final(struct rev_info *revs,
1846                                         const char **name_p)
1847 {
1848         int i;
1849         struct commit *found = NULL;
1850         const char *name = NULL;
1851
1852         for (i = 0; i < revs->pending.nr; i++) {
1853                 struct object *obj = revs->pending.objects[i].item;
1854                 if (obj->flags & UNINTERESTING)
1855                         continue;
1856                 obj = deref_tag(revs->repo, obj, NULL, 0);
1857                 if (obj->type != OBJ_COMMIT)
1858                         die("Non commit %s?", revs->pending.objects[i].name);
1859                 if (found)
1860                         die("More than one commit to dig from %s and %s?",
1861                             revs->pending.objects[i].name, name);
1862                 found = (struct commit *)obj;
1863                 name = revs->pending.objects[i].name;
1864         }
1865         if (name_p)
1866                 *name_p = xstrdup_or_null(name);
1867         return found;
1868 }
1869
1870 static struct commit *dwim_reverse_initial(struct rev_info *revs,
1871                                            const char **name_p)
1872 {
1873         /*
1874          * DWIM "git blame --reverse ONE -- PATH" as
1875          * "git blame --reverse ONE..HEAD -- PATH" but only do so
1876          * when it makes sense.
1877          */
1878         struct object *obj;
1879         struct commit *head_commit;
1880         struct object_id head_oid;
1881
1882         if (revs->pending.nr != 1)
1883                 return NULL;
1884
1885         /* Is that sole rev a committish? */
1886         obj = revs->pending.objects[0].item;
1887         obj = deref_tag(revs->repo, obj, NULL, 0);
1888         if (obj->type != OBJ_COMMIT)
1889                 return NULL;
1890
1891         /* Do we have HEAD? */
1892         if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
1893                 return NULL;
1894         head_commit = lookup_commit_reference_gently(revs->repo,
1895                                                      &head_oid, 1);
1896         if (!head_commit)
1897                 return NULL;
1898
1899         /* Turn "ONE" into "ONE..HEAD" then */
1900         obj->flags |= UNINTERESTING;
1901         add_pending_object(revs, &head_commit->object, "HEAD");
1902
1903         if (name_p)
1904                 *name_p = revs->pending.objects[0].name;
1905         return (struct commit *)obj;
1906 }
1907
1908 static struct commit *find_single_initial(struct rev_info *revs,
1909                                           const char **name_p)
1910 {
1911         int i;
1912         struct commit *found = NULL;
1913         const char *name = NULL;
1914
1915         /*
1916          * There must be one and only one negative commit, and it must be
1917          * the boundary.
1918          */
1919         for (i = 0; i < revs->pending.nr; i++) {
1920                 struct object *obj = revs->pending.objects[i].item;
1921                 if (!(obj->flags & UNINTERESTING))
1922                         continue;
1923                 obj = deref_tag(revs->repo, obj, NULL, 0);
1924                 if (obj->type != OBJ_COMMIT)
1925                         die("Non commit %s?", revs->pending.objects[i].name);
1926                 if (found)
1927                         die("More than one commit to dig up from, %s and %s?",
1928                             revs->pending.objects[i].name, name);
1929                 found = (struct commit *) obj;
1930                 name = revs->pending.objects[i].name;
1931         }
1932
1933         if (!name)
1934                 found = dwim_reverse_initial(revs, &name);
1935         if (!name)
1936                 die("No commit to dig up from?");
1937
1938         if (name_p)
1939                 *name_p = xstrdup(name);
1940         return found;
1941 }
1942
1943 void init_scoreboard(struct blame_scoreboard *sb)
1944 {
1945         memset(sb, 0, sizeof(struct blame_scoreboard));
1946         sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
1947         sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
1948 }
1949
1950 void setup_scoreboard(struct blame_scoreboard *sb,
1951                       const char *path,
1952                       struct blame_origin **orig)
1953 {
1954         const char *final_commit_name = NULL;
1955         struct blame_origin *o;
1956         struct commit *final_commit = NULL;
1957         enum object_type type;
1958
1959         init_blame_suspects(&blame_suspects);
1960
1961         if (sb->reverse && sb->contents_from)
1962                 die(_("--contents and --reverse do not blend well."));
1963
1964         if (!sb->repo)
1965                 BUG("repo is NULL");
1966
1967         if (!sb->reverse) {
1968                 sb->final = find_single_final(sb->revs, &final_commit_name);
1969                 sb->commits.compare = compare_commits_by_commit_date;
1970         } else {
1971                 sb->final = find_single_initial(sb->revs, &final_commit_name);
1972                 sb->commits.compare = compare_commits_by_reverse_commit_date;
1973         }
1974
1975         if (sb->final && sb->contents_from)
1976                 die(_("cannot use --contents with final commit object name"));
1977
1978         if (sb->reverse && sb->revs->first_parent_only)
1979                 sb->revs->children.name = NULL;
1980
1981         if (!sb->final) {
1982                 /*
1983                  * "--not A B -- path" without anything positive;
1984                  * do not default to HEAD, but use the working tree
1985                  * or "--contents".
1986                  */
1987                 setup_work_tree();
1988                 sb->final = fake_working_tree_commit(sb->repo,
1989                                                      &sb->revs->diffopt,
1990                                                      path, sb->contents_from);
1991                 add_pending_object(sb->revs, &(sb->final->object), ":");
1992         }
1993
1994         if (sb->reverse && sb->revs->first_parent_only) {
1995                 final_commit = find_single_final(sb->revs, NULL);
1996                 if (!final_commit)
1997                         die(_("--reverse and --first-parent together require specified latest commit"));
1998         }
1999
2000         /*
2001          * If we have bottom, this will mark the ancestors of the
2002          * bottom commits we would reach while traversing as
2003          * uninteresting.
2004          */
2005         if (prepare_revision_walk(sb->revs))
2006                 die(_("revision walk setup failed"));
2007
2008         if (sb->reverse && sb->revs->first_parent_only) {
2009                 struct commit *c = final_commit;
2010
2011                 sb->revs->children.name = "children";
2012                 while (c->parents &&
2013                        !oideq(&c->object.oid, &sb->final->object.oid)) {
2014                         struct commit_list *l = xcalloc(1, sizeof(*l));
2015
2016                         l->item = c;
2017                         if (add_decoration(&sb->revs->children,
2018                                            &c->parents->item->object, l))
2019                                 BUG("not unique item in first-parent chain");
2020                         c = c->parents->item;
2021                 }
2022
2023                 if (!oideq(&c->object.oid, &sb->final->object.oid))
2024                         die(_("--reverse --first-parent together require range along first-parent chain"));
2025         }
2026
2027         if (is_null_oid(&sb->final->object.oid)) {
2028                 o = get_blame_suspects(sb->final);
2029                 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
2030                 sb->final_buf_size = o->file.size;
2031         }
2032         else {
2033                 o = get_origin(sb->final, path);
2034                 if (fill_blob_sha1_and_mode(sb->repo, o))
2035                         die(_("no such path %s in %s"), path, final_commit_name);
2036
2037                 if (sb->revs->diffopt.flags.allow_textconv &&
2038                     textconv_object(sb->repo, path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
2039                                     &sb->final_buf_size))
2040                         ;
2041                 else
2042                         sb->final_buf = read_object_file(&o->blob_oid, &type,
2043                                                          &sb->final_buf_size);
2044
2045                 if (!sb->final_buf)
2046                         die(_("cannot read blob %s for path %s"),
2047                             oid_to_hex(&o->blob_oid),
2048                             path);
2049         }
2050         sb->num_read_blob++;
2051         prepare_lines(sb);
2052
2053         if (orig)
2054                 *orig = o;
2055
2056         free((char *)final_commit_name);
2057 }
2058
2059
2060
2061 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
2062                                         long start, long end,
2063                                         struct blame_origin *o)
2064 {
2065         struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
2066         new_head->lno = start;
2067         new_head->num_lines = end - start;
2068         new_head->suspect = o;
2069         new_head->s_lno = start;
2070         new_head->next = head;
2071         blame_origin_incref(o);
2072         return new_head;
2073 }