File Coverage

blib/lib/Plack/Middleware/Debug/GitStatus.pm
Criterion Covered Total %
statement 32 56 57.1
branch 4 14 28.5
condition n/a
subroutine 8 10 80.0
pod 1 4 25.0
total 45 84 53.5


line stmt bran cond sub pod time code
1             package Plack::Middleware::Debug::GitStatus;
2 1     1   117839 use strict;
  1         3  
  1         30  
3 1     1   6 use warnings;
  1         3  
  1         56  
4              
5             our $VERSION = '0.04';
6             $VERSION = eval $VERSION;
7              
8 1     1   1283 use Plack::Util::Accessor qw( git_dir gitweb_url );
  1         431  
  1         10  
9 1     1   20824 use Encode qw/ decode_utf8 encode_utf8 /;
  1         15258  
  1         86  
10              
11 1     1   740 use parent 'Plack::Middleware::Debug::Base';
  1         283  
  1         5  
12              
13             sub run {
14 1     1 1 1306 my ( $self, undef, $panel ) = @_;
15              
16             return sub {
17 1     1   490 my ( $branch, $info ) = $self->get_git_info;
18 1         78 $panel->title( 'GitStatus' );
19 1         73 $panel->nav_title( 'GitStatus' );
20 1         69 $panel->nav_subtitle( 'On branch ' . $branch );
21 1         52 $panel->content( $self->render_info( $info ) );
22             }
23 1         6 }
24              
25             sub render_info {
26 3     3 0 5951 my $self = shift;
27 3         5 my $info = shift;
28              
29 3         6 my $html = qq|\n|, $info->{ sha_1 };
|;
30              
31 3 100       9 if ( $info->{ error } ) {
32 1         4 $html .= qq|Error
33            
Git reported an error$info->{ error }
34             |;
35             }
36             else {
37 2         18 $html .= qq|Git status information
38            
Current Branch
$info->{ current_branch }
39            
Status
$info->{ status }
40            
Last commit
41            
Date$info->{ date }
42            
Author$info->{ author }
43            
SHA-1$info->{ sha_1 }
44            
Message$info->{ message }
45             |;
46              
47 2 100       13 if ( my $url = $self->gitweb_url ) {
48 1         112 $html .= sprintf qq|
View
49             }
50             }
51              
52 3         91 $html .= '
';
53              
54 3         13 return encode_utf8 $html;
55             }
56              
57             sub get_git_info {
58 0     0 0   my $self = shift;
59              
60 0           my ( $branch, $info ) = eval {
61 0 0         if ( my $dir = $self->git_dir ) {
62 0 0         if ( ! chdir $dir ) {
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 0         if ( $? ) {
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 0         if ( my $err = $@ ) {
87 0           return 'unknown', { error => $err };
88             }
89             else {
90 0           return $branch, $info;
91             }
92             }
93              
94             sub get_git_status {
95 0     0 0   my $self = shift;
96              
97 0           my $status = `git status -s`;
98 0 0         return 'clean' unless $status;
99              
100 0           return $status;
101             }
102              
103             1;
104              
105             __END__