#!/usr/bin/env perl
use 5.12.0;
use Getopt::Long;
use Git::Release;

my $opt_testing;
GetOptions(
    't|testing=s' => \$opt_testing,
);

my $re = Git::Release->new;
$|++;

my @ready = $re->branch->ready_branches;

my $rc = $re->branch->find_branches('rc');
unless( $rc ) {
    say "rc branch is not found, creating rc branch from master.";
    $rc = $re->branch->new_branch('rc')->create(from => 'master');
}
$rc->checkout;

say "Found ready branches:";
for my $b ( @ready ) {
    say "\t", $b->name;
}

for my $b ( @ready ) {
    say "===> Merging " , $b->ref;
    my @lines = $rc->merge( $b );
    say @lines;
    die 'Merge error' unless @lines;
    system( $opt_testing ) == 0
        or die("test command $opt_testing failed.");
}


