git-pickaxe: improve "best match" heuristics
Instead of comparing number of lines matched, look at the matched characters and count alnums, so that we do not pass blame on not-so-interesting lines, such as an empty line and a line that is indentation followed by a closing brace. Add an option --score-debug to show the score of each blame_entry while we cook this further on the "next" branch. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 20, 2006 at 14:51 UTC
5ff62c300209d761e8e7b3c78da19b910cd9b860
1 file changed
+48
-23
builtin-pickaxe.c
+48
-23
@@ -34,8 +34,7 @@ static int longest_file;
34
static int longest_author;
35
static int max_orig_digits;
36
static int max_digits;
37
-
38
-#define DEBUG 0
37
+static int max_score_digits;
38
39
#define PICKAXE_BLAME_MOVE 01
40
#define PICKAXE_BLAME_COPY 02
@@ -78,6 +77,11 @@ struct blame_entry {
77
* suspect's file; internally all line numbers are 0 based.
78
*/
79
int s_lno;
80
+
81
+ /* how significant this entry is -- cached to avoid
82
+ * scanning the lines over and over
83
+ */
84
+ unsigned score;
85
};
86
87
struct scoreboard {
@@ -215,9 +219,6 @@ static void process_u_diff(void *state_, char *line, unsigned long len)
219
struct chunk *chunk;
220
int off1, off2, len1, len2, num;
221
218
- if (DEBUG)
219
- fprintf(stderr, "%.*s", (int) len, line);
220
-
222
num = state->ret->num;
223
if (len < 4 || line[0] != '@' || line[1] != '@') {
224
if (state->hunk_in_pre_context && line[0] == ' ')
@@ -295,10 +296,6 @@ static struct patch *get_patch(struct origin *parent, struct origin *origin)
296
char *blob_p, *blob_o;
297
struct patch *patch;
298
298
- if (DEBUG) fprintf(stderr, "get patch %.8s %.8s\n",
299
- sha1_to_hex(parent->commit->object.sha1),
300
- sha1_to_hex(origin->commit->object.sha1));
301
-
299
blob_p = read_sha1_file(parent->blob_sha1, type,
300
(unsigned long *) &file_p.size);
301
blob_o = read_sha1_file(origin->blob_sha1, type,
@@ -352,6 +349,7 @@ static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
349
memcpy(dst, src, sizeof(*src));
350
dst->prev = p;
351
dst->next = n;
352
+ dst->score = 0;
353
}
354
355
static const char *nth_line(struct scoreboard *sb, int lno)
@@ -448,7 +446,7 @@ static void split_blame(struct scoreboard *sb,
446
add_blame_entry(sb, new_entry);
447
}
448
451
- if (DEBUG) {
449
+ if (1) { /* sanity */
450
struct blame_entry *ent;
451
int lno = 0, corrupt = 0;
452
@@ -530,12 +528,6 @@ static int pass_blame_to_parent(struct scoreboard *sb,
528
for (i = 0; i < patch->num; i++) {
529
struct chunk *chunk = &patch->chunks[i];
530
533
- if (DEBUG)
534
- fprintf(stderr,
535
- "plno = %d, tlno = %d, "
536
- "same as parent up to %d, resync %d and %d\n",
537
- plno, tlno,
538
- chunk->same, chunk->p_next, chunk->t_next);
531
blame_chunk(sb, tlno, plno, chunk->same, target, parent);
532
plno = chunk->p_next;
533
tlno = chunk->t_next;
@@ -547,14 +539,37 @@ static int pass_blame_to_parent(struct scoreboard *sb,
539
return 0;
540
}
541
550
-static void copy_split_if_better(struct blame_entry best_so_far[3],
542
+static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
543
+{
544
+ unsigned score;
545
+ const char *cp, *ep;
546
+
547
+ if (e->score)
548
+ return e->score;
549
+
550
+ score = 0;
551
+ cp = nth_line(sb, e->lno);
552
+ ep = nth_line(sb, e->lno + e->num_lines);
553
+ while (cp < ep) {
554
+ unsigned ch = *((unsigned char *)cp);
555
+ if (isalnum(ch))
556
+ score++;
557
+ cp++;
558
+ }
559
+ e->score = score;
560
+ return score;
561
+}
562
+
563
+static void copy_split_if_better(struct scoreboard *sb,
564
+ struct blame_entry best_so_far[3],
565
struct blame_entry this[3])
566
{
567
if (!this[1].suspect)
568
return;
555
- if (best_so_far[1].suspect &&
556
- (this[1].num_lines < best_so_far[1].num_lines))
557
- return;
569
+ if (best_so_far[1].suspect) {
570
+ if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
571
+ return;
572
+ }
573
memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
574
}
575
@@ -596,7 +611,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
611
tlno + ent->s_lno, plno,
612
chunk->same + ent->s_lno,
613
parent);
599
- copy_split_if_better(split, this);
614
+ copy_split_if_better(sb, split, this);
615
}
616
plno = chunk->p_next;
617
tlno = chunk->t_next;
@@ -699,7 +714,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
714
continue;
715
}
716
find_copy_in_blob(sb, ent, norigin, this, &file_p);
702
- copy_split_if_better(split, this);
717
+ copy_split_if_better(sb, split, this);
718
}
719
if (split[1].suspect)
720
split_blame(sb, split, ent);
@@ -944,6 +959,7 @@ static void get_commit_info(struct commit *commit,
959
#define OUTPUT_PORCELAIN 010
960
#define OUTPUT_SHOW_NAME 020
961
#define OUTPUT_SHOW_NUMBER 040
962
+#define OUTPUT_SHOW_SCORE 0100
963
964
static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
965
{
@@ -1016,6 +1032,8 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1032
show_raw_time),
1033
ent->lno + 1 + cnt);
1034
else {
1035
+ if (opt & OUTPUT_SHOW_SCORE)
1036
+ printf(" %*d", max_score_digits, ent->score);
1037
if (opt & OUTPUT_SHOW_NAME)
1038
printf(" %-*.*s", longest_file, longest_file,
1039
suspect->path);
@@ -1060,8 +1078,9 @@ static void output(struct scoreboard *sb, int option)
1078
for (ent = sb->ent; ent; ent = ent->next) {
1079
if (option & OUTPUT_PORCELAIN)
1080
emit_porcelain(sb, ent);
1063
- else
1081
+ else {
1082
emit_other(sb, ent, option);
1083
+ }
1084
}
1085
}
1086
@@ -1121,6 +1140,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
1140
{
1141
int longest_src_lines = 0;
1142
int longest_dst_lines = 0;
1143
+ unsigned largest_score = 0;
1144
struct blame_entry *e;
1145
1146
for (e = sb->ent; e; e = e->next) {
@@ -1146,9 +1166,12 @@ static void find_alignment(struct scoreboard *sb, int *option)
1166
num = e->lno + e->num_lines;
1167
if (longest_dst_lines < num)
1168
longest_dst_lines = num;
1169
+ if (largest_score < ent_score(sb, e))
1170
+ largest_score = ent_score(sb, e);
1171
}
1172
max_orig_digits = lineno_width(longest_src_lines);
1173
max_digits = lineno_width(longest_dst_lines);
1174
+ max_score_digits = lineno_width(largest_score);
1175
}
1176
1177
static int has_path_in_work_tree(const char *path)
@@ -1209,6 +1232,8 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1232
tmp = top; top = bottom; bottom = tmp;
1233
}
1234
}
1235
+ else if (!strcmp("--score-debug", arg))
1236
+ output_option |= OUTPUT_SHOW_SCORE;
1237
else if (!strcmp("-f", arg) ||
1238
!strcmp("--show-name", arg))
1239
output_option |= OUTPUT_SHOW_NAME;