doc lint: Perl "strict" and "warnings" in lint-gitlink.perl
[git] / Documentation / lint-gitlink.perl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use File::Find;
6 use Getopt::Long;
7
8 my $basedir = ".";
9 GetOptions("basedir=s" => \$basedir)
10         or die("Cannot parse command line arguments\n");
11
12 my $found_errors = 0;
13
14 sub report {
15         my ($where, $what, $error) = @_;
16         print "$where: $error: $what\n";
17         $found_errors = 1;
18 }
19
20 sub grab_section {
21         my ($page) = @_;
22         open my $fh, "<", "$basedir/$page.txt";
23         my $firstline = <$fh>;
24         chomp $firstline;
25         close $fh;
26         my ($section) = ($firstline =~ /.*\((\d)\)$/);
27         return $section;
28 }
29
30 sub lint {
31         my ($file) = @_;
32         open my $fh, "<", $file
33                 or return;
34         while (<$fh>) {
35                 my $where = "$file:$.";
36                 while (s/linkgit:((.*?)\[(\d)\])//) {
37                         my ($target, $page, $section) = ($1, $2, $3);
38
39                         # De-AsciiDoc
40                         $page =~ s/{litdd}/--/g;
41
42                         if ($page !~ /^git/) {
43                                 report($where, $target, "nongit link");
44                                 next;
45                         }
46                         if (! -f "$basedir/$page.txt") {
47                                 report($where, $target, "no such source");
48                                 next;
49                         }
50                         my $real_section = grab_section($page);
51                         if ($real_section != $section) {
52                                 report($where, $target,
53                                         "wrong section (should be $real_section)");
54                                 next;
55                         }
56                 }
57         }
58         close $fh;
59 }
60
61 sub lint_it {
62         lint($File::Find::name) if -f && /\.txt$/;
63 }
64
65 if (!@ARGV) {
66         find({ wanted => \&lint_it, no_chdir => 1 }, $basedir);
67 } else {
68         for (@ARGV) {
69                 lint($_);
70         }
71 }
72
73 exit $found_errors;