add aliases for bump-version alpha option
[p5sagit/Distar.git] / lib / Distar / helpers / preflight
1 #!/usr/bin/env perl
2 use strict;
3 use warnings FATAL => 'all';
4 use Config;
5 use File::Spec;
6 use File::Find;
7 use Getopt::Long qw(:config gnu_getopt);
8
9 GetOptions(
10   "branch=s"     => \(my $branch = 'master'),
11   "changelog=s"  => \(my $changelog = 'Changes'),
12 ) or die("Error in command line arguments\n");
13
14 my $version = shift or die "version required!";
15
16 my $make = $Config{make};
17 my $null = File::Spec->devnull;
18
19 system("git fetch");
20 if (system("git rev-parse --quiet --verify v$version >$null") == 0) {
21   die "Tag v$version already exists!";
22 }
23
24 chomp(my $head = `git symbolic-ref -q HEAD`);
25 (my $current_branch = $head) =~ s{^refs/heads/}{};
26 $head eq "refs/heads/$branch"
27   or die "Current branch is $current_branch, not $branch. EEEK!\n";
28 chomp(my $upstream = `git for-each-ref --format="%(upstream)" $head`);
29 length $upstream
30   or die "No upstream branch configured for $branch!\n";
31 my $base_rev = `git merge-base $upstream $head`;
32 my $upstream_rev = `git rev-parse --verify $upstream`;
33 $upstream_rev eq $base_rev
34   or die "Not synced with upstream!\n";
35
36 for (scalar `git diff`) {
37   length && die "Outstanding changes!\n";
38 }
39 my $ymd = sprintf(
40   "%i-%02i-%02i", (gmtime)[5]+1900, (gmtime)[4]+1, (gmtime)[3]
41 );
42 my $changes_line = "$version - $ymd\n";
43 my @cached = grep /^\+/, `git diff --cached -U0 --no-prefix`;
44 @cached > 0 or die "Please add:\n\n$changes_line\nto $changelog and stage $changelog (git add $changelog)\n";
45 @cached == 2 or die "All '$changelog' changes must be committed aside from version heading\n";
46 $cached[0] =~ /^\+\+\+ \Q$changelog\E\n/ or die "$changelog not changed\n";
47 $cached[1] eq "+$changes_line" or die "$changelog new line should be: \n\n$changes_line\n";