3 # This tool is copyright (c) 2005, Matthias Urlichs.
4 # It is released under the Gnu Public License, version 2.
6 # The basic idea is to aggregate CVS check-ins into related changes.
7 # Fortunately, "cvsps" does that for us; all we have to do is to parse
10 # Checking out the files is done by a single long-running CVS connection
13 # The head revision is on branch "origin" by default.
14 # You can change that with the '-o' option.
19 use File::Path qw(mkpath);
20 use File::Basename qw(basename dirname);
24 use POSIX qw(strftime dup2);
26 $SIG{'PIPE'}="IGNORE";
29 our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C);
33 Usage: ${\basename $0} # fetch/update GIT from CVS
34 [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ]
35 [ -p opts-for-cvsps ] [ -C GIT_repository ]
41 getopts("hqvo:d:p:C:") or usage();
44 @ARGV <= 1 or usage();
47 $ENV{"CVSROOT"} = $opt_d;
48 } elsif(-f 'CVS/Root') {
49 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
53 $ENV{"CVSROOT"} = $opt_d;
54 } elsif($ENV{"CVSROOT"}) {
55 $opt_d = $ENV{"CVSROOT"};
57 die "CVSROOT needs to be set";
60 my $git_tree = $opt_C;
66 } elsif (-f 'CVS/Repository') {
67 open my $f, '<', 'CVS/Repository' or
68 die 'Failed to open CVS/Repository';
76 select(STDERR); $|=1; select(STDOUT);
81 # We're only interested in connecting and downloading, so ...
84 use File::Temp qw(tempfile);
85 use POSIX qw(strftime dup2);
88 my($what,$repo,$subdir) = @_;
89 $what=ref($what) if ref($what);
92 $self->{'buffer'} = "";
96 $self->{'fullrep'} = $repo;
99 $self->{'subdir'} = $subdir;
100 $self->{'lines'} = undef;
107 my $repo = $self->{'fullrep'};
108 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
109 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
110 $user="anonymous" unless defined $user;
113 $rr2 = ":pserver:$user\@$serv:$repo";
116 my $rr = ":pserver:$user\@$serv:$port$repo";
119 open(H,$ENV{'HOME'}."/.cvspass") and do {
120 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
124 my ($w,$p) = split(/\s/,$_,2);
125 if($w eq $rr or $w eq $rr2) {
132 $pass="A" unless $pass;
134 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
135 die "Socket to $serv: $!\n" unless defined $s;
136 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
137 or die "Write to $serv: $!\n";
142 if($rep ne "I LOVE YOU\n") {
143 $rep="<unknown>" unless $rep;
144 die "AuthReply: $rep\n";
146 $self->{'socketo'} = $s;
147 $self->{'socketi'} = $s;
148 } else { # local or ext: Fork off our own cvs server.
149 my $pr = IO::Pipe->new();
150 my $pw = IO::Pipe->new();
152 die "Fork: $!\n" unless defined $pid;
154 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
156 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
158 my @cvs = ($cvs, 'server');
159 my ($local, $user, $host);
160 $local = $repo =~ s/:local://;
163 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
164 ($user, $host) = ($1, $2);
168 unshift @cvs, $rsh, '-l', $user, $host;
170 unshift @cvs, $rsh, $host;
177 dup2($pw->fileno(),0);
178 dup2($pr->fileno(),1);
185 $self->{'socketo'} = $pw;
186 $self->{'socketi'} = $pr;
188 $self->{'socketo'}->write("Root $repo\n");
190 # Trial and error says that this probably is the minimum set
191 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
193 $self->{'socketo'}->write("valid-requests\n");
194 $self->{'socketo'}->flush();
196 chomp(my $rep=$self->readline());
197 if($rep !~ s/^Valid-requests\s*//) {
198 $rep="<unknown>" unless $rep;
199 die "Expected Valid-requests from server, but got: $rep\n";
201 chomp(my $res=$self->readline());
202 die "validReply: $res\n" if $res ne "ok";
204 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
205 $self->{'repo'} = $repo;
210 return $self->{'socketi'}->getline();
214 # Request a file with a given revision.
215 # Trial and error says this is a good way to do it. :-/
216 my($self,$fn,$rev) = @_;
217 $self->{'socketo'}->write("Argument -N\n") or return undef;
218 $self->{'socketo'}->write("Argument -P\n") or return undef;
219 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
220 # -ko: Linus' version doesn't use it
221 $self->{'socketo'}->write("Argument -r\n") or return undef;
222 $self->{'socketo'}->write("Argument $rev\n") or return undef;
223 $self->{'socketo'}->write("Argument --\n") or return undef;
224 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
225 $self->{'socketo'}->write("Directory .\n") or return undef;
226 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
227 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
228 $self->{'socketo'}->write("co\n") or return undef;
229 $self->{'socketo'}->flush() or return undef;
230 $self->{'lines'} = 0;
234 # Read a line from the server.
235 # ... except that 'line' may be an entire file. ;-)
237 die "Not in lines" unless defined $self->{'lines'};
241 while(defined($line = $self->readline())) {
242 # M U gnupg-cvs-rep/AUTHORS
243 # Updated gnupg-cvs-rep/
244 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
245 # /AUTHORS/1.1///T1.1
250 if($line =~ s/^(?:Created|Updated) //) {
251 $line = $self->readline(); # path
252 $line = $self->readline(); # Entries line
253 my $mode = $self->readline(); chomp $mode;
254 $self->{'mode'} = $mode;
255 defined (my $cnt = $self->readline())
256 or die "EOF from server after 'Changed'\n";
258 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
263 my $num = $self->{'socketi'}->read($buf,$cnt);
264 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
269 } elsif($line =~ s/^ //) {
271 $res += length($line);
272 } elsif($line =~ /^M\b/) {
274 } elsif($line =~ /^Mbinary\b/) {
276 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
278 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
282 my $num = $self->{'socketi'}->read($buf,$cnt);
283 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
291 # print STDERR "S: ok (".length($res).")\n";
293 } elsif($line =~ s/^E //) {
294 # print STDERR "S: $line\n";
296 die "Unknown: $line\n";
302 my($self,$fn,$rev) = @_;
305 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
306 DIR => File::Spec->tmpdir(), UNLINK => 1);
308 $self->_file($fn,$rev) and $res = $self->_line($fh);
313 $self->_file($fn,$rev)
314 or die "No file command send\n";
315 $res = $self->_line($fh);
316 die "No input: $fn $rev\n" unless defined $res;
319 return ($name, $res);
325 my $cvs = CVSconn->new($opt_d, $cvs_tree);
330 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
331 or die "Unparseable date: $d\n";
332 my $y=$1; $y-=1900 if $y>1900;
333 return timegm($6||0,$5,$4,$3,$2-1,$y);
341 for my $x(split(//,$mode)) {
346 } elsif($x eq "u") { $um |= 0700;
347 } elsif($x eq "g") { $um |= 0070;
348 } elsif($x eq "o") { $um |= 0007;
349 } elsif($x eq "r") { $mm |= 0444;
350 } elsif($x eq "w") { $mm |= 0222;
351 } elsif($x eq "x") { $mm |= 0111;
352 } elsif($x eq "=") { # do nothing
353 } else { die "Unknown mode: $mode\n";
360 my $tmpcv = "/var/cache/cvs";
369 or mkdir($git_tree,0777)
370 or die "Could not create $git_tree: $!";
373 my $last_branch = "";
374 my $orig_branch = "";
377 my $git_dir = $ENV{"GIT_DIR"} || ".git";
378 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
379 $ENV{"GIT_DIR"} = $git_dir;
380 unless(-d $git_dir) {
381 system("git-init-db");
382 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
383 system("git-read-tree");
384 die "Cannot init an empty tree: $?\n" if $?;
386 $last_branch = $opt_o;
389 -f "$git_dir/refs/heads/$opt_o"
390 or die "Branch '$opt_o' does not exist.\n".
391 "Either use the correct '-o branch' option,\n".
392 "or import to a new repository.\n";
394 $last_branch = basename(readlink("$git_dir/HEAD"));
395 unless($last_branch) {
396 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
397 $last_branch = "master";
399 $orig_branch = $last_branch;
401 # Get the last import timestamps
402 opendir(D,"$git_dir/refs/heads");
403 while(defined(my $head = readdir(D))) {
404 next if $head =~ /^\./;
405 open(F,"$git_dir/refs/heads/$head")
406 or die "Bad head branch: $head: $!\n";
407 chomp(my $ftag = <F>);
409 open(F,"git-cat-file commit $ftag |");
411 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
412 $branch_date{$head} = $1;
421 or die "Could not create git subdir ($git_dir).\n";
423 my $pid = open(CVS,"-|");
424 die "Cannot fork: $!\n" unless defined $pid;
427 @opt = split(/,/,$opt_p) if defined $opt_p;
428 exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
429 die "Could not start cvsps: $!\n";
434 #---------------------
436 #Date: 1999/09/18 13:03:59
438 #Branch: STABLE-BRANCH-1-0
439 #Ancestor branch: HEAD
442 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
444 # README:1.57->1.57.2.1
445 # VERSION:1.96->1.96.2.1
447 #---------------------
451 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
458 @o2 = splice(@old,0,50);
463 system("git-update-cache","--force-remove","--",@o2);
464 die "Cannot remove files: $?\n" if $?;
469 @n2 = splice(@new,0,10);
474 system("git-update-cache","--add",
475 (map { ('--cacheinfo', @$_) } @n2));
476 die "Cannot add files: $?\n" if $?;
480 die "Cannot fork: $!" unless defined $pid;
482 exec("git-write-tree");
483 die "Cannot exec git-write-tree: $!\n";
485 chomp(my $tree = <C>);
487 or die "Cannot get tree id ($tree): $!\n";
489 or die "Error running git-write-tree: $?\n";
490 print "Tree ID $tree\n" if $opt_v;
493 if(open(C,"$git_dir/refs/heads/$last_branch")) {
494 chomp($parent = <C>);
496 length($parent) == 40
497 or die "Cannot get parent id ($parent): $!\n";
498 print "Parent ID $parent\n" if $opt_v;
501 my $pr = IO::Pipe->new();
502 my $pw = IO::Pipe->new();
504 die "Fork: $!\n" unless defined $pid;
508 dup2($pw->fileno(),0);
509 dup2($pr->fileno(),1);
514 @par = ("-p",$parent) if $parent;
516 "GIT_AUTHOR_NAME=$author",
517 "GIT_AUTHOR_EMAIL=$author",
518 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
519 "GIT_COMMITTER_NAME=$author",
520 "GIT_COMMITTER_EMAIL=$author",
521 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
522 "git-commit-tree", $tree,@par);
523 die "Cannot exec git-commit-tree: $!\n";
528 # compatibility with git2cvs
529 substr($logmsg,32767) = "" if length($logmsg) > 32767;
530 $logmsg =~ s/[\s\n]+\z//;
532 print $pw "$logmsg\n"
533 or die "Error writing to git-commit-tree: $!\n";
536 print "Committed patch $patchset ($branch)\n" if $opt_v;
537 chomp(my $cid = <$pr>);
539 or die "Cannot get commit id ($cid): $!\n";
540 print "Commit ID $cid\n" if $opt_v;
544 die "Error running git-commit-tree: $?\n" if $?;
546 open(C,">$git_dir/refs/heads/$branch")
547 or die "Cannot open branch $branch for update: $!\n";
549 or die "Cannot write branch $branch for update: $!\n";
551 or die "Cannot write branch $branch for update: $!\n";
554 open(C,">$git_dir/refs/tags/$tag")
555 or die "Cannot create tag $tag: $!\n";
557 or die "Cannot write tag $branch: $!\n";
559 or die "Cannot write tag $branch: $!\n";
560 print "Created tag '$tag' on '$branch'\n" if $opt_v;
566 if($state == 0 and /^-+$/) {
568 } elsif($state == 0) {
571 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
574 } elsif($state == 2 and s/^Date:\s+//) {
577 print STDERR "Could not parse date: $_\n";
582 } elsif($state == 3 and s/^Author:\s+//) {
586 } elsif($state == 4 and s/^Branch:\s+//) {
590 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
593 $ancestor = $opt_o if $ancestor eq "HEAD";
595 } elsif($state == 5) {
599 } elsif($state == 6 and s/^Tag:\s+//) {
607 } elsif($state == 7 and /^Log:/) {
610 } elsif($state == 8 and /^Members:/) {
611 $branch = $opt_o if $branch eq "HEAD";
612 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
614 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
619 if(-f "$git_dir/refs/heads/$branch") {
620 print STDERR "Branch $branch already exists!\n";
624 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
625 print STDERR "Branch $ancestor does not exist!\n";
631 unless(open(H,"> $git_dir/refs/heads/$branch")) {
632 print STDERR "Could not create branch $branch: $!\n";
637 or die "Could not write branch $branch: $!";
639 or die "Could not write branch $branch: $!";
641 if(($ancestor || $branch) ne $last_branch) {
642 print "Switching from $last_branch to $branch\n" if $opt_v;
643 system("git-read-tree","-m","$last_branch","$branch");
644 die "read-tree failed: $?\n" if $?;
646 if($branch ne $last_branch) {
647 unlink("$git_dir/HEAD");
648 symlink("refs/heads/$branch","$git_dir/HEAD");
649 $last_branch = $branch;
652 } elsif($state == 8) {
654 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
655 # VERSION:1.96->1.96.2.1
656 my $init = ($2 eq "INITIAL");
660 my ($tmpname, $size) = $cvs->file($fn,$rev);
661 print "".($init ? "New" : "Update")." $fn: $size bytes.\n" if $opt_v;
662 open my $F, '-|', "git-write-blob $tmpname"
663 or die "Cannot create object: $!\n";
668 my $mode = pmode($cvs->{'mode'});
669 push(@new,[$mode, $sha, $fn]); # may be resurrected!
670 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
674 } elsif($state == 9 and /^\s*$/) {
676 } elsif(($state == 9 or $state == 10) and /^-+$/) {
679 } elsif($state == 11 and /^-+$/) {
681 } elsif(/^-+$/) { # end of unknown-line processing
683 } elsif($state != 11) { # ignore stuff when skipping
684 print "* UNKNOWN LINE * $_\n";
687 &$commit() if $branch and $state != 11;
689 # Now switch back to the branch we were in before all of this happened
691 print "DONE; switching back to $orig_branch\n" if $opt_v;
693 $orig_branch = "master";
694 print "DONE; creating $orig_branch branch\n" if $opt_v;
695 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
696 unless -f "$git_dir/refs/heads/master";
700 system("git-read-tree",$last_branch);
701 die "read-tree failed: $?\n" if $?;
703 system('git-read-tree', $orig_branch);
704 die "read-tree failed: $?\n" if $?;
705 system('git-checkout-cache', '-a');
706 die "checkout-cache failed: $?\n" if $?;
709 unlink("$git_dir/HEAD");
710 symlink("refs/heads/$orig_branch","$git_dir/HEAD");