use sha1_to_hex_r() instead of strcpy
[git/git.git] / builtin / blame.c
CommitLineData
cee7f245 1/*
31653c1a 2 * Blame
cee7f245 3 *
7e6ac6e4
DK
4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
cee7f245
JH
6 */
7
8#include "cache.h"
fb58c8d5 9#include "refs.h"
cee7f245
JH
10#include "builtin.h"
11#include "blob.h"
12#include "commit.h"
13#include "tag.h"
14#include "tree-walk.h"
15#include "diff.h"
16#include "diffcore.h"
17#include "revision.h"
717d1462 18#include "quote.h"
cee7f245 19#include "xdiff-interface.h"
1cfe7733 20#include "cache-tree.h"
c455c87c 21#include "string-list.h"
f9567384 22#include "mailmap.h"
7e6ac6e4 23#include "mergesort.h"
5817da01 24#include "parse-options.h"
7e6ac6e4 25#include "prio-queue.h"
ffaf9cc0 26#include "utf8.h"
3b8a12e8 27#include "userdiff.h"
25ed3412 28#include "line-range.h"
58dbfa2e 29#include "line-log.h"
dbe44faa 30#include "dir.h"
cee7f245 31
ce41720c 32static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
5817da01
PH
33
34static const char *blame_opt_usage[] = {
35 blame_usage,
36 "",
9c9b4f2f 37 N_("<rev-opts> are documented in git-rev-list(1)"),
5817da01
PH
38 NULL
39};
cee7f245
JH
40
41static int longest_file;
42static int longest_author;
43static int max_orig_digits;
44static int max_digits;
5ff62c30 45static int max_score_digits;
4c10a5ca 46static int show_root;
85af7929 47static int reverse;
4c10a5ca 48static int blank_boundary;
717d1462 49static int incremental;
582aa00b 50static int xdl_opts;
84393bfd 51static int abbrev = -1;
3d1aa566 52static int no_whole_file_rename;
31653c1a 53
a5481a6c 54static struct date_mode blame_date_mode = { DATE_ISO8601 };
31653c1a
EL
55static size_t blame_date_width;
56
c455c87c 57static struct string_list mailmap;
cee7f245 58
54a4c617
JH
59#ifndef DEBUG
60#define DEBUG 0
61#endif
62
c2e525d9
JH
63/* stats */
64static int num_read_blob;
65static int num_get_patch;
66static int num_commits;
67
d24bba80 68#define PICKAXE_BLAME_MOVE 01
18abd745
JH
69#define PICKAXE_BLAME_COPY 02
70#define PICKAXE_BLAME_COPY_HARDER 04
c63777c0 71#define PICKAXE_BLAME_COPY_HARDEST 010
d24bba80 72
4a0fc95f
JH
73/*
74 * blame for a blame_entry with score lower than these thresholds
75 * is not passed to the parent using move/copy logic.
76 */
77static unsigned blame_move_score;
78static unsigned blame_copy_score;
79#define BLAME_DEFAULT_MOVE_SCORE 20
80#define BLAME_DEFAULT_COPY_SCORE 40
81
208acbfb 82/* Remember to update object flag allocation in object.h */
cee7f245
JH
83#define METAINFO_SHOWN (1u<<12)
84#define MORE_THAN_ONE_PATH (1u<<13)
85
86/*
54a4c617 87 * One blob in a commit that is being suspected
cee7f245
JH
88 */
89struct origin {
54a4c617 90 int refcnt;
7e6ac6e4 91 /* Record preceding blame record for this blob */
96e11709 92 struct origin *previous;
7e6ac6e4
DK
93 /* origins are put in a list linked via `next' hanging off the
94 * corresponding commit's util field in order to make finding
95 * them fast. The presence in this chain does not count
96 * towards the origin's reference count. It is tempting to
97 * let it count as long as the commit is pending examination,
98 * but even under circumstances where the commit will be
99 * present multiple times in the priority queue of unexamined
100 * commits, processing the first instance will not leave any
101 * work requiring the origin data for the second instance. An
102 * interspersed commit changing that would have to be
103 * preexisting with a different ancestry and with the same
104 * commit date in order to wedge itself between two instances
105 * of the same commit in the priority queue _and_ produce
106 * blame entries relevant for it. While we don't want to let
107 * us get tripped up by this case, it certainly does not seem
108 * worth optimizing for.
109 */
110 struct origin *next;
cee7f245 111 struct commit *commit;
7e6ac6e4
DK
112 /* `suspects' contains blame entries that may be attributed to
113 * this origin's commit or to parent commits. When a commit
114 * is being processed, all suspects will be moved, either by
115 * assigning them to an origin in a different commit, or by
116 * shipping them to the scoreboard's ent list because they
117 * cannot be attributed to a different commit.
118 */
119 struct blame_entry *suspects;
c2e525d9 120 mmfile_t file;
cee7f245 121 unsigned char blob_sha1[20];
90064710 122 unsigned mode;
7e6ac6e4
DK
123 /* guilty gets set when shipping any suspects to the final
124 * blame list instead of other commits
125 */
126 char guilty;
cee7f245
JH
127 char path[FLEX_ARRAY];
128};
129
4b4132fd
RS
130static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
131 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
132{
133 xpparam_t xpp = {0};
134 xdemitconf_t xecfg = {0};
85c20c30 135 xdemitcb_t ecb = {NULL};
4b4132fd
RS
136
137 xpp.flags = xdl_opts;
138 xecfg.ctxlen = ctxlen;
139 xecfg.hunk_func = hunk_func;
140 ecb.priv = cb_data;
141 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
142}
143
3b8a12e8
AB
144/*
145 * Prepare diff_filespec and convert it using diff textconv API
146 * if the textconv driver exists.
147 * Return 1 if the conversion succeeds, 0 otherwise.
148 */
e5fba602 149int textconv_object(const char *path,
90064710 150 unsigned mode,
e5fba602 151 const unsigned char *sha1,
e5450100 152 int sha1_valid,
e5fba602
CP
153 char **buf,
154 unsigned long *buf_size)
3b8a12e8
AB
155{
156 struct diff_filespec *df;
157 struct userdiff_driver *textconv;
158
159 df = alloc_filespec(path);
e5450100 160 fill_filespec(df, sha1, sha1_valid, mode);
3b8a12e8
AB
161 textconv = get_textconv(df);
162 if (!textconv) {
163 free_filespec(df);
164 return 0;
165 }
166
167 *buf_size = fill_textconv(textconv, df, buf);
168 free_filespec(df);
169 return 1;
170}
171
1732a1fd
JH
172/*
173 * Given an origin, prepare mmfile_t structure to be used by the
174 * diff machinery
175 */
3b8a12e8
AB
176static void fill_origin_blob(struct diff_options *opt,
177 struct origin *o, mmfile_t *file)
c2e525d9
JH
178{
179 if (!o->file.ptr) {
21666f1a 180 enum object_type type;
3b8a12e8
AB
181 unsigned long file_size;
182
c2e525d9 183 num_read_blob++;
3b8a12e8 184 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
e5450100 185 textconv_object(o->path, o->mode, o->blob_sha1, 1, &file->ptr, &file_size))
3b8a12e8
AB
186 ;
187 else
188 file->ptr = read_sha1_file(o->blob_sha1, &type, &file_size);
189 file->size = file_size;
190
ab43e495
JH
191 if (!file->ptr)
192 die("Cannot read blob %s for path %s",
193 sha1_to_hex(o->blob_sha1),
194 o->path);
c2e525d9
JH
195 o->file = *file;
196 }
197 else
198 *file = o->file;
c2e525d9
JH
199}
200
1732a1fd
JH
201/*
202 * Origin is refcounted and usually we keep the blob contents to be
203 * reused.
204 */
54a4c617
JH
205static inline struct origin *origin_incref(struct origin *o)
206{
207 if (o)
208 o->refcnt++;
209 return o;
210}
211
212static void origin_decref(struct origin *o)
213{
214 if (o && --o->refcnt <= 0) {
7e6ac6e4 215 struct origin *p, *l = NULL;
96e11709
JH
216 if (o->previous)
217 origin_decref(o->previous);
8e0f7003 218 free(o->file.ptr);
7e6ac6e4
DK
219 /* Should be present exactly once in commit chain */
220 for (p = o->commit->util; p; l = p, p = p->next) {
221 if (p == o) {
222 if (l)
223 l->next = p->next;
224 else
225 o->commit->util = p->next;
226 free(o);
227 return;
228 }
229 }
230 die("internal error in blame::origin_decref");
54a4c617
JH
231 }
232}
233
7c3c7962
JH
234static void drop_origin_blob(struct origin *o)
235{
236 if (o->file.ptr) {
237 free(o->file.ptr);
238 o->file.ptr = NULL;
239 }
240}
241
1732a1fd
JH
242/*
243 * Each group of lines is described by a blame_entry; it can be split
7e6ac6e4
DK
244 * as we pass blame to the parents. They are arranged in linked lists
245 * kept as `suspects' of some unprocessed origin, or entered (when the
246 * blame origin has been finalized) into the scoreboard structure.
247 * While the scoreboard structure is only sorted at the end of
248 * processing (according to final image line number), the lists
249 * attached to an origin are sorted by the target line number.
1732a1fd 250 */
cee7f245 251struct blame_entry {
cee7f245
JH
252 struct blame_entry *next;
253
254 /* the first line of this group in the final image;
255 * internally all line numbers are 0 based.
256 */
257 int lno;
258
259 /* how many lines this group has */
260 int num_lines;
261
262 /* the commit that introduced this group into the final image */
263 struct origin *suspect;
264
cee7f245
JH
265 /* the line number of the first line of this group in the
266 * suspect's file; internally all line numbers are 0 based.
267 */
268 int s_lno;
5ff62c30
JH
269
270 /* how significant this entry is -- cached to avoid
1732a1fd 271 * scanning the lines over and over.
5ff62c30
JH
272 */
273 unsigned score;
cee7f245
JH
274};
275
7e6ac6e4
DK
276/*
277 * Any merge of blames happens on lists of blames that arrived via
278 * different parents in a single suspect. In this case, we want to
279 * sort according to the suspect line numbers as opposed to the final
280 * image line numbers. The function body is somewhat longish because
281 * it avoids unnecessary writes.
282 */
283
284static struct blame_entry *blame_merge(struct blame_entry *list1,
285 struct blame_entry *list2)
286{
287 struct blame_entry *p1 = list1, *p2 = list2,
288 **tail = &list1;
289
290 if (!p1)
291 return p2;
292 if (!p2)
293 return p1;
294
295 if (p1->s_lno <= p2->s_lno) {
296 do {
297 tail = &p1->next;
298 if ((p1 = *tail) == NULL) {
299 *tail = p2;
300 return list1;
301 }
302 } while (p1->s_lno <= p2->s_lno);
303 }
304 for (;;) {
305 *tail = p2;
306 do {
307 tail = &p2->next;
308 if ((p2 = *tail) == NULL) {
309 *tail = p1;
310 return list1;
311 }
312 } while (p1->s_lno > p2->s_lno);
313 *tail = p1;
314 do {
315 tail = &p1->next;
316 if ((p1 = *tail) == NULL) {
317 *tail = p2;
318 return list1;
319 }
320 } while (p1->s_lno <= p2->s_lno);
321 }
322}
323
324static void *get_next_blame(const void *p)
325{
326 return ((struct blame_entry *)p)->next;
327}
328
329static void set_next_blame(void *p1, void *p2)
330{
331 ((struct blame_entry *)p1)->next = p2;
332}
333
334/*
335 * Final image line numbers are all different, so we don't need a
336 * three-way comparison here.
337 */
338
339static int compare_blame_final(const void *p1, const void *p2)
340{
341 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
342 ? 1 : -1;
343}
344
345static int compare_blame_suspect(const void *p1, const void *p2)
346{
347 const struct blame_entry *s1 = p1, *s2 = p2;
348 /*
349 * to allow for collating suspects, we sort according to the
350 * respective pointer value as the primary sorting criterion.
351 * The actual relation is pretty unimportant as long as it
352 * establishes a total order. Comparing as integers gives us
353 * that.
354 */
355 if (s1->suspect != s2->suspect)
356 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
357 if (s1->s_lno == s2->s_lno)
358 return 0;
359 return s1->s_lno > s2->s_lno ? 1 : -1;
360}
361
362static struct blame_entry *blame_sort(struct blame_entry *head,
363 int (*compare_fn)(const void *, const void *))
364{
365 return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn);
366}
367
368static int compare_commits_by_reverse_commit_date(const void *a,
369 const void *b,
370 void *c)
371{
372 return -compare_commits_by_commit_date(a, b, c);
373}
374
1732a1fd
JH
375/*
376 * The current state of the blame assignment.
377 */
cee7f245
JH
378struct scoreboard {
379 /* the final commit (i.e. where we started digging from) */
380 struct commit *final;
7e6ac6e4
DK
381 /* Priority queue for commits with unassigned blame records */
382 struct prio_queue commits;
85af7929 383 struct rev_info *revs;
cee7f245
JH
384 const char *path;
385
1732a1fd
JH
386 /*
387 * The contents in the final image.
388 * Used by many functions to obtain contents of the nth line,
389 * indexed with scoreboard.lineno[blame_entry.lno].
cee7f245
JH
390 */
391 const char *final_buf;
392 unsigned long final_buf_size;
393
394 /* linked list of blames */
395 struct blame_entry *ent;
396
612702e8 397 /* look-up a line in the final buffer */
cee7f245
JH
398 int num_lines;
399 int *lineno;
400};
401
54a4c617
JH
402static void sanity_check_refcnt(struct scoreboard *);
403
1732a1fd
JH
404/*
405 * If two blame entries that are next to each other came from
406 * contiguous lines in the same origin (i.e. <commit, path> pair),
407 * merge them together.
408 */
cee7f245
JH
409static void coalesce(struct scoreboard *sb)
410{
411 struct blame_entry *ent, *next;
412
413 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
0a88f08e 414 if (ent->suspect == next->suspect &&
cee7f245
JH
415 ent->s_lno + ent->num_lines == next->s_lno) {
416 ent->num_lines += next->num_lines;
417 ent->next = next->next;
54a4c617 418 origin_decref(next->suspect);
cee7f245 419 free(next);
46014766 420 ent->score = 0;
cee7f245
JH
421 next = ent; /* again */
422 }
423 }
54a4c617
JH
424
425 if (DEBUG) /* sanity */
426 sanity_check_refcnt(sb);
cee7f245
JH
427}
428
7e6ac6e4
DK
429/*
430 * Merge the given sorted list of blames into a preexisting origin.
431 * If there were no previous blames to that commit, it is entered into
432 * the commit priority queue of the score board.
433 */
434
435static void queue_blames(struct scoreboard *sb, struct origin *porigin,
436 struct blame_entry *sorted)
437{
438 if (porigin->suspects)
439 porigin->suspects = blame_merge(porigin->suspects, sorted);
440 else {
441 struct origin *o;
442 for (o = porigin->commit->util; o; o = o->next) {
443 if (o->suspects) {
444 porigin->suspects = sorted;
445 return;
446 }
447 }
448 porigin->suspects = sorted;
449 prio_queue_put(&sb->commits, porigin->commit);
450 }
451}
452
1732a1fd
JH
453/*
454 * Given a commit and a path in it, create a new origin structure.
455 * The callers that add blame to the scoreboard should use
456 * get_origin() to obtain shared, refcounted copy instead of calling
457 * this function directly.
458 */
854b97f6
JH
459static struct origin *make_origin(struct commit *commit, const char *path)
460{
461 struct origin *o;
462 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
463 o->commit = commit;
464 o->refcnt = 1;
7e6ac6e4
DK
465 o->next = commit->util;
466 commit->util = o;
854b97f6
JH
467 strcpy(o->path, path);
468 return o;
469}
470
1732a1fd
JH
471/*
472 * Locate an existing origin or create a new one.
7e6ac6e4 473 * This moves the origin to front position in the commit util list.
1732a1fd 474 */
f6c0e191
JH
475static struct origin *get_origin(struct scoreboard *sb,
476 struct commit *commit,
477 const char *path)
cee7f245 478{
7e6ac6e4 479 struct origin *o, *l;
cee7f245 480
7e6ac6e4
DK
481 for (o = commit->util, l = NULL; o; l = o, o = o->next) {
482 if (!strcmp(o->path, path)) {
483 /* bump to front */
484 if (l) {
485 l->next = o->next;
486 o->next = commit->util;
487 commit->util = o;
488 }
489 return origin_incref(o);
490 }
cee7f245 491 }
854b97f6 492 return make_origin(commit, path);
cee7f245
JH
493}
494
1732a1fd
JH
495/*
496 * Fill the blob_sha1 field of an origin if it hasn't, so that later
497 * call to fill_origin_blob() can use it to locate the data. blob_sha1
498 * for an origin is also used to pass the blame for the entire file to
499 * the parent to detect the case where a child's blob is identical to
500 * that of its parent's.
90064710
KS
501 *
502 * This also fills origin->mode for corresponding tree path.
1732a1fd 503 */
90064710 504static int fill_blob_sha1_and_mode(struct origin *origin)
f6c0e191 505{
f6c0e191
JH
506 if (!is_null_sha1(origin->blob_sha1))
507 return 0;
508 if (get_tree_entry(origin->commit->object.sha1,
509 origin->path,
90064710 510 origin->blob_sha1, &origin->mode))
f6c0e191 511 goto error_out;
21666f1a 512 if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
f6c0e191
JH
513 goto error_out;
514 return 0;
515 error_out:
516 hashclr(origin->blob_sha1);
90064710 517 origin->mode = S_IFINVALID;
f6c0e191
JH
518 return -1;
519}
520
1732a1fd
JH
521/*
522 * We have an origin -- check if the same path exists in the
523 * parent and return an origin structure to represent it.
524 */
f6c0e191 525static struct origin *find_origin(struct scoreboard *sb,
cee7f245
JH
526 struct commit *parent,
527 struct origin *origin)
528{
7e6ac6e4 529 struct origin *porigin;
cee7f245 530 struct diff_options diff_opts;
f6c0e191
JH
531 const char *paths[2];
532
7e6ac6e4
DK
533 /* First check any existing origins */
534 for (porigin = parent->util; porigin; porigin = porigin->next)
535 if (!strcmp(porigin->path, origin->path)) {
1732a1fd
JH
536 /*
537 * The same path between origin and its parent
538 * without renaming -- the most common case.
539 */
7e6ac6e4 540 return origin_incref (porigin);
854b97f6 541 }
0d981c67 542
f6c0e191
JH
543 /* See if the origin->path is different between parent
544 * and origin first. Most of the time they are the
545 * same and diff-tree is fairly efficient about this.
546 */
547 diff_setup(&diff_opts);
8f67f8ae 548 DIFF_OPT_SET(&diff_opts, RECURSIVE);
f6c0e191
JH
549 diff_opts.detect_rename = 0;
550 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
551 paths[0] = origin->path;
552 paths[1] = NULL;
553
4a2d5ae2
NTND
554 parse_pathspec(&diff_opts.pathspec,
555 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
556 PATHSPEC_LITERAL_PATH, "", paths);
28452655 557 diff_setup_done(&diff_opts);
1cfe7733
JH
558
559 if (is_null_sha1(origin->commit->object.sha1))
560 do_diff_cache(parent->tree->object.sha1, &diff_opts);
561 else
562 diff_tree_sha1(parent->tree->object.sha1,
563 origin->commit->tree->object.sha1,
564 "", &diff_opts);
f6c0e191
JH
565 diffcore_std(&diff_opts);
566
f6c0e191
JH
567 if (!diff_queued_diff.nr) {
568 /* The path is the same as parent */
569 porigin = get_origin(sb, parent, origin->path);
570 hashcpy(porigin->blob_sha1, origin->blob_sha1);
90064710 571 porigin->mode = origin->mode;
a9b2d424
JH
572 } else {
573 /*
574 * Since origin->path is a pathspec, if the parent
575 * commit had it as a directory, we will see a whole
576 * bunch of deletion of files in the directory that we
577 * do not care about.
578 */
579 int i;
580 struct diff_filepair *p = NULL;
581 for (i = 0; i < diff_queued_diff.nr; i++) {
582 const char *name;
583 p = diff_queued_diff.queue[i];
584 name = p->one->path ? p->one->path : p->two->path;
585 if (!strcmp(name, origin->path))
586 break;
587 }
588 if (!p)
589 die("internal error in blame::find_origin");
f6c0e191
JH
590 switch (p->status) {
591 default:
acca687f 592 die("internal error in blame::find_origin (%c)",
f6c0e191
JH
593 p->status);
594 case 'M':
595 porigin = get_origin(sb, parent, origin->path);
596 hashcpy(porigin->blob_sha1, p->one->sha1);
90064710 597 porigin->mode = p->one->mode;
f6c0e191
JH
598 break;
599 case 'A':
600 case 'T':
601 /* Did not exist in parent, or type changed */
602 break;
603 }
604 }
605 diff_flush(&diff_opts);
bd1928df 606 free_pathspec(&diff_opts.pathspec);
f69e743d
JH
607 return porigin;
608}
f6c0e191 609
1732a1fd
JH
610/*
611 * We have an origin -- find the path that corresponds to it in its
612 * parent and return an origin structure to represent it.
613 */
f69e743d
JH
614static struct origin *find_rename(struct scoreboard *sb,
615 struct commit *parent,
616 struct origin *origin)
617{
618 struct origin *porigin = NULL;
619 struct diff_options diff_opts;
620 int i;
cee7f245
JH
621
622 diff_setup(&diff_opts);
8f67f8ae 623 DIFF_OPT_SET(&diff_opts, RECURSIVE);
cee7f245
JH
624 diff_opts.detect_rename = DIFF_DETECT_RENAME;
625 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
2f3f8b21 626 diff_opts.single_follow = origin->path;
28452655 627 diff_setup_done(&diff_opts);
1cfe7733
JH
628
629 if (is_null_sha1(origin->commit->object.sha1))
630 do_diff_cache(parent->tree->object.sha1, &diff_opts);
631 else
632 diff_tree_sha1(parent->tree->object.sha1,
633 origin->commit->tree->object.sha1,
634 "", &diff_opts);
cee7f245
JH
635 diffcore_std(&diff_opts);
636
637 for (i = 0; i < diff_queued_diff.nr; i++) {
638 struct diff_filepair *p = diff_queued_diff.queue[i];
612702e8 639 if ((p->status == 'R' || p->status == 'C') &&
f6c0e191
JH
640 !strcmp(p->two->path, origin->path)) {
641 porigin = get_origin(sb, parent, p->one->path);
642 hashcpy(porigin->blob_sha1, p->one->sha1);
90064710 643 porigin->mode = p->one->mode;
cee7f245
JH
644 break;
645 }
646 }
647 diff_flush(&diff_opts);
bd1928df 648 free_pathspec(&diff_opts.pathspec);
cee7f245
JH
649 return porigin;
650}
651
1732a1fd 652/*
7e6ac6e4 653 * Append a new blame entry to a given output queue.
1732a1fd 654 */
7e6ac6e4 655static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e)
cee7f245 656{
54a4c617
JH
657 origin_incref(e->suspect);
658
7e6ac6e4
DK
659 e->next = **queue;
660 **queue = e;
661 *queue = &e->next;
cee7f245
JH
662}
663
1732a1fd
JH
664/*
665 * src typically is on-stack; we want to copy the information in it to
7e6ac6e4
DK
666 * a malloced blame_entry that gets added to the given queue. The
667 * origin of dst loses a refcnt.
1732a1fd 668 */
7e6ac6e4
DK
669static void dup_entry(struct blame_entry ***queue,
670 struct blame_entry *dst, struct blame_entry *src)
cee7f245 671{
54a4c617
JH
672 origin_incref(src->suspect);
673 origin_decref(dst->suspect);
cee7f245 674 memcpy(dst, src, sizeof(*src));
7e6ac6e4
DK
675 dst->next = **queue;
676 **queue = dst;
677 *queue = &dst->next;
cee7f245
JH
678}
679
25ed3412 680static const char *nth_line(struct scoreboard *sb, long lno)
cee7f245
JH
681{
682 return sb->final_buf + sb->lineno[lno];
683}
684
25ed3412
BY
685static const char *nth_line_cb(void *data, long lno)
686{
687 return nth_line((struct scoreboard *)data, lno);
688}
689
1732a1fd
JH
690/*
691 * It is known that lines between tlno to same came from parent, and e
692 * has an overlap with that range. it also is known that parent's
693 * line plno corresponds to e's line tlno.
694 *
695 * <---- e ----->
696 * <------>
697 * <------------>
698 * <------------>
699 * <------------------>
700 *
701 * Split e into potentially three parts; before this chunk, the chunk
702 * to be blamed for the parent, and after that portion.
703 */
54a4c617 704static void split_overlap(struct blame_entry *split,
cee7f245
JH
705 struct blame_entry *e,
706 int tlno, int plno, int same,
707 struct origin *parent)
708{
cee7f245
JH
709 int chunk_end_lno;
710 memset(split, 0, sizeof(struct blame_entry [3]));
711
712 if (e->s_lno < tlno) {
713 /* there is a pre-chunk part not blamed on parent */
54a4c617 714 split[0].suspect = origin_incref(e->suspect);
cee7f245
JH
715 split[0].lno = e->lno;
716 split[0].s_lno = e->s_lno;
717 split[0].num_lines = tlno - e->s_lno;
718 split[1].lno = e->lno + tlno - e->s_lno;
719 split[1].s_lno = plno;
720 }
721 else {
722 split[1].lno = e->lno;
723 split[1].s_lno = plno + (e->s_lno - tlno);
724 }
725
726 if (same < e->s_lno + e->num_lines) {
727 /* there is a post-chunk part not blamed on parent */
54a4c617 728 split[2].suspect = origin_incref(e->suspect);
cee7f245
JH
729 split[2].lno = e->lno + (same - e->s_lno);
730 split[2].s_lno = e->s_lno + (same - e->s_lno);
731 split[2].num_lines = e->s_lno + e->num_lines - same;
732 chunk_end_lno = split[2].lno;
733 }
734 else
735 chunk_end_lno = e->lno + e->num_lines;
736 split[1].num_lines = chunk_end_lno - split[1].lno;
737
1732a1fd
JH
738 /*
739 * if it turns out there is nothing to blame the parent for,
740 * forget about the splitting. !split[1].suspect signals this.
741 */
cee7f245
JH
742 if (split[1].num_lines < 1)
743 return;
54a4c617 744 split[1].suspect = origin_incref(parent);
cee7f245
JH
745}
746
1732a1fd
JH
747/*
748 * split_overlap() divided an existing blame e into up to three parts
7e6ac6e4 749 * in split. Any assigned blame is moved to queue to
1732a1fd
JH
750 * reflect the split.
751 */
7e6ac6e4
DK
752static void split_blame(struct blame_entry ***blamed,
753 struct blame_entry ***unblamed,
54a4c617 754 struct blame_entry *split,
cee7f245
JH
755 struct blame_entry *e)
756{
757 struct blame_entry *new_entry;
758
759 if (split[0].suspect && split[2].suspect) {
1732a1fd 760 /* The first part (reuse storage for the existing entry e) */
7e6ac6e4 761 dup_entry(unblamed, e, &split[0]);
cee7f245 762
1732a1fd 763 /* The last part -- me */
cee7f245
JH
764 new_entry = xmalloc(sizeof(*new_entry));
765 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
7e6ac6e4 766 add_blame_entry(unblamed, new_entry);
cee7f245 767
1732a1fd 768 /* ... and the middle part -- parent */
cee7f245
JH
769 new_entry = xmalloc(sizeof(*new_entry));
770 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
7e6ac6e4 771 add_blame_entry(blamed, new_entry);
cee7f245
JH
772 }
773 else if (!split[0].suspect && !split[2].suspect)
1732a1fd
JH
774 /*
775 * The parent covers the entire area; reuse storage for
776 * e and replace it with the parent.
777 */
7e6ac6e4 778 dup_entry(blamed, e, &split[1]);
cee7f245 779 else if (split[0].suspect) {
1732a1fd 780 /* me and then parent */
7e6ac6e4 781 dup_entry(unblamed, e, &split[0]);
cee7f245
JH
782
783 new_entry = xmalloc(sizeof(*new_entry));
784 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
7e6ac6e4 785 add_blame_entry(blamed, new_entry);
cee7f245
JH
786 }
787 else {
1732a1fd 788 /* parent and then me */
7e6ac6e4 789 dup_entry(blamed, e, &split[1]);
cee7f245
JH
790
791 new_entry = xmalloc(sizeof(*new_entry));
792 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
7e6ac6e4 793 add_blame_entry(unblamed, new_entry);
cee7f245
JH
794 }
795}
796
1732a1fd
JH
797/*
798 * After splitting the blame, the origins used by the
799 * on-stack blame_entry should lose one refcnt each.
800 */
54a4c617
JH
801static void decref_split(struct blame_entry *split)
802{
803 int i;
804
805 for (i = 0; i < 3; i++)
806 origin_decref(split[i].suspect);
807}
808
1732a1fd 809/*
7e6ac6e4
DK
810 * reverse_blame reverses the list given in head, appending tail.
811 * That allows us to build lists in reverse order, then reverse them
812 * afterwards. This can be faster than building the list in proper
813 * order right away. The reason is that building in proper order
814 * requires writing a link in the _previous_ element, while building
815 * in reverse order just requires placing the list head into the
816 * _current_ element.
1732a1fd 817 */
cee7f245 818
7e6ac6e4
DK
819static struct blame_entry *reverse_blame(struct blame_entry *head,
820 struct blame_entry *tail)
cee7f245 821{
7e6ac6e4
DK
822 while (head) {
823 struct blame_entry *next = head->next;
824 head->next = tail;
825 tail = head;
826 head = next;
cee7f245 827 }
7e6ac6e4 828 return tail;
cee7f245
JH
829}
830
1732a1fd
JH
831/*
832 * Process one hunk from the patch between the current suspect for
7e6ac6e4
DK
833 * blame_entry e and its parent. This first blames any unfinished
834 * entries before the chunk (which is where target and parent start
835 * differing) on the parent, and then splits blame entries at the
836 * start and at the end of the difference region. Since use of -M and
837 * -C options may lead to overlapping/duplicate source line number
838 * ranges, all we can rely on from sorting/merging is the order of the
839 * first suspect line number.
1732a1fd 840 */
7e6ac6e4
DK
841static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
842 int tlno, int offset, int same,
843 struct origin *parent)
cee7f245 844{
7e6ac6e4
DK
845 struct blame_entry *e = **srcq;
846 struct blame_entry *samep = NULL, *diffp = NULL;
cee7f245 847
7e6ac6e4
DK
848 while (e && e->s_lno < tlno) {
849 struct blame_entry *next = e->next;
850 /*
851 * current record starts before differing portion. If
852 * it reaches into it, we need to split it up and
853 * examine the second part separately.
854 */
855 if (e->s_lno + e->num_lines > tlno) {
856 /* Move second half to a new record */
857 int len = tlno - e->s_lno;
858 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
859 n->suspect = e->suspect;
860 n->lno = e->lno + len;
861 n->s_lno = e->s_lno + len;
862 n->num_lines = e->num_lines - len;
863 e->num_lines = len;
864 e->score = 0;
865 /* Push new record to diffp */
866 n->next = diffp;
867 diffp = n;
868 } else
869 origin_decref(e->suspect);
870 /* Pass blame for everything before the differing
871 * chunk to the parent */
872 e->suspect = origin_incref(parent);
873 e->s_lno += offset;
874 e->next = samep;
875 samep = e;
876 e = next;
877 }
878 /*
879 * As we don't know how much of a common stretch after this
880 * diff will occur, the currently blamed parts are all that we
881 * can assign to the parent for now.
882 */
883
884 if (samep) {
885 **dstq = reverse_blame(samep, **dstq);
886 *dstq = &samep->next;
cee7f245 887 }
7e6ac6e4
DK
888 /*
889 * Prepend the split off portions: everything after e starts
890 * after the blameable portion.
891 */
892 e = reverse_blame(diffp, e);
893
894 /*
895 * Now retain records on the target while parts are different
896 * from the parent.
897 */
898 samep = NULL;
899 diffp = NULL;
900 while (e && e->s_lno < same) {
901 struct blame_entry *next = e->next;
902
903 /*
904 * If current record extends into sameness, need to split.
905 */
906 if (e->s_lno + e->num_lines > same) {
907 /*
908 * Move second half to a new record to be
909 * processed by later chunks
910 */
911 int len = same - e->s_lno;
912 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
913 n->suspect = origin_incref(e->suspect);
914 n->lno = e->lno + len;
915 n->s_lno = e->s_lno + len;
916 n->num_lines = e->num_lines - len;
917 e->num_lines = len;
918 e->score = 0;
919 /* Push new record to samep */
920 n->next = samep;
921 samep = n;
922 }
923 e->next = diffp;
924 diffp = e;
925 e = next;
926 }
927 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
928 /* Move across elements that are in the unblamable portion */
929 if (diffp)
930 *srcq = &diffp->next;
cee7f245
JH
931}
932
cdcab133 933struct blame_chunk_cb_data {
cdcab133 934 struct origin *parent;
7e6ac6e4
DK
935 long offset;
936 struct blame_entry **dstq;
937 struct blame_entry **srcq;
cdcab133
RS
938};
939
7e6ac6e4 940/* diff chunks are from parent to target */
0af596c6
RS
941static int blame_chunk_cb(long start_a, long count_a,
942 long start_b, long count_b, void *data)
cdcab133
RS
943{
944 struct blame_chunk_cb_data *d = data;
7e6ac6e4
DK
945 if (start_a - start_b != d->offset)
946 die("internal error in blame::blame_chunk_cb");
947 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
948 start_b + count_b, d->parent);
949 d->offset = start_a + count_a - (start_b + count_b);
0af596c6 950 return 0;
cdcab133
RS
951}
952
1732a1fd
JH
953/*
954 * We are looking at the origin 'target' and aiming to pass blame
955 * for the lines it is suspected to its parent. Run diff to find
956 * which lines came from parent and pass blame for them.
957 */
7e6ac6e4
DK
958static void pass_blame_to_parent(struct scoreboard *sb,
959 struct origin *target,
960 struct origin *parent)
cee7f245 961{
7c4ed8c8 962 mmfile_t file_p, file_o;
66dbfd55 963 struct blame_chunk_cb_data d;
7e6ac6e4 964 struct blame_entry *newdest = NULL;
0af596c6 965
7e6ac6e4
DK
966 if (!target->suspects)
967 return; /* nothing remains for this target */
968
969 d.parent = parent;
970 d.offset = 0;
971 d.dstq = &newdest; d.srcq = &target->suspects;
cee7f245 972
3b8a12e8
AB
973 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
974 fill_origin_blob(&sb->revs->diffopt, target, &file_o);
7c4ed8c8 975 num_get_patch++;
cee7f245 976
4b4132fd 977 diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d);
7e6ac6e4
DK
978 /* The rest are the same as the parent */
979 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
980 *d.dstq = NULL;
981 queue_blames(sb, parent, newdest);
cee7f245 982
7e6ac6e4 983 return;
cee7f245
JH
984}
985
1732a1fd
JH
986/*
987 * The lines in blame_entry after splitting blames many times can become
988 * very small and trivial, and at some point it becomes pointless to
989 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
990 * ordinary C program, and it is not worth to say it was copied from
991 * totally unrelated file in the parent.
992 *
993 * Compute how trivial the lines in the blame_entry are.
994 */
5ff62c30
JH
995static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
996{
997 unsigned score;
998 const char *cp, *ep;
999
1000 if (e->score)
1001 return e->score;
1002
612702e8 1003 score = 1;
5ff62c30
JH
1004 cp = nth_line(sb, e->lno);
1005 ep = nth_line(sb, e->lno + e->num_lines);
1006 while (cp < ep) {
1007 unsigned ch = *((unsigned char *)cp);
1008 if (isalnum(ch))
1009 score++;
1010 cp++;
1011 }
1012 e->score = score;
1013 return score;
1014}
1015
1732a1fd
JH
1016/*
1017 * best_so_far[] and this[] are both a split of an existing blame_entry
1018 * that passes blame to the parent. Maintain best_so_far the best split
1019 * so far, by comparing this and best_so_far and copying this into
1020 * bst_so_far as needed.
1021 */
5ff62c30 1022static void copy_split_if_better(struct scoreboard *sb,
54a4c617
JH
1023 struct blame_entry *best_so_far,
1024 struct blame_entry *this)
d24bba80 1025{
54a4c617
JH
1026 int i;
1027
d24bba80
JH
1028 if (!this[1].suspect)
1029 return;
5ff62c30
JH
1030 if (best_so_far[1].suspect) {
1031 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
1032 return;
1033 }
54a4c617
JH
1034
1035 for (i = 0; i < 3; i++)
1036 origin_incref(this[i].suspect);
1037 decref_split(best_so_far);
d24bba80
JH
1038 memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
1039}
1040
dd166aa8
JH
1041/*
1042 * We are looking at a part of the final image represented by
1043 * ent (tlno and same are offset by ent->s_lno).
1044 * tlno is where we are looking at in the final image.
1045 * up to (but not including) same match preimage.
1046 * plno is where we are looking at in the preimage.
1047 *
1048 * <-------------- final image ---------------------->
1049 * <------ent------>
1050 * ^tlno ^same
1051 * <---------preimage----->
1052 * ^plno
1053 *
1054 * All line numbers are 0-based.
1055 */
1056static void handle_split(struct scoreboard *sb,
1057 struct blame_entry *ent,
1058 int tlno, int plno, int same,
1059 struct origin *parent,
1060 struct blame_entry *split)
1061{
1062 if (ent->num_lines <= tlno)
1063 return;
1064 if (tlno < same) {
1065 struct blame_entry this[3];
1066 tlno += ent->s_lno;
1067 same += ent->s_lno;
1068 split_overlap(this, ent, tlno, plno, same, parent);
1069 copy_split_if_better(sb, split, this);
1070 decref_split(this);
1071 }
1072}
1073
cdcab133
RS
1074struct handle_split_cb_data {
1075 struct scoreboard *sb;
1076 struct blame_entry *ent;
1077 struct origin *parent;
1078 struct blame_entry *split;
1079 long plno;
1080 long tlno;
1081};
1082
5d23ec76
RS
1083static int handle_split_cb(long start_a, long count_a,
1084 long start_b, long count_b, void *data)
cdcab133
RS
1085{
1086 struct handle_split_cb_data *d = data;
5d23ec76
RS
1087 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1088 d->split);
1089 d->plno = start_a + count_a;
1090 d->tlno = start_b + count_b;
1091 return 0;
cdcab133
RS
1092}
1093
1732a1fd
JH
1094/*
1095 * Find the lines from parent that are the same as ent so that
1096 * we can pass blames to it. file_p has the blob contents for
1097 * the parent.
1098 */
d24bba80
JH
1099static void find_copy_in_blob(struct scoreboard *sb,
1100 struct blame_entry *ent,
1101 struct origin *parent,
54a4c617 1102 struct blame_entry *split,
d24bba80
JH
1103 mmfile_t *file_p)
1104{
1105 const char *cp;
d24bba80 1106 mmfile_t file_o;
66dbfd55 1107 struct handle_split_cb_data d;
5d23ec76 1108
66dbfd55
GV
1109 memset(&d, 0, sizeof(d));
1110 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1732a1fd
JH
1111 /*
1112 * Prepare mmfile that contains only the lines in ent.
1113 */
d24bba80 1114 cp = nth_line(sb, ent->lno);
4b25d091 1115 file_o.ptr = (char *) cp;
3ee8944f 1116 file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
d24bba80 1117
dd166aa8
JH
1118 /*
1119 * file_o is a part of final image we are annotating.
1120 * file_p partially may match that image.
1121 */
d24bba80 1122 memset(split, 0, sizeof(struct blame_entry [3]));
4b4132fd 1123 diff_hunks(file_p, &file_o, 1, handle_split_cb, &d);
dd166aa8 1124 /* remainder, if any, all match the preimage */
cdcab133 1125 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
d24bba80
JH
1126}
1127
7e6ac6e4
DK
1128/* Move all blame entries from list *source that have a score smaller
1129 * than score_min to the front of list *small.
1130 * Returns a pointer to the link pointing to the old head of the small list.
1131 */
1132
1133static struct blame_entry **filter_small(struct scoreboard *sb,
1134 struct blame_entry **small,
1135 struct blame_entry **source,
1136 unsigned score_min)
1137{
1138 struct blame_entry *p = *source;
1139 struct blame_entry *oldsmall = *small;
1140 while (p) {
1141 if (ent_score(sb, p) <= score_min) {
1142 *small = p;
1143 small = &p->next;
1144 p = *small;
1145 } else {
1146 *source = p;
1147 source = &p->next;
1148 p = *source;
1149 }
1150 }
1151 *small = oldsmall;
1152 *source = NULL;
1153 return small;
1154}
1155
1732a1fd
JH
1156/*
1157 * See if lines currently target is suspected for can be attributed to
1158 * parent.
1159 */
7e6ac6e4
DK
1160static void find_move_in_parent(struct scoreboard *sb,
1161 struct blame_entry ***blamed,
1162 struct blame_entry **toosmall,
1163 struct origin *target,
1164 struct origin *parent)
d24bba80 1165{
46014766 1166 struct blame_entry *e, split[3];
7e6ac6e4
DK
1167 struct blame_entry *unblamed = target->suspects;
1168 struct blame_entry *leftover = NULL;
d24bba80 1169 mmfile_t file_p;
d24bba80 1170
7e6ac6e4
DK
1171 if (!unblamed)
1172 return; /* nothing remains for this target */
d24bba80 1173
3b8a12e8 1174 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
c2e525d9 1175 if (!file_p.ptr)
7e6ac6e4 1176 return;
d24bba80 1177
7e6ac6e4
DK
1178 /* At each iteration, unblamed has a NULL-terminated list of
1179 * entries that have not yet been tested for blame. leftover
1180 * contains the reversed list of entries that have been tested
1181 * without being assignable to the parent.
1182 */
1183 do {
1184 struct blame_entry **unblamedtail = &unblamed;
1185 struct blame_entry *next;
1186 for (e = unblamed; e; e = next) {
1187 next = e->next;
650e2f67
JH
1188 find_copy_in_blob(sb, e, parent, split, &file_p);
1189 if (split[1].suspect &&
1190 blame_move_score < ent_score(sb, &split[1])) {
7e6ac6e4
DK
1191 split_blame(blamed, &unblamedtail, split, e);
1192 } else {
1193 e->next = leftover;
1194 leftover = e;
650e2f67
JH
1195 }
1196 decref_split(split);
1197 }
7e6ac6e4
DK
1198 *unblamedtail = NULL;
1199 toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
1200 } while (unblamed);
1201 target->suspects = reverse_blame(leftover, NULL);
d24bba80
JH
1202}
1203
33494784
JH
1204struct blame_list {
1205 struct blame_entry *ent;
1206 struct blame_entry split[3];
1207};
1208
1732a1fd
JH
1209/*
1210 * Count the number of entries the target is suspected for,
1211 * and prepare a list of entry and the best split.
1212 */
7e6ac6e4 1213static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
33494784
JH
1214 int *num_ents_p)
1215{
1216 struct blame_entry *e;
1217 int num_ents, i;
1218 struct blame_list *blame_list = NULL;
1219
7e6ac6e4
DK
1220 for (e = unblamed, num_ents = 0; e; e = e->next)
1221 num_ents++;
33494784
JH
1222 if (num_ents) {
1223 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
7e6ac6e4
DK
1224 for (e = unblamed, i = 0; e; e = e->next)
1225 blame_list[i++].ent = e;
33494784
JH
1226 }
1227 *num_ents_p = num_ents;
1228 return blame_list;
1229}
1230
1732a1fd
JH
1231/*
1232 * For lines target is suspected for, see if we can find code movement
1233 * across file boundary from the parent commit. porigin is the path
1234 * in the parent we already tried.
1235 */
7e6ac6e4
DK
1236static void find_copy_in_parent(struct scoreboard *sb,
1237 struct blame_entry ***blamed,
1238 struct blame_entry **toosmall,
1239 struct origin *target,
1240 struct commit *parent,
1241 struct origin *porigin,
1242 int opt)
18abd745
JH
1243{
1244 struct diff_options diff_opts;
aec8fa1f 1245 int i, j;
33494784 1246 struct blame_list *blame_list;
aec8fa1f 1247 int num_ents;
7e6ac6e4
DK
1248 struct blame_entry *unblamed = target->suspects;
1249 struct blame_entry *leftover = NULL;
18abd745 1250
7e6ac6e4
DK
1251 if (!unblamed)
1252 return; /* nothing remains for this target */
18abd745
JH
1253
1254 diff_setup(&diff_opts);
8f67f8ae 1255 DIFF_OPT_SET(&diff_opts, RECURSIVE);
18abd745
JH
1256 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1257
28452655 1258 diff_setup_done(&diff_opts);
33494784
JH
1259
1260 /* Try "find copies harder" on new path if requested;
1261 * we do not want to use diffcore_rename() actually to
1262 * match things up; find_copies_harder is set only to
1263 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1264 * and this code needs to be after diff_setup_done(), which
1265 * usually makes find-copies-harder imply copy detection.
1266 */
c63777c0
JH
1267 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1268 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1269 && (!porigin || strcmp(target->path, porigin->path))))
8f67f8ae 1270 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
33494784 1271
1cfe7733
JH
1272 if (is_null_sha1(target->commit->object.sha1))
1273 do_diff_cache(parent->tree->object.sha1, &diff_opts);
1274 else
1275 diff_tree_sha1(parent->tree->object.sha1,
1276 target->commit->tree->object.sha1,
1277 "", &diff_opts);
18abd745 1278
8f67f8ae 1279 if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
33494784 1280 diffcore_std(&diff_opts);
18abd745 1281
7e6ac6e4
DK
1282 do {
1283 struct blame_entry **unblamedtail = &unblamed;
1284 blame_list = setup_blame_list(unblamed, &num_ents);
33494784
JH
1285
1286 for (i = 0; i < diff_queued_diff.nr; i++) {
1287 struct diff_filepair *p = diff_queued_diff.queue[i];
1288 struct origin *norigin;
1289 mmfile_t file_p;
33494784
JH
1290 struct blame_entry this[3];
1291
1292 if (!DIFF_FILE_VALID(p->one))
1293 continue; /* does not exist in parent */
5209ac4d
AG
1294 if (S_ISGITLINK(p->one->mode))
1295 continue; /* ignore git links */
33494784
JH
1296 if (porigin && !strcmp(p->one->path, porigin->path))
1297 /* find_move already dealt with this path */
1298 continue;
1299
1300 norigin = get_origin(sb, parent, p->one->path);
1301 hashcpy(norigin->blob_sha1, p->one->sha1);
90064710 1302 norigin->mode = p->one->mode;
3b8a12e8 1303 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
33494784
JH
1304 if (!file_p.ptr)
1305 continue;
1306
1307 for (j = 0; j < num_ents; j++) {
1308 find_copy_in_blob(sb, blame_list[j].ent,
1309 norigin, this, &file_p);
1310 copy_split_if_better(sb, blame_list[j].split,
1311 this);
1312 decref_split(this);
1313 }
33494784
JH
1314 origin_decref(norigin);
1315 }
18abd745 1316
aec8fa1f 1317 for (j = 0; j < num_ents; j++) {
33494784
JH
1318 struct blame_entry *split = blame_list[j].split;
1319 if (split[1].suspect &&
1320 blame_copy_score < ent_score(sb, &split[1])) {
7e6ac6e4
DK
1321 split_blame(blamed, &unblamedtail, split,
1322 blame_list[j].ent);
1323 } else {
1324 blame_list[j].ent->next = leftover;
1325 leftover = blame_list[j].ent;
33494784
JH
1326 }
1327 decref_split(split);
18abd745 1328 }
33494784 1329 free(blame_list);
7e6ac6e4
DK
1330 *unblamedtail = NULL;
1331 toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
1332 } while (unblamed);
1333 target->suspects = reverse_blame(leftover, NULL);
33494784 1334 diff_flush(&diff_opts);
bd1928df 1335 free_pathspec(&diff_opts.pathspec);
18abd745
JH
1336}
1337
1732a1fd
JH
1338/*
1339 * The blobs of origin and porigin exactly match, so everything
c2e525d9
JH
1340 * origin is suspected for can be blamed on the parent.
1341 */
1342static void pass_whole_blame(struct scoreboard *sb,
1343 struct origin *origin, struct origin *porigin)
1344{
7e6ac6e4 1345 struct blame_entry *e, *suspects;
c2e525d9
JH
1346
1347 if (!porigin->file.ptr && origin->file.ptr) {
1348 /* Steal its file */
1349 porigin->file = origin->file;
1350 origin->file.ptr = NULL;
1351 }
7e6ac6e4
DK
1352 suspects = origin->suspects;
1353 origin->suspects = NULL;
1354 for (e = suspects; e; e = e->next) {
c2e525d9
JH
1355 origin_incref(porigin);
1356 origin_decref(e->suspect);
1357 e->suspect = porigin;
1358 }
7e6ac6e4 1359 queue_blames(sb, porigin, suspects);
c2e525d9
JH
1360}
1361
69264f46
JH
1362/*
1363 * We pass blame from the current commit to its parents. We keep saying
1364 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1365 * exonerate ourselves.
1366 */
85af7929 1367static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit)
69264f46 1368{
85af7929
JH
1369 if (!reverse)
1370 return commit->parents;
1371 return lookup_decoration(&revs->children, &commit->object);
69264f46
JH
1372}
1373
85af7929 1374static int num_scapegoats(struct rev_info *revs, struct commit *commit)
69264f46 1375{
85af7929 1376 struct commit_list *l = first_scapegoat(revs, commit);
4bbaa1eb 1377 return commit_list_count(l);
69264f46
JH
1378}
1379
7e6ac6e4
DK
1380/* Distribute collected unsorted blames to the respected sorted lists
1381 * in the various origins.
1382 */
1383static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
1384{
1385 blamed = blame_sort(blamed, compare_blame_suspect);
1386 while (blamed)
1387 {
1388 struct origin *porigin = blamed->suspect;
1389 struct blame_entry *suspects = NULL;
1390 do {
1391 struct blame_entry *next = blamed->next;
1392 blamed->next = suspects;
1393 suspects = blamed;
1394 blamed = next;
1395 } while (blamed && blamed->suspect == porigin);
1396 suspects = reverse_blame(suspects, NULL);
1397 queue_blames(sb, porigin, suspects);
1398 }
1399}
1400
69264f46 1401#define MAXSG 16
cee7f245 1402
d24bba80 1403static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
cee7f245 1404{
85af7929 1405 struct rev_info *revs = sb->revs;
69264f46 1406 int i, pass, num_sg;
cee7f245 1407 struct commit *commit = origin->commit;
69264f46
JH
1408 struct commit_list *sg;
1409 struct origin *sg_buf[MAXSG];
1410 struct origin *porigin, **sg_origin = sg_buf;
7e6ac6e4
DK
1411 struct blame_entry *toosmall = NULL;
1412 struct blame_entry *blames, **blametail = &blames;
69264f46 1413
85af7929 1414 num_sg = num_scapegoats(revs, commit);
69264f46
JH
1415 if (!num_sg)
1416 goto finish;
1417 else if (num_sg < ARRAY_SIZE(sg_buf))
1418 memset(sg_buf, 0, sizeof(sg_buf));
1419 else
1420 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
cee7f245 1421
69264f46
JH
1422 /*
1423 * The first pass looks for unrenamed path to optimize for
f69e743d
JH
1424 * common cases, then we look for renames in the second pass.
1425 */
3d1aa566 1426 for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
f69e743d
JH
1427 struct origin *(*find)(struct scoreboard *,
1428 struct commit *, struct origin *);
1429 find = pass ? find_rename : find_origin;
1430
85af7929 1431 for (i = 0, sg = first_scapegoat(revs, commit);
69264f46
JH
1432 i < num_sg && sg;
1433 sg = sg->next, i++) {
1434 struct commit *p = sg->item;
0421d9f8 1435 int j, same;
f69e743d 1436
69264f46 1437 if (sg_origin[i])
f69e743d
JH
1438 continue;
1439 if (parse_commit(p))
1440 continue;
0d981c67 1441 porigin = find(sb, p, origin);
f69e743d
JH
1442 if (!porigin)
1443 continue;
1444 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
c2e525d9 1445 pass_whole_blame(sb, origin, porigin);
f69e743d
JH
1446 origin_decref(porigin);
1447 goto finish;
1448 }
0421d9f8 1449 for (j = same = 0; j < i; j++)
69264f46
JH
1450 if (sg_origin[j] &&
1451 !hashcmp(sg_origin[j]->blob_sha1,
0421d9f8
JH
1452 porigin->blob_sha1)) {
1453 same = 1;
1454 break;
1455 }
1456 if (!same)
69264f46 1457 sg_origin[i] = porigin;
0421d9f8
JH
1458 else
1459 origin_decref(porigin);
cee7f245 1460 }
cee7f245
JH
1461 }
1462
c2e525d9 1463 num_commits++;
85af7929 1464 for (i = 0, sg = first_scapegoat(revs, commit);
69264f46
JH
1465 i < num_sg && sg;
1466 sg = sg->next, i++) {
1467 struct origin *porigin = sg_origin[i];
cee7f245
JH
1468 if (!porigin)
1469 continue;
96e11709
JH
1470 if (!origin->previous) {
1471 origin_incref(porigin);
1472 origin->previous = porigin;
1473 }
7e6ac6e4
DK
1474 pass_blame_to_parent(sb, origin, porigin);
1475 if (!origin->suspects)
54a4c617 1476 goto finish;
cee7f245 1477 }
d24bba80
JH
1478
1479 /*
1732a1fd 1480 * Optionally find moves in parents' files.
d24bba80 1481 */
7e6ac6e4
DK
1482 if (opt & PICKAXE_BLAME_MOVE) {
1483 filter_small(sb, &toosmall, &origin->suspects, blame_move_score);
1484 if (origin->suspects) {
1485 for (i = 0, sg = first_scapegoat(revs, commit);
1486 i < num_sg && sg;
1487 sg = sg->next, i++) {
1488 struct origin *porigin = sg_origin[i];
1489 if (!porigin)
1490 continue;
1491 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1492 if (!origin->suspects)
1493 break;
1494 }
d24bba80 1495 }
7e6ac6e4 1496 }
d24bba80 1497
18abd745 1498 /*
1732a1fd 1499 * Optionally find copies from parents' files.
18abd745 1500 */
7e6ac6e4
DK
1501 if (opt & PICKAXE_BLAME_COPY) {
1502 if (blame_copy_score > blame_move_score)
1503 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1504 else if (blame_copy_score < blame_move_score) {
1505 origin->suspects = blame_merge(origin->suspects, toosmall);
1506 toosmall = NULL;
1507 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1508 }
1509 if (!origin->suspects)
1510 goto finish;
1511
85af7929 1512 for (i = 0, sg = first_scapegoat(revs, commit);
69264f46
JH
1513 i < num_sg && sg;
1514 sg = sg->next, i++) {
1515 struct origin *porigin = sg_origin[i];
7e6ac6e4
DK
1516 find_copy_in_parent(sb, &blametail, &toosmall,
1517 origin, sg->item, porigin, opt);
1518 if (!origin->suspects)
54a4c617 1519 goto finish;
18abd745 1520 }
7e6ac6e4 1521 }
54a4c617 1522
7e6ac6e4
DK
1523finish:
1524 *blametail = NULL;
1525 distribute_blame(sb, blames);
1526 /*
1527 * prepend toosmall to origin->suspects
1528 *
1529 * There is no point in sorting: this ends up on a big
1530 * unsorted list in the caller anyway.
1531 */
1532 if (toosmall) {
1533 struct blame_entry **tail = &toosmall;
1534 while (*tail)
1535 tail = &(*tail)->next;
1536 *tail = origin->suspects;
1537 origin->suspects = toosmall;
1538 }
69264f46
JH
1539 for (i = 0; i < num_sg; i++) {
1540 if (sg_origin[i]) {
1541 drop_origin_blob(sg_origin[i]);
1542 origin_decref(sg_origin[i]);
7c3c7962
JH
1543 }
1544 }
1545 drop_origin_blob(origin);
69264f46
JH
1546 if (sg_buf != sg_origin)
1547 free(sg_origin);
cee7f245
JH
1548}
1549
1732a1fd
JH
1550/*
1551 * Information on commits, used for output.
1552 */
9cba13ca 1553struct commit_info {
ea02ffa3
AP
1554 struct strbuf author;
1555 struct strbuf author_mail;
cee7f245 1556 unsigned long author_time;
ea02ffa3 1557 struct strbuf author_tz;
cee7f245
JH
1558
1559 /* filled only when asked for details */
ea02ffa3
AP
1560 struct strbuf committer;
1561 struct strbuf committer_mail;
cee7f245 1562 unsigned long committer_time;
ea02ffa3 1563 struct strbuf committer_tz;
cee7f245 1564
ea02ffa3 1565 struct strbuf summary;
cee7f245
JH
1566};
1567
1732a1fd
JH
1568/*
1569 * Parse author/committer line in the commit object buffer
1570 */
cee7f245 1571static void get_ac_line(const char *inbuf, const char *what,
ea02ffa3
AP
1572 struct strbuf *name, struct strbuf *mail,
1573 unsigned long *time, struct strbuf *tz)
cee7f245 1574{
3c020bd5 1575 struct ident_split ident;
ea02ffa3
AP
1576 size_t len, maillen, namelen;
1577 char *tmp, *endp;
1578 const char *namebuf, *mailbuf;
cee7f245
JH
1579
1580 tmp = strstr(inbuf, what);
1581 if (!tmp)
1582 goto error_out;
1583 tmp += strlen(what);
1584 endp = strchr(tmp, '\n');
1585 if (!endp)
1586 len = strlen(tmp);
1587 else
1588 len = endp - tmp;
3c020bd5
AP
1589
1590 if (split_ident_line(&ident, tmp, len)) {
cee7f245
JH
1591 error_out:
1592 /* Ugh */
ea02ffa3
AP
1593 tmp = "(unknown)";
1594 strbuf_addstr(name, tmp);
1595 strbuf_addstr(mail, tmp);
1596 strbuf_addstr(tz, tmp);
cee7f245
JH
1597 *time = 0;
1598 return;
1599 }
cee7f245 1600
3c020bd5 1601 namelen = ident.name_end - ident.name_begin;
ea02ffa3 1602 namebuf = ident.name_begin;
cee7f245 1603
ea02ffa3
AP
1604 maillen = ident.mail_end - ident.mail_begin;
1605 mailbuf = ident.mail_begin;
f9567384 1606
de5abe9f
RS
1607 if (ident.date_begin && ident.date_end)
1608 *time = strtoul(ident.date_begin, NULL, 10);
1609 else
1610 *time = 0;
f9567384 1611
de5abe9f
RS
1612 if (ident.tz_begin && ident.tz_end)
1613 strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
1614 else
1615 strbuf_addstr(tz, "(unknown)");
f9567384 1616
f9567384 1617 /*
d20d654f 1618 * Now, convert both name and e-mail using mailmap
f9567384 1619 */
ea02ffa3
AP
1620 map_user(&mailmap, &mailbuf, &maillen,
1621 &namebuf, &namelen);
1622
1623 strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
1624 strbuf_add(name, namebuf, namelen);
1625}
1626
1627static void commit_info_init(struct commit_info *ci)
1628{
1629
1630 strbuf_init(&ci->author, 0);
1631 strbuf_init(&ci->author_mail, 0);
1632 strbuf_init(&ci->author_tz, 0);
1633 strbuf_init(&ci->committer, 0);
1634 strbuf_init(&ci->committer_mail, 0);
1635 strbuf_init(&ci->committer_tz, 0);
1636 strbuf_init(&ci->summary, 0);
1637}
1638
1639static void commit_info_destroy(struct commit_info *ci)
1640{
1641
1642 strbuf_release(&ci->author);
1643 strbuf_release(&ci->author_mail);
1644 strbuf_release(&ci->author_tz);
1645 strbuf_release(&ci->committer);
1646 strbuf_release(&ci->committer_mail);
1647 strbuf_release(&ci->committer_tz);
1648 strbuf_release(&ci->summary);
cee7f245
JH
1649}
1650
1651static void get_commit_info(struct commit *commit,
1652 struct commit_info *ret,
1653 int detailed)
1654{
1655 int len;
e297cf5a 1656 const char *subject, *encoding;
b000c59b 1657 const char *message;
ea02ffa3
AP
1658
1659 commit_info_init(ret);
cee7f245 1660
e297cf5a 1661 encoding = get_log_output_encoding();
5a10d236 1662 message = logmsg_reencode(commit, NULL, encoding);
69cd8f63 1663 get_ac_line(message, "\nauthor ",
ea02ffa3 1664 &ret->author, &ret->author_mail,
cee7f245
JH
1665 &ret->author_time, &ret->author_tz);
1666
69cd8f63 1667 if (!detailed) {
b66103c3 1668 unuse_commit_buffer(commit, message);
cee7f245 1669 return;
69cd8f63 1670 }
cee7f245 1671
69cd8f63 1672 get_ac_line(message, "\ncommitter ",
ea02ffa3 1673 &ret->committer, &ret->committer_mail,
cee7f245
JH
1674 &ret->committer_time, &ret->committer_tz);
1675
ad98a58b 1676 len = find_commit_subject(message, &subject);
ea02ffa3
AP
1677 if (len)
1678 strbuf_add(&ret->summary, subject, len);
1679 else
1680 strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
1681
b66103c3 1682 unuse_commit_buffer(commit, message);
cee7f245
JH
1683}
1684
1732a1fd
JH
1685/*
1686 * To allow LF and other nonportable characters in pathnames,
1687 * they are c-style quoted as needed.
1688 */
46e5e69d
JH
1689static void write_filename_info(const char *path)
1690{
1691 printf("filename ");
663af342 1692 write_name_quoted(path, stdout, '\n');
46e5e69d
JH
1693}
1694
9991030c
JH
1695/*
1696 * Porcelain/Incremental format wants to show a lot of details per
1697 * commit. Instead of repeating this every line, emit it only once,
e86226e3
JK
1698 * the first time each commit appears in the output (unless the
1699 * user has specifically asked for us to repeat).
9991030c 1700 */
e86226e3 1701static int emit_one_suspect_detail(struct origin *suspect, int repeat)
9991030c
JH
1702{
1703 struct commit_info ci;
1704
e86226e3 1705 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
9991030c
JH
1706 return 0;
1707
1708 suspect->commit->object.flags |= METAINFO_SHOWN;
1709 get_commit_info(suspect->commit, &ci, 1);
ea02ffa3
AP
1710 printf("author %s\n", ci.author.buf);
1711 printf("author-mail %s\n", ci.author_mail.buf);
9991030c 1712 printf("author-time %lu\n", ci.author_time);
ea02ffa3
AP
1713 printf("author-tz %s\n", ci.author_tz.buf);
1714 printf("committer %s\n", ci.committer.buf);
1715 printf("committer-mail %s\n", ci.committer_mail.buf);
9991030c 1716 printf("committer-time %lu\n", ci.committer_time);
ea02ffa3
AP
1717 printf("committer-tz %s\n", ci.committer_tz.buf);
1718 printf("summary %s\n", ci.summary.buf);
9991030c
JH
1719 if (suspect->commit->object.flags & UNINTERESTING)
1720 printf("boundary\n");
96e11709
JH
1721 if (suspect->previous) {
1722 struct origin *prev = suspect->previous;
1723 printf("previous %s ", sha1_to_hex(prev->commit->object.sha1));
1724 write_name_quoted(prev->path, stdout, '\n');
1725 }
ea02ffa3
AP
1726
1727 commit_info_destroy(&ci);
1728
9991030c
JH
1729 return 1;
1730}
1731
1732a1fd 1732/*
7e6ac6e4
DK
1733 * The blame_entry is found to be guilty for the range.
1734 * Show it in incremental output.
1732a1fd 1735 */
717d1462
LT
1736static void found_guilty_entry(struct blame_entry *ent)
1737{
717d1462
LT
1738 if (incremental) {
1739 struct origin *suspect = ent->suspect;
1740
1741 printf("%s %d %d %d\n",
1742 sha1_to_hex(suspect->commit->object.sha1),
1743 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
e86226e3 1744 emit_one_suspect_detail(suspect, 0);
46e5e69d 1745 write_filename_info(suspect->path);
06f59e9f 1746 maybe_flush_or_die(stdout, "stdout");
717d1462
LT
1747 }
1748}
1749
1732a1fd 1750/*
7e6ac6e4
DK
1751 * The main loop -- while we have blobs with lines whose true origin
1752 * is still unknown, pick one blob, and allow its lines to pass blames
1753 * to its parents. */
85af7929 1754static void assign_blame(struct scoreboard *sb, int opt)
717d1462 1755{
85af7929 1756 struct rev_info *revs = sb->revs;
7e6ac6e4 1757 struct commit *commit = prio_queue_get(&sb->commits);
85af7929 1758
7e6ac6e4 1759 while (commit) {
717d1462 1760 struct blame_entry *ent;
7e6ac6e4 1761 struct origin *suspect = commit->util;
717d1462
LT
1762
1763 /* find one suspect to break down */
7e6ac6e4
DK
1764 while (suspect && !suspect->suspects)
1765 suspect = suspect->next;
1766
1767 if (!suspect) {
1768 commit = prio_queue_get(&sb->commits);
1769 continue;
1770 }
1771
1772 assert(commit == suspect->commit);
717d1462 1773
1732a1fd
JH
1774 /*
1775 * We will use this suspect later in the loop,
1776 * so hold onto it in the meantime.
1777 */
717d1462 1778 origin_incref(suspect);
0064053b 1779 parse_commit(commit);
85af7929
JH
1780 if (reverse ||
1781 (!(commit->object.flags & UNINTERESTING) &&
1782 !(revs->max_age != -1 && commit->date < revs->max_age)))
717d1462
LT
1783 pass_blame(sb, suspect, opt);
1784 else {
1785 commit->object.flags |= UNINTERESTING;
1786 if (commit->object.parsed)
1787 mark_parents_uninteresting(commit);
1788 }
1789 /* treat root commit as boundary */
1790 if (!commit->parents && !show_root)
1791 commit->object.flags |= UNINTERESTING;
1792
1793 /* Take responsibility for the remaining entries */
7e6ac6e4
DK
1794 ent = suspect->suspects;
1795 if (ent) {
1796 suspect->guilty = 1;
1797 for (;;) {
1798 struct blame_entry *next = ent->next;
717d1462 1799 found_guilty_entry(ent);
7e6ac6e4
DK
1800 if (next) {
1801 ent = next;
1802 continue;
1803 }
1804 ent->next = sb->ent;
1805 sb->ent = suspect->suspects;
1806 suspect->suspects = NULL;
1807 break;
1808 }
1809 }
717d1462
LT
1810 origin_decref(suspect);
1811
1812 if (DEBUG) /* sanity */
1813 sanity_check_refcnt(sb);
1814 }
1815}
1816
1817static const char *format_time(unsigned long time, const char *tz_str,
1818 int show_raw_time)
1819{
bccce0f8 1820 static struct strbuf time_buf = STRBUF_INIT;
717d1462 1821
bccce0f8 1822 strbuf_reset(&time_buf);
717d1462 1823 if (show_raw_time) {
bccce0f8 1824 strbuf_addf(&time_buf, "%lu %s", time, tz_str);
717d1462 1825 }
31653c1a 1826 else {
ac39b277 1827 const char *time_str;
bccce0f8 1828 size_t time_width;
ac39b277 1829 int tz;
31653c1a 1830 tz = atoi(tz_str);
a5481a6c 1831 time_str = show_date(time, tz, &blame_date_mode);
bccce0f8
JX
1832 strbuf_addstr(&time_buf, time_str);
1833 /*
1834 * Add space paddings to time_buf to display a fixed width
1835 * string, and use time_width for display width calibration.
1836 */
1837 for (time_width = utf8_strwidth(time_str);
1838 time_width < blame_date_width;
1839 time_width++)
1840 strbuf_addch(&time_buf, ' ');
31653c1a 1841 }
bccce0f8 1842 return time_buf.buf;
717d1462
LT
1843}
1844
cee7f245
JH
1845#define OUTPUT_ANNOTATE_COMPAT 001
1846#define OUTPUT_LONG_OBJECT_NAME 002
1847#define OUTPUT_RAW_TIMESTAMP 004
1848#define OUTPUT_PORCELAIN 010
1849#define OUTPUT_SHOW_NAME 020
1850#define OUTPUT_SHOW_NUMBER 040
5ff62c30 1851#define OUTPUT_SHOW_SCORE 0100
093dc5be 1852#define OUTPUT_NO_AUTHOR 0200
1b8cdce9 1853#define OUTPUT_SHOW_EMAIL 0400
ed747dd5 1854#define OUTPUT_LINE_PORCELAIN 01000
cee7f245 1855
e86226e3
JK
1856static void emit_porcelain_details(struct origin *suspect, int repeat)
1857{
1858 if (emit_one_suspect_detail(suspect, repeat) ||
1859 (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
1860 write_filename_info(suspect->path);
1861}
1862
1863static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
1864 int opt)
cee7f245 1865{
ed747dd5 1866 int repeat = opt & OUTPUT_LINE_PORCELAIN;
cee7f245
JH
1867 int cnt;
1868 const char *cp;
1869 struct origin *suspect = ent->suspect;
d59f765a 1870 char hex[GIT_SHA1_HEXSZ + 1];
cee7f245 1871
d59f765a 1872 sha1_to_hex_r(hex, suspect->commit->object.sha1);
7e6ac6e4 1873 printf("%s %d %d %d\n",
cee7f245 1874 hex,
cee7f245
JH
1875 ent->s_lno + 1,
1876 ent->lno + 1,
1877 ent->num_lines);
ed747dd5 1878 emit_porcelain_details(suspect, repeat);
cee7f245
JH
1879
1880 cp = nth_line(sb, ent->lno);
1881 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1882 char ch;
ed747dd5 1883 if (cnt) {
cee7f245
JH
1884 printf("%s %d %d\n", hex,
1885 ent->s_lno + 1 + cnt,
1886 ent->lno + 1 + cnt);
ed747dd5
JK
1887 if (repeat)
1888 emit_porcelain_details(suspect, 1);
1889 }
cee7f245
JH
1890 putchar('\t');
1891 do {
1892 ch = *cp++;
1893 putchar(ch);
1894 } while (ch != '\n' &&
1895 cp < sb->final_buf + sb->final_buf_size);
1896 }
a5ca8367
JS
1897
1898 if (sb->final_buf_size && cp[-1] != '\n')
1899 putchar('\n');
cee7f245
JH
1900}
1901
1902static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1903{
1904 int cnt;
1905 const char *cp;
1906 struct origin *suspect = ent->suspect;
1907 struct commit_info ci;
d59f765a 1908 char hex[GIT_SHA1_HEXSZ + 1];
cee7f245
JH
1909 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1910
1911 get_commit_info(suspect->commit, &ci, 1);
d59f765a 1912 sha1_to_hex_r(hex, suspect->commit->object.sha1);
cee7f245
JH
1913
1914 cp = nth_line(sb, ent->lno);
1915 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1916 char ch;
84393bfd 1917 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev;
b11121d9
JH
1918
1919 if (suspect->commit->object.flags & UNINTERESTING) {
e68989a7
JH
1920 if (blank_boundary)
1921 memset(hex, ' ', length);
7ceacdff 1922 else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
4c10a5ca
JH
1923 length--;
1924 putchar('^');
1925 }
b11121d9 1926 }
cee7f245 1927
b11121d9 1928 printf("%.*s", length, hex);
1b8cdce9
KB
1929 if (opt & OUTPUT_ANNOTATE_COMPAT) {
1930 const char *name;
1931 if (opt & OUTPUT_SHOW_EMAIL)
ea02ffa3 1932 name = ci.author_mail.buf;
1b8cdce9 1933 else
ea02ffa3 1934 name = ci.author.buf;
1b8cdce9 1935 printf("\t(%10s\t%10s\t%d)", name,
ea02ffa3 1936 format_time(ci.author_time, ci.author_tz.buf,
cee7f245
JH
1937 show_raw_time),
1938 ent->lno + 1 + cnt);
1b8cdce9 1939 } else {
5ff62c30 1940 if (opt & OUTPUT_SHOW_SCORE)
54a4c617
JH
1941 printf(" %*d %02d",
1942 max_score_digits, ent->score,
1943 ent->suspect->refcnt);
cee7f245
JH
1944 if (opt & OUTPUT_SHOW_NAME)
1945 printf(" %-*.*s", longest_file, longest_file,
1946 suspect->path);
1947 if (opt & OUTPUT_SHOW_NUMBER)
1948 printf(" %*d", max_orig_digits,
1949 ent->s_lno + 1 + cnt);
093dc5be 1950
ffaf9cc0 1951 if (!(opt & OUTPUT_NO_AUTHOR)) {
1b8cdce9
KB
1952 const char *name;
1953 int pad;
1954 if (opt & OUTPUT_SHOW_EMAIL)
ea02ffa3 1955 name = ci.author_mail.buf;
1b8cdce9 1956 else
ea02ffa3 1957 name = ci.author.buf;
1b8cdce9 1958 pad = longest_author - utf8_strwidth(name);
ffaf9cc0 1959 printf(" (%s%*s %10s",
1b8cdce9 1960 name, pad, "",
093dc5be 1961 format_time(ci.author_time,
ea02ffa3 1962 ci.author_tz.buf,
093dc5be 1963 show_raw_time));
ffaf9cc0 1964 }
093dc5be 1965 printf(" %*d) ",
cee7f245
JH
1966 max_digits, ent->lno + 1 + cnt);
1967 }
1968 do {
1969 ch = *cp++;
1970 putchar(ch);
1971 } while (ch != '\n' &&
1972 cp < sb->final_buf + sb->final_buf_size);
1973 }
a5ca8367
JS
1974
1975 if (sb->final_buf_size && cp[-1] != '\n')
1976 putchar('\n');
ea02ffa3
AP
1977
1978 commit_info_destroy(&ci);
cee7f245
JH
1979}
1980
1981static void output(struct scoreboard *sb, int option)
1982{
1983 struct blame_entry *ent;
1984
1985 if (option & OUTPUT_PORCELAIN) {
1986 for (ent = sb->ent; ent; ent = ent->next) {
7e6ac6e4
DK
1987 int count = 0;
1988 struct origin *suspect;
1989 struct commit *commit = ent->suspect->commit;
cee7f245
JH
1990 if (commit->object.flags & MORE_THAN_ONE_PATH)
1991 continue;
7e6ac6e4
DK
1992 for (suspect = commit->util; suspect; suspect = suspect->next) {
1993 if (suspect->guilty && count++) {
1994 commit->object.flags |= MORE_THAN_ONE_PATH;
1995 break;
1996 }
cee7f245
JH
1997 }
1998 }
1999 }
2000
2001 for (ent = sb->ent; ent; ent = ent->next) {
2002 if (option & OUTPUT_PORCELAIN)
e86226e3 2003 emit_porcelain(sb, ent, option);
5ff62c30 2004 else {
cee7f245 2005 emit_other(sb, ent, option);
5ff62c30 2006 }
cee7f245
JH
2007 }
2008}
2009
29aa0b20
RS
2010static const char *get_next_line(const char *start, const char *end)
2011{
2012 const char *nl = memchr(start, '\n', end - start);
60d85e11 2013 return nl ? nl + 1 : end;
29aa0b20
RS
2014}
2015
1732a1fd
JH
2016/*
2017 * To allow quick access to the contents of nth line in the
2018 * final image, prepare an index in the scoreboard.
2019 */
cee7f245
JH
2020static int prepare_lines(struct scoreboard *sb)
2021{
2022 const char *buf = sb->final_buf;
2023 unsigned long len = sb->final_buf_size;
352bbbd9
DK
2024 const char *end = buf + len;
2025 const char *p;
2026 int *lineno;
60d85e11 2027 int num = 0;
352bbbd9 2028
60d85e11 2029 for (p = buf; p < end; p = get_next_line(p, end))
29aa0b20 2030 num++;
352bbbd9 2031
60d85e11 2032 sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
352bbbd9 2033
60d85e11 2034 for (p = buf; p < end; p = get_next_line(p, end))
29aa0b20 2035 *lineno++ = p - buf;
352bbbd9 2036
60d85e11 2037 *lineno = len;
352bbbd9 2038
60d85e11 2039 sb->num_lines = num;
cee7f245
JH
2040 return sb->num_lines;
2041}
2042
1732a1fd
JH
2043/*
2044 * Add phony grafts for use with -S; this is primarily to
34baebce 2045 * support git's cvsserver that wants to give a linear history
1732a1fd
JH
2046 * to its clients.
2047 */
cee7f245
JH
2048static int read_ancestry(const char *graft_file)
2049{
2050 FILE *fp = fopen(graft_file, "r");
e228c173 2051 struct strbuf buf = STRBUF_INIT;
cee7f245
JH
2052 if (!fp)
2053 return -1;
e228c173 2054 while (!strbuf_getwholeline(&buf, fp, '\n')) {
cee7f245 2055 /* The format is just "Commit Parent1 Parent2 ...\n" */
e228c173 2056 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
8eaf7986
JH
2057 if (graft)
2058 register_commit_graft(graft, 0);
cee7f245
JH
2059 }
2060 fclose(fp);
e228c173 2061 strbuf_release(&buf);
cee7f245
JH
2062 return 0;
2063}
2064
b31272f7
JH
2065static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
2066{
2067 const char *uniq = find_unique_abbrev(suspect->commit->object.sha1,
2068 auto_abbrev);
2069 int len = strlen(uniq);
2070 if (auto_abbrev < len)
2071 return len;
2072 return auto_abbrev;
2073}
2074
1732a1fd
JH
2075/*
2076 * How many columns do we need to show line numbers, authors,
2077 * and filenames?
2078 */
cee7f245
JH
2079static void find_alignment(struct scoreboard *sb, int *option)
2080{
2081 int longest_src_lines = 0;
2082 int longest_dst_lines = 0;
5ff62c30 2083 unsigned largest_score = 0;
cee7f245 2084 struct blame_entry *e;
b31272f7
JH
2085 int compute_auto_abbrev = (abbrev < 0);
2086 int auto_abbrev = default_abbrev;
cee7f245
JH
2087
2088 for (e = sb->ent; e; e = e->next) {
2089 struct origin *suspect = e->suspect;
cee7f245
JH
2090 int num;
2091
b31272f7
JH
2092 if (compute_auto_abbrev)
2093 auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
ab3bb800
JH
2094 if (strcmp(suspect->path, sb->path))
2095 *option |= OUTPUT_SHOW_NAME;
2096 num = strlen(suspect->path);
2097 if (longest_file < num)
2098 longest_file = num;
cee7f245 2099 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
e6005927 2100 struct commit_info ci;
cee7f245
JH
2101 suspect->commit->object.flags |= METAINFO_SHOWN;
2102 get_commit_info(suspect->commit, &ci, 1);
1b8cdce9 2103 if (*option & OUTPUT_SHOW_EMAIL)
ea02ffa3 2104 num = utf8_strwidth(ci.author_mail.buf);
1b8cdce9 2105 else
ea02ffa3 2106 num = utf8_strwidth(ci.author.buf);
cee7f245
JH
2107 if (longest_author < num)
2108 longest_author = num;
e6005927 2109 commit_info_destroy(&ci);
cee7f245
JH
2110 }
2111 num = e->s_lno + e->num_lines;
2112 if (longest_src_lines < num)
2113 longest_src_lines = num;
2114 num = e->lno + e->num_lines;
2115 if (longest_dst_lines < num)
2116 longest_dst_lines = num;
5ff62c30
JH
2117 if (largest_score < ent_score(sb, e))
2118 largest_score = ent_score(sb, e);
cee7f245 2119 }
ec7ff5ba
ZJS
2120 max_orig_digits = decimal_width(longest_src_lines);
2121 max_digits = decimal_width(longest_dst_lines);
2122 max_score_digits = decimal_width(largest_score);
b31272f7
JH
2123
2124 if (compute_auto_abbrev)
2125 /* one more abbrev length is needed for the boundary commit */
2126 abbrev = auto_abbrev + 1;
cee7f245
JH
2127}
2128
1732a1fd
JH
2129/*
2130 * For debugging -- origin is refcounted, and this asserts that
2131 * we do not underflow.
2132 */
54a4c617
JH
2133static void sanity_check_refcnt(struct scoreboard *sb)
2134{
2135 int baa = 0;
2136 struct blame_entry *ent;
2137
2138 for (ent = sb->ent; ent; ent = ent->next) {
ae86ad65 2139 /* Nobody should have zero or negative refcnt */
854b97f6
JH
2140 if (ent->suspect->refcnt <= 0) {
2141 fprintf(stderr, "%s in %s has negative refcnt %d\n",
2142 ent->suspect->path,
2143 sha1_to_hex(ent->suspect->commit->object.sha1),
2144 ent->suspect->refcnt);
ae86ad65 2145 baa = 1;
854b97f6 2146 }
ae86ad65 2147 }
54a4c617
JH
2148 if (baa) {
2149 int opt = 0160;
2150 find_alignment(sb, &opt);
2151 output(sb, opt);
854b97f6 2152 die("Baa %d!", baa);
54a4c617
JH
2153 }
2154}
2155
4a0fc95f
JH
2156static unsigned parse_score(const char *arg)
2157{
2158 char *end;
2159 unsigned long score = strtoul(arg, &end, 10);
2160 if (*end)
2161 return 0;
2162 return score;
2163}
2164
20239bae
JK
2165static const char *add_prefix(const char *prefix, const char *path)
2166{
097971f5 2167 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
20239bae
JK
2168}
2169
ef90d6d4 2170static int git_blame_config(const char *var, const char *value, void *cb)
4c10a5ca
JH
2171{
2172 if (!strcmp(var, "blame.showroot")) {
2173 show_root = git_config_bool(var, value);
2174 return 0;
2175 }
2176 if (!strcmp(var, "blame.blankboundary")) {
2177 blank_boundary = git_config_bool(var, value);
2178 return 0;
2179 }
8b504db3
QN
2180 if (!strcmp(var, "blame.showemail")) {
2181 int *output_option = cb;
2182 if (git_config_bool(var, value))
2183 *output_option |= OUTPUT_SHOW_EMAIL;
2184 else
2185 *output_option &= ~OUTPUT_SHOW_EMAIL;
2186 return 0;
2187 }
31653c1a
EL
2188 if (!strcmp(var, "blame.date")) {
2189 if (!value)
2190 return config_error_nonbool(var);
a5481a6c 2191 parse_date_format(value, &blame_date_mode);
31653c1a
EL
2192 return 0;
2193 }
3b8a12e8 2194
6680a087 2195 if (userdiff_config(var, value) < 0)
3b8a12e8 2196 return -1;
3b8a12e8 2197
ef90d6d4 2198 return git_default_config(var, value, cb);
4c10a5ca
JH
2199}
2200
9aeaab68 2201static void verify_working_tree_path(struct commit *work_tree, const char *path)
ffcabccf 2202{
9aeaab68 2203 struct commit_list *parents;
ffcabccf 2204
9aeaab68
JH
2205 for (parents = work_tree->parents; parents; parents = parents->next) {
2206 const unsigned char *commit_sha1 = parents->item->object.sha1;
2207 unsigned char blob_sha1[20];
2208 unsigned mode;
2209
2210 if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) &&
2211 sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
2212 return;
2213 }
2214 die("no such path '%s' in HEAD", path);
2215}
2216
2217static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
2218{
2219 struct commit *parent;
2220
2221 parent = lookup_commit_reference(sha1);
2222 if (!parent)
2223 die("no such commit %s", sha1_to_hex(sha1));
2224 return &commit_list_insert(parent, tail)->next;
2225}
2226
2227static void append_merge_parents(struct commit_list **tail)
2228{
2229 int merge_head;
9aeaab68
JH
2230 struct strbuf line = STRBUF_INIT;
2231
f932729c 2232 merge_head = open(git_path_merge_head(), O_RDONLY);
9aeaab68
JH
2233 if (merge_head < 0) {
2234 if (errno == ENOENT)
2235 return;
f932729c 2236 die("cannot open '%s' for reading", git_path_merge_head());
9aeaab68
JH
2237 }
2238
2239 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
2240 unsigned char sha1[20];
2241 if (line.len < 40 || get_sha1_hex(line.buf, sha1))
f932729c 2242 die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
9aeaab68
JH
2243 tail = append_parent(tail, sha1);
2244 }
2245 close(merge_head);
2246 strbuf_release(&line);
ffcabccf
JH
2247}
2248
8597ea3a
JK
2249/*
2250 * This isn't as simple as passing sb->buf and sb->len, because we
2251 * want to transfer ownership of the buffer to the commit (so we
2252 * must use detach).
2253 */
2254static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
2255{
2256 size_t len;
2257 void *buf = strbuf_detach(sb, &len);
2258 set_commit_buffer(c, buf, len);
2259}
2260
f6c07d7d
JH
2261/*
2262 * Prepare a dummy commit that represents the work tree (or staged) item.
2263 * Note that annotating work tree item never works in the reverse.
2264 */
3b8a12e8
AB
2265static struct commit *fake_working_tree_commit(struct diff_options *opt,
2266 const char *path,
2267 const char *contents_from)
1cfe7733
JH
2268{
2269 struct commit *commit;
2270 struct origin *origin;
9aeaab68 2271 struct commit_list **parent_tail, *parent;
1cfe7733 2272 unsigned char head_sha1[20];
f285a2d7 2273 struct strbuf buf = STRBUF_INIT;
1cfe7733 2274 const char *ident;
1cfe7733 2275 time_t now;
1cfe7733
JH
2276 int size, len;
2277 struct cache_entry *ce;
2278 unsigned mode;
9aeaab68 2279 struct strbuf msg = STRBUF_INIT;
1cfe7733
JH
2280
2281 time(&now);
10322a0a 2282 commit = alloc_commit_node();
1cfe7733
JH
2283 commit->object.parsed = 1;
2284 commit->date = now;
9aeaab68
JH
2285 parent_tail = &commit->parents;
2286
7695d118 2287 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
9aeaab68
JH
2288 die("no such ref: HEAD");
2289
2290 parent_tail = append_parent(parent_tail, head_sha1);
2291 append_merge_parents(parent_tail);
2292 verify_working_tree_path(commit, path);
1cfe7733
JH
2293
2294 origin = make_origin(commit, path);
2295
9aeaab68
JH
2296 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2297 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
2298 for (parent = commit->parents; parent; parent = parent->next)
2299 strbuf_addf(&msg, "parent %s\n",
2300 sha1_to_hex(parent->item->object.sha1));
2301 strbuf_addf(&msg,
2302 "author %s\n"
2303 "committer %s\n\n"
2304 "Version of %s from %s\n",
2305 ident, ident, path,
2306 (!contents_from ? path :
2307 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
8597ea3a 2308 set_commit_buffer_from_strbuf(commit, &msg);
9aeaab68 2309
1cfe7733
JH
2310 if (!contents_from || strcmp("-", contents_from)) {
2311 struct stat st;
2312 const char *read_from;
8518088f 2313 char *buf_ptr;
3b8a12e8 2314 unsigned long buf_len;
1cfe7733
JH
2315
2316 if (contents_from) {
2317 if (stat(contents_from, &st) < 0)
0721c314 2318 die_errno("Cannot stat '%s'", contents_from);
1cfe7733
JH
2319 read_from = contents_from;
2320 }
2321 else {
2322 if (lstat(path, &st) < 0)
0721c314 2323 die_errno("Cannot lstat '%s'", path);
1cfe7733
JH
2324 read_from = path;
2325 }
1cfe7733 2326 mode = canon_mode(st.st_mode);
3b8a12e8 2327
1cfe7733
JH
2328 switch (st.st_mode & S_IFMT) {
2329 case S_IFREG:
3b8a12e8 2330 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
e5450100 2331 textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len))
8518088f 2332 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
3b8a12e8 2333 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
0721c314 2334 die_errno("cannot open or read '%s'", read_from);
1cfe7733
JH
2335 break;
2336 case S_IFLNK:
edfd45d2 2337 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
0721c314 2338 die_errno("cannot readlink '%s'", read_from);
1cfe7733
JH
2339 break;
2340 default:
2341 die("unsupported file type %s", read_from);
2342 }
2343 }
2344 else {
2345 /* Reading from stdin */
1cfe7733 2346 mode = 0;
f1696ee3 2347 if (strbuf_read(&buf, 0, 0) < 0)
d824cbba 2348 die_errno("failed to read from stdin");
1cfe7733 2349 }
4bf256d6 2350 convert_to_git(path, buf.buf, buf.len, &buf, 0);
af6eb822
PH
2351 origin->file.ptr = buf.buf;
2352 origin->file.size = buf.len;
2353 pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
1cfe7733
JH
2354
2355 /*
2356 * Read the current index, replace the path entry with
2357 * origin->blob_sha1 without mucking with its mode or type
2358 * bits; we are not going to write this index out -- we just
2359 * want to run "diff-index --cached".
2360 */
2361 discard_cache();
2362 read_cache();
2363
2364 len = strlen(path);
2365 if (!mode) {
2366 int pos = cache_name_pos(path, len);
2367 if (0 <= pos)
7a51ed66 2368 mode = active_cache[pos]->ce_mode;
1cfe7733
JH
2369 else
2370 /* Let's not bother reading from HEAD tree */
2371 mode = S_IFREG | 0644;
2372 }
2373 size = cache_entry_size(len);
2374 ce = xcalloc(1, size);
2375 hashcpy(ce->sha1, origin->blob_sha1);
2376 memcpy(ce->name, path, len);
b60e188c
TG
2377 ce->ce_flags = create_ce_flags(0);
2378 ce->ce_namelen = len;
1cfe7733
JH
2379 ce->ce_mode = create_ce_mode(mode);
2380 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2381
2382 /*
2383 * We are not going to write this out, so this does not matter
2384 * right now, but someday we might optimize diff-index --cached
2385 * with cache-tree information.
2386 */
a5400efe 2387 cache_tree_invalidate_path(&the_index, path);
1cfe7733 2388
1cfe7733
JH
2389 return commit;
2390}
2391
a46442f1 2392static char *prepare_final(struct scoreboard *sb)
f6c07d7d
JH
2393{
2394 int i;
2395 const char *final_commit_name = NULL;
85af7929 2396 struct rev_info *revs = sb->revs;
f6c07d7d
JH
2397
2398 /*
2399 * There must be one and only one positive commit in the
2400 * revs->pending array.
2401 */
2402 for (i = 0; i < revs->pending.nr; i++) {
2403 struct object *obj = revs->pending.objects[i].item;
2404 if (obj->flags & UNINTERESTING)
2405 continue;
2406 while (obj->type == OBJ_TAG)
2407 obj = deref_tag(obj, NULL, 0);
2408 if (obj->type != OBJ_COMMIT)
2409 die("Non commit %s?", revs->pending.objects[i].name);
2410 if (sb->final)
2411 die("More than one commit to dig from %s and %s?",
2412 revs->pending.objects[i].name,
2413 final_commit_name);
2414 sb->final = (struct commit *) obj;
2415 final_commit_name = revs->pending.objects[i].name;
2416 }
a46442f1 2417 return xstrdup_or_null(final_commit_name);
f6c07d7d
JH
2418}
2419
a46442f1 2420static char *prepare_initial(struct scoreboard *sb)
85af7929
JH
2421{
2422 int i;
2423 const char *final_commit_name = NULL;
2424 struct rev_info *revs = sb->revs;
2425
2426 /*
2427 * There must be one and only one negative commit, and it must be
2428 * the boundary.
2429 */
2430 for (i = 0; i < revs->pending.nr; i++) {
2431 struct object *obj = revs->pending.objects[i].item;
2432 if (!(obj->flags & UNINTERESTING))
2433 continue;
2434 while (obj->type == OBJ_TAG)
2435 obj = deref_tag(obj, NULL, 0);
2436 if (obj->type != OBJ_COMMIT)
2437 die("Non commit %s?", revs->pending.objects[i].name);
2438 if (sb->final)
2439 die("More than one commit to dig down to %s and %s?",
2440 revs->pending.objects[i].name,
2441 final_commit_name);
2442 sb->final = (struct commit *) obj;
2443 final_commit_name = revs->pending.objects[i].name;
2444 }
2445 if (!final_commit_name)
2446 die("No commit to dig down to?");
a46442f1 2447 return xstrdup(final_commit_name);
85af7929
JH
2448}
2449
5817da01
PH
2450static int blame_copy_callback(const struct option *option, const char *arg, int unset)
2451{
2452 int *opt = option->value;
2453
2454 /*
2455 * -C enables copy from removed files;
2456 * -C -C enables copy from existing files, but only
2457 * when blaming a new file;
2458 * -C -C -C enables copy from existing files for
2459 * everybody
2460 */
2461 if (*opt & PICKAXE_BLAME_COPY_HARDER)
2462 *opt |= PICKAXE_BLAME_COPY_HARDEST;
2463 if (*opt & PICKAXE_BLAME_COPY)
2464 *opt |= PICKAXE_BLAME_COPY_HARDER;
2465 *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2466
2467 if (arg)
2468 blame_copy_score = parse_score(arg);
2469 return 0;
2470}
2471
2472static int blame_move_callback(const struct option *option, const char *arg, int unset)
2473{
2474 int *opt = option->value;
2475
2476 *opt |= PICKAXE_BLAME_MOVE;
2477
2478 if (arg)
2479 blame_move_score = parse_score(arg);
2480 return 0;
2481}
2482
acca687f 2483int cmd_blame(int argc, const char **argv, const char *prefix)
cee7f245
JH
2484{
2485 struct rev_info revs;
2486 const char *path;
2487 struct scoreboard sb;
2488 struct origin *o;
58dbfa2e
ES
2489 struct blame_entry *ent = NULL;
2490 long dashdash_pos, lno;
a46442f1 2491 char *final_commit_name = NULL;
21666f1a 2492 enum object_type type;
5817da01 2493
58dbfa2e 2494 static struct string_list range_list;
5817da01
PH
2495 static int output_option = 0, opt = 0;
2496 static int show_stats = 0;
2497 static const char *revs_file = NULL;
2498 static const char *contents_from = NULL;
2499 static const struct option options[] = {
d5d09d47
SB
2500 OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
2501 OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2502 OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
2503 OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
efd2a8bd
NTND
2504 OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
2505 OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
2506 OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
2507 OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
2508 OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
2509 OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
2510 OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
2511 OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
2512 OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
2513 OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
2514 OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
2515 OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
2516 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2517 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
2518 { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
2519 { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
58dbfa2e 2520 OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
84393bfd 2521 OPT__ABBREV(&abbrev),
5817da01
PH
2522 OPT_END()
2523 };
2524
2525 struct parse_opt_ctx_t ctx;
7ceacdff 2526 int cmd_is_annotate = !strcmp(argv[0], "annotate");
58dbfa2e
ES
2527 struct range_set ranges;
2528 unsigned int range_i;
52f4d126 2529 long anchor;
e68989a7 2530
8b504db3 2531 git_config(git_blame_config, &output_option);
5817da01 2532 init_revisions(&revs, NULL);
31653c1a 2533 revs.date_mode = blame_date_mode;
3b8a12e8 2534 DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
3d1aa566 2535 DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
31653c1a 2536
612702e8 2537 save_commit_buffer = 0;
3f8d5204 2538 dashdash_pos = 0;
612702e8 2539
9ca1169f
SB
2540 parse_options_start(&ctx, argc, argv, prefix, options,
2541 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
5817da01 2542 for (;;) {
5817da01
PH
2543 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
2544 case PARSE_OPT_HELP:
2545 exit(129);
2546 case PARSE_OPT_DONE:
3f8d5204
PH
2547 if (ctx.argv[0])
2548 dashdash_pos = ctx.cpidx;
5817da01
PH
2549 goto parse_done;
2550 }
2551
2552 if (!strcmp(ctx.argv[0], "--reverse")) {
2553 ctx.argv[0] = "--children";
2554 reverse = 1;
2555 }
6b61ec05 2556 parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
5817da01
PH
2557 }
2558parse_done:
3d1aa566
JH
2559 no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
2560 DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
5817da01
PH
2561 argc = parse_options_end(&ctx);
2562
b31272f7
JH
2563 if (0 < abbrev)
2564 /* one more abbrev length is needed for the boundary commit */
2565 abbrev++;
84393bfd 2566
aa9ea77d 2567 if (revs_file && read_ancestry(revs_file))
d824cbba 2568 die_errno("reading graft file '%s' failed", revs_file);
aa9ea77d 2569
31653c1a 2570 if (cmd_is_annotate) {
7ceacdff 2571 output_option |= OUTPUT_ANNOTATE_COMPAT;
a5481a6c 2572 blame_date_mode.type = DATE_ISO8601;
31653c1a
EL
2573 } else {
2574 blame_date_mode = revs.date_mode;
2575 }
2576
2577 /* The maximum width used to show the dates */
a5481a6c 2578 switch (blame_date_mode.type) {
31653c1a
EL
2579 case DATE_RFC2822:
2580 blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2581 break;
466fb674
BB
2582 case DATE_ISO8601_STRICT:
2583 blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
2584 break;
31653c1a
EL
2585 case DATE_ISO8601:
2586 blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2587 break;
2588 case DATE_RAW:
2589 blame_date_width = sizeof("1161298804 -0700");
2590 break;
2591 case DATE_SHORT:
2592 blame_date_width = sizeof("2006-10-19");
2593 break;
2594 case DATE_RELATIVE:
dd75553b
JX
2595 /* TRANSLATORS: This string is used to tell us the maximum
2596 display width for a relative timestamp in "git blame"
2597 output. For C locale, "4 years, 11 months ago", which
2598 takes 22 places, is the longest among various forms of
2599 relative timestamps, but your language may need more or
2600 fewer display columns. */
2601 blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2602 break;
31653c1a
EL
2603 case DATE_LOCAL:
2604 case DATE_NORMAL:
2605 blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2606 break;
aa1462cc
JK
2607 case DATE_STRFTIME:
2608 blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2609 break;
31653c1a
EL
2610 }
2611 blame_date_width -= 1; /* strip the null */
7ceacdff 2612
b3123f98
JH
2613 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
2614 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
2615 PICKAXE_BLAME_COPY_HARDER);
2616
4a0fc95f
JH
2617 if (!blame_move_score)
2618 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2619 if (!blame_copy_score)
2620 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2621
1732a1fd
JH
2622 /*
2623 * We have collected options unknown to us in argv[1..unk]
cee7f245 2624 * which are to be passed to revision machinery if we are
3dff5379 2625 * going to do the "bottom" processing.
cee7f245
JH
2626 *
2627 * The remaining are:
2628 *
22e5e58a 2629 * (1) if dashdash_pos != 0, it is either
3f8d5204
PH
2630 * "blame [revisions] -- <path>" or
2631 * "blame -- <path> <rev>"
cee7f245 2632 *
22e5e58a 2633 * (2) otherwise, it is one of the two:
3f8d5204
PH
2634 * "blame [revisions] <path>"
2635 * "blame <path> <rev>"
cee7f245 2636 *
3f8d5204
PH
2637 * Note that we must strip out <path> from the arguments: we do not
2638 * want the path pruning but we may want "bottom" processing.
cee7f245 2639 */
3f8d5204
PH
2640 if (dashdash_pos) {
2641 switch (argc - dashdash_pos - 1) {
2642 case 2: /* (1b) */
2643 if (argc != 4)
5817da01 2644 usage_with_options(blame_opt_usage, options);
3f8d5204
PH
2645 /* reorder for the new way: <rev> -- <path> */
2646 argv[1] = argv[3];
2647 argv[3] = argv[2];
2648 argv[2] = "--";
2649 /* FALLTHROUGH */
2650 case 1: /* (1a) */
2651 path = add_prefix(prefix, argv[--argc]);
2652 argv[argc] = NULL;
2653 break;
2654 default:
2655 usage_with_options(blame_opt_usage, options);
cee7f245 2656 }
3f8d5204
PH
2657 } else {
2658 if (argc < 2)
5817da01 2659 usage_with_options(blame_opt_usage, options);
3f8d5204 2660 path = add_prefix(prefix, argv[argc - 1]);
dbe44faa 2661 if (argc == 3 && !file_exists(path)) { /* (2b) */
3f8d5204
PH
2662 path = add_prefix(prefix, argv[1]);
2663 argv[1] = argv[2];
cee7f245 2664 }
3f8d5204 2665 argv[argc - 1] = "--";
cee7f245 2666
3f8d5204 2667 setup_work_tree();
dbe44faa 2668 if (!file_exists(path))
d824cbba 2669 die_errno("cannot stat path '%s'", path);
cee7f245
JH
2670 }
2671
8b3dce56 2672 revs.disable_stdin = 1;
3f8d5204 2673 setup_revisions(argc, argv, &revs, NULL);
cee7f245
JH
2674 memset(&sb, 0, sizeof(sb));
2675
85af7929 2676 sb.revs = &revs;
7e6ac6e4 2677 if (!reverse) {
85af7929 2678 final_commit_name = prepare_final(&sb);
7e6ac6e4
DK
2679 sb.commits.compare = compare_commits_by_commit_date;
2680 }
85af7929
JH
2681 else if (contents_from)
2682 die("--contents and --children do not blend well.");
7e6ac6e4 2683 else {
85af7929 2684 final_commit_name = prepare_initial(&sb);
7e6ac6e4
DK
2685 sb.commits.compare = compare_commits_by_reverse_commit_date;
2686 }
cee7f245
JH
2687
2688 if (!sb.final) {
1732a1fd
JH
2689 /*
2690 * "--not A B -- path" without anything positive;
1cfe7733
JH
2691 * do not default to HEAD, but use the working tree
2692 * or "--contents".
1732a1fd 2693 */
1981820b 2694 setup_work_tree();
3b8a12e8
AB
2695 sb.final = fake_working_tree_commit(&sb.revs->diffopt,
2696 path, contents_from);
1cfe7733 2697 add_pending_object(&revs, &(sb.final->object), ":");
cee7f245 2698 }
1cfe7733
JH
2699 else if (contents_from)
2700 die("Cannot use --contents with final commit object name");
cee7f245 2701
1732a1fd
JH
2702 /*
2703 * If we have bottom, this will mark the ancestors of the
cee7f245
JH
2704 * bottom commits we would reach while traversing as
2705 * uninteresting.
2706 */
3d51e1b5 2707 if (prepare_revision_walk(&revs))
20108742 2708 die(_("revision walk setup failed"));
cee7f245 2709
1cfe7733 2710 if (is_null_sha1(sb.final->object.sha1)) {
1cfe7733 2711 o = sb.final->util;
5c0b13f8 2712 sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
1cfe7733
JH
2713 sb.final_buf_size = o->file.size;
2714 }
2715 else {
2716 o = get_origin(&sb, sb.final, path);
90064710 2717 if (fill_blob_sha1_and_mode(o))
1cfe7733 2718 die("no such path %s in %s", path, final_commit_name);
cee7f245 2719
3b8a12e8 2720 if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
e5450100 2721 textconv_object(path, o->mode, o->blob_sha1, 1, (char **) &sb.final_buf,
3b8a12e8
AB
2722 &sb.final_buf_size))
2723 ;
2724 else
2725 sb.final_buf = read_sha1_file(o->blob_sha1, &type,
2726 &sb.final_buf_size);
2727
ab43e495
JH
2728 if (!sb.final_buf)
2729 die("Cannot read blob %s for path %s",
2730 sha1_to_hex(o->blob_sha1),
2731 path);
1cfe7733 2732 }
c2e525d9 2733 num_read_blob++;
cee7f245
JH
2734 lno = prepare_lines(&sb);
2735
58dbfa2e
ES
2736 if (lno && !range_list.nr)
2737 string_list_append(&range_list, xstrdup("1"));
2738
52f4d126 2739 anchor = 1;
58dbfa2e
ES
2740 range_set_init(&ranges, range_list.nr);
2741 for (range_i = 0; range_i < range_list.nr; ++range_i) {
2742 long bottom, top;
2743 if (parse_range_arg(range_list.items[range_i].string,
52f4d126 2744 nth_line_cb, &sb, lno, anchor,
58dbfa2e
ES
2745 &bottom, &top, sb.path))
2746 usage(blame_usage);
2747 if (lno < top || ((lno || bottom) && lno < bottom))
2748 die("file %s has only %lu lines", path, lno);
2749 if (bottom < 1)
2750 bottom = 1;
2751 if (top < 1)
2752 top = lno;
2753 bottom--;
2754 range_set_append_unsafe(&ranges, bottom, top);
52f4d126 2755 anchor = top + 1;
58dbfa2e
ES
2756 }
2757 sort_and_merge_range_set(&ranges);
2758
2759 for (range_i = ranges.nr; range_i > 0; --range_i) {
2760 const struct range *r = &ranges.ranges[range_i - 1];
2761 long bottom = r->start;
2762 long top = r->end;
2763 struct blame_entry *next = ent;
2764 ent = xcalloc(1, sizeof(*ent));
2765 ent->lno = bottom;
2766 ent->num_lines = top - bottom;
2767 ent->suspect = o;
2768 ent->s_lno = bottom;
2769 ent->next = next;
58dbfa2e
ES
2770 origin_incref(o);
2771 }
7e6ac6e4
DK
2772
2773 o->suspects = ent;
2774 prio_queue_put(&sb.commits, o->commit);
2775
58dbfa2e
ES
2776 origin_decref(o);
2777
2778 range_set_release(&ranges);
2779 string_list_clear(&range_list, 0);
cee7f245 2780
7e6ac6e4 2781 sb.ent = NULL;
cee7f245
JH
2782 sb.path = path;
2783
d551a488 2784 read_mailmap(&mailmap, NULL);
f9567384 2785
b92565dc
MH
2786 if (!incremental)
2787 setup_pager();
2788
85af7929 2789 assign_blame(&sb, opt);
cee7f245 2790
a46442f1
LF
2791 free(final_commit_name);
2792
717d1462
LT
2793 if (incremental)
2794 return 0;
2795
7e6ac6e4
DK
2796 sb.ent = blame_sort(sb.ent, compare_blame_final);
2797
cee7f245
JH
2798 coalesce(&sb);
2799
2800 if (!(output_option & OUTPUT_PORCELAIN))
2801 find_alignment(&sb, &output_option);
2802
2803 output(&sb, output_option);
2804 free((void *)sb.final_buf);
2805 for (ent = sb.ent; ent; ) {
2806 struct blame_entry *e = ent->next;
2807 free(ent);
2808 ent = e;
2809 }
c2e525d9 2810
870b39c1 2811 if (show_stats) {
c2e525d9
JH
2812 printf("num read blob: %d\n", num_read_blob);
2813 printf("num get patch: %d\n", num_get_patch);
2814 printf("num commits: %d\n", num_commits);
2815 }
cee7f245
JH
2816 return 0;
2817}