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