/[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 2 by dpavlin, Tue Mar 9 21:02:42 2004 UTC revision 31 by dpavlin, Wed Sep 5 20:39:29 2007 UTC
# Line 8  Line 8 
8  # http://raw.no/personal/blog  # http://raw.no/personal/blog
9  #  #
10  # 2004-03-09 Dobrica Pavlinusic <dpavlin@rot13.org>  # 2004-03-09 Dobrica Pavlinusic <dpavlin@rot13.org>
11    #
12    # documentation is after __END__
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  # get current user home directory  # do we want to sync just part of repository?
21  my $HOME = $ENV{'HOME'} || die "can't get home directory!";  my $partial_import = 1;
22    
23    if (@ARGV < 2) {
24            print "usage: $0 SVN_URL CVSROOT CVSREPOSITORY\n";
25            exit 1;
26    }
27    
28    my ($SVNROOT,$CVSROOT, $CVSREP) = @ARGV;
29    
30    if ($SVNROOT !~ m,^[\w+]+:///*\w+,) {
31            print "ERROR: invalid svn root $SVNROOT\n";
32            exit 1;
33    }
34    
35  # cvsroot directory  # Ensure File::Temp::END can clean up:
36  my $CVSROOT="$HOME/x/cvsroot";  $SIG{__DIE__} = sub { chdir("/tmp"); die @_ };
 # 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";  
37    
38  my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 );  my $TMPDIR=tempdir( "/tmp/checkoutXXXXX", CLEANUP => 1 );
39    
40  chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";  sub cd_tmp {
41            chdir($TMPDIR) || die "can't cd to $TMPDIR: $!";
42    }
43    
44    sub cd_rep {
45            chdir("$TMPDIR/$CVSREP") || die "can't cd to $TMPDIR/$CVSREP: $!";
46    }
47    
48    print "## using TMPDIR $TMPDIR\n";
49    
50  # cvs command with root  # cvs command with root
51  my $cvs="cvs -d $CVSROOT";  my $cvs="cvs -f -d $CVSROOT";
52    
53    # current revision in CVS
54    my $rev;
55    
56  #  #
57  # sub to do logging and system calls  # sub to do logging and system calls
# Line 40  my $cvs="cvs -d $CVSROOT"; Line 59  my $cvs="cvs -d $CVSROOT";
59  sub log_system($$) {  sub log_system($$) {
60          my ($cmd,$errmsg) = @_;          my ($cmd,$errmsg) = @_;
61          print STDERR "## $cmd\n";          print STDERR "## $cmd\n";
62          system $cmd || die "$errmsg: $!";          system($cmd) == 0 || die "$errmsg: $!";
63  }  }
64    
65    #
66    # sub to commit .svn rev file later
67    #
68    sub commit_svnrev {
69            my $rev = shift @_;
70            my $add_new = shift @_;
71    
72  # ok, now do the checkout          die "commit_svnrev needs revision" if (! defined($rev));
73    
74  log_system("$cvs checkout $CVSREP","cvs checkout failed");          open(SVNREV,"> .svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
75            print SVNREV $rev;
76            close(SVNREV);
77    
78  # check if svnrev exists          my $path=".svnrev";
79    
80  if (! -e "$CVSREP/.svnrev") {          if ($add_new) {
81          print <<_USAGE_;                  system "$cvs add '$path'" || die "cvs add of $path failed: $!";
82            } else {
83                    my $msg="subversion revision $rev commited to CVS";
84                    print "$msg\n";
85                    system "$cvs commit -m '$msg' '$path'" || die "cvs commit of $path failed: $!";
86            }
87    }
88    
89          Your CVS repository doesn't have $TMPDIR/$CVSREP/.svnrev file!  sub add_dir($$) {
90            my ($path,$msg) = @_;
91            print "# add_dir($path)\n";
92            die "add_dir($path) is not directory" unless (-d $path);
93    
94          This file is used to commit changes to CVS repository with correct          my $curr_dir;
         logs from Subversion.  
95    
96          Please create .svnrec file with currect svn revision which          foreach my $d (split(m#/#, $path)) {
97          corresponds to version of checkouted tree and commit that file                  $curr_dir .= ( $curr_dir ? '/' : '') . $d;
         to CVS repository, e.g.  
98    
99          echo 233 > .svnrev                  next if in_entries($curr_dir);
100          cvs add .svnrev                  next if (-e "$curr_dir/CVS");
         cvs commit -m "subversion repository revision sync file" .svnrev  
101    
102  _USAGE_                  log_system("$cvs add '$curr_dir'", "cvs add of $curr_dir failed");
103          print "CVS Repository left in $TMPDIR/$CVSREP\n";          }
         exit 1;  
104  }  }
105    
106    # ok, now do the checkout
107    eval {
108            cd_tmp;
109            log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
110    };
111    
112  open(SVNREV,"$CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";  if ($@) {
113  my $rev = <SVNREV>;          print <<_NEW_REP_;
 chomp($rev);  
 close(SVNREV);  
114    
115  print "Starting after revision $rev\n";  There is no CVS repository '$CVSREP' in your CVS. I will assume that
116  $rev++;  this is import of new module in your CVS and start from revision 0.
117    
118    Press enter to continue importing new CVS repository or CTRL+C to abort.
119    
120  #  _NEW_REP_
 # sub to commit .svn rev file later  
 #  
121    
122  sub commit_svnrev($) {          print "start import of new module [yes]: ";
123          my $rev = shift @_ || die "commit_svnrev needs revision";          my $in = <STDIN>;
124            cd_tmp;
125            mkdir($CVSREP) || die "can't create $CVSREP: $!";
126            cd_rep;
127    
128          open(SVNREV,"> $CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";          open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!";
129          print SVNREV $rev;          print SVNREV "0";
130          close(SVNREV);          close(SVNREV);
131    
132          my $msg="subversion revision $rev commited to CVS";          $rev = 0;
133          my $path=".svnrev";  
134          print "$msg\n";          # create new module
135          system "$cvs commit -m \"$msg\" $CVSREP/$path" || die "cvs commit of $path failed: $!";          cd_rep;
136            log_system("$cvs import -d -m 'new CVS module' $CVSREP svn r$rev", "import of new repository");
137            cd_tmp;
138            rmtree($CVSREP) || die "can't remove $CVSREP";
139            log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
140            cd_rep;
141    
142    } else {
143            # import into existing module directory in CVS
144    
145            cd_rep;
146            # check if svnrev exists
147            if (! -e ".svnrev") {
148                    print <<_USAGE_;
149    
150    Your CVS repository doesn't have .svnrev file!
151    
152    This file is used to keep CVS repository and Subversion in sync, so
153    that only newer changes will be commited.
154    
155    It's quote possible that this is first svn2cvs run for this repository.
156    If so, you will have to identify correct svn revision which
157    corresponds to current version of CVS repository that has just
158    been checkouted.
159    
160    If you migrated your cvs repository to svn using cvs2svn, this will be
161    last Subversion revision. If this is initial run of conversion of
162    Subversion repository to CVS, correct revision is 0.
163    
164    _USAGE_
165    
166                    print "svn revision corresponding to CVS [abort]: ";
167                    my $in = <STDIN>;
168                    chomp($in);
169                    if ($in !~ /^\d+$/) {
170                            print "Aborting: revision not a number\n";
171                            exit 1;
172                    } else {
173                            $rev = $in;
174                            commit_svnrev($rev,1);  # create new
175                    }
176            } else {
177                    open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
178                    $rev = <SVNREV>;
179                    chomp($rev);
180                    close(SVNREV);
181            }
182    
183            print "Starting after revision $rev\n";
184            $rev++;
185  }  }
186    
187    
188  #  #
189  # FIXME!! HEAD should really be next verison and loop because this way we  # FIXME!! HEAD should really be next verison and loop because this way we
190  # loose multiple edits of same file and corresponding messages. On the  # loose multiple edits of same file and corresponding messages. On the
# Line 105  sub commit_svnrev($) { Line 192  sub commit_svnrev($) {
192  # case much about accuracy and completnes of logs there, this might  # case much about accuracy and completnes of logs there, this might
193  # be good. YMMV  # be good. YMMV
194  #  #
195  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: $!";
196  my $log;  my $log;
197  while(<LOG>) {  while(<LOG>) {
198          $log .= $_;          $log .= $_;
199  }  }
200  close(LOG);  close(LOG);
201    
 my $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);  
   
   
 =begin log_example  
202    
203  ------------------------------------------------------------------------  my $xml;
204  r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines  eval {
205            $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);
206    };
207    
 ported r254 from hidra branch  
208    
209  =cut  #=begin log_example
210    #
211    #------------------------------------------------------------------------
212    #r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines
213    #
214    #ported r254 from hidra branch
215    #
216    #=cut
217    
218  my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n";  my $fmt = "\n" . "-" x 79 . "\nr%5s| %8s | %s\n\n%s\n";
219    
220  if (! $xml->{'logentry'}) {  if (! $xml->{'logentry'}) {
221          print "no newer log entries in SubVersion repostory. CVS is current\n";          print "no newer log entries in Subversion repostory. CVS is current\n";
222          exit 0;          exit 0;
223  }  }
224    
225  print Dumper($xml);  # check if file exists in CVS/Entries
226    sub in_entries($) {
227            my $path = shift;
228            if ($path !~ m,^(.*?/*)([^/]+)$,) {
229                    die "can't split '$path' to dir and file!";
230            } else {
231                    my ($d,$f) = ($1,$2);
232                    if ($d !~ m,/$, && $d ne "") {
233                            $d .= "/";
234                    }
235                    open(E, $d."CVS/Entries") || return 0;
236                    while(<E>) {
237                            return(1) if (m,^/$f/,);
238                    }
239                    close(E);
240                    return 0;
241            }
242    }
243    
244    cd_tmp;
245    cd_rep;
246    
247  foreach my $e (@{$xml->{'logentry'}}) {  foreach my $e (@{$xml->{'logentry'}}) {
248          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'});
249          $rev = $e->{'revision'};          $rev = $e->{'revision'};
250          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");
251    
252            # deduce name of svn directory
253            my $SVNREP = "";
254            my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";
255            my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";
256            do {
257                    if ($tmpsvn =~ s#(/[^/]+)/*$##) {
258                            $SVNREP = $1 . $SVNREP;
259                    } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) {
260                            print "NOTICE: copyfrom outside synced repository ignored - skipping\n";
261                            next;
262                    } else {
263                            print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";
264                            next;
265                    }
266            } until ($tmppath =~ m/^$SVNREP/);
267    
268            print "NOTICE: using $SVNREP as directory for svn\n";
269    
270          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});
271            my @commit;
272    
273          foreach my $p (@{$e->{'paths'}->{'path'}}) {          foreach my $p (@{$e->{'paths'}->{'path'}}) {
274                  my ($action,$path) = ($p->{'action'},$p->{'content'});                  my ($action,$path) = ($p->{'action'},$p->{'content'});
275    
276                    next if ($path =~ m#/\.svnrev$#);
277    
278                  print "svn2cvs: $action $path\n";                  print "svn2cvs: $action $path\n";
279    
280                  # prepare path and message                  # prepare path and message
281                  my $file = $path;                  my $file = $path;
282                  $path =~ s,^/$SVNREP/*,, || die "BUG: can't strip SVNREP from path";                  if ( $path !~ s#^\Q$SVNREP\E/*## ) {
283                            print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n";
284                            die unless $partial_import;
285                            next;
286                    }
287    
288                    if (! $path) {
289                            print "NOTICE: skipped this operation. Probably trunk creation\n";
290                            next;
291                    }
292    
293                  my $msg = $e->{'msg'};                  my $msg = $e->{'msg'};
294                  $msg =~ s/"/\\"/g;      # quote "                  $msg =~ s/'/'\\''/g;    # quote "
295    
296                    sub add_path {
297                            my $path = shift || die "no path?";
298            
299                            if (-d $path) {
300                                    add_dir($path, $msg);
301                            } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {
302                                    my $dir = $1;
303                                    in_entries($dir) || add_dir($dir, $msg);
304                                    in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed");
305                            } else {
306                                    in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed");
307                            }
308                    }
309    
310                  if ($action =~ /M/) {                  if ($action =~ /M/) {
311                          print "svn2cvs: modify $path -- nop\n";                          if ( in_entries( $path ) ) {
312                                    print "svn2cvs: modify $path -- nop\n";
313                            } else {
314                                    print "WARNING: modify $path which isn't in CVS, adding...\n";
315                                    add_path($path);
316                            }
317                  } elsif ($action =~ /A/) {                  } elsif ($action =~ /A/) {
318                          log_system("$cvs add -m \"$msg\" $CVSREP/$path", "cvs add of $path failed");                          add_path($path);
319                  } elsif ($action =~ /D/) {                  } elsif ($action =~ /D/) {
320                          log_system("$cvs delete -m \"$msg\" $CVSREP/$path", "cvs delete of $path failed");                          if (-e $path) {
321                                    unlink $path || die "can't delete $path: $!";
322                                    log_system("$cvs delete '$path'", "cvs delete of $path failed");
323                            } else {
324                                    print "WARNING: $path is not present, skipping...\n";
325                                    undef $path;
326                            }
327                  } else {                  } else {
328                          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";
329                  }                  }
330    
331                  # now commit changes                  # save commits for later
332                  log_system("$cvs commit -m \"$msg\" $CVSREP/$path", "cvs commit of $path failed");                  push @commit, $path if ($path);
333    
334          }          }
335    
336            my $msg = $e->{'msg'};
337            $msg =~ s/'/'\\''/g;    # quote "
338    
339            # now commit changes
340            log_system("$cvs commit -m '$msg' '".join("' '",@commit)."'", "cvs commit of ".join(",",@commit)." failed");
341    
342          commit_svnrev($rev);          commit_svnrev($rev);
343  }  }
344    
345    # cd out of $CVSREP before File::Temp::END is called
346    chdir("/tmp") || die "can't cd to /tmp: $!";
347    
348  __END__  __END__
349    
350  svn export --force "$SVNROOT/$SVNREP" "$CVSREP"  =pod
351    
352  cd dotfiles  =head1 NAME
353    
354  for file in $(find -type f -not -path \*CVS\*); do  svn2cvs - save subversion commits to (read-only) cvs repository
355          FILE=$(basename $file)  
356          DIR=$(dirname $file)  =head1 SYNOPSIS
357          if ! grep -q "^/$FILE/" $DIR/CVS/Entries ; then  
358                  cvs add $file    ./svn2cvs.pl SVN_URL CVSROOT CVSREPOSITORY
359          fi  
360  done  Usage example (used to self-host this script):
361    
362  #cvs commit -m "Automatic commit from SVN"    ./svn2cvs.pl file:///home/dpavlin/private/svn/svn2cvs/trunk/ \
363                   :pserver:dpavlin@cvs.tigris.org:/cvs svn2cvs/src
364  #rm -rf $TMPDIR  
365    =head1 DESCRIPTION
366    
367    This script will allows you to commit changes made to Subversion repository to
368    (read-only) CVS repository manually or from Subversion's C<post-commit> hook.
369    
370    It's using F<.svnrev> file (which will be created on first run) in
371    B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was
372    committed into CVS.
373    
374    One run will do following things:
375    
376    =over 4
377    
378    =item *
379    checkout B<CVSREPOSITORY> from B<CVSROOT> to temporary directory
380    
381    =item *
382    check if F<.svnrev> file exists and create it if it doesn't
383    
384    =item *
385    loop through all revisions from current in B<CVSROOT/CVSREPOSITORY> (using
386    F<.svnrev>) up to B<HEAD> (current one)
387    
388    =over 5
389    
390    =item *
391    checkout next Subversion revision from B<SVN_URL> over CVS checkout
392    temporary directory
393    
394    =item *
395    make modification (add and/or delete) done in that revision
396    
397    =item *
398    commit modification (added, deleted or modified files/dirs) while
399    preserving original message from CVS
400    
401    =item *
402    update F<.svnrev> to match current revision
403    
404    =back
405    
406    =item *
407    cleanup temporary directory
408    
409    =back
410    
411    If checkout fails for some reason (e.g. flaky ssh connection), you will
412    still have valid CVS repository, so all you have to do is run B<svn2cvs.pl>
413    again.
414    
415    =head1 WARNINGS
416    
417    "Cheap" copy operations in Subversion are not at all cheap in CVS. They will
418    create multiple copies of files in CVS repository!
419    
420    This script assume that you want to sync your C<trunk> (or any other
421    directory for that matter) directory with CVS, not root of your subversion.
422    This might be considered bug, but since common practise is to have
423    directories C<trunk> and C<branches> in svn and source code in them, it's
424    not serious limitation.
425    
426    =head1 RELATED PROJECTS
427    
428    B<Subversion> L<http://subversion.tigris.org/> version control system that is a
429    compelling replacement for CVS in the open source community.
430    
431    B<cvs2svn> L<http://cvs2svn.tigris.org/> converts a CVS repository to a
432    Subversion repository. It is designed for one-time conversions, not for
433    repeated synchronizations between CVS and Subversion.
434    
435    =head1 CHANGES
436    
437    Versions of this utility are actually Subversion repository revisions,
438    so they might not be in sequence.
439    
440    =over 3
441    
442    =item r10
443    
444    First release available to public
445    
446    =item r15
447    
448    Addition of comprehensive documentation, fixes for quoting in commit
449    messages, and support for skipping changes which are not under current
450    Subversion checkout root (e.g. branches).
451    
452    =item r18
453    
454    Support for importing your svn into empty CVS repository (it will first
455    create module and than dump all revisions).
456    Group commit operations to save round-trips to CVS server.
457    Documentation improvements and other small fixes.
458    
459    =item r20
460    
461    Fixed path deduction (overlap between Subversion reporistory and CVS checkout).
462    
463    =item r21
464    
465    Use C<update -d> instead of checkout after import.
466    Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working
467    directory.
468    
469    =item r22
470    
471    Rewritten import from revision 0 to empty repository, better importing
472    of deep directory structures, initial support for recovery from partial
473    commit.
474    
475    =back
476    
477    =head1 AUTHOR
478    
479    Dobrica Pavlinusic <dpavlin@rot13.org>
480    
481    L<http://www.rot13.org/~dpavlin/>
482    
483    =head1 LICENSE
484    
485    This product is licensed under GNU Public License (GPL) v2 or later.
486    
487    =cut
488    
 echo "cvs left in $TMPDIR"  

Legend:
Removed from v.2  
changed lines
  Added in v.31

  ViewVC Help
Powered by ViewVC 1.1.26