add core boilerplate to three new Test::Simple tests
[p5sagit/p5-mst-13.2.git] / Porting / Maintainers.pm
CommitLineData
0cf51544 1#
2# Maintainers.pm - show information about maintainers
3#
4
5package Maintainers;
6
7use strict;
8
9use lib "Porting";
357244ac 10# Please don't use post 5.008 features as this module is used by
11# Porting/makemeta, and that in turn has to be run by the perl just built.
12use 5.008;
0cf51544 13
14require "Maintainers.pl";
15use vars qw(%Modules %Maintainers);
16
d8528f07 17use vars qw(@ISA @EXPORT_OK $VERSION);
0cf51544 18@ISA = qw(Exporter);
19@EXPORT_OK = qw(%Modules %Maintainers
20 get_module_files get_module_pat
da92fd60 21 show_results process_options files_to_modules
22 reload_manifest);
23$VERSION = 0.03;
0cf51544 24require Exporter;
25
26use File::Find;
27use Getopt::Long;
28
29my %MANIFEST;
da92fd60 30
31# (re)read the MANIFEST file, blowing away any previous effort
32
33sub reload_manifest {
34 %MANIFEST = ();
35 if (open(MANIFEST, "MANIFEST")) {
36 while (<MANIFEST>) {
37 if (/^(\S+)/) {
38 $MANIFEST{$1}++;
39 }
40 else {
41 warn "MANIFEST:$.: malformed line: $_\n";
42 }
0cf51544 43 }
da92fd60 44 close MANIFEST;
45 } else {
46 die "$0: Failed to open MANIFEST for reading: $!\n";
0cf51544 47 }
0cf51544 48}
49
da92fd60 50reload_manifest;
51
52
0cf51544 53sub get_module_pat {
54 my $m = shift;
55 split ' ', $Modules{$m}{FILES};
56}
57
adcdf46b 58# exand dir/ or foo* into a full list of files
59#
60sub expand_glob {
61 sort { lc $a cmp lc $b }
62 map {
63 -f $_ ? # File as-is.
64 $_ :
65 -d _ ? # Recurse into directories.
66 do {
67 my @files;
68 find(
69 sub {
70 push @files, $File::Find::name
71 if -f $_ && exists $MANIFEST{$File::Find::name};
72 }, $_);
73 @files;
74 }
75 # The rest are globbable patterns; expand the glob, then
76 # recurively perform directory expansion on any results
77 : expand_glob(grep -e $_,glob($_))
78 } @_;
79}
80
0cf51544 81sub get_module_files {
82 my $m = shift;
adcdf46b 83 my %exclude;
84 my @files;
85 for (get_module_pat($m)) {
86 if (s/^!//) {
87 $exclude{$_}=1 for expand_glob($_);
88 }
89 else {
90 push @files, expand_glob($_);
91 }
92 }
93 return grep !$exclude{$_}, @files;
0cf51544 94}
95
adcdf46b 96
0cf51544 97sub get_maintainer_modules {
98 my $m = shift;
99 sort { lc $a cmp lc $b }
100 grep { $Modules{$_}{MAINTAINER} eq $m }
101 keys %Modules;
102}
103
104sub usage {
105 print <<__EOF__;
29638d28 106$0: Usage: $0 [[--maintainer M --module M --files]|[--check] [commit] | [file ...]
0cf51544 107--maintainer M list all maintainers matching M
108--module M list all modules matching M
109--files list all files
678b26d7 110--check check consistency of Maintainers.pl
3428fdd5 111 with a file checks if it has a maintainer
112 with a dir checks all files have a maintainer
113 otherwise checks for multiple maintainers
bfca551d 114--checkmani like --check, but only reports on unclaimed files if they
115 are in MANIFEST
1be1464a 116--opened list all modules of modified files
0cf51544 117Matching is case-ignoring regexp, author matching is both by
118the short id and by the full name and email. A "module" may
119not be just a module, it may be a file or files or a subdirectory.
120The options may be abbreviated to their unique prefixes
121__EOF__
122 exit(0);
123}
124
125my $Maintainer;
126my $Module;
127my $Files;
678b26d7 128my $Check;
bfca551d 129my $Checkmani;
d933dc9e 130my $Opened;
0cf51544 131
132sub process_options {
133 usage()
134 unless
135 GetOptions(
136 'maintainer=s' => \$Maintainer,
137 'module=s' => \$Module,
138 'files' => \$Files,
678b26d7 139 'check' => \$Check,
bfca551d 140 'checkmani' => \$Checkmani,
d933dc9e 141 'opened' => \$Opened,
0cf51544 142 );
143
d933dc9e 144 my @Files;
1be1464a 145
d933dc9e 146 if ($Opened) {
fdd40f96 147 chomp (@Files = `git ls-files -m --full-name`);
d933dc9e 148 die if $?;
29638d28 149 } elsif (@ARGV == 1 &&
150 $ARGV[0] =~ /^(?:HEAD|[0-9a-f]{4,40})(?:~\d+)?\^*$/) {
151 my $command = "git diff --name-only $ARGV[0]^ $ARGV[0]";
152 chomp (@Files = `$command`);
153 die "'$command' failed: $?" if $?;
d933dc9e 154 } else {
155 @Files = @ARGV;
156 }
0cf51544 157
158 usage() if @Files && ($Maintainer || $Module || $Files);
159
160 for my $mean ($Maintainer, $Module) {
161 warn "$0: Did you mean '$0 $mean'?\n"
162 if $mean && -e $mean && $mean ne '.' && !$Files;
163 }
164
165 warn "$0: Did you mean '$0 -mo $Maintainer'?\n"
166 if defined $Maintainer && exists $Modules{$Maintainer};
167
168 warn "$0: Did you mean '$0 -ma $Module'?\n"
169 if defined $Module && exists $Maintainers{$Module};
170
171 return ($Maintainer, $Module, $Files, @Files);
172}
173
e1ae7bac 174sub files_to_modules {
175 my @Files = @_;
176 my %ModuleByFile;
177
178 for (@Files) { s:^\./:: }
179
180 @ModuleByFile{@Files} = ();
181
182 # First try fast match.
183
184 my %ModuleByPat;
185 for my $module (keys %Modules) {
186 for my $pat (get_module_pat($module)) {
187 $ModuleByPat{$pat} = $module;
188 }
189 }
190 # Expand any globs.
191 my %ExpModuleByPat;
192 for my $pat (keys %ModuleByPat) {
193 if (-e $pat) {
194 $ExpModuleByPat{$pat} = $ModuleByPat{$pat};
195 } else {
196 for my $exp (glob($pat)) {
197 $ExpModuleByPat{$exp} = $ModuleByPat{$pat};
198 }
199 }
200 }
201 %ModuleByPat = %ExpModuleByPat;
202 for my $file (@Files) {
203 $ModuleByFile{$file} = $ModuleByPat{$file}
204 if exists $ModuleByPat{$file};
205 }
206
207 # If still unresolved files...
208 if (my @ToDo = grep { !defined $ModuleByFile{$_} } keys %ModuleByFile) {
209
210 # Cannot match what isn't there.
211 @ToDo = grep { -e $_ } @ToDo;
212
213 if (@ToDo) {
214 # Try prefix matching.
215
216 # Remove trailing slashes.
217 for (@ToDo) { s|/$|| }
218
219 my %ToDo;
220 @ToDo{@ToDo} = ();
221
222 for my $pat (keys %ModuleByPat) {
223 last unless keys %ToDo;
224 if (-d $pat) {
225 my @Done;
226 for my $file (keys %ToDo) {
227 if ($file =~ m|^$pat|i) {
228 $ModuleByFile{$file} = $ModuleByPat{$pat};
229 push @Done, $file;
230 }
231 }
232 delete @ToDo{@Done};
233 }
234 }
235 }
236 }
237 \%ModuleByFile;
238}
0cf51544 239sub show_results {
240 my ($Maintainer, $Module, $Files, @Files) = @_;
241
242 if ($Maintainer) {
243 for my $m (sort keys %Maintainers) {
244 if ($m =~ /$Maintainer/io || $Maintainers{$m} =~ /$Maintainer/io) {
245 my @modules = get_maintainer_modules($m);
246 if ($Module) {
247 @modules = grep { /$Module/io } @modules;
248 }
249 if ($Files) {
250 my @files;
251 for my $module (@modules) {
252 push @files, get_module_files($module);
253 }
254 printf "%-15s @files\n", $m;
255 } else {
256 if ($Module) {
257 printf "%-15s @modules\n", $m;
258 } else {
259 printf "%-15s $Maintainers{$m}\n", $m;
260 }
261 }
262 }
263 }
264 } elsif ($Module) {
265 for my $m (sort { lc $a cmp lc $b } keys %Modules) {
266 if ($m =~ /$Module/io) {
267 if ($Files) {
268 my @files = get_module_files($m);
269 printf "%-15s @files\n", $m;
270 } else {
adc42316 271 printf "%-15s %-12s %s\n", $m, $Modules{$m}{MAINTAINER}, $Modules{$m}{UPSTREAM}||'unknown';
0cf51544 272 }
273 }
274 }
bfca551d 275 } elsif ($Check or $Checkmani) {
3428fdd5 276 if( @Files ) {
bfca551d 277 missing_maintainers(
278 $Checkmani
279 ? sub { -f $_ and exists $MANIFEST{$File::Find::name} }
280 : sub { /\.(?:[chty]|p[lm]|xs)\z/msx },
281 @Files
282 );
3428fdd5 283 }
284 else {
285 duplicated_maintainers();
286 }
0cf51544 287 } elsif (@Files) {
e1ae7bac 288 my $ModuleByFile = files_to_modules(@Files);
0cf51544 289 for my $file (@Files) {
e1ae7bac 290 if (defined $ModuleByFile->{$file}) {
291 my $module = $ModuleByFile->{$file};
292 my $maintainer = $Modules{$ModuleByFile->{$file}}{MAINTAINER};
c5654d5b 293 my $upstream = $Modules{$module}{UPSTREAM}||'unknown';
294 printf "%-15s [%-7s] $module $maintainer $Maintainers{$maintainer}\n", $file, $upstream;
0cf51544 295 } else {
296 printf "%-15s ?\n", $file;
297 }
298 }
299 }
fdd40f96 300 elsif ($Opened) {
f340d83a 301 print STDERR "(No files are modified)\n";
fdd40f96 302 }
0cf51544 303 else {
304 usage();
305 }
306}
307
3428fdd5 308my %files;
309
310sub maintainers_files {
311 %files = ();
678b26d7 312 for my $k (keys %Modules) {
313 for my $f (get_module_files($k)) {
314 ++$files{$f};
315 }
316 }
3428fdd5 317}
318
319sub duplicated_maintainers {
320 maintainers_files();
678b26d7 321 for my $f (keys %files) {
322 if ($files{$f} > 1) {
323 warn "File $f appears $files{$f} times in Maintainers.pl\n";
324 }
325 }
326}
327
357244ac 328sub warn_maintainer {
329 my $name = shift;
330 warn "File $name has no maintainer\n" if not $files{$name};
331}
332
3428fdd5 333sub missing_maintainers {
334 my($check, @path) = @_;
335 maintainers_files();
336 my @dir;
357244ac 337 for my $d (@path) {
338 if( -d $d ) { push @dir, $d } else { warn_maintainer($d) }
339 }
bfca551d 340 find sub { warn_maintainer($File::Find::name) if $check->() }, @dir
3428fdd5 341 if @dir;
342}
343
0cf51544 3441;
345