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

git-pickaxe -M: blame line movements within a file.

This makes pickaxe more intelligent than the classic blame. A typical example is a change that moves one static C function from lower part of the file to upper part of the same file, because you added a new caller in the middle. The versions in the parent and the child would look like this: parent child A static foo() { B ... C } D A E B F C G D static foo() { ... call foo(); ... E } F H G H With the classic blame algorithm, we can blame lines A B C D E F G and H to the parent. The child is guilty of introducing the line "... call foo();", and the blame is placed on the child. However, the classic blame algorithm fails to notice that the implementation of foo() at the top of the file is not new, and moved from the lower part of the parent. This commit introduces detection of such line movements, and correctly blames the lines that were simply moved in the file to the parent. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 19, 2006 at 18:49 UTC d24bba8008d8e537cb48e9760f7621cbe7ae9e38
2 files changed +130 -7
Documentation/git-pickaxe.txt
+13 -1
@@ -7,7 +7,9 @@ git-pickaxe - Show what revision and author last modified each line of a file
7
8 SYNOPSIS
9 --------
10 -'git-pickaxe' [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [<rev>] [--] <file>
10 +[verse]
11 +'git-pickaxe' [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>]
12 + [-M] [--since=<date>] [<rev>] [--] <file>
13
14 DESCRIPTION
15 -----------
@@ -61,6 +63,16 @@ OPTIONS
63 -p, --porcelain::
64 Show in a format designed for machine consumption.
65
66 +-M::
67 + Detect moving lines in the file as well. When a commit
68 + moves a block of lines in a file (e.g. the original file
69 + has A and then B, and the commit changes it to B and
70 + then A), traditional 'blame' algorithm typically blames
71 + the lines that were moved up (i.e. B) to the parent and
72 + assigns blame to the lines that were moved down (i.e. A)
73 + to the child commit. With this option, both groups of
74 + lines are blamed on the parent.
75 +
76 -h, --help::
77 Show help message.
78
builtin-pickaxe.c
+117 -6
@@ -19,7 +19,7 @@
19 #include <sys/time.h>
20
21 static char pickaxe_usage[] =
22 -"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [commit] [--] file\n"
22 +"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [commit] [--] file\n"
23 " -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
24 " -l, --long Show long commit SHA1 (Default: off)\n"
25 " -t, --time Show raw timestamp (Default: off)\n"
@@ -27,6 +27,7 @@ static char pickaxe_usage[] =
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 Find line movements within the file\n"
31 " -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
32
33 static int longest_file;
@@ -36,6 +37,8 @@ static int max_digits;
37
38 #define DEBUG 0
39
40 +#define PICKAXE_BLAME_MOVE 01
41 +
42 /* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
43 #define METAINFO_SHOWN (1u<<12)
44 #define MORE_THAN_ONE_PATH (1u<<13)
@@ -542,9 +545,99 @@ static int pass_blame_to_parent(struct scoreboard *sb,
545 return 0;
546 }
547
548 +static void copy_split_if_better(struct blame_entry best_so_far[3],
549 + struct blame_entry this[3])
550 +{
551 + if (!this[1].suspect)
552 + return;
553 + if (best_so_far[1].suspect &&
554 + (this[1].num_lines < best_so_far[1].num_lines))
555 + return;
556 + memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
557 +}
558 +
559 +static void find_copy_in_blob(struct scoreboard *sb,
560 + struct blame_entry *ent,
561 + struct origin *parent,
562 + struct blame_entry split[3],
563 + mmfile_t *file_p)
564 +{
565 + const char *cp;
566 + int cnt;
567 + mmfile_t file_o;
568 + struct patch *patch;
569 + int i, plno, tlno;
570 +
571 + cp = nth_line(sb, ent->lno);
572 + file_o.ptr = (char*) cp;
573 + cnt = ent->num_lines;
574 +
575 + while (cnt && cp < sb->final_buf + sb->final_buf_size) {
576 + if (*cp++ == '\n')
577 + cnt--;
578 + }
579 + file_o.size = cp - file_o.ptr;
580 +
581 + patch = compare_buffer(file_p, &file_o, 1);
582 +
583 + memset(split, 0, sizeof(struct blame_entry [3]));
584 + plno = tlno = 0;
585 + for (i = 0; i < patch->num; i++) {
586 + struct chunk *chunk = &patch->chunks[i];
587 +
588 + /* tlno to chunk->same are the same as ent */
589 + if (ent->num_lines <= tlno)
590 + break;
591 + if (tlno < chunk->same) {
592 + struct blame_entry this[3];
593 + split_overlap(this, ent,
594 + tlno + ent->s_lno, plno,
595 + chunk->same + ent->s_lno,
596 + parent);
597 + copy_split_if_better(split, this);
598 + }
599 + plno = chunk->p_next;
600 + tlno = chunk->t_next;
601 + }
602 + free_patch(patch);
603 +}
604 +
605 +static int find_move_in_parent(struct scoreboard *sb,
606 + struct origin *target,
607 + struct origin *parent)
608 +{
609 + int last_in_target;
610 + struct blame_entry *ent, split[3];
611 + mmfile_t file_p;
612 + char type[10];
613 + char *blob_p;
614 +
615 + last_in_target = find_last_in_target(sb, target);
616 + if (last_in_target < 0)
617 + return 1; /* nothing remains for this target */
618 +
619 + blob_p = read_sha1_file(parent->blob_sha1, type,
620 + (unsigned long *) &file_p.size);
621 + file_p.ptr = blob_p;
622 + if (!file_p.ptr) {
623 + free(blob_p);
624 + return 0;
625 + }
626 +
627 + for (ent = sb->ent; ent; ent = ent->next) {
628 + if (ent->suspect != target || ent->guilty)
629 + continue;
630 + find_copy_in_blob(sb, ent, parent, split, &file_p);
631 + if (split[1].suspect)
632 + split_blame(sb, split, ent);
633 + }
634 + free(blob_p);
635 + return 0;
636 +}
637 +
638 #define MAXPARENT 16
639
547 -static void pass_blame(struct scoreboard *sb, struct origin *origin)
640 +static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
641 {
642 int i;
643 struct commit *commit = origin->commit;
@@ -589,9 +682,24 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin)
682 if (pass_blame_to_parent(sb, origin, porigin))
683 return;
684 }
685 +
686 + /*
687 + * Optionally run "miff" to find moves in parents' files here.
688 + */
689 + if (opt & PICKAXE_BLAME_MOVE)
690 + for (i = 0, parent = commit->parents;
691 + i < MAXPARENT && parent;
692 + parent = parent->next, i++) {
693 + struct origin *porigin = parent_origin[i];
694 + if (!porigin)
695 + continue;
696 + if (find_move_in_parent(sb, origin, porigin))
697 + return;
698 + }
699 +
700 }
701
594 -static void assign_blame(struct scoreboard *sb, struct rev_info *revs)
702 +static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
703 {
704 while (1) {
705 struct blame_entry *ent;
@@ -609,7 +717,7 @@ static void assign_blame(struct scoreboard *sb, struct rev_info *revs)
717 parse_commit(commit);
718 if (!(commit->object.flags & UNINTERESTING) &&
719 !(revs->max_age != -1 && commit->date < revs->max_age))
612 - pass_blame(sb, suspect);
720 + pass_blame(sb, suspect, opt);
721
722 /* Take responsibility for the remaining entries */
723 for (ent = sb->ent; ent; ent = ent->next)
@@ -967,13 +1075,14 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1075 struct scoreboard sb;
1076 struct origin *o;
1077 struct blame_entry *ent;
970 - int i, seen_dashdash, unk;
1078 + int i, seen_dashdash, unk, opt;
1079 long bottom, top, lno;
1080 int output_option = 0;
1081 const char *revs_file = NULL;
1082 const char *final_commit_name = NULL;
1083 char type[10];
1084
1085 + opt = 0;
1086 bottom = top = 0;
1087 seen_dashdash = 0;
1088 for (unk = i = 1; i < argc; i++) {
@@ -988,6 +1097,8 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1097 output_option |= OUTPUT_LONG_OBJECT_NAME;
1098 else if (!strcmp("-S", arg) && ++i < argc)
1099 revs_file = argv[i];
1100 + else if (!strcmp("-M", arg))
1101 + opt |= PICKAXE_BLAME_MOVE;
1102 else if (!strcmp("-L", arg) && ++i < argc) {
1103 char *term;
1104 arg = argv[i];
@@ -1176,7 +1287,7 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1287 die("reading graft file %s failed: %s",
1288 revs_file, strerror(errno));
1289
1179 - assign_blame(&sb, &revs);
1290 + assign_blame(&sb, &revs, opt);
1291
1292 coalesce(&sb);
1293