| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 62 | 0 | ▶ | if ( ! chdir $dir ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 99 | |||||||||||
| 100 | 0 | return $status; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 1 | 1; | |||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||