4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
18 #include "cache-tree.h"
20 static char blame_usage[] =
21 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
22 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
23 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
24 " -l, --long Show long commit SHA1 (Default: off)\n"
25 " --root Do not treat root commits as boundaries (Default: off)\n"
26 " -t, --time Show raw timestamp (Default: off)\n"
27 " -f, --show-name Show original filename (Default: auto)\n"
28 " -n, --show-number Show original linenumber (Default: off)\n"
29 " -p, --porcelain Show in a format designed for machine consumption\n"
30 " -L n,m Process only line range n,m, counting from 1\n"
31 " -M, -C Find line movements within and across files\n"
32 " --incremental Show blame entries as we find them, incrementally\n"
33 " --contents file Use <file>'s contents as the final image\n"
34 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
36 static int longest_file;
37 static int longest_author;
38 static int max_orig_digits;
39 static int max_digits;
40 static int max_score_digits;
42 static int blank_boundary;
43 static int incremental;
50 static int num_read_blob;
51 static int num_get_patch;
52 static int num_commits;
54 #define PICKAXE_BLAME_MOVE 01
55 #define PICKAXE_BLAME_COPY 02
56 #define PICKAXE_BLAME_COPY_HARDER 04
59 * blame for a blame_entry with score lower than these thresholds
60 * is not passed to the parent using move/copy logic.
62 static unsigned blame_move_score;
63 static unsigned blame_copy_score;
64 #define BLAME_DEFAULT_MOVE_SCORE 20
65 #define BLAME_DEFAULT_COPY_SCORE 40
67 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
68 #define METAINFO_SHOWN (1u<<12)
69 #define MORE_THAN_ONE_PATH (1u<<13)
72 * One blob in a commit that is being suspected
76 struct commit *commit;
78 unsigned char blob_sha1[20];
79 char path[FLEX_ARRAY];
83 * Given an origin, prepare mmfile_t structure to be used by the
86 static char *fill_origin_blob(struct origin *o, mmfile_t *file)
91 file->ptr = read_sha1_file(o->blob_sha1, type,
92 (unsigned long *)(&(file->size)));
101 * Origin is refcounted and usually we keep the blob contents to be
104 static inline struct origin *origin_incref(struct origin *o)
111 static void origin_decref(struct origin *o)
113 if (o && --o->refcnt <= 0) {
116 memset(o, 0, sizeof(*o));
122 * Each group of lines is described by a blame_entry; it can be split
123 * as we pass blame to the parents. They form a linked list in the
124 * scoreboard structure, sorted by the target line number.
127 struct blame_entry *prev;
128 struct blame_entry *next;
130 /* the first line of this group in the final image;
131 * internally all line numbers are 0 based.
135 /* how many lines this group has */
138 /* the commit that introduced this group into the final image */
139 struct origin *suspect;
141 /* true if the suspect is truly guilty; false while we have not
142 * checked if the group came from one of its parents.
146 /* the line number of the first line of this group in the
147 * suspect's file; internally all line numbers are 0 based.
151 /* how significant this entry is -- cached to avoid
152 * scanning the lines over and over.
158 * The current state of the blame assignment.
161 /* the final commit (i.e. where we started digging from) */
162 struct commit *final;
167 * The contents in the final image.
168 * Used by many functions to obtain contents of the nth line,
169 * indexed with scoreboard.lineno[blame_entry.lno].
171 const char *final_buf;
172 unsigned long final_buf_size;
174 /* linked list of blames */
175 struct blame_entry *ent;
177 /* look-up a line in the final buffer */
182 static int cmp_suspect(struct origin *a, struct origin *b)
184 int cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);
187 return strcmp(a->path, b->path);
190 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
192 static void sanity_check_refcnt(struct scoreboard *);
195 * If two blame entries that are next to each other came from
196 * contiguous lines in the same origin (i.e. <commit, path> pair),
197 * merge them together.
199 static void coalesce(struct scoreboard *sb)
201 struct blame_entry *ent, *next;
203 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
204 if (!cmp_suspect(ent->suspect, next->suspect) &&
205 ent->guilty == next->guilty &&
206 ent->s_lno + ent->num_lines == next->s_lno) {
207 ent->num_lines += next->num_lines;
208 ent->next = next->next;
210 ent->next->prev = ent;
211 origin_decref(next->suspect);
214 next = ent; /* again */
218 if (DEBUG) /* sanity */
219 sanity_check_refcnt(sb);
223 * Given a commit and a path in it, create a new origin structure.
224 * The callers that add blame to the scoreboard should use
225 * get_origin() to obtain shared, refcounted copy instead of calling
226 * this function directly.
228 static struct origin *make_origin(struct commit *commit, const char *path)
231 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
234 strcpy(o->path, path);
239 * Locate an existing origin or create a new one.
241 static struct origin *get_origin(struct scoreboard *sb,
242 struct commit *commit,
245 struct blame_entry *e;
247 for (e = sb->ent; e; e = e->next) {
248 if (e->suspect->commit == commit &&
249 !strcmp(e->suspect->path, path))
250 return origin_incref(e->suspect);
252 return make_origin(commit, path);
256 * Fill the blob_sha1 field of an origin if it hasn't, so that later
257 * call to fill_origin_blob() can use it to locate the data. blob_sha1
258 * for an origin is also used to pass the blame for the entire file to
259 * the parent to detect the case where a child's blob is identical to
260 * that of its parent's.
262 static int fill_blob_sha1(struct origin *origin)
267 if (!is_null_sha1(origin->blob_sha1))
269 if (get_tree_entry(origin->commit->object.sha1,
271 origin->blob_sha1, &mode))
273 if (sha1_object_info(origin->blob_sha1, type, NULL) ||
274 strcmp(type, blob_type))
278 hashclr(origin->blob_sha1);
283 * We have an origin -- check if the same path exists in the
284 * parent and return an origin structure to represent it.
286 static struct origin *find_origin(struct scoreboard *sb,
287 struct commit *parent,
288 struct origin *origin)
290 struct origin *porigin = NULL;
291 struct diff_options diff_opts;
292 const char *paths[2];
296 * Each commit object can cache one origin in that
297 * commit. This is a freestanding copy of origin and
300 struct origin *cached = parent->util;
301 if (!strcmp(cached->path, origin->path)) {
303 * The same path between origin and its parent
304 * without renaming -- the most common case.
306 porigin = get_origin(sb, parent, cached->path);
309 * If the origin was newly created (i.e. get_origin
310 * would call make_origin if none is found in the
311 * scoreboard), it does not know the blob_sha1,
312 * so copy it. Otherwise porigin was in the
313 * scoreboard and already knows blob_sha1.
315 if (porigin->refcnt == 1)
316 hashcpy(porigin->blob_sha1, cached->blob_sha1);
319 /* otherwise it was not very useful; free it */
324 /* See if the origin->path is different between parent
325 * and origin first. Most of the time they are the
326 * same and diff-tree is fairly efficient about this.
328 diff_setup(&diff_opts);
329 diff_opts.recursive = 1;
330 diff_opts.detect_rename = 0;
331 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
332 paths[0] = origin->path;
335 diff_tree_setup_paths(paths, &diff_opts);
336 if (diff_setup_done(&diff_opts) < 0)
339 if (is_null_sha1(origin->commit->object.sha1))
340 do_diff_cache(parent->tree->object.sha1, &diff_opts);
342 diff_tree_sha1(parent->tree->object.sha1,
343 origin->commit->tree->object.sha1,
345 diffcore_std(&diff_opts);
347 /* It is either one entry that says "modified", or "created",
350 if (!diff_queued_diff.nr) {
351 /* The path is the same as parent */
352 porigin = get_origin(sb, parent, origin->path);
353 hashcpy(porigin->blob_sha1, origin->blob_sha1);
355 else if (diff_queued_diff.nr != 1)
356 die("internal error in blame::find_origin");
358 struct diff_filepair *p = diff_queued_diff.queue[0];
361 die("internal error in blame::find_origin (%c)",
364 porigin = get_origin(sb, parent, origin->path);
365 hashcpy(porigin->blob_sha1, p->one->sha1);
369 /* Did not exist in parent, or type changed */
373 diff_flush(&diff_opts);
376 * Create a freestanding copy that is not part of
377 * the refcounted origin found in the scoreboard, and
378 * cache it in the commit.
380 struct origin *cached;
382 cached = make_origin(porigin->commit, porigin->path);
383 hashcpy(cached->blob_sha1, porigin->blob_sha1);
384 parent->util = cached;
390 * We have an origin -- find the path that corresponds to it in its
391 * parent and return an origin structure to represent it.
393 static struct origin *find_rename(struct scoreboard *sb,
394 struct commit *parent,
395 struct origin *origin)
397 struct origin *porigin = NULL;
398 struct diff_options diff_opts;
400 const char *paths[2];
402 diff_setup(&diff_opts);
403 diff_opts.recursive = 1;
404 diff_opts.detect_rename = DIFF_DETECT_RENAME;
405 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
406 diff_opts.single_follow = origin->path;
408 diff_tree_setup_paths(paths, &diff_opts);
409 if (diff_setup_done(&diff_opts) < 0)
412 if (is_null_sha1(origin->commit->object.sha1))
413 do_diff_cache(parent->tree->object.sha1, &diff_opts);
415 diff_tree_sha1(parent->tree->object.sha1,
416 origin->commit->tree->object.sha1,
418 diffcore_std(&diff_opts);
420 for (i = 0; i < diff_queued_diff.nr; i++) {
421 struct diff_filepair *p = diff_queued_diff.queue[i];
422 if ((p->status == 'R' || p->status == 'C') &&
423 !strcmp(p->two->path, origin->path)) {
424 porigin = get_origin(sb, parent, p->one->path);
425 hashcpy(porigin->blob_sha1, p->one->sha1);
429 diff_flush(&diff_opts);
434 * Parsing of patch chunks...
437 /* line number in postimage; up to but not including this
438 * line is the same as preimage
442 /* preimage line number after this chunk */
445 /* postimage line number after this chunk */
450 struct chunk *chunks;
454 struct blame_diff_state {
455 struct xdiff_emit_state xm;
457 unsigned hunk_post_context;
458 unsigned hunk_in_pre_context : 1;
461 static void process_u_diff(void *state_, char *line, unsigned long len)
463 struct blame_diff_state *state = state_;
465 int off1, off2, len1, len2, num;
467 num = state->ret->num;
468 if (len < 4 || line[0] != '@' || line[1] != '@') {
469 if (state->hunk_in_pre_context && line[0] == ' ')
470 state->ret->chunks[num - 1].same++;
472 state->hunk_in_pre_context = 0;
474 state->hunk_post_context++;
476 state->hunk_post_context = 0;
481 if (num && state->hunk_post_context) {
482 chunk = &state->ret->chunks[num - 1];
483 chunk->p_next -= state->hunk_post_context;
484 chunk->t_next -= state->hunk_post_context;
486 state->ret->num = ++num;
487 state->ret->chunks = xrealloc(state->ret->chunks,
488 sizeof(struct chunk) * num);
489 chunk = &state->ret->chunks[num - 1];
490 if (parse_hunk_header(line, len, &off1, &len1, &off2, &len2)) {
495 /* Line numbers in patch output are one based. */
499 chunk->same = len2 ? off2 : (off2 + 1);
501 chunk->p_next = off1 + (len1 ? len1 : 1);
502 chunk->t_next = chunk->same + len2;
503 state->hunk_in_pre_context = 1;
504 state->hunk_post_context = 0;
507 static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
510 struct blame_diff_state state;
515 xpp.flags = XDF_NEED_MINIMAL;
516 xecfg.ctxlen = context;
518 ecb.outf = xdiff_outf;
520 memset(&state, 0, sizeof(state));
521 state.xm.consume = process_u_diff;
522 state.ret = xmalloc(sizeof(struct patch));
523 state.ret->chunks = NULL;
526 xdl_diff(file_p, file_o, &xpp, &xecfg, &ecb);
528 if (state.ret->num) {
530 chunk = &state.ret->chunks[state.ret->num - 1];
531 chunk->p_next -= state.hunk_post_context;
532 chunk->t_next -= state.hunk_post_context;
538 * Run diff between two origins and grab the patch output, so that
539 * we can pass blame for lines origin is currently suspected for
542 static struct patch *get_patch(struct origin *parent, struct origin *origin)
544 mmfile_t file_p, file_o;
547 fill_origin_blob(parent, &file_p);
548 fill_origin_blob(origin, &file_o);
549 if (!file_p.ptr || !file_o.ptr)
551 patch = compare_buffer(&file_p, &file_o, 0);
556 static void free_patch(struct patch *p)
563 * Link in a new blame entry to the scoreboard. Entries that cover the
564 * same line range have been removed from the scoreboard previously.
566 static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
568 struct blame_entry *ent, *prev = NULL;
570 origin_incref(e->suspect);
572 for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
575 /* prev, if not NULL, is the last one that is below e */
578 e->next = prev->next;
590 * src typically is on-stack; we want to copy the information in it to
591 * an malloced blame_entry that is already on the linked list of the
592 * scoreboard. The origin of dst loses a refcnt while the origin of src
595 static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
597 struct blame_entry *p, *n;
601 origin_incref(src->suspect);
602 origin_decref(dst->suspect);
603 memcpy(dst, src, sizeof(*src));
609 static const char *nth_line(struct scoreboard *sb, int lno)
611 return sb->final_buf + sb->lineno[lno];
615 * It is known that lines between tlno to same came from parent, and e
616 * has an overlap with that range. it also is known that parent's
617 * line plno corresponds to e's line tlno.
623 * <------------------>
625 * Split e into potentially three parts; before this chunk, the chunk
626 * to be blamed for the parent, and after that portion.
628 static void split_overlap(struct blame_entry *split,
629 struct blame_entry *e,
630 int tlno, int plno, int same,
631 struct origin *parent)
634 memset(split, 0, sizeof(struct blame_entry [3]));
636 if (e->s_lno < tlno) {
637 /* there is a pre-chunk part not blamed on parent */
638 split[0].suspect = origin_incref(e->suspect);
639 split[0].lno = e->lno;
640 split[0].s_lno = e->s_lno;
641 split[0].num_lines = tlno - e->s_lno;
642 split[1].lno = e->lno + tlno - e->s_lno;
643 split[1].s_lno = plno;
646 split[1].lno = e->lno;
647 split[1].s_lno = plno + (e->s_lno - tlno);
650 if (same < e->s_lno + e->num_lines) {
651 /* there is a post-chunk part not blamed on parent */
652 split[2].suspect = origin_incref(e->suspect);
653 split[2].lno = e->lno + (same - e->s_lno);
654 split[2].s_lno = e->s_lno + (same - e->s_lno);
655 split[2].num_lines = e->s_lno + e->num_lines - same;
656 chunk_end_lno = split[2].lno;
659 chunk_end_lno = e->lno + e->num_lines;
660 split[1].num_lines = chunk_end_lno - split[1].lno;
663 * if it turns out there is nothing to blame the parent for,
664 * forget about the splitting. !split[1].suspect signals this.
666 if (split[1].num_lines < 1)
668 split[1].suspect = origin_incref(parent);
672 * split_overlap() divided an existing blame e into up to three parts
673 * in split. Adjust the linked list of blames in the scoreboard to
676 static void split_blame(struct scoreboard *sb,
677 struct blame_entry *split,
678 struct blame_entry *e)
680 struct blame_entry *new_entry;
682 if (split[0].suspect && split[2].suspect) {
683 /* The first part (reuse storage for the existing entry e) */
684 dup_entry(e, &split[0]);
686 /* The last part -- me */
687 new_entry = xmalloc(sizeof(*new_entry));
688 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
689 add_blame_entry(sb, new_entry);
691 /* ... and the middle part -- parent */
692 new_entry = xmalloc(sizeof(*new_entry));
693 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
694 add_blame_entry(sb, new_entry);
696 else if (!split[0].suspect && !split[2].suspect)
698 * The parent covers the entire area; reuse storage for
699 * e and replace it with the parent.
701 dup_entry(e, &split[1]);
702 else if (split[0].suspect) {
703 /* me and then parent */
704 dup_entry(e, &split[0]);
706 new_entry = xmalloc(sizeof(*new_entry));
707 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
708 add_blame_entry(sb, new_entry);
711 /* parent and then me */
712 dup_entry(e, &split[1]);
714 new_entry = xmalloc(sizeof(*new_entry));
715 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
716 add_blame_entry(sb, new_entry);
719 if (DEBUG) { /* sanity */
720 struct blame_entry *ent;
721 int lno = sb->ent->lno, corrupt = 0;
723 for (ent = sb->ent; ent; ent = ent->next) {
728 lno += ent->num_lines;
732 for (ent = sb->ent; ent; ent = ent->next) {
733 printf("L %8d l %8d n %8d\n",
734 lno, ent->lno, ent->num_lines);
735 lno = ent->lno + ent->num_lines;
743 * After splitting the blame, the origins used by the
744 * on-stack blame_entry should lose one refcnt each.
746 static void decref_split(struct blame_entry *split)
750 for (i = 0; i < 3; i++)
751 origin_decref(split[i].suspect);
755 * Helper for blame_chunk(). blame_entry e is known to overlap with
756 * the patch hunk; split it and pass blame to the parent.
758 static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
759 int tlno, int plno, int same,
760 struct origin *parent)
762 struct blame_entry split[3];
764 split_overlap(split, e, tlno, plno, same, parent);
765 if (split[1].suspect)
766 split_blame(sb, split, e);
771 * Find the line number of the last line the target is suspected for.
773 static int find_last_in_target(struct scoreboard *sb, struct origin *target)
775 struct blame_entry *e;
776 int last_in_target = -1;
778 for (e = sb->ent; e; e = e->next) {
779 if (e->guilty || cmp_suspect(e->suspect, target))
781 if (last_in_target < e->s_lno + e->num_lines)
782 last_in_target = e->s_lno + e->num_lines;
784 return last_in_target;
788 * Process one hunk from the patch between the current suspect for
789 * blame_entry e and its parent. Find and split the overlap, and
790 * pass blame to the overlapping part to the parent.
792 static void blame_chunk(struct scoreboard *sb,
793 int tlno, int plno, int same,
794 struct origin *target, struct origin *parent)
796 struct blame_entry *e;
798 for (e = sb->ent; e; e = e->next) {
799 if (e->guilty || cmp_suspect(e->suspect, target))
801 if (same <= e->s_lno)
803 if (tlno < e->s_lno + e->num_lines)
804 blame_overlap(sb, e, tlno, plno, same, parent);
809 * We are looking at the origin 'target' and aiming to pass blame
810 * for the lines it is suspected to its parent. Run diff to find
811 * which lines came from parent and pass blame for them.
813 static int pass_blame_to_parent(struct scoreboard *sb,
814 struct origin *target,
815 struct origin *parent)
817 int i, last_in_target, plno, tlno;
820 last_in_target = find_last_in_target(sb, target);
821 if (last_in_target < 0)
822 return 1; /* nothing remains for this target */
824 patch = get_patch(parent, target);
826 for (i = 0; i < patch->num; i++) {
827 struct chunk *chunk = &patch->chunks[i];
829 blame_chunk(sb, tlno, plno, chunk->same, target, parent);
830 plno = chunk->p_next;
831 tlno = chunk->t_next;
833 /* The rest (i.e. anything after tlno) are the same as the parent */
834 blame_chunk(sb, tlno, plno, last_in_target, target, parent);
841 * The lines in blame_entry after splitting blames many times can become
842 * very small and trivial, and at some point it becomes pointless to
843 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
844 * ordinary C program, and it is not worth to say it was copied from
845 * totally unrelated file in the parent.
847 * Compute how trivial the lines in the blame_entry are.
849 static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
858 cp = nth_line(sb, e->lno);
859 ep = nth_line(sb, e->lno + e->num_lines);
861 unsigned ch = *((unsigned char *)cp);
871 * best_so_far[] and this[] are both a split of an existing blame_entry
872 * that passes blame to the parent. Maintain best_so_far the best split
873 * so far, by comparing this and best_so_far and copying this into
874 * bst_so_far as needed.
876 static void copy_split_if_better(struct scoreboard *sb,
877 struct blame_entry *best_so_far,
878 struct blame_entry *this)
882 if (!this[1].suspect)
884 if (best_so_far[1].suspect) {
885 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
889 for (i = 0; i < 3; i++)
890 origin_incref(this[i].suspect);
891 decref_split(best_so_far);
892 memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
896 * Find the lines from parent that are the same as ent so that
897 * we can pass blames to it. file_p has the blob contents for
900 static void find_copy_in_blob(struct scoreboard *sb,
901 struct blame_entry *ent,
902 struct origin *parent,
903 struct blame_entry *split,
913 * Prepare mmfile that contains only the lines in ent.
915 cp = nth_line(sb, ent->lno);
916 file_o.ptr = (char*) cp;
917 cnt = ent->num_lines;
919 while (cnt && cp < sb->final_buf + sb->final_buf_size) {
923 file_o.size = cp - file_o.ptr;
925 patch = compare_buffer(file_p, &file_o, 1);
927 memset(split, 0, sizeof(struct blame_entry [3]));
929 for (i = 0; i < patch->num; i++) {
930 struct chunk *chunk = &patch->chunks[i];
932 /* tlno to chunk->same are the same as ent */
933 if (ent->num_lines <= tlno)
935 if (tlno < chunk->same) {
936 struct blame_entry this[3];
937 split_overlap(this, ent,
938 tlno + ent->s_lno, plno,
939 chunk->same + ent->s_lno,
941 copy_split_if_better(sb, split, this);
944 plno = chunk->p_next;
945 tlno = chunk->t_next;
951 * See if lines currently target is suspected for can be attributed to
954 static int find_move_in_parent(struct scoreboard *sb,
955 struct origin *target,
956 struct origin *parent)
958 int last_in_target, made_progress;
959 struct blame_entry *e, split[3];
962 last_in_target = find_last_in_target(sb, target);
963 if (last_in_target < 0)
964 return 1; /* nothing remains for this target */
966 fill_origin_blob(parent, &file_p);
971 while (made_progress) {
973 for (e = sb->ent; e; e = e->next) {
974 if (e->guilty || cmp_suspect(e->suspect, target))
976 find_copy_in_blob(sb, e, parent, split, &file_p);
977 if (split[1].suspect &&
978 blame_move_score < ent_score(sb, &split[1])) {
979 split_blame(sb, split, e);
989 struct blame_entry *ent;
990 struct blame_entry split[3];
994 * Count the number of entries the target is suspected for,
995 * and prepare a list of entry and the best split.
997 static struct blame_list *setup_blame_list(struct scoreboard *sb,
998 struct origin *target,
1001 struct blame_entry *e;
1003 struct blame_list *blame_list = NULL;
1005 for (e = sb->ent, num_ents = 0; e; e = e->next)
1006 if (!e->guilty && !cmp_suspect(e->suspect, target))
1009 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1010 for (e = sb->ent, i = 0; e; e = e->next)
1011 if (!e->guilty && !cmp_suspect(e->suspect, target))
1012 blame_list[i++].ent = e;
1014 *num_ents_p = num_ents;
1019 * For lines target is suspected for, see if we can find code movement
1020 * across file boundary from the parent commit. porigin is the path
1021 * in the parent we already tried.
1023 static int find_copy_in_parent(struct scoreboard *sb,
1024 struct origin *target,
1025 struct commit *parent,
1026 struct origin *porigin,
1029 struct diff_options diff_opts;
1030 const char *paths[1];
1033 struct blame_list *blame_list;
1036 blame_list = setup_blame_list(sb, target, &num_ents);
1038 return 1; /* nothing remains for this target */
1040 diff_setup(&diff_opts);
1041 diff_opts.recursive = 1;
1042 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1045 diff_tree_setup_paths(paths, &diff_opts);
1046 if (diff_setup_done(&diff_opts) < 0)
1049 /* Try "find copies harder" on new path if requested;
1050 * we do not want to use diffcore_rename() actually to
1051 * match things up; find_copies_harder is set only to
1052 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1053 * and this code needs to be after diff_setup_done(), which
1054 * usually makes find-copies-harder imply copy detection.
1056 if ((opt & PICKAXE_BLAME_COPY_HARDER) &&
1057 (!porigin || strcmp(target->path, porigin->path)))
1058 diff_opts.find_copies_harder = 1;
1060 if (is_null_sha1(target->commit->object.sha1))
1061 do_diff_cache(parent->tree->object.sha1, &diff_opts);
1063 diff_tree_sha1(parent->tree->object.sha1,
1064 target->commit->tree->object.sha1,
1067 if (!diff_opts.find_copies_harder)
1068 diffcore_std(&diff_opts);
1072 int made_progress = 0;
1074 for (i = 0; i < diff_queued_diff.nr; i++) {
1075 struct diff_filepair *p = diff_queued_diff.queue[i];
1076 struct origin *norigin;
1078 struct blame_entry this[3];
1080 if (!DIFF_FILE_VALID(p->one))
1081 continue; /* does not exist in parent */
1082 if (porigin && !strcmp(p->one->path, porigin->path))
1083 /* find_move already dealt with this path */
1086 norigin = get_origin(sb, parent, p->one->path);
1087 hashcpy(norigin->blob_sha1, p->one->sha1);
1088 fill_origin_blob(norigin, &file_p);
1092 for (j = 0; j < num_ents; j++) {
1093 find_copy_in_blob(sb, blame_list[j].ent,
1094 norigin, this, &file_p);
1095 copy_split_if_better(sb, blame_list[j].split,
1099 origin_decref(norigin);
1102 for (j = 0; j < num_ents; j++) {
1103 struct blame_entry *split = blame_list[j].split;
1104 if (split[1].suspect &&
1105 blame_copy_score < ent_score(sb, &split[1])) {
1106 split_blame(sb, split, blame_list[j].ent);
1109 decref_split(split);
1115 blame_list = setup_blame_list(sb, target, &num_ents);
1121 diff_flush(&diff_opts);
1127 * The blobs of origin and porigin exactly match, so everything
1128 * origin is suspected for can be blamed on the parent.
1130 static void pass_whole_blame(struct scoreboard *sb,
1131 struct origin *origin, struct origin *porigin)
1133 struct blame_entry *e;
1135 if (!porigin->file.ptr && origin->file.ptr) {
1136 /* Steal its file */
1137 porigin->file = origin->file;
1138 origin->file.ptr = NULL;
1140 for (e = sb->ent; e; e = e->next) {
1141 if (cmp_suspect(e->suspect, origin))
1143 origin_incref(porigin);
1144 origin_decref(e->suspect);
1145 e->suspect = porigin;
1149 #define MAXPARENT 16
1151 static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1154 struct commit *commit = origin->commit;
1155 struct commit_list *parent;
1156 struct origin *parent_origin[MAXPARENT], *porigin;
1158 memset(parent_origin, 0, sizeof(parent_origin));
1160 /* The first pass looks for unrenamed path to optimize for
1161 * common cases, then we look for renames in the second pass.
1163 for (pass = 0; pass < 2; pass++) {
1164 struct origin *(*find)(struct scoreboard *,
1165 struct commit *, struct origin *);
1166 find = pass ? find_rename : find_origin;
1168 for (i = 0, parent = commit->parents;
1169 i < MAXPARENT && parent;
1170 parent = parent->next, i++) {
1171 struct commit *p = parent->item;
1174 if (parent_origin[i])
1176 if (parse_commit(p))
1178 porigin = find(sb, p, origin);
1181 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
1182 pass_whole_blame(sb, origin, porigin);
1183 origin_decref(porigin);
1186 for (j = same = 0; j < i; j++)
1187 if (parent_origin[j] &&
1188 !hashcmp(parent_origin[j]->blob_sha1,
1189 porigin->blob_sha1)) {
1194 parent_origin[i] = porigin;
1196 origin_decref(porigin);
1201 for (i = 0, parent = commit->parents;
1202 i < MAXPARENT && parent;
1203 parent = parent->next, i++) {
1204 struct origin *porigin = parent_origin[i];
1207 if (pass_blame_to_parent(sb, origin, porigin))
1212 * Optionally find moves in parents' files.
1214 if (opt & PICKAXE_BLAME_MOVE)
1215 for (i = 0, parent = commit->parents;
1216 i < MAXPARENT && parent;
1217 parent = parent->next, i++) {
1218 struct origin *porigin = parent_origin[i];
1221 if (find_move_in_parent(sb, origin, porigin))
1226 * Optionally find copies from parents' files.
1228 if (opt & PICKAXE_BLAME_COPY)
1229 for (i = 0, parent = commit->parents;
1230 i < MAXPARENT && parent;
1231 parent = parent->next, i++) {
1232 struct origin *porigin = parent_origin[i];
1233 if (find_copy_in_parent(sb, origin, parent->item,
1239 for (i = 0; i < MAXPARENT; i++)
1240 origin_decref(parent_origin[i]);
1244 * Information on commits, used for output.
1250 unsigned long author_time;
1253 /* filled only when asked for details */
1255 char *committer_mail;
1256 unsigned long committer_time;
1263 * Parse author/committer line in the commit object buffer
1265 static void get_ac_line(const char *inbuf, const char *what,
1266 int bufsz, char *person, char **mail,
1267 unsigned long *time, char **tz)
1272 tmp = strstr(inbuf, what);
1275 tmp += strlen(what);
1276 endp = strchr(tmp, '\n');
1284 person = *mail = *tz = "(unknown)";
1288 memcpy(person, tmp, len);
1300 *time = strtoul(tmp, NULL, 10);
1309 static void get_commit_info(struct commit *commit,
1310 struct commit_info *ret,
1315 static char author_buf[1024];
1316 static char committer_buf[1024];
1317 static char summary_buf[1024];
1320 * We've operated without save_commit_buffer, so
1321 * we now need to populate them for output.
1323 if (!commit->buffer) {
1327 read_sha1_file(commit->object.sha1, type, &size);
1329 ret->author = author_buf;
1330 get_ac_line(commit->buffer, "\nauthor ",
1331 sizeof(author_buf), author_buf, &ret->author_mail,
1332 &ret->author_time, &ret->author_tz);
1337 ret->committer = committer_buf;
1338 get_ac_line(commit->buffer, "\ncommitter ",
1339 sizeof(committer_buf), committer_buf, &ret->committer_mail,
1340 &ret->committer_time, &ret->committer_tz);
1342 ret->summary = summary_buf;
1343 tmp = strstr(commit->buffer, "\n\n");
1346 sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1350 endp = strchr(tmp, '\n');
1352 endp = tmp + strlen(tmp);
1354 if (len >= sizeof(summary_buf) || len == 0)
1356 memcpy(summary_buf, tmp, len);
1357 summary_buf[len] = 0;
1361 * To allow LF and other nonportable characters in pathnames,
1362 * they are c-style quoted as needed.
1364 static void write_filename_info(const char *path)
1366 printf("filename ");
1367 write_name_quoted(NULL, 0, path, 1, stdout);
1372 * The blame_entry is found to be guilty for the range. Mark it
1373 * as such, and show it in incremental output.
1375 static void found_guilty_entry(struct blame_entry *ent)
1381 struct origin *suspect = ent->suspect;
1383 printf("%s %d %d %d\n",
1384 sha1_to_hex(suspect->commit->object.sha1),
1385 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1386 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1387 struct commit_info ci;
1388 suspect->commit->object.flags |= METAINFO_SHOWN;
1389 get_commit_info(suspect->commit, &ci, 1);
1390 printf("author %s\n", ci.author);
1391 printf("author-mail %s\n", ci.author_mail);
1392 printf("author-time %lu\n", ci.author_time);
1393 printf("author-tz %s\n", ci.author_tz);
1394 printf("committer %s\n", ci.committer);
1395 printf("committer-mail %s\n", ci.committer_mail);
1396 printf("committer-time %lu\n", ci.committer_time);
1397 printf("committer-tz %s\n", ci.committer_tz);
1398 printf("summary %s\n", ci.summary);
1399 if (suspect->commit->object.flags & UNINTERESTING)
1400 printf("boundary\n");
1402 write_filename_info(suspect->path);
1407 * The main loop -- while the scoreboard has lines whose true origin
1408 * is still unknown, pick one blame_entry, and allow its current
1409 * suspect to pass blames to its parents.
1411 static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1414 struct blame_entry *ent;
1415 struct commit *commit;
1416 struct origin *suspect = NULL;
1418 /* find one suspect to break down */
1419 for (ent = sb->ent; !suspect && ent; ent = ent->next)
1421 suspect = ent->suspect;
1423 return; /* all done */
1426 * We will use this suspect later in the loop,
1427 * so hold onto it in the meantime.
1429 origin_incref(suspect);
1430 commit = suspect->commit;
1431 if (!commit->object.parsed)
1432 parse_commit(commit);
1433 if (!(commit->object.flags & UNINTERESTING) &&
1434 !(revs->max_age != -1 && commit->date < revs->max_age))
1435 pass_blame(sb, suspect, opt);
1437 commit->object.flags |= UNINTERESTING;
1438 if (commit->object.parsed)
1439 mark_parents_uninteresting(commit);
1441 /* treat root commit as boundary */
1442 if (!commit->parents && !show_root)
1443 commit->object.flags |= UNINTERESTING;
1445 /* Take responsibility for the remaining entries */
1446 for (ent = sb->ent; ent; ent = ent->next)
1447 if (!cmp_suspect(ent->suspect, suspect))
1448 found_guilty_entry(ent);
1449 origin_decref(suspect);
1451 if (DEBUG) /* sanity */
1452 sanity_check_refcnt(sb);
1456 static const char *format_time(unsigned long time, const char *tz_str,
1459 static char time_buf[128];
1464 if (show_raw_time) {
1465 sprintf(time_buf, "%lu %s", time, tz_str);
1470 minutes = tz < 0 ? -tz : tz;
1471 minutes = (minutes / 100)*60 + (minutes % 100);
1472 minutes = tz < 0 ? -minutes : minutes;
1473 t = time + minutes * 60;
1476 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1477 strcat(time_buf, tz_str);
1481 #define OUTPUT_ANNOTATE_COMPAT 001
1482 #define OUTPUT_LONG_OBJECT_NAME 002
1483 #define OUTPUT_RAW_TIMESTAMP 004
1484 #define OUTPUT_PORCELAIN 010
1485 #define OUTPUT_SHOW_NAME 020
1486 #define OUTPUT_SHOW_NUMBER 040
1487 #define OUTPUT_SHOW_SCORE 0100
1489 static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
1493 struct origin *suspect = ent->suspect;
1496 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1497 printf("%s%c%d %d %d\n",
1499 ent->guilty ? ' ' : '*', // purely for debugging
1503 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1504 struct commit_info ci;
1505 suspect->commit->object.flags |= METAINFO_SHOWN;
1506 get_commit_info(suspect->commit, &ci, 1);
1507 printf("author %s\n", ci.author);
1508 printf("author-mail %s\n", ci.author_mail);
1509 printf("author-time %lu\n", ci.author_time);
1510 printf("author-tz %s\n", ci.author_tz);
1511 printf("committer %s\n", ci.committer);
1512 printf("committer-mail %s\n", ci.committer_mail);
1513 printf("committer-time %lu\n", ci.committer_time);
1514 printf("committer-tz %s\n", ci.committer_tz);
1515 write_filename_info(suspect->path);
1516 printf("summary %s\n", ci.summary);
1517 if (suspect->commit->object.flags & UNINTERESTING)
1518 printf("boundary\n");
1520 else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
1521 write_filename_info(suspect->path);
1523 cp = nth_line(sb, ent->lno);
1524 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1527 printf("%s %d %d\n", hex,
1528 ent->s_lno + 1 + cnt,
1529 ent->lno + 1 + cnt);
1534 } while (ch != '\n' &&
1535 cp < sb->final_buf + sb->final_buf_size);
1539 static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1543 struct origin *suspect = ent->suspect;
1544 struct commit_info ci;
1546 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1548 get_commit_info(suspect->commit, &ci, 1);
1549 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1551 cp = nth_line(sb, ent->lno);
1552 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1554 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8;
1556 if (suspect->commit->object.flags & UNINTERESTING) {
1557 if (!blank_boundary) {
1562 memset(hex, ' ', length);
1565 printf("%.*s", length, hex);
1566 if (opt & OUTPUT_ANNOTATE_COMPAT)
1567 printf("\t(%10s\t%10s\t%d)", ci.author,
1568 format_time(ci.author_time, ci.author_tz,
1570 ent->lno + 1 + cnt);
1572 if (opt & OUTPUT_SHOW_SCORE)
1574 max_score_digits, ent->score,
1575 ent->suspect->refcnt);
1576 if (opt & OUTPUT_SHOW_NAME)
1577 printf(" %-*.*s", longest_file, longest_file,
1579 if (opt & OUTPUT_SHOW_NUMBER)
1580 printf(" %*d", max_orig_digits,
1581 ent->s_lno + 1 + cnt);
1582 printf(" (%-*.*s %10s %*d) ",
1583 longest_author, longest_author, ci.author,
1584 format_time(ci.author_time, ci.author_tz,
1586 max_digits, ent->lno + 1 + cnt);
1591 } while (ch != '\n' &&
1592 cp < sb->final_buf + sb->final_buf_size);
1596 static void output(struct scoreboard *sb, int option)
1598 struct blame_entry *ent;
1600 if (option & OUTPUT_PORCELAIN) {
1601 for (ent = sb->ent; ent; ent = ent->next) {
1602 struct blame_entry *oth;
1603 struct origin *suspect = ent->suspect;
1604 struct commit *commit = suspect->commit;
1605 if (commit->object.flags & MORE_THAN_ONE_PATH)
1607 for (oth = ent->next; oth; oth = oth->next) {
1608 if ((oth->suspect->commit != commit) ||
1609 !strcmp(oth->suspect->path, suspect->path))
1611 commit->object.flags |= MORE_THAN_ONE_PATH;
1617 for (ent = sb->ent; ent; ent = ent->next) {
1618 if (option & OUTPUT_PORCELAIN)
1619 emit_porcelain(sb, ent);
1621 emit_other(sb, ent, option);
1627 * To allow quick access to the contents of nth line in the
1628 * final image, prepare an index in the scoreboard.
1630 static int prepare_lines(struct scoreboard *sb)
1632 const char *buf = sb->final_buf;
1633 unsigned long len = sb->final_buf_size;
1634 int num = 0, incomplete = 0, bol = 1;
1636 if (len && buf[len-1] != '\n')
1637 incomplete++; /* incomplete line at the end */
1640 sb->lineno = xrealloc(sb->lineno,
1641 sizeof(int* ) * (num + 1));
1642 sb->lineno[num] = buf - sb->final_buf;
1645 if (*buf++ == '\n') {
1650 sb->lineno = xrealloc(sb->lineno,
1651 sizeof(int* ) * (num + incomplete + 1));
1652 sb->lineno[num + incomplete] = buf - sb->final_buf;
1653 sb->num_lines = num + incomplete;
1654 return sb->num_lines;
1658 * Add phony grafts for use with -S; this is primarily to
1659 * support git-cvsserver that wants to give a linear history
1662 static int read_ancestry(const char *graft_file)
1664 FILE *fp = fopen(graft_file, "r");
1668 while (fgets(buf, sizeof(buf), fp)) {
1669 /* The format is just "Commit Parent1 Parent2 ...\n" */
1670 int len = strlen(buf);
1671 struct commit_graft *graft = read_graft_line(buf, len);
1673 register_commit_graft(graft, 0);
1680 * How many columns do we need to show line numbers in decimal?
1682 static int lineno_width(int lines)
1686 for (width = 1, i = 10; i <= lines + 1; width++)
1692 * How many columns do we need to show line numbers, authors,
1695 static void find_alignment(struct scoreboard *sb, int *option)
1697 int longest_src_lines = 0;
1698 int longest_dst_lines = 0;
1699 unsigned largest_score = 0;
1700 struct blame_entry *e;
1702 for (e = sb->ent; e; e = e->next) {
1703 struct origin *suspect = e->suspect;
1704 struct commit_info ci;
1707 if (strcmp(suspect->path, sb->path))
1708 *option |= OUTPUT_SHOW_NAME;
1709 num = strlen(suspect->path);
1710 if (longest_file < num)
1712 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
1713 suspect->commit->object.flags |= METAINFO_SHOWN;
1714 get_commit_info(suspect->commit, &ci, 1);
1715 num = strlen(ci.author);
1716 if (longest_author < num)
1717 longest_author = num;
1719 num = e->s_lno + e->num_lines;
1720 if (longest_src_lines < num)
1721 longest_src_lines = num;
1722 num = e->lno + e->num_lines;
1723 if (longest_dst_lines < num)
1724 longest_dst_lines = num;
1725 if (largest_score < ent_score(sb, e))
1726 largest_score = ent_score(sb, e);
1728 max_orig_digits = lineno_width(longest_src_lines);
1729 max_digits = lineno_width(longest_dst_lines);
1730 max_score_digits = lineno_width(largest_score);
1734 * For debugging -- origin is refcounted, and this asserts that
1735 * we do not underflow.
1737 static void sanity_check_refcnt(struct scoreboard *sb)
1740 struct blame_entry *ent;
1742 for (ent = sb->ent; ent; ent = ent->next) {
1743 /* Nobody should have zero or negative refcnt */
1744 if (ent->suspect->refcnt <= 0) {
1745 fprintf(stderr, "%s in %s has negative refcnt %d\n",
1747 sha1_to_hex(ent->suspect->commit->object.sha1),
1748 ent->suspect->refcnt);
1752 for (ent = sb->ent; ent; ent = ent->next) {
1753 /* Mark the ones that haven't been checked */
1754 if (0 < ent->suspect->refcnt)
1755 ent->suspect->refcnt = -ent->suspect->refcnt;
1757 for (ent = sb->ent; ent; ent = ent->next) {
1759 * ... then pick each and see if they have the the
1763 struct blame_entry *e;
1764 struct origin *suspect = ent->suspect;
1766 if (0 < suspect->refcnt)
1768 suspect->refcnt = -suspect->refcnt; /* Unmark */
1769 for (found = 0, e = sb->ent; e; e = e->next) {
1770 if (e->suspect != suspect)
1774 if (suspect->refcnt != found) {
1775 fprintf(stderr, "%s in %s has refcnt %d, not %d\n",
1777 sha1_to_hex(ent->suspect->commit->object.sha1),
1778 ent->suspect->refcnt, found);
1784 find_alignment(sb, &opt);
1786 die("Baa %d!", baa);
1791 * Used for the command line parsing; check if the path exists
1792 * in the working tree.
1794 static int has_path_in_work_tree(const char *path)
1797 return !lstat(path, &st);
1800 static unsigned parse_score(const char *arg)
1803 unsigned long score = strtoul(arg, &end, 10);
1809 static const char *add_prefix(const char *prefix, const char *path)
1811 if (!prefix || !prefix[0])
1813 return prefix_path(prefix, strlen(prefix), path);
1817 * Parsing of (comma separated) one item in the -L option
1819 static const char *parse_loc(const char *spec,
1820 struct scoreboard *sb, long lno,
1821 long begin, long *ret)
1828 regmatch_t match[1];
1830 /* Allow "-L <something>,+20" to mean starting at <something>
1831 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1834 if (1 < begin && (spec[0] == '+' || spec[0] == '-')) {
1835 num = strtol(spec + 1, &term, 10);
1836 if (term != spec + 1) {
1840 *ret = begin + num - 2;
1849 num = strtol(spec, &term, 10);
1857 /* it could be a regexp of form /.../ */
1858 for (term = (char*) spec + 1; *term && *term != '/'; term++) {
1865 /* try [spec+1 .. term-1] as regexp */
1867 begin--; /* input is in human terms */
1868 line = nth_line(sb, begin);
1870 if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) &&
1871 !(reg_error = regexec(®exp, line, 1, match, 0))) {
1872 const char *cp = line + match[0].rm_so;
1875 while (begin++ < lno) {
1876 nline = nth_line(sb, begin);
1877 if (line <= cp && cp < nline)
1888 regerror(reg_error, ®exp, errbuf, 1024);
1889 die("-L parameter '%s': %s", spec + 1, errbuf);
1894 * Parsing of -L option
1896 static void prepare_blame_range(struct scoreboard *sb,
1897 const char *bottomtop,
1899 long *bottom, long *top)
1903 term = parse_loc(bottomtop, sb, lno, 1, bottom);
1905 term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
1913 static int git_blame_config(const char *var, const char *value)
1915 if (!strcmp(var, "blame.showroot")) {
1916 show_root = git_config_bool(var, value);
1919 if (!strcmp(var, "blame.blankboundary")) {
1920 blank_boundary = git_config_bool(var, value);
1923 return git_default_config(var, value);
1926 static struct commit *fake_working_tree_commit(const char *path, const char *contents_from)
1928 struct commit *commit;
1929 struct origin *origin;
1930 unsigned char head_sha1[20];
1935 unsigned long fin_size;
1937 struct cache_entry *ce;
1940 if (get_sha1("HEAD", head_sha1))
1941 die("No such ref: HEAD");
1944 commit = xcalloc(1, sizeof(*commit));
1945 commit->parents = xcalloc(1, sizeof(*commit->parents));
1946 commit->parents->item = lookup_commit_reference(head_sha1);
1947 commit->object.parsed = 1;
1949 commit->object.type = OBJ_COMMIT;
1951 origin = make_origin(commit, path);
1953 if (!contents_from || strcmp("-", contents_from)) {
1955 const char *read_from;
1957 if (contents_from) {
1958 if (stat(contents_from, &st) < 0)
1959 die("Cannot stat %s", contents_from);
1960 read_from = contents_from;
1963 if (lstat(path, &st) < 0)
1964 die("Cannot lstat %s", path);
1967 fin_size = st.st_size;
1968 buf = xmalloc(fin_size+1);
1969 mode = canon_mode(st.st_mode);
1970 switch (st.st_mode & S_IFMT) {
1972 fd = open(read_from, O_RDONLY);
1974 die("cannot open %s", read_from);
1975 if (read_in_full(fd, buf, fin_size) != fin_size)
1976 die("cannot read %s", read_from);
1979 if (readlink(read_from, buf, fin_size+1) != fin_size)
1980 die("cannot readlink %s", read_from);
1983 die("unsupported file type %s", read_from);
1987 /* Reading from stdin */
1988 contents_from = "standard input";
1994 buf = xrealloc(buf, fin_size + cnt);
1995 cnt = xread(0, buf + fin_size, cnt);
1997 die("read error %s from stdin",
2003 buf = xrealloc(buf, fin_size + 1);
2006 origin->file.ptr = buf;
2007 origin->file.size = fin_size;
2008 pretend_sha1_file(buf, fin_size, blob_type, origin->blob_sha1);
2009 commit->util = origin;
2012 * Read the current index, replace the path entry with
2013 * origin->blob_sha1 without mucking with its mode or type
2014 * bits; we are not going to write this index out -- we just
2015 * want to run "diff-index --cached".
2022 int pos = cache_name_pos(path, len);
2024 mode = ntohl(active_cache[pos]->ce_mode);
2026 /* Let's not bother reading from HEAD tree */
2027 mode = S_IFREG | 0644;
2029 size = cache_entry_size(len);
2030 ce = xcalloc(1, size);
2031 hashcpy(ce->sha1, origin->blob_sha1);
2032 memcpy(ce->name, path, len);
2033 ce->ce_flags = create_ce_flags(len, 0);
2034 ce->ce_mode = create_ce_mode(mode);
2035 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2038 * We are not going to write this out, so this does not matter
2039 * right now, but someday we might optimize diff-index --cached
2040 * with cache-tree information.
2042 cache_tree_invalidate_path(active_cache_tree, path);
2044 commit->buffer = xmalloc(400);
2045 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2046 sprintf(commit->buffer,
2047 "tree 0000000000000000000000000000000000000000\n"
2051 "Version of %s from %s\n",
2052 sha1_to_hex(head_sha1),
2053 ident, ident, path, contents_from ? contents_from : path);
2057 int cmd_blame(int argc, const char **argv, const char *prefix)
2059 struct rev_info revs;
2061 struct scoreboard sb;
2063 struct blame_entry *ent;
2064 int i, seen_dashdash, unk, opt;
2065 long bottom, top, lno;
2066 int output_option = 0;
2067 const char *revs_file = NULL;
2068 const char *final_commit_name = NULL;
2070 const char *bottomtop = NULL;
2071 const char *contents_from = NULL;
2073 git_config(git_blame_config);
2074 save_commit_buffer = 0;
2078 for (unk = i = 1; i < argc; i++) {
2079 const char *arg = argv[i];
2082 else if (!strcmp("-b", arg))
2084 else if (!strcmp("--root", arg))
2086 else if (!strcmp("-c", arg))
2087 output_option |= OUTPUT_ANNOTATE_COMPAT;
2088 else if (!strcmp("-t", arg))
2089 output_option |= OUTPUT_RAW_TIMESTAMP;
2090 else if (!strcmp("-l", arg))
2091 output_option |= OUTPUT_LONG_OBJECT_NAME;
2092 else if (!strcmp("-S", arg) && ++i < argc)
2093 revs_file = argv[i];
2094 else if (!strncmp("-M", arg, 2)) {
2095 opt |= PICKAXE_BLAME_MOVE;
2096 blame_move_score = parse_score(arg+2);
2098 else if (!strncmp("-C", arg, 2)) {
2099 if (opt & PICKAXE_BLAME_COPY)
2100 opt |= PICKAXE_BLAME_COPY_HARDER;
2101 opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2102 blame_copy_score = parse_score(arg+2);
2104 else if (!strncmp("-L", arg, 2)) {
2113 die("More than one '-L n,m' option given");
2116 else if (!strcmp("--contents", arg)) {
2119 contents_from = argv[i];
2121 else if (!strcmp("--incremental", arg))
2123 else if (!strcmp("--score-debug", arg))
2124 output_option |= OUTPUT_SHOW_SCORE;
2125 else if (!strcmp("-f", arg) ||
2126 !strcmp("--show-name", arg))
2127 output_option |= OUTPUT_SHOW_NAME;
2128 else if (!strcmp("-n", arg) ||
2129 !strcmp("--show-number", arg))
2130 output_option |= OUTPUT_SHOW_NUMBER;
2131 else if (!strcmp("-p", arg) ||
2132 !strcmp("--porcelain", arg))
2133 output_option |= OUTPUT_PORCELAIN;
2134 else if (!strcmp("--", arg)) {
2146 if (!blame_move_score)
2147 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2148 if (!blame_copy_score)
2149 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2152 * We have collected options unknown to us in argv[1..unk]
2153 * which are to be passed to revision machinery if we are
2154 * going to do the "bottom" processing.
2156 * The remaining are:
2158 * (1) if seen_dashdash, its either
2159 * "-options -- <path>" or
2160 * "-options -- <path> <rev>".
2161 * but the latter is allowed only if there is no
2162 * options that we passed to revision machinery.
2164 * (2) otherwise, we may have "--" somewhere later and
2165 * might be looking at the first one of multiple 'rev'
2166 * parameters (e.g. " master ^next ^maint -- path").
2167 * See if there is a dashdash first, and give the
2168 * arguments before that to revision machinery.
2169 * After that there must be one 'path'.
2171 * (3) otherwise, its one of the three:
2172 * "-options <path> <rev>"
2173 * "-options <rev> <path>"
2175 * but again the first one is allowed only if
2176 * there is no options that we passed to revision
2180 if (seen_dashdash) {
2184 path = add_prefix(prefix, argv[i]);
2185 if (i + 1 == argc - 1) {
2188 argv[unk++] = argv[i + 1];
2190 else if (i + 1 != argc)
2191 /* garbage at end */
2196 for (j = i; !seen_dashdash && j < argc; j++)
2197 if (!strcmp(argv[j], "--"))
2199 if (seen_dashdash) {
2200 if (seen_dashdash + 1 != argc - 1)
2202 path = add_prefix(prefix, argv[seen_dashdash + 1]);
2203 for (j = i; j < seen_dashdash; j++)
2204 argv[unk++] = argv[j];
2208 path = add_prefix(prefix, argv[i]);
2209 if (i + 1 == argc - 1) {
2210 final_commit_name = argv[i + 1];
2212 /* if (unk == 1) we could be getting
2215 if (unk == 1 && !has_path_in_work_tree(path)) {
2216 path = add_prefix(prefix, argv[i + 1]);
2217 final_commit_name = argv[i];
2220 else if (i != argc - 1)
2221 usage(blame_usage); /* garbage at end */
2223 if (!has_path_in_work_tree(path))
2224 die("cannot stat path %s: %s",
2225 path, strerror(errno));
2229 if (final_commit_name)
2230 argv[unk++] = final_commit_name;
2233 * Now we got rev and path. We do not want the path pruning
2234 * but we may want "bottom" processing.
2236 argv[unk++] = "--"; /* terminate the rev name */
2239 init_revisions(&revs, NULL);
2240 setup_revisions(unk, argv, &revs, NULL);
2241 memset(&sb, 0, sizeof(sb));
2244 * There must be one and only one positive commit in the
2245 * revs->pending array.
2247 for (i = 0; i < revs.pending.nr; i++) {
2248 struct object *obj = revs.pending.objects[i].item;
2249 if (obj->flags & UNINTERESTING)
2251 while (obj->type == OBJ_TAG)
2252 obj = deref_tag(obj, NULL, 0);
2253 if (obj->type != OBJ_COMMIT)
2254 die("Non commit %s?",
2255 revs.pending.objects[i].name);
2257 die("More than one commit to dig from %s and %s?",
2258 revs.pending.objects[i].name,
2260 sb.final = (struct commit *) obj;
2261 final_commit_name = revs.pending.objects[i].name;
2266 * "--not A B -- path" without anything positive;
2267 * do not default to HEAD, but use the working tree
2270 sb.final = fake_working_tree_commit(path, contents_from);
2271 add_pending_object(&revs, &(sb.final->object), ":");
2273 else if (contents_from)
2274 die("Cannot use --contents with final commit object name");
2277 * If we have bottom, this will mark the ancestors of the
2278 * bottom commits we would reach while traversing as
2281 prepare_revision_walk(&revs);
2283 if (is_null_sha1(sb.final->object.sha1)) {
2286 buf = xmalloc(o->file.size + 1);
2287 memcpy(buf, o->file.ptr, o->file.size + 1);
2289 sb.final_buf_size = o->file.size;
2292 o = get_origin(&sb, sb.final, path);
2293 if (fill_blob_sha1(o))
2294 die("no such path %s in %s", path, final_commit_name);
2296 sb.final_buf = read_sha1_file(o->blob_sha1, type,
2297 &sb.final_buf_size);
2300 lno = prepare_lines(&sb);
2304 prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
2305 if (bottom && top && top < bottom) {
2307 tmp = top; top = bottom; bottom = tmp;
2315 die("file %s has only %lu lines", path, lno);
2317 ent = xcalloc(1, sizeof(*ent));
2319 ent->num_lines = top - bottom;
2321 ent->s_lno = bottom;
2326 if (revs_file && read_ancestry(revs_file))
2327 die("reading graft file %s failed: %s",
2328 revs_file, strerror(errno));
2330 assign_blame(&sb, &revs, opt);
2337 if (!(output_option & OUTPUT_PORCELAIN))
2338 find_alignment(&sb, &output_option);
2340 output(&sb, output_option);
2341 free((void *)sb.final_buf);
2342 for (ent = sb.ent; ent; ) {
2343 struct blame_entry *e = ent->next;
2349 printf("num read blob: %d\n", num_read_blob);
2350 printf("num get patch: %d\n", num_get_patch);
2351 printf("num commits: %d\n", num_commits);