--- trunk/svn2cvs.pl 2005/03/06 12:09:42 18 +++ trunk/svn2cvs.pl 2007/08/16 15:27:09 27 @@ -13,9 +13,13 @@ use strict; use File::Temp qw/ tempdir /; +use File::Path; use Data::Dumper; use XML::Simple; +# do we want to sync just part of repository? +my $partial_import = 1; + if (@ARGV < 2) { print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n"; exit 1; @@ -28,13 +32,27 @@ exit 1; } +# Ensure File::Temp::END can clean up: +$SIG{__DIE__} = sub { chdir("/tmp"); die @_ }; + my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); -chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; +sub cd_tmp { + chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; +} + +sub cd_rep { + chdir("$TMPDIR/$CVSREP") || die "can't cd to $TMPDIR/$CVSREP: $!"; +} + +print "## using TMPDIR $TMPDIR\n"; # cvs command with root my $cvs="cvs -d $CVSROOT"; +# current revision in CVS +my $rev; + # # sub to do logging and system calls # @@ -68,11 +86,26 @@ } } -# current revision in CVS -my $rev; +sub add_dir($$) { + my ($path,$msg) = @_; + print "# add_dir($path)\n"; + die "add_dir($path) is not directory" unless (-d $path); + + my $curr_dir; + + foreach my $d (split(m#/#, $path)) { + $curr_dir .= ( $curr_dir ? '/' : '') . $d; + + next if in_entries($curr_dir); + next if (-e "$curr_dir/CVS"); + + log_system("$cvs add $curr_dir", "cvs add of $curr_dir failed"); + } +} # ok, now do the checkout eval { + cd_tmp; log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); }; @@ -88,9 +121,9 @@ print "start import of new module [yes]: "; my $in = ; + cd_tmp; mkdir($CVSREP) || die "can't create $CVSREP: $!"; - - chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; + cd_rep; open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!"; print SVNREV "0"; @@ -99,24 +132,17 @@ $rev = 0; # create new module - log_system("$cvs import -m 'new CVS module' $CVSREP svn2cvs r0", "can't import new module into $CVSREP"); - - unlink ".svnrev" || die "can't remove .svnrev: $!"; - chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; - rmdir $CVSREP || die "can't remove $CVSREP: $!"; - - # and checkout it + cd_rep; + log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository"); + cd_tmp; + rmtree($CVSREP) || die "can't remove $CVSREP"; log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); - - chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; + cd_rep; } else { - # import into existing module directory in CVS - chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; - - + cd_rep; # check if svnrev exists if (! -e ".svnrev") { print <<_USAGE_; @@ -173,7 +199,11 @@ } close(LOG); -my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); + +my $xml; +eval { + $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); +}; #=begin log_example @@ -202,7 +232,7 @@ if ($d !~ m,/$, && $d ne "") { $d .= "/"; } - open(E, $d."CVS/Entries") || die "can't open ${d}CVS/Entries: $!"; + open(E, $d."CVS/Entries") || return 0; while() { return(1) if (m,^/$f/,); } @@ -211,6 +241,9 @@ } } +cd_tmp; +cd_rep; + foreach my $e (@{$xml->{'logentry'}}) { die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'}); $rev = $e->{'revision'}; @@ -221,8 +254,11 @@ my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!"; my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!"; do { - if ($tmpsvn =~ s,(/\w+)/*$,,) { - $SVNREP .= $1; + if ($tmpsvn =~ s#(/[^/]+)/*$##) { + $SVNREP = $1 . $SVNREP; + } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) { + print "NOTICE: copyfrom outside synced repository ignored - skipping\n"; + next; } else { print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n"; next; @@ -237,11 +273,17 @@ foreach my $p (@{$e->{'paths'}->{'path'}}) { my ($action,$path) = ($p->{'action'},$p->{'content'}); + next if ($path =~ m#/\.svnrev$#); + print "svn2cvs: $action $path\n"; # prepare path and message my $file = $path; - $path =~ s,^$SVNREP/*,, || die "BUG: can't strip SVNREP '$SVNREP' from path"; + if ( $path !~ s#^\Q$SVNREP\E/*## ) { + print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n"; + die unless $partial_import; + next; + } if (! $path) { print "NOTICE: skipped this operation. Probably trunk creation\n"; @@ -255,32 +297,28 @@ print "svn2cvs: modify $path -- nop\n"; } elsif ($action =~ /A/) { if (-d $path) { - chdir($path) || die "can't cd into dir $path for import: $!"; - log_system("$cvs import -d -m '$msg' $CVSREP/$path svn r$rev", "cvs import of $path failed"); - if (-d "$CVSREP/$path") { - rmdir "$CVSREP/$path" || die "can't remove $CVSREP/$path: $!"; - } else { - unlink "$CVSREP/$path" || die "can't remove $CVSREP/$path: $!"; - } - chdir("$TMPDIR") || die "can't cd to $TMPDIR/$CVSREP: $!"; - log_system("$cvs checkout $CVSREP/$path", "cvs checkout of imported dir $path failed"); - chdir("$TMPDIR/$CVSREP") || die "can't cd back to $TMPDIR/$CVSREP: $!"; + add_dir($path, $msg); } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") { my $dir = $1; - in_entries($dir) || log_system("$cvs add $dir", "cvs add of dir $dir failed"); + in_entries($dir) || add_dir($dir, $msg); in_entries($path) || log_system("$cvs add $path", "cvs add of $path failed"); } else { in_entries($path) || log_system("$cvs add $path", "cvs add of $path failed"); } } elsif ($action =~ /D/) { - unlink $path || die "can't delete $path: $!"; - log_system("$cvs delete $path", "cvs delete of $path failed"); + if (-e $path) { + unlink $path || die "can't delete $path: $!"; + log_system("$cvs delete $path", "cvs delete of $path failed"); + } else { + print "WARNING: $path is not present, skipping...\n"; + undef $path; + } } else { print "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n"; } # save commits for later - push @commit, $path; + push @commit, $path if ($path); } @@ -293,6 +331,9 @@ commit_svnrev($rev); } +# cd out of $CVSREP before File::Temp::END is called +chdir("/tmp") || die "can't cd to /tmp: $!"; + __END__ =pod @@ -397,13 +438,28 @@ messages, and support for skipping changes which are not under current Subversion checkout root (e.g. branches). -=item r16 +=item r18 Support for importing your svn into empty CVS repository (it will first create module and than dump all revisions). Group commit operations to save round-trips to CVS server. Documentation improvements and other small fixes. +=item r20 + +Fixed path deduction (overlap between Subversion reporistory and CVS checkout). + +=item r21 + +Use C instead of checkout after import. +Added fixes by Paul Egan for XMLin and fixing working +directory. + +=item r22 + +Rewritten import from revision 0 to empty repository, better importing +of deep directory structures, initial support for recovery from partial +commit. =back