]>
Commit | Line | Data |
---|---|---|
072bf432 JS |
1 | #include "cache.h" |
2 | #include "refs.h" | |
cbd53a21 | 3 | #include "object-store.h" |
072bf432 | 4 | #include "cache-tree.h" |
b543bb1c JS |
5 | #include "mergesort.h" |
6 | #include "diff.h" | |
7 | #include "diffcore.h" | |
09002f1b | 8 | #include "tag.h" |
f5dd754c | 9 | #include "blame.h" |
14ba97f8 | 10 | #include "alloc.h" |
4e0df4e6 NTND |
11 | #include "commit-slab.h" |
12 | ||
13 | define_commit_slab(blame_suspects, struct blame_origin *); | |
14 | static struct blame_suspects blame_suspects; | |
15 | ||
16 | struct blame_origin *get_blame_suspects(struct commit *commit) | |
17 | { | |
18 | struct blame_origin **result; | |
19 | ||
20 | result = blame_suspects_peek(&blame_suspects, commit); | |
21 | ||
22 | return result ? *result : NULL; | |
23 | } | |
24 | ||
25 | static void set_blame_suspects(struct commit *commit, struct blame_origin *origin) | |
26 | { | |
27 | *blame_suspects_at(&blame_suspects, commit) = origin; | |
28 | } | |
f5dd754c JS |
29 | |
30 | void blame_origin_decref(struct blame_origin *o) | |
31 | { | |
32 | if (o && --o->refcnt <= 0) { | |
33 | struct blame_origin *p, *l = NULL; | |
34 | if (o->previous) | |
35 | blame_origin_decref(o->previous); | |
36 | free(o->file.ptr); | |
37 | /* Should be present exactly once in commit chain */ | |
4e0df4e6 | 38 | for (p = get_blame_suspects(o->commit); p; l = p, p = p->next) { |
f5dd754c JS |
39 | if (p == o) { |
40 | if (l) | |
41 | l->next = p->next; | |
42 | else | |
4e0df4e6 | 43 | set_blame_suspects(o->commit, p->next); |
f5dd754c JS |
44 | free(o); |
45 | return; | |
46 | } | |
47 | } | |
48 | die("internal error in blame_origin_decref"); | |
49 | } | |
50 | } | |
51 | ||
52 | /* | |
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. | |
57 | */ | |
072bf432 | 58 | static struct blame_origin *make_origin(struct commit *commit, const char *path) |
f5dd754c JS |
59 | { |
60 | struct blame_origin *o; | |
61 | FLEX_ALLOC_STR(o, path, path); | |
62 | o->commit = commit; | |
63 | o->refcnt = 1; | |
4e0df4e6 NTND |
64 | o->next = get_blame_suspects(commit); |
65 | set_blame_suspects(commit, o); | |
f5dd754c JS |
66 | return o; |
67 | } | |
68 | ||
69 | /* | |
70 | * Locate an existing origin or create a new one. | |
71 | * This moves the origin to front position in the commit util list. | |
72 | */ | |
09002f1b | 73 | static struct blame_origin *get_origin(struct commit *commit, const char *path) |
f5dd754c JS |
74 | { |
75 | struct blame_origin *o, *l; | |
76 | ||
4e0df4e6 | 77 | for (o = get_blame_suspects(commit), l = NULL; o; l = o, o = o->next) { |
f5dd754c JS |
78 | if (!strcmp(o->path, path)) { |
79 | /* bump to front */ | |
80 | if (l) { | |
81 | l->next = o->next; | |
4e0df4e6 NTND |
82 | o->next = get_blame_suspects(commit); |
83 | set_blame_suspects(commit, o); | |
f5dd754c JS |
84 | } |
85 | return blame_origin_incref(o); | |
86 | } | |
87 | } | |
88 | return make_origin(commit, path); | |
89 | } | |
072bf432 JS |
90 | |
91 | ||
92 | ||
a470beea | 93 | static void verify_working_tree_path(struct repository *r, |
ecbbc0a5 | 94 | struct commit *work_tree, const char *path) |
072bf432 JS |
95 | { |
96 | struct commit_list *parents; | |
97 | int pos; | |
98 | ||
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; | |
102 | unsigned mode; | |
103 | ||
916bc35b | 104 | if (!get_tree_entry(commit_oid, path, &blob_oid, &mode) && |
a470beea | 105 | oid_object_info(r, &blob_oid, NULL) == OBJ_BLOB) |
072bf432 JS |
106 | return; |
107 | } | |
108 | ||
a470beea | 109 | pos = index_name_pos(r->index, path, strlen(path)); |
072bf432 JS |
110 | if (pos >= 0) |
111 | ; /* path is in the index */ | |
a470beea NTND |
112 | else if (-1 - pos < r->index->cache_nr && |
113 | !strcmp(r->index->cache[-1 - pos]->name, path)) | |
072bf432 JS |
114 | ; /* path is in the index, unmerged */ |
115 | else | |
116 | die("no such path '%s' in HEAD", path); | |
117 | } | |
118 | ||
fb998eae NTND |
119 | static struct commit_list **append_parent(struct repository *r, |
120 | struct commit_list **tail, | |
121 | const struct object_id *oid) | |
072bf432 JS |
122 | { |
123 | struct commit *parent; | |
124 | ||
fb998eae | 125 | parent = lookup_commit_reference(r, oid); |
072bf432 JS |
126 | if (!parent) |
127 | die("no such commit %s", oid_to_hex(oid)); | |
128 | return &commit_list_insert(parent, tail)->next; | |
129 | } | |
130 | ||
fb998eae NTND |
131 | static void append_merge_parents(struct repository *r, |
132 | struct commit_list **tail) | |
072bf432 JS |
133 | { |
134 | int merge_head; | |
135 | struct strbuf line = STRBUF_INIT; | |
136 | ||
fb998eae | 137 | merge_head = open(git_path_merge_head(r), O_RDONLY); |
072bf432 JS |
138 | if (merge_head < 0) { |
139 | if (errno == ENOENT) | |
140 | return; | |
102de880 | 141 | die("cannot open '%s' for reading", |
fb998eae | 142 | git_path_merge_head(r)); |
072bf432 JS |
143 | } |
144 | ||
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)) | |
102de880 | 148 | die("unknown line in '%s': %s", |
fb998eae NTND |
149 | git_path_merge_head(r), line.buf); |
150 | tail = append_parent(r, tail, &oid); | |
072bf432 JS |
151 | } |
152 | close(merge_head); | |
153 | strbuf_release(&line); | |
154 | } | |
155 | ||
156 | /* | |
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 | |
159 | * must use detach). | |
160 | */ | |
fb998eae NTND |
161 | static void set_commit_buffer_from_strbuf(struct repository *r, |
162 | struct commit *c, | |
163 | struct strbuf *sb) | |
072bf432 JS |
164 | { |
165 | size_t len; | |
166 | void *buf = strbuf_detach(sb, &len); | |
fb998eae | 167 | set_commit_buffer(r, c, buf, len); |
072bf432 JS |
168 | } |
169 | ||
170 | /* | |
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. | |
173 | */ | |
a470beea | 174 | static struct commit *fake_working_tree_commit(struct repository *r, |
ecbbc0a5 | 175 | struct diff_options *opt, |
09002f1b JS |
176 | const char *path, |
177 | const char *contents_from) | |
072bf432 JS |
178 | { |
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; | |
184 | const char *ident; | |
185 | time_t now; | |
a849735b | 186 | int len; |
072bf432 JS |
187 | struct cache_entry *ce; |
188 | unsigned mode; | |
189 | struct strbuf msg = STRBUF_INIT; | |
190 | ||
a470beea | 191 | read_index(r->index); |
072bf432 | 192 | time(&now); |
fb998eae | 193 | commit = alloc_commit_node(r); |
072bf432 JS |
194 | commit->object.parsed = 1; |
195 | commit->date = now; | |
196 | parent_tail = &commit->parents; | |
197 | ||
49e61479 | 198 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
072bf432 JS |
199 | die("no such ref: HEAD"); |
200 | ||
fb998eae NTND |
201 | parent_tail = append_parent(r, parent_tail, &head_oid); |
202 | append_merge_parents(r, parent_tail); | |
a470beea | 203 | verify_working_tree_path(r, commit, path); |
072bf432 JS |
204 | |
205 | origin = make_origin(commit, path); | |
206 | ||
207 | ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); | |
208 | strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n"); | |
209 | for (parent = commit->parents; parent; parent = parent->next) | |
210 | strbuf_addf(&msg, "parent %s\n", | |
211 | oid_to_hex(&parent->item->object.oid)); | |
212 | strbuf_addf(&msg, | |
213 | "author %s\n" | |
214 | "committer %s\n\n" | |
215 | "Version of %s from %s\n", | |
216 | ident, ident, path, | |
217 | (!contents_from ? path : | |
218 | (!strcmp(contents_from, "-") ? "standard input" : contents_from))); | |
fb998eae | 219 | set_commit_buffer_from_strbuf(r, commit, &msg); |
072bf432 JS |
220 | |
221 | if (!contents_from || strcmp("-", contents_from)) { | |
222 | struct stat st; | |
223 | const char *read_from; | |
224 | char *buf_ptr; | |
225 | unsigned long buf_len; | |
226 | ||
227 | if (contents_from) { | |
228 | if (stat(contents_from, &st) < 0) | |
229 | die_errno("Cannot stat '%s'", contents_from); | |
230 | read_from = contents_from; | |
231 | } | |
232 | else { | |
233 | if (lstat(path, &st) < 0) | |
234 | die_errno("Cannot lstat '%s'", path); | |
235 | read_from = path; | |
236 | } | |
237 | mode = canon_mode(st.st_mode); | |
238 | ||
239 | switch (st.st_mode & S_IFMT) { | |
240 | case S_IFREG: | |
0d1e0e78 | 241 | if (opt->flags.allow_textconv && |
6afaf807 | 242 | textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len)) |
072bf432 JS |
243 | strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); |
244 | else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) | |
245 | die_errno("cannot open or read '%s'", read_from); | |
246 | break; | |
247 | case S_IFLNK: | |
248 | if (strbuf_readlink(&buf, read_from, st.st_size) < 0) | |
249 | die_errno("cannot readlink '%s'", read_from); | |
250 | break; | |
251 | default: | |
252 | die("unsupported file type %s", read_from); | |
253 | } | |
254 | } | |
255 | else { | |
256 | /* Reading from stdin */ | |
257 | mode = 0; | |
258 | if (strbuf_read(&buf, 0, 0) < 0) | |
259 | die_errno("failed to read from stdin"); | |
260 | } | |
a470beea | 261 | convert_to_git(r->index, path, buf.buf, buf.len, &buf, 0); |
072bf432 JS |
262 | origin->file.ptr = buf.buf; |
263 | origin->file.size = buf.len; | |
829e5c3b | 264 | pretend_object_file(buf.buf, buf.len, OBJ_BLOB, &origin->blob_oid); |
072bf432 JS |
265 | |
266 | /* | |
267 | * Read the current index, replace the path entry with | |
268 | * origin->blob_sha1 without mucking with its mode or type | |
269 | * bits; we are not going to write this index out -- we just | |
270 | * want to run "diff-index --cached". | |
271 | */ | |
a470beea NTND |
272 | discard_index(r->index); |
273 | read_index(r->index); | |
072bf432 JS |
274 | |
275 | len = strlen(path); | |
276 | if (!mode) { | |
a470beea | 277 | int pos = index_name_pos(r->index, path, len); |
072bf432 | 278 | if (0 <= pos) |
a470beea | 279 | mode = r->index->cache[pos]->ce_mode; |
072bf432 JS |
280 | else |
281 | /* Let's not bother reading from HEAD tree */ | |
282 | mode = S_IFREG | 0644; | |
283 | } | |
a470beea | 284 | ce = make_empty_cache_entry(r->index, len); |
072bf432 JS |
285 | oidcpy(&ce->oid, &origin->blob_oid); |
286 | memcpy(ce->name, path, len); | |
287 | ce->ce_flags = create_ce_flags(0); | |
288 | ce->ce_namelen = len; | |
289 | ce->ce_mode = create_ce_mode(mode); | |
a470beea | 290 | add_index_entry(r->index, ce, |
ecbbc0a5 | 291 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
072bf432 | 292 | |
a470beea | 293 | cache_tree_invalidate_path(r->index, path); |
072bf432 JS |
294 | |
295 | return commit; | |
296 | } | |
b543bb1c JS |
297 | |
298 | ||
299 | ||
300 | static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, | |
301 | xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts) | |
302 | { | |
303 | xpparam_t xpp = {0}; | |
304 | xdemitconf_t xecfg = {0}; | |
305 | xdemitcb_t ecb = {NULL}; | |
306 | ||
307 | xpp.flags = xdl_opts; | |
308 | xecfg.hunk_func = hunk_func; | |
309 | ecb.priv = cb_data; | |
310 | return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); | |
311 | } | |
312 | ||
313 | /* | |
314 | * Given an origin, prepare mmfile_t structure to be used by the | |
315 | * diff machinery | |
316 | */ | |
317 | static void fill_origin_blob(struct diff_options *opt, | |
318 | struct blame_origin *o, mmfile_t *file, int *num_read_blob) | |
319 | { | |
320 | if (!o->file.ptr) { | |
321 | enum object_type type; | |
322 | unsigned long file_size; | |
323 | ||
324 | (*num_read_blob)++; | |
0d1e0e78 | 325 | if (opt->flags.allow_textconv && |
6afaf807 NTND |
326 | textconv_object(opt->repo, o->path, o->mode, |
327 | &o->blob_oid, 1, &file->ptr, &file_size)) | |
b543bb1c JS |
328 | ; |
329 | else | |
b4f5aca4 | 330 | file->ptr = read_object_file(&o->blob_oid, &type, |
331 | &file_size); | |
b543bb1c JS |
332 | file->size = file_size; |
333 | ||
334 | if (!file->ptr) | |
335 | die("Cannot read blob %s for path %s", | |
336 | oid_to_hex(&o->blob_oid), | |
337 | o->path); | |
338 | o->file = *file; | |
339 | } | |
340 | else | |
341 | *file = o->file; | |
342 | } | |
343 | ||
344 | static void drop_origin_blob(struct blame_origin *o) | |
345 | { | |
ce528de0 | 346 | FREE_AND_NULL(o->file.ptr); |
b543bb1c JS |
347 | } |
348 | ||
349 | /* | |
350 | * Any merge of blames happens on lists of blames that arrived via | |
351 | * different parents in a single suspect. In this case, we want to | |
352 | * sort according to the suspect line numbers as opposed to the final | |
353 | * image line numbers. The function body is somewhat longish because | |
354 | * it avoids unnecessary writes. | |
355 | */ | |
356 | ||
357 | static struct blame_entry *blame_merge(struct blame_entry *list1, | |
358 | struct blame_entry *list2) | |
359 | { | |
360 | struct blame_entry *p1 = list1, *p2 = list2, | |
361 | **tail = &list1; | |
362 | ||
363 | if (!p1) | |
364 | return p2; | |
365 | if (!p2) | |
366 | return p1; | |
367 | ||
368 | if (p1->s_lno <= p2->s_lno) { | |
369 | do { | |
370 | tail = &p1->next; | |
371 | if ((p1 = *tail) == NULL) { | |
372 | *tail = p2; | |
373 | return list1; | |
374 | } | |
375 | } while (p1->s_lno <= p2->s_lno); | |
376 | } | |
377 | for (;;) { | |
378 | *tail = p2; | |
379 | do { | |
380 | tail = &p2->next; | |
381 | if ((p2 = *tail) == NULL) { | |
382 | *tail = p1; | |
383 | return list1; | |
384 | } | |
385 | } while (p1->s_lno > p2->s_lno); | |
386 | *tail = p1; | |
387 | do { | |
388 | tail = &p1->next; | |
389 | if ((p1 = *tail) == NULL) { | |
390 | *tail = p2; | |
391 | return list1; | |
392 | } | |
393 | } while (p1->s_lno <= p2->s_lno); | |
394 | } | |
395 | } | |
396 | ||
397 | static void *get_next_blame(const void *p) | |
398 | { | |
399 | return ((struct blame_entry *)p)->next; | |
400 | } | |
401 | ||
402 | static void set_next_blame(void *p1, void *p2) | |
403 | { | |
404 | ((struct blame_entry *)p1)->next = p2; | |
405 | } | |
406 | ||
407 | /* | |
408 | * Final image line numbers are all different, so we don't need a | |
409 | * three-way comparison here. | |
410 | */ | |
411 | ||
412 | static int compare_blame_final(const void *p1, const void *p2) | |
413 | { | |
414 | return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno | |
415 | ? 1 : -1; | |
416 | } | |
417 | ||
418 | static int compare_blame_suspect(const void *p1, const void *p2) | |
419 | { | |
420 | const struct blame_entry *s1 = p1, *s2 = p2; | |
421 | /* | |
422 | * to allow for collating suspects, we sort according to the | |
423 | * respective pointer value as the primary sorting criterion. | |
424 | * The actual relation is pretty unimportant as long as it | |
425 | * establishes a total order. Comparing as integers gives us | |
426 | * that. | |
427 | */ | |
428 | if (s1->suspect != s2->suspect) | |
429 | return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1; | |
430 | if (s1->s_lno == s2->s_lno) | |
431 | return 0; | |
432 | return s1->s_lno > s2->s_lno ? 1 : -1; | |
433 | } | |
434 | ||
435 | void blame_sort_final(struct blame_scoreboard *sb) | |
436 | { | |
437 | sb->ent = llist_mergesort(sb->ent, get_next_blame, set_next_blame, | |
438 | compare_blame_final); | |
439 | } | |
440 | ||
09002f1b JS |
441 | static int compare_commits_by_reverse_commit_date(const void *a, |
442 | const void *b, | |
443 | void *c) | |
444 | { | |
445 | return -compare_commits_by_commit_date(a, b, c); | |
446 | } | |
447 | ||
b543bb1c JS |
448 | /* |
449 | * For debugging -- origin is refcounted, and this asserts that | |
450 | * we do not underflow. | |
451 | */ | |
452 | static void sanity_check_refcnt(struct blame_scoreboard *sb) | |
453 | { | |
454 | int baa = 0; | |
455 | struct blame_entry *ent; | |
456 | ||
457 | for (ent = sb->ent; ent; ent = ent->next) { | |
458 | /* Nobody should have zero or negative refcnt */ | |
459 | if (ent->suspect->refcnt <= 0) { | |
460 | fprintf(stderr, "%s in %s has negative refcnt %d\n", | |
461 | ent->suspect->path, | |
462 | oid_to_hex(&ent->suspect->commit->object.oid), | |
463 | ent->suspect->refcnt); | |
464 | baa = 1; | |
465 | } | |
466 | } | |
467 | if (baa) | |
468 | sb->on_sanity_fail(sb, baa); | |
469 | } | |
470 | ||
471 | /* | |
472 | * If two blame entries that are next to each other came from | |
473 | * contiguous lines in the same origin (i.e. <commit, path> pair), | |
474 | * merge them together. | |
475 | */ | |
476 | void blame_coalesce(struct blame_scoreboard *sb) | |
477 | { | |
478 | struct blame_entry *ent, *next; | |
479 | ||
480 | for (ent = sb->ent; ent && (next = ent->next); ent = next) { | |
481 | if (ent->suspect == next->suspect && | |
8934ac8c BR |
482 | ent->s_lno + ent->num_lines == next->s_lno && |
483 | ent->ignored == next->ignored && | |
484 | ent->unblamable == next->unblamable) { | |
b543bb1c JS |
485 | ent->num_lines += next->num_lines; |
486 | ent->next = next->next; | |
487 | blame_origin_decref(next->suspect); | |
488 | free(next); | |
489 | ent->score = 0; | |
490 | next = ent; /* again */ | |
491 | } | |
492 | } | |
493 | ||
494 | if (sb->debug) /* sanity */ | |
495 | sanity_check_refcnt(sb); | |
496 | } | |
497 | ||
498 | /* | |
499 | * Merge the given sorted list of blames into a preexisting origin. | |
500 | * If there were no previous blames to that commit, it is entered into | |
501 | * the commit priority queue of the score board. | |
502 | */ | |
503 | ||
504 | static void queue_blames(struct blame_scoreboard *sb, struct blame_origin *porigin, | |
505 | struct blame_entry *sorted) | |
506 | { | |
507 | if (porigin->suspects) | |
508 | porigin->suspects = blame_merge(porigin->suspects, sorted); | |
509 | else { | |
510 | struct blame_origin *o; | |
4e0df4e6 | 511 | for (o = get_blame_suspects(porigin->commit); o; o = o->next) { |
b543bb1c JS |
512 | if (o->suspects) { |
513 | porigin->suspects = sorted; | |
514 | return; | |
515 | } | |
516 | } | |
517 | porigin->suspects = sorted; | |
518 | prio_queue_put(&sb->commits, porigin->commit); | |
519 | } | |
520 | } | |
521 | ||
09002f1b JS |
522 | /* |
523 | * Fill the blob_sha1 field of an origin if it hasn't, so that later | |
524 | * call to fill_origin_blob() can use it to locate the data. blob_sha1 | |
525 | * for an origin is also used to pass the blame for the entire file to | |
526 | * the parent to detect the case where a child's blob is identical to | |
527 | * that of its parent's. | |
528 | * | |
529 | * This also fills origin->mode for corresponding tree path. | |
530 | */ | |
a470beea | 531 | static int fill_blob_sha1_and_mode(struct repository *r, |
ecbbc0a5 | 532 | struct blame_origin *origin) |
09002f1b JS |
533 | { |
534 | if (!is_null_oid(&origin->blob_oid)) | |
535 | return 0; | |
916bc35b | 536 | if (get_tree_entry(&origin->commit->object.oid, origin->path, &origin->blob_oid, &origin->mode)) |
09002f1b | 537 | goto error_out; |
a470beea | 538 | if (oid_object_info(r, &origin->blob_oid, NULL) != OBJ_BLOB) |
09002f1b JS |
539 | goto error_out; |
540 | return 0; | |
541 | error_out: | |
542 | oidclr(&origin->blob_oid); | |
543 | origin->mode = S_IFINVALID; | |
544 | return -1; | |
545 | } | |
546 | ||
b543bb1c JS |
547 | /* |
548 | * We have an origin -- check if the same path exists in the | |
549 | * parent and return an origin structure to represent it. | |
550 | */ | |
e6757652 NTND |
551 | static struct blame_origin *find_origin(struct repository *r, |
552 | struct commit *parent, | |
553 | struct blame_origin *origin) | |
b543bb1c JS |
554 | { |
555 | struct blame_origin *porigin; | |
556 | struct diff_options diff_opts; | |
557 | const char *paths[2]; | |
558 | ||
559 | /* First check any existing origins */ | |
4e0df4e6 | 560 | for (porigin = get_blame_suspects(parent); porigin; porigin = porigin->next) |
b543bb1c JS |
561 | if (!strcmp(porigin->path, origin->path)) { |
562 | /* | |
563 | * The same path between origin and its parent | |
564 | * without renaming -- the most common case. | |
565 | */ | |
566 | return blame_origin_incref (porigin); | |
567 | } | |
568 | ||
569 | /* See if the origin->path is different between parent | |
570 | * and origin first. Most of the time they are the | |
571 | * same and diff-tree is fairly efficient about this. | |
572 | */ | |
e6757652 | 573 | repo_diff_setup(r, &diff_opts); |
0d1e0e78 | 574 | diff_opts.flags.recursive = 1; |
b543bb1c JS |
575 | diff_opts.detect_rename = 0; |
576 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
577 | paths[0] = origin->path; | |
578 | paths[1] = NULL; | |
579 | ||
580 | parse_pathspec(&diff_opts.pathspec, | |
581 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
582 | PATHSPEC_LITERAL_PATH, "", paths); | |
583 | diff_setup_done(&diff_opts); | |
584 | ||
585 | if (is_null_oid(&origin->commit->object.oid)) | |
2e27bd77 | 586 | do_diff_cache(get_commit_tree_oid(parent), &diff_opts); |
b543bb1c | 587 | else |
2e27bd77 DS |
588 | diff_tree_oid(get_commit_tree_oid(parent), |
589 | get_commit_tree_oid(origin->commit), | |
a6f38c10 | 590 | "", &diff_opts); |
b543bb1c JS |
591 | diffcore_std(&diff_opts); |
592 | ||
593 | if (!diff_queued_diff.nr) { | |
594 | /* The path is the same as parent */ | |
595 | porigin = get_origin(parent, origin->path); | |
596 | oidcpy(&porigin->blob_oid, &origin->blob_oid); | |
597 | porigin->mode = origin->mode; | |
598 | } else { | |
599 | /* | |
600 | * Since origin->path is a pathspec, if the parent | |
601 | * commit had it as a directory, we will see a whole | |
602 | * bunch of deletion of files in the directory that we | |
603 | * do not care about. | |
604 | */ | |
605 | int i; | |
606 | struct diff_filepair *p = NULL; | |
607 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
608 | const char *name; | |
609 | p = diff_queued_diff.queue[i]; | |
610 | name = p->one->path ? p->one->path : p->two->path; | |
611 | if (!strcmp(name, origin->path)) | |
612 | break; | |
613 | } | |
614 | if (!p) | |
615 | die("internal error in blame::find_origin"); | |
616 | switch (p->status) { | |
617 | default: | |
618 | die("internal error in blame::find_origin (%c)", | |
619 | p->status); | |
620 | case 'M': | |
621 | porigin = get_origin(parent, origin->path); | |
622 | oidcpy(&porigin->blob_oid, &p->one->oid); | |
623 | porigin->mode = p->one->mode; | |
624 | break; | |
625 | case 'A': | |
626 | case 'T': | |
627 | /* Did not exist in parent, or type changed */ | |
628 | break; | |
629 | } | |
630 | } | |
631 | diff_flush(&diff_opts); | |
632 | clear_pathspec(&diff_opts.pathspec); | |
633 | return porigin; | |
634 | } | |
635 | ||
636 | /* | |
637 | * We have an origin -- find the path that corresponds to it in its | |
638 | * parent and return an origin structure to represent it. | |
639 | */ | |
e6757652 NTND |
640 | static struct blame_origin *find_rename(struct repository *r, |
641 | struct commit *parent, | |
642 | struct blame_origin *origin) | |
b543bb1c JS |
643 | { |
644 | struct blame_origin *porigin = NULL; | |
645 | struct diff_options diff_opts; | |
646 | int i; | |
647 | ||
e6757652 | 648 | repo_diff_setup(r, &diff_opts); |
0d1e0e78 | 649 | diff_opts.flags.recursive = 1; |
b543bb1c JS |
650 | diff_opts.detect_rename = DIFF_DETECT_RENAME; |
651 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
652 | diff_opts.single_follow = origin->path; | |
653 | diff_setup_done(&diff_opts); | |
654 | ||
655 | if (is_null_oid(&origin->commit->object.oid)) | |
2e27bd77 | 656 | do_diff_cache(get_commit_tree_oid(parent), &diff_opts); |
b543bb1c | 657 | else |
2e27bd77 DS |
658 | diff_tree_oid(get_commit_tree_oid(parent), |
659 | get_commit_tree_oid(origin->commit), | |
a6f38c10 | 660 | "", &diff_opts); |
b543bb1c JS |
661 | diffcore_std(&diff_opts); |
662 | ||
663 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
664 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
665 | if ((p->status == 'R' || p->status == 'C') && | |
666 | !strcmp(p->two->path, origin->path)) { | |
667 | porigin = get_origin(parent, p->one->path); | |
668 | oidcpy(&porigin->blob_oid, &p->one->oid); | |
669 | porigin->mode = p->one->mode; | |
670 | break; | |
671 | } | |
672 | } | |
673 | diff_flush(&diff_opts); | |
674 | clear_pathspec(&diff_opts.pathspec); | |
675 | return porigin; | |
676 | } | |
677 | ||
678 | /* | |
679 | * Append a new blame entry to a given output queue. | |
680 | */ | |
681 | static void add_blame_entry(struct blame_entry ***queue, | |
682 | const struct blame_entry *src) | |
683 | { | |
684 | struct blame_entry *e = xmalloc(sizeof(*e)); | |
685 | memcpy(e, src, sizeof(*e)); | |
686 | blame_origin_incref(e->suspect); | |
687 | ||
688 | e->next = **queue; | |
689 | **queue = e; | |
690 | *queue = &e->next; | |
691 | } | |
692 | ||
693 | /* | |
694 | * src typically is on-stack; we want to copy the information in it to | |
695 | * a malloced blame_entry that gets added to the given queue. The | |
696 | * origin of dst loses a refcnt. | |
697 | */ | |
698 | static void dup_entry(struct blame_entry ***queue, | |
699 | struct blame_entry *dst, struct blame_entry *src) | |
700 | { | |
701 | blame_origin_incref(src->suspect); | |
702 | blame_origin_decref(dst->suspect); | |
703 | memcpy(dst, src, sizeof(*src)); | |
704 | dst->next = **queue; | |
705 | **queue = dst; | |
706 | *queue = &dst->next; | |
707 | } | |
708 | ||
709 | const char *blame_nth_line(struct blame_scoreboard *sb, long lno) | |
710 | { | |
711 | return sb->final_buf + sb->lineno[lno]; | |
712 | } | |
713 | ||
714 | /* | |
715 | * It is known that lines between tlno to same came from parent, and e | |
716 | * has an overlap with that range. it also is known that parent's | |
717 | * line plno corresponds to e's line tlno. | |
718 | * | |
719 | * <---- e -----> | |
720 | * <------> | |
721 | * <------------> | |
722 | * <------------> | |
723 | * <------------------> | |
724 | * | |
725 | * Split e into potentially three parts; before this chunk, the chunk | |
726 | * to be blamed for the parent, and after that portion. | |
727 | */ | |
728 | static void split_overlap(struct blame_entry *split, | |
729 | struct blame_entry *e, | |
730 | int tlno, int plno, int same, | |
731 | struct blame_origin *parent) | |
732 | { | |
733 | int chunk_end_lno; | |
8934ac8c | 734 | int i; |
b543bb1c JS |
735 | memset(split, 0, sizeof(struct blame_entry [3])); |
736 | ||
8934ac8c BR |
737 | for (i = 0; i < 3; i++) { |
738 | split[i].ignored = e->ignored; | |
739 | split[i].unblamable = e->unblamable; | |
740 | } | |
741 | ||
b543bb1c JS |
742 | if (e->s_lno < tlno) { |
743 | /* there is a pre-chunk part not blamed on parent */ | |
744 | split[0].suspect = blame_origin_incref(e->suspect); | |
745 | split[0].lno = e->lno; | |
746 | split[0].s_lno = e->s_lno; | |
747 | split[0].num_lines = tlno - e->s_lno; | |
748 | split[1].lno = e->lno + tlno - e->s_lno; | |
749 | split[1].s_lno = plno; | |
750 | } | |
751 | else { | |
752 | split[1].lno = e->lno; | |
753 | split[1].s_lno = plno + (e->s_lno - tlno); | |
754 | } | |
755 | ||
756 | if (same < e->s_lno + e->num_lines) { | |
757 | /* there is a post-chunk part not blamed on parent */ | |
758 | split[2].suspect = blame_origin_incref(e->suspect); | |
759 | split[2].lno = e->lno + (same - e->s_lno); | |
760 | split[2].s_lno = e->s_lno + (same - e->s_lno); | |
761 | split[2].num_lines = e->s_lno + e->num_lines - same; | |
762 | chunk_end_lno = split[2].lno; | |
763 | } | |
764 | else | |
765 | chunk_end_lno = e->lno + e->num_lines; | |
766 | split[1].num_lines = chunk_end_lno - split[1].lno; | |
767 | ||
768 | /* | |
769 | * if it turns out there is nothing to blame the parent for, | |
770 | * forget about the splitting. !split[1].suspect signals this. | |
771 | */ | |
772 | if (split[1].num_lines < 1) | |
773 | return; | |
774 | split[1].suspect = blame_origin_incref(parent); | |
775 | } | |
776 | ||
777 | /* | |
778 | * split_overlap() divided an existing blame e into up to three parts | |
779 | * in split. Any assigned blame is moved to queue to | |
780 | * reflect the split. | |
781 | */ | |
782 | static void split_blame(struct blame_entry ***blamed, | |
783 | struct blame_entry ***unblamed, | |
784 | struct blame_entry *split, | |
785 | struct blame_entry *e) | |
786 | { | |
787 | if (split[0].suspect && split[2].suspect) { | |
788 | /* The first part (reuse storage for the existing entry e) */ | |
789 | dup_entry(unblamed, e, &split[0]); | |
790 | ||
791 | /* The last part -- me */ | |
792 | add_blame_entry(unblamed, &split[2]); | |
793 | ||
794 | /* ... and the middle part -- parent */ | |
795 | add_blame_entry(blamed, &split[1]); | |
796 | } | |
797 | else if (!split[0].suspect && !split[2].suspect) | |
798 | /* | |
799 | * The parent covers the entire area; reuse storage for | |
800 | * e and replace it with the parent. | |
801 | */ | |
802 | dup_entry(blamed, e, &split[1]); | |
803 | else if (split[0].suspect) { | |
804 | /* me and then parent */ | |
805 | dup_entry(unblamed, e, &split[0]); | |
806 | add_blame_entry(blamed, &split[1]); | |
807 | } | |
808 | else { | |
809 | /* parent and then me */ | |
810 | dup_entry(blamed, e, &split[1]); | |
811 | add_blame_entry(unblamed, &split[2]); | |
812 | } | |
813 | } | |
814 | ||
815 | /* | |
816 | * After splitting the blame, the origins used by the | |
817 | * on-stack blame_entry should lose one refcnt each. | |
818 | */ | |
819 | static void decref_split(struct blame_entry *split) | |
820 | { | |
821 | int i; | |
822 | ||
823 | for (i = 0; i < 3; i++) | |
824 | blame_origin_decref(split[i].suspect); | |
825 | } | |
826 | ||
827 | /* | |
828 | * reverse_blame reverses the list given in head, appending tail. | |
829 | * That allows us to build lists in reverse order, then reverse them | |
830 | * afterwards. This can be faster than building the list in proper | |
831 | * order right away. The reason is that building in proper order | |
832 | * requires writing a link in the _previous_ element, while building | |
833 | * in reverse order just requires placing the list head into the | |
834 | * _current_ element. | |
835 | */ | |
836 | ||
837 | static struct blame_entry *reverse_blame(struct blame_entry *head, | |
838 | struct blame_entry *tail) | |
839 | { | |
840 | while (head) { | |
841 | struct blame_entry *next = head->next; | |
842 | head->next = tail; | |
843 | tail = head; | |
844 | head = next; | |
845 | } | |
846 | return tail; | |
847 | } | |
848 | ||
55f808fb BR |
849 | /* |
850 | * Splits a blame entry into two entries at 'len' lines. The original 'e' | |
851 | * consists of len lines, i.e. [e->lno, e->lno + len), and the second part, | |
852 | * which is returned, consists of the remainder: [e->lno + len, e->lno + | |
853 | * e->num_lines). The caller needs to sort out the reference counting for the | |
854 | * new entry's suspect. | |
855 | */ | |
856 | static struct blame_entry *split_blame_at(struct blame_entry *e, int len, | |
857 | struct blame_origin *new_suspect) | |
858 | { | |
859 | struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry)); | |
860 | ||
861 | n->suspect = new_suspect; | |
8934ac8c BR |
862 | n->ignored = e->ignored; |
863 | n->unblamable = e->unblamable; | |
55f808fb BR |
864 | n->lno = e->lno + len; |
865 | n->s_lno = e->s_lno + len; | |
866 | n->num_lines = e->num_lines - len; | |
867 | e->num_lines = len; | |
868 | e->score = 0; | |
869 | return n; | |
870 | } | |
871 | ||
ae3f36de BR |
872 | struct blame_line_tracker { |
873 | int is_parent; | |
874 | int s_lno; | |
875 | }; | |
876 | ||
877 | static int are_lines_adjacent(struct blame_line_tracker *first, | |
878 | struct blame_line_tracker *second) | |
879 | { | |
880 | return first->is_parent == second->is_parent && | |
881 | first->s_lno + 1 == second->s_lno; | |
882 | } | |
883 | ||
884 | /* | |
885 | * This cheap heuristic assigns lines in the chunk to their relative location in | |
886 | * the parent's chunk. Any additional lines are left with the target. | |
887 | */ | |
888 | static void guess_line_blames(struct blame_origin *parent, | |
889 | struct blame_origin *target, | |
890 | int tlno, int offset, int same, int parent_len, | |
891 | struct blame_line_tracker *line_blames) | |
892 | { | |
893 | int i, best_idx, target_idx; | |
894 | int parent_slno = tlno + offset; | |
895 | ||
896 | for (i = 0; i < same - tlno; i++) { | |
897 | target_idx = tlno + i; | |
898 | best_idx = target_idx + offset; | |
899 | if (best_idx < parent_slno + parent_len) { | |
900 | line_blames[i].is_parent = 1; | |
901 | line_blames[i].s_lno = best_idx; | |
902 | } else { | |
903 | line_blames[i].is_parent = 0; | |
904 | line_blames[i].s_lno = target_idx; | |
905 | } | |
906 | } | |
907 | } | |
908 | ||
909 | /* | |
910 | * This decides which parts of a blame entry go to the parent (added to the | |
911 | * ignoredp list) and which stay with the target (added to the diffp list). The | |
912 | * actual decision was made in a separate heuristic function, and those answers | |
913 | * for the lines in 'e' are in line_blames. This consumes e, essentially | |
914 | * putting it on a list. | |
915 | * | |
916 | * Note that the blame entries on the ignoredp list are not necessarily sorted | |
917 | * with respect to the parent's line numbers yet. | |
918 | */ | |
919 | static void ignore_blame_entry(struct blame_entry *e, | |
920 | struct blame_origin *parent, | |
921 | struct blame_origin *target, | |
922 | struct blame_entry **diffp, | |
923 | struct blame_entry **ignoredp, | |
924 | struct blame_line_tracker *line_blames) | |
925 | { | |
926 | int entry_len, nr_lines, i; | |
927 | ||
928 | /* | |
929 | * We carve new entries off the front of e. Each entry comes from a | |
930 | * contiguous chunk of lines: adjacent lines from the same origin | |
931 | * (either the parent or the target). | |
932 | */ | |
933 | entry_len = 1; | |
934 | nr_lines = e->num_lines; /* e changes in the loop */ | |
935 | for (i = 0; i < nr_lines; i++) { | |
936 | struct blame_entry *next = NULL; | |
937 | ||
938 | /* | |
939 | * We are often adjacent to the next line - only split the blame | |
940 | * entry when we have to. | |
941 | */ | |
942 | if (i + 1 < nr_lines) { | |
943 | if (are_lines_adjacent(&line_blames[i], | |
944 | &line_blames[i + 1])) { | |
945 | entry_len++; | |
946 | continue; | |
947 | } | |
948 | next = split_blame_at(e, entry_len, | |
949 | blame_origin_incref(e->suspect)); | |
950 | } | |
951 | if (line_blames[i].is_parent) { | |
8934ac8c | 952 | e->ignored = 1; |
ae3f36de BR |
953 | blame_origin_decref(e->suspect); |
954 | e->suspect = blame_origin_incref(parent); | |
955 | e->s_lno = line_blames[i - entry_len + 1].s_lno; | |
956 | e->next = *ignoredp; | |
957 | *ignoredp = e; | |
958 | } else { | |
8934ac8c | 959 | e->unblamable = 1; |
ae3f36de BR |
960 | /* e->s_lno is already in the target's address space. */ |
961 | e->next = *diffp; | |
962 | *diffp = e; | |
963 | } | |
964 | assert(e->num_lines == entry_len); | |
965 | e = next; | |
966 | entry_len = 1; | |
967 | } | |
968 | assert(!e); | |
969 | } | |
970 | ||
b543bb1c JS |
971 | /* |
972 | * Process one hunk from the patch between the current suspect for | |
973 | * blame_entry e and its parent. This first blames any unfinished | |
974 | * entries before the chunk (which is where target and parent start | |
975 | * differing) on the parent, and then splits blame entries at the | |
976 | * start and at the end of the difference region. Since use of -M and | |
977 | * -C options may lead to overlapping/duplicate source line number | |
978 | * ranges, all we can rely on from sorting/merging is the order of the | |
979 | * first suspect line number. | |
ae3f36de BR |
980 | * |
981 | * tlno: line number in the target where this chunk begins | |
982 | * same: line number in the target where this chunk ends | |
983 | * offset: add to tlno to get the chunk starting point in the parent | |
984 | * parent_len: number of lines in the parent chunk | |
b543bb1c JS |
985 | */ |
986 | static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq, | |
ae3f36de BR |
987 | int tlno, int offset, int same, int parent_len, |
988 | struct blame_origin *parent, | |
989 | struct blame_origin *target, int ignore_diffs) | |
b543bb1c JS |
990 | { |
991 | struct blame_entry *e = **srcq; | |
ae3f36de BR |
992 | struct blame_entry *samep = NULL, *diffp = NULL, *ignoredp = NULL; |
993 | struct blame_line_tracker *line_blames = NULL; | |
b543bb1c JS |
994 | |
995 | while (e && e->s_lno < tlno) { | |
996 | struct blame_entry *next = e->next; | |
997 | /* | |
998 | * current record starts before differing portion. If | |
999 | * it reaches into it, we need to split it up and | |
1000 | * examine the second part separately. | |
1001 | */ | |
1002 | if (e->s_lno + e->num_lines > tlno) { | |
1003 | /* Move second half to a new record */ | |
55f808fb BR |
1004 | struct blame_entry *n; |
1005 | ||
1006 | n = split_blame_at(e, tlno - e->s_lno, e->suspect); | |
b543bb1c JS |
1007 | /* Push new record to diffp */ |
1008 | n->next = diffp; | |
1009 | diffp = n; | |
1010 | } else | |
1011 | blame_origin_decref(e->suspect); | |
1012 | /* Pass blame for everything before the differing | |
1013 | * chunk to the parent */ | |
1014 | e->suspect = blame_origin_incref(parent); | |
1015 | e->s_lno += offset; | |
1016 | e->next = samep; | |
1017 | samep = e; | |
1018 | e = next; | |
1019 | } | |
1020 | /* | |
1021 | * As we don't know how much of a common stretch after this | |
1022 | * diff will occur, the currently blamed parts are all that we | |
1023 | * can assign to the parent for now. | |
1024 | */ | |
1025 | ||
1026 | if (samep) { | |
1027 | **dstq = reverse_blame(samep, **dstq); | |
1028 | *dstq = &samep->next; | |
1029 | } | |
1030 | /* | |
1031 | * Prepend the split off portions: everything after e starts | |
1032 | * after the blameable portion. | |
1033 | */ | |
1034 | e = reverse_blame(diffp, e); | |
1035 | ||
1036 | /* | |
1037 | * Now retain records on the target while parts are different | |
1038 | * from the parent. | |
1039 | */ | |
1040 | samep = NULL; | |
1041 | diffp = NULL; | |
ae3f36de BR |
1042 | |
1043 | if (ignore_diffs && same - tlno > 0) { | |
1044 | line_blames = xcalloc(sizeof(struct blame_line_tracker), | |
1045 | same - tlno); | |
1046 | guess_line_blames(parent, target, tlno, offset, same, | |
1047 | parent_len, line_blames); | |
1048 | } | |
1049 | ||
b543bb1c JS |
1050 | while (e && e->s_lno < same) { |
1051 | struct blame_entry *next = e->next; | |
1052 | ||
1053 | /* | |
1054 | * If current record extends into sameness, need to split. | |
1055 | */ | |
1056 | if (e->s_lno + e->num_lines > same) { | |
1057 | /* | |
1058 | * Move second half to a new record to be | |
1059 | * processed by later chunks | |
1060 | */ | |
55f808fb BR |
1061 | struct blame_entry *n; |
1062 | ||
1063 | n = split_blame_at(e, same - e->s_lno, | |
1064 | blame_origin_incref(e->suspect)); | |
b543bb1c JS |
1065 | /* Push new record to samep */ |
1066 | n->next = samep; | |
1067 | samep = n; | |
1068 | } | |
ae3f36de BR |
1069 | if (ignore_diffs) { |
1070 | ignore_blame_entry(e, parent, target, &diffp, &ignoredp, | |
1071 | line_blames + e->s_lno - tlno); | |
1072 | } else { | |
1073 | e->next = diffp; | |
1074 | diffp = e; | |
1075 | } | |
b543bb1c JS |
1076 | e = next; |
1077 | } | |
ae3f36de BR |
1078 | free(line_blames); |
1079 | if (ignoredp) { | |
1080 | /* | |
1081 | * Note ignoredp is not sorted yet, and thus neither is dstq. | |
1082 | * That list must be sorted before we queue_blames(). We defer | |
1083 | * sorting until after all diff hunks are processed, so that | |
1084 | * guess_line_blames() can pick *any* line in the parent. The | |
1085 | * slight drawback is that we end up sorting all blame entries | |
1086 | * passed to the parent, including those that are unrelated to | |
1087 | * changes made by the ignored commit. | |
1088 | */ | |
1089 | **dstq = reverse_blame(ignoredp, **dstq); | |
1090 | *dstq = &ignoredp->next; | |
1091 | } | |
b543bb1c JS |
1092 | **srcq = reverse_blame(diffp, reverse_blame(samep, e)); |
1093 | /* Move across elements that are in the unblamable portion */ | |
1094 | if (diffp) | |
1095 | *srcq = &diffp->next; | |
1096 | } | |
1097 | ||
1098 | struct blame_chunk_cb_data { | |
1099 | struct blame_origin *parent; | |
ae3f36de | 1100 | struct blame_origin *target; |
b543bb1c | 1101 | long offset; |
ae3f36de | 1102 | int ignore_diffs; |
b543bb1c JS |
1103 | struct blame_entry **dstq; |
1104 | struct blame_entry **srcq; | |
1105 | }; | |
1106 | ||
1107 | /* diff chunks are from parent to target */ | |
1108 | static int blame_chunk_cb(long start_a, long count_a, | |
1109 | long start_b, long count_b, void *data) | |
1110 | { | |
1111 | struct blame_chunk_cb_data *d = data; | |
1112 | if (start_a - start_b != d->offset) | |
1113 | die("internal error in blame::blame_chunk_cb"); | |
1114 | blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, | |
ae3f36de BR |
1115 | start_b + count_b, count_a, d->parent, d->target, |
1116 | d->ignore_diffs); | |
b543bb1c JS |
1117 | d->offset = start_a + count_a - (start_b + count_b); |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | /* | |
1122 | * We are looking at the origin 'target' and aiming to pass blame | |
1123 | * for the lines it is suspected to its parent. Run diff to find | |
1124 | * which lines came from parent and pass blame for them. | |
1125 | */ | |
1126 | static void pass_blame_to_parent(struct blame_scoreboard *sb, | |
1127 | struct blame_origin *target, | |
ae3f36de | 1128 | struct blame_origin *parent, int ignore_diffs) |
b543bb1c JS |
1129 | { |
1130 | mmfile_t file_p, file_o; | |
1131 | struct blame_chunk_cb_data d; | |
1132 | struct blame_entry *newdest = NULL; | |
1133 | ||
1134 | if (!target->suspects) | |
1135 | return; /* nothing remains for this target */ | |
1136 | ||
1137 | d.parent = parent; | |
ae3f36de | 1138 | d.target = target; |
b543bb1c | 1139 | d.offset = 0; |
ae3f36de | 1140 | d.ignore_diffs = ignore_diffs; |
b543bb1c JS |
1141 | d.dstq = &newdest; d.srcq = &target->suspects; |
1142 | ||
1143 | fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); | |
1144 | fill_origin_blob(&sb->revs->diffopt, target, &file_o, &sb->num_read_blob); | |
1145 | sb->num_get_patch++; | |
1146 | ||
1147 | if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts)) | |
1148 | die("unable to generate diff (%s -> %s)", | |
1149 | oid_to_hex(&parent->commit->object.oid), | |
1150 | oid_to_hex(&target->commit->object.oid)); | |
1151 | /* The rest are the same as the parent */ | |
ae3f36de BR |
1152 | blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0, |
1153 | parent, target, 0); | |
b543bb1c | 1154 | *d.dstq = NULL; |
ae3f36de BR |
1155 | if (ignore_diffs) |
1156 | newdest = llist_mergesort(newdest, get_next_blame, | |
1157 | set_next_blame, | |
1158 | compare_blame_suspect); | |
b543bb1c JS |
1159 | queue_blames(sb, parent, newdest); |
1160 | ||
1161 | return; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * The lines in blame_entry after splitting blames many times can become | |
1166 | * very small and trivial, and at some point it becomes pointless to | |
1167 | * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any | |
1168 | * ordinary C program, and it is not worth to say it was copied from | |
1169 | * totally unrelated file in the parent. | |
1170 | * | |
1171 | * Compute how trivial the lines in the blame_entry are. | |
1172 | */ | |
1173 | unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e) | |
1174 | { | |
1175 | unsigned score; | |
1176 | const char *cp, *ep; | |
1177 | ||
1178 | if (e->score) | |
1179 | return e->score; | |
1180 | ||
1181 | score = 1; | |
1182 | cp = blame_nth_line(sb, e->lno); | |
1183 | ep = blame_nth_line(sb, e->lno + e->num_lines); | |
1184 | while (cp < ep) { | |
1185 | unsigned ch = *((unsigned char *)cp); | |
1186 | if (isalnum(ch)) | |
1187 | score++; | |
1188 | cp++; | |
1189 | } | |
1190 | e->score = score; | |
1191 | return score; | |
1192 | } | |
1193 | ||
1194 | /* | |
abeacb25 BW |
1195 | * best_so_far[] and potential[] are both a split of an existing blame_entry |
1196 | * that passes blame to the parent. Maintain best_so_far the best split so | |
1197 | * far, by comparing potential and best_so_far and copying potential into | |
b543bb1c JS |
1198 | * bst_so_far as needed. |
1199 | */ | |
1200 | static void copy_split_if_better(struct blame_scoreboard *sb, | |
1201 | struct blame_entry *best_so_far, | |
abeacb25 | 1202 | struct blame_entry *potential) |
b543bb1c JS |
1203 | { |
1204 | int i; | |
1205 | ||
abeacb25 | 1206 | if (!potential[1].suspect) |
b543bb1c JS |
1207 | return; |
1208 | if (best_so_far[1].suspect) { | |
abeacb25 BW |
1209 | if (blame_entry_score(sb, &potential[1]) < |
1210 | blame_entry_score(sb, &best_so_far[1])) | |
b543bb1c JS |
1211 | return; |
1212 | } | |
1213 | ||
1214 | for (i = 0; i < 3; i++) | |
abeacb25 | 1215 | blame_origin_incref(potential[i].suspect); |
b543bb1c | 1216 | decref_split(best_so_far); |
abeacb25 | 1217 | memcpy(best_so_far, potential, sizeof(struct blame_entry[3])); |
b543bb1c JS |
1218 | } |
1219 | ||
1220 | /* | |
1221 | * We are looking at a part of the final image represented by | |
1222 | * ent (tlno and same are offset by ent->s_lno). | |
1223 | * tlno is where we are looking at in the final image. | |
1224 | * up to (but not including) same match preimage. | |
1225 | * plno is where we are looking at in the preimage. | |
1226 | * | |
1227 | * <-------------- final image ----------------------> | |
1228 | * <------ent------> | |
1229 | * ^tlno ^same | |
1230 | * <---------preimage-----> | |
1231 | * ^plno | |
1232 | * | |
1233 | * All line numbers are 0-based. | |
1234 | */ | |
1235 | static void handle_split(struct blame_scoreboard *sb, | |
1236 | struct blame_entry *ent, | |
1237 | int tlno, int plno, int same, | |
1238 | struct blame_origin *parent, | |
1239 | struct blame_entry *split) | |
1240 | { | |
1241 | if (ent->num_lines <= tlno) | |
1242 | return; | |
1243 | if (tlno < same) { | |
abeacb25 | 1244 | struct blame_entry potential[3]; |
b543bb1c JS |
1245 | tlno += ent->s_lno; |
1246 | same += ent->s_lno; | |
abeacb25 BW |
1247 | split_overlap(potential, ent, tlno, plno, same, parent); |
1248 | copy_split_if_better(sb, split, potential); | |
1249 | decref_split(potential); | |
b543bb1c JS |
1250 | } |
1251 | } | |
1252 | ||
1253 | struct handle_split_cb_data { | |
1254 | struct blame_scoreboard *sb; | |
1255 | struct blame_entry *ent; | |
1256 | struct blame_origin *parent; | |
1257 | struct blame_entry *split; | |
1258 | long plno; | |
1259 | long tlno; | |
1260 | }; | |
1261 | ||
1262 | static int handle_split_cb(long start_a, long count_a, | |
1263 | long start_b, long count_b, void *data) | |
1264 | { | |
1265 | struct handle_split_cb_data *d = data; | |
1266 | handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent, | |
1267 | d->split); | |
1268 | d->plno = start_a + count_a; | |
1269 | d->tlno = start_b + count_b; | |
1270 | return 0; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Find the lines from parent that are the same as ent so that | |
1275 | * we can pass blames to it. file_p has the blob contents for | |
1276 | * the parent. | |
1277 | */ | |
1278 | static void find_copy_in_blob(struct blame_scoreboard *sb, | |
1279 | struct blame_entry *ent, | |
1280 | struct blame_origin *parent, | |
1281 | struct blame_entry *split, | |
1282 | mmfile_t *file_p) | |
1283 | { | |
1284 | const char *cp; | |
1285 | mmfile_t file_o; | |
1286 | struct handle_split_cb_data d; | |
1287 | ||
1288 | memset(&d, 0, sizeof(d)); | |
1289 | d.sb = sb; d.ent = ent; d.parent = parent; d.split = split; | |
1290 | /* | |
1291 | * Prepare mmfile that contains only the lines in ent. | |
1292 | */ | |
1293 | cp = blame_nth_line(sb, ent->lno); | |
1294 | file_o.ptr = (char *) cp; | |
1295 | file_o.size = blame_nth_line(sb, ent->lno + ent->num_lines) - cp; | |
1296 | ||
1297 | /* | |
1298 | * file_o is a part of final image we are annotating. | |
1299 | * file_p partially may match that image. | |
1300 | */ | |
1301 | memset(split, 0, sizeof(struct blame_entry [3])); | |
1302 | if (diff_hunks(file_p, &file_o, handle_split_cb, &d, sb->xdl_opts)) | |
1303 | die("unable to generate diff (%s)", | |
1304 | oid_to_hex(&parent->commit->object.oid)); | |
1305 | /* remainder, if any, all match the preimage */ | |
1306 | handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); | |
1307 | } | |
1308 | ||
1309 | /* Move all blame entries from list *source that have a score smaller | |
1310 | * than score_min to the front of list *small. | |
1311 | * Returns a pointer to the link pointing to the old head of the small list. | |
1312 | */ | |
1313 | ||
1314 | static struct blame_entry **filter_small(struct blame_scoreboard *sb, | |
1315 | struct blame_entry **small, | |
1316 | struct blame_entry **source, | |
1317 | unsigned score_min) | |
1318 | { | |
1319 | struct blame_entry *p = *source; | |
1320 | struct blame_entry *oldsmall = *small; | |
1321 | while (p) { | |
1322 | if (blame_entry_score(sb, p) <= score_min) { | |
1323 | *small = p; | |
1324 | small = &p->next; | |
1325 | p = *small; | |
1326 | } else { | |
1327 | *source = p; | |
1328 | source = &p->next; | |
1329 | p = *source; | |
1330 | } | |
1331 | } | |
1332 | *small = oldsmall; | |
1333 | *source = NULL; | |
1334 | return small; | |
1335 | } | |
1336 | ||
1337 | /* | |
1338 | * See if lines currently target is suspected for can be attributed to | |
1339 | * parent. | |
1340 | */ | |
1341 | static void find_move_in_parent(struct blame_scoreboard *sb, | |
1342 | struct blame_entry ***blamed, | |
1343 | struct blame_entry **toosmall, | |
1344 | struct blame_origin *target, | |
1345 | struct blame_origin *parent) | |
1346 | { | |
1347 | struct blame_entry *e, split[3]; | |
1348 | struct blame_entry *unblamed = target->suspects; | |
1349 | struct blame_entry *leftover = NULL; | |
1350 | mmfile_t file_p; | |
1351 | ||
1352 | if (!unblamed) | |
1353 | return; /* nothing remains for this target */ | |
1354 | ||
1355 | fill_origin_blob(&sb->revs->diffopt, parent, &file_p, &sb->num_read_blob); | |
1356 | if (!file_p.ptr) | |
1357 | return; | |
1358 | ||
1359 | /* At each iteration, unblamed has a NULL-terminated list of | |
1360 | * entries that have not yet been tested for blame. leftover | |
1361 | * contains the reversed list of entries that have been tested | |
1362 | * without being assignable to the parent. | |
1363 | */ | |
1364 | do { | |
1365 | struct blame_entry **unblamedtail = &unblamed; | |
1366 | struct blame_entry *next; | |
1367 | for (e = unblamed; e; e = next) { | |
1368 | next = e->next; | |
1369 | find_copy_in_blob(sb, e, parent, split, &file_p); | |
1370 | if (split[1].suspect && | |
1371 | sb->move_score < blame_entry_score(sb, &split[1])) { | |
1372 | split_blame(blamed, &unblamedtail, split, e); | |
1373 | } else { | |
1374 | e->next = leftover; | |
1375 | leftover = e; | |
1376 | } | |
1377 | decref_split(split); | |
1378 | } | |
1379 | *unblamedtail = NULL; | |
1380 | toosmall = filter_small(sb, toosmall, &unblamed, sb->move_score); | |
1381 | } while (unblamed); | |
1382 | target->suspects = reverse_blame(leftover, NULL); | |
1383 | } | |
1384 | ||
1385 | struct blame_list { | |
1386 | struct blame_entry *ent; | |
1387 | struct blame_entry split[3]; | |
1388 | }; | |
1389 | ||
1390 | /* | |
1391 | * Count the number of entries the target is suspected for, | |
1392 | * and prepare a list of entry and the best split. | |
1393 | */ | |
1394 | static struct blame_list *setup_blame_list(struct blame_entry *unblamed, | |
1395 | int *num_ents_p) | |
1396 | { | |
1397 | struct blame_entry *e; | |
1398 | int num_ents, i; | |
1399 | struct blame_list *blame_list = NULL; | |
1400 | ||
1401 | for (e = unblamed, num_ents = 0; e; e = e->next) | |
1402 | num_ents++; | |
1403 | if (num_ents) { | |
1404 | blame_list = xcalloc(num_ents, sizeof(struct blame_list)); | |
1405 | for (e = unblamed, i = 0; e; e = e->next) | |
1406 | blame_list[i++].ent = e; | |
1407 | } | |
1408 | *num_ents_p = num_ents; | |
1409 | return blame_list; | |
1410 | } | |
1411 | ||
1412 | /* | |
1413 | * For lines target is suspected for, see if we can find code movement | |
1414 | * across file boundary from the parent commit. porigin is the path | |
1415 | * in the parent we already tried. | |
1416 | */ | |
1417 | static void find_copy_in_parent(struct blame_scoreboard *sb, | |
1418 | struct blame_entry ***blamed, | |
1419 | struct blame_entry **toosmall, | |
1420 | struct blame_origin *target, | |
1421 | struct commit *parent, | |
1422 | struct blame_origin *porigin, | |
1423 | int opt) | |
1424 | { | |
1425 | struct diff_options diff_opts; | |
1426 | int i, j; | |
1427 | struct blame_list *blame_list; | |
1428 | int num_ents; | |
1429 | struct blame_entry *unblamed = target->suspects; | |
1430 | struct blame_entry *leftover = NULL; | |
1431 | ||
1432 | if (!unblamed) | |
1433 | return; /* nothing remains for this target */ | |
1434 | ||
e6757652 | 1435 | repo_diff_setup(sb->repo, &diff_opts); |
0d1e0e78 | 1436 | diff_opts.flags.recursive = 1; |
b543bb1c JS |
1437 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
1438 | ||
1439 | diff_setup_done(&diff_opts); | |
1440 | ||
1441 | /* Try "find copies harder" on new path if requested; | |
1442 | * we do not want to use diffcore_rename() actually to | |
1443 | * match things up; find_copies_harder is set only to | |
a6f38c10 | 1444 | * force diff_tree_oid() to feed all filepairs to diff_queue, |
b543bb1c JS |
1445 | * and this code needs to be after diff_setup_done(), which |
1446 | * usually makes find-copies-harder imply copy detection. | |
1447 | */ | |
1448 | if ((opt & PICKAXE_BLAME_COPY_HARDEST) | |
1449 | || ((opt & PICKAXE_BLAME_COPY_HARDER) | |
1450 | && (!porigin || strcmp(target->path, porigin->path)))) | |
0d1e0e78 | 1451 | diff_opts.flags.find_copies_harder = 1; |
b543bb1c JS |
1452 | |
1453 | if (is_null_oid(&target->commit->object.oid)) | |
2e27bd77 | 1454 | do_diff_cache(get_commit_tree_oid(parent), &diff_opts); |
b543bb1c | 1455 | else |
2e27bd77 DS |
1456 | diff_tree_oid(get_commit_tree_oid(parent), |
1457 | get_commit_tree_oid(target->commit), | |
a6f38c10 | 1458 | "", &diff_opts); |
b543bb1c | 1459 | |
0d1e0e78 | 1460 | if (!diff_opts.flags.find_copies_harder) |
b543bb1c JS |
1461 | diffcore_std(&diff_opts); |
1462 | ||
1463 | do { | |
1464 | struct blame_entry **unblamedtail = &unblamed; | |
1465 | blame_list = setup_blame_list(unblamed, &num_ents); | |
1466 | ||
1467 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
1468 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
1469 | struct blame_origin *norigin; | |
1470 | mmfile_t file_p; | |
abeacb25 | 1471 | struct blame_entry potential[3]; |
b543bb1c JS |
1472 | |
1473 | if (!DIFF_FILE_VALID(p->one)) | |
1474 | continue; /* does not exist in parent */ | |
1475 | if (S_ISGITLINK(p->one->mode)) | |
1476 | continue; /* ignore git links */ | |
1477 | if (porigin && !strcmp(p->one->path, porigin->path)) | |
1478 | /* find_move already dealt with this path */ | |
1479 | continue; | |
1480 | ||
1481 | norigin = get_origin(parent, p->one->path); | |
1482 | oidcpy(&norigin->blob_oid, &p->one->oid); | |
1483 | norigin->mode = p->one->mode; | |
1484 | fill_origin_blob(&sb->revs->diffopt, norigin, &file_p, &sb->num_read_blob); | |
1485 | if (!file_p.ptr) | |
1486 | continue; | |
1487 | ||
1488 | for (j = 0; j < num_ents; j++) { | |
1489 | find_copy_in_blob(sb, blame_list[j].ent, | |
abeacb25 | 1490 | norigin, potential, &file_p); |
b543bb1c | 1491 | copy_split_if_better(sb, blame_list[j].split, |
abeacb25 BW |
1492 | potential); |
1493 | decref_split(potential); | |
b543bb1c JS |
1494 | } |
1495 | blame_origin_decref(norigin); | |
1496 | } | |
1497 | ||
1498 | for (j = 0; j < num_ents; j++) { | |
1499 | struct blame_entry *split = blame_list[j].split; | |
1500 | if (split[1].suspect && | |
1501 | sb->copy_score < blame_entry_score(sb, &split[1])) { | |
1502 | split_blame(blamed, &unblamedtail, split, | |
1503 | blame_list[j].ent); | |
1504 | } else { | |
1505 | blame_list[j].ent->next = leftover; | |
1506 | leftover = blame_list[j].ent; | |
1507 | } | |
1508 | decref_split(split); | |
1509 | } | |
1510 | free(blame_list); | |
1511 | *unblamedtail = NULL; | |
1512 | toosmall = filter_small(sb, toosmall, &unblamed, sb->copy_score); | |
1513 | } while (unblamed); | |
1514 | target->suspects = reverse_blame(leftover, NULL); | |
1515 | diff_flush(&diff_opts); | |
1516 | clear_pathspec(&diff_opts.pathspec); | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * The blobs of origin and porigin exactly match, so everything | |
1521 | * origin is suspected for can be blamed on the parent. | |
1522 | */ | |
1523 | static void pass_whole_blame(struct blame_scoreboard *sb, | |
1524 | struct blame_origin *origin, struct blame_origin *porigin) | |
1525 | { | |
1526 | struct blame_entry *e, *suspects; | |
1527 | ||
1528 | if (!porigin->file.ptr && origin->file.ptr) { | |
1529 | /* Steal its file */ | |
1530 | porigin->file = origin->file; | |
1531 | origin->file.ptr = NULL; | |
1532 | } | |
1533 | suspects = origin->suspects; | |
1534 | origin->suspects = NULL; | |
1535 | for (e = suspects; e; e = e->next) { | |
1536 | blame_origin_incref(porigin); | |
1537 | blame_origin_decref(e->suspect); | |
1538 | e->suspect = porigin; | |
1539 | } | |
1540 | queue_blames(sb, porigin, suspects); | |
1541 | } | |
1542 | ||
1543 | /* | |
1544 | * We pass blame from the current commit to its parents. We keep saying | |
1545 | * "parent" (and "porigin"), but what we mean is to find scapegoat to | |
1546 | * exonerate ourselves. | |
1547 | */ | |
1548 | static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit, | |
1549 | int reverse) | |
1550 | { | |
1551 | if (!reverse) { | |
1552 | if (revs->first_parent_only && | |
1553 | commit->parents && | |
1554 | commit->parents->next) { | |
1555 | free_commit_list(commit->parents->next); | |
1556 | commit->parents->next = NULL; | |
1557 | } | |
1558 | return commit->parents; | |
1559 | } | |
1560 | return lookup_decoration(&revs->children, &commit->object); | |
1561 | } | |
1562 | ||
1563 | static int num_scapegoats(struct rev_info *revs, struct commit *commit, int reverse) | |
1564 | { | |
1565 | struct commit_list *l = first_scapegoat(revs, commit, reverse); | |
1566 | return commit_list_count(l); | |
1567 | } | |
1568 | ||
1569 | /* Distribute collected unsorted blames to the respected sorted lists | |
1570 | * in the various origins. | |
1571 | */ | |
1572 | static void distribute_blame(struct blame_scoreboard *sb, struct blame_entry *blamed) | |
1573 | { | |
1574 | blamed = llist_mergesort(blamed, get_next_blame, set_next_blame, | |
1575 | compare_blame_suspect); | |
1576 | while (blamed) | |
1577 | { | |
1578 | struct blame_origin *porigin = blamed->suspect; | |
1579 | struct blame_entry *suspects = NULL; | |
1580 | do { | |
1581 | struct blame_entry *next = blamed->next; | |
1582 | blamed->next = suspects; | |
1583 | suspects = blamed; | |
1584 | blamed = next; | |
1585 | } while (blamed && blamed->suspect == porigin); | |
1586 | suspects = reverse_blame(suspects, NULL); | |
1587 | queue_blames(sb, porigin, suspects); | |
1588 | } | |
1589 | } | |
1590 | ||
1591 | #define MAXSG 16 | |
1592 | ||
1593 | static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin, int opt) | |
1594 | { | |
1595 | struct rev_info *revs = sb->revs; | |
1596 | int i, pass, num_sg; | |
1597 | struct commit *commit = origin->commit; | |
1598 | struct commit_list *sg; | |
1599 | struct blame_origin *sg_buf[MAXSG]; | |
1600 | struct blame_origin *porigin, **sg_origin = sg_buf; | |
1601 | struct blame_entry *toosmall = NULL; | |
1602 | struct blame_entry *blames, **blametail = &blames; | |
1603 | ||
1604 | num_sg = num_scapegoats(revs, commit, sb->reverse); | |
1605 | if (!num_sg) | |
1606 | goto finish; | |
1607 | else if (num_sg < ARRAY_SIZE(sg_buf)) | |
1608 | memset(sg_buf, 0, sizeof(sg_buf)); | |
1609 | else | |
1610 | sg_origin = xcalloc(num_sg, sizeof(*sg_origin)); | |
1611 | ||
1612 | /* | |
1613 | * The first pass looks for unrenamed path to optimize for | |
1614 | * common cases, then we look for renames in the second pass. | |
1615 | */ | |
1616 | for (pass = 0; pass < 2 - sb->no_whole_file_rename; pass++) { | |
e6757652 | 1617 | struct blame_origin *(*find)(struct repository *, struct commit *, struct blame_origin *); |
b543bb1c JS |
1618 | find = pass ? find_rename : find_origin; |
1619 | ||
1620 | for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse); | |
1621 | i < num_sg && sg; | |
1622 | sg = sg->next, i++) { | |
1623 | struct commit *p = sg->item; | |
1624 | int j, same; | |
1625 | ||
1626 | if (sg_origin[i]) | |
1627 | continue; | |
1628 | if (parse_commit(p)) | |
1629 | continue; | |
e6757652 | 1630 | porigin = find(sb->repo, p, origin); |
b543bb1c JS |
1631 | if (!porigin) |
1632 | continue; | |
4a7e27e9 | 1633 | if (oideq(&porigin->blob_oid, &origin->blob_oid)) { |
b543bb1c JS |
1634 | pass_whole_blame(sb, origin, porigin); |
1635 | blame_origin_decref(porigin); | |
1636 | goto finish; | |
1637 | } | |
1638 | for (j = same = 0; j < i; j++) | |
1639 | if (sg_origin[j] && | |
4a7e27e9 | 1640 | oideq(&sg_origin[j]->blob_oid, &porigin->blob_oid)) { |
b543bb1c JS |
1641 | same = 1; |
1642 | break; | |
1643 | } | |
1644 | if (!same) | |
1645 | sg_origin[i] = porigin; | |
1646 | else | |
1647 | blame_origin_decref(porigin); | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | sb->num_commits++; | |
1652 | for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse); | |
1653 | i < num_sg && sg; | |
1654 | sg = sg->next, i++) { | |
1655 | struct blame_origin *porigin = sg_origin[i]; | |
1656 | if (!porigin) | |
1657 | continue; | |
1658 | if (!origin->previous) { | |
1659 | blame_origin_incref(porigin); | |
1660 | origin->previous = porigin; | |
1661 | } | |
ae3f36de | 1662 | pass_blame_to_parent(sb, origin, porigin, 0); |
b543bb1c JS |
1663 | if (!origin->suspects) |
1664 | goto finish; | |
1665 | } | |
1666 | ||
ae3f36de BR |
1667 | /* |
1668 | * Pass remaining suspects for ignored commits to their parents. | |
1669 | */ | |
1670 | if (oidset_contains(&sb->ignore_list, &commit->object.oid)) { | |
1671 | for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse); | |
1672 | i < num_sg && sg; | |
1673 | sg = sg->next, i++) { | |
1674 | struct blame_origin *porigin = sg_origin[i]; | |
1675 | ||
1676 | if (!porigin) | |
1677 | continue; | |
1678 | pass_blame_to_parent(sb, origin, porigin, 1); | |
1679 | if (!origin->suspects) | |
1680 | goto finish; | |
1681 | } | |
1682 | } | |
1683 | ||
b543bb1c JS |
1684 | /* |
1685 | * Optionally find moves in parents' files. | |
1686 | */ | |
1687 | if (opt & PICKAXE_BLAME_MOVE) { | |
1688 | filter_small(sb, &toosmall, &origin->suspects, sb->move_score); | |
1689 | if (origin->suspects) { | |
1690 | for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse); | |
1691 | i < num_sg && sg; | |
1692 | sg = sg->next, i++) { | |
1693 | struct blame_origin *porigin = sg_origin[i]; | |
1694 | if (!porigin) | |
1695 | continue; | |
1696 | find_move_in_parent(sb, &blametail, &toosmall, origin, porigin); | |
1697 | if (!origin->suspects) | |
1698 | break; | |
1699 | } | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | /* | |
1704 | * Optionally find copies from parents' files. | |
1705 | */ | |
1706 | if (opt & PICKAXE_BLAME_COPY) { | |
1707 | if (sb->copy_score > sb->move_score) | |
1708 | filter_small(sb, &toosmall, &origin->suspects, sb->copy_score); | |
1709 | else if (sb->copy_score < sb->move_score) { | |
1710 | origin->suspects = blame_merge(origin->suspects, toosmall); | |
1711 | toosmall = NULL; | |
1712 | filter_small(sb, &toosmall, &origin->suspects, sb->copy_score); | |
1713 | } | |
1714 | if (!origin->suspects) | |
1715 | goto finish; | |
1716 | ||
1717 | for (i = 0, sg = first_scapegoat(revs, commit, sb->reverse); | |
1718 | i < num_sg && sg; | |
1719 | sg = sg->next, i++) { | |
1720 | struct blame_origin *porigin = sg_origin[i]; | |
1721 | find_copy_in_parent(sb, &blametail, &toosmall, | |
1722 | origin, sg->item, porigin, opt); | |
1723 | if (!origin->suspects) | |
1724 | goto finish; | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | finish: | |
1729 | *blametail = NULL; | |
1730 | distribute_blame(sb, blames); | |
1731 | /* | |
1732 | * prepend toosmall to origin->suspects | |
1733 | * | |
1734 | * There is no point in sorting: this ends up on a big | |
1735 | * unsorted list in the caller anyway. | |
1736 | */ | |
1737 | if (toosmall) { | |
1738 | struct blame_entry **tail = &toosmall; | |
1739 | while (*tail) | |
1740 | tail = &(*tail)->next; | |
1741 | *tail = origin->suspects; | |
1742 | origin->suspects = toosmall; | |
1743 | } | |
1744 | for (i = 0; i < num_sg; i++) { | |
1745 | if (sg_origin[i]) { | |
1746 | drop_origin_blob(sg_origin[i]); | |
1747 | blame_origin_decref(sg_origin[i]); | |
1748 | } | |
1749 | } | |
1750 | drop_origin_blob(origin); | |
1751 | if (sg_buf != sg_origin) | |
1752 | free(sg_origin); | |
1753 | } | |
1754 | ||
1755 | /* | |
1756 | * The main loop -- while we have blobs with lines whose true origin | |
1757 | * is still unknown, pick one blob, and allow its lines to pass blames | |
1758 | * to its parents. */ | |
1759 | void assign_blame(struct blame_scoreboard *sb, int opt) | |
1760 | { | |
1761 | struct rev_info *revs = sb->revs; | |
1762 | struct commit *commit = prio_queue_get(&sb->commits); | |
1763 | ||
1764 | while (commit) { | |
1765 | struct blame_entry *ent; | |
4e0df4e6 | 1766 | struct blame_origin *suspect = get_blame_suspects(commit); |
b543bb1c JS |
1767 | |
1768 | /* find one suspect to break down */ | |
1769 | while (suspect && !suspect->suspects) | |
1770 | suspect = suspect->next; | |
1771 | ||
1772 | if (!suspect) { | |
1773 | commit = prio_queue_get(&sb->commits); | |
1774 | continue; | |
1775 | } | |
1776 | ||
1777 | assert(commit == suspect->commit); | |
1778 | ||
1779 | /* | |
1780 | * We will use this suspect later in the loop, | |
1781 | * so hold onto it in the meantime. | |
1782 | */ | |
1783 | blame_origin_incref(suspect); | |
1784 | parse_commit(commit); | |
1785 | if (sb->reverse || | |
1786 | (!(commit->object.flags & UNINTERESTING) && | |
1787 | !(revs->max_age != -1 && commit->date < revs->max_age))) | |
1788 | pass_blame(sb, suspect, opt); | |
1789 | else { | |
1790 | commit->object.flags |= UNINTERESTING; | |
1791 | if (commit->object.parsed) | |
1792 | mark_parents_uninteresting(commit); | |
1793 | } | |
1794 | /* treat root commit as boundary */ | |
1795 | if (!commit->parents && !sb->show_root) | |
1796 | commit->object.flags |= UNINTERESTING; | |
1797 | ||
1798 | /* Take responsibility for the remaining entries */ | |
1799 | ent = suspect->suspects; | |
1800 | if (ent) { | |
1801 | suspect->guilty = 1; | |
1802 | for (;;) { | |
1803 | struct blame_entry *next = ent->next; | |
1804 | if (sb->found_guilty_entry) | |
1805 | sb->found_guilty_entry(ent, sb->found_guilty_entry_data); | |
1806 | if (next) { | |
1807 | ent = next; | |
1808 | continue; | |
1809 | } | |
1810 | ent->next = sb->ent; | |
1811 | sb->ent = suspect->suspects; | |
1812 | suspect->suspects = NULL; | |
1813 | break; | |
1814 | } | |
1815 | } | |
1816 | blame_origin_decref(suspect); | |
1817 | ||
1818 | if (sb->debug) /* sanity */ | |
1819 | sanity_check_refcnt(sb); | |
1820 | } | |
1821 | } | |
09002f1b JS |
1822 | |
1823 | static const char *get_next_line(const char *start, const char *end) | |
1824 | { | |
1825 | const char *nl = memchr(start, '\n', end - start); | |
1826 | return nl ? nl + 1 : end; | |
1827 | } | |
1828 | ||
1829 | /* | |
1830 | * To allow quick access to the contents of nth line in the | |
1831 | * final image, prepare an index in the scoreboard. | |
1832 | */ | |
1833 | static int prepare_lines(struct blame_scoreboard *sb) | |
1834 | { | |
1835 | const char *buf = sb->final_buf; | |
1836 | unsigned long len = sb->final_buf_size; | |
1837 | const char *end = buf + len; | |
1838 | const char *p; | |
1839 | int *lineno; | |
1840 | int num = 0; | |
1841 | ||
1842 | for (p = buf; p < end; p = get_next_line(p, end)) | |
1843 | num++; | |
1844 | ||
1845 | ALLOC_ARRAY(sb->lineno, num + 1); | |
1846 | lineno = sb->lineno; | |
1847 | ||
1848 | for (p = buf; p < end; p = get_next_line(p, end)) | |
1849 | *lineno++ = p - buf; | |
1850 | ||
1851 | *lineno = len; | |
1852 | ||
1853 | sb->num_lines = num; | |
1854 | return sb->num_lines; | |
1855 | } | |
1856 | ||
1857 | static struct commit *find_single_final(struct rev_info *revs, | |
1858 | const char **name_p) | |
1859 | { | |
1860 | int i; | |
1861 | struct commit *found = NULL; | |
1862 | const char *name = NULL; | |
1863 | ||
1864 | for (i = 0; i < revs->pending.nr; i++) { | |
1865 | struct object *obj = revs->pending.objects[i].item; | |
1866 | if (obj->flags & UNINTERESTING) | |
1867 | continue; | |
fb998eae | 1868 | obj = deref_tag(revs->repo, obj, NULL, 0); |
09002f1b JS |
1869 | if (obj->type != OBJ_COMMIT) |
1870 | die("Non commit %s?", revs->pending.objects[i].name); | |
1871 | if (found) | |
1872 | die("More than one commit to dig from %s and %s?", | |
1873 | revs->pending.objects[i].name, name); | |
1874 | found = (struct commit *)obj; | |
1875 | name = revs->pending.objects[i].name; | |
1876 | } | |
1877 | if (name_p) | |
9e7d8a9b | 1878 | *name_p = xstrdup_or_null(name); |
09002f1b JS |
1879 | return found; |
1880 | } | |
1881 | ||
1882 | static struct commit *dwim_reverse_initial(struct rev_info *revs, | |
1883 | const char **name_p) | |
1884 | { | |
1885 | /* | |
1886 | * DWIM "git blame --reverse ONE -- PATH" as | |
1887 | * "git blame --reverse ONE..HEAD -- PATH" but only do so | |
1888 | * when it makes sense. | |
1889 | */ | |
1890 | struct object *obj; | |
1891 | struct commit *head_commit; | |
583c6a22 | 1892 | struct object_id head_oid; |
09002f1b JS |
1893 | |
1894 | if (revs->pending.nr != 1) | |
1895 | return NULL; | |
1896 | ||
1897 | /* Is that sole rev a committish? */ | |
1898 | obj = revs->pending.objects[0].item; | |
fb998eae | 1899 | obj = deref_tag(revs->repo, obj, NULL, 0); |
09002f1b JS |
1900 | if (obj->type != OBJ_COMMIT) |
1901 | return NULL; | |
1902 | ||
1903 | /* Do we have HEAD? */ | |
49e61479 | 1904 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
09002f1b | 1905 | return NULL; |
fb998eae | 1906 | head_commit = lookup_commit_reference_gently(revs->repo, |
21e1ee8f | 1907 | &head_oid, 1); |
09002f1b JS |
1908 | if (!head_commit) |
1909 | return NULL; | |
1910 | ||
1911 | /* Turn "ONE" into "ONE..HEAD" then */ | |
1912 | obj->flags |= UNINTERESTING; | |
1913 | add_pending_object(revs, &head_commit->object, "HEAD"); | |
1914 | ||
1915 | if (name_p) | |
1916 | *name_p = revs->pending.objects[0].name; | |
1917 | return (struct commit *)obj; | |
1918 | } | |
1919 | ||
1920 | static struct commit *find_single_initial(struct rev_info *revs, | |
1921 | const char **name_p) | |
1922 | { | |
1923 | int i; | |
1924 | struct commit *found = NULL; | |
1925 | const char *name = NULL; | |
1926 | ||
1927 | /* | |
1928 | * There must be one and only one negative commit, and it must be | |
1929 | * the boundary. | |
1930 | */ | |
1931 | for (i = 0; i < revs->pending.nr; i++) { | |
1932 | struct object *obj = revs->pending.objects[i].item; | |
1933 | if (!(obj->flags & UNINTERESTING)) | |
1934 | continue; | |
fb998eae | 1935 | obj = deref_tag(revs->repo, obj, NULL, 0); |
09002f1b JS |
1936 | if (obj->type != OBJ_COMMIT) |
1937 | die("Non commit %s?", revs->pending.objects[i].name); | |
1938 | if (found) | |
1939 | die("More than one commit to dig up from, %s and %s?", | |
1940 | revs->pending.objects[i].name, name); | |
1941 | found = (struct commit *) obj; | |
1942 | name = revs->pending.objects[i].name; | |
1943 | } | |
1944 | ||
1945 | if (!name) | |
1946 | found = dwim_reverse_initial(revs, &name); | |
1947 | if (!name) | |
1948 | die("No commit to dig up from?"); | |
1949 | ||
1950 | if (name_p) | |
9e7d8a9b | 1951 | *name_p = xstrdup(name); |
09002f1b JS |
1952 | return found; |
1953 | } | |
1954 | ||
1955 | void init_scoreboard(struct blame_scoreboard *sb) | |
1956 | { | |
1957 | memset(sb, 0, sizeof(struct blame_scoreboard)); | |
1958 | sb->move_score = BLAME_DEFAULT_MOVE_SCORE; | |
1959 | sb->copy_score = BLAME_DEFAULT_COPY_SCORE; | |
1960 | } | |
1961 | ||
ecbbc0a5 NTND |
1962 | void setup_scoreboard(struct blame_scoreboard *sb, |
1963 | const char *path, | |
1964 | struct blame_origin **orig) | |
09002f1b JS |
1965 | { |
1966 | const char *final_commit_name = NULL; | |
1967 | struct blame_origin *o; | |
1968 | struct commit *final_commit = NULL; | |
1969 | enum object_type type; | |
1970 | ||
4e0df4e6 NTND |
1971 | init_blame_suspects(&blame_suspects); |
1972 | ||
09002f1b JS |
1973 | if (sb->reverse && sb->contents_from) |
1974 | die(_("--contents and --reverse do not blend well.")); | |
1975 | ||
ecbbc0a5 NTND |
1976 | if (!sb->repo) |
1977 | BUG("repo is NULL"); | |
1978 | ||
09002f1b JS |
1979 | if (!sb->reverse) { |
1980 | sb->final = find_single_final(sb->revs, &final_commit_name); | |
1981 | sb->commits.compare = compare_commits_by_commit_date; | |
1982 | } else { | |
1983 | sb->final = find_single_initial(sb->revs, &final_commit_name); | |
1984 | sb->commits.compare = compare_commits_by_reverse_commit_date; | |
1985 | } | |
1986 | ||
1987 | if (sb->final && sb->contents_from) | |
1988 | die(_("cannot use --contents with final commit object name")); | |
1989 | ||
1990 | if (sb->reverse && sb->revs->first_parent_only) | |
1991 | sb->revs->children.name = NULL; | |
1992 | ||
1993 | if (!sb->final) { | |
1994 | /* | |
1995 | * "--not A B -- path" without anything positive; | |
1996 | * do not default to HEAD, but use the working tree | |
1997 | * or "--contents". | |
1998 | */ | |
1999 | setup_work_tree(); | |
ecbbc0a5 NTND |
2000 | sb->final = fake_working_tree_commit(sb->repo, |
2001 | &sb->revs->diffopt, | |
09002f1b JS |
2002 | path, sb->contents_from); |
2003 | add_pending_object(sb->revs, &(sb->final->object), ":"); | |
2004 | } | |
2005 | ||
2006 | if (sb->reverse && sb->revs->first_parent_only) { | |
2007 | final_commit = find_single_final(sb->revs, NULL); | |
2008 | if (!final_commit) | |
2009 | die(_("--reverse and --first-parent together require specified latest commit")); | |
2010 | } | |
2011 | ||
2012 | /* | |
2013 | * If we have bottom, this will mark the ancestors of the | |
2014 | * bottom commits we would reach while traversing as | |
2015 | * uninteresting. | |
2016 | */ | |
2017 | if (prepare_revision_walk(sb->revs)) | |
2018 | die(_("revision walk setup failed")); | |
2019 | ||
2020 | if (sb->reverse && sb->revs->first_parent_only) { | |
2021 | struct commit *c = final_commit; | |
2022 | ||
2023 | sb->revs->children.name = "children"; | |
2024 | while (c->parents && | |
9001dc2a | 2025 | !oideq(&c->object.oid, &sb->final->object.oid)) { |
09002f1b JS |
2026 | struct commit_list *l = xcalloc(1, sizeof(*l)); |
2027 | ||
2028 | l->item = c; | |
2029 | if (add_decoration(&sb->revs->children, | |
2030 | &c->parents->item->object, l)) | |
033abf97 | 2031 | BUG("not unique item in first-parent chain"); |
09002f1b JS |
2032 | c = c->parents->item; |
2033 | } | |
2034 | ||
9001dc2a | 2035 | if (!oideq(&c->object.oid, &sb->final->object.oid)) |
09002f1b JS |
2036 | die(_("--reverse --first-parent together require range along first-parent chain")); |
2037 | } | |
2038 | ||
2039 | if (is_null_oid(&sb->final->object.oid)) { | |
4e0df4e6 | 2040 | o = get_blame_suspects(sb->final); |
09002f1b JS |
2041 | sb->final_buf = xmemdupz(o->file.ptr, o->file.size); |
2042 | sb->final_buf_size = o->file.size; | |
2043 | } | |
2044 | else { | |
2045 | o = get_origin(sb->final, path); | |
ecbbc0a5 | 2046 | if (fill_blob_sha1_and_mode(sb->repo, o)) |
09002f1b JS |
2047 | die(_("no such path %s in %s"), path, final_commit_name); |
2048 | ||
0d1e0e78 | 2049 | if (sb->revs->diffopt.flags.allow_textconv && |
6afaf807 | 2050 | textconv_object(sb->repo, path, o->mode, &o->blob_oid, 1, (char **) &sb->final_buf, |
09002f1b JS |
2051 | &sb->final_buf_size)) |
2052 | ; | |
2053 | else | |
b4f5aca4 | 2054 | sb->final_buf = read_object_file(&o->blob_oid, &type, |
2055 | &sb->final_buf_size); | |
09002f1b JS |
2056 | |
2057 | if (!sb->final_buf) | |
2058 | die(_("cannot read blob %s for path %s"), | |
2059 | oid_to_hex(&o->blob_oid), | |
2060 | path); | |
2061 | } | |
2062 | sb->num_read_blob++; | |
2063 | prepare_lines(sb); | |
2064 | ||
2065 | if (orig) | |
2066 | *orig = o; | |
9e7d8a9b SG |
2067 | |
2068 | free((char *)final_commit_name); | |
09002f1b | 2069 | } |
bd481de7 JS |
2070 | |
2071 | ||
2072 | ||
2073 | struct blame_entry *blame_entry_prepend(struct blame_entry *head, | |
2074 | long start, long end, | |
2075 | struct blame_origin *o) | |
2076 | { | |
2077 | struct blame_entry *new_head = xcalloc(1, sizeof(struct blame_entry)); | |
2078 | new_head->lno = start; | |
2079 | new_head->num_lines = end - start; | |
2080 | new_head->suspect = o; | |
2081 | new_head->s_lno = start; | |
2082 | new_head->next = head; | |
2083 | blame_origin_incref(o); | |
2084 | return new_head; | |
2085 | } |