about summary refs log tree commit homepage
path: root/lib/PublicInbox/RepoGitRaw.pm
blob: fabc237bc5ebcb11d0bf92ac2b469fca39e3c7cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (C) 2015-2016 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
package PublicInbox::RepoGitRaw;
use strict;
use warnings;
use base qw(PublicInbox::RepoBase);
use PublicInbox::Hval qw(utf8_html);
use PublicInbox::Qspawn;
my $MAX_ASYNC = 65536;

sub call_git_raw {
	my ($self, $req) = @_;
	my $repo = $req->{-repo};
	my $git = $repo->{git};
	my $tip = $req->{tip} || $repo->tip;
	my $expath = $req->{expath};
	my $obj = $tip;
	$obj .= ":$expath" if $expath ne '';
	my $env = $req->{env};
	sub {
		my ($res) = @_;
		$git->check_async($env, $obj, sub {
			my ($info) = @_;
			my ($hex, $type, $size) = @$info;
			if (!defined $type || $type eq 'missing') {
				return $res->($self->rt(404, 'plain',
						'Not Found'));
			}
			my $ct;
			if ($type eq 'blob') {
				my $base = $req->{extra}->[-1];
				$ct = $self->mime_type($base) if defined $base;
				$ct ||= 'text/plain; charset=UTF-8' if !$size;
			} elsif ($type eq 'commit' || $type eq 'tag') {
				$ct = 'text/plain; charset=UTF-8';
			} elsif ($type eq 'tree') {
				return git_tree_raw($self, $req, $res, $hex);
			} else {
				$ct = 'application/octet-stream';
			}
			show_raw($self, $req, $res, $ct, $hex, $type, $size);
		});
	}
}

sub git_tree_sed ($) {
	my ($req) = @_;
	my $buf = '';
	my $end = '';
	my $pfx = $req->{tpfx};
	sub { # $_[0] = buffer or undef
		my $dst = delete $req->{tstart} || '';
		my @files;
		if (defined $_[0]) {
			@files = split(/\0/, $buf .= $_[0]);
			$buf = pop @files if scalar @files;
		} else {
			@files = split(/\0/, $buf);
			$end = '</ul></body></html>';
		}
		foreach my $n (@files) {
			$n = PublicInbox::Hval->utf8($n);
			my $ref = $n->as_path;
			$dst .= qq(<li><a\nhref="$pfx$ref">);
			$dst .= $n->as_html;
			$dst .= '</a></li>';
		}
		$dst .= $end;
	}
}

sub git_tree_raw {
	my ($self, $req, $res, $hex) = @_;

	my @ex = @{$req->{extra}};
	my $rel = $req->{relcmd};
	my $title = utf8_html(join('/', '', @ex, ''));
	my $repo = $req->{-repo};
	my $pfx = ($req->{tip} || $repo->tip) . '/';
	my $t = "<h2>$title</h2><ul>";
	if (@ex) {
		$t .= qq(<li><a\nhref="./">../</a></li>);
		my $last = PublicInbox::Hval->utf8($ex[-1])->as_href;
		$pfx = "$last/";
	}

	$req->{tpfx} = $pfx;
	$req->{tstart} = "<html><head><title>$title</title></head><body>".$t;
	my $git = $repo->{git};
	my $cmd = $git->cmd(qw(ls-tree --name-only -z), $hex);
	my $rdr = { 2 => $git->err_begin };
	my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
	my $env = $req->{env};
	$env->{'qspawn.response'} = $res;
	$qsp->psgi_return($env, undef, sub {
		my ($r) = @_;
		if (!defined $r) {
			$self->rt(500, 'plain', $git->err);
		} else {
			$env->{'qspawn.filter'} = git_tree_sed($req);
			$self->rt(200, 'html');
		}
	});
}

sub show_raw {
	my ($self, $req, $res, $ct, $hex, $type, $size) = @_;
	my $env = $req->{env};
	my $git = $req->{-repo}->{git};
	if (1 || $size > $MAX_ASYNC) {
		my $rdr = { 2 => $git->err_begin };
		my $cmd = $git->cmd('cat-file', $type, $hex);
		my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
		$env->{'qspawn.response'} = $res;
		my $n = 0;
		my $buf = '';
		$qsp->psgi_return($env, undef, sub {
			my ($r, $bref) = @_;
			if (!defined $r) {
				$self->rt(500, 'plain', $git->err);
			} elsif (defined $ct) {
				[ 200, [ 'Content-Type', $ct ] ];
			} else {
				return $self->rt(200, 'plain');
				if (index($$bref, "\0") >= 0) {
					$ct = 'application/octet-stream';
					return [200, ['Content-Type', $ct]];
				}
				my $n = bytes::length($$bref);
				if ($n >= 8000 || $n == $size) {
					return $self->rt(200, 'plain');
				}
				# else: bref will keep growing...
			}
		});
	}
	# TODO: forkless for small files
}

1;