This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump version to 5.33.5
[perl5.git] / Porting / cmpVERSION.pl
1 #!/usr/bin/perl -w
2
3 #
4 # cmpVERSION - compare the current Perl source tree and a given tag
5 # for modules that have identical version numbers but different contents.
6 #
7 # with -d option, output the diffs too
8 # with -x option, exclude files from modules where blead is not upstream
9 #
10 # (after all, there are tools like core-cpan-diff that can already deal with
11 # them)
12 #
13 # Original by slaven@rezic.de, modified by jhi and matt.w.johnson@gmail.com.
14 # Adaptation to produce TAP by Abigail, folded back into this file by Nicholas
15
16 use strict;
17 use 5.006;
18
19 use ExtUtils::MakeMaker;
20 use File::Spec::Functions qw(devnull);
21 use Getopt::Long;
22
23 my ($diffs, $exclude_upstream, $tag_to_compare, $tap);
24 unless (GetOptions('diffs' => \$diffs,
25                    'exclude|x' => \$exclude_upstream,
26                    'tag=s' => \$tag_to_compare,
27                    'tap' => \$tap,
28                    ) && @ARGV == 0) {
29     die "usage: $0 [ -d -x --tag TAG --tap]";
30 }
31
32 die "$0: This does not look like a Perl directory\n"
33     unless -f "perl.h" && -d "Porting";
34 die "$0: 'This is a Perl directory but does not look like Git working directory\n"
35     unless (-d ".git" || (exists $ENV{GIT_DIR} && -d $ENV{GIT_DIR}));
36
37 my $null = devnull();
38
39 unless (defined $tag_to_compare) {
40     my $check = 'HEAD';
41     while(1) {
42         $check = `git describe --abbrev=0 $check 2>$null`;
43         chomp $check;
44         last unless $check =~ /-RC/;
45         $check .= '~1';
46     }
47     $tag_to_compare = $check;
48     # Thanks to David Golden for this suggestion.
49
50 }
51
52 unless (length $tag_to_compare) {
53     die "$0: Git found, but no Git tags found\n"
54         unless $tap;
55     print "1..0 # SKIP: Git found, but no Git tags found\n";
56     exit 0;
57 }
58
59 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
60 chomp $tag_exists;
61
62 unless ($tag_exists eq $tag_to_compare) {
63     die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
64     print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
65     exit 0;
66 }
67
68 my %upstream_files;
69 if ($exclude_upstream) {
70     unshift @INC, 'Porting';
71     require Maintainers;
72
73     for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
74                          or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
75                keys %Maintainers::Modules) {
76         $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
77     }
78 }
79
80 # Files to skip from the check for one reason or another,
81 # usually because they pull in their version from some other file.
82 my %skip;
83 @skip{
84     'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
85     'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
86     'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
87     'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm',  # just a test module
88     'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
89     'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
90     'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm',   # just a test module
91     'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm',     # just a test module
92     'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm',   # just a test module
93     'cpan/Math-BigRat/t/Math/BigRat/Test.pm',       # just a test module
94     'cpan/Module-Load/t/to_load/LoadIt.pm',         # just a test module
95     'cpan/Module-Load/t/to_load/Must/Be/Loaded.pm', # just a test module
96     'cpan/Module-Load-Conditional/t/test_lib/a/X.pm',          # just a test module
97     'cpan/Module-Load-Conditional/t/test_lib/b/X.pm',          # just a test module
98     'cpan/Module-Load-Conditional/t/to_load/Commented.pm',     # just a test module
99     'cpan/Module-Load-Conditional/t/to_load/HereDoc.pm',       # just a test module
100     'cpan/Module-Load-Conditional/t/to_load/InPod.pm',         # just a test module
101     'cpan/Module-Load-Conditional/t/to_load/LoadIt.pm',        # just a test module
102     'cpan/Module-Load-Conditional/t/to_load/MustBe/Loaded.pm', # just a test module
103     'cpan/Module-Load-Conditional/t/to_load/NotMain.pm',       # just a test module
104     'cpan/Module-Load-Conditional/t/to_load/NotX.pm',          # just a test module
105     'cpan/Pod-Usage/t/inc/Pod/InputObjects.pm',     # just a test module
106     'cpan/Pod-Usage/t/inc/Pod/Parser.pm',           # just a test module
107     'cpan/Pod-Usage/t/inc/Pod/PlainText.pm',        # just a test module
108     'cpan/Pod-Usage/t/inc/Pod/Select.pm',           # just a test module
109     'cpan/podlators/t/lib/Test/Podlators.pm',       # just a test module
110     'cpan/podlators/t/lib/Test/RRA.pm',             # just a test module
111     'cpan/podlators/t/lib/Test/RRA/Config.pm',      # just a test module
112     'cpan/version/t/coretests.pm', # just a test module
113     'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
114     'dist/Exporter/lib/Exporter/Heavy.pm',
115     'dist/Module-CoreList/lib/Module/CoreList.pm',
116     'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
117     'lib/Carp/Heavy.pm',
118     'lib/Config.pm',            # no version number but contents will vary
119     'win32/FindExt.pm',
120 } = ();
121
122 # Files to skip just for particular version(s),
123 # usually due to some # mix-up
124
125 my %skip_versions = (
126            # 'some/sample/file.pm' => [ '1.23', '1.24' ],
127           );
128
129 my $skip_dirs = qr|^t/lib|;
130
131 sub pm_file_from_xs {
132     my $xs = shift;
133
134     foreach my $try (sub {
135                          # First try a .pm at the same level as the .xs file
136                          # with the same basename
137                          return shift =~ s/\.xs\z//r;
138                      },
139                      sub {
140                          # Try for a (different) .pm at the same level, based
141                          # on the directory name:
142                          my ($path) = shift =~ m!^(.*)/!;
143                          my ($last) = $path =~ m!([^-/]+)\z!;
144                          return "$path/$last";
145                      },
146                      sub {
147                          # Try to work out the extension's full package, and
148                          # look for a .pm in lib/ based on that:
149                          my ($path) = shift =~ m!^(.*)/!;
150                          my ($last) = $path =~ m!([^/]+)\z!;
151                          $last = 'List-Util' if $last eq 'Scalar-List-Utils';
152                          $last =~ tr !-!/!;
153                          return "$path/lib/$last";
154                      }) {
155         # For all cases, first look to see if the .pm file is generated.
156         my $base = $try->($xs);
157         return "${base}_pm.PL" if -f "${base}_pm.PL";
158         return "${base}.pm" if -f "${base}.pm";
159     }
160
161     die "No idea which .pm file corresponds to '$xs', so aborting";
162 }
163
164 # Key is the .pm file from which we check the version.
165 # Value is a reference to an array of files to check for differences
166 # The trivial case is a pure perl module, where the array holds one element,
167 # the perl module's file. The "fun" comes with XS modules, and the real fun
168 # with XS modules with more than one XS file, and "interesting" layouts.
169
170 my %module_diffs;
171
172 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
173     chomp;
174     next unless m/^(.*)\//;
175     my $this_dir = $1;
176     next if $this_dir =~ $skip_dirs || exists $skip{$_};
177     next if exists $upstream_files{$_};
178     if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
179         push @{$module_diffs{$_}}, $_;
180     } elsif (/\.xs\z/ && !/\bt\b/) {
181         push @{$module_diffs{pm_file_from_xs($_)}}, $_;
182     }
183 }
184
185 unless (%module_diffs) {
186     print "1..1\nok 1 - No difference found\n" if $tap;
187     exit;
188 }
189
190 printf "1..%d\n" => scalar keys %module_diffs if $tap;
191 print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
192
193 my $count;
194 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
195 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
196 my (@diff);
197
198 foreach my $pm_file (sort keys %module_diffs) {
199     # git has already told us that the files differ, so no need to grab each as
200     # a blob from git, and do the comparison ourselves.
201     my $pm_version = eval {MM->parse_version($pm_file)};
202     my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
203     my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
204     ++$count;
205
206     if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
207         print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
208           if $tap;
209     } elsif (!defined $pm_version || $pm_version eq 'undef') {
210         my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
211         print $nok if $tap;
212         print STDERR "# $nok\n";
213     } elsif ($pm_version ne $orig_pm_version) { # good
214         print "ok $count - $pm_file\n" if $tap;
215     } else {
216         if ($tap) {
217             print "#\n# " . '-' x 75 . "\n"
218             . "# Version number ($pm_version) unchanged since"
219             . " $tag_to_compare, but contents have changed:\n#\n";
220             foreach (sort @{$module_diffs{$pm_file}}) {
221                 print "# $_" for `$diff_cmd $q$_$q`;
222             }
223             print "# " . '-' x 75 . "\n";
224
225             if (exists $skip_versions{$pm_file}
226                 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
227                 print "ok $count - SKIP $pm_file version $pm_version\n";
228             } else {
229                 my $nok = "not ok $count - $pm_file version $pm_version\n";
230                 print $nok;
231                 print STDERR "# $nok";
232             }
233         } else {
234             push @diff, @{$module_diffs{$pm_file}};
235             print "$pm_file version $pm_version\n";
236         }
237     }
238 }
239
240 sub get_file_from_git {
241     my ($file, $tag) = @_;
242     local $/;
243
244     use open IN => ':raw';
245     return scalar `git --no-pager show $tag:$file 2>$null`;
246 }
247
248 if ($diffs) {
249     for (sort @diff) {
250         print "\n";
251         system "$diff_cmd $q$_$q";
252     }
253 }