This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Detect empty git tag in cmpVERSION
[perl5.git] / Porting / cmpVERSION.pl
CommitLineData
f1c5bace
JH
1#!/usr/bin/perl -w
2
96048c95 3#
42e700c9
MJ
4# cmpVERSION - compare the current Perl source tree and a given tag
5# for modules that have identical version numbers but different contents.
f1c5bace 6#
2fb8ff88 7# with -d option, output the diffs too
844d3843
NC
8# with -x option, exclude files from modules where blead is not upstream
9#
10# (after all, there are tools like core-cpan-diff that can already deal with
11# them)
2547c837 12#
42e700c9 13# Original by slaven@rezic.de, modified by jhi and matt.w.johnson@gmail.com.
76733a60 14# Adaptation to produce TAP by Abigail, folded back into this file by Nicholas
f1c5bace
JH
15
16use strict;
96048c95 17use 5.006;
f1c5bace
JH
18
19use ExtUtils::MakeMaker;
e01fd32a 20use File::Spec::Functions qw(devnull);
00ad0422 21use Getopt::Long;
2547c837 22
76733a60 23my ($diffs, $exclude_upstream, $tag_to_compare, $tap);
00ad0422 24unless (GetOptions('diffs' => \$diffs,
844d3843 25 'exclude|x' => \$exclude_upstream,
e01fd32a 26 'tag=s' => \$tag_to_compare,
76733a60 27 'tap' => \$tap,
e01fd32a 28 ) && @ARGV == 0) {
76733a60 29 die "usage: $0 [ -d -x --tag TAG --tap]";
2547c837 30}
f1c5bace 31
e01fd32a
NC
32die "$0: This does not look like a Perl directory\n"
33 unless -f "perl.h" && -d "Porting";
34die "$0: 'This is a Perl directory but does not look like Git working directory\n"
35 unless -d ".git";
42e700c9 36
2385f340 37my $null = devnull();
68d2af03 38
e01fd32a
NC
39unless (defined $tag_to_compare) {
40 # Thanks to David Golden for this suggestion.
41
42 $tag_to_compare = `git describe --abbrev=0`;
43 chomp $tag_to_compare;
44}
45
0f85f13d
HS
46unless (length $tag_to_compare) {
47 die "$0: Git found, but no Git tags found\n"
48 unless $tap;
49 print "1..0 # SKIP: Git found, but no Git tags found\n";
50 exit 0;
51}
52
68d2af03 53my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42e700c9
MJ
54chomp $tag_exists;
55
76733a60
NC
56unless ($tag_exists eq $tag_to_compare) {
57 die "$0: '$tag_to_compare' is not a known Git tag\n" unless $tap;
58 print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
59 exit 0;
60}
f1c5bace 61
844d3843
NC
62my %upstream_files;
63if ($exclude_upstream) {
3a06b4ed
NC
64 unshift @INC, 'Porting';
65 require Maintainers;
66
844d3843
NC
67 for my $m (grep {!defined $Maintainers::Modules{$_}{UPSTREAM}
68 or $Maintainers::Modules{$_}{UPSTREAM} ne 'blead'}
69 keys %Maintainers::Modules) {
70 $upstream_files{$_} = 1 for Maintainers::get_module_files($m);
2fb8ff88
DM
71 }
72}
73
88830c88
JH
74# Files to skip from the check for one reason or another,
75# usually because they pull in their version from some other file.
76my %skip;
477acd91 77@skip{
42e700c9
MJ
78 'lib/Carp/Heavy.pm',
79 'lib/Config.pm', # no version number but contents will vary
80 'lib/Exporter/Heavy.pm',
81 'win32/FindExt.pm',
477acd91 82} = ();
76733a60
NC
83
84# Files to skip just for particular version(s),
85# usually due to some # mix-up
86
f008f53b 87my %skip_versions = (
76733a60
NC
88 # 'some/sample/file.pm' => [ '1.23', '1.24' ],
89 'dist/threads/lib/threads.pm' => [ '1.83' ],
90 );
76733a60 91
42e700c9
MJ
92my $skip_dirs = qr|^t/lib|;
93
96048c95
NC
94sub pm_file_from_xs {
95 my $xs = shift;
96
7ab1c1a4
NC
97 foreach my $try (sub {
98 # First try a .pm at the same level as the .xs file
99 # with the same basename
100 return shift =~ s/\.xs\z//r;
101 },
102 sub {
103 # Try for a (different) .pm at the same level, based
104 # on the directory name:
105 my ($path) = shift =~ m!^(.*)/!;
106 my ($last) = $path =~ m!([^-/]+)\z!;
107 return "$path/$last";
108 },
109 sub {
110 # Try to work out the extension's full package, and
111 # look for a .pm in lib/ based on that:
112 my ($path) = shift =~ m!^(.*)/!;
113 my ($last) = $path =~ m!([^/]+)\z!;
114 $last =~ tr !-!/!;
115 return "$path/lib/$last";
116 }) {
117 # For all cases, first look to see if the .pm file is generated.
118 my $base = $try->($xs);
119 return "${base}_pm.PL" if -f "${base}_pm.PL";
120 return "${base}.pm" if -f "${base}.pm";
121 }
42e700c9 122
96048c95
NC
123 die "No idea which .pm file corresponds to '$xs', so aborting";
124}
125
126# Key is the .pm file from which we check the version.
127# Value is a reference to an array of files to check for differences
128# The trivial case is a pure perl module, where the array holds one element,
129# the perl module's file. The "fun" comes with XS modules, and the real fun
130# with XS modules with more than one XS file, and "interesting" layouts.
131
132my %module_diffs;
133
7ac818b2 134foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
96048c95
NC
135 chomp;
136 next unless m/^(.*)\//;
137 my $this_dir = $1;
138 next if $this_dir =~ $skip_dirs || exists $skip{$_};
139 next if exists $upstream_files{$_};
7ab1c1a4 140 if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
96048c95 141 push @{$module_diffs{$_}}, $_;
e54eb2b6 142 } elsif (/\.xs\z/ && !/\bt\b/) {
96048c95
NC
143 push @{$module_diffs{pm_file_from_xs($_)}}, $_;
144 }
145}
42e700c9 146
96048c95 147unless (%module_diffs) {
76733a60
NC
148 print "1..1\nok 1 - No difference found\n" if $tap;
149 exit;
150}
151
96048c95 152printf "1..%d\n" => scalar keys %module_diffs if $tap;
76733a60
NC
153
154my $count;
155my $diff_cmd = "git --no-pager diff $tag_to_compare ";
7ba8970d 156my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
76733a60 157my (@diff);
42e700c9 158
96048c95
NC
159foreach my $pm_file (sort keys %module_diffs) {
160 # git has already told us that the files differ, so no need to grab each as
161 # a blob from git, and do the comparison ourselves.
42e700c9
MJ
162 my $pm_version = eval {MM->parse_version($pm_file)};
163 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
164 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
63ffcb73 165 ++$count;
97a78671
NC
166
167 if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
40f7a2b9
FC
168 print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
169 if $tap;
97a78671
NC
170 } elsif (!defined $pm_version || $pm_version eq 'undef') {
171 print "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n" if $tap;
172 } elsif ($pm_version ne $orig_pm_version) { # good
63ffcb73 173 print "ok $count - $pm_file\n" if $tap;
96048c95 174 } else {
76733a60 175 if ($tap) {
96048c95 176 foreach (sort @{$module_diffs{$pm_file}}) {
7ba8970d 177 print "# $_" for `$diff_cmd $q$_$q`;
76733a60 178 }
f008f53b
NC
179 if (exists $skip_versions{$pm_file}
180 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
63ffcb73 181 print "ok $count - SKIP $pm_file version $pm_version\n";
f008f53b 182 } else {
63ffcb73 183 print "not ok $count - $pm_file\n";
f008f53b 184 }
76733a60 185 } else {
96048c95 186 push @diff, @{$module_diffs{$pm_file}};
76733a60
NC
187 print "$pm_file\n";
188 }
189 }
42e700c9
MJ
190}
191
192sub get_file_from_git {
193 my ($file, $tag) = @_;
96048c95
NC
194 local $/;
195 return scalar `git --no-pager show $tag:$file 2>$null`;
42e700c9
MJ
196}
197
76733a60
NC
198if ($diffs) {
199 for (sort @diff) {
200 print "\n";
7ba8970d 201 system "$diff_cmd $q$_$q";
76733a60 202 }
2547c837 203}