3 #include "object-store.h"
4 #include "cache-tree.h"
12 void blame_origin_decref(struct blame_origin
*o
)
14 if (o
&& --o
->refcnt
<= 0) {
15 struct blame_origin
*p
, *l
= NULL
;
17 blame_origin_decref(o
->previous
);
19 /* Should be present exactly once in commit chain */
20 for (p
= o
->commit
->util
; p
; l
= p
, p
= p
->next
) {
25 o
->commit
->util
= p
->next
;
30 die("internal error in blame_origin_decref");
35 * Given a commit and a path in it, create a new origin structure.
36 * The callers that add blame to the scoreboard should use
37 * get_origin() to obtain shared, refcounted copy instead of calling
38 * this function directly.
40 static struct blame_origin
*make_origin(struct commit
*commit
, const char *path
)
42 struct blame_origin
*o
;
43 FLEX_ALLOC_STR(o
, path
, path
);
46 o
->next
= commit
->util
;
52 * Locate an existing origin or create a new one.
53 * This moves the origin to front position in the commit util list.
55 static struct blame_origin
*get_origin(struct commit
*commit
, const char *path
)
57 struct blame_origin
*o
, *l
;
59 for (o
= commit
->util
, l
= NULL
; o
; l
= o
, o
= o
->next
) {
60 if (!strcmp(o
->path
, path
)) {
64 o
->next
= commit
->util
;
67 return blame_origin_incref(o
);
70 return make_origin(commit
, path
);
75 static void verify_working_tree_path(struct commit
*work_tree
, const char *path
)
77 struct commit_list
*parents
;
80 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
81 const struct object_id
*commit_oid
= &parents
->item
->object
.oid
;
82 struct object_id blob_oid
;
85 if (!get_tree_entry(commit_oid
, path
, &blob_oid
, &mode
) &&
86 oid_object_info(the_repository
, &blob_oid
, NULL
) == OBJ_BLOB
)
90 pos
= cache_name_pos(path
, strlen(path
));
92 ; /* path is in the index */
93 else if (-1 - pos
< active_nr
&&
94 !strcmp(active_cache
[-1 - pos
]->name
, path
))
95 ; /* path is in the index, unmerged */
97 die("no such path '%s' in HEAD", path
);
100 static struct commit_list
**append_parent(struct commit_list
**tail
, const struct object_id
*oid
)
102 struct commit
*parent
;
104 parent
= lookup_commit_reference(oid
);
106 die("no such commit %s", oid_to_hex(oid
));
107 return &commit_list_insert(parent
, tail
)->next
;
110 static void append_merge_parents(struct commit_list
**tail
)
113 struct strbuf line
= STRBUF_INIT
;
115 merge_head
= open(git_path_merge_head(the_repository
), O_RDONLY
);
116 if (merge_head
< 0) {
119 die("cannot open '%s' for reading",
120 git_path_merge_head(the_repository
));
123 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
124 struct object_id oid
;
125 if (line
.len
< GIT_SHA1_HEXSZ
|| get_oid_hex(line
.buf
, &oid
))
126 die("unknown line in '%s': %s",
127 git_path_merge_head(the_repository
), line
.buf
);
128 tail
= append_parent(tail
, &oid
);
131 strbuf_release(&line
);
135 * This isn't as simple as passing sb->buf and sb->len, because we
136 * want to transfer ownership of the buffer to the commit (so we
139 static void set_commit_buffer_from_strbuf(struct commit
*c
, struct strbuf
*sb
)
142 void *buf
= strbuf_detach(sb
, &len
);
143 set_commit_buffer(c
, buf
, len
);
147 * Prepare a dummy commit that represents the work tree (or staged) item.
148 * Note that annotating work tree item never works in the reverse.
150 static struct commit
*fake_working_tree_commit(struct diff_options
*opt
,
152 const char *contents_from
)
154 struct commit
*commit
;
155 struct blame_origin
*origin
;
156 struct commit_list
**parent_tail
, *parent
;
157 struct object_id head_oid
;
158 struct strbuf buf
= STRBUF_INIT
;
162 struct cache_entry
*ce
;
164 struct strbuf msg
= STRBUF_INIT
;
168 commit
= alloc_commit_node(the_repository
);
169 commit
->object
.parsed
= 1;
171 parent_tail
= &commit
->parents
;
173 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
174 die("no such ref: HEAD");
176 parent_tail
= append_parent(parent_tail
, &head_oid
);
177 append_merge_parents(parent_tail
);
178 verify_working_tree_path(commit
, path
);
180 origin
= make_origin(commit
, path
);
182 ident
= fmt_ident("Not Committed Yet", "not.committed.yet", NULL
, 0);
183 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
184 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
185 strbuf_addf(&msg
, "parent %s\n",
186 oid_to_hex(&parent
->item
->object
.oid
));
190 "Version of %s from %s\n",
192 (!contents_from
? path
:
193 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
194 set_commit_buffer_from_strbuf(commit
, &msg
);
196 if (!contents_from
|| strcmp("-", contents_from
)) {
198 const char *read_from
;
200 unsigned long buf_len
;
203 if (stat(contents_from
, &st
) < 0)
204 die_errno("Cannot stat '%s'", contents_from
);
205 read_from
= contents_from
;
208 if (lstat(path
, &st
) < 0)
209 die_errno("Cannot lstat '%s'", path
);
212 mode
= canon_mode(st
.st_mode
);
214 switch (st
.st_mode
& S_IFMT
) {
216 if (opt
->flags
.allow_textconv
&&
217 textconv_object(read_from
, mode
, &null_oid
, 0, &buf_ptr
, &buf_len
))
218 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
219 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
220 die_errno("cannot open or read '%s'", read_from
);
223 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
224 die_errno("cannot readlink '%s'", read_from
);
227 die("unsupported file type %s", read_from
);
231 /* Reading from stdin */
233 if (strbuf_read(&buf
, 0, 0) < 0)
234 die_errno("failed to read from stdin");
236 convert_to_git(&the_index
, path
, buf
.buf
, buf
.len
, &buf
, 0);
237 origin
->file
.ptr
= buf
.buf
;
238 origin
->file
.size
= buf
.len
;
239 pretend_object_file(buf
.buf
, buf
.len
, OBJ_BLOB
, &origin
->blob_oid
);
242 * Read the current index, replace the path entry with
243 * origin->blob_sha1 without mucking with its mode or type
244 * bits; we are not going to write this index out -- we just
245 * want to run "diff-index --cached".
252 int pos
= cache_name_pos(path
, len
);
254 mode
= active_cache
[pos
]->ce_mode
;
256 /* Let's not bother reading from HEAD tree */
257 mode
= S_IFREG
| 0644;
259 size
= cache_entry_size(len
);
260 ce
= xcalloc(1, size
);
261 oidcpy(&ce
->oid
, &origin
->blob_oid
);
262 memcpy(ce
->name
, path
, len
);
263 ce
->ce_flags
= create_ce_flags(0);
264 ce
->ce_namelen
= len
;
265 ce
->ce_mode
= create_ce_mode(mode
);
266 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
268 cache_tree_invalidate_path(&the_index
, path
);
275 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
276 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
, int xdl_opts
)
279 xdemitconf_t xecfg
= {0};
280 xdemitcb_t ecb
= {NULL
};
282 xpp
.flags
= xdl_opts
;
283 xecfg
.hunk_func
= hunk_func
;
285 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
289 * Given an origin, prepare mmfile_t structure to be used by the
292 static void fill_origin_blob(struct diff_options
*opt
,
293 struct blame_origin
*o
, mmfile_t
*file
, int *num_read_blob
)
296 enum object_type type
;
297 unsigned long file_size
;
300 if (opt
->flags
.allow_textconv
&&
301 textconv_object(o
->path
, o
->mode
, &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
304 file
->ptr
= read_object_file(&o
->blob_oid
, &type
,
306 file
->size
= file_size
;
309 die("Cannot read blob %s for path %s",
310 oid_to_hex(&o
->blob_oid
),
318 static void drop_origin_blob(struct blame_origin
*o
)
321 FREE_AND_NULL(o
->file
.ptr
);
326 * Any merge of blames happens on lists of blames that arrived via
327 * different parents in a single suspect. In this case, we want to
328 * sort according to the suspect line numbers as opposed to the final
329 * image line numbers. The function body is somewhat longish because
330 * it avoids unnecessary writes.
333 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
334 struct blame_entry
*list2
)
336 struct blame_entry
*p1
= list1
, *p2
= list2
,
344 if (p1
->s_lno
<= p2
->s_lno
) {
347 if ((p1
= *tail
) == NULL
) {
351 } while (p1
->s_lno
<= p2
->s_lno
);
357 if ((p2
= *tail
) == NULL
) {
361 } while (p1
->s_lno
> p2
->s_lno
);
365 if ((p1
= *tail
) == NULL
) {
369 } while (p1
->s_lno
<= p2
->s_lno
);
373 static void *get_next_blame(const void *p
)
375 return ((struct blame_entry
*)p
)->next
;
378 static void set_next_blame(void *p1
, void *p2
)
380 ((struct blame_entry
*)p1
)->next
= p2
;
384 * Final image line numbers are all different, so we don't need a
385 * three-way comparison here.
388 static int compare_blame_final(const void *p1
, const void *p2
)
390 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
394 static int compare_blame_suspect(const void *p1
, const void *p2
)
396 const struct blame_entry
*s1
= p1
, *s2
= p2
;
398 * to allow for collating suspects, we sort according to the
399 * respective pointer value as the primary sorting criterion.
400 * The actual relation is pretty unimportant as long as it
401 * establishes a total order. Comparing as integers gives us
404 if (s1
->suspect
!= s2
->suspect
)
405 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
406 if (s1
->s_lno
== s2
->s_lno
)
408 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
411 void blame_sort_final(struct blame_scoreboard
*sb
)
413 sb
->ent
= llist_mergesort(sb
->ent
, get_next_blame
, set_next_blame
,
414 compare_blame_final
);
417 static int compare_commits_by_reverse_commit_date(const void *a
,
421 return -compare_commits_by_commit_date(a
, b
, c
);
425 * For debugging -- origin is refcounted, and this asserts that
426 * we do not underflow.
428 static void sanity_check_refcnt(struct blame_scoreboard
*sb
)
431 struct blame_entry
*ent
;
433 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
434 /* Nobody should have zero or negative refcnt */
435 if (ent
->suspect
->refcnt
<= 0) {
436 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
438 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
439 ent
->suspect
->refcnt
);
444 sb
->on_sanity_fail(sb
, baa
);
448 * If two blame entries that are next to each other came from
449 * contiguous lines in the same origin (i.e. <commit, path> pair),
450 * merge them together.
452 void blame_coalesce(struct blame_scoreboard
*sb
)
454 struct blame_entry
*ent
, *next
;
456 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
457 if (ent
->suspect
== next
->suspect
&&
458 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
459 ent
->num_lines
+= next
->num_lines
;
460 ent
->next
= next
->next
;
461 blame_origin_decref(next
->suspect
);
464 next
= ent
; /* again */
468 if (sb
->debug
) /* sanity */
469 sanity_check_refcnt(sb
);
473 * Merge the given sorted list of blames into a preexisting origin.
474 * If there were no previous blames to that commit, it is entered into
475 * the commit priority queue of the score board.
478 static void queue_blames(struct blame_scoreboard
*sb
, struct blame_origin
*porigin
,
479 struct blame_entry
*sorted
)
481 if (porigin
->suspects
)
482 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
484 struct blame_origin
*o
;
485 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
487 porigin
->suspects
= sorted
;
491 porigin
->suspects
= sorted
;
492 prio_queue_put(&sb
->commits
, porigin
->commit
);
497 * Fill the blob_sha1 field of an origin if it hasn't, so that later
498 * call to fill_origin_blob() can use it to locate the data. blob_sha1
499 * for an origin is also used to pass the blame for the entire file to
500 * the parent to detect the case where a child's blob is identical to
501 * that of its parent's.
503 * This also fills origin->mode for corresponding tree path.
505 static int fill_blob_sha1_and_mode(struct blame_origin
*origin
)
507 if (!is_null_oid(&origin
->blob_oid
))
509 if (get_tree_entry(&origin
->commit
->object
.oid
, origin
->path
, &origin
->blob_oid
, &origin
->mode
))
511 if (oid_object_info(the_repository
, &origin
->blob_oid
, NULL
) != OBJ_BLOB
)
515 oidclr(&origin
->blob_oid
);
516 origin
->mode
= S_IFINVALID
;
521 * We have an origin -- check if the same path exists in the
522 * parent and return an origin structure to represent it.
524 static struct blame_origin
*find_origin(struct commit
*parent
,
525 struct blame_origin
*origin
)
527 struct blame_origin
*porigin
;
528 struct diff_options diff_opts
;
529 const char *paths
[2];
531 /* First check any existing origins */
532 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
533 if (!strcmp(porigin
->path
, origin
->path
)) {
535 * The same path between origin and its parent
536 * without renaming -- the most common case.
538 return blame_origin_incref (porigin
);
541 /* See if the origin->path is different between parent
542 * and origin first. Most of the time they are the
543 * same and diff-tree is fairly efficient about this.
545 diff_setup(&diff_opts
);
546 diff_opts
.flags
.recursive
= 1;
547 diff_opts
.detect_rename
= 0;
548 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
549 paths
[0] = origin
->path
;
552 parse_pathspec(&diff_opts
.pathspec
,
553 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
554 PATHSPEC_LITERAL_PATH
, "", paths
);
555 diff_setup_done(&diff_opts
);
557 if (is_null_oid(&origin
->commit
->object
.oid
))
558 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
560 diff_tree_oid(&parent
->tree
->object
.oid
,
561 &origin
->commit
->tree
->object
.oid
,
563 diffcore_std(&diff_opts
);
565 if (!diff_queued_diff
.nr
) {
566 /* The path is the same as parent */
567 porigin
= get_origin(parent
, origin
->path
);
568 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
569 porigin
->mode
= origin
->mode
;
572 * Since origin->path is a pathspec, if the parent
573 * commit had it as a directory, we will see a whole
574 * bunch of deletion of files in the directory that we
578 struct diff_filepair
*p
= NULL
;
579 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
581 p
= diff_queued_diff
.queue
[i
];
582 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
583 if (!strcmp(name
, origin
->path
))
587 die("internal error in blame::find_origin");
590 die("internal error in blame::find_origin (%c)",
593 porigin
= get_origin(parent
, origin
->path
);
594 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
595 porigin
->mode
= p
->one
->mode
;
599 /* Did not exist in parent, or type changed */
603 diff_flush(&diff_opts
);
604 clear_pathspec(&diff_opts
.pathspec
);
609 * We have an origin -- find the path that corresponds to it in its
610 * parent and return an origin structure to represent it.
612 static struct blame_origin
*find_rename(struct commit
*parent
,
613 struct blame_origin
*origin
)
615 struct blame_origin
*porigin
= NULL
;
616 struct diff_options diff_opts
;
619 diff_setup(&diff_opts
);
620 diff_opts
.flags
.recursive
= 1;
621 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
622 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
623 diff_opts
.single_follow
= origin
->path
;
624 diff_setup_done(&diff_opts
);
626 if (is_null_oid(&origin
->commit
->object
.oid
))
627 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
629 diff_tree_oid(&parent
->tree
->object
.oid
,
630 &origin
->commit
->tree
->object
.oid
,
632 diffcore_std(&diff_opts
);
634 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
635 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
636 if ((p
->status
== 'R' || p
->status
== 'C') &&
637 !strcmp(p
->two
->path
, origin
->path
)) {
638 porigin
= get_origin(parent
, p
->one
->path
);
639 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
640 porigin
->mode
= p
->one
->mode
;
644 diff_flush(&diff_opts
);
645 clear_pathspec(&diff_opts
.pathspec
);
650 * Append a new blame entry to a given output queue.
652 static void add_blame_entry(struct blame_entry
***queue
,
653 const struct blame_entry
*src
)
655 struct blame_entry
*e
= xmalloc(sizeof(*e
));
656 memcpy(e
, src
, sizeof(*e
));
657 blame_origin_incref(e
->suspect
);
665 * src typically is on-stack; we want to copy the information in it to
666 * a malloced blame_entry that gets added to the given queue. The
667 * origin of dst loses a refcnt.
669 static void dup_entry(struct blame_entry
***queue
,
670 struct blame_entry
*dst
, struct blame_entry
*src
)
672 blame_origin_incref(src
->suspect
);
673 blame_origin_decref(dst
->suspect
);
674 memcpy(dst
, src
, sizeof(*src
));
680 const char *blame_nth_line(struct blame_scoreboard
*sb
, long lno
)
682 return sb
->final_buf
+ sb
->lineno
[lno
];
686 * It is known that lines between tlno to same came from parent, and e
687 * has an overlap with that range. it also is known that parent's
688 * line plno corresponds to e's line tlno.
694 * <------------------>
696 * Split e into potentially three parts; before this chunk, the chunk
697 * to be blamed for the parent, and after that portion.
699 static void split_overlap(struct blame_entry
*split
,
700 struct blame_entry
*e
,
701 int tlno
, int plno
, int same
,
702 struct blame_origin
*parent
)
705 memset(split
, 0, sizeof(struct blame_entry
[3]));
707 if (e
->s_lno
< tlno
) {
708 /* there is a pre-chunk part not blamed on parent */
709 split
[0].suspect
= blame_origin_incref(e
->suspect
);
710 split
[0].lno
= e
->lno
;
711 split
[0].s_lno
= e
->s_lno
;
712 split
[0].num_lines
= tlno
- e
->s_lno
;
713 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
714 split
[1].s_lno
= plno
;
717 split
[1].lno
= e
->lno
;
718 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
721 if (same
< e
->s_lno
+ e
->num_lines
) {
722 /* there is a post-chunk part not blamed on parent */
723 split
[2].suspect
= blame_origin_incref(e
->suspect
);
724 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
725 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
726 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
727 chunk_end_lno
= split
[2].lno
;
730 chunk_end_lno
= e
->lno
+ e
->num_lines
;
731 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
734 * if it turns out there is nothing to blame the parent for,
735 * forget about the splitting. !split[1].suspect signals this.
737 if (split
[1].num_lines
< 1)
739 split
[1].suspect
= blame_origin_incref(parent
);
743 * split_overlap() divided an existing blame e into up to three parts
744 * in split. Any assigned blame is moved to queue to
747 static void split_blame(struct blame_entry
***blamed
,
748 struct blame_entry
***unblamed
,
749 struct blame_entry
*split
,
750 struct blame_entry
*e
)
752 if (split
[0].suspect
&& split
[2].suspect
) {
753 /* The first part (reuse storage for the existing entry e) */
754 dup_entry(unblamed
, e
, &split
[0]);
756 /* The last part -- me */
757 add_blame_entry(unblamed
, &split
[2]);
759 /* ... and the middle part -- parent */
760 add_blame_entry(blamed
, &split
[1]);
762 else if (!split
[0].suspect
&& !split
[2].suspect
)
764 * The parent covers the entire area; reuse storage for
765 * e and replace it with the parent.
767 dup_entry(blamed
, e
, &split
[1]);
768 else if (split
[0].suspect
) {
769 /* me and then parent */
770 dup_entry(unblamed
, e
, &split
[0]);
771 add_blame_entry(blamed
, &split
[1]);
774 /* parent and then me */
775 dup_entry(blamed
, e
, &split
[1]);
776 add_blame_entry(unblamed
, &split
[2]);
781 * After splitting the blame, the origins used by the
782 * on-stack blame_entry should lose one refcnt each.
784 static void decref_split(struct blame_entry
*split
)
788 for (i
= 0; i
< 3; i
++)
789 blame_origin_decref(split
[i
].suspect
);
793 * reverse_blame reverses the list given in head, appending tail.
794 * That allows us to build lists in reverse order, then reverse them
795 * afterwards. This can be faster than building the list in proper
796 * order right away. The reason is that building in proper order
797 * requires writing a link in the _previous_ element, while building
798 * in reverse order just requires placing the list head into the
802 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
803 struct blame_entry
*tail
)
806 struct blame_entry
*next
= head
->next
;
815 * Process one hunk from the patch between the current suspect for
816 * blame_entry e and its parent. This first blames any unfinished
817 * entries before the chunk (which is where target and parent start
818 * differing) on the parent, and then splits blame entries at the
819 * start and at the end of the difference region. Since use of -M and
820 * -C options may lead to overlapping/duplicate source line number
821 * ranges, all we can rely on from sorting/merging is the order of the
822 * first suspect line number.
824 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
825 int tlno
, int offset
, int same
,
826 struct blame_origin
*parent
)
828 struct blame_entry
*e
= **srcq
;
829 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
831 while (e
&& e
->s_lno
< tlno
) {
832 struct blame_entry
*next
= e
->next
;
834 * current record starts before differing portion. If
835 * it reaches into it, we need to split it up and
836 * examine the second part separately.
838 if (e
->s_lno
+ e
->num_lines
> tlno
) {
839 /* Move second half to a new record */
840 int len
= tlno
- e
->s_lno
;
841 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
842 n
->suspect
= e
->suspect
;
843 n
->lno
= e
->lno
+ len
;
844 n
->s_lno
= e
->s_lno
+ len
;
845 n
->num_lines
= e
->num_lines
- len
;
848 /* Push new record to diffp */
852 blame_origin_decref(e
->suspect
);
853 /* Pass blame for everything before the differing
854 * chunk to the parent */
855 e
->suspect
= blame_origin_incref(parent
);
862 * As we don't know how much of a common stretch after this
863 * diff will occur, the currently blamed parts are all that we
864 * can assign to the parent for now.
868 **dstq
= reverse_blame(samep
, **dstq
);
869 *dstq
= &samep
->next
;
872 * Prepend the split off portions: everything after e starts
873 * after the blameable portion.
875 e
= reverse_blame(diffp
, e
);
878 * Now retain records on the target while parts are different
883 while (e
&& e
->s_lno
< same
) {
884 struct blame_entry
*next
= e
->next
;
887 * If current record extends into sameness, need to split.
889 if (e
->s_lno
+ e
->num_lines
> same
) {
891 * Move second half to a new record to be
892 * processed by later chunks
894 int len
= same
- e
->s_lno
;
895 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
896 n
->suspect
= blame_origin_incref(e
->suspect
);
897 n
->lno
= e
->lno
+ len
;
898 n
->s_lno
= e
->s_lno
+ len
;
899 n
->num_lines
= e
->num_lines
- len
;
902 /* Push new record to samep */
910 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
911 /* Move across elements that are in the unblamable portion */
913 *srcq
= &diffp
->next
;
916 struct blame_chunk_cb_data
{
917 struct blame_origin
*parent
;
919 struct blame_entry
**dstq
;
920 struct blame_entry
**srcq
;
923 /* diff chunks are from parent to target */
924 static int blame_chunk_cb(long start_a
, long count_a
,
925 long start_b
, long count_b
, void *data
)
927 struct blame_chunk_cb_data
*d
= data
;
928 if (start_a
- start_b
!= d
->offset
)
929 die("internal error in blame::blame_chunk_cb");
930 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
931 start_b
+ count_b
, d
->parent
);
932 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
937 * We are looking at the origin 'target' and aiming to pass blame
938 * for the lines it is suspected to its parent. Run diff to find
939 * which lines came from parent and pass blame for them.
941 static void pass_blame_to_parent(struct blame_scoreboard
*sb
,
942 struct blame_origin
*target
,
943 struct blame_origin
*parent
)
945 mmfile_t file_p
, file_o
;
946 struct blame_chunk_cb_data d
;
947 struct blame_entry
*newdest
= NULL
;
949 if (!target
->suspects
)
950 return; /* nothing remains for this target */
954 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
956 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
957 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
, &sb
->num_read_blob
);
960 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, sb
->xdl_opts
))
961 die("unable to generate diff (%s -> %s)",
962 oid_to_hex(&parent
->commit
->object
.oid
),
963 oid_to_hex(&target
->commit
->object
.oid
));
964 /* The rest are the same as the parent */
965 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
967 queue_blames(sb
, parent
, newdest
);
973 * The lines in blame_entry after splitting blames many times can become
974 * very small and trivial, and at some point it becomes pointless to
975 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
976 * ordinary C program, and it is not worth to say it was copied from
977 * totally unrelated file in the parent.
979 * Compute how trivial the lines in the blame_entry are.
981 unsigned blame_entry_score(struct blame_scoreboard
*sb
, struct blame_entry
*e
)
990 cp
= blame_nth_line(sb
, e
->lno
);
991 ep
= blame_nth_line(sb
, e
->lno
+ e
->num_lines
);
993 unsigned ch
= *((unsigned char *)cp
);
1003 * best_so_far[] and potential[] are both a split of an existing blame_entry
1004 * that passes blame to the parent. Maintain best_so_far the best split so
1005 * far, by comparing potential and best_so_far and copying potential into
1006 * bst_so_far as needed.
1008 static void copy_split_if_better(struct blame_scoreboard
*sb
,
1009 struct blame_entry
*best_so_far
,
1010 struct blame_entry
*potential
)
1014 if (!potential
[1].suspect
)
1016 if (best_so_far
[1].suspect
) {
1017 if (blame_entry_score(sb
, &potential
[1]) <
1018 blame_entry_score(sb
, &best_so_far
[1]))
1022 for (i
= 0; i
< 3; i
++)
1023 blame_origin_incref(potential
[i
].suspect
);
1024 decref_split(best_so_far
);
1025 memcpy(best_so_far
, potential
, sizeof(struct blame_entry
[3]));
1029 * We are looking at a part of the final image represented by
1030 * ent (tlno and same are offset by ent->s_lno).
1031 * tlno is where we are looking at in the final image.
1032 * up to (but not including) same match preimage.
1033 * plno is where we are looking at in the preimage.
1035 * <-------------- final image ---------------------->
1038 * <---------preimage----->
1041 * All line numbers are 0-based.
1043 static void handle_split(struct blame_scoreboard
*sb
,
1044 struct blame_entry
*ent
,
1045 int tlno
, int plno
, int same
,
1046 struct blame_origin
*parent
,
1047 struct blame_entry
*split
)
1049 if (ent
->num_lines
<= tlno
)
1052 struct blame_entry potential
[3];
1055 split_overlap(potential
, ent
, tlno
, plno
, same
, parent
);
1056 copy_split_if_better(sb
, split
, potential
);
1057 decref_split(potential
);
1061 struct handle_split_cb_data
{
1062 struct blame_scoreboard
*sb
;
1063 struct blame_entry
*ent
;
1064 struct blame_origin
*parent
;
1065 struct blame_entry
*split
;
1070 static int handle_split_cb(long start_a
, long count_a
,
1071 long start_b
, long count_b
, void *data
)
1073 struct handle_split_cb_data
*d
= data
;
1074 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1076 d
->plno
= start_a
+ count_a
;
1077 d
->tlno
= start_b
+ count_b
;
1082 * Find the lines from parent that are the same as ent so that
1083 * we can pass blames to it. file_p has the blob contents for
1086 static void find_copy_in_blob(struct blame_scoreboard
*sb
,
1087 struct blame_entry
*ent
,
1088 struct blame_origin
*parent
,
1089 struct blame_entry
*split
,
1094 struct handle_split_cb_data d
;
1096 memset(&d
, 0, sizeof(d
));
1097 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1099 * Prepare mmfile that contains only the lines in ent.
1101 cp
= blame_nth_line(sb
, ent
->lno
);
1102 file_o
.ptr
= (char *) cp
;
1103 file_o
.size
= blame_nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1106 * file_o is a part of final image we are annotating.
1107 * file_p partially may match that image.
1109 memset(split
, 0, sizeof(struct blame_entry
[3]));
1110 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, sb
->xdl_opts
))
1111 die("unable to generate diff (%s)",
1112 oid_to_hex(&parent
->commit
->object
.oid
));
1113 /* remainder, if any, all match the preimage */
1114 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1117 /* Move all blame entries from list *source that have a score smaller
1118 * than score_min to the front of list *small.
1119 * Returns a pointer to the link pointing to the old head of the small list.
1122 static struct blame_entry
**filter_small(struct blame_scoreboard
*sb
,
1123 struct blame_entry
**small
,
1124 struct blame_entry
**source
,
1127 struct blame_entry
*p
= *source
;
1128 struct blame_entry
*oldsmall
= *small
;
1130 if (blame_entry_score(sb
, p
) <= score_min
) {
1146 * See if lines currently target is suspected for can be attributed to
1149 static void find_move_in_parent(struct blame_scoreboard
*sb
,
1150 struct blame_entry
***blamed
,
1151 struct blame_entry
**toosmall
,
1152 struct blame_origin
*target
,
1153 struct blame_origin
*parent
)
1155 struct blame_entry
*e
, split
[3];
1156 struct blame_entry
*unblamed
= target
->suspects
;
1157 struct blame_entry
*leftover
= NULL
;
1161 return; /* nothing remains for this target */
1163 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
1167 /* At each iteration, unblamed has a NULL-terminated list of
1168 * entries that have not yet been tested for blame. leftover
1169 * contains the reversed list of entries that have been tested
1170 * without being assignable to the parent.
1173 struct blame_entry
**unblamedtail
= &unblamed
;
1174 struct blame_entry
*next
;
1175 for (e
= unblamed
; e
; e
= next
) {
1177 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1178 if (split
[1].suspect
&&
1179 sb
->move_score
< blame_entry_score(sb
, &split
[1])) {
1180 split_blame(blamed
, &unblamedtail
, split
, e
);
1185 decref_split(split
);
1187 *unblamedtail
= NULL
;
1188 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->move_score
);
1190 target
->suspects
= reverse_blame(leftover
, NULL
);
1194 struct blame_entry
*ent
;
1195 struct blame_entry split
[3];
1199 * Count the number of entries the target is suspected for,
1200 * and prepare a list of entry and the best split.
1202 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1205 struct blame_entry
*e
;
1207 struct blame_list
*blame_list
= NULL
;
1209 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1212 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1213 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1214 blame_list
[i
++].ent
= e
;
1216 *num_ents_p
= num_ents
;
1221 * For lines target is suspected for, see if we can find code movement
1222 * across file boundary from the parent commit. porigin is the path
1223 * in the parent we already tried.
1225 static void find_copy_in_parent(struct blame_scoreboard
*sb
,
1226 struct blame_entry
***blamed
,
1227 struct blame_entry
**toosmall
,
1228 struct blame_origin
*target
,
1229 struct commit
*parent
,
1230 struct blame_origin
*porigin
,
1233 struct diff_options diff_opts
;
1235 struct blame_list
*blame_list
;
1237 struct blame_entry
*unblamed
= target
->suspects
;
1238 struct blame_entry
*leftover
= NULL
;
1241 return; /* nothing remains for this target */
1243 diff_setup(&diff_opts
);
1244 diff_opts
.flags
.recursive
= 1;
1245 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1247 diff_setup_done(&diff_opts
);
1249 /* Try "find copies harder" on new path if requested;
1250 * we do not want to use diffcore_rename() actually to
1251 * match things up; find_copies_harder is set only to
1252 * force diff_tree_oid() to feed all filepairs to diff_queue,
1253 * and this code needs to be after diff_setup_done(), which
1254 * usually makes find-copies-harder imply copy detection.
1256 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1257 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1258 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1259 diff_opts
.flags
.find_copies_harder
= 1;
1261 if (is_null_oid(&target
->commit
->object
.oid
))
1262 do_diff_cache(&parent
->tree
->object
.oid
, &diff_opts
);
1264 diff_tree_oid(&parent
->tree
->object
.oid
,
1265 &target
->commit
->tree
->object
.oid
,
1268 if (!diff_opts
.flags
.find_copies_harder
)
1269 diffcore_std(&diff_opts
);
1272 struct blame_entry
**unblamedtail
= &unblamed
;
1273 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1275 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1276 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1277 struct blame_origin
*norigin
;
1279 struct blame_entry potential
[3];
1281 if (!DIFF_FILE_VALID(p
->one
))
1282 continue; /* does not exist in parent */
1283 if (S_ISGITLINK(p
->one
->mode
))
1284 continue; /* ignore git links */
1285 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1286 /* find_move already dealt with this path */
1289 norigin
= get_origin(parent
, p
->one
->path
);
1290 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
1291 norigin
->mode
= p
->one
->mode
;
1292 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
, &sb
->num_read_blob
);
1296 for (j
= 0; j
< num_ents
; j
++) {
1297 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1298 norigin
, potential
, &file_p
);
1299 copy_split_if_better(sb
, blame_list
[j
].split
,
1301 decref_split(potential
);
1303 blame_origin_decref(norigin
);
1306 for (j
= 0; j
< num_ents
; j
++) {
1307 struct blame_entry
*split
= blame_list
[j
].split
;
1308 if (split
[1].suspect
&&
1309 sb
->copy_score
< blame_entry_score(sb
, &split
[1])) {
1310 split_blame(blamed
, &unblamedtail
, split
,
1313 blame_list
[j
].ent
->next
= leftover
;
1314 leftover
= blame_list
[j
].ent
;
1316 decref_split(split
);
1319 *unblamedtail
= NULL
;
1320 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->copy_score
);
1322 target
->suspects
= reverse_blame(leftover
, NULL
);
1323 diff_flush(&diff_opts
);
1324 clear_pathspec(&diff_opts
.pathspec
);
1328 * The blobs of origin and porigin exactly match, so everything
1329 * origin is suspected for can be blamed on the parent.
1331 static void pass_whole_blame(struct blame_scoreboard
*sb
,
1332 struct blame_origin
*origin
, struct blame_origin
*porigin
)
1334 struct blame_entry
*e
, *suspects
;
1336 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1337 /* Steal its file */
1338 porigin
->file
= origin
->file
;
1339 origin
->file
.ptr
= NULL
;
1341 suspects
= origin
->suspects
;
1342 origin
->suspects
= NULL
;
1343 for (e
= suspects
; e
; e
= e
->next
) {
1344 blame_origin_incref(porigin
);
1345 blame_origin_decref(e
->suspect
);
1346 e
->suspect
= porigin
;
1348 queue_blames(sb
, porigin
, suspects
);
1352 * We pass blame from the current commit to its parents. We keep saying
1353 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1354 * exonerate ourselves.
1356 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
,
1360 if (revs
->first_parent_only
&&
1362 commit
->parents
->next
) {
1363 free_commit_list(commit
->parents
->next
);
1364 commit
->parents
->next
= NULL
;
1366 return commit
->parents
;
1368 return lookup_decoration(&revs
->children
, &commit
->object
);
1371 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
, int reverse
)
1373 struct commit_list
*l
= first_scapegoat(revs
, commit
, reverse
);
1374 return commit_list_count(l
);
1377 /* Distribute collected unsorted blames to the respected sorted lists
1378 * in the various origins.
1380 static void distribute_blame(struct blame_scoreboard
*sb
, struct blame_entry
*blamed
)
1382 blamed
= llist_mergesort(blamed
, get_next_blame
, set_next_blame
,
1383 compare_blame_suspect
);
1386 struct blame_origin
*porigin
= blamed
->suspect
;
1387 struct blame_entry
*suspects
= NULL
;
1389 struct blame_entry
*next
= blamed
->next
;
1390 blamed
->next
= suspects
;
1393 } while (blamed
&& blamed
->suspect
== porigin
);
1394 suspects
= reverse_blame(suspects
, NULL
);
1395 queue_blames(sb
, porigin
, suspects
);
1401 static void pass_blame(struct blame_scoreboard
*sb
, struct blame_origin
*origin
, int opt
)
1403 struct rev_info
*revs
= sb
->revs
;
1404 int i
, pass
, num_sg
;
1405 struct commit
*commit
= origin
->commit
;
1406 struct commit_list
*sg
;
1407 struct blame_origin
*sg_buf
[MAXSG
];
1408 struct blame_origin
*porigin
, **sg_origin
= sg_buf
;
1409 struct blame_entry
*toosmall
= NULL
;
1410 struct blame_entry
*blames
, **blametail
= &blames
;
1412 num_sg
= num_scapegoats(revs
, commit
, sb
->reverse
);
1415 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1416 memset(sg_buf
, 0, sizeof(sg_buf
));
1418 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1421 * The first pass looks for unrenamed path to optimize for
1422 * common cases, then we look for renames in the second pass.
1424 for (pass
= 0; pass
< 2 - sb
->no_whole_file_rename
; pass
++) {
1425 struct blame_origin
*(*find
)(struct commit
*, struct blame_origin
*);
1426 find
= pass
? find_rename
: find_origin
;
1428 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1430 sg
= sg
->next
, i
++) {
1431 struct commit
*p
= sg
->item
;
1436 if (parse_commit(p
))
1438 porigin
= find(p
, origin
);
1441 if (!oidcmp(&porigin
->blob_oid
, &origin
->blob_oid
)) {
1442 pass_whole_blame(sb
, origin
, porigin
);
1443 blame_origin_decref(porigin
);
1446 for (j
= same
= 0; j
< i
; j
++)
1448 !oidcmp(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
1453 sg_origin
[i
] = porigin
;
1455 blame_origin_decref(porigin
);
1460 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1462 sg
= sg
->next
, i
++) {
1463 struct blame_origin
*porigin
= sg_origin
[i
];
1466 if (!origin
->previous
) {
1467 blame_origin_incref(porigin
);
1468 origin
->previous
= porigin
;
1470 pass_blame_to_parent(sb
, origin
, porigin
);
1471 if (!origin
->suspects
)
1476 * Optionally find moves in parents' files.
1478 if (opt
& PICKAXE_BLAME_MOVE
) {
1479 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->move_score
);
1480 if (origin
->suspects
) {
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 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1488 if (!origin
->suspects
)
1495 * Optionally find copies from parents' files.
1497 if (opt
& PICKAXE_BLAME_COPY
) {
1498 if (sb
->copy_score
> sb
->move_score
)
1499 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1500 else if (sb
->copy_score
< sb
->move_score
) {
1501 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1503 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1505 if (!origin
->suspects
)
1508 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1510 sg
= sg
->next
, i
++) {
1511 struct blame_origin
*porigin
= sg_origin
[i
];
1512 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1513 origin
, sg
->item
, porigin
, opt
);
1514 if (!origin
->suspects
)
1521 distribute_blame(sb
, blames
);
1523 * prepend toosmall to origin->suspects
1525 * There is no point in sorting: this ends up on a big
1526 * unsorted list in the caller anyway.
1529 struct blame_entry
**tail
= &toosmall
;
1531 tail
= &(*tail
)->next
;
1532 *tail
= origin
->suspects
;
1533 origin
->suspects
= toosmall
;
1535 for (i
= 0; i
< num_sg
; i
++) {
1537 drop_origin_blob(sg_origin
[i
]);
1538 blame_origin_decref(sg_origin
[i
]);
1541 drop_origin_blob(origin
);
1542 if (sg_buf
!= sg_origin
)
1547 * The main loop -- while we have blobs with lines whose true origin
1548 * is still unknown, pick one blob, and allow its lines to pass blames
1549 * to its parents. */
1550 void assign_blame(struct blame_scoreboard
*sb
, int opt
)
1552 struct rev_info
*revs
= sb
->revs
;
1553 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1556 struct blame_entry
*ent
;
1557 struct blame_origin
*suspect
= commit
->util
;
1559 /* find one suspect to break down */
1560 while (suspect
&& !suspect
->suspects
)
1561 suspect
= suspect
->next
;
1564 commit
= prio_queue_get(&sb
->commits
);
1568 assert(commit
== suspect
->commit
);
1571 * We will use this suspect later in the loop,
1572 * so hold onto it in the meantime.
1574 blame_origin_incref(suspect
);
1575 parse_commit(commit
);
1577 (!(commit
->object
.flags
& UNINTERESTING
) &&
1578 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1579 pass_blame(sb
, suspect
, opt
);
1581 commit
->object
.flags
|= UNINTERESTING
;
1582 if (commit
->object
.parsed
)
1583 mark_parents_uninteresting(commit
);
1585 /* treat root commit as boundary */
1586 if (!commit
->parents
&& !sb
->show_root
)
1587 commit
->object
.flags
|= UNINTERESTING
;
1589 /* Take responsibility for the remaining entries */
1590 ent
= suspect
->suspects
;
1592 suspect
->guilty
= 1;
1594 struct blame_entry
*next
= ent
->next
;
1595 if (sb
->found_guilty_entry
)
1596 sb
->found_guilty_entry(ent
, sb
->found_guilty_entry_data
);
1601 ent
->next
= sb
->ent
;
1602 sb
->ent
= suspect
->suspects
;
1603 suspect
->suspects
= NULL
;
1607 blame_origin_decref(suspect
);
1609 if (sb
->debug
) /* sanity */
1610 sanity_check_refcnt(sb
);
1614 static const char *get_next_line(const char *start
, const char *end
)
1616 const char *nl
= memchr(start
, '\n', end
- start
);
1617 return nl
? nl
+ 1 : end
;
1621 * To allow quick access to the contents of nth line in the
1622 * final image, prepare an index in the scoreboard.
1624 static int prepare_lines(struct blame_scoreboard
*sb
)
1626 const char *buf
= sb
->final_buf
;
1627 unsigned long len
= sb
->final_buf_size
;
1628 const char *end
= buf
+ len
;
1633 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1636 ALLOC_ARRAY(sb
->lineno
, num
+ 1);
1637 lineno
= sb
->lineno
;
1639 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1640 *lineno
++ = p
- buf
;
1644 sb
->num_lines
= num
;
1645 return sb
->num_lines
;
1648 static struct commit
*find_single_final(struct rev_info
*revs
,
1649 const char **name_p
)
1652 struct commit
*found
= NULL
;
1653 const char *name
= NULL
;
1655 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1656 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1657 if (obj
->flags
& UNINTERESTING
)
1659 obj
= deref_tag(obj
, NULL
, 0);
1660 if (obj
->type
!= OBJ_COMMIT
)
1661 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1663 die("More than one commit to dig from %s and %s?",
1664 revs
->pending
.objects
[i
].name
, name
);
1665 found
= (struct commit
*)obj
;
1666 name
= revs
->pending
.objects
[i
].name
;
1669 *name_p
= xstrdup_or_null(name
);
1673 static struct commit
*dwim_reverse_initial(struct rev_info
*revs
,
1674 const char **name_p
)
1677 * DWIM "git blame --reverse ONE -- PATH" as
1678 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1679 * when it makes sense.
1682 struct commit
*head_commit
;
1683 struct object_id head_oid
;
1685 if (revs
->pending
.nr
!= 1)
1688 /* Is that sole rev a committish? */
1689 obj
= revs
->pending
.objects
[0].item
;
1690 obj
= deref_tag(obj
, NULL
, 0);
1691 if (obj
->type
!= OBJ_COMMIT
)
1694 /* Do we have HEAD? */
1695 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
1697 head_commit
= lookup_commit_reference_gently(&head_oid
, 1);
1701 /* Turn "ONE" into "ONE..HEAD" then */
1702 obj
->flags
|= UNINTERESTING
;
1703 add_pending_object(revs
, &head_commit
->object
, "HEAD");
1706 *name_p
= revs
->pending
.objects
[0].name
;
1707 return (struct commit
*)obj
;
1710 static struct commit
*find_single_initial(struct rev_info
*revs
,
1711 const char **name_p
)
1714 struct commit
*found
= NULL
;
1715 const char *name
= NULL
;
1718 * There must be one and only one negative commit, and it must be
1721 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1722 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1723 if (!(obj
->flags
& UNINTERESTING
))
1725 obj
= deref_tag(obj
, NULL
, 0);
1726 if (obj
->type
!= OBJ_COMMIT
)
1727 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1729 die("More than one commit to dig up from, %s and %s?",
1730 revs
->pending
.objects
[i
].name
, name
);
1731 found
= (struct commit
*) obj
;
1732 name
= revs
->pending
.objects
[i
].name
;
1736 found
= dwim_reverse_initial(revs
, &name
);
1738 die("No commit to dig up from?");
1741 *name_p
= xstrdup(name
);
1745 void init_scoreboard(struct blame_scoreboard
*sb
)
1747 memset(sb
, 0, sizeof(struct blame_scoreboard
));
1748 sb
->move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1749 sb
->copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1752 void setup_scoreboard(struct blame_scoreboard
*sb
, const char *path
, struct blame_origin
**orig
)
1754 const char *final_commit_name
= NULL
;
1755 struct blame_origin
*o
;
1756 struct commit
*final_commit
= NULL
;
1757 enum object_type type
;
1759 if (sb
->reverse
&& sb
->contents_from
)
1760 die(_("--contents and --reverse do not blend well."));
1763 sb
->final
= find_single_final(sb
->revs
, &final_commit_name
);
1764 sb
->commits
.compare
= compare_commits_by_commit_date
;
1766 sb
->final
= find_single_initial(sb
->revs
, &final_commit_name
);
1767 sb
->commits
.compare
= compare_commits_by_reverse_commit_date
;
1770 if (sb
->final
&& sb
->contents_from
)
1771 die(_("cannot use --contents with final commit object name"));
1773 if (sb
->reverse
&& sb
->revs
->first_parent_only
)
1774 sb
->revs
->children
.name
= NULL
;
1778 * "--not A B -- path" without anything positive;
1779 * do not default to HEAD, but use the working tree
1783 sb
->final
= fake_working_tree_commit(&sb
->revs
->diffopt
,
1784 path
, sb
->contents_from
);
1785 add_pending_object(sb
->revs
, &(sb
->final
->object
), ":");
1788 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1789 final_commit
= find_single_final(sb
->revs
, NULL
);
1791 die(_("--reverse and --first-parent together require specified latest commit"));
1795 * If we have bottom, this will mark the ancestors of the
1796 * bottom commits we would reach while traversing as
1799 if (prepare_revision_walk(sb
->revs
))
1800 die(_("revision walk setup failed"));
1802 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1803 struct commit
*c
= final_commit
;
1805 sb
->revs
->children
.name
= "children";
1806 while (c
->parents
&&
1807 oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
)) {
1808 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1811 if (add_decoration(&sb
->revs
->children
,
1812 &c
->parents
->item
->object
, l
))
1813 die("BUG: not unique item in first-parent chain");
1814 c
= c
->parents
->item
;
1817 if (oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
))
1818 die(_("--reverse --first-parent together require range along first-parent chain"));
1821 if (is_null_oid(&sb
->final
->object
.oid
)) {
1822 o
= sb
->final
->util
;
1823 sb
->final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
1824 sb
->final_buf_size
= o
->file
.size
;
1827 o
= get_origin(sb
->final
, path
);
1828 if (fill_blob_sha1_and_mode(o
))
1829 die(_("no such path %s in %s"), path
, final_commit_name
);
1831 if (sb
->revs
->diffopt
.flags
.allow_textconv
&&
1832 textconv_object(path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
->final_buf
,
1833 &sb
->final_buf_size
))
1836 sb
->final_buf
= read_object_file(&o
->blob_oid
, &type
,
1837 &sb
->final_buf_size
);
1840 die(_("cannot read blob %s for path %s"),
1841 oid_to_hex(&o
->blob_oid
),
1844 sb
->num_read_blob
++;
1850 free((char *)final_commit_name
);
1855 struct blame_entry
*blame_entry_prepend(struct blame_entry
*head
,
1856 long start
, long end
,
1857 struct blame_origin
*o
)
1859 struct blame_entry
*new_head
= xcalloc(1, sizeof(struct blame_entry
));
1860 new_head
->lno
= start
;
1861 new_head
->num_lines
= end
- start
;
1862 new_head
->suspect
= o
;
1863 new_head
->s_lno
= start
;
1864 new_head
->next
= head
;
1865 blame_origin_incref(o
);