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

Legend:
Removed from v.3  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26