blib/lib/Plack/Middleware/Debug/GitStatus.pm

filter: Statementstmt 59.3% 35 / 59 Branchbran 28.5% 4 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 25.0% 1 / 4 totaltotal 55.1% 48 / 87 CC 8 SCAR 23.5
CC
8
Cov
53%
get_git_info
30.0
get_git_status
6.0
render_info
3.0
CRAP 10.5
↑ Summary
1 package Plack::Middleware::Debug::GitStatus;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.04';
6 1 $VERSION = eval $VERSION;
7  
8 1 ▶use Plack::Util::Accessor qw( git_dir gitweb_url );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode qw/ decode_utf8 encode_utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use parent 'Plack::Middleware::Debug::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
14 1 my ( $self, undef, $panel ) = @_;
15  
16 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 my ( $branch, $info ) = $self->get_git_info;
18 1 $panel->title( 'GitStatus' );
19 1 $panel->nav_title( 'GitStatus' );
20 1 $panel->nav_subtitle( 'On branch ' . $branch );
21 1 $panel->content( $self->render_info( $info ) );
22 }
23 1 }
24  
25 3 ▶sub render_info {
Subroutine render_info: called CC=3 SCAR=11.0
Pod undocumented
26 3 my $self = shift;
27 3 my $info = shift;
28  
29 3 my $html = qq|<table><thead><tr><th colspan="2">|;
30  
31 3 ▶ if ( $info->{ error } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'error'}) { } 1 2 3
32 1 $html .= qq|Error</th></tr></thead><tbody>
33 <tr><td>Git reported an error</td><td>$info->{ error }</td></tr>
34 |;
35 }
36 else {
37 2 $html .= qq|Git status information</th></tr></thead><tbody>
38 <tr><td>Current Branch</td><td><pre>$info->{ current_branch }</pre></td></tr>
39 <tr><td>Status</td><td><pre>$info->{ status }</pre></td></tr>
40 <tr><th>Last commit</th><th></th></tr>
41 <tr><td>Date</td><td>$info->{ date }</td></tr>
42 <tr><td>Author</td><td>$info->{ author }</td></tr>
43 <tr><td>SHA-1</td><td>$info->{ sha_1 }</td></tr>
44 <tr><td>Message</td><td>$info->{ message }</td></tr>
45 |;
46  
47 2 ▶ if ( my $url = $self->gitweb_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $self->gitweb_url) 1 1 2
48 1 $html .= sprintf qq|<tr><td><a style="color: blue" href="$url" target="_blank">View</a></td><td></td></tr>\n|, $info->{ sha_1 };
49 }
50 }
51  
52 3 $html .= '</tbody></table>';
53  
54 3 return encode_utf8 $html;
55 }
56  
57 0 ▶sub get_git_info {
Subroutine get_git_info: not called CC=5 SCAR=34.0
Pod undocumented
58 0 my $self = shift;
59  
60 0 my ( $branch, $info ) = eval {
61 0 ▶ if ( my $dir = $self->git_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $self->git_dir) 0 0 0
62 0 ▶ if ( ! chdir $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $dir) 0 0 0
63 0 die "Could not change to directory '$dir': $!";
64 }
65 }
66  
67 0 my $current_branch = `git symbolic-ref HEAD 2>&1`;
68  
69 0 ▶ if ( $? ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
70 0 die "Git reported an error: $current_branch";
71 }
72  
73 0 $current_branch =~ s|refs/heads/||;
74 0 my %info = ( current_branch => $current_branch );
75 0 $info{ status } = $self->get_git_status;
76  
77 0 my @ci_info = split /\0/, `git log -1 --pretty=format:%H%x00%an%x00%aD%x00%s`;
78 0 $info{ sha_1 } = shift @ci_info;
79 0 $info{ author } = decode_utf8 shift @ci_info;
80 0 $info{ date } = shift @ci_info;
81 0 $info{ message } = decode_utf8 shift @ci_info;
82  
83 0 return $current_branch, \%info;
84 };
85  
86 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 0 0
87 0 return 'unknown', { error => $err };
88 }
89 else {
90 0 return $branch, $info;
91 }
92 }
93  
94 0 ▶sub get_git_status {
Subroutine get_git_status: not called CC=2 SCAR=17.9
Pod undocumented
95 0 my $self = shift;
96  
97 0 my $status = `git status -s`;
98 0 ▶ return 'clean' unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 0 0
99  
100 0 return $status;
101 }
102  
103 1 1;
104  
105 __END__
↑ Summary