This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Pass the git tag to cmpVERSION.pl with a --tag argument.
[perl5.git] / Porting / cmpVERSION.pl
CommitLineData
f1c5bace
JH
1#!/usr/bin/perl -w
2
42e700c9
MJ
3# cmpVERSION - compare the current Perl source tree and a given tag
4# for modules that have identical version numbers but different contents.
f1c5bace 5#
2fb8ff88
DM
6# with -d option, output the diffs too
7# with -x option, exclude dual-life modules (after all, there are tools
8# like core-cpan-diff that can already deal with them)
2547c837 9#
42e700c9 10# Original by slaven@rezic.de, modified by jhi and matt.w.johnson@gmail.com.
f1c5bace
JH
11#
12
13use strict;
14
15use ExtUtils::MakeMaker;
16use File::Compare;
e01fd32a 17use File::Spec::Functions qw(devnull);
00ad0422 18use Getopt::Long;
2547c837 19
e01fd32a 20my ($diffs, $exclude_dual, $tag_to_compare);
00ad0422
NC
21unless (GetOptions('diffs' => \$diffs,
22 'exclude|x' => \$exclude_dual,
e01fd32a
NC
23 'tag=s' => \$tag_to_compare,
24 ) && @ARGV == 0) {
25 die "usage: $0 [ -d -x --tag TAG]";
2547c837 26}
f1c5bace 27
e01fd32a
NC
28die "$0: This does not look like a Perl directory\n"
29 unless -f "perl.h" && -d "Porting";
30die "$0: 'This is a Perl directory but does not look like Git working directory\n"
31 unless -d ".git";
42e700c9 32
2385f340 33my $null = devnull();
68d2af03 34
e01fd32a
NC
35unless (defined $tag_to_compare) {
36 # Thanks to David Golden for this suggestion.
37
38 $tag_to_compare = `git describe --abbrev=0`;
39 chomp $tag_to_compare;
40}
41
68d2af03 42my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42e700c9
MJ
43chomp $tag_exists;
44
45die "$0: '$tag_to_compare' is not a known Git tag\n"
46 unless $tag_exists eq $tag_to_compare;
f1c5bace 47
2fb8ff88 48my %dual_files;
00ad0422 49if ($exclude_dual) {
3a06b4ed
NC
50 unshift @INC, 'Porting';
51 require Maintainers;
52
53 for my $m (grep $Maintainers::Modules{$_}{CPAN},
54 keys %Maintainers::Modules) {
42e700c9 55 $dual_files{$_} = 1 for Maintainers::get_module_files($m);
2fb8ff88
DM
56 }
57}
58
88830c88
JH
59# Files to skip from the check for one reason or another,
60# usually because they pull in their version from some other file.
61my %skip;
477acd91 62@skip{
42e700c9
MJ
63 'lib/Carp/Heavy.pm',
64 'lib/Config.pm', # no version number but contents will vary
65 'lib/Exporter/Heavy.pm',
66 'win32/FindExt.pm',
477acd91 67} = ();
42e700c9
MJ
68my $skip_dirs = qr|^t/lib|;
69
70my @all_diffs = `git --no-pager diff --name-only $tag_to_compare`;
71chomp @all_diffs;
72
73my @module_diffs = grep {
74 my $this_dir;
75 $this_dir = $1 if m/^(.*)\//;
76 /\.pm$/ &&
77 (!defined($this_dir) || ($this_dir !~ $skip_dirs)) &&
515cd855 78 !exists $skip{$_} &&
42e700c9
MJ
79 !exists $dual_files{$_}
80} @all_diffs;
81
82my (@output_files, @output_diffs);
83
84foreach my $pm_file (@module_diffs) {
85 (my $xs_file = $pm_file) =~ s/\.pm$/.xs/;
86 my $pm_eq = compare_git_file($pm_file, $tag_to_compare);
87 next unless defined $pm_eq;
88 my $xs_eq = 1;
89 if (-e $xs_file) {
90 $xs_eq = compare_git_file($xs_file, $tag_to_compare);
91 next unless defined $xs_eq;
92 }
93 next if ($pm_eq && $xs_eq);
94 my $pm_version = eval {MM->parse_version($pm_file)};
95 my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
96 my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
300da4a1
DG
97 next if ( ! defined $pm_version || ! defined $orig_pm_version );
98 next if ( $pm_version eq 'undef' || $orig_pm_version eq 'undef' ); # sigh
99 next if $pm_version ne $orig_pm_version;
42e700c9
MJ
100 push @output_files, $pm_file;
101 push @output_diffs, $pm_file unless $pm_eq;
102 push @output_diffs, $xs_file unless $xs_eq;
103}
104
105sub compare_git_file {
106 my ($file, $tag) = @_;
68d2af03 107 open(my $orig_fh, "-|", "git --no-pager show $tag:$file 2>$null");
42e700c9
MJ
108 return undef if eof($orig_fh);
109 my $is_eq = compare($file, $orig_fh) == 0;
110 close($orig_fh);
111 return $is_eq;
112}
113
114sub get_file_from_git {
115 my ($file, $tag) = @_;
116 local $/ = undef;
68d2af03 117 my $file_content = `git --no-pager show $tag:$file 2>$null`;
42e700c9
MJ
118 return $file_content;
119}
120
121for (sort @output_files) {
2547c837
DM
122 print "$_\n";
123}
42e700c9 124
00ad0422 125exit unless $diffs;
42e700c9
MJ
126
127for (sort @output_diffs) {
2547c837 128 print "\n";
42e700c9 129 system "git --no-pager diff $tag_to_compare '$_'";
2547c837 130}
f1c5bace 131