5757my $file_emails = 0;
5858my $from_filename = 0;
5959my $pattern_depth = 0;
60- my $self_test = 0 ;
60+ my $self_test = undef ;
6161my $version = 0;
6262my $help = 0;
6363my $find_maintainer_files = 0;
221221
222222if ($#ARGV > 0) {
223223 foreach (@ARGV ) {
224- if ($_ eq " - self-test" || $_ eq " --self-test " ) {
224+ if ($_ =~ / ^-{1,2} self-test(?:=| $) / ) {
225225 die " $P : using --self-test does not allow any other option or argument\n " ;
226226 }
227227 }
263263 ' fe|file-emails!' => \$file_emails ,
264264 ' f|file' => \$from_filename ,
265265 ' find-maintainer-files' => \$find_maintainer_files ,
266- ' self-test' => \$self_test ,
266+ ' self-test:s ' => \$self_test ,
267267 ' v|version' => \$version ,
268268 ' h|help|usage' => \$help ,
269269 )) {
280280 exit 0;
281281}
282282
283- if ($self_test ) {
283+ if (defined $self_test ) {
284284 read_all_maintainer_files();
285- check_maintainers_patterns ();
285+ self_test ();
286286 exit 0;
287287}
288288
329329my @typevalue = ();
330330my %keyword_hash ;
331331my @mfiles = ();
332- my @self_test_pattern_info = ();
332+ my @self_test_info = ();
333333
334334sub read_maintainer_file {
335335 my ($file ) = @_ ;
@@ -339,6 +339,7 @@ sub read_maintainer_file {
339339 my $i = 1;
340340 while (<$maint >) {
341341 my $line = $_ ;
342+ chomp $line ;
342343
343344 if ($line =~ m / ^([A-Z]):\s *(.*)/ ) {
344345 my $type = $1 ;
@@ -353,17 +354,16 @@ sub read_maintainer_file {
353354 if ((-d $value )) {
354355 $value =~ s @ ([^/])$@ $1 /@ ;
355356 }
356- if ($self_test ) {
357- push (@self_test_pattern_info , {file => $file , line => $line , linenr => $i , pat => $value });
358- }
359357 } elsif ($type eq " K" ) {
360358 $keyword_hash {@typevalue } = $value ;
361359 }
362360 push (@typevalue , " $type :$value " );
363361 } elsif (!(/ ^\s *$ / || / ^\s *\# / )) {
364- $line =~ s /\n $// g ;
365362 push (@typevalue , $line );
366363 }
364+ if (defined $self_test ) {
365+ push (@self_test_info , {file => $file , linenr => $i , line => $line });
366+ }
367367 $i ++;
368368 }
369369 close ($maint );
@@ -614,17 +614,132 @@ sub read_mailmap {
614614
615615exit ($exit );
616616
617- sub check_maintainers_patterns {
617+ sub self_test {
618618 my @lsfiles = ();
619+ my @good_links = ();
620+ my @bad_links = ();
621+ my @section_headers = ();
622+ my $index = 0;
619623
620624 @lsfiles = vcs_list_files($lk_path );
621625
622- for my $x (@self_test_pattern_info ) {
623- if (!grep (m @ ^$x ->{pat}@ , @lsfiles )) {
624- my $line = $x -> {line };
625- chomp ($line );
626- print (" $x ->{file}:$x ->{linenr}: warning: no matches $line \n " );
627- }
626+ for my $x (@self_test_info ) {
627+ $index ++;
628+
629+ # # Section header duplication and missing section content
630+ if (($self_test eq " " || $self_test =~ / \b sections\b / ) &&
631+ $x -> {line } =~ / ^\S [^:]/ &&
632+ defined $self_test_info [$index ] &&
633+ $self_test_info [$index ]-> {line } =~ / ^([A-Z]):\s *\S / ) {
634+ my $has_S = 0;
635+ my $has_F = 0;
636+ my $has_ML = 0;
637+ my $status = " " ;
638+ if (grep (m @ ^\Q $x ->{line}\E @ , @section_headers )) {
639+ print (" $x ->{file}:$x ->{linenr}: warning: duplicate section header\t $x ->{line}\n " );
640+ } else {
641+ push (@section_headers , $x -> {line });
642+ }
643+ my $nextline = $index ;
644+ while (defined $self_test_info [$nextline ] &&
645+ $self_test_info [$nextline ]-> {line } =~ / ^([A-Z]):\s *(\S .*)/ ) {
646+ my $type = $1 ;
647+ my $value = $2 ;
648+ if ($type eq " S" ) {
649+ $has_S = 1;
650+ $status = $value ;
651+ } elsif ($type eq " F" || $type eq " N" ) {
652+ $has_F = 1;
653+ } elsif ($type eq " M" || $type eq " R" || $type eq " L" ) {
654+ $has_ML = 1;
655+ }
656+ $nextline ++;
657+ }
658+ if (!$has_ML && $status !~ / orphan|obsolete/i ) {
659+ print (" $x ->{file}:$x ->{linenr}: warning: section without email address\t $x ->{line}\n " );
660+ }
661+ if (!$has_S ) {
662+ print (" $x ->{file}:$x ->{linenr}: warning: section without status \t $x ->{line}\n " );
663+ }
664+ if (!$has_F ) {
665+ print (" $x ->{file}:$x ->{linenr}: warning: section without file pattern\t $x ->{line}\n " );
666+ }
667+ }
668+
669+ next if ($x -> {line } !~ / ^([A-Z]):\s *(.*)/ );
670+
671+ my $type = $1 ;
672+ my $value = $2 ;
673+
674+ # # Filename pattern matching
675+ if (($type eq " F" || $type eq " X" ) &&
676+ ($self_test eq " " || $self_test =~ / \b patterns\b / )) {
677+ $value =~ s @\. @ \\\. @ g ; # #Convert . to \.
678+ $value =~ s /\* / \.\* / g ; # #Convert * to .*
679+ $value =~ s /\? / \. / g ; # #Convert ? to .
680+ # #if pattern is a directory and it lacks a trailing slash, add one
681+ if ((-d $value )) {
682+ $value =~ s @ ([^/])$@ $1 /@ ;
683+ }
684+ if (!grep (m @ ^$value @ , @lsfiles )) {
685+ print (" $x ->{file}:$x ->{linenr}: warning: no file matches\t $x ->{line}\n " );
686+ }
687+
688+ # # Link reachability
689+ } elsif (($type eq " W" || $type eq " Q" || $type eq " B" ) &&
690+ $value =~ / ^https?:/ &&
691+ ($self_test eq " " || $self_test =~ / \b links\b / )) {
692+ next if (grep (m @ ^\Q $value \E $ @ , @good_links ));
693+ my $isbad = 0;
694+ if (grep (m @ ^\Q $value \E $ @ , @bad_links )) {
695+ $isbad = 1;
696+ } else {
697+ my $output = ` wget --spider -q --no-check-certificate --timeout 10 --tries 1 $value ` ;
698+ if ($? == 0) {
699+ push (@good_links , $value );
700+ } else {
701+ push (@bad_links , $value );
702+ $isbad = 1;
703+ }
704+ }
705+ if ($isbad ) {
706+ print (" $x ->{file}:$x ->{linenr}: warning: possible bad link\t $x ->{line}\n " );
707+ }
708+
709+ # # SCM reachability
710+ } elsif ($type eq " T" &&
711+ ($self_test eq " " || $self_test =~ / \b scm\b / )) {
712+ next if (grep (m @ ^\Q $value \E $ @ , @good_links ));
713+ my $isbad = 0;
714+ if (grep (m @ ^\Q $value \E $ @ , @bad_links )) {
715+ $isbad = 1;
716+ } elsif ($value !~ / ^(?:git|quilt|hg)\s +\S / ) {
717+ print (" $x ->{file}:$x ->{linenr}: warning: malformed entry\t $x ->{line}\n " );
718+ } elsif ($value =~ / ^git\s +(\S +)(\s +([^\( ]+\S +))?/ ) {
719+ my $url = $1 ;
720+ my $branch = " " ;
721+ $branch = $3 if $3 ;
722+ my $output = ` git ls-remote --exit-code -h "$url " $branch > /dev/null 2>&1` ;
723+ if ($? == 0) {
724+ push (@good_links , $value );
725+ } else {
726+ push (@bad_links , $value );
727+ $isbad = 1;
728+ }
729+ } elsif ($value =~ / ^(?:quilt|hg)\s +(https?:\S +)/ ) {
730+ my $url = $1 ;
731+ my $output = ` wget --spider -q --no-check-certificate --timeout 10 --tries 1 $url ` ;
732+ if ($? == 0) {
733+ push (@good_links , $value );
734+ } else {
735+ push (@bad_links , $value );
736+ $isbad = 1;
737+ }
738+ }
739+ if ($isbad ) {
740+ print (" $x ->{file}:$x ->{linenr}: warning: possible bad link\t $x ->{line}\n " );
741+ }
742+ }
628743 }
629744}
630745
0 commit comments