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" | |
16 | #include "xdiff-interface.h" | |
17 | ||
18 | #include <time.h> | |
19 | #include <sys/time.h> | |
20 | ||
21 | static char pickaxe_usage[] = | |
18abd745 | 22 | "git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n" |
cee7f245 JH |
23 | " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n" |
24 | " -l, --long Show long commit SHA1 (Default: off)\n" | |
25 | " -t, --time Show raw timestamp (Default: off)\n" | |
26 | " -f, --show-name Show original filename (Default: auto)\n" | |
27 | " -n, --show-number Show original linenumber (Default: off)\n" | |
28 | " -p, --porcelain Show in a format designed for machine consumption\n" | |
29 | " -L n,m Process only line range n,m, counting from 1\n" | |
18abd745 | 30 | " -M, -C Find line movements within and across files\n" |
cee7f245 JH |
31 | " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n"; |
32 | ||
33 | static int longest_file; | |
34 | static int longest_author; | |
35 | static int max_orig_digits; | |
36 | static int max_digits; | |
37 | ||
38 | #define DEBUG 0 | |
39 | ||
d24bba80 | 40 | #define PICKAXE_BLAME_MOVE 01 |
18abd745 JH |
41 | #define PICKAXE_BLAME_COPY 02 |
42 | #define PICKAXE_BLAME_COPY_HARDER 04 | |
d24bba80 | 43 | |
cee7f245 JH |
44 | /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */ |
45 | #define METAINFO_SHOWN (1u<<12) | |
46 | #define MORE_THAN_ONE_PATH (1u<<13) | |
47 | ||
48 | /* | |
49 | * One blob in a commit | |
50 | */ | |
51 | struct origin { | |
52 | struct commit *commit; | |
53 | unsigned char blob_sha1[20]; | |
54 | char path[FLEX_ARRAY]; | |
55 | }; | |
56 | ||
57 | struct blame_entry { | |
58 | struct blame_entry *prev; | |
59 | struct blame_entry *next; | |
60 | ||
61 | /* the first line of this group in the final image; | |
62 | * internally all line numbers are 0 based. | |
63 | */ | |
64 | int lno; | |
65 | ||
66 | /* how many lines this group has */ | |
67 | int num_lines; | |
68 | ||
69 | /* the commit that introduced this group into the final image */ | |
70 | struct origin *suspect; | |
71 | ||
72 | /* true if the suspect is truly guilty; false while we have not | |
73 | * checked if the group came from one of its parents. | |
74 | */ | |
75 | char guilty; | |
76 | ||
77 | /* the line number of the first line of this group in the | |
78 | * suspect's file; internally all line numbers are 0 based. | |
79 | */ | |
80 | int s_lno; | |
81 | }; | |
82 | ||
83 | struct scoreboard { | |
84 | /* the final commit (i.e. where we started digging from) */ | |
85 | struct commit *final; | |
86 | ||
87 | const char *path; | |
88 | ||
89 | /* the contents in the final; pointed into by buf pointers of | |
90 | * blame_entries | |
91 | */ | |
92 | const char *final_buf; | |
93 | unsigned long final_buf_size; | |
94 | ||
95 | /* linked list of blames */ | |
96 | struct blame_entry *ent; | |
97 | ||
98 | int num_lines; | |
99 | int *lineno; | |
100 | }; | |
101 | ||
102 | static void coalesce(struct scoreboard *sb) | |
103 | { | |
104 | struct blame_entry *ent, *next; | |
105 | ||
106 | for (ent = sb->ent; ent && (next = ent->next); ent = next) { | |
107 | if (ent->suspect == next->suspect && | |
108 | ent->guilty == next->guilty && | |
109 | ent->s_lno + ent->num_lines == next->s_lno) { | |
110 | ent->num_lines += next->num_lines; | |
111 | ent->next = next->next; | |
112 | if (ent->next) | |
113 | ent->next->prev = ent; | |
114 | free(next); | |
115 | next = ent; /* again */ | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | static void free_origin(struct origin *o) | |
121 | { | |
122 | free(o); | |
123 | } | |
124 | ||
125 | static struct origin *find_origin(struct scoreboard *sb, | |
126 | struct commit *commit, | |
127 | const char *path) | |
128 | { | |
129 | struct blame_entry *ent; | |
130 | struct origin *o; | |
131 | unsigned mode; | |
132 | char type[10]; | |
133 | ||
134 | for (ent = sb->ent; ent; ent = ent->next) { | |
135 | if (ent->suspect->commit == commit && | |
136 | !strcmp(ent->suspect->path, path)) | |
137 | return ent->suspect; | |
138 | } | |
139 | ||
140 | o = xcalloc(1, sizeof(*o) + strlen(path) + 1); | |
141 | o->commit = commit; | |
142 | strcpy(o->path, path); | |
143 | if (get_tree_entry(commit->object.sha1, path, o->blob_sha1, &mode)) | |
144 | goto err_out; | |
145 | if (sha1_object_info(o->blob_sha1, type, NULL) || | |
146 | strcmp(type, blob_type)) | |
147 | goto err_out; | |
148 | return o; | |
149 | err_out: | |
150 | free_origin(o); | |
151 | return NULL; | |
152 | } | |
153 | ||
154 | static struct origin *find_rename(struct scoreboard *sb, | |
155 | struct commit *parent, | |
156 | struct origin *origin) | |
157 | { | |
158 | struct origin *porigin = NULL; | |
159 | struct diff_options diff_opts; | |
160 | int i; | |
161 | const char *paths[1]; | |
162 | ||
163 | diff_setup(&diff_opts); | |
164 | diff_opts.recursive = 1; | |
165 | diff_opts.detect_rename = DIFF_DETECT_RENAME; | |
166 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
167 | paths[0] = NULL; | |
168 | diff_tree_setup_paths(paths, &diff_opts); | |
169 | if (diff_setup_done(&diff_opts) < 0) | |
170 | die("diff-setup"); | |
171 | diff_tree_sha1(origin->commit->tree->object.sha1, | |
172 | parent->tree->object.sha1, | |
173 | "", &diff_opts); | |
174 | diffcore_std(&diff_opts); | |
175 | ||
176 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
177 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
178 | if (p->status == 'R' && !strcmp(p->one->path, origin->path)) { | |
179 | porigin = find_origin(sb, parent, p->two->path); | |
180 | break; | |
181 | } | |
182 | } | |
183 | diff_flush(&diff_opts); | |
184 | return porigin; | |
185 | } | |
186 | ||
187 | struct chunk { | |
188 | /* line number in postimage; up to but not including this | |
189 | * line is the same as preimage | |
190 | */ | |
191 | int same; | |
192 | ||
193 | /* preimage line number after this chunk */ | |
194 | int p_next; | |
195 | ||
196 | /* postimage line number after this chunk */ | |
197 | int t_next; | |
198 | }; | |
199 | ||
200 | struct patch { | |
201 | struct chunk *chunks; | |
202 | int num; | |
203 | }; | |
204 | ||
205 | struct blame_diff_state { | |
206 | struct xdiff_emit_state xm; | |
207 | struct patch *ret; | |
208 | unsigned hunk_post_context; | |
209 | unsigned hunk_in_pre_context : 1; | |
210 | }; | |
211 | ||
212 | static void process_u_diff(void *state_, char *line, unsigned long len) | |
213 | { | |
214 | struct blame_diff_state *state = state_; | |
215 | struct chunk *chunk; | |
216 | int off1, off2, len1, len2, num; | |
217 | ||
218 | if (DEBUG) | |
219 | fprintf(stderr, "%.*s", (int) len, line); | |
220 | ||
221 | num = state->ret->num; | |
222 | if (len < 4 || line[0] != '@' || line[1] != '@') { | |
223 | if (state->hunk_in_pre_context && line[0] == ' ') | |
224 | state->ret->chunks[num - 1].same++; | |
225 | else { | |
226 | state->hunk_in_pre_context = 0; | |
227 | if (line[0] == ' ') | |
228 | state->hunk_post_context++; | |
229 | else | |
230 | state->hunk_post_context = 0; | |
231 | } | |
232 | return; | |
233 | } | |
234 | ||
235 | if (num && state->hunk_post_context) { | |
236 | chunk = &state->ret->chunks[num - 1]; | |
237 | chunk->p_next -= state->hunk_post_context; | |
238 | chunk->t_next -= state->hunk_post_context; | |
239 | } | |
240 | state->ret->num = ++num; | |
241 | state->ret->chunks = xrealloc(state->ret->chunks, | |
242 | sizeof(struct chunk) * num); | |
243 | chunk = &state->ret->chunks[num - 1]; | |
244 | if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) { | |
245 | state->ret->num--; | |
246 | return; | |
247 | } | |
248 | ||
249 | /* Line numbers in patch output are one based. */ | |
250 | off1--; | |
251 | off2--; | |
252 | ||
253 | chunk->same = len2 ? off2 : (off2 + 1); | |
254 | ||
255 | chunk->p_next = off1 + (len1 ? len1 : 1); | |
256 | chunk->t_next = chunk->same + len2; | |
257 | state->hunk_in_pre_context = 1; | |
258 | state->hunk_post_context = 0; | |
259 | } | |
260 | ||
261 | static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o, | |
262 | int context) | |
263 | { | |
264 | struct blame_diff_state state; | |
265 | xpparam_t xpp; | |
266 | xdemitconf_t xecfg; | |
267 | xdemitcb_t ecb; | |
268 | ||
269 | xpp.flags = XDF_NEED_MINIMAL; | |
270 | xecfg.ctxlen = context; | |
271 | xecfg.flags = 0; | |
272 | ecb.outf = xdiff_outf; | |
273 | ecb.priv = &state; | |
274 | memset(&state, 0, sizeof(state)); | |
275 | state.xm.consume = process_u_diff; | |
276 | state.ret = xmalloc(sizeof(struct patch)); | |
277 | state.ret->chunks = NULL; | |
278 | state.ret->num = 0; | |
279 | ||
280 | xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb); | |
281 | ||
282 | if (state.ret->num) { | |
283 | struct chunk *chunk; | |
284 | chunk = &state.ret->chunks[state.ret->num - 1]; | |
285 | chunk->p_next -= state.hunk_post_context; | |
286 | chunk->t_next -= state.hunk_post_context; | |
287 | } | |
288 | return state.ret; | |
289 | } | |
290 | ||
291 | static struct patch *get_patch(struct origin *parent, struct origin *origin) | |
292 | { | |
293 | mmfile_t file_p, file_o; | |
294 | char type[10]; | |
295 | char *blob_p, *blob_o; | |
296 | struct patch *patch; | |
297 | ||
298 | if (DEBUG) fprintf(stderr, "get patch %.8s %.8s\n", | |
299 | sha1_to_hex(parent->commit->object.sha1), | |
300 | sha1_to_hex(origin->commit->object.sha1)); | |
301 | ||
302 | blob_p = read_sha1_file(parent->blob_sha1, type, | |
303 | (unsigned long *) &file_p.size); | |
304 | blob_o = read_sha1_file(origin->blob_sha1, type, | |
305 | (unsigned long *) &file_o.size); | |
306 | file_p.ptr = blob_p; | |
307 | file_o.ptr = blob_o; | |
308 | if (!file_p.ptr || !file_o.ptr) { | |
309 | free(blob_p); | |
310 | free(blob_o); | |
311 | return NULL; | |
312 | } | |
313 | ||
314 | patch = compare_buffer(&file_p, &file_o, 0); | |
315 | free(blob_p); | |
316 | free(blob_o); | |
317 | return patch; | |
318 | } | |
319 | ||
320 | static void free_patch(struct patch *p) | |
321 | { | |
322 | free(p->chunks); | |
323 | free(p); | |
324 | } | |
325 | ||
326 | static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) | |
327 | { | |
328 | struct blame_entry *ent, *prev = NULL; | |
329 | ||
330 | for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) | |
331 | prev = ent; | |
332 | ||
333 | /* prev, if not NULL, is the last one that is below e */ | |
334 | e->prev = prev; | |
335 | if (prev) { | |
336 | e->next = prev->next; | |
337 | prev->next = e; | |
338 | } | |
339 | else { | |
340 | e->next = sb->ent; | |
341 | sb->ent = e; | |
342 | } | |
343 | if (e->next) | |
344 | e->next->prev = e; | |
345 | } | |
346 | ||
347 | static void dup_entry(struct blame_entry *dst, struct blame_entry *src) | |
348 | { | |
349 | struct blame_entry *p, *n; | |
350 | p = dst->prev; | |
351 | n = dst->next; | |
352 | memcpy(dst, src, sizeof(*src)); | |
353 | dst->prev = p; | |
354 | dst->next = n; | |
355 | } | |
356 | ||
357 | static const char *nth_line(struct scoreboard *sb, int lno) | |
358 | { | |
359 | return sb->final_buf + sb->lineno[lno]; | |
360 | } | |
361 | ||
362 | static void split_overlap(struct blame_entry split[3], | |
363 | struct blame_entry *e, | |
364 | int tlno, int plno, int same, | |
365 | struct origin *parent) | |
366 | { | |
367 | /* it is known that lines between tlno to same came from | |
368 | * parent, and e has an overlap with that range. it also is | |
369 | * known that parent's line plno corresponds to e's line tlno. | |
370 | * | |
371 | * <---- e -----> | |
372 | * <------> | |
373 | * <------------> | |
374 | * <------------> | |
375 | * <------------------> | |
376 | * | |
377 | * Potentially we need to split e into three parts; before | |
378 | * this chunk, the chunk to be blamed for parent, and after | |
379 | * that portion. | |
380 | */ | |
381 | int chunk_end_lno; | |
382 | memset(split, 0, sizeof(struct blame_entry [3])); | |
383 | ||
384 | if (e->s_lno < tlno) { | |
385 | /* there is a pre-chunk part not blamed on parent */ | |
386 | split[0].suspect = e->suspect; | |
387 | split[0].lno = e->lno; | |
388 | split[0].s_lno = e->s_lno; | |
389 | split[0].num_lines = tlno - e->s_lno; | |
390 | split[1].lno = e->lno + tlno - e->s_lno; | |
391 | split[1].s_lno = plno; | |
392 | } | |
393 | else { | |
394 | split[1].lno = e->lno; | |
395 | split[1].s_lno = plno + (e->s_lno - tlno); | |
396 | } | |
397 | ||
398 | if (same < e->s_lno + e->num_lines) { | |
399 | /* there is a post-chunk part not blamed on parent */ | |
400 | split[2].suspect = e->suspect; | |
401 | split[2].lno = e->lno + (same - e->s_lno); | |
402 | split[2].s_lno = e->s_lno + (same - e->s_lno); | |
403 | split[2].num_lines = e->s_lno + e->num_lines - same; | |
404 | chunk_end_lno = split[2].lno; | |
405 | } | |
406 | else | |
407 | chunk_end_lno = e->lno + e->num_lines; | |
408 | split[1].num_lines = chunk_end_lno - split[1].lno; | |
409 | ||
410 | if (split[1].num_lines < 1) | |
411 | return; | |
412 | split[1].suspect = parent; | |
413 | } | |
414 | ||
415 | static void split_blame(struct scoreboard *sb, | |
416 | struct blame_entry split[3], | |
417 | struct blame_entry *e) | |
418 | { | |
419 | struct blame_entry *new_entry; | |
420 | ||
421 | if (split[0].suspect && split[2].suspect) { | |
422 | /* we need to split e into two and add another for parent */ | |
423 | dup_entry(e, &split[0]); | |
424 | ||
425 | new_entry = xmalloc(sizeof(*new_entry)); | |
426 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
427 | add_blame_entry(sb, new_entry); | |
428 | ||
429 | new_entry = xmalloc(sizeof(*new_entry)); | |
430 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
431 | add_blame_entry(sb, new_entry); | |
432 | } | |
433 | else if (!split[0].suspect && !split[2].suspect) | |
434 | /* parent covers the entire area */ | |
435 | dup_entry(e, &split[1]); | |
436 | else if (split[0].suspect) { | |
437 | dup_entry(e, &split[0]); | |
438 | ||
439 | new_entry = xmalloc(sizeof(*new_entry)); | |
440 | memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); | |
441 | add_blame_entry(sb, new_entry); | |
442 | } | |
443 | else { | |
444 | dup_entry(e, &split[1]); | |
445 | ||
446 | new_entry = xmalloc(sizeof(*new_entry)); | |
447 | memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); | |
448 | add_blame_entry(sb, new_entry); | |
449 | } | |
450 | ||
451 | if (DEBUG) { | |
452 | struct blame_entry *ent; | |
453 | int lno = 0, corrupt = 0; | |
454 | ||
455 | for (ent = sb->ent; ent; ent = ent->next) { | |
456 | if (lno != ent->lno) | |
457 | corrupt = 1; | |
458 | if (ent->s_lno < 0) | |
459 | corrupt = 1; | |
460 | lno += ent->num_lines; | |
461 | } | |
462 | if (corrupt) { | |
463 | lno = 0; | |
464 | for (ent = sb->ent; ent; ent = ent->next) { | |
465 | printf("L %8d l %8d n %8d\n", | |
466 | lno, ent->lno, ent->num_lines); | |
467 | lno = ent->lno + ent->num_lines; | |
468 | } | |
469 | die("oops"); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
474 | static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, | |
475 | int tlno, int plno, int same, | |
476 | struct origin *parent) | |
477 | { | |
478 | struct blame_entry split[3]; | |
479 | ||
480 | split_overlap(split, e, tlno, plno, same, parent); | |
481 | if (!split[1].suspect) | |
482 | return; | |
483 | split_blame(sb, split, e); | |
484 | } | |
485 | ||
486 | static int find_last_in_target(struct scoreboard *sb, struct origin *target) | |
487 | { | |
488 | struct blame_entry *e; | |
489 | int last_in_target = -1; | |
490 | ||
491 | for (e = sb->ent; e; e = e->next) { | |
492 | if (e->guilty || e->suspect != target) | |
493 | continue; | |
494 | if (last_in_target < e->s_lno + e->num_lines) | |
495 | last_in_target = e->s_lno + e->num_lines; | |
496 | } | |
497 | return last_in_target; | |
498 | } | |
499 | ||
500 | static void blame_chunk(struct scoreboard *sb, | |
501 | int tlno, int plno, int same, | |
502 | struct origin *target, struct origin *parent) | |
503 | { | |
504 | struct blame_entry *e, *n; | |
505 | ||
506 | for (e = sb->ent; e; e = n) { | |
507 | n = e->next; | |
508 | if (e->guilty || e->suspect != target) | |
509 | continue; | |
510 | if (same <= e->s_lno) | |
511 | continue; | |
512 | if (tlno < e->s_lno + e->num_lines) | |
513 | blame_overlap(sb, e, tlno, plno, same, parent); | |
514 | } | |
515 | } | |
516 | ||
517 | static int pass_blame_to_parent(struct scoreboard *sb, | |
518 | struct origin *target, | |
519 | struct origin *parent) | |
520 | { | |
521 | int i, last_in_target, plno, tlno; | |
522 | struct patch *patch; | |
523 | ||
524 | last_in_target = find_last_in_target(sb, target); | |
525 | if (last_in_target < 0) | |
526 | return 1; /* nothing remains for this target */ | |
527 | ||
528 | patch = get_patch(parent, target); | |
529 | plno = tlno = 0; | |
530 | for (i = 0; i < patch->num; i++) { | |
531 | struct chunk *chunk = &patch->chunks[i]; | |
532 | ||
533 | if (DEBUG) | |
534 | fprintf(stderr, | |
535 | "plno = %d, tlno = %d, " | |
536 | "same as parent up to %d, resync %d and %d\n", | |
537 | plno, tlno, | |
538 | chunk->same, chunk->p_next, chunk->t_next); | |
539 | blame_chunk(sb, tlno, plno, chunk->same, target, parent); | |
540 | plno = chunk->p_next; | |
541 | tlno = chunk->t_next; | |
542 | } | |
543 | /* rest (i.e. anything above tlno) are the same as parent */ | |
544 | blame_chunk(sb, tlno, plno, last_in_target, target, parent); | |
545 | ||
546 | free_patch(patch); | |
547 | return 0; | |
548 | } | |
549 | ||
d24bba80 JH |
550 | static void copy_split_if_better(struct blame_entry best_so_far[3], |
551 | struct blame_entry this[3]) | |
552 | { | |
553 | if (!this[1].suspect) | |
554 | return; | |
555 | if (best_so_far[1].suspect && | |
556 | (this[1].num_lines < best_so_far[1].num_lines)) | |
557 | return; | |
558 | memcpy(best_so_far, this, sizeof(struct blame_entry [3])); | |
559 | } | |
560 | ||
561 | static void find_copy_in_blob(struct scoreboard *sb, | |
562 | struct blame_entry *ent, | |
563 | struct origin *parent, | |
564 | struct blame_entry split[3], | |
565 | mmfile_t *file_p) | |
566 | { | |
567 | const char *cp; | |
568 | int cnt; | |
569 | mmfile_t file_o; | |
570 | struct patch *patch; | |
571 | int i, plno, tlno; | |
572 | ||
573 | cp = nth_line(sb, ent->lno); | |
574 | file_o.ptr = (char*) cp; | |
575 | cnt = ent->num_lines; | |
576 | ||
577 | while (cnt && cp < sb->final_buf + sb->final_buf_size) { | |
578 | if (*cp++ == '\n') | |
579 | cnt--; | |
580 | } | |
581 | file_o.size = cp - file_o.ptr; | |
582 | ||
583 | patch = compare_buffer(file_p, &file_o, 1); | |
584 | ||
585 | memset(split, 0, sizeof(struct blame_entry [3])); | |
586 | plno = tlno = 0; | |
587 | for (i = 0; i < patch->num; i++) { | |
588 | struct chunk *chunk = &patch->chunks[i]; | |
589 | ||
590 | /* tlno to chunk->same are the same as ent */ | |
591 | if (ent->num_lines <= tlno) | |
592 | break; | |
593 | if (tlno < chunk->same) { | |
594 | struct blame_entry this[3]; | |
595 | split_overlap(this, ent, | |
596 | tlno + ent->s_lno, plno, | |
597 | chunk->same + ent->s_lno, | |
598 | parent); | |
599 | copy_split_if_better(split, this); | |
600 | } | |
601 | plno = chunk->p_next; | |
602 | tlno = chunk->t_next; | |
603 | } | |
604 | free_patch(patch); | |
605 | } | |
606 | ||
607 | static int find_move_in_parent(struct scoreboard *sb, | |
608 | struct origin *target, | |
609 | struct origin *parent) | |
610 | { | |
611 | int last_in_target; | |
612 | struct blame_entry *ent, split[3]; | |
613 | mmfile_t file_p; | |
614 | char type[10]; | |
615 | char *blob_p; | |
616 | ||
617 | last_in_target = find_last_in_target(sb, target); | |
618 | if (last_in_target < 0) | |
619 | return 1; /* nothing remains for this target */ | |
620 | ||
621 | blob_p = read_sha1_file(parent->blob_sha1, type, | |
622 | (unsigned long *) &file_p.size); | |
623 | file_p.ptr = blob_p; | |
624 | if (!file_p.ptr) { | |
625 | free(blob_p); | |
626 | return 0; | |
627 | } | |
628 | ||
629 | for (ent = sb->ent; ent; ent = ent->next) { | |
630 | if (ent->suspect != target || ent->guilty) | |
631 | continue; | |
632 | find_copy_in_blob(sb, ent, parent, split, &file_p); | |
633 | if (split[1].suspect) | |
634 | split_blame(sb, split, ent); | |
635 | } | |
636 | free(blob_p); | |
637 | return 0; | |
638 | } | |
639 | ||
18abd745 JH |
640 | static int find_copy_in_parent(struct scoreboard *sb, |
641 | struct origin *target, | |
642 | struct commit *parent, | |
643 | struct origin *porigin, | |
644 | int opt) | |
645 | { | |
646 | struct diff_options diff_opts; | |
647 | const char *paths[1]; | |
648 | struct blame_entry *ent; | |
649 | int i; | |
650 | ||
651 | if (find_last_in_target(sb, target) < 0) | |
652 | return 1; /* nothing remains for this target */ | |
653 | ||
654 | diff_setup(&diff_opts); | |
655 | diff_opts.recursive = 1; | |
656 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
657 | ||
658 | /* Try "find copies harder" on new path */ | |
659 | if ((opt & PICKAXE_BLAME_COPY_HARDER) && | |
660 | (!porigin || strcmp(target->path, porigin->path))) { | |
661 | diff_opts.detect_rename = DIFF_DETECT_COPY; | |
662 | diff_opts.find_copies_harder = 1; | |
663 | } | |
664 | paths[0] = NULL; | |
665 | diff_tree_setup_paths(paths, &diff_opts); | |
666 | if (diff_setup_done(&diff_opts) < 0) | |
667 | die("diff-setup"); | |
668 | diff_tree_sha1(parent->tree->object.sha1, | |
669 | target->commit->tree->object.sha1, | |
670 | "", &diff_opts); | |
671 | diffcore_std(&diff_opts); | |
672 | ||
673 | for (ent = sb->ent; ent; ent = ent->next) { | |
674 | struct blame_entry split[3]; | |
675 | if (ent->suspect != target || ent->guilty) | |
676 | continue; | |
677 | ||
678 | memset(split, 0, sizeof(split)); | |
679 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
680 | struct diff_filepair *p = diff_queued_diff.queue[i]; | |
681 | struct origin *norigin; | |
682 | mmfile_t file_p; | |
683 | char type[10]; | |
684 | char *blob; | |
685 | struct blame_entry this[3]; | |
686 | ||
687 | if (!DIFF_FILE_VALID(p->one)) | |
688 | continue; /* does not exist in parent */ | |
689 | if (porigin && !strcmp(p->one->path, porigin->path)) | |
690 | /* find_move already dealt with this path */ | |
691 | continue; | |
692 | norigin = find_origin(sb, parent, p->one->path); | |
693 | ||
694 | blob = read_sha1_file(norigin->blob_sha1, type, | |
695 | (unsigned long *) &file_p.size); | |
696 | file_p.ptr = blob; | |
697 | if (!file_p.ptr) { | |
698 | free(blob); | |
699 | continue; | |
700 | } | |
701 | find_copy_in_blob(sb, ent, norigin, this, &file_p); | |
702 | copy_split_if_better(split, this); | |
703 | } | |
704 | if (split[1].suspect) | |
705 | split_blame(sb, split, ent); | |
706 | } | |
707 | diff_flush(&diff_opts); | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
cee7f245 JH |
712 | #define MAXPARENT 16 |
713 | ||
d24bba80 | 714 | static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt) |
cee7f245 JH |
715 | { |
716 | int i; | |
717 | struct commit *commit = origin->commit; | |
718 | struct commit_list *parent; | |
719 | struct origin *parent_origin[MAXPARENT], *porigin; | |
720 | ||
721 | memset(parent_origin, 0, sizeof(parent_origin)); | |
722 | for (i = 0, parent = commit->parents; | |
723 | i < MAXPARENT && parent; | |
724 | parent = parent->next, i++) { | |
725 | struct commit *p = parent->item; | |
726 | ||
727 | if (parse_commit(p)) | |
728 | continue; | |
729 | porigin = find_origin(sb, parent->item, origin->path); | |
730 | if (!porigin) | |
731 | porigin = find_rename(sb, parent->item, origin); | |
732 | if (!porigin) | |
733 | continue; | |
734 | if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) { | |
735 | struct blame_entry *e; | |
736 | for (e = sb->ent; e; e = e->next) | |
737 | if (e->suspect == origin) | |
738 | e->suspect = porigin; | |
739 | /* now everything blamed for origin is blamed for | |
740 | * porigin, we do not need to keep it anymore. | |
741 | * Do not free porigin (or the ones we got from | |
742 | * earlier round); they may still be used elsewhere. | |
743 | */ | |
744 | free_origin(origin); | |
745 | return; | |
746 | } | |
747 | parent_origin[i] = porigin; | |
748 | } | |
749 | ||
750 | for (i = 0, parent = commit->parents; | |
751 | i < MAXPARENT && parent; | |
752 | parent = parent->next, i++) { | |
753 | struct origin *porigin = parent_origin[i]; | |
754 | if (!porigin) | |
755 | continue; | |
756 | if (pass_blame_to_parent(sb, origin, porigin)) | |
757 | return; | |
758 | } | |
d24bba80 JH |
759 | |
760 | /* | |
761 | * Optionally run "miff" to find moves in parents' files here. | |
762 | */ | |
763 | if (opt & PICKAXE_BLAME_MOVE) | |
764 | for (i = 0, parent = commit->parents; | |
765 | i < MAXPARENT && parent; | |
766 | parent = parent->next, i++) { | |
767 | struct origin *porigin = parent_origin[i]; | |
768 | if (!porigin) | |
769 | continue; | |
770 | if (find_move_in_parent(sb, origin, porigin)) | |
771 | return; | |
772 | } | |
773 | ||
18abd745 JH |
774 | /* |
775 | * Optionally run "ciff" to find copies from parents' files here. | |
776 | */ | |
777 | if (opt & PICKAXE_BLAME_COPY) | |
778 | for (i = 0, parent = commit->parents; | |
779 | i < MAXPARENT && parent; | |
780 | parent = parent->next, i++) { | |
781 | struct origin *porigin = parent_origin[i]; | |
782 | if (find_copy_in_parent(sb, origin, parent->item, | |
783 | porigin, opt)) | |
784 | return; | |
785 | } | |
cee7f245 JH |
786 | } |
787 | ||
d24bba80 | 788 | static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt) |
cee7f245 JH |
789 | { |
790 | while (1) { | |
791 | struct blame_entry *ent; | |
792 | struct commit *commit; | |
793 | struct origin *suspect = NULL; | |
794 | ||
795 | /* find one suspect to break down */ | |
796 | for (ent = sb->ent; !suspect && ent; ent = ent->next) | |
797 | if (!ent->guilty) | |
798 | suspect = ent->suspect; | |
799 | if (!suspect) | |
800 | return; /* all done */ | |
801 | ||
802 | commit = suspect->commit; | |
803 | parse_commit(commit); | |
804 | if (!(commit->object.flags & UNINTERESTING) && | |
805 | !(revs->max_age != -1 && commit->date < revs->max_age)) | |
d24bba80 | 806 | pass_blame(sb, suspect, opt); |
cee7f245 JH |
807 | |
808 | /* Take responsibility for the remaining entries */ | |
809 | for (ent = sb->ent; ent; ent = ent->next) | |
810 | if (ent->suspect == suspect) | |
811 | ent->guilty = 1; | |
812 | } | |
813 | } | |
814 | ||
815 | static const char *format_time(unsigned long time, const char *tz_str, | |
816 | int show_raw_time) | |
817 | { | |
818 | static char time_buf[128]; | |
819 | time_t t = time; | |
820 | int minutes, tz; | |
821 | struct tm *tm; | |
822 | ||
823 | if (show_raw_time) { | |
824 | sprintf(time_buf, "%lu %s", time, tz_str); | |
825 | return time_buf; | |
826 | } | |
827 | ||
828 | tz = atoi(tz_str); | |
829 | minutes = tz < 0 ? -tz : tz; | |
830 | minutes = (minutes / 100)*60 + (minutes % 100); | |
831 | minutes = tz < 0 ? -minutes : minutes; | |
832 | t = time + minutes * 60; | |
833 | tm = gmtime(&t); | |
834 | ||
835 | strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm); | |
836 | strcat(time_buf, tz_str); | |
837 | return time_buf; | |
838 | } | |
839 | ||
840 | struct commit_info | |
841 | { | |
842 | char *author; | |
843 | char *author_mail; | |
844 | unsigned long author_time; | |
845 | char *author_tz; | |
846 | ||
847 | /* filled only when asked for details */ | |
848 | char *committer; | |
849 | char *committer_mail; | |
850 | unsigned long committer_time; | |
851 | char *committer_tz; | |
852 | ||
853 | char *summary; | |
854 | }; | |
855 | ||
856 | static void get_ac_line(const char *inbuf, const char *what, | |
857 | int bufsz, char *person, char **mail, | |
858 | unsigned long *time, char **tz) | |
859 | { | |
860 | int len; | |
861 | char *tmp, *endp; | |
862 | ||
863 | tmp = strstr(inbuf, what); | |
864 | if (!tmp) | |
865 | goto error_out; | |
866 | tmp += strlen(what); | |
867 | endp = strchr(tmp, '\n'); | |
868 | if (!endp) | |
869 | len = strlen(tmp); | |
870 | else | |
871 | len = endp - tmp; | |
872 | if (bufsz <= len) { | |
873 | error_out: | |
874 | /* Ugh */ | |
875 | person = *mail = *tz = "(unknown)"; | |
876 | *time = 0; | |
877 | return; | |
878 | } | |
879 | memcpy(person, tmp, len); | |
880 | ||
881 | tmp = person; | |
882 | tmp += len; | |
883 | *tmp = 0; | |
884 | while (*tmp != ' ') | |
885 | tmp--; | |
886 | *tz = tmp+1; | |
887 | ||
888 | *tmp = 0; | |
889 | while (*tmp != ' ') | |
890 | tmp--; | |
891 | *time = strtoul(tmp, NULL, 10); | |
892 | ||
893 | *tmp = 0; | |
894 | while (*tmp != ' ') | |
895 | tmp--; | |
896 | *mail = tmp + 1; | |
897 | *tmp = 0; | |
898 | } | |
899 | ||
900 | static void get_commit_info(struct commit *commit, | |
901 | struct commit_info *ret, | |
902 | int detailed) | |
903 | { | |
904 | int len; | |
905 | char *tmp, *endp; | |
906 | static char author_buf[1024]; | |
907 | static char committer_buf[1024]; | |
908 | static char summary_buf[1024]; | |
909 | ||
910 | ret->author = author_buf; | |
911 | get_ac_line(commit->buffer, "\nauthor ", | |
912 | sizeof(author_buf), author_buf, &ret->author_mail, | |
913 | &ret->author_time, &ret->author_tz); | |
914 | ||
915 | if (!detailed) | |
916 | return; | |
917 | ||
918 | ret->committer = committer_buf; | |
919 | get_ac_line(commit->buffer, "\ncommitter ", | |
920 | sizeof(committer_buf), committer_buf, &ret->committer_mail, | |
921 | &ret->committer_time, &ret->committer_tz); | |
922 | ||
923 | ret->summary = summary_buf; | |
924 | tmp = strstr(commit->buffer, "\n\n"); | |
925 | if (!tmp) { | |
926 | error_out: | |
927 | sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1)); | |
928 | return; | |
929 | } | |
930 | tmp += 2; | |
931 | endp = strchr(tmp, '\n'); | |
932 | if (!endp) | |
933 | goto error_out; | |
934 | len = endp - tmp; | |
935 | if (len >= sizeof(summary_buf)) | |
936 | goto error_out; | |
937 | memcpy(summary_buf, tmp, len); | |
938 | summary_buf[len] = 0; | |
939 | } | |
940 | ||
941 | #define OUTPUT_ANNOTATE_COMPAT 001 | |
942 | #define OUTPUT_LONG_OBJECT_NAME 002 | |
943 | #define OUTPUT_RAW_TIMESTAMP 004 | |
944 | #define OUTPUT_PORCELAIN 010 | |
945 | #define OUTPUT_SHOW_NAME 020 | |
946 | #define OUTPUT_SHOW_NUMBER 040 | |
947 | ||
948 | static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent) | |
949 | { | |
950 | int cnt; | |
951 | const char *cp; | |
952 | struct origin *suspect = ent->suspect; | |
953 | char hex[41]; | |
954 | ||
955 | strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); | |
956 | printf("%s%c%d %d %d\n", | |
957 | hex, | |
958 | ent->guilty ? ' ' : '*', // purely for debugging | |
959 | ent->s_lno + 1, | |
960 | ent->lno + 1, | |
961 | ent->num_lines); | |
962 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { | |
963 | struct commit_info ci; | |
964 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
965 | get_commit_info(suspect->commit, &ci, 1); | |
966 | printf("author %s\n", ci.author); | |
967 | printf("author-mail %s\n", ci.author_mail); | |
968 | printf("author-time %lu\n", ci.author_time); | |
969 | printf("author-tz %s\n", ci.author_tz); | |
970 | printf("committer %s\n", ci.committer); | |
971 | printf("committer-mail %s\n", ci.committer_mail); | |
972 | printf("committer-time %lu\n", ci.committer_time); | |
973 | printf("committer-tz %s\n", ci.committer_tz); | |
974 | printf("filename %s\n", suspect->path); | |
975 | printf("summary %s\n", ci.summary); | |
976 | } | |
977 | else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH) | |
978 | printf("filename %s\n", suspect->path); | |
979 | ||
980 | cp = nth_line(sb, ent->lno); | |
981 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
982 | char ch; | |
983 | if (cnt) | |
984 | printf("%s %d %d\n", hex, | |
985 | ent->s_lno + 1 + cnt, | |
986 | ent->lno + 1 + cnt); | |
987 | putchar('\t'); | |
988 | do { | |
989 | ch = *cp++; | |
990 | putchar(ch); | |
991 | } while (ch != '\n' && | |
992 | cp < sb->final_buf + sb->final_buf_size); | |
993 | } | |
994 | } | |
995 | ||
996 | static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt) | |
997 | { | |
998 | int cnt; | |
999 | const char *cp; | |
1000 | struct origin *suspect = ent->suspect; | |
1001 | struct commit_info ci; | |
1002 | char hex[41]; | |
1003 | int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); | |
1004 | ||
1005 | get_commit_info(suspect->commit, &ci, 1); | |
1006 | strcpy(hex, sha1_to_hex(suspect->commit->object.sha1)); | |
1007 | ||
1008 | cp = nth_line(sb, ent->lno); | |
1009 | for (cnt = 0; cnt < ent->num_lines; cnt++) { | |
1010 | char ch; | |
1011 | ||
1012 | printf("%.*s", (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8, hex); | |
1013 | if (opt & OUTPUT_ANNOTATE_COMPAT) | |
1014 | printf("\t(%10s\t%10s\t%d)", ci.author, | |
1015 | format_time(ci.author_time, ci.author_tz, | |
1016 | show_raw_time), | |
1017 | ent->lno + 1 + cnt); | |
1018 | else { | |
1019 | if (opt & OUTPUT_SHOW_NAME) | |
1020 | printf(" %-*.*s", longest_file, longest_file, | |
1021 | suspect->path); | |
1022 | if (opt & OUTPUT_SHOW_NUMBER) | |
1023 | printf(" %*d", max_orig_digits, | |
1024 | ent->s_lno + 1 + cnt); | |
1025 | printf(" (%-*.*s %10s %*d) ", | |
1026 | longest_author, longest_author, ci.author, | |
1027 | format_time(ci.author_time, ci.author_tz, | |
1028 | show_raw_time), | |
1029 | max_digits, ent->lno + 1 + cnt); | |
1030 | } | |
1031 | do { | |
1032 | ch = *cp++; | |
1033 | putchar(ch); | |
1034 | } while (ch != '\n' && | |
1035 | cp < sb->final_buf + sb->final_buf_size); | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | static void output(struct scoreboard *sb, int option) | |
1040 | { | |
1041 | struct blame_entry *ent; | |
1042 | ||
1043 | if (option & OUTPUT_PORCELAIN) { | |
1044 | for (ent = sb->ent; ent; ent = ent->next) { | |
1045 | struct blame_entry *oth; | |
1046 | struct origin *suspect = ent->suspect; | |
1047 | struct commit *commit = suspect->commit; | |
1048 | if (commit->object.flags & MORE_THAN_ONE_PATH) | |
1049 | continue; | |
1050 | for (oth = ent->next; oth; oth = oth->next) { | |
1051 | if ((oth->suspect->commit != commit) || | |
1052 | !strcmp(oth->suspect->path, suspect->path)) | |
1053 | continue; | |
1054 | commit->object.flags |= MORE_THAN_ONE_PATH; | |
1055 | break; | |
1056 | } | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | for (ent = sb->ent; ent; ent = ent->next) { | |
1061 | if (option & OUTPUT_PORCELAIN) | |
1062 | emit_porcelain(sb, ent); | |
1063 | else | |
1064 | emit_other(sb, ent, option); | |
1065 | } | |
1066 | } | |
1067 | ||
1068 | static int prepare_lines(struct scoreboard *sb) | |
1069 | { | |
1070 | const char *buf = sb->final_buf; | |
1071 | unsigned long len = sb->final_buf_size; | |
1072 | int num = 0, incomplete = 0, bol = 1; | |
1073 | ||
1074 | if (len && buf[len-1] != '\n') | |
1075 | incomplete++; /* incomplete line at the end */ | |
1076 | while (len--) { | |
1077 | if (bol) { | |
1078 | sb->lineno = xrealloc(sb->lineno, | |
1079 | sizeof(int* ) * (num + 1)); | |
1080 | sb->lineno[num] = buf - sb->final_buf; | |
1081 | bol = 0; | |
1082 | } | |
1083 | if (*buf++ == '\n') { | |
1084 | num++; | |
1085 | bol = 1; | |
1086 | } | |
1087 | } | |
1ca6ca87 JH |
1088 | sb->lineno = xrealloc(sb->lineno, |
1089 | sizeof(int* ) * (num + incomplete + 1)); | |
1090 | sb->lineno[num + incomplete] = buf - sb->final_buf; | |
cee7f245 JH |
1091 | sb->num_lines = num + incomplete; |
1092 | return sb->num_lines; | |
1093 | } | |
1094 | ||
1095 | static int read_ancestry(const char *graft_file) | |
1096 | { | |
1097 | FILE *fp = fopen(graft_file, "r"); | |
1098 | char buf[1024]; | |
1099 | if (!fp) | |
1100 | return -1; | |
1101 | while (fgets(buf, sizeof(buf), fp)) { | |
1102 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
1103 | int len = strlen(buf); | |
1104 | struct commit_graft *graft = read_graft_line(buf, len); | |
1105 | register_commit_graft(graft, 0); | |
1106 | } | |
1107 | fclose(fp); | |
1108 | return 0; | |
1109 | } | |
1110 | ||
1111 | static int lineno_width(int lines) | |
1112 | { | |
1113 | int i, width; | |
1114 | ||
1115 | for (width = 1, i = 10; i <= lines + 1; width++) | |
1116 | i *= 10; | |
1117 | return width; | |
1118 | } | |
1119 | ||
1120 | static void find_alignment(struct scoreboard *sb, int *option) | |
1121 | { | |
1122 | int longest_src_lines = 0; | |
1123 | int longest_dst_lines = 0; | |
1124 | struct blame_entry *e; | |
1125 | ||
1126 | for (e = sb->ent; e; e = e->next) { | |
1127 | struct origin *suspect = e->suspect; | |
1128 | struct commit_info ci; | |
1129 | int num; | |
1130 | ||
1131 | if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { | |
1132 | suspect->commit->object.flags |= METAINFO_SHOWN; | |
1133 | get_commit_info(suspect->commit, &ci, 1); | |
1134 | if (strcmp(suspect->path, sb->path)) | |
1135 | *option |= OUTPUT_SHOW_NAME; | |
1136 | num = strlen(suspect->path); | |
1137 | if (longest_file < num) | |
1138 | longest_file = num; | |
1139 | num = strlen(ci.author); | |
1140 | if (longest_author < num) | |
1141 | longest_author = num; | |
1142 | } | |
1143 | num = e->s_lno + e->num_lines; | |
1144 | if (longest_src_lines < num) | |
1145 | longest_src_lines = num; | |
1146 | num = e->lno + e->num_lines; | |
1147 | if (longest_dst_lines < num) | |
1148 | longest_dst_lines = num; | |
1149 | } | |
1150 | max_orig_digits = lineno_width(longest_src_lines); | |
1151 | max_digits = lineno_width(longest_dst_lines); | |
1152 | } | |
1153 | ||
1154 | static int has_path_in_work_tree(const char *path) | |
1155 | { | |
1156 | struct stat st; | |
1157 | return !lstat(path, &st); | |
1158 | } | |
1159 | ||
1160 | int cmd_pickaxe(int argc, const char **argv, const char *prefix) | |
1161 | { | |
1162 | struct rev_info revs; | |
1163 | const char *path; | |
1164 | struct scoreboard sb; | |
1165 | struct origin *o; | |
1166 | struct blame_entry *ent; | |
d24bba80 | 1167 | int i, seen_dashdash, unk, opt; |
cee7f245 JH |
1168 | long bottom, top, lno; |
1169 | int output_option = 0; | |
1170 | const char *revs_file = NULL; | |
1171 | const char *final_commit_name = NULL; | |
1172 | char type[10]; | |
1173 | ||
d24bba80 | 1174 | opt = 0; |
cee7f245 JH |
1175 | bottom = top = 0; |
1176 | seen_dashdash = 0; | |
1177 | for (unk = i = 1; i < argc; i++) { | |
1178 | const char *arg = argv[i]; | |
1179 | if (*arg != '-') | |
1180 | break; | |
1181 | else if (!strcmp("-c", arg)) | |
1182 | output_option |= OUTPUT_ANNOTATE_COMPAT; | |
1183 | else if (!strcmp("-t", arg)) | |
1184 | output_option |= OUTPUT_RAW_TIMESTAMP; | |
1185 | else if (!strcmp("-l", arg)) | |
1186 | output_option |= OUTPUT_LONG_OBJECT_NAME; | |
1187 | else if (!strcmp("-S", arg) && ++i < argc) | |
1188 | revs_file = argv[i]; | |
d24bba80 JH |
1189 | else if (!strcmp("-M", arg)) |
1190 | opt |= PICKAXE_BLAME_MOVE; | |
18abd745 JH |
1191 | else if (!strcmp("-C", arg)) { |
1192 | if (opt & PICKAXE_BLAME_COPY) | |
1193 | opt |= PICKAXE_BLAME_COPY_HARDER; | |
1194 | opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE; | |
1195 | } | |
cee7f245 JH |
1196 | else if (!strcmp("-L", arg) && ++i < argc) { |
1197 | char *term; | |
1198 | arg = argv[i]; | |
1199 | if (bottom || top) | |
1200 | die("More than one '-L n,m' option given"); | |
1201 | bottom = strtol(arg, &term, 10); | |
1202 | if (*term == ',') { | |
1203 | top = strtol(term + 1, &term, 10); | |
1204 | if (*term) | |
1205 | usage(pickaxe_usage); | |
1206 | } | |
1207 | if (bottom && top && top < bottom) { | |
1208 | unsigned long tmp; | |
1209 | tmp = top; top = bottom; bottom = tmp; | |
1210 | } | |
1211 | } | |
1212 | else if (!strcmp("-f", arg) || | |
1213 | !strcmp("--show-name", arg)) | |
1214 | output_option |= OUTPUT_SHOW_NAME; | |
1215 | else if (!strcmp("-n", arg) || | |
1216 | !strcmp("--show-number", arg)) | |
1217 | output_option |= OUTPUT_SHOW_NUMBER; | |
1218 | else if (!strcmp("-p", arg) || | |
1219 | !strcmp("--porcelain", arg)) | |
1220 | output_option |= OUTPUT_PORCELAIN; | |
1221 | else if (!strcmp("--", arg)) { | |
1222 | seen_dashdash = 1; | |
1223 | i++; | |
1224 | break; | |
1225 | } | |
1226 | else | |
1227 | argv[unk++] = arg; | |
1228 | } | |
1229 | ||
1230 | /* We have collected options unknown to us in argv[1..unk] | |
1231 | * which are to be passed to revision machinery if we are | |
1232 | * going to do the "bottom" procesing. | |
1233 | * | |
1234 | * The remaining are: | |
1235 | * | |
1236 | * (1) if seen_dashdash, its either | |
1237 | * "-options -- <path>" or | |
1238 | * "-options -- <path> <rev>". | |
1239 | * but the latter is allowed only if there is no | |
1240 | * options that we passed to revision machinery. | |
1241 | * | |
1242 | * (2) otherwise, we may have "--" somewhere later and | |
1243 | * might be looking at the first one of multiple 'rev' | |
1244 | * parameters (e.g. " master ^next ^maint -- path"). | |
1245 | * See if there is a dashdash first, and give the | |
1246 | * arguments before that to revision machinery. | |
1247 | * After that there must be one 'path'. | |
1248 | * | |
1249 | * (3) otherwise, its one of the three: | |
1250 | * "-options <path> <rev>" | |
1251 | * "-options <rev> <path>" | |
1252 | * "-options <path>" | |
1253 | * but again the first one is allowed only if | |
1254 | * there is no options that we passed to revision | |
1255 | * machinery. | |
1256 | */ | |
1257 | ||
1258 | if (seen_dashdash) { | |
1259 | /* (1) */ | |
1260 | if (argc <= i) | |
1261 | usage(pickaxe_usage); | |
1262 | path = argv[i]; | |
1263 | if (i + 1 == argc - 1) { | |
1264 | if (unk != 1) | |
1265 | usage(pickaxe_usage); | |
1266 | argv[unk++] = argv[i + 1]; | |
1267 | } | |
1268 | else if (i + 1 != argc) | |
1269 | /* garbage at end */ | |
1270 | usage(pickaxe_usage); | |
1271 | } | |
1272 | else { | |
1273 | int j; | |
1274 | for (j = i; !seen_dashdash && j < argc; j++) | |
1275 | if (!strcmp(argv[j], "--")) | |
1276 | seen_dashdash = j; | |
1277 | if (seen_dashdash) { | |
1278 | if (seen_dashdash + 1 != argc - 1) | |
1279 | usage(pickaxe_usage); | |
1280 | path = argv[seen_dashdash + 1]; | |
1281 | for (j = i; j < seen_dashdash; j++) | |
1282 | argv[unk++] = argv[j]; | |
1283 | } | |
1284 | else { | |
1285 | /* (3) */ | |
1286 | path = argv[i]; | |
1287 | if (i + 1 == argc - 1) { | |
1288 | final_commit_name = argv[i + 1]; | |
1289 | ||
1290 | /* if (unk == 1) we could be getting | |
1291 | * old-style | |
1292 | */ | |
1293 | if (unk == 1 && !has_path_in_work_tree(path)) { | |
1294 | path = argv[i + 1]; | |
1295 | final_commit_name = argv[i]; | |
1296 | } | |
1297 | } | |
1298 | else if (i != argc - 1) | |
1299 | usage(pickaxe_usage); /* garbage at end */ | |
1300 | ||
1301 | if (!has_path_in_work_tree(path)) | |
1302 | die("cannot stat path %s: %s", | |
1303 | path, strerror(errno)); | |
1304 | } | |
1305 | } | |
1306 | ||
1307 | if (final_commit_name) | |
1308 | argv[unk++] = final_commit_name; | |
1309 | ||
1310 | /* Now we got rev and path. We do not want the path pruning | |
1311 | * but we may want "bottom" processing. | |
1312 | */ | |
1313 | argv[unk] = NULL; | |
1314 | ||
1315 | init_revisions(&revs, NULL); | |
1316 | setup_revisions(unk, argv, &revs, "HEAD"); | |
1317 | memset(&sb, 0, sizeof(sb)); | |
1318 | ||
1319 | /* There must be one and only one positive commit in the | |
1320 | * revs->pending array. | |
1321 | */ | |
1322 | for (i = 0; i < revs.pending.nr; i++) { | |
1323 | struct object *obj = revs.pending.objects[i].item; | |
1324 | if (obj->flags & UNINTERESTING) | |
1325 | continue; | |
1326 | while (obj->type == OBJ_TAG) | |
1327 | obj = deref_tag(obj, NULL, 0); | |
1328 | if (obj->type != OBJ_COMMIT) | |
1329 | die("Non commit %s?", | |
1330 | revs.pending.objects[i].name); | |
1331 | if (sb.final) | |
1332 | die("More than one commit to dig from %s and %s?", | |
1333 | revs.pending.objects[i].name, | |
1334 | final_commit_name); | |
1335 | sb.final = (struct commit *) obj; | |
1336 | final_commit_name = revs.pending.objects[i].name; | |
1337 | } | |
1338 | ||
1339 | if (!sb.final) { | |
1340 | /* "--not A B -- path" without anything positive */ | |
1341 | unsigned char head_sha1[20]; | |
1342 | ||
1343 | final_commit_name = "HEAD"; | |
1344 | if (get_sha1(final_commit_name, head_sha1)) | |
1345 | die("No such ref: HEAD"); | |
1346 | sb.final = lookup_commit_reference(head_sha1); | |
1347 | add_pending_object(&revs, &(sb.final->object), "HEAD"); | |
1348 | } | |
1349 | ||
1350 | /* If we have bottom, this will mark the ancestors of the | |
1351 | * bottom commits we would reach while traversing as | |
1352 | * uninteresting. | |
1353 | */ | |
1354 | prepare_revision_walk(&revs); | |
1355 | ||
1356 | o = find_origin(&sb, sb.final, path); | |
1357 | if (!o) | |
1358 | die("no such path %s in %s", path, final_commit_name); | |
1359 | ||
1360 | sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size); | |
1361 | lno = prepare_lines(&sb); | |
1362 | ||
1363 | if (bottom < 1) | |
1364 | bottom = 1; | |
1365 | if (top < 1) | |
1366 | top = lno; | |
1367 | bottom--; | |
1368 | if (lno < top) | |
1369 | die("file %s has only %lu lines", path, lno); | |
1370 | ||
1371 | ent = xcalloc(1, sizeof(*ent)); | |
1372 | ent->lno = bottom; | |
1373 | ent->num_lines = top - bottom; | |
1374 | ent->suspect = o; | |
1375 | ent->s_lno = bottom; | |
1376 | ||
1377 | sb.ent = ent; | |
1378 | sb.path = path; | |
1379 | ||
1380 | if (revs_file && read_ancestry(revs_file)) | |
1381 | die("reading graft file %s failed: %s", | |
1382 | revs_file, strerror(errno)); | |
1383 | ||
d24bba80 | 1384 | assign_blame(&sb, &revs, opt); |
cee7f245 JH |
1385 | |
1386 | coalesce(&sb); | |
1387 | ||
1388 | if (!(output_option & OUTPUT_PORCELAIN)) | |
1389 | find_alignment(&sb, &output_option); | |
1390 | ||
1391 | output(&sb, output_option); | |
1392 | free((void *)sb.final_buf); | |
1393 | for (ent = sb.ent; ent; ) { | |
1394 | struct blame_entry *e = ent->next; | |
1395 | free(ent); | |
1396 | ent = e; | |
1397 | } | |
1398 | return 0; | |
1399 | } |