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"
39 unless (defined $tag_to_compare) {
40 # Thanks to David Golden for this suggestion.
42 $tag_to_compare = `git describe --abbrev=0`;
43 chomp $tag_to_compare;
46 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
49 unless ($tag_exists eq $tag_to_compare) {
50 die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
51 print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
56 if ($exclude_upstream) {
57 unshift @INC, 'Porting';
60 for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
61 or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
62 keys %Maintainers::Modules) {
63 $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
67 # Files to skip from the check for one reason or another,
68 # usually because they pull in their version from some other file.
72 'lib/Config.pm', # no version number but contents will vary
73 'lib/Exporter/Heavy.pm',
77 # Files to skip just for particular version(s),
78 # usually due to some # mix-up
81 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
82 'dist/threads/lib/threads.pm' => [ '1.83' ],
85 my $skip_dirs = qr|^t/lib|;
90 foreach my $try (sub {
91 # First try a .pm at the same level as the .xs file
92 # with the same basename
93 return shift =~ s/\.xs\z//r;
96 # Try for a (different) .pm at the same level, based
97 # on the directory name:
98 my ($path) = shift =~ m!^(.*)/!;
99 my ($last) = $path =~ m!([^-/]+)\z!;
100 return "$path/$last";
103 # Try to work out the extension's full package, and
104 # look for a .pm in lib/ based on that:
105 my ($path) = shift =~ m!^(.*)/!;
106 my ($last) = $path =~ m!([^/]+)\z!;
108 return "$path/lib/$last";
110 # For all cases, first look to see if the .pm file is generated.
111 my $base = $try->($xs);
112 return "${base}_pm.PL" if -f "${base}_pm.PL";
113 return "${base}.pm" if -f "${base}.pm";
116 die "No idea which .pm file corresponds to '$xs', so aborting";
119 # Key is the .pm file from which we check the version.
120 # Value is a reference to an array of files to check for differences
121 # The trivial case is a pure perl module, where the array holds one element,
122 # the perl module's file. The "fun" comes with XS modules, and the real fun
123 # with XS modules with more than one XS file, and "interesting" layouts.
127 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
129 next unless m/^(.*)\//;
131 next if $this_dir =~ $skip_dirs || exists $skip{$_};
132 next if exists $upstream_files{$_};
133 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
134 push @{$module_diffs{$_}}, $_;
135 } elsif (/\.xs\z/ && !/\bt\b/) {
136 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
140 unless (%module_diffs) {
141 print "1..1\nok 1 - No difference found\n" if $tap;
145 printf "1..%d\n" => scalar keys %module_diffs if $tap;
148 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
149 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
152 foreach my $pm_file (sort keys %module_diffs) {
153 # git has already told us that the files differ, so no need to grab each as
154 # a blob from git, and do the comparison ourselves.
155 my $pm_version = eval {MM->parse_version($pm_file)};
156 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
157 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
160 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
161 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
163 } elsif (!defined $pm_version || $pm_version eq 'undef') {
164 print "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n" if $tap;
165 } elsif ($pm_version ne $orig_pm_version) { # good
166 print "ok $count - $pm_file\n" if $tap;
169 foreach (sort @{$module_diffs{$pm_file}}) {
170 print "# $_" for `$diff_cmd $q$_$q`;
172 if (exists $skip_versions{$pm_file}
173 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
174 print "ok $count - SKIP $pm_file version $pm_version\n";
176 print "not ok $count - $pm_file\n";
179 push @diff, @{$module_diffs{$pm_file}};
185 sub get_file_from_git {
186 my ($file, $tag) = @_;
188 return scalar `git --no-pager show $tag:$file 2>$null`;
194 system "$diff_cmd $q$_$q";