public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 5104877302679a000b44ee7c0700336fd68c999b 6363 bytes (raw)
$ git show repobrowse:lib/PublicInbox/RepoGitSrc.pm	# shows this blob on the CLI

  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 
# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
package PublicInbox::RepoGitSrc;
use strict;
use warnings;
use base qw(PublicInbox::RepoBase);
use PublicInbox::Hval qw(utf8_html);
use PublicInbox::Qspawn;

my %GIT_MODE = (
	'100644' => ' ', # blob
	'100755' => 'x', # executable blob
	'040000' => 'd', # tree
	'120000' => 'l', # symlink
	'160000' => 'g', # commit (gitlink)
);

my $BINARY_MSG = "Binary file, save using the 'raw' link above";
my $TOOBIG_MSG = "File is too big to display, save using the 'raw' link above";
my $MAX_ASYNC = 65536; # same as pipe size on Linux
my $BIN_DETECT = 8000; # same as git (buffer_is_binary in git.git)

sub call_git_src {
	my ($self, $req) = @_;
	my $repo = $req->{-repo};
	my $git = $repo->{git};
	my $tip = $req->{tip} or return $self->r(302, $req, $repo->tip);
	sub {
		my ($res) = @_;
		$git->check_async($req->{env}, "$tip:$req->{expath}", sub {
			my ($info) = @_;
			my ($hex, $type, $size) = @$info;
			unless (defined $type) {
				$res->($self->rt(404, 'plain', 'Not Found'));
			}
			show_tree($self, $req, $res, $hex, $type, $size);
		});
	}
}

sub show_tree {
	my ($self, $req, $res, $hex, $type, $size) = @_;
	my $opts = { nofollow => 1 };
	my $title = "tree: ".utf8_html($req->{expath});
	$req->{thtml} = $self->html_start($req, $title, $opts) . "\n";
	if ($type eq 'tree') {
		$opts->{noindex} = 1;
		git_tree_show($self, $req, $res, $hex);
	} elsif ($type eq 'blob') {
		git_blob_show($self, $req, $res, $hex, $size);
	} else {
		$res->($self->rt(404, 'plain',
			"Unrecognized type ($type) for $hex\n"));
	}
}

sub cur_path {
	my ($req) = @_;
	my @ex = @{$req->{extra}} or return '<b>root</b>';
	my $s;
	my $tip = $req->{tip};
	my $rel = $req->{relcmd};
	# avoid relative paths, here, we don't want to propagate
	# trailing-slash URLs although we tolerate them
	$s = "<a\nhref=\"${rel}src/$tip\">root</a>/";
	my $cur = pop @ex;
	my @t;
	$s .= join('/', (map {
		push @t, $_;
		my $e = PublicInbox::Hval->utf8($_, join('/', @t));
		my $ep = $e->as_path;
		my $eh = $e->as_html;
		"<a\nhref=\"${rel}src/$tip/$ep\">$eh</a>";
	} @ex), '<b>'.utf8_html($cur).'</b>');
}

sub git_blob_sed ($$$) {
	my ($req, $hex, $size) = @_;
	my $pfx = $req->{tpfx};
	my $nl = 0;
	my $bytes = 0;
	my @lines;
	my $buf = '';
	my $end = '';
	my $s;

	sub {
		my $dst = delete $req->{thtml} || '';
		if (defined $_[0]) {
			return '' if $bytes < 0; # binary
			if ($bytes <= $BIN_DETECT) {
				if (index($_[0], "\0") >= 0) {
					$bytes = -1;
					$s = delete $req->{lstart} and
						$dst .= $s;
					$dst .= "\n";
					$dst .= $BINARY_MSG;
					return $dst .= '</pre></body></html>';
				}
			}
			$bytes += bytes::length($_[0]);
			$buf .= $_[0];
			$_[0] = ''; # save some memory
			$s = delete $req->{lstart} and $dst .= $s;
			@lines = split(/\r?\n/, $buf, -1);
			$buf = pop @lines; # last line, careful...
		} else { # EOF
			$s = delete $req->{lstart} and $dst .= $s;
			@lines = split(/\r?\n/, $buf, -1);
			$buf = pop @lines;
			$end .= '</pre></body></html>';
		}
		foreach (@lines) {
			++$nl;
			$dst .= "<a\nid=n$nl>";
			$dst .= sprintf("% 5u</a>\t", $nl);
			$dst .= utf8_html($_);
			$dst .= "\n";
		}
		@lines = ();
		if ($end && defined $buf && $buf ne '') {
			++$nl;
			$dst .= "<a\nid=n$nl>";
			$dst .= sprintf("% 5u</a>\t", $nl);
			$dst .= utf8_html($buf);
			$buf = undef;
			$dst .= "\n\\ No newline at end of file";
		}
		$dst .= $end;
	}
}

sub git_blob_show {
	my ($self, $req, $res, $hex, $size) = @_;
	my $t = cur_path($req);
	my $rel = $req->{relcmd};
	my $raw = join('/', "${rel}raw", $req->{tip}, @{$req->{extra}});
	$raw = PublicInbox::Hval->utf8($raw)->as_path;
	$req->{thtml} .= qq{\npath: $t\n\nblob $hex} .
			qq{\t$size bytes (<a\nhref="$raw">raw</a>)};
	$req->{lstart} = '</pre><hr/><pre>';
	my $git = $req->{-repo}->{git};
	if ($size > $MAX_ASYNC) {
		my $html = delete($req->{thtml}) . delete($req->{lstart});
		$html .= $TOOBIG_MSG;
		$html .= '</pre></body></html>';
		return $res->($self->rt(200, 'html', $html));
	}

	my $buf = ''; # we slurp small files
	$git->cat_async($req->{env}, $hex, sub {
		my ($r) = @_;
		my $ref = ref($r);
		return if $ref eq 'ARRAY'; # redundant info
		if ($ref eq 'SCALAR') {
			$buf .= $$r;
		} elsif (!defined $r) {
			my $cb = $res or return;
			$res = undef;
			$cb->($self->rt(500, 'plain', "Error\n"));
		} elsif ($r == 0) {
			my $fh = $res->($self->rt(200, 'html'));
			my $sed = git_blob_sed($req, $hex, $size);
			$fh->write($sed->($buf));
			$fh->write($sed->(undef));
			$fh->close;
		}
	});
}

sub git_tree_sed ($) {
	my ($req) = @_;
	my @lines;
	my $buf = '';
	my $pfx = $req->{tpfx};
	my $end;
	sub {
		my $dst = delete $req->{thtml} || '';
		if (defined $_[0]) {
			@lines = split(/\0/, $buf .= $_[0]);
			$buf = pop @lines if @lines;
		} else {
			@lines = split(/\0/, $buf);
			$end = '</pre></body></html>';
		}
		for (@lines) {
			my ($m, $x, $s, $path) =
					(/\A(\S+) \S+ (\S+)( *\S+)\t(.+)\z/s);
			$m = $GIT_MODE{$m} or next;
			$path = PublicInbox::Hval->utf8($path);
			my $ref = $path->as_path;
			$path = $path->as_html;

			if ($m eq 'g') {
				# TODO: support cross-repository gitlinks
				$dst .= 'g' . (' ' x 15) . "$path @ $x\n";
				next;
			}
			elsif ($m eq 'd') { $path = "$path/" }
			elsif ($m eq 'x') { $path = "<b>$path</b>" }
			elsif ($m eq 'l') { $path = "<i>$path</i>" }
			$s =~ s/\s+//g;

			# 'raw' and 'log' links intentionally omitted
			# for brevity and speed
			$dst .= qq($m\t).
				qq($s\t<a\nhref="$pfx/$ref">$path</a>\n);
		}
		$dst;
	}
}

sub git_tree_show {
	my ($self, $req, $res, $hex) = @_;
	my $git = $req->{-repo}->{git};
	my $cmd = $git->cmd(qw(ls-tree -l -z --no-abbrev), $hex);
	my $rdr = { 2 => $git->err_begin };
	my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
	my $t = cur_path($req);
	my $pfx;

	$req->{thtml} .= "\npath: $t\n\n<b>mode\tsize\tname</b>\n";
	if (defined(my $last = $req->{extra}->[-1])) {
		$pfx = PublicInbox::Hval->utf8($last)->as_path;
	} else {
		$pfx = 'src/' . $req->{tip};
	}
	$req->{tpfx} = $pfx;
	my $env = $req->{env};
	$env->{'qspawn.response'} = $res;
	$qsp->psgi_return($env, undef, sub {
		my ($r) = @_;
		if (defined $r) {
			$env->{'qspawn.filter'} = git_tree_sed($req);
			$self->rt(200, 'html');
		} else {
			$self->rt(500, 'plain', $git->err);
		}
	});
}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git