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, $bump) = @ARGV;
15 my ($new_decimal, $new_vstring) = bump_version($old_version, $bump);
17 warn "Bumping $old_version -> $new_decimal\n";
19 my $file_match = qr{^(?:
21 |lib[/\\].*\.(?:pod|pm)
28 if (system "git diff --quiet --cached HEAD") {
29 die "Staged changes!\n";
31 for (`git ls-files`) {
35 $files{$_} = `git show HEAD:"$_"`;
45 unless /(?:\.[\/\\])?$file_match/;
47 or die "can't open $_: $!";
48 $files{$_} = do { local $/; <$fh> };
55 (^.* \$VERSION \s* = \s* )
56 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
65 (^.* ['"]?version['"] \s* => \s* )
66 (['"]?) v?([0-9]+(?:[._][0-9]+)*) \2
76 for my $file (sort keys %files) {
77 my $content = $files{$file};
79 my $re = $file eq 'Makefile.PL' ? $MAKE_RE : $FILE_RE;
80 my @lines = split /\r?\n/, $content;
81 for my $ln (0 .. $#lines) {
82 my $line = $lines[$ln];
83 if ($lines[$ln] =~ $re) {
84 die "unable to bump version number in $file from $old_version, found $3\n"
85 if !$force && $3 ne $old_version;
86 my $comment = ($5 ? $5 . $new_vstring : '');
87 my $new_line = "$1'$new_decimal'$4$comment$6";
88 $file_diff .= <<"END_DIFF";
89 @@ -@{[ $ln ]},3 +@{[ $ln ]},3 @@
98 $patch .= <<"END_HEADER" . $file_diff;
105 my ($fh, $file) = File::Temp::tempfile( "bump-version-XXXXXX", TMPDIR => 1 );
106 print { $fh } $patch;
108 system qw(git --no-pager apply --apply --stat), $file
112 system qw(git apply --cached), $file
115 my $message = "Bumping version to $new_decimal";
116 system qw(git commit -m), $message
122 my $dotted = $version =~ s/^v//;
123 my @parts = split /\./, $version;
124 if (!$dotted && @parts == 2) {
125 my $dec = pop @parts;
127 $dec .= "0" x ((- length $dec) % 3);
128 push @parts, $dec =~ /(\d{1,3})/g;
135 my ($version, $new) = @_;
137 my %bump_part = (major => 0, minor => 1, bugfix => 2, last => -1);
138 my $bump_this = $bump_part{$new||'last'};
143 if (defined $bump_this) {
144 if ($version =~ /^v/ || ($version =~ tr/.//) > 1) {
145 my @parts = version_parts($version);
146 $parts[$bump_this]++;
147 $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts);
150 $new_vstring = join '.', @parts;
151 my $format = '%i.'. join '', map { '%03i' } @parts[1 .. $#parts];
152 $new_decimal = sprintf $format, @parts;
155 my $alpha_pos = index($version, '_');
157 $version =~ s/^(\d+)\.//;
159 push @parts, $version =~ /(\d{1,3})/g;
160 my $format = '%i.'.join '', map { '%0'.length($_).'i' } @parts[1 .. $#parts];
161 $parts[$bump_this]++;
162 $parts[$_] = 0 for (($bump_this < 0 ? @parts : 0)+$bump_this+1 .. $#parts);
163 $new_decimal = sprintf $format, @parts;
164 substr $new_decimal, $alpha_pos, 0, '_'
166 $new_vstring = join '.', version_parts($new_decimal);
169 elsif ($new =~ /^v?[0-9]+(?:[._][0-9]+)*$/) {
171 $new_vstring = join('.', version_parts($new_decimal));
174 die "no idea which part to bump - $new means nothing to me"
176 return ($new_decimal, $new_vstring);