4 use warnings FATAL => 'all';
6 use Getopt::Long qw(:config gnu_getopt);
11 "force" => \my $force,
12 ) or die("Error in command line arguments\n");
14 my $old_version = shift
15 or die "no old version provided!\n";
17 my ($new_decimal, $new_vstring) = bump_version($old_version, $bump);
19 warn "Bumping $old_version -> $new_decimal\n";
21 my $file_match = qr{^(?:
23 |lib[/\\].*\.(?:pod|pm)
30 if (system "git diff --quiet --cached HEAD") {
31 die "Staged changes!\n";
33 for (`git ls-files`) {
37 $files{$_} = `git show HEAD:"$_"`;
47 unless /(?:\.[\/\\])?$file_match/;
49 or die "can't open $_: $!";
50 $files{$_} = do { local $/; <$fh> };
57 (^.* \$VERSION \s* = \s* )
58 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
67 (^.* ['"]?version['"] \s* => \s* )
68 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
78 for my $file (sort keys %files) {
79 my $content = $files{$file};
81 my $re = $file eq 'Makefile.PL' ? $MAKE_RE : $FILE_RE;
82 my @lines = split /\r?\n/, $content;
83 for my $ln (0 .. $#lines) {
84 my $line = $lines[$ln];
85 if ($lines[$ln] =~ $re) {
86 die "unable to bump version number in $file from $old_version, found $3\n"
87 if !$force && $3 ne $old_version;
88 my $comment = ($5 ? $5 . $new_vstring : '');
89 my $new_line = "$1'$new_decimal'$4$comment$6";
90 $file_diff .= <<"END_DIFF";
91 @@ -@{[ $ln ]},3 +@{[ $ln ]},3 @@
100 $patch .= <<"END_HEADER" . $file_diff;
107 my ($fh, $file) = File::Temp::tempfile( "bump-version-XXXXXX", TMPDIR => 1 );
108 print { $fh } $patch;
110 system qw(git --no-pager apply --apply --stat), $file
114 system qw(git apply --cached), $file
117 my $message = "Bumping version to $new_decimal";
118 system qw(git commit -m), $message
124 my $dotted = $version =~ s/^v//;
125 my @parts = split /\./, $version;
126 if (!$dotted && @parts == 2) {
127 my $dec = pop @parts;
129 $dec .= "0" x ((- length $dec) % 3);
130 push @parts, $dec =~ /(\d{1,3})/g;
137 my ($version, $new) = @_;
139 my %bump_part = (major => 0, minor => 1, bugfix => 2, last => -1);
140 my $bump_this = $bump_part{$new||'last'};
145 if (defined $bump_this) {
146 if ($version =~ /^v/ || ($version =~ tr/.//) > 1) {
147 my @parts = version_parts($version);
148 $parts[$bump_this]++;
149 $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts);
152 $new_vstring = join '.', @parts;
153 my $format = '%i.'. join '', map { '%03i' } @parts[1 .. $#parts];
154 $new_decimal = sprintf $format, @parts;
157 my $alpha_pos = index($version, '_');
159 $version =~ s/^(\d+)\.//;
161 push @parts, $version =~ /(\d{1,3})/g;
162 my $format = '%i.'.join '', map { '%0'.length($_).'i' } @parts[1 .. $#parts];
163 $parts[$bump_this]++;
164 $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts);
165 $new_decimal = sprintf $format, @parts;
166 substr $new_decimal, $alpha_pos, 0, '_'
168 $new_vstring = join '.', version_parts($new_decimal);
171 elsif ($new =~ /^v?[0-9]+(?:[._][0-9]+)*$/) {
173 $new_vstring = join('.', version_parts($new_decimal));
176 die "no idea which part to bump - $new means nothing to me"
178 return ($new_decimal, $new_vstring);