File Coverage

blib/lib/Plack/Middleware/GitStatus.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::GitStatus;
2 3     3   118186 use strict;
  3         7  
  3         91  
3 3     3   15 use warnings;
  3         5  
  3         102  
4             our $VERSION = '0.02';
5              
6 3     3   776 use parent 'Plack::Middleware';
  3         307  
  3         15  
7 3     3   15949 use Plack::Util::Accessor qw(path git_dir);
  3         5  
  3         15  
8 3     3   119 use Plack::Util;
  3         3  
  3         61  
9              
10 3     3   2213 use Cache::FileCache;
  3         123595  
  3         159  
11 3     3   1231 use Cwd;
  3         1419  
  3         236  
12 3     3   3692 use Git::Repository 'Log';
  3         70975  
  3         25  
13             use Time::Piece;
14             use Try::Tiny;
15              
16             our $CACHE = Cache::FileCache->new({
17             namespace => 'Plack::Middleware::GitStatus',
18             default_expires_in => 24 * 60 * 60, # 1 day
19             });
20             our $CACHE_KEY = 'git-status';
21              
22             our $WORKTREE;
23              
24             sub prepare_app {
25             my $self = shift;
26              
27             try {
28             $WORKTREE = Git::Repository->new(work_tree => $self->{git_dir} || getcwd);
29             } catch {
30             $self->{error} ||= $_;
31             };
32              
33             # reset git message cache when restating app
34             $CACHE->clear;
35             $CACHE->set($CACHE_KEY => $self->_git_message || '');
36             }
37              
38             sub call {
39             my ($self, $env) = @_;
40              
41             if ($self->path && $env->{PATH_INFO} eq $self->path) {
42             my $body = $CACHE->get($CACHE_KEY) || do {
43             $CACHE->set($CACHE_KEY => $self->_git_message || '');
44             };
45              
46             if ($self->{error}) {
47             return [500, ['Content-Type' => 'text/plain'], [ $self->{error} ]];
48             }
49              
50             return [200, ['Content-Type' => 'text/plain'], [ $body ]];
51             }
52              
53             return $self->app->($env);
54             }
55              
56             sub _git_message {
57             my $self = shift;
58              
59             return undef unless defined $WORKTREE;
60              
61             my ($brach_name, $last_commit);
62             try {
63             $brach_name = $self->_current_branch;
64             $last_commit = $self->_last_commit;
65             } catch {
66             $self->{error} ||= $_;
67             return undef;
68             };
69              
70             my $msg = "CurrentBranch: $brach_name\n";
71             $msg .= sprintf "Commit: %s\n", $last_commit->{commit};
72             $msg .= sprintf "Author: %s\n", $last_commit->{author};
73             $msg .= sprintf "Date: %s\n", $last_commit->{date};
74             $msg .= sprintf "Message: %s", $last_commit->{message};
75              
76             return $msg;
77             }
78              
79             sub _current_branch {
80             my $self = shift;
81             my (@lines) = $WORKTREE->run('status');
82             $lines[0] =~ /branch (.+)$/;
83             return $1;
84             }
85              
86             sub _last_commit {
87             my $self = shift;
88              
89             my ($log) = Git::Repository->log('-1');
90             return +{
91             commit => $log->commit,
92             author => $log->author,
93             message => $log->message,
94             date => _unixtime_to_date($log->author_localtime),
95             };
96             }
97              
98             sub _unixtime_to_date {
99             my $lt = localtime($_[0]);
100             my $t = gmtime($lt->epoch);
101             return $t;
102             }
103              
104             1;
105             __END__