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