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
*repo
,
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(repo
, &blob_oid
, NULL
) == OBJ_BLOB
)
109 pos
= index_name_pos(repo
->index
, path
, strlen(path
));
111 ; /* path is in the index */
112 else if (-1 - pos
< repo
->index
->cache_nr
&&
113 !strcmp(repo
->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 commit_list
**tail
, const struct object_id
*oid
)
121 struct commit
*parent
;
123 parent
= lookup_commit_reference(the_repository
, oid
);
125 die("no such commit %s", oid_to_hex(oid
));
126 return &commit_list_insert(parent
, tail
)->next
;
129 static void append_merge_parents(struct commit_list
**tail
)
132 struct strbuf line
= STRBUF_INIT
;
134 merge_head
= open(git_path_merge_head(the_repository
), O_RDONLY
);
135 if (merge_head
< 0) {
138 die("cannot open '%s' for reading",
139 git_path_merge_head(the_repository
));
142 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
143 struct object_id oid
;
144 if (line
.len
< GIT_SHA1_HEXSZ
|| get_oid_hex(line
.buf
, &oid
))
145 die("unknown line in '%s': %s",
146 git_path_merge_head(the_repository
), line
.buf
);
147 tail
= append_parent(tail
, &oid
);
150 strbuf_release(&line
);
154 * This isn't as simple as passing sb->buf and sb->len, because we
155 * want to transfer ownership of the buffer to the commit (so we
158 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
161 void *buf
= strbuf_detach(sb
, &len
);
162 set_commit_buffer(the_repository
, c
, buf
, len
);
166 * Prepare a dummy commit that represents the work tree (or staged) item.
167 * Note that annotating work tree item never works in the reverse.
169 static struct commit
*fake_working_tree_commit(struct repository
*repo
,
170 struct diff_options
*opt
,
172 const char *contents_from
)
174 struct commit
*commit
;
175 struct blame_origin
*origin
;
176 struct commit_list
**parent_tail
, *parent
;
177 struct object_id head_oid
;
178 struct strbuf buf
= STRBUF_INIT
;
182 struct cache_entry
*ce
;
184 struct strbuf msg
= STRBUF_INIT
;
186 read_index(repo
->index
);
188 commit
= alloc_commit_node(the_repository
);
189 commit
->object
.parsed
= 1;
191 parent_tail
= &commit
->parents
;
193 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
194 die("no such ref: HEAD");
196 parent_tail
= append_parent(parent_tail
, &head_oid
);
197 append_merge_parents(parent_tail
);
198 verify_working_tree_path(repo
, commit
, path
);
200 origin
= make_origin(commit
, path
);
202 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
203 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
204 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
205 strbuf_addf(&msg
, "parent %s\n",
206 oid_to_hex(&parent
->item
->object
.oid
));
210 "Version of %s from %s\n",
212 (!contents_from
? path
:
213 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
214 set_commit_buffer_from_strbuf(commit
, &msg
);
216 if (!contents_from
|| strcmp("-", contents_from
)) {
218 const char *read_from
;
220 unsigned long buf_len
;
223 if (stat(contents_from
, &st
) < 0)
224 die_errno("Cannot stat '%s'", contents_from
);
225 read_from
= contents_from
;
228 if (lstat(path
, &st
) < 0)
229 die_errno("Cannot lstat '%s'", path
);
232 mode
= canon_mode(st
.st_mode
);
234 switch (st
.st_mode
& S_IFMT
) {
236 if (opt
->flags
.allow_textconv
&&
237 textconv_object(read_from
, mode
, &null_oid
, 0, &buf_ptr
, &buf_len
))
238 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
239 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
240 die_errno("cannot open or read '%s'", read_from
);
243 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
244 die_errno("cannot readlink '%s'", read_from
);
247 die("unsupported file type %s", read_from
);
251 /* Reading from stdin */
253 if (strbuf_read(&buf
, 0, 0) < 0)
254 die_errno("failed to read from stdin");
256 convert_to_git(repo
->index
, path
, buf
.buf
, buf
.len
, &buf
, 0);
257 origin
->file
.ptr
= buf
.buf
;
258 origin
->file
.size
= buf
.len
;
259 pretend_object_file(buf
.buf
, buf
.len
, OBJ_BLOB
, &origin
->blob_oid
);
262 * Read the current index, replace the path entry with
263 * origin->blob_sha1 without mucking with its mode or type
264 * bits; we are not going to write this index out -- we just
265 * want to run "diff-index --cached".
267 discard_index(repo
->index
);
268 read_index(repo
->index
);
272 int pos
= index_name_pos(repo
->index
, path
, len
);
274 mode
= repo
->index
->cache
[pos
]->ce_mode
;
276 /* Let's not bother reading from HEAD tree */
277 mode
= S_IFREG
| 0644;
279 ce
= make_empty_cache_entry(repo
->index
, len
);
280 oidcpy(&ce
->oid
, &origin
->blob_oid
);
281 memcpy(ce
->name
, path
, len
);
282 ce
->ce_flags
= create_ce_flags(0);
283 ce
->ce_namelen
= len
;
284 ce
->ce_mode
= create_ce_mode(mode
);
285 add_index_entry(repo
->index
, ce
,
286 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
288 cache_tree_invalidate_path(repo
->index
, path
);
295 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
296 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
, int xdl_opts
)
299 xdemitconf_t xecfg
= {0};
300 xdemitcb_t ecb
= {NULL
};
302 xpp
.flags
= xdl_opts
;
303 xecfg
.hunk_func
= hunk_func
;
305 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
309 * Given an origin, prepare mmfile_t structure to be used by the
312 static void fill_origin_blob(struct diff_options
*opt
,
313 struct blame_origin
*o
, mmfile_t
*file
, int *num_read_blob
)
316 enum object_type type
;
317 unsigned long file_size
;
320 if (opt
->flags
.allow_textconv
&&
321 textconv_object(o
->path
, o
->mode
, &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
324 file
->ptr
= read_object_file(&o
->blob_oid
, &type
,
326 file
->size
= file_size
;
329 die("Cannot read blob %s for path %s",
330 oid_to_hex(&o
->blob_oid
),
338 static void drop_origin_blob(struct blame_origin
*o
)
341 FREE_AND_NULL(o
->file
.ptr
);
346 * Any merge of blames happens on lists of blames that arrived via
347 * different parents in a single suspect. In this case, we want to
348 * sort according to the suspect line numbers as opposed to the final
349 * image line numbers. The function body is somewhat longish because
350 * it avoids unnecessary writes.
353 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
354 struct blame_entry
*list2
)
356 struct blame_entry
*p1
= list1
, *p2
= list2
,
364 if (p1
->s_lno
<= p2
->s_lno
) {
367 if ((p1
= *tail
) == NULL
) {
371 } while (p1
->s_lno
<= p2
->s_lno
);
377 if ((p2
= *tail
) == NULL
) {
381 } while (p1
->s_lno
> p2
->s_lno
);
385 if ((p1
= *tail
) == NULL
) {
389 } while (p1
->s_lno
<= p2
->s_lno
);
393 static void *get_next_blame(const void *p
)
395 return ((struct blame_entry
*)p
)->next
;
398 static void set_next_blame(void *p1
, void *p2
)
400 ((struct blame_entry
*)p1
)->next
= p2
;
404 * Final image line numbers are all different, so we don't need a
405 * three-way comparison here.
408 static int compare_blame_final(const void *p1
, const void *p2
)
410 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
414 static int compare_blame_suspect(const void *p1
, const void *p2
)
416 const struct blame_entry
*s1
= p1
, *s2
= p2
;
418 * to allow for collating suspects, we sort according to the
419 * respective pointer value as the primary sorting criterion.
420 * The actual relation is pretty unimportant as long as it
421 * establishes a total order. Comparing as integers gives us
424 if (s1
->suspect
!= s2
->suspect
)
425 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
426 if (s1
->s_lno
== s2
->s_lno
)
428 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
431 void blame_sort_final(struct blame_scoreboard
*sb
)
433 sb
->ent
= llist_mergesort(sb
->ent
, get_next_blame
, set_next_blame
,
434 compare_blame_final
);
437 static int compare_commits_by_reverse_commit_date(const void *a
,
441 return -compare_commits_by_commit_date(a
, b
, c
);
445 * For debugging -- origin is refcounted, and this asserts that
446 * we do not underflow.
448 static void sanity_check_refcnt(struct blame_scoreboard
*sb
)
451 struct blame_entry
*ent
;
453 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
454 /* Nobody should have zero or negative refcnt */
455 if (ent
->suspect
->refcnt
<= 0) {
456 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
458 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
459 ent
->suspect
->refcnt
);
464 sb
->on_sanity_fail(sb
, baa
);
468 * If two blame entries that are next to each other came from
469 * contiguous lines in the same origin (i.e. <commit, path> pair),
470 * merge them together.
472 void blame_coalesce(struct blame_scoreboard
*sb
)
474 struct blame_entry
*ent
, *next
;
476 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
477 if (ent
->suspect
== next
->suspect
&&
478 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
479 ent
->num_lines
+= next
->num_lines
;
480 ent
->next
= next
->next
;
481 blame_origin_decref(next
->suspect
);
484 next
= ent
; /* again */
488 if (sb
->debug
) /* sanity */
489 sanity_check_refcnt(sb
);
493 * Merge the given sorted list of blames into a preexisting origin.
494 * If there were no previous blames to that commit, it is entered into
495 * the commit priority queue of the score board.
498 static void queue_blames(struct blame_scoreboard
*sb
, struct blame_origin
*porigin
,
499 struct blame_entry
*sorted
)
501 if (porigin
->suspects
)
502 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
504 struct blame_origin
*o
;
505 for (o
= get_blame_suspects(porigin
->commit
); o
; o
= o
->next
) {
507 porigin
->suspects
= sorted
;
511 porigin
->suspects
= sorted
;
512 prio_queue_put(&sb
->commits
, porigin
->commit
);
517 * Fill the blob_sha1 field of an origin if it hasn't, so that later
518 * call to fill_origin_blob() can use it to locate the data. blob_sha1
519 * for an origin is also used to pass the blame for the entire file to
520 * the parent to detect the case where a child's blob is identical to
521 * that of its parent's.
523 * This also fills origin->mode for corresponding tree path.
525 static int fill_blob_sha1_and_mode(struct repository
*repo
,
526 struct blame_origin
*origin
)
528 if (!is_null_oid(&origin
->blob_oid
))
530 if (get_tree_entry(&origin
->commit
->object
.oid
, origin
->path
, &origin
->blob_oid
, &origin
->mode
))
532 if (oid_object_info(repo
, &origin
->blob_oid
, NULL
) != OBJ_BLOB
)
536 oidclr(&origin
->blob_oid
);
537 origin
->mode
= S_IFINVALID
;
542 * We have an origin -- check if the same path exists in the
543 * parent and return an origin structure to represent it.
545 static struct blame_origin
*find_origin(struct commit
*parent
,
546 struct blame_origin
*origin
)
548 struct blame_origin
*porigin
;
549 struct diff_options diff_opts
;
550 const char *paths
[2];
552 /* First check any existing origins */
553 for (porigin
= get_blame_suspects(parent
); porigin
; porigin
= porigin
->next
)
554 if (!strcmp(porigin
->path
, origin
->path
)) {
556 * The same path between origin and its parent
557 * without renaming -- the most common case.
559 return blame_origin_incref (porigin
);
562 /* See if the origin->path is different between parent
563 * and origin first. Most of the time they are the
564 * same and diff-tree is fairly efficient about this.
566 diff_setup(&diff_opts
);
567 diff_opts
.flags
.recursive
= 1;
568 diff_opts
.detect_rename
= 0;
569 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
570 paths
[0] = origin
->path
;
573 parse_pathspec(&diff_opts
.pathspec
,
574 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
575 PATHSPEC_LITERAL_PATH
, "", paths
);
576 diff_setup_done(&diff_opts
);
578 if (is_null_oid(&origin
->commit
->object
.oid
))
579 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
581 diff_tree_oid(get_commit_tree_oid(parent
),
582 get_commit_tree_oid(origin
->commit
),
584 diffcore_std(&diff_opts
);
586 if (!diff_queued_diff
.nr
) {
587 /* The path is the same as parent */
588 porigin
= get_origin(parent
, origin
->path
);
589 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
590 porigin
->mode
= origin
->mode
;
593 * Since origin->path is a pathspec, if the parent
594 * commit had it as a directory, we will see a whole
595 * bunch of deletion of files in the directory that we
599 struct diff_filepair
*p
= NULL
;
600 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
602 p
= diff_queued_diff
.queue
[i
];
603 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
604 if (!strcmp(name
, origin
->path
))
608 die("internal error in blame::find_origin");
611 die("internal error in blame::find_origin (%c)",
614 porigin
= get_origin(parent
, origin
->path
);
615 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
616 porigin
->mode
= p
->one
->mode
;
620 /* Did not exist in parent, or type changed */
624 diff_flush(&diff_opts
);
625 clear_pathspec(&diff_opts
.pathspec
);
630 * We have an origin -- find the path that corresponds to it in its
631 * parent and return an origin structure to represent it.
633 static struct blame_origin
*find_rename(struct commit
*parent
,
634 struct blame_origin
*origin
)
636 struct blame_origin
*porigin
= NULL
;
637 struct diff_options diff_opts
;
640 diff_setup(&diff_opts
);
641 diff_opts
.flags
.recursive
= 1;
642 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
643 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
644 diff_opts
.single_follow
= origin
->path
;
645 diff_setup_done(&diff_opts
);
647 if (is_null_oid(&origin
->commit
->object
.oid
))
648 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
650 diff_tree_oid(get_commit_tree_oid(parent
),
651 get_commit_tree_oid(origin
->commit
),
653 diffcore_std(&diff_opts
);
655 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
656 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
657 if ((p
->status
== 'R' || p
->status
== 'C') &&
658 !strcmp(p
->two
->path
, origin
->path
)) {
659 porigin
= get_origin(parent
, p
->one
->path
);
660 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
661 porigin
->mode
= p
->one
->mode
;
665 diff_flush(&diff_opts
);
666 clear_pathspec(&diff_opts
.pathspec
);
671 * Append a new blame entry to a given output queue.
673 static void add_blame_entry(struct blame_entry
***queue
,
674 const struct blame_entry
*src
)
676 struct blame_entry
*e
= xmalloc(sizeof(*e
));
677 memcpy(e
, src
, sizeof(*e
));
678 blame_origin_incref(e
->suspect
);
686 * src typically is on-stack; we want to copy the information in it to
687 * a malloced blame_entry that gets added to the given queue. The
688 * origin of dst loses a refcnt.
690 static void dup_entry(struct blame_entry
***queue
,
691 struct blame_entry
*dst
, struct blame_entry
*src
)
693 blame_origin_incref(src
->suspect
);
694 blame_origin_decref(dst
->suspect
);
695 memcpy(dst
, src
, sizeof(*src
));
701 const char *blame_nth_line(struct blame_scoreboard
*sb
, long lno
)
703 return sb
->final_buf
+ sb
->lineno
[lno
];
707 * It is known that lines between tlno to same came from parent, and e
708 * has an overlap with that range. it also is known that parent's
709 * line plno corresponds to e's line tlno.
715 * <------------------>
717 * Split e into potentially three parts; before this chunk, the chunk
718 * to be blamed for the parent, and after that portion.
720 static void split_overlap(struct blame_entry
*split
,
721 struct blame_entry
*e
,
722 int tlno
, int plno
, int same
,
723 struct blame_origin
*parent
)
726 memset(split
, 0, sizeof(struct blame_entry
[3]));
728 if (e
->s_lno
< tlno
) {
729 /* there is a pre-chunk part not blamed on parent */
730 split
[0].suspect
= blame_origin_incref(e
->suspect
);
731 split
[0].lno
= e
->lno
;
732 split
[0].s_lno
= e
->s_lno
;
733 split
[0].num_lines
= tlno
- e
->s_lno
;
734 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
735 split
[1].s_lno
= plno
;
738 split
[1].lno
= e
->lno
;
739 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
742 if (same
< e
->s_lno
+ e
->num_lines
) {
743 /* there is a post-chunk part not blamed on parent */
744 split
[2].suspect
= blame_origin_incref(e
->suspect
);
745 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
746 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
747 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
748 chunk_end_lno
= split
[2].lno
;
751 chunk_end_lno
= e
->lno
+ e
->num_lines
;
752 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
755 * if it turns out there is nothing to blame the parent for,
756 * forget about the splitting. !split[1].suspect signals this.
758 if (split
[1].num_lines
< 1)
760 split
[1].suspect
= blame_origin_incref(parent
);
764 * split_overlap() divided an existing blame e into up to three parts
765 * in split. Any assigned blame is moved to queue to
768 static void split_blame(struct blame_entry
***blamed
,
769 struct blame_entry
***unblamed
,
770 struct blame_entry
*split
,
771 struct blame_entry
*e
)
773 if (split
[0].suspect
&& split
[2].suspect
) {
774 /* The first part (reuse storage for the existing entry e) */
775 dup_entry(unblamed
, e
, &split
[0]);
777 /* The last part -- me */
778 add_blame_entry(unblamed
, &split
[2]);
780 /* ... and the middle part -- parent */
781 add_blame_entry(blamed
, &split
[1]);
783 else if (!split
[0].suspect
&& !split
[2].suspect
)
785 * The parent covers the entire area; reuse storage for
786 * e and replace it with the parent.
788 dup_entry(blamed
, e
, &split
[1]);
789 else if (split
[0].suspect
) {
790 /* me and then parent */
791 dup_entry(unblamed
, e
, &split
[0]);
792 add_blame_entry(blamed
, &split
[1]);
795 /* parent and then me */
796 dup_entry(blamed
, e
, &split
[1]);
797 add_blame_entry(unblamed
, &split
[2]);
802 * After splitting the blame, the origins used by the
803 * on-stack blame_entry should lose one refcnt each.
805 static void decref_split(struct blame_entry
*split
)
809 for (i
= 0; i
< 3; i
++)
810 blame_origin_decref(split
[i
].suspect
);
814 * reverse_blame reverses the list given in head, appending tail.
815 * That allows us to build lists in reverse order, then reverse them
816 * afterwards. This can be faster than building the list in proper
817 * order right away. The reason is that building in proper order
818 * requires writing a link in the _previous_ element, while building
819 * in reverse order just requires placing the list head into the
823 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
824 struct blame_entry
*tail
)
827 struct blame_entry
*next
= head
->next
;
836 * Process one hunk from the patch between the current suspect for
837 * blame_entry e and its parent. This first blames any unfinished
838 * entries before the chunk (which is where target and parent start
839 * differing) on the parent, and then splits blame entries at the
840 * start and at the end of the difference region. Since use of -M and
841 * -C options may lead to overlapping/duplicate source line number
842 * ranges, all we can rely on from sorting/merging is the order of the
843 * first suspect line number.
845 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
846 int tlno
, int offset
, int same
,
847 struct blame_origin
*parent
)
849 struct blame_entry
*e
= **srcq
;
850 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
852 while (e
&& e
->s_lno
< tlno
) {
853 struct blame_entry
*next
= e
->next
;
855 * current record starts before differing portion. If
856 * it reaches into it, we need to split it up and
857 * examine the second part separately.
859 if (e
->s_lno
+ e
->num_lines
> tlno
) {
860 /* Move second half to a new record */
861 int len
= tlno
- e
->s_lno
;
862 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
863 n
->suspect
= e
->suspect
;
864 n
->lno
= e
->lno
+ len
;
865 n
->s_lno
= e
->s_lno
+ len
;
866 n
->num_lines
= e
->num_lines
- len
;
869 /* Push new record to diffp */
873 blame_origin_decref(e
->suspect
);
874 /* Pass blame for everything before the differing
875 * chunk to the parent */
876 e
->suspect
= blame_origin_incref(parent
);
883 * As we don't know how much of a common stretch after this
884 * diff will occur, the currently blamed parts are all that we
885 * can assign to the parent for now.
889 **dstq
= reverse_blame(samep
, **dstq
);
890 *dstq
= &samep
->next
;
893 * Prepend the split off portions: everything after e starts
894 * after the blameable portion.
896 e
= reverse_blame(diffp
, e
);
899 * Now retain records on the target while parts are different
904 while (e
&& e
->s_lno
< same
) {
905 struct blame_entry
*next
= e
->next
;
908 * If current record extends into sameness, need to split.
910 if (e
->s_lno
+ e
->num_lines
> same
) {
912 * Move second half to a new record to be
913 * processed by later chunks
915 int len
= same
- e
->s_lno
;
916 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
917 n
->suspect
= blame_origin_incref(e
->suspect
);
918 n
->lno
= e
->lno
+ len
;
919 n
->s_lno
= e
->s_lno
+ len
;
920 n
->num_lines
= e
->num_lines
- len
;
923 /* Push new record to samep */
931 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
932 /* Move across elements that are in the unblamable portion */
934 *srcq
= &diffp
->next
;
937 struct blame_chunk_cb_data
{
938 struct blame_origin
*parent
;
940 struct blame_entry
**dstq
;
941 struct blame_entry
**srcq
;
944 /* diff chunks are from parent to target */
945 static int blame_chunk_cb(long start_a
, long count_a
,
946 long start_b
, long count_b
, void *data
)
948 struct blame_chunk_cb_data
*d
= data
;
949 if (start_a
- start_b
!= d
->offset
)
950 die("internal error in blame::blame_chunk_cb");
951 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
952 start_b
+ count_b
, d
->parent
);
953 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
958 * We are looking at the origin 'target' and aiming to pass blame
959 * for the lines it is suspected to its parent. Run diff to find
960 * which lines came from parent and pass blame for them.
962 static void pass_blame_to_parent(struct blame_scoreboard
*sb
,
963 struct blame_origin
*target
,
964 struct blame_origin
*parent
)
966 mmfile_t file_p
, file_o
;
967 struct blame_chunk_cb_data d
;
968 struct blame_entry
*newdest
= NULL
;
970 if (!target
->suspects
)
971 return; /* nothing remains for this target */
975 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
977 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
978 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
, &sb
->num_read_blob
);
981 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, sb
->xdl_opts
))
982 die("unable to generate diff (%s -> %s)",
983 oid_to_hex(&parent
->commit
->object
.oid
),
984 oid_to_hex(&target
->commit
->object
.oid
));
985 /* The rest are the same as the parent */
986 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
988 queue_blames(sb
, parent
, newdest
);
994 * The lines in blame_entry after splitting blames many times can become
995 * very small and trivial, and at some point it becomes pointless to
996 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
997 * ordinary C program, and it is not worth to say it was copied from
998 * totally unrelated file in the parent.
1000 * Compute how trivial the lines in the blame_entry are.
1002 unsigned blame_entry_score(struct blame_scoreboard
*sb
, struct blame_entry
*e
)
1005 const char *cp
, *ep
;
1011 cp
= blame_nth_line(sb
, e
->lno
);
1012 ep
= blame_nth_line(sb
, e
->lno
+ e
->num_lines
);
1014 unsigned ch
= *((unsigned char *)cp
);
1024 * best_so_far[] and potential[] are both a split of an existing blame_entry
1025 * that passes blame to the parent. Maintain best_so_far the best split so
1026 * far, by comparing potential and best_so_far and copying potential into
1027 * bst_so_far as needed.
1029 static void copy_split_if_better(struct blame_scoreboard
*sb
,
1030 struct blame_entry
*best_so_far
,
1031 struct blame_entry
*potential
)
1035 if (!potential
[1].suspect
)
1037 if (best_so_far
[1].suspect
) {
1038 if (blame_entry_score(sb
, &potential
[1]) <
1039 blame_entry_score(sb
, &best_so_far
[1]))
1043 for (i
= 0; i
< 3; i
++)
1044 blame_origin_incref(potential
[i
].suspect
);
1045 decref_split(best_so_far
);
1046 memcpy(best_so_far
, potential
, sizeof(struct blame_entry
[3]));
1050 * We are looking at a part of the final image represented by
1051 * ent (tlno and same are offset by ent->s_lno).
1052 * tlno is where we are looking at in the final image.
1053 * up to (but not including) same match preimage.
1054 * plno is where we are looking at in the preimage.
1056 * <-------------- final image ---------------------->
1059 * <---------preimage----->
1062 * All line numbers are 0-based.
1064 static void handle_split(struct blame_scoreboard
*sb
,
1065 struct blame_entry
*ent
,
1066 int tlno
, int plno
, int same
,
1067 struct blame_origin
*parent
,
1068 struct blame_entry
*split
)
1070 if (ent
->num_lines
<= tlno
)
1073 struct blame_entry potential
[3];
1076 split_overlap(potential
, ent
, tlno
, plno
, same
, parent
);
1077 copy_split_if_better(sb
, split
, potential
);
1078 decref_split(potential
);
1082 struct handle_split_cb_data
{
1083 struct blame_scoreboard
*sb
;
1084 struct blame_entry
*ent
;
1085 struct blame_origin
*parent
;
1086 struct blame_entry
*split
;
1091 static int handle_split_cb(long start_a
, long count_a
,
1092 long start_b
, long count_b
, void *data
)
1094 struct handle_split_cb_data
*d
= data
;
1095 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1097 d
->plno
= start_a
+ count_a
;
1098 d
->tlno
= start_b
+ count_b
;
1103 * Find the lines from parent that are the same as ent so that
1104 * we can pass blames to it. file_p has the blob contents for
1107 static void find_copy_in_blob(struct blame_scoreboard
*sb
,
1108 struct blame_entry
*ent
,
1109 struct blame_origin
*parent
,
1110 struct blame_entry
*split
,
1115 struct handle_split_cb_data d
;
1117 memset(&d
, 0, sizeof(d
));
1118 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1120 * Prepare mmfile that contains only the lines in ent.
1122 cp
= blame_nth_line(sb
, ent
->lno
);
1123 file_o
.ptr
= (char *) cp
;
1124 file_o
.size
= blame_nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1127 * file_o is a part of final image we are annotating.
1128 * file_p partially may match that image.
1130 memset(split
, 0, sizeof(struct blame_entry
[3]));
1131 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, sb
->xdl_opts
))
1132 die("unable to generate diff (%s)",
1133 oid_to_hex(&parent
->commit
->object
.oid
));
1134 /* remainder, if any, all match the preimage */
1135 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1138 /* Move all blame entries from list *source that have a score smaller
1139 * than score_min to the front of list *small.
1140 * Returns a pointer to the link pointing to the old head of the small list.
1143 static struct blame_entry
**filter_small(struct blame_scoreboard
*sb
,
1144 struct blame_entry
**small
,
1145 struct blame_entry
**source
,
1148 struct blame_entry
*p
= *source
;
1149 struct blame_entry
*oldsmall
= *small
;
1151 if (blame_entry_score(sb
, p
) <= score_min
) {
1167 * See if lines currently target is suspected for can be attributed to
1170 static void find_move_in_parent(struct blame_scoreboard
*sb
,
1171 struct blame_entry
***blamed
,
1172 struct blame_entry
**toosmall
,
1173 struct blame_origin
*target
,
1174 struct blame_origin
*parent
)
1176 struct blame_entry
*e
, split
[3];
1177 struct blame_entry
*unblamed
= target
->suspects
;
1178 struct blame_entry
*leftover
= NULL
;
1182 return; /* nothing remains for this target */
1184 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
1188 /* At each iteration, unblamed has a NULL-terminated list of
1189 * entries that have not yet been tested for blame. leftover
1190 * contains the reversed list of entries that have been tested
1191 * without being assignable to the parent.
1194 struct blame_entry
**unblamedtail
= &unblamed
;
1195 struct blame_entry
*next
;
1196 for (e
= unblamed
; e
; e
= next
) {
1198 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1199 if (split
[1].suspect
&&
1200 sb
->move_score
< blame_entry_score(sb
, &split
[1])) {
1201 split_blame(blamed
, &unblamedtail
, split
, e
);
1206 decref_split(split
);
1208 *unblamedtail
= NULL
;
1209 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->move_score
);
1211 target
->suspects
= reverse_blame(leftover
, NULL
);
1215 struct blame_entry
*ent
;
1216 struct blame_entry split
[3];
1220 * Count the number of entries the target is suspected for,
1221 * and prepare a list of entry and the best split.
1223 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1226 struct blame_entry
*e
;
1228 struct blame_list
*blame_list
= NULL
;
1230 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1233 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1234 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1235 blame_list
[i
++].ent
= e
;
1237 *num_ents_p
= num_ents
;
1242 * For lines target is suspected for, see if we can find code movement
1243 * across file boundary from the parent commit. porigin is the path
1244 * in the parent we already tried.
1246 static void find_copy_in_parent(struct blame_scoreboard
*sb
,
1247 struct blame_entry
***blamed
,
1248 struct blame_entry
**toosmall
,
1249 struct blame_origin
*target
,
1250 struct commit
*parent
,
1251 struct blame_origin
*porigin
,
1254 struct diff_options diff_opts
;
1256 struct blame_list
*blame_list
;
1258 struct blame_entry
*unblamed
= target
->suspects
;
1259 struct blame_entry
*leftover
= NULL
;
1262 return; /* nothing remains for this target */
1264 diff_setup(&diff_opts
);
1265 diff_opts
.flags
.recursive
= 1;
1266 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1268 diff_setup_done(&diff_opts
);
1270 /* Try "find copies harder" on new path if requested;
1271 * we do not want to use diffcore_rename() actually to
1272 * match things up; find_copies_harder is set only to
1273 * force diff_tree_oid() to feed all filepairs to diff_queue,
1274 * and this code needs to be after diff_setup_done(), which
1275 * usually makes find-copies-harder imply copy detection.
1277 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1278 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1279 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1280 diff_opts
.flags
.find_copies_harder
= 1;
1282 if (is_null_oid(&target
->commit
->object
.oid
))
1283 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
1285 diff_tree_oid(get_commit_tree_oid(parent
),
1286 get_commit_tree_oid(target
->commit
),
1289 if (!diff_opts
.flags
.find_copies_harder
)
1290 diffcore_std(&diff_opts
);
1293 struct blame_entry
**unblamedtail
= &unblamed
;
1294 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1296 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1297 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1298 struct blame_origin
*norigin
;
1300 struct blame_entry potential
[3];
1302 if (!DIFF_FILE_VALID(p
->one
))
1303 continue; /* does not exist in parent */
1304 if (S_ISGITLINK(p
->one
->mode
))
1305 continue; /* ignore git links */
1306 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1307 /* find_move already dealt with this path */
1310 norigin
= get_origin(parent
, p
->one
->path
);
1311 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
1312 norigin
->mode
= p
->one
->mode
;
1313 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
, &sb
->num_read_blob
);
1317 for (j
= 0; j
< num_ents
; j
++) {
1318 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1319 norigin
, potential
, &file_p
);
1320 copy_split_if_better(sb
, blame_list
[j
].split
,
1322 decref_split(potential
);
1324 blame_origin_decref(norigin
);
1327 for (j
= 0; j
< num_ents
; j
++) {
1328 struct blame_entry
*split
= blame_list
[j
].split
;
1329 if (split
[1].suspect
&&
1330 sb
->copy_score
< blame_entry_score(sb
, &split
[1])) {
1331 split_blame(blamed
, &unblamedtail
, split
,
1334 blame_list
[j
].ent
->next
= leftover
;
1335 leftover
= blame_list
[j
].ent
;
1337 decref_split(split
);
1340 *unblamedtail
= NULL
;
1341 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->copy_score
);
1343 target
->suspects
= reverse_blame(leftover
, NULL
);
1344 diff_flush(&diff_opts
);
1345 clear_pathspec(&diff_opts
.pathspec
);
1349 * The blobs of origin and porigin exactly match, so everything
1350 * origin is suspected for can be blamed on the parent.
1352 static void pass_whole_blame(struct blame_scoreboard
*sb
,
1353 struct blame_origin
*origin
, struct blame_origin
*porigin
)
1355 struct blame_entry
*e
, *suspects
;
1357 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1358 /* Steal its file */
1359 porigin
->file
= origin
->file
;
1360 origin
->file
.ptr
= NULL
;
1362 suspects
= origin
->suspects
;
1363 origin
->suspects
= NULL
;
1364 for (e
= suspects
; e
; e
= e
->next
) {
1365 blame_origin_incref(porigin
);
1366 blame_origin_decref(e
->suspect
);
1367 e
->suspect
= porigin
;
1369 queue_blames(sb
, porigin
, suspects
);
1373 * We pass blame from the current commit to its parents. We keep saying
1374 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1375 * exonerate ourselves.
1377 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
,
1381 if (revs
->first_parent_only
&&
1383 commit
->parents
->next
) {
1384 free_commit_list(commit
->parents
->next
);
1385 commit
->parents
->next
= NULL
;
1387 return commit
->parents
;
1389 return lookup_decoration(&revs
->children
, &commit
->object
);
1392 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
, int reverse
)
1394 struct commit_list
*l
= first_scapegoat(revs
, commit
, reverse
);
1395 return commit_list_count(l
);
1398 /* Distribute collected unsorted blames to the respected sorted lists
1399 * in the various origins.
1401 static void distribute_blame(struct blame_scoreboard
*sb
, struct blame_entry
*blamed
)
1403 blamed
= llist_mergesort(blamed
, get_next_blame
, set_next_blame
,
1404 compare_blame_suspect
);
1407 struct blame_origin
*porigin
= blamed
->suspect
;
1408 struct blame_entry
*suspects
= NULL
;
1410 struct blame_entry
*next
= blamed
->next
;
1411 blamed
->next
= suspects
;
1414 } while (blamed
&& blamed
->suspect
== porigin
);
1415 suspects
= reverse_blame(suspects
, NULL
);
1416 queue_blames(sb
, porigin
, suspects
);
1422 static void pass_blame(struct blame_scoreboard
*sb
, struct blame_origin
*origin
, int opt
)
1424 struct rev_info
*revs
= sb
->revs
;
1425 int i
, pass
, num_sg
;
1426 struct commit
*commit
= origin
->commit
;
1427 struct commit_list
*sg
;
1428 struct blame_origin
*sg_buf
[MAXSG
];
1429 struct blame_origin
*porigin
, **sg_origin
= sg_buf
;
1430 struct blame_entry
*toosmall
= NULL
;
1431 struct blame_entry
*blames
, **blametail
= &blames
;
1433 num_sg
= num_scapegoats(revs
, commit
, sb
->reverse
);
1436 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1437 memset(sg_buf
, 0, sizeof(sg_buf
));
1439 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1442 * The first pass looks for unrenamed path to optimize for
1443 * common cases, then we look for renames in the second pass.
1445 for (pass
= 0; pass
< 2 - sb
->no_whole_file_rename
; pass
++) {
1446 struct blame_origin
*(*find
)(struct commit
*, struct blame_origin
*);
1447 find
= pass
? find_rename
: find_origin
;
1449 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1451 sg
= sg
->next
, i
++) {
1452 struct commit
*p
= sg
->item
;
1457 if (parse_commit(p
))
1459 porigin
= find(p
, origin
);
1462 if (!oidcmp(&porigin
->blob_oid
, &origin
->blob_oid
)) {
1463 pass_whole_blame(sb
, origin
, porigin
);
1464 blame_origin_decref(porigin
);
1467 for (j
= same
= 0; j
< i
; j
++)
1469 !oidcmp(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
1474 sg_origin
[i
] = porigin
;
1476 blame_origin_decref(porigin
);
1481 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1483 sg
= sg
->next
, i
++) {
1484 struct blame_origin
*porigin
= sg_origin
[i
];
1487 if (!origin
->previous
) {
1488 blame_origin_incref(porigin
);
1489 origin
->previous
= porigin
;
1491 pass_blame_to_parent(sb
, origin
, porigin
);
1492 if (!origin
->suspects
)
1497 * Optionally find moves in parents' files.
1499 if (opt
& PICKAXE_BLAME_MOVE
) {
1500 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->move_score
);
1501 if (origin
->suspects
) {
1502 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1504 sg
= sg
->next
, i
++) {
1505 struct blame_origin
*porigin
= sg_origin
[i
];
1508 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1509 if (!origin
->suspects
)
1516 * Optionally find copies from parents' files.
1518 if (opt
& PICKAXE_BLAME_COPY
) {
1519 if (sb
->copy_score
> sb
->move_score
)
1520 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1521 else if (sb
->copy_score
< sb
->move_score
) {
1522 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1524 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1526 if (!origin
->suspects
)
1529 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1531 sg
= sg
->next
, i
++) {
1532 struct blame_origin
*porigin
= sg_origin
[i
];
1533 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1534 origin
, sg
->item
, porigin
, opt
);
1535 if (!origin
->suspects
)
1542 distribute_blame(sb
, blames
);
1544 * prepend toosmall to origin->suspects
1546 * There is no point in sorting: this ends up on a big
1547 * unsorted list in the caller anyway.
1550 struct blame_entry
**tail
= &toosmall
;
1552 tail
= &(*tail
)->next
;
1553 *tail
= origin
->suspects
;
1554 origin
->suspects
= toosmall
;
1556 for (i
= 0; i
< num_sg
; i
++) {
1558 drop_origin_blob(sg_origin
[i
]);
1559 blame_origin_decref(sg_origin
[i
]);
1562 drop_origin_blob(origin
);
1563 if (sg_buf
!= sg_origin
)
1568 * The main loop -- while we have blobs with lines whose true origin
1569 * is still unknown, pick one blob, and allow its lines to pass blames
1570 * to its parents. */
1571 void assign_blame(struct blame_scoreboard
*sb
, int opt
)
1573 struct rev_info
*revs
= sb
->revs
;
1574 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1577 struct blame_entry
*ent
;
1578 struct blame_origin
*suspect
= get_blame_suspects(commit
);
1580 /* find one suspect to break down */
1581 while (suspect
&& !suspect
->suspects
)
1582 suspect
= suspect
->next
;
1585 commit
= prio_queue_get(&sb
->commits
);
1589 assert(commit
== suspect
->commit
);
1592 * We will use this suspect later in the loop,
1593 * so hold onto it in the meantime.
1595 blame_origin_incref(suspect
);
1596 parse_commit(commit
);
1598 (!(commit
->object
.flags
& UNINTERESTING
) &&
1599 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1600 pass_blame(sb
, suspect
, opt
);
1602 commit
->object
.flags
|= UNINTERESTING
;
1603 if (commit
->object
.parsed
)
1604 mark_parents_uninteresting(commit
);
1606 /* treat root commit as boundary */
1607 if (!commit
->parents
&& !sb
->show_root
)
1608 commit
->object
.flags
|= UNINTERESTING
;
1610 /* Take responsibility for the remaining entries */
1611 ent
= suspect
->suspects
;
1613 suspect
->guilty
= 1;
1615 struct blame_entry
*next
= ent
->next
;
1616 if (sb
->found_guilty_entry
)
1617 sb
->found_guilty_entry(ent
, sb
->found_guilty_entry_data
);
1622 ent
->next
= sb
->ent
;
1623 sb
->ent
= suspect
->suspects
;
1624 suspect
->suspects
= NULL
;
1628 blame_origin_decref(suspect
);
1630 if (sb
->debug
) /* sanity */
1631 sanity_check_refcnt(sb
);
1635 static const char *get_next_line(const char *start
, const char *end
)
1637 const char *nl
= memchr(start
, '\n', end
- start
);
1638 return nl
? nl
+ 1 : end
;
1642 * To allow quick access to the contents of nth line in the
1643 * final image, prepare an index in the scoreboard.
1645 static int prepare_lines(struct blame_scoreboard
*sb
)
1647 const char *buf
= sb
->final_buf
;
1648 unsigned long len
= sb
->final_buf_size
;
1649 const char *end
= buf
+ len
;
1654 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1657 ALLOC_ARRAY(sb
->lineno
, num
+ 1);
1658 lineno
= sb
->lineno
;
1660 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1661 *lineno
++ = p
- buf
;
1665 sb
->num_lines
= num
;
1666 return sb
->num_lines
;
1669 static struct commit
*find_single_final(struct rev_info
*revs
,
1670 const char **name_p
)
1673 struct commit
*found
= NULL
;
1674 const char *name
= NULL
;
1676 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1677 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1678 if (obj
->flags
& UNINTERESTING
)
1680 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
1681 if (obj
->type
!= OBJ_COMMIT
)
1682 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1684 die("More than one commit to dig from %s and %s?",
1685 revs
->pending
.objects
[i
].name
, name
);
1686 found
= (struct commit
*)obj
;
1687 name
= revs
->pending
.objects
[i
].name
;
1690 *name_p
= xstrdup_or_null(name
);
1694 static struct commit
*dwim_reverse_initial(struct rev_info
*revs
,
1695 const char **name_p
)
1698 * DWIM "git blame --reverse ONE -- PATH" as
1699 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1700 * when it makes sense.
1703 struct commit
*head_commit
;
1704 struct object_id head_oid
;
1706 if (revs
->pending
.nr
!= 1)
1709 /* Is that sole rev a committish? */
1710 obj
= revs
->pending
.objects
[0].item
;
1711 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
1712 if (obj
->type
!= OBJ_COMMIT
)
1715 /* Do we have HEAD? */
1716 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
1718 head_commit
= lookup_commit_reference_gently(the_repository
,
1723 /* Turn "ONE" into "ONE..HEAD" then */
1724 obj
->flags
|= UNINTERESTING
;
1725 add_pending_object(revs
, &head_commit
->object
, "HEAD");
1728 *name_p
= revs
->pending
.objects
[0].name
;
1729 return (struct commit
*)obj
;
1732 static struct commit
*find_single_initial(struct rev_info
*revs
,
1733 const char **name_p
)
1736 struct commit
*found
= NULL
;
1737 const char *name
= NULL
;
1740 * There must be one and only one negative commit, and it must be
1743 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1744 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1745 if (!(obj
->flags
& UNINTERESTING
))
1747 obj
= deref_tag(the_repository
, obj
, NULL
, 0);
1748 if (obj
->type
!= OBJ_COMMIT
)
1749 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1751 die("More than one commit to dig up from, %s and %s?",
1752 revs
->pending
.objects
[i
].name
, name
);
1753 found
= (struct commit
*) obj
;
1754 name
= revs
->pending
.objects
[i
].name
;
1758 found
= dwim_reverse_initial(revs
, &name
);
1760 die("No commit to dig up from?");
1763 *name_p
= xstrdup(name
);
1767 void init_scoreboard(struct blame_scoreboard
*sb
)
1769 memset(sb
, 0, sizeof(struct blame_scoreboard
));
1770 sb
->move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1771 sb
->copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1774 void setup_scoreboard(struct blame_scoreboard
*sb
,
1776 struct blame_origin
**orig
)
1778 const char *final_commit_name
= NULL
;
1779 struct blame_origin
*o
;
1780 struct commit
*final_commit
= NULL
;
1781 enum object_type type
;
1783 init_blame_suspects(&blame_suspects
);
1785 if (sb
->reverse
&& sb
->contents_from
)
1786 die(_("--contents and --reverse do not blend well."));
1789 BUG("repo is NULL");
1792 sb
->final
= find_single_final(sb
->revs
, &final_commit_name
);
1793 sb
->commits
.compare
= compare_commits_by_commit_date
;
1795 sb
->final
= find_single_initial(sb
->revs
, &final_commit_name
);
1796 sb
->commits
.compare
= compare_commits_by_reverse_commit_date
;
1799 if (sb
->final
&& sb
->contents_from
)
1800 die(_("cannot use --contents with final commit object name"));
1802 if (sb
->reverse
&& sb
->revs
->first_parent_only
)
1803 sb
->revs
->children
.name
= NULL
;
1807 * "--not A B -- path" without anything positive;
1808 * do not default to HEAD, but use the working tree
1812 sb
->final
= fake_working_tree_commit(sb
->repo
,
1814 path
, sb
->contents_from
);
1815 add_pending_object(sb
->revs
, &(sb
->final
->object
), ":");
1818 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1819 final_commit
= find_single_final(sb
->revs
, NULL
);
1821 die(_("--reverse and --first-parent together require specified latest commit"));
1825 * If we have bottom, this will mark the ancestors of the
1826 * bottom commits we would reach while traversing as
1829 if (prepare_revision_walk(sb
->revs
))
1830 die(_("revision walk setup failed"));
1832 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1833 struct commit
*c
= final_commit
;
1835 sb
->revs
->children
.name
= "children";
1836 while (c
->parents
&&
1837 oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
)) {
1838 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1841 if (add_decoration(&sb
->revs
->children
,
1842 &c
->parents
->item
->object
, l
))
1843 BUG("not unique item in first-parent chain");
1844 c
= c
->parents
->item
;
1847 if (oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
))
1848 die(_("--reverse --first-parent together require range along first-parent chain"));
1851 if (is_null_oid(&sb
->final
->object
.oid
)) {
1852 o
= get_blame_suspects(sb
->final
);
1853 sb
->final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
1854 sb
->final_buf_size
= o
->file
.size
;
1857 o
= get_origin(sb
->final
, path
);
1858 if (fill_blob_sha1_and_mode(sb
->repo
, o
))
1859 die(_("no such path %s in %s"), path
, final_commit_name
);
1861 if (sb
->revs
->diffopt
.flags
.allow_textconv
&&
1862 textconv_object(path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
->final_buf
,
1863 &sb
->final_buf_size
))
1866 sb
->final_buf
= read_object_file(&o
->blob_oid
, &type
,
1867 &sb
->final_buf_size
);
1870 die(_("cannot read blob %s for path %s"),
1871 oid_to_hex(&o
->blob_oid
),
1874 sb
->num_read_blob
++;
1880 free((char *)final_commit_name
);
1885 struct blame_entry
*blame_entry_prepend(struct blame_entry
*head
,
1886 long start
, long end
,
1887 struct blame_origin
*o
)
1889 struct blame_entry
*new_head
= xcalloc(1, sizeof(struct blame_entry
));
1890 new_head
->lno
= start
;
1891 new_head
->num_lines
= end
- start
;
1892 new_head
->suspect
= o
;
1893 new_head
->s_lno
= start
;
1894 new_head
->next
= head
;
1895 blame_origin_incref(o
);