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: 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};
156 dup2($pw->fileno(),0);
157 dup2($pr->fileno(),1);
164 $self->{'socketo'} = $pw;
165 $self->{'socketi'} = $pr;
167 $self->{'socketo'}->write("Root $repo\n");
169 # Trial and error says that this probably is the minimum set
170 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
172 $self->{'socketo'}->write("valid-requests\n");
173 $self->{'socketo'}->flush();
175 chomp(my $rep=$self->readline());
176 if($rep !~ s/^Valid-requests\s*//) {
177 $rep="<unknown>" unless $rep;
178 die "Expected Valid-requests from server, but got: $rep\n";
180 chomp(my $res=$self->readline());
181 die "validReply: $res\n" if $res ne "ok";
183 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
184 $self->{'repo'} = $repo;
189 return $self->{'socketi'}->getline();
193 # Request a file with a given revision.
194 # Trial and error says this is a good way to do it. :-/
195 my($self,$fn,$rev) = @_;
196 $self->{'socketo'}->write("Argument -N\n") or return undef;
197 $self->{'socketo'}->write("Argument -P\n") or return undef;
198 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
199 # -ko: Linus' version doesn't use it
200 $self->{'socketo'}->write("Argument -r\n") or return undef;
201 $self->{'socketo'}->write("Argument $rev\n") or return undef;
202 $self->{'socketo'}->write("Argument --\n") or return undef;
203 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
204 $self->{'socketo'}->write("Directory .\n") or return undef;
205 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
206 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
207 $self->{'socketo'}->write("co\n") or return undef;
208 $self->{'socketo'}->flush() or return undef;
209 $self->{'lines'} = 0;
213 # Read a line from the server.
214 # ... except that 'line' may be an entire file. ;-)
216 die "Not in lines" unless defined $self->{'lines'};
220 while(defined($line = $self->readline())) {
221 # M U gnupg-cvs-rep/AUTHORS
222 # Updated gnupg-cvs-rep/
223 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
224 # /AUTHORS/1.1///T1.1
229 if($line =~ s/^(?:Created|Updated) //) {
230 $line = $self->readline(); # path
231 $line = $self->readline(); # Entries line
232 my $mode = $self->readline(); chomp $mode;
233 $self->{'mode'} = $mode;
234 defined (my $cnt = $self->readline())
235 or die "EOF from server after 'Changed'\n";
237 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
242 my $num = $self->{'socketi'}->read($buf,$cnt);
243 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
247 } elsif($line =~ s/^ //) {
249 } elsif($line =~ /^M\b/) {
251 } elsif($line =~ /^Mbinary\b/) {
253 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
255 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
259 my $num = $self->{'socketi'}->read($buf,$cnt);
260 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
267 # print STDERR "S: ok (".length($res).")\n";
269 } elsif($line =~ s/^E //) {
270 # print STDERR "S: $line\n";
272 die "Unknown: $line\n";
278 my($self,$fn,$rev) = @_;
281 if ($self->_file($fn,$rev)) {
282 $res = $self->_line();
283 return $res if defined $res;
288 $self->_file($fn,$rev)
289 or die "No file command send\n";
290 $res = $self->_line();
291 die "No input: $fn $rev\n" unless defined $res;
298 my $cvs = CVSconn->new($opt_d, $cvs_tree);
303 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
304 or die "Unparseable date: $d\n";
305 my $y=$1; $y-=1900 if $y>1900;
306 return timegm($6||0,$5,$4,$3,$2-1,$y);
314 for my $x(split(//,$mode)) {
319 } elsif($x eq "u") { $um |= 0700;
320 } elsif($x eq "g") { $um |= 0070;
321 } elsif($x eq "o") { $um |= 0007;
322 } elsif($x eq "r") { $mm |= 0444;
323 } elsif($x eq "w") { $mm |= 0222;
324 } elsif($x eq "x") { $mm |= 0111;
325 } elsif($x eq "=") { # do nothing
326 } else { die "Unknown mode: $mode\n";
333 my $tmpcv = "/var/cache/cvs";
342 or mkdir($git_tree,0777)
343 or die "Could not create $git_tree: $!";
346 my $last_branch = "";
347 my $orig_branch = "";
350 my $git_dir = $ENV{"GIT_DIR"} || ".git";
351 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
352 $ENV{"GIT_DIR"} = $git_dir;
353 unless(-d $git_dir) {
354 system("git-init-db");
355 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
356 system("git-read-tree");
357 die "Cannot init an empty tree: $?\n" if $?;
359 $last_branch = $opt_o;
362 -f "$git_dir/refs/head/$opt_o"
363 or die "Branch '$opt_o' does not exist.\n".
364 "Either use the correct '-o branch' option,\n".
365 "or import to a new repository.\n";
367 $last_branch = basename(readlink("$git_dir/HEAD"));
368 unless($last_branch) {
369 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
370 $last_branch = "master";
372 $orig_branch = $last_branch;
374 # Get the last import timestamps
375 opendir(D,"$git_dir/refs/heads");
376 while(defined(my $head = readdir(D))) {
377 next if $head =~ /^\./;
378 open(F,"$git_dir/refs/heads/$head")
379 or die "Bad head branch: $head: $!\n";
380 chomp(my $ftag = <F>);
382 open(F,"git-cat-file commit $ftag |");
384 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
385 $branch_date{$head} = $1;
394 or die "Could not create git subdir ($git_dir).\n";
396 my $pid = open(CVS,"-|");
397 die "Cannot fork: $!\n" unless defined $pid;
400 @opt = split(/,/,$opt_p) if defined $opt_p;
401 exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
402 die "Could not start cvsps: $!\n";
407 #---------------------
409 #Date: 1999/09/18 13:03:59
411 #Branch: STABLE-BRANCH-1-0
412 #Ancestor branch: HEAD
415 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
417 # README:1.57->1.57.2.1
418 # VERSION:1.96->1.96.2.1
420 #---------------------
424 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
431 @o2 = splice(@old,0,50);
436 system("git-update-cache","--force-remove","--",@o2);
437 die "Cannot remove files: $?\n" if $?;
442 @n2 = splice(@new,0,50);
447 system("git-update-cache","--add","--",@n2);
448 die "Cannot add files: $?\n" if $?;
452 die "Cannot fork: $!" unless defined $pid;
454 exec("git-write-tree");
455 die "Cannot exec git-write-tree: $!\n";
457 chomp(my $tree = <C>);
459 or die "Cannot get tree id ($tree): $!\n";
461 or die "Error running git-write-tree: $?\n";
462 print "Tree ID $tree\n" if $opt_v;
465 if(open(C,"$git_dir/refs/heads/$last_branch")) {
466 chomp($parent = <C>);
468 length($parent) == 40
469 or die "Cannot get parent id ($parent): $!\n";
470 print "Parent ID $parent\n" if $opt_v;
473 my $pr = IO::Pipe->new();
474 my $pw = IO::Pipe->new();
476 die "Fork: $!\n" unless defined $pid;
480 dup2($pw->fileno(),0);
481 dup2($pr->fileno(),1);
486 @par = ("-p",$parent) if $parent;
488 "GIT_AUTHOR_NAME=$author",
489 "GIT_AUTHOR_EMAIL=$author",
490 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
491 "GIT_COMMITTER_NAME=$author",
492 "GIT_COMMITTER_EMAIL=$author",
493 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
494 "git-commit-tree", $tree,@par);
495 die "Cannot exec git-commit-tree: $!\n";
500 # compatibility with git2cvs
501 substr($logmsg,32767) = "" if length($logmsg) > 32767;
502 $logmsg =~ s/[\s\n]+\z//;
504 print $pw "$logmsg\n"
505 or die "Error writing to git-commit-tree: $!\n";
508 print "Committed patch $patchset ($branch)\n" if $opt_v;
509 chomp(my $cid = <$pr>);
511 or die "Cannot get commit id ($cid): $!\n";
512 print "Commit ID $cid\n" if $opt_v;
516 die "Error running git-commit-tree: $?\n" if $?;
518 open(C,">$git_dir/refs/heads/$branch")
519 or die "Cannot open branch $branch for update: $!\n";
521 or die "Cannot write branch $branch for update: $!\n";
523 or die "Cannot write branch $branch for update: $!\n";
526 open(C,">$git_dir/refs/tags/$tag")
527 or die "Cannot create tag $tag: $!\n";
529 or die "Cannot write tag $branch: $!\n";
531 or die "Cannot write tag $branch: $!\n";
532 print "Created tag '$tag' on '$branch'\n" if $opt_v;
538 if($state == 0 and /^-+$/) {
540 } elsif($state == 0) {
543 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
546 } elsif($state == 2 and s/^Date:\s+//) {
549 print STDERR "Could not parse date: $_\n";
554 } elsif($state == 3 and s/^Author:\s+//) {
558 } elsif($state == 4 and s/^Branch:\s+//) {
562 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
565 $ancestor = $opt_o if $ancestor eq "HEAD";
567 } elsif($state == 5) {
571 } elsif($state == 6 and s/^Tag:\s+//) {
579 } elsif($state == 7 and /^Log:/) {
582 } elsif($state == 8 and /^Members:/) {
583 $branch = $opt_o if $branch eq "HEAD";
584 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
586 print "skip patchset $patchset: $date before $branch_date{$branch}\n";
591 if(-f "$git_dir/refs/heads/$branch") {
592 print STDERR "Branch $branch already exists!\n";
596 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
597 print STDERR "Branch $ancestor does not exist!\n";
603 unless(open(H,"> $git_dir/refs/heads/$branch")) {
604 print STDERR "Could not create branch $branch: $!\n";
609 or die "Could not write branch $branch: $!";
611 or die "Could not write branch $branch: $!";
613 if(($ancestor || $branch) ne $last_branch) {
614 print "Switching from $last_branch to $branch\n" if $opt_v;
615 system("git-read-tree","-m","-u","$last_branch","$branch");
616 die "read-tree failed: $?\n" if $?;
618 if($branch ne $last_branch) {
619 unlink("$git_dir/HEAD");
620 symlink("refs/heads/$branch","$git_dir/HEAD");
621 $last_branch = $branch;
624 } elsif($state == 8) {
626 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
627 # VERSION:1.96->1.96.2.1
628 my $init = ($2 eq "INITIAL");
632 my $data = $cvs->file($fn,$rev);
633 print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n";
634 mkpath(dirname($fn),$opt_v);
636 or die "Cannot create '$fn': $!\n";
638 or die "Cannot write to '$fn': $!\n";
640 or die "Cannot write to '$fn': $!\n";
641 chmod(pmode($cvs->{'mode'}), $fn);
642 push(@new,$fn); # may be resurrected!
643 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
647 } elsif($state == 9 and /^\s*$/) {
649 } elsif(($state == 9 or $state == 10) and /^-+$/) {
652 } elsif($state == 11 and /^-+$/) {
654 } elsif(/^-+$/) { # end of unknown-line processing
656 } elsif($state != 11) { # ignore stuff when skipping
657 print "* UNKNOWN LINE * $_\n";
660 &$commit() if $branch and $state != 11;
662 # Now switch back to the branch we were in before all of this happened
664 print "DONE; switching back to $orig_branch\n" if $opt_v;
666 $orig_branch = "master";
667 print "DONE; creating $orig_branch branch\n" if $opt_v;
668 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
669 unless -f "$git_dir/refs/heads/master";
672 system("git-read-tree","-m","-u","$last_branch","$orig_branch");
673 die "read-tree failed: $?\n" if $?;
675 unlink("$git_dir/HEAD");
676 symlink("refs/heads/$orig_branch","$git_dir/HEAD");