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

Legend:
Removed from v.1  
changed lines
  Added in v.38

  ViewVC Help
Powered by ViewVC 1.1.26