8 |
# http://raw.no/personal/blog |
# http://raw.no/personal/blog |
9 |
# |
# |
10 |
# 2004-03-09 Dobrica Pavlinusic <dpavlin@rot13.org> |
# 2004-03-09 Dobrica Pavlinusic <dpavlin@rot13.org> |
11 |
|
# |
12 |
|
# documentation is after __END__ |
13 |
|
|
14 |
use strict; |
use strict; |
15 |
use File::Temp qw/ tempdir /; |
use File::Temp qw/ tempdir /; |
16 |
|
use File::Path; |
17 |
use Data::Dumper; |
use Data::Dumper; |
18 |
use XML::Simple; |
use XML::Simple; |
19 |
|
|
20 |
|
# do we want to sync just part of repository? |
21 |
|
my $partial_import = 1; |
22 |
|
|
23 |
if (@ARGV < 2) { |
if (@ARGV < 2) { |
24 |
print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n"; |
print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n"; |
25 |
exit 1; |
exit 1; |
32 |
exit 1; |
exit 1; |
33 |
} |
} |
34 |
|
|
35 |
|
# Ensure File::Temp::END can clean up: |
36 |
|
$SIG{__DIE__} = sub { chdir("/tmp"); die @_ }; |
37 |
|
|
38 |
my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); |
my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); |
39 |
|
|
40 |
chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; |
sub cd_tmp { |
41 |
|
chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; |
42 |
|
} |
43 |
|
|
44 |
|
sub cd_rep { |
45 |
|
chdir("$TMPDIR/$CVSREP") || die "can't cd to $TMPDIR/$CVSREP: $!"; |
46 |
|
} |
47 |
|
|
48 |
|
print "## using TMPDIR $TMPDIR\n"; |
49 |
|
|
50 |
# cvs command with root |
# cvs command with root |
51 |
my $cvs="cvs -d $CVSROOT"; |
my $cvs="cvs -f -d $CVSROOT"; |
52 |
|
|
53 |
|
# current revision in CVS |
54 |
|
my $rev; |
55 |
|
|
56 |
# |
# |
57 |
# sub to do logging and system calls |
# sub to do logging and system calls |
78 |
my $path=".svnrev"; |
my $path=".svnrev"; |
79 |
|
|
80 |
if ($add_new) { |
if ($add_new) { |
81 |
system "$cvs add $path" || die "cvs add of $path failed: $!"; |
system "$cvs add '$path'" || die "cvs add of $path failed: $!"; |
82 |
} else { |
} else { |
83 |
my $msg="subversion revision $rev commited to CVS"; |
my $msg="subversion revision $rev commited to CVS"; |
84 |
print "$msg\n"; |
print "$msg\n"; |
85 |
system "$cvs commit -m \"$msg\" $path" || die "cvs commit of $path failed: $!"; |
system "$cvs commit -m '$msg' '$path'" || die "cvs commit of $path failed: $!"; |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
sub add_dir($$) { |
90 |
|
my ($path,$msg) = @_; |
91 |
|
print "# add_dir($path)\n"; |
92 |
|
die "add_dir($path) is not directory" unless (-d $path); |
93 |
|
|
94 |
|
my $curr_dir; |
95 |
|
|
96 |
|
foreach my $d (split(m#/#, $path)) { |
97 |
|
$curr_dir .= ( $curr_dir ? '/' : '') . $d; |
98 |
|
|
99 |
|
next if in_entries($curr_dir); |
100 |
|
next if (-e "$curr_dir/CVS"); |
101 |
|
|
102 |
|
log_system("$cvs add '$curr_dir'", "cvs add of $curr_dir failed"); |
103 |
} |
} |
104 |
} |
} |
105 |
|
|
106 |
# ok, now do the checkout |
# ok, now do the checkout |
107 |
|
eval { |
108 |
|
cd_tmp; |
109 |
|
log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); |
110 |
|
}; |
111 |
|
|
112 |
log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); |
if ($@) { |
113 |
|
print <<_NEW_REP_; |
114 |
|
|
115 |
chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; |
There is no CVS repository '$CVSREP' in your CVS. I will assume that |
116 |
|
this is import of new module in your CVS and start from revision 0. |
117 |
|
|
118 |
|
Press enter to continue importing new CVS repository or CTRL+C to abort. |
119 |
|
|
120 |
my $rev; |
_NEW_REP_ |
121 |
|
|
122 |
|
print "start import of new module [yes]: "; |
123 |
|
my $in = <STDIN>; |
124 |
|
cd_tmp; |
125 |
|
mkdir($CVSREP) || die "can't create $CVSREP: $!"; |
126 |
|
cd_rep; |
127 |
|
|
128 |
|
open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!"; |
129 |
|
print SVNREV "0"; |
130 |
|
close(SVNREV); |
131 |
|
|
132 |
# check if svnrev exists |
$rev = 0; |
133 |
if (! -e ".svnrev") { |
|
134 |
print <<_USAGE_; |
# create new module |
135 |
|
cd_rep; |
136 |
|
log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository"); |
137 |
|
cd_tmp; |
138 |
|
rmtree($CVSREP) || die "can't remove $CVSREP"; |
139 |
|
log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); |
140 |
|
cd_rep; |
141 |
|
|
142 |
|
} else { |
143 |
|
# import into existing module directory in CVS |
144 |
|
|
145 |
|
cd_rep; |
146 |
|
# check if svnrev exists |
147 |
|
if (! -e ".svnrev") { |
148 |
|
print <<_USAGE_; |
149 |
|
|
150 |
Your CVS repository doesn't have .svnrev file! |
Your CVS repository doesn't have .svnrev file! |
151 |
|
|
152 |
This file is used to keep CVS repository and SubVersion in sync, so |
This file is used to keep CVS repository and Subversion in sync, so |
153 |
that only newer changes will be commited. |
that only newer changes will be commited. |
154 |
|
|
155 |
It's quote possible that this is first svn2cvs run for this repository. |
It's quote possible that this is first svn2cvs run for this repository. |
158 |
been checkouted. |
been checkouted. |
159 |
|
|
160 |
If you migrated your cvs repository to svn using cvs2svn, this will be |
If you migrated your cvs repository to svn using cvs2svn, this will be |
161 |
last SubVersion revision. If this is initial run of conversion of |
last Subversion revision. If this is initial run of conversion of |
162 |
SubVersion repository to CVS, correct revision is 0. |
Subversion repository to CVS, correct revision is 0. |
163 |
|
|
164 |
_USAGE_ |
_USAGE_ |
165 |
|
|
166 |
print "svn revision corresponding to CVS [abort]: "; |
print "svn revision corresponding to CVS [abort]: "; |
167 |
my $in = <STDIN>; |
my $in = <STDIN>; |
168 |
chomp($in); |
chomp($in); |
169 |
if ($in !~ /^\d+$/) { |
if ($in !~ /^\d+$/) { |
170 |
print "Aborting: revision not a number\n"; |
print "Aborting: revision not a number\n"; |
171 |
exit 1; |
exit 1; |
172 |
|
} else { |
173 |
|
$rev = $in; |
174 |
|
commit_svnrev($rev,1); # create new |
175 |
|
} |
176 |
} else { |
} else { |
177 |
$rev = $in; |
open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; |
178 |
commit_svnrev($rev,1); # create new |
$rev = <SVNREV>; |
179 |
|
chomp($rev); |
180 |
|
close(SVNREV); |
181 |
} |
} |
|
} else { |
|
|
open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; |
|
|
$rev = <SVNREV>; |
|
|
chomp($rev); |
|
|
close(SVNREV); |
|
|
} |
|
182 |
|
|
183 |
print "Starting after revision $rev\n"; |
print "Starting after revision $rev\n"; |
184 |
$rev++; |
$rev++; |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
# |
# |
199 |
} |
} |
200 |
close(LOG); |
close(LOG); |
201 |
|
|
|
my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); |
|
202 |
|
|
203 |
|
my $xml; |
204 |
|
eval { |
205 |
|
$xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); |
206 |
|
}; |
207 |
|
|
|
=begin log_example |
|
208 |
|
|
209 |
------------------------------------------------------------------------ |
#=begin log_example |
210 |
r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines |
# |
211 |
|
#------------------------------------------------------------------------ |
212 |
ported r254 from hidra branch |
#r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines |
213 |
|
# |
214 |
=cut |
#ported r254 from hidra branch |
215 |
|
# |
216 |
|
#=cut |
217 |
|
|
218 |
my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n"; |
my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n"; |
219 |
|
|
220 |
if (! $xml->{'logentry'}) { |
if (! $xml->{'logentry'}) { |
221 |
print "no newer log entries in SubVersion repostory. CVS is current\n"; |
print "no newer log entries in Subversion repostory. CVS is current\n"; |
222 |
exit 0; |
exit 0; |
223 |
} |
} |
224 |
|
|
225 |
|
# check if file exists in CVS/Entries |
226 |
|
sub in_entries($) { |
227 |
|
my $path = shift; |
228 |
|
if ($path !~ m,^(.*?/*)([^/]+)$,) { |
229 |
|
die "can't split '$path' to dir and file!"; |
230 |
|
} else { |
231 |
|
my ($d,$f) = ($1,$2); |
232 |
|
if ($d !~ m,/$, && $d ne "") { |
233 |
|
$d .= "/"; |
234 |
|
} |
235 |
|
open(E, $d."CVS/Entries") || return 0; |
236 |
|
while(<E>) { |
237 |
|
return(1) if (m,^/$f/,); |
238 |
|
} |
239 |
|
close(E); |
240 |
|
return 0; |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
cd_tmp; |
245 |
|
cd_rep; |
246 |
|
|
247 |
foreach my $e (@{$xml->{'logentry'}}) { |
foreach my $e (@{$xml->{'logentry'}}) { |
248 |
die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'}); |
die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'}); |
249 |
$rev = $e->{'revision'}; |
$rev = $e->{'revision'}; |
254 |
my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!"; |
my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!"; |
255 |
my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!"; |
my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!"; |
256 |
do { |
do { |
257 |
print "## tmppath: $tmppath tmpsvn: $tmpsvn SVNREP: $SVNREP\n"; |
if ($tmpsvn =~ s#(/[^/]+)/*$##) { |
258 |
if ($tmpsvn =~ s,(/\w+/*)$,,) { |
$SVNREP = $1 . $SVNREP; |
259 |
$SVNREP .= $1; |
} elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) { |
260 |
|
print "NOTICE: copyfrom outside synced repository ignored - skipping\n"; |
261 |
|
next; |
262 |
} else { |
} else { |
263 |
die "ERROR: can't deduce svn dir from $SVNROOT.\nUsing root of snv repository for current version instead of /trunk/ is not supported.\n"; |
print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n"; |
264 |
|
next; |
265 |
} |
} |
266 |
} until ($tmppath =~ m/^$SVNREP/); |
} until ($tmppath =~ m/^$SVNREP/); |
267 |
|
|
268 |
print "NOTICE: using $SVNREP as directory for svn\n"; |
print "NOTICE: using $SVNREP as directory for svn\n"; |
269 |
|
|
270 |
printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'}); |
printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'}); |
271 |
|
my @commit; |
272 |
|
|
273 |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
foreach my $p (@{$e->{'paths'}->{'path'}}) { |
274 |
my ($action,$path) = ($p->{'action'},$p->{'content'}); |
my ($action,$path) = ($p->{'action'},$p->{'content'}); |
275 |
|
|
276 |
|
next if ($path =~ m#/\.svnrev$#); |
277 |
|
|
278 |
print "svn2cvs: $action $path\n"; |
print "svn2cvs: $action $path\n"; |
279 |
|
|
280 |
# prepare path and message |
# prepare path and message |
281 |
my $file = $path; |
my $file = $path; |
282 |
$path =~ s,^$SVNREP/*,, || die "BUG: can't strip SVNREP from path"; |
if ( $path !~ s#^\Q$SVNREP\E/*## ) { |
283 |
|
print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n"; |
284 |
|
die unless $partial_import; |
285 |
|
next; |
286 |
|
} |
287 |
|
|
288 |
if (! $path) { |
if (! $path) { |
289 |
print "NOTICE: skipped this operation. Probably trunk creation\n"; |
print "NOTICE: skipped this operation. Probably trunk creation\n"; |
291 |
} |
} |
292 |
|
|
293 |
my $msg = $e->{'msg'}; |
my $msg = $e->{'msg'}; |
294 |
$msg =~ s/"/\\"/g; # quote " |
$msg =~ s/'/'\\''/g; # quote " |
295 |
|
|
296 |
if ($action =~ /M/) { |
if ($action =~ /M/) { |
297 |
print "svn2cvs: modify $path -- nop\n"; |
print "svn2cvs: modify $path -- nop\n"; |
298 |
} elsif ($action =~ /A/) { |
} elsif ($action =~ /A/) { |
299 |
if (-d $path) { |
if (-d $path) { |
300 |
chdir($path) || die "can't cd into dir $path for import: $!"; |
add_dir($path, $msg); |
301 |
log_system("$cvs import -d -m \"$msg\" $CVSREP/$path svn r$rev", "cvs import of $path failed"); |
} elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") { |
302 |
chdir("$TMPDIR") || die "can't cd to $TMPDIR/$CVSREP: $!"; |
my $dir = $1; |
303 |
log_system("$cvs checkout $CVSREP/$path", "cvs checkout of imported dir $path failed"); |
in_entries($dir) || add_dir($dir, $msg); |
304 |
chdir("$TMPDIR/$CVSREP") || die "can't cd back to $TMPDIR/$CVSREP: $!"; |
in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed"); |
305 |
} else { |
} else { |
306 |
log_system("$cvs add -m \"$msg\" $path", "cvs add of $path failed"); |
in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed"); |
307 |
} |
} |
308 |
} elsif ($action =~ /D/) { |
} elsif ($action =~ /D/) { |
309 |
log_system("$cvs delete -m \"$msg\" $path", "cvs delete of $path failed"); |
if (-e $path) { |
310 |
|
unlink $path || die "can't delete $path: $!"; |
311 |
|
log_system("$cvs delete '$path'", "cvs delete of $path failed"); |
312 |
|
} else { |
313 |
|
print "WARNING: $path is not present, skipping...\n"; |
314 |
|
undef $path; |
315 |
|
} |
316 |
} else { |
} else { |
317 |
print "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n"; |
print "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n"; |
318 |
} |
} |
319 |
|
|
320 |
# now commit changes |
# save commits for later |
321 |
log_system("$cvs commit -m \"$msg\" $path", "cvs commit of $path failed"); |
push @commit, $path if ($path); |
322 |
|
|
323 |
} |
} |
324 |
|
|
325 |
|
my $msg = $e->{'msg'}; |
326 |
|
$msg =~ s/'/'\\''/g; # quote " |
327 |
|
|
328 |
|
# now commit changes |
329 |
|
log_system("$cvs commit -m '$msg' '".join("' '",@commit)."'", "cvs commit of ".join(",",@commit)." failed"); |
330 |
|
|
331 |
commit_svnrev($rev); |
commit_svnrev($rev); |
332 |
} |
} |
333 |
|
|
334 |
|
# cd out of $CVSREP before File::Temp::END is called |
335 |
|
chdir("/tmp") || die "can't cd to /tmp: $!"; |
336 |
|
|
337 |
|
__END__ |
338 |
|
|
339 |
|
=pod |
340 |
|
|
341 |
|
=head1 NAME |
342 |
|
|
343 |
|
svn2cvs - save subversion commits to (read-only) cvs repository |
344 |
|
|
345 |
|
=head1 SYNOPSIS |
346 |
|
|
347 |
|
./svn2cvs.pl SVN_URL CVSROOT CVSREPOSITORY |
348 |
|
|
349 |
|
Usage example (used to self-host this script): |
350 |
|
|
351 |
|
./svn2cvs.pl file:///home/dpavlin/private/svn/svn2cvs/trunk/ \ |
352 |
|
:pserver:dpavlin@cvs.tigris.org:/cvs svn2cvs/src |
353 |
|
|
354 |
|
=head1 DESCRIPTION |
355 |
|
|
356 |
|
This script will allows you to commit changes made to Subversion repository to |
357 |
|
(read-only) CVS repository manually or from Subversion's C<post-commit> hook. |
358 |
|
|
359 |
|
It's using F<.svnrev> file (which will be created on first run) in |
360 |
|
B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was |
361 |
|
committed into CVS. |
362 |
|
|
363 |
|
One run will do following things: |
364 |
|
|
365 |
|
=over 4 |
366 |
|
|
367 |
|
=item * |
368 |
|
checkout B<CVSREPOSITORY> from B<CVSROOT> to temporary directory |
369 |
|
|
370 |
|
=item * |
371 |
|
check if F<.svnrev> file exists and create it if it doesn't |
372 |
|
|
373 |
|
=item * |
374 |
|
loop through all revisions from current in B<CVSROOT/CVSREPOSITORY> (using |
375 |
|
F<.svnrev>) up to B<HEAD> (current one) |
376 |
|
|
377 |
|
=over 5 |
378 |
|
|
379 |
|
=item * |
380 |
|
checkout next Subversion revision from B<SVN_URL> over CVS checkout |
381 |
|
temporary directory |
382 |
|
|
383 |
|
=item * |
384 |
|
make modification (add and/or delete) done in that revision |
385 |
|
|
386 |
|
=item * |
387 |
|
commit modification (added, deleted or modified files/dirs) while |
388 |
|
preserving original message from CVS |
389 |
|
|
390 |
|
=item * |
391 |
|
update F<.svnrev> to match current revision |
392 |
|
|
393 |
|
=back |
394 |
|
|
395 |
|
=item * |
396 |
|
cleanup temporary directory |
397 |
|
|
398 |
|
=back |
399 |
|
|
400 |
|
If checkout fails for some reason (e.g. flaky ssh connection), you will |
401 |
|
still have valid CVS repository, so all you have to do is run B<svn2cvs.pl> |
402 |
|
again. |
403 |
|
|
404 |
|
=head1 WARNINGS |
405 |
|
|
406 |
|
"Cheap" copy operations in Subversion are not at all cheap in CVS. They will |
407 |
|
create multiple copies of files in CVS repository! |
408 |
|
|
409 |
|
This script assume that you want to sync your C<trunk> (or any other |
410 |
|
directory for that matter) directory with CVS, not root of your subversion. |
411 |
|
This might be considered bug, but since common practise is to have |
412 |
|
directories C<trunk> and C<branches> in svn and source code in them, it's |
413 |
|
not serious limitation. |
414 |
|
|
415 |
|
=head1 RELATED PROJECTS |
416 |
|
|
417 |
|
B<Subversion> L<http://subversion.tigris.org/> version control system that is a |
418 |
|
compelling replacement for CVS in the open source community. |
419 |
|
|
420 |
|
B<cvs2svn> L<http://cvs2svn.tigris.org/> converts a CVS repository to a |
421 |
|
Subversion repository. It is designed for one-time conversions, not for |
422 |
|
repeated synchronizations between CVS and Subversion. |
423 |
|
|
424 |
|
=head1 CHANGES |
425 |
|
|
426 |
|
Versions of this utility are actually Subversion repository revisions, |
427 |
|
so they might not be in sequence. |
428 |
|
|
429 |
|
=over 3 |
430 |
|
|
431 |
|
=item r10 |
432 |
|
|
433 |
|
First release available to public |
434 |
|
|
435 |
|
=item r15 |
436 |
|
|
437 |
|
Addition of comprehensive documentation, fixes for quoting in commit |
438 |
|
messages, and support for skipping changes which are not under current |
439 |
|
Subversion checkout root (e.g. branches). |
440 |
|
|
441 |
|
=item r18 |
442 |
|
|
443 |
|
Support for importing your svn into empty CVS repository (it will first |
444 |
|
create module and than dump all revisions). |
445 |
|
Group commit operations to save round-trips to CVS server. |
446 |
|
Documentation improvements and other small fixes. |
447 |
|
|
448 |
|
=item r20 |
449 |
|
|
450 |
|
Fixed path deduction (overlap between Subversion reporistory and CVS checkout). |
451 |
|
|
452 |
|
=item r21 |
453 |
|
|
454 |
|
Use C<update -d> instead of checkout after import. |
455 |
|
Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working |
456 |
|
directory. |
457 |
|
|
458 |
|
=item r22 |
459 |
|
|
460 |
|
Rewritten import from revision 0 to empty repository, better importing |
461 |
|
of deep directory structures, initial support for recovery from partial |
462 |
|
commit. |
463 |
|
|
464 |
|
=back |
465 |
|
|
466 |
|
=head1 AUTHOR |
467 |
|
|
468 |
|
Dobrica Pavlinusic <dpavlin@rot13.org> |
469 |
|
|
470 |
|
L<http://www.rot13.org/~dpavlin/> |
471 |
|
|
472 |
|
=head1 LICENSE |
473 |
|
|
474 |
|
This product is licensed under GNU Public License (GPL) v2 or later. |
475 |
|
|
476 |
|
=cut |
477 |
|
|