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