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
CommitLineData
f1c5bace
JH
1#!/usr/bin/perl -w
2
96048c95 3#
42e700c9
MJ
4# cmpVERSION - compare the current Perl source tree and a given tag
5# for modules that have identical version numbers but different contents.
f1c5bace 6#
2fb8ff88 7# with -d option, output the diffs too
844d3843
NC
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)
2547c837 12#
42e700c9 13# Original by slaven@rezic.de, modified by jhi and matt.w.johnson@gmail.com.
76733a60 14# Adaptation to produce TAP by Abigail, folded back into this file by Nicholas
f1c5bace
JH
15
16use strict;
96048c95 17use 5.006;
f1c5bace
JH
18
19use ExtUtils::MakeMaker;
e01fd32a 20use File::Spec::Functions qw(devnull);
00ad0422 21use Getopt::Long;
2547c837 22
76733a60 23my ($diffs, $exclude_upstream, $tag_to_compare, $tap);
00ad0422 24unless (GetOptions('diffs' => \$diffs,
844d3843 25 'exclude|x' => \$exclude_upstream,
e01fd32a 26 'tag=s' => \$tag_to_compare,
76733a60 27 'tap' => \$tap,
e01fd32a 28 ) && @ARGV == 0) {
76733a60 29 die "usage: $0 [ -d -x --tag TAG --tap]";
2547c837 30}
f1c5bace 31
e01fd32a
NC
32die "$0: This does not look like a Perl directory\n"
33 unless -f "perl.h" && -d "Porting";
34die "$0: 'This is a Perl directory but does not look like Git working directory\n"
6b44ec68 35 unless (-d ".git" || (exists $ENV{GIT_DIR} && -d $ENV{GIT_DIR}));
42e700c9 36
2385f340 37my $null = devnull();
68d2af03 38
e01fd32a 39unless (defined $tag_to_compare) {
d7d4eceb
GK
40 my $check = 'HEAD';
41 while(1) {
42 $check = `git describe --abbrev=0 $check 2>$null`;
43 chomp $check;
44 last unless $check =~ /-RC/;
17714d93 45 $check .= '~1';
d7d4eceb
GK
46 }
47 $tag_to_compare = $check;
e01fd32a
NC
48 # Thanks to David Golden for this suggestion.
49
e01fd32a
NC
50}
51
0f85f13d
HS
52unless (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
68d2af03 59my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42e700c9
MJ
60chomp $tag_exists;
61
76733a60
NC
62unless ($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}
f1c5bace 67
844d3843
NC
68my %upstream_files;
69if ($exclude_upstream) {
3a06b4ed
NC
70 unshift @INC, 'Porting';
71 require Maintainers;
72
844d3843
NC
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);
2fb8ff88
DM
77 }
78}
79
88830c88
JH
80# Files to skip from the check for one reason or another,
81# usually because they pull in their version from some other file.
82my %skip;
477acd91 83@skip{
551856fd 84 'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
9de35bb2 85 'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
955b6d35
SH
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
5173674b 88 'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
da020b69
SH
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
ffdf828c
TR
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
d813268c
TR
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
a808dd67
SH
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
d42e1eb7 109 'cpan/podlators/t/lib/Test/Podlators.pm', # just a test module
77218c1e
CB
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
130cad93 112 'cpan/version/t/coretests.pm', # just a test module
955b6d35
SH
113 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
114 'dist/Exporter/lib/Exporter/Heavy.pm',
c6b8d200 115 'dist/Module-CoreList/lib/Module/CoreList.pm',
c6b8d200 116 'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
42e700c9
MJ
117 'lib/Carp/Heavy.pm',
118 'lib/Config.pm', # no version number but contents will vary
42e700c9 119 'win32/FindExt.pm',
477acd91 120} = ();
76733a60
NC
121
122# Files to skip just for particular version(s),
123# usually due to some # mix-up
124
f008f53b 125my %skip_versions = (
76733a60 126 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
76733a60 127 );
76733a60 128
42e700c9
MJ
129my $skip_dirs = qr|^t/lib|;
130
96048c95
NC
131sub pm_file_from_xs {
132 my $xs = shift;
133
7ab1c1a4
NC
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!;
4421e1e9 151 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
7ab1c1a4
NC
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 }
42e700c9 160
96048c95
NC
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
170my %module_diffs;
171
7ac818b2 172foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
96048c95
NC
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{$_};
7ab1c1a4 178 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
96048c95 179 push @{$module_diffs{$_}}, $_;
e54eb2b6 180 } elsif (/\.xs\z/ && !/\bt\b/) {
96048c95
NC
181 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
182 }
183}
42e700c9 184
96048c95 185unless (%module_diffs) {
76733a60
NC
186 print "1..1\nok 1 - No difference found\n" if $tap;
187 exit;
188}
189
96048c95 190printf "1..%d\n" => scalar keys %module_diffs if $tap;
8ecf5aeb 191print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
76733a60
NC
192
193my $count;
194my $diff_cmd = "git --no-pager diff $tag_to_compare ";
7ba8970d 195my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
76733a60 196my (@diff);
42e700c9 197
96048c95
NC
198foreach 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.
42e700c9
MJ
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)};
63ffcb73 204 ++$count;
97a78671
NC
205
206 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
40f7a2b9
FC
207 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
208 if $tap;
97a78671 209 } elsif (!defined $pm_version || $pm_version eq 'undef') {
db8086f1
JH
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";
97a78671 213 } elsif ($pm_version ne $orig_pm_version) { # good
63ffcb73 214 print "ok $count - $pm_file\n" if $tap;
96048c95 215 } else {
76733a60 216 if ($tap) {
8ecf5aeb
DM
217 print "#\n# " . '-' x 75 . "\n"
218 . "# Version number ($pm_version) unchanged since"
219 . " $tag_to_compare, but contents have changed:\n#\n";
96048c95 220 foreach (sort @{$module_diffs{$pm_file}}) {
7ba8970d 221 print "# $_" for `$diff_cmd $q$_$q`;
76733a60 222 }
8ecf5aeb
DM
223 print "# " . '-' x 75 . "\n";
224
f008f53b
NC
225 if (exists $skip_versions{$pm_file}
226 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
63ffcb73 227 print "ok $count - SKIP $pm_file version $pm_version\n";
f008f53b 228 } else {
db8086f1
JH
229 my $nok = "not ok $count - $pm_file version $pm_version\n";
230 print $nok;
231 print STDERR "# $nok";
f008f53b 232 }
76733a60 233 } else {
96048c95 234 push @diff, @{$module_diffs{$pm_file}};
f5872097 235 print "$pm_file version $pm_version\n";
76733a60
NC
236 }
237 }
42e700c9
MJ
238}
239
240sub get_file_from_git {
241 my ($file, $tag) = @_;
96048c95 242 local $/;
f378d7a6
TC
243
244 use open IN => ':raw';
96048c95 245 return scalar `git --no-pager show $tag:$file 2>$null`;
42e700c9
MJ
246}
247
76733a60
NC
248if ($diffs) {
249 for (sort @diff) {
250 print "\n";
7ba8970d 251 system "$diff_cmd $q$_$q";
76733a60 252 }
2547c837 253}