about summary refs log tree commit homepage
path: root/lib/PublicInbox/RepoGitLog.pm
blob: 7331963324435044e06aaa195f8981f1b46f099d (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
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# show the log view
package PublicInbox::RepoGitLog;
use strict;
use warnings;
use PublicInbox::Hval qw(utf8_html);
use base qw(PublicInbox::RepoBase);
use PublicInbox::RepoGit qw(git_dec_links git_commit_title);
use PublicInbox::Qspawn;
# cannot rely on --date=format-local:... yet, it is too new (September 2015)
use constant STATES => qw(H p D ai an s b);
use constant STATE_BODY => (scalar(STATES) - 1);
my $LOG_FMT = '--pretty=tformat:'.  join('%n', map { "%$_" } STATES).'%x00';

sub parent_links {
	if (@_ == 1) { # typical, single-parent commit
		qq(\n  parent <a\nhref="#p$_[0]">$_[0]</a>);
	} elsif (@_ > 0) { # merge commit
		"\n parents " .
			join("\n         ",
			map { qq(<a\nhref="#p$_">$_</a>) } @_);
	} else {
		''; # root commit
	}
}

sub flush_log_hdr ($$$) {
	my ($req, $dst, $hdr) = @_;
	my $lpfx = $req->{lpfx};
	my $seen = $req->{seen};
	$$dst .= '<hr /><pre>' if scalar keys %$seen;
	my $id = $hdr->{H};
	$seen->{$id} = 1;
	$$dst .= qq(<a\nid=p$id\n);
	$$dst .= qq(href="${lpfx}commit/$id"><b>);
	$$dst .= utf8_html($hdr->{'s'}); # FIXME may still OOM
	$$dst .= '</b></a>';
	my $D = $hdr->{D}; # FIXME: thousands of decorations may OOM us
	if ($D ne '') {
		$$dst .= ' (' . join(', ', git_dec_links($lpfx, $D)) . ')';
	}
	my @p = split(/ /, $hdr->{p});
	push @{$req->{parents}}, @p;
	my $plinks = parent_links(@p);
	$$dst .= "\n- ";
	$$dst .= utf8_html($hdr->{an});
	$$dst .= " @ $hdr->{ai}\n  commit $id$plinks\n";
	undef
}

sub git_log_sed_end ($$) {
	my ($req, $dst) = @_;
	$$dst .= '<hr /><pre>';
	my $m = '';
	my $np = 0;
	my $seen = $req->{seen};
	my $git = $req->{-repo}->{git};
	my $lpfx = $req->{lpfx};
	foreach my $p (@{$req->{parents}}) {
		next if $seen->{$p};
		$seen->{$p} = ++$np;
		my $s = git_commit_title($git, $p);
		$m .= qq(\n<a\nid=p$p\nhref="$p">$p</a>\t);
		$s = defined($s) ? utf8_html($s) : '';
		$m .= qq(<a\nhref="${lpfx}commit/$p">$s</a>);
	}
	if ($np == 0) {
		$$dst .= "No commits follow";
	} elsif ($np > 1) {
		$$dst .= "Unseen parent commits to follow (multiple choice):\n";
	} else {
		$$dst .= "Next parent to follow:\n";
	}
	$$dst .= $m;
	$$dst .= '</pre></body></html>';
}

sub git_log_sed ($$) {
	my ($self, $req) = @_;
	my $buf = '';
	my $state = 0;
	$req->{seen} = {};
	$req->{parents} = [];
	my $hdr = {};
	sub {
		my $dst;
		# $_[0] == scalar buffer, undef means EOF from "git log"
		$dst = delete $req->{lhtml} || '';
		my @tmp;
		if (defined $_[0]) {
			$buf .= $_[0];
			@tmp = split(/\n/, $buf, -1);
			$buf = @tmp ? pop(@tmp) : '';
		} else {
			@tmp = split(/\n/, $buf, -1);
			$buf = undef;
		}

		foreach my $l (@tmp) {
			if ($state != STATE_BODY) {
				$hdr->{((STATES)[$state])} = $l;
				if (++$state == STATE_BODY) {
					flush_log_hdr($req, \$dst, $hdr);
					$hdr = {};
				}
				next;
			}
			if ($l eq "\0") {
				$dst .= qq(</pre>);
				$state = 0;
			} else {
				$dst .= "\n";
				$dst .= utf8_html($l);
			}
		}
		git_log_sed_end($req, \$dst) unless defined $buf;
		$dst;
	};
}

sub call_git_log {
	my ($self, $req) = @_;
	my $repo = $req->{-repo};
	my $max = $repo->{max_commit_count} || 50;
	my $tip = $req->{tip} || $repo->tip;
	$req->{lpfx} = $req->{relcmd};
	$max = int($max);
	$max = 50 if $max == 0;
	my $env = $req->{env};
	my $git = $repo->{git};
	my $cmd = $git->cmd(qw(log --no-notes --no-color --no-abbrev),
				$LOG_FMT, "-$max", $tip, '--');
	my $rdr = { 2 => $git->err_begin };
	my $title = 'log: '.$repo->{repo}.' ('.utf8_html($tip).')';
	$req->{lhtml} = $self->html_start($req, $title) . "\n\n";
	my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
	$qsp->psgi_return($env, undef, sub {
		my ($r) = @_;
		if (!defined $r) {
			$self->rt(500, 'html', $git->err);
		} elsif ($r == 0) {
			$self->rt(404, 'html', $git->err);
		} else {
			$env->{'qspawn.filter'} = git_log_sed($self, $req);
			$self->rt(200, 'html');
		}
	});
}

1;