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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26