--- trunk/svn2cvs.pl 2007/08/20 08:37:22 30 +++ trunk/svn2cvs.pl 2007/09/22 13:57:28 42 @@ -20,14 +20,17 @@ # do we want to sync just part of repository? my $partial_import = 1; -if (@ARGV < 2) { +# do we want to add svk-like prefix with original revision, author and date? +my $decorate_commit_message = 1; + +if ( @ARGV < 2 ) { print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n"; exit 1; } -my ($SVNROOT,$CVSROOT, $CVSREP) = @ARGV; +my ( $SVNROOT, $CVSROOT, $CVSREP ) = @ARGV; -if ($SVNROOT !~ m,^[\w+]+:///*\w+,) { +if ( $SVNROOT !~ m,^[\w+]+:///*\w+, ) { print "ERROR: invalid svn root $SVNROOT\n"; exit 1; } @@ -35,7 +38,7 @@ # Ensure File::Temp::END can clean up: $SIG{__DIE__} = sub { chdir("/tmp"); die @_ }; -my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); +my $TMPDIR = tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 ); sub cd_tmp { chdir($TMPDIR) || die "can't cd to $TMPDIR: $!"; @@ -48,7 +51,7 @@ print "## using TMPDIR $TMPDIR\n"; # cvs command with root -my $cvs="cvs -f -d $CVSROOT"; +my $cvs = "cvs -f -d $CVSROOT"; # current revision in CVS my $rev; @@ -57,7 +60,7 @@ # sub to do logging and system calls # sub log_system($$) { - my ($cmd,$errmsg) = @_; + my ( $cmd, $errmsg ) = @_; print STDERR "## $cmd\n"; system($cmd) == 0 || die "$errmsg: $!"; } @@ -66,47 +69,49 @@ # sub to commit .svn rev file later # sub commit_svnrev { - my $rev = shift @_; + my $rev = shift @_; my $add_new = shift @_; - die "commit_svnrev needs revision" if (! defined($rev)); + die "commit_svnrev needs revision" if ( !defined($rev) ); - open(SVNREV,"> .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"; + my $path = ".svnrev"; if ($add_new) { system "$cvs add '$path'" || die "cvs add of $path failed: $!"; } else { - my $msg="subversion revision $rev commited to CVS"; + my $msg = "subversion revision $rev commited to CVS"; print "$msg\n"; - system "$cvs commit -m '$msg' '$path'" || die "cvs commit of $path failed: $!"; + system "$cvs commit -m '$msg' '$path'" + || die "cvs commit of $path failed: $!"; } } sub add_dir($$) { - my ($path,$msg) = @_; + my ( $path, $msg ) = @_; print "# add_dir($path)\n"; - die "add_dir($path) is not directory" unless (-d $path); + die "add_dir($path) is not directory" unless ( -d $path ); my $curr_dir; - foreach my $d (split(m#/#, $path)) { - $curr_dir .= ( $curr_dir ? '/' : '') . $d; + foreach my $d ( split( m#/#, $path ) ) { + $curr_dir .= ( $curr_dir ? '/' : '' ) . $d; next if in_entries($curr_dir); - next if (-e "$curr_dir/CVS"); + next if ( -e "$curr_dir/CVS" ); - log_system("$cvs add '$curr_dir'", "cvs add of $curr_dir failed"); + 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"); + log_system( "$cvs -q checkout $CVSREP", "cvs checkout failed" ); }; if ($@) { @@ -125,7 +130,7 @@ mkdir($CVSREP) || die "can't create $CVSREP: $!"; cd_rep; - open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!"; + open( SVNREV, "> .svnrev" ) || die "can't open $CVSREP/.svnrev: $!"; print SVNREV "0"; close(SVNREV); @@ -133,18 +138,21 @@ # create new module cd_rep; - log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository"); + 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"); + log_system( "$cvs -q checkout $CVSREP", "cvs checkout failed" ); cd_rep; } else { + # import into existing module directory in CVS cd_rep; + # check if svnrev exists - if (! -e ".svnrev") { + if ( !-e ".svnrev" ) { print <<_USAGE_; Your CVS repository doesn't have .svnrev file! @@ -166,15 +174,16 @@ print "svn revision corresponding to CVS [abort]: "; my $in = ; chomp($in); - if ($in !~ /^\d+$/) { + if ( $in !~ /^\d+$/ ) { print "Aborting: revision not a number\n"; exit 1; } else { $rev = $in; - commit_svnrev($rev,1); # create new + commit_svnrev( $rev, 1 ); # create new } } else { - open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; + open( SVNREV, ".svnrev" ) + || die "can't open $TMPDIR/$CVSREP/.svnrev: $!"; $rev = ; chomp($rev); close(SVNREV); @@ -184,7 +193,6 @@ $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 @@ -192,19 +200,16 @@ # case much about accuracy and completnes of logs there, this might # be good. YMMV # -open(LOG, "svn log -r $rev:HEAD -v --xml $SVNROOT |") || die "svn log for repository $SVNROOT failed: $!"; +open( LOG, "svn log -r $rev:HEAD -v --xml $SVNROOT |" ) + || die "svn log for repository $SVNROOT failed: $!"; my $log; -while() { +while () { $log .= $_; } close(LOG); - my $xml; -eval { - $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]); -}; - +eval { $xml = XMLin( $log, ForceArray => [ 'logentry', 'path' ] ); }; #=begin log_example # @@ -217,109 +222,195 @@ my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n"; -if (! $xml->{'logentry'}) { +if ( !$xml->{'logentry'} ) { print "no newer log entries in Subversion repostory. CVS is current\n"; exit 0; } +# return all files in CVS/Entries +sub entries($) { + my $dir = shift; + die "entries expects directory argument!" unless -d $dir; + my @entries; + open( my $fh, "./$dir/CVS/Entries" ) || return 0; + while (<$fh>) { + if ( m{^D/([^/]+)}, ) { + my $sub_dir = $1; + warn "#### entries recurse into: $dir/$sub_dir"; + push @entries, map {"$sub_dir/$_"} entries("$dir/$sub_dir"); + push @entries, $sub_dir; + } elsif (m{^/([^/]+)/}) { + push @entries, $1; + } elsif ( !m{^D$} ) { + die "can't decode entries line: $_"; + } + } + close($fh); + warn "#### entries($dir) => ", join( "|", @entries ); + return @entries; +} + # check if file exists in CVS/Entries sub in_entries($) { my $path = shift; - if ($path !~ m,^(.*?/*)([^/]+)$,) { - die "can't split '$path' to dir and file!"; - } else { - my ($d,$f) = ($1,$2); - if ($d !~ m,/$, && $d ne "") { - $d .= "/"; + if ( $path =~ m,^(.*?/*)([^/]+)$, ) { + my ( $dir, $file ) = ( $1, $2 ); + if ( $dir !~ m,/$, && $dir ne "" ) { + $dir .= "/"; } - open(E, $d."CVS/Entries") || return 0; - while() { - return(1) if (m,^/$f/,); + + open( my $fh, "./$dir/CVS/Entries" ) + || return 0; #die "no entries file: $dir/CVS/Entries"; + while (<$fh>) { + return 1 if (m{^/$file/}); } - close(E); + close($fh); return 0; + } else { + die "can't split '$path' to dir and file!"; } } 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'}); +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 $TMPDIR/$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!"; + my $SVNREP = ""; + my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!"; + my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} + || die "BUG: tmppath empty!"; do { - if ($tmpsvn =~ s#(/[^/]+)/*$##) { + if ( $tmpsvn =~ s#(/[^/]+)/*$## ) { # vim fix $SVNREP = $1 . $SVNREP; - } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) { - print "NOTICE: copyfrom outside synced repository ignored - skipping\n"; + } 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; } - } until ($tmppath =~ m/^$SVNREP/); + } until ( $tmppath =~ m/^$SVNREP/ ); print "NOTICE: using $SVNREP as directory for svn\n"; - printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'}); + printf( $fmt, + $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'} ); my @commit; - foreach my $p (@{$e->{'paths'}->{'path'}}) { - my ($action,$path) = ($p->{'action'},$p->{'content'}); + my $msg = $e->{'msg'}; + $msg =~ s/'/'\\''/g; # quote " + + $msg = 'r' . $rev . ' ' . $e->{author} . ' | ' . $e->{date} . "\n" . $msg + if $decorate_commit_message; + + sub cvs_commit { + my $msg = shift || die "no msg?"; + if ( !@_ ) { + warn "commit ignored, no files\n"; + return; + } + log_system( + "$cvs commit -m '$msg' '" . join( "' '", @_ ) . "'", + "cvs commit of " . join( ",", @_ ) . " failed" + ); + } - next if ($path =~ m#/\.svnrev$#); + 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; if ( $path !~ s#^\Q$SVNREP\E/*## ) { - print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n"; + print + "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n"; die unless $partial_import; next; } - if (! $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/) { - if ( in_entries( $path ) ) { - print "svn2cvs: modify $path -- nop\n"; + sub add_path { + my $path = shift || die "no path?"; + + if ( -d $path ) { + add_dir( $path, $msg ); + } elsif ( $path =~ m,^(.+)/[^/]+$, && !-e "$1/CVS/Root" ) { + my $dir = $1; + in_entries($dir) || add_dir( $dir, $msg ); + in_entries($path) || log_system( "$cvs add '$path'", + "cvs add of $path failed" ); } else { - print "WARNING: modify $path which isn't in CVS, adding...\n"; - log_system("$cvs add '$path'", "cvs add of $path failed"); + in_entries($path) || log_system( "$cvs add '$path'", + "cvs add of $path failed" ); } - } elsif ($action =~ /A/) { - if (-d $path) { - add_dir($path, $msg); - } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") { - my $dir = $1; - in_entries($dir) || add_dir($dir, $msg); - in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed"); + } + + if ( $action =~ /M/ ) { + if ( in_entries($path) ) { + print "svn2cvs: modify $path -- nop\n"; } else { - in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed"); + print "WARNING: modify $path which isn't in CVS, adding...\n"; + add_path($path); } - } elsif ($action =~ /D/) { - if (-e $path) { - unlink $path || die "can't delete $path: $!"; - log_system("$cvs delete '$path'", "cvs delete of $path failed"); + } elsif ( $action =~ /A/ ) { + add_path($path); + } elsif ( $action =~ /D/ ) { + if ( -e $path ) { + if ( -d $path ) { + warn "#### remove directory: $path"; + my @sub_commit; + foreach my $f ( entries($path) ) { + $f = "$path/$f"; + if ( -f $f ) { + unlink($f) || die "can't delete file $f: $!"; + + # } else { + # rmtree($f) || die "can't delete dir $f: $!"; + } + log_system( "$cvs delete '$f'", + "cvs delete of file $f failed" ); + push @sub_commit, $f; + } + log_system( "$cvs delete '$path'", + "cvs delete of file $path failed" ); + cvs_commit( $msg, @sub_commit, $path ); + log_system( + "$cvs update -dP '$path'", + "cvs update -dP $path failed" + ); + undef $path; + } else { + warn "#### remove file: $path"; + unlink($path) || die "can't delete $path: $!"; + log_system( "$cvs delete '$path'", + "cvs delete of dir $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"; + print + "WARNING: action $action not implemented on $path. Bug or missing feature of $0\n"; } # save commits for later @@ -327,11 +418,8 @@ } - my $msg = $e->{'msg'}; - $msg =~ s/'/'\\''/g; # quote " - # now commit changes - log_system("$cvs commit -m '$msg' '".join("' '",@commit)."'", "cvs commit of ".join(",",@commit)." failed"); + cvs_commit( $msg, @commit ); commit_svnrev($rev); }