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 # the hash of the initial commit in perl.git (perl-1.0)
42 my $commit = '8d063cd8450e59ea1c611a2f4f5a21059a2804f1';
43 my $out = `git rev-parse --verify --quiet '$commit^{commit}'`;
50 die "$0: This is a Perl directory but does not look like Git working directory\n"
56 unless (defined $tag_to_compare) {
59 $check = `git describe --abbrev=0 $check 2>$null`;
61 last unless $check =~ /-RC/;
64 $tag_to_compare = $check;
65 # Thanks to David Golden for this suggestion.
69 unless (length $tag_to_compare) {
70 die "$0: Git found, but no Git tags found\n"
72 print "1..0 # SKIP: Git found, but no Git tags found\n";
76 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
79 unless ($tag_exists eq $tag_to_compare) {
80 die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
81 print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
86 if ($exclude_upstream) {
87 unshift @INC, 'Porting';
90 for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
91 or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
92 keys %Maintainers::Modules) {
93 $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
97 # Files to skip from the check for one reason or another,
98 # usually because they pull in their version from some other file.
101 'cpan/Digest/t/lib/Digest/Dummy.pm', # just a test module
102 'cpan/ExtUtils-Install/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
103 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module
104 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm', # just a test module
105 'cpan/IO-Compress/lib/File/GlobMapper.pm', # upstream needs to supply $VERSION
106 'cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm', # just a test module
107 'cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm', # just a test module
108 'cpan/Math-BigInt/t/Math/BigInt/Scalar.pm', # just a test module
109 'cpan/Math-BigInt/t/Math/BigInt/Subclass.pm', # just a test module
110 'cpan/Math-BigRat/t/Math/BigRat/Test.pm', # just a test module
111 'cpan/Module-Load/t/to_load/LoadIt.pm', # just a test module
112 'cpan/Module-Load/t/to_load/Must/Be/Loaded.pm', # just a test module
113 'cpan/Module-Load-Conditional/t/test_lib/a/X.pm', # just a test module
114 'cpan/Module-Load-Conditional/t/test_lib/b/X.pm', # just a test module
115 'cpan/Module-Load-Conditional/t/to_load/Commented.pm', # just a test module
116 'cpan/Module-Load-Conditional/t/to_load/HereDoc.pm', # just a test module
117 'cpan/Module-Load-Conditional/t/to_load/InPod.pm', # just a test module
118 'cpan/Module-Load-Conditional/t/to_load/LoadIt.pm', # just a test module
119 'cpan/Module-Load-Conditional/t/to_load/MustBe/Loaded.pm', # just a test module
120 'cpan/Module-Load-Conditional/t/to_load/NotMain.pm', # just a test module
121 'cpan/Module-Load-Conditional/t/to_load/NotX.pm', # just a test module
122 'cpan/Pod-Usage/t/inc/Pod/InputObjects.pm', # just a test module
123 'cpan/Pod-Usage/t/inc/Pod/Parser.pm', # just a test module
124 'cpan/Pod-Usage/t/inc/Pod/PlainText.pm', # just a test module
125 'cpan/Pod-Usage/t/inc/Pod/Select.pm', # just a test module
126 'cpan/podlators/t/lib/Test/Podlators.pm', # just a test module
127 'cpan/podlators/t/lib/Test/RRA.pm', # just a test module
128 'cpan/podlators/t/lib/Test/RRA/Config.pm', # just a test module
129 'cpan/version/t/coretests.pm', # just a test module
130 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code
131 'dist/Exporter/lib/Exporter/Heavy.pm',
132 'dist/Module-CoreList/lib/Module/CoreList.pm',
133 'dist/Module-CoreList/lib/Module/CoreList/Utils.pm',
135 'lib/Config.pm', # no version number but contents will vary
139 # Files to skip just for particular version(s),
140 # usually due to some # mix-up
142 my %skip_versions = (
143 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
146 my $skip_dirs = qr|^t/lib|;
148 sub pm_file_from_xs {
151 foreach my $try (sub {
152 # First try a .pm at the same level as the .xs file
153 # with the same basename
154 return shift =~ s/\.xs\z//r;
157 # Try for a (different) .pm at the same level, based
158 # on the directory name:
159 my ($path) = shift =~ m!^(.*)/!;
160 my ($last) = $path =~ m!([^-/]+)\z!;
161 return "$path/$last";
164 # Try to work out the extension's full package, and
165 # look for a .pm in lib/ based on that:
166 my ($path) = shift =~ m!^(.*)/!;
167 my ($last) = $path =~ m!([^/]+)\z!;
168 $last = 'List-Util' if $last eq 'Scalar-List-Utils';
170 return "$path/lib/$last";
172 # For all cases, first look to see if the .pm file is generated.
173 my $base = $try->($xs);
174 return "${base}_pm.PL" if -f "${base}_pm.PL";
175 return "${base}.pm" if -f "${base}.pm";
178 die "No idea which .pm file corresponds to '$xs', so aborting";
181 # Key is the .pm file from which we check the version.
182 # Value is a reference to an array of files to check for differences
183 # The trivial case is a pure perl module, where the array holds one element,
184 # the perl module's file. The "fun" comes with XS modules, and the real fun
185 # with XS modules with more than one XS file, and "interesting" layouts.
189 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
191 next unless m/^(.*)\//;
193 next if $this_dir =~ $skip_dirs || exists $skip{$_};
194 next if exists $upstream_files{$_};
195 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
196 push @{$module_diffs{$_}}, $_;
197 } elsif (/\.xs\z/ && !/\bt\b/) {
198 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
202 unless (%module_diffs) {
203 print "1..1\nok 1 - No difference found\n" if $tap;
207 printf "1..%d\n" => scalar keys %module_diffs if $tap;
208 print "#\n# Comparing against $tag_to_compare ....\n#\n" if $tap;
211 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
212 my $q = ($^O eq 'MSWin32' || $^O eq 'VMS') ? '"' : "'";
215 foreach my $pm_file (sort keys %module_diffs) {
216 # git has already told us that the files differ, so no need to grab each as
217 # a blob from git, and do the comparison ourselves.
218 my $pm_version = eval {MM->parse_version($pm_file)};
219 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
220 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
223 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
224 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
226 } elsif (!defined $pm_version || $pm_version eq 'undef') {
227 my $nok = "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n";
229 print STDERR "# $nok\n";
230 } elsif ($pm_version ne $orig_pm_version) { # good
231 print "ok $count - $pm_file\n" if $tap;
234 print "#\n# " . '-' x 75 . "\n"
235 . "# Version number ($pm_version) unchanged since"
236 . " $tag_to_compare, but contents have changed:\n#\n";
237 foreach (sort @{$module_diffs{$pm_file}}) {
238 print "# $_" for `$diff_cmd $q$_$q`;
240 print "# " . '-' x 75 . "\n";
242 if (exists $skip_versions{$pm_file}
243 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
244 print "ok $count - SKIP $pm_file version $pm_version\n";
246 my $nok = "not ok $count - $pm_file version $pm_version\n";
248 print STDERR "# $nok";
251 push @diff, @{$module_diffs{$pm_file}};
252 print "$pm_file version $pm_version\n";
257 sub get_file_from_git {
258 my ($file, $tag) = @_;
261 use open IN => ':raw';
262 return scalar `git --no-pager show $tag:$file 2>$null`;
268 system "$diff_cmd $q$_$q";