checkpatch: refine mode selection
authorPaolo Bonzini <pbonzini@redhat.com>
Wed, 4 Oct 2017 14:35:53 +0000 (16:35 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Thu, 12 Oct 2017 10:10:37 +0000 (12:10 +0200)
stgit produces patch files that lack the ".patch" extensions.  Others
might be using ".diff" too.  But since we are already limiting source files
to only a handful of extensions, we can reuse that in the mode selection
code.

While at it, do not match "../foo" as a branch name.

Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
scripts/checkpatch.pl

index 0c41f1212f2c6c9baec788046014753ac9c23d9f..ce9f08eb9f5d13da8718408bf49f2b12f562efc9 100755 (executable)
@@ -11,6 +11,8 @@ use warnings;
 my $P = $0;
 $P =~ s@.*/@@g;
 
+our $SrcFile    = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$};
+
 my $V = '0.31';
 
 use Getopt::Long qw(:config no_auto_abbrev);
@@ -101,30 +103,29 @@ if ($#ARGV < 0) {
 }
 
 if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
-       $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
-       $chk_patch = $chk_branch ? 0 :
-               $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ? 1 : 0;
-       $file = $chk_branch || $chk_patch ? 0 : 1;
+       $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
+       $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
+       $chk_patch = $chk_branch || $file ? 0 : 1;
 } elsif (!defined $chk_branch && !defined $chk_patch) {
        if ($file) {
                $chk_branch = $chk_patch = 0;
        } else {
-               $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
+               $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
                $chk_patch = $chk_branch ? 0 : 1;
        }
 } elsif (!defined $chk_branch && !defined $file) {
        if ($chk_patch) {
                $chk_branch = $file = 0;
        } else {
-               $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
+               $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0;
                $file = $chk_branch ? 0 : 1;
        }
 } elsif (!defined $chk_patch && !defined $file) {
        if ($chk_branch) {
                $chk_patch = $file = 0;
        } else {
-               $chk_patch = $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ? 1 : 0;
-               $file = $chk_patch ? 0 : 1;
+               $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0;
+               $chk_patch = $file ? 0 : 1;
        }
 } elsif (!defined $chk_branch) {
        $chk_branch = $chk_patch || $file ? 0 : 1;
@@ -1443,7 +1444,7 @@ sub process {
                }
 
 # check we are in a valid source file if not then ignore this hunk
-               next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
+               next if ($realfile !~ /$SrcFile/);
 
 #90 column limit
                if ($line =~ /^\+/ &&