Merge branch 'ma/test-quote-cleanup'
[git] / Documentation / cmd-list.perl
1 #!/usr/bin/perl -w
2
3 use File::Compare qw(compare);
4
5 sub format_one {
6         my ($out, $nameattr) = @_;
7         my ($name, $attr) = @$nameattr;
8         my ($state, $description);
9         my $mansection;
10         $state = 0;
11         open I, '<', "$name.txt" or die "No such file $name.txt";
12         while (<I>) {
13                 if (/^git[a-z0-9-]*\(([0-9])\)$/) {
14                         $mansection = $1;
15                         next;
16                 }
17                 if (/^NAME$/) {
18                         $state = 1;
19                         next;
20                 }
21                 if ($state == 1 && /^----$/) {
22                         $state = 2;
23                         next;
24                 }
25                 next if ($state != 2);
26                 chomp;
27                 $description = $_;
28                 last;
29         }
30         close I;
31         if (!defined $description) {
32                 die "No description found in $name.txt";
33         }
34         if (my ($verify_name, $text) = ($description =~ /^($name) - (.*)/)) {
35                 print $out "linkgit:$name\[$mansection\]::\n\t";
36                 if ($attr =~ / deprecated /) {
37                         print $out "(deprecated) ";
38                 }
39                 print $out "$text.\n\n";
40         }
41         else {
42                 die "Description does not match $name: $description";
43         }
44 }
45
46 my ($input, @categories) = @ARGV;
47
48 open IN, "<$input";
49 while (<IN>) {
50         last if /^### command list/;
51 }
52
53 my %cmds = ();
54 for (sort <IN>) {
55         next if /^#/;
56
57         chomp;
58         my ($name, $cat, $attr) = /^(\S+)\s+(.*?)(?:\s+(.*))?$/;
59         $attr = '' unless defined $attr;
60         push @{$cmds{$cat}}, [$name, " $attr "];
61 }
62 close IN;
63
64 for my $out (@categories) {
65         my ($cat) = $out =~ /^cmds-(.*)\.txt$/;
66         open O, '>', "$out+" or die "Cannot open output file $out+";
67         for (@{$cmds{$cat}}) {
68                 format_one(\*O, $_);
69         }
70         close O;
71
72         if (-f "$out" && compare("$out", "$out+") == 0) {
73                 unlink "$out+";
74         }
75         else {
76                 print STDERR "$out\n";
77                 rename "$out+", "$out";
78         }
79 }