/[svn2cvs]/trunk/svn2cvs.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/svn2cvs.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (hide annotations)
Sat Sep 22 13:52:30 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 14107 byte(s)
pass source through tidy target
1 dpavlin 1 #!/usr/bin/perl -w
2    
3     # This script will transfer changes from Subversion repository
4     # to CVS repository (e.g. SourceForge) while preserving commit
5     # logs.
6     #
7     # Based on original shell version by Tollef Fog Heen available at
8     # http://raw.no/personal/blog
9     #
10     # 2004-03-09 Dobrica Pavlinusic <dpavlin@rot13.org>
11 dpavlin 12 #
12     # documentation is after __END__
13 dpavlin 1
14     use strict;
15     use File::Temp qw/ tempdir /;
16 dpavlin 21 use File::Path;
17 dpavlin 1 use Data::Dumper;
18     use XML::Simple;
19    
20 dpavlin 27 # do we want to sync just part of repository?
21     my $partial_import = 1;
22    
23 dpavlin 36 # do we want to add svk-like prefix with original revision, author and date?
24     my $decorate_commit_message = 1;
25    
26 dpavlin 41 if ( @ARGV < 2 ) {
27 dpavlin 8 print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n";
28     exit 1;
29     }
30 dpavlin 1
31 dpavlin 41 my ( $SVNROOT, $CVSROOT, $CVSREP ) = @ARGV;
32 dpavlin 1
33 dpavlin 41 if ( $SVNROOT !~ m,^[\w+]+:///*\w+, ) {
34 dpavlin 8 print "ERROR: invalid svn root $SVNROOT\n";
35     exit 1;
36     }
37 dpavlin 7
38 dpavlin 21 # Ensure File::Temp::END can clean up:
39     $SIG{__DIE__} = sub { chdir("/tmp"); die @_ };
40    
41 dpavlin 41 my $TMPDIR = tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 );
42 dpavlin 1
43 dpavlin 22 sub cd_tmp {
44     chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";
45     }
46 dpavlin 1
47 dpavlin 22 sub cd_rep {
48     chdir("$TMPDIR/$CVSREP") || die "can't cd to $TMPDIR/$CVSREP: $!";
49     }
50    
51     print "## using TMPDIR $TMPDIR\n";
52    
53 dpavlin 1 # cvs command with root
54 dpavlin 41 my $cvs = "cvs -f -d $CVSROOT";
55 dpavlin 1
56 dpavlin 22 # current revision in CVS
57     my $rev;
58    
59 dpavlin 1 #
60     # sub to do logging and system calls
61     #
62     sub log_system($$) {
63 dpavlin 41 my ( $cmd, $errmsg ) = @_;
64 dpavlin 1 print STDERR "## $cmd\n";
65 dpavlin 8 system($cmd) == 0 || die "$errmsg: $!";
66 dpavlin 1 }
67    
68 dpavlin 3 #
69     # sub to commit .svn rev file later
70     #
71     sub commit_svnrev {
72 dpavlin 41 my $rev = shift @_;
73 dpavlin 3 my $add_new = shift @_;
74 dpavlin 1
75 dpavlin 41 die "commit_svnrev needs revision" if ( !defined($rev) );
76 dpavlin 3
77 dpavlin 41 open( SVNREV, "> .svnrev" )
78     || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
79 dpavlin 3 print SVNREV $rev;
80     close(SVNREV);
81    
82 dpavlin 41 my $path = ".svnrev";
83 dpavlin 3
84     if ($add_new) {
85 dpavlin 28 system "$cvs add '$path'" || die "cvs add of $path failed: $!";
86 dpavlin 41 }
87     else {
88     my $msg = "subversion revision $rev commited to CVS";
89 dpavlin 4 print "$msg\n";
90 dpavlin 41 system "$cvs commit -m '$msg' '$path'"
91     || die "cvs commit of $path failed: $!";
92 dpavlin 3 }
93     }
94    
95 dpavlin 22 sub add_dir($$) {
96 dpavlin 41 my ( $path, $msg ) = @_;
97 dpavlin 22 print "# add_dir($path)\n";
98 dpavlin 41 die "add_dir($path) is not directory" unless ( -d $path );
99 dpavlin 18
100 dpavlin 22 my $curr_dir;
101    
102 dpavlin 41 foreach my $d ( split( m#/#, $path ) ) {
103     $curr_dir .= ( $curr_dir ? '/' : '' ) . $d;
104 dpavlin 22
105     next if in_entries($curr_dir);
106 dpavlin 41 next if ( -e "$curr_dir/CVS" );
107 dpavlin 22
108 dpavlin 41 log_system( "$cvs add '$curr_dir'", "cvs add of $curr_dir failed" );
109 dpavlin 22 }
110     }
111    
112 dpavlin 1 # ok, now do the checkout
113 dpavlin 18 eval {
114 dpavlin 22 cd_tmp;
115 dpavlin 41 log_system( "$cvs -q checkout $CVSREP", "cvs checkout failed" );
116 dpavlin 18 };
117 dpavlin 1
118 dpavlin 18 if ($@) {
119     print <<_NEW_REP_;
120 dpavlin 1
121 dpavlin 18 There is no CVS repository '$CVSREP' in your CVS. I will assume that
122     this is import of new module in your CVS and start from revision 0.
123 dpavlin 7
124 dpavlin 18 Press enter to continue importing new CVS repository or CTRL+C to abort.
125 dpavlin 7
126 dpavlin 18 _NEW_REP_
127 dpavlin 3
128 dpavlin 18 print "start import of new module [yes]: ";
129     my $in = <STDIN>;
130 dpavlin 22 cd_tmp;
131 dpavlin 18 mkdir($CVSREP) || die "can't create $CVSREP: $!";
132 dpavlin 22 cd_rep;
133 dpavlin 1
134 dpavlin 41 open( SVNREV, "> .svnrev" ) || die "can't open $CVSREP/.svnrev: $!";
135 dpavlin 18 print SVNREV "0";
136     close(SVNREV);
137    
138     $rev = 0;
139    
140     # create new module
141 dpavlin 22 cd_rep;
142 dpavlin 41 log_system( "$cvs import -d -m 'new CVS module' $CVSREP svn r$rev",
143     "import of new repository" );
144 dpavlin 22 cd_tmp;
145     rmtree($CVSREP) || die "can't remove $CVSREP";
146 dpavlin 41 log_system( "$cvs -q checkout $CVSREP", "cvs checkout failed" );
147 dpavlin 22 cd_rep;
148 dpavlin 18
149 dpavlin 41 }
150     else {
151    
152 dpavlin 18 # import into existing module directory in CVS
153    
154 dpavlin 22 cd_rep;
155 dpavlin 41
156 dpavlin 18 # check if svnrev exists
157 dpavlin 41 if ( !-e ".svnrev" ) {
158 dpavlin 18 print <<_USAGE_;
159    
160 dpavlin 3 Your CVS repository doesn't have .svnrev file!
161 dpavlin 1
162 dpavlin 12 This file is used to keep CVS repository and Subversion in sync, so
163 dpavlin 3 that only newer changes will be commited.
164 dpavlin 1
165 dpavlin 3 It's quote possible that this is first svn2cvs run for this repository.
166     If so, you will have to identify correct svn revision which
167     corresponds to current version of CVS repository that has just
168     been checkouted.
169 dpavlin 1
170 dpavlin 3 If you migrated your cvs repository to svn using cvs2svn, this will be
171 dpavlin 12 last Subversion revision. If this is initial run of conversion of
172     Subversion repository to CVS, correct revision is 0.
173 dpavlin 1
174     _USAGE_
175 dpavlin 3
176 dpavlin 18 print "svn revision corresponding to CVS [abort]: ";
177     my $in = <STDIN>;
178     chomp($in);
179 dpavlin 41 if ( $in !~ /^\d+$/ ) {
180 dpavlin 18 print "Aborting: revision not a number\n";
181     exit 1;
182 dpavlin 41 }
183     else {
184 dpavlin 18 $rev = $in;
185 dpavlin 41 commit_svnrev( $rev, 1 ); # create new
186 dpavlin 18 }
187 dpavlin 41 }
188     else {
189     open( SVNREV, ".svnrev" )
190     || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
191 dpavlin 18 $rev = <SVNREV>;
192     chomp($rev);
193     close(SVNREV);
194 dpavlin 3 }
195 dpavlin 18
196     print "Starting after revision $rev\n";
197     $rev++;
198 dpavlin 1 }
199    
200     #
201     # FIXME!! HEAD should really be next verison and loop because this way we
202     # loose multiple edits of same file and corresponding messages. On the
203     # other hand, if you want to compress your traffic to CVS server and don't
204     # case much about accuracy and completnes of logs there, this might
205     # be good. YMMV
206     #
207 dpavlin 41 open( LOG, "svn log -r $rev:HEAD -v --xml $SVNROOT |" )
208     || die "svn log for repository $SVNROOT failed: $!";
209 dpavlin 1 my $log;
210 dpavlin 41 while (<LOG>) {
211 dpavlin 1 $log .= $_;
212     }
213     close(LOG);
214    
215 dpavlin 21 my $xml;
216 dpavlin 41 eval { $xml = XMLin( $log, ForceArray => [ 'logentry', 'path' ] ); };
217 dpavlin 1
218 dpavlin 12 #=begin log_example
219     #
220     #------------------------------------------------------------------------
221     #r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines
222     #
223     #ported r254 from hidra branch
224     #
225     #=cut
226 dpavlin 1
227     my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n";
228    
229 dpavlin 41 if ( !$xml->{'logentry'} ) {
230 dpavlin 12 print "no newer log entries in Subversion repostory. CVS is current\n";
231 dpavlin 2 exit 0;
232     }
233    
234 dpavlin 35 # return all files in CVS/Entries
235     sub entries($) {
236     my $dir = shift;
237     die "entries expects directory argument!" unless -d $dir;
238     my @entries;
239 dpavlin 41 open( my $fh, "./$dir/CVS/Entries" ) || return 0;
240     while (<$fh>) {
241 dpavlin 35 if ( m{^D/([^/]+)}, ) {
242     my $sub_dir = $1;
243     warn "#### entries recurse into: $dir/$sub_dir";
244 dpavlin 41 push @entries, map {"$sub_dir/$_"} entries("$dir/$sub_dir");
245 dpavlin 35 push @entries, $sub_dir;
246 dpavlin 41 }
247     elsif (m{^/([^/]+)/}) {
248 dpavlin 35 push @entries, $1;
249 dpavlin 41 }
250     elsif ( !m{^D$} ) {
251 dpavlin 35 die "can't decode entries line: $_";
252 dpavlin 14 }
253     }
254 dpavlin 35 close($fh);
255 dpavlin 41 warn "#### entries($dir) => ", join( "|", @entries );
256 dpavlin 35 return @entries;
257 dpavlin 14 }
258    
259 dpavlin 35 # check if file exists in CVS/Entries
260     sub in_entries($) {
261     my $path = shift;
262 dpavlin 41 if ( $path =~ m,^(.*?/*)([^/]+)$, ) {
263     my ( $dir, $file ) = ( $1, $2 );
264     if ( $dir !~ m,/$, && $dir ne "" ) {
265 dpavlin 37 $dir .= "/";
266     }
267    
268 dpavlin 41 open( my $fh, "./$dir/CVS/Entries" )
269     || return 0; #die "no entries file: $dir/CVS/Entries";
270     while (<$fh>) {
271 dpavlin 37 return 1 if (m{^/$file/});
272     }
273     close($fh);
274     return 0;
275 dpavlin 41 }
276     else {
277 dpavlin 37 die "can't split '$path' to dir and file!";
278 dpavlin 35 }
279     }
280    
281 dpavlin 22 cd_tmp;
282     cd_rep;
283 dpavlin 21
284 dpavlin 41 foreach my $e ( @{ $xml->{'logentry'} } ) {
285     die "BUG: revision from .svnrev ($rev) greater than from subversion ("
286     . $e->{'revision'} . ")"
287     if ( $rev > $e->{'revision'} );
288 dpavlin 1 $rev = $e->{'revision'};
289 dpavlin 41 log_system( "svn export --force -q -r $rev $SVNROOT $TMPDIR/$CVSREP",
290     "svn export of revision $rev failed" );
291 dpavlin 1
292 dpavlin 8 # deduce name of svn directory
293 dpavlin 41 my $SVNREP = "";
294     my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";
295     my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'}
296     || die "BUG: tmppath empty!";
297 dpavlin 8 do {
298 dpavlin 41 if ( $tmpsvn =~ s#(/[^/]+)/*$## ) { # vim fix
299 dpavlin 20 $SVNREP = $1 . $SVNREP;
300 dpavlin 41 }
301     elsif ( $e->{'paths'}->{'path'}->[0]->{'copyfrom-path'} ) {
302     print
303     "NOTICE: copyfrom outside synced repository ignored - skipping\n";
304 dpavlin 26 next;
305 dpavlin 41 }
306     else {
307 dpavlin 15 print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";
308     next;
309 dpavlin 8 }
310 dpavlin 41 } until ( $tmppath =~ m/^$SVNREP/ );
311 dpavlin 8
312     print "NOTICE: using $SVNREP as directory for svn\n";
313    
314 dpavlin 41 printf( $fmt,
315     $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'} );
316 dpavlin 18 my @commit;
317    
318 dpavlin 35 my $msg = $e->{'msg'};
319 dpavlin 41 $msg =~ s/'/'\\''/g; # quote "
320 dpavlin 35
321 dpavlin 41 $msg = 'r' . $rev . ' ' . $e->{author} . ' | ' . $e->{date} . "\n" . $msg
322     if $decorate_commit_message;
323 dpavlin 36
324 dpavlin 35 sub cvs_commit {
325     my $msg = shift || die "no msg?";
326 dpavlin 41 if ( !@_ ) {
327 dpavlin 35 warn "commit ignored, no files\n";
328     return;
329     }
330 dpavlin 41 log_system(
331     "$cvs commit -m '$msg' '" . join( "' '", @_ ) . "'",
332     "cvs commit of " . join( ",", @_ ) . " failed"
333     );
334 dpavlin 35 }
335    
336 dpavlin 41 foreach my $p ( @{ $e->{'paths'}->{'path'} } ) {
337     my ( $action, $path ) = ( $p->{'action'}, $p->{'content'} );
338 dpavlin 1
339 dpavlin 41 next if ( $path =~ m#/\.svnrev$# );
340 dpavlin 23
341 dpavlin 1 print "svn2cvs: $action $path\n";
342    
343     # prepare path and message
344     my $file = $path;
345 dpavlin 27 if ( $path !~ s#^\Q$SVNREP\E/*## ) {
346 dpavlin 41 print
347     "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n";
348 dpavlin 27 die unless $partial_import;
349     next;
350     }
351 dpavlin 7
352 dpavlin 41 if ( !$path ) {
353 dpavlin 7 print "NOTICE: skipped this operation. Probably trunk creation\n";
354     next;
355     }
356    
357 dpavlin 1 my $msg = $e->{'msg'};
358 dpavlin 41 $msg =~ s/'/'\\''/g; # quote "
359 dpavlin 1
360 dpavlin 31 sub add_path {
361     my $path = shift || die "no path?";
362 dpavlin 41
363     if ( -d $path ) {
364     add_dir( $path, $msg );
365     }
366     elsif ( $path =~ m,^(.+)/[^/]+$, && !-e "$1/CVS/Root" ) {
367 dpavlin 14 my $dir = $1;
368 dpavlin 41 in_entries($dir) || add_dir( $dir, $msg );
369     in_entries($path) || log_system( "$cvs add '$path'",
370     "cvs add of $path failed" );
371 dpavlin 8 }
372 dpavlin 41 else {
373     in_entries($path) || log_system( "$cvs add '$path'",
374     "cvs add of $path failed" );
375     }
376 dpavlin 31 }
377    
378 dpavlin 41 if ( $action =~ /M/ ) {
379     if ( in_entries($path) ) {
380 dpavlin 31 print "svn2cvs: modify $path -- nop\n";
381 dpavlin 41 }
382     else {
383 dpavlin 31 print "WARNING: modify $path which isn't in CVS, adding...\n";
384     add_path($path);
385     }
386 dpavlin 41 }
387     elsif ( $action =~ /A/ ) {
388 dpavlin 31 add_path($path);
389 dpavlin 41 }
390     elsif ( $action =~ /D/ ) {
391     if ( -e $path ) {
392 dpavlin 35 if ( -d $path ) {
393     warn "#### remove directory: $path";
394     my @sub_commit;
395     foreach my $f ( entries($path) ) {
396     $f = "$path/$f";
397 dpavlin 41 if ( -f $f ) {
398 dpavlin 35 unlink($f) || die "can't delete file $f: $!";
399 dpavlin 41
400     # } else {
401     # rmtree($f) || die "can't delete dir $f: $!";
402 dpavlin 35 }
403 dpavlin 41 log_system( "$cvs delete '$f'",
404     "cvs delete of file $f failed" );
405 dpavlin 35 push @sub_commit, $f;
406     }
407 dpavlin 41 log_system( "$cvs delete '$path'",
408     "cvs delete of file $path failed" );
409     cvs_commit( $msg, @sub_commit, $path );
410     log_system(
411     "$cvs update -dP '$path'",
412     "cvs update -dP $path failed"
413     );
414 dpavlin 40 undef $path;
415 dpavlin 41 }
416     else {
417 dpavlin 35 warn "#### remove file: $path";
418     unlink($path) || die "can't delete $path: $!";
419 dpavlin 41 log_system( "$cvs delete '$path'",
420     "cvs delete of dir $path failed" );
421 dpavlin 35 }
422 dpavlin 41 }
423     else {
424 dpavlin 22 print "WARNING: $path is not present, skipping...\n";
425     undef $path;
426     }
427 dpavlin 1 }
428 dpavlin 41 else {
429     print
430     "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n";
431     }
432 dpavlin 1
433 dpavlin 18 # save commits for later
434 dpavlin 22 push @commit, $path if ($path);
435 dpavlin 1
436     }
437    
438 dpavlin 18 # now commit changes
439 dpavlin 41 cvs_commit( $msg, @commit );
440 dpavlin 18
441 dpavlin 1 commit_svnrev($rev);
442     }
443 dpavlin 12
444 dpavlin 21 # cd out of $CVSREP before File::Temp::END is called
445     chdir("/tmp") || die "can't cd to /tmp: $!";
446    
447 dpavlin 12 __END__
448    
449     =pod
450    
451     =head1 NAME
452    
453     svn2cvs - save subversion commits to (read-only) cvs repository
454    
455     =head1 SYNOPSIS
456    
457     ./svn2cvs.pl SVN_URL CVSROOT CVSREPOSITORY
458    
459     Usage example (used to self-host this script):
460    
461     ./svn2cvs.pl file:///home/dpavlin/private/svn/svn2cvs/trunk/ \
462     :pserver:dpavlin@cvs.tigris.org:/cvs svn2cvs/src
463    
464     =head1 DESCRIPTION
465    
466 dpavlin 18 This script will allows you to commit changes made to Subversion repository to
467     (read-only) CVS repository manually or from Subversion's C<post-commit> hook.
468 dpavlin 12
469     It's using F<.svnrev> file (which will be created on first run) in
470     B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was
471     committed into CVS.
472    
473     One run will do following things:
474    
475     =over 4
476    
477     =item *
478     checkout B<CVSREPOSITORY> from B<CVSROOT> to temporary directory
479    
480     =item *
481     check if F<.svnrev> file exists and create it if it doesn't
482    
483     =item *
484     loop through all revisions from current in B<CVSROOT/CVSREPOSITORY> (using
485     F<.svnrev>) up to B<HEAD> (current one)
486    
487     =over 5
488    
489     =item *
490     checkout next Subversion revision from B<SVN_URL> over CVS checkout
491     temporary directory
492    
493     =item *
494     make modification (add and/or delete) done in that revision
495    
496     =item *
497     commit modification (added, deleted or modified files/dirs) while
498     preserving original message from CVS
499    
500     =item *
501     update F<.svnrev> to match current revision
502    
503     =back
504    
505     =item *
506     cleanup temporary directory
507    
508     =back
509    
510     If checkout fails for some reason (e.g. flaky ssh connection), you will
511     still have valid CVS repository, so all you have to do is run B<svn2cvs.pl>
512     again.
513    
514     =head1 WARNINGS
515    
516     "Cheap" copy operations in Subversion are not at all cheap in CVS. They will
517     create multiple copies of files in CVS repository!
518    
519 dpavlin 18 This script assume that you want to sync your C<trunk> (or any other
520     directory for that matter) directory with CVS, not root of your subversion.
521     This might be considered bug, but since common practise is to have
522     directories C<trunk> and C<branches> in svn and source code in them, it's
523     not serious limitation.
524    
525 dpavlin 12 =head1 RELATED PROJECTS
526    
527     B<Subversion> L<http://subversion.tigris.org/> version control system that is a
528     compelling replacement for CVS in the open source community.
529    
530     B<cvs2svn> L<http://cvs2svn.tigris.org/> converts a CVS repository to a
531     Subversion repository. It is designed for one-time conversions, not for
532     repeated synchronizations between CVS and Subversion.
533    
534 dpavlin 16 =head1 CHANGES
535    
536     Versions of this utility are actually Subversion repository revisions,
537     so they might not be in sequence.
538    
539     =over 3
540    
541     =item r10
542    
543     First release available to public
544    
545     =item r15
546    
547     Addition of comprehensive documentation, fixes for quoting in commit
548     messages, and support for skipping changes which are not under current
549     Subversion checkout root (e.g. branches).
550    
551 dpavlin 19 =item r18
552 dpavlin 18
553     Support for importing your svn into empty CVS repository (it will first
554     create module and than dump all revisions).
555     Group commit operations to save round-trips to CVS server.
556     Documentation improvements and other small fixes.
557    
558 dpavlin 20 =item r20
559 dpavlin 18
560 dpavlin 20 Fixed path deduction (overlap between Subversion reporistory and CVS checkout).
561    
562 dpavlin 21 =item r21
563    
564     Use C<update -d> instead of checkout after import.
565     Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working
566     directory.
567    
568 dpavlin 22 =item r22
569 dpavlin 21
570 dpavlin 22 Rewritten import from revision 0 to empty repository, better importing
571     of deep directory structures, initial support for recovery from partial
572     commit.
573    
574 dpavlin 16 =back
575    
576 dpavlin 12 =head1 AUTHOR
577    
578     Dobrica Pavlinusic <dpavlin@rot13.org>
579    
580     L<http://www.rot13.org/~dpavlin/>
581    
582     =head1 LICENSE
583    
584     This product is licensed under GNU Public License (GPL) v2 or later.
585    
586     =cut
587    

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26