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";
34 die "$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}));
39 unless (defined $tag_to_compare) {
42 $check = `git describe --abbrev=0 $check 2>$null`;
44 last unless $check =~ /-RC/;
47 $tag_to_compare = $check;
48 # Thanks to David Golden for this suggestion.
52 unless (length $tag_to_compare) {
53 die "$0: Git found, but no Git tags found\n"
55 print "1..0 # SKIP: Git found, but no Git tags found\n";
59 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
62 unless ($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";
69 if ($exclude_upstream) {
70 unshift @INC, 'Porting';
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);
80 # Files to skip from the check for one reason or another,
81 # usually because they pull in their version from some other file.
84 'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
85 'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
86 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
87 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm', # just a test module
88 'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
89 'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
90 'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm', # just a test module
91 'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm', # just a test module
92 'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm', # just a test module
93 'cpan/Math-BigRat/t/Math/BigRat/Test.pm', # just a test module
94 'cpan/Module-Load/t/to_load/LoadIt.pm', # just a test module
95 'cpan/Module-Load/t/to_load/Must/Be/Loaded.pm', # just a test module
96 'cpan/Module-Load-Conditional/t/test_lib/a/X.pm', # just a test module
97 'cpan/Module-Load-Conditional/t/test_lib/b/X.pm', # just a test module
98 'cpan/Module-Load-Conditional/t/to_load/Commented.pm', # just a test module
99 'cpan/Module-Load-Conditional/t/to_load/HereDoc.pm', # just a test module
100 'cpan/Module-Load-Conditional/t/to_load/InPod.pm', # just a test module
101 'cpan/Module-Load-Conditional/t/to_load/LoadIt.pm', # just a test module
102 'cpan/Module-Load-Conditional/t/to_load/MustBe/Loaded.pm', # just a test module
103 'cpan/Module-Load-Conditional/t/to_load/NotMain.pm', # just a test module
104 'cpan/Module-Load-Conditional/t/to_load/NotX.pm', # just a test module
105 'cpan/Pod-Usage/t/inc/Pod/InputObjects.pm', # just a test module
106 'cpan/Pod-Usage/t/inc/Pod/Parser.pm', # just a test module
107 'cpan/Pod-Usage/t/inc/Pod/PlainText.pm', # just a test module
108 'cpan/Pod-Usage/t/inc/Pod/Select.pm', # just a test module
109 'cpan/podlators/t/lib/Test/Podlators.pm', # just a test module
110 'cpan/podlators/t/lib/Test/RRA.pm', # just a test module
111 'cpan/podlators/t/lib/Test/RRA/Config.pm', # just a test module
112 'cpan/version/t/coretests.pm', # just a test module
113 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
114 'dist/Exporter/lib/Exporter/Heavy.pm',
115 'dist/Module-CoreList/lib/Module/CoreList.pm',
116 'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
118 'lib/Config.pm', # no version number but contents will vary
122 # Files to skip just for particular version(s),
123 # usually due to some # mix-up
125 my %skip_versions = (
126 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
129 my $skip_dirs = qr|^t/lib|;
131 sub pm_file_from_xs {
134 foreach my $try (sub {
135 # First try a .pm at the same level as the .xs file
136 # with the same basename
137 return shift =~ s/\.xs\z//r;
140 # Try for a (different) .pm at the same level, based
141 # on the directory name:
142 my ($path) = shift =~ m!^(.*)/!;
143 my ($last) = $path =~ m!([^-/]+)\z!;
144 return "$path/$last";
147 # Try to work out the extension's full package, and
148 # look for a .pm in lib/ based on that:
149 my ($path) = shift =~ m!^(.*)/!;
150 my ($last) = $path =~ m!([^/]+)\z!;
151 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
153 return "$path/lib/$last";
155 # For all cases, first look to see if the .pm file is generated.
156 my $base = $try->($xs);
157 return "${base}_pm.PL" if -f "${base}_pm.PL";
158 return "${base}.pm" if -f "${base}.pm";
161 die "No idea which .pm file corresponds to '$xs', so aborting";
164 # Key is the .pm file from which we check the version.
165 # Value is a reference to an array of files to check for differences
166 # The trivial case is a pure perl module, where the array holds one element,
167 # the perl module's file. The "fun" comes with XS modules, and the real fun
168 # with XS modules with more than one XS file, and "interesting" layouts.
172 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
174 next unless m/^(.*)\//;
176 next if $this_dir =~ $skip_dirs || exists $skip{$_};
177 next if exists $upstream_files{$_};
178 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
179 push @{$module_diffs{$_}}, $_;
180 } elsif (/\.xs\z/ && !/\bt\b/) {
181 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
185 unless (%module_diffs) {
186 print "1..1\nok 1 - No difference found\n" if $tap;
190 printf "1..%d\n" => scalar keys %module_diffs if $tap;
191 print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
194 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
195 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
198 foreach my $pm_file (sort keys %module_diffs) {
199 # git has already told us that the files differ, so no need to grab each as
200 # a blob from git, and do the comparison ourselves.
201 my $pm_version = eval {MM->parse_version($pm_file)};
202 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
203 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
206 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
207 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
209 } elsif (!defined $pm_version || $pm_version eq 'undef') {
210 my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
212 print STDERR "# $nok\n";
213 } elsif ($pm_version ne $orig_pm_version) { # good
214 print "ok $count - $pm_file\n" if $tap;
217 print "#\n# " . '-' x 75 . "\n"
218 . "# Version number ($pm_version) unchanged since"
219 . " $tag_to_compare, but contents have changed:\n#\n";
220 foreach (sort @{$module_diffs{$pm_file}}) {
221 print "# $_" for `$diff_cmd $q$_$q`;
223 print "# " . '-' x 75 . "\n";
225 if (exists $skip_versions{$pm_file}
226 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
227 print "ok $count - SKIP $pm_file version $pm_version\n";
229 my $nok = "not ok $count - $pm_file version $pm_version\n";
231 print STDERR "# $nok";
234 push @diff, @{$module_diffs{$pm_file}};
235 print "$pm_file version $pm_version\n";
240 sub get_file_from_git {
241 my ($file, $tag) = @_;
244 use open IN => ':raw';
245 return scalar `git --no-pager show $tag:$file 2>$null`;
251 system "$diff_cmd $q$_$q";