/[svn2cvs]/trunk/svn2cvs.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/svn2cvs.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 20 by dpavlin, Mon Apr 18 16:43:08 2005 UTC revision 37 by dpavlin, Fri Sep 7 17:16:48 2007 UTC
# Line 13  Line 13 
13    
14  use strict;  use strict;
15  use File::Temp qw/ tempdir /;  use File::Temp qw/ tempdir /;
16    use File::Path;
17  use Data::Dumper;  use Data::Dumper;
18  use XML::Simple;  use XML::Simple;
19    
20    # do we want to sync just part of repository?
21    my $partial_import = 1;
22    
23    # do we want to add svk-like prefix with original revision, author and date?
24    my $decorate_commit_message = 1;
25    
26  if (@ARGV < 2) {  if (@ARGV < 2) {
27          print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n";          print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n";
28          exit 1;          exit 1;
# Line 28  if ($SVNROOT !~ m,^[\w+]+:///*\w+,) { Line 35  if ($SVNROOT !~ m,^[\w+]+:///*\w+,) {
35          exit 1;          exit 1;
36  }  }
37    
38    # Ensure File::Temp::END can clean up:
39    $SIG{__DIE__} = sub { chdir("/tmp"); die @_ };
40    
41  my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 );  my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 );
42    
43  chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";  sub cd_tmp {
44            chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";
45    }
46    
47    sub cd_rep {
48            chdir("$TMPDIR/$CVSREP") || die "can't cd to $TMPDIR/$CVSREP: $!";
49    }
50    
51    print "## using TMPDIR $TMPDIR\n";
52    
53  # cvs command with root  # cvs command with root
54  my $cvs="cvs -d $CVSROOT";  my $cvs="cvs -f -d $CVSROOT";
55    
56    # current revision in CVS
57    my $rev;
58    
59  #  #
60  # sub to do logging and system calls  # sub to do logging and system calls
# Line 60  sub commit_svnrev { Line 81  sub commit_svnrev {
81          my $path=".svnrev";          my $path=".svnrev";
82    
83          if ($add_new) {          if ($add_new) {
84                  system "$cvs add $path" || die "cvs add of $path failed: $!";                  system "$cvs add '$path'" || die "cvs add of $path failed: $!";
85          } else {          } else {
86                  my $msg="subversion revision $rev commited to CVS";                  my $msg="subversion revision $rev commited to CVS";
87                  print "$msg\n";                  print "$msg\n";
88                  system "$cvs commit -m '$msg' $path" || die "cvs commit of $path failed: $!";                  system "$cvs commit -m '$msg' '$path'" || die "cvs commit of $path failed: $!";
89          }          }
90  }  }
91    
92  # current revision in CVS  sub add_dir($$) {
93  my $rev;          my ($path,$msg) = @_;
94            print "# add_dir($path)\n";
95            die "add_dir($path) is not directory" unless (-d $path);
96    
97            my $curr_dir;
98    
99            foreach my $d (split(m#/#, $path)) {
100                    $curr_dir .= ( $curr_dir ? '/' : '') . $d;
101    
102                    next if in_entries($curr_dir);
103                    next if (-e "$curr_dir/CVS");
104    
105                    log_system("$cvs add '$curr_dir'", "cvs add of $curr_dir failed");
106            }
107    }
108    
109  # ok, now do the checkout  # ok, now do the checkout
110  eval {  eval {
111            cd_tmp;
112          log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");          log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
113  };  };
114    
# Line 88  _NEW_REP_ Line 124  _NEW_REP_
124    
125          print "start import of new module [yes]: ";          print "start import of new module [yes]: ";
126          my $in = <STDIN>;          my $in = <STDIN>;
127            cd_tmp;
128          mkdir($CVSREP) || die "can't create $CVSREP: $!";          mkdir($CVSREP) || die "can't create $CVSREP: $!";
129            cd_rep;
         chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!";  
130    
131          open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!";          open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!";
132          print SVNREV "0";          print SVNREV "0";
# Line 99  _NEW_REP_ Line 135  _NEW_REP_
135          $rev = 0;          $rev = 0;
136    
137          # create new module          # create new module
138          log_system("$cvs import -m 'new CVS module' $CVSREP svn2cvs r0", "can't import new module into $CVSREP");          cd_rep;
139            log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository");
140          unlink ".svnrev" || die "can't remove .svnrev: $!";          cd_tmp;
141          chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";          rmtree($CVSREP) || die "can't remove $CVSREP";
         rmdir $CVSREP || die "can't remove $CVSREP: $!";  
   
         # and checkout it  
142          log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");          log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
143            cd_rep;
         chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!";  
144    
145  } else {  } else {
   
146          # import into existing module directory in CVS          # import into existing module directory in CVS
147    
148          chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!";          cd_rep;
   
   
149          # check if svnrev exists          # check if svnrev exists
150          if (! -e ".svnrev") {          if (! -e ".svnrev") {
151                  print <<_USAGE_;                  print <<_USAGE_;
# Line 173  while(<LOG>) { Line 202  while(<LOG>) {
202  }  }
203  close(LOG);  close(LOG);
204    
205  my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);  
206    my $xml;
207    eval {
208            $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);
209    };
210    
211    
212  #=begin log_example  #=begin log_example
# Line 192  if (! $xml->{'logentry'}) { Line 225  if (! $xml->{'logentry'}) {
225          exit 0;          exit 0;
226  }  }
227    
228    # return all files in CVS/Entries
229    sub entries($) {
230            my $dir = shift;
231            die "entries expects directory argument!" unless -d $dir;
232            my @entries;
233            open(my $fh, "$dir/CVS/Entries") || return 0;
234            while(<$fh>) {
235                    if ( m{^D/([^/]+)}, ) {
236                            my $sub_dir = $1;
237                            warn "#### entries recurse into: $dir/$sub_dir";
238                            push @entries, map { "$sub_dir/$_" } entries("$dir/$sub_dir");
239                            push @entries, $sub_dir;
240                    } elsif ( m{^/([^/]+)/} ) {
241                            push @entries, $1;
242                    } elsif ( ! m{^D$} ) {
243                            die "can't decode entries line: $_";
244                    }
245            }
246            close($fh);
247            warn "#### entries($dir) => ",join("|",@entries);
248            return @entries;
249    }
250    
251  # check if file exists in CVS/Entries  # check if file exists in CVS/Entries
252  sub in_entries($) {  sub in_entries($) {
253          my $path = shift;          my $path = shift;
254          if ($path !~ m,^(.*?/*)([^/]+)$,) {          if ($path =~ m,^(.*?/*)([^/]+)$,) {
255                  die "can't split '$path' to dir and file!";                  my ($dir,$file) = ($1,$2);
256          } else {                  if ($dir !~ m,/$, && $dir ne "") {
257                  my ($d,$f) = ($1,$2);                          $dir .= "/";
                 if ($d !~ m,/$, && $d ne "") {  
                         $d .= "/";  
258                  }                  }
259                  open(E, $d."CVS/Entries") || die "can't open ${d}CVS/Entries: $!";  
260                  while(<E>) {                  open(my $fh, "$dir/CVS/Entries") || return 0; #die "no entries file: $dir/CVS/Entries";
261                          return(1) if (m,^/$f/,);                  while(<$fh>) {
262                            return 1 if (m{^/$file/});
263                  }                  }
264                  close(E);                  close($fh);
265                  return 0;                  return 0;
266            } else {
267                    die "can't split '$path' to dir and file!";
268          }          }
269  }  }
270    
271    cd_tmp;
272    cd_rep;
273    
274  foreach my $e (@{$xml->{'logentry'}}) {  foreach my $e (@{$xml->{'logentry'}}) {
275          die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'});          die "BUG: revision from .svnrev ($rev) greater than from subversion (".$e->{'revision'}.")" if ($rev > $e->{'revision'});
276          $rev = $e->{'revision'};          $rev = $e->{'revision'};
# Line 221  foreach my $e (@{$xml->{'logentry'}}) { Line 281  foreach my $e (@{$xml->{'logentry'}}) {
281          my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";          my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";
282          my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";          my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";
283          do {          do {
284                  if ($tmpsvn =~ s#(/[^/]+)/*$##) {                  if ($tmpsvn =~ s#(/[^/]+)/*$##) {       # vim fix
285                          $SVNREP = $1 . $SVNREP;                          $SVNREP = $1 . $SVNREP;
286                    } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) {
287                            print "NOTICE: copyfrom outside synced repository ignored - skipping\n";
288                            next;
289                  } else {                  } else {
290                          print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";                          print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";
291                          next;                          next;
# Line 234  foreach my $e (@{$xml->{'logentry'}}) { Line 297  foreach my $e (@{$xml->{'logentry'}}) {
297          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});
298          my @commit;          my @commit;
299    
300            my $msg = $e->{'msg'};
301            $msg =~ s/'/'\\''/g;    # quote "
302    
303            $msg = 'r' . $rev . ' ' . $e->{author} . ' | ' . $e->{date} . "\n" . $msg if $decorate_commit_message;
304    
305            sub cvs_commit {
306                    my $msg = shift || die "no msg?";
307                    if ( ! @_ ) {
308                            warn "commit ignored, no files\n";
309                            return;
310                    }
311                    log_system("$cvs commit -m '$msg' '".join("' '",@_)."'", "cvs commit of ".join(",",@_)." failed");
312            }
313    
314          foreach my $p (@{$e->{'paths'}->{'path'}}) {          foreach my $p (@{$e->{'paths'}->{'path'}}) {
315                  my ($action,$path) = ($p->{'action'},$p->{'content'});                  my ($action,$path) = ($p->{'action'},$p->{'content'});
316    
317                    next if ($path =~ m#/\.svnrev$#);
318    
319                  print "svn2cvs: $action $path\n";                  print "svn2cvs: $action $path\n";
320    
321                  # prepare path and message                  # prepare path and message
322                  my $file = $path;                  my $file = $path;
323                  $path =~ s,^$SVNREP/*,, || die "BUG: can't strip SVNREP '$SVNREP' from path";                  if ( $path !~ s#^\Q$SVNREP\E/*## ) {
324                            print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n";
325                            die unless $partial_import;
326                            next;
327                    }
328    
329                  if (! $path) {                  if (! $path) {
330                          print "NOTICE: skipped this operation. Probably trunk creation\n";                          print "NOTICE: skipped this operation. Probably trunk creation\n";
# Line 251  foreach my $e (@{$xml->{'logentry'}}) { Line 334  foreach my $e (@{$xml->{'logentry'}}) {
334                  my $msg = $e->{'msg'};                  my $msg = $e->{'msg'};
335                  $msg =~ s/'/'\\''/g;    # quote "                  $msg =~ s/'/'\\''/g;    # quote "
336    
337                  if ($action =~ /M/) {                  sub add_path {
338                          print "svn2cvs: modify $path -- nop\n";                          my $path = shift || die "no path?";
339                  } elsif ($action =~ /A/) {          
340                          if (-d $path) {                          if (-d $path) {
341                                  chdir($path) || die "can't cd into dir $path for import: $!";                                  add_dir($path, $msg);
                                 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: $!";  
342                          } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {                          } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {
343                                  my $dir = $1;                                  my $dir = $1;
344                                  in_entries($dir) || log_system("$cvs add $dir", "cvs add of dir $dir failed");                                  in_entries($dir) || add_dir($dir, $msg);
345                                  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");
346                          } else {                          } else {
347                                  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");
348                          }                          }
349                    }
350    
351                    if ($action =~ /M/) {
352                            if ( in_entries( $path ) ) {
353                                    print "svn2cvs: modify $path -- nop\n";
354                            } else {
355                                    print "WARNING: modify $path which isn't in CVS, adding...\n";
356                                    add_path($path);
357                            }
358                    } elsif ($action =~ /A/) {
359                            add_path($path);
360                  } elsif ($action =~ /D/) {                  } elsif ($action =~ /D/) {
361                          unlink $path || die "can't delete $path: $!";                          if (-e $path) {
362                          log_system("$cvs delete $path", "cvs delete of $path failed");                                  if ( -d $path ) {
363                                            warn "#### remove directory: $path";
364                                            my @sub_commit;
365                                            foreach my $f ( entries($path) ) {
366                                                    $f = "$path/$f";
367                                            if ( -f $f ) {
368                                                            unlink($f) || die "can't delete file $f: $!";
369    #                                               } else {
370    #                                                       rmtree($f) || die "can't delete dir $f: $!";
371                                                    }
372                                                    log_system("$cvs delete '$f'", "cvs delete of file $f failed");
373                                                    push @sub_commit, $f;
374                                            }
375                                            log_system("$cvs delete '$path'", "cvs delete of file $path failed");
376                                            cvs_commit($msg, @sub_commit, $path);
377                                            log_system("$cvs update -dP '$path'", "cvs update -dP $path failed");
378                                    } else {
379                                            warn "#### remove file: $path";
380                                            unlink($path) || die "can't delete $path: $!";
381                                            log_system("$cvs delete '$path'", "cvs delete of dir $path failed");
382                                    }
383                            } else {
384                                    print "WARNING: $path is not present, skipping...\n";
385                                    undef $path;
386                            }
387                  } else {                  } else {
388                          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";
389                  }                  }
390    
391                  # save commits for later                  # save commits for later
392                  push @commit, $path;                  push @commit, $path if ($path);
393    
394          }          }
395    
         my $msg = $e->{'msg'};  
         $msg =~ s/'/'\\''/g;    # quote "  
   
396          # now commit changes          # now commit changes
397          log_system("$cvs commit -m '$msg' ".join(" ",@commit), "cvs commit of ".join(",",@commit)." failed");          cvs_commit($msg, @commit);
398    
399          commit_svnrev($rev);          commit_svnrev($rev);
400  }  }
401    
402    # cd out of $CVSREP before File::Temp::END is called
403    chdir("/tmp") || die "can't cd to /tmp: $!";
404    
405  __END__  __END__
406    
407  =pod  =pod
# Line 408  Documentation improvements and other sma Line 517  Documentation improvements and other sma
517    
518  Fixed path deduction (overlap between Subversion reporistory and CVS checkout).  Fixed path deduction (overlap between Subversion reporistory and CVS checkout).
519    
520    =item r21
521    
522    Use C<update -d> instead of checkout after import.
523    Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working
524    directory.
525    
526    =item r22
527    
528    Rewritten import from revision 0 to empty repository, better importing
529    of deep directory structures, initial support for recovery from partial
530    commit.
531    
532  =back  =back
533    
534  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.20  
changed lines
  Added in v.37

  ViewVC Help
Powered by ViewVC 1.1.26