blob: 21f42b0b62b81b637f1cc9589cd6c0306a93d05e [file] [log] [blame]
Junio C Hamanocee7f242006-10-19 16:00:04 -07001/*
Eugene Letuchy31653c12009-02-20 14:51:11 -08002 * Blame
Junio C Hamanocee7f242006-10-19 16:00:04 -07003 *
David Kastrup7e6ac6e2014-04-26 01:56:49 +02004 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
Junio C Hamanocee7f242006-10-19 16:00:04 -07006 */
7
8#include "cache.h"
Michael Haggertyfb58c8d2015-06-22 16:03:05 +02009#include "refs.h"
Junio C Hamanocee7f242006-10-19 16:00:04 -070010#include "builtin.h"
11#include "blob.h"
12#include "commit.h"
13#include "tag.h"
14#include "tree-walk.h"
15#include "diff.h"
16#include "diffcore.h"
17#include "revision.h"
Linus Torvalds717d1462007-01-28 01:34:06 -080018#include "quote.h"
Junio C Hamanocee7f242006-10-19 16:00:04 -070019#include "xdiff-interface.h"
Junio C Hamano1cfe7732007-01-30 01:11:08 -080020#include "cache-tree.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +010021#include "string-list.h"
Junio C Hamanof9567382007-04-27 00:42:15 -070022#include "mailmap.h"
David Kastrup7e6ac6e2014-04-26 01:56:49 +020023#include "mergesort.h"
Pierre Habouzit5817da02008-07-08 15:19:34 +020024#include "parse-options.h"
David Kastrup7e6ac6e2014-04-26 01:56:49 +020025#include "prio-queue.h"
Geoffrey Thomasffaf9cc2009-01-30 04:41:29 -050026#include "utf8.h"
Axel Bonnet3b8a12e2010-06-15 15:58:48 +020027#include "userdiff.h"
Bo Yang25ed3412013-03-28 17:47:30 +010028#include "line-range.h"
Eric Sunshine58dbfa22013-08-06 09:59:38 -040029#include "line-log.h"
René Scharfedbe44fa2015-05-19 23:44:23 +020030#include "dir.h"
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -060031#include "progress.h"
Junio C Hamanocee7f242006-10-19 16:00:04 -070032
Alex Henriece417202015-04-02 15:26:56 -060033static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
Pierre Habouzit5817da02008-07-08 15:19:34 +020034
35static const char *blame_opt_usage[] = {
36 blame_usage,
37 "",
Alex Henrie9c9b4f22015-01-13 00:44:47 -070038 N_("<rev-opts> are documented in git-rev-list(1)"),
Pierre Habouzit5817da02008-07-08 15:19:34 +020039 NULL
40};
Junio C Hamanocee7f242006-10-19 16:00:04 -070041
42static int longest_file;
43static int longest_author;
44static int max_orig_digits;
45static int max_digits;
Junio C Hamano5ff62c32006-10-20 14:51:12 -070046static int max_score_digits;
Junio C Hamano4c10a5c2006-12-18 14:04:38 -080047static int show_root;
Junio C Hamano85af7922008-04-02 22:17:53 -070048static int reverse;
Junio C Hamano4c10a5c2006-12-18 14:04:38 -080049static int blank_boundary;
Linus Torvalds717d1462007-01-28 01:34:06 -080050static int incremental;
René Scharfe582aa002010-05-02 15:04:41 +020051static int xdl_opts;
Namhyung Kim84393bf2011-04-06 11:20:50 +090052static int abbrev = -1;
Junio C Hamano3d1aa562012-09-21 13:52:25 -070053static int no_whole_file_rename;
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -060054static int show_progress;
Eugene Letuchy31653c12009-02-20 14:51:11 -080055
Jeff Kinga5481a62015-06-25 12:55:02 -040056static struct date_mode blame_date_mode = { DATE_ISO8601 };
Eugene Letuchy31653c12009-02-20 14:51:11 -080057static size_t blame_date_width;
58
Johannes Schindelinc455c872008-07-21 19:03:49 +010059static struct string_list mailmap;
Junio C Hamanocee7f242006-10-19 16:00:04 -070060
Junio C Hamano54a4c612006-10-29 03:07:40 -080061#ifndef DEBUG
62#define DEBUG 0
63#endif
64
Junio C Hamanoc2e525d2006-11-05 11:51:41 -080065/* stats */
66static int num_read_blob;
67static int num_get_patch;
68static int num_commits;
69
Junio C Hamanod24bba82006-10-19 18:49:30 -070070#define PICKAXE_BLAME_MOVE 01
Junio C Hamano18abd742006-10-19 18:50:17 -070071#define PICKAXE_BLAME_COPY 02
72#define PICKAXE_BLAME_COPY_HARDER 04
Junio C Hamanoc63777c2007-05-05 21:18:57 -070073#define PICKAXE_BLAME_COPY_HARDEST 010
Junio C Hamanod24bba82006-10-19 18:49:30 -070074
Junio C Hamano4a0fc952006-10-20 15:37:12 -070075/*
76 * blame for a blame_entry with score lower than these thresholds
77 * is not passed to the parent using move/copy logic.
78 */
79static unsigned blame_move_score;
80static unsigned blame_copy_score;
81#define BLAME_DEFAULT_MOVE_SCORE 20
82#define BLAME_DEFAULT_COPY_SCORE 40
83
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070084/* Remember to update object flag allocation in object.h */
Junio C Hamanocee7f242006-10-19 16:00:04 -070085#define METAINFO_SHOWN (1u<<12)
86#define MORE_THAN_ONE_PATH (1u<<13)
87
88/*
Junio C Hamano54a4c612006-10-29 03:07:40 -080089 * One blob in a commit that is being suspected
Junio C Hamanocee7f242006-10-19 16:00:04 -070090 */
91struct origin {
Junio C Hamano54a4c612006-10-29 03:07:40 -080092 int refcnt;
David Kastrup7e6ac6e2014-04-26 01:56:49 +020093 /* Record preceding blame record for this blob */
Junio C Hamano96e11702008-06-04 22:58:40 -070094 struct origin *previous;
David Kastrup7e6ac6e2014-04-26 01:56:49 +020095 /* origins are put in a list linked via `next' hanging off the
96 * corresponding commit's util field in order to make finding
97 * them fast. The presence in this chain does not count
98 * towards the origin's reference count. It is tempting to
99 * let it count as long as the commit is pending examination,
100 * but even under circumstances where the commit will be
101 * present multiple times in the priority queue of unexamined
102 * commits, processing the first instance will not leave any
103 * work requiring the origin data for the second instance. An
104 * interspersed commit changing that would have to be
105 * preexisting with a different ancestry and with the same
106 * commit date in order to wedge itself between two instances
107 * of the same commit in the priority queue _and_ produce
108 * blame entries relevant for it. While we don't want to let
109 * us get tripped up by this case, it certainly does not seem
110 * worth optimizing for.
111 */
112 struct origin *next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700113 struct commit *commit;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200114 /* `suspects' contains blame entries that may be attributed to
115 * this origin's commit or to parent commits. When a commit
116 * is being processed, all suspects will be moved, either by
117 * assigning them to an origin in a different commit, or by
118 * shipping them to the scoreboard's ent list because they
119 * cannot be attributed to a different commit.
120 */
121 struct blame_entry *suspects;
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800122 mmfile_t file;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700123 unsigned char blob_sha1[20];
Kirill Smelkov90064712010-09-29 15:35:24 +0400124 unsigned mode;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200125 /* guilty gets set when shipping any suspects to the final
126 * blame list instead of other commits
127 */
128 char guilty;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700129 char path[FLEX_ARRAY];
130};
131
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -0600132struct progress_info {
133 struct progress *progress;
134 int blamed_lines;
135};
136
René Scharfe4b4132f2012-05-09 22:23:39 +0200137static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
138 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
139{
140 xpparam_t xpp = {0};
141 xdemitconf_t xecfg = {0};
Ramsay Jones85c20c32012-05-13 22:41:16 +0100142 xdemitcb_t ecb = {NULL};
René Scharfe4b4132f2012-05-09 22:23:39 +0200143
144 xpp.flags = xdl_opts;
145 xecfg.ctxlen = ctxlen;
146 xecfg.hunk_func = hunk_func;
147 ecb.priv = cb_data;
148 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
149}
150
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800151/*
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200152 * Prepare diff_filespec and convert it using diff textconv API
153 * if the textconv driver exists.
154 * Return 1 if the conversion succeeds, 0 otherwise.
155 */
Clément Poulaine5fba602010-06-15 17:50:28 +0200156int textconv_object(const char *path,
Kirill Smelkov90064712010-09-29 15:35:24 +0400157 unsigned mode,
Clément Poulaine5fba602010-06-15 17:50:28 +0200158 const unsigned char *sha1,
Jeff Kinge5450102012-07-28 11:03:01 -0400159 int sha1_valid,
Clément Poulaine5fba602010-06-15 17:50:28 +0200160 char **buf,
161 unsigned long *buf_size)
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200162{
163 struct diff_filespec *df;
164 struct userdiff_driver *textconv;
165
166 df = alloc_filespec(path);
Jeff Kinge5450102012-07-28 11:03:01 -0400167 fill_filespec(df, sha1, sha1_valid, mode);
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200168 textconv = get_textconv(df);
169 if (!textconv) {
170 free_filespec(df);
171 return 0;
172 }
173
174 *buf_size = fill_textconv(textconv, df, buf);
175 free_filespec(df);
176 return 1;
177}
178
179/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800180 * Given an origin, prepare mmfile_t structure to be used by the
181 * diff machinery
182 */
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200183static void fill_origin_blob(struct diff_options *opt,
184 struct origin *o, mmfile_t *file)
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800185{
186 if (!o->file.ptr) {
Nicolas Pitre21666f12007-02-26 14:55:59 -0500187 enum object_type type;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200188 unsigned long file_size;
189
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800190 num_read_blob++;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200191 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
Jeff Kinge5450102012-07-28 11:03:01 -0400192 textconv_object(o->path, o->mode, o->blob_sha1, 1, &file->ptr, &file_size))
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200193 ;
194 else
195 file->ptr = read_sha1_file(o->blob_sha1, &type, &file_size);
196 file->size = file_size;
197
Junio C Hamanoab43e492007-08-25 01:26:20 -0700198 if (!file->ptr)
199 die("Cannot read blob %s for path %s",
200 sha1_to_hex(o->blob_sha1),
201 o->path);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800202 o->file = *file;
203 }
204 else
205 *file = o->file;
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800206}
207
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800208/*
209 * Origin is refcounted and usually we keep the blob contents to be
210 * reused.
211 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800212static inline struct origin *origin_incref(struct origin *o)
213{
214 if (o)
215 o->refcnt++;
216 return o;
217}
218
219static void origin_decref(struct origin *o)
220{
221 if (o && --o->refcnt <= 0) {
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200222 struct origin *p, *l = NULL;
Junio C Hamano96e11702008-06-04 22:58:40 -0700223 if (o->previous)
224 origin_decref(o->previous);
Jim Meyering8e0f7002008-01-31 18:26:32 +0100225 free(o->file.ptr);
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200226 /* Should be present exactly once in commit chain */
227 for (p = o->commit->util; p; l = p, p = p->next) {
228 if (p == o) {
229 if (l)
230 l->next = p->next;
231 else
232 o->commit->util = p->next;
233 free(o);
234 return;
235 }
236 }
237 die("internal error in blame::origin_decref");
Junio C Hamano54a4c612006-10-29 03:07:40 -0800238 }
239}
240
Junio C Hamano7c3c7962007-12-11 16:05:50 -0800241static void drop_origin_blob(struct origin *o)
242{
243 if (o->file.ptr) {
244 free(o->file.ptr);
245 o->file.ptr = NULL;
246 }
247}
248
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800249/*
250 * Each group of lines is described by a blame_entry; it can be split
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200251 * as we pass blame to the parents. They are arranged in linked lists
252 * kept as `suspects' of some unprocessed origin, or entered (when the
253 * blame origin has been finalized) into the scoreboard structure.
254 * While the scoreboard structure is only sorted at the end of
255 * processing (according to final image line number), the lists
256 * attached to an origin are sorted by the target line number.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800257 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700258struct blame_entry {
Junio C Hamanocee7f242006-10-19 16:00:04 -0700259 struct blame_entry *next;
260
261 /* the first line of this group in the final image;
262 * internally all line numbers are 0 based.
263 */
264 int lno;
265
266 /* how many lines this group has */
267 int num_lines;
268
269 /* the commit that introduced this group into the final image */
270 struct origin *suspect;
271
Junio C Hamanocee7f242006-10-19 16:00:04 -0700272 /* the line number of the first line of this group in the
273 * suspect's file; internally all line numbers are 0 based.
274 */
275 int s_lno;
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700276
277 /* how significant this entry is -- cached to avoid
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800278 * scanning the lines over and over.
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700279 */
280 unsigned score;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700281};
282
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800283/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200284 * Any merge of blames happens on lists of blames that arrived via
285 * different parents in a single suspect. In this case, we want to
286 * sort according to the suspect line numbers as opposed to the final
287 * image line numbers. The function body is somewhat longish because
288 * it avoids unnecessary writes.
289 */
290
291static struct blame_entry *blame_merge(struct blame_entry *list1,
292 struct blame_entry *list2)
293{
294 struct blame_entry *p1 = list1, *p2 = list2,
295 **tail = &list1;
296
297 if (!p1)
298 return p2;
299 if (!p2)
300 return p1;
301
302 if (p1->s_lno <= p2->s_lno) {
303 do {
304 tail = &p1->next;
305 if ((p1 = *tail) == NULL) {
306 *tail = p2;
307 return list1;
308 }
309 } while (p1->s_lno <= p2->s_lno);
310 }
311 for (;;) {
312 *tail = p2;
313 do {
314 tail = &p2->next;
315 if ((p2 = *tail) == NULL) {
316 *tail = p1;
317 return list1;
318 }
319 } while (p1->s_lno > p2->s_lno);
320 *tail = p1;
321 do {
322 tail = &p1->next;
323 if ((p1 = *tail) == NULL) {
324 *tail = p2;
325 return list1;
326 }
327 } while (p1->s_lno <= p2->s_lno);
328 }
329}
330
331static void *get_next_blame(const void *p)
332{
333 return ((struct blame_entry *)p)->next;
334}
335
336static void set_next_blame(void *p1, void *p2)
337{
338 ((struct blame_entry *)p1)->next = p2;
339}
340
341/*
342 * Final image line numbers are all different, so we don't need a
343 * three-way comparison here.
344 */
345
346static int compare_blame_final(const void *p1, const void *p2)
347{
348 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
349 ? 1 : -1;
350}
351
352static int compare_blame_suspect(const void *p1, const void *p2)
353{
354 const struct blame_entry *s1 = p1, *s2 = p2;
355 /*
356 * to allow for collating suspects, we sort according to the
357 * respective pointer value as the primary sorting criterion.
358 * The actual relation is pretty unimportant as long as it
359 * establishes a total order. Comparing as integers gives us
360 * that.
361 */
362 if (s1->suspect != s2->suspect)
363 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
364 if (s1->s_lno == s2->s_lno)
365 return 0;
366 return s1->s_lno > s2->s_lno ? 1 : -1;
367}
368
369static struct blame_entry *blame_sort(struct blame_entry *head,
370 int (*compare_fn)(const void *, const void *))
371{
372 return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn);
373}
374
375static int compare_commits_by_reverse_commit_date(const void *a,
376 const void *b,
377 void *c)
378{
379 return -compare_commits_by_commit_date(a, b, c);
380}
381
382/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800383 * The current state of the blame assignment.
384 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700385struct scoreboard {
386 /* the final commit (i.e. where we started digging from) */
387 struct commit *final;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200388 /* Priority queue for commits with unassigned blame records */
389 struct prio_queue commits;
Junio C Hamano85af7922008-04-02 22:17:53 -0700390 struct rev_info *revs;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700391 const char *path;
392
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800393 /*
394 * The contents in the final image.
395 * Used by many functions to obtain contents of the nth line,
396 * indexed with scoreboard.lineno[blame_entry.lno].
Junio C Hamanocee7f242006-10-19 16:00:04 -0700397 */
398 const char *final_buf;
399 unsigned long final_buf_size;
400
401 /* linked list of blames */
402 struct blame_entry *ent;
403
Junio C Hamano612702e2006-10-20 23:49:31 -0700404 /* look-up a line in the final buffer */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700405 int num_lines;
406 int *lineno;
407};
408
Junio C Hamano54a4c612006-10-29 03:07:40 -0800409static void sanity_check_refcnt(struct scoreboard *);
410
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800411/*
412 * If two blame entries that are next to each other came from
413 * contiguous lines in the same origin (i.e. <commit, path> pair),
414 * merge them together.
415 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700416static void coalesce(struct scoreboard *sb)
417{
418 struct blame_entry *ent, *next;
419
420 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
David Kastrup0a88f082014-01-22 01:20:15 +0100421 if (ent->suspect == next->suspect &&
Junio C Hamanocee7f242006-10-19 16:00:04 -0700422 ent->s_lno + ent->num_lines == next->s_lno) {
423 ent->num_lines += next->num_lines;
424 ent->next = next->next;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800425 origin_decref(next->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700426 free(next);
Junio C Hamano46014762006-10-21 00:41:38 -0700427 ent->score = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700428 next = ent; /* again */
429 }
430 }
Junio C Hamano54a4c612006-10-29 03:07:40 -0800431
432 if (DEBUG) /* sanity */
433 sanity_check_refcnt(sb);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700434}
435
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800436/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200437 * Merge the given sorted list of blames into a preexisting origin.
438 * If there were no previous blames to that commit, it is entered into
439 * the commit priority queue of the score board.
440 */
441
442static void queue_blames(struct scoreboard *sb, struct origin *porigin,
443 struct blame_entry *sorted)
444{
445 if (porigin->suspects)
446 porigin->suspects = blame_merge(porigin->suspects, sorted);
447 else {
448 struct origin *o;
449 for (o = porigin->commit->util; o; o = o->next) {
450 if (o->suspects) {
451 porigin->suspects = sorted;
452 return;
453 }
454 }
455 porigin->suspects = sorted;
456 prio_queue_put(&sb->commits, porigin->commit);
457 }
458}
459
460/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800461 * Given a commit and a path in it, create a new origin structure.
462 * The callers that add blame to the scoreboard should use
463 * get_origin() to obtain shared, refcounted copy instead of calling
464 * this function directly.
465 */
Junio C Hamano854b97f2006-11-04 19:18:50 -0800466static struct origin *make_origin(struct commit *commit, const char *path)
467{
468 struct origin *o;
Jeff King96ffc062016-02-22 17:44:32 -0500469 FLEX_ALLOC_STR(o, path, path);
Junio C Hamano854b97f2006-11-04 19:18:50 -0800470 o->commit = commit;
471 o->refcnt = 1;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200472 o->next = commit->util;
473 commit->util = o;
Junio C Hamano854b97f2006-11-04 19:18:50 -0800474 return o;
475}
476
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800477/*
478 * Locate an existing origin or create a new one.
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200479 * This moves the origin to front position in the commit util list.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800480 */
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700481static struct origin *get_origin(struct scoreboard *sb,
482 struct commit *commit,
483 const char *path)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700484{
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200485 struct origin *o, *l;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700486
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200487 for (o = commit->util, l = NULL; o; l = o, o = o->next) {
488 if (!strcmp(o->path, path)) {
489 /* bump to front */
490 if (l) {
491 l->next = o->next;
492 o->next = commit->util;
493 commit->util = o;
494 }
495 return origin_incref(o);
496 }
Junio C Hamanocee7f242006-10-19 16:00:04 -0700497 }
Junio C Hamano854b97f2006-11-04 19:18:50 -0800498 return make_origin(commit, path);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700499}
500
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800501/*
502 * Fill the blob_sha1 field of an origin if it hasn't, so that later
503 * call to fill_origin_blob() can use it to locate the data. blob_sha1
504 * for an origin is also used to pass the blame for the entire file to
505 * the parent to detect the case where a child's blob is identical to
506 * that of its parent's.
Kirill Smelkov90064712010-09-29 15:35:24 +0400507 *
508 * This also fills origin->mode for corresponding tree path.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800509 */
Kirill Smelkov90064712010-09-29 15:35:24 +0400510static int fill_blob_sha1_and_mode(struct origin *origin)
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700511{
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700512 if (!is_null_sha1(origin->blob_sha1))
513 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000514 if (get_tree_entry(origin->commit->object.oid.hash,
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700515 origin->path,
Kirill Smelkov90064712010-09-29 15:35:24 +0400516 origin->blob_sha1, &origin->mode))
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700517 goto error_out;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500518 if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700519 goto error_out;
520 return 0;
521 error_out:
522 hashclr(origin->blob_sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +0400523 origin->mode = S_IFINVALID;
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700524 return -1;
525}
526
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800527/*
528 * We have an origin -- check if the same path exists in the
529 * parent and return an origin structure to represent it.
530 */
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700531static struct origin *find_origin(struct scoreboard *sb,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700532 struct commit *parent,
533 struct origin *origin)
534{
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200535 struct origin *porigin;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700536 struct diff_options diff_opts;
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700537 const char *paths[2];
538
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200539 /* First check any existing origins */
540 for (porigin = parent->util; porigin; porigin = porigin->next)
541 if (!strcmp(porigin->path, origin->path)) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800542 /*
543 * The same path between origin and its parent
544 * without renaming -- the most common case.
545 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200546 return origin_incref (porigin);
Junio C Hamano854b97f2006-11-04 19:18:50 -0800547 }
Junio C Hamano0d981c62006-10-31 01:00:01 -0800548
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700549 /* See if the origin->path is different between parent
550 * and origin first. Most of the time they are the
551 * same and diff-tree is fairly efficient about this.
552 */
553 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100554 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700555 diff_opts.detect_rename = 0;
556 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
557 paths[0] = origin->path;
558 paths[1] = NULL;
559
Nguyễn Thái Ngọc Duy4a2d5ae2013-10-26 09:09:20 +0700560 parse_pathspec(&diff_opts.pathspec,
561 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
562 PATHSPEC_LITERAL_PATH, "", paths);
Thomas Rast28452652012-08-03 14:16:24 +0200563 diff_setup_done(&diff_opts);
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800564
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000565 if (is_null_oid(&origin->commit->object.oid))
brian m. carlsoned1c9972015-11-10 02:22:29 +0000566 do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800567 else
brian m. carlsoned1c9972015-11-10 02:22:29 +0000568 diff_tree_sha1(parent->tree->object.oid.hash,
569 origin->commit->tree->object.oid.hash,
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800570 "", &diff_opts);
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700571 diffcore_std(&diff_opts);
572
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700573 if (!diff_queued_diff.nr) {
574 /* The path is the same as parent */
575 porigin = get_origin(sb, parent, origin->path);
576 hashcpy(porigin->blob_sha1, origin->blob_sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +0400577 porigin->mode = origin->mode;
Junio C Hamanoa9b2d422009-06-03 00:43:22 -0700578 } else {
579 /*
580 * Since origin->path is a pathspec, if the parent
581 * commit had it as a directory, we will see a whole
582 * bunch of deletion of files in the directory that we
583 * do not care about.
584 */
585 int i;
586 struct diff_filepair *p = NULL;
587 for (i = 0; i < diff_queued_diff.nr; i++) {
588 const char *name;
589 p = diff_queued_diff.queue[i];
590 name = p->one->path ? p->one->path : p->two->path;
591 if (!strcmp(name, origin->path))
592 break;
593 }
594 if (!p)
595 die("internal error in blame::find_origin");
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700596 switch (p->status) {
597 default:
Junio C Hamanoacca6872006-11-08 18:47:54 -0800598 die("internal error in blame::find_origin (%c)",
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700599 p->status);
600 case 'M':
601 porigin = get_origin(sb, parent, origin->path);
602 hashcpy(porigin->blob_sha1, p->one->sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +0400603 porigin->mode = p->one->mode;
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700604 break;
605 case 'A':
606 case 'T':
607 /* Did not exist in parent, or type changed */
608 break;
609 }
610 }
611 diff_flush(&diff_opts);
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700612 free_pathspec(&diff_opts.pathspec);
Junio C Hamanof69e7432006-10-30 17:17:41 -0800613 return porigin;
614}
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700615
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800616/*
617 * We have an origin -- find the path that corresponds to it in its
618 * parent and return an origin structure to represent it.
619 */
Junio C Hamanof69e7432006-10-30 17:17:41 -0800620static struct origin *find_rename(struct scoreboard *sb,
621 struct commit *parent,
622 struct origin *origin)
623{
624 struct origin *porigin = NULL;
625 struct diff_options diff_opts;
626 int i;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700627
628 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100629 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700630 diff_opts.detect_rename = DIFF_DETECT_RENAME;
631 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800632 diff_opts.single_follow = origin->path;
Thomas Rast28452652012-08-03 14:16:24 +0200633 diff_setup_done(&diff_opts);
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800634
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000635 if (is_null_oid(&origin->commit->object.oid))
brian m. carlsoned1c9972015-11-10 02:22:29 +0000636 do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800637 else
brian m. carlsoned1c9972015-11-10 02:22:29 +0000638 diff_tree_sha1(parent->tree->object.oid.hash,
639 origin->commit->tree->object.oid.hash,
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800640 "", &diff_opts);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700641 diffcore_std(&diff_opts);
642
643 for (i = 0; i < diff_queued_diff.nr; i++) {
644 struct diff_filepair *p = diff_queued_diff.queue[i];
Junio C Hamano612702e2006-10-20 23:49:31 -0700645 if ((p->status == 'R' || p->status == 'C') &&
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700646 !strcmp(p->two->path, origin->path)) {
647 porigin = get_origin(sb, parent, p->one->path);
648 hashcpy(porigin->blob_sha1, p->one->sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +0400649 porigin->mode = p->one->mode;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700650 break;
651 }
652 }
653 diff_flush(&diff_opts);
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +0700654 free_pathspec(&diff_opts.pathspec);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700655 return porigin;
656}
657
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800658/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200659 * Append a new blame entry to a given output queue.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800660 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200661static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700662{
Junio C Hamano54a4c612006-10-29 03:07:40 -0800663 origin_incref(e->suspect);
664
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200665 e->next = **queue;
666 **queue = e;
667 *queue = &e->next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700668}
669
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800670/*
671 * src typically is on-stack; we want to copy the information in it to
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200672 * a malloced blame_entry that gets added to the given queue. The
673 * origin of dst loses a refcnt.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800674 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200675static void dup_entry(struct blame_entry ***queue,
676 struct blame_entry *dst, struct blame_entry *src)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700677{
Junio C Hamano54a4c612006-10-29 03:07:40 -0800678 origin_incref(src->suspect);
679 origin_decref(dst->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700680 memcpy(dst, src, sizeof(*src));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200681 dst->next = **queue;
682 **queue = dst;
683 *queue = &dst->next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700684}
685
Bo Yang25ed3412013-03-28 17:47:30 +0100686static const char *nth_line(struct scoreboard *sb, long lno)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700687{
688 return sb->final_buf + sb->lineno[lno];
689}
690
Bo Yang25ed3412013-03-28 17:47:30 +0100691static const char *nth_line_cb(void *data, long lno)
692{
693 return nth_line((struct scoreboard *)data, lno);
694}
695
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800696/*
697 * It is known that lines between tlno to same came from parent, and e
698 * has an overlap with that range. it also is known that parent's
699 * line plno corresponds to e's line tlno.
700 *
701 * <---- e ----->
702 * <------>
703 * <------------>
704 * <------------>
705 * <------------------>
706 *
707 * Split e into potentially three parts; before this chunk, the chunk
708 * to be blamed for the parent, and after that portion.
709 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800710static void split_overlap(struct blame_entry *split,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700711 struct blame_entry *e,
712 int tlno, int plno, int same,
713 struct origin *parent)
714{
Junio C Hamanocee7f242006-10-19 16:00:04 -0700715 int chunk_end_lno;
716 memset(split, 0, sizeof(struct blame_entry [3]));
717
718 if (e->s_lno < tlno) {
719 /* there is a pre-chunk part not blamed on parent */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800720 split[0].suspect = origin_incref(e->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700721 split[0].lno = e->lno;
722 split[0].s_lno = e->s_lno;
723 split[0].num_lines = tlno - e->s_lno;
724 split[1].lno = e->lno + tlno - e->s_lno;
725 split[1].s_lno = plno;
726 }
727 else {
728 split[1].lno = e->lno;
729 split[1].s_lno = plno + (e->s_lno - tlno);
730 }
731
732 if (same < e->s_lno + e->num_lines) {
733 /* there is a post-chunk part not blamed on parent */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800734 split[2].suspect = origin_incref(e->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700735 split[2].lno = e->lno + (same - e->s_lno);
736 split[2].s_lno = e->s_lno + (same - e->s_lno);
737 split[2].num_lines = e->s_lno + e->num_lines - same;
738 chunk_end_lno = split[2].lno;
739 }
740 else
741 chunk_end_lno = e->lno + e->num_lines;
742 split[1].num_lines = chunk_end_lno - split[1].lno;
743
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800744 /*
745 * if it turns out there is nothing to blame the parent for,
746 * forget about the splitting. !split[1].suspect signals this.
747 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700748 if (split[1].num_lines < 1)
749 return;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800750 split[1].suspect = origin_incref(parent);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700751}
752
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800753/*
754 * split_overlap() divided an existing blame e into up to three parts
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200755 * in split. Any assigned blame is moved to queue to
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800756 * reflect the split.
757 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200758static void split_blame(struct blame_entry ***blamed,
759 struct blame_entry ***unblamed,
Junio C Hamano54a4c612006-10-29 03:07:40 -0800760 struct blame_entry *split,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700761 struct blame_entry *e)
762{
763 struct blame_entry *new_entry;
764
765 if (split[0].suspect && split[2].suspect) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800766 /* The first part (reuse storage for the existing entry e) */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200767 dup_entry(unblamed, e, &split[0]);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700768
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800769 /* The last part -- me */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700770 new_entry = xmalloc(sizeof(*new_entry));
771 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200772 add_blame_entry(unblamed, new_entry);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700773
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800774 /* ... and the middle part -- parent */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700775 new_entry = xmalloc(sizeof(*new_entry));
776 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200777 add_blame_entry(blamed, new_entry);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700778 }
779 else if (!split[0].suspect && !split[2].suspect)
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800780 /*
781 * The parent covers the entire area; reuse storage for
782 * e and replace it with the parent.
783 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200784 dup_entry(blamed, e, &split[1]);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700785 else if (split[0].suspect) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800786 /* me and then parent */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200787 dup_entry(unblamed, e, &split[0]);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700788
789 new_entry = xmalloc(sizeof(*new_entry));
790 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200791 add_blame_entry(blamed, new_entry);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700792 }
793 else {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800794 /* parent and then me */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200795 dup_entry(blamed, e, &split[1]);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700796
797 new_entry = xmalloc(sizeof(*new_entry));
798 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200799 add_blame_entry(unblamed, new_entry);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700800 }
801}
802
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800803/*
804 * After splitting the blame, the origins used by the
805 * on-stack blame_entry should lose one refcnt each.
806 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800807static void decref_split(struct blame_entry *split)
808{
809 int i;
810
811 for (i = 0; i < 3; i++)
812 origin_decref(split[i].suspect);
813}
814
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800815/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200816 * reverse_blame reverses the list given in head, appending tail.
817 * That allows us to build lists in reverse order, then reverse them
818 * afterwards. This can be faster than building the list in proper
819 * order right away. The reason is that building in proper order
820 * requires writing a link in the _previous_ element, while building
821 * in reverse order just requires placing the list head into the
822 * _current_ element.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800823 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200824
825static struct blame_entry *reverse_blame(struct blame_entry *head,
826 struct blame_entry *tail)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700827{
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200828 while (head) {
829 struct blame_entry *next = head->next;
830 head->next = tail;
831 tail = head;
832 head = next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700833 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200834 return tail;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700835}
836
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800837/*
838 * Process one hunk from the patch between the current suspect for
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200839 * blame_entry e and its parent. This first blames any unfinished
840 * entries before the chunk (which is where target and parent start
841 * differing) on the parent, and then splits blame entries at the
842 * start and at the end of the difference region. Since use of -M and
843 * -C options may lead to overlapping/duplicate source line number
844 * ranges, all we can rely on from sorting/merging is the order of the
845 * first suspect line number.
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800846 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200847static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
848 int tlno, int offset, int same,
849 struct origin *parent)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700850{
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200851 struct blame_entry *e = **srcq;
852 struct blame_entry *samep = NULL, *diffp = NULL;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700853
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200854 while (e && e->s_lno < tlno) {
855 struct blame_entry *next = e->next;
856 /*
857 * current record starts before differing portion. If
858 * it reaches into it, we need to split it up and
859 * examine the second part separately.
860 */
861 if (e->s_lno + e->num_lines > tlno) {
862 /* Move second half to a new record */
863 int len = tlno - e->s_lno;
864 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
865 n->suspect = e->suspect;
866 n->lno = e->lno + len;
867 n->s_lno = e->s_lno + len;
868 n->num_lines = e->num_lines - len;
869 e->num_lines = len;
870 e->score = 0;
871 /* Push new record to diffp */
872 n->next = diffp;
873 diffp = n;
874 } else
875 origin_decref(e->suspect);
876 /* Pass blame for everything before the differing
877 * chunk to the parent */
878 e->suspect = origin_incref(parent);
879 e->s_lno += offset;
880 e->next = samep;
881 samep = e;
882 e = next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700883 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200884 /*
885 * As we don't know how much of a common stretch after this
886 * diff will occur, the currently blamed parts are all that we
887 * can assign to the parent for now.
888 */
889
890 if (samep) {
891 **dstq = reverse_blame(samep, **dstq);
892 *dstq = &samep->next;
893 }
894 /*
895 * Prepend the split off portions: everything after e starts
896 * after the blameable portion.
897 */
898 e = reverse_blame(diffp, e);
899
900 /*
901 * Now retain records on the target while parts are different
902 * from the parent.
903 */
904 samep = NULL;
905 diffp = NULL;
906 while (e && e->s_lno < same) {
907 struct blame_entry *next = e->next;
908
909 /*
910 * If current record extends into sameness, need to split.
911 */
912 if (e->s_lno + e->num_lines > same) {
913 /*
914 * Move second half to a new record to be
915 * processed by later chunks
916 */
917 int len = same - e->s_lno;
918 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
919 n->suspect = origin_incref(e->suspect);
920 n->lno = e->lno + len;
921 n->s_lno = e->s_lno + len;
922 n->num_lines = e->num_lines - len;
923 e->num_lines = len;
924 e->score = 0;
925 /* Push new record to samep */
926 n->next = samep;
927 samep = n;
928 }
929 e->next = diffp;
930 diffp = e;
931 e = next;
932 }
933 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
934 /* Move across elements that are in the unblamable portion */
935 if (diffp)
936 *srcq = &diffp->next;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700937}
938
René Scharfecdcab132008-10-25 15:31:36 +0200939struct blame_chunk_cb_data {
René Scharfecdcab132008-10-25 15:31:36 +0200940 struct origin *parent;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200941 long offset;
942 struct blame_entry **dstq;
943 struct blame_entry **srcq;
René Scharfecdcab132008-10-25 15:31:36 +0200944};
945
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200946/* diff chunks are from parent to target */
René Scharfe0af596c2012-05-09 22:21:56 +0200947static int blame_chunk_cb(long start_a, long count_a,
948 long start_b, long count_b, void *data)
René Scharfecdcab132008-10-25 15:31:36 +0200949{
950 struct blame_chunk_cb_data *d = data;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200951 if (start_a - start_b != d->offset)
952 die("internal error in blame::blame_chunk_cb");
953 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
954 start_b + count_b, d->parent);
955 d->offset = start_a + count_a - (start_b + count_b);
René Scharfe0af596c2012-05-09 22:21:56 +0200956 return 0;
René Scharfecdcab132008-10-25 15:31:36 +0200957}
958
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800959/*
960 * We are looking at the origin 'target' and aiming to pass blame
961 * for the lines it is suspected to its parent. Run diff to find
962 * which lines came from parent and pass blame for them.
963 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200964static void pass_blame_to_parent(struct scoreboard *sb,
965 struct origin *target,
966 struct origin *parent)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700967{
René Scharfe7c4ed8c2008-10-25 15:30:22 +0200968 mmfile_t file_p, file_o;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000969 struct blame_chunk_cb_data d;
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200970 struct blame_entry *newdest = NULL;
René Scharfe0af596c2012-05-09 22:21:56 +0200971
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200972 if (!target->suspects)
973 return; /* nothing remains for this target */
974
975 d.parent = parent;
976 d.offset = 0;
977 d.dstq = &newdest; d.srcq = &target->suspects;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700978
Axel Bonnet3b8a12e2010-06-15 15:58:48 +0200979 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
980 fill_origin_blob(&sb->revs->diffopt, target, &file_o);
René Scharfe7c4ed8c2008-10-25 15:30:22 +0200981 num_get_patch++;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700982
Jeff King3efb9882015-09-24 19:12:23 -0400983 if (diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d))
984 die("unable to generate diff (%s -> %s)",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000985 oid_to_hex(&parent->commit->object.oid),
986 oid_to_hex(&target->commit->object.oid));
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200987 /* The rest are the same as the parent */
988 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
989 *d.dstq = NULL;
990 queue_blames(sb, parent, newdest);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700991
David Kastrup7e6ac6e2014-04-26 01:56:49 +0200992 return;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700993}
994
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800995/*
996 * The lines in blame_entry after splitting blames many times can become
997 * very small and trivial, and at some point it becomes pointless to
998 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
999 * ordinary C program, and it is not worth to say it was copied from
1000 * totally unrelated file in the parent.
1001 *
1002 * Compute how trivial the lines in the blame_entry are.
1003 */
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001004static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
1005{
1006 unsigned score;
1007 const char *cp, *ep;
1008
1009 if (e->score)
1010 return e->score;
1011
Junio C Hamano612702e2006-10-20 23:49:31 -07001012 score = 1;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001013 cp = nth_line(sb, e->lno);
1014 ep = nth_line(sb, e->lno + e->num_lines);
1015 while (cp < ep) {
1016 unsigned ch = *((unsigned char *)cp);
1017 if (isalnum(ch))
1018 score++;
1019 cp++;
1020 }
1021 e->score = score;
1022 return score;
1023}
1024
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001025/*
1026 * best_so_far[] and this[] are both a split of an existing blame_entry
1027 * that passes blame to the parent. Maintain best_so_far the best split
1028 * so far, by comparing this and best_so_far and copying this into
1029 * bst_so_far as needed.
1030 */
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001031static void copy_split_if_better(struct scoreboard *sb,
Junio C Hamano54a4c612006-10-29 03:07:40 -08001032 struct blame_entry *best_so_far,
1033 struct blame_entry *this)
Junio C Hamanod24bba82006-10-19 18:49:30 -07001034{
Junio C Hamano54a4c612006-10-29 03:07:40 -08001035 int i;
1036
Junio C Hamanod24bba82006-10-19 18:49:30 -07001037 if (!this[1].suspect)
1038 return;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001039 if (best_so_far[1].suspect) {
1040 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
1041 return;
1042 }
Junio C Hamano54a4c612006-10-29 03:07:40 -08001043
1044 for (i = 0; i < 3; i++)
1045 origin_incref(this[i].suspect);
1046 decref_split(best_so_far);
Junio C Hamanod24bba82006-10-19 18:49:30 -07001047 memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
1048}
1049
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001050/*
Junio C Hamanodd166aa2007-05-05 09:13:26 -07001051 * We are looking at a part of the final image represented by
1052 * ent (tlno and same are offset by ent->s_lno).
1053 * tlno is where we are looking at in the final image.
1054 * up to (but not including) same match preimage.
1055 * plno is where we are looking at in the preimage.
1056 *
1057 * <-------------- final image ---------------------->
1058 * <------ent------>
1059 * ^tlno ^same
1060 * <---------preimage----->
1061 * ^plno
1062 *
1063 * All line numbers are 0-based.
1064 */
1065static void handle_split(struct scoreboard *sb,
1066 struct blame_entry *ent,
1067 int tlno, int plno, int same,
1068 struct origin *parent,
1069 struct blame_entry *split)
1070{
1071 if (ent->num_lines <= tlno)
1072 return;
1073 if (tlno < same) {
1074 struct blame_entry this[3];
1075 tlno += ent->s_lno;
1076 same += ent->s_lno;
1077 split_overlap(this, ent, tlno, plno, same, parent);
1078 copy_split_if_better(sb, split, this);
1079 decref_split(this);
1080 }
1081}
1082
René Scharfecdcab132008-10-25 15:31:36 +02001083struct handle_split_cb_data {
1084 struct scoreboard *sb;
1085 struct blame_entry *ent;
1086 struct origin *parent;
1087 struct blame_entry *split;
1088 long plno;
1089 long tlno;
1090};
1091
René Scharfe5d23ec72012-05-09 22:22:47 +02001092static int handle_split_cb(long start_a, long count_a,
1093 long start_b, long count_b, void *data)
René Scharfecdcab132008-10-25 15:31:36 +02001094{
1095 struct handle_split_cb_data *d = data;
René Scharfe5d23ec72012-05-09 22:22:47 +02001096 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1097 d->split);
1098 d->plno = start_a + count_a;
1099 d->tlno = start_b + count_b;
1100 return 0;
René Scharfecdcab132008-10-25 15:31:36 +02001101}
1102
Junio C Hamanodd166aa2007-05-05 09:13:26 -07001103/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001104 * Find the lines from parent that are the same as ent so that
1105 * we can pass blames to it. file_p has the blob contents for
1106 * the parent.
1107 */
Junio C Hamanod24bba82006-10-19 18:49:30 -07001108static void find_copy_in_blob(struct scoreboard *sb,
1109 struct blame_entry *ent,
1110 struct origin *parent,
Junio C Hamano54a4c612006-10-29 03:07:40 -08001111 struct blame_entry *split,
Junio C Hamanod24bba82006-10-19 18:49:30 -07001112 mmfile_t *file_p)
1113{
1114 const char *cp;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001115 mmfile_t file_o;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001116 struct handle_split_cb_data d;
René Scharfe5d23ec72012-05-09 22:22:47 +02001117
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +00001118 memset(&d, 0, sizeof(d));
1119 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001120 /*
1121 * Prepare mmfile that contains only the lines in ent.
1122 */
Junio C Hamanod24bba82006-10-19 18:49:30 -07001123 cp = nth_line(sb, ent->lno);
Felipe Contreras4b25d092009-05-01 12:06:36 +03001124 file_o.ptr = (char *) cp;
David Kastrup3ee89442014-02-22 17:02:47 +01001125 file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001126
Junio C Hamanodd166aa2007-05-05 09:13:26 -07001127 /*
1128 * file_o is a part of final image we are annotating.
1129 * file_p partially may match that image.
1130 */
Junio C Hamanod24bba82006-10-19 18:49:30 -07001131 memset(split, 0, sizeof(struct blame_entry [3]));
Jeff King3efb9882015-09-24 19:12:23 -04001132 if (diff_hunks(file_p, &file_o, 1, handle_split_cb, &d))
1133 die("unable to generate diff (%s)",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001134 oid_to_hex(&parent->commit->object.oid));
Junio C Hamanodd166aa2007-05-05 09:13:26 -07001135 /* remainder, if any, all match the preimage */
René Scharfecdcab132008-10-25 15:31:36 +02001136 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
Junio C Hamanod24bba82006-10-19 18:49:30 -07001137}
1138
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001139/* Move all blame entries from list *source that have a score smaller
1140 * than score_min to the front of list *small.
1141 * Returns a pointer to the link pointing to the old head of the small list.
1142 */
1143
1144static struct blame_entry **filter_small(struct scoreboard *sb,
1145 struct blame_entry **small,
1146 struct blame_entry **source,
1147 unsigned score_min)
1148{
1149 struct blame_entry *p = *source;
1150 struct blame_entry *oldsmall = *small;
1151 while (p) {
1152 if (ent_score(sb, p) <= score_min) {
1153 *small = p;
1154 small = &p->next;
1155 p = *small;
1156 } else {
1157 *source = p;
1158 source = &p->next;
1159 p = *source;
1160 }
1161 }
1162 *small = oldsmall;
1163 *source = NULL;
1164 return small;
1165}
1166
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001167/*
1168 * See if lines currently target is suspected for can be attributed to
1169 * parent.
1170 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001171static void find_move_in_parent(struct scoreboard *sb,
1172 struct blame_entry ***blamed,
1173 struct blame_entry **toosmall,
1174 struct origin *target,
1175 struct origin *parent)
Junio C Hamanod24bba82006-10-19 18:49:30 -07001176{
Junio C Hamano46014762006-10-21 00:41:38 -07001177 struct blame_entry *e, split[3];
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001178 struct blame_entry *unblamed = target->suspects;
1179 struct blame_entry *leftover = NULL;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001180 mmfile_t file_p;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001181
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001182 if (!unblamed)
1183 return; /* nothing remains for this target */
Junio C Hamanod24bba82006-10-19 18:49:30 -07001184
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02001185 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001186 if (!file_p.ptr)
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001187 return;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001188
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001189 /* At each iteration, unblamed has a NULL-terminated list of
1190 * entries that have not yet been tested for blame. leftover
1191 * contains the reversed list of entries that have been tested
1192 * without being assignable to the parent.
1193 */
1194 do {
1195 struct blame_entry **unblamedtail = &unblamed;
1196 struct blame_entry *next;
1197 for (e = unblamed; e; e = next) {
1198 next = e->next;
Junio C Hamano650e2f62006-11-04 12:37:02 -08001199 find_copy_in_blob(sb, e, parent, split, &file_p);
1200 if (split[1].suspect &&
1201 blame_move_score < ent_score(sb, &split[1])) {
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001202 split_blame(blamed, &unblamedtail, split, e);
1203 } else {
1204 e->next = leftover;
1205 leftover = e;
Junio C Hamano650e2f62006-11-04 12:37:02 -08001206 }
1207 decref_split(split);
1208 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001209 *unblamedtail = NULL;
1210 toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
1211 } while (unblamed);
1212 target->suspects = reverse_blame(leftover, NULL);
Junio C Hamanod24bba82006-10-19 18:49:30 -07001213}
1214
Junio C Hamano33494782006-11-04 16:39:03 -08001215struct blame_list {
1216 struct blame_entry *ent;
1217 struct blame_entry split[3];
1218};
1219
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001220/*
1221 * Count the number of entries the target is suspected for,
1222 * and prepare a list of entry and the best split.
1223 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001224static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
Junio C Hamano33494782006-11-04 16:39:03 -08001225 int *num_ents_p)
1226{
1227 struct blame_entry *e;
1228 int num_ents, i;
1229 struct blame_list *blame_list = NULL;
1230
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001231 for (e = unblamed, num_ents = 0; e; e = e->next)
1232 num_ents++;
Junio C Hamano33494782006-11-04 16:39:03 -08001233 if (num_ents) {
1234 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001235 for (e = unblamed, i = 0; e; e = e->next)
1236 blame_list[i++].ent = e;
Junio C Hamano33494782006-11-04 16:39:03 -08001237 }
1238 *num_ents_p = num_ents;
1239 return blame_list;
1240}
1241
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001242/*
1243 * For lines target is suspected for, see if we can find code movement
1244 * across file boundary from the parent commit. porigin is the path
1245 * in the parent we already tried.
1246 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001247static void find_copy_in_parent(struct scoreboard *sb,
1248 struct blame_entry ***blamed,
1249 struct blame_entry **toosmall,
1250 struct origin *target,
1251 struct commit *parent,
1252 struct origin *porigin,
1253 int opt)
Junio C Hamano18abd742006-10-19 18:50:17 -07001254{
1255 struct diff_options diff_opts;
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001256 int i, j;
Junio C Hamano33494782006-11-04 16:39:03 -08001257 struct blame_list *blame_list;
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001258 int num_ents;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001259 struct blame_entry *unblamed = target->suspects;
1260 struct blame_entry *leftover = NULL;
Junio C Hamano18abd742006-10-19 18:50:17 -07001261
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001262 if (!unblamed)
1263 return; /* nothing remains for this target */
Junio C Hamano18abd742006-10-19 18:50:17 -07001264
1265 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001266 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamano18abd742006-10-19 18:50:17 -07001267 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1268
Thomas Rast28452652012-08-03 14:16:24 +02001269 diff_setup_done(&diff_opts);
Junio C Hamano33494782006-11-04 16:39:03 -08001270
1271 /* Try "find copies harder" on new path if requested;
1272 * we do not want to use diffcore_rename() actually to
1273 * match things up; find_copies_harder is set only to
1274 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1275 * and this code needs to be after diff_setup_done(), which
1276 * usually makes find-copies-harder imply copy detection.
1277 */
Junio C Hamanoc63777c2007-05-05 21:18:57 -07001278 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1279 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1280 && (!porigin || strcmp(target->path, porigin->path))))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001281 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
Junio C Hamano33494782006-11-04 16:39:03 -08001282
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001283 if (is_null_oid(&target->commit->object.oid))
brian m. carlsoned1c9972015-11-10 02:22:29 +00001284 do_diff_cache(parent->tree->object.oid.hash, &diff_opts);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08001285 else
brian m. carlsoned1c9972015-11-10 02:22:29 +00001286 diff_tree_sha1(parent->tree->object.oid.hash,
1287 target->commit->tree->object.oid.hash,
Junio C Hamano1cfe7732007-01-30 01:11:08 -08001288 "", &diff_opts);
Junio C Hamano18abd742006-10-19 18:50:17 -07001289
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001290 if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
Junio C Hamano33494782006-11-04 16:39:03 -08001291 diffcore_std(&diff_opts);
Junio C Hamanof6c0e192006-10-21 02:56:33 -07001292
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001293 do {
1294 struct blame_entry **unblamedtail = &unblamed;
1295 blame_list = setup_blame_list(unblamed, &num_ents);
Junio C Hamano18abd742006-10-19 18:50:17 -07001296
Junio C Hamano33494782006-11-04 16:39:03 -08001297 for (i = 0; i < diff_queued_diff.nr; i++) {
1298 struct diff_filepair *p = diff_queued_diff.queue[i];
1299 struct origin *norigin;
1300 mmfile_t file_p;
Junio C Hamano33494782006-11-04 16:39:03 -08001301 struct blame_entry this[3];
1302
1303 if (!DIFF_FILE_VALID(p->one))
1304 continue; /* does not exist in parent */
Alexander Gavrilov5209ac42008-10-03 20:23:50 +04001305 if (S_ISGITLINK(p->one->mode))
1306 continue; /* ignore git links */
Junio C Hamano33494782006-11-04 16:39:03 -08001307 if (porigin && !strcmp(p->one->path, porigin->path))
1308 /* find_move already dealt with this path */
1309 continue;
1310
1311 norigin = get_origin(sb, parent, p->one->path);
1312 hashcpy(norigin->blob_sha1, p->one->sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +04001313 norigin->mode = p->one->mode;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02001314 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
Junio C Hamano33494782006-11-04 16:39:03 -08001315 if (!file_p.ptr)
1316 continue;
1317
1318 for (j = 0; j < num_ents; j++) {
1319 find_copy_in_blob(sb, blame_list[j].ent,
1320 norigin, this, &file_p);
1321 copy_split_if_better(sb, blame_list[j].split,
1322 this);
1323 decref_split(this);
1324 }
Junio C Hamano33494782006-11-04 16:39:03 -08001325 origin_decref(norigin);
1326 }
Junio C Hamano18abd742006-10-19 18:50:17 -07001327
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001328 for (j = 0; j < num_ents; j++) {
Junio C Hamano33494782006-11-04 16:39:03 -08001329 struct blame_entry *split = blame_list[j].split;
1330 if (split[1].suspect &&
1331 blame_copy_score < ent_score(sb, &split[1])) {
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001332 split_blame(blamed, &unblamedtail, split,
1333 blame_list[j].ent);
1334 } else {
1335 blame_list[j].ent->next = leftover;
1336 leftover = blame_list[j].ent;
Junio C Hamano33494782006-11-04 16:39:03 -08001337 }
1338 decref_split(split);
Junio C Hamano18abd742006-10-19 18:50:17 -07001339 }
Junio C Hamano33494782006-11-04 16:39:03 -08001340 free(blame_list);
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001341 *unblamedtail = NULL;
1342 toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
1343 } while (unblamed);
1344 target->suspects = reverse_blame(leftover, NULL);
Junio C Hamano18abd742006-10-19 18:50:17 -07001345 diff_flush(&diff_opts);
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001346 free_pathspec(&diff_opts.pathspec);
Junio C Hamano18abd742006-10-19 18:50:17 -07001347}
1348
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001349/*
1350 * The blobs of origin and porigin exactly match, so everything
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001351 * origin is suspected for can be blamed on the parent.
1352 */
1353static void pass_whole_blame(struct scoreboard *sb,
1354 struct origin *origin, struct origin *porigin)
1355{
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001356 struct blame_entry *e, *suspects;
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001357
1358 if (!porigin->file.ptr && origin->file.ptr) {
1359 /* Steal its file */
1360 porigin->file = origin->file;
1361 origin->file.ptr = NULL;
1362 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001363 suspects = origin->suspects;
1364 origin->suspects = NULL;
1365 for (e = suspects; e; e = e->next) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001366 origin_incref(porigin);
1367 origin_decref(e->suspect);
1368 e->suspect = porigin;
1369 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001370 queue_blames(sb, porigin, suspects);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001371}
1372
Junio C Hamano69264f42008-04-03 00:56:23 -07001373/*
1374 * We pass blame from the current commit to its parents. We keep saying
1375 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1376 * exonerate ourselves.
1377 */
Junio C Hamano85af7922008-04-02 22:17:53 -07001378static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit)
Junio C Hamano69264f42008-04-03 00:56:23 -07001379{
Jeff King95a4fb02015-09-15 06:05:39 -04001380 if (!reverse) {
1381 if (revs->first_parent_only &&
1382 commit->parents &&
1383 commit->parents->next) {
1384 free_commit_list(commit->parents->next);
1385 commit->parents->next = NULL;
1386 }
Junio C Hamano85af7922008-04-02 22:17:53 -07001387 return commit->parents;
Jeff King95a4fb02015-09-15 06:05:39 -04001388 }
Junio C Hamano85af7922008-04-02 22:17:53 -07001389 return lookup_decoration(&revs->children, &commit->object);
Junio C Hamano69264f42008-04-03 00:56:23 -07001390}
1391
Junio C Hamano85af7922008-04-02 22:17:53 -07001392static int num_scapegoats(struct rev_info *revs, struct commit *commit)
Junio C Hamano69264f42008-04-03 00:56:23 -07001393{
Junio C Hamano85af7922008-04-02 22:17:53 -07001394 struct commit_list *l = first_scapegoat(revs, commit);
René Scharfe4bbaa1e2014-07-17 01:52:09 +02001395 return commit_list_count(l);
Junio C Hamano69264f42008-04-03 00:56:23 -07001396}
1397
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001398/* Distribute collected unsorted blames to the respected sorted lists
1399 * in the various origins.
1400 */
1401static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
1402{
1403 blamed = blame_sort(blamed, compare_blame_suspect);
1404 while (blamed)
1405 {
1406 struct origin *porigin = blamed->suspect;
1407 struct blame_entry *suspects = NULL;
1408 do {
1409 struct blame_entry *next = blamed->next;
1410 blamed->next = suspects;
1411 suspects = blamed;
1412 blamed = next;
1413 } while (blamed && blamed->suspect == porigin);
1414 suspects = reverse_blame(suspects, NULL);
1415 queue_blames(sb, porigin, suspects);
1416 }
1417}
1418
Junio C Hamano69264f42008-04-03 00:56:23 -07001419#define MAXSG 16
Junio C Hamanocee7f242006-10-19 16:00:04 -07001420
Junio C Hamanod24bba82006-10-19 18:49:30 -07001421static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001422{
Junio C Hamano85af7922008-04-02 22:17:53 -07001423 struct rev_info *revs = sb->revs;
Junio C Hamano69264f42008-04-03 00:56:23 -07001424 int i, pass, num_sg;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001425 struct commit *commit = origin->commit;
Junio C Hamano69264f42008-04-03 00:56:23 -07001426 struct commit_list *sg;
1427 struct origin *sg_buf[MAXSG];
1428 struct origin *porigin, **sg_origin = sg_buf;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001429 struct blame_entry *toosmall = NULL;
1430 struct blame_entry *blames, **blametail = &blames;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001431
Junio C Hamano85af7922008-04-02 22:17:53 -07001432 num_sg = num_scapegoats(revs, commit);
Junio C Hamano69264f42008-04-03 00:56:23 -07001433 if (!num_sg)
1434 goto finish;
1435 else if (num_sg < ARRAY_SIZE(sg_buf))
1436 memset(sg_buf, 0, sizeof(sg_buf));
1437 else
1438 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
Junio C Hamanocee7f242006-10-19 16:00:04 -07001439
Junio C Hamano69264f42008-04-03 00:56:23 -07001440 /*
1441 * The first pass looks for unrenamed path to optimize for
Junio C Hamanof69e7432006-10-30 17:17:41 -08001442 * common cases, then we look for renames in the second pass.
1443 */
Junio C Hamano3d1aa562012-09-21 13:52:25 -07001444 for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
Junio C Hamanof69e7432006-10-30 17:17:41 -08001445 struct origin *(*find)(struct scoreboard *,
1446 struct commit *, struct origin *);
1447 find = pass ? find_rename : find_origin;
1448
Junio C Hamano85af7922008-04-02 22:17:53 -07001449 for (i = 0, sg = first_scapegoat(revs, commit);
Junio C Hamano69264f42008-04-03 00:56:23 -07001450 i < num_sg && sg;
1451 sg = sg->next, i++) {
1452 struct commit *p = sg->item;
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001453 int j, same;
Junio C Hamanof69e7432006-10-30 17:17:41 -08001454
Junio C Hamano69264f42008-04-03 00:56:23 -07001455 if (sg_origin[i])
Junio C Hamanof69e7432006-10-30 17:17:41 -08001456 continue;
1457 if (parse_commit(p))
1458 continue;
Junio C Hamano0d981c62006-10-31 01:00:01 -08001459 porigin = find(sb, p, origin);
Junio C Hamanof69e7432006-10-30 17:17:41 -08001460 if (!porigin)
1461 continue;
1462 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001463 pass_whole_blame(sb, origin, porigin);
Junio C Hamanof69e7432006-10-30 17:17:41 -08001464 origin_decref(porigin);
1465 goto finish;
1466 }
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001467 for (j = same = 0; j < i; j++)
Junio C Hamano69264f42008-04-03 00:56:23 -07001468 if (sg_origin[j] &&
1469 !hashcmp(sg_origin[j]->blob_sha1,
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001470 porigin->blob_sha1)) {
1471 same = 1;
1472 break;
1473 }
1474 if (!same)
Junio C Hamano69264f42008-04-03 00:56:23 -07001475 sg_origin[i] = porigin;
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001476 else
1477 origin_decref(porigin);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001478 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001479 }
1480
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001481 num_commits++;
Junio C Hamano85af7922008-04-02 22:17:53 -07001482 for (i = 0, sg = first_scapegoat(revs, commit);
Junio C Hamano69264f42008-04-03 00:56:23 -07001483 i < num_sg && sg;
1484 sg = sg->next, i++) {
1485 struct origin *porigin = sg_origin[i];
Junio C Hamanocee7f242006-10-19 16:00:04 -07001486 if (!porigin)
1487 continue;
Junio C Hamano96e11702008-06-04 22:58:40 -07001488 if (!origin->previous) {
1489 origin_incref(porigin);
1490 origin->previous = porigin;
1491 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001492 pass_blame_to_parent(sb, origin, porigin);
1493 if (!origin->suspects)
Junio C Hamano54a4c612006-10-29 03:07:40 -08001494 goto finish;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001495 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001496
1497 /*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001498 * Optionally find moves in parents' files.
Junio C Hamanod24bba82006-10-19 18:49:30 -07001499 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001500 if (opt & PICKAXE_BLAME_MOVE) {
1501 filter_small(sb, &toosmall, &origin->suspects, blame_move_score);
1502 if (origin->suspects) {
1503 for (i = 0, sg = first_scapegoat(revs, commit);
1504 i < num_sg && sg;
1505 sg = sg->next, i++) {
1506 struct origin *porigin = sg_origin[i];
1507 if (!porigin)
1508 continue;
1509 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1510 if (!origin->suspects)
1511 break;
1512 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001513 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001514 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001515
Junio C Hamano18abd742006-10-19 18:50:17 -07001516 /*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001517 * Optionally find copies from parents' files.
Junio C Hamano18abd742006-10-19 18:50:17 -07001518 */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001519 if (opt & PICKAXE_BLAME_COPY) {
1520 if (blame_copy_score > blame_move_score)
1521 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1522 else if (blame_copy_score < blame_move_score) {
1523 origin->suspects = blame_merge(origin->suspects, toosmall);
1524 toosmall = NULL;
1525 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1526 }
1527 if (!origin->suspects)
1528 goto finish;
1529
Junio C Hamano85af7922008-04-02 22:17:53 -07001530 for (i = 0, sg = first_scapegoat(revs, commit);
Junio C Hamano69264f42008-04-03 00:56:23 -07001531 i < num_sg && sg;
1532 sg = sg->next, i++) {
1533 struct origin *porigin = sg_origin[i];
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001534 find_copy_in_parent(sb, &blametail, &toosmall,
1535 origin, sg->item, porigin, opt);
1536 if (!origin->suspects)
Junio C Hamano54a4c612006-10-29 03:07:40 -08001537 goto finish;
Junio C Hamano18abd742006-10-19 18:50:17 -07001538 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001539 }
Junio C Hamano54a4c612006-10-29 03:07:40 -08001540
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001541finish:
1542 *blametail = NULL;
1543 distribute_blame(sb, blames);
1544 /*
1545 * prepend toosmall to origin->suspects
1546 *
1547 * There is no point in sorting: this ends up on a big
1548 * unsorted list in the caller anyway.
1549 */
1550 if (toosmall) {
1551 struct blame_entry **tail = &toosmall;
1552 while (*tail)
1553 tail = &(*tail)->next;
1554 *tail = origin->suspects;
1555 origin->suspects = toosmall;
1556 }
Junio C Hamano69264f42008-04-03 00:56:23 -07001557 for (i = 0; i < num_sg; i++) {
1558 if (sg_origin[i]) {
1559 drop_origin_blob(sg_origin[i]);
1560 origin_decref(sg_origin[i]);
Junio C Hamano7c3c7962007-12-11 16:05:50 -08001561 }
1562 }
1563 drop_origin_blob(origin);
Junio C Hamano69264f42008-04-03 00:56:23 -07001564 if (sg_buf != sg_origin)
1565 free(sg_origin);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001566}
1567
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001568/*
1569 * Information on commits, used for output.
1570 */
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001571struct commit_info {
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001572 struct strbuf author;
1573 struct strbuf author_mail;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001574 unsigned long author_time;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001575 struct strbuf author_tz;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001576
1577 /* filled only when asked for details */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001578 struct strbuf committer;
1579 struct strbuf committer_mail;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001580 unsigned long committer_time;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001581 struct strbuf committer_tz;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001582
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001583 struct strbuf summary;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001584};
1585
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001586/*
1587 * Parse author/committer line in the commit object buffer
1588 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001589static void get_ac_line(const char *inbuf, const char *what,
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001590 struct strbuf *name, struct strbuf *mail,
1591 unsigned long *time, struct strbuf *tz)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001592{
Antoine Pelisse3c020bd2013-01-05 22:26:38 +01001593 struct ident_split ident;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001594 size_t len, maillen, namelen;
1595 char *tmp, *endp;
1596 const char *namebuf, *mailbuf;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001597
1598 tmp = strstr(inbuf, what);
1599 if (!tmp)
1600 goto error_out;
1601 tmp += strlen(what);
1602 endp = strchr(tmp, '\n');
1603 if (!endp)
1604 len = strlen(tmp);
1605 else
1606 len = endp - tmp;
Antoine Pelisse3c020bd2013-01-05 22:26:38 +01001607
1608 if (split_ident_line(&ident, tmp, len)) {
Junio C Hamanocee7f242006-10-19 16:00:04 -07001609 error_out:
1610 /* Ugh */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001611 tmp = "(unknown)";
1612 strbuf_addstr(name, tmp);
1613 strbuf_addstr(mail, tmp);
1614 strbuf_addstr(tz, tmp);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001615 *time = 0;
1616 return;
1617 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001618
Antoine Pelisse3c020bd2013-01-05 22:26:38 +01001619 namelen = ident.name_end - ident.name_begin;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001620 namebuf = ident.name_begin;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001621
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001622 maillen = ident.mail_end - ident.mail_begin;
1623 mailbuf = ident.mail_begin;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001624
René Scharfede5abe92013-04-17 20:33:54 +02001625 if (ident.date_begin && ident.date_end)
1626 *time = strtoul(ident.date_begin, NULL, 10);
1627 else
1628 *time = 0;
Junio C Hamanof9567382007-04-27 00:42:15 -07001629
René Scharfede5abe92013-04-17 20:33:54 +02001630 if (ident.tz_begin && ident.tz_end)
1631 strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
1632 else
1633 strbuf_addstr(tz, "(unknown)");
Junio C Hamanof9567382007-04-27 00:42:15 -07001634
Junio C Hamanof9567382007-04-27 00:42:15 -07001635 /*
Marius Storm-Olsend20d6542009-02-08 15:34:30 +01001636 * Now, convert both name and e-mail using mailmap
Junio C Hamanof9567382007-04-27 00:42:15 -07001637 */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001638 map_user(&mailmap, &mailbuf, &maillen,
1639 &namebuf, &namelen);
1640
1641 strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
1642 strbuf_add(name, namebuf, namelen);
1643}
1644
1645static void commit_info_init(struct commit_info *ci)
1646{
1647
1648 strbuf_init(&ci->author, 0);
1649 strbuf_init(&ci->author_mail, 0);
1650 strbuf_init(&ci->author_tz, 0);
1651 strbuf_init(&ci->committer, 0);
1652 strbuf_init(&ci->committer_mail, 0);
1653 strbuf_init(&ci->committer_tz, 0);
1654 strbuf_init(&ci->summary, 0);
1655}
1656
1657static void commit_info_destroy(struct commit_info *ci)
1658{
1659
1660 strbuf_release(&ci->author);
1661 strbuf_release(&ci->author_mail);
1662 strbuf_release(&ci->author_tz);
1663 strbuf_release(&ci->committer);
1664 strbuf_release(&ci->committer_mail);
1665 strbuf_release(&ci->committer_tz);
1666 strbuf_release(&ci->summary);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001667}
1668
1669static void get_commit_info(struct commit *commit,
1670 struct commit_info *ret,
1671 int detailed)
1672{
1673 int len;
Junio C Hamanoe297cf52012-10-17 17:12:55 -07001674 const char *subject, *encoding;
Jeff Kingb000c592014-06-10 17:39:30 -04001675 const char *message;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001676
1677 commit_info_init(ret);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001678
Junio C Hamanoe297cf52012-10-17 17:12:55 -07001679 encoding = get_log_output_encoding();
Nguyễn Thái Ngọc Duy5a10d232013-04-19 09:08:40 +10001680 message = logmsg_reencode(commit, NULL, encoding);
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001681 get_ac_line(message, "\nauthor ",
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001682 &ret->author, &ret->author_mail,
Junio C Hamanocee7f242006-10-19 16:00:04 -07001683 &ret->author_time, &ret->author_tz);
1684
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001685 if (!detailed) {
Jeff Kingb66103c2014-06-10 17:41:39 -04001686 unuse_commit_buffer(commit, message);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001687 return;
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001688 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001689
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001690 get_ac_line(message, "\ncommitter ",
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001691 &ret->committer, &ret->committer_mail,
Junio C Hamanocee7f242006-10-19 16:00:04 -07001692 &ret->committer_time, &ret->committer_tz);
1693
Christian Couderad98a582010-07-22 15:18:35 +02001694 len = find_commit_subject(message, &subject);
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001695 if (len)
1696 strbuf_add(&ret->summary, subject, len);
1697 else
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001698 strbuf_addf(&ret->summary, "(%s)", oid_to_hex(&commit->object.oid));
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001699
Jeff Kingb66103c2014-06-10 17:41:39 -04001700 unuse_commit_buffer(commit, message);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001701}
1702
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001703/*
1704 * To allow LF and other nonportable characters in pathnames,
1705 * they are c-style quoted as needed.
1706 */
Junio C Hamano46e5e692007-01-28 01:42:31 -08001707static void write_filename_info(const char *path)
1708{
1709 printf("filename ");
Pierre Habouzit663af342007-09-20 00:42:15 +02001710 write_name_quoted(path, stdout, '\n');
Junio C Hamano46e5e692007-01-28 01:42:31 -08001711}
1712
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001713/*
Junio C Hamano99910302008-06-04 22:48:42 -07001714 * Porcelain/Incremental format wants to show a lot of details per
1715 * commit. Instead of repeating this every line, emit it only once,
Jeff Kinge86226e2011-05-09 09:34:02 -04001716 * the first time each commit appears in the output (unless the
1717 * user has specifically asked for us to repeat).
Junio C Hamano99910302008-06-04 22:48:42 -07001718 */
Jeff Kinge86226e2011-05-09 09:34:02 -04001719static int emit_one_suspect_detail(struct origin *suspect, int repeat)
Junio C Hamano99910302008-06-04 22:48:42 -07001720{
1721 struct commit_info ci;
1722
Jeff Kinge86226e2011-05-09 09:34:02 -04001723 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
Junio C Hamano99910302008-06-04 22:48:42 -07001724 return 0;
1725
1726 suspect->commit->object.flags |= METAINFO_SHOWN;
1727 get_commit_info(suspect->commit, &ci, 1);
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001728 printf("author %s\n", ci.author.buf);
1729 printf("author-mail %s\n", ci.author_mail.buf);
Junio C Hamano99910302008-06-04 22:48:42 -07001730 printf("author-time %lu\n", ci.author_time);
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001731 printf("author-tz %s\n", ci.author_tz.buf);
1732 printf("committer %s\n", ci.committer.buf);
1733 printf("committer-mail %s\n", ci.committer_mail.buf);
Junio C Hamano99910302008-06-04 22:48:42 -07001734 printf("committer-time %lu\n", ci.committer_time);
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001735 printf("committer-tz %s\n", ci.committer_tz.buf);
1736 printf("summary %s\n", ci.summary.buf);
Junio C Hamano99910302008-06-04 22:48:42 -07001737 if (suspect->commit->object.flags & UNINTERESTING)
1738 printf("boundary\n");
Junio C Hamano96e11702008-06-04 22:58:40 -07001739 if (suspect->previous) {
1740 struct origin *prev = suspect->previous;
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001741 printf("previous %s ", oid_to_hex(&prev->commit->object.oid));
Junio C Hamano96e11702008-06-04 22:58:40 -07001742 write_name_quoted(prev->path, stdout, '\n');
1743 }
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001744
1745 commit_info_destroy(&ci);
1746
Junio C Hamano99910302008-06-04 22:48:42 -07001747 return 1;
1748}
1749
1750/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001751 * The blame_entry is found to be guilty for the range.
1752 * Show it in incremental output.
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001753 */
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06001754static void found_guilty_entry(struct blame_entry *ent,
1755 struct progress_info *pi)
Linus Torvalds717d1462007-01-28 01:34:06 -08001756{
Linus Torvalds717d1462007-01-28 01:34:06 -08001757 if (incremental) {
1758 struct origin *suspect = ent->suspect;
1759
1760 printf("%s %d %d %d\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001761 oid_to_hex(&suspect->commit->object.oid),
Linus Torvalds717d1462007-01-28 01:34:06 -08001762 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
Jeff Kinge86226e2011-05-09 09:34:02 -04001763 emit_one_suspect_detail(suspect, 0);
Junio C Hamano46e5e692007-01-28 01:42:31 -08001764 write_filename_info(suspect->path);
Theodore Ts'o06f59e92007-06-29 13:40:46 -04001765 maybe_flush_or_die(stdout, "stdout");
Linus Torvalds717d1462007-01-28 01:34:06 -08001766 }
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06001767 pi->blamed_lines += ent->num_lines;
1768 display_progress(pi->progress, pi->blamed_lines);
Linus Torvalds717d1462007-01-28 01:34:06 -08001769}
1770
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001771/*
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001772 * The main loop -- while we have blobs with lines whose true origin
1773 * is still unknown, pick one blob, and allow its lines to pass blames
1774 * to its parents. */
Junio C Hamano85af7922008-04-02 22:17:53 -07001775static void assign_blame(struct scoreboard *sb, int opt)
Linus Torvalds717d1462007-01-28 01:34:06 -08001776{
Junio C Hamano85af7922008-04-02 22:17:53 -07001777 struct rev_info *revs = sb->revs;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001778 struct commit *commit = prio_queue_get(&sb->commits);
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06001779 struct progress_info pi = { NULL, 0 };
1780
1781 if (show_progress)
1782 pi.progress = start_progress_delay(_("Blaming lines"),
1783 sb->num_lines, 50, 1);
Junio C Hamano85af7922008-04-02 22:17:53 -07001784
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001785 while (commit) {
Linus Torvalds717d1462007-01-28 01:34:06 -08001786 struct blame_entry *ent;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001787 struct origin *suspect = commit->util;
Linus Torvalds717d1462007-01-28 01:34:06 -08001788
1789 /* find one suspect to break down */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001790 while (suspect && !suspect->suspects)
1791 suspect = suspect->next;
1792
1793 if (!suspect) {
1794 commit = prio_queue_get(&sb->commits);
1795 continue;
1796 }
1797
1798 assert(commit == suspect->commit);
Linus Torvalds717d1462007-01-28 01:34:06 -08001799
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001800 /*
1801 * We will use this suspect later in the loop,
1802 * so hold onto it in the meantime.
1803 */
Linus Torvalds717d1462007-01-28 01:34:06 -08001804 origin_incref(suspect);
Jeff King00640532013-10-24 04:53:01 -04001805 parse_commit(commit);
Junio C Hamano85af7922008-04-02 22:17:53 -07001806 if (reverse ||
1807 (!(commit->object.flags & UNINTERESTING) &&
1808 !(revs->max_age != -1 && commit->date < revs->max_age)))
Linus Torvalds717d1462007-01-28 01:34:06 -08001809 pass_blame(sb, suspect, opt);
1810 else {
1811 commit->object.flags |= UNINTERESTING;
1812 if (commit->object.parsed)
1813 mark_parents_uninteresting(commit);
1814 }
1815 /* treat root commit as boundary */
1816 if (!commit->parents && !show_root)
1817 commit->object.flags |= UNINTERESTING;
1818
1819 /* Take responsibility for the remaining entries */
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001820 ent = suspect->suspects;
1821 if (ent) {
1822 suspect->guilty = 1;
1823 for (;;) {
1824 struct blame_entry *next = ent->next;
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06001825 found_guilty_entry(ent, &pi);
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001826 if (next) {
1827 ent = next;
1828 continue;
1829 }
1830 ent->next = sb->ent;
1831 sb->ent = suspect->suspects;
1832 suspect->suspects = NULL;
1833 break;
1834 }
1835 }
Linus Torvalds717d1462007-01-28 01:34:06 -08001836 origin_decref(suspect);
1837
1838 if (DEBUG) /* sanity */
1839 sanity_check_refcnt(sb);
1840 }
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06001841
1842 stop_progress(&pi.progress);
Linus Torvalds717d1462007-01-28 01:34:06 -08001843}
1844
1845static const char *format_time(unsigned long time, const char *tz_str,
1846 int show_raw_time)
1847{
Jiang Xinbccce0f2014-04-21 14:02:03 +08001848 static struct strbuf time_buf = STRBUF_INIT;
Linus Torvalds717d1462007-01-28 01:34:06 -08001849
Jiang Xinbccce0f2014-04-21 14:02:03 +08001850 strbuf_reset(&time_buf);
Linus Torvalds717d1462007-01-28 01:34:06 -08001851 if (show_raw_time) {
Jiang Xinbccce0f2014-04-21 14:02:03 +08001852 strbuf_addf(&time_buf, "%lu %s", time, tz_str);
Linus Torvalds717d1462007-01-28 01:34:06 -08001853 }
Eugene Letuchy31653c12009-02-20 14:51:11 -08001854 else {
Elia Pintoac39b272014-01-29 05:33:15 -08001855 const char *time_str;
Jiang Xinbccce0f2014-04-21 14:02:03 +08001856 size_t time_width;
Elia Pintoac39b272014-01-29 05:33:15 -08001857 int tz;
Eugene Letuchy31653c12009-02-20 14:51:11 -08001858 tz = atoi(tz_str);
Jeff Kinga5481a62015-06-25 12:55:02 -04001859 time_str = show_date(time, tz, &blame_date_mode);
Jiang Xinbccce0f2014-04-21 14:02:03 +08001860 strbuf_addstr(&time_buf, time_str);
1861 /*
1862 * Add space paddings to time_buf to display a fixed width
1863 * string, and use time_width for display width calibration.
1864 */
1865 for (time_width = utf8_strwidth(time_str);
1866 time_width < blame_date_width;
1867 time_width++)
1868 strbuf_addch(&time_buf, ' ');
Eugene Letuchy31653c12009-02-20 14:51:11 -08001869 }
Jiang Xinbccce0f2014-04-21 14:02:03 +08001870 return time_buf.buf;
Linus Torvalds717d1462007-01-28 01:34:06 -08001871}
1872
Junio C Hamanocee7f242006-10-19 16:00:04 -07001873#define OUTPUT_ANNOTATE_COMPAT 001
1874#define OUTPUT_LONG_OBJECT_NAME 002
1875#define OUTPUT_RAW_TIMESTAMP 004
1876#define OUTPUT_PORCELAIN 010
1877#define OUTPUT_SHOW_NAME 020
1878#define OUTPUT_SHOW_NUMBER 040
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001879#define OUTPUT_SHOW_SCORE 0100
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001880#define OUTPUT_NO_AUTHOR 0200
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001881#define OUTPUT_SHOW_EMAIL 0400
Jeff Kinged747dd2011-05-09 09:34:42 -04001882#define OUTPUT_LINE_PORCELAIN 01000
Junio C Hamanocee7f242006-10-19 16:00:04 -07001883
Jeff Kinge86226e2011-05-09 09:34:02 -04001884static void emit_porcelain_details(struct origin *suspect, int repeat)
1885{
1886 if (emit_one_suspect_detail(suspect, repeat) ||
1887 (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
1888 write_filename_info(suspect->path);
1889}
1890
1891static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
1892 int opt)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001893{
Jeff Kinged747dd2011-05-09 09:34:42 -04001894 int repeat = opt & OUTPUT_LINE_PORCELAIN;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001895 int cnt;
1896 const char *cp;
1897 struct origin *suspect = ent->suspect;
Jeff Kingd59f7652015-09-24 17:08:03 -04001898 char hex[GIT_SHA1_HEXSZ + 1];
Junio C Hamanocee7f242006-10-19 16:00:04 -07001899
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001900 sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
David Kastrup7e6ac6e2014-04-26 01:56:49 +02001901 printf("%s %d %d %d\n",
Junio C Hamanocee7f242006-10-19 16:00:04 -07001902 hex,
Junio C Hamanocee7f242006-10-19 16:00:04 -07001903 ent->s_lno + 1,
1904 ent->lno + 1,
1905 ent->num_lines);
Jeff Kinged747dd2011-05-09 09:34:42 -04001906 emit_porcelain_details(suspect, repeat);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001907
1908 cp = nth_line(sb, ent->lno);
1909 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1910 char ch;
Jeff Kinged747dd2011-05-09 09:34:42 -04001911 if (cnt) {
Junio C Hamanocee7f242006-10-19 16:00:04 -07001912 printf("%s %d %d\n", hex,
1913 ent->s_lno + 1 + cnt,
1914 ent->lno + 1 + cnt);
Jeff Kinged747dd2011-05-09 09:34:42 -04001915 if (repeat)
1916 emit_porcelain_details(suspect, 1);
1917 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001918 putchar('\t');
1919 do {
1920 ch = *cp++;
1921 putchar(ch);
1922 } while (ch != '\n' &&
1923 cp < sb->final_buf + sb->final_buf_size);
1924 }
Johannes Schindelina5ca8362009-10-19 22:06:28 -05001925
1926 if (sb->final_buf_size && cp[-1] != '\n')
1927 putchar('\n');
Junio C Hamanocee7f242006-10-19 16:00:04 -07001928}
1929
1930static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1931{
1932 int cnt;
1933 const char *cp;
1934 struct origin *suspect = ent->suspect;
1935 struct commit_info ci;
Jeff Kingd59f7652015-09-24 17:08:03 -04001936 char hex[GIT_SHA1_HEXSZ + 1];
Junio C Hamanocee7f242006-10-19 16:00:04 -07001937 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1938
1939 get_commit_info(suspect->commit, &ci, 1);
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001940 sha1_to_hex_r(hex, suspect->commit->object.oid.hash);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001941
1942 cp = nth_line(sb, ent->lno);
1943 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1944 char ch;
Namhyung Kim84393bf2011-04-06 11:20:50 +09001945 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001946
Junio C Hamanob11121d2006-12-01 20:45:45 -08001947 if (suspect->commit->object.flags & UNINTERESTING) {
Junio C Hamanoe68989a2007-02-06 01:52:04 -08001948 if (blank_boundary)
1949 memset(hex, ' ', length);
Junio C Hamano7ceacdf2008-09-05 00:57:35 -07001950 else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08001951 length--;
1952 putchar('^');
1953 }
Junio C Hamanob11121d2006-12-01 20:45:45 -08001954 }
1955
1956 printf("%.*s", length, hex);
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001957 if (opt & OUTPUT_ANNOTATE_COMPAT) {
1958 const char *name;
1959 if (opt & OUTPUT_SHOW_EMAIL)
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001960 name = ci.author_mail.buf;
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001961 else
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001962 name = ci.author.buf;
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001963 printf("\t(%10s\t%10s\t%d)", name,
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001964 format_time(ci.author_time, ci.author_tz.buf,
Junio C Hamanocee7f242006-10-19 16:00:04 -07001965 show_raw_time),
1966 ent->lno + 1 + cnt);
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001967 } else {
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001968 if (opt & OUTPUT_SHOW_SCORE)
Junio C Hamano54a4c612006-10-29 03:07:40 -08001969 printf(" %*d %02d",
1970 max_score_digits, ent->score,
1971 ent->suspect->refcnt);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001972 if (opt & OUTPUT_SHOW_NAME)
1973 printf(" %-*.*s", longest_file, longest_file,
1974 suspect->path);
1975 if (opt & OUTPUT_SHOW_NUMBER)
1976 printf(" %*d", max_orig_digits,
1977 ent->s_lno + 1 + cnt);
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001978
Geoffrey Thomasffaf9cc2009-01-30 04:41:29 -05001979 if (!(opt & OUTPUT_NO_AUTHOR)) {
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001980 const char *name;
1981 int pad;
1982 if (opt & OUTPUT_SHOW_EMAIL)
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001983 name = ci.author_mail.buf;
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001984 else
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001985 name = ci.author.buf;
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001986 pad = longest_author - utf8_strwidth(name);
Geoffrey Thomasffaf9cc2009-01-30 04:41:29 -05001987 printf(" (%s%*s %10s",
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07001988 name, pad, "",
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001989 format_time(ci.author_time,
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01001990 ci.author_tz.buf,
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001991 show_raw_time));
Geoffrey Thomasffaf9cc2009-01-30 04:41:29 -05001992 }
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001993 printf(" %*d) ",
Junio C Hamanocee7f242006-10-19 16:00:04 -07001994 max_digits, ent->lno + 1 + cnt);
1995 }
1996 do {
1997 ch = *cp++;
1998 putchar(ch);
1999 } while (ch != '\n' &&
2000 cp < sb->final_buf + sb->final_buf_size);
2001 }
Johannes Schindelina5ca8362009-10-19 22:06:28 -05002002
2003 if (sb->final_buf_size && cp[-1] != '\n')
2004 putchar('\n');
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01002005
2006 commit_info_destroy(&ci);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002007}
2008
2009static void output(struct scoreboard *sb, int option)
2010{
2011 struct blame_entry *ent;
2012
2013 if (option & OUTPUT_PORCELAIN) {
2014 for (ent = sb->ent; ent; ent = ent->next) {
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002015 int count = 0;
2016 struct origin *suspect;
2017 struct commit *commit = ent->suspect->commit;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002018 if (commit->object.flags & MORE_THAN_ONE_PATH)
2019 continue;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002020 for (suspect = commit->util; suspect; suspect = suspect->next) {
2021 if (suspect->guilty && count++) {
2022 commit->object.flags |= MORE_THAN_ONE_PATH;
2023 break;
2024 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002025 }
2026 }
2027 }
2028
2029 for (ent = sb->ent; ent; ent = ent->next) {
2030 if (option & OUTPUT_PORCELAIN)
Jeff Kinge86226e2011-05-09 09:34:02 -04002031 emit_porcelain(sb, ent, option);
Junio C Hamano5ff62c32006-10-20 14:51:12 -07002032 else {
Junio C Hamanocee7f242006-10-19 16:00:04 -07002033 emit_other(sb, ent, option);
Junio C Hamano5ff62c32006-10-20 14:51:12 -07002034 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002035 }
2036}
2037
René Scharfe29aa0b22014-06-13 21:53:03 +02002038static const char *get_next_line(const char *start, const char *end)
2039{
2040 const char *nl = memchr(start, '\n', end - start);
René Scharfe60d85e12014-06-13 21:54:59 +02002041 return nl ? nl + 1 : end;
René Scharfe29aa0b22014-06-13 21:53:03 +02002042}
2043
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002044/*
2045 * To allow quick access to the contents of nth line in the
2046 * final image, prepare an index in the scoreboard.
2047 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002048static int prepare_lines(struct scoreboard *sb)
2049{
2050 const char *buf = sb->final_buf;
2051 unsigned long len = sb->final_buf_size;
David Kastrup352bbbd2014-02-12 15:27:24 +01002052 const char *end = buf + len;
2053 const char *p;
2054 int *lineno;
René Scharfe60d85e12014-06-13 21:54:59 +02002055 int num = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002056
René Scharfe60d85e12014-06-13 21:54:59 +02002057 for (p = buf; p < end; p = get_next_line(p, end))
René Scharfe29aa0b22014-06-13 21:53:03 +02002058 num++;
David Kastrup352bbbd2014-02-12 15:27:24 +01002059
Jeff Kingb32fa952016-02-22 17:44:25 -05002060 ALLOC_ARRAY(sb->lineno, num + 1);
2061 lineno = sb->lineno;
David Kastrup352bbbd2014-02-12 15:27:24 +01002062
René Scharfe60d85e12014-06-13 21:54:59 +02002063 for (p = buf; p < end; p = get_next_line(p, end))
René Scharfe29aa0b22014-06-13 21:53:03 +02002064 *lineno++ = p - buf;
David Kastrup352bbbd2014-02-12 15:27:24 +01002065
René Scharfe60d85e12014-06-13 21:54:59 +02002066 *lineno = len;
David Kastrup352bbbd2014-02-12 15:27:24 +01002067
René Scharfe60d85e12014-06-13 21:54:59 +02002068 sb->num_lines = num;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002069 return sb->num_lines;
2070}
2071
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002072/*
2073 * Add phony grafts for use with -S; this is primarily to
Heikki Orsila34baebc2008-08-30 14:12:53 +03002074 * support git's cvsserver that wants to give a linear history
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002075 * to its clients.
2076 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002077static int read_ancestry(const char *graft_file)
2078{
2079 FILE *fp = fopen(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +01002080 struct strbuf buf = STRBUF_INIT;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002081 if (!fp)
2082 return -1;
Johannes Schindeline228c172013-12-27 21:49:57 +01002083 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamanocee7f242006-10-19 16:00:04 -07002084 /* The format is just "Commit Parent1 Parent2 ...\n" */
Johannes Schindeline228c172013-12-27 21:49:57 +01002085 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
Junio C Hamano8eaf7982006-11-10 13:39:01 -08002086 if (graft)
2087 register_commit_graft(graft, 0);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002088 }
2089 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +01002090 strbuf_release(&buf);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002091 return 0;
2092}
2093
Junio C Hamanob31272f2012-07-02 00:54:00 -07002094static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
2095{
brian m. carlsoned1c9972015-11-10 02:22:29 +00002096 const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
Junio C Hamanob31272f2012-07-02 00:54:00 -07002097 auto_abbrev);
2098 int len = strlen(uniq);
2099 if (auto_abbrev < len)
2100 return len;
2101 return auto_abbrev;
2102}
2103
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002104/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002105 * How many columns do we need to show line numbers, authors,
2106 * and filenames?
2107 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002108static void find_alignment(struct scoreboard *sb, int *option)
2109{
2110 int longest_src_lines = 0;
2111 int longest_dst_lines = 0;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07002112 unsigned largest_score = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002113 struct blame_entry *e;
Junio C Hamanob31272f2012-07-02 00:54:00 -07002114 int compute_auto_abbrev = (abbrev < 0);
2115 int auto_abbrev = default_abbrev;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002116
2117 for (e = sb->ent; e; e = e->next) {
2118 struct origin *suspect = e->suspect;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002119 int num;
2120
Junio C Hamanob31272f2012-07-02 00:54:00 -07002121 if (compute_auto_abbrev)
2122 auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
Junio C Hamanoab3bb802006-11-28 22:29:18 -08002123 if (strcmp(suspect->path, sb->path))
2124 *option |= OUTPUT_SHOW_NAME;
2125 num = strlen(suspect->path);
2126 if (longest_file < num)
2127 longest_file = num;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002128 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
Eric Sunshinee6005922015-02-09 16:28:07 -05002129 struct commit_info ci;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002130 suspect->commit->object.flags |= METAINFO_SHOWN;
2131 get_commit_info(suspect->commit, &ci, 1);
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07002132 if (*option & OUTPUT_SHOW_EMAIL)
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01002133 num = utf8_strwidth(ci.author_mail.buf);
Kevin Ballard1b8cdce2010-10-15 23:57:51 -07002134 else
Antoine Pelisseea02ffa2013-01-05 22:26:40 +01002135 num = utf8_strwidth(ci.author.buf);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002136 if (longest_author < num)
2137 longest_author = num;
Eric Sunshinee6005922015-02-09 16:28:07 -05002138 commit_info_destroy(&ci);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002139 }
2140 num = e->s_lno + e->num_lines;
2141 if (longest_src_lines < num)
2142 longest_src_lines = num;
2143 num = e->lno + e->num_lines;
2144 if (longest_dst_lines < num)
2145 longest_dst_lines = num;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07002146 if (largest_score < ent_score(sb, e))
2147 largest_score = ent_score(sb, e);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002148 }
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +01002149 max_orig_digits = decimal_width(longest_src_lines);
2150 max_digits = decimal_width(longest_dst_lines);
2151 max_score_digits = decimal_width(largest_score);
Junio C Hamanob31272f2012-07-02 00:54:00 -07002152
2153 if (compute_auto_abbrev)
2154 /* one more abbrev length is needed for the boundary commit */
2155 abbrev = auto_abbrev + 1;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002156}
2157
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002158/*
2159 * For debugging -- origin is refcounted, and this asserts that
2160 * we do not underflow.
2161 */
Junio C Hamano54a4c612006-10-29 03:07:40 -08002162static void sanity_check_refcnt(struct scoreboard *sb)
2163{
2164 int baa = 0;
2165 struct blame_entry *ent;
2166
2167 for (ent = sb->ent; ent; ent = ent->next) {
Junio C Hamanoae86ad62006-10-30 14:27:52 -08002168 /* Nobody should have zero or negative refcnt */
Junio C Hamano854b97f2006-11-04 19:18:50 -08002169 if (ent->suspect->refcnt <= 0) {
2170 fprintf(stderr, "%s in %s has negative refcnt %d\n",
2171 ent->suspect->path,
brian m. carlsonf2fd0762015-11-10 02:22:28 +00002172 oid_to_hex(&ent->suspect->commit->object.oid),
Junio C Hamano854b97f2006-11-04 19:18:50 -08002173 ent->suspect->refcnt);
Junio C Hamanoae86ad62006-10-30 14:27:52 -08002174 baa = 1;
Junio C Hamano854b97f2006-11-04 19:18:50 -08002175 }
Junio C Hamanoae86ad62006-10-30 14:27:52 -08002176 }
Junio C Hamano54a4c612006-10-29 03:07:40 -08002177 if (baa) {
2178 int opt = 0160;
2179 find_alignment(sb, &opt);
2180 output(sb, opt);
Junio C Hamano854b97f2006-11-04 19:18:50 -08002181 die("Baa %d!", baa);
Junio C Hamano54a4c612006-10-29 03:07:40 -08002182 }
2183}
2184
Junio C Hamano4a0fc952006-10-20 15:37:12 -07002185static unsigned parse_score(const char *arg)
2186{
2187 char *end;
2188 unsigned long score = strtoul(arg, &end, 10);
2189 if (*end)
2190 return 0;
2191 return score;
2192}
2193
Jeff King20239ba2006-11-02 02:22:49 -05002194static const char *add_prefix(const char *prefix, const char *path)
2195{
Robin Rosenberg097971f2008-02-01 05:07:04 +01002196 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
Jeff King20239ba2006-11-02 02:22:49 -05002197}
2198
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01002199static int git_blame_config(const char *var, const char *value, void *cb)
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08002200{
2201 if (!strcmp(var, "blame.showroot")) {
2202 show_root = git_config_bool(var, value);
2203 return 0;
2204 }
2205 if (!strcmp(var, "blame.blankboundary")) {
2206 blank_boundary = git_config_bool(var, value);
2207 return 0;
2208 }
Quentin Neill8b504db2015-05-31 14:27:37 -05002209 if (!strcmp(var, "blame.showemail")) {
2210 int *output_option = cb;
2211 if (git_config_bool(var, value))
2212 *output_option |= OUTPUT_SHOW_EMAIL;
2213 else
2214 *output_option &= ~OUTPUT_SHOW_EMAIL;
2215 return 0;
2216 }
Eugene Letuchy31653c12009-02-20 14:51:11 -08002217 if (!strcmp(var, "blame.date")) {
2218 if (!value)
2219 return config_error_nonbool(var);
Jeff Kinga5481a62015-06-25 12:55:02 -04002220 parse_date_format(value, &blame_date_mode);
Eugene Letuchy31653c12009-02-20 14:51:11 -08002221 return 0;
2222 }
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002223
Jeff King6680a082012-02-07 13:23:02 -05002224 if (userdiff_config(var, value) < 0)
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002225 return -1;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002226
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01002227 return git_default_config(var, value, cb);
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08002228}
2229
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002230static void verify_working_tree_path(struct commit *work_tree, const char *path)
Junio C Hamanoffcabcc2012-09-10 16:30:20 -07002231{
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002232 struct commit_list *parents;
Junio C Hamanoffcabcc2012-09-10 16:30:20 -07002233
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002234 for (parents = work_tree->parents; parents; parents = parents->next) {
brian m. carlsoned1c9972015-11-10 02:22:29 +00002235 const unsigned char *commit_sha1 = parents->item->object.oid.hash;
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002236 unsigned char blob_sha1[20];
2237 unsigned mode;
2238
2239 if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) &&
2240 sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
2241 return;
2242 }
2243 die("no such path '%s' in HEAD", path);
2244}
2245
2246static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
2247{
2248 struct commit *parent;
2249
2250 parent = lookup_commit_reference(sha1);
2251 if (!parent)
2252 die("no such commit %s", sha1_to_hex(sha1));
2253 return &commit_list_insert(parent, tail)->next;
2254}
2255
2256static void append_merge_parents(struct commit_list **tail)
2257{
2258 int merge_head;
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002259 struct strbuf line = STRBUF_INIT;
2260
Jeff Kingf9327292015-08-10 05:38:57 -04002261 merge_head = open(git_path_merge_head(), O_RDONLY);
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002262 if (merge_head < 0) {
2263 if (errno == ENOENT)
2264 return;
Jeff Kingf9327292015-08-10 05:38:57 -04002265 die("cannot open '%s' for reading", git_path_merge_head());
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002266 }
2267
2268 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
2269 unsigned char sha1[20];
2270 if (line.len < 40 || get_sha1_hex(line.buf, sha1))
Jeff Kingf9327292015-08-10 05:38:57 -04002271 die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002272 tail = append_parent(tail, sha1);
2273 }
2274 close(merge_head);
2275 strbuf_release(&line);
Junio C Hamanoffcabcc2012-09-10 16:30:20 -07002276}
2277
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002278/*
Jeff King8597ea32014-06-10 17:44:13 -04002279 * This isn't as simple as passing sb->buf and sb->len, because we
2280 * want to transfer ownership of the buffer to the commit (so we
2281 * must use detach).
2282 */
2283static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
2284{
2285 size_t len;
2286 void *buf = strbuf_detach(sb, &len);
2287 set_commit_buffer(c, buf, len);
2288}
2289
2290/*
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002291 * Prepare a dummy commit that represents the work tree (or staged) item.
2292 * Note that annotating work tree item never works in the reverse.
2293 */
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002294static struct commit *fake_working_tree_commit(struct diff_options *opt,
2295 const char *path,
2296 const char *contents_from)
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002297{
2298 struct commit *commit;
2299 struct origin *origin;
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002300 struct commit_list **parent_tail, *parent;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002301 unsigned char head_sha1[20];
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002302 struct strbuf buf = STRBUF_INIT;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002303 const char *ident;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002304 time_t now;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002305 int size, len;
2306 struct cache_entry *ce;
2307 unsigned mode;
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002308 struct strbuf msg = STRBUF_INIT;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002309
Torsten Bögershausena08feb82016-04-05 21:23:54 +02002310 read_cache();
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002311 time(&now);
Jeff King10322a02014-06-10 17:39:11 -04002312 commit = alloc_commit_node();
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002313 commit->object.parsed = 1;
2314 commit->date = now;
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002315 parent_tail = &commit->parents;
2316
Ronnie Sahlberg7695d112014-07-15 12:59:36 -07002317 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002318 die("no such ref: HEAD");
2319
2320 parent_tail = append_parent(parent_tail, head_sha1);
2321 append_merge_parents(parent_tail);
2322 verify_working_tree_path(commit, path);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002323
2324 origin = make_origin(commit, path);
2325
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002326 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2327 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
2328 for (parent = commit->parents; parent; parent = parent->next)
2329 strbuf_addf(&msg, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00002330 oid_to_hex(&parent->item->object.oid));
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002331 strbuf_addf(&msg,
2332 "author %s\n"
2333 "committer %s\n\n"
2334 "Version of %s from %s\n",
2335 ident, ident, path,
2336 (!contents_from ? path :
2337 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
Jeff King8597ea32014-06-10 17:44:13 -04002338 set_commit_buffer_from_strbuf(commit, &msg);
Junio C Hamano9aeaab62012-09-11 14:30:03 -07002339
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002340 if (!contents_from || strcmp("-", contents_from)) {
2341 struct stat st;
2342 const char *read_from;
Sebastian Schuberth85180882011-11-07 18:33:34 +01002343 char *buf_ptr;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002344 unsigned long buf_len;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002345
2346 if (contents_from) {
2347 if (stat(contents_from, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02002348 die_errno("Cannot stat '%s'", contents_from);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002349 read_from = contents_from;
2350 }
2351 else {
2352 if (lstat(path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02002353 die_errno("Cannot lstat '%s'", path);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002354 read_from = path;
2355 }
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002356 mode = canon_mode(st.st_mode);
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002357
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002358 switch (st.st_mode & S_IFMT) {
2359 case S_IFREG:
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002360 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
Jeff Kinge5450102012-07-28 11:03:01 -04002361 textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len))
Sebastian Schuberth85180882011-11-07 18:33:34 +01002362 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002363 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
Thomas Rast0721c312009-06-27 17:58:47 +02002364 die_errno("cannot open or read '%s'", read_from);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002365 break;
2366 case S_IFLNK:
Junio C Hamanoedfd45d2008-12-17 12:37:53 -08002367 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02002368 die_errno("cannot readlink '%s'", read_from);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002369 break;
2370 default:
2371 die("unsupported file type %s", read_from);
2372 }
2373 }
2374 else {
2375 /* Reading from stdin */
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002376 mode = 0;
Pierre Habouzitf1696ee2007-09-10 12:35:04 +02002377 if (strbuf_read(&buf, 0, 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02002378 die_errno("failed to read from stdin");
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002379 }
Torsten Bögershausen4bf256d2015-05-03 18:38:01 +02002380 convert_to_git(path, buf.buf, buf.len, &buf, 0);
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002381 origin->file.ptr = buf.buf;
2382 origin->file.size = buf.len;
2383 pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002384
2385 /*
2386 * Read the current index, replace the path entry with
2387 * origin->blob_sha1 without mucking with its mode or type
2388 * bits; we are not going to write this index out -- we just
2389 * want to run "diff-index --cached".
2390 */
2391 discard_cache();
2392 read_cache();
2393
2394 len = strlen(path);
2395 if (!mode) {
2396 int pos = cache_name_pos(path, len);
2397 if (0 <= pos)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002398 mode = active_cache[pos]->ce_mode;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002399 else
2400 /* Let's not bother reading from HEAD tree */
2401 mode = S_IFREG | 0644;
2402 }
2403 size = cache_entry_size(len);
2404 ce = xcalloc(1, size);
2405 hashcpy(ce->sha1, origin->blob_sha1);
2406 memcpy(ce->name, path, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002407 ce->ce_flags = create_ce_flags(0);
2408 ce->ce_namelen = len;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002409 ce->ce_mode = create_ce_mode(mode);
2410 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2411
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +07002412 cache_tree_invalidate_path(&the_index, path);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002413
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002414 return commit;
2415}
2416
Jeff King7cb5f7c2015-11-17 18:22:37 -05002417static struct commit *find_single_final(struct rev_info *revs,
2418 const char **name_p)
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002419{
2420 int i;
Jeff King7cb5f7c2015-11-17 18:22:37 -05002421 struct commit *found = NULL;
2422 const char *name = NULL;
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002423
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002424 for (i = 0; i < revs->pending.nr; i++) {
2425 struct object *obj = revs->pending.objects[i].item;
2426 if (obj->flags & UNINTERESTING)
2427 continue;
2428 while (obj->type == OBJ_TAG)
2429 obj = deref_tag(obj, NULL, 0);
2430 if (obj->type != OBJ_COMMIT)
2431 die("Non commit %s?", revs->pending.objects[i].name);
Max Kirillov1b0d4002015-10-30 07:01:52 +02002432 if (found)
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002433 die("More than one commit to dig from %s and %s?",
Jeff King7cb5f7c2015-11-17 18:22:37 -05002434 revs->pending.objects[i].name, name);
2435 found = (struct commit *)obj;
2436 name = revs->pending.objects[i].name;
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002437 }
Jeff King7cb5f7c2015-11-17 18:22:37 -05002438 if (name_p)
2439 *name_p = name;
Max Kirillov1b0d4002015-10-30 07:01:52 +02002440 return found;
2441}
2442
2443static char *prepare_final(struct scoreboard *sb)
2444{
Jeff King7cb5f7c2015-11-17 18:22:37 -05002445 const char *name;
2446 sb->final = find_single_final(sb->revs, &name);
2447 return xstrdup_or_null(name);
Junio C Hamanof6c07d72008-04-02 22:17:53 -07002448}
2449
Lukas Fleischera46442f2015-01-12 20:59:26 -05002450static char *prepare_initial(struct scoreboard *sb)
Junio C Hamano85af7922008-04-02 22:17:53 -07002451{
2452 int i;
2453 const char *final_commit_name = NULL;
2454 struct rev_info *revs = sb->revs;
2455
2456 /*
2457 * There must be one and only one negative commit, and it must be
2458 * the boundary.
2459 */
2460 for (i = 0; i < revs->pending.nr; i++) {
2461 struct object *obj = revs->pending.objects[i].item;
2462 if (!(obj->flags & UNINTERESTING))
2463 continue;
2464 while (obj->type == OBJ_TAG)
2465 obj = deref_tag(obj, NULL, 0);
2466 if (obj->type != OBJ_COMMIT)
2467 die("Non commit %s?", revs->pending.objects[i].name);
2468 if (sb->final)
2469 die("More than one commit to dig down to %s and %s?",
2470 revs->pending.objects[i].name,
2471 final_commit_name);
2472 sb->final = (struct commit *) obj;
2473 final_commit_name = revs->pending.objects[i].name;
2474 }
2475 if (!final_commit_name)
2476 die("No commit to dig down to?");
Lukas Fleischera46442f2015-01-12 20:59:26 -05002477 return xstrdup(final_commit_name);
Junio C Hamano85af7922008-04-02 22:17:53 -07002478}
2479
Pierre Habouzit5817da02008-07-08 15:19:34 +02002480static int blame_copy_callback(const struct option *option, const char *arg, int unset)
2481{
2482 int *opt = option->value;
2483
2484 /*
2485 * -C enables copy from removed files;
2486 * -C -C enables copy from existing files, but only
2487 * when blaming a new file;
2488 * -C -C -C enables copy from existing files for
2489 * everybody
2490 */
2491 if (*opt & PICKAXE_BLAME_COPY_HARDER)
2492 *opt |= PICKAXE_BLAME_COPY_HARDEST;
2493 if (*opt & PICKAXE_BLAME_COPY)
2494 *opt |= PICKAXE_BLAME_COPY_HARDER;
2495 *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2496
2497 if (arg)
2498 blame_copy_score = parse_score(arg);
2499 return 0;
2500}
2501
2502static int blame_move_callback(const struct option *option, const char *arg, int unset)
2503{
2504 int *opt = option->value;
2505
2506 *opt |= PICKAXE_BLAME_MOVE;
2507
2508 if (arg)
2509 blame_move_score = parse_score(arg);
2510 return 0;
2511}
2512
Junio C Hamanoacca6872006-11-08 18:47:54 -08002513int cmd_blame(int argc, const char **argv, const char *prefix)
Junio C Hamanocee7f242006-10-19 16:00:04 -07002514{
2515 struct rev_info revs;
2516 const char *path;
2517 struct scoreboard sb;
2518 struct origin *o;
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002519 struct blame_entry *ent = NULL;
2520 long dashdash_pos, lno;
Lukas Fleischera46442f2015-01-12 20:59:26 -05002521 char *final_commit_name = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002522 enum object_type type;
Max Kirillov700fd282015-10-30 07:01:53 +02002523 struct commit *final_commit = NULL;
Pierre Habouzit5817da02008-07-08 15:19:34 +02002524
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002525 static struct string_list range_list;
Pierre Habouzit5817da02008-07-08 15:19:34 +02002526 static int output_option = 0, opt = 0;
2527 static int show_stats = 0;
2528 static const char *revs_file = NULL;
2529 static const char *contents_from = NULL;
2530 static const struct option options[] = {
Stefan Bellerd5d09d42013-08-03 13:51:19 +02002531 OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
2532 OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2533 OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
2534 OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06002535 OPT_BOOL(0, "progress", &show_progress, N_("Force progress reporting")),
Nguyễn Thái Ngọc Duyefd2a8b2012-08-20 19:31:54 +07002536 OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
2537 OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
2538 OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
2539 OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
2540 OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
2541 OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
2542 OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
2543 OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
2544 OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
2545 OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
2546 OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
2547 OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
2548 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2549 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
2550 { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
2551 { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002552 OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
Namhyung Kim84393bf2011-04-06 11:20:50 +09002553 OPT__ABBREV(&abbrev),
Pierre Habouzit5817da02008-07-08 15:19:34 +02002554 OPT_END()
2555 };
2556
2557 struct parse_opt_ctx_t ctx;
Junio C Hamano7ceacdf2008-09-05 00:57:35 -07002558 int cmd_is_annotate = !strcmp(argv[0], "annotate");
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002559 struct range_set ranges;
2560 unsigned int range_i;
Eric Sunshine52f4d122013-08-06 09:59:42 -04002561 long anchor;
Junio C Hamanoe68989a2007-02-06 01:52:04 -08002562
Quentin Neill8b504db2015-05-31 14:27:37 -05002563 git_config(git_blame_config, &output_option);
Pierre Habouzit5817da02008-07-08 15:19:34 +02002564 init_revisions(&revs, NULL);
Eugene Letuchy31653c12009-02-20 14:51:11 -08002565 revs.date_mode = blame_date_mode;
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002566 DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
Junio C Hamano3d1aa562012-09-21 13:52:25 -07002567 DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
Eugene Letuchy31653c12009-02-20 14:51:11 -08002568
Junio C Hamano612702e2006-10-20 23:49:31 -07002569 save_commit_buffer = 0;
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002570 dashdash_pos = 0;
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06002571 show_progress = -1;
Junio C Hamano612702e2006-10-20 23:49:31 -07002572
Stephen Boyd9ca11692010-12-05 23:57:42 -08002573 parse_options_start(&ctx, argc, argv, prefix, options,
2574 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
Pierre Habouzit5817da02008-07-08 15:19:34 +02002575 for (;;) {
Pierre Habouzit5817da02008-07-08 15:19:34 +02002576 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
2577 case PARSE_OPT_HELP:
2578 exit(129);
2579 case PARSE_OPT_DONE:
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002580 if (ctx.argv[0])
2581 dashdash_pos = ctx.cpidx;
Pierre Habouzit5817da02008-07-08 15:19:34 +02002582 goto parse_done;
2583 }
2584
2585 if (!strcmp(ctx.argv[0], "--reverse")) {
2586 ctx.argv[0] = "--children";
2587 reverse = 1;
2588 }
Pierre Habouzit6b61ec02008-07-09 23:38:34 +02002589 parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
Pierre Habouzit5817da02008-07-08 15:19:34 +02002590 }
2591parse_done:
Junio C Hamano3d1aa562012-09-21 13:52:25 -07002592 no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
2593 DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
Pierre Habouzit5817da02008-07-08 15:19:34 +02002594 argc = parse_options_end(&ctx);
2595
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06002596 if (incremental || (output_option & OUTPUT_PORCELAIN)) {
2597 if (show_progress > 0)
2598 die("--progress can't be used with --incremental or porcelain formats");
2599 show_progress = 0;
2600 } else if (show_progress < 0)
2601 show_progress = isatty(2);
2602
Junio C Hamanob31272f2012-07-02 00:54:00 -07002603 if (0 < abbrev)
2604 /* one more abbrev length is needed for the boundary commit */
2605 abbrev++;
Namhyung Kim84393bf2011-04-06 11:20:50 +09002606
Junio C Hamanoaa9ea772009-03-18 00:13:03 -07002607 if (revs_file && read_ancestry(revs_file))
Thomas Rastd824cbb2009-06-27 17:58:46 +02002608 die_errno("reading graft file '%s' failed", revs_file);
Junio C Hamanoaa9ea772009-03-18 00:13:03 -07002609
Eugene Letuchy31653c12009-02-20 14:51:11 -08002610 if (cmd_is_annotate) {
Junio C Hamano7ceacdf2008-09-05 00:57:35 -07002611 output_option |= OUTPUT_ANNOTATE_COMPAT;
Jeff Kinga5481a62015-06-25 12:55:02 -04002612 blame_date_mode.type = DATE_ISO8601;
Eugene Letuchy31653c12009-02-20 14:51:11 -08002613 } else {
2614 blame_date_mode = revs.date_mode;
2615 }
2616
2617 /* The maximum width used to show the dates */
Jeff Kinga5481a62015-06-25 12:55:02 -04002618 switch (blame_date_mode.type) {
Eugene Letuchy31653c12009-02-20 14:51:11 -08002619 case DATE_RFC2822:
2620 blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2621 break;
Beat Bolli466fb672014-08-29 18:58:42 +02002622 case DATE_ISO8601_STRICT:
2623 blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
2624 break;
Eugene Letuchy31653c12009-02-20 14:51:11 -08002625 case DATE_ISO8601:
2626 blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2627 break;
2628 case DATE_RAW:
2629 blame_date_width = sizeof("1161298804 -0700");
2630 break;
2631 case DATE_SHORT:
2632 blame_date_width = sizeof("2006-10-19");
2633 break;
2634 case DATE_RELATIVE:
Jiang Xindd755532014-04-22 22:39:10 +08002635 /* TRANSLATORS: This string is used to tell us the maximum
2636 display width for a relative timestamp in "git blame"
2637 output. For C locale, "4 years, 11 months ago", which
2638 takes 22 places, is the longest among various forms of
2639 relative timestamps, but your language may need more or
2640 fewer display columns. */
2641 blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2642 break;
Eugene Letuchy31653c12009-02-20 14:51:11 -08002643 case DATE_NORMAL:
2644 blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2645 break;
Jeff Kingaa1462c2015-06-25 12:55:45 -04002646 case DATE_STRFTIME:
2647 blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2648 break;
Eugene Letuchy31653c12009-02-20 14:51:11 -08002649 }
2650 blame_date_width -= 1; /* strip the null */
Junio C Hamano7ceacdf2008-09-05 00:57:35 -07002651
Junio C Hamanob3123f92008-07-31 00:05:22 -07002652 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
2653 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
2654 PICKAXE_BLAME_COPY_HARDER);
2655
Junio C Hamano4a0fc952006-10-20 15:37:12 -07002656 if (!blame_move_score)
2657 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2658 if (!blame_copy_score)
2659 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2660
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002661 /*
2662 * We have collected options unknown to us in argv[1..unk]
Junio C Hamanocee7f242006-10-19 16:00:04 -07002663 * which are to be passed to revision machinery if we are
Pavel Roskin3dff5372007-02-03 23:49:16 -05002664 * going to do the "bottom" processing.
Junio C Hamanocee7f242006-10-19 16:00:04 -07002665 *
2666 * The remaining are:
2667 *
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02002668 * (1) if dashdash_pos != 0, it is either
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002669 * "blame [revisions] -- <path>" or
2670 * "blame -- <path> <rev>"
Junio C Hamanocee7f242006-10-19 16:00:04 -07002671 *
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02002672 * (2) otherwise, it is one of the two:
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002673 * "blame [revisions] <path>"
2674 * "blame <path> <rev>"
Junio C Hamanocee7f242006-10-19 16:00:04 -07002675 *
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002676 * Note that we must strip out <path> from the arguments: we do not
2677 * want the path pruning but we may want "bottom" processing.
Junio C Hamanocee7f242006-10-19 16:00:04 -07002678 */
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002679 if (dashdash_pos) {
2680 switch (argc - dashdash_pos - 1) {
2681 case 2: /* (1b) */
2682 if (argc != 4)
2683 usage_with_options(blame_opt_usage, options);
2684 /* reorder for the new way: <rev> -- <path> */
2685 argv[1] = argv[3];
2686 argv[3] = argv[2];
2687 argv[2] = "--";
2688 /* FALLTHROUGH */
2689 case 1: /* (1a) */
2690 path = add_prefix(prefix, argv[--argc]);
2691 argv[argc] = NULL;
2692 break;
2693 default:
Pierre Habouzit5817da02008-07-08 15:19:34 +02002694 usage_with_options(blame_opt_usage, options);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002695 }
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002696 } else {
2697 if (argc < 2)
Pierre Habouzit5817da02008-07-08 15:19:34 +02002698 usage_with_options(blame_opt_usage, options);
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002699 path = add_prefix(prefix, argv[argc - 1]);
René Scharfedbe44fa2015-05-19 23:44:23 +02002700 if (argc == 3 && !file_exists(path)) { /* (2b) */
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002701 path = add_prefix(prefix, argv[1]);
2702 argv[1] = argv[2];
Junio C Hamanocee7f242006-10-19 16:00:04 -07002703 }
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002704 argv[argc - 1] = "--";
Junio C Hamanocee7f242006-10-19 16:00:04 -07002705
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002706 setup_work_tree();
René Scharfedbe44fa2015-05-19 23:44:23 +02002707 if (!file_exists(path))
Thomas Rastd824cbb2009-06-27 17:58:46 +02002708 die_errno("cannot stat path '%s'", path);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002709 }
2710
Junio C Hamano8b3dce52009-11-03 06:59:18 -08002711 revs.disable_stdin = 1;
Pierre Habouzit3f8d5202008-07-08 15:19:35 +02002712 setup_revisions(argc, argv, &revs, NULL);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002713 memset(&sb, 0, sizeof(sb));
2714
Junio C Hamano85af7922008-04-02 22:17:53 -07002715 sb.revs = &revs;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002716 if (!reverse) {
Junio C Hamano85af7922008-04-02 22:17:53 -07002717 final_commit_name = prepare_final(&sb);
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002718 sb.commits.compare = compare_commits_by_commit_date;
2719 }
Junio C Hamano85af7922008-04-02 22:17:53 -07002720 else if (contents_from)
Max Kirillov95261972015-10-26 07:29:00 +02002721 die("--contents and --reverse do not blend well.");
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002722 else {
Junio C Hamano85af7922008-04-02 22:17:53 -07002723 final_commit_name = prepare_initial(&sb);
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002724 sb.commits.compare = compare_commits_by_reverse_commit_date;
Max Kirillov700fd282015-10-30 07:01:53 +02002725 if (revs.first_parent_only)
2726 revs.children.name = NULL;
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002727 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002728
2729 if (!sb.final) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002730 /*
2731 * "--not A B -- path" without anything positive;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002732 * do not default to HEAD, but use the working tree
2733 * or "--contents".
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002734 */
Mike Hommey19818202007-11-03 13:22:55 +01002735 setup_work_tree();
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002736 sb.final = fake_working_tree_commit(&sb.revs->diffopt,
2737 path, contents_from);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002738 add_pending_object(&revs, &(sb.final->object), ":");
Junio C Hamanocee7f242006-10-19 16:00:04 -07002739 }
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002740 else if (contents_from)
2741 die("Cannot use --contents with final commit object name");
Junio C Hamanocee7f242006-10-19 16:00:04 -07002742
Max Kirillov700fd282015-10-30 07:01:53 +02002743 if (reverse && revs.first_parent_only) {
Jeff King7cb5f7c2015-11-17 18:22:37 -05002744 final_commit = find_single_final(sb.revs, NULL);
2745 if (!final_commit)
Max Kirillov700fd282015-10-30 07:01:53 +02002746 die("--reverse and --first-parent together require specified latest commit");
Max Kirillov700fd282015-10-30 07:01:53 +02002747 }
2748
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002749 /*
2750 * If we have bottom, this will mark the ancestors of the
Junio C Hamanocee7f242006-10-19 16:00:04 -07002751 * bottom commits we would reach while traversing as
2752 * uninteresting.
2753 */
Martin Koegler3d51e1b2008-02-18 08:31:56 +01002754 if (prepare_revision_walk(&revs))
Stefan Beller20108742014-08-10 23:33:25 +02002755 die(_("revision walk setup failed"));
Junio C Hamanocee7f242006-10-19 16:00:04 -07002756
Max Kirillov700fd282015-10-30 07:01:53 +02002757 if (reverse && revs.first_parent_only) {
2758 struct commit *c = final_commit;
2759
2760 sb.revs->children.name = "children";
2761 while (c->parents &&
brian m. carlsonf2fd0762015-11-10 02:22:28 +00002762 oidcmp(&c->object.oid, &sb.final->object.oid)) {
Max Kirillov700fd282015-10-30 07:01:53 +02002763 struct commit_list *l = xcalloc(1, sizeof(*l));
2764
2765 l->item = c;
2766 if (add_decoration(&sb.revs->children,
2767 &c->parents->item->object, l))
2768 die("BUG: not unique item in first-parent chain");
2769 c = c->parents->item;
2770 }
2771
brian m. carlsonf2fd0762015-11-10 02:22:28 +00002772 if (oidcmp(&c->object.oid, &sb.final->object.oid))
Max Kirillov700fd282015-10-30 07:01:53 +02002773 die("--reverse --first-parent together require range along first-parent chain");
2774 }
2775
brian m. carlsonf2fd0762015-11-10 02:22:28 +00002776 if (is_null_oid(&sb.final->object.oid)) {
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002777 o = sb.final->util;
René Scharfe5c0b13f2014-07-19 17:35:34 +02002778 sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002779 sb.final_buf_size = o->file.size;
2780 }
2781 else {
2782 o = get_origin(&sb, sb.final, path);
Kirill Smelkov90064712010-09-29 15:35:24 +04002783 if (fill_blob_sha1_and_mode(o))
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002784 die("no such path %s in %s", path, final_commit_name);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002785
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002786 if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
Jeff Kinge5450102012-07-28 11:03:01 -04002787 textconv_object(path, o->mode, o->blob_sha1, 1, (char **) &sb.final_buf,
Axel Bonnet3b8a12e2010-06-15 15:58:48 +02002788 &sb.final_buf_size))
2789 ;
2790 else
2791 sb.final_buf = read_sha1_file(o->blob_sha1, &type,
2792 &sb.final_buf_size);
2793
Junio C Hamanoab43e492007-08-25 01:26:20 -07002794 if (!sb.final_buf)
2795 die("Cannot read blob %s for path %s",
2796 sha1_to_hex(o->blob_sha1),
2797 path);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002798 }
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002799 num_read_blob++;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002800 lno = prepare_lines(&sb);
2801
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002802 if (lno && !range_list.nr)
2803 string_list_append(&range_list, xstrdup("1"));
Junio C Hamanocee7f242006-10-19 16:00:04 -07002804
Eric Sunshine52f4d122013-08-06 09:59:42 -04002805 anchor = 1;
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002806 range_set_init(&ranges, range_list.nr);
2807 for (range_i = 0; range_i < range_list.nr; ++range_i) {
2808 long bottom, top;
2809 if (parse_range_arg(range_list.items[range_i].string,
Eric Sunshine52f4d122013-08-06 09:59:42 -04002810 nth_line_cb, &sb, lno, anchor,
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002811 &bottom, &top, sb.path))
2812 usage(blame_usage);
2813 if (lno < top || ((lno || bottom) && lno < bottom))
2814 die("file %s has only %lu lines", path, lno);
2815 if (bottom < 1)
2816 bottom = 1;
2817 if (top < 1)
2818 top = lno;
2819 bottom--;
2820 range_set_append_unsafe(&ranges, bottom, top);
Eric Sunshine52f4d122013-08-06 09:59:42 -04002821 anchor = top + 1;
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002822 }
2823 sort_and_merge_range_set(&ranges);
2824
2825 for (range_i = ranges.nr; range_i > 0; --range_i) {
2826 const struct range *r = &ranges.ranges[range_i - 1];
2827 long bottom = r->start;
2828 long top = r->end;
2829 struct blame_entry *next = ent;
2830 ent = xcalloc(1, sizeof(*ent));
2831 ent->lno = bottom;
2832 ent->num_lines = top - bottom;
2833 ent->suspect = o;
2834 ent->s_lno = bottom;
2835 ent->next = next;
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002836 origin_incref(o);
2837 }
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002838
2839 o->suspects = ent;
2840 prio_queue_put(&sb.commits, o->commit);
2841
Eric Sunshine58dbfa22013-08-06 09:59:38 -04002842 origin_decref(o);
2843
2844 range_set_release(&ranges);
2845 string_list_clear(&range_list, 0);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002846
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002847 sb.ent = NULL;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002848 sb.path = path;
2849
Marius Storm-Olsend551a482009-02-08 15:34:27 +01002850 read_mailmap(&mailmap, NULL);
Junio C Hamanof9567382007-04-27 00:42:15 -07002851
Edmundo Carmona Antoranzaba37f42015-12-12 18:51:03 -06002852 assign_blame(&sb, opt);
2853
Mike Hommeyb92565d2007-11-03 13:22:53 +01002854 if (!incremental)
2855 setup_pager();
2856
Lukas Fleischera46442f2015-01-12 20:59:26 -05002857 free(final_commit_name);
2858
Linus Torvalds717d1462007-01-28 01:34:06 -08002859 if (incremental)
2860 return 0;
2861
David Kastrup7e6ac6e2014-04-26 01:56:49 +02002862 sb.ent = blame_sort(sb.ent, compare_blame_final);
2863
Junio C Hamanocee7f242006-10-19 16:00:04 -07002864 coalesce(&sb);
2865
2866 if (!(output_option & OUTPUT_PORCELAIN))
2867 find_alignment(&sb, &output_option);
2868
2869 output(&sb, output_option);
2870 free((void *)sb.final_buf);
2871 for (ent = sb.ent; ent; ) {
2872 struct blame_entry *e = ent->next;
2873 free(ent);
2874 ent = e;
2875 }
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002876
Junio C Hamano870b39c2006-11-05 11:52:43 -08002877 if (show_stats) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002878 printf("num read blob: %d\n", num_read_blob);
2879 printf("num get patch: %d\n", num_get_patch);
2880 printf("num commits: %d\n", num_commits);
2881 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002882 return 0;
2883}