--- trunk/svn2cvs.pl 2004/03/09 22:16:19 6 +++ trunk/svn2cvs.pl 2004/03/13 15:44:04 11 @@ -14,25 +14,17 @@ use Data::Dumper; use XML::Simple; -# get current user home directory -my $HOME = $ENV{'HOME'} || die "can't get home directory!"; +if (@ARGV < 2) { + print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n"; + exit 1; +} + +my ($SVNROOT,$CVSROOT, $CVSREP) = @ARGV; -# cvsroot directory -#my $CVSROOT="$HOME/x/cvsroot"; -my $CVSROOT=':pserver:dpavlin@cvs.tigris.org:/cvs'; -# name of cvs repository to commit to -my $CVSREP="svn2cvs/src"; - -# svnroot directory -my $SVNROOT="file://$HOME/private/svn/svn2cvs"; -# name of respository -my $SVNREP="trunk"; - -# webpac example -#$CVSROOT="$HOME/x/cvsroot"; -#$CVSREP="webpac"; -#$SVNROOT="file://$HOME/private/svn/webpac/"; -#$SVNREP="trunk"; +if ($SVNROOT !~ m,^[\w+]+:///*\w+,) { + print "ERROR: invalid svn root $SVNROOT\n"; + exit 1; +} my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); @@ -41,14 +33,13 @@ # cvs command with root my $cvs="cvs -d $CVSROOT"; - # # sub to do logging and system calls # sub log_system($$) { my ($cmd,$errmsg) = @_; print STDERR "## $cmd\n"; - system $cmd || die "$errmsg: $!"; + system($cmd) == 0 || die "$errmsg: $!"; } # @@ -60,30 +51,32 @@ die "commit_svnrev needs revision" if (! defined($rev)); - open(SVNREV,"> $CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; + open(SVNREV,"> .svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; print SVNREV $rev; close(SVNREV); my $path=".svnrev"; if ($add_new) { - system "$cvs add $CVSREP/$path" || die "cvs add of $path failed: $!"; + system "$cvs add $path" || die "cvs add of $path failed: $!"; } else { my $msg="subversion revision $rev commited to CVS"; print "$msg\n"; - system "$cvs commit -m \"$msg\" $CVSREP/$path" || die "cvs commit of $path failed: $!"; + system "$cvs commit -m '$msg' $path" || die "cvs commit of $path failed: $!"; } } - # ok, now do the checkout -log_system("$cvs -q checkout $CVSREP","cvs checkout failed"); +log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); + +chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; + my $rev; # check if svnrev exists -if (! -e "$CVSREP/.svnrev") { +if (! -e ".svnrev") { print <<_USAGE_; Your CVS repository doesn't have .svnrev file! @@ -113,7 +106,7 @@ commit_svnrev($rev,1); # create new } } else { - open(SVNREV,"$CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; + open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; $rev = ; chomp($rev); close(SVNREV); @@ -130,7 +123,7 @@ # case much about accuracy and completnes of logs there, this might # be good. YMMV # -open(LOG, "svn log -r $rev:HEAD -v --xml $SVNROOT/$SVNREP |") || die "svn log for repository $SVNROOT/$SVNREP failed: $!"; +open(LOG, "svn log -r $rev:HEAD -v --xml $SVNROOT |") || die "svn log for repository $SVNROOT failed: $!"; my $log; while() { $log .= $_; @@ -159,7 +152,22 @@ foreach my $e (@{$xml->{'logentry'}}) { die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'}); $rev = $e->{'revision'}; - log_system("svn export --force -q -r $rev $SVNROOT/$SVNREP $CVSREP", "svn export of revision $rev failed"); + log_system("svn export --force -q -r $rev $SVNROOT $TMPDIR/$CVSREP", "svn export of revision $rev failed"); + + # deduce name of svn directory + my $SVNREP = ""; + my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!"; + my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!"; + do { +print "## tmppath: $tmppath tmpsvn: $tmpsvn SVNREP: $SVNREP\n"; + if ($tmpsvn =~ s,(/\w+/*)$,,) { + $SVNREP .= $1; + } else { + die "ERROR: can't deduce svn dir from $SVNROOT.\nUsing root of snv repository for current version instead of /trunk/ is not supported.\n"; + } + } until ($tmppath =~ m/^$SVNREP/); + + print "NOTICE: using $SVNREP as directory for svn\n"; printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'}); foreach my $p (@{$e->{'paths'}->{'path'}}) { @@ -169,44 +177,38 @@ # prepare path and message my $file = $path; - $path =~ s,^/$SVNREP/*,, || die "BUG: can't strip SVNREP from path"; + $path =~ s,^$SVNREP/*,, || die "BUG: can't strip SVNREP from path"; + + if (! $path) { + print "NOTICE: skipped this operation. Probably trunk creation\n"; + next; + } + my $msg = $e->{'msg'}; - $msg =~ s/"/\\"/g; # quote " + $msg =~ s/'/\\'/g; # quote " if ($action =~ /M/) { print "svn2cvs: modify $path -- nop\n"; } elsif ($action =~ /A/) { - log_system("$cvs add -m \"$msg\" $CVSREP/$path", "cvs add of $path failed"); + 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"); + 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: $!"; + } else { + log_system("$cvs add -m '$msg' $path", "cvs add of $path failed"); + } } elsif ($action =~ /D/) { - log_system("$cvs delete -m \"$msg\" $CVSREP/$path", "cvs delete of $path failed"); + log_system("$cvs delete -m '$msg' $path", "cvs delete of $path failed"); } else { print "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n"; } # now commit changes - log_system("$cvs commit -m \"$msg\" $CVSREP/$path", "cvs commit of $path failed"); + log_system("$cvs commit -m '$msg' $path", "cvs commit of $path failed"); } commit_svnrev($rev); } - -__END__ - -svn export --force "$SVNROOT/$SVNREP" "$CVSREP" - -cd dotfiles - -for file in $(find -type f -not -path \*CVS\*); do - FILE=$(basename $file) - DIR=$(dirname $file) - if ! grep -q "^/$FILE/" $DIR/CVS/Entries ; then - cvs add $file - fi -done - -#cvs commit -m "Automatic commit from SVN" - -#rm -rf $TMPDIR - -echo "cvs left in $TMPDIR"