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);
33 Usage: ${\basename $0} # fetch/update GIT from CVS
34 [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ]
35 CVS_module [ GIT_repository ]
40 getopts
("hqvo:d:") or usage
();
43 @ARGV == 1 or @ARGV == 2 or usage
();
45 my($cvs_tree, $git_tree) = @ARGV;
48 $ENV{"CVSROOT"} = $opt_d;
49 } elsif($ENV{"CVSROOT"}) {
50 $opt_d = $ENV{"CVSROOT"};
52 die "CVSROOT needs to be set";
57 select(STDERR
); $|=1; select(STDOUT
);
62 # We're only interested in connecting and downloading, so ...
64 use POSIX
qw(strftime dup2);
67 my($what,$repo,$subdir) = @_;
68 $what=ref($what) if ref($what);
71 $self->{'buffer'} = "";
75 $self->{'fullrep'} = $repo;
78 $self->{'subdir'} = $subdir;
79 $self->{'lines'} = undef;
86 my $repo = $self->{'fullrep'};
87 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
88 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
89 $user="anonymous" unless defined $user;
92 $rr2 = ":pserver:$user\@$serv:$repo";
95 my $rr = ":pserver:$user\@$serv:$port$repo";
98 open(H
,$ENV{'HOME'}."/.cvspass") and do {
99 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
103 my ($w,$p) = split(/\s/,$_,2);
104 if($w eq $rr or $w eq $rr2) {
111 $pass="A" unless $pass;
113 my $s = IO
::Socket
::INET
->new(PeerHost
=> $serv, PeerPort
=> $port);
114 die "Socket to $serv: $!\n" unless defined $s;
115 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
116 or die "Write to $serv: $!\n";
121 if($rep ne "I LOVE YOU\n") {
122 $rep="<unknown>" unless $rep;
123 die "AuthReply: $rep\n";
125 $self->{'socketo'} = $s;
126 $self->{'socketi'} = $s;
127 } else { # local: Fork off our own cvs server.
128 my $pr = IO
::Pipe
->new();
129 my $pw = IO
::Pipe
->new();
131 die "Fork: $!\n" unless defined $pid;
135 dup2
($pw->fileno(),0);
136 dup2
($pr->fileno(),1);
139 exec("cvs","server");
143 $self->{'socketo'} = $pw;
144 $self->{'socketi'} = $pr;
146 $self->{'socketo'}->write("Root $repo\n");
148 # Trial and error says that this probably is the minimum set
149 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
151 $self->{'socketo'}->write("valid-requests\n");
152 $self->{'socketo'}->flush();
154 chomp(my $rep=$self->readline());
155 if($rep !~ s/^Valid-requests\s*//) {
156 $rep="<unknown>" unless $rep;
157 die "Expected Valid-requests from server, but got: $rep\n";
159 chomp(my $res=$self->readline());
160 die "validReply: $res\n" if $res ne "ok";
162 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
163 $self->{'repo'} = $repo;
168 return $self->{'socketi'}->getline();
172 # Request a file with a given revision.
173 # Trial and error says this is a good way to do it. :-/
174 my($self,$fn,$rev) = @_;
175 $self->{'socketo'}->write("Argument -N\n") or return undef;
176 $self->{'socketo'}->write("Argument -P\n") or return undef;
177 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
178 # -ko: Linus' version doesn't use it
179 $self->{'socketo'}->write("Argument -r\n") or return undef;
180 $self->{'socketo'}->write("Argument $rev\n") or return undef;
181 $self->{'socketo'}->write("Argument --\n") or return undef;
182 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
183 $self->{'socketo'}->write("Directory .\n") or return undef;
184 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
185 $self->{'socketo'}->write("Sticky T1.1\n") or return undef;
186 $self->{'socketo'}->write("co\n") or return undef;
187 $self->{'socketo'}->flush() or return undef;
188 $self->{'lines'} = 0;
192 # Read a line from the server.
193 # ... except that 'line' may be an entire file. ;-)
195 die "Not in lines" unless defined $self->{'lines'};
199 while(defined($line = $self->readline())) {
200 # M U gnupg-cvs-rep/AUTHORS
201 # Updated gnupg-cvs-rep/
202 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
203 # /AUTHORS/1.1///T1.1
208 if($line =~ s/^(?:Created|Updated) //) {
209 $line = $self->readline(); # path
210 $line = $self->readline(); # Entries line
211 my $mode = $self->readline(); chomp $mode;
212 $self->{'mode'} = $mode;
213 defined (my $cnt = $self->readline())
214 or die "EOF from server after 'Changed'\n";
216 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
221 my $num = $self->{'socketi'}->read($buf,$cnt);
222 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
226 } elsif($line =~ s/^ //) {
228 } elsif($line =~ /^M\b/) {
230 } elsif($line =~ /^Mbinary\b/) {
232 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
234 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
238 my $num = $self->{'socketi'}->read($buf,$cnt);
239 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
246 # print STDERR "S: ok (".length($res).")\n";
248 } elsif($line =~ s/^E //) {
249 # print STDERR "S: $line\n";
251 die "Unknown: $line\n";
257 my($self,$fn,$rev) = @_;
260 if ($self->_file($fn,$rev)) {
261 $res = $self->_line();
262 return $res if defined $res;
267 $self->_file($fn,$rev)
268 or die "No file command send\n";
269 $res = $self->_line();
270 die "No input: $fn $rev\n" unless defined $res;
277 my $cvs = CVSconn
->new($opt_d, $cvs_tree);
282 m
#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
283 or die "Unparseable date: $d\n";
284 my $y=$1; $y-=1900 if $y>1900;
285 return timegm
($6||0,$5,$4,$3,$2-1,$y);
293 for my $x(split(//,$mode)) {
298 } elsif($x eq "u") { $um |= 0700;
299 } elsif($x eq "g") { $um |= 0070;
300 } elsif($x eq "o") { $um |= 0007;
301 } elsif($x eq "r") { $mm |= 0444;
302 } elsif($x eq "w") { $mm |= 0222;
303 } elsif($x eq "x") { $mm |= 0111;
304 } elsif($x eq "=") { # do nothing
305 } else { die "Unknown mode: $mode\n";
312 my $tmpcv = "/var/cache/cvs";
321 or mkdir($git_tree,0777)
322 or die "Could not create $git_tree: $!";
325 my $last_branch = "";
326 my $orig_branch = "";
329 my $git_dir = $ENV{"GIT_DIR"} || ".git";
330 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
331 $ENV{"GIT_DIR"} = $git_dir;
332 unless(-d
$git_dir) {
333 system("git-init-db");
334 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
335 system("git-read-tree");
336 die "Cannot init an empty tree: $?\n" if $?
;
338 $last_branch = $opt_o;
341 $last_branch = basename
(readlink("$git_dir/HEAD"));
342 unless($last_branch) {
343 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
344 $last_branch = "master";
346 $orig_branch = $last_branch;
348 # Get the last import timestamps
349 opendir(D
,"$git_dir/refs/heads");
350 while(defined(my $head = readdir(D
))) {
351 next if $head =~ /^\./;
352 open(F
,"$git_dir/refs/heads/$head")
353 or die "Bad head branch: $head: $!\n";
354 chomp(my $ftag = <F
>);
356 open(F
,"git-cat-file commit $ftag |");
358 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
359 $branch_date{$head} = $1;
368 or die "Could not create git subdir ($git_dir).\n";
370 my $pid = open(CVS
,"-|");
371 die "Cannot fork: $!\n" unless defined $pid;
373 exec("cvsps","-A","--cvs-direct",$cvs_tree);
374 die "Could not start cvsps: $!\n";
379 #---------------------
381 #Date: 1999/09/18 13:03:59
383 #Branch: STABLE-BRANCH-1-0
384 #Ancestor branch: HEAD
387 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
389 # README:1.57->1.57.2.1
390 # VERSION:1.96->1.96.2.1
392 #---------------------
396 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
400 system("git-update-cache","--force-remove","--",@old) if @old;
401 die "Cannot remove files: $?\n" if $?
;
402 system("git-update-cache","--add","--",@new) if @new;
403 die "Cannot add files: $?\n" if $?
;
406 die "Cannot fork: $!" unless defined $pid;
408 exec("git-write-tree");
409 die "Cannot exec git-write-tree: $!\n";
411 chomp(my $tree = <C
>);
413 or die "Cannot get tree id ($tree): $!\n";
415 or die "Error running git-write-tree: $?\n";
416 print "Tree ID $tree\n" if $opt_v;
419 if(open(C
,"$git_dir/refs/heads/$last_branch")) {
420 chomp($parent = <C
>);
422 length($parent) == 40
423 or die "Cannot get parent id ($parent): $!\n";
424 print "Parent ID $parent\n" if $opt_v;
427 my $pr = IO
::Pipe
->new();
428 my $pw = IO
::Pipe
->new();
430 die "Fork: $!\n" unless defined $pid;
434 dup2
($pw->fileno(),0);
435 dup2
($pr->fileno(),1);
440 @par = ("-p",$parent) if $parent;
442 "GIT_AUTHOR_NAME=$author",
443 "GIT_AUTHOR_EMAIL=$author",
444 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
445 "GIT_COMMITTER_NAME=$author",
446 "GIT_COMMITTER_EMAIL=$author",
447 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
448 "git-commit-tree", $tree,@par);
449 die "Cannot exec git-commit-tree: $!\n";
454 or die "Error writing to git-commit-tree: $!\n";
457 print "Committed patch $patchset ($branch)\n" if $opt_v;
458 chomp(my $cid = <$pr>);
460 or die "Cannot get commit id ($cid): $!\n";
461 print "Commit ID $cid\n" if $opt_v;
465 die "Error running git-commit-tree: $?\n" if $?
;
467 open(C
,">$git_dir/refs/heads/$branch")
468 or die "Cannot open branch $branch for update: $!\n";
470 or die "Cannot write branch $branch for update: $!\n";
472 or die "Cannot write branch $branch for update: $!\n";
475 open(C
,">$git_dir/refs/tags/$tag")
476 or die "Cannot create tag $tag: $!\n";
478 or die "Cannot write tag $branch: $!\n";
480 or die "Cannot write tag $branch: $!\n";
481 print "Created tag '$tag' on '$branch'\n" if $opt_v;
490 if($state == 0 and /^-+$/) {
492 } elsif($state == 0) {
495 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
498 } elsif($state == 2 and s/^Date:\s+//) {
501 print STDERR
"Could not parse date: $_\n";
506 } elsif($state == 3 and s/^Author:\s+//) {
510 } elsif($state == 4 and s/^Branch:\s+//) {
514 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
517 $ancestor = $opt_o if $ancestor == "HEAD";
519 } elsif($state == 5) {
523 } elsif($state == 6 and s/^Tag:\s+//) {
531 } elsif($state == 7 and /^Log:/) {
534 } elsif($state == 8 and /^Members:/) {
535 $branch = $opt_o if $branch eq "HEAD";
536 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
538 print "skip patchset $patchset: $date before $branch_date{$branch}\n";
543 if(-f
"$git_dir/refs/heads/$branch") {
544 print STDERR
"Branch $branch already exists!\n";
548 unless(open(H
,"$git_dir/refs/heads/$ancestor")) {
549 print STDERR
"Branch $ancestor does not exist!\n";
555 unless(open(H
,"> $git_dir/refs/heads/$branch")) {
556 print STDERR
"Could not create branch $branch: $!\n";
561 or die "Could not write branch $branch: $!";
563 or die "Could not write branch $branch: $!";
565 if(($ancestor || $branch) ne $last_branch) {
566 print "Switching from $last_branch to $branch\n" if $opt_v;
567 system("git-read-tree","-m","-u","$last_branch","$branch");
568 die "read-tree failed: $?\n" if $?
;
570 if($branch ne $last_branch) {
571 unlink("$git_dir/HEAD");
572 symlink("refs/heads/$branch","$git_dir/HEAD");
573 $last_branch = $branch;
576 } elsif($state == 8) {
578 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
579 # VERSION:1.96->1.96.2.1
580 my $init = ($2 eq "INITIAL");
584 my $data = $cvs->file($fn,$rev);
585 print "".($init ?
"New" : "Update")." $fn: ".length($data)." bytes.\n";
586 mkpath
(dirname
($fn),$opt_v);
588 or die "Cannot create '$fn': $!\n";
590 or die "Cannot write to '$fn': $!\n";
592 or die "Cannot write to '$fn': $!\n";
593 chmod(pmode
($cvs->{'mode'}), $fn);
594 push(@new,$fn); # may be resurrected!
595 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
599 } elsif($state == 9 and /^\s*$/) {
601 } elsif(($state == 9 or $state == 10) and /^-+$/) {
604 } elsif($state == 11 and /^-+$/) {
606 } elsif(/^-+$/) { # end of unknown-line processing
608 } elsif($state != 11) { # ignore stuff when skipping
609 print "* UNKNOWN LINE * $_\n";
612 &$commit() if $branch and $state != 11;
614 # Now switch back to the branch we were in before all of this happened
616 print "DONE; switching back to $orig_branch\n" if $opt_v;
618 $orig_branch = "master";
619 print "DONE; creating $orig_branch branch\n" if $opt_v;
620 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
621 unless -f
"$git_dir/refs/heads/master";
624 system("git-read-tree","-m","-u","$last_branch","$orig_branch");
625 die "read-tree failed: $?\n" if $?
;
627 unlink("$git_dir/HEAD");
628 symlink("refs/heads/$orig_branch","$git_dir/HEAD");