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