This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Extract _cmd_l_calc_initial_end_and_i .
[perl5.git] / Porting / cmpVERSION.pl
1 #!/usr/bin/perl -w
2
3 #
4 # cmpVERSION - compare the current Perl source tree and a given tag
5 # for modules that have identical version numbers but different contents.
6 #
7 # with -d option, output the diffs too
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)
12 #
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
15
16 use strict;
17 use 5.006;
18
19 use ExtUtils::MakeMaker;
20 use File::Spec::Functions qw(devnull);
21 use Getopt::Long;
22
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,
27                    'tap' => \$tap,
28                    ) && @ARGV == 0) {
29     die "usage: $0 [ -d -x --tag TAG --tap]";
30 }
31
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"
35     unless -d ".git";
36
37 my $null = devnull();
38
39 unless (defined $tag_to_compare) {
40     # Thanks to David Golden for this suggestion.
41
42     $tag_to_compare = `git describe --abbrev=0 2>$null`;
43     chomp $tag_to_compare;
44 }
45
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
53 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
54 chomp $tag_exists;
55
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 }
61
62 my %upstream_files;
63 if ($exclude_upstream) {
64     unshift @INC, 'Porting';
65     require Maintainers;
66
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);
71     }
72 }
73
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;
77 @skip{
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',
82 } = ();
83
84 # Files to skip just for particular version(s),
85 # usually due to some # mix-up
86
87 my %skip_versions = (
88            # 'some/sample/file.pm' => [ '1.23', '1.24' ],
89            'dist/threads/lib/threads.pm' => [ '1.83' ],
90           );
91
92 my $skip_dirs = qr|^t/lib|;
93
94 sub pm_file_from_xs {
95     my $xs = shift;
96
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     }
122
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
132 my %module_diffs;
133
134 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
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{$_};
140     if (/\.pm\z/ || m|^lib/.*\.pl\z| || /_pm\.PL\z/) {
141         push @{$module_diffs{$_}}, $_;
142     } elsif (/\.xs\z/ && !/\bt\b/) {
143         push @{$module_diffs{pm_file_from_xs($_)}}, $_;
144     }
145 }
146
147 unless (%module_diffs) {
148     print "1..1\nok 1 - No difference found\n" if $tap;
149     exit;
150 }
151
152 printf "1..%d\n" => scalar keys %module_diffs if $tap;
153
154 my $count;
155 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
156 my $q = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? '"' : "'";
157 my (@diff);
158
159 foreach 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.
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)};
165     ++$count;
166
167     if (!defined $orig_pm_version || $orig_pm_version eq 'undef') { # sigh
168         print "ok $count - SKIP Can't parse \$VERSION in $pm_file\n"
169           if $tap;
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
173         print "ok $count - $pm_file\n" if $tap;
174     } else {
175         if ($tap) {
176             foreach (sort @{$module_diffs{$pm_file}}) {
177                 print "# $_" for `$diff_cmd $q$_$q`;
178             }
179             if (exists $skip_versions{$pm_file}
180                 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
181                 print "ok $count - SKIP $pm_file version $pm_version\n";
182             } else {
183                 print "not ok $count - $pm_file\n";
184             }
185         } else {
186             push @diff, @{$module_diffs{$pm_file}};
187             print "$pm_file\n";
188         }
189     }
190 }
191
192 sub get_file_from_git {
193     my ($file, $tag) = @_;
194     local $/;
195     return scalar `git --no-pager show $tag:$file 2>$null`;
196 }
197
198 if ($diffs) {
199     for (sort @diff) {
200         print "\n";
201         system "$diff_cmd $q$_$q";
202     }
203 }