]>
Commit | Line | Data |
---|---|---|
cee7f245 | 1 | /* |
31653c1a | 2 | * Blame |
cee7f245 | 3 | * |
7e6ac6e4 DK |
4 | * Copyright (c) 2006, 2014 by its authors |
5 | * See COPYING for licensing conditions | |
cee7f245 JH |
6 | */ |
7 | ||
8 | #include "cache.h" | |
fb58c8d5 | 9 | #include "refs.h" |
cee7f245 JH |
10 | #include "builtin.h" |
11 | #include "blob.h" | |
12 | #include "commit.h" | |
13 | #include "tag.h" | |
14 | #include "tree-walk.h" | |
15 | #include "diff.h" | |
16 | #include "diffcore.h" | |
17 | #include "revision.h" | |
717d1462 | 18 | #include "quote.h" |
cee7f245 | 19 | #include "xdiff-interface.h" |
1cfe7733 | 20 | #include "cache-tree.h" |
c455c87c | 21 | #include "string-list.h" |
f9567384 | 22 | #include "mailmap.h" |
7e6ac6e4 | 23 | #include "mergesort.h" |
5817da01 | 24 | #include "parse-options.h" |
7e6ac6e4 | 25 | #include "prio-queue.h" |
ffaf9cc0 | 26 | #include "utf8.h" |
3b8a12e8 | 27 | #include "userdiff.h" |
25ed3412 | 28 | #include "line-range.h" |
58dbfa2e | 29 | #include "line-log.h" |
dbe44faa | 30 | #include "dir.h" |
aba37f49 | 31 | #include "progress.h" |
cee7f245 | 32 | |
ce41720c | 33 | static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>"); |
5817da01 PH |
34 | |
35 | static const char *blame_opt_usage[] = { | |
36 | blame_usage, | |
37 | "", | |
9c9b4f2f | 38 | N_("<rev-opts> are documented in git-rev-list(1)"), |
5817da01 PH |
39 | NULL |
40 | }; | |
cee7f245 JH |
41 | |
42 | static int longest_file; | |
43 | static int longest_author; | |
44 | static int max_orig_digits; | |
45 | static int max_digits; | |
5ff62c30 | 46 | static int max_score_digits; |
4c10a5ca | 47 | static int show_root; |
85af7929 | 48 | static int reverse; |
4c10a5ca | 49 | static int blank_boundary; |
717d1462 | 50 | static int incremental; |
582aa00b | 51 | static int xdl_opts; |
84393bfd | 52 | static int abbrev = -1; |
3d1aa566 | 53 | static int no_whole_file_rename; |
aba37f49 | 54 | static int show_progress; |
31653c1a | 55 | |
a5481a6c | 56 | static struct date_mode blame_date_mode = { DATE_ISO8601 }; |
31653c1a EL |
57 | static size_t blame_date_width; |
58 | ||
2721ce21 | 59 | static struct string_list mailmap = STRING_LIST_INIT_NODUP; |
cee7f245 | 60 | |
54a4c617 JH |
61 | #ifndef DEBUG |
62 | #define DEBUG 0 | |
63 | #endif | |
64 | ||
c2e525d9 JH |
65 | /* stats */ |
66 | static int num_read_blob; | |
67 | static int num_get_patch; | |
68 | static int num_commits; | |
69 | ||
d24bba80 | 70 | #define PICKAXE_BLAME_MOVE 01 |
18abd745 JH |
71 | #define PICKAXE_BLAME_COPY 02 |
72 | #define PICKAXE_BLAME_COPY_HARDER 04 | |
c63777c0 | 73 | #define PICKAXE_BLAME_COPY_HARDEST 010 |
d24bba80 | 74 | |
4a0fc95f JH |
75 | /* |
76 | * blame for a blame_entry with score lower than these thresholds | |
77 | * is not passed to the parent using move/copy logic. | |
78 | */ | |
79 | static unsigned blame_move_score; | |
80 | static unsigned blame_copy_score; | |
81 | #define BLAME_DEFAULT_MOVE_SCORE 20 | |
82 | #define BLAME_DEFAULT_COPY_SCORE 40 | |
83 | ||
208acbfb | 84 | /* Remember to update object flag allocation in object.h */ |
cee7f245 JH |
85 | #define METAINFO_SHOWN (1u<<12) |
86 | #define MORE_THAN_ONE_PATH (1u<<13) | |
87 | ||
88 | /* | |
54a4c617 | 89 | * One blob in a commit that is being suspected |
cee7f245 JH |
90 | */ |
91 | struct origin { | |
54a4c617 | 92 | int refcnt; |
7e6ac6e4 | 93 | /* Record preceding blame record for this blob */ |
96e11709 | 94 | struct origin *previous; |
7e6ac6e4 DK |
95 | /* origins are put in a list linked via `next' hanging off the |
96 | * corresponding commit's util field in order to make finding | |
97 | * them fast. The presence in this chain does not count | |
98 | * towards the origin's reference count. It is tempting to | |
99 | * let it count as long as the commit is pending examination, | |
100 | * but even under circumstances where the commit will be | |
101 | * present multiple times in the priority queue of unexamined | |
102 | * commits, processing the first instance will not leave any | |
103 | * work requiring the origin data for the second instance. An | |
104 | * interspersed commit changing that would have to be | |
105 | * preexisting with a different ancestry and with the same | |
106 | * commit date in order to wedge itself between two instances | |
107 | * of the same commit in the priority queue _and_ produce | |
108 | * blame entries relevant for it. While we don't want to let | |
109 | * us get tripped up by this case, it certainly does not seem | |
110 | * worth optimizing for. | |
111 | */ | |
112 | struct origin *next; | |
cee7f245 | 113 | struct commit *commit; |
7e6ac6e4 DK |
114 | /* `suspects' contains blame entries that may be attributed to |
115 | * this origin's commit or to parent commits. When a commit | |
116 | * is being processed, all suspects will be moved, either by | |
117 | * assigning them to an origin in a different commit, or by | |
118 | * shipping them to the scoreboard's ent list because they | |
119 | * cannot be attributed to a different commit. | |
120 | */ | |
121 | struct blame_entry *suspects; | |
c2e525d9 | 122 | mmfile_t file; |
a7bcfa12 | 123 | struct object_id blob_oid; |
90064710 | 124 | unsigned mode; |
7e6ac6e4 DK |
125 | /* guilty gets set when shipping any suspects to the final |
126 | * blame list instead of other commits | |
127 | */ | |
128 | char guilty; | |
cee7f245 JH |
129 | char path[FLEX_ARRAY]; |
130 | }; | |
131 | ||
aba37f49 ECA |
132 | struct progress_info { |
133 | struct progress *progress; | |
134 | int blamed_lines; | |
135 | }; | |
136 | ||
17a07e2a | 137 | static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, |
4b4132fd RS |
138 | xdl_emit_hunk_consume_func_t hunk_func, void *cb_data) |
139 | { | |
140 | xpparam_t xpp = {0}; | |
141 | xdemitconf_t xecfg = {0}; | |
85c20c30 | 142 | xdemitcb_t ecb = {NULL}; |
4b4132fd RS |
143 | |
144 | xpp.flags = xdl_opts; | |
4b4132fd RS |
145 | xecfg.hunk_func = hunk_func; |
146 | ecb.priv = cb_data; | |
147 | return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); | |
148 | } | |
149 | ||
3b8a12e8 AB |
150 | /* |
151 | * Prepare diff_filespec and convert it using diff textconv API | |
152 | * if the textconv driver exists. | |
153 | * Return 1 if the conversion succeeds, 0 otherwise. | |
154 | */ | |
e5fba602 | 155 | int textconv_object(const char *path, |
90064710 | 156 | unsigned mode, |
acad70d1 | 157 | const struct object_id *oid, |
158 | int oid_valid, | |
e5fba602 CP |
159 | char **buf, |
160 | unsigned long *buf_size) | |
3b8a12e8 AB |
161 | { |
162 | struct diff_filespec *df; | |
163 | struct userdiff_driver *textconv; | |
164 | ||
165 | df = alloc_filespec(path); | |
acad70d1 | 166 | fill_filespec(df, oid->hash, oid_valid, mode); |
3b8a12e8 AB |
167 | textconv = get_textconv(df); |
168 | if (!textconv) { | |
169 | free_filespec(df); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | *buf_size = fill_textconv(textconv, df, buf); | |
174 | free_filespec(df); | |
175 | return 1; | |
176 | } | |
177 | ||
1732a1fd JH |
178 | /* |
179 | * Given an origin, prepare mmfile_t structure to be used by the | |
180 | * diff machinery | |
181 | */ | |
3b8a12e8 AB |
182 | static void fill_origin_blob(struct diff_options *opt, |
183 | struct origin *o, mmfile_t *file) | |
c2e525d9 JH |
184 | { |
185 | if (!o->file.ptr) { | |
21666f1a | 186 | enum object_type type; |
3b8a12e8 AB |
187 | unsigned long file_size; |
188 | ||
c2e525d9 | 189 | num_read_blob++; |
3b8a12e8 | 190 | if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && |
acad70d1 | 191 | textconv_object(o->path, o->mode, &o->blob_oid, 1, &file->ptr, &file_size)) |
3b8a12e8 AB |
192 | ; |
193 | else | |
a7bcfa12 | 194 | file->ptr = read_sha1_file(o->blob_oid.hash, &type, |
195 | &file_size); | |
3b8a12e8 AB |
196 | file->size = file_size; |
197 | ||
ab43e495 JH |
198 | if (!file->ptr) |
199 | die("Cannot read blob %s for path %s", | |
a7bcfa12 | 200 | oid_to_hex(&o->blob_oid), |
ab43e495 | 201 | o->path); |
c2e525d9 JH |
202 | o->file = *file; |
203 | } | |
204 | else | |
205 | *file = o->file; | |
c2e525d9 JH |
206 | } |
207 | ||
1732a1fd JH |
208 | /* |
209 | * Origin is refcounted and usually we keep the blob contents to be | |
210 | * reused. | |
211 | */ | |
54a4c617 JH |
212 | static inline struct origin *origin_incref(struct origin *o) |
213 | { | |
214 | if (o) | |
215 | o->refcnt++; | |
216 | return o; | |
217 | } | |
218 | ||
219 | static void origin_decref(struct origin *o) | |
220 | { | |
221 | if (o && --o->refcnt <= 0) { | |
7e6ac6e4 | 222 | struct origin *p, *l = NULL; |
96e11709 JH |
223 | if (o->previous) |
224 | origin_decref(o->previous); | |
8e0f7003 | 225 | free(o->file.ptr); |
7e6ac6e4 DK |
226 | /* Should be present exactly once in commit chain */ |
227 | for (p = o->commit->util; p; l = p, p = p->next) { | |
228 | if (p == o) { | |
229 | if (l) | |
230 | l->next = p->next; | |
231 | else | |
232 | o->commit->util = p->next; | |
233 | free(o); | |
234 | return; | |
235 | } | |
236 | } | |
237 | die("internal error in blame::origin_decref"); | |
54a4c617 JH |
238 | } |
239 | } | |
240 | ||
7c3c7962 JH |
241 | static void drop_origin_blob(struct origin *o) |
242 | { | |
243 | if (o->file.ptr) { | |
244 | free(o->file.ptr); | |
245 | o->file.ptr = NULL; | |
246 | } | |
247 | } | |
248 | ||
1732a1fd JH |
249 | /* |
250 | * Each group of lines is described by a blame_entry; it can be split | |
7e6ac6e4 DK |
251 | * as we pass blame to the parents. They are arranged in linked lists |
252 | * kept as `suspects' of some unprocessed origin, or entered (when the | |
253 | * blame origin has been finalized) into the scoreboard structure. | |
254 | * While the scoreboard structure is only sorted at the end of | |
255 | * processing (according to final image line number), the lists | |
256 | * attached to an origin are sorted by the target line number. | |
1732a1fd | 257 | */ |
cee7f245 | 258 | struct blame_entry { |
cee7f245 JH |
259 | struct blame_entry *next; |
260 | ||
261 | /* the first line of this group in the final image; | |
262 | * internally all line numbers are 0 based. | |
263 | */ | |
264 | int lno; | |
265 | ||
266 | /* how many lines this group has */ | |
267 | int num_lines; | |
268 | ||
269 | /* the commit that introduced this group into the final image */ | |
270 | struct origin *suspect; | |
271 | ||
cee7f245 JH |
272 | /* the line number of the first line of this group in the |
273 | * suspect's file; internally all line numbers are 0 based. | |
274 | */ | |
275 | int s_lno; | |
5ff62c30 JH |
276 | |
277 | /* how significant this entry is -- cached to avoid | |
1732a1fd | 278 | * scanning the lines over and over. |
5ff62c30 JH |
279 | */ |
280 | unsigned score; | |
cee7f245 JH |
281 | }; |
282 | ||
7e6ac6e4 DK |
283 | /* |
284 | * Any merge of blames happens on lists of blames that arrived via | |
285 | * different parents in a single suspect. In this case, we want to | |
286 | * sort according to the suspect line numbers as opposed to the final | |
287 | * image line numbers. The function body is somewhat longish because | |
288 | * it avoids unnecessary writes. | |
289 | */ | |
290 | ||
291 | static struct blame_entry *blame_merge(struct blame_entry *list1, | |
292 | struct blame_entry *list2) | |
293 | { | |
294 | struct blame_entry *p1 = list1, *p2 = list2, | |
295 | **tail = &list1; | |
296 | ||
297 | if (!p1) | |
298 | return p2; | |
299 | if (!p2) | |
300 | return p1; | |
301 | ||
302 | if (p1->s_lno <= p2->s_lno) { | |
303 | do { | |
304 | tail = &p1->next; | |
305 | if ((p1 = *tail) == NULL) { | |
306 | *tail = p2; | |
307 | return list1; | |
308 | } | |
309 | } while (p1->s_lno <= p2->s_lno); | |
310 | } | |
311 | for (;;) { | |
312 | *tail = p2; | |
313 | do { | |
314 | tail = &p2->next; | |
315 | if ((p2 = *tail) == NULL) { | |
316 | *tail = p1; | |
317 | return list1; | |
318 | } | |
319 | } while (p1->s_lno > p2->s_lno); | |
320 | *tail = p1; | |
321 | do { | |
322 | tail = &p1->next; | |
323 | if ((p1 = *tail) == NULL) { | |
324 | *tail = p2; | |
325 | return list1; | |
326 | } | |
327 | } while (p1->s_lno <= p2->s_lno); | |
328 | } | |
329 | } | |
330 | ||
331 | static void *get_next_blame(const void *p) | |
332 | { | |
333 | return ((struct blame_entry *)p)->next; | |
334 | } | |
335 | ||
336 | static void set_next_blame(void *p1, void *p2) | |
337 | { | |
338 | ((struct blame_entry *)p1)->next = p2; | |
339 | } | |
340 | ||
341 | /* | |
342 | * Final image line numbers are all different, so we don't need a | |
343 | * three-way comparison here. | |
344 | */ | |
345 | ||
346 | static int compare_blame_final(const void *p1, const void *p2) | |
347 | { | |
348 | return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno | |
349 | ? 1 : -1; | |
350 | } | |
351 | ||
352 | static int compare_blame_suspect(const void *p1, const void *p2) | |
353 | { | |
354 | const struct blame_entry *s1 = p1, *s2 = p2; | |
355 | /* | |
356 | * to allow for collating suspects, we sort according to the | |
357 | * respective pointer value as the primary sorting criterion. | |
358 | * The actual relation is pretty unimportant as long as it | |
359 | * establishes a total order. Comparing as integers gives us | |
360 | * that. | |
361 | */ | |
362 | if (s1->suspect != s2->suspect) | |
363 | return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1; | |
364 | if (s1->s_lno == s2->s_lno) | |
365 | return 0; | |
366 | return s1->s_lno > s2->s_lno ? 1 : -1; | |
367 | } | |
368 | ||
369 | static struct blame_entry *blame_sort(struct blame_entry *head, | |
370 | int (*compare_fn)(const void *, const void *)) | |
371 | { | |
372 | return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn); | |
373 | } | |
374 | ||
375 | static int compare_commits_by_reverse_commit_date(const void *a, | |
376 | const void *b, | |
377 | void *c) | |
378 | { | |
379 | return -compare_commits_by_commit_date(a, b, c); | |
380 | } | |
381 | ||
1732a1fd JH |
382 | /* |
383 | * The current state of the blame assignment. | |
384 | */ | |
cee7f245 JH |
385 | struct scoreboard { |
386 | /* the final commit (i.e. where we started digging from) */ | |
387 | struct commit *final; | |
7e6ac6e4 DK |
388 | /* Priority queue for commits with unassigned blame records */ |
389 | struct prio_queue commits; | |
85af7929 | 390 | struct rev_info *revs; |
cee7f245 JH |
391 | const char *path; |
392 | ||
1732a1fd JH |
393 | /* |
394 | * The contents in the final image. | |
395 | * Used by many functions to obtain contents of the nth line, | |
396 | * indexed with scoreboard.lineno[blame_entry.lno]. | |
cee7f245 JH |
397 | */ |
398 | const char *final_buf; | |
399 | unsigned long final_buf_size; | |
400 | ||
401 | /* linked list of blames */ | |
402 | struct blame_entry *ent; | |
403 | ||
612702e8 | 404 | /* look-up a line in the final buffer */ |
cee7f245 JH |
405 | int num_lines; |
406 | int *lineno; | |
407 | }; | |
408 | ||
54a4c617 JH |
409 | static void sanity_check_refcnt(struct scoreboard *); |
410 | ||
1732a1fd JH |
411 | /* |
412 | * If two blame entries that are next to each other came from | |
413 | * contiguous lines in the same origin (i.e. <commit, path> pair), | |
414 | * merge them together. | |
415 | */ | |
cee7f245 JH |
416 | static void coalesce(struct scoreboard *sb) |
417 | { | |
418 | struct blame_entry *ent, *next; | |
419 | ||
420 | for (ent = sb->ent; ent && (next = ent->next); ent = next) { | |
0a88f08e | 421 | if (ent->suspect == next->suspect && |
cee7f245 JH |
422 | ent->s_lno + ent->num_lines == next->s_lno) { |
423 | ent->num_lines += next->num_lines; | |
424 | ent->next = next->next; | |
54a4c617 | 425 | origin_decref(next->suspect); |
cee7f245 | 426 | free(next); |
46014766 | 427 | ent->score = 0; |
cee7f245 JH |
428 | next = ent; /* again */ |
429 | } | |
430 | } | |
54a4c617 JH |
431 | |
432 | if (DEBUG) /* sanity */ | |
433 | sanity_check_refcnt(sb); | |
cee7f245 JH |
434 | } |
435 | ||
7e6ac6e4 DK |
436 | /* |
437 | * Merge the given sorted list of blames into a preexisting origin. | |
438 | * If there were no previous blames to that commit, it is entered into | |
439 | * the commit priority queue of the score board. | |
440 | */ | |
441 | ||
442 | static void queue_blames(struct scoreboard *sb, struct origin *porigin, | |
443 | struct blame_entry *sorted) | |
444 | { | |
445 | if (porigin->suspects) | |
446 | porigin->suspects = blame_merge(porigin->suspects, sorted); | |
447 | else { | |
448 | struct origin *o; | |
449 | for (o = porigin->commit->util; o; o = o->next) { | |
450 | if (o->suspects) { | |
451 | porigin->suspects = sorted; | |
452 | return; | |
453 | } | |
454 | } | |
455 | porigin->suspects = sorted; | |
456 | prio_queue_put(&sb->commits, porigin->commit); | |
457 | } | |
458 | } | |
459 | ||
1732a1fd JH |
460 | /* |
461 | * Given a commit and a path in it, create a new origin structure. | |
462 | * The callers that add blame to the scoreboard should use | |
463 | * get_origin() to obtain shared, refcounted copy instead of calling | |
464 | * this function directly. | |
465 | */ | |
854b97f6 JH |
466 | static struct origin *make_origin(struct commit *commit, const char *path) |
467 | { | |
468 | struct origin *o; | |
96ffc06f | 469 | FLEX_ALLOC_STR(o, path, path); |
854b97f6 JH |
470 | o->commit = commit; |
471 | o->refcnt = 1; | |
7e6ac6e4 DK |
472 | o->next = commit->util; |
473 | commit->util = o; | |
854b97f6 JH |
474 | return o; |
475 | } | |
476 | ||
1732a1fd JH |
477 | /* |
478 | * Locate an existing origin or create a new one. | |
7e6ac6e4 | 479 | * This moves the origin to front position in the commit util list. |
1732a1fd | 480 | */ |
f6c0e191 JH |
481 | static struct origin *get_origin(struct scoreboard *sb, |
482 | struct commit *commit, | |
483 | const char *path) | |
cee7f245 | 484 | { |
7e6ac6e4 | 485 | struct origin *o, *l; |
cee7f245 | 486 | |
7e6ac6e4 DK |
487 | for (o = commit->util, l = NULL; o; l = o, o = o->next) { |
488 | if (!strcmp(o->path, path)) { | |
489 | /* bump to front */ | |
490 | if (l) { | |
491 | l->next = o->next; | |
492 | o->next = commit->util; | |
493 | commit->util = o; | |
494 | } | |
495 | return origin_incref(o); | |
496 | } | |
cee7f245 | 497 | } |
854b97f6 | 498 | return make_origin(commit, path); |
cee7f245 JH |
499 | } |
500 | ||
1732a1fd JH |
501 | /* |
502 | * Fill the blob_sha1 field of an origin if it hasn't, so that later | |
503 | * call to fill_origin_blob() can use it to locate the data. blob_sha1 | |
504 | * for an origin is also used to pass the blame for the entire file to | |
505 | * the parent to detect the case where a child's blob is identical to | |
506 | * that of its parent's. | |
90064710 KS |
507 | * |
508 | * This also fills origin->mode for corresponding tree path. | |
1732a1fd | 509 | */ |
90064710 | 510 | static int fill_blob_sha1_and_mode(struct origin *origin) |
f6c0e191 | 511 | { |
a7bcfa12 | 512 | if (!is_null_oid(&origin->blob_oid)) |
f6c0e191 | 513 | return 0; |
ed1c9977 | 514 | if (get_tree_entry(origin->commit->object.oid.hash, |
f6c0e191 | 515 | origin->path, |
a7bcfa12 | 516 | origin->blob_oid.hash, &origin->mode)) |
f6c0e191 | 517 | goto error_out; |
a7bcfa12 | 518 | if (sha1_object_info(origin->blob_oid.hash, NULL) != OBJ_BLOB) |
f6c0e191 JH |
519 | goto error_out; |
520 | return 0; | |
521 | error_out: | |
a7bcfa12 | 522 | oidclr(&origin->blob_oid); |
90064710 | 523 | origin->mode = S_IFINVALID; |
f6c0e191 JH |
524 | return -1; |
525 | } | |
526 | ||
1732a1fd JH |
527 | /* |
528 | * We have an origin -- check if the same path exists in the | |
529 | * parent and return an origin structure to represent it. | |
530 | */ | |
f6c0e191 | 531 | static struct origin *find_origin(struct scoreboard *sb, |
cee7f245 JH |
532 | struct commit *parent, |
533 | struct origin *origin) | |
534 | { | |
7e6ac6e4 | 535 | struct origin *porigin; |
cee7f245 | 536 | struct diff_options diff_opts; |
f6c0e191 JH |
537 | const char *paths[2]; |
538 | ||
7e6ac6e4 DK |
539 | /* First check any existing origins */ |
540 | for (porigin = parent->util; porigin; porigin = porigin->next) | |
541 | if (!strcmp(porigin->path, origin->path)) { | |
1732a1fd JH |
542 | /* |
543 | * The same path between origin and its parent | |
544 | * without renaming -- the most common case. | |
545 | */ | |
7e6ac6e4 | 546 | return origin_incref (porigin); |
854b97f6 | 547 | } |
0d981c67 | 548 | |
f6c0e191 JH |
549 | /* See if the origin->path is different between parent |
550 | * and origin first. Most of the time they are the | |
551 | * same and diff-tree is fairly efficient about this. | |
552 | */ | |
553 | diff_setup(&diff_opts); | |
8f67f8ae | 554 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
f6c0e191 JH |
555 | diff_opts.detect_rename = 0; |
556 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
557 | paths[0] = origin->path; | |
558 | paths[1] = NULL; | |
559 | ||
4a2d5ae2 NTND |
560 | parse_pathspec(&diff_opts.pathspec, |
561 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
562 | PATHSPEC_LITERAL_PATH, "", paths); | |
28452655 | 563 | diff_setup_done(&diff_opts); |
1cfe7733 | 564 | |
f2fd0760 | 565 | if (is_null_oid(&origin->commit->object.oid)) |
ed1c9977 | 566 | do_diff_cache(parent->tree->object.oid.hash, &diff_opts); |
1cfe7733 | 567 | else |
ed1c9977 | 568 | diff_tree_sha1(parent->tree->object.oid.hash, |
569 | origin->commit->tree->object.oid.hash, | |
1cfe7733 | 570 | "", &diff_opts); |
f6c0e191 JH |
571 | diffcore_std(&diff_opts); |
572 | ||
f6c0e191 JH |
573 | if (!diff_queued_diff.nr) { |
574 | /* The path is the same as parent */ | |
575 | porigin = get_origin(sb, parent, origin->path); | |
a7bcfa12 | 576 | oidcpy(&porigin->blob_oid, &origin->blob_oid); |
90064710 | 577 | porigin->mode = origin->mode; |
a9b2d424 JH |
578 | } else { |
579 | /* | |
580 | * Since origin->path is a pathspec, if the parent | |
581 | * commit had it as a directory, we will see a whole | |
582 | * bunch of deletion of files in the directory that we | |
583 | * do not care about. | |
584 | */ | |
585 | int i; | |
586 | struct diff_filepair *p = NULL; | |
587 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
588 | const char *name; | |
589 | p = diff_queued_diff.queue[i]; | |
590 | name = p->one->path ? p->one->path : p->two->path; | |
591 | if (!strcmp(name, origin->path)) | |
592 | break; | |
593 | } | |
594 | if (!p) | |
595 | die("internal error in blame::find_origin"); | |
f6c0e191 JH |
596 | switch (p->status) { |
597 | default: | |
acca687f | 598 | die("internal error in blame::find_origin (%c)", |
f6c0e191 JH |
599 | p->status); |
600 | case 'M': | |
601 | porigin = get_origin(sb, parent, origin->path); | |
a7bcfa12 | 602 | oidcpy(&porigin->blob_oid, &p->one->oid); |
90064710 | 603 | porigin->mode = p->one->mode; |
f6c0e191 JH |
604 | break; |
605 | case 'A': | |
606 | case 'T': | |
607 | /* Did not exist in parent, or type changed */ | |
608 | break; | |
609 | } | |
610 | } | |
611 | diff_flush(&diff_opts); | |
ed6e8038 | 612 | clear_pathspec(&diff_opts.pathspec); |
f69e743d JH |
613 | return porigin; |
614 | } | |
f6c0e191 | 615 | |
1732a1fd JH |
616 | /* |
617 | * We have an origin -- find the path that corresponds to it in its | |
618 | * parent and return an origin structure to represent it. | |
619 | */ | |
f69e743d JH |
620 | static struct origin *find_rename(struct scoreboard *sb, |
621 | struct commit *parent, | |
622 | struct origin *origin) | |
623 | { | |
624 | struct origin *porigin = NULL; | |
625 | struct diff_options diff_opts; | |
626 | int i; | |
cee7f245 JH |
627 | |
628 | diff_setup(&diff_opts); | |
8f67f8ae | 629 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
cee7f245 JH |
630 | diff_opts.detect_rename = DIFF_DETECT_RENAME; |
631 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
2f3f8b21 | 632 | diff_opts.single_follow = origin->path; |
28452655 | 633 | diff_setup_done(&diff_opts); |
1cfe7733 | 634 | |
f2fd0760 | 635 | if (is_null_oid(&origin->commit->object.oid)) |
ed1c9977 | 636 | do_diff_cache(parent->tree->object.oid.hash, &diff_opts); |
1cfe7733 | 637 | else |
ed1c9977 | 638 | diff_tree_sha1(parent->tree->object.oid.hash, |
639 | origin->commit->tree->object.oid.hash, | |
1cfe7733 | 640 | "", &diff_opts); |
cee7f245 JH |
641 | diffcore_std(&diff_opts); |
642 | ||
643 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
644 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
612702e8 | 645 | if ((p->status == 'R' || p->status == 'C') && |
f6c0e191 JH |
646 | !strcmp(p->two->path, origin->path)) { |
647 | porigin = get_origin(sb, parent, p->one->path); | |
a7bcfa12 | 648 | oidcpy(&porigin->blob_oid, &p->one->oid); |
90064710 | 649 | porigin->mode = p->one->mode; |
cee7f245 JH |
650 | break; |
651 | } | |
652 | } | |
653 | diff_flush(&diff_opts); | |
ed6e8038 | 654 | clear_pathspec(&diff_opts.pathspec); |
cee7f245 JH |
655 | return porigin; |
656 | } | |
657 | ||
1732a1fd | 658 | /* |
7e6ac6e4 | 659 | * Append a new blame entry to a given output queue. |
1732a1fd | 660 | */ |
7e6ac6e4 | 661 | static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e) |
cee7f245 | 662 | { |
54a4c617 JH |
663 | origin_incref(e->suspect); |
664 | ||
7e6ac6e4 DK |
665 | e->next = **queue; |
666 | **queue = e; | |
667 | *queue = &e->next; | |
cee7f245 JH |
668 | } |
669 | ||
1732a1fd JH |
670 | /* |
671 | * src typically is on-stack; we want to copy the information in it to | |
7e6ac6e4 DK |
672 | * a malloced blame_entry that gets added to the given queue. The |
673 | * origin of dst loses a refcnt. | |
1732a1fd | 674 | */ |
7e6ac6e4 DK |
675 | static void dup_entry(struct blame_entry ***queue, |
676 | struct blame_entry *dst, struct blame_entry *src) | |
cee7f245 | 677 | { |
54a4c617 JH |
678 | origin_incref(src->suspect); |
679 | origin_decref(dst->suspect); | |
cee7f245 | 680 | memcpy(dst, src, sizeof(*src)); |
7e6ac6e4 DK |
681 | dst->next = **queue; |
682 | **queue = dst; | |
683 | *queue = &dst->next; | |
cee7f245 JH |
684 | } |
685 | ||
25ed3412 | 686 | static const char *nth_line(struct scoreboard *sb, long lno) |
cee7f245 JH |
687 | { |
688 | return sb->final_buf + sb->lineno[lno]; | |
689 | } | |
690 | ||
25ed3412 BY |
691 | static const char *nth_line_cb(void *data, long lno) |
692 | { | |
693 | return nth_line((struct scoreboard *)data, lno); | |
694 | } | |
695 | ||
1732a1fd JH |
696 | /* |
697 | * It is known that lines between tlno to same came from parent, and e | |
698 | * has an overlap with that range. it also is known that parent's | |
699 | * line plno corresponds to e's line tlno. | |
700 | * | |
701 | * <---- e -----> | |
702 | * <------> | |
703 | * <------------> | |
704 | * <------------> | |
705 | * <------------------> | |
706 | * | |
707 | * Split e into potentially three parts; before this chunk, the chunk | |
708 | * to be blamed for the parent, and after that portion. | |
709 | */ | |
54a4c617 | 710 | static void split_overlap(struct blame_entry *split, |
cee7f245 JH |
711 | struct blame_entry *e, |
712 | int tlno, int plno, int same, | |
713 | struct origin *parent) | |
714 | { | |
cee7f245 JH |
715 | int chunk_end_lno; |
716 | memset(split, 0, sizeof(struct blame_entry [3])); | |
717 | ||
718 | if (e->s_lno < tlno) { | |
719 | /* there is a pre-chunk part not blamed on parent */ | |
54a4c617 | 720 | split[0].suspect = origin_incref(e->suspect); |
cee7f245 JH |
721 | split[0].lno = e->lno; |
722 | split[0].s_lno = e->s_lno; | |
723 | split[0].num_lines = tlno - e->s_lno; | |
724 | split[1].lno = e->lno + tlno - e->s_lno; | |
725 | split[1].s_lno = plno; | |
726 | } | |
727 | else { | |
728 | split[1].lno = e->lno; | |
729 | split[1].s_lno = plno + (e->s_lno - tlno); | |
730 | } | |
731 | ||
732 | if (same < e->s_lno + e->num_lines) { | |
733 | /* there is a post-chunk part not blamed on parent */ | |
54a4c617 | 734 | split[2].suspect = origin_incref(e->suspect); |
cee7f245 JH |
735 | split[2].lno = e->lno + (same - e->s_lno); |
736 | split[2].s_lno = e->s_lno + (same - e->s_lno); | |
737 | split[2].num_lines = e->s_lno + e->num_lines - same; | |
738 | chunk_end_lno = split[2].lno; | |
739 | } | |
740 | else | |
741 | chunk_end_lno = e->lno + e->num_lines; | |
742 | split[1].num_lines = chunk_end_lno - split[1].lno; | |
743 | ||
1732a1fd JH |
744 | /* |
745 | * if it turns out there is nothing to blame the parent for, | |
746 | * forget about the splitting. !split[1].suspect signals this. | |
747 | */ | |
cee7f245 JH |
748 | if (split[1].num_lines < 1) |
749 | return; | |
54a4c617 | 750 | split[1].suspect = origin_incref(parent); |
cee7f245 JH |
751 | } |
752 | ||
1732a1fd JH |
753 | /* |
754 | * split_overlap() divided an existing blame e into up to three parts | |
7e6ac6e4 | 755 | * in split. Any assigned blame is moved to queue to |
1732a1fd JH |
756 | * reflect the split. |
757 | */ | |
7e6ac6e4 DK |
758 | static void split_blame(struct blame_entry ***blamed, |
759 | struct blame_entry ***unblamed, | |
54a4c617 | 760 | struct blame_entry *split, |
cee7f245 JH |
761 | struct blame_entry *e) |
762 | { | |
763 | struct blame_entry *new_entry; | |
764 | ||
765 | if (split[0].suspect && split[2].suspect) { | |
1732a1fd | 766 | /* The first part (reuse storage for the existing entry e) */ |
7e6ac6e4 | 767 | dup_entry(unblamed, e, &split[0]); |
cee7f245 | 768 | |
1732a1fd | 769 | /* The last part -- me */ |
cee7f245 JH |
770 | new_entry = xmalloc(sizeof(*new_entry)); |
771 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
7e6ac6e4 | 772 | add_blame_entry(unblamed, new_entry); |
cee7f245 | 773 | |
1732a1fd | 774 | /* ... and the middle part -- parent */ |
cee7f245 JH |
775 | new_entry = xmalloc(sizeof(*new_entry)); |
776 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
7e6ac6e4 | 777 | add_blame_entry(blamed, new_entry); |
cee7f245 JH |
778 | } |
779 | else if (!split[0].suspect && !split[2].suspect) | |
1732a1fd JH |
780 | /* |
781 | * The parent covers the entire area; reuse storage for | |
782 | * e and replace it with the parent. | |
783 | */ | |
7e6ac6e4 | 784 | dup_entry(blamed, e, &split[1]); |
cee7f245 | 785 | else if (split[0].suspect) { |
1732a1fd | 786 | /* me and then parent */ |
7e6ac6e4 | 787 | dup_entry(unblamed, e, &split[0]); |
cee7f245 JH |
788 | |
789 | new_entry = xmalloc(sizeof(*new_entry)); | |
790 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
7e6ac6e4 | 791 | add_blame_entry(blamed, new_entry); |
cee7f245 JH |
792 | } |
793 | else { | |
1732a1fd | 794 | /* parent and then me */ |
7e6ac6e4 | 795 | dup_entry(blamed, e, &split[1]); |
cee7f245 JH |
796 | |
797 | new_entry = xmalloc(sizeof(*new_entry)); | |
798 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
7e6ac6e4 | 799 | add_blame_entry(unblamed, new_entry); |
cee7f245 JH |
800 | } |
801 | } | |
802 | ||
1732a1fd JH |
803 | /* |
804 | * After splitting the blame, the origins used by the | |
805 | * on-stack blame_entry should lose one refcnt each. | |
806 | */ | |
54a4c617 JH |
807 | static void decref_split(struct blame_entry *split) |
808 | { | |
809 | int i; | |
810 | ||
811 | for (i = 0; i < 3; i++) | |
812 | origin_decref(split[i].suspect); | |
813 | } | |
814 | ||
1732a1fd | 815 | /* |
7e6ac6e4 DK |
816 | * reverse_blame reverses the list given in head, appending tail. |
817 | * That allows us to build lists in reverse order, then reverse them | |
818 | * afterwards. This can be faster than building the list in proper | |
819 | * order right away. The reason is that building in proper order | |
820 | * requires writing a link in the _previous_ element, while building | |
821 | * in reverse order just requires placing the list head into the | |
822 | * _current_ element. | |
1732a1fd | 823 | */ |
cee7f245 | 824 | |
7e6ac6e4 DK |
825 | static struct blame_entry *reverse_blame(struct blame_entry *head, |
826 | struct blame_entry *tail) | |
cee7f245 | 827 | { |
7e6ac6e4 DK |
828 | while (head) { |
829 | struct blame_entry *next = head->next; | |
830 | head->next = tail; | |
831 | tail = head; | |
832 | head = next; | |
cee7f245 | 833 | } |
7e6ac6e4 | 834 | return tail; |
cee7f245 JH |
835 | } |
836 | ||
1732a1fd JH |
837 | /* |
838 | * Process one hunk from the patch between the current suspect for | |
7e6ac6e4 DK |
839 | * blame_entry e and its parent. This first blames any unfinished |
840 | * entries before the chunk (which is where target and parent start | |
841 | * differing) on the parent, and then splits blame entries at the | |
842 | * start and at the end of the difference region. Since use of -M and | |
843 | * -C options may lead to overlapping/duplicate source line number | |
844 | * ranges, all we can rely on from sorting/merging is the order of the | |
845 | * first suspect line number. | |
1732a1fd | 846 | */ |
7e6ac6e4 DK |
847 | static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq, |
848 | int tlno, int offset, int same, | |
849 | struct origin *parent) | |
cee7f245 | 850 | { |
7e6ac6e4 DK |
851 | struct blame_entry *e = **srcq; |
852 | struct blame_entry *samep = NULL, *diffp = NULL; | |
cee7f245 | 853 | |
7e6ac6e4 DK |
854 | while (e && e->s_lno < tlno) { |
855 | struct blame_entry *next = e->next; | |
856 | /* | |
857 | * current record starts before differing portion. If | |
858 | * it reaches into it, we need to split it up and | |
859 | * examine the second part separately. | |
860 | */ | |
861 | if (e->s_lno + e->num_lines > tlno) { | |
862 | /* Move second half to a new record */ | |
863 | int len = tlno - e->s_lno; | |
864 | struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry)); | |
865 | n->suspect = e->suspect; | |
866 | n->lno = e->lno + len; | |
867 | n->s_lno = e->s_lno + len; | |
868 | n->num_lines = e->num_lines - len; | |
869 | e->num_lines = len; | |
870 | e->score = 0; | |
871 | /* Push new record to diffp */ | |
872 | n->next = diffp; | |
873 | diffp = n; | |
874 | } else | |
875 | origin_decref(e->suspect); | |
876 | /* Pass blame for everything before the differing | |
877 | * chunk to the parent */ | |
878 | e->suspect = origin_incref(parent); | |
879 | e->s_lno += offset; | |
880 | e->next = samep; | |
881 | samep = e; | |
882 | e = next; | |
883 | } | |
884 | /* | |
885 | * As we don't know how much of a common stretch after this | |
886 | * diff will occur, the currently blamed parts are all that we | |
887 | * can assign to the parent for now. | |
888 | */ | |
889 | ||
890 | if (samep) { | |
891 | **dstq = reverse_blame(samep, **dstq); | |
892 | *dstq = &samep->next; | |
cee7f245 | 893 | } |
7e6ac6e4 DK |
894 | /* |
895 | * Prepend the split off portions: everything after e starts | |
896 | * after the blameable portion. | |
897 | */ | |
898 | e = reverse_blame(diffp, e); | |
899 | ||
900 | /* | |
901 | * Now retain records on the target while parts are different | |
902 | * from the parent. | |
903 | */ | |
904 | samep = NULL; | |
905 | diffp = NULL; | |
906 | while (e && e->s_lno < same) { | |
907 | struct blame_entry *next = e->next; | |
908 | ||
909 | /* | |
910 | * If current record extends into sameness, need to split. | |
911 | */ | |
912 | if (e->s_lno + e->num_lines > same) { | |
913 | /* | |
914 | * Move second half to a new record to be | |
915 | * processed by later chunks | |
916 | */ | |
917 | int len = same - e->s_lno; | |
918 | struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry)); | |
919 | n->suspect = origin_incref(e->suspect); | |
920 | n->lno = e->lno + len; | |
921 | n->s_lno = e->s_lno + len; | |
922 | n->num_lines = e->num_lines - len; | |
923 | e->num_lines = len; | |
924 | e->score = 0; | |
925 | /* Push new record to samep */ | |
926 | n->next = samep; | |
927 | samep = n; | |
928 | } | |
929 | e->next = diffp; | |
930 | diffp = e; | |
931 | e = next; | |
932 | } | |
933 | **srcq = reverse_blame(diffp, reverse_blame(samep, e)); | |
934 | /* Move across elements that are in the unblamable portion */ | |
935 | if (diffp) | |
936 | *srcq = &diffp->next; | |
cee7f245 JH |
937 | } |
938 | ||
cdcab133 | 939 | struct blame_chunk_cb_data { |
cdcab133 | 940 | struct origin *parent; |
7e6ac6e4 DK |
941 | long offset; |
942 | struct blame_entry **dstq; | |
943 | struct blame_entry **srcq; | |
cdcab133 RS |
944 | }; |
945 | ||
7e6ac6e4 | 946 | /* diff chunks are from parent to target */ |
0af596c6 RS |
947 | static int blame_chunk_cb(long start_a, long count_a, |
948 | long start_b, long count_b, void *data) | |
cdcab133 RS |
949 | { |
950 | struct blame_chunk_cb_data *d = data; | |
7e6ac6e4 DK |
951 | if (start_a - start_b != d->offset) |
952 | die("internal error in blame::blame_chunk_cb"); | |
953 | blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b, | |
954 | start_b + count_b, d->parent); | |
955 | d->offset = start_a + count_a - (start_b + count_b); | |
0af596c6 | 956 | return 0; |
cdcab133 RS |
957 | } |
958 | ||
1732a1fd JH |
959 | /* |
960 | * We are looking at the origin 'target' and aiming to pass blame | |
961 | * for the lines it is suspected to its parent. Run diff to find | |
962 | * which lines came from parent and pass blame for them. | |
963 | */ | |
7e6ac6e4 DK |
964 | static void pass_blame_to_parent(struct scoreboard *sb, |
965 | struct origin *target, | |
966 | struct origin *parent) | |
cee7f245 | 967 | { |
7c4ed8c8 | 968 | mmfile_t file_p, file_o; |
66dbfd55 | 969 | struct blame_chunk_cb_data d; |
7e6ac6e4 | 970 | struct blame_entry *newdest = NULL; |
0af596c6 | 971 | |
7e6ac6e4 DK |
972 | if (!target->suspects) |
973 | return; /* nothing remains for this target */ | |
974 | ||
975 | d.parent = parent; | |
976 | d.offset = 0; | |
977 | d.dstq = &newdest; d.srcq = &target->suspects; | |
cee7f245 | 978 | |
3b8a12e8 AB |
979 | fill_origin_blob(&sb->revs->diffopt, parent, &file_p); |
980 | fill_origin_blob(&sb->revs->diffopt, target, &file_o); | |
7c4ed8c8 | 981 | num_get_patch++; |
cee7f245 | 982 | |
17a07e2a | 983 | if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d)) |
3efb9880 | 984 | die("unable to generate diff (%s -> %s)", |
f2fd0760 | 985 | oid_to_hex(&parent->commit->object.oid), |
986 | oid_to_hex(&target->commit->object.oid)); | |
7e6ac6e4 DK |
987 | /* The rest are the same as the parent */ |
988 | blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent); | |
989 | *d.dstq = NULL; | |
990 | queue_blames(sb, parent, newdest); | |
cee7f245 | 991 | |
7e6ac6e4 | 992 | return; |
cee7f245 JH |
993 | } |
994 | ||
1732a1fd JH |
995 | /* |
996 | * The lines in blame_entry after splitting blames many times can become | |
997 | * very small and trivial, and at some point it becomes pointless to | |
998 | * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any | |
999 | * ordinary C program, and it is not worth to say it was copied from | |
1000 | * totally unrelated file in the parent. | |
1001 | * | |
1002 | * Compute how trivial the lines in the blame_entry are. | |
1003 | */ | |
5ff62c30 JH |
1004 | static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e) |
1005 | { | |
1006 | unsigned score; | |
1007 | const char *cp, *ep; | |
1008 | ||
1009 | if (e->score) | |
1010 | return e->score; | |
1011 | ||
612702e8 | 1012 | score = 1; |
5ff62c30 JH |
1013 | cp = nth_line(sb, e->lno); |
1014 | ep = nth_line(sb, e->lno + e->num_lines); | |
1015 | while (cp < ep) { | |
1016 | unsigned ch = *((unsigned char *)cp); | |
1017 | if (isalnum(ch)) | |
1018 | score++; | |
1019 | cp++; | |
1020 | } | |
1021 | e->score = score; | |
1022 | return score; | |
1023 | } | |
1024 | ||
1732a1fd JH |
1025 | /* |
1026 | * best_so_far[] and this[] are both a split of an existing blame_entry | |
1027 | * that passes blame to the parent. Maintain best_so_far the best split | |
1028 | * so far, by comparing this and best_so_far and copying this into | |
1029 | * bst_so_far as needed. | |
1030 | */ | |
5ff62c30 | 1031 | static void copy_split_if_better(struct scoreboard *sb, |
54a4c617 JH |
1032 | struct blame_entry *best_so_far, |
1033 | struct blame_entry *this) | |
d24bba80 | 1034 | { |
54a4c617 JH |
1035 | int i; |
1036 | ||
d24bba80 JH |
1037 | if (!this[1].suspect) |
1038 | return; | |
5ff62c30 JH |
1039 | if (best_so_far[1].suspect) { |
1040 | if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1])) | |
1041 | return; | |
1042 | } | |
54a4c617 JH |
1043 | |
1044 | for (i = 0; i < 3; i++) | |
1045 | origin_incref(this[i].suspect); | |
1046 | decref_split(best_so_far); | |
d24bba80 JH |
1047 | memcpy(best_so_far, this, sizeof(struct blame_entry [3])); |
1048 | } | |
1049 | ||
dd166aa8 JH |
1050 | /* |
1051 | * We are looking at a part of the final image represented by | |
1052 | * ent (tlno and same are offset by ent->s_lno). | |
1053 | * tlno is where we are looking at in the final image. | |
1054 | * up to (but not including) same match preimage. | |
1055 | * plno is where we are looking at in the preimage. | |
1056 | * | |
1057 | * <-------------- final image ----------------------> | |
1058 | * <------ent------> | |
1059 | * ^tlno ^same | |
1060 | * <---------preimage-----> | |
1061 | * ^plno | |
1062 | * | |
1063 | * All line numbers are 0-based. | |
1064 | */ | |
1065 | static void handle_split(struct scoreboard *sb, | |
1066 | struct blame_entry *ent, | |
1067 | int tlno, int plno, int same, | |
1068 | struct origin *parent, | |
1069 | struct blame_entry *split) | |
1070 | { | |
1071 | if (ent->num_lines <= tlno) | |
1072 | return; | |
1073 | if (tlno < same) { | |
1074 | struct blame_entry this[3]; | |
1075 | tlno += ent->s_lno; | |
1076 | same += ent->s_lno; | |
1077 | split_overlap(this, ent, tlno, plno, same, parent); | |
1078 | copy_split_if_better(sb, split, this); | |
1079 | decref_split(this); | |
1080 | } | |
1081 | } | |
1082 | ||
cdcab133 RS |
1083 | struct handle_split_cb_data { |
1084 | struct scoreboard *sb; | |
1085 | struct blame_entry *ent; | |
1086 | struct origin *parent; | |
1087 | struct blame_entry *split; | |
1088 | long plno; | |
1089 | long tlno; | |
1090 | }; | |
1091 | ||
5d23ec76 RS |
1092 | static int handle_split_cb(long start_a, long count_a, |
1093 | long start_b, long count_b, void *data) | |
cdcab133 RS |
1094 | { |
1095 | struct handle_split_cb_data *d = data; | |
5d23ec76 RS |
1096 | handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent, |
1097 | d->split); | |
1098 | d->plno = start_a + count_a; | |
1099 | d->tlno = start_b + count_b; | |
1100 | return 0; | |
cdcab133 RS |
1101 | } |
1102 | ||
1732a1fd JH |
1103 | /* |
1104 | * Find the lines from parent that are the same as ent so that | |
1105 | * we can pass blames to it. file_p has the blob contents for | |
1106 | * the parent. | |
1107 | */ | |
d24bba80 JH |
1108 | static void find_copy_in_blob(struct scoreboard *sb, |
1109 | struct blame_entry *ent, | |
1110 | struct origin *parent, | |
54a4c617 | 1111 | struct blame_entry *split, |
d24bba80 JH |
1112 | mmfile_t *file_p) |
1113 | { | |
1114 | const char *cp; | |
d24bba80 | 1115 | mmfile_t file_o; |
66dbfd55 | 1116 | struct handle_split_cb_data d; |
5d23ec76 | 1117 | |
66dbfd55 GV |
1118 | memset(&d, 0, sizeof(d)); |
1119 | d.sb = sb; d.ent = ent; d.parent = parent; d.split = split; | |
1732a1fd JH |
1120 | /* |
1121 | * Prepare mmfile that contains only the lines in ent. | |
1122 | */ | |
d24bba80 | 1123 | cp = nth_line(sb, ent->lno); |
4b25d091 | 1124 | file_o.ptr = (char *) cp; |
3ee8944f | 1125 | file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp; |
d24bba80 | 1126 | |
dd166aa8 JH |
1127 | /* |
1128 | * file_o is a part of final image we are annotating. | |
1129 | * file_p partially may match that image. | |
1130 | */ | |
d24bba80 | 1131 | memset(split, 0, sizeof(struct blame_entry [3])); |
17a07e2a | 1132 | if (diff_hunks(file_p, &file_o, handle_split_cb, &d)) |
3efb9880 | 1133 | die("unable to generate diff (%s)", |
f2fd0760 | 1134 | oid_to_hex(&parent->commit->object.oid)); |
dd166aa8 | 1135 | /* remainder, if any, all match the preimage */ |
cdcab133 | 1136 | handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split); |
d24bba80 JH |
1137 | } |
1138 | ||
7e6ac6e4 DK |
1139 | /* Move all blame entries from list *source that have a score smaller |
1140 | * than score_min to the front of list *small. | |
1141 | * Returns a pointer to the link pointing to the old head of the small list. | |
1142 | */ | |
1143 | ||
1144 | static struct blame_entry **filter_small(struct scoreboard *sb, | |
1145 | struct blame_entry **small, | |
1146 | struct blame_entry **source, | |
1147 | unsigned score_min) | |
1148 | { | |
1149 | struct blame_entry *p = *source; | |
1150 | struct blame_entry *oldsmall = *small; | |
1151 | while (p) { | |
1152 | if (ent_score(sb, p) <= score_min) { | |
1153 | *small = p; | |
1154 | small = &p->next; | |
1155 | p = *small; | |
1156 | } else { | |
1157 | *source = p; | |
1158 | source = &p->next; | |
1159 | p = *source; | |
1160 | } | |
1161 | } | |
1162 | *small = oldsmall; | |
1163 | *source = NULL; | |
1164 | return small; | |
1165 | } | |
1166 | ||
1732a1fd JH |
1167 | /* |
1168 | * See if lines currently target is suspected for can be attributed to | |
1169 | * parent. | |
1170 | */ | |
7e6ac6e4 DK |
1171 | static void find_move_in_parent(struct scoreboard *sb, |
1172 | struct blame_entry ***blamed, | |
1173 | struct blame_entry **toosmall, | |
1174 | struct origin *target, | |
1175 | struct origin *parent) | |
d24bba80 | 1176 | { |
46014766 | 1177 | struct blame_entry *e, split[3]; |
7e6ac6e4 DK |
1178 | struct blame_entry *unblamed = target->suspects; |
1179 | struct blame_entry *leftover = NULL; | |
d24bba80 | 1180 | mmfile_t file_p; |
d24bba80 | 1181 | |
7e6ac6e4 DK |
1182 | if (!unblamed) |
1183 | return; /* nothing remains for this target */ | |
d24bba80 | 1184 | |
3b8a12e8 | 1185 | fill_origin_blob(&sb->revs->diffopt, parent, &file_p); |
c2e525d9 | 1186 | if (!file_p.ptr) |
7e6ac6e4 | 1187 | return; |
d24bba80 | 1188 | |
7e6ac6e4 DK |
1189 | /* At each iteration, unblamed has a NULL-terminated list of |
1190 | * entries that have not yet been tested for blame. leftover | |
1191 | * contains the reversed list of entries that have been tested | |
1192 | * without being assignable to the parent. | |
1193 | */ | |
1194 | do { | |
1195 | struct blame_entry **unblamedtail = &unblamed; | |
1196 | struct blame_entry *next; | |
1197 | for (e = unblamed; e; e = next) { | |
1198 | next = e->next; | |
650e2f67 JH |
1199 | find_copy_in_blob(sb, e, parent, split, &file_p); |
1200 | if (split[1].suspect && | |
1201 | blame_move_score < ent_score(sb, &split[1])) { | |
7e6ac6e4 DK |
1202 | split_blame(blamed, &unblamedtail, split, e); |
1203 | } else { | |
1204 | e->next = leftover; | |
1205 | leftover = e; | |
650e2f67 JH |
1206 | } |
1207 | decref_split(split); | |
1208 | } | |
7e6ac6e4 DK |
1209 | *unblamedtail = NULL; |
1210 | toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score); | |
1211 | } while (unblamed); | |
1212 | target->suspects = reverse_blame(leftover, NULL); | |
d24bba80 JH |
1213 | } |
1214 | ||
33494784 JH |
1215 | struct blame_list { |
1216 | struct blame_entry *ent; | |
1217 | struct blame_entry split[3]; | |
1218 | }; | |
1219 | ||
1732a1fd JH |
1220 | /* |
1221 | * Count the number of entries the target is suspected for, | |
1222 | * and prepare a list of entry and the best split. | |
1223 | */ | |
7e6ac6e4 | 1224 | static struct blame_list *setup_blame_list(struct blame_entry *unblamed, |
33494784 JH |
1225 | int *num_ents_p) |
1226 | { | |
1227 | struct blame_entry *e; | |
1228 | int num_ents, i; | |
1229 | struct blame_list *blame_list = NULL; | |
1230 | ||
7e6ac6e4 DK |
1231 | for (e = unblamed, num_ents = 0; e; e = e->next) |
1232 | num_ents++; | |
33494784 JH |
1233 | if (num_ents) { |
1234 | blame_list = xcalloc(num_ents, sizeof(struct blame_list)); | |
7e6ac6e4 DK |
1235 | for (e = unblamed, i = 0; e; e = e->next) |
1236 | blame_list[i++].ent = e; | |
33494784 JH |
1237 | } |
1238 | *num_ents_p = num_ents; | |
1239 | return blame_list; | |
1240 | } | |
1241 | ||
1732a1fd JH |
1242 | /* |
1243 | * For lines target is suspected for, see if we can find code movement | |
1244 | * across file boundary from the parent commit. porigin is the path | |
1245 | * in the parent we already tried. | |
1246 | */ | |
7e6ac6e4 DK |
1247 | static void find_copy_in_parent(struct scoreboard *sb, |
1248 | struct blame_entry ***blamed, | |
1249 | struct blame_entry **toosmall, | |
1250 | struct origin *target, | |
1251 | struct commit *parent, | |
1252 | struct origin *porigin, | |
1253 | int opt) | |
18abd745 JH |
1254 | { |
1255 | struct diff_options diff_opts; | |
aec8fa1f | 1256 | int i, j; |
33494784 | 1257 | struct blame_list *blame_list; |
aec8fa1f | 1258 | int num_ents; |
7e6ac6e4 DK |
1259 | struct blame_entry *unblamed = target->suspects; |
1260 | struct blame_entry *leftover = NULL; | |
18abd745 | 1261 | |
7e6ac6e4 DK |
1262 | if (!unblamed) |
1263 | return; /* nothing remains for this target */ | |
18abd745 JH |
1264 | |
1265 | diff_setup(&diff_opts); | |
8f67f8ae | 1266 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
18abd745 JH |
1267 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
1268 | ||
28452655 | 1269 | diff_setup_done(&diff_opts); |
33494784 JH |
1270 | |
1271 | /* Try "find copies harder" on new path if requested; | |
1272 | * we do not want to use diffcore_rename() actually to | |
1273 | * match things up; find_copies_harder is set only to | |
1274 | * force diff_tree_sha1() to feed all filepairs to diff_queue, | |
1275 | * and this code needs to be after diff_setup_done(), which | |
1276 | * usually makes find-copies-harder imply copy detection. | |
1277 | */ | |
c63777c0 JH |
1278 | if ((opt & PICKAXE_BLAME_COPY_HARDEST) |
1279 | || ((opt & PICKAXE_BLAME_COPY_HARDER) | |
1280 | && (!porigin || strcmp(target->path, porigin->path)))) | |
8f67f8ae | 1281 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
33494784 | 1282 | |
f2fd0760 | 1283 | if (is_null_oid(&target->commit->object.oid)) |
ed1c9977 | 1284 | do_diff_cache(parent->tree->object.oid.hash, &diff_opts); |
1cfe7733 | 1285 | else |
ed1c9977 | 1286 | diff_tree_sha1(parent->tree->object.oid.hash, |
1287 | target->commit->tree->object.oid.hash, | |
1cfe7733 | 1288 | "", &diff_opts); |
18abd745 | 1289 | |
8f67f8ae | 1290 | if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER)) |
33494784 | 1291 | diffcore_std(&diff_opts); |
18abd745 | 1292 | |
7e6ac6e4 DK |
1293 | do { |
1294 | struct blame_entry **unblamedtail = &unblamed; | |
1295 | blame_list = setup_blame_list(unblamed, &num_ents); | |
33494784 JH |
1296 | |
1297 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
1298 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
1299 | struct origin *norigin; | |
1300 | mmfile_t file_p; | |
33494784 JH |
1301 | struct blame_entry this[3]; |
1302 | ||
1303 | if (!DIFF_FILE_VALID(p->one)) | |
1304 | continue; /* does not exist in parent */ | |
5209ac4d AG |
1305 | if (S_ISGITLINK(p->one->mode)) |
1306 | continue; /* ignore git links */ | |
33494784 JH |
1307 | if (porigin && !strcmp(p->one->path, porigin->path)) |
1308 | /* find_move already dealt with this path */ | |
1309 | continue; | |
1310 | ||
1311 | norigin = get_origin(sb, parent, p->one->path); | |
a7bcfa12 | 1312 | oidcpy(&norigin->blob_oid, &p->one->oid); |
90064710 | 1313 | norigin->mode = p->one->mode; |
3b8a12e8 | 1314 | fill_origin_blob(&sb->revs->diffopt, norigin, &file_p); |
33494784 JH |
1315 | if (!file_p.ptr) |
1316 | continue; | |
1317 | ||
1318 | for (j = 0; j < num_ents; j++) { | |
1319 | find_copy_in_blob(sb, blame_list[j].ent, | |
1320 | norigin, this, &file_p); | |
1321 | copy_split_if_better(sb, blame_list[j].split, | |
1322 | this); | |
1323 | decref_split(this); | |
1324 | } | |
33494784 JH |
1325 | origin_decref(norigin); |
1326 | } | |
18abd745 | 1327 | |
aec8fa1f | 1328 | for (j = 0; j < num_ents; j++) { |
33494784 JH |
1329 | struct blame_entry *split = blame_list[j].split; |
1330 | if (split[1].suspect && | |
1331 | blame_copy_score < ent_score(sb, &split[1])) { | |
7e6ac6e4 DK |
1332 | split_blame(blamed, &unblamedtail, split, |
1333 | blame_list[j].ent); | |
1334 | } else { | |
1335 | blame_list[j].ent->next = leftover; | |
1336 | leftover = blame_list[j].ent; | |
33494784 JH |
1337 | } |
1338 | decref_split(split); | |
18abd745 | 1339 | } |
33494784 | 1340 | free(blame_list); |
7e6ac6e4 DK |
1341 | *unblamedtail = NULL; |
1342 | toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score); | |
1343 | } while (unblamed); | |
1344 | target->suspects = reverse_blame(leftover, NULL); | |
33494784 | 1345 | diff_flush(&diff_opts); |
ed6e8038 | 1346 | clear_pathspec(&diff_opts.pathspec); |
18abd745 JH |
1347 | } |
1348 | ||
1732a1fd JH |
1349 | /* |
1350 | * The blobs of origin and porigin exactly match, so everything | |
c2e525d9 JH |
1351 | * origin is suspected for can be blamed on the parent. |
1352 | */ | |
1353 | static void pass_whole_blame(struct scoreboard *sb, | |
1354 | struct origin *origin, struct origin *porigin) | |
1355 | { | |
7e6ac6e4 | 1356 | struct blame_entry *e, *suspects; |
c2e525d9 JH |
1357 | |
1358 | if (!porigin->file.ptr && origin->file.ptr) { | |
1359 | /* Steal its file */ | |
1360 | porigin->file = origin->file; | |
1361 | origin->file.ptr = NULL; | |
1362 | } | |
7e6ac6e4 DK |
1363 | suspects = origin->suspects; |
1364 | origin->suspects = NULL; | |
1365 | for (e = suspects; e; e = e->next) { | |
c2e525d9 JH |
1366 | origin_incref(porigin); |
1367 | origin_decref(e->suspect); | |
1368 | e->suspect = porigin; | |
1369 | } | |
7e6ac6e4 | 1370 | queue_blames(sb, porigin, suspects); |
c2e525d9 JH |
1371 | } |
1372 | ||
69264f46 JH |
1373 | /* |
1374 | * We pass blame from the current commit to its parents. We keep saying | |
1375 | * "parent" (and "porigin"), but what we mean is to find scapegoat to | |
1376 | * exonerate ourselves. | |
1377 | */ | |
85af7929 | 1378 | static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit) |
69264f46 | 1379 | { |
95a4fb0e JK |
1380 | if (!reverse) { |
1381 | if (revs->first_parent_only && | |
1382 | commit->parents && | |
1383 | commit->parents->next) { | |
1384 | free_commit_list(commit->parents->next); | |
1385 | commit->parents->next = NULL; | |
1386 | } | |
85af7929 | 1387 | return commit->parents; |
95a4fb0e | 1388 | } |
85af7929 | 1389 | return lookup_decoration(&revs->children, &commit->object); |
69264f46 JH |
1390 | } |
1391 | ||
85af7929 | 1392 | static int num_scapegoats(struct rev_info *revs, struct commit *commit) |
69264f46 | 1393 | { |
85af7929 | 1394 | struct commit_list *l = first_scapegoat(revs, commit); |
4bbaa1eb | 1395 | return commit_list_count(l); |
69264f46 JH |
1396 | } |
1397 | ||
7e6ac6e4 DK |
1398 | /* Distribute collected unsorted blames to the respected sorted lists |
1399 | * in the various origins. | |
1400 | */ | |
1401 | static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed) | |
1402 | { | |
1403 | blamed = blame_sort(blamed, compare_blame_suspect); | |
1404 | while (blamed) | |
1405 | { | |
1406 | struct origin *porigin = blamed->suspect; | |
1407 | struct blame_entry *suspects = NULL; | |
1408 | do { | |
1409 | struct blame_entry *next = blamed->next; | |
1410 | blamed->next = suspects; | |
1411 | suspects = blamed; | |
1412 | blamed = next; | |
1413 | } while (blamed && blamed->suspect == porigin); | |
1414 | suspects = reverse_blame(suspects, NULL); | |
1415 | queue_blames(sb, porigin, suspects); | |
1416 | } | |
1417 | } | |
1418 | ||
69264f46 | 1419 | #define MAXSG 16 |
cee7f245 | 1420 | |
d24bba80 | 1421 | static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt) |
cee7f245 | 1422 | { |
85af7929 | 1423 | struct rev_info *revs = sb->revs; |
69264f46 | 1424 | int i, pass, num_sg; |
cee7f245 | 1425 | struct commit *commit = origin->commit; |
69264f46 JH |
1426 | struct commit_list *sg; |
1427 | struct origin *sg_buf[MAXSG]; | |
1428 | struct origin *porigin, **sg_origin = sg_buf; | |
7e6ac6e4 DK |
1429 | struct blame_entry *toosmall = NULL; |
1430 | struct blame_entry *blames, **blametail = &blames; | |
69264f46 | 1431 | |
85af7929 | 1432 | num_sg = num_scapegoats(revs, commit); |
69264f46 JH |
1433 | if (!num_sg) |
1434 | goto finish; | |
1435 | else if (num_sg < ARRAY_SIZE(sg_buf)) | |
1436 | memset(sg_buf, 0, sizeof(sg_buf)); | |
1437 | else | |
1438 | sg_origin = xcalloc(num_sg, sizeof(*sg_origin)); | |
cee7f245 | 1439 | |
69264f46 JH |
1440 | /* |
1441 | * The first pass looks for unrenamed path to optimize for | |
f69e743d JH |
1442 | * common cases, then we look for renames in the second pass. |
1443 | */ | |
3d1aa566 | 1444 | for (pass = 0; pass < 2 - no_whole_file_rename; pass++) { |
f69e743d JH |
1445 | struct origin *(*find)(struct scoreboard *, |
1446 | struct commit *, struct origin *); | |
1447 | find = pass ? find_rename : find_origin; | |
1448 | ||
85af7929 | 1449 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1450 | i < num_sg && sg; |
1451 | sg = sg->next, i++) { | |
1452 | struct commit *p = sg->item; | |
0421d9f8 | 1453 | int j, same; |
f69e743d | 1454 | |
69264f46 | 1455 | if (sg_origin[i]) |
f69e743d JH |
1456 | continue; |
1457 | if (parse_commit(p)) | |
1458 | continue; | |
0d981c67 | 1459 | porigin = find(sb, p, origin); |
f69e743d JH |
1460 | if (!porigin) |
1461 | continue; | |
a7bcfa12 | 1462 | if (!oidcmp(&porigin->blob_oid, &origin->blob_oid)) { |
c2e525d9 | 1463 | pass_whole_blame(sb, origin, porigin); |
f69e743d JH |
1464 | origin_decref(porigin); |
1465 | goto finish; | |
1466 | } | |
0421d9f8 | 1467 | for (j = same = 0; j < i; j++) |
69264f46 | 1468 | if (sg_origin[j] && |
a7bcfa12 | 1469 | !oidcmp(&sg_origin[j]->blob_oid, &porigin->blob_oid)) { |
0421d9f8 JH |
1470 | same = 1; |
1471 | break; | |
1472 | } | |
1473 | if (!same) | |
69264f46 | 1474 | sg_origin[i] = porigin; |
0421d9f8 JH |
1475 | else |
1476 | origin_decref(porigin); | |
cee7f245 | 1477 | } |
cee7f245 JH |
1478 | } |
1479 | ||
c2e525d9 | 1480 | num_commits++; |
85af7929 | 1481 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1482 | i < num_sg && sg; |
1483 | sg = sg->next, i++) { | |
1484 | struct origin *porigin = sg_origin[i]; | |
cee7f245 JH |
1485 | if (!porigin) |
1486 | continue; | |
96e11709 JH |
1487 | if (!origin->previous) { |
1488 | origin_incref(porigin); | |
1489 | origin->previous = porigin; | |
1490 | } | |
7e6ac6e4 DK |
1491 | pass_blame_to_parent(sb, origin, porigin); |
1492 | if (!origin->suspects) | |
54a4c617 | 1493 | goto finish; |
cee7f245 | 1494 | } |
d24bba80 JH |
1495 | |
1496 | /* | |
1732a1fd | 1497 | * Optionally find moves in parents' files. |
d24bba80 | 1498 | */ |
7e6ac6e4 DK |
1499 | if (opt & PICKAXE_BLAME_MOVE) { |
1500 | filter_small(sb, &toosmall, &origin->suspects, blame_move_score); | |
1501 | if (origin->suspects) { | |
1502 | for (i = 0, sg = first_scapegoat(revs, commit); | |
1503 | i < num_sg && sg; | |
1504 | sg = sg->next, i++) { | |
1505 | struct origin *porigin = sg_origin[i]; | |
1506 | if (!porigin) | |
1507 | continue; | |
1508 | find_move_in_parent(sb, &blametail, &toosmall, origin, porigin); | |
1509 | if (!origin->suspects) | |
1510 | break; | |
1511 | } | |
d24bba80 | 1512 | } |
7e6ac6e4 | 1513 | } |
d24bba80 | 1514 | |
18abd745 | 1515 | /* |
1732a1fd | 1516 | * Optionally find copies from parents' files. |
18abd745 | 1517 | */ |
7e6ac6e4 DK |
1518 | if (opt & PICKAXE_BLAME_COPY) { |
1519 | if (blame_copy_score > blame_move_score) | |
1520 | filter_small(sb, &toosmall, &origin->suspects, blame_copy_score); | |
1521 | else if (blame_copy_score < blame_move_score) { | |
1522 | origin->suspects = blame_merge(origin->suspects, toosmall); | |
1523 | toosmall = NULL; | |
1524 | filter_small(sb, &toosmall, &origin->suspects, blame_copy_score); | |
1525 | } | |
1526 | if (!origin->suspects) | |
1527 | goto finish; | |
1528 | ||
85af7929 | 1529 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1530 | i < num_sg && sg; |
1531 | sg = sg->next, i++) { | |
1532 | struct origin *porigin = sg_origin[i]; | |
7e6ac6e4 DK |
1533 | find_copy_in_parent(sb, &blametail, &toosmall, |
1534 | origin, sg->item, porigin, opt); | |
1535 | if (!origin->suspects) | |
54a4c617 | 1536 | goto finish; |
18abd745 | 1537 | } |
7e6ac6e4 | 1538 | } |
54a4c617 | 1539 | |
7e6ac6e4 DK |
1540 | finish: |
1541 | *blametail = NULL; | |
1542 | distribute_blame(sb, blames); | |
1543 | /* | |
1544 | * prepend toosmall to origin->suspects | |
1545 | * | |
1546 | * There is no point in sorting: this ends up on a big | |
1547 | * unsorted list in the caller anyway. | |
1548 | */ | |
1549 | if (toosmall) { | |
1550 | struct blame_entry **tail = &toosmall; | |
1551 | while (*tail) | |
1552 | tail = &(*tail)->next; | |
1553 | *tail = origin->suspects; | |
1554 | origin->suspects = toosmall; | |
1555 | } | |
69264f46 JH |
1556 | for (i = 0; i < num_sg; i++) { |
1557 | if (sg_origin[i]) { | |
1558 | drop_origin_blob(sg_origin[i]); | |
1559 | origin_decref(sg_origin[i]); | |
7c3c7962 JH |
1560 | } |
1561 | } | |
1562 | drop_origin_blob(origin); | |
69264f46 JH |
1563 | if (sg_buf != sg_origin) |
1564 | free(sg_origin); | |
cee7f245 JH |
1565 | } |
1566 | ||
1732a1fd JH |
1567 | /* |
1568 | * Information on commits, used for output. | |
1569 | */ | |
9cba13ca | 1570 | struct commit_info { |
ea02ffa3 AP |
1571 | struct strbuf author; |
1572 | struct strbuf author_mail; | |
cee7f245 | 1573 | unsigned long author_time; |
ea02ffa3 | 1574 | struct strbuf author_tz; |
cee7f245 JH |
1575 | |
1576 | /* filled only when asked for details */ | |
ea02ffa3 AP |
1577 | struct strbuf committer; |
1578 | struct strbuf committer_mail; | |
cee7f245 | 1579 | unsigned long committer_time; |
ea02ffa3 | 1580 | struct strbuf committer_tz; |
cee7f245 | 1581 | |
ea02ffa3 | 1582 | struct strbuf summary; |
cee7f245 JH |
1583 | }; |
1584 | ||
1732a1fd JH |
1585 | /* |
1586 | * Parse author/committer line in the commit object buffer | |
1587 | */ | |
cee7f245 | 1588 | static void get_ac_line(const char *inbuf, const char *what, |
ea02ffa3 AP |
1589 | struct strbuf *name, struct strbuf *mail, |
1590 | unsigned long *time, struct strbuf *tz) | |
cee7f245 | 1591 | { |
3c020bd5 | 1592 | struct ident_split ident; |
ea02ffa3 AP |
1593 | size_t len, maillen, namelen; |
1594 | char *tmp, *endp; | |
1595 | const char *namebuf, *mailbuf; | |
cee7f245 JH |
1596 | |
1597 | tmp = strstr(inbuf, what); | |
1598 | if (!tmp) | |
1599 | goto error_out; | |
1600 | tmp += strlen(what); | |
1601 | endp = strchr(tmp, '\n'); | |
1602 | if (!endp) | |
1603 | len = strlen(tmp); | |
1604 | else | |
1605 | len = endp - tmp; | |
3c020bd5 AP |
1606 | |
1607 | if (split_ident_line(&ident, tmp, len)) { | |
cee7f245 JH |
1608 | error_out: |
1609 | /* Ugh */ | |
ea02ffa3 AP |
1610 | tmp = "(unknown)"; |
1611 | strbuf_addstr(name, tmp); | |
1612 | strbuf_addstr(mail, tmp); | |
1613 | strbuf_addstr(tz, tmp); | |
cee7f245 JH |
1614 | *time = 0; |
1615 | return; | |
1616 | } | |
cee7f245 | 1617 | |
3c020bd5 | 1618 | namelen = ident.name_end - ident.name_begin; |
ea02ffa3 | 1619 | namebuf = ident.name_begin; |
cee7f245 | 1620 | |
ea02ffa3 AP |
1621 | maillen = ident.mail_end - ident.mail_begin; |
1622 | mailbuf = ident.mail_begin; | |
f9567384 | 1623 | |
de5abe9f RS |
1624 | if (ident.date_begin && ident.date_end) |
1625 | *time = strtoul(ident.date_begin, NULL, 10); | |
1626 | else | |
1627 | *time = 0; | |
f9567384 | 1628 | |
de5abe9f RS |
1629 | if (ident.tz_begin && ident.tz_end) |
1630 | strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin); | |
1631 | else | |
1632 | strbuf_addstr(tz, "(unknown)"); | |
f9567384 | 1633 | |
f9567384 | 1634 | /* |
d20d654f | 1635 | * Now, convert both name and e-mail using mailmap |
f9567384 | 1636 | */ |
ea02ffa3 AP |
1637 | map_user(&mailmap, &mailbuf, &maillen, |
1638 | &namebuf, &namelen); | |
1639 | ||
1640 | strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf); | |
1641 | strbuf_add(name, namebuf, namelen); | |
1642 | } | |
1643 | ||
1644 | static void commit_info_init(struct commit_info *ci) | |
1645 | { | |
1646 | ||
1647 | strbuf_init(&ci->author, 0); | |
1648 | strbuf_init(&ci->author_mail, 0); | |
1649 | strbuf_init(&ci->author_tz, 0); | |
1650 | strbuf_init(&ci->committer, 0); | |
1651 | strbuf_init(&ci->committer_mail, 0); | |
1652 | strbuf_init(&ci->committer_tz, 0); | |
1653 | strbuf_init(&ci->summary, 0); | |
1654 | } | |
1655 | ||
1656 | static void commit_info_destroy(struct commit_info *ci) | |
1657 | { | |
1658 | ||
1659 | strbuf_release(&ci->author); | |
1660 | strbuf_release(&ci->author_mail); | |
1661 | strbuf_release(&ci->author_tz); | |
1662 | strbuf_release(&ci->committer); | |
1663 | strbuf_release(&ci->committer_mail); | |
1664 | strbuf_release(&ci->committer_tz); | |
1665 | strbuf_release(&ci->summary); | |
cee7f245 JH |
1666 | } |
1667 | ||
1668 | static void get_commit_info(struct commit *commit, | |
1669 | struct commit_info *ret, | |
1670 | int detailed) | |
1671 | { | |
1672 | int len; | |
e297cf5a | 1673 | const char *subject, *encoding; |
b000c59b | 1674 | const char *message; |
ea02ffa3 AP |
1675 | |
1676 | commit_info_init(ret); | |
cee7f245 | 1677 | |
e297cf5a | 1678 | encoding = get_log_output_encoding(); |
5a10d236 | 1679 | message = logmsg_reencode(commit, NULL, encoding); |
69cd8f63 | 1680 | get_ac_line(message, "\nauthor ", |
ea02ffa3 | 1681 | &ret->author, &ret->author_mail, |
cee7f245 JH |
1682 | &ret->author_time, &ret->author_tz); |
1683 | ||
69cd8f63 | 1684 | if (!detailed) { |
b66103c3 | 1685 | unuse_commit_buffer(commit, message); |
cee7f245 | 1686 | return; |
69cd8f63 | 1687 | } |
cee7f245 | 1688 | |
69cd8f63 | 1689 | get_ac_line(message, "\ncommitter ", |
ea02ffa3 | 1690 | &ret->committer, &ret->committer_mail, |
cee7f245 JH |
1691 | &ret->committer_time, &ret->committer_tz); |
1692 | ||
ad98a58b | 1693 | len = find_commit_subject(message, &subject); |
ea02ffa3 AP |
1694 | if (len) |
1695 | strbuf_add(&ret->summary, subject, len); | |
1696 | else | |
f2fd0760 | 1697 | strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid)); |
ea02ffa3 | 1698 | |
b66103c3 | 1699 | unuse_commit_buffer(commit, message); |
cee7f245 JH |
1700 | } |
1701 | ||
1732a1fd | 1702 | /* |
4e768329 JK |
1703 | * Write out any suspect information which depends on the path. This must be |
1704 | * handled separately from emit_one_suspect_detail(), because a given commit | |
1705 | * may have changes in multiple paths. So this needs to appear each time | |
1706 | * we mention a new group. | |
1707 | * | |
1732a1fd JH |
1708 | * To allow LF and other nonportable characters in pathnames, |
1709 | * they are c-style quoted as needed. | |
1710 | */ | |
4e768329 | 1711 | static void write_filename_info(struct origin *suspect) |
46e5e69d | 1712 | { |
4e768329 JK |
1713 | if (suspect->previous) { |
1714 | struct origin *prev = suspect->previous; | |
1715 | printf("previous %s ", oid_to_hex(&prev->commit->object.oid)); | |
1716 | write_name_quoted(prev->path, stdout, '\n'); | |
1717 | } | |
46e5e69d | 1718 | printf("filename "); |
4e768329 | 1719 | write_name_quoted(suspect->path, stdout, '\n'); |
46e5e69d JH |
1720 | } |
1721 | ||
9991030c JH |
1722 | /* |
1723 | * Porcelain/Incremental format wants to show a lot of details per | |
1724 | * commit. Instead of repeating this every line, emit it only once, | |
e86226e3 JK |
1725 | * the first time each commit appears in the output (unless the |
1726 | * user has specifically asked for us to repeat). | |
9991030c | 1727 | */ |
e86226e3 | 1728 | static int emit_one_suspect_detail(struct origin *suspect, int repeat) |
9991030c JH |
1729 | { |
1730 | struct commit_info ci; | |
1731 | ||
e86226e3 | 1732 | if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN)) |
9991030c JH |
1733 | return 0; |
1734 | ||
1735 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
1736 | get_commit_info(suspect->commit, &ci, 1); | |
ea02ffa3 AP |
1737 | printf("author %s\n", ci.author.buf); |
1738 | printf("author-mail %s\n", ci.author_mail.buf); | |
9991030c | 1739 | printf("author-time %lu\n", ci.author_time); |
ea02ffa3 AP |
1740 | printf("author-tz %s\n", ci.author_tz.buf); |
1741 | printf("committer %s\n", ci.committer.buf); | |
1742 | printf("committer-mail %s\n", ci.committer_mail.buf); | |
9991030c | 1743 | printf("committer-time %lu\n", ci.committer_time); |
ea02ffa3 AP |
1744 | printf("committer-tz %s\n", ci.committer_tz.buf); |
1745 | printf("summary %s\n", ci.summary.buf); | |
9991030c JH |
1746 | if (suspect->commit->object.flags & UNINTERESTING) |
1747 | printf("boundary\n"); | |
ea02ffa3 AP |
1748 | |
1749 | commit_info_destroy(&ci); | |
1750 | ||
9991030c JH |
1751 | return 1; |
1752 | } | |
1753 | ||
1732a1fd | 1754 | /* |
7e6ac6e4 DK |
1755 | * The blame_entry is found to be guilty for the range. |
1756 | * Show it in incremental output. | |
1732a1fd | 1757 | */ |
aba37f49 ECA |
1758 | static void found_guilty_entry(struct blame_entry *ent, |
1759 | struct progress_info *pi) | |
717d1462 | 1760 | { |
717d1462 LT |
1761 | if (incremental) { |
1762 | struct origin *suspect = ent->suspect; | |
1763 | ||
1764 | printf("%s %d %d %d\n", | |
f2fd0760 | 1765 | oid_to_hex(&suspect->commit->object.oid), |
717d1462 | 1766 | ent->s_lno + 1, ent->lno + 1, ent->num_lines); |
e86226e3 | 1767 | emit_one_suspect_detail(suspect, 0); |
4e768329 | 1768 | write_filename_info(suspect); |
06f59e9f | 1769 | maybe_flush_or_die(stdout, "stdout"); |
717d1462 | 1770 | } |
aba37f49 ECA |
1771 | pi->blamed_lines += ent->num_lines; |
1772 | display_progress(pi->progress, pi->blamed_lines); | |
717d1462 LT |
1773 | } |
1774 | ||
1732a1fd | 1775 | /* |
7e6ac6e4 DK |
1776 | * The main loop -- while we have blobs with lines whose true origin |
1777 | * is still unknown, pick one blob, and allow its lines to pass blames | |
1778 | * to its parents. */ | |
85af7929 | 1779 | static void assign_blame(struct scoreboard *sb, int opt) |
717d1462 | 1780 | { |
85af7929 | 1781 | struct rev_info *revs = sb->revs; |
7e6ac6e4 | 1782 | struct commit *commit = prio_queue_get(&sb->commits); |
aba37f49 ECA |
1783 | struct progress_info pi = { NULL, 0 }; |
1784 | ||
1785 | if (show_progress) | |
1786 | pi.progress = start_progress_delay(_("Blaming lines"), | |
1787 | sb->num_lines, 50, 1); | |
85af7929 | 1788 | |
7e6ac6e4 | 1789 | while (commit) { |
717d1462 | 1790 | struct blame_entry *ent; |
7e6ac6e4 | 1791 | struct origin *suspect = commit->util; |
717d1462 LT |
1792 | |
1793 | /* find one suspect to break down */ | |
7e6ac6e4 DK |
1794 | while (suspect && !suspect->suspects) |
1795 | suspect = suspect->next; | |
1796 | ||
1797 | if (!suspect) { | |
1798 | commit = prio_queue_get(&sb->commits); | |
1799 | continue; | |
1800 | } | |
1801 | ||
1802 | assert(commit == suspect->commit); | |
717d1462 | 1803 | |
1732a1fd JH |
1804 | /* |
1805 | * We will use this suspect later in the loop, | |
1806 | * so hold onto it in the meantime. | |
1807 | */ | |
717d1462 | 1808 | origin_incref(suspect); |
0064053b | 1809 | parse_commit(commit); |
85af7929 JH |
1810 | if (reverse || |
1811 | (!(commit->object.flags & UNINTERESTING) && | |
1812 | !(revs->max_age != -1 && commit->date < revs->max_age))) | |
717d1462 LT |
1813 | pass_blame(sb, suspect, opt); |
1814 | else { | |
1815 | commit->object.flags |= UNINTERESTING; | |
1816 | if (commit->object.parsed) | |
1817 | mark_parents_uninteresting(commit); | |
1818 | } | |
1819 | /* treat root commit as boundary */ | |
1820 | if (!commit->parents && !show_root) | |
1821 | commit->object.flags |= UNINTERESTING; | |
1822 | ||
1823 | /* Take responsibility for the remaining entries */ | |
7e6ac6e4 DK |
1824 | ent = suspect->suspects; |
1825 | if (ent) { | |
1826 | suspect->guilty = 1; | |
1827 | for (;;) { | |
1828 | struct blame_entry *next = ent->next; | |
aba37f49 | 1829 | found_guilty_entry(ent, &pi); |
7e6ac6e4 DK |
1830 | if (next) { |
1831 | ent = next; | |
1832 | continue; | |
1833 | } | |
1834 | ent->next = sb->ent; | |
1835 | sb->ent = suspect->suspects; | |
1836 | suspect->suspects = NULL; | |
1837 | break; | |
1838 | } | |
1839 | } | |
717d1462 LT |
1840 | origin_decref(suspect); |
1841 | ||
1842 | if (DEBUG) /* sanity */ | |
1843 | sanity_check_refcnt(sb); | |
1844 | } | |
aba37f49 ECA |
1845 | |
1846 | stop_progress(&pi.progress); | |
717d1462 LT |
1847 | } |
1848 | ||
1849 | static const char *format_time(unsigned long time, const char *tz_str, | |
1850 | int show_raw_time) | |
1851 | { | |
bccce0f8 | 1852 | static struct strbuf time_buf = STRBUF_INIT; |
717d1462 | 1853 | |
bccce0f8 | 1854 | strbuf_reset(&time_buf); |
717d1462 | 1855 | if (show_raw_time) { |
bccce0f8 | 1856 | strbuf_addf(&time_buf, "%lu %s", time, tz_str); |
717d1462 | 1857 | } |
31653c1a | 1858 | else { |
ac39b277 | 1859 | const char *time_str; |
bccce0f8 | 1860 | size_t time_width; |
ac39b277 | 1861 | int tz; |
31653c1a | 1862 | tz = atoi(tz_str); |
a5481a6c | 1863 | time_str = show_date(time, tz, &blame_date_mode); |
bccce0f8 JX |
1864 | strbuf_addstr(&time_buf, time_str); |
1865 | /* | |
1866 | * Add space paddings to time_buf to display a fixed width | |
1867 | * string, and use time_width for display width calibration. | |
1868 | */ | |
1869 | for (time_width = utf8_strwidth(time_str); | |
1870 | time_width < blame_date_width; | |
1871 | time_width++) | |
1872 | strbuf_addch(&time_buf, ' '); | |
31653c1a | 1873 | } |
bccce0f8 | 1874 | return time_buf.buf; |
717d1462 LT |
1875 | } |
1876 | ||
cee7f245 JH |
1877 | #define OUTPUT_ANNOTATE_COMPAT 001 |
1878 | #define OUTPUT_LONG_OBJECT_NAME 002 | |
1879 | #define OUTPUT_RAW_TIMESTAMP 004 | |
1880 | #define OUTPUT_PORCELAIN 010 | |
1881 | #define OUTPUT_SHOW_NAME 020 | |
1882 | #define OUTPUT_SHOW_NUMBER 040 | |
5ff62c30 | 1883 | #define OUTPUT_SHOW_SCORE 0100 |
093dc5be | 1884 | #define OUTPUT_NO_AUTHOR 0200 |
1b8cdce9 | 1885 | #define OUTPUT_SHOW_EMAIL 0400 |
ed747dd5 | 1886 | #define OUTPUT_LINE_PORCELAIN 01000 |
cee7f245 | 1887 | |
e86226e3 JK |
1888 | static void emit_porcelain_details(struct origin *suspect, int repeat) |
1889 | { | |
1890 | if (emit_one_suspect_detail(suspect, repeat) || | |
1891 | (suspect->commit->object.flags & MORE_THAN_ONE_PATH)) | |
4e768329 | 1892 | write_filename_info(suspect); |
e86226e3 JK |
1893 | } |
1894 | ||
1895 | static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent, | |
1896 | int opt) | |
cee7f245 | 1897 | { |
ed747dd5 | 1898 | int repeat = opt & OUTPUT_LINE_PORCELAIN; |
cee7f245 JH |
1899 | int cnt; |
1900 | const char *cp; | |
1901 | struct origin *suspect = ent->suspect; | |
d59f765a | 1902 | char hex[GIT_SHA1_HEXSZ + 1]; |
cee7f245 | 1903 | |
f2fd0760 | 1904 | sha1_to_hex_r(hex, suspect->commit->object.oid.hash); |
7e6ac6e4 | 1905 | printf("%s %d %d %d\n", |
cee7f245 | 1906 | hex, |
cee7f245 JH |
1907 | ent->s_lno + 1, |
1908 | ent->lno + 1, | |
1909 | ent->num_lines); | |
ed747dd5 | 1910 | emit_porcelain_details(suspect, repeat); |
cee7f245 JH |
1911 | |
1912 | cp = nth_line(sb, ent->lno); | |
1913 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
1914 | char ch; | |
ed747dd5 | 1915 | if (cnt) { |
cee7f245 JH |
1916 | printf("%s %d %d\n", hex, |
1917 | ent->s_lno + 1 + cnt, | |
1918 | ent->lno + 1 + cnt); | |
ed747dd5 JK |
1919 | if (repeat) |
1920 | emit_porcelain_details(suspect, 1); | |
1921 | } | |
cee7f245 JH |
1922 | putchar('\t'); |
1923 | do { | |
1924 | ch = *cp++; | |
1925 | putchar(ch); | |
1926 | } while (ch != '\n' && | |
1927 | cp < sb->final_buf + sb->final_buf_size); | |
1928 | } | |
a5ca8367 JS |
1929 | |
1930 | if (sb->final_buf_size && cp[-1] != '\n') | |
1931 | putchar('\n'); | |
cee7f245 JH |
1932 | } |
1933 | ||
1934 | static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt) | |
1935 | { | |
1936 | int cnt; | |
1937 | const char *cp; | |
1938 | struct origin *suspect = ent->suspect; | |
1939 | struct commit_info ci; | |
d59f765a | 1940 | char hex[GIT_SHA1_HEXSZ + 1]; |
cee7f245 JH |
1941 | int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); |
1942 | ||
1943 | get_commit_info(suspect->commit, &ci, 1); | |
f2fd0760 | 1944 | sha1_to_hex_r(hex, suspect->commit->object.oid.hash); |
cee7f245 JH |
1945 | |
1946 | cp = nth_line(sb, ent->lno); | |
1947 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
1948 | char ch; | |
110d26fc | 1949 | int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev; |
b11121d9 JH |
1950 | |
1951 | if (suspect->commit->object.flags & UNINTERESTING) { | |
e68989a7 JH |
1952 | if (blank_boundary) |
1953 | memset(hex, ' ', length); | |
7ceacdff | 1954 | else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) { |
4c10a5ca JH |
1955 | length--; |
1956 | putchar('^'); | |
1957 | } | |
b11121d9 | 1958 | } |
cee7f245 | 1959 | |
b11121d9 | 1960 | printf("%.*s", length, hex); |
1b8cdce9 KB |
1961 | if (opt & OUTPUT_ANNOTATE_COMPAT) { |
1962 | const char *name; | |
1963 | if (opt & OUTPUT_SHOW_EMAIL) | |
ea02ffa3 | 1964 | name = ci.author_mail.buf; |
1b8cdce9 | 1965 | else |
ea02ffa3 | 1966 | name = ci.author.buf; |
1b8cdce9 | 1967 | printf("\t(%10s\t%10s\t%d)", name, |
ea02ffa3 | 1968 | format_time(ci.author_time, ci.author_tz.buf, |
cee7f245 JH |
1969 | show_raw_time), |
1970 | ent->lno + 1 + cnt); | |
1b8cdce9 | 1971 | } else { |
5ff62c30 | 1972 | if (opt & OUTPUT_SHOW_SCORE) |
54a4c617 JH |
1973 | printf(" %*d %02d", |
1974 | max_score_digits, ent->score, | |
1975 | ent->suspect->refcnt); | |
cee7f245 JH |
1976 | if (opt & OUTPUT_SHOW_NAME) |
1977 | printf(" %-*.*s", longest_file, longest_file, | |
1978 | suspect->path); | |
1979 | if (opt & OUTPUT_SHOW_NUMBER) | |
1980 | printf(" %*d", max_orig_digits, | |
1981 | ent->s_lno + 1 + cnt); | |
093dc5be | 1982 | |
ffaf9cc0 | 1983 | if (!(opt & OUTPUT_NO_AUTHOR)) { |
1b8cdce9 KB |
1984 | const char *name; |
1985 | int pad; | |
1986 | if (opt & OUTPUT_SHOW_EMAIL) | |
ea02ffa3 | 1987 | name = ci.author_mail.buf; |
1b8cdce9 | 1988 | else |
ea02ffa3 | 1989 | name = ci.author.buf; |
1b8cdce9 | 1990 | pad = longest_author - utf8_strwidth(name); |
ffaf9cc0 | 1991 | printf(" (%s%*s %10s", |
1b8cdce9 | 1992 | name, pad, "", |
093dc5be | 1993 | format_time(ci.author_time, |
ea02ffa3 | 1994 | ci.author_tz.buf, |
093dc5be | 1995 | show_raw_time)); |
ffaf9cc0 | 1996 | } |
093dc5be | 1997 | printf(" %*d) ", |
cee7f245 JH |
1998 | max_digits, ent->lno + 1 + cnt); |
1999 | } | |
2000 | do { | |
2001 | ch = *cp++; | |
2002 | putchar(ch); | |
2003 | } while (ch != '\n' && | |
2004 | cp < sb->final_buf + sb->final_buf_size); | |
2005 | } | |
a5ca8367 JS |
2006 | |
2007 | if (sb->final_buf_size && cp[-1] != '\n') | |
2008 | putchar('\n'); | |
ea02ffa3 AP |
2009 | |
2010 | commit_info_destroy(&ci); | |
cee7f245 JH |
2011 | } |
2012 | ||
2013 | static void output(struct scoreboard *sb, int option) | |
2014 | { | |
2015 | struct blame_entry *ent; | |
2016 | ||
2017 | if (option & OUTPUT_PORCELAIN) { | |
2018 | for (ent = sb->ent; ent; ent = ent->next) { | |
7e6ac6e4 DK |
2019 | int count = 0; |
2020 | struct origin *suspect; | |
2021 | struct commit *commit = ent->suspect->commit; | |
cee7f245 JH |
2022 | if (commit->object.flags & MORE_THAN_ONE_PATH) |
2023 | continue; | |
7e6ac6e4 DK |
2024 | for (suspect = commit->util; suspect; suspect = suspect->next) { |
2025 | if (suspect->guilty && count++) { | |
2026 | commit->object.flags |= MORE_THAN_ONE_PATH; | |
2027 | break; | |
2028 | } | |
cee7f245 JH |
2029 | } |
2030 | } | |
2031 | } | |
2032 | ||
2033 | for (ent = sb->ent; ent; ent = ent->next) { | |
2034 | if (option & OUTPUT_PORCELAIN) | |
e86226e3 | 2035 | emit_porcelain(sb, ent, option); |
5ff62c30 | 2036 | else { |
cee7f245 | 2037 | emit_other(sb, ent, option); |
5ff62c30 | 2038 | } |
cee7f245 JH |
2039 | } |
2040 | } | |
2041 | ||
29aa0b20 RS |
2042 | static const char *get_next_line(const char *start, const char *end) |
2043 | { | |
2044 | const char *nl = memchr(start, '\n', end - start); | |
60d85e11 | 2045 | return nl ? nl + 1 : end; |
29aa0b20 RS |
2046 | } |
2047 | ||
1732a1fd JH |
2048 | /* |
2049 | * To allow quick access to the contents of nth line in the | |
2050 | * final image, prepare an index in the scoreboard. | |
2051 | */ | |
cee7f245 JH |
2052 | static int prepare_lines(struct scoreboard *sb) |
2053 | { | |
2054 | const char *buf = sb->final_buf; | |
2055 | unsigned long len = sb->final_buf_size; | |
352bbbd9 DK |
2056 | const char *end = buf + len; |
2057 | const char *p; | |
2058 | int *lineno; | |
60d85e11 | 2059 | int num = 0; |
352bbbd9 | 2060 | |
60d85e11 | 2061 | for (p = buf; p < end; p = get_next_line(p, end)) |
29aa0b20 | 2062 | num++; |
352bbbd9 | 2063 | |
b32fa95f JK |
2064 | ALLOC_ARRAY(sb->lineno, num + 1); |
2065 | lineno = sb->lineno; | |
352bbbd9 | 2066 | |
60d85e11 | 2067 | for (p = buf; p < end; p = get_next_line(p, end)) |
29aa0b20 | 2068 | *lineno++ = p - buf; |
352bbbd9 | 2069 | |
60d85e11 | 2070 | *lineno = len; |
352bbbd9 | 2071 | |
60d85e11 | 2072 | sb->num_lines = num; |
cee7f245 JH |
2073 | return sb->num_lines; |
2074 | } | |
2075 | ||
1732a1fd JH |
2076 | /* |
2077 | * Add phony grafts for use with -S; this is primarily to | |
34baebce | 2078 | * support git's cvsserver that wants to give a linear history |
1732a1fd JH |
2079 | * to its clients. |
2080 | */ | |
cee7f245 JH |
2081 | static int read_ancestry(const char *graft_file) |
2082 | { | |
2083 | FILE *fp = fopen(graft_file, "r"); | |
e228c173 | 2084 | struct strbuf buf = STRBUF_INIT; |
cee7f245 JH |
2085 | if (!fp) |
2086 | return -1; | |
e228c173 | 2087 | while (!strbuf_getwholeline(&buf, fp, '\n')) { |
cee7f245 | 2088 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
e228c173 | 2089 | struct commit_graft *graft = read_graft_line(buf.buf, buf.len); |
8eaf7986 JH |
2090 | if (graft) |
2091 | register_commit_graft(graft, 0); | |
cee7f245 JH |
2092 | } |
2093 | fclose(fp); | |
e228c173 | 2094 | strbuf_release(&buf); |
cee7f245 JH |
2095 | return 0; |
2096 | } | |
2097 | ||
b31272f7 JH |
2098 | static int update_auto_abbrev(int auto_abbrev, struct origin *suspect) |
2099 | { | |
ed1c9977 | 2100 | const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash, |
b31272f7 JH |
2101 | auto_abbrev); |
2102 | int len = strlen(uniq); | |
2103 | if (auto_abbrev < len) | |
2104 | return len; | |
2105 | return auto_abbrev; | |
2106 | } | |
2107 | ||
1732a1fd JH |
2108 | /* |
2109 | * How many columns do we need to show line numbers, authors, | |
2110 | * and filenames? | |
2111 | */ | |
cee7f245 JH |
2112 | static void find_alignment(struct scoreboard *sb, int *option) |
2113 | { | |
2114 | int longest_src_lines = 0; | |
2115 | int longest_dst_lines = 0; | |
5ff62c30 | 2116 | unsigned largest_score = 0; |
cee7f245 | 2117 | struct blame_entry *e; |
b31272f7 | 2118 | int compute_auto_abbrev = (abbrev < 0); |
5293284b | 2119 | int auto_abbrev = DEFAULT_ABBREV; |
cee7f245 JH |
2120 | |
2121 | for (e = sb->ent; e; e = e->next) { | |
2122 | struct origin *suspect = e->suspect; | |
cee7f245 JH |
2123 | int num; |
2124 | ||
b31272f7 JH |
2125 | if (compute_auto_abbrev) |
2126 | auto_abbrev = update_auto_abbrev(auto_abbrev, suspect); | |
ab3bb800 JH |
2127 | if (strcmp(suspect->path, sb->path)) |
2128 | *option |= OUTPUT_SHOW_NAME; | |
2129 | num = strlen(suspect->path); | |
2130 | if (longest_file < num) | |
2131 | longest_file = num; | |
cee7f245 | 2132 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { |
e6005927 | 2133 | struct commit_info ci; |
cee7f245 JH |
2134 | suspect->commit->object.flags |= METAINFO_SHOWN; |
2135 | get_commit_info(suspect->commit, &ci, 1); | |
1b8cdce9 | 2136 | if (*option & OUTPUT_SHOW_EMAIL) |
ea02ffa3 | 2137 | num = utf8_strwidth(ci.author_mail.buf); |
1b8cdce9 | 2138 | else |
ea02ffa3 | 2139 | num = utf8_strwidth(ci.author.buf); |
cee7f245 JH |
2140 | if (longest_author < num) |
2141 | longest_author = num; | |
e6005927 | 2142 | commit_info_destroy(&ci); |
cee7f245 JH |
2143 | } |
2144 | num = e->s_lno + e->num_lines; | |
2145 | if (longest_src_lines < num) | |
2146 | longest_src_lines = num; | |
2147 | num = e->lno + e->num_lines; | |
2148 | if (longest_dst_lines < num) | |
2149 | longest_dst_lines = num; | |
5ff62c30 JH |
2150 | if (largest_score < ent_score(sb, e)) |
2151 | largest_score = ent_score(sb, e); | |
cee7f245 | 2152 | } |
ec7ff5ba ZJS |
2153 | max_orig_digits = decimal_width(longest_src_lines); |
2154 | max_digits = decimal_width(longest_dst_lines); | |
2155 | max_score_digits = decimal_width(largest_score); | |
b31272f7 JH |
2156 | |
2157 | if (compute_auto_abbrev) | |
2158 | /* one more abbrev length is needed for the boundary commit */ | |
2159 | abbrev = auto_abbrev + 1; | |
cee7f245 JH |
2160 | } |
2161 | ||
1732a1fd JH |
2162 | /* |
2163 | * For debugging -- origin is refcounted, and this asserts that | |
2164 | * we do not underflow. | |
2165 | */ | |
54a4c617 JH |
2166 | static void sanity_check_refcnt(struct scoreboard *sb) |
2167 | { | |
2168 | int baa = 0; | |
2169 | struct blame_entry *ent; | |
2170 | ||
2171 | for (ent = sb->ent; ent; ent = ent->next) { | |
ae86ad65 | 2172 | /* Nobody should have zero or negative refcnt */ |
854b97f6 JH |
2173 | if (ent->suspect->refcnt <= 0) { |
2174 | fprintf(stderr, "%s in %s has negative refcnt %d\n", | |
2175 | ent->suspect->path, | |
f2fd0760 | 2176 | oid_to_hex(&ent->suspect->commit->object.oid), |
854b97f6 | 2177 | ent->suspect->refcnt); |
ae86ad65 | 2178 | baa = 1; |
854b97f6 | 2179 | } |
ae86ad65 | 2180 | } |
54a4c617 JH |
2181 | if (baa) { |
2182 | int opt = 0160; | |
2183 | find_alignment(sb, &opt); | |
2184 | output(sb, opt); | |
854b97f6 | 2185 | die("Baa %d!", baa); |
54a4c617 JH |
2186 | } |
2187 | } | |
2188 | ||
4a0fc95f JH |
2189 | static unsigned parse_score(const char *arg) |
2190 | { | |
2191 | char *end; | |
2192 | unsigned long score = strtoul(arg, &end, 10); | |
2193 | if (*end) | |
2194 | return 0; | |
2195 | return score; | |
2196 | } | |
2197 | ||
20239bae JK |
2198 | static const char *add_prefix(const char *prefix, const char *path) |
2199 | { | |
097971f5 | 2200 | return prefix_path(prefix, prefix ? strlen(prefix) : 0, path); |
20239bae JK |
2201 | } |
2202 | ||
ef90d6d4 | 2203 | static int git_blame_config(const char *var, const char *value, void *cb) |
4c10a5ca JH |
2204 | { |
2205 | if (!strcmp(var, "blame.showroot")) { | |
2206 | show_root = git_config_bool(var, value); | |
2207 | return 0; | |
2208 | } | |
2209 | if (!strcmp(var, "blame.blankboundary")) { | |
2210 | blank_boundary = git_config_bool(var, value); | |
2211 | return 0; | |
2212 | } | |
8b504db3 QN |
2213 | if (!strcmp(var, "blame.showemail")) { |
2214 | int *output_option = cb; | |
2215 | if (git_config_bool(var, value)) | |
2216 | *output_option |= OUTPUT_SHOW_EMAIL; | |
2217 | else | |
2218 | *output_option &= ~OUTPUT_SHOW_EMAIL; | |
2219 | return 0; | |
2220 | } | |
31653c1a EL |
2221 | if (!strcmp(var, "blame.date")) { |
2222 | if (!value) | |
2223 | return config_error_nonbool(var); | |
a5481a6c | 2224 | parse_date_format(value, &blame_date_mode); |
31653c1a EL |
2225 | return 0; |
2226 | } | |
3b8a12e8 | 2227 | |
5b162879 MH |
2228 | if (git_diff_heuristic_config(var, value, cb) < 0) |
2229 | return -1; | |
6680a087 | 2230 | if (userdiff_config(var, value) < 0) |
3b8a12e8 | 2231 | return -1; |
3b8a12e8 | 2232 | |
ef90d6d4 | 2233 | return git_default_config(var, value, cb); |
4c10a5ca JH |
2234 | } |
2235 | ||
9aeaab68 | 2236 | static void verify_working_tree_path(struct commit *work_tree, const char *path) |
ffcabccf | 2237 | { |
9aeaab68 | 2238 | struct commit_list *parents; |
3b75ee93 | 2239 | int pos; |
ffcabccf | 2240 | |
9aeaab68 | 2241 | for (parents = work_tree->parents; parents; parents = parents->next) { |
110d26fc | 2242 | const struct object_id *commit_oid = &parents->item->object.oid; |
2243 | struct object_id blob_oid; | |
9aeaab68 JH |
2244 | unsigned mode; |
2245 | ||
110d26fc | 2246 | if (!get_tree_entry(commit_oid->hash, path, blob_oid.hash, &mode) && |
2247 | sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB) | |
9aeaab68 JH |
2248 | return; |
2249 | } | |
3b75ee93 MH |
2250 | |
2251 | pos = cache_name_pos(path, strlen(path)); | |
2252 | if (pos >= 0) | |
2253 | ; /* path is in the index */ | |
bc6b13a7 TG |
2254 | else if (-1 - pos < active_nr && |
2255 | !strcmp(active_cache[-1 - pos]->name, path)) | |
3b75ee93 MH |
2256 | ; /* path is in the index, unmerged */ |
2257 | else | |
2258 | die("no such path '%s' in HEAD", path); | |
9aeaab68 JH |
2259 | } |
2260 | ||
110d26fc | 2261 | static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid) |
9aeaab68 JH |
2262 | { |
2263 | struct commit *parent; | |
2264 | ||
110d26fc | 2265 | parent = lookup_commit_reference(oid->hash); |
9aeaab68 | 2266 | if (!parent) |
110d26fc | 2267 | die("no such commit %s", oid_to_hex(oid)); |
9aeaab68 JH |
2268 | return &commit_list_insert(parent, tail)->next; |
2269 | } | |
2270 | ||
2271 | static void append_merge_parents(struct commit_list **tail) | |
2272 | { | |
2273 | int merge_head; | |
9aeaab68 JH |
2274 | struct strbuf line = STRBUF_INIT; |
2275 | ||
f932729c | 2276 | merge_head = open(git_path_merge_head(), O_RDONLY); |
9aeaab68 JH |
2277 | if (merge_head < 0) { |
2278 | if (errno == ENOENT) | |
2279 | return; | |
f932729c | 2280 | die("cannot open '%s' for reading", git_path_merge_head()); |
9aeaab68 JH |
2281 | } |
2282 | ||
2283 | while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) { | |
110d26fc | 2284 | struct object_id oid; |
2285 | if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid)) | |
f932729c | 2286 | die("unknown line in '%s': %s", git_path_merge_head(), line.buf); |
110d26fc | 2287 | tail = append_parent(tail, &oid); |
9aeaab68 JH |
2288 | } |
2289 | close(merge_head); | |
2290 | strbuf_release(&line); | |
ffcabccf JH |
2291 | } |
2292 | ||
8597ea3a JK |
2293 | /* |
2294 | * This isn't as simple as passing sb->buf and sb->len, because we | |
2295 | * want to transfer ownership of the buffer to the commit (so we | |
2296 | * must use detach). | |
2297 | */ | |
2298 | static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb) | |
2299 | { | |
2300 | size_t len; | |
2301 | void *buf = strbuf_detach(sb, &len); | |
2302 | set_commit_buffer(c, buf, len); | |
2303 | } | |
2304 | ||
f6c07d7d JH |
2305 | /* |
2306 | * Prepare a dummy commit that represents the work tree (or staged) item. | |
2307 | * Note that annotating work tree item never works in the reverse. | |
2308 | */ | |
3b8a12e8 AB |
2309 | static struct commit *fake_working_tree_commit(struct diff_options *opt, |
2310 | const char *path, | |
2311 | const char *contents_from) | |
1cfe7733 JH |
2312 | { |
2313 | struct commit *commit; | |
2314 | struct origin *origin; | |
9aeaab68 | 2315 | struct commit_list **parent_tail, *parent; |
110d26fc | 2316 | struct object_id head_oid; |
f285a2d7 | 2317 | struct strbuf buf = STRBUF_INIT; |
1cfe7733 | 2318 | const char *ident; |
1cfe7733 | 2319 | time_t now; |
1cfe7733 JH |
2320 | int size, len; |
2321 | struct cache_entry *ce; | |
2322 | unsigned mode; | |
9aeaab68 | 2323 | struct strbuf msg = STRBUF_INIT; |
1cfe7733 | 2324 | |
a08feb8e | 2325 | read_cache(); |
1cfe7733 | 2326 | time(&now); |
10322a0a | 2327 | commit = alloc_commit_node(); |
1cfe7733 JH |
2328 | commit->object.parsed = 1; |
2329 | commit->date = now; | |
9aeaab68 JH |
2330 | parent_tail = &commit->parents; |
2331 | ||
110d26fc | 2332 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL)) |
9aeaab68 JH |
2333 | die("no such ref: HEAD"); |
2334 | ||
110d26fc | 2335 | parent_tail = append_parent(parent_tail, &head_oid); |
9aeaab68 JH |
2336 | append_merge_parents(parent_tail); |
2337 | verify_working_tree_path(commit, path); | |
1cfe7733 JH |
2338 | |
2339 | origin = make_origin(commit, path); | |
2340 | ||
9aeaab68 JH |
2341 | ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); |
2342 | strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n"); | |
2343 | for (parent = commit->parents; parent; parent = parent->next) | |
2344 | strbuf_addf(&msg, "parent %s\n", | |
f2fd0760 | 2345 | oid_to_hex(&parent->item->object.oid)); |
9aeaab68 JH |
2346 | strbuf_addf(&msg, |
2347 | "author %s\n" | |
2348 | "committer %s\n\n" | |
2349 | "Version of %s from %s\n", | |
2350 | ident, ident, path, | |
2351 | (!contents_from ? path : | |
2352 | (!strcmp(contents_from, "-") ? "standard input" : contents_from))); | |
8597ea3a | 2353 | set_commit_buffer_from_strbuf(commit, &msg); |
9aeaab68 | 2354 | |
1cfe7733 JH |
2355 | if (!contents_from || strcmp("-", contents_from)) { |
2356 | struct stat st; | |
2357 | const char *read_from; | |
8518088f | 2358 | char *buf_ptr; |
3b8a12e8 | 2359 | unsigned long buf_len; |
1cfe7733 JH |
2360 | |
2361 | if (contents_from) { | |
2362 | if (stat(contents_from, &st) < 0) | |
0721c314 | 2363 | die_errno("Cannot stat '%s'", contents_from); |
1cfe7733 JH |
2364 | read_from = contents_from; |
2365 | } | |
2366 | else { | |
2367 | if (lstat(path, &st) < 0) | |
0721c314 | 2368 | die_errno("Cannot lstat '%s'", path); |
1cfe7733 JH |
2369 | read_from = path; |
2370 | } | |
1cfe7733 | 2371 | mode = canon_mode(st.st_mode); |
3b8a12e8 | 2372 | |
1cfe7733 JH |
2373 | switch (st.st_mode & S_IFMT) { |
2374 | case S_IFREG: | |
3b8a12e8 | 2375 | if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && |
acad70d1 | 2376 | textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len)) |
8518088f | 2377 | strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); |
3b8a12e8 | 2378 | else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) |
0721c314 | 2379 | die_errno("cannot open or read '%s'", read_from); |
1cfe7733 JH |
2380 | break; |
2381 | case S_IFLNK: | |
edfd45d2 | 2382 | if (strbuf_readlink(&buf, read_from, st.st_size) < 0) |
0721c314 | 2383 | die_errno("cannot readlink '%s'", read_from); |
1cfe7733 JH |
2384 | break; |
2385 | default: | |
2386 | die("unsupported file type %s", read_from); | |
2387 | } | |
2388 | } | |
2389 | else { | |
2390 | /* Reading from stdin */ | |
1cfe7733 | 2391 | mode = 0; |
f1696ee3 | 2392 | if (strbuf_read(&buf, 0, 0) < 0) |
d824cbba | 2393 | die_errno("failed to read from stdin"); |
1cfe7733 | 2394 | } |
4bf256d6 | 2395 | convert_to_git(path, buf.buf, buf.len, &buf, 0); |
af6eb822 PH |
2396 | origin->file.ptr = buf.buf; |
2397 | origin->file.size = buf.len; | |
a7bcfa12 | 2398 | pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash); |
1cfe7733 JH |
2399 | |
2400 | /* | |
2401 | * Read the current index, replace the path entry with | |
2402 | * origin->blob_sha1 without mucking with its mode or type | |
2403 | * bits; we are not going to write this index out -- we just | |
2404 | * want to run "diff-index --cached". | |
2405 | */ | |
2406 | discard_cache(); | |
2407 | read_cache(); | |
2408 | ||
2409 | len = strlen(path); | |
2410 | if (!mode) { | |
2411 | int pos = cache_name_pos(path, len); | |
2412 | if (0 <= pos) | |
7a51ed66 | 2413 | mode = active_cache[pos]->ce_mode; |
1cfe7733 JH |
2414 | else |
2415 | /* Let's not bother reading from HEAD tree */ | |
2416 | mode = S_IFREG | 0644; | |
2417 | } | |
2418 | size = cache_entry_size(len); | |
2419 | ce = xcalloc(1, size); | |
a7bcfa12 | 2420 | oidcpy(&ce->oid, &origin->blob_oid); |
1cfe7733 | 2421 | memcpy(ce->name, path, len); |
b60e188c TG |
2422 | ce->ce_flags = create_ce_flags(0); |
2423 | ce->ce_namelen = len; | |
1cfe7733 JH |
2424 | ce->ce_mode = create_ce_mode(mode); |
2425 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
2426 | ||
a5400efe | 2427 | cache_tree_invalidate_path(&the_index, path); |
1cfe7733 | 2428 | |
1cfe7733 JH |
2429 | return commit; |
2430 | } | |
2431 | ||
7cb5f7c4 JK |
2432 | static struct commit *find_single_final(struct rev_info *revs, |
2433 | const char **name_p) | |
f6c07d7d JH |
2434 | { |
2435 | int i; | |
7cb5f7c4 JK |
2436 | struct commit *found = NULL; |
2437 | const char *name = NULL; | |
f6c07d7d | 2438 | |
f6c07d7d JH |
2439 | for (i = 0; i < revs->pending.nr; i++) { |
2440 | struct object *obj = revs->pending.objects[i].item; | |
2441 | if (obj->flags & UNINTERESTING) | |
2442 | continue; | |
31da121f | 2443 | obj = deref_tag(obj, NULL, 0); |
f6c07d7d JH |
2444 | if (obj->type != OBJ_COMMIT) |
2445 | die("Non commit %s?", revs->pending.objects[i].name); | |
1b0d4000 | 2446 | if (found) |
f6c07d7d | 2447 | die("More than one commit to dig from %s and %s?", |
7cb5f7c4 JK |
2448 | revs->pending.objects[i].name, name); |
2449 | found = (struct commit *)obj; | |
2450 | name = revs->pending.objects[i].name; | |
1b0d4000 | 2451 | } |
7cb5f7c4 JK |
2452 | if (name_p) |
2453 | *name_p = name; | |
1b0d4000 MK |
2454 | return found; |
2455 | } | |
2456 | ||
2457 | static char *prepare_final(struct scoreboard *sb) | |
2458 | { | |
7cb5f7c4 JK |
2459 | const char *name; |
2460 | sb->final = find_single_final(sb->revs, &name); | |
2461 | return xstrdup_or_null(name); | |
f6c07d7d JH |
2462 | } |
2463 | ||
e1d09701 JH |
2464 | static const char *dwim_reverse_initial(struct scoreboard *sb) |
2465 | { | |
2466 | /* | |
2467 | * DWIM "git blame --reverse ONE -- PATH" as | |
2468 | * "git blame --reverse ONE..HEAD -- PATH" but only do so | |
2469 | * when it makes sense. | |
2470 | */ | |
2471 | struct object *obj; | |
2472 | struct commit *head_commit; | |
2473 | unsigned char head_sha1[20]; | |
2474 | ||
2475 | if (sb->revs->pending.nr != 1) | |
2476 | return NULL; | |
2477 | ||
2478 | /* Is that sole rev a committish? */ | |
2479 | obj = sb->revs->pending.objects[0].item; | |
2480 | obj = deref_tag(obj, NULL, 0); | |
2481 | if (obj->type != OBJ_COMMIT) | |
2482 | return NULL; | |
2483 | ||
2484 | /* Do we have HEAD? */ | |
2485 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) | |
2486 | return NULL; | |
2487 | head_commit = lookup_commit_reference_gently(head_sha1, 1); | |
2488 | if (!head_commit) | |
2489 | return NULL; | |
2490 | ||
2491 | /* Turn "ONE" into "ONE..HEAD" then */ | |
2492 | obj->flags |= UNINTERESTING; | |
2493 | add_pending_object(sb->revs, &head_commit->object, "HEAD"); | |
2494 | ||
2495 | sb->final = (struct commit *)obj; | |
2496 | return sb->revs->pending.objects[0].name; | |
2497 | } | |
2498 | ||
a46442f1 | 2499 | static char *prepare_initial(struct scoreboard *sb) |
85af7929 JH |
2500 | { |
2501 | int i; | |
2502 | const char *final_commit_name = NULL; | |
2503 | struct rev_info *revs = sb->revs; | |
2504 | ||
2505 | /* | |
2506 | * There must be one and only one negative commit, and it must be | |
2507 | * the boundary. | |
2508 | */ | |
2509 | for (i = 0; i < revs->pending.nr; i++) { | |
2510 | struct object *obj = revs->pending.objects[i].item; | |
2511 | if (!(obj->flags & UNINTERESTING)) | |
2512 | continue; | |
31da121f | 2513 | obj = deref_tag(obj, NULL, 0); |
85af7929 JH |
2514 | if (obj->type != OBJ_COMMIT) |
2515 | die("Non commit %s?", revs->pending.objects[i].name); | |
2516 | if (sb->final) | |
d993ce1e | 2517 | die("More than one commit to dig up from, %s and %s?", |
85af7929 JH |
2518 | revs->pending.objects[i].name, |
2519 | final_commit_name); | |
2520 | sb->final = (struct commit *) obj; | |
2521 | final_commit_name = revs->pending.objects[i].name; | |
2522 | } | |
e1d09701 JH |
2523 | |
2524 | if (!final_commit_name) | |
2525 | final_commit_name = dwim_reverse_initial(sb); | |
85af7929 | 2526 | if (!final_commit_name) |
d993ce1e | 2527 | die("No commit to dig up from?"); |
a46442f1 | 2528 | return xstrdup(final_commit_name); |
85af7929 JH |
2529 | } |
2530 | ||
5817da01 PH |
2531 | static int blame_copy_callback(const struct option *option, const char *arg, int unset) |
2532 | { | |
2533 | int *opt = option->value; | |
2534 | ||
2535 | /* | |
2536 | * -C enables copy from removed files; | |
2537 | * -C -C enables copy from existing files, but only | |
2538 | * when blaming a new file; | |
2539 | * -C -C -C enables copy from existing files for | |
2540 | * everybody | |
2541 | */ | |
2542 | if (*opt & PICKAXE_BLAME_COPY_HARDER) | |
2543 | *opt |= PICKAXE_BLAME_COPY_HARDEST; | |
2544 | if (*opt & PICKAXE_BLAME_COPY) | |
2545 | *opt |= PICKAXE_BLAME_COPY_HARDER; | |
2546 | *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE; | |
2547 | ||
2548 | if (arg) | |
2549 | blame_copy_score = parse_score(arg); | |
2550 | return 0; | |
2551 | } | |
2552 | ||
2553 | static int blame_move_callback(const struct option *option, const char *arg, int unset) | |
2554 | { | |
2555 | int *opt = option->value; | |
2556 | ||
2557 | *opt |= PICKAXE_BLAME_MOVE; | |
2558 | ||
2559 | if (arg) | |
2560 | blame_move_score = parse_score(arg); | |
2561 | return 0; | |
2562 | } | |
2563 | ||
acca687f | 2564 | int cmd_blame(int argc, const char **argv, const char *prefix) |
cee7f245 JH |
2565 | { |
2566 | struct rev_info revs; | |
2567 | const char *path; | |
2568 | struct scoreboard sb; | |
2569 | struct origin *o; | |
58dbfa2e ES |
2570 | struct blame_entry *ent = NULL; |
2571 | long dashdash_pos, lno; | |
a46442f1 | 2572 | char *final_commit_name = NULL; |
21666f1a | 2573 | enum object_type type; |
700fd28e | 2574 | struct commit *final_commit = NULL; |
5817da01 | 2575 | |
64093fc0 JK |
2576 | struct string_list range_list = STRING_LIST_INIT_NODUP; |
2577 | int output_option = 0, opt = 0; | |
2578 | int show_stats = 0; | |
2579 | const char *revs_file = NULL; | |
2580 | const char *contents_from = NULL; | |
2581 | const struct option options[] = { | |
d5d09d47 SB |
2582 | OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")), |
2583 | OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")), | |
2584 | OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")), | |
2585 | OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")), | |
aba37f49 | 2586 | OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")), |
efd2a8bd NTND |
2587 | OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE), |
2588 | OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME), | |
2589 | OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER), | |
2590 | OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN), | |
2591 | OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN), | |
2592 | OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT), | |
2593 | OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP), | |
2594 | OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME), | |
2595 | OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR), | |
2596 | OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL), | |
2597 | OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE), | |
5b162879 MH |
2598 | |
2599 | /* | |
2600 | * The following two options are parsed by parse_revision_opt() | |
2601 | * and are only included here to get included in the "-h" | |
2602 | * output: | |
2603 | */ | |
3cde4e02 | 2604 | { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb }, |
5b162879 | 2605 | |
efd2a8bd NTND |
2606 | OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL), |
2607 | OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")), | |
2608 | OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")), | |
2609 | { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback }, | |
2610 | { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback }, | |
58dbfa2e | 2611 | OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")), |
84393bfd | 2612 | OPT__ABBREV(&abbrev), |
5817da01 PH |
2613 | OPT_END() |
2614 | }; | |
2615 | ||
2616 | struct parse_opt_ctx_t ctx; | |
7ceacdff | 2617 | int cmd_is_annotate = !strcmp(argv[0], "annotate"); |
58dbfa2e ES |
2618 | struct range_set ranges; |
2619 | unsigned int range_i; | |
52f4d126 | 2620 | long anchor; |
e68989a7 | 2621 | |
8b504db3 | 2622 | git_config(git_blame_config, &output_option); |
5817da01 | 2623 | init_revisions(&revs, NULL); |
31653c1a | 2624 | revs.date_mode = blame_date_mode; |
3b8a12e8 | 2625 | DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV); |
3d1aa566 | 2626 | DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES); |
31653c1a | 2627 | |
612702e8 | 2628 | save_commit_buffer = 0; |
3f8d5204 | 2629 | dashdash_pos = 0; |
aba37f49 | 2630 | show_progress = -1; |
612702e8 | 2631 | |
9ca1169f SB |
2632 | parse_options_start(&ctx, argc, argv, prefix, options, |
2633 | PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); | |
5817da01 | 2634 | for (;;) { |
5817da01 PH |
2635 | switch (parse_options_step(&ctx, options, blame_opt_usage)) { |
2636 | case PARSE_OPT_HELP: | |
2637 | exit(129); | |
2638 | case PARSE_OPT_DONE: | |
3f8d5204 PH |
2639 | if (ctx.argv[0]) |
2640 | dashdash_pos = ctx.cpidx; | |
5817da01 PH |
2641 | goto parse_done; |
2642 | } | |
2643 | ||
2644 | if (!strcmp(ctx.argv[0], "--reverse")) { | |
2645 | ctx.argv[0] = "--children"; | |
2646 | reverse = 1; | |
2647 | } | |
6b61ec05 | 2648 | parse_revision_opt(&revs, &ctx, options, blame_opt_usage); |
5817da01 PH |
2649 | } |
2650 | parse_done: | |
3d1aa566 | 2651 | no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES); |
3cde4e02 | 2652 | xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC; |
3d1aa566 | 2653 | DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES); |
5817da01 PH |
2654 | argc = parse_options_end(&ctx); |
2655 | ||
aba37f49 ECA |
2656 | if (incremental || (output_option & OUTPUT_PORCELAIN)) { |
2657 | if (show_progress > 0) | |
e3f54bff | 2658 | die(_("--progress can't be used with --incremental or porcelain formats")); |
aba37f49 ECA |
2659 | show_progress = 0; |
2660 | } else if (show_progress < 0) | |
2661 | show_progress = isatty(2); | |
2662 | ||
91229834 | 2663 | if (0 < abbrev && abbrev < GIT_SHA1_HEXSZ) |
b31272f7 JH |
2664 | /* one more abbrev length is needed for the boundary commit */ |
2665 | abbrev++; | |
ed58d808 JK |
2666 | else if (!abbrev) |
2667 | abbrev = GIT_SHA1_HEXSZ; | |
84393bfd | 2668 | |
aa9ea77d | 2669 | if (revs_file && read_ancestry(revs_file)) |
d824cbba | 2670 | die_errno("reading graft file '%s' failed", revs_file); |
aa9ea77d | 2671 | |
31653c1a | 2672 | if (cmd_is_annotate) { |
7ceacdff | 2673 | output_option |= OUTPUT_ANNOTATE_COMPAT; |
a5481a6c | 2674 | blame_date_mode.type = DATE_ISO8601; |
31653c1a EL |
2675 | } else { |
2676 | blame_date_mode = revs.date_mode; | |
2677 | } | |
2678 | ||
2679 | /* The maximum width used to show the dates */ | |
a5481a6c | 2680 | switch (blame_date_mode.type) { |
31653c1a EL |
2681 | case DATE_RFC2822: |
2682 | blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700"); | |
2683 | break; | |
466fb674 BB |
2684 | case DATE_ISO8601_STRICT: |
2685 | blame_date_width = sizeof("2006-10-19T16:00:04-07:00"); | |
2686 | break; | |
31653c1a EL |
2687 | case DATE_ISO8601: |
2688 | blame_date_width = sizeof("2006-10-19 16:00:04 -0700"); | |
2689 | break; | |
2690 | case DATE_RAW: | |
2691 | blame_date_width = sizeof("1161298804 -0700"); | |
2692 | break; | |
642833db JK |
2693 | case DATE_UNIX: |
2694 | blame_date_width = sizeof("1161298804"); | |
2695 | break; | |
31653c1a EL |
2696 | case DATE_SHORT: |
2697 | blame_date_width = sizeof("2006-10-19"); | |
2698 | break; | |
2699 | case DATE_RELATIVE: | |
dd75553b JX |
2700 | /* TRANSLATORS: This string is used to tell us the maximum |
2701 | display width for a relative timestamp in "git blame" | |
2702 | output. For C locale, "4 years, 11 months ago", which | |
2703 | takes 22 places, is the longest among various forms of | |
2704 | relative timestamps, but your language may need more or | |
2705 | fewer display columns. */ | |
2706 | blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */ | |
2707 | break; | |
31653c1a EL |
2708 | case DATE_NORMAL: |
2709 | blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700"); | |
2710 | break; | |
aa1462cc JK |
2711 | case DATE_STRFTIME: |
2712 | blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */ | |
2713 | break; | |
31653c1a EL |
2714 | } |
2715 | blame_date_width -= 1; /* strip the null */ | |
7ceacdff | 2716 | |
b3123f98 JH |
2717 | if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER)) |
2718 | opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE | | |
2719 | PICKAXE_BLAME_COPY_HARDER); | |
2720 | ||
4a0fc95f JH |
2721 | if (!blame_move_score) |
2722 | blame_move_score = BLAME_DEFAULT_MOVE_SCORE; | |
2723 | if (!blame_copy_score) | |
2724 | blame_copy_score = BLAME_DEFAULT_COPY_SCORE; | |
2725 | ||
1732a1fd JH |
2726 | /* |
2727 | * We have collected options unknown to us in argv[1..unk] | |
cee7f245 | 2728 | * which are to be passed to revision machinery if we are |
3dff5379 | 2729 | * going to do the "bottom" processing. |
cee7f245 JH |
2730 | * |
2731 | * The remaining are: | |
2732 | * | |
22e5e58a | 2733 | * (1) if dashdash_pos != 0, it is either |
3f8d5204 PH |
2734 | * "blame [revisions] -- <path>" or |
2735 | * "blame -- <path> <rev>" | |
cee7f245 | 2736 | * |
22e5e58a | 2737 | * (2) otherwise, it is one of the two: |
3f8d5204 PH |
2738 | * "blame [revisions] <path>" |
2739 | * "blame <path> <rev>" | |
cee7f245 | 2740 | * |
3f8d5204 PH |
2741 | * Note that we must strip out <path> from the arguments: we do not |
2742 | * want the path pruning but we may want "bottom" processing. | |
cee7f245 | 2743 | */ |
3f8d5204 PH |
2744 | if (dashdash_pos) { |
2745 | switch (argc - dashdash_pos - 1) { | |
2746 | case 2: /* (1b) */ | |
2747 | if (argc != 4) | |
5817da01 | 2748 | usage_with_options(blame_opt_usage, options); |
3f8d5204 PH |
2749 | /* reorder for the new way: <rev> -- <path> */ |
2750 | argv[1] = argv[3]; | |
2751 | argv[3] = argv[2]; | |
2752 | argv[2] = "--"; | |
2753 | /* FALLTHROUGH */ | |
2754 | case 1: /* (1a) */ | |
2755 | path = add_prefix(prefix, argv[--argc]); | |
2756 | argv[argc] = NULL; | |
2757 | break; | |
2758 | default: | |
2759 | usage_with_options(blame_opt_usage, options); | |
cee7f245 | 2760 | } |
3f8d5204 PH |
2761 | } else { |
2762 | if (argc < 2) | |
5817da01 | 2763 | usage_with_options(blame_opt_usage, options); |
3f8d5204 | 2764 | path = add_prefix(prefix, argv[argc - 1]); |
dbe44faa | 2765 | if (argc == 3 && !file_exists(path)) { /* (2b) */ |
3f8d5204 PH |
2766 | path = add_prefix(prefix, argv[1]); |
2767 | argv[1] = argv[2]; | |
cee7f245 | 2768 | } |
3f8d5204 | 2769 | argv[argc - 1] = "--"; |
cee7f245 | 2770 | |
3f8d5204 | 2771 | setup_work_tree(); |
dbe44faa | 2772 | if (!file_exists(path)) |
d824cbba | 2773 | die_errno("cannot stat path '%s'", path); |
cee7f245 JH |
2774 | } |
2775 | ||
8b3dce56 | 2776 | revs.disable_stdin = 1; |
3f8d5204 | 2777 | setup_revisions(argc, argv, &revs, NULL); |
cee7f245 JH |
2778 | memset(&sb, 0, sizeof(sb)); |
2779 | ||
85af7929 | 2780 | sb.revs = &revs; |
7e6ac6e4 | 2781 | if (!reverse) { |
85af7929 | 2782 | final_commit_name = prepare_final(&sb); |
7e6ac6e4 DK |
2783 | sb.commits.compare = compare_commits_by_commit_date; |
2784 | } | |
85af7929 | 2785 | else if (contents_from) |
e3f54bff | 2786 | die(_("--contents and --reverse do not blend well.")); |
7e6ac6e4 | 2787 | else { |
85af7929 | 2788 | final_commit_name = prepare_initial(&sb); |
7e6ac6e4 | 2789 | sb.commits.compare = compare_commits_by_reverse_commit_date; |
700fd28e MK |
2790 | if (revs.first_parent_only) |
2791 | revs.children.name = NULL; | |
7e6ac6e4 | 2792 | } |
cee7f245 JH |
2793 | |
2794 | if (!sb.final) { | |
1732a1fd JH |
2795 | /* |
2796 | * "--not A B -- path" without anything positive; | |
1cfe7733 JH |
2797 | * do not default to HEAD, but use the working tree |
2798 | * or "--contents". | |
1732a1fd | 2799 | */ |
1981820b | 2800 | setup_work_tree(); |
3b8a12e8 AB |
2801 | sb.final = fake_working_tree_commit(&sb.revs->diffopt, |
2802 | path, contents_from); | |
1cfe7733 | 2803 | add_pending_object(&revs, &(sb.final->object), ":"); |
cee7f245 | 2804 | } |
1cfe7733 | 2805 | else if (contents_from) |
e3f54bff | 2806 | die(_("cannot use --contents with final commit object name")); |
cee7f245 | 2807 | |
700fd28e | 2808 | if (reverse && revs.first_parent_only) { |
7cb5f7c4 JK |
2809 | final_commit = find_single_final(sb.revs, NULL); |
2810 | if (!final_commit) | |
e3f54bff | 2811 | die(_("--reverse and --first-parent together require specified latest commit")); |
700fd28e MK |
2812 | } |
2813 | ||
1732a1fd JH |
2814 | /* |
2815 | * If we have bottom, this will mark the ancestors of the | |
cee7f245 JH |
2816 | * bottom commits we would reach while traversing as |
2817 | * uninteresting. | |
2818 | */ | |
3d51e1b5 | 2819 | if (prepare_revision_walk(&revs)) |
20108742 | 2820 | die(_("revision walk setup failed")); |
cee7f245 | 2821 | |
700fd28e MK |
2822 | if (reverse && revs.first_parent_only) { |
2823 | struct commit *c = final_commit; | |
2824 | ||
2825 | sb.revs->children.name = "children"; | |
2826 | while (c->parents && | |
f2fd0760 | 2827 | oidcmp(&c->object.oid, &sb.final->object.oid)) { |
700fd28e MK |
2828 | struct commit_list *l = xcalloc(1, sizeof(*l)); |
2829 | ||
2830 | l->item = c; | |
2831 | if (add_decoration(&sb.revs->children, | |
2832 | &c->parents->item->object, l)) | |
2833 | die("BUG: not unique item in first-parent chain"); | |
2834 | c = c->parents->item; | |
2835 | } | |
2836 | ||
f2fd0760 | 2837 | if (oidcmp(&c->object.oid, &sb.final->object.oid)) |
e3f54bff | 2838 | die(_("--reverse --first-parent together require range along first-parent chain")); |
700fd28e MK |
2839 | } |
2840 | ||
f2fd0760 | 2841 | if (is_null_oid(&sb.final->object.oid)) { |
1cfe7733 | 2842 | o = sb.final->util; |
5c0b13f8 | 2843 | sb.final_buf = xmemdupz(o->file.ptr, o->file.size); |
1cfe7733 JH |
2844 | sb.final_buf_size = o->file.size; |
2845 | } | |
2846 | else { | |
2847 | o = get_origin(&sb, sb.final, path); | |
90064710 | 2848 | if (fill_blob_sha1_and_mode(o)) |
e3f54bff | 2849 | die(_("no such path %s in %s"), path, final_commit_name); |
cee7f245 | 2850 | |
3b8a12e8 | 2851 | if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) && |
acad70d1 | 2852 | textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb.final_buf, |
3b8a12e8 AB |
2853 | &sb.final_buf_size)) |
2854 | ; | |
2855 | else | |
a7bcfa12 | 2856 | sb.final_buf = read_sha1_file(o->blob_oid.hash, &type, |
3b8a12e8 AB |
2857 | &sb.final_buf_size); |
2858 | ||
ab43e495 | 2859 | if (!sb.final_buf) |
e3f54bff | 2860 | die(_("cannot read blob %s for path %s"), |
a7bcfa12 | 2861 | oid_to_hex(&o->blob_oid), |
ab43e495 | 2862 | path); |
1cfe7733 | 2863 | } |
c2e525d9 | 2864 | num_read_blob++; |
cee7f245 JH |
2865 | lno = prepare_lines(&sb); |
2866 | ||
58dbfa2e | 2867 | if (lno && !range_list.nr) |
aa59e14b | 2868 | string_list_append(&range_list, "1"); |
58dbfa2e | 2869 | |
52f4d126 | 2870 | anchor = 1; |
58dbfa2e ES |
2871 | range_set_init(&ranges, range_list.nr); |
2872 | for (range_i = 0; range_i < range_list.nr; ++range_i) { | |
2873 | long bottom, top; | |
2874 | if (parse_range_arg(range_list.items[range_i].string, | |
52f4d126 | 2875 | nth_line_cb, &sb, lno, anchor, |
58dbfa2e ES |
2876 | &bottom, &top, sb.path)) |
2877 | usage(blame_usage); | |
2878 | if (lno < top || ((lno || bottom) && lno < bottom)) | |
e3f54bff VA |
2879 | die(Q_("file %s has only %lu line", |
2880 | "file %s has only %lu lines", | |
2881 | lno), path, lno); | |
58dbfa2e ES |
2882 | if (bottom < 1) |
2883 | bottom = 1; | |
2884 | if (top < 1) | |
2885 | top = lno; | |
2886 | bottom--; | |
2887 | range_set_append_unsafe(&ranges, bottom, top); | |
52f4d126 | 2888 | anchor = top + 1; |
58dbfa2e ES |
2889 | } |
2890 | sort_and_merge_range_set(&ranges); | |
2891 | ||
2892 | for (range_i = ranges.nr; range_i > 0; --range_i) { | |
2893 | const struct range *r = &ranges.ranges[range_i - 1]; | |
2894 | long bottom = r->start; | |
2895 | long top = r->end; | |
2896 | struct blame_entry *next = ent; | |
2897 | ent = xcalloc(1, sizeof(*ent)); | |
2898 | ent->lno = bottom; | |
2899 | ent->num_lines = top - bottom; | |
2900 | ent->suspect = o; | |
2901 | ent->s_lno = bottom; | |
2902 | ent->next = next; | |
58dbfa2e ES |
2903 | origin_incref(o); |
2904 | } | |
7e6ac6e4 DK |
2905 | |
2906 | o->suspects = ent; | |
2907 | prio_queue_put(&sb.commits, o->commit); | |
2908 | ||
58dbfa2e ES |
2909 | origin_decref(o); |
2910 | ||
2911 | range_set_release(&ranges); | |
2912 | string_list_clear(&range_list, 0); | |
cee7f245 | 2913 | |
7e6ac6e4 | 2914 | sb.ent = NULL; |
cee7f245 JH |
2915 | sb.path = path; |
2916 | ||
d551a488 | 2917 | read_mailmap(&mailmap, NULL); |
f9567384 | 2918 | |
aba37f49 ECA |
2919 | assign_blame(&sb, opt); |
2920 | ||
b92565dc MH |
2921 | if (!incremental) |
2922 | setup_pager(); | |
2923 | ||
a46442f1 LF |
2924 | free(final_commit_name); |
2925 | ||
717d1462 LT |
2926 | if (incremental) |
2927 | return 0; | |
2928 | ||
7e6ac6e4 DK |
2929 | sb.ent = blame_sort(sb.ent, compare_blame_final); |
2930 | ||
cee7f245 JH |
2931 | coalesce(&sb); |
2932 | ||
2933 | if (!(output_option & OUTPUT_PORCELAIN)) | |
2934 | find_alignment(&sb, &output_option); | |
2935 | ||
2936 | output(&sb, output_option); | |
2937 | free((void *)sb.final_buf); | |
2938 | for (ent = sb.ent; ent; ) { | |
2939 | struct blame_entry *e = ent->next; | |
2940 | free(ent); | |
2941 | ent = e; | |
2942 | } | |
c2e525d9 | 2943 | |
870b39c1 | 2944 | if (show_stats) { |
c2e525d9 JH |
2945 | printf("num read blob: %d\n", num_read_blob); |
2946 | printf("num get patch: %d\n", num_get_patch); | |
2947 | printf("num commits: %d\n", num_commits); | |
2948 | } | |
cee7f245 JH |
2949 | return 0; |
2950 | } |