Skip to content

Commit f143d9c

Browse files
jrngitster
authored andcommitted
userdiff/perl: tighten BEGIN/END block pattern to reject here-doc delimiters
A naive method of treating BEGIN/END blocks with a brace on the second line as diff/grep funcname context involves also matching unrelated lines that consist of all-caps letters: sub foo { print <<'EOF' text goes here ... EOF ... rest of foo ... } That's not so great, because it means that "git diff" and "git grep --show-function" would write "=EOF" or "@@ EOF" as context instead of a more useful reminder like "@@ sub foo {". To avoid this, tighten the pattern to only match the special block names that perl accepts (namely BEGIN, END, INIT, CHECK, UNITCHECK, AUTOLOAD, and DESTROY). The list is taken from perl's toke.c. Suggested-by: Jakub Narebski <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f5b7ce1 commit f143d9c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

t/t4018-diff-funcname.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Beer
2929
}
3030
EOF
3131
sed 's/beer\\/beer,\\/' <Beer.java >Beer-correct.java
32-
cat >Beer.perl <<\EOF
32+
cat >Beer.perl <<\EOT
3333
package Beer;
3434
3535
use strict;
@@ -56,6 +56,15 @@ sub finalround
5656
print "99 bottles of beer on the wall.\n");
5757
}
5858
59+
sub withheredocument {
60+
print <<"EOF"
61+
decoy here-doc
62+
EOF
63+
# some lines of context
64+
# to pad it out
65+
print "hello\n";
66+
}
67+
5968
__END__
6069
6170
=head1 NAME
@@ -76,7 +85,7 @@ Beer - subroutine to output fragment of a drinking song
7685
song;
7786
7887
=cut
79-
EOF
88+
EOT
8089
sed -e '
8190
s/hello/goodbye/
8291
s/beer\\/beer,\\/
@@ -138,6 +147,10 @@ test_expect_success 'perl pattern accepts K&R style brace placement, too' '
138147
test_expect_funcname "sub finalround\$" perl
139148
'
140149

150+
test_expect_success 'but is not distracted by end of <<here document' '
151+
test_expect_funcname "sub withheredocument {\$" perl
152+
'
153+
141154
test_expect_success 'perl pattern is not distracted by sub within POD' '
142155
test_expect_funcname "=head" perl
143156
'

userdiff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PATTERNS("perl",
7474
"(:[^;#]*)?"
7575
"(\\{[ \t]*)?" /* brace can come here or on the next line */
7676
"(#.*)?$\n" /* comment */
77-
"^[A-Z]+[ \t]*" /* BEGIN, END, ... */
77+
"^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
7878
"(\\{[ \t]*)?" /* brace can come here or on the next line */
7979
"(#.*)?$\n"
8080
"^=head[0-9] .*", /* POD */

0 commit comments

Comments
 (0)