about summary refs log tree commit homepage
path: root/lib/PublicInbox/RepobrowseGitDiff.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-03-29 17:30:32 +0000
committerEric Wong <e@80x24.org>2016-04-05 18:58:27 +0000
commit7f72ab0d9ab18642ed9fc0546a56bd6e1e11820b (patch)
tree21265d410d23279f3bd48292320c0af49f962a53 /lib/PublicInbox/RepobrowseGitDiff.pm
parent7d88f5491d5c0d68842fbc4b5e4ca4afc93903ef (diff)
downloadpublic-inbox-7f72ab0d9ab18642ed9fc0546a56bd6e1e11820b.tar.gz
Hopefully this makes it easier to reuse code.
Diffstat (limited to 'lib/PublicInbox/RepobrowseGitDiff.pm')
-rw-r--r--lib/PublicInbox/RepobrowseGitDiff.pm47
1 files changed, 23 insertions, 24 deletions
diff --git a/lib/PublicInbox/RepobrowseGitDiff.pm b/lib/PublicInbox/RepobrowseGitDiff.pm
index 3459ec5f..8e535708 100644
--- a/lib/PublicInbox/RepobrowseGitDiff.pm
+++ b/lib/PublicInbox/RepobrowseGitDiff.pm
@@ -30,16 +30,16 @@ sub call_git_diff {
         if (defined(my $expath = $req->{expath})) {
                 push @cmd, $expath;
         }
-        my $rpipe = $git->popen(\@cmd, undef, { 2 => $git->err_begin });
+        $req->{rpipe} = $git->popen(\@cmd, undef, { 2 => $git->err_begin });
         my $env = $req->{cgi}->env;
         my $err = $env->{'psgi.errors'};
-        my ($res, $vin, $fh);
+        my ($vin);
         $req->{dbuf} = '';
         $req->{p} = [ $id2 ];
         $req->{h} = $id;
-
-        my $end = sub {
-                if ($fh) {
+        $req->{end} = sub {
+                $req->{fail} = $req->{cb} = $req->{end} = undef;
+                if (my $fh = delete $req->{fh}) {
                         # write out the last bit that was buffered
                         my @buf = split(/\n/, delete $req->{dbuf}, -1);
                         my $s = '';
@@ -48,48 +48,47 @@ sub call_git_diff {
                         $fh->write($s);
 
                         $fh->close;
-                        $fh = undef;
-                } elsif ($res) {
+                } elsif (my $res = delete $req->{res}) {
                         $res->($self->r(500));
                 }
-                if ($rpipe) {
+                if (my $rpipe = delete $req->{rpipe}) {
                         $rpipe->close; # _may_ be Danga::Socket::close
-                        $rpipe = undef;
                 }
         };
-        my $fail = sub {
+        $req->{fail} = sub {
                 if ($!{EAGAIN} || $!{EINTR}) {
                         select($vin, undef, undef, undef) if defined $vin;
                         # $vin is undef on async, so this is a noop on EAGAIN
                         return;
                 }
                 my $e = $!;
-                $end->();
+                $req->{end}->();
                 $err->print("git diff ($git->{git_dir}): $e\n");
         };
-        my $cb = sub {
+        $req->{cb} = sub {
                 my $off = length($req->{dbuf});
-                my $n = $rpipe->sysread($req->{dbuf}, 8192, $off);
-                return $fail->() unless defined $n;
-                return $end->() if $n == 0;
-                if ($res) {
+                my $n = $req->{rpipe}->sysread($req->{dbuf}, 8192, $off);
+                return $req->{fail}->() unless defined $n;
+                return $req->{end}->() if $n == 0;
+                if (my $res = delete $req->{res}) {
                         my $h = ['Content-Type', 'text/html; charset=UTF-8'];
-                        $fh = $res->([200, $h]);
-                        $res = undef;
+                        my $fh = $req->{fh} = $res->([200, $h]);
                         my $o = { nofollow => 1, noindex => 1 };
                         $fh->write($self->html_start($req, 'diff', $o)."\n");
                 }
-                git_diff_to_html($req, $fh) if $fh;
+                if (my $fh = $req->{fh}) {
+                        git_diff_to_html($req, $fh);
+                }
         };
         if (my $async = $env->{'pi-httpd.async'}) {
-                $rpipe = $async->($rpipe, $cb);
-                sub { ($res) = @_ } # let Danga::Socket handle the rest.
+                $req->{rpipe} = $async->($req->{rpipe}, $req->{cb});
+                sub { $req->{res} = $_[0] } # let Danga::Socket handle the rest.
         } else { # synchronous loop for other PSGI servers
                 $vin = '';
-                vec($vin, fileno($rpipe), 1) = 1;
+                vec($vin, fileno($req->{rpipe}), 1) = 1;
                 sub {
-                        ($res) = @_;
-                        while ($rpipe) { $cb->() }
+                        $req->{res} = $_[0];
+                        while ($req->{rpipe}) { $req->{cb}->() }
                 }
         }
 }