Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
add /info/refs request
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-kimoto committed Oct 1, 2013
1 parent 7d1fdc3 commit a300cc8
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 2 deletions.
81 changes: 79 additions & 2 deletions lib/Gitprep.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Encode qw/encode decode/;
use Gitprep::API;
use Gitprep::Git;
use Gitprep::Manager;
use Gitprep::SmartHTTP;
use Scalar::Util 'weaken';
use Validator::Custom;

Expand All @@ -24,6 +25,8 @@ has 'git';
has 'manager';
has 'validator';

use constant BUFFER_SIZE => 8192;

sub startup {
my $self = shift;

Expand Down Expand Up @@ -70,7 +73,7 @@ sub startup {
$self->git($git);

# Added public path
push @{$self->static->paths}, $rep_home;
# push @{$self->static->paths}, $rep_home;

# DBI
my $db_file = $ENV{GITPREP_DB_FILE}
Expand Down Expand Up @@ -177,7 +180,81 @@ sub startup {
# Settings
$r->get('/_settings' => template '/user-settings');
}


# Smart HTTP
{
my $r = $r->route('/(:project).git', project => $id_re);

my $sh = Gitprep::SmartHTTP->new;

# Fetch
$r->get('/info/refs')->to(cb => sub {
my $self = shift;

my $service = $self->param('service') || '';

my $user = $self->param('user');
my $project = $self->param('project');

if ($service eq 'git-upload-pack') {

my $rep = $git->rep($user, $project);
my @cmd = $git->cmd($user, $project, 'upload-pack', '--stateless-rpc', '--advertise-refs', $rep);

warn "@cmd";

use IPC::Open3 'open3';
use Symbol 'gensym';
my ($cout, $cerr) = (gensym, gensym );
my $pid = open3(my $cin, $cout, $cerr, @cmd );
close $cin;
my ( $refs, $err, $buf ) = ( '', '', '' );
my $s = IO::Select->new( $cout, $cerr );
while (my @ready = $s->can_read) {
for my $handle (@ready) {
while ( sysread( $handle, $buf, BUFFER_SIZE ) ) {
if ( $handle == $cerr ) {
$err .= $buf;
}
else {
$refs .= $buf;
}
}
$s->remove($handle) if eof($handle);
}
}
close $cout;
close $cerr;
waitpid($pid, 0);

if ($err) {
app->log->error($err);
$self->render_exception($err);
}

$self->res->headers->content_type('application/x-git-upload-pack-advertisement');

my $data =
$sh->pkt_write("# service=git-upload-pack\n") . $sh->pkt_flush() . $refs;

$self->render(data => $data);
}
else {
$sh->dumb_info_refs;
}
});

# $r->post('/git-upload-pack');
# $r->post('/git-receive-pack');
# $r->get('/HEAD');
# $r->get('/objects/info/alternates');
# $r->get('/objects/info/http-alternates');
# $r->get('/objects/info/packs');
# $r->get('/objects/[0-9a-f]{2}/[0-9a-f]{38}');
# $r->get('/objects/pack/pack-[0-9a-f]{40}\.pack');
# $r->get('/objects/pack/pack-[0-9a-f]{40}\.idx');
}

# Project
{
my $r = $r->route('/:project', project => $id_re);
Expand Down
Loading

0 comments on commit a300cc8

Please sign in to comment.