about summary refs log tree commit diff
path: root/third_party/git/Documentation/lint-gitlink.perl
blob: 476cc30b837ecfb25a82406dd0859d04a00c1190 (plain) (blame)
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
68
69
70
71
#!/usr/bin/perl

use File::Find;
use Getopt::Long;

my $basedir = ".";
GetOptions("basedir=s" => \$basedir)
	or die("Cannot parse command line arguments\n");

my $found_errors = 0;

sub report {
	my ($where, $what, $error) = @_;
	print "$where: $error: $what\n";
	$found_errors = 1;
}

sub grab_section {
	my ($page) = @_;
	open my $fh, "<", "$basedir/$page.txt";
	my $firstline = <$fh>;
	chomp $firstline;
	close $fh;
	my ($section) = ($firstline =~ /.*\((\d)\)$/);
	return $section;
}

sub lint {
	my ($file) = @_;
	open my $fh, "<", $file
		or return;
	while (<$fh>) {
		my $where = "$file:$.";
		while (s/linkgit:((.*?)\[(\d)\])//) {
			my ($target, $page, $section) = ($1, $2, $3);

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

			if ($page !~ /^git/) {
				report($where, $target, "nongit link");
				next;
			}
			if (! -f "$basedir/$page.txt") {
				report($where, $target, "no such source");
				next;
			}
			$real_section = grab_section($page);
			if ($real_section != $section) {
				report($where, $target,
					"wrong section (should be $real_section)");
				next;
			}
		}
	}
	close $fh;
}

sub lint_it {
	lint($File::Find::name) if -f && /\.txt$/;
}

if (!@ARGV) {
	find({ wanted => \&lint_it, no_chdir => 1 }, $basedir);
} else {
	for (@ARGV) {
		lint($_);
	}
}

exit $found_errors;