--- trunk/svn2cvs.pl 2006/01/03 00:03:22 23 +++ trunk/svn2cvs.pl 2007/09/07 16:39:42 35 @@ -17,6 +17,9 @@ 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; @@ -45,7 +48,7 @@ print "## using TMPDIR $TMPDIR\n"; # cvs command with root -my $cvs="cvs -d $CVSROOT"; +my $cvs="cvs -f -d $CVSROOT"; # current revision in CVS my $rev; @@ -75,11 +78,11 @@ my $path=".svnrev"; if ($add_new) { - system "$cvs add $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' $path" || die "cvs commit of $path failed: $!"; + system "$cvs commit -m '$msg' '$path'" || die "cvs commit of $path failed: $!"; } } @@ -96,7 +99,7 @@ next if in_entries($curr_dir); 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"); } } @@ -219,8 +222,8 @@ exit 0; } -# check if file exists in CVS/Entries -sub in_entries($) { +# my ($dir,$file) = dir_file($path); +sub dir_file($) { my $path = shift; if ($path !~ m,^(.*?/*)([^/]+)$,) { die "can't split '$path' to dir and file!"; @@ -229,13 +232,43 @@ if ($d !~ m,/$, && $d ne "") { $d .= "/"; } - open(E, $d."CVS/Entries") || return 0; - while() { - return(1) if (m,^/$f/,); + return ($d,$f); + } +} + +# 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(E); - return 0; } + close($fh); + warn "#### entries($dir) => ",join("|",@entries); + return @entries; +} + +# check if file exists in CVS/Entries +sub in_entries($) { + my $path = shift; + my ($dir,$file) = dir_file($path); + open(E, "$dir/CVS/Entries") || return 0; + while() { + return(1) if (m,^/$file/,); + } + close(E); + return 0; } cd_tmp; @@ -251,8 +284,11 @@ 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"; + next; } else { print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n"; next; @@ -264,6 +300,18 @@ printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'}); my @commit; + my $msg = $e->{'msg'}; + $msg =~ s/'/'\\''/g; # quote " + + 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"); + } + foreach my $p (@{$e->{'paths'}->{'path'}}) { my ($action,$path) = ($p->{'action'},$p->{'content'}); @@ -273,7 +321,11 @@ # prepare path and message my $file = $path; - $path =~ s#^\Q$SVNREP\E/*## || 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"; @@ -283,22 +335,52 @@ my $msg = $e->{'msg'}; $msg =~ s/'/'\\''/g; # quote " - if ($action =~ /M/) { - print "svn2cvs: modify $path -- nop\n"; - } elsif ($action =~ /A/) { + 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"); + 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"); + } + } + + 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 =~ /A/) { + 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"); + 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"); + } 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; @@ -312,11 +394,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); }