Merge branch 'am/doc-sha256'
[git.git] / blame.c
blob141756975bf5a58a1744eda78c1750d9d949272f
1 #include "git-compat-util.h"
2 #include "refs.h"
3 #include "object-store-ll.h"
4 #include "cache-tree.h"
5 #include "mergesort.h"
6 #include "convert.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "path.h"
12 #include "read-cache.h"
13 #include "setup.h"
14 #include "tag.h"
15 #include "trace2.h"
16 #include "blame.h"
17 #include "alloc.h"
18 #include "commit-slab.h"
19 #include "bloom.h"
20 #include "commit-graph.h"
22 define_commit_slab(blame_suspects, struct blame_origin *);
23 static struct blame_suspects blame_suspects;
25 struct blame_origin *get_blame_suspects(struct commit *commit)
27 struct blame_origin **result;
29 result = blame_suspects_peek(&blame_suspects, commit);
31 return result ? *result : NULL;
34 static void set_blame_suspects(struct commit *commit, struct blame_origin *origin)
36 *blame_suspects_at(&blame_suspects, commit) = origin;
39 void blame_origin_decref(struct blame_origin *o)
41 if (o && --o->refcnt <= 0) {
42 struct blame_origin *p, *l = NULL;
43 if (o->previous)
44 blame_origin_decref(o->previous);
45 free(o->file.ptr);
46 /* Should be present exactly once in commit chain */
47 for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) {
48 if (p == o) {
49 if (l)
50 l->next = p->next;
51 else
52 set_blame_suspects(o->commit, p->next);
53 free(o);
54 return;
57 die("internal error in blame_origin_decref");
62 * Given a commit and a path in it, create a new origin structure.
63 * The callers that add blame to the scoreboard should use
64 * get_origin() to obtain shared, refcounted copy instead of calling
65 * this function directly.
67 static struct blame_origin *make_origin(struct commit *commit, const char *path)
69 struct blame_origin *o;
70 FLEX_ALLOC_STR(o, path, path);
71 o->commit = commit;
72 o->refcnt = 1;
73 o->next = get_blame_suspects(commit);
74 set_blame_suspects(commit, o);
75 return o;
79 * Locate an existing origin or create a new one.
80 * This moves the origin to front position in the commit util list.
82 static struct blame_origin *get_origin(struct commit *commit, const char *path)
84 struct blame_origin *o, *l;
86 for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) {
87 if (!strcmp(o->path, path)) {
88 /* bump to front */
89 if (l) {
90 l->next = o->next;
91 o->next = get_blame_suspects(commit);
92 set_blame_suspects(commit, o);
94 return blame_origin_incref(o);
97 return make_origin(commit, path);
102 static void verify_working_tree_path(struct repository *r,
103 struct commit *work_tree, const char *path)
105 struct commit_list *parents;
106 int pos;
108 for (parents = work_tree->parents; parents; parents = parents->next) {
109 const struct object_id *commit_oid = &parents->item->object.oid;
110 struct object_id blob_oid;
111 unsigned short mode;
113 if (!get_tree_entry(r, commit_oid, path, &blob_oid, &mode) &&
114 oid_object_info(r, &blob_oid, NULL) == OBJ_BLOB)
115 return;
118 pos = index_name_pos(r->index, path, strlen(path));
119 if (pos >= 0)
120 ; /* path is in the index */
121 else if (-1 - pos < r->index->cache_nr &&
122 !strcmp(r->index->cache[-1 - pos]->name, path))
123 ; /* path is in the index, unmerged */
124 else
125 die("no such path '%s' in HEAD", path);
128 static struct commit_list **append_parent(struct repository *r,
129 struct commit_list **tail,
130 const struct object_id *oid)
132 struct commit *parent;
134 parent = lookup_commit_reference(r, oid);
135 if (!parent)
136 die("no such commit %s", oid_to_hex(oid));
137 return &commit_list_insert(parent, tail)->next;
140 static void append_merge_parents(struct repository *r,
141 struct commit_list **tail)
143 int merge_head;
144 struct strbuf line = STRBUF_INIT;
146 merge_head = open(git_path_merge_head(r), O_RDONLY);
147 if (merge_head < 0) {
148 if (errno == ENOENT)
149 return;
150 die("cannot open '%s' for reading",
151 git_path_merge_head(r));
154 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
155 struct object_id oid;
156 if (get_oid_hex(line.buf, &oid))
157 die("unknown line in '%s': %s",
158 git_path_merge_head(r), line.buf);
159 tail = append_parent(r, tail, &oid);
161 close(merge_head);
162 strbuf_release(&line);
166 * This isn't as simple as passing sb->buf and sb->len, because we
167 * want to transfer ownership of the buffer to the commit (so we
168 * must use detach).
170 static void set_commit_buffer_from_strbuf(struct repository *r,
171 struct commit *c,
172 struct strbuf *sb)
174 size_t len;
175 void *buf = strbuf_detach(sb, &len);
176 set_commit_buffer(r, c, buf, len);
180 * Prepare a dummy commit that represents the work tree (or staged) item.
181 * Note that annotating work tree item never works in the reverse.
183 static struct commit *fake_working_tree_commit(struct repository *r,
184 struct diff_options *opt,
185 const char *path,
186 const char *contents_from,
187 struct object_id *oid)
189 struct commit *commit;
190 struct blame_origin *origin;
191 struct commit_list **parent_tail, *parent;
192 struct strbuf buf = STRBUF_INIT;
193 const char *ident;
194 time_t now;
195 int len;
196 struct cache_entry *ce;
197 unsigned mode;
198 struct strbuf msg = STRBUF_INIT;
200 repo_read_index(r);
201 time(&now);
202 commit = alloc_commit_node(r);
203 commit->object.parsed = 1;
204 commit->date = now;
205 parent_tail = &commit->parents;
207 parent_tail = append_parent(r, parent_tail, oid);
208 append_merge_parents(r, parent_tail);
209 verify_working_tree_path(r, commit, path);
211 origin = make_origin(commit, path);
213 if (contents_from)
214 ident = fmt_ident("External file (--contents)", "external.file",
215 WANT_BLANK_IDENT, NULL, 0);
216 else
217 ident = fmt_ident("Not Committed Yet", "not.committed.yet",
218 WANT_BLANK_IDENT, NULL, 0);
219 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
220 for (parent = commit->parents; parent; parent = parent->next)
221 strbuf_addf(&msg, "parent %s\n",
222 oid_to_hex(&parent->item->object.oid));
223 strbuf_addf(&msg,
224 "author %s\n"
225 "committer %s\n\n"
226 "Version of %s from %s\n",
227 ident, ident, path,
228 (!contents_from ? path :
229 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
230 set_commit_buffer_from_strbuf(r, commit, &msg);
232 if (!contents_from || strcmp("-", contents_from)) {
233 struct stat st;
234 const char *read_from;
235 char *buf_ptr;
236 unsigned long buf_len;
238 if (contents_from) {
239 if (stat(contents_from, &st) < 0)
240 die_errno("Cannot stat '%s'", contents_from);
241 read_from = contents_from;
243 else {
244 if (lstat(path, &st) < 0)
245 die_errno("Cannot lstat '%s'", path);
246 read_from = path;
248 mode = canon_mode(st.st_mode);
250 switch (st.st_mode & S_IFMT) {
251 case S_IFREG:
252 if (opt->flags.allow_textconv &&
253 textconv_object(r, read_from, mode, null_oid(), 0, &buf_ptr, &buf_len))
254 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
255 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
256 die_errno("cannot open or read '%s'", read_from);
257 break;
258 case S_IFLNK:
259 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
260 die_errno("cannot readlink '%s'", read_from);
261 break;
262 default:
263 die("unsupported file type %s", read_from);
266 else {
267 /* Reading from stdin */
268 mode = 0;
269 if (strbuf_read(&buf, 0, 0) < 0)
270 die_errno("failed to read from stdin");
272 convert_to_git(r->index, path, buf.buf, buf.len, &buf, 0);
273 origin->file.ptr = buf.buf;
274 origin->file.size = buf.len;
275 pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
278 * Read the current index, replace the path entry with
279 * origin->blob_sha1 without mucking with its mode or type
280 * bits; we are not going to write this index out -- we just
281 * want to run "diff-index --cached".
283 discard_index(r->index);
284 repo_read_index(r);
286 len = strlen(path);
287 if (!mode) {
288 int pos = index_name_pos(r->index, path, len);
289 if (0 <= pos)
290 mode = r->index->cache[pos]->ce_mode;
291 else
292 /* Let's not bother reading from HEAD tree */
293 mode = S_IFREG | 0644;
295 ce = make_empty_cache_entry(r->index, len);
296 oidcpy(&ce->oid, &origin->blob_oid);
297 memcpy(ce->name, path, len);
298 ce->ce_flags = create_ce_flags(0);
299 ce->ce_namelen = len;
300 ce->ce_mode = create_ce_mode(mode);
301 add_index_entry(r->index, ce,
302 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
304 cache_tree_invalidate_path(r->index, path);
306 return commit;
311 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
312 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
314 xpparam_t xpp = {0};
315 xdemitconf_t xecfg = {0};
316 xdemitcb_t ecb = {NULL};
318 xpp.flags = xdl_opts;
319 xecfg.hunk_func = hunk_func;
320 ecb.priv = cb_data;
321 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
324 static const char *get_next_line(const char *start, const char *end)
326 const char *nl = memchr(start, '\n', end - start);
328 return nl ? nl + 1 : end;
331 static int find_line_starts(int **line_starts, const char *buf,
332 unsigned long len)
334 const char *end = buf + len;
335 const char *p;
336 int *lineno;
337 int num = 0;
339 for (p = buf; p < end; p = get_next_line(p, end))
340 num++;
342 ALLOC_ARRAY(*line_starts, num + 1);
343 lineno = *line_starts;
345 for (p = buf; p < end; p = get_next_line(p, end))
346 *lineno++ = p - buf;
348 *lineno = len;
350 return num;
353 struct fingerprint_entry;
355 /* A fingerprint is intended to loosely represent a string, such that two
356 * fingerprints can be quickly compared to give an indication of the similarity
357 * of the strings that they represent.
359 * A fingerprint is represented as a multiset of the lower-cased byte pairs in
360 * the string that it represents. Whitespace is added at each end of the
361 * string. Whitespace pairs are ignored. Whitespace is converted to '\0'.
362 * For example, the string "Darth Radar" will be converted to the following
363 * fingerprint:
364 * {"\0d", "da", "da", "ar", "ar", "rt", "th", "h\0", "\0r", "ra", "ad", "r\0"}
366 * The similarity between two fingerprints is the size of the intersection of
367 * their multisets, including repeated elements. See fingerprint_similarity for
368 * examples.
370 * For ease of implementation, the fingerprint is implemented as a map
371 * of byte pairs to the count of that byte pair in the string, instead of
372 * allowing repeated elements in a set.
374 struct fingerprint {
375 struct hashmap map;
376 /* As we know the maximum number of entries in advance, it's
377 * convenient to store the entries in a single array instead of having
378 * the hashmap manage the memory.
380 struct fingerprint_entry *entries;
383 /* A byte pair in a fingerprint. Stores the number of times the byte pair
384 * occurs in the string that the fingerprint represents.
386 struct fingerprint_entry {
387 /* The hashmap entry - the hash represents the byte pair in its
388 * entirety so we don't need to store the byte pair separately.
390 struct hashmap_entry entry;
391 /* The number of times the byte pair occurs in the string that the
392 * fingerprint represents.
394 int count;
397 /* See `struct fingerprint` for an explanation of what a fingerprint is.
398 * \param result the fingerprint of the string is stored here. This must be
399 * freed later using free_fingerprint.
400 * \param line_begin the start of the string
401 * \param line_end the end of the string
403 static void get_fingerprint(struct fingerprint *result,
404 const char *line_begin,
405 const char *line_end)
407 unsigned int hash, c0 = 0, c1;
408 const char *p;
409 int max_map_entry_count = 1 + line_end - line_begin;
410 struct fingerprint_entry *entry = xcalloc(max_map_entry_count,
411 sizeof(struct fingerprint_entry));
412 struct fingerprint_entry *found_entry;
414 hashmap_init(&result->map, NULL, NULL, max_map_entry_count);
415 result->entries = entry;
416 for (p = line_begin; p <= line_end; ++p, c0 = c1) {
417 /* Always terminate the string with whitespace.
418 * Normalise whitespace to 0, and normalise letters to
419 * lower case. This won't work for multibyte characters but at
420 * worst will match some unrelated characters.
422 if ((p == line_end) || isspace(*p))
423 c1 = 0;
424 else
425 c1 = tolower(*p);
426 hash = c0 | (c1 << 8);
427 /* Ignore whitespace pairs */
428 if (hash == 0)
429 continue;
430 hashmap_entry_init(&entry->entry, hash);
432 found_entry = hashmap_get_entry(&result->map, entry,
433 /* member name */ entry, NULL);
434 if (found_entry) {
435 found_entry->count += 1;
436 } else {
437 entry->count = 1;
438 hashmap_add(&result->map, &entry->entry);
439 ++entry;
444 static void free_fingerprint(struct fingerprint *f)
446 hashmap_clear(&f->map);
447 free(f->entries);
450 /* Calculates the similarity between two fingerprints as the size of the
451 * intersection of their multisets, including repeated elements. See
452 * `struct fingerprint` for an explanation of the fingerprint representation.
453 * The similarity between "cat mat" and "father rather" is 2 because "at" is
454 * present twice in both strings while the similarity between "tim" and "mit"
455 * is 0.
457 static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
459 int intersection = 0;
460 struct hashmap_iter iter;
461 const struct fingerprint_entry *entry_a, *entry_b;
463 hashmap_for_each_entry(&b->map, &iter, entry_b,
464 entry /* member name */) {
465 entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL);
466 if (entry_a) {
467 intersection += entry_a->count < entry_b->count ?
468 entry_a->count : entry_b->count;
471 return intersection;
474 /* Subtracts byte-pair elements in B from A, modifying A in place.
476 static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
478 struct hashmap_iter iter;
479 struct fingerprint_entry *entry_a;
480 const struct fingerprint_entry *entry_b;
482 hashmap_iter_init(&b->map, &iter);
484 hashmap_for_each_entry(&b->map, &iter, entry_b,
485 entry /* member name */) {
486 entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL);
487 if (entry_a) {
488 if (entry_a->count <= entry_b->count)
489 hashmap_remove(&a->map, &entry_b->entry, NULL);
490 else
491 entry_a->count -= entry_b->count;
496 /* Calculate fingerprints for a series of lines.
497 * Puts the fingerprints in the fingerprints array, which must have been
498 * preallocated to allow storing line_count elements.
500 static void get_line_fingerprints(struct fingerprint *fingerprints,
501 const char *content, const int *line_starts,
502 long first_line, long line_count)
504 int i;
505 const char *linestart, *lineend;
507 line_starts += first_line;
508 for (i = 0; i < line_count; ++i) {
509 linestart = content + line_starts[i];
510 lineend = content + line_starts[i + 1];
511 get_fingerprint(fingerprints + i, linestart, lineend);
515 static void free_line_fingerprints(struct fingerprint *fingerprints,
516 int nr_fingerprints)
518 int i;
520 for (i = 0; i < nr_fingerprints; i++)
521 free_fingerprint(&fingerprints[i]);
524 /* This contains the data necessary to linearly map a line number in one half
525 * of a diff chunk to the line in the other half of the diff chunk that is
526 * closest in terms of its position as a fraction of the length of the chunk.
528 struct line_number_mapping {
529 int destination_start, destination_length,
530 source_start, source_length;
533 /* Given a line number in one range, offset and scale it to map it onto the
534 * other range.
535 * Essentially this mapping is a simple linear equation but the calculation is
536 * more complicated to allow performing it with integer operations.
537 * Another complication is that if a line could map onto many lines in the
538 * destination range then we want to choose the line at the center of those
539 * possibilities.
540 * Example: if the chunk is 2 lines long in A and 10 lines long in B then the
541 * first 5 lines in B will map onto the first line in the A chunk, while the
542 * last 5 lines will all map onto the second line in the A chunk.
543 * Example: if the chunk is 10 lines long in A and 2 lines long in B then line
544 * 0 in B will map onto line 2 in A, and line 1 in B will map onto line 7 in A.
546 static int map_line_number(int line_number,
547 const struct line_number_mapping *mapping)
549 return ((line_number - mapping->source_start) * 2 + 1) *
550 mapping->destination_length /
551 (mapping->source_length * 2) +
552 mapping->destination_start;
555 /* Get a pointer to the element storing the similarity between a line in A
556 * and a line in B.
558 * The similarities are stored in a 2-dimensional array. Each "row" in the
559 * array contains the similarities for a line in B. The similarities stored in
560 * a row are the similarities between the line in B and the nearby lines in A.
561 * To keep the length of each row the same, it is padded out with values of -1
562 * where the search range extends beyond the lines in A.
563 * For example, if max_search_distance_a is 2 and the two sides of a diff chunk
564 * look like this:
565 * a | m
566 * b | n
567 * c | o
568 * d | p
569 * e | q
570 * Then the similarity array will contain:
571 * [-1, -1, am, bm, cm,
572 * -1, an, bn, cn, dn,
573 * ao, bo, co, do, eo,
574 * bp, cp, dp, ep, -1,
575 * cq, dq, eq, -1, -1]
576 * Where similarities are denoted either by -1 for invalid, or the
577 * concatenation of the two lines in the diff being compared.
579 * \param similarities array of similarities between lines in A and B
580 * \param line_a the index of the line in A, in the same frame of reference as
581 * closest_line_a.
582 * \param local_line_b the index of the line in B, relative to the first line
583 * in B that similarities represents.
584 * \param closest_line_a the index of the line in A that is deemed to be
585 * closest to local_line_b. This must be in the same
586 * frame of reference as line_a. This value defines
587 * where similarities is centered for the line in B.
588 * \param max_search_distance_a maximum distance in lines from the closest line
589 * in A for other lines in A for which
590 * similarities may be calculated.
592 static int *get_similarity(int *similarities,
593 int line_a, int local_line_b,
594 int closest_line_a, int max_search_distance_a)
596 assert(abs(line_a - closest_line_a) <=
597 max_search_distance_a);
598 return similarities + line_a - closest_line_a +
599 max_search_distance_a +
600 local_line_b * (max_search_distance_a * 2 + 1);
603 #define CERTAIN_NOTHING_MATCHES -2
604 #define CERTAINTY_NOT_CALCULATED -1
606 /* Given a line in B, first calculate its similarities with nearby lines in A
607 * if not already calculated, then identify the most similar and second most
608 * similar lines. The "certainty" is calculated based on those two
609 * similarities.
611 * \param start_a the index of the first line of the chunk in A
612 * \param length_a the length in lines of the chunk in A
613 * \param local_line_b the index of the line in B, relative to the first line
614 * in the chunk.
615 * \param fingerprints_a array of fingerprints for the chunk in A
616 * \param fingerprints_b array of fingerprints for the chunk in B
617 * \param similarities 2-dimensional array of similarities between lines in A
618 * and B. See get_similarity() for more details.
619 * \param certainties array of values indicating how strongly a line in B is
620 * matched with some line in A.
621 * \param second_best_result array of absolute indices in A for the second
622 * closest match of a line in B.
623 * \param result array of absolute indices in A for the closest match of a line
624 * in B.
625 * \param max_search_distance_a maximum distance in lines from the closest line
626 * in A for other lines in A for which
627 * similarities may be calculated.
628 * \param map_line_number_in_b_to_a parameter to map_line_number().
630 static void find_best_line_matches(
631 int start_a,
632 int length_a,
633 int start_b,
634 int local_line_b,
635 struct fingerprint *fingerprints_a,
636 struct fingerprint *fingerprints_b,
637 int *similarities,
638 int *certainties,
639 int *second_best_result,
640 int *result,
641 const int max_search_distance_a,
642 const struct line_number_mapping *map_line_number_in_b_to_a)
645 int i, search_start, search_end, closest_local_line_a, *similarity,
646 best_similarity = 0, second_best_similarity = 0,
647 best_similarity_index = 0, second_best_similarity_index = 0;
649 /* certainty has already been calculated so no need to redo the work */
650 if (certainties[local_line_b] != CERTAINTY_NOT_CALCULATED)
651 return;
653 closest_local_line_a = map_line_number(
654 local_line_b + start_b, map_line_number_in_b_to_a) - start_a;
656 search_start = closest_local_line_a - max_search_distance_a;
657 if (search_start < 0)
658 search_start = 0;
660 search_end = closest_local_line_a + max_search_distance_a + 1;
661 if (search_end > length_a)
662 search_end = length_a;
664 for (i = search_start; i < search_end; ++i) {
665 similarity = get_similarity(similarities,
666 i, local_line_b,
667 closest_local_line_a,
668 max_search_distance_a);
669 if (*similarity == -1) {
670 /* This value will never exceed 10 but assert just in
671 * case
673 assert(abs(i - closest_local_line_a) < 1000);
674 /* scale the similarity by (1000 - distance from
675 * closest line) to act as a tie break between lines
676 * that otherwise are equally similar.
678 *similarity = fingerprint_similarity(
679 fingerprints_b + local_line_b,
680 fingerprints_a + i) *
681 (1000 - abs(i - closest_local_line_a));
683 if (*similarity > best_similarity) {
684 second_best_similarity = best_similarity;
685 second_best_similarity_index = best_similarity_index;
686 best_similarity = *similarity;
687 best_similarity_index = i;
688 } else if (*similarity > second_best_similarity) {
689 second_best_similarity = *similarity;
690 second_best_similarity_index = i;
694 if (best_similarity == 0) {
695 /* this line definitely doesn't match with anything. Mark it
696 * with this special value so it doesn't get invalidated and
697 * won't be recalculated.
699 certainties[local_line_b] = CERTAIN_NOTHING_MATCHES;
700 result[local_line_b] = -1;
701 } else {
702 /* Calculate the certainty with which this line matches.
703 * If the line matches well with two lines then that reduces
704 * the certainty. However we still want to prioritise matching
705 * a line that matches very well with two lines over matching a
706 * line that matches poorly with one line, hence doubling
707 * best_similarity.
708 * This means that if we have
709 * line X that matches only one line with a score of 3,
710 * line Y that matches two lines equally with a score of 5,
711 * and line Z that matches only one line with a score or 2,
712 * then the lines in order of certainty are X, Y, Z.
714 certainties[local_line_b] = best_similarity * 2 -
715 second_best_similarity;
717 /* We keep both the best and second best results to allow us to
718 * check at a later stage of the matching process whether the
719 * result needs to be invalidated.
721 result[local_line_b] = start_a + best_similarity_index;
722 second_best_result[local_line_b] =
723 start_a + second_best_similarity_index;
728 * This finds the line that we can match with the most confidence, and
729 * uses it as a partition. It then calls itself on the lines on either side of
730 * that partition. In this way we avoid lines appearing out of order, and
731 * retain a sensible line ordering.
732 * \param start_a index of the first line in A with which lines in B may be
733 * compared.
734 * \param start_b index of the first line in B for which matching should be
735 * done.
736 * \param length_a number of lines in A with which lines in B may be compared.
737 * \param length_b number of lines in B for which matching should be done.
738 * \param fingerprints_a mutable array of fingerprints in A. The first element
739 * corresponds to the line at start_a.
740 * \param fingerprints_b array of fingerprints in B. The first element
741 * corresponds to the line at start_b.
742 * \param similarities 2-dimensional array of similarities between lines in A
743 * and B. See get_similarity() for more details.
744 * \param certainties array of values indicating how strongly a line in B is
745 * matched with some line in A.
746 * \param second_best_result array of absolute indices in A for the second
747 * closest match of a line in B.
748 * \param result array of absolute indices in A for the closest match of a line
749 * in B.
750 * \param max_search_distance_a maximum distance in lines from the closest line
751 * in A for other lines in A for which
752 * similarities may be calculated.
753 * \param max_search_distance_b an upper bound on the greatest possible
754 * distance between lines in B such that they will
755 * both be compared with the same line in A
756 * according to max_search_distance_a.
757 * \param map_line_number_in_b_to_a parameter to map_line_number().
759 static void fuzzy_find_matching_lines_recurse(
760 int start_a, int start_b,
761 int length_a, int length_b,
762 struct fingerprint *fingerprints_a,
763 struct fingerprint *fingerprints_b,
764 int *similarities,
765 int *certainties,
766 int *second_best_result,
767 int *result,
768 int max_search_distance_a,
769 int max_search_distance_b,
770 const struct line_number_mapping *map_line_number_in_b_to_a)
772 int i, invalidate_min, invalidate_max, offset_b,
773 second_half_start_a, second_half_start_b,
774 second_half_length_a, second_half_length_b,
775 most_certain_line_a, most_certain_local_line_b = -1,
776 most_certain_line_certainty = -1,
777 closest_local_line_a;
779 for (i = 0; i < length_b; ++i) {
780 find_best_line_matches(start_a,
781 length_a,
782 start_b,
784 fingerprints_a,
785 fingerprints_b,
786 similarities,
787 certainties,
788 second_best_result,
789 result,
790 max_search_distance_a,
791 map_line_number_in_b_to_a);
793 if (certainties[i] > most_certain_line_certainty) {
794 most_certain_line_certainty = certainties[i];
795 most_certain_local_line_b = i;
799 /* No matches. */
800 if (most_certain_local_line_b == -1)
801 return;
803 most_certain_line_a = result[most_certain_local_line_b];
806 * Subtract the most certain line's fingerprint in B from the matched
807 * fingerprint in A. This means that other lines in B can't also match
808 * the same parts of the line in A.
810 fingerprint_subtract(fingerprints_a + most_certain_line_a - start_a,
811 fingerprints_b + most_certain_local_line_b);
813 /* Invalidate results that may be affected by the choice of most
814 * certain line.
816 invalidate_min = most_certain_local_line_b - max_search_distance_b;
817 invalidate_max = most_certain_local_line_b + max_search_distance_b + 1;
818 if (invalidate_min < 0)
819 invalidate_min = 0;
820 if (invalidate_max > length_b)
821 invalidate_max = length_b;
823 /* As the fingerprint in A has changed, discard previously calculated
824 * similarity values with that fingerprint.
826 for (i = invalidate_min; i < invalidate_max; ++i) {
827 closest_local_line_a = map_line_number(
828 i + start_b, map_line_number_in_b_to_a) - start_a;
830 /* Check that the lines in A and B are close enough that there
831 * is a similarity value for them.
833 if (abs(most_certain_line_a - start_a - closest_local_line_a) >
834 max_search_distance_a) {
835 continue;
838 *get_similarity(similarities, most_certain_line_a - start_a,
839 i, closest_local_line_a,
840 max_search_distance_a) = -1;
843 /* More invalidating of results that may be affected by the choice of
844 * most certain line.
845 * Discard the matches for lines in B that are currently matched with a
846 * line in A such that their ordering contradicts the ordering imposed
847 * by the choice of most certain line.
849 for (i = most_certain_local_line_b - 1; i >= invalidate_min; --i) {
850 /* In this loop we discard results for lines in B that are
851 * before most-certain-line-B but are matched with a line in A
852 * that is after most-certain-line-A.
854 if (certainties[i] >= 0 &&
855 (result[i] >= most_certain_line_a ||
856 second_best_result[i] >= most_certain_line_a)) {
857 certainties[i] = CERTAINTY_NOT_CALCULATED;
860 for (i = most_certain_local_line_b + 1; i < invalidate_max; ++i) {
861 /* In this loop we discard results for lines in B that are
862 * after most-certain-line-B but are matched with a line in A
863 * that is before most-certain-line-A.
865 if (certainties[i] >= 0 &&
866 (result[i] <= most_certain_line_a ||
867 second_best_result[i] <= most_certain_line_a)) {
868 certainties[i] = CERTAINTY_NOT_CALCULATED;
872 /* Repeat the matching process for lines before the most certain line.
874 if (most_certain_local_line_b > 0) {
875 fuzzy_find_matching_lines_recurse(
876 start_a, start_b,
877 most_certain_line_a + 1 - start_a,
878 most_certain_local_line_b,
879 fingerprints_a, fingerprints_b, similarities,
880 certainties, second_best_result, result,
881 max_search_distance_a,
882 max_search_distance_b,
883 map_line_number_in_b_to_a);
885 /* Repeat the matching process for lines after the most certain line.
887 if (most_certain_local_line_b + 1 < length_b) {
888 second_half_start_a = most_certain_line_a;
889 offset_b = most_certain_local_line_b + 1;
890 second_half_start_b = start_b + offset_b;
891 second_half_length_a =
892 length_a + start_a - second_half_start_a;
893 second_half_length_b =
894 length_b + start_b - second_half_start_b;
895 fuzzy_find_matching_lines_recurse(
896 second_half_start_a, second_half_start_b,
897 second_half_length_a, second_half_length_b,
898 fingerprints_a + second_half_start_a - start_a,
899 fingerprints_b + offset_b,
900 similarities +
901 offset_b * (max_search_distance_a * 2 + 1),
902 certainties + offset_b,
903 second_best_result + offset_b, result + offset_b,
904 max_search_distance_a,
905 max_search_distance_b,
906 map_line_number_in_b_to_a);
910 /* Find the lines in the parent line range that most closely match the lines in
911 * the target line range. This is accomplished by matching fingerprints in each
912 * blame_origin, and choosing the best matches that preserve the line ordering.
913 * See struct fingerprint for details of fingerprint matching, and
914 * fuzzy_find_matching_lines_recurse for details of preserving line ordering.
916 * The performance is believed to be O(n log n) in the typical case and O(n^2)
917 * in a pathological case, where n is the number of lines in the target range.
919 static int *fuzzy_find_matching_lines(struct blame_origin *parent,
920 struct blame_origin *target,
921 int tlno, int parent_slno, int same,
922 int parent_len)
924 /* We use the terminology "A" for the left hand side of the diff AKA
925 * parent, and "B" for the right hand side of the diff AKA target. */
926 int start_a = parent_slno;
927 int length_a = parent_len;
928 int start_b = tlno;
929 int length_b = same - tlno;
931 struct line_number_mapping map_line_number_in_b_to_a = {
932 start_a, length_a, start_b, length_b
935 struct fingerprint *fingerprints_a = parent->fingerprints;
936 struct fingerprint *fingerprints_b = target->fingerprints;
938 int i, *result, *second_best_result,
939 *certainties, *similarities, similarity_count;
942 * max_search_distance_a means that given a line in B, compare it to
943 * the line in A that is closest to its position, and the lines in A
944 * that are no greater than max_search_distance_a lines away from the
945 * closest line in A.
947 * max_search_distance_b is an upper bound on the greatest possible
948 * distance between lines in B such that they will both be compared
949 * with the same line in A according to max_search_distance_a.
951 int max_search_distance_a = 10, max_search_distance_b;
953 if (length_a <= 0)
954 return NULL;
956 if (max_search_distance_a >= length_a)
957 max_search_distance_a = length_a ? length_a - 1 : 0;
959 max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
960 - 1) / length_a;
962 CALLOC_ARRAY(result, length_b);
963 CALLOC_ARRAY(second_best_result, length_b);
964 CALLOC_ARRAY(certainties, length_b);
966 /* See get_similarity() for details of similarities. */
967 similarity_count = length_b * (max_search_distance_a * 2 + 1);
968 CALLOC_ARRAY(similarities, similarity_count);
970 for (i = 0; i < length_b; ++i) {
971 result[i] = -1;
972 second_best_result[i] = -1;
973 certainties[i] = CERTAINTY_NOT_CALCULATED;
976 for (i = 0; i < similarity_count; ++i)
977 similarities[i] = -1;
979 fuzzy_find_matching_lines_recurse(start_a, start_b,
980 length_a, length_b,
981 fingerprints_a + start_a,
982 fingerprints_b + start_b,
983 similarities,
984 certainties,
985 second_best_result,
986 result,
987 max_search_distance_a,
988 max_search_distance_b,
989 &map_line_number_in_b_to_a);
991 free(similarities);
992 free(certainties);
993 free(second_best_result);
995 return result;
998 static void fill_origin_fingerprints(struct blame_origin *o)
1000 int *line_starts;
1002 if (o->fingerprints)
1003 return;
1004 o->num_lines = find_line_starts(&line_starts, o->file.ptr,
1005 o->file.size);
1006 CALLOC_ARRAY(o->fingerprints, o->num_lines);
1007 get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
1008 0, o->num_lines);
1009 free(line_starts);
1012 static void drop_origin_fingerprints(struct blame_origin *o)
1014 if (o->fingerprints) {
1015 free_line_fingerprints(o->fingerprints, o->num_lines);
1016 o->num_lines = 0;
1017 FREE_AND_NULL(o->fingerprints);
1022 * Given an origin, prepare mmfile_t structure to be used by the
1023 * diff machinery
1025 static void fill_origin_blob(struct diff_options *opt,
1026 struct blame_origin *o, mmfile_t *file,
1027 int *num_read_blob, int fill_fingerprints)
1029 if (!o->file.ptr) {
1030 enum object_type type;
1031 unsigned long file_size;
1033 (*num_read_blob)++;
1034 if (opt->flags.allow_textconv &&
1035 textconv_object(opt->repo, o->path, o->mode,
1036 &o->blob_oid, 1, &file->ptr, &file_size))
1038 else
1039 file->ptr = repo_read_object_file(the_repository,
1040 &o->blob_oid, &type,
1041 &file_size);
1042 file->size = file_size;
1044 if (!file->ptr)
1045 die("Cannot read blob %s for path %s",
1046 oid_to_hex(&o->blob_oid),
1047 o->path);
1048 o->file = *file;
1050 else
1051 *file = o->file;
1052 if (fill_fingerprints)
1053 fill_origin_fingerprints(o);
1056 static void drop_origin_blob(struct blame_origin *o)
1058 FREE_AND_NULL(o->file.ptr);
1059 drop_origin_fingerprints(o);
1063 * Any merge of blames happens on lists of blames that arrived via
1064 * different parents in a single suspect. In this case, we want to
1065 * sort according to the suspect line numbers as opposed to the final
1066 * image line numbers. The function body is somewhat longish because
1067 * it avoids unnecessary writes.
1070 static struct blame_entry *blame_merge(struct blame_entry *list1,
1071 struct blame_entry *list2)
1073 struct blame_entry *p1 = list1, *p2 = list2,
1074 **tail = &list1;
1076 if (!p1)
1077 return p2;
1078 if (!p2)
1079 return p1;
1081 if (p1->s_lno <= p2->s_lno) {
1082 do {
1083 tail = &p1->next;
1084 if (!(p1 = *tail)) {
1085 *tail = p2;
1086 return list1;
1088 } while (p1->s_lno <= p2->s_lno);
1090 for (;;) {
1091 *tail = p2;
1092 do {
1093 tail = &p2->next;
1094 if (!(p2 = *tail)) {
1095 *tail = p1;
1096 return list1;
1098 } while (p1->s_lno > p2->s_lno);
1099 *tail = p1;
1100 do {
1101 tail = &p1->next;
1102 if (!(p1 = *tail)) {
1103 *tail = p2;
1104 return list1;
1106 } while (p1->s_lno <= p2->s_lno);
1110 DEFINE_LIST_SORT(static, sort_blame_entries, struct blame_entry, next);
1113 * Final image line numbers are all different, so we don't need a
1114 * three-way comparison here.
1117 static int compare_blame_final(const struct blame_entry *e1,
1118 const struct blame_entry *e2)
1120 return e1->lno > e2->lno ? 1 : -1;
1123 static int compare_blame_suspect(const struct blame_entry *s1,
1124 const struct blame_entry *s2)
1127 * to allow for collating suspects, we sort according to the
1128 * respective pointer value as the primary sorting criterion.
1129 * The actual relation is pretty unimportant as long as it
1130 * establishes a total order. Comparing as integers gives us
1131 * that.
1133 if (s1->suspect != s2->suspect)
1134 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
1135 if (s1->s_lno == s2->s_lno)
1136 return 0;
1137 return s1->s_lno > s2->s_lno ? 1 : -1;
1140 void blame_sort_final(struct blame_scoreboard *sb)
1142 sort_blame_entries(&sb->ent, compare_blame_final);
1145 static int compare_commits_by_reverse_commit_date(const void *a,
1146 const void *b,
1147 void *c)
1149 return -compare_commits_by_commit_date(a, b, c);
1153 * For debugging -- origin is refcounted, and this asserts that
1154 * we do not underflow.
1156 static void sanity_check_refcnt(struct blame_scoreboard *sb)
1158 int baa = 0;
1159 struct blame_entry *ent;
1161 for (ent = sb->ent; ent; ent = ent->next) {
1162 /* Nobody should have zero or negative refcnt */
1163 if (ent->suspect->refcnt <= 0) {
1164 fprintf(stderr, "%s in %s has negative refcnt %d\n",
1165 ent->suspect->path,
1166 oid_to_hex(&ent->suspect->commit->object.oid),
1167 ent->suspect->refcnt);
1168 baa = 1;
1171 if (baa)
1172 sb->on_sanity_fail(sb, baa);
1176 * If two blame entries that are next to each other came from
1177 * contiguous lines in the same origin (i.e. <commit, path> pair),
1178 * merge them together.
1180 void blame_coalesce(struct blame_scoreboard *sb)
1182 struct blame_entry *ent, *next;
1184 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
1185 if (ent->suspect == next->suspect &&
1186 ent->s_lno + ent->num_lines == next->s_lno &&
1187 ent->lno + ent->num_lines == next->lno &&
1188 ent->ignored == next->ignored &&
1189 ent->unblamable == next->unblamable) {
1190 ent->num_lines += next->num_lines;
1191 ent->next = next->next;
1192 blame_origin_decref(next->suspect);
1193 free(next);
1194 ent->score = 0;
1195 next = ent; /* again */
1199 if (sb->debug) /* sanity */
1200 sanity_check_refcnt(sb);
1204 * Merge the given sorted list of blames into a preexisting origin.
1205 * If there were no previous blames to that commit, it is entered into
1206 * the commit priority queue of the score board.
1209 static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
1210 struct blame_entry *sorted)
1212 if (porigin->suspects)
1213 porigin->suspects = blame_merge(porigin->suspects, sorted);
1214 else {
1215 struct blame_origin *o;
1216 for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
1217 if (o->suspects) {
1218 porigin->suspects = sorted;
1219 return;
1222 porigin->suspects = sorted;
1223 prio_queue_put(&sb->commits, porigin->commit);
1228 * Fill the blob_sha1 field of an origin if it hasn't, so that later
1229 * call to fill_origin_blob() can use it to locate the data. blob_sha1
1230 * for an origin is also used to pass the blame for the entire file to
1231 * the parent to detect the case where a child's blob is identical to
1232 * that of its parent's.
1234 * This also fills origin->mode for corresponding tree path.
1236 static int fill_blob_sha1_and_mode(struct repository *r,
1237 struct blame_origin *origin)
1239 if (!is_null_oid(&origin->blob_oid))
1240 return 0;
1241 if (get_tree_entry(r, &origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
1242 goto error_out;
1243 if (oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB)
1244 goto error_out;
1245 return 0;
1246 error_out:
1247 oidclr(&origin->blob_oid);
1248 origin->mode = S_IFINVALID;
1249 return -1;
1252 struct blame_bloom_data {
1254 * Changed-path Bloom filter keys. These can help prevent
1255 * computing diffs against first parents, but we need to
1256 * expand the list as code is moved or files are renamed.
1258 struct bloom_filter_settings *settings;
1259 struct bloom_key **keys;
1260 int nr;
1261 int alloc;
1264 static int bloom_count_queries = 0;
1265 static int bloom_count_no = 0;
1266 static int maybe_changed_path(struct repository *r,
1267 struct blame_origin *origin,
1268 struct blame_bloom_data *bd)
1270 int i;
1271 struct bloom_filter *filter;
1273 if (!bd)
1274 return 1;
1276 if (commit_graph_generation(origin->commit) == GENERATION_NUMBER_INFINITY)
1277 return 1;
1279 filter = get_bloom_filter(r, origin->commit);
1281 if (!filter)
1282 return 1;
1284 bloom_count_queries++;
1285 for (i = 0; i < bd->nr; i++) {
1286 if (bloom_filter_contains(filter,
1287 bd->keys[i],
1288 bd->settings))
1289 return 1;
1292 bloom_count_no++;
1293 return 0;
1296 static void add_bloom_key(struct blame_bloom_data *bd,
1297 const char *path)
1299 if (!bd)
1300 return;
1302 if (bd->nr >= bd->alloc) {
1303 bd->alloc *= 2;
1304 REALLOC_ARRAY(bd->keys, bd->alloc);
1307 bd->keys[bd->nr] = xmalloc(sizeof(struct bloom_key));
1308 fill_bloom_key(path, strlen(path), bd->keys[bd->nr], bd->settings);
1309 bd->nr++;
1313 * We have an origin -- check if the same path exists in the
1314 * parent and return an origin structure to represent it.
1316 static struct blame_origin *find_origin(struct repository *r,
1317 struct commit *parent,
1318 struct blame_origin *origin,
1319 struct blame_bloom_data *bd)
1321 struct blame_origin *porigin;
1322 struct diff_options diff_opts;
1323 const char *paths[2];
1325 /* First check any existing origins */
1326 for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
1327 if (!strcmp(porigin->path, origin->path)) {
1329 * The same path between origin and its parent
1330 * without renaming -- the most common case.
1332 return blame_origin_incref (porigin);
1335 /* See if the origin->path is different between parent
1336 * and origin first. Most of the time they are the
1337 * same and diff-tree is fairly efficient about this.
1339 repo_diff_setup(r, &diff_opts);
1340 diff_opts.flags.recursive = 1;
1341 diff_opts.detect_rename = 0;
1342 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1343 paths[0] = origin->path;
1344 paths[1] = NULL;
1346 parse_pathspec(&diff_opts.pathspec,
1347 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1348 PATHSPEC_LITERAL_PATH, "", paths);
1349 diff_setup_done(&diff_opts);
1351 if (is_null_oid(&origin->commit->object.oid))
1352 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1353 else {
1354 int compute_diff = 1;
1355 if (origin->commit->parents &&
1356 oideq(&parent->object.oid,
1357 &origin->commit->parents->item->object.oid))
1358 compute_diff = maybe_changed_path(r, origin, bd);
1360 if (compute_diff)
1361 diff_tree_oid(get_commit_tree_oid(parent),
1362 get_commit_tree_oid(origin->commit),
1363 "", &diff_opts);
1365 diffcore_std(&diff_opts);
1367 if (!diff_queued_diff.nr) {
1368 /* The path is the same as parent */
1369 porigin = get_origin(parent, origin->path);
1370 oidcpy(&porigin->blob_oid, &origin->blob_oid);
1371 porigin->mode = origin->mode;
1372 } else {
1374 * Since origin->path is a pathspec, if the parent
1375 * commit had it as a directory, we will see a whole
1376 * bunch of deletion of files in the directory that we
1377 * do not care about.
1379 int i;
1380 struct diff_filepair *p = NULL;
1381 for (i = 0; i < diff_queued_diff.nr; i++) {
1382 const char *name;
1383 p = diff_queued_diff.queue[i];
1384 name = p->one->path ? p->one->path : p->two->path;
1385 if (!strcmp(name, origin->path))
1386 break;
1388 if (!p)
1389 die("internal error in blame::find_origin");
1390 switch (p->status) {
1391 default:
1392 die("internal error in blame::find_origin (%c)",
1393 p->status);
1394 case 'M':
1395 porigin = get_origin(parent, origin->path);
1396 oidcpy(&porigin->blob_oid, &p->one->oid);
1397 porigin->mode = p->one->mode;
1398 break;
1399 case 'A':
1400 case 'T':
1401 /* Did not exist in parent, or type changed */
1402 break;
1405 diff_flush(&diff_opts);
1406 return porigin;
1410 * We have an origin -- find the path that corresponds to it in its
1411 * parent and return an origin structure to represent it.
1413 static struct blame_origin *find_rename(struct repository *r,
1414 struct commit *parent,
1415 struct blame_origin *origin,
1416 struct blame_bloom_data *bd)
1418 struct blame_origin *porigin = NULL;
1419 struct diff_options diff_opts;
1420 int i;
1422 repo_diff_setup(r, &diff_opts);
1423 diff_opts.flags.recursive = 1;
1424 diff_opts.detect_rename = DIFF_DETECT_RENAME;
1425 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1426 diff_opts.single_follow = origin->path;
1427 diff_setup_done(&diff_opts);
1429 if (is_null_oid(&origin->commit->object.oid))
1430 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1431 else
1432 diff_tree_oid(get_commit_tree_oid(parent),
1433 get_commit_tree_oid(origin->commit),
1434 "", &diff_opts);
1435 diffcore_std(&diff_opts);
1437 for (i = 0; i < diff_queued_diff.nr; i++) {
1438 struct diff_filepair *p = diff_queued_diff.queue[i];
1439 if ((p->status == 'R' || p->status == 'C') &&
1440 !strcmp(p->two->path, origin->path)) {
1441 add_bloom_key(bd, p->one->path);
1442 porigin = get_origin(parent, p->one->path);
1443 oidcpy(&porigin->blob_oid, &p->one->oid);
1444 porigin->mode = p->one->mode;
1445 break;
1448 diff_flush(&diff_opts);
1449 return porigin;
1453 * Append a new blame entry to a given output queue.
1455 static void add_blame_entry(struct blame_entry ***queue,
1456 const struct blame_entry *src)
1458 struct blame_entry *e = xmalloc(sizeof(*e));
1459 memcpy(e, src, sizeof(*e));
1460 blame_origin_incref(e->suspect);
1462 e->next = **queue;
1463 **queue = e;
1464 *queue = &e->next;
1468 * src typically is on-stack; we want to copy the information in it to
1469 * a malloced blame_entry that gets added to the given queue. The
1470 * origin of dst loses a refcnt.
1472 static void dup_entry(struct blame_entry ***queue,
1473 struct blame_entry *dst, struct blame_entry *src)
1475 blame_origin_incref(src->suspect);
1476 blame_origin_decref(dst->suspect);
1477 memcpy(dst, src, sizeof(*src));
1478 dst->next = **queue;
1479 **queue = dst;
1480 *queue = &dst->next;
1483 const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
1485 return sb->final_buf + sb->lineno[lno];
1489 * It is known that lines between tlno to same came from parent, and e
1490 * has an overlap with that range. it also is known that parent's
1491 * line plno corresponds to e's line tlno.
1493 * <---- e ----->
1494 * <------>
1495 * <------------>
1496 * <------------>
1497 * <------------------>
1499 * Split e into potentially three parts; before this chunk, the chunk
1500 * to be blamed for the parent, and after that portion.
1502 static void split_overlap(struct blame_entry *split,
1503 struct blame_entry *e,
1504 int tlno, int plno, int same,
1505 struct blame_origin *parent)
1507 int chunk_end_lno;
1508 int i;
1509 memset(split, 0, sizeof(struct blame_entry [3]));
1511 for (i = 0; i < 3; i++) {
1512 split[i].ignored = e->ignored;
1513 split[i].unblamable = e->unblamable;
1516 if (e->s_lno < tlno) {
1517 /* there is a pre-chunk part not blamed on parent */
1518 split[0].suspect = blame_origin_incref(e->suspect);
1519 split[0].lno = e->lno;
1520 split[0].s_lno = e->s_lno;
1521 split[0].num_lines = tlno - e->s_lno;
1522 split[1].lno = e->lno + tlno - e->s_lno;
1523 split[1].s_lno = plno;
1525 else {
1526 split[1].lno = e->lno;
1527 split[1].s_lno = plno + (e->s_lno - tlno);
1530 if (same < e->s_lno + e->num_lines) {
1531 /* there is a post-chunk part not blamed on parent */
1532 split[2].suspect = blame_origin_incref(e->suspect);
1533 split[2].lno = e->lno + (same - e->s_lno);
1534 split[2].s_lno = e->s_lno + (same - e->s_lno);
1535 split[2].num_lines = e->s_lno + e->num_lines - same;
1536 chunk_end_lno = split[2].lno;
1538 else
1539 chunk_end_lno = e->lno + e->num_lines;
1540 split[1].num_lines = chunk_end_lno - split[1].lno;
1543 * if it turns out there is nothing to blame the parent for,
1544 * forget about the splitting. !split[1].suspect signals this.
1546 if (split[1].num_lines < 1)
1547 return;
1548 split[1].suspect = blame_origin_incref(parent);
1552 * split_overlap() divided an existing blame e into up to three parts
1553 * in split. Any assigned blame is moved to queue to
1554 * reflect the split.
1556 static void split_blame(struct blame_entry ***blamed,
1557 struct blame_entry ***unblamed,
1558 struct blame_entry *split,
1559 struct blame_entry *e)
1561 if (split[0].suspect && split[2].suspect) {
1562 /* The first part (reuse storage for the existing entry e) */
1563 dup_entry(unblamed, e, &split[0]);
1565 /* The last part -- me */
1566 add_blame_entry(unblamed, &split[2]);
1568 /* ... and the middle part -- parent */
1569 add_blame_entry(blamed, &split[1]);
1571 else if (!split[0].suspect && !split[2].suspect)
1573 * The parent covers the entire area; reuse storage for
1574 * e and replace it with the parent.
1576 dup_entry(blamed, e, &split[1]);
1577 else if (split[0].suspect) {
1578 /* me and then parent */
1579 dup_entry(unblamed, e, &split[0]);
1580 add_blame_entry(blamed, &split[1]);
1582 else {
1583 /* parent and then me */
1584 dup_entry(blamed, e, &split[1]);
1585 add_blame_entry(unblamed, &split[2]);
1590 * After splitting the blame, the origins used by the
1591 * on-stack blame_entry should lose one refcnt each.
1593 static void decref_split(struct blame_entry *split)
1595 int i;
1597 for (i = 0; i < 3; i++)
1598 blame_origin_decref(split[i].suspect);
1602 * reverse_blame reverses the list given in head, appending tail.
1603 * That allows us to build lists in reverse order, then reverse them
1604 * afterwards. This can be faster than building the list in proper
1605 * order right away. The reason is that building in proper order
1606 * requires writing a link in the _previous_ element, while building
1607 * in reverse order just requires placing the list head into the
1608 * _current_ element.
1611 static struct blame_entry *reverse_blame(struct blame_entry *head,
1612 struct blame_entry *tail)
1614 while (head) {
1615 struct blame_entry *next = head->next;
1616 head->next = tail;
1617 tail = head;
1618 head = next;
1620 return tail;
1624 * Splits a blame entry into two entries at 'len' lines. The original 'e'
1625 * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
1626 * which is returned, consists of the remainder: [e->lno + len, e->lno +
1627 * e->num_lines). The caller needs to sort out the reference counting for the
1628 * new entry's suspect.
1630 static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
1631 struct blame_origin *new_suspect)
1633 struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
1635 n->suspect = new_suspect;
1636 n->ignored = e->ignored;
1637 n->unblamable = e->unblamable;
1638 n->lno = e->lno + len;
1639 n->s_lno = e->s_lno + len;
1640 n->num_lines = e->num_lines - len;
1641 e->num_lines = len;
1642 e->score = 0;
1643 return n;
1646 struct blame_line_tracker {
1647 int is_parent;
1648 int s_lno;
1651 static int are_lines_adjacent(struct blame_line_tracker *first,
1652 struct blame_line_tracker *second)
1654 return first->is_parent == second->is_parent &&
1655 first->s_lno + 1 == second->s_lno;
1658 static int scan_parent_range(struct fingerprint *p_fps,
1659 struct fingerprint *t_fps, int t_idx,
1660 int from, int nr_lines)
1662 int sim, p_idx;
1663 #define FINGERPRINT_FILE_THRESHOLD 10
1664 int best_sim_val = FINGERPRINT_FILE_THRESHOLD;
1665 int best_sim_idx = -1;
1667 for (p_idx = from; p_idx < from + nr_lines; p_idx++) {
1668 sim = fingerprint_similarity(&t_fps[t_idx], &p_fps[p_idx]);
1669 if (sim < best_sim_val)
1670 continue;
1671 /* Break ties with the closest-to-target line number */
1672 if (sim == best_sim_val && best_sim_idx != -1 &&
1673 abs(best_sim_idx - t_idx) < abs(p_idx - t_idx))
1674 continue;
1675 best_sim_val = sim;
1676 best_sim_idx = p_idx;
1678 return best_sim_idx;
1682 * The first pass checks the blame entry (from the target) against the parent's
1683 * diff chunk. If that fails for a line, the second pass tries to match that
1684 * line to any part of parent file. That catches cases where a change was
1685 * broken into two chunks by 'context.'
1687 static void guess_line_blames(struct blame_origin *parent,
1688 struct blame_origin *target,
1689 int tlno, int offset, int same, int parent_len,
1690 struct blame_line_tracker *line_blames)
1692 int i, best_idx, target_idx;
1693 int parent_slno = tlno + offset;
1694 int *fuzzy_matches;
1696 fuzzy_matches = fuzzy_find_matching_lines(parent, target,
1697 tlno, parent_slno, same,
1698 parent_len);
1699 for (i = 0; i < same - tlno; i++) {
1700 target_idx = tlno + i;
1701 if (fuzzy_matches && fuzzy_matches[i] >= 0) {
1702 best_idx = fuzzy_matches[i];
1703 } else {
1704 best_idx = scan_parent_range(parent->fingerprints,
1705 target->fingerprints,
1706 target_idx, 0,
1707 parent->num_lines);
1709 if (best_idx >= 0) {
1710 line_blames[i].is_parent = 1;
1711 line_blames[i].s_lno = best_idx;
1712 } else {
1713 line_blames[i].is_parent = 0;
1714 line_blames[i].s_lno = target_idx;
1717 free(fuzzy_matches);
1721 * This decides which parts of a blame entry go to the parent (added to the
1722 * ignoredp list) and which stay with the target (added to the diffp list). The
1723 * actual decision was made in a separate heuristic function, and those answers
1724 * for the lines in 'e' are in line_blames. This consumes e, essentially
1725 * putting it on a list.
1727 * Note that the blame entries on the ignoredp list are not necessarily sorted
1728 * with respect to the parent's line numbers yet.
1730 static void ignore_blame_entry(struct blame_entry *e,
1731 struct blame_origin *parent,
1732 struct blame_entry **diffp,
1733 struct blame_entry **ignoredp,
1734 struct blame_line_tracker *line_blames)
1736 int entry_len, nr_lines, i;
1739 * We carve new entries off the front of e. Each entry comes from a
1740 * contiguous chunk of lines: adjacent lines from the same origin
1741 * (either the parent or the target).
1743 entry_len = 1;
1744 nr_lines = e->num_lines; /* e changes in the loop */
1745 for (i = 0; i < nr_lines; i++) {
1746 struct blame_entry *next = NULL;
1749 * We are often adjacent to the next line - only split the blame
1750 * entry when we have to.
1752 if (i + 1 < nr_lines) {
1753 if (are_lines_adjacent(&line_blames[i],
1754 &line_blames[i + 1])) {
1755 entry_len++;
1756 continue;
1758 next = split_blame_at(e, entry_len,
1759 blame_origin_incref(e->suspect));
1761 if (line_blames[i].is_parent) {
1762 e->ignored = 1;
1763 blame_origin_decref(e->suspect);
1764 e->suspect = blame_origin_incref(parent);
1765 e->s_lno = line_blames[i - entry_len + 1].s_lno;
1766 e->next = *ignoredp;
1767 *ignoredp = e;
1768 } else {
1769 e->unblamable = 1;
1770 /* e->s_lno is already in the target's address space. */
1771 e->next = *diffp;
1772 *diffp = e;
1774 assert(e->num_lines == entry_len);
1775 e = next;
1776 entry_len = 1;
1778 assert(!e);
1782 * Process one hunk from the patch between the current suspect for
1783 * blame_entry e and its parent. This first blames any unfinished
1784 * entries before the chunk (which is where target and parent start
1785 * differing) on the parent, and then splits blame entries at the
1786 * start and at the end of the difference region. Since use of -M and
1787 * -C options may lead to overlapping/duplicate source line number
1788 * ranges, all we can rely on from sorting/merging is the order of the
1789 * first suspect line number.
1791 * tlno: line number in the target where this chunk begins
1792 * same: line number in the target where this chunk ends
1793 * offset: add to tlno to get the chunk starting point in the parent
1794 * parent_len: number of lines in the parent chunk
1796 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
1797 int tlno, int offset, int same, int parent_len,
1798 struct blame_origin *parent,
1799 struct blame_origin *target, int ignore_diffs)
1801 struct blame_entry *e = **srcq;
1802 struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL;
1803 struct blame_line_tracker *line_blames = NULL;
1805 while (e && e->s_lno < tlno) {
1806 struct blame_entry *next = e->next;
1808 * current record starts before differing portion. If
1809 * it reaches into it, we need to split it up and
1810 * examine the second part separately.
1812 if (e->s_lno + e->num_lines > tlno) {
1813 /* Move second half to a new record */
1814 struct blame_entry *n;
1816 n = split_blame_at(e, tlno - e->s_lno, e->suspect);
1817 /* Push new record to diffp */
1818 n->next = diffp;
1819 diffp = n;
1820 } else
1821 blame_origin_decref(e->suspect);
1822 /* Pass blame for everything before the differing
1823 * chunk to the parent */
1824 e->suspect = blame_origin_incref(parent);
1825 e->s_lno += offset;
1826 e->next = samep;
1827 samep = e;
1828 e = next;
1831 * As we don't know how much of a common stretch after this
1832 * diff will occur, the currently blamed parts are all that we
1833 * can assign to the parent for now.
1836 if (samep) {
1837 **dstq = reverse_blame(samep, **dstq);
1838 *dstq = &samep->next;
1841 * Prepend the split off portions: everything after e starts
1842 * after the blameable portion.
1844 e = reverse_blame(diffp, e);
1847 * Now retain records on the target while parts are different
1848 * from the parent.
1850 samep = NULL;
1851 diffp = NULL;
1853 if (ignore_diffs && same - tlno > 0) {
1854 CALLOC_ARRAY(line_blames, same - tlno);
1855 guess_line_blames(parent, target, tlno, offset, same,
1856 parent_len, line_blames);
1859 while (e && e->s_lno < same) {
1860 struct blame_entry *next = e->next;
1863 * If current record extends into sameness, need to split.
1865 if (e->s_lno + e->num_lines > same) {
1867 * Move second half to a new record to be
1868 * processed by later chunks
1870 struct blame_entry *n;
1872 n = split_blame_at(e, same - e->s_lno,
1873 blame_origin_incref(e->suspect));
1874 /* Push new record to samep */
1875 n->next = samep;
1876 samep = n;
1878 if (ignore_diffs) {
1879 ignore_blame_entry(e, parent, &diffp, &ignoredp,
1880 line_blames + e->s_lno - tlno);
1881 } else {
1882 e->next = diffp;
1883 diffp = e;
1885 e = next;
1887 free(line_blames);
1888 if (ignoredp) {
1890 * Note ignoredp is not sorted yet, and thus neither is dstq.
1891 * That list must be sorted before we queue_blames(). We defer
1892 * sorting until after all diff hunks are processed, so that
1893 * guess_line_blames() can pick *any* line in the parent. The
1894 * slight drawback is that we end up sorting all blame entries
1895 * passed to the parent, including those that are unrelated to
1896 * changes made by the ignored commit.
1898 **dstq = reverse_blame(ignoredp, **dstq);
1899 *dstq = &ignoredp->next;
1901 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
1902 /* Move across elements that are in the unblamable portion */
1903 if (diffp)
1904 *srcq = &diffp->next;
1907 struct blame_chunk_cb_data {
1908 struct blame_origin *parent;
1909 struct blame_origin *target;
1910 long offset;
1911 int ignore_diffs;
1912 struct blame_entry **dstq;
1913 struct blame_entry **srcq;
1916 /* diff chunks are from parent to target */
1917 static int blame_chunk_cb(long start_a, long count_a,
1918 long start_b, long count_b, void *data)
1920 struct blame_chunk_cb_data *d = data;
1921 if (start_a - start_b != d->offset)
1922 die("internal error in blame::blame_chunk_cb");
1923 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
1924 start_b + count_b, count_a, d->parent, d->target,
1925 d->ignore_diffs);
1926 d->offset = start_a + count_a - (start_b + count_b);
1927 return 0;
1931 * We are looking at the origin 'target' and aiming to pass blame
1932 * for the lines it is suspected to its parent. Run diff to find
1933 * which lines came from parent and pass blame for them.
1935 static void pass_blame_to_parent(struct blame_scoreboard *sb,
1936 struct blame_origin *target,
1937 struct blame_origin *parent, int ignore_diffs)
1939 mmfile_t file_p, file_o;
1940 struct blame_chunk_cb_data d;
1941 struct blame_entry *newdest = NULL;
1943 if (!target->suspects)
1944 return; /* nothing remains for this target */
1946 d.parent = parent;
1947 d.target = target;
1948 d.offset = 0;
1949 d.ignore_diffs = ignore_diffs;
1950 d.dstq = &newdest; d.srcq = &target->suspects;
1952 fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
1953 &sb->num_read_blob, ignore_diffs);
1954 fill_origin_blob(&sb->revs->diffopt, target, &file_o,
1955 &sb->num_read_blob, ignore_diffs);
1956 sb->num_get_patch++;
1958 if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
1959 die("unable to generate diff (%s -> %s)",
1960 oid_to_hex(&parent->commit->object.oid),
1961 oid_to_hex(&target->commit->object.oid));
1962 /* The rest are the same as the parent */
1963 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
1964 parent, target, 0);
1965 *d.dstq = NULL;
1966 if (ignore_diffs)
1967 sort_blame_entries(&newdest, compare_blame_suspect);
1968 queue_blames(sb, parent, newdest);
1970 return;
1974 * The lines in blame_entry after splitting blames many times can become
1975 * very small and trivial, and at some point it becomes pointless to
1976 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
1977 * ordinary C program, and it is not worth to say it was copied from
1978 * totally unrelated file in the parent.
1980 * Compute how trivial the lines in the blame_entry are.
1982 unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
1984 unsigned score;
1985 const char *cp, *ep;
1987 if (e->score)
1988 return e->score;
1990 score = 1;
1991 cp = blame_nth_line(sb, e->lno);
1992 ep = blame_nth_line(sb, e->lno + e->num_lines);
1993 while (cp < ep) {
1994 unsigned ch = *((unsigned char *)cp);
1995 if (isalnum(ch))
1996 score++;
1997 cp++;
1999 e->score = score;
2000 return score;
2004 * best_so_far[] and potential[] are both a split of an existing blame_entry
2005 * that passes blame to the parent. Maintain best_so_far the best split so
2006 * far, by comparing potential and best_so_far and copying potential into
2007 * bst_so_far as needed.
2009 static void copy_split_if_better(struct blame_scoreboard *sb,
2010 struct blame_entry *best_so_far,
2011 struct blame_entry *potential)
2013 int i;
2015 if (!potential[1].suspect)
2016 return;
2017 if (best_so_far[1].suspect) {
2018 if (blame_entry_score(sb, &potential[1]) <
2019 blame_entry_score(sb, &best_so_far[1]))
2020 return;
2023 for (i = 0; i < 3; i++)
2024 blame_origin_incref(potential[i].suspect);
2025 decref_split(best_so_far);
2026 memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
2030 * We are looking at a part of the final image represented by
2031 * ent (tlno and same are offset by ent->s_lno).
2032 * tlno is where we are looking at in the final image.
2033 * up to (but not including) same match preimage.
2034 * plno is where we are looking at in the preimage.
2036 * <-------------- final image ---------------------->
2037 * <------ent------>
2038 * ^tlno ^same
2039 * <---------preimage----->
2040 * ^plno
2042 * All line numbers are 0-based.
2044 static void handle_split(struct blame_scoreboard *sb,
2045 struct blame_entry *ent,
2046 int tlno, int plno, int same,
2047 struct blame_origin *parent,
2048 struct blame_entry *split)
2050 if (ent->num_lines <= tlno)
2051 return;
2052 if (tlno < same) {
2053 struct blame_entry potential[3];
2054 tlno += ent->s_lno;
2055 same += ent->s_lno;
2056 split_overlap(potential, ent, tlno, plno, same, parent);
2057 copy_split_if_better(sb, split, potential);
2058 decref_split(potential);
2062 struct handle_split_cb_data {
2063 struct blame_scoreboard *sb;
2064 struct blame_entry *ent;
2065 struct blame_origin *parent;
2066 struct blame_entry *split;
2067 long plno;
2068 long tlno;
2071 static int handle_split_cb(long start_a, long count_a,
2072 long start_b, long count_b, void *data)
2074 struct handle_split_cb_data *d = data;
2075 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
2076 d->split);
2077 d->plno = start_a + count_a;
2078 d->tlno = start_b + count_b;
2079 return 0;
2083 * Find the lines from parent that are the same as ent so that
2084 * we can pass blames to it. file_p has the blob contents for
2085 * the parent.
2087 static void find_copy_in_blob(struct blame_scoreboard *sb,
2088 struct blame_entry *ent,
2089 struct blame_origin *parent,
2090 struct blame_entry *split,
2091 mmfile_t *file_p)
2093 const char *cp;
2094 mmfile_t file_o;
2095 struct handle_split_cb_data d;
2097 memset(&d, 0, sizeof(d));
2098 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
2100 * Prepare mmfile that contains only the lines in ent.
2102 cp = blame_nth_line(sb, ent->lno);
2103 file_o.ptr = (char *) cp;
2104 file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
2107 * file_o is a part of final image we are annotating.
2108 * file_p partially may match that image.
2110 memset(split, 0, sizeof(struct blame_entry [3]));
2111 if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
2112 die("unable to generate diff (%s)",
2113 oid_to_hex(&parent->commit->object.oid));
2114 /* remainder, if any, all match the preimage */
2115 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
2118 /* Move all blame entries from list *source that have a score smaller
2119 * than score_min to the front of list *small.
2120 * Returns a pointer to the link pointing to the old head of the small list.
2123 static struct blame_entry **filter_small(struct blame_scoreboard *sb,
2124 struct blame_entry **small,
2125 struct blame_entry **source,
2126 unsigned score_min)
2128 struct blame_entry *p = *source;
2129 struct blame_entry *oldsmall = *small;
2130 while (p) {
2131 if (blame_entry_score(sb, p) <= score_min) {
2132 *small = p;
2133 small = &p->next;
2134 p = *small;
2135 } else {
2136 *source = p;
2137 source = &p->next;
2138 p = *source;
2141 *small = oldsmall;
2142 *source = NULL;
2143 return small;
2147 * See if lines currently target is suspected for can be attributed to
2148 * parent.
2150 static void find_move_in_parent(struct blame_scoreboard *sb,
2151 struct blame_entry ***blamed,
2152 struct blame_entry **toosmall,
2153 struct blame_origin *target,
2154 struct blame_origin *parent)
2156 struct blame_entry *e, split[3];
2157 struct blame_entry *unblamed = target->suspects;
2158 struct blame_entry *leftover = NULL;
2159 mmfile_t file_p;
2161 if (!unblamed)
2162 return; /* nothing remains for this target */
2164 fill_origin_blob(&sb->revs->diffopt, parent, &file_p,
2165 &sb->num_read_blob, 0);
2166 if (!file_p.ptr)
2167 return;
2169 /* At each iteration, unblamed has a NULL-terminated list of
2170 * entries that have not yet been tested for blame. leftover
2171 * contains the reversed list of entries that have been tested
2172 * without being assignable to the parent.
2174 do {
2175 struct blame_entry **unblamedtail = &unblamed;
2176 struct blame_entry *next;
2177 for (e = unblamed; e; e = next) {
2178 next = e->next;
2179 find_copy_in_blob(sb, e, parent, split, &file_p);
2180 if (split[1].suspect &&
2181 sb->move_score < blame_entry_score(sb, &split[1])) {
2182 split_blame(blamed, &unblamedtail, split, e);
2183 } else {
2184 e->next = leftover;
2185 leftover = e;
2187 decref_split(split);
2189 *unblamedtail = NULL;
2190 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
2191 } while (unblamed);
2192 target->suspects = reverse_blame(leftover, NULL);
2195 struct blame_list {
2196 struct blame_entry *ent;
2197 struct blame_entry split[3];
2201 * Count the number of entries the target is suspected for,
2202 * and prepare a list of entry and the best split.
2204 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
2205 int *num_ents_p)
2207 struct blame_entry *e;
2208 int num_ents, i;
2209 struct blame_list *blame_list = NULL;
2211 for (e = unblamed, num_ents = 0; e; e = e->next)
2212 num_ents++;
2213 if (num_ents) {
2214 CALLOC_ARRAY(blame_list, num_ents);
2215 for (e = unblamed, i = 0; e; e = e->next)
2216 blame_list[i++].ent = e;
2218 *num_ents_p = num_ents;
2219 return blame_list;
2223 * For lines target is suspected for, see if we can find code movement
2224 * across file boundary from the parent commit. porigin is the path
2225 * in the parent we already tried.
2227 static void find_copy_in_parent(struct blame_scoreboard *sb,
2228 struct blame_entry ***blamed,
2229 struct blame_entry **toosmall,
2230 struct blame_origin *target,
2231 struct commit *parent,
2232 struct blame_origin *porigin,
2233 int opt)
2235 struct diff_options diff_opts;
2236 int i, j;
2237 struct blame_list *blame_list;
2238 int num_ents;
2239 struct blame_entry *unblamed = target->suspects;
2240 struct blame_entry *leftover = NULL;
2242 if (!unblamed)
2243 return; /* nothing remains for this target */
2245 repo_diff_setup(sb->repo, &diff_opts);
2246 diff_opts.flags.recursive = 1;
2247 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
2249 diff_setup_done(&diff_opts);
2251 /* Try "find copies harder" on new path if requested;
2252 * we do not want to use diffcore_rename() actually to
2253 * match things up; find_copies_harder is set only to
2254 * force diff_tree_oid() to feed all filepairs to diff_queue,
2255 * and this code needs to be after diff_setup_done(), which
2256 * usually makes find-copies-harder imply copy detection.
2258 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
2259 || ((opt & PICKAXE_BLAME_COPY_HARDER)
2260 && (!porigin || strcmp(target->path, porigin->path))))
2261 diff_opts.flags.find_copies_harder = 1;
2263 if (is_null_oid(&target->commit->object.oid))
2264 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
2265 else
2266 diff_tree_oid(get_commit_tree_oid(parent),
2267 get_commit_tree_oid(target->commit),
2268 "", &diff_opts);
2270 if (!diff_opts.flags.find_copies_harder)
2271 diffcore_std(&diff_opts);
2273 do {
2274 struct blame_entry **unblamedtail = &unblamed;
2275 blame_list = setup_blame_list(unblamed, &num_ents);
2277 for (i = 0; i < diff_queued_diff.nr; i++) {
2278 struct diff_filepair *p = diff_queued_diff.queue[i];
2279 struct blame_origin *norigin;
2280 mmfile_t file_p;
2281 struct blame_entry potential[3];
2283 if (!DIFF_FILE_VALID(p->one))
2284 continue; /* does not exist in parent */
2285 if (S_ISGITLINK(p->one->mode))
2286 continue; /* ignore git links */
2287 if (porigin && !strcmp(p->one->path, porigin->path))
2288 /* find_move already dealt with this path */
2289 continue;
2291 norigin = get_origin(parent, p->one->path);
2292 oidcpy(&norigin->blob_oid, &p->one->oid);
2293 norigin->mode = p->one->mode;
2294 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p,
2295 &sb->num_read_blob, 0);
2296 if (!file_p.ptr)
2297 continue;
2299 for (j = 0; j < num_ents; j++) {
2300 find_copy_in_blob(sb, blame_list[j].ent,
2301 norigin, potential, &file_p);
2302 copy_split_if_better(sb, blame_list[j].split,
2303 potential);
2304 decref_split(potential);
2306 blame_origin_decref(norigin);
2309 for (j = 0; j < num_ents; j++) {
2310 struct blame_entry *split = blame_list[j].split;
2311 if (split[1].suspect &&
2312 sb->copy_score < blame_entry_score(sb, &split[1])) {
2313 split_blame(blamed, &unblamedtail, split,
2314 blame_list[j].ent);
2315 } else {
2316 blame_list[j].ent->next = leftover;
2317 leftover = blame_list[j].ent;
2319 decref_split(split);
2321 free(blame_list);
2322 *unblamedtail = NULL;
2323 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
2324 } while (unblamed);
2325 target->suspects = reverse_blame(leftover, NULL);
2326 diff_flush(&diff_opts);
2330 * The blobs of origin and porigin exactly match, so everything
2331 * origin is suspected for can be blamed on the parent.
2333 static void pass_whole_blame(struct blame_scoreboard *sb,
2334 struct blame_origin *origin, struct blame_origin *porigin)
2336 struct blame_entry *e, *suspects;
2338 if (!porigin->file.ptr && origin->file.ptr) {
2339 /* Steal its file */
2340 porigin->file = origin->file;
2341 origin->file.ptr = NULL;
2343 suspects = origin->suspects;
2344 origin->suspects = NULL;
2345 for (e = suspects; e; e = e->next) {
2346 blame_origin_incref(porigin);
2347 blame_origin_decref(e->suspect);
2348 e->suspect = porigin;
2350 queue_blames(sb, porigin, suspects);
2354 * We pass blame from the current commit to its parents. We keep saying
2355 * "parent" (and "porigin"), but what we mean is to find scapegoat to
2356 * exonerate ourselves.
2358 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
2359 int reverse)
2361 if (!reverse) {
2362 if (revs->first_parent_only &&
2363 commit->parents &&
2364 commit->parents->next) {
2365 free_commit_list(commit->parents->next);
2366 commit->parents->next = NULL;
2368 return commit->parents;
2370 return lookup_decoration(&revs->children, &commit->object);
2373 static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
2375 struct commit_list *l = first_scapegoat(revs, commit, reverse);
2376 return commit_list_count(l);
2379 /* Distribute collected unsorted blames to the respected sorted lists
2380 * in the various origins.
2382 static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
2384 sort_blame_entries(&blamed, compare_blame_suspect);
2385 while (blamed)
2387 struct blame_origin *porigin = blamed->suspect;
2388 struct blame_entry *suspects = NULL;
2389 do {
2390 struct blame_entry *next = blamed->next;
2391 blamed->next = suspects;
2392 suspects = blamed;
2393 blamed = next;
2394 } while (blamed && blamed->suspect == porigin);
2395 suspects = reverse_blame(suspects, NULL);
2396 queue_blames(sb, porigin, suspects);
2400 #define MAXSG 16
2402 typedef struct blame_origin *(*blame_find_alg)(struct repository *,
2403 struct commit *,
2404 struct blame_origin *,
2405 struct blame_bloom_data *);
2407 static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
2409 struct rev_info *revs = sb->revs;
2410 int i, pass, num_sg;
2411 struct commit *commit = origin->commit;
2412 struct commit_list *sg;
2413 struct blame_origin *sg_buf[MAXSG];
2414 struct blame_origin *porigin, **sg_origin = sg_buf;
2415 struct blame_entry *toosmall = NULL;
2416 struct blame_entry *blames, **blametail = &blames;
2418 num_sg = num_scapegoats(revs, commit, sb->reverse);
2419 if (!num_sg)
2420 goto finish;
2421 else if (num_sg < ARRAY_SIZE(sg_buf))
2422 memset(sg_buf, 0, sizeof(sg_buf));
2423 else
2424 CALLOC_ARRAY(sg_origin, num_sg);
2427 * The first pass looks for unrenamed path to optimize for
2428 * common cases, then we look for renames in the second pass.
2430 for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
2431 blame_find_alg find = pass ? find_rename : find_origin;
2433 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2434 i < num_sg && sg;
2435 sg = sg->next, i++) {
2436 struct commit *p = sg->item;
2437 int j, same;
2439 if (sg_origin[i])
2440 continue;
2441 if (repo_parse_commit(the_repository, p))
2442 continue;
2443 porigin = find(sb->repo, p, origin, sb->bloom_data);
2444 if (!porigin)
2445 continue;
2446 if (oideq(&porigin->blob_oid, &origin->blob_oid)) {
2447 pass_whole_blame(sb, origin, porigin);
2448 blame_origin_decref(porigin);
2449 goto finish;
2451 for (j = same = 0; j < i; j++)
2452 if (sg_origin[j] &&
2453 oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
2454 same = 1;
2455 break;
2457 if (!same)
2458 sg_origin[i] = porigin;
2459 else
2460 blame_origin_decref(porigin);
2464 sb->num_commits++;
2465 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2466 i < num_sg && sg;
2467 sg = sg->next, i++) {
2468 struct blame_origin *porigin = sg_origin[i];
2469 if (!porigin)
2470 continue;
2471 if (!origin->previous) {
2472 blame_origin_incref(porigin);
2473 origin->previous = porigin;
2475 pass_blame_to_parent(sb, origin, porigin, 0);
2476 if (!origin->suspects)
2477 goto finish;
2481 * Pass remaining suspects for ignored commits to their parents.
2483 if (oidset_contains(&sb->ignore_list, &commit->object.oid)) {
2484 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2485 i < num_sg && sg;
2486 sg = sg->next, i++) {
2487 struct blame_origin *porigin = sg_origin[i];
2489 if (!porigin)
2490 continue;
2491 pass_blame_to_parent(sb, origin, porigin, 1);
2493 * Preemptively drop porigin so we can refresh the
2494 * fingerprints if we use the parent again, which can
2495 * occur if you ignore back-to-back commits.
2497 drop_origin_blob(porigin);
2498 if (!origin->suspects)
2499 goto finish;
2504 * Optionally find moves in parents' files.
2506 if (opt & PICKAXE_BLAME_MOVE) {
2507 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
2508 if (origin->suspects) {
2509 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2510 i < num_sg && sg;
2511 sg = sg->next, i++) {
2512 struct blame_origin *porigin = sg_origin[i];
2513 if (!porigin)
2514 continue;
2515 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
2516 if (!origin->suspects)
2517 break;
2523 * Optionally find copies from parents' files.
2525 if (opt & PICKAXE_BLAME_COPY) {
2526 if (sb->copy_score > sb->move_score)
2527 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
2528 else if (sb->copy_score < sb->move_score) {
2529 origin->suspects = blame_merge(origin->suspects, toosmall);
2530 toosmall = NULL;
2531 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
2533 if (!origin->suspects)
2534 goto finish;
2536 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
2537 i < num_sg && sg;
2538 sg = sg->next, i++) {
2539 struct blame_origin *porigin = sg_origin[i];
2540 find_copy_in_parent(sb, &blametail, &toosmall,
2541 origin, sg->item, porigin, opt);
2542 if (!origin->suspects)
2543 goto finish;
2547 finish:
2548 *blametail = NULL;
2549 distribute_blame(sb, blames);
2551 * prepend toosmall to origin->suspects
2553 * There is no point in sorting: this ends up on a big
2554 * unsorted list in the caller anyway.
2556 if (toosmall) {
2557 struct blame_entry **tail = &toosmall;
2558 while (*tail)
2559 tail = &(*tail)->next;
2560 *tail = origin->suspects;
2561 origin->suspects = toosmall;
2563 for (i = 0; i < num_sg; i++) {
2564 if (sg_origin[i]) {
2565 if (!sg_origin[i]->suspects)
2566 drop_origin_blob(sg_origin[i]);
2567 blame_origin_decref(sg_origin[i]);
2570 drop_origin_blob(origin);
2571 if (sg_buf != sg_origin)
2572 free(sg_origin);
2576 * The main loop -- while we have blobs with lines whose true origin
2577 * is still unknown, pick one blob, and allow its lines to pass blames
2578 * to its parents. */
2579 void assign_blame(struct blame_scoreboard *sb, int opt)
2581 struct rev_info *revs = sb->revs;
2582 struct commit *commit = prio_queue_get(&sb->commits);
2584 while (commit) {
2585 struct blame_entry *ent;
2586 struct blame_origin *suspect = get_blame_suspects(commit);
2588 /* find one suspect to break down */
2589 while (suspect && !suspect->suspects)
2590 suspect = suspect->next;
2592 if (!suspect) {
2593 commit = prio_queue_get(&sb->commits);
2594 continue;
2597 assert(commit == suspect->commit);
2600 * We will use this suspect later in the loop,
2601 * so hold onto it in the meantime.
2603 blame_origin_incref(suspect);
2604 repo_parse_commit(the_repository, commit);
2605 if (sb->reverse ||
2606 (!(commit->object.flags & UNINTERESTING) &&
2607 !(revs->max_age != -1 && commit->date < revs->max_age)))
2608 pass_blame(sb, suspect, opt);
2609 else {
2610 commit->object.flags |= UNINTERESTING;
2611 if (commit->object.parsed)
2612 mark_parents_uninteresting(sb->revs, commit);
2614 /* treat root commit as boundary */
2615 if (!commit->parents && !sb->show_root)
2616 commit->object.flags |= UNINTERESTING;
2618 /* Take responsibility for the remaining entries */
2619 ent = suspect->suspects;
2620 if (ent) {
2621 suspect->guilty = 1;
2622 for (;;) {
2623 struct blame_entry *next = ent->next;
2624 if (sb->found_guilty_entry)
2625 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
2626 if (next) {
2627 ent = next;
2628 continue;
2630 ent->next = sb->ent;
2631 sb->ent = suspect->suspects;
2632 suspect->suspects = NULL;
2633 break;
2636 blame_origin_decref(suspect);
2638 if (sb->debug) /* sanity */
2639 sanity_check_refcnt(sb);
2644 * To allow quick access to the contents of nth line in the
2645 * final image, prepare an index in the scoreboard.
2647 static int prepare_lines(struct blame_scoreboard *sb)
2649 sb->num_lines = find_line_starts(&sb->lineno, sb->final_buf,
2650 sb->final_buf_size);
2651 return sb->num_lines;
2654 static struct commit *find_single_final(struct rev_info *revs,
2655 const char **name_p)
2657 int i;
2658 struct commit *found = NULL;
2659 const char *name = NULL;
2661 for (i = 0; i < revs->pending.nr; i++) {
2662 struct object *obj = revs->pending.objects[i].item;
2663 if (obj->flags & UNINTERESTING)
2664 continue;
2665 obj = deref_tag(revs->repo, obj, NULL, 0);
2666 if (!obj || obj->type != OBJ_COMMIT)
2667 die("Non commit %s?", revs->pending.objects[i].name);
2668 if (found)
2669 die("More than one commit to dig from %s and %s?",
2670 revs->pending.objects[i].name, name);
2671 found = (struct commit *)obj;
2672 name = revs->pending.objects[i].name;
2674 if (name_p)
2675 *name_p = xstrdup_or_null(name);
2676 return found;
2679 static struct commit *dwim_reverse_initial(struct rev_info *revs,
2680 const char **name_p)
2683 * DWIM "git blame --reverse ONE -- PATH" as
2684 * "git blame --reverse ONE..HEAD -- PATH" but only do so
2685 * when it makes sense.
2687 struct object *obj;
2688 struct commit *head_commit;
2689 struct object_id head_oid;
2691 if (revs->pending.nr != 1)
2692 return NULL;
2694 /* Is that sole rev a committish? */
2695 obj = revs->pending.objects[0].item;
2696 obj = deref_tag(revs->repo, obj, NULL, 0);
2697 if (!obj || obj->type != OBJ_COMMIT)
2698 return NULL;
2700 /* Do we have HEAD? */
2701 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
2702 return NULL;
2703 head_commit = lookup_commit_reference_gently(revs->repo,
2704 &head_oid, 1);
2705 if (!head_commit)
2706 return NULL;
2708 /* Turn "ONE" into "ONE..HEAD" then */
2709 obj->flags |= UNINTERESTING;
2710 add_pending_object(revs, &head_commit->object, "HEAD");
2712 if (name_p)
2713 *name_p = revs->pending.objects[0].name;
2714 return (struct commit *)obj;
2717 static struct commit *find_single_initial(struct rev_info *revs,
2718 const char **name_p)
2720 int i;
2721 struct commit *found = NULL;
2722 const char *name = NULL;
2725 * There must be one and only one negative commit, and it must be
2726 * the boundary.
2728 for (i = 0; i < revs->pending.nr; i++) {
2729 struct object *obj = revs->pending.objects[i].item;
2730 if (!(obj->flags & UNINTERESTING))
2731 continue;
2732 obj = deref_tag(revs->repo, obj, NULL, 0);
2733 if (!obj || obj->type != OBJ_COMMIT)
2734 die("Non commit %s?", revs->pending.objects[i].name);
2735 if (found)
2736 die("More than one commit to dig up from, %s and %s?",
2737 revs->pending.objects[i].name, name);
2738 found = (struct commit *) obj;
2739 name = revs->pending.objects[i].name;
2742 if (!name)
2743 found = dwim_reverse_initial(revs, &name);
2744 if (!name)
2745 die("No commit to dig up from?");
2747 if (name_p)
2748 *name_p = xstrdup(name);
2749 return found;
2752 void init_scoreboard(struct blame_scoreboard *sb)
2754 memset(sb, 0, sizeof(struct blame_scoreboard));
2755 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
2756 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
2759 void setup_scoreboard(struct blame_scoreboard *sb,
2760 struct blame_origin **orig)
2762 const char *final_commit_name = NULL;
2763 struct blame_origin *o;
2764 struct commit *final_commit = NULL;
2765 enum object_type type;
2767 init_blame_suspects(&blame_suspects);
2769 if (sb->reverse && sb->contents_from)
2770 die(_("--contents and --reverse do not blend well."));
2772 if (!sb->repo)
2773 BUG("repo is NULL");
2775 if (!sb->reverse) {
2776 sb->final = find_single_final(sb->revs, &final_commit_name);
2777 sb->commits.compare = compare_commits_by_commit_date;
2778 } else {
2779 sb->final = find_single_initial(sb->revs, &final_commit_name);
2780 sb->commits.compare = compare_commits_by_reverse_commit_date;
2783 if (sb->reverse && sb->revs->first_parent_only)
2784 sb->revs->children.name = NULL;
2786 if (sb->contents_from || !sb->final) {
2787 struct object_id head_oid, *parent_oid;
2790 * Build a fake commit at the top of the history, when
2791 * (1) "git blame [^A] --path", i.e. with no positive end
2792 * of the history range, in which case we build such
2793 * a fake commit on top of the HEAD to blame in-tree
2794 * modifications.
2795 * (2) "git blame --contents=file [A] -- path", with or
2796 * without positive end of the history range but with
2797 * --contents, in which case we pretend that there is
2798 * a fake commit on top of the positive end (defaulting to
2799 * HEAD) that has the given contents in the path.
2801 if (sb->final) {
2802 parent_oid = &sb->final->object.oid;
2803 } else {
2804 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
2805 die("no such ref: HEAD");
2806 parent_oid = &head_oid;
2809 if (!sb->contents_from)
2810 setup_work_tree();
2812 sb->final = fake_working_tree_commit(sb->repo,
2813 &sb->revs->diffopt,
2814 sb->path, sb->contents_from,
2815 parent_oid);
2816 add_pending_object(sb->revs, &(sb->final->object), ":");
2819 if (sb->reverse && sb->revs->first_parent_only) {
2820 final_commit = find_single_final(sb->revs, NULL);
2821 if (!final_commit)
2822 die(_("--reverse and --first-parent together require specified latest commit"));
2826 * If we have bottom, this will mark the ancestors of the
2827 * bottom commits we would reach while traversing as
2828 * uninteresting.
2830 if (prepare_revision_walk(sb->revs))
2831 die(_("revision walk setup failed"));
2833 if (sb->reverse && sb->revs->first_parent_only) {
2834 struct commit *c = final_commit;
2836 sb->revs->children.name = "children";
2837 while (c->parents &&
2838 !oideq(&c->object.oid, &sb->final->object.oid)) {
2839 struct commit_list *l = xcalloc(1, sizeof(*l));
2841 l->item = c;
2842 if (add_decoration(&sb->revs->children,
2843 &c->parents->item->object, l))
2844 BUG("not unique item in first-parent chain");
2845 c = c->parents->item;
2848 if (!oideq(&c->object.oid, &sb->final->object.oid))
2849 die(_("--reverse --first-parent together require range along first-parent chain"));
2852 if (is_null_oid(&sb->final->object.oid)) {
2853 o = get_blame_suspects(sb->final);
2854 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
2855 sb->final_buf_size = o->file.size;
2857 else {
2858 o = get_origin(sb->final, sb->path);
2859 if (fill_blob_sha1_and_mode(sb->repo, o))
2860 die(_("no such path %s in %s"), sb->path, final_commit_name);
2862 if (sb->revs->diffopt.flags.allow_textconv &&
2863 textconv_object(sb->repo, sb->path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
2864 &sb->final_buf_size))
2866 else
2867 sb->final_buf = repo_read_object_file(the_repository,
2868 &o->blob_oid,
2869 &type,
2870 &sb->final_buf_size);
2872 if (!sb->final_buf)
2873 die(_("cannot read blob %s for path %s"),
2874 oid_to_hex(&o->blob_oid),
2875 sb->path);
2877 sb->num_read_blob++;
2878 prepare_lines(sb);
2880 if (orig)
2881 *orig = o;
2883 free((char *)final_commit_name);
2888 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
2889 long start, long end,
2890 struct blame_origin *o)
2892 struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
2893 new_head->lno = start;
2894 new_head->num_lines = end - start;
2895 new_head->suspect = o;
2896 new_head->s_lno = start;
2897 new_head->next = head;
2898 blame_origin_incref(o);
2899 return new_head;
2902 void setup_blame_bloom_data(struct blame_scoreboard *sb)
2904 struct blame_bloom_data *bd;
2905 struct bloom_filter_settings *bs;
2907 if (!sb->repo->objects->commit_graph)
2908 return;
2910 bs = get_bloom_filter_settings(sb->repo);
2911 if (!bs)
2912 return;
2914 bd = xmalloc(sizeof(struct blame_bloom_data));
2916 bd->settings = bs;
2918 bd->alloc = 4;
2919 bd->nr = 0;
2920 ALLOC_ARRAY(bd->keys, bd->alloc);
2922 add_bloom_key(bd, sb->path);
2924 sb->bloom_data = bd;
2927 void cleanup_scoreboard(struct blame_scoreboard *sb)
2929 if (sb->bloom_data) {
2930 int i;
2931 for (i = 0; i < sb->bloom_data->nr; i++) {
2932 free(sb->bloom_data->keys[i]->hashes);
2933 free(sb->bloom_data->keys[i]);
2935 free(sb->bloom_data->keys);
2936 FREE_AND_NULL(sb->bloom_data);
2938 trace2_data_intmax("blame", sb->repo,
2939 "bloom/queries", bloom_count_queries);
2940 trace2_data_intmax("blame", sb->repo,
2941 "bloom/response-no", bloom_count_no);