public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 4b074fcc79834415c5b5929f7ab4a40dbed0d41a 4102 bytes (raw)
$ git show repobrowse:lib/PublicInbox/RepoGitAtom.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
 
# Copyright (C) 2016 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# show log as an Atom feed
package PublicInbox::RepoGitAtom;
use strict;
use warnings;
use PublicInbox::Hval qw(utf8_html);
use base qw(PublicInbox::RepoBase);
use PublicInbox::Qspawn;

use constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
use constant STATES => qw(H ct an ae at s b);
use constant STATE_BODY => (scalar(STATES) - 1);
my $ATOM_FMT = '--pretty=tformat:'.
		join('%n', map { "%$_" } STATES).'%x00';
use POSIX qw(strftime);

sub repo_root_url {
	my ($self, $req) = @_;
	my $env = $req->{env};
	my $uri = $env->{REQUEST_URI};
	$uri =~ s/\?.+\z//; # no query string
	my @uri = split(m!/+!, $uri);
	my @extra = @{$req->{extra}};
	while (@uri && @extra && $uri[-1] eq $extra[-1]) {
		pop @uri;
		pop @extra;
	}
	pop @uri if $uri[-1] eq 'atom'; # warn if not equal?
	PublicInbox::Repobrowse::base_url($env) . join('/', @uri);
}

sub flush_hdr ($$$) {
	my ($dst, $hdr, $url) = @_;
	$$dst .= '<entry><title>';
	$$dst .= utf8_html($hdr->{'s'}); # commit subject
	$$dst .= '</title><updated>';
	$$dst .= strftime(DATEFMT, gmtime($hdr->{ct}));
	$$dst .= '</updated><author><name>';
	$$dst .= utf8_html($hdr->{an});
	$$dst .= '</name><email>';
	$$dst .= utf8_html($hdr->{ae});
	$$dst .= '</email></author><published>';
	$$dst .= strftime(DATEFMT, gmtime($hdr->{at}));
	$$dst .= '</published>';
	$$dst .= qq(<link\nrel="alternate"\ntype="text/html"\nhref=");
	$$dst .= $url;
	$$dst .= '/commit/';

	my $H = $hdr->{H};
	$$dst .= $H;
	$$dst .= qq("\n/><id>);
	$$dst .= $H;
	$$dst .= qq(</id>);

	$$dst .= qq(<content\ntype="xhtml"><div\nxmlns=");
	$$dst .= qq(http://www.w3.org/1999/xhtml">);
	$$dst .= qq(<pre\nstyle="white-space:pre-wrap">);
	undef
}

sub git_atom_sed ($$) {
	my ($self, $req) = @_;
	my $buf = '';
	my $state = 0;
	my $rel = $req->{relcmd};
	my $repo = $req->{-repo};
	my $tip = $repo->tip;
	my $title = join('/', $repo->{repo}, @{$req->{extra}});
	$title = utf8_html("$title, $tip");
	my $url = repo_root_url($self, $req);
	my $hdr = {};
	my $subtitle = $repo->desc_html;
	$req->{axml} = qq(<?xml version="1.0"?>\n) .
		qq(<feed\nxmlns="http://www.w3.org/2005/Atom">) .
		qq(<title>$title</title>) .
		qq(<subtitle>$subtitle</subtitle>) .
		qq(<link\nrel="alternate"\ntype="text/html"\nhref="$url"\n/>);
	my ($plinks, $ai);
	my $end = '';
	my $blines;
	sub {
		my $dst;
		# $_[0] == scalar buffer, undef means EOF from "git log"
		$dst = delete $req->{axml} || '';
		my @tmp;
		if (defined $_[0]) {
			$buf .= $_[0];
			@tmp = split(/\n/, $buf, -1);
			$buf = @tmp ? pop(@tmp) : '';
		} else {
			@tmp = split(/\n/, $buf, -1);
			$buf = '';
			$end = '</feed>';
		}

		foreach my $l (@tmp) {
			if ($state != STATE_BODY) {
				$hdr->{((STATES)[$state])} = $l;
				if (++$state == STATE_BODY) {
					flush_hdr(\$dst, $hdr, $url);
					$hdr = {};
					$blines = 0;
				}
				next;
			}
			if ($l eq "\0") {
				$dst .= qq(</pre></div></content></entry>);
				$state = 0;
			} else {
				$dst .= "\n" if $blines++;
				$dst .= utf8_html($l);
			}
		}
		$dst .= $end;
	}
}

sub git_atom_cb {
	my ($self, $req) = @_;
	sub {
		my ($r) = @_;
		my $env = $req->{env};
		if (!defined $r) {
			my $git = $req->{-repo}->{git};
			return $self->rt(400, 'plain', $git->err);
		}
		$env->{'qspawn.filter'} = git_atom_sed($self, $req);
		[ 200, [ 'Content-Type', 'application/atom+xml' ] ];
	}
}

sub call_git_atom {
	my ($self, $req) = @_;
	my $repo = $req->{-repo};
	my $max = $repo->{max_commit_count} || 10;
	$max = int($max);
	$max = 50 if $max == 0;

	my $git = $repo->{git};
	my $env = $req->{env};
	my $tip = $req->{tip} || $repo->tip;
	my $read_log = sub {
		my $cmd = $git->cmd(qw(log --no-notes --no-color --no-abbrev),
					$ATOM_FMT, "-$max", $tip, '--');
		my $expath = $req->{expath};
		push @$cmd, $expath if $expath ne '';
		my $rdr = { 2 => $git->err_begin };
		my $qsp = PublicInbox::Qspawn->new($cmd, undef, undef, $rdr);
		$qsp->psgi_return($env, undef, git_atom_cb($self, $req));
	};

	sub {
		$env->{'qspawn.response'} = $_[0];
		$read_log->();
	}
}

1;

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