| 1 | #!/usr/bin/env perl | ||||||||||||||||||||
| 2 | package Git::ReleaseDiffs; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | ▶ | use Archive::Tar; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Cwd qw(getcwd); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use CLI::Simple::Utils qw(slurp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use version; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | use parent qw(Class::Accessor::Fast); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | __PACKAGE__->follow_best_practice; | |||||||||||||||||||
| 18 | 2 | __PACKAGE__->mk_accessors(qw(repo last_tag diffs diff_list repo_path release_version)); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | ▶ | caller or exit __PACKAGE__->main; | ||||||||||||||||||
|
Condition50%1/2
caller || exit __PACKAGE__->main
MC/DC0%0/1
caller || exit __PACKAGE__->main
caller
Truth table50%1/2
A
A= caller
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | ######################################################################## | ||||||||||||||||||||
| 23 | 0 | ▶ | sub _last_tag { | ||||||||||||||||||
|
Subroutine
_last_tag: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 24 | ######################################################################## | ||||||||||||||||||||
| 25 | 0 | my ( $self, $repo ) = @_; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | my @tags = map { basename( $_->name ) } grep { $_->name =~ /\d+[.]\d+[.]\d+$/xsm } $repo->tags; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | return | ||||||||||||||||||||
| 30 | 0 | ▶ | if !@tags; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | @tags = reverse sort { version->parse($a) <=> version->parse($b) } @tags; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | return $tags[0]; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | ######################################################################## | ||||||||||||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 39 | ######################################################################## | ||||||||||||||||||||
| 40 | 0 | my ( $class, @args ) = @_; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | eval { require Git::Raw; 1; } or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | die "ERROR: Git::Raw is not installed.\n"; | |||||||||||||||||||
| 44 | }; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | my $options = ref $args[0] ? $args[0] : {@args}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | $options->{repo_path} //= getcwd; | ||||||||||||||||||
|
Condition0%0/3
$options->{"repo_path"} //= getcwd()
MC/DC0%0/2
$options->{"repo_path"} //= getcwd()
$options->{"repo_path"}
getcwd()
Truth table0%0/3
A //= B
A= $options->{"repo_path"}B= getcwd()
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | my $self = $class->SUPER::new($options); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | die "ERROR: directory not found\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | if !-d $self->get_repo_path; | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | die sprintf "ERROR: not a git repository (%s)\n", $self->get_repo_path | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | if !-d sprintf '%s/.git', $self->get_repo_path; | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | if ( !$self->get_release_version ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | my $release_version = eval { slurp('VERSION') }; | |||||||||||||||||||
| 60 | 0 | ▶ | die "ERROR: could not determine release version\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | if !$release_version; | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | chomp $release_version; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | $self->set_release_version($release_version); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | my $repo = Git::Raw::Repository->open( $self->get_repo_path ); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | $self->set_repo($repo); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | $self->set_last_tag( $ENV{LAST_TAG} // $self->_last_tag($repo) ); | ||||||||||||||||||
|
Condition0%0/3
$ENV{"LAST_TAG"} // $self->_last_tag($repo)
MC/DC0%0/2
$ENV{"LAST_TAG"} // $self->_last_tag($repo)
$ENV{"LAST_TAG"}
$self->_last_tag($repo)
Truth table0%0/3
A // B
A= $ENV{"LAST_TAG"}B= $self->_last_tag($repo)
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | $self->_diffs; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | return $self; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | ######################################################################## | ||||||||||||||||||||
| 80 | 0 | ▶ | sub _diffs { | ||||||||||||||||||
|
Subroutine
_diffs: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 81 | ######################################################################## | ||||||||||||||||||||
| 82 | 0 | my ($self) = @_; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my $repo = $self->get_repo; | |||||||||||||||||||
| 85 | 0 | my $tag = sprintf 'refs/tags/%s', $self->get_last_tag; | |||||||||||||||||||
| 86 | 0 | my $ref = Git::Raw::Reference->lookup( $tag, $repo ); | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | my $commit = $ref->peel('commit'); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | my $index = $repo->index; | |||||||||||||||||||
| 91 | 0 | $index->read; | |||||||||||||||||||
| 92 | 0 | my $index_tree = $index->write_tree; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my $diff = $commit->tree->diff( $index_tree, {} ); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | $self->set_diffs( $diff->buffer('patch') ); | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | my @files = map { $_->new_file->path } | |||||||||||||||||||
| 99 | 0 | grep { $_->status =~ /^(?:added|modified|renamed)$/xsm } $diff->deltas; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | $self->set_diff_list( \@files ); | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | return; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | ######################################################################## | ||||||||||||||||||||
| 107 | 0 | ▶ | sub write_diffs { | ||||||||||||||||||
|
Subroutine
write_diffs: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 108 | ######################################################################## | ||||||||||||||||||||
| 109 | 0 | my ($self) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | my $file = sprintf 'release-%s.diffs', $self->get_release_version; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | open my $fh, '>', $file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | or die "ERROR: could not open $file for writing: $!\n"; | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | print {$fh} $self->get_diffs; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | close $fh; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | return $file; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | ######################################################################## | ||||||||||||||||||||
| 124 | 0 | ▶ | sub write_list { | ||||||||||||||||||
|
Subroutine
write_list: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 125 | ######################################################################## | ||||||||||||||||||||
| 126 | 0 | my ($self) = @_; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | my $file = sprintf 'release-%s.lst', $self->get_release_version; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | open my $fh, '>', $file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | or die "ERROR: could not open $file for writing: $!\n"; | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | print {$fh} "$_\n" for @{ $self->get_diff_list }; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | close $fh; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | return $file; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | ######################################################################## | ||||||||||||||||||||
| 141 | 0 | ▶ | sub write_tarball { | ||||||||||||||||||
|
Subroutine
write_tarball: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 142 | ######################################################################## | ||||||||||||||||||||
| 143 | 0 | my ($self) = @_; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | my $tag = $self->get_release_version; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | my $tarball = sprintf 'release-%s.tar.gz', $tag; | |||||||||||||||||||
| 148 | 0 | my $prefix = sprintf 'release-%s', $tag; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | my $tar = Archive::Tar->new; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | for my $file ( @{ $self->get_diff_list } ) { | |||||||||||||||||||
| 153 | 0 | ▶ | if ( -e $file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | $tar->add_files($file); | |||||||||||||||||||
| 155 | 0 | $tar->rename( $file, "$prefix/$file" ); | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||
| 158 | 0 | warn "WARNING: $file not found, skipping\n"; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | $tar->write( $tarball, COMPRESS_GZIP ); | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | return $tarball; | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | ######################################################################## | ||||||||||||||||||||
| 168 | 0 | ▶ | sub main { | ||||||||||||||||||
|
Subroutine
main: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 169 | ######################################################################## | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | my $repo_path = shift @ARGV; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | my $release = Git::ReleaseDiffs->new( repo_path => $repo_path ); | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 0 | my $diffs = $release->write_diffs; | |||||||||||||||||||
| 176 | 0 | my $list = $release->write_list; | |||||||||||||||||||
| 177 | 0 | my $tarball = $release->write_tarball; | |||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 0 | printf "wrote: %s\n", $_ for ( $diffs, $list, $tarball ); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | return 0; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||