git/Documentation/cmd-list.perl
Philippe Blain f442f28a81 git.txt: add list of guides
Not all man5/man7 guides are mentioned in the 'git(1)' documentation,
which makes the missing ones somewhat hard to find.

Add a list of the guides to git(1) by leveraging the existing
`Documentation/cmd-list.perl` script to generate a file `cmds-guide.txt`
which gets included in git.txt.

Also, do not hard-code the manual section '1'. Instead, use a regex so
that the manual section is discovered from the first line of each
`git*.txt` file.

This addition was hinted at in 1b81d8cb19 (help: use command-list.txt
for the source of guides, 2018-05-20).

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-08-04 18:34:02 -07:00

79 lines
1.5 KiB
Perl
Executable file

#!/usr/bin/perl -w
use File::Compare qw(compare);
sub format_one {
my ($out, $nameattr) = @_;
my ($name, $attr) = @$nameattr;
my ($state, $description);
my $mansection;
$state = 0;
open I, '<', "$name.txt" or die "No such file $name.txt";
while (<I>) {
if (/^git[a-z0-9-]*\(([0-9])\)$/) {
$mansection = $1;
next;
}
if (/^NAME$/) {
$state = 1;
next;
}
if ($state == 1 && /^----$/) {
$state = 2;
next;
}
next if ($state != 2);
chomp;
$description = $_;
last;
}
close I;
if (!defined $description) {
die "No description found in $name.txt";
}
if (my ($verify_name, $text) = ($description =~ /^($name) - (.*)/)) {
print $out "linkgit:$name\[$mansection\]::\n\t";
if ($attr =~ / deprecated /) {
print $out "(deprecated) ";
}
print $out "$text.\n\n";
}
else {
die "Description does not match $name: $description";
}
}
my ($input, @categories) = @ARGV;
open IN, "<$input";
while (<IN>) {
last if /^### command list/;
}
my %cmds = ();
for (sort <IN>) {
next if /^#/;
chomp;
my ($name, $cat, $attr) = /^(\S+)\s+(.*?)(?:\s+(.*))?$/;
$attr = '' unless defined $attr;
push @{$cmds{$cat}}, [$name, " $attr "];
}
close IN;
for my $out (@categories) {
my ($cat) = $out =~ /^cmds-(.*)\.txt$/;
open O, '>', "$out+" or die "Cannot open output file $out+";
for (@{$cmds{$cat}}) {
format_one(\*O, $_);
}
close O;
if (-f "$out" && compare("$out", "$out+") == 0) {
unlink "$out+";
}
else {
print STDERR "$out\n";
rename "$out+", "$out";
}
}