/[Frey]/branches/zimbardo/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

Diff of /branches/zimbardo/lib/Frey/SVN.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.770  
changed lines
  Added in v.1081

  ViewVC Help
Powered by ViewVC 1.1.26