]> git.ipfire.org Git - thirdparty/git.git/blob - Documentation/lint-gitlink.perl
CodingGuidelines: quote assigned value in 'local var=$val'
[thirdparty/git.git] / Documentation / lint-gitlink.perl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 # Parse arguments, a simple state machine for input like:
7 #
8 # <file-to-check.txt> <valid-files-to-link-to> --section=1 git.txt git-add.txt [...] --to-lint git-add.txt a-file.txt [...]
9 my %TXT;
10 my %SECTION;
11 my $section;
12 my $lint_these = 0;
13 my $to_check = shift @ARGV;
14 for my $arg (@ARGV) {
15 if (my ($sec) = $arg =~ /^--section=(\d+)$/s) {
16 $section = $sec;
17 next;
18 }
19
20 my ($name) = $arg =~ /^(.*?)\.txt$/s;
21 unless (defined $section) {
22 $TXT{$name} = $arg;
23 next;
24 }
25
26 $SECTION{$name} = $section;
27 }
28
29 my $exit_code = 0;
30 sub report {
31 my ($pos, $line, $target, $msg) = @_;
32 substr($line, $pos) = "' <-- HERE";
33 $line =~ s/^\s+//;
34 print STDERR "$ARGV:$.: error: $target: $msg, shown with 'HERE' below:\n";
35 print STDERR "$ARGV:$.:\t'$line\n";
36 $exit_code = 1;
37 }
38
39 @ARGV = sort values %TXT;
40 die "BUG: No list of valid linkgit:* files given" unless @ARGV;
41 @ARGV = $to_check;
42 while (<>) {
43 my $line = $_;
44 while ($line =~ m/linkgit:((.*?)\[(\d)\])/g) {
45 my $pos = pos $line;
46 my ($target, $page, $section) = ($1, $2, $3);
47
48 # De-AsciiDoc
49 $page =~ s/{litdd}/--/g;
50
51 if (!exists $TXT{$page}) {
52 report($pos, $line, $target, "link outside of our own docs");
53 next;
54 }
55 if (!exists $SECTION{$page}) {
56 report($pos, $line, $target, "link outside of our sectioned docs");
57 next;
58 }
59 my $real_section = $SECTION{$page};
60 if ($section != $SECTION{$page}) {
61 report($pos, $line, $target, "wrong section (should be $real_section)");
62 next;
63 }
64 }
65 # this resets our $. for each file
66 close ARGV if eof;
67 }
68
69 exit $exit_code;