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/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
85 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm', # just a test module
86 'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
87 'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm', # just a test module
88 'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm', # just a test module
89 'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm', # just a test module
90 'cpan/Math-BigRat/t/Math/BigRat/Test.pm', # just a test module
91 'cpan/podlators/t/lib/Test/Podlators.pm', # just a test module
92 'cpan/podlators/t/lib/Test/RRA.pm', # just a test module
93 'cpan/podlators/t/lib/Test/RRA/Config.pm', # just a test module
94 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
95 'dist/Exporter/lib/Exporter/Heavy.pm',
97 'lib/Config.pm', # no version number but contents will vary
101 # Files to skip just for particular version(s),
102 # usually due to some # mix-up
104 my %skip_versions = (
105 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
108 my $skip_dirs = qr|^t/lib|;
110 sub pm_file_from_xs {
113 foreach my $try (sub {
114 # First try a .pm at the same level as the .xs file
115 # with the same basename
116 return shift =~ s/\.xs\z//r;
119 # Try for a (different) .pm at the same level, based
120 # on the directory name:
121 my ($path) = shift =~ m!^(.*)/!;
122 my ($last) = $path =~ m!([^-/]+)\z!;
123 return "$path/$last";
126 # Try to work out the extension's full package, and
127 # look for a .pm in lib/ based on that:
128 my ($path) = shift =~ m!^(.*)/!;
129 my ($last) = $path =~ m!([^/]+)\z!;
130 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
132 return "$path/lib/$last";
134 # For all cases, first look to see if the .pm file is generated.
135 my $base = $try->($xs);
136 return "${base}_pm.PL" if -f "${base}_pm.PL";
137 return "${base}.pm" if -f "${base}.pm";
140 die "No idea which .pm file corresponds to '$xs', so aborting";
143 # Key is the .pm file from which we check the version.
144 # Value is a reference to an array of files to check for differences
145 # The trivial case is a pure perl module, where the array holds one element,
146 # the perl module's file. The "fun" comes with XS modules, and the real fun
147 # with XS modules with more than one XS file, and "interesting" layouts.
151 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
153 next unless m/^(.*)\//;
155 next if $this_dir =~ $skip_dirs || exists $skip{$_};
156 next if exists $upstream_files{$_};
157 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
158 push @{$module_diffs{$_}}, $_;
159 } elsif (/\.xs\z/ && !/\bt\b/) {
160 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
164 unless (%module_diffs) {
165 print "1..1\nok 1 - No difference found\n" if $tap;
169 printf "1..%d\n" => scalar keys %module_diffs if $tap;
172 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
173 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
176 foreach my $pm_file (sort keys %module_diffs) {
177 # git has already told us that the files differ, so no need to grab each as
178 # a blob from git, and do the comparison ourselves.
179 my $pm_version = eval {MM->parse_version($pm_file)};
180 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
181 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
184 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
185 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
187 } elsif (!defined $pm_version || $pm_version eq 'undef') {
188 my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
190 print STDERR "# $nok\n";
191 } elsif ($pm_version ne $orig_pm_version) { # good
192 print "ok $count - $pm_file\n" if $tap;
195 foreach (sort @{$module_diffs{$pm_file}}) {
196 print "# $_" for `$diff_cmd $q$_$q`;
198 if (exists $skip_versions{$pm_file}
199 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
200 print "ok $count - SKIP $pm_file version $pm_version\n";
202 my $nok = "not ok $count - $pm_file version $pm_version\n";
204 print STDERR "# $nok";
207 push @diff, @{$module_diffs{$pm_file}};
208 print "$pm_file version $pm_version\n";
213 sub get_file_from_git {
214 my ($file, $tag) = @_;
217 use open IN => ':raw';
218 return scalar `git --no-pager show $tag:$file 2>$null`;
224 system "$diff_cmd $q$_$q";