4 * Copyright (c) 2006, 2014 by its authors
5 * See COPYING for licensing conditions
14 #include "tree-walk.h"
19 #include "xdiff-interface.h"
20 #include "cache-tree.h"
21 #include "string-list.h"
23 #include "mergesort.h"
24 #include "parse-options.h"
25 #include "prio-queue.h"
28 #include "line-range.h"
32 static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
34 static const char *blame_opt_usage[] = {
37 N_("<rev-opts> are documented in git-rev-list(1)"),
41 static int longest_file;
42 static int longest_author;
43 static int max_orig_digits;
44 static int max_digits;
45 static int max_score_digits;
48 static int blank_boundary;
49 static int incremental;
51 static int abbrev = -1;
52 static int no_whole_file_rename;
54 static struct date_mode blame_date_mode = { DATE_ISO8601 };
55 static size_t blame_date_width;
57 static struct string_list mailmap;
64 static int num_read_blob;
65 static int num_get_patch;
66 static int num_commits;
68 #define PICKAXE_BLAME_MOVE 01
69 #define PICKAXE_BLAME_COPY 02
70 #define PICKAXE_BLAME_COPY_HARDER 04
71 #define PICKAXE_BLAME_COPY_HARDEST 010
74 * blame for a blame_entry with score lower than these thresholds
75 * is not passed to the parent using move/copy logic.
77 static unsigned blame_move_score;
78 static unsigned blame_copy_score;
79 #define BLAME_DEFAULT_MOVE_SCORE 20
80 #define BLAME_DEFAULT_COPY_SCORE 40
82 /* Remember to update object flag allocation in object.h */
83 #define METAINFO_SHOWN (1u<<12)
84 #define MORE_THAN_ONE_PATH (1u<<13)
87 * One blob in a commit that is being suspected
91 /* Record preceding blame record for this blob */
92 struct origin *previous;
93 /* origins are put in a list linked via `next' hanging off the
94 * corresponding commit's util field in order to make finding
95 * them fast. The presence in this chain does not count
96 * towards the origin's reference count. It is tempting to
97 * let it count as long as the commit is pending examination,
98 * but even under circumstances where the commit will be
99 * present multiple times in the priority queue of unexamined
100 * commits, processing the first instance will not leave any
101 * work requiring the origin data for the second instance. An
102 * interspersed commit changing that would have to be
103 * preexisting with a different ancestry and with the same
104 * commit date in order to wedge itself between two instances
105 * of the same commit in the priority queue _and_ produce
106 * blame entries relevant for it. While we don't want to let
107 * us get tripped up by this case, it certainly does not seem
108 * worth optimizing for.
111 struct commit *commit;
112 /* `suspects' contains blame entries that may be attributed to
113 * this origin's commit or to parent commits. When a commit
114 * is being processed, all suspects will be moved, either by
115 * assigning them to an origin in a different commit, or by
116 * shipping them to the scoreboard's ent list because they
117 * cannot be attributed to a different commit.
119 struct blame_entry *suspects;
121 unsigned char blob_sha1[20];
123 /* guilty gets set when shipping any suspects to the final
124 * blame list instead of other commits
127 char path[FLEX_ARRAY];
130 static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, long ctxlen,
131 xdl_emit_hunk_consume_func_t hunk_func, void *cb_data)
134 xdemitconf_t xecfg = {0};
135 xdemitcb_t ecb = {NULL};
137 xpp.flags = xdl_opts;
138 xecfg.ctxlen = ctxlen;
139 xecfg.hunk_func = hunk_func;
141 return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
145 * Prepare diff_filespec and convert it using diff textconv API
146 * if the textconv driver exists.
147 * Return 1 if the conversion succeeds, 0 otherwise.
149 int textconv_object(const char *path,
151 const unsigned char *sha1,
154 unsigned long *buf_size)
156 struct diff_filespec *df;
157 struct userdiff_driver *textconv;
159 df = alloc_filespec(path);
160 fill_filespec(df, sha1, sha1_valid, mode);
161 textconv = get_textconv(df);
167 *buf_size = fill_textconv(textconv, df, buf);
173 * Given an origin, prepare mmfile_t structure to be used by the
176 static void fill_origin_blob(struct diff_options *opt,
177 struct origin *o, mmfile_t *file)
180 enum object_type type;
181 unsigned long file_size;
184 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
185 textconv_object(o->path, o->mode, o->blob_sha1, 1, &file->ptr, &file_size))
188 file->ptr = read_sha1_file(o->blob_sha1, &type, &file_size);
189 file->size = file_size;
192 die("Cannot read blob %s for path %s",
193 sha1_to_hex(o->blob_sha1),
202 * Origin is refcounted and usually we keep the blob contents to be
205 static inline struct origin *origin_incref(struct origin *o)
212 static void origin_decref(struct origin *o)
214 if (o && --o->refcnt <= 0) {
215 struct origin *p, *l = NULL;
217 origin_decref(o->previous);
219 /* Should be present exactly once in commit chain */
220 for (p = o->commit->util; p; l = p, p = p->next) {
225 o->commit->util = p->next;
230 die("internal error in blame::origin_decref");
234 static void drop_origin_blob(struct origin *o)
243 * Each group of lines is described by a blame_entry; it can be split
244 * as we pass blame to the parents. They are arranged in linked lists
245 * kept as `suspects' of some unprocessed origin, or entered (when the
246 * blame origin has been finalized) into the scoreboard structure.
247 * While the scoreboard structure is only sorted at the end of
248 * processing (according to final image line number), the lists
249 * attached to an origin are sorted by the target line number.
252 struct blame_entry *next;
254 /* the first line of this group in the final image;
255 * internally all line numbers are 0 based.
259 /* how many lines this group has */
262 /* the commit that introduced this group into the final image */
263 struct origin *suspect;
265 /* the line number of the first line of this group in the
266 * suspect's file; internally all line numbers are 0 based.
270 /* how significant this entry is -- cached to avoid
271 * scanning the lines over and over.
277 * Any merge of blames happens on lists of blames that arrived via
278 * different parents in a single suspect. In this case, we want to
279 * sort according to the suspect line numbers as opposed to the final
280 * image line numbers. The function body is somewhat longish because
281 * it avoids unnecessary writes.
284 static struct blame_entry *blame_merge(struct blame_entry *list1,
285 struct blame_entry *list2)
287 struct blame_entry *p1 = list1, *p2 = list2,
295 if (p1->s_lno <= p2->s_lno) {
298 if ((p1 = *tail) == NULL) {
302 } while (p1->s_lno <= p2->s_lno);
308 if ((p2 = *tail) == NULL) {
312 } while (p1->s_lno > p2->s_lno);
316 if ((p1 = *tail) == NULL) {
320 } while (p1->s_lno <= p2->s_lno);
324 static void *get_next_blame(const void *p)
326 return ((struct blame_entry *)p)->next;
329 static void set_next_blame(void *p1, void *p2)
331 ((struct blame_entry *)p1)->next = p2;
335 * Final image line numbers are all different, so we don't need a
336 * three-way comparison here.
339 static int compare_blame_final(const void *p1, const void *p2)
341 return ((struct blame_entry *)p1)->lno > ((struct blame_entry *)p2)->lno
345 static int compare_blame_suspect(const void *p1, const void *p2)
347 const struct blame_entry *s1 = p1, *s2 = p2;
349 * to allow for collating suspects, we sort according to the
350 * respective pointer value as the primary sorting criterion.
351 * The actual relation is pretty unimportant as long as it
352 * establishes a total order. Comparing as integers gives us
355 if (s1->suspect != s2->suspect)
356 return (intptr_t)s1->suspect > (intptr_t)s2->suspect ? 1 : -1;
357 if (s1->s_lno == s2->s_lno)
359 return s1->s_lno > s2->s_lno ? 1 : -1;
362 static struct blame_entry *blame_sort(struct blame_entry *head,
363 int (*compare_fn)(const void *, const void *))
365 return llist_mergesort (head, get_next_blame, set_next_blame, compare_fn);
368 static int compare_commits_by_reverse_commit_date(const void *a,
372 return -compare_commits_by_commit_date(a, b, c);
376 * The current state of the blame assignment.
379 /* the final commit (i.e. where we started digging from) */
380 struct commit *final;
381 /* Priority queue for commits with unassigned blame records */
382 struct prio_queue commits;
383 struct rev_info *revs;
387 * The contents in the final image.
388 * Used by many functions to obtain contents of the nth line,
389 * indexed with scoreboard.lineno[blame_entry.lno].
391 const char *final_buf;
392 unsigned long final_buf_size;
394 /* linked list of blames */
395 struct blame_entry *ent;
397 /* look-up a line in the final buffer */
402 static void sanity_check_refcnt(struct scoreboard *);
405 * If two blame entries that are next to each other came from
406 * contiguous lines in the same origin (i.e. <commit, path> pair),
407 * merge them together.
409 static void coalesce(struct scoreboard *sb)
411 struct blame_entry *ent, *next;
413 for (ent = sb->ent; ent && (next = ent->next); ent = next) {
414 if (ent->suspect == next->suspect &&
415 ent->s_lno + ent->num_lines == next->s_lno) {
416 ent->num_lines += next->num_lines;
417 ent->next = next->next;
418 origin_decref(next->suspect);
421 next = ent; /* again */
425 if (DEBUG) /* sanity */
426 sanity_check_refcnt(sb);
430 * Merge the given sorted list of blames into a preexisting origin.
431 * If there were no previous blames to that commit, it is entered into
432 * the commit priority queue of the score board.
435 static void queue_blames(struct scoreboard *sb, struct origin *porigin,
436 struct blame_entry *sorted)
438 if (porigin->suspects)
439 porigin->suspects = blame_merge(porigin->suspects, sorted);
442 for (o = porigin->commit->util; o; o = o->next) {
444 porigin->suspects = sorted;
448 porigin->suspects = sorted;
449 prio_queue_put(&sb->commits, porigin->commit);
454 * Given a commit and a path in it, create a new origin structure.
455 * The callers that add blame to the scoreboard should use
456 * get_origin() to obtain shared, refcounted copy instead of calling
457 * this function directly.
459 static struct origin *make_origin(struct commit *commit, const char *path)
462 o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
465 o->next = commit->util;
467 strcpy(o->path, path);
472 * Locate an existing origin or create a new one.
473 * This moves the origin to front position in the commit util list.
475 static struct origin *get_origin(struct scoreboard *sb,
476 struct commit *commit,
479 struct origin *o, *l;
481 for (o = commit->util, l = NULL; o; l = o, o = o->next) {
482 if (!strcmp(o->path, path)) {
486 o->next = commit->util;
489 return origin_incref(o);
492 return make_origin(commit, path);
496 * Fill the blob_sha1 field of an origin if it hasn't, so that later
497 * call to fill_origin_blob() can use it to locate the data. blob_sha1
498 * for an origin is also used to pass the blame for the entire file to
499 * the parent to detect the case where a child's blob is identical to
500 * that of its parent's.
502 * This also fills origin->mode for corresponding tree path.
504 static int fill_blob_sha1_and_mode(struct origin *origin)
506 if (!is_null_sha1(origin->blob_sha1))
508 if (get_tree_entry(origin->commit->object.sha1,
510 origin->blob_sha1, &origin->mode))
512 if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
516 hashclr(origin->blob_sha1);
517 origin->mode = S_IFINVALID;
522 * We have an origin -- check if the same path exists in the
523 * parent and return an origin structure to represent it.
525 static struct origin *find_origin(struct scoreboard *sb,
526 struct commit *parent,
527 struct origin *origin)
529 struct origin *porigin;
530 struct diff_options diff_opts;
531 const char *paths[2];
533 /* First check any existing origins */
534 for (porigin = parent->util; porigin; porigin = porigin->next)
535 if (!strcmp(porigin->path, origin->path)) {
537 * The same path between origin and its parent
538 * without renaming -- the most common case.
540 return origin_incref (porigin);
543 /* See if the origin->path is different between parent
544 * and origin first. Most of the time they are the
545 * same and diff-tree is fairly efficient about this.
547 diff_setup(&diff_opts);
548 DIFF_OPT_SET(&diff_opts, RECURSIVE);
549 diff_opts.detect_rename = 0;
550 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
551 paths[0] = origin->path;
554 parse_pathspec(&diff_opts.pathspec,
555 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
556 PATHSPEC_LITERAL_PATH, "", paths);
557 diff_setup_done(&diff_opts);
559 if (is_null_sha1(origin->commit->object.sha1))
560 do_diff_cache(parent->tree->object.sha1, &diff_opts);
562 diff_tree_sha1(parent->tree->object.sha1,
563 origin->commit->tree->object.sha1,
565 diffcore_std(&diff_opts);
567 if (!diff_queued_diff.nr) {
568 /* The path is the same as parent */
569 porigin = get_origin(sb, parent, origin->path);
570 hashcpy(porigin->blob_sha1, origin->blob_sha1);
571 porigin->mode = origin->mode;
574 * Since origin->path is a pathspec, if the parent
575 * commit had it as a directory, we will see a whole
576 * bunch of deletion of files in the directory that we
580 struct diff_filepair *p = NULL;
581 for (i = 0; i < diff_queued_diff.nr; i++) {
583 p = diff_queued_diff.queue[i];
584 name = p->one->path ? p->one->path : p->two->path;
585 if (!strcmp(name, origin->path))
589 die("internal error in blame::find_origin");
592 die("internal error in blame::find_origin (%c)",
595 porigin = get_origin(sb, parent, origin->path);
596 hashcpy(porigin->blob_sha1, p->one->sha1);
597 porigin->mode = p->one->mode;
601 /* Did not exist in parent, or type changed */
605 diff_flush(&diff_opts);
606 free_pathspec(&diff_opts.pathspec);
611 * We have an origin -- find the path that corresponds to it in its
612 * parent and return an origin structure to represent it.
614 static struct origin *find_rename(struct scoreboard *sb,
615 struct commit *parent,
616 struct origin *origin)
618 struct origin *porigin = NULL;
619 struct diff_options diff_opts;
622 diff_setup(&diff_opts);
623 DIFF_OPT_SET(&diff_opts, RECURSIVE);
624 diff_opts.detect_rename = DIFF_DETECT_RENAME;
625 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
626 diff_opts.single_follow = origin->path;
627 diff_setup_done(&diff_opts);
629 if (is_null_sha1(origin->commit->object.sha1))
630 do_diff_cache(parent->tree->object.sha1, &diff_opts);
632 diff_tree_sha1(parent->tree->object.sha1,
633 origin->commit->tree->object.sha1,
635 diffcore_std(&diff_opts);
637 for (i = 0; i < diff_queued_diff.nr; i++) {
638 struct diff_filepair *p = diff_queued_diff.queue[i];
639 if ((p->status == 'R' || p->status == 'C') &&
640 !strcmp(p->two->path, origin->path)) {
641 porigin = get_origin(sb, parent, p->one->path);
642 hashcpy(porigin->blob_sha1, p->one->sha1);
643 porigin->mode = p->one->mode;
647 diff_flush(&diff_opts);
648 free_pathspec(&diff_opts.pathspec);
653 * Append a new blame entry to a given output queue.
655 static void add_blame_entry(struct blame_entry ***queue, struct blame_entry *e)
657 origin_incref(e->suspect);
665 * src typically is on-stack; we want to copy the information in it to
666 * a malloced blame_entry that gets added to the given queue. The
667 * origin of dst loses a refcnt.
669 static void dup_entry(struct blame_entry ***queue,
670 struct blame_entry *dst, struct blame_entry *src)
672 origin_incref(src->suspect);
673 origin_decref(dst->suspect);
674 memcpy(dst, src, sizeof(*src));
680 static const char *nth_line(struct scoreboard *sb, long lno)
682 return sb->final_buf + sb->lineno[lno];
685 static const char *nth_line_cb(void *data, long lno)
687 return nth_line((struct scoreboard *)data, lno);
691 * It is known that lines between tlno to same came from parent, and e
692 * has an overlap with that range. it also is known that parent's
693 * line plno corresponds to e's line tlno.
699 * <------------------>
701 * Split e into potentially three parts; before this chunk, the chunk
702 * to be blamed for the parent, and after that portion.
704 static void split_overlap(struct blame_entry *split,
705 struct blame_entry *e,
706 int tlno, int plno, int same,
707 struct origin *parent)
710 memset(split, 0, sizeof(struct blame_entry [3]));
712 if (e->s_lno < tlno) {
713 /* there is a pre-chunk part not blamed on parent */
714 split[0].suspect = origin_incref(e->suspect);
715 split[0].lno = e->lno;
716 split[0].s_lno = e->s_lno;
717 split[0].num_lines = tlno - e->s_lno;
718 split[1].lno = e->lno + tlno - e->s_lno;
719 split[1].s_lno = plno;
722 split[1].lno = e->lno;
723 split[1].s_lno = plno + (e->s_lno - tlno);
726 if (same < e->s_lno + e->num_lines) {
727 /* there is a post-chunk part not blamed on parent */
728 split[2].suspect = origin_incref(e->suspect);
729 split[2].lno = e->lno + (same - e->s_lno);
730 split[2].s_lno = e->s_lno + (same - e->s_lno);
731 split[2].num_lines = e->s_lno + e->num_lines - same;
732 chunk_end_lno = split[2].lno;
735 chunk_end_lno = e->lno + e->num_lines;
736 split[1].num_lines = chunk_end_lno - split[1].lno;
739 * if it turns out there is nothing to blame the parent for,
740 * forget about the splitting. !split[1].suspect signals this.
742 if (split[1].num_lines < 1)
744 split[1].suspect = origin_incref(parent);
748 * split_overlap() divided an existing blame e into up to three parts
749 * in split. Any assigned blame is moved to queue to
752 static void split_blame(struct blame_entry ***blamed,
753 struct blame_entry ***unblamed,
754 struct blame_entry *split,
755 struct blame_entry *e)
757 struct blame_entry *new_entry;
759 if (split[0].suspect && split[2].suspect) {
760 /* The first part (reuse storage for the existing entry e) */
761 dup_entry(unblamed, e, &split[0]);
763 /* The last part -- me */
764 new_entry = xmalloc(sizeof(*new_entry));
765 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
766 add_blame_entry(unblamed, new_entry);
768 /* ... and the middle part -- parent */
769 new_entry = xmalloc(sizeof(*new_entry));
770 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
771 add_blame_entry(blamed, new_entry);
773 else if (!split[0].suspect && !split[2].suspect)
775 * The parent covers the entire area; reuse storage for
776 * e and replace it with the parent.
778 dup_entry(blamed, e, &split[1]);
779 else if (split[0].suspect) {
780 /* me and then parent */
781 dup_entry(unblamed, e, &split[0]);
783 new_entry = xmalloc(sizeof(*new_entry));
784 memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
785 add_blame_entry(blamed, new_entry);
788 /* parent and then me */
789 dup_entry(blamed, e, &split[1]);
791 new_entry = xmalloc(sizeof(*new_entry));
792 memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
793 add_blame_entry(unblamed, new_entry);
798 * After splitting the blame, the origins used by the
799 * on-stack blame_entry should lose one refcnt each.
801 static void decref_split(struct blame_entry *split)
805 for (i = 0; i < 3; i++)
806 origin_decref(split[i].suspect);
810 * reverse_blame reverses the list given in head, appending tail.
811 * That allows us to build lists in reverse order, then reverse them
812 * afterwards. This can be faster than building the list in proper
813 * order right away. The reason is that building in proper order
814 * requires writing a link in the _previous_ element, while building
815 * in reverse order just requires placing the list head into the
819 static struct blame_entry *reverse_blame(struct blame_entry *head,
820 struct blame_entry *tail)
823 struct blame_entry *next = head->next;
832 * Process one hunk from the patch between the current suspect for
833 * blame_entry e and its parent. This first blames any unfinished
834 * entries before the chunk (which is where target and parent start
835 * differing) on the parent, and then splits blame entries at the
836 * start and at the end of the difference region. Since use of -M and
837 * -C options may lead to overlapping/duplicate source line number
838 * ranges, all we can rely on from sorting/merging is the order of the
839 * first suspect line number.
841 static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
842 int tlno, int offset, int same,
843 struct origin *parent)
845 struct blame_entry *e = **srcq;
846 struct blame_entry *samep = NULL, *diffp = NULL;
848 while (e && e->s_lno < tlno) {
849 struct blame_entry *next = e->next;
851 * current record starts before differing portion. If
852 * it reaches into it, we need to split it up and
853 * examine the second part separately.
855 if (e->s_lno + e->num_lines > tlno) {
856 /* Move second half to a new record */
857 int len = tlno - e->s_lno;
858 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
859 n->suspect = e->suspect;
860 n->lno = e->lno + len;
861 n->s_lno = e->s_lno + len;
862 n->num_lines = e->num_lines - len;
865 /* Push new record to diffp */
869 origin_decref(e->suspect);
870 /* Pass blame for everything before the differing
871 * chunk to the parent */
872 e->suspect = origin_incref(parent);
879 * As we don't know how much of a common stretch after this
880 * diff will occur, the currently blamed parts are all that we
881 * can assign to the parent for now.
885 **dstq = reverse_blame(samep, **dstq);
886 *dstq = &samep->next;
889 * Prepend the split off portions: everything after e starts
890 * after the blameable portion.
892 e = reverse_blame(diffp, e);
895 * Now retain records on the target while parts are different
900 while (e && e->s_lno < same) {
901 struct blame_entry *next = e->next;
904 * If current record extends into sameness, need to split.
906 if (e->s_lno + e->num_lines > same) {
908 * Move second half to a new record to be
909 * processed by later chunks
911 int len = same - e->s_lno;
912 struct blame_entry *n = xcalloc(1, sizeof (struct blame_entry));
913 n->suspect = origin_incref(e->suspect);
914 n->lno = e->lno + len;
915 n->s_lno = e->s_lno + len;
916 n->num_lines = e->num_lines - len;
919 /* Push new record to samep */
927 **srcq = reverse_blame(diffp, reverse_blame(samep, e));
928 /* Move across elements that are in the unblamable portion */
930 *srcq = &diffp->next;
933 struct blame_chunk_cb_data {
934 struct origin *parent;
936 struct blame_entry **dstq;
937 struct blame_entry **srcq;
940 /* diff chunks are from parent to target */
941 static int blame_chunk_cb(long start_a, long count_a,
942 long start_b, long count_b, void *data)
944 struct blame_chunk_cb_data *d = data;
945 if (start_a - start_b != d->offset)
946 die("internal error in blame::blame_chunk_cb");
947 blame_chunk(&d->dstq, &d->srcq, start_b, start_a - start_b,
948 start_b + count_b, d->parent);
949 d->offset = start_a + count_a - (start_b + count_b);
954 * We are looking at the origin 'target' and aiming to pass blame
955 * for the lines it is suspected to its parent. Run diff to find
956 * which lines came from parent and pass blame for them.
958 static void pass_blame_to_parent(struct scoreboard *sb,
959 struct origin *target,
960 struct origin *parent)
962 mmfile_t file_p, file_o;
963 struct blame_chunk_cb_data d;
964 struct blame_entry *newdest = NULL;
966 if (!target->suspects)
967 return; /* nothing remains for this target */
971 d.dstq = &newdest; d.srcq = &target->suspects;
973 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
974 fill_origin_blob(&sb->revs->diffopt, target, &file_o);
977 if (diff_hunks(&file_p, &file_o, 0, blame_chunk_cb, &d))
978 die("unable to generate diff (%s -> %s)",
979 sha1_to_hex(parent->commit->object.sha1),
980 sha1_to_hex(target->commit->object.sha1));
981 /* The rest are the same as the parent */
982 blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, parent);
984 queue_blames(sb, parent, newdest);
990 * The lines in blame_entry after splitting blames many times can become
991 * very small and trivial, and at some point it becomes pointless to
992 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
993 * ordinary C program, and it is not worth to say it was copied from
994 * totally unrelated file in the parent.
996 * Compute how trivial the lines in the blame_entry are.
998 static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
1001 const char *cp, *ep;
1007 cp = nth_line(sb, e->lno);
1008 ep = nth_line(sb, e->lno + e->num_lines);
1010 unsigned ch = *((unsigned char *)cp);
1020 * best_so_far[] and this[] are both a split of an existing blame_entry
1021 * that passes blame to the parent. Maintain best_so_far the best split
1022 * so far, by comparing this and best_so_far and copying this into
1023 * bst_so_far as needed.
1025 static void copy_split_if_better(struct scoreboard *sb,
1026 struct blame_entry *best_so_far,
1027 struct blame_entry *this)
1031 if (!this[1].suspect)
1033 if (best_so_far[1].suspect) {
1034 if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
1038 for (i = 0; i < 3; i++)
1039 origin_incref(this[i].suspect);
1040 decref_split(best_so_far);
1041 memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
1045 * We are looking at a part of the final image represented by
1046 * ent (tlno and same are offset by ent->s_lno).
1047 * tlno is where we are looking at in the final image.
1048 * up to (but not including) same match preimage.
1049 * plno is where we are looking at in the preimage.
1051 * <-------------- final image ---------------------->
1054 * <---------preimage----->
1057 * All line numbers are 0-based.
1059 static void handle_split(struct scoreboard *sb,
1060 struct blame_entry *ent,
1061 int tlno, int plno, int same,
1062 struct origin *parent,
1063 struct blame_entry *split)
1065 if (ent->num_lines <= tlno)
1068 struct blame_entry this[3];
1071 split_overlap(this, ent, tlno, plno, same, parent);
1072 copy_split_if_better(sb, split, this);
1077 struct handle_split_cb_data {
1078 struct scoreboard *sb;
1079 struct blame_entry *ent;
1080 struct origin *parent;
1081 struct blame_entry *split;
1086 static int handle_split_cb(long start_a, long count_a,
1087 long start_b, long count_b, void *data)
1089 struct handle_split_cb_data *d = data;
1090 handle_split(d->sb, d->ent, d->tlno, d->plno, start_b, d->parent,
1092 d->plno = start_a + count_a;
1093 d->tlno = start_b + count_b;
1098 * Find the lines from parent that are the same as ent so that
1099 * we can pass blames to it. file_p has the blob contents for
1102 static void find_copy_in_blob(struct scoreboard *sb,
1103 struct blame_entry *ent,
1104 struct origin *parent,
1105 struct blame_entry *split,
1110 struct handle_split_cb_data d;
1112 memset(&d, 0, sizeof(d));
1113 d.sb = sb; d.ent = ent; d.parent = parent; d.split = split;
1115 * Prepare mmfile that contains only the lines in ent.
1117 cp = nth_line(sb, ent->lno);
1118 file_o.ptr = (char *) cp;
1119 file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
1122 * file_o is a part of final image we are annotating.
1123 * file_p partially may match that image.
1125 memset(split, 0, sizeof(struct blame_entry [3]));
1126 if (diff_hunks(file_p, &file_o, 1, handle_split_cb, &d))
1127 die("unable to generate diff (%s)",
1128 sha1_to_hex(parent->commit->object.sha1));
1129 /* remainder, if any, all match the preimage */
1130 handle_split(sb, ent, d.tlno, d.plno, ent->num_lines, parent, split);
1133 /* Move all blame entries from list *source that have a score smaller
1134 * than score_min to the front of list *small.
1135 * Returns a pointer to the link pointing to the old head of the small list.
1138 static struct blame_entry **filter_small(struct scoreboard *sb,
1139 struct blame_entry **small,
1140 struct blame_entry **source,
1143 struct blame_entry *p = *source;
1144 struct blame_entry *oldsmall = *small;
1146 if (ent_score(sb, p) <= score_min) {
1162 * See if lines currently target is suspected for can be attributed to
1165 static void find_move_in_parent(struct scoreboard *sb,
1166 struct blame_entry ***blamed,
1167 struct blame_entry **toosmall,
1168 struct origin *target,
1169 struct origin *parent)
1171 struct blame_entry *e, split[3];
1172 struct blame_entry *unblamed = target->suspects;
1173 struct blame_entry *leftover = NULL;
1177 return; /* nothing remains for this target */
1179 fill_origin_blob(&sb->revs->diffopt, parent, &file_p);
1183 /* At each iteration, unblamed has a NULL-terminated list of
1184 * entries that have not yet been tested for blame. leftover
1185 * contains the reversed list of entries that have been tested
1186 * without being assignable to the parent.
1189 struct blame_entry **unblamedtail = &unblamed;
1190 struct blame_entry *next;
1191 for (e = unblamed; e; e = next) {
1193 find_copy_in_blob(sb, e, parent, split, &file_p);
1194 if (split[1].suspect &&
1195 blame_move_score < ent_score(sb, &split[1])) {
1196 split_blame(blamed, &unblamedtail, split, e);
1201 decref_split(split);
1203 *unblamedtail = NULL;
1204 toosmall = filter_small(sb, toosmall, &unblamed, blame_move_score);
1206 target->suspects = reverse_blame(leftover, NULL);
1210 struct blame_entry *ent;
1211 struct blame_entry split[3];
1215 * Count the number of entries the target is suspected for,
1216 * and prepare a list of entry and the best split.
1218 static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
1221 struct blame_entry *e;
1223 struct blame_list *blame_list = NULL;
1225 for (e = unblamed, num_ents = 0; e; e = e->next)
1228 blame_list = xcalloc(num_ents, sizeof(struct blame_list));
1229 for (e = unblamed, i = 0; e; e = e->next)
1230 blame_list[i++].ent = e;
1232 *num_ents_p = num_ents;
1237 * For lines target is suspected for, see if we can find code movement
1238 * across file boundary from the parent commit. porigin is the path
1239 * in the parent we already tried.
1241 static void find_copy_in_parent(struct scoreboard *sb,
1242 struct blame_entry ***blamed,
1243 struct blame_entry **toosmall,
1244 struct origin *target,
1245 struct commit *parent,
1246 struct origin *porigin,
1249 struct diff_options diff_opts;
1251 struct blame_list *blame_list;
1253 struct blame_entry *unblamed = target->suspects;
1254 struct blame_entry *leftover = NULL;
1257 return; /* nothing remains for this target */
1259 diff_setup(&diff_opts);
1260 DIFF_OPT_SET(&diff_opts, RECURSIVE);
1261 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1263 diff_setup_done(&diff_opts);
1265 /* Try "find copies harder" on new path if requested;
1266 * we do not want to use diffcore_rename() actually to
1267 * match things up; find_copies_harder is set only to
1268 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1269 * and this code needs to be after diff_setup_done(), which
1270 * usually makes find-copies-harder imply copy detection.
1272 if ((opt & PICKAXE_BLAME_COPY_HARDEST)
1273 || ((opt & PICKAXE_BLAME_COPY_HARDER)
1274 && (!porigin || strcmp(target->path, porigin->path))))
1275 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
1277 if (is_null_sha1(target->commit->object.sha1))
1278 do_diff_cache(parent->tree->object.sha1, &diff_opts);
1280 diff_tree_sha1(parent->tree->object.sha1,
1281 target->commit->tree->object.sha1,
1284 if (!DIFF_OPT_TST(&diff_opts, FIND_COPIES_HARDER))
1285 diffcore_std(&diff_opts);
1288 struct blame_entry **unblamedtail = &unblamed;
1289 blame_list = setup_blame_list(unblamed, &num_ents);
1291 for (i = 0; i < diff_queued_diff.nr; i++) {
1292 struct diff_filepair *p = diff_queued_diff.queue[i];
1293 struct origin *norigin;
1295 struct blame_entry this[3];
1297 if (!DIFF_FILE_VALID(p->one))
1298 continue; /* does not exist in parent */
1299 if (S_ISGITLINK(p->one->mode))
1300 continue; /* ignore git links */
1301 if (porigin && !strcmp(p->one->path, porigin->path))
1302 /* find_move already dealt with this path */
1305 norigin = get_origin(sb, parent, p->one->path);
1306 hashcpy(norigin->blob_sha1, p->one->sha1);
1307 norigin->mode = p->one->mode;
1308 fill_origin_blob(&sb->revs->diffopt, norigin, &file_p);
1312 for (j = 0; j < num_ents; j++) {
1313 find_copy_in_blob(sb, blame_list[j].ent,
1314 norigin, this, &file_p);
1315 copy_split_if_better(sb, blame_list[j].split,
1319 origin_decref(norigin);
1322 for (j = 0; j < num_ents; j++) {
1323 struct blame_entry *split = blame_list[j].split;
1324 if (split[1].suspect &&
1325 blame_copy_score < ent_score(sb, &split[1])) {
1326 split_blame(blamed, &unblamedtail, split,
1329 blame_list[j].ent->next = leftover;
1330 leftover = blame_list[j].ent;
1332 decref_split(split);
1335 *unblamedtail = NULL;
1336 toosmall = filter_small(sb, toosmall, &unblamed, blame_copy_score);
1338 target->suspects = reverse_blame(leftover, NULL);
1339 diff_flush(&diff_opts);
1340 free_pathspec(&diff_opts.pathspec);
1344 * The blobs of origin and porigin exactly match, so everything
1345 * origin is suspected for can be blamed on the parent.
1347 static void pass_whole_blame(struct scoreboard *sb,
1348 struct origin *origin, struct origin *porigin)
1350 struct blame_entry *e, *suspects;
1352 if (!porigin->file.ptr && origin->file.ptr) {
1353 /* Steal its file */
1354 porigin->file = origin->file;
1355 origin->file.ptr = NULL;
1357 suspects = origin->suspects;
1358 origin->suspects = NULL;
1359 for (e = suspects; e; e = e->next) {
1360 origin_incref(porigin);
1361 origin_decref(e->suspect);
1362 e->suspect = porigin;
1364 queue_blames(sb, porigin, suspects);
1368 * We pass blame from the current commit to its parents. We keep saying
1369 * "parent" (and "porigin"), but what we mean is to find scapegoat to
1370 * exonerate ourselves.
1372 static struct commit_list *first_scapegoat(struct rev_info *revs, struct commit *commit)
1375 return commit->parents;
1376 return lookup_decoration(&revs->children, &commit->object);
1379 static int num_scapegoats(struct rev_info *revs, struct commit *commit)
1381 struct commit_list *l = first_scapegoat(revs, commit);
1382 return commit_list_count(l);
1385 /* Distribute collected unsorted blames to the respected sorted lists
1386 * in the various origins.
1388 static void distribute_blame(struct scoreboard *sb, struct blame_entry *blamed)
1390 blamed = blame_sort(blamed, compare_blame_suspect);
1393 struct origin *porigin = blamed->suspect;
1394 struct blame_entry *suspects = NULL;
1396 struct blame_entry *next = blamed->next;
1397 blamed->next = suspects;
1400 } while (blamed && blamed->suspect == porigin);
1401 suspects = reverse_blame(suspects, NULL);
1402 queue_blames(sb, porigin, suspects);
1408 static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1410 struct rev_info *revs = sb->revs;
1411 int i, pass, num_sg;
1412 struct commit *commit = origin->commit;
1413 struct commit_list *sg;
1414 struct origin *sg_buf[MAXSG];
1415 struct origin *porigin, **sg_origin = sg_buf;
1416 struct blame_entry *toosmall = NULL;
1417 struct blame_entry *blames, **blametail = &blames;
1419 num_sg = num_scapegoats(revs, commit);
1422 else if (num_sg < ARRAY_SIZE(sg_buf))
1423 memset(sg_buf, 0, sizeof(sg_buf));
1425 sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
1428 * The first pass looks for unrenamed path to optimize for
1429 * common cases, then we look for renames in the second pass.
1431 for (pass = 0; pass < 2 - no_whole_file_rename; pass++) {
1432 struct origin *(*find)(struct scoreboard *,
1433 struct commit *, struct origin *);
1434 find = pass ? find_rename : find_origin;
1436 for (i = 0, sg = first_scapegoat(revs, commit);
1438 sg = sg->next, i++) {
1439 struct commit *p = sg->item;
1444 if (parse_commit(p))
1446 porigin = find(sb, p, origin);
1449 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
1450 pass_whole_blame(sb, origin, porigin);
1451 origin_decref(porigin);
1454 for (j = same = 0; j < i; j++)
1456 !hashcmp(sg_origin[j]->blob_sha1,
1457 porigin->blob_sha1)) {
1462 sg_origin[i] = porigin;
1464 origin_decref(porigin);
1469 for (i = 0, sg = first_scapegoat(revs, commit);
1471 sg = sg->next, i++) {
1472 struct origin *porigin = sg_origin[i];
1475 if (!origin->previous) {
1476 origin_incref(porigin);
1477 origin->previous = porigin;
1479 pass_blame_to_parent(sb, origin, porigin);
1480 if (!origin->suspects)
1485 * Optionally find moves in parents' files.
1487 if (opt & PICKAXE_BLAME_MOVE) {
1488 filter_small(sb, &toosmall, &origin->suspects, blame_move_score);
1489 if (origin->suspects) {
1490 for (i = 0, sg = first_scapegoat(revs, commit);
1492 sg = sg->next, i++) {
1493 struct origin *porigin = sg_origin[i];
1496 find_move_in_parent(sb, &blametail, &toosmall, origin, porigin);
1497 if (!origin->suspects)
1504 * Optionally find copies from parents' files.
1506 if (opt & PICKAXE_BLAME_COPY) {
1507 if (blame_copy_score > blame_move_score)
1508 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1509 else if (blame_copy_score < blame_move_score) {
1510 origin->suspects = blame_merge(origin->suspects, toosmall);
1512 filter_small(sb, &toosmall, &origin->suspects, blame_copy_score);
1514 if (!origin->suspects)
1517 for (i = 0, sg = first_scapegoat(revs, commit);
1519 sg = sg->next, i++) {
1520 struct origin *porigin = sg_origin[i];
1521 find_copy_in_parent(sb, &blametail, &toosmall,
1522 origin, sg->item, porigin, opt);
1523 if (!origin->suspects)
1530 distribute_blame(sb, blames);
1532 * prepend toosmall to origin->suspects
1534 * There is no point in sorting: this ends up on a big
1535 * unsorted list in the caller anyway.
1538 struct blame_entry **tail = &toosmall;
1540 tail = &(*tail)->next;
1541 *tail = origin->suspects;
1542 origin->suspects = toosmall;
1544 for (i = 0; i < num_sg; i++) {
1546 drop_origin_blob(sg_origin[i]);
1547 origin_decref(sg_origin[i]);
1550 drop_origin_blob(origin);
1551 if (sg_buf != sg_origin)
1556 * Information on commits, used for output.
1558 struct commit_info {
1559 struct strbuf author;
1560 struct strbuf author_mail;
1561 unsigned long author_time;
1562 struct strbuf author_tz;
1564 /* filled only when asked for details */
1565 struct strbuf committer;
1566 struct strbuf committer_mail;
1567 unsigned long committer_time;
1568 struct strbuf committer_tz;
1570 struct strbuf summary;
1574 * Parse author/committer line in the commit object buffer
1576 static void get_ac_line(const char *inbuf, const char *what,
1577 struct strbuf *name, struct strbuf *mail,
1578 unsigned long *time, struct strbuf *tz)
1580 struct ident_split ident;
1581 size_t len, maillen, namelen;
1583 const char *namebuf, *mailbuf;
1585 tmp = strstr(inbuf, what);
1588 tmp += strlen(what);
1589 endp = strchr(tmp, '\n');
1595 if (split_ident_line(&ident, tmp, len)) {
1599 strbuf_addstr(name, tmp);
1600 strbuf_addstr(mail, tmp);
1601 strbuf_addstr(tz, tmp);
1606 namelen = ident.name_end - ident.name_begin;
1607 namebuf = ident.name_begin;
1609 maillen = ident.mail_end - ident.mail_begin;
1610 mailbuf = ident.mail_begin;
1612 if (ident.date_begin && ident.date_end)
1613 *time = strtoul(ident.date_begin, NULL, 10);
1617 if (ident.tz_begin && ident.tz_end)
1618 strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
1620 strbuf_addstr(tz, "(unknown)");
1623 * Now, convert both name and e-mail using mailmap
1625 map_user(&mailmap, &mailbuf, &maillen,
1626 &namebuf, &namelen);
1628 strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
1629 strbuf_add(name, namebuf, namelen);
1632 static void commit_info_init(struct commit_info *ci)
1635 strbuf_init(&ci->author, 0);
1636 strbuf_init(&ci->author_mail, 0);
1637 strbuf_init(&ci->author_tz, 0);
1638 strbuf_init(&ci->committer, 0);
1639 strbuf_init(&ci->committer_mail, 0);
1640 strbuf_init(&ci->committer_tz, 0);
1641 strbuf_init(&ci->summary, 0);
1644 static void commit_info_destroy(struct commit_info *ci)
1647 strbuf_release(&ci->author);
1648 strbuf_release(&ci->author_mail);
1649 strbuf_release(&ci->author_tz);
1650 strbuf_release(&ci->committer);
1651 strbuf_release(&ci->committer_mail);
1652 strbuf_release(&ci->committer_tz);
1653 strbuf_release(&ci->summary);
1656 static void get_commit_info(struct commit *commit,
1657 struct commit_info *ret,
1661 const char *subject, *encoding;
1662 const char *message;
1664 commit_info_init(ret);
1666 encoding = get_log_output_encoding();
1667 message = logmsg_reencode(commit, NULL, encoding);
1668 get_ac_line(message, "\nauthor ",
1669 &ret->author, &ret->author_mail,
1670 &ret->author_time, &ret->author_tz);
1673 unuse_commit_buffer(commit, message);
1677 get_ac_line(message, "\ncommitter ",
1678 &ret->committer, &ret->committer_mail,
1679 &ret->committer_time, &ret->committer_tz);
1681 len = find_commit_subject(message, &subject);
1683 strbuf_add(&ret->summary, subject, len);
1685 strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
1687 unuse_commit_buffer(commit, message);
1691 * To allow LF and other nonportable characters in pathnames,
1692 * they are c-style quoted as needed.
1694 static void write_filename_info(const char *path)
1696 printf("filename ");
1697 write_name_quoted(path, stdout, '\n');
1701 * Porcelain/Incremental format wants to show a lot of details per
1702 * commit. Instead of repeating this every line, emit it only once,
1703 * the first time each commit appears in the output (unless the
1704 * user has specifically asked for us to repeat).
1706 static int emit_one_suspect_detail(struct origin *suspect, int repeat)
1708 struct commit_info ci;
1710 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN))
1713 suspect->commit->object.flags |= METAINFO_SHOWN;
1714 get_commit_info(suspect->commit, &ci, 1);
1715 printf("author %s\n", ci.author.buf);
1716 printf("author-mail %s\n", ci.author_mail.buf);
1717 printf("author-time %lu\n", ci.author_time);
1718 printf("author-tz %s\n", ci.author_tz.buf);
1719 printf("committer %s\n", ci.committer.buf);
1720 printf("committer-mail %s\n", ci.committer_mail.buf);
1721 printf("committer-time %lu\n", ci.committer_time);
1722 printf("committer-tz %s\n", ci.committer_tz.buf);
1723 printf("summary %s\n", ci.summary.buf);
1724 if (suspect->commit->object.flags & UNINTERESTING)
1725 printf("boundary\n");
1726 if (suspect->previous) {
1727 struct origin *prev = suspect->previous;
1728 printf("previous %s ", sha1_to_hex(prev->commit->object.sha1));
1729 write_name_quoted(prev->path, stdout, '\n');
1732 commit_info_destroy(&ci);
1738 * The blame_entry is found to be guilty for the range.
1739 * Show it in incremental output.
1741 static void found_guilty_entry(struct blame_entry *ent)
1744 struct origin *suspect = ent->suspect;
1746 printf("%s %d %d %d\n",
1747 sha1_to_hex(suspect->commit->object.sha1),
1748 ent->s_lno + 1, ent->lno + 1, ent->num_lines);
1749 emit_one_suspect_detail(suspect, 0);
1750 write_filename_info(suspect->path);
1751 maybe_flush_or_die(stdout, "stdout");
1756 * The main loop -- while we have blobs with lines whose true origin
1757 * is still unknown, pick one blob, and allow its lines to pass blames
1758 * to its parents. */
1759 static void assign_blame(struct scoreboard *sb, int opt)
1761 struct rev_info *revs = sb->revs;
1762 struct commit *commit = prio_queue_get(&sb->commits);
1765 struct blame_entry *ent;
1766 struct origin *suspect = commit->util;
1768 /* find one suspect to break down */
1769 while (suspect && !suspect->suspects)
1770 suspect = suspect->next;
1773 commit = prio_queue_get(&sb->commits);
1777 assert(commit == suspect->commit);
1780 * We will use this suspect later in the loop,
1781 * so hold onto it in the meantime.
1783 origin_incref(suspect);
1784 parse_commit(commit);
1786 (!(commit->object.flags & UNINTERESTING) &&
1787 !(revs->max_age != -1 && commit->date < revs->max_age)))
1788 pass_blame(sb, suspect, opt);
1790 commit->object.flags |= UNINTERESTING;
1791 if (commit->object.parsed)
1792 mark_parents_uninteresting(commit);
1794 /* treat root commit as boundary */
1795 if (!commit->parents && !show_root)
1796 commit->object.flags |= UNINTERESTING;
1798 /* Take responsibility for the remaining entries */
1799 ent = suspect->suspects;
1801 suspect->guilty = 1;
1803 struct blame_entry *next = ent->next;
1804 found_guilty_entry(ent);
1809 ent->next = sb->ent;
1810 sb->ent = suspect->suspects;
1811 suspect->suspects = NULL;
1815 origin_decref(suspect);
1817 if (DEBUG) /* sanity */
1818 sanity_check_refcnt(sb);
1822 static const char *format_time(unsigned long time, const char *tz_str,
1825 static struct strbuf time_buf = STRBUF_INIT;
1827 strbuf_reset(&time_buf);
1828 if (show_raw_time) {
1829 strbuf_addf(&time_buf, "%lu %s", time, tz_str);
1832 const char *time_str;
1836 time_str = show_date(time, tz, &blame_date_mode);
1837 strbuf_addstr(&time_buf, time_str);
1839 * Add space paddings to time_buf to display a fixed width
1840 * string, and use time_width for display width calibration.
1842 for (time_width = utf8_strwidth(time_str);
1843 time_width < blame_date_width;
1845 strbuf_addch(&time_buf, ' ');
1847 return time_buf.buf;
1850 #define OUTPUT_ANNOTATE_COMPAT 001
1851 #define OUTPUT_LONG_OBJECT_NAME 002
1852 #define OUTPUT_RAW_TIMESTAMP 004
1853 #define OUTPUT_PORCELAIN 010
1854 #define OUTPUT_SHOW_NAME 020
1855 #define OUTPUT_SHOW_NUMBER 040
1856 #define OUTPUT_SHOW_SCORE 0100
1857 #define OUTPUT_NO_AUTHOR 0200
1858 #define OUTPUT_SHOW_EMAIL 0400
1859 #define OUTPUT_LINE_PORCELAIN 01000
1861 static void emit_porcelain_details(struct origin *suspect, int repeat)
1863 if (emit_one_suspect_detail(suspect, repeat) ||
1864 (suspect->commit->object.flags & MORE_THAN_ONE_PATH))
1865 write_filename_info(suspect->path);
1868 static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent,
1871 int repeat = opt & OUTPUT_LINE_PORCELAIN;
1874 struct origin *suspect = ent->suspect;
1877 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1878 printf("%s %d %d %d\n",
1883 emit_porcelain_details(suspect, repeat);
1885 cp = nth_line(sb, ent->lno);
1886 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1889 printf("%s %d %d\n", hex,
1890 ent->s_lno + 1 + cnt,
1891 ent->lno + 1 + cnt);
1893 emit_porcelain_details(suspect, 1);
1899 } while (ch != '\n' &&
1900 cp < sb->final_buf + sb->final_buf_size);
1903 if (sb->final_buf_size && cp[-1] != '\n')
1907 static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1911 struct origin *suspect = ent->suspect;
1912 struct commit_info ci;
1914 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
1916 get_commit_info(suspect->commit, &ci, 1);
1917 strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));
1919 cp = nth_line(sb, ent->lno);
1920 for (cnt = 0; cnt < ent->num_lines; cnt++) {
1922 int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev;
1924 if (suspect->commit->object.flags & UNINTERESTING) {
1926 memset(hex, ' ', length);
1927 else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
1933 printf("%.*s", length, hex);
1934 if (opt & OUTPUT_ANNOTATE_COMPAT) {
1936 if (opt & OUTPUT_SHOW_EMAIL)
1937 name = ci.author_mail.buf;
1939 name = ci.author.buf;
1940 printf("\t(%10s\t%10s\t%d)", name,
1941 format_time(ci.author_time, ci.author_tz.buf,
1943 ent->lno + 1 + cnt);
1945 if (opt & OUTPUT_SHOW_SCORE)
1947 max_score_digits, ent->score,
1948 ent->suspect->refcnt);
1949 if (opt & OUTPUT_SHOW_NAME)
1950 printf(" %-*.*s", longest_file, longest_file,
1952 if (opt & OUTPUT_SHOW_NUMBER)
1953 printf(" %*d", max_orig_digits,
1954 ent->s_lno + 1 + cnt);
1956 if (!(opt & OUTPUT_NO_AUTHOR)) {
1959 if (opt & OUTPUT_SHOW_EMAIL)
1960 name = ci.author_mail.buf;
1962 name = ci.author.buf;
1963 pad = longest_author - utf8_strwidth(name);
1964 printf(" (%s%*s %10s",
1966 format_time(ci.author_time,
1971 max_digits, ent->lno + 1 + cnt);
1976 } while (ch != '\n' &&
1977 cp < sb->final_buf + sb->final_buf_size);
1980 if (sb->final_buf_size && cp[-1] != '\n')
1983 commit_info_destroy(&ci);
1986 static void output(struct scoreboard *sb, int option)
1988 struct blame_entry *ent;
1990 if (option & OUTPUT_PORCELAIN) {
1991 for (ent = sb->ent; ent; ent = ent->next) {
1993 struct origin *suspect;
1994 struct commit *commit = ent->suspect->commit;
1995 if (commit->object.flags & MORE_THAN_ONE_PATH)
1997 for (suspect = commit->util; suspect; suspect = suspect->next) {
1998 if (suspect->guilty && count++) {
1999 commit->object.flags |= MORE_THAN_ONE_PATH;
2006 for (ent = sb->ent; ent; ent = ent->next) {
2007 if (option & OUTPUT_PORCELAIN)
2008 emit_porcelain(sb, ent, option);
2010 emit_other(sb, ent, option);
2015 static const char *get_next_line(const char *start, const char *end)
2017 const char *nl = memchr(start, '\n', end - start);
2018 return nl ? nl + 1 : end;
2022 * To allow quick access to the contents of nth line in the
2023 * final image, prepare an index in the scoreboard.
2025 static int prepare_lines(struct scoreboard *sb)
2027 const char *buf = sb->final_buf;
2028 unsigned long len = sb->final_buf_size;
2029 const char *end = buf + len;
2034 for (p = buf; p < end; p = get_next_line(p, end))
2037 sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
2039 for (p = buf; p < end; p = get_next_line(p, end))
2040 *lineno++ = p - buf;
2044 sb->num_lines = num;
2045 return sb->num_lines;
2049 * Add phony grafts for use with -S; this is primarily to
2050 * support git's cvsserver that wants to give a linear history
2053 static int read_ancestry(const char *graft_file)
2055 FILE *fp = fopen(graft_file, "r");
2056 struct strbuf buf = STRBUF_INIT;
2059 while (!strbuf_getwholeline(&buf, fp, '\n')) {
2060 /* The format is just "Commit Parent1 Parent2 ...\n" */
2061 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
2063 register_commit_graft(graft, 0);
2066 strbuf_release(&buf);
2070 static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
2072 const char *uniq = find_unique_abbrev(suspect->commit->object.sha1,
2074 int len = strlen(uniq);
2075 if (auto_abbrev < len)
2081 * How many columns do we need to show line numbers, authors,
2084 static void find_alignment(struct scoreboard *sb, int *option)
2086 int longest_src_lines = 0;
2087 int longest_dst_lines = 0;
2088 unsigned largest_score = 0;
2089 struct blame_entry *e;
2090 int compute_auto_abbrev = (abbrev < 0);
2091 int auto_abbrev = default_abbrev;
2093 for (e = sb->ent; e; e = e->next) {
2094 struct origin *suspect = e->suspect;
2097 if (compute_auto_abbrev)
2098 auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
2099 if (strcmp(suspect->path, sb->path))
2100 *option |= OUTPUT_SHOW_NAME;
2101 num = strlen(suspect->path);
2102 if (longest_file < num)
2104 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
2105 struct commit_info ci;
2106 suspect->commit->object.flags |= METAINFO_SHOWN;
2107 get_commit_info(suspect->commit, &ci, 1);
2108 if (*option & OUTPUT_SHOW_EMAIL)
2109 num = utf8_strwidth(ci.author_mail.buf);
2111 num = utf8_strwidth(ci.author.buf);
2112 if (longest_author < num)
2113 longest_author = num;
2114 commit_info_destroy(&ci);
2116 num = e->s_lno + e->num_lines;
2117 if (longest_src_lines < num)
2118 longest_src_lines = num;
2119 num = e->lno + e->num_lines;
2120 if (longest_dst_lines < num)
2121 longest_dst_lines = num;
2122 if (largest_score < ent_score(sb, e))
2123 largest_score = ent_score(sb, e);
2125 max_orig_digits = decimal_width(longest_src_lines);
2126 max_digits = decimal_width(longest_dst_lines);
2127 max_score_digits = decimal_width(largest_score);
2129 if (compute_auto_abbrev)
2130 /* one more abbrev length is needed for the boundary commit */
2131 abbrev = auto_abbrev + 1;
2135 * For debugging -- origin is refcounted, and this asserts that
2136 * we do not underflow.
2138 static void sanity_check_refcnt(struct scoreboard *sb)
2141 struct blame_entry *ent;
2143 for (ent = sb->ent; ent; ent = ent->next) {
2144 /* Nobody should have zero or negative refcnt */
2145 if (ent->suspect->refcnt <= 0) {
2146 fprintf(stderr, "%s in %s has negative refcnt %d\n",
2148 sha1_to_hex(ent->suspect->commit->object.sha1),
2149 ent->suspect->refcnt);
2155 find_alignment(sb, &opt);
2157 die("Baa %d!", baa);
2161 static unsigned parse_score(const char *arg)
2164 unsigned long score = strtoul(arg, &end, 10);
2170 static const char *add_prefix(const char *prefix, const char *path)
2172 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
2175 static int git_blame_config(const char *var, const char *value, void *cb)
2177 if (!strcmp(var, "blame.showroot")) {
2178 show_root = git_config_bool(var, value);
2181 if (!strcmp(var, "blame.blankboundary")) {
2182 blank_boundary = git_config_bool(var, value);
2185 if (!strcmp(var, "blame.showemail")) {
2186 int *output_option = cb;
2187 if (git_config_bool(var, value))
2188 *output_option |= OUTPUT_SHOW_EMAIL;
2190 *output_option &= ~OUTPUT_SHOW_EMAIL;
2193 if (!strcmp(var, "blame.date")) {
2195 return config_error_nonbool(var);
2196 parse_date_format(value, &blame_date_mode);
2200 if (userdiff_config(var, value) < 0)
2203 return git_default_config(var, value, cb);
2206 static void verify_working_tree_path(struct commit *work_tree, const char *path)
2208 struct commit_list *parents;
2210 for (parents = work_tree->parents; parents; parents = parents->next) {
2211 const unsigned char *commit_sha1 = parents->item->object.sha1;
2212 unsigned char blob_sha1[20];
2215 if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) &&
2216 sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
2219 die("no such path '%s' in HEAD", path);
2222 static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
2224 struct commit *parent;
2226 parent = lookup_commit_reference(sha1);
2228 die("no such commit %s", sha1_to_hex(sha1));
2229 return &commit_list_insert(parent, tail)->next;
2232 static void append_merge_parents(struct commit_list **tail)
2235 struct strbuf line = STRBUF_INIT;
2237 merge_head = open(git_path_merge_head(), O_RDONLY);
2238 if (merge_head < 0) {
2239 if (errno == ENOENT)
2241 die("cannot open '%s' for reading", git_path_merge_head());
2244 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
2245 unsigned char sha1[20];
2246 if (line.len < 40 || get_sha1_hex(line.buf, sha1))
2247 die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
2248 tail = append_parent(tail, sha1);
2251 strbuf_release(&line);
2255 * This isn't as simple as passing sb->buf and sb->len, because we
2256 * want to transfer ownership of the buffer to the commit (so we
2259 static void set_commit_buffer_from_strbuf(struct commit *c, struct strbuf *sb)
2262 void *buf = strbuf_detach(sb, &len);
2263 set_commit_buffer(c, buf, len);
2267 * Prepare a dummy commit that represents the work tree (or staged) item.
2268 * Note that annotating work tree item never works in the reverse.
2270 static struct commit *fake_working_tree_commit(struct diff_options *opt,
2272 const char *contents_from)
2274 struct commit *commit;
2275 struct origin *origin;
2276 struct commit_list **parent_tail, *parent;
2277 unsigned char head_sha1[20];
2278 struct strbuf buf = STRBUF_INIT;
2282 struct cache_entry *ce;
2284 struct strbuf msg = STRBUF_INIT;
2287 commit = alloc_commit_node();
2288 commit->object.parsed = 1;
2290 parent_tail = &commit->parents;
2292 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
2293 die("no such ref: HEAD");
2295 parent_tail = append_parent(parent_tail, head_sha1);
2296 append_merge_parents(parent_tail);
2297 verify_working_tree_path(commit, path);
2299 origin = make_origin(commit, path);
2301 ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2302 strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n");
2303 for (parent = commit->parents; parent; parent = parent->next)
2304 strbuf_addf(&msg, "parent %s\n",
2305 sha1_to_hex(parent->item->object.sha1));
2309 "Version of %s from %s\n",
2311 (!contents_from ? path :
2312 (!strcmp(contents_from, "-") ? "standard input" : contents_from)));
2313 set_commit_buffer_from_strbuf(commit, &msg);
2315 if (!contents_from || strcmp("-", contents_from)) {
2317 const char *read_from;
2319 unsigned long buf_len;
2321 if (contents_from) {
2322 if (stat(contents_from, &st) < 0)
2323 die_errno("Cannot stat '%s'", contents_from);
2324 read_from = contents_from;
2327 if (lstat(path, &st) < 0)
2328 die_errno("Cannot lstat '%s'", path);
2331 mode = canon_mode(st.st_mode);
2333 switch (st.st_mode & S_IFMT) {
2335 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
2336 textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len))
2337 strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
2338 else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
2339 die_errno("cannot open or read '%s'", read_from);
2342 if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
2343 die_errno("cannot readlink '%s'", read_from);
2346 die("unsupported file type %s", read_from);
2350 /* Reading from stdin */
2352 if (strbuf_read(&buf, 0, 0) < 0)
2353 die_errno("failed to read from stdin");
2355 convert_to_git(path, buf.buf, buf.len, &buf, 0);
2356 origin->file.ptr = buf.buf;
2357 origin->file.size = buf.len;
2358 pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
2361 * Read the current index, replace the path entry with
2362 * origin->blob_sha1 without mucking with its mode or type
2363 * bits; we are not going to write this index out -- we just
2364 * want to run "diff-index --cached".
2371 int pos = cache_name_pos(path, len);
2373 mode = active_cache[pos]->ce_mode;
2375 /* Let's not bother reading from HEAD tree */
2376 mode = S_IFREG | 0644;
2378 size = cache_entry_size(len);
2379 ce = xcalloc(1, size);
2380 hashcpy(ce->sha1, origin->blob_sha1);
2381 memcpy(ce->name, path, len);
2382 ce->ce_flags = create_ce_flags(0);
2383 ce->ce_namelen = len;
2384 ce->ce_mode = create_ce_mode(mode);
2385 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2388 * We are not going to write this out, so this does not matter
2389 * right now, but someday we might optimize diff-index --cached
2390 * with cache-tree information.
2392 cache_tree_invalidate_path(&the_index, path);
2397 static char *prepare_final(struct scoreboard *sb)
2400 const char *final_commit_name = NULL;
2401 struct rev_info *revs = sb->revs;
2404 * There must be one and only one positive commit in the
2405 * revs->pending array.
2407 for (i = 0; i < revs->pending.nr; i++) {
2408 struct object *obj = revs->pending.objects[i].item;
2409 if (obj->flags & UNINTERESTING)
2411 while (obj->type == OBJ_TAG)
2412 obj = deref_tag(obj, NULL, 0);
2413 if (obj->type != OBJ_COMMIT)
2414 die("Non commit %s?", revs->pending.objects[i].name);
2416 die("More than one commit to dig from %s and %s?",
2417 revs->pending.objects[i].name,
2419 sb->final = (struct commit *) obj;
2420 final_commit_name = revs->pending.objects[i].name;
2422 return xstrdup_or_null(final_commit_name);
2425 static char *prepare_initial(struct scoreboard *sb)
2428 const char *final_commit_name = NULL;
2429 struct rev_info *revs = sb->revs;
2432 * There must be one and only one negative commit, and it must be
2435 for (i = 0; i < revs->pending.nr; i++) {
2436 struct object *obj = revs->pending.objects[i].item;
2437 if (!(obj->flags & UNINTERESTING))
2439 while (obj->type == OBJ_TAG)
2440 obj = deref_tag(obj, NULL, 0);
2441 if (obj->type != OBJ_COMMIT)
2442 die("Non commit %s?", revs->pending.objects[i].name);
2444 die("More than one commit to dig down to %s and %s?",
2445 revs->pending.objects[i].name,
2447 sb->final = (struct commit *) obj;
2448 final_commit_name = revs->pending.objects[i].name;
2450 if (!final_commit_name)
2451 die("No commit to dig down to?");
2452 return xstrdup(final_commit_name);
2455 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
2457 int *opt = option->value;
2460 * -C enables copy from removed files;
2461 * -C -C enables copy from existing files, but only
2462 * when blaming a new file;
2463 * -C -C -C enables copy from existing files for
2466 if (*opt & PICKAXE_BLAME_COPY_HARDER)
2467 *opt |= PICKAXE_BLAME_COPY_HARDEST;
2468 if (*opt & PICKAXE_BLAME_COPY)
2469 *opt |= PICKAXE_BLAME_COPY_HARDER;
2470 *opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
2473 blame_copy_score = parse_score(arg);
2477 static int blame_move_callback(const struct option *option, const char *arg, int unset)
2479 int *opt = option->value;
2481 *opt |= PICKAXE_BLAME_MOVE;
2484 blame_move_score = parse_score(arg);
2488 int cmd_blame(int argc, const char **argv, const char *prefix)
2490 struct rev_info revs;
2492 struct scoreboard sb;
2494 struct blame_entry *ent = NULL;
2495 long dashdash_pos, lno;
2496 char *final_commit_name = NULL;
2497 enum object_type type;
2499 static struct string_list range_list;
2500 static int output_option = 0, opt = 0;
2501 static int show_stats = 0;
2502 static const char *revs_file = NULL;
2503 static const char *contents_from = NULL;
2504 static const struct option options[] = {
2505 OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
2506 OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
2507 OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
2508 OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
2509 OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
2510 OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
2511 OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
2512 OPT_BIT('p', "porcelain", &output_option, N_("Show in a format designed for machine consumption"), OUTPUT_PORCELAIN),
2513 OPT_BIT(0, "line-porcelain", &output_option, N_("Show porcelain format with per-line commit information"), OUTPUT_PORCELAIN|OUTPUT_LINE_PORCELAIN),
2514 OPT_BIT('c', NULL, &output_option, N_("Use the same output mode as git-annotate (Default: off)"), OUTPUT_ANNOTATE_COMPAT),
2515 OPT_BIT('t', NULL, &output_option, N_("Show raw timestamp (Default: off)"), OUTPUT_RAW_TIMESTAMP),
2516 OPT_BIT('l', NULL, &output_option, N_("Show long commit SHA1 (Default: off)"), OUTPUT_LONG_OBJECT_NAME),
2517 OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
2518 OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
2519 OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
2520 OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
2521 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
2522 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
2523 { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
2524 { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
2525 OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2526 OPT__ABBREV(&abbrev),
2530 struct parse_opt_ctx_t ctx;
2531 int cmd_is_annotate = !strcmp(argv[0], "annotate");
2532 struct range_set ranges;
2533 unsigned int range_i;
2536 git_config(git_blame_config, &output_option);
2537 init_revisions(&revs, NULL);
2538 revs.date_mode = blame_date_mode;
2539 DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
2540 DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
2542 save_commit_buffer = 0;
2545 parse_options_start(&ctx, argc, argv, prefix, options,
2546 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
2548 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
2549 case PARSE_OPT_HELP:
2551 case PARSE_OPT_DONE:
2553 dashdash_pos = ctx.cpidx;
2557 if (!strcmp(ctx.argv[0], "--reverse")) {
2558 ctx.argv[0] = "--children";
2561 parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
2564 no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
2565 DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
2566 argc = parse_options_end(&ctx);
2569 /* one more abbrev length is needed for the boundary commit */
2572 if (revs_file && read_ancestry(revs_file))
2573 die_errno("reading graft file '%s' failed", revs_file);
2575 if (cmd_is_annotate) {
2576 output_option |= OUTPUT_ANNOTATE_COMPAT;
2577 blame_date_mode.type = DATE_ISO8601;
2579 blame_date_mode = revs.date_mode;
2582 /* The maximum width used to show the dates */
2583 switch (blame_date_mode.type) {
2585 blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2587 case DATE_ISO8601_STRICT:
2588 blame_date_width = sizeof("2006-10-19T16:00:04-07:00");
2591 blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2594 blame_date_width = sizeof("1161298804 -0700");
2597 blame_date_width = sizeof("2006-10-19");
2600 /* TRANSLATORS: This string is used to tell us the maximum
2601 display width for a relative timestamp in "git blame"
2602 output. For C locale, "4 years, 11 months ago", which
2603 takes 22 places, is the longest among various forms of
2604 relative timestamps, but your language may need more or
2605 fewer display columns. */
2606 blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2610 blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2613 blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2616 blame_date_width -= 1; /* strip the null */
2618 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
2619 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
2620 PICKAXE_BLAME_COPY_HARDER);
2622 if (!blame_move_score)
2623 blame_move_score = BLAME_DEFAULT_MOVE_SCORE;
2624 if (!blame_copy_score)
2625 blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
2628 * We have collected options unknown to us in argv[1..unk]
2629 * which are to be passed to revision machinery if we are
2630 * going to do the "bottom" processing.
2632 * The remaining are:
2634 * (1) if dashdash_pos != 0, it is either
2635 * "blame [revisions] -- <path>" or
2636 * "blame -- <path> <rev>"
2638 * (2) otherwise, it is one of the two:
2639 * "blame [revisions] <path>"
2640 * "blame <path> <rev>"
2642 * Note that we must strip out <path> from the arguments: we do not
2643 * want the path pruning but we may want "bottom" processing.
2646 switch (argc - dashdash_pos - 1) {
2649 usage_with_options(blame_opt_usage, options);
2650 /* reorder for the new way: <rev> -- <path> */
2656 path = add_prefix(prefix, argv[--argc]);
2660 usage_with_options(blame_opt_usage, options);
2664 usage_with_options(blame_opt_usage, options);
2665 path = add_prefix(prefix, argv[argc - 1]);
2666 if (argc == 3 && !file_exists(path)) { /* (2b) */
2667 path = add_prefix(prefix, argv[1]);
2670 argv[argc - 1] = "--";
2673 if (!file_exists(path))
2674 die_errno("cannot stat path '%s'", path);
2677 revs.disable_stdin = 1;
2678 setup_revisions(argc, argv, &revs, NULL);
2679 memset(&sb, 0, sizeof(sb));
2683 final_commit_name = prepare_final(&sb);
2684 sb.commits.compare = compare_commits_by_commit_date;
2686 else if (contents_from)
2687 die("--contents and --children do not blend well.");
2689 final_commit_name = prepare_initial(&sb);
2690 sb.commits.compare = compare_commits_by_reverse_commit_date;
2695 * "--not A B -- path" without anything positive;
2696 * do not default to HEAD, but use the working tree
2700 sb.final = fake_working_tree_commit(&sb.revs->diffopt,
2701 path, contents_from);
2702 add_pending_object(&revs, &(sb.final->object), ":");
2704 else if (contents_from)
2705 die("Cannot use --contents with final commit object name");
2708 * If we have bottom, this will mark the ancestors of the
2709 * bottom commits we would reach while traversing as
2712 if (prepare_revision_walk(&revs))
2713 die(_("revision walk setup failed"));
2715 if (is_null_sha1(sb.final->object.sha1)) {
2717 sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
2718 sb.final_buf_size = o->file.size;
2721 o = get_origin(&sb, sb.final, path);
2722 if (fill_blob_sha1_and_mode(o))
2723 die("no such path %s in %s", path, final_commit_name);
2725 if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
2726 textconv_object(path, o->mode, o->blob_sha1, 1, (char **) &sb.final_buf,
2727 &sb.final_buf_size))
2730 sb.final_buf = read_sha1_file(o->blob_sha1, &type,
2731 &sb.final_buf_size);
2734 die("Cannot read blob %s for path %s",
2735 sha1_to_hex(o->blob_sha1),
2739 lno = prepare_lines(&sb);
2741 if (lno && !range_list.nr)
2742 string_list_append(&range_list, xstrdup("1"));
2745 range_set_init(&ranges, range_list.nr);
2746 for (range_i = 0; range_i < range_list.nr; ++range_i) {
2748 if (parse_range_arg(range_list.items[range_i].string,
2749 nth_line_cb, &sb, lno, anchor,
2750 &bottom, &top, sb.path))
2752 if (lno < top || ((lno || bottom) && lno < bottom))
2753 die("file %s has only %lu lines", path, lno);
2759 range_set_append_unsafe(&ranges, bottom, top);
2762 sort_and_merge_range_set(&ranges);
2764 for (range_i = ranges.nr; range_i > 0; --range_i) {
2765 const struct range *r = &ranges.ranges[range_i - 1];
2766 long bottom = r->start;
2768 struct blame_entry *next = ent;
2769 ent = xcalloc(1, sizeof(*ent));
2771 ent->num_lines = top - bottom;
2773 ent->s_lno = bottom;
2779 prio_queue_put(&sb.commits, o->commit);
2783 range_set_release(&ranges);
2784 string_list_clear(&range_list, 0);
2789 read_mailmap(&mailmap, NULL);
2794 assign_blame(&sb, opt);
2796 free(final_commit_name);
2801 sb.ent = blame_sort(sb.ent, compare_blame_final);
2805 if (!(output_option & OUTPUT_PORCELAIN))
2806 find_alignment(&sb, &output_option);
2808 output(&sb, output_option);
2809 free((void *)sb.final_buf);
2810 for (ent = sb.ent; ent; ) {
2811 struct blame_entry *e = ent->next;
2817 printf("num read blob: %d\n", num_read_blob);
2818 printf("num get patch: %d\n", num_get_patch);
2819 printf("num commits: %d\n", num_commits);