This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables: Improve \p{xids} defn for early Unicodes
[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`;
43     chomp $tag_to_compare;
44 }
45
46 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
47 chomp $tag_exists;
48
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";
52     exit 0;
53 }
54
55 my %upstream_files;
56 if ($exclude_upstream) {
57     unshift @INC, 'Porting';
58     require Maintainers;
59
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);
64     }
65 }
66
67 # Files to skip from the check for one reason or another,
68 # usually because they pull in their version from some other file.
69 my %skip;
70 @skip{
71     'lib/Carp/Heavy.pm',
72     'lib/Config.pm',            # no version number but contents will vary
73     'lib/Exporter/Heavy.pm',
74     'win32/FindExt.pm',
75 } = ();
76
77 # Files to skip just for particular version(s),
78 # usually due to some # mix-up
79
80 my %skip_versions = (
81            # 'some/sample/file.pm' => [ '1.23', '1.24' ],
82            'dist/threads/lib/threads.pm' => [ '1.83' ],
83           );
84
85 my $skip_dirs = qr|^t/lib|;
86
87 sub pm_file_from_xs {
88     my $xs = shift;
89
90     # First try a .pm at the same level as the .xs file, with the same basename
91     my $pm = $xs;
92     $pm =~ s/xs\z/pm/;
93     return $pm if -f $pm;
94
95     # Try for a (different) .pm at the same level, based on the directory name:
96     my ($path) = $xs =~ m!^(.*)/!;
97     my ($last) = $path =~ m!([^-/]+)\z!;
98     $pm = "$path/$last.pm";
99     return $pm if -f $pm;
100
101     # Try to work out the extension's full package, and look for a .pm in lib/
102     # based on that:
103     ($last) = $path =~ m!([^/]+)\z!;
104     $last =~ tr !-!/!;
105     $pm = "$path/lib/$last.pm";
106     return $pm if -f $pm;
107
108     die "No idea which .pm file corresponds to '$xs', so aborting";
109 }
110
111 # Key is the .pm file from which we check the version.
112 # Value is a reference to an array of files to check for differences
113 # The trivial case is a pure perl module, where the array holds one element,
114 # the perl module's file. The "fun" comes with XS modules, and the real fun
115 # with XS modules with more than one XS file, and "interesting" layouts.
116
117 my %module_diffs;
118
119 foreach (`git --no-pager diff --name-only $tag_to_compare --diff-filter=ACMRTUXB`) {
120     chomp;
121     next unless m/^(.*)\//;
122     my $this_dir = $1;
123     next if $this_dir =~ $skip_dirs || exists $skip{$_};
124     next if exists $upstream_files{$_};
125     if (/\.pm\z/ || m|^lib/.*\.pl\z|) {
126         push @{$module_diffs{$_}}, $_;
127     } elsif (/\.xs\z/ && !/\bt\b/) {
128         push @{$module_diffs{pm_file_from_xs($_)}}, $_;
129     }
130 }
131
132 unless (%module_diffs) {
133     print "1..1\nok 1 - No difference found\n" if $tap;
134     exit;
135 }
136
137 printf "1..%d\n" => scalar keys %module_diffs if $tap;
138
139 my $count;
140 my $diff_cmd = "git --no-pager diff $tag_to_compare ";
141 my (@diff);
142
143 foreach my $pm_file (sort keys %module_diffs) {
144     # git has already told us that the files differ, so no need to grab each as
145     # a blob from git, and do the comparison ourselves.
146     my $pm_version = eval {MM->parse_version($pm_file)};
147     my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
148     my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
149     
150     if ((!defined $pm_version || !defined $orig_pm_version)
151         || ($pm_version eq 'undef' || $orig_pm_version eq 'undef') # sigh
152         || ($pm_version ne $orig_pm_version) # good
153        ) {
154         printf "ok %d - %s\n", ++$count, $pm_file if $tap;
155     } else {
156         if ($tap) {
157             foreach (sort @{$module_diffs{$pm_file}}) {
158                 print "# $_" for `$diff_cmd '$_'`;
159             }
160             if (exists $skip_versions{$pm_file}
161                 and grep $pm_version eq $_, @{$skip_versions{$pm_file}}) {
162                 printf "ok %d - SKIP $pm_file version $pm_version\n", ++$count;
163             } else {
164                 printf "not ok %d - %s\n", ++$count, $pm_file;
165             }
166         } else {
167             push @diff, @{$module_diffs{$pm_file}};
168             print "$pm_file\n";
169         }
170     }
171 }
172
173 sub get_file_from_git {
174     my ($file, $tag) = @_;
175     local $/;
176     return scalar `git --no-pager show $tag:$file 2>$null`;
177 }
178
179 if ($diffs) {
180     for (sort @diff) {
181         print "\n";
182         system "$diff_cmd '$_'";
183     }
184 }