git-fetch: fail if specified refspec does not match remote.
[git/jnareb-git/bp-gitweb.git] / git-cvsimport.perl
blob7bd9136205f46d4334b7272e6617c0f7f553f5e8
1 #!/usr/bin/perl -w
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
8 # its output.
10 # Checking out the files is done by a single long-running CVS connection
11 # / server process.
13 # The head revision is on branch "origin" by default.
14 # You can change that with the '-o' option.
16 use strict;
17 use warnings;
18 use Getopt::Std;
19 use File::Spec;
20 use File::Temp qw(tempfile);
21 use File::Path qw(mkpath);
22 use File::Basename qw(basename dirname);
23 use Time::Local;
24 use IO::Socket;
25 use IO::Pipe;
26 use POSIX qw(strftime dup2);
27 use IPC::Open2;
29 $SIG{'PIPE'}="IGNORE";
30 $ENV{'TZ'}="UTC";
32 our($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M);
34 sub usage() {
35 print STDERR <<END;
36 Usage: ${\basename $0} # fetch/update GIT from CVS
37 [-o branch-for-HEAD] [-h] [-v] [-d CVSROOT]
38 [-p opts-for-cvsps] [-C GIT_repository] [-z fuzz]
39 [-i] [-k] [-u] [-s subst] [-m] [-M regex] [CVS_module]
40 END
41 exit(1);
44 getopts("hivmkuo:d:p:C:z:s:M:P:") or usage();
45 usage if $opt_h;
47 @ARGV <= 1 or usage();
49 if($opt_d) {
50 $ENV{"CVSROOT"} = $opt_d;
51 } elsif(-f 'CVS/Root') {
52 open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root';
53 $opt_d = <$f>;
54 chomp $opt_d;
55 close $f;
56 $ENV{"CVSROOT"} = $opt_d;
57 } elsif($ENV{"CVSROOT"}) {
58 $opt_d = $ENV{"CVSROOT"};
59 } else {
60 die "CVSROOT needs to be set";
62 $opt_o ||= "origin";
63 $opt_s ||= "-";
64 my $git_tree = $opt_C;
65 $git_tree ||= ".";
67 my $cvs_tree;
68 if ($#ARGV == 0) {
69 $cvs_tree = $ARGV[0];
70 } elsif (-f 'CVS/Repository') {
71 open my $f, '<', 'CVS/Repository' or
72 die 'Failed to open CVS/Repository';
73 $cvs_tree = <$f>;
74 chomp $cvs_tree;
75 close $f;
76 } else {
77 usage();
80 our @mergerx = ();
81 if ($opt_m) {
82 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
84 if ($opt_M) {
85 push (@mergerx, qr/$opt_M/);
88 select(STDERR); $|=1; select(STDOUT);
91 package CVSconn;
92 # Basic CVS dialog.
93 # We're only interested in connecting and downloading, so ...
95 use File::Spec;
96 use File::Temp qw(tempfile);
97 use POSIX qw(strftime dup2);
99 sub new {
100 my($what,$repo,$subdir) = @_;
101 $what=ref($what) if ref($what);
103 my $self = {};
104 $self->{'buffer'} = "";
105 bless($self,$what);
107 $repo =~ s#/+$##;
108 $self->{'fullrep'} = $repo;
109 $self->conn();
111 $self->{'subdir'} = $subdir;
112 $self->{'lines'} = undef;
114 return $self;
117 sub conn {
118 my $self = shift;
119 my $repo = $self->{'fullrep'};
120 if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) {
121 my($user,$pass,$serv,$port) = ($1,$2,$3,$4);
122 $user="anonymous" unless defined $user;
123 my $rr2 = "-";
124 unless($port) {
125 $rr2 = ":pserver:$user\@$serv:$repo";
126 $port=2401;
128 my $rr = ":pserver:$user\@$serv:$port$repo";
130 unless($pass) {
131 open(H,$ENV{'HOME'}."/.cvspass") and do {
132 # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z
133 while(<H>) {
134 chomp;
135 s/^\/\d+\s+//;
136 my ($w,$p) = split(/\s/,$_,2);
137 if($w eq $rr or $w eq $rr2) {
138 $pass = $p;
139 last;
144 $pass="A" unless $pass;
146 my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port);
147 die "Socket to $serv: $!\n" unless defined $s;
148 $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n")
149 or die "Write to $serv: $!\n";
150 $s->flush();
152 my $rep = <$s>;
154 if($rep ne "I LOVE YOU\n") {
155 $rep="<unknown>" unless $rep;
156 die "AuthReply: $rep\n";
158 $self->{'socketo'} = $s;
159 $self->{'socketi'} = $s;
160 } else { # local or ext: Fork off our own cvs server.
161 my $pr = IO::Pipe->new();
162 my $pw = IO::Pipe->new();
163 my $pid = fork();
164 die "Fork: $!\n" unless defined $pid;
165 my $cvs = 'cvs';
166 $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER};
167 my $rsh = 'rsh';
168 $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH};
170 my @cvs = ($cvs, 'server');
171 my ($local, $user, $host);
172 $local = $repo =~ s/:local://;
173 if (!$local) {
174 $repo =~ s/:ext://;
175 $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://);
176 ($user, $host) = ($1, $2);
178 if (!$local) {
179 if ($user) {
180 unshift @cvs, $rsh, '-l', $user, $host;
181 } else {
182 unshift @cvs, $rsh, $host;
186 unless($pid) {
187 $pr->writer();
188 $pw->reader();
189 dup2($pw->fileno(),0);
190 dup2($pr->fileno(),1);
191 $pr->close();
192 $pw->close();
193 exec(@cvs);
195 $pw->writer();
196 $pr->reader();
197 $self->{'socketo'} = $pw;
198 $self->{'socketi'} = $pr;
200 $self->{'socketo'}->write("Root $repo\n");
202 # Trial and error says that this probably is the minimum set
203 $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
205 $self->{'socketo'}->write("valid-requests\n");
206 $self->{'socketo'}->flush();
208 chomp(my $rep=$self->readline());
209 if($rep !~ s/^Valid-requests\s*//) {
210 $rep="<unknown>" unless $rep;
211 die "Expected Valid-requests from server, but got: $rep\n";
213 chomp(my $res=$self->readline());
214 die "validReply: $res\n" if $res ne "ok";
216 $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/;
217 $self->{'repo'} = $repo;
220 sub readline {
221 my($self) = @_;
222 return $self->{'socketi'}->getline();
225 sub _file {
226 # Request a file with a given revision.
227 # Trial and error says this is a good way to do it. :-/
228 my($self,$fn,$rev) = @_;
229 $self->{'socketo'}->write("Argument -N\n") or return undef;
230 $self->{'socketo'}->write("Argument -P\n") or return undef;
231 # -kk: Linus' version doesn't use it - defaults to off
232 if ($opt_k) {
233 $self->{'socketo'}->write("Argument -kk\n") or return undef;
235 $self->{'socketo'}->write("Argument -r\n") or return undef;
236 $self->{'socketo'}->write("Argument $rev\n") or return undef;
237 $self->{'socketo'}->write("Argument --\n") or return undef;
238 $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef;
239 $self->{'socketo'}->write("Directory .\n") or return undef;
240 $self->{'socketo'}->write("$self->{'repo'}\n") or return undef;
241 # $self->{'socketo'}->write("Sticky T1.0\n") or return undef;
242 $self->{'socketo'}->write("co\n") or return undef;
243 $self->{'socketo'}->flush() or return undef;
244 $self->{'lines'} = 0;
245 return 1;
247 sub _line {
248 # Read a line from the server.
249 # ... except that 'line' may be an entire file. ;-)
250 my($self, $fh) = @_;
251 die "Not in lines" unless defined $self->{'lines'};
253 my $line;
254 my $res=0;
255 while(defined($line = $self->readline())) {
256 # M U gnupg-cvs-rep/AUTHORS
257 # Updated gnupg-cvs-rep/
258 # /daten/src/rsync/gnupg-cvs-rep/AUTHORS
259 # /AUTHORS/1.1///T1.1
260 # u=rw,g=rw,o=rw
262 # ok
264 if($line =~ s/^(?:Created|Updated) //) {
265 $line = $self->readline(); # path
266 $line = $self->readline(); # Entries line
267 my $mode = $self->readline(); chomp $mode;
268 $self->{'mode'} = $mode;
269 defined (my $cnt = $self->readline())
270 or die "EOF from server after 'Changed'\n";
271 chomp $cnt;
272 die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
273 $line="";
274 $res=0;
275 while($cnt) {
276 my $buf;
277 my $num = $self->{'socketi'}->read($buf,$cnt);
278 die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
279 print $fh $buf;
280 $res += $num;
281 $cnt -= $num;
283 } elsif($line =~ s/^ //) {
284 print $fh $line;
285 $res += length($line);
286 } elsif($line =~ /^M\b/) {
287 # output, do nothing
288 } elsif($line =~ /^Mbinary\b/) {
289 my $cnt;
290 die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline());
291 chomp $cnt;
292 die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
293 $line="";
294 while($cnt) {
295 my $buf;
296 my $num = $self->{'socketi'}->read($buf,$cnt);
297 die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
298 print $fh $buf;
299 $res += $num;
300 $cnt -= $num;
302 } else {
303 chomp $line;
304 if($line eq "ok") {
305 # print STDERR "S: ok (".length($res).")\n";
306 return $res;
307 } elsif($line =~ s/^E //) {
308 # print STDERR "S: $line\n";
309 } elsif($line =~ /^Remove-entry /i) {
310 $line = $self->readline(); # filename
311 $line = $self->readline(); # OK
312 chomp $line;
313 die "Unknown: $line" if $line ne "ok";
314 return -1;
315 } else {
316 die "Unknown: $line\n";
321 sub file {
322 my($self,$fn,$rev) = @_;
323 my $res;
325 my ($fh, $name) = tempfile('gitcvs.XXXXXX',
326 DIR => File::Spec->tmpdir(), UNLINK => 1);
328 $self->_file($fn,$rev) and $res = $self->_line($fh);
330 if (!defined $res) {
331 # retry
332 $self->conn();
333 $self->_file($fn,$rev)
334 or die "No file command send\n";
335 $res = $self->_line($fh);
336 die "No input: $fn $rev\n" unless defined $res;
338 close ($fh);
340 if ($res eq '') {
341 die "Looks like the server has gone away while fetching $fn $rev -- exiting!";
344 return ($name, $res);
348 package main;
350 my $cvs = CVSconn->new($opt_d, $cvs_tree);
353 sub pdate($) {
354 my($d) = @_;
355 m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?#
356 or die "Unparseable date: $d\n";
357 my $y=$1; $y-=1900 if $y>1900;
358 return timegm($6||0,$5,$4,$3,$2-1,$y);
361 sub pmode($) {
362 my($mode) = @_;
363 my $m = 0;
364 my $mm = 0;
365 my $um = 0;
366 for my $x(split(//,$mode)) {
367 if($x eq ",") {
368 $m |= $mm&$um;
369 $mm = 0;
370 $um = 0;
371 } elsif($x eq "u") { $um |= 0700;
372 } elsif($x eq "g") { $um |= 0070;
373 } elsif($x eq "o") { $um |= 0007;
374 } elsif($x eq "r") { $mm |= 0444;
375 } elsif($x eq "w") { $mm |= 0222;
376 } elsif($x eq "x") { $mm |= 0111;
377 } elsif($x eq "=") { # do nothing
378 } else { die "Unknown mode: $mode\n";
381 $m |= $mm&$um;
382 return $m;
385 sub getwd() {
386 my $pwd = `pwd`;
387 chomp $pwd;
388 return $pwd;
392 sub get_headref($$) {
393 my $name = shift;
394 my $git_dir = shift;
395 my $sha;
397 if (open(C,"$git_dir/refs/heads/$name")) {
398 chomp($sha = <C>);
399 close(C);
400 length($sha) == 40
401 or die "Cannot get head id for $name ($sha): $!\n";
403 return $sha;
407 -d $git_tree
408 or mkdir($git_tree,0777)
409 or die "Could not create $git_tree: $!";
410 chdir($git_tree);
412 my $last_branch = "";
413 my $orig_branch = "";
414 my $forward_master = 0;
415 my %branch_date;
417 my $git_dir = $ENV{"GIT_DIR"} || ".git";
418 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
419 $ENV{"GIT_DIR"} = $git_dir;
420 my $orig_git_index;
421 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
422 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
423 DIR => File::Spec->tmpdir());
424 close ($git_ih);
425 $ENV{GIT_INDEX_FILE} = $git_index;
426 unless(-d $git_dir) {
427 system("git-init-db");
428 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
429 system("git-read-tree");
430 die "Cannot init an empty tree: $?\n" if $?;
432 $last_branch = $opt_o;
433 $orig_branch = "";
434 } else {
435 -f "$git_dir/refs/heads/$opt_o"
436 or die "Branch '$opt_o' does not exist.\n".
437 "Either use the correct '-o branch' option,\n".
438 "or import to a new repository.\n";
440 $last_branch = basename(readlink("$git_dir/HEAD"));
441 unless($last_branch) {
442 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
443 $last_branch = "master";
445 $orig_branch = $last_branch;
446 if (-f "$git_dir/CVS2GIT_HEAD") {
447 die <<EOM;
448 CVS2GIT_HEAD exists.
449 Make sure your working directory corresponds to HEAD and remove CVS2GIT_HEAD.
450 You may need to run
452 git-read-tree -m -u CVS2GIT_HEAD HEAD
455 system('cp', "$git_dir/HEAD", "$git_dir/CVS2GIT_HEAD");
457 $forward_master =
458 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
459 system('cmp', '-s', "$git_dir/refs/heads/master",
460 "$git_dir/refs/heads/$opt_o") == 0;
462 # populate index
463 system('git-read-tree', $last_branch);
464 die "read-tree failed: $?\n" if $?;
466 # Get the last import timestamps
467 opendir(D,"$git_dir/refs/heads");
468 while(defined(my $head = readdir(D))) {
469 next if $head =~ /^\./;
470 open(F,"$git_dir/refs/heads/$head")
471 or die "Bad head branch: $head: $!\n";
472 chomp(my $ftag = <F>);
473 close(F);
474 open(F,"git-cat-file commit $ftag |");
475 while(<F>) {
476 next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/;
477 $branch_date{$head} = $1;
478 last;
480 close(F);
482 closedir(D);
485 -d $git_dir
486 or die "Could not create git subdir ($git_dir).\n";
488 my $pid = open(CVS,"-|");
489 die "Cannot fork: $!\n" unless defined $pid;
490 unless($pid) {
491 my @opt;
492 @opt = split(/,/,$opt_p) if defined $opt_p;
493 unshift @opt, '-z', $opt_z if defined $opt_z;
494 unshift @opt, '-q' unless defined $opt_v;
495 unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) {
496 push @opt, '--cvs-direct';
498 if ($opt_P) {
499 exec("cat", $opt_P);
500 } else {
501 exec("cvsps",@opt,"-u","-A",'--root',$opt_d,$cvs_tree);
502 die "Could not start cvsps: $!\n";
507 ## cvsps output:
508 #---------------------
509 #PatchSet 314
510 #Date: 1999/09/18 13:03:59
511 #Author: wkoch
512 #Branch: STABLE-BRANCH-1-0
513 #Ancestor branch: HEAD
514 #Tag: (none)
515 #Log:
516 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
517 #Members:
518 # README:1.57->1.57.2.1
519 # VERSION:1.96->1.96.2.1
521 #---------------------
523 my $state = 0;
525 my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
526 my(@old,@new);
527 my $commit = sub {
528 my $pid;
529 while(@old) {
530 my @o2;
531 if(@old > 55) {
532 @o2 = splice(@old,0,50);
533 } else {
534 @o2 = @old;
535 @old = ();
537 system("git-update-index","--force-remove","--",@o2);
538 die "Cannot remove files: $?\n" if $?;
540 while(@new) {
541 my @n2;
542 if(@new > 12) {
543 @n2 = splice(@new,0,10);
544 } else {
545 @n2 = @new;
546 @new = ();
548 system("git-update-index","--add",
549 (map { ('--cacheinfo', @$_) } @n2));
550 die "Cannot add files: $?\n" if $?;
553 $pid = open(C,"-|");
554 die "Cannot fork: $!" unless defined $pid;
555 unless($pid) {
556 exec("git-write-tree");
557 die "Cannot exec git-write-tree: $!\n";
559 chomp(my $tree = <C>);
560 length($tree) == 40
561 or die "Cannot get tree id ($tree): $!\n";
562 close(C)
563 or die "Error running git-write-tree: $?\n";
564 print "Tree ID $tree\n" if $opt_v;
566 my $parent = "";
567 if(open(C,"$git_dir/refs/heads/$last_branch")) {
568 chomp($parent = <C>);
569 close(C);
570 length($parent) == 40
571 or die "Cannot get parent id ($parent): $!\n";
572 print "Parent ID $parent\n" if $opt_v;
575 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
576 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
577 $pid = fork();
578 die "Fork: $!\n" unless defined $pid;
579 unless($pid) {
580 $pr->writer();
581 $pw->reader();
582 open(OUT,">&STDOUT");
583 dup2($pw->fileno(),0);
584 dup2($pr->fileno(),1);
585 $pr->close();
586 $pw->close();
588 my @par = ();
589 @par = ("-p",$parent) if $parent;
591 # loose detection of merges
592 # based on the commit msg
593 foreach my $rx (@mergerx) {
594 if ($logmsg =~ $rx) {
595 my $mparent = $1;
596 if ($mparent eq 'HEAD') { $mparent = $opt_o };
597 if ( -e "$git_dir/refs/heads/$mparent") {
598 $mparent = get_headref($mparent, $git_dir);
599 push @par, '-p', $mparent;
600 print OUT "Merge parent branch: $mparent\n" if $opt_v;
605 exec("env",
606 "GIT_AUTHOR_NAME=$author_name",
607 "GIT_AUTHOR_EMAIL=$author_email",
608 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
609 "GIT_COMMITTER_NAME=$author_name",
610 "GIT_COMMITTER_EMAIL=$author_email",
611 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
612 "git-commit-tree", $tree,@par);
613 die "Cannot exec git-commit-tree: $!\n";
615 $pw->writer();
616 $pr->reader();
618 # compatibility with git2cvs
619 substr($logmsg,32767) = "" if length($logmsg) > 32767;
620 $logmsg =~ s/[\s\n]+\z//;
622 print $pw "$logmsg\n"
623 or die "Error writing to git-commit-tree: $!\n";
624 $pw->close();
626 print "Committed patch $patchset ($branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
627 chomp(my $cid = <$pr>);
628 length($cid) == 40
629 or die "Cannot get commit id ($cid): $!\n";
630 print "Commit ID $cid\n" if $opt_v;
631 $pr->close();
633 waitpid($pid,0);
634 die "Error running git-commit-tree: $?\n" if $?;
636 open(C,">$git_dir/refs/heads/$branch")
637 or die "Cannot open branch $branch for update: $!\n";
638 print C "$cid\n"
639 or die "Cannot write branch $branch for update: $!\n";
640 close(C)
641 or die "Cannot write branch $branch for update: $!\n";
643 if($tag) {
644 my($in, $out) = ('','');
645 my($xtag) = $tag;
646 $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY **
647 $xtag =~ tr/_/\./ if ( $opt_u );
649 my $pid = open2($in, $out, 'git-mktag');
650 print $out "object $cid\n".
651 "type commit\n".
652 "tag $xtag\n".
653 "tagger $author_name <$author_email>\n"
654 or die "Cannot create tag object $xtag: $!\n";
655 close($out)
656 or die "Cannot create tag object $xtag: $!\n";
658 my $tagobj = <$in>;
659 chomp $tagobj;
661 if ( !close($in) or waitpid($pid, 0) != $pid or
662 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
663 die "Cannot create tag object $xtag: $!\n";
667 open(C,">$git_dir/refs/tags/$xtag")
668 or die "Cannot create tag $xtag: $!\n";
669 print C "$tagobj\n"
670 or die "Cannot write tag $xtag: $!\n";
671 close(C)
672 or die "Cannot write tag $xtag: $!\n";
674 print "Created tag '$xtag' on '$branch'\n" if $opt_v;
678 while(<CVS>) {
679 chomp;
680 if($state == 0 and /^-+$/) {
681 $state = 1;
682 } elsif($state == 0) {
683 $state = 1;
684 redo;
685 } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) {
686 $patchset = 0+$_;
687 $state=2;
688 } elsif($state == 2 and s/^Date:\s+//) {
689 $date = pdate($_);
690 unless($date) {
691 print STDERR "Could not parse date: $_\n";
692 $state=0;
693 next;
695 $state=3;
696 } elsif($state == 3 and s/^Author:\s+//) {
697 s/\s+$//;
698 if (/^(.*?)\s+<(.*)>/) {
699 ($author_name, $author_email) = ($1, $2);
700 } else {
701 $author_name = $author_email = $_;
703 $state = 4;
704 } elsif($state == 4 and s/^Branch:\s+//) {
705 s/\s+$//;
706 s/[\/]/$opt_s/g;
707 $branch = $_;
708 $state = 5;
709 } elsif($state == 5 and s/^Ancestor branch:\s+//) {
710 s/\s+$//;
711 $ancestor = $_;
712 $ancestor = $opt_o if $ancestor eq "HEAD";
713 $state = 6;
714 } elsif($state == 5) {
715 $ancestor = undef;
716 $state = 6;
717 redo;
718 } elsif($state == 6 and s/^Tag:\s+//) {
719 s/\s+$//;
720 if($_ eq "(none)") {
721 $tag = undef;
722 } else {
723 $tag = $_;
725 $state = 7;
726 } elsif($state == 7 and /^Log:/) {
727 $logmsg = "";
728 $state = 8;
729 } elsif($state == 8 and /^Members:/) {
730 $branch = $opt_o if $branch eq "HEAD";
731 if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) {
732 # skip
733 print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v;
734 $state = 11;
735 next;
737 if($ancestor) {
738 if(-f "$git_dir/refs/heads/$branch") {
739 print STDERR "Branch $branch already exists!\n";
740 $state=11;
741 next;
743 unless(open(H,"$git_dir/refs/heads/$ancestor")) {
744 print STDERR "Branch $ancestor does not exist!\n";
745 $state=11;
746 next;
748 chomp(my $id = <H>);
749 close(H);
750 unless(open(H,"> $git_dir/refs/heads/$branch")) {
751 print STDERR "Could not create branch $branch: $!\n";
752 $state=11;
753 next;
755 print H "$id\n"
756 or die "Could not write branch $branch: $!";
757 close(H)
758 or die "Could not write branch $branch: $!";
760 if(($ancestor || $branch) ne $last_branch) {
761 print "Switching from $last_branch to $branch\n" if $opt_v;
762 system("git-read-tree", $branch);
763 die "read-tree failed: $?\n" if $?;
765 $last_branch = $branch if $branch ne $last_branch;
766 $state = 9;
767 } elsif($state == 8) {
768 $logmsg .= "$_\n";
769 } elsif($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) {
770 # VERSION:1.96->1.96.2.1
771 my $init = ($2 eq "INITIAL");
772 my $fn = $1;
773 my $rev = $3;
774 $fn =~ s#^/+##;
775 my ($tmpname, $size) = $cvs->file($fn,$rev);
776 if($size == -1) {
777 push(@old,$fn);
778 print "Drop $fn\n" if $opt_v;
779 } else {
780 print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v;
781 open my $F, '-|', "git-hash-object -w $tmpname"
782 or die "Cannot create object: $!\n";
783 my $sha = <$F>;
784 chomp $sha;
785 close $F;
786 my $mode = pmode($cvs->{'mode'});
787 push(@new,[$mode, $sha, $fn]); # may be resurrected!
789 unlink($tmpname);
790 } elsif($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
791 my $fn = $1;
792 $fn =~ s#^/+##;
793 push(@old,$fn);
794 print "Delete $fn\n" if $opt_v;
795 } elsif($state == 9 and /^\s*$/) {
796 $state = 10;
797 } elsif(($state == 9 or $state == 10) and /^-+$/) {
798 &$commit();
799 $state = 1;
800 } elsif($state == 11 and /^-+$/) {
801 $state = 1;
802 } elsif(/^-+$/) { # end of unknown-line processing
803 $state = 1;
804 } elsif($state != 11) { # ignore stuff when skipping
805 print "* UNKNOWN LINE * $_\n";
808 &$commit() if $branch and $state != 11;
810 unlink($git_index);
812 if (defined $orig_git_index) {
813 $ENV{GIT_INDEX_FILE} = $orig_git_index;
814 } else {
815 delete $ENV{GIT_INDEX_FILE};
818 # Now switch back to the branch we were in before all of this happened
819 if($orig_branch) {
820 print "DONE\n" if $opt_v;
821 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
822 if $forward_master;
823 unless ($opt_i) {
824 system('git-read-tree', '-m', '-u', 'CVS2GIT_HEAD', 'HEAD');
825 die "read-tree failed: $?\n" if $?;
827 } else {
828 $orig_branch = "master";
829 print "DONE; creating $orig_branch branch\n" if $opt_v;
830 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
831 unless -f "$git_dir/refs/heads/master";
832 unlink("$git_dir/HEAD");
833 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
834 unless ($opt_i) {
835 system('git checkout');
836 die "checkout failed: $?\n" if $?;
839 unlink("$git_dir/CVS2GIT_HEAD");