blib/lib/Dist/Zilla/Plugin/Git/CommitBuild.pm

filter: Statementstmt 96.5% 113 / 117 Branchbran 55.5% 10 / 18 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 93.5% 29 / 31 Podpod 0.0% 0 / 2 totaltotal 90.6% 155 / 171 CC 13 SCAR 9.9
CC
13
Cov
91%
_commit_build
7.3
__ANON__
6.0
_create_tree
4.0
CRAP 2.7
« blib/lib/Dist/Zilla/PluginBundle/Git.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Git/Tag.pm »
1 #
2 # This file is part of Dist-Zilla-Plugin-Git
3 #
4 # This software is copyright (c) 2009 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Dist::Zilla::Plugin::Git::CommitBuild;
14 # ABSTRACT: Check in build results on separate branch
15  
16 3 our $VERSION = '2.052';
17  
18 3 ▶use Git::Wrapper 0.021 (); # need -STDIN
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use IPC::System::Simple; # required for Fatalised/autodying system
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use File::Spec::Functions qw/ rel2abs catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Path::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Types::Path::Tiny 'Path';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Types::Standard qw(Str Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶use String::Formatter (
Subroutine BEGIN: called CC=1 SCAR=0.5
33 method_stringf => {
34 -as => '_format_branch',
35 codes => {
36 7 ▶ b => sub { shift->_source_branch },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 },
38 },
39 method_stringf => {
40 -as => '_format_message',
41 codes => {
42 7 ▶ b => sub { shift->_source_branch },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 7 ▶ h => sub { (shift->git->rev_parse( '--short', 'HEAD' ))[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 0 ▶ H => sub { (shift->git->rev_parse('HEAD'))[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 ▶ t => sub { shift->zilla->is_trial ? '-TRIAL' : '' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
(shift())->zilla->is_trial ? : 0 0 0
46 1 ▶ v => sub { shift->zilla->version },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48 }
49 3 );
50  
51 # debugging...
52 #use Smart::Comments '###';
53  
54 3 with 'Dist::Zilla::Role::AfterBuild',
55 'Dist::Zilla::Role::AfterRelease',
56 'Dist::Zilla::Role::Git::Repo';
57  
58 # -- attributes
59  
60 3 has branch => ( is => 'ro', isa => Str, default => 'build/%b', required => 1 );
61 3 has release_branch => ( is => 'ro', isa => Str, required => 0 );
62 3 has message => ( is => 'ro', isa => Str, default => 'Build results of %h (on %b)', required => 1 );
63 3 has release_message => ( is => 'ro', isa => Str, lazy => 1, builder => '_build_release_message' );
64 3 has build_root => ( is => 'rw', coerce => 1, isa => Path );
65  
66 has _source_branch => (
67 is => 'ro',
68 isa => Str,
69 lazy => 1,
70 init_arg=> undef,
71 7 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 7 ($_[0]->git->name_rev( '--name-only', 'HEAD' ))[0];
73 },
74 3 );
75  
76 3 has multiple_inheritance => (
77 is => 'ro',
78 isa => Bool,
79 default => 0,
80 );
81  
82 # -- attribute builders
83  
84 6 ▶sub _build_release_message { return shift->message; }
Subroutine _build_release_message: called CC=1 SCAR=0.0
85  
86 # -- role implementation
87  
88 around dump_config => sub
89 7 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
90 7 my $orig = shift;
91 7 my $self = shift;
92  
93 7 my $config = $self->$orig;
94  
95 7 ▶ $config->{+__PACKAGE__} = {
Branch: 2 branches
BranchTrueFalseTotal
$self->multiple_inheritance ? : 1 6 7
builtin::blessed($self) ne __PACKAGE__ ? : 0 7 7
96 (map +($_ => $self->$_),
97 qw(branch release_branch message release_message build_root)),
98 multiple_inheritance => $self->multiple_inheritance ? 1 : 0,
99 blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
100 };
101  
102 7 return $config;
103 3 };
104  
105 7 ▶sub after_build {
Subroutine after_build: called CC=1 SCAR=0.0
Pod undocumented
106 7 my ( $self, $args) = @_;
107  
108 # because the build_root mysteriously change at
109 # the 'after_release' stage
110 7 $self->build_root( $args->{build_root} );
111  
112 7 $self->_commit_build( $args, $self->branch, $self->message );
113 }
114  
115 1 ▶sub after_release {
Subroutine after_release: called CC=1 SCAR=0.0
Pod undocumented
116 1 my ( $self, $args) = @_;
117  
118 1 $self->_commit_build( $args, $self->release_branch, $self->release_message );
119 }
120  
121 8 ▶sub _commit_build {
Subroutine _commit_build: called CC=7 SCAR=19.8
122 8 my ( $self, undef, $branch, $message ) = @_;
123  
124 8 ▶ return unless $branch;
Branch: 1 branch
BranchTrueFalseTotal
unless $branch 0 8 8
125  
126 8 my $dir = Path::Tiny->tempdir( CLEANUP => 1) ;
127 8 my $src = $self->git;
128  
129 8 my $target_branch = _format_branch( $branch, $self );
130  
131 8 for my $file ( @{ $self->zilla->files } ) {
132 16 ▶ my ( $name, $content ) = ( $file->name, (Dist::Zilla->VERSION < 5
Branch: 1 branch
BranchTrueFalseTotal
'Dist::Zilla'->VERSION < 5 ? : 0 16 16
133 ? $file->content
134 : $file->encoded_content) );
135 16 my ( $outfile ) = $dir->child( $name );
136 16 $outfile->parent->mkpath();
137 16 $outfile->spew_raw( $content );
138 16 ▶ chmod $file->mode, "$outfile" or die "couldn't chmod $outfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod $file->mode, "$outfile" or die "couldn't chmod ${outfile}: $!" 16 0 16
139 }
140  
141 # returns the sha1 of the created tree object
142 8 my $tree = $self->_create_tree($src, $dir);
143  
144 8 ▶ my ($last_build_tree) = try { $src->rev_parse("$target_branch^{tree}") };
Subroutine __ANON__: called CC=1 SCAR=0.0
145 8 ▶ $last_build_tree ||= 'none';
Condition100%2/2
$last_build_tree ||= "none"
l!l
26
MC/DC100%1/1
$last_build_tree ||= "none"
$last_build_tree
Truth table100%2/2
A
A= $last_build_tree
Aresult
11
00
146  
147 ### $last_build_tree
148 8 ▶ if ($tree eq $last_build_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree eq $last_build_tree) 0 8 8
149  
150 0 $self->log("No changes since the last build; not committing");
151 0 return;
152 }
153  
154 my @parents = (
155 ( $self->_source_branch ) x $self->multiple_inheritance,
156 grep
157 8 eval { $src->rev_parse({ 'q' => 1, 'verify'=>1}, $_ ) },
158 $target_branch
159 );
160  
161 ### @parents
162  
163 8 my $this_message = _format_message( $message, $self );
164 8 my @commit = $src->commit_tree( { -STDIN => $this_message }, $tree, map +( '-p' => $_), @parents );
165  
166 ### @commit
167 8 $src->update_ref( 'refs/heads/' . $target_branch, $commit[0] );
168 }
169  
170 24 ▶sub _create_tree {
Subroutine _create_tree: called CC=4 SCAR=13.9
171 24 my ($self, $repo, $fs_obj) = @_;
172  
173 ### called with: "$fs_obj"
174 24 ▶ if (!$fs_obj->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs_obj->is_dir) 16 8 24
175  
176 16 my ($sha) = $repo->hash_object({ w => 1 }, "$fs_obj");
177 ### hashed: "$sha $fs_obj"
178 16 return $sha;
179 }
180  
181 8 my @entries;
182 8 for my $obj ($fs_obj->children) {
183  
184 ### working on: "$obj"
185 16 my $sha = $self->_create_tree($repo, $obj);
186 16 my $mode = sprintf('%o', $obj->stat->mode); # $obj->is_dir ? '040000' : '
187 16 ▶ my $type = $obj->is_dir ? 'tree' : 'blob';
Branch: 1 branch
BranchTrueFalseTotal
$obj->is_dir ? : 0 16 16
188 16 my $name = $obj->basename;
189  
190 16 push @entries, "$mode $type $sha\t$name";
191 }
192  
193 ### @entries
194  
195 8 my ($sha) = $repo->mktree({ -STDIN => join("\n", @entries, q{}) });
196  
197 8 return $sha;
198 }
199  
200 3 __PACKAGE__->meta->make_immutable;
201 3 1;
202  
203 __END__
« blib/lib/Dist/Zilla/PluginBundle/Git.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Git/Tag.pm »