1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# svn2js.pl |
4 |
# |
5 |
# 10/05/08 16:15:38 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
7 |
use strict; |
8 |
use XML::Simple; |
9 |
use Data::Dump qw/dump/; |
10 |
use JSON; |
11 |
|
12 |
my $debug = 0; |
13 |
|
14 |
my $max_items = 500; |
15 |
|
16 |
my $repository = 'file:///srv/svn/strix/'; |
17 |
my $svnweb = 'https://svn-strix.carnet.hr/private/svnweb/index.cgi/strix/revision?rev='; |
18 |
|
19 |
# for older JSON |
20 |
sub encode_json { objToJson( @_ ) } |
21 |
|
22 |
sub path_strip { |
23 |
my $path = shift; |
24 |
# $path =~ s!^/[^/]+/[^/]+/!/!; |
25 |
$path =~ s!^/(branches/)?[^/]+/!/!; |
26 |
return $path; |
27 |
} |
28 |
|
29 |
my $json = { |
30 |
properties => { |
31 |
revision => { |
32 |
valueType => "number", |
33 |
}, |
34 |
'google-code-svn-link' => { |
35 |
valueType => "url" |
36 |
} |
37 |
}, |
38 |
items => [], |
39 |
}; |
40 |
|
41 |
my $log; |
42 |
{ |
43 |
open( my $log_fh, '-|', "svn log --xml --stop-on-copy -v $repository" ) || die "can't read svn log: $!"; |
44 |
local $/ = undef; |
45 |
$log = <$log_fh>; |
46 |
close( $log_fh ); |
47 |
} |
48 |
|
49 |
my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); |
50 |
|
51 |
warn dump( $xml ) if $debug; |
52 |
|
53 |
foreach my $e (@{$xml->{'logentry'}}) { |
54 |
|
55 |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
56 |
my $path = delete( $p->{'content'} ); |
57 |
my $label = $e->{'revision'} . ' - ' . path_strip( $path ); |
58 |
my $item = { |
59 |
type => 'Item', |
60 |
id => $label, |
61 |
label => $label, |
62 |
uri => $svnweb . $e->{'revision'}, |
63 |
path_full => $path, |
64 |
path => path_strip( $path ), |
65 |
}; |
66 |
|
67 |
$item->{$_} = $p->{$_} foreach grep { ! defined $item->{$_} } keys %$p; |
68 |
$item->{$_} = $e->{$_} foreach grep { ! defined $item->{$_} && ! /^(paths)$/ } keys %$e; |
69 |
|
70 |
warn dump( $item ) if $debug; |
71 |
|
72 |
push @{ $json->{items} }, $item; |
73 |
|
74 |
$max_items--; |
75 |
# we skip all other items in commits with more than 10 files as they are too chatty |
76 |
last if $#{ $e->{'paths'}->{'path'} } > 10; |
77 |
# last; # FIXME record just single item for each commit! |
78 |
# we also break out when $max_items is reached to keep browser (somewhat) alive |
79 |
last if ! $max_items; |
80 |
} |
81 |
|
82 |
last if ! $max_items; |
83 |
} |
84 |
|
85 |
print qq|Content-type: application/json\n\r\n\r|, encode_json( $json ); |
86 |
|