xref: /openbsd/gnu/usr.bin/perl/Porting/cmpVERSION.pl (revision 4bdff4be)
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
16use strict;
17use 5.006;
18
19use ExtUtils::MakeMaker;
20use File::Spec::Functions qw(devnull);
21use Getopt::Long;
22
23my ($diffs, $exclude_upstream, $tag_to_compare, $tap);
24unless (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
32die "$0: This does not look like a Perl directory\n"
33    unless -f "perl.h" && -d "Porting";
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') {
41        # the hash of the initial commit in perl.git (perl-1.0)
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}
53
54my $null = devnull();
55
56unless (defined $tag_to_compare) {
57    my $check = 'HEAD';
58    while(1) {
59        $check = `git describe --abbrev=0 $check 2>$null`;
60        chomp $check;
61        last unless $check =~ /-RC/;
62        $check .= '~1';
63    }
64    $tag_to_compare = $check;
65    # Thanks to David Golden for this suggestion.
66
67}
68
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
76my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
77chomp $tag_exists;
78
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}
84
85my %upstream_files;
86if ($exclude_upstream) {
87    unshift @INC, 'Porting';
88    require Maintainers;
89
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);
94    }
95}
96
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;
100@skip{
101    'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
102    'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
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
105    'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
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
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
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
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
126    'cpan/podlators/t/lib/Test/Podlators.pm',       # just a test module
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
129    'cpan/version/t/coretests.pm', # just a test module
130    'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
131    'dist/Exporter/lib/Exporter/Heavy.pm',
132    'dist/Module-CoreList/lib/Module/CoreList.pm',
133    'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
134    'lib/Carp/Heavy.pm',
135    'lib/Config.pm',		# no version number but contents will vary
136    'win32/FindExt.pm',
137} = ();
138
139# Files to skip just for particular version(s),
140# usually due to some # mix-up
141
142my %skip_versions = (
143	   # 'some/sample/file.pm' => [ '1.23', '1.24' ],
144	  );
145
146my $skip_dirs = qr|^t/lib|;
147
148sub pm_file_from_xs {
149    my $xs = shift;
150
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!;
168			 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
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    }
177
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
189foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
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{$_};
195    if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
196	push @{$module_diffs{$_}}, $_;
197    } elsif (/\.xs\z/ && !/\bt\b/) {
198	push @{$module_diffs{pm_file_from_xs($_)}}, $_;
199    }
200}
201
202unless (%module_diffs) {
203    print "1..1\nok 1 - No difference found\n" if $tap;
204    exit;
205}
206
207printf "1..%d\n" => scalar keys %module_diffs if $tap;
208print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
209
210my $count;
211my $diff_cmd = "git --no-pager diff $tag_to_compare ";
212my $q = ($^O eq 'MSWin32' || $^O eq 'VMS') ? '"' : "'";
213my (@diff);
214
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.
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)};
221    ++$count;
222
223    if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
224        print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
225          if $tap;
226    } elsif (!defined $pm_version || $pm_version eq 'undef') {
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";
230    } elsif ($pm_version ne $orig_pm_version) { # good
231        print "ok $count - $pm_file\n" if $tap;
232    } else {
233	if ($tap) {
234            print "#\n# " . '-' x 75 . "\n"
235            . "# Version number ($pm_version) unchanged since"
236            . " $tag_to_compare, but contents have changed:\n#\n";
237	    foreach (sort @{$module_diffs{$pm_file}}) {
238		print "# $_" for `$diff_cmd $q$_$q`;
239	    }
240            print "# " . '-' x 75 . "\n";
241
242	    if (exists $skip_versions{$pm_file}
243		and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
244		print "ok $count - SKIP $pm_file version $pm_version\n";
245	    } else {
246		my $nok = "not ok $count - $pm_file version $pm_version\n";
247		print $nok;
248		print STDERR "# $nok";
249	    }
250	} else {
251	    push @diff, @{$module_diffs{$pm_file}};
252	    print "$pm_file version $pm_version\n";
253	}
254    }
255}
256
257sub get_file_from_git {
258    my ($file, $tag) = @_;
259    local $/;
260
261    use open IN => ':raw';
262    return scalar `git --no-pager show $tag:$file 2>$null`;
263}
264
265if ($diffs) {
266    for (sort @diff) {
267	print "\n";
268	system "$diff_cmd $q$_$q";
269    }
270}
271