X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=helpers%2Fbump-version;h=e0c1fa8cbc3ed1f5d13bc69f52854a892764ddd4;hb=f7d6fb435210129982ff62723e5bfcaeb56c63bf;hp=af3ecad578d448174359ed56fc614fdb37ea121c;hpb=22de1c6a69a6367802ffb0440ac5a0981ea785cd;p=p5sagit%2FDistar.git diff --git a/helpers/bump-version b/helpers/bump-version index af3ecad..e0c1fa8 100755 --- a/helpers/bump-version +++ b/helpers/bump-version @@ -3,24 +3,38 @@ use strict; use warnings FATAL => 'all'; use File::Find; -use Getopt::Long qw(:config gnu_compat); +use Getopt::Long qw(:config gnu_getopt); use File::Temp (); GetOptions( "git" => \my $git, + "force" => \my $force, + 'n|dry-run' => \my $dry_run, ) or die("Error in command line arguments\n"); -my ($old_version, $bump) = @ARGV; +my $old_version = shift + or die "no old version provided!\n"; +my $bump = shift; my ($new_decimal, $new_vstring) = bump_version($old_version, $bump); -warn "Bumping $old_version -> $new_decimal\n"; +warn "Bumping $old_version -> $new_decimal" . ($new_decimal ne $new_vstring ? " ($new_vstring)" : '') . "\n"; -my $file_match = qr{^(?: +my $file_match = qr{ Makefile\.PL |lib[/\\].*\.(?:pod|pm) |bin[/\\].* |script[/\\].* -)$}x; +}x; + +my $dir_match = qr{ + (?: + . + |lib + |bin + |script + ) + (?:[/\\]|$) +}x; my %files; if ($git) { @@ -30,7 +44,7 @@ if ($git) { for (`git ls-files`) { chomp; next - unless /$file_match/; + unless /^$file_match$/; $files{$_} = `git show HEAD:"$_"`; } } @@ -38,13 +52,18 @@ else { find({ no_chdir => 1, wanted => sub { - next + my $fn = File::Spec->abs2rel($_, '.'); + if (-d && $fn !~ /^$dir_match$/) { + $File::Find::prune = 1; + return; + } + return unless -f; - next - unless /(?:\.[\/\\])?$file_match/; - open my $fh, '<', $_ - or die "can't open $_: $!"; - $files{$_} = do { local $/; <$fh> }; + return + unless $fn =~ /^$file_match$/; + open my $fh, '<', $fn + or die "can't open $fn: $!"; + $files{$fn} = do { local $/; <$fh> }; close $fh; }, }, '.'); @@ -73,34 +92,41 @@ my $MAKE_RE = qr{ my $patch = ''; for my $file (sort keys %files) { - my $content = $files{$file}; - my $file_diff = ''; - my $re = $file eq 'Makefile.PL' ? $MAKE_RE : $FILE_RE; - my @lines = split /\r?\n/, $content; - for my $ln (0 .. $#lines) { - my $line = $lines[$ln]; - if ($lines[$ln] =~ $re) { - die "unable to bump version number in $file from $old_version, found $3\n" - if $3 ne $old_version; - my $comment = ($5 ? $5 . $new_vstring : ''); - my $new_line = "$1'$new_decimal'$4$comment$6"; - $file_diff .= <<"END_DIFF"; + eval { + my $content = $files{$file}; + my $file_diff = ''; + my $re = $file eq 'Makefile.PL' ? $MAKE_RE : $FILE_RE; + my @lines = split /\r?\n/, $content; + for my $ln (0 .. $#lines) { + my $line = $lines[$ln]; + if ($lines[$ln] =~ $re) { + die "unable to bump version number in $file from $old_version, found $3\n" + if !$force && $3 ne $old_version; + my $comment = ($5 ? $5 . $new_vstring : ''); + my $new_line = "$1'$new_decimal'$4$comment$6"; + $file_diff .= <<"END_DIFF"; @@ -@{[ $ln ]},3 +@{[ $ln ]},3 @@ $lines[$ln-1] -$lines[$ln] +$new_line $lines[$ln+1] END_DIFF + } } - } - if ($file_diff) { - $patch .= <<"END_HEADER" . $file_diff; + if ($file_diff) { + $patch .= <<"END_HEADER" . $file_diff; --- a/$file +++ b/$file END_HEADER - } + } + 1; + } or $dry_run ? warn($@) : die($@); } +if ($dry_run) { + print $patch; + exit; +} my ($fh, $file) = File::Temp::tempfile( "bump-version-XXXXXX", TMPDIR => 1 ); print { $fh } $patch; close $fh; @@ -120,11 +146,16 @@ sub version_parts { my $version = shift; my $dotted = $version =~ s/^v//; my @parts = split /\./, $version; - if (!$dotted && @parts == 2) { - my $dec = pop @parts; - $dec =~ s/_//g; - $dec .= "0" x ((- length $dec) % 3); - push @parts, $dec =~ /(\d{1,3})/g; + if (!$dotted && @parts <= 2) { + tr/_//d for @parts; + if (@parts == 2) { + my $dec = pop @parts; + $dec .= "0" x ((- length $dec) % 3); + push @parts, $dec =~ /(\d{1,3})/g; + } + } + elsif ($version =~ tr/_//) { + die "don't know how to handle underscores in dotted-decimal versions!\n"; } $_ += 0 for @parts; return @parts; @@ -141,28 +172,44 @@ sub bump_version { if (defined $bump_this) { if ($version =~ /^v/ || ($version =~ tr/.//) > 1) { + my $v = $version =~ /^(v)/ ? $1 : ''; my @parts = version_parts($version); + $bump_this += @parts + if $bump_this < 0; + $parts[$_] = 0 for $bump_this+1 .. $#parts; + $parts[$_] = 0 for $#parts+1 .. $bump_this; $parts[$bump_this]++; - $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts); $_ += 0 for @parts; - $new_vstring = join '.', @parts; - my $format = '%i.'. join '', map { '%03i' } @parts[1 .. $#parts]; - $new_decimal = sprintf $format, @parts; + $new_decimal = $new_vstring = $v . join '.', @parts; } else { my $alpha_pos = index($version, '_'); - $version =~ s/_//g; - $version =~ s/^(\d+)\.//; - my @parts = $1; - push @parts, $version =~ /(\d{1,3})/g; - my $format = '%i.'.join '', map { '%0'.length($_).'i' } @parts[1 .. $#parts]; - $parts[$bump_this]++; - $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts); - $new_decimal = sprintf $format, @parts; - substr $new_decimal, $alpha_pos, 0, '_' - if $alpha_pos != -1; - $new_vstring = join '.', version_parts($new_decimal); + if ($alpha_pos == -1) { + undef $alpha_pos; + } + else { + my $dot_pos = index($version, '.'); + $alpha_pos = $dot_pos == -1 ? -$alpha_pos : $alpha_pos - $dot_pos; + } + $new_decimal = $version; + $new_decimal =~ tr/_//d; + my $dec_len = $new_decimal =~ /(\.\d+)/ ? length($1) - 1 : 0; + if ($bump_this != -1) { + my $cut_len = $bump_this * 3; + $dec_len = $cut_len + if $dec_len < $cut_len; + $new_decimal =~ s/(\..{1,$cut_len}).*/$1/; + } + $new_decimal += 10 ** -($bump_this == -1 ? $dec_len : ($bump_this * 3)); + $new_decimal = sprintf "%.${dec_len}f", $new_decimal; + if (defined $alpha_pos) { + my $dot_pos = index($new_decimal, '.'); + $dot_pos = length $new_decimal + if $dot_pos == -1; + substr $new_decimal, $dot_pos + $alpha_pos, 0, '_'; + } + $new_vstring = 'v' . join '.', version_parts($new_decimal); } } elsif ($new =~ /^v?[0-9]+(?:[._][0-9]+)*$/) {