aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/lint-man-section-order.perl
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2021-04-09 17:02:49 +0200
committerJunio C Hamano <gitster@pobox.com>2021-04-10 23:36:34 -0700
commitea8b9271b17e64502dc80981cf09cec42739dce7 (patch)
tree7660e38921c7d2d36df523bbefdbfbff260e9869 /Documentation/lint-man-section-order.perl
parentcafd9828e89328fef563e39f3b8e78e9aee74da5 (diff)
downloadgit-ea8b9271b17e64502dc80981cf09cec42739dce7.tar.gz
doc lint: lint relative section order
Add a linting script to check the relative order of the sections in the documentation. We should have NAME, then SYNOPSIS, DESCRIPTION, OPTIONS etc. in that order. That holds true throughout our documentation, except for a few exceptions which are hardcoded in the linting script. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'Documentation/lint-man-section-order.perl')
-rwxr-xr-xDocumentation/lint-man-section-order.perl125
1 files changed, 125 insertions, 0 deletions
diff --git a/Documentation/lint-man-section-order.perl b/Documentation/lint-man-section-order.perl
new file mode 100755
index 0000000000..5767e7e456
--- /dev/null
+++ b/Documentation/lint-man-section-order.perl
@@ -0,0 +1,125 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my %SECTIONS;
+{
+ my $order = 0;
+ %SECTIONS = (
+ 'NAME' => {
+ required => 1,
+ order => $order++,
+ },
+ 'SYNOPSIS' => {
+ required => 1,
+ order => $order++,
+ },
+ 'DESCRIPTION' => {
+ required => 1,
+ order => $order++,
+ bad => {
+ 'git-mktag.txt' => 'OPTIONS',
+ 'git-cvsserver.txt' => 'OPTIONS',
+ },
+ },
+ 'OPTIONS' => {
+ order => $order++,
+ required => 0,
+ bad => {
+ 'git-grep.txt' => 'CONFIGURATION',
+ 'git-rebase.txt' => 'CONFIGURATION',
+ },
+ },
+ 'CONFIGURATION' => {
+ order => $order++,
+ bad => {
+ 'git-svn.txt' => 'BUGS',
+ },
+ },
+ 'BUGS' => {
+ order => $order++,
+ },
+ 'SEE ALSO' => {
+ order => $order++,
+ },
+ 'GIT' => {
+ required => 1,
+ order => $order++,
+ },
+ );
+}
+my $SECTION_RX = do {
+ my ($names) = join "|", keys %SECTIONS;
+ qr/^($names)$/s;
+};
+
+my $exit_code = 0;
+sub report {
+ my ($msg) = @_;
+ print "$ARGV:$.: $msg\n";
+ $exit_code = 1;
+}
+
+my $last_was_section;
+my @actual_order;
+while (my $line = <>) {
+ chomp $line;
+ if ($line =~ $SECTION_RX) {
+ push @actual_order => $line;
+ $last_was_section = 1;
+ # Have no "last" section yet, processing NAME
+ next if @actual_order == 1;
+
+ my @expected_order = sort {
+ $SECTIONS{$a}->{order} <=> $SECTIONS{$b}->{order}
+ } @actual_order;
+
+ my $expected_last = $expected_order[-2];
+ my $actual_last = $actual_order[-2];
+ my $except_last = $SECTIONS{$line}->{bad}->{$ARGV} || '';
+ if (($SECTIONS{$line}->{bad}->{$ARGV} || '') eq $actual_last) {
+ # Either we're whitelisted, or ...
+ next
+ } elsif (exists $SECTIONS{$actual_last}->{bad}->{$ARGV}) {
+ # ... we're complaing about the next section
+ # which is out of order because this one is,
+ # don't complain about that one.
+ next;
+ } elsif ($actual_last ne $expected_last) {
+ report("section '$line' incorrectly ordered, comes after '$actual_last'");
+ }
+ next;
+ }
+ if ($last_was_section) {
+ my $last_section = $actual_order[-1];
+ if (length $last_section ne length $line) {
+ report("dashes under '$last_section' should match its length!");
+ }
+ if ($line !~ /^-+$/) {
+ report("dashes under '$last_section' should be '-' dashes!");
+ }
+ $last_was_section = 0;
+ }
+
+ if (eof) {
+ # We have both a hash and an array to consider, for
+ # convenience
+ my %actual_sections;
+ @actual_sections{@actual_order} = ();
+
+ for my $section (sort keys %SECTIONS) {
+ next if !$SECTIONS{$section}->{required} or exists $actual_sections{$section};
+ report("has no required '$section' section!");
+ }
+
+ # Reset per-file state
+ {
+ @actual_order = ();
+ # this resets our $. for each file
+ close ARGV;
+ }
+ }
+}
+
+exit $exit_code;