Skip to content

Commit f12c66b

Browse files
jrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d64d6cd commit f12c66b

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

t/t4018-diff-funcname.sh

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,57 @@ public class Beer
2929
}
3030
EOF
3131
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
3273

3374
test_config () {
3475
git config "$1" "$2" &&
3576
test_when_finished "git config --unset $1"
3677
}
3778

3879
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 &&
4183
grep "^@@.*@@ $1" diff
4284
}
4385

@@ -65,13 +107,24 @@ test_expect_success 'default behaviour' '
65107
'
66108

67109
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
69114
'
70115

71116
test_expect_success 'preset java pattern' '
72117
test_expect_funcname "public static void main("
73118
'
74119

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+
75128
test_expect_success 'custom pattern' '
76129
test_config diff.java.funcname "!static
77130
!String

userdiff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ PATTERNS("pascal",
6060
"|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
6161
"|<>|<=|>=|:=|\\.\\."),
6262
PATTERNS("perl",
63-
"^[ \t]*package .*;\n"
64-
"^[ \t]*sub .* \\{\n"
63+
"^package .*;\n"
64+
"^sub .* \\{\n"
6565
"^[A-Z]+ \\{\n" /* BEGIN, END, ... */
6666
"^=head[0-9] ", /* POD */
6767
/* -- */

0 commit comments

Comments
 (0)