summaryrefslogtreecommitdiff
path: root/Documentation/lint-gitlink.perl
blob: b22a367844a219a4206d5a96371eb60920268385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl

use strict;
use warnings;

# Parse arguments, a simple state machine for input like:
#
# howto/*.txt config/*.txt --section=1 git.txt git-add.txt [...] --to-lint git-add.txt a-file.txt [...]
my %TXT;
my %SECTION;
my $section;
my $lint_these = 0;
for my $arg (@ARGV) {
	if (my ($sec) = $arg =~ /^--section=(\d+)$/s) {
		$section = $sec;
		next;
	}

	my ($name) = $arg =~ /^(.*?)\.txt$/s;
	unless (defined $section) {
		$TXT{$name} = $arg;
		next;
	}

	$SECTION{$name} = $section;
}

my $exit_code = 0;
sub report {
	my ($pos, $line, $target, $msg) = @_;
	substr($line, $pos) = "' <-- HERE";
	$line =~ s/^\s+//;
	print "$ARGV:$.: error: $target: $msg, shown with 'HERE' below:\n";
	print "$ARGV:$.:\t'$line\n";
	$exit_code = 1;
}

@ARGV = sort values %TXT;
die "BUG: Nothing to process!" unless @ARGV;
while (<>) {
	my $line = $_;
	while ($line =~ m/linkgit:((.*?)\[(\d)\])/g) {
		my $pos = pos $line;
		my ($target, $page, $section) = ($1, $2, $3);

		# De-AsciiDoc
		$page =~ s/{litdd}/--/g;

		if (!exists $TXT{$page}) {
			report($pos, $line, $target, "link outside of our own docs");
			next;
		}
		if (!exists $SECTION{$page}) {
			report($pos, $line, $target, "link outside of our sectioned docs");
			next;
		}
		my $real_section = $SECTION{$page};
		if ($section != $SECTION{$page}) {
			report($pos, $line, $target, "wrong section (should be $real_section)");
			next;
		}
	}
	# this resets our $. for each file
	close ARGV if eof;
}

exit $exit_code;