-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsvn-grab
executable file
·156 lines (127 loc) · 3.81 KB
/
svn-grab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl
# Main Source: http://www.cirt.net/svnpristine
# Main Source: https://github.com/tautology0/ayfabtu/
# changes: v1.1: Soroush Dalili (@irsdl) - Feb. 2014 - Windows compatibility and better interface!
use LWP::UserAgent;
use DBI;
use File::Temp qw/tempfile tempdir/;
# Read roots
sub readroots
{
my ($dbh) = @_;
my @ROOTS;
my $roots = $dbh->prepare("select id,local_abspath from WCROOT");
$roots->execute;
while (my @i = $roots->fetchrow_array())
{
push(@ROOTS,{'id'=>$i[0], 'path'=>$i[1]});
}
return @ROOTS;
}
# Read nodes
sub readnodes
{
my ($dbh) = @_;
my @NODES;
my $nodes = $dbh->prepare("select wc_id, local_relpath, checksum, kind from NODES");
$nodes->execute;
while (my @i = $nodes->fetchrow_array())
{
push(@NODES,{'id'=>$i[0], 'path'=>$i[1], 'sum'=>$i[2], 'kind'=>$i[3]});
}
return @NODES;
}
$File::Temp::KEEP_ALL=0;
my @ROOTS;;
my @NODES;
# grab the database file
my $target=$ARGV[0];
my $iswindows=($^O=~/Win/)?1:0; # Are we running on windows?
my $windowsInvalidChars = '[\<\>\:\"\|\?\*]'; # to make it compatible with Windows
my $windowsInvalidWords = '^(CON|PRN|AUX|NUL|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9)(\.|$)'; # to make it compatible with Windows
my $illegalWords = '([\.]{2,}|(^[\/]+))'; # to block simple dir traversal
if ($iswindows){
print "Windows File System Compatible = true \n";
}else{
print "Windows File System Compatible = false \n";
}
my $svnurl="http://$ARGV[0]/.svn/wc.db";
my $ua=LWP::UserAgent->new;
$ua->agent("SVNScanner/1.1");
print "Downloading the database...\n";
my $request=HTTP::Request->new(GET => $svnurl);
my $result=$ua->request($request);
if ($result->status_line !~ /^200 .*/) {
die "Could not get the svn database from $svnurl";
}else{
print "svn database has been detected and downloaded from $svnurl \n";
my $quit = 0;
until ($quit) {
print "Do you want to download the content? \n";
print "Enter Y [Default] to continue OR enter anything else to exit: ";
chomp(my $input = <STDIN>);
if ($input =~ /^[Y]?$/i) { # Match Yy or blank
print "Downloading the content...\n";
$quit = 1;
}else{
die "Exit";
}
}
}
my ($dbfileh, $dbfilen) = tempfile();
print "$dbfilen\n";
print $dbfileh $result->content;
close $dbfileh;
# open database
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfilen","","");
@ROOTS=readroots($dbh);
@NODES=readnodes($dbh);
# Now to read it all from pristine
# create output dir
my $server=$ARGV[0];
mkdir $server;
foreach my $node (@NODES)
{
if ($node->{'kind'} eq "dir")
{
my $destDir = "$server/$node->{'local_relpath'}";
$destDir =~ s/${illegalWords}/_/ig;
if ($iswindows == 1){
$destDir =~ s/${windowsInvalidChars}/_/ig;
$destDir =~ s/${windowsInvalidWords}/_/ig;
}
mkdir "$destDir";
}
else
{
my $checksum=substr($node->{'sum'}, 6);
my $twochars=substr($checksum, 0, 2);
my $svnurl="http://$server/.svn/pristine/$twochars/$checksum.svn-base";
my $fua=LWP::UserAgent->new;
$fua->agent("SVNScanner/1.1");
my @brokenup=split(/\//,$node->{'path'});
my $pathstr="$server/";
for (my $i=0;$i < $#brokenup; $i++)
{
$pathstr="$pathstr$brokenup[$i]/";
$pathstr =~ s/${illegalWords}/_/ig;
if ($iswindows == 1){
$pathstr =~ s/${windowsInvalidChars}/_/ig;
$pathstr =~ s/${windowsInvalidWords}/_/ig;
}
mkdir "$pathstr";
}
my $frequest=HTTP::Request->new(GET => $svnurl);
my $fresult=$fua->request($frequest);
my $filepath = "$server/$node->{'path'}";
$filepath =~ s/${illegalWords}/_/ig;
if ($iswindows == 1){
$filepath =~ s/${windowsInvalidChars}/_/ig;
$filepath =~ s/${windowsInvalidWords}/_/ig;
}
print "grabbing $server/$node->{'path'} from $svnurl -- will be saved in $filepath \n";
open $fh,">$filepath";
print $fh $fresult->content;
close $fh;
}
}