/[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 14 by dpavlin, Sun Mar 14 21:49:36 2004 UTC revision 36 by dpavlin, Fri Sep 7 16:53:56 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    sub add_dir($$) {
93            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 {
111            cd_tmp;
112            log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
113    };
114    
115  log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");  if ($@) {
116            print <<_NEW_REP_;
117    
118  chdir($CVSREP) || die "can't cd to $TMPDIR/$CVSREP: $!";  There is no CVS repository '$CVSREP' in your CVS. I will assume that
119    this is import of new module in your CVS and start from revision 0.
120    
121    Press enter to continue importing new CVS repository or CTRL+C to abort.
122    
123  my $rev;  _NEW_REP_
124    
125            print "start import of new module [yes]: ";
126            my $in = <STDIN>;
127            cd_tmp;
128            mkdir($CVSREP) || die "can't create $CVSREP: $!";
129            cd_rep;
130    
131            open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!";
132            print SVNREV "0";
133            close(SVNREV);
134    
135            $rev = 0;
136    
137  # check if svnrev exists          # create new module
138  if (! -e ".svnrev") {          cd_rep;
139          print <<_USAGE_;          log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository");
140            cd_tmp;
141            rmtree($CVSREP) || die "can't remove $CVSREP";
142            log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
143            cd_rep;
144    
145    } else {
146            # import into existing module directory in CVS
147    
148            cd_rep;
149            # check if svnrev exists
150            if (! -e ".svnrev") {
151                    print <<_USAGE_;
152    
153  Your CVS repository doesn't have .svnrev file!  Your CVS repository doesn't have .svnrev file!
154    
# Line 97  Subversion repository to CVS, correct re Line 166  Subversion repository to CVS, correct re
166    
167  _USAGE_  _USAGE_
168    
169          print "svn revision corresponding to CVS [abort]: ";                  print "svn revision corresponding to CVS [abort]: ";
170          my $in = <STDIN>;                  my $in = <STDIN>;
171          chomp($in);                  chomp($in);
172          if ($in !~ /^\d+$/) {                  if ($in !~ /^\d+$/) {
173                  print "Aborting: revision not a number\n";                          print "Aborting: revision not a number\n";
174                  exit 1;                          exit 1;
175                    } else {
176                            $rev = $in;
177                            commit_svnrev($rev,1);  # create new
178                    }
179          } else {          } else {
180                  $rev = $in;                  open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
181                  commit_svnrev($rev,1);  # create new                  $rev = <SVNREV>;
182                    chomp($rev);
183                    close(SVNREV);
184          }          }
 } else {  
         open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";  
         $rev = <SVNREV>;  
         chomp($rev);  
         close(SVNREV);  
 }  
185    
186  print "Starting after revision $rev\n";          print "Starting after revision $rev\n";
187  $rev++;          $rev++;
188    }
189    
190    
191  #  #
# Line 132  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 151  if (! $xml->{'logentry'}) { Line 225  if (! $xml->{'logentry'}) {
225          exit 0;          exit 0;
226  }  }
227    
228  # check if file exists in CVS/Entries  # my ($dir,$file) = dir_file($path);
229  sub in_entries($) {  sub dir_file($) {
230          my $path = shift;          my $path = shift;
231          if ($path !~ m,^(.*?/*)([^/]+)$,) {          if ($path !~ m,^(.*?/*)([^/]+)$,) {
232                  die "can't split '$path' to dir and file!";                  die "can't split '$path' to dir and file!";
233          } else {          } else {
234                  my ($d,$f) = ($1,$2);                  my ($d,$f) = ($1,$2);
235                  if ($d !~ m,/$,) {                  if ($d !~ m,/$, && $d ne "") {
236                          $d .= "/";                          $d .= "/";
237                  }                  }
238                  open(E, $d."CVS/Entries") || die "can't open ${d}CVS/Entries: $!";                  return ($d,$f);
239                  while(<E>) {          }
240                          return(1) if (m,^/$f/,);  }
241    
242    # return all files in CVS/Entries
243    sub entries($) {
244            my $dir = shift;
245            die "entries expects directory argument!" unless -d $dir;
246            my @entries;
247            open(my $fh, "$dir/CVS/Entries") || return 0;
248            while(<$fh>) {
249                    if ( m{^D/([^/]+)}, ) {
250                            my $sub_dir = $1;
251                            warn "#### entries recurse into: $dir/$sub_dir";
252                            push @entries, map { "$sub_dir/$_" } entries("$dir/$sub_dir");
253                            push @entries, $sub_dir;
254                    } elsif ( m{^/([^/]+)/} ) {
255                            push @entries, $1;
256                    } elsif ( ! m{^D$} ) {
257                            die "can't decode entries line: $_";
258                  }                  }
                 close(E);  
                 return 0;  
259          }          }
260            close($fh);
261            warn "#### entries($dir) => ",join("|",@entries);
262            return @entries;
263    }
264    
265    # check if file exists in CVS/Entries
266    sub in_entries($) {
267            my $path = shift;
268            my ($dir,$file) = dir_file($path);
269            open(E, "$dir/CVS/Entries") || return 0;
270            while(<E>) {
271                    return(1) if (m,^/$file/,);
272            }
273            close(E);
274            return 0;
275  }  }
276    
277    cd_tmp;
278    cd_rep;
279    
280  foreach my $e (@{$xml->{'logentry'}}) {  foreach my $e (@{$xml->{'logentry'}}) {
281          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'});
282          $rev = $e->{'revision'};          $rev = $e->{'revision'};
# Line 180  foreach my $e (@{$xml->{'logentry'}}) { Line 287  foreach my $e (@{$xml->{'logentry'}}) {
287          my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";          my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";
288          my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";          my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";
289          do {          do {
290  print "## tmppath: $tmppath tmpsvn: $tmpsvn SVNREP: $SVNREP\n";                  if ($tmpsvn =~ s#(/[^/]+)/*$##) {       # vim fix
291                  if ($tmpsvn =~ s,(/\w+/*)$,,) {                          $SVNREP = $1 . $SVNREP;
292                          $SVNREP .= $1;                  } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) {
293                            print "NOTICE: copyfrom outside synced repository ignored - skipping\n";
294                            next;
295                  } else {                  } else {
296                          die "ERROR: can't deduce svn dir from $SVNROOT.\nUsing root of snv repository for current version instead of /trunk/ is not supported.\n";                          print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";
297                            next;
298                  }                  }
299          } until ($tmppath =~ m/^$SVNREP/);          } until ($tmppath =~ m/^$SVNREP/);
300    
301          print "NOTICE: using $SVNREP as directory for svn\n";          print "NOTICE: using $SVNREP as directory for svn\n";
302    
303          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});
304            my @commit;
305    
306            my $msg = $e->{'msg'};
307            $msg =~ s/'/'\\''/g;    # quote "
308    
309            $msg = 'r' . $rev . ' ' . $e->{author} . ' | ' . $e->{date} . "\n" . $msg if $decorate_commit_message;
310    
311            sub cvs_commit {
312                    my $msg = shift || die "no msg?";
313                    if ( ! @_ ) {
314                            warn "commit ignored, no files\n";
315                            return;
316                    }
317                    log_system("$cvs commit -m '$msg' '".join("' '",@_)."'", "cvs commit of ".join(",",@_)." failed");
318            }
319    
320          foreach my $p (@{$e->{'paths'}->{'path'}}) {          foreach my $p (@{$e->{'paths'}->{'path'}}) {
321                  my ($action,$path) = ($p->{'action'},$p->{'content'});                  my ($action,$path) = ($p->{'action'},$p->{'content'});
322    
323                    next if ($path =~ m#/\.svnrev$#);
324    
325                  print "svn2cvs: $action $path\n";                  print "svn2cvs: $action $path\n";
326    
327                  # prepare path and message                  # prepare path and message
328                  my $file = $path;                  my $file = $path;
329                  $path =~ s,^$SVNREP/*,, || die "BUG: can't strip SVNREP from path";                  if ( $path !~ s#^\Q$SVNREP\E/*## ) {
330                            print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n";
331                            die unless $partial_import;
332                            next;
333                    }
334    
335                  if (! $path) {                  if (! $path) {
336                          print "NOTICE: skipped this operation. Probably trunk creation\n";                          print "NOTICE: skipped this operation. Probably trunk creation\n";
# Line 208  print "## tmppath: $tmppath tmpsvn: $tmp Line 340  print "## tmppath: $tmppath tmpsvn: $tmp
340                  my $msg = $e->{'msg'};                  my $msg = $e->{'msg'};
341                  $msg =~ s/'/'\\''/g;    # quote "                  $msg =~ s/'/'\\''/g;    # quote "
342    
343                  if ($action =~ /M/) {                  sub add_path {
344                          print "svn2cvs: modify $path -- nop\n";                          my $path = shift || die "no path?";
345                  } elsif ($action =~ /A/) {          
346                          if (-d $path) {                          if (-d $path) {
347                                  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");  
                                 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: $!";  
348                          } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {                          } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {
349                                  my $dir = $1;                                  my $dir = $1;
350                                  in_entries($dir) || log_system("$cvs add $dir", "cvs add of dir $dir failed");                                  in_entries($dir) || add_dir($dir, $msg);
351                                  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");
352                          } else {                          } else {
353                                  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");
354                          }                          }
355                    }
356    
357                    if ($action =~ /M/) {
358                            if ( in_entries( $path ) ) {
359                                    print "svn2cvs: modify $path -- nop\n";
360                            } else {
361                                    print "WARNING: modify $path which isn't in CVS, adding...\n";
362                                    add_path($path);
363                            }
364                    } elsif ($action =~ /A/) {
365                            add_path($path);
366                  } elsif ($action =~ /D/) {                  } elsif ($action =~ /D/) {
367                          unlink $path || die "can't delete $path: $!";                          if (-e $path) {
368                          log_system("$cvs delete $path", "cvs delete of $path failed");                                  if ( -d $path ) {
369                                            warn "#### remove directory: $path";
370                                            my @sub_commit;
371                                            foreach my $f ( entries($path) ) {
372                                                    $f = "$path/$f";
373                                            if ( -f $f ) {
374                                                            unlink($f) || die "can't delete file $f: $!";
375    #                                               } else {
376    #                                                       rmtree($f) || die "can't delete dir $f: $!";
377                                                    }
378                                                    log_system("$cvs delete '$f'", "cvs delete of file $f failed");
379                                                    push @sub_commit, $f;
380                                            }
381                                            log_system("$cvs delete '$path'", "cvs delete of file $path failed");
382                                            cvs_commit($msg, @sub_commit, $path);
383                                            log_system("$cvs update -dP '$path'", "cvs update -dP $path failed");
384                                    } else {
385                                            warn "#### remove file: $path";
386                                            unlink($path) || die "can't delete $path: $!";
387                                            log_system("$cvs delete '$path'", "cvs delete of dir $path failed");
388                                    }
389                            } else {
390                                    print "WARNING: $path is not present, skipping...\n";
391                                    undef $path;
392                            }
393                  } else {                  } else {
394                          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";
395                  }                  }
396    
397                  # now commit changes                  # save commits for later
398                  log_system("$cvs commit -m '$msg' $path", "cvs commit of $path failed");                  push @commit, $path if ($path);
399    
400          }          }
401    
402            # now commit changes
403            cvs_commit($msg, @commit);
404    
405          commit_svnrev($rev);          commit_svnrev($rev);
406  }  }
407    
408    # cd out of $CVSREP before File::Temp::END is called
409    chdir("/tmp") || die "can't cd to /tmp: $!";
410    
411  __END__  __END__
412    
413  =pod  =pod
# Line 258  Usage example (used to self-host this sc Line 427  Usage example (used to self-host this sc
427    
428  =head1 DESCRIPTION  =head1 DESCRIPTION
429    
430  This script will allow you to commit changes made to Subversion repository also to (read-only) CVS repository manually or from Subversion's C<post-commit> hook.  This script will allows you to commit changes made to Subversion repository to
431    (read-only) CVS repository manually or from Subversion's C<post-commit> hook.
432    
433  It's using F<.svnrev> file (which will be created on first run) in  It's using F<.svnrev> file (which will be created on first run) in
434  B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was  B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was
# Line 310  again. Line 480  again.
480  "Cheap" copy operations in Subversion are not at all cheap in CVS. They will  "Cheap" copy operations in Subversion are not at all cheap in CVS. They will
481  create multiple copies of files in CVS repository!  create multiple copies of files in CVS repository!
482    
483    This script assume that you want to sync your C<trunk> (or any other
484    directory for that matter) directory with CVS, not root of your subversion.
485    This might be considered bug, but since common practise is to have
486    directories C<trunk> and C<branches> in svn and source code in them, it's
487    not serious limitation.
488    
489  =head1 RELATED PROJECTS  =head1 RELATED PROJECTS
490    
491  B<Subversion> L<http://subversion.tigris.org/> version control system that is a  B<Subversion> L<http://subversion.tigris.org/> version control system that is a
# Line 319  B<cvs2svn> L<http://cvs2svn.tigris.org/> Line 495  B<cvs2svn> L<http://cvs2svn.tigris.org/>
495  Subversion repository. It is designed for one-time conversions, not for  Subversion repository. It is designed for one-time conversions, not for
496  repeated synchronizations between CVS and Subversion.  repeated synchronizations between CVS and Subversion.
497    
498    =head1 CHANGES
499    
500    Versions of this utility are actually Subversion repository revisions,
501    so they might not be in sequence.
502    
503    =over 3
504    
505    =item r10
506    
507    First release available to public
508    
509    =item r15
510    
511    Addition of comprehensive documentation, fixes for quoting in commit
512    messages, and support for skipping changes which are not under current
513    Subversion checkout root (e.g. branches).
514    
515    =item r18
516    
517    Support for importing your svn into empty CVS repository (it will first
518    create module and than dump all revisions).
519    Group commit operations to save round-trips to CVS server.
520    Documentation improvements and other small fixes.
521    
522    =item r20
523    
524    Fixed path deduction (overlap between Subversion reporistory and CVS checkout).
525    
526    =item r21
527    
528    Use C<update -d> instead of checkout after import.
529    Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working
530    directory.
531    
532    =item r22
533    
534    Rewritten import from revision 0 to empty repository, better importing
535    of deep directory structures, initial support for recovery from partial
536    commit.
537    
538    =back
539    
540  =head1 AUTHOR  =head1 AUTHOR
541    
542  Dobrica Pavlinusic <dpavlin@rot13.org>  Dobrica Pavlinusic <dpavlin@rot13.org>

Legend:
Removed from v.14  
changed lines
  Added in v.36

  ViewVC Help
Powered by ViewVC 1.1.26