/[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

Annotation of /trunk/svn2cvs.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26