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 ...
83 use POSIX qw(strftime dup2);
86 my($what,$repo,$subdir) = @_;
87 $what=ref($what) if ref($what);
90 $self->{'buffer'} = "";
94 $self->{'fullrep'} = $repo;
97 $self->{'subdir'} = $subdir;
98 $self->{'lines'} = undef;
105 my $repo = $self->{'fullrep'};
106 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
107 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
108 $user="anonymous" unless defined $user;
111 $rr2 = ":pserver:$user\@$serv:$repo";
114 my $rr = ":pserver:$user\@$serv:$port$repo";
117 open(H,$ENV{'HOME'}."/.cvspass") and do {
118 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
122 my ($w,$p) = split(/\s/,$_,2);
123 if($w eq $rr or $w eq $rr2) {
130 $pass="A" unless $pass;
132 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
133 die "Socket to $serv: $!\n" unless defined $s;
134 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
135 or die "Write to $serv: $!\n";
140 if($rep ne "I LOVE YOU\n") {
141 $rep="<unknown>" unless $rep;
142 die "AuthReply: $rep\n";
144 $self->{'socketo'} = $s;
145 $self->{'socketi'} = $s;
146 } else { # local or ext: Fork off our own cvs server.
147 my $pr = IO::Pipe->new();
148 my $pw = IO::Pipe->new();
150 die "Fork: $!\n" unless defined $pid;
152 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
154 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
156 my @cvs = ($cvs, 'server');
157 my ($local, $user, $host);
158 $local = $repo =~ s/:local://;
161 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
162 ($user, $host) = ($1, $2);
166 unshift @cvs, $rsh, '-l', $user, $host;
168 unshift @cvs, $rsh, $host;
175 dup2($pw->fileno(),0);
176 dup2($pr->fileno(),1);
183 $self->{'socketo'} = $pw;
184 $self->{'socketi'} = $pr;
186 $self->{'socketo'}->write("Root $repo\n");
188 # Trial and error says that this probably is the minimum set
189 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
191 $self->{'socketo'}->write("valid-requests\n");
192 $self->{'socketo'}->flush();
194 chomp(my $rep=$self->readline());
195 if($rep !~ s/^Valid-requests\s*//) {
196 $rep="<unknown>" unless $rep;
197 die "Expected Valid-requests from server, but got: $rep\n";
199 chomp(my $res=$self->readline());
200 die "validReply: $res\n" if $res ne "ok";
202 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
203 $self->{'repo'} = $repo;
208 return $self->{'socketi'}->getline();
212 # Request a file with a given revision.
213 # Trial and error says this is a good way to do it. :-/
214 my($self,$fn,$rev) = @_;
215 $self->{'socketo'}->write("Argument -N\n") or return undef;
216 $self->{'socketo'}->write("Argument -P\n") or return undef;
217 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
218 # -ko: Linus' version doesn't use it
219 $self->{'socketo'}->write("Argument -r\n") or return undef;
220 $self->{'socketo'}->write("Argument $rev\n") or return undef;
221 $self->{'socketo'}->write("Argument --\n") or return undef;
222 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
223 $self->{'socketo'}->write("Directory .\n") or return undef;
224 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
225 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
226 $self->{'socketo'}->write("co\n") or return undef;
227 $self->{'socketo'}->flush() or return undef;
228 $self->{'lines'} = 0;
232 # Read a line from the server.
233 # ... except that 'line' may be an entire file. ;-)
235 die "Not in lines" unless defined $self->{'lines'};
239 while(defined($line = $self->readline())) {
240 # M U gnupg-cvs-rep/AUTHORS
241 # Updated gnupg-cvs-rep/
242 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
243 # /AUTHORS/1.1///T1.1
248 if($line =~ s/^(?:Created|Updated) //) {
249 $line = $self->readline(); # path
250 $line = $self->readline(); # Entries line
251 my $mode = $self->readline(); chomp $mode;
252 $self->{'mode'} = $mode;
253 defined (my $cnt = $self->readline())
254 or die "EOF from server after 'Changed'\n";
256 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
261 my $num = $self->{'socketi'}->read($buf,$cnt);
262 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
266 } elsif($line =~ s/^ //) {
268 } elsif($line =~ /^M\b/) {
270 } elsif($line =~ /^Mbinary\b/) {
272 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
274 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
278 my $num = $self->{'socketi'}->read($buf,$cnt);
279 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
286 # print STDERR "S: ok (".length($res).")\n";
288 } elsif($line =~ s/^E //) {
289 # print STDERR "S: $line\n";
291 die "Unknown: $line\n";
297 my($self,$fn,$rev) = @_;
300 if ($self->_file($fn,$rev)) {
301 $res = $self->_line();
302 return $res if defined $res;
307 $self->_file($fn,$rev)
308 or die "No file command send\n";
309 $res = $self->_line();
310 die "No input: $fn $rev\n" unless defined $res;
317 my $cvs = CVSconn->new($opt_d, $cvs_tree);
322 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
323 or die "Unparseable date: $d\n";
324 my $y=$1; $y-=1900 if $y>1900;
325 return timegm($6||0,$5,$4,$3,$2-1,$y);
333 for my $x(split(//,$mode)) {
338 } elsif($x eq "u") { $um |= 0700;
339 } elsif($x eq "g") { $um |= 0070;
340 } elsif($x eq "o") { $um |= 0007;
341 } elsif($x eq "r") { $mm |= 0444;
342 } elsif($x eq "w") { $mm |= 0222;
343 } elsif($x eq "x") { $mm |= 0111;
344 } elsif($x eq "=") { # do nothing
345 } else { die "Unknown mode: $mode\n";
352 my $tmpcv = "/var/cache/cvs";
361 or mkdir($git_tree,0777)
362 or die "Could not create $git_tree: $!";
365 my $last_branch = "";
366 my $orig_branch = "";
369 my $git_dir = $ENV{"GIT_DIR"} || ".git";
370 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
371 $ENV{"GIT_DIR"} = $git_dir;
372 unless(-d $git_dir) {
373 system("git-init-db");
374 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
375 system("git-read-tree");
376 die "Cannot init an empty tree: $?\n" if $?;
378 $last_branch = $opt_o;
381 -f "$git_dir/refs/head/$opt_o"
382 or die "Branch '$opt_o' does not exist.\n".
383 "Either use the correct '-o branch' option,\n".
384 "or import to a new repository.\n";
386 $last_branch = basename(readlink("$git_dir/HEAD"));
387 unless($last_branch) {
388 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
389 $last_branch = "master";
391 $orig_branch = $last_branch;
393 # Get the last import timestamps
394 opendir(D,"$git_dir/refs/heads");
395 while(defined(my $head = readdir(D))) {
396 next if $head =~ /^\./;
397 open(F,"$git_dir/refs/heads/$head")
398 or die "Bad head branch: $head: $!\n";
399 chomp(my $ftag = <F>);
401 open(F,"git-cat-file commit $ftag |");
403 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
404 $branch_date{$head} = $1;
413 or die "Could not create git subdir ($git_dir).\n";
415 my $pid = open(CVS,"-|");
416 die "Cannot fork: $!\n" unless defined $pid;
419 @opt = split(/,/,$opt_p) if defined $opt_p;
420 exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
421 die "Could not start cvsps: $!\n";
426 #---------------------
428 #Date: 1999/09/18 13:03:59
430 #Branch: STABLE-BRANCH-1-0
431 #Ancestor branch: HEAD
434 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
436 # README:1.57->1.57.2.1
437 # VERSION:1.96->1.96.2.1
439 #---------------------
443 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
450 @o2 = splice(@old,0,50);
455 system("git-update-cache","--force-remove","--",@o2);
456 die "Cannot remove files: $?\n" if $?;
461 @n2 = splice(@new,0,50);
466 system("git-update-cache","--add","--",@n2);
467 die "Cannot add files: $?\n" if $?;
471 die "Cannot fork: $!" unless defined $pid;
473 exec("git-write-tree");
474 die "Cannot exec git-write-tree: $!\n";
476 chomp(my $tree = <C>);
478 or die "Cannot get tree id ($tree): $!\n";
480 or die "Error running git-write-tree: $?\n";
481 print "Tree ID $tree\n" if $opt_v;
484 if(open(C,"$git_dir/refs/heads/$last_branch")) {
485 chomp($parent = <C>);
487 length($parent) == 40
488 or die "Cannot get parent id ($parent): $!\n";
489 print "Parent ID $parent\n" if $opt_v;
492 my $pr = IO::Pipe->new();
493 my $pw = IO::Pipe->new();
495 die "Fork: $!\n" unless defined $pid;
499 dup2($pw->fileno(),0);
500 dup2($pr->fileno(),1);
505 @par = ("-p",$parent) if $parent;
507 "GIT_AUTHOR_NAME=$author",
508 "GIT_AUTHOR_EMAIL=$author",
509 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
510 "GIT_COMMITTER_NAME=$author",
511 "GIT_COMMITTER_EMAIL=$author",
512 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
513 "git-commit-tree", $tree,@par);
514 die "Cannot exec git-commit-tree: $!\n";
519 # compatibility with git2cvs
520 substr($logmsg,32767) = "" if length($logmsg) > 32767;
521 $logmsg =~ s/[\s\n]+\z//;
523 print $pw "$logmsg\n"
524 or die "Error writing to git-commit-tree: $!\n";
527 print "Committed patch $patchset ($branch)\n" if $opt_v;
528 chomp(my $cid = <$pr>);
530 or die "Cannot get commit id ($cid): $!\n";
531 print "Commit ID $cid\n" if $opt_v;
535 die "Error running git-commit-tree: $?\n" if $?;
537 open(C,">$git_dir/refs/heads/$branch")
538 or die "Cannot open branch $branch for update: $!\n";
540 or die "Cannot write branch $branch for update: $!\n";
542 or die "Cannot write branch $branch for update: $!\n";
545 open(C,">$git_dir/refs/tags/$tag")
546 or die "Cannot create tag $tag: $!\n";
548 or die "Cannot write tag $branch: $!\n";
550 or die "Cannot write tag $branch: $!\n";
551 print "Created tag '$tag' on '$branch'\n" if $opt_v;
557 if($state == 0 and /^-+$/) {
559 } elsif($state == 0) {
562 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
565 } elsif($state == 2 and s/^Date:\s+//) {
568 print STDERR "Could not parse date: $_\n";
573 } elsif($state == 3 and s/^Author:\s+//) {
577 } elsif($state == 4 and s/^Branch:\s+//) {
581 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
584 $ancestor = $opt_o if $ancestor eq "HEAD";
586 } elsif($state == 5) {
590 } elsif($state == 6 and s/^Tag:\s+//) {
598 } elsif($state == 7 and /^Log:/) {
601 } elsif($state == 8 and /^Members:/) {
602 $branch = $opt_o if $branch eq "HEAD";
603 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
605 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
610 if(-f "$git_dir/refs/heads/$branch") {
611 print STDERR "Branch $branch already exists!\n";
615 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
616 print STDERR "Branch $ancestor does not exist!\n";
622 unless(open(H,"> $git_dir/refs/heads/$branch")) {
623 print STDERR "Could not create branch $branch: $!\n";
628 or die "Could not write branch $branch: $!";
630 or die "Could not write branch $branch: $!";
632 if(($ancestor || $branch) ne $last_branch) {
633 print "Switching from $last_branch to $branch\n" if $opt_v;
634 system("git-read-tree","-m","-u","$last_branch","$branch");
635 die "read-tree failed: $?\n" if $?;
637 if($branch ne $last_branch) {
638 unlink("$git_dir/HEAD");
639 symlink("refs/heads/$branch","$git_dir/HEAD");
640 $last_branch = $branch;
643 } elsif($state == 8) {
645 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
646 # VERSION:1.96->1.96.2.1
647 my $init = ($2 eq "INITIAL");
651 my $data = $cvs->file($fn,$rev);
652 print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n" if $opt_v;
653 mkpath(dirname($fn),$opt_v);
655 or die "Cannot create '$fn': $!\n";
657 or die "Cannot write to '$fn': $!\n";
659 or die "Cannot write to '$fn': $!\n";
660 chmod(pmode($cvs->{'mode'}), $fn);
661 push(@new,$fn); # may be resurrected!
662 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
666 } elsif($state == 9 and /^\s*$/) {
668 } elsif(($state == 9 or $state == 10) and /^-+$/) {
671 } elsif($state == 11 and /^-+$/) {
673 } elsif(/^-+$/) { # end of unknown-line processing
675 } elsif($state != 11) { # ignore stuff when skipping
676 print "* UNKNOWN LINE * $_\n";
679 &$commit() if $branch and $state != 11;
681 # Now switch back to the branch we were in before all of this happened
683 print "DONE; switching back to $orig_branch\n" if $opt_v;
685 $orig_branch = "master";
686 print "DONE; creating $orig_branch branch\n" if $opt_v;
687 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
688 unless -f "$git_dir/refs/heads/master";
691 system("git-read-tree","-m","-u","$last_branch","$orig_branch");
692 die "read-tree failed: $?\n" if $?;
694 unlink("$git_dir/HEAD");
695 symlink("refs/heads/$orig_branch","$git_dir/HEAD");