@matheusbsbs / matheus-bagliano / commits / cee7f245dc
Fork 0

git-pickaxe: blame rewritten.

Currently it does what git-blame does, but only faster. More importantly, its internal structure is designed to support content movement (aka cut-and-paste) more easily by allowing more than one paths to be taken from the same commit. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 19, 2006 at 16:00 UTC cee7f245dcaef6dade28464f59420095a9949aac
8 files changed +1314
Documentation/git-pickaxe.txt new
+104
@@ -0,0 +1,104 @@
1 +git-pickaxe(1)
2 +==============
3 +
4 +NAME
5 +----
6 +git-pickaxe - Show what revision and author last modified each line of a file
7 +
8 +SYNOPSIS
9 +--------
10 +'git-pickaxe' [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [<rev>] [--] <file>
11 +
12 +DESCRIPTION
13 +-----------
14 +
15 +Annotates each line in the given file with information from the revision which
16 +last modified the line. Optionally, start annotating from the given revision.
17 +
18 +Also it can limit the range of lines annotated.
19 +
20 +This report doesn't tell you anything about lines which have been deleted or
21 +replaced; you need to use a tool such as gitlink:git-diff[1] or the "pickaxe"
22 +interface briefly mentioned in the following paragraph.
23 +
24 +Apart from supporting file annotation, git also supports searching the
25 +development history for when a code snippet occured in a change. This makes it
26 +possible to track when a code snippet was added to a file, moved or copied
27 +between files, and eventually deleted or replaced. It works by searching for
28 +a text string in the diff. A small example:
29 +
30 +-----------------------------------------------------------------------------
31 +$ git log --pretty=oneline -S'blame_usage'
32 +5040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
33 +ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output
34 +-----------------------------------------------------------------------------
35 +
36 +OPTIONS
37 +-------
38 +-c, --compatibility::
39 + Use the same output mode as gitlink:git-annotate[1] (Default: off).
40 +
41 +-L n,m::
42 + Annotate only the specified line range (lines count from 1).
43 +
44 +-l, --long::
45 + Show long rev (Default: off).
46 +
47 +-t, --time::
48 + Show raw timestamp (Default: off).
49 +
50 +-S, --rev-file <revs-file>::
51 + Use revs from revs-file instead of calling gitlink:git-rev-list[1].
52 +
53 +-f, --show-name::
54 + Show filename in the original commit. By default
55 + filename is shown if there is any line that came from a
56 + file with different name, due to rename detection.
57 +
58 +-n, --show-number::
59 + Show line number in the original commit (Default: off).
60 +
61 +-p, --porcelain::
62 + Show in a format designed for machine consumption.
63 +
64 +-h, --help::
65 + Show help message.
66 +
67 +
68 +THE PORCELAIN FORMAT
69 +--------------------
70 +
71 +In this format, each line is output after a header; the
72 +header at the minumum has the first line which has:
73 +
74 +- 40-byte SHA-1 of the commit the line is attributed to;
75 +- the line number of the line in the original file;
76 +- the line number of the line in the final file;
77 +- on a line that starts a group of line from a different
78 + commit than the previous one, the number of lines in this
79 + group. On subsequent lines this field is absent.
80 +
81 +This header line is followed by the following information
82 +at least once for each commit:
83 +
84 +- author name ("author"), email ("author-mail"), time
85 + ("author-time"), and timezone ("author-tz"); similarly
86 + for committer.
87 +- filename in the commit the line is attributed to.
88 +- the first line of the commit log message ("summary").
89 +
90 +The contents of the actual line is output after the above
91 +header, prefixed by a TAB. This is to allow adding more
92 +header elements later.
93 +
94 +SEE ALSO
95 +--------
96 +gitlink:git-blame[1]
97 +
98 +AUTHOR
99 +------
100 +Written by Junio C Hamano <junkio@cox.net>
101 +
102 +GIT
103 +---
104 +Part of the gitlink:git[7] suite
Documentation/git.txt
+3
@@ -430,6 +430,9 @@ gitlink:git-annotate[1]::
430 gitlink:git-blame[1]::
431 Blame file lines on commits.
432
433 +gitlink:git-pickaxe[1]::
434 + Find out where each line in a file came from.
435 +
436 gitlink:git-check-ref-format[1]::
437 Make sure ref name is well formed.
438
Makefile
+1
@@ -288,6 +288,7 @@ BUILTIN_OBJS = \
288 builtin-mv.o \
289 builtin-name-rev.o \
290 builtin-pack-objects.o \
291 + builtin-pickaxe.o \
292 builtin-prune.o \
293 builtin-prune-packed.o \
294 builtin-push.o \
builtin-pickaxe.c new
+1194
@@ -0,0 +1,1194 @@
1 +/*
2 + * Pickaxe
3 + *
4 + * Copyright (c) 2006, Junio C Hamano
5 + */
6 +
7 +#include "cache.h"
8 +#include "builtin.h"
9 +#include "blob.h"
10 +#include "commit.h"
11 +#include "tag.h"
12 +#include "tree-walk.h"
13 +#include "diff.h"
14 +#include "diffcore.h"
15 +#include "revision.h"
16 +#include "xdiff-interface.h"
17 +
18 +#include <time.h>
19 +#include <sys/time.h>
20 +
21 +static char pickaxe_usage[] =
22 +"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [commit] [--] file\n"
23 +" -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
24 +" -l, --long Show long commit SHA1 (Default: off)\n"
25 +" -t, --time Show raw timestamp (Default: off)\n"
26 +" -f, --show-name Show original filename (Default: auto)\n"
27 +" -n, --show-number Show original linenumber (Default: off)\n"
28 +" -p, --porcelain Show in a format designed for machine consumption\n"
29 +" -L n,m Process only line range n,m, counting from 1\n"
30 +" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
31 +
32 +static int longest_file;
33 +static int longest_author;
34 +static int max_orig_digits;
35 +static int max_digits;
36 +
37 +#define DEBUG 0
38 +
39 +/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
40 +#define METAINFO_SHOWN (1u<<12)
41 +#define MORE_THAN_ONE_PATH (1u<<13)
42 +
43 +/*
44 + * One blob in a commit
45 + */
46 +struct origin {
47 + struct commit *commit;
48 + unsigned char blob_sha1[20];
49 + char path[FLEX_ARRAY];
50 +};
51 +
52 +struct blame_entry {
53 + struct blame_entry *prev;
54 + struct blame_entry *next;
55 +
56 + /* the first line of this group in the final image;
57 + * internally all line numbers are 0 based.
58 + */
59 + int lno;
60 +
61 + /* how many lines this group has */
62 + int num_lines;
63 +
64 + /* the commit that introduced this group into the final image */
65 + struct origin *suspect;
66 +
67 + /* true if the suspect is truly guilty; false while we have not
68 + * checked if the group came from one of its parents.
69 + */
70 + char guilty;
71 +
72 + /* the line number of the first line of this group in the
73 + * suspect's file; internally all line numbers are 0 based.
74 + */
75 + int s_lno;
76 +};
77 +
78 +struct scoreboard {
79 + /* the final commit (i.e. where we started digging from) */
80 + struct commit *final;
81 +
82 + const char *path;
83 +
84 + /* the contents in the final; pointed into by buf pointers of
85 + * blame_entries
86 + */
87 + const char *final_buf;
88 + unsigned long final_buf_size;
89 +
90 + /* linked list of blames */
91 + struct blame_entry *ent;
92 +
93 + int num_lines;
94 + int *lineno;
95 +};
96 +
97 +static void coalesce(struct scoreboard *sb)
98 +{
99 + struct blame_entry *ent, *next;
100 +
101 + for (ent = sb->ent; ent && (next = ent->next); ent = next) {
102 + if (ent->suspect == next->suspect &&
103 + ent->guilty == next->guilty &&
104 + ent->s_lno + ent->num_lines == next->s_lno) {
105 + ent->num_lines += next->num_lines;
106 + ent->next = next->next;
107 + if (ent->next)
108 + ent->next->prev = ent;
109 + free(next);
110 + next = ent; /* again */
111 + }
112 + }
113 +}
114 +
115 +static void free_origin(struct origin *o)
116 +{
117 + free(o);
118 +}
119 +
120 +static struct origin *find_origin(struct scoreboard *sb,
121 + struct commit *commit,
122 + const char *path)
123 +{
124 + struct blame_entry *ent;
125 + struct origin *o;
126 + unsigned mode;
127 + char type[10];
128 +
129 + for (ent = sb->ent; ent; ent = ent->next) {
130 + if (ent->suspect->commit == commit &&
131 + !strcmp(ent->suspect->path, path))
132 + return ent->suspect;
133 + }
134 +
135 + o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
136 + o->commit = commit;
137 + strcpy(o->path, path);
138 + if (get_tree_entry(commit->object.sha1, path, o->blob_sha1, &mode))
139 + goto err_out;
140 + if (sha1_object_info(o->blob_sha1, type, NULL) ||
141 + strcmp(type, blob_type))
142 + goto err_out;
143 + return o;
144 + err_out:
145 + free_origin(o);
146 + return NULL;
147 +}
148 +
149 +static struct origin *find_rename(struct scoreboard *sb,
150 + struct commit *parent,
151 + struct origin *origin)
152 +{
153 + struct origin *porigin = NULL;
154 + struct diff_options diff_opts;
155 + int i;
156 + const char *paths[1];
157 +
158 + diff_setup(&diff_opts);
159 + diff_opts.recursive = 1;
160 + diff_opts.detect_rename = DIFF_DETECT_RENAME;
161 + diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
162 + paths[0] = NULL;
163 + diff_tree_setup_paths(paths, &diff_opts);
164 + if (diff_setup_done(&diff_opts) < 0)
165 + die("diff-setup");
166 + diff_tree_sha1(origin->commit->tree->object.sha1,
167 + parent->tree->object.sha1,
168 + "", &diff_opts);
169 + diffcore_std(&diff_opts);
170 +
171 + for (i = 0; i < diff_queued_diff.nr; i++) {
172 + struct diff_filepair *p = diff_queued_diff.queue[i];
173 + if (p->status == 'R' && !strcmp(p->one->path, origin->path)) {
174 + porigin = find_origin(sb, parent, p->two->path);
175 + break;
176 + }
177 + }
178 + diff_flush(&diff_opts);
179 + return porigin;
180 +}
181 +
182 +struct chunk {
183 + /* line number in postimage; up to but not including this
184 + * line is the same as preimage
185 + */
186 + int same;
187 +
188 + /* preimage line number after this chunk */
189 + int p_next;
190 +
191 + /* postimage line number after this chunk */
192 + int t_next;
193 +};
194 +
195 +struct patch {
196 + struct chunk *chunks;
197 + int num;
198 +};
199 +
200 +struct blame_diff_state {
201 + struct xdiff_emit_state xm;
202 + struct patch *ret;
203 + unsigned hunk_post_context;
204 + unsigned hunk_in_pre_context : 1;
205 +};
206 +
207 +static void process_u_diff(void *state_, char *line, unsigned long len)
208 +{
209 + struct blame_diff_state *state = state_;
210 + struct chunk *chunk;
211 + int off1, off2, len1, len2, num;
212 +
213 + if (DEBUG)
214 + fprintf(stderr, "%.*s", (int) len, line);
215 +
216 + num = state->ret->num;
217 + if (len < 4 || line[0] != '@' || line[1] != '@') {
218 + if (state->hunk_in_pre_context && line[0] == ' ')
219 + state->ret->chunks[num - 1].same++;
220 + else {
221 + state->hunk_in_pre_context = 0;
222 + if (line[0] == ' ')
223 + state->hunk_post_context++;
224 + else
225 + state->hunk_post_context = 0;
226 + }
227 + return;
228 + }
229 +
230 + if (num && state->hunk_post_context) {
231 + chunk = &state->ret->chunks[num - 1];
232 + chunk->p_next -= state->hunk_post_context;
233 + chunk->t_next -= state->hunk_post_context;
234 + }
235 + state->ret->num = ++num;
236 + state->ret->chunks = xrealloc(state->ret->chunks,
237 + sizeof(struct chunk) * num);
238 + chunk = &state->ret->chunks[num - 1];
239 + if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) {
240 + state->ret->num--;
241 + return;
242 + }
243 +
244 + /* Line numbers in patch output are one based. */
245 + off1--;
246 + off2--;
247 +
248 + chunk->same = len2 ? off2 : (off2 + 1);
249 +
250 + chunk->p_next = off1 + (len1 ? len1 : 1);
251 + chunk->t_next = chunk->same + len2;
252 + state->hunk_in_pre_context = 1;
253 + state->hunk_post_context = 0;
254 +}
255 +
256 +static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
257 + int context)
258 +{
259 + struct blame_diff_state state;
260 + xpparam_t xpp;
261 + xdemitconf_t xecfg;
262 + xdemitcb_t ecb;
263 +
264 + xpp.flags = XDF_NEED_MINIMAL;
265 + xecfg.ctxlen = context;
266 + xecfg.flags = 0;
267 + ecb.outf = xdiff_outf;
268 + ecb.priv = &state;
269 + memset(&state, 0, sizeof(state));
270 + state.xm.consume = process_u_diff;
271 + state.ret = xmalloc(sizeof(struct patch));
272 + state.ret->chunks = NULL;
273 + state.ret->num = 0;
274 +
275 + xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
276 +
277 + if (state.ret->num) {
278 + struct chunk *chunk;
279 + chunk = &state.ret->chunks[state.ret->num - 1];
280 + chunk->p_next -= state.hunk_post_context;
281 + chunk->t_next -= state.hunk_post_context;
282 + }
283 + return state.ret;
284 +}
285 +
286 +static struct patch *get_patch(struct origin *parent, struct origin *origin)
287 +{
288 + mmfile_t file_p, file_o;
289 + char type[10];
290 + char *blob_p, *blob_o;
291 + struct patch *patch;
292 +
293 + if (DEBUG) fprintf(stderr, "get patch %.8s %.8s\n",
294 + sha1_to_hex(parent->commit->object.sha1),
295 + sha1_to_hex(origin->commit->object.sha1));
296 +
297 + blob_p = read_sha1_file(parent->blob_sha1, type,
298 + (unsigned long *) &file_p.size);
299 + blob_o = read_sha1_file(origin->blob_sha1, type,
300 + (unsigned long *) &file_o.size);
301 + file_p.ptr = blob_p;
302 + file_o.ptr = blob_o;
303 + if (!file_p.ptr || !file_o.ptr) {
304 + free(blob_p);
305 + free(blob_o);
306 + return NULL;
307 + }
308 +
309 + patch = compare_buffer(&file_p, &file_o, 0);
310 + free(blob_p);
311 + free(blob_o);
312 + return patch;
313 +}
314 +
315 +static void free_patch(struct patch *p)
316 +{
317 + free(p->chunks);
318 + free(p);
319 +}
320 +
321 +static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
322 +{
323 + struct blame_entry *ent, *prev = NULL;
324 +
325 + for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
326 + prev = ent;
327 +
328 + /* prev, if not NULL, is the last one that is below e */
329 + e->prev = prev;
330 + if (prev) {
331 + e->next = prev->next;
332 + prev->next = e;
333 + }
334 + else {
335 + e->next = sb->ent;
336 + sb->ent = e;
337 + }
338 + if (e->next)
339 + e->next->prev = e;
340 +}
341 +
342 +static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
343 +{
344 + struct blame_entry *p, *n;
345 + p = dst->prev;
346 + n = dst->next;
347 + memcpy(dst, src, sizeof(*src));
348 + dst->prev = p;
349 + dst->next = n;
350 +}
351 +
352 +static const char *nth_line(struct scoreboard *sb, int lno)
353 +{
354 + return sb->final_buf + sb->lineno[lno];
355 +}
356 +
357 +static void split_overlap(struct blame_entry split[3],
358 + struct blame_entry *e,
359 + int tlno, int plno, int same,
360 + struct origin *parent)
361 +{
362 + /* it is known that lines between tlno to same came from
363 + * parent, and e has an overlap with that range. it also is
364 + * known that parent's line plno corresponds to e's line tlno.
365 + *
366 + * <---- e ----->
367 + * <------>
368 + * <------------>
369 + * <------------>
370 + * <------------------>
371 + *
372 + * Potentially we need to split e into three parts; before
373 + * this chunk, the chunk to be blamed for parent, and after
374 + * that portion.
375 + */
376 + int chunk_end_lno;
377 + memset(split, 0, sizeof(struct blame_entry [3]));
378 +
379 + if (e->s_lno < tlno) {
380 + /* there is a pre-chunk part not blamed on parent */
381 + split[0].suspect = e->suspect;
382 + split[0].lno = e->lno;
383 + split[0].s_lno = e->s_lno;
384 + split[0].num_lines = tlno - e->s_lno;
385 + split[1].lno = e->lno + tlno - e->s_lno;
386 + split[1].s_lno = plno;
387 + }
388 + else {
389 + split[1].lno = e->lno;
390 + split[1].s_lno = plno + (e->s_lno - tlno);
391 + }
392 +
393 + if (same < e->s_lno + e->num_lines) {
394 + /* there is a post-chunk part not blamed on parent */
395 + split[2].suspect = e->suspect;
396 + split[2].lno = e->lno + (same - e->s_lno);
397 + split[2].s_lno = e->s_lno + (same - e->s_lno);
398 + split[2].num_lines = e->s_lno + e->num_lines - same;
399 + chunk_end_lno = split[2].lno;
400 + }
401 + else
402 + chunk_end_lno = e->lno + e->num_lines;
403 + split[1].num_lines = chunk_end_lno - split[1].lno;
404 +
405 + if (split[1].num_lines < 1)
406 + return;
407 + split[1].suspect = parent;
408 +}
409 +
410 +static void split_blame(struct scoreboard *sb,
411 + struct blame_entry split[3],
412 + struct blame_entry *e)
413 +{
414 + struct blame_entry *new_entry;
415 +
416 + if (split[0].suspect && split[2].suspect) {
417 + /* we need to split e into two and add another for parent */
418 + dup_entry(e, &split[0]);
419 +
420 + new_entry = xmalloc(sizeof(*new_entry));
421 + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
422 + add_blame_entry(sb, new_entry);
423 +
424 + new_entry = xmalloc(sizeof(*new_entry));
425 + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
426 + add_blame_entry(sb, new_entry);
427 + }
428 + else if (!split[0].suspect && !split[2].suspect)
429 + /* parent covers the entire area */
430 + dup_entry(e, &split[1]);
431 + else if (split[0].suspect) {
432 + dup_entry(e, &split[0]);
433 +
434 + new_entry = xmalloc(sizeof(*new_entry));
435 + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
436 + add_blame_entry(sb, new_entry);
437 + }
438 + else {
439 + dup_entry(e, &split[1]);
440 +
441 + new_entry = xmalloc(sizeof(*new_entry));
442 + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
443 + add_blame_entry(sb, new_entry);
444 + }
445 +
446 + if (DEBUG) {
447 + struct blame_entry *ent;
448 + int lno = 0, corrupt = 0;
449 +
450 + for (ent = sb->ent; ent; ent = ent->next) {
451 + if (lno != ent->lno)
452 + corrupt = 1;
453 + if (ent->s_lno < 0)
454 + corrupt = 1;
455 + lno += ent->num_lines;
456 + }
457 + if (corrupt) {
458 + lno = 0;
459 + for (ent = sb->ent; ent; ent = ent->next) {
460 + printf("L %8d l %8d n %8d\n",
461 + lno, ent->lno, ent->num_lines);
462 + lno = ent->lno + ent->num_lines;
463 + }
464 + die("oops");
465 + }
466 + }
467 +}
468 +
469 +static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
470 + int tlno, int plno, int same,
471 + struct origin *parent)
472 +{
473 + struct blame_entry split[3];
474 +
475 + split_overlap(split, e, tlno, plno, same, parent);
476 + if (!split[1].suspect)
477 + return;
478 + split_blame(sb, split, e);
479 +}
480 +
481 +static int find_last_in_target(struct scoreboard *sb, struct origin *target)
482 +{
483 + struct blame_entry *e;
484 + int last_in_target = -1;
485 +
486 + for (e = sb->ent; e; e = e->next) {
487 + if (e->guilty || e->suspect != target)
488 + continue;
489 + if (last_in_target < e->s_lno + e->num_lines)
490 + last_in_target = e->s_lno + e->num_lines;
491 + }
492 + return last_in_target;
493 +}
494 +
495 +static void blame_chunk(struct scoreboard *sb,
496 + int tlno, int plno, int same,
497 + struct origin *target, struct origin *parent)
498 +{
499 + struct blame_entry *e, *n;
500 +
501 + for (e = sb->ent; e; e = n) {
502 + n = e->next;
503 + if (e->guilty || e->suspect != target)
504 + continue;
505 + if (same <= e->s_lno)
506 + continue;
507 + if (tlno < e->s_lno + e->num_lines)
508 + blame_overlap(sb, e, tlno, plno, same, parent);
509 + }
510 +}
511 +
512 +static int pass_blame_to_parent(struct scoreboard *sb,
513 + struct origin *target,
514 + struct origin *parent)
515 +{
516 + int i, last_in_target, plno, tlno;
517 + struct patch *patch;
518 +
519 + last_in_target = find_last_in_target(sb, target);
520 + if (last_in_target < 0)
521 + return 1; /* nothing remains for this target */
522 +
523 + patch = get_patch(parent, target);
524 + plno = tlno = 0;
525 + for (i = 0; i < patch->num; i++) {
526 + struct chunk *chunk = &patch->chunks[i];
527 +
528 + if (DEBUG)
529 + fprintf(stderr,
530 + "plno = %d, tlno = %d, "
531 + "same as parent up to %d, resync %d and %d\n",
532 + plno, tlno,
533 + chunk->same, chunk->p_next, chunk->t_next);
534 + blame_chunk(sb, tlno, plno, chunk->same, target, parent);
535 + plno = chunk->p_next;
536 + tlno = chunk->t_next;
537 + }
538 + /* rest (i.e. anything above tlno) are the same as parent */
539 + blame_chunk(sb, tlno, plno, last_in_target, target, parent);
540 +
541 + free_patch(patch);
542 + return 0;
543 +}
544 +
545 +#define MAXPARENT 16
546 +
547 +static void pass_blame(struct scoreboard *sb, struct origin *origin)
548 +{
549 + int i;
550 + struct commit *commit = origin->commit;
551 + struct commit_list *parent;
552 + struct origin *parent_origin[MAXPARENT], *porigin;
553 +
554 + memset(parent_origin, 0, sizeof(parent_origin));
555 + for (i = 0, parent = commit->parents;
556 + i < MAXPARENT && parent;
557 + parent = parent->next, i++) {
558 + struct commit *p = parent->item;
559 +
560 + if (parse_commit(p))
561 + continue;
562 + porigin = find_origin(sb, parent->item, origin->path);
563 + if (!porigin)
564 + porigin = find_rename(sb, parent->item, origin);
565 + if (!porigin)
566 + continue;
567 + if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
568 + struct blame_entry *e;
569 + for (e = sb->ent; e; e = e->next)
570 + if (e->suspect == origin)
571 + e->suspect = porigin;
572 + /* now everything blamed for origin is blamed for
573 + * porigin, we do not need to keep it anymore.
574 + * Do not free porigin (or the ones we got from
575 + * earlier round); they may still be used elsewhere.
576 + */
577 + free_origin(origin);
578 + return;
579 + }
580 + parent_origin[i] = porigin;
581 + }
582 +
583 + for (i = 0, parent = commit->parents;
584 + i < MAXPARENT && parent;
585 + parent = parent->next, i++) {
586 + struct origin *porigin = parent_origin[i];
587 + if (!porigin)
588 + continue;
589 + if (pass_blame_to_parent(sb, origin, porigin))
590 + return;
591 + }
592 +}
593 +
594 +static void assign_blame(struct scoreboard *sb, struct rev_info *revs)
595 +{
596 + while (1) {
597 + struct blame_entry *ent;
598 + struct commit *commit;
599 + struct origin *suspect = NULL;
600 +
601 + /* find one suspect to break down */
602 + for (ent = sb->ent; !suspect && ent; ent = ent->next)
603 + if (!ent->guilty)
604 + suspect = ent->suspect;
605 + if (!suspect)
606 + return; /* all done */
607 +
608 + commit = suspect->commit;
609 + parse_commit(commit);
610 + if (!(commit->object.flags & UNINTERESTING) &&
611 + !(revs->max_age != -1 && commit->date < revs->max_age))
612 + pass_blame(sb, suspect);
613 +
614 + /* Take responsibility for the remaining entries */
615 + for (ent = sb->ent; ent; ent = ent->next)
616 + if (ent->suspect == suspect)
617 + ent->guilty = 1;
618 + }
619 +}
620 +
621 +static const char *format_time(unsigned long time, const char *tz_str,
622 + int show_raw_time)
623 +{
624 + static char time_buf[128];
625 + time_t t = time;
626 + int minutes, tz;
627 + struct tm *tm;
628 +
629 + if (show_raw_time) {
630 + sprintf(time_buf, "%lu %s", time, tz_str);
631 + return time_buf;
632 + }
633 +
634 + tz = atoi(tz_str);
635 + minutes = tz < 0 ? -tz : tz;
636 + minutes = (minutes / 100)*60 + (minutes % 100);
637 + minutes = tz < 0 ? -minutes : minutes;
638 + t = time + minutes * 60;
639 + tm = gmtime(&t);
640 +
641 + strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
642 + strcat(time_buf, tz_str);
643 + return time_buf;
644 +}
645 +
646 +struct commit_info
647 +{
648 + char *author;
649 + char *author_mail;
650 + unsigned long author_time;
651 + char *author_tz;
652 +
653 + /* filled only when asked for details */
654 + char *committer;
655 + char *committer_mail;
656 + unsigned long committer_time;
657 + char *committer_tz;
658 +
659 + char *summary;
660 +};
661 +
662 +static void get_ac_line(const char *inbuf, const char *what,
663 + int bufsz, char *person, char **mail,
664 + unsigned long *time, char **tz)
665 +{
666 + int len;
667 + char *tmp, *endp;
668 +
669 + tmp = strstr(inbuf, what);
670 + if (!tmp)
671 + goto error_out;
672 + tmp += strlen(what);
673 + endp = strchr(tmp, '\n');
674 + if (!endp)
675 + len = strlen(tmp);
676 + else
677 + len = endp - tmp;
678 + if (bufsz <= len) {
679 + error_out:
680 + /* Ugh */
681 + person = *mail = *tz = "(unknown)";
682 + *time = 0;
683 + return;
684 + }
685 + memcpy(person, tmp, len);
686 +
687 + tmp = person;
688 + tmp += len;
689 + *tmp = 0;
690 + while (*tmp != ' ')
691 + tmp--;
692 + *tz = tmp+1;
693 +
694 + *tmp = 0;
695 + while (*tmp != ' ')
696 + tmp--;
697 + *time = strtoul(tmp, NULL, 10);
698 +
699 + *tmp = 0;
700 + while (*tmp != ' ')
701 + tmp--;
702 + *mail = tmp + 1;
703 + *tmp = 0;
704 +}
705 +
706 +static void get_commit_info(struct commit *commit,
707 + struct commit_info *ret,
708 + int detailed)
709 +{
710 + int len;
711 + char *tmp, *endp;
712 + static char author_buf[1024];
713 + static char committer_buf[1024];
714 + static char summary_buf[1024];
715 +
716 + ret->author = author_buf;
717 + get_ac_line(commit->buffer, "\nauthor ",
718 + sizeof(author_buf), author_buf, &ret->author_mail,
719 + &ret->author_time, &ret->author_tz);
720 +
721 + if (!detailed)
722 + return;
723 +
724 + ret->committer = committer_buf;
725 + get_ac_line(commit->buffer, "\ncommitter ",
726 + sizeof(committer_buf), committer_buf, &ret->committer_mail,
727 + &ret->committer_time, &ret->committer_tz);
728 +
729 + ret->summary = summary_buf;
730 + tmp = strstr(commit->buffer, "\n\n");
731 + if (!tmp) {
732 + error_out:
733 + sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
734 + return;
735 + }
736 + tmp += 2;
737 + endp = strchr(tmp, '\n');
738 + if (!endp)
739 + goto error_out;
740 + len = endp - tmp;
741 + if (len >= sizeof(summary_buf))
742 + goto error_out;
743 + memcpy(summary_buf, tmp, len);
744 + summary_buf[len] = 0;
745 +}
746 +
747 +#define OUTPUT_ANNOTATE_COMPAT 001
748 +#define OUTPUT_LONG_OBJECT_NAME 002
749 +#define OUTPUT_RAW_TIMESTAMP 004
750 +#define OUTPUT_PORCELAIN 010
751 +#define OUTPUT_SHOW_NAME 020
752 +#define OUTPUT_SHOW_NUMBER 040
753 +
754 +static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
755 +{
756 + int cnt;
757 + const char *cp;
758 + struct origin *suspect = ent->suspect;
759 + char hex[41];
760 +
761 + strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
762 + printf("%s%c%d %d %d\n",
763 + hex,
764 + ent->guilty ? ' ' : '*', // purely for debugging
765 + ent->s_lno + 1,
766 + ent->lno + 1,
767 + ent->num_lines);
768 + if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
769 + struct commit_info ci;
770 + suspect->commit->object.flags |= METAINFO_SHOWN;
771 + get_commit_info(suspect->commit, &ci, 1);
772 + printf("author %s\n", ci.author);
773 + printf("author-mail %s\n", ci.author_mail);
774 + printf("author-time %lu\n", ci.author_time);
775 + printf("author-tz %s\n", ci.author_tz);
776 + printf("committer %s\n", ci.committer);
777 + printf("committer-mail %s\n", ci.committer_mail);
778 + printf("committer-time %lu\n", ci.committer_time);
779 + printf("committer-tz %s\n", ci.committer_tz);
780 + printf("filename %s\n", suspect->path);
781 + printf("summary %s\n", ci.summary);
782 + }
783 + else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
784 + printf("filename %s\n", suspect->path);
785 +
786 + cp = nth_line(sb, ent->lno);
787 + for (cnt = 0; cnt < ent->num_lines; cnt++) {
788 + char ch;
789 + if (cnt)
790 + printf("%s %d %d\n", hex,
791 + ent->s_lno + 1 + cnt,
792 + ent->lno + 1 + cnt);
793 + putchar('\t');
794 + do {
795 + ch = *cp++;
796 + putchar(ch);
797 + } while (ch != '\n' &&
798 + cp < sb->final_buf + sb->final_buf_size);
799 + }
800 +}
801 +
802 +static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
803 +{
804 + int cnt;
805 + const char *cp;
806 + struct origin *suspect = ent->suspect;
807 + struct commit_info ci;
808 + char hex[41];
809 + int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
810 +
811 + get_commit_info(suspect->commit, &ci, 1);
812 + strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
813 +
814 + cp = nth_line(sb, ent->lno);
815 + for (cnt = 0; cnt < ent->num_lines; cnt++) {
816 + char ch;
817 +
818 + printf("%.*s", (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8, hex);
819 + if (opt & OUTPUT_ANNOTATE_COMPAT)
820 + printf("\t(%10s\t%10s\t%d)", ci.author,
821 + format_time(ci.author_time, ci.author_tz,
822 + show_raw_time),
823 + ent->lno + 1 + cnt);
824 + else {
825 + if (opt & OUTPUT_SHOW_NAME)
826 + printf(" %-*.*s", longest_file, longest_file,
827 + suspect->path);
828 + if (opt & OUTPUT_SHOW_NUMBER)
829 + printf(" %*d", max_orig_digits,
830 + ent->s_lno + 1 + cnt);
831 + printf(" (%-*.*s %10s %*d) ",
832 + longest_author, longest_author, ci.author,
833 + format_time(ci.author_time, ci.author_tz,
834 + show_raw_time),
835 + max_digits, ent->lno + 1 + cnt);
836 + }
837 + do {
838 + ch = *cp++;
839 + putchar(ch);
840 + } while (ch != '\n' &&
841 + cp < sb->final_buf + sb->final_buf_size);
842 + }
843 +}
844 +
845 +static void output(struct scoreboard *sb, int option)
846 +{
847 + struct blame_entry *ent;
848 +
849 + if (option & OUTPUT_PORCELAIN) {
850 + for (ent = sb->ent; ent; ent = ent->next) {
851 + struct blame_entry *oth;
852 + struct origin *suspect = ent->suspect;
853 + struct commit *commit = suspect->commit;
854 + if (commit->object.flags & MORE_THAN_ONE_PATH)
855 + continue;
856 + for (oth = ent->next; oth; oth = oth->next) {
857 + if ((oth->suspect->commit != commit) ||
858 + !strcmp(oth->suspect->path, suspect->path))
859 + continue;
860 + commit->object.flags |= MORE_THAN_ONE_PATH;
861 + break;
862 + }
863 + }
864 + }
865 +
866 + for (ent = sb->ent; ent; ent = ent->next) {
867 + if (option & OUTPUT_PORCELAIN)
868 + emit_porcelain(sb, ent);
869 + else
870 + emit_other(sb, ent, option);
871 + }
872 +}
873 +
874 +static int prepare_lines(struct scoreboard *sb)
875 +{
876 + const char *buf = sb->final_buf;
877 + unsigned long len = sb->final_buf_size;
878 + int num = 0, incomplete = 0, bol = 1;
879 +
880 + if (len && buf[len-1] != '\n')
881 + incomplete++; /* incomplete line at the end */
882 + while (len--) {
883 + if (bol) {
884 + sb->lineno = xrealloc(sb->lineno,
885 + sizeof(int* ) * (num + 1));
886 + sb->lineno[num] = buf - sb->final_buf;
887 + bol = 0;
888 + }
889 + if (*buf++ == '\n') {
890 + num++;
891 + bol = 1;
892 + }
893 + }
894 + sb->num_lines = num + incomplete;
895 + return sb->num_lines;
896 +}
897 +
898 +static int read_ancestry(const char *graft_file)
899 +{
900 + FILE *fp = fopen(graft_file, "r");
901 + char buf[1024];
902 + if (!fp)
903 + return -1;
904 + while (fgets(buf, sizeof(buf), fp)) {
905 + /* The format is just "Commit Parent1 Parent2 ...\n" */
906 + int len = strlen(buf);
907 + struct commit_graft *graft = read_graft_line(buf, len);
908 + register_commit_graft(graft, 0);
909 + }
910 + fclose(fp);
911 + return 0;
912 +}
913 +
914 +static int lineno_width(int lines)
915 +{
916 + int i, width;
917 +
918 + for (width = 1, i = 10; i <= lines + 1; width++)
919 + i *= 10;
920 + return width;
921 +}
922 +
923 +static void find_alignment(struct scoreboard *sb, int *option)
924 +{
925 + int longest_src_lines = 0;
926 + int longest_dst_lines = 0;
927 + struct blame_entry *e;
928 +
929 + for (e = sb->ent; e; e = e->next) {
930 + struct origin *suspect = e->suspect;
931 + struct commit_info ci;
932 + int num;
933 +
934 + if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
935 + suspect->commit->object.flags |= METAINFO_SHOWN;
936 + get_commit_info(suspect->commit, &ci, 1);
937 + if (strcmp(suspect->path, sb->path))
938 + *option |= OUTPUT_SHOW_NAME;
939 + num = strlen(suspect->path);
940 + if (longest_file < num)
941 + longest_file = num;
942 + num = strlen(ci.author);
943 + if (longest_author < num)
944 + longest_author = num;
945 + }
946 + num = e->s_lno + e->num_lines;
947 + if (longest_src_lines < num)
948 + longest_src_lines = num;
949 + num = e->lno + e->num_lines;
950 + if (longest_dst_lines < num)
951 + longest_dst_lines = num;
952 + }
953 + max_orig_digits = lineno_width(longest_src_lines);
954 + max_digits = lineno_width(longest_dst_lines);
955 +}
956 +
957 +static int has_path_in_work_tree(const char *path)
958 +{
959 + struct stat st;
960 + return !lstat(path, &st);
961 +}
962 +
963 +int cmd_pickaxe(int argc, const char **argv, const char *prefix)
964 +{
965 + struct rev_info revs;
966 + const char *path;
967 + struct scoreboard sb;
968 + struct origin *o;
969 + struct blame_entry *ent;
970 + int i, seen_dashdash, unk;
971 + long bottom, top, lno;
972 + int output_option = 0;
973 + const char *revs_file = NULL;
974 + const char *final_commit_name = NULL;
975 + char type[10];
976 +
977 + bottom = top = 0;
978 + seen_dashdash = 0;
979 + for (unk = i = 1; i < argc; i++) {
980 + const char *arg = argv[i];
981 + if (*arg != '-')
982 + break;
983 + else if (!strcmp("-c", arg))
984 + output_option |= OUTPUT_ANNOTATE_COMPAT;
985 + else if (!strcmp("-t", arg))
986 + output_option |= OUTPUT_RAW_TIMESTAMP;
987 + else if (!strcmp("-l", arg))
988 + output_option |= OUTPUT_LONG_OBJECT_NAME;
989 + else if (!strcmp("-S", arg) && ++i < argc)
990 + revs_file = argv[i];
991 + else if (!strcmp("-L", arg) && ++i < argc) {
992 + char *term;
993 + arg = argv[i];
994 + if (bottom || top)
995 + die("More than one '-L n,m' option given");
996 + bottom = strtol(arg, &term, 10);
997 + if (*term == ',') {
998 + top = strtol(term + 1, &term, 10);
999 + if (*term)
1000 + usage(pickaxe_usage);
1001 + }
1002 + if (bottom && top && top < bottom) {
1003 + unsigned long tmp;
1004 + tmp = top; top = bottom; bottom = tmp;
1005 + }
1006 + }
1007 + else if (!strcmp("-f", arg) ||
1008 + !strcmp("--show-name", arg))
1009 + output_option |= OUTPUT_SHOW_NAME;
1010 + else if (!strcmp("-n", arg) ||
1011 + !strcmp("--show-number", arg))
1012 + output_option |= OUTPUT_SHOW_NUMBER;
1013 + else if (!strcmp("-p", arg) ||
1014 + !strcmp("--porcelain", arg))
1015 + output_option |= OUTPUT_PORCELAIN;
1016 + else if (!strcmp("--", arg)) {
1017 + seen_dashdash = 1;
1018 + i++;
1019 + break;
1020 + }
1021 + else
1022 + argv[unk++] = arg;
1023 + }
1024 +
1025 + /* We have collected options unknown to us in argv[1..unk]
1026 + * which are to be passed to revision machinery if we are
1027 + * going to do the "bottom" procesing.
1028 + *
1029 + * The remaining are:
1030 + *
1031 + * (1) if seen_dashdash, its either
1032 + * "-options -- <path>" or
1033 + * "-options -- <path> <rev>".
1034 + * but the latter is allowed only if there is no
1035 + * options that we passed to revision machinery.
1036 + *
1037 + * (2) otherwise, we may have "--" somewhere later and
1038 + * might be looking at the first one of multiple 'rev'
1039 + * parameters (e.g. " master ^next ^maint -- path").
1040 + * See if there is a dashdash first, and give the
1041 + * arguments before that to revision machinery.
1042 + * After that there must be one 'path'.
1043 + *
1044 + * (3) otherwise, its one of the three:
1045 + * "-options <path> <rev>"
1046 + * "-options <rev> <path>"
1047 + * "-options <path>"
1048 + * but again the first one is allowed only if
1049 + * there is no options that we passed to revision
1050 + * machinery.
1051 + */
1052 +
1053 + if (seen_dashdash) {
1054 + /* (1) */
1055 + if (argc <= i)
1056 + usage(pickaxe_usage);
1057 + path = argv[i];
1058 + if (i + 1 == argc - 1) {
1059 + if (unk != 1)
1060 + usage(pickaxe_usage);
1061 + argv[unk++] = argv[i + 1];
1062 + }
1063 + else if (i + 1 != argc)
1064 + /* garbage at end */
1065 + usage(pickaxe_usage);
1066 + }
1067 + else {
1068 + int j;
1069 + for (j = i; !seen_dashdash && j < argc; j++)
1070 + if (!strcmp(argv[j], "--"))
1071 + seen_dashdash = j;
1072 + if (seen_dashdash) {
1073 + if (seen_dashdash + 1 != argc - 1)
1074 + usage(pickaxe_usage);
1075 + path = argv[seen_dashdash + 1];
1076 + for (j = i; j < seen_dashdash; j++)
1077 + argv[unk++] = argv[j];
1078 + }
1079 + else {
1080 + /* (3) */
1081 + path = argv[i];
1082 + if (i + 1 == argc - 1) {
1083 + final_commit_name = argv[i + 1];
1084 +
1085 + /* if (unk == 1) we could be getting
1086 + * old-style
1087 + */
1088 + if (unk == 1 && !has_path_in_work_tree(path)) {
1089 + path = argv[i + 1];
1090 + final_commit_name = argv[i];
1091 + }
1092 + }
1093 + else if (i != argc - 1)
1094 + usage(pickaxe_usage); /* garbage at end */
1095 +
1096 + if (!has_path_in_work_tree(path))
1097 + die("cannot stat path %s: %s",
1098 + path, strerror(errno));
1099 + }
1100 + }
1101 +
1102 + if (final_commit_name)
1103 + argv[unk++] = final_commit_name;
1104 +
1105 + /* Now we got rev and path. We do not want the path pruning
1106 + * but we may want "bottom" processing.
1107 + */
1108 + argv[unk] = NULL;
1109 +
1110 + init_revisions(&revs, NULL);
1111 + setup_revisions(unk, argv, &revs, "HEAD");
1112 + memset(&sb, 0, sizeof(sb));
1113 +
1114 + /* There must be one and only one positive commit in the
1115 + * revs->pending array.
1116 + */
1117 + for (i = 0; i < revs.pending.nr; i++) {
1118 + struct object *obj = revs.pending.objects[i].item;
1119 + if (obj->flags & UNINTERESTING)
1120 + continue;
1121 + while (obj->type == OBJ_TAG)
1122 + obj = deref_tag(obj, NULL, 0);
1123 + if (obj->type != OBJ_COMMIT)
1124 + die("Non commit %s?",
1125 + revs.pending.objects[i].name);
1126 + if (sb.final)
1127 + die("More than one commit to dig from %s and %s?",
1128 + revs.pending.objects[i].name,
1129 + final_commit_name);
1130 + sb.final = (struct commit *) obj;
1131 + final_commit_name = revs.pending.objects[i].name;
1132 + }
1133 +
1134 + if (!sb.final) {
1135 + /* "--not A B -- path" without anything positive */
1136 + unsigned char head_sha1[20];
1137 +
1138 + final_commit_name = "HEAD";
1139 + if (get_sha1(final_commit_name, head_sha1))
1140 + die("No such ref: HEAD");
1141 + sb.final = lookup_commit_reference(head_sha1);
1142 + add_pending_object(&revs, &(sb.final->object), "HEAD");
1143 + }
1144 +
1145 + /* If we have bottom, this will mark the ancestors of the
1146 + * bottom commits we would reach while traversing as
1147 + * uninteresting.
1148 + */
1149 + prepare_revision_walk(&revs);
1150 +
1151 + o = find_origin(&sb, sb.final, path);
1152 + if (!o)
1153 + die("no such path %s in %s", path, final_commit_name);
1154 +
1155 + sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);
1156 + lno = prepare_lines(&sb);
1157 +
1158 + if (bottom < 1)
1159 + bottom = 1;
1160 + if (top < 1)
1161 + top = lno;
1162 + bottom--;
1163 + if (lno < top)
1164 + die("file %s has only %lu lines", path, lno);
1165 +
1166 + ent = xcalloc(1, sizeof(*ent));
1167 + ent->lno = bottom;
1168 + ent->num_lines = top - bottom;
1169 + ent->suspect = o;
1170 + ent->s_lno = bottom;
1171 +
1172 + sb.ent = ent;
1173 + sb.path = path;
1174 +
1175 + if (revs_file && read_ancestry(revs_file))
1176 + die("reading graft file %s failed: %s",
1177 + revs_file, strerror(errno));
1178 +
1179 + assign_blame(&sb, &revs);
1180 +
1181 + coalesce(&sb);
1182 +
1183 + if (!(output_option & OUTPUT_PORCELAIN))
1184 + find_alignment(&sb, &output_option);
1185 +
1186 + output(&sb, output_option);
1187 + free((void *)sb.final_buf);
1188 + for (ent = sb.ent; ent; ) {
1189 + struct blame_entry *e = ent->next;
1190 + free(ent);
1191 + ent = e;
1192 + }
1193 + return 0;
1194 +}
builtin.h
+1
@@ -39,6 +39,7 @@ extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
39 extern int cmd_mv(int argc, const char **argv, const char *prefix);
40 extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
41 extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
42 +extern int cmd_pickaxe(int argc, const char **argv, const char *prefix);
43 extern int cmd_prune(int argc, const char **argv, const char *prefix);
44 extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
45 extern int cmd_push(int argc, const char **argv, const char *prefix);
git.c
+1
@@ -245,6 +245,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
245 { "mv", cmd_mv, RUN_SETUP },
246 { "name-rev", cmd_name_rev, RUN_SETUP },
247 { "pack-objects", cmd_pack_objects, RUN_SETUP },
248 + { "pickaxe", cmd_pickaxe, RUN_SETUP },
249 { "prune", cmd_prune, RUN_SETUP },
250 { "prune-packed", cmd_prune_packed, RUN_SETUP },
251 { "push", cmd_push, RUN_SETUP },
t/annotate-tests.sh
+1
@@ -4,6 +4,7 @@
4 check_count () {
5 head=
6 case "$1" in -h) head="$2"; shift; shift ;; esac
7 + echo "$PROG file $head" >&4
8 $PROG file $head >.result || return 1
9 cat .result | perl -e '
10 my %expect = (@ARGV);
t/t8003-pickaxe.sh new
+9
@@ -0,0 +1,9 @@
1 +#!/bin/sh
2 +
3 +test_description='git-pickaxe'
4 +. ./test-lib.sh
5 +
6 +PROG='git pickaxe -c'
7 +. ../annotate-tests.sh
8 +
9 +test_done