Merge branch 'dl/rev-list-doc-cleanup'
[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 short mode;
103
104                 if (!get_tree_entry(r, 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 (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         repo_read_index(r);
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",
208                         WANT_BLANK_IDENT, NULL, 0);
209         strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
210         for (parent = commit->parents; parent; parent = parent->next)
211                 strbuf_addf(&msg, "parent %s\n",
212                             oid_to_hex(&parent->item->object.oid));
213         strbuf_addf(&msg,
214                     "author %s\n"
215                     "committer %s\n\n"
216                     "Version of %s from %s\n",
217                     ident, ident, path,
218                     (!contents_from ? path :
219                      (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
220         set_commit_buffer_from_strbuf(r, commit, &msg);
221
222         if (!contents_from || strcmp("-", contents_from)) {
223                 struct stat st;
224                 const char *read_from;
225                 char *buf_ptr;
226                 unsigned long buf_len;
227
228                 if (contents_from) {
229                         if (stat(contents_from, &st) < 0)
230                                 die_errno("Cannot stat '%s'", contents_from);
231                         read_from = contents_from;
232                 }
233                 else {
234                         if (lstat(path, &st) < 0)
235                                 die_errno("Cannot lstat '%s'", path);
236                         read_from = path;
237                 }
238                 mode = canon_mode(st.st_mode);
239
240                 switch (st.st_mode & S_IFMT) {
241                 case S_IFREG:
242                         if (opt->flags.allow_textconv &&
243                             textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
244                                 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
245                         else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
246                                 die_errno("cannot open or read '%s'", read_from);
247                         break;
248                 case S_IFLNK:
249                         if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
250                                 die_errno("cannot readlink '%s'", read_from);
251                         break;
252                 default:
253                         die("unsupported file type %s", read_from);
254                 }
255         }
256         else {
257                 /* Reading from stdin */
258                 mode = 0;
259                 if (strbuf_read(&buf, 0, 0) < 0)
260                         die_errno("failed to read from stdin");
261         }
262         convert_to_git(r->index, path, buf.buf, buf.len, &buf, 0);
263         origin->file.ptr = buf.buf;
264         origin->file.size = buf.len;
265         pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
266
267         /*
268          * Read the current index, replace the path entry with
269          * origin->blob_sha1 without mucking with its mode or type
270          * bits; we are not going to write this index out -- we just
271          * want to run "diff-index --cached".
272          */
273         discard_index(r->index);
274         repo_read_index(r);
275
276         len = strlen(path);
277         if (!mode) {
278                 int pos = index_name_pos(r->index, path, len);
279                 if (0 <= pos)
280                         mode = r->index->cache[pos]->ce_mode;
281                 else
282                         /* Let's not bother reading from HEAD tree */
283                         mode = S_IFREG | 0644;
284         }
285         ce = make_empty_cache_entry(r->index, len);
286         oidcpy(&ce->oid, &origin->blob_oid);
287         memcpy(ce->name, path, len);
288         ce->ce_flags = create_ce_flags(0);
289         ce->ce_namelen = len;
290         ce->ce_mode = create_ce_mode(mode);
291         add_index_entry(r->index, ce,
292                         ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
293
294         cache_tree_invalidate_path(r->index, path);
295
296         return commit;
297 }
298
299
300
301 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
302                       xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
303 {
304         xpparam_t xpp = {0};
305         xdemitconf_t xecfg = {0};
306         xdemitcb_t ecb = {NULL};
307
308         xpp.flags = xdl_opts;
309         xecfg.hunk_func = hunk_func;
310         ecb.priv = cb_data;
311         return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
312 }
313
314 static const char *get_next_line(const char *start, const char *end)
315 {
316         const char *nl = memchr(start, '\n', end - start);
317
318         return nl ? nl + 1 : end;
319 }
320
321 static int find_line_starts(int **line_starts, const char *buf,
322                             unsigned long len)
323 {
324         const char *end = buf + len;
325         const char *p;
326         int *lineno;
327         int num = 0;
328
329         for (p = buf; p < end; p = get_next_line(p, end))
330                 num++;
331
332         ALLOC_ARRAY(*line_starts, num + 1);
333         lineno = *line_starts;
334
335         for (p = buf; p < end; p = get_next_line(p, end))
336                 *lineno++ = p - buf;
337
338         *lineno = len;
339
340         return num;
341 }
342
343 struct fingerprint_entry;
344
345 /* A fingerprint is intended to loosely represent a string, such that two
346  * fingerprints can be quickly compared to give an indication of the similarity
347  * of the strings that they represent.
348  *
349  * A fingerprint is represented as a multiset of the lower-cased byte pairs in
350  * the string that it represents. Whitespace is added at each end of the
351  * string. Whitespace pairs are ignored. Whitespace is converted to '\0'.
352  * For example, the string "Darth   Radar" will be converted to the following
353  * fingerprint:
354  * {"\0d", "da", "da", "ar", "ar", "rt", "th", "h\0", "\0r", "ra", "ad", "r\0"}
355  *
356  * The similarity between two fingerprints is the size of the intersection of
357  * their multisets, including repeated elements. See fingerprint_similarity for
358  * examples.
359  *
360  * For ease of implementation, the fingerprint is implemented as a map
361  * of byte pairs to the count of that byte pair in the string, instead of
362  * allowing repeated elements in a set.
363  */
364 struct fingerprint {
365         struct hashmap map;
366         /* As we know the maximum number of entries in advance, it's
367          * convenient to store the entries in a single array instead of having
368          * the hashmap manage the memory.
369          */
370         struct fingerprint_entry *entries;
371 };
372
373 /* A byte pair in a fingerprint. Stores the number of times the byte pair
374  * occurs in the string that the fingerprint represents.
375  */
376 struct fingerprint_entry {
377         /* The hashmap entry - the hash represents the byte pair in its
378          * entirety so we don't need to store the byte pair separately.
379          */
380         struct hashmap_entry entry;
381         /* The number of times the byte pair occurs in the string that the
382          * fingerprint represents.
383          */
384         int count;
385 };
386
387 /* See `struct fingerprint` for an explanation of what a fingerprint is.
388  * \param result the fingerprint of the string is stored here. This must be
389  *               freed later using free_fingerprint.
390  * \param line_begin the start of the string
391  * \param line_end the end of the string
392  */
393 static void get_fingerprint(struct fingerprint *result,
394                             const char *line_begin,
395                             const char *line_end)
396 {
397         unsigned int hash, c0 = 0, c1;
398         const char *p;
399         int max_map_entry_count = 1 + line_end - line_begin;
400         struct fingerprint_entry *entry = xcalloc(max_map_entry_count,
401                 sizeof(struct fingerprint_entry));
402         struct fingerprint_entry *found_entry;
403
404         hashmap_init(&result->map, NULL, NULL, max_map_entry_count);
405         result->entries = entry;
406         for (p = line_begin; p <= line_end; ++p, c0 = c1) {
407                 /* Always terminate the string with whitespace.
408                  * Normalise whitespace to 0, and normalise letters to
409                  * lower case. This won't work for multibyte characters but at
410                  * worst will match some unrelated characters.
411                  */
412                 if ((p == line_end) || isspace(*p))
413                         c1 = 0;
414                 else
415                         c1 = tolower(*p);
416                 hash = c0 | (c1 << 8);
417                 /* Ignore whitespace pairs */
418                 if (hash == 0)
419                         continue;
420                 hashmap_entry_init(entry, hash);
421
422                 found_entry = hashmap_get(&result->map, entry, NULL);
423                 if (found_entry) {
424                         found_entry->count += 1;
425                 } else {
426                         entry->count = 1;
427                         hashmap_add(&result->map, entry);
428                         ++entry;
429                 }
430         }
431 }
432
433 static void free_fingerprint(struct fingerprint *f)
434 {
435         hashmap_free(&f->map, 0);
436         free(f->entries);
437 }
438
439 /* Calculates the similarity between two fingerprints as the size of the
440  * intersection of their multisets, including repeated elements. See
441  * `struct fingerprint` for an explanation of the fingerprint representation.
442  * The similarity between "cat mat" and "father rather" is 2 because "at" is
443  * present twice in both strings while the similarity between "tim" and "mit"
444  * is 0.
445  */
446 static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
447 {
448         int intersection = 0;
449         struct hashmap_iter iter;
450         const struct fingerprint_entry *entry_a, *entry_b;
451
452         hashmap_iter_init(&b->map, &iter);
453
454         while ((entry_b = hashmap_iter_next(&iter))) {
455                 if ((entry_a = hashmap_get(&a->map, entry_b, NULL))) {
456                         intersection += entry_a->count < entry_b->count ?
457                                         entry_a->count : entry_b->count;
458                 }
459         }
460         return intersection;
461 }
462
463 /* Subtracts byte-pair elements in B from A, modifying A in place.
464  */
465 static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
466 {
467         struct hashmap_iter iter;
468         struct fingerprint_entry *entry_a;
469         const struct fingerprint_entry *entry_b;
470
471         hashmap_iter_init(&b->map, &iter);
472
473         while ((entry_b = hashmap_iter_next(&iter))) {
474                 if ((entry_a = hashmap_get(&a->map, entry_b, NULL))) {
475                         if (entry_a->count <= entry_b->count)
476                                 hashmap_remove(&a->map, entry_b, NULL);
477                         else
478                                 entry_a->count -= entry_b->count;
479                 }
480         }
481 }
482
483 /* Calculate fingerprints for a series of lines.
484  * Puts the fingerprints in the fingerprints array, which must have been
485  * preallocated to allow storing line_count elements.
486  */
487 static void get_line_fingerprints(struct fingerprint *fingerprints,
488                                   const char *content, const int *line_starts,
489                                   long first_line, long line_count)
490 {
491         int i;
492         const char *linestart, *lineend;
493
494         line_starts += first_line;
495         for (i = 0; i < line_count; ++i) {
496                 linestart = content + line_starts[i];
497                 lineend = content + line_starts[i + 1];
498                 get_fingerprint(fingerprints + i, linestart, lineend);
499         }
500 }
501
502 static void free_line_fingerprints(struct fingerprint *fingerprints,
503                                    int nr_fingerprints)
504 {
505         int i;
506
507         for (i = 0; i < nr_fingerprints; i++)
508                 free_fingerprint(&fingerprints[i]);
509 }
510
511 /* This contains the data necessary to linearly map a line number in one half
512  * of a diff chunk to the line in the other half of the diff chunk that is
513  * closest in terms of its position as a fraction of the length of the chunk.
514  */
515 struct line_number_mapping {
516         int destination_start, destination_length,
517                 source_start, source_length;
518 };
519
520 /* Given a line number in one range, offset and scale it to map it onto the
521  * other range.
522  * Essentially this mapping is a simple linear equation but the calculation is
523  * more complicated to allow performing it with integer operations.
524  * Another complication is that if a line could map onto many lines in the
525  * destination range then we want to choose the line at the center of those
526  * possibilities.
527  * Example: if the chunk is 2 lines long in A and 10 lines long in B then the
528  * first 5 lines in B will map onto the first line in the A chunk, while the
529  * last 5 lines will all map onto the second line in the A chunk.
530  * Example: if the chunk is 10 lines long in A and 2 lines long in B then line
531  * 0 in B will map onto line 2 in A, and line 1 in B will map onto line 7 in A.
532  */
533 static int map_line_number(int line_number,
534         const struct line_number_mapping *mapping)
535 {
536         return ((line_number - mapping->source_start) * 2 + 1) *
537                mapping->destination_length /
538                (mapping->source_length * 2) +
539                mapping->destination_start;
540 }
541
542 /* Get a pointer to the element storing the similarity between a line in A
543  * and a line in B.
544  *
545  * The similarities are stored in a 2-dimensional array. Each "row" in the
546  * array contains the similarities for a line in B. The similarities stored in
547  * a row are the similarities between the line in B and the nearby lines in A.
548  * To keep the length of each row the same, it is padded out with values of -1
549  * where the search range extends beyond the lines in A.
550  * For example, if max_search_distance_a is 2 and the two sides of a diff chunk
551  * look like this:
552  * a | m
553  * b | n
554  * c | o
555  * d | p
556  * e | q
557  * Then the similarity array will contain:
558  * [-1, -1, am, bm, cm,
559  *  -1, an, bn, cn, dn,
560  *  ao, bo, co, do, eo,
561  *  bp, cp, dp, ep, -1,
562  *  cq, dq, eq, -1, -1]
563  * Where similarities are denoted either by -1 for invalid, or the
564  * concatenation of the two lines in the diff being compared.
565  *
566  * \param similarities array of similarities between lines in A and B
567  * \param line_a the index of the line in A, in the same frame of reference as
568  *      closest_line_a.
569  * \param local_line_b the index of the line in B, relative to the first line
570  *                     in B that similarities represents.
571  * \param closest_line_a the index of the line in A that is deemed to be
572  *                       closest to local_line_b. This must be in the same
573  *                       frame of reference as line_a. This value defines
574  *                       where similarities is centered for the line in B.
575  * \param max_search_distance_a maximum distance in lines from the closest line
576  *                              in A for other lines in A for which
577  *                              similarities may be calculated.
578  */
579 static int *get_similarity(int *similarities,
580                            int line_a, int local_line_b,
581                            int closest_line_a, int max_search_distance_a)
582 {
583         assert(abs(line_a - closest_line_a) <=
584                max_search_distance_a);
585         return similarities + line_a - closest_line_a +
586                max_search_distance_a +
587                local_line_b * (max_search_distance_a * 2 + 1);
588 }
589
590 #define CERTAIN_NOTHING_MATCHES -2
591 #define CERTAINTY_NOT_CALCULATED -1
592
593 /* Given a line in B, first calculate its similarities with nearby lines in A
594  * if not already calculated, then identify the most similar and second most
595  * similar lines. The "certainty" is calculated based on those two
596  * similarities.
597  *
598  * \param start_a the index of the first line of the chunk in A
599  * \param length_a the length in lines of the chunk in A
600  * \param local_line_b the index of the line in B, relative to the first line
601  *                     in the chunk.
602  * \param fingerprints_a array of fingerprints for the chunk in A
603  * \param fingerprints_b array of fingerprints for the chunk in B
604  * \param similarities 2-dimensional array of similarities between lines in A
605  *                     and B. See get_similarity() for more details.
606  * \param certainties array of values indicating how strongly a line in B is
607  *                    matched with some line in A.
608  * \param second_best_result array of absolute indices in A for the second
609  *                           closest match of a line in B.
610  * \param result array of absolute indices in A for the closest match of a line
611  *               in B.
612  * \param max_search_distance_a maximum distance in lines from the closest line
613  *                              in A for other lines in A for which
614  *                              similarities may be calculated.
615  * \param map_line_number_in_b_to_a parameter to map_line_number().
616  */
617 static void find_best_line_matches(
618         int start_a,
619         int length_a,
620         int start_b,
621         int local_line_b,
622         struct fingerprint *fingerprints_a,
623         struct fingerprint *fingerprints_b,
624         int *similarities,
625         int *certainties,
626         int *second_best_result,
627         int *result,
628         const int max_search_distance_a,
629         const struct line_number_mapping *map_line_number_in_b_to_a)
630 {
631
632         int i, search_start, search_end, closest_local_line_a, *similarity,
633                 best_similarity = 0, second_best_similarity = 0,
634                 best_similarity_index = 0, second_best_similarity_index = 0;
635
636         /* certainty has already been calculated so no need to redo the work */
637         if (certainties[local_line_b] != CERTAINTY_NOT_CALCULATED)
638                 return;
639
640         closest_local_line_a = map_line_number(
641                 local_line_b + start_b, map_line_number_in_b_to_a) - start_a;
642
643         search_start = closest_local_line_a - max_search_distance_a;
644         if (search_start < 0)
645                 search_start = 0;
646
647         search_end = closest_local_line_a + max_search_distance_a + 1;
648         if (search_end > length_a)
649                 search_end = length_a;
650
651         for (i = search_start; i < search_end; ++i) {
652                 similarity = get_similarity(similarities,
653                                             i, local_line_b,
654                                             closest_local_line_a,
655                                             max_search_distance_a);
656                 if (*similarity == -1) {
657                         /* This value will never exceed 10 but assert just in
658                          * case
659                          */
660                         assert(abs(i - closest_local_line_a) < 1000);
661                         /* scale the similarity by (1000 - distance from
662                          * closest line) to act as a tie break between lines
663                          * that otherwise are equally similar.
664                          */
665                         *similarity = fingerprint_similarity(
666                                 fingerprints_b + local_line_b,
667                                 fingerprints_a + i) *
668                                 (1000 - abs(i - closest_local_line_a));
669                 }
670                 if (*similarity > best_similarity) {
671                         second_best_similarity = best_similarity;
672                         second_best_similarity_index = best_similarity_index;
673                         best_similarity = *similarity;
674                         best_similarity_index = i;
675                 } else if (*similarity > second_best_similarity) {
676                         second_best_similarity = *similarity;
677                         second_best_similarity_index = i;
678                 }
679         }
680
681         if (best_similarity == 0) {
682                 /* this line definitely doesn't match with anything. Mark it
683                  * with this special value so it doesn't get invalidated and
684                  * won't be recalculated.
685                  */
686                 certainties[local_line_b] = CERTAIN_NOTHING_MATCHES;
687                 result[local_line_b] = -1;
688         } else {
689                 /* Calculate the certainty with which this line matches.
690                  * If the line matches well with two lines then that reduces
691                  * the certainty. However we still want to prioritise matching
692                  * a line that matches very well with two lines over matching a
693                  * line that matches poorly with one line, hence doubling
694                  * best_similarity.
695                  * This means that if we have
696                  * line X that matches only one line with a score of 3,
697                  * line Y that matches two lines equally with a score of 5,
698                  * and line Z that matches only one line with a score or 2,
699                  * then the lines in order of certainty are X, Y, Z.
700                  */
701                 certainties[local_line_b] = best_similarity * 2 -
702                         second_best_similarity;
703
704                 /* We keep both the best and second best results to allow us to
705                  * check at a later stage of the matching process whether the
706                  * result needs to be invalidated.
707                  */
708                 result[local_line_b] = start_a + best_similarity_index;
709                 second_best_result[local_line_b] =
710                         start_a + second_best_similarity_index;
711         }
712 }
713
714 /*
715  * This finds the line that we can match with the most confidence, and
716  * uses it as a partition. It then calls itself on the lines on either side of
717  * that partition. In this way we avoid lines appearing out of order, and
718  * retain a sensible line ordering.
719  * \param start_a index of the first line in A with which lines in B may be
720  *                compared.
721  * \param start_b index of the first line in B for which matching should be
722  *                done.
723  * \param length_a number of lines in A with which lines in B may be compared.
724  * \param length_b number of lines in B for which matching should be done.
725  * \param fingerprints_a mutable array of fingerprints in A. The first element
726  *                       corresponds to the line at start_a.
727  * \param fingerprints_b array of fingerprints in B. The first element
728  *                       corresponds to the line at start_b.
729  * \param similarities 2-dimensional array of similarities between lines in A
730  *                     and B. See get_similarity() for more details.
731  * \param certainties array of values indicating how strongly a line in B is
732  *                    matched with some line in A.
733  * \param second_best_result array of absolute indices in A for the second
734  *                           closest match of a line in B.
735  * \param result array of absolute indices in A for the closest match of a line
736  *               in B.
737  * \param max_search_distance_a maximum distance in lines from the closest line
738  *                            in A for other lines in A for which
739  *                            similarities may be calculated.
740  * \param max_search_distance_b an upper bound on the greatest possible
741  *                            distance between lines in B such that they will
742  *                              both be compared with the same line in A
743  *                            according to max_search_distance_a.
744  * \param map_line_number_in_b_to_a parameter to map_line_number().
745  */
746 static void fuzzy_find_matching_lines_recurse(
747         int start_a, int start_b,
748         int length_a, int length_b,
749         struct fingerprint *fingerprints_a,
750         struct fingerprint *fingerprints_b,
751         int *similarities,
752         int *certainties,
753         int *second_best_result,
754         int *result,
755         int max_search_distance_a,
756         int max_search_distance_b,
757         const struct line_number_mapping *map_line_number_in_b_to_a)
758 {
759         int i, invalidate_min, invalidate_max, offset_b,
760                 second_half_start_a, second_half_start_b,
761                 second_half_length_a, second_half_length_b,
762                 most_certain_line_a, most_certain_local_line_b = -1,
763                 most_certain_line_certainty = -1,
764                 closest_local_line_a;
765
766         for (i = 0; i < length_b; ++i) {
767                 find_best_line_matches(start_a,
768                                        length_a,
769                                        start_b,
770                                        i,
771                                        fingerprints_a,
772                                        fingerprints_b,
773                                        similarities,
774                                        certainties,
775                                        second_best_result,
776                                        result,
777                                        max_search_distance_a,
778                                        map_line_number_in_b_to_a);
779
780                 if (certainties[i] > most_certain_line_certainty) {
781                         most_certain_line_certainty = certainties[i];
782                         most_certain_local_line_b = i;
783                 }
784         }
785
786         /* No matches. */
787         if (most_certain_local_line_b == -1)
788                 return;
789
790         most_certain_line_a = result[most_certain_local_line_b];
791
792         /*
793          * Subtract the most certain line's fingerprint in B from the matched
794          * fingerprint in A. This means that other lines in B can't also match
795          * the same parts of the line in A.
796          */
797         fingerprint_subtract(fingerprints_a + most_certain_line_a - start_a,
798                              fingerprints_b + most_certain_local_line_b);
799
800         /* Invalidate results that may be affected by the choice of most
801          * certain line.
802          */
803         invalidate_min = most_certain_local_line_b - max_search_distance_b;
804         invalidate_max = most_certain_local_line_b + max_search_distance_b + 1;
805         if (invalidate_min < 0)
806                 invalidate_min = 0;
807         if (invalidate_max > length_b)
808                 invalidate_max = length_b;
809
810         /* As the fingerprint in A has changed, discard previously calculated
811          * similarity values with that fingerprint.
812          */
813         for (i = invalidate_min; i < invalidate_max; ++i) {
814                 closest_local_line_a = map_line_number(
815                         i + start_b, map_line_number_in_b_to_a) - start_a;
816
817                 /* Check that the lines in A and B are close enough that there
818                  * is a similarity value for them.
819                  */
820                 if (abs(most_certain_line_a - start_a - closest_local_line_a) >
821                         max_search_distance_a) {
822                         continue;
823                 }
824
825                 *get_similarity(similarities, most_certain_line_a - start_a,
826                                 i, closest_local_line_a,
827                                 max_search_distance_a) = -1;
828         }
829
830         /* More invalidating of results that may be affected by the choice of
831          * most certain line.
832          * Discard the matches for lines in B that are currently matched with a
833          * line in A such that their ordering contradicts the ordering imposed
834          * by the choice of most certain line.
835          */
836         for (i = most_certain_local_line_b - 1; i >= invalidate_min; --i) {
837                 /* In this loop we discard results for lines in B that are
838                  * before most-certain-line-B but are matched with a line in A
839                  * that is after most-certain-line-A.
840                  */
841                 if (certainties[i] >= 0 &&
842                     (result[i] >= most_certain_line_a ||
843                      second_best_result[i] >= most_certain_line_a)) {
844                         certainties[i] = CERTAINTY_NOT_CALCULATED;
845                 }
846         }
847         for (i = most_certain_local_line_b + 1; i < invalidate_max; ++i) {
848                 /* In this loop we discard results for lines in B that are
849                  * after most-certain-line-B but are matched with a line in A
850                  * that is before most-certain-line-A.
851                  */
852                 if (certainties[i] >= 0 &&
853                     (result[i] <= most_certain_line_a ||
854                      second_best_result[i] <= most_certain_line_a)) {
855                         certainties[i] = CERTAINTY_NOT_CALCULATED;
856                 }
857         }
858
859         /* Repeat the matching process for lines before the most certain line.
860          */
861         if (most_certain_local_line_b > 0) {
862                 fuzzy_find_matching_lines_recurse(
863                         start_a, start_b,
864                         most_certain_line_a + 1 - start_a,
865                         most_certain_local_line_b,
866                         fingerprints_a, fingerprints_b, similarities,
867                         certainties, second_best_result, result,
868                         max_search_distance_a,
869                         max_search_distance_b,
870                         map_line_number_in_b_to_a);
871         }
872         /* Repeat the matching process for lines after the most certain line.
873          */
874         if (most_certain_local_line_b + 1 < length_b) {
875                 second_half_start_a = most_certain_line_a;
876                 offset_b = most_certain_local_line_b + 1;
877                 second_half_start_b = start_b + offset_b;
878                 second_half_length_a =
879                         length_a + start_a - second_half_start_a;
880                 second_half_length_b =
881                         length_b + start_b - second_half_start_b;
882                 fuzzy_find_matching_lines_recurse(
883                         second_half_start_a, second_half_start_b,
884                         second_half_length_a, second_half_length_b,
885                         fingerprints_a + second_half_start_a - start_a,
886                         fingerprints_b + offset_b,
887                         similarities +
888                                 offset_b * (max_search_distance_a * 2 + 1),
889                         certainties + offset_b,
890                         second_best_result + offset_b, result + offset_b,
891                         max_search_distance_a,
892                         max_search_distance_b,
893                         map_line_number_in_b_to_a);
894         }
895 }
896
897 /* Find the lines in the parent line range that most closely match the lines in
898  * the target line range. This is accomplished by matching fingerprints in each
899  * blame_origin, and choosing the best matches that preserve the line ordering.
900  * See struct fingerprint for details of fingerprint matching, and
901  * fuzzy_find_matching_lines_recurse for details of preserving line ordering.
902  *
903  * The performance is believed to be O(n log n) in the typical case and O(n^2)
904  * in a pathological case, where n is the number of lines in the target range.
905  */
906 static int *fuzzy_find_matching_lines(struct blame_origin *parent,
907                                       struct blame_origin *target,
908                                       int tlno, int parent_slno, int same,
909                                       int parent_len)
910 {
911         /* We use the terminology "A" for the left hand side of the diff AKA
912          * parent, and "B" for the right hand side of the diff AKA target. */
913         int start_a = parent_slno;
914         int length_a = parent_len;
915         int start_b = tlno;
916         int length_b = same - tlno;
917
918         struct line_number_mapping map_line_number_in_b_to_a = {
919                 start_a, length_a, start_b, length_b
920         };
921
922         struct fingerprint *fingerprints_a = parent->fingerprints;
923         struct fingerprint *fingerprints_b = target->fingerprints;
924
925         int i, *result, *second_best_result,
926                 *certainties, *similarities, similarity_count;
927
928         /*
929          * max_search_distance_a means that given a line in B, compare it to
930          * the line in A that is closest to its position, and the lines in A
931          * that are no greater than max_search_distance_a lines away from the
932          * closest line in A.
933          *
934          * max_search_distance_b is an upper bound on the greatest possible
935          * distance between lines in B such that they will both be compared
936          * with the same line in A according to max_search_distance_a.
937          */
938         int max_search_distance_a = 10, max_search_distance_b;
939
940         if (length_a <= 0)
941                 return NULL;
942
943         if (max_search_distance_a >= length_a)
944                 max_search_distance_a = length_a ? length_a - 1 : 0;
945
946         max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
947                                  - 1) / length_a;
948
949         result = xcalloc(sizeof(int), length_b);
950         second_best_result = xcalloc(sizeof(int), length_b);
951         certainties = xcalloc(sizeof(int), length_b);
952
953         /* See get_similarity() for details of similarities. */
954         similarity_count = length_b * (max_search_distance_a * 2 + 1);
955         similarities = xcalloc(sizeof(int), similarity_count);
956
957         for (i = 0; i < length_b; ++i) {
958                 result[i] = -1;
959                 second_best_result[i] = -1;
960                 certainties[i] = CERTAINTY_NOT_CALCULATED;
961         }
962
963         for (i = 0; i < similarity_count; ++i)
964                 similarities[i] = -1;
965
966         fuzzy_find_matching_lines_recurse(start_a, start_b,
967                                           length_a, length_b,
968                                           fingerprints_a + start_a,
969                                           fingerprints_b + start_b,
970                                           similarities,
971                                           certainties,
972                                           second_best_result,
973                                           result,
974                                           max_search_distance_a,
975                                           max_search_distance_b,
976                                           &map_line_number_in_b_to_a);
977
978         free(similarities);
979         free(certainties);
980         free(second_best_result);
981
982         return result;
983 }
984
985 static void fill_origin_fingerprints(struct blame_origin *o)
986 {
987         int *line_starts;
988
989         if (o->fingerprints)
990                 return;
991         o->num_lines = find_line_starts(&line_starts, o->file.ptr,
992                                         o->file.size);
993         o->fingerprints = xcalloc(sizeof(struct fingerprint), o->num_lines);
994         get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
995                               0, o->num_lines);
996         free(line_starts);
997 }
998
999 static void drop_origin_fingerprints(struct blame_origin *o)
1000 {
1001         if (o->fingerprints) {
1002                 free_line_fingerprints(o->fingerprints, o->num_lines);
1003                 o->num_lines = 0;
1004                 FREE_AND_NULL(o->fingerprints);
1005         }
1006 }
1007
1008 /*
1009  * Given an origin, prepare mmfile_t structure to be used by the
1010  * diff machinery
1011  */
1012 static void fill_origin_blob(struct diff_options *opt,
1013                              struct blame_origin *o, mmfile_t *file,
1014                              int *num_read_blob, int fill_fingerprints)
1015 {
1016         if (!o->file.ptr) {
1017                 enum object_type type;
1018                 unsigned long file_size;
1019
1020                 (*num_read_blob)++;
1021                 if (opt->flags.allow_textconv &&
1022                     textconv_object(opt->repo, o->path, o->mode,
1023                                     &o->blob_oid, 1, &file->ptr, &file_size))
1024                         ;
1025                 else
1026                         file->ptr = read_object_file(&o->blob_oid, &type,
1027                                                      &file_size);
1028                 file->size = file_size;
1029
1030                 if (!file->ptr)
1031                         die("Cannot read blob %s for path %s",
1032                             oid_to_hex(&o->blob_oid),
1033                             o->path);
1034                 o->file = *file;
1035         }
1036         else
1037                 *file = o->file;
1038         if (fill_fingerprints)
1039                 fill_origin_fingerprints(o);
1040 }
1041
1042 static void drop_origin_blob(struct blame_origin *o)
1043 {
1044         FREE_AND_NULL(o->file.ptr);
1045         drop_origin_fingerprints(o);
1046 }
1047
1048 /*
1049  * Any merge of blames happens on lists of blames that arrived via
1050  * different parents in a single suspect.  In this case, we want to
1051  * sort according to the suspect line numbers as opposed to the final
1052  * image line numbers.  The function body is somewhat longish because
1053  * it avoids unnecessary writes.
1054  */
1055
1056 static struct blame_entry *blame_merge(struct blame_entry *list1,
1057                                        struct blame_entry *list2)
1058 {
1059         struct blame_entry *p1 = list1, *p2 = list2,
1060                 **tail = &list1;
1061
1062         if (!p1)
1063                 return p2;
1064         if (!p2)
1065                 return p1;
1066
1067         if (p1->s_lno <= p2->s_lno) {
1068                 do {
1069                         tail = &p1->next;
1070                         if ((p1 = *tail) == NULL) {
1071                                 *tail = p2;
1072                                 return list1;
1073                         }
1074                 } while (p1->s_lno <= p2->s_lno);
1075         }
1076         for (;;) {
1077                 *tail = p2;
1078                 do {
1079                         tail = &p2->next;
1080                         if ((p2 = *tail) == NULL)  {
1081                                 *tail = p1;
1082                                 return list1;
1083                         }
1084                 } while (p1->s_lno > p2->s_lno);
1085                 *tail = p1;
1086                 do {
1087                         tail = &p1->next;
1088                         if ((p1 = *tail) == NULL) {
1089                                 *tail = p2;
1090                                 return list1;
1091                         }
1092                 } while (p1->s_lno <= p2->s_lno);
1093         }
1094 }
1095
1096 static void *get_next_blame(const void *p)
1097 {
1098         return ((struct blame_entry *)p)->next;
1099 }
1100
1101 static void set_next_blame(void *p1, void *p2)
1102 {
1103         ((struct blame_entry *)p1)->next = p2;
1104 }
1105
1106 /*
1107  * Final image line numbers are all different, so we don't need a
1108  * three-way comparison here.
1109  */
1110
1111 static int compare_blame_final(const void *p1, const void *p2)
1112 {
1113         return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
1114                 ? 1 : -1;
1115 }
1116
1117 static int compare_blame_suspect(const void *p1, const void *p2)
1118 {
1119         const struct blame_entry *s1 = p1, *s2 = p2;
1120         /*
1121          * to allow for collating suspects, we sort according to the
1122          * respective pointer value as the primary sorting criterion.
1123          * The actual relation is pretty unimportant as long as it
1124          * establishes a total order.  Comparing as integers gives us
1125          * that.
1126          */
1127         if (s1->suspect != s2->suspect)
1128                 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
1129         if (s1->s_lno == s2->s_lno)
1130                 return 0;
1131         return s1->s_lno > s2->s_lno ? 1 : -1;
1132 }
1133
1134 void blame_sort_final(struct blame_scoreboard *sb)
1135 {
1136         sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,
1137                                   compare_blame_final);
1138 }
1139
1140 static int compare_commits_by_reverse_commit_date(const void *a,
1141                                                   const void *b,
1142                                                   void *c)
1143 {
1144         return -compare_commits_by_commit_date(a, b, c);
1145 }
1146
1147 /*
1148  * For debugging -- origin is refcounted, and this asserts that
1149  * we do not underflow.
1150  */
1151 static void sanity_check_refcnt(struct blame_scoreboard *sb)
1152 {
1153         int baa = 0;
1154         struct blame_entry *ent;
1155
1156         for (ent = sb->ent; ent; ent = ent->next) {
1157                 /* Nobody should have zero or negative refcnt */
1158                 if (ent->suspect->refcnt <= 0) {
1159                         fprintf(stderr, "%s in %s has negative refcnt %d\n",
1160                                 ent->suspect->path,
1161                                 oid_to_hex(&ent->suspect->commit->object.oid),
1162                                 ent->suspect->refcnt);
1163                         baa = 1;
1164                 }
1165         }
1166         if (baa)
1167                 sb->on_sanity_fail(sb, baa);
1168 }
1169
1170 /*
1171  * If two blame entries that are next to each other came from
1172  * contiguous lines in the same origin (i.e. <commit, path> pair),
1173  * merge them together.
1174  */
1175 void blame_coalesce(struct blame_scoreboard *sb)
1176 {
1177         struct blame_entry *ent, *next;
1178
1179         for (ent = sb->ent; ent && (next = ent->next); ent = next) {
1180                 if (ent->suspect == next->suspect &&
1181                     ent->s_lno + ent->num_lines == next->s_lno &&
1182                     ent->ignored == next->ignored &&
1183                     ent->unblamable == next->unblamable) {
1184                         ent->num_lines += next->num_lines;
1185                         ent->next = next->next;
1186                         blame_origin_decref(next->suspect);
1187                         free(next);
1188                         ent->score = 0;
1189                         next = ent; /* again */
1190                 }
1191         }
1192
1193         if (sb->debug) /* sanity */
1194                 sanity_check_refcnt(sb);
1195 }
1196
1197 /*
1198  * Merge the given sorted list of blames into a preexisting origin.
1199  * If there were no previous blames to that commit, it is entered into
1200  * the commit priority queue of the score board.
1201  */
1202
1203 static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
1204                          struct blame_entry *sorted)
1205 {
1206         if (porigin->suspects)
1207                 porigin->suspects = blame_merge(porigin->suspects, sorted);
1208         else {
1209                 struct blame_origin *o;
1210                 for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
1211                         if (o->suspects) {
1212                                 porigin->suspects = sorted;
1213                                 return;
1214                         }
1215                 }
1216                 porigin->suspects = sorted;
1217                 prio_queue_put(&sb->commits, porigin->commit);
1218         }
1219 }
1220
1221 /*
1222  * Fill the blob_sha1 field of an origin if it hasn't, so that later
1223  * call to fill_origin_blob() can use it to locate the data.  blob_sha1
1224  * for an origin is also used to pass the blame for the entire file to
1225  * the parent to detect the case where a child's blob is identical to
1226  * that of its parent's.
1227  *
1228  * This also fills origin->mode for corresponding tree path.
1229  */
1230 static int fill_blob_sha1_and_mode(struct repository *r,
1231                                    struct blame_origin *origin)
1232 {
1233         if (!is_null_oid(&origin->blob_oid))
1234                 return 0;
1235         if (get_tree_entry(r, &origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
1236                 goto error_out;
1237         if (oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB)
1238                 goto error_out;
1239         return 0;
1240  error_out:
1241         oidclr(&origin->blob_oid);
1242         origin->mode = S_IFINVALID;
1243         return -1;
1244 }
1245
1246 /*
1247  * We have an origin -- check if the same path exists in the
1248  * parent and return an origin structure to represent it.
1249  */
1250 static struct blame_origin *find_origin(struct repository *r,
1251                                         struct commit *parent,
1252                                         struct blame_origin *origin)
1253 {
1254         struct blame_origin *porigin;
1255         struct diff_options diff_opts;
1256         const char *paths[2];
1257
1258         /* First check any existing origins */
1259         for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
1260                 if (!strcmp(porigin->path, origin->path)) {
1261                         /*
1262                          * The same path between origin and its parent
1263                          * without renaming -- the most common case.
1264                          */
1265                         return blame_origin_incref (porigin);
1266                 }
1267
1268         /* See if the origin->path is different between parent
1269          * and origin first.  Most of the time they are the
1270          * same and diff-tree is fairly efficient about this.
1271          */
1272         repo_diff_setup(r, &diff_opts);
1273         diff_opts.flags.recursive = 1;
1274         diff_opts.detect_rename = 0;
1275         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1276         paths[0] = origin->path;
1277         paths[1] = NULL;
1278
1279         parse_pathspec(&diff_opts.pathspec,
1280                        PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1281                        PATHSPEC_LITERAL_PATH, "", paths);
1282         diff_setup_done(&diff_opts);
1283
1284         if (is_null_oid(&origin->commit->object.oid))
1285                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1286         else
1287                 diff_tree_oid(get_commit_tree_oid(parent),
1288                               get_commit_tree_oid(origin->commit),
1289                               "", &diff_opts);
1290         diffcore_std(&diff_opts);
1291
1292         if (!diff_queued_diff.nr) {
1293                 /* The path is the same as parent */
1294                 porigin = get_origin(parent, origin->path);
1295                 oidcpy(&porigin->blob_oid, &origin->blob_oid);
1296                 porigin->mode = origin->mode;
1297         } else {
1298                 /*
1299                  * Since origin->path is a pathspec, if the parent
1300                  * commit had it as a directory, we will see a whole
1301                  * bunch of deletion of files in the directory that we
1302                  * do not care about.
1303                  */
1304                 int i;
1305                 struct diff_filepair *p = NULL;
1306                 for (i = 0; i < diff_queued_diff.nr; i++) {
1307                         const char *name;
1308                         p = diff_queued_diff.queue[i];
1309                         name = p->one->path ? p->one->path : p->two->path;
1310                         if (!strcmp(name, origin->path))
1311                                 break;
1312                 }
1313                 if (!p)
1314                         die("internal error in blame::find_origin");
1315                 switch (p->status) {
1316                 default:
1317                         die("internal error in blame::find_origin (%c)",
1318                             p->status);
1319                 case 'M':
1320                         porigin = get_origin(parent, origin->path);
1321                         oidcpy(&porigin->blob_oid, &p->one->oid);
1322                         porigin->mode = p->one->mode;
1323                         break;
1324                 case 'A':
1325                 case 'T':
1326                         /* Did not exist in parent, or type changed */
1327                         break;
1328                 }
1329         }
1330         diff_flush(&diff_opts);
1331         clear_pathspec(&diff_opts.pathspec);
1332         return porigin;
1333 }
1334
1335 /*
1336  * We have an origin -- find the path that corresponds to it in its
1337  * parent and return an origin structure to represent it.
1338  */
1339 static struct blame_origin *find_rename(struct repository *r,
1340                                         struct commit *parent,
1341                                         struct blame_origin *origin)
1342 {
1343         struct blame_origin *porigin = NULL;
1344         struct diff_options diff_opts;
1345         int i;
1346
1347         repo_diff_setup(r, &diff_opts);
1348         diff_opts.flags.recursive = 1;
1349         diff_opts.detect_rename = DIFF_DETECT_RENAME;
1350         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1351         diff_opts.single_follow = origin->path;
1352         diff_setup_done(&diff_opts);
1353
1354         if (is_null_oid(&origin->commit->object.oid))
1355                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1356         else
1357                 diff_tree_oid(get_commit_tree_oid(parent),
1358                               get_commit_tree_oid(origin->commit),
1359                               "", &diff_opts);
1360         diffcore_std(&diff_opts);
1361
1362         for (i = 0; i < diff_queued_diff.nr; i++) {
1363                 struct diff_filepair *p = diff_queued_diff.queue[i];
1364                 if ((p->status == 'R' || p->status == 'C') &&
1365                     !strcmp(p->two->path, origin->path)) {
1366                         porigin = get_origin(parent, p->one->path);
1367                         oidcpy(&porigin->blob_oid, &p->one->oid);
1368                         porigin->mode = p->one->mode;
1369                         break;
1370                 }
1371         }
1372         diff_flush(&diff_opts);
1373         clear_pathspec(&diff_opts.pathspec);
1374         return porigin;
1375 }
1376
1377 /*
1378  * Append a new blame entry to a given output queue.
1379  */
1380 static void add_blame_entry(struct blame_entry ***queue,
1381                             const struct blame_entry *src)
1382 {
1383         struct blame_entry *e = xmalloc(sizeof(*e));
1384         memcpy(e, src, sizeof(*e));
1385         blame_origin_incref(e->suspect);
1386
1387         e->next = **queue;
1388         **queue = e;
1389         *queue = &e->next;
1390 }
1391
1392 /*
1393  * src typically is on-stack; we want to copy the information in it to
1394  * a malloced blame_entry that gets added to the given queue.  The
1395  * origin of dst loses a refcnt.
1396  */
1397 static void dup_entry(struct blame_entry ***queue,
1398                       struct blame_entry *dst, struct blame_entry *src)
1399 {
1400         blame_origin_incref(src->suspect);
1401         blame_origin_decref(dst->suspect);
1402         memcpy(dst, src, sizeof(*src));
1403         dst->next = **queue;
1404         **queue = dst;
1405         *queue = &dst->next;
1406 }
1407
1408 const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
1409 {
1410         return sb->final_buf + sb->lineno[lno];
1411 }
1412
1413 /*
1414  * It is known that lines between tlno to same came from parent, and e
1415  * has an overlap with that range.  it also is known that parent's
1416  * line plno corresponds to e's line tlno.
1417  *
1418  *                <---- e ----->
1419  *                   <------>
1420  *                   <------------>
1421  *             <------------>
1422  *             <------------------>
1423  *
1424  * Split e into potentially three parts; before this chunk, the chunk
1425  * to be blamed for the parent, and after that portion.
1426  */
1427 static void split_overlap(struct blame_entry *split,
1428                           struct blame_entry *e,
1429                           int tlno, int plno, int same,
1430                           struct blame_origin *parent)
1431 {
1432         int chunk_end_lno;
1433         int i;
1434         memset(split, 0, sizeof(struct blame_entry [3]));
1435
1436         for (i = 0; i < 3; i++) {
1437                 split[i].ignored = e->ignored;
1438                 split[i].unblamable = e->unblamable;
1439         }
1440
1441         if (e->s_lno < tlno) {
1442                 /* there is a pre-chunk part not blamed on parent */
1443                 split[0].suspect = blame_origin_incref(e->suspect);
1444                 split[0].lno = e->lno;
1445                 split[0].s_lno = e->s_lno;
1446                 split[0].num_lines = tlno - e->s_lno;
1447                 split[1].lno = e->lno + tlno - e->s_lno;
1448                 split[1].s_lno = plno;
1449         }
1450         else {
1451                 split[1].lno = e->lno;
1452                 split[1].s_lno = plno + (e->s_lno - tlno);
1453         }
1454
1455         if (same < e->s_lno + e->num_lines) {
1456                 /* there is a post-chunk part not blamed on parent */
1457                 split[2].suspect = blame_origin_incref(e->suspect);
1458                 split[2].lno = e->lno + (same - e->s_lno);
1459                 split[2].s_lno = e->s_lno + (same - e->s_lno);
1460                 split[2].num_lines = e->s_lno + e->num_lines - same;
1461                 chunk_end_lno = split[2].lno;
1462         }
1463         else
1464                 chunk_end_lno = e->lno + e->num_lines;
1465         split[1].num_lines = chunk_end_lno - split[1].lno;
1466
1467         /*
1468          * if it turns out there is nothing to blame the parent for,
1469          * forget about the splitting.  !split[1].suspect signals this.
1470          */
1471         if (split[1].num_lines < 1)
1472                 return;
1473         split[1].suspect = blame_origin_incref(parent);
1474 }
1475
1476 /*
1477  * split_overlap() divided an existing blame e into up to three parts
1478  * in split.  Any assigned blame is moved to queue to
1479  * reflect the split.
1480  */
1481 static void split_blame(struct blame_entry ***blamed,
1482                         struct blame_entry ***unblamed,
1483                         struct blame_entry *split,
1484                         struct blame_entry *e)
1485 {
1486         if (split[0].suspect && split[2].suspect) {
1487                 /* The first part (reuse storage for the existing entry e) */
1488                 dup_entry(unblamed, e, &split[0]);
1489
1490                 /* The last part -- me */
1491                 add_blame_entry(unblamed, &split[2]);
1492
1493                 /* ... and the middle part -- parent */
1494                 add_blame_entry(blamed, &split[1]);
1495         }
1496         else if (!split[0].suspect && !split[2].suspect)
1497                 /*
1498                  * The parent covers the entire area; reuse storage for
1499                  * e and replace it with the parent.
1500                  */
1501                 dup_entry(blamed, e, &split[1]);
1502         else if (split[0].suspect) {
1503                 /* me and then parent */
1504                 dup_entry(unblamed, e, &split[0]);
1505                 add_blame_entry(blamed, &split[1]);
1506         }
1507         else {
1508                 /* parent and then me */
1509                 dup_entry(blamed, e, &split[1]);
1510                 add_blame_entry(unblamed, &split[2]);
1511         }
1512 }
1513
1514 /*
1515  * After splitting the blame, the origins used by the
1516  * on-stack blame_entry should lose one refcnt each.
1517  */
1518 static void decref_split(struct blame_entry *split)
1519 {
1520         int i;
1521
1522         for (i = 0; i < 3; i++)
1523                 blame_origin_decref(split[i].suspect);
1524 }
1525
1526 /*
1527  * reverse_blame reverses the list given in head, appending tail.
1528  * That allows us to build lists in reverse order, then reverse them
1529  * afterwards.  This can be faster than building the list in proper
1530  * order right away.  The reason is that building in proper order
1531  * requires writing a link in the _previous_ element, while building
1532  * in reverse order just requires placing the list head into the
1533  * _current_ element.
1534  */
1535
1536 static struct blame_entry *reverse_blame(struct blame_entry *head,
1537                                          struct blame_entry *tail)
1538 {
1539         while (head) {
1540                 struct blame_entry *next = head->next;
1541                 head->next = tail;
1542                 tail = head;
1543                 head = next;
1544         }
1545         return tail;
1546 }
1547
1548 /*
1549  * Splits a blame entry into two entries at 'len' lines.  The original 'e'
1550  * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
1551  * which is returned, consists of the remainder: [e->lno + len, e->lno +
1552  * e->num_lines).  The caller needs to sort out the reference counting for the
1553  * new entry's suspect.
1554  */
1555 static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
1556                                           struct blame_origin *new_suspect)
1557 {
1558         struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
1559
1560         n->suspect = new_suspect;
1561         n->ignored = e->ignored;
1562         n->unblamable = e->unblamable;
1563         n->lno = e->lno + len;
1564         n->s_lno = e->s_lno + len;
1565         n->num_lines = e->num_lines - len;
1566         e->num_lines = len;
1567         e->score = 0;
1568         return n;
1569 }
1570
1571 struct blame_line_tracker {
1572         int is_parent;
1573         int s_lno;
1574 };
1575
1576 static int are_lines_adjacent(struct blame_line_tracker *first,
1577                               struct blame_line_tracker *second)
1578 {
1579         return first->is_parent == second->is_parent &&
1580                first->s_lno + 1 == second->s_lno;
1581 }
1582
1583 static int scan_parent_range(struct fingerprint *p_fps,
1584                              struct fingerprint *t_fps, int t_idx,
1585                              int from, int nr_lines)
1586 {
1587         int sim, p_idx;
1588         #define FINGERPRINT_FILE_THRESHOLD      10
1589         int best_sim_val = FINGERPRINT_FILE_THRESHOLD;
1590         int best_sim_idx = -1;
1591
1592         for (p_idx = from; p_idx < from + nr_lines; p_idx++) {
1593                 sim = fingerprint_similarity(&t_fps[t_idx], &p_fps[p_idx]);
1594                 if (sim < best_sim_val)
1595                         continue;
1596                 /* Break ties with the closest-to-target line number */
1597                 if (sim == best_sim_val && best_sim_idx != -1 &&
1598                     abs(best_sim_idx - t_idx) < abs(p_idx - t_idx))
1599                         continue;
1600                 best_sim_val = sim;
1601                 best_sim_idx = p_idx;
1602         }
1603         return best_sim_idx;
1604 }
1605
1606 /*
1607  * The first pass checks the blame entry (from the target) against the parent's
1608  * diff chunk.  If that fails for a line, the second pass tries to match that
1609  * line to any part of parent file.  That catches cases where a change was
1610  * broken into two chunks by 'context.'
1611  */
1612 static void guess_line_blames(struct blame_origin *parent,
1613                               struct blame_origin *target,
1614                               int tlno, int offset, int same, int parent_len,
1615                               struct blame_line_tracker *line_blames)
1616 {
1617         int i, best_idx, target_idx;
1618         int parent_slno = tlno + offset;
1619         int *fuzzy_matches;
1620
1621         fuzzy_matches = fuzzy_find_matching_lines(parent, target,
1622                                                   tlno, parent_slno, same,
1623                                                   parent_len);
1624         for (i = 0; i < same - tlno; i++) {
1625                 target_idx = tlno + i;
1626                 if (fuzzy_matches && fuzzy_matches[i] >= 0) {
1627                         best_idx = fuzzy_matches[i];
1628                 } else {
1629                         best_idx = scan_parent_range(parent->fingerprints,
1630                                                      target->fingerprints,
1631                                                      target_idx, 0,
1632                                                      parent->num_lines);
1633                 }
1634                 if (best_idx >= 0) {
1635                         line_blames[i].is_parent = 1;
1636                         line_blames[i].s_lno = best_idx;
1637                 } else {
1638                         line_blames[i].is_parent = 0;
1639                         line_blames[i].s_lno = target_idx;
1640                 }
1641         }
1642         free(fuzzy_matches);
1643 }
1644
1645 /*
1646  * This decides which parts of a blame entry go to the parent (added to the
1647  * ignoredp list) and which stay with the target (added to the diffp list).  The
1648  * actual decision was made in a separate heuristic function, and those answers
1649  * for the lines in 'e' are in line_blames.  This consumes e, essentially
1650  * putting it on a list.
1651  *
1652  * Note that the blame entries on the ignoredp list are not necessarily sorted
1653  * with respect to the parent's line numbers yet.
1654  */
1655 static void ignore_blame_entry(struct blame_entry *e,
1656                                struct blame_origin *parent,
1657                                struct blame_entry **diffp,
1658                                struct blame_entry **ignoredp,
1659                                struct blame_line_tracker *line_blames)
1660 {
1661         int entry_len, nr_lines, i;
1662
1663         /*
1664          * We carve new entries off the front of e.  Each entry comes from a
1665          * contiguous chunk of lines: adjacent lines from the same origin
1666          * (either the parent or the target).
1667          */
1668         entry_len = 1;
1669         nr_lines = e->num_lines;        /* e changes in the loop */
1670         for (i = 0; i < nr_lines; i++) {
1671                 struct blame_entry *next = NULL;
1672
1673                 /*
1674                  * We are often adjacent to the next line - only split the blame
1675                  * entry when we have to.
1676                  */
1677                 if (i + 1 < nr_lines) {
1678                         if (are_lines_adjacent(&line_blames[i],
1679                                                &line_blames[i + 1])) {
1680                                 entry_len++;
1681                                 continue;
1682                         }
1683                         next = split_blame_at(e, entry_len,
1684                                               blame_origin_incref(e->suspect));
1685                 }
1686                 if (line_blames[i].is_parent) {
1687                         e->ignored = 1;
1688                         blame_origin_decref(e->suspect);
1689                         e->suspect = blame_origin_incref(parent);
1690                         e->s_lno = line_blames[i - entry_len + 1].s_lno;
1691                         e->next = *ignoredp;
1692                         *ignoredp = e;
1693                 } else {
1694                         e->unblamable = 1;
1695                         /* e->s_lno is already in the target's address space. */
1696                         e->next = *diffp;
1697                         *diffp = e;
1698                 }
1699                 assert(e->num_lines == entry_len);
1700                 e = next;
1701                 entry_len = 1;
1702         }
1703         assert(!e);
1704 }
1705
1706 /*
1707  * Process one hunk from the patch between the current suspect for
1708  * blame_entry e and its parent.  This first blames any unfinished
1709  * entries before the chunk (which is where target and parent start
1710  * differing) on the parent, and then splits blame entries at the
1711  * start and at the end of the difference region.  Since use of -M and
1712  * -C options may lead to overlapping/duplicate source line number
1713  * ranges, all we can rely on from sorting/merging is the order of the
1714  * first suspect line number.
1715  *
1716  * tlno: line number in the target where this chunk begins
1717  * same: line number in the target where this chunk ends
1718  * offset: add to tlno to get the chunk starting point in the parent
1719  * parent_len: number of lines in the parent chunk
1720  */
1721 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
1722                         int tlno, int offset, int same, int parent_len,
1723                         struct blame_origin *parent,
1724                         struct blame_origin *target, int ignore_diffs)
1725 {
1726         struct blame_entry *e = **srcq;
1727         struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL;
1728         struct blame_line_tracker *line_blames = NULL;
1729
1730         while (e && e->s_lno < tlno) {
1731                 struct blame_entry *next = e->next;
1732                 /*
1733                  * current record starts before differing portion.  If
1734                  * it reaches into it, we need to split it up and
1735                  * examine the second part separately.
1736                  */
1737                 if (e->s_lno + e->num_lines > tlno) {
1738                         /* Move second half to a new record */
1739                         struct blame_entry *n;
1740
1741                         n = split_blame_at(e, tlno - e->s_lno, e->suspect);
1742                         /* Push new record to diffp */
1743                         n->next = diffp;
1744                         diffp = n;
1745                 } else
1746                         blame_origin_decref(e->suspect);
1747                 /* Pass blame for everything before the differing
1748                  * chunk to the parent */
1749                 e->suspect = blame_origin_incref(parent);
1750                 e->s_lno += offset;
1751                 e->next = samep;
1752                 samep = e;
1753                 e = next;
1754         }
1755         /*
1756          * As we don't know how much of a common stretch after this
1757          * diff will occur, the currently blamed parts are all that we
1758          * can assign to the parent for now.
1759          */
1760
1761         if (samep) {
1762                 **dstq = reverse_blame(samep, **dstq);
1763                 *dstq = &samep->next;
1764         }
1765         /*
1766          * Prepend the split off portions: everything after e starts
1767          * after the blameable portion.
1768          */
1769         e = reverse_blame(diffp, e);
1770
1771         /*
1772          * Now retain records on the target while parts are different
1773          * from the parent.
1774          */
1775         samep = NULL;
1776         diffp = NULL;
1777
1778         if (ignore_diffs && same - tlno > 0) {
1779                 line_blames = xcalloc(sizeof(struct blame_line_tracker),
1780                                       same - tlno);
1781                 guess_line_blames(parent, target, tlno, offset, same,
1782                                   parent_len, line_blames);
1783         }
1784
1785         while (e && e->s_lno < same) {
1786                 struct blame_entry *next = e->next;
1787
1788                 /*
1789                  * If current record extends into sameness, need to split.
1790                  */
1791                 if (e->s_lno + e->num_lines > same) {
1792                         /*
1793                          * Move second half to a new record to be
1794                          * processed by later chunks
1795                          */
1796                         struct blame_entry *n;
1797
1798                         n = split_blame_at(e, same - e->s_lno,
1799                                            blame_origin_incref(e->suspect));
1800                         /* Push new record to samep */
1801                         n->next = samep;
1802                         samep = n;
1803                 }
1804                 if (ignore_diffs) {
1805                         ignore_blame_entry(e, parent, &diffp, &ignoredp,
1806                                            line_blames + e->s_lno - tlno);
1807                 } else {
1808                         e->next = diffp;
1809                         diffp = e;
1810                 }
1811                 e = next;
1812         }
1813         free(line_blames);
1814         if (ignoredp) {
1815                 /*
1816                  * Note ignoredp is not sorted yet, and thus neither is dstq.
1817                  * That list must be sorted before we queue_blames().  We defer
1818                  * sorting until after all diff hunks are processed, so that
1819                  * guess_line_blames() can pick *any* line in the parent.  The
1820                  * slight drawback is that we end up sorting all blame entries
1821                  * passed to the parent, including those that are unrelated to
1822                  * changes made by the ignored commit.
1823                  */
1824                 **dstq = reverse_blame(ignoredp, **dstq);
1825                 *dstq = &ignoredp->next;
1826         }
1827         **srcq = reverse_blame(diffp, reverse_blame(samep, e));
1828         /* Move across elements that are in the unblamable portion */
1829         if (diffp)
1830                 *srcq = &diffp->next;
1831 }
1832
1833 struct blame_chunk_cb_data {
1834         struct blame_origin *parent;
1835         struct blame_origin *target;
1836         long offset;
1837         int ignore_diffs;
1838         struct blame_entry **dstq;
1839         struct blame_entry **srcq;
1840 };
1841
1842 /* diff chunks are from parent to target */
1843 static int blame_chunk_cb(long start_a, long count_a,
1844                           long start_b, long count_b, void *data)
1845 {
1846         struct blame_chunk_cb_data *d = data;
1847         if (start_a - start_b != d->offset)
1848                 die("internal error in blame::blame_chunk_cb");
1849         blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
1850                     start_b + count_b, count_a, d->parent, d->target,
1851                     d->ignore_diffs);
1852         d->offset = start_a + count_a - (start_b + count_b);
1853         return 0;
1854 }
1855
1856 /*
1857  * We are looking at the origin 'target' and aiming to pass blame
1858  * for the lines it is suspected to its parent.  Run diff to find
1859  * which lines came from parent and pass blame for them.
1860  */
1861 static void pass_blame_to_parent(struct blame_scoreboard *sb,
1862                                  struct blame_origin *target,
1863                                  struct blame_origin *parent, int ignore_diffs)
1864 {
1865         mmfile_t file_p, file_o;
1866         struct blame_chunk_cb_data d;
1867         struct blame_entry *newdest = NULL;
1868
1869         if (!target->suspects)
1870                 return; /* nothing remains for this target */
1871
1872         d.parent = parent;
1873         d.target = target;
1874         d.offset = 0;
1875         d.ignore_diffs = ignore_diffs;
1876         d.dstq = &newdest; d.srcq = &target->suspects;
1877
1878         fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
1879                          &sb->num_read_blob, ignore_diffs);
1880         fill_origin_blob(&sb->revs->diffopt, target, &file_o,
1881                          &sb->num_read_blob, ignore_diffs);
1882         sb->num_get_patch++;
1883
1884         if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
1885                 die("unable to generate diff (%s -> %s)",
1886                     oid_to_hex(&parent->commit->object.oid),
1887                     oid_to_hex(&target->commit->object.oid));
1888         /* The rest are the same as the parent */
1889         blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
1890                     parent, target, 0);
1891         *d.dstq = NULL;
1892         if (ignore_diffs)
1893                 newdest = llist_mergesort(newdest, get_next_blame,
1894                                           set_next_blame,
1895                                           compare_blame_suspect);
1896         queue_blames(sb, parent, newdest);
1897
1898         return;
1899 }
1900
1901 /*
1902  * The lines in blame_entry after splitting blames many times can become
1903  * very small and trivial, and at some point it becomes pointless to
1904  * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
1905  * ordinary C program, and it is not worth to say it was copied from
1906  * totally unrelated file in the parent.
1907  *
1908  * Compute how trivial the lines in the blame_entry are.
1909  */
1910 unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
1911 {
1912         unsigned score;
1913         const char *cp, *ep;
1914
1915         if (e->score)
1916                 return e->score;
1917
1918         score = 1;
1919         cp = blame_nth_line(sb, e->lno);
1920         ep = blame_nth_line(sb, e->lno + e->num_lines);
1921         while (cp < ep) {
1922                 unsigned ch = *((unsigned char *)cp);
1923                 if (isalnum(ch))
1924                         score++;
1925                 cp++;
1926         }
1927         e->score = score;
1928         return score;
1929 }
1930
1931 /*
1932  * best_so_far[] and potential[] are both a split of an existing blame_entry
1933  * that passes blame to the parent.  Maintain best_so_far the best split so
1934  * far, by comparing potential and best_so_far and copying potential into
1935  * bst_so_far as needed.
1936  */
1937 static void copy_split_if_better(struct blame_scoreboard *sb,
1938                                  struct blame_entry *best_so_far,
1939                                  struct blame_entry *potential)
1940 {
1941         int i;
1942
1943         if (!potential[1].suspect)
1944                 return;
1945         if (best_so_far[1].suspect) {
1946                 if (blame_entry_score(sb, &potential[1]) <
1947                     blame_entry_score(sb, &best_so_far[1]))
1948                         return;
1949         }
1950
1951         for (i = 0; i < 3; i++)
1952                 blame_origin_incref(potential[i].suspect);
1953         decref_split(best_so_far);
1954         memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
1955 }
1956
1957 /*
1958  * We are looking at a part of the final image represented by
1959  * ent (tlno and same are offset by ent->s_lno).
1960  * tlno is where we are looking at in the final image.
1961  * up to (but not including) same match preimage.
1962  * plno is where we are looking at in the preimage.
1963  *
1964  * <-------------- final image ---------------------->
1965  *       <------ent------>
1966  *         ^tlno ^same
1967  *    <---------preimage----->
1968  *         ^plno
1969  *
1970  * All line numbers are 0-based.
1971  */
1972 static void handle_split(struct blame_scoreboard *sb,
1973                          struct blame_entry *ent,
1974                          int tlno, int plno, int same,
1975                          struct blame_origin *parent,
1976                          struct blame_entry *split)
1977 {
1978         if (ent->num_lines <= tlno)
1979                 return;
1980         if (tlno < same) {
1981                 struct blame_entry potential[3];
1982                 tlno += ent->s_lno;
1983                 same += ent->s_lno;
1984                 split_overlap(potential, ent, tlno, plno, same, parent);
1985                 copy_split_if_better(sb, split, potential);
1986                 decref_split(potential);
1987         }
1988 }
1989
1990 struct handle_split_cb_data {
1991         struct blame_scoreboard *sb;
1992         struct blame_entry *ent;
1993         struct blame_origin *parent;
1994         struct blame_entry *split;
1995         long plno;
1996         long tlno;
1997 };
1998
1999 static int handle_split_cb(long start_a, long count_a,
2000                            long start_b, long count_b, void *data)
2001 {
2002         struct handle_split_cb_data *d = data;
2003         handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
2004                      d->split);
2005         d->plno = start_a + count_a;
2006         d->tlno = start_b + count_b;
2007         return 0;
2008 }
2009
2010 /*
2011  * Find the lines from parent that are the same as ent so that
2012  * we can pass blames to it.  file_p has the blob contents for
2013  * the parent.
2014  */
2015 static void find_copy_in_blob(struct blame_scoreboard *sb,
2016                               struct blame_entry *ent,
2017                               struct blame_origin *parent,
2018                               struct blame_entry *split,
2019                               mmfile_t *file_p)
2020 {
2021         const char *cp;
2022         mmfile_t file_o;
2023         struct handle_split_cb_data d;
2024
2025         memset(&d, 0, sizeof(d));
2026         d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
2027         /*
2028          * Prepare mmfile that contains only the lines in ent.
2029          */
2030         cp = blame_nth_line(sb, ent->lno);
2031         file_o.ptr = (char *) cp;
2032         file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
2033
2034         /*
2035          * file_o is a part of final image we are annotating.
2036          * file_p partially may match that image.
2037          */
2038         memset(split, 0, sizeof(struct blame_entry [3]));
2039         if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
2040                 die("unable to generate diff (%s)",
2041                     oid_to_hex(&parent->commit->object.oid));
2042         /* remainder, if any, all match the preimage */
2043         handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
2044 }
2045
2046 /* Move all blame entries from list *source that have a score smaller
2047  * than score_min to the front of list *small.
2048  * Returns a pointer to the link pointing to the old head of the small list.
2049  */
2050
2051 static struct blame_entry **filter_small(struct blame_scoreboard *sb,
2052                                          struct blame_entry **small,
2053                                          struct blame_entry **source,
2054                                          unsigned score_min)
2055 {
2056         struct blame_entry *p = *source;
2057         struct blame_entry *oldsmall = *small;
2058         while (p) {
2059                 if (blame_entry_score(sb, p) <= score_min) {
2060                         *small = p;
2061                         small = &p->next;
2062                         p = *small;
2063                 } else {
2064                         *source = p;
2065                         source = &p->next;
2066                         p = *source;
2067                 }
2068         }
2069         *small = oldsmall;
2070         *source = NULL;
2071         return small;
2072 }
2073
2074 /*
2075  * See if lines currently target is suspected for can be attributed to
2076  * parent.
2077  */
2078 static void find_move_in_parent(struct blame_scoreboard *sb,
2079                                 struct blame_entry ***blamed,
2080                                 struct blame_entry **toosmall,
2081                                 struct blame_origin *target,
2082                                 struct blame_origin *parent)
2083 {
2084         struct blame_entry *e, split[3];
2085         struct blame_entry *unblamed = target->suspects;
2086         struct blame_entry *leftover = NULL;
2087         mmfile_t file_p;
2088
2089         if (!unblamed)
2090                 return; /* nothing remains for this target */
2091
2092         fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
2093                          &sb->num_read_blob, 0);
2094         if (!file_p.ptr)
2095                 return;
2096
2097         /* At each iteration, unblamed has a NULL-terminated list of
2098          * entries that have not yet been tested for blame.  leftover
2099          * contains the reversed list of entries that have been tested
2100          * without being assignable to the parent.
2101          */
2102         do {
2103                 struct blame_entry **unblamedtail = &unblamed;
2104                 struct blame_entry *next;
2105                 for (e = unblamed; e; e = next) {
2106                         next = e->next;
2107                         find_copy_in_blob(sb, e, parent, split, &file_p);
2108                         if (split[1].suspect &&
2109                             sb->move_score < blame_entry_score(sb, &split[1])) {
2110                                 split_blame(blamed, &unblamedtail, split, e);
2111                         } else {
2112                                 e->next = leftover;
2113                                 leftover = e;
2114                         }
2115                         decref_split(split);
2116                 }
2117                 *unblamedtail = NULL;
2118                 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
2119         } while (unblamed);
2120         target->suspects = reverse_blame(leftover, NULL);
2121 }
2122
2123 struct blame_list {
2124         struct blame_entry *ent;
2125         struct blame_entry split[3];
2126 };
2127
2128 /*
2129  * Count the number of entries the target is suspected for,
2130  * and prepare a list of entry and the best split.
2131  */
2132 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
2133                                            int *num_ents_p)
2134 {
2135         struct blame_entry *e;
2136         int num_ents, i;
2137         struct blame_list *blame_list = NULL;
2138
2139         for (e = unblamed, num_ents = 0; e; e = e->next)
2140                 num_ents++;
2141         if (num_ents) {
2142                 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
2143                 for (e = unblamed, i = 0; e; e = e->next)
2144                         blame_list[i++].ent = e;
2145         }
2146         *num_ents_p = num_ents;
2147         return blame_list;
2148 }
2149
2150 /*
2151  * For lines target is suspected for, see if we can find code movement
2152  * across file boundary from the parent commit.  porigin is the path
2153  * in the parent we already tried.
2154  */
2155 static void find_copy_in_parent(struct blame_scoreboard *sb,
2156                                 struct blame_entry ***blamed,
2157                                 struct blame_entry **toosmall,
2158                                 struct blame_origin *target,
2159                                 struct commit *parent,
2160                                 struct blame_origin *porigin,
2161                                 int opt)
2162 {
2163         struct diff_options diff_opts;
2164         int i, j;
2165         struct blame_list *blame_list;
2166         int num_ents;
2167         struct blame_entry *unblamed = target->suspects;
2168         struct blame_entry *leftover = NULL;
2169
2170         if (!unblamed)
2171                 return; /* nothing remains for this target */
2172
2173         repo_diff_setup(sb->repo, &diff_opts);
2174         diff_opts.flags.recursive = 1;
2175         diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
2176
2177         diff_setup_done(&diff_opts);
2178
2179         /* Try "find copies harder" on new path if requested;
2180          * we do not want to use diffcore_rename() actually to
2181          * match things up; find_copies_harder is set only to
2182          * force diff_tree_oid() to feed all filepairs to diff_queue,
2183          * and this code needs to be after diff_setup_done(), which
2184          * usually makes find-copies-harder imply copy detection.
2185          */
2186         if ((opt & PICKAXE_BLAME_COPY_HARDEST)
2187             || ((opt & PICKAXE_BLAME_COPY_HARDER)
2188                 && (!porigin || strcmp(target->path, porigin->path))))
2189                 diff_opts.flags.find_copies_harder = 1;
2190
2191         if (is_null_oid(&target->commit->object.oid))
2192                 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
2193         else
2194                 diff_tree_oid(get_commit_tree_oid(parent),
2195                               get_commit_tree_oid(target->commit),
2196                               "", &diff_opts);
2197
2198         if (!diff_opts.flags.find_copies_harder)
2199                 diffcore_std(&diff_opts);
2200
2201         do {
2202                 struct blame_entry **unblamedtail = &unblamed;
2203                 blame_list = setup_blame_list(unblamed, &num_ents);
2204
2205                 for (i = 0; i < diff_queued_diff.nr; i++) {
2206                         struct diff_filepair *p = diff_queued_diff.queue[i];
2207                         struct blame_origin *norigin;
2208                         mmfile_t file_p;
2209                         struct blame_entry potential[3];
2210
2211                         if (!DIFF_FILE_VALID(p->one))
2212                                 continue; /* does not exist in parent */
2213                         if (S_ISGITLINK(p->one->mode))
2214                                 continue; /* ignore git links */
2215                         if (porigin && !strcmp(p->one->path, porigin->path))
2216                                 /* find_move already dealt with this path */
2217                                 continue;
2218
2219                         norigin = get_origin(parent, p->one->path);
2220                         oidcpy(&norigin->blob_oid, &p->one->oid);
2221                         norigin->mode = p->one->mode;
2222                         fill_origin_blob(&sb->revs->diffopt, norigin, &file_p,
2223                                          &sb->num_read_blob, 0);
2224                         if (!file_p.ptr)
2225                                 continue;
2226
2227                         for (j = 0; j < num_ents; j++) {
2228                                 find_copy_in_blob(sb, blame_list[j].ent,
2229                                                   norigin, potential, &file_p);
2230                                 copy_split_if_better(sb, blame_list[j].split,
2231                                                      potential);
2232                                 decref_split(potential);
2233                         }
2234                         blame_origin_decref(norigin);
2235                 }
2236
2237                 for (j = 0; j < num_ents; j++) {
2238                         struct blame_entry *split = blame_list[j].split;
2239                         if (split[1].suspect &&
2240                             sb->copy_score < blame_entry_score(sb, &split[1])) {
2241                                 split_blame(blamed, &unblamedtail, split,
2242                                             blame_list[j].ent);
2243                         } else {
2244                                 blame_list[j].ent->next = leftover;
2245                                 leftover = blame_list[j].ent;
2246                         }
2247                         decref_split(split);
2248                 }
2249                 free(blame_list);
2250                 *unblamedtail = NULL;
2251                 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
2252         } while (unblamed);
2253         target->suspects = reverse_blame(leftover, NULL);
2254         diff_flush(&diff_opts);
2255         clear_pathspec(&diff_opts.pathspec);
2256 }
2257
2258 /*
2259  * The blobs of origin and porigin exactly match, so everything
2260  * origin is suspected for can be blamed on the parent.
2261  */
2262 static void pass_whole_blame(struct blame_scoreboard *sb,
2263                              struct blame_origin *origin, struct blame_origin *porigin)
2264 {
2265         struct blame_entry *e, *suspects;
2266
2267         if (!porigin->file.ptr && origin->file.ptr) {
2268                 /* Steal its file */
2269                 porigin->file = origin->file;
2270                 origin->file.ptr = NULL;
2271         }
2272         suspects = origin->suspects;
2273         origin->suspects = NULL;
2274         for (e = suspects; e; e = e->next) {
2275                 blame_origin_incref(porigin);
2276                 blame_origin_decref(e->suspect);
2277                 e->suspect = porigin;
2278         }
2279         queue_blames(sb, porigin, suspects);
2280 }
2281
2282 /*
2283  * We pass blame from the current commit to its parents.  We keep saying
2284  * "parent" (and "porigin"), but what we mean is to find scapegoat to
2285  * exonerate ourselves.
2286  */
2287 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
2288                                         int reverse)
2289 {
2290         if (!reverse) {
2291                 if (revs->first_parent_only &&
2292                     commit->parents &&
2293                     commit->parents->next) {
2294                         free_commit_list(commit->parents->next);
2295                         commit->parents->next = NULL;
2296                 }
2297                 return commit->parents;
2298         }
2299         return lookup_decoration(&revs->children, &commit->object);
2300 }
2301
2302 static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
2303 {
2304         struct commit_list *l = first_scapegoat(revs, commit, reverse);
2305         return commit_list_count(l);
2306 }
2307
2308 /* Distribute collected unsorted blames to the respected sorted lists
2309  * in the various origins.
2310  */
2311 static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
2312 {
2313         blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,
2314                                  compare_blame_suspect);
2315         while (blamed)
2316         {
2317                 struct blame_origin *porigin = blamed->suspect;
2318                 struct blame_entry *suspects = NULL;
2319                 do {
2320                         struct blame_entry *next = blamed->next;
2321                         blamed->next = suspects;
2322                         suspects = blamed;
2323                         blamed = next;
2324                 } while (blamed && blamed->suspect == porigin);
2325                 suspects = reverse_blame(suspects, NULL);
2326                 queue_blames(sb, porigin, suspects);
2327         }
2328 }
2329
2330 #define MAXSG 16
2331
2332 static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
2333 {
2334         struct rev_info *revs = sb->revs;
2335         int i, pass, num_sg;
2336         struct commit *commit = origin->commit;
2337         struct commit_list *sg;
2338         struct blame_origin *sg_buf[MAXSG];
2339         struct blame_origin *porigin, **sg_origin = sg_buf;
2340         struct blame_entry *toosmall = NULL;
2341         struct blame_entry *blames, **blametail = &blames;
2342
2343         num_sg = num_scapegoats(revs, commit, sb->reverse);
2344         if (!num_sg)
2345                 goto finish;
2346         else if (num_sg < ARRAY_SIZE(sg_buf))
2347                 memset(sg_buf, 0, sizeof(sg_buf));
2348         else
2349                 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
2350
2351         /*
2352          * The first pass looks for unrenamed path to optimize for
2353          * common cases, then we look for renames in the second pass.
2354          */
2355         for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
2356                 struct blame_origin *(*find)(struct repository *, struct commit *, struct blame_origin *);
2357                 find = pass ? find_rename : find_origin;
2358
2359                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2360                      i < num_sg && sg;
2361                      sg = sg->next, i++) {
2362                         struct commit *p = sg->item;
2363                         int j, same;
2364
2365                         if (sg_origin[i])
2366                                 continue;
2367                         if (parse_commit(p))
2368                                 continue;
2369                         porigin = find(sb->repo, p, origin);
2370                         if (!porigin)
2371                                 continue;
2372                         if (oideq(&porigin->blob_oid, &origin->blob_oid)) {
2373                                 pass_whole_blame(sb, origin, porigin);
2374                                 blame_origin_decref(porigin);
2375                                 goto finish;
2376                         }
2377                         for (j = same = 0; j < i; j++)
2378                                 if (sg_origin[j] &&
2379                                     oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
2380                                         same = 1;
2381                                         break;
2382                                 }
2383                         if (!same)
2384                                 sg_origin[i] = porigin;
2385                         else
2386                                 blame_origin_decref(porigin);
2387                 }
2388         }
2389
2390         sb->num_commits++;
2391         for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2392              i < num_sg && sg;
2393              sg = sg->next, i++) {
2394                 struct blame_origin *porigin = sg_origin[i];
2395                 if (!porigin)
2396                         continue;
2397                 if (!origin->previous) {
2398                         blame_origin_incref(porigin);
2399                         origin->previous = porigin;
2400                 }
2401                 pass_blame_to_parent(sb, origin, porigin, 0);
2402                 if (!origin->suspects)
2403                         goto finish;
2404         }
2405
2406         /*
2407          * Pass remaining suspects for ignored commits to their parents.
2408          */
2409         if (oidset_contains(&sb->ignore_list, &commit->object.oid)) {
2410                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2411                      i < num_sg && sg;
2412                      sg = sg->next, i++) {
2413                         struct blame_origin *porigin = sg_origin[i];
2414
2415                         if (!porigin)
2416                                 continue;
2417                         pass_blame_to_parent(sb, origin, porigin, 1);
2418                         /*
2419                          * Preemptively drop porigin so we can refresh the
2420                          * fingerprints if we use the parent again, which can
2421                          * occur if you ignore back-to-back commits.
2422                          */
2423                         drop_origin_blob(porigin);
2424                         if (!origin->suspects)
2425                                 goto finish;
2426                 }
2427         }
2428
2429         /*
2430          * Optionally find moves in parents' files.
2431          */
2432         if (opt & PICKAXE_BLAME_MOVE) {
2433                 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
2434                 if (origin->suspects) {
2435                         for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2436                              i < num_sg && sg;
2437                              sg = sg->next, i++) {
2438                                 struct blame_origin *porigin = sg_origin[i];
2439                                 if (!porigin)
2440                                         continue;
2441                                 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
2442                                 if (!origin->suspects)
2443                                         break;
2444                         }
2445                 }
2446         }
2447
2448         /*
2449          * Optionally find copies from parents' files.
2450          */
2451         if (opt & PICKAXE_BLAME_COPY) {
2452                 if (sb->copy_score > sb->move_score)
2453                         filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
2454                 else if (sb->copy_score < sb->move_score) {
2455                         origin->suspects = blame_merge(origin->suspects, toosmall);
2456                         toosmall = NULL;
2457                         filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
2458                 }
2459                 if (!origin->suspects)
2460                         goto finish;
2461
2462                 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2463                      i < num_sg && sg;
2464                      sg = sg->next, i++) {
2465                         struct blame_origin *porigin = sg_origin[i];
2466                         find_copy_in_parent(sb, &blametail, &toosmall,
2467                                             origin, sg->item, porigin, opt);
2468                         if (!origin->suspects)
2469                                 goto finish;
2470                 }
2471         }
2472
2473 finish:
2474         *blametail = NULL;
2475         distribute_blame(sb, blames);
2476         /*
2477          * prepend toosmall to origin->suspects
2478          *
2479          * There is no point in sorting: this ends up on a big
2480          * unsorted list in the caller anyway.
2481          */
2482         if (toosmall) {
2483                 struct blame_entry **tail = &toosmall;
2484                 while (*tail)
2485                         tail = &(*tail)->next;
2486                 *tail = origin->suspects;
2487                 origin->suspects = toosmall;
2488         }
2489         for (i = 0; i < num_sg; i++) {
2490                 if (sg_origin[i]) {
2491                         if (!sg_origin[i]->suspects)
2492                                 drop_origin_blob(sg_origin[i]);
2493                         blame_origin_decref(sg_origin[i]);
2494                 }
2495         }
2496         drop_origin_blob(origin);
2497         if (sg_buf != sg_origin)
2498                 free(sg_origin);
2499 }
2500
2501 /*
2502  * The main loop -- while we have blobs with lines whose true origin
2503  * is still unknown, pick one blob, and allow its lines to pass blames
2504  * to its parents. */
2505 void assign_blame(struct blame_scoreboard *sb, int opt)
2506 {
2507         struct rev_info *revs = sb->revs;
2508         struct commit *commit = prio_queue_get(&sb->commits);
2509
2510         while (commit) {
2511                 struct blame_entry *ent;
2512                 struct blame_origin *suspect = get_blame_suspects(commit);
2513
2514                 /* find one suspect to break down */
2515                 while (suspect && !suspect->suspects)
2516                         suspect = suspect->next;
2517
2518                 if (!suspect) {
2519                         commit = prio_queue_get(&sb->commits);
2520                         continue;
2521                 }
2522
2523                 assert(commit == suspect->commit);
2524
2525                 /*
2526                  * We will use this suspect later in the loop,
2527                  * so hold onto it in the meantime.
2528                  */
2529                 blame_origin_incref(suspect);
2530                 parse_commit(commit);
2531                 if (sb->reverse ||
2532                     (!(commit->object.flags & UNINTERESTING) &&
2533                      !(revs->max_age != -1 && commit->date < revs->max_age)))
2534                         pass_blame(sb, suspect, opt);
2535                 else {
2536                         commit->object.flags |= UNINTERESTING;
2537                         if (commit->object.parsed)
2538                                 mark_parents_uninteresting(commit);
2539                 }
2540                 /* treat root commit as boundary */
2541                 if (!commit->parents && !sb->show_root)
2542                         commit->object.flags |= UNINTERESTING;
2543
2544                 /* Take responsibility for the remaining entries */
2545                 ent = suspect->suspects;
2546                 if (ent) {
2547                         suspect->guilty = 1;
2548                         for (;;) {
2549                                 struct blame_entry *next = ent->next;
2550                                 if (sb->found_guilty_entry)
2551                                         sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
2552                                 if (next) {
2553                                         ent = next;
2554                                         continue;
2555                                 }
2556                                 ent->next = sb->ent;
2557                                 sb->ent = suspect->suspects;
2558                                 suspect->suspects = NULL;
2559                                 break;
2560                         }
2561                 }
2562                 blame_origin_decref(suspect);
2563
2564                 if (sb->debug) /* sanity */
2565                         sanity_check_refcnt(sb);
2566         }
2567 }
2568
2569 /*
2570  * To allow quick access to the contents of nth line in the
2571  * final image, prepare an index in the scoreboard.
2572  */
2573 static int prepare_lines(struct blame_scoreboard *sb)
2574 {
2575         sb->num_lines = find_line_starts(&sb->lineno, sb->final_buf,
2576                                          sb->final_buf_size);
2577         return sb->num_lines;
2578 }
2579
2580 static struct commit *find_single_final(struct rev_info *revs,
2581                                         const char **name_p)
2582 {
2583         int i;
2584         struct commit *found = NULL;
2585         const char *name = NULL;
2586
2587         for (i = 0; i < revs->pending.nr; i++) {
2588                 struct object *obj = revs->pending.objects[i].item;
2589                 if (obj->flags & UNINTERESTING)
2590                         continue;
2591                 obj = deref_tag(revs->repo, obj, NULL, 0);
2592                 if (obj->type != OBJ_COMMIT)
2593                         die("Non commit %s?", revs->pending.objects[i].name);
2594                 if (found)
2595                         die("More than one commit to dig from %s and %s?",
2596                             revs->pending.objects[i].name, name);
2597                 found = (struct commit *)obj;
2598                 name = revs->pending.objects[i].name;
2599         }
2600         if (name_p)
2601                 *name_p = xstrdup_or_null(name);
2602         return found;
2603 }
2604
2605 static struct commit *dwim_reverse_initial(struct rev_info *revs,
2606                                            const char **name_p)
2607 {
2608         /*
2609          * DWIM "git blame --reverse ONE -- PATH" as
2610          * "git blame --reverse ONE..HEAD -- PATH" but only do so
2611          * when it makes sense.
2612          */
2613         struct object *obj;
2614         struct commit *head_commit;
2615         struct object_id head_oid;
2616
2617         if (revs->pending.nr != 1)
2618                 return NULL;
2619
2620         /* Is that sole rev a committish? */
2621         obj = revs->pending.objects[0].item;
2622         obj = deref_tag(revs->repo, obj, NULL, 0);
2623         if (obj->type != OBJ_COMMIT)
2624                 return NULL;
2625
2626         /* Do we have HEAD? */
2627         if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
2628                 return NULL;
2629         head_commit = lookup_commit_reference_gently(revs->repo,
2630                                                      &head_oid, 1);
2631         if (!head_commit)
2632                 return NULL;
2633
2634         /* Turn "ONE" into "ONE..HEAD" then */
2635         obj->flags |= UNINTERESTING;
2636         add_pending_object(revs, &head_commit->object, "HEAD");
2637
2638         if (name_p)
2639                 *name_p = revs->pending.objects[0].name;
2640         return (struct commit *)obj;
2641 }
2642
2643 static struct commit *find_single_initial(struct rev_info *revs,
2644                                           const char **name_p)
2645 {
2646         int i;
2647         struct commit *found = NULL;
2648         const char *name = NULL;
2649
2650         /*
2651          * There must be one and only one negative commit, and it must be
2652          * the boundary.
2653          */
2654         for (i = 0; i < revs->pending.nr; i++) {
2655                 struct object *obj = revs->pending.objects[i].item;
2656                 if (!(obj->flags & UNINTERESTING))
2657                         continue;
2658                 obj = deref_tag(revs->repo, obj, NULL, 0);
2659                 if (obj->type != OBJ_COMMIT)
2660                         die("Non commit %s?", revs->pending.objects[i].name);
2661                 if (found)
2662                         die("More than one commit to dig up from, %s and %s?",
2663                             revs->pending.objects[i].name, name);
2664                 found = (struct commit *) obj;
2665                 name = revs->pending.objects[i].name;
2666         }
2667
2668         if (!name)
2669                 found = dwim_reverse_initial(revs, &name);
2670         if (!name)
2671                 die("No commit to dig up from?");
2672
2673         if (name_p)
2674                 *name_p = xstrdup(name);
2675         return found;
2676 }
2677
2678 void init_scoreboard(struct blame_scoreboard *sb)
2679 {
2680         memset(sb, 0, sizeof(struct blame_scoreboard));
2681         sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
2682         sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
2683 }
2684
2685 void setup_scoreboard(struct blame_scoreboard *sb,
2686                       const char *path,
2687                       struct blame_origin **orig)
2688 {
2689         const char *final_commit_name = NULL;
2690         struct blame_origin *o;
2691         struct commit *final_commit = NULL;
2692         enum object_type type;
2693
2694         init_blame_suspects(&blame_suspects);
2695
2696         if (sb->reverse && sb->contents_from)
2697                 die(_("--contents and --reverse do not blend well."));
2698
2699         if (!sb->repo)
2700                 BUG("repo is NULL");
2701
2702         if (!sb->reverse) {
2703                 sb->final = find_single_final(sb->revs, &final_commit_name);
2704                 sb->commits.compare = compare_commits_by_commit_date;
2705         } else {
2706                 sb->final = find_single_initial(sb->revs, &final_commit_name);
2707                 sb->commits.compare = compare_commits_by_reverse_commit_date;
2708         }
2709
2710         if (sb->final && sb->contents_from)
2711                 die(_("cannot use --contents with final commit object name"));
2712
2713         if (sb->reverse && sb->revs->first_parent_only)
2714                 sb->revs->children.name = NULL;
2715
2716         if (!sb->final) {
2717                 /*
2718                  * "--not A B -- path" without anything positive;
2719                  * do not default to HEAD, but use the working tree
2720                  * or "--contents".
2721                  */
2722                 setup_work_tree();
2723                 sb->final = fake_working_tree_commit(sb->repo,
2724                                                      &sb->revs->diffopt,
2725                                                      path, sb->contents_from);
2726                 add_pending_object(sb->revs, &(sb->final->object), ":");
2727         }
2728
2729         if (sb->reverse && sb->revs->first_parent_only) {
2730                 final_commit = find_single_final(sb->revs, NULL);
2731                 if (!final_commit)
2732                         die(_("--reverse and --first-parent together require specified latest commit"));
2733         }
2734
2735         /*
2736          * If we have bottom, this will mark the ancestors of the
2737          * bottom commits we would reach while traversing as
2738          * uninteresting.
2739          */
2740         if (prepare_revision_walk(sb->revs))
2741                 die(_("revision walk setup failed"));
2742
2743         if (sb->reverse && sb->revs->first_parent_only) {
2744                 struct commit *c = final_commit;
2745
2746                 sb->revs->children.name = "children";
2747                 while (c->parents &&
2748                        !oideq(&c->object.oid, &sb->final->object.oid)) {
2749                         struct commit_list *l = xcalloc(1, sizeof(*l));
2750
2751                         l->item = c;
2752                         if (add_decoration(&sb->revs->children,
2753                                            &c->parents->item->object, l))
2754                                 BUG("not unique item in first-parent chain");
2755                         c = c->parents->item;
2756                 }
2757
2758                 if (!oideq(&c->object.oid, &sb->final->object.oid))
2759                         die(_("--reverse --first-parent together require range along first-parent chain"));
2760         }
2761
2762         if (is_null_oid(&sb->final->object.oid)) {
2763                 o = get_blame_suspects(sb->final);
2764                 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
2765                 sb->final_buf_size = o->file.size;
2766         }
2767         else {
2768                 o = get_origin(sb->final, path);
2769                 if (fill_blob_sha1_and_mode(sb->repo, o))
2770                         die(_("no such path %s in %s"), path, final_commit_name);
2771
2772                 if (sb->revs->diffopt.flags.allow_textconv &&
2773                     textconv_object(sb->repo, path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
2774                                     &sb->final_buf_size))
2775                         ;
2776                 else
2777                         sb->final_buf = read_object_file(&o->blob_oid, &type,
2778                                                          &sb->final_buf_size);
2779
2780                 if (!sb->final_buf)
2781                         die(_("cannot read blob %s for path %s"),
2782                             oid_to_hex(&o->blob_oid),
2783                             path);
2784         }
2785         sb->num_read_blob++;
2786         prepare_lines(sb);
2787
2788         if (orig)
2789                 *orig = o;
2790
2791         free((char *)final_commit_name);
2792 }
2793
2794
2795
2796 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
2797                                         long start, long end,
2798                                         struct blame_origin *o)
2799 {
2800         struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
2801         new_head->lno = start;
2802         new_head->num_lines = end - start;
2803         new_head->suspect = o;
2804         new_head->s_lno = start;
2805         new_head->next = head;
2806         blame_origin_incref(o);
2807         return new_head;
2808 }