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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#!/usr/bin/perl -w
# vcsweb - a sample utility program to show off the power of
# VCS::
# Do not edit this file, rather edit vcsweb.ini instead!
sub BEGIN {
# Read the configuration file
my $configfile = 'vcsweb.ini';
unless (my $return = do $configfile) {
die "Couldn't parse $configfile: $@" if $@;
die "Couldn't do $configfile: $!" unless defined $return;
die "Couldn't run $configfile" unless $return;
}
}
use strict;
no strict 'vars';
use CGI;
use CGI::Carp 'fatalsToBrowser';
use VCS;
$| = 1;
my $q = new CGI;
my $what = $q->param('what') || "";
if ($what =~ /\.\./) {
print "Nice try.";
exit 0;
}
my $base;
my $project = $q->param('project');
$base = $projects{$project} if (defined $project);
$base .= '/' if (defined $base && $base !~ m|/$|);
print $q->header;
if (defined $base) {
print qq|<head><title>vcsweb: $project</title></head>
<body bgcolor="#ffffff"><h3>vcsweb: $project</h3>|;
} else {
print qq|<head><title>vcsweb</title></head>
<body bgcolor="#ffffff"><h3>vcsweb</h3>|;
}
if (not defined $base) {
choose_project($q);
} elsif (defined $q->param('show')) {
show($q, $base, $what);
} elsif (defined $q->param('diff')) {
diff($q, $base, $what);
} else {
dir($q, $base, $what);
}
sub choose_project {
my $q = shift;
my $url;
my $bgcol = '#ffffff';
print qq|
<hr noshade><table cellpadding=8 cellspacing=0>
<tr><td bgcolor="#000000"><b><font color="#ffffff">Choose project to view...</font></b>
</td></tr>|;
foreach my $project (sort keys %projects) {
$q->param('project', $project);
$url = $q->self_url;
print qq|<tr><td bgcolor="$bgcol"><a href="$url">$project</a></td></tr>\n|;
$bgcol = ($bgcol eq "#ffffff") ? "#ddddff" : "#ffffff";
}
print "</table></ul><p><hr noshade>";
}
sub html_encode {
my $line = shift;
$line =~ s|&|&|g;
$line =~ s|\>|>|g;
$line =~ s|\<|<|g;
$line =~ s| |\ |g;
$line;
}
sub diff {
my($q, $base, $what) = @_;
my $file = $base . $what;
print qq|<hr noshade>
<table cellspacing=0 border=0 width=100% cellpadding=5>
<tr><td bgcolor="#000000"><img src="file.gif"> <b><font color="#ffffff">/$what</font></b><p></td></tr>
</table>
<ul>
|;
my $fromversion = $q->param('from');
my $toversion = $q->param('to');
my $fromobj = VCS::Version->new("$file/$fromversion");
my $toobj = VCS::Version->new("$file/$toversion");
print qq|Differences from <b>Revision $fromversion</b> to <b>Revision $toversion</b>...<p>
<table border=0 cellspacing=0 cellpadding=1>|;
foreach my $diffref (parse_diff($fromobj->diff($toobj)
or die "diff error")) {
print qq|<tr><td align=center bgcolor="#ccccff">...Line $diffref->{'oldline'}...</td>|;
print qq|<td bgcolor="#ffffff"> </td>|;
print qq|<td align=center bgcolor="#ccccff">...Line $diffref->{'newline'}...</td></tr>|;
foreach my $difflineref (@{$diffref->{'difflines'}}) {
my $old = html_encode($difflineref->{'old'});
my $new = html_encode($difflineref->{'new'});
if ($old eq $new) { # Line has not changed
print qq|<tr><td><tt><small>$old</small></tt></td>|;
print qq|<td bgcolor="#ffffff"> </td>|;
print qq|<td><tt><small>$new</small></tt></td></tr>\n|;
} elsif ($old eq '') { # Line has been added
print qq|<tr><td></td>|;
print qq|<td bgcolor="#ffffff"> </td>|;
print qq|<td bgcolor="#ccffcc"><tt><small>$new</small></tt></td></tr>\n|;
} elsif ($new eq '') { # Line has been deleted
print qq|<tr><td bgcolor="#ffcccc"><tt><small>$old</small></tt></td>|;
print qq|<td bgcolor="#ffffff"> </td>|;
print qq|<td> </td></tr>\n|;
} else { # Line has been modified
print qq|<tr><td bgcolor="#ffffbb"><tt><small>$old</small></tt></td>|;
print qq|<td bgcolor="#ffffff"> </td>|;
print qq|<td bgcolor="#ffffbb"><tt><small>$new</small></tt></td></tr>\n|;
}
}
print "<p>";
}
print "</table></ul>";
print "<p><hr><p>";
}
# parse_diff takes a unified diff and returns a list of \%diff
# %diff holds oldline=>num, newline=>num, difflines=>\list of \%difflines
# %difflines holds old=>text, new=>text
# It still needs the "flush" subroutine, just below
#
# And example structure follows, with one changed line (1), one empty
# line which has not changed (2), one added line (3), and one deleted
# line (15)
#
# (
# {
# 'oldline' => 1,
# 'newline' => 1,
# 'difflines' => (
# {
# 'old' => '# This is version 1.12',
# 'new' => '# This is version 1.13'
# },
# {
# 'old' => '',
# 'new' => ''
# },
# {
# 'old' => '',
# 'new' => 'use DBI;'
# }
# )
# },
# {
# 'oldline' => 15,
# 'newline' => 16,
# 'difflines' => (
# {
# 'old' => 'use strict;',
# 'new' => ''
# },
# )
# }
# )
sub parse_diff {
my $diff_text = shift;
my(@left, @right);
my @difflist; # this holds a list of \%diff
my $state = "dump";
my @difflines;
foreach my $line (split "\n", $diff_text) {
my ($oldline,$newline) = $line =~ /@@ \-(\d+).*\+(\d+).*@@/;
if ($oldline) {
if (@difflist) {
$difflist[-1]->{'difflines'} = [ @difflines ];
@difflines = ();
}
push @difflist, {
oldline => $oldline,
newline => $newline,
difflines => \@difflines,
};
} elsif ($line =~ s|^\+||) {
if ($state eq "dump") {
push @difflines, { old => '', new => $line };
} else {
$state = "PreChange";
push @right, $line;
}
} elsif ($line =~ s|^-||) {
$state = "PreChangeRemove";
push @left, $line;
} elsif ($line =~ m|^\\|) {
} else {
if ($state eq "PreChangeRemove") {
push @difflines, map { { old => $_, new => '' } } @left;
} elsif ($state eq "PreChange") {
for (my $j = 0; $j < @left || $j < @right ; $j++) {
push @difflines, {
old => ($j < @left ? $left[$j] : ''),
new => ($j < @right ? $right[$j] : ''),
};
}
}
@left = ();
@right = ();
$state = "dump";
$line =~ s|^.||;
push @difflines, { old => $line, new => $line };
}
}
if (@difflist) {
my @newdifflines = @difflines;
$difflist[-1]->{'difflines'} = \@newdifflines;
}
@difflist;
}
sub show {
my($q, $base, $what) = @_;
$q->delete('show');
my $file = $base . $what;
print qq|<hr noshade>
<table cellspacing=0 border=0 width=100% cellpadding=5>
<tr><td bgcolor="#000000"><img src="file.gif"> <b><font color="#ffffff">/$what</font></b><p></td></tr>
</table>
<ul>
|;
my $obj = VCS::File->new($file);
unless (defined $obj) {
print "Not a VCS file!</ul>";
return;
}
my($version, $number, $author, $tags, $date, $reason, $diffversion, $url);
my @versions = reverse $obj->versions;
my @diffversions = @versions;
shift @diffversions;
foreach $version (@versions) {
$number = $version->version;
$author = $version->author;
$tags = join ", ", sort $version->tags;
$date = $version->date;
$reason = html_encode($version->reason);
$reason =~ s|\n|<br>|g;
$diffversion = (@diffversions) ? (shift @diffversions)->version : "";
$q->param('to', $number);
$q->param('from', $diffversion);
$q->param('what', $what);
$q->param('diff', 1);
$url = $q->self_url;
print qq|
<table width="70%" bgcolor="#ddddff" cellspacing=0 cellpadding=5 border=0>
<tr>
<td><b>Revision $number</b></td>
<td align=right width="80%"><small>$date</small></td>
</tr>|;
print qq|<tr><td colspan=2>Tags: $tags</td></tr>| if $tags;
print qq|<tr><td valign=top><i>$author</i></td><td>$reason</td></tr>|;
print qq|<tr><td colspan=2><a href="$url">Diff with $diffversion...</a></td></tr>| if $diffversion;
print qq|</table><p>|;
}
print "</ul><hr noshade>";
}
sub dir {
my($q, $base, $what) = @_;
my($file, $relfile, $name, $url, $thing);
my $dir = $base . $what;
print qq|<hr noshade>
<table cellspacing=0 border=0 width=100% cellpadding=5>
<tr><td bgcolor="#000000"><img src="folder.gif"> <b><font color="#ffffff">/$what</font></b><p></td></tr>
|;
my $bgcol = "#ffffff";
my $d = VCS::Dir->new($dir);
unless (defined $d) {
print "</table><p>Not a VCS dir!</ul>";
return;
}
foreach $thing ($d->content) {
next unless ref($thing);
$file = $thing->url; # FIXME: suboptimal but should do
($name = $file) =~ s|$dir||;
($relfile = $file) =~ s|$base||;
if (ref($thing) =~ /::Dir$/) {
$q->param('what', "$relfile");
$url = $q->self_url;
print qq|<tr><td bgcolor="$bgcol"><img src="folder.gif"> <a href="$url">$name</a></td></tr>\n|;
$bgcol = ($bgcol eq "#ffffff") ? "#ddddff" : "#ffffff";
} elsif (ref($thing) =~ /::File$/) {
$q->param('what', "$relfile");
$q->param('show', 1);
$url = $q->self_url;
$q->delete('show');
print qq|<tr><td bgcolor="$bgcol"><img src="file.gif"> <a href="$url">$name</a></td></tr>\n|;
$bgcol = ($bgcol eq "#ffffff") ? "#ddddff" : "#ffffff";
}
}
print "</table><hr noshade>";
}
__END__
=head1 NAME
vcsweb - a web interface to the VCS suite
=head1 SYNOPSIS
http://hostname/location/vcsweb.cgi
=head1 DESCRIPTION
C<vcsweb> is a demonstration of the power of the C<VCS> (Version
Control System) suite, providing a web interface to projects under
version control. A project under any VCS can be viewed, provided there
is a C<VCS>-compliant module for that system.
To use, either copy or symlink C<vcsweb.cgi>, C<folder.gif> and
C<file.gif> to somewhere appropriate in the document root of a
web-server, and copy C<vcsweb.ini>. Modify C<vcsweb.ini> to taste.
=head1 SEE ALSO
L<VCS>.
|