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 NC |
39 | unless (defined $tag_to_compare) { |
40 | # Thanks to David Golden for this suggestion. | |
41 | ||
1870a3e0 | 42 | $tag_to_compare = `git describe --abbrev=0 2>$null`; |
e01fd32a NC |
43 | chomp $tag_to_compare; |
44 | } | |
45 | ||
0f85f13d HS |
46 | unless (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 | 53 | my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`; |
42e700c9 MJ |
54 | chomp $tag_exists; |
55 | ||
76733a60 NC |
56 | unless ($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 |
62 | my %upstream_files; |
63 | if ($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. | |
76 | my %skip; | |
477acd91 | 77 | @skip{ |
955b6d35 SH |
78 | 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/BFD.pm', # just a test module |
79 | 'cpan/ExtUtils-MakeMaker/t/lib/MakeMaker/Test/Setup/XS.pm', # just a test module | |
80 | 'dist/Attribute-Handlers/demo/MyClass.pm', # it's just demonstration code | |
81 | 'dist/Exporter/lib/Exporter/Heavy.pm', | |
42e700c9 MJ |
82 | 'lib/Carp/Heavy.pm', |
83 | 'lib/Config.pm', # no version number but contents will vary | |
42e700c9 | 84 | 'win32/FindExt.pm', |
477acd91 | 85 | } = (); |
76733a60 NC |
86 | |
87 | # Files to skip just for particular version(s), | |
88 | # usually due to some # mix-up | |
89 | ||
f008f53b | 90 | my %skip_versions = ( |
76733a60 NC |
91 | # 'some/sample/file.pm' => [ '1.23', '1.24' ], |
92 | 'dist/threads/lib/threads.pm' => [ '1.83' ], | |
93 | ); | |
76733a60 | 94 | |
42e700c9 MJ |
95 | my $skip_dirs = qr|^t/lib|; |
96 | ||
96048c95 NC |
97 | sub pm_file_from_xs { |
98 | my $xs = shift; | |
99 | ||
7ab1c1a4 NC |
100 | foreach my $try (sub { |
101 | # First try a .pm at the same level as the .xs file | |
102 | # with the same basename | |
103 | return shift =~ s/\.xs\z//r; | |
104 | }, | |
105 | sub { | |
106 | # Try for a (different) .pm at the same level, based | |
107 | # on the directory name: | |
108 | my ($path) = shift =~ m!^(.*)/!; | |
109 | my ($last) = $path =~ m!([^-/]+)\z!; | |
110 | return "$path/$last"; | |
111 | }, | |
112 | sub { | |
113 | # Try to work out the extension's full package, and | |
114 | # look for a .pm in lib/ based on that: | |
115 | my ($path) = shift =~ m!^(.*)/!; | |
116 | my ($last) = $path =~ m!([^/]+)\z!; | |
117 | $last =~ tr !-!/!; | |
118 | return "$path/lib/$last"; | |
119 | }) { | |
120 | # For all cases, first look to see if the .pm file is generated. | |
121 | my $base = $try->($xs); | |
122 | return "${base}_pm.PL" if -f "${base}_pm.PL"; | |
123 | return "${base}.pm" if -f "${base}.pm"; | |
124 | } | |
42e700c9 | 125 | |
96048c95 NC |
126 | die "No idea which .pm file corresponds to '$xs', so aborting"; |
127 | } | |
128 | ||
129 | # Key is the .pm file from which we check the version. | |
130 | # Value is a reference to an array of files to check for differences | |
131 | # The trivial case is a pure perl module, where the array holds one element, | |
132 | # the perl module's file. The "fun" comes with XS modules, and the real fun | |
133 | # with XS modules with more than one XS file, and "interesting" layouts. | |
134 | ||
135 | my %module_diffs; | |
136 | ||
7ac818b2 | 137 | foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) { |
96048c95 NC |
138 | chomp; |
139 | next unless m/^(.*)\//; | |
140 | my $this_dir = $1; | |
141 | next if $this_dir =~ $skip_dirs || exists $skip{$_}; | |
142 | next if exists $upstream_files{$_}; | |
7ab1c1a4 | 143 | if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) { |
96048c95 | 144 | push @{$module_diffs{$_}}, $_; |
e54eb2b6 | 145 | } elsif (/\.xs\z/ && !/\bt\b/) { |
96048c95 NC |
146 | push @{$module_diffs{pm_file_from_xs($_)}}, $_; |
147 | } | |
148 | } | |
42e700c9 | 149 | |
96048c95 | 150 | unless (%module_diffs) { |
76733a60 NC |
151 | print "1..1\nok 1 - No difference found\n" if $tap; |
152 | exit; | |
153 | } | |
154 | ||
96048c95 | 155 | printf "1..%d\n" => scalar keys %module_diffs if $tap; |
76733a60 NC |
156 | |
157 | my $count; | |
158 | my $diff_cmd = "git --no-pager diff $tag_to_compare "; | |
7ba8970d | 159 | my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'"; |
76733a60 | 160 | my (@diff); |
42e700c9 | 161 | |
96048c95 NC |
162 | foreach my $pm_file (sort keys %module_diffs) { |
163 | # git has already told us that the files differ, so no need to grab each as | |
164 | # a blob from git, and do the comparison ourselves. | |
42e700c9 MJ |
165 | my $pm_version = eval {MM->parse_version($pm_file)}; |
166 | my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare); | |
167 | my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)}; | |
63ffcb73 | 168 | ++$count; |
97a78671 NC |
169 | |
170 | if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh | |
40f7a2b9 FC |
171 | print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n" |
172 | if $tap; | |
97a78671 NC |
173 | } elsif (!defined $pm_version || $pm_version eq 'undef') { |
174 | print "not ok $count - in $pm_file version was $orig_pm_version, now unparsable\n" if $tap; | |
175 | } elsif ($pm_version ne $orig_pm_version) { # good | |
63ffcb73 | 176 | print "ok $count - $pm_file\n" if $tap; |
96048c95 | 177 | } else { |
76733a60 | 178 | if ($tap) { |
96048c95 | 179 | foreach (sort @{$module_diffs{$pm_file}}) { |
7ba8970d | 180 | print "# $_" for `$diff_cmd $q$_$q`; |
76733a60 | 181 | } |
f008f53b NC |
182 | if (exists $skip_versions{$pm_file} |
183 | and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) { | |
63ffcb73 | 184 | print "ok $count - SKIP $pm_file version $pm_version\n"; |
f008f53b | 185 | } else { |
63ffcb73 | 186 | print "not ok $count - $pm_file\n"; |
f008f53b | 187 | } |
76733a60 | 188 | } else { |
96048c95 | 189 | push @diff, @{$module_diffs{$pm_file}}; |
76733a60 NC |
190 | print "$pm_file\n"; |
191 | } | |
192 | } | |
42e700c9 MJ |
193 | } |
194 | ||
195 | sub get_file_from_git { | |
196 | my ($file, $tag) = @_; | |
96048c95 | 197 | local $/; |
f378d7a6 TC |
198 | |
199 | use open IN => ':raw'; | |
96048c95 | 200 | return scalar `git --no-pager show $tag:$file 2>$null`; |
42e700c9 MJ |
201 | } |
202 | ||
76733a60 NC |
203 | if ($diffs) { |
204 | for (sort @diff) { | |
205 | print "\n"; | |
7ba8970d | 206 | system "$diff_cmd $q$_$q"; |
76733a60 | 207 | } |
2547c837 | 208 | } |