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 ...
65 my($what,$repo,$subdir) = @_;
66 $what=ref($what) if ref($what);
69 $self->{'buffer'} = "";
73 $self->{'fullrep'} = $repo;
76 $self->{'subdir'} = $subdir;
77 $self->{'lines'} = undef;
84 my $repo = $self->{'fullrep'};
85 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
86 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
87 $user="anonymous" unless defined $user;
90 $rr2 = ":pserver:$user\@$serv:$repo";
93 my $rr = ":pserver:$user\@$serv:$port$repo";
96 open(H,$ENV{'HOME'}."/.cvspass") and do {
97 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
101 my ($w,$p) = split(/\s/,$_,2);
102 if($w eq $rr or $w eq $rr2) {
109 $pass="A" unless $pass;
111 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
112 die "Socket to $serv: $!\n" unless defined $s;
113 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
114 or die "Write to $serv: $!\n";
119 if($rep ne "I LOVE YOU\n") {
120 $rep="<unknown>" unless $rep;
121 die "AuthReply: $rep\n";
123 $self->{'socketo'} = $s;
124 $self->{'socketi'} = $s;
125 } else { # local: Fork off our own cvs server.
126 my $pr = IO::Pipe->new();
127 my $pw = IO::Pipe->new();
129 die "Fork: $!\n" unless defined $pid;
133 dup2($pw->fileno(),0);
134 dup2($pr->fileno(),1);
137 exec("cvs","server");
141 $self->{'socketo'} = $pw;
142 $self->{'socketi'} = $pr;
144 $self->{'socketo'}->write("Root $repo\n");
146 # Trial and error says that this probably is the minimum set
147 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
149 $self->{'socketo'}->write("valid-requests\n");
150 $self->{'socketo'}->flush();
152 chomp(my $rep=$self->readline());
153 if($rep !~ s/^Valid-requests\s*//) {
154 $rep="<unknown>" unless $rep;
155 die "Expected Valid-requests from server, but got: $rep\n";
157 chomp(my $res=$self->readline());
158 die "validReply: $res\n" if $res ne "ok";
160 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
161 $self->{'repo'} = $repo;
166 return $self->{'socketi'}->getline();
170 # Request a file with a given revision.
171 # Trial and error says this is a good way to do it. :-/
172 my($self,$fn,$rev) = @_;
173 $self->{'socketo'}->write("Argument -N\n") or return undef;
174 $self->{'socketo'}->write("Argument -P\n") or return undef;
175 # $self->{'socketo'}->write("Argument -ko\n") or return undef;
176 # -ko: Linus' version doesn't use it
177 $self->{'socketo'}->write("Argument -r\n") or return undef;
178 $self->{'socketo'}->write("Argument $rev\n") or return undef;
179 $self->{'socketo'}->write("Argument --\n") or return undef;
180 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
181 $self->{'socketo'}->write("Directory .\n") or return undef;
182 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
183 $self->{'socketo'}->write("Sticky T1.1\n") or return undef;
184 $self->{'socketo'}->write("co\n") or return undef;
185 $self->{'socketo'}->flush() or return undef;
186 $self->{'lines'} = 0;
190 # Read a line from the server.
191 # ... except that 'line' may be an entire file. ;-)
193 die "Not in lines" unless defined $self->{'lines'};
197 while(defined($line = $self->readline())) {
198 # M U gnupg-cvs-rep/AUTHORS
199 # Updated gnupg-cvs-rep/
200 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
201 # /AUTHORS/1.1///T1.1
206 if($line =~ s/^(?:Created|Updated) //) {
207 $line = $self->readline(); # path
208 $line = $self->readline(); # Entries line
209 my $mode = $self->readline(); chomp $mode;
210 $self->{'mode'} = $mode;
211 defined (my $cnt = $self->readline())
212 or die "EOF from server after 'Changed'\n";
214 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
219 my $num = $self->{'socketi'}->read($buf,$cnt);
220 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
224 } elsif($line =~ s/^ //) {
226 } elsif($line =~ /^M\b/) {
228 } elsif($line =~ /^Mbinary\b/) {
230 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
232 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
236 my $num = $self->{'socketi'}->read($buf,$cnt);
237 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
244 # print STDERR "S: ok (".length($res).")\n";
246 } elsif($line =~ s/^E //) {
247 # print STDERR "S: $line\n";
249 die "Unknown: $line\n";
255 my($self,$fn,$rev) = @_;
258 if ($self->_file($fn,$rev)) {
259 $res = $self->_line();
260 return $res if defined $res;
265 $self->_file($fn,$rev)
266 or die "No file command send\n";
267 $res = $self->_line();
268 die "No input: $fn $rev\n" unless defined $res;
275 my $cvs = CVSconn->new($opt_d, $cvs_tree);
280 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
281 or die "Unparseable date: $d\n";
282 my $y=$1; $y-=1900 if $y>1900;
283 return timegm($6||0,$5,$4,$3,$2-1,$y);
291 for my $x(split(//,$mode)) {
296 } elsif($x eq "u") { $um |= 0700;
297 } elsif($x eq "g") { $um |= 0070;
298 } elsif($x eq "o") { $um |= 0007;
299 } elsif($x eq "r") { $mm |= 0444;
300 } elsif($x eq "w") { $mm |= 0222;
301 } elsif($x eq "x") { $mm |= 0111;
302 } elsif($x eq "=") { # do nothing
303 } else { die "Unknown mode: $mode\n";
310 my $tmpcv = "/var/cache/cvs";
319 or mkdir($git_tree,0777)
320 or die "Could not create $git_tree: $!";
323 my $last_branch = "";
326 my $git_dir = $ENV{"GIT_DIR"} || ".git";
327 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
328 $ENV{"GIT_DIR"} = $git_dir;
329 unless(-d $git_dir) {
330 system("git-init-db");
331 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
332 system("git-read-tree");
333 die "Cannot init an empty tree: $?\n" if $?;
335 $last_branch = $opt_o;
337 $last_branch = basename(readlink("$git_dir/HEAD"));
338 die "Cannot read the last branch name: $!\n" unless $last_branch;
340 # Get the last import timestamps
341 opendir(D,"$git_dir/refs/heads");
342 while(defined(my $head = readdir(D))) {
343 next if $head =~ /^\./;
344 open(F,"$git_dir/refs/heads/$head")
345 or die "Bad head branch: $head: $!\n";
346 chomp(my $ftag = <F>);
348 open(F,"git-cat-file commit $ftag |");
350 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
351 $branch_date{$head} = $1;
360 or die "Could not create git subdir ($git_dir).\n";
362 my $pid = open(CVS,"-|");
363 die "Cannot fork: $!\n" unless defined $pid;
365 exec("cvsps","-A","--cvs-direct",$cvs_tree);
366 die "Could not start cvsps: $!\n";
371 #---------------------
373 #Date: 1999/09/18 13:03:59
375 #Branch: STABLE-BRANCH-1-0
376 #Ancestor branch: HEAD
379 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
381 # README:1.57->1.57.2.1
382 # VERSION:1.96->1.96.2.1
384 #---------------------
388 my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg);
392 system("git-update-cache","--force-remove","--",@old) if @old;
393 die "Cannot remove files: $?\n" if $?;
394 system("git-update-cache","--add","--",@new) if @new;
395 die "Cannot add files: $?\n" if $?;
398 die "Cannot fork: $!" unless defined $pid;
400 exec("git-write-tree");
401 die "Cannot exec git-write-tree: $!\n";
403 chomp(my $tree = <C>);
405 or die "Cannot get tree id ($tree): $!\n";
407 or die "Error running git-write-tree: $?\n";
408 print "Tree ID $tree\n" if $opt_v;
411 if(open(C,"$git_dir/refs/heads/$last_branch")) {
412 chomp($parent = <C>);
414 length($parent) == 40
415 or die "Cannot get parent id ($parent): $!\n";
416 print "Parent ID $parent\n" if $opt_v;
419 my $pr = IO::Pipe->new();
420 my $pw = IO::Pipe->new();
422 die "Fork: $!\n" unless defined $pid;
426 dup2($pw->fileno(),0);
427 dup2($pr->fileno(),1);
432 @par = ("-p",$parent) if $parent;
434 "GIT_AUTHOR_NAME=$author",
435 "GIT_AUTHOR_EMAIL=$author",
436 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
437 "GIT_COMMITTER_NAME=$author",
438 "GIT_COMMITTER_EMAIL=$author",
439 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
440 "git-commit-tree", $tree,@par);
441 die "Cannot exec git-commit-tree: $!\n";
446 or die "Error writing to git-commit-tree: $!\n";
449 print "Committed patch $patchset ($branch)\n" if $opt_v;
450 chomp(my $cid = <$pr>);
452 or die "Cannot get commit id ($cid): $!\n";
453 print "Commit ID $cid\n" if $opt_v;
457 die "Error running git-commit-tree: $?\n" if $?;
459 open(C,">$git_dir/refs/heads/$branch")
460 or die "Cannot open branch $branch for update: $!\n";
462 or die "Cannot write branch $branch for update: $!\n";
464 or die "Cannot write branch $branch for update: $!\n";
467 open(C,">$git_dir/refs/tags/$tag")
468 or die "Cannot create tag $tag: $!\n";
470 or die "Cannot write tag $branch: $!\n";
472 or die "Cannot write tag $branch: $!\n";
473 print "Created tag '$tag' on '$branch'\n" if $opt_v;
482 if($state == 0 and /^-+$/) {
484 } elsif($state == 0) {
487 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
490 } elsif($state == 2 and s/^Date:\s+//) {
493 print STDERR "Could not parse date: $_\n";
498 } elsif($state == 3 and s/^Author:\s+//) {
502 } elsif($state == 4 and s/^Branch:\s+//) {
506 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
509 $ancestor = $opt_o if $ancestor == "HEAD";
511 } elsif($state == 5) {
515 } elsif($state == 6 and s/^Tag:\s+//) {
523 } elsif($state == 7 and /^Log:/) {
526 } elsif($state == 8 and /^Members:/) {
527 $branch = $opt_o if $branch eq "HEAD";
528 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
530 print "skip patchset $patchset: $date before $branch_date{$branch}\n";
535 if(-f "$git_dir/refs/heads/$branch") {
536 print STDERR "Branch $branch already exists!\n";
540 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
541 print STDERR "Branch $ancestor does not exist!\n";
547 unless(open(H,"> $git_dir/refs/heads/$branch")) {
548 print STDERR "Could not create branch $branch: $!\n";
553 or die "Could not write branch $branch: $!";
555 or die "Could not write branch $branch: $!";
557 if(($ancestor || $branch) ne $last_branch) {
558 print "Switching from $last_branch to $branch\n" if $opt_v;
559 system("git-read-tree -m -u $last_branch $branch");
560 die "read-tree $branch failed at $branch: $?\n" if $?;
562 if($branch ne $last_branch) {
563 unlink("$git_dir/HEAD");
564 symlink("refs/heads/$branch","$git_dir/HEAD");
565 $last_branch = $branch;
568 } elsif($state == 8) {
570 } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) {
571 # VERSION:1.96->1.96.2.1
572 my $init = ($2 eq "INITIAL");
574 my $data = $cvs->file($fn,$3);
575 print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n";
576 mkpath(dirname($fn),$opt_v);
578 or die "Cannot create '$fn': $!\n";
580 or die "Cannot write to '$fn': $!\n";
582 or die "Cannot write to '$fn': $!\n";
583 chmod(pmode($cvs->{'mode'}), $fn);
584 push(@new,$fn); # may be resurrected!
585 } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
587 } elsif($state == 9 and /^\s*$/) {
589 } elsif(($state == 9 or $state == 10) and /^-+$/) {
592 } elsif($state == 11 and /^-+$/) {
594 } elsif(/^-+$/) { # end of unknown-line processing
596 } elsif($state != 11) { # ignore stuff when skipping
597 print "* UNKNOWN LINE * $_\n";
600 &$commit() if $branch and $state != 11;
602 print "DONE\n" if $opt_v;