App-Changelord/lib/App/Changelord/Role/GitRepo.pm

50 lines
1020 B
Perl

package App::Changelord::Role::GitRepo;
use v5.36.0;
use Git::Repository;
use Moo::Role;
use feature 'try'; no warnings qw/ experimental /;
requires 'changelog';
# stolen from Dist::Zilla::Plugin::ChangeStats::Git
has repo => (
is => 'lazy',
default => sub {
Git::Repository->new( work_tree => '.' )
},
);
has stats => ( is => 'lazy' );
sub _build_stats ($self) {
my $comparison_data = $self->_get_comparison_data or return;
my $stats = 'code churn: ' . $comparison_data;
return $stats =~ s/\s+/ /gr;
}
sub _get_comparison_data($self) {
# HEAD versus previous release
# What are we diffing against? :)
my $previous = $self->changelog->{releases}->@* > 1
? $self->changelog->{releases}[1]{version}
: '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; # empty tree
my $output = eval {
$self->repo->run( 'diff', '--shortstat', $previous, 'HEAD')
};
warn "could not gather stats: $@\n" if $@;
return $output;
}
1;