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 |
|
13 |
use XML::Simple; |
14 |
|
15 |
has repository => ( |
16 |
is => 'rw', |
17 |
isa => 'Str', |
18 |
required => 1, |
19 |
default => 'file:///home/dpavlin/private/svn/Frey', |
20 |
); |
21 |
|
22 |
sub as_markup { |
23 |
my ($self) = @_; |
24 |
|
25 |
# extract svk revision: r113@athlon (orig r999): dpavlin | 2005-09-01 20:38:07 +0200 |
26 |
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*'; |
27 |
|
28 |
sub encode { |
29 |
my $foo = shift; |
30 |
$foo =~ s/$svk_rev_re//gsm; |
31 |
$foo =~ s/</</g; |
32 |
$foo =~ s/>/>/g; |
33 |
$foo =~ s/"/"/g; |
34 |
$foo =~ s/([\n\r][\n\r]+)/<\/p>$1<p>/gis; |
35 |
$foo =~ s/([\n\r]+)([\-\*]\s+)/$1<br\/>$2/gis; |
36 |
$foo =~ s/([\n\r]+)(r\d+:\s+)/$1<br\/>$2/gis; |
37 |
$foo =~ s/([\n\r]+)(\s+r\d+@)/$1<br\/>$2/gis; # svk |
38 |
return $foo; |
39 |
} |
40 |
|
41 |
sub sh_regex($$) { |
42 |
my ($cmd,$regex) = @_; |
43 |
open(my $sh, $cmd . ' |') || die "sh_regex failed on $cmd: $!"; |
44 |
while(my $l = <$sh>) { |
45 |
chomp($l); |
46 |
if ($l =~ $regex) { |
47 |
if ($1 && $2) { |
48 |
return ($1,$2); |
49 |
} elsif ($1) { |
50 |
return $1; |
51 |
} else { |
52 |
return $l; |
53 |
} |
54 |
} |
55 |
} |
56 |
#warn "can't find $regex in output of $cmd\n"; |
57 |
return; |
58 |
} |
59 |
|
60 |
my $path = $self->repository; |
61 |
warn "# path $path\n"; |
62 |
|
63 |
my $cmd; |
64 |
if ($path =~ m#file://# || -e "$path/.svn") { |
65 |
$cmd = "svn log -v --xml $path"; |
66 |
} else { |
67 |
my $svn_path = sh_regex('svk info', qr#Mirrored From:\s+([^,]+)#i); |
68 |
|
69 |
if (! $svn_path) { |
70 |
|
71 |
my $svk_depot = sh_regex('svk info', qr#Depot Path: (/.+)#i); |
72 |
|
73 |
my $depot = $svk_depot; |
74 |
my $rel_path; |
75 |
|
76 |
my $path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i); |
77 |
|
78 |
while (! $path && $depot =~ s{^(/.*/)([^/]+)/?$}{$1} ) { |
79 |
$rel_path = "$2/$rel_path"; |
80 |
$path = sh_regex('svk depot --list', qr/^$depot\s+(\S+)/i); |
81 |
} |
82 |
|
83 |
die "can't find depot path '$svk_depot' in svk depot --list\n" unless ($path); |
84 |
$svn_path = "file:///$path/$rel_path"; |
85 |
} |
86 |
|
87 |
$cmd = "svn log -v --xml $svn_path"; |
88 |
} |
89 |
|
90 |
warn "# $cmd\n"; |
91 |
open(my $fh, $cmd .' |') || die "failed $cmd: $!"; |
92 |
my $log; |
93 |
while(<$fh>) { |
94 |
$log .= $_; |
95 |
} |
96 |
close($fh); |
97 |
|
98 |
my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); |
99 |
|
100 |
our $html = ''; |
101 |
sub html { |
102 |
$html .= join("\n", @_); |
103 |
} |
104 |
|
105 |
foreach my $e (@{$xml->{'logentry'}}) { |
106 |
my $rev = $e->{'revision'}; |
107 |
my $date = $e->{'date'}; |
108 |
|
109 |
$date =~ s/T/ /; |
110 |
$date =~ s/\.\d+Z$//; |
111 |
|
112 |
html '<p><tt>'.$date.'</tt> <em>',$e->{'author'},'</em> <tt style="color:#808080">r',$e->{'revision'},'</tt></p>'; |
113 |
|
114 |
my @files; |
115 |
|
116 |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
117 |
my ($action,$path) = ($p->{'action'},$p->{'content'}); |
118 |
|
119 |
if ($action eq "A") { |
120 |
push @files, "<ins>$path</ins>"; |
121 |
} elsif ($action eq "D") { |
122 |
push @files, "<del>$path</del>"; |
123 |
} else{ |
124 |
push @files, $path; |
125 |
} |
126 |
} |
127 |
|
128 |
html '<blockquote><p><tt style="color:#808080">',join(", ",@files),':</tt> ',encode($e->{'msg'}),'</p></blockquote>'; |
129 |
|
130 |
} |
131 |
|
132 |
return $html; |
133 |
} |
134 |
|
135 |
1; |