4 * Copyright (c) 2006, Junio C Hamano
12 #include "tree-walk.h"
17 #include "xdiff-interface.h"
19 static char blame_usage
[] =
20 "git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
21 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
22 " -b Show blank SHA-1 for boundary commits (Default: off)\n"
23 " -l, --long Show long commit SHA1 (Default: off)\n"
24 " --root Do not treat root commits as boundaries (Default: off)\n"
25 " -t, --time Show raw timestamp (Default: off)\n"
26 " -f, --show-name Show original filename (Default: auto)\n"
27 " -n, --show-number Show original linenumber (Default: off)\n"
28 " -p, --porcelain Show in a format designed for machine consumption\n"
29 " -L n,m Process only line range n,m, counting from 1\n"
30 " -M, -C Find line movements within and across files\n"
31 " --incremental Show blame entries as we find them, incrementally\n"
32 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
34 static int longest_file
;
35 static int longest_author
;
36 static int max_orig_digits
;
37 static int max_digits
;
38 static int max_score_digits
;
40 static int blank_boundary
;
41 static int incremental
;
48 static int num_read_blob
;
49 static int num_get_patch
;
50 static int num_commits
;
52 #define PICKAXE_BLAME_MOVE 01
53 #define PICKAXE_BLAME_COPY 02
54 #define PICKAXE_BLAME_COPY_HARDER 04
57 * blame for a blame_entry with score lower than these thresholds
58 * is not passed to the parent using move/copy logic.
60 static unsigned blame_move_score
;
61 static unsigned blame_copy_score
;
62 #define BLAME_DEFAULT_MOVE_SCORE 20
63 #define BLAME_DEFAULT_COPY_SCORE 40
65 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
66 #define METAINFO_SHOWN (1u<<12)
67 #define MORE_THAN_ONE_PATH (1u<<13)
70 * One blob in a commit that is being suspected
74 struct commit
*commit
;
76 unsigned char blob_sha1
[20];
77 char path
[FLEX_ARRAY
];
81 * Given an origin, prepare mmfile_t structure to be used by the
84 static char *fill_origin_blob(struct origin
*o
, mmfile_t
*file
)
89 file
->ptr
= read_sha1_file(o
->blob_sha1
, type
,
90 (unsigned long *)(&(file
->size
)));
99 * Origin is refcounted and usually we keep the blob contents to be
102 static inline struct origin
*origin_incref(struct origin
*o
)
109 static void origin_decref(struct origin
*o
)
111 if (o
&& --o
->refcnt
<= 0) {
114 memset(o
, 0, sizeof(*o
));
120 * Each group of lines is described by a blame_entry; it can be split
121 * as we pass blame to the parents. They form a linked list in the
122 * scoreboard structure, sorted by the target line number.
125 struct blame_entry
*prev
;
126 struct blame_entry
*next
;
128 /* the first line of this group in the final image;
129 * internally all line numbers are 0 based.
133 /* how many lines this group has */
136 /* the commit that introduced this group into the final image */
137 struct origin
*suspect
;
139 /* true if the suspect is truly guilty; false while we have not
140 * checked if the group came from one of its parents.
144 /* the line number of the first line of this group in the
145 * suspect's file; internally all line numbers are 0 based.
149 /* how significant this entry is -- cached to avoid
150 * scanning the lines over and over.
156 * The current state of the blame assignment.
159 /* the final commit (i.e. where we started digging from) */
160 struct commit
*final
;
165 * The contents in the final image.
166 * Used by many functions to obtain contents of the nth line,
167 * indexed with scoreboard.lineno[blame_entry.lno].
169 const char *final_buf
;
170 unsigned long final_buf_size
;
172 /* linked list of blames */
173 struct blame_entry
*ent
;
175 /* look-up a line in the final buffer */
180 static int cmp_suspect(struct origin
*a
, struct origin
*b
)
182 int cmp
= hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
185 return strcmp(a
->path
, b
->path
);
188 #define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
190 static void sanity_check_refcnt(struct scoreboard
*);
193 * If two blame entries that are next to each other came from
194 * contiguous lines in the same origin (i.e. <commit, path> pair),
195 * merge them together.
197 static void coalesce(struct scoreboard
*sb
)
199 struct blame_entry
*ent
, *next
;
201 for (ent
= sb
->ent
; ent
&& (next
= ent
->next
); ent
= next
) {
202 if (!cmp_suspect(ent
->suspect
, next
->suspect
) &&
203 ent
->guilty
== next
->guilty
&&
204 ent
->s_lno
+ ent
->num_lines
== next
->s_lno
) {
205 ent
->num_lines
+= next
->num_lines
;
206 ent
->next
= next
->next
;
208 ent
->next
->prev
= ent
;
209 origin_decref(next
->suspect
);
212 next
= ent
; /* again */
216 if (DEBUG
) /* sanity */
217 sanity_check_refcnt(sb
);
221 * Given a commit and a path in it, create a new origin structure.
222 * The callers that add blame to the scoreboard should use
223 * get_origin() to obtain shared, refcounted copy instead of calling
224 * this function directly.
226 static struct origin
*make_origin(struct commit
*commit
, const char *path
)
229 o
= xcalloc(1, sizeof(*o
) + strlen(path
) + 1);
232 strcpy(o
->path
, path
);
237 * Locate an existing origin or create a new one.
239 static struct origin
*get_origin(struct scoreboard
*sb
,
240 struct commit
*commit
,
243 struct blame_entry
*e
;
245 for (e
= sb
->ent
; e
; e
= e
->next
) {
246 if (e
->suspect
->commit
== commit
&&
247 !strcmp(e
->suspect
->path
, path
))
248 return origin_incref(e
->suspect
);
250 return make_origin(commit
, path
);
254 * Fill the blob_sha1 field of an origin if it hasn't, so that later
255 * call to fill_origin_blob() can use it to locate the data. blob_sha1
256 * for an origin is also used to pass the blame for the entire file to
257 * the parent to detect the case where a child's blob is identical to
258 * that of its parent's.
260 static int fill_blob_sha1(struct origin
*origin
)
265 if (!is_null_sha1(origin
->blob_sha1
))
267 if (get_tree_entry(origin
->commit
->object
.sha1
,
269 origin
->blob_sha1
, &mode
))
271 if (sha1_object_info(origin
->blob_sha1
, type
, NULL
) ||
272 strcmp(type
, blob_type
))
276 hashclr(origin
->blob_sha1
);
281 * We have an origin -- check if the same path exists in the
282 * parent and return an origin structure to represent it.
284 static struct origin
*find_origin(struct scoreboard
*sb
,
285 struct commit
*parent
,
286 struct origin
*origin
)
288 struct origin
*porigin
= NULL
;
289 struct diff_options diff_opts
;
290 const char *paths
[2];
294 * Each commit object can cache one origin in that
295 * commit. This is a freestanding copy of origin and
298 struct origin
*cached
= parent
->util
;
299 if (!strcmp(cached
->path
, origin
->path
)) {
301 * The same path between origin and its parent
302 * without renaming -- the most common case.
304 porigin
= get_origin(sb
, parent
, cached
->path
);
307 * If the origin was newly created (i.e. get_origin
308 * would call make_origin if none is found in the
309 * scoreboard), it does not know the blob_sha1,
310 * so copy it. Otherwise porigin was in the
311 * scoreboard and already knows blob_sha1.
313 if (porigin
->refcnt
== 1)
314 hashcpy(porigin
->blob_sha1
, cached
->blob_sha1
);
317 /* otherwise it was not very useful; free it */
322 /* See if the origin->path is different between parent
323 * and origin first. Most of the time they are the
324 * same and diff-tree is fairly efficient about this.
326 diff_setup(&diff_opts
);
327 diff_opts
.recursive
= 1;
328 diff_opts
.detect_rename
= 0;
329 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
330 paths
[0] = origin
->path
;
333 diff_tree_setup_paths(paths
, &diff_opts
);
334 if (diff_setup_done(&diff_opts
) < 0)
336 diff_tree_sha1(parent
->tree
->object
.sha1
,
337 origin
->commit
->tree
->object
.sha1
,
339 diffcore_std(&diff_opts
);
341 /* It is either one entry that says "modified", or "created",
344 if (!diff_queued_diff
.nr
) {
345 /* The path is the same as parent */
346 porigin
= get_origin(sb
, parent
, origin
->path
);
347 hashcpy(porigin
->blob_sha1
, origin
->blob_sha1
);
349 else if (diff_queued_diff
.nr
!= 1)
350 die("internal error in blame::find_origin");
352 struct diff_filepair
*p
= diff_queued_diff
.queue
[0];
355 die("internal error in blame::find_origin (%c)",
358 porigin
= get_origin(sb
, parent
, origin
->path
);
359 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
363 /* Did not exist in parent, or type changed */
367 diff_flush(&diff_opts
);
370 * Create a freestanding copy that is not part of
371 * the refcounted origin found in the scoreboard, and
372 * cache it in the commit.
374 struct origin
*cached
;
376 cached
= make_origin(porigin
->commit
, porigin
->path
);
377 hashcpy(cached
->blob_sha1
, porigin
->blob_sha1
);
378 parent
->util
= cached
;
384 * We have an origin -- find the path that corresponds to it in its
385 * parent and return an origin structure to represent it.
387 static struct origin
*find_rename(struct scoreboard
*sb
,
388 struct commit
*parent
,
389 struct origin
*origin
)
391 struct origin
*porigin
= NULL
;
392 struct diff_options diff_opts
;
394 const char *paths
[2];
396 diff_setup(&diff_opts
);
397 diff_opts
.recursive
= 1;
398 diff_opts
.detect_rename
= DIFF_DETECT_RENAME
;
399 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
400 diff_opts
.single_follow
= origin
->path
;
402 diff_tree_setup_paths(paths
, &diff_opts
);
403 if (diff_setup_done(&diff_opts
) < 0)
405 diff_tree_sha1(parent
->tree
->object
.sha1
,
406 origin
->commit
->tree
->object
.sha1
,
408 diffcore_std(&diff_opts
);
410 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
411 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
412 if ((p
->status
== 'R' || p
->status
== 'C') &&
413 !strcmp(p
->two
->path
, origin
->path
)) {
414 porigin
= get_origin(sb
, parent
, p
->one
->path
);
415 hashcpy(porigin
->blob_sha1
, p
->one
->sha1
);
419 diff_flush(&diff_opts
);
424 * Parsing of patch chunks...
427 /* line number in postimage; up to but not including this
428 * line is the same as preimage
432 /* preimage line number after this chunk */
435 /* postimage line number after this chunk */
440 struct chunk
*chunks
;
444 struct blame_diff_state
{
445 struct xdiff_emit_state xm
;
447 unsigned hunk_post_context
;
448 unsigned hunk_in_pre_context
: 1;
451 static void process_u_diff(void *state_
, char *line
, unsigned long len
)
453 struct blame_diff_state
*state
= state_
;
455 int off1
, off2
, len1
, len2
, num
;
457 num
= state
->ret
->num
;
458 if (len
< 4 || line
[0] != '@' || line
[1] != '@') {
459 if (state
->hunk_in_pre_context
&& line
[0] == ' ')
460 state
->ret
->chunks
[num
- 1].same
++;
462 state
->hunk_in_pre_context
= 0;
464 state
->hunk_post_context
++;
466 state
->hunk_post_context
= 0;
471 if (num
&& state
->hunk_post_context
) {
472 chunk
= &state
->ret
->chunks
[num
- 1];
473 chunk
->p_next
-= state
->hunk_post_context
;
474 chunk
->t_next
-= state
->hunk_post_context
;
476 state
->ret
->num
= ++num
;
477 state
->ret
->chunks
= xrealloc(state
->ret
->chunks
,
478 sizeof(struct chunk
) * num
);
479 chunk
= &state
->ret
->chunks
[num
- 1];
480 if (parse_hunk_header(line
, len
, &off1
, &len1
, &off2
, &len2
)) {
485 /* Line numbers in patch output are one based. */
489 chunk
->same
= len2
? off2
: (off2
+ 1);
491 chunk
->p_next
= off1
+ (len1
? len1
: 1);
492 chunk
->t_next
= chunk
->same
+ len2
;
493 state
->hunk_in_pre_context
= 1;
494 state
->hunk_post_context
= 0;
497 static struct patch
*compare_buffer(mmfile_t
*file_p
, mmfile_t
*file_o
,
500 struct blame_diff_state state
;
505 xpp
.flags
= XDF_NEED_MINIMAL
;
506 xecfg
.ctxlen
= context
;
508 ecb
.outf
= xdiff_outf
;
510 memset(&state
, 0, sizeof(state
));
511 state
.xm
.consume
= process_u_diff
;
512 state
.ret
= xmalloc(sizeof(struct patch
));
513 state
.ret
->chunks
= NULL
;
516 xdl_diff(file_p
, file_o
, &xpp
, &xecfg
, &ecb
);
518 if (state
.ret
->num
) {
520 chunk
= &state
.ret
->chunks
[state
.ret
->num
- 1];
521 chunk
->p_next
-= state
.hunk_post_context
;
522 chunk
->t_next
-= state
.hunk_post_context
;
528 * Run diff between two origins and grab the patch output, so that
529 * we can pass blame for lines origin is currently suspected for
532 static struct patch
*get_patch(struct origin
*parent
, struct origin
*origin
)
534 mmfile_t file_p
, file_o
;
537 fill_origin_blob(parent
, &file_p
);
538 fill_origin_blob(origin
, &file_o
);
539 if (!file_p
.ptr
|| !file_o
.ptr
)
541 patch
= compare_buffer(&file_p
, &file_o
, 0);
546 static void free_patch(struct patch
*p
)
553 * Link in a new blame entry to the scorebord. Entries that cover the
554 * same line range have been removed from the scoreboard previously.
556 static void add_blame_entry(struct scoreboard
*sb
, struct blame_entry
*e
)
558 struct blame_entry
*ent
, *prev
= NULL
;
560 origin_incref(e
->suspect
);
562 for (ent
= sb
->ent
; ent
&& ent
->lno
< e
->lno
; ent
= ent
->next
)
565 /* prev, if not NULL, is the last one that is below e */
568 e
->next
= prev
->next
;
580 * src typically is on-stack; we want to copy the information in it to
581 * an malloced blame_entry that is already on the linked list of the
582 * scoreboard. The origin of dst loses a refcnt while the origin of src
585 static void dup_entry(struct blame_entry
*dst
, struct blame_entry
*src
)
587 struct blame_entry
*p
, *n
;
591 origin_incref(src
->suspect
);
592 origin_decref(dst
->suspect
);
593 memcpy(dst
, src
, sizeof(*src
));
599 static const char *nth_line(struct scoreboard
*sb
, int lno
)
601 return sb
->final_buf
+ sb
->lineno
[lno
];
605 * It is known that lines between tlno to same came from parent, and e
606 * has an overlap with that range. it also is known that parent's
607 * line plno corresponds to e's line tlno.
613 * <------------------>
615 * Split e into potentially three parts; before this chunk, the chunk
616 * to be blamed for the parent, and after that portion.
618 static void split_overlap(struct blame_entry
*split
,
619 struct blame_entry
*e
,
620 int tlno
, int plno
, int same
,
621 struct origin
*parent
)
624 memset(split
, 0, sizeof(struct blame_entry
[3]));
626 if (e
->s_lno
< tlno
) {
627 /* there is a pre-chunk part not blamed on parent */
628 split
[0].suspect
= origin_incref(e
->suspect
);
629 split
[0].lno
= e
->lno
;
630 split
[0].s_lno
= e
->s_lno
;
631 split
[0].num_lines
= tlno
- e
->s_lno
;
632 split
[1].lno
= e
->lno
+ tlno
- e
->s_lno
;
633 split
[1].s_lno
= plno
;
636 split
[1].lno
= e
->lno
;
637 split
[1].s_lno
= plno
+ (e
->s_lno
- tlno
);
640 if (same
< e
->s_lno
+ e
->num_lines
) {
641 /* there is a post-chunk part not blamed on parent */
642 split
[2].suspect
= origin_incref(e
->suspect
);
643 split
[2].lno
= e
->lno
+ (same
- e
->s_lno
);
644 split
[2].s_lno
= e
->s_lno
+ (same
- e
->s_lno
);
645 split
[2].num_lines
= e
->s_lno
+ e
->num_lines
- same
;
646 chunk_end_lno
= split
[2].lno
;
649 chunk_end_lno
= e
->lno
+ e
->num_lines
;
650 split
[1].num_lines
= chunk_end_lno
- split
[1].lno
;
653 * if it turns out there is nothing to blame the parent for,
654 * forget about the splitting. !split[1].suspect signals this.
656 if (split
[1].num_lines
< 1)
658 split
[1].suspect
= origin_incref(parent
);
662 * split_overlap() divided an existing blame e into up to three parts
663 * in split. Adjust the linked list of blames in the scoreboard to
666 static void split_blame(struct scoreboard
*sb
,
667 struct blame_entry
*split
,
668 struct blame_entry
*e
)
670 struct blame_entry
*new_entry
;
672 if (split
[0].suspect
&& split
[2].suspect
) {
673 /* The first part (reuse storage for the existing entry e) */
674 dup_entry(e
, &split
[0]);
676 /* The last part -- me */
677 new_entry
= xmalloc(sizeof(*new_entry
));
678 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
679 add_blame_entry(sb
, new_entry
);
681 /* ... and the middle part -- parent */
682 new_entry
= xmalloc(sizeof(*new_entry
));
683 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
684 add_blame_entry(sb
, new_entry
);
686 else if (!split
[0].suspect
&& !split
[2].suspect
)
688 * The parent covers the entire area; reuse storage for
689 * e and replace it with the parent.
691 dup_entry(e
, &split
[1]);
692 else if (split
[0].suspect
) {
693 /* me and then parent */
694 dup_entry(e
, &split
[0]);
696 new_entry
= xmalloc(sizeof(*new_entry
));
697 memcpy(new_entry
, &(split
[1]), sizeof(struct blame_entry
));
698 add_blame_entry(sb
, new_entry
);
701 /* parent and then me */
702 dup_entry(e
, &split
[1]);
704 new_entry
= xmalloc(sizeof(*new_entry
));
705 memcpy(new_entry
, &(split
[2]), sizeof(struct blame_entry
));
706 add_blame_entry(sb
, new_entry
);
709 if (DEBUG
) { /* sanity */
710 struct blame_entry
*ent
;
711 int lno
= sb
->ent
->lno
, corrupt
= 0;
713 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
718 lno
+= ent
->num_lines
;
722 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
723 printf("L %8d l %8d n %8d\n",
724 lno
, ent
->lno
, ent
->num_lines
);
725 lno
= ent
->lno
+ ent
->num_lines
;
733 * After splitting the blame, the origins used by the
734 * on-stack blame_entry should lose one refcnt each.
736 static void decref_split(struct blame_entry
*split
)
740 for (i
= 0; i
< 3; i
++)
741 origin_decref(split
[i
].suspect
);
745 * Helper for blame_chunk(). blame_entry e is known to overlap with
746 * the patch hunk; split it and pass blame to the parent.
748 static void blame_overlap(struct scoreboard
*sb
, struct blame_entry
*e
,
749 int tlno
, int plno
, int same
,
750 struct origin
*parent
)
752 struct blame_entry split
[3];
754 split_overlap(split
, e
, tlno
, plno
, same
, parent
);
755 if (split
[1].suspect
)
756 split_blame(sb
, split
, e
);
761 * Find the line number of the last line the target is suspected for.
763 static int find_last_in_target(struct scoreboard
*sb
, struct origin
*target
)
765 struct blame_entry
*e
;
766 int last_in_target
= -1;
768 for (e
= sb
->ent
; e
; e
= e
->next
) {
769 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
771 if (last_in_target
< e
->s_lno
+ e
->num_lines
)
772 last_in_target
= e
->s_lno
+ e
->num_lines
;
774 return last_in_target
;
778 * Process one hunk from the patch between the current suspect for
779 * blame_entry e and its parent. Find and split the overlap, and
780 * pass blame to the overlapping part to the parent.
782 static void blame_chunk(struct scoreboard
*sb
,
783 int tlno
, int plno
, int same
,
784 struct origin
*target
, struct origin
*parent
)
786 struct blame_entry
*e
;
788 for (e
= sb
->ent
; e
; e
= e
->next
) {
789 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
791 if (same
<= e
->s_lno
)
793 if (tlno
< e
->s_lno
+ e
->num_lines
)
794 blame_overlap(sb
, e
, tlno
, plno
, same
, parent
);
799 * We are looking at the origin 'target' and aiming to pass blame
800 * for the lines it is suspected to its parent. Run diff to find
801 * which lines came from parent and pass blame for them.
803 static int pass_blame_to_parent(struct scoreboard
*sb
,
804 struct origin
*target
,
805 struct origin
*parent
)
807 int i
, last_in_target
, plno
, tlno
;
810 last_in_target
= find_last_in_target(sb
, target
);
811 if (last_in_target
< 0)
812 return 1; /* nothing remains for this target */
814 patch
= get_patch(parent
, target
);
816 for (i
= 0; i
< patch
->num
; i
++) {
817 struct chunk
*chunk
= &patch
->chunks
[i
];
819 blame_chunk(sb
, tlno
, plno
, chunk
->same
, target
, parent
);
820 plno
= chunk
->p_next
;
821 tlno
= chunk
->t_next
;
823 /* The rest (i.e. anything after tlno) are the same as the parent */
824 blame_chunk(sb
, tlno
, plno
, last_in_target
, target
, parent
);
831 * The lines in blame_entry after splitting blames many times can become
832 * very small and trivial, and at some point it becomes pointless to
833 * blame the parents. E.g. "\t\t}\n\t}\n\n" appears everywhere in any
834 * ordinary C program, and it is not worth to say it was copied from
835 * totally unrelated file in the parent.
837 * Compute how trivial the lines in the blame_entry are.
839 static unsigned ent_score(struct scoreboard
*sb
, struct blame_entry
*e
)
848 cp
= nth_line(sb
, e
->lno
);
849 ep
= nth_line(sb
, e
->lno
+ e
->num_lines
);
851 unsigned ch
= *((unsigned char *)cp
);
861 * best_so_far[] and this[] are both a split of an existing blame_entry
862 * that passes blame to the parent. Maintain best_so_far the best split
863 * so far, by comparing this and best_so_far and copying this into
864 * bst_so_far as needed.
866 static void copy_split_if_better(struct scoreboard
*sb
,
867 struct blame_entry
*best_so_far
,
868 struct blame_entry
*this)
872 if (!this[1].suspect
)
874 if (best_so_far
[1].suspect
) {
875 if (ent_score(sb
, &this[1]) < ent_score(sb
, &best_so_far
[1]))
879 for (i
= 0; i
< 3; i
++)
880 origin_incref(this[i
].suspect
);
881 decref_split(best_so_far
);
882 memcpy(best_so_far
, this, sizeof(struct blame_entry
[3]));
886 * Find the lines from parent that are the same as ent so that
887 * we can pass blames to it. file_p has the blob contents for
890 static void find_copy_in_blob(struct scoreboard
*sb
,
891 struct blame_entry
*ent
,
892 struct origin
*parent
,
893 struct blame_entry
*split
,
903 * Prepare mmfile that contains only the lines in ent.
905 cp
= nth_line(sb
, ent
->lno
);
906 file_o
.ptr
= (char*) cp
;
907 cnt
= ent
->num_lines
;
909 while (cnt
&& cp
< sb
->final_buf
+ sb
->final_buf_size
) {
913 file_o
.size
= cp
- file_o
.ptr
;
915 patch
= compare_buffer(file_p
, &file_o
, 1);
917 memset(split
, 0, sizeof(struct blame_entry
[3]));
919 for (i
= 0; i
< patch
->num
; i
++) {
920 struct chunk
*chunk
= &patch
->chunks
[i
];
922 /* tlno to chunk->same are the same as ent */
923 if (ent
->num_lines
<= tlno
)
925 if (tlno
< chunk
->same
) {
926 struct blame_entry
this[3];
927 split_overlap(this, ent
,
928 tlno
+ ent
->s_lno
, plno
,
929 chunk
->same
+ ent
->s_lno
,
931 copy_split_if_better(sb
, split
, this);
934 plno
= chunk
->p_next
;
935 tlno
= chunk
->t_next
;
941 * See if lines currently target is suspected for can be attributed to
944 static int find_move_in_parent(struct scoreboard
*sb
,
945 struct origin
*target
,
946 struct origin
*parent
)
948 int last_in_target
, made_progress
;
949 struct blame_entry
*e
, split
[3];
952 last_in_target
= find_last_in_target(sb
, target
);
953 if (last_in_target
< 0)
954 return 1; /* nothing remains for this target */
956 fill_origin_blob(parent
, &file_p
);
961 while (made_progress
) {
963 for (e
= sb
->ent
; e
; e
= e
->next
) {
964 if (e
->guilty
|| cmp_suspect(e
->suspect
, target
))
966 find_copy_in_blob(sb
, e
, parent
, split
, &file_p
);
967 if (split
[1].suspect
&&
968 blame_move_score
< ent_score(sb
, &split
[1])) {
969 split_blame(sb
, split
, e
);
979 struct blame_entry
*ent
;
980 struct blame_entry split
[3];
984 * Count the number of entries the target is suspected for,
985 * and prepare a list of entry and the best split.
987 static struct blame_list
*setup_blame_list(struct scoreboard
*sb
,
988 struct origin
*target
,
991 struct blame_entry
*e
;
993 struct blame_list
*blame_list
= NULL
;
995 for (e
= sb
->ent
, num_ents
= 0; e
; e
= e
->next
)
996 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
999 blame_list
= xcalloc(num_ents
, sizeof(struct blame_list
));
1000 for (e
= sb
->ent
, i
= 0; e
; e
= e
->next
)
1001 if (!e
->guilty
&& !cmp_suspect(e
->suspect
, target
))
1002 blame_list
[i
++].ent
= e
;
1004 *num_ents_p
= num_ents
;
1009 * For lines target is suspected for, see if we can find code movement
1010 * across file boundary from the parent commit. porigin is the path
1011 * in the parent we already tried.
1013 static int find_copy_in_parent(struct scoreboard
*sb
,
1014 struct origin
*target
,
1015 struct commit
*parent
,
1016 struct origin
*porigin
,
1019 struct diff_options diff_opts
;
1020 const char *paths
[1];
1023 struct blame_list
*blame_list
;
1026 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1028 return 1; /* nothing remains for this target */
1030 diff_setup(&diff_opts
);
1031 diff_opts
.recursive
= 1;
1032 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1035 diff_tree_setup_paths(paths
, &diff_opts
);
1036 if (diff_setup_done(&diff_opts
) < 0)
1039 /* Try "find copies harder" on new path if requested;
1040 * we do not want to use diffcore_rename() actually to
1041 * match things up; find_copies_harder is set only to
1042 * force diff_tree_sha1() to feed all filepairs to diff_queue,
1043 * and this code needs to be after diff_setup_done(), which
1044 * usually makes find-copies-harder imply copy detection.
1046 if ((opt
& PICKAXE_BLAME_COPY_HARDER
) &&
1047 (!porigin
|| strcmp(target
->path
, porigin
->path
)))
1048 diff_opts
.find_copies_harder
= 1;
1050 diff_tree_sha1(parent
->tree
->object
.sha1
,
1051 target
->commit
->tree
->object
.sha1
,
1054 if (!diff_opts
.find_copies_harder
)
1055 diffcore_std(&diff_opts
);
1059 int made_progress
= 0;
1061 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
1062 struct diff_filepair
*p
= diff_queued_diff
.queue
[i
];
1063 struct origin
*norigin
;
1065 struct blame_entry
this[3];
1067 if (!DIFF_FILE_VALID(p
->one
))
1068 continue; /* does not exist in parent */
1069 if (porigin
&& !strcmp(p
->one
->path
, porigin
->path
))
1070 /* find_move already dealt with this path */
1073 norigin
= get_origin(sb
, parent
, p
->one
->path
);
1074 hashcpy(norigin
->blob_sha1
, p
->one
->sha1
);
1075 fill_origin_blob(norigin
, &file_p
);
1079 for (j
= 0; j
< num_ents
; j
++) {
1080 find_copy_in_blob(sb
, blame_list
[j
].ent
,
1081 norigin
, this, &file_p
);
1082 copy_split_if_better(sb
, blame_list
[j
].split
,
1086 origin_decref(norigin
);
1089 for (j
= 0; j
< num_ents
; j
++) {
1090 struct blame_entry
*split
= blame_list
[j
].split
;
1091 if (split
[1].suspect
&&
1092 blame_copy_score
< ent_score(sb
, &split
[1])) {
1093 split_blame(sb
, split
, blame_list
[j
].ent
);
1096 decref_split(split
);
1102 blame_list
= setup_blame_list(sb
, target
, &num_ents
);
1108 diff_flush(&diff_opts
);
1114 * The blobs of origin and porigin exactly match, so everything
1115 * origin is suspected for can be blamed on the parent.
1117 static void pass_whole_blame(struct scoreboard
*sb
,
1118 struct origin
*origin
, struct origin
*porigin
)
1120 struct blame_entry
*e
;
1122 if (!porigin
->file
.ptr
&& origin
->file
.ptr
) {
1123 /* Steal its file */
1124 porigin
->file
= origin
->file
;
1125 origin
->file
.ptr
= NULL
;
1127 for (e
= sb
->ent
; e
; e
= e
->next
) {
1128 if (cmp_suspect(e
->suspect
, origin
))
1130 origin_incref(porigin
);
1131 origin_decref(e
->suspect
);
1132 e
->suspect
= porigin
;
1136 #define MAXPARENT 16
1138 static void pass_blame(struct scoreboard
*sb
, struct origin
*origin
, int opt
)
1141 struct commit
*commit
= origin
->commit
;
1142 struct commit_list
*parent
;
1143 struct origin
*parent_origin
[MAXPARENT
], *porigin
;
1145 memset(parent_origin
, 0, sizeof(parent_origin
));
1147 /* The first pass looks for unrenamed path to optimize for
1148 * common cases, then we look for renames in the second pass.
1150 for (pass
= 0; pass
< 2; pass
++) {
1151 struct origin
*(*find
)(struct scoreboard
*,
1152 struct commit
*, struct origin
*);
1153 find
= pass
? find_rename
: find_origin
;
1155 for (i
= 0, parent
= commit
->parents
;
1156 i
< MAXPARENT
&& parent
;
1157 parent
= parent
->next
, i
++) {
1158 struct commit
*p
= parent
->item
;
1161 if (parent_origin
[i
])
1163 if (parse_commit(p
))
1165 porigin
= find(sb
, p
, origin
);
1168 if (!hashcmp(porigin
->blob_sha1
, origin
->blob_sha1
)) {
1169 pass_whole_blame(sb
, origin
, porigin
);
1170 origin_decref(porigin
);
1173 for (j
= same
= 0; j
< i
; j
++)
1174 if (parent_origin
[j
] &&
1175 !hashcmp(parent_origin
[j
]->blob_sha1
,
1176 porigin
->blob_sha1
)) {
1181 parent_origin
[i
] = porigin
;
1183 origin_decref(porigin
);
1188 for (i
= 0, parent
= commit
->parents
;
1189 i
< MAXPARENT
&& parent
;
1190 parent
= parent
->next
, i
++) {
1191 struct origin
*porigin
= parent_origin
[i
];
1194 if (pass_blame_to_parent(sb
, origin
, porigin
))
1199 * Optionally find moves in parents' files.
1201 if (opt
& PICKAXE_BLAME_MOVE
)
1202 for (i
= 0, parent
= commit
->parents
;
1203 i
< MAXPARENT
&& parent
;
1204 parent
= parent
->next
, i
++) {
1205 struct origin
*porigin
= parent_origin
[i
];
1208 if (find_move_in_parent(sb
, origin
, porigin
))
1213 * Optionally find copies from parents' files.
1215 if (opt
& PICKAXE_BLAME_COPY
)
1216 for (i
= 0, parent
= commit
->parents
;
1217 i
< MAXPARENT
&& parent
;
1218 parent
= parent
->next
, i
++) {
1219 struct origin
*porigin
= parent_origin
[i
];
1220 if (find_copy_in_parent(sb
, origin
, parent
->item
,
1226 for (i
= 0; i
< MAXPARENT
; i
++)
1227 origin_decref(parent_origin
[i
]);
1231 * Information on commits, used for output.
1237 unsigned long author_time
;
1240 /* filled only when asked for details */
1242 char *committer_mail
;
1243 unsigned long committer_time
;
1250 * Parse author/committer line in the commit object buffer
1252 static void get_ac_line(const char *inbuf
, const char *what
,
1253 int bufsz
, char *person
, char **mail
,
1254 unsigned long *time
, char **tz
)
1259 tmp
= strstr(inbuf
, what
);
1262 tmp
+= strlen(what
);
1263 endp
= strchr(tmp
, '\n');
1271 person
= *mail
= *tz
= "(unknown)";
1275 memcpy(person
, tmp
, len
);
1287 *time
= strtoul(tmp
, NULL
, 10);
1296 static void get_commit_info(struct commit
*commit
,
1297 struct commit_info
*ret
,
1302 static char author_buf
[1024];
1303 static char committer_buf
[1024];
1304 static char summary_buf
[1024];
1307 * We've operated without save_commit_buffer, so
1308 * we now need to populate them for output.
1310 if (!commit
->buffer
) {
1314 read_sha1_file(commit
->object
.sha1
, type
, &size
);
1316 ret
->author
= author_buf
;
1317 get_ac_line(commit
->buffer
, "\nauthor ",
1318 sizeof(author_buf
), author_buf
, &ret
->author_mail
,
1319 &ret
->author_time
, &ret
->author_tz
);
1324 ret
->committer
= committer_buf
;
1325 get_ac_line(commit
->buffer
, "\ncommitter ",
1326 sizeof(committer_buf
), committer_buf
, &ret
->committer_mail
,
1327 &ret
->committer_time
, &ret
->committer_tz
);
1329 ret
->summary
= summary_buf
;
1330 tmp
= strstr(commit
->buffer
, "\n\n");
1333 sprintf(summary_buf
, "(%s)", sha1_to_hex(commit
->object
.sha1
));
1337 endp
= strchr(tmp
, '\n');
1341 if (len
>= sizeof(summary_buf
))
1343 memcpy(summary_buf
, tmp
, len
);
1344 summary_buf
[len
] = 0;
1348 * To allow LF and other nonportable characters in pathnames,
1349 * they are c-style quoted as needed.
1351 static void write_filename_info(const char *path
)
1353 printf("filename ");
1354 write_name_quoted(NULL
, 0, path
, 1, stdout
);
1359 * The blame_entry is found to be guilty for the range. Mark it
1360 * as such, and show it in incremental output.
1362 static void found_guilty_entry(struct blame_entry
*ent
)
1368 struct origin
*suspect
= ent
->suspect
;
1370 printf("%s %d %d %d\n",
1371 sha1_to_hex(suspect
->commit
->object
.sha1
),
1372 ent
->s_lno
+ 1, ent
->lno
+ 1, ent
->num_lines
);
1373 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1374 struct commit_info ci
;
1375 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1376 get_commit_info(suspect
->commit
, &ci
, 1);
1377 printf("author %s\n", ci
.author
);
1378 printf("author-mail %s\n", ci
.author_mail
);
1379 printf("author-time %lu\n", ci
.author_time
);
1380 printf("author-tz %s\n", ci
.author_tz
);
1381 printf("committer %s\n", ci
.committer
);
1382 printf("committer-mail %s\n", ci
.committer_mail
);
1383 printf("committer-time %lu\n", ci
.committer_time
);
1384 printf("committer-tz %s\n", ci
.committer_tz
);
1385 printf("summary %s\n", ci
.summary
);
1386 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1387 printf("boundary\n");
1389 write_filename_info(suspect
->path
);
1394 * The main loop -- while the scoreboard has lines whose true origin
1395 * is still unknown, pick one brame_entry, and allow its current
1396 * suspect to pass blames to its parents.
1398 static void assign_blame(struct scoreboard
*sb
, struct rev_info
*revs
, int opt
)
1401 struct blame_entry
*ent
;
1402 struct commit
*commit
;
1403 struct origin
*suspect
= NULL
;
1405 /* find one suspect to break down */
1406 for (ent
= sb
->ent
; !suspect
&& ent
; ent
= ent
->next
)
1408 suspect
= ent
->suspect
;
1410 return; /* all done */
1413 * We will use this suspect later in the loop,
1414 * so hold onto it in the meantime.
1416 origin_incref(suspect
);
1417 commit
= suspect
->commit
;
1418 if (!commit
->object
.parsed
)
1419 parse_commit(commit
);
1420 if (!(commit
->object
.flags
& UNINTERESTING
) &&
1421 !(revs
->max_age
!= -1 && commit
->date
< revs
->max_age
))
1422 pass_blame(sb
, suspect
, opt
);
1424 commit
->object
.flags
|= UNINTERESTING
;
1425 if (commit
->object
.parsed
)
1426 mark_parents_uninteresting(commit
);
1428 /* treat root commit as boundary */
1429 if (!commit
->parents
&& !show_root
)
1430 commit
->object
.flags
|= UNINTERESTING
;
1432 /* Take responsibility for the remaining entries */
1433 for (ent
= sb
->ent
; ent
; ent
= ent
->next
)
1434 if (!cmp_suspect(ent
->suspect
, suspect
))
1435 found_guilty_entry(ent
);
1436 origin_decref(suspect
);
1438 if (DEBUG
) /* sanity */
1439 sanity_check_refcnt(sb
);
1443 static const char *format_time(unsigned long time
, const char *tz_str
,
1446 static char time_buf
[128];
1451 if (show_raw_time
) {
1452 sprintf(time_buf
, "%lu %s", time
, tz_str
);
1457 minutes
= tz
< 0 ? -tz
: tz
;
1458 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
1459 minutes
= tz
< 0 ? -minutes
: minutes
;
1460 t
= time
+ minutes
* 60;
1463 strftime(time_buf
, sizeof(time_buf
), "%Y-%m-%d %H:%M:%S ", tm
);
1464 strcat(time_buf
, tz_str
);
1468 #define OUTPUT_ANNOTATE_COMPAT 001
1469 #define OUTPUT_LONG_OBJECT_NAME 002
1470 #define OUTPUT_RAW_TIMESTAMP 004
1471 #define OUTPUT_PORCELAIN 010
1472 #define OUTPUT_SHOW_NAME 020
1473 #define OUTPUT_SHOW_NUMBER 040
1474 #define OUTPUT_SHOW_SCORE 0100
1476 static void emit_porcelain(struct scoreboard
*sb
, struct blame_entry
*ent
)
1480 struct origin
*suspect
= ent
->suspect
;
1483 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1484 printf("%s%c%d %d %d\n",
1486 ent
->guilty
? ' ' : '*', // purely for debugging
1490 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1491 struct commit_info ci
;
1492 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1493 get_commit_info(suspect
->commit
, &ci
, 1);
1494 printf("author %s\n", ci
.author
);
1495 printf("author-mail %s\n", ci
.author_mail
);
1496 printf("author-time %lu\n", ci
.author_time
);
1497 printf("author-tz %s\n", ci
.author_tz
);
1498 printf("committer %s\n", ci
.committer
);
1499 printf("committer-mail %s\n", ci
.committer_mail
);
1500 printf("committer-time %lu\n", ci
.committer_time
);
1501 printf("committer-tz %s\n", ci
.committer_tz
);
1502 write_filename_info(suspect
->path
);
1503 printf("summary %s\n", ci
.summary
);
1504 if (suspect
->commit
->object
.flags
& UNINTERESTING
)
1505 printf("boundary\n");
1507 else if (suspect
->commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1508 write_filename_info(suspect
->path
);
1510 cp
= nth_line(sb
, ent
->lno
);
1511 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1514 printf("%s %d %d\n", hex
,
1515 ent
->s_lno
+ 1 + cnt
,
1516 ent
->lno
+ 1 + cnt
);
1521 } while (ch
!= '\n' &&
1522 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1526 static void emit_other(struct scoreboard
*sb
, struct blame_entry
*ent
, int opt
)
1530 struct origin
*suspect
= ent
->suspect
;
1531 struct commit_info ci
;
1533 int show_raw_time
= !!(opt
& OUTPUT_RAW_TIMESTAMP
);
1535 get_commit_info(suspect
->commit
, &ci
, 1);
1536 strcpy(hex
, sha1_to_hex(suspect
->commit
->object
.sha1
));
1538 cp
= nth_line(sb
, ent
->lno
);
1539 for (cnt
= 0; cnt
< ent
->num_lines
; cnt
++) {
1541 int length
= (opt
& OUTPUT_LONG_OBJECT_NAME
) ? 40 : 8;
1543 if (suspect
->commit
->object
.flags
& UNINTERESTING
) {
1544 if (!blank_boundary
) {
1549 memset(hex
, ' ', length
);
1552 printf("%.*s", length
, hex
);
1553 if (opt
& OUTPUT_ANNOTATE_COMPAT
)
1554 printf("\t(%10s\t%10s\t%d)", ci
.author
,
1555 format_time(ci
.author_time
, ci
.author_tz
,
1557 ent
->lno
+ 1 + cnt
);
1559 if (opt
& OUTPUT_SHOW_SCORE
)
1561 max_score_digits
, ent
->score
,
1562 ent
->suspect
->refcnt
);
1563 if (opt
& OUTPUT_SHOW_NAME
)
1564 printf(" %-*.*s", longest_file
, longest_file
,
1566 if (opt
& OUTPUT_SHOW_NUMBER
)
1567 printf(" %*d", max_orig_digits
,
1568 ent
->s_lno
+ 1 + cnt
);
1569 printf(" (%-*.*s %10s %*d) ",
1570 longest_author
, longest_author
, ci
.author
,
1571 format_time(ci
.author_time
, ci
.author_tz
,
1573 max_digits
, ent
->lno
+ 1 + cnt
);
1578 } while (ch
!= '\n' &&
1579 cp
< sb
->final_buf
+ sb
->final_buf_size
);
1583 static void output(struct scoreboard
*sb
, int option
)
1585 struct blame_entry
*ent
;
1587 if (option
& OUTPUT_PORCELAIN
) {
1588 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1589 struct blame_entry
*oth
;
1590 struct origin
*suspect
= ent
->suspect
;
1591 struct commit
*commit
= suspect
->commit
;
1592 if (commit
->object
.flags
& MORE_THAN_ONE_PATH
)
1594 for (oth
= ent
->next
; oth
; oth
= oth
->next
) {
1595 if ((oth
->suspect
->commit
!= commit
) ||
1596 !strcmp(oth
->suspect
->path
, suspect
->path
))
1598 commit
->object
.flags
|= MORE_THAN_ONE_PATH
;
1604 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1605 if (option
& OUTPUT_PORCELAIN
)
1606 emit_porcelain(sb
, ent
);
1608 emit_other(sb
, ent
, option
);
1614 * To allow quick access to the contents of nth line in the
1615 * final image, prepare an index in the scoreboard.
1617 static int prepare_lines(struct scoreboard
*sb
)
1619 const char *buf
= sb
->final_buf
;
1620 unsigned long len
= sb
->final_buf_size
;
1621 int num
= 0, incomplete
= 0, bol
= 1;
1623 if (len
&& buf
[len
-1] != '\n')
1624 incomplete
++; /* incomplete line at the end */
1627 sb
->lineno
= xrealloc(sb
->lineno
,
1628 sizeof(int* ) * (num
+ 1));
1629 sb
->lineno
[num
] = buf
- sb
->final_buf
;
1632 if (*buf
++ == '\n') {
1637 sb
->lineno
= xrealloc(sb
->lineno
,
1638 sizeof(int* ) * (num
+ incomplete
+ 1));
1639 sb
->lineno
[num
+ incomplete
] = buf
- sb
->final_buf
;
1640 sb
->num_lines
= num
+ incomplete
;
1641 return sb
->num_lines
;
1645 * Add phony grafts for use with -S; this is primarily to
1646 * support git-cvsserver that wants to give a linear history
1649 static int read_ancestry(const char *graft_file
)
1651 FILE *fp
= fopen(graft_file
, "r");
1655 while (fgets(buf
, sizeof(buf
), fp
)) {
1656 /* The format is just "Commit Parent1 Parent2 ...\n" */
1657 int len
= strlen(buf
);
1658 struct commit_graft
*graft
= read_graft_line(buf
, len
);
1660 register_commit_graft(graft
, 0);
1667 * How many columns do we need to show line numbers in decimal?
1669 static int lineno_width(int lines
)
1673 for (width
= 1, i
= 10; i
<= lines
+ 1; width
++)
1679 * How many columns do we need to show line numbers, authors,
1682 static void find_alignment(struct scoreboard
*sb
, int *option
)
1684 int longest_src_lines
= 0;
1685 int longest_dst_lines
= 0;
1686 unsigned largest_score
= 0;
1687 struct blame_entry
*e
;
1689 for (e
= sb
->ent
; e
; e
= e
->next
) {
1690 struct origin
*suspect
= e
->suspect
;
1691 struct commit_info ci
;
1694 if (strcmp(suspect
->path
, sb
->path
))
1695 *option
|= OUTPUT_SHOW_NAME
;
1696 num
= strlen(suspect
->path
);
1697 if (longest_file
< num
)
1699 if (!(suspect
->commit
->object
.flags
& METAINFO_SHOWN
)) {
1700 suspect
->commit
->object
.flags
|= METAINFO_SHOWN
;
1701 get_commit_info(suspect
->commit
, &ci
, 1);
1702 num
= strlen(ci
.author
);
1703 if (longest_author
< num
)
1704 longest_author
= num
;
1706 num
= e
->s_lno
+ e
->num_lines
;
1707 if (longest_src_lines
< num
)
1708 longest_src_lines
= num
;
1709 num
= e
->lno
+ e
->num_lines
;
1710 if (longest_dst_lines
< num
)
1711 longest_dst_lines
= num
;
1712 if (largest_score
< ent_score(sb
, e
))
1713 largest_score
= ent_score(sb
, e
);
1715 max_orig_digits
= lineno_width(longest_src_lines
);
1716 max_digits
= lineno_width(longest_dst_lines
);
1717 max_score_digits
= lineno_width(largest_score
);
1721 * For debugging -- origin is refcounted, and this asserts that
1722 * we do not underflow.
1724 static void sanity_check_refcnt(struct scoreboard
*sb
)
1727 struct blame_entry
*ent
;
1729 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1730 /* Nobody should have zero or negative refcnt */
1731 if (ent
->suspect
->refcnt
<= 0) {
1732 fprintf(stderr
, "%s in %s has negative refcnt %d\n",
1734 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1735 ent
->suspect
->refcnt
);
1739 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1740 /* Mark the ones that haven't been checked */
1741 if (0 < ent
->suspect
->refcnt
)
1742 ent
->suspect
->refcnt
= -ent
->suspect
->refcnt
;
1744 for (ent
= sb
->ent
; ent
; ent
= ent
->next
) {
1746 * ... then pick each and see if they have the the
1750 struct blame_entry
*e
;
1751 struct origin
*suspect
= ent
->suspect
;
1753 if (0 < suspect
->refcnt
)
1755 suspect
->refcnt
= -suspect
->refcnt
; /* Unmark */
1756 for (found
= 0, e
= sb
->ent
; e
; e
= e
->next
) {
1757 if (e
->suspect
!= suspect
)
1761 if (suspect
->refcnt
!= found
) {
1762 fprintf(stderr
, "%s in %s has refcnt %d, not %d\n",
1764 sha1_to_hex(ent
->suspect
->commit
->object
.sha1
),
1765 ent
->suspect
->refcnt
, found
);
1771 find_alignment(sb
, &opt
);
1773 die("Baa %d!", baa
);
1778 * Used for the command line parsing; check if the path exists
1779 * in the working tree.
1781 static int has_path_in_work_tree(const char *path
)
1784 return !lstat(path
, &st
);
1787 static unsigned parse_score(const char *arg
)
1790 unsigned long score
= strtoul(arg
, &end
, 10);
1796 static const char *add_prefix(const char *prefix
, const char *path
)
1798 if (!prefix
|| !prefix
[0])
1800 return prefix_path(prefix
, strlen(prefix
), path
);
1804 * Parsing of (comma separated) one item in the -L option
1806 static const char *parse_loc(const char *spec
,
1807 struct scoreboard
*sb
, long lno
,
1808 long begin
, long *ret
)
1815 regmatch_t match
[1];
1817 /* Allow "-L <something>,+20" to mean starting at <something>
1818 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
1821 if (1 < begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
1822 num
= strtol(spec
+ 1, &term
, 10);
1823 if (term
!= spec
+ 1) {
1827 *ret
= begin
+ num
- 2;
1836 num
= strtol(spec
, &term
, 10);
1844 /* it could be a regexp of form /.../ */
1845 for (term
= (char*) spec
+ 1; *term
&& *term
!= '/'; term
++) {
1852 /* try [spec+1 .. term-1] as regexp */
1854 begin
--; /* input is in human terms */
1855 line
= nth_line(sb
, begin
);
1857 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
1858 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
1859 const char *cp
= line
+ match
[0].rm_so
;
1862 while (begin
++ < lno
) {
1863 nline
= nth_line(sb
, begin
);
1864 if (line
<= cp
&& cp
< nline
)
1875 regerror(reg_error
, ®exp
, errbuf
, 1024);
1876 die("-L parameter '%s': %s", spec
+ 1, errbuf
);
1881 * Parsing of -L option
1883 static void prepare_blame_range(struct scoreboard
*sb
,
1884 const char *bottomtop
,
1886 long *bottom
, long *top
)
1890 term
= parse_loc(bottomtop
, sb
, lno
, 1, bottom
);
1892 term
= parse_loc(term
+ 1, sb
, lno
, *bottom
+ 1, top
);
1900 static int git_blame_config(const char *var
, const char *value
)
1902 if (!strcmp(var
, "blame.showroot")) {
1903 show_root
= git_config_bool(var
, value
);
1906 if (!strcmp(var
, "blame.blankboundary")) {
1907 blank_boundary
= git_config_bool(var
, value
);
1910 return git_default_config(var
, value
);
1913 int cmd_blame(int argc
, const char **argv
, const char *prefix
)
1915 struct rev_info revs
;
1917 struct scoreboard sb
;
1919 struct blame_entry
*ent
;
1920 int i
, seen_dashdash
, unk
, opt
;
1921 long bottom
, top
, lno
;
1922 int output_option
= 0;
1923 const char *revs_file
= NULL
;
1924 const char *final_commit_name
= NULL
;
1926 const char *bottomtop
= NULL
;
1928 git_config(git_blame_config
);
1929 save_commit_buffer
= 0;
1933 for (unk
= i
= 1; i
< argc
; i
++) {
1934 const char *arg
= argv
[i
];
1937 else if (!strcmp("-b", arg
))
1939 else if (!strcmp("--root", arg
))
1941 else if (!strcmp("-c", arg
))
1942 output_option
|= OUTPUT_ANNOTATE_COMPAT
;
1943 else if (!strcmp("-t", arg
))
1944 output_option
|= OUTPUT_RAW_TIMESTAMP
;
1945 else if (!strcmp("-l", arg
))
1946 output_option
|= OUTPUT_LONG_OBJECT_NAME
;
1947 else if (!strcmp("-S", arg
) && ++i
< argc
)
1948 revs_file
= argv
[i
];
1949 else if (!strncmp("-M", arg
, 2)) {
1950 opt
|= PICKAXE_BLAME_MOVE
;
1951 blame_move_score
= parse_score(arg
+2);
1953 else if (!strncmp("-C", arg
, 2)) {
1954 if (opt
& PICKAXE_BLAME_COPY
)
1955 opt
|= PICKAXE_BLAME_COPY_HARDER
;
1956 opt
|= PICKAXE_BLAME_COPY
| PICKAXE_BLAME_MOVE
;
1957 blame_copy_score
= parse_score(arg
+2);
1959 else if (!strncmp("-L", arg
, 2)) {
1968 die("More than one '-L n,m' option given");
1971 else if (!strcmp("--incremental", arg
))
1973 else if (!strcmp("--score-debug", arg
))
1974 output_option
|= OUTPUT_SHOW_SCORE
;
1975 else if (!strcmp("-f", arg
) ||
1976 !strcmp("--show-name", arg
))
1977 output_option
|= OUTPUT_SHOW_NAME
;
1978 else if (!strcmp("-n", arg
) ||
1979 !strcmp("--show-number", arg
))
1980 output_option
|= OUTPUT_SHOW_NUMBER
;
1981 else if (!strcmp("-p", arg
) ||
1982 !strcmp("--porcelain", arg
))
1983 output_option
|= OUTPUT_PORCELAIN
;
1984 else if (!strcmp("--", arg
)) {
1996 if (!blame_move_score
)
1997 blame_move_score
= BLAME_DEFAULT_MOVE_SCORE
;
1998 if (!blame_copy_score
)
1999 blame_copy_score
= BLAME_DEFAULT_COPY_SCORE
;
2002 * We have collected options unknown to us in argv[1..unk]
2003 * which are to be passed to revision machinery if we are
2004 * going to do the "bottom" procesing.
2006 * The remaining are:
2008 * (1) if seen_dashdash, its either
2009 * "-options -- <path>" or
2010 * "-options -- <path> <rev>".
2011 * but the latter is allowed only if there is no
2012 * options that we passed to revision machinery.
2014 * (2) otherwise, we may have "--" somewhere later and
2015 * might be looking at the first one of multiple 'rev'
2016 * parameters (e.g. " master ^next ^maint -- path").
2017 * See if there is a dashdash first, and give the
2018 * arguments before that to revision machinery.
2019 * After that there must be one 'path'.
2021 * (3) otherwise, its one of the three:
2022 * "-options <path> <rev>"
2023 * "-options <rev> <path>"
2025 * but again the first one is allowed only if
2026 * there is no options that we passed to revision
2030 if (seen_dashdash
) {
2034 path
= add_prefix(prefix
, argv
[i
]);
2035 if (i
+ 1 == argc
- 1) {
2038 argv
[unk
++] = argv
[i
+ 1];
2040 else if (i
+ 1 != argc
)
2041 /* garbage at end */
2046 for (j
= i
; !seen_dashdash
&& j
< argc
; j
++)
2047 if (!strcmp(argv
[j
], "--"))
2049 if (seen_dashdash
) {
2050 if (seen_dashdash
+ 1 != argc
- 1)
2052 path
= add_prefix(prefix
, argv
[seen_dashdash
+ 1]);
2053 for (j
= i
; j
< seen_dashdash
; j
++)
2054 argv
[unk
++] = argv
[j
];
2058 path
= add_prefix(prefix
, argv
[i
]);
2059 if (i
+ 1 == argc
- 1) {
2060 final_commit_name
= argv
[i
+ 1];
2062 /* if (unk == 1) we could be getting
2065 if (unk
== 1 && !has_path_in_work_tree(path
)) {
2066 path
= add_prefix(prefix
, argv
[i
+ 1]);
2067 final_commit_name
= argv
[i
];
2070 else if (i
!= argc
- 1)
2071 usage(blame_usage
); /* garbage at end */
2073 if (!has_path_in_work_tree(path
))
2074 die("cannot stat path %s: %s",
2075 path
, strerror(errno
));
2079 if (final_commit_name
)
2080 argv
[unk
++] = final_commit_name
;
2083 * Now we got rev and path. We do not want the path pruning
2084 * but we may want "bottom" processing.
2086 argv
[unk
++] = "--"; /* terminate the rev name */
2089 init_revisions(&revs
, NULL
);
2090 setup_revisions(unk
, argv
, &revs
, "HEAD");
2091 memset(&sb
, 0, sizeof(sb
));
2094 * There must be one and only one positive commit in the
2095 * revs->pending array.
2097 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
2098 struct object
*obj
= revs
.pending
.objects
[i
].item
;
2099 if (obj
->flags
& UNINTERESTING
)
2101 while (obj
->type
== OBJ_TAG
)
2102 obj
= deref_tag(obj
, NULL
, 0);
2103 if (obj
->type
!= OBJ_COMMIT
)
2104 die("Non commit %s?",
2105 revs
.pending
.objects
[i
].name
);
2107 die("More than one commit to dig from %s and %s?",
2108 revs
.pending
.objects
[i
].name
,
2110 sb
.final
= (struct commit
*) obj
;
2111 final_commit_name
= revs
.pending
.objects
[i
].name
;
2116 * "--not A B -- path" without anything positive;
2119 unsigned char head_sha1
[20];
2121 final_commit_name
= "HEAD";
2122 if (get_sha1(final_commit_name
, head_sha1
))
2123 die("No such ref: HEAD");
2124 sb
.final
= lookup_commit_reference(head_sha1
);
2125 add_pending_object(&revs
, &(sb
.final
->object
), "HEAD");
2129 * If we have bottom, this will mark the ancestors of the
2130 * bottom commits we would reach while traversing as
2133 prepare_revision_walk(&revs
);
2135 o
= get_origin(&sb
, sb
.final
, path
);
2136 if (fill_blob_sha1(o
))
2137 die("no such path %s in %s", path
, final_commit_name
);
2139 sb
.final_buf
= read_sha1_file(o
->blob_sha1
, type
, &sb
.final_buf_size
);
2141 lno
= prepare_lines(&sb
);
2145 prepare_blame_range(&sb
, bottomtop
, lno
, &bottom
, &top
);
2146 if (bottom
&& top
&& top
< bottom
) {
2148 tmp
= top
; top
= bottom
; bottom
= tmp
;
2156 die("file %s has only %lu lines", path
, lno
);
2158 ent
= xcalloc(1, sizeof(*ent
));
2160 ent
->num_lines
= top
- bottom
;
2162 ent
->s_lno
= bottom
;
2167 if (revs_file
&& read_ancestry(revs_file
))
2168 die("reading graft file %s failed: %s",
2169 revs_file
, strerror(errno
));
2171 assign_blame(&sb
, &revs
, opt
);
2178 if (!(output_option
& OUTPUT_PORCELAIN
))
2179 find_alignment(&sb
, &output_option
);
2181 output(&sb
, output_option
);
2182 free((void *)sb
.final_buf
);
2183 for (ent
= sb
.ent
; ent
; ) {
2184 struct blame_entry
*e
= ent
->next
;
2190 printf("num read blob: %d\n", num_read_blob
);
2191 printf("num get patch: %d\n", num_get_patch
);
2192 printf("num commits: %d\n", num_commits
);