3 #include "object-store.h"
4 #include "cache-tree.h"
11 #include "commit-slab.h"
13 define_commit_slab(blame_suspects
, struct blame_origin
*);
14 static struct blame_suspects blame_suspects
;
16 struct blame_origin
*get_blame_suspects(struct commit
*commit
)
18 struct blame_origin
**result
;
20 result
= blame_suspects_peek(&blame_suspects
, commit
);
22 return result
? *result
: NULL
;
25 static void set_blame_suspects(struct commit
*commit
, struct blame_origin
*origin
)
27 *blame_suspects_at(&blame_suspects
, commit
) = origin
;
30 void blame_origin_decref(struct blame_origin
*o
)
32 if (o
&& --o
->refcnt
<= 0) {
33 struct blame_origin
*p
, *l
= NULL
;
35 blame_origin_decref(o
->previous
);
37 /* Should be present exactly once in commit chain */
38 for (p
= get_blame_suspects(o
->commit
); p
; l
= p
, p
= p
->next
) {
43 set_blame_suspects(o
->commit
, p
->next
);
48 die("internal error in blame_origin_decref");
53 * Given a commit and a path in it, create a new origin structure.
54 * The callers that add blame to the scoreboard should use
55 * get_origin() to obtain shared, refcounted copy instead of calling
56 * this function directly.
58 static struct blame_origin
*make_origin(struct commit
*commit
, const char *path
)
60 struct blame_origin
*o
;
61 FLEX_ALLOC_STR(o
, path
, path
);
64 o
->next
= get_blame_suspects(commit
);
65 set_blame_suspects(commit
, o
);
70 * Locate an existing origin or create a new one.
71 * This moves the origin to front position in the commit util list.
73 static struct blame_origin
*get_origin(struct commit
*commit
, const char *path
)
75 struct blame_origin
*o
, *l
;
77 for (o
= get_blame_suspects(commit
), l
= NULL
; o
; l
= o
, o
= o
->next
) {
78 if (!strcmp(o
->path
, path
)) {
82 o
->next
= get_blame_suspects(commit
);
83 set_blame_suspects(commit
, o
);
85 return blame_origin_incref(o
);
88 return make_origin(commit
, path
);
93 static void verify_working_tree_path(struct repository
*r
,
94 struct commit
*work_tree
, const char *path
)
96 struct commit_list
*parents
;
99 for (parents
= work_tree
->parents
; parents
; parents
= parents
->next
) {
100 const struct object_id
*commit_oid
= &parents
->item
->object
.oid
;
101 struct object_id blob_oid
;
104 if (!get_tree_entry(r
, commit_oid
, path
, &blob_oid
, &mode
) &&
105 oid_object_info(r
, &blob_oid
, NULL
) == OBJ_BLOB
)
109 pos
= index_name_pos(r
->index
, path
, strlen(path
));
111 ; /* path is in the index */
112 else if (-1 - pos
< r
->index
->cache_nr
&&
113 !strcmp(r
->index
->cache
[-1 - pos
]->name
, path
))
114 ; /* path is in the index, unmerged */
116 die("no such path '%s' in HEAD", path
);
119 static struct commit_list
**append_parent(struct repository
*r
,
120 struct commit_list
**tail
,
121 const struct object_id
*oid
)
123 struct commit
*parent
;
125 parent
= lookup_commit_reference(r
, oid
);
127 die("no such commit %s", oid_to_hex(oid
));
128 return &commit_list_insert(parent
, tail
)->next
;
131 static void append_merge_parents(struct repository
*r
,
132 struct commit_list
**tail
)
135 struct strbuf line
= STRBUF_INIT
;
137 merge_head
= open(git_path_merge_head(r
), O_RDONLY
);
138 if (merge_head
< 0) {
141 die("cannot open '%s' for reading",
142 git_path_merge_head(r
));
145 while (!strbuf_getwholeline_fd(&line
, merge_head
, '\n')) {
146 struct object_id oid
;
147 if (line
.len
< GIT_SHA1_HEXSZ
|| get_oid_hex(line
.buf
, &oid
))
148 die("unknown line in '%s': %s",
149 git_path_merge_head(r
), line
.buf
);
150 tail
= append_parent(r
, tail
, &oid
);
153 strbuf_release(&line
);
157 * This isn't as simple as passing sb->buf and sb->len, because we
158 * want to transfer ownership of the buffer to the commit (so we
161 static void set_commit_buffer_from_strbuf(struct repository
*r
,
166 void *buf
= strbuf_detach(sb
, &len
);
167 set_commit_buffer(r
, c
, buf
, len
);
171 * Prepare a dummy commit that represents the work tree (or staged) item.
172 * Note that annotating work tree item never works in the reverse.
174 static struct commit
*fake_working_tree_commit(struct repository
*r
,
175 struct diff_options
*opt
,
177 const char *contents_from
)
179 struct commit
*commit
;
180 struct blame_origin
*origin
;
181 struct commit_list
**parent_tail
, *parent
;
182 struct object_id head_oid
;
183 struct strbuf buf
= STRBUF_INIT
;
187 struct cache_entry
*ce
;
189 struct strbuf msg
= STRBUF_INIT
;
193 commit
= alloc_commit_node(r
);
194 commit
->object
.parsed
= 1;
196 parent_tail
= &commit
->parents
;
198 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
199 die("no such ref: HEAD");
201 parent_tail
= append_parent(r
, parent_tail
, &head_oid
);
202 append_merge_parents(r
, parent_tail
);
203 verify_working_tree_path(r
, commit
, path
);
205 origin
= make_origin(commit
, path
);
207 ident
= fmt_ident("Not Committed Yet", "not.committed.yet",
208 WANT_BLANK_IDENT
, NULL
, 0);
209 strbuf_addstr(&msg
, "tree 0000000000000000000000000000000000000000\n");
210 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
211 strbuf_addf(&msg
, "parent %s\n",
212 oid_to_hex(&parent
->item
->object
.oid
));
216 "Version of %s from %s\n",
218 (!contents_from
? path
:
219 (!strcmp(contents_from
, "-") ? "standard input" : contents_from
)));
220 set_commit_buffer_from_strbuf(r
, commit
, &msg
);
222 if (!contents_from
|| strcmp("-", contents_from
)) {
224 const char *read_from
;
226 unsigned long buf_len
;
229 if (stat(contents_from
, &st
) < 0)
230 die_errno("Cannot stat '%s'", contents_from
);
231 read_from
= contents_from
;
234 if (lstat(path
, &st
) < 0)
235 die_errno("Cannot lstat '%s'", path
);
238 mode
= canon_mode(st
.st_mode
);
240 switch (st
.st_mode
& S_IFMT
) {
242 if (opt
->flags
.allow_textconv
&&
243 textconv_object(r
, read_from
, mode
, &null_oid
, 0, &buf_ptr
, &buf_len
))
244 strbuf_attach(&buf
, buf_ptr
, buf_len
, buf_len
+ 1);
245 else if (strbuf_read_file(&buf
, read_from
, st
.st_size
) != st
.st_size
)
246 die_errno("cannot open or read '%s'", read_from
);
249 if (strbuf_readlink(&buf
, read_from
, st
.st_size
) < 0)
250 die_errno("cannot readlink '%s'", read_from
);
253 die("unsupported file type %s", read_from
);
257 /* Reading from stdin */
259 if (strbuf_read(&buf
, 0, 0) < 0)
260 die_errno("failed to read from stdin");
262 convert_to_git(r
->index
, path
, buf
.buf
, buf
.len
, &buf
, 0);
263 origin
->file
.ptr
= buf
.buf
;
264 origin
->file
.size
= buf
.len
;
265 pretend_object_file(buf
.buf
, buf
.len
, OBJ_BLOB
, &origin
->blob_oid
);
268 * Read the current index, replace the path entry with
269 * origin->blob_sha1 without mucking with its mode or type
270 * bits; we are not going to write this index out -- we just
271 * want to run "diff-index --cached".
273 discard_index(r
->index
);
278 int pos
= index_name_pos(r
->index
, path
, len
);
280 mode
= r
->index
->cache
[pos
]->ce_mode
;
282 /* Let's not bother reading from HEAD tree */
283 mode
= S_IFREG
| 0644;
285 ce
= make_empty_cache_entry(r
->index
, len
);
286 oidcpy(&ce
->oid
, &origin
->blob_oid
);
287 memcpy(ce
->name
, path
, len
);
288 ce
->ce_flags
= create_ce_flags(0);
289 ce
->ce_namelen
= len
;
290 ce
->ce_mode
= create_ce_mode(mode
);
291 add_index_entry(r
->index
, ce
,
292 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
294 cache_tree_invalidate_path(r
->index
, path
);
301 static int diff_hunks(mmfile_t
*file_a
, mmfile_t
*file_b
,
302 xdl_emit_hunk_consume_func_t hunk_func
, void *cb_data
, int xdl_opts
)
305 xdemitconf_t xecfg
= {0};
306 xdemitcb_t ecb
= {NULL
};
308 xpp
.flags
= xdl_opts
;
309 xecfg
.hunk_func
= hunk_func
;
311 return xdi_diff(file_a
, file_b
, &xpp
, &xecfg
, &ecb
);
314 static const char *get_next_line(const char *start
, const char *end
)
316 const char *nl
= memchr(start
, '\n', end
- start
);
318 return nl
? nl
+ 1 : end
;
321 static int find_line_starts(int **line_starts
, const char *buf
,
324 const char *end
= buf
+ len
;
329 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
332 ALLOC_ARRAY(*line_starts
, num
+ 1);
333 lineno
= *line_starts
;
335 for (p
= buf
; p
< end
; p
= get_next_line(p
, end
))
343 struct fingerprint_entry
;
345 /* A fingerprint is intended to loosely represent a string, such that two
346 * fingerprints can be quickly compared to give an indication of the similarity
347 * of the strings that they represent.
349 * A fingerprint is represented as a multiset of the lower-cased byte pairs in
350 * the string that it represents. Whitespace is added at each end of the
351 * string. Whitespace pairs are ignored. Whitespace is converted to '\0'.
352 * For example, the string "Darth Radar" will be converted to the following
354 * {"\0d", "da", "da", "ar", "ar", "rt", "th", "h\0", "\0r", "ra", "ad", "r\0"}
356 * The similarity between two fingerprints is the size of the intersection of
357 * their multisets, including repeated elements. See fingerprint_similarity for
360 * For ease of implementation, the fingerprint is implemented as a map
361 * of byte pairs to the count of that byte pair in the string, instead of
362 * allowing repeated elements in a set.
366 /* As we know the maximum number of entries in advance, it's
367 * convenient to store the entries in a single array instead of having
368 * the hashmap manage the memory.
370 struct fingerprint_entry
*entries
;
373 /* A byte pair in a fingerprint. Stores the number of times the byte pair
374 * occurs in the string that the fingerprint represents.
376 struct fingerprint_entry
{
377 /* The hashmap entry - the hash represents the byte pair in its
378 * entirety so we don't need to store the byte pair separately.
380 struct hashmap_entry entry
;
381 /* The number of times the byte pair occurs in the string that the
382 * fingerprint represents.
387 /* See `struct fingerprint` for an explanation of what a fingerprint is.
388 * \param result the fingerprint of the string is stored here. This must be
389 * freed later using free_fingerprint.
390 * \param line_begin the start of the string
391 * \param line_end the end of the string
393 static void get_fingerprint(struct fingerprint
*result
,
394 const char *line_begin
,
395 const char *line_end
)
397 unsigned int hash
, c0
= 0, c1
;
399 int max_map_entry_count
= 1 + line_end
- line_begin
;
400 struct fingerprint_entry
*entry
= xcalloc(max_map_entry_count
,
401 sizeof(struct fingerprint_entry
));
402 struct fingerprint_entry
*found_entry
;
404 hashmap_init(&result
->map
, NULL
, NULL
, max_map_entry_count
);
405 result
->entries
= entry
;
406 for (p
= line_begin
; p
<= line_end
; ++p
, c0
= c1
) {
407 /* Always terminate the string with whitespace.
408 * Normalise whitespace to 0, and normalise letters to
409 * lower case. This won't work for multibyte characters but at
410 * worst will match some unrelated characters.
412 if ((p
== line_end
) || isspace(*p
))
416 hash
= c0
| (c1
<< 8);
417 /* Ignore whitespace pairs */
420 hashmap_entry_init(&entry
->entry
, hash
);
422 found_entry
= hashmap_get_entry(&result
->map
, entry
, NULL
,
423 struct fingerprint_entry
, entry
);
425 found_entry
->count
+= 1;
428 hashmap_add(&result
->map
, &entry
->entry
);
434 static void free_fingerprint(struct fingerprint
*f
)
436 hashmap_free(&f
->map
);
440 /* Calculates the similarity between two fingerprints as the size of the
441 * intersection of their multisets, including repeated elements. See
442 * `struct fingerprint` for an explanation of the fingerprint representation.
443 * The similarity between "cat mat" and "father rather" is 2 because "at" is
444 * present twice in both strings while the similarity between "tim" and "mit"
447 static int fingerprint_similarity(struct fingerprint
*a
, struct fingerprint
*b
)
449 int intersection
= 0;
450 struct hashmap_iter iter
;
451 const struct fingerprint_entry
*entry_a
, *entry_b
;
453 hashmap_for_each_entry(&b
->map
, &iter
, entry_b
,
454 const struct fingerprint_entry
,
455 entry
/* member name */) {
456 entry_a
= hashmap_get_entry(&a
->map
, entry_b
, NULL
,
457 struct fingerprint_entry
, entry
);
459 intersection
+= entry_a
->count
< entry_b
->count
?
460 entry_a
->count
: entry_b
->count
;
466 /* Subtracts byte-pair elements in B from A, modifying A in place.
468 static void fingerprint_subtract(struct fingerprint
*a
, struct fingerprint
*b
)
470 struct hashmap_iter iter
;
471 struct fingerprint_entry
*entry_a
;
472 const struct fingerprint_entry
*entry_b
;
474 hashmap_iter_init(&b
->map
, &iter
);
476 hashmap_for_each_entry(&b
->map
, &iter
, entry_b
,
477 const struct fingerprint_entry
,
478 entry
/* member name */) {
479 entry_a
= hashmap_get_entry(&a
->map
, entry_b
, NULL
,
480 struct fingerprint_entry
, entry
);
482 if (entry_a
->count
<= entry_b
->count
)
483 hashmap_remove(&a
->map
, &entry_b
->entry
, NULL
);
485 entry_a
->count
-= entry_b
->count
;
490 /* Calculate fingerprints for a series of lines.
491 * Puts the fingerprints in the fingerprints array, which must have been
492 * preallocated to allow storing line_count elements.
494 static void get_line_fingerprints(struct fingerprint
*fingerprints
,
495 const char *content
, const int *line_starts
,
496 long first_line
, long line_count
)
499 const char *linestart
, *lineend
;
501 line_starts
+= first_line
;
502 for (i
= 0; i
< line_count
; ++i
) {
503 linestart
= content
+ line_starts
[i
];
504 lineend
= content
+ line_starts
[i
+ 1];
505 get_fingerprint(fingerprints
+ i
, linestart
, lineend
);
509 static void free_line_fingerprints(struct fingerprint
*fingerprints
,
514 for (i
= 0; i
< nr_fingerprints
; i
++)
515 free_fingerprint(&fingerprints
[i
]);
518 /* This contains the data necessary to linearly map a line number in one half
519 * of a diff chunk to the line in the other half of the diff chunk that is
520 * closest in terms of its position as a fraction of the length of the chunk.
522 struct line_number_mapping
{
523 int destination_start
, destination_length
,
524 source_start
, source_length
;
527 /* Given a line number in one range, offset and scale it to map it onto the
529 * Essentially this mapping is a simple linear equation but the calculation is
530 * more complicated to allow performing it with integer operations.
531 * Another complication is that if a line could map onto many lines in the
532 * destination range then we want to choose the line at the center of those
534 * Example: if the chunk is 2 lines long in A and 10 lines long in B then the
535 * first 5 lines in B will map onto the first line in the A chunk, while the
536 * last 5 lines will all map onto the second line in the A chunk.
537 * Example: if the chunk is 10 lines long in A and 2 lines long in B then line
538 * 0 in B will map onto line 2 in A, and line 1 in B will map onto line 7 in A.
540 static int map_line_number(int line_number
,
541 const struct line_number_mapping
*mapping
)
543 return ((line_number
- mapping
->source_start
) * 2 + 1) *
544 mapping
->destination_length
/
545 (mapping
->source_length
* 2) +
546 mapping
->destination_start
;
549 /* Get a pointer to the element storing the similarity between a line in A
552 * The similarities are stored in a 2-dimensional array. Each "row" in the
553 * array contains the similarities for a line in B. The similarities stored in
554 * a row are the similarities between the line in B and the nearby lines in A.
555 * To keep the length of each row the same, it is padded out with values of -1
556 * where the search range extends beyond the lines in A.
557 * For example, if max_search_distance_a is 2 and the two sides of a diff chunk
564 * Then the similarity array will contain:
565 * [-1, -1, am, bm, cm,
566 * -1, an, bn, cn, dn,
567 * ao, bo, co, do, eo,
568 * bp, cp, dp, ep, -1,
569 * cq, dq, eq, -1, -1]
570 * Where similarities are denoted either by -1 for invalid, or the
571 * concatenation of the two lines in the diff being compared.
573 * \param similarities array of similarities between lines in A and B
574 * \param line_a the index of the line in A, in the same frame of reference as
576 * \param local_line_b the index of the line in B, relative to the first line
577 * in B that similarities represents.
578 * \param closest_line_a the index of the line in A that is deemed to be
579 * closest to local_line_b. This must be in the same
580 * frame of reference as line_a. This value defines
581 * where similarities is centered for the line in B.
582 * \param max_search_distance_a maximum distance in lines from the closest line
583 * in A for other lines in A for which
584 * similarities may be calculated.
586 static int *get_similarity(int *similarities
,
587 int line_a
, int local_line_b
,
588 int closest_line_a
, int max_search_distance_a
)
590 assert(abs(line_a
- closest_line_a
) <=
591 max_search_distance_a
);
592 return similarities
+ line_a
- closest_line_a
+
593 max_search_distance_a
+
594 local_line_b
* (max_search_distance_a
* 2 + 1);
597 #define CERTAIN_NOTHING_MATCHES -2
598 #define CERTAINTY_NOT_CALCULATED -1
600 /* Given a line in B, first calculate its similarities with nearby lines in A
601 * if not already calculated, then identify the most similar and second most
602 * similar lines. The "certainty" is calculated based on those two
605 * \param start_a the index of the first line of the chunk in A
606 * \param length_a the length in lines of the chunk in A
607 * \param local_line_b the index of the line in B, relative to the first line
609 * \param fingerprints_a array of fingerprints for the chunk in A
610 * \param fingerprints_b array of fingerprints for the chunk in B
611 * \param similarities 2-dimensional array of similarities between lines in A
612 * and B. See get_similarity() for more details.
613 * \param certainties array of values indicating how strongly a line in B is
614 * matched with some line in A.
615 * \param second_best_result array of absolute indices in A for the second
616 * closest match of a line in B.
617 * \param result array of absolute indices in A for the closest match of a line
619 * \param max_search_distance_a maximum distance in lines from the closest line
620 * in A for other lines in A for which
621 * similarities may be calculated.
622 * \param map_line_number_in_b_to_a parameter to map_line_number().
624 static void find_best_line_matches(
629 struct fingerprint
*fingerprints_a
,
630 struct fingerprint
*fingerprints_b
,
633 int *second_best_result
,
635 const int max_search_distance_a
,
636 const struct line_number_mapping
*map_line_number_in_b_to_a
)
639 int i
, search_start
, search_end
, closest_local_line_a
, *similarity
,
640 best_similarity
= 0, second_best_similarity
= 0,
641 best_similarity_index
= 0, second_best_similarity_index
= 0;
643 /* certainty has already been calculated so no need to redo the work */
644 if (certainties
[local_line_b
] != CERTAINTY_NOT_CALCULATED
)
647 closest_local_line_a
= map_line_number(
648 local_line_b
+ start_b
, map_line_number_in_b_to_a
) - start_a
;
650 search_start
= closest_local_line_a
- max_search_distance_a
;
651 if (search_start
< 0)
654 search_end
= closest_local_line_a
+ max_search_distance_a
+ 1;
655 if (search_end
> length_a
)
656 search_end
= length_a
;
658 for (i
= search_start
; i
< search_end
; ++i
) {
659 similarity
= get_similarity(similarities
,
661 closest_local_line_a
,
662 max_search_distance_a
);
663 if (*similarity
== -1) {
664 /* This value will never exceed 10 but assert just in
667 assert(abs(i
- closest_local_line_a
) < 1000);
668 /* scale the similarity by (1000 - distance from
669 * closest line) to act as a tie break between lines
670 * that otherwise are equally similar.
672 *similarity
= fingerprint_similarity(
673 fingerprints_b
+ local_line_b
,
674 fingerprints_a
+ i
) *
675 (1000 - abs(i
- closest_local_line_a
));
677 if (*similarity
> best_similarity
) {
678 second_best_similarity
= best_similarity
;
679 second_best_similarity_index
= best_similarity_index
;
680 best_similarity
= *similarity
;
681 best_similarity_index
= i
;
682 } else if (*similarity
> second_best_similarity
) {
683 second_best_similarity
= *similarity
;
684 second_best_similarity_index
= i
;
688 if (best_similarity
== 0) {
689 /* this line definitely doesn't match with anything. Mark it
690 * with this special value so it doesn't get invalidated and
691 * won't be recalculated.
693 certainties
[local_line_b
] = CERTAIN_NOTHING_MATCHES
;
694 result
[local_line_b
] = -1;
696 /* Calculate the certainty with which this line matches.
697 * If the line matches well with two lines then that reduces
698 * the certainty. However we still want to prioritise matching
699 * a line that matches very well with two lines over matching a
700 * line that matches poorly with one line, hence doubling
702 * This means that if we have
703 * line X that matches only one line with a score of 3,
704 * line Y that matches two lines equally with a score of 5,
705 * and line Z that matches only one line with a score or 2,
706 * then the lines in order of certainty are X, Y, Z.
708 certainties
[local_line_b
] = best_similarity
* 2 -
709 second_best_similarity
;
711 /* We keep both the best and second best results to allow us to
712 * check at a later stage of the matching process whether the
713 * result needs to be invalidated.
715 result
[local_line_b
] = start_a
+ best_similarity_index
;
716 second_best_result
[local_line_b
] =
717 start_a
+ second_best_similarity_index
;
722 * This finds the line that we can match with the most confidence, and
723 * uses it as a partition. It then calls itself on the lines on either side of
724 * that partition. In this way we avoid lines appearing out of order, and
725 * retain a sensible line ordering.
726 * \param start_a index of the first line in A with which lines in B may be
728 * \param start_b index of the first line in B for which matching should be
730 * \param length_a number of lines in A with which lines in B may be compared.
731 * \param length_b number of lines in B for which matching should be done.
732 * \param fingerprints_a mutable array of fingerprints in A. The first element
733 * corresponds to the line at start_a.
734 * \param fingerprints_b array of fingerprints in B. The first element
735 * corresponds to the line at start_b.
736 * \param similarities 2-dimensional array of similarities between lines in A
737 * and B. See get_similarity() for more details.
738 * \param certainties array of values indicating how strongly a line in B is
739 * matched with some line in A.
740 * \param second_best_result array of absolute indices in A for the second
741 * closest match of a line in B.
742 * \param result array of absolute indices in A for the closest match of a line
744 * \param max_search_distance_a maximum distance in lines from the closest line
745 * in A for other lines in A for which
746 * similarities may be calculated.
747 * \param max_search_distance_b an upper bound on the greatest possible
748 * distance between lines in B such that they will
749 * both be compared with the same line in A
750 * according to max_search_distance_a.
751 * \param map_line_number_in_b_to_a parameter to map_line_number().
753 static void fuzzy_find_matching_lines_recurse(
754 int start_a
, int start_b
,
755 int length_a
, int length_b
,
756 struct fingerprint
*fingerprints_a
,
757 struct fingerprint
*fingerprints_b
,
760 int *second_best_result
,
762 int max_search_distance_a
,
763 int max_search_distance_b
,
764 const struct line_number_mapping
*map_line_number_in_b_to_a
)
766 int i
, invalidate_min
, invalidate_max
, offset_b
,
767 second_half_start_a
, second_half_start_b
,
768 second_half_length_a
, second_half_length_b
,
769 most_certain_line_a
, most_certain_local_line_b
= -1,
770 most_certain_line_certainty
= -1,
771 closest_local_line_a
;
773 for (i
= 0; i
< length_b
; ++i
) {
774 find_best_line_matches(start_a
,
784 max_search_distance_a
,
785 map_line_number_in_b_to_a
);
787 if (certainties
[i
] > most_certain_line_certainty
) {
788 most_certain_line_certainty
= certainties
[i
];
789 most_certain_local_line_b
= i
;
794 if (most_certain_local_line_b
== -1)
797 most_certain_line_a
= result
[most_certain_local_line_b
];
800 * Subtract the most certain line's fingerprint in B from the matched
801 * fingerprint in A. This means that other lines in B can't also match
802 * the same parts of the line in A.
804 fingerprint_subtract(fingerprints_a
+ most_certain_line_a
- start_a
,
805 fingerprints_b
+ most_certain_local_line_b
);
807 /* Invalidate results that may be affected by the choice of most
810 invalidate_min
= most_certain_local_line_b
- max_search_distance_b
;
811 invalidate_max
= most_certain_local_line_b
+ max_search_distance_b
+ 1;
812 if (invalidate_min
< 0)
814 if (invalidate_max
> length_b
)
815 invalidate_max
= length_b
;
817 /* As the fingerprint in A has changed, discard previously calculated
818 * similarity values with that fingerprint.
820 for (i
= invalidate_min
; i
< invalidate_max
; ++i
) {
821 closest_local_line_a
= map_line_number(
822 i
+ start_b
, map_line_number_in_b_to_a
) - start_a
;
824 /* Check that the lines in A and B are close enough that there
825 * is a similarity value for them.
827 if (abs(most_certain_line_a
- start_a
- closest_local_line_a
) >
828 max_search_distance_a
) {
832 *get_similarity(similarities
, most_certain_line_a
- start_a
,
833 i
, closest_local_line_a
,
834 max_search_distance_a
) = -1;
837 /* More invalidating of results that may be affected by the choice of
839 * Discard the matches for lines in B that are currently matched with a
840 * line in A such that their ordering contradicts the ordering imposed
841 * by the choice of most certain line.
843 for (i
= most_certain_local_line_b
- 1; i
>= invalidate_min
; --i
) {
844 /* In this loop we discard results for lines in B that are
845 * before most-certain-line-B but are matched with a line in A
846 * that is after most-certain-line-A.
848 if (certainties
[i
] >= 0 &&
849 (result
[i
] >= most_certain_line_a
||
850 second_best_result
[i
] >= most_certain_line_a
)) {
851 certainties
[i
] = CERTAINTY_NOT_CALCULATED
;
854 for (i
= most_certain_local_line_b
+ 1; i
< invalidate_max
; ++i
) {
855 /* In this loop we discard results for lines in B that are
856 * after most-certain-line-B but are matched with a line in A
857 * that is before most-certain-line-A.
859 if (certainties
[i
] >= 0 &&
860 (result
[i
] <= most_certain_line_a
||
861 second_best_result
[i
] <= most_certain_line_a
)) {
862 certainties
[i
] = CERTAINTY_NOT_CALCULATED
;
866 /* Repeat the matching process for lines before the most certain line.
868 if (most_certain_local_line_b
> 0) {
869 fuzzy_find_matching_lines_recurse(
871 most_certain_line_a
+ 1 - start_a
,
872 most_certain_local_line_b
,
873 fingerprints_a
, fingerprints_b
, similarities
,
874 certainties
, second_best_result
, result
,
875 max_search_distance_a
,
876 max_search_distance_b
,
877 map_line_number_in_b_to_a
);
879 /* Repeat the matching process for lines after the most certain line.
881 if (most_certain_local_line_b
+ 1 < length_b
) {
882 second_half_start_a
= most_certain_line_a
;
883 offset_b
= most_certain_local_line_b
+ 1;
884 second_half_start_b
= start_b
+ offset_b
;
885 second_half_length_a
=
886 length_a
+ start_a
- second_half_start_a
;
887 second_half_length_b
=
888 length_b
+ start_b
- second_half_start_b
;
889 fuzzy_find_matching_lines_recurse(
890 second_half_start_a
, second_half_start_b
,
891 second_half_length_a
, second_half_length_b
,
892 fingerprints_a
+ second_half_start_a
- start_a
,
893 fingerprints_b
+ offset_b
,
895 offset_b
* (max_search_distance_a
* 2 + 1),
896 certainties
+ offset_b
,
897 second_best_result
+ offset_b
, result
+ offset_b
,
898 max_search_distance_a
,
899 max_search_distance_b
,
900 map_line_number_in_b_to_a
);
904 /* Find the lines in the parent line range that most closely match the lines in
905 * the target line range. This is accomplished by matching fingerprints in each
906 * blame_origin, and choosing the best matches that preserve the line ordering.
907 * See struct fingerprint for details of fingerprint matching, and
908 * fuzzy_find_matching_lines_recurse for details of preserving line ordering.
910 * The performance is believed to be O(n log n) in the typical case and O(n^2)
911 * in a pathological case, where n is the number of lines in the target range.
913 static int *fuzzy_find_matching_lines(struct blame_origin
*parent
,
914 struct blame_origin
*target
,
915 int tlno
, int parent_slno
, int same
,
918 /* We use the terminology "A" for the left hand side of the diff AKA
919 * parent, and "B" for the right hand side of the diff AKA target. */
920 int start_a
= parent_slno
;
921 int length_a
= parent_len
;
923 int length_b
= same
- tlno
;
925 struct line_number_mapping map_line_number_in_b_to_a
= {
926 start_a
, length_a
, start_b
, length_b
929 struct fingerprint
*fingerprints_a
= parent
->fingerprints
;
930 struct fingerprint
*fingerprints_b
= target
->fingerprints
;
932 int i
, *result
, *second_best_result
,
933 *certainties
, *similarities
, similarity_count
;
936 * max_search_distance_a means that given a line in B, compare it to
937 * the line in A that is closest to its position, and the lines in A
938 * that are no greater than max_search_distance_a lines away from the
941 * max_search_distance_b is an upper bound on the greatest possible
942 * distance between lines in B such that they will both be compared
943 * with the same line in A according to max_search_distance_a.
945 int max_search_distance_a
= 10, max_search_distance_b
;
950 if (max_search_distance_a
>= length_a
)
951 max_search_distance_a
= length_a
? length_a
- 1 : 0;
953 max_search_distance_b
= ((2 * max_search_distance_a
+ 1) * length_b
956 result
= xcalloc(sizeof(int), length_b
);
957 second_best_result
= xcalloc(sizeof(int), length_b
);
958 certainties
= xcalloc(sizeof(int), length_b
);
960 /* See get_similarity() for details of similarities. */
961 similarity_count
= length_b
* (max_search_distance_a
* 2 + 1);
962 similarities
= xcalloc(sizeof(int), similarity_count
);
964 for (i
= 0; i
< length_b
; ++i
) {
966 second_best_result
[i
] = -1;
967 certainties
[i
] = CERTAINTY_NOT_CALCULATED
;
970 for (i
= 0; i
< similarity_count
; ++i
)
971 similarities
[i
] = -1;
973 fuzzy_find_matching_lines_recurse(start_a
, start_b
,
975 fingerprints_a
+ start_a
,
976 fingerprints_b
+ start_b
,
981 max_search_distance_a
,
982 max_search_distance_b
,
983 &map_line_number_in_b_to_a
);
987 free(second_best_result
);
992 static void fill_origin_fingerprints(struct blame_origin
*o
)
998 o
->num_lines
= find_line_starts(&line_starts
, o
->file
.ptr
,
1000 o
->fingerprints
= xcalloc(sizeof(struct fingerprint
), o
->num_lines
);
1001 get_line_fingerprints(o
->fingerprints
, o
->file
.ptr
, line_starts
,
1006 static void drop_origin_fingerprints(struct blame_origin
*o
)
1008 if (o
->fingerprints
) {
1009 free_line_fingerprints(o
->fingerprints
, o
->num_lines
);
1011 FREE_AND_NULL(o
->fingerprints
);
1016 * Given an origin, prepare mmfile_t structure to be used by the
1019 static void fill_origin_blob(struct diff_options
*opt
,
1020 struct blame_origin
*o
, mmfile_t
*file
,
1021 int *num_read_blob
, int fill_fingerprints
)
1024 enum object_type type
;
1025 unsigned long file_size
;
1028 if (opt
->flags
.allow_textconv
&&
1029 textconv_object(opt
->repo
, o
->path
, o
->mode
,
1030 &o
->blob_oid
, 1, &file
->ptr
, &file_size
))
1033 file
->ptr
= read_object_file(&o
->blob_oid
, &type
,
1035 file
->size
= file_size
;
1038 die("Cannot read blob %s for path %s",
1039 oid_to_hex(&o
->blob_oid
),
1045 if (fill_fingerprints
)
1046 fill_origin_fingerprints(o
);
1049 static void drop_origin_blob(struct blame_origin
*o
)
1051 FREE_AND_NULL(o
->file
.ptr
);
1052 drop_origin_fingerprints(o
);
1056 * Any merge of blames happens on lists of blames that arrived via
1057 * different parents in a single suspect. In this case, we want to
1058 * sort according to the suspect line numbers as opposed to the final
1059 * image line numbers. The function body is somewhat longish because
1060 * it avoids unnecessary writes.
1063 static struct blame_entry
*blame_merge(struct blame_entry
*list1
,
1064 struct blame_entry
*list2
)
1066 struct blame_entry
*p1
= list1
, *p2
= list2
,
1074 if (p1
->s_lno
<= p2
->s_lno
) {
1077 if ((p1
= *tail
) == NULL
) {
1081 } while (p1
->s_lno
<= p2
->s_lno
);
1087 if ((p2
= *tail
) == NULL
) {
1091 } while (p1
->s_lno
> p2
->s_lno
);
1095 if ((p1
= *tail
) == NULL
) {
1099 } while (p1
->s_lno
<= p2
->s_lno
);
1103 static void *get_next_blame(const void *p
)
1105 return ((struct blame_entry
*)p
)->next
;
1108 static void set_next_blame(void *p1
, void *p2
)
1110 ((struct blame_entry
*)p1
)->next
= p2
;
1114 * Final image line numbers are all different, so we don't need a
1115 * three-way comparison here.
1118 static int compare_blame_final(const void *p1
, const void *p2
)
1120 return ((struct blame_entry
*)p1
)->lno
> ((struct blame_entry
*)p2
)->lno
1124 static int compare_blame_suspect(const void *p1
, const void *p2
)
1126 const struct blame_entry
*s1
= p1
, *s2
= p2
;
1128 * to allow for collating suspects, we sort according to the
1129 * respective pointer value as the primary sorting criterion.
1130 * The actual relation is pretty unimportant as long as it
1131 * establishes a total order. Comparing as integers gives us
1134 if (s1
->suspect
!= s2
->suspect
)
1135 return (intptr_t)s1
->suspect
> (intptr_t)s2
->suspect
? 1 : -1;
1136 if (s1
->s_lno
== s2
->s_lno
)
1138 return s1
->s_lno
> s2
->s_lno
? 1 : -1;
1141 void blame_sort_final(struct blame_scoreboard
*sb
)
1143 sb
->ent
= llist_mergesort(sb
->ent
, get_next_blame
, set_next_blame
,
1144 compare_blame_final
);
1147 static int compare_commits_by_reverse_commit_date(const void *a
,
1151 return -compare_commits_by_commit_date(a
, b
, c
);
1155 * For debugging -- origin is refcounted, and this asserts that
1156 * we do not underflow.
1158 static void sanity_check_refcnt(struct blame_scoreboard
*sb
)
1161 struct blame_entry
*ent
;
1163 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1164 /* Nobody should have zero or negative refcnt */
1165 if (ent
->suspect
->refcnt
<= 0) {
1166 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1168 oid_to_hex(&ent
->suspect
->commit
->object
.oid
),
1169 ent
->suspect
->refcnt
);
1174 sb
->on_sanity_fail(sb
, baa
);
1178 * If two blame entries that are next to each other came from
1179 * contiguous lines in the same origin (i.e. <commit, path> pair),
1180 * merge them together.
1182 void blame_coalesce(struct blame_scoreboard
*sb
)
1184 struct blame_entry
*ent
, *next
;
1186 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
1187 if (ent
->suspect
== next
->suspect
&&
1188 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
&&
1189 ent
->ignored
== next
->ignored
&&
1190 ent
->unblamable
== next
->unblamable
) {
1191 ent
->num_lines
+= next
->num_lines
;
1192 ent
->next
= next
->next
;
1193 blame_origin_decref(next
->suspect
);
1196 next
= ent
; /* again */
1200 if (sb
->debug
) /* sanity */
1201 sanity_check_refcnt(sb
);
1205 * Merge the given sorted list of blames into a preexisting origin.
1206 * If there were no previous blames to that commit, it is entered into
1207 * the commit priority queue of the score board.
1210 static void queue_blames(struct blame_scoreboard
*sb
, struct blame_origin
*porigin
,
1211 struct blame_entry
*sorted
)
1213 if (porigin
->suspects
)
1214 porigin
->suspects
= blame_merge(porigin
->suspects
, sorted
);
1216 struct blame_origin
*o
;
1217 for (o
= get_blame_suspects(porigin
->commit
); o
; o
= o
->next
) {
1219 porigin
->suspects
= sorted
;
1223 porigin
->suspects
= sorted
;
1224 prio_queue_put(&sb
->commits
, porigin
->commit
);
1229 * Fill the blob_sha1 field of an origin if it hasn't, so that later
1230 * call to fill_origin_blob() can use it to locate the data. blob_sha1
1231 * for an origin is also used to pass the blame for the entire file to
1232 * the parent to detect the case where a child's blob is identical to
1233 * that of its parent's.
1235 * This also fills origin->mode for corresponding tree path.
1237 static int fill_blob_sha1_and_mode(struct repository
*r
,
1238 struct blame_origin
*origin
)
1240 if (!is_null_oid(&origin
->blob_oid
))
1242 if (get_tree_entry(r
, &origin
->commit
->object
.oid
, origin
->path
, &origin
->blob_oid
, &origin
->mode
))
1244 if (oid_object_info(r
, &origin
->blob_oid
, NULL
) != OBJ_BLOB
)
1248 oidclr(&origin
->blob_oid
);
1249 origin
->mode
= S_IFINVALID
;
1254 * We have an origin -- check if the same path exists in the
1255 * parent and return an origin structure to represent it.
1257 static struct blame_origin
*find_origin(struct repository
*r
,
1258 struct commit
*parent
,
1259 struct blame_origin
*origin
)
1261 struct blame_origin
*porigin
;
1262 struct diff_options diff_opts
;
1263 const char *paths
[2];
1265 /* First check any existing origins */
1266 for (porigin
= get_blame_suspects(parent
); porigin
; porigin
= porigin
->next
)
1267 if (!strcmp(porigin
->path
, origin
->path
)) {
1269 * The same path between origin and its parent
1270 * without renaming -- the most common case.
1272 return blame_origin_incref (porigin
);
1275 /* See if the origin->path is different between parent
1276 * and origin first. Most of the time they are the
1277 * same and diff-tree is fairly efficient about this.
1279 repo_diff_setup(r
, &diff_opts
);
1280 diff_opts
.flags
.recursive
= 1;
1281 diff_opts
.detect_rename
= 0;
1282 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1283 paths
[0] = origin
->path
;
1286 parse_pathspec(&diff_opts
.pathspec
,
1287 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1288 PATHSPEC_LITERAL_PATH
, "", paths
);
1289 diff_setup_done(&diff_opts
);
1291 if (is_null_oid(&origin
->commit
->object
.oid
))
1292 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
1294 diff_tree_oid(get_commit_tree_oid(parent
),
1295 get_commit_tree_oid(origin
->commit
),
1297 diffcore_std(&diff_opts
);
1299 if (!diff_queued_diff
.nr
) {
1300 /* The path is the same as parent */
1301 porigin
= get_origin(parent
, origin
->path
);
1302 oidcpy(&porigin
->blob_oid
, &origin
->blob_oid
);
1303 porigin
->mode
= origin
->mode
;
1306 * Since origin->path is a pathspec, if the parent
1307 * commit had it as a directory, we will see a whole
1308 * bunch of deletion of files in the directory that we
1309 * do not care about.
1312 struct diff_filepair
*p
= NULL
;
1313 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1315 p
= diff_queued_diff
.queue
[i
];
1316 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1317 if (!strcmp(name
, origin
->path
))
1321 die("internal error in blame::find_origin");
1322 switch (p
->status
) {
1324 die("internal error in blame::find_origin (%c)",
1327 porigin
= get_origin(parent
, origin
->path
);
1328 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
1329 porigin
->mode
= p
->one
->mode
;
1333 /* Did not exist in parent, or type changed */
1337 diff_flush(&diff_opts
);
1338 clear_pathspec(&diff_opts
.pathspec
);
1343 * We have an origin -- find the path that corresponds to it in its
1344 * parent and return an origin structure to represent it.
1346 static struct blame_origin
*find_rename(struct repository
*r
,
1347 struct commit
*parent
,
1348 struct blame_origin
*origin
)
1350 struct blame_origin
*porigin
= NULL
;
1351 struct diff_options diff_opts
;
1354 repo_diff_setup(r
, &diff_opts
);
1355 diff_opts
.flags
.recursive
= 1;
1356 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
1357 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1358 diff_opts
.single_follow
= origin
->path
;
1359 diff_setup_done(&diff_opts
);
1361 if (is_null_oid(&origin
->commit
->object
.oid
))
1362 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
1364 diff_tree_oid(get_commit_tree_oid(parent
),
1365 get_commit_tree_oid(origin
->commit
),
1367 diffcore_std(&diff_opts
);
1369 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1370 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1371 if ((p
->status
== 'R' || p
->status
== 'C') &&
1372 !strcmp(p
->two
->path
, origin
->path
)) {
1373 porigin
= get_origin(parent
, p
->one
->path
);
1374 oidcpy(&porigin
->blob_oid
, &p
->one
->oid
);
1375 porigin
->mode
= p
->one
->mode
;
1379 diff_flush(&diff_opts
);
1380 clear_pathspec(&diff_opts
.pathspec
);
1385 * Append a new blame entry to a given output queue.
1387 static void add_blame_entry(struct blame_entry
***queue
,
1388 const struct blame_entry
*src
)
1390 struct blame_entry
*e
= xmalloc(sizeof(*e
));
1391 memcpy(e
, src
, sizeof(*e
));
1392 blame_origin_incref(e
->suspect
);
1400 * src typically is on-stack; we want to copy the information in it to
1401 * a malloced blame_entry that gets added to the given queue. The
1402 * origin of dst loses a refcnt.
1404 static void dup_entry(struct blame_entry
***queue
,
1405 struct blame_entry
*dst
, struct blame_entry
*src
)
1407 blame_origin_incref(src
->suspect
);
1408 blame_origin_decref(dst
->suspect
);
1409 memcpy(dst
, src
, sizeof(*src
));
1410 dst
->next
= **queue
;
1412 *queue
= &dst
->next
;
1415 const char *blame_nth_line(struct blame_scoreboard
*sb
, long lno
)
1417 return sb
->final_buf
+ sb
->lineno
[lno
];
1421 * It is known that lines between tlno to same came from parent, and e
1422 * has an overlap with that range. it also is known that parent's
1423 * line plno corresponds to e's line tlno.
1429 * <------------------>
1431 * Split e into potentially three parts; before this chunk, the chunk
1432 * to be blamed for the parent, and after that portion.
1434 static void split_overlap(struct blame_entry
*split
,
1435 struct blame_entry
*e
,
1436 int tlno
, int plno
, int same
,
1437 struct blame_origin
*parent
)
1441 memset(split
, 0, sizeof(struct blame_entry
[3]));
1443 for (i
= 0; i
< 3; i
++) {
1444 split
[i
].ignored
= e
->ignored
;
1445 split
[i
].unblamable
= e
->unblamable
;
1448 if (e
->s_lno
< tlno
) {
1449 /* there is a pre-chunk part not blamed on parent */
1450 split
[0].suspect
= blame_origin_incref(e
->suspect
);
1451 split
[0].lno
= e
->lno
;
1452 split
[0].s_lno
= e
->s_lno
;
1453 split
[0].num_lines
= tlno
- e
->s_lno
;
1454 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
1455 split
[1].s_lno
= plno
;
1458 split
[1].lno
= e
->lno
;
1459 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
1462 if (same
< e
->s_lno
+ e
->num_lines
) {
1463 /* there is a post-chunk part not blamed on parent */
1464 split
[2].suspect
= blame_origin_incref(e
->suspect
);
1465 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
1466 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
1467 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
1468 chunk_end_lno
= split
[2].lno
;
1471 chunk_end_lno
= e
->lno
+ e
->num_lines
;
1472 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
1475 * if it turns out there is nothing to blame the parent for,
1476 * forget about the splitting. !split[1].suspect signals this.
1478 if (split
[1].num_lines
< 1)
1480 split
[1].suspect
= blame_origin_incref(parent
);
1484 * split_overlap() divided an existing blame e into up to three parts
1485 * in split. Any assigned blame is moved to queue to
1486 * reflect the split.
1488 static void split_blame(struct blame_entry
***blamed
,
1489 struct blame_entry
***unblamed
,
1490 struct blame_entry
*split
,
1491 struct blame_entry
*e
)
1493 if (split
[0].suspect
&& split
[2].suspect
) {
1494 /* The first part (reuse storage for the existing entry e) */
1495 dup_entry(unblamed
, e
, &split
[0]);
1497 /* The last part -- me */
1498 add_blame_entry(unblamed
, &split
[2]);
1500 /* ... and the middle part -- parent */
1501 add_blame_entry(blamed
, &split
[1]);
1503 else if (!split
[0].suspect
&& !split
[2].suspect
)
1505 * The parent covers the entire area; reuse storage for
1506 * e and replace it with the parent.
1508 dup_entry(blamed
, e
, &split
[1]);
1509 else if (split
[0].suspect
) {
1510 /* me and then parent */
1511 dup_entry(unblamed
, e
, &split
[0]);
1512 add_blame_entry(blamed
, &split
[1]);
1515 /* parent and then me */
1516 dup_entry(blamed
, e
, &split
[1]);
1517 add_blame_entry(unblamed
, &split
[2]);
1522 * After splitting the blame, the origins used by the
1523 * on-stack blame_entry should lose one refcnt each.
1525 static void decref_split(struct blame_entry
*split
)
1529 for (i
= 0; i
< 3; i
++)
1530 blame_origin_decref(split
[i
].suspect
);
1534 * reverse_blame reverses the list given in head, appending tail.
1535 * That allows us to build lists in reverse order, then reverse them
1536 * afterwards. This can be faster than building the list in proper
1537 * order right away. The reason is that building in proper order
1538 * requires writing a link in the _previous_ element, while building
1539 * in reverse order just requires placing the list head into the
1540 * _current_ element.
1543 static struct blame_entry
*reverse_blame(struct blame_entry
*head
,
1544 struct blame_entry
*tail
)
1547 struct blame_entry
*next
= head
->next
;
1556 * Splits a blame entry into two entries at 'len' lines. The original 'e'
1557 * consists of len lines, i.e. [e->lno, e->lno + len), and the second part,
1558 * which is returned, consists of the remainder: [e->lno + len, e->lno +
1559 * e->num_lines). The caller needs to sort out the reference counting for the
1560 * new entry's suspect.
1562 static struct blame_entry
*split_blame_at(struct blame_entry
*e
, int len
,
1563 struct blame_origin
*new_suspect
)
1565 struct blame_entry
*n
= xcalloc(1, sizeof(struct blame_entry
));
1567 n
->suspect
= new_suspect
;
1568 n
->ignored
= e
->ignored
;
1569 n
->unblamable
= e
->unblamable
;
1570 n
->lno
= e
->lno
+ len
;
1571 n
->s_lno
= e
->s_lno
+ len
;
1572 n
->num_lines
= e
->num_lines
- len
;
1578 struct blame_line_tracker
{
1583 static int are_lines_adjacent(struct blame_line_tracker
*first
,
1584 struct blame_line_tracker
*second
)
1586 return first
->is_parent
== second
->is_parent
&&
1587 first
->s_lno
+ 1 == second
->s_lno
;
1590 static int scan_parent_range(struct fingerprint
*p_fps
,
1591 struct fingerprint
*t_fps
, int t_idx
,
1592 int from
, int nr_lines
)
1595 #define FINGERPRINT_FILE_THRESHOLD 10
1596 int best_sim_val
= FINGERPRINT_FILE_THRESHOLD
;
1597 int best_sim_idx
= -1;
1599 for (p_idx
= from
; p_idx
< from
+ nr_lines
; p_idx
++) {
1600 sim
= fingerprint_similarity(&t_fps
[t_idx
], &p_fps
[p_idx
]);
1601 if (sim
< best_sim_val
)
1603 /* Break ties with the closest-to-target line number */
1604 if (sim
== best_sim_val
&& best_sim_idx
!= -1 &&
1605 abs(best_sim_idx
- t_idx
) < abs(p_idx
- t_idx
))
1608 best_sim_idx
= p_idx
;
1610 return best_sim_idx
;
1614 * The first pass checks the blame entry (from the target) against the parent's
1615 * diff chunk. If that fails for a line, the second pass tries to match that
1616 * line to any part of parent file. That catches cases where a change was
1617 * broken into two chunks by 'context.'
1619 static void guess_line_blames(struct blame_origin
*parent
,
1620 struct blame_origin
*target
,
1621 int tlno
, int offset
, int same
, int parent_len
,
1622 struct blame_line_tracker
*line_blames
)
1624 int i
, best_idx
, target_idx
;
1625 int parent_slno
= tlno
+ offset
;
1628 fuzzy_matches
= fuzzy_find_matching_lines(parent
, target
,
1629 tlno
, parent_slno
, same
,
1631 for (i
= 0; i
< same
- tlno
; i
++) {
1632 target_idx
= tlno
+ i
;
1633 if (fuzzy_matches
&& fuzzy_matches
[i
] >= 0) {
1634 best_idx
= fuzzy_matches
[i
];
1636 best_idx
= scan_parent_range(parent
->fingerprints
,
1637 target
->fingerprints
,
1641 if (best_idx
>= 0) {
1642 line_blames
[i
].is_parent
= 1;
1643 line_blames
[i
].s_lno
= best_idx
;
1645 line_blames
[i
].is_parent
= 0;
1646 line_blames
[i
].s_lno
= target_idx
;
1649 free(fuzzy_matches
);
1653 * This decides which parts of a blame entry go to the parent (added to the
1654 * ignoredp list) and which stay with the target (added to the diffp list). The
1655 * actual decision was made in a separate heuristic function, and those answers
1656 * for the lines in 'e' are in line_blames. This consumes e, essentially
1657 * putting it on a list.
1659 * Note that the blame entries on the ignoredp list are not necessarily sorted
1660 * with respect to the parent's line numbers yet.
1662 static void ignore_blame_entry(struct blame_entry
*e
,
1663 struct blame_origin
*parent
,
1664 struct blame_entry
**diffp
,
1665 struct blame_entry
**ignoredp
,
1666 struct blame_line_tracker
*line_blames
)
1668 int entry_len
, nr_lines
, i
;
1671 * We carve new entries off the front of e. Each entry comes from a
1672 * contiguous chunk of lines: adjacent lines from the same origin
1673 * (either the parent or the target).
1676 nr_lines
= e
->num_lines
; /* e changes in the loop */
1677 for (i
= 0; i
< nr_lines
; i
++) {
1678 struct blame_entry
*next
= NULL
;
1681 * We are often adjacent to the next line - only split the blame
1682 * entry when we have to.
1684 if (i
+ 1 < nr_lines
) {
1685 if (are_lines_adjacent(&line_blames
[i
],
1686 &line_blames
[i
+ 1])) {
1690 next
= split_blame_at(e
, entry_len
,
1691 blame_origin_incref(e
->suspect
));
1693 if (line_blames
[i
].is_parent
) {
1695 blame_origin_decref(e
->suspect
);
1696 e
->suspect
= blame_origin_incref(parent
);
1697 e
->s_lno
= line_blames
[i
- entry_len
+ 1].s_lno
;
1698 e
->next
= *ignoredp
;
1702 /* e->s_lno is already in the target's address space. */
1706 assert(e
->num_lines
== entry_len
);
1714 * Process one hunk from the patch between the current suspect for
1715 * blame_entry e and its parent. This first blames any unfinished
1716 * entries before the chunk (which is where target and parent start
1717 * differing) on the parent, and then splits blame entries at the
1718 * start and at the end of the difference region. Since use of -M and
1719 * -C options may lead to overlapping/duplicate source line number
1720 * ranges, all we can rely on from sorting/merging is the order of the
1721 * first suspect line number.
1723 * tlno: line number in the target where this chunk begins
1724 * same: line number in the target where this chunk ends
1725 * offset: add to tlno to get the chunk starting point in the parent
1726 * parent_len: number of lines in the parent chunk
1728 static void blame_chunk(struct blame_entry
***dstq
, struct blame_entry
***srcq
,
1729 int tlno
, int offset
, int same
, int parent_len
,
1730 struct blame_origin
*parent
,
1731 struct blame_origin
*target
, int ignore_diffs
)
1733 struct blame_entry
*e
= **srcq
;
1734 struct blame_entry
*samep
= NULL
, *diffp
= NULL
, *ignoredp
= NULL
;
1735 struct blame_line_tracker
*line_blames
= NULL
;
1737 while (e
&& e
->s_lno
< tlno
) {
1738 struct blame_entry
*next
= e
->next
;
1740 * current record starts before differing portion. If
1741 * it reaches into it, we need to split it up and
1742 * examine the second part separately.
1744 if (e
->s_lno
+ e
->num_lines
> tlno
) {
1745 /* Move second half to a new record */
1746 struct blame_entry
*n
;
1748 n
= split_blame_at(e
, tlno
- e
->s_lno
, e
->suspect
);
1749 /* Push new record to diffp */
1753 blame_origin_decref(e
->suspect
);
1754 /* Pass blame for everything before the differing
1755 * chunk to the parent */
1756 e
->suspect
= blame_origin_incref(parent
);
1763 * As we don't know how much of a common stretch after this
1764 * diff will occur, the currently blamed parts are all that we
1765 * can assign to the parent for now.
1769 **dstq
= reverse_blame(samep
, **dstq
);
1770 *dstq
= &samep
->next
;
1773 * Prepend the split off portions: everything after e starts
1774 * after the blameable portion.
1776 e
= reverse_blame(diffp
, e
);
1779 * Now retain records on the target while parts are different
1785 if (ignore_diffs
&& same
- tlno
> 0) {
1786 line_blames
= xcalloc(sizeof(struct blame_line_tracker
),
1788 guess_line_blames(parent
, target
, tlno
, offset
, same
,
1789 parent_len
, line_blames
);
1792 while (e
&& e
->s_lno
< same
) {
1793 struct blame_entry
*next
= e
->next
;
1796 * If current record extends into sameness, need to split.
1798 if (e
->s_lno
+ e
->num_lines
> same
) {
1800 * Move second half to a new record to be
1801 * processed by later chunks
1803 struct blame_entry
*n
;
1805 n
= split_blame_at(e
, same
- e
->s_lno
,
1806 blame_origin_incref(e
->suspect
));
1807 /* Push new record to samep */
1812 ignore_blame_entry(e
, parent
, &diffp
, &ignoredp
,
1813 line_blames
+ e
->s_lno
- tlno
);
1823 * Note ignoredp is not sorted yet, and thus neither is dstq.
1824 * That list must be sorted before we queue_blames(). We defer
1825 * sorting until after all diff hunks are processed, so that
1826 * guess_line_blames() can pick *any* line in the parent. The
1827 * slight drawback is that we end up sorting all blame entries
1828 * passed to the parent, including those that are unrelated to
1829 * changes made by the ignored commit.
1831 **dstq
= reverse_blame(ignoredp
, **dstq
);
1832 *dstq
= &ignoredp
->next
;
1834 **srcq
= reverse_blame(diffp
, reverse_blame(samep
, e
));
1835 /* Move across elements that are in the unblamable portion */
1837 *srcq
= &diffp
->next
;
1840 struct blame_chunk_cb_data
{
1841 struct blame_origin
*parent
;
1842 struct blame_origin
*target
;
1845 struct blame_entry
**dstq
;
1846 struct blame_entry
**srcq
;
1849 /* diff chunks are from parent to target */
1850 static int blame_chunk_cb(long start_a
, long count_a
,
1851 long start_b
, long count_b
, void *data
)
1853 struct blame_chunk_cb_data
*d
= data
;
1854 if (start_a
- start_b
!= d
->offset
)
1855 die("internal error in blame::blame_chunk_cb");
1856 blame_chunk(&d
->dstq
, &d
->srcq
, start_b
, start_a
- start_b
,
1857 start_b
+ count_b
, count_a
, d
->parent
, d
->target
,
1859 d
->offset
= start_a
+ count_a
- (start_b
+ count_b
);
1864 * We are looking at the origin 'target' and aiming to pass blame
1865 * for the lines it is suspected to its parent. Run diff to find
1866 * which lines came from parent and pass blame for them.
1868 static void pass_blame_to_parent(struct blame_scoreboard
*sb
,
1869 struct blame_origin
*target
,
1870 struct blame_origin
*parent
, int ignore_diffs
)
1872 mmfile_t file_p
, file_o
;
1873 struct blame_chunk_cb_data d
;
1874 struct blame_entry
*newdest
= NULL
;
1876 if (!target
->suspects
)
1877 return; /* nothing remains for this target */
1882 d
.ignore_diffs
= ignore_diffs
;
1883 d
.dstq
= &newdest
; d
.srcq
= &target
->suspects
;
1885 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
,
1886 &sb
->num_read_blob
, ignore_diffs
);
1887 fill_origin_blob(&sb
->revs
->diffopt
, target
, &file_o
,
1888 &sb
->num_read_blob
, ignore_diffs
);
1889 sb
->num_get_patch
++;
1891 if (diff_hunks(&file_p
, &file_o
, blame_chunk_cb
, &d
, sb
->xdl_opts
))
1892 die("unable to generate diff (%s -> %s)",
1893 oid_to_hex(&parent
->commit
->object
.oid
),
1894 oid_to_hex(&target
->commit
->object
.oid
));
1895 /* The rest are the same as the parent */
1896 blame_chunk(&d
.dstq
, &d
.srcq
, INT_MAX
, d
.offset
, INT_MAX
, 0,
1900 newdest
= llist_mergesort(newdest
, get_next_blame
,
1902 compare_blame_suspect
);
1903 queue_blames(sb
, parent
, newdest
);
1909 * The lines in blame_entry after splitting blames many times can become
1910 * very small and trivial, and at some point it becomes pointless to
1911 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
1912 * ordinary C program, and it is not worth to say it was copied from
1913 * totally unrelated file in the parent.
1915 * Compute how trivial the lines in the blame_entry are.
1917 unsigned blame_entry_score(struct blame_scoreboard
*sb
, struct blame_entry
*e
)
1920 const char *cp
, *ep
;
1926 cp
= blame_nth_line(sb
, e
->lno
);
1927 ep
= blame_nth_line(sb
, e
->lno
+ e
->num_lines
);
1929 unsigned ch
= *((unsigned char *)cp
);
1939 * best_so_far[] and potential[] are both a split of an existing blame_entry
1940 * that passes blame to the parent. Maintain best_so_far the best split so
1941 * far, by comparing potential and best_so_far and copying potential into
1942 * bst_so_far as needed.
1944 static void copy_split_if_better(struct blame_scoreboard
*sb
,
1945 struct blame_entry
*best_so_far
,
1946 struct blame_entry
*potential
)
1950 if (!potential
[1].suspect
)
1952 if (best_so_far
[1].suspect
) {
1953 if (blame_entry_score(sb
, &potential
[1]) <
1954 blame_entry_score(sb
, &best_so_far
[1]))
1958 for (i
= 0; i
< 3; i
++)
1959 blame_origin_incref(potential
[i
].suspect
);
1960 decref_split(best_so_far
);
1961 memcpy(best_so_far
, potential
, sizeof(struct blame_entry
[3]));
1965 * We are looking at a part of the final image represented by
1966 * ent (tlno and same are offset by ent->s_lno).
1967 * tlno is where we are looking at in the final image.
1968 * up to (but not including) same match preimage.
1969 * plno is where we are looking at in the preimage.
1971 * <-------------- final image ---------------------->
1974 * <---------preimage----->
1977 * All line numbers are 0-based.
1979 static void handle_split(struct blame_scoreboard
*sb
,
1980 struct blame_entry
*ent
,
1981 int tlno
, int plno
, int same
,
1982 struct blame_origin
*parent
,
1983 struct blame_entry
*split
)
1985 if (ent
->num_lines
<= tlno
)
1988 struct blame_entry potential
[3];
1991 split_overlap(potential
, ent
, tlno
, plno
, same
, parent
);
1992 copy_split_if_better(sb
, split
, potential
);
1993 decref_split(potential
);
1997 struct handle_split_cb_data
{
1998 struct blame_scoreboard
*sb
;
1999 struct blame_entry
*ent
;
2000 struct blame_origin
*parent
;
2001 struct blame_entry
*split
;
2006 static int handle_split_cb(long start_a
, long count_a
,
2007 long start_b
, long count_b
, void *data
)
2009 struct handle_split_cb_data
*d
= data
;
2010 handle_split(d
->sb
, d
->ent
, d
->tlno
, d
->plno
, start_b
, d
->parent
,
2012 d
->plno
= start_a
+ count_a
;
2013 d
->tlno
= start_b
+ count_b
;
2018 * Find the lines from parent that are the same as ent so that
2019 * we can pass blames to it. file_p has the blob contents for
2022 static void find_copy_in_blob(struct blame_scoreboard
*sb
,
2023 struct blame_entry
*ent
,
2024 struct blame_origin
*parent
,
2025 struct blame_entry
*split
,
2030 struct handle_split_cb_data d
;
2032 memset(&d
, 0, sizeof(d
));
2033 d
.sb
= sb
; d
.ent
= ent
; d
.parent
= parent
; d
.split
= split
;
2035 * Prepare mmfile that contains only the lines in ent.
2037 cp
= blame_nth_line(sb
, ent
->lno
);
2038 file_o
.ptr
= (char *) cp
;
2039 file_o
.size
= blame_nth_line(sb
, ent
->lno
+ ent
->num_lines
) - cp
;
2042 * file_o is a part of final image we are annotating.
2043 * file_p partially may match that image.
2045 memset(split
, 0, sizeof(struct blame_entry
[3]));
2046 if (diff_hunks(file_p
, &file_o
, handle_split_cb
, &d
, sb
->xdl_opts
))
2047 die("unable to generate diff (%s)",
2048 oid_to_hex(&parent
->commit
->object
.oid
));
2049 /* remainder, if any, all match the preimage */
2050 handle_split(sb
, ent
, d
.tlno
, d
.plno
, ent
->num_lines
, parent
, split
);
2053 /* Move all blame entries from list *source that have a score smaller
2054 * than score_min to the front of list *small.
2055 * Returns a pointer to the link pointing to the old head of the small list.
2058 static struct blame_entry
**filter_small(struct blame_scoreboard
*sb
,
2059 struct blame_entry
**small
,
2060 struct blame_entry
**source
,
2063 struct blame_entry
*p
= *source
;
2064 struct blame_entry
*oldsmall
= *small
;
2066 if (blame_entry_score(sb
, p
) <= score_min
) {
2082 * See if lines currently target is suspected for can be attributed to
2085 static void find_move_in_parent(struct blame_scoreboard
*sb
,
2086 struct blame_entry
***blamed
,
2087 struct blame_entry
**toosmall
,
2088 struct blame_origin
*target
,
2089 struct blame_origin
*parent
)
2091 struct blame_entry
*e
, split
[3];
2092 struct blame_entry
*unblamed
= target
->suspects
;
2093 struct blame_entry
*leftover
= NULL
;
2097 return; /* nothing remains for this target */
2099 fill_origin_blob(&sb
->revs
->diffopt
, parent
, &file_p
,
2100 &sb
->num_read_blob
, 0);
2104 /* At each iteration, unblamed has a NULL-terminated list of
2105 * entries that have not yet been tested for blame. leftover
2106 * contains the reversed list of entries that have been tested
2107 * without being assignable to the parent.
2110 struct blame_entry
**unblamedtail
= &unblamed
;
2111 struct blame_entry
*next
;
2112 for (e
= unblamed
; e
; e
= next
) {
2114 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
2115 if (split
[1].suspect
&&
2116 sb
->move_score
< blame_entry_score(sb
, &split
[1])) {
2117 split_blame(blamed
, &unblamedtail
, split
, e
);
2122 decref_split(split
);
2124 *unblamedtail
= NULL
;
2125 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->move_score
);
2127 target
->suspects
= reverse_blame(leftover
, NULL
);
2131 struct blame_entry
*ent
;
2132 struct blame_entry split
[3];
2136 * Count the number of entries the target is suspected for,
2137 * and prepare a list of entry and the best split.
2139 static struct blame_list
*setup_blame_list(struct blame_entry
*unblamed
,
2142 struct blame_entry
*e
;
2144 struct blame_list
*blame_list
= NULL
;
2146 for (e
= unblamed
, num_ents
= 0; e
; e
= e
->next
)
2149 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
2150 for (e
= unblamed
, i
= 0; e
; e
= e
->next
)
2151 blame_list
[i
++].ent
= e
;
2153 *num_ents_p
= num_ents
;
2158 * For lines target is suspected for, see if we can find code movement
2159 * across file boundary from the parent commit. porigin is the path
2160 * in the parent we already tried.
2162 static void find_copy_in_parent(struct blame_scoreboard
*sb
,
2163 struct blame_entry
***blamed
,
2164 struct blame_entry
**toosmall
,
2165 struct blame_origin
*target
,
2166 struct commit
*parent
,
2167 struct blame_origin
*porigin
,
2170 struct diff_options diff_opts
;
2172 struct blame_list
*blame_list
;
2174 struct blame_entry
*unblamed
= target
->suspects
;
2175 struct blame_entry
*leftover
= NULL
;
2178 return; /* nothing remains for this target */
2180 repo_diff_setup(sb
->repo
, &diff_opts
);
2181 diff_opts
.flags
.recursive
= 1;
2182 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
2184 diff_setup_done(&diff_opts
);
2186 /* Try "find copies harder" on new path if requested;
2187 * we do not want to use diffcore_rename() actually to
2188 * match things up; find_copies_harder is set only to
2189 * force diff_tree_oid() to feed all filepairs to diff_queue,
2190 * and this code needs to be after diff_setup_done(), which
2191 * usually makes find-copies-harder imply copy detection.
2193 if ((opt
& PICKAXE_BLAME_COPY_HARDEST
)
2194 || ((opt
& PICKAXE_BLAME_COPY_HARDER
)
2195 && (!porigin
|| strcmp(target
->path
, porigin
->path
))))
2196 diff_opts
.flags
.find_copies_harder
= 1;
2198 if (is_null_oid(&target
->commit
->object
.oid
))
2199 do_diff_cache(get_commit_tree_oid(parent
), &diff_opts
);
2201 diff_tree_oid(get_commit_tree_oid(parent
),
2202 get_commit_tree_oid(target
->commit
),
2205 if (!diff_opts
.flags
.find_copies_harder
)
2206 diffcore_std(&diff_opts
);
2209 struct blame_entry
**unblamedtail
= &unblamed
;
2210 blame_list
= setup_blame_list(unblamed
, &num_ents
);
2212 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
2213 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
2214 struct blame_origin
*norigin
;
2216 struct blame_entry potential
[3];
2218 if (!DIFF_FILE_VALID(p
->one
))
2219 continue; /* does not exist in parent */
2220 if (S_ISGITLINK(p
->one
->mode
))
2221 continue; /* ignore git links */
2222 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
2223 /* find_move already dealt with this path */
2226 norigin
= get_origin(parent
, p
->one
->path
);
2227 oidcpy(&norigin
->blob_oid
, &p
->one
->oid
);
2228 norigin
->mode
= p
->one
->mode
;
2229 fill_origin_blob(&sb
->revs
->diffopt
, norigin
, &file_p
,
2230 &sb
->num_read_blob
, 0);
2234 for (j
= 0; j
< num_ents
; j
++) {
2235 find_copy_in_blob(sb
, blame_list
[j
].ent
,
2236 norigin
, potential
, &file_p
);
2237 copy_split_if_better(sb
, blame_list
[j
].split
,
2239 decref_split(potential
);
2241 blame_origin_decref(norigin
);
2244 for (j
= 0; j
< num_ents
; j
++) {
2245 struct blame_entry
*split
= blame_list
[j
].split
;
2246 if (split
[1].suspect
&&
2247 sb
->copy_score
< blame_entry_score(sb
, &split
[1])) {
2248 split_blame(blamed
, &unblamedtail
, split
,
2251 blame_list
[j
].ent
->next
= leftover
;
2252 leftover
= blame_list
[j
].ent
;
2254 decref_split(split
);
2257 *unblamedtail
= NULL
;
2258 toosmall
= filter_small(sb
, toosmall
, &unblamed
, sb
->copy_score
);
2260 target
->suspects
= reverse_blame(leftover
, NULL
);
2261 diff_flush(&diff_opts
);
2262 clear_pathspec(&diff_opts
.pathspec
);
2266 * The blobs of origin and porigin exactly match, so everything
2267 * origin is suspected for can be blamed on the parent.
2269 static void pass_whole_blame(struct blame_scoreboard
*sb
,
2270 struct blame_origin
*origin
, struct blame_origin
*porigin
)
2272 struct blame_entry
*e
, *suspects
;
2274 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
2275 /* Steal its file */
2276 porigin
->file
= origin
->file
;
2277 origin
->file
.ptr
= NULL
;
2279 suspects
= origin
->suspects
;
2280 origin
->suspects
= NULL
;
2281 for (e
= suspects
; e
; e
= e
->next
) {
2282 blame_origin_incref(porigin
);
2283 blame_origin_decref(e
->suspect
);
2284 e
->suspect
= porigin
;
2286 queue_blames(sb
, porigin
, suspects
);
2290 * We pass blame from the current commit to its parents. We keep saying
2291 * "parent" (and "porigin"), but what we mean is to find scapegoat to
2292 * exonerate ourselves.
2294 static struct commit_list
*first_scapegoat(struct rev_info
*revs
, struct commit
*commit
,
2298 if (revs
->first_parent_only
&&
2300 commit
->parents
->next
) {
2301 free_commit_list(commit
->parents
->next
);
2302 commit
->parents
->next
= NULL
;
2304 return commit
->parents
;
2306 return lookup_decoration(&revs
->children
, &commit
->object
);
2309 static int num_scapegoats(struct rev_info
*revs
, struct commit
*commit
, int reverse
)
2311 struct commit_list
*l
= first_scapegoat(revs
, commit
, reverse
);
2312 return commit_list_count(l
);
2315 /* Distribute collected unsorted blames to the respected sorted lists
2316 * in the various origins.
2318 static void distribute_blame(struct blame_scoreboard
*sb
, struct blame_entry
*blamed
)
2320 blamed
= llist_mergesort(blamed
, get_next_blame
, set_next_blame
,
2321 compare_blame_suspect
);
2324 struct blame_origin
*porigin
= blamed
->suspect
;
2325 struct blame_entry
*suspects
= NULL
;
2327 struct blame_entry
*next
= blamed
->next
;
2328 blamed
->next
= suspects
;
2331 } while (blamed
&& blamed
->suspect
== porigin
);
2332 suspects
= reverse_blame(suspects
, NULL
);
2333 queue_blames(sb
, porigin
, suspects
);
2339 static void pass_blame(struct blame_scoreboard
*sb
, struct blame_origin
*origin
, int opt
)
2341 struct rev_info
*revs
= sb
->revs
;
2342 int i
, pass
, num_sg
;
2343 struct commit
*commit
= origin
->commit
;
2344 struct commit_list
*sg
;
2345 struct blame_origin
*sg_buf
[MAXSG
];
2346 struct blame_origin
*porigin
, **sg_origin
= sg_buf
;
2347 struct blame_entry
*toosmall
= NULL
;
2348 struct blame_entry
*blames
, **blametail
= &blames
;
2350 num_sg
= num_scapegoats(revs
, commit
, sb
->reverse
);
2353 else if (num_sg
< ARRAY_SIZE(sg_buf
))
2354 memset(sg_buf
, 0, sizeof(sg_buf
));
2356 sg_origin
= xcalloc(num_sg
, sizeof(*sg_origin
));
2359 * The first pass looks for unrenamed path to optimize for
2360 * common cases, then we look for renames in the second pass.
2362 for (pass
= 0; pass
< 2 - sb
->no_whole_file_rename
; pass
++) {
2363 struct blame_origin
*(*find
)(struct repository
*, struct commit
*, struct blame_origin
*);
2364 find
= pass
? find_rename
: find_origin
;
2366 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
2368 sg
= sg
->next
, i
++) {
2369 struct commit
*p
= sg
->item
;
2374 if (parse_commit(p
))
2376 porigin
= find(sb
->repo
, p
, origin
);
2379 if (oideq(&porigin
->blob_oid
, &origin
->blob_oid
)) {
2380 pass_whole_blame(sb
, origin
, porigin
);
2381 blame_origin_decref(porigin
);
2384 for (j
= same
= 0; j
< i
; j
++)
2386 oideq(&sg_origin
[j
]->blob_oid
, &porigin
->blob_oid
)) {
2391 sg_origin
[i
] = porigin
;
2393 blame_origin_decref(porigin
);
2398 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
2400 sg
= sg
->next
, i
++) {
2401 struct blame_origin
*porigin
= sg_origin
[i
];
2404 if (!origin
->previous
) {
2405 blame_origin_incref(porigin
);
2406 origin
->previous
= porigin
;
2408 pass_blame_to_parent(sb
, origin
, porigin
, 0);
2409 if (!origin
->suspects
)
2414 * Pass remaining suspects for ignored commits to their parents.
2416 if (oidset_contains(&sb
->ignore_list
, &commit
->object
.oid
)) {
2417 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
2419 sg
= sg
->next
, i
++) {
2420 struct blame_origin
*porigin
= sg_origin
[i
];
2424 pass_blame_to_parent(sb
, origin
, porigin
, 1);
2426 * Preemptively drop porigin so we can refresh the
2427 * fingerprints if we use the parent again, which can
2428 * occur if you ignore back-to-back commits.
2430 drop_origin_blob(porigin
);
2431 if (!origin
->suspects
)
2437 * Optionally find moves in parents' files.
2439 if (opt
& PICKAXE_BLAME_MOVE
) {
2440 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->move_score
);
2441 if (origin
->suspects
) {
2442 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
2444 sg
= sg
->next
, i
++) {
2445 struct blame_origin
*porigin
= sg_origin
[i
];
2448 find_move_in_parent(sb
, &blametail
, &toosmall
, origin
, porigin
);
2449 if (!origin
->suspects
)
2456 * Optionally find copies from parents' files.
2458 if (opt
& PICKAXE_BLAME_COPY
) {
2459 if (sb
->copy_score
> sb
->move_score
)
2460 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
2461 else if (sb
->copy_score
< sb
->move_score
) {
2462 origin
->suspects
= blame_merge(origin
->suspects
, toosmall
);
2464 filter_small(sb
, &toosmall
, &origin
->suspects
, sb
->copy_score
);
2466 if (!origin
->suspects
)
2469 for (i
= 0, sg
= first_scapegoat(revs
, commit
, sb
->reverse
);
2471 sg
= sg
->next
, i
++) {
2472 struct blame_origin
*porigin
= sg_origin
[i
];
2473 find_copy_in_parent(sb
, &blametail
, &toosmall
,
2474 origin
, sg
->item
, porigin
, opt
);
2475 if (!origin
->suspects
)
2482 distribute_blame(sb
, blames
);
2484 * prepend toosmall to origin->suspects
2486 * There is no point in sorting: this ends up on a big
2487 * unsorted list in the caller anyway.
2490 struct blame_entry
**tail
= &toosmall
;
2492 tail
= &(*tail
)->next
;
2493 *tail
= origin
->suspects
;
2494 origin
->suspects
= toosmall
;
2496 for (i
= 0; i
< num_sg
; i
++) {
2498 if (!sg_origin
[i
]->suspects
)
2499 drop_origin_blob(sg_origin
[i
]);
2500 blame_origin_decref(sg_origin
[i
]);
2503 drop_origin_blob(origin
);
2504 if (sg_buf
!= sg_origin
)
2509 * The main loop -- while we have blobs with lines whose true origin
2510 * is still unknown, pick one blob, and allow its lines to pass blames
2511 * to its parents. */
2512 void assign_blame(struct blame_scoreboard
*sb
, int opt
)
2514 struct rev_info
*revs
= sb
->revs
;
2515 struct commit
*commit
= prio_queue_get(&sb
->commits
);
2518 struct blame_entry
*ent
;
2519 struct blame_origin
*suspect
= get_blame_suspects(commit
);
2521 /* find one suspect to break down */
2522 while (suspect
&& !suspect
->suspects
)
2523 suspect
= suspect
->next
;
2526 commit
= prio_queue_get(&sb
->commits
);
2530 assert(commit
== suspect
->commit
);
2533 * We will use this suspect later in the loop,
2534 * so hold onto it in the meantime.
2536 blame_origin_incref(suspect
);
2537 parse_commit(commit
);
2539 (!(commit
->object
.flags
& UNINTERESTING
) &&
2540 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
)))
2541 pass_blame(sb
, suspect
, opt
);
2543 commit
->object
.flags
|= UNINTERESTING
;
2544 if (commit
->object
.parsed
)
2545 mark_parents_uninteresting(commit
);
2547 /* treat root commit as boundary */
2548 if (!commit
->parents
&& !sb
->show_root
)
2549 commit
->object
.flags
|= UNINTERESTING
;
2551 /* Take responsibility for the remaining entries */
2552 ent
= suspect
->suspects
;
2554 suspect
->guilty
= 1;
2556 struct blame_entry
*next
= ent
->next
;
2557 if (sb
->found_guilty_entry
)
2558 sb
->found_guilty_entry(ent
, sb
->found_guilty_entry_data
);
2563 ent
->next
= sb
->ent
;
2564 sb
->ent
= suspect
->suspects
;
2565 suspect
->suspects
= NULL
;
2569 blame_origin_decref(suspect
);
2571 if (sb
->debug
) /* sanity */
2572 sanity_check_refcnt(sb
);
2577 * To allow quick access to the contents of nth line in the
2578 * final image, prepare an index in the scoreboard.
2580 static int prepare_lines(struct blame_scoreboard
*sb
)
2582 sb
->num_lines
= find_line_starts(&sb
->lineno
, sb
->final_buf
,
2583 sb
->final_buf_size
);
2584 return sb
->num_lines
;
2587 static struct commit
*find_single_final(struct rev_info
*revs
,
2588 const char **name_p
)
2591 struct commit
*found
= NULL
;
2592 const char *name
= NULL
;
2594 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2595 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2596 if (obj
->flags
& UNINTERESTING
)
2598 obj
= deref_tag(revs
->repo
, obj
, NULL
, 0);
2599 if (obj
->type
!= OBJ_COMMIT
)
2600 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2602 die("More than one commit to dig from %s and %s?",
2603 revs
->pending
.objects
[i
].name
, name
);
2604 found
= (struct commit
*)obj
;
2605 name
= revs
->pending
.objects
[i
].name
;
2608 *name_p
= xstrdup_or_null(name
);
2612 static struct commit
*dwim_reverse_initial(struct rev_info
*revs
,
2613 const char **name_p
)
2616 * DWIM "git blame --reverse ONE -- PATH" as
2617 * "git blame --reverse ONE..HEAD -- PATH" but only do so
2618 * when it makes sense.
2621 struct commit
*head_commit
;
2622 struct object_id head_oid
;
2624 if (revs
->pending
.nr
!= 1)
2627 /* Is that sole rev a committish? */
2628 obj
= revs
->pending
.objects
[0].item
;
2629 obj
= deref_tag(revs
->repo
, obj
, NULL
, 0);
2630 if (obj
->type
!= OBJ_COMMIT
)
2633 /* Do we have HEAD? */
2634 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &head_oid
, NULL
))
2636 head_commit
= lookup_commit_reference_gently(revs
->repo
,
2641 /* Turn "ONE" into "ONE..HEAD" then */
2642 obj
->flags
|= UNINTERESTING
;
2643 add_pending_object(revs
, &head_commit
->object
, "HEAD");
2646 *name_p
= revs
->pending
.objects
[0].name
;
2647 return (struct commit
*)obj
;
2650 static struct commit
*find_single_initial(struct rev_info
*revs
,
2651 const char **name_p
)
2654 struct commit
*found
= NULL
;
2655 const char *name
= NULL
;
2658 * There must be one and only one negative commit, and it must be
2661 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
2662 struct object
*obj
= revs
->pending
.objects
[i
].item
;
2663 if (!(obj
->flags
& UNINTERESTING
))
2665 obj
= deref_tag(revs
->repo
, obj
, NULL
, 0);
2666 if (obj
->type
!= OBJ_COMMIT
)
2667 die("Non commit %s?", revs
->pending
.objects
[i
].name
);
2669 die("More than one commit to dig up from, %s and %s?",
2670 revs
->pending
.objects
[i
].name
, name
);
2671 found
= (struct commit
*) obj
;
2672 name
= revs
->pending
.objects
[i
].name
;
2676 found
= dwim_reverse_initial(revs
, &name
);
2678 die("No commit to dig up from?");
2681 *name_p
= xstrdup(name
);
2685 void init_scoreboard(struct blame_scoreboard
*sb
)
2687 memset(sb
, 0, sizeof(struct blame_scoreboard
));
2688 sb
->move_score
= BLAME_DEFAULT_MOVE_SCORE
;
2689 sb
->copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2692 void setup_scoreboard(struct blame_scoreboard
*sb
,
2694 struct blame_origin
**orig
)
2696 const char *final_commit_name
= NULL
;
2697 struct blame_origin
*o
;
2698 struct commit
*final_commit
= NULL
;
2699 enum object_type type
;
2701 init_blame_suspects(&blame_suspects
);
2703 if (sb
->reverse
&& sb
->contents_from
)
2704 die(_("--contents and --reverse do not blend well."));
2707 BUG("repo is NULL");
2710 sb
->final
= find_single_final(sb
->revs
, &final_commit_name
);
2711 sb
->commits
.compare
= compare_commits_by_commit_date
;
2713 sb
->final
= find_single_initial(sb
->revs
, &final_commit_name
);
2714 sb
->commits
.compare
= compare_commits_by_reverse_commit_date
;
2717 if (sb
->final
&& sb
->contents_from
)
2718 die(_("cannot use --contents with final commit object name"));
2720 if (sb
->reverse
&& sb
->revs
->first_parent_only
)
2721 sb
->revs
->children
.name
= NULL
;
2725 * "--not A B -- path" without anything positive;
2726 * do not default to HEAD, but use the working tree
2730 sb
->final
= fake_working_tree_commit(sb
->repo
,
2732 path
, sb
->contents_from
);
2733 add_pending_object(sb
->revs
, &(sb
->final
->object
), ":");
2736 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
2737 final_commit
= find_single_final(sb
->revs
, NULL
);
2739 die(_("--reverse and --first-parent together require specified latest commit"));
2743 * If we have bottom, this will mark the ancestors of the
2744 * bottom commits we would reach while traversing as
2747 if (prepare_revision_walk(sb
->revs
))
2748 die(_("revision walk setup failed"));
2750 if (sb
->reverse
&& sb
->revs
->first_parent_only
) {
2751 struct commit
*c
= final_commit
;
2753 sb
->revs
->children
.name
= "children";
2754 while (c
->parents
&&
2755 !oideq(&c
->object
.oid
, &sb
->final
->object
.oid
)) {
2756 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2759 if (add_decoration(&sb
->revs
->children
,
2760 &c
->parents
->item
->object
, l
))
2761 BUG("not unique item in first-parent chain");
2762 c
= c
->parents
->item
;
2765 if (!oideq(&c
->object
.oid
, &sb
->final
->object
.oid
))
2766 die(_("--reverse --first-parent together require range along first-parent chain"));
2769 if (is_null_oid(&sb
->final
->object
.oid
)) {
2770 o
= get_blame_suspects(sb
->final
);
2771 sb
->final_buf
= xmemdupz(o
->file
.ptr
, o
->file
.size
);
2772 sb
->final_buf_size
= o
->file
.size
;
2775 o
= get_origin(sb
->final
, path
);
2776 if (fill_blob_sha1_and_mode(sb
->repo
, o
))
2777 die(_("no such path %s in %s"), path
, final_commit_name
);
2779 if (sb
->revs
->diffopt
.flags
.allow_textconv
&&
2780 textconv_object(sb
->repo
, path
, o
->mode
, &o
->blob_oid
, 1, (char **) &sb
->final_buf
,
2781 &sb
->final_buf_size
))
2784 sb
->final_buf
= read_object_file(&o
->blob_oid
, &type
,
2785 &sb
->final_buf_size
);
2788 die(_("cannot read blob %s for path %s"),
2789 oid_to_hex(&o
->blob_oid
),
2792 sb
->num_read_blob
++;
2798 free((char *)final_commit_name
);
2803 struct blame_entry
*blame_entry_prepend(struct blame_entry
*head
,
2804 long start
, long end
,
2805 struct blame_origin
*o
)
2807 struct blame_entry
*new_head
= xcalloc(1, sizeof(struct blame_entry
));
2808 new_head
->lno
= start
;
2809 new_head
->num_lines
= end
- start
;
2810 new_head
->suspect
= o
;
2811 new_head
->s_lno
= start
;
2812 new_head
->next
= head
;
2813 blame_origin_incref(o
);