/[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 35 by dpavlin, Fri Sep 7 16:39:42 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  # cvsroot directory  if ($SVNROOT !~ m,^[\w+]+:///*\w+,) {
31  my $CVSROOT="$HOME/x/cvsroot";          print "ERROR: invalid svn root $SVNROOT\n";
32  # name of cvs repository to commit to          exit 1;
33  my $CVSREP="svn2cvs";  }
34    
35  # svnroot directory  # Ensure File::Temp::END can clean up:
36  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";  
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 47  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  #  #
# Line 59  sub commit_svnrev { Line 71  sub commit_svnrev {
71    
72          die "commit_svnrev needs revision" if (! defined($rev));          die "commit_svnrev needs revision" if (! defined($rev));
73    
74          open(SVNREV,"> $CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";          open(SVNREV,"> .svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
75          print SVNREV $rev;          print SVNREV $rev;
76          close(SVNREV);          close(SVNREV);
77    
78          my $path=".svnrev";          my $path=".svnrev";
79    
80          if ($add_new) {          if ($add_new) {
81                  system "$cvs add $CVSREP/$path" || die "cvs add of $path failed: $!";                  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          }          }
   
         my $msg="subversion revision $rev commited to CVS";  
         print "$msg\n";  
         system "$cvs commit -m \"$msg\" $CVSREP/$path" || die "cvs commit of $path failed: $!";  
87  }  }
88    
89    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            my $curr_dir;
95    
96            foreach my $d (split(m#/#, $path)) {
97                    $curr_dir .= ( $curr_dir ? '/' : '') . $d;
98    
99                    next if in_entries($curr_dir);
100                    next if (-e "$curr_dir/CVS");
101    
102                    log_system("$cvs add '$curr_dir'", "cvs add of $curr_dir failed");
103            }
104    }
105    
106  # ok, now do the checkout  # ok, now do the checkout
107    eval {
108            cd_tmp;
109            log_system("$cvs -q checkout $CVSREP", "cvs checkout failed");
110    };
111    
112  log_system("$cvs -q checkout $CVSREP","cvs checkout failed");  if ($@) {
113            print <<_NEW_REP_;
114    
115  my $rev;  There is no CVS repository '$CVSREP' in your CVS. I will assume that
116    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_
121    
122            print "start import of new module [yes]: ";
123            my $in = <STDIN>;
124            cd_tmp;
125            mkdir($CVSREP) || die "can't create $CVSREP: $!";
126            cd_rep;
127    
128  # check if svnrev exists          open(SVNREV,"> .svnrev") || die "can't open $CVSREP/.svnrev: $!";
129  if (! -e "$CVSREP/.svnrev") {          print SVNREV "0";
130          print <<_USAGE_;          close(SVNREV);
131    
132            $rev = 0;
133    
134            # create new module
135            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!  Your CVS repository doesn't have .svnrev file!
151    
152  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
153  that only newer changes will be commited.  that only newer changes will be commited.
154    
155  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 158  corresponds to current version of CVS re
158  been checkouted.  been checkouted.
159    
160  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
161  last SubVersion revision. If this is initial run of conversion of  last Subversion revision. If this is initial run of conversion of
162  SubVersion repository to CVS, correct revision is 0.  Subversion repository to CVS, correct revision is 0.
163    
164  _USAGE_  _USAGE_
165    
166          print "svn revision corresponding to CVS [abort]: ";                  print "svn revision corresponding to CVS [abort]: ";
167          my $in = <STDIN>;                  my $in = <STDIN>;
168          chomp($in);                  chomp($in);
169          if ($in !~ /^\d+$/) {                  if ($in !~ /^\d+$/) {
170                  print "Aborting: revision not a number\n";                          print "Aborting: revision not a number\n";
171                  exit 1;                          exit 1;
172                    } else {
173                            $rev = $in;
174                            commit_svnrev($rev,1);  # create new
175                    }
176          } else {          } else {
177                  $rev = $in;                  open(SVNREV,".svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";
178                  commit_svnrev($rev,1);  # create new                  $rev = <SVNREV>;
179                    chomp($rev);
180                    close(SVNREV);
181          }          }
 } else {  
         open(SVNREV,"$CVSREP/.svnrev") || die "can't open $TMPDIR/$CVSREP/.svnrev: $!";  
         my $rev = <SVNREV>;  
         chomp($rev);  
         close(SVNREV);  
 }  
182    
183  print "Starting after revision $rev\n";          print "Starting after revision $rev\n";
184  $rev++;          $rev++;
185    }
186    
187    
188  #  #
# Line 129  $rev++; Line 192  $rev++;
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' ]);  
   
202    
203  =begin log_example  my $xml;
204    eval {
205            $xml = XMLin($log, ForceArray => [ 'logentry', 'path' ]);
206    };
207    
 ------------------------------------------------------------------------  
 r256 | dpavlin | 2004-03-09 13:18:17 +0100 (Tue, 09 Mar 2004) | 2 lines  
208    
209  ported r254 from hidra branch  #=begin log_example
210    #
211  =cut  #------------------------------------------------------------------------
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);  # my ($dir,$file) = dir_file($path);
226    sub dir_file($) {
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                    return ($d,$f);
236            }
237    }
238    
239    # return all files in CVS/Entries
240    sub entries($) {
241            my $dir = shift;
242            die "entries expects directory argument!" unless -d $dir;
243            my @entries;
244            open(my $fh, "$dir/CVS/Entries") || return 0;
245            while(<$fh>) {
246                    if ( m{^D/([^/]+)}, ) {
247                            my $sub_dir = $1;
248                            warn "#### entries recurse into: $dir/$sub_dir";
249                            push @entries, map { "$sub_dir/$_" } entries("$dir/$sub_dir");
250                            push @entries, $sub_dir;
251                    } elsif ( m{^/([^/]+)/} ) {
252                            push @entries, $1;
253                    } elsif ( ! m{^D$} ) {
254                            die "can't decode entries line: $_";
255                    }
256            }
257            close($fh);
258            warn "#### entries($dir) => ",join("|",@entries);
259            return @entries;
260    }
261    
262    # check if file exists in CVS/Entries
263    sub in_entries($) {
264            my $path = shift;
265            my ($dir,$file) = dir_file($path);
266            open(E, "$dir/CVS/Entries") || return 0;
267            while(<E>) {
268                    return(1) if (m,^/$file/,);
269            }
270            close(E);
271            return 0;
272    }
273    
274    cd_tmp;
275    cd_rep;
276    
277  foreach my $e (@{$xml->{'logentry'}}) {  foreach my $e (@{$xml->{'logentry'}}) {
278          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'});
279          $rev = $e->{'revision'};          $rev = $e->{'revision'};
280          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");
281    
282            # deduce name of svn directory
283            my $SVNREP = "";
284            my $tmpsvn = $SVNROOT || die "BUG: SVNROOT empty!";
285            my $tmppath = $e->{'paths'}->{'path'}->[0]->{'content'} || die "BUG: tmppath empty!";
286            do {
287                    if ($tmpsvn =~ s#(/[^/]+)/*$##) {       # vim fix
288                            $SVNREP = $1 . $SVNREP;
289                    } elsif ($e->{'paths'}->{'path'}->[0]->{'copyfrom-path'}) {
290                            print "NOTICE: copyfrom outside synced repository ignored - skipping\n";
291                            next;
292                    } else {
293                            print "NOTICE: can't deduce svn dir from $SVNROOT - skipping\n";
294                            next;
295                    }
296            } until ($tmppath =~ m/^$SVNREP/);
297    
298            print "NOTICE: using $SVNREP as directory for svn\n";
299    
300          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});          printf($fmt, $e->{'revision'}, $e->{'author'}, $e->{'date'}, $e->{'msg'});
301            my @commit;
302    
303            my $msg = $e->{'msg'};
304            $msg =~ s/'/'\\''/g;    # quote "
305    
306            sub cvs_commit {
307                    my $msg = shift || die "no msg?";
308                    if ( ! @_ ) {
309                            warn "commit ignored, no files\n";
310                            return;
311                    }
312                    log_system("$cvs commit -m '$msg' '".join("' '",@_)."'", "cvs commit of ".join(",",@_)." failed");
313            }
314    
315          foreach my $p (@{$e->{'paths'}->{'path'}}) {          foreach my $p (@{$e->{'paths'}->{'path'}}) {
316                  my ($action,$path) = ($p->{'action'},$p->{'content'});                  my ($action,$path) = ($p->{'action'},$p->{'content'});
317    
318                    next if ($path =~ m#/\.svnrev$#);
319    
320                  print "svn2cvs: $action $path\n";                  print "svn2cvs: $action $path\n";
321    
322                  # prepare path and message                  # prepare path and message
323                  my $file = $path;                  my $file = $path;
324                  $path =~ s,^/$SVNREP/*,, || die "BUG: can't strip SVNREP from path";                  if ( $path !~ s#^\Q$SVNREP\E/*## ) {
325                            print "NOTICE: skipping '$path' which isn't under repository root '$SVNREP'\n";
326                            die unless $partial_import;
327                            next;
328                    }
329    
330                    if (! $path) {
331                            print "NOTICE: skipped this operation. Probably trunk creation\n";
332                            next;
333                    }
334    
335                  my $msg = $e->{'msg'};                  my $msg = $e->{'msg'};
336                  $msg =~ s/"/\\"/g;      # quote "                  $msg =~ s/'/'\\''/g;    # quote "
337    
338                    sub add_path {
339                            my $path = shift || die "no path?";
340            
341                            if (-d $path) {
342                                    add_dir($path, $msg);
343                            } elsif ($path =~ m,^(.+)/[^/]+$, && ! -e "$1/CVS/Root") {
344                                    my $dir = $1;
345                                    in_entries($dir) || add_dir($dir, $msg);
346                                    in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed");
347                            } else {
348                                    in_entries($path) || log_system("$cvs add '$path'", "cvs add of $path failed");
349                            }
350                    }
351    
352                  if ($action =~ /M/) {                  if ($action =~ /M/) {
353                          print "svn2cvs: modify $path -- nop\n";                          if ( in_entries( $path ) ) {
354                                    print "svn2cvs: modify $path -- nop\n";
355                            } else {
356                                    print "WARNING: modify $path which isn't in CVS, adding...\n";
357                                    add_path($path);
358                            }
359                  } elsif ($action =~ /A/) {                  } elsif ($action =~ /A/) {
360                          log_system("$cvs add -m \"$msg\" $CVSREP/$path", "cvs add of $path failed");                          add_path($path);
361                  } elsif ($action =~ /D/) {                  } elsif ($action =~ /D/) {
362                          log_system("$cvs delete -m \"$msg\" $CVSREP/$path", "cvs delete of $path failed");                          if (-e $path) {
363                                    if ( -d $path ) {
364                                            warn "#### remove directory: $path";
365                                            my @sub_commit;
366                                            foreach my $f ( entries($path) ) {
367                                                    $f = "$path/$f";
368                                            if ( -f $f ) {
369                                                            unlink($f) || die "can't delete file $f: $!";
370    #                                               } else {
371    #                                                       rmtree($f) || die "can't delete dir $f: $!";
372                                                    }
373                                                    log_system("$cvs delete '$f'", "cvs delete of file $f failed");
374                                                    push @sub_commit, $f;
375                                            }
376                                            log_system("$cvs delete '$path'", "cvs delete of file $path failed");
377                                            cvs_commit($msg, @sub_commit, $path);
378                                            log_system("$cvs update -dP '$path'", "cvs update -dP $path failed");
379                                    } else {
380                                            warn "#### remove file: $path";
381                                            unlink($path) || die "can't delete $path: $!";
382                                            log_system("$cvs delete '$path'", "cvs delete of dir $path failed");
383                                    }
384                            } else {
385                                    print "WARNING: $path is not present, skipping...\n";
386                                    undef $path;
387                            }
388                  } else {                  } else {
389                          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";
390                  }                  }
391    
392                  # now commit changes                  # save commits for later
393                  log_system("$cvs commit -m \"$msg\" $CVSREP/$path", "cvs commit of $path failed");                  push @commit, $path if ($path);
394    
395          }          }
396    
397            # now commit changes
398            cvs_commit($msg, @commit);
399    
400          commit_svnrev($rev);          commit_svnrev($rev);
401  }  }
402    
403    # cd out of $CVSREP before File::Temp::END is called
404    chdir("/tmp") || die "can't cd to /tmp: $!";
405    
406  __END__  __END__
407    
408  svn export --force "$SVNROOT/$SVNREP" "$CVSREP"  =pod
409    
410  cd dotfiles  =head1 NAME
411    
412  for file in $(find -type f -not -path \*CVS\*); do  svn2cvs - save subversion commits to (read-only) cvs repository
413          FILE=$(basename $file)  
414          DIR=$(dirname $file)  =head1 SYNOPSIS
415          if ! grep -q "^/$FILE/" $DIR/CVS/Entries ; then  
416                  cvs add $file    ./svn2cvs.pl SVN_URL CVSROOT CVSREPOSITORY
417          fi  
418  done  Usage example (used to self-host this script):
419    
420  #cvs commit -m "Automatic commit from SVN"    ./svn2cvs.pl file:///home/dpavlin/private/svn/svn2cvs/trunk/ \
421                   :pserver:dpavlin@cvs.tigris.org:/cvs svn2cvs/src
422  #rm -rf $TMPDIR  
423    =head1 DESCRIPTION
424    
425    This script will allows you to commit changes made to Subversion repository to
426    (read-only) CVS repository manually or from Subversion's C<post-commit> hook.
427    
428    It's using F<.svnrev> file (which will be created on first run) in
429    B<CVSROOT/CVSREPOSITORY> to store last Subversion revision which was
430    committed into CVS.
431    
432    One run will do following things:
433    
434    =over 4
435    
436    =item *
437    checkout B<CVSREPOSITORY> from B<CVSROOT> to temporary directory
438    
439    =item *
440    check if F<.svnrev> file exists and create it if it doesn't
441    
442    =item *
443    loop through all revisions from current in B<CVSROOT/CVSREPOSITORY> (using
444    F<.svnrev>) up to B<HEAD> (current one)
445    
446    =over 5
447    
448    =item *
449    checkout next Subversion revision from B<SVN_URL> over CVS checkout
450    temporary directory
451    
452    =item *
453    make modification (add and/or delete) done in that revision
454    
455    =item *
456    commit modification (added, deleted or modified files/dirs) while
457    preserving original message from CVS
458    
459    =item *
460    update F<.svnrev> to match current revision
461    
462    =back
463    
464    =item *
465    cleanup temporary directory
466    
467    =back
468    
469    If checkout fails for some reason (e.g. flaky ssh connection), you will
470    still have valid CVS repository, so all you have to do is run B<svn2cvs.pl>
471    again.
472    
473    =head1 WARNINGS
474    
475    "Cheap" copy operations in Subversion are not at all cheap in CVS. They will
476    create multiple copies of files in CVS repository!
477    
478    This script assume that you want to sync your C<trunk> (or any other
479    directory for that matter) directory with CVS, not root of your subversion.
480    This might be considered bug, but since common practise is to have
481    directories C<trunk> and C<branches> in svn and source code in them, it's
482    not serious limitation.
483    
484    =head1 RELATED PROJECTS
485    
486    B<Subversion> L<http://subversion.tigris.org/> version control system that is a
487    compelling replacement for CVS in the open source community.
488    
489    B<cvs2svn> L<http://cvs2svn.tigris.org/> converts a CVS repository to a
490    Subversion repository. It is designed for one-time conversions, not for
491    repeated synchronizations between CVS and Subversion.
492    
493    =head1 CHANGES
494    
495    Versions of this utility are actually Subversion repository revisions,
496    so they might not be in sequence.
497    
498    =over 3
499    
500    =item r10
501    
502    First release available to public
503    
504    =item r15
505    
506    Addition of comprehensive documentation, fixes for quoting in commit
507    messages, and support for skipping changes which are not under current
508    Subversion checkout root (e.g. branches).
509    
510    =item r18
511    
512    Support for importing your svn into empty CVS repository (it will first
513    create module and than dump all revisions).
514    Group commit operations to save round-trips to CVS server.
515    Documentation improvements and other small fixes.
516    
517    =item r20
518    
519    Fixed path deduction (overlap between Subversion reporistory and CVS checkout).
520    
521    =item r21
522    
523    Use C<update -d> instead of checkout after import.
524    Added fixes by Paul Egan <paulegan@mail.com> for XMLin and fixing working
525    directory.
526    
527    =item r22
528    
529    Rewritten import from revision 0 to empty repository, better importing
530    of deep directory structures, initial support for recovery from partial
531    commit.
532    
533    =back
534    
535    =head1 AUTHOR
536    
537    Dobrica Pavlinusic <dpavlin@rot13.org>
538    
539    L<https://www.rot13.org/~dpavlin/>
540    
541    =head1 LICENSE
542    
543    This product is licensed under GNU Public License (GPL) v2 or later.
544    
545    =cut
546    
 echo "cvs left in $TMPDIR"  

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

  ViewVC Help
Powered by ViewVC 1.1.26