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