4 # cmpVERSION - compare the current Perl source tree and a given tag
5 # for modules that have identical version numbers but different contents.
7 # with -d option, output the diffs too
8 # with -x option, exclude files from modules where blead is not upstream
10 # (after all, there are tools like core-cpan-diff that can already deal with
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
19 use ExtUtils::MakeMaker;
20 use File::Spec::Functions qw(devnull);
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,
29 die "usage: $0 [ -d -x --tag TAG --tap]";
32 die "$0: This does not look like a Perl directory\n"
33 unless -f "perl.h" && -d "Porting";
35 if (-d ".git" || (exists $ENV{GIT_DIR} && -d $ENV{GIT_DIR})) {
38 # Also handle linked worktrees created by git-worktree:
41 my $commit = '8d063cd8450e59ea1c611a2f4f5a21059a2804f1';
42 my $out = `git rev-parse --verify --quiet '$commit^{commit}'`;
49 die "$0: This is a Perl directory but does not look like Git working directory\n"
55 unless (defined $tag_to_compare) {
58 $check = `git describe --abbrev=0 $check 2>$null`;
60 last unless $check =~ /-RC/;
63 $tag_to_compare = $check;
64 # Thanks to David Golden for this suggestion.
68 unless (length $tag_to_compare) {
69 die "$0: Git found, but no Git tags found\n"
71 print "1..0 # SKIP: Git found, but no Git tags found\n";
75 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
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";
85 if ($exclude_upstream) {
86 unshift @INC, 'Porting';
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);
96 # Files to skip from the check for one reason or another,
97 # usually because they pull in their version from some other file.
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',
134 'lib/Config.pm', # no version number but contents will vary
138 # Files to skip just for particular version(s),
139 # usually due to some # mix-up
141 my %skip_versions = (
142 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
145 my $skip_dirs = qr|^t/lib|;
147 sub pm_file_from_xs {
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;
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";
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';
169 return "$path/lib/$last";
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";
177 die "No idea which .pm file corresponds to '$xs', so aborting";
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.
188 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
190 next unless m/^(.*)\//;
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($_)}}, $_;
201 unless (%module_diffs) {
202 print "1..1\nok 1 - No difference found\n" if $tap;
206 printf "1..%d\n" => scalar keys %module_diffs if $tap;
207 print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
210 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
211 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
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)};
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"
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";
228 print STDERR "# $nok\n";
229 } elsif ($pm_version ne $orig_pm_version) { # good
230 print "ok $count - $pm_file\n" 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`;
239 print "# " . '-' x 75 . "\n";
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";
245 my $nok = "not ok $count - $pm_file version $pm_version\n";
247 print STDERR "# $nok";
250 push @diff, @{$module_diffs{$pm_file}};
251 print "$pm_file version $pm_version\n";
256 sub get_file_from_git {
257 my ($file, $tag) = @_;
260 use open IN => ':raw';
261 return scalar `git --no-pager show $tag:$file 2>$null`;
267 system "$diff_cmd $q$_$q";