Skip to content

Instantly share code, notes, and snippets.

@shiba-yu36
Forked from motemen/app.psgi
Created June 28, 2011 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiba-yu36/1050904 to your computer and use it in GitHub Desktop.
Save shiba-yu36/1050904 to your computer and use it in GitHub Desktop.
Git log chat
use strict;
use warnings;
use opts;
use autodie;
use Plack::Request;
use File::Path qw(mkpath);
use File::chdir;
use Sys::Hostname;
use Text::MicroTemplate qw(render_mt);
sub git (@);
sub whole (&);
my $hostname;
my $git_root;
my $pull_remote;
my $push_remote;
# for plackup
BEGIN {
opts $hostname => { isa => 'Str', default => hostname() },
$git_root => { isa => 'Str', default => 'db' },
$pull_remote => { isa => 'Str', default => 'origin' },
$push_remote => { isa => 'Str', default => 'origin' };
}
mkpath $git_root;
git 'init';
my $Template = whole { <DATA> };
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
if ($req->path_info eq '/pull') {
git pull => $pull_remote, 'master';
return [ 302, [ Location => '/' ], [] ];
}
if ($req->path_info eq '/push') {
git push => $push_remote, 'master';
return [ 302, [ Location => '/' ], [] ];
}
if ($req->method eq 'POST') {
my $message = $req->param('message');
my $name = $req->param('name');
if (length $message) {
git commit => '--allow-empty', '--message' => $message, length $name ? ( '--author' => "$name <$name\@$hostname>" ) : ();
}
return [ 302, [ Location => '/' ], [] ];
} else {
my $html = render_mt($Template, log => whole { git log => '--pretty=format:%ad %an %s' });
return [ 200, [ 'Content-Type' => 'text/html' ], [ $html ] ];
}
};
sub git (@) {
local $CWD = $git_root;
open my $pipe, '-|', (git => @_);
return <$pipe>;
}
sub whole (&) {
my $block = shift;
local $/;
return scalar $block->();
}
__DATA__
? local %_ = @_;
<!DOCTYPE html>
<html>
<head>
<title>GLC</title>
</head>
<body>
<pre><?= $_{log} ?></pre>
<form action="/" method="post">
<input type="text" name="name" placeholder="name">
<input type="text" name="message" placeholder="message">
<input type="submit" value="enter">
</form>
</body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment