userdiff/perl: anchor "sub" and "package" patterns on the left

The userdiff funcname mechanism has no concept of nested scopes --- instead, "git diff" and "git grep --show-function" simply label the diff header with the most recent matching line. Unfortunately that means text following a subroutine in a POD section: =head1 DESCRIPTION You might use this facility like so: sub example { foo; } Now, having said that, let's say more about the facility. Blah blah blah ... etc etc. gets the subroutine name instead of the POD header in its diff/grep funcname header, making it harder to get oriented when reading a diff without enough context. The fix is simple: anchor the funcname syntax to the left margin so nested subroutines and packages like this won't get picked up. (The builtin C++ funcname pattern already does the same thing.) This means the userdiff driver will misparse the idiom { my $static; sub foo { ... use $static ... } } but I think that's worth it; we can revisit this later if the userdiff mechanism learns to keep track of the beginning and end of nested scopes. Reported-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed May 21, 2011 at 14:29 UTC f12c66b9bb851aa7350d40370e6adf78535c5930
2 files changed +58 -5
t/t4018-diff-funcname.sh
+56 -3
@@ -29,6 +29,47 @@ public class Beer
29 }
30 EOF
31 sed 's/beer\\/beer,\\/' <Beer.java >Beer-correct.java
32 +cat >Beer.perl <<\EOF
33 +package Beer;
34 +
35 +use strict;
36 +use warnings;
37 +use parent qw(Exporter);
38 +our @EXPORT_OK = qw(round);
39 +
40 +sub round {
41 + my ($n) = @_;
42 + print "$n bottles of beer on the wall ";
43 + print "$n bottles of beer\n";
44 + print "Take one down, pass it around, ";
45 + $n = $n - 1;
46 + print "$n bottles of beer on the wall.\n";
47 +}
48 +
49 +__END__
50 +
51 +=head1 NAME
52 +
53 +Beer - subroutine to output fragment of a drinking song
54 +
55 +=head1 SYNOPSIS
56 +
57 + use Beer qw(round);
58 +
59 + sub song {
60 + for (my $i = 99; $i > 0; $i--) {
61 + round $i;
62 + }
63 + }
64 +
65 + song;
66 +
67 +=cut
68 +EOF
69 +sed -e '
70 + s/beer\\/beer,\\/
71 + s/song;/song();/
72 +' <Beer.perl >Beer-correct.perl
73
74 test_config () {
75 git config "$1" "$2" &&
@@ -36,8 +77,9 @@ test_config () {
77 }
78
79 test_expect_funcname () {
39 - test_expect_code 1 git diff --no-index \
40 - Beer.java Beer-correct.java >diff &&
80 + lang=${2-java}
81 + test_expect_code 1 git diff --no-index -U1 \
82 + "Beer.$lang" "Beer-correct.$lang" >diff &&
83 grep "^@@.*@@ $1" diff
84 }
85
@@ -65,13 +107,24 @@ test_expect_success 'default behaviour' '
107 '
108
109 test_expect_success 'set up .gitattributes declaring drivers to test' '
68 - echo "*.java diff=java" >.gitattributes
110 + cat >.gitattributes <<-\EOF
111 + *.java diff=java
112 + *.perl diff=perl
113 + EOF
114 '
115
116 test_expect_success 'preset java pattern' '
117 test_expect_funcname "public static void main("
118 '
119
120 +test_expect_success 'preset perl pattern' '
121 + test_expect_funcname "sub round {\$" perl
122 +'
123 +
124 +test_expect_success 'perl pattern is not distracted by sub within POD' '
125 + test_expect_funcname "=head" perl
126 +'
127 +
128 test_expect_success 'custom pattern' '
129 test_config diff.java.funcname "!static
130 !String
userdiff.c
+2 -2
@@ -60,8 +60,8 @@ PATTERNS("pascal",
60 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
61 "|<>|<=|>=|:=|\\.\\."),
62 PATTERNS("perl",
63 - "^[ \t]*package .*;\n"
64 - "^[ \t]*sub .* \\{\n"
63 + "^package .*;\n"
64 + "^sub .* \\{\n"
65 "^[A-Z]+ \\{\n" /* BEGIN, END, ... */
66 "^=head[0-9] ", /* POD */
67 /* -- */