1 |
package Frey::SVN; |
2 |
use Moose; |
3 |
|
4 |
# Convert output from svn log to html page (with some formatting of |
5 |
# commit messages) |
6 |
# |
7 |
# 2004-04-28 Dobrica Pavlinusic <dpavlin@rot13.org> |
8 |
|
9 |
extends 'Frey'; |
10 |
with 'Frey::Web'; |
11 |
with 'Frey::Storage'; |
12 |
with 'Frey::HTML::Diff'; |
13 |
|
14 |
use XML::Simple; |
15 |
use DateTimeX::Easy; |
16 |
use Text::Diff::Parser; |
17 |
use File::Path qw/mkpath/; |
18 |
|
19 |
has repository => ( |
20 |
is => 'rw', |
21 |
isa => 'Str', |
22 |
required => 1, |
23 |
default => 'file:///home/dpavlin/private/svn/Frey', |
24 |
); |
25 |
|
26 |
has path => ( |
27 |
is => 'rw', |
28 |
isa => 'Str' |
29 |
); |
30 |
|
31 |
has limit => ( |
32 |
is => 'rw', |
33 |
isa => 'Int', |
34 |
default => 50, |
35 |
); |
36 |
|
37 |
has include_diff => ( |
38 |
is => 'ro', |
39 |
isa => 'Bool', |
40 |
default => 0, |
41 |
); |
42 |
|
43 |
has file_stats => ( |
44 |
is => 'ro', |
45 |
isa => 'Bool', |
46 |
default => 1, |
47 |
); |
48 |
|
49 |
sub iterator { |
50 |
my ($self,$coderef) = @_; |
51 |
|
52 |
sub sh_regex($$) { |
53 |
my ($cmd,$regex) = @_; |
54 |
open(my $sh, $cmd . ' |') || die "sh_regex failed on $cmd: $!"; |
55 |
while(my $l = <$sh>) { |
56 |
chomp($l); |
57 |
if ($l =~ $regex) { |
58 |
if ($1 && $2) { |
59 |
return ($1,$2); |
60 |
} elsif ($1) { |
61 |
return $1; |
62 |
} else { |
63 |
return $l; |
64 |
} |
65 |
} |
66 |
} |
67 |
#warn "can't find $regex in output of $cmd\n"; |
68 |
return; |
69 |
} |
70 |
|
71 |
my $path = $self->repository . $self->path; |
72 |
warn "# path $path\n"; |
73 |
|
74 |
my $cmd; |
75 |
my $svn_path = $path; |
76 |
|
77 |
if ($path =~ m#file://# || -e "$path/.svn") { |
78 |
$cmd = "svn log -v --xml $path"; |
79 |
} else { |
80 |
$svn_path = sh_regex('svk info', qr#Mirrored From:\s+([^,]+)#i); |
81 |
|
82 |
if (! $svn_path) { |
83 |
|
84 |
my $svk_depot = sh_regex('svk info', qr#Depot Path: (/.+)#i); |
85 |
|
86 |
my $depot = $svk_depot; |
87 |
my $rel_path; |
88 |
|
89 |
my $path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i); |
90 |
|
91 |
while (! $path && $depot =~ s{^(/.*/)([^/]+)/?$}{$1} ) { |
92 |
$rel_path = "$2/$rel_path"; |
93 |
$path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i); |
94 |
} |
95 |
|
96 |
die "can't find depot path '$svk_depot' in svk depot --list\n" unless ($path); |
97 |
$svn_path = "file:///$path/$rel_path"; |
98 |
} |
99 |
|
100 |
$cmd = "svn log -v --xml $svn_path"; |
101 |
} |
102 |
|
103 |
$cmd .= " --limit " . $self->limit if $self->limit; |
104 |
|
105 |
warn "# $cmd\n"; |
106 |
open(my $fh, $cmd .' |') || die "failed $cmd: $!"; |
107 |
my $log; |
108 |
while(<$fh>) { |
109 |
$log .= $_; |
110 |
} |
111 |
close($fh); |
112 |
|
113 |
my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); |
114 |
|
115 |
foreach my $e (@{$xml->{'logentry'}}) { |
116 |
warn "# e = ",$self->dump( $e ) if $self->debug; |
117 |
|
118 |
if ( $self->include_diff || $self->file_stats ) { |
119 |
my $rev = $e->{'revision'}; |
120 |
|
121 |
$e->{diff_paths}->{rev} = $rev; # XXX debug |
122 |
|
123 |
my $cache = $svn_path; |
124 |
$cache =~ s{^\w+:/+}{}; |
125 |
$cache = "var/svn/$cache"; |
126 |
mkpath $cache unless -e $cache; |
127 |
my $diff_paths = $self->load( "$cache/$rev.yaml" ); |
128 |
if ( ! $diff_paths ) { |
129 |
my $cmd = "svn diff -c $rev $svn_path"; |
130 |
my ( $diff_fh, $diff_out ); |
131 |
my $diff_file = "$cache/$rev.diff"; |
132 |
|
133 |
open($diff_fh, '-|', $cmd) || die "can't open pipe from $cmd: $!"; |
134 |
open($diff_out,'>' , $diff_file) || die "can't write $diff_file: $!"; |
135 |
warn "# creating $diff_file from $cmd\n"; |
136 |
|
137 |
my $diff_path; |
138 |
|
139 |
while( <$diff_fh> ) { |
140 |
print $diff_out $_; |
141 |
|
142 |
if ( m{^\+\+\+ (\S+)} ) { |
143 |
$diff_path = "/$1"; # subversion paths start with / |
144 |
} elsif ( m{^\+} && $diff_path ) { |
145 |
$diff_paths->{$diff_path}->{added}++; |
146 |
} elsif ( m{^-} && $diff_path ) { |
147 |
$diff_paths->{$diff_path}->{removed}++; |
148 |
} |
149 |
} |
150 |
|
151 |
$self->store( "$cache/$rev.yaml", $diff_paths ); |
152 |
} |
153 |
$e->{diff} = $self->load( "$cache/$rev.diff" ) if $self->include_diff; |
154 |
$e->{diff_paths} = $self->load( "$cache/$rev.yaml" ); |
155 |
} |
156 |
|
157 |
$coderef->($e); |
158 |
} |
159 |
} |
160 |
|
161 |
sub as_markup { |
162 |
my ($self) = @_; |
163 |
|
164 |
# extract svk revision: r113@athlon (orig r999): dpavlin | 2005-09-01 20:38:07 +0200 |
165 |
our $svk_rev_re = '\s+(r\d+@\w+(\s+\(orig\s+r\d+\))*:\s+\w+\s+\|\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\s+\+\d+)\s*'; |
166 |
|
167 |
sub encode { |
168 |
my $foo = shift; |
169 |
$foo =~ s/$svk_rev_re//gsm; |
170 |
$foo =~ s/</</g; |
171 |
$foo =~ s/>/>/g; |
172 |
$foo =~ s/"/"/g; |
173 |
# $foo =~ s/([\n\r][\n\r]+)/$1<br\/>/gis; |
174 |
$foo =~ s/([\n\r]+)([\-\*]\s+)/$1<br\/>$2/gis; |
175 |
$foo =~ s/([\n\r]+)(r\d+:\s+)/$1<br\/>$2/gis; |
176 |
$foo =~ s/([\n\r]+)(\s+r\d+@)/$1<br\/>$2/gis; # svk |
177 |
return $foo; |
178 |
} |
179 |
|
180 |
my $repository = $self->repository; |
181 |
my $path = $self->path; |
182 |
|
183 |
our $html = qq| |
184 |
<h1><a href="?repository=$repository">$repository</a></h1> |
185 |
<h2>$path</h2> |
186 |
|; |
187 |
|
188 |
$self->add_css(qq| |
189 |
.commit { |
190 |
clear: both; |
191 |
padding-top: 1em; |
192 |
padding-bottom: 1em; |
193 |
border-top: 1px dashed #ccc; |
194 |
} |
195 |
.files { |
196 |
color: #888; |
197 |
font-family: monospace; |
198 |
font-size: 80%; |
199 |
float: right; |
200 |
padding-bottom: 1.2em; /* fix 80% back to original 1em */ |
201 |
} |
202 |
.files a { |
203 |
text-decoration: none; |
204 |
color: #888; |
205 |
} |
206 |
.date, .revision { color: #666; } |
207 |
.message { |
208 |
padding-top: 0.5em; |
209 |
padding-left: 2em; /* like blockquote */ |
210 |
white-space: pre-wrap; |
211 |
} |
212 |
|
213 |
ins { color: #8c8 } |
214 |
del { color: #c88 } |
215 |
|); |
216 |
|
217 |
my $max_path_len = 0; |
218 |
my $path_count; |
219 |
my $stats; |
220 |
|
221 |
$self->iterator( sub { |
222 |
my $e = shift; |
223 |
|
224 |
my $rev = $e->{'revision'}; |
225 |
my $date = $e->{'date'}; |
226 |
|
227 |
$date =~ s/T/ /; |
228 |
$date =~ s/\.\d+Z$//; |
229 |
|
230 |
my $msg = $e->{'msg'}; |
231 |
$msg = '' if ref($msg); # FIXME why do I need this, dammit? |
232 |
if ( $msg ) { |
233 |
$msg = encode( $msg ); |
234 |
$msg = qq|<div class="message">$msg</div>|; |
235 |
} |
236 |
|
237 |
my @files; |
238 |
|
239 |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
240 |
my ($action,$path) = ($p->{'action'},$p->{'content'}); |
241 |
|
242 |
if ($action eq "A") { |
243 |
push @files, "<ins>$path</ins>"; |
244 |
} elsif ($action eq "D") { |
245 |
push @files, "<del>$path</del>"; |
246 |
} else { |
247 |
push @files, $path; |
248 |
} |
249 |
|
250 |
$max_path_len = length $path if length $path > $max_path_len; |
251 |
$path_count->{$path}++; |
252 |
|
253 |
if ( my $added = $e->{diff_paths}->{$path}->{added} ) { |
254 |
$stats->{total_added} += $added; |
255 |
} |
256 |
|
257 |
if ( my $removed = $e->{diff_paths}->{$path}->{removed} ) { |
258 |
$stats->{total_removed} += $removed; |
259 |
} |
260 |
} |
261 |
|
262 |
my $diff = $self->html_diff( $e->{diff} ) if $e->{diff}; |
263 |
|
264 |
$self->add_css(qq| |
265 |
.diff-lines { |
266 |
margin-left: 1em; |
267 |
float: right; |
268 |
} |
269 |
|); |
270 |
|
271 |
$html .= qq| |
272 |
<div class="commit"> |
273 |
<span class="date">$date</span> |
274 |
<em>$e->{author}</em> |
275 |
<span class="revision">$e->{revision}</span> |
276 |
<div class="files">\n |
277 |
| |
278 |
. join("<br>\n", |
279 |
map { |
280 |
my $path = $_; |
281 |
$path =~ s{<[^>]+>}{}g; |
282 |
my $diff = ''; |
283 |
if ( $diff = $e->{diff_paths}->{$path} ) { |
284 |
$diff |
285 |
= qq|<span class="diff-lines">| |
286 |
. join(" ", |
287 |
map { |
288 |
my $v = $diff->{$_}; |
289 |
s[added][+$v]; |
290 |
s[removed][-$v]; |
291 |
$_; |
292 |
} keys %$diff |
293 |
) |
294 |
. qq|</span>| |
295 |
; |
296 |
warn "DIFF $diff"; |
297 |
} |
298 |
|
299 |
qq|$diff<a href="?repository=$repository;path=$path" title="$path ##">$_</a>| |
300 |
} @files |
301 |
) |
302 |
. qq| |
303 |
</div> |
304 |
$msg |
305 |
$diff |
306 |
</div> |
307 |
|; |
308 |
|
309 |
}); |
310 |
|
311 |
$max_path_len += |
312 |
length( $stats->{total_added} ) |
313 |
+ length( $stats->{total_removed} ) |
314 |
; |
315 |
|
316 |
$max_path_len += int( $max_path_len / 10 ); # we are using ex, so we add 10% |
317 |
|
318 |
$self->add_css(qq| |
319 |
.files { |
320 |
width: ${max_path_len}ex; |
321 |
} |
322 |
|); |
323 |
|
324 |
$html =~ s[title="(\S+) ##"]['title="' . $path_count->{$1} . '"']gse; |
325 |
|
326 |
$html .= $self->dump( 'stats', $stats ); |
327 |
|
328 |
return $html; |
329 |
} |
330 |
|
331 |
sub codeswarm_as_markup { |
332 |
my ($self) = @_; |
333 |
|
334 |
$self->content_type('text/xml'); |
335 |
|
336 |
my $file_events = ''; |
337 |
|
338 |
$self->iterator( sub { |
339 |
my $e = shift; |
340 |
|
341 |
my $rev = $e->{'revision'}; |
342 |
my $date = DateTimeX::Easy->new( $e->{'date'} )->epoch . '000'; # ms |
343 |
my $author = $e->{'author'}; |
344 |
|
345 |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
346 |
my ($action,$path) = ($p->{'action'},$p->{'content'}); |
347 |
my $weight = ''; |
348 |
if ( my $s = $e->{diff_paths}->{$path} ) { |
349 |
$weight = $s->{removed} || 0; |
350 |
$weight += $s->{added} * 2 if $s->{added}; |
351 |
$weight = qq| weight="$weight" |; |
352 |
} |
353 |
$file_events .= qq|\t<event filename="$path" date="$date" author="$author"$weight/>\n|; |
354 |
} |
355 |
|
356 |
}); |
357 |
|
358 |
return qq|<?xml version="1.0"?> |
359 |
<!-- One commit per day for one month by a documenter and programmer. --> |
360 |
<file_events> |
361 |
$file_events |
362 |
</file_events> |
363 |
|; |
364 |
|
365 |
} |
366 |
|
367 |
1; |