/[Frey]/trunk/lib/Frey/SVN.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/SVN.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1068 - (show annotations)
Mon Apr 27 20:30:20 2009 UTC (15 years ago) by dpavlin
File size: 6991 byte(s)
read from diff directly for huge memory savings
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 $diff_file = $svn_path;
124 $diff_file =~ s{^\w+:/+}{};
125 mkpath "var/svn/$diff_file" unless -e "var/svn/$diff_file";
126 $diff_file = "var/svn/$diff_file/$rev.diff";
127 my $diff_fh;
128 my $diff_out;
129 if ( ! -e $diff_file || ! -s $diff_file ) {
130 my $cmd = "svn diff -c $rev $svn_path";
131 open($diff_fh, '-|', $cmd ) || die "can't open pipe from $cmd: $!";
132 open($diff_out,'>' , $diff_file ) || die "can't write $diff_file: $!";
133 warn "# creating $diff_file from $cmd\n";
134 } else {
135 open($diff_fh, '<' , $diff_file ) || die "can't read $diff_file: $!";
136 }
137
138 my $diff_path;
139
140 while( <$diff_fh> ) {
141 $e->{diff} .= $_ if $self->include_diff;
142 print $diff_out $_ if defined $diff_out;
143
144 if ( m{^\+\+\+ (\S+)} ) {
145 $diff_path = "/$1"; # subversion paths start with /
146 } elsif ( m{^\+} && $diff_path ) {
147 $e->{diff_paths}->{$diff_path}->{added}++;
148 } elsif ( m{^-} && $diff_path ) {
149 $e->{diff_paths}->{$diff_path}->{removed}++;
150 }
151 }
152 }
153
154 $coderef->($e);
155 }
156 }
157
158 sub as_markup {
159 my ($self) = @_;
160
161 # extract svk revision: r113@athlon (orig r999): dpavlin | 2005-09-01 20:38:07 +0200
162 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*';
163
164 sub encode {
165 my $foo = shift;
166 $foo =~ s/$svk_rev_re//gsm;
167 $foo =~ s/</&lt;/g;
168 $foo =~ s/>/&gt;/g;
169 $foo =~ s/"/&quot;/g;
170 # $foo =~ s/([\n\r][\n\r]+)/$1<br\/>/gis;
171 $foo =~ s/([\n\r]+)([\-\*]\s+)/$1<br\/>$2/gis;
172 $foo =~ s/([\n\r]+)(r\d+:\s+)/$1<br\/>$2/gis;
173 $foo =~ s/([\n\r]+)(\s+r\d+@)/$1<br\/>$2/gis; # svk
174 return $foo;
175 }
176
177 my $repository = $self->repository;
178 my $path = $self->path;
179
180 our $html = qq|
181 <h1><a href="?repository=$repository">$repository</a></h1>
182 <h2>$path</h2>
183 |;
184
185 $self->add_css(qq|
186 .commit {
187 clear: both;
188 padding-top: 1em;
189 padding-bottom: 1em;
190 border-top: 1px dashed #ccc;
191 }
192 .files {
193 color: #888;
194 font-family: monospace;
195 font-size: 80%;
196 float: right;
197 padding-bottom: 1.2em; /* fix 80% back to original 1em */
198 }
199 .files a {
200 text-decoration: none;
201 color: #888;
202 }
203 .date, .revision { color: #666; }
204 .message {
205 padding-top: 0.5em;
206 padding-left: 2em; /* like blockquote */
207 white-space: pre-wrap;
208 }
209
210 ins { color: #8c8 }
211 del { color: #c88 }
212 |);
213
214 my $max_path_len = 0;
215 my $path_count;
216
217 $self->iterator( sub {
218 my $e = shift;
219
220 my $rev = $e->{'revision'};
221 my $date = $e->{'date'};
222
223 $date =~ s/T/ /;
224 $date =~ s/\.\d+Z$//;
225
226 my $msg = $e->{'msg'};
227 $msg = '' if ref($msg); # FIXME why do I need this, dammit?
228 if ( $msg ) {
229 $msg = encode( $msg );
230 $msg = qq|<div class="message">$msg</div>|;
231 }
232
233 my @files;
234
235 foreach my $p (@{$e->{'paths'}->{'path'}}) {
236 my ($action,$path) = ($p->{'action'},$p->{'content'});
237
238 if ($action eq "A") {
239 push @files, "<ins>$path</ins>";
240 } elsif ($action eq "D") {
241 push @files, "<del>$path</del>";
242 } else {
243 push @files, $path;
244 }
245
246 $max_path_len = length $path if length $path > $max_path_len;
247 $path_count->{$path}++;
248 }
249
250 my $diff = $self->html_diff( $e->{diff} ) if $e->{diff};
251
252 $html .= $self->dump( $e->{diff_paths} );
253
254 $html .= qq|
255 <div class="commit">
256 <span class="date">$date</span>
257 <em>$e->{author}</em>
258 <span class="revision">$e->{revision}</span>
259 <div class="files">\n
260 |
261 . join("<br>\n",
262 map {
263 my $path = $_;
264 $path =~ s{<[^>]+>}{}g;
265 qq|<a href="?repository=$repository;path=$path" title="$path ##">$_</a>|
266 } @files
267 )
268 . qq|
269 </div>
270 $msg
271 $diff
272 </div>
273 |;
274
275 });
276
277 $self->add_css(qq|
278 .files {
279 width: ${max_path_len}ex;
280 }
281 |);
282
283 $html =~ s[title="(\S+) ##"]['title="' . $path_count->{$1} . '"']gse;
284
285 return $html;
286 }
287
288 sub codeswarm_as_markup {
289 my ($self) = @_;
290
291 $self->content_type('text/xml');
292
293 my $file_events = '';
294
295 $self->iterator( sub {
296 my $e = shift;
297
298 my $rev = $e->{'revision'};
299 my $date = DateTimeX::Easy->new( $e->{'date'} )->epoch . '000'; # ms
300 my $author = $e->{'author'};
301
302 foreach my $p (@{$e->{'paths'}->{'path'}}) {
303 my ($action,$path) = ($p->{'action'},$p->{'content'});
304 my $weight = '';
305 if ( my $s = $e->{diff_paths}->{$path} ) {
306 $weight = $s->{removed} if $s->{removed};
307 $weight += $s->{added} * 2 if $s->{added};
308 $weight = qq| weight="$weight" |;
309 }
310 $file_events .= qq|\t<event filename="$path" date="$date" author="$author"$weight/>\n|;
311 }
312
313 });
314
315 return qq|<?xml version="1.0"?>
316 <!-- One commit per day for one month by a documenter and programmer. -->
317 <file_events>
318 $file_events
319 </file_events>
320 |;
321
322 }
323
324 1;

  ViewVC Help
Powered by ViewVC 1.1.26