RSS

(root)/bzr-plugins/bzr-to-cvs : 2 : bzr-to-cvs.pl

To get this branch, use:
bzr branch /bzr-plugins/bzr-to-cvs

« back to all changes in this revision

Viewing changes to bzr-to-cvs.pl

Max Kanat-Alexander
2010-02-01 15:45:19
Revision ID: mkanat@bugzilla.org-20100201234519-3p3ftuijinumt330
Fix bugs, change configuration, and add a -v switch in preparation for
actually using this to sync the Bugzilla repos.

Show diffs side-by-side

added added

removed removed

2
2
 
3
3
use strict;
4
4
use warnings;
 
5
use Cwd qw(cwd);
5
6
use File::Temp qw(tempdir);
 
7
use File::Spec;
6
8
use Getopt::Long;
7
9
 
8
10
# This should be read-only.
9
 
use constant BZR_REPO   => 'bzr://localhost/bugzilla/';
 
11
use constant BZR_REPO   => 'http://bzr.mozilla.org/bugzilla/';
10
12
# This should be a full CVSROOT that you can commit to.
11
 
use constant CVS_REPO   => '/home/mkanat/cvs-import/mozilla-cvs/';
 
13
use constant CVS_REPO   => 'mkanat%bugzilla.org@cvs.mozilla.org:/cvsroot';
12
14
use constant CVS_MODULE => 'mozilla/webtools/bugzilla';
13
15
 
 
16
our (%switch, $verbose);
 
17
 
14
18
sub do_command {
15
 
    print join(' ', @_), "\n";
 
19
    print join(' ', @_), "\n" if $verbose;
16
20
    return system(@_);
17
21
}
18
22
 
62
66
    my ($branch) = @_;
63
67
    my $to_dir = tempdir(CLEANUP => 1);
64
68
    my @branch_args = ($branch eq 'HEAD') ? () : ('-r', $branch);
 
69
    # When using cvs in server mode, we can't specify an absolute path, so
 
70
    # we chdir to /tmp and use a relative path.
 
71
    my $cwd = cwd();
 
72
    my $temp_dir = File::Spec->tmpdir;
 
73
    chdir($temp_dir);
 
74
    $to_dir =~ s{^\Q$temp_dir\E/}{};
65
75
    do_command('cvs', '-Q', '-d', CVS_REPO, 'checkout', '-P', '-d', $to_dir,
66
76
               @branch_args, 'Bugzilla') && exit $?;
67
 
    return $to_dir;
 
77
    chdir($cwd);
 
78
    return "$temp_dir/$to_dir";
68
79
}
69
80
 
70
81
sub remove_removed_files {
76
87
            remove_cvs_file($_) foreach @files;
77
88
        }
78
89
        # We retain .cvsignore files in CVS even if we deleted them in bzr.
79
 
        elsif ($item !~ /\.cvsignore/) {
 
90
        # Also, we don't delete the .bzrrev file, of course.
 
91
        elsif ($item !~ /\.cvsignore/ and $item !~ /\.bzrrev/) {
80
92
            remove_cvs_file($item);
81
93
        }
82
94
    }
88
100
    do_command('cvs', 'rm', $file);
89
101
}
90
102
 
91
 
sub get_added_files {
92
 
    my $list = `bzr status -S`;
93
 
    my @files;
 
103
sub get_added_and_removed_files {
 
104
    my $list = `bzr status -S | grep -v '/CVS'`;
 
105
    my (@added, @removed);
94
106
    my @lines = split("\n", $list);
95
107
    foreach my $line (@lines) {
96
108
        if ($line =~ /^\s*D\s+(.+)/) {
97
 
            push(@files, $1);
 
109
            push(@added, $1);
 
110
        }
 
111
        elsif ($line =~ /^\?\s+(.+)/) {
 
112
            push(@removed, $1);
98
113
        }
99
114
    }
100
 
    return @files;
 
115
    return (\@added, \@removed);
101
116
}
102
117
 
103
118
sub get_log_message {
124
139
    do_command('cvs', @switches, 'add', @files) if @files;
125
140
}
126
141
 
127
 
our %switch;
128
 
GetOptions(\%switch, 'from=s', 'to=s', 'dry-run|n') || die $@;
 
142
GetOptions(\%switch, 'from=s', 'to=s', 'dry-run|n', 'verbose|v') || die $@;
129
143
($switch{from} && $switch{to}) or die "--from and --to must be specified.\n";
130
144
my ($from, $to) = @switch{qw(from to)};
 
145
$verbose = $switch{'verbose'};
131
146
 
132
147
my $cvs_checkout = checkout_cvs($to);
133
148
my $last_bzr_rev = get_last_rev($cvs_checkout);
144
159
 
145
160
    my $bzr_checkout = checkout_bzr($from, $last_bzr_rev);
146
161
 
147
 
    # Figure out added files by moving the .bzr directory to the CVS
148
 
    # directory and using bzr commands on the CVS directory.
 
162
    # Figure out added and removed files by moving the .bzr directory
 
163
    # to the CVS directory, and using bzr commands on the CVS directory.
149
164
    do_command("mv", "$bzr_checkout/.bzr/", "$cvs_checkout/");
150
165
    chdir($cvs_checkout);
151
 
    my @added = get_added_files();
 
166
    my ($added, $removed) = get_added_and_removed_files();
152
167
    do_command('mv', "$cvs_checkout/.bzr/", "$bzr_checkout/");
153
168
 
154
 
    # Figure out removed files by copying over the CVS stuff into bzr
155
 
    # and seeing what it says is "added".
156
 
    do_command("cp -arf $cvs_checkout/* $bzr_checkout/");
157
 
    do_command("find $bzr_checkout -depth -type d -name CVS"
158
 
                               . " -exec rm -rf {} \\;");
159
169
    chdir($bzr_checkout);
160
 
    my $removed_list = `bzr unknowns`;
161
 
    my @removed = split("\n", $removed_list);
162
 
 
163
170
    my $log_message = get_log_message($last_bzr_rev);
164
171
 
165
172
    # Copy over the bzr data into the CVS checkout in preparation for commit.
166
173
    do_command('rm', '-rf', "$bzr_checkout/.bzr/");
167
 
    do_command("cp -arf $bzr_checkout/* $cvs_checkout/");
 
174
    my @v_switch = $verbose ? ('-v') : ();
 
175
    do_command("rsync", "-a", @v_switch, "$bzr_checkout/", "$cvs_checkout/");
168
176
 
169
177
    chdir($cvs_checkout);
170
 
    remove_removed_files(@removed);
171
 
    add_added_files($bzr_checkout, $switch{'dry-run'}, @added);
 
178
    remove_removed_files(@$removed);
 
179
    add_added_files($bzr_checkout, $switch{'dry-run'}, @$added);
172
180
    my @dry_switch = $switch{'dry-run'} ? ('-n') : ();
173
181
    my $updated_rev = update_last_rev($cvs_checkout, $last_bzr_rev);
174
182
    do_command('cvs', @dry_switch, 'commit', '-m', $log_message);

Loggerhead 1.18.1 is a web-based interface for Bazaar branches