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
, path
, &blob_oid
, &mode
) &&
84 oid_object_info(the_repository
, &blob_oid
, 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 (opt
->flags
.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_object_file(buf
.buf
, buf
.len
, OBJ_BLOB
, &origin
->blob_oid
);
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 ce
= make_empty_cache_entry(&the_index
, len
);
256 oidcpy(&ce
->oid
, &origin
->blob_oid
);
257 memcpy(ce
->name
, path
, len
);
258 ce
->ce_flags
= create_ce_flags(0);
259 ce
->ce_namelen
= len
;
260 ce
->ce_mode
= create_ce_mode(mode
);
261 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
263 cache_tree_invalidate_path(&the_index
, path
);
270 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
271 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
, int xdl_opts
)
274 xdemitconf_t xecfg
= {0};
275 xdemitcb_t ecb
= {NULL
};
277 xpp
.flags
= xdl_opts
;
278 xecfg
.hunk_func
= hunk_func
;
280 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
284 * Given an origin, prepare mmfile_t structure to be used by the
287 static void fill_origin_blob(struct diff_options
*opt
,
288 struct blame_origin
*o
, mmfile_t
*file
, int *num_read_blob
)
291 enum object_type type
;
292 unsigned long file_size
;
295 if (opt
->flags
.allow_textconv
&&
296 textconv_object(o
->path
, o
->mode
, &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
299 file
->ptr
= read_object_file(&o
->blob_oid
, &type
,
301 file
->size
= file_size
;
304 die("Cannot read blob %s for path %s",
305 oid_to_hex(&o
->blob_oid
),
313 static void drop_origin_blob(struct blame_origin
*o
)
316 FREE_AND_NULL(o
->file
.ptr
);
321 * Any merge of blames happens on lists of blames that arrived via
322 * different parents in a single suspect. In this case, we want to
323 * sort according to the suspect line numbers as opposed to the final
324 * image line numbers. The function body is somewhat longish because
325 * it avoids unnecessary writes.
328 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
329 struct blame_entry
*list2
)
331 struct blame_entry
*p1
= list1
, *p2
= list2
,
339 if (p1
->s_lno
<= p2
->s_lno
) {
342 if ((p1
= *tail
) == NULL
) {
346 } while (p1
->s_lno
<= p2
->s_lno
);
352 if ((p2
= *tail
) == NULL
) {
356 } while (p1
->s_lno
> p2
->s_lno
);
360 if ((p1
= *tail
) == NULL
) {
364 } while (p1
->s_lno
<= p2
->s_lno
);
368 static void *get_next_blame(const void *p
)
370 return ((struct blame_entry
*)p
)->next
;
373 static void set_next_blame(void *p1
, void *p2
)
375 ((struct blame_entry
*)p1
)->next
= p2
;
379 * Final image line numbers are all different, so we don't need a
380 * three-way comparison here.
383 static int compare_blame_final(const void *p1
, const void *p2
)
385 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
389 static int compare_blame_suspect(const void *p1
, const void *p2
)
391 const struct blame_entry
*s1
= p1
, *s2
= p2
;
393 * to allow for collating suspects, we sort according to the
394 * respective pointer value as the primary sorting criterion.
395 * The actual relation is pretty unimportant as long as it
396 * establishes a total order. Comparing as integers gives us
399 if (s1
->suspect
!= s2
->suspect
)
400 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
401 if (s1
->s_lno
== s2
->s_lno
)
403 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
406 void blame_sort_final(struct blame_scoreboard
*sb
)
408 sb
->ent
= llist_mergesort(sb
->ent
, get_next_blame
, set_next_blame
,
409 compare_blame_final
);
412 static int compare_commits_by_reverse_commit_date(const void *a
,
416 return -compare_commits_by_commit_date(a
, b
, c
);
420 * For debugging -- origin is refcounted, and this asserts that
421 * we do not underflow.
423 static void sanity_check_refcnt(struct blame_scoreboard
*sb
)
426 struct blame_entry
*ent
;
428 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
429 /* Nobody should have zero or negative refcnt */
430 if (ent
->suspect
->refcnt
<= 0) {
431 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
433 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
434 ent
->suspect
->refcnt
);
439 sb
->on_sanity_fail(sb
, baa
);
443 * If two blame entries that are next to each other came from
444 * contiguous lines in the same origin (i.e. <commit, path> pair),
445 * merge them together.
447 void blame_coalesce(struct blame_scoreboard
*sb
)
449 struct blame_entry
*ent
, *next
;
451 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
452 if (ent
->suspect
== next
->suspect
&&
453 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
454 ent
->num_lines
+= next
->num_lines
;
455 ent
->next
= next
->next
;
456 blame_origin_decref(next
->suspect
);
459 next
= ent
; /* again */
463 if (sb
->debug
) /* sanity */
464 sanity_check_refcnt(sb
);
468 * Merge the given sorted list of blames into a preexisting origin.
469 * If there were no previous blames to that commit, it is entered into
470 * the commit priority queue of the score board.
473 static void queue_blames(struct blame_scoreboard
*sb
, struct blame_origin
*porigin
,
474 struct blame_entry
*sorted
)
476 if (porigin
->suspects
)
477 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
479 struct blame_origin
*o
;
480 for (o
= porigin
->commit
->util
; o
; o
= o
->next
) {
482 porigin
->suspects
= sorted
;
486 porigin
->suspects
= sorted
;
487 prio_queue_put(&sb
->commits
, porigin
->commit
);
492 * Fill the blob_sha1 field of an origin if it hasn't, so that later
493 * call to fill_origin_blob() can use it to locate the data. blob_sha1
494 * for an origin is also used to pass the blame for the entire file to
495 * the parent to detect the case where a child's blob is identical to
496 * that of its parent's.
498 * This also fills origin->mode for corresponding tree path.
500 static int fill_blob_sha1_and_mode(struct blame_origin
*origin
)
502 if (!is_null_oid(&origin
->blob_oid
))
504 if (get_tree_entry(&origin
->commit
->object
.oid
, origin
->path
, &origin
->blob_oid
, &origin
->mode
))
506 if (oid_object_info(the_repository
, &origin
->blob_oid
, NULL
) != OBJ_BLOB
)
510 oidclr(&origin
->blob_oid
);
511 origin
->mode
= S_IFINVALID
;
516 * We have an origin -- check if the same path exists in the
517 * parent and return an origin structure to represent it.
519 static struct blame_origin
*find_origin(struct commit
*parent
,
520 struct blame_origin
*origin
)
522 struct blame_origin
*porigin
;
523 struct diff_options diff_opts
;
524 const char *paths
[2];
526 /* First check any existing origins */
527 for (porigin
= parent
->util
; porigin
; porigin
= porigin
->next
)
528 if (!strcmp(porigin
->path
, origin
->path
)) {
530 * The same path between origin and its parent
531 * without renaming -- the most common case.
533 return blame_origin_incref (porigin
);
536 /* See if the origin->path is different between parent
537 * and origin first. Most of the time they are the
538 * same and diff-tree is fairly efficient about this.
540 diff_setup(&diff_opts
);
541 diff_opts
.flags
.recursive
= 1;
542 diff_opts
.detect_rename
= 0;
543 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
544 paths
[0] = origin
->path
;
547 parse_pathspec(&diff_opts
.pathspec
,
548 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
549 PATHSPEC_LITERAL_PATH
, "", paths
);
550 diff_setup_done(&diff_opts
);
552 if (is_null_oid(&origin
->commit
->object
.oid
))
553 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
555 diff_tree_oid(get_commit_tree_oid(parent
),
556 get_commit_tree_oid(origin
->commit
),
558 diffcore_std(&diff_opts
);
560 if (!diff_queued_diff
.nr
) {
561 /* The path is the same as parent */
562 porigin
= get_origin(parent
, origin
->path
);
563 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
564 porigin
->mode
= origin
->mode
;
567 * Since origin->path is a pathspec, if the parent
568 * commit had it as a directory, we will see a whole
569 * bunch of deletion of files in the directory that we
573 struct diff_filepair
*p
= NULL
;
574 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
576 p
= diff_queued_diff
.queue
[i
];
577 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
578 if (!strcmp(name
, origin
->path
))
582 die("internal error in blame::find_origin");
585 die("internal error in blame::find_origin (%c)",
588 porigin
= get_origin(parent
, origin
->path
);
589 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
590 porigin
->mode
= p
->one
->mode
;
594 /* Did not exist in parent, or type changed */
598 diff_flush(&diff_opts
);
599 clear_pathspec(&diff_opts
.pathspec
);
604 * We have an origin -- find the path that corresponds to it in its
605 * parent and return an origin structure to represent it.
607 static struct blame_origin
*find_rename(struct commit
*parent
,
608 struct blame_origin
*origin
)
610 struct blame_origin
*porigin
= NULL
;
611 struct diff_options diff_opts
;
614 diff_setup(&diff_opts
);
615 diff_opts
.flags
.recursive
= 1;
616 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
617 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
618 diff_opts
.single_follow
= origin
->path
;
619 diff_setup_done(&diff_opts
);
621 if (is_null_oid(&origin
->commit
->object
.oid
))
622 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
624 diff_tree_oid(get_commit_tree_oid(parent
),
625 get_commit_tree_oid(origin
->commit
),
627 diffcore_std(&diff_opts
);
629 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
630 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
631 if ((p
->status
== 'R' || p
->status
== 'C') &&
632 !strcmp(p
->two
->path
, origin
->path
)) {
633 porigin
= get_origin(parent
, p
->one
->path
);
634 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
635 porigin
->mode
= p
->one
->mode
;
639 diff_flush(&diff_opts
);
640 clear_pathspec(&diff_opts
.pathspec
);
645 * Append a new blame entry to a given output queue.
647 static void add_blame_entry(struct blame_entry
***queue
,
648 const struct blame_entry
*src
)
650 struct blame_entry
*e
= xmalloc(sizeof(*e
));
651 memcpy(e
, src
, sizeof(*e
));
652 blame_origin_incref(e
->suspect
);
660 * src typically is on-stack; we want to copy the information in it to
661 * a malloced blame_entry that gets added to the given queue. The
662 * origin of dst loses a refcnt.
664 static void dup_entry(struct blame_entry
***queue
,
665 struct blame_entry
*dst
, struct blame_entry
*src
)
667 blame_origin_incref(src
->suspect
);
668 blame_origin_decref(dst
->suspect
);
669 memcpy(dst
, src
, sizeof(*src
));
675 const char *blame_nth_line(struct blame_scoreboard
*sb
, long lno
)
677 return sb
->final_buf
+ sb
->lineno
[lno
];
681 * It is known that lines between tlno to same came from parent, and e
682 * has an overlap with that range. it also is known that parent's
683 * line plno corresponds to e's line tlno.
689 * <------------------>
691 * Split e into potentially three parts; before this chunk, the chunk
692 * to be blamed for the parent, and after that portion.
694 static void split_overlap(struct blame_entry
*split
,
695 struct blame_entry
*e
,
696 int tlno
, int plno
, int same
,
697 struct blame_origin
*parent
)
700 memset(split
, 0, sizeof(struct blame_entry
[3]));
702 if (e
->s_lno
< tlno
) {
703 /* there is a pre-chunk part not blamed on parent */
704 split
[0].suspect
= blame_origin_incref(e
->suspect
);
705 split
[0].lno
= e
->lno
;
706 split
[0].s_lno
= e
->s_lno
;
707 split
[0].num_lines
= tlno
- e
->s_lno
;
708 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
709 split
[1].s_lno
= plno
;
712 split
[1].lno
= e
->lno
;
713 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
716 if (same
< e
->s_lno
+ e
->num_lines
) {
717 /* there is a post-chunk part not blamed on parent */
718 split
[2].suspect
= blame_origin_incref(e
->suspect
);
719 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
720 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
721 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
722 chunk_end_lno
= split
[2].lno
;
725 chunk_end_lno
= e
->lno
+ e
->num_lines
;
726 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
729 * if it turns out there is nothing to blame the parent for,
730 * forget about the splitting. !split[1].suspect signals this.
732 if (split
[1].num_lines
< 1)
734 split
[1].suspect
= blame_origin_incref(parent
);
738 * split_overlap() divided an existing blame e into up to three parts
739 * in split. Any assigned blame is moved to queue to
742 static void split_blame(struct blame_entry
***blamed
,
743 struct blame_entry
***unblamed
,
744 struct blame_entry
*split
,
745 struct blame_entry
*e
)
747 if (split
[0].suspect
&& split
[2].suspect
) {
748 /* The first part (reuse storage for the existing entry e) */
749 dup_entry(unblamed
, e
, &split
[0]);
751 /* The last part -- me */
752 add_blame_entry(unblamed
, &split
[2]);
754 /* ... and the middle part -- parent */
755 add_blame_entry(blamed
, &split
[1]);
757 else if (!split
[0].suspect
&& !split
[2].suspect
)
759 * The parent covers the entire area; reuse storage for
760 * e and replace it with the parent.
762 dup_entry(blamed
, e
, &split
[1]);
763 else if (split
[0].suspect
) {
764 /* me and then parent */
765 dup_entry(unblamed
, e
, &split
[0]);
766 add_blame_entry(blamed
, &split
[1]);
769 /* parent and then me */
770 dup_entry(blamed
, e
, &split
[1]);
771 add_blame_entry(unblamed
, &split
[2]);
776 * After splitting the blame, the origins used by the
777 * on-stack blame_entry should lose one refcnt each.
779 static void decref_split(struct blame_entry
*split
)
783 for (i
= 0; i
< 3; i
++)
784 blame_origin_decref(split
[i
].suspect
);
788 * reverse_blame reverses the list given in head, appending tail.
789 * That allows us to build lists in reverse order, then reverse them
790 * afterwards. This can be faster than building the list in proper
791 * order right away. The reason is that building in proper order
792 * requires writing a link in the _previous_ element, while building
793 * in reverse order just requires placing the list head into the
797 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
798 struct blame_entry
*tail
)
801 struct blame_entry
*next
= head
->next
;
810 * Process one hunk from the patch between the current suspect for
811 * blame_entry e and its parent. This first blames any unfinished
812 * entries before the chunk (which is where target and parent start
813 * differing) on the parent, and then splits blame entries at the
814 * start and at the end of the difference region. Since use of -M and
815 * -C options may lead to overlapping/duplicate source line number
816 * ranges, all we can rely on from sorting/merging is the order of the
817 * first suspect line number.
819 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
820 int tlno
, int offset
, int same
,
821 struct blame_origin
*parent
)
823 struct blame_entry
*e
= **srcq
;
824 struct blame_entry
*samep
= NULL
, *diffp
= NULL
;
826 while (e
&& e
->s_lno
< tlno
) {
827 struct blame_entry
*next
= e
->next
;
829 * current record starts before differing portion. If
830 * it reaches into it, we need to split it up and
831 * examine the second part separately.
833 if (e
->s_lno
+ e
->num_lines
> tlno
) {
834 /* Move second half to a new record */
835 int len
= tlno
- e
->s_lno
;
836 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
837 n
->suspect
= e
->suspect
;
838 n
->lno
= e
->lno
+ len
;
839 n
->s_lno
= e
->s_lno
+ len
;
840 n
->num_lines
= e
->num_lines
- len
;
843 /* Push new record to diffp */
847 blame_origin_decref(e
->suspect
);
848 /* Pass blame for everything before the differing
849 * chunk to the parent */
850 e
->suspect
= blame_origin_incref(parent
);
857 * As we don't know how much of a common stretch after this
858 * diff will occur, the currently blamed parts are all that we
859 * can assign to the parent for now.
863 **dstq
= reverse_blame(samep
, **dstq
);
864 *dstq
= &samep
->next
;
867 * Prepend the split off portions: everything after e starts
868 * after the blameable portion.
870 e
= reverse_blame(diffp
, e
);
873 * Now retain records on the target while parts are different
878 while (e
&& e
->s_lno
< same
) {
879 struct blame_entry
*next
= e
->next
;
882 * If current record extends into sameness, need to split.
884 if (e
->s_lno
+ e
->num_lines
> same
) {
886 * Move second half to a new record to be
887 * processed by later chunks
889 int len
= same
- e
->s_lno
;
890 struct blame_entry
*n
= xcalloc(1, sizeof (struct blame_entry
));
891 n
->suspect
= blame_origin_incref(e
->suspect
);
892 n
->lno
= e
->lno
+ len
;
893 n
->s_lno
= e
->s_lno
+ len
;
894 n
->num_lines
= e
->num_lines
- len
;
897 /* Push new record to samep */
905 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
906 /* Move across elements that are in the unblamable portion */
908 *srcq
= &diffp
->next
;
911 struct blame_chunk_cb_data
{
912 struct blame_origin
*parent
;
914 struct blame_entry
**dstq
;
915 struct blame_entry
**srcq
;
918 /* diff chunks are from parent to target */
919 static int blame_chunk_cb(long start_a
, long count_a
,
920 long start_b
, long count_b
, void *data
)
922 struct blame_chunk_cb_data
*d
= data
;
923 if (start_a
- start_b
!= d
->offset
)
924 die("internal error in blame::blame_chunk_cb");
925 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
926 start_b
+ count_b
, d
->parent
);
927 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
932 * We are looking at the origin 'target' and aiming to pass blame
933 * for the lines it is suspected to its parent. Run diff to find
934 * which lines came from parent and pass blame for them.
936 static void pass_blame_to_parent(struct blame_scoreboard
*sb
,
937 struct blame_origin
*target
,
938 struct blame_origin
*parent
)
940 mmfile_t file_p
, file_o
;
941 struct blame_chunk_cb_data d
;
942 struct blame_entry
*newdest
= NULL
;
944 if (!target
->suspects
)
945 return; /* nothing remains for this target */
949 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
951 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
952 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
, &sb
->num_read_blob
);
955 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, sb
->xdl_opts
))
956 die("unable to generate diff (%s -> %s)",
957 oid_to_hex(&parent
->commit
->object
.oid
),
958 oid_to_hex(&target
->commit
->object
.oid
));
959 /* The rest are the same as the parent */
960 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, parent
);
962 queue_blames(sb
, parent
, newdest
);
968 * The lines in blame_entry after splitting blames many times can become
969 * very small and trivial, and at some point it becomes pointless to
970 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
971 * ordinary C program, and it is not worth to say it was copied from
972 * totally unrelated file in the parent.
974 * Compute how trivial the lines in the blame_entry are.
976 unsigned blame_entry_score(struct blame_scoreboard
*sb
, struct blame_entry
*e
)
985 cp
= blame_nth_line(sb
, e
->lno
);
986 ep
= blame_nth_line(sb
, e
->lno
+ e
->num_lines
);
988 unsigned ch
= *((unsigned char *)cp
);
998 * best_so_far[] and potential[] are both a split of an existing blame_entry
999 * that passes blame to the parent. Maintain best_so_far the best split so
1000 * far, by comparing potential and best_so_far and copying potential into
1001 * bst_so_far as needed.
1003 static void copy_split_if_better(struct blame_scoreboard
*sb
,
1004 struct blame_entry
*best_so_far
,
1005 struct blame_entry
*potential
)
1009 if (!potential
[1].suspect
)
1011 if (best_so_far
[1].suspect
) {
1012 if (blame_entry_score(sb
, &potential
[1]) <
1013 blame_entry_score(sb
, &best_so_far
[1]))
1017 for (i
= 0; i
< 3; i
++)
1018 blame_origin_incref(potential
[i
].suspect
);
1019 decref_split(best_so_far
);
1020 memcpy(best_so_far
, potential
, sizeof(struct blame_entry
[3]));
1024 * We are looking at a part of the final image represented by
1025 * ent (tlno and same are offset by ent->s_lno).
1026 * tlno is where we are looking at in the final image.
1027 * up to (but not including) same match preimage.
1028 * plno is where we are looking at in the preimage.
1030 * <-------------- final image ---------------------->
1033 * <---------preimage----->
1036 * All line numbers are 0-based.
1038 static void handle_split(struct blame_scoreboard
*sb
,
1039 struct blame_entry
*ent
,
1040 int tlno
, int plno
, int same
,
1041 struct blame_origin
*parent
,
1042 struct blame_entry
*split
)
1044 if (ent
->num_lines
<= tlno
)
1047 struct blame_entry potential
[3];
1050 split_overlap(potential
, ent
, tlno
, plno
, same
, parent
);
1051 copy_split_if_better(sb
, split
, potential
);
1052 decref_split(potential
);
1056 struct handle_split_cb_data
{
1057 struct blame_scoreboard
*sb
;
1058 struct blame_entry
*ent
;
1059 struct blame_origin
*parent
;
1060 struct blame_entry
*split
;
1065 static int handle_split_cb(long start_a
, long count_a
,
1066 long start_b
, long count_b
, void *data
)
1068 struct handle_split_cb_data
*d
= data
;
1069 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
1071 d
->plno
= start_a
+ count_a
;
1072 d
->tlno
= start_b
+ count_b
;
1077 * Find the lines from parent that are the same as ent so that
1078 * we can pass blames to it. file_p has the blob contents for
1081 static void find_copy_in_blob(struct blame_scoreboard
*sb
,
1082 struct blame_entry
*ent
,
1083 struct blame_origin
*parent
,
1084 struct blame_entry
*split
,
1089 struct handle_split_cb_data d
;
1091 memset(&d
, 0, sizeof(d
));
1092 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
1094 * Prepare mmfile that contains only the lines in ent.
1096 cp
= blame_nth_line(sb
, ent
->lno
);
1097 file_o
.ptr
= (char *) cp
;
1098 file_o
.size
= blame_nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
1101 * file_o is a part of final image we are annotating.
1102 * file_p partially may match that image.
1104 memset(split
, 0, sizeof(struct blame_entry
[3]));
1105 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, sb
->xdl_opts
))
1106 die("unable to generate diff (%s)",
1107 oid_to_hex(&parent
->commit
->object
.oid
));
1108 /* remainder, if any, all match the preimage */
1109 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
1112 /* Move all blame entries from list *source that have a score smaller
1113 * than score_min to the front of list *small.
1114 * Returns a pointer to the link pointing to the old head of the small list.
1117 static struct blame_entry
**filter_small(struct blame_scoreboard
*sb
,
1118 struct blame_entry
**small
,
1119 struct blame_entry
**source
,
1122 struct blame_entry
*p
= *source
;
1123 struct blame_entry
*oldsmall
= *small
;
1125 if (blame_entry_score(sb
, p
) <= score_min
) {
1141 * See if lines currently target is suspected for can be attributed to
1144 static void find_move_in_parent(struct blame_scoreboard
*sb
,
1145 struct blame_entry
***blamed
,
1146 struct blame_entry
**toosmall
,
1147 struct blame_origin
*target
,
1148 struct blame_origin
*parent
)
1150 struct blame_entry
*e
, split
[3];
1151 struct blame_entry
*unblamed
= target
->suspects
;
1152 struct blame_entry
*leftover
= NULL
;
1156 return; /* nothing remains for this target */
1158 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
, &sb
->num_read_blob
);
1162 /* At each iteration, unblamed has a NULL-terminated list of
1163 * entries that have not yet been tested for blame. leftover
1164 * contains the reversed list of entries that have been tested
1165 * without being assignable to the parent.
1168 struct blame_entry
**unblamedtail
= &unblamed
;
1169 struct blame_entry
*next
;
1170 for (e
= unblamed
; e
; e
= next
) {
1172 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
1173 if (split
[1].suspect
&&
1174 sb
->move_score
< blame_entry_score(sb
, &split
[1])) {
1175 split_blame(blamed
, &unblamedtail
, split
, e
);
1180 decref_split(split
);
1182 *unblamedtail
= NULL
;
1183 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->move_score
);
1185 target
->suspects
= reverse_blame(leftover
, NULL
);
1189 struct blame_entry
*ent
;
1190 struct blame_entry split
[3];
1194 * Count the number of entries the target is suspected for,
1195 * and prepare a list of entry and the best split.
1197 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
1200 struct blame_entry
*e
;
1202 struct blame_list
*blame_list
= NULL
;
1204 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
1207 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1208 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
1209 blame_list
[i
++].ent
= e
;
1211 *num_ents_p
= num_ents
;
1216 * For lines target is suspected for, see if we can find code movement
1217 * across file boundary from the parent commit. porigin is the path
1218 * in the parent we already tried.
1220 static void find_copy_in_parent(struct blame_scoreboard
*sb
,
1221 struct blame_entry
***blamed
,
1222 struct blame_entry
**toosmall
,
1223 struct blame_origin
*target
,
1224 struct commit
*parent
,
1225 struct blame_origin
*porigin
,
1228 struct diff_options diff_opts
;
1230 struct blame_list
*blame_list
;
1232 struct blame_entry
*unblamed
= target
->suspects
;
1233 struct blame_entry
*leftover
= NULL
;
1236 return; /* nothing remains for this target */
1238 diff_setup(&diff_opts
);
1239 diff_opts
.flags
.recursive
= 1;
1240 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1242 diff_setup_done(&diff_opts
);
1244 /* Try "find copies harder" on new path if requested;
1245 * we do not want to use diffcore_rename() actually to
1246 * match things up; find_copies_harder is set only to
1247 * force diff_tree_oid() to feed all filepairs to diff_queue,
1248 * and this code needs to be after diff_setup_done(), which
1249 * usually makes find-copies-harder imply copy detection.
1251 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
1252 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
1253 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
1254 diff_opts
.flags
.find_copies_harder
= 1;
1256 if (is_null_oid(&target
->commit
->object
.oid
))
1257 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
1259 diff_tree_oid(get_commit_tree_oid(parent
),
1260 get_commit_tree_oid(target
->commit
),
1263 if (!diff_opts
.flags
.find_copies_harder
)
1264 diffcore_std(&diff_opts
);
1267 struct blame_entry
**unblamedtail
= &unblamed
;
1268 blame_list
= setup_blame_list(unblamed
, &num_ents
);
1270 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1271 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1272 struct blame_origin
*norigin
;
1274 struct blame_entry potential
[3];
1276 if (!DIFF_FILE_VALID(p
->one
))
1277 continue; /* does not exist in parent */
1278 if (S_ISGITLINK(p
->one
->mode
))
1279 continue; /* ignore git links */
1280 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1281 /* find_move already dealt with this path */
1284 norigin
= get_origin(parent
, p
->one
->path
);
1285 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
1286 norigin
->mode
= p
->one
->mode
;
1287 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
, &sb
->num_read_blob
);
1291 for (j
= 0; j
< num_ents
; j
++) {
1292 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1293 norigin
, potential
, &file_p
);
1294 copy_split_if_better(sb
, blame_list
[j
].split
,
1296 decref_split(potential
);
1298 blame_origin_decref(norigin
);
1301 for (j
= 0; j
< num_ents
; j
++) {
1302 struct blame_entry
*split
= blame_list
[j
].split
;
1303 if (split
[1].suspect
&&
1304 sb
->copy_score
< blame_entry_score(sb
, &split
[1])) {
1305 split_blame(blamed
, &unblamedtail
, split
,
1308 blame_list
[j
].ent
->next
= leftover
;
1309 leftover
= blame_list
[j
].ent
;
1311 decref_split(split
);
1314 *unblamedtail
= NULL
;
1315 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->copy_score
);
1317 target
->suspects
= reverse_blame(leftover
, NULL
);
1318 diff_flush(&diff_opts
);
1319 clear_pathspec(&diff_opts
.pathspec
);
1323 * The blobs of origin and porigin exactly match, so everything
1324 * origin is suspected for can be blamed on the parent.
1326 static void pass_whole_blame(struct blame_scoreboard
*sb
,
1327 struct blame_origin
*origin
, struct blame_origin
*porigin
)
1329 struct blame_entry
*e
, *suspects
;
1331 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1332 /* Steal its file */
1333 porigin
->file
= origin
->file
;
1334 origin
->file
.ptr
= NULL
;
1336 suspects
= origin
->suspects
;
1337 origin
->suspects
= NULL
;
1338 for (e
= suspects
; e
; e
= e
->next
) {
1339 blame_origin_incref(porigin
);
1340 blame_origin_decref(e
->suspect
);
1341 e
->suspect
= porigin
;
1343 queue_blames(sb
, porigin
, suspects
);
1347 * We pass blame from the current commit to its parents. We keep saying
1348 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1349 * exonerate ourselves.
1351 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
,
1355 if (revs
->first_parent_only
&&
1357 commit
->parents
->next
) {
1358 free_commit_list(commit
->parents
->next
);
1359 commit
->parents
->next
= NULL
;
1361 return commit
->parents
;
1363 return lookup_decoration(&revs
->children
, &commit
->object
);
1366 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
, int reverse
)
1368 struct commit_list
*l
= first_scapegoat(revs
, commit
, reverse
);
1369 return commit_list_count(l
);
1372 /* Distribute collected unsorted blames to the respected sorted lists
1373 * in the various origins.
1375 static void distribute_blame(struct blame_scoreboard
*sb
, struct blame_entry
*blamed
)
1377 blamed
= llist_mergesort(blamed
, get_next_blame
, set_next_blame
,
1378 compare_blame_suspect
);
1381 struct blame_origin
*porigin
= blamed
->suspect
;
1382 struct blame_entry
*suspects
= NULL
;
1384 struct blame_entry
*next
= blamed
->next
;
1385 blamed
->next
= suspects
;
1388 } while (blamed
&& blamed
->suspect
== porigin
);
1389 suspects
= reverse_blame(suspects
, NULL
);
1390 queue_blames(sb
, porigin
, suspects
);
1396 static void pass_blame(struct blame_scoreboard
*sb
, struct blame_origin
*origin
, int opt
)
1398 struct rev_info
*revs
= sb
->revs
;
1399 int i
, pass
, num_sg
;
1400 struct commit
*commit
= origin
->commit
;
1401 struct commit_list
*sg
;
1402 struct blame_origin
*sg_buf
[MAXSG
];
1403 struct blame_origin
*porigin
, **sg_origin
= sg_buf
;
1404 struct blame_entry
*toosmall
= NULL
;
1405 struct blame_entry
*blames
, **blametail
= &blames
;
1407 num_sg
= num_scapegoats(revs
, commit
, sb
->reverse
);
1410 else if (num_sg
< ARRAY_SIZE(sg_buf
))
1411 memset(sg_buf
, 0, sizeof(sg_buf
));
1413 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
1416 * The first pass looks for unrenamed path to optimize for
1417 * common cases, then we look for renames in the second pass.
1419 for (pass
= 0; pass
< 2 - sb
->no_whole_file_rename
; pass
++) {
1420 struct blame_origin
*(*find
)(struct commit
*, struct blame_origin
*);
1421 find
= pass
? find_rename
: find_origin
;
1423 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1425 sg
= sg
->next
, i
++) {
1426 struct commit
*p
= sg
->item
;
1431 if (parse_commit(p
))
1433 porigin
= find(p
, origin
);
1436 if (!oidcmp(&porigin
->blob_oid
, &origin
->blob_oid
)) {
1437 pass_whole_blame(sb
, origin
, porigin
);
1438 blame_origin_decref(porigin
);
1441 for (j
= same
= 0; j
< i
; j
++)
1443 !oidcmp(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
1448 sg_origin
[i
] = porigin
;
1450 blame_origin_decref(porigin
);
1455 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1457 sg
= sg
->next
, i
++) {
1458 struct blame_origin
*porigin
= sg_origin
[i
];
1461 if (!origin
->previous
) {
1462 blame_origin_incref(porigin
);
1463 origin
->previous
= porigin
;
1465 pass_blame_to_parent(sb
, origin
, porigin
);
1466 if (!origin
->suspects
)
1471 * Optionally find moves in parents' files.
1473 if (opt
& PICKAXE_BLAME_MOVE
) {
1474 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->move_score
);
1475 if (origin
->suspects
) {
1476 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1478 sg
= sg
->next
, i
++) {
1479 struct blame_origin
*porigin
= sg_origin
[i
];
1482 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
1483 if (!origin
->suspects
)
1490 * Optionally find copies from parents' files.
1492 if (opt
& PICKAXE_BLAME_COPY
) {
1493 if (sb
->copy_score
> sb
->move_score
)
1494 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1495 else if (sb
->copy_score
< sb
->move_score
) {
1496 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
1498 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
1500 if (!origin
->suspects
)
1503 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
1505 sg
= sg
->next
, i
++) {
1506 struct blame_origin
*porigin
= sg_origin
[i
];
1507 find_copy_in_parent(sb
, &blametail
, &toosmall
,
1508 origin
, sg
->item
, porigin
, opt
);
1509 if (!origin
->suspects
)
1516 distribute_blame(sb
, blames
);
1518 * prepend toosmall to origin->suspects
1520 * There is no point in sorting: this ends up on a big
1521 * unsorted list in the caller anyway.
1524 struct blame_entry
**tail
= &toosmall
;
1526 tail
= &(*tail
)->next
;
1527 *tail
= origin
->suspects
;
1528 origin
->suspects
= toosmall
;
1530 for (i
= 0; i
< num_sg
; i
++) {
1532 drop_origin_blob(sg_origin
[i
]);
1533 blame_origin_decref(sg_origin
[i
]);
1536 drop_origin_blob(origin
);
1537 if (sg_buf
!= sg_origin
)
1542 * The main loop -- while we have blobs with lines whose true origin
1543 * is still unknown, pick one blob, and allow its lines to pass blames
1544 * to its parents. */
1545 void assign_blame(struct blame_scoreboard
*sb
, int opt
)
1547 struct rev_info
*revs
= sb
->revs
;
1548 struct commit
*commit
= prio_queue_get(&sb
->commits
);
1551 struct blame_entry
*ent
;
1552 struct blame_origin
*suspect
= commit
->util
;
1554 /* find one suspect to break down */
1555 while (suspect
&& !suspect
->suspects
)
1556 suspect
= suspect
->next
;
1559 commit
= prio_queue_get(&sb
->commits
);
1563 assert(commit
== suspect
->commit
);
1566 * We will use this suspect later in the loop,
1567 * so hold onto it in the meantime.
1569 blame_origin_incref(suspect
);
1570 parse_commit(commit
);
1572 (!(commit
->object
.flags
& UNINTERESTING
) &&
1573 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
1574 pass_blame(sb
, suspect
, opt
);
1576 commit
->object
.flags
|= UNINTERESTING
;
1577 if (commit
->object
.parsed
)
1578 mark_parents_uninteresting(commit
);
1580 /* treat root commit as boundary */
1581 if (!commit
->parents
&& !sb
->show_root
)
1582 commit
->object
.flags
|= UNINTERESTING
;
1584 /* Take responsibility for the remaining entries */
1585 ent
= suspect
->suspects
;
1587 suspect
->guilty
= 1;
1589 struct blame_entry
*next
= ent
->next
;
1590 if (sb
->found_guilty_entry
)
1591 sb
->found_guilty_entry(ent
, sb
->found_guilty_entry_data
);
1596 ent
->next
= sb
->ent
;
1597 sb
->ent
= suspect
->suspects
;
1598 suspect
->suspects
= NULL
;
1602 blame_origin_decref(suspect
);
1604 if (sb
->debug
) /* sanity */
1605 sanity_check_refcnt(sb
);
1609 static const char *get_next_line(const char *start
, const char *end
)
1611 const char *nl
= memchr(start
, '\n', end
- start
);
1612 return nl
? nl
+ 1 : end
;
1616 * To allow quick access to the contents of nth line in the
1617 * final image, prepare an index in the scoreboard.
1619 static int prepare_lines(struct blame_scoreboard
*sb
)
1621 const char *buf
= sb
->final_buf
;
1622 unsigned long len
= sb
->final_buf_size
;
1623 const char *end
= buf
+ len
;
1628 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1631 ALLOC_ARRAY(sb
->lineno
, num
+ 1);
1632 lineno
= sb
->lineno
;
1634 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
1635 *lineno
++ = p
- buf
;
1639 sb
->num_lines
= num
;
1640 return sb
->num_lines
;
1643 static struct commit
*find_single_final(struct rev_info
*revs
,
1644 const char **name_p
)
1647 struct commit
*found
= NULL
;
1648 const char *name
= NULL
;
1650 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1651 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1652 if (obj
->flags
& UNINTERESTING
)
1654 obj
= deref_tag(obj
, NULL
, 0);
1655 if (obj
->type
!= OBJ_COMMIT
)
1656 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1658 die("More than one commit to dig from %s and %s?",
1659 revs
->pending
.objects
[i
].name
, name
);
1660 found
= (struct commit
*)obj
;
1661 name
= revs
->pending
.objects
[i
].name
;
1664 *name_p
= xstrdup_or_null(name
);
1668 static struct commit
*dwim_reverse_initial(struct rev_info
*revs
,
1669 const char **name_p
)
1672 * DWIM "git blame --reverse ONE -- PATH" as
1673 * "git blame --reverse ONE..HEAD -- PATH" but only do so
1674 * when it makes sense.
1677 struct commit
*head_commit
;
1678 struct object_id head_oid
;
1680 if (revs
->pending
.nr
!= 1)
1683 /* Is that sole rev a committish? */
1684 obj
= revs
->pending
.objects
[0].item
;
1685 obj
= deref_tag(obj
, NULL
, 0);
1686 if (obj
->type
!= OBJ_COMMIT
)
1689 /* Do we have HEAD? */
1690 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
1692 head_commit
= lookup_commit_reference_gently(&head_oid
, 1);
1696 /* Turn "ONE" into "ONE..HEAD" then */
1697 obj
->flags
|= UNINTERESTING
;
1698 add_pending_object(revs
, &head_commit
->object
, "HEAD");
1701 *name_p
= revs
->pending
.objects
[0].name
;
1702 return (struct commit
*)obj
;
1705 static struct commit
*find_single_initial(struct rev_info
*revs
,
1706 const char **name_p
)
1709 struct commit
*found
= NULL
;
1710 const char *name
= NULL
;
1713 * There must be one and only one negative commit, and it must be
1716 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
1717 struct object
*obj
= revs
->pending
.objects
[i
].item
;
1718 if (!(obj
->flags
& UNINTERESTING
))
1720 obj
= deref_tag(obj
, NULL
, 0);
1721 if (obj
->type
!= OBJ_COMMIT
)
1722 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
1724 die("More than one commit to dig up from, %s and %s?",
1725 revs
->pending
.objects
[i
].name
, name
);
1726 found
= (struct commit
*) obj
;
1727 name
= revs
->pending
.objects
[i
].name
;
1731 found
= dwim_reverse_initial(revs
, &name
);
1733 die("No commit to dig up from?");
1736 *name_p
= xstrdup(name
);
1740 void init_scoreboard(struct blame_scoreboard
*sb
)
1742 memset(sb
, 0, sizeof(struct blame_scoreboard
));
1743 sb
->move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1744 sb
->copy_score
= BLAME_DEFAULT_COPY_SCORE
;
1747 void setup_scoreboard(struct blame_scoreboard
*sb
, const char *path
, struct blame_origin
**orig
)
1749 const char *final_commit_name
= NULL
;
1750 struct blame_origin
*o
;
1751 struct commit
*final_commit
= NULL
;
1752 enum object_type type
;
1754 if (sb
->reverse
&& sb
->contents_from
)
1755 die(_("--contents and --reverse do not blend well."));
1758 sb
->final
= find_single_final(sb
->revs
, &final_commit_name
);
1759 sb
->commits
.compare
= compare_commits_by_commit_date
;
1761 sb
->final
= find_single_initial(sb
->revs
, &final_commit_name
);
1762 sb
->commits
.compare
= compare_commits_by_reverse_commit_date
;
1765 if (sb
->final
&& sb
->contents_from
)
1766 die(_("cannot use --contents with final commit object name"));
1768 if (sb
->reverse
&& sb
->revs
->first_parent_only
)
1769 sb
->revs
->children
.name
= NULL
;
1773 * "--not A B -- path" without anything positive;
1774 * do not default to HEAD, but use the working tree
1778 sb
->final
= fake_working_tree_commit(&sb
->revs
->diffopt
,
1779 path
, sb
->contents_from
);
1780 add_pending_object(sb
->revs
, &(sb
->final
->object
), ":");
1783 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1784 final_commit
= find_single_final(sb
->revs
, NULL
);
1786 die(_("--reverse and --first-parent together require specified latest commit"));
1790 * If we have bottom, this will mark the ancestors of the
1791 * bottom commits we would reach while traversing as
1794 if (prepare_revision_walk(sb
->revs
))
1795 die(_("revision walk setup failed"));
1797 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
1798 struct commit
*c
= final_commit
;
1800 sb
->revs
->children
.name
= "children";
1801 while (c
->parents
&&
1802 oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
)) {
1803 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1806 if (add_decoration(&sb
->revs
->children
,
1807 &c
->parents
->item
->object
, l
))
1808 BUG("not unique item in first-parent chain");
1809 c
= c
->parents
->item
;
1812 if (oidcmp(&c
->object
.oid
, &sb
->final
->object
.oid
))
1813 die(_("--reverse --first-parent together require range along first-parent chain"));
1816 if (is_null_oid(&sb
->final
->object
.oid
)) {
1817 o
= sb
->final
->util
;
1818 sb
->final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
1819 sb
->final_buf_size
= o
->file
.size
;
1822 o
= get_origin(sb
->final
, path
);
1823 if (fill_blob_sha1_and_mode(o
))
1824 die(_("no such path %s in %s"), path
, final_commit_name
);
1826 if (sb
->revs
->diffopt
.flags
.allow_textconv
&&
1827 textconv_object(path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
->final_buf
,
1828 &sb
->final_buf_size
))
1831 sb
->final_buf
= read_object_file(&o
->blob_oid
, &type
,
1832 &sb
->final_buf_size
);
1835 die(_("cannot read blob %s for path %s"),
1836 oid_to_hex(&o
->blob_oid
),
1839 sb
->num_read_blob
++;
1845 free((char *)final_commit_name
);
1850 struct blame_entry
*blame_entry_prepend(struct blame_entry
*head
,
1851 long start
, long end
,
1852 struct blame_origin
*o
)
1854 struct blame_entry
*new_head
= xcalloc(1, sizeof(struct blame_entry
));
1855 new_head
->lno
= start
;
1856 new_head
->num_lines
= end
- start
;
1857 new_head
->suspect
= o
;
1858 new_head
->s_lno
= start
;
1859 new_head
->next
= head
;
1860 blame_origin_incref(o
);