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.
20 use File
::Temp
qw(tempfile);
21 use File
::Path
qw(mkpath);
22 use File
::Basename
qw(basename dirname);
26 use POSIX
qw(strftime dup2);
28 $SIG{'PIPE'}="IGNORE";
31 our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C);
35 Usage: ${\basename $0} # fetch/update GIT from CVS
36 [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ]
37 [ -p opts-for-cvsps ] [ -C GIT_repository ]
43 getopts
("hqvo:d:p:C:") or usage
();
46 @ARGV <= 1 or usage
();
49 $ENV{"CVSROOT"} = $opt_d;
50 } elsif(-f
'CVS/Root') {
51 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
55 $ENV{"CVSROOT"} = $opt_d;
56 } elsif($ENV{"CVSROOT"}) {
57 $opt_d = $ENV{"CVSROOT"};
59 die "CVSROOT needs to be set";
62 my $git_tree = $opt_C;
68 } elsif (-f
'CVS/Repository') {
69 open my $f, '<', 'CVS/Repository' or
70 die 'Failed to open CVS/Repository';
78 select(STDERR
); $|=1; select(STDOUT
);
83 # We're only interested in connecting and downloading, so ...
86 use File
::Temp
qw(tempfile);
87 use POSIX
qw(strftime dup2);
90 my($what,$repo,$subdir) = @_;
91 $what=ref($what) if ref($what);
94 $self->{'buffer'} = "";
98 $self->{'fullrep'} = $repo;
101 $self->{'subdir'} = $subdir;
102 $self->{'lines'} = undef;
109 my $repo = $self->{'fullrep'};
110 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
111 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
112 $user="anonymous" unless defined $user;
115 $rr2 = ":pserver:$user\@$serv:$repo";
118 my $rr = ":pserver:$user\@$serv:$port$repo";
121 open(H
,$ENV{'HOME'}."/.cvspass") and do {
122 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
126 my ($w,$p) = split(/\s/,$_,2);
127 if($w eq $rr or $w eq $rr2) {
134 $pass="A" unless $pass;
136 my $s = IO
::Socket
::INET
->new(PeerHost
=> $serv, PeerPort
=> $port);
137 die "Socket to $serv: $!\n" unless defined $s;
138 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
139 or die "Write to $serv: $!\n";
144 if($rep ne "I LOVE YOU\n") {
145 $rep="<unknown>" unless $rep;
146 die "AuthReply: $rep\n";
148 $self->{'socketo'} = $s;
149 $self->{'socketi'} = $s;
150 } else { # local or ext: Fork off our own cvs server.
151 my $pr = IO
::Pipe
->new();
152 my $pw = IO
::Pipe
->new();
154 die "Fork: $!\n" unless defined $pid;
156 $cvs = $ENV{CVS_SERVER
} if exists $ENV{CVS_SERVER
};
158 $rsh = $ENV{CVS_RSH
} if exists $ENV{CVS_RSH
};
160 my @cvs = ($cvs, 'server');
161 my ($local, $user, $host);
162 $local = $repo =~ s/:local://;
165 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
166 ($user, $host) = ($1, $2);
170 unshift @cvs, $rsh, '-l', $user, $host;
172 unshift @cvs, $rsh, $host;
179 dup2
($pw->fileno(),0);
180 dup2
($pr->fileno(),1);
187 $self->{'socketo'} = $pw;
188 $self->{'socketi'} = $pr;
190 $self->{'socketo'}->write("Root $repo\n");
192 # Trial and error says that this probably is the minimum set
193 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
195 $self->{'socketo'}->write("valid-requests\n");
196 $self->{'socketo'}->flush();
198 chomp(my $rep=$self->readline());
199 if($rep !~ s/^Valid-requests\s*//) {
200 $rep="<unknown>" unless $rep;
201 die "Expected Valid-requests from server, but got: $rep\n";
203 chomp(my $res=$self->readline());
204 die "validReply: $res\n" if $res ne "ok";
206 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
207 $self->{'repo'} = $repo;
212 return $self->{'socketi'}->getline();
216 # Request a file with a given revision.
217 # Trial and error says this is a good way to do it. :-/
218 my($self,$fn,$rev) = @_;
219 $self->{'socketo'}->write("Argument -N\n") or return undef;
220 $self->{'socketo'}->write("Argument -P\n") or return undef;
221 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
222 # -ko: Linus' version doesn't use it
223 $self->{'socketo'}->write("Argument -r\n") or return undef;
224 $self->{'socketo'}->write("Argument $rev\n") or return undef;
225 $self->{'socketo'}->write("Argument --\n") or return undef;
226 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
227 $self->{'socketo'}->write("Directory .\n") or return undef;
228 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
229 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
230 $self->{'socketo'}->write("co\n") or return undef;
231 $self->{'socketo'}->flush() or return undef;
232 $self->{'lines'} = 0;
236 # Read a line from the server.
237 # ... except that 'line' may be an entire file. ;-)
239 die "Not in lines" unless defined $self->{'lines'};
243 while(defined($line = $self->readline())) {
244 # M U gnupg-cvs-rep/AUTHORS
245 # Updated gnupg-cvs-rep/
246 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
247 # /AUTHORS/1.1///T1.1
252 if($line =~ s/^(?:Created|Updated) //) {
253 $line = $self->readline(); # path
254 $line = $self->readline(); # Entries line
255 my $mode = $self->readline(); chomp $mode;
256 $self->{'mode'} = $mode;
257 defined (my $cnt = $self->readline())
258 or die "EOF from server after 'Changed'\n";
260 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
265 my $num = $self->{'socketi'}->read($buf,$cnt);
266 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
271 } elsif($line =~ s/^ //) {
273 $res += length($line);
274 } elsif($line =~ /^M\b/) {
276 } elsif($line =~ /^Mbinary\b/) {
278 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
280 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
284 my $num = $self->{'socketi'}->read($buf,$cnt);
285 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
293 # print STDERR "S: ok (".length($res).")\n";
295 } elsif($line =~ s/^E //) {
296 # print STDERR "S: $line\n";
298 die "Unknown: $line\n";
304 my($self,$fn,$rev) = @_;
307 my ($fh, $name) = tempfile
('gitcvs.XXXXXX',
308 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
310 $self->_file($fn,$rev) and $res = $self->_line($fh);
315 $self->_file($fn,$rev)
316 or die "No file command send\n";
317 $res = $self->_line($fh);
318 die "No input: $fn $rev\n" unless defined $res;
321 return ($name, $res);
327 my $cvs = CVSconn
->new($opt_d, $cvs_tree);
332 m
#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
333 or die "Unparseable date: $d\n";
334 my $y=$1; $y-=1900 if $y>1900;
335 return timegm
($6||0,$5,$4,$3,$2-1,$y);
343 for my $x(split(//,$mode)) {
348 } elsif($x eq "u") { $um |= 0700;
349 } elsif($x eq "g") { $um |= 0070;
350 } elsif($x eq "o") { $um |= 0007;
351 } elsif($x eq "r") { $mm |= 0444;
352 } elsif($x eq "w") { $mm |= 0222;
353 } elsif($x eq "x") { $mm |= 0111;
354 } elsif($x eq "=") { # do nothing
355 } else { die "Unknown mode: $mode\n";
362 my $tmpcv = "/var/cache/cvs";
371 or mkdir($git_tree,0777)
372 or die "Could not create $git_tree: $!";
375 my $last_branch = "";
376 my $orig_branch = "";
379 my $git_dir = $ENV{"GIT_DIR"} || ".git";
380 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
381 $ENV{"GIT_DIR"} = $git_dir;
383 $orig_git_index = $ENV{GIT_INDEX_FILE
} if exists $ENV{GIT_INDEX_FILE
};
384 my ($git_ih, $git_index) = tempfile
('gitXXXXXX', SUFFIX
=> '.idx',
385 DIR
=> File
::Spec
->tmpdir());
387 $ENV{GIT_INDEX_FILE
} = $git_index;
388 unless(-d
$git_dir) {
389 system("git-init-db");
390 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
391 system("git-read-tree");
392 die "Cannot init an empty tree: $?\n" if $?
;
394 $last_branch = $opt_o;
397 -f
"$git_dir/refs/heads/$opt_o"
398 or die "Branch '$opt_o' does not exist.\n".
399 "Either use the correct '-o branch' option,\n".
400 "or import to a new repository.\n";
402 $last_branch = basename
(readlink("$git_dir/HEAD"));
403 unless($last_branch) {
404 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
405 $last_branch = "master";
407 $orig_branch = $last_branch;
410 system('git-read-tree', $last_branch);
412 # Get the last import timestamps
413 opendir(D
,"$git_dir/refs/heads");
414 while(defined(my $head = readdir(D
))) {
415 next if $head =~ /^\./;
416 open(F
,"$git_dir/refs/heads/$head")
417 or die "Bad head branch: $head: $!\n";
418 chomp(my $ftag = <F
>);
420 open(F
,"git-cat-file commit $ftag |");
422 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
423 $branch_date{$head} = $1;
432 or die "Could not create git subdir ($git_dir).\n";
434 my $pid = open(CVS
,"-|");
435 die "Cannot fork: $!\n" unless defined $pid;
438 @opt = split(/,/,$opt_p) if defined $opt_p;
439 exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
440 die "Could not start cvsps: $!\n";
445 #---------------------
447 #Date: 1999/09/18 13:03:59
449 #Branch: STABLE-BRANCH-1-0
450 #Ancestor branch: HEAD
453 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
455 # README:1.57->1.57.2.1
456 # VERSION:1.96->1.96.2.1
458 #---------------------
462 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
469 @o2 = splice(@old,0,50);
474 system("git-update-cache","--force-remove","--",@o2);
475 die "Cannot remove files: $?\n" if $?
;
480 @n2 = splice(@new,0,10);
485 system("git-update-cache","--add",
486 (map { ('--cacheinfo', @
$_) } @n2));
487 die "Cannot add files: $?\n" if $?
;
491 die "Cannot fork: $!" unless defined $pid;
493 exec("git-write-tree");
494 die "Cannot exec git-write-tree: $!\n";
496 chomp(my $tree = <C
>);
498 or die "Cannot get tree id ($tree): $!\n";
500 or die "Error running git-write-tree: $?\n";
501 print "Tree ID $tree\n" if $opt_v;
504 if(open(C
,"$git_dir/refs/heads/$last_branch")) {
505 chomp($parent = <C
>);
507 length($parent) == 40
508 or die "Cannot get parent id ($parent): $!\n";
509 print "Parent ID $parent\n" if $opt_v;
512 my $pr = IO
::Pipe
->new();
513 my $pw = IO
::Pipe
->new();
515 die "Fork: $!\n" unless defined $pid;
519 dup2
($pw->fileno(),0);
520 dup2
($pr->fileno(),1);
525 @par = ("-p",$parent) if $parent;
527 "GIT_AUTHOR_NAME=$author",
528 "GIT_AUTHOR_EMAIL=$author",
529 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
530 "GIT_COMMITTER_NAME=$author",
531 "GIT_COMMITTER_EMAIL=$author",
532 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
533 "git-commit-tree", $tree,@par);
534 die "Cannot exec git-commit-tree: $!\n";
539 # compatibility with git2cvs
540 substr($logmsg,32767) = "" if length($logmsg) > 32767;
541 $logmsg =~ s/[\s\n]+\z//;
543 print $pw "$logmsg\n"
544 or die "Error writing to git-commit-tree: $!\n";
547 print "Committed patch $patchset ($branch)\n" if $opt_v;
548 chomp(my $cid = <$pr>);
550 or die "Cannot get commit id ($cid): $!\n";
551 print "Commit ID $cid\n" if $opt_v;
555 die "Error running git-commit-tree: $?\n" if $?
;
557 open(C
,">$git_dir/refs/heads/$branch")
558 or die "Cannot open branch $branch for update: $!\n";
560 or die "Cannot write branch $branch for update: $!\n";
562 or die "Cannot write branch $branch for update: $!\n";
565 open(C
,">$git_dir/refs/tags/$tag")
566 or die "Cannot create tag $tag: $!\n";
568 or die "Cannot write tag $branch: $!\n";
570 or die "Cannot write tag $branch: $!\n";
571 print "Created tag '$tag' on '$branch'\n" if $opt_v;
577 if($state == 0 and /^-+$/) {
579 } elsif($state == 0) {
582 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
585 } elsif($state == 2 and s/^Date:\s+//) {
588 print STDERR
"Could not parse date: $_\n";
593 } elsif($state == 3 and s/^Author:\s+//) {
597 } elsif($state == 4 and s/^Branch:\s+//) {
601 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
604 $ancestor = $opt_o if $ancestor eq "HEAD";
606 } elsif($state == 5) {
610 } elsif($state == 6 and s/^Tag:\s+//) {
618 } elsif($state == 7 and /^Log:/) {
621 } elsif($state == 8 and /^Members:/) {
622 $branch = $opt_o if $branch eq "HEAD";
623 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
625 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
630 if(-f
"$git_dir/refs/heads/$branch") {
631 print STDERR
"Branch $branch already exists!\n";
635 unless(open(H
,"$git_dir/refs/heads/$ancestor")) {
636 print STDERR
"Branch $ancestor does not exist!\n";
642 unless(open(H
,"> $git_dir/refs/heads/$branch")) {
643 print STDERR
"Could not create branch $branch: $!\n";
648 or die "Could not write branch $branch: $!";
650 or die "Could not write branch $branch: $!";
652 if(($ancestor || $branch) ne $last_branch) {
653 print "Switching from $last_branch to $branch\n" if $opt_v;
654 system("git-read-tree","-m","$last_branch","$branch");
655 die "read-tree failed: $?\n" if $?
;
658 } elsif($state == 8) {
660 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
661 # VERSION:1.96->1.96.2.1
662 my $init = ($2 eq "INITIAL");
666 my ($tmpname, $size) = $cvs->file($fn,$rev);
667 print "".($init ?
"New" : "Update")." $fn: $size bytes.\n" if $opt_v;
668 open my $F, '-|', "git-write-blob $tmpname"
669 or die "Cannot create object: $!\n";
674 my $mode = pmode
($cvs->{'mode'});
675 push(@new,[$mode, $sha, $fn]); # may be resurrected!
676 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
680 } elsif($state == 9 and /^\s*$/) {
682 } elsif(($state == 9 or $state == 10) and /^-+$/) {
685 } elsif($state == 11 and /^-+$/) {
687 } elsif(/^-+$/) { # end of unknown-line processing
689 } elsif($state != 11) { # ignore stuff when skipping
690 print "* UNKNOWN LINE * $_\n";
693 &$commit() if $branch and $state != 11;
697 # Now switch back to the branch we were in before all of this happened
699 print "DONE\n" if $opt_v;
701 $orig_branch = "master";
702 print "DONE; creating $orig_branch branch\n" if $opt_v;
703 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
704 unless -f
"$git_dir/refs/heads/master";
705 unlink("$git_dir/HEAD");
706 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
707 if (defined $orig_git_index) {
708 $ENV{GIT_INDEX_FILE
} = $orig_git_index;
710 delete $ENV{GIT_INDEX_FILE
};
712 system('git checkout');
713 die "checkout failed: $?\n" if $?
;