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 repository *r,
94 struct commit *work_tree, const char *path)
96 struct commit_list *parents;
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;
104 if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) &&
105 oid_object_info(r, &blob_oid, NULL) == OBJ_BLOB)
109 pos = index_name_pos(r->index, path, strlen(path));
111 ; /* path is in the index */
112 else if (-1 - pos < r->index->cache_nr &&
113 !strcmp(r->index->cache[-1 - pos]->name, path))
114 ; /* path is in the index, unmerged */
116 die("no such path '%s' in HEAD", path);
119 static struct commit_list **append_parent(struct repository *r,
120 struct commit_list **tail,
121 const struct object_id *oid)
123 struct commit *parent;
125 parent = lookup_commit_reference(r, oid);
127 die("no such commit %s", oid_to_hex(oid));
128 return &commit_list_insert(parent, tail)->next;
131 static void append_merge_parents(struct repository *r,
132 struct commit_list **tail)
135 struct strbuf line = STRBUF_INIT;
137 merge_head = open(git_path_merge_head(r), O_RDONLY);
138 if (merge_head < 0) {
141 die("cannot open '%s' for reading",
142 git_path_merge_head(r));
145 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
146 struct object_id oid;
147 if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
148 die("unknown line in '%s': %s",
149 git_path_merge_head(r), line.buf);
150 tail = append_parent(r, tail, &oid);
153 strbuf_release(&line);
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
161 static void set_commit_buffer_from_strbuf(struct repository *r,
166 void *buf = strbuf_detach(sb, &len);
167 set_commit_buffer(r, c, buf, len);
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.
174 static struct commit *fake_working_tree_commit(struct repository *r,
175 struct diff_options *opt,
177 const char *contents_from)
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;
187 struct cache_entry *ce;
189 struct strbuf msg = STRBUF_INIT;
191 read_index(r->index);
193 commit = alloc_commit_node(r);
194 commit->object.parsed = 1;
196 parent_tail = &commit->parents;
198 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
199 die("no such ref: HEAD");
201 parent_tail = append_parent(r, parent_tail, &head_oid);
202 append_merge_parents(r, parent_tail);
203 verify_working_tree_path(r, commit, path);
205 origin = make_origin(commit, path);
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));
215 "Version of %s from %s\n",
217 (!contents_from ? path :
218 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
219 set_commit_buffer_from_strbuf(r, commit, &msg);
221 if (!contents_from || strcmp("-", contents_from)) {
223 const char *read_from;
225 unsigned long buf_len;
228 if (stat(contents_from, &st) < 0)
229 die_errno("Cannot stat '%s'", contents_from);
230 read_from = contents_from;
233 if (lstat(path, &st) < 0)
234 die_errno("Cannot lstat '%s'", path);
237 mode = canon_mode(st.st_mode);
239 switch (st.st_mode & S_IFMT) {
241 if (opt->flags.allow_textconv &&
242 textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
243 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
244 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
245 die_errno("cannot open or read '%s'", read_from);
248 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
249 die_errno("cannot readlink '%s'", read_from);
252 die("unsupported file type %s", read_from);
256 /* Reading from stdin */
258 if (strbuf_read(&buf, 0, 0) < 0)
259 die_errno("failed to read from stdin");
261 convert_to_git(r->index, path, buf.buf, buf.len, &buf, 0);
262 origin->file.ptr = buf.buf;
263 origin->file.size = buf.len;
264 pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid);
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".
272 discard_index(r->index);
273 read_index(r->index);
277 int pos = index_name_pos(r->index, path, len);
279 mode = r->index->cache[pos]->ce_mode;
281 /* Let's not bother reading from HEAD tree */
282 mode = S_IFREG | 0644;
284 ce = make_empty_cache_entry(r->index, len);
285 oidcpy(&ce->oid, &origin->blob_oid);
286 memcpy(ce->name, path, len);
287 ce->ce_flags = create_ce_flags(0);
288 ce->ce_namelen = len;
289 ce->ce_mode = create_ce_mode(mode);
290 add_index_entry(r->index, ce,
291 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
293 cache_tree_invalidate_path(r->index, path);
300 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
301 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
304 xdemitconf_t xecfg = {0};
305 xdemitcb_t ecb = {NULL};
307 xpp.flags = xdl_opts;
308 xecfg.hunk_func = hunk_func;
310 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
314 * Given an origin, prepare mmfile_t structure to be used by the
317 static void fill_origin_blob(struct diff_options *opt,
318 struct blame_origin *o, mmfile_t *file, int *num_read_blob)
321 enum object_type type;
322 unsigned long file_size;
325 if (opt->flags.allow_textconv &&
326 textconv_object(opt->repo, o->path, o->mode,
327 &o->blob_oid, 1, &file->ptr, &file_size))
330 file->ptr = read_object_file(&o->blob_oid, &type,
332 file->size = file_size;
335 die("Cannot read blob %s for path %s",
336 oid_to_hex(&o->blob_oid),
344 static void drop_origin_blob(struct blame_origin *o)
346 FREE_AND_NULL(o->file.ptr);
350 * Any merge of blames happens on lists of blames that arrived via
351 * different parents in a single suspect. In this case, we want to
352 * sort according to the suspect line numbers as opposed to the final
353 * image line numbers. The function body is somewhat longish because
354 * it avoids unnecessary writes.
357 static struct blame_entry *blame_merge(struct blame_entry *list1,
358 struct blame_entry *list2)
360 struct blame_entry *p1 = list1, *p2 = list2,
368 if (p1->s_lno <= p2->s_lno) {
371 if ((p1 = *tail) == NULL) {
375 } while (p1->s_lno <= p2->s_lno);
381 if ((p2 = *tail) == NULL) {
385 } while (p1->s_lno > p2->s_lno);
389 if ((p1 = *tail) == NULL) {
393 } while (p1->s_lno <= p2->s_lno);
397 static void *get_next_blame(const void *p)
399 return ((struct blame_entry *)p)->next;
402 static void set_next_blame(void *p1, void *p2)
404 ((struct blame_entry *)p1)->next = p2;
408 * Final image line numbers are all different, so we don't need a
409 * three-way comparison here.
412 static int compare_blame_final(const void *p1, const void *p2)
414 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
418 static int compare_blame_suspect(const void *p1, const void *p2)
420 const struct blame_entry *s1 = p1, *s2 = p2;
422 * to allow for collating suspects, we sort according to the
423 * respective pointer value as the primary sorting criterion.
424 * The actual relation is pretty unimportant as long as it
425 * establishes a total order. Comparing as integers gives us
428 if (s1->suspect != s2->suspect)
429 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
430 if (s1->s_lno == s2->s_lno)
432 return s1->s_lno > s2->s_lno ? 1 : -1;
435 void blame_sort_final(struct blame_scoreboard *sb)
437 sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame,
438 compare_blame_final);
441 static int compare_commits_by_reverse_commit_date(const void *a,
445 return -compare_commits_by_commit_date(a, b, c);
449 * For debugging -- origin is refcounted, and this asserts that
450 * we do not underflow.
452 static void sanity_check_refcnt(struct blame_scoreboard *sb)
455 struct blame_entry *ent;
457 for (ent = sb->ent; ent; ent = ent->next) {
458 /* Nobody should have zero or negative refcnt */
459 if (ent->suspect->refcnt <= 0) {
460 fprintf(stderr, "%s in %s has negative refcnt %d\n",
462 oid_to_hex(&ent->suspect->commit->object.oid),
463 ent->suspect->refcnt);
468 sb->on_sanity_fail(sb, baa);
472 * If two blame entries that are next to each other came from
473 * contiguous lines in the same origin (i.e. <commit, path> pair),
474 * merge them together.
476 void blame_coalesce(struct blame_scoreboard *sb)
478 struct blame_entry *ent, *next;
480 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
481 if (ent->suspect == next->suspect &&
482 ent->s_lno + ent->num_lines == next->s_lno) {
483 ent->num_lines += next->num_lines;
484 ent->next = next->next;
485 blame_origin_decref(next->suspect);
488 next = ent; /* again */
492 if (sb->debug) /* sanity */
493 sanity_check_refcnt(sb);
497 * Merge the given sorted list of blames into a preexisting origin.
498 * If there were no previous blames to that commit, it is entered into
499 * the commit priority queue of the score board.
502 static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin,
503 struct blame_entry *sorted)
505 if (porigin->suspects)
506 porigin->suspects = blame_merge(porigin->suspects, sorted);
508 struct blame_origin *o;
509 for (o = get_blame_suspects(porigin->commit); o; o = o->next) {
511 porigin->suspects = sorted;
515 porigin->suspects = sorted;
516 prio_queue_put(&sb->commits, porigin->commit);
521 * Fill the blob_sha1 field of an origin if it hasn't, so that later
522 * call to fill_origin_blob() can use it to locate the data. blob_sha1
523 * for an origin is also used to pass the blame for the entire file to
524 * the parent to detect the case where a child's blob is identical to
525 * that of its parent's.
527 * This also fills origin->mode for corresponding tree path.
529 static int fill_blob_sha1_and_mode(struct repository *r,
530 struct blame_origin *origin)
532 if (!is_null_oid(&origin->blob_oid))
534 if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode))
536 if (oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB)
540 oidclr(&origin->blob_oid);
541 origin->mode = S_IFINVALID;
546 * We have an origin -- check if the same path exists in the
547 * parent and return an origin structure to represent it.
549 static struct blame_origin *find_origin(struct repository *r,
550 struct commit *parent,
551 struct blame_origin *origin)
553 struct blame_origin *porigin;
554 struct diff_options diff_opts;
555 const char *paths[2];
557 /* First check any existing origins */
558 for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next)
559 if (!strcmp(porigin->path, origin->path)) {
561 * The same path between origin and its parent
562 * without renaming -- the most common case.
564 return blame_origin_incref (porigin);
567 /* See if the origin->path is different between parent
568 * and origin first. Most of the time they are the
569 * same and diff-tree is fairly efficient about this.
571 repo_diff_setup(r, &diff_opts);
572 diff_opts.flags.recursive = 1;
573 diff_opts.detect_rename = 0;
574 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
575 paths[0] = origin->path;
578 parse_pathspec(&diff_opts.pathspec,
579 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
580 PATHSPEC_LITERAL_PATH, "", paths);
581 diff_setup_done(&diff_opts);
583 if (is_null_oid(&origin->commit->object.oid))
584 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
586 diff_tree_oid(get_commit_tree_oid(parent),
587 get_commit_tree_oid(origin->commit),
589 diffcore_std(&diff_opts);
591 if (!diff_queued_diff.nr) {
592 /* The path is the same as parent */
593 porigin = get_origin(parent, origin->path);
594 oidcpy(&porigin->blob_oid, &origin->blob_oid);
595 porigin->mode = origin->mode;
598 * Since origin->path is a pathspec, if the parent
599 * commit had it as a directory, we will see a whole
600 * bunch of deletion of files in the directory that we
604 struct diff_filepair *p = NULL;
605 for (i = 0; i < diff_queued_diff.nr; i++) {
607 p = diff_queued_diff.queue[i];
608 name = p->one->path ? p->one->path : p->two->path;
609 if (!strcmp(name, origin->path))
613 die("internal error in blame::find_origin");
616 die("internal error in blame::find_origin (%c)",
619 porigin = get_origin(parent, origin->path);
620 oidcpy(&porigin->blob_oid, &p->one->oid);
621 porigin->mode = p->one->mode;
625 /* Did not exist in parent, or type changed */
629 diff_flush(&diff_opts);
630 clear_pathspec(&diff_opts.pathspec);
635 * We have an origin -- find the path that corresponds to it in its
636 * parent and return an origin structure to represent it.
638 static struct blame_origin *find_rename(struct repository *r,
639 struct commit *parent,
640 struct blame_origin *origin)
642 struct blame_origin *porigin = NULL;
643 struct diff_options diff_opts;
646 repo_diff_setup(r, &diff_opts);
647 diff_opts.flags.recursive = 1;
648 diff_opts.detect_rename = DIFF_DETECT_RENAME;
649 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
650 diff_opts.single_follow = origin->path;
651 diff_setup_done(&diff_opts);
653 if (is_null_oid(&origin->commit->object.oid))
654 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
656 diff_tree_oid(get_commit_tree_oid(parent),
657 get_commit_tree_oid(origin->commit),
659 diffcore_std(&diff_opts);
661 for (i = 0; i < diff_queued_diff.nr; i++) {
662 struct diff_filepair *p = diff_queued_diff.queue[i];
663 if ((p->status == 'R' || p->status == 'C') &&
664 !strcmp(p->two->path, origin->path)) {
665 porigin = get_origin(parent, p->one->path);
666 oidcpy(&porigin->blob_oid, &p->one->oid);
667 porigin->mode = p->one->mode;
671 diff_flush(&diff_opts);
672 clear_pathspec(&diff_opts.pathspec);
677 * Append a new blame entry to a given output queue.
679 static void add_blame_entry(struct blame_entry ***queue,
680 const struct blame_entry *src)
682 struct blame_entry *e = xmalloc(sizeof(*e));
683 memcpy(e, src, sizeof(*e));
684 blame_origin_incref(e->suspect);
692 * src typically is on-stack; we want to copy the information in it to
693 * a malloced blame_entry that gets added to the given queue. The
694 * origin of dst loses a refcnt.
696 static void dup_entry(struct blame_entry ***queue,
697 struct blame_entry *dst, struct blame_entry *src)
699 blame_origin_incref(src->suspect);
700 blame_origin_decref(dst->suspect);
701 memcpy(dst, src, sizeof(*src));
707 const char *blame_nth_line(struct blame_scoreboard *sb, long lno)
709 return sb->final_buf + sb->lineno[lno];
713 * It is known that lines between tlno to same came from parent, and e
714 * has an overlap with that range. it also is known that parent's
715 * line plno corresponds to e's line tlno.
721 * <------------------>
723 * Split e into potentially three parts; before this chunk, the chunk
724 * to be blamed for the parent, and after that portion.
726 static void split_overlap(struct blame_entry *split,
727 struct blame_entry *e,
728 int tlno, int plno, int same,
729 struct blame_origin *parent)
732 memset(split, 0, sizeof(struct blame_entry [3]));
734 if (e->s_lno < tlno) {
735 /* there is a pre-chunk part not blamed on parent */
736 split[0].suspect = blame_origin_incref(e->suspect);
737 split[0].lno = e->lno;
738 split[0].s_lno = e->s_lno;
739 split[0].num_lines = tlno - e->s_lno;
740 split[1].lno = e->lno + tlno - e->s_lno;
741 split[1].s_lno = plno;
744 split[1].lno = e->lno;
745 split[1].s_lno = plno + (e->s_lno - tlno);
748 if (same < e->s_lno + e->num_lines) {
749 /* there is a post-chunk part not blamed on parent */
750 split[2].suspect = blame_origin_incref(e->suspect);
751 split[2].lno = e->lno + (same - e->s_lno);
752 split[2].s_lno = e->s_lno + (same - e->s_lno);
753 split[2].num_lines = e->s_lno + e->num_lines - same;
754 chunk_end_lno = split[2].lno;
757 chunk_end_lno = e->lno + e->num_lines;
758 split[1].num_lines = chunk_end_lno - split[1].lno;
761 * if it turns out there is nothing to blame the parent for,
762 * forget about the splitting. !split[1].suspect signals this.
764 if (split[1].num_lines < 1)
766 split[1].suspect = blame_origin_incref(parent);
770 * split_overlap() divided an existing blame e into up to three parts
771 * in split. Any assigned blame is moved to queue to
774 static void split_blame(struct blame_entry ***blamed,
775 struct blame_entry ***unblamed,
776 struct blame_entry *split,
777 struct blame_entry *e)
779 if (split[0].suspect && split[2].suspect) {
780 /* The first part (reuse storage for the existing entry e) */
781 dup_entry(unblamed, e, &split[0]);
783 /* The last part -- me */
784 add_blame_entry(unblamed, &split[2]);
786 /* ... and the middle part -- parent */
787 add_blame_entry(blamed, &split[1]);
789 else if (!split[0].suspect && !split[2].suspect)
791 * The parent covers the entire area; reuse storage for
792 * e and replace it with the parent.
794 dup_entry(blamed, e, &split[1]);
795 else if (split[0].suspect) {
796 /* me and then parent */
797 dup_entry(unblamed, e, &split[0]);
798 add_blame_entry(blamed, &split[1]);
801 /* parent and then me */
802 dup_entry(blamed, e, &split[1]);
803 add_blame_entry(unblamed, &split[2]);
808 * After splitting the blame, the origins used by the
809 * on-stack blame_entry should lose one refcnt each.
811 static void decref_split(struct blame_entry *split)
815 for (i = 0; i < 3; i++)
816 blame_origin_decref(split[i].suspect);
820 * reverse_blame reverses the list given in head, appending tail.
821 * That allows us to build lists in reverse order, then reverse them
822 * afterwards. This can be faster than building the list in proper
823 * order right away. The reason is that building in proper order
824 * requires writing a link in the _previous_ element, while building
825 * in reverse order just requires placing the list head into the
829 static struct blame_entry *reverse_blame(struct blame_entry *head,
830 struct blame_entry *tail)
833 struct blame_entry *next = head->next;
842 * Splits a blame entry into two entries at 'len' lines. The original 'e'
843 * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
844 * which is returned, consists of the remainder: [e->lno + len, e->lno +
845 * e->num_lines). The caller needs to sort out the reference counting for the
846 * new entry's suspect.
848 static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
849 struct blame_origin *new_suspect)
851 struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
853 n->suspect = new_suspect;
854 n->lno = e->lno + len;
855 n->s_lno = e->s_lno + len;
856 n->num_lines = e->num_lines - len;
862 struct blame_line_tracker {
867 static int are_lines_adjacent(struct blame_line_tracker *first,
868 struct blame_line_tracker *second)
870 return first->is_parent == second->is_parent &&
871 first->s_lno + 1 == second->s_lno;
875 * This cheap heuristic assigns lines in the chunk to their relative location in
876 * the parent's chunk. Any additional lines are left with the target.
878 static void guess_line_blames(struct blame_origin *parent,
879 struct blame_origin *target,
880 int tlno, int offset, int same, int parent_len,
881 struct blame_line_tracker *line_blames)
883 int i, best_idx, target_idx;
884 int parent_slno = tlno + offset;
886 for (i = 0; i < same - tlno; i++) {
887 target_idx = tlno + i;
888 best_idx = target_idx + offset;
889 if (best_idx < parent_slno + parent_len) {
890 line_blames[i].is_parent = 1;
891 line_blames[i].s_lno = best_idx;
893 line_blames[i].is_parent = 0;
894 line_blames[i].s_lno = target_idx;
900 * This decides which parts of a blame entry go to the parent (added to the
901 * ignoredp list) and which stay with the target (added to the diffp list). The
902 * actual decision was made in a separate heuristic function, and those answers
903 * for the lines in 'e' are in line_blames. This consumes e, essentially
904 * putting it on a list.
906 * Note that the blame entries on the ignoredp list are not necessarily sorted
907 * with respect to the parent's line numbers yet.
909 static void ignore_blame_entry(struct blame_entry *e,
910 struct blame_origin *parent,
911 struct blame_origin *target,
912 struct blame_entry **diffp,
913 struct blame_entry **ignoredp,
914 struct blame_line_tracker *line_blames)
916 int entry_len, nr_lines, i;
919 * We carve new entries off the front of e. Each entry comes from a
920 * contiguous chunk of lines: adjacent lines from the same origin
921 * (either the parent or the target).
924 nr_lines = e->num_lines; /* e changes in the loop */
925 for (i = 0; i < nr_lines; i++) {
926 struct blame_entry *next = NULL;
929 * We are often adjacent to the next line - only split the blame
930 * entry when we have to.
932 if (i + 1 < nr_lines) {
933 if (are_lines_adjacent(&line_blames[i],
934 &line_blames[i + 1])) {
938 next = split_blame_at(e, entry_len,
939 blame_origin_incref(e->suspect));
941 if (line_blames[i].is_parent) {
942 blame_origin_decref(e->suspect);
943 e->suspect = blame_origin_incref(parent);
944 e->s_lno = line_blames[i - entry_len + 1].s_lno;
948 /* e->s_lno is already in the target's address space. */
952 assert(e->num_lines == entry_len);
960 * Process one hunk from the patch between the current suspect for
961 * blame_entry e and its parent. This first blames any unfinished
962 * entries before the chunk (which is where target and parent start
963 * differing) on the parent, and then splits blame entries at the
964 * start and at the end of the difference region. Since use of -M and
965 * -C options may lead to overlapping/duplicate source line number
966 * ranges, all we can rely on from sorting/merging is the order of the
967 * first suspect line number.
969 * tlno: line number in the target where this chunk begins
970 * same: line number in the target where this chunk ends
971 * offset: add to tlno to get the chunk starting point in the parent
972 * parent_len: number of lines in the parent chunk
974 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
975 int tlno, int offset, int same, int parent_len,
976 struct blame_origin *parent,
977 struct blame_origin *target, int ignore_diffs)
979 struct blame_entry *e = **srcq;
980 struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL;
981 struct blame_line_tracker *line_blames = NULL;
983 while (e && e->s_lno < tlno) {
984 struct blame_entry *next = e->next;
986 * current record starts before differing portion. If
987 * it reaches into it, we need to split it up and
988 * examine the second part separately.
990 if (e->s_lno + e->num_lines > tlno) {
991 /* Move second half to a new record */
992 struct blame_entry *n;
994 n = split_blame_at(e, tlno - e->s_lno, e->suspect);
995 /* Push new record to diffp */
999 blame_origin_decref(e->suspect);
1000 /* Pass blame for everything before the differing
1001 * chunk to the parent */
1002 e->suspect = blame_origin_incref(parent);
1009 * As we don't know how much of a common stretch after this
1010 * diff will occur, the currently blamed parts are all that we
1011 * can assign to the parent for now.
1015 **dstq = reverse_blame(samep, **dstq);
1016 *dstq = &samep->next;
1019 * Prepend the split off portions: everything after e starts
1020 * after the blameable portion.
1022 e = reverse_blame(diffp, e);
1025 * Now retain records on the target while parts are different
1031 if (ignore_diffs && same - tlno > 0) {
1032 line_blames = xcalloc(sizeof(struct blame_line_tracker),
1034 guess_line_blames(parent, target, tlno, offset, same,
1035 parent_len, line_blames);
1038 while (e && e->s_lno < same) {
1039 struct blame_entry *next = e->next;
1042 * If current record extends into sameness, need to split.
1044 if (e->s_lno + e->num_lines > same) {
1046 * Move second half to a new record to be
1047 * processed by later chunks
1049 struct blame_entry *n;
1051 n = split_blame_at(e, same - e->s_lno,
1052 blame_origin_incref(e->suspect));
1053 /* Push new record to samep */
1058 ignore_blame_entry(e, parent, target, &diffp, &ignoredp,
1059 line_blames + e->s_lno - tlno);
1069 * Note ignoredp is not sorted yet, and thus neither is dstq.
1070 * That list must be sorted before we queue_blames(). We defer
1071 * sorting until after all diff hunks are processed, so that
1072 * guess_line_blames() can pick *any* line in the parent. The
1073 * slight drawback is that we end up sorting all blame entries
1074 * passed to the parent, including those that are unrelated to
1075 * changes made by the ignored commit.
1077 **dstq = reverse_blame(ignoredp, **dstq);
1078 *dstq = &ignoredp->next;
1080 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
1081 /* Move across elements that are in the unblamable portion */
1083 *srcq = &diffp->next;
1086 struct blame_chunk_cb_data {
1087 struct blame_origin *parent;
1088 struct blame_origin *target;
1091 struct blame_entry **dstq;
1092 struct blame_entry **srcq;
1095 /* diff chunks are from parent to target */
1096 static int blame_chunk_cb(long start_a, long count_a,
1097 long start_b, long count_b, void *data)
1099 struct blame_chunk_cb_data *d = data;
1100 if (start_a - start_b != d->offset)
1101 die("internal error in blame::blame_chunk_cb");
1102 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
1103 start_b + count_b, count_a, d->parent, d->target,
1105 d->offset = start_a + count_a - (start_b + count_b);
1110 * We are looking at the origin 'target' and aiming to pass blame
1111 * for the lines it is suspected to its parent. Run diff to find
1112 * which lines came from parent and pass blame for them.
1114 static void pass_blame_to_parent(struct blame_scoreboard *sb,
1115 struct blame_origin *target,
1116 struct blame_origin *parent, int ignore_diffs)
1118 mmfile_t file_p, file_o;
1119 struct blame_chunk_cb_data d;
1120 struct blame_entry *newdest = NULL;
1122 if (!target->suspects)
1123 return; /* nothing remains for this target */
1128 d.ignore_diffs = ignore_diffs;
1129 d.dstq = &newdest; d.srcq = &target->suspects;
1131 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1132 fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob);
1133 sb->num_get_patch++;
1135 if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
1136 die("unable to generate diff (%s -> %s)",
1137 oid_to_hex(&parent->commit->object.oid),
1138 oid_to_hex(&target->commit->object.oid));
1139 /* The rest are the same as the parent */
1140 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
1144 newdest = llist_mergesort(newdest, get_next_blame,
1146 compare_blame_suspect);
1147 queue_blames(sb, parent, newdest);
1153 * The lines in blame_entry after splitting blames many times can become
1154 * very small and trivial, and at some point it becomes pointless to
1155 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
1156 * ordinary C program, and it is not worth to say it was copied from
1157 * totally unrelated file in the parent.
1159 * Compute how trivial the lines in the blame_entry are.
1161 unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e)
1164 const char *cp, *ep;
1170 cp = blame_nth_line(sb, e->lno);
1171 ep = blame_nth_line(sb, e->lno + e->num_lines);
1173 unsigned ch = *((unsigned char *)cp);
1183 * best_so_far[] and potential[] are both a split of an existing blame_entry
1184 * that passes blame to the parent. Maintain best_so_far the best split so
1185 * far, by comparing potential and best_so_far and copying potential into
1186 * bst_so_far as needed.
1188 static void copy_split_if_better(struct blame_scoreboard *sb,
1189 struct blame_entry *best_so_far,
1190 struct blame_entry *potential)
1194 if (!potential[1].suspect)
1196 if (best_so_far[1].suspect) {
1197 if (blame_entry_score(sb, &potential[1]) <
1198 blame_entry_score(sb, &best_so_far[1]))
1202 for (i = 0; i < 3; i++)
1203 blame_origin_incref(potential[i].suspect);
1204 decref_split(best_so_far);
1205 memcpy(best_so_far, potential, sizeof(struct blame_entry[3]));
1209 * We are looking at a part of the final image represented by
1210 * ent (tlno and same are offset by ent->s_lno).
1211 * tlno is where we are looking at in the final image.
1212 * up to (but not including) same match preimage.
1213 * plno is where we are looking at in the preimage.
1215 * <-------------- final image ---------------------->
1218 * <---------preimage----->
1221 * All line numbers are 0-based.
1223 static void handle_split(struct blame_scoreboard *sb,
1224 struct blame_entry *ent,
1225 int tlno, int plno, int same,
1226 struct blame_origin *parent,
1227 struct blame_entry *split)
1229 if (ent->num_lines <= tlno)
1232 struct blame_entry potential[3];
1235 split_overlap(potential, ent, tlno, plno, same, parent);
1236 copy_split_if_better(sb, split, potential);
1237 decref_split(potential);
1241 struct handle_split_cb_data {
1242 struct blame_scoreboard *sb;
1243 struct blame_entry *ent;
1244 struct blame_origin *parent;
1245 struct blame_entry *split;
1250 static int handle_split_cb(long start_a, long count_a,
1251 long start_b, long count_b, void *data)
1253 struct handle_split_cb_data *d = data;
1254 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1256 d->plno = start_a + count_a;
1257 d->tlno = start_b + count_b;
1262 * Find the lines from parent that are the same as ent so that
1263 * we can pass blames to it. file_p has the blob contents for
1266 static void find_copy_in_blob(struct blame_scoreboard *sb,
1267 struct blame_entry *ent,
1268 struct blame_origin *parent,
1269 struct blame_entry *split,
1274 struct handle_split_cb_data d;
1276 memset(&d, 0, sizeof(d));
1277 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1279 * Prepare mmfile that contains only the lines in ent.
1281 cp = blame_nth_line(sb, ent->lno);
1282 file_o.ptr = (char *) cp;
1283 file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp;
1286 * file_o is a part of final image we are annotating.
1287 * file_p partially may match that image.
1289 memset(split, 0, sizeof(struct blame_entry [3]));
1290 if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts))
1291 die("unable to generate diff (%s)",
1292 oid_to_hex(&parent->commit->object.oid));
1293 /* remainder, if any, all match the preimage */
1294 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1297 /* Move all blame entries from list *source that have a score smaller
1298 * than score_min to the front of list *small.
1299 * Returns a pointer to the link pointing to the old head of the small list.
1302 static struct blame_entry **filter_small(struct blame_scoreboard *sb,
1303 struct blame_entry **small,
1304 struct blame_entry **source,
1307 struct blame_entry *p = *source;
1308 struct blame_entry *oldsmall = *small;
1310 if (blame_entry_score(sb, p) <= score_min) {
1326 * See if lines currently target is suspected for can be attributed to
1329 static void find_move_in_parent(struct blame_scoreboard *sb,
1330 struct blame_entry ***blamed,
1331 struct blame_entry **toosmall,
1332 struct blame_origin *target,
1333 struct blame_origin *parent)
1335 struct blame_entry *e, split[3];
1336 struct blame_entry *unblamed = target->suspects;
1337 struct blame_entry *leftover = NULL;
1341 return; /* nothing remains for this target */
1343 fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob);
1347 /* At each iteration, unblamed has a NULL-terminated list of
1348 * entries that have not yet been tested for blame. leftover
1349 * contains the reversed list of entries that have been tested
1350 * without being assignable to the parent.
1353 struct blame_entry **unblamedtail = &unblamed;
1354 struct blame_entry *next;
1355 for (e = unblamed; e; e = next) {
1357 find_copy_in_blob(sb, e, parent, split, &file_p);
1358 if (split[1].suspect &&
1359 sb->move_score < blame_entry_score(sb, &split[1])) {
1360 split_blame(blamed, &unblamedtail, split, e);
1365 decref_split(split);
1367 *unblamedtail = NULL;
1368 toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score);
1370 target->suspects = reverse_blame(leftover, NULL);
1374 struct blame_entry *ent;
1375 struct blame_entry split[3];
1379 * Count the number of entries the target is suspected for,
1380 * and prepare a list of entry and the best split.
1382 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1385 struct blame_entry *e;
1387 struct blame_list *blame_list = NULL;
1389 for (e = unblamed, num_ents = 0; e; e = e->next)
1392 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1393 for (e = unblamed, i = 0; e; e = e->next)
1394 blame_list[i++].ent = e;
1396 *num_ents_p = num_ents;
1401 * For lines target is suspected for, see if we can find code movement
1402 * across file boundary from the parent commit. porigin is the path
1403 * in the parent we already tried.
1405 static void find_copy_in_parent(struct blame_scoreboard *sb,
1406 struct blame_entry ***blamed,
1407 struct blame_entry **toosmall,
1408 struct blame_origin *target,
1409 struct commit *parent,
1410 struct blame_origin *porigin,
1413 struct diff_options diff_opts;
1415 struct blame_list *blame_list;
1417 struct blame_entry *unblamed = target->suspects;
1418 struct blame_entry *leftover = NULL;
1421 return; /* nothing remains for this target */
1423 repo_diff_setup(sb->repo, &diff_opts);
1424 diff_opts.flags.recursive = 1;
1425 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1427 diff_setup_done(&diff_opts);
1429 /* Try "find copies harder" on new path if requested;
1430 * we do not want to use diffcore_rename() actually to
1431 * match things up; find_copies_harder is set only to
1432 * force diff_tree_oid() to feed all filepairs to diff_queue,
1433 * and this code needs to be after diff_setup_done(), which
1434 * usually makes find-copies-harder imply copy detection.
1436 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1437 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1438 && (!porigin || strcmp(target->path, porigin->path))))
1439 diff_opts.flags.find_copies_harder = 1;
1441 if (is_null_oid(&target->commit->object.oid))
1442 do_diff_cache(get_commit_tree_oid(parent), &diff_opts);
1444 diff_tree_oid(get_commit_tree_oid(parent),
1445 get_commit_tree_oid(target->commit),
1448 if (!diff_opts.flags.find_copies_harder)
1449 diffcore_std(&diff_opts);
1452 struct blame_entry **unblamedtail = &unblamed;
1453 blame_list = setup_blame_list(unblamed, &num_ents);
1455 for (i = 0; i < diff_queued_diff.nr; i++) {
1456 struct diff_filepair *p = diff_queued_diff.queue[i];
1457 struct blame_origin *norigin;
1459 struct blame_entry potential[3];
1461 if (!DIFF_FILE_VALID(p->one))
1462 continue; /* does not exist in parent */
1463 if (S_ISGITLINK(p->one->mode))
1464 continue; /* ignore git links */
1465 if (porigin && !strcmp(p->one->path, porigin->path))
1466 /* find_move already dealt with this path */
1469 norigin = get_origin(parent, p->one->path);
1470 oidcpy(&norigin->blob_oid, &p->one->oid);
1471 norigin->mode = p->one->mode;
1472 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob);
1476 for (j = 0; j < num_ents; j++) {
1477 find_copy_in_blob(sb, blame_list[j].ent,
1478 norigin, potential, &file_p);
1479 copy_split_if_better(sb, blame_list[j].split,
1481 decref_split(potential);
1483 blame_origin_decref(norigin);
1486 for (j = 0; j < num_ents; j++) {
1487 struct blame_entry *split = blame_list[j].split;
1488 if (split[1].suspect &&
1489 sb->copy_score < blame_entry_score(sb, &split[1])) {
1490 split_blame(blamed, &unblamedtail, split,
1493 blame_list[j].ent->next = leftover;
1494 leftover = blame_list[j].ent;
1496 decref_split(split);
1499 *unblamedtail = NULL;
1500 toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score);
1502 target->suspects = reverse_blame(leftover, NULL);
1503 diff_flush(&diff_opts);
1504 clear_pathspec(&diff_opts.pathspec);
1508 * The blobs of origin and porigin exactly match, so everything
1509 * origin is suspected for can be blamed on the parent.
1511 static void pass_whole_blame(struct blame_scoreboard *sb,
1512 struct blame_origin *origin, struct blame_origin *porigin)
1514 struct blame_entry *e, *suspects;
1516 if (!porigin->file.ptr && origin->file.ptr) {
1517 /* Steal its file */
1518 porigin->file = origin->file;
1519 origin->file.ptr = NULL;
1521 suspects = origin->suspects;
1522 origin->suspects = NULL;
1523 for (e = suspects; e; e = e->next) {
1524 blame_origin_incref(porigin);
1525 blame_origin_decref(e->suspect);
1526 e->suspect = porigin;
1528 queue_blames(sb, porigin, suspects);
1532 * We pass blame from the current commit to its parents. We keep saying
1533 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1534 * exonerate ourselves.
1536 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit,
1540 if (revs->first_parent_only &&
1542 commit->parents->next) {
1543 free_commit_list(commit->parents->next);
1544 commit->parents->next = NULL;
1546 return commit->parents;
1548 return lookup_decoration(&revs->children, &commit->object);
1551 static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse)
1553 struct commit_list *l = first_scapegoat(revs, commit, reverse);
1554 return commit_list_count(l);
1557 /* Distribute collected unsorted blames to the respected sorted lists
1558 * in the various origins.
1560 static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed)
1562 blamed = llist_mergesort(blamed, get_next_blame, set_next_blame,
1563 compare_blame_suspect);
1566 struct blame_origin *porigin = blamed->suspect;
1567 struct blame_entry *suspects = NULL;
1569 struct blame_entry *next = blamed->next;
1570 blamed->next = suspects;
1573 } while (blamed && blamed->suspect == porigin);
1574 suspects = reverse_blame(suspects, NULL);
1575 queue_blames(sb, porigin, suspects);
1581 static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt)
1583 struct rev_info *revs = sb->revs;
1584 int i, pass, num_sg;
1585 struct commit *commit = origin->commit;
1586 struct commit_list *sg;
1587 struct blame_origin *sg_buf[MAXSG];
1588 struct blame_origin *porigin, **sg_origin = sg_buf;
1589 struct blame_entry *toosmall = NULL;
1590 struct blame_entry *blames, **blametail = &blames;
1592 num_sg = num_scapegoats(revs, commit, sb->reverse);
1595 else if (num_sg < ARRAY_SIZE(sg_buf))
1596 memset(sg_buf, 0, sizeof(sg_buf));
1598 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1601 * The first pass looks for unrenamed path to optimize for
1602 * common cases, then we look for renames in the second pass.
1604 for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) {
1605 struct blame_origin *(*find)(struct repository *, struct commit *, struct blame_origin *);
1606 find = pass ? find_rename : find_origin;
1608 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1610 sg = sg->next, i++) {
1611 struct commit *p = sg->item;
1616 if (parse_commit(p))
1618 porigin = find(sb->repo, p, origin);
1621 if (oideq(&porigin->blob_oid, &origin->blob_oid)) {
1622 pass_whole_blame(sb, origin, porigin);
1623 blame_origin_decref(porigin);
1626 for (j = same = 0; j < i; j++)
1628 oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) {
1633 sg_origin[i] = porigin;
1635 blame_origin_decref(porigin);
1640 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1642 sg = sg->next, i++) {
1643 struct blame_origin *porigin = sg_origin[i];
1646 if (!origin->previous) {
1647 blame_origin_incref(porigin);
1648 origin->previous = porigin;
1650 pass_blame_to_parent(sb, origin, porigin, 0);
1651 if (!origin->suspects)
1656 * Pass remaining suspects for ignored commits to their parents.
1658 if (oidset_contains(&sb->ignore_list, &commit->object.oid)) {
1659 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1661 sg = sg->next, i++) {
1662 struct blame_origin *porigin = sg_origin[i];
1666 pass_blame_to_parent(sb, origin, porigin, 1);
1667 if (!origin->suspects)
1673 * Optionally find moves in parents' files.
1675 if (opt & PICKAXE_BLAME_MOVE) {
1676 filter_small(sb, &toosmall, &origin->suspects, sb->move_score);
1677 if (origin->suspects) {
1678 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1680 sg = sg->next, i++) {
1681 struct blame_origin *porigin = sg_origin[i];
1684 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1685 if (!origin->suspects)
1692 * Optionally find copies from parents' files.
1694 if (opt & PICKAXE_BLAME_COPY) {
1695 if (sb->copy_score > sb->move_score)
1696 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1697 else if (sb->copy_score < sb->move_score) {
1698 origin->suspects = blame_merge(origin->suspects, toosmall);
1700 filter_small(sb, &toosmall, &origin->suspects, sb->copy_score);
1702 if (!origin->suspects)
1705 for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse);
1707 sg = sg->next, i++) {
1708 struct blame_origin *porigin = sg_origin[i];
1709 find_copy_in_parent(sb, &blametail, &toosmall,
1710 origin, sg->item, porigin, opt);
1711 if (!origin->suspects)
1718 distribute_blame(sb, blames);
1720 * prepend toosmall to origin->suspects
1722 * There is no point in sorting: this ends up on a big
1723 * unsorted list in the caller anyway.
1726 struct blame_entry **tail = &toosmall;
1728 tail = &(*tail)->next;
1729 *tail = origin->suspects;
1730 origin->suspects = toosmall;
1732 for (i = 0; i < num_sg; i++) {
1734 drop_origin_blob(sg_origin[i]);
1735 blame_origin_decref(sg_origin[i]);
1738 drop_origin_blob(origin);
1739 if (sg_buf != sg_origin)
1744 * The main loop -- while we have blobs with lines whose true origin
1745 * is still unknown, pick one blob, and allow its lines to pass blames
1746 * to its parents. */
1747 void assign_blame(struct blame_scoreboard *sb, int opt)
1749 struct rev_info *revs = sb->revs;
1750 struct commit *commit = prio_queue_get(&sb->commits);
1753 struct blame_entry *ent;
1754 struct blame_origin *suspect = get_blame_suspects(commit);
1756 /* find one suspect to break down */
1757 while (suspect && !suspect->suspects)
1758 suspect = suspect->next;
1761 commit = prio_queue_get(&sb->commits);
1765 assert(commit == suspect->commit);
1768 * We will use this suspect later in the loop,
1769 * so hold onto it in the meantime.
1771 blame_origin_incref(suspect);
1772 parse_commit(commit);
1774 (!(commit->object.flags & UNINTERESTING) &&
1775 !(revs->max_age != -1 && commit->date < revs->max_age)))
1776 pass_blame(sb, suspect, opt);
1778 commit->object.flags |= UNINTERESTING;
1779 if (commit->object.parsed)
1780 mark_parents_uninteresting(commit);
1782 /* treat root commit as boundary */
1783 if (!commit->parents && !sb->show_root)
1784 commit->object.flags |= UNINTERESTING;
1786 /* Take responsibility for the remaining entries */
1787 ent = suspect->suspects;
1789 suspect->guilty = 1;
1791 struct blame_entry *next = ent->next;
1792 if (sb->found_guilty_entry)
1793 sb->found_guilty_entry(ent, sb->found_guilty_entry_data);
1798 ent->next = sb->ent;
1799 sb->ent = suspect->suspects;
1800 suspect->suspects = NULL;
1804 blame_origin_decref(suspect);
1806 if (sb->debug) /* sanity */
1807 sanity_check_refcnt(sb);
1811 static const char *get_next_line(const char *start, const char *end)
1813 const char *nl = memchr(start, '\n', end - start);
1814 return nl ? nl + 1 : end;
1818 * To allow quick access to the contents of nth line in the
1819 * final image, prepare an index in the scoreboard.
1821 static int prepare_lines(struct blame_scoreboard *sb)
1823 const char *buf = sb->final_buf;
1824 unsigned long len = sb->final_buf_size;
1825 const char *end = buf + len;
1830 for (p = buf; p < end; p = get_next_line(p, end))
1833 ALLOC_ARRAY(sb->lineno, num + 1);
1834 lineno = sb->lineno;
1836 for (p = buf; p < end; p = get_next_line(p, end))
1837 *lineno++ = p - buf;
1841 sb->num_lines = num;
1842 return sb->num_lines;
1845 static struct commit *find_single_final(struct rev_info *revs,
1846 const char **name_p)
1849 struct commit *found = NULL;
1850 const char *name = NULL;
1852 for (i = 0; i < revs->pending.nr; i++) {
1853 struct object *obj = revs->pending.objects[i].item;
1854 if (obj->flags & UNINTERESTING)
1856 obj = deref_tag(revs->repo, obj, NULL, 0);
1857 if (obj->type != OBJ_COMMIT)
1858 die("Non commit %s?", revs->pending.objects[i].name);
1860 die("More than one commit to dig from %s and %s?",
1861 revs->pending.objects[i].name, name);
1862 found = (struct commit *)obj;
1863 name = revs->pending.objects[i].name;
1866 *name_p = xstrdup_or_null(name);
1870 static struct commit *dwim_reverse_initial(struct rev_info *revs,
1871 const char **name_p)
1874 * DWIM "git blame --reverse ONE -- PATH" as
1875 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1876 * when it makes sense.
1879 struct commit *head_commit;
1880 struct object_id head_oid;
1882 if (revs->pending.nr != 1)
1885 /* Is that sole rev a committish? */
1886 obj = revs->pending.objects[0].item;
1887 obj = deref_tag(revs->repo, obj, NULL, 0);
1888 if (obj->type != OBJ_COMMIT)
1891 /* Do we have HEAD? */
1892 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
1894 head_commit = lookup_commit_reference_gently(revs->repo,
1899 /* Turn "ONE" into "ONE..HEAD" then */
1900 obj->flags |= UNINTERESTING;
1901 add_pending_object(revs, &head_commit->object, "HEAD");
1904 *name_p = revs->pending.objects[0].name;
1905 return (struct commit *)obj;
1908 static struct commit *find_single_initial(struct rev_info *revs,
1909 const char **name_p)
1912 struct commit *found = NULL;
1913 const char *name = NULL;
1916 * There must be one and only one negative commit, and it must be
1919 for (i = 0; i < revs->pending.nr; i++) {
1920 struct object *obj = revs->pending.objects[i].item;
1921 if (!(obj->flags & UNINTERESTING))
1923 obj = deref_tag(revs->repo, obj, NULL, 0);
1924 if (obj->type != OBJ_COMMIT)
1925 die("Non commit %s?", revs->pending.objects[i].name);
1927 die("More than one commit to dig up from, %s and %s?",
1928 revs->pending.objects[i].name, name);
1929 found = (struct commit *) obj;
1930 name = revs->pending.objects[i].name;
1934 found = dwim_reverse_initial(revs, &name);
1936 die("No commit to dig up from?");
1939 *name_p = xstrdup(name);
1943 void init_scoreboard(struct blame_scoreboard *sb)
1945 memset(sb, 0, sizeof(struct blame_scoreboard));
1946 sb->move_score = BLAME_DEFAULT_MOVE_SCORE;
1947 sb->copy_score = BLAME_DEFAULT_COPY_SCORE;
1950 void setup_scoreboard(struct blame_scoreboard *sb,
1952 struct blame_origin **orig)
1954 const char *final_commit_name = NULL;
1955 struct blame_origin *o;
1956 struct commit *final_commit = NULL;
1957 enum object_type type;
1959 init_blame_suspects(&blame_suspects);
1961 if (sb->reverse && sb->contents_from)
1962 die(_("--contents and --reverse do not blend well."));
1965 BUG("repo is NULL");
1968 sb->final = find_single_final(sb->revs, &final_commit_name);
1969 sb->commits.compare = compare_commits_by_commit_date;
1971 sb->final = find_single_initial(sb->revs, &final_commit_name);
1972 sb->commits.compare = compare_commits_by_reverse_commit_date;
1975 if (sb->final && sb->contents_from)
1976 die(_("cannot use --contents with final commit object name"));
1978 if (sb->reverse && sb->revs->first_parent_only)
1979 sb->revs->children.name = NULL;
1983 * "--not A B -- path" without anything positive;
1984 * do not default to HEAD, but use the working tree
1988 sb->final = fake_working_tree_commit(sb->repo,
1990 path, sb->contents_from);
1991 add_pending_object(sb->revs, &(sb->final->object), ":");
1994 if (sb->reverse && sb->revs->first_parent_only) {
1995 final_commit = find_single_final(sb->revs, NULL);
1997 die(_("--reverse and --first-parent together require specified latest commit"));
2001 * If we have bottom, this will mark the ancestors of the
2002 * bottom commits we would reach while traversing as
2005 if (prepare_revision_walk(sb->revs))
2006 die(_("revision walk setup failed"));
2008 if (sb->reverse && sb->revs->first_parent_only) {
2009 struct commit *c = final_commit;
2011 sb->revs->children.name = "children";
2012 while (c->parents &&
2013 !oideq(&c->object.oid, &sb->final->object.oid)) {
2014 struct commit_list *l = xcalloc(1, sizeof(*l));
2017 if (add_decoration(&sb->revs->children,
2018 &c->parents->item->object, l))
2019 BUG("not unique item in first-parent chain");
2020 c = c->parents->item;
2023 if (!oideq(&c->object.oid, &sb->final->object.oid))
2024 die(_("--reverse --first-parent together require range along first-parent chain"));
2027 if (is_null_oid(&sb->final->object.oid)) {
2028 o = get_blame_suspects(sb->final);
2029 sb->final_buf = xmemdupz(o->file.ptr, o->file.size);
2030 sb->final_buf_size = o->file.size;
2033 o = get_origin(sb->final, path);
2034 if (fill_blob_sha1_and_mode(sb->repo, o))
2035 die(_("no such path %s in %s"), path, final_commit_name);
2037 if (sb->revs->diffopt.flags.allow_textconv &&
2038 textconv_object(sb->repo, path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf,
2039 &sb->final_buf_size))
2042 sb->final_buf = read_object_file(&o->blob_oid, &type,
2043 &sb->final_buf_size);
2046 die(_("cannot read blob %s for path %s"),
2047 oid_to_hex(&o->blob_oid),
2050 sb->num_read_blob++;
2056 free((char *)final_commit_name);
2061 struct blame_entry *blame_entry_prepend(struct blame_entry *head,
2062 long start, long end,
2063 struct blame_origin *o)
2065 struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry));
2066 new_head->lno = start;
2067 new_head->num_lines = end - start;
2068 new_head->suspect = o;
2069 new_head->s_lno = start;
2070 new_head->next = head;
2071 blame_origin_incref(o);