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