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