#!/usr/bin/env perl
# ABSTRACT: post-commit hook for managing git issues
# PODNAME: git-issue-commit-hook
use strict;
use warnings;
use Try::Tiny;
use Git::IssueManager;
use Git::LowLevel;
use utf8;

#
# main program
#

my $manager     = Git::IssueManager->new(repository=>Git::LowLevel->new(git_dir=> "."));

warn("IssueManager not yet initialize ignoring commit\n") unless $manager->ready();
exit(0) unless $manager->ready();

my $tag = $manager->tag();

my $help=readpipe("git log -1 HEAD");
my @lines=split /\n/, $help;

my $commit="";
for my $line (@lines)
{
  #check for commit
  if ($line =~ /^commit\s([0-9abcdef]+)$/)
  {
    $commit=$1;
  }

  #search for an issue tag within the commit message
  if ($line =~ /($tag-[0-9abcdef]{8})/)
  {
    my $id=$1;
    # check for all possible commit issue commands
    if ($line =~ /#close/)
    {
      $manager->close($id, $commit);
    }
  }
}

__END__

=pod

=encoding UTF-8

=head1 NAME

git-issue-commit-hook - post-commit hook for managing git issues

=head1 VERSION

version 0.2

=head1 AUTHOR

Dominik Meyer <dmeyer@federationhq.de>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by Dominik Meyer.

This is free software, licensed under:

  The GNU General Public License, Version 2, June 1991

=head1 AVAILABILITY

The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
site near you, or see L<https://metacpan.org/module/App::Git::IssueManager/>.

=head1 BUGS

Please report any bugs or feature requests by email to
L<byterazor@federationhq.de|mailto:byterazor@federationhq.de>.

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by Dominik Meyer.

This is free software, licensed under:

  The GNU General Public License, Version 2, June 1991

=cut
