#!/usr/bin/env perl

use 5.10.0;

use strict;
use warnings;

use autodie;

use Git;

use Pod::Usage;
use Getopt::Long;

my %opt;
GetOptions( \%opt, 
    'help' => sub { pod2usage(1) },
    'man'  => sub { pod2usage( verbose => 2 ) },
) or pod2usage( "for a list of all valid options, do 'git cpan-last-version --help'" );

my $repo = Git->repository;

my $last_commit = $repo->command_oneline('rev-parse', '--verify', 'cpan/master');

my $last = join "\n", $repo->command( log => '--pretty=format:%b', '-n', 1, $last_commit );

$last =~ /git-cpan-module:\ (?<module>.*?) \s+ git-cpan-version:\ (?<version>.*?) \s*$/sx
    or die "Couldn't parse message:\n$last\n";

say $+{version};

__END__

=pod

=head1 NAME

git-cpan-last-version - Report the last imported version

=head1 SYNOPSIS

    % git cpan-last-version

=head1 DESCRIPTION

This command searches through the history of the C<cpan/master> reference for
the last imported version.


=head1 VERSION

This document describes git-cpan-last-version version 0.1.0.

=head1 BUGS AND LIMITATIONS

Please report any bugs or feature requests to
C<bug-git-cpan-patch@rt.cpan.org>, or through the web 
interface at L<http://rt.cpan.org>.
  
=head1 AUTHORS

Yuval Kogman C<< <nothingmuch@woobling.org> >>

Yanick Champoux C<< <yanick@cpan.org> >>

=head1 LICENCE

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=head1 SEE ALSO

L<Git::CPAN::Patch>

=cut
