git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 6/7] doc lint: lint relative section order

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 9, 2021, 15:02 UTC
Message-ID
<patch-6.7-8c294afe2a-20210409T145728Z-avarab@gmail.com>
In-Reply-To
<cover-0.7-0000000000-20210409T145728Z-avarab@gmail.com>

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>
---
 Documentation/Makefile                    |   3 +-
 Documentation/lint-man-section-order.perl | 125 ++++++++++++++++++++++
 2 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100755 Documentation/lint-man-section-order.perl
diff --git a/Documentation/Makefile b/Documentation/Makefile
index 34b4f5716a..5e0828869b 100644
--- a/Documentation/Makefile
+++ b/Documentation/Makefile
@@ -483,7 +483,8 @@ lint-docs::
 		--section=1 $(MAN1_TXT) \
 		--section=5 $(MAN5_TXT) \
 		--section=7 $(MAN7_TXT); \
-	$(PERL_PATH) lint-man-end-blurb.perl $(MAN_TXT)
+	$(PERL_PATH) lint-man-end-blurb.perl $(MAN_TXT); \
+	$(PERL_PATH) lint-man-section-order.perl $(MAN_TXT);
 
 ifeq ($(wildcard po/Makefile),po/Makefile)
 doc-l10n install-l10n::
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;
-- 
2.31.1.622.g1b8cc22e65
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 30 of 32 in “small doc make and lint fixes”
  1. 0/5 small doc make and lint fixesÆvar Arnfjörð Bjarmason, Mar 26, 2021
  2. 1/5 Documentation/Makefile: make $(wildcard howto/*.txt) a varÆvar Arnfjörð Bjarmason, Mar 26, 2021
  3. Junio C HamanoMar 28, 2021
  4. 3/5 doc lint: Perl "strict" and "warnings" in lint-gitlink.perlÆvar Arnfjörð Bjarmason, Mar 26, 2021
  5. Junio C HamanoMar 28, 2021
  6. 2/5 Documentation/Makefile: make $(wildcard <doc deps>) a varÆvar Arnfjörð Bjarmason, Mar 26, 2021
  7. Junio C HamanoMar 28, 2021
  8. 5/5 doc lint: lint and fix missing "GIT" end sectionsÆvar Arnfjörð Bjarmason, Mar 26, 2021
  9. Jeff KingMar 26, 2021
  10. Ævar Arnfjörð BjarmasonMar 26, 2021
  11. Jeff KingMar 27, 2021
  12. Junio C HamanoMar 28, 2021
  13. Junio C HamanoMar 28, 2021
  14. Ævar Arnfjörð BjarmasonApr 9, 2021
  15. Junio C HamanoApr 10, 2021
  16. 4/5 doc lint: fix bugs in, simplify and improve lint scriptÆvar Arnfjörð Bjarmason, Mar 26, 2021
  17. Jeff KingMar 26, 2021
  18. Junio C HamanoMar 28, 2021
  19. Philip OakleyMar 26, 2021
  20. Junio C HamanoMar 28, 2021
  21. Junio C HamanoMar 28, 2021
  22. Jeff KingMar 26, 2021
  23. Ævar Arnfjörð BjarmasonMar 26, 2021
  24. Jeff KingMar 27, 2021
  25. 0/7 doc make and lint fixesÆvar Arnfjörð Bjarmason, Apr 9, 2021
  26. 1/7 Documentation/Makefile: make $(wildcard howto/*.txt) a varÆvar Arnfjörð Bjarmason, Apr 9, 2021
  27. 3/7 doc lint: Perl "strict" and "warnings" in lint-gitlink.perlÆvar Arnfjörð Bjarmason, Apr 9, 2021
  28. 2/7 Documentation/Makefile: make doc.dep dependencies a variable againÆvar Arnfjörð Bjarmason, Apr 9, 2021
  29. 4/7 doc lint: fix bugs in, simplify and improve lint scriptÆvar Arnfjörð Bjarmason, Apr 9, 2021
  30. 6/7 doc lint: lint relative section orderÆvar Arnfjörð Bjarmason, Apr 9, 2021
  31. 5/7 doc lint: lint and fix missing "GIT" end sectionsÆvar Arnfjörð Bjarmason, Apr 9, 2021
  32. 7/7 docs: fix linting issues due to incorrect relative section orderÆvar Arnfjörð Bjarmason, Apr 9, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.