4 use warnings FATAL => 'all';
6 use Getopt::Long qw(:config gnu_getopt);
11 'force' => \my $force,
12 'n|dry-run' => \my $dry_run,
13 'stable' => \my $stable,
14 'alpha|beta|dev' => \my $alpha,
15 ) or die("Error in command line arguments\n");
17 my $old_version = shift
18 or die "no old version provided!\n";
20 my ($new_decimal, $new_vstring) = bump_version($old_version, $bump);
21 die "--stable and --alpha are incompatible!\n"
22 if $stable and $alpha;
24 warn "Bumping $old_version -> $new_decimal" . ($new_decimal ne $new_vstring ? " ($new_vstring)" : '') . "\n";
28 |lib[/\\].*\.(?:pod|pm)
45 if (system "git diff --quiet --cached HEAD") {
46 die "Staged changes!\n";
48 for (`git ls-files`) {
51 unless /^$file_match$/;
52 $files{$_} = `git show HEAD:"$_"`;
59 my $fn = File::Spec->abs2rel($_, '.');
60 if (-d && $fn !~ /^$dir_match/) {
61 $File::Find::prune = 1;
67 unless $fn =~ /^$file_match$/;
69 or die "can't open $fn: $!";
70 $files{$fn} = do { local $/; <$fh> };
77 (^.* \$VERSION \s* = \s* )
78 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
87 (^.* ['"]?version['"] \s* => \s* )
88 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
98 for my $file (sort keys %files) {
100 my $content = $files{$file};
102 my $re = $file eq 'Makefile.PL' ? $MAKE_RE : $FILE_RE;
103 my @lines = split /\r?\n/, $content;
105 for my $ln (0 .. $#lines) {
106 my $line = $lines[$ln];
108 if ($in_pod && $line =~ /^=cut$/) {
111 elsif ($line =~ /^=\w+/) {
114 elsif (!$in_pod && $line =~ $re) {
115 die "unable to bump version number in $file from $old_version, found $3\n"
116 if !$force && $3 ne $old_version;
117 my $comment = ($5 ? $5 . $new_vstring : '');
118 $new_line = "$1'$new_decimal'$4$comment$6";
120 elsif ($in_pod =~ /\bversion\b/i && $lines[$ln] =~ /^((?:version\s+)?)v?([0-9]+(?:[._][0-9]+)*)$/) {
121 die "unable to bump version number in $file from $old_version, found $2\n"
122 if !$force && $2 ne $old_version;
123 $new_line = "$1$new_decimal";
125 if (defined $new_line) {
126 $file_diff .= <<"END_DIFF";
127 @@ -@{[ $ln ]},3 +@{[ $ln ]},3 @@
136 $patch .= <<"END_HEADER" . $file_diff;
142 } or $dry_run ? warn($@) : die($@);
149 my ($fh, $file) = File::Temp::tempfile( "bump-version-XXXXXX", TMPDIR => 1 );
150 print { $fh } $patch;
152 system qw(git --no-pager apply --apply --stat), $file
156 system qw(git apply --cached), $file
159 my $message = "Bumping version to $new_decimal";
160 system qw(git commit -m), $message
166 my $dotted = $version =~ s/^v//;
167 my @parts = split /\./, $version;
168 if (!$dotted && @parts <= 2) {
171 my $dec = pop @parts;
172 $dec .= "0" x ((- length $dec) % 3);
173 push @parts, $dec =~ /(\d{1,3})/g;
176 elsif ($version =~ tr/_//) {
177 die "don't know how to handle underscores in dotted-decimal versions!\n";
184 my ($version, $new) = @_;
186 my %bump_part = (major => 0, minor => 1, bugfix => 2, patch => 2, last => -1);
187 my $bump_this = $bump_part{$new||'last'};
192 if (defined $bump_this) {
193 if ($version =~ /^v/ || ($version =~ tr/.//) > 1) {
194 my $v = $version =~ /^(v)/ ? $1 : '';
195 if ($version =~ tr/_//d && !$stable || $alpha) {
196 die "can't bump dotted decimal versions with alpha components!\n";
198 my @parts = version_parts($version);
201 $parts[$_] = 0 for $bump_this+1 .. $#parts;
202 $parts[$_] = 0 for $#parts+1 .. $bump_this;
203 $parts[$bump_this]++;
206 if (grep $_ > 999, @parts[1 .. $#parts]) {
207 warn "$new_decimal has a version component greater than 999. It will be incompatible with some uses in perl.\n";
209 $new_decimal = $new_vstring = $v . join '.', @parts;
214 $alpha_pos = index($version, '_');
215 if ($alpha_pos == -1) {
219 my $dot_pos = index($version, '.');
220 $alpha_pos = $dot_pos == -1 ? -$alpha_pos : $alpha_pos - $dot_pos;
223 $new_decimal = $version;
224 $new_decimal =~ tr/_//d;
225 my $dec_len = $new_decimal =~ /(\.\d+)/ ? length($1) - 1 : 0;
226 if ($bump_this != -1) {
227 my $cut_len = $bump_this * 3;
229 if $dec_len < $cut_len;
231 $new_decimal =~ s/(\..{1,$cut_len}).*/$1/;
234 $new_decimal =~ s/\..*//;
237 $new_decimal += 10 ** -($bump_this == -1 ? $dec_len : ($bump_this * 3));
238 $new_decimal = sprintf "%.${dec_len}f", $new_decimal;
240 $alpha_pos ||= $dec_len >= 2 ? int($dec_len / 2) + 1 :
241 die "don't know how to make $new_decimal into an alpha version";
243 if (defined $alpha_pos) {
244 my $dot_pos = index($new_decimal, '.');
245 $dot_pos = length $new_decimal
247 substr $new_decimal, $dot_pos + $alpha_pos, 0, '_';
249 $new_vstring = 'v' . join '.', version_parts($new_decimal);
252 elsif ($new =~ /^v?[0-9]+(?:[._][0-9]+)*$/) {
254 $new_vstring = join('.', version_parts($new_decimal));
257 die "no idea which part to bump - $new means nothing to me"
259 return ($new_decimal, $new_vstring);