This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
makerel: use Digest::SHA to print sha256sum
[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";
111f3874
NC
34
35if (-d ".git" || (exists $ENV{GIT_DIR} && -d $ENV{GIT_DIR})) {
36 # Looks good
37} else {
38 # Also handle linked worktrees created by git-worktree:
39 my $found;
40 if (-f '.git') {
4b21956e 41 # the hash of the initial commit in perl.git (perl-1.0)
111f3874
NC
42 my $commit = '8d063cd8450e59ea1c611a2f4f5a21059a2804f1';
43 my $out = `git rev-parse --verify --quiet '$commit^{commit}'`;
44 chomp $out;
45 if($out eq $commit) {
46 ++$found;
47 }
48 }
49
50 die "$0: This is a Perl directory but does not look like Git working directory\n"
51 unless $found;
52}
42e700c9 53
2385f340 54my $null = devnull();
68d2af03 55
e01fd32a 56unless (defined $tag_to_compare) {
d7d4eceb
GK
57 my $check = 'HEAD';
58 while(1) {
59 $check = `git describe --abbrev=0 $check 2>$null`;
60 chomp $check;
61 last unless $check =~ /-RC/;
17714d93 62 $check .= '~1';
d7d4eceb
GK
63 }
64 $tag_to_compare = $check;
e01fd32a
NC
65 # Thanks to David Golden for this suggestion.
66
e01fd32a
NC
67}
68
0f85f13d
HS
69unless (length $tag_to_compare) {
70 die "$0: Git found, but no Git tags found\n"
71 unless $tap;
72 print "1..0 # SKIP: Git found, but no Git tags found\n";
73 exit 0;
74}
75
68d2af03 76my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42e700c9
MJ
77chomp $tag_exists;
78
76733a60
NC
79unless ($tag_exists eq $tag_to_compare) {
80 die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
81 print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
82 exit 0;
83}
f1c5bace 84
844d3843
NC
85my %upstream_files;
86if ($exclude_upstream) {
3a06b4ed
NC
87 unshift @INC, 'Porting';
88 require Maintainers;
89
844d3843
NC
90 for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
91 or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
92 keys %Maintainers::Modules) {
93 $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
2fb8ff88
DM
94 }
95}
96
88830c88
JH
97# Files to skip from the check for one reason or another,
98# usually because they pull in their version from some other file.
99my %skip;
477acd91 100@skip{
551856fd 101 'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
9de35bb2 102 'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
955b6d35
SH
103 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
104 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm', # just a test module
5173674b 105 'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
da020b69
SH
106 'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
107 'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm', # just a test module
108 'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm', # just a test module
109 'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm', # just a test module
110 'cpan/Math-BigRat/t/Math/BigRat/Test.pm', # just a test module
ffdf828c
TR
111 'cpan/Module-Load/t/to_load/LoadIt.pm', # just a test module
112 'cpan/Module-Load/t/to_load/Must/Be/Loaded.pm', # just a test module
d813268c
TR
113 'cpan/Module-Load-Conditional/t/test_lib/a/X.pm', # just a test module
114 'cpan/Module-Load-Conditional/t/test_lib/b/X.pm', # just a test module
115 'cpan/Module-Load-Conditional/t/to_load/Commented.pm', # just a test module
116 'cpan/Module-Load-Conditional/t/to_load/HereDoc.pm', # just a test module
117 'cpan/Module-Load-Conditional/t/to_load/InPod.pm', # just a test module
118 'cpan/Module-Load-Conditional/t/to_load/LoadIt.pm', # just a test module
119 'cpan/Module-Load-Conditional/t/to_load/MustBe/Loaded.pm', # just a test module
120 'cpan/Module-Load-Conditional/t/to_load/NotMain.pm', # just a test module
121 'cpan/Module-Load-Conditional/t/to_load/NotX.pm', # just a test module
a808dd67
SH
122 'cpan/Pod-Usage/t/inc/Pod/InputObjects.pm', # just a test module
123 'cpan/Pod-Usage/t/inc/Pod/Parser.pm', # just a test module
124 'cpan/Pod-Usage/t/inc/Pod/PlainText.pm', # just a test module
125 'cpan/Pod-Usage/t/inc/Pod/Select.pm', # just a test module
d42e1eb7 126 'cpan/podlators/t/lib/Test/Podlators.pm', # just a test module
77218c1e
CB
127 'cpan/podlators/t/lib/Test/RRA.pm', # just a test module
128 'cpan/podlators/t/lib/Test/RRA/Config.pm', # just a test module
130cad93 129 'cpan/version/t/coretests.pm', # just a test module
955b6d35
SH
130 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
131 'dist/Exporter/lib/Exporter/Heavy.pm',
c6b8d200 132 'dist/Module-CoreList/lib/Module/CoreList.pm',
c6b8d200 133 'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
42e700c9
MJ
134 'lib/Carp/Heavy.pm',
135 'lib/Config.pm', # no version number but contents will vary
42e700c9 136 'win32/FindExt.pm',
477acd91 137} = ();
76733a60
NC
138
139# Files to skip just for particular version(s),
140# usually due to some # mix-up
141
f008f53b 142my %skip_versions = (
76733a60 143 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
76733a60 144 );
76733a60 145
42e700c9
MJ
146my $skip_dirs = qr|^t/lib|;
147
96048c95
NC
148sub pm_file_from_xs {
149 my $xs = shift;
150
7ab1c1a4
NC
151 foreach my $try (sub {
152 # First try a .pm at the same level as the .xs file
153 # with the same basename
154 return shift =~ s/\.xs\z//r;
155 },
156 sub {
157 # Try for a (different) .pm at the same level, based
158 # on the directory name:
159 my ($path) = shift =~ m!^(.*)/!;
160 my ($last) = $path =~ m!([^-/]+)\z!;
161 return "$path/$last";
162 },
163 sub {
164 # Try to work out the extension's full package, and
165 # look for a .pm in lib/ based on that:
166 my ($path) = shift =~ m!^(.*)/!;
167 my ($last) = $path =~ m!([^/]+)\z!;
4421e1e9 168 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
7ab1c1a4
NC
169 $last =~ tr !-!/!;
170 return "$path/lib/$last";
171 }) {
172 # For all cases, first look to see if the .pm file is generated.
173 my $base = $try->($xs);
174 return "${base}_pm.PL" if -f "${base}_pm.PL";
175 return "${base}.pm" if -f "${base}.pm";
176 }
42e700c9 177
96048c95
NC
178 die "No idea which .pm file corresponds to '$xs', so aborting";
179}
180
181# Key is the .pm file from which we check the version.
182# Value is a reference to an array of files to check for differences
183# The trivial case is a pure perl module, where the array holds one element,
184# the perl module's file. The "fun" comes with XS modules, and the real fun
185# with XS modules with more than one XS file, and "interesting" layouts.
186
187my %module_diffs;
188
7ac818b2 189foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
96048c95
NC
190 chomp;
191 next unless m/^(.*)\//;
192 my $this_dir = $1;
193 next if $this_dir =~ $skip_dirs || exists $skip{$_};
194 next if exists $upstream_files{$_};
7ab1c1a4 195 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
96048c95 196 push @{$module_diffs{$_}}, $_;
e54eb2b6 197 } elsif (/\.xs\z/ && !/\bt\b/) {
96048c95
NC
198 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
199 }
200}
42e700c9 201
96048c95 202unless (%module_diffs) {
76733a60
NC
203 print "1..1\nok 1 - No difference found\n" if $tap;
204 exit;
205}
206
96048c95 207printf "1..%d\n" => scalar keys %module_diffs if $tap;
8ecf5aeb 208print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
76733a60
NC
209
210my $count;
211my $diff_cmd = "git --no-pager diff $tag_to_compare ";
2eb109a4 212my $q = ($^O eq 'MSWin32' || $^O eq 'VMS') ? '"' : "'";
76733a60 213my (@diff);
42e700c9 214
96048c95
NC
215foreach my $pm_file (sort keys %module_diffs) {
216 # git has already told us that the files differ, so no need to grab each as
217 # a blob from git, and do the comparison ourselves.
42e700c9
MJ
218 my $pm_version = eval {MM->parse_version($pm_file)};
219 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
220 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
63ffcb73 221 ++$count;
97a78671
NC
222
223 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
40f7a2b9
FC
224 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
225 if $tap;
97a78671 226 } elsif (!defined $pm_version || $pm_version eq 'undef') {
db8086f1
JH
227 my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
228 print $nok if $tap;
229 print STDERR "# $nok\n";
97a78671 230 } elsif ($pm_version ne $orig_pm_version) { # good
63ffcb73 231 print "ok $count - $pm_file\n" if $tap;
96048c95 232 } else {
76733a60 233 if ($tap) {
8ecf5aeb
DM
234 print "#\n# " . '-' x 75 . "\n"
235 . "# Version number ($pm_version) unchanged since"
236 . " $tag_to_compare, but contents have changed:\n#\n";
96048c95 237 foreach (sort @{$module_diffs{$pm_file}}) {
7ba8970d 238 print "# $_" for `$diff_cmd $q$_$q`;
76733a60 239 }
8ecf5aeb
DM
240 print "# " . '-' x 75 . "\n";
241
f008f53b
NC
242 if (exists $skip_versions{$pm_file}
243 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
63ffcb73 244 print "ok $count - SKIP $pm_file version $pm_version\n";
f008f53b 245 } else {
db8086f1
JH
246 my $nok = "not ok $count - $pm_file version $pm_version\n";
247 print $nok;
248 print STDERR "# $nok";
f008f53b 249 }
76733a60 250 } else {
96048c95 251 push @diff, @{$module_diffs{$pm_file}};
f5872097 252 print "$pm_file version $pm_version\n";
76733a60
NC
253 }
254 }
42e700c9
MJ
255}
256
257sub get_file_from_git {
258 my ($file, $tag) = @_;
96048c95 259 local $/;
f378d7a6
TC
260
261 use open IN => ':raw';
96048c95 262 return scalar `git --no-pager show $tag:$file 2>$null`;
42e700c9
MJ
263}
264
76733a60
NC
265if ($diffs) {
266 for (sort @diff) {
267 print "\n";
7ba8970d 268 system "$diff_cmd $q$_$q";
76733a60 269 }
2547c837 270}