interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / lint-man-section-order.perl

125 lines · 2510 bytes Raw Blame History
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6my %SECTIONS;
7{
8 my $order = 0;
9 %SECTIONS = (
10 'NAME' => {
11 required => 1,
12 order => $order++,
13 },
14 'SYNOPSIS' => {
15 required => 1,
16 optional_in_man_sections => {
17 '7' => 1,
18 },
19 order => $order++,
20 },
21 'DESCRIPTION' => {
22 required => 1,
23 order => $order++,
24 },
25 'OPTIONS' => {
26 order => $order++,
27 required => 0,
28 },
29 'CONFIGURATION' => {
30 order => $order++,
31 },
32 'BUGS' => {
33 order => $order++,
34 },
35 'SEE ALSO' => {
36 order => $order++,
37 },
38 'FILE FORMAT' => {
39 order => $order++,
40 },
41 'GIT' => {
42 required => 1,
43 order => $order++,
44 },
45 );
46}
47my $SECTION_RX = do {
48 my ($names) = join "|", keys %SECTIONS;
49 qr/^($names)$/s;
50};
51
52my $exit_code = 0;
53sub report {
54 my ($msg) = @_;
55 print STDERR "$ARGV:$.: $msg\n";
56 $exit_code = 1;
57}
58
59my $man_section_number;
60my $last_was_section;
61my @actual_order;
62while (my $line = <>) {
63 chomp $line;
64
65 if ($. == 1) {
66 $line =~ m/\((\d)\)/;
67 $man_section_number = $1;
68 if (!$man_section_number) {
69 report("first line must be formatted like 'gitfaq(7)'");
70 # exit to avoid dealing with $man_section_number being undefined later
71 last;
72 }
73 }
74
75 if ($line =~ $SECTION_RX) {
76 push @actual_order => $line;
77 $last_was_section = 1;
78 # Have no "last" section yet, processing NAME
79 next if @actual_order == 1;
80
81 my @expected_order = sort {
82 $SECTIONS{$a}->{order} <=> $SECTIONS{$b}->{order}
83 } @actual_order;
84
85 my $expected_last = $expected_order[-2];
86 my $actual_last = $actual_order[-2];
87 if ($actual_last ne $expected_last) {
88 report("section '$line' incorrectly ordered, comes after '$actual_last'");
89 }
90 next;
91 }
92 if ($last_was_section) {
93 my $last_section = $actual_order[-1];
94 if (length $last_section ne length $line) {
95 report("dashes under '$last_section' should match its length!");
96 }
97 if ($line !~ /^-+$/) {
98 report("dashes under '$last_section' should be '-' dashes!");
99 }
100 $last_was_section = 0;
101 }
102
103 if (eof) {
104 # We have both a hash and an array to consider, for
105 # convenience
106 my %actual_sections;
107 @actual_sections{@actual_order} = ();
108
109 for my $section (sort keys %SECTIONS) {
110 next if !$SECTIONS{$section}->{required} or
111 $SECTIONS{$section}->{optional_in_man_sections}->{$man_section_number} or
112 exists $actual_sections{$section};
113 report("has no required '$section' section!");
114 }
115
116 # Reset per-file state
117 {
118 @actual_order = ();
119 # this resets our $. for each file
120 close ARGV;
121 }
122 }
123}
124
125exit $exit_code;