3 #include "cache-tree.h"
10 void blame_origin_decref(struct blame_origin
*o
)
12 if (o
&& --o
->refcnt
<= 0) {
13 struct blame_origin
*p
, *l
= NULL
;
15 blame_origin_decref(o
->previous
);
17 /* Should be present exactly once in commit chain */
18 for (p
= o
->commit
->util
; p
; l
= p
, p
= p
->next
) {
23 o
->commit
->util
= p
->next
;
28 die("internal error in blame_origin_decref");
33 * Given a commit and a path in it, create a new origin structure.
34 * The callers that add blame to the scoreboard should use
35 * get_origin() to obtain shared, refcounted copy instead of calling
36 * this function directly.
38 static struct blame_origin
*make_origin(struct commit
*commit
, const char *path
)
40 struct blame_origin
*o
;
41 FLEX_ALLOC_STR(o
, path
, path
);
44 o
->next
= commit
->util
;
50 * Locate an existing origin or create a new one.
51 * This moves the origin to front position in the commit util list.
53 static struct blame_origin
*get_origin(struct commit
*commit
, const char *path
)
55 struct blame_origin
*o
, *l
;
57 for (o
= commit
->util
, l
= NULL
; o
; l
= o
, o
= o
->next
) {
58 if (!strcmp(o
->path
, path
)) {
62 o
->next
= commit
->util
;
65 return blame_origin_incref(o
);
68 return make_origin(commit
, path
);
73 static void verify_working_tree_path(struct commit
*work_tree
, const char *path
)
75 struct commit_list
*parents
;
78 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
79 const struct object_id
*commit_oid
= &parents
->item
->object
.oid
;
80 struct object_id blob_oid
;
83 if (!get_tree_entry(commit_oid
->hash
, path
, blob_oid
.hash
, &mode
) &&
84 sha1_object_info(blob_oid
.hash
, NULL
) == OBJ_BLOB
)
88 pos
= cache_name_pos(path
, strlen(path
));
90 ; /* path is in the index */
91 else if (-1 - pos
< active_nr
&&
92 !strcmp(active_cache
[-1 - pos
]->name
, path
))
93 ; /* path is in the index, unmerged */
95 die("no such path '%s' in HEAD", path
);
98 static struct commit_list
**append_parent(struct commit_list
**tail
, const struct object_id
*oid
)
100 struct commit
*parent
;
102 parent
= lookup_commit_reference(oid
);
104 die("no such commit %s", oid_to_hex(oid
));
105 return &commit_list_insert(parent
, tail
)->next
;
108 static void append_merge_parents(struct commit_list
**tail
)
111 struct strbuf line
= STRBUF_INIT
;
113 merge_head
= open(git_path_merge_head(), O_RDONLY
);
114 if (merge_head
< 0) {
117 die("cannot open '%s' for reading", git_path_merge_head());
120 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
121 struct object_id oid
;
122 if (line
.len
< GIT_SHA1_HEXSZ
|| get_oid_hex(line
.buf
, &oid
))
123 die("unknown line in '%s': %s", git_path_merge_head(), line
.buf
);
124 tail
= append_parent(tail
, &oid
);
127 strbuf_release(&line
);
131 * This isn't as simple as passing sb->buf and sb->len, because we
132 * want to transfer ownership of the buffer to the commit (so we
135 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
138 void *buf
= strbuf_detach(sb
, &len
);
139 set_commit_buffer(c
, buf
, len
);
143 * Prepare a dummy commit that represents the work tree (or staged) item.
144 * Note that annotating work tree item never works in the reverse.
146 static struct commit
*fake_working_tree_commit(struct diff_options
*opt
,
148 const char *contents_from
)
150 struct commit
*commit
;
151 struct blame_origin
*origin
;
152 struct commit_list
**parent_tail
, *parent
;
153 struct object_id head_oid
;
154 struct strbuf buf
= STRBUF_INIT
;
158 struct cache_entry
*ce
;
160 struct strbuf msg
= STRBUF_INIT
;
164 commit
= alloc_commit_node();
165 commit
->object
.parsed
= 1;
167 parent_tail
= &commit
->parents
;
169 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
170 die("no such ref: HEAD");
172 parent_tail
= append_parent(parent_tail
, &head_oid
);
173 append_merge_parents(parent_tail
);
174 verify_working_tree_path(commit
, path
);
176 origin
= make_origin(commit
, path
);
178 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
179 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
180 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
181 strbuf_addf(&msg
, "parent %s\n",
182 oid_to_hex(&parent
->item
->object
.oid
));
186 "Version of %s from %s\n",
188 (!contents_from
? path
:
189 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
190 set_commit_buffer_from_strbuf(commit
, &msg
);
192 if (!contents_from
|| strcmp("-", contents_from
)) {
194 const char *read_from
;
196 unsigned long buf_len
;
199 if (stat(contents_from
, &st
) < 0)
200 die_errno("Cannot stat '%s'", contents_from
);
201 read_from
= contents_from
;
204 if (lstat(path
, &st
) < 0)
205 die_errno("Cannot lstat '%s'", path
);
208 mode
= canon_mode(st
.st_mode
);
210 switch (st
.st_mode
& S_IFMT
) {
212 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
213 textconv_object(read_from
, mode
, &null_oid
, 0, &buf_ptr
, &buf_len
))
214 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
215 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
216 die_errno("cannot open or read '%s'", read_from
);
219 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
220 die_errno("cannot readlink '%s'", read_from
);
223 die("unsupported file type %s", read_from
);
227 /* Reading from stdin */
229 if (strbuf_read(&buf
, 0, 0) < 0)
230 die_errno("failed to read from stdin");
232 convert_to_git(&the_index
, path
, buf
.buf
, buf
.len
, &buf
, 0);
233 origin
->file
.ptr
= buf
.buf
;
234 origin
->file
.size
= buf
.len
;
235 pretend_sha1_file(buf
.buf
, buf
.len
, OBJ_BLOB
, origin
->blob_oid
.hash
);
238 * Read the current index, replace the path entry with
239 * origin->blob_sha1 without mucking with its mode or type
240 * bits; we are not going to write this index out -- we just
241 * want to run "diff-index --cached".
248 int pos
= cache_name_pos(path
, len
);
250 mode
= active_cache
[pos
]->ce_mode
;
252 /* Let's not bother reading from HEAD tree */
253 mode
= S_IFREG
| 0644;
255 size
= cache_entry_size(len
);
256 ce
= xcalloc(1, size
);
257 oidcpy(&ce
->oid
, &origin
->blob_oid
);
258 memcpy(ce
->name
, path
, len
);
259 ce
->ce_flags
= create_ce_flags(0);
260 ce
->ce_namelen
= len
;
261 ce
->ce_mode
= create_ce_mode(mode
);
262 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
264 cache_tree_invalidate_path(&the_index
, path
);
271 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
272 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
, int xdl_opts
)
275 xdemitconf_t xecfg
= {0};
276 xdemitcb_t ecb
= {NULL
};
278 xpp
.flags
= xdl_opts
;
279 xecfg
.hunk_func
= hunk_func
;
281 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
285 * Given an origin, prepare mmfile_t structure to be used by the
288 static void fill_origin_blob(struct diff_options
*opt
,
289 struct blame_origin
*o
, mmfile_t
*file
, int *num_read_blob
)
292 enum object_type type
;
293 unsigned long file_size
;
296 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
) &&
297 textconv_object(o
->path
, o
->mode
, &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
300 file
->ptr
= read_sha1_file(o
->blob_oid
.hash
, &type
,
302 file
->size
= file_size
;
305 die("Cannot read blob %s for path %s",
306 oid_to_hex(&o
->blob_oid
),
314 static void drop_origin_blob(struct blame_origin
*o
)
317 FREE_AND_NULL(o
->file
.ptr
);
322 * Any merge of blames happens on lists of blames that arrived via
323 * different parents in a single suspect. In this case, we want to
324 * sort according to the suspect line numbers as opposed to the final
325 * image line numbers. The function body is somewhat longish because
326 * it avoids unnecessary writes.
329 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
330 struct blame_entry
*list2
)
332 struct blame_entry
*p1
= list1
, *p2
= list2
,
340 if (p1
->s_lno
<= p2
->s_lno
) {
343 if ((p1
= *tail
) == NULL
) {
347 } while (p1
->s_lno
<= p2
->s_lno
);
353 if ((p2
= *tail
) == NULL
) {
357 } while (p1
->s_lno
> p2
->s_lno
);
361 if ((p1
= *tail
) == NULL
) {
365 } while (p1
->s_lno
<= p2
->s_lno
);
369 static void *get_next_blame(const void *p
)
371 return ((struct blame_entry
*)p
)->next
;
374 static void set_next_blame(void *p1
, void *p2
)
376 ((struct blame_entry
*)p1
)->next
= p2
;
380 * Final image line numbers are all different, so we don't need a
381 * three-way comparison here.
384 static int compare_blame_final(const void *p1
, const void *p2
)
386 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
390 static int compare_blame_suspect(const void *p1
, const void *p2
)
392 const struct blame_entry
*s1
= p1
, *s2
= p2
;
394 * to allow for collating suspects, we sort according to the
395 * respective pointer value as the primary sorting criterion.
396 * The actual relation is pretty unimportant as long as it
397 * establishes a total order. Comparing as integers gives us
400 if (s1
->suspect
!= s2
->suspect
)
401 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
402 if (s1
->s_lno
== s2
->s_lno
)
404 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
407 void blame_sort_final(struct blame_scoreboard
*sb
)
409 sb
->ent
= llist_mergesort(sb
->ent
, get_next_blame
, set_next_blame
,
410 compare_blame_final
);
413 static int compare_commits_by_reverse_commit_date(const void *a
,
417 return -compare_commits_by_commit_date(a
, b
, c
);
421 * For debugging -- origin is refcounted, and this asserts that
422 * we do not underflow.
424 static void sanity_check_refcnt(struct blame_scoreboard
*sb
)
427 struct blame_entry
*ent
;
429 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
430 /* Nobody should have zero or negative refcnt */
431 if (ent
->suspect
->refcnt
<= 0) {
432 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
434 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
435 ent
->suspect
->refcnt
);
440 sb
->on_sanity_fail(sb
, baa
);
444 * If two blame entries that are next to each other came from
445 * contiguous lines in the same origin (i.e. <commit, path> pair),
446 * merge them together.
448 void blame_coalesce(struct blame_scoreboard
*sb
)
450 struct blame_entry
*ent
, *next
;
452 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
453 if (ent
->suspect
== next
->suspect
&&
454 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
455 ent
->num_lines
+= next
->num_lines
;
456 ent
->next
= next
->next
;
457 blame_origin_decref(next
->suspect
);
460 next
= ent
; /* again */
464 if (sb
->debug
) /* sanity */
465 sanity_check_refcnt(sb
);
469 * Merge the given sorted list of blames into a preexisting origin.
470 * If there were no previous blames to that commit, it is entered into
471 * the commit priority queue of the score board.
474 static void queue_blames(struct blame_scoreboard
*sb
, struct blame_origin
*porigin
,
475 struct blame_entry
*sorted
)
477 if (porigin
->suspects
)
478 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
480 struct blame_origin
*o
;
481 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
483 porigin
->suspects
= sorted
;
487 porigin
->suspects
= sorted
;
488 prio_queue_put(&sb
->commits
, porigin
->commit
);
493 * Fill the blob_sha1 field of an origin if it hasn't, so that later
494 * call to fill_origin_blob() can use it to locate the data. blob_sha1
495 * for an origin is also used to pass the blame for the entire file to
496 * the parent to detect the case where a child's blob is identical to
497 * that of its parent's.
499 * This also fills origin->mode for corresponding tree path.
501 static int fill_blob_sha1_and_mode(struct blame_origin
*origin
)
503 if (!is_null_oid(&origin
->blob_oid
))
505 if (get_tree_entry(origin
->commit
->object
.oid
.hash
,
507 origin
->blob_oid
.hash
, &origin
->mode
))
509 if (sha1_object_info(origin
->blob_oid
.hash
, NULL
) != OBJ_BLOB
)
513 oidclr(&origin
->blob_oid
);
514 origin
->mode
= S_IFINVALID
;
519 * We have an origin -- check if the same path exists in the
520 * parent and return an origin structure to represent it.
522 static struct blame_origin
*find_origin(struct commit
*parent
,
523 struct blame_origin
*origin
)
525 struct blame_origin
*porigin
;
526 struct diff_options diff_opts
;
527 const char *paths
[2];
529 /* First check any existing origins */
530 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
531 if (!strcmp(porigin
->path
, origin
->path
)) {
533 * The same path between origin and its parent
534 * without renaming -- the most common case.
536 return blame_origin_incref (porigin
);
539 /* See if the origin->path is different between parent
540 * and origin first. Most of the time they are the
541 * same and diff-tree is fairly efficient about this.
543 diff_setup(&diff_opts
);
544 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
545 diff_opts
.detect_rename
= 0;
546 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
547 paths
[0] = origin
->path
;
550 parse_pathspec(&diff_opts
.pathspec
,
551 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
552 PATHSPEC_LITERAL_PATH
, "", paths
);
553 diff_setup_done(&diff_opts
);
555 if (is_null_oid(&origin
->commit
->object
.oid
))
556 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
558 diff_tree_oid(&parent
->tree
->object
.oid
,
559 &origin
->commit
->tree
->object
.oid
,
561 diffcore_std(&diff_opts
);
563 if (!diff_queued_diff
.nr
) {
564 /* The path is the same as parent */
565 porigin
= get_origin(parent
, origin
->path
);
566 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
567 porigin
->mode
= origin
->mode
;
570 * Since origin->path is a pathspec, if the parent
571 * commit had it as a directory, we will see a whole
572 * bunch of deletion of files in the directory that we
576 struct diff_filepair
*p
= NULL
;
577 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
579 p
= diff_queued_diff
.queue
[i
];
580 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
581 if (!strcmp(name
, origin
->path
))
585 die("internal error in blame::find_origin");
588 die("internal error in blame::find_origin (%c)",
591 porigin
= get_origin(parent
, origin
->path
);
592 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
593 porigin
->mode
= p
->one
->mode
;
597 /* Did not exist in parent, or type changed */
601 diff_flush(&diff_opts
);
602 clear_pathspec(&diff_opts
.pathspec
);
607 * We have an origin -- find the path that corresponds to it in its
608 * parent and return an origin structure to represent it.
610 static struct blame_origin
*find_rename(struct commit
*parent
,
611 struct blame_origin
*origin
)
613 struct blame_origin
*porigin
= NULL
;
614 struct diff_options diff_opts
;
617 diff_setup(&diff_opts
);
618 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
619 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
620 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
621 diff_opts
.single_follow
= origin
->path
;
622 diff_setup_done(&diff_opts
);
624 if (is_null_oid(&origin
->commit
->object
.oid
))
625 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
627 diff_tree_oid(&parent
->tree
->object
.oid
,
628 &origin
->commit
->tree
->object
.oid
,
630 diffcore_std(&diff_opts
);
632 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
633 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
634 if ((p
->status
== 'R' || p
->status
== 'C') &&
635 !strcmp(p
->two
->path
, origin
->path
)) {
636 porigin
= get_origin(parent
, p
->one
->path
);
637 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
638 porigin
->mode
= p
->one
->mode
;
642 diff_flush(&diff_opts
);
643 clear_pathspec(&diff_opts
.pathspec
);
648 * Append a new blame entry to a given output queue.
650 static void add_blame_entry(struct blame_entry
***queue
,
651 const struct blame_entry
*src
)
653 struct blame_entry
*e
= xmalloc(sizeof(*e
));
654 memcpy(e
, src
, sizeof(*e
));
655 blame_origin_incref(e
->suspect
);
663 * src typically is on-stack; we want to copy the information in it to
664 * a malloced blame_entry that gets added to the given queue. The
665 * origin of dst loses a refcnt.
667 static void dup_entry(struct blame_entry
***queue
,
668 struct blame_entry
*dst
, struct blame_entry
*src
)
670 blame_origin_incref(src
->suspect
);
671 blame_origin_decref(dst
->suspect
);
672 memcpy(dst
, src
, sizeof(*src
));
678 const char *blame_nth_line(struct blame_scoreboard
*sb
, long lno
)
680 return sb
->final_buf
+ sb
->lineno
[lno
];
684 * It is known that lines between tlno to same came from parent, and e
685 * has an overlap with that range. it also is known that parent's
686 * line plno corresponds to e's line tlno.
692 * <------------------>
694 * Split e into potentially three parts; before this chunk, the chunk
695 * to be blamed for the parent, and after that portion.
697 static void split_overlap(struct blame_entry
*split
,
698 struct blame_entry
*e
,
699 int tlno
, int plno
, int same
,
700 struct blame_origin
*parent
)
703 memset(split
, 0, sizeof(struct blame_entry
[3]));
705 if (e
->s_lno
< tlno
) {
706 /* there is a pre-chunk part not blamed on parent */
707 split
[0].suspect
= blame_origin_incref(e
->suspect
);
708 split
[0].lno
= e
->lno
;
709 split
[0].s_lno
= e
->s_lno
;
710 split
[0].num_lines
= tlno
- e
->s_lno
;
711 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
712 split
[1].s_lno
= plno
;
715 split
[1].lno
= e
->lno
;
716 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
719 if (same
< e
->s_lno
+ e
->num_lines
) {
720 /* there is a post-chunk part not blamed on parent */
721 split
[2].suspect
= blame_origin_incref(e
->suspect
);
722 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
723 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
724 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
725 chunk_end_lno
= split
[2].lno
;
728 chunk_end_lno
= e
->lno
+ e
->num_lines
;
729 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
732 * if it turns out there is nothing to blame the parent for,
733 * forget about the splitting. !split[1].suspect signals this.
735 if (split
[1].num_lines
< 1)
737 split
[1].suspect
= blame_origin_incref(parent
);
741 * split_overlap() divided an existing blame e into up to three parts
742 * in split. Any assigned blame is moved to queue to
745 static void split_blame(struct blame_entry
***blamed
,
746 struct blame_entry
***unblamed
,
747 struct blame_entry
*split
,
748 struct blame_entry
*e
)
750 if (split
[0].suspect
&& split
[2].suspect
) {
751 /* The first part (reuse storage for the existing entry e) */
752 dup_entry(unblamed
, e
, &split
[0]);
754 /* The last part -- me */
755 add_blame_entry(unblamed
, &split
[2]);
757 /* ... and the middle part -- parent */
758 add_blame_entry(blamed
, &split
[1]);
760 else if (!split
[0].suspect
&& !split
[2].suspect
)
762 * The parent covers the entire area; reuse storage for
763 * e and replace it with the parent.
765 dup_entry(blamed
, e
, &split
[1]);
766 else if (split
[0].suspect
) {
767 /* me and then parent */
768 dup_entry(unblamed
, e
, &split
[0]);
769 add_blame_entry(blamed
, &split
[1]);
772 /* parent and then me */
773 dup_entry(blamed
, e
, &split
[1]);
774 add_blame_entry(unblamed
, &split
[2]);
779 * After splitting the blame, the origins used by the
780 * on-stack blame_entry should lose one refcnt each.
782 static void decref_split(struct blame_entry
*split
)
786 for (i
= 0; i
< 3; i
++)
787 blame_origin_decref(split
[i
].suspect
);
791 * reverse_blame reverses the list given in head, appending tail.
792 * That allows us to build lists in reverse order, then reverse them
793 * afterwards. This can be faster than building the list in proper
794 * order right away. The reason is that building in proper order
795 * requires writing a link in the _previous_ element, while building
796 * in reverse order just requires placing the list head into the
800 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
801 struct blame_entry
*tail
)
804 struct blame_entry
*next
= head
->next
;
813 * Process one hunk from the patch between the current suspect for
814 * blame_entry e and its parent. This first blames any unfinished
815 * entries before the chunk (which is where target and parent start
816 * differing) on the parent, and then splits blame entries at the
817 * start and at the end of the difference region. Since use of -M and
818 * -C options may lead to overlapping/duplicate source line number
819 * ranges, all we can rely on from sorting/merging is the order of the
820 * first suspect line number.
822 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
823 int tlno
, int offset
, int same
,
824 struct blame_origin
*parent
)
826 struct blame_entry
*e
= **srcq
;
827 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
829 while (e
&& e
->s_lno
< tlno
) {
830 struct blame_entry
*next
= e
->next
;
832 * current record starts before differing portion. If
833 * it reaches into it, we need to split it up and
834 * examine the second part separately.
836 if (e
->s_lno
+ e
->num_lines
> tlno
) {
837 /* Move second half to a new record */
838 int len
= tlno
- e
->s_lno
;
839 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
840 n
->suspect
= e
->suspect
;
841 n
->lno
= e
->lno
+ len
;
842 n
->s_lno
= e
->s_lno
+ len
;
843 n
->num_lines
= e
->num_lines
- len
;
846 /* Push new record to diffp */
850 blame_origin_decref(e
->suspect
);
851 /* Pass blame for everything before the differing
852 * chunk to the parent */
853 e
->suspect
= blame_origin_incref(parent
);
860 * As we don't know how much of a common stretch after this
861 * diff will occur, the currently blamed parts are all that we
862 * can assign to the parent for now.
866 **dstq
= reverse_blame(samep
, **dstq
);
867 *dstq
= &samep
->next
;
870 * Prepend the split off portions: everything after e starts
871 * after the blameable portion.
873 e
= reverse_blame(diffp
, e
);
876 * Now retain records on the target while parts are different
881 while (e
&& e
->s_lno
< same
) {
882 struct blame_entry
*next
= e
->next
;
885 * If current record extends into sameness, need to split.
887 if (e
->s_lno
+ e
->num_lines
> same
) {
889 * Move second half to a new record to be
890 * processed by later chunks
892 int len
= same
- e
->s_lno
;
893 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
894 n
->suspect
= blame_origin_incref(e
->suspect
);
895 n
->lno
= e
->lno
+ len
;
896 n
->s_lno
= e
->s_lno
+ len
;
897 n
->num_lines
= e
->num_lines
- len
;
900 /* Push new record to samep */
908 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
909 /* Move across elements that are in the unblamable portion */
911 *srcq
= &diffp
->next
;
914 struct blame_chunk_cb_data
{
915 struct blame_origin
*parent
;
917 struct blame_entry
**dstq
;
918 struct blame_entry
**srcq
;
921 /* diff chunks are from parent to target */
922 static int blame_chunk_cb(long start_a
, long count_a
,
923 long start_b
, long count_b
, void *data
)
925 struct blame_chunk_cb_data
*d
= data
;
926 if (start_a
- start_b
!= d
->offset
)
927 die("internal error in blame::blame_chunk_cb");
928 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
929 start_b
+ count_b
, d
->parent
);
930 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
935 * We are looking at the origin 'target' and aiming to pass blame
936 * for the lines it is suspected to its parent. Run diff to find
937 * which lines came from parent and pass blame for them.
939 static void pass_blame_to_parent(struct blame_scoreboard
*sb
,
940 struct blame_origin
*target
,
941 struct blame_origin
*parent
)
943 mmfile_t file_p
, file_o
;
944 struct blame_chunk_cb_data d
;
945 struct blame_entry
*newdest
= NULL
;
947 if (!target
->suspects
)
948 return; /* nothing remains for this target */
952 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
954 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
955 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
, &sb
->num_read_blob
);
958 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, sb
->xdl_opts
))
959 die("unable to generate diff (%s -> %s)",
960 oid_to_hex(&parent
->commit
->object
.oid
),
961 oid_to_hex(&target
->commit
->object
.oid
));
962 /* The rest are the same as the parent */
963 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
965 queue_blames(sb
, parent
, newdest
);
971 * The lines in blame_entry after splitting blames many times can become
972 * very small and trivial, and at some point it becomes pointless to
973 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
974 * ordinary C program, and it is not worth to say it was copied from
975 * totally unrelated file in the parent.
977 * Compute how trivial the lines in the blame_entry are.
979 unsigned blame_entry_score(struct blame_scoreboard
*sb
, struct blame_entry
*e
)
988 cp
= blame_nth_line(sb
, e
->lno
);
989 ep
= blame_nth_line(sb
, e
->lno
+ e
->num_lines
);
991 unsigned ch
= *((unsigned char *)cp
);
1001 * best_so_far[] and this[] are both a split of an existing blame_entry
1002 * that passes blame to the parent. Maintain best_so_far the best split
1003 * so far, by comparing this and best_so_far and copying this into
1004 * bst_so_far as needed.
1006 static void copy_split_if_better(struct blame_scoreboard
*sb
,
1007 struct blame_entry
*best_so_far
,
1008 struct blame_entry
*this)
1012 if (!this[1].suspect
)
1014 if (best_so_far
[1].suspect
) {
1015 if (blame_entry_score(sb
, &this[1]) < blame_entry_score(sb
, &best_so_far
[1]))
1019 for (i
= 0; i
< 3; i
++)
1020 blame_origin_incref(this[i
].suspect
);
1021 decref_split(best_so_far
);
1022 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
1026 * We are looking at a part of the final image represented by
1027 * ent (tlno and same are offset by ent->s_lno).
1028 * tlno is where we are looking at in the final image.
1029 * up to (but not including) same match preimage.
1030 * plno is where we are looking at in the preimage.
1032 * <-------------- final image ---------------------->
1035 * <---------preimage----->
1038 * All line numbers are 0-based.
1040 static void handle_split(struct blame_scoreboard
*sb
,
1041 struct blame_entry
*ent
,
1042 int tlno
, int plno
, int same
,
1043 struct blame_origin
*parent
,
1044 struct blame_entry
*split
)
1046 if (ent
->num_lines
<= tlno
)
1049 struct blame_entry
this[3];
1052 split_overlap(this, ent
, tlno
, plno
, same
, parent
);
1053 copy_split_if_better(sb
, split
, this);
1058 struct handle_split_cb_data
{
1059 struct blame_scoreboard
*sb
;
1060 struct blame_entry
*ent
;
1061 struct blame_origin
*parent
;
1062 struct blame_entry
*split
;
1067 static int handle_split_cb(long start_a
, long count_a
,
1068 long start_b
, long count_b
, void *data
)
1070 struct handle_split_cb_data
*d
= data
;
1071 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1073 d
->plno
= start_a
+ count_a
;
1074 d
->tlno
= start_b
+ count_b
;
1079 * Find the lines from parent that are the same as ent so that
1080 * we can pass blames to it. file_p has the blob contents for
1083 static void find_copy_in_blob(struct blame_scoreboard
*sb
,
1084 struct blame_entry
*ent
,
1085 struct blame_origin
*parent
,
1086 struct blame_entry
*split
,
1091 struct handle_split_cb_data d
;
1093 memset(&d
, 0, sizeof(d
));
1094 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1096 * Prepare mmfile that contains only the lines in ent.
1098 cp
= blame_nth_line(sb
, ent
->lno
);
1099 file_o
.ptr
= (char *) cp
;
1100 file_o
.size
= blame_nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1103 * file_o is a part of final image we are annotating.
1104 * file_p partially may match that image.
1106 memset(split
, 0, sizeof(struct blame_entry
[3]));
1107 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, sb
->xdl_opts
))
1108 die("unable to generate diff (%s)",
1109 oid_to_hex(&parent
->commit
->object
.oid
));
1110 /* remainder, if any, all match the preimage */
1111 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1114 /* Move all blame entries from list *source that have a score smaller
1115 * than score_min to the front of list *small.
1116 * Returns a pointer to the link pointing to the old head of the small list.
1119 static struct blame_entry
**filter_small(struct blame_scoreboard
*sb
,
1120 struct blame_entry
**small
,
1121 struct blame_entry
**source
,
1124 struct blame_entry
*p
= *source
;
1125 struct blame_entry
*oldsmall
= *small
;
1127 if (blame_entry_score(sb
, p
) <= score_min
) {
1143 * See if lines currently target is suspected for can be attributed to
1146 static void find_move_in_parent(struct blame_scoreboard
*sb
,
1147 struct blame_entry
***blamed
,
1148 struct blame_entry
**toosmall
,
1149 struct blame_origin
*target
,
1150 struct blame_origin
*parent
)
1152 struct blame_entry
*e
, split
[3];
1153 struct blame_entry
*unblamed
= target
->suspects
;
1154 struct blame_entry
*leftover
= NULL
;
1158 return; /* nothing remains for this target */
1160 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
1164 /* At each iteration, unblamed has a NULL-terminated list of
1165 * entries that have not yet been tested for blame. leftover
1166 * contains the reversed list of entries that have been tested
1167 * without being assignable to the parent.
1170 struct blame_entry
**unblamedtail
= &unblamed
;
1171 struct blame_entry
*next
;
1172 for (e
= unblamed
; e
; e
= next
) {
1174 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1175 if (split
[1].suspect
&&
1176 sb
->move_score
< blame_entry_score(sb
, &split
[1])) {
1177 split_blame(blamed
, &unblamedtail
, split
, e
);
1182 decref_split(split
);
1184 *unblamedtail
= NULL
;
1185 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->move_score
);
1187 target
->suspects
= reverse_blame(leftover
, NULL
);
1191 struct blame_entry
*ent
;
1192 struct blame_entry split
[3];
1196 * Count the number of entries the target is suspected for,
1197 * and prepare a list of entry and the best split.
1199 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1202 struct blame_entry
*e
;
1204 struct blame_list
*blame_list
= NULL
;
1206 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1209 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1210 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1211 blame_list
[i
++].ent
= e
;
1213 *num_ents_p
= num_ents
;
1218 * For lines target is suspected for, see if we can find code movement
1219 * across file boundary from the parent commit. porigin is the path
1220 * in the parent we already tried.
1222 static void find_copy_in_parent(struct blame_scoreboard
*sb
,
1223 struct blame_entry
***blamed
,
1224 struct blame_entry
**toosmall
,
1225 struct blame_origin
*target
,
1226 struct commit
*parent
,
1227 struct blame_origin
*porigin
,
1230 struct diff_options diff_opts
;
1232 struct blame_list
*blame_list
;
1234 struct blame_entry
*unblamed
= target
->suspects
;
1235 struct blame_entry
*leftover
= NULL
;
1238 return; /* nothing remains for this target */
1240 diff_setup(&diff_opts
);
1241 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1242 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1244 diff_setup_done(&diff_opts
);
1246 /* Try "find copies harder" on new path if requested;
1247 * we do not want to use diffcore_rename() actually to
1248 * match things up; find_copies_harder is set only to
1249 * force diff_tree_oid() to feed all filepairs to diff_queue,
1250 * and this code needs to be after diff_setup_done(), which
1251 * usually makes find-copies-harder imply copy detection.
1253 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1254 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1255 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1256 DIFF_OPT_SET(&diff_opts
, FIND_COPIES_HARDER
);
1258 if (is_null_oid(&target
->commit
->object
.oid
))
1259 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
1261 diff_tree_oid(&parent
->tree
->object
.oid
,
1262 &target
->commit
->tree
->object
.oid
,
1265 if (!DIFF_OPT_TST(&diff_opts
, FIND_COPIES_HARDER
))
1266 diffcore_std(&diff_opts
);
1269 struct blame_entry
**unblamedtail
= &unblamed
;
1270 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1272 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1273 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1274 struct blame_origin
*norigin
;
1276 struct blame_entry
this[3];
1278 if (!DIFF_FILE_VALID(p
->one
))
1279 continue; /* does not exist in parent */
1280 if (S_ISGITLINK(p
->one
->mode
))
1281 continue; /* ignore git links */
1282 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1283 /* find_move already dealt with this path */
1286 norigin
= get_origin(parent
, p
->one
->path
);
1287 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
1288 norigin
->mode
= p
->one
->mode
;
1289 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
, &sb
->num_read_blob
);
1293 for (j
= 0; j
< num_ents
; j
++) {
1294 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1295 norigin
, this, &file_p
);
1296 copy_split_if_better(sb
, blame_list
[j
].split
,
1300 blame_origin_decref(norigin
);
1303 for (j
= 0; j
< num_ents
; j
++) {
1304 struct blame_entry
*split
= blame_list
[j
].split
;
1305 if (split
[1].suspect
&&
1306 sb
->copy_score
< blame_entry_score(sb
, &split
[1])) {
1307 split_blame(blamed
, &unblamedtail
, split
,
1310 blame_list
[j
].ent
->next
= leftover
;
1311 leftover
= blame_list
[j
].ent
;
1313 decref_split(split
);
1316 *unblamedtail
= NULL
;
1317 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->copy_score
);
1319 target
->suspects
= reverse_blame(leftover
, NULL
);
1320 diff_flush(&diff_opts
);
1321 clear_pathspec(&diff_opts
.pathspec
);
1325 * The blobs of origin and porigin exactly match, so everything
1326 * origin is suspected for can be blamed on the parent.
1328 static void pass_whole_blame(struct blame_scoreboard
*sb
,
1329 struct blame_origin
*origin
, struct blame_origin
*porigin
)
1331 struct blame_entry
*e
, *suspects
;
1333 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1334 /* Steal its file */
1335 porigin
->file
= origin
->file
;
1336 origin
->file
.ptr
= NULL
;
1338 suspects
= origin
->suspects
;
1339 origin
->suspects
= NULL
;
1340 for (e
= suspects
; e
; e
= e
->next
) {
1341 blame_origin_incref(porigin
);
1342 blame_origin_decref(e
->suspect
);
1343 e
->suspect
= porigin
;
1345 queue_blames(sb
, porigin
, suspects
);
1349 * We pass blame from the current commit to its parents. We keep saying
1350 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1351 * exonerate ourselves.
1353 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
,
1357 if (revs
->first_parent_only
&&
1359 commit
->parents
->next
) {
1360 free_commit_list(commit
->parents
->next
);
1361 commit
->parents
->next
= NULL
;
1363 return commit
->parents
;
1365 return lookup_decoration(&revs
->children
, &commit
->object
);
1368 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
, int reverse
)
1370 struct commit_list
*l
= first_scapegoat(revs
, commit
, reverse
);
1371 return commit_list_count(l
);
1374 /* Distribute collected unsorted blames to the respected sorted lists
1375 * in the various origins.
1377 static void distribute_blame(struct blame_scoreboard
*sb
, struct blame_entry
*blamed
)
1379 blamed
= llist_mergesort(blamed
, get_next_blame
, set_next_blame
,
1380 compare_blame_suspect
);
1383 struct blame_origin
*porigin
= blamed
->suspect
;
1384 struct blame_entry
*suspects
= NULL
;
1386 struct blame_entry
*next
= blamed
->next
;
1387 blamed
->next
= suspects
;
1390 } while (blamed
&& blamed
->suspect
== porigin
);
1391 suspects
= reverse_blame(suspects
, NULL
);
1392 queue_blames(sb
, porigin
, suspects
);
1398 static void pass_blame(struct blame_scoreboard
*sb
, struct blame_origin
*origin
, int opt
)
1400 struct rev_info
*revs
= sb
->revs
;
1401 int i
, pass
, num_sg
;
1402 struct commit
*commit
= origin
->commit
;
1403 struct commit_list
*sg
;
1404 struct blame_origin
*sg_buf
[MAXSG
];
1405 struct blame_origin
*porigin
, **sg_origin
= sg_buf
;
1406 struct blame_entry
*toosmall
= NULL
;
1407 struct blame_entry
*blames
, **blametail
= &blames
;
1409 num_sg
= num_scapegoats(revs
, commit
, sb
->reverse
);
1412 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1413 memset(sg_buf
, 0, sizeof(sg_buf
));
1415 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1418 * The first pass looks for unrenamed path to optimize for
1419 * common cases, then we look for renames in the second pass.
1421 for (pass
= 0; pass
< 2 - sb
->no_whole_file_rename
; pass
++) {
1422 struct blame_origin
*(*find
)(struct commit
*, struct blame_origin
*);
1423 find
= pass
? find_rename
: find_origin
;
1425 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1427 sg
= sg
->next
, i
++) {
1428 struct commit
*p
= sg
->item
;
1433 if (parse_commit(p
))
1435 porigin
= find(p
, origin
);
1438 if (!oidcmp(&porigin
->blob_oid
, &origin
->blob_oid
)) {
1439 pass_whole_blame(sb
, origin
, porigin
);
1440 blame_origin_decref(porigin
);
1443 for (j
= same
= 0; j
< i
; j
++)
1445 !oidcmp(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
1450 sg_origin
[i
] = porigin
;
1452 blame_origin_decref(porigin
);
1457 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1459 sg
= sg
->next
, i
++) {
1460 struct blame_origin
*porigin
= sg_origin
[i
];
1463 if (!origin
->previous
) {
1464 blame_origin_incref(porigin
);
1465 origin
->previous
= porigin
;
1467 pass_blame_to_parent(sb
, origin
, porigin
);
1468 if (!origin
->suspects
)
1473 * Optionally find moves in parents' files.
1475 if (opt
& PICKAXE_BLAME_MOVE
) {
1476 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->move_score
);
1477 if (origin
->suspects
) {
1478 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1480 sg
= sg
->next
, i
++) {
1481 struct blame_origin
*porigin
= sg_origin
[i
];
1484 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1485 if (!origin
->suspects
)
1492 * Optionally find copies from parents' files.
1494 if (opt
& PICKAXE_BLAME_COPY
) {
1495 if (sb
->copy_score
> sb
->move_score
)
1496 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1497 else if (sb
->copy_score
< sb
->move_score
) {
1498 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1500 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1502 if (!origin
->suspects
)
1505 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1507 sg
= sg
->next
, i
++) {
1508 struct blame_origin
*porigin
= sg_origin
[i
];
1509 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1510 origin
, sg
->item
, porigin
, opt
);
1511 if (!origin
->suspects
)
1518 distribute_blame(sb
, blames
);
1520 * prepend toosmall to origin->suspects
1522 * There is no point in sorting: this ends up on a big
1523 * unsorted list in the caller anyway.
1526 struct blame_entry
**tail
= &toosmall
;
1528 tail
= &(*tail
)->next
;
1529 *tail
= origin
->suspects
;
1530 origin
->suspects
= toosmall
;
1532 for (i
= 0; i
< num_sg
; i
++) {
1534 drop_origin_blob(sg_origin
[i
]);
1535 blame_origin_decref(sg_origin
[i
]);
1538 drop_origin_blob(origin
);
1539 if (sg_buf
!= sg_origin
)
1544 * The main loop -- while we have blobs with lines whose true origin
1545 * is still unknown, pick one blob, and allow its lines to pass blames
1546 * to its parents. */
1547 void assign_blame(struct blame_scoreboard
*sb
, int opt
)
1549 struct rev_info
*revs
= sb
->revs
;
1550 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1553 struct blame_entry
*ent
;
1554 struct blame_origin
*suspect
= commit
->util
;
1556 /* find one suspect to break down */
1557 while (suspect
&& !suspect
->suspects
)
1558 suspect
= suspect
->next
;
1561 commit
= prio_queue_get(&sb
->commits
);
1565 assert(commit
== suspect
->commit
);
1568 * We will use this suspect later in the loop,
1569 * so hold onto it in the meantime.
1571 blame_origin_incref(suspect
);
1572 parse_commit(commit
);
1574 (!(commit
->object
.flags
& UNINTERESTING
) &&
1575 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1576 pass_blame(sb
, suspect
, opt
);
1578 commit
->object
.flags
|= UNINTERESTING
;
1579 if (commit
->object
.parsed
)
1580 mark_parents_uninteresting(commit
);
1582 /* treat root commit as boundary */
1583 if (!commit
->parents
&& !sb
->show_root
)
1584 commit
->object
.flags
|= UNINTERESTING
;
1586 /* Take responsibility for the remaining entries */
1587 ent
= suspect
->suspects
;
1589 suspect
->guilty
= 1;
1591 struct blame_entry
*next
= ent
->next
;
1592 if (sb
->found_guilty_entry
)
1593 sb
->found_guilty_entry(ent
, sb
->found_guilty_entry_data
);
1598 ent
->next
= sb
->ent
;
1599 sb
->ent
= suspect
->suspects
;
1600 suspect
->suspects
= NULL
;
1604 blame_origin_decref(suspect
);
1606 if (sb
->debug
) /* sanity */
1607 sanity_check_refcnt(sb
);
1611 static const char *get_next_line(const char *start
, const char *end
)
1613 const char *nl
= memchr(start
, '\n', end
- start
);
1614 return nl
? nl
+ 1 : end
;
1618 * To allow quick access to the contents of nth line in the
1619 * final image, prepare an index in the scoreboard.
1621 static int prepare_lines(struct blame_scoreboard
*sb
)
1623 const char *buf
= sb
->final_buf
;
1624 unsigned long len
= sb
->final_buf_size
;
1625 const char *end
= buf
+ len
;
1630 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1633 ALLOC_ARRAY(sb
->lineno
, num
+ 1);
1634 lineno
= sb
->lineno
;
1636 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1637 *lineno
++ = p
- buf
;
1641 sb
->num_lines
= num
;
1642 return sb
->num_lines
;
1645 static struct commit
*find_single_final(struct rev_info
*revs
,
1646 const char **name_p
)
1649 struct commit
*found
= NULL
;
1650 const char *name
= NULL
;
1652 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1653 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1654 if (obj
->flags
& UNINTERESTING
)
1656 obj
= deref_tag(obj
, NULL
, 0);
1657 if (obj
->type
!= OBJ_COMMIT
)
1658 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1660 die("More than one commit to dig from %s and %s?",
1661 revs
->pending
.objects
[i
].name
, name
);
1662 found
= (struct commit
*)obj
;
1663 name
= revs
->pending
.objects
[i
].name
;
1666 *name_p
= xstrdup_or_null(name
);
1670 static struct commit
*dwim_reverse_initial(struct rev_info
*revs
,
1671 const char **name_p
)
1674 * DWIM "git blame --reverse ONE -- PATH" as
1675 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1676 * when it makes sense.
1679 struct commit
*head_commit
;
1680 struct object_id head_oid
;
1682 if (revs
->pending
.nr
!= 1)
1685 /* Is that sole rev a committish? */
1686 obj
= revs
->pending
.objects
[0].item
;
1687 obj
= deref_tag(obj
, NULL
, 0);
1688 if (obj
->type
!= OBJ_COMMIT
)
1691 /* Do we have HEAD? */
1692 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
1694 head_commit
= lookup_commit_reference_gently(&head_oid
, 1);
1698 /* Turn "ONE" into "ONE..HEAD" then */
1699 obj
->flags
|= UNINTERESTING
;
1700 add_pending_object(revs
, &head_commit
->object
, "HEAD");
1703 *name_p
= revs
->pending
.objects
[0].name
;
1704 return (struct commit
*)obj
;
1707 static struct commit
*find_single_initial(struct rev_info
*revs
,
1708 const char **name_p
)
1711 struct commit
*found
= NULL
;
1712 const char *name
= NULL
;
1715 * There must be one and only one negative commit, and it must be
1718 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1719 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1720 if (!(obj
->flags
& UNINTERESTING
))
1722 obj
= deref_tag(obj
, NULL
, 0);
1723 if (obj
->type
!= OBJ_COMMIT
)
1724 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1726 die("More than one commit to dig up from, %s and %s?",
1727 revs
->pending
.objects
[i
].name
, name
);
1728 found
= (struct commit
*) obj
;
1729 name
= revs
->pending
.objects
[i
].name
;
1733 found
= dwim_reverse_initial(revs
, &name
);
1735 die("No commit to dig up from?");
1738 *name_p
= xstrdup(name
);
1742 void init_scoreboard(struct blame_scoreboard
*sb
)
1744 memset(sb
, 0, sizeof(struct blame_scoreboard
));
1745 sb
->move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1746 sb
->copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1749 void setup_scoreboard(struct blame_scoreboard
*sb
, const char *path
, struct blame_origin
**orig
)
1751 const char *final_commit_name
= NULL
;
1752 struct blame_origin
*o
;
1753 struct commit
*final_commit
= NULL
;
1754 enum object_type type
;
1756 if (sb
->reverse
&& sb
->contents_from
)
1757 die(_("--contents and --reverse do not blend well."));
1760 sb
->final
= find_single_final(sb
->revs
, &final_commit_name
);
1761 sb
->commits
.compare
= compare_commits_by_commit_date
;
1763 sb
->final
= find_single_initial(sb
->revs
, &final_commit_name
);
1764 sb
->commits
.compare
= compare_commits_by_reverse_commit_date
;
1767 if (sb
->final
&& sb
->contents_from
)
1768 die(_("cannot use --contents with final commit object name"));
1770 if (sb
->reverse
&& sb
->revs
->first_parent_only
)
1771 sb
->revs
->children
.name
= NULL
;
1775 * "--not A B -- path" without anything positive;
1776 * do not default to HEAD, but use the working tree
1780 sb
->final
= fake_working_tree_commit(&sb
->revs
->diffopt
,
1781 path
, sb
->contents_from
);
1782 add_pending_object(sb
->revs
, &(sb
->final
->object
), ":");
1785 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1786 final_commit
= find_single_final(sb
->revs
, NULL
);
1788 die(_("--reverse and --first-parent together require specified latest commit"));
1792 * If we have bottom, this will mark the ancestors of the
1793 * bottom commits we would reach while traversing as
1796 if (prepare_revision_walk(sb
->revs
))
1797 die(_("revision walk setup failed"));
1799 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1800 struct commit
*c
= final_commit
;
1802 sb
->revs
->children
.name
= "children";
1803 while (c
->parents
&&
1804 oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
)) {
1805 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1808 if (add_decoration(&sb
->revs
->children
,
1809 &c
->parents
->item
->object
, l
))
1810 die("BUG: not unique item in first-parent chain");
1811 c
= c
->parents
->item
;
1814 if (oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
))
1815 die(_("--reverse --first-parent together require range along first-parent chain"));
1818 if (is_null_oid(&sb
->final
->object
.oid
)) {
1819 o
= sb
->final
->util
;
1820 sb
->final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
1821 sb
->final_buf_size
= o
->file
.size
;
1824 o
= get_origin(sb
->final
, path
);
1825 if (fill_blob_sha1_and_mode(o
))
1826 die(_("no such path %s in %s"), path
, final_commit_name
);
1828 if (DIFF_OPT_TST(&sb
->revs
->diffopt
, ALLOW_TEXTCONV
) &&
1829 textconv_object(path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
->final_buf
,
1830 &sb
->final_buf_size
))
1833 sb
->final_buf
= read_sha1_file(o
->blob_oid
.hash
, &type
,
1834 &sb
->final_buf_size
);
1837 die(_("cannot read blob %s for path %s"),
1838 oid_to_hex(&o
->blob_oid
),
1841 sb
->num_read_blob
++;
1847 free((char *)final_commit_name
);
1852 struct blame_entry
*blame_entry_prepend(struct blame_entry
*head
,
1853 long start
, long end
,
1854 struct blame_origin
*o
)
1856 struct blame_entry
*new_head
= xcalloc(1, sizeof(struct blame_entry
));
1857 new_head
->lno
= start
;
1858 new_head
->num_lines
= end
- start
;
1859 new_head
->suspect
= o
;
1860 new_head
->s_lno
= start
;
1861 new_head
->next
= head
;
1862 blame_origin_incref(o
);