Commit | Line | Data |
---|---|---|
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 | |
16 | use strict; | |
96048c95 | 17 | use 5.006; |
f1c5bace JH |
18 | |
19 | use ExtUtils::MakeMaker; | |
e01fd32a | 20 | use File::Spec::Functions qw(devnull); |
00ad0422 | 21 | use Getopt::Long; |
2547c837 | 22 | |
76733a60 | 23 | my ($diffs, $exclude_upstream, $tag_to_compare, $tap); |
00ad0422 | 24 | unless (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 |
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" | |
6b44ec68 | 35 | unless (-d ".git" || (exists $ENV{GIT_DIR} && -d $ENV{GIT_DIR})); |
42e700c9 | 36 | |
2385f340 | 37 | my $null = devnull(); |
68d2af03 | 38 | |
e01fd32a | 39 | unless (defined $tag_to_compare) { |
d7d4eceb GK |
40 | my $check = 'HEAD'; |
41 | while(1) { | |
42 | $check = `git describe --abbrev=0 $check 2>$null`; | |
43 | chomp $check; | |
44 | last unless $check =~ /-RC/; | |
45 | $check .= '^'; | |
46 | } | |
47 | $tag_to_compare = $check; | |
e01fd32a NC |
48 | # Thanks to David Golden for this suggestion. |
49 | ||
e01fd32a NC |
50 | } |
51 | ||
0f85f13d HS |
52 | unless (length $tag_to_compare) { |
53 | die "$0: Git found, but no Git tags found\n" | |
54 | unless $tap; | |
55 | print "1..0 # SKIP: Git found, but no Git tags found\n"; | |
56 | exit 0; | |
57 | } | |
58 | ||
68d2af03 | 59 | my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`; |
42e700c9 MJ |
60 | chomp $tag_exists; |
61 | ||
76733a60 NC |
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"; | |
65 | exit 0; | |
66 | } | |
f1c5bace | 67 | |
844d3843 NC |
68 | my %upstream_files; |
69 | if ($exclude_upstream) { | |
3a06b4ed NC |
70 | unshift @INC, 'Porting'; |
71 | require Maintainers; | |
72 | ||
844d3843 NC |
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); | |
2fb8ff88 DM |
77 | } |
78 | } | |
79 | ||
88830c88 JH |
80 | # Files to skip from the check for one reason or another, |
81 | # usually because they pull in their version from some other file. | |
82 | my %skip; | |
477acd91 | 83 | @skip{ |
955b6d35 SH |
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 | 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code | |
87 | 'dist/Exporter/lib/Exporter/Heavy.pm', | |
42e700c9 MJ |
88 | 'lib/Carp/Heavy.pm', |
89 | 'lib/Config.pm', # no version number but contents will vary | |
42e700c9 | 90 | 'win32/FindExt.pm', |
477acd91 | 91 | } = (); |
76733a60 NC |
92 | |
93 | # Files to skip just for particular version(s), | |
94 | # usually due to some # mix-up | |
95 | ||
f008f53b | 96 | my %skip_versions = ( |
76733a60 | 97 | # 'some/sample/file.pm' => [ '1.23', '1.24' ], |
76733a60 | 98 | ); |
76733a60 | 99 | |
42e700c9 MJ |
100 | my $skip_dirs = qr|^t/lib|; |
101 | ||
96048c95 NC |
102 | sub pm_file_from_xs { |
103 | my $xs = shift; | |
104 | ||
7ab1c1a4 NC |
105 | foreach my $try (sub { |
106 | # First try a .pm at the same level as the .xs file | |
107 | # with the same basename | |
108 | return shift =~ s/\.xs\z//r; | |
109 | }, | |
110 | sub { | |
111 | # Try for a (different) .pm at the same level, based | |
112 | # on the directory name: | |
113 | my ($path) = shift =~ m!^(.*)/!; | |
114 | my ($last) = $path =~ m!([^-/]+)\z!; | |
115 | return "$path/$last"; | |
116 | }, | |
117 | sub { | |
118 | # Try to work out the extension's full package, and | |
119 | # look for a .pm in lib/ based on that: | |
120 | my ($path) = shift =~ m!^(.*)/!; | |
121 | my ($last) = $path =~ m!([^/]+)\z!; | |
4421e1e9 | 122 | $last = 'List-Util' if $last eq 'Scalar-List-Utils'; |
7ab1c1a4 NC |
123 | $last =~ tr !-!/!; |
124 | return "$path/lib/$last"; | |
125 | }) { | |
126 | # For all cases, first look to see if the .pm file is generated. | |
127 | my $base = $try->($xs); | |
128 | return "${base}_pm.PL" if -f "${base}_pm.PL"; | |
129 | return "${base}.pm" if -f "${base}.pm"; | |
130 | } | |
42e700c9 | 131 | |
96048c95 NC |
132 | die "No idea which .pm file corresponds to '$xs', so aborting"; |
133 | } | |
134 | ||
135 | # Key is the .pm file from which we check the version. | |
136 | # Value is a reference to an array of files to check for differences | |
137 | # The trivial case is a pure perl module, where the array holds one element, | |
138 | # the perl module's file. The "fun" comes with XS modules, and the real fun | |
139 | # with XS modules with more than one XS file, and "interesting" layouts. | |
140 | ||
141 | my %module_diffs; | |
142 | ||
7ac818b2 | 143 | foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) { |
96048c95 NC |
144 | chomp; |
145 | next unless m/^(.*)\//; | |
146 | my $this_dir = $1; | |
147 | next if $this_dir =~ $skip_dirs || exists $skip{$_}; | |
148 | next if exists $upstream_files{$_}; | |
7ab1c1a4 | 149 | if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) { |
96048c95 | 150 | push @{$module_diffs{$_}}, $_; |
e54eb2b6 | 151 | } elsif (/\.xs\z/ && !/\bt\b/) { |
96048c95 NC |
152 | push @{$module_diffs{pm_file_from_xs($_)}}, $_; |
153 | } | |
154 | } | |
42e700c9 | 155 | |
96048c95 | 156 | unless (%module_diffs) { |
76733a60 NC |
157 | print "1..1\nok 1 - No difference found\n" if $tap; |
158 | exit; | |
159 | } | |
160 | ||
96048c95 | 161 | printf "1..%d\n" => scalar keys %module_diffs if $tap; |
76733a60 NC |
162 | |
163 | my $count; | |
164 | my $diff_cmd = "git --no-pager diff $tag_to_compare "; | |
7ba8970d | 165 | my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'"; |
76733a60 | 166 | my (@diff); |
42e700c9 | 167 | |
96048c95 NC |
168 | foreach my $pm_file (sort keys %module_diffs) { |
169 | # git has already told us that the files differ, so no need to grab each as | |
170 | # a blob from git, and do the comparison ourselves. | |
42e700c9 MJ |
171 | my $pm_version = eval {MM->parse_version($pm_file)}; |
172 | my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare); | |
173 | my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)}; | |
63ffcb73 | 174 | ++$count; |
97a78671 NC |
175 | |
176 | if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh | |
40f7a2b9 FC |
177 | print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n" |
178 | if $tap; | |
97a78671 NC |
179 | } elsif (!defined $pm_version || $pm_version eq 'undef') { |
180 | print "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n" if $tap; | |
181 | } elsif ($pm_version ne $orig_pm_version) { # good | |
63ffcb73 | 182 | print "ok $count - $pm_file\n" if $tap; |
96048c95 | 183 | } else { |
76733a60 | 184 | if ($tap) { |
96048c95 | 185 | foreach (sort @{$module_diffs{$pm_file}}) { |
7ba8970d | 186 | print "# $_" for `$diff_cmd $q$_$q`; |
76733a60 | 187 | } |
f008f53b NC |
188 | if (exists $skip_versions{$pm_file} |
189 | and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) { | |
63ffcb73 | 190 | print "ok $count - SKIP $pm_file version $pm_version\n"; |
f008f53b | 191 | } else { |
63ffcb73 | 192 | print "not ok $count - $pm_file\n"; |
f008f53b | 193 | } |
76733a60 | 194 | } else { |
96048c95 | 195 | push @diff, @{$module_diffs{$pm_file}}; |
76733a60 NC |
196 | print "$pm_file\n"; |
197 | } | |
198 | } | |
42e700c9 MJ |
199 | } |
200 | ||
201 | sub get_file_from_git { | |
202 | my ($file, $tag) = @_; | |
96048c95 | 203 | local $/; |
f378d7a6 TC |
204 | |
205 | use open IN => ':raw'; | |
96048c95 | 206 | return scalar `git --no-pager show $tag:$file 2>$null`; |
42e700c9 MJ |
207 | } |
208 | ||
76733a60 NC |
209 | if ($diffs) { |
210 | for (sort @diff) { | |
211 | print "\n"; | |
7ba8970d | 212 | system "$diff_cmd $q$_$q"; |
76733a60 | 213 | } |
2547c837 | 214 | } |