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