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