29
29
$SIG{'PIPE'}="IGNORE";
32
our($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M,$opt_A,$opt_S,$opt_L);
32
our ($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M,$opt_A,$opt_S,$opt_L, $opt_a);
33
33
my (%conv_author_name, %conv_author_email);
37
print(STDERR "Error: $msg\n") if $msg;
36
38
print STDERR <<END;
37
39
Usage: ${\basename $0} # fetch/update GIT from CVS
38
40
[-o branch-for-HEAD] [-h] [-v] [-d CVSROOT] [-A author-conv-file]
39
[-p opts-for-cvsps] [-C GIT_repository] [-z fuzz] [-i] [-k] [-u]
40
[-s subst] [-m] [-M regex] [-S regex] [CVS_module]
41
[-p opts-for-cvsps] [-P file] [-C GIT_repository] [-z fuzz] [-i] [-k]
42
[-u] [-s subst] [-a] [-m] [-M regex] [-S regex] [-L commitlimit]
88
getopts("hivmkuo:d:p:C:z:s:M:P:A:S:L:") or usage();
91
# convert getopts specs for use by git-repo-config
92
sub read_repo_config {
93
# Split the string between characters, unless there is a ':'
94
# So "abc:de" becomes ["a", "b", "c:", "d", "e"]
95
my @opts = split(/ *(?!:)/, shift);
96
foreach my $o (@opts) {
99
my $arg = 'git-repo-config';
100
$arg .= ' --bool' if ($o !~ /:$/);
102
chomp(my $tmp = `$arg --get cvsimport.$key`);
103
if ($tmp && !($arg =~ /--bool/ && $tmp eq 'false')) {
105
my $opt_name = "opt_" . $key;
112
chomp(my $module = `git-repo-config --get cvsimport.module`);
113
push(@ARGV, $module);
117
my $opts = "haivmkuo:d:p:C:z:s:M:P:A:S:L:";
118
read_repo_config($opts);
119
getopts($opts) or usage();
91
@ARGV <= 1 or usage();
122
@ARGV <= 1 or usage("You can't specify more than one CVS module");
94
125
$ENV{"CVSROOT"} = $opt_d;
95
} elsif(-f 'CVS/Root') {
126
} elsif (-f 'CVS/Root') {
96
127
open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
100
131
$ENV{"CVSROOT"} = $opt_d;
101
} elsif($ENV{"CVSROOT"}) {
132
} elsif ($ENV{"CVSROOT"}) {
102
133
$opt_d = $ENV{"CVSROOT"};
104
die "CVSROOT needs to be set";
135
usage("CVSROOT needs to be set");
106
137
$opt_o ||= "origin";
108
141
my $git_tree = $opt_C;
109
142
$git_tree ||= ".";
129
162
push (@mergerx, qr/$opt_M/);
165
# Remember UTC of our starting time
166
# we'll want to avoid importing commits
167
# that are too recent
168
our $starttime = time();
132
170
select(STDERR); $|=1; select(STDOUT);
141
179
use POSIX qw(strftime dup2);
144
my($what,$repo,$subdir) = @_;
182
my ($what,$repo,$subdir) = @_;
145
183
$what=ref($what) if ref($what);
162
200
my $self = shift;
163
201
my $repo = $self->{'fullrep'};
164
if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
165
my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
202
if ($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
203
my ($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5);
205
my ($proxyhost,$proxyport);
206
if ($param && ($param =~ m/proxy=([^;]+)/)) {
208
# Default proxyport, if not specified, is 8080.
210
if ($ENV{"CVS_PROXY_PORT"}) {
211
$proxyport = $ENV{"CVS_PROXY_PORT"};
213
if ($param =~ m/proxyport=([^;]+)/) {
166
218
$user="anonymous" unless defined $user;
169
221
$rr2 = ":pserver:$user\@$serv:$repo";
172
224
my $rr = ":pserver:$user\@$serv:$port$repo";
175
227
open(H,$ENV{'HOME'}."/.cvspass") and do {
176
228
# :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
180
232
my ($w,$p) = split(/\s/,$_,2);
181
if($w eq $rr or $w eq $rr2) {
233
if ($w eq $rr or $w eq $rr2) {
188
240
$pass="A" unless $pass;
190
my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
191
die "Socket to $serv: $!\n" unless defined $s;
245
# Use a HTTP Proxy. Only works for HTTP proxies that
246
# don't require user authentication
248
# See: http://www.ietf.org/rfc/rfc2817.txt
250
$s = IO::Socket::INET->new(PeerHost => $proxyhost, PeerPort => $proxyport);
251
die "Socket to $proxyhost: $!\n" unless defined $s;
252
$s->write("CONNECT $serv:$port HTTP/1.1\r\nHost: $serv:$port\r\n\r\n")
253
or die "Write to $proxyhost: $!\n";
258
# The answer should look like 'HTTP/1.x 2yy ....'
259
if (!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) {
260
die "Proxy connect: $rep\n";
262
# Skip up to the empty line of the proxy server output
263
# including the response headers.
264
while ($rep = <$s>) {
265
last if (!defined $rep ||
270
$s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
271
die "Socket to $serv: $!\n" unless defined $s;
192
274
$s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
193
275
or die "Write to $serv: $!\n";
198
if($rep ne "I LOVE YOU\n") {
280
if ($rep ne "I LOVE YOU\n") {
199
281
$rep="<unknown>" unless $rep;
200
282
die "AuthReply: $rep\n";
250
332
$self->{'socketo'}->flush();
252
334
chomp(my $rep=$self->readline());
253
if($rep !~ s/^Valid-requests\s*//) {
335
if ($rep !~ s/^Valid-requests\s*//) {
254
336
$rep="<unknown>" unless $rep;
255
337
die "Expected Valid-requests from server, but got: $rep\n";
266
348
return $self->{'socketi'}->getline();
270
352
# Request a file with a given revision.
271
353
# Trial and error says this is a good way to do it. :-/
272
my($self,$fn,$rev) = @_;
354
my ($self,$fn,$rev) = @_;
273
355
$self->{'socketo'}->write("Argument -N\n") or return undef;
274
356
$self->{'socketo'}->write("Argument -P\n") or return undef;
275
357
# -kk: Linus' version doesn't use it - defaults to off
292
374
# Read a line from the server.
293
375
# ... except that 'line' may be an entire file. ;-)
376
my ($self, $fh) = @_;
295
377
die "Not in lines" unless defined $self->{'lines'};
299
while(defined($line = $self->readline())) {
381
while (defined($line = $self->readline())) {
300
382
# M U gnupg-cvs-rep/AUTHORS
301
383
# Updated gnupg-cvs-rep/
302
384
# /daten/src/rsync/gnupg-cvs-rep/AUTHORS
308
if($line =~ s/^(?:Created|Updated) //) {
390
if ($line =~ s/^(?:Created|Updated) //) {
309
391
$line = $self->readline(); # path
310
392
$line = $self->readline(); # Entries line
311
393
my $mode = $self->readline(); chomp $mode;
316
398
die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
318
400
$res = $self->_fetchfile($fh, $cnt);
319
} elsif($line =~ s/^ //) {
401
} elsif ($line =~ s/^ //) {
321
403
$res += length($line);
322
} elsif($line =~ /^M\b/) {
404
} elsif ($line =~ /^M\b/) {
323
405
# output, do nothing
324
} elsif($line =~ /^Mbinary\b/) {
406
} elsif ($line =~ /^Mbinary\b/) {
326
408
die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
330
412
$res += $self->_fetchfile($fh, $cnt);
334
416
# print STDERR "S: ok (".length($res).")\n";
336
} elsif($line =~ s/^E //) {
418
} elsif ($line =~ s/^E //) {
337
419
# print STDERR "S: $line\n";
338
} elsif($line =~ /^(Remove-entry|Removed) /i) {
420
} elsif ($line =~ /^(Remove-entry|Removed) /i) {
339
421
$line = $self->readline(); # filename
340
422
$line = $self->readline(); # OK
409
491
for my $x(split(//,$mode)) {
414
} elsif($x eq "u") { $um |= 0700;
415
} elsif($x eq "g") { $um |= 0070;
416
} elsif($x eq "o") { $um |= 0007;
417
} elsif($x eq "r") { $mm |= 0444;
418
} elsif($x eq "w") { $mm |= 0222;
419
} elsif($x eq "x") { $mm |= 0111;
420
} elsif($x eq "=") { # do nothing
496
} elsif ($x eq "u") { $um |= 0700;
497
} elsif ($x eq "g") { $um |= 0070;
498
} elsif ($x eq "o") { $um |= 0007;
499
} elsif ($x eq "r") { $mm |= 0444;
500
} elsif ($x eq "w") { $mm |= 0222;
501
} elsif ($x eq "x") { $mm |= 0111;
502
} elsif ($x eq "=") { # do nothing
421
503
} else { die "Unknown mode: $mode\n";
498
580
my $fmt = '($ref, $author) = (%(refname), %(author));';
499
581
open(H, "git-for-each-ref --perl --format='$fmt' refs/heads |") or
500
582
die "Cannot run git-for-each-ref: $!\n";
501
while(defined(my $entry = <H>)) {
583
while (defined(my $entry = <H>)) {
502
584
my ($ref, $author);
503
585
eval($entry) || die "cannot eval refs list: $@";
504
586
my ($head) = ($ref =~ m|^refs/heads/(.*)|);
524
606
# run cvsps into a file unless we are getting
525
607
# it passed as a file via $opt_P
527
610
unless ($opt_P) {
528
611
print "Running cvsps...\n" if $opt_v;
529
612
my $pid = open(CVSPS,"-|");
530
614
die "Cannot fork: $!\n" unless defined $pid;
533
617
@opt = split(/,/,$opt_p) if defined $opt_p;
534
618
unshift @opt, '-z', $opt_z if defined $opt_z;
539
623
exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
540
624
die "Could not start cvsps: $!\n";
542
my ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
543
DIR => File::Spec->tmpdir());
626
($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps',
627
DIR => File::Spec->tmpdir());
544
628
while (<CVSPS>) {
545
629
print $cvspsfh $_;
553
open(CVS, "<$opt_P") or die $!;
637
open(CVS, "<$cvspsfile") or die $!;
556
640
#---------------------
601
my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
602
my(@old,@new,@skipped,%ignorebranch);
685
my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
686
my (@old,@new,@skipped,%ignorebranch);
604
688
# commits that cvsps cannot place anywhere...
605
689
$ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
608
692
if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) {
609
693
# looks like an initial commit
610
# use the index primed by git-init-db
694
# use the index primed by git-init
611
695
$ENV{GIT_INDEX_FILE} = '.git/index';
612
696
$index{$branch} = '.git/index';
640
724
foreach my $rx (@mergerx) {
641
725
next unless $logmsg =~ $rx && $1;
642
726
my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
643
if(my $sha1 = get_headref($mparent, $git_dir)) {
727
if (my $sha1 = get_headref($mparent, $git_dir)) {
644
728
push @commit_args, '-p', $mparent;
645
729
print "Merge parent branch: $mparent\n" if $opt_v;
681
765
system("git-update-ref refs/heads/$branch $cid") == 0
682
766
or die "Cannot write branch $branch for update: $!\n";
685
my($in, $out) = ('','');
769
my ($in, $out) = ('','');
687
771
$xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
688
772
$xtag =~ tr/_/\./ if ( $opt_u );
689
773
$xtag =~ s/[\/]/$opt_s/g;
720
804
my $commitcount = 1;
723
if($state == 0 and /^-+$/) {
807
if ($state == 0 and /^-+$/) {
725
} elsif($state == 0) {
809
} elsif ($state == 0) {
728
} elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
812
} elsif (($state==0 or $state==1) and s/^PatchSet\s+//) {
729
813
$patchset = 0+$_;
731
} elsif($state == 2 and s/^Date:\s+//) {
815
} elsif ($state == 2 and s/^Date:\s+//) {
732
816
$date = pdate($_);
734
818
print STDERR "Could not parse date: $_\n";
739
} elsif($state == 3 and s/^Author:\s+//) {
823
} elsif ($state == 3 and s/^Author:\s+//) {
741
825
if (/^(.*?)\s+<(.*)>/) {
742
826
($author_name, $author_email) = ($1, $2);
747
831
$author_name = $author_email = $_;
750
} elsif($state == 4 and s/^Branch:\s+//) {
834
} elsif ($state == 4 and s/^Branch:\s+//) {
755
} elsif($state == 5 and s/^Ancestor branch:\s+//) {
839
} elsif ($state == 5 and s/^Ancestor branch:\s+//) {
758
842
$ancestor = $opt_o if $ancestor eq "HEAD";
760
} elsif($state == 5) {
844
} elsif ($state == 5) {
761
845
$ancestor = undef;
764
} elsif($state == 6 and s/^Tag:\s+//) {
848
} elsif ($state == 6 and s/^Tag:\s+//) {
850
if ($_ eq "(none)") {
772
} elsif($state == 7 and /^Log:/) {
856
} elsif ($state == 7 and /^Log:/) {
775
} elsif($state == 8 and /^Members:/) {
859
} elsif ($state == 8 and /^Members:/) {
776
860
$branch = $opt_o if $branch eq "HEAD";
777
if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
861
if (defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
779
863
print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
867
if (!$opt_a && $starttime - 300 - (defined $opt_z ? $opt_z : 300) <= $date) {
868
# skip if the commit is too recent
869
# that the cvsps default fuzz is 300s, we give ourselves another
870
# 300s just in case -- this also prevents skipping commits
871
# due to server clock drift
872
print "skip patchset $patchset: $date too recent\n" if $opt_v;
783
876
if (exists $ignorebranch{$branch}) {
784
877
print STDERR "Skipping $branch\n";
789
if($ancestor eq $branch) {
882
if ($ancestor eq $branch) {
790
883
print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
791
884
$ancestor = $opt_o;
793
if(-f "$git_dir/refs/heads/$branch") {
886
if (-f "$git_dir/refs/heads/$branch") {
794
887
print STDERR "Branch $branch already exists!\n";
798
unless(open(H,"$git_dir/refs/heads/$ancestor")) {
891
unless (open(H,"$git_dir/refs/heads/$ancestor")) {
799
892
print STDERR "Branch $ancestor does not exist!\n";
800
893
$ignorebranch{$branch} = 1;
804
897
chomp(my $id = <H>);
806
unless(open(H,"> $git_dir/refs/heads/$branch")) {
899
unless (open(H,"> $git_dir/refs/heads/$branch")) {
807
900
print STDERR "Could not create branch $branch: $!\n";
808
901
$ignorebranch{$branch} = 1;
817
910
$last_branch = $branch if $branch ne $last_branch;
819
} elsif($state == 8) {
912
} elsif ($state == 8) {
820
913
$logmsg .= "$_\n";
821
} elsif($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
914
} elsif ($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
822
915
# VERSION:1.96->1.96.2.1
823
916
my $init = ($2 eq "INITIAL");
849
942
push(@new,[$mode, $sha, $fn]); # may be resurrected!
851
944
unlink($tmpname);
852
} elsif($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
945
} elsif ($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
856
949
print "Delete $fn\n" if $opt_v;
857
} elsif($state == 9 and /^\s*$/) {
950
} elsif ($state == 9 and /^\s*$/) {
859
} elsif(($state == 9 or $state == 10) and /^-+$/) {
952
} elsif (($state == 9 or $state == 10) and /^-+$/) {
861
954
if ($opt_L && $commitcount > $opt_L) {
866
959
system("git repack -a -d");
869
} elsif($state == 11 and /^-+$/) {
871
} elsif(/^-+$/) { # end of unknown-line processing
873
} elsif($state != 11) { # ignore stuff when skipping
962
} elsif ($state == 11 and /^-+$/) {
964
} elsif (/^-+$/) { # end of unknown-line processing
966
} elsif ($state != 11) { # ignore stuff when skipping
874
967
print "* UNKNOWN LINE * $_\n";
877
970
commit() if $branch and $state != 11;
879
976
# The heuristic of repacking every 1024 commits can leave a
880
977
# lot of unpacked data. If there is more than 1MB worth of
881
978
# not-packed objects, repack once more.