This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Restore cmpVERSION.pl's ability to spot differences in XS files.
[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"
35 unless -d ".git";
42e700c9 36
2385f340 37my $null = devnull();
68d2af03 38
e01fd32a
NC
39unless (defined $tag_to_compare) {
40 # Thanks to David Golden for this suggestion.
41
42 $tag_to_compare = `git describe --abbrev=0`;
43 chomp $tag_to_compare;
44}
45
68d2af03 46my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42e700c9
MJ
47chomp $tag_exists;
48
76733a60
NC
49unless ($tag_exists eq $tag_to_compare) {
50 die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
51 print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
52 exit 0;
53}
f1c5bace 54
844d3843
NC
55my %upstream_files;
56if ($exclude_upstream) {
3a06b4ed
NC
57 unshift @INC, 'Porting';
58 require Maintainers;
59
844d3843
NC
60 for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
61 or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
62 keys %Maintainers::Modules) {
63 $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
2fb8ff88
DM
64 }
65}
66
88830c88
JH
67# Files to skip from the check for one reason or another,
68# usually because they pull in their version from some other file.
69my %skip;
477acd91 70@skip{
42e700c9
MJ
71 'lib/Carp/Heavy.pm',
72 'lib/Config.pm', # no version number but contents will vary
73 'lib/Exporter/Heavy.pm',
74 'win32/FindExt.pm',
477acd91 75} = ();
76733a60
NC
76
77# Files to skip just for particular version(s),
78# usually due to some # mix-up
79
80my %skip_versions;
81if ($tap) {
82 %skip_versions
83 = (
84 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
85 'dist/threads/lib/threads.pm' => [ '1.83' ],
86 );
87}
88
42e700c9
MJ
89my $skip_dirs = qr|^t/lib|;
90
96048c95
NC
91sub pm_file_from_xs {
92 my $xs = shift;
93
94 # First try a .pm at the same level as the .xs file, with the same basename
95 my $pm = $xs;
96 $pm =~ s/xs\z/pm/;
97 return $pm if -f $pm;
98
99 # Try for a (different) .pm at the same level, based on the directory name:
100 my ($path) = $xs =~ m!^(.*)/!;
101 my ($last) = $path =~ m!([^-/]+)\z!;
102 $pm = "$path/$last.pm";
103 return $pm if -f $pm;
104
105 # Try to work out the extension's full package, and look for a .pm in lib/
106 # based on that:
107 ($last) = $path =~ m!([^/]+)\z!;
108 $last =~ tr !-!/!;
109 $pm = "$path/lib/$last.pm";
110 return $pm if -f $pm;
42e700c9 111
96048c95
NC
112 die "No idea which .pm file corresponds to '$xs', so aborting";
113}
114
115# Key is the .pm file from which we check the version.
116# Value is a reference to an array of files to check for differences
117# The trivial case is a pure perl module, where the array holds one element,
118# the perl module's file. The "fun" comes with XS modules, and the real fun
119# with XS modules with more than one XS file, and "interesting" layouts.
120
121my %module_diffs;
122
123foreach (`git --no-pager diff --name-only $tag_to_compare`) {
124 chomp;
125 next unless m/^(.*)\//;
126 my $this_dir = $1;
127 next if $this_dir =~ $skip_dirs || exists $skip{$_};
128 next if exists $upstream_files{$_};
129 if (/\.pm\z/) {
130 push @{$module_diffs{$_}}, $_;
131 } elsif (/\.xs\z/) {
132 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
133 }
134}
42e700c9 135
96048c95 136unless (%module_diffs) {
76733a60
NC
137 print "1..1\nok 1 - No difference found\n" if $tap;
138 exit;
139}
140
96048c95 141printf "1..%d\n" => scalar keys %module_diffs if $tap;
76733a60
NC
142
143my $count;
144my $diff_cmd = "git --no-pager diff $tag_to_compare ";
145my (@diff);
42e700c9 146
96048c95
NC
147foreach my $pm_file (sort keys %module_diffs) {
148 # git has already told us that the files differ, so no need to grab each as
149 # a blob from git, and do the comparison ourselves.
42e700c9
MJ
150 my $pm_version = eval {MM->parse_version($pm_file)};
151 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
152 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
96048c95
NC
153
154 if ((!defined $pm_version || !defined $orig_pm_version)
155 || ($pm_version eq 'undef' || $orig_pm_version eq 'undef') # sigh
156 || ($pm_version ne $orig_pm_version) # good
157 || (exists $skip_versions{$pm_file}
158 and grep $pm_version eq $_, @{$skip_versions{$pm_file}})
159 ) {
160 printf "ok %d - %s\n", ++$count, $pm_file if $tap;
161 } else {
76733a60 162 if ($tap) {
96048c95 163 foreach (sort @{$module_diffs{$pm_file}}) {
76733a60
NC
164 print "# $_" for `$diff_cmd '$_'`;
165 }
166 printf "not ok %d - %s\n", ++$count, $pm_file;
167 } else {
96048c95 168 push @diff, @{$module_diffs{$pm_file}};
76733a60
NC
169 print "$pm_file\n";
170 }
171 }
42e700c9
MJ
172}
173
174sub get_file_from_git {
175 my ($file, $tag) = @_;
96048c95
NC
176 local $/;
177 return scalar `git --no-pager show $tag:$file 2>$null`;
42e700c9
MJ
178}
179
76733a60
NC
180if ($diffs) {
181 for (sort @diff) {
182 print "\n";
183 system "$diff_cmd '$_'";
184 }
2547c837 185}