3 #include "object-store.h"
4 #include "cache-tree.h"
11 #include "commit-slab.h"
13 define_commit_slab(blame_suspects, struct blame_origin *);
14 static struct blame_suspects blame_suspects;
16 struct blame_origin *get_blame_suspects(struct commit *commit)
18 struct blame_origin **result;
20 result = blame_suspects_peek(&blame_suspects, commit);
22 return result ? *result : NULL;
25 static void set_blame_suspects(struct commit *commit, struct blame_origin *origin)
27 *blame_suspects_at(&blame_suspects, commit) = origin;
30 void blame_origin_decref(struct blame_origin *o)
32 if (o && --o->refcnt <= 0) {
33 struct blame_origin *p, *l = NULL;
35 blame_origin_decref(o->previous);
37 /* Should be present exactly once in commit chain */
38 for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) {
43 set_blame_suspects(o->commit, p->next);
48 die("internal error in blame_origin_decref");
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.
58 static struct blame_origin *make_origin(struct commit *commit, const char *path)
60 struct blame_origin *o;
61 FLEX_ALLOC_STR(o, path, path);
64 o->next = get_blame_suspects(commit);
65 set_blame_suspects(commit, o);
70 * Locate an existing origin or create a new one.
71 * This moves the origin to front position in the commit util list.
73 static struct blame_origin *get_origin(struct commit *commit, const char *path)
75 struct blame_origin *o, *l;
77 for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) {
78 if (!strcmp(o->path, path)) {
82 o->next = get_blame_suspects(commit);
83 set_blame_suspects(commit, o);
85 return blame_origin_incref(o);
88 return make_origin(commit, path);
93 static void verify_working_tree_path(struct commit *work_tree, const char *path)
95 struct commit_list *parents;
98 for (parents = work_tree->parents; parents; parents = parents->next) {
99 const struct object_id *commit_oid = &parents->item->object.oid;
100 struct object_id blob_oid;
103 if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
104 oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
108 pos = cache_name_pos(path, strlen(path));
110 ; /* path is in the index */
111 else if (-1 - pos < active_nr &&
112 !strcmp(active_cache[-1 - pos]->name, path))
113 ; /* path is in the index, unmerged */
115 die("no such path '%s' in HEAD", path);
118 static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid)
120 struct commit *parent;
122 parent = lookup_commit_reference(oid);
124 die("no such commit %s", oid_to_hex(oid));
125 return &commit_list_insert(parent, tail)->next;
128 static void append_merge_parents(struct commit_list **tail)
131 struct strbuf line = STRBUF_INIT;
133 merge_head = open(git_path_merge_head(the_repository), O_RDONLY);
134 if (merge_head < 0) {
137 die("cannot open '%s' for reading",
138 git_path_merge_head(the_repository));
141 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
142 struct object_id oid;
143 if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
144 die("unknown line in '%s': %s",
145 git_path_merge_head(the_repository), line.buf);
146 tail = append_parent(tail, &oid);
149 strbuf_release(&line);
153 * This isn't as simple as passing sb->buf and sb->len, because we
154 * want to transfer ownership of the buffer to the commit (so we
157 static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
160 void *buf = strbuf_detach(sb, &len);
161 set_commit_buffer(c, buf, len);
165 * Prepare a dummy commit that represents the work tree (or staged) item.
166 * Note that annotating work tree item never works in the reverse.
168 static struct commit *fake_working_tree_commit(struct diff_options *opt,
170 const char *contents_from)
172 struct commit *commit;
173 struct blame_origin *origin;
174 struct commit_list **parent_tail, *parent;
175 struct object_id head_oid;
176 struct strbuf buf = STRBUF_INIT;
180 struct cache_entry *ce;
182 struct strbuf msg = STRBUF_INIT;
186 commit = alloc_commit_node(the_repository);
187 commit->object.parsed = 1;
189 parent_tail = &commit->parents;
191 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
192 die("no such ref: HEAD");
194 parent_tail = append_parent(parent_tail, &head_oid);
195 append_merge_parents(parent_tail);
196 verify_working_tree_path(commit, path);
198 origin = make_origin(commit, path);
200 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
201 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
202 for (parent = commit->parents; parent; parent = parent->next)
203 strbuf_addf(&msg, "parent %s\n",
204 oid_to_hex(&parent->item->object.oid));
208 "Version of %s from %s\n",
210 (!contents_from ? path :
211 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
212 set_commit_buffer_from_strbuf(commit, &msg);
214 if (!contents_from || strcmp("-", contents_from)) {
216 const char *read_from;
218 unsigned long buf_len;
221 if (stat(contents_from, &st) < 0)
222 die_errno("Cannot stat '%s'", contents_from);
223 read_from = contents_from;
226 if (lstat(path, &st) < 0)
227 die_errno("Cannot lstat '%s'", path);
230 mode = canon_mode(st.st_mode);
232 switch (st.st_mode & S_IFMT) {
234 if (opt->flags.allow_textconv &&
235 textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
236 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
237 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
238 die_errno("cannot open or read '%s'", read_from);
241 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
242 die_errno("cannot readlink '%s'", read_from);
245 die("unsupported file type %s", read_from);
249 /* Reading from stdin */
251 if (strbuf_read(&buf, 0, 0) < 0)
252 die_errno("failed to read from stdin");
254 convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
255 origin->file.ptr = buf.buf;
256 origin->file.size = buf.len;
257 pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
260 * Read the current index, replace the path entry with
261 * origin->blob_sha1 without mucking with its mode or type
262 * bits; we are not going to write this index out -- we just
263 * want to run "diff-index --cached".
270 int pos = cache_name_pos(path, len);
272 mode = active_cache[pos]->ce_mode;
274 /* Let's not bother reading from HEAD tree */
275 mode = S_IFREG | 0644;
277 size = cache_entry_size(len);
278 ce = xcalloc(1, size);
279 oidcpy(&ce->oid, &origin->blob_oid);
280 memcpy(ce->name, path, len);
281 ce->ce_flags = create_ce_flags(0);
282 ce->ce_namelen = len;
283 ce->ce_mode = create_ce_mode(mode);
284 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
286 cache_tree_invalidate_path(&the_index, path);
293 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
294 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
297 xdemitconf_t xecfg = {0};
298 xdemitcb_t ecb = {NULL};
300 xpp.flags = xdl_opts;
301 xecfg.hunk_func = hunk_func;
303 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
307 * Given an origin, prepare mmfile_t structure to be used by the
310 static void fill_origin_blob(struct diff_options *opt,
311 struct blame_origin *o, mmfile_t *file, int *num_read_blob)
314 enum object_type type;
315 unsigned long file_size;
318 if (opt->flags.allow_textconv &&
319 textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size))
322 file->ptr = read_object_file(&o->blob_oid, &type,
324 file->size = file_size;
327 die("Cannot read blob %s for path %s",
328 oid_to_hex(&o->blob_oid),
336 static void drop_origin_blob(struct blame_origin *o)
339 FREE_AND_NULL(o->file.ptr);
344 * Any merge of blames happens on lists of blames that arrived via
345 * different parents in a single suspect. In this case, we want to
346 * sort according to the suspect line numbers as opposed to the final
347 * image line numbers. The function body is somewhat longish because
348 * it avoids unnecessary writes.
351 static struct blame_entry *blame_merge(struct blame_entry *list1,
352 struct blame_entry *list2)
354 struct blame_entry *p1 = list1, *p2 = list2,
362 if (p1->s_lno <= p2->s_lno) {
365 if ((p1 = *tail) == NULL) {
369 } while (p1->s_lno <= p2->s_lno);
375 if ((p2 = *tail) == NULL) {
379 } while (p1->s_lno > p2->s_lno);
383 if ((p1 = *tail) == NULL) {
387 } while (p1->s_lno <= p2->s_lno);
391 static void *get_next_blame(const void *p)
393 return ((struct blame_entry *)p)->next;
396 static void set_next_blame(void *p1, void *p2)
398 ((struct blame_entry *)p1)->next = p2;
402 * Final image line numbers are all different, so we don't need a
403 * three-way comparison here.
406 static int compare_blame_final(const void *p1, const void *p2)
408 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
412 static int compare_blame_suspect(const void *p1, const void *p2)
414 const struct blame_entry *s1 = p1, *s2 = p2;
416 * to allow for collating suspects, we sort according to the
417 * respective pointer value as the primary sorting criterion.
418 * The actual relation is pretty unimportant as long as it
419 * establishes a total order. Comparing as integers gives us
422 if (s1->suspect != s2->suspect)
423 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
424 if (s1->s_lno == s2->s_lno)
426 return s1->s_lno > s2->s_lno ? 1 : -1;
429 void blame_sort_final(struct blame_scoreboard *sb)
431 sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,
432 compare_blame_final);
435 static int compare_commits_by_reverse_commit_date(const void *a,
439 return -compare_commits_by_commit_date(a, b, c);
443 * For debugging -- origin is refcounted, and this asserts that
444 * we do not underflow.
446 static void sanity_check_refcnt(struct blame_scoreboard *sb)
449 struct blame_entry *ent;
451 for (ent = sb->ent; ent; ent = ent->next) {
452 /* Nobody should have zero or negative refcnt */
453 if (ent->suspect->refcnt <= 0) {
454 fprintf(stderr, "%s in %s has negative refcnt %d\n",
456 oid_to_hex(&ent->suspect->commit->object.oid),
457 ent->suspect->refcnt);
462 sb->on_sanity_fail(sb, baa);
466 * If two blame entries that are next to each other came from
467 * contiguous lines in the same origin (i.e. <commit, path> pair),
468 * merge them together.
470 void blame_coalesce(struct blame_scoreboard *sb)
472 struct blame_entry *ent, *next;
474 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
475 if (ent->suspect == next->suspect &&
476 ent->s_lno + ent->num_lines == next->s_lno) {
477 ent->num_lines += next->num_lines;
478 ent->next = next->next;
479 blame_origin_decref(next->suspect);
482 next = ent; /* again */
486 if (sb->debug) /* sanity */
487 sanity_check_refcnt(sb);
491 * Merge the given sorted list of blames into a preexisting origin.
492 * If there were no previous blames to that commit, it is entered into
493 * the commit priority queue of the score board.
496 static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
497 struct blame_entry *sorted)
499 if (porigin->suspects)
500 porigin->suspects = blame_merge(porigin->suspects, sorted);
502 struct blame_origin *o;
503 for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
505 porigin->suspects = sorted;
509 porigin->suspects = sorted;
510 prio_queue_put(&sb->commits, porigin->commit);
515 * Fill the blob_sha1 field of an origin if it hasn't, so that later
516 * call to fill_origin_blob() can use it to locate the data. blob_sha1
517 * for an origin is also used to pass the blame for the entire file to
518 * the parent to detect the case where a child's blob is identical to
519 * that of its parent's.
521 * This also fills origin->mode for corresponding tree path.
523 static int fill_blob_sha1_and_mode(struct blame_origin *origin)
525 if (!is_null_oid(&origin->blob_oid))
527 if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
529 if (oid_object_info(the_repository, &origin->blob_oid, NULL) != OBJ_BLOB)
533 oidclr(&origin->blob_oid);
534 origin->mode = S_IFINVALID;
539 * We have an origin -- check if the same path exists in the
540 * parent and return an origin structure to represent it.
542 static struct blame_origin *find_origin(struct commit *parent,
543 struct blame_origin *origin)
545 struct blame_origin *porigin;
546 struct diff_options diff_opts;
547 const char *paths[2];
549 /* First check any existing origins */
550 for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
551 if (!strcmp(porigin->path, origin->path)) {
553 * The same path between origin and its parent
554 * without renaming -- the most common case.
556 return blame_origin_incref (porigin);
559 /* See if the origin->path is different between parent
560 * and origin first. Most of the time they are the
561 * same and diff-tree is fairly efficient about this.
563 diff_setup(&diff_opts);
564 diff_opts.flags.recursive = 1;
565 diff_opts.detect_rename = 0;
566 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
567 paths[0] = origin->path;
570 parse_pathspec(&diff_opts.pathspec,
571 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
572 PATHSPEC_LITERAL_PATH, "", paths);
573 diff_setup_done(&diff_opts);
575 if (is_null_oid(&origin->commit->object.oid))
576 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
578 diff_tree_oid(get_commit_tree_oid(parent),
579 get_commit_tree_oid(origin->commit),
581 diffcore_std(&diff_opts);
583 if (!diff_queued_diff.nr) {
584 /* The path is the same as parent */
585 porigin = get_origin(parent, origin->path);
586 oidcpy(&porigin->blob_oid, &origin->blob_oid);
587 porigin->mode = origin->mode;
590 * Since origin->path is a pathspec, if the parent
591 * commit had it as a directory, we will see a whole
592 * bunch of deletion of files in the directory that we
596 struct diff_filepair *p = NULL;
597 for (i = 0; i < diff_queued_diff.nr; i++) {
599 p = diff_queued_diff.queue[i];
600 name = p->one->path ? p->one->path : p->two->path;
601 if (!strcmp(name, origin->path))
605 die("internal error in blame::find_origin");
608 die("internal error in blame::find_origin (%c)",
611 porigin = get_origin(parent, origin->path);
612 oidcpy(&porigin->blob_oid, &p->one->oid);
613 porigin->mode = p->one->mode;
617 /* Did not exist in parent, or type changed */
621 diff_flush(&diff_opts);
622 clear_pathspec(&diff_opts.pathspec);
627 * We have an origin -- find the path that corresponds to it in its
628 * parent and return an origin structure to represent it.
630 static struct blame_origin *find_rename(struct commit *parent,
631 struct blame_origin *origin)
633 struct blame_origin *porigin = NULL;
634 struct diff_options diff_opts;
637 diff_setup(&diff_opts);
638 diff_opts.flags.recursive = 1;
639 diff_opts.detect_rename = DIFF_DETECT_RENAME;
640 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
641 diff_opts.single_follow = origin->path;
642 diff_setup_done(&diff_opts);
644 if (is_null_oid(&origin->commit->object.oid))
645 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
647 diff_tree_oid(get_commit_tree_oid(parent),
648 get_commit_tree_oid(origin->commit),
650 diffcore_std(&diff_opts);
652 for (i = 0; i < diff_queued_diff.nr; i++) {
653 struct diff_filepair *p = diff_queued_diff.queue[i];
654 if ((p->status == 'R' || p->status == 'C') &&
655 !strcmp(p->two->path, origin->path)) {
656 porigin = get_origin(parent, p->one->path);
657 oidcpy(&porigin->blob_oid, &p->one->oid);
658 porigin->mode = p->one->mode;
662 diff_flush(&diff_opts);
663 clear_pathspec(&diff_opts.pathspec);
668 * Append a new blame entry to a given output queue.
670 static void add_blame_entry(struct blame_entry ***queue,
671 const struct blame_entry *src)
673 struct blame_entry *e = xmalloc(sizeof(*e));
674 memcpy(e, src, sizeof(*e));
675 blame_origin_incref(e->suspect);
683 * src typically is on-stack; we want to copy the information in it to
684 * a malloced blame_entry that gets added to the given queue. The
685 * origin of dst loses a refcnt.
687 static void dup_entry(struct blame_entry ***queue,
688 struct blame_entry *dst, struct blame_entry *src)
690 blame_origin_incref(src->suspect);
691 blame_origin_decref(dst->suspect);
692 memcpy(dst, src, sizeof(*src));
698 const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
700 return sb->final_buf + sb->lineno[lno];
704 * It is known that lines between tlno to same came from parent, and e
705 * has an overlap with that range. it also is known that parent's
706 * line plno corresponds to e's line tlno.
712 * <------------------>
714 * Split e into potentially three parts; before this chunk, the chunk
715 * to be blamed for the parent, and after that portion.
717 static void split_overlap(struct blame_entry *split,
718 struct blame_entry *e,
719 int tlno, int plno, int same,
720 struct blame_origin *parent)
723 memset(split, 0, sizeof(struct blame_entry [3]));
725 if (e->s_lno < tlno) {
726 /* there is a pre-chunk part not blamed on parent */
727 split[0].suspect = blame_origin_incref(e->suspect);
728 split[0].lno = e->lno;
729 split[0].s_lno = e->s_lno;
730 split[0].num_lines = tlno - e->s_lno;
731 split[1].lno = e->lno + tlno - e->s_lno;
732 split[1].s_lno = plno;
735 split[1].lno = e->lno;
736 split[1].s_lno = plno + (e->s_lno - tlno);
739 if (same < e->s_lno + e->num_lines) {
740 /* there is a post-chunk part not blamed on parent */
741 split[2].suspect = blame_origin_incref(e->suspect);
742 split[2].lno = e->lno + (same - e->s_lno);
743 split[2].s_lno = e->s_lno + (same - e->s_lno);
744 split[2].num_lines = e->s_lno + e->num_lines - same;
745 chunk_end_lno = split[2].lno;
748 chunk_end_lno = e->lno + e->num_lines;
749 split[1].num_lines = chunk_end_lno - split[1].lno;
752 * if it turns out there is nothing to blame the parent for,
753 * forget about the splitting. !split[1].suspect signals this.
755 if (split[1].num_lines < 1)
757 split[1].suspect = blame_origin_incref(parent);
761 * split_overlap() divided an existing blame e into up to three parts
762 * in split. Any assigned blame is moved to queue to
765 static void split_blame(struct blame_entry ***blamed,
766 struct blame_entry ***unblamed,
767 struct blame_entry *split,
768 struct blame_entry *e)
770 if (split[0].suspect && split[2].suspect) {
771 /* The first part (reuse storage for the existing entry e) */
772 dup_entry(unblamed, e, &split[0]);
774 /* The last part -- me */
775 add_blame_entry(unblamed, &split[2]);
777 /* ... and the middle part -- parent */
778 add_blame_entry(blamed, &split[1]);
780 else if (!split[0].suspect && !split[2].suspect)
782 * The parent covers the entire area; reuse storage for
783 * e and replace it with the parent.
785 dup_entry(blamed, e, &split[1]);
786 else if (split[0].suspect) {
787 /* me and then parent */
788 dup_entry(unblamed, e, &split[0]);
789 add_blame_entry(blamed, &split[1]);
792 /* parent and then me */
793 dup_entry(blamed, e, &split[1]);
794 add_blame_entry(unblamed, &split[2]);
799 * After splitting the blame, the origins used by the
800 * on-stack blame_entry should lose one refcnt each.
802 static void decref_split(struct blame_entry *split)
806 for (i = 0; i < 3; i++)
807 blame_origin_decref(split[i].suspect);
811 * reverse_blame reverses the list given in head, appending tail.
812 * That allows us to build lists in reverse order, then reverse them
813 * afterwards. This can be faster than building the list in proper
814 * order right away. The reason is that building in proper order
815 * requires writing a link in the _previous_ element, while building
816 * in reverse order just requires placing the list head into the
820 static struct blame_entry *reverse_blame(struct blame_entry *head,
821 struct blame_entry *tail)
824 struct blame_entry *next = head->next;
833 * Process one hunk from the patch between the current suspect for
834 * blame_entry e and its parent. This first blames any unfinished
835 * entries before the chunk (which is where target and parent start
836 * differing) on the parent, and then splits blame entries at the
837 * start and at the end of the difference region. Since use of -M and
838 * -C options may lead to overlapping/duplicate source line number
839 * ranges, all we can rely on from sorting/merging is the order of the
840 * first suspect line number.
842 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
843 int tlno, int offset, int same,
844 struct blame_origin *parent)
846 struct blame_entry *e = **srcq;
847 struct blame_entry *samep = NULL, *diffp = NULL;
849 while (e && e->s_lno < tlno) {
850 struct blame_entry *next = e->next;
852 * current record starts before differing portion. If
853 * it reaches into it, we need to split it up and
854 * examine the second part separately.
856 if (e->s_lno + e->num_lines > tlno) {
857 /* Move second half to a new record */
858 int len = tlno - e->s_lno;
859 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
860 n->suspect = e->suspect;
861 n->lno = e->lno + len;
862 n->s_lno = e->s_lno + len;
863 n->num_lines = e->num_lines - len;
866 /* Push new record to diffp */
870 blame_origin_decref(e->suspect);
871 /* Pass blame for everything before the differing
872 * chunk to the parent */
873 e->suspect = blame_origin_incref(parent);
880 * As we don't know how much of a common stretch after this
881 * diff will occur, the currently blamed parts are all that we
882 * can assign to the parent for now.
886 **dstq = reverse_blame(samep, **dstq);
887 *dstq = &samep->next;
890 * Prepend the split off portions: everything after e starts
891 * after the blameable portion.
893 e = reverse_blame(diffp, e);
896 * Now retain records on the target while parts are different
901 while (e && e->s_lno < same) {
902 struct blame_entry *next = e->next;
905 * If current record extends into sameness, need to split.
907 if (e->s_lno + e->num_lines > same) {
909 * Move second half to a new record to be
910 * processed by later chunks
912 int len = same - e->s_lno;
913 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
914 n->suspect = blame_origin_incref(e->suspect);
915 n->lno = e->lno + len;
916 n->s_lno = e->s_lno + len;
917 n->num_lines = e->num_lines - len;
920 /* Push new record to samep */
928 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
929 /* Move across elements that are in the unblamable portion */
931 *srcq = &diffp->next;
934 struct blame_chunk_cb_data {
935 struct blame_origin *parent;
937 struct blame_entry **dstq;
938 struct blame_entry **srcq;
941 /* diff chunks are from parent to target */
942 static int blame_chunk_cb(long start_a, long count_a,
943 long start_b, long count_b, void *data)
945 struct blame_chunk_cb_data *d = data;
946 if (start_a - start_b != d->offset)
947 die("internal error in blame::blame_chunk_cb");
948 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
949 start_b + count_b, d->parent);
950 d->offset = start_a + count_a - (start_b + count_b);
955 * We are looking at the origin 'target' and aiming to pass blame
956 * for the lines it is suspected to its parent. Run diff to find
957 * which lines came from parent and pass blame for them.
959 static void pass_blame_to_parent(struct blame_scoreboard *sb,
960 struct blame_origin *target,
961 struct blame_origin *parent)
963 mmfile_t file_p, file_o;
964 struct blame_chunk_cb_data d;
965 struct blame_entry *newdest = NULL;
967 if (!target->suspects)
968 return; /* nothing remains for this target */
972 d.dstq = &newdest; d.srcq = &target->suspects;
974 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
975 fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
978 if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
979 die("unable to generate diff (%s -> %s)",
980 oid_to_hex(&parent->commit->object.oid),
981 oid_to_hex(&target->commit->object.oid));
982 /* The rest are the same as the parent */
983 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
985 queue_blames(sb, parent, newdest);
991 * The lines in blame_entry after splitting blames many times can become
992 * very small and trivial, and at some point it becomes pointless to
993 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
994 * ordinary C program, and it is not worth to say it was copied from
995 * totally unrelated file in the parent.
997 * Compute how trivial the lines in the blame_entry are.
999 unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
1002 const char *cp, *ep;
1008 cp = blame_nth_line(sb, e->lno);
1009 ep = blame_nth_line(sb, e->lno + e->num_lines);
1011 unsigned ch = *((unsigned char *)cp);
1021 * best_so_far[] and potential[] are both a split of an existing blame_entry
1022 * that passes blame to the parent. Maintain best_so_far the best split so
1023 * far, by comparing potential and best_so_far and copying potential into
1024 * bst_so_far as needed.
1026 static void copy_split_if_better(struct blame_scoreboard *sb,
1027 struct blame_entry *best_so_far,
1028 struct blame_entry *potential)
1032 if (!potential[1].suspect)
1034 if (best_so_far[1].suspect) {
1035 if (blame_entry_score(sb, &potential[1]) <
1036 blame_entry_score(sb, &best_so_far[1]))
1040 for (i = 0; i < 3; i++)
1041 blame_origin_incref(potential[i].suspect);
1042 decref_split(best_so_far);
1043 memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
1047 * We are looking at a part of the final image represented by
1048 * ent (tlno and same are offset by ent->s_lno).
1049 * tlno is where we are looking at in the final image.
1050 * up to (but not including) same match preimage.
1051 * plno is where we are looking at in the preimage.
1053 * <-------------- final image ---------------------->
1056 * <---------preimage----->
1059 * All line numbers are 0-based.
1061 static void handle_split(struct blame_scoreboard *sb,
1062 struct blame_entry *ent,
1063 int tlno, int plno, int same,
1064 struct blame_origin *parent,
1065 struct blame_entry *split)
1067 if (ent->num_lines <= tlno)
1070 struct blame_entry potential[3];
1073 split_overlap(potential, ent, tlno, plno, same, parent);
1074 copy_split_if_better(sb, split, potential);
1075 decref_split(potential);
1079 struct handle_split_cb_data {
1080 struct blame_scoreboard *sb;
1081 struct blame_entry *ent;
1082 struct blame_origin *parent;
1083 struct blame_entry *split;
1088 static int handle_split_cb(long start_a, long count_a,
1089 long start_b, long count_b, void *data)
1091 struct handle_split_cb_data *d = data;
1092 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1094 d->plno = start_a + count_a;
1095 d->tlno = start_b + count_b;
1100 * Find the lines from parent that are the same as ent so that
1101 * we can pass blames to it. file_p has the blob contents for
1104 static void find_copy_in_blob(struct blame_scoreboard *sb,
1105 struct blame_entry *ent,
1106 struct blame_origin *parent,
1107 struct blame_entry *split,
1112 struct handle_split_cb_data d;
1114 memset(&d, 0, sizeof(d));
1115 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1117 * Prepare mmfile that contains only the lines in ent.
1119 cp = blame_nth_line(sb, ent->lno);
1120 file_o.ptr = (char *) cp;
1121 file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
1124 * file_o is a part of final image we are annotating.
1125 * file_p partially may match that image.
1127 memset(split, 0, sizeof(struct blame_entry [3]));
1128 if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
1129 die("unable to generate diff (%s)",
1130 oid_to_hex(&parent->commit->object.oid));
1131 /* remainder, if any, all match the preimage */
1132 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1135 /* Move all blame entries from list *source that have a score smaller
1136 * than score_min to the front of list *small.
1137 * Returns a pointer to the link pointing to the old head of the small list.
1140 static struct blame_entry **filter_small(struct blame_scoreboard *sb,
1141 struct blame_entry **small,
1142 struct blame_entry **source,
1145 struct blame_entry *p = *source;
1146 struct blame_entry *oldsmall = *small;
1148 if (blame_entry_score(sb, p) <= score_min) {
1164 * See if lines currently target is suspected for can be attributed to
1167 static void find_move_in_parent(struct blame_scoreboard *sb,
1168 struct blame_entry ***blamed,
1169 struct blame_entry **toosmall,
1170 struct blame_origin *target,
1171 struct blame_origin *parent)
1173 struct blame_entry *e, split[3];
1174 struct blame_entry *unblamed = target->suspects;
1175 struct blame_entry *leftover = NULL;
1179 return; /* nothing remains for this target */
1181 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1185 /* At each iteration, unblamed has a NULL-terminated list of
1186 * entries that have not yet been tested for blame. leftover
1187 * contains the reversed list of entries that have been tested
1188 * without being assignable to the parent.
1191 struct blame_entry **unblamedtail = &unblamed;
1192 struct blame_entry *next;
1193 for (e = unblamed; e; e = next) {
1195 find_copy_in_blob(sb, e, parent, split, &file_p);
1196 if (split[1].suspect &&
1197 sb->move_score < blame_entry_score(sb, &split[1])) {
1198 split_blame(blamed, &unblamedtail, split, e);
1203 decref_split(split);
1205 *unblamedtail = NULL;
1206 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
1208 target->suspects = reverse_blame(leftover, NULL);
1212 struct blame_entry *ent;
1213 struct blame_entry split[3];
1217 * Count the number of entries the target is suspected for,
1218 * and prepare a list of entry and the best split.
1220 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1223 struct blame_entry *e;
1225 struct blame_list *blame_list = NULL;
1227 for (e = unblamed, num_ents = 0; e; e = e->next)
1230 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1231 for (e = unblamed, i = 0; e; e = e->next)
1232 blame_list[i++].ent = e;
1234 *num_ents_p = num_ents;
1239 * For lines target is suspected for, see if we can find code movement
1240 * across file boundary from the parent commit. porigin is the path
1241 * in the parent we already tried.
1243 static void find_copy_in_parent(struct blame_scoreboard *sb,
1244 struct blame_entry ***blamed,
1245 struct blame_entry **toosmall,
1246 struct blame_origin *target,
1247 struct commit *parent,
1248 struct blame_origin *porigin,
1251 struct diff_options diff_opts;
1253 struct blame_list *blame_list;
1255 struct blame_entry *unblamed = target->suspects;
1256 struct blame_entry *leftover = NULL;
1259 return; /* nothing remains for this target */
1261 diff_setup(&diff_opts);
1262 diff_opts.flags.recursive = 1;
1263 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1265 diff_setup_done(&diff_opts);
1267 /* Try "find copies harder" on new path if requested;
1268 * we do not want to use diffcore_rename() actually to
1269 * match things up; find_copies_harder is set only to
1270 * force diff_tree_oid() to feed all filepairs to diff_queue,
1271 * and this code needs to be after diff_setup_done(), which
1272 * usually makes find-copies-harder imply copy detection.
1274 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1275 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1276 && (!porigin || strcmp(target->path, porigin->path))))
1277 diff_opts.flags.find_copies_harder = 1;
1279 if (is_null_oid(&target->commit->object.oid))
1280 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1282 diff_tree_oid(get_commit_tree_oid(parent),
1283 get_commit_tree_oid(target->commit),
1286 if (!diff_opts.flags.find_copies_harder)
1287 diffcore_std(&diff_opts);
1290 struct blame_entry **unblamedtail = &unblamed;
1291 blame_list = setup_blame_list(unblamed, &num_ents);
1293 for (i = 0; i < diff_queued_diff.nr; i++) {
1294 struct diff_filepair *p = diff_queued_diff.queue[i];
1295 struct blame_origin *norigin;
1297 struct blame_entry potential[3];
1299 if (!DIFF_FILE_VALID(p->one))
1300 continue; /* does not exist in parent */
1301 if (S_ISGITLINK(p->one->mode))
1302 continue; /* ignore git links */
1303 if (porigin && !strcmp(p->one->path, porigin->path))
1304 /* find_move already dealt with this path */
1307 norigin = get_origin(parent, p->one->path);
1308 oidcpy(&norigin->blob_oid, &p->one->oid);
1309 norigin->mode = p->one->mode;
1310 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1314 for (j = 0; j < num_ents; j++) {
1315 find_copy_in_blob(sb, blame_list[j].ent,
1316 norigin, potential, &file_p);
1317 copy_split_if_better(sb, blame_list[j].split,
1319 decref_split(potential);
1321 blame_origin_decref(norigin);
1324 for (j = 0; j < num_ents; j++) {
1325 struct blame_entry *split = blame_list[j].split;
1326 if (split[1].suspect &&
1327 sb->copy_score < blame_entry_score(sb, &split[1])) {
1328 split_blame(blamed, &unblamedtail, split,
1331 blame_list[j].ent->next = leftover;
1332 leftover = blame_list[j].ent;
1334 decref_split(split);
1337 *unblamedtail = NULL;
1338 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
1340 target->suspects = reverse_blame(leftover, NULL);
1341 diff_flush(&diff_opts);
1342 clear_pathspec(&diff_opts.pathspec);
1346 * The blobs of origin and porigin exactly match, so everything
1347 * origin is suspected for can be blamed on the parent.
1349 static void pass_whole_blame(struct blame_scoreboard *sb,
1350 struct blame_origin *origin, struct blame_origin *porigin)
1352 struct blame_entry *e, *suspects;
1354 if (!porigin->file.ptr && origin->file.ptr) {
1355 /* Steal its file */
1356 porigin->file = origin->file;
1357 origin->file.ptr = NULL;
1359 suspects = origin->suspects;
1360 origin->suspects = NULL;
1361 for (e = suspects; e; e = e->next) {
1362 blame_origin_incref(porigin);
1363 blame_origin_decref(e->suspect);
1364 e->suspect = porigin;
1366 queue_blames(sb, porigin, suspects);
1370 * We pass blame from the current commit to its parents. We keep saying
1371 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1372 * exonerate ourselves.
1374 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
1378 if (revs->first_parent_only &&
1380 commit->parents->next) {
1381 free_commit_list(commit->parents->next);
1382 commit->parents->next = NULL;
1384 return commit->parents;
1386 return lookup_decoration(&revs->children, &commit->object);
1389 static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
1391 struct commit_list *l = first_scapegoat(revs, commit, reverse);
1392 return commit_list_count(l);
1395 /* Distribute collected unsorted blames to the respected sorted lists
1396 * in the various origins.
1398 static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
1400 blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,
1401 compare_blame_suspect);
1404 struct blame_origin *porigin = blamed->suspect;
1405 struct blame_entry *suspects = NULL;
1407 struct blame_entry *next = blamed->next;
1408 blamed->next = suspects;
1411 } while (blamed && blamed->suspect == porigin);
1412 suspects = reverse_blame(suspects, NULL);
1413 queue_blames(sb, porigin, suspects);
1419 static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
1421 struct rev_info *revs = sb->revs;
1422 int i, pass, num_sg;
1423 struct commit *commit = origin->commit;
1424 struct commit_list *sg;
1425 struct blame_origin *sg_buf[MAXSG];
1426 struct blame_origin *porigin, **sg_origin = sg_buf;
1427 struct blame_entry *toosmall = NULL;
1428 struct blame_entry *blames, **blametail = &blames;
1430 num_sg = num_scapegoats(revs, commit, sb->reverse);
1433 else if (num_sg < ARRAY_SIZE(sg_buf))
1434 memset(sg_buf, 0, sizeof(sg_buf));
1436 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1439 * The first pass looks for unrenamed path to optimize for
1440 * common cases, then we look for renames in the second pass.
1442 for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
1443 struct blame_origin *(*find)(struct commit *, struct blame_origin *);
1444 find = pass ? find_rename : find_origin;
1446 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1448 sg = sg->next, i++) {
1449 struct commit *p = sg->item;
1454 if (parse_commit(p))
1456 porigin = find(p, origin);
1459 if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) {
1460 pass_whole_blame(sb, origin, porigin);
1461 blame_origin_decref(porigin);
1464 for (j = same = 0; j < i; j++)
1466 !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
1471 sg_origin[i] = porigin;
1473 blame_origin_decref(porigin);
1478 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1480 sg = sg->next, i++) {
1481 struct blame_origin *porigin = sg_origin[i];
1484 if (!origin->previous) {
1485 blame_origin_incref(porigin);
1486 origin->previous = porigin;
1488 pass_blame_to_parent(sb, origin, porigin);
1489 if (!origin->suspects)
1494 * Optionally find moves in parents' files.
1496 if (opt & PICKAXE_BLAME_MOVE) {
1497 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
1498 if (origin->suspects) {
1499 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1501 sg = sg->next, i++) {
1502 struct blame_origin *porigin = sg_origin[i];
1505 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1506 if (!origin->suspects)
1513 * Optionally find copies from parents' files.
1515 if (opt & PICKAXE_BLAME_COPY) {
1516 if (sb->copy_score > sb->move_score)
1517 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1518 else if (sb->copy_score < sb->move_score) {
1519 origin->suspects = blame_merge(origin->suspects, toosmall);
1521 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1523 if (!origin->suspects)
1526 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1528 sg = sg->next, i++) {
1529 struct blame_origin *porigin = sg_origin[i];
1530 find_copy_in_parent(sb, &blametail, &toosmall,
1531 origin, sg->item, porigin, opt);
1532 if (!origin->suspects)
1539 distribute_blame(sb, blames);
1541 * prepend toosmall to origin->suspects
1543 * There is no point in sorting: this ends up on a big
1544 * unsorted list in the caller anyway.
1547 struct blame_entry **tail = &toosmall;
1549 tail = &(*tail)->next;
1550 *tail = origin->suspects;
1551 origin->suspects = toosmall;
1553 for (i = 0; i < num_sg; i++) {
1555 drop_origin_blob(sg_origin[i]);
1556 blame_origin_decref(sg_origin[i]);
1559 drop_origin_blob(origin);
1560 if (sg_buf != sg_origin)
1565 * The main loop -- while we have blobs with lines whose true origin
1566 * is still unknown, pick one blob, and allow its lines to pass blames
1567 * to its parents. */
1568 void assign_blame(struct blame_scoreboard *sb, int opt)
1570 struct rev_info *revs = sb->revs;
1571 struct commit *commit = prio_queue_get(&sb->commits);
1574 struct blame_entry *ent;
1575 struct blame_origin *suspect = get_blame_suspects(commit);
1577 /* find one suspect to break down */
1578 while (suspect && !suspect->suspects)
1579 suspect = suspect->next;
1582 commit = prio_queue_get(&sb->commits);
1586 assert(commit == suspect->commit);
1589 * We will use this suspect later in the loop,
1590 * so hold onto it in the meantime.
1592 blame_origin_incref(suspect);
1593 parse_commit(commit);
1595 (!(commit->object.flags & UNINTERESTING) &&
1596 !(revs->max_age != -1 && commit->date < revs->max_age)))
1597 pass_blame(sb, suspect, opt);
1599 commit->object.flags |= UNINTERESTING;
1600 if (commit->object.parsed)
1601 mark_parents_uninteresting(commit);
1603 /* treat root commit as boundary */
1604 if (!commit->parents && !sb->show_root)
1605 commit->object.flags |= UNINTERESTING;
1607 /* Take responsibility for the remaining entries */
1608 ent = suspect->suspects;
1610 suspect->guilty = 1;
1612 struct blame_entry *next = ent->next;
1613 if (sb->found_guilty_entry)
1614 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
1619 ent->next = sb->ent;
1620 sb->ent = suspect->suspects;
1621 suspect->suspects = NULL;
1625 blame_origin_decref(suspect);
1627 if (sb->debug) /* sanity */
1628 sanity_check_refcnt(sb);
1632 static const char *get_next_line(const char *start, const char *end)
1634 const char *nl = memchr(start, '\n', end - start);
1635 return nl ? nl + 1 : end;
1639 * To allow quick access to the contents of nth line in the
1640 * final image, prepare an index in the scoreboard.
1642 static int prepare_lines(struct blame_scoreboard *sb)
1644 const char *buf = sb->final_buf;
1645 unsigned long len = sb->final_buf_size;
1646 const char *end = buf + len;
1651 for (p = buf; p < end; p = get_next_line(p, end))
1654 ALLOC_ARRAY(sb->lineno, num + 1);
1655 lineno = sb->lineno;
1657 for (p = buf; p < end; p = get_next_line(p, end))
1658 *lineno++ = p - buf;
1662 sb->num_lines = num;
1663 return sb->num_lines;
1666 static struct commit *find_single_final(struct rev_info *revs,
1667 const char **name_p)
1670 struct commit *found = NULL;
1671 const char *name = NULL;
1673 for (i = 0; i < revs->pending.nr; i++) {
1674 struct object *obj = revs->pending.objects[i].item;
1675 if (obj->flags & UNINTERESTING)
1677 obj = deref_tag(obj, NULL, 0);
1678 if (obj->type != OBJ_COMMIT)
1679 die("Non commit %s?", revs->pending.objects[i].name);
1681 die("More than one commit to dig from %s and %s?",
1682 revs->pending.objects[i].name, name);
1683 found = (struct commit *)obj;
1684 name = revs->pending.objects[i].name;
1687 *name_p = xstrdup_or_null(name);
1691 static struct commit *dwim_reverse_initial(struct rev_info *revs,
1692 const char **name_p)
1695 * DWIM "git blame --reverse ONE -- PATH" as
1696 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1697 * when it makes sense.
1700 struct commit *head_commit;
1701 struct object_id head_oid;
1703 if (revs->pending.nr != 1)
1706 /* Is that sole rev a committish? */
1707 obj = revs->pending.objects[0].item;
1708 obj = deref_tag(obj, NULL, 0);
1709 if (obj->type != OBJ_COMMIT)
1712 /* Do we have HEAD? */
1713 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
1715 head_commit = lookup_commit_reference_gently(&head_oid, 1);
1719 /* Turn "ONE" into "ONE..HEAD" then */
1720 obj->flags |= UNINTERESTING;
1721 add_pending_object(revs, &head_commit->object, "HEAD");
1724 *name_p = revs->pending.objects[0].name;
1725 return (struct commit *)obj;
1728 static struct commit *find_single_initial(struct rev_info *revs,
1729 const char **name_p)
1732 struct commit *found = NULL;
1733 const char *name = NULL;
1736 * There must be one and only one negative commit, and it must be
1739 for (i = 0; i < revs->pending.nr; i++) {
1740 struct object *obj = revs->pending.objects[i].item;
1741 if (!(obj->flags & UNINTERESTING))
1743 obj = deref_tag(obj, NULL, 0);
1744 if (obj->type != OBJ_COMMIT)
1745 die("Non commit %s?", revs->pending.objects[i].name);
1747 die("More than one commit to dig up from, %s and %s?",
1748 revs->pending.objects[i].name, name);
1749 found = (struct commit *) obj;
1750 name = revs->pending.objects[i].name;
1754 found = dwim_reverse_initial(revs, &name);
1756 die("No commit to dig up from?");
1759 *name_p = xstrdup(name);
1763 void init_scoreboard(struct blame_scoreboard *sb)
1765 memset(sb, 0, sizeof(struct blame_scoreboard));
1766 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
1767 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
1770 void setup_scoreboard(struct blame_scoreboard *sb, const char *path, struct blame_origin **orig)
1772 const char *final_commit_name = NULL;
1773 struct blame_origin *o;
1774 struct commit *final_commit = NULL;
1775 enum object_type type;
1777 init_blame_suspects(&blame_suspects);
1779 if (sb->reverse && sb->contents_from)
1780 die(_("--contents and --reverse do not blend well."));
1783 sb->final = find_single_final(sb->revs, &final_commit_name);
1784 sb->commits.compare = compare_commits_by_commit_date;
1786 sb->final = find_single_initial(sb->revs, &final_commit_name);
1787 sb->commits.compare = compare_commits_by_reverse_commit_date;
1790 if (sb->final && sb->contents_from)
1791 die(_("cannot use --contents with final commit object name"));
1793 if (sb->reverse && sb->revs->first_parent_only)
1794 sb->revs->children.name = NULL;
1798 * "--not A B -- path" without anything positive;
1799 * do not default to HEAD, but use the working tree
1803 sb->final = fake_working_tree_commit(&sb->revs->diffopt,
1804 path, sb->contents_from);
1805 add_pending_object(sb->revs, &(sb->final->object), ":");
1808 if (sb->reverse && sb->revs->first_parent_only) {
1809 final_commit = find_single_final(sb->revs, NULL);
1811 die(_("--reverse and --first-parent together require specified latest commit"));
1815 * If we have bottom, this will mark the ancestors of the
1816 * bottom commits we would reach while traversing as
1819 if (prepare_revision_walk(sb->revs))
1820 die(_("revision walk setup failed"));
1822 if (sb->reverse && sb->revs->first_parent_only) {
1823 struct commit *c = final_commit;
1825 sb->revs->children.name = "children";
1826 while (c->parents &&
1827 oidcmp(&c->object.oid, &sb->final->object.oid)) {
1828 struct commit_list *l = xcalloc(1, sizeof(*l));
1831 if (add_decoration(&sb->revs->children,
1832 &c->parents->item->object, l))
1833 BUG("not unique item in first-parent chain");
1834 c = c->parents->item;
1837 if (oidcmp(&c->object.oid, &sb->final->object.oid))
1838 die(_("--reverse --first-parent together require range along first-parent chain"));
1841 if (is_null_oid(&sb->final->object.oid)) {
1842 o = get_blame_suspects(sb->final);
1843 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
1844 sb->final_buf_size = o->file.size;
1847 o = get_origin(sb->final, path);
1848 if (fill_blob_sha1_and_mode(o))
1849 die(_("no such path %s in %s"), path, final_commit_name);
1851 if (sb->revs->diffopt.flags.allow_textconv &&
1852 textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
1853 &sb->final_buf_size))
1856 sb->final_buf = read_object_file(&o->blob_oid, &type,
1857 &sb->final_buf_size);
1860 die(_("cannot read blob %s for path %s"),
1861 oid_to_hex(&o->blob_oid),
1864 sb->num_read_blob++;
1870 free((char *)final_commit_name);
1875 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
1876 long start, long end,
1877 struct blame_origin *o)
1879 struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
1880 new_head->lno = start;
1881 new_head->num_lines = end - start;
1882 new_head->suspect = o;
1883 new_head->s_lno = start;
1884 new_head->next = head;
1885 blame_origin_incref(o);