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