blob: 99ea0a02cb39bb72b39f147e75a2aa3850bfe3d2 [file] [log] [blame]
Junio C Hamanocee7f242006-10-19 16:00:04 -07001/*
2 * Pickaxe
3 *
4 * Copyright (c) 2006, Junio C Hamano
5 */
6
7#include "cache.h"
8#include "builtin.h"
9#include "blob.h"
10#include "commit.h"
11#include "tag.h"
12#include "tree-walk.h"
13#include "diff.h"
14#include "diffcore.h"
15#include "revision.h"
Linus Torvalds717d1462007-01-28 01:34:06 -080016#include "quote.h"
Junio C Hamanocee7f242006-10-19 16:00:04 -070017#include "xdiff-interface.h"
Junio C Hamano1cfe7732007-01-30 01:11:08 -080018#include "cache-tree.h"
Junio C Hamanof9567382007-04-27 00:42:15 -070019#include "path-list.h"
20#include "mailmap.h"
Junio C Hamanocee7f242006-10-19 16:00:04 -070021
Junio C Hamanoacca6872006-11-08 18:47:54 -080022static char blame_usage[] =
Junio C Hamanob82871b2007-06-09 18:14:56 -070023"git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
Andrew Ruder635f4a32007-04-16 01:20:34 -050024" -c Use the same output mode as git-annotate (Default: off)\n"
Junio C Hamano4c10a5c2006-12-18 14:04:38 -080025" -b Show blank SHA-1 for boundary commits (Default: off)\n"
Andrew Ruder635f4a32007-04-16 01:20:34 -050026" -l Show long commit SHA1 (Default: off)\n"
Junio C Hamano4c10a5c2006-12-18 14:04:38 -080027" --root Do not treat root commits as boundaries (Default: off)\n"
Andrew Ruder635f4a32007-04-16 01:20:34 -050028" -t Show raw timestamp (Default: off)\n"
Junio C Hamanocee7f242006-10-19 16:00:04 -070029" -f, --show-name Show original filename (Default: auto)\n"
30" -n, --show-number Show original linenumber (Default: off)\n"
Junio C Hamano093dc5b2007-04-12 15:50:45 -070031" -s Suppress author name and timestamp (Default: off)\n"
Junio C Hamanocee7f242006-10-19 16:00:04 -070032" -p, --porcelain Show in a format designed for machine consumption\n"
Junio C Hamanob82871b2007-06-09 18:14:56 -070033" -w Ignore whitespace differences\n"
Junio C Hamanocee7f242006-10-19 16:00:04 -070034" -L n,m Process only line range n,m, counting from 1\n"
Junio C Hamano18abd742006-10-19 18:50:17 -070035" -M, -C Find line movements within and across files\n"
Linus Torvalds717d1462007-01-28 01:34:06 -080036" --incremental Show blame entries as we find them, incrementally\n"
Junio C Hamano1cfe7732007-01-30 01:11:08 -080037" --contents file Use <file>'s contents as the final image\n"
Junio C Hamanocee7f242006-10-19 16:00:04 -070038" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
39
40static int longest_file;
41static int longest_author;
42static int max_orig_digits;
43static int max_digits;
Junio C Hamano5ff62c32006-10-20 14:51:12 -070044static int max_score_digits;
Junio C Hamano4c10a5c2006-12-18 14:04:38 -080045static int show_root;
46static int blank_boundary;
Linus Torvalds717d1462007-01-28 01:34:06 -080047static int incremental;
Junio C Hamanoe68989a2007-02-06 01:52:04 -080048static int cmd_is_annotate;
Junio C Hamanob82871b2007-06-09 18:14:56 -070049static int xdl_opts = XDF_NEED_MINIMAL;
Junio C Hamanof9567382007-04-27 00:42:15 -070050static struct path_list mailmap;
Junio C Hamanocee7f242006-10-19 16:00:04 -070051
Junio C Hamano54a4c612006-10-29 03:07:40 -080052#ifndef DEBUG
53#define DEBUG 0
54#endif
55
Junio C Hamanoc2e525d2006-11-05 11:51:41 -080056/* stats */
57static int num_read_blob;
58static int num_get_patch;
59static int num_commits;
60
Junio C Hamanod24bba82006-10-19 18:49:30 -070061#define PICKAXE_BLAME_MOVE 01
Junio C Hamano18abd742006-10-19 18:50:17 -070062#define PICKAXE_BLAME_COPY 02
63#define PICKAXE_BLAME_COPY_HARDER 04
Junio C Hamanoc63777c2007-05-05 21:18:57 -070064#define PICKAXE_BLAME_COPY_HARDEST 010
Junio C Hamanod24bba82006-10-19 18:49:30 -070065
Junio C Hamano4a0fc952006-10-20 15:37:12 -070066/*
67 * blame for a blame_entry with score lower than these thresholds
68 * is not passed to the parent using move/copy logic.
69 */
70static unsigned blame_move_score;
71static unsigned blame_copy_score;
72#define BLAME_DEFAULT_MOVE_SCORE 20
73#define BLAME_DEFAULT_COPY_SCORE 40
74
Junio C Hamanocee7f242006-10-19 16:00:04 -070075/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
76#define METAINFO_SHOWN (1u<<12)
77#define MORE_THAN_ONE_PATH (1u<<13)
78
79/*
Junio C Hamano54a4c612006-10-29 03:07:40 -080080 * One blob in a commit that is being suspected
Junio C Hamanocee7f242006-10-19 16:00:04 -070081 */
82struct origin {
Junio C Hamano54a4c612006-10-29 03:07:40 -080083 int refcnt;
Junio C Hamanocee7f242006-10-19 16:00:04 -070084 struct commit *commit;
Junio C Hamanoc2e525d2006-11-05 11:51:41 -080085 mmfile_t file;
Junio C Hamanocee7f242006-10-19 16:00:04 -070086 unsigned char blob_sha1[20];
87 char path[FLEX_ARRAY];
88};
89
Junio C Hamano1732a1f2007-01-29 17:36:22 -080090/*
91 * Given an origin, prepare mmfile_t structure to be used by the
92 * diff machinery
93 */
Junio C Hamanoc2e525d2006-11-05 11:51:41 -080094static char *fill_origin_blob(struct origin *o, mmfile_t *file)
95{
96 if (!o->file.ptr) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050097 enum object_type type;
Junio C Hamanoc2e525d2006-11-05 11:51:41 -080098 num_read_blob++;
Nicolas Pitre21666f12007-02-26 14:55:59 -050099 file->ptr = read_sha1_file(o->blob_sha1, &type,
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800100 (unsigned long *)(&(file->size)));
Junio C Hamanoab43e492007-08-25 01:26:20 -0700101 if (!file->ptr)
102 die("Cannot read blob %s for path %s",
103 sha1_to_hex(o->blob_sha1),
104 o->path);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800105 o->file = *file;
106 }
107 else
108 *file = o->file;
109 return file->ptr;
110}
111
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800112/*
113 * Origin is refcounted and usually we keep the blob contents to be
114 * reused.
115 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800116static inline struct origin *origin_incref(struct origin *o)
117{
118 if (o)
119 o->refcnt++;
120 return o;
121}
122
123static void origin_decref(struct origin *o)
124{
125 if (o && --o->refcnt <= 0) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800126 if (o->file.ptr)
127 free(o->file.ptr);
Junio C Hamano54a4c612006-10-29 03:07:40 -0800128 memset(o, 0, sizeof(*o));
129 free(o);
130 }
131}
132
Junio C Hamano7c3c7962007-12-11 16:05:50 -0800133static void drop_origin_blob(struct origin *o)
134{
135 if (o->file.ptr) {
136 free(o->file.ptr);
137 o->file.ptr = NULL;
138 }
139}
140
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800141/*
142 * Each group of lines is described by a blame_entry; it can be split
143 * as we pass blame to the parents. They form a linked list in the
144 * scoreboard structure, sorted by the target line number.
145 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700146struct blame_entry {
147 struct blame_entry *prev;
148 struct blame_entry *next;
149
150 /* the first line of this group in the final image;
151 * internally all line numbers are 0 based.
152 */
153 int lno;
154
155 /* how many lines this group has */
156 int num_lines;
157
158 /* the commit that introduced this group into the final image */
159 struct origin *suspect;
160
161 /* true if the suspect is truly guilty; false while we have not
162 * checked if the group came from one of its parents.
163 */
164 char guilty;
165
166 /* the line number of the first line of this group in the
167 * suspect's file; internally all line numbers are 0 based.
168 */
169 int s_lno;
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700170
171 /* how significant this entry is -- cached to avoid
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800172 * scanning the lines over and over.
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700173 */
174 unsigned score;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700175};
176
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800177/*
178 * The current state of the blame assignment.
179 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700180struct scoreboard {
181 /* the final commit (i.e. where we started digging from) */
182 struct commit *final;
183
184 const char *path;
185
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800186 /*
187 * The contents in the final image.
188 * Used by many functions to obtain contents of the nth line,
189 * indexed with scoreboard.lineno[blame_entry.lno].
Junio C Hamanocee7f242006-10-19 16:00:04 -0700190 */
191 const char *final_buf;
192 unsigned long final_buf_size;
193
194 /* linked list of blames */
195 struct blame_entry *ent;
196
Junio C Hamano612702e2006-10-20 23:49:31 -0700197 /* look-up a line in the final buffer */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700198 int num_lines;
199 int *lineno;
200};
201
Junio C Hamano171dccd2007-03-20 23:37:51 -0700202static inline int same_suspect(struct origin *a, struct origin *b)
Junio C Hamano46014762006-10-21 00:41:38 -0700203{
Junio C Hamano171dccd2007-03-20 23:37:51 -0700204 if (a == b)
Junio C Hamano57584d92007-03-19 22:17:10 -0700205 return 1;
Junio C Hamano171dccd2007-03-20 23:37:51 -0700206 if (a->commit != b->commit)
207 return 0;
208 return !strcmp(a->path, b->path);
Junio C Hamano46014762006-10-21 00:41:38 -0700209}
210
Junio C Hamano54a4c612006-10-29 03:07:40 -0800211static void sanity_check_refcnt(struct scoreboard *);
212
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800213/*
214 * If two blame entries that are next to each other came from
215 * contiguous lines in the same origin (i.e. <commit, path> pair),
216 * merge them together.
217 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700218static void coalesce(struct scoreboard *sb)
219{
220 struct blame_entry *ent, *next;
221
222 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
Junio C Hamano171dccd2007-03-20 23:37:51 -0700223 if (same_suspect(ent->suspect, next->suspect) &&
Junio C Hamanocee7f242006-10-19 16:00:04 -0700224 ent->guilty == next->guilty &&
225 ent->s_lno + ent->num_lines == next->s_lno) {
226 ent->num_lines += next->num_lines;
227 ent->next = next->next;
228 if (ent->next)
229 ent->next->prev = ent;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800230 origin_decref(next->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700231 free(next);
Junio C Hamano46014762006-10-21 00:41:38 -0700232 ent->score = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700233 next = ent; /* again */
234 }
235 }
Junio C Hamano54a4c612006-10-29 03:07:40 -0800236
237 if (DEBUG) /* sanity */
238 sanity_check_refcnt(sb);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700239}
240
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800241/*
242 * Given a commit and a path in it, create a new origin structure.
243 * The callers that add blame to the scoreboard should use
244 * get_origin() to obtain shared, refcounted copy instead of calling
245 * this function directly.
246 */
Junio C Hamano854b97f2006-11-04 19:18:50 -0800247static struct origin *make_origin(struct commit *commit, const char *path)
248{
249 struct origin *o;
250 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
251 o->commit = commit;
252 o->refcnt = 1;
253 strcpy(o->path, path);
254 return o;
255}
256
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800257/*
258 * Locate an existing origin or create a new one.
259 */
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700260static struct origin *get_origin(struct scoreboard *sb,
261 struct commit *commit,
262 const char *path)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700263{
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700264 struct blame_entry *e;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700265
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700266 for (e = sb->ent; e; e = e->next) {
267 if (e->suspect->commit == commit &&
268 !strcmp(e->suspect->path, path))
Junio C Hamano54a4c612006-10-29 03:07:40 -0800269 return origin_incref(e->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700270 }
Junio C Hamano854b97f2006-11-04 19:18:50 -0800271 return make_origin(commit, path);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700272}
273
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800274/*
275 * Fill the blob_sha1 field of an origin if it hasn't, so that later
276 * call to fill_origin_blob() can use it to locate the data. blob_sha1
277 * for an origin is also used to pass the blame for the entire file to
278 * the parent to detect the case where a child's blob is identical to
279 * that of its parent's.
280 */
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700281static int fill_blob_sha1(struct origin *origin)
282{
283 unsigned mode;
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700284
285 if (!is_null_sha1(origin->blob_sha1))
286 return 0;
287 if (get_tree_entry(origin->commit->object.sha1,
288 origin->path,
289 origin->blob_sha1, &mode))
290 goto error_out;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500291 if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700292 goto error_out;
293 return 0;
294 error_out:
295 hashclr(origin->blob_sha1);
296 return -1;
297}
298
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800299/*
300 * We have an origin -- check if the same path exists in the
301 * parent and return an origin structure to represent it.
302 */
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700303static struct origin *find_origin(struct scoreboard *sb,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700304 struct commit *parent,
305 struct origin *origin)
306{
307 struct origin *porigin = NULL;
308 struct diff_options diff_opts;
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700309 const char *paths[2];
310
Junio C Hamano0d981c62006-10-31 01:00:01 -0800311 if (parent->util) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800312 /*
313 * Each commit object can cache one origin in that
314 * commit. This is a freestanding copy of origin and
315 * not refcounted.
Junio C Hamano854b97f2006-11-04 19:18:50 -0800316 */
Junio C Hamano0d981c62006-10-31 01:00:01 -0800317 struct origin *cached = parent->util;
Junio C Hamano854b97f2006-11-04 19:18:50 -0800318 if (!strcmp(cached->path, origin->path)) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800319 /*
320 * The same path between origin and its parent
321 * without renaming -- the most common case.
322 */
Junio C Hamano854b97f2006-11-04 19:18:50 -0800323 porigin = get_origin(sb, parent, cached->path);
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800324
325 /*
326 * If the origin was newly created (i.e. get_origin
327 * would call make_origin if none is found in the
328 * scoreboard), it does not know the blob_sha1,
329 * so copy it. Otherwise porigin was in the
330 * scoreboard and already knows blob_sha1.
331 */
Junio C Hamano854b97f2006-11-04 19:18:50 -0800332 if (porigin->refcnt == 1)
333 hashcpy(porigin->blob_sha1, cached->blob_sha1);
334 return porigin;
335 }
336 /* otherwise it was not very useful; free it */
337 free(parent->util);
338 parent->util = NULL;
Junio C Hamano0d981c62006-10-31 01:00:01 -0800339 }
340
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700341 /* See if the origin->path is different between parent
342 * and origin first. Most of the time they are the
343 * same and diff-tree is fairly efficient about this.
344 */
345 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100346 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700347 diff_opts.detect_rename = 0;
348 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
349 paths[0] = origin->path;
350 paths[1] = NULL;
351
352 diff_tree_setup_paths(paths, &diff_opts);
353 if (diff_setup_done(&diff_opts) < 0)
354 die("diff-setup");
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800355
356 if (is_null_sha1(origin->commit->object.sha1))
357 do_diff_cache(parent->tree->object.sha1, &diff_opts);
358 else
359 diff_tree_sha1(parent->tree->object.sha1,
360 origin->commit->tree->object.sha1,
361 "", &diff_opts);
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700362 diffcore_std(&diff_opts);
363
364 /* It is either one entry that says "modified", or "created",
365 * or nothing.
366 */
367 if (!diff_queued_diff.nr) {
368 /* The path is the same as parent */
369 porigin = get_origin(sb, parent, origin->path);
370 hashcpy(porigin->blob_sha1, origin->blob_sha1);
371 }
372 else if (diff_queued_diff.nr != 1)
Junio C Hamanoacca6872006-11-08 18:47:54 -0800373 die("internal error in blame::find_origin");
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700374 else {
375 struct diff_filepair *p = diff_queued_diff.queue[0];
376 switch (p->status) {
377 default:
Junio C Hamanoacca6872006-11-08 18:47:54 -0800378 die("internal error in blame::find_origin (%c)",
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700379 p->status);
380 case 'M':
381 porigin = get_origin(sb, parent, origin->path);
382 hashcpy(porigin->blob_sha1, p->one->sha1);
383 break;
384 case 'A':
385 case 'T':
386 /* Did not exist in parent, or type changed */
387 break;
388 }
389 }
390 diff_flush(&diff_opts);
Mike Hommey03b69c72007-12-11 22:59:55 +0100391 diff_tree_release_paths(&diff_opts);
Junio C Hamano0d981c62006-10-31 01:00:01 -0800392 if (porigin) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800393 /*
394 * Create a freestanding copy that is not part of
395 * the refcounted origin found in the scoreboard, and
396 * cache it in the commit.
397 */
Junio C Hamano854b97f2006-11-04 19:18:50 -0800398 struct origin *cached;
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800399
Junio C Hamano854b97f2006-11-04 19:18:50 -0800400 cached = make_origin(porigin->commit, porigin->path);
401 hashcpy(cached->blob_sha1, porigin->blob_sha1);
402 parent->util = cached;
Junio C Hamano0d981c62006-10-31 01:00:01 -0800403 }
Junio C Hamanof69e7432006-10-30 17:17:41 -0800404 return porigin;
405}
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700406
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800407/*
408 * We have an origin -- find the path that corresponds to it in its
409 * parent and return an origin structure to represent it.
410 */
Junio C Hamanof69e7432006-10-30 17:17:41 -0800411static struct origin *find_rename(struct scoreboard *sb,
412 struct commit *parent,
413 struct origin *origin)
414{
415 struct origin *porigin = NULL;
416 struct diff_options diff_opts;
417 int i;
418 const char *paths[2];
Junio C Hamanocee7f242006-10-19 16:00:04 -0700419
420 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100421 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700422 diff_opts.detect_rename = DIFF_DETECT_RENAME;
423 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamano2f3f8b22006-11-02 00:02:11 -0800424 diff_opts.single_follow = origin->path;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700425 paths[0] = NULL;
426 diff_tree_setup_paths(paths, &diff_opts);
427 if (diff_setup_done(&diff_opts) < 0)
428 die("diff-setup");
Junio C Hamano1cfe7732007-01-30 01:11:08 -0800429
430 if (is_null_sha1(origin->commit->object.sha1))
431 do_diff_cache(parent->tree->object.sha1, &diff_opts);
432 else
433 diff_tree_sha1(parent->tree->object.sha1,
434 origin->commit->tree->object.sha1,
435 "", &diff_opts);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700436 diffcore_std(&diff_opts);
437
438 for (i = 0; i < diff_queued_diff.nr; i++) {
439 struct diff_filepair *p = diff_queued_diff.queue[i];
Junio C Hamano612702e2006-10-20 23:49:31 -0700440 if ((p->status == 'R' || p->status == 'C') &&
Junio C Hamanof6c0e192006-10-21 02:56:33 -0700441 !strcmp(p->two->path, origin->path)) {
442 porigin = get_origin(sb, parent, p->one->path);
443 hashcpy(porigin->blob_sha1, p->one->sha1);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700444 break;
445 }
446 }
447 diff_flush(&diff_opts);
Mike Hommey03b69c72007-12-11 22:59:55 +0100448 diff_tree_release_paths(&diff_opts);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700449 return porigin;
450}
451
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800452/*
453 * Parsing of patch chunks...
454 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700455struct chunk {
456 /* line number in postimage; up to but not including this
457 * line is the same as preimage
458 */
459 int same;
460
461 /* preimage line number after this chunk */
462 int p_next;
463
464 /* postimage line number after this chunk */
465 int t_next;
466};
467
468struct patch {
469 struct chunk *chunks;
470 int num;
471};
472
473struct blame_diff_state {
474 struct xdiff_emit_state xm;
475 struct patch *ret;
476 unsigned hunk_post_context;
477 unsigned hunk_in_pre_context : 1;
478};
479
480static void process_u_diff(void *state_, char *line, unsigned long len)
481{
482 struct blame_diff_state *state = state_;
483 struct chunk *chunk;
484 int off1, off2, len1, len2, num;
485
Junio C Hamanocee7f242006-10-19 16:00:04 -0700486 num = state->ret->num;
487 if (len < 4 || line[0] != '@' || line[1] != '@') {
488 if (state->hunk_in_pre_context && line[0] == ' ')
489 state->ret->chunks[num - 1].same++;
490 else {
491 state->hunk_in_pre_context = 0;
492 if (line[0] == ' ')
493 state->hunk_post_context++;
494 else
495 state->hunk_post_context = 0;
496 }
497 return;
498 }
499
500 if (num && state->hunk_post_context) {
501 chunk = &state->ret->chunks[num - 1];
502 chunk->p_next -= state->hunk_post_context;
503 chunk->t_next -= state->hunk_post_context;
504 }
505 state->ret->num = ++num;
506 state->ret->chunks = xrealloc(state->ret->chunks,
507 sizeof(struct chunk) * num);
508 chunk = &state->ret->chunks[num - 1];
509 if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) {
510 state->ret->num--;
511 return;
512 }
513
514 /* Line numbers in patch output are one based. */
515 off1--;
516 off2--;
517
518 chunk->same = len2 ? off2 : (off2 + 1);
519
520 chunk->p_next = off1 + (len1 ? len1 : 1);
521 chunk->t_next = chunk->same + len2;
522 state->hunk_in_pre_context = 1;
523 state->hunk_post_context = 0;
524}
525
526static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
527 int context)
528{
529 struct blame_diff_state state;
530 xpparam_t xpp;
531 xdemitconf_t xecfg;
532 xdemitcb_t ecb;
533
Junio C Hamanob82871b2007-06-09 18:14:56 -0700534 xpp.flags = xdl_opts;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100535 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanocee7f242006-10-19 16:00:04 -0700536 xecfg.ctxlen = context;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700537 ecb.outf = xdiff_outf;
538 ecb.priv = &state;
539 memset(&state, 0, sizeof(state));
540 state.xm.consume = process_u_diff;
541 state.ret = xmalloc(sizeof(struct patch));
542 state.ret->chunks = NULL;
543 state.ret->num = 0;
544
Junio C Hamanoc279d7e2007-12-13 13:25:07 -0800545 xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700546
547 if (state.ret->num) {
548 struct chunk *chunk;
549 chunk = &state.ret->chunks[state.ret->num - 1];
550 chunk->p_next -= state.hunk_post_context;
551 chunk->t_next -= state.hunk_post_context;
552 }
553 return state.ret;
554}
555
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800556/*
557 * Run diff between two origins and grab the patch output, so that
558 * we can pass blame for lines origin is currently suspected for
559 * to its parent.
560 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700561static struct patch *get_patch(struct origin *parent, struct origin *origin)
562{
563 mmfile_t file_p, file_o;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700564 struct patch *patch;
565
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800566 fill_origin_blob(parent, &file_p);
567 fill_origin_blob(origin, &file_o);
568 if (!file_p.ptr || !file_o.ptr)
Junio C Hamanocee7f242006-10-19 16:00:04 -0700569 return NULL;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700570 patch = compare_buffer(&file_p, &file_o, 0);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -0800571 num_get_patch++;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700572 return patch;
573}
574
575static void free_patch(struct patch *p)
576{
577 free(p->chunks);
578 free(p);
579}
580
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800581/*
Pavel Roskin3dff5372007-02-03 23:49:16 -0500582 * Link in a new blame entry to the scoreboard. Entries that cover the
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800583 * same line range have been removed from the scoreboard previously.
584 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700585static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
586{
587 struct blame_entry *ent, *prev = NULL;
588
Junio C Hamano54a4c612006-10-29 03:07:40 -0800589 origin_incref(e->suspect);
590
Junio C Hamanocee7f242006-10-19 16:00:04 -0700591 for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
592 prev = ent;
593
594 /* prev, if not NULL, is the last one that is below e */
595 e->prev = prev;
596 if (prev) {
597 e->next = prev->next;
598 prev->next = e;
599 }
600 else {
601 e->next = sb->ent;
602 sb->ent = e;
603 }
604 if (e->next)
605 e->next->prev = e;
606}
607
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800608/*
609 * src typically is on-stack; we want to copy the information in it to
610 * an malloced blame_entry that is already on the linked list of the
611 * scoreboard. The origin of dst loses a refcnt while the origin of src
612 * gains one.
613 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700614static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
615{
616 struct blame_entry *p, *n;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800617
Junio C Hamanocee7f242006-10-19 16:00:04 -0700618 p = dst->prev;
619 n = dst->next;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800620 origin_incref(src->suspect);
621 origin_decref(dst->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700622 memcpy(dst, src, sizeof(*src));
623 dst->prev = p;
624 dst->next = n;
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700625 dst->score = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700626}
627
628static const char *nth_line(struct scoreboard *sb, int lno)
629{
630 return sb->final_buf + sb->lineno[lno];
631}
632
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800633/*
634 * It is known that lines between tlno to same came from parent, and e
635 * has an overlap with that range. it also is known that parent's
636 * line plno corresponds to e's line tlno.
637 *
638 * <---- e ----->
639 * <------>
640 * <------------>
641 * <------------>
642 * <------------------>
643 *
644 * Split e into potentially three parts; before this chunk, the chunk
645 * to be blamed for the parent, and after that portion.
646 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800647static void split_overlap(struct blame_entry *split,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700648 struct blame_entry *e,
649 int tlno, int plno, int same,
650 struct origin *parent)
651{
Junio C Hamanocee7f242006-10-19 16:00:04 -0700652 int chunk_end_lno;
653 memset(split, 0, sizeof(struct blame_entry [3]));
654
655 if (e->s_lno < tlno) {
656 /* there is a pre-chunk part not blamed on parent */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800657 split[0].suspect = origin_incref(e->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700658 split[0].lno = e->lno;
659 split[0].s_lno = e->s_lno;
660 split[0].num_lines = tlno - e->s_lno;
661 split[1].lno = e->lno + tlno - e->s_lno;
662 split[1].s_lno = plno;
663 }
664 else {
665 split[1].lno = e->lno;
666 split[1].s_lno = plno + (e->s_lno - tlno);
667 }
668
669 if (same < e->s_lno + e->num_lines) {
670 /* there is a post-chunk part not blamed on parent */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800671 split[2].suspect = origin_incref(e->suspect);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700672 split[2].lno = e->lno + (same - e->s_lno);
673 split[2].s_lno = e->s_lno + (same - e->s_lno);
674 split[2].num_lines = e->s_lno + e->num_lines - same;
675 chunk_end_lno = split[2].lno;
676 }
677 else
678 chunk_end_lno = e->lno + e->num_lines;
679 split[1].num_lines = chunk_end_lno - split[1].lno;
680
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800681 /*
682 * if it turns out there is nothing to blame the parent for,
683 * forget about the splitting. !split[1].suspect signals this.
684 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700685 if (split[1].num_lines < 1)
686 return;
Junio C Hamano54a4c612006-10-29 03:07:40 -0800687 split[1].suspect = origin_incref(parent);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700688}
689
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800690/*
691 * split_overlap() divided an existing blame e into up to three parts
692 * in split. Adjust the linked list of blames in the scoreboard to
693 * reflect the split.
694 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700695static void split_blame(struct scoreboard *sb,
Junio C Hamano54a4c612006-10-29 03:07:40 -0800696 struct blame_entry *split,
Junio C Hamanocee7f242006-10-19 16:00:04 -0700697 struct blame_entry *e)
698{
699 struct blame_entry *new_entry;
700
701 if (split[0].suspect && split[2].suspect) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800702 /* The first part (reuse storage for the existing entry e) */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700703 dup_entry(e, &split[0]);
704
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800705 /* The last part -- me */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700706 new_entry = xmalloc(sizeof(*new_entry));
707 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
708 add_blame_entry(sb, new_entry);
709
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800710 /* ... and the middle part -- parent */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700711 new_entry = xmalloc(sizeof(*new_entry));
712 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
713 add_blame_entry(sb, new_entry);
714 }
715 else if (!split[0].suspect && !split[2].suspect)
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800716 /*
717 * The parent covers the entire area; reuse storage for
718 * e and replace it with the parent.
719 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700720 dup_entry(e, &split[1]);
721 else if (split[0].suspect) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800722 /* me and then parent */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700723 dup_entry(e, &split[0]);
724
725 new_entry = xmalloc(sizeof(*new_entry));
726 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
727 add_blame_entry(sb, new_entry);
728 }
729 else {
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800730 /* parent and then me */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700731 dup_entry(e, &split[1]);
732
733 new_entry = xmalloc(sizeof(*new_entry));
734 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
735 add_blame_entry(sb, new_entry);
736 }
737
Junio C Hamano54a4c612006-10-29 03:07:40 -0800738 if (DEBUG) { /* sanity */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700739 struct blame_entry *ent;
Junio C Hamano612702e2006-10-20 23:49:31 -0700740 int lno = sb->ent->lno, corrupt = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700741
742 for (ent = sb->ent; ent; ent = ent->next) {
743 if (lno != ent->lno)
744 corrupt = 1;
745 if (ent->s_lno < 0)
746 corrupt = 1;
747 lno += ent->num_lines;
748 }
749 if (corrupt) {
Junio C Hamano612702e2006-10-20 23:49:31 -0700750 lno = sb->ent->lno;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700751 for (ent = sb->ent; ent; ent = ent->next) {
752 printf("L %8d l %8d n %8d\n",
753 lno, ent->lno, ent->num_lines);
754 lno = ent->lno + ent->num_lines;
755 }
756 die("oops");
757 }
758 }
759}
760
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800761/*
762 * After splitting the blame, the origins used by the
763 * on-stack blame_entry should lose one refcnt each.
764 */
Junio C Hamano54a4c612006-10-29 03:07:40 -0800765static void decref_split(struct blame_entry *split)
766{
767 int i;
768
769 for (i = 0; i < 3; i++)
770 origin_decref(split[i].suspect);
771}
772
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800773/*
774 * Helper for blame_chunk(). blame_entry e is known to overlap with
775 * the patch hunk; split it and pass blame to the parent.
776 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700777static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
778 int tlno, int plno, int same,
779 struct origin *parent)
780{
781 struct blame_entry split[3];
782
783 split_overlap(split, e, tlno, plno, same, parent);
Junio C Hamano54a4c612006-10-29 03:07:40 -0800784 if (split[1].suspect)
785 split_blame(sb, split, e);
786 decref_split(split);
Junio C Hamanocee7f242006-10-19 16:00:04 -0700787}
788
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800789/*
790 * Find the line number of the last line the target is suspected for.
791 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700792static int find_last_in_target(struct scoreboard *sb, struct origin *target)
793{
794 struct blame_entry *e;
795 int last_in_target = -1;
796
797 for (e = sb->ent; e; e = e->next) {
Junio C Hamano171dccd2007-03-20 23:37:51 -0700798 if (e->guilty || !same_suspect(e->suspect, target))
Junio C Hamanocee7f242006-10-19 16:00:04 -0700799 continue;
800 if (last_in_target < e->s_lno + e->num_lines)
801 last_in_target = e->s_lno + e->num_lines;
802 }
803 return last_in_target;
804}
805
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800806/*
807 * Process one hunk from the patch between the current suspect for
808 * blame_entry e and its parent. Find and split the overlap, and
809 * pass blame to the overlapping part to the parent.
810 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700811static void blame_chunk(struct scoreboard *sb,
812 int tlno, int plno, int same,
813 struct origin *target, struct origin *parent)
814{
Junio C Hamano612702e2006-10-20 23:49:31 -0700815 struct blame_entry *e;
Junio C Hamanocee7f242006-10-19 16:00:04 -0700816
Junio C Hamano612702e2006-10-20 23:49:31 -0700817 for (e = sb->ent; e; e = e->next) {
Junio C Hamano171dccd2007-03-20 23:37:51 -0700818 if (e->guilty || !same_suspect(e->suspect, target))
Junio C Hamanocee7f242006-10-19 16:00:04 -0700819 continue;
820 if (same <= e->s_lno)
821 continue;
822 if (tlno < e->s_lno + e->num_lines)
823 blame_overlap(sb, e, tlno, plno, same, parent);
824 }
825}
826
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800827/*
828 * We are looking at the origin 'target' and aiming to pass blame
829 * for the lines it is suspected to its parent. Run diff to find
830 * which lines came from parent and pass blame for them.
831 */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700832static int pass_blame_to_parent(struct scoreboard *sb,
833 struct origin *target,
834 struct origin *parent)
835{
836 int i, last_in_target, plno, tlno;
837 struct patch *patch;
838
839 last_in_target = find_last_in_target(sb, target);
840 if (last_in_target < 0)
841 return 1; /* nothing remains for this target */
842
843 patch = get_patch(parent, target);
844 plno = tlno = 0;
845 for (i = 0; i < patch->num; i++) {
846 struct chunk *chunk = &patch->chunks[i];
847
Junio C Hamanocee7f242006-10-19 16:00:04 -0700848 blame_chunk(sb, tlno, plno, chunk->same, target, parent);
849 plno = chunk->p_next;
850 tlno = chunk->t_next;
851 }
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800852 /* The rest (i.e. anything after tlno) are the same as the parent */
Junio C Hamanocee7f242006-10-19 16:00:04 -0700853 blame_chunk(sb, tlno, plno, last_in_target, target, parent);
854
855 free_patch(patch);
856 return 0;
857}
858
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800859/*
860 * The lines in blame_entry after splitting blames many times can become
861 * very small and trivial, and at some point it becomes pointless to
862 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
863 * ordinary C program, and it is not worth to say it was copied from
864 * totally unrelated file in the parent.
865 *
866 * Compute how trivial the lines in the blame_entry are.
867 */
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700868static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
869{
870 unsigned score;
871 const char *cp, *ep;
872
873 if (e->score)
874 return e->score;
875
Junio C Hamano612702e2006-10-20 23:49:31 -0700876 score = 1;
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700877 cp = nth_line(sb, e->lno);
878 ep = nth_line(sb, e->lno + e->num_lines);
879 while (cp < ep) {
880 unsigned ch = *((unsigned char *)cp);
881 if (isalnum(ch))
882 score++;
883 cp++;
884 }
885 e->score = score;
886 return score;
887}
888
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800889/*
890 * best_so_far[] and this[] are both a split of an existing blame_entry
891 * that passes blame to the parent. Maintain best_so_far the best split
892 * so far, by comparing this and best_so_far and copying this into
893 * bst_so_far as needed.
894 */
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700895static void copy_split_if_better(struct scoreboard *sb,
Junio C Hamano54a4c612006-10-29 03:07:40 -0800896 struct blame_entry *best_so_far,
897 struct blame_entry *this)
Junio C Hamanod24bba82006-10-19 18:49:30 -0700898{
Junio C Hamano54a4c612006-10-29 03:07:40 -0800899 int i;
900
Junio C Hamanod24bba82006-10-19 18:49:30 -0700901 if (!this[1].suspect)
902 return;
Junio C Hamano5ff62c32006-10-20 14:51:12 -0700903 if (best_so_far[1].suspect) {
904 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
905 return;
906 }
Junio C Hamano54a4c612006-10-29 03:07:40 -0800907
908 for (i = 0; i < 3; i++)
909 origin_incref(this[i].suspect);
910 decref_split(best_so_far);
Junio C Hamanod24bba82006-10-19 18:49:30 -0700911 memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
912}
913
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800914/*
Junio C Hamanodd166aa2007-05-05 09:13:26 -0700915 * We are looking at a part of the final image represented by
916 * ent (tlno and same are offset by ent->s_lno).
917 * tlno is where we are looking at in the final image.
918 * up to (but not including) same match preimage.
919 * plno is where we are looking at in the preimage.
920 *
921 * <-------------- final image ---------------------->
922 * <------ent------>
923 * ^tlno ^same
924 * <---------preimage----->
925 * ^plno
926 *
927 * All line numbers are 0-based.
928 */
929static void handle_split(struct scoreboard *sb,
930 struct blame_entry *ent,
931 int tlno, int plno, int same,
932 struct origin *parent,
933 struct blame_entry *split)
934{
935 if (ent->num_lines <= tlno)
936 return;
937 if (tlno < same) {
938 struct blame_entry this[3];
939 tlno += ent->s_lno;
940 same += ent->s_lno;
941 split_overlap(this, ent, tlno, plno, same, parent);
942 copy_split_if_better(sb, split, this);
943 decref_split(this);
944 }
945}
946
947/*
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800948 * Find the lines from parent that are the same as ent so that
949 * we can pass blames to it. file_p has the blob contents for
950 * the parent.
951 */
Junio C Hamanod24bba82006-10-19 18:49:30 -0700952static void find_copy_in_blob(struct scoreboard *sb,
953 struct blame_entry *ent,
954 struct origin *parent,
Junio C Hamano54a4c612006-10-29 03:07:40 -0800955 struct blame_entry *split,
Junio C Hamanod24bba82006-10-19 18:49:30 -0700956 mmfile_t *file_p)
957{
958 const char *cp;
959 int cnt;
960 mmfile_t file_o;
961 struct patch *patch;
962 int i, plno, tlno;
963
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800964 /*
965 * Prepare mmfile that contains only the lines in ent.
966 */
Junio C Hamanod24bba82006-10-19 18:49:30 -0700967 cp = nth_line(sb, ent->lno);
968 file_o.ptr = (char*) cp;
969 cnt = ent->num_lines;
970
971 while (cnt && cp < sb->final_buf + sb->final_buf_size) {
972 if (*cp++ == '\n')
973 cnt--;
974 }
975 file_o.size = cp - file_o.ptr;
976
977 patch = compare_buffer(file_p, &file_o, 1);
978
Junio C Hamanodd166aa2007-05-05 09:13:26 -0700979 /*
980 * file_o is a part of final image we are annotating.
981 * file_p partially may match that image.
982 */
Junio C Hamanod24bba82006-10-19 18:49:30 -0700983 memset(split, 0, sizeof(struct blame_entry [3]));
984 plno = tlno = 0;
985 for (i = 0; i < patch->num; i++) {
986 struct chunk *chunk = &patch->chunks[i];
987
Junio C Hamanodd166aa2007-05-05 09:13:26 -0700988 handle_split(sb, ent, tlno, plno, chunk->same, parent, split);
Junio C Hamanod24bba82006-10-19 18:49:30 -0700989 plno = chunk->p_next;
990 tlno = chunk->t_next;
991 }
Junio C Hamanodd166aa2007-05-05 09:13:26 -0700992 /* remainder, if any, all match the preimage */
993 handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split);
Junio C Hamanod24bba82006-10-19 18:49:30 -0700994 free_patch(patch);
995}
996
Junio C Hamano1732a1f2007-01-29 17:36:22 -0800997/*
998 * See if lines currently target is suspected for can be attributed to
999 * parent.
1000 */
Junio C Hamanod24bba82006-10-19 18:49:30 -07001001static int find_move_in_parent(struct scoreboard *sb,
1002 struct origin *target,
1003 struct origin *parent)
1004{
Junio C Hamano650e2f62006-11-04 12:37:02 -08001005 int last_in_target, made_progress;
Junio C Hamano46014762006-10-21 00:41:38 -07001006 struct blame_entry *e, split[3];
Junio C Hamanod24bba82006-10-19 18:49:30 -07001007 mmfile_t file_p;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001008
1009 last_in_target = find_last_in_target(sb, target);
1010 if (last_in_target < 0)
1011 return 1; /* nothing remains for this target */
1012
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001013 fill_origin_blob(parent, &file_p);
1014 if (!file_p.ptr)
Junio C Hamanod24bba82006-10-19 18:49:30 -07001015 return 0;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001016
Junio C Hamano650e2f62006-11-04 12:37:02 -08001017 made_progress = 1;
1018 while (made_progress) {
1019 made_progress = 0;
1020 for (e = sb->ent; e; e = e->next) {
Junio C Hamano171dccd2007-03-20 23:37:51 -07001021 if (e->guilty || !same_suspect(e->suspect, target))
Junio C Hamano650e2f62006-11-04 12:37:02 -08001022 continue;
1023 find_copy_in_blob(sb, e, parent, split, &file_p);
1024 if (split[1].suspect &&
1025 blame_move_score < ent_score(sb, &split[1])) {
1026 split_blame(sb, split, e);
1027 made_progress = 1;
1028 }
1029 decref_split(split);
1030 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001031 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001032 return 0;
1033}
1034
Junio C Hamano33494782006-11-04 16:39:03 -08001035struct blame_list {
1036 struct blame_entry *ent;
1037 struct blame_entry split[3];
1038};
1039
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001040/*
1041 * Count the number of entries the target is suspected for,
1042 * and prepare a list of entry and the best split.
1043 */
Junio C Hamano33494782006-11-04 16:39:03 -08001044static struct blame_list *setup_blame_list(struct scoreboard *sb,
1045 struct origin *target,
1046 int *num_ents_p)
1047{
1048 struct blame_entry *e;
1049 int num_ents, i;
1050 struct blame_list *blame_list = NULL;
1051
Junio C Hamano33494782006-11-04 16:39:03 -08001052 for (e = sb->ent, num_ents = 0; e; e = e->next)
Junio C Hamano171dccd2007-03-20 23:37:51 -07001053 if (!e->guilty && same_suspect(e->suspect, target))
Junio C Hamano33494782006-11-04 16:39:03 -08001054 num_ents++;
1055 if (num_ents) {
1056 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1057 for (e = sb->ent, i = 0; e; e = e->next)
Junio C Hamano171dccd2007-03-20 23:37:51 -07001058 if (!e->guilty && same_suspect(e->suspect, target))
Junio C Hamano33494782006-11-04 16:39:03 -08001059 blame_list[i++].ent = e;
1060 }
1061 *num_ents_p = num_ents;
1062 return blame_list;
1063}
1064
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001065/*
1066 * For lines target is suspected for, see if we can find code movement
1067 * across file boundary from the parent commit. porigin is the path
1068 * in the parent we already tried.
1069 */
Junio C Hamano18abd742006-10-19 18:50:17 -07001070static int find_copy_in_parent(struct scoreboard *sb,
1071 struct origin *target,
1072 struct commit *parent,
1073 struct origin *porigin,
1074 int opt)
1075{
1076 struct diff_options diff_opts;
1077 const char *paths[1];
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001078 int i, j;
Junio C Hamano33494782006-11-04 16:39:03 -08001079 int retval;
1080 struct blame_list *blame_list;
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001081 int num_ents;
Junio C Hamano18abd742006-10-19 18:50:17 -07001082
Junio C Hamano33494782006-11-04 16:39:03 -08001083 blame_list = setup_blame_list(sb, target, &num_ents);
1084 if (!blame_list)
Junio C Hamano18abd742006-10-19 18:50:17 -07001085 return 1; /* nothing remains for this target */
1086
1087 diff_setup(&diff_opts);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001088 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Junio C Hamano18abd742006-10-19 18:50:17 -07001089 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1090
Junio C Hamano18abd742006-10-19 18:50:17 -07001091 paths[0] = NULL;
1092 diff_tree_setup_paths(paths, &diff_opts);
1093 if (diff_setup_done(&diff_opts) < 0)
1094 die("diff-setup");
Junio C Hamano33494782006-11-04 16:39:03 -08001095
1096 /* Try "find copies harder" on new path if requested;
1097 * we do not want to use diffcore_rename() actually to
1098 * match things up; find_copies_harder is set only to
1099 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1100 * and this code needs to be after diff_setup_done(), which
1101 * usually makes find-copies-harder imply copy detection.
1102 */
Junio C Hamanoc63777c2007-05-05 21:18:57 -07001103 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1104 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1105 && (!porigin || strcmp(target->path, porigin->path))))
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001106 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
Junio C Hamano33494782006-11-04 16:39:03 -08001107
Junio C Hamano1cfe7732007-01-30 01:11:08 -08001108 if (is_null_sha1(target->commit->object.sha1))
1109 do_diff_cache(parent->tree->object.sha1, &diff_opts);
1110 else
1111 diff_tree_sha1(parent->tree->object.sha1,
1112 target->commit->tree->object.sha1,
1113 "", &diff_opts);
Junio C Hamano18abd742006-10-19 18:50:17 -07001114
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001115 if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
Junio C Hamano33494782006-11-04 16:39:03 -08001116 diffcore_std(&diff_opts);
Junio C Hamanof6c0e192006-10-21 02:56:33 -07001117
Junio C Hamano33494782006-11-04 16:39:03 -08001118 retval = 0;
1119 while (1) {
1120 int made_progress = 0;
Junio C Hamano18abd742006-10-19 18:50:17 -07001121
Junio C Hamano33494782006-11-04 16:39:03 -08001122 for (i = 0; i < diff_queued_diff.nr; i++) {
1123 struct diff_filepair *p = diff_queued_diff.queue[i];
1124 struct origin *norigin;
1125 mmfile_t file_p;
Junio C Hamano33494782006-11-04 16:39:03 -08001126 struct blame_entry this[3];
1127
1128 if (!DIFF_FILE_VALID(p->one))
1129 continue; /* does not exist in parent */
1130 if (porigin && !strcmp(p->one->path, porigin->path))
1131 /* find_move already dealt with this path */
1132 continue;
1133
1134 norigin = get_origin(sb, parent, p->one->path);
1135 hashcpy(norigin->blob_sha1, p->one->sha1);
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001136 fill_origin_blob(norigin, &file_p);
Junio C Hamano33494782006-11-04 16:39:03 -08001137 if (!file_p.ptr)
1138 continue;
1139
1140 for (j = 0; j < num_ents; j++) {
1141 find_copy_in_blob(sb, blame_list[j].ent,
1142 norigin, this, &file_p);
1143 copy_split_if_better(sb, blame_list[j].split,
1144 this);
1145 decref_split(this);
1146 }
Junio C Hamano33494782006-11-04 16:39:03 -08001147 origin_decref(norigin);
1148 }
Junio C Hamano18abd742006-10-19 18:50:17 -07001149
Junio C Hamanoaec8fa12006-10-21 03:30:53 -07001150 for (j = 0; j < num_ents; j++) {
Junio C Hamano33494782006-11-04 16:39:03 -08001151 struct blame_entry *split = blame_list[j].split;
1152 if (split[1].suspect &&
1153 blame_copy_score < ent_score(sb, &split[1])) {
1154 split_blame(sb, split, blame_list[j].ent);
1155 made_progress = 1;
1156 }
1157 decref_split(split);
Junio C Hamano18abd742006-10-19 18:50:17 -07001158 }
Junio C Hamano33494782006-11-04 16:39:03 -08001159 free(blame_list);
1160
1161 if (!made_progress)
1162 break;
1163 blame_list = setup_blame_list(sb, target, &num_ents);
1164 if (!blame_list) {
1165 retval = 1;
1166 break;
1167 }
Junio C Hamano18abd742006-10-19 18:50:17 -07001168 }
1169 diff_flush(&diff_opts);
Mike Hommey03b69c72007-12-11 22:59:55 +01001170 diff_tree_release_paths(&diff_opts);
Junio C Hamano33494782006-11-04 16:39:03 -08001171 return retval;
Junio C Hamano18abd742006-10-19 18:50:17 -07001172}
1173
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001174/*
1175 * The blobs of origin and porigin exactly match, so everything
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001176 * origin is suspected for can be blamed on the parent.
1177 */
1178static void pass_whole_blame(struct scoreboard *sb,
1179 struct origin *origin, struct origin *porigin)
1180{
1181 struct blame_entry *e;
1182
1183 if (!porigin->file.ptr && origin->file.ptr) {
1184 /* Steal its file */
1185 porigin->file = origin->file;
1186 origin->file.ptr = NULL;
1187 }
1188 for (e = sb->ent; e; e = e->next) {
Junio C Hamano171dccd2007-03-20 23:37:51 -07001189 if (!same_suspect(e->suspect, origin))
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001190 continue;
1191 origin_incref(porigin);
1192 origin_decref(e->suspect);
1193 e->suspect = porigin;
1194 }
1195}
1196
Junio C Hamanocee7f242006-10-19 16:00:04 -07001197#define MAXPARENT 16
1198
Junio C Hamanod24bba82006-10-19 18:49:30 -07001199static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001200{
Junio C Hamanof69e7432006-10-30 17:17:41 -08001201 int i, pass;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001202 struct commit *commit = origin->commit;
1203 struct commit_list *parent;
1204 struct origin *parent_origin[MAXPARENT], *porigin;
1205
1206 memset(parent_origin, 0, sizeof(parent_origin));
Junio C Hamanocee7f242006-10-19 16:00:04 -07001207
Junio C Hamanof69e7432006-10-30 17:17:41 -08001208 /* The first pass looks for unrenamed path to optimize for
1209 * common cases, then we look for renames in the second pass.
1210 */
1211 for (pass = 0; pass < 2; pass++) {
1212 struct origin *(*find)(struct scoreboard *,
1213 struct commit *, struct origin *);
1214 find = pass ? find_rename : find_origin;
1215
1216 for (i = 0, parent = commit->parents;
1217 i < MAXPARENT && parent;
1218 parent = parent->next, i++) {
1219 struct commit *p = parent->item;
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001220 int j, same;
Junio C Hamanof69e7432006-10-30 17:17:41 -08001221
1222 if (parent_origin[i])
1223 continue;
1224 if (parse_commit(p))
1225 continue;
Junio C Hamano0d981c62006-10-31 01:00:01 -08001226 porigin = find(sb, p, origin);
Junio C Hamanof69e7432006-10-30 17:17:41 -08001227 if (!porigin)
1228 continue;
1229 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001230 pass_whole_blame(sb, origin, porigin);
Junio C Hamanof69e7432006-10-30 17:17:41 -08001231 origin_decref(porigin);
1232 goto finish;
1233 }
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001234 for (j = same = 0; j < i; j++)
Junio C Hamano33494782006-11-04 16:39:03 -08001235 if (parent_origin[j] &&
1236 !hashcmp(parent_origin[j]->blob_sha1,
Junio C Hamano0421d9f2006-11-04 12:20:09 -08001237 porigin->blob_sha1)) {
1238 same = 1;
1239 break;
1240 }
1241 if (!same)
1242 parent_origin[i] = porigin;
1243 else
1244 origin_decref(porigin);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001245 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001246 }
1247
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08001248 num_commits++;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001249 for (i = 0, parent = commit->parents;
1250 i < MAXPARENT && parent;
1251 parent = parent->next, i++) {
1252 struct origin *porigin = parent_origin[i];
1253 if (!porigin)
1254 continue;
1255 if (pass_blame_to_parent(sb, origin, porigin))
Junio C Hamano54a4c612006-10-29 03:07:40 -08001256 goto finish;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001257 }
Junio C Hamanod24bba82006-10-19 18:49:30 -07001258
1259 /*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001260 * Optionally find moves in parents' files.
Junio C Hamanod24bba82006-10-19 18:49:30 -07001261 */
1262 if (opt & PICKAXE_BLAME_MOVE)
1263 for (i = 0, parent = commit->parents;
1264 i < MAXPARENT && parent;
1265 parent = parent->next, i++) {
1266 struct origin *porigin = parent_origin[i];
1267 if (!porigin)
1268 continue;
1269 if (find_move_in_parent(sb, origin, porigin))
Junio C Hamano54a4c612006-10-29 03:07:40 -08001270 goto finish;
Junio C Hamanod24bba82006-10-19 18:49:30 -07001271 }
1272
Junio C Hamano18abd742006-10-19 18:50:17 -07001273 /*
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001274 * Optionally find copies from parents' files.
Junio C Hamano18abd742006-10-19 18:50:17 -07001275 */
1276 if (opt & PICKAXE_BLAME_COPY)
1277 for (i = 0, parent = commit->parents;
1278 i < MAXPARENT && parent;
1279 parent = parent->next, i++) {
1280 struct origin *porigin = parent_origin[i];
1281 if (find_copy_in_parent(sb, origin, parent->item,
1282 porigin, opt))
Junio C Hamano54a4c612006-10-29 03:07:40 -08001283 goto finish;
Junio C Hamano18abd742006-10-19 18:50:17 -07001284 }
Junio C Hamano54a4c612006-10-29 03:07:40 -08001285
1286 finish:
Junio C Hamano7c3c7962007-12-11 16:05:50 -08001287 for (i = 0; i < MAXPARENT; i++) {
1288 if (parent_origin[i]) {
1289 drop_origin_blob(parent_origin[i]);
1290 origin_decref(parent_origin[i]);
1291 }
1292 }
1293 drop_origin_blob(origin);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001294}
1295
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001296/*
1297 * Information on commits, used for output.
1298 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001299struct commit_info
1300{
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001301 const char *author;
1302 const char *author_mail;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001303 unsigned long author_time;
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001304 const char *author_tz;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001305
1306 /* filled only when asked for details */
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001307 const char *committer;
1308 const char *committer_mail;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001309 unsigned long committer_time;
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001310 const char *committer_tz;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001311
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001312 const char *summary;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001313};
1314
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001315/*
1316 * Parse author/committer line in the commit object buffer
1317 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001318static void get_ac_line(const char *inbuf, const char *what,
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001319 int bufsz, char *person, const char **mail,
1320 unsigned long *time, const char **tz)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001321{
Junio C Hamanof9567382007-04-27 00:42:15 -07001322 int len, tzlen, maillen;
1323 char *tmp, *endp, *timepos;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001324
1325 tmp = strstr(inbuf, what);
1326 if (!tmp)
1327 goto error_out;
1328 tmp += strlen(what);
1329 endp = strchr(tmp, '\n');
1330 if (!endp)
1331 len = strlen(tmp);
1332 else
1333 len = endp - tmp;
1334 if (bufsz <= len) {
1335 error_out:
1336 /* Ugh */
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001337 *mail = *tz = "(unknown)";
Junio C Hamanocee7f242006-10-19 16:00:04 -07001338 *time = 0;
1339 return;
1340 }
1341 memcpy(person, tmp, len);
1342
1343 tmp = person;
1344 tmp += len;
1345 *tmp = 0;
1346 while (*tmp != ' ')
1347 tmp--;
1348 *tz = tmp+1;
Junio C Hamanof9567382007-04-27 00:42:15 -07001349 tzlen = (person+len)-(tmp+1);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001350
1351 *tmp = 0;
1352 while (*tmp != ' ')
1353 tmp--;
1354 *time = strtoul(tmp, NULL, 10);
Junio C Hamanof9567382007-04-27 00:42:15 -07001355 timepos = tmp;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001356
1357 *tmp = 0;
1358 while (*tmp != ' ')
1359 tmp--;
1360 *mail = tmp + 1;
1361 *tmp = 0;
Junio C Hamanof9567382007-04-27 00:42:15 -07001362 maillen = timepos - tmp;
1363
1364 if (!mailmap.nr)
1365 return;
1366
1367 /*
1368 * mailmap expansion may make the name longer.
1369 * make room by pushing stuff down.
1370 */
1371 tmp = person + bufsz - (tzlen + 1);
1372 memmove(tmp, *tz, tzlen);
1373 tmp[tzlen] = 0;
1374 *tz = tmp;
1375
1376 tmp = tmp - (maillen + 1);
1377 memmove(tmp, *mail, maillen);
1378 tmp[maillen] = 0;
1379 *mail = tmp;
1380
1381 /*
1382 * Now, convert e-mail using mailmap
1383 */
1384 map_email(&mailmap, tmp + 1, person, tmp-person-1);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001385}
1386
1387static void get_commit_info(struct commit *commit,
1388 struct commit_info *ret,
1389 int detailed)
1390{
1391 int len;
1392 char *tmp, *endp;
1393 static char author_buf[1024];
1394 static char committer_buf[1024];
1395 static char summary_buf[1024];
1396
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001397 /*
1398 * We've operated without save_commit_buffer, so
Junio C Hamano612702e2006-10-20 23:49:31 -07001399 * we now need to populate them for output.
1400 */
1401 if (!commit->buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -05001402 enum object_type type;
Junio C Hamano612702e2006-10-20 23:49:31 -07001403 unsigned long size;
1404 commit->buffer =
Nicolas Pitre21666f12007-02-26 14:55:59 -05001405 read_sha1_file(commit->object.sha1, &type, &size);
Junio C Hamanoab43e492007-08-25 01:26:20 -07001406 if (!commit->buffer)
1407 die("Cannot read commit %s",
1408 sha1_to_hex(commit->object.sha1));
Junio C Hamano612702e2006-10-20 23:49:31 -07001409 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001410 ret->author = author_buf;
1411 get_ac_line(commit->buffer, "\nauthor ",
1412 sizeof(author_buf), author_buf, &ret->author_mail,
1413 &ret->author_time, &ret->author_tz);
1414
1415 if (!detailed)
1416 return;
1417
1418 ret->committer = committer_buf;
1419 get_ac_line(commit->buffer, "\ncommitter ",
1420 sizeof(committer_buf), committer_buf, &ret->committer_mail,
1421 &ret->committer_time, &ret->committer_tz);
1422
1423 ret->summary = summary_buf;
1424 tmp = strstr(commit->buffer, "\n\n");
1425 if (!tmp) {
1426 error_out:
1427 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1428 return;
1429 }
1430 tmp += 2;
1431 endp = strchr(tmp, '\n');
1432 if (!endp)
Junio C Hamano1cfe7732007-01-30 01:11:08 -08001433 endp = tmp + strlen(tmp);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001434 len = endp - tmp;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08001435 if (len >= sizeof(summary_buf) || len == 0)
Junio C Hamanocee7f242006-10-19 16:00:04 -07001436 goto error_out;
1437 memcpy(summary_buf, tmp, len);
1438 summary_buf[len] = 0;
1439}
1440
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001441/*
1442 * To allow LF and other nonportable characters in pathnames,
1443 * they are c-style quoted as needed.
1444 */
Junio C Hamano46e5e692007-01-28 01:42:31 -08001445static void write_filename_info(const char *path)
1446{
1447 printf("filename ");
Pierre Habouzit663af342007-09-20 00:42:15 +02001448 write_name_quoted(path, stdout, '\n');
Junio C Hamano46e5e692007-01-28 01:42:31 -08001449}
1450
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001451/*
1452 * The blame_entry is found to be guilty for the range. Mark it
1453 * as such, and show it in incremental output.
1454 */
Linus Torvalds717d1462007-01-28 01:34:06 -08001455static void found_guilty_entry(struct blame_entry *ent)
1456{
1457 if (ent->guilty)
1458 return;
1459 ent->guilty = 1;
1460 if (incremental) {
1461 struct origin *suspect = ent->suspect;
1462
1463 printf("%s %d %d %d\n",
1464 sha1_to_hex(suspect->commit->object.sha1),
1465 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1466 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1467 struct commit_info ci;
1468 suspect->commit->object.flags |= METAINFO_SHOWN;
1469 get_commit_info(suspect->commit, &ci, 1);
1470 printf("author %s\n", ci.author);
1471 printf("author-mail %s\n", ci.author_mail);
1472 printf("author-time %lu\n", ci.author_time);
1473 printf("author-tz %s\n", ci.author_tz);
1474 printf("committer %s\n", ci.committer);
1475 printf("committer-mail %s\n", ci.committer_mail);
1476 printf("committer-time %lu\n", ci.committer_time);
1477 printf("committer-tz %s\n", ci.committer_tz);
1478 printf("summary %s\n", ci.summary);
1479 if (suspect->commit->object.flags & UNINTERESTING)
1480 printf("boundary\n");
1481 }
Junio C Hamano46e5e692007-01-28 01:42:31 -08001482 write_filename_info(suspect->path);
Theodore Ts'o06f59e92007-06-29 13:40:46 -04001483 maybe_flush_or_die(stdout, "stdout");
Linus Torvalds717d1462007-01-28 01:34:06 -08001484 }
1485}
1486
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001487/*
1488 * The main loop -- while the scoreboard has lines whose true origin
Pavel Roskin3dff5372007-02-03 23:49:16 -05001489 * is still unknown, pick one blame_entry, and allow its current
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001490 * suspect to pass blames to its parents.
1491 */
Linus Torvalds717d1462007-01-28 01:34:06 -08001492static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1493{
1494 while (1) {
1495 struct blame_entry *ent;
1496 struct commit *commit;
1497 struct origin *suspect = NULL;
1498
1499 /* find one suspect to break down */
1500 for (ent = sb->ent; !suspect && ent; ent = ent->next)
1501 if (!ent->guilty)
1502 suspect = ent->suspect;
1503 if (!suspect)
1504 return; /* all done */
1505
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001506 /*
1507 * We will use this suspect later in the loop,
1508 * so hold onto it in the meantime.
1509 */
Linus Torvalds717d1462007-01-28 01:34:06 -08001510 origin_incref(suspect);
1511 commit = suspect->commit;
1512 if (!commit->object.parsed)
1513 parse_commit(commit);
1514 if (!(commit->object.flags & UNINTERESTING) &&
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001515 !(revs->max_age != -1 && commit->date < revs->max_age))
Linus Torvalds717d1462007-01-28 01:34:06 -08001516 pass_blame(sb, suspect, opt);
1517 else {
1518 commit->object.flags |= UNINTERESTING;
1519 if (commit->object.parsed)
1520 mark_parents_uninteresting(commit);
1521 }
1522 /* treat root commit as boundary */
1523 if (!commit->parents && !show_root)
1524 commit->object.flags |= UNINTERESTING;
1525
1526 /* Take responsibility for the remaining entries */
1527 for (ent = sb->ent; ent; ent = ent->next)
Junio C Hamano171dccd2007-03-20 23:37:51 -07001528 if (same_suspect(ent->suspect, suspect))
Linus Torvalds717d1462007-01-28 01:34:06 -08001529 found_guilty_entry(ent);
1530 origin_decref(suspect);
1531
1532 if (DEBUG) /* sanity */
1533 sanity_check_refcnt(sb);
1534 }
1535}
1536
1537static const char *format_time(unsigned long time, const char *tz_str,
1538 int show_raw_time)
1539{
1540 static char time_buf[128];
1541 time_t t = time;
1542 int minutes, tz;
1543 struct tm *tm;
1544
1545 if (show_raw_time) {
1546 sprintf(time_buf, "%lu %s", time, tz_str);
1547 return time_buf;
1548 }
1549
1550 tz = atoi(tz_str);
1551 minutes = tz < 0 ? -tz : tz;
1552 minutes = (minutes / 100)*60 + (minutes % 100);
1553 minutes = tz < 0 ? -minutes : minutes;
1554 t = time + minutes * 60;
1555 tm = gmtime(&t);
1556
1557 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1558 strcat(time_buf, tz_str);
1559 return time_buf;
1560}
1561
Junio C Hamanocee7f242006-10-19 16:00:04 -07001562#define OUTPUT_ANNOTATE_COMPAT 001
1563#define OUTPUT_LONG_OBJECT_NAME 002
1564#define OUTPUT_RAW_TIMESTAMP 004
1565#define OUTPUT_PORCELAIN 010
1566#define OUTPUT_SHOW_NAME 020
1567#define OUTPUT_SHOW_NUMBER 040
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001568#define OUTPUT_SHOW_SCORE 0100
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001569#define OUTPUT_NO_AUTHOR 0200
Junio C Hamanocee7f242006-10-19 16:00:04 -07001570
1571static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
1572{
1573 int cnt;
1574 const char *cp;
1575 struct origin *suspect = ent->suspect;
1576 char hex[41];
1577
1578 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1579 printf("%s%c%d %d %d\n",
1580 hex,
1581 ent->guilty ? ' ' : '*', // purely for debugging
1582 ent->s_lno + 1,
1583 ent->lno + 1,
1584 ent->num_lines);
1585 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1586 struct commit_info ci;
1587 suspect->commit->object.flags |= METAINFO_SHOWN;
1588 get_commit_info(suspect->commit, &ci, 1);
1589 printf("author %s\n", ci.author);
1590 printf("author-mail %s\n", ci.author_mail);
1591 printf("author-time %lu\n", ci.author_time);
1592 printf("author-tz %s\n", ci.author_tz);
1593 printf("committer %s\n", ci.committer);
1594 printf("committer-mail %s\n", ci.committer_mail);
1595 printf("committer-time %lu\n", ci.committer_time);
1596 printf("committer-tz %s\n", ci.committer_tz);
Junio C Hamano46e5e692007-01-28 01:42:31 -08001597 write_filename_info(suspect->path);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001598 printf("summary %s\n", ci.summary);
Junio C Hamanob11121d2006-12-01 20:45:45 -08001599 if (suspect->commit->object.flags & UNINTERESTING)
1600 printf("boundary\n");
Junio C Hamanocee7f242006-10-19 16:00:04 -07001601 }
1602 else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
Junio C Hamano46e5e692007-01-28 01:42:31 -08001603 write_filename_info(suspect->path);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001604
1605 cp = nth_line(sb, ent->lno);
1606 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1607 char ch;
1608 if (cnt)
1609 printf("%s %d %d\n", hex,
1610 ent->s_lno + 1 + cnt,
1611 ent->lno + 1 + cnt);
1612 putchar('\t');
1613 do {
1614 ch = *cp++;
1615 putchar(ch);
1616 } while (ch != '\n' &&
1617 cp < sb->final_buf + sb->final_buf_size);
1618 }
1619}
1620
1621static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1622{
1623 int cnt;
1624 const char *cp;
1625 struct origin *suspect = ent->suspect;
1626 struct commit_info ci;
1627 char hex[41];
1628 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1629
1630 get_commit_info(suspect->commit, &ci, 1);
1631 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1632
1633 cp = nth_line(sb, ent->lno);
1634 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1635 char ch;
Junio C Hamanob11121d2006-12-01 20:45:45 -08001636 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001637
Junio C Hamanob11121d2006-12-01 20:45:45 -08001638 if (suspect->commit->object.flags & UNINTERESTING) {
Junio C Hamanoe68989a2007-02-06 01:52:04 -08001639 if (blank_boundary)
1640 memset(hex, ' ', length);
1641 else if (!cmd_is_annotate) {
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08001642 length--;
1643 putchar('^');
1644 }
Junio C Hamanob11121d2006-12-01 20:45:45 -08001645 }
1646
1647 printf("%.*s", length, hex);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001648 if (opt & OUTPUT_ANNOTATE_COMPAT)
1649 printf("\t(%10s\t%10s\t%d)", ci.author,
1650 format_time(ci.author_time, ci.author_tz,
1651 show_raw_time),
1652 ent->lno + 1 + cnt);
1653 else {
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001654 if (opt & OUTPUT_SHOW_SCORE)
Junio C Hamano54a4c612006-10-29 03:07:40 -08001655 printf(" %*d %02d",
1656 max_score_digits, ent->score,
1657 ent->suspect->refcnt);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001658 if (opt & OUTPUT_SHOW_NAME)
1659 printf(" %-*.*s", longest_file, longest_file,
1660 suspect->path);
1661 if (opt & OUTPUT_SHOW_NUMBER)
1662 printf(" %*d", max_orig_digits,
1663 ent->s_lno + 1 + cnt);
Junio C Hamano093dc5b2007-04-12 15:50:45 -07001664
1665 if (!(opt & OUTPUT_NO_AUTHOR))
1666 printf(" (%-*.*s %10s",
1667 longest_author, longest_author,
1668 ci.author,
1669 format_time(ci.author_time,
1670 ci.author_tz,
1671 show_raw_time));
1672 printf(" %*d) ",
Junio C Hamanocee7f242006-10-19 16:00:04 -07001673 max_digits, ent->lno + 1 + cnt);
1674 }
1675 do {
1676 ch = *cp++;
1677 putchar(ch);
1678 } while (ch != '\n' &&
1679 cp < sb->final_buf + sb->final_buf_size);
1680 }
1681}
1682
1683static void output(struct scoreboard *sb, int option)
1684{
1685 struct blame_entry *ent;
1686
1687 if (option & OUTPUT_PORCELAIN) {
1688 for (ent = sb->ent; ent; ent = ent->next) {
1689 struct blame_entry *oth;
1690 struct origin *suspect = ent->suspect;
1691 struct commit *commit = suspect->commit;
1692 if (commit->object.flags & MORE_THAN_ONE_PATH)
1693 continue;
1694 for (oth = ent->next; oth; oth = oth->next) {
1695 if ((oth->suspect->commit != commit) ||
1696 !strcmp(oth->suspect->path, suspect->path))
1697 continue;
1698 commit->object.flags |= MORE_THAN_ONE_PATH;
1699 break;
1700 }
1701 }
1702 }
1703
1704 for (ent = sb->ent; ent; ent = ent->next) {
1705 if (option & OUTPUT_PORCELAIN)
1706 emit_porcelain(sb, ent);
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001707 else {
Junio C Hamanocee7f242006-10-19 16:00:04 -07001708 emit_other(sb, ent, option);
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001709 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07001710 }
1711}
1712
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001713/*
1714 * To allow quick access to the contents of nth line in the
1715 * final image, prepare an index in the scoreboard.
1716 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001717static int prepare_lines(struct scoreboard *sb)
1718{
1719 const char *buf = sb->final_buf;
1720 unsigned long len = sb->final_buf_size;
1721 int num = 0, incomplete = 0, bol = 1;
1722
1723 if (len && buf[len-1] != '\n')
1724 incomplete++; /* incomplete line at the end */
1725 while (len--) {
1726 if (bol) {
1727 sb->lineno = xrealloc(sb->lineno,
1728 sizeof(int* ) * (num + 1));
1729 sb->lineno[num] = buf - sb->final_buf;
1730 bol = 0;
1731 }
1732 if (*buf++ == '\n') {
1733 num++;
1734 bol = 1;
1735 }
1736 }
Junio C Hamano1ca6ca82006-10-20 18:48:18 -07001737 sb->lineno = xrealloc(sb->lineno,
1738 sizeof(int* ) * (num + incomplete + 1));
1739 sb->lineno[num + incomplete] = buf - sb->final_buf;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001740 sb->num_lines = num + incomplete;
1741 return sb->num_lines;
1742}
1743
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001744/*
1745 * Add phony grafts for use with -S; this is primarily to
1746 * support git-cvsserver that wants to give a linear history
1747 * to its clients.
1748 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001749static int read_ancestry(const char *graft_file)
1750{
1751 FILE *fp = fopen(graft_file, "r");
1752 char buf[1024];
1753 if (!fp)
1754 return -1;
1755 while (fgets(buf, sizeof(buf), fp)) {
1756 /* The format is just "Commit Parent1 Parent2 ...\n" */
1757 int len = strlen(buf);
1758 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano8eaf7982006-11-10 13:39:01 -08001759 if (graft)
1760 register_commit_graft(graft, 0);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001761 }
1762 fclose(fp);
1763 return 0;
1764}
1765
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001766/*
1767 * How many columns do we need to show line numbers in decimal?
1768 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001769static int lineno_width(int lines)
1770{
Junio C Hamanof2915042007-06-09 18:15:24 -07001771 int i, width;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001772
Junio C Hamanof2915042007-06-09 18:15:24 -07001773 for (width = 1, i = 10; i <= lines + 1; width++)
1774 i *= 10;
1775 return width;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001776}
1777
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001778/*
1779 * How many columns do we need to show line numbers, authors,
1780 * and filenames?
1781 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001782static void find_alignment(struct scoreboard *sb, int *option)
1783{
1784 int longest_src_lines = 0;
1785 int longest_dst_lines = 0;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001786 unsigned largest_score = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001787 struct blame_entry *e;
1788
1789 for (e = sb->ent; e; e = e->next) {
1790 struct origin *suspect = e->suspect;
1791 struct commit_info ci;
1792 int num;
1793
Junio C Hamanoab3bb802006-11-28 22:29:18 -08001794 if (strcmp(suspect->path, sb->path))
1795 *option |= OUTPUT_SHOW_NAME;
1796 num = strlen(suspect->path);
1797 if (longest_file < num)
1798 longest_file = num;
Junio C Hamanocee7f242006-10-19 16:00:04 -07001799 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1800 suspect->commit->object.flags |= METAINFO_SHOWN;
1801 get_commit_info(suspect->commit, &ci, 1);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001802 num = strlen(ci.author);
1803 if (longest_author < num)
1804 longest_author = num;
1805 }
1806 num = e->s_lno + e->num_lines;
1807 if (longest_src_lines < num)
1808 longest_src_lines = num;
1809 num = e->lno + e->num_lines;
1810 if (longest_dst_lines < num)
1811 longest_dst_lines = num;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001812 if (largest_score < ent_score(sb, e))
1813 largest_score = ent_score(sb, e);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001814 }
1815 max_orig_digits = lineno_width(longest_src_lines);
1816 max_digits = lineno_width(longest_dst_lines);
Junio C Hamano5ff62c32006-10-20 14:51:12 -07001817 max_score_digits = lineno_width(largest_score);
Junio C Hamanocee7f242006-10-19 16:00:04 -07001818}
1819
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001820/*
1821 * For debugging -- origin is refcounted, and this asserts that
1822 * we do not underflow.
1823 */
Junio C Hamano54a4c612006-10-29 03:07:40 -08001824static void sanity_check_refcnt(struct scoreboard *sb)
1825{
1826 int baa = 0;
1827 struct blame_entry *ent;
1828
1829 for (ent = sb->ent; ent; ent = ent->next) {
Junio C Hamanoae86ad62006-10-30 14:27:52 -08001830 /* Nobody should have zero or negative refcnt */
Junio C Hamano854b97f2006-11-04 19:18:50 -08001831 if (ent->suspect->refcnt <= 0) {
1832 fprintf(stderr, "%s in %s has negative refcnt %d\n",
1833 ent->suspect->path,
1834 sha1_to_hex(ent->suspect->commit->object.sha1),
1835 ent->suspect->refcnt);
Junio C Hamanoae86ad62006-10-30 14:27:52 -08001836 baa = 1;
Junio C Hamano854b97f2006-11-04 19:18:50 -08001837 }
Junio C Hamanoae86ad62006-10-30 14:27:52 -08001838 }
1839 for (ent = sb->ent; ent; ent = ent->next) {
1840 /* Mark the ones that haven't been checked */
Junio C Hamano54a4c612006-10-29 03:07:40 -08001841 if (0 < ent->suspect->refcnt)
1842 ent->suspect->refcnt = -ent->suspect->refcnt;
Junio C Hamano54a4c612006-10-29 03:07:40 -08001843 }
1844 for (ent = sb->ent; ent; ent = ent->next) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001845 /*
1846 * ... then pick each and see if they have the the
1847 * correct refcnt.
Junio C Hamano54a4c612006-10-29 03:07:40 -08001848 */
1849 int found;
1850 struct blame_entry *e;
1851 struct origin *suspect = ent->suspect;
1852
1853 if (0 < suspect->refcnt)
1854 continue;
Junio C Hamanoae86ad62006-10-30 14:27:52 -08001855 suspect->refcnt = -suspect->refcnt; /* Unmark */
Junio C Hamano54a4c612006-10-29 03:07:40 -08001856 for (found = 0, e = sb->ent; e; e = e->next) {
1857 if (e->suspect != suspect)
1858 continue;
1859 found++;
1860 }
Junio C Hamano854b97f2006-11-04 19:18:50 -08001861 if (suspect->refcnt != found) {
1862 fprintf(stderr, "%s in %s has refcnt %d, not %d\n",
1863 ent->suspect->path,
1864 sha1_to_hex(ent->suspect->commit->object.sha1),
1865 ent->suspect->refcnt, found);
1866 baa = 2;
1867 }
Junio C Hamano54a4c612006-10-29 03:07:40 -08001868 }
1869 if (baa) {
1870 int opt = 0160;
1871 find_alignment(sb, &opt);
1872 output(sb, opt);
Junio C Hamano854b97f2006-11-04 19:18:50 -08001873 die("Baa %d!", baa);
Junio C Hamano54a4c612006-10-29 03:07:40 -08001874 }
1875}
1876
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001877/*
1878 * Used for the command line parsing; check if the path exists
1879 * in the working tree.
1880 */
Junio C Hamanocee7f242006-10-19 16:00:04 -07001881static int has_path_in_work_tree(const char *path)
1882{
1883 struct stat st;
1884 return !lstat(path, &st);
1885}
1886
Junio C Hamano4a0fc952006-10-20 15:37:12 -07001887static unsigned parse_score(const char *arg)
1888{
1889 char *end;
1890 unsigned long score = strtoul(arg, &end, 10);
1891 if (*end)
1892 return 0;
1893 return score;
1894}
1895
Jeff King20239ba2006-11-02 02:22:49 -05001896static const char *add_prefix(const char *prefix, const char *path)
1897{
1898 if (!prefix || !prefix[0])
1899 return path;
1900 return prefix_path(prefix, strlen(prefix), path);
1901}
1902
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001903/*
1904 * Parsing of (comma separated) one item in the -L option
1905 */
Junio C Hamano931233b2006-11-06 17:08:32 -08001906static const char *parse_loc(const char *spec,
1907 struct scoreboard *sb, long lno,
1908 long begin, long *ret)
1909{
1910 char *term;
1911 const char *line;
1912 long num;
1913 int reg_error;
1914 regex_t regexp;
1915 regmatch_t match[1];
1916
Junio C Hamano7bd96412006-11-07 16:20:02 -08001917 /* Allow "-L <something>,+20" to mean starting at <something>
1918 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1919 * <something>.
1920 */
1921 if (1 < begin && (spec[0] == '+' || spec[0] == '-')) {
1922 num = strtol(spec + 1, &term, 10);
1923 if (term != spec + 1) {
1924 if (spec[0] == '-')
1925 num = 0 - num;
1926 if (0 < num)
1927 *ret = begin + num - 2;
1928 else if (!num)
1929 *ret = begin;
1930 else
1931 *ret = begin + num;
1932 return term;
1933 }
1934 return spec;
1935 }
Junio C Hamano931233b2006-11-06 17:08:32 -08001936 num = strtol(spec, &term, 10);
1937 if (term != spec) {
1938 *ret = num;
1939 return term;
1940 }
1941 if (spec[0] != '/')
1942 return spec;
1943
1944 /* it could be a regexp of form /.../ */
1945 for (term = (char*) spec + 1; *term && *term != '/'; term++) {
1946 if (*term == '\\')
1947 term++;
1948 }
1949 if (*term != '/')
1950 return spec;
1951
1952 /* try [spec+1 .. term-1] as regexp */
1953 *term = 0;
1954 begin--; /* input is in human terms */
1955 line = nth_line(sb, begin);
1956
1957 if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
1958 !(reg_error = regexec(&regexp, line, 1, match, 0))) {
1959 const char *cp = line + match[0].rm_so;
1960 const char *nline;
1961
1962 while (begin++ < lno) {
1963 nline = nth_line(sb, begin);
1964 if (line <= cp && cp < nline)
1965 break;
1966 line = nline;
1967 }
1968 *ret = begin;
1969 regfree(&regexp);
1970 *term++ = '/';
1971 return term;
1972 }
1973 else {
1974 char errbuf[1024];
1975 regerror(reg_error, &regexp, errbuf, 1024);
1976 die("-L parameter '%s': %s", spec + 1, errbuf);
1977 }
1978}
1979
Junio C Hamano1732a1f2007-01-29 17:36:22 -08001980/*
1981 * Parsing of -L option
1982 */
Junio C Hamano931233b2006-11-06 17:08:32 -08001983static void prepare_blame_range(struct scoreboard *sb,
1984 const char *bottomtop,
1985 long lno,
1986 long *bottom, long *top)
1987{
1988 const char *term;
1989
1990 term = parse_loc(bottomtop, sb, lno, 1, bottom);
1991 if (*term == ',') {
1992 term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
1993 if (*term)
Junio C Hamanoacca6872006-11-08 18:47:54 -08001994 usage(blame_usage);
Junio C Hamano931233b2006-11-06 17:08:32 -08001995 }
1996 if (*term)
Junio C Hamanoacca6872006-11-08 18:47:54 -08001997 usage(blame_usage);
Junio C Hamano931233b2006-11-06 17:08:32 -08001998}
1999
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08002000static int git_blame_config(const char *var, const char *value)
2001{
2002 if (!strcmp(var, "blame.showroot")) {
2003 show_root = git_config_bool(var, value);
2004 return 0;
2005 }
2006 if (!strcmp(var, "blame.blankboundary")) {
2007 blank_boundary = git_config_bool(var, value);
2008 return 0;
2009 }
2010 return git_default_config(var, value);
2011}
2012
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002013static struct commit *fake_working_tree_commit(const char *path, const char *contents_from)
2014{
2015 struct commit *commit;
2016 struct origin *origin;
2017 unsigned char head_sha1[20];
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002018 struct strbuf buf;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002019 const char *ident;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002020 time_t now;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002021 int size, len;
2022 struct cache_entry *ce;
2023 unsigned mode;
2024
2025 if (get_sha1("HEAD", head_sha1))
2026 die("No such ref: HEAD");
2027
2028 time(&now);
2029 commit = xcalloc(1, sizeof(*commit));
2030 commit->parents = xcalloc(1, sizeof(*commit->parents));
2031 commit->parents->item = lookup_commit_reference(head_sha1);
2032 commit->object.parsed = 1;
2033 commit->date = now;
2034 commit->object.type = OBJ_COMMIT;
2035
2036 origin = make_origin(commit, path);
2037
Pierre Habouzitf1696ee2007-09-10 12:35:04 +02002038 strbuf_init(&buf, 0);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002039 if (!contents_from || strcmp("-", contents_from)) {
2040 struct stat st;
2041 const char *read_from;
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002042 unsigned long fin_size;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002043
2044 if (contents_from) {
2045 if (stat(contents_from, &st) < 0)
2046 die("Cannot stat %s", contents_from);
2047 read_from = contents_from;
2048 }
2049 else {
2050 if (lstat(path, &st) < 0)
2051 die("Cannot lstat %s", path);
2052 read_from = path;
2053 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002054 fin_size = xsize_t(st.st_size);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002055 mode = canon_mode(st.st_mode);
2056 switch (st.st_mode & S_IFMT) {
2057 case S_IFREG:
Pierre Habouzit387e7e12007-09-27 15:25:55 +02002058 if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
2059 die("cannot open or read %s", read_from);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002060 break;
2061 case S_IFLNK:
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002062 if (readlink(read_from, buf.buf, buf.alloc) != fin_size)
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002063 die("cannot readlink %s", read_from);
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002064 buf.len = fin_size;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002065 break;
2066 default:
2067 die("unsupported file type %s", read_from);
2068 }
2069 }
2070 else {
2071 /* Reading from stdin */
2072 contents_from = "standard input";
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002073 mode = 0;
Pierre Habouzitf1696ee2007-09-10 12:35:04 +02002074 if (strbuf_read(&buf, 0, 0) < 0)
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002075 die("read error %s from stdin", strerror(errno));
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002076 }
Marius Storm-Olsen8f353ee2007-10-17 12:57:47 +02002077 convert_to_git(path, buf.buf, buf.len, &buf);
Pierre Habouzitaf6eb822007-09-06 13:20:09 +02002078 origin->file.ptr = buf.buf;
2079 origin->file.size = buf.len;
2080 pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002081 commit->util = origin;
2082
2083 /*
2084 * Read the current index, replace the path entry with
2085 * origin->blob_sha1 without mucking with its mode or type
2086 * bits; we are not going to write this index out -- we just
2087 * want to run "diff-index --cached".
2088 */
2089 discard_cache();
2090 read_cache();
2091
2092 len = strlen(path);
2093 if (!mode) {
2094 int pos = cache_name_pos(path, len);
2095 if (0 <= pos)
2096 mode = ntohl(active_cache[pos]->ce_mode);
2097 else
2098 /* Let's not bother reading from HEAD tree */
2099 mode = S_IFREG | 0644;
2100 }
2101 size = cache_entry_size(len);
2102 ce = xcalloc(1, size);
2103 hashcpy(ce->sha1, origin->blob_sha1);
2104 memcpy(ce->name, path, len);
2105 ce->ce_flags = create_ce_flags(len, 0);
2106 ce->ce_mode = create_ce_mode(mode);
2107 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2108
2109 /*
2110 * We are not going to write this out, so this does not matter
2111 * right now, but someday we might optimize diff-index --cached
2112 * with cache-tree information.
2113 */
2114 cache_tree_invalidate_path(active_cache_tree, path);
2115
2116 commit->buffer = xmalloc(400);
2117 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
Michael Spang1bb88be2007-04-14 17:26:20 -04002118 snprintf(commit->buffer, 400,
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002119 "tree 0000000000000000000000000000000000000000\n"
2120 "parent %s\n"
2121 "author %s\n"
2122 "committer %s\n\n"
2123 "Version of %s from %s\n",
2124 sha1_to_hex(head_sha1),
2125 ident, ident, path, contents_from ? contents_from : path);
2126 return commit;
2127}
2128
Junio C Hamanoacca6872006-11-08 18:47:54 -08002129int cmd_blame(int argc, const char **argv, const char *prefix)
Junio C Hamanocee7f242006-10-19 16:00:04 -07002130{
2131 struct rev_info revs;
2132 const char *path;
2133 struct scoreboard sb;
2134 struct origin *o;
2135 struct blame_entry *ent;
Junio C Hamanod24bba82006-10-19 18:49:30 -07002136 int i, seen_dashdash, unk, opt;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002137 long bottom, top, lno;
2138 int output_option = 0;
Junio C Hamano870b39c2006-11-05 11:52:43 -08002139 int show_stats = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002140 const char *revs_file = NULL;
2141 const char *final_commit_name = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05002142 enum object_type type;
Junio C Hamano931233b2006-11-06 17:08:32 -08002143 const char *bottomtop = NULL;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002144 const char *contents_from = NULL;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002145
Junio C Hamanoe68989a2007-02-06 01:52:04 -08002146 cmd_is_annotate = !strcmp(argv[0], "annotate");
2147
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08002148 git_config(git_blame_config);
Junio C Hamano612702e2006-10-20 23:49:31 -07002149 save_commit_buffer = 0;
2150
Junio C Hamanod24bba82006-10-19 18:49:30 -07002151 opt = 0;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002152 seen_dashdash = 0;
2153 for (unk = i = 1; i < argc; i++) {
2154 const char *arg = argv[i];
2155 if (*arg != '-')
2156 break;
Junio C Hamano4c10a5c2006-12-18 14:04:38 -08002157 else if (!strcmp("-b", arg))
2158 blank_boundary = 1;
2159 else if (!strcmp("--root", arg))
2160 show_root = 1;
Junio C Hamano870b39c2006-11-05 11:52:43 -08002161 else if (!strcmp(arg, "--show-stats"))
2162 show_stats = 1;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002163 else if (!strcmp("-c", arg))
2164 output_option |= OUTPUT_ANNOTATE_COMPAT;
2165 else if (!strcmp("-t", arg))
2166 output_option |= OUTPUT_RAW_TIMESTAMP;
2167 else if (!strcmp("-l", arg))
2168 output_option |= OUTPUT_LONG_OBJECT_NAME;
Junio C Hamano093dc5b2007-04-12 15:50:45 -07002169 else if (!strcmp("-s", arg))
2170 output_option |= OUTPUT_NO_AUTHOR;
Junio C Hamanob82871b2007-06-09 18:14:56 -07002171 else if (!strcmp("-w", arg))
2172 xdl_opts |= XDF_IGNORE_WHITESPACE;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002173 else if (!strcmp("-S", arg) && ++i < argc)
2174 revs_file = argv[i];
Junio C Hamano599065a2007-02-20 01:54:00 -08002175 else if (!prefixcmp(arg, "-M")) {
Junio C Hamanod24bba82006-10-19 18:49:30 -07002176 opt |= PICKAXE_BLAME_MOVE;
Junio C Hamano4a0fc952006-10-20 15:37:12 -07002177 blame_move_score = parse_score(arg+2);
2178 }
Junio C Hamano599065a2007-02-20 01:54:00 -08002179 else if (!prefixcmp(arg, "-C")) {
Junio C Hamanoc63777c2007-05-05 21:18:57 -07002180 /*
2181 * -C enables copy from removed files;
2182 * -C -C enables copy from existing files, but only
2183 * when blaming a new file;
2184 * -C -C -C enables copy from existing files for
2185 * everybody
2186 */
2187 if (opt & PICKAXE_BLAME_COPY_HARDER)
2188 opt |= PICKAXE_BLAME_COPY_HARDEST;
Junio C Hamano18abd742006-10-19 18:50:17 -07002189 if (opt & PICKAXE_BLAME_COPY)
2190 opt |= PICKAXE_BLAME_COPY_HARDER;
2191 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
Junio C Hamano4a0fc952006-10-20 15:37:12 -07002192 blame_copy_score = parse_score(arg+2);
Junio C Hamano18abd742006-10-19 18:50:17 -07002193 }
Junio C Hamano599065a2007-02-20 01:54:00 -08002194 else if (!prefixcmp(arg, "-L")) {
Junio C Hamano2c40f982006-10-29 23:50:38 -08002195 if (!arg[2]) {
2196 if (++i >= argc)
Junio C Hamanoacca6872006-11-08 18:47:54 -08002197 usage(blame_usage);
Junio C Hamano2c40f982006-10-29 23:50:38 -08002198 arg = argv[i];
2199 }
2200 else
2201 arg += 2;
Junio C Hamano931233b2006-11-06 17:08:32 -08002202 if (bottomtop)
Junio C Hamanocee7f242006-10-19 16:00:04 -07002203 die("More than one '-L n,m' option given");
Junio C Hamano931233b2006-11-06 17:08:32 -08002204 bottomtop = arg;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002205 }
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002206 else if (!strcmp("--contents", arg)) {
2207 if (++i >= argc)
2208 usage(blame_usage);
2209 contents_from = argv[i];
2210 }
Linus Torvalds717d1462007-01-28 01:34:06 -08002211 else if (!strcmp("--incremental", arg))
2212 incremental = 1;
Junio C Hamano5ff62c32006-10-20 14:51:12 -07002213 else if (!strcmp("--score-debug", arg))
2214 output_option |= OUTPUT_SHOW_SCORE;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002215 else if (!strcmp("-f", arg) ||
2216 !strcmp("--show-name", arg))
2217 output_option |= OUTPUT_SHOW_NAME;
2218 else if (!strcmp("-n", arg) ||
2219 !strcmp("--show-number", arg))
2220 output_option |= OUTPUT_SHOW_NUMBER;
2221 else if (!strcmp("-p", arg) ||
2222 !strcmp("--porcelain", arg))
2223 output_option |= OUTPUT_PORCELAIN;
2224 else if (!strcmp("--", arg)) {
2225 seen_dashdash = 1;
2226 i++;
2227 break;
2228 }
2229 else
2230 argv[unk++] = arg;
2231 }
2232
Junio C Hamano4a0fc952006-10-20 15:37:12 -07002233 if (!blame_move_score)
2234 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2235 if (!blame_copy_score)
2236 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2237
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002238 /*
2239 * We have collected options unknown to us in argv[1..unk]
Junio C Hamanocee7f242006-10-19 16:00:04 -07002240 * which are to be passed to revision machinery if we are
Pavel Roskin3dff5372007-02-03 23:49:16 -05002241 * going to do the "bottom" processing.
Junio C Hamanocee7f242006-10-19 16:00:04 -07002242 *
2243 * The remaining are:
2244 *
2245 * (1) if seen_dashdash, its either
2246 * "-options -- <path>" or
2247 * "-options -- <path> <rev>".
2248 * but the latter is allowed only if there is no
2249 * options that we passed to revision machinery.
2250 *
2251 * (2) otherwise, we may have "--" somewhere later and
2252 * might be looking at the first one of multiple 'rev'
2253 * parameters (e.g. " master ^next ^maint -- path").
2254 * See if there is a dashdash first, and give the
2255 * arguments before that to revision machinery.
2256 * After that there must be one 'path'.
2257 *
2258 * (3) otherwise, its one of the three:
2259 * "-options <path> <rev>"
2260 * "-options <rev> <path>"
2261 * "-options <path>"
2262 * but again the first one is allowed only if
2263 * there is no options that we passed to revision
2264 * machinery.
2265 */
2266
2267 if (seen_dashdash) {
2268 /* (1) */
2269 if (argc <= i)
Junio C Hamanoacca6872006-11-08 18:47:54 -08002270 usage(blame_usage);
Jeff King20239ba2006-11-02 02:22:49 -05002271 path = add_prefix(prefix, argv[i]);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002272 if (i + 1 == argc - 1) {
2273 if (unk != 1)
Junio C Hamanoacca6872006-11-08 18:47:54 -08002274 usage(blame_usage);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002275 argv[unk++] = argv[i + 1];
2276 }
2277 else if (i + 1 != argc)
2278 /* garbage at end */
Junio C Hamanoacca6872006-11-08 18:47:54 -08002279 usage(blame_usage);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002280 }
2281 else {
2282 int j;
2283 for (j = i; !seen_dashdash && j < argc; j++)
2284 if (!strcmp(argv[j], "--"))
2285 seen_dashdash = j;
2286 if (seen_dashdash) {
Tommi Kyntolaf4421322007-02-16 10:50:58 +02002287 /* (2) */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002288 if (seen_dashdash + 1 != argc - 1)
Junio C Hamanoacca6872006-11-08 18:47:54 -08002289 usage(blame_usage);
Jeff King20239ba2006-11-02 02:22:49 -05002290 path = add_prefix(prefix, argv[seen_dashdash + 1]);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002291 for (j = i; j < seen_dashdash; j++)
2292 argv[unk++] = argv[j];
2293 }
2294 else {
2295 /* (3) */
Tommi Kyntolaf4421322007-02-16 10:50:58 +02002296 if (argc <= i)
2297 usage(blame_usage);
Jeff King20239ba2006-11-02 02:22:49 -05002298 path = add_prefix(prefix, argv[i]);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002299 if (i + 1 == argc - 1) {
2300 final_commit_name = argv[i + 1];
2301
2302 /* if (unk == 1) we could be getting
2303 * old-style
2304 */
2305 if (unk == 1 && !has_path_in_work_tree(path)) {
Jeff King20239ba2006-11-02 02:22:49 -05002306 path = add_prefix(prefix, argv[i + 1]);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002307 final_commit_name = argv[i];
2308 }
2309 }
2310 else if (i != argc - 1)
Junio C Hamanoacca6872006-11-08 18:47:54 -08002311 usage(blame_usage); /* garbage at end */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002312
Johannes Schindelin354e6532007-11-09 11:34:07 +00002313 setup_work_tree();
Junio C Hamanocee7f242006-10-19 16:00:04 -07002314 if (!has_path_in_work_tree(path))
2315 die("cannot stat path %s: %s",
2316 path, strerror(errno));
2317 }
2318 }
2319
2320 if (final_commit_name)
2321 argv[unk++] = final_commit_name;
2322
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002323 /*
2324 * Now we got rev and path. We do not want the path pruning
Junio C Hamanocee7f242006-10-19 16:00:04 -07002325 * but we may want "bottom" processing.
2326 */
Alex Riesen6bee4e42006-11-15 22:52:25 +01002327 argv[unk++] = "--"; /* terminate the rev name */
Junio C Hamanocee7f242006-10-19 16:00:04 -07002328 argv[unk] = NULL;
2329
2330 init_revisions(&revs, NULL);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002331 setup_revisions(unk, argv, &revs, NULL);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002332 memset(&sb, 0, sizeof(sb));
2333
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002334 /*
2335 * There must be one and only one positive commit in the
Junio C Hamanocee7f242006-10-19 16:00:04 -07002336 * revs->pending array.
2337 */
2338 for (i = 0; i < revs.pending.nr; i++) {
2339 struct object *obj = revs.pending.objects[i].item;
2340 if (obj->flags & UNINTERESTING)
2341 continue;
2342 while (obj->type == OBJ_TAG)
2343 obj = deref_tag(obj, NULL, 0);
2344 if (obj->type != OBJ_COMMIT)
2345 die("Non commit %s?",
2346 revs.pending.objects[i].name);
2347 if (sb.final)
2348 die("More than one commit to dig from %s and %s?",
2349 revs.pending.objects[i].name,
2350 final_commit_name);
2351 sb.final = (struct commit *) obj;
2352 final_commit_name = revs.pending.objects[i].name;
2353 }
2354
2355 if (!sb.final) {
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002356 /*
2357 * "--not A B -- path" without anything positive;
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002358 * do not default to HEAD, but use the working tree
2359 * or "--contents".
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002360 */
Mike Hommey19818202007-11-03 13:22:55 +01002361 setup_work_tree();
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002362 sb.final = fake_working_tree_commit(path, contents_from);
2363 add_pending_object(&revs, &(sb.final->object), ":");
Junio C Hamanocee7f242006-10-19 16:00:04 -07002364 }
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002365 else if (contents_from)
2366 die("Cannot use --contents with final commit object name");
Junio C Hamanocee7f242006-10-19 16:00:04 -07002367
Junio C Hamano1732a1f2007-01-29 17:36:22 -08002368 /*
2369 * If we have bottom, this will mark the ancestors of the
Junio C Hamanocee7f242006-10-19 16:00:04 -07002370 * bottom commits we would reach while traversing as
2371 * uninteresting.
2372 */
2373 prepare_revision_walk(&revs);
2374
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002375 if (is_null_sha1(sb.final->object.sha1)) {
2376 char *buf;
2377 o = sb.final->util;
2378 buf = xmalloc(o->file.size + 1);
2379 memcpy(buf, o->file.ptr, o->file.size + 1);
2380 sb.final_buf = buf;
2381 sb.final_buf_size = o->file.size;
2382 }
2383 else {
2384 o = get_origin(&sb, sb.final, path);
2385 if (fill_blob_sha1(o))
2386 die("no such path %s in %s", path, final_commit_name);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002387
Nicolas Pitre21666f12007-02-26 14:55:59 -05002388 sb.final_buf = read_sha1_file(o->blob_sha1, &type,
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002389 &sb.final_buf_size);
Junio C Hamanoab43e492007-08-25 01:26:20 -07002390 if (!sb.final_buf)
2391 die("Cannot read blob %s for path %s",
2392 sha1_to_hex(o->blob_sha1),
2393 path);
Junio C Hamano1cfe7732007-01-30 01:11:08 -08002394 }
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002395 num_read_blob++;
Junio C Hamanocee7f242006-10-19 16:00:04 -07002396 lno = prepare_lines(&sb);
2397
Junio C Hamano931233b2006-11-06 17:08:32 -08002398 bottom = top = 0;
2399 if (bottomtop)
2400 prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
2401 if (bottom && top && top < bottom) {
2402 long tmp;
2403 tmp = top; top = bottom; bottom = tmp;
2404 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002405 if (bottom < 1)
2406 bottom = 1;
2407 if (top < 1)
2408 top = lno;
2409 bottom--;
2410 if (lno < top)
2411 die("file %s has only %lu lines", path, lno);
2412
2413 ent = xcalloc(1, sizeof(*ent));
2414 ent->lno = bottom;
2415 ent->num_lines = top - bottom;
2416 ent->suspect = o;
2417 ent->s_lno = bottom;
2418
2419 sb.ent = ent;
2420 sb.path = path;
2421
2422 if (revs_file && read_ancestry(revs_file))
2423 die("reading graft file %s failed: %s",
2424 revs_file, strerror(errno));
2425
Junio C Hamano50acc582007-05-02 23:58:14 -07002426 read_mailmap(&mailmap, ".mailmap", NULL);
Junio C Hamanof9567382007-04-27 00:42:15 -07002427
Mike Hommeyb92565d2007-11-03 13:22:53 +01002428 if (!incremental)
2429 setup_pager();
2430
Junio C Hamanod24bba82006-10-19 18:49:30 -07002431 assign_blame(&sb, &revs, opt);
Junio C Hamanocee7f242006-10-19 16:00:04 -07002432
Linus Torvalds717d1462007-01-28 01:34:06 -08002433 if (incremental)
2434 return 0;
2435
Junio C Hamanocee7f242006-10-19 16:00:04 -07002436 coalesce(&sb);
2437
2438 if (!(output_option & OUTPUT_PORCELAIN))
2439 find_alignment(&sb, &output_option);
2440
2441 output(&sb, output_option);
2442 free((void *)sb.final_buf);
2443 for (ent = sb.ent; ent; ) {
2444 struct blame_entry *e = ent->next;
2445 free(ent);
2446 ent = e;
2447 }
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002448
Junio C Hamano870b39c2006-11-05 11:52:43 -08002449 if (show_stats) {
Junio C Hamanoc2e525d2006-11-05 11:51:41 -08002450 printf("num read blob: %d\n", num_read_blob);
2451 printf("num get patch: %d\n", num_get_patch);
2452 printf("num commits: %d\n", num_commits);
2453 }
Junio C Hamanocee7f242006-10-19 16:00:04 -07002454 return 0;
2455}