--- trunk/svn2cvs.pl 2004/03/09 21:02:42 2 +++ trunk/svn2cvs.pl 2004/03/11 16:26:49 8 @@ -14,18 +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"; -# name of cvs repository to commit to -my $CVSREP="webpac"; - -# svnroot directory -my $SVNROOT="file://$HOME/private/svn/webpac/"; -# name of respository -my $SVNREP="trunk"; +if ($SVNROOT !~ m,^\w+:///*\w+,) { + print "ERROR: invalid svn root $SVNROOT\n"; + exit 1; +} my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); @@ -40,64 +39,83 @@ sub log_system($$) { my ($cmd,$errmsg) = @_; print STDERR "## $cmd\n"; - system $cmd || die "$errmsg: $!"; + system($cmd) == 0 || die "$errmsg: $!"; } +# +# sub to commit .svn rev file later +# +sub commit_svnrev { + my $rev = shift @_; + my $add_new = shift @_; -# ok, now do the checkout + die "commit_svnrev needs revision" if (! defined($rev)); -log_system("$cvs checkout $CVSREP","cvs checkout failed"); + open(SVNREV,"> .svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; + print SVNREV $rev; + close(SVNREV); -# check if svnrev exists + my $path=".svnrev"; -if (! -e "$CVSREP/.svnrev") { - print <<_USAGE_; + if ($add_new) { + 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\" $path" || die "cvs commit of $path failed: $!"; + } +} - Your CVS repository doesn't have $TMPDIR/$CVSREP/.svnrev file! +# ok, now do the checkout - This file is used to commit changes to CVS repository with correct - logs from Subversion. +log_system("$cvs -q checkout $CVSREP", "cvs checkout failed"); - Please create .svnrec file with currect svn revision which - corresponds to version of checkouted tree and commit that file - to CVS repository, e.g. - - echo 233 > .svnrev - cvs add .svnrev - cvs commit -m "subversion repository revision sync file" .svnrev +chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!"; -_USAGE_ - print "CVS Repository left in $TMPDIR/$CVSREP\n"; - exit 1; -} +my $rev; -open(SVNREV,"$CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; -my $rev = ; -chomp($rev); -close(SVNREV); +# check if svnrev exists +if (! -e ".svnrev") { + print <<_USAGE_; -print "Starting after revision $rev\n"; -$rev++; +Your CVS repository doesn't have .svnrev file! +This file is used to keep CVS repository and SubVersion in sync, so +that only newer changes will be commited. -# -# sub to commit .svn rev file later -# +It's quote possible that this is first svn2cvs run for this repository. +If so, you will have to identify correct svn revision which +corresponds to current version of CVS repository that has just +been checkouted. + +If you migrated your cvs repository to svn using cvs2svn, this will be +last SubVersion revision. If this is initial run of conversion of +SubVersion repository to CVS, correct revision is 0. -sub commit_svnrev($) { - my $rev = shift @_ || die "commit_svnrev needs revision"; +_USAGE_ - open(SVNREV,"> $CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; - print SVNREV $rev; + print "svn revision corresponding to CVS [abort]: "; + my $in = ; + chomp($in); + if ($in !~ /^\d+$/) { + print "Aborting: revision not a number\n"; + exit 1; + } else { + $rev = $in; + commit_svnrev($rev,1); # create new + } +} else { + open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; + $rev = ; + chomp($rev); close(SVNREV); - - my $msg="subversion revision $rev commited to CVS"; - my $path=".svnrev"; - print "$msg\n"; - system "$cvs commit -m \"$msg\" $CVSREP/$path" || die "cvs commit of $path failed: $!"; } +print "Starting after revision $rev\n"; +$rev++; + + # # FIXME!! HEAD should really be next verison and loop because this way we # loose multiple edits of same file and corresponding messages. On the @@ -105,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 .= $_; @@ -131,12 +149,25 @@ exit 0; } -print Dumper($xml); - 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'}}) { @@ -146,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 " 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"