]>
Commit | Line | Data |
---|---|---|
cee7f245 JH |
1 | /* |
2 | * Pickaxe | |
3 | * | |
4 | * Copyright (c) 2006, Junio C Hamano | |
5 | */ | |
6 | ||
7 | #include "cache.h" | |
8 | #include "builtin.h" | |
9 | #include "blob.h" | |
10 | #include "commit.h" | |
11 | #include "tag.h" | |
12 | #include "tree-walk.h" | |
13 | #include "diff.h" | |
14 | #include "diffcore.h" | |
15 | #include "revision.h" | |
717d1462 | 16 | #include "quote.h" |
cee7f245 | 17 | #include "xdiff-interface.h" |
1cfe7733 | 18 | #include "cache-tree.h" |
c455c87c | 19 | #include "string-list.h" |
f9567384 | 20 | #include "mailmap.h" |
5817da01 | 21 | #include "parse-options.h" |
cee7f245 | 22 | |
588c038a | 23 | static char blame_usage[] = "git blame [options] [rev-opts] [rev] [--] file"; |
5817da01 PH |
24 | |
25 | static const char *blame_opt_usage[] = { | |
26 | blame_usage, | |
27 | "", | |
76447d23 | 28 | "[rev-opts] are documented in git-rev-list(1)", |
5817da01 PH |
29 | NULL |
30 | }; | |
cee7f245 JH |
31 | |
32 | static int longest_file; | |
33 | static int longest_author; | |
34 | static int max_orig_digits; | |
35 | static int max_digits; | |
5ff62c30 | 36 | static int max_score_digits; |
4c10a5ca | 37 | static int show_root; |
85af7929 | 38 | static int reverse; |
4c10a5ca | 39 | static int blank_boundary; |
717d1462 | 40 | static int incremental; |
b82871b3 | 41 | static int xdl_opts = XDF_NEED_MINIMAL; |
c455c87c | 42 | static struct string_list mailmap; |
cee7f245 | 43 | |
54a4c617 JH |
44 | #ifndef DEBUG |
45 | #define DEBUG 0 | |
46 | #endif | |
47 | ||
c2e525d9 JH |
48 | /* stats */ |
49 | static int num_read_blob; | |
50 | static int num_get_patch; | |
51 | static int num_commits; | |
52 | ||
d24bba80 | 53 | #define PICKAXE_BLAME_MOVE 01 |
18abd745 JH |
54 | #define PICKAXE_BLAME_COPY 02 |
55 | #define PICKAXE_BLAME_COPY_HARDER 04 | |
c63777c0 | 56 | #define PICKAXE_BLAME_COPY_HARDEST 010 |
d24bba80 | 57 | |
4a0fc95f JH |
58 | /* |
59 | * blame for a blame_entry with score lower than these thresholds | |
60 | * is not passed to the parent using move/copy logic. | |
61 | */ | |
62 | static unsigned blame_move_score; | |
63 | static unsigned blame_copy_score; | |
64 | #define BLAME_DEFAULT_MOVE_SCORE 20 | |
65 | #define BLAME_DEFAULT_COPY_SCORE 40 | |
66 | ||
cee7f245 JH |
67 | /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ |
68 | #define METAINFO_SHOWN (1u<<12) | |
69 | #define MORE_THAN_ONE_PATH (1u<<13) | |
70 | ||
71 | /* | |
54a4c617 | 72 | * One blob in a commit that is being suspected |
cee7f245 JH |
73 | */ |
74 | struct origin { | |
54a4c617 | 75 | int refcnt; |
cee7f245 | 76 | struct commit *commit; |
c2e525d9 | 77 | mmfile_t file; |
cee7f245 JH |
78 | unsigned char blob_sha1[20]; |
79 | char path[FLEX_ARRAY]; | |
80 | }; | |
81 | ||
1732a1fd JH |
82 | /* |
83 | * Given an origin, prepare mmfile_t structure to be used by the | |
84 | * diff machinery | |
85 | */ | |
f6c07d7d | 86 | static void fill_origin_blob(struct origin *o, mmfile_t *file) |
c2e525d9 JH |
87 | { |
88 | if (!o->file.ptr) { | |
21666f1a | 89 | enum object_type type; |
c2e525d9 | 90 | num_read_blob++; |
21666f1a | 91 | file->ptr = read_sha1_file(o->blob_sha1, &type, |
c2e525d9 | 92 | (unsigned long *)(&(file->size))); |
ab43e495 JH |
93 | if (!file->ptr) |
94 | die("Cannot read blob %s for path %s", | |
95 | sha1_to_hex(o->blob_sha1), | |
96 | o->path); | |
c2e525d9 JH |
97 | o->file = *file; |
98 | } | |
99 | else | |
100 | *file = o->file; | |
c2e525d9 JH |
101 | } |
102 | ||
1732a1fd JH |
103 | /* |
104 | * Origin is refcounted and usually we keep the blob contents to be | |
105 | * reused. | |
106 | */ | |
54a4c617 JH |
107 | static inline struct origin *origin_incref(struct origin *o) |
108 | { | |
109 | if (o) | |
110 | o->refcnt++; | |
111 | return o; | |
112 | } | |
113 | ||
114 | static void origin_decref(struct origin *o) | |
115 | { | |
116 | if (o && --o->refcnt <= 0) { | |
8e0f7003 | 117 | free(o->file.ptr); |
54a4c617 JH |
118 | free(o); |
119 | } | |
120 | } | |
121 | ||
7c3c7962 JH |
122 | static void drop_origin_blob(struct origin *o) |
123 | { | |
124 | if (o->file.ptr) { | |
125 | free(o->file.ptr); | |
126 | o->file.ptr = NULL; | |
127 | } | |
128 | } | |
129 | ||
1732a1fd JH |
130 | /* |
131 | * Each group of lines is described by a blame_entry; it can be split | |
132 | * as we pass blame to the parents. They form a linked list in the | |
133 | * scoreboard structure, sorted by the target line number. | |
134 | */ | |
cee7f245 JH |
135 | struct blame_entry { |
136 | struct blame_entry *prev; | |
137 | struct blame_entry *next; | |
138 | ||
139 | /* the first line of this group in the final image; | |
140 | * internally all line numbers are 0 based. | |
141 | */ | |
142 | int lno; | |
143 | ||
144 | /* how many lines this group has */ | |
145 | int num_lines; | |
146 | ||
147 | /* the commit that introduced this group into the final image */ | |
148 | struct origin *suspect; | |
149 | ||
150 | /* true if the suspect is truly guilty; false while we have not | |
151 | * checked if the group came from one of its parents. | |
152 | */ | |
153 | char guilty; | |
154 | ||
eb46328e AG |
155 | /* true if the entry has been scanned for copies in the current parent |
156 | */ | |
157 | char scanned; | |
158 | ||
cee7f245 JH |
159 | /* the line number of the first line of this group in the |
160 | * suspect's file; internally all line numbers are 0 based. | |
161 | */ | |
162 | int s_lno; | |
5ff62c30 JH |
163 | |
164 | /* how significant this entry is -- cached to avoid | |
1732a1fd | 165 | * scanning the lines over and over. |
5ff62c30 JH |
166 | */ |
167 | unsigned score; | |
cee7f245 JH |
168 | }; |
169 | ||
1732a1fd JH |
170 | /* |
171 | * The current state of the blame assignment. | |
172 | */ | |
cee7f245 JH |
173 | struct scoreboard { |
174 | /* the final commit (i.e. where we started digging from) */ | |
175 | struct commit *final; | |
85af7929 | 176 | struct rev_info *revs; |
cee7f245 JH |
177 | const char *path; |
178 | ||
1732a1fd JH |
179 | /* |
180 | * The contents in the final image. | |
181 | * Used by many functions to obtain contents of the nth line, | |
182 | * indexed with scoreboard.lineno[blame_entry.lno]. | |
cee7f245 JH |
183 | */ |
184 | const char *final_buf; | |
185 | unsigned long final_buf_size; | |
186 | ||
187 | /* linked list of blames */ | |
188 | struct blame_entry *ent; | |
189 | ||
612702e8 | 190 | /* look-up a line in the final buffer */ |
cee7f245 JH |
191 | int num_lines; |
192 | int *lineno; | |
193 | }; | |
194 | ||
171dccd5 | 195 | static inline int same_suspect(struct origin *a, struct origin *b) |
46014766 | 196 | { |
171dccd5 | 197 | if (a == b) |
57584d9e | 198 | return 1; |
171dccd5 JH |
199 | if (a->commit != b->commit) |
200 | return 0; | |
201 | return !strcmp(a->path, b->path); | |
46014766 JH |
202 | } |
203 | ||
54a4c617 JH |
204 | static void sanity_check_refcnt(struct scoreboard *); |
205 | ||
1732a1fd JH |
206 | /* |
207 | * If two blame entries that are next to each other came from | |
208 | * contiguous lines in the same origin (i.e. <commit, path> pair), | |
209 | * merge them together. | |
210 | */ | |
cee7f245 JH |
211 | static void coalesce(struct scoreboard *sb) |
212 | { | |
213 | struct blame_entry *ent, *next; | |
214 | ||
215 | for (ent = sb->ent; ent && (next = ent->next); ent = next) { | |
171dccd5 | 216 | if (same_suspect(ent->suspect, next->suspect) && |
cee7f245 JH |
217 | ent->guilty == next->guilty && |
218 | ent->s_lno + ent->num_lines == next->s_lno) { | |
219 | ent->num_lines += next->num_lines; | |
220 | ent->next = next->next; | |
221 | if (ent->next) | |
222 | ent->next->prev = ent; | |
54a4c617 | 223 | origin_decref(next->suspect); |
cee7f245 | 224 | free(next); |
46014766 | 225 | ent->score = 0; |
cee7f245 JH |
226 | next = ent; /* again */ |
227 | } | |
228 | } | |
54a4c617 JH |
229 | |
230 | if (DEBUG) /* sanity */ | |
231 | sanity_check_refcnt(sb); | |
cee7f245 JH |
232 | } |
233 | ||
1732a1fd JH |
234 | /* |
235 | * Given a commit and a path in it, create a new origin structure. | |
236 | * The callers that add blame to the scoreboard should use | |
237 | * get_origin() to obtain shared, refcounted copy instead of calling | |
238 | * this function directly. | |
239 | */ | |
854b97f6 JH |
240 | static struct origin *make_origin(struct commit *commit, const char *path) |
241 | { | |
242 | struct origin *o; | |
243 | o = xcalloc(1, sizeof(*o) + strlen(path) + 1); | |
244 | o->commit = commit; | |
245 | o->refcnt = 1; | |
246 | strcpy(o->path, path); | |
247 | return o; | |
248 | } | |
249 | ||
1732a1fd JH |
250 | /* |
251 | * Locate an existing origin or create a new one. | |
252 | */ | |
f6c0e191 JH |
253 | static struct origin *get_origin(struct scoreboard *sb, |
254 | struct commit *commit, | |
255 | const char *path) | |
cee7f245 | 256 | { |
f6c0e191 | 257 | struct blame_entry *e; |
cee7f245 | 258 | |
f6c0e191 JH |
259 | for (e = sb->ent; e; e = e->next) { |
260 | if (e->suspect->commit == commit && | |
261 | !strcmp(e->suspect->path, path)) | |
54a4c617 | 262 | return origin_incref(e->suspect); |
cee7f245 | 263 | } |
854b97f6 | 264 | return make_origin(commit, path); |
cee7f245 JH |
265 | } |
266 | ||
1732a1fd JH |
267 | /* |
268 | * Fill the blob_sha1 field of an origin if it hasn't, so that later | |
269 | * call to fill_origin_blob() can use it to locate the data. blob_sha1 | |
270 | * for an origin is also used to pass the blame for the entire file to | |
271 | * the parent to detect the case where a child's blob is identical to | |
272 | * that of its parent's. | |
273 | */ | |
f6c0e191 JH |
274 | static int fill_blob_sha1(struct origin *origin) |
275 | { | |
276 | unsigned mode; | |
f6c0e191 JH |
277 | |
278 | if (!is_null_sha1(origin->blob_sha1)) | |
279 | return 0; | |
280 | if (get_tree_entry(origin->commit->object.sha1, | |
281 | origin->path, | |
282 | origin->blob_sha1, &mode)) | |
283 | goto error_out; | |
21666f1a | 284 | if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB) |
f6c0e191 JH |
285 | goto error_out; |
286 | return 0; | |
287 | error_out: | |
288 | hashclr(origin->blob_sha1); | |
289 | return -1; | |
290 | } | |
291 | ||
1732a1fd JH |
292 | /* |
293 | * We have an origin -- check if the same path exists in the | |
294 | * parent and return an origin structure to represent it. | |
295 | */ | |
f6c0e191 | 296 | static struct origin *find_origin(struct scoreboard *sb, |
cee7f245 JH |
297 | struct commit *parent, |
298 | struct origin *origin) | |
299 | { | |
300 | struct origin *porigin = NULL; | |
301 | struct diff_options diff_opts; | |
f6c0e191 JH |
302 | const char *paths[2]; |
303 | ||
0d981c67 | 304 | if (parent->util) { |
1732a1fd JH |
305 | /* |
306 | * Each commit object can cache one origin in that | |
307 | * commit. This is a freestanding copy of origin and | |
308 | * not refcounted. | |
854b97f6 | 309 | */ |
0d981c67 | 310 | struct origin *cached = parent->util; |
854b97f6 | 311 | if (!strcmp(cached->path, origin->path)) { |
1732a1fd JH |
312 | /* |
313 | * The same path between origin and its parent | |
314 | * without renaming -- the most common case. | |
315 | */ | |
854b97f6 | 316 | porigin = get_origin(sb, parent, cached->path); |
1732a1fd JH |
317 | |
318 | /* | |
319 | * If the origin was newly created (i.e. get_origin | |
320 | * would call make_origin if none is found in the | |
321 | * scoreboard), it does not know the blob_sha1, | |
322 | * so copy it. Otherwise porigin was in the | |
323 | * scoreboard and already knows blob_sha1. | |
324 | */ | |
854b97f6 JH |
325 | if (porigin->refcnt == 1) |
326 | hashcpy(porigin->blob_sha1, cached->blob_sha1); | |
327 | return porigin; | |
328 | } | |
329 | /* otherwise it was not very useful; free it */ | |
330 | free(parent->util); | |
331 | parent->util = NULL; | |
0d981c67 JH |
332 | } |
333 | ||
f6c0e191 JH |
334 | /* See if the origin->path is different between parent |
335 | * and origin first. Most of the time they are the | |
336 | * same and diff-tree is fairly efficient about this. | |
337 | */ | |
338 | diff_setup(&diff_opts); | |
8f67f8ae | 339 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
f6c0e191 JH |
340 | diff_opts.detect_rename = 0; |
341 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
342 | paths[0] = origin->path; | |
343 | paths[1] = NULL; | |
344 | ||
345 | diff_tree_setup_paths(paths, &diff_opts); | |
346 | if (diff_setup_done(&diff_opts) < 0) | |
347 | die("diff-setup"); | |
1cfe7733 JH |
348 | |
349 | if (is_null_sha1(origin->commit->object.sha1)) | |
350 | do_diff_cache(parent->tree->object.sha1, &diff_opts); | |
351 | else | |
352 | diff_tree_sha1(parent->tree->object.sha1, | |
353 | origin->commit->tree->object.sha1, | |
354 | "", &diff_opts); | |
f6c0e191 JH |
355 | diffcore_std(&diff_opts); |
356 | ||
357 | /* It is either one entry that says "modified", or "created", | |
358 | * or nothing. | |
359 | */ | |
360 | if (!diff_queued_diff.nr) { | |
361 | /* The path is the same as parent */ | |
362 | porigin = get_origin(sb, parent, origin->path); | |
363 | hashcpy(porigin->blob_sha1, origin->blob_sha1); | |
364 | } | |
365 | else if (diff_queued_diff.nr != 1) | |
acca687f | 366 | die("internal error in blame::find_origin"); |
f6c0e191 JH |
367 | else { |
368 | struct diff_filepair *p = diff_queued_diff.queue[0]; | |
369 | switch (p->status) { | |
370 | default: | |
acca687f | 371 | die("internal error in blame::find_origin (%c)", |
f6c0e191 JH |
372 | p->status); |
373 | case 'M': | |
374 | porigin = get_origin(sb, parent, origin->path); | |
375 | hashcpy(porigin->blob_sha1, p->one->sha1); | |
376 | break; | |
377 | case 'A': | |
378 | case 'T': | |
379 | /* Did not exist in parent, or type changed */ | |
380 | break; | |
381 | } | |
382 | } | |
383 | diff_flush(&diff_opts); | |
03b69c76 | 384 | diff_tree_release_paths(&diff_opts); |
0d981c67 | 385 | if (porigin) { |
1732a1fd JH |
386 | /* |
387 | * Create a freestanding copy that is not part of | |
388 | * the refcounted origin found in the scoreboard, and | |
389 | * cache it in the commit. | |
390 | */ | |
854b97f6 | 391 | struct origin *cached; |
1732a1fd | 392 | |
854b97f6 JH |
393 | cached = make_origin(porigin->commit, porigin->path); |
394 | hashcpy(cached->blob_sha1, porigin->blob_sha1); | |
395 | parent->util = cached; | |
0d981c67 | 396 | } |
f69e743d JH |
397 | return porigin; |
398 | } | |
f6c0e191 | 399 | |
1732a1fd JH |
400 | /* |
401 | * We have an origin -- find the path that corresponds to it in its | |
402 | * parent and return an origin structure to represent it. | |
403 | */ | |
f69e743d JH |
404 | static struct origin *find_rename(struct scoreboard *sb, |
405 | struct commit *parent, | |
406 | struct origin *origin) | |
407 | { | |
408 | struct origin *porigin = NULL; | |
409 | struct diff_options diff_opts; | |
410 | int i; | |
411 | const char *paths[2]; | |
cee7f245 JH |
412 | |
413 | diff_setup(&diff_opts); | |
8f67f8ae | 414 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
cee7f245 JH |
415 | diff_opts.detect_rename = DIFF_DETECT_RENAME; |
416 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
2f3f8b21 | 417 | diff_opts.single_follow = origin->path; |
cee7f245 JH |
418 | paths[0] = NULL; |
419 | diff_tree_setup_paths(paths, &diff_opts); | |
420 | if (diff_setup_done(&diff_opts) < 0) | |
421 | die("diff-setup"); | |
1cfe7733 JH |
422 | |
423 | if (is_null_sha1(origin->commit->object.sha1)) | |
424 | do_diff_cache(parent->tree->object.sha1, &diff_opts); | |
425 | else | |
426 | diff_tree_sha1(parent->tree->object.sha1, | |
427 | origin->commit->tree->object.sha1, | |
428 | "", &diff_opts); | |
cee7f245 JH |
429 | diffcore_std(&diff_opts); |
430 | ||
431 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
432 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
612702e8 | 433 | if ((p->status == 'R' || p->status == 'C') && |
f6c0e191 JH |
434 | !strcmp(p->two->path, origin->path)) { |
435 | porigin = get_origin(sb, parent, p->one->path); | |
436 | hashcpy(porigin->blob_sha1, p->one->sha1); | |
cee7f245 JH |
437 | break; |
438 | } | |
439 | } | |
440 | diff_flush(&diff_opts); | |
03b69c76 | 441 | diff_tree_release_paths(&diff_opts); |
cee7f245 JH |
442 | return porigin; |
443 | } | |
444 | ||
1732a1fd JH |
445 | /* |
446 | * Parsing of patch chunks... | |
447 | */ | |
cee7f245 JH |
448 | struct chunk { |
449 | /* line number in postimage; up to but not including this | |
450 | * line is the same as preimage | |
451 | */ | |
452 | int same; | |
453 | ||
454 | /* preimage line number after this chunk */ | |
455 | int p_next; | |
456 | ||
457 | /* postimage line number after this chunk */ | |
458 | int t_next; | |
459 | }; | |
460 | ||
461 | struct patch { | |
462 | struct chunk *chunks; | |
463 | int num; | |
464 | }; | |
465 | ||
466 | struct blame_diff_state { | |
467 | struct xdiff_emit_state xm; | |
468 | struct patch *ret; | |
469 | unsigned hunk_post_context; | |
470 | unsigned hunk_in_pre_context : 1; | |
471 | }; | |
472 | ||
473 | static void process_u_diff(void *state_, char *line, unsigned long len) | |
474 | { | |
475 | struct blame_diff_state *state = state_; | |
476 | struct chunk *chunk; | |
477 | int off1, off2, len1, len2, num; | |
478 | ||
cee7f245 JH |
479 | num = state->ret->num; |
480 | if (len < 4 || line[0] != '@' || line[1] != '@') { | |
481 | if (state->hunk_in_pre_context && line[0] == ' ') | |
482 | state->ret->chunks[num - 1].same++; | |
483 | else { | |
484 | state->hunk_in_pre_context = 0; | |
485 | if (line[0] == ' ') | |
486 | state->hunk_post_context++; | |
487 | else | |
488 | state->hunk_post_context = 0; | |
489 | } | |
490 | return; | |
491 | } | |
492 | ||
493 | if (num && state->hunk_post_context) { | |
494 | chunk = &state->ret->chunks[num - 1]; | |
495 | chunk->p_next -= state->hunk_post_context; | |
496 | chunk->t_next -= state->hunk_post_context; | |
497 | } | |
498 | state->ret->num = ++num; | |
499 | state->ret->chunks = xrealloc(state->ret->chunks, | |
500 | sizeof(struct chunk) * num); | |
501 | chunk = &state->ret->chunks[num - 1]; | |
502 | if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { | |
503 | state->ret->num--; | |
504 | return; | |
505 | } | |
506 | ||
507 | /* Line numbers in patch output are one based. */ | |
508 | off1--; | |
509 | off2--; | |
510 | ||
511 | chunk->same = len2 ? off2 : (off2 + 1); | |
512 | ||
513 | chunk->p_next = off1 + (len1 ? len1 : 1); | |
514 | chunk->t_next = chunk->same + len2; | |
515 | state->hunk_in_pre_context = 1; | |
516 | state->hunk_post_context = 0; | |
517 | } | |
518 | ||
519 | static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, | |
520 | int context) | |
521 | { | |
522 | struct blame_diff_state state; | |
523 | xpparam_t xpp; | |
524 | xdemitconf_t xecfg; | |
525 | xdemitcb_t ecb; | |
526 | ||
b82871b3 | 527 | xpp.flags = xdl_opts; |
30b25010 | 528 | memset(&xecfg, 0, sizeof(xecfg)); |
cee7f245 | 529 | xecfg.ctxlen = context; |
cee7f245 JH |
530 | ecb.outf = xdiff_outf; |
531 | ecb.priv = &state; | |
532 | memset(&state, 0, sizeof(state)); | |
533 | state.xm.consume = process_u_diff; | |
534 | state.ret = xmalloc(sizeof(struct patch)); | |
535 | state.ret->chunks = NULL; | |
536 | state.ret->num = 0; | |
537 | ||
c279d7e9 | 538 | xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb); |
cee7f245 JH |
539 | |
540 | if (state.ret->num) { | |
541 | struct chunk *chunk; | |
542 | chunk = &state.ret->chunks[state.ret->num - 1]; | |
543 | chunk->p_next -= state.hunk_post_context; | |
544 | chunk->t_next -= state.hunk_post_context; | |
545 | } | |
546 | return state.ret; | |
547 | } | |
548 | ||
1732a1fd JH |
549 | /* |
550 | * Run diff between two origins and grab the patch output, so that | |
551 | * we can pass blame for lines origin is currently suspected for | |
552 | * to its parent. | |
553 | */ | |
cee7f245 JH |
554 | static struct patch *get_patch(struct origin *parent, struct origin *origin) |
555 | { | |
556 | mmfile_t file_p, file_o; | |
cee7f245 JH |
557 | struct patch *patch; |
558 | ||
c2e525d9 JH |
559 | fill_origin_blob(parent, &file_p); |
560 | fill_origin_blob(origin, &file_o); | |
561 | if (!file_p.ptr || !file_o.ptr) | |
cee7f245 | 562 | return NULL; |
cee7f245 | 563 | patch = compare_buffer(&file_p, &file_o, 0); |
c2e525d9 | 564 | num_get_patch++; |
cee7f245 JH |
565 | return patch; |
566 | } | |
567 | ||
568 | static void free_patch(struct patch *p) | |
569 | { | |
570 | free(p->chunks); | |
571 | free(p); | |
572 | } | |
573 | ||
1732a1fd | 574 | /* |
3dff5379 | 575 | * Link in a new blame entry to the scoreboard. Entries that cover the |
1732a1fd JH |
576 | * same line range have been removed from the scoreboard previously. |
577 | */ | |
cee7f245 JH |
578 | static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) |
579 | { | |
580 | struct blame_entry *ent, *prev = NULL; | |
581 | ||
54a4c617 JH |
582 | origin_incref(e->suspect); |
583 | ||
cee7f245 JH |
584 | for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) |
585 | prev = ent; | |
586 | ||
587 | /* prev, if not NULL, is the last one that is below e */ | |
588 | e->prev = prev; | |
589 | if (prev) { | |
590 | e->next = prev->next; | |
591 | prev->next = e; | |
592 | } | |
593 | else { | |
594 | e->next = sb->ent; | |
595 | sb->ent = e; | |
596 | } | |
597 | if (e->next) | |
598 | e->next->prev = e; | |
599 | } | |
600 | ||
1732a1fd JH |
601 | /* |
602 | * src typically is on-stack; we want to copy the information in it to | |
790296fd | 603 | * a malloced blame_entry that is already on the linked list of the |
1732a1fd JH |
604 | * scoreboard. The origin of dst loses a refcnt while the origin of src |
605 | * gains one. | |
606 | */ | |
cee7f245 JH |
607 | static void dup_entry(struct blame_entry *dst, struct blame_entry *src) |
608 | { | |
609 | struct blame_entry *p, *n; | |
54a4c617 | 610 | |
cee7f245 JH |
611 | p = dst->prev; |
612 | n = dst->next; | |
54a4c617 JH |
613 | origin_incref(src->suspect); |
614 | origin_decref(dst->suspect); | |
cee7f245 JH |
615 | memcpy(dst, src, sizeof(*src)); |
616 | dst->prev = p; | |
617 | dst->next = n; | |
5ff62c30 | 618 | dst->score = 0; |
cee7f245 JH |
619 | } |
620 | ||
621 | static const char *nth_line(struct scoreboard *sb, int lno) | |
622 | { | |
623 | return sb->final_buf + sb->lineno[lno]; | |
624 | } | |
625 | ||
1732a1fd JH |
626 | /* |
627 | * It is known that lines between tlno to same came from parent, and e | |
628 | * has an overlap with that range. it also is known that parent's | |
629 | * line plno corresponds to e's line tlno. | |
630 | * | |
631 | * <---- e -----> | |
632 | * <------> | |
633 | * <------------> | |
634 | * <------------> | |
635 | * <------------------> | |
636 | * | |
637 | * Split e into potentially three parts; before this chunk, the chunk | |
638 | * to be blamed for the parent, and after that portion. | |
639 | */ | |
54a4c617 | 640 | static void split_overlap(struct blame_entry *split, |
cee7f245 JH |
641 | struct blame_entry *e, |
642 | int tlno, int plno, int same, | |
643 | struct origin *parent) | |
644 | { | |
cee7f245 JH |
645 | int chunk_end_lno; |
646 | memset(split, 0, sizeof(struct blame_entry [3])); | |
647 | ||
648 | if (e->s_lno < tlno) { | |
649 | /* there is a pre-chunk part not blamed on parent */ | |
54a4c617 | 650 | split[0].suspect = origin_incref(e->suspect); |
cee7f245 JH |
651 | split[0].lno = e->lno; |
652 | split[0].s_lno = e->s_lno; | |
653 | split[0].num_lines = tlno - e->s_lno; | |
654 | split[1].lno = e->lno + tlno - e->s_lno; | |
655 | split[1].s_lno = plno; | |
656 | } | |
657 | else { | |
658 | split[1].lno = e->lno; | |
659 | split[1].s_lno = plno + (e->s_lno - tlno); | |
660 | } | |
661 | ||
662 | if (same < e->s_lno + e->num_lines) { | |
663 | /* there is a post-chunk part not blamed on parent */ | |
54a4c617 | 664 | split[2].suspect = origin_incref(e->suspect); |
cee7f245 JH |
665 | split[2].lno = e->lno + (same - e->s_lno); |
666 | split[2].s_lno = e->s_lno + (same - e->s_lno); | |
667 | split[2].num_lines = e->s_lno + e->num_lines - same; | |
668 | chunk_end_lno = split[2].lno; | |
669 | } | |
670 | else | |
671 | chunk_end_lno = e->lno + e->num_lines; | |
672 | split[1].num_lines = chunk_end_lno - split[1].lno; | |
673 | ||
1732a1fd JH |
674 | /* |
675 | * if it turns out there is nothing to blame the parent for, | |
676 | * forget about the splitting. !split[1].suspect signals this. | |
677 | */ | |
cee7f245 JH |
678 | if (split[1].num_lines < 1) |
679 | return; | |
54a4c617 | 680 | split[1].suspect = origin_incref(parent); |
cee7f245 JH |
681 | } |
682 | ||
1732a1fd JH |
683 | /* |
684 | * split_overlap() divided an existing blame e into up to three parts | |
685 | * in split. Adjust the linked list of blames in the scoreboard to | |
686 | * reflect the split. | |
687 | */ | |
cee7f245 | 688 | static void split_blame(struct scoreboard *sb, |
54a4c617 | 689 | struct blame_entry *split, |
cee7f245 JH |
690 | struct blame_entry *e) |
691 | { | |
692 | struct blame_entry *new_entry; | |
693 | ||
694 | if (split[0].suspect && split[2].suspect) { | |
1732a1fd | 695 | /* The first part (reuse storage for the existing entry e) */ |
cee7f245 JH |
696 | dup_entry(e, &split[0]); |
697 | ||
1732a1fd | 698 | /* The last part -- me */ |
cee7f245 JH |
699 | new_entry = xmalloc(sizeof(*new_entry)); |
700 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
701 | add_blame_entry(sb, new_entry); | |
702 | ||
1732a1fd | 703 | /* ... and the middle part -- parent */ |
cee7f245 JH |
704 | new_entry = xmalloc(sizeof(*new_entry)); |
705 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
706 | add_blame_entry(sb, new_entry); | |
707 | } | |
708 | else if (!split[0].suspect && !split[2].suspect) | |
1732a1fd JH |
709 | /* |
710 | * The parent covers the entire area; reuse storage for | |
711 | * e and replace it with the parent. | |
712 | */ | |
cee7f245 JH |
713 | dup_entry(e, &split[1]); |
714 | else if (split[0].suspect) { | |
1732a1fd | 715 | /* me and then parent */ |
cee7f245 JH |
716 | dup_entry(e, &split[0]); |
717 | ||
718 | new_entry = xmalloc(sizeof(*new_entry)); | |
719 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
720 | add_blame_entry(sb, new_entry); | |
721 | } | |
722 | else { | |
1732a1fd | 723 | /* parent and then me */ |
cee7f245 JH |
724 | dup_entry(e, &split[1]); |
725 | ||
726 | new_entry = xmalloc(sizeof(*new_entry)); | |
727 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
728 | add_blame_entry(sb, new_entry); | |
729 | } | |
730 | ||
54a4c617 | 731 | if (DEBUG) { /* sanity */ |
cee7f245 | 732 | struct blame_entry *ent; |
612702e8 | 733 | int lno = sb->ent->lno, corrupt = 0; |
cee7f245 JH |
734 | |
735 | for (ent = sb->ent; ent; ent = ent->next) { | |
736 | if (lno != ent->lno) | |
737 | corrupt = 1; | |
738 | if (ent->s_lno < 0) | |
739 | corrupt = 1; | |
740 | lno += ent->num_lines; | |
741 | } | |
742 | if (corrupt) { | |
612702e8 | 743 | lno = sb->ent->lno; |
cee7f245 JH |
744 | for (ent = sb->ent; ent; ent = ent->next) { |
745 | printf("L %8d l %8d n %8d\n", | |
746 | lno, ent->lno, ent->num_lines); | |
747 | lno = ent->lno + ent->num_lines; | |
748 | } | |
749 | die("oops"); | |
750 | } | |
751 | } | |
752 | } | |
753 | ||
1732a1fd JH |
754 | /* |
755 | * After splitting the blame, the origins used by the | |
756 | * on-stack blame_entry should lose one refcnt each. | |
757 | */ | |
54a4c617 JH |
758 | static void decref_split(struct blame_entry *split) |
759 | { | |
760 | int i; | |
761 | ||
762 | for (i = 0; i < 3; i++) | |
763 | origin_decref(split[i].suspect); | |
764 | } | |
765 | ||
1732a1fd JH |
766 | /* |
767 | * Helper for blame_chunk(). blame_entry e is known to overlap with | |
768 | * the patch hunk; split it and pass blame to the parent. | |
769 | */ | |
cee7f245 JH |
770 | static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, |
771 | int tlno, int plno, int same, | |
772 | struct origin *parent) | |
773 | { | |
774 | struct blame_entry split[3]; | |
775 | ||
776 | split_overlap(split, e, tlno, plno, same, parent); | |
54a4c617 JH |
777 | if (split[1].suspect) |
778 | split_blame(sb, split, e); | |
779 | decref_split(split); | |
cee7f245 JH |
780 | } |
781 | ||
1732a1fd JH |
782 | /* |
783 | * Find the line number of the last line the target is suspected for. | |
784 | */ | |
cee7f245 JH |
785 | static int find_last_in_target(struct scoreboard *sb, struct origin *target) |
786 | { | |
787 | struct blame_entry *e; | |
788 | int last_in_target = -1; | |
789 | ||
790 | for (e = sb->ent; e; e = e->next) { | |
171dccd5 | 791 | if (e->guilty || !same_suspect(e->suspect, target)) |
cee7f245 JH |
792 | continue; |
793 | if (last_in_target < e->s_lno + e->num_lines) | |
794 | last_in_target = e->s_lno + e->num_lines; | |
795 | } | |
796 | return last_in_target; | |
797 | } | |
798 | ||
1732a1fd JH |
799 | /* |
800 | * Process one hunk from the patch between the current suspect for | |
801 | * blame_entry e and its parent. Find and split the overlap, and | |
802 | * pass blame to the overlapping part to the parent. | |
803 | */ | |
cee7f245 JH |
804 | static void blame_chunk(struct scoreboard *sb, |
805 | int tlno, int plno, int same, | |
806 | struct origin *target, struct origin *parent) | |
807 | { | |
612702e8 | 808 | struct blame_entry *e; |
cee7f245 | 809 | |
612702e8 | 810 | for (e = sb->ent; e; e = e->next) { |
171dccd5 | 811 | if (e->guilty || !same_suspect(e->suspect, target)) |
cee7f245 JH |
812 | continue; |
813 | if (same <= e->s_lno) | |
814 | continue; | |
815 | if (tlno < e->s_lno + e->num_lines) | |
816 | blame_overlap(sb, e, tlno, plno, same, parent); | |
817 | } | |
818 | } | |
819 | ||
1732a1fd JH |
820 | /* |
821 | * We are looking at the origin 'target' and aiming to pass blame | |
822 | * for the lines it is suspected to its parent. Run diff to find | |
823 | * which lines came from parent and pass blame for them. | |
824 | */ | |
cee7f245 JH |
825 | static int pass_blame_to_parent(struct scoreboard *sb, |
826 | struct origin *target, | |
827 | struct origin *parent) | |
828 | { | |
829 | int i, last_in_target, plno, tlno; | |
830 | struct patch *patch; | |
831 | ||
832 | last_in_target = find_last_in_target(sb, target); | |
833 | if (last_in_target < 0) | |
834 | return 1; /* nothing remains for this target */ | |
835 | ||
836 | patch = get_patch(parent, target); | |
837 | plno = tlno = 0; | |
838 | for (i = 0; i < patch->num; i++) { | |
839 | struct chunk *chunk = &patch->chunks[i]; | |
840 | ||
cee7f245 JH |
841 | blame_chunk(sb, tlno, plno, chunk->same, target, parent); |
842 | plno = chunk->p_next; | |
843 | tlno = chunk->t_next; | |
844 | } | |
1732a1fd | 845 | /* The rest (i.e. anything after tlno) are the same as the parent */ |
cee7f245 JH |
846 | blame_chunk(sb, tlno, plno, last_in_target, target, parent); |
847 | ||
848 | free_patch(patch); | |
849 | return 0; | |
850 | } | |
851 | ||
1732a1fd JH |
852 | /* |
853 | * The lines in blame_entry after splitting blames many times can become | |
854 | * very small and trivial, and at some point it becomes pointless to | |
855 | * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any | |
856 | * ordinary C program, and it is not worth to say it was copied from | |
857 | * totally unrelated file in the parent. | |
858 | * | |
859 | * Compute how trivial the lines in the blame_entry are. | |
860 | */ | |
5ff62c30 JH |
861 | static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e) |
862 | { | |
863 | unsigned score; | |
864 | const char *cp, *ep; | |
865 | ||
866 | if (e->score) | |
867 | return e->score; | |
868 | ||
612702e8 | 869 | score = 1; |
5ff62c30 JH |
870 | cp = nth_line(sb, e->lno); |
871 | ep = nth_line(sb, e->lno + e->num_lines); | |
872 | while (cp < ep) { | |
873 | unsigned ch = *((unsigned char *)cp); | |
874 | if (isalnum(ch)) | |
875 | score++; | |
876 | cp++; | |
877 | } | |
878 | e->score = score; | |
879 | return score; | |
880 | } | |
881 | ||
1732a1fd JH |
882 | /* |
883 | * best_so_far[] and this[] are both a split of an existing blame_entry | |
884 | * that passes blame to the parent. Maintain best_so_far the best split | |
885 | * so far, by comparing this and best_so_far and copying this into | |
886 | * bst_so_far as needed. | |
887 | */ | |
5ff62c30 | 888 | static void copy_split_if_better(struct scoreboard *sb, |
54a4c617 JH |
889 | struct blame_entry *best_so_far, |
890 | struct blame_entry *this) | |
d24bba80 | 891 | { |
54a4c617 JH |
892 | int i; |
893 | ||
d24bba80 JH |
894 | if (!this[1].suspect) |
895 | return; | |
5ff62c30 JH |
896 | if (best_so_far[1].suspect) { |
897 | if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1])) | |
898 | return; | |
899 | } | |
54a4c617 JH |
900 | |
901 | for (i = 0; i < 3; i++) | |
902 | origin_incref(this[i].suspect); | |
903 | decref_split(best_so_far); | |
d24bba80 JH |
904 | memcpy(best_so_far, this, sizeof(struct blame_entry [3])); |
905 | } | |
906 | ||
dd166aa8 JH |
907 | /* |
908 | * We are looking at a part of the final image represented by | |
909 | * ent (tlno and same are offset by ent->s_lno). | |
910 | * tlno is where we are looking at in the final image. | |
911 | * up to (but not including) same match preimage. | |
912 | * plno is where we are looking at in the preimage. | |
913 | * | |
914 | * <-------------- final image ----------------------> | |
915 | * <------ent------> | |
916 | * ^tlno ^same | |
917 | * <---------preimage-----> | |
918 | * ^plno | |
919 | * | |
920 | * All line numbers are 0-based. | |
921 | */ | |
922 | static void handle_split(struct scoreboard *sb, | |
923 | struct blame_entry *ent, | |
924 | int tlno, int plno, int same, | |
925 | struct origin *parent, | |
926 | struct blame_entry *split) | |
927 | { | |
928 | if (ent->num_lines <= tlno) | |
929 | return; | |
930 | if (tlno < same) { | |
931 | struct blame_entry this[3]; | |
932 | tlno += ent->s_lno; | |
933 | same += ent->s_lno; | |
934 | split_overlap(this, ent, tlno, plno, same, parent); | |
935 | copy_split_if_better(sb, split, this); | |
936 | decref_split(this); | |
937 | } | |
938 | } | |
939 | ||
1732a1fd JH |
940 | /* |
941 | * Find the lines from parent that are the same as ent so that | |
942 | * we can pass blames to it. file_p has the blob contents for | |
943 | * the parent. | |
944 | */ | |
d24bba80 JH |
945 | static void find_copy_in_blob(struct scoreboard *sb, |
946 | struct blame_entry *ent, | |
947 | struct origin *parent, | |
54a4c617 | 948 | struct blame_entry *split, |
d24bba80 JH |
949 | mmfile_t *file_p) |
950 | { | |
951 | const char *cp; | |
952 | int cnt; | |
953 | mmfile_t file_o; | |
954 | struct patch *patch; | |
955 | int i, plno, tlno; | |
956 | ||
1732a1fd JH |
957 | /* |
958 | * Prepare mmfile that contains only the lines in ent. | |
959 | */ | |
d24bba80 JH |
960 | cp = nth_line(sb, ent->lno); |
961 | file_o.ptr = (char*) cp; | |
962 | cnt = ent->num_lines; | |
963 | ||
964 | while (cnt && cp < sb->final_buf + sb->final_buf_size) { | |
965 | if (*cp++ == '\n') | |
966 | cnt--; | |
967 | } | |
968 | file_o.size = cp - file_o.ptr; | |
969 | ||
970 | patch = compare_buffer(file_p, &file_o, 1); | |
971 | ||
dd166aa8 JH |
972 | /* |
973 | * file_o is a part of final image we are annotating. | |
974 | * file_p partially may match that image. | |
975 | */ | |
d24bba80 JH |
976 | memset(split, 0, sizeof(struct blame_entry [3])); |
977 | plno = tlno = 0; | |
978 | for (i = 0; i < patch->num; i++) { | |
979 | struct chunk *chunk = &patch->chunks[i]; | |
980 | ||
dd166aa8 | 981 | handle_split(sb, ent, tlno, plno, chunk->same, parent, split); |
d24bba80 JH |
982 | plno = chunk->p_next; |
983 | tlno = chunk->t_next; | |
984 | } | |
dd166aa8 JH |
985 | /* remainder, if any, all match the preimage */ |
986 | handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split); | |
d24bba80 JH |
987 | free_patch(patch); |
988 | } | |
989 | ||
1732a1fd JH |
990 | /* |
991 | * See if lines currently target is suspected for can be attributed to | |
992 | * parent. | |
993 | */ | |
d24bba80 JH |
994 | static int find_move_in_parent(struct scoreboard *sb, |
995 | struct origin *target, | |
996 | struct origin *parent) | |
997 | { | |
650e2f67 | 998 | int last_in_target, made_progress; |
46014766 | 999 | struct blame_entry *e, split[3]; |
d24bba80 | 1000 | mmfile_t file_p; |
d24bba80 JH |
1001 | |
1002 | last_in_target = find_last_in_target(sb, target); | |
1003 | if (last_in_target < 0) | |
1004 | return 1; /* nothing remains for this target */ | |
1005 | ||
c2e525d9 JH |
1006 | fill_origin_blob(parent, &file_p); |
1007 | if (!file_p.ptr) | |
d24bba80 | 1008 | return 0; |
d24bba80 | 1009 | |
650e2f67 JH |
1010 | made_progress = 1; |
1011 | while (made_progress) { | |
1012 | made_progress = 0; | |
1013 | for (e = sb->ent; e; e = e->next) { | |
1617fe98 AG |
1014 | if (e->guilty || !same_suspect(e->suspect, target) || |
1015 | ent_score(sb, e) < blame_move_score) | |
650e2f67 JH |
1016 | continue; |
1017 | find_copy_in_blob(sb, e, parent, split, &file_p); | |
1018 | if (split[1].suspect && | |
1019 | blame_move_score < ent_score(sb, &split[1])) { | |
1020 | split_blame(sb, split, e); | |
1021 | made_progress = 1; | |
1022 | } | |
1023 | decref_split(split); | |
1024 | } | |
d24bba80 | 1025 | } |
d24bba80 JH |
1026 | return 0; |
1027 | } | |
1028 | ||
33494784 JH |
1029 | struct blame_list { |
1030 | struct blame_entry *ent; | |
1031 | struct blame_entry split[3]; | |
1032 | }; | |
1033 | ||
1732a1fd JH |
1034 | /* |
1035 | * Count the number of entries the target is suspected for, | |
1036 | * and prepare a list of entry and the best split. | |
1037 | */ | |
33494784 JH |
1038 | static struct blame_list *setup_blame_list(struct scoreboard *sb, |
1039 | struct origin *target, | |
1617fe98 | 1040 | int min_score, |
33494784 JH |
1041 | int *num_ents_p) |
1042 | { | |
1043 | struct blame_entry *e; | |
1044 | int num_ents, i; | |
1045 | struct blame_list *blame_list = NULL; | |
1046 | ||
33494784 | 1047 | for (e = sb->ent, num_ents = 0; e; e = e->next) |
1617fe98 AG |
1048 | if (!e->scanned && !e->guilty && |
1049 | same_suspect(e->suspect, target) && | |
1050 | min_score < ent_score(sb, e)) | |
33494784 JH |
1051 | num_ents++; |
1052 | if (num_ents) { | |
1053 | blame_list = xcalloc(num_ents, sizeof(struct blame_list)); | |
1054 | for (e = sb->ent, i = 0; e; e = e->next) | |
1617fe98 AG |
1055 | if (!e->scanned && !e->guilty && |
1056 | same_suspect(e->suspect, target) && | |
1057 | min_score < ent_score(sb, e)) | |
33494784 JH |
1058 | blame_list[i++].ent = e; |
1059 | } | |
1060 | *num_ents_p = num_ents; | |
1061 | return blame_list; | |
1062 | } | |
1063 | ||
eb46328e AG |
1064 | /* |
1065 | * Reset the scanned status on all entries. | |
1066 | */ | |
1067 | static void reset_scanned_flag(struct scoreboard *sb) | |
1068 | { | |
1069 | struct blame_entry *e; | |
1070 | for (e = sb->ent; e; e = e->next) | |
1071 | e->scanned = 0; | |
1072 | } | |
1073 | ||
1732a1fd JH |
1074 | /* |
1075 | * For lines target is suspected for, see if we can find code movement | |
1076 | * across file boundary from the parent commit. porigin is the path | |
1077 | * in the parent we already tried. | |
1078 | */ | |
18abd745 JH |
1079 | static int find_copy_in_parent(struct scoreboard *sb, |
1080 | struct origin *target, | |
1081 | struct commit *parent, | |
1082 | struct origin *porigin, | |
1083 | int opt) | |
1084 | { | |
1085 | struct diff_options diff_opts; | |
1086 | const char *paths[1]; | |
aec8fa1f | 1087 | int i, j; |
33494784 JH |
1088 | int retval; |
1089 | struct blame_list *blame_list; | |
aec8fa1f | 1090 | int num_ents; |
18abd745 | 1091 | |
1617fe98 | 1092 | blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents); |
33494784 | 1093 | if (!blame_list) |
18abd745 JH |
1094 | return 1; /* nothing remains for this target */ |
1095 | ||
1096 | diff_setup(&diff_opts); | |
8f67f8ae | 1097 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
18abd745 JH |
1098 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
1099 | ||
18abd745 JH |
1100 | paths[0] = NULL; |
1101 | diff_tree_setup_paths(paths, &diff_opts); | |
1102 | if (diff_setup_done(&diff_opts) < 0) | |
1103 | die("diff-setup"); | |
33494784 JH |
1104 | |
1105 | /* Try "find copies harder" on new path if requested; | |
1106 | * we do not want to use diffcore_rename() actually to | |
1107 | * match things up; find_copies_harder is set only to | |
1108 | * force diff_tree_sha1() to feed all filepairs to diff_queue, | |
1109 | * and this code needs to be after diff_setup_done(), which | |
1110 | * usually makes find-copies-harder imply copy detection. | |
1111 | */ | |
c63777c0 JH |
1112 | if ((opt & PICKAXE_BLAME_COPY_HARDEST) |
1113 | || ((opt & PICKAXE_BLAME_COPY_HARDER) | |
1114 | && (!porigin || strcmp(target->path, porigin->path)))) | |
8f67f8ae | 1115 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
33494784 | 1116 | |
1cfe7733 JH |
1117 | if (is_null_sha1(target->commit->object.sha1)) |
1118 | do_diff_cache(parent->tree->object.sha1, &diff_opts); | |
1119 | else | |
1120 | diff_tree_sha1(parent->tree->object.sha1, | |
1121 | target->commit->tree->object.sha1, | |
1122 | "", &diff_opts); | |
18abd745 | 1123 | |
8f67f8ae | 1124 | if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER)) |
33494784 | 1125 | diffcore_std(&diff_opts); |
18abd745 | 1126 | |
33494784 JH |
1127 | retval = 0; |
1128 | while (1) { | |
1129 | int made_progress = 0; | |
1130 | ||
1131 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
1132 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
1133 | struct origin *norigin; | |
1134 | mmfile_t file_p; | |
33494784 JH |
1135 | struct blame_entry this[3]; |
1136 | ||
1137 | if (!DIFF_FILE_VALID(p->one)) | |
1138 | continue; /* does not exist in parent */ | |
1139 | if (porigin && !strcmp(p->one->path, porigin->path)) | |
1140 | /* find_move already dealt with this path */ | |
1141 | continue; | |
1142 | ||
1143 | norigin = get_origin(sb, parent, p->one->path); | |
1144 | hashcpy(norigin->blob_sha1, p->one->sha1); | |
c2e525d9 | 1145 | fill_origin_blob(norigin, &file_p); |
33494784 JH |
1146 | if (!file_p.ptr) |
1147 | continue; | |
1148 | ||
1149 | for (j = 0; j < num_ents; j++) { | |
1150 | find_copy_in_blob(sb, blame_list[j].ent, | |
1151 | norigin, this, &file_p); | |
1152 | copy_split_if_better(sb, blame_list[j].split, | |
1153 | this); | |
1154 | decref_split(this); | |
1155 | } | |
33494784 JH |
1156 | origin_decref(norigin); |
1157 | } | |
18abd745 | 1158 | |
aec8fa1f | 1159 | for (j = 0; j < num_ents; j++) { |
33494784 JH |
1160 | struct blame_entry *split = blame_list[j].split; |
1161 | if (split[1].suspect && | |
1162 | blame_copy_score < ent_score(sb, &split[1])) { | |
1163 | split_blame(sb, split, blame_list[j].ent); | |
1164 | made_progress = 1; | |
1165 | } | |
eb46328e AG |
1166 | else |
1167 | blame_list[j].ent->scanned = 1; | |
33494784 | 1168 | decref_split(split); |
18abd745 | 1169 | } |
33494784 | 1170 | free(blame_list); |
aec8fa1f | 1171 | |
33494784 JH |
1172 | if (!made_progress) |
1173 | break; | |
1617fe98 | 1174 | blame_list = setup_blame_list(sb, target, blame_copy_score, &num_ents); |
33494784 JH |
1175 | if (!blame_list) { |
1176 | retval = 1; | |
1177 | break; | |
1178 | } | |
18abd745 | 1179 | } |
eb46328e | 1180 | reset_scanned_flag(sb); |
33494784 | 1181 | diff_flush(&diff_opts); |
03b69c76 | 1182 | diff_tree_release_paths(&diff_opts); |
33494784 | 1183 | return retval; |
18abd745 JH |
1184 | } |
1185 | ||
1732a1fd JH |
1186 | /* |
1187 | * The blobs of origin and porigin exactly match, so everything | |
c2e525d9 JH |
1188 | * origin is suspected for can be blamed on the parent. |
1189 | */ | |
1190 | static void pass_whole_blame(struct scoreboard *sb, | |
1191 | struct origin *origin, struct origin *porigin) | |
1192 | { | |
1193 | struct blame_entry *e; | |
1194 | ||
1195 | if (!porigin->file.ptr && origin->file.ptr) { | |
1196 | /* Steal its file */ | |
1197 | porigin->file = origin->file; | |
1198 | origin->file.ptr = NULL; | |
1199 | } | |
1200 | for (e = sb->ent; e; e = e->next) { | |
171dccd5 | 1201 | if (!same_suspect(e->suspect, origin)) |
c2e525d9 JH |
1202 | continue; |
1203 | origin_incref(porigin); | |
1204 | origin_decref(e->suspect); | |
1205 | e->suspect = porigin; | |
1206 | } | |
1207 | } | |
1208 | ||
69264f46 JH |
1209 | /* |
1210 | * We pass blame from the current commit to its parents. We keep saying | |
1211 | * "parent" (and "porigin"), but what we mean is to find scapegoat to | |
1212 | * exonerate ourselves. | |
1213 | */ | |
85af7929 | 1214 | static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit) |
69264f46 | 1215 | { |
85af7929 JH |
1216 | if (!reverse) |
1217 | return commit->parents; | |
1218 | return lookup_decoration(&revs->children, &commit->object); | |
69264f46 JH |
1219 | } |
1220 | ||
85af7929 | 1221 | static int num_scapegoats(struct rev_info *revs, struct commit *commit) |
69264f46 JH |
1222 | { |
1223 | int cnt; | |
85af7929 | 1224 | struct commit_list *l = first_scapegoat(revs, commit); |
69264f46 JH |
1225 | for (cnt = 0; l; l = l->next) |
1226 | cnt++; | |
1227 | return cnt; | |
1228 | } | |
1229 | ||
1230 | #define MAXSG 16 | |
cee7f245 | 1231 | |
d24bba80 | 1232 | static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt) |
cee7f245 | 1233 | { |
85af7929 | 1234 | struct rev_info *revs = sb->revs; |
69264f46 | 1235 | int i, pass, num_sg; |
cee7f245 | 1236 | struct commit *commit = origin->commit; |
69264f46 JH |
1237 | struct commit_list *sg; |
1238 | struct origin *sg_buf[MAXSG]; | |
1239 | struct origin *porigin, **sg_origin = sg_buf; | |
1240 | ||
85af7929 | 1241 | num_sg = num_scapegoats(revs, commit); |
69264f46 JH |
1242 | if (!num_sg) |
1243 | goto finish; | |
1244 | else if (num_sg < ARRAY_SIZE(sg_buf)) | |
1245 | memset(sg_buf, 0, sizeof(sg_buf)); | |
1246 | else | |
1247 | sg_origin = xcalloc(num_sg, sizeof(*sg_origin)); | |
cee7f245 | 1248 | |
69264f46 JH |
1249 | /* |
1250 | * The first pass looks for unrenamed path to optimize for | |
f69e743d JH |
1251 | * common cases, then we look for renames in the second pass. |
1252 | */ | |
1253 | for (pass = 0; pass < 2; pass++) { | |
1254 | struct origin *(*find)(struct scoreboard *, | |
1255 | struct commit *, struct origin *); | |
1256 | find = pass ? find_rename : find_origin; | |
1257 | ||
85af7929 | 1258 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1259 | i < num_sg && sg; |
1260 | sg = sg->next, i++) { | |
1261 | struct commit *p = sg->item; | |
0421d9f8 | 1262 | int j, same; |
f69e743d | 1263 | |
69264f46 | 1264 | if (sg_origin[i]) |
f69e743d JH |
1265 | continue; |
1266 | if (parse_commit(p)) | |
1267 | continue; | |
0d981c67 | 1268 | porigin = find(sb, p, origin); |
f69e743d JH |
1269 | if (!porigin) |
1270 | continue; | |
1271 | if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) { | |
c2e525d9 | 1272 | pass_whole_blame(sb, origin, porigin); |
f69e743d JH |
1273 | origin_decref(porigin); |
1274 | goto finish; | |
1275 | } | |
0421d9f8 | 1276 | for (j = same = 0; j < i; j++) |
69264f46 JH |
1277 | if (sg_origin[j] && |
1278 | !hashcmp(sg_origin[j]->blob_sha1, | |
0421d9f8 JH |
1279 | porigin->blob_sha1)) { |
1280 | same = 1; | |
1281 | break; | |
1282 | } | |
1283 | if (!same) | |
69264f46 | 1284 | sg_origin[i] = porigin; |
0421d9f8 JH |
1285 | else |
1286 | origin_decref(porigin); | |
cee7f245 | 1287 | } |
cee7f245 JH |
1288 | } |
1289 | ||
c2e525d9 | 1290 | num_commits++; |
85af7929 | 1291 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1292 | i < num_sg && sg; |
1293 | sg = sg->next, i++) { | |
1294 | struct origin *porigin = sg_origin[i]; | |
cee7f245 JH |
1295 | if (!porigin) |
1296 | continue; | |
1297 | if (pass_blame_to_parent(sb, origin, porigin)) | |
54a4c617 | 1298 | goto finish; |
cee7f245 | 1299 | } |
d24bba80 JH |
1300 | |
1301 | /* | |
1732a1fd | 1302 | * Optionally find moves in parents' files. |
d24bba80 JH |
1303 | */ |
1304 | if (opt & PICKAXE_BLAME_MOVE) | |
85af7929 | 1305 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1306 | i < num_sg && sg; |
1307 | sg = sg->next, i++) { | |
1308 | struct origin *porigin = sg_origin[i]; | |
d24bba80 JH |
1309 | if (!porigin) |
1310 | continue; | |
1311 | if (find_move_in_parent(sb, origin, porigin)) | |
54a4c617 | 1312 | goto finish; |
d24bba80 JH |
1313 | } |
1314 | ||
18abd745 | 1315 | /* |
1732a1fd | 1316 | * Optionally find copies from parents' files. |
18abd745 JH |
1317 | */ |
1318 | if (opt & PICKAXE_BLAME_COPY) | |
85af7929 | 1319 | for (i = 0, sg = first_scapegoat(revs, commit); |
69264f46 JH |
1320 | i < num_sg && sg; |
1321 | sg = sg->next, i++) { | |
1322 | struct origin *porigin = sg_origin[i]; | |
1323 | if (find_copy_in_parent(sb, origin, sg->item, | |
18abd745 | 1324 | porigin, opt)) |
54a4c617 | 1325 | goto finish; |
18abd745 | 1326 | } |
54a4c617 JH |
1327 | |
1328 | finish: | |
69264f46 JH |
1329 | for (i = 0; i < num_sg; i++) { |
1330 | if (sg_origin[i]) { | |
1331 | drop_origin_blob(sg_origin[i]); | |
1332 | origin_decref(sg_origin[i]); | |
7c3c7962 JH |
1333 | } |
1334 | } | |
1335 | drop_origin_blob(origin); | |
69264f46 JH |
1336 | if (sg_buf != sg_origin) |
1337 | free(sg_origin); | |
cee7f245 JH |
1338 | } |
1339 | ||
1732a1fd JH |
1340 | /* |
1341 | * Information on commits, used for output. | |
1342 | */ | |
cee7f245 JH |
1343 | struct commit_info |
1344 | { | |
3a55602e SP |
1345 | const char *author; |
1346 | const char *author_mail; | |
cee7f245 | 1347 | unsigned long author_time; |
3a55602e | 1348 | const char *author_tz; |
cee7f245 JH |
1349 | |
1350 | /* filled only when asked for details */ | |
3a55602e SP |
1351 | const char *committer; |
1352 | const char *committer_mail; | |
cee7f245 | 1353 | unsigned long committer_time; |
3a55602e | 1354 | const char *committer_tz; |
cee7f245 | 1355 | |
3a55602e | 1356 | const char *summary; |
cee7f245 JH |
1357 | }; |
1358 | ||
1732a1fd JH |
1359 | /* |
1360 | * Parse author/committer line in the commit object buffer | |
1361 | */ | |
cee7f245 | 1362 | static void get_ac_line(const char *inbuf, const char *what, |
3a55602e SP |
1363 | int bufsz, char *person, const char **mail, |
1364 | unsigned long *time, const char **tz) | |
cee7f245 | 1365 | { |
f9567384 JH |
1366 | int len, tzlen, maillen; |
1367 | char *tmp, *endp, *timepos; | |
cee7f245 JH |
1368 | |
1369 | tmp = strstr(inbuf, what); | |
1370 | if (!tmp) | |
1371 | goto error_out; | |
1372 | tmp += strlen(what); | |
1373 | endp = strchr(tmp, '\n'); | |
1374 | if (!endp) | |
1375 | len = strlen(tmp); | |
1376 | else | |
1377 | len = endp - tmp; | |
1378 | if (bufsz <= len) { | |
1379 | error_out: | |
1380 | /* Ugh */ | |
3a55602e | 1381 | *mail = *tz = "(unknown)"; |
cee7f245 JH |
1382 | *time = 0; |
1383 | return; | |
1384 | } | |
1385 | memcpy(person, tmp, len); | |
1386 | ||
1387 | tmp = person; | |
1388 | tmp += len; | |
1389 | *tmp = 0; | |
1390 | while (*tmp != ' ') | |
1391 | tmp--; | |
1392 | *tz = tmp+1; | |
f9567384 | 1393 | tzlen = (person+len)-(tmp+1); |
cee7f245 JH |
1394 | |
1395 | *tmp = 0; | |
1396 | while (*tmp != ' ') | |
1397 | tmp--; | |
1398 | *time = strtoul(tmp, NULL, 10); | |
f9567384 | 1399 | timepos = tmp; |
cee7f245 JH |
1400 | |
1401 | *tmp = 0; | |
1402 | while (*tmp != ' ') | |
1403 | tmp--; | |
1404 | *mail = tmp + 1; | |
1405 | *tmp = 0; | |
f9567384 JH |
1406 | maillen = timepos - tmp; |
1407 | ||
1408 | if (!mailmap.nr) | |
1409 | return; | |
1410 | ||
1411 | /* | |
1412 | * mailmap expansion may make the name longer. | |
1413 | * make room by pushing stuff down. | |
1414 | */ | |
1415 | tmp = person + bufsz - (tzlen + 1); | |
1416 | memmove(tmp, *tz, tzlen); | |
1417 | tmp[tzlen] = 0; | |
1418 | *tz = tmp; | |
1419 | ||
1420 | tmp = tmp - (maillen + 1); | |
1421 | memmove(tmp, *mail, maillen); | |
1422 | tmp[maillen] = 0; | |
1423 | *mail = tmp; | |
1424 | ||
1425 | /* | |
1426 | * Now, convert e-mail using mailmap | |
1427 | */ | |
1428 | map_email(&mailmap, tmp + 1, person, tmp-person-1); | |
cee7f245 JH |
1429 | } |
1430 | ||
1431 | static void get_commit_info(struct commit *commit, | |
1432 | struct commit_info *ret, | |
1433 | int detailed) | |
1434 | { | |
1435 | int len; | |
1436 | char *tmp, *endp; | |
1437 | static char author_buf[1024]; | |
1438 | static char committer_buf[1024]; | |
1439 | static char summary_buf[1024]; | |
1440 | ||
1732a1fd JH |
1441 | /* |
1442 | * We've operated without save_commit_buffer, so | |
612702e8 JH |
1443 | * we now need to populate them for output. |
1444 | */ | |
1445 | if (!commit->buffer) { | |
21666f1a | 1446 | enum object_type type; |
612702e8 JH |
1447 | unsigned long size; |
1448 | commit->buffer = | |
21666f1a | 1449 | read_sha1_file(commit->object.sha1, &type, &size); |
ab43e495 JH |
1450 | if (!commit->buffer) |
1451 | die("Cannot read commit %s", | |
1452 | sha1_to_hex(commit->object.sha1)); | |
612702e8 | 1453 | } |
cee7f245 JH |
1454 | ret->author = author_buf; |
1455 | get_ac_line(commit->buffer, "\nauthor ", | |
1456 | sizeof(author_buf), author_buf, &ret->author_mail, | |
1457 | &ret->author_time, &ret->author_tz); | |
1458 | ||
1459 | if (!detailed) | |
1460 | return; | |
1461 | ||
1462 | ret->committer = committer_buf; | |
1463 | get_ac_line(commit->buffer, "\ncommitter ", | |
1464 | sizeof(committer_buf), committer_buf, &ret->committer_mail, | |
1465 | &ret->committer_time, &ret->committer_tz); | |
1466 | ||
1467 | ret->summary = summary_buf; | |
1468 | tmp = strstr(commit->buffer, "\n\n"); | |
1469 | if (!tmp) { | |
1470 | error_out: | |
1471 | sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1)); | |
1472 | return; | |
1473 | } | |
1474 | tmp += 2; | |
1475 | endp = strchr(tmp, '\n'); | |
1476 | if (!endp) | |
1cfe7733 | 1477 | endp = tmp + strlen(tmp); |
cee7f245 | 1478 | len = endp - tmp; |
1cfe7733 | 1479 | if (len >= sizeof(summary_buf) || len == 0) |
cee7f245 JH |
1480 | goto error_out; |
1481 | memcpy(summary_buf, tmp, len); | |
1482 | summary_buf[len] = 0; | |
1483 | } | |
1484 | ||
1732a1fd JH |
1485 | /* |
1486 | * To allow LF and other nonportable characters in pathnames, | |
1487 | * they are c-style quoted as needed. | |
1488 | */ | |
46e5e69d JH |
1489 | static void write_filename_info(const char *path) |
1490 | { | |
1491 | printf("filename "); | |
663af342 | 1492 | write_name_quoted(path, stdout, '\n'); |
46e5e69d JH |
1493 | } |
1494 | ||
1732a1fd JH |
1495 | /* |
1496 | * The blame_entry is found to be guilty for the range. Mark it | |
1497 | * as such, and show it in incremental output. | |
1498 | */ | |
717d1462 LT |
1499 | static void found_guilty_entry(struct blame_entry *ent) |
1500 | { | |
1501 | if (ent->guilty) | |
1502 | return; | |
1503 | ent->guilty = 1; | |
1504 | if (incremental) { | |
1505 | struct origin *suspect = ent->suspect; | |
1506 | ||
1507 | printf("%s %d %d %d\n", | |
1508 | sha1_to_hex(suspect->commit->object.sha1), | |
1509 | ent->s_lno + 1, ent->lno + 1, ent->num_lines); | |
1510 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { | |
1511 | struct commit_info ci; | |
1512 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
1513 | get_commit_info(suspect->commit, &ci, 1); | |
1514 | printf("author %s\n", ci.author); | |
1515 | printf("author-mail %s\n", ci.author_mail); | |
1516 | printf("author-time %lu\n", ci.author_time); | |
1517 | printf("author-tz %s\n", ci.author_tz); | |
1518 | printf("committer %s\n", ci.committer); | |
1519 | printf("committer-mail %s\n", ci.committer_mail); | |
1520 | printf("committer-time %lu\n", ci.committer_time); | |
1521 | printf("committer-tz %s\n", ci.committer_tz); | |
1522 | printf("summary %s\n", ci.summary); | |
1523 | if (suspect->commit->object.flags & UNINTERESTING) | |
1524 | printf("boundary\n"); | |
1525 | } | |
46e5e69d | 1526 | write_filename_info(suspect->path); |
06f59e9f | 1527 | maybe_flush_or_die(stdout, "stdout"); |
717d1462 LT |
1528 | } |
1529 | } | |
1530 | ||
1732a1fd JH |
1531 | /* |
1532 | * The main loop -- while the scoreboard has lines whose true origin | |
3dff5379 | 1533 | * is still unknown, pick one blame_entry, and allow its current |
1732a1fd JH |
1534 | * suspect to pass blames to its parents. |
1535 | */ | |
85af7929 | 1536 | static void assign_blame(struct scoreboard *sb, int opt) |
717d1462 | 1537 | { |
85af7929 JH |
1538 | struct rev_info *revs = sb->revs; |
1539 | ||
717d1462 LT |
1540 | while (1) { |
1541 | struct blame_entry *ent; | |
1542 | struct commit *commit; | |
1543 | struct origin *suspect = NULL; | |
1544 | ||
1545 | /* find one suspect to break down */ | |
1546 | for (ent = sb->ent; !suspect && ent; ent = ent->next) | |
1547 | if (!ent->guilty) | |
1548 | suspect = ent->suspect; | |
1549 | if (!suspect) | |
1550 | return; /* all done */ | |
1551 | ||
1732a1fd JH |
1552 | /* |
1553 | * We will use this suspect later in the loop, | |
1554 | * so hold onto it in the meantime. | |
1555 | */ | |
717d1462 LT |
1556 | origin_incref(suspect); |
1557 | commit = suspect->commit; | |
1558 | if (!commit->object.parsed) | |
1559 | parse_commit(commit); | |
85af7929 JH |
1560 | if (reverse || |
1561 | (!(commit->object.flags & UNINTERESTING) && | |
1562 | !(revs->max_age != -1 && commit->date < revs->max_age))) | |
717d1462 LT |
1563 | pass_blame(sb, suspect, opt); |
1564 | else { | |
1565 | commit->object.flags |= UNINTERESTING; | |
1566 | if (commit->object.parsed) | |
1567 | mark_parents_uninteresting(commit); | |
1568 | } | |
1569 | /* treat root commit as boundary */ | |
1570 | if (!commit->parents && !show_root) | |
1571 | commit->object.flags |= UNINTERESTING; | |
1572 | ||
1573 | /* Take responsibility for the remaining entries */ | |
1574 | for (ent = sb->ent; ent; ent = ent->next) | |
171dccd5 | 1575 | if (same_suspect(ent->suspect, suspect)) |
717d1462 LT |
1576 | found_guilty_entry(ent); |
1577 | origin_decref(suspect); | |
1578 | ||
1579 | if (DEBUG) /* sanity */ | |
1580 | sanity_check_refcnt(sb); | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | static const char *format_time(unsigned long time, const char *tz_str, | |
1585 | int show_raw_time) | |
1586 | { | |
1587 | static char time_buf[128]; | |
1588 | time_t t = time; | |
1589 | int minutes, tz; | |
1590 | struct tm *tm; | |
1591 | ||
1592 | if (show_raw_time) { | |
1593 | sprintf(time_buf, "%lu %s", time, tz_str); | |
1594 | return time_buf; | |
1595 | } | |
1596 | ||
1597 | tz = atoi(tz_str); | |
1598 | minutes = tz < 0 ? -tz : tz; | |
1599 | minutes = (minutes / 100)*60 + (minutes % 100); | |
1600 | minutes = tz < 0 ? -minutes : minutes; | |
1601 | t = time + minutes * 60; | |
1602 | tm = gmtime(&t); | |
1603 | ||
1604 | strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm); | |
1605 | strcat(time_buf, tz_str); | |
1606 | return time_buf; | |
1607 | } | |
1608 | ||
cee7f245 JH |
1609 | #define OUTPUT_ANNOTATE_COMPAT 001 |
1610 | #define OUTPUT_LONG_OBJECT_NAME 002 | |
1611 | #define OUTPUT_RAW_TIMESTAMP 004 | |
1612 | #define OUTPUT_PORCELAIN 010 | |
1613 | #define OUTPUT_SHOW_NAME 020 | |
1614 | #define OUTPUT_SHOW_NUMBER 040 | |
5ff62c30 | 1615 | #define OUTPUT_SHOW_SCORE 0100 |
093dc5be | 1616 | #define OUTPUT_NO_AUTHOR 0200 |
cee7f245 JH |
1617 | |
1618 | static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent) | |
1619 | { | |
1620 | int cnt; | |
1621 | const char *cp; | |
1622 | struct origin *suspect = ent->suspect; | |
1623 | char hex[41]; | |
1624 | ||
1625 | strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); | |
1626 | printf("%s%c%d %d %d\n", | |
1627 | hex, | |
1628 | ent->guilty ? ' ' : '*', // purely for debugging | |
1629 | ent->s_lno + 1, | |
1630 | ent->lno + 1, | |
1631 | ent->num_lines); | |
1632 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { | |
1633 | struct commit_info ci; | |
1634 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
1635 | get_commit_info(suspect->commit, &ci, 1); | |
1636 | printf("author %s\n", ci.author); | |
1637 | printf("author-mail %s\n", ci.author_mail); | |
1638 | printf("author-time %lu\n", ci.author_time); | |
1639 | printf("author-tz %s\n", ci.author_tz); | |
1640 | printf("committer %s\n", ci.committer); | |
1641 | printf("committer-mail %s\n", ci.committer_mail); | |
1642 | printf("committer-time %lu\n", ci.committer_time); | |
1643 | printf("committer-tz %s\n", ci.committer_tz); | |
46e5e69d | 1644 | write_filename_info(suspect->path); |
cee7f245 | 1645 | printf("summary %s\n", ci.summary); |
b11121d9 JH |
1646 | if (suspect->commit->object.flags & UNINTERESTING) |
1647 | printf("boundary\n"); | |
cee7f245 JH |
1648 | } |
1649 | else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH) | |
46e5e69d | 1650 | write_filename_info(suspect->path); |
cee7f245 JH |
1651 | |
1652 | cp = nth_line(sb, ent->lno); | |
1653 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
1654 | char ch; | |
1655 | if (cnt) | |
1656 | printf("%s %d %d\n", hex, | |
1657 | ent->s_lno + 1 + cnt, | |
1658 | ent->lno + 1 + cnt); | |
1659 | putchar('\t'); | |
1660 | do { | |
1661 | ch = *cp++; | |
1662 | putchar(ch); | |
1663 | } while (ch != '\n' && | |
1664 | cp < sb->final_buf + sb->final_buf_size); | |
1665 | } | |
1666 | } | |
1667 | ||
1668 | static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt) | |
1669 | { | |
1670 | int cnt; | |
1671 | const char *cp; | |
1672 | struct origin *suspect = ent->suspect; | |
1673 | struct commit_info ci; | |
1674 | char hex[41]; | |
1675 | int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); | |
1676 | ||
1677 | get_commit_info(suspect->commit, &ci, 1); | |
1678 | strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); | |
1679 | ||
1680 | cp = nth_line(sb, ent->lno); | |
1681 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
1682 | char ch; | |
b11121d9 JH |
1683 | int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8; |
1684 | ||
1685 | if (suspect->commit->object.flags & UNINTERESTING) { | |
e68989a7 JH |
1686 | if (blank_boundary) |
1687 | memset(hex, ' ', length); | |
7ceacdff | 1688 | else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) { |
4c10a5ca JH |
1689 | length--; |
1690 | putchar('^'); | |
1691 | } | |
b11121d9 | 1692 | } |
cee7f245 | 1693 | |
b11121d9 | 1694 | printf("%.*s", length, hex); |
cee7f245 JH |
1695 | if (opt & OUTPUT_ANNOTATE_COMPAT) |
1696 | printf("\t(%10s\t%10s\t%d)", ci.author, | |
1697 | format_time(ci.author_time, ci.author_tz, | |
1698 | show_raw_time), | |
1699 | ent->lno + 1 + cnt); | |
1700 | else { | |
5ff62c30 | 1701 | if (opt & OUTPUT_SHOW_SCORE) |
54a4c617 JH |
1702 | printf(" %*d %02d", |
1703 | max_score_digits, ent->score, | |
1704 | ent->suspect->refcnt); | |
cee7f245 JH |
1705 | if (opt & OUTPUT_SHOW_NAME) |
1706 | printf(" %-*.*s", longest_file, longest_file, | |
1707 | suspect->path); | |
1708 | if (opt & OUTPUT_SHOW_NUMBER) | |
1709 | printf(" %*d", max_orig_digits, | |
1710 | ent->s_lno + 1 + cnt); | |
093dc5be JH |
1711 | |
1712 | if (!(opt & OUTPUT_NO_AUTHOR)) | |
1713 | printf(" (%-*.*s %10s", | |
1714 | longest_author, longest_author, | |
1715 | ci.author, | |
1716 | format_time(ci.author_time, | |
1717 | ci.author_tz, | |
1718 | show_raw_time)); | |
1719 | printf(" %*d) ", | |
cee7f245 JH |
1720 | max_digits, ent->lno + 1 + cnt); |
1721 | } | |
1722 | do { | |
1723 | ch = *cp++; | |
1724 | putchar(ch); | |
1725 | } while (ch != '\n' && | |
1726 | cp < sb->final_buf + sb->final_buf_size); | |
1727 | } | |
1728 | } | |
1729 | ||
1730 | static void output(struct scoreboard *sb, int option) | |
1731 | { | |
1732 | struct blame_entry *ent; | |
1733 | ||
1734 | if (option & OUTPUT_PORCELAIN) { | |
1735 | for (ent = sb->ent; ent; ent = ent->next) { | |
1736 | struct blame_entry *oth; | |
1737 | struct origin *suspect = ent->suspect; | |
1738 | struct commit *commit = suspect->commit; | |
1739 | if (commit->object.flags & MORE_THAN_ONE_PATH) | |
1740 | continue; | |
1741 | for (oth = ent->next; oth; oth = oth->next) { | |
1742 | if ((oth->suspect->commit != commit) || | |
1743 | !strcmp(oth->suspect->path, suspect->path)) | |
1744 | continue; | |
1745 | commit->object.flags |= MORE_THAN_ONE_PATH; | |
1746 | break; | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | ||
1751 | for (ent = sb->ent; ent; ent = ent->next) { | |
1752 | if (option & OUTPUT_PORCELAIN) | |
1753 | emit_porcelain(sb, ent); | |
5ff62c30 | 1754 | else { |
cee7f245 | 1755 | emit_other(sb, ent, option); |
5ff62c30 | 1756 | } |
cee7f245 JH |
1757 | } |
1758 | } | |
1759 | ||
1732a1fd JH |
1760 | /* |
1761 | * To allow quick access to the contents of nth line in the | |
1762 | * final image, prepare an index in the scoreboard. | |
1763 | */ | |
cee7f245 JH |
1764 | static int prepare_lines(struct scoreboard *sb) |
1765 | { | |
1766 | const char *buf = sb->final_buf; | |
1767 | unsigned long len = sb->final_buf_size; | |
1768 | int num = 0, incomplete = 0, bol = 1; | |
1769 | ||
1770 | if (len && buf[len-1] != '\n') | |
1771 | incomplete++; /* incomplete line at the end */ | |
1772 | while (len--) { | |
1773 | if (bol) { | |
1774 | sb->lineno = xrealloc(sb->lineno, | |
1775 | sizeof(int* ) * (num + 1)); | |
1776 | sb->lineno[num] = buf - sb->final_buf; | |
1777 | bol = 0; | |
1778 | } | |
1779 | if (*buf++ == '\n') { | |
1780 | num++; | |
1781 | bol = 1; | |
1782 | } | |
1783 | } | |
1ca6ca87 JH |
1784 | sb->lineno = xrealloc(sb->lineno, |
1785 | sizeof(int* ) * (num + incomplete + 1)); | |
1786 | sb->lineno[num + incomplete] = buf - sb->final_buf; | |
cee7f245 JH |
1787 | sb->num_lines = num + incomplete; |
1788 | return sb->num_lines; | |
1789 | } | |
1790 | ||
1732a1fd JH |
1791 | /* |
1792 | * Add phony grafts for use with -S; this is primarily to | |
34baebce | 1793 | * support git's cvsserver that wants to give a linear history |
1732a1fd JH |
1794 | * to its clients. |
1795 | */ | |
cee7f245 JH |
1796 | static int read_ancestry(const char *graft_file) |
1797 | { | |
1798 | FILE *fp = fopen(graft_file, "r"); | |
1799 | char buf[1024]; | |
1800 | if (!fp) | |
1801 | return -1; | |
1802 | while (fgets(buf, sizeof(buf), fp)) { | |
1803 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
1804 | int len = strlen(buf); | |
1805 | struct commit_graft *graft = read_graft_line(buf, len); | |
8eaf7986 JH |
1806 | if (graft) |
1807 | register_commit_graft(graft, 0); | |
cee7f245 JH |
1808 | } |
1809 | fclose(fp); | |
1810 | return 0; | |
1811 | } | |
1812 | ||
1732a1fd JH |
1813 | /* |
1814 | * How many columns do we need to show line numbers in decimal? | |
1815 | */ | |
cee7f245 JH |
1816 | static int lineno_width(int lines) |
1817 | { | |
f2915045 | 1818 | int i, width; |
cee7f245 | 1819 | |
f2915045 JH |
1820 | for (width = 1, i = 10; i <= lines + 1; width++) |
1821 | i *= 10; | |
1822 | return width; | |
cee7f245 JH |
1823 | } |
1824 | ||
1732a1fd JH |
1825 | /* |
1826 | * How many columns do we need to show line numbers, authors, | |
1827 | * and filenames? | |
1828 | */ | |
cee7f245 JH |
1829 | static void find_alignment(struct scoreboard *sb, int *option) |
1830 | { | |
1831 | int longest_src_lines = 0; | |
1832 | int longest_dst_lines = 0; | |
5ff62c30 | 1833 | unsigned largest_score = 0; |
cee7f245 JH |
1834 | struct blame_entry *e; |
1835 | ||
1836 | for (e = sb->ent; e; e = e->next) { | |
1837 | struct origin *suspect = e->suspect; | |
1838 | struct commit_info ci; | |
1839 | int num; | |
1840 | ||
ab3bb800 JH |
1841 | if (strcmp(suspect->path, sb->path)) |
1842 | *option |= OUTPUT_SHOW_NAME; | |
1843 | num = strlen(suspect->path); | |
1844 | if (longest_file < num) | |
1845 | longest_file = num; | |
cee7f245 JH |
1846 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { |
1847 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
1848 | get_commit_info(suspect->commit, &ci, 1); | |
cee7f245 JH |
1849 | num = strlen(ci.author); |
1850 | if (longest_author < num) | |
1851 | longest_author = num; | |
1852 | } | |
1853 | num = e->s_lno + e->num_lines; | |
1854 | if (longest_src_lines < num) | |
1855 | longest_src_lines = num; | |
1856 | num = e->lno + e->num_lines; | |
1857 | if (longest_dst_lines < num) | |
1858 | longest_dst_lines = num; | |
5ff62c30 JH |
1859 | if (largest_score < ent_score(sb, e)) |
1860 | largest_score = ent_score(sb, e); | |
cee7f245 JH |
1861 | } |
1862 | max_orig_digits = lineno_width(longest_src_lines); | |
1863 | max_digits = lineno_width(longest_dst_lines); | |
5ff62c30 | 1864 | max_score_digits = lineno_width(largest_score); |
cee7f245 JH |
1865 | } |
1866 | ||
1732a1fd JH |
1867 | /* |
1868 | * For debugging -- origin is refcounted, and this asserts that | |
1869 | * we do not underflow. | |
1870 | */ | |
54a4c617 JH |
1871 | static void sanity_check_refcnt(struct scoreboard *sb) |
1872 | { | |
1873 | int baa = 0; | |
1874 | struct blame_entry *ent; | |
1875 | ||
1876 | for (ent = sb->ent; ent; ent = ent->next) { | |
ae86ad65 | 1877 | /* Nobody should have zero or negative refcnt */ |
854b97f6 JH |
1878 | if (ent->suspect->refcnt <= 0) { |
1879 | fprintf(stderr, "%s in %s has negative refcnt %d\n", | |
1880 | ent->suspect->path, | |
1881 | sha1_to_hex(ent->suspect->commit->object.sha1), | |
1882 | ent->suspect->refcnt); | |
ae86ad65 | 1883 | baa = 1; |
854b97f6 | 1884 | } |
ae86ad65 JH |
1885 | } |
1886 | for (ent = sb->ent; ent; ent = ent->next) { | |
1887 | /* Mark the ones that haven't been checked */ | |
54a4c617 JH |
1888 | if (0 < ent->suspect->refcnt) |
1889 | ent->suspect->refcnt = -ent->suspect->refcnt; | |
54a4c617 JH |
1890 | } |
1891 | for (ent = sb->ent; ent; ent = ent->next) { | |
1732a1fd JH |
1892 | /* |
1893 | * ... then pick each and see if they have the the | |
1894 | * correct refcnt. | |
54a4c617 JH |
1895 | */ |
1896 | int found; | |
1897 | struct blame_entry *e; | |
1898 | struct origin *suspect = ent->suspect; | |
1899 | ||
1900 | if (0 < suspect->refcnt) | |
1901 | continue; | |
ae86ad65 | 1902 | suspect->refcnt = -suspect->refcnt; /* Unmark */ |
54a4c617 JH |
1903 | for (found = 0, e = sb->ent; e; e = e->next) { |
1904 | if (e->suspect != suspect) | |
1905 | continue; | |
1906 | found++; | |
1907 | } | |
854b97f6 JH |
1908 | if (suspect->refcnt != found) { |
1909 | fprintf(stderr, "%s in %s has refcnt %d, not %d\n", | |
1910 | ent->suspect->path, | |
1911 | sha1_to_hex(ent->suspect->commit->object.sha1), | |
1912 | ent->suspect->refcnt, found); | |
1913 | baa = 2; | |
1914 | } | |
54a4c617 JH |
1915 | } |
1916 | if (baa) { | |
1917 | int opt = 0160; | |
1918 | find_alignment(sb, &opt); | |
1919 | output(sb, opt); | |
854b97f6 | 1920 | die("Baa %d!", baa); |
54a4c617 JH |
1921 | } |
1922 | } | |
1923 | ||
1732a1fd JH |
1924 | /* |
1925 | * Used for the command line parsing; check if the path exists | |
1926 | * in the working tree. | |
1927 | */ | |
c455c87c | 1928 | static int has_string_in_work_tree(const char *path) |
cee7f245 JH |
1929 | { |
1930 | struct stat st; | |
1931 | return !lstat(path, &st); | |
1932 | } | |
1933 | ||
4a0fc95f JH |
1934 | static unsigned parse_score(const char *arg) |
1935 | { | |
1936 | char *end; | |
1937 | unsigned long score = strtoul(arg, &end, 10); | |
1938 | if (*end) | |
1939 | return 0; | |
1940 | return score; | |
1941 | } | |
1942 | ||
20239bae JK |
1943 | static const char *add_prefix(const char *prefix, const char *path) |
1944 | { | |
097971f5 | 1945 | return prefix_path(prefix, prefix ? strlen(prefix) : 0, path); |
20239bae JK |
1946 | } |
1947 | ||
1732a1fd JH |
1948 | /* |
1949 | * Parsing of (comma separated) one item in the -L option | |
1950 | */ | |
931233bc JH |
1951 | static const char *parse_loc(const char *spec, |
1952 | struct scoreboard *sb, long lno, | |
1953 | long begin, long *ret) | |
1954 | { | |
1955 | char *term; | |
1956 | const char *line; | |
1957 | long num; | |
1958 | int reg_error; | |
1959 | regex_t regexp; | |
1960 | regmatch_t match[1]; | |
1961 | ||
7bd9641d JH |
1962 | /* Allow "-L <something>,+20" to mean starting at <something> |
1963 | * for 20 lines, or "-L <something>,-5" for 5 lines ending at | |
1964 | * <something>. | |
1965 | */ | |
1966 | if (1 < begin && (spec[0] == '+' || spec[0] == '-')) { | |
1967 | num = strtol(spec + 1, &term, 10); | |
1968 | if (term != spec + 1) { | |
1969 | if (spec[0] == '-') | |
1970 | num = 0 - num; | |
1971 | if (0 < num) | |
1972 | *ret = begin + num - 2; | |
1973 | else if (!num) | |
1974 | *ret = begin; | |
1975 | else | |
1976 | *ret = begin + num; | |
1977 | return term; | |
1978 | } | |
1979 | return spec; | |
1980 | } | |
931233bc JH |
1981 | num = strtol(spec, &term, 10); |
1982 | if (term != spec) { | |
1983 | *ret = num; | |
1984 | return term; | |
1985 | } | |
1986 | if (spec[0] != '/') | |
1987 | return spec; | |
1988 | ||
1989 | /* it could be a regexp of form /.../ */ | |
1990 | for (term = (char*) spec + 1; *term && *term != '/'; term++) { | |
1991 | if (*term == '\\') | |
1992 | term++; | |
1993 | } | |
1994 | if (*term != '/') | |
1995 | return spec; | |
1996 | ||
1997 | /* try [spec+1 .. term-1] as regexp */ | |
1998 | *term = 0; | |
1999 | begin--; /* input is in human terms */ | |
2000 | line = nth_line(sb, begin); | |
2001 | ||
2002 | if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) && | |
2003 | !(reg_error = regexec(®exp, line, 1, match, 0))) { | |
2004 | const char *cp = line + match[0].rm_so; | |
2005 | const char *nline; | |
2006 | ||
2007 | while (begin++ < lno) { | |
2008 | nline = nth_line(sb, begin); | |
2009 | if (line <= cp && cp < nline) | |
2010 | break; | |
2011 | line = nline; | |
2012 | } | |
2013 | *ret = begin; | |
2014 | regfree(®exp); | |
2015 | *term++ = '/'; | |
2016 | return term; | |
2017 | } | |
2018 | else { | |
2019 | char errbuf[1024]; | |
2020 | regerror(reg_error, ®exp, errbuf, 1024); | |
2021 | die("-L parameter '%s': %s", spec + 1, errbuf); | |
2022 | } | |
2023 | } | |
2024 | ||
1732a1fd JH |
2025 | /* |
2026 | * Parsing of -L option | |
2027 | */ | |
931233bc JH |
2028 | static void prepare_blame_range(struct scoreboard *sb, |
2029 | const char *bottomtop, | |
2030 | long lno, | |
2031 | long *bottom, long *top) | |
2032 | { | |
2033 | const char *term; | |
2034 | ||
2035 | term = parse_loc(bottomtop, sb, lno, 1, bottom); | |
2036 | if (*term == ',') { | |
2037 | term = parse_loc(term + 1, sb, lno, *bottom + 1, top); | |
2038 | if (*term) | |
acca687f | 2039 | usage(blame_usage); |
931233bc JH |
2040 | } |
2041 | if (*term) | |
acca687f | 2042 | usage(blame_usage); |
931233bc JH |
2043 | } |
2044 | ||
ef90d6d4 | 2045 | static int git_blame_config(const char *var, const char *value, void *cb) |
4c10a5ca JH |
2046 | { |
2047 | if (!strcmp(var, "blame.showroot")) { | |
2048 | show_root = git_config_bool(var, value); | |
2049 | return 0; | |
2050 | } | |
2051 | if (!strcmp(var, "blame.blankboundary")) { | |
2052 | blank_boundary = git_config_bool(var, value); | |
2053 | return 0; | |
2054 | } | |
ef90d6d4 | 2055 | return git_default_config(var, value, cb); |
4c10a5ca JH |
2056 | } |
2057 | ||
f6c07d7d JH |
2058 | /* |
2059 | * Prepare a dummy commit that represents the work tree (or staged) item. | |
2060 | * Note that annotating work tree item never works in the reverse. | |
2061 | */ | |
1cfe7733 JH |
2062 | static struct commit *fake_working_tree_commit(const char *path, const char *contents_from) |
2063 | { | |
2064 | struct commit *commit; | |
2065 | struct origin *origin; | |
2066 | unsigned char head_sha1[20]; | |
af6eb822 | 2067 | struct strbuf buf; |
1cfe7733 | 2068 | const char *ident; |
1cfe7733 | 2069 | time_t now; |
1cfe7733 JH |
2070 | int size, len; |
2071 | struct cache_entry *ce; | |
2072 | unsigned mode; | |
2073 | ||
2074 | if (get_sha1("HEAD", head_sha1)) | |
2075 | die("No such ref: HEAD"); | |
2076 | ||
2077 | time(&now); | |
2078 | commit = xcalloc(1, sizeof(*commit)); | |
2079 | commit->parents = xcalloc(1, sizeof(*commit->parents)); | |
2080 | commit->parents->item = lookup_commit_reference(head_sha1); | |
2081 | commit->object.parsed = 1; | |
2082 | commit->date = now; | |
2083 | commit->object.type = OBJ_COMMIT; | |
2084 | ||
2085 | origin = make_origin(commit, path); | |
2086 | ||
f1696ee3 | 2087 | strbuf_init(&buf, 0); |
1cfe7733 JH |
2088 | if (!contents_from || strcmp("-", contents_from)) { |
2089 | struct stat st; | |
2090 | const char *read_from; | |
af6eb822 | 2091 | unsigned long fin_size; |
1cfe7733 JH |
2092 | |
2093 | if (contents_from) { | |
2094 | if (stat(contents_from, &st) < 0) | |
2095 | die("Cannot stat %s", contents_from); | |
2096 | read_from = contents_from; | |
2097 | } | |
2098 | else { | |
2099 | if (lstat(path, &st) < 0) | |
2100 | die("Cannot lstat %s", path); | |
2101 | read_from = path; | |
2102 | } | |
dc49cd76 | 2103 | fin_size = xsize_t(st.st_size); |
1cfe7733 JH |
2104 | mode = canon_mode(st.st_mode); |
2105 | switch (st.st_mode & S_IFMT) { | |
2106 | case S_IFREG: | |
387e7e19 PH |
2107 | if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) |
2108 | die("cannot open or read %s", read_from); | |
1cfe7733 JH |
2109 | break; |
2110 | case S_IFLNK: | |
af6eb822 | 2111 | if (readlink(read_from, buf.buf, buf.alloc) != fin_size) |
1cfe7733 | 2112 | die("cannot readlink %s", read_from); |
af6eb822 | 2113 | buf.len = fin_size; |
1cfe7733 JH |
2114 | break; |
2115 | default: | |
2116 | die("unsupported file type %s", read_from); | |
2117 | } | |
2118 | } | |
2119 | else { | |
2120 | /* Reading from stdin */ | |
2121 | contents_from = "standard input"; | |
1cfe7733 | 2122 | mode = 0; |
f1696ee3 | 2123 | if (strbuf_read(&buf, 0, 0) < 0) |
af6eb822 | 2124 | die("read error %s from stdin", strerror(errno)); |
1cfe7733 | 2125 | } |
21e5ad50 | 2126 | convert_to_git(path, buf.buf, buf.len, &buf, 0); |
af6eb822 PH |
2127 | origin->file.ptr = buf.buf; |
2128 | origin->file.size = buf.len; | |
2129 | pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1); | |
1cfe7733 JH |
2130 | commit->util = origin; |
2131 | ||
2132 | /* | |
2133 | * Read the current index, replace the path entry with | |
2134 | * origin->blob_sha1 without mucking with its mode or type | |
2135 | * bits; we are not going to write this index out -- we just | |
2136 | * want to run "diff-index --cached". | |
2137 | */ | |
2138 | discard_cache(); | |
2139 | read_cache(); | |
2140 | ||
2141 | len = strlen(path); | |
2142 | if (!mode) { | |
2143 | int pos = cache_name_pos(path, len); | |
2144 | if (0 <= pos) | |
7a51ed66 | 2145 | mode = active_cache[pos]->ce_mode; |
1cfe7733 JH |
2146 | else |
2147 | /* Let's not bother reading from HEAD tree */ | |
2148 | mode = S_IFREG | 0644; | |
2149 | } | |
2150 | size = cache_entry_size(len); | |
2151 | ce = xcalloc(1, size); | |
2152 | hashcpy(ce->sha1, origin->blob_sha1); | |
2153 | memcpy(ce->name, path, len); | |
2154 | ce->ce_flags = create_ce_flags(len, 0); | |
2155 | ce->ce_mode = create_ce_mode(mode); | |
2156 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
2157 | ||
2158 | /* | |
2159 | * We are not going to write this out, so this does not matter | |
2160 | * right now, but someday we might optimize diff-index --cached | |
2161 | * with cache-tree information. | |
2162 | */ | |
2163 | cache_tree_invalidate_path(active_cache_tree, path); | |
2164 | ||
2165 | commit->buffer = xmalloc(400); | |
2166 | ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); | |
1bb88be9 | 2167 | snprintf(commit->buffer, 400, |
1cfe7733 JH |
2168 | "tree 0000000000000000000000000000000000000000\n" |
2169 | "parent %s\n" | |
2170 | "author %s\n" | |
2171 | "committer %s\n\n" | |
2172 | "Version of %s from %s\n", | |
2173 | sha1_to_hex(head_sha1), | |
2174 | ident, ident, path, contents_from ? contents_from : path); | |
2175 | return commit; | |
2176 | } | |
2177 | ||
85af7929 | 2178 | static const char *prepare_final(struct scoreboard *sb) |
f6c07d7d JH |
2179 | { |
2180 | int i; | |
2181 | const char *final_commit_name = NULL; | |
85af7929 | 2182 | struct rev_info *revs = sb->revs; |
f6c07d7d JH |
2183 | |
2184 | /* | |
2185 | * There must be one and only one positive commit in the | |
2186 | * revs->pending array. | |
2187 | */ | |
2188 | for (i = 0; i < revs->pending.nr; i++) { | |
2189 | struct object *obj = revs->pending.objects[i].item; | |
2190 | if (obj->flags & UNINTERESTING) | |
2191 | continue; | |
2192 | while (obj->type == OBJ_TAG) | |
2193 | obj = deref_tag(obj, NULL, 0); | |
2194 | if (obj->type != OBJ_COMMIT) | |
2195 | die("Non commit %s?", revs->pending.objects[i].name); | |
2196 | if (sb->final) | |
2197 | die("More than one commit to dig from %s and %s?", | |
2198 | revs->pending.objects[i].name, | |
2199 | final_commit_name); | |
2200 | sb->final = (struct commit *) obj; | |
2201 | final_commit_name = revs->pending.objects[i].name; | |
2202 | } | |
2203 | return final_commit_name; | |
2204 | } | |
2205 | ||
85af7929 JH |
2206 | static const char *prepare_initial(struct scoreboard *sb) |
2207 | { | |
2208 | int i; | |
2209 | const char *final_commit_name = NULL; | |
2210 | struct rev_info *revs = sb->revs; | |
2211 | ||
2212 | /* | |
2213 | * There must be one and only one negative commit, and it must be | |
2214 | * the boundary. | |
2215 | */ | |
2216 | for (i = 0; i < revs->pending.nr; i++) { | |
2217 | struct object *obj = revs->pending.objects[i].item; | |
2218 | if (!(obj->flags & UNINTERESTING)) | |
2219 | continue; | |
2220 | while (obj->type == OBJ_TAG) | |
2221 | obj = deref_tag(obj, NULL, 0); | |
2222 | if (obj->type != OBJ_COMMIT) | |
2223 | die("Non commit %s?", revs->pending.objects[i].name); | |
2224 | if (sb->final) | |
2225 | die("More than one commit to dig down to %s and %s?", | |
2226 | revs->pending.objects[i].name, | |
2227 | final_commit_name); | |
2228 | sb->final = (struct commit *) obj; | |
2229 | final_commit_name = revs->pending.objects[i].name; | |
2230 | } | |
2231 | if (!final_commit_name) | |
2232 | die("No commit to dig down to?"); | |
2233 | return final_commit_name; | |
2234 | } | |
2235 | ||
5817da01 PH |
2236 | static int blame_copy_callback(const struct option *option, const char *arg, int unset) |
2237 | { | |
2238 | int *opt = option->value; | |
2239 | ||
2240 | /* | |
2241 | * -C enables copy from removed files; | |
2242 | * -C -C enables copy from existing files, but only | |
2243 | * when blaming a new file; | |
2244 | * -C -C -C enables copy from existing files for | |
2245 | * everybody | |
2246 | */ | |
2247 | if (*opt & PICKAXE_BLAME_COPY_HARDER) | |
2248 | *opt |= PICKAXE_BLAME_COPY_HARDEST; | |
2249 | if (*opt & PICKAXE_BLAME_COPY) | |
2250 | *opt |= PICKAXE_BLAME_COPY_HARDER; | |
2251 | *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE; | |
2252 | ||
2253 | if (arg) | |
2254 | blame_copy_score = parse_score(arg); | |
2255 | return 0; | |
2256 | } | |
2257 | ||
2258 | static int blame_move_callback(const struct option *option, const char *arg, int unset) | |
2259 | { | |
2260 | int *opt = option->value; | |
2261 | ||
2262 | *opt |= PICKAXE_BLAME_MOVE; | |
2263 | ||
2264 | if (arg) | |
2265 | blame_move_score = parse_score(arg); | |
2266 | return 0; | |
2267 | } | |
2268 | ||
2269 | static int blame_bottomtop_callback(const struct option *option, const char *arg, int unset) | |
2270 | { | |
2271 | const char **bottomtop = option->value; | |
2272 | if (!arg) | |
2273 | return -1; | |
2274 | if (*bottomtop) | |
2275 | die("More than one '-L n,m' option given"); | |
2276 | *bottomtop = arg; | |
2277 | return 0; | |
2278 | } | |
2279 | ||
acca687f | 2280 | int cmd_blame(int argc, const char **argv, const char *prefix) |
cee7f245 JH |
2281 | { |
2282 | struct rev_info revs; | |
2283 | const char *path; | |
2284 | struct scoreboard sb; | |
2285 | struct origin *o; | |
2286 | struct blame_entry *ent; | |
3f8d5204 | 2287 | long dashdash_pos, bottom, top, lno; |
cee7f245 | 2288 | const char *final_commit_name = NULL; |
21666f1a | 2289 | enum object_type type; |
5817da01 PH |
2290 | |
2291 | static const char *bottomtop = NULL; | |
2292 | static int output_option = 0, opt = 0; | |
2293 | static int show_stats = 0; | |
2294 | static const char *revs_file = NULL; | |
2295 | static const char *contents_from = NULL; | |
2296 | static const struct option options[] = { | |
2297 | OPT_BOOLEAN(0, "incremental", &incremental, "Show blame entries as we find them, incrementally"), | |
2298 | OPT_BOOLEAN('b', NULL, &blank_boundary, "Show blank SHA-1 for boundary commits (Default: off)"), | |
2299 | OPT_BOOLEAN(0, "root", &show_root, "Do not treat root commits as boundaries (Default: off)"), | |
2300 | OPT_BOOLEAN(0, "show-stats", &show_stats, "Show work cost statistics"), | |
2301 | OPT_BIT(0, "score-debug", &output_option, "Show output score for blame entries", OUTPUT_SHOW_SCORE), | |
2302 | OPT_BIT('f', "show-name", &output_option, "Show original filename (Default: auto)", OUTPUT_SHOW_NAME), | |
2303 | OPT_BIT('n', "show-number", &output_option, "Show original linenumber (Default: off)", OUTPUT_SHOW_NUMBER), | |
2304 | OPT_BIT('p', "porcelain", &output_option, "Show in a format designed for machine consumption", OUTPUT_PORCELAIN), | |
2305 | OPT_BIT('c', NULL, &output_option, "Use the same output mode as git-annotate (Default: off)", OUTPUT_ANNOTATE_COMPAT), | |
2306 | OPT_BIT('t', NULL, &output_option, "Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP), | |
2307 | OPT_BIT('l', NULL, &output_option, "Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME), | |
2308 | OPT_BIT('s', NULL, &output_option, "Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR), | |
2309 | OPT_BIT('w', NULL, &xdl_opts, "Ignore whitespace differences", XDF_IGNORE_WHITESPACE), | |
2310 | OPT_STRING('S', NULL, &revs_file, "file", "Use revisions from <file> instead of calling git-rev-list"), | |
2311 | OPT_STRING(0, "contents", &contents_from, "file", "Use <file>'s contents as the final image"), | |
2312 | { OPTION_CALLBACK, 'C', NULL, &opt, "score", "Find line copies within and across files", PARSE_OPT_OPTARG, blame_copy_callback }, | |
2313 | { OPTION_CALLBACK, 'M', NULL, &opt, "score", "Find line movements within and across files", PARSE_OPT_OPTARG, blame_move_callback }, | |
2314 | OPT_CALLBACK('L', NULL, &bottomtop, "n,m", "Process only line range n,m, counting from 1", blame_bottomtop_callback), | |
2315 | OPT_END() | |
2316 | }; | |
2317 | ||
2318 | struct parse_opt_ctx_t ctx; | |
7ceacdff | 2319 | int cmd_is_annotate = !strcmp(argv[0], "annotate"); |
e68989a7 | 2320 | |
ef90d6d4 | 2321 | git_config(git_blame_config, NULL); |
5817da01 | 2322 | init_revisions(&revs, NULL); |
612702e8 | 2323 | save_commit_buffer = 0; |
3f8d5204 | 2324 | dashdash_pos = 0; |
612702e8 | 2325 | |
5817da01 PH |
2326 | parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH | |
2327 | PARSE_OPT_KEEP_ARGV0); | |
2328 | for (;;) { | |
5817da01 PH |
2329 | switch (parse_options_step(&ctx, options, blame_opt_usage)) { |
2330 | case PARSE_OPT_HELP: | |
2331 | exit(129); | |
2332 | case PARSE_OPT_DONE: | |
3f8d5204 PH |
2333 | if (ctx.argv[0]) |
2334 | dashdash_pos = ctx.cpidx; | |
5817da01 PH |
2335 | goto parse_done; |
2336 | } | |
2337 | ||
2338 | if (!strcmp(ctx.argv[0], "--reverse")) { | |
2339 | ctx.argv[0] = "--children"; | |
2340 | reverse = 1; | |
2341 | } | |
6b61ec05 | 2342 | parse_revision_opt(&revs, &ctx, options, blame_opt_usage); |
5817da01 PH |
2343 | } |
2344 | parse_done: | |
2345 | argc = parse_options_end(&ctx); | |
2346 | ||
7ceacdff JH |
2347 | if (cmd_is_annotate) |
2348 | output_option |= OUTPUT_ANNOTATE_COMPAT; | |
2349 | ||
b3123f98 JH |
2350 | if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER)) |
2351 | opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE | | |
2352 | PICKAXE_BLAME_COPY_HARDER); | |
2353 | ||
4a0fc95f JH |
2354 | if (!blame_move_score) |
2355 | blame_move_score = BLAME_DEFAULT_MOVE_SCORE; | |
2356 | if (!blame_copy_score) | |
2357 | blame_copy_score = BLAME_DEFAULT_COPY_SCORE; | |
2358 | ||
1732a1fd JH |
2359 | /* |
2360 | * We have collected options unknown to us in argv[1..unk] | |
cee7f245 | 2361 | * which are to be passed to revision machinery if we are |
3dff5379 | 2362 | * going to do the "bottom" processing. |
cee7f245 JH |
2363 | * |
2364 | * The remaining are: | |
2365 | * | |
3f8d5204 PH |
2366 | * (1) if dashdash_pos != 0, its either |
2367 | * "blame [revisions] -- <path>" or | |
2368 | * "blame -- <path> <rev>" | |
cee7f245 | 2369 | * |
3f8d5204 PH |
2370 | * (2) otherwise, its one of the two: |
2371 | * "blame [revisions] <path>" | |
2372 | * "blame <path> <rev>" | |
cee7f245 | 2373 | * |
3f8d5204 PH |
2374 | * Note that we must strip out <path> from the arguments: we do not |
2375 | * want the path pruning but we may want "bottom" processing. | |
cee7f245 | 2376 | */ |
3f8d5204 PH |
2377 | if (dashdash_pos) { |
2378 | switch (argc - dashdash_pos - 1) { | |
2379 | case 2: /* (1b) */ | |
2380 | if (argc != 4) | |
5817da01 | 2381 | usage_with_options(blame_opt_usage, options); |
3f8d5204 PH |
2382 | /* reorder for the new way: <rev> -- <path> */ |
2383 | argv[1] = argv[3]; | |
2384 | argv[3] = argv[2]; | |
2385 | argv[2] = "--"; | |
2386 | /* FALLTHROUGH */ | |
2387 | case 1: /* (1a) */ | |
2388 | path = add_prefix(prefix, argv[--argc]); | |
2389 | argv[argc] = NULL; | |
2390 | break; | |
2391 | default: | |
2392 | usage_with_options(blame_opt_usage, options); | |
cee7f245 | 2393 | } |
3f8d5204 PH |
2394 | } else { |
2395 | if (argc < 2) | |
5817da01 | 2396 | usage_with_options(blame_opt_usage, options); |
3f8d5204 | 2397 | path = add_prefix(prefix, argv[argc - 1]); |
c455c87c | 2398 | if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */ |
3f8d5204 PH |
2399 | path = add_prefix(prefix, argv[1]); |
2400 | argv[1] = argv[2]; | |
cee7f245 | 2401 | } |
3f8d5204 | 2402 | argv[argc - 1] = "--"; |
cee7f245 | 2403 | |
3f8d5204 | 2404 | setup_work_tree(); |
c455c87c | 2405 | if (!has_string_in_work_tree(path)) |
3f8d5204 | 2406 | die("cannot stat path %s: %s", path, strerror(errno)); |
cee7f245 JH |
2407 | } |
2408 | ||
3f8d5204 | 2409 | setup_revisions(argc, argv, &revs, NULL); |
cee7f245 JH |
2410 | memset(&sb, 0, sizeof(sb)); |
2411 | ||
85af7929 JH |
2412 | sb.revs = &revs; |
2413 | if (!reverse) | |
2414 | final_commit_name = prepare_final(&sb); | |
2415 | else if (contents_from) | |
2416 | die("--contents and --children do not blend well."); | |
2417 | else | |
2418 | final_commit_name = prepare_initial(&sb); | |
cee7f245 JH |
2419 | |
2420 | if (!sb.final) { | |
1732a1fd JH |
2421 | /* |
2422 | * "--not A B -- path" without anything positive; | |
1cfe7733 JH |
2423 | * do not default to HEAD, but use the working tree |
2424 | * or "--contents". | |
1732a1fd | 2425 | */ |
1981820b | 2426 | setup_work_tree(); |
1cfe7733 JH |
2427 | sb.final = fake_working_tree_commit(path, contents_from); |
2428 | add_pending_object(&revs, &(sb.final->object), ":"); | |
cee7f245 | 2429 | } |
1cfe7733 JH |
2430 | else if (contents_from) |
2431 | die("Cannot use --contents with final commit object name"); | |
cee7f245 | 2432 | |
1732a1fd JH |
2433 | /* |
2434 | * If we have bottom, this will mark the ancestors of the | |
cee7f245 JH |
2435 | * bottom commits we would reach while traversing as |
2436 | * uninteresting. | |
2437 | */ | |
3d51e1b5 MK |
2438 | if (prepare_revision_walk(&revs)) |
2439 | die("revision walk setup failed"); | |
cee7f245 | 2440 | |
1cfe7733 JH |
2441 | if (is_null_sha1(sb.final->object.sha1)) { |
2442 | char *buf; | |
2443 | o = sb.final->util; | |
2444 | buf = xmalloc(o->file.size + 1); | |
2445 | memcpy(buf, o->file.ptr, o->file.size + 1); | |
2446 | sb.final_buf = buf; | |
2447 | sb.final_buf_size = o->file.size; | |
2448 | } | |
2449 | else { | |
2450 | o = get_origin(&sb, sb.final, path); | |
2451 | if (fill_blob_sha1(o)) | |
2452 | die("no such path %s in %s", path, final_commit_name); | |
cee7f245 | 2453 | |
21666f1a | 2454 | sb.final_buf = read_sha1_file(o->blob_sha1, &type, |
1cfe7733 | 2455 | &sb.final_buf_size); |
ab43e495 JH |
2456 | if (!sb.final_buf) |
2457 | die("Cannot read blob %s for path %s", | |
2458 | sha1_to_hex(o->blob_sha1), | |
2459 | path); | |
1cfe7733 | 2460 | } |
c2e525d9 | 2461 | num_read_blob++; |
cee7f245 JH |
2462 | lno = prepare_lines(&sb); |
2463 | ||
931233bc JH |
2464 | bottom = top = 0; |
2465 | if (bottomtop) | |
2466 | prepare_blame_range(&sb, bottomtop, lno, &bottom, &top); | |
2467 | if (bottom && top && top < bottom) { | |
2468 | long tmp; | |
2469 | tmp = top; top = bottom; bottom = tmp; | |
2470 | } | |
cee7f245 JH |
2471 | if (bottom < 1) |
2472 | bottom = 1; | |
2473 | if (top < 1) | |
2474 | top = lno; | |
2475 | bottom--; | |
2476 | if (lno < top) | |
2477 | die("file %s has only %lu lines", path, lno); | |
2478 | ||
2479 | ent = xcalloc(1, sizeof(*ent)); | |
2480 | ent->lno = bottom; | |
2481 | ent->num_lines = top - bottom; | |
2482 | ent->suspect = o; | |
2483 | ent->s_lno = bottom; | |
2484 | ||
2485 | sb.ent = ent; | |
2486 | sb.path = path; | |
2487 | ||
2488 | if (revs_file && read_ancestry(revs_file)) | |
2489 | die("reading graft file %s failed: %s", | |
2490 | revs_file, strerror(errno)); | |
2491 | ||
50acc589 | 2492 | read_mailmap(&mailmap, ".mailmap", NULL); |
f9567384 | 2493 | |
b92565dc MH |
2494 | if (!incremental) |
2495 | setup_pager(); | |
2496 | ||
85af7929 | 2497 | assign_blame(&sb, opt); |
cee7f245 | 2498 | |
717d1462 LT |
2499 | if (incremental) |
2500 | return 0; | |
2501 | ||
cee7f245 JH |
2502 | coalesce(&sb); |
2503 | ||
2504 | if (!(output_option & OUTPUT_PORCELAIN)) | |
2505 | find_alignment(&sb, &output_option); | |
2506 | ||
2507 | output(&sb, output_option); | |
2508 | free((void *)sb.final_buf); | |
2509 | for (ent = sb.ent; ent; ) { | |
2510 | struct blame_entry *e = ent->next; | |
2511 | free(ent); | |
2512 | ent = e; | |
2513 | } | |
c2e525d9 | 2514 | |
870b39c1 | 2515 | if (show_stats) { |
c2e525d9 JH |
2516 | printf("num read blob: %d\n", num_read_blob); |
2517 | printf("num get patch: %d\n", num_get_patch); | |
2518 | printf("num commits: %d\n", num_commits); | |
2519 | } | |
cee7f245 JH |
2520 | return 0; |
2521 | } |