xref: /openbsd/gnu/usr.bin/perl/Porting/cmpVERSION.pl (revision 3cab2bb3)
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";
34die "$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
37my $null = devnull();
38
39unless (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
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
59my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
60chomp $tag_exists;
61
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}
67
68my %upstream_files;
69if ($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.
82my %skip;
83@skip{
84    'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
85    'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
86    'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm',  # just a test module
87    'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
88    'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
89    'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm',   # just a test module
90    'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm',     # just a test module
91    'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm',   # just a test module
92    'cpan/Math-BigRat/t/Math/BigRat/Test.pm',       # just a test module
93    'cpan/podlators/t/lib/Test/Podlators.pm',       # just a test module
94    'cpan/podlators/t/lib/Test/RRA.pm',             # just a test module
95    'cpan/podlators/t/lib/Test/RRA/Config.pm',      # just a test module
96    'cpan/version/t/coretests.pm', # just a test module
97    'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
98    'dist/Exporter/lib/Exporter/Heavy.pm',
99    'dist/Module-CoreList/lib/Module/CoreList.pm',
100    'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
101    'lib/Carp/Heavy.pm',
102    'lib/Config.pm',		# no version number but contents will vary
103    'win32/FindExt.pm',
104} = ();
105
106# Files to skip just for particular version(s),
107# usually due to some # mix-up
108
109my %skip_versions = (
110	   # 'some/sample/file.pm' => [ '1.23', '1.24' ],
111	  );
112
113my $skip_dirs = qr|^t/lib|;
114
115sub pm_file_from_xs {
116    my $xs = shift;
117
118    foreach my $try (sub {
119			 # First try a .pm at the same level as the .xs file
120			 # with the same basename
121			 return shift =~ s/\.xs\z//r;
122		     },
123		     sub {
124			 # Try for a (different) .pm at the same level, based
125			 # on the directory name:
126			 my ($path) = shift =~ m!^(.*)/!;
127			 my ($last) = $path =~ m!([^-/]+)\z!;
128			 return "$path/$last";
129		     },
130		     sub {
131			 # Try to work out the extension's full package, and
132			 # look for a .pm in lib/ based on that:
133			 my ($path) = shift =~ m!^(.*)/!;
134			 my ($last) = $path =~ m!([^/]+)\z!;
135			 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
136			 $last =~ tr !-!/!;
137			 return "$path/lib/$last";
138		     }) {
139	# For all cases, first look to see if the .pm file is generated.
140	my $base = $try->($xs);
141	return "${base}_pm.PL" if -f "${base}_pm.PL";
142	return "${base}.pm" if -f "${base}.pm";
143    }
144
145    die "No idea which .pm file corresponds to '$xs', so aborting";
146}
147
148# Key is the .pm file from which we check the version.
149# Value is a reference to an array of files to check for differences
150# The trivial case is a pure perl module, where the array holds one element,
151# the perl module's file. The "fun" comes with XS modules, and the real fun
152# with XS modules with more than one XS file, and "interesting" layouts.
153
154my %module_diffs;
155
156foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
157    chomp;
158    next unless m/^(.*)\//;
159    my $this_dir = $1;
160    next if $this_dir =~ $skip_dirs || exists $skip{$_};
161    next if exists $upstream_files{$_};
162    if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
163	push @{$module_diffs{$_}}, $_;
164    } elsif (/\.xs\z/ && !/\bt\b/) {
165	push @{$module_diffs{pm_file_from_xs($_)}}, $_;
166    }
167}
168
169unless (%module_diffs) {
170    print "1..1\nok 1 - No difference found\n" if $tap;
171    exit;
172}
173
174printf "1..%d\n" => scalar keys %module_diffs if $tap;
175print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
176
177my $count;
178my $diff_cmd = "git --no-pager diff $tag_to_compare ";
179my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
180my (@diff);
181
182foreach my $pm_file (sort keys %module_diffs) {
183    # git has already told us that the files differ, so no need to grab each as
184    # a blob from git, and do the comparison ourselves.
185    my $pm_version = eval {MM->parse_version($pm_file)};
186    my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
187    my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
188    ++$count;
189
190    if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
191        print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
192          if $tap;
193    } elsif (!defined $pm_version || $pm_version eq 'undef') {
194        my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
195        print $nok if $tap;
196        print STDERR "# $nok\n";
197    } elsif ($pm_version ne $orig_pm_version) { # good
198        print "ok $count - $pm_file\n" if $tap;
199    } else {
200	if ($tap) {
201            print "#\n# " . '-' x 75 . "\n"
202            . "# Version number ($pm_version) unchanged since"
203            . " $tag_to_compare, but contents have changed:\n#\n";
204	    foreach (sort @{$module_diffs{$pm_file}}) {
205		print "# $_" for `$diff_cmd $q$_$q`;
206	    }
207            print "# " . '-' x 75 . "\n";
208
209	    if (exists $skip_versions{$pm_file}
210		and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
211		print "ok $count - SKIP $pm_file version $pm_version\n";
212	    } else {
213		my $nok = "not ok $count - $pm_file version $pm_version\n";
214		print $nok;
215		print STDERR "# $nok";
216	    }
217	} else {
218	    push @diff, @{$module_diffs{$pm_file}};
219	    print "$pm_file version $pm_version\n";
220	}
221    }
222}
223
224sub get_file_from_git {
225    my ($file, $tag) = @_;
226    local $/;
227
228    use open IN => ':raw';
229    return scalar `git --no-pager show $tag:$file 2>$null`;
230}
231
232if ($diffs) {
233    for (sort @diff) {
234	print "\n";
235	system "$diff_cmd $q$_$q";
236    }
237}
238