/[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 1066 - (show annotations)
Mon Apr 27 18:45:14 2009 UTC (15 years ago) by dpavlin
File size: 5827 byte(s)
added include_diff option to show diff with each commit
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
18 has repository => (
19 is => 'rw',
20 isa => 'Str',
21 required => 1,
22 default => 'file:///home/dpavlin/private/svn/Frey',
23 );
24
25 has path => (
26 is => 'rw',
27 isa => 'Str'
28 );
29
30 has limit => (
31 is => 'rw',
32 isa => 'Int',
33 default => 50,
34 );
35
36 has include_diff => (
37 is => 'ro',
38 isa => 'Bool',
39 default => 1,
40 );
41
42 sub iterator {
43 my ($self,$coderef) = @_;
44
45 sub sh_regex($$) {
46 my ($cmd,$regex) = @_;
47 open(my $sh, $cmd . ' |') || die "sh_regex failed on $cmd: $!";
48 while(my $l = <$sh>) {
49 chomp($l);
50 if ($l =~ $regex) {
51 if ($1 && $2) {
52 return ($1,$2);
53 } elsif ($1) {
54 return $1;
55 } else {
56 return $l;
57 }
58 }
59 }
60 #warn "can't find $regex in output of $cmd\n";
61 return;
62 }
63
64 my $path = $self->repository . $self->path;
65 warn "# path $path\n";
66
67 my $cmd;
68 my $svn_path = $path;
69
70 if ($path =~ m#file://# || -e "$path/.svn") {
71 $cmd = "svn log -v --xml $path";
72 } else {
73 $svn_path = sh_regex('svk info', qr#Mirrored From:\s+([^,]+)#i);
74
75 if (! $svn_path) {
76
77 my $svk_depot = sh_regex('svk info', qr#Depot Path: (/.+)#i);
78
79 my $depot = $svk_depot;
80 my $rel_path;
81
82 my $path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i);
83
84 while (! $path && $depot =~ s{^(/.*/)([^/]+)/?$}{$1} ) {
85 $rel_path = "$2/$rel_path";
86 $path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i);
87 }
88
89 die "can't find depot path '$svk_depot' in svk depot --list\n" unless ($path);
90 $svn_path = "file:///$path/$rel_path";
91 }
92
93 $cmd = "svn log -v --xml $svn_path";
94 }
95
96 $cmd .= " --limit " . $self->limit if $self->limit;
97
98 warn "# $cmd\n";
99 open(my $fh, $cmd .' |') || die "failed $cmd: $!";
100 my $log;
101 while(<$fh>) {
102 $log .= $_;
103 }
104 close($fh);
105
106 my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);
107
108 foreach my $e (@{$xml->{'logentry'}}) {
109 warn "# e = ",$self->dump( $e ) if $self->debug;
110
111 if ( $self->include_diff ) {
112 my $rev = $e->{'revision'};
113 my $file = $svn_path;
114 $file =~ s{^\w+:/+}{};
115 my $file = "var/svn/$path.diff";
116 my $diff = $self->load( $file );
117 if ( ! $diff ) {
118 $diff = `svn diff -c $rev $svn_path`;
119 $self->store( $file, $diff );
120 }
121 $e->{diff} .= $diff;
122 }
123
124 $coderef->($e);
125 }
126 }
127
128 sub as_markup {
129 my ($self) = @_;
130
131 # extract svk revision: r113@athlon (orig r999): dpavlin | 2005-09-01 20:38:07 +0200
132 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*';
133
134 sub encode {
135 my $foo = shift;
136 $foo =~ s/$svk_rev_re//gsm;
137 $foo =~ s/</&lt;/g;
138 $foo =~ s/>/&gt;/g;
139 $foo =~ s/"/&quot;/g;
140 # $foo =~ s/([\n\r][\n\r]+)/$1<br\/>/gis;
141 $foo =~ s/([\n\r]+)([\-\*]\s+)/$1<br\/>$2/gis;
142 $foo =~ s/([\n\r]+)(r\d+:\s+)/$1<br\/>$2/gis;
143 $foo =~ s/([\n\r]+)(\s+r\d+@)/$1<br\/>$2/gis; # svk
144 return $foo;
145 }
146
147 my $repository = $self->repository;
148 my $path = $self->path;
149
150 our $html = qq|
151 <h1><a href="?repository=$repository">$repository</a></h1>
152 <h2>$path</h2>
153 |;
154
155 $self->add_css(qq|
156 .commit {
157 clear: both;
158 padding-top: 1em;
159 padding-bottom: 1em;
160 border-top: 1px dashed #ccc;
161 }
162 .files {
163 color: #888;
164 font-family: monospace;
165 font-size: 80%;
166 float: right;
167 padding-bottom: 1.2em; /* fix 80% back to original 1em */
168 }
169 .files a {
170 text-decoration: none;
171 color: #888;
172 }
173 .date, .revision { color: #666; }
174 .message {
175 padding-top: 0.5em;
176 padding-left: 2em; /* like blockquote */
177 white-space: pre-wrap;
178 }
179
180 ins { color: #8c8 }
181 del { color: #c88 }
182 |);
183
184 my $max_path_len = 0;
185 my $path_count;
186
187 $self->iterator( sub {
188 my $e = shift;
189
190 my $rev = $e->{'revision'};
191 my $date = $e->{'date'};
192
193 $date =~ s/T/ /;
194 $date =~ s/\.\d+Z$//;
195
196 my $msg = $e->{'msg'};
197 $msg = '' if ref($msg); # FIXME why do I need this, dammit?
198 if ( $msg ) {
199 $msg = encode( $msg );
200 $msg = qq|<div class="message">$msg</div>|;
201 }
202
203 my @files;
204
205 foreach my $p (@{$e->{'paths'}->{'path'}}) {
206 my ($action,$path) = ($p->{'action'},$p->{'content'});
207
208 if ($action eq "A") {
209 push @files, "<ins>$path</ins>";
210 } elsif ($action eq "D") {
211 push @files, "<del>$path</del>";
212 } else {
213 push @files, $path;
214 }
215
216 $max_path_len = length $path if length $path > $max_path_len;
217 $path_count->{$path}++;
218 }
219
220 my $diff = $self->html_diff( $e->{diff} ) if $e->{diff};
221
222 $html .= qq|
223 <div class="commit">
224 <span class="date">$date</span>
225 <em>$e->{author}</em>
226 <span class="revision">$e->{revision}</span>
227 <div class="files">\n
228 |
229 . join("<br>\n",
230 map {
231 my $path = $_;
232 $path =~ s{<[^>]+>}{}g;
233 qq|<a href="?repository=$repository;path=$path" title="$path ##">$_</a>|
234 } @files
235 )
236 . qq|
237 </div>
238 $msg
239 $diff
240 </div>
241 |;
242
243 });
244
245 $self->add_css(qq|
246 .files {
247 width: ${max_path_len}ex;
248 }
249 |);
250
251 $html =~ s[title="(\S+) ##"]['title="' . $path_count->{$1} . '"']gse;
252
253 return $html;
254 }
255
256 sub codeswarm_as_markup {
257 my ($self) = @_;
258
259 $self->content_type('text/xml');
260
261 my $file_events = '';
262
263 $self->iterator( sub {
264 my $e = shift;
265
266 my $rev = $e->{'revision'};
267 my $date = DateTimeX::Easy->new( $e->{'date'} )->epoch . '000'; # ms
268 my $author = $e->{'author'};
269
270 foreach my $p (@{$e->{'paths'}->{'path'}}) {
271 my ($action,$path) = ($p->{'action'},$p->{'content'});
272 $file_events .= qq|\t<event filename="$path" date="$date" author="$author" />\n|;
273 }
274
275 });
276
277 return qq|<?xml version="1.0"?>
278 <!-- One commit per day for one month by a documenter and programmer. -->
279 <file_events>
280 $file_events
281 </file_events>
282 |;
283
284 }
285
286 1;

  ViewVC Help
Powered by ViewVC 1.1.26