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;
154 dup2
($pw->fileno(),0);
155 dup2
($pr->fileno(),1);
158 exec("cvs","server");
162 $self->{'socketo'} = $pw;
163 $self->{'socketi'} = $pr;
165 $self->{'socketo'}->write("Root $repo\n");
167 # Trial and error says that this probably is the minimum set
168 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
170 $self->{'socketo'}->write("valid-requests\n");
171 $self->{'socketo'}->flush();
173 chomp(my $rep=$self->readline());
174 if($rep !~ s/^Valid-requests\s*//) {
175 $rep="<unknown>" unless $rep;
176 die "Expected Valid-requests from server, but got: $rep\n";
178 chomp(my $res=$self->readline());
179 die "validReply: $res\n" if $res ne "ok";
181 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
182 $self->{'repo'} = $repo;
187 return $self->{'socketi'}->getline();
191 # Request a file with a given revision.
192 # Trial and error says this is a good way to do it. :-/
193 my($self,$fn,$rev) = @_;
194 $self->{'socketo'}->write("Argument -N\n") or return undef;
195 $self->{'socketo'}->write("Argument -P\n") or return undef;
196 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
197 # -ko: Linus' version doesn't use it
198 $self->{'socketo'}->write("Argument -r\n") or return undef;
199 $self->{'socketo'}->write("Argument $rev\n") or return undef;
200 $self->{'socketo'}->write("Argument --\n") or return undef;
201 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
202 $self->{'socketo'}->write("Directory .\n") or return undef;
203 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
204 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
205 $self->{'socketo'}->write("co\n") or return undef;
206 $self->{'socketo'}->flush() or return undef;
207 $self->{'lines'} = 0;
211 # Read a line from the server.
212 # ... except that 'line' may be an entire file. ;-)
214 die "Not in lines" unless defined $self->{'lines'};
218 while(defined($line = $self->readline())) {
219 # M U gnupg-cvs-rep/AUTHORS
220 # Updated gnupg-cvs-rep/
221 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
222 # /AUTHORS/1.1///T1.1
227 if($line =~ s/^(?:Created|Updated) //) {
228 $line = $self->readline(); # path
229 $line = $self->readline(); # Entries line
230 my $mode = $self->readline(); chomp $mode;
231 $self->{'mode'} = $mode;
232 defined (my $cnt = $self->readline())
233 or die "EOF from server after 'Changed'\n";
235 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
240 my $num = $self->{'socketi'}->read($buf,$cnt);
241 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
245 } elsif($line =~ s/^ //) {
247 } elsif($line =~ /^M\b/) {
249 } elsif($line =~ /^Mbinary\b/) {
251 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
253 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
257 my $num = $self->{'socketi'}->read($buf,$cnt);
258 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
265 # print STDERR "S: ok (".length($res).")\n";
267 } elsif($line =~ s/^E //) {
268 # print STDERR "S: $line\n";
270 die "Unknown: $line\n";
276 my($self,$fn,$rev) = @_;
279 if ($self->_file($fn,$rev)) {
280 $res = $self->_line();
281 return $res if defined $res;
286 $self->_file($fn,$rev)
287 or die "No file command send\n";
288 $res = $self->_line();
289 die "No input: $fn $rev\n" unless defined $res;
296 my $cvs = CVSconn
->new($opt_d, $cvs_tree);
301 m
#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
302 or die "Unparseable date: $d\n";
303 my $y=$1; $y-=1900 if $y>1900;
304 return timegm
($6||0,$5,$4,$3,$2-1,$y);
312 for my $x(split(//,$mode)) {
317 } elsif($x eq "u") { $um |= 0700;
318 } elsif($x eq "g") { $um |= 0070;
319 } elsif($x eq "o") { $um |= 0007;
320 } elsif($x eq "r") { $mm |= 0444;
321 } elsif($x eq "w") { $mm |= 0222;
322 } elsif($x eq "x") { $mm |= 0111;
323 } elsif($x eq "=") { # do nothing
324 } else { die "Unknown mode: $mode\n";
331 my $tmpcv = "/var/cache/cvs";
340 or mkdir($git_tree,0777)
341 or die "Could not create $git_tree: $!";
344 my $last_branch = "";
345 my $orig_branch = "";
348 my $git_dir = $ENV{"GIT_DIR"} || ".git";
349 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
350 $ENV{"GIT_DIR"} = $git_dir;
351 unless(-d
$git_dir) {
352 system("git-init-db");
353 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
354 system("git-read-tree");
355 die "Cannot init an empty tree: $?\n" if $?
;
357 $last_branch = $opt_o;
360 -f
"$git_dir/refs/head/$opt_o"
361 or die "Branch '$opt_o' does not exist.\n".
362 "Either use the correct '-o branch' option,\n".
363 "or import to a new repository.\n";
365 $last_branch = basename
(readlink("$git_dir/HEAD"));
366 unless($last_branch) {
367 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
368 $last_branch = "master";
370 $orig_branch = $last_branch;
372 # Get the last import timestamps
373 opendir(D
,"$git_dir/refs/heads");
374 while(defined(my $head = readdir(D
))) {
375 next if $head =~ /^\./;
376 open(F
,"$git_dir/refs/heads/$head")
377 or die "Bad head branch: $head: $!\n";
378 chomp(my $ftag = <F
>);
380 open(F
,"git-cat-file commit $ftag |");
382 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
383 $branch_date{$head} = $1;
392 or die "Could not create git subdir ($git_dir).\n";
394 my $pid = open(CVS
,"-|");
395 die "Cannot fork: $!\n" unless defined $pid;
398 @opt = split(/,/,$opt_p) if defined $opt_p;
399 exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree);
400 die "Could not start cvsps: $!\n";
405 #---------------------
407 #Date: 1999/09/18 13:03:59
409 #Branch: STABLE-BRANCH-1-0
410 #Ancestor branch: HEAD
413 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
415 # README:1.57->1.57.2.1
416 # VERSION:1.96->1.96.2.1
418 #---------------------
422 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
429 @o2 = splice(@old,0,50);
434 system("git-update-cache","--force-remove","--",@o2);
435 die "Cannot remove files: $?\n" if $?
;
440 @n2 = splice(@new,0,50);
445 system("git-update-cache","--add","--",@n2);
446 die "Cannot add files: $?\n" if $?
;
450 die "Cannot fork: $!" unless defined $pid;
452 exec("git-write-tree");
453 die "Cannot exec git-write-tree: $!\n";
455 chomp(my $tree = <C
>);
457 or die "Cannot get tree id ($tree): $!\n";
459 or die "Error running git-write-tree: $?\n";
460 print "Tree ID $tree\n" if $opt_v;
463 if(open(C
,"$git_dir/refs/heads/$last_branch")) {
464 chomp($parent = <C
>);
466 length($parent) == 40
467 or die "Cannot get parent id ($parent): $!\n";
468 print "Parent ID $parent\n" if $opt_v;
471 my $pr = IO
::Pipe
->new();
472 my $pw = IO
::Pipe
->new();
474 die "Fork: $!\n" unless defined $pid;
478 dup2
($pw->fileno(),0);
479 dup2
($pr->fileno(),1);
484 @par = ("-p",$parent) if $parent;
486 "GIT_AUTHOR_NAME=$author",
487 "GIT_AUTHOR_EMAIL=$author",
488 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
489 "GIT_COMMITTER_NAME=$author",
490 "GIT_COMMITTER_EMAIL=$author",
491 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
492 "git-commit-tree", $tree,@par);
493 die "Cannot exec git-commit-tree: $!\n";
498 # compatibility with git2cvs
499 substr($logmsg,32767) = "" if length($logmsg) > 32767;
500 $logmsg =~ s/[\s\n]+\z//;
502 print $pw "$logmsg\n"
503 or die "Error writing to git-commit-tree: $!\n";
506 print "Committed patch $patchset ($branch)\n" if $opt_v;
507 chomp(my $cid = <$pr>);
509 or die "Cannot get commit id ($cid): $!\n";
510 print "Commit ID $cid\n" if $opt_v;
514 die "Error running git-commit-tree: $?\n" if $?
;
516 open(C
,">$git_dir/refs/heads/$branch")
517 or die "Cannot open branch $branch for update: $!\n";
519 or die "Cannot write branch $branch for update: $!\n";
521 or die "Cannot write branch $branch for update: $!\n";
524 open(C
,">$git_dir/refs/tags/$tag")
525 or die "Cannot create tag $tag: $!\n";
527 or die "Cannot write tag $branch: $!\n";
529 or die "Cannot write tag $branch: $!\n";
530 print "Created tag '$tag' on '$branch'\n" if $opt_v;
536 if($state == 0 and /^-+$/) {
538 } elsif($state == 0) {
541 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
544 } elsif($state == 2 and s/^Date:\s+//) {
547 print STDERR
"Could not parse date: $_\n";
552 } elsif($state == 3 and s/^Author:\s+//) {
556 } elsif($state == 4 and s/^Branch:\s+//) {
560 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
563 $ancestor = $opt_o if $ancestor eq "HEAD";
565 } elsif($state == 5) {
569 } elsif($state == 6 and s/^Tag:\s+//) {
577 } elsif($state == 7 and /^Log:/) {
580 } elsif($state == 8 and /^Members:/) {
581 $branch = $opt_o if $branch eq "HEAD";
582 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
584 print "skip patchset $patchset: $date before $branch_date{$branch}\n";
589 if(-f
"$git_dir/refs/heads/$branch") {
590 print STDERR
"Branch $branch already exists!\n";
594 unless(open(H
,"$git_dir/refs/heads/$ancestor")) {
595 print STDERR
"Branch $ancestor does not exist!\n";
601 unless(open(H
,"> $git_dir/refs/heads/$branch")) {
602 print STDERR
"Could not create branch $branch: $!\n";
607 or die "Could not write branch $branch: $!";
609 or die "Could not write branch $branch: $!";
611 if(($ancestor || $branch) ne $last_branch) {
612 print "Switching from $last_branch to $branch\n" if $opt_v;
613 system("git-read-tree","-m","-u","$last_branch","$branch");
614 die "read-tree failed: $?\n" if $?
;
616 if($branch ne $last_branch) {
617 unlink("$git_dir/HEAD");
618 symlink("refs/heads/$branch","$git_dir/HEAD");
619 $last_branch = $branch;
622 } elsif($state == 8) {
624 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
625 # VERSION:1.96->1.96.2.1
626 my $init = ($2 eq "INITIAL");
630 my $data = $cvs->file($fn,$rev);
631 print "".($init ?
"New" : "Update")." $fn: ".length($data)." bytes.\n";
632 mkpath
(dirname
($fn),$opt_v);
634 or die "Cannot create '$fn': $!\n";
636 or die "Cannot write to '$fn': $!\n";
638 or die "Cannot write to '$fn': $!\n";
639 chmod(pmode
($cvs->{'mode'}), $fn);
640 push(@new,$fn); # may be resurrected!
641 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
645 } elsif($state == 9 and /^\s*$/) {
647 } elsif(($state == 9 or $state == 10) and /^-+$/) {
650 } elsif($state == 11 and /^-+$/) {
652 } elsif(/^-+$/) { # end of unknown-line processing
654 } elsif($state != 11) { # ignore stuff when skipping
655 print "* UNKNOWN LINE * $_\n";
658 &$commit() if $branch and $state != 11;
660 # Now switch back to the branch we were in before all of this happened
662 print "DONE; switching back to $orig_branch\n" if $opt_v;
664 $orig_branch = "master";
665 print "DONE; creating $orig_branch branch\n" if $opt_v;
666 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
667 unless -f
"$git_dir/refs/heads/master";
670 system("git-read-tree","-m","-u","$last_branch","$orig_branch");
671 die "read-tree failed: $?\n" if $?
;
673 unlink("$git_dir/HEAD");
674 symlink("refs/heads/$orig_branch","$git_dir/HEAD");