git-pickaxe -C: blame cut-and-pasted lines.
This completes the initial round of git-pickaxe. In addition to the detection of line movements we already have, this finds new lines that were created by moving or cutting-and-pasting lines from different files in the parent. With this, git pickaxe -f -n -C v1.4.0 -- revision.c finds that a major part of that file actually came from rev-list.c when Linus split the latter at commit ae563642 and blames them to earlier commits that touch rev-list.c. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 19, 2006 at 18:50 UTC
18abd745a05197f498219f5ba88ce238a3d51580
2 files changed
+102
-3
Documentation/git-pickaxe.txt
+9
-1
@@ -9,7 +9,7 @@ SYNOPSIS
9
--------
10
[verse]
11
'git-pickaxe' [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>]
12
- [-M] [--since=<date>] [<rev>] [--] <file>
12
+ [-M] [-C] [-C] [--since=<date>] [<rev>] [--] <file>
13
14
DESCRIPTION
15
-----------
@@ -73,6 +73,14 @@ OPTIONS
73
to the child commit. With this option, both groups of
74
lines are blamed on the parent.
75
76
+-C::
77
+ In addition to `-M`, detect lines copied from other
78
+ files that were modified in the same commit. This is
79
+ useful when you reorganize your program and move code
80
+ around across files. When this option is given twice,
81
+ the command looks for copies from all other files in the
82
+ parent for the commit that creates the file in addition.
83
+
84
-h, --help::
85
Show help message.
86
builtin-pickaxe.c
+93
-2
@@ -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>] [-M] [commit] [--] file\n"
22
+"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [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,7 +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"
30
+" -M, -C Find line movements within and across files\n"
31
" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
32
33
static int longest_file;
@@ -38,6 +38,8 @@ static int max_digits;
38
#define DEBUG 0
39
40
#define PICKAXE_BLAME_MOVE 01
41
+#define PICKAXE_BLAME_COPY 02
42
+#define PICKAXE_BLAME_COPY_HARDER 04
43
44
/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
45
#define METAINFO_SHOWN (1u<<12)
@@ -635,6 +637,78 @@ static int find_move_in_parent(struct scoreboard *sb,
637
return 0;
638
}
639
640
+static int find_copy_in_parent(struct scoreboard *sb,
641
+ struct origin *target,
642
+ struct commit *parent,
643
+ struct origin *porigin,
644
+ int opt)
645
+{
646
+ struct diff_options diff_opts;
647
+ const char *paths[1];
648
+ struct blame_entry *ent;
649
+ int i;
650
+
651
+ if (find_last_in_target(sb, target) < 0)
652
+ return 1; /* nothing remains for this target */
653
+
654
+ diff_setup(&diff_opts);
655
+ diff_opts.recursive = 1;
656
+ diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
657
+
658
+ /* Try "find copies harder" on new path */
659
+ if ((opt & PICKAXE_BLAME_COPY_HARDER) &&
660
+ (!porigin || strcmp(target->path, porigin->path))) {
661
+ diff_opts.detect_rename = DIFF_DETECT_COPY;
662
+ diff_opts.find_copies_harder = 1;
663
+ }
664
+ paths[0] = NULL;
665
+ diff_tree_setup_paths(paths, &diff_opts);
666
+ if (diff_setup_done(&diff_opts) < 0)
667
+ die("diff-setup");
668
+ diff_tree_sha1(parent->tree->object.sha1,
669
+ target->commit->tree->object.sha1,
670
+ "", &diff_opts);
671
+ diffcore_std(&diff_opts);
672
+
673
+ for (ent = sb->ent; ent; ent = ent->next) {
674
+ struct blame_entry split[3];
675
+ if (ent->suspect != target || ent->guilty)
676
+ continue;
677
+
678
+ memset(split, 0, sizeof(split));
679
+ for (i = 0; i < diff_queued_diff.nr; i++) {
680
+ struct diff_filepair *p = diff_queued_diff.queue[i];
681
+ struct origin *norigin;
682
+ mmfile_t file_p;
683
+ char type[10];
684
+ char *blob;
685
+ struct blame_entry this[3];
686
+
687
+ if (!DIFF_FILE_VALID(p->one))
688
+ continue; /* does not exist in parent */
689
+ if (porigin && !strcmp(p->one->path, porigin->path))
690
+ /* find_move already dealt with this path */
691
+ continue;
692
+ norigin = find_origin(sb, parent, p->one->path);
693
+
694
+ blob = read_sha1_file(norigin->blob_sha1, type,
695
+ (unsigned long *) &file_p.size);
696
+ file_p.ptr = blob;
697
+ if (!file_p.ptr) {
698
+ free(blob);
699
+ continue;
700
+ }
701
+ find_copy_in_blob(sb, ent, norigin, this, &file_p);
702
+ copy_split_if_better(split, this);
703
+ }
704
+ if (split[1].suspect)
705
+ split_blame(sb, split, ent);
706
+ }
707
+ diff_flush(&diff_opts);
708
+
709
+ return 0;
710
+}
711
+
712
#define MAXPARENT 16
713
714
static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
@@ -697,6 +771,18 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
771
return;
772
}
773
774
+ /*
775
+ * Optionally run "ciff" to find copies from parents' files here.
776
+ */
777
+ if (opt & PICKAXE_BLAME_COPY)
778
+ for (i = 0, parent = commit->parents;
779
+ i < MAXPARENT && parent;
780
+ parent = parent->next, i++) {
781
+ struct origin *porigin = parent_origin[i];
782
+ if (find_copy_in_parent(sb, origin, parent->item,
783
+ porigin, opt))
784
+ return;
785
+ }
786
}
787
788
static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
@@ -1099,6 +1185,11 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1185
revs_file = argv[i];
1186
else if (!strcmp("-M", arg))
1187
opt |= PICKAXE_BLAME_MOVE;
1188
+ else if (!strcmp("-C", arg)) {
1189
+ if (opt & PICKAXE_BLAME_COPY)
1190
+ opt |= PICKAXE_BLAME_COPY_HARDER;
1191
+ opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
1192
+ }
1193
else if (!strcmp("-L", arg) && ++i < argc) {
1194
char *term;
1195
arg = argv[i];