about summary refs log tree commit homepage
path: root/lib/PublicInbox/RepoGitSummary.pm
blob: 0ecef981872a3bc5c070b4e06649dfff71d20c91 (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
# Copyright (C) 2016 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# The main summary/landing page of a git repository viewer
package PublicInbox::RepoGitSummary;
use strict;
use warnings;
use PublicInbox::Hval qw(utf8_html);
use base qw(PublicInbox::RepoBase);
use PublicInbox::Qspawn;

sub call_git_summary {
	my ($self, $req) = @_;
	my $git = $req->{repo_info}->{git};
	my $env = $req->{env};

	# n.b. we would use %(HEAD) in for-each-ref --format if we could
	# rely on git 1.9.0+, but it's too soon for that in early 2017...
	my $cmd = $git->cmd(qw(symbolic-ref HEAD));
	my $rdr = { 2 => $git->err_begin };
	my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
	sub {
		my ($res) = @_; # Plack streaming callback
		$qsp->psgi_qx($env, undef, sub {
			chomp(my $head_ref = ${$_[0]});
			for_each_ref($self, $req, $res, $head_ref);
		});
	}
}

use constant EACH_REF_FMT => '--format=' .
		join(' ', map { "%($_)" }
		qw(refname objecttype objectname creatordate:short subject));

sub for_each_ref {
	my ($self, $req, $res, $head_ref) = @_;
	my $count = 10; # TODO: configurable
	my $fh;
	my $repo_info = $req->{repo_info};
	my $git = $repo_info->{git};
	my $refs = $git->popen(qw(for-each-ref --sort=-creatordate),
				EACH_REF_FMT, "--count=$count",
				qw(refs/heads/ refs/tags/));
	$fh = $res->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
	# ref names are unpredictable in length and requires tables :<
	$fh->write($self->html_start($req,
				"$repo_info->{repo}: overview") .
			'</pre><table>');

	my $rel = $req->{relcmd};
	while (<$refs>) {
		my ($ref, $type, $hex, $date, $s) = split(' ', $_, 5);
		my $x = $ref eq $head_ref ? ' (HEAD)' : '';
		$ref =~ s!\Arefs/(?:heads|tags)/!!;
		$ref = PublicInbox::Hval->utf8($ref);
		my $h = $ref->as_html;
		$ref = $ref->as_href;
		my $sref;
		if ($type eq 'tag') {
			$h = "<b>$h</b>";
			$sref = $ref = $rel . 'tag/' . $ref;
		} elsif ($type eq 'commit') {
			$sref = $rel . 'commit/' . $ref;
			$ref = $rel . 'log/' . $ref;
		} else {
			# no point in wasting code to support tagged
			# trees/blobs...
			next;
		}
		chomp $s;
		$fh->write(qq(<tr><td><tt><a\nhref="$ref">$h</a>$x</tt></td>) .
			qq(<td><tt>$date <a\nhref="$sref">) . utf8_html($s) .
			'</a></tt></td></tr>');

	}
	$fh->write('</table>');

	# some people will use README.md or even README.sh here...
	my $readme = $repo_info->{readme};
	defined $readme or $readme = [ 'README', 'README.md' ];
	$readme = [ $readme ] if (ref($readme) ne 'ARRAY');
	foreach my $r (@$readme) {
		my $doc = $git->cat_file('HEAD:'.$r);
		defined $doc or next;
		$fh->write('<pre>' . readme_path_links($req, $rel, $r) .
			" (HEAD)\n\n" . utf8_html($$doc) . '</pre>');
	}
	$fh->write('</body></html>');
	$fh->close;
}

sub readme_path_links {
	my ($req, $rel, $readme) = @_;
	my @path = split(m!/+!, $readme);

	my $s = "tree <a\nhref=\"${rel}tree/$req->{-tip}\">root</a>/";
	my @t;
	$s .= join('/', (map {
		push @t, $_;
		my $e = PublicInbox::Hval->utf8($_, join('/', @t));
		my $ep = $e->as_path;
		my $eh = $e->as_html;
		$e = "<a\nhref=\"${rel}tree/$req->{-tip}/$ep\">$eh</a>";
		# bold the last one
		scalar(@t) == scalar(@path) ? "<b>$e</b>" : $e;
	} @path));
}

1;