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